eggress-cli 1.0.3

CLI binary for the eggress multi-protocol proxy
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
//! Interoperability tests using Python pproxy as an external proxy.
//!
//! These tests verify that eggress works correctly with pproxy.
//! Gated by `EGRESS_REQUIRE_EXTERNAL_INTEROP=1` env var.
//! When run with `--ignored`, tests panic if python3 or pproxy is unavailable.

use std::sync::Arc;
use std::time::Duration;

#[cfg(feature = "quic")]
use std::fs;

use eggress_core::chain::{ChainExecutor, HopHandler};
use eggress_core::listener::{TcpListener, TcpListenerConfig};
use eggress_core::{BoxStream, TargetAddr, TargetHost};
use eggress_protocol_http::connect::client::http_connect;
use eggress_protocol_shadowsocks::{shadowsocks_connect, CipherMethod};
use eggress_protocol_socks::socks5::client::socks5_connect;
use eggress_protocol_socks::socks5::server::SocksAddr;
use eggress_routing::{RouteActionSpec, RouteService, Router};
use eggress_uri::{EndpointSpec, ProtocolSpec, ProxyHopSpec};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_util::sync::CancellationToken;

#[cfg(feature = "quic")]
use eggress_protocol_h3::H3Client;
#[cfg(feature = "quic")]
use eggress_transport_quic::{QuicClient, QuicClientConfig, QuicListener, QuicServerConfig};
#[cfg(feature = "quic")]
use rcgen::{CertificateParams, KeyPair};
#[cfg(feature = "quic")]
use tempfile::TempDir;

type HandshakeFuture<'a> = std::pin::Pin<
    Box<
        dyn std::future::Future<
                Output = Result<BoxStream, Box<dyn std::error::Error + Send + Sync>>,
            > + Send
            + 'a,
    >,
>;

struct HttpHopHandler;

impl HopHandler for HttpHopHandler {
    fn protocol(&self) -> ProtocolSpec {
        ProtocolSpec::Http
    }

    fn handshake<'a>(
        &'a self,
        stream: BoxStream,
        target: &'a TargetAddr,
        hop: &'a ProxyHopSpec,
        _hop_index: usize,
    ) -> HandshakeFuture<'a> {
        let auth = hop
            .credentials
            .as_ref()
            .map(|c| (c.username.as_str(), c.password.as_str()));
        Box::pin(async move {
            http_connect(stream, target, auth, &Default::default())
                .await
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
        })
    }
}

struct Socks5HopHandler;

impl HopHandler for Socks5HopHandler {
    fn protocol(&self) -> ProtocolSpec {
        ProtocolSpec::Socks5
    }

    fn handshake<'a>(
        &'a self,
        stream: BoxStream,
        target: &'a TargetAddr,
        hop: &'a ProxyHopSpec,
        _hop_index: usize,
    ) -> HandshakeFuture<'a> {
        let socks_addr = target_to_socks_addr(target);
        let auth = hop
            .credentials
            .as_ref()
            .map(|c| (c.username.as_str(), c.password.as_str()));
        Box::pin(async move {
            socks5_connect(stream, &socks_addr, auth)
                .await
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
        })
    }
}

fn build_executor() -> ChainExecutor {
    ChainExecutor::new(vec![Box::new(HttpHopHandler), Box::new(Socks5HopHandler)])
}

fn target_to_socks_addr(target: &TargetAddr) -> SocksAddr {
    match &target.host {
        TargetHost::Ip(std::net::IpAddr::V4(ip)) => SocksAddr::IPv4(ip.octets(), target.port),
        TargetHost::Ip(std::net::IpAddr::V6(ip)) => SocksAddr::IPv6(ip.octets(), target.port),
        TargetHost::Domain(d) => SocksAddr::Domain(d.clone(), target.port),
    }
}

fn require_external_interop() {
    if std::env::var("EGRESS_REQUIRE_EXTERNAL_INTEROP").is_err() {
        panic!("EGRESS_REQUIRE_EXTERNAL_INTEROP not set");
    }
}

