Skip to main content

A python module for Chaining of Proxies.

Project description

proxychains

This is a pretty heavily modified pyChainedProxy, which is a fork of socksipy, and is somehow related to PySocksipyChain.

I'm not entirely sure about the hierarchy of those projects, so i'm going to mention everyone, and use a GPL license.

Copyright (c) 2020 Aman Kumar. All rights reserved.
Copyright 2011 Bjarni R. Einarsson. All rights reserved.
Copyright 2006 Dan-Haim. All rights reserved.

Changes include:

  • Dropped python 2 support
  • Code readability changes
  • https/ssl connect chaining support

https://pypi.org/project/proxychains/

Proxy support

  • SOCKS4
  • SOCKS5
  • HTTP: Cannot be anywhere except the end of the chain, or proxy anything except http requests, unless the proxy supports the connect method, in which case the library will use HTTP_CONNECT
  • HTTPS: Same as above
  • HTTP_CONNECT
  • HTTPS_CONNECT
  • SSL: Unsure about the usage.
  • DEFAULT: Use the global chain. Unsure about the usage.
  • TOR: Should be completely identical to SOCKS5

Install

pip install proxychains

Example

Note that the example isn't complete. Check the source code for other methods or use the documentation of the other projects.

import requests
from requests.structures import CaseInsensitiveDict
from http.client import HTTPConnection
from urllib.parse import urlparse
import proxychains as socks
# socks.ENABLE_DEBUG = True

# https://stackoverflow.com/questions/46446904/encoding-an-http-request-in-python/46448489
class TunneledHTTPConnection(HTTPConnection):
    def __init__(self, transport, *args, **kwargs):
        self.transport = transport
        HTTPConnection.__init__(self, *args, **kwargs)

    def connect(self):
        self.transport.connect((self.host, self.port))
        self.sock = self.transport

class TunneledHTTPAdapter(requests.adapters.BaseAdapter):
    def __init__(self, transport):
        self.transport = transport

    def close(self):
        pass

    def send(self, request, **kwargs):
        scheme, location, path, params, query, anchor = urlparse(request.url)
        if ':' in location:
            host, port = location.split(':')
            port = int(port)
        else:
            host = location
            port = 80

        connection = TunneledHTTPConnection(self.transport, host, port)
        connection.request(method=request.method,
                           url=request.url,
                           body=request.body,
                           headers=request.headers)
        r = connection.getresponse()
        resp = requests.Response()
        resp.status_code = r.status
        resp.headers = CaseInsensitiveDict(r.headers)
        resp.raw = r
        resp.reason = r.reason
        resp.url = request.url
        resp.request = request
        resp.connection = connection
        resp.encoding = requests.utils.get_encoding_from_headers(r.headers)
        requests.cookies.extract_cookies_to_jar(resp.cookies, request, r)
        return resp

if __name__ == '__main__':
    with requests.Session() as session:
        sock = socks.socksocket()
        sock.addproxy(socks.parseproxy("httpcs://127.0.0.1:8080"))  # this will use HTTPS_CONNECT
        sock.addproxy(socks.Proxy(
            socks.ProxyType.SOCKS5,  # the proxy type
            "127.0.0.1",  # host
            8081,  # port
            remote_dns=False,  # whether we should resolve the next host locally, or let this host handle it. Default: False
            username="login",  # proxy login
            password="hunter2"  # proxy password
        ))
        # socks.usesystemdefaults()  # this will use enviroment variables to set proxy. there is no support for chains
        session.mount("http://", TunneledHTTPAdapter(sock))
        session.mount("https://", TunneledHTTPAdapter(sock))
        print(session.get("https://httpbin.org/ip").json()["origin"])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

proxychains-1.0.tar.gz (24.3 kB view details)

Uploaded Source

File details

Details for the file proxychains-1.0.tar.gz.

File metadata

  • Download URL: proxychains-1.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for proxychains-1.0.tar.gz
Algorithm Hash digest
SHA256 8a625cd24005a180e523e65f46fffe2a155fa346f4a9d315c17b54dc5d03e0c7
MD5 bdb916eb67576aeac8686586f95becf4
BLAKE2b-256 39b3037aff6e28e2c242d3c7bc7120b9a0a2a882acdd8a8e0e02b63a16f181c4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page