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
use std::process::Command;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;

static LISTENER_MUTEX: Mutex<()> = Mutex::new(());

fn pproxy_bin() -> Command {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_pproxy"));
    cmd.env("RUST_LOG", "error");
    cmd
}

struct ProcessGuard(std::process::Child);

impl ProcessGuard {
    fn new(child: std::process::Child) -> Self {
        Self(child)
    }
}

impl Drop for ProcessGuard {
    fn drop(&mut self) {
        let _ = self.0.kill();
        let _ = self.0.wait();
    }
}

fn run_output(cmd: &mut Command) -> std::process::Output {
    let _guard = LISTENER_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
    cmd.output().expect("failed to run pproxy")
}

/// Spawn pproxy, capture stderr via temp file, kill after timeout_ms.
/// Holds LISTENER_MUTEX to prevent port/resource conflicts under parallel execution.
fn spawn_and_collect(cmd: &mut Command, timeout_ms: u64) -> (Option<i32>, String) {
    let _guard = LISTENER_MUTEX.lock().unwrap_or_else(|e| e.into_inner());

    let tmp = tempfile::NamedTempFile::new().expect("failed to create temp file");
    let stderr_path = tmp.path().to_path_buf();

    let stderr_file = std::fs::File::create(&stderr_path).expect("failed to create stderr file");
    let child = cmd
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::from(stderr_file))
        .spawn()
        .expect("failed to spawn pproxy");

    thread::sleep(Duration::from_millis(timeout_ms));
    // Use RAII guard so child is killed even on panic/timeout paths
    let mut guard = ProcessGuard::new(child);
    let _ = guard.0.kill();
    let status = guard.0.wait().ok().and_then(|s| s.code());
    std::mem::forget(guard);

    let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default();
    (status, stderr)
}

#[test]
fn help_flag() {
    let output = run_output(pproxy_bin().arg("--help"));
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("pproxy compatibility binary"));
    assert!(stdout.contains("-l"));
    assert!(stdout.contains("-r"));
    assert!(stdout.contains("--test"));
    assert!(stdout.contains("--sys"));
    assert!(stdout.contains("--ssl"));
    assert!(stdout.contains("--pac"));
    assert!(stdout.contains("-d"));
    assert!(stdout.contains("--reuse"));
    assert!(stdout.contains("--auth"));
    assert!(stdout.contains("--daemon"));
}

#[test]
fn help_flag_d_and_log_wording() {
    let output = run_output(pproxy_bin().arg("--help"));
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    // -d must not claim native-equivalent Python traceback semantics
    assert!(
        !stdout.contains("native equivalent") || !stdout.contains("traceback"),
        "-d help must not pair 'native equivalent' with traceback wording: {stdout}"
    );
    assert!(
        stdout.contains("tracing") || stdout.contains("debug") || stdout.contains("Debug"),
        "-d help should mention tracing or debug: {stdout}"
    );
    // --log must not describe stderr as native-equivalent file output
    assert!(
        !stdout.contains("native equivalent: stderr"),
        "--log help must not describe stderr as native equivalent: {stdout}"
    );
    assert!(
        stdout.contains("stderr") || stdout.contains("recognized") || stdout.contains("compat"),
        "--log help should mention stderr or compat: {stdout}"
    );
}

#[test]
fn short_help_flag() {
    let output = run_output(pproxy_bin().arg("-h"));
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("pproxy compatibility binary"));
}

#[test]
fn version_flag() {
    let output = run_output(pproxy_bin().arg("--version"));
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("eggress-pproxy-compat"));
}

#[test]
fn no_args_starts_with_default_listener() {
    let (_, stderr) = spawn_and_collect(&mut pproxy_bin(), 3000);
    assert!(
        stderr.contains("eggress-pproxy-compat"),
        "expected version banner for default startup, got: {stderr}",
    );
    assert!(
        stderr.contains("listen:"),
        "expected listener line in default startup banner, got: {stderr}",
    );
}

#[test]
fn startup_banner_shows_version_and_listeners() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args(["-l", "http://:19800", "-r", "socks5://127.0.0.1:1080"]),
        3000,
    );
    assert!(
        stderr.contains("eggress-pproxy-compat"),
        "expected version in banner, got: {stderr}",
    );
    assert!(
        stderr.contains("listen:") && stderr.contains("http://:19800"),
        "expected listener in banner, got: {stderr}",
    );
    assert!(
        stderr.contains("remote:") && stderr.contains("socks5://127.0.0.1:1080"),
        "expected remote in banner, got: {stderr}",
    );
}

#[test]
fn startup_banner_redacts_uri_credentials() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19811",
            "-r",
            "socks5://user:secret@127.0.0.1:1080",
        ]),
        3000,
    );
    assert!(
        !stderr.contains("secret"),
        "startup banner leaked credentials: {stderr}"
    );
    assert!(
        stderr.contains("socks5://****:****@127.0.0.1:1080"),
        "expected redacted remote in banner, got: {stderr}"
    );
}

