eggress-runtime 1.0.3

Service supervisor and composition layer for eggress
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
//! Reverse proxy interop tests against the pproxy 2.7.9 oracle.
//!
//! Two flavours of test exist:
//!
//! * **Un-gated** (`#[test]`): pure Rust end-to-end tests that exercise
//!   the eggress native `ReverseServer`/`ReverseClient` and the bounded
//!   `PproxyBackwardClient`/`PproxyBackwardServer` adapters against a
//!   loopback echo fixture. They run in any environment and verify the
//!   wire format, reconnect logic, framing mode, and lifecycle behave
//!   as documented.
//!
//! * **Gated** (`#[ignore]`): require the canonical pproxy 2.7.9 oracle
//!   interpreter at `$EGRESS_PPROXY_PYTHON` (or `$EGRESS_ORACLE_PYTHON`),
//!   and the `EGRESS_REQUIRE_REVERSE_INTEROP=1` gate. Run with:
//!   ```text
//!   EGRESS_PPROXY_PYTHON=/path/to/pproxy-2.7.9-oracle/bin/python \
//!     EGRESS_REQUIRE_REVERSE_INTEROP=1 \
//!     cargo test -p eggress-runtime --test reverse_interop -- --ignored --test-threads=1
//!   ```
//!
//! The gated tests exercise real payload-level interop against the
//! pinned pproxy interpreter in both directions: pproxy-as-worker
//! dialing an Eggress pproxy-compatibility listener and
//! Eggress-as-worker dialing a pproxy backward listener. Each test
//! captures the pproxy child via a RAII guard, allocates ports from the
//! shared testkit helper, and verifies byte-for-byte equality through
//! a local echo target. No port-range scanning, no path lookups; each
//! pproxy child is bound to the explicit port the test chose.
//!
//! The capability manifest points at this file as the source of
//! `strict_phase = "5"` reverse/backward evidence for the
//! `pproxy_capability_manifest.toml` record set.

#![allow(clippy::zombie_processes)] // Gated tests intentionally spawn pproxy and kill it via drop.

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

use eggress_protocol_reverse::client::{ReverseClient, ReverseClientConfig, TargetResolution};
use eggress_protocol_reverse::compat_pproxy::{
    PproxyBackwardClient, PproxyBackwardClientConfig, PproxyBackwardFraming, PproxyBackwardServer,
    PproxyBackwardServerConfig,
};
use eggress_protocol_reverse::metrics::ReverseMetrics;
use eggress_protocol_reverse::server::{ReverseServer, ReverseServerConfig};
use eggress_testkit::differential::{
    assert_port_ready, find_oracle_python, ProcessGuard, PINNED_PPROXY_VERSION,
};
use eggress_testkit::fixtures::TcpEchoServer;
use eggress_uri::ProxyChainSpec;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

fn require_reverse_interop() -> String {
    if std::env::var("EGRESS_REQUIRE_REVERSE_INTEROP").is_err() {
        panic!(
            "EGRESS_REQUIRE_REVERSE_INTEROP not set; gated reverse interop test requires it.\n\
             Run with: EGRESS_REQUIRE_REVERSE_INTEROP=1 cargo test ... -- --ignored"
        );
    }
    let python = find_oracle_python(true);
    // find_oracle_python(true) already panics on resolution failure, so we
    // can safely use the returned path here.
    if std::process::Command::new(&python)
        .args(["-c", "import pproxy; print(pproxy.__file__)"])
        .output()
        .map(|o| !o.status.success())
        .unwrap_or(true)
    {
        panic!(
            "no pproxy=={} oracle interpreter available; set {} or {}",
            PINNED_PPROXY_VERSION,
            eggress_testkit::differential::ORACLE_PYTHON_VAR,
            eggress_testkit::differential::LEGACY_PYTHON_VAR,
        );
    }
    python
}

// ===========================================================================
// Un-gated tests — eggress reverse wire format and lifecycle
// ===========================================================================

