From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 7E98D138350 for ; Mon, 2 Mar 2020 06:13:05 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 979D5E08B0; Mon, 2 Mar 2020 06:13:04 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 677E7E08B0 for ; Mon, 2 Mar 2020 06:13:04 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id D578134F303 for ; Mon, 2 Mar 2020 06:13:02 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 6160717E for ; Mon, 2 Mar 2020 06:13:00 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1583128673.e836c4ffb32aba02b28b0254a3edb2147dd362b5.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: bin/ X-VCS-Repository: proj/portage X-VCS-Files: bin/socks5-server.py X-VCS-Directories: bin/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: e836c4ffb32aba02b28b0254a3edb2147dd362b5 X-VCS-Branch: master Date: Mon, 2 Mar 2020 06:13:00 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 9e694912-b111-4c64-b060-944e072f88c2 X-Archives-Hash: d701b9f0c6b709e0ed78dd3c7fcb68b0 commit: e836c4ffb32aba02b28b0254a3edb2147dd362b5 Author: Zac Medico gentoo org> AuthorDate: Mon Mar 2 05:56:12 2020 +0000 Commit: Zac Medico gentoo org> CommitDate: Mon Mar 2 05:57:53 2020 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=e836c4ff bin/socks5-server.py: PEP 492 coroutines with async and await syntax https://bugs.gentoo.org/709334 Signed-off-by: Zac Medico gentoo.org> bin/socks5-server.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/bin/socks5-server.py b/bin/socks5-server.py index d1649ad4a..1d07c98ed 100644 --- a/bin/socks5-server.py +++ b/bin/socks5-server.py @@ -29,8 +29,7 @@ class Socks5Server(object): An asynchronous SOCKSv5 server. """ - @asyncio.coroutine - def handle_proxy_conn(self, reader, writer): + async def handle_proxy_conn(self, reader, writer): """ Handle incoming client connection. Perform SOCKSv5 request exchange, open a proxied connection and start relaying. @@ -43,7 +42,7 @@ class Socks5Server(object): try: # SOCKS hello - data = yield from reader.readexactly(2) + data = await reader.readexactly(2) vers, method_no = struct.unpack('!BB', data) if vers != 0x05: @@ -53,7 +52,7 @@ class Socks5Server(object): return # ...and auth method list - data = yield from reader.readexactly(method_no) + data = await reader.readexactly(method_no) for method in data: if method == 0x00: break @@ -64,13 +63,13 @@ class Socks5Server(object): # auth reply repl = struct.pack('!BB', 0x05, method) writer.write(repl) - yield from writer.drain() + await writer.drain() if method == 0xFF: writer.close() return # request - data = yield from reader.readexactly(4) + data = await reader.readexactly(4) vers, cmd, rsv, atyp = struct.unpack('!BBBB', data) if vers != 0x05 or rsv != 0x00: @@ -83,31 +82,31 @@ class Socks5Server(object): if cmd != 0x01: # CONNECT rpl = 0x07 # command not supported elif atyp == 0x01: # IPv4 - data = yield from reader.readexactly(4) + data = await reader.readexactly(4) addr = socket.inet_ntoa(data) elif atyp == 0x03: # domain name - data = yield from reader.readexactly(1) + data = await reader.readexactly(1) addr_len, = struct.unpack('!B', data) - addr = yield from reader.readexactly(addr_len) + addr = await reader.readexactly(addr_len) try: addr = addr.decode('idna') except UnicodeDecodeError: rpl = 0x04 # host unreachable elif atyp == 0x04: # IPv6 - data = yield from reader.readexactly(16) + data = await reader.readexactly(16) addr = socket.inet_ntop(socket.AF_INET6, data) else: rpl = 0x08 # address type not supported # try to connect if we can handle it if rpl == 0x00: - data = yield from reader.readexactly(2) + data = await reader.readexactly(2) port, = struct.unpack('!H', data) try: # open a proxied connection - proxied_reader, proxied_writer = yield from asyncio.open_connection( + proxied_reader, proxied_writer = await asyncio.open_connection( addr, port) except (socket.gaierror, socket.herror): # DNS failure @@ -150,7 +149,7 @@ class Socks5Server(object): # reply to the request repl = struct.pack('!BBB', 0x05, rpl, 0x00) writer.write(repl + repl_addr) - yield from writer.drain() + await writer.drain() # close if an error occured if rpl != 0x00: @@ -166,7 +165,7 @@ class Socks5Server(object): try: try: while True: - data = yield from reader.read(4096) + data = await reader.read(4096) if data == b'': # client disconnected, stop relaying from # remote host @@ -174,7 +173,7 @@ class Socks5Server(object): break proxied_writer.write(data) - yield from proxied_writer.drain() + await proxied_writer.drain() except OSError: # read or write failure t.cancel() @@ -193,8 +192,7 @@ class Socks5Server(object): writer.close() raise - @asyncio.coroutine - def handle_proxied_conn(self, proxied_reader, writer, parent_task): + async def handle_proxied_conn(self, proxied_reader, writer, parent_task): """ Handle the proxied connection. Relay incoming data to the client. @@ -208,12 +206,12 @@ class Socks5Server(object): try: try: while True: - data = yield from proxied_reader.read(4096) + data = await proxied_reader.read(4096) if data == b'': break writer.write(data) - yield from writer.drain() + await writer.drain() finally: parent_task.cancel() except (OSError, asyncio.CancelledError):