Skip to content

Add native HTTP/2 proxy support with TLS ALPN and isolated CONNECT streams - #2

Merged
Ray0907 merged 9 commits into
mainfrom
feat/http2-proxy
Sep 19, 2026
Merged

Ray0907 merged 9 commits into
mainfrom
feat/http2-proxy

Conversation

@Ray0907

@Ray0907 Ray0907 commented Sep 19, 2026

Copy link
Copy Markdown
Owner

Summary

Add native client-to-proxy HTTP/2, not just opaque HTTP/2 bytes inside an HTTP/1 CONNECT tunnel. Retain HTTP/1 compatibility and keep deployment to one executable with optional PEM certificate/key files.

Protocol support

  • Optional Rustls TLS listener, certificate/key validation before binding, ALPN h2 / http/1.1, HTTP/1 fallback without ALPN, bounded TLS handshake deadline, and disabled TLS early data.
  • Explicit opt-in plaintext HTTP/2 prior knowledge via AllowH2C; fragmented prefaces have a total deadline and sniffed bytes are preserved. No h2c Upgrade support.
  • Concurrent HTTP requests and independent ordinary CONNECT tunnels on one HTTP/2 connection.
  • Per-stream authentication/filtering, H2-to-H1 Cookie joining, version-correct Via, Host/authority validation, repeated response fields, and hop-by-hop/credential sanitization including trailers.
  • Hyper/h2 own framing, HPACK and flow control. Ordinary HTTP origins are still reached via HTTP/1.1 streaming; HTTPS origins use end-to-end opaque CONNECT.

Resource and lifecycle controls

  • MaxConcurrentStreams (default 32) per H2 connection and MaxInflightRequests (default 256) across the process. Requests beyond application capacity receive 503 rather than entering an unbounded queue.
  • Preserve connection/request permits through response and upstream/tunnel lifetimes; authenticate every stream rather than trusting an entire connection.
  • Independent exchange activity/cancellation, tracked H2 drivers, GOAWAY/draining and forced shutdown.
  • Explicit header/table/frame/window/send-buffer and reset limits; no adaptive receive-window growth.
  • H2-only half-close-safe relay checks upgrade writer errors every 250ms without inserting bytes. This works around Hyper mapping CANCEL/NO_ERROR resets to read EOF, which otherwise retains idle upstream sockets after a reset. Normal HTTP/1 CONNECT still uses Tokio copy_bidirectional.

Verification

  • Add 22 HTTP/2/TLS regression tests, alongside the existing 35 policy/HTTP/CONNECT tests. All 57 passed on Linux during development; current-head results are in Checks.
  • Tests include independently progressing streams, different origins on one connection, 2 MiB upload/response flow control, TLS certificate validation, ALPN/fallback, early CONNECT data, half-close, reset before headers and after half-close, stream/global capacity, independent tunnel idle expiry, and graceful draining.
  • Add scripts/test_curl_proxy.py: real curl interoperability against local HTTP/HTTPS origins, with generated throwaway certificates and full verification. No public Internet origin or insecure TLS bypass.
  • Formal read-only CI runs tests, Clippy with warnings denied, release builds and configuration checks on Linux/macOS, plus curl interoperability on Linux.
  • Remove the temporary branch preparation workflow. Add config/http2-tls.conf.example and exclude deployment keys/certificates from Git tracking.

Scope / limitations

  • No native H2 origin connection pool, upstream proxy chaining, TLS interception, RFC 8441 extended CONNECT/WebSocket Upgrade, server push, or HTTP/3. These are not advertised as implemented.
  • HeaderTimeout covers HTTP/1 request headers and initial H2 preface detection, not a separate deadline for every fragmented H2 HEADERS/CONTINUATION block after negotiation. Resource/reset limits are not a complete DoS defense.
  • This remains a proxy for trusted clients, not a destination-IP/DNS-rebinding sandbox or DLP boundary. h2c does not encrypt BasicAuth. Destination-IP policy, rate limiting and constant-time authentication remain follow-ups.
  • No throughput/latency superiority over C Tinyproxy is claimed. This PR is not a release, comprehensive security audit, or complete HTTP/2 conformance certification.

Manual use

Supply a certificate valid for the proxy hostname and matching PEM private key, then validate/run the TLS example. With an HTTP/2-enabled curl 8.1+:

curl --proxy-http2 \
  --proxy https://localhost:8888 \
  --proxy-cacert config/certs/proxy-cert.pem \
  https://example.com/

--proxy-http2 negotiates the proxy hop; --http2 concerns the origin. See the updated README for exact supported hops, secure defaults, configuration and known limitations.

Ray0907 and others added 9 commits September 19, 2026 08:57
Add opt-in h2c, bounded stream/request resources, independent per-stream
idle and cancellation lifetimes, tracked H2 drivers, and local TLS/H2
regressions. Preserve HTTP1 forwarding and end-to-end origin TLS in CONNECT.
Use a bounded H2-only relay that observes upgrade writer reset errors
without inserting bytes or disturbing valid half-close. Correct the
stream-limit regression to assert actual wire dispatch, not h2 readiness.
Remove the temporary feature-branch preparation workflow; retain only
read-only checks. Add a TLS configuration example and ignore deployment
private keys. Run all regressions, Clippy, release build and config checks.

Ray0907 commented Sep 19, 2026

Copy link
Copy Markdown
Owner Author

Verified final head 66722d46be59ad63653b286bd046f7c20dd4c1a9 in CI run https://github.com/Ray0907/tinyproxy-rust/actions/runs/35411685132.

All jobs passed:

  • Formatting check.
  • Linux and macOS: all-target tests (22 HTTP/2/TLS regressions plus the existing 35 policy/HTTP/CONNECT tests), Clippy with -D warnings, locked release build, and shipped configuration validation.
  • Linux: real curl interoperability with local origins and verified throwaway TLS certificates: HTTP/2 forwarding, HTTP/2 CONNECT to HTTPS and plaintext origins, HTTP/1 HTTPS-proxy fallback, and graceful process shutdown. The curl step is intentionally Linux-only; it was not run on macOS.

The temporary branch preparation workflow is absent from this final tree; retained CI has read-only contents permission. No deployment certificates or private keys were committed.

PR remains unmerged; main is unchanged by this HTTP/2 implementation. These results do not establish performance superiority, exhaustive HTTP/2 conformance, or full DoS resistance. The README documents the remaining H2 fragmented-header deadline limitation and the inbound-H2 / HTTP/1-origin boundary.

@Ray0907
Ray0907 merged commit d500d25 into main Sep 19, 2026
6 checks passed
@Ray0907
Ray0907 deleted the feat/http2-proxy branch September 19, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant