rustgate-proxy 0.4.0

MITM-capable HTTP/HTTPS proxy with WebSocket C2 tunneling (SOCKS5, reverse TCP)
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 crate::handler::{
    extract_body_bytes, extract_response_body_bytes, put_body_back, put_response_body_back,
    BoxBody, Buffered, Dropped, RequestHandler,
};
use base64::Engine;
use bytes::Bytes;
use hyper::{Request, Response};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::Write;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{mpsc, Arc, Mutex};
use tracing::info;

/// Unique ID for pairing request and response in the log.
#[derive(Clone, Debug)]
pub struct LogId(pub u64);

/// Upstream target info stored in request extensions for logging.
#[derive(Clone, Debug)]
pub struct UpstreamTarget {
    pub scheme: String,
    pub host: String,
    pub port: u16,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LogEntry {
    pub id: u64,
    pub timestamp_req: String,
    pub timestamp_res: String,
    pub request: LoggedRequest,
    pub response: LoggedResponse,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LoggedRequest {
    pub method: String,
    pub uri: String,
    pub version: String,
    #[serde(default)]
    pub target_scheme: String,
    #[serde(default)]
    pub target_host: String,
    #[serde(default)]
    pub target_port: u16,
    pub headers: Vec<(String, String)>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body_base64: Option<String>,
    pub body_truncated: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LoggedResponse {
    pub status: u16,
    pub version: String,
    pub headers: Vec<(String, String)>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body_base64: Option<String>,
    pub body_truncated: bool,
}

struct PendingLogEntry {
    created_at: std::time::Instant,
    timestamp_req: String,
    request: LoggedRequest,
}

/// Format SystemTime as ISO 8601 UTC string.
fn format_timestamp() -> String {
    let d = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    let secs = d.as_secs();
    let millis = d.subsec_millis();

    // Simple UTC datetime formatting
    let days = secs / 86400;
    let time_secs = secs % 86400;
    let hours = time_secs / 3600;
    let minutes = (time_secs % 3600) / 60;
    let seconds = time_secs % 60;

    // Days since epoch to Y-M-D (simplified leap year calculation)
    let (year, month, day) = days_to_ymd(days);

    format!(
        "{year:04}-{month:02}-{day:02}T{hours:02}:{minutes:02}:{seconds:02}.{millis:03}Z"
    )
}

fn days_to_ymd(mut days: u64) -> (u64, u64, u64) {
    let mut year = 1970;
    loop {
        let days_in_year = if is_leap(year) { 366 } else { 365 };
        if days < days_in_year {
            break;
        }
        days -= days_in_year;
        year += 1;
    }
    let month_days = if is_leap(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };
    let mut month = 1;
    for &md in &month_days {
        if days < md {
            break;
        }
        days -= md;
        month += 1;
    }
    (year, month, days + 1)
}

fn is_leap(y: u64) -> bool {
    (y.is_multiple_of(4) && !y.is_multiple_of(100)) || y.is_multiple_of(400)
}

/// Encode body bytes for logging.
/// Returns (body_text, body_base64, body_truncated).
/// `may_have_body` is true if the message had Content-Length or Transfer-Encoding,
/// indicating a body was expected but may not have been captured.
fn encode_body(
    bytes: &Bytes,
    is_buffered: bool,
    may_have_body: bool,
) -> (Option<String>, Option<String>, bool) {
    if !is_buffered || bytes.is_empty() {
        // truncated if body was expected but we couldn't buffer it
        let truncated = !is_buffered && may_have_body;
        return (None, None, truncated);
    }
    match std::str::from_utf8(bytes) {
        Ok(text) => (Some(text.to_string()), None, false),
        Err(_) => {
            let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
            (None, Some(b64), false)
        }
    }
}

/// Safe headers that are logged verbatim. All others are redacted to prevent
/// credential persistence (Authorization, Cookie, vendor API keys, etc.).
const SAFE_LOG_HEADERS: &[&str] = &[
    "accept", "accept-encoding", "accept-language", "cache-control",
    "connection", "content-encoding", "content-language", "content-length",
    "content-type", "date", "etag", "expires", "host", "if-match",
    "if-modified-since", "if-none-match", "if-unmodified-since",
    "last-modified", "location", "pragma", "range", "server",
    "transfer-encoding", "user-agent", "vary", "via",
    "access-control-allow-origin", "access-control-allow-methods",
    "access-control-allow-headers", "access-control-max-age",
    "x-content-type-options", "x-frame-options", "x-request-id",
    "strict-transport-security", "content-security-policy",
];

fn capture_headers(headers: &hyper::HeaderMap) -> Vec<(String, String)> {
    headers
        .iter()
        .map(|(name, value)| {
            let val = if SAFE_LOG_HEADERS.iter().any(|h| name.as_str().eq_ignore_ascii_case(h)) {
                value.to_str().unwrap_or("<binary>").to_string()
            } else {
                "<redacted>".to_string()
            };
            (name.to_string(), val)
        })
        .collect()
}

/// Redact query parameter values in a URI to prevent credential persistence.
/// `/path?key=secret&token=xxx` → `/path?key=<redacted>&token=<redacted>`
fn redact_query_values(uri: &hyper::Uri) -> String {
    let path = uri.path();
    match uri.query() {
        None => path.to_string(),
        Some(query) => {
            let redacted: Vec<String> = query
                .split('&')
                .map(|pair| {
                    if let Some((key, _)) = pair.split_once('=') {
                        format!("{key}=<redacted>")
                    } else {
                        pair.to_string()
                    }
                })
                .collect();
            format!("{path}?{}", redacted.join("&"))
        }
    }
}

/// Background writer thread that receives LogEntry values and writes JSON Lines.
struct LogWriter {
    rx: mpsc::Receiver<LogEntry>,
    file: std::io::BufWriter<std::fs::File>,
}

impl LogWriter {
    fn run(mut self) {
        while let Ok(entry) = self.rx.recv() {
            match serde_json::to_string(&entry) {
                Ok(json) => {
                    if let Err(e) = writeln!(self.file, "{json}") {
                        eprintln!("[rustgate] Traffic log write error: {e}");
                    }
                    if let Err(e) = self.file.flush() {
                        eprintln!("[rustgate] Traffic log flush error: {e}");
                    }
                }
                Err(e) => {
                    eprintln!("[rustgate] Traffic log serialize error: {e}");
                }
            }
        }
    }
}

/// Decorator handler that logs traffic to a JSON Lines file.
/// Wraps any inner RequestHandler.
pub struct TrafficLogHandler {
    inner: Arc<dyn RequestHandler>,
    tx: mpsc::SyncSender<LogEntry>,
    next_id: AtomicU64,
    pending: Mutex<HashMap<u64, PendingLogEntry>>,
}

impl TrafficLogHandler {
    pub fn new(
        inner: Arc<dyn RequestHandler>,
        path: &Path,
    ) -> std::io::Result<Self> {
        // Reject symlinks to prevent writing to unintended locations
        #[cfg(unix)]
        if let Ok(meta) = std::fs::symlink_metadata(path) {
            if meta.file_type().is_symlink() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    format!("Refusing to write log to symlink: {}", path.display()),
                ));
            }
        }

        // Create with restricted permissions (owner-only on Unix)
        #[cfg(unix)]
        let file = {
            use std::os::unix::fs::OpenOptionsExt;
            use std::os::unix::fs::PermissionsExt;
            let f = std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .mode(0o600)
                .open(path)?;
            // Force 0o600 even if the file already existed with broader permissions
            f.set_permissions(std::fs::Permissions::from_mode(0o600))?;
            f
        };
        #[cfg(not(unix))]
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)?;
        let writer = std::io::BufWriter::new(file);
        let (tx, rx) = mpsc::sync_channel(256);