#[tokio::test]
async fn reverse_eggress_self_interop_loopback() {
    let control_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let control_addr = control_listener.local_addr().unwrap();
    drop(control_listener);

    let target_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let target_addr = target_listener.local_addr().unwrap();

    let echo_task = tokio::spawn(async move {
        if let Ok((mut stream, _)) = target_listener.accept().await {
            let mut buf = [0u8; 64];
            if let Ok(n) = stream.read(&mut buf).await {
                let _ = stream.write_all(&buf[..n]).await;
            }
        }
    });

    let server_config = ReverseServerConfig {
        control_bind: control_addr,
        ..Default::default()
    };
    let mut server = ReverseServer::new(server_config);
    let server_metrics = Arc::new(ReverseMetrics::new());
    server.set_metrics(server_metrics.clone());
    let server_cancel = server.cancel_token();
    let server_handle = tokio::spawn(async move {
        server.run().await.unwrap();
    });

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

    let client_config = ReverseClientConfig {
        server_addr: control_addr,
        reconnect_initial_ms: 50,
        reconnect_max_ms: 100,
        ..Default::default()
    };
    let mut client = ReverseClient::new(client_config);
    let client_metrics = Arc::new(ReverseMetrics::new());
    client.set_metrics(client_metrics.clone());
    client.set_resolver(Arc::new(StaticTargetResolver::new(
        target_addr.ip().to_string(),
        target_addr.port(),
    )));
    let client_cancel = client.cancel_token();
    let client_handle = tokio::spawn(async move {
        let _ = client.run().await;
    });

    tokio::time::sleep(Duration::from_secs(2)).await;

    let server_snap = server_metrics.snapshot();
    assert!(
        server_snap.control_connections_accepted_total >= 1
            || server_snap.control_connections_rejected_total >= 1,
        "expected server to record a control connection: {:?}",
        server_snap,
    );

    let client_snap = client_metrics.snapshot();
    assert!(
        client_snap.control_reconnects_total >= 1 || client_snap.streams_opened_total >= 1,
        "expected client to record a control connection attempt: {:?}",
        client_snap
    );

    client_cancel.cancel();
    server_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), client_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(1), echo_task).await;
}

#[tokio::test]
async fn reverse_payload_byte_equality_eggress_loopback() {
    let control_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let control_addr = control_listener.local_addr().unwrap();
    drop(control_listener);

    let external_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let external_addr = external_listener.local_addr().unwrap();
    drop(external_listener);

    let target_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let target_addr = target_listener.local_addr().unwrap();

    let echo_task = tokio::spawn(async move {
        loop {
            let (mut stream, _) = match target_listener.accept().await {
                Ok(s) => s,
                Err(_) => return,
            };
            tokio::spawn(async move {
                let mut buf = vec![0u8; 4096];
                loop {
                    match stream.read(&mut buf).await {
                        Ok(0) => return,
                        Ok(n) => {
                            if stream.write_all(&buf[..n]).await.is_err() {
                                return;
                            }
                        }
                        Err(_) => return,
                    }
                }
            });
        }
    });

    let server = ReverseServer::new(ReverseServerConfig {
        control_bind: control_addr,
        external_bind: Some(external_addr),
        ..Default::default()
    });
    let server_cancel = server.cancel_token();
    let server_handle = tokio::spawn(async move {
        let _ = server.run().await;
    });

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

    let mut client = ReverseClient::new(ReverseClientConfig {
        server_addr: control_addr,
        reconnect_initial_ms: 50,
        reconnect_max_ms: 100,
        ..Default::default()
    });
    client.set_resolver(Arc::new(StaticTargetResolver::new(
        target_addr.ip().to_string(),
        target_addr.port(),
    )));
    let client_cancel = client.cancel_token();
    let client_handle = tokio::spawn(async move {
        let _ = client.run().await;
    });

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

    let mut stream = tokio::net::TcpStream::connect(external_addr).await.unwrap();
    let payload: Vec<u8> = (0..=255u8).cycle().take(1024).collect();
    stream.write_all(&payload).await.unwrap();

    let mut received = vec![0u8; payload.len()];
    let mut total_read = 0;
    tokio::time::timeout(Duration::from_secs(5), async {
        while total_read < received.len() {
            match stream.read(&mut received[total_read..]).await {
                Ok(0) => break,
                Ok(n) => total_read += n,
                Err(e) => return Err(e),
            }
        }
        Ok::<(), std::io::Error>(())
    })
    .await
    .expect("read timed out")
    .expect("read failed");
    received.truncate(total_read);

    drop(stream);

    assert_eq!(
        received,
        payload,
        "echo server returned different bytes than sent ({} bytes sent, {} received)",
        payload.len(),
        received.len(),
    );

    client_cancel.cancel();
    server_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), client_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(1), echo_task).await;
}

