noxy 0.0.7

HTTP forward and reverse proxy with a pluggable tower middleware pipeline
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
mod common;

use std::convert::Infallible;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};

use axum::Router;
use axum::response::sse::{Event, Sse};
use axum::routing::get;
use axum_server::tls_rustls::RustlsConfig;
use common::*;
use futures_util::stream::StreamExt;
use http_body_util::BodyExt;
use noxy::http::{Body, BoxError, HttpService, full_body};
use noxy::{CertificateAuthority, Proxy};
use rcgen::{CertificateParams, KeyPair};
use tokio::net::TcpListener;

/// A tower Service that adds a response header, wrapping an inner service.
struct AddResponseHeader {
    inner: HttpService,
    name: http::HeaderName,
    value: http::HeaderValue,
}

impl tower::Service<http::Request<Body>> for AddResponseHeader {
    type Response = http::Response<Body>;
    type Error = BoxError;
    type Future = Pin<Box<dyn Future<Output = Result<http::Response<Body>, BoxError>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        let fut = self.inner.call(req);
        let name = self.name.clone();
        let value = self.value.clone();
        Box::pin(async move {
            let mut resp = fut.await?;
            resp.headers_mut().insert(name, value);
            Ok(resp)
        })
    }
}

#[tokio::test]
async fn proxy_relays_data() {
    let upstream_addr = start_upstream("hello world").await;
    let proxy_addr = start_proxy(vec![]).await;
    let client = http_client(proxy_addr);

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.text().await.unwrap(), "hello world");
}

#[tokio::test]
async fn proxy_applies_layer() {
    let upstream_addr = start_upstream("hello").await;
    let proxy_addr = start_proxy(vec![Box::new(|inner: HttpService| {
        tower::util::BoxService::new(AddResponseHeader {
            inner,
            name: http::HeaderName::from_static("x-proxy"),
            value: http::HeaderValue::from_static("noxy"),
        })
    })])
    .await;
    let client = http_client(proxy_addr);

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.headers().get("x-proxy").unwrap(), "noxy");
    assert_eq!(resp.text().await.unwrap(), "hello");
}

#[tokio::test]
async fn proxy_chains_layers() {
    let upstream_addr = start_upstream("hello").await;
    let proxy_addr = start_proxy(vec![
        Box::new(|inner: HttpService| {
            tower::util::BoxService::new(AddResponseHeader {
                inner,
                name: http::HeaderName::from_static("x-first"),
                value: http::HeaderValue::from_static("1"),
            })
        }),
        Box::new(|inner: HttpService| {
            tower::util::BoxService::new(AddResponseHeader {
                inner,
                name: http::HeaderName::from_static("x-second"),
                value: http::HeaderValue::from_static("2"),
            })
        }),
    ])
    .await;
    let client = http_client(proxy_addr);

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.headers().get("x-first").unwrap(), "1");
    assert_eq!(resp.headers().get("x-second").unwrap(), "2");
}

#[tokio::test]
async fn proxy_rejects_non_connect() {
    let proxy_addr = start_proxy(vec![]).await;

    let resp = reqwest::Client::builder()
        .no_proxy()
        .build()
        .unwrap()
        .get(format!("http://{proxy_addr}/"))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.status(), 400);
}

/// A tower Service that adds a request header before forwarding.
struct AddRequestHeader {
    inner: HttpService,
    name: http::HeaderName,
    value: http::HeaderValue,
}

impl tower::Service<http::Request<Body>> for AddRequestHeader {
    type Response = http::Response<Body>;
    type Error = BoxError;
    type Future = Pin<Box<dyn Future<Output = Result<http::Response<Body>, BoxError>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, mut req: http::Request<Body>) -> Self::Future {
        req.headers_mut()
            .insert(self.name.clone(), self.value.clone());
        self.inner.call(req)
    }
}