        std::thread::spawn(move || {
            LogWriter { rx, file: writer }.run();
        });

        info!("Traffic logging to {}", path.display());

        Ok(Self {
            inner,
            tx,
            next_id: AtomicU64::new(1),
            pending: Mutex::new(HashMap::new()),
        })
    }
}

impl RequestHandler for TrafficLogHandler {
    fn handle_request(&self, req: &mut Request<BoxBody>) {
        // Let inner handler process first (e.g., InterceptHandler may modify/drop)
        self.inner.handle_request(req);

        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        let is_buffered = req.extensions().get::<Buffered>().is_some();
        let is_dropped = req.extensions().get::<Dropped>().is_some();

        // Capture request data (after inner handler's modifications)
        let body_bytes = if is_buffered && !is_dropped {
            let b = extract_body_bytes(req);
            put_body_back(req, b.clone());
            b
        } else {
            Bytes::new()
        };

        let may_have_body = req.headers().contains_key(hyper::header::CONTENT_LENGTH)
            || req.headers().contains_key(hyper::header::TRANSFER_ENCODING);
        let (body, body_base64, body_truncated) = encode_body(&body_bytes, is_buffered, may_have_body);

        let upstream = req.extensions().get::<UpstreamTarget>().cloned();
        let logged_req = LoggedRequest {
            method: req.method().to_string(),
            uri: redact_query_values(req.uri()),
            version: format!("{:?}", req.version()),
            target_scheme: upstream.as_ref().map(|t| t.scheme.clone()).unwrap_or_default(),
            target_host: upstream.as_ref().map(|t| t.host.clone()).unwrap_or_default(),
            target_port: upstream.as_ref().map(|t| t.port).unwrap_or(0),
            headers: capture_headers(req.headers()),
            body,
            body_base64,
            body_truncated,
        };

        // If dropped, emit log entry immediately with synthetic response
        if is_dropped {
            let entry = LogEntry {
                id,
                timestamp_req: format_timestamp(),
                timestamp_res: format_timestamp(),
                request: logged_req,
                response: LoggedResponse {
                    status: 0,
                    version: String::new(),
                    headers: Vec::new(),
                    body: None,
                    body_base64: None,
                    body_truncated: true,
                },
            };
            if self.tx.try_send(entry).is_err() {
                tracing::warn!("Traffic log queue full, entry dropped");
            }
            return;
        }

        // Store pending for pairing with response
        req.extensions_mut().insert(LogId(id));
        if let Ok(mut pending) = self.pending.lock() {
            // Expire stale entries (>60s) instead of evicting live ones
            let now = std::time::Instant::now();
            let expired: Vec<u64> = pending
                .iter()
                .filter(|(_, v)| now.duration_since(v.created_at).as_secs() > 300)
                .map(|(k, _)| *k)
                .collect();
            for eid in &expired {
                if let Some(stale) = pending.remove(eid) {
                    tracing::warn!("Expired unpaired log entry {eid} (>300s)");
                    // Emit synthetic timeout entry
                    let entry = LogEntry {
                        id: *eid,
                        timestamp_req: stale.timestamp_req,
                        timestamp_res: format_timestamp(),
                        request: stale.request,
                        response: LoggedResponse {
                            status: 0,
                            version: String::new(),
                            headers: Vec::new(),
                            body: None,
                            body_base64: None,
                            body_truncated: true,
                        },
                    };
                    if self.tx.try_send(entry).is_err() {
                        tracing::warn!("Traffic log queue full, expired entry dropped");
                    }
                }
            }
            pending.insert(id, PendingLogEntry {
                created_at: now,
                timestamp_req: format_timestamp(),
                request: logged_req,
            });
        }
    }