#[tokio::test]
async fn reverse_redacts_credentials_in_logs() {
    let auth = "admin:super-secret-p@ssw0rd";
    let redacted = eggress_protocol_reverse::redact_auth(auth);
    assert!(!redacted.contains("super-secret-p@ssw0rd"));
    assert!(redacted.contains("admin"));
    assert!(redacted.contains("****"));
}

#[tokio::test]
async fn reverse_compat_pproxy_raw_byte_pipe_relays_byte_for_byte() {
    let echo = TcpEchoServer::start().await;
    let control_addr = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .unwrap()
        .local_addr()
        .unwrap();
    let external_addr = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .unwrap()
        .local_addr()
        .unwrap();
    let server = PproxyBackwardServer::new(PproxyBackwardServerConfig {
        control_bind: control_addr,
        external_bind: external_addr,
        auth: b"alice:wonderland".to_vec(),
        read_timeout_ms: 2_000,
        client_framing: PproxyBackwardFraming::Raw,
        ..Default::default()
    });
    let server_cancel = server.cancel_token();
    let server_handle = tokio::spawn(server.run());

    let client = PproxyBackwardClient::new(
        PproxyBackwardClientConfig {
            server_addr: control_addr,
            auth: b"alice:wonderland".to_vec(),
            reconnect_initial_ms: 1,
            reconnect_max_ms: 5,
            server_framing: PproxyBackwardFraming::Raw,
            ..Default::default()
        },
        Arc::new(FixedAddrResolver::new(echo.addr())),
    );
    let client_cancel = client.cancel_token();
    let client_handle = tokio::spawn(async move { client.run().await });

    let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
    let mut external = loop {
        match tokio::net::TcpStream::connect(external_addr).await {
            Ok(stream) => break stream,
            Err(error) if tokio::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(5)).await;
                let _ = error;
            }
            Err(error) => panic!("backward external listener did not start: {error}"),
        }
    };

    let payload: Vec<u8> = (0..=255u8).cycle().take(1024).collect();
    external.write_all(&payload).await.unwrap();

    let mut received = Vec::with_capacity(payload.len());
    let read_fut = async {
        let mut chunk = vec![0u8; payload.len()];
        external.read_exact(&mut chunk).await?;
        Ok::<_, std::io::Error>(chunk)
    };
    let chunk = tokio::time::timeout(Duration::from_secs(3), read_fut)
        .await
        .expect("read timed out")
        .expect("read failed");
    received.extend_from_slice(&chunk);

    assert_eq!(
        received, payload,
        "echo server returned different bytes than sent"
    );

    client_cancel.cancel();
    server_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), client_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

