Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
72cf0b4
docs: record phase 0 compatibility baseline
by275 Aug 22, 2026
d82ff73
build: add modern Python package metadata
by275 Aug 22, 2026
6855f7b
chore: ignore Python build artifacts
by275 Aug 22, 2026
857912d
test: add phase 1 compatibility contracts
by275 Aug 22, 2026
35572bf
fix: correct websocket protocol detection
by275 Aug 22, 2026
f4ffba5
ci: add core Python checks
by275 Aug 22, 2026
1b17e9e
build: keep setup.py as a compatibility shim
by275 Aug 22, 2026
2ca6534
style: format phase 1 tests
by275 Aug 22, 2026
662117c
docs: document Git installation and current CLI
by275 Aug 22, 2026
11e6399
ci: smoke-test the installed CLI
by275 Aug 22, 2026
c1e22ec
fix: report version from installed metadata
by275 Aug 22, 2026
a2c292f
ci: add optional backend import gates
by275 Aug 22, 2026
1c4cd7a
refactor: add stream compatibility helpers
by275 Aug 22, 2026
c86fbc9
refactor: route protocol reads through transport helpers
by275 Aug 22, 2026
ee64ca4
refactor: centralize stream buffer access
by275 Aug 22, 2026
1e2f63a
test: cover websocket frame contracts
by275 Aug 22, 2026
69e7128
test: add pure Python cipher contracts
by275 Aug 22, 2026
f95e407
refactor: route payload reads through transport helpers
by275 Aug 22, 2026
0340ac9
refactor: add protocol validation errors
by275 Aug 22, 2026
eb3f5ec
fix: keep protocol validation under optimized Python
by275 Aug 22, 2026
4cb7ff2
refactor: isolate TLS stream adapter
by275 Aug 22, 2026
88569e3
refactor: isolate WebSocket stream adapter
by275 Aug 22, 2026
1467c3b
refactor: isolate stream cipher adapter
by275 Aug 22, 2026
2cac5e6
refactor: add typed proxy configuration
by275 Aug 22, 2026
f5ee1c0
refactor: isolate relay task coordination
by275 Aug 22, 2026
b6024d3
api: expose additive configuration contracts
by275 Aug 22, 2026
da96af2
fix: cancel backward tunnel tasks on close
by275 Aug 22, 2026
18a8bc3
refactor: isolate CLI application lifecycle
by275 Aug 22, 2026
f41ec96
refactor: isolate HTTP/2 optional adapter
by275 Aug 22, 2026
d116cf6
refactor: isolate SSH optional adapter
by275 Aug 22, 2026
6fd9b61
refactor: isolate QUIC and HTTP/3 adapters
by275 Aug 22, 2026
7d8ccd5
fix: release UDP connection accounting
by275 Aug 22, 2026
1a7f3ca
perf: avoid duplicate stream timeout wrappers
by275 Aug 22, 2026
203da5a
docs: record phase 4 performance checks
by275 Aug 22, 2026
8337a1b
perf: close phase 4 streaming read regressions
by275 Aug 22, 2026
51c6c92
refactor: convert transport helpers into a package
by275 Aug 22, 2026
0cfe437
refactor: extract HTTP protocol parsing
by275 Aug 22, 2026
7cc3168
refactor: extract SOCKS address parsing
by275 Aug 22, 2026
fa7cd93
refactor: move netloc parsing into config
by275 Aug 22, 2026
f65be41
refactor: extract protocol base classes
by275 Aug 22, 2026
a0535cf
refactor: extract HTTP protocol classes
by275 Aug 22, 2026
0354ae2
refactor: extract SOCKS protocol classes
by275 Aug 22, 2026
1c117f1
refactor: split protocol adapters and registry
by275 Aug 22, 2026
34f5e86
refactor: add runtime task ownership
by275 Aug 22, 2026
1cdc5c3
feat: add additive runtime observability APIs
by275 Aug 22, 2026
887224b
docs: record final structural performance review
by275 Aug 22, 2026
7a31e6d
fix: prefer checkout version metadata
by275 Aug 22, 2026
6416747
fix: align source checkout version fallback
by275 Aug 22, 2026
b26b209
docs: align final performance review reference
by275 Aug 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Core checks

