(self, destaddr, destport)
__negotiatesocks5(self,destaddr,destport)
        Negotiates a connection through a SOCKS5 server.
__negotiatesocks5(self,destaddr,destport)
        Negotiates a connection through a SOCKS5 server.
__negotiatesocks5 ( self destaddr destport ) Negotiates a connection through a SOCKS5 server .
def __negotiatesocks5(self, destaddr, destport):
        """__negotiatesocks5(self,destaddr,destport)
        Negotiates a connection through a SOCKS5 server.
        """
        # First we'll send the authentication packages we support.
        if (self.__proxy[4]!=None) and (self.__proxy[5]!=None):
            # The username/password details were supplied to the
            # setproxy method so we support the USERNAME/PASSWORD
            # authentication (in addition to the standard none).
            self.sendall(struct.pack('BBBB', 0x05, 0x02, 0x00, 0x02))
        else:
            # No username/password were entered, therefore we
            # only support connections with no authentication.
            self.sendall(struct.pack('BBB', 0x05, 0x01, 0x00))
        # We'll receive the server's response to determine which
        # method was selected
        chosenauth = self.__recvall(2)
        if chosenauth[0:1] != chr(0x05).encode():
            self.close()
            raise GeneralProxyError((1, _generalerrors[1]))
        # Check the chosen authentication method
        if chosenauth[1:2] == chr(0x00).encode():
            # No authentication is required
            pass
        elif chosenauth[1:2] == chr(0x02).encode():
            # Okay, we need to perform a basic username/password
            # authentication.
            self.sendall(chr(0x01).encode() + chr(len(self.__proxy[4])) + self.__proxy[4] + chr(len(self.__proxy[5])) + self.__proxy[5])
            authstat = self.__recvall(2)
            if authstat[0:1] != chr(0x01).encode():
                # Bad response
                self.close()
                raise GeneralProxyError((1, _generalerrors[1]))
            if authstat[1:2] != chr(0x00).encode():
                # Authentication failed
                self.close()
                raise Socks5AuthError((3, _socks5autherrors[3]))
            # Authentication succeeded
        else:
            # Reaching here is always bad
            self.close()
            if chosenauth[1] == chr(0xFF).encode():
                raise Socks5AuthError((2, _socks5autherrors[2]))
            else:
                raise GeneralProxyError((1, _generalerrors[1]))
        # Now we can request the actual connection
        req = struct.pack('BBB', 0x05, 0x01, 0x00)
        # If the given destination address is an IP address, we'll
        # use the IPv4 address request even if remote resolving was specified.
        try:
            ipaddr = socket.inet_aton(destaddr)
            req = req + chr(0x01).encode() + ipaddr
        except socket.error:
            # Well it's not an IP number,  so it's probably a DNS name.
            if self.__proxy[3]:
                # Resolve remotely
                ipaddr = None
                req = req + chr(0x03).encode() + chr(len(destaddr)).encode() + destaddr
            else:
                # Resolve locally
                ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
                req = req + chr(0x01).encode() + ipaddr
        req = req + struct.pack(">H", destport)
        self.sendall(req)
        # Get the response
        resp = self.__recvall(4)
        if resp[0:1] != chr(0x05).encode():
            self.close()
            raise GeneralProxyError((1, _generalerrors[1]))
        elif resp[1:2] != chr(0x00).encode():
            # Connection failed
            self.close()
            if ord(resp[1:2])<=8:
                raise Socks5Error((ord(resp[1:2]), _socks5errors[ord(resp[1:2])]))
            else:
                raise Socks5Error((9, _socks5errors[9]))
        # Get the bound address/port
        elif resp[3:4] == chr(0x01).encode():
            boundaddr = self.__recvall(4)
        elif resp[3:4] == chr(0x03).encode():
            resp = resp + self.recv(1)
            boundaddr = self.__recvall(ord(resp[4:5]))
        else:
            self.close()
            raise GeneralProxyError((1,_generalerrors[1]))
        boundport = struct.unpack(">H", self.__recvall(2))[0]
        self.__proxysockname = (boundaddr, boundport)
        if ipaddr != None:
            self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
        else:
            self.__proxypeername = (destaddr, destport)
