MCPcopy Index your code
hub / github.com/Bitmessage/PyBitmessage / __connectsocks5

Method __connectsocks5

src/socks/__init__.py:211–257  ·  view source on GitHub ↗
(self, destaddr, destport)

Source from the content-addressed store, hash-verified

209 raise GeneralProxyError((1, _generalerrors[1]))
210
211 def __connectsocks5(self, destaddr, destport):
212 # Now we can request the actual connection
213 req = struct.pack('BBB', 0x05, 0x01, 0x00)
214 # If the given destination address is an IP address, we'll
215 # use the IPv4 address request even if remote resolving was specified.
216 try:
217 ipaddr = socket.inet_aton(destaddr)
218 req = req + chr(0x01).encode() + ipaddr
219 except socket.error:
220 # Well it's not an IP number, so it's probably a DNS name.
221 if self.__proxy[3]:
222 # Resolve remotely
223 ipaddr = None
224 req = req + chr(0x03).encode() + chr(len(destaddr)).encode() + destaddr
225 else:
226 # Resolve locally
227 ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
228 req = req + chr(0x01).encode() + ipaddr
229 req = req + struct.pack(">H", destport)
230 self.sendall(req)
231 # Get the response
232 resp = self.__recvall(4)
233 if resp[0:1] != chr(0x05).encode():
234 self.close()
235 raise GeneralProxyError((1, _generalerrors[1]))
236 elif resp[1:2] != chr(0x00).encode():
237 # Connection failed
238 self.close()
239 if ord(resp[1:2])<=8:
240 raise Socks5Error((ord(resp[1:2]), _socks5errors[ord(resp[1:2])]))
241 else:
242 raise Socks5Error((9, _socks5errors[9]))
243 # Get the bound address/port
244 elif resp[3:4] == chr(0x01).encode():
245 boundaddr = self.__recvall(4)
246 elif resp[3:4] == chr(0x03).encode():
247 resp = resp + self.recv(1)
248 boundaddr = self.__recvall(ord(resp[4:5]))
249 else:
250 self.close()
251 raise GeneralProxyError((1,_generalerrors[1]))
252 boundport = struct.unpack(">H", self.__recvall(2))[0]
253 self.__proxysockname = (boundaddr, boundport)
254 if ipaddr != None:
255 self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
256 else:
257 self.__proxypeername = (destaddr, destport)
258
259 def __resolvesocks5(self, host):
260 # Now we can request the actual connection

Callers 1

connectMethod · 0.95

Calls 6

__recvallMethod · 0.95
GeneralProxyErrorClass · 0.70
Socks5ErrorClass · 0.70
encodeMethod · 0.45
closeMethod · 0.45
recvMethod · 0.45

Tested by

no test coverage detected