axum-reverse-proxy 1.3.0

A flexible and efficient reverse proxy implementation for Axum web applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use axum::{
    body::Body,
    http::{Request, Response, Uri},
};
use futures_util::{SinkExt, stream::StreamExt};
use http::{HeaderMap, HeaderValue, StatusCode};
use hyper_util::rt::TokioIo;
use tokio::net::TcpStream;
use tokio::sync::mpsc;
use tokio::time::{Duration, timeout};
use tokio_tungstenite::{
    MaybeTlsStream, WebSocketStream, connect_async,
    tungstenite::{Error, Message, handshake::derive_accept_key},
};
use tracing::{error, trace};
use url::{Host, Url};

/// Check if a request is a WebSocket upgrade request by examining the headers.
///
/// According to the WebSocket protocol specification (RFC 6455), a WebSocket upgrade request must have:
/// - An "Upgrade: websocket" header (case-insensitive)
/// - A "Connection: Upgrade" header (case-insensitive)
/// - A "Sec-WebSocket-Key" header with a base64-encoded 16-byte value
/// - A "Sec-WebSocket-Version" header
pub(crate) fn is_websocket_upgrade(headers: &HeaderMap<HeaderValue>) -> bool {
    // Check for required WebSocket upgrade headers
    let has_upgrade = headers
        .get("upgrade")
        .and_then(|v| v.to_str().ok())
        .map(|v| v.eq_ignore_ascii_case("websocket"))
        .unwrap_or(false);

    let has_connection = headers
        .get("connection")
        .and_then(|v| v.to_str().ok())
        .map(|v| {
            v.split(',')
                .any(|part| part.trim().eq_ignore_ascii_case("upgrade"))
        })
        .unwrap_or(false);

    let has_websocket_key = headers.contains_key("sec-websocket-key");
    let has_websocket_version = headers.contains_key("sec-websocket-version");

    trace!(
        "is_websocket_upgrade - upgrade: {has_upgrade}, connection: {has_connection}, websocket key: {has_websocket_key}, websocket version: {has_websocket_version}"
    );
    has_upgrade && has_connection && has_websocket_key && has_websocket_version
}

/// Compute the host header value for a WebSocket URL.
///
/// Returns (host_header, port) where host_header includes the port only if non-default.
fn compute_host_header_from_url(url: &Url) -> (String, u16) {
    let scheme = url.scheme();
    let host = match url.host() {
        Some(Host::Ipv6(addr)) => format!("[{addr}]"),
        Some(Host::Ipv4(addr)) => addr.to_string(),
        Some(Host::Domain(s)) => s.to_string(),
        None => "localhost".to_string(),
    };
    let port = match url.port() {
        Some(p) => p,
        None => {
            if scheme == "wss" {
                443
            } else {
                80
            }
        }
    };
    let header = if (scheme == "wss" && port == 443) || (scheme == "ws" && port == 80) {
        host.clone()
    } else {
        format!("{host}:{port}")
    };
    (header, port)
}

#[cfg(test)]
fn compute_host_header(url: &str) -> (String, u16) {
    let url = Url::parse(url).unwrap();
    compute_host_header_from_url(&url)
}

