import trio  # python3 -m pip install --upgrade trio

HTML = "<html>Hello World! I'm a %83%83%72 - HTTP polyglot</html>"
HTTP_BANNER = f"HTTP/1.1 200 OK\nContent-Length: {len(HTML)+1}\n\n{HTML}\n".encode()

async def forward_from_a_to_b(a, b):
    async for chunk in a:
        print(f"=> {chunk}", flush=True)
        await b.send_all(chunk)


async def proxy(listen_stream):
    await listen_stream.send_all(HTTP_BANNER)
    target_stream = await trio.open_tcp_stream('localhost', 22)
    async with trio.open_nursery() as nursery:
        nursery.start_soon(forward_from_a_to_b, listen_stream, target_stream)
        nursery.start_soon(forward_from_a_to_b, target_stream, listen_stream)


async def main():
    # 443 for HTTPS, but what about 422 for HTTP+SSH
    while True:
        try:
            await trio.serve_tcp(proxy, 422)
        except KeyboardInterrupt:
            break
        except trio.BrokenResourceError:
            print("Conn died")


if __name__ == '__main__':
    trio.run(main)