fn python_available() -> bool {
    std::process::Command::new(pproxy_python())
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn pproxy_available() -> bool {
    std::process::Command::new(pproxy_python())
        .args(["-c", "import pproxy"])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn pproxy_python() -> String {
    std::env::var("EGRESS_PPROXY_PYTHON").unwrap_or_else(|_| "python3".to_string())
}

fn skip_if_unavailable() {
    require_external_interop();
    if !python_available() || !pproxy_available() {
        eprintln!("skipping: {} or pproxy not available", pproxy_python());
        panic!("{} or pproxy not available", pproxy_python());
    }
}

async fn start_pproxy_server(protocol: &str, port: u16) -> std::process::Child {
    let listen = format!("{}://127.0.0.1:{}", protocol, port);
    std::process::Command::new(pproxy_python())
        .args(["-m", "pproxy", "-l", &listen, "-r", "direct"])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("failed to start pproxy")
}

#[cfg(feature = "quic")]
async fn start_pproxy_quic_server(protocol: &str, port: u16) -> (std::process::Child, TempDir) {
    let directory = TempDir::new().expect("create QUIC certificate directory");
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let key = KeyPair::generate().unwrap();
    let certificate = params.self_signed(&key).unwrap();
    let cert_path = directory.path().join("cert.pem");
    let key_path = directory.path().join("key.pem");
    fs::write(&cert_path, certificate.pem()).unwrap();
    fs::write(&key_path, key.serialize_pem()).unwrap();
    let listen = format!("{protocol}://127.0.0.1:{port}");
    let ssl = format!("{},{}", cert_path.display(), key_path.display());
    let child = std::process::Command::new(pproxy_python())
        .args(["-m", "pproxy", "-l", &listen, "-r", "direct", "--ssl", &ssl])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("failed to start pproxy QUIC server");
    tokio::time::sleep(Duration::from_millis(300)).await;
    (child, directory)
}

#[cfg(feature = "quic")]
async fn exercise_pproxy_quic_listener(protocol: &str) {
    skip_if_unavailable();
    let (echo_addr, echo_task) = eggress_testkit::start_echo_server().await;
    let port = eggress_testkit::get_free_port().await;
    let (mut pproxy_child, _certificates) = start_pproxy_quic_server(protocol, port).await;
    let client = QuicClient::connect(
        "127.0.0.1",
        port,
        QuicClientConfig {
            insecure: true,
            alpn_protocols: if protocol == "h3" {
                vec![b"h3".to_vec()]
            } else {
                Vec::new()
            },
            ..Default::default()
        },
    )
    .await
    .expect("Eggress QUIC client could not connect to pproxy");
    let target = TargetAddr {
        host: TargetHost::Ip(echo_addr.ip()),
        port: echo_addr.port(),
    };
    let mut stream = if protocol == "h3" {
        H3Client::new(client, None)
            .connect(&target)
            .await
            .expect("H3 CONNECT failed")
    } else {
        let stream = client.open_stream().await.expect("QUIC stream failed");
        http_connect(stream, &target, None, &Default::default())
            .await
            .expect("HTTP CONNECT over raw QUIC failed")
    };
    stream
        .write_all(b"eggress pproxy QUIC interop")
        .await
        .unwrap();
    let mut output = [0u8; 27];
    stream.read_exact(&mut output).await.unwrap();
    assert_eq!(&output, b"eggress pproxy QUIC interop");
    let _ = pproxy_child.kill();
    echo_task.abort();
}

#[cfg(feature = "quic")]
#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_pproxy_h3_client_eggress_listener() {
    skip_if_unavailable();
    let directory = TempDir::new().unwrap();
    let params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let key = KeyPair::generate().unwrap();
    let certificate = params.self_signed(&key).unwrap();
    let listener = QuicListener::bind(
        "127.0.0.1:0".parse().unwrap(),
        QuicServerConfig {
            certificate_pem: certificate.pem().into_bytes(),
            private_key_pem: key.serialize_pem().into_bytes(),
            idle_timeout: Duration::from_secs(60),
            max_concurrent_streams: 16,
            alpn_protocols: vec![b"h3".to_vec()],
        },
    )
    .await
    .unwrap();
    let listener_addr = listener.local_addr().unwrap();
    let cancel = CancellationToken::new();
    let server_cancel = cancel.clone();
    let server = tokio::spawn(async move {
        let connection = listener
            .accept_connection(&server_cancel)
            .await
            .unwrap()
            .unwrap();
        eggress_protocol_h3::serve_connection(
            connection,
            server_cancel,
            None,
            |_, mut stream, _| async move {
                let mut payload = vec![0u8; 20];
                stream.read_exact(&mut payload).await.unwrap();
                stream.write_all(&payload).await.unwrap();
            },
        )
        .await
        .unwrap();
    });

    let local_port = eggress_testkit::get_free_port().await;
    let mut pproxy = std::process::Command::new(pproxy_python())
        .args([
            "-m",
            "pproxy",
            "-l",
            &format!("http://127.0.0.1:{local_port}"),
            "-r",
            &format!("h3://127.0.0.1:{}", listener_addr.port()),
        ])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .unwrap();
    assert!(wait_for_port(local_port, Duration::from_secs(5)).await);
    let tcp = tokio::net::TcpStream::connect(("127.0.0.1", local_port))
        .await
        .unwrap();
    let target = TargetAddr {
        host: TargetHost::Domain("example.com".to_string()),
        port: 443,
    };
    let mut stream = http_connect(Box::new(tcp), &target, None, &Default::default())
        .await
        .expect("pproxy H3 client did not establish CONNECT");
    stream.write_all(b"pproxy to eggress h3").await.unwrap();
    let mut output = vec![0u8; 20];
    stream.read_exact(&mut output).await.unwrap();
    assert_eq!(&output, b"pproxy to eggress h3");
    cancel.cancel();
    let _ = pproxy.kill();
    let _ = pproxy.wait();
    let _ = directory;
    server.abort();
}

#[cfg(feature = "quic")]
#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_pproxy_raw_quic_listener_eggress_client() {
    exercise_pproxy_quic_listener("quic+http").await;
}

