qwj / python-proxy

HTTP/HTTP2/HTTP3/Socks4/Socks5/Shadowsocks/ShadowsocksR/SSH/Redirect/Pf TCP/UDP asynchronous tunnel proxy implemented in Python 3 asyncio.
MIT License
1.99k stars 333 forks source link
asyncio http2 http3 nat pf pproxy proxy python quic shadowsocks shadowsocksr socks4 socks5 ssh tunnel

python-proxy

|made-with-python| |PyPI-version| |Hit-Count| |Downloads| |Downloads-month| |Downloads-week|

.. |made-with-python| image:: https://img.shields.io/badge/Made%20with-Python-1f425f.svg :target: https://www.python.org/ .. |PyPI-version| image:: https://badge.fury.io/py/pproxy.svg :target: https://pypi.python.org/pypi/pproxy/ .. |Hit-Count| image:: http://hits.dwyl.io/qwj/python-proxy.svg :target: https://pypi.python.org/pypi/pproxy/ .. |Downloads| image:: https://pepy.tech/badge/pproxy :target: https://pepy.tech/project/pproxy .. |Downloads-month| image:: https://pepy.tech/badge/pproxy/month :target: https://pepy.tech/project/pproxy .. |Downloads-week| image:: https://pepy.tech/badge/pproxy/week :target: https://pepy.tech/project/pproxy

HTTP/HTTP2/HTTP3/Socks4/Socks5/Shadowsocks/SSH/Redirect/Pf/QUIC TCP/UDP asynchronous tunnel proxy implemented in Python3 asyncio.

QuickStart

.. code:: rst

$ pip3 install pproxy Successfully installed pproxy-1.9.5 $ pproxy Serving on :8080 by http,socks4,socks5 ^C $ pproxy -l ss://chacha20:abc@:8080 Serving on :8080 by ss (chacha20-py)

Optional: (better performance with C ciphers)

.. code:: rst

$ pip3 install pproxy[accelerated] Successfully installed pycryptodome-3.6.4

Apply OS system-wide proxy: (MacOS, Windows)

.. code:: rst

$ pproxy -r ss://chacha20:abc@server_ip:8080 --sys -vv Serving on :8080 by http,socks4,socks5 System proxy setting -> socks5 localhost:8080 socks5 ::1:57345 -> ss server_ip:8080 -> slack.com:443 socks5 ::1:57345 -> ss server_ip:8080 -> www.google.com:443 ..... (all local traffic log) ......

Apply CLI proxy: (MacOS, Linux)

.. code:: rst

$ export http_proxy=http://localhost:8080 $ export https_proxy=http://localhost:8080

Run With Docker

pproxy Docker container has both python3 (with Cryptodome for performance optimizations) and pypy versions available.

Python3:

docker run -it -p 8080:8080 mosajjal/pproxy:latest -l http://:8080 -vv

Pypy3:

docker run -it -p 8080:8080 mosajjal/pproxy:latest-pypy -l http://:8080 -vv

Features

.. _One-Time-Auth: https://shadowsocks.org/en/spec/one-time-auth.html

Protocols

+-------------------+------------+------------+------------+------------+--------------+ | Name | TCP server | TCP client | UDP server | UDP client | scheme | +===================+============+============+============+============+==============+ | http (connect) | ✔ | ✔ | | | http:// | +-------------------+ +------------+------------+------------+--------------+ | http | | ✔ | | | httponly:// | | (get,post,etc) | | | | | (as client) | +-------------------+------------+------------+------------+------------+--------------+ | http v2 (connect) | ✔ | ✔ | | | h2:// | +-------------------+------------+------------+------------+------------+--------------+ | http v3 (connect) | ✔ by UDP | ✔ by UDP | | | h3:// | +-------------------+------------+------------+------------+------------+--------------+ | https | ✔ | ✔ | | | http+ssl:// | +-------------------+------------+------------+------------+------------+--------------+ | socks4 | ✔ | ✔ | | | socks4:// | +-------------------+------------+------------+------------+------------+--------------+ | socks5 | ✔ | ✔ | ✔ udp-only | ✔ udp-only | socks5:// | +-------------------+------------+------------+------------+------------+--------------+ | socks5 over TLS | ✔ | ✔ | | | socks5+ssl://| +-------------------+------------+------------+------------+------------+--------------+ | shadowsocks | ✔ | ✔ | ✔ | ✔ | ss:// | +-------------------+------------+------------+------------+------------+--------------+ | shadowsocks aead | ✔ | ✔ | | | ss:// | +-------------------+------------+------------+------------+------------+--------------+ | shadowsocksR | ✔ | ✔ | | | ssr:// | +-------------------+------------+------------+------------+------------+--------------+ | trojan | ✔ | ✔ | | | trojan:// | +-------------------+------------+------------+------------+------------+--------------+ | ssh tunnel | | ✔ | | | ssh:// | +-------------------+------------+------------+------------+------------+--------------+ | quic | ✔ by UDP | ✔ by UDP | ✔ | ✔ | http+quic:// | +-------------------+------------+------------+------------+------------+--------------+ | iptables nat | ✔ | | | | redir:// | +-------------------+------------+------------+------------+------------+--------------+ | pfctl nat (macos) | ✔ | | | | pf:// | +-------------------+------------+------------+------------+------------+--------------+ | echo | ✔ | | ✔ | | echo:// | +-------------------+------------+------------+------------+------------+--------------+ | tunnel | ✔ | ✔ | ✔ | ✔ | tunnel:// | | (raw socket) | | | | | tunnel{ip}://| +-------------------+------------+------------+------------+------------+--------------+ | websocket | ✔ | ✔ | | | ws:// | | (simple tunnel) | | | | | ws{dst_ip}://| +-------------------+------------+------------+------------+------------+--------------+ | xxx over TLS | ✔ | ✔ | | | xxx+ssl:// | +-------------------+------------+------------+------------+------------+--------------+ | AUTO DETECT | ✔ | | ✔ | | a+b+c+d:// | +-------------------+------------+------------+------------+------------+--------------+

