33import random
44import shared
55import socks
6- import socket
6+ from i2p import socket
77import sys
88import tr
99
@@ -31,7 +31,12 @@ def _getPeer(self):
3131 peer = shared .trustedPeer
3232 shared .knownNodes [self .streamNumber ][peer ] = time .time ()
3333 else :
34- peer , = random .sample (shared .knownNodes [self .streamNumber ], 1 )
34+ try :
35+ peer , = random .sample (shared .knownNodes [self .streamNumber ], 1 )
36+ except ValueError :
37+ with shared .printLock :
38+ print "knownNodes is empty. Cannot _getPeer"
39+ return None
3540 shared .knownNodesLock .release ()
3641
3742 return peer
@@ -47,8 +52,10 @@ def run(self):
4752 break
4853 random .seed ()
4954 peer = self ._getPeer ()
55+ if not peer :
56+ break
5057 shared .alreadyAttemptedConnectionsListLock .acquire ()
51- while peer in shared .alreadyAttemptedConnectionsList or peer .host in shared .connectedHostsList :
58+ while peer in shared .alreadyAttemptedConnectionsList or peer .dest in shared .connectedHostsList :
5259 shared .alreadyAttemptedConnectionsListLock .release ()
5360 # print 'choosing new sample'
5461 random .seed ()
@@ -64,12 +71,9 @@ def run(self):
6471 shared .alreadyAttemptedConnectionsListLock .acquire ()
6572 shared .alreadyAttemptedConnectionsList [peer ] = 0
6673 shared .alreadyAttemptedConnectionsListLock .release ()
67- if peer .host .find (':' ) == - 1 :
68- address_family = socket .AF_INET
69- else :
70- address_family = socket .AF_INET6
74+
7175 try :
72- sock = socks .socksocket (address_family , socket .SOCK_STREAM )
76+ sock = socks .socksocket (shared . i2psession , socket .SOCK_STREAM )
7377 except :
7478 """
7579 The line can fail on Windows systems which aren't
@@ -91,65 +95,20 @@ def run(self):
9195 continue
9296 # This option apparently avoids the TIME_WAIT state so that we
9397 # can rebind faster
94- sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
98+ # sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
9599 sock .settimeout (20 )
96100 if shared .config .get ('bitmessagesettings' , 'socksproxytype' ) == 'none' and shared .verbose >= 2 :
97101 with shared .printLock :
98102 print 'Trying an outgoing connection to' , peer
99103
100- # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
101- elif shared .config .get ('bitmessagesettings' , 'socksproxytype' ) == 'SOCKS4a' :
102- if shared .verbose >= 2 :
103- with shared .printLock :
104- print '(Using SOCKS4a) Trying an outgoing connection to' , peer
105-
106- proxytype = socks .PROXY_TYPE_SOCKS4
107- sockshostname = shared .config .get (
108- 'bitmessagesettings' , 'sockshostname' )
109- socksport = shared .config .getint (
110- 'bitmessagesettings' , 'socksport' )
111- rdns = True # Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
112- if shared .config .getboolean ('bitmessagesettings' , 'socksauthentication' ):
113- socksusername = shared .config .get (
114- 'bitmessagesettings' , 'socksusername' )
115- sockspassword = shared .config .get (
116- 'bitmessagesettings' , 'sockspassword' )
117- sock .setproxy (
118- proxytype , sockshostname , socksport , rdns , socksusername , sockspassword )
119- else :
120- sock .setproxy (
121- proxytype , sockshostname , socksport , rdns )
122- elif shared .config .get ('bitmessagesettings' , 'socksproxytype' ) == 'SOCKS5' :
123- if shared .verbose >= 2 :
124- with shared .printLock :
125- print '(Using SOCKS5) Trying an outgoing connection to' , peer
126-
127- proxytype = socks .PROXY_TYPE_SOCKS5
128- sockshostname = shared .config .get (
129- 'bitmessagesettings' , 'sockshostname' )
130- socksport = shared .config .getint (
131- 'bitmessagesettings' , 'socksport' )
132- rdns = True # Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
133- if shared .config .getboolean ('bitmessagesettings' , 'socksauthentication' ):
134- socksusername = shared .config .get (
135- 'bitmessagesettings' , 'socksusername' )
136- sockspassword = shared .config .get (
137- 'bitmessagesettings' , 'sockspassword' )
138- sock .setproxy (
139- proxytype , sockshostname , socksport , rdns , socksusername , sockspassword )
140- else :
141- sock .setproxy (
142- proxytype , sockshostname , socksport , rdns )
143-
144104 try :
145- sock .connect (( peer .host , peer . port ) )
105+ sock .connect (peer .dest )
146106 rd = receiveDataThread ()
147107 rd .daemon = True # close the main program even if there are threads left
148108 someObjectsOfWhichThisRemoteNodeIsAlreadyAware = {} # This is not necessairly a complete list; we clear it from time to time to save memory.
149109 sendDataThreadQueue = Queue .Queue () # Used to submit information to the send data thread for this connection.
150110 rd .setup (sock ,
151- peer .host ,
152- peer .port ,
111+ peer .dest ,
153112 self .streamNumber ,
154113 someObjectsOfWhichThisRemoteNodeIsAlreadyAware ,
155114 self .selfInitiatedConnections ,
@@ -160,7 +119,7 @@ def run(self):
160119
161120
162121 sd = sendDataThread (sendDataThreadQueue )
163- sd .setup (sock , peer .host , peer . port , self .streamNumber ,
122+ sd .setup (sock , peer .dest , self .streamNumber ,
164123 someObjectsOfWhichThisRemoteNodeIsAlreadyAware )
165124 sd .start ()
166125 sd .sendVersionMessage ()
@@ -189,22 +148,10 @@ def run(self):
189148 with shared .printLock :
190149 print 'deleting' , peer , 'from shared.knownNodes because it is more than 48 hours old and we could not connect to it.'
191150
192- except socks .Socks5AuthError as err :
193- shared .UISignalQueue .put ((
194- 'updateStatusBar' , tr .translateText (
195- "MainWindow" , "SOCKS5 Authentication problem: %1" ).arg (str (err ))))
196- except socks .Socks5Error as err :
197- pass
198- print 'SOCKS5 error. (It is possible that the server wants authentication).)' , str (err )
199- except socks .Socks4Error as err :
200- print 'Socks4Error:' , err
201- except socket .error as err :
202- if shared .config .get ('bitmessagesettings' , 'socksproxytype' )[0 :5 ] == 'SOCKS' :
203- print 'Bitmessage MIGHT be having trouble connecting to the SOCKS server. ' + str (err )
204- else :
205- if shared .verbose >= 1 :
206- with shared .printLock :
207- print 'Could NOT connect to' , peer , 'during outgoing attempt.' , err
151+ except socket .Error as err :
152+ if shared .verbose >= 1 :
153+ with shared .printLock :
154+ print 'Could NOT connect to' , peer , 'during outgoing attempt.' , err
208155
209156 deletedPeer = None
210157 with shared .knownNodesLock :
@@ -231,3 +178,4 @@ def run(self):
231178 import traceback
232179 traceback .print_exc ()
233180 time .sleep (0.1 )
181+
0 commit comments