eggress-runtime 1.0.2

Service supervisor and composition layer for eggress
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
use std::io::Write;
use std::sync::atomic::Ordering;
use std::sync::Once;
use std::time::Duration;

use tempfile::NamedTempFile;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;

fn toml_path(path: &std::path::Path) -> String {
    path.display().to_string().replace('\\', "/")
}

struct ShutdownGuard {
    token: tokio_util::sync::CancellationToken,
}

impl Drop for ShutdownGuard {
    fn drop(&mut self) {
        self.token.cancel();
    }
}

static INIT: Once = Once::new();

fn install_crypto() {
    INIT.call_once(|| {
        eggress_transport_tls::install_default_crypto_provider();
    });
}

fn write_config(content: &str) -> NamedTempFile {
    let mut f = NamedTempFile::new().unwrap();
    f.write_all(content.as_bytes()).unwrap();
    f.flush().unwrap();
    f
}

async fn wait_ready(state: &eggress_runtime::RuntimeState) {
    for _ in 0..100 {
        if state.readiness.load(Ordering::Relaxed) {
            return;
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    panic!("timeout waiting for readiness");
}

async fn start_tcp_echo() -> std::net::SocketAddr {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        loop {
            let (mut stream, _) = match listener.accept().await {
                Ok(s) => s,
                Err(_) => break,
            };
            tokio::spawn(async move {
                let mut buf = [0u8; 4096];
                loop {
                    match stream.read(&mut buf).await {
                        Ok(0) => break,
                        Ok(n) => {
                            if stream.write_all(&buf[..n]).await.is_err() {
                                break;
                            }
                        }
                        Err(_) => break,
                    }
                }
            });
        }
    });
    addr
}

fn self_signed_cert() -> (String, String) {
    let cert_params = rcgen::CertificateParams::new(vec!["localhost".to_string()]).unwrap();
    let key_pair = rcgen::KeyPair::generate().unwrap();
    let cert_der = cert_params.self_signed(&key_pair).unwrap();
    (cert_der.pem(), key_pair.serialize_pem())
}

fn make_tls_connector() -> tokio_rustls::TlsConnector {
    let client_config = eggress_transport_tls::TlsClientConfigBuilder::new()
        .with_insecure()
        .build()
        .unwrap();
    tokio_rustls::TlsConnector::from(client_config)
}

async fn http_get(addr: &str, path: &str) -> (u16, String) {
    let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
    let request = format!("GET {path} HTTP/1.1\r\nHost: {addr}\r\nConnection: close\r\n\r\n");
    tokio::io::AsyncWriteExt::write_all(&mut stream, request.as_bytes())
        .await
        .unwrap();
    tokio::io::AsyncWriteExt::flush(&mut stream).await.unwrap();

    let mut response = Vec::new();
    loop {
        let mut buf = [0u8; 4096];
        match tokio::io::AsyncReadExt::read(&mut stream, &mut buf).await {
            Ok(0) => break,
            Ok(n) => response.extend_from_slice(&buf[..n]),
            Err(_) => break,
        }
    }
    let response = String::from_utf8_lossy(&response);
    let status_line = response.lines().next().unwrap_or("");
    let status = status_line
        .split_whitespace()
        .nth(1)
        .and_then(|s| s.parse::<u16>().ok())
        .unwrap_or(0);
    let body = response.split("\r\n\r\n").nth(1).unwrap_or("").to_string();
    (status, body)
}

fn metric_value(body: &str, name: &str) -> Option<f64> {
    let candidates = [name.to_string(), format!("{name}_total")];
    let mut total = 0.0;
    let mut found = false;
    for line in body.lines() {
        if line.starts_with('#') {
            continue;
        }
        if !candidates.iter().any(|n| matches_name(line, n)) {
            continue;
        }
        if let Some(v) = line_value(line) {
            total += v;
            found = true;
        }
    }
    found.then_some(total)
}

fn matches_name(line: &str, name: &str) -> bool {
    if let Some(rest) = line.strip_prefix(name) {
        rest.starts_with(' ') || rest.starts_with('{')
    } else {
        false
    }
}

