Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e8db7bc96d | |||
| 2621dea9fa | |||
| bb5b9b3d12 | |||
| d70c2d77ed | |||
| 4cf13c36f8 | |||
| 37c7233780 | |||
| 15d0a721d5 | |||
| af970c447e |
29
changelog.md
29
changelog.md
@@ -1,5 +1,34 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-03-19 - 25.16.3 - fix(rustproxy)
|
||||||
|
upgrade fallback UDP listeners to QUIC when TLS certificates become available
|
||||||
|
|
||||||
|
- Rebuild and apply QUIC TLS configuration during route and certificate updates instead of only when adding new UDP ports.
|
||||||
|
- Add logic to drain UDP sessions, stop raw fallback listeners, and start QUIC endpoints on existing ports once TLS is available.
|
||||||
|
- Retry QUIC endpoint creation during upgrade and fall back to rebinding raw UDP if the upgrade cannot complete.
|
||||||
|
|
||||||
|
## 2026-03-19 - 25.16.2 - fix(rustproxy-http)
|
||||||
|
cache backend Alt-Svc only from original upstream responses during protocol auto-detection
|
||||||
|
|
||||||
|
- Moves Alt-Svc discovery into streaming response construction so it reads backend headers before response filters inject client-facing Alt-Svc values
|
||||||
|
- Stores the protocol cache key in connection activity during auto-detect mode and clears it after HTTP/3 connection failure to avoid re-caching failed H3 routes
|
||||||
|
- Prevents fallback requests from reintroducing stale or self-injected Alt-Svc entries that could cause repeated H3 retry loops
|
||||||
|
|
||||||
|
## 2026-03-19 - 25.16.1 - fix(http-proxy)
|
||||||
|
avoid repeated HTTP/3 recaching after QUIC fallback and document backend protocol selection
|
||||||
|
|
||||||
|
- Suppress Alt-Svc HTTP/3 recaching after a failed QUIC backend connection to prevent repeated H3 timeout fallback loops
|
||||||
|
- Force an ALPN probe on TCP fallback so auto detection correctly reselects HTTP/2 or HTTP/1.1 after H3 connection failure
|
||||||
|
- Add README documentation for best-effort backendProtocol selection and supported protocol modes
|
||||||
|
|
||||||
|
## 2026-03-19 - 25.16.0 - feat(quic,http3)
|
||||||
|
add HTTP/3 proxy handling and hot-reload QUIC TLS configuration
|
||||||
|
|
||||||
|
- initialize and wire H3ProxyService into QUIC listeners so HTTP/3 requests are handled instead of being kept as placeholder connections
|
||||||
|
- add backend HTTP/3 support with protocol caching that stores Alt-Svc advertised H3 ports for auto-detection
|
||||||
|
- hot-swap TLS certificates across active QUIC endpoints and require terminating TLS for QUIC route validation
|
||||||
|
- document QUIC route setup with required TLS and ACME configuration
|
||||||
|
|
||||||
## 2026-03-19 - 25.15.0 - feat(readme)
|
## 2026-03-19 - 25.15.0 - feat(readme)
|
||||||
document UDP, QUIC, and HTTP/3 support in the README
|
document UDP, QUIC, and HTTP/3 support in the README
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartproxy",
|
"name": "@push.rocks/smartproxy",
|
||||||
"version": "25.15.0",
|
"version": "25.16.3",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.",
|
"description": "A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
|||||||
66
readme.md
66
readme.md
@@ -306,6 +306,10 @@ const quicRoute: IRouteConfig = {
|
|||||||
port: 8443,
|
port: 8443,
|
||||||
backendTransport: 'tcp' // 👈 Translate QUIC → TCP for backend
|
backendTransport: 'tcp' // 👈 Translate QUIC → TCP for backend
|
||||||
}],
|
}],
|
||||||
|
tls: {
|
||||||
|
mode: 'terminate',
|
||||||
|
certificate: 'auto' // 👈 QUIC requires TLS 1.3
|
||||||
|
},
|
||||||
udp: {
|
udp: {
|
||||||
quic: {
|
quic: {
|
||||||
enableHttp3: true,
|
enableHttp3: true,
|
||||||
@@ -318,9 +322,47 @@ const quicRoute: IRouteConfig = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const proxy = new SmartProxy({ routes: [quicRoute] });
|
const proxy = new SmartProxy({
|
||||||
|
acme: { email: 'ssl@example.com' },
|
||||||
|
routes: [quicRoute]
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 🚄 Best-Effort Backend Protocol (H3 > H2 > H1)
|
||||||
|
|
||||||
|
SmartProxy automatically uses the **highest protocol your backend supports** for HTTP requests. The backend protocol is independent of the client protocol — a client using HTTP/1.1 can be forwarded over HTTP/3 to the backend, and vice versa.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const route: IRouteConfig = {
|
||||||
|
name: 'auto-protocol',
|
||||||
|
match: { ports: 443, domains: 'app.example.com' },
|
||||||
|
action: {
|
||||||
|
type: 'forward',
|
||||||
|
targets: [{ host: 'backend', port: 8443 }],
|
||||||
|
tls: { mode: 'terminate', certificate: 'auto' },
|
||||||
|
options: {
|
||||||
|
backendProtocol: 'auto' // 👈 Default — best-effort selection
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
**How protocol discovery works (browser model):**
|
||||||
|
|
||||||
|
1. First request → TLS ALPN probe detects H2 or H1
|
||||||
|
2. Backend response inspected for `Alt-Svc: h3=":port"` header
|
||||||
|
3. If H3 advertised → cached and used for subsequent requests via QUIC
|
||||||
|
4. Graceful fallback: H3 failure → H2 → H1 with automatic cache invalidation
|
||||||
|
|
||||||
|
| `backendProtocol` | Behavior |
|
||||||
|
|---|---|
|
||||||
|
| `'auto'` (default) | Best-effort: H3 > H2 > H1 with Alt-Svc discovery |
|
||||||
|
| `'http1'` | Always HTTP/1.1 |
|
||||||
|
| `'http2'` | Always HTTP/2 (hard-fail if unsupported) |
|
||||||
|
| `'http3'` | Always HTTP/3 via QUIC (hard-fail if unsupported) |
|
||||||
|
|
||||||
|
> **Note:** WebSocket upgrades always use HTTP/1.1 to the backend regardless of `backendProtocol`, since there's no performance benefit from H2/H3 Extended CONNECT for tunneled connections, and backend support is rare.
|
||||||
|
|
||||||
### 🔁 Dual-Stack TCP + UDP Route
|
### 🔁 Dual-Stack TCP + UDP Route
|
||||||
|
|
||||||
Listen on both TCP and UDP with a single route — handle each transport with its own handler:
|
Listen on both TCP and UDP with a single route — handle each transport with its own handler:
|
||||||
@@ -769,6 +811,28 @@ interface IRouteLoadBalancing {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Backend Protocol Options
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Set on action.options
|
||||||
|
{
|
||||||
|
action: {
|
||||||
|
type: 'forward',
|
||||||
|
targets: [...],
|
||||||
|
options: {
|
||||||
|
backendProtocol: 'auto' | 'http1' | 'http2' | 'http3'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
| Value | Backend Behavior |
|
||||||
|
|-------|-----------------|
|
||||||
|
| `'auto'` | Best-effort: discovers H3 via Alt-Svc, probes H2 via ALPN, falls back to H1 |
|
||||||
|
| `'http1'` | Always HTTP/1.1 (no ALPN probe) |
|
||||||
|
| `'http2'` | Always HTTP/2 (hard-fail if handshake fails) |
|
||||||
|
| `'http3'` | Always HTTP/3 over QUIC (3s connect timeout, hard-fail if unreachable) |
|
||||||
|
|
||||||
### UDP & QUIC Options
|
### UDP & QUIC Options
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
//! Bounded, TTL-based protocol detection cache for HTTP/2 auto-detection.
|
//! Bounded, TTL-based protocol detection cache for backend protocol auto-detection.
|
||||||
//!
|
//!
|
||||||
//! Caches the ALPN-negotiated protocol (H1 or H2) per backend endpoint and requested
|
//! Caches the detected protocol (H1, H2, or H3) per backend endpoint and requested
|
||||||
//! domain (host:port + requested_host). This prevents cache oscillation when multiple
|
//! domain (host:port + requested_host). This prevents cache oscillation when multiple
|
||||||
//! frontend domains share the same backend but differ in HTTP/2 support.
|
//! frontend domains share the same backend but differ in protocol support.
|
||||||
|
//!
|
||||||
|
//! H3 detection uses the browser model: Alt-Svc headers from H1/H2 responses are
|
||||||
|
//! parsed and cached, including the advertised H3 port (which may differ from TCP).
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@@ -29,6 +32,14 @@ pub enum DetectedProtocol {
|
|||||||
H3,
|
H3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Result of a protocol cache lookup.
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct CachedProtocol {
|
||||||
|
pub protocol: DetectedProtocol,
|
||||||
|
/// For H3: the port advertised by Alt-Svc (may differ from TCP port).
|
||||||
|
pub h3_port: Option<u16>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Key for the protocol cache: (host, port, requested_host).
|
/// Key for the protocol cache: (host, port, requested_host).
|
||||||
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
|
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
|
||||||
pub struct ProtocolCacheKey {
|
pub struct ProtocolCacheKey {
|
||||||
@@ -43,6 +54,8 @@ pub struct ProtocolCacheKey {
|
|||||||
struct CachedEntry {
|
struct CachedEntry {
|
||||||
protocol: DetectedProtocol,
|
protocol: DetectedProtocol,
|
||||||
detected_at: Instant,
|
detected_at: Instant,
|
||||||
|
/// For H3: the port advertised by Alt-Svc (may differ from TCP port).
|
||||||
|
h3_port: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bounded, TTL-based protocol detection cache.
|
/// Bounded, TTL-based protocol detection cache.
|
||||||
@@ -75,11 +88,14 @@ impl ProtocolCache {
|
|||||||
|
|
||||||
/// Look up the cached protocol for a backend endpoint.
|
/// Look up the cached protocol for a backend endpoint.
|
||||||
/// Returns `None` if not cached or expired (caller should probe via ALPN).
|
/// Returns `None` if not cached or expired (caller should probe via ALPN).
|
||||||
pub fn get(&self, key: &ProtocolCacheKey) -> Option<DetectedProtocol> {
|
pub fn get(&self, key: &ProtocolCacheKey) -> Option<CachedProtocol> {
|
||||||
let entry = self.cache.get(key)?;
|
let entry = self.cache.get(key)?;
|
||||||
if entry.detected_at.elapsed() < PROTOCOL_CACHE_TTL {
|
if entry.detected_at.elapsed() < PROTOCOL_CACHE_TTL {
|
||||||
debug!("Protocol cache hit: {:?} for {}:{} (requested: {:?})", entry.protocol, key.host, key.port, key.requested_host);
|
debug!("Protocol cache hit: {:?} for {}:{} (requested: {:?})", entry.protocol, key.host, key.port, key.requested_host);
|
||||||
Some(entry.protocol)
|
Some(CachedProtocol {
|
||||||
|
protocol: entry.protocol,
|
||||||
|
h3_port: entry.h3_port,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
// Expired — remove and return None to trigger re-probe
|
// Expired — remove and return None to trigger re-probe
|
||||||
drop(entry); // release DashMap ref before remove
|
drop(entry); // release DashMap ref before remove
|
||||||
@@ -91,6 +107,16 @@ impl ProtocolCache {
|
|||||||
/// Insert a detected protocol into the cache.
|
/// Insert a detected protocol into the cache.
|
||||||
/// If the cache is at capacity, evict the oldest entry first.
|
/// If the cache is at capacity, evict the oldest entry first.
|
||||||
pub fn insert(&self, key: ProtocolCacheKey, protocol: DetectedProtocol) {
|
pub fn insert(&self, key: ProtocolCacheKey, protocol: DetectedProtocol) {
|
||||||
|
self.insert_with_h3_port(key, protocol, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert an H3 detection result with the Alt-Svc advertised port.
|
||||||
|
pub fn insert_h3(&self, key: ProtocolCacheKey, h3_port: u16) {
|
||||||
|
self.insert_with_h3_port(key, DetectedProtocol::H3, Some(h3_port));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert a protocol detection result with an optional H3 port.
|
||||||
|
fn insert_with_h3_port(&self, key: ProtocolCacheKey, protocol: DetectedProtocol, h3_port: Option<u16>) {
|
||||||
if self.cache.len() >= PROTOCOL_CACHE_MAX_ENTRIES && !self.cache.contains_key(&key) {
|
if self.cache.len() >= PROTOCOL_CACHE_MAX_ENTRIES && !self.cache.contains_key(&key) {
|
||||||
// Evict the oldest entry to stay within bounds
|
// Evict the oldest entry to stay within bounds
|
||||||
let oldest = self.cache.iter()
|
let oldest = self.cache.iter()
|
||||||
@@ -103,6 +129,7 @@ impl ProtocolCache {
|
|||||||
self.cache.insert(key, CachedEntry {
|
self.cache.insert(key, CachedEntry {
|
||||||
protocol,
|
protocol,
|
||||||
detected_at: Instant::now(),
|
detected_at: Instant::now(),
|
||||||
|
h3_port,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,10 @@ struct ConnActivity {
|
|||||||
/// increments on creation and decrements on Drop, keeping the watchdog aware that
|
/// increments on creation and decrements on Drop, keeping the watchdog aware that
|
||||||
/// a response body is still streaming after the request handler has returned.
|
/// a response body is still streaming after the request handler has returned.
|
||||||
active_requests: Option<Arc<AtomicU64>>,
|
active_requests: Option<Arc<AtomicU64>>,
|
||||||
|
/// Protocol cache key for Alt-Svc discovery. When set, `build_streaming_response`
|
||||||
|
/// checks the backend's original response headers for Alt-Svc before our
|
||||||
|
/// ResponseFilter injects its own. None when not in auto-detect mode or after H3 failure.
|
||||||
|
alt_svc_cache_key: Option<crate::protocol_cache::ProtocolCacheKey>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default upstream connect timeout (30 seconds).
|
/// Default upstream connect timeout (30 seconds).
|
||||||
@@ -58,6 +62,18 @@ const DEFAULT_WS_INACTIVITY_TIMEOUT: std::time::Duration = std::time::Duration::
|
|||||||
/// Default WebSocket max lifetime (24 hours).
|
/// Default WebSocket max lifetime (24 hours).
|
||||||
const DEFAULT_WS_MAX_LIFETIME: std::time::Duration = std::time::Duration::from_secs(86400);
|
const DEFAULT_WS_MAX_LIFETIME: std::time::Duration = std::time::Duration::from_secs(86400);
|
||||||
|
|
||||||
|
/// Timeout for QUIC (H3) backend connections. Short because UDP is often firewalled.
|
||||||
|
const QUIC_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(3);
|
||||||
|
|
||||||
|
/// Protocol decision for backend connection.
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum ProtocolDecision {
|
||||||
|
H1,
|
||||||
|
H2,
|
||||||
|
H3 { port: u16 },
|
||||||
|
AlpnProbe,
|
||||||
|
}
|
||||||
|
|
||||||
/// RAII guard that decrements the active request counter on drop.
|
/// RAII guard that decrements the active request counter on drop.
|
||||||
/// Ensures the counter is correct even if the request handler panics.
|
/// Ensures the counter is correct even if the request handler panics.
|
||||||
struct ActiveRequestGuard {
|
struct ActiveRequestGuard {
|
||||||
@@ -190,6 +206,9 @@ pub struct HttpProxyService {
|
|||||||
ws_inactivity_timeout: std::time::Duration,
|
ws_inactivity_timeout: std::time::Duration,
|
||||||
/// WebSocket maximum connection lifetime.
|
/// WebSocket maximum connection lifetime.
|
||||||
ws_max_lifetime: std::time::Duration,
|
ws_max_lifetime: std::time::Duration,
|
||||||
|
/// Shared QUIC client endpoint for outbound H3 backend connections.
|
||||||
|
/// Lazily initialized on first H3 backend attempt.
|
||||||
|
quinn_client_endpoint: Arc<quinn::Endpoint>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpProxyService {
|
impl HttpProxyService {
|
||||||
@@ -209,6 +228,7 @@ impl HttpProxyService {
|
|||||||
http_idle_timeout: DEFAULT_HTTP_IDLE_TIMEOUT,
|
http_idle_timeout: DEFAULT_HTTP_IDLE_TIMEOUT,
|
||||||
ws_inactivity_timeout: DEFAULT_WS_INACTIVITY_TIMEOUT,
|
ws_inactivity_timeout: DEFAULT_WS_INACTIVITY_TIMEOUT,
|
||||||
ws_max_lifetime: DEFAULT_WS_MAX_LIFETIME,
|
ws_max_lifetime: DEFAULT_WS_MAX_LIFETIME,
|
||||||
|
quinn_client_endpoint: Arc::new(Self::create_quinn_client_endpoint()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,6 +253,7 @@ impl HttpProxyService {
|
|||||||
http_idle_timeout: DEFAULT_HTTP_IDLE_TIMEOUT,
|
http_idle_timeout: DEFAULT_HTTP_IDLE_TIMEOUT,
|
||||||
ws_inactivity_timeout: DEFAULT_WS_INACTIVITY_TIMEOUT,
|
ws_inactivity_timeout: DEFAULT_WS_INACTIVITY_TIMEOUT,
|
||||||
ws_max_lifetime: DEFAULT_WS_MAX_LIFETIME,
|
ws_max_lifetime: DEFAULT_WS_MAX_LIFETIME,
|
||||||
|
quinn_client_endpoint: Arc::new(Self::create_quinn_client_endpoint()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +345,7 @@ impl HttpProxyService {
|
|||||||
let cn = cancel_inner.clone();
|
let cn = cancel_inner.clone();
|
||||||
let la = Arc::clone(&la_inner);
|
let la = Arc::clone(&la_inner);
|
||||||
let st = start;
|
let st = start;
|
||||||
let ca = ConnActivity { last_activity: Arc::clone(&la_inner), start, active_requests: Some(Arc::clone(&ar_inner)) };
|
let ca = ConnActivity { last_activity: Arc::clone(&la_inner), start, active_requests: Some(Arc::clone(&ar_inner)), alt_svc_cache_key: None };
|
||||||
async move {
|
async move {
|
||||||
let result = svc.handle_request(req, peer, port, cn, ca).await;
|
let result = svc.handle_request(req, peer, port, cn, ca).await;
|
||||||
// Mark request end — update activity timestamp before guard drops
|
// Mark request end — update activity timestamp before guard drops
|
||||||
@@ -401,7 +422,7 @@ impl HttpProxyService {
|
|||||||
peer_addr: std::net::SocketAddr,
|
peer_addr: std::net::SocketAddr,
|
||||||
port: u16,
|
port: u16,
|
||||||
cancel: CancellationToken,
|
cancel: CancellationToken,
|
||||||
conn_activity: ConnActivity,
|
mut conn_activity: ConnActivity,
|
||||||
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
|
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
|
||||||
let host = req.headers()
|
let host = req.headers()
|
||||||
.get("host")
|
.get("host")
|
||||||
@@ -645,37 +666,101 @@ impl HttpProxyService {
|
|||||||
|
|
||||||
// --- Resolve protocol decision based on backend protocol mode ---
|
// --- Resolve protocol decision based on backend protocol mode ---
|
||||||
let is_auto_detect_mode = matches!(backend_protocol_mode, rustproxy_config::BackendProtocol::Auto);
|
let is_auto_detect_mode = matches!(backend_protocol_mode, rustproxy_config::BackendProtocol::Auto);
|
||||||
let (use_h2, needs_alpn_probe) = match backend_protocol_mode {
|
let protocol_cache_key = crate::protocol_cache::ProtocolCacheKey {
|
||||||
rustproxy_config::BackendProtocol::Http1 => (false, false),
|
|
||||||
rustproxy_config::BackendProtocol::Http2 => (true, false),
|
|
||||||
rustproxy_config::BackendProtocol::Http3 => {
|
|
||||||
// HTTP/3 (QUIC) backend connections not yet implemented — fall back to H1
|
|
||||||
warn!("backendProtocol 'http3' not yet implemented, falling back to http1");
|
|
||||||
(false, false)
|
|
||||||
}
|
|
||||||
rustproxy_config::BackendProtocol::Auto => {
|
|
||||||
if !upstream.use_tls {
|
|
||||||
// No ALPN without TLS — default to H1
|
|
||||||
(false, false)
|
|
||||||
} else {
|
|
||||||
let cache_key = crate::protocol_cache::ProtocolCacheKey {
|
|
||||||
host: upstream.host.clone(),
|
host: upstream.host.clone(),
|
||||||
port: upstream.port,
|
port: upstream.port,
|
||||||
requested_host: host.clone(),
|
requested_host: host.clone(),
|
||||||
};
|
};
|
||||||
match self.protocol_cache.get(&cache_key) {
|
let protocol_decision = match backend_protocol_mode {
|
||||||
Some(crate::protocol_cache::DetectedProtocol::H2) => (true, false),
|
rustproxy_config::BackendProtocol::Http1 => ProtocolDecision::H1,
|
||||||
Some(crate::protocol_cache::DetectedProtocol::H1) => (false, false),
|
rustproxy_config::BackendProtocol::Http2 => ProtocolDecision::H2,
|
||||||
Some(crate::protocol_cache::DetectedProtocol::H3) => {
|
rustproxy_config::BackendProtocol::Http3 => ProtocolDecision::H3 { port: upstream.port },
|
||||||
// H3 cached but we're on TCP — fall back to H2 probe
|
rustproxy_config::BackendProtocol::Auto => {
|
||||||
(false, true)
|
if !upstream.use_tls {
|
||||||
|
// No ALPN without TLS, no QUIC without TLS — default to H1
|
||||||
|
ProtocolDecision::H1
|
||||||
|
} else {
|
||||||
|
match self.protocol_cache.get(&protocol_cache_key) {
|
||||||
|
Some(cached) => match cached.protocol {
|
||||||
|
crate::protocol_cache::DetectedProtocol::H3 => {
|
||||||
|
if let Some(h3_port) = cached.h3_port {
|
||||||
|
ProtocolDecision::H3 { port: h3_port }
|
||||||
|
} else {
|
||||||
|
// H3 cached but no port — fall back to ALPN probe
|
||||||
|
ProtocolDecision::AlpnProbe
|
||||||
}
|
}
|
||||||
None => (false, true), // needs ALPN probe
|
}
|
||||||
|
crate::protocol_cache::DetectedProtocol::H2 => ProtocolDecision::H2,
|
||||||
|
crate::protocol_cache::DetectedProtocol::H1 => ProtocolDecision::H1,
|
||||||
|
},
|
||||||
|
None => ProtocolDecision::AlpnProbe,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Derive legacy flags for the existing H1/H2 connection path
|
||||||
|
let (use_h2, mut needs_alpn_probe) = match &protocol_decision {
|
||||||
|
ProtocolDecision::H1 => (false, false),
|
||||||
|
ProtocolDecision::H2 => (true, false),
|
||||||
|
ProtocolDecision::H3 { .. } => (false, false), // H3 path handled separately below
|
||||||
|
ProtocolDecision::AlpnProbe => (false, true),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set Alt-Svc cache key on conn_activity so build_streaming_response can check
|
||||||
|
// the backend's original Alt-Svc header before ResponseFilter injects our own.
|
||||||
|
if is_auto_detect_mode {
|
||||||
|
conn_activity.alt_svc_cache_key = Some(protocol_cache_key.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- H3 path: try QUIC connection before TCP ---
|
||||||
|
if let ProtocolDecision::H3 { port: h3_port } = protocol_decision {
|
||||||
|
let h3_pool_key = crate::connection_pool::PoolKey {
|
||||||
|
host: upstream.host.clone(),
|
||||||
|
port: h3_port,
|
||||||
|
use_tls: true,
|
||||||
|
protocol: crate::connection_pool::PoolProtocol::H3,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Try H3 pool checkout first
|
||||||
|
if let Some((quic_conn, _age)) = self.connection_pool.checkout_h3(&h3_pool_key) {
|
||||||
|
self.metrics.backend_pool_hit(&upstream_key);
|
||||||
|
let result = self.forward_h3(
|
||||||
|
quic_conn, parts, body, upstream_headers, &upstream_path,
|
||||||
|
route_match.route, route_id, &ip_str, &h3_pool_key, domain_str, &conn_activity, &upstream_key,
|
||||||
|
).await;
|
||||||
|
self.upstream_selector.connection_ended(&upstream_key);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try fresh QUIC connection
|
||||||
|
match self.connect_quic_backend(&upstream.host, h3_port).await {
|
||||||
|
Ok(quic_conn) => {
|
||||||
|
self.metrics.backend_pool_miss(&upstream_key);
|
||||||
|
self.metrics.backend_connection_opened(&upstream_key, std::time::Instant::now().elapsed());
|
||||||
|
let result = self.forward_h3(
|
||||||
|
quic_conn, parts, body, upstream_headers, &upstream_path,
|
||||||
|
route_match.route, route_id, &ip_str, &h3_pool_key, domain_str, &conn_activity, &upstream_key,
|
||||||
|
).await;
|
||||||
|
self.upstream_selector.connection_ended(&upstream_key);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(backend = %upstream_key, error = %e,
|
||||||
|
"H3 backend connect failed, falling back to H2/H1");
|
||||||
|
// Suppress Alt-Svc caching for the fallback to prevent re-caching H3
|
||||||
|
// from our own injected Alt-Svc header or a stale backend Alt-Svc
|
||||||
|
conn_activity.alt_svc_cache_key = None;
|
||||||
|
// Force ALPN probe on TCP fallback so we correctly detect H2 vs H1
|
||||||
|
// (don't cache anything yet — let the ALPN probe decide)
|
||||||
|
if is_auto_detect_mode && upstream.use_tls {
|
||||||
|
needs_alpn_probe = true;
|
||||||
|
}
|
||||||
|
// Fall through to TCP path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// --- Connection pooling: try reusing an existing connection first ---
|
// --- Connection pooling: try reusing an existing connection first ---
|
||||||
// For ALPN probe mode, skip pool checkout (we don't know the protocol yet)
|
// For ALPN probe mode, skip pool checkout (we don't know the protocol yet)
|
||||||
if !needs_alpn_probe {
|
if !needs_alpn_probe {
|
||||||
@@ -870,6 +955,7 @@ impl HttpProxyService {
|
|||||||
};
|
};
|
||||||
self.upstream_selector.connection_ended(&upstream_key);
|
self.upstream_selector.connection_ended(&upstream_key);
|
||||||
self.metrics.backend_connection_closed(&upstream_key);
|
self.metrics.backend_connection_closed(&upstream_key);
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1668,6 +1754,19 @@ impl HttpProxyService {
|
|||||||
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
|
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
|
||||||
let (resp_parts, resp_body) = upstream_response.into_parts();
|
let (resp_parts, resp_body) = upstream_response.into_parts();
|
||||||
|
|
||||||
|
// Check for Alt-Svc in the backend's ORIGINAL response headers BEFORE
|
||||||
|
// ResponseFilter::apply_headers runs — the filter may inject our own Alt-Svc
|
||||||
|
// for client-facing HTTP/3 advertisement, which must not be confused with
|
||||||
|
// backend-originated Alt-Svc.
|
||||||
|
if let Some(ref cache_key) = conn_activity.alt_svc_cache_key {
|
||||||
|
if let Some(alt_svc) = resp_parts.headers.get("alt-svc").and_then(|v| v.to_str().ok()) {
|
||||||
|
if let Some(h3_port) = parse_alt_svc_h3_port(alt_svc) {
|
||||||
|
debug!(h3_port, "Backend advertises H3 via Alt-Svc");
|
||||||
|
self.protocol_cache.insert_h3(cache_key.clone(), h3_port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut response = Response::builder()
|
let mut response = Response::builder()
|
||||||
.status(resp_parts.status);
|
.status(resp_parts.status);
|
||||||
|
|
||||||
@@ -2393,6 +2492,252 @@ impl HttpProxyService {
|
|||||||
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
|
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
|
||||||
Arc::new(config)
|
Arc::new(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a shared QUIC client endpoint for outbound H3 backend connections.
|
||||||
|
fn create_quinn_client_endpoint() -> quinn::Endpoint {
|
||||||
|
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||||
|
let mut tls_config = rustls::ClientConfig::builder()
|
||||||
|
.dangerous()
|
||||||
|
.with_custom_certificate_verifier(Arc::new(InsecureBackendVerifier))
|
||||||
|
.with_no_client_auth();
|
||||||
|
tls_config.alpn_protocols = vec![b"h3".to_vec()];
|
||||||
|
|
||||||
|
let quic_crypto = quinn::crypto::rustls::QuicClientConfig::try_from(tls_config)
|
||||||
|
.expect("Failed to create QUIC client crypto config");
|
||||||
|
let client_config = quinn::ClientConfig::new(Arc::new(quic_crypto));
|
||||||
|
|
||||||
|
let mut endpoint = quinn::Endpoint::client("0.0.0.0:0".parse().unwrap())
|
||||||
|
.expect("Failed to create QUIC client endpoint");
|
||||||
|
endpoint.set_default_client_config(client_config);
|
||||||
|
endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to a backend via QUIC (H3).
|
||||||
|
async fn connect_quic_backend(
|
||||||
|
&self,
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
) -> Result<quinn::Connection, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let addr = tokio::net::lookup_host(format!("{}:{}", host, port))
|
||||||
|
.await?
|
||||||
|
.next()
|
||||||
|
.ok_or("DNS resolution returned no addresses")?;
|
||||||
|
|
||||||
|
let server_name = host.to_string();
|
||||||
|
let connecting = self.quinn_client_endpoint.connect(addr, &server_name)?;
|
||||||
|
|
||||||
|
let connection = tokio::time::timeout(QUIC_CONNECT_TIMEOUT, connecting).await
|
||||||
|
.map_err(|_| "QUIC connect timeout (3s)")??;
|
||||||
|
|
||||||
|
debug!("QUIC backend connection established to {}:{}", host, port);
|
||||||
|
Ok(connection)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Forward request to backend via HTTP/3 over QUIC.
|
||||||
|
async fn forward_h3(
|
||||||
|
&self,
|
||||||
|
quic_conn: quinn::Connection,
|
||||||
|
parts: hyper::http::request::Parts,
|
||||||
|
body: Incoming,
|
||||||
|
upstream_headers: hyper::HeaderMap,
|
||||||
|
upstream_path: &str,
|
||||||
|
route: &rustproxy_config::RouteConfig,
|
||||||
|
route_id: Option<&str>,
|
||||||
|
source_ip: &str,
|
||||||
|
pool_key: &crate::connection_pool::PoolKey,
|
||||||
|
domain: &str,
|
||||||
|
conn_activity: &ConnActivity,
|
||||||
|
backend_key: &str,
|
||||||
|
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
|
||||||
|
let h3_quinn_conn = h3_quinn::Connection::new(quic_conn.clone());
|
||||||
|
let (mut driver, mut send_request) = match h3::client::new(h3_quinn_conn).await {
|
||||||
|
Ok(pair) => pair,
|
||||||
|
Err(e) => {
|
||||||
|
error!(backend = %backend_key, domain = %domain, error = %e, "H3 client handshake failed");
|
||||||
|
self.metrics.backend_handshake_error(backend_key);
|
||||||
|
return Ok(error_response(StatusCode::BAD_GATEWAY, "H3 handshake failed"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Spawn the h3 connection driver
|
||||||
|
let driver_pool = Arc::clone(&self.connection_pool);
|
||||||
|
let driver_pool_key = pool_key.clone();
|
||||||
|
let gen_holder = Arc::new(std::sync::atomic::AtomicU64::new(u64::MAX));
|
||||||
|
let driver_gen = Arc::clone(&gen_holder);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let close_err = std::future::poll_fn(|cx| driver.poll_close(cx)).await;
|
||||||
|
debug!("H3 connection driver closed: {:?}", close_err);
|
||||||
|
let g = driver_gen.load(std::sync::atomic::Ordering::Relaxed);
|
||||||
|
if g != u64::MAX {
|
||||||
|
driver_pool.remove_h3_if_generation(&driver_pool_key, g);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Build the H3 request
|
||||||
|
let uri = hyper::Uri::builder()
|
||||||
|
.scheme("https")
|
||||||
|
.authority(domain)
|
||||||
|
.path_and_query(upstream_path)
|
||||||
|
.build()
|
||||||
|
.unwrap_or_else(|_| upstream_path.parse().unwrap_or_default());
|
||||||
|
|
||||||
|
let mut h3_req = hyper::Request::builder()
|
||||||
|
.method(parts.method.clone())
|
||||||
|
.uri(uri);
|
||||||
|
|
||||||
|
if let Some(headers) = h3_req.headers_mut() {
|
||||||
|
*headers = upstream_headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
let h3_req = h3_req.body(()).unwrap();
|
||||||
|
|
||||||
|
// Send the request
|
||||||
|
let mut stream = match send_request.send_request(h3_req).await {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
error!(backend = %backend_key, domain = %domain, error = %e, "H3 send_request failed");
|
||||||
|
self.metrics.backend_request_error(backend_key);
|
||||||
|
return Ok(error_response(StatusCode::BAD_GATEWAY, "H3 request failed"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stream request body
|
||||||
|
let rid: Option<Arc<str>> = route_id.map(Arc::from);
|
||||||
|
let sip: Arc<str> = Arc::from(source_ip);
|
||||||
|
|
||||||
|
{
|
||||||
|
use http_body_util::BodyExt;
|
||||||
|
let mut body = body;
|
||||||
|
while let Some(frame) = body.frame().await {
|
||||||
|
match frame {
|
||||||
|
Ok(frame) => {
|
||||||
|
if let Some(data) = frame.data_ref() {
|
||||||
|
self.metrics.record_bytes(data.len() as u64, 0, rid.as_deref(), Some(&sip));
|
||||||
|
if let Err(e) = stream.send_data(Bytes::copy_from_slice(data)).await {
|
||||||
|
error!(backend = %backend_key, error = %e, "H3 send_data failed");
|
||||||
|
return Ok(error_response(StatusCode::BAD_GATEWAY, "H3 body send failed"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(backend = %backend_key, error = %e, "Client body read error during H3 forward");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Signal end of body
|
||||||
|
stream.finish().await.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
let h3_response = match stream.recv_response().await {
|
||||||
|
Ok(resp) => resp,
|
||||||
|
Err(e) => {
|
||||||
|
error!(backend = %backend_key, domain = %domain, error = %e, "H3 recv_response failed");
|
||||||
|
self.metrics.backend_request_error(backend_key);
|
||||||
|
return Ok(error_response(StatusCode::BAD_GATEWAY, "H3 response failed"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build the response for the client
|
||||||
|
let status = h3_response.status();
|
||||||
|
let mut response = Response::builder().status(status);
|
||||||
|
|
||||||
|
if let Some(headers) = response.headers_mut() {
|
||||||
|
for (name, value) in h3_response.headers() {
|
||||||
|
let n = name.as_str();
|
||||||
|
// Skip hop-by-hop headers
|
||||||
|
if n == "transfer-encoding" || n == "connection" || n == "keep-alive" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
headers.insert(name.clone(), value.clone());
|
||||||
|
}
|
||||||
|
ResponseFilter::apply_headers(route, headers, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stream response body back via an adapter
|
||||||
|
let h3_body = H3ClientResponseBody { stream };
|
||||||
|
let counting_body = CountingBody::new(
|
||||||
|
h3_body,
|
||||||
|
Arc::clone(&self.metrics),
|
||||||
|
rid,
|
||||||
|
Some(sip),
|
||||||
|
Direction::Out,
|
||||||
|
).with_connection_activity(Arc::clone(&conn_activity.last_activity), conn_activity.start);
|
||||||
|
|
||||||
|
let counting_body = if let Some(ref ar) = conn_activity.active_requests {
|
||||||
|
counting_body.with_active_requests(Arc::clone(ar))
|
||||||
|
} else {
|
||||||
|
counting_body
|
||||||
|
};
|
||||||
|
|
||||||
|
let body: BoxBody<Bytes, hyper::Error> = BoxBody::new(counting_body);
|
||||||
|
|
||||||
|
// Register connection in pool on success
|
||||||
|
if status != StatusCode::BAD_GATEWAY {
|
||||||
|
let g = self.connection_pool.register_h3(pool_key.clone(), quic_conn);
|
||||||
|
gen_holder.store(g, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.metrics.set_backend_protocol(backend_key, "h3");
|
||||||
|
Ok(response.body(body).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse an Alt-Svc header value to extract the H3 port.
|
||||||
|
/// Handles formats like `h3=":443"; ma=86400` and `h3=":8443", h2=":443"`.
|
||||||
|
fn parse_alt_svc_h3_port(header_value: &str) -> Option<u16> {
|
||||||
|
for directive in header_value.split(',') {
|
||||||
|
let directive = directive.trim();
|
||||||
|
// Match h3=":<port>" or h3-29=":<port>" etc.
|
||||||
|
if directive.starts_with("h3=") || directive.starts_with("h3-") {
|
||||||
|
// Find the port in ":<port>"
|
||||||
|
if let Some(start) = directive.find("\":") {
|
||||||
|
let rest = &directive[start + 2..];
|
||||||
|
if let Some(end) = rest.find('"') {
|
||||||
|
if let Ok(port) = rest[..end].parse::<u16>() {
|
||||||
|
return Some(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Response body adapter for H3 client responses.
|
||||||
|
/// Reads data from the h3 `RequestStream` recv side and presents it as an `http_body::Body`.
|
||||||
|
struct H3ClientResponseBody {
|
||||||
|
stream: h3::client::RequestStream<h3_quinn::BidiStream<Bytes>, Bytes>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl http_body::Body for H3ClientResponseBody {
|
||||||
|
type Data = Bytes;
|
||||||
|
type Error = hyper::Error;
|
||||||
|
|
||||||
|
fn poll_frame(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
_cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
|
||||||
|
// h3's recv_data is async, so we need to poll it manually.
|
||||||
|
// Use a small future to poll the recv_data call.
|
||||||
|
use std::future::Future;
|
||||||
|
let mut fut = Box::pin(self.stream.recv_data());
|
||||||
|
match fut.as_mut().poll(_cx) {
|
||||||
|
Poll::Ready(Ok(Some(mut buf))) => {
|
||||||
|
use bytes::Buf;
|
||||||
|
let data = Bytes::copy_from_slice(buf.chunk());
|
||||||
|
buf.advance(buf.remaining());
|
||||||
|
Poll::Ready(Some(Ok(http_body::Frame::data(data))))
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(None)) => Poll::Ready(None),
|
||||||
|
Poll::Ready(Err(e)) => {
|
||||||
|
warn!("H3 response body recv error: {}", e);
|
||||||
|
Poll::Ready(None)
|
||||||
|
}
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insecure certificate verifier for backend TLS connections (fallback only).
|
/// Insecure certificate verifier for backend TLS connections (fallback only).
|
||||||
@@ -2463,6 +2808,7 @@ impl Default for HttpProxyService {
|
|||||||
http_idle_timeout: DEFAULT_HTTP_IDLE_TIMEOUT,
|
http_idle_timeout: DEFAULT_HTTP_IDLE_TIMEOUT,
|
||||||
ws_inactivity_timeout: DEFAULT_WS_INACTIVITY_TIMEOUT,
|
ws_inactivity_timeout: DEFAULT_WS_INACTIVITY_TIMEOUT,
|
||||||
ws_max_lifetime: DEFAULT_WS_MAX_LIFETIME,
|
ws_max_lifetime: DEFAULT_WS_MAX_LIFETIME,
|
||||||
|
quinn_client_endpoint: Arc::new(Self::create_quinn_client_endpoint()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ use rustproxy_config::{RouteConfig, TransportProtocol};
|
|||||||
use rustproxy_metrics::MetricsCollector;
|
use rustproxy_metrics::MetricsCollector;
|
||||||
use rustproxy_routing::{MatchContext, RouteManager};
|
use rustproxy_routing::{MatchContext, RouteManager};
|
||||||
|
|
||||||
|
use rustproxy_http::h3_service::H3ProxyService;
|
||||||
|
|
||||||
use crate::connection_tracker::ConnectionTracker;
|
use crate::connection_tracker::ConnectionTracker;
|
||||||
|
|
||||||
/// Create a QUIC server endpoint on the given port with the provided TLS config.
|
/// Create a QUIC server endpoint on the given port with the provided TLS config.
|
||||||
@@ -55,6 +57,7 @@ pub async fn quic_accept_loop(
|
|||||||
metrics: Arc<MetricsCollector>,
|
metrics: Arc<MetricsCollector>,
|
||||||
conn_tracker: Arc<ConnectionTracker>,
|
conn_tracker: Arc<ConnectionTracker>,
|
||||||
cancel: CancellationToken,
|
cancel: CancellationToken,
|
||||||
|
h3_service: Option<Arc<H3ProxyService>>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
let incoming = tokio::select! {
|
let incoming = tokio::select! {
|
||||||
@@ -113,9 +116,10 @@ pub async fn quic_accept_loop(
|
|||||||
let metrics = Arc::clone(&metrics);
|
let metrics = Arc::clone(&metrics);
|
||||||
let conn_tracker = Arc::clone(&conn_tracker);
|
let conn_tracker = Arc::clone(&conn_tracker);
|
||||||
let cancel = cancel.child_token();
|
let cancel = cancel.child_token();
|
||||||
|
let h3_svc = h3_service.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
match handle_quic_connection(incoming, route, port, Arc::clone(&metrics), &cancel).await {
|
match handle_quic_connection(incoming, route, port, Arc::clone(&metrics), &cancel, h3_svc).await {
|
||||||
Ok(()) => debug!("QUIC connection from {} completed", remote_addr),
|
Ok(()) => debug!("QUIC connection from {} completed", remote_addr),
|
||||||
Err(e) => debug!("QUIC connection from {} error: {}", remote_addr, e),
|
Err(e) => debug!("QUIC connection from {} error: {}", remote_addr, e),
|
||||||
}
|
}
|
||||||
@@ -139,6 +143,7 @@ async fn handle_quic_connection(
|
|||||||
port: u16,
|
port: u16,
|
||||||
metrics: Arc<MetricsCollector>,
|
metrics: Arc<MetricsCollector>,
|
||||||
cancel: &CancellationToken,
|
cancel: &CancellationToken,
|
||||||
|
h3_service: Option<Arc<H3ProxyService>>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let connection = incoming.await?;
|
let connection = incoming.await?;
|
||||||
let remote_addr = connection.remote_address();
|
let remote_addr = connection.remote_address();
|
||||||
@@ -151,10 +156,20 @@ async fn handle_quic_connection(
|
|||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
if enable_http3 {
|
if enable_http3 {
|
||||||
// Phase 5: dispatch to H3ProxyService
|
if let Some(ref h3_svc) = h3_service {
|
||||||
// For now, log and accept streams for basic handling
|
debug!("HTTP/3 enabled for route {:?}, dispatching to H3ProxyService", route.name);
|
||||||
debug!("HTTP/3 enabled for route {:?}, dispatching to H3 handler", route.name);
|
h3_svc.handle_connection(connection, &route, port).await
|
||||||
handle_h3_connection(connection, route, port, &metrics, cancel).await
|
} else {
|
||||||
|
warn!("HTTP/3 enabled for route {:?} but H3ProxyService not initialized", route.name);
|
||||||
|
// Keep connection alive until cancelled
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel.cancelled() => {}
|
||||||
|
reason = connection.closed() => {
|
||||||
|
debug!("HTTP/3 connection closed (no service): {}", reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Non-HTTP3 QUIC: bidirectional stream forwarding to TCP backend
|
// Non-HTTP3 QUIC: bidirectional stream forwarding to TCP backend
|
||||||
handle_quic_stream_forwarding(connection, route, port, metrics, cancel).await
|
handle_quic_stream_forwarding(connection, route, port, metrics, cancel).await
|
||||||
@@ -257,29 +272,6 @@ async fn forward_quic_stream_to_tcp(
|
|||||||
Ok((bytes_in, bytes_out))
|
Ok((bytes_in, bytes_out))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Placeholder for HTTP/3 connection handling (Phase 5).
|
|
||||||
///
|
|
||||||
/// Once h3_service is implemented, this will delegate to it.
|
|
||||||
async fn handle_h3_connection(
|
|
||||||
connection: quinn::Connection,
|
|
||||||
_route: RouteConfig,
|
|
||||||
_port: u16,
|
|
||||||
_metrics: &MetricsCollector,
|
|
||||||
cancel: &CancellationToken,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
warn!("HTTP/3 handling not yet fully implemented — accepting connection but no request processing");
|
|
||||||
|
|
||||||
// Keep the connection alive until cancelled or closed
|
|
||||||
tokio::select! {
|
|
||||||
_ = cancel.cancelled() => {}
|
|
||||||
reason = connection.closed() => {
|
|
||||||
debug!("HTTP/3 connection closed: {}", reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -21,13 +21,15 @@ use rustproxy_config::{RouteActionType, TransportProtocol};
|
|||||||
use rustproxy_metrics::MetricsCollector;
|
use rustproxy_metrics::MetricsCollector;
|
||||||
use rustproxy_routing::{MatchContext, RouteManager};
|
use rustproxy_routing::{MatchContext, RouteManager};
|
||||||
|
|
||||||
|
use rustproxy_http::h3_service::H3ProxyService;
|
||||||
|
|
||||||
use crate::connection_tracker::ConnectionTracker;
|
use crate::connection_tracker::ConnectionTracker;
|
||||||
use crate::udp_session::{SessionKey, UdpSession, UdpSessionConfig, UdpSessionTable};
|
use crate::udp_session::{SessionKey, UdpSession, UdpSessionConfig, UdpSessionTable};
|
||||||
|
|
||||||
/// Manages UDP listeners across all configured ports.
|
/// Manages UDP listeners across all configured ports.
|
||||||
pub struct UdpListenerManager {
|
pub struct UdpListenerManager {
|
||||||
/// Port → recv loop task handle
|
/// Port → (recv loop task handle, optional QUIC endpoint for TLS updates)
|
||||||
listeners: HashMap<u16, JoinHandle<()>>,
|
listeners: HashMap<u16, (JoinHandle<()>, Option<quinn::Endpoint>)>,
|
||||||
/// Hot-reloadable route table
|
/// Hot-reloadable route table
|
||||||
route_manager: Arc<ArcSwap<RouteManager>>,
|
route_manager: Arc<ArcSwap<RouteManager>>,
|
||||||
/// Shared metrics collector
|
/// Shared metrics collector
|
||||||
@@ -44,13 +46,18 @@ pub struct UdpListenerManager {
|
|||||||
relay_writer: Arc<Mutex<Option<tokio::net::unix::OwnedWriteHalf>>>,
|
relay_writer: Arc<Mutex<Option<tokio::net::unix::OwnedWriteHalf>>>,
|
||||||
/// Cancel token for the current relay reply reader task
|
/// Cancel token for the current relay reply reader task
|
||||||
relay_reader_cancel: Option<CancellationToken>,
|
relay_reader_cancel: Option<CancellationToken>,
|
||||||
|
/// H3 proxy service for HTTP/3 request handling
|
||||||
|
h3_service: Option<Arc<H3ProxyService>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for UdpListenerManager {
|
impl Drop for UdpListenerManager {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.cancel_token.cancel();
|
self.cancel_token.cancel();
|
||||||
for (_, handle) in self.listeners.drain() {
|
for (_, (handle, endpoint)) in self.listeners.drain() {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
|
if let Some(ep) = endpoint {
|
||||||
|
ep.close(quinn::VarInt::from_u32(0), b"shutdown");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,9 +79,15 @@ impl UdpListenerManager {
|
|||||||
datagram_handler_relay: Arc::new(RwLock::new(None)),
|
datagram_handler_relay: Arc::new(RwLock::new(None)),
|
||||||
relay_writer: Arc::new(Mutex::new(None)),
|
relay_writer: Arc::new(Mutex::new(None)),
|
||||||
relay_reader_cancel: None,
|
relay_reader_cancel: None,
|
||||||
|
h3_service: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the H3 proxy service for HTTP/3 request handling.
|
||||||
|
pub fn set_h3_service(&mut self, svc: Arc<H3ProxyService>) {
|
||||||
|
self.h3_service = Some(svc);
|
||||||
|
}
|
||||||
|
|
||||||
/// Update the route manager (for hot-reload).
|
/// Update the route manager (for hot-reload).
|
||||||
pub fn update_routes(&self, route_manager: Arc<RouteManager>) {
|
pub fn update_routes(&self, route_manager: Arc<RouteManager>) {
|
||||||
self.route_manager.store(route_manager);
|
self.route_manager.store(route_manager);
|
||||||
@@ -109,8 +122,9 @@ impl UdpListenerManager {
|
|||||||
|
|
||||||
if has_quic {
|
if has_quic {
|
||||||
if let Some(tls) = tls_config {
|
if let Some(tls) = tls_config {
|
||||||
// Create QUIC endpoint
|
// Create QUIC endpoint; clone it so we can hot-swap TLS later
|
||||||
let endpoint = crate::quic_handler::create_quic_endpoint(port, tls)?;
|
let endpoint = crate::quic_handler::create_quic_endpoint(port, tls)?;
|
||||||
|
let endpoint_for_updates = endpoint.clone(); // quinn::Endpoint is Arc-based
|
||||||
let handle = tokio::spawn(crate::quic_handler::quic_accept_loop(
|
let handle = tokio::spawn(crate::quic_handler::quic_accept_loop(
|
||||||
endpoint,
|
endpoint,
|
||||||
port,
|
port,
|
||||||
@@ -118,8 +132,9 @@ impl UdpListenerManager {
|
|||||||
Arc::clone(&self.metrics),
|
Arc::clone(&self.metrics),
|
||||||
Arc::clone(&self.conn_tracker),
|
Arc::clone(&self.conn_tracker),
|
||||||
self.cancel_token.child_token(),
|
self.cancel_token.child_token(),
|
||||||
|
self.h3_service.clone(),
|
||||||
));
|
));
|
||||||
self.listeners.insert(port, handle);
|
self.listeners.insert(port, (handle, Some(endpoint_for_updates)));
|
||||||
info!("QUIC endpoint started on port {}", port);
|
info!("QUIC endpoint started on port {}", port);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
@@ -145,7 +160,7 @@ impl UdpListenerManager {
|
|||||||
self.cancel_token.child_token(),
|
self.cancel_token.child_token(),
|
||||||
));
|
));
|
||||||
|
|
||||||
self.listeners.insert(port, handle);
|
self.listeners.insert(port, (handle, None));
|
||||||
|
|
||||||
// Start the session cleanup task if this is the first port
|
// Start the session cleanup task if this is the first port
|
||||||
if self.listeners.len() == 1 {
|
if self.listeners.len() == 1 {
|
||||||
@@ -157,8 +172,11 @@ impl UdpListenerManager {
|
|||||||
|
|
||||||
/// Stop listening on a UDP port.
|
/// Stop listening on a UDP port.
|
||||||
pub fn remove_port(&mut self, port: u16) {
|
pub fn remove_port(&mut self, port: u16) {
|
||||||
if let Some(handle) = self.listeners.remove(&port) {
|
if let Some((handle, endpoint)) = self.listeners.remove(&port) {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
|
if let Some(ep) = endpoint {
|
||||||
|
ep.close(quinn::VarInt::from_u32(0), b"port removed");
|
||||||
|
}
|
||||||
info!("UDP listener removed from port {}", port);
|
info!("UDP listener removed from port {}", port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,14 +191,148 @@ impl UdpListenerManager {
|
|||||||
/// Stop all listeners and clean up.
|
/// Stop all listeners and clean up.
|
||||||
pub async fn stop(&mut self) {
|
pub async fn stop(&mut self) {
|
||||||
self.cancel_token.cancel();
|
self.cancel_token.cancel();
|
||||||
for (port, handle) in self.listeners.drain() {
|
for (port, (handle, endpoint)) in self.listeners.drain() {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
|
if let Some(ep) = endpoint {
|
||||||
|
ep.close(quinn::VarInt::from_u32(0), b"shutdown");
|
||||||
|
}
|
||||||
debug!("UDP listener stopped on port {}", port);
|
debug!("UDP listener stopped on port {}", port);
|
||||||
}
|
}
|
||||||
info!("All UDP listeners stopped, {} sessions remaining",
|
info!("All UDP listeners stopped, {} sessions remaining",
|
||||||
self.session_table.session_count());
|
self.session_table.session_count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update TLS config on all active QUIC endpoints (cert refresh).
|
||||||
|
/// Only affects new incoming connections — existing connections are undisturbed.
|
||||||
|
/// Uses quinn's Endpoint::set_server_config() for zero-downtime hot-swap.
|
||||||
|
pub fn update_quic_tls(&self, tls_config: Arc<rustls::ServerConfig>) {
|
||||||
|
for (port, (_handle, endpoint)) in &self.listeners {
|
||||||
|
if let Some(ep) = endpoint {
|
||||||
|
match quinn::crypto::rustls::QuicServerConfig::try_from(Arc::clone(&tls_config)) {
|
||||||
|
Ok(quic_crypto) => {
|
||||||
|
let server_config = quinn::ServerConfig::with_crypto(Arc::new(quic_crypto));
|
||||||
|
ep.set_server_config(Some(server_config));
|
||||||
|
info!("Updated QUIC TLS config on port {}", port);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("Failed to update QUIC TLS config on port {}: {}", port, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Upgrade raw UDP fallback listeners to QUIC endpoints.
|
||||||
|
///
|
||||||
|
/// At startup, if no TLS certs are available, QUIC routes fall back to raw UDP.
|
||||||
|
/// When certs become available later (via loadCertificate IPC or ACME), this method
|
||||||
|
/// stops the raw UDP listener, drains sessions, and creates a proper QUIC endpoint.
|
||||||
|
///
|
||||||
|
/// This is idempotent — ports that already have QUIC endpoints are skipped.
|
||||||
|
pub async fn upgrade_raw_to_quic(&mut self, tls_config: Arc<rustls::ServerConfig>) {
|
||||||
|
// Find ports that are raw UDP fallback (endpoint=None) but have QUIC routes
|
||||||
|
let rm = self.route_manager.load();
|
||||||
|
let upgrade_ports: Vec<u16> = self.listeners.iter()
|
||||||
|
.filter(|(_, (_, endpoint))| endpoint.is_none())
|
||||||
|
.filter(|(port, _)| {
|
||||||
|
rm.routes_for_port(**port).iter().any(|r| {
|
||||||
|
r.action.udp.as_ref()
|
||||||
|
.and_then(|u| u.quic.as_ref())
|
||||||
|
.is_some()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.map(|(port, _)| *port)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for port in upgrade_ports {
|
||||||
|
info!("Upgrading raw UDP listener on port {} to QUIC endpoint", port);
|
||||||
|
|
||||||
|
// Stop the raw UDP listener task and drain sessions to release the socket
|
||||||
|
if let Some((handle, _)) = self.listeners.remove(&port) {
|
||||||
|
handle.abort();
|
||||||
|
}
|
||||||
|
let drained = self.session_table.drain_port(
|
||||||
|
port, &self.metrics, &self.conn_tracker,
|
||||||
|
);
|
||||||
|
if drained > 0 {
|
||||||
|
debug!("Drained {} UDP sessions on port {} for QUIC upgrade", drained, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Brief yield to let aborted tasks drop their socket references
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
|
||||||
|
// Create QUIC endpoint on the now-free port
|
||||||
|
match crate::quic_handler::create_quic_endpoint(port, Arc::clone(&tls_config)) {
|
||||||
|
Ok(endpoint) => {
|
||||||
|
let endpoint_for_updates = endpoint.clone();
|
||||||
|
let handle = tokio::spawn(crate::quic_handler::quic_accept_loop(
|
||||||
|
endpoint,
|
||||||
|
port,
|
||||||
|
Arc::clone(&self.route_manager),
|
||||||
|
Arc::clone(&self.metrics),
|
||||||
|
Arc::clone(&self.conn_tracker),
|
||||||
|
self.cancel_token.child_token(),
|
||||||
|
self.h3_service.clone(),
|
||||||
|
));
|
||||||
|
self.listeners.insert(port, (handle, Some(endpoint_for_updates)));
|
||||||
|
info!("QUIC endpoint started on port {} (upgraded from raw UDP)", port);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// Port may still be held — retry once after a brief delay
|
||||||
|
warn!("QUIC endpoint creation failed on port {}, retrying: {}", port, e);
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||||
|
|
||||||
|
match crate::quic_handler::create_quic_endpoint(port, Arc::clone(&tls_config)) {
|
||||||
|
Ok(endpoint) => {
|
||||||
|
let endpoint_for_updates = endpoint.clone();
|
||||||
|
let handle = tokio::spawn(crate::quic_handler::quic_accept_loop(
|
||||||
|
endpoint,
|
||||||
|
port,
|
||||||
|
Arc::clone(&self.route_manager),
|
||||||
|
Arc::clone(&self.metrics),
|
||||||
|
Arc::clone(&self.conn_tracker),
|
||||||
|
self.cancel_token.child_token(),
|
||||||
|
self.h3_service.clone(),
|
||||||
|
));
|
||||||
|
self.listeners.insert(port, (handle, Some(endpoint_for_updates)));
|
||||||
|
info!("QUIC endpoint started on port {} (upgraded from raw UDP, retry)", port);
|
||||||
|
}
|
||||||
|
Err(e2) => {
|
||||||
|
error!("Failed to upgrade port {} to QUIC after retry: {}. \
|
||||||
|
Rebinding as raw UDP.", port, e2);
|
||||||
|
// Fallback: rebind as raw UDP so the port isn't dead
|
||||||
|
if let Ok(()) = self.rebind_raw_udp(port).await {
|
||||||
|
warn!("Port {} rebound as raw UDP (QUIC upgrade failed)", port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rebind a port as a raw UDP listener (fallback when QUIC upgrade fails).
|
||||||
|
async fn rebind_raw_udp(&mut self, port: u16) -> anyhow::Result<()> {
|
||||||
|
let addr: std::net::SocketAddr = ([0, 0, 0, 0], port).into();
|
||||||
|
let socket = UdpSocket::bind(addr).await?;
|
||||||
|
let socket = Arc::new(socket);
|
||||||
|
|
||||||
|
let handle = tokio::spawn(Self::recv_loop(
|
||||||
|
socket,
|
||||||
|
port,
|
||||||
|
Arc::clone(&self.route_manager),
|
||||||
|
Arc::clone(&self.metrics),
|
||||||
|
Arc::clone(&self.conn_tracker),
|
||||||
|
Arc::clone(&self.session_table),
|
||||||
|
Arc::clone(&self.datagram_handler_relay),
|
||||||
|
Arc::clone(&self.relay_writer),
|
||||||
|
self.cancel_token.child_token(),
|
||||||
|
));
|
||||||
|
|
||||||
|
self.listeners.insert(port, (handle, None));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the datagram handler relay socket path and establish connection.
|
/// Set the datagram handler relay socket path and establish connection.
|
||||||
pub async fn set_datagram_handler_relay(&mut self, path: String) {
|
pub async fn set_datagram_handler_relay(&mut self, path: String) {
|
||||||
// Cancel previous relay reader task if any
|
// Cancel previous relay reader task if any
|
||||||
|
|||||||
@@ -201,6 +201,36 @@ impl UdpSessionTable {
|
|||||||
removed
|
removed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Drain all sessions on a given listening port, releasing socket references.
|
||||||
|
/// Used when upgrading a raw UDP listener to QUIC — the raw UDP socket's
|
||||||
|
/// Arc refcount must drop to zero so the port can be rebound.
|
||||||
|
pub fn drain_port(
|
||||||
|
&self,
|
||||||
|
port: u16,
|
||||||
|
metrics: &MetricsCollector,
|
||||||
|
conn_tracker: &ConnectionTracker,
|
||||||
|
) -> usize {
|
||||||
|
let keys: Vec<SessionKey> = self.sessions.iter()
|
||||||
|
.filter(|entry| entry.key().1 == port)
|
||||||
|
.map(|entry| *entry.key())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut removed = 0;
|
||||||
|
for key in keys {
|
||||||
|
if let Some(session) = self.remove(&key) {
|
||||||
|
session.cancel.cancel();
|
||||||
|
conn_tracker.connection_closed(&session.source_ip);
|
||||||
|
metrics.connection_closed(
|
||||||
|
session.route_id.as_deref(),
|
||||||
|
Some(&session.source_ip.to_string()),
|
||||||
|
);
|
||||||
|
metrics.udp_session_closed();
|
||||||
|
removed += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
removed
|
||||||
|
}
|
||||||
|
|
||||||
/// Total number of active sessions.
|
/// Total number of active sessions.
|
||||||
pub fn session_count(&self) -> usize {
|
pub fn session_count(&self) -> usize {
|
||||||
self.sessions.len()
|
self.sessions.len()
|
||||||
|
|||||||
@@ -340,6 +340,17 @@ impl RustProxy {
|
|||||||
self.cancel_token.clone(),
|
self.cancel_token.clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Construct H3ProxyService for HTTP/3 request handling
|
||||||
|
let h3_svc = rustproxy_http::h3_service::H3ProxyService::new(
|
||||||
|
Arc::new(ArcSwap::from(Arc::clone(&*self.route_table.load()))),
|
||||||
|
Arc::clone(&self.metrics),
|
||||||
|
Arc::new(rustproxy_http::connection_pool::ConnectionPool::new()),
|
||||||
|
Arc::new(rustproxy_http::protocol_cache::ProtocolCache::new()),
|
||||||
|
rustproxy_passthrough::tls_handler::shared_backend_tls_config(),
|
||||||
|
std::time::Duration::from_secs(30),
|
||||||
|
);
|
||||||
|
udp_mgr.set_h3_service(Arc::new(h3_svc));
|
||||||
|
|
||||||
for port in &udp_ports {
|
for port in &udp_ports {
|
||||||
udp_mgr.add_port_with_tls(*port, quic_tls_config.clone()).await?;
|
udp_mgr.add_port_with_tls(*port, quic_tls_config.clone()).await?;
|
||||||
}
|
}
|
||||||
@@ -772,13 +783,19 @@ impl RustProxy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build TLS config for QUIC (needed for new ports and upgrading existing raw UDP)
|
||||||
|
let quic_tls = {
|
||||||
|
let tls_configs = self.current_tls_configs().await;
|
||||||
|
Self::build_quic_tls_config(&tls_configs)
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(ref mut udp_mgr) = self.udp_listener_manager {
|
if let Some(ref mut udp_mgr) = self.udp_listener_manager {
|
||||||
udp_mgr.update_routes(Arc::clone(&new_manager));
|
udp_mgr.update_routes(Arc::clone(&new_manager));
|
||||||
|
|
||||||
// Add new UDP ports
|
// Add new UDP ports (with TLS for QUIC)
|
||||||
for port in &new_udp_ports {
|
for port in &new_udp_ports {
|
||||||
if !old_udp_ports.contains(port) {
|
if !old_udp_ports.contains(port) {
|
||||||
udp_mgr.add_port(*port).await?;
|
udp_mgr.add_port_with_tls(*port, quic_tls.clone()).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Remove old UDP ports
|
// Remove old UDP ports
|
||||||
@@ -787,6 +804,12 @@ impl RustProxy {
|
|||||||
udp_mgr.remove_port(*port);
|
udp_mgr.remove_port(*port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upgrade existing raw UDP fallback listeners to QUIC if TLS is now available
|
||||||
|
if let Some(ref quic_config) = quic_tls {
|
||||||
|
udp_mgr.update_quic_tls(Arc::clone(quic_config));
|
||||||
|
udp_mgr.upgrade_raw_to_quic(Arc::clone(quic_config)).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if self.udp_listener_manager.is_some() {
|
} else if self.udp_listener_manager.is_some() {
|
||||||
// All UDP routes removed — shut down UDP manager
|
// All UDP routes removed — shut down UDP manager
|
||||||
@@ -843,12 +866,12 @@ impl RustProxy {
|
|||||||
.map_err(|e| anyhow::anyhow!("ACME provisioning failed: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("ACME provisioning failed: {}", e))?;
|
||||||
|
|
||||||
// Hot-swap into TLS configs
|
// Hot-swap into TLS configs
|
||||||
if let Some(ref mut listener) = self.listener_manager {
|
|
||||||
let mut tls_configs = Self::extract_tls_configs(&self.options.routes);
|
let mut tls_configs = Self::extract_tls_configs(&self.options.routes);
|
||||||
tls_configs.insert(domain.clone(), TlsCertConfig {
|
tls_configs.insert(domain.clone(), TlsCertConfig {
|
||||||
cert_pem: bundle.cert_pem.clone(),
|
cert_pem: bundle.cert_pem.clone(),
|
||||||
key_pem: bundle.key_pem.clone(),
|
key_pem: bundle.key_pem.clone(),
|
||||||
});
|
});
|
||||||
|
{
|
||||||
let cm = cm_arc.lock().await;
|
let cm = cm_arc.lock().await;
|
||||||
for (d, b) in cm.store().iter() {
|
for (d, b) in cm.store().iter() {
|
||||||
if !tls_configs.contains_key(d) {
|
if !tls_configs.contains_key(d) {
|
||||||
@@ -858,9 +881,22 @@ impl RustProxy {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let quic_tls = Self::build_quic_tls_config(&tls_configs);
|
||||||
|
|
||||||
|
if let Some(ref listener) = self.listener_manager {
|
||||||
listener.set_tls_configs(tls_configs);
|
listener.set_tls_configs(tls_configs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update existing QUIC endpoints and upgrade raw UDP fallback listeners
|
||||||
|
if let Some(ref mut udp_mgr) = self.udp_listener_manager {
|
||||||
|
if let Some(ref quic_config) = quic_tls {
|
||||||
|
udp_mgr.update_quic_tls(Arc::clone(quic_config));
|
||||||
|
udp_mgr.upgrade_raw_to_quic(Arc::clone(quic_config)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
info!("Certificate provisioned and loaded for route '{}'", route_name);
|
info!("Certificate provisioned and loaded for route '{}'", route_name);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -1005,6 +1041,33 @@ impl RustProxy {
|
|||||||
Some(Arc::new(tls_config))
|
Some(Arc::new(tls_config))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build the current full TLS config map from all sources (route configs, loaded certs, cert manager).
|
||||||
|
async fn current_tls_configs(&self) -> HashMap<String, TlsCertConfig> {
|
||||||
|
let mut configs = Self::extract_tls_configs(&self.options.routes);
|
||||||
|
|
||||||
|
// Merge dynamically loaded certs (from loadCertificate IPC)
|
||||||
|
for (d, c) in &self.loaded_certs {
|
||||||
|
if !configs.contains_key(d) {
|
||||||
|
configs.insert(d.clone(), c.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge certs from cert manager store
|
||||||
|
if let Some(ref cm_arc) = self.cert_manager {
|
||||||
|
let cm = cm_arc.lock().await;
|
||||||
|
for (d, b) in cm.store().iter() {
|
||||||
|
if !configs.contains_key(d) {
|
||||||
|
configs.insert(d.clone(), TlsCertConfig {
|
||||||
|
cert_pem: b.cert_pem.clone(),
|
||||||
|
key_pem: b.key_pem.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
configs
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the Unix domain socket path for relaying UDP datagrams to TypeScript datagramHandler callbacks.
|
/// Set the Unix domain socket path for relaying UDP datagrams to TypeScript datagramHandler callbacks.
|
||||||
pub async fn set_datagram_handler_relay_path(&mut self, path: Option<String>) {
|
pub async fn set_datagram_handler_relay_path(&mut self, path: Option<String>) {
|
||||||
info!("Datagram handler relay path set to: {:?}", path);
|
info!("Datagram handler relay path set to: {:?}", path);
|
||||||
@@ -1055,39 +1118,24 @@ impl RustProxy {
|
|||||||
key_pem: key_pem.clone(),
|
key_pem: key_pem.clone(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Hot-swap TLS config on the listener
|
// Hot-swap TLS config on TCP and QUIC listeners
|
||||||
if let Some(ref mut listener) = self.listener_manager {
|
let tls_configs = self.current_tls_configs().await;
|
||||||
let mut tls_configs = Self::extract_tls_configs(&self.options.routes);
|
|
||||||
|
|
||||||
// Add the new cert
|
// Build QUIC TLS config before TCP consumes the map
|
||||||
tls_configs.insert(domain.to_string(), TlsCertConfig {
|
let quic_tls = Self::build_quic_tls_config(&tls_configs);
|
||||||
cert_pem: cert_pem.clone(),
|
|
||||||
key_pem: key_pem.clone(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Also include all existing certs from cert manager
|
|
||||||
if let Some(ref cm_arc) = self.cert_manager {
|
|
||||||
let cm = cm_arc.lock().await;
|
|
||||||
for (d, b) in cm.store().iter() {
|
|
||||||
if !tls_configs.contains_key(d) {
|
|
||||||
tls_configs.insert(d.clone(), TlsCertConfig {
|
|
||||||
cert_pem: b.cert_pem.clone(),
|
|
||||||
key_pem: b.key_pem.clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge dynamically loaded certs from previous loadCertificate calls
|
|
||||||
for (d, c) in &self.loaded_certs {
|
|
||||||
if !tls_configs.contains_key(d) {
|
|
||||||
tls_configs.insert(d.clone(), c.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if let Some(ref listener) = self.listener_manager {
|
||||||
listener.set_tls_configs(tls_configs);
|
listener.set_tls_configs(tls_configs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update existing QUIC endpoints and upgrade raw UDP fallback listeners
|
||||||
|
if let Some(ref mut udp_mgr) = self.udp_listener_manager {
|
||||||
|
if let Some(ref quic_config) = quic_tls {
|
||||||
|
udp_mgr.update_quic_tls(Arc::clone(quic_config));
|
||||||
|
udp_mgr.upgrade_raw_to_quic(Arc::clone(quic_config)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
info!("Certificate loaded and TLS config updated for {}", domain);
|
info!("Certificate loaded and TLS config updated for {}", domain);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartproxy',
|
name: '@push.rocks/smartproxy',
|
||||||
version: '25.15.0',
|
version: '25.16.3',
|
||||||
description: 'A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.'
|
description: 'A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type { IRouteConfig, IRouteMatch, IRouteAction, TPortRange } from '../mod
|
|||||||
export class RouteValidator {
|
export class RouteValidator {
|
||||||
private static readonly VALID_TLS_MODES = ['terminate', 'passthrough', 'terminate-and-reencrypt'];
|
private static readonly VALID_TLS_MODES = ['terminate', 'passthrough', 'terminate-and-reencrypt'];
|
||||||
private static readonly VALID_ACTION_TYPES = ['forward', 'socket-handler'];
|
private static readonly VALID_ACTION_TYPES = ['forward', 'socket-handler'];
|
||||||
private static readonly VALID_PROTOCOLS = ['tcp', 'http', 'https', 'ws', 'wss'];
|
private static readonly VALID_PROTOCOLS = ['tcp', 'http', 'https', 'ws', 'wss', 'udp', 'quic', 'http3'];
|
||||||
private static readonly MAX_PORTS = 100;
|
private static readonly MAX_PORTS = 100;
|
||||||
private static readonly MAX_DOMAINS = 1000;
|
private static readonly MAX_DOMAINS = 1000;
|
||||||
private static readonly MAX_HEADER_SIZE = 8192;
|
private static readonly MAX_HEADER_SIZE = 8192;
|
||||||
@@ -173,6 +173,22 @@ export class RouteValidator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QUIC routes require TLS with termination (QUIC mandates TLS 1.3)
|
||||||
|
if (route.action.udp?.quic && route.action.type === 'forward') {
|
||||||
|
if (!route.action.tls) {
|
||||||
|
errors.push('QUIC routes require TLS configuration (action.tls) — QUIC mandates TLS 1.3');
|
||||||
|
} else if (route.action.tls.mode === 'passthrough') {
|
||||||
|
errors.push('QUIC routes cannot use TLS mode "passthrough" — use "terminate" or "terminate-and-reencrypt"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Protocol quic/http3 requires transport udp or all
|
||||||
|
if (route.match?.protocol && ['quic', 'http3'].includes(route.match.protocol)) {
|
||||||
|
if (route.match.transport && route.match.transport !== 'udp' && route.match.transport !== 'all') {
|
||||||
|
errors.push(`Protocol "${route.match.protocol}" requires transport "udp" or "all"`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate security settings
|
// Validate security settings
|
||||||
@@ -619,6 +635,15 @@ export function validateRouteAction(action: IRouteAction): { valid: boolean; err
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QUIC routes require TLS with termination
|
||||||
|
if (action.udp?.quic && action.type === 'forward') {
|
||||||
|
if (!action.tls) {
|
||||||
|
errors.push('QUIC routes require TLS configuration — QUIC mandates TLS 1.3');
|
||||||
|
} else if (action.tls.mode === 'passthrough') {
|
||||||
|
errors.push('QUIC routes cannot use TLS mode "passthrough"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (action.type === 'socket-handler') {
|
if (action.type === 'socket-handler') {
|
||||||
if (!action.socketHandler && !action.datagramHandler) {
|
if (!action.socketHandler && !action.datagramHandler) {
|
||||||
errors.push('Socket handler or datagram handler function is required for socket-handler action');
|
errors.push('Socket handler or datagram handler function is required for socket-handler action');
|
||||||
|
|||||||
Reference in New Issue
Block a user