#[tokio::test]
async fn reverse_compat_pproxy_socks5_server_mode_negotiates_and_relays() {
    // Verifies the new `Socks5` worker framing matches a SOCKS5 server
    // pattern end-to-end against the eggress compatibility server, without
    // needing the pproxy oracle. The worker runs SOCKS5 server mode and the
    // server speaks SOCKS5 client framing toward it, then the server pairs
    // the worker's channel with a fresh external client.
    let local_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let local_addr = local_listener.local_addr().unwrap();
    let echo_task = tokio::spawn(async move {
        if let Ok((mut s, _)) = local_listener.accept().await {
            let mut buf = vec![0u8; 4096];
            loop {
                match s.read(&mut buf).await {
                    Ok(0) => return,
                    Ok(n) => {
                        if s.write_all(&buf[..n]).await.is_err() {
                            return;
                        }
                    }
                    Err(_) => return,
                }
            }
        }
    });

    let control_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let control_addr = control_listener.local_addr().unwrap();
    drop(control_listener);
    let external_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let external_addr = external_listener.local_addr().unwrap();
    drop(external_listener);

    let server = PproxyBackwardServer::new(PproxyBackwardServerConfig {
        control_bind: control_addr,
        external_bind: external_addr,
        auth: Vec::new(),
        read_timeout_ms: 2_000,
        client_framing: PproxyBackwardFraming::Socks5,
        socks5_target: Some((local_addr.ip().to_string(), local_addr.port())),
        ..Default::default()
    });
    let server_cancel = server.cancel_token();
    let server_handle = tokio::spawn(server.run());

    let worker = PproxyBackwardClient::new(
        PproxyBackwardClientConfig {
            server_addr: control_addr,
            server_chain: None,
            auth: b"".to_vec(),
            reconnect_initial_ms: 1,
            reconnect_max_ms: 5,
            server_framing: PproxyBackwardFraming::Socks5,
            ..Default::default()
        },
        Arc::new(NoopResolver),
    );
    let worker_cancel = worker.cancel_token();
    let worker_handle = tokio::spawn(async move { worker.run().await });

    let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
    let mut external = loop {
        match tokio::net::TcpStream::connect(external_addr).await {
            Ok(stream) => break stream,
            Err(error) if tokio::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(5)).await;
                let _ = error;
            }
            Err(error) => panic!("backward external listener did not start: {error}"),
        }
    };

    let payload: Vec<u8> = (0..=255u8).cycle().take(512).collect();
    external.write_all(&payload).await.unwrap();

    let mut received = Vec::with_capacity(payload.len());
    let read_fut = async {
        let mut chunk = vec![0u8; payload.len()];
        external.read_exact(&mut chunk).await?;
        Ok::<_, std::io::Error>(chunk)
    };
    let chunk = tokio::time::timeout(Duration::from_secs(3), read_fut)
        .await
        .expect("read timed out")
        .expect("read failed");
    received.extend_from_slice(&chunk);

    assert_eq!(
        received, payload,
        "SOCKS5 worker did not relay payload byte-for-byte"
    );

    worker_cancel.cancel();
    server_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), worker_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
    let _ = tokio::time::timeout(Duration::from_secs(1), echo_task).await;
}

// --- Static resolvers used by the un-gated tests ---

struct StaticTargetResolver {
    host: String,
    port: u16,
}

impl StaticTargetResolver {
    fn new(host: String, port: u16) -> Self {
        Self { host, port }
    }
}

impl eggress_protocol_reverse::client::TargetResolver for StaticTargetResolver {
    fn resolve(&self) -> TargetResolution {
        TargetResolution::Connect {
            host: self.host.clone(),
            port: self.port,
        }
    }
}

struct FixedAddrResolver(SocketAddr);

impl FixedAddrResolver {
    fn new(addr: SocketAddr) -> Self {
        Self(addr)
    }
}

impl eggress_protocol_reverse::client::TargetResolver for FixedAddrResolver {
    fn resolve(&self) -> TargetResolution {
        TargetResolution::Connect {
            host: self.0.ip().to_string(),
            port: self.0.port(),
        }
    }
}

struct NoopResolver;

impl eggress_protocol_reverse::client::TargetResolver for NoopResolver {
    fn resolve(&self) -> TargetResolution {
        TargetResolution::Reject {
            reason: "socks5 mode derives target from CONNECT".into(),
        }
    }
}

// ===========================================================================
// Gated tests — real pproxy 2.7.9 oracle interop
// ===========================================================================
//
// Each test below:
//   * Resolves the canonical pproxy 2.7.9 oracle interpreter with
//     `find_oracle_python(true)`. This panics when the gate is enabled
//     but no validated interpreter is available, instead of silently
//     falling back to `python3`.
//   * Allocates explicit loopback ports through `eggress_testkit::get_free_port`.
//   * Spawns the oracle via `$python -m pproxy ...` with the chosen
//     ports. The child is captured in a `ProcessGuard` so a test
//     failure or normal completion always releases it deterministically.
//   * Verifies byte-for-byte equality through a local TCP echo fixture.
//   * Keeps cancellation tokens live for the Eggress worker and the
//     listener so no detached reconnect task leaks past test end.