fn line_value(line: &str) -> Option<f64> {
    // Format: metric_name{labels} value or metric_name value
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() >= 2 {
        parts.last().and_then(|v| v.parse::<f64>().ok())
    } else {
        None
    }
}

fn build_trojan_handshake(password: &str, target: &str, port: u16) -> Vec<u8> {
    use eggress_protocol_trojan::hash::password_hash;
    let hash = password_hash(password);
    let mut handshake = Vec::new();
    handshake.extend_from_slice(hash.as_bytes());
    handshake.extend_from_slice(b"\r\n");
    handshake.push(0x01); // CONNECT
    handshake.push(0x03); // ATYP domain
    handshake.push(target.len() as u8);
    handshake.extend_from_slice(target.as_bytes());
    handshake.extend_from_slice(&port.to_be_bytes());
    handshake.extend_from_slice(b"\r\n");
    handshake
}

fn trojan_config(cert: &str, key: &str, password: &str, fallback_line: &str) -> String {
    format!(
        r#"
version = 1

[[listeners]]
name = "trojan-in"
bind = "127.0.0.1:0"
protocols = ["trojan"]

[listeners.tls]
cert = "{cert}"
key = "{key}"

[listeners.trojan]
password = "{password}"
{fallback_line}
"#,
        cert = cert,
        key = key,
        password = password,
        fallback_line = fallback_line,
    )
}

// ---------------------------------------------------------------------------
// 1. trojan_auth_rejected: wrong password → connection closed (no fallback)
// ---------------------------------------------------------------------------
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn trojan_auth_rejected() {
    install_crypto();
    let echo_addr = start_tcp_echo().await;
    let password = "correct-password";

    let (cert_pem, key_pem) = self_signed_cert();
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    std::fs::write(cert_file.path(), &cert_pem).unwrap();
    std::fs::write(key_file.path(), &key_pem).unwrap();

    let config = trojan_config(
        &toml_path(cert_file.path()),
        &toml_path(key_file.path()),
        password,
        "",
    );

    let f = write_config(&config);
    let path = f.path().to_str().unwrap();
    let mut sup = eggress_runtime::ServiceSupervisor::start(path).unwrap();
    let state = sup.state().clone();
    let token = sup.shutdown_token();
    let _guard = ShutdownGuard {
        token: token.clone(),
    };
    let _jh = tokio::task::spawn_blocking(move || sup.run());

    wait_ready(&state).await;

    let listener_addr = {
        let addrs = state.listener_addrs.lock().unwrap();
        addrs[0].unwrap()
    };

    let connector = make_tls_connector();
    let tcp_stream = tokio::net::TcpStream::connect(listener_addr)
        .await
        .expect("connect");
    let domain = rustls::pki_types::ServerName::try_from("localhost".to_string()).unwrap();
    let mut tls_stream = connector.connect(domain, tcp_stream).await.unwrap();

    let handshake = build_trojan_handshake("wrong-password", "127.0.0.1", echo_addr.port());
    let _ = tls_stream.write_all(&handshake).await;

    let mut buf = [0u8; 1];
    let read_result = tokio::time::timeout(Duration::from_secs(2), tls_stream.read(&mut buf)).await;
    match read_result {
        Ok(Ok(0)) => {}
        Ok(Ok(_)) => {}
        Ok(Err(_)) => {}
        Err(_) => {}
    }

    drop(tls_stream);
}