/// Handle a WebSocket upgrade request by:
/// 1. Validating the upgrade request
/// 2. Computing the WebSocket accept key
/// 3. Establishing a connection to the upstream server
/// 4. Returning an upgrade response to the client
/// 5. Spawning a task to bridge the client and upstream WebSocket connections
///
/// The upstream WebSocket connection is established **before** the 101 response is
/// returned to the client. This ensures the client only receives a successful upgrade
/// if the upstream server actually accepted the WebSocket connection.
///
/// `Sec-WebSocket-Extensions` headers are stripped when forwarding to upstream because
/// the proxy performs frame-level forwarding (not raw byte forwarding) and cannot
/// transparently handle extensions like `permessage-deflate`.
///
/// This function follows the WebSocket protocol specification (RFC 6455) for the upgrade handshake.
/// It ensures that all required headers are properly handled and forwarded to the upstream server.
pub(crate) async fn handle_websocket_with_upstream_uri(
    req: Request<Body>,
    upstream_http_uri: Uri,
) -> Result<Response<Body>, Box<dyn std::error::Error + Send + Sync>> {
    trace!("Handling WebSocket upgrade request");

    // Get the WebSocket key before upgrading
    let ws_key = req
        .headers()
        .get("sec-websocket-key")
        .and_then(|key| key.to_str().ok())
        .ok_or("Missing or invalid Sec-WebSocket-Key header")?;

    // Calculate the WebSocket accept key
    let ws_accept = derive_accept_key(ws_key.as_bytes());

    // Build a ws:// or wss:// URL from the provided upstream HTTP URI (which already
    // has correct path+query joining applied)
    let scheme = upstream_http_uri.scheme_str().unwrap_or("http");
    let ws_scheme = match scheme {
        "wss" | "ws" => scheme,
        "https" => "wss",
        _ => "ws",
    };

    let authority = upstream_http_uri
        .authority()
        .ok_or("Upstream URI missing authority")?
        .as_str();
    let path_q = upstream_http_uri
        .path_and_query()
        .map(|pq| pq.as_str())
        .unwrap_or("/");
    let upstream_url = format!("{ws_scheme}://{authority}{path_q}");

    trace!("Connecting to upstream WebSocket at {}", upstream_url);

    // Parse the URL and compute the host header
    let url = Url::parse(&upstream_url)?;
    let (host_header, _port) = compute_host_header_from_url(&url);

    // Forward all headers except host and sec-websocket-extensions to upstream.
    // Extensions are stripped because the proxy performs frame-level forwarding and
    // cannot transparently handle negotiated extensions (e.g. permessage-deflate).
    let mut request = tokio_tungstenite::tungstenite::handshake::client::Request::builder()
        .uri(upstream_url)
        .header("host", host_header);

    for (key, value) in req.headers() {
        if key != "host" && key != "sec-websocket-extensions" {
            request = request.header(key.as_str(), value);
        }
    }

    // Build the request
    let request = request.body(())?;

    // Connect to upstream WebSocket BEFORE returning 101 to the client.
    // This ensures we only tell the client the upgrade succeeded if the
    // upstream actually accepted the WebSocket connection.
    let (upstream_ws, upstream_response) = timeout(Duration::from_secs(5), connect_async(request))
        .await
        .map_err(|_| "Upstream WebSocket connection timed out")??;

    trace!("Upstream WebSocket connected successfully");

    // Build 101 response for client
    let mut response_builder = Response::builder()
        .status(StatusCode::SWITCHING_PROTOCOLS)
        .header("Upgrade", "websocket")
        .header("Connection", "Upgrade")
        .header("Sec-WebSocket-Accept", ws_accept);

    // Forward negotiated sub-protocol from upstream if present
    if let Some(protocol) = upstream_response.headers().get("sec-websocket-protocol") {
        response_builder = response_builder.header("Sec-WebSocket-Protocol", protocol);
    }

    trace!("Returning upgrade response to client");
    let response = response_builder.body(Body::empty())?;

    // Spawn a task to bridge the client and upstream WebSocket connections
    let (parts, body) = req.into_parts();
    let req = Request::from_parts(parts, body);
    tokio::spawn(async move {
        match handle_websocket_bridge(req, upstream_ws).await {
            Ok(_) => trace!("WebSocket connection closed gracefully"),
            Err(e) => error!("WebSocket connection error: {}", e),
        }
    });

    Ok(response)
}