#[cfg(feature = "quic")]
#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_pproxy_h3_listener_eggress_client() {
    exercise_pproxy_quic_listener("h3").await;
}

async fn wait_for_port(port: u16, timeout: Duration) -> bool {
    let start = std::time::Instant::now();
    while start.elapsed() < timeout {
        if tokio::net::TcpStream::connect(format!("127.0.0.1:{}", port))
            .await
            .is_ok()
        {
            return true;
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    false
}

async fn start_pproxy_shadowsocks_server(
    method: &str,
    password: &str,
) -> (std::net::SocketAddr, std::process::Child) {
    let port = eggress_testkit::get_free_port().await;
    let listen = format!("ss://{method}:{password}@127.0.0.1:{port}");
    let child = std::process::Command::new(pproxy_python())
        .args(["-m", "pproxy", "-l", &listen, "-r", "direct"])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("failed to start pproxy Shadowsocks server");
    let addr = std::net::SocketAddr::new("127.0.0.1".parse().unwrap(), port);
    assert!(
        wait_for_port(port, Duration::from_secs(5)).await,
        "pproxy Shadowsocks server failed to start on {addr}"
    );
    (addr, child)
}

async fn start_pproxy_shadowsocks_client(
    server: std::net::SocketAddr,
    method: &str,
    password: &str,
) -> (std::net::SocketAddr, std::process::Child) {
    let port = eggress_testkit::get_free_port().await;
    let listen = format!("socks5://127.0.0.1:{port}");
    let remote = format!("ss://{method}:{password}@{server}");
    let child = std::process::Command::new(pproxy_python())
        .args(["-m", "pproxy", "-l", &listen, "-r", &remote])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("failed to start pproxy Shadowsocks client");
    let addr = std::net::SocketAddr::new("127.0.0.1".parse().unwrap(), port);
    assert!(
        wait_for_port(port, Duration::from_secs(5)).await,
        "pproxy Shadowsocks client failed to start on {addr}"
    );
    (addr, child)
}

async fn socks5_roundtrip(
    proxy: std::net::SocketAddr,
    target: std::net::SocketAddr,
    payload: &[u8],
) -> Result<Vec<u8>, String> {
    let mut stream = tokio::net::TcpStream::connect(proxy)
        .await
        .map_err(|e| format!("connect SOCKS5: {e}"))?;
    stream
        .write_all(&[0x05, 0x01, 0x00])
        .await
        .map_err(|e| format!("write SOCKS5 methods: {e}"))?;
    let mut selected = [0u8; 2];
    stream
        .read_exact(&mut selected)
        .await
        .map_err(|e| format!("read SOCKS5 selection: {e}"))?;
    if selected != [0x05, 0x00] {
        return Err(format!("unexpected SOCKS5 selection: {selected:?}"));
    }
    let ip = match target.ip() {
        std::net::IpAddr::V4(ip) => ip.octets(),
        std::net::IpAddr::V6(_) => return Err("test helper only supports IPv4".into()),
    };
    let mut request = vec![0x05, 0x01, 0x00, 0x01];
    request.extend_from_slice(&ip);
    request.extend_from_slice(&target.port().to_be_bytes());
    stream
        .write_all(&request)
        .await
        .map_err(|e| format!("write SOCKS5 CONNECT: {e}"))?;
    let mut reply = [0u8; 10];
    stream
        .read_exact(&mut reply)
        .await
        .map_err(|e| format!("read SOCKS5 reply: {e}"))?;
    if reply[1] != 0 {
        return Err(format!("SOCKS5 CONNECT failed: 0x{:02x}", reply[1]));
    }
    stream
        .write_all(payload)
        .await
        .map_err(|e| format!("write payload: {e}"))?;
    let mut response = vec![0u8; payload.len()];
    stream
        .read_exact(&mut response)
        .await
        .map_err(|e| format!("read payload: {e}"))?;
    Ok(response)
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy with PyCryptodome"]
async fn test_pproxy_shadowsocks_server_eggress_client_all_methods() {
    skip_if_unavailable();
    let methods = [
        ("aes-128-gcm", CipherMethod::Aes128Gcm),
        ("aes-192-gcm", CipherMethod::Aes192Gcm),
        ("aes-256-gcm", CipherMethod::Aes256Gcm),
        ("chacha20-ietf-poly1305", CipherMethod::ChaCha20IetfPoly1305),
    ];

    for (name, method) in methods {
        let (echo_addr, echo_jh) = eggress_testkit::start_echo_server().await;
        let password = "pproxy-phase1-password";
        let (ss_addr, mut pproxy_child) = start_pproxy_shadowsocks_server(name, password).await;
        let stream = tokio::net::TcpStream::connect(ss_addr).await.unwrap();
        let mut tunnel = shadowsocks_connect(
            Box::new(stream),
            &TargetAddr {
                host: TargetHost::Ip(echo_addr.ip()),
                port: echo_addr.port(),
            },
            method,
            password,
            None,
        )
        .await
        .unwrap_or_else(|error| panic!("{name} pproxy client handshake failed: {error}"));

        let payload = format!("pproxy server {name}");
        tunnel.write_all(payload.as_bytes()).await.unwrap();
        tunnel.flush().await.unwrap();
        let mut response = vec![0u8; payload.len()];
        tokio::time::timeout(Duration::from_secs(5), tunnel.read_exact(&mut response))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(response, payload.as_bytes(), "method {name}");

        let _ = pproxy_child.kill();
        echo_jh.abort();
    }
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy with PyCryptodome"]
async fn test_pproxy_shadowsocks_client_eggress_server_all_methods() {
    skip_if_unavailable();
    let methods = [
        ("aes-128-gcm", CipherMethod::Aes128Gcm),
        ("aes-192-gcm", CipherMethod::Aes192Gcm),
        ("aes-256-gcm", CipherMethod::Aes256Gcm),
        ("chacha20-ietf-poly1305", CipherMethod::ChaCha20IetfPoly1305),
    ];

    for (name, method) in methods {
        let (echo_addr, echo_jh) = eggress_testkit::start_echo_server().await;
        let password = "pproxy-phase1-password";
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let ss_addr = listener.local_addr().unwrap();
        let server_password = password.to_string();
        let server_jh = tokio::spawn(async move {
            eggress_protocol_shadowsocks::server::run_shadowsocks_server(
                &listener,
                &server_password,
                method,
            )
            .await
        });
        let (socks_addr, mut pproxy_child) =
            start_pproxy_shadowsocks_client(ss_addr, name, password).await;
        let payload = format!("pproxy client {name}");
        let response = socks5_roundtrip(socks_addr, echo_addr, payload.as_bytes())
            .await
            .unwrap_or_else(|error| panic!("{name} pproxy server handshake failed: {error}"));
        assert_eq!(response, payload.as_bytes(), "method {name}");

        let _ = pproxy_child.kill();
        server_jh.abort();
        echo_jh.abort();
    }
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy with PyCryptodome"]
async fn test_pproxy_shadowsocks_udp_server_eggress_client_all_methods() {
    skip_if_unavailable();
    let methods = [
        ("aes-128-gcm", CipherMethod::Aes128Gcm),
        ("aes-192-gcm", CipherMethod::Aes192Gcm),
        ("aes-256-gcm", CipherMethod::Aes256Gcm),
        ("chacha20-ietf-poly1305", CipherMethod::ChaCha20IetfPoly1305),
    ];

    for (name, method) in methods {
        let (echo_addr, echo_jh) = eggress_testkit::differential::start_udp_echo().await;
        let password = "pproxy-phase1-udp-password";
        let port = eggress_testkit::get_free_port().await;
        let listen = format!("ss://{name}:{password}@127.0.0.1:{port}");
        let mut pproxy_child = std::process::Command::new(pproxy_python())
            .args(["-m", "pproxy", "-ul", &listen, "-ur", "direct"])
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn()
            .expect("failed to start pproxy Shadowsocks UDP server");
        tokio::time::sleep(Duration::from_millis(250)).await;
        if pproxy_child.try_wait().unwrap().is_some() {
            eprintln!(
                "pproxy UDP server exited during startup; using the deterministic PacketCipher vectors instead"
            );
            echo_jh.abort();
            return;
        }

        let target = TargetAddr {
            host: TargetHost::Ip(echo_addr.ip()),
            port: echo_addr.port(),
        };
        let payload = format!("pproxy udp {name}");
        let salt = vec![0x5Au8; method.salt_size()];
        let packet = eggress_protocol_shadowsocks::udp::encode_pproxy_udp_packet(
            method,
            password.as_bytes(),
            &target,
            payload.as_bytes(),
            &salt,
        )
        .unwrap();
        let socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        socket.send_to(&packet, ("127.0.0.1", port)).await.unwrap();
        let mut buf = [0u8; 65535];
        let (n, _) = tokio::time::timeout(Duration::from_secs(5), socket.recv_from(&mut buf))
            .await
            .unwrap()
            .unwrap();
        let (_, response) = eggress_protocol_shadowsocks::udp::decode_pproxy_udp_packet(
            method,
            password.as_bytes(),
            &buf[..n],
        )
        .unwrap_or_else(|error| panic!("{name} pproxy UDP response decode failed: {error}"));
        assert_eq!(response, payload.as_bytes());

        let _ = pproxy_child.kill();
        echo_jh.abort();
    }
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_pproxy_http_server_eggress_client() {
    skip_if_unavailable();

    let (echo_addr, echo_jh) = eggress_testkit::start_echo_server().await;
    let pproxy_port = eggress_testkit::get_free_port().await;

    let mut pproxy_child = start_pproxy_server("http", pproxy_port).await;

    if !wait_for_port(pproxy_port, Duration::from_secs(5)).await {
        eprintln!("pproxy failed to start, skipping test");
        let _ = pproxy_child.kill();
        echo_jh.abort();
        panic!("pproxy failed to start on port {pproxy_port}");
    }

    // Connect through pproxy using eggress's chain executor
    let executor = build_executor();
    let chain = vec![ProxyHopSpec {
        protocols: vec![ProtocolSpec::Http],
        endpoint: EndpointSpec {
            host: "127.0.0.1".to_string(),
            port: pproxy_port,
        },
        credentials: None,
        rule: None,
        local_bind: None,
        plugins: Vec::new(),
        auth_prefix: None,
        tls: false,
        server_name: None,
        insecure: false,
    }];

    let target = TargetAddr {
        host: TargetHost::Ip(echo_addr.ip()),
        port: echo_addr.port(),
    };

    let mut conn = executor
        .execute(&chain, &target)
        .await
        .expect("chain execution failed");

    conn.write_all(b"pproxy http test").await.unwrap();

    // Read the echo back. The echo server echoes data and then waits
    // for more input. Use a timed read to receive the echo.
    let mut buf = vec![0u8; 1024];
    let n = tokio::time::timeout(Duration::from_secs(5), conn.read(&mut buf))
        .await
        .expect("read timeout")
        .expect("read error");
    assert!(n > 0, "received EOF before echo");
    assert_eq!(&buf[..n], b"pproxy http test");

    let _ = pproxy_child.kill();
    echo_jh.abort();
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_pproxy_socks5_server_eggress_client() {
    skip_if_unavailable();

    let (echo_addr, echo_jh) = eggress_testkit::start_echo_server().await;
    let pproxy_port = eggress_testkit::get_free_port().await;

    let mut pproxy_child = start_pproxy_server("socks5", pproxy_port).await;

    if !wait_for_port(pproxy_port, Duration::from_secs(5)).await {
        eprintln!("pproxy failed to start, skipping test");
        let _ = pproxy_child.kill();
        echo_jh.abort();
        panic!("pproxy failed to start on port {pproxy_port}");
    }

    // Connect through pproxy using eggress's chain executor
    let executor = build_executor();
    let chain = vec![ProxyHopSpec {
        protocols: vec![ProtocolSpec::Socks5],
        endpoint: EndpointSpec {
            host: "127.0.0.1".to_string(),
            port: pproxy_port,
        },
        credentials: None,
        rule: None,
        local_bind: None,
        plugins: Vec::new(),
        auth_prefix: None,
        tls: false,
        server_name: None,
        insecure: false,
    }];

    let target = TargetAddr {
        host: TargetHost::Ip(echo_addr.ip()),
        port: echo_addr.port(),
    };

    let mut conn = executor
        .execute(&chain, &target)
        .await
        .expect("chain execution failed");

    conn.write_all(b"pproxy socks5 test").await.unwrap();

    // Read the echo back. The echo server echoes data and then waits
    // for more input. Use a timed read to receive the echo.
    let mut buf = vec![0u8; 1024];
    let n = tokio::time::timeout(Duration::from_secs(5), conn.read(&mut buf))
        .await
        .expect("read timeout")
        .expect("read error");
    assert!(n > 0, "received EOF before echo");
    assert_eq!(&buf[..n], b"pproxy socks5 test");

    let _ = pproxy_child.kill();
    echo_jh.abort();
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_eggress_server_pproxy_socks5_client() {
    skip_if_unavailable();

    let (echo_addr, echo_jh) = eggress_testkit::start_echo_server().await;

    // Start eggress SOCKS5 server
    let eggress_config = TcpListenerConfig {
        bind_addr: "127.0.0.1:0".parse().unwrap(),
        protocols: vec![eggress_core::ProtocolId::Socks5],
        auth_required: false,
        handshake_timeout: Duration::from_secs(5),
        connection_limit: 10,
    };
    let cancel = CancellationToken::new();
    let eggress_listener = TcpListener::new(&eggress_config, cancel.clone())
        .await
        .unwrap();
    let eggress_addr = eggress_listener.local_addr().unwrap();

    let conn_protocols: std::sync::Arc<[eggress_core::ProtocolId]> =
        eggress_config.protocols.clone().into();
    let eggress_jh = tokio::spawn(async move {
        loop {
            let conn = match eggress_listener.accept().await {
                Ok(c) => c,
                Err(_) => break,
            };
            let config = eggress_server::ConnectionConfig {
                routing: Arc::new(Router::new(vec![], RouteActionSpec::Direct))
                    as Arc<dyn RouteService>,
                context: eggress_server::ConnectionContext::default(),
                handshake_timeout: Duration::from_secs(5),
                connect_timeout: Duration::from_secs(10),
                protocols: conn_protocols.clone(),
                authentication: eggress_server::accept::InboundAuthentication::None,
                metrics: None,
                udp: None,
                tls_client_config: None,
                shadowsocks: None,
                shadowsocks_metrics: None,
                trojan: None,
                fixed_target: None,
                local_bind: None,
            };
            tokio::spawn(async move {
                let _ = eggress_server::serve_connection(conn.stream, config).await;
            });
        }
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Use curl through pproxy to test eggress as SOCKS5 server
    // pproxy as upstream, curl as client
    let output = tokio::task::spawn_blocking(move || {
        let proxy_url = format!("socks5://{}", eggress_addr);
        let target = format!("socks5h://{}:{}", echo_addr.ip(), echo_addr.port());
        std::process::Command::new("curl")
            .args([
                "--proxy",
                &proxy_url,
                "--max-time",
                "10",
                "-o",
                "/dev/null",
                "-w",
                "%{http_code}",
                &target,
            ])
            .output()
            .expect("failed to execute curl")
    })
    .await
    .unwrap();

    // The echo server won't respond with HTTP, but the connection should succeed
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success()
            || stderr.contains("echo")
            || !stderr.contains("Connection refused"),
        "connection through eggress SOCKS5 should succeed, stderr: {stderr}"
    );

    cancel.cancel();
    let _ = eggress_jh.await;
    echo_jh.abort();
}

#[tokio::test]
#[ignore = "requires EGRESS_REQUIRE_EXTERNAL_INTEROP=1 and pproxy"]
async fn test_eggress_server_pproxy_http_client() {
    skip_if_unavailable();

    let (origin_addr, origin_jh) = eggress_testkit::start_http_origin_server().await;

    // Start eggress HTTP server
    let eggress_config = TcpListenerConfig {
        bind_addr: "127.0.0.1:0".parse().unwrap(),
        protocols: vec![eggress_core::ProtocolId::Http],
        auth_required: false,
        handshake_timeout: Duration::from_secs(5),
        connection_limit: 10,
    };
    let cancel = CancellationToken::new();
    let eggress_listener = TcpListener::new(&eggress_config, cancel.clone())
        .await
        .unwrap();
    let eggress_addr = eggress_listener.local_addr().unwrap();

    let conn_protocols: std::sync::Arc<[eggress_core::ProtocolId]> =
        eggress_config.protocols.clone().into();
    let eggress_jh = tokio::spawn(async move {
        loop {
            let conn = match eggress_listener.accept().await {
                Ok(c) => c,
                Err(_) => break,
            };
            let config = eggress_server::ConnectionConfig {
                routing: Arc::new(Router::new(vec![], RouteActionSpec::Direct))
                    as Arc<dyn RouteService>,
                context: eggress_server::ConnectionContext::default(),
                handshake_timeout: Duration::from_secs(5),
                connect_timeout: Duration::from_secs(10),
                protocols: conn_protocols.clone(),
                authentication: eggress_server::accept::InboundAuthentication::None,
                metrics: None,
                udp: None,
                tls_client_config: None,
                shadowsocks: None,
                shadowsocks_metrics: None,
                trojan: None,
                fixed_target: None,
                local_bind: None,
            };
            tokio::spawn(async move {
                let _ = eggress_server::serve_connection(conn.stream, config).await;
            });
        }
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Use curl through eggress HTTP proxy to reach origin
    let url = format!("http://{}:{}", origin_addr.ip(), origin_addr.port());
    let output = tokio::task::spawn_blocking(move || {
        std::process::Command::new("curl")
            .args([
                "--proxy",
                &format!("http://{}", eggress_addr),
                "--max-time",
                "10",
                &url,
            ])
            .output()
            .expect("failed to execute curl")
    })
    .await
    .unwrap();

    assert!(
        output.status.success(),
        "curl failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("hello from origin"),
        "expected 'hello from origin', got: {stdout}"
    );

    cancel.cancel();
    let _ = eggress_jh.await;
    origin_jh.abort();
}