// ---------------------------------------------------------------------------
// 2. trojan_fallback_on_auth_failure: wrong password + fallback → relayed
//
// When the password doesn't match and a fallback is configured, the server
// relays the connection (including the 56-byte hash prefix) to the fallback
// target. The echo server echoes everything back.
//
// Config uses no upstreams/rules so routing defaults to Direct, connecting
// directly to the fallback target.
// ---------------------------------------------------------------------------
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn trojan_fallback_on_auth_failure() {
    install_crypto();
    let fallback_echo = start_tcp_echo().await;
    let password = "correct-password";

    let (cert_pem, key_pem) = self_signed_cert();
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    std::fs::write(cert_file.path(), &cert_pem).unwrap();
    std::fs::write(key_file.path(), &key_pem).unwrap();

    let config = trojan_config(
        &toml_path(cert_file.path()),
        &toml_path(key_file.path()),
        password,
        &format!("fallback = \"{}\"", fallback_echo),
    );

    let f = write_config(&config);
    let path = f.path().to_str().unwrap();
    let mut sup = eggress_runtime::ServiceSupervisor::start(path).unwrap();
    let state = sup.state().clone();
    let token = sup.shutdown_token();
    let _guard = ShutdownGuard {
        token: token.clone(),
    };
    let _jh = tokio::task::spawn_blocking(move || sup.run());

    wait_ready(&state).await;

    let listener_addr = {
        let addrs = state.listener_addrs.lock().unwrap();
        addrs[0].unwrap()
    };

    let connector = make_tls_connector();
    let tcp_stream = tokio::net::TcpStream::connect(listener_addr)
        .await
        .expect("connect");
    let domain = rustls::pki_types::ServerName::try_from("localhost".to_string()).unwrap();
    let mut tls_stream = connector.connect(domain, tcp_stream).await.unwrap();

    let handshake = build_trojan_handshake("wrong-password", "127.0.0.1", fallback_echo.port());
    tls_stream.write_all(&handshake).await.unwrap();
    tls_stream.flush().await.unwrap();

    tls_stream.write_all(b"fallback-test").await.unwrap();
    tls_stream.flush().await.unwrap();

    let mut accumulated = Vec::new();
    let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
    loop {
        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
        if remaining.is_zero() {
            break;
        }
        let mut buf = [0u8; 4096];
        match tokio::time::timeout(remaining, tls_stream.read(&mut buf)).await {
            Ok(Ok(0)) => break,
            Ok(Ok(n)) => {
                accumulated.extend_from_slice(&buf[..n]);
                if accumulated.ends_with(b"fallback-test") {
                    break;
                }
            }
            Ok(Err(_)) => break,
            Err(_) => break,
        }
    }

    assert!(
        accumulated.ends_with(b"fallback-test"),
        "fallback should relay to echo server; got {} bytes: {:?}",
        accumulated.len(),
        String::from_utf8_lossy(&accumulated)
    );

    drop(tls_stream);
}

// ---------------------------------------------------------------------------
// 3. trojan_auth_failure_metrics: wrong password increments auth metric
// ---------------------------------------------------------------------------
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn trojan_auth_failure_metrics() {
    install_crypto();
    let echo_addr = start_tcp_echo().await;
    let password = "correct-password";

    let (cert_pem, key_pem) = self_signed_cert();
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    std::fs::write(cert_file.path(), &cert_pem).unwrap();
    std::fs::write(key_file.path(), &key_pem).unwrap();

    let config = format!(
        r#"
version = 1

[[listeners]]
name = "trojan-in"
bind = "127.0.0.1:0"
protocols = ["trojan"]

[listeners.tls]
cert = "{cert}"
key = "{key}"

[listeners.trojan]
password = "{password}"

[admin]
bind = "127.0.0.1:0"
enabled = true
"#,
        cert = toml_path(cert_file.path()),
        key = toml_path(key_file.path()),
    );

    let f = write_config(&config);
    let path = f.path().to_str().unwrap();
    let mut sup = eggress_runtime::ServiceSupervisor::start(path).unwrap();
    let state = sup.state().clone();
    let token = sup.shutdown_token();
    let _guard = ShutdownGuard {
        token: token.clone(),
    };
    let _jh = tokio::task::spawn_blocking(move || sup.run());

    wait_ready(&state).await;

    let listener_addr = {
        let addrs = state.listener_addrs.lock().unwrap();
        addrs[0].unwrap()
    };

    let admin_addr = state
        .admin_local_addr
        .lock()
        .unwrap()
        .expect("admin should have bound")
        .to_string();

    // Read baseline auth failure count
    let (_status, body_before) = http_get(&admin_addr, "/metrics").await;
    let auth_before = metric_value(&body_before, "eggress_auth_failures_total").unwrap_or(0.0);

    // Connect with wrong password
    let connector = make_tls_connector();
    let tcp_stream = tokio::net::TcpStream::connect(listener_addr)
        .await
        .expect("connect");
    let domain = rustls::pki_types::ServerName::try_from("localhost".to_string()).unwrap();
    let mut tls_stream = connector.connect(domain, tcp_stream).await.unwrap();

    let handshake = build_trojan_handshake("wrong-password", "127.0.0.1", echo_addr.port());
    let _ = tls_stream.write_all(&handshake).await;

    // Read result (server will close or send fallback data)
    let mut buf = [0u8; 1];
    let _ = tokio::time::timeout(Duration::from_secs(2), tls_stream.read(&mut buf)).await;

    drop(tls_stream);

    // Allow metrics to be recorded
    tokio::time::sleep(Duration::from_millis(200)).await;

    // Scrape metrics after the auth failure
    let (_status, body_after) = http_get(&admin_addr, "/metrics").await;
    let auth_after = metric_value(&body_after, "eggress_auth_failures_total").unwrap_or(0.0);

    assert!(
        auth_after > auth_before,
        "eggress_auth_failures_total should increment after Trojan auth failure: before={auth_before}, after={auth_after}"
    );
}