#[tokio::test]
async fn proxy_injects_request_header() {
    // Upstream echoes the x-injected header value back in the response body
    install_crypto_provider();
    let key_pair = KeyPair::generate().unwrap();
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let cert = params.self_signed(&key_pair).unwrap();
    let cert_der = cert.der().to_vec();
    let key_der = key_pair.serialized_der().to_vec();

    let config = RustlsConfig::from_der(vec![cert_der], key_der)
        .await
        .unwrap();

    let app = Router::new().route(
        "/",
        get(|headers: axum::http::HeaderMap| async move {
            headers
                .get("x-injected")
                .map(|v| v.to_str().unwrap().to_string())
                .unwrap_or_default()
        }),
    );

    let handle = axum_server::Handle::new();
    let listener_handle = handle.clone();

    tokio::spawn(async move {
        axum_server::bind_rustls("127.0.0.1:0".parse().unwrap(), config)
            .handle(handle)
            .serve(app.into_make_service())
            .await
            .unwrap();
    });

    let upstream_addr = listener_handle.listening().await.unwrap();

    let proxy_addr = start_proxy(vec![Box::new(|inner: HttpService| {
        tower::util::BoxService::new(AddRequestHeader {
            inner,
            name: http::HeaderName::from_static("x-injected"),
            value: http::HeaderValue::from_static("from-proxy"),
        })
    })])
    .await;
    let client = http_client(proxy_addr);

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.text().await.unwrap(), "from-proxy");
}

/// A tower Service that buffers the response body, computes a checksum
/// (sum of all bytes mod 256), and adds it as the `x-body-checksum` header.
struct AddBodyChecksum {
    inner: HttpService,
}

impl tower::Service<http::Request<Body>> for AddBodyChecksum {
    type Response = http::Response<Body>;
    type Error = BoxError;
    type Future = Pin<Box<dyn Future<Output = Result<http::Response<Body>, BoxError>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        let fut = self.inner.call(req);
        Box::pin(async move {
            let (parts, body) = fut.await?.into_parts();
            let bytes = body.collect().await?.to_bytes();
            let checksum: u8 = bytes.iter().fold(0u8, |acc, &b| acc.wrapping_add(b));
            let mut resp = http::Response::from_parts(parts, full_body(bytes));
            resp.headers_mut().insert(
                http::HeaderName::from_static("x-body-checksum"),
                http::HeaderValue::from_str(&checksum.to_string()).unwrap(),
            );
            Ok(resp)
        })
    }
}

#[tokio::test]
async fn proxy_layer_buffers_body_for_checksum() {
    let body = "hello world";
    let expected_checksum: u8 = body.bytes().fold(0u8, |acc, b| acc.wrapping_add(b));

    let upstream_addr = start_upstream(body).await;
    let proxy_addr = start_proxy(vec![Box::new(|inner: HttpService| {
        tower::util::BoxService::new(AddBodyChecksum { inner })
    })])
    .await;
    let client = http_client(proxy_addr);

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(
        resp.headers().get("x-body-checksum").unwrap(),
        &expected_checksum.to_string()
    );
    assert_eq!(resp.text().await.unwrap(), body);
}

