1 |
Report bound socket name as requested by the protocol. Supports both |
2 |
IPv4 and IPv6 sockets. |
3 |
--- |
4 |
bin/socks5-server.py | 21 +++++++++++++++++---- |
5 |
1 file changed, 17 insertions(+), 4 deletions(-) |
6 |
|
7 |
diff --git a/bin/socks5-server.py b/bin/socks5-server.py |
8 |
index 4795dcc..78a6751 100644 |
9 |
--- a/bin/socks5-server.py |
10 |
+++ b/bin/socks5-server.py |
11 |
@@ -11,6 +11,7 @@ import sys |
12 |
|
13 |
|
14 |
class ProxyConnection(asyncore.dispatcher_with_send): |
15 |
+ _family = None |
16 |
_proxy_conn = None |
17 |
|
18 |
def __init__(self, host, port, proxy_conn): |
19 |
@@ -18,6 +19,7 @@ class ProxyConnection(asyncore.dispatcher_with_send): |
20 |
asyncore.dispatcher_with_send.__init__(self) |
21 |
# TODO: how to support IPv6? ugly fail-then-reinit? |
22 |
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
23 |
+ self._family = socket.AF_INET |
24 |
self.connect((host, port)) |
25 |
|
26 |
def handle_read(self): |
27 |
@@ -25,7 +27,7 @@ class ProxyConnection(asyncore.dispatcher_with_send): |
28 |
self._proxy_conn.send(buf) |
29 |
|
30 |
def handle_connect(self): |
31 |
- self._proxy_conn.send_connected() |
32 |
+ self._proxy_conn.send_connected(self._family, self.getsockname()) |
33 |
|
34 |
def handle_close(self): |
35 |
self._proxy_conn.close() |
36 |
@@ -126,9 +128,20 @@ class ProxyHandler(asyncore.dispatcher_with_send): |
37 |
if self._my_conn is not None: |
38 |
self._my_conn.close() |
39 |
|
40 |
- def send_connected(self): |
41 |
- repl = struct.pack('!BBBBLH', 0x05, 0x00, 0x00, 0x01, |
42 |
- 0x00000000, 0x0000) |
43 |
+ def send_connected(self, family, addr): |
44 |
+ if family == socket.AF_INET: |
45 |
+ host, port = addr |
46 |
+ bin_host = socket.inet_aton(host) |
47 |
+ |
48 |
+ repl = struct.pack('!BBBB4sH', 0x05, 0x00, 0x00, 0x01, |
49 |
+ bin_host, port) |
50 |
+ elif family == socket.AF_INET6: |
51 |
+ host, port = addr |
52 |
+ bin_host = socket.inet_pton(family, host) |
53 |
+ |
54 |
+ repl = struct.pack('!BBBB16sH', 0x05, 0x00, 0x00, 0x04, |
55 |
+ bin_host, port) |
56 |
+ |
57 |
self.send(repl) |
58 |
self._my_state = 3 |
59 |
|
60 |
-- |
61 |
2.2.2 |