runewarp 0.1.0

Runewarp is an ingress tunneling tool for exposing local services without moving TLS termination to the edge. Clients connect out over QUIC, so you can publish services without putting your backend directly on the Internet or leaking your public IP.
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
use std::collections::HashMap;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use quinn::{RecvStream, SendStream};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf, copy_bidirectional};
use tokio::net::TcpStream;
use tokio_rustls::TlsAcceptor;

use crate::acme::ACME_TLS_ALPN;
use crate::client_hello::{ParsedClientHello, read_client_hello};
use crate::runtime_log;
use crate::runtime_log::{AcmeEvent, AcmeRole, ClientRouteOutcome};
use crate::{
    ClientServiceSettings, ClientTlsMode, proxy::proxy_stream_error_code,
    proxy::proxy_tcp_over_quic,
};

#[derive(Clone)]
pub(crate) struct TunnelConnectionStreamHandler {
    services: Arc<[ClientServiceSettings]>,
    hostname_tls_configs: Arc<HashMap<String, Arc<rustls::ServerConfig>>>,
    hostname_acme_challenge_tls_configs: Arc<HashMap<String, Arc<rustls::ServerConfig>>>,
}

impl TunnelConnectionStreamHandler {
    #[cfg(test)]
    pub(crate) fn new(
        services: Vec<ClientServiceSettings>,
        hostname_tls_configs: HashMap<String, Arc<rustls::ServerConfig>>,
    ) -> Self {
        Self::new_with_acme_challenge_configs(services, hostname_tls_configs, HashMap::new())
    }

    pub(crate) fn new_with_acme_challenge_configs(
        services: Vec<ClientServiceSettings>,
        hostname_tls_configs: HashMap<String, Arc<rustls::ServerConfig>>,
        hostname_acme_challenge_tls_configs: HashMap<String, Arc<rustls::ServerConfig>>,
    ) -> Self {
        Self {
            services: services.into(),
            hostname_tls_configs: Arc::new(hostname_tls_configs),
            hostname_acme_challenge_tls_configs: Arc::new(hostname_acme_challenge_tls_configs),
        }
    }

    pub(crate) async fn handle(&self, send: SendStream, mut recv: RecvStream) -> io::Result<()> {
        let parsed_client_hello = match read_client_hello(&mut recv).await {
            Ok(parsed_client_hello) => parsed_client_hello,
            Err(error) => {
                runtime_log::warning("client", &format!("rejected stream: {error}"));
                reject_stream(send, recv);
                return Err(io::Error::other(error));
            }
        };
        let public_hostname = parsed_client_hello.server_name().to_owned();
        let Some(service) = self.select_service(&public_hostname) else {
            runtime_log::client_route(
                &public_hostname,
                ClientRouteOutcome::RejectedNoMatchingService,
            );
            reject_stream(send, recv);
            return Ok(());
        };

        if service.tls_mode == ClientTlsMode::Terminate {
            return self
                .handle_terminate(send, recv, parsed_client_hello, service)
                .await;
        }

        let (_, buffered_bytes) = parsed_client_hello.into_parts();

        let mut backend_stream = match TcpStream::connect(service.backend_address.as_str()).await {
            Ok(stream) => stream,
            Err(error) => {
                runtime_log::client_route(
                    &public_hostname,
                    ClientRouteOutcome::BackendConnectFailed {
                        backend_address: service.backend_address.as_str(),
                    },
                );
                reject_stream(send, recv);
                return Err(error);
            }
        };
        if let Err(error) = backend_stream.write_all(&buffered_bytes).await {
            runtime_log::client_route(
                &public_hostname,
                ClientRouteOutcome::BackendWriteFailed {
                    backend_address: service.backend_address.as_str(),
                },
            );
            reject_stream(send, recv);
            return Err(error);
        }
        runtime_log::client_route(
            &public_hostname,
            ClientRouteOutcome::Passthrough {
                backend_address: service.backend_address.as_str(),
            },
        );

        proxy_tcp_over_quic(backend_stream, Vec::new(), send, recv).await
    }

    async fn handle_terminate(
        &self,
        send: SendStream,
        recv: RecvStream,
        parsed_client_hello: ParsedClientHello,
        service: &ClientServiceSettings,
    ) -> io::Result<()> {
        let public_hostname = parsed_client_hello.server_name().to_owned();
        if parsed_client_hello.offers_alpn_protocol(ACME_TLS_ALPN)
            && let Some(challenge_tls_config) = self
                .hostname_acme_challenge_tls_configs
                .get(public_hostname.as_str())
        {
            return self
                .handle_acme_tls_alpn_challenge(
                    send,
                    recv,
                    public_hostname.as_str(),
                    parsed_client_hello.into_buffered_bytes(),
                    challenge_tls_config,
                )
                .await;
        }

        let Some(tls_config) = self.hostname_tls_configs.get(public_hostname.as_str()) else {
            runtime_log::client_route(
                public_hostname.as_str(),
                ClientRouteOutcome::MissingTlsConfig,
            );
            let mut s = send;
            let mut r = recv;
            let _ = s.reset(proxy_stream_error_code());
            let _ = r.stop(proxy_stream_error_code());
            return Err(io::Error::other(format!(
                "no TLS config for {public_hostname}"
            )));
        };

        let acceptor = TlsAcceptor::from(tls_config.clone());
        // Replay the buffered ClientHello bytes back into the stream so TlsAcceptor can
        // complete the handshake from the beginning of the TLS record stream.
        let quic_stream =
            ReplayedQuicBiStream::new(send, recv, parsed_client_hello.into_buffered_bytes());
        let mut tls_stream = match acceptor.accept(quic_stream).await {
            Ok(stream) => stream,
            Err(error) => {
                return Err(io::Error::other(format!(
                    "TLS termination handshake failed for {public_hostname}: {error}"
                )));
            }
        };

        let mut backend_stream = match TcpStream::connect(service.backend_address.as_str()).await {
            Ok(stream) => stream,
            Err(error) => {
                runtime_log::client_route(
                    public_hostname.as_str(),
                    ClientRouteOutcome::BackendConnectFailed {
                        backend_address: service.backend_address.as_str(),
                    },
                );
                return Err(error);
            }
        };

        runtime_log::client_route(
            public_hostname.as_str(),
            ClientRouteOutcome::Terminated {
                backend_address: service.backend_address.as_str(),
            },
        );

        copy_bidirectional(&mut tls_stream, &mut backend_stream)
            .await
            .map(|_| ())
    }

