relay-core-lib 0.7.0

[Internal] Transport and interception engine for relay-core-runtime. Use `relay-core-runtime` instead.
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
use super::*;
use crate::capture::loop_detection::LoopDetector;
use crate::proxy::http_utils::HttpsClient;
use crate::proxy::outbound::{DirectConnector, OutboundConnector};
use chrono::Utc;
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::{Request, Response, StatusCode};
use hyper_util::client::legacy::Client;
use relay_core_api::flow::Flow;
use relay_core_api::flow::{NetworkInfo, TransportProtocol};
use relay_core_api::policy::{ProxyPolicy, QuicMode};
use std::collections::{BTreeSet, HashMap};
use std::sync::Arc;
use std::sync::Once;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use uuid::Uuid;

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

fn init_crypto() {
    INIT.call_once(|| {
        let _ = rustls::crypto::ring::default_provider().install_default();
    });
}

fn create_test_flow() -> Flow {
    Flow {
        id: Uuid::new_v4(),
        start_time: Utc::now(),
        end_time: None,
        network: NetworkInfo {
            client_ip: "127.0.0.1".to_string(),
            client_port: 12345,
            server_ip: "1.1.1.1".to_string(),
            server_port: 80,
            protocol: TransportProtocol::TCP,
            tls: false,
            tls_version: None,
            sni: None,
        },
        layer: relay_core_api::flow::Layer::Unknown,
        tags: vec![],
        meta: HashMap::new(),
        resilience_trace: None,
        rule_variables: std::collections::HashMap::new(),
        matched_rules: vec![],
    }
}

#[tokio::test]
async fn test_request_timeout() {
    init_crypto();
    // 1. Setup a slow server
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    tokio::spawn(async move {
        if let Ok((mut socket, _)) = listener.accept().await {
            // Read request
            let mut buf = [0; 1024];
            let _ = socket.read(&mut buf).await;
            // Sleep longer than timeout
            tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
            // Write response
            let response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
            let _ = socket.write_all(response.as_bytes()).await;
        }
    });

    // 2. Setup Policy with short timeout
    let policy = ProxyPolicy {
        request_timeout_ms: 100, // 100ms timeout
        ..Default::default()
    };

    // 3. Make request
    let req = Request::builder()
        .method("GET")
        .uri(format!("http://{}", addr))
        .body(Full::new(Bytes::new()))
        .unwrap();

    let loop_detector = Arc::new(LoopDetector::new(BTreeSet::new()));
    let circuit_breaker = Arc::new(crate::proxy::circuit_breaker::CircuitBreaker::default());
    let (tx, _rx) = tokio::sync::mpsc::channel(100);
    let (_tx_policy, rx_policy) = tokio::sync::watch::channel(policy);

    let resp = handle_http_request(
        req,
        "127.0.0.1:12345".parse().unwrap(),
        tx,
        create_dummy_connector(),
        Arc::new(crate::interceptor::NoOpInterceptor),
        false, // is_mitm
        rx_policy,
        None, // target_addr
        loop_detector,
        circuit_breaker,
    )
    .await
    .unwrap();

    // 4. Verify 504 Gateway Timeout
    assert_eq!(resp.status(), StatusCode::GATEWAY_TIMEOUT);
}

fn create_dummy_connector() -> Arc<dyn OutboundConnector> {
    init_crypto();
    let https = hyper_rustls::HttpsConnectorBuilder::new()
        .with_native_roots()
        .unwrap()
        .https_or_http()
        .enable_http1()
        .build();
    let client: HttpsClient = Client::builder(hyper_util::rt::TokioExecutor::new()).build(https);
    Arc::new(DirectConnector::new(Arc::new(client)))
}

#[test]
fn test_quic_downgrade_logic() {
    // 1. Setup Policy (Downgrade Mode)
    let policy = ProxyPolicy {
        quic_mode: QuicMode::Downgrade,
        quic_downgrade_clear_cache: true,
        ..Default::default()
    };

    // 2. Setup Response with Alt-Svc
    let response = Response::builder()
        .header("Alt-Svc", "h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400")
        .body(())
        .unwrap();
    let (mut parts, _) = response.into_parts();

    // 3. Setup Flow
    let mut flow = create_test_flow();

    // 4. Apply Logic
    super::apply_quic_downgrade(&mut parts, &mut flow, &policy);

    // 5. Verify
    // Alt-Svc should be removed
    assert!(parts.headers.get("Alt-Svc").is_none());
    // Tag added
    assert!(flow.tags.contains(&"quic-downgraded".to_string()));
    // Clear-Site-Data added
    if let Some(val) = parts.headers.get("clear-site-data") {
        assert_eq!(val, "\"cache\"");
    } else {
        panic!("clear-site-data header missing");
    }
}

