simple TCP proxies#

By connecting a client and server’s input and output together, it is possible to create a simple TCP proxy.

  1. Create a named pipe by using the mkfifo command:

mkfifo backpipe
  1. Create a server on port 8000 and feed it’s output into a connection to the destination host on port 80 while using the backpipe to feed the host connection’s output back to the server’s input stream:

pync -l 8000 < backpipe | pync host.example.com 80 > backpipe
 1# proxy.py
 2import pync
 3
 4
 5def main():
 6    server = pync.Netcat(port=8000,
 7        l=True,
 8        stdin=pync.PIPE,
 9        stdout=pync.PIPE,
10    )
11    server.start_process(daemon=True)
12
13    client = pync.Netcat('host.example.com', 80,
14        stdin=server.stdout,
15        stdout=server.stdin,
16    )
17    client.run()
18
19
20if __name__ == '__main__':
21    main()

This would create a proxy server on port 8000 that will forward any request made to it to the destination server (host.example.com in this case).

To test this out, connect to the proxy server on port 8000 and send a HTTP GET request:

echo "GET / HTTP/1.1\r\n\r\n" | pync localhost 8000
1# http_get.py
2import pync
3pync.run('localhost 8000', input=b'GET / HTTP/1.1\r\n\r\n')