    async fn handle_acme_tls_alpn_challenge(
        &self,
        send: SendStream,
        recv: RecvStream,
        public_hostname: &str,
        buffered_bytes: Vec<u8>,
        challenge_tls_config: &Arc<rustls::ServerConfig>,
    ) -> io::Result<()> {
        let acceptor = TlsAcceptor::from(challenge_tls_config.clone());
        let quic_stream = ReplayedQuicBiStream::new(send, recv, buffered_bytes);
        match acceptor.accept(quic_stream).await {
            Ok(_) => {
                runtime_log::acme(
                    AcmeRole::Client { public_hostname },
                    AcmeEvent::ChallengeHandled,
                );
                Ok(())
            }
            Err(error) => {
                let error = error.to_string();
                runtime_log::acme(
                    AcmeRole::Client { public_hostname },
                    AcmeEvent::ChallengeFailed {
                        error: error.as_str(),
                    },
                );
                Err(io::Error::other(format!(
                    "ACME TLS-ALPN-01 handshake failed for {public_hostname}: {error}"
                )))
            }
        }
    }

    fn select_service(&self, public_hostname: &str) -> Option<&ClientServiceSettings> {
        if let [service] = &*self.services
            && service.public_hostnames.is_none()
        {
            return Some(service);
        }

        self.services.iter().find(|service| {
            service.public_hostnames.as_ref().is_some_and(|hostnames| {
                hostnames.iter().any(|hostname| hostname == public_hostname)
            })
        })
    }
}

/// A bidirectional QUIC stream that replays `buffered_bytes` before reading from `recv`.
/// Used to feed back a partially-consumed TLS ClientHello to `TlsAcceptor`.
struct ReplayedQuicBiStream {
    send: SendStream,
    recv: RecvStream,
    buffered_bytes: Vec<u8>,
    replay_offset: usize,
}

impl ReplayedQuicBiStream {
    fn new(send: SendStream, recv: RecvStream, buffered_bytes: Vec<u8>) -> Self {
        Self {
            send,
            recv,
            buffered_bytes,
            replay_offset: 0,
        }
    }
}

impl AsyncRead for ReplayedQuicBiStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        if self.replay_offset < self.buffered_bytes.len() {
            let remaining = &self.buffered_bytes[self.replay_offset..];
            let to_copy = remaining.len().min(buf.remaining());
            buf.put_slice(&remaining[..to_copy]);
            self.replay_offset += to_copy;
            return Poll::Ready(Ok(()));
        }
        Pin::new(&mut self.recv).poll_read(cx, buf)
    }
}

impl AsyncWrite for ReplayedQuicBiStream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        Pin::new(&mut self.send)
            .poll_write(cx, buf)
            .map(|result| result.map_err(io::Error::other))
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Pin::new(&mut self.send)
            .poll_flush(cx)
            .map(|result| result.map_err(io::Error::other))
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Pin::new(&mut self.send)
            .poll_shutdown(cx)
            .map(|result| result.map_err(io::Error::other))
    }
}

