1 |
commit: e836c4ffb32aba02b28b0254a3edb2147dd362b5 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Mar 2 05:56:12 2020 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Mar 2 05:57:53 2020 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=e836c4ff |
7 |
|
8 |
bin/socks5-server.py: PEP 492 coroutines with async and await syntax |
9 |
|
10 |
https://bugs.gentoo.org/709334 |
11 |
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> |
12 |
|
13 |
bin/socks5-server.py | 36 +++++++++++++++++------------------- |
14 |
1 file changed, 17 insertions(+), 19 deletions(-) |
15 |
|
16 |
diff --git a/bin/socks5-server.py b/bin/socks5-server.py |
17 |
index d1649ad4a..1d07c98ed 100644 |
18 |
--- a/bin/socks5-server.py |
19 |
+++ b/bin/socks5-server.py |
20 |
@@ -29,8 +29,7 @@ class Socks5Server(object): |
21 |
An asynchronous SOCKSv5 server. |
22 |
""" |
23 |
|
24 |
- @asyncio.coroutine |
25 |
- def handle_proxy_conn(self, reader, writer): |
26 |
+ async def handle_proxy_conn(self, reader, writer): |
27 |
""" |
28 |
Handle incoming client connection. Perform SOCKSv5 request |
29 |
exchange, open a proxied connection and start relaying. |
30 |
@@ -43,7 +42,7 @@ class Socks5Server(object): |
31 |
|
32 |
try: |
33 |
# SOCKS hello |
34 |
- data = yield from reader.readexactly(2) |
35 |
+ data = await reader.readexactly(2) |
36 |
vers, method_no = struct.unpack('!BB', data) |
37 |
|
38 |
if vers != 0x05: |
39 |
@@ -53,7 +52,7 @@ class Socks5Server(object): |
40 |
return |
41 |
|
42 |
# ...and auth method list |
43 |
- data = yield from reader.readexactly(method_no) |
44 |
+ data = await reader.readexactly(method_no) |
45 |
for method in data: |
46 |
if method == 0x00: |
47 |
break |
48 |
@@ -64,13 +63,13 @@ class Socks5Server(object): |
49 |
# auth reply |
50 |
repl = struct.pack('!BB', 0x05, method) |
51 |
writer.write(repl) |
52 |
- yield from writer.drain() |
53 |
+ await writer.drain() |
54 |
if method == 0xFF: |
55 |
writer.close() |
56 |
return |
57 |
|
58 |
# request |
59 |
- data = yield from reader.readexactly(4) |
60 |
+ data = await reader.readexactly(4) |
61 |
vers, cmd, rsv, atyp = struct.unpack('!BBBB', data) |
62 |
|
63 |
if vers != 0x05 or rsv != 0x00: |
64 |
@@ -83,31 +82,31 @@ class Socks5Server(object): |
65 |
if cmd != 0x01: # CONNECT |
66 |
rpl = 0x07 # command not supported |
67 |
elif atyp == 0x01: # IPv4 |
68 |
- data = yield from reader.readexactly(4) |
69 |
+ data = await reader.readexactly(4) |
70 |
addr = socket.inet_ntoa(data) |
71 |
elif atyp == 0x03: # domain name |
72 |
- data = yield from reader.readexactly(1) |
73 |
+ data = await reader.readexactly(1) |
74 |
addr_len, = struct.unpack('!B', data) |
75 |
- addr = yield from reader.readexactly(addr_len) |
76 |
+ addr = await reader.readexactly(addr_len) |
77 |
try: |
78 |
addr = addr.decode('idna') |
79 |
except UnicodeDecodeError: |
80 |
rpl = 0x04 # host unreachable |
81 |
|
82 |
elif atyp == 0x04: # IPv6 |
83 |
- data = yield from reader.readexactly(16) |
84 |
+ data = await reader.readexactly(16) |
85 |
addr = socket.inet_ntop(socket.AF_INET6, data) |
86 |
else: |
87 |
rpl = 0x08 # address type not supported |
88 |
|
89 |
# try to connect if we can handle it |
90 |
if rpl == 0x00: |
91 |
- data = yield from reader.readexactly(2) |
92 |
+ data = await reader.readexactly(2) |
93 |
port, = struct.unpack('!H', data) |
94 |
|
95 |
try: |
96 |
# open a proxied connection |
97 |
- proxied_reader, proxied_writer = yield from asyncio.open_connection( |
98 |
+ proxied_reader, proxied_writer = await asyncio.open_connection( |
99 |
addr, port) |
100 |
except (socket.gaierror, socket.herror): |
101 |
# DNS failure |
102 |
@@ -150,7 +149,7 @@ class Socks5Server(object): |
103 |
# reply to the request |
104 |
repl = struct.pack('!BBB', 0x05, rpl, 0x00) |
105 |
writer.write(repl + repl_addr) |
106 |
- yield from writer.drain() |
107 |
+ await writer.drain() |
108 |
|
109 |
# close if an error occured |
110 |
if rpl != 0x00: |
111 |
@@ -166,7 +165,7 @@ class Socks5Server(object): |
112 |
try: |
113 |
try: |
114 |
while True: |
115 |
- data = yield from reader.read(4096) |
116 |
+ data = await reader.read(4096) |
117 |
if data == b'': |
118 |
# client disconnected, stop relaying from |
119 |
# remote host |
120 |
@@ -174,7 +173,7 @@ class Socks5Server(object): |
121 |
break |
122 |
|
123 |
proxied_writer.write(data) |
124 |
- yield from proxied_writer.drain() |
125 |
+ await proxied_writer.drain() |
126 |
except OSError: |
127 |
# read or write failure |
128 |
t.cancel() |
129 |
@@ -193,8 +192,7 @@ class Socks5Server(object): |
130 |
writer.close() |
131 |
raise |
132 |
|
133 |
- @asyncio.coroutine |
134 |
- def handle_proxied_conn(self, proxied_reader, writer, parent_task): |
135 |
+ async def handle_proxied_conn(self, proxied_reader, writer, parent_task): |
136 |
""" |
137 |
Handle the proxied connection. Relay incoming data |
138 |
to the client. |
139 |
@@ -208,12 +206,12 @@ class Socks5Server(object): |
140 |
try: |
141 |
try: |
142 |
while True: |
143 |
- data = yield from proxied_reader.read(4096) |
144 |
+ data = await proxied_reader.read(4096) |
145 |
if data == b'': |
146 |
break |
147 |
|
148 |
writer.write(data) |
149 |
- yield from writer.drain() |
150 |
+ await writer.drain() |
151 |
finally: |
152 |
parent_task.cancel() |
153 |
except (OSError, asyncio.CancelledError): |