#[tokio::test]
async fn proxy_streams_sse_incrementally() {
    const EVENT_COUNT: usize = 5;
    const EVENT_DELAY: Duration = Duration::from_millis(100);

    // Upstream SSE endpoint: sends EVENT_COUNT events with EVENT_DELAY between each
    install_crypto_provider();
    let key_pair = KeyPair::generate().unwrap();
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let cert = params.self_signed(&key_pair).unwrap();
    let cert_der = cert.der().to_vec();
    let key_der = key_pair.serialized_der().to_vec();

    let config = RustlsConfig::from_der(vec![cert_der], key_der)
        .await
        .unwrap();

    let app = Router::new().route(
        "/sse",
        get(|| async {
            Sse::new(futures_util::stream::unfold(0usize, |i| async move {
                if i >= EVENT_COUNT {
                    return None;
                }
                if i > 0 {
                    tokio::time::sleep(EVENT_DELAY).await;
                }
                Some((
                    Ok::<_, Infallible>(Event::default().data(format!("event-{i}"))),
                    i + 1,
                ))
            }))
        }),
    );

    let handle = axum_server::Handle::new();
    let listener_handle = handle.clone();

    tokio::spawn(async move {
        axum_server::bind_rustls("127.0.0.1:0".parse().unwrap(), config)
            .handle(handle)
            .serve(app.into_make_service())
            .await
            .unwrap();
    });

    let upstream_addr = listener_handle.listening().await.unwrap();
    let proxy_addr = start_proxy(vec![]).await;
    let client = http_client(proxy_addr);

    let start = Instant::now();
    let resp = client
        .get(format!("https://localhost:{}/sse", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    // Stream the response and collect events with their arrival times
    let mut stream = resp.bytes_stream();
    let mut events = Vec::new();
    let mut buf = String::new();

    while let Some(chunk) = stream.next().await {
        buf.push_str(&String::from_utf8_lossy(&chunk.unwrap()));
        // SSE events are separated by double newlines
        while let Some(pos) = buf.find("\n\n") {
            let event_text = buf[..pos].to_string();
            buf.drain(..pos + 2);
            if event_text.contains("data:") {
                events.push((event_text, start.elapsed()));
            }
        }
    }

    assert_eq!(events.len(), EVENT_COUNT, "should receive all SSE events");

    // The first event should arrive well before the total stream duration.
    // Total stream takes ~(EVENT_COUNT-1)*EVENT_DELAY = ~400ms.
    // If streaming works, the first event arrives in ~0ms (no delay before it).
    // If the proxy were buffering, all events would arrive together after ~400ms.
    let total_stream_time = EVENT_DELAY * (EVENT_COUNT as u32 - 1);
    assert!(
        events[0].1 < total_stream_time / 2,
        "first event arrived at {:?}, expected well before {:?} (total stream time) — \
         proxy may be buffering instead of streaming",
        events[0].1,
        total_stream_time,
    );

    // Verify event content
    for (i, (event_text, _)) in events.iter().enumerate() {
        assert!(
            event_text.contains(&format!("event-{i}")),
            "event {i} should contain 'event-{i}', got: {event_text}"
        );
    }
}

#[test]
fn certificate_authority_generates_valid_cert() {
    let ca = CertificateAuthority::from_pem_files("tests/dummy-cert.pem", "tests/dummy-key.pem")
        .unwrap();

    let (cert_der, key_der) = ca.generate_cert("example.com").unwrap();

    // Cert should be parseable
    assert!(!cert_der.is_empty());
    // Key should be parseable
    assert!(!key_der.secret_der().is_empty());
}

#[tokio::test]
async fn handshake_timeout_drops_slow_connection() {
    let upstream_addr = start_upstream("hello").await;

    let proxy = Proxy::builder()
        .ca_pem_files("tests/dummy-cert.pem", "tests/dummy-key.pem")
        .unwrap()
        .danger_accept_invalid_upstream_certs()
        .handshake_timeout(Duration::from_millis(200))
        .build()
        .unwrap();
    let proxy_addr = spawn_proxy(proxy).await;

    // Connect raw TCP and send CONNECT very slowly — the proxy should drop us
    use tokio::io::AsyncWriteExt;
    let mut stream = tokio::net::TcpStream::connect(proxy_addr).await.unwrap();
    stream
        .write_all(format!("CONNECT localhost:{}", upstream_addr.port()).as_bytes())
        .await
        .unwrap();

    // Sleep past the handshake timeout without finishing the request
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Try finishing the request — connection should be dead
    let result = stream.write_all(b" HTTP/1.1\r\n\r\n").await;
    if result.is_ok() {
        // Even if the write succeeds (buffered), the read should fail
        let mut buf = [0u8; 128];
        let n = tokio::io::AsyncReadExt::read(&mut stream, &mut buf)
            .await
            .unwrap_or(0);
        assert_eq!(n, 0, "expected proxy to have closed the connection");
    }
}

#[tokio::test]
async fn handshake_timeout_allows_fast_connection() {
    let upstream_addr = start_upstream("hello").await;

    let proxy = Proxy::builder()
        .ca_pem_files("tests/dummy-cert.pem", "tests/dummy-key.pem")
        .unwrap()
        .danger_accept_invalid_upstream_certs()
        .handshake_timeout(Duration::from_secs(10))
        .build()
        .unwrap();
    let proxy_addr = spawn_proxy(proxy).await;
    let client = http_client(proxy_addr);

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.text().await.unwrap(), "hello");
}

#[tokio::test]
async fn max_connections_applies_backpressure() {
    // Upstream that takes 300ms to respond
    install_crypto_provider();
    let key_pair = KeyPair::generate().unwrap();
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let cert = params.self_signed(&key_pair).unwrap();
    let cert_der = cert.der().to_vec();
    let key_der = key_pair.serialized_der().to_vec();

    let config = RustlsConfig::from_der(vec![cert_der], key_der)
        .await
        .unwrap();

    let app = Router::new().route(
        "/",
        get(|| async {
            tokio::time::sleep(Duration::from_millis(300)).await;
            "ok"
        }),
    );

    let handle = axum_server::Handle::new();
    let listener_handle = handle.clone();

    tokio::spawn(async move {
        axum_server::bind_rustls("127.0.0.1:0".parse().unwrap(), config)
            .handle(handle)
            .serve(app.into_make_service())
            .await
            .unwrap();
    });

    let upstream_addr = listener_handle.listening().await.unwrap();

    // Proxy allows only 2 concurrent connections
    let proxy = Proxy::builder()
        .ca_pem_files("tests/dummy-cert.pem", "tests/dummy-key.pem")
        .unwrap()
        .danger_accept_invalid_upstream_certs()
        .max_connections(2)
        .build()
        .unwrap();

    // Use the proxy's own listen() loop so the semaphore is enforced
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let proxy_addr = listener.local_addr().unwrap();

    tokio::spawn(async move {
        proxy.listen_on(listener).await.unwrap();
    });

    let client = http_client(proxy_addr);

    // Fire 3 requests concurrently — with max_connections=2, the 3rd must
    // wait for a slot, so total time should be ~600ms (2 batches of 300ms)
    // instead of ~300ms if all 3 ran in parallel.
    let start = Instant::now();
    let url = format!("https://localhost:{}/", upstream_addr.port());
    let (r1, r2, r3) = tokio::join!(
        client.get(&url).send(),
        client.get(&url).send(),
        client.get(&url).send(),
    );
    let elapsed = start.elapsed();

    assert_eq!(r1.unwrap().text().await.unwrap(), "ok");
    assert_eq!(r2.unwrap().text().await.unwrap(), "ok");
    assert_eq!(r3.unwrap().text().await.unwrap(), "ok");

    assert!(
        elapsed >= Duration::from_millis(500),
        "3 requests with max_connections=2 and 300ms upstream should take ~600ms, took {elapsed:?}"
    );
}

fn start_authenticated_proxy() -> noxy::ProxyBuilder {
    Proxy::builder()
        .ca_pem_files("tests/dummy-cert.pem", "tests/dummy-key.pem")
        .unwrap()
        .danger_accept_invalid_upstream_certs()
        .credential("admin", "secret")
        .credential("user2", "pass2")
}

fn http_client_with_auth(
    proxy_addr: SocketAddr,
    username: &str,
    password: &str,
) -> reqwest::Client {
    let ca_pem = std::fs::read("tests/dummy-cert.pem").unwrap();
    let ca_cert = reqwest::tls::Certificate::from_pem(&ca_pem).unwrap();

    reqwest::Client::builder()
        .proxy(
            reqwest::Proxy::https(format!("http://{proxy_addr}"))
                .unwrap()
                .basic_auth(username, password),
        )
        .add_root_certificate(ca_cert)
        .build()
        .unwrap()
}

#[tokio::test]
async fn proxy_auth_rejects_missing_credentials() {
    let upstream_addr = start_upstream("hello").await;

    let proxy = start_authenticated_proxy().build().unwrap();
    let proxy_addr = spawn_proxy(proxy).await;

    // Client without credentials — should get rejected (connection error)
    let client = http_client(proxy_addr);
    let result = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await;

    assert!(
        result.is_err(),
        "expected request without credentials to fail"
    );
}

#[tokio::test]
async fn proxy_auth_rejects_wrong_credentials() {
    let upstream_addr = start_upstream("hello").await;

    let proxy = start_authenticated_proxy().build().unwrap();
    let proxy_addr = spawn_proxy(proxy).await;

    let client = http_client_with_auth(proxy_addr, "admin", "wrong");
    let result = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await;

    assert!(
        result.is_err(),
        "expected request with wrong credentials to fail"
    );
}

#[tokio::test]
async fn proxy_auth_accepts_valid_credentials() {
    let upstream_addr = start_upstream("hello").await;

    let proxy = start_authenticated_proxy().build().unwrap();
    let proxy_addr = spawn_proxy(proxy).await;

    let client = http_client_with_auth(proxy_addr, "admin", "secret");
    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.text().await.unwrap(), "hello");
}

