__negotiatesocks5(self,destaddr,destport) Negotiates a connection through a SOCKS5 server.
(self)
| 163 | self.__proxy = (proxytype, addr, port, rdns, username, password) |
| 164 | |
| 165 | def __negotiatesocks5(self): |
| 166 | """__negotiatesocks5(self,destaddr,destport) |
| 167 | Negotiates a connection through a SOCKS5 server. |
| 168 | """ |
| 169 | # First we'll send the authentication packages we support. |
| 170 | if (self.__proxy[4]!=None) and (self.__proxy[5]!=None): |
| 171 | # The username/password details were supplied to the |
| 172 | # setproxy method so we support the USERNAME/PASSWORD |
| 173 | # authentication (in addition to the standard none). |
| 174 | self.sendall(struct.pack('BBBB', 0x05, 0x02, 0x00, 0x02)) |
| 175 | else: |
| 176 | # No username/password were entered, therefore we |
| 177 | # only support connections with no authentication. |
| 178 | self.sendall(struct.pack('BBB', 0x05, 0x01, 0x00)) |
| 179 | # We'll receive the server's response to determine which |
| 180 | # method was selected |
| 181 | chosenauth = self.__recvall(2) |
| 182 | if chosenauth[0:1] != chr(0x05).encode(): |
| 183 | self.close() |
| 184 | raise GeneralProxyError((1, _generalerrors[1])) |
| 185 | # Check the chosen authentication method |
| 186 | if chosenauth[1:2] == chr(0x00).encode(): |
| 187 | # No authentication is required |
| 188 | pass |
| 189 | elif chosenauth[1:2] == chr(0x02).encode(): |
| 190 | # Okay, we need to perform a basic username/password |
| 191 | # authentication. |
| 192 | self.sendall(chr(0x01).encode() + chr(len(self.__proxy[4])) + self.__proxy[4] + chr(len(self.__proxy[5])) + self.__proxy[5]) |
| 193 | authstat = self.__recvall(2) |
| 194 | if authstat[0:1] != chr(0x01).encode(): |
| 195 | # Bad response |
| 196 | self.close() |
| 197 | raise GeneralProxyError((1, _generalerrors[1])) |
| 198 | if authstat[1:2] != chr(0x00).encode(): |
| 199 | # Authentication failed |
| 200 | self.close() |
| 201 | raise Socks5AuthError((3, _socks5autherrors[3])) |
| 202 | # Authentication succeeded |
| 203 | else: |
| 204 | # Reaching here is always bad |
| 205 | self.close() |
| 206 | if chosenauth[1] == chr(0xFF).encode(): |
| 207 | raise Socks5AuthError((2, _socks5autherrors[2])) |
| 208 | else: |
| 209 | raise GeneralProxyError((1, _generalerrors[1])) |
| 210 | |
| 211 | def __connectsocks5(self, destaddr, destport): |
| 212 | # Now we can request the actual connection |
no test coverage detected