on:
push:
branches:
- master
- modernize
pull_request:

permissions:
contents: read

jobs:
core:
name: Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.12", "3.13"]

steps:
- name: Check out source
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip

- name: Install the package and development tools
run: python -m pip install ".[dev]"

- name: Compile source and tests
run: python -m compileall -q pproxy tests

- name: Smoke-test the installed CLI
run: |
python -m pproxy --version
python -m pproxy -h

- name: Run unittest contracts
run: python -m unittest discover -s tests -p "test_*.py" -v

- name: Run pytest collection
run: python -m pytest -q

- name: Lint new tests
run: python -m ruff check tests/test_contracts.py tests/test_websocket.py

- name: Build a wheel
run: python -m build --wheel
48 changes: 48 additions & 0 deletions .github/workflows/optional.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Optional backend checks

on:
push:
branches:
- master
- modernize
pull_request:

permissions:
contents: read

jobs:
imports:
name: ${{ matrix.name }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- name: HTTP/2
extra: h2
module: h2
- name: SSH
extra: sshtunnel
module: asyncssh
- name: QUIC and HTTP/3
extra: quic
module: aioquic
- name: Accelerated crypto and event loop
extra: accelerated
module: Crypto

steps:
- name: Check out source
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip

- name: Install the selected optional extra
run: python -m pip install ".[${{ matrix.extra }}]"

- name: Import the optional backend
run: python -c "import importlib; importlib.import_module('${{ matrix.module }}')"
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
__pycache__/
*.py[cod]
build/
dist/
*.egg-info/
.pytest_cache/
.coverage
htmlcov/
.venv/
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Changelog

## Unreleased

This modernization keeps the existing CLI, URI grammar, public factory names,
and protocol wire formats compatible for Python 3.12+ Git installations.

### Internal improvements

- Split protocol parsing and implementations into `pproxy.protocols` while
keeping the `pproxy.proto` compatibility facade.
- Added a protocol registry extension hook for future optional adapters such
as `cfp://`; CFP is not enabled by this branch.
- Added `pproxy.runtime.TaskRegistry` and additive `wait_closed()`, `aclose()`,
and async context-manager support to runtime proxy objects. Existing
synchronous `close()` and `start_server(args)` calls remain valid.
- Added opt-in JSON logging through `pproxy.observability`; legacy verbose
output is unchanged unless an application configures the new logger.
- Preserved the existing optional dependency boundaries for H2, H3, QUIC,
SSH, accelerated ciphers, and daemon mode.

### Verification

- Core unittest contracts pass under normal and optimized (`python -O`)
interpreters.
- Clean wheel builds include all protocol, transport, and runtime subpackages.
- Dockerfile, `entrypoint.sh`, and `dev.txt` are intentionally outside this
modernization and are not part of the release commits.
49 changes: 27 additions & 22 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
python-proxy
============

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

.. |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
$ python3 -m pip install "pproxy @ git+https://github.com/by275/python-proxy.git"
Successfully installed pproxy from Git
$ pproxy
Serving on :8080 by http,socks4,socks5
^C
Expand All @@ -35,8 +24,8 @@ Optional: (better performance with C ciphers)

.. code:: rst

$ pip3 install pproxy[accelerated]
Successfully installed pycryptodome-3.6.4
$ python3 -m pip install "pproxy[accelerated] @ git+https://github.com/by275/python-proxy.git"
Successfully installed pycryptodome and uvloop

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

Expand Down Expand Up @@ -166,6 +155,12 @@ pycryptodome_ is an optional library to enable faster (C version) cipher. **ppro

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

h2_ is an optional library to enable HTTP/2 support.

aioquic_ is an optional library to enable HTTP/3 and QUIC support.

python-daemon_ is an optional library for the ``--daemon`` mode.

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

+---------------------+----------------+
Expand All @@ -181,10 +176,13 @@ PyPy3 Quickstart:
.. code:: rst

$ pypy3 -m ensurepip
$ pypy3 -m pip install asyncio pproxy
$ pypy3 -m pip install "pproxy @ git+https://github.com/by275/python-proxy.git"

.. _pycryptodome: https://pycryptodome.readthedocs.io/en/latest/src/introduction.html
.. _asyncssh: https://asyncssh.readthedocs.io/en/latest/
.. _h2: https://python-hyper.org/projects/h2/en/stable/
.. _aioquic: https://github.com/aiortc/aioquic
.. _python-daemon: https://pypi.org/project/python-daemon/
.. _PyPy: http://pypy.org

Usage
Expand All @@ -194,13 +192,14 @@ Usage

$ 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]
[-b BLOCK] [-a ALIVED] [-s {fa,rr,rc,lc}] [-d] [-v]
[--ssl SSLFILE] [--pac PAC] [--get GETS] [--auth AUTHTIME]
[--sys] [--reuse] [--daemon] [--test TEST] [--version]

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