#[tokio::test]
async fn proxy_auth_accepts_second_credential() {
    let upstream_addr = start_upstream("hello").await;

    let proxy = start_authenticated_proxy().build().unwrap();
    let proxy_addr = spawn_proxy(proxy).await;

    let client = http_client_with_auth(proxy_addr, "user2", "pass2");
    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.text().await.unwrap(), "hello");
}

#[tokio::test]
async fn proxy_relays_websocket() {
    use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
    use futures_util::{SinkExt, StreamExt};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    async fn echo_ws(mut socket: WebSocket) {
        while let Some(Ok(msg)) = socket.recv().await {
            if matches!(msg, Message::Text(_) | Message::Binary(_))
                && socket.send(msg).await.is_err()
            {
                break;
            }
        }
    }

    install_crypto_provider();
    let key_pair = KeyPair::generate().unwrap();
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let cert = params.self_signed(&key_pair).unwrap();
    let cert_der = cert.der().clone();
    let key_der =
        rustls::pki_types::PrivateKeyDer::Pkcs8(key_pair.serialized_der().to_vec().into());

    let mut server_config = rustls::ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(vec![cert_der], key_der)
        .unwrap();
    server_config.alpn_protocols = vec![b"http/1.1".to_vec()];
    let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(server_config));

    let app = Router::new().route(
        "/ws",
        get(|ws: WebSocketUpgrade| async { ws.on_upgrade(echo_ws) }),
    );

    let upstream_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let upstream_addr = upstream_listener.local_addr().unwrap();

    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = upstream_listener.accept().await else {
                break;
            };
            let acceptor = acceptor.clone();
            let app = app.clone();
            tokio::spawn(async move {
                let Ok(tls_stream) = acceptor.accept(stream).await else {
                    return;
                };
                let io = hyper_util::rt::TokioIo::new(tls_stream);
                hyper::server::conn::http1::Builder::new()
                    .serve_connection(
                        io,
                        hyper::service::service_fn(
                            move |req: hyper::Request<hyper::body::Incoming>| {
                                let app = app.clone();
                                async move {
                                    use tower::Service;
                                    let mut app = app;
                                    let req = req.map(axum::body::Body::new);
                                    Ok::<_, Infallible>(app.call(req).await.unwrap())
                                }
                            },
                        ),
                    )
                    .with_upgrades()
                    .await
                    .ok();
            });
        }
    });

    let proxy_addr = start_proxy(vec![]).await;
    let port = upstream_addr.port();

    // Raw TCP → CONNECT → read 200
    let mut stream = tokio::net::TcpStream::connect(proxy_addr).await.unwrap();
    stream
        .write_all(
            format!("CONNECT localhost:{port} HTTP/1.1\r\nHost: localhost:{port}\r\n\r\n")
                .as_bytes(),
        )
        .await
        .unwrap();

    let mut buf = [0u8; 256];
    let mut total = 0;
    loop {
        let n = stream.read(&mut buf[total..]).await.unwrap();
        assert!(n > 0, "proxy closed connection before 200 response");
        total += n;
        if buf[..total].windows(4).any(|w| w == b"\r\n\r\n") {
            break;
        }
    }
    let resp = std::str::from_utf8(&buf[..total]).unwrap();
    assert!(
        resp.starts_with("HTTP/1.1 200"),
        "expected 200, got: {resp}"
    );

    // TLS handshake trusting the test CA
    let ca_pem = std::fs::read("tests/dummy-cert.pem").unwrap();
    let ca_certs: Vec<_> = rustls_pemfile::certs(&mut &*ca_pem)
        .collect::<Result<_, _>>()
        .unwrap();
    let mut root_store = rustls::RootCertStore::empty();
    for cert in ca_certs {
        root_store.add(cert).unwrap();
    }
    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();
    let connector = tokio_rustls::TlsConnector::from(Arc::new(tls_config));
    let server_name = rustls::pki_types::ServerName::try_from("localhost").unwrap();
    let tls_stream = connector.connect(server_name, stream).await.unwrap();

    // WebSocket handshake over the TLS stream
    let (mut ws, _) =
        tokio_tungstenite::client_async(format!("ws://localhost:{port}/ws"), tls_stream)
            .await
            .unwrap();

    // Send a message and assert echo
    ws.send(tokio_tungstenite::tungstenite::Message::Text(
        "hello".into(),
    ))
    .await
    .unwrap();
    let msg = ws.next().await.unwrap().unwrap();
    assert_eq!(msg.into_text().unwrap(), "hello");

    // Clean close
    ws.close(None).await.unwrap();
}

