__negotiatesocks4(self,destaddr,destport) Negotiates a connection through a SOCKS4 server.
(self,destaddr,destport)
| 280 | return self.__proxypeername |
| 281 | |
| 282 | def __negotiatesocks4(self,destaddr,destport): |
| 283 | """__negotiatesocks4(self,destaddr,destport) |
| 284 | Negotiates a connection through a SOCKS4 server. |
| 285 | """ |
| 286 | # Check if the destination address provided is an IP address |
| 287 | rmtrslv = False |
| 288 | try: |
| 289 | ipaddr = socket.inet_aton(destaddr) |
| 290 | except socket.error: |
| 291 | # It's a DNS name. Check where it should be resolved. |
| 292 | if self.__proxy[3]: |
| 293 | ipaddr = struct.pack("BBBB", 0x00, 0x00, 0x00, 0x01) |
| 294 | rmtrslv = True |
| 295 | else: |
| 296 | ipaddr = socket.inet_aton(socket.gethostbyname(destaddr)) |
| 297 | # Construct the request packet |
| 298 | req = struct.pack(">BBH", 0x04, 0x01, destport) + ipaddr |
| 299 | # The username parameter is considered userid for SOCKS4 |
| 300 | if self.__proxy[4] != None: |
| 301 | req = req + self.__proxy[4] |
| 302 | req = req + chr(0x00).encode() |
| 303 | # DNS name if remote resolving is required |
| 304 | # NOTE: This is actually an extension to the SOCKS4 protocol |
| 305 | # called SOCKS4A and may not be supported in all cases. |
| 306 | if rmtrslv: |
| 307 | req = req + destaddr + chr(0x00).encode() |
| 308 | self.sendall(req) |
| 309 | # Get the response from the server |
| 310 | resp = self.__recvall(8) |
| 311 | if resp[0:1] != chr(0x00).encode(): |
| 312 | # Bad data |
| 313 | self.close() |
| 314 | raise GeneralProxyError((1,_generalerrors[1])) |
| 315 | if resp[1:2] != chr(0x5A).encode(): |
| 316 | # Server returned an error |
| 317 | self.close() |
| 318 | if ord(resp[1:2]) in (91, 92, 93): |
| 319 | self.close() |
| 320 | raise Socks4Error((ord(resp[1:2]), _socks4errors[ord(resp[1:2]) - 90])) |
| 321 | else: |
| 322 | raise Socks4Error((94, _socks4errors[4])) |
| 323 | # Get the bound address/port |
| 324 | self.__proxysockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0]) |
| 325 | if rmtrslv != None: |
| 326 | self.__proxypeername = (socket.inet_ntoa(ipaddr), destport) |
| 327 | else: |
| 328 | self.__proxypeername = (destaddr, destport) |
| 329 | |
| 330 | def __negotiatehttp(self, destaddr, destport): |
| 331 | """__negotiatehttp(self,destaddr,destport) |
no test coverage detected