/// Scenario A: pproxy backward worker -> Eggress pproxy-compatibility
/// listening endpoint, payload-level interop.
///
/// pproxy runs `-l socks5+in://control -r direct` to dial into the
/// Eggress control port and act as the SOCKS5 server on the channel.
/// Eggress runs `PproxyBackwardServer` with `Socks5` framing, drives the
/// SOCKS5 hello + CONNECT handshake against the pproxy worker, and pins
/// the relayed channel to the local echo target. After the channel is
/// established the external client writes raw payload and observes
/// byte-for-byte echo through the pproxy worker side of the channel.
///
/// Wire-compatibility rationale: pproxy 2.7.9's `ProxyBackward.start_server_run`
/// reads exactly one byte after the auth handshake to decide whether to
/// queue the channel. The Socks5 framing's hello `[0x05, 0x01, 0x00]`
/// makes that first non-zero byte the SOCKS5 version marker, satisfying
/// both the channel-alive signal and pproxy's `Socks5.guess`/`accept`
/// protocol detection in a single shot. A pure `Raw` byte-pipe channel
/// has no byte to send and pproxy times out at `SOCKET_TIMEOUT=60s`.
///
/// The backward URL fragment is intentionally empty: pproxy 2.7.9 ties
/// the user list directly to the URI fragment, and the `start_server_run`
/// auth-byte exchange does not call `set_authed` on the AutoTable. With
/// `users` set, pproxy's `Socks5.accept` requires either `0x02`
/// (USERNAME/PASSWORD) in the hello methods list or a pre-authenticated
/// `user` from the authtable, neither of which is satisfied by the eggress
/// `proxy_socks5_setup` no-auth hello. Leaving the fragment empty keeps
/// the SOCKS5 negotiation in no-auth mode and is faithful to what the
/// canonical pproxy oracle accepts from a `+in` worker.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn gated_pproxy_backward_worker_to_eggress_listener_payload() {
    let python = require_reverse_interop();
    let echo = TcpEchoServer::start().await;
    let control_port = eggress_testkit::get_free_port().await;
    let control_addr: SocketAddr = format!("127.0.0.1:{control_port}").parse().unwrap();
    let external_port = eggress_testkit::get_free_port().await;
    let external_addr: SocketAddr = format!("127.0.0.1:{external_port}").parse().unwrap();
    let keepalive_port = eggress_testkit::get_free_port().await;

    let auth = "";

    let server = PproxyBackwardServer::new(PproxyBackwardServerConfig {
        control_bind: control_addr,
        external_bind: external_addr,
        auth: Vec::new(),
        read_timeout_ms: 6_000,
        client_framing: PproxyBackwardFraming::Socks5,
        socks5_target: Some((echo.addr().ip().to_string(), echo.addr().port())),
        ..Default::default()
    });
    let server_cancel = server.cancel_token();
    let server_handle = tokio::spawn(server.run());

    let mut pproxy_guard = spawn_pproxy_worker(&python, control_port, auth, keepalive_port);
    assert_port_ready(control_port, Duration::from_secs(5)).await;

    // Let the pproxy worker dial control_addr, send auth, and complete
    // the SOCKS5 hello + CONNECT handshake so the channel is queued and
    // ready to be paired with an external client.
    tokio::time::sleep(Duration::from_millis(400)).await;

    let payload: Vec<u8> = (0..=255u8).cycle().take(1024).collect();
    let result = drive_external_raw_payload_to_pproxy_worker(external_addr, &payload).await;
    if result.is_err() {
        let stderr = pproxy_guard.drain_stderr();
        eprintln!("DBG pproxy stderr: {stderr}");
    }
    result.expect("payload roundtrip failed");

    server_cancel.cancel();
    pproxy_guard.kill();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Scenario B: Eggress backward worker -> pproxy listener endpoint,
/// payload-level interop.
///
/// pproxy runs `-l http://:external -r socks5+in://control#auth`. The
/// external HTTP client sends `CONNECT echo_addr`, pproxy obtains the
/// target from the request, then performs the SOCKS5 negotiation with
/// the Eggress worker via the channel. Eggress runs
/// `PproxyBackwardClient` with `Socks5` framing so it acts as the
/// SOCKS5 server that pproxy's listener expects after auth.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn gated_eggress_backward_worker_to_pproxy_listener_payload() {
    let python = require_reverse_interop();
    let echo = TcpEchoServer::start().await;
    let control_port = eggress_testkit::get_free_port().await;
    let control_addr: SocketAddr = format!("127.0.0.1:{control_port}").parse().unwrap();
    let external_port = eggress_testkit::get_free_port().await;
    let external_addr: SocketAddr = format!("127.0.0.1:{external_port}").parse().unwrap();

    let auth = "";

    let _pproxy_guard = spawn_pproxy_listener(&python, control_port, external_port, auth);
    assert_port_ready(external_port, Duration::from_secs(5)).await;
    let _ = control_addr; // re-used by the spawned Socks5 worker below

    let worker = PproxyBackwardClient::new(
        PproxyBackwardClientConfig {
            server_addr: control_addr,
            server_chain: None,
            auth: Vec::new(),
            reconnect_initial_ms: 50,
            reconnect_max_ms: 200,
            read_timeout_ms: 6_000,
            target_connect_timeout_ms: 4_000,
            server_framing: PproxyBackwardFraming::Socks5,
        },
        Arc::new(NoopResolver),
    );
    let worker_cancel = worker.cancel_token();
    let worker_handle = tokio::spawn(async move { worker.run().await });

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

    let payload: Vec<u8> = (0..=255u8).cycle().take(1024).collect();
    drive_external_http_connect_to_pproxy_listener(external_addr, echo.addr(), &payload)
        .await
        .expect("payload roundtrip failed");

    eprintln!(
        "DBG test B: echo connection_count = {}",
        echo.connection_count()
            .load(std::sync::atomic::Ordering::Relaxed)
    );
    worker_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), worker_handle).await;
}

