eggress-cli 1.0.2

CLI binary for the eggress multi-protocol proxy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Tests demonstrating the reply-before-route defect.
//!
//! These tests prove that HTTP CONNECT, SOCKS4, and SOCKS5 success replies
//! are sent to the client BEFORE the outbound route is actually established.
//! This is a protocol correctness bug: the client receives a "success" reply
//! but the proxy has not yet opened the upstream connection.
//!
//! Tests 1–4 demonstrate the DEFECT: success replies arrive immediately,
//! before any outbound connection is made.
//!
//! Tests 5–7 demonstrate the CORRECT pattern: a server that waits for the
//! route before sending the reply, so the client does not see "success"
//! until the upstream is ready.

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

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::Notify;
use tokio::time::timeout;

/// Timeout for proving a reply arrives "immediately" (without waiting for a route).
const IMMEDIATE: Duration = Duration::from_millis(50);

/// Timeout for proving a reply is NOT yet available (still waiting for route).
const NOT_YET: Duration = Duration::from_millis(50);

// ===========================================================================
// DEFECT TESTS — these PASS, proving the bug exists
// ===========================================================================

/// SOCKS5: success reply (REP=0x00) arrives before any outbound connection.
#[tokio::test]
async fn defect_socks5_success_reply_arrives_before_route() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read method negotiation
        let mut header = [0u8; 2];
        stream.read_exact(&mut header).await.unwrap();
        let nmethods = header[1] as usize;
        let mut methods = vec![0u8; nmethods];
        stream.read_exact(&mut methods).await.unwrap();

        // Send method selection (no auth)
        stream.write_all(&[0x05, 0x00]).await.unwrap();
        stream.flush().await.unwrap();

        // Read CONNECT request header
        let mut req = [0u8; 4];
        stream.read_exact(&mut req).await.unwrap();
        assert_eq!(req[0], 0x05);
        assert_eq!(req[1], 0x01); // CONNECT

        // Read address payload based on ATYP
        match req[3] {
            0x01 => {
                // IPv4: 4 bytes + 2 bytes port
                let mut buf = [0u8; 6];
                stream.read_exact(&mut buf).await.unwrap();
            }
            0x03 => {
                // Domain: 1 byte len + domain + 2 bytes port
                let mut len = [0u8; 1];
                stream.read_exact(&mut len).await.unwrap();
                let mut domain = vec![0u8; len[0] as usize];
                stream.read_exact(&mut domain).await.unwrap();
                let mut port = [0u8; 2];
                stream.read_exact(&mut port).await.unwrap();
            }
            0x04 => {
                // IPv6: 16 bytes + 2 bytes port
                let mut buf = [0u8; 18];
                stream.read_exact(&mut buf).await.unwrap();
            }
            _ => panic!("unexpected atyp"),
        }

        // DEFECT: Send success reply IMMEDIATELY — no outbound route exists yet!
        let reply: [u8; 10] = [
            0x05, // version
            0x00, // REP = success
            0x00, // reserved
            0x01, // ATYP IPv4
            0x00, 0x00, 0x00, 0x00, // bind addr
            0x00, 0x00, // bind port
        ];
        stream.write_all(&reply).await.unwrap();
        stream.flush().await.unwrap();
        // No outbound connection is ever made — this proves the defect.
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    // Method negotiation: offer no-auth
    client.write_all(&[0x05, 0x01, 0x00]).await.unwrap();
    let mut method_resp = [0u8; 2];
    client.read_exact(&mut method_resp).await.unwrap();
    assert_eq!(method_resp, [0x05, 0x00]);

    // CONNECT request for 198.51.100.1:443
    client
        .write_all(&[0x05, 0x01, 0x00, 0x01, 198, 51, 100, 1])
        .await
        .unwrap();
    client.write_all(&443u16.to_be_bytes()).await.unwrap();

    // KEY: The success reply is available IMMEDIATELY.
    // No outbound connection was opened — the proxy sent "success" too early.
    let mut reply_buf = [0u8; 10];
    let result = timeout(IMMEDIATE, client.read_exact(&mut reply_buf)).await;
    assert!(
        result.is_ok(),
        "SOCKS5 success reply is immediately available — defect: reply sent before route"
    );
    let inner = result.unwrap();
    assert!(inner.is_ok());
    assert_eq!(reply_buf[0], 0x05, "version must be 5");
    assert_eq!(reply_buf[1], 0x00, "REP must be 0x00 (success)");

    server.await.unwrap();
}

