]>
Commit | Line | Data |
---|---|---|
71aff188 YCH |
1 | # Public Domain SOCKS proxy protocol implementation |
2 | # Adapted from https://gist.github.com/bluec0re/cafd3764412967417fd3 | |
9e9cd724 YCH |
3 | # References: |
4 | # SOCKS4 protocol http://www.openssh.com/txt/socks4.protocol | |
5 | # SOCKS4A protocol http://www.openssh.com/txt/socks4a.protocol | |
6 | # SOCKS5 protocol https://tools.ietf.org/html/rfc1928 | |
7 | # SOCKS5 username/password authentication https://tools.ietf.org/html/rfc1929 | |
8 | ||
71aff188 YCH |
9 | import collections |
10 | import socket | |
ac668111 | 11 | import struct |
4350b745 | 12 | |
ac668111 | 13 | from .compat import compat_ord |
4350b745 | 14 | |
71aff188 | 15 | __author__ = 'Timo Schmid <coding@timoschmid.de>' |
4350b745 | 16 | |
9e9cd724 YCH |
17 | SOCKS4_VERSION = 4 |
18 | SOCKS4_REPLY_VERSION = 0x00 | |
19 | # Excerpt from SOCKS4A protocol: | |
20 | # if the client cannot resolve the destination host's domain name to find its | |
21 | # IP address, it should set the first three bytes of DSTIP to NULL and the last | |
22 | # byte to a non-zero value. | |
ac668111 | 23 | SOCKS4_DEFAULT_DSTIP = struct.pack('!BBBB', 0, 0, 0, 0xFF) |
9e9cd724 YCH |
24 | |
25 | SOCKS5_VERSION = 5 | |
26 | SOCKS5_USER_AUTH_VERSION = 0x01 | |
27 | SOCKS5_USER_AUTH_SUCCESS = 0x00 | |
28 | ||
29 | ||
86e5f3ed | 30 | class Socks4Command: |
9e9cd724 YCH |
31 | CMD_CONNECT = 0x01 |
32 | CMD_BIND = 0x02 | |
33 | ||
34 | ||
35 | class Socks5Command(Socks4Command): | |
36 | CMD_UDP_ASSOCIATE = 0x03 | |
37 | ||
38 | ||
86e5f3ed | 39 | class Socks5Auth: |
9e9cd724 YCH |
40 | AUTH_NONE = 0x00 |
41 | AUTH_GSSAPI = 0x01 | |
42 | AUTH_USER_PASS = 0x02 | |
43 | AUTH_NO_ACCEPTABLE = 0xFF # For server response | |
44 | ||
45 | ||
86e5f3ed | 46 | class Socks5AddressType: |
9e9cd724 YCH |
47 | ATYP_IPV4 = 0x01 |
48 | ATYP_DOMAINNAME = 0x03 | |
49 | ATYP_IPV6 = 0x04 | |
50 | ||
4350b745 | 51 | |
4bd7d9d4 | 52 | class ProxyError(socket.error): |
9e9cd724 YCH |
53 | ERR_SUCCESS = 0x00 |
54 | ||
55 | def __init__(self, code=None, msg=None): | |
56 | if code is not None and msg is None: | |
83442966 | 57 | msg = self.CODES.get(code) or 'unknown error' |
86e5f3ed | 58 | super().__init__(code, msg) |
9e9cd724 YCH |
59 | |
60 | ||
61 | class InvalidVersionError(ProxyError): | |
62 | def __init__(self, expected_version, got_version): | |
86e5f3ed | 63 | msg = ('Invalid response version from server. Expected {:02x} got ' |
64 | '{:02x}'.format(expected_version, got_version)) | |
65 | super().__init__(0, msg) | |
4350b745 YCH |
66 | |
67 | ||
4350b745 | 68 | class Socks4Error(ProxyError): |
9e9cd724 YCH |
69 | ERR_SUCCESS = 90 |
70 | ||
4350b745 | 71 | CODES = { |
9e9cd724 | 72 | 91: 'request rejected or failed', |
a2406fce | 73 | 92: 'request rejected because SOCKS server cannot connect to identd on the client', |
9e9cd724 | 74 | 93: 'request rejected because the client program and identd report different user-ids' |
4350b745 | 75 | } |
71aff188 | 76 | |
4350b745 | 77 | |
9e9cd724 YCH |
78 | class Socks5Error(ProxyError): |
79 | ERR_GENERAL_FAILURE = 0x01 | |
71aff188 | 80 | |
4350b745 YCH |
81 | CODES = { |
82 | 0x01: 'general SOCKS server failure', | |
83 | 0x02: 'connection not allowed by ruleset', | |
84 | 0x03: 'Network unreachable', | |
85 | 0x04: 'Host unreachable', | |
86 | 0x05: 'Connection refused', | |
87 | 0x06: 'TTL expired', | |
88 | 0x07: 'Command not supported', | |
89 | 0x08: 'Address type not supported', | |
90 | 0xFE: 'unknown username or invalid password', | |
91 | 0xFF: 'all offered authentication methods were rejected' | |
92 | } | |
93 | ||
71aff188 | 94 | |
86e5f3ed | 95 | class ProxyType: |
71aff188 | 96 | SOCKS4 = 0 |
4350b745 | 97 | SOCKS4A = 1 |
71aff188 | 98 | SOCKS5 = 2 |
4350b745 | 99 | |
582be358 | 100 | |
9e9cd724 YCH |
101 | Proxy = collections.namedtuple('Proxy', ( |
102 | 'type', 'host', 'port', 'username', 'password', 'remote_dns')) | |
4350b745 | 103 | |
4350b745 | 104 | |
71aff188 | 105 | class sockssocket(socket.socket): |
9e9cd724 YCH |
106 | def __init__(self, *args, **kwargs): |
107 | self._proxy = None | |
86e5f3ed | 108 | super().__init__(*args, **kwargs) |
9e9cd724 YCH |
109 | |
110 | def setproxy(self, proxytype, addr, port, rdns=True, username=None, password=None): | |
111 | assert proxytype in (ProxyType.SOCKS4, ProxyType.SOCKS4A, ProxyType.SOCKS5) | |
112 | ||
113 | self._proxy = Proxy(proxytype, addr, port, username, password, rdns) | |
4350b745 YCH |
114 | |
115 | def recvall(self, cnt): | |
116 | data = b'' | |
117 | while len(data) < cnt: | |
118 | cur = self.recv(cnt - len(data)) | |
119 | if not cur: | |
86e5f3ed | 120 | raise EOFError(f'{cnt - len(data)} bytes missing') |
4350b745 YCH |
121 | data += cur |
122 | return data | |
123 | ||
9e9cd724 YCH |
124 | def _recv_bytes(self, cnt): |
125 | data = self.recvall(cnt) | |
ac668111 | 126 | return struct.unpack(f'!{cnt}B', data) |
9e9cd724 YCH |
127 | |
128 | @staticmethod | |
129 | def _len_and_data(data): | |
ac668111 | 130 | return struct.pack('!B', len(data)) + data |
9e9cd724 YCH |
131 | |
132 | def _check_response_version(self, expected_version, got_version): | |
133 | if got_version != expected_version: | |
134 | self.close() | |
135 | raise InvalidVersionError(expected_version, got_version) | |
4350b745 | 136 | |
9e9cd724 | 137 | def _resolve_address(self, destaddr, default, use_remote_dns): |
4350b745 | 138 | try: |
9e9cd724 | 139 | return socket.inet_aton(destaddr) |
86e5f3ed | 140 | except OSError: |
9e9cd724 YCH |
141 | if use_remote_dns and self._proxy.remote_dns: |
142 | return default | |
4350b745 | 143 | else: |
9e9cd724 YCH |
144 | return socket.inet_aton(socket.gethostbyname(destaddr)) |
145 | ||
146 | def _setup_socks4(self, address, is_4a=False): | |
147 | destaddr, port = address | |
148 | ||
149 | ipaddr = self._resolve_address(destaddr, SOCKS4_DEFAULT_DSTIP, use_remote_dns=is_4a) | |
150 | ||
ac668111 | 151 | packet = struct.pack('!BBH', SOCKS4_VERSION, Socks4Command.CMD_CONNECT, port) + ipaddr |
9e9cd724 | 152 | |
0f06bcd7 | 153 | username = (self._proxy.username or '').encode() |
9e9cd724 | 154 | packet += username + b'\x00' |
4350b745 YCH |
155 | |
156 | if is_4a and self._proxy.remote_dns: | |
0f06bcd7 | 157 | packet += destaddr.encode() + b'\x00' |
4350b745 YCH |
158 | |
159 | self.sendall(packet) | |
160 | ||
ac668111 | 161 | version, resp_code, dstport, dsthost = struct.unpack('!BBHI', self.recvall(8)) |
4350b745 | 162 | |
9e9cd724 | 163 | self._check_response_version(SOCKS4_REPLY_VERSION, version) |
4350b745 | 164 | |
9e9cd724 | 165 | if resp_code != Socks4Error.ERR_SUCCESS: |
4350b745 YCH |
166 | self.close() |
167 | raise Socks4Error(resp_code) | |
168 | ||
71aff188 YCH |
169 | return (dsthost, dstport) |
170 | ||
9e9cd724 YCH |
171 | def _setup_socks4a(self, address): |
172 | self._setup_socks4(address, is_4a=True) | |
4350b745 | 173 | |
9e9cd724 | 174 | def _socks5_auth(self): |
ac668111 | 175 | packet = struct.pack('!B', SOCKS5_VERSION) |
4350b745 | 176 | |
9e9cd724 | 177 | auth_methods = [Socks5Auth.AUTH_NONE] |
4350b745 | 178 | if self._proxy.username and self._proxy.password: |
9e9cd724 YCH |
179 | auth_methods.append(Socks5Auth.AUTH_USER_PASS) |
180 | ||
ac668111 | 181 | packet += struct.pack('!B', len(auth_methods)) |
182 | packet += struct.pack(f'!{len(auth_methods)}B', *auth_methods) | |
4350b745 YCH |
183 | |
184 | self.sendall(packet) | |
185 | ||
9e9cd724 | 186 | version, method = self._recv_bytes(2) |
4350b745 | 187 | |
9e9cd724 | 188 | self._check_response_version(SOCKS5_VERSION, version) |
4350b745 | 189 | |
54f54fcc YCH |
190 | if method == Socks5Auth.AUTH_NO_ACCEPTABLE or ( |
191 | method == Socks5Auth.AUTH_USER_PASS and (not self._proxy.username or not self._proxy.password)): | |
4350b745 | 192 | self.close() |
54f54fcc | 193 | raise Socks5Error(Socks5Auth.AUTH_NO_ACCEPTABLE) |
4350b745 | 194 | |
9e9cd724 | 195 | if method == Socks5Auth.AUTH_USER_PASS: |
0f06bcd7 | 196 | username = self._proxy.username.encode() |
197 | password = self._proxy.password.encode() | |
ac668111 | 198 | packet = struct.pack('!B', SOCKS5_USER_AUTH_VERSION) |
9e9cd724 | 199 | packet += self._len_and_data(username) + self._len_and_data(password) |
4350b745 YCH |
200 | self.sendall(packet) |
201 | ||
9e9cd724 | 202 | version, status = self._recv_bytes(2) |
4350b745 | 203 | |
9e9cd724 | 204 | self._check_response_version(SOCKS5_USER_AUTH_VERSION, version) |
4350b745 | 205 | |
9e9cd724 | 206 | if status != SOCKS5_USER_AUTH_SUCCESS: |
4350b745 | 207 | self.close() |
9e9cd724 | 208 | raise Socks5Error(Socks5Error.ERR_GENERAL_FAILURE) |
4350b745 | 209 | |
9e9cd724 YCH |
210 | def _setup_socks5(self, address): |
211 | destaddr, port = address | |
212 | ||
213 | ipaddr = self._resolve_address(destaddr, None, use_remote_dns=True) | |
214 | ||
215 | self._socks5_auth() | |
216 | ||
217 | reserved = 0 | |
ac668111 | 218 | packet = struct.pack('!BBB', SOCKS5_VERSION, Socks5Command.CMD_CONNECT, reserved) |
4350b745 | 219 | if ipaddr is None: |
0f06bcd7 | 220 | destaddr = destaddr.encode() |
ac668111 | 221 | packet += struct.pack('!B', Socks5AddressType.ATYP_DOMAINNAME) |
9e9cd724 | 222 | packet += self._len_and_data(destaddr) |
4350b745 | 223 | else: |
ac668111 | 224 | packet += struct.pack('!B', Socks5AddressType.ATYP_IPV4) + ipaddr |
225 | packet += struct.pack('!H', port) | |
4350b745 YCH |
226 | |
227 | self.sendall(packet) | |
228 | ||
9e9cd724 | 229 | version, status, reserved, atype = self._recv_bytes(4) |
4350b745 | 230 | |
9e9cd724 | 231 | self._check_response_version(SOCKS5_VERSION, version) |
4350b745 | 232 | |
9e9cd724 | 233 | if status != Socks5Error.ERR_SUCCESS: |
4350b745 YCH |
234 | self.close() |
235 | raise Socks5Error(status) | |
236 | ||
9e9cd724 | 237 | if atype == Socks5AddressType.ATYP_IPV4: |
4350b745 | 238 | destaddr = self.recvall(4) |
9e9cd724 YCH |
239 | elif atype == Socks5AddressType.ATYP_DOMAINNAME: |
240 | alen = compat_ord(self.recv(1)) | |
4350b745 | 241 | destaddr = self.recvall(alen) |
9e9cd724 | 242 | elif atype == Socks5AddressType.ATYP_IPV6: |
4350b745 | 243 | destaddr = self.recvall(16) |
ac668111 | 244 | destport = struct.unpack('!H', self.recvall(2))[0] |
71aff188 YCH |
245 | |
246 | return (destaddr, destport) | |
4350b745 YCH |
247 | |
248 | def _make_proxy(self, connect_func, address): | |
9e9cd724 | 249 | if not self._proxy: |
4350b745 YCH |
250 | return connect_func(self, address) |
251 | ||
9e9cd724 YCH |
252 | result = connect_func(self, (self._proxy.host, self._proxy.port)) |
253 | if result != 0 and result is not None: | |
254 | return result | |
255 | setup_funcs = { | |
256 | ProxyType.SOCKS4: self._setup_socks4, | |
257 | ProxyType.SOCKS4A: self._setup_socks4a, | |
258 | ProxyType.SOCKS5: self._setup_socks5, | |
259 | } | |
260 | setup_funcs[self._proxy.type](address) | |
261 | return result | |
262 | ||
4350b745 | 263 | def connect(self, address): |
71aff188 | 264 | self._make_proxy(socket.socket.connect, address) |
4350b745 YCH |
265 | |
266 | def connect_ex(self, address): | |
71aff188 | 267 | return self._make_proxy(socket.socket.connect_ex, address) |