def __negotiatesocks5 ( self , destaddr , destport ) : # First we'll send the authentication packages we support. if ( self . __proxy [ 4 ] != None ) and ( self . __proxy [ 5 ] != None ) : # The username/password details were supplied to the # setproxy method so we support the USERNAME/PASSWORD # authentication (in addition to the standard none). self . sendall ( struct . pack ( 'BBBB' , 0x05 , 0x02 , 0x00 , 0x02 ) ) else : # No username/password were entered, therefore we # only support connections with no authentication. self . sendall ( struct . pack ( 'BBB' , 0x05 , 0x01 , 0x00 ) ) # We'll receive the server's response to determine which # method was selected chosenauth = self . __recvall ( 2 ) if chosenauth [ 0 : 1 ] != chr ( 0x05 ) . encode ( ) : self . close ( ) raise GeneralProxyError ( ( 1 , _generalerrors [ 1 ] ) ) # Check the chosen authentication method if chosenauth [ 1 : 2 ] == chr ( 0x00 ) . encode ( ) : # No authentication is required pass elif chosenauth [ 1 : 2 ] == chr ( 0x02 ) . encode ( ) : # Okay, we need to perform a basic username/password # authentication. self . sendall ( chr ( 0x01 ) . encode ( ) + chr ( len ( self . __proxy [ 4 ] ) ) + self . __proxy [ 4 ] + chr ( len ( self . __proxy [ 5 ] ) ) + self . __proxy [ 5 ] ) authstat = self . __recvall ( 2 ) if authstat [ 0 : 1 ] != chr ( 0x01 ) . encode ( ) : # Bad response self . close ( ) raise GeneralProxyError ( ( 1 , _generalerrors [ 1 ] ) ) if authstat [ 1 : 2 ] != chr ( 0x00 ) . encode ( ) : # Authentication failed self . close ( ) raise Socks5AuthError ( ( 3 , _socks5autherrors [ 3 ] ) ) # Authentication succeeded else : # Reaching here is always bad self . close ( ) if chosenauth [ 1 ] == chr ( 0xFF ) . encode ( ) : raise Socks5AuthError ( ( 2 , _socks5autherrors [ 2 ] ) ) else : raise GeneralProxyError ( ( 1 , _generalerrors [ 1 ] ) ) # Now we can request the actual connection req = struct . pack ( 'BBB' , 0x05 , 0x01 , 0x00 ) # If the given destination address is an IP address, we'll # use the IPv4 address request even if remote resolving was specified. try : ipaddr = socket . inet_aton ( destaddr ) req = req + chr ( 0x01 ) . encode ( ) + ipaddr except socket . error : # Well it's not an IP number,  so it's probably a DNS name. if self . __proxy [ 3 ] : # Resolve remotely ipaddr = None req = req + chr ( 0x03 ) . encode ( ) + chr ( len ( destaddr ) ) . encode ( ) + destaddr else : # Resolve locally ipaddr = socket . inet_aton ( socket . gethostbyname ( destaddr ) ) req = req + chr ( 0x01 ) . encode ( ) + ipaddr req = req + struct . pack ( ">H" , destport ) self . sendall ( req ) # Get the response resp = self . __recvall ( 4 ) if resp [ 0 : 1 ] != chr ( 0x05 ) . encode ( ) : self . close ( ) raise GeneralProxyError ( ( 1 , _generalerrors [ 1 ] ) ) elif resp [ 1 : 2 ] != chr ( 0x00 ) . encode ( ) : # Connection failed self . close ( ) if ord ( resp [ 1 : 2 ] ) <= 8 : raise Socks5Error ( ( ord ( resp [ 1 : 2 ] ) , _socks5errors [ ord ( resp [ 1 : 2 ] ) ] ) ) else : raise Socks5Error ( ( 9 , _socks5errors [ 9 ] ) ) # Get the bound address/port elif resp [ 3 : 4 ] == chr ( 0x01 ) . encode ( ) : boundaddr = self . __recvall ( 4 ) elif resp [ 3 : 4 ] == chr ( 0x03 ) . encode ( ) : resp = resp + self . recv ( 1 ) boundaddr = self . __recvall ( ord ( resp [ 4 : 5 ] ) ) else : self . close ( ) raise GeneralProxyError ( ( 1 , _generalerrors [ 1 ] ) ) boundport = struct . unpack ( ">H" , self . __recvall ( 2 ) ) [ 0 ] self . __proxysockname = ( boundaddr , boundport ) if ipaddr != None : self . __proxypeername = ( socket . inet_ntoa ( ipaddr ) , destport ) else : self . __proxypeername = ( destaddr , destport )