/// SOCKS4: granted reply (status=90) arrives before any outbound connection.
#[tokio::test]
async fn defect_socks4_granted_reply_arrives_before_route() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read SOCKS4 request header (8 bytes)
        let mut header = [0u8; 8];
        stream.read_exact(&mut header).await.unwrap();
        assert_eq!(header[0], 0x04); // SOCKS4
        assert_eq!(header[1], 0x01); // CONNECT

        // Read NUL-terminated user ID
        loop {
            let mut byte = [0u8; 1];
            stream.read_exact(&mut byte).await.unwrap();
            if byte[0] == 0x00 {
                break;
            }
        }

        // DEFECT: Send granted reply IMMEDIATELY — no outbound route exists yet!
        let reply: [u8; 8] = [
            0x00, // VN (null for reply)
            90,   // CD = granted
            0x00, 0x00, // DSTPORT
            0x00, 0x00, 0x00, 0x00, // DSTIP
        ];
        stream.write_all(&reply).await.unwrap();
        stream.flush().await.unwrap();
        // No outbound connection is ever made — this proves the defect.
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    // SOCKS4 CONNECT request for 10.0.0.1:80
    client
        .write_all(&[0x04, 0x01, 0x00, 80, 10, 0, 0, 1])
        .await
        .unwrap();
    client.write_all(b"testuser").await.unwrap();
    client.write_all(&[0x00]).await.unwrap();

    // KEY: The granted reply is available IMMEDIATELY.
    let mut reply_buf = [0u8; 8];
    let result = timeout(IMMEDIATE, client.read_exact(&mut reply_buf)).await;
    assert!(
        result.is_ok(),
        "SOCKS4 granted reply is immediately available — defect: reply sent before route"
    );
    let inner = result.unwrap();
    assert!(inner.is_ok());
    assert_eq!(reply_buf[0], 0x00, "VN must be 0x00 for reply");
    assert_eq!(reply_buf[1], 90, "CD must be 90 (granted)");

    server.await.unwrap();
}

/// HTTP CONNECT: 200 response arrives before any outbound connection.
#[tokio::test]
async fn defect_http_connect_200_reply_arrives_before_route() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read HTTP CONNECT request until \r\n\r\n
        let mut head = Vec::with_capacity(1024);
        loop {
            let mut byte = [0u8; 1];
            stream.read_exact(&mut byte).await.unwrap();
            head.push(byte[0]);
            if head.len() >= 4 && head[head.len() - 4..] == *b"\r\n\r\n" {
                break;
            }
        }

        let head_str = String::from_utf8_lossy(&head);
        assert!(head_str.starts_with("CONNECT "));

        // DEFECT: Send 200 response IMMEDIATELY — no outbound route exists yet!
        stream
            .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
            .await
            .unwrap();
        stream.flush().await.unwrap();
        // No outbound connection is ever made — this proves the defect.
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    client
        .write_all(b"CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n")
        .await
        .unwrap();

    // KEY: The 200 response is available IMMEDIATELY.
    let mut resp_buf = [0u8; 4096];
    let result = timeout(IMMEDIATE, client.read(&mut resp_buf)).await;
    assert!(
        result.is_ok(),
        "HTTP 200 reply is immediately available — defect: reply sent before route"
    );
    let n = result.unwrap().unwrap();
    let resp = String::from_utf8_lossy(&resp_buf[..n]);
    assert!(
        resp.starts_with("HTTP/1.1 200"),
        "expected HTTP 200, got: {}",
        &resp[..resp.len().min(40)]
    );

    server.await.unwrap();
}

