eggtunnel 0.2.0

Embeddable Eggtunnel reverse-session library
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
use super::*;

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_http_connect_keeps_eggtunnel_tls_end_to_end() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"proxy-profile-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();

        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let tunnel_addr = server.local_addr();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut request = Vec::new();
                    while !request.ends_with(b"\r\n\r\n") {
                        let byte = downstream.read_u8().await.unwrap();
                        request.push(byte);
                        assert!(request.len() <= 4096, "CONNECT request too large");
                    }
                    assert!(request.starts_with(b"CONNECT "));
                    let mut upstream = TcpStream::connect(tunnel_addr).await.unwrap();
                    downstream
                        .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
                        .await
                        .unwrap();
                    tokio::io::copy_bidirectional(&mut downstream, &mut upstream)
                        .await
                        .unwrap();
                });
            }
        });

        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &format!("http://{proxy_addr}"),
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        let bind = tokio::time::timeout(Duration::from_secs(5), async {
            loop {
                if let Some((_, _, bind)) = server.handle().snapshot().effective_binds.first() {
                    break bind.clone();
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .unwrap();
        let addr = SocketAddr::V6(std::net::SocketAddrV6::new(
            std::net::Ipv6Addr::from(bind.address),
            bind.port,
            0,
            0,
        ));
        assert_eq!(
            roundtrip(addr, b"tls-through-connect").await,
            b"tls-through-connect"
        );
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_socks5_keeps_eggtunnel_tls_end_to_end() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"socks-proxy-profile-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let tunnel_addr = server.local_addr();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut greeting = [0u8; 2];
                    downstream.read_exact(&mut greeting).await.unwrap();
                    let mut methods = vec![0; greeting[1] as usize];
                    downstream.read_exact(&mut methods).await.unwrap();
                    downstream.write_all(&[5, 0]).await.unwrap();
                    let mut request = [0u8; 4];
                    downstream.read_exact(&mut request).await.unwrap();
                    assert_eq!(request[0], 5);
                    assert_eq!(request[1], 1);
                    match request[3] {
                        1 => {
                            let mut tail = [0u8; 6];
                            downstream.read_exact(&mut tail).await.unwrap();
                        }
                        3 => {
                            let length = downstream.read_u8().await.unwrap() as usize;
                            let mut tail = vec![0; length + 2];
                            downstream.read_exact(&mut tail).await.unwrap();
                        }
                        atyp => panic!("unexpected SOCKS address type: {atyp}"),
                    }
                    let mut upstream = TcpStream::connect(tunnel_addr).await.unwrap();
                    downstream
                        .write_all(&[5, 0, 0, 1, 127, 0, 0, 1, 0, 0])
                        .await
                        .unwrap();
                    tokio::io::copy_bidirectional(&mut downstream, &mut upstream)
                        .await
                        .unwrap();
                });
            }
        });
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &format!("socks5://{proxy_addr}"),
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        let bind = tokio::time::timeout(Duration::from_secs(5), async {
            loop {
                if let Some((_, _, bind)) = server.handle().snapshot().effective_binds.first() {
                    break bind.clone();
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .unwrap();
        let addr = SocketAddr::V6(std::net::SocketAddrV6::new(
            std::net::Ipv6Addr::from(bind.address),
            bind.port,
            0,
            0,
        ));
        assert_eq!(
            roundtrip(addr, b"tls-through-socks5").await,
            b"tls-through-socks5"
        );
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_proxy_refused_endpoint_terminates_without_secret_in_diagnostic() {
        let (cert, key) = certificate();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: SecretToken::new(b"refused-proxy-secret".to_vec()).unwrap(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        // Bind a listener only to immediately drop it, leaving a deterministic
        // unbound loopback address with credentials embedded in the URI.
        let unbound = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let refused_addr = unbound.local_addr().unwrap();
        drop(unbound);
        let secret_marker = "REDACTED-PROXY-CREDENTIAL";
        let uri = format!("http://user:{secret_marker}@{refused_addr}");
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token: SecretToken::new(b"refused-proxy-secret".to_vec()).unwrap(),
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &uri,
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        let snapshot = tokio::time::timeout(Duration::from_secs(10), async {
            loop {
                let snap = client.handle().snapshot();
                if snap.reconnects > 0 || snap.last_termination.is_some() {
                    break snap;
                }
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
        })
        .await
        .unwrap();
        assert!(
            snapshot.rejected_connections > 0
                || matches!(
                    snapshot.last_termination,
                    Some(TerminationCategory::Transport)
                        | Some(TerminationCategory::Authorization)
                        | Some(TerminationCategory::Authentication)
                        | Some(TerminationCategory::Timeout)
                ),
            "refused proxy must produce a bounded termination, got {snapshot:?}"
        );
        client.shutdown().await;
        server.shutdown().await;
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_proxy_handshake_timeout_tears_down_bounded() {
        let (cert, key) = certificate();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: SecretToken::new(b"timeout-proxy-secret".to_vec()).unwrap(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        // Proxy fixture accepts the TCP connection but never sends the HTTP CONNECT
        // 200 response, so the client-side handshake must time out.
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    // Hold the TCP connection open without sending a CONNECT
                    // response; the client side eventually hits its connect timeout.
                    let mut buf = [0u8; 1024];
                    loop {
                        match downstream.read(&mut buf).await {
                            Ok(0) | Err(_) => return,
                            Ok(_) => continue,
                        }
                    }
                });
            }
        });
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token: SecretToken::new(b"timeout-proxy-secret".to_vec()).unwrap(),
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &format!("http://{proxy_addr}"),
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        // Wait for the client to record a timeout termination.
        tokio::time::timeout(Duration::from_secs(15), async {
            loop {
                if client.handle().snapshot().last_termination == Some(TerminationCategory::Timeout)
                {
                    break;
                }
                tokio::time::sleep(Duration::from_millis(50)).await;
            }
        })
        .await
        .unwrap();
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_proxy_cancellation_terminates_in_progress_handshake() {
        let (cert, key) = certificate();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: SecretToken::new(b"cancel-proxy-secret".to_vec()).unwrap(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    // Drain bytes without ever responding so cancellation has an
                    // in-progress handshake to interrupt.
                    let mut buf = [0u8; 1024];
                    while downstream.read(&mut buf).await.is_ok_and(|n| n > 0) {}
                });
            }
        });
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token: SecretToken::new(b"cancel-proxy-secret".to_vec()).unwrap(),
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &format!("http://{proxy_addr}"),
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        // Cancel the client promptly. The in-progress proxy handshake task must
        // observe the cancellation and tear down without leaking resources.
        tokio::time::sleep(Duration::from_millis(50)).await;
        client.shutdown().await;
        // Verify the server side never observed an authenticated session: the
        // proxy canceled before any Eggtunnel registration completed.
        assert_eq!(server.handle().snapshot().active_sessions, 0);
        assert_eq!(server.handle().snapshot().registered_services, 0);
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_http_connect_auth_success_routes_through_proxy() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"http-auth-ok-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let tunnel_addr = server.local_addr();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut request = Vec::new();
                    while !request.ends_with(b"\r\n\r\n") {
                        let byte = downstream.read_u8().await.unwrap();
                        request.push(byte);
                        assert!(request.len() <= 4096, "CONNECT request too large");
                    }
                    assert!(request.starts_with(b"CONNECT "));
                    // Require Basic auth; reject otherwise.
                    let header = std::str::from_utf8(&request).unwrap_or_default();
                    let auth_line = header
                        .split("\r\n")
                        .find(|l| l.to_ascii_lowercase().starts_with("proxy-authorization:"))
                        .unwrap_or("");
                    let value = auth_line
                        .trim_start_matches("Proxy-Authorization:")
                        .trim_start_matches("proxy-authorization:")
                        .trim();
                    if value != "Basic YWxpY2U6czNjcmV0" {
                        downstream
                            .write_all(b"HTTP/1.1 407 Proxy Authentication Required\r\n\r\n")
                            .await
                            .unwrap();
                        return;
                    }
                    let mut upstream = TcpStream::connect(tunnel_addr).await.unwrap();
                    downstream
                        .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
                        .await
                        .unwrap();
                    tokio::io::copy_bidirectional(&mut downstream, &mut upstream)
                        .await
                        .unwrap();
                });
            }
        });
        let uri = format!("http://alice:s3cret@{proxy_addr}");
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &uri,
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        let bind = tokio::time::timeout(Duration::from_secs(5), async {
            loop {
                if let Some((_, _, bind)) = server.handle().snapshot().effective_binds.first() {
                    break bind.clone();
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .unwrap();
        let addr = SocketAddr::V6(std::net::SocketAddrV6::new(
            std::net::Ipv6Addr::from(bind.address),
            bind.port,
            0,
            0,
        ));
        assert_eq!(
            roundtrip(addr, b"tls-through-connect-auth").await,
            b"tls-through-connect-auth"
        );
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_http_connect_auth_failure_rejects_without_secret_leak() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"http-auth-fail-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut request = Vec::new();
                    while !request.ends_with(b"\r\n\r\n") {
                        let byte = downstream.read_u8().await.unwrap();
                        request.push(byte);
                        assert!(request.len() <= 4096, "CONNECT request too large");
                    }
                    downstream
                        .write_all(b"HTTP/1.1 407 Proxy Authentication Required\r\n\r\n")
                        .await
                        .unwrap();
                });
            }
        });
        let uri = format!("http://alice:s3cret@{proxy_addr}");
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &uri,
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        // The client should record an authorization failure and never register a
        // service. We poll for either a reconnection or a bounded termination; the
        // URI/credential must not appear in any client-side diagnostic.
        tokio::time::timeout(Duration::from_secs(10), async {
            loop {
                if client.handle().snapshot().reconnects > 0
                    || client.handle().snapshot().last_termination.is_some()
                {
                    break;
                }
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
        })
        .await
        .unwrap();
        assert_eq!(server.handle().snapshot().registered_services, 0);
        let snapshot_text = format!("{:?}", client.handle().snapshot());
        assert!(
            !snapshot_text.contains("s3cret"),
            "client snapshot must not contain proxy credentials: {snapshot_text}"
        );
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_socks5_auth_success_routes_through_proxy() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"socks5-auth-ok-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let tunnel_addr = server.local_addr();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut greeting = [0u8; 2];
                    let _ = downstream.read_exact(&mut greeting).await;
                    let mut methods = vec![0; greeting[1] as usize];
                    let _ = downstream.read_exact(&mut methods).await;
                    // Accept username/password auth (0x02) only.
                    assert!(
                        methods.contains(&0x02),
                        "expected SOCKS5 username/password auth method, got {methods:?}"
                    );
                    downstream.write_all(&[5, 0x02]).await.unwrap();
                    // Username/password sub-negotiation: version, username length, username,
                    // password length, password.
                    let version = downstream.read_u8().await.unwrap();
                    assert_eq!(version, 1);
                    let user_len = downstream.read_u8().await.unwrap() as usize;
                    let mut user = vec![0; user_len];
                    downstream.read_exact(&mut user).await.unwrap();
                    let pass_len = downstream.read_u8().await.unwrap() as usize;
                    let mut pass = vec![0; pass_len];
                    downstream.read_exact(&mut pass).await.unwrap();
                    if user != b"alice" || pass != b"s3cret" {
                        // 0x01 = version, 0x01 = failure
                        downstream.write_all(&[1, 1]).await.unwrap();
                        return;
                    }
                    // 0x01 = version, 0x00 = success. Send it before reading the
                    // CONNECT request so the client unblocks.
                    downstream.write_all(&[1, 0]).await.unwrap();
                    let mut request = [0u8; 4];
                    let _ = downstream.read_exact(&mut request).await;
                    assert_eq!(request[0], 5);
                    assert_eq!(request[1], 1);
                    match request[3] {
                        1 => {
                            let mut tail = [0u8; 6];
                            let _ = downstream.read_exact(&mut tail).await;
                        }
                        3 => {
                            let length = downstream.read_u8().await.unwrap() as usize;
                            let mut tail = vec![0; length + 2];
                            let _ = downstream.read_exact(&mut tail).await;
                        }
                        atyp => panic!("unexpected SOCKS address type: {atyp}"),
                    }
                    let mut upstream = TcpStream::connect(tunnel_addr).await.unwrap();
                    downstream
                        .write_all(&[5, 0, 0, 1, 127, 0, 0, 1, 0, 0])
                        .await
                        .unwrap();
                    let _ = tokio::io::copy_bidirectional(&mut downstream, &mut upstream).await;
                });
            }
        });
        let uri = format!("socks5://alice:s3cret@{proxy_addr}");
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &uri,
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        let snapshot = tokio::time::timeout(Duration::from_secs(5), async {
            loop {
                let snap = client.handle().snapshot();
                if snap.registered_services == 1
                    || snap.reconnects > 0
                    || snap.last_termination.is_some()
                {
                    break snap;
                }
                tokio::time::sleep(Duration::from_millis(50)).await;
            }
        })
        .await
        .unwrap();
        assert_eq!(
            snapshot.registered_services, 1,
            "service registration must succeed through SOCKS5 auth"
        );
        let bind = snapshot.effective_binds.first().cloned().unwrap().2;
        let addr = SocketAddr::V6(std::net::SocketAddrV6::new(
            std::net::Ipv6Addr::from(bind.address),
            bind.port,
            0,
            0,
        ));
        assert_eq!(
            roundtrip(addr, b"tls-through-socks5-auth").await,
            b"tls-through-socks5-auth"
        );
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_socks5_auth_failure_rejects_without_secret_leak() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"socks5-auth-fail-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let proxy_addr = proxy_listener.local_addr().unwrap();
        let proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = proxy_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut greeting = [0u8; 2];
                    downstream.read_exact(&mut greeting).await.unwrap();
                    let mut methods = vec![0; greeting[1] as usize];
                    downstream.read_exact(&mut methods).await.unwrap();
                    downstream.write_all(&[5, 0x02]).await.unwrap();
                    let _ = downstream.read_u8().await.unwrap();
                    let user_len = downstream.read_u8().await.unwrap() as usize;
                    let mut _user = vec![0; user_len];
                    downstream.read_exact(&mut _user).await.unwrap();
                    let pass_len = downstream.read_u8().await.unwrap() as usize;
                    let mut _pass = vec![0; pass_len];
                    downstream.read_exact(&mut _pass).await.unwrap();
                    // Reject auth: version 1, status 0x01 (failure).
                    downstream.write_all(&[1, 1]).await.unwrap();
                });
            }
        });
        let uri = format!("socks5://alice:s3cret@{proxy_addr}");
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &uri,
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        tokio::time::timeout(Duration::from_secs(10), async {
            loop {
                if client.handle().snapshot().reconnects > 0
                    || client.handle().snapshot().last_termination.is_some()
                {
                    break;
                }
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
        })
        .await
        .unwrap();
        assert_eq!(server.handle().snapshot().registered_services, 0);
        let snapshot_text = format!("{:?}", client.handle().snapshot());
        assert!(
            !snapshot_text.contains("s3cret"),
            "client snapshot must not contain proxy credentials: {snapshot_text}"
        );
        client.shutdown().await;
        server.shutdown().await;
        proxy_task.abort();
    }

    #[cfg(feature = "outbound-proxy")]
    #[tokio::test]
    async fn outbound_two_hop_socks5_then_http_connect_routes_end_to_end() {
        let (cert, key) = certificate();
        let token = SecretToken::new(b"two-hop-secret".to_vec()).unwrap();
        let server = Server::bind(ServerConfig {
            listen_addr: "127.0.0.1:0".parse().unwrap(),
            certificate_pem: cert.as_bytes().to_vec(),
            private_key_pem: key.as_bytes().to_vec(),
            token: token.clone(),
            allow_public_service_binds: false,
        })
        .await
        .unwrap();
        let tunnel_addr = server.local_addr();
        // Second hop: HTTP CONNECT proxy listening on its own port.
        let http_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let http_addr = http_listener.local_addr().unwrap();
        let http_proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = http_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut request = Vec::new();
                    while !request.ends_with(b"\r\n\r\n") {
                        let byte = downstream.read_u8().await.unwrap();
                        request.push(byte);
                        assert!(request.len() <= 4096, "CONNECT request too large");
                    }
                    assert!(request.starts_with(b"CONNECT "));
                    let mut upstream = TcpStream::connect(tunnel_addr).await.unwrap();
                    downstream
                        .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
                        .await
                        .unwrap();
                    let _ = tokio::io::copy_bidirectional(&mut downstream, &mut upstream).await;
                });
            }
        });
        // First hop: SOCKS5 proxy that performs a SOCKS5 CONNECT to the second hop
        // (HTTP CONNECT proxy) and then bridges bytes. The chain executor
        // delivers the SOCKS5 CONNECT bytes for the second hop's endpoint.
        let socks5_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let socks5_addr = socks5_listener.local_addr().unwrap();
        let http_target_addr = http_addr;
        let socks5_proxy_task = tokio::spawn(async move {
            loop {
                let Ok((mut downstream, _)) = socks5_listener.accept().await else {
                    break;
                };
                tokio::spawn(async move {
                    let mut greeting = [0u8; 2];
                    let _ = downstream.read_exact(&mut greeting).await;
                    let mut methods = vec![0; greeting[1] as usize];
                    let _ = downstream.read_exact(&mut methods).await;
                    // No-auth greeting only.
                    assert!(methods.contains(&0x00), "expected SOCKS5 no-auth method");
                    downstream.write_all(&[5, 0x00]).await.unwrap();
                    // Read SOCKS5 CONNECT request.
                    let mut request = [0u8; 4];
                    let _ = downstream.read_exact(&mut request).await;
                    assert_eq!(request[0], 5);
                    assert_eq!(request[1], 1);
                    // Consume the address payload (we do not need the host/port here
                    // because the SOCKS5 proxy simply opens a TCP connection to the
                    // configured second-hop endpoint).
                    match request[3] {
                        1 => {
                            let mut tail = [0u8; 6];
                            let _ = downstream.read_exact(&mut tail).await;
                        }
                        3 => {
                            let length = downstream.read_u8().await.unwrap() as usize;
                            let mut tail = vec![0; length + 2];
                            let _ = downstream.read_exact(&mut tail).await;
                        }
                        atyp => panic!("unexpected SOCKS address type: {atyp}"),
                    }
                    let mut upstream = TcpStream::connect(http_target_addr).await.unwrap();
                    // Reply to the client with SOCKS5 success; the HTTP CONNECT
                    // bytes will arrive over the bridged stream.
                    downstream
                        .write_all(&[5, 0, 0, 1, 127, 0, 0, 1, 0, 0])
                        .await
                        .unwrap();
                    let _ = tokio::io::copy_bidirectional(&mut downstream, &mut upstream).await;
                });
            }
        });
        let uri = format!("socks5://{socks5_addr}__http://{http_addr}");
        let client = Client::start_with_outbound_proxy_and_connector(
            ClientConfig {
                server_addr: server.local_addr().to_string(),
                tls_server_name: "localhost".into(),
                ca_pem: Some(cert.into_bytes()),
                token,
                services: vec![ClientService::new(
                    ServiceId(1),
                    ServiceName::new("direct-echo").unwrap(),
                    RequestedBind::Loopback { port: 0 },
                    TcpTarget::new("127.0.0.1", 9).unwrap(),
                )],
            },
            &uri,
            Arc::new(DuplexEchoConnector),
        )
        .await
        .unwrap();
        let bind = tokio::time::timeout(Duration::from_secs(10), async {
            loop {
                if let Some((_, _, bind)) = server.handle().snapshot().effective_binds.first() {
                    break bind.clone();
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .unwrap();
        let addr = SocketAddr::V6(std::net::SocketAddrV6::new(
            std::net::Ipv6Addr::from(bind.address),
            bind.port,
            0,
            0,
        ));
        assert_eq!(
            roundtrip(addr, b"tls-through-two-hop-chain").await,
            b"tls-through-two-hop-chain",
            "two-hop SOCKS5+HTTP CONNECT chain must deliver TLS end-to-end"
        );
        client.shutdown().await;
        server.shutdown().await;
        socks5_proxy_task.abort();
        http_proxy_task.abort();
    }