    fn handle_response(&self, res: &mut Response<BoxBody>) {
        let log_id = res.extensions().get::<LogId>().cloned();

        // Let inner handler process response FIRST (e.g., interceptor may edit/drop)
        self.inner.handle_response(res);

        // Now capture the final post-interception state for logging
        let is_buffered = res.extensions().get::<Buffered>().is_some();
        let is_dropped = res.extensions().get::<Dropped>().is_some();

        let body_bytes = if is_buffered && !is_dropped {
            let b = extract_response_body_bytes(res);
            put_response_body_back(res, b.clone());
            b
        } else {
            Bytes::new()
        };

        let may_have_body = res.headers().contains_key(hyper::header::CONTENT_LENGTH)
            || res.headers().contains_key(hyper::header::TRANSFER_ENCODING);
        let (body, body_base64, body_truncated) = encode_body(&body_bytes, is_buffered, may_have_body);

        let logged_res = LoggedResponse {
            status: if is_dropped { 0 } else { res.status().as_u16() },
            version: format!("{:?}", res.version()),
            headers: if is_dropped { Vec::new() } else { capture_headers(res.headers()) },
            body,
            body_base64,
            body_truncated: body_truncated || is_dropped,
        };

        // Pair with pending request
        if let Some(LogId(id)) = log_id {
            let pending_entry = self.pending.lock().ok().and_then(|mut p| p.remove(&id));
            if let Some(pending) = pending_entry {
                let entry = LogEntry {
                    id,
                    timestamp_req: pending.timestamp_req,
                    timestamp_res: format_timestamp(),
                    request: pending.request,
                    response: logged_res,
                };
                if self.tx.try_send(entry).is_err() {
                tracing::warn!("Traffic log queue full, entry dropped");
            }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_log_entry_serde_roundtrip() {
        let entry = LogEntry {
            id: 1,
            timestamp_req: "2026-04-11T12:00:00.000Z".into(),
            timestamp_res: "2026-04-11T12:00:00.123Z".into(),
            request: LoggedRequest {
                method: "GET".into(),
                uri: "/api".into(),
                version: "HTTP/1.1".into(),
                target_scheme: "https".into(),
                target_host: "example.com".into(),
                target_port: 443,
                headers: vec![("host".into(), "example.com".into())],
                body: None,
                body_base64: None,
                body_truncated: false,
            },
            response: LoggedResponse {
                status: 200,
                version: "HTTP/1.1".into(),
                headers: vec![("content-type".into(), "application/json".into())],
                body: Some("{\"ok\":true}".into()),
                body_base64: None,
                body_truncated: false,
            },
        };
        let json = serde_json::to_string(&entry).unwrap();
        let parsed: LogEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.id, 1);
        assert_eq!(parsed.request.method, "GET");
        assert_eq!(parsed.response.status, 200);
    }

    #[test]
    fn test_encode_body_utf8() {
        let bytes = Bytes::from("hello world");
        let (body, b64, trunc) = encode_body(&bytes, true, true);
        assert_eq!(body.unwrap(), "hello world");
        assert!(b64.is_none());
        assert!(!trunc);
    }

    #[test]
    fn test_encode_body_binary() {
        let bytes = Bytes::from(vec![0xFF, 0xFE, 0x00, 0x01]);
        let (body, b64, trunc) = encode_body(&bytes, true, true);
        assert!(body.is_none());
        assert!(b64.is_some());
        assert!(!trunc);
    }

    #[test]
    fn test_encode_body_not_buffered_with_cl() {
        // Has Content-Length but wasn't buffered → truncated
        let bytes = Bytes::new();
        let (body, b64, trunc) = encode_body(&bytes, false, true);
        assert!(body.is_none());
        assert!(b64.is_none());
        assert!(trunc);
    }

    #[test]
    fn test_encode_body_not_buffered_no_cl() {
        // No Content-Length, not buffered → NOT truncated (bodyless request)
        let bytes = Bytes::new();
        let (body, b64, trunc) = encode_body(&bytes, false, false);
        assert!(body.is_none());
        assert!(b64.is_none());
        assert!(!trunc);
    }

    #[test]
    fn test_format_timestamp() {
        let ts = format_timestamp();
        assert!(ts.ends_with('Z'));
        assert!(ts.contains('T'));
    }
}