/// Scenario C: Eggress backward worker reaches a pproxy listener through
/// one HTTP CONNECT jump. The jump uses a local `HttpConnectUpstream`
/// fixture so no public internet is involved. End-to-end byte equality is
/// verified through the HTTP-jumped channel.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn gated_eggress_backward_worker_pproxy_http_jump_payload() {
    use eggress_testkit::fixtures::HttpConnectUpstream;
    use eggress_uri::{EndpointSpec, ProtocolSpec, ProxyHopSpec};

    let python = require_reverse_interop();
    let echo = TcpEchoServer::start().await;

    let jump = HttpConnectUpstream::start().await;
    let control_port = eggress_testkit::get_free_port().await;
    let control_addr: SocketAddr = format!("127.0.0.1:{control_port}").parse().unwrap();
    let external_port = eggress_testkit::get_free_port().await;
    let external_addr: SocketAddr = format!("127.0.0.1:{external_port}").parse().unwrap();

    let auth = "";
    let _pproxy_guard = spawn_pproxy_listener(&python, control_port, external_port, auth);
    assert_port_ready(external_port, Duration::from_secs(5)).await;

    // chain order: [target (control), jump (HTTP CONNECT)] — see
    // PproxyBackwardClient::connect_control
    let chain = ProxyChainSpec {
        hops: vec![
            ProxyHopSpec {
                protocols: vec![ProtocolSpec::Socks5],
                endpoint: EndpointSpec {
                    host: control_addr.ip().to_string(),
                    port: control_addr.port(),
                },
                credentials: None,
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            },
            ProxyHopSpec {
                protocols: vec![ProtocolSpec::Http],
                endpoint: EndpointSpec {
                    host: jump.addr().ip().to_string(),
                    port: jump.addr().port(),
                },
                credentials: None,
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            },
        ],
    };

    let worker = PproxyBackwardClient::new(
        PproxyBackwardClientConfig {
            server_addr: control_addr,
            server_chain: Some(chain),
            auth: Vec::new(),
            reconnect_initial_ms: 50,
            reconnect_max_ms: 200,
            read_timeout_ms: 6_000,
            target_connect_timeout_ms: 4_000,
            server_framing: PproxyBackwardFraming::Socks5,
        },
        Arc::new(NoopResolver),
    );
    let worker_cancel = worker.cancel_token();
    let worker_handle = tokio::spawn(async move { worker.run().await });

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

    let payload: Vec<u8> = (0..=255u8).cycle().take(1024).collect();
    drive_external_http_connect_to_pproxy_listener(external_addr, echo.addr(), &payload)
        .await
        .expect("payload roundtrip failed");

    worker_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), worker_handle).await;
}