/// Bridge an upgraded client connection with an already-connected upstream WebSocket.
///
/// This function:
/// 1. Completes the HTTP upgrade on the client connection
/// 2. Wraps both connections as WebSocket streams
/// 3. Creates two tasks for bidirectional message forwarding:
///    - Client to upstream: forwards messages from the client to the upstream server
///    - Upstream to client: forwards messages from the upstream server to the client
/// 4. Handles various WebSocket message types:
///    - Text messages
///    - Binary messages
///    - Ping/Pong messages
///    - Close frames
///
/// The connection is maintained until either:
/// - A close frame is received from either side
/// - An error occurs in the connection
/// - The connection is dropped
///
/// When a close frame is received, it is properly forwarded to ensure clean connection termination.
async fn handle_websocket_bridge(
    req: Request<Body>,
    upstream_ws: WebSocketStream<MaybeTlsStream<TcpStream>>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let upgraded = match timeout(Duration::from_secs(5), hyper::upgrade::on(req)).await {
        Ok(Ok(upgraded)) => upgraded,
        Ok(Err(e)) => return Err(Box::new(e)),
        Err(e) => return Err(Box::new(e)),
    };

    let io = TokioIo::new(upgraded);
    let client_ws = tokio_tungstenite::WebSocketStream::from_raw_socket(
        io,
        tokio_tungstenite::tungstenite::protocol::Role::Server,
        None,
    )
    .await;

    let (mut client_sender, mut client_receiver) = client_ws.split();
    let (mut upstream_sender, mut upstream_receiver) = upstream_ws.split();

    let (close_tx, mut close_rx) = mpsc::channel::<()>(1);
    let close_tx_upstream = close_tx.clone();

    let client_to_upstream = tokio::spawn(async move {
        let mut client_closed = false;
        while let Some(msg) = client_receiver.next().await {
            let msg = msg?;
            match msg {
                Message::Close(_) => {
                    if !client_closed {
                        upstream_sender.send(Message::Close(None)).await?;
                        close_tx.send(()).await.ok();
                        client_closed = true;
                        break;
                    }
                }
                msg @ Message::Binary(_)
                | msg @ Message::Text(_)
                | msg @ Message::Ping(_)
                | msg @ Message::Pong(_) => {
                    if !client_closed {
                        upstream_sender.send(msg).await?;
                    }
                }
                Message::Frame(_) => {}
            }
        }
        if !client_closed {
            upstream_sender.send(Message::Close(None)).await?;
            close_tx.send(()).await.ok();
        }
        Ok::<_, Error>(())
    });

    let upstream_to_client = tokio::spawn(async move {
        let mut upstream_closed = false;
        while let Some(msg) = upstream_receiver.next().await {
            let msg = msg?;
            match msg {
                Message::Close(_) => {
                    if !upstream_closed {
                        client_sender.send(Message::Close(None)).await?;
                        close_tx_upstream.send(()).await.ok();
                        upstream_closed = true;
                        break;
                    }
                }
                msg @ Message::Binary(_)
                | msg @ Message::Text(_)
                | msg @ Message::Ping(_)
                | msg @ Message::Pong(_) => {
                    if !upstream_closed {
                        client_sender.send(msg).await?;
                    }
                }
                Message::Frame(_) => {}
            }
        }
        if !upstream_closed {
            client_sender.send(Message::Close(None)).await?;
            close_tx_upstream.send(()).await.ok();
        }
        Ok::<_, Error>(())
    });

    tokio::select! {
        _ = close_rx.recv() => {
            trace!("WebSocket connection closed gracefully");
        }
        res = client_to_upstream => {
            if let Err(e) = res {
                error!("Client to upstream task failed: {:?}", e);
            }
        }
        res = upstream_to_client => {
            if let Err(e) = res {
                error!("Upstream to client task failed: {:?}", e);
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{compute_host_header, is_websocket_upgrade};
    use http::{HeaderMap, HeaderValue};

    #[test]
    fn host_header_ws_default_port() {
        let (host, port) = compute_host_header("ws://example.com/path");
        assert_eq!(host, "example.com");
        assert_eq!(port, 80);
    }

    #[test]
    fn host_header_wss_default_port() {
        let (host, port) = compute_host_header("wss://example.com/path");
        assert_eq!(host, "example.com");
        assert_eq!(port, 443);
    }

    #[test]
    fn host_header_wss_custom_port() {
        let (host, port) = compute_host_header("wss://example.com:8443/path");
        assert_eq!(host, "example.com:8443");
        assert_eq!(port, 8443);
    }

    #[test]
    fn websocket_upgrade_valid_headers() {
        let mut headers = HeaderMap::new();
        headers.insert("Upgrade", HeaderValue::from_static("websocket"));
        headers.insert(
            "Connection",
            HeaderValue::from_static("keep-alive, Upgrade"),
        );
        headers.insert(
            "Sec-WebSocket-Key",
            HeaderValue::from_static("dGhlIHNhbXBsZSBub25jZQ=="),
        );
        headers.insert("Sec-WebSocket-Version", HeaderValue::from_static("13"));

        assert!(is_websocket_upgrade(&headers));
    }

    #[test]
    fn websocket_upgrade_missing_upgrade_header() {
        let mut headers = HeaderMap::new();
        headers.insert("Connection", HeaderValue::from_static("Upgrade"));
        headers.insert(
            "Sec-WebSocket-Key",
            HeaderValue::from_static("dGhlIHNhbXBsZSBub25jZQ=="),
        );
        headers.insert("Sec-WebSocket-Version", HeaderValue::from_static("13"));

        assert!(!is_websocket_upgrade(&headers));
    }

    #[test]
    fn websocket_upgrade_invalid_connection_header() {
        let mut headers = HeaderMap::new();
        headers.insert("Upgrade", HeaderValue::from_static("websocket"));
        headers.insert("Connection", HeaderValue::from_static("keep-alive"));
        headers.insert(
            "Sec-WebSocket-Key",
            HeaderValue::from_static("dGhlIHNhbXBsZSBub25jZQ=="),
        );
        headers.insert("Sec-WebSocket-Version", HeaderValue::from_static("13"));

        assert!(!is_websocket_upgrade(&headers));
    }

    #[test]
    fn websocket_upgrade_missing_key_or_version() {
        let mut headers = HeaderMap::new();
        headers.insert("Upgrade", HeaderValue::from_static("websocket"));
        headers.insert("Connection", HeaderValue::from_static("Upgrade"));

        // Missing Sec-WebSocket-Key
        headers.insert("Sec-WebSocket-Version", HeaderValue::from_static("13"));
        assert!(!is_websocket_upgrade(&headers));

        headers.insert(
            "Sec-WebSocket-Key",
            HeaderValue::from_static("dGhlIHNhbXBsZSBub25jZQ=="),
        );
        headers.remove("Sec-WebSocket-Version");
        assert!(!is_websocket_upgrade(&headers));
    }

    #[test]
    fn host_header_ipv6_with_port() {
        let (host, port) = compute_host_header("ws://[::1]:9000/path");
        assert_eq!(host, "[::1]:9000");
        assert_eq!(port, 9000);
    }
}