/// SOCKS5: no failure reply when route fails — connection just drops.
///
/// The current code has no failure handling: if the upstream connection fails
/// after the success reply was already sent, the client just sees an ambiguous
/// connection close with no SOCKS5 error reply (REP != 0x00).
#[tokio::test]
async fn defect_socks5_no_failure_reply_when_route_fails() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read method negotiation
        let mut header = [0u8; 2];
        stream.read_exact(&mut header).await.unwrap();
        let nmethods = header[1] as usize;
        let mut methods = vec![0u8; nmethods];
        stream.read_exact(&mut methods).await.unwrap();

        // Send method selection
        stream.write_all(&[0x05, 0x00]).await.unwrap();
        stream.flush().await.unwrap();

        // Read CONNECT request
        let mut req = [0u8; 4];
        stream.read_exact(&mut req).await.unwrap();
        match req[3] {
            0x01 => {
                let mut buf = [0u8; 6];
                stream.read_exact(&mut buf).await.unwrap();
            }
            0x03 => {
                let mut len = [0u8; 1];
                stream.read_exact(&mut len).await.unwrap();
                let mut domain = vec![0u8; len[0] as usize];
                stream.read_exact(&mut domain).await.unwrap();
                let mut port = [0u8; 2];
                stream.read_exact(&mut port).await.unwrap();
            }
            _ => panic!("unexpected atyp"),
        }

        // Simulate route failure: drop stream without sending any reply.
        // No SOCKS5 error reply is sent — just an ambiguous connection close.
        drop(stream);
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    // Method negotiation
    client.write_all(&[0x05, 0x01, 0x00]).await.unwrap();
    let mut method_resp = [0u8; 2];
    client.read_exact(&mut method_resp).await.unwrap();

    // CONNECT request
    client
        .write_all(&[0x05, 0x01, 0x00, 0x01, 198, 51, 100, 1])
        .await
        .unwrap();
    client.write_all(&443u16.to_be_bytes()).await.unwrap();

    // Server drops without any reply. Client sees EOF — ambiguous error.
    let mut reply_buf = [0u8; 10];
    let result = timeout(
        Duration::from_millis(200),
        client.read_exact(&mut reply_buf),
    )
    .await;

    // The read should fail (connection closed / EOF), not get a SOCKS5 error reply.
    assert!(
        result.is_err() || result.unwrap().is_err(),
        "client sees connection close with no failure reply — missing error handling"
    );

    server.await.unwrap();
}

// ===========================================================================
// CORRECT BEHAVIOR TESTS — demonstrate the proper reply ordering pattern
// ===========================================================================