/// Scenario D: forced disconnect on the pproxy listener side, followed by
/// Eggress worker reconnect + second payload roundtrip.
///
/// The pproxy listener is started, relays a first payload, then is killed.
/// Within the bounded retry window the Eggress worker reconnects, the
/// listener is restarted on the same ports, and a second payload
/// roundtrip succeeds. Cancellation tokens are kept alive across the
/// relaunch so no detached reconnect task leaks.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn gated_eggress_backward_worker_pproxy_disconnect_reconnect() {
    let python = require_reverse_interop();
    let echo = TcpEchoServer::start().await;

    let control_port = eggress_testkit::get_free_port().await;
    let control_addr: SocketAddr = format!("127.0.0.1:{control_port}").parse().unwrap();
    let external_port = eggress_testkit::get_free_port().await;
    let external_addr: SocketAddr = format!("127.0.0.1:{external_port}").parse().unwrap();
    let auth = "";

    let mut pproxy_guard = spawn_pproxy_listener(&python, control_port, external_port, auth);
    assert_port_ready(external_port, Duration::from_secs(5)).await;

    let worker = PproxyBackwardClient::new(
        PproxyBackwardClientConfig {
            server_addr: control_addr,
            server_chain: None,
            auth: Vec::new(),
            reconnect_initial_ms: 50,
            reconnect_max_ms: 200,
            read_timeout_ms: 6_000,
            target_connect_timeout_ms: 4_000,
            server_framing: PproxyBackwardFraming::Socks5,
        },
        Arc::new(NoopResolver),
    );
    let worker_cancel = worker.cancel_token();
    let worker_handle = tokio::spawn(async move { worker.run().await });

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

    let payload: Vec<u8> = (0..=255u8).cycle().take(1024).collect();
    drive_external_http_connect_to_pproxy_listener(external_addr, echo.addr(), &payload)
        .await
        .expect("first payload roundtrip failed");

    // Forced disconnect: kill the listener process without cancelling the
    // Eggress worker.
    pproxy_guard.kill();
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Relaunch the listener on the same explicit ports.
    let _pproxy_guard = spawn_pproxy_listener_explicit(&python, control_port, external_port, auth);
    assert_port_ready(external_port, Duration::from_secs(5)).await;

    let second_payload: Vec<u8> = payload.iter().rev().copied().collect();
    drive_external_http_connect_to_pproxy_listener(external_addr, echo.addr(), &second_payload)
        .await
        .expect("second payload roundtrip after reconnect failed");

    worker_cancel.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(2), worker_handle).await;
}

// ---------------------------------------------------------------------------
// pproxy process helpers
// ---------------------------------------------------------------------------

fn spawn_pproxy_listener(
    python: &str,
    control_port: u16,
    external_port: u16,
    auth: &str,
) -> ProcessGuard {
    let listen = format!("http://127.0.0.1:{external_port}");
    // No `#auth` fragment so pproxy uses the SOCKS5 NO AUTH method
    // (matching the Eggress compatibility `PproxyBackwardClient`
    // `Socks5` framing). With `#user:pass` pproxy would offer
    // USERNAME/PASSWORD (`\x05\x01\x02`) and the Eggress-side
    // server would refuse with `\x05\xff`.
    let auth_segment = if auth.is_empty() {
        String::new()
    } else {
        format!("#{auth}")
    };
    let remote = format!("socks5+in://127.0.0.1:{control_port}{auth_segment}");
    let args = vec!["-l", listen.as_str(), "-r", remote.as_str()];
    let child = std::process::Command::new(python)
        .args(["-m", "pproxy"])
        .args(args)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("failed to spawn pproxy listener");
    ProcessGuard::new(child)
}

fn spawn_pproxy_listener_explicit(
    python: &str,
    control_port: u16,
    external_port: u16,
    auth: &str,
) -> ProcessGuard {
    let listen = format!("http://127.0.0.1:{external_port}");
    let auth_segment = if auth.is_empty() {
        String::new()
    } else {
        format!("#{auth}")
    };
    let remote = format!("socks5+in://127.0.0.1:{control_port}{auth_segment}");
    let args = vec!["-l", listen.as_str(), "-r", remote.as_str()];
    let child = std::process::Command::new(python)
        .args(["-m", "pproxy"])
        .args(args)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("failed to spawn pproxy listener");
    ProcessGuard::new(child)
}

fn spawn_pproxy_worker(
    python: &str,
    control_port: u16,
    auth: &str,
    keepalive_port: u16,
) -> ProcessGuard {
    // pproxy worker side of a backward/reverse proxy. The `-l socks5+in://`
    // URI tells pproxy to dial out to `control_port` and act as a SOCKS5
    // server on that channel. pproxy 2.7.9 has a CLI bug where
    // `ProxyBackward` lacks a `sockets` attribute, causing
    // `print_server_started` to fail and the process to exit because the
    // `servers` list ends up empty. Adding a second `-l` with a real TCP
    // listener keeps pproxy alive long enough to perform the dial-out.
    let keepalive = format!("socks5://127.0.0.1:{keepalive_port}");
    let listen = format!("socks5+in://127.0.0.1:{control_port}#{auth}");
    let args = vec![
        "-l",
        keepalive.as_str(),
        "-l",
        listen.as_str(),
        "-r",
        "direct",
    ];
    let child = std::process::Command::new(python)
        .args(["-m", "pproxy"])
        .args(args)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("failed to spawn pproxy worker");
    ProcessGuard::new(child)
}