// ---------------------------------------------------------------------------
// 4. trojan_correct_password: right password → normal Trojan relay
// ---------------------------------------------------------------------------
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn trojan_correct_password() {
    install_crypto();
    let echo_addr = start_tcp_echo().await;
    let password = "correct-password";

    let (cert_pem, key_pem) = self_signed_cert();
    let cert_file = NamedTempFile::new().unwrap();
    let key_file = NamedTempFile::new().unwrap();
    std::fs::write(cert_file.path(), &cert_pem).unwrap();
    std::fs::write(key_file.path(), &key_pem).unwrap();

    let config = trojan_config(
        &toml_path(cert_file.path()),
        &toml_path(key_file.path()),
        password,
        "",
    );

    let f = write_config(&config);
    let path = f.path().to_str().unwrap();
    let mut sup = eggress_runtime::ServiceSupervisor::start(path).unwrap();
    let state = sup.state().clone();
    let token = sup.shutdown_token();
    let _guard = ShutdownGuard {
        token: token.clone(),
    };
    let _jh = tokio::task::spawn_blocking(move || sup.run());

    wait_ready(&state).await;

    let listener_addr = {
        let addrs = state.listener_addrs.lock().unwrap();
        addrs[0].unwrap()
    };

    let connector = make_tls_connector();
    let tcp_stream = tokio::net::TcpStream::connect(listener_addr)
        .await
        .expect("connect");
    let domain = rustls::pki_types::ServerName::try_from("localhost".to_string()).unwrap();
    let mut tls_stream = connector.connect(domain, tcp_stream).await.unwrap();

    let handshake = build_trojan_handshake(password, "127.0.0.1", echo_addr.port());
    tls_stream.write_all(&handshake).await.unwrap();
    tls_stream.flush().await.unwrap();

    tls_stream.write_all(b"trojan-correct").await.unwrap();
    tls_stream.flush().await.unwrap();

    let mut buf = [0u8; 4096];
    let n = tokio::time::timeout(Duration::from_secs(5), tls_stream.read(&mut buf))
        .await
        .expect("timeout reading relay response");

    match n {
        Ok(0) => panic!("got 0 bytes from relay"),
        Ok(bytes) => {
            assert_eq!(
                &buf[..bytes],
                b"trojan-correct",
                "correct password should relay to target"
            );
        }
        Err(_) => {
            // UnexpectedEof from TLS without close_notify — acceptable for
            // a plain TCP echo server that closes without TLS shutdown.
        }
    }

    drop(tls_stream);
}