/// SOCKS5: correct behavior — reply sent only after route is established.
///
/// The server waits for a `Notify` signal (simulating route readiness) before
/// sending the success reply. The client verifies the reply is NOT available
/// immediately, then becomes available after the signal.
#[tokio::test]
async fn correct_socks5_reply_after_route_ready() {
    let route_ready = Arc::new(Notify::new());
    let route_ready_clone = route_ready.clone();

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read method negotiation
        let mut header = [0u8; 2];
        stream.read_exact(&mut header).await.unwrap();
        let nmethods = header[1] as usize;
        let mut methods = vec![0u8; nmethods];
        stream.read_exact(&mut methods).await.unwrap();

        // Send method selection
        stream.write_all(&[0x05, 0x00]).await.unwrap();
        stream.flush().await.unwrap();

        // Read CONNECT request
        let mut req = [0u8; 4];
        stream.read_exact(&mut req).await.unwrap();
        match req[3] {
            0x01 => {
                let mut buf = [0u8; 6];
                stream.read_exact(&mut buf).await.unwrap();
            }
            0x03 => {
                let mut len = [0u8; 1];
                stream.read_exact(&mut len).await.unwrap();
                let mut domain = vec![0u8; len[0] as usize];
                stream.read_exact(&mut domain).await.unwrap();
                let mut port = [0u8; 2];
                stream.read_exact(&mut port).await.unwrap();
            }
            _ => panic!("unexpected atyp"),
        }

        // CORRECT: Wait for route to be established before sending reply.
        tokio::time::timeout(Duration::from_secs(5), route_ready_clone.notified())
            .await
            .expect("route_ready signal should arrive");

        // Now send success reply — route is ready.
        let reply: [u8; 10] = [0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
        stream.write_all(&reply).await.unwrap();
        stream.flush().await.unwrap();
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    // Method negotiation
    client.write_all(&[0x05, 0x01, 0x00]).await.unwrap();
    let mut method_resp = [0u8; 2];
    client.read_exact(&mut method_resp).await.unwrap();

    // CONNECT request
    client
        .write_all(&[0x05, 0x01, 0x00, 0x01, 198, 51, 100, 1])
        .await
        .unwrap();
    client.write_all(&443u16.to_be_bytes()).await.unwrap();

    // ASSERTION: Reply should NOT be available yet (route not ready).
    let mut reply_buf = [0u8; 10];
    let result = timeout(NOT_YET, client.read_exact(&mut reply_buf)).await;
    assert!(
        result.is_err(),
        "reply should not be available before route is established"
    );

    // Signal that the route is now ready.
    route_ready.notify_one();

    // Now the reply should become available.
    let result = timeout(Duration::from_secs(5), client.read_exact(&mut reply_buf)).await;
    result
        .expect("timed out waiting for reply")
        .expect("read failed");
    assert_eq!(reply_buf[0], 0x05);
    assert_eq!(reply_buf[1], 0x00);

    server.await.unwrap();
}

/// SOCKS4: correct behavior — granted reply sent only after route is established.
#[tokio::test]
async fn correct_socks4_reply_after_route_ready() {
    let route_ready = Arc::new(Notify::new());
    let route_ready_clone = route_ready.clone();

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read SOCKS4 request
        let mut header = [0u8; 8];
        stream.read_exact(&mut header).await.unwrap();

        // Read NUL-terminated user ID
        loop {
            let mut byte = [0u8; 1];
            stream.read_exact(&mut byte).await.unwrap();
            if byte[0] == 0x00 {
                break;
            }
        }

        // CORRECT: Wait for route before sending reply.
        tokio::time::timeout(Duration::from_secs(5), route_ready_clone.notified())
            .await
            .expect("route_ready signal should arrive");

        // Now send granted reply.
        let reply: [u8; 8] = [0x00, 90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
        stream.write_all(&reply).await.unwrap();
        stream.flush().await.unwrap();
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    // SOCKS4 CONNECT request
    client
        .write_all(&[0x04, 0x01, 0x00, 80, 10, 0, 0, 1])
        .await
        .unwrap();
    client.write_all(b"testuser").await.unwrap();
    client.write_all(&[0x00]).await.unwrap();

    // ASSERTION: Reply should NOT be available yet.
    let mut reply_buf = [0u8; 8];
    let result = timeout(NOT_YET, client.read_exact(&mut reply_buf)).await;
    assert!(
        result.is_err(),
        "reply should not be available before route is established"
    );

    // Signal route ready.
    route_ready.notify_one();

    // Now reply should arrive.
    let result = timeout(Duration::from_secs(5), client.read_exact(&mut reply_buf)).await;
    result
        .expect("timed out waiting for reply")
        .expect("read failed");
    assert_eq!(reply_buf[0], 0x00);
    assert_eq!(reply_buf[1], 90);

    server.await.unwrap();
}

/// HTTP CONNECT: correct behavior — 200 sent only after route is established.
#[tokio::test]
async fn correct_http_connect_reply_after_route_ready() {
    let route_ready = Arc::new(Notify::new());
    let route_ready_clone = route_ready.clone();

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();

        // Read HTTP CONNECT request until \r\n\r\n
        let mut head = Vec::with_capacity(1024);
        loop {
            let mut byte = [0u8; 1];
            stream.read_exact(&mut byte).await.unwrap();
            head.push(byte[0]);
            if head.len() >= 4 && head[head.len() - 4..] == *b"\r\n\r\n" {
                break;
            }
        }

        // CORRECT: Wait for route before sending 200.
        tokio::time::timeout(Duration::from_secs(5), route_ready_clone.notified())
            .await
            .expect("route_ready signal should arrive");

        // Now send 200 response.
        stream
            .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
            .await
            .unwrap();
        stream.flush().await.unwrap();
    });

    let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();

    client
        .write_all(b"CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n")
        .await
        .unwrap();

    // ASSERTION: Reply should NOT be available yet.
    let mut resp_buf = [0u8; 4096];
    let result = timeout(NOT_YET, client.read(&mut resp_buf)).await;
    assert!(
        result.is_err(),
        "reply should not be available before route is established"
    );

    // Signal route ready.
    route_ready.notify_one();

    // Now reply should arrive.
    let result = timeout(Duration::from_secs(5), client.read(&mut resp_buf)).await;
    let n = result.expect("timed out").expect("read failed");
    let resp = String::from_utf8_lossy(&resp_buf[..n]);
    assert!(resp.starts_with("HTTP/1.1 200"));

    server.await.unwrap();
}