#[test]
fn test_quic_passthrough_logic() {
    // 1. Setup Policy (Passthrough Mode)
    let policy = ProxyPolicy {
        quic_mode: QuicMode::Passthrough,
        ..Default::default()
    };

    // 2. Setup Response with Alt-Svc
    let response = Response::builder()
        .header("Alt-Svc", "h3=\":443\"")
        .body(())
        .unwrap();
    let (mut parts, _) = response.into_parts();
    let mut flow = create_test_flow();

    // 3. Apply Logic
    super::apply_quic_downgrade(&mut parts, &mut flow, &policy);

    // 4. Verify
    // Alt-Svc should remain
    assert!(parts.headers.get("Alt-Svc").is_some());
    // No tag
    assert!(!flow.tags.contains(&"quic-downgraded".to_string()));
}

#[test]
fn test_quic_downgrade_without_clear_cache_does_not_add_header() {
    let policy = ProxyPolicy {
        quic_mode: QuicMode::Downgrade,
        quic_downgrade_clear_cache: false,
        ..Default::default()
    };

    let response = Response::builder()
        .header("Alt-Svc", "h3=\":443\"; ma=86400")
        .body(())
        .unwrap();
    let (mut parts, _) = response.into_parts();
    let mut flow = create_test_flow();

    super::apply_quic_downgrade(&mut parts, &mut flow, &policy);

    assert!(parts.headers.get("Alt-Svc").is_none());
    assert!(flow.tags.contains(&"quic-downgraded".to_string()));
    assert!(
        parts.headers.get("clear-site-data").is_none(),
        "clear-site-data should only be injected when quic_downgrade_clear_cache=true"
    );
}

// ── P3: Timeout classification + circuit breaker integration tests ──

/// P3-01: Verify timeout sets resilience_trace.timeout_type = "total"
#[tokio::test]
async fn test_timeout_sets_trace_timeout_type() {
    init_crypto();
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    tokio::spawn(async move {
        if let Ok((mut socket, _)) = listener.accept().await {
            let mut buf = [0; 1024];
            let _ = socket.read(&mut buf).await;
            tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
            let response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
            let _ = socket.write_all(response.as_bytes()).await;
        }
    });

    let policy = ProxyPolicy {
        request_timeout_ms: 100,
        ..Default::default()
    };

    let req = Request::builder()
        .method("GET")
        .uri(format!("http://{}", addr))
        .body(Full::new(Bytes::new()))
        .unwrap();

    let loop_detector = Arc::new(LoopDetector::new(BTreeSet::new()));
    let circuit_breaker = Arc::new(crate::proxy::circuit_breaker::CircuitBreaker::default());
    let (tx, mut rx) = tokio::sync::mpsc::channel(100);
    let (_tx_policy, rx_policy) = tokio::sync::watch::channel(policy);

    let resp = handle_http_request(
        req,
        "127.0.0.1:12345".parse().unwrap(),
        tx,
        create_dummy_connector(),
        Arc::new(crate::interceptor::NoOpInterceptor),
        false,
        rx_policy,
        None,
        loop_detector,
        circuit_breaker,
    )
    .await
    .unwrap();

    assert_eq!(resp.status(), StatusCode::GATEWAY_TIMEOUT);

    // Verify resilience trace in flow update
    while let Ok(update) = rx.try_recv() {
        if let FlowUpdate::Full(flow) = update {
            if let Some(trace) = flow.resilience_trace.as_ref() {
                assert_eq!(
                    trace.timeout_type.as_deref(),
                    Some("total"),
                    "timeout_type should be 'total'"
                );
                assert!(
                    trace
                        .upstream_errors
                        .iter()
                        .any(|e| e.contains("Timed Out")),
                    "upstream_errors should contain timeout error"
                );
            }
        }
    }
}

/// P3-02: Verify circuit breaker rejection sets resilience_trace.circuit_open = true
/// Use a local echo server so the circuit breaker host key matches.
#[tokio::test]
async fn test_circuit_breaker_sets_trace() {
    init_crypto();

    // Start a local server so the host key matches the request URI authority
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let server_addr = listener.local_addr().unwrap();
    let host_key = format!("127.0.0.1:{}", server_addr.port());

    tokio::spawn(async move {
        // Don't accept — let circuit breaker reject first
        let _ = listener;
    });

    let cb =
        crate::proxy::circuit_breaker::CircuitBreaker::new(1, std::time::Duration::from_secs(30));

    // Record a failure to open the circuit for this exact host
    cb.record_failure(&host_key).await;
    assert!(!cb.allow_request(&host_key).await, "circuit should be open");

    let policy = ProxyPolicy {
        request_timeout_ms: 5000,
        ..Default::default()
    };

    let req = Request::builder()
        .method("GET")
        .uri(format!("http://{}", host_key))
        .body(Full::new(Bytes::new()))
        .unwrap();

    let loop_detector = Arc::new(LoopDetector::new(BTreeSet::new()));
    let (tx, mut rx) = tokio::sync::mpsc::channel(100);
    let (_tx_policy, rx_policy) = tokio::sync::watch::channel(policy);

    let resp = handle_http_request(
        req,
        "127.0.0.1:12345".parse().unwrap(),
        tx,
        create_dummy_connector(),
        Arc::new(crate::interceptor::NoOpInterceptor),
        false,
        rx_policy,
        None,
        loop_detector,
        Arc::new(cb),
    )
    .await
    .unwrap();

    assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);

    // Verify resilience trace
    while let Ok(update) = rx.try_recv() {
        if let FlowUpdate::Full(flow) = update {
            if let Some(trace) = flow.resilience_trace.as_ref() {
                assert!(trace.circuit_open, "circuit_open should be true");
            }
        }
    }
}