#[tokio::test]
async fn forward_proxy_with_tls_listener() {
    install_crypto_provider();
    let upstream_addr = start_upstream("hello tls forward").await;

    // Generate a self-signed cert for the proxy listener
    let key_pair = KeyPair::generate().unwrap();
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let cert = params.self_signed(&key_pair).unwrap();

    let cert_pem = cert.pem();
    let key_pem = key_pair.serialize_pem();

    let cert_path = std::env::temp_dir().join("noxy-test-forward-tls-cert.pem");
    let key_path = std::env::temp_dir().join("noxy-test-forward-tls-key.pem");
    std::fs::write(&cert_path, &cert_pem).unwrap();
    std::fs::write(&key_path, &key_pem).unwrap();

    let proxy = Proxy::builder()
        .ca_pem_files("tests/dummy-cert.pem", "tests/dummy-key.pem")
        .unwrap()
        .danger_accept_invalid_upstream_certs()
        .tls_identity(&cert_path, &key_path)
        .unwrap()
        .build()
        .unwrap();

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let proxy_addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        proxy.listen_on(listener).await.unwrap();
    });

    // Client trusts the proxy's listener cert and connects via HTTPS proxy
    let listener_ca = reqwest::tls::Certificate::from_pem(cert_pem.as_bytes()).unwrap();
    let ca_pem = std::fs::read("tests/dummy-cert.pem").unwrap();
    let mitm_ca = reqwest::tls::Certificate::from_pem(&ca_pem).unwrap();

    let client = reqwest::Client::builder()
        .proxy(reqwest::Proxy::all(format!("https://localhost:{}", proxy_addr.port())).unwrap())
        .add_root_certificate(listener_ca)
        .add_root_certificate(mitm_ca)
        .build()
        .unwrap();

    let resp = client
        .get(format!("https://localhost:{}/", upstream_addr.port()))
        .send()
        .await
        .unwrap();

    assert_eq!(resp.status(), 200);
    assert_eq!(resp.text().await.unwrap(), "hello tls forward");

    std::fs::remove_file(&cert_path).ok();
    std::fs::remove_file(&key_path).ok();
}