__negotiatesocks4(self,destaddr,destport) Negotiates a connection through a SOCKS4 server.
(self,destaddr,destport)
| 310 | return self.__proxy[0] |
| 311 | |
| 312 | def __negotiatesocks4(self,destaddr,destport): |
| 313 | """__negotiatesocks4(self,destaddr,destport) |
| 314 | Negotiates a connection through a SOCKS4 server. |
| 315 | """ |
| 316 | # Check if the destination address provided is an IP address |
| 317 | rmtrslv = False |
| 318 | try: |
| 319 | ipaddr = socket.inet_aton(destaddr) |
| 320 | except socket.error: |
| 321 | # It's a DNS name. Check where it should be resolved. |
| 322 | if self.__proxy[3]: |
| 323 | ipaddr = struct.pack("BBBB", 0x00, 0x00, 0x00, 0x01) |
| 324 | rmtrslv = True |
| 325 | else: |
| 326 | ipaddr = socket.inet_aton(socket.gethostbyname(destaddr)) |
| 327 | # Construct the request packet |
| 328 | req = struct.pack(">BBH", 0x04, 0x01, destport) + ipaddr |
| 329 | # The username parameter is considered userid for SOCKS4 |
| 330 | if self.__proxy[4] != None: |
| 331 | req = req + self.__proxy[4] |
| 332 | req = req + chr(0x00).encode() |
| 333 | # DNS name if remote resolving is required |
| 334 | # NOTE: This is actually an extension to the SOCKS4 protocol |
| 335 | # called SOCKS4A and may not be supported in all cases. |
| 336 | if rmtrslv: |
| 337 | req = req + destaddr + chr(0x00).encode() |
| 338 | self.sendall(req) |
| 339 | # Get the response from the server |
| 340 | resp = self.__recvall(8) |
| 341 | if resp[0:1] != chr(0x00).encode(): |
| 342 | # Bad data |
| 343 | self.close() |
| 344 | raise GeneralProxyError((1,_generalerrors[1])) |
| 345 | if resp[1:2] != chr(0x5A).encode(): |
| 346 | # Server returned an error |
| 347 | self.close() |
| 348 | if ord(resp[1:2]) in (91, 92, 93): |
| 349 | self.close() |
| 350 | raise Socks4Error((ord(resp[1:2]), _socks4errors[ord(resp[1:2]) - 90])) |
| 351 | else: |
| 352 | raise Socks4Error((94, _socks4errors[4])) |
| 353 | # Get the bound address/port |
| 354 | self.__proxysockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0]) |
| 355 | if rmtrslv != None: |
| 356 | self.__proxypeername = (socket.inet_ntoa(ipaddr), destport) |
| 357 | else: |
| 358 | self.__proxypeername = (destaddr, destport) |
| 359 | |
| 360 | def __negotiatehttp(self, destaddr, destport): |
| 361 | """__negotiatehttp(self,destaddr,destport) |
no test coverage detected