/// RE2: Verify Throttle rule wires ThrottleBody into HTTP pipeline.
/// A 10 KB body with 5 KB/s throttle should take roughly 2 seconds.
#[tokio::test]
async fn test_throttle_wires_into_http_pipeline() {
    init_crypto();
    let body_size = 10 * 1024; // 10 KB
    let _bps = 5 * 1024; // 5 KB/s
    let expected_min_ms = 1500; // ~2s minus tolerance

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

    // Echo server: reads request, sends response with the body
    tokio::spawn(async move {
        if let Ok((mut socket, _)) = listener.accept().await {
            let mut buf = vec![0u8; 4096];
            let _ = socket.read(&mut buf).await;
            let body_bytes = vec![b'X'; body_size];
            let response = format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body_size);
            let _ = socket.write_all(response.as_bytes()).await;
            // Write body in chunks to simulate streaming
            for chunk in body_bytes.chunks(1024) {
                let _ = socket.write_all(chunk).await;
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
            }
        }
    });

    let policy = ProxyPolicy {
        request_timeout_ms: 10000,
        ..Default::default()
    };

    let req = Request::builder()
        .method("GET")
        .uri(format!("http://{}", addr))
        .body(Full::new(Bytes::new()))
        .unwrap();

    let loop_detector = Arc::new(LoopDetector::new(BTreeSet::new()));
    let circuit_breaker = Arc::new(crate::proxy::circuit_breaker::CircuitBreaker::default());
    let (tx, _rx) = tokio::sync::mpsc::channel(100);
    let (_tx_policy, rx_policy) = tokio::sync::watch::channel(policy);

    // Inject throttle_bytes_per_sec into flow.meta via interceptor
    // We use a simple interceptor that sets the throttle meta
    let throttle_interceptor = Arc::new(ThrottleTestInterceptor);

    let start = std::time::Instant::now();
    let resp = handle_http_request(
        req,
        "127.0.0.1:12345".parse().unwrap(),
        tx,
        create_dummy_connector(),
        throttle_interceptor,
        false,
        rx_policy,
        None,
        loop_detector,
        circuit_breaker,
    )
    .await
    .unwrap();

    assert_eq!(resp.status(), StatusCode::OK);

    // Consume the body so ThrottleBody actually throttles
    let _body_bytes = http_body_util::BodyExt::collect(resp.into_body())
        .await
        .unwrap()
        .to_bytes();

    let elapsed = start.elapsed();

    // Verify throttle actually slowed down the response
    assert!(
        elapsed.as_millis() >= expected_min_ms as u128,
        "Throttle should slow response: elapsed {}ms, expected >= {}ms",
        elapsed.as_millis(),
        expected_min_ms
    );
}

/// Simple interceptor that injects throttle_bytes_per_sec into flow.meta
struct ThrottleTestInterceptor;

#[async_trait::async_trait]
impl crate::interceptor::Interceptor for ThrottleTestInterceptor {
    async fn on_request(
        &self,
        flow: &mut Flow,
        _body: crate::interceptor::HttpBody,
    ) -> Result<crate::interceptor::RequestAction, Box<dyn std::error::Error + Send + Sync>> {
        // Inject throttle rate: 5 KB/s
        flow.meta
            .insert("throttle_bytes_per_sec".to_string(), (5 * 1024).to_string());
        Ok(crate::interceptor::RequestAction::Continue(_body))
    }

    async fn on_response(
        &self,
        _flow: &mut Flow,
        body: crate::interceptor::HttpBody,
    ) -> Result<crate::interceptor::ResponseAction, Box<dyn std::error::Error + Send + Sync>> {
        Ok(crate::interceptor::ResponseAction::Continue(body))
    }

    async fn on_websocket_message(
        &self,
        _flow: &mut Flow,
        message: relay_core_api::flow::WebSocketMessage,
    ) -> Result<crate::interceptor::WebSocketMessageAction, Box<dyn std::error::Error + Send + Sync>>
    {
        Ok(crate::interceptor::WebSocketMessageAction::Continue(
            message,
        ))
    }
}