optional arguments:
options:
-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)
Expand All @@ -209,16 +208,23 @@ Usage
-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)
-d turn on debug to see tracebacks (default: no debug)
-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}
--auth AUTHTIME re-auth time interval for same ip (default: 86400*30)
--sys change system proxy setting (mac, windows)
--reuse set SO_REUSEPORT (Linux only)
--daemon run as a daemon (Linux only)
--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>

The ``cfp://`` Cloudflare Worker protocol is a planned compatibility target from
the ``feat-cfp`` branch and is not enabled in this branch yet.

URI Syntax
----------

Expand Down Expand Up @@ -759,4 +765,3 @@ Projects

+ `python-vpn <https://github.com/qwj/python-vpn>`_ - VPN Server (IPSec,IKE,IKEv2,L2TP,WireGuard) in pure python
+ `shadowproxy <https://github.com/guyingbo/shadowproxy>`_ - Awesome python proxy implementation by guyingbo

96 changes: 96 additions & 0 deletions docs/COMPATIBILITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Compatibility Baseline

This document defines the Phase 0 baseline for the modernization work. It is not a list of features to remove; it records the external surfaces that must remain stable after internal refactoring.

## Baseline

- Code baseline: `master` `768d87a` (`Fix websocket tunnel frame handling`)
- Working branch: `modernize`
- Supported Python: `3.12+`
- Installation: `pip install git+https://github.com/...` from the personal GitHub repository
- PyPI distribution: none
- `cfp://`: behavior from `feat-cfp` `3fabba2` is a future compatibility target. Phase 0 does not add it to `master`.
- Excluded files: `Dockerfile`, `entrypoint.sh`, and `dev.txt` must not be modified or committed unless explicitly requested.

## External Compatibility Surface

| Area | Surface to preserve | Phase 0 status |
| --- | --- | --- |
| CLI | `pproxy`, `-l`, `-r`, `-ul`, `-ur`, `-b`, `-a`, `-s`, `-d`, `-v`, `--ssl`, `--pac`, `--get`, `--auth`, `--sys`, `--reuse`, `--daemon`, `--test`, `--version` | Recorded from the current parser |
| Defaults | `http+socks4+socks5://:8080/` when no listener is specified; `fa` scheduling | Preserve |
| Python API | `pproxy.Connection`, `pproxy.Server`, `pproxy.Rule`, `pproxy.DIRECT` | Preserve as compatibility facades |
| Server API | `Server(uri)`, `start_server(args)`, and the existing args shape | Preserve while introducing internal adapters incrementally |
| URI grammar | Scheme composition, cipher/userinfo, netloc, local bind, plugin, query rule, fragment auth, and `__` jumps | Preserve |
| Authentication | HTTP/SOCKS/WebSocket/remote proxy auth and the meaning of the per-IP auth cache | Preserve |
| Transports | TCP, UDP, Unix domain sockets, backward tunnels, transparent redirect/PF | Preserve |
| Scheduling | First available (`fa`), round robin (`rr`), random choice (`rc`), least connection (`lc`) | Preserve semantics |
| Wire formats | HTTP, SOCKS4/5, Shadowsocks/SSR/Trojan, WebSocket, and cipher/plugin framing | Lock down with golden and integration tests |
| Optional backends | H2, H3, QUIC, SSH, and future CFP | Isolate behind adapters and optional test jobs |

