archive
/
tcp-nat-proxy
Archived
1
Fork 0

Add basic hard coded implementation

This commit is contained in:
Jake Howard 2019-01-01 18:44:40 +00:00
parent 3739dcef42
commit 907d9d1a78
Signed by: jake
GPG Key ID: 57AFB45680EDD477
1 changed files with 37 additions and 0 deletions

View File

@ -1,3 +1,40 @@
import asyncio
import uvloop
uvloop.install()
BUFFER_SIZE = 4096
async def pipe(reader, writer):
try:
while not reader.at_eof():
writer.write(await reader.read(BUFFER_SIZE))
finally:
writer.close()
async def create_proxy_pipe(listen_port, destination_host, destination_port):
async def handle_client(local_reader, local_writer):
try:
remote_reader, remote_writer = await asyncio.open_connection(
destination_host, destination_port
)
pipe1 = pipe(local_reader, remote_writer)
pipe2 = pipe(remote_reader, local_writer)
await asyncio.gather(pipe1, pipe2)
finally:
local_writer.close()
server = await asyncio.start_server(handle_client, "0.0.0.0", listen_port)
print("Serving on {}".format(server.sockets[0].getsockname()))
await server.wait_closed()
async def main():
await asyncio.gather(create_proxy_pipe(8888, "127.0.0.1", 8889))
if __name__ == "__main__":
asyncio.run(main())