// ---------------------------------------------------------------------------
// External client drivers
// ---------------------------------------------------------------------------

/// Drive an external raw TCP payload through the eggress + pproxy channel
/// when the SOCKS5 framing is owned by the eggress server. The eggress
/// listener has already driven the SOCKS5 hello + CONNECT handshake with
/// the pproxy worker and pinned the channel to the configured target,
/// so the external client only needs to push raw payload and read the
/// byte-for-byte echo back through the same channel.
async fn drive_external_raw_payload_to_pproxy_worker(
    external_addr: SocketAddr,
    payload: &[u8],
) -> Result<(), String> {
    let mut external = tokio::net::TcpStream::connect(external_addr)
        .await
        .map_err(|e| format!("connect external: {e}"))?;
    external
        .write_all(payload)
        .await
        .map_err(|e| format!("write payload: {e}"))?;
    external
        .shutdown()
        .await
        .map_err(|e| format!("shutdown: {e}"))?;
    let mut echoed = vec![0u8; payload.len()];
    external
        .read_exact(&mut echoed)
        .await
        .map_err(|e| format!("read echo: {e}"))?;
    drop(external);
    if echoed != payload {
        return Err(format!(
            "payload mismatch: sent {} bytes, received {}",
            payload.len(),
            echoed.len()
        ));
    }
    Ok(())
}

/// Drive an external HTTP client through the pproxy HTTP listener:
///   1. Open TCP to the listener.
///   2. Send `CONNECT <echo> HTTP/1.1`.
///   3. Read 200 Connection Established.
///   4. Stream the payload and assert byte equality.
///
/// pproxy performs the SOCKS5 negotiation with the Eggress worker
/// internally as part of forwarding the CONNECT target, so the external
/// client never speaks SOCKS5 over the established tunnel.
async fn drive_external_http_connect_to_pproxy_listener(
    external_addr: SocketAddr,
    echo_addr: SocketAddr,
    payload: &[u8],
) -> Result<(), String> {
    let mut external = tokio::net::TcpStream::connect(external_addr)
        .await
        .map_err(|e| format!("connect pproxy listener: {e}"))?;

    let connect_line = format!(
        "CONNECT {echo_ip}:{echo_port} HTTP/1.1\r\nHost: {echo_ip}:{echo_port}\r\n\r\n",
        echo_ip = echo_addr.ip(),
        echo_port = echo_addr.port(),
    );
    external
        .write_all(connect_line.as_bytes())
        .await
        .map_err(|e| format!("write CONNECT: {e}"))?;
    external
        .flush()
        .await
        .map_err(|e| format!("flush CONNECT: {e}"))?;

    let mut response = Vec::new();
    let mut buf = [0u8; 256];
    while !response.windows(4).any(|w| w == b"\r\n\r\n") {
        let n = external
            .read(&mut buf)
            .await
            .map_err(|e| format!("read HTTP response: {e}"))?;
        if n == 0 {
            return Err(format!(
                "EOF reading HTTP response: {:?}",
                String::from_utf8_lossy(&response)
            ));
        }
        response.extend_from_slice(&buf[..n]);
        if response.len() > 4096 {
            return Err("HTTP response too large".into());
        }
    }
    let response_text = String::from_utf8_lossy(&response);
    if !response_text.starts_with("HTTP/1.1 200") {
        return Err(format!("non-200 HTTP response: {response_text}"));
    }

    external
        .write_all(payload)
        .await
        .map_err(|e| format!("write payload: {e}"))?;
    let mut echoed = vec![0u8; payload.len()];
    external
        .read_exact(&mut echoed)
        .await
        .map_err(|e| format!("read echo: {e}"))?;
    drop(external);
    if echoed != payload {
        return Err(format!(
            "payload mismatch: sent {} bytes, received {}",
            payload.len(),
            echoed.len()
        ));
    }
    Ok(())
}