## Protocol Inventory

| Scheme | Current baseline | Direction | Notes |
| --- | --- | --- | --- |
| `http` / `httponly` | Implemented | Preserve | CONNECT and HTTP request forwarding |
| `socks4` / `socks5` / `socks` | Implemented | Preserve | Includes SOCKS5 TCP/UDP |
| `ss` / `ssr` / `trojan` | Implemented | Preserve | Includes cipher, auth, and plugin framing |
| `ssh` | Optional `asyncssh` | Preserve | Verify separately when the dependency is available |
| `h2` | Optional `h2` | Preserve | HTTP/2 stream and flow-control adapter |
| `h3` / `quic` | Optional `aioquic` | Preserve | HTTP/3 and QUIC stream adapters |
| `redir` / `pf` / `tunnel` | Platform/socket dependent | Preserve | Verify in the relevant Linux/macOS environments |
| `ws` | Implemented | Preserve | Needs framing, masking, and control-frame regression tests |
| `cfp` | Implemented only on `feat-cfp` | Future support target | TLS WebSocket, target header, auth, and graceful close; do not add now |
| `echo` / `direct` / `ssl` / `secure` / `in` | Implemented | Preserve | Includes combined-scheme helper behavior |

## Cipher and Plugin Policy

- Do not remove current cipher names or pure-Python fallbacks in the first modernization release.
- Preserve the current selection behavior where PyCryptodome acceleration is used when available.
- Lock down AEAD packet boundaries, nonce/IV handling, tag validation, and pure-Python versus accelerated parity with golden/vector tests.
- Verify Shadowsocks plugin handshakes and framing separately from the protocol refactor.
- Propose legacy cipher deprecations only after a separate security review.

## Baseline Verification Commands

These commands are intended to be reproducible Phase 0 checks:

```text
python3 -m compileall -q pproxy tests
python3 tests/benchmark_proxy.py --requests 20 --concurrency 2 --payload-size 1024 --warmup 1
python3 -m pproxy -h
python3 -m pproxy --version
python3 -m pytest -q
```

Observed in the current environment:

- `compileall`: passed
- Loopback benchmark for direct/http/socks5: passed
- `-h`: worked
- `--version` from an uninstalled source checkout: displayed `unknown`
- `pytest`: unavailable because pytest is not installed (`No module named pytest`)

A representative run with Python `3.12.3`, 20 requests, concurrency 2, and a 1 KiB payload produced direct `3232.3 req/s`, HTTP `3146.5 req/s`, and SOCKS5 `3120.1 req/s`. These values are environment-dependent and are not absolute performance gates.

## Regression Candidates to Lock Down in Phase 0

The following are recorded as test targets before making source changes:

1. `WS.guess()` compares the reader object with `b'GET '` instead of the header it read.
2. WebSocket fragmented frames, masking, extended lengths, ping/pong, and close handling.
3. `ProxyBackward` close/CLOSE_WAIT behavior and normal EOF cleanup.
4. Handshake/auth/checksum/tag validation that currently relies on `assert`, including behavior under `python -O`.
5. Lifecycle behavior while moving direct access to private `asyncio.StreamReader/Writer`, `sslproto`, H2, and QUIC APIs behind adapters.
6. `cfp://` handshake, `X-Proxy-Target`, Authorization, and close control, kept separate from ordinary `ws://`.

## Change Approval Rule

Internal changes should follow a compatibility-preserving path. If a breaking change is found to be necessary, stop before implementing it and request review for changes to any of these surfaces:

- CLI options or output semantics
- URI syntax, default ports, or scheme behavior
- Public Python APIs and return/callback shapes
- Protocol, cipher, or plugin wire behavior
- Authentication, security, or connection-close semantics

The review request must include current behavior, why the change is needed, affected surfaces, alternatives, a migration example, and a rollback plan.
Loading
Loading