Search code examples
pythonpython-3.xsocketsproxytunnel

Python - How to create a tunnel of proxies


I asked this question: Wrap packets in connect requests until reach the last proxy

And I learnt that to create a chains of proxies I have to:

  • create a socket
  • connect the socket to proxy A
  • create a tunnel via A to proxy B - either with HTTP or SOCKS protocol similar
  • create a tunnel via [A,B] to proxy C similar
  • create a tunnel via [A,B,C] to D
  • ... until your last proxy is instructed to built the tunnel to the
    final target T

I got what I have to do until the second point, cause I think I just have to add the "CONNECT" header to the http request to the proxy A. But my question is, in this example http request:

CONNECT ipproxy:80 HTTP/1.1
Host: ?:80

In the host header I should put again the proxy ip or something else? Like the proxy B ip or the final site domain?

Also, I didn't understand how to go on from the third point to the next... because I don't know how to tell the proxy A to create a tunnel to proxyB and then proxy B to create a tunnel to proxy C that goes to the final site..

Examples of how can I do it with python? Or some doc?


Solution

  • There is no Host header with CONNECT. I.e. to request HTTP proxy A to create a tunnel to HTTP proxy B you just use:

    >>> CONNECT B_host:B_port HTTP/1.0
    >>>
    <<< 200 connections established
    <<<
    

    And then you have this tunnel to proxy B via proxy A. Inside this tunnel you then can create another tunnel to target T, i.e. on the same socket send and receive next:

    >>> CONNECT T_host:T_port HTTP/1.0
    >>>
    <<< 200 connections established
    <<<
    

    Note that not all proxies allow you to CONNECT to arbitrary hosts and ports and they might also not allow arbitrary protocols like a tunnel inside a tunnel but only selected protocols like HTTPS.