fn reject_stream(mut send: SendStream, mut recv: RecvStream) {
    let _ = send.reset(proxy_stream_error_code());
    let _ = recv.stop(proxy_stream_error_code());
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::io;
    use std::net::{Ipv4Addr, SocketAddr};
    use std::pin::Pin;
    use std::sync::{Arc, Mutex};
    use std::task::{Context, Poll};
    use std::time::Duration;

    use quinn::{Connection, Endpoint, RecvStream, SendStream};
    use rcgen::generate_simple_self_signed;
    use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName};
    use rustls::{ClientConnection, RootCertStore};
    use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
    use tokio::net::TcpListener;
    use tokio::sync::Mutex as AsyncMutex;
    use tokio::task::JoinHandle;
    use tokio::time::timeout;
    use tokio_rustls::{TlsAcceptor, TlsConnector};
    use tracing_subscriber::filter::LevelFilter;
    use tracing_subscriber::fmt::writer::MakeWriter;
    use tracing_subscriber::layer::SubscriberExt;

    use super::{ACME_TLS_ALPN, TunnelConnectionStreamHandler};
    use crate::{
        ClientServiceSettings, ClientTlsMode, LogLevel, make_client_quic_config,
        make_server_quic_config,
    };

    static LOG_CAPTURE_LOCK: AsyncMutex<()> = AsyncMutex::const_new(());

    #[tokio::test]
    async fn forwards_streams_for_exact_match_services() -> io::Result<()> {
        assert_forwarded_stream(
            vec![
                ClientServiceSettings {
                    public_hostnames: Some(vec!["app.example.test".to_owned()]),
                    backend_address: "127.0.0.1:443".to_owned(),
                    tls_mode: ClientTlsMode::Passthrough,
                },
                ClientServiceSettings {
                    public_hostnames: Some(vec!["api.example.test".to_owned()]),
                    backend_address: backend_placeholder(),
                    tls_mode: ClientTlsMode::Passthrough,
                },
            ],
            "api.example.test",
        )
        .await
    }

    #[tokio::test]
    async fn forwards_streams_for_the_catch_all_service() -> io::Result<()> {
        assert_forwarded_stream(
            vec![ClientServiceSettings {
                public_hostnames: None,
                backend_address: backend_placeholder(),
                tls_mode: ClientTlsMode::Passthrough,
            }],
            "app.example.test",
        )
        .await
    }

    #[tokio::test]
    async fn rejects_streams_without_a_matching_service() -> io::Result<()> {
        assert_rejected_stream(
            vec![ClientServiceSettings {
                public_hostnames: Some(vec!["app.example.test".to_owned()]),
                backend_address: "127.0.0.1:443".to_owned(),
                tls_mode: ClientTlsMode::Passthrough,
            }],
            "api.example.test",
            |result: io::Result<()>| assert!(result.is_ok()),
        )
        .await
    }

    #[tokio::test]
    async fn rejects_streams_when_backend_connect_fails() -> io::Result<()> {
        let backend_address = unused_localhost_address().await?.to_string();

        assert_rejected_stream(
            vec![ClientServiceSettings {
                public_hostnames: Some(vec!["app.example.test".to_owned()]),
                backend_address,
                tls_mode: ClientTlsMode::Passthrough,
            }],
            "app.example.test",
            |result: io::Result<()>| assert!(result.is_err()),
        )
        .await
    }

    #[tokio::test]
    #[allow(deprecated)]
    async fn rejects_streams_when_backend_write_fails() -> io::Result<()> {
        let backend_listener = TcpListener::bind(localhost(0)).await?;
        let backend_address = backend_listener.local_addr()?.to_string();
        let reset_backend_task = tokio::spawn(async move {
            let (backend_stream, _) = timeout(Duration::from_secs(1), backend_listener.accept())
                .await
                .map_err(|_| timeout_error("backend should accept a connection"))??;
            backend_stream.set_linger(Some(Duration::ZERO))?;
            drop(backend_stream);
            Ok::<(), io::Error>(())
        });

        assert_rejected_stream(
            vec![ClientServiceSettings {
                public_hostnames: Some(vec!["app.example.test".to_owned()]),
                backend_address,
                tls_mode: ClientTlsMode::Passthrough,
            }],
            "app.example.test",
            |result: io::Result<()>| assert!(result.is_err()),
        )
        .await?;

        reset_backend_task
            .await
            .map_err(|error| join_error("backend reset task failed", error))??;
        Ok(())
    }

    #[tokio::test(flavor = "current_thread")]
    async fn routing_logs_include_backend_selection_at_debug_and_warn_failures() -> io::Result<()> {
        let debug_output = capture_logs_with_wait(
            LogLevel::Debug,
            "DEBUG client route passthrough: public-hostname=app.example.test backend-address=",
            async {
                assert_forwarded_stream_without_spawning_handler(
                    vec![ClientServiceSettings {
                        public_hostnames: Some(vec!["app.example.test".to_owned()]),
                        backend_address: backend_placeholder(),
                        tls_mode: ClientTlsMode::Passthrough,
                    }],
                    "app.example.test",
                )
                .await
            },
        )
        .await?;

        assert!(!debug_output.contains("ping"));
        assert!(!debug_output.contains("pong"));
        assert!(debug_output.contains(
            "DEBUG client route passthrough: public-hostname=app.example.test backend-address="
        ));
        assert!(!debug_output.contains(backend_placeholder().as_str()));

        let info_output = capture_logs_with_wait(
            LogLevel::Info,
            "WARN client route unavailable: public-hostname=app.example.test reason=tls-config-missing",
            async {
                assert_forwarded_stream_without_spawning_handler(
                    vec![ClientServiceSettings {
                        public_hostnames: Some(vec!["app.example.test".to_owned()]),
                        backend_address: backend_placeholder(),
                        tls_mode: ClientTlsMode::Passthrough,
                    }],
                    "app.example.test",
                )
                .await?;

                assert_rejected_stream_without_spawning_handler(
                    vec![ClientServiceSettings {
                        public_hostnames: Some(vec!["app.example.test".to_owned()]),
                        backend_address: "127.0.0.1:443".to_owned(),
                        tls_mode: ClientTlsMode::Passthrough,
                    }],
                    "api.example.test",
                    |result: io::Result<()>| assert!(result.is_ok()),
                )
                .await?;

                let backend_address = unused_localhost_address().await?.to_string();
                assert_rejected_stream_without_spawning_handler(
                    vec![ClientServiceSettings {
                        public_hostnames: Some(vec!["app.example.test".to_owned()]),
                        backend_address,
                        tls_mode: ClientTlsMode::Passthrough,
                    }],
                    "app.example.test",
                    |result: io::Result<()>| assert!(result.is_err()),
                )
                .await?;

                assert_rejected_stream_without_spawning_handler(
                    vec![ClientServiceSettings {
                        public_hostnames: Some(vec!["app.example.test".to_owned()]),
                        backend_address: "127.0.0.1:8080".to_owned(),
                        tls_mode: ClientTlsMode::Terminate,
                    }],
                    "app.example.test",
                    |result: io::Result<()>| assert!(result.is_err()),
                )
                .await?;

                Ok(())
            },
        )
        .await?;

        assert!(!info_output.contains("passthrough to 127.0.0.1:"));
        assert!(info_output.contains(
            "WARN client route unavailable: public-hostname=api.example.test reason=no-matching-service"
        ));
        assert!(info_output.contains(
            "WARN client route unavailable: public-hostname=app.example.test backend-address="
        ));
        assert!(info_output.contains(
            "WARN client route unavailable: public-hostname=app.example.test reason=tls-config-missing"
        ));

        Ok(())
    }

    async fn assert_forwarded_stream_without_spawning_handler(
        mut services: Vec<ClientServiceSettings>,
        requested_hostname: &str,
    ) -> io::Result<()> {
        let (backend_cert, backend_key) = make_self_signed_cert(requested_hostname)?;
        let (backend_address, backend_task) = spawn_tls_backend(
            private_key_from_der(&backend_key),
            backend_cert.clone(),
            *b"pong",
        )
        .await?;
        for service in &mut services {
            if service.backend_address == backend_placeholder() {
                service.backend_address = backend_address.clone();
            }
        }

        let stream_handler = TunnelConnectionStreamHandler::new(services, HashMap::new());
        let fixture = TunnelConnectionFixture::connect().await?;
        let server_connection = fixture.server_connection.clone();
        let client_connection = fixture.client_connection.clone();

        let handle_stream = async {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept a tunnel stream"))?
                .map_err(io::Error::other)?;
            stream_handler.handle(send, recv).await
        };
        let request_stream =
            request_tls_response_over_tunnel(server_connection, &backend_cert, requested_hostname);
        let ((), response) = tokio::try_join!(handle_stream, request_stream)?;
        assert_eq!(response, *b"pong");

        backend_task
            .await
            .map_err(|error| join_error("backend task failed", error))??;
        Ok(())
    }

    async fn assert_rejected_stream_without_spawning_handler(
        services: Vec<ClientServiceSettings>,
        requested_hostname: &str,
        assert_handler_result: impl FnOnce(io::Result<()>),
    ) -> io::Result<()> {
        let stream_handler = TunnelConnectionStreamHandler::new(services, HashMap::new());
        let fixture = TunnelConnectionFixture::connect().await?;
        let server_connection = fixture.server_connection.clone();
        let client_connection = fixture.client_connection.clone();
        let client_hello = build_client_hello(requested_hostname)?;

        let handle_stream = async {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept a tunnel stream"))?
                .map_err(io::Error::other)?;
            stream_handler.handle(send, recv).await
        };
        let drive_rejected_stream = async {
            let (mut tunnel_send, mut tunnel_recv) =
                timeout(Duration::from_secs(1), server_connection.open_bi())
                    .await
                    .map_err(|_| timeout_error("test should open a tunnel stream"))?
                    .map_err(io::Error::other)?;
            tunnel_send.write_all(&client_hello).await?;
            tunnel_send.finish().map_err(io::Error::other)?;

            if let Ok(Ok(response)) =
                timeout(Duration::from_secs(1), tunnel_recv.read_to_end(1)).await
            {
                assert!(response.is_empty());
            }

            Ok::<(), io::Error>(())
        };
        let (handler_result, drive_result) = tokio::join!(handle_stream, drive_rejected_stream);
        drive_result?;
        assert_handler_result(handler_result);
        Ok(())
    }

    async fn assert_forwarded_stream(
        mut services: Vec<ClientServiceSettings>,
        requested_hostname: &str,
    ) -> io::Result<()> {
        let (backend_cert, backend_key) = make_self_signed_cert(requested_hostname)?;
        let (backend_address, backend_task) = spawn_tls_backend(
            private_key_from_der(&backend_key),
            backend_cert.clone(),
            *b"pong",
        )
        .await?;
        for service in &mut services {
            if service.backend_address == backend_placeholder() {
                service.backend_address = backend_address.clone();
            }
        }

        let stream_handler = TunnelConnectionStreamHandler::new(services, HashMap::new());
        let fixture = TunnelConnectionFixture::connect().await?;
        let server_connection = fixture.server_connection.clone();
        let client_connection = fixture.client_connection.clone();

        let stream_handler_task = tokio::spawn(async move {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept a tunnel stream"))?
                .map_err(io::Error::other)?;
            stream_handler.handle(send, recv).await
        });

        let response =
            request_tls_response_over_tunnel(server_connection, &backend_cert, requested_hostname)
                .await?;
        assert_eq!(response, *b"pong");

        backend_task
            .await
            .map_err(|error| join_error("backend task failed", error))??;
        stream_handler_task
            .await
            .map_err(|error| join_error("stream handler task failed", error))??;
        Ok(())
    }

    async fn assert_rejected_stream(
        services: Vec<ClientServiceSettings>,
        requested_hostname: &str,
        assert_handler_result: impl FnOnce(io::Result<()>) + Send + 'static,
    ) -> io::Result<()> {
        let stream_handler = TunnelConnectionStreamHandler::new(services, HashMap::new());
        let fixture = TunnelConnectionFixture::connect().await?;
        let server_connection = fixture.server_connection.clone();
        let client_connection = fixture.client_connection.clone();
        let client_hello = build_client_hello(requested_hostname)?;

        let stream_handler_task = tokio::spawn(async move {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept a tunnel stream"))?
                .map_err(io::Error::other)?;
            assert_handler_result(stream_handler.handle(send, recv).await);
            Ok::<(), io::Error>(())
        });

        let (mut tunnel_send, mut tunnel_recv) =
            timeout(Duration::from_secs(1), server_connection.open_bi())
                .await
                .map_err(|_| timeout_error("test should open a tunnel stream"))?
                .map_err(io::Error::other)?;
        tunnel_send.write_all(&client_hello).await?;
        tunnel_send.finish().map_err(io::Error::other)?;

        if let Ok(Ok(response)) = timeout(Duration::from_secs(1), tunnel_recv.read_to_end(1)).await
        {
            assert!(response.is_empty());
        }

        stream_handler_task
            .await
            .map_err(|error| join_error("stream handler task failed", error))??;
        Ok(())
    }

    async fn spawn_tls_backend(
        private_key: PrivateKeyDer<'static>,
        certificate: CertificateDer<'static>,
        response: [u8; 4],
    ) -> io::Result<(String, JoinHandle<io::Result<()>>)> {
        let listener = TcpListener::bind(localhost(0)).await?;
        let address = listener.local_addr()?.to_string();
        let acceptor = TlsAcceptor::from(Arc::new(
            rustls::ServerConfig::builder()
                .with_no_client_auth()
                .with_single_cert(vec![certificate], private_key)
                .map_err(io::Error::other)?,
        ));

        let task = tokio::spawn(async move {
            let (tcp_stream, _) = timeout(Duration::from_secs(1), listener.accept())
                .await
                .map_err(|_| timeout_error("backend should accept a forwarded connection"))??;
            let mut tls_stream = timeout(Duration::from_secs(1), acceptor.accept(tcp_stream))
                .await
                .map_err(|_| timeout_error("backend TLS handshake should complete"))?
                .map_err(io::Error::other)?;
            let mut request = [0_u8; 4];
            timeout(Duration::from_secs(1), tls_stream.read_exact(&mut request))
                .await
                .map_err(|_| timeout_error("backend should receive request bytes"))??;
            assert_eq!(&request, b"ping");
            timeout(Duration::from_secs(1), tls_stream.write_all(&response))
                .await
                .map_err(|_| timeout_error("backend should send response bytes"))??;
            timeout(Duration::from_secs(1), tls_stream.shutdown())
                .await
                .map_err(|_| timeout_error("backend should close cleanly"))??;
            Ok::<(), io::Error>(())
        });

        Ok((address, task))
    }

    async fn request_tls_response_over_tunnel(
        server_connection: Connection,
        backend_cert: &CertificateDer<'static>,
        requested_hostname: &str,
    ) -> io::Result<[u8; 4]> {
        let (send, recv) = timeout(Duration::from_secs(1), server_connection.open_bi())
            .await
            .map_err(|_| timeout_error("test should open a tunnel stream"))?
            .map_err(io::Error::other)?;
        let connector = TlsConnector::from(Arc::new(
            rustls::ClientConfig::builder()
                .with_root_certificates(root_store_with(backend_cert)?)
                .with_no_client_auth(),
        ));
        let server_name =
            ServerName::try_from(requested_hostname.to_owned()).map_err(io::Error::other)?;
        let mut tls_stream = timeout(
            Duration::from_secs(1),
            connector.connect(server_name, QuicBiStream::new(send, recv)),
        )
        .await
        .map_err(|_| timeout_error("TLS handshake over the tunnel should complete"))?
        .map_err(io::Error::other)?;
        timeout(Duration::from_secs(1), tls_stream.write_all(b"ping"))
            .await
            .map_err(|_| timeout_error("TLS client should send request bytes"))??;

        let mut response = [0_u8; 4];
        timeout(Duration::from_secs(1), tls_stream.read_exact(&mut response))
            .await
            .map_err(|_| timeout_error("TLS client should receive response bytes"))??;
        timeout(Duration::from_secs(1), tls_stream.shutdown())
            .await
            .map_err(|_| timeout_error("TLS client should close cleanly"))??;

        Ok(response)
    }

    struct TunnelConnectionFixture {
        _server_endpoint: Endpoint,
        _client_endpoint: Endpoint,
        server_connection: Connection,
        client_connection: Connection,
    }

    impl TunnelConnectionFixture {
        async fn connect() -> io::Result<Self> {
            let (certificate, private_key) = make_self_signed_cert("tunnel.example.test")?;
            let server_endpoint = Endpoint::server(
                make_server_quic_config(
                    vec![certificate.clone()],
                    private_key_from_der(&private_key),
                )
                .map_err(io::Error::other)?,
                localhost(0),
            )
            .map_err(io::Error::other)?;
            let server_addr = server_endpoint.local_addr()?;

            let mut client_endpoint = Endpoint::client(localhost(0)).map_err(io::Error::other)?;
            client_endpoint.set_default_client_config(
                make_client_quic_config(root_store_with(&certificate)?)
                    .map_err(io::Error::other)?,
            );

            let accept_connection = async {
                let incoming = timeout(Duration::from_secs(1), server_endpoint.accept())
                    .await
                    .map_err(|_| timeout_error("server endpoint should accept a QUIC connection"))?
                    .ok_or_else(|| {
                        io::Error::new(io::ErrorKind::UnexpectedEof, "server endpoint closed")
                    })?;
                timeout(Duration::from_secs(1), incoming)
                    .await
                    .map_err(|_| timeout_error("server should finish the QUIC handshake"))?
                    .map_err(io::Error::other)
            };
            let connect_client = async {
                let connect = client_endpoint
                    .connect(server_addr, "tunnel.example.test")
                    .map_err(io::Error::other)?;
                timeout(Duration::from_secs(1), connect)
                    .await
                    .map_err(|_| timeout_error("client should finish the QUIC handshake"))?
                    .map_err(io::Error::other)
            };
            let (server_connection, client_connection) =
                tokio::try_join!(accept_connection, connect_client)?;

            Ok(Self {
                _server_endpoint: server_endpoint,
                _client_endpoint: client_endpoint,
                server_connection,
                client_connection,
            })
        }
    }

    fn backend_placeholder() -> String {
        "__backend__".to_owned()
    }

    async fn unused_localhost_address() -> io::Result<SocketAddr> {
        let listener = TcpListener::bind(localhost(0)).await?;
        let address = listener.local_addr()?;
        drop(listener);
        Ok(address)
    }

    fn build_client_hello(server_name: &str) -> io::Result<Vec<u8>> {
        let trusted_cert =
            generate_simple_self_signed(vec!["localhost".to_owned()]).map_err(io::Error::other)?;
        let cert_der = CertificateDer::from(trusted_cert.cert);
        let mut roots = RootCertStore::empty();
        roots.add(cert_der).map_err(io::Error::other)?;

        let config = rustls::ClientConfig::builder()
            .with_root_certificates(roots)
            .with_no_client_auth();
        let mut connection = ClientConnection::new(
            Arc::new(config),
            ServerName::try_from(server_name.to_owned()).map_err(io::Error::other)?,
        )
        .map_err(io::Error::other)?;
        let mut bytes = Vec::new();
        connection.write_tls(&mut bytes)?;
        Ok(bytes)
    }

    fn localhost(port: u16) -> SocketAddr {
        SocketAddr::from((Ipv4Addr::LOCALHOST, port))
    }

    fn make_self_signed_cert(server_name: &str) -> io::Result<(CertificateDer<'static>, Vec<u8>)> {
        let certified_key =
            generate_simple_self_signed(vec![server_name.to_owned()]).map_err(io::Error::other)?;
        Ok((
            CertificateDer::from(certified_key.cert),
            certified_key.signing_key.serialize_der(),
        ))
    }

    fn private_key_from_der(der: &[u8]) -> PrivateKeyDer<'static> {
        PrivatePkcs8KeyDer::from(der.to_vec()).into()
    }

    fn root_store_with(certificate: &CertificateDer<'static>) -> io::Result<RootCertStore> {
        let mut roots = RootCertStore::empty();
        roots.add(certificate.clone()).map_err(io::Error::other)?;
        Ok(roots)
    }

    #[derive(Clone, Default)]
    struct SharedBuffer(Arc<Mutex<Vec<u8>>>);

    struct BufferWriter(SharedBuffer);

    impl SharedBuffer {
        fn read(&self) -> String {
            String::from_utf8(self.0.lock().expect("buffer mutex poisoned").clone())
                .expect("runtime log output must be valid UTF-8")
        }
    }

    impl std::io::Write for BufferWriter {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.0
                .0
                .lock()
                .expect("buffer mutex poisoned")
                .extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    impl<'writer> MakeWriter<'writer> for SharedBuffer {
        type Writer = BufferWriter;

        fn make_writer(&'writer self) -> Self::Writer {
            BufferWriter(self.clone())
        }
    }

    async fn capture_logs_with_wait<Fut>(
        level: LogLevel,
        needle: &str,
        action: Fut,
    ) -> io::Result<String>
    where
        Fut: std::future::Future<Output = io::Result<()>>,
    {
        let _lock = LOG_CAPTURE_LOCK.lock().await;
        let _ = crate::runtime_log::install(level);
        let buffer = SharedBuffer::default();
        let subscriber = tracing_subscriber::registry()
            .with(level_filter(level))
            .with(
                tracing_subscriber::fmt::layer()
                    .with_writer(buffer.clone())
                    .with_ansi(false)
                    .without_time()
                    .with_target(false),
            );
        let _guard = tracing::subscriber::set_default(subscriber);
        action.await?;
        timeout(Duration::from_secs(5), async {
            loop {
                if buffer.read().contains(needle) {
                    break;
                }
                tokio::task::yield_now().await;
            }
        })
        .await
        .map_err(|_| timeout_error("expected log line to be emitted within timeout"))?;
        Ok(buffer.read())
    }

    fn level_filter(level: LogLevel) -> LevelFilter {
        match level {
            LogLevel::Off => LevelFilter::OFF,
            LogLevel::Error => LevelFilter::ERROR,
            LogLevel::Warn => LevelFilter::WARN,
            LogLevel::Info => LevelFilter::INFO,
            LogLevel::Debug => LevelFilter::DEBUG,
            LogLevel::Trace => LevelFilter::TRACE,
        }
    }

    fn timeout_error(message: &'static str) -> io::Error {
        io::Error::new(io::ErrorKind::TimedOut, message)
    }

    fn join_error(context: &'static str, error: tokio::task::JoinError) -> io::Error {
        io::Error::other(format!("{context}: {error}"))
    }

    struct QuicBiStream {
        send: SendStream,
        recv: RecvStream,
    }

    impl QuicBiStream {
        fn new(send: SendStream, recv: RecvStream) -> Self {
            Self { send, recv }
        }
    }

    impl AsyncRead for QuicBiStream {
        fn poll_read(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<io::Result<()>> {
            Pin::new(&mut self.recv).poll_read(cx, buf)
        }
    }

    impl AsyncWrite for QuicBiStream {
        fn poll_write(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<io::Result<usize>> {
            Pin::new(&mut self.send)
                .poll_write(cx, buf)
                .map(|result| result.map_err(io::Error::other))
        }

        fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
            Pin::new(&mut self.send)
                .poll_flush(cx)
                .map(|result| result.map_err(io::Error::other))
        }

        fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
            Pin::new(&mut self.send)
                .poll_shutdown(cx)
                .map(|result| result.map_err(io::Error::other))
        }
    }

    // ---- TLS termination tests ----

    #[tokio::test]
    async fn terminates_tls_and_forwards_plaintext_to_backend() -> io::Result<()> {
        let hostname = "app.example.test";
        let (public_cert, public_key) = make_self_signed_cert(hostname)?;
        let tls_config = Arc::new(
            rustls::ServerConfig::builder()
                .with_no_client_auth()
                .with_single_cert(vec![public_cert.clone()], private_key_from_der(&public_key))
                .map_err(io::Error::other)?,
        );

        // Plain-TCP backend (receives decrypted data)
        let backend_listener = TcpListener::bind(localhost(0)).await?;
        let backend_address = backend_listener.local_addr()?.to_string();
        let backend_task = tokio::spawn(async move {
            use tokio::io::AsyncReadExt;
            let (mut backend_stream, _) =
                timeout(Duration::from_secs(1), backend_listener.accept())
                    .await
                    .map_err(|_| timeout_error("backend should accept a forwarded connection"))??;
            let mut request = [0_u8; 4];
            timeout(
                Duration::from_secs(1),
                backend_stream.read_exact(&mut request),
            )
            .await
            .map_err(|_| timeout_error("backend should receive request bytes"))??;
            assert_eq!(&request, b"ping");
            timeout(Duration::from_secs(1), backend_stream.write_all(b"pong"))
                .await
                .map_err(|_| timeout_error("backend should send response bytes"))??;
            timeout(Duration::from_secs(1), backend_stream.shutdown())
                .await
                .map_err(|_| timeout_error("backend should close cleanly"))??;
            Ok::<(), io::Error>(())
        });

        let services = vec![ClientServiceSettings {
            public_hostnames: Some(vec![hostname.to_owned()]),
            backend_address,
            tls_mode: ClientTlsMode::Terminate,
        }];
        let mut tls_configs = HashMap::new();
        tls_configs.insert(hostname.to_owned(), tls_config.clone());
        let stream_handler = TunnelConnectionStreamHandler::new(services, tls_configs);

        let fixture = TunnelConnectionFixture::connect().await?;
        let server_connection = fixture.server_connection.clone();
        let client_connection = fixture.client_connection.clone();

        let stream_handler_task = tokio::spawn(async move {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept a tunnel stream"))?
                .map_err(io::Error::other)?;
            stream_handler.handle(send, recv).await
        });

        // The tunnel sends a real TLS connection using the Public hostname CA
        let response = request_plaintext_response_over_terminated_tunnel(
            server_connection,
            &public_cert,
            hostname,
        )
        .await?;
        assert_eq!(response, *b"pong");

        backend_task
            .await
            .map_err(|error| join_error("backend task failed", error))??;
        stream_handler_task
            .await
            .map_err(|error| join_error("stream handler task failed", error))??;
        Ok(())
    }

    #[tokio::test(flavor = "current_thread")]
    async fn acme_challenge_traffic_logs_distinctly_from_terminate_routing() -> io::Result<()> {
        let hostname = "app.example.test";
        let (public_cert, public_key) = make_self_signed_cert(hostname)?;
        let default_tls_config = Arc::new(
            rustls::ServerConfig::builder()
                .with_no_client_auth()
                .with_single_cert(vec![public_cert.clone()], private_key_from_der(&public_key))
                .map_err(io::Error::other)?,
        );
        let mut challenge_server_config = rustls::ServerConfig::builder()
            .with_no_client_auth()
            .with_single_cert(vec![public_cert.clone()], private_key_from_der(&public_key))
            .map_err(io::Error::other)?;
        challenge_server_config.alpn_protocols = vec![ACME_TLS_ALPN.to_vec()];
        let challenge_tls_config = Arc::new(challenge_server_config);

        let backend_listener = TcpListener::bind(localhost(0)).await?;
        let backend_address = backend_listener.local_addr()?.to_string();
        let backend_task = tokio::spawn(async move {
            let (mut backend_stream, _) =
                timeout(Duration::from_secs(1), backend_listener.accept())
                    .await
                    .map_err(|_| {
                        timeout_error("backend should accept ordinary terminate traffic")
                    })??;
            let mut request = [0_u8; 4];
            timeout(
                Duration::from_secs(1),
                backend_stream.read_exact(&mut request),
            )
            .await
            .map_err(|_| timeout_error("backend should receive terminate request bytes"))??;
            assert_eq!(&request, b"ping");
            timeout(Duration::from_secs(1), backend_stream.write_all(b"pong"))
                .await
                .map_err(|_| timeout_error("backend should send terminate response bytes"))??;
            timeout(Duration::from_secs(1), backend_stream.shutdown())
                .await
                .map_err(|_| timeout_error("backend should close cleanly"))??;
            Ok::<(), io::Error>(())
        });

        let services = vec![ClientServiceSettings {
            public_hostnames: Some(vec![hostname.to_owned()]),
            backend_address,
            tls_mode: ClientTlsMode::Terminate,
        }];
        let mut tls_configs = HashMap::new();
        tls_configs.insert(hostname.to_owned(), default_tls_config);
        let mut challenge_tls_configs = HashMap::new();
        challenge_tls_configs.insert(hostname.to_owned(), challenge_tls_config);
        let stream_handler = TunnelConnectionStreamHandler::new_with_acme_challenge_configs(
            services,
            tls_configs,
            challenge_tls_configs,
        );

        let output = capture_logs_with_wait(
            LogLevel::Debug,
            "DEBUG client acme challenge handled: public-hostname=app.example.test",
            async {
                let fixture = TunnelConnectionFixture::connect().await?;
                let server_connection = fixture.server_connection.clone();
                let client_connection = fixture.client_connection.clone();

                let stream_handler_task = tokio::spawn(async move {
                    for _ in 0..2 {
                        let (send, recv) =
                            timeout(Duration::from_secs(1), client_connection.accept_bi())
                                .await
                                .map_err(|_| {
                                    timeout_error("handler should accept a tunnel stream")
                                })?
                                .map_err(io::Error::other)?;
                        stream_handler.clone().handle(send, recv).await?;
                    }
                    Ok::<(), io::Error>(())
                });

                let response = request_plaintext_response_over_terminated_tunnel(
                    server_connection.clone(),
                    &public_cert,
                    hostname,
                )
                .await?;
                assert_eq!(response, *b"pong");

                let (send, recv) = timeout(Duration::from_secs(1), server_connection.open_bi())
                    .await
                    .map_err(|_| timeout_error("test should open a tunnel stream"))?
                    .map_err(io::Error::other)?;
                let mut client_tls_config = rustls::ClientConfig::builder()
                    .with_root_certificates(root_store_with(&public_cert)?)
                    .with_no_client_auth();
                client_tls_config.alpn_protocols = vec![ACME_TLS_ALPN.to_vec()];
                let connector = TlsConnector::from(Arc::new(client_tls_config));
                let server_name =
                    ServerName::try_from(hostname.to_owned()).map_err(io::Error::other)?;
                let mut tls_stream = timeout(
                    Duration::from_secs(1),
                    connector.connect(server_name, QuicBiStream::new(send, recv)),
                )
                .await
                .map_err(|_| timeout_error("ACME challenge handshake should complete"))?
                .map_err(io::Error::other)?;
                timeout(Duration::from_secs(1), tls_stream.shutdown())
                    .await
                    .map_err(|_| timeout_error("ACME challenge client should close cleanly"))??;

                stream_handler_task
                    .await
                    .map_err(|error| join_error("stream handler task failed", error))??;
                Ok(())
            },
        )
        .await?;

        assert!(output.contains(
            "DEBUG client route terminate: public-hostname=app.example.test backend-address="
        ));
        assert_eq!(
            output
                .lines()
                .filter(|line| {
                    line.contains("client route terminate: public-hostname=app.example.test")
                })
                .count(),
            1
        );
        assert!(!output.contains("client route unavailable"));
        backend_task.abort();
        let _ = backend_task.await;
        Ok(())
    }

    /// Makes a TLS connection over the QUIC tunnel stream, talks to a plain-TCP backend
    /// (because the Client terminates TLS and forwards plaintext).
    async fn request_plaintext_response_over_terminated_tunnel(
        server_connection: Connection,
        public_cert: &CertificateDer<'static>,
        hostname: &str,
    ) -> io::Result<[u8; 4]> {
        let (send, recv) = timeout(Duration::from_secs(1), server_connection.open_bi())
            .await
            .map_err(|_| timeout_error("test should open a tunnel stream"))?
            .map_err(io::Error::other)?;
        let connector = TlsConnector::from(Arc::new(
            rustls::ClientConfig::builder()
                .with_root_certificates(root_store_with(public_cert)?)
                .with_no_client_auth(),
        ));
        let server_name = ServerName::try_from(hostname.to_owned()).map_err(io::Error::other)?;
        let mut tls_stream = timeout(
            Duration::from_secs(1),
            connector.connect(server_name, QuicBiStream::new(send, recv)),
        )
        .await
        .map_err(|_| timeout_error("TLS handshake over the tunnel should complete"))?
        .map_err(io::Error::other)?;
        timeout(Duration::from_secs(1), tls_stream.write_all(b"ping"))
            .await
            .map_err(|_| timeout_error("TLS client should send request bytes"))??;

        let mut response = [0_u8; 4];
        timeout(Duration::from_secs(1), tls_stream.read_exact(&mut response))
            .await
            .map_err(|_| timeout_error("TLS client should receive response bytes"))??;
        timeout(Duration::from_secs(1), tls_stream.shutdown())
            .await
            .map_err(|_| timeout_error("TLS client should close cleanly"))??;
        Ok(response)
    }

    // ---- Mixed terminate + passthrough tests ----

    /// A handler that owns both a terminating and a passthrough Service routes each stream
    /// independently: the terminating hostname gets TLS-terminated; the passthrough hostname
    /// is proxied with the original encrypted bytes intact.
    #[tokio::test]
    async fn routes_mixed_terminate_and_passthrough_services() -> io::Result<()> {
        let terminate_hostname = "app.example.test";
        let passthrough_hostname = "api.example.test";

        // TLS material for the terminating service (Client terminates, backend gets plaintext)
        let (terminate_cert, terminate_key) = make_self_signed_cert(terminate_hostname)?;
        let tls_config = Arc::new(
            rustls::ServerConfig::builder()
                .with_no_client_auth()
                .with_single_cert(
                    vec![terminate_cert.clone()],
                    private_key_from_der(&terminate_key),
                )
                .map_err(io::Error::other)?,
        );

        // Plain-TCP backend for the terminating service
        let term_backend_listener = TcpListener::bind(localhost(0)).await?;
        let term_backend_address = term_backend_listener.local_addr()?.to_string();
        let term_backend_task = tokio::spawn(async move {
            use tokio::io::AsyncReadExt;
            let (mut stream, _) = timeout(Duration::from_secs(1), term_backend_listener.accept())
                .await
                .map_err(|_| timeout_error("term backend should accept connection"))??;
            let mut buf = [0_u8; 4];
            timeout(Duration::from_secs(1), stream.read_exact(&mut buf))
                .await
                .map_err(|_| timeout_error("term backend should receive bytes"))??;
            assert_eq!(&buf, b"ping");
            timeout(Duration::from_secs(1), stream.write_all(b"pong"))
                .await
                .map_err(|_| timeout_error("term backend should send bytes"))??;
            timeout(Duration::from_secs(1), stream.shutdown())
                .await
                .map_err(|_| timeout_error("term backend should close cleanly"))??;
            Ok::<(), io::Error>(())
        });

        // TLS-terminating backend for the passthrough service (receives raw TLS)
        let (pass_cert, pass_key) = make_self_signed_cert(passthrough_hostname)?;
        let (pass_backend_address, pass_backend_task) =
            spawn_tls_backend(private_key_from_der(&pass_key), pass_cert.clone(), *b"pong").await?;

        let services = vec![
            ClientServiceSettings {
                public_hostnames: Some(vec![terminate_hostname.to_owned()]),
                backend_address: term_backend_address,
                tls_mode: ClientTlsMode::Terminate,
            },
            ClientServiceSettings {
                public_hostnames: Some(vec![passthrough_hostname.to_owned()]),
                backend_address: pass_backend_address,
                tls_mode: ClientTlsMode::Passthrough,
            },
        ];
        let mut tls_configs = HashMap::new();
        tls_configs.insert(terminate_hostname.to_owned(), tls_config);
        let stream_handler = TunnelConnectionStreamHandler::new(services, tls_configs);

        let fixture = TunnelConnectionFixture::connect().await?;
        let server_conn_for_term = fixture.server_connection.clone();
        let server_conn_for_pass = fixture.server_connection.clone();
        let client_connection = fixture.client_connection.clone();

        // Accept two streams concurrently on the handler side
        let handler_for_first_stream = stream_handler.clone();
        let handler_for_second_stream = stream_handler.clone();
        let first_stream_task = tokio::spawn(async move {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept first tunnel stream"))?
                .map_err(io::Error::other)?;
            handler_for_first_stream.handle(send, recv).await
        });
        let fixture2 = TunnelConnectionFixture::connect().await?;
        let client_connection2 = fixture2.client_connection.clone();
        let server_conn_for_pass2 = fixture2.server_connection.clone();
        let second_stream_task = tokio::spawn(async move {
            let (send, recv) = timeout(Duration::from_secs(1), client_connection2.accept_bi())
                .await
                .map_err(|_| timeout_error("handler should accept second tunnel stream"))?
                .map_err(io::Error::other)?;
            handler_for_second_stream.handle(send, recv).await
        });

        // Terminating stream: Visitor sends TLS → Client decrypts → backend gets plaintext
        let term_response = request_plaintext_response_over_terminated_tunnel(
            server_conn_for_term,
            &terminate_cert,
            terminate_hostname,
        )
        .await?;
        assert_eq!(term_response, *b"pong");

        // Passthrough stream: Visitor sends TLS → Client forwards raw bytes → backend decrypts
        let pass_response = request_tls_response_over_tunnel(
            server_conn_for_pass2,
            &pass_cert,
            passthrough_hostname,
        )
        .await?;
        assert_eq!(pass_response, *b"pong");

        term_backend_task
            .await
            .map_err(|e| join_error("term backend failed", e))??;
        pass_backend_task
            .await
            .map_err(|e| join_error("pass backend failed", e))??;
        first_stream_task
            .await
            .map_err(|e| join_error("first stream handler failed", e))??;
        second_stream_task
            .await
            .map_err(|e| join_error("second stream handler failed", e))??;

        // Suppress unused-variable warnings for connections not used directly
        drop(server_conn_for_pass);
        Ok(())
    }
}