Scheduling Algorithms

+-------------------+------------+------------+------------+------------+ | Name | TCP | UDP | Parameter | Default | +===================+============+============+============+============+ | first_available | ✔ | ✔ | -s fa | ✔ | +-------------------+------------+------------+------------+------------+ | round_robin | ✔ | ✔ | -s rr | | +-------------------+------------+------------+------------+------------+ | random_choice | ✔ | ✔ | -s rc | | +-------------------+------------+------------+------------+------------+ | least_connection | ✔ | | -s lc | | +-------------------+------------+------------+------------+------------+

Requirement

pycryptodome is an optional library to enable faster (C version) cipher. pproxy has many built-in pure python ciphers. They are lightweight and stable, but slower than C ciphers. After speedup with PyPy, pure python ciphers can get similar performance as C version. If the performance is important and don't have PyPy, install pycryptodome instead.

asyncssh_ is an optional library to enable ssh tunnel client support.

These are some performance benchmarks between Python and C ciphers (dataset: 8M):

+---------------------+----------------+ | chacha20-c | 0.64 secs | +---------------------+----------------+ | chacha20-py (pypy3) | 1.32 secs | +---------------------+----------------+ | chacha20-py | 48.86 secs | +---------------------+----------------+

PyPy3 Quickstart:

.. code:: rst

$ pypy3 -m ensurepip $ pypy3 -m pip install asyncio pproxy

.. _pycryptodome: https://pycryptodome.readthedocs.io/en/latest/src/introduction.html .. _asyncssh: https://asyncssh.readthedocs.io/en/latest/ .. _PyPy: http://pypy.org

Usage

.. code:: rst

$ pproxy -h usage: pproxy [-h] [-l LISTEN] [-r RSERVER] [-ul ULISTEN] [-ur URSERVER] [-b BLOCK] [-a ALIVED] [-v] [--ssl SSLFILE] [--pac PAC] [--get GETS] [--sys] [--test TESTURL] [--version]

Proxy server that can tunnel among remote servers by regex rules. Supported protocols: http,socks4,socks5,shadowsocks,shadowsocksr,redirect,pf,tunnel

optional arguments: -h, --help show this help message and exit -l LISTEN tcp server uri (default: http+socks4+socks5://:8080/) -r RSERVER tcp remote server uri (default: direct) -ul ULISTEN udp server setting uri (default: none) -ur URSERVER udp remote server uri (default: direct) -b BLOCK block regex rules -a ALIVED interval to check remote alive (default: no check) -s {fa,rr,rc,lc} scheduling algorithm (default: first_available) -v print verbose output --ssl SSLFILE certfile[,keyfile] if server listen in ssl mode --pac PAC http PAC path --get GETS http custom {path,file} --sys change system proxy setting (mac, windows) --test TEST test this url for all remote proxies and exit --version show program's version number and exit

Online help: https://github.com/qwj/python-proxy

URI Syntax

.. code:: rst

{scheme}://[{cipher}@]{netloc}/[@{localbind}][,{plugins}][?{rules}][#{auth}]

.. _trojan: https://trojan-gfw.github.io/trojan/protocol

URIs can be joined by "" to indicate tunneling by jump. For example, ss://1.2.3.4:1324http://4.5.6.7:4321 make remote connection to the first shadowsocks proxy server, and then jump to the second http proxy server.

.. _AEAD: http://shadowsocks.org/en/spec/AEAD-Ciphers.html

Client API

Server API

Examples

Projects