@@ -27,6 +27,9 @@ def setup(self, selfInitiatedConnections):
2727
2828 def _createListenSocket (self , family ):
2929 HOST = '' # Symbolic name meaning all available interfaces
30+ # If not sockslisten, but onionhostname defined, only listen on localhost
31+ if not shared .safeConfigGetBoolean ('bitmessagesettings' , 'sockslisten' ) and ".onion" in shared .config .get ('bitmessagesettings' , 'onionhostname' ):
32+ HOST = shared .config .get ('bitmessagesettings' , 'onionbindip' )
3033 PORT = shared .config .getint ('bitmessagesettings' , 'port' )
3134 sock = socket .socket (family , socket .SOCK_STREAM )
3235 if family == socket .AF_INET6 :
@@ -43,12 +46,14 @@ def _createListenSocket(self, family):
4346 def stopThread (self ):
4447 super (singleListener , self ).stopThread ()
4548 s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
46- try :
47- s .connect (('127.0.0.1' , shared .config .getint ('bitmessagesettings' , 'port' )))
48- s .shutdown (socket .SHUT_RDWR )
49- s .close ()
50- except :
51- pass
49+ for ip in ('127.0.0.1' , shared .config .get ('bitmessagesettings' , 'onionbindip' )):
50+ try :
51+ s .connect ((ip , shared .config .getint ('bitmessagesettings' , 'port' )))
52+ s .shutdown (socket .SHUT_RDWR )
53+ s .close ()
54+ break
55+ except :
56+ pass
5257
5358 def run (self ):
5459 # If there is a trusted peer then we don't want to accept
@@ -62,8 +67,12 @@ def run(self):
6267 # We typically don't want to accept incoming connections if the user is using a
6368 # SOCKS proxy, unless they have configured otherwise. If they eventually select
6469 # proxy 'none' or configure SOCKS listening then this will start listening for
65- # connections.
66- while shared .config .get ('bitmessagesettings' , 'socksproxytype' )[0 :5 ] == 'SOCKS' and not shared .config .getboolean ('bitmessagesettings' , 'sockslisten' ) and shared .shutdown == 0 :
70+ # connections. But if on SOCKS and have an onionhostname, listen
71+ # (socket is then only opened for localhost)
72+ while shared .config .get ('bitmessagesettings' , 'socksproxytype' )[0 :5 ] == 'SOCKS' and \
73+ (not shared .config .getboolean ('bitmessagesettings' , 'sockslisten' ) and \
74+ ".onion" not in shared .config .get ('bitmessagesettings' , 'onionhostname' )) and \
75+ shared .shutdown == 0 :
6776 self .stop .wait (5 )
6877
6978 logger .info ('Listening for incoming connections.' )
@@ -77,6 +86,7 @@ def run(self):
7786 if (isinstance (e .args , tuple ) and
7887 e .args [0 ] in (errno .EAFNOSUPPORT ,
7988 errno .EPFNOSUPPORT ,
89+ errno .EADDRNOTAVAIL ,
8090 errno .ENOPROTOOPT )):
8191 sock = self ._createListenSocket (socket .AF_INET )
8292 else :
@@ -90,7 +100,7 @@ def run(self):
90100 # SOCKS proxy, unless they have configured otherwise. If they eventually select
91101 # proxy 'none' or configure SOCKS listening then this will start listening for
92102 # connections.
93- while shared .config .get ('bitmessagesettings' , 'socksproxytype' )[0 :5 ] == 'SOCKS' and not shared .config .getboolean ('bitmessagesettings' , 'sockslisten' ) and shared .shutdown == 0 :
103+ while shared .config .get ('bitmessagesettings' , 'socksproxytype' )[0 :5 ] == 'SOCKS' and not shared .config .getboolean ('bitmessagesettings' , 'sockslisten' ) and ".onion" not in shared . config . get ( 'bitmessagesettings' , 'onionhostname' ) and shared .shutdown == 0 :
94104 self .stop .wait (10 )
95105 while len (shared .connectedHostsList ) > 220 and shared .shutdown == 0 :
96106 logger .info ('We are connected to too many people. Not accepting further incoming connections for ten seconds.' )
@@ -112,7 +122,9 @@ def run(self):
112122 # is already connected because the two computers will
113123 # share the same external IP. This is here to prevent
114124 # connection flooding.
115- if HOST in shared .connectedHostsList :
125+ # permit repeated connections from Tor
126+ # FIXME: sockshostname may be a hostname rather than IP, in such a case this will break
127+ if HOST in shared .connectedHostsList and (".onion" not in shared .config .get ('bitmessagesettings' , 'onionhostname' ) or HOST != shared .config .get ('bitmessagesettings' , 'sockshostname' )):
116128 socketObject .close ()
117129 logger .info ('We are already connected to ' + str (HOST ) + '. Ignoring connection.' )
118130 else :
0 commit comments