#[test]
fn startup_banner_shows_tls_when_ssl() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19801",
            "-r",
            "socks5://127.0.0.1:1080",
            "--ssl",
            "cert.pem,key.pem",
        ]),
        3000,
    );
    assert!(
        stderr.contains("tls:      enabled"),
        "expected TLS enabled in banner, got: {stderr}",
    );
}

#[test]
fn startup_banner_shows_pac() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19802",
            "-r",
            "socks5://127.0.0.1:1080",
            "--pac",
            "/proxy.pac",
        ]),
        3000,
    );
    assert!(
        stderr.contains("pac:      enabled"),
        "expected PAC enabled in banner, got: {stderr}",
    );
}

#[test]
fn startup_banner_shows_udp() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19803",
            "-r",
            "socks5://127.0.0.1:1080",
            "-ul",
            "socks5://:19804",
        ]),
        3000,
    );
    assert!(
        stderr.contains("udp:"),
        "expected UDP in banner, got: {stderr}",
    );
}

#[test]
fn unsupported_daemon_flag_fails() {
    let output = run_output(pproxy_bin().args([
        "-l",
        "http://:19805",
        "-r",
        "socks5://127.0.0.1:1080",
        "--daemon",
    ]));
    assert_eq!(
        output.status.code(),
        Some(5),
        "expected exit code 5 (unsupported --daemon), got {:?}",
        output.status.code()
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("daemon") || stderr.contains("not supported"),
        "expected daemon error in stderr, got: {stderr}",
    );
}

#[test]
fn verbose_flag_accepted() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args(["-l", "http://:19806", "-r", "socks5://127.0.0.1:1080", "-v"]),
        3000,
    );
    assert!(
        stderr.contains("listen:"),
        "expected listener in banner for verbose startup, got: {stderr}",
    );
}

#[test]
fn verbose_double_flag_accepted() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19807",
            "-r",
            "socks5://127.0.0.1:1080",
            "-vv",
        ]),
        3000,
    );
    assert!(
        stderr.contains("listen:"),
        "expected listener in banner for -vv startup, got: {stderr}",
    );
}

#[test]
fn debug_flag_accepted_independently() {
    // `-d` is a debug-level flag in pproxy 2.7.9; it must not affect
    // the startup banner and must not enable daemon behavior.
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args(["-l", "http://:19820", "-r", "socks5://127.0.0.1:1080", "-d"]),
        3000,
    );
    assert!(
        stderr.contains("listen:") && stderr.contains("http://:19820"),
        "expected listener in banner for -d startup, got: {stderr}",
    );
    assert!(
        !stderr.to_lowercase().contains("daemon"),
        "-d must not enable daemon behavior, got: {stderr}",
    );
}

#[test]
fn debug_flag_and_daemon_flag_still_fatal() {
    // Even though -d is independent of --daemon, --daemon remains
    // fatal before startup in pproxy compatibility mode.
    let output = run_output(pproxy_bin().args([
        "-l",
        "http://:19821",
        "-r",
        "socks5://127.0.0.1:1080",
        "-d",
        "--daemon",
    ]));
    assert!(
        !output.status.success(),
        "expected non-zero exit for --daemon with -d, got {:?}",
        output.status.code(),
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("daemon") || stderr.contains("not supported"),
        "expected daemon error in stderr when combining -d with --daemon, got: {stderr}",
    );
}

#[test]
fn debug_flag_changes_default_log_level() {
    // -d alone selects a debug-level default log filter; a clean
    // startup confirms that logging initialization succeeds.
    // The actual log level is exercised by the unit-level
    // `default_log_level` helper in eggress-pproxy-compat.
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args(["-l", "http://:19822", "-r", "socks5://127.0.0.1:1080", "-d"]),
        2500,
    );
    assert!(
        stderr.contains("eggress-pproxy-compat"),
        "expected successful -d startup, got: {stderr}",
    );
}

#[test]
fn verbose_triple_flag_accepted() {
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19808",
            "-r",
            "socks5://127.0.0.1:1080",
            "-vvv",
        ]),
        3000,
    );
    assert!(
        stderr.contains("listen:"),
        "expected listener in banner for -vvv startup, got: {stderr}",
    );
}

#[test]
fn unsupported_ssh_scheme_fails() {
    let (code, stderr) = spawn_and_collect_inner(pproxy_bin().args(["-l", "ssh://host:22"]), 2000);
    assert!(
        stderr.contains("unsupported") || stderr.contains("not supported") || code != Some(0),
        "expected unsupported diagnostic for SSH scheme, got: code={code:?}, stderr={stderr}",
    );
}

#[test]
fn missing_value_for_l_fails() {
    let output = run_output(pproxy_bin().arg("-l"));
    assert!(!output.status.success());
}

#[test]
fn missing_value_for_r_fails() {
    let output = run_output(pproxy_bin().args(["-l", "http://:19809", "-r"]));
    assert!(!output.status.success());
}

#[test]
fn sys_flag_fails_before_startup() {
    // The local Linux backend may reject system-proxy application, or keep
    // the service running when the host extension is available. Either way,
    // it must use the compatibility operation rather than an unsupported
    // feature gate.
    let (status, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19811",
            "-r",
            "socks5://127.0.0.1:1080",
            "--sys",
        ]),
        1500,
    );
    assert!(
        stderr.contains("listen:") || stderr.contains("sys") || stderr.contains("proxy"),
        "expected system-proxy startup or operation output, got status {status:?}: {stderr}",
    );
    assert!(
        !stderr.contains("unsupported"),
        "--sys must not be rejected as unsupported: {stderr}",
    );
}

#[test]
fn auth_flag_starts_compatibility_listener() {
    let (status, stderr) = spawn_and_collect(
        pproxy_bin().args([
            "-l",
            "http://:19812",
            "-r",
            "socks5://127.0.0.1:1080",
            "--auth",
            "30",
        ]),
        1500,
    );
    assert!(
        status.is_none(),
        "expected listener to remain running, got {status:?}: {stderr}"
    );
    assert!(
        stderr.contains("listen:"),
        "expected startup banner, got: {stderr}"
    );
    assert!(
        !stderr.contains("unsupported"),
        "--auth must not be rejected as unsupported: {stderr}"
    );
}

#[test]
fn malformed_auth_fails() {
    let output = run_output(pproxy_bin().args([
        "-l",
        "http://:19813",
        "-r",
        "socks5://127.0.0.1:1080",
        "--auth",
        "abc",
    ]));
    assert!(
        !output.status.success(),
        "expected non-zero exit for malformed --auth, got {:?}",
        output.status.code(),
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("auth") || stderr.contains("error"),
        "expected auth error in stderr, got: {stderr}",
    );
}

#[test]
fn unknown_flag_fails() {
    let output = run_output(pproxy_bin().args([
        "-l",
        "http://:19810",
        "-r",
        "socks5://127.0.0.1:1080",
        "--bogus-flag",
    ]));
    assert!(
        !output.status.success(),
        "expected non-zero exit for unknown flag, got {:?}",
        output.status.code()
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("unknown") || stderr.contains("bogus-flag"),
        "expected unknown flag error in stderr, got: {stderr}",
    );
}

#[test]
fn strict_parser_surface_fails_closed_before_startup() {
    let cases: &[(&[&str], &str)] = &[
        (&["--log", "/tmp/pproxy.log"], "--log"),
        (&["--rulefile", "/tmp/rules"], "--rulefile"),
        (&["--listen", "http://:19814"], "--listen"),
        (&["proxy://:19814"], "proxy://:19814"),
        (&["-s", "invalid"], "invalid choice"),
        (&["-a", "invalid"], "valid integer"),
    ];

    for (args, expected) in cases {
        let output = run_output(pproxy_bin().args(*args));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert_eq!(output.status.code(), Some(2), "args={args:?}: {stderr}");
        assert!(stderr.contains(expected), "args={args:?}: {stderr}");
        assert!(
            !stderr.contains("pproxy started") && !stderr.contains("listen:"),
            "parser failure must not start a listener: {stderr}"
        );
    }
}

/// Inner helper that does NOT acquire the mutex (caller is responsible).
fn spawn_and_collect_inner(cmd: &mut Command, timeout_ms: u64) -> (Option<i32>, String) {
    let tmp = tempfile::NamedTempFile::new().expect("failed to create temp file");
    let stderr_path = tmp.path().to_path_buf();

    let stderr_file = std::fs::File::create(&stderr_path).expect("failed to create stderr file");
    let child = cmd
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::from(stderr_file))
        .spawn()
        .expect("failed to spawn pproxy");

    thread::sleep(Duration::from_millis(timeout_ms));
    let mut guard = ProcessGuard::new(child);
    let _ = guard.0.kill();
    let status = guard.0.wait().ok().and_then(|s| s.code());
    std::mem::forget(guard);

    let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default();
    (status, stderr)
}

#[test]
fn test_mode_runs_in_process_no_sibling_binary() {
    // Regression: --test must call the shared Rust upstream-test implementation
    // in-process, not spawn a sibling `eggress` binary. We verify by running
    // with a target that connects to a non-existent upstream; the test should
    // complete with a failure exit code (unreachable) without needing an
    // `eggress` binary on PATH.
    let output = run_output(pproxy_bin().args([
        "-l",
        "http://:19890",
        "-r",
        "socks5://127.0.0.1:19891",
        "--test",
        "http://example.com",
    ]));
    // The test mode should exit (not hang) and report the upstream as
    // unreachable. Exit code 1 = unreachable (all upstreams failed).
    assert_ne!(
        output.status.code(),
        Some(0),
        "expected non-zero exit for unreachable upstream test"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.contains("pproxy started") && !stderr.contains("listen:"),
        "--test must not start or advertise a listener: {stderr}"
    );
}

#[test]
fn in_memory_startup_no_tempfile() {
    // Regression: pproxy startup must not create temporary files. We verify
    // by checking the startup banner appears (which is printed before config
    // validation) and the process starts correctly with in-memory config.
    let (_, stderr) = spawn_and_collect(
        pproxy_bin().args(["-l", "http://:19892", "-r", "socks5://127.0.0.1:1080"]),
        2000,
    );
    assert!(
        stderr.contains("listen:") && stderr.contains("http://:19892"),
        "expected successful in-memory startup, got: {stderr}",
    );
}