Tunneling TCP traffic through HTTP proxy

It is not obvious but possible to use HTTP proxy to tunnel arbitrary TCP traffic for applications and protocols other than HTTP. Most http proxies support CONNECT command that is used to establish secure https connections. This command literary creates a tunnel for the requested address. And all the data between destination and client is redirected through the proxy without any modification. This is because https traffic is encrypted and proxy don't have any access to the underlying information being transmitted.

So if your application establish only one-way connection to the destination you can use HTTP proxy that supports CONNECT to make connections.

Our proxies also support CONNECT and can be used to tunnel TCP connections. Moreover all the IP rotation is also supported and you can request a new IP on every new session or specify outgoing locations using headers or username API (see docs)

Here is an example in python how to tunnel connection through our proxies:

def parse_proxy_string(proxy):
    auth, addr = proxy.split('@')
    user, pwd = auth.split(':')
    host, port = addr.split(':')
    token = base64.b64encode(auth.encode()).strip()
    return {
        'user': user,
        'password': pwd,
        'host': host,
        'port': int(port),
        'token': token.decode(),
        }

def connect_proxy(sock, proxy, target_host, target_port):
    try:
        sock.connect((proxy['host'], proxy['port']))
        msg = (
            'CONNECT {host}:{port} HTTP/1.1\r\n'
            'X-BOTPROXY-SESSION: {session}\r\n'
            'Proxy-Authorization: Basic {auth}\r\n\r\n').format(
                session=str(random.randint(0, 65536)),
                host=target_host,
                port=target_port,
                auth=proxy['token'])
        sock.sendall(msg.encode())
        res = sock.recv(100)
        res = res.decode('ascii', errors='ignore')
        res = [x for x in [x.strip() for x in res.split('\n')] if x]
        if not '200' in res[0]:
            raise Exception(res)
        for hdr in res[1:]:
            print('Proxy response: %s', hdr)
    except Exception as exc:
        print(
            'Proxy connect error (%s) to %s:%d',
            exc, proxy['host'], proxy['port'])
        return False
    return True


def main():
    proxy = parse_proxy_string("proxy_user:[email protected]:8080")
    target_host = "your.host.com"
    target_port = 55432 # destination port
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.settimeout(5)
        self.connect_proxy(sock, proxy, target_host, target_port):

        # rest of the code
        sock.sendall(your_message)

In this example each new connection will initiate a new BotProxy session ensuring a new IP from the available pool of addresses. Please note that we block the following destination ports 21, 22, 23, 25, 465, 587, 993, 2525, 2526 (telnet, ftp and smtp) to avoid malicious behavior which is also against our Acceptable Use Policy. However don't forget that you can contact support anytime if you have questions or need to use these ports in your application.



BotProxy: Anonymous HTTP Rotating Proxy

Plug BotProxy into your web scraping application and it will automatically route your parsers requests through one of our outgoing proxy servers. Multiple locations, fresh IP addresses every day. Typical integrations take less than 5 minutes into any script or application.

Start Free Trial or learn more