lingxia-proxy 0.6.0

Local HTTP CONNECT proxy with PAC routing and optional MITM capture
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
// Structured HTTP/1.1 capture over decrypted streams (MITM or plain HTTP).
//
// Consumer API:
//   let mut rx = proxy.session_receiver();           // Receiver<Arc<CapturedSession>>
//   while let Ok(s) = rx.recv().await {
//       if s.host.ends_with("openai.com") { ... }   // one-line filter
//       let json = serde_json::to_value(&*s)?;       // ready for LLM / UI
//   }

use serde::Serialize;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::sync::broadcast;

// ── Session counter ───────────────────────────────────────────────────────

static SESSION_COUNTER: AtomicU64 = AtomicU64::new(1);
fn next_id() -> u64 {
    SESSION_COUNTER.fetch_add(1, Ordering::Relaxed)
}
fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as u64
}

// ── Public types ──────────────────────────────────────────────────────────

/// One complete HTTP request/response exchange.
///
/// Serialises directly to JSON — pass `serde_json::to_value(&session)` to
/// any agent or LLM without further transformation.
#[derive(Debug, Clone, Serialize)]
pub struct CapturedSession {
    pub id: u64,
    pub host: String,
    pub port: u16,
    /// `true` when the bytes were originally TLS-encrypted (HTTPS MITM).
    pub decrypted: bool,
    pub request: CapturedRequest,
    pub response: Option<CapturedResponse>,
    pub timing: SessionTiming,
}

#[derive(Debug, Clone, Serialize)]
pub struct CapturedRequest {
    pub method: String,
    pub path: String,
    pub version: String,
    pub headers: Vec<(String, String)>,
    pub body: CapturedBody,
}

#[derive(Debug, Clone, Serialize)]
pub struct CapturedResponse {
    pub status: u16,
    pub reason: String,
    pub version: String,
    pub headers: Vec<(String, String)>,
    pub body: CapturedBody,
}

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CapturedBody {
    Text {
        content: String,
    },
    /// Body was binary or exceeded `MAX_BODY`.  `preview_hex` = first 32 bytes.
    Binary {
        size: usize,
        preview_hex: String,
    },
    Empty,
}

#[derive(Debug, Clone, Serialize)]
pub struct SessionTiming {
    pub request_start_ms: u64,
    pub response_start_ms: Option<u64>,
    pub response_end_ms: Option<u64>,
}

// ── Constants ─────────────────────────────────────────────────────────────

const MAX_HEADER: usize = 64 * 1024; // 64 KB — plenty for any HTTP header set
const MAX_BODY: usize = 1024 * 1024; // 1 MB  — cap captured body size

// ── Main entry point ──────────────────────────────────────────────────────

/// Drive HTTP/1.1 session parsing on two already-decrypted async streams.
///
/// `client` — stream facing the browser.
/// `server` — stream facing the upstream server.
///
/// Both streams are used bidirectionally: requests are forwarded from client
/// to server, responses from server to client, while bytes are captured for
/// structured emission.
pub async fn handle_http_sessions<C, S>(
    host: String,
    port: u16,
    decrypted: bool,
    mut client: C,
    mut server: S,
    tx: broadcast::Sender<Arc<CapturedSession>>,
) -> std::io::Result<()>
where
    C: AsyncRead + AsyncWrite + Unpin,
    S: AsyncRead + AsyncWrite + Unpin,
{
    loop {
        let t_req_start = now_ms();

        // ── REQUEST ───────────────────────────────────────────────────────

        let raw_req = match read_until_crlf2(&mut client, MAX_HEADER).await {
            Ok(b) => b,
            Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
            Err(e) => return Err(e),
        };

        let (method, path, version, req_headers) = parse_request_head(&raw_req)?;

        server.write_all(&raw_req).await?;

        let mut req_body_raw = Vec::new();
        let req_body_total = forward_body(
            &mut client,
            &mut server,
            &req_headers,
            &mut req_body_raw,
            MAX_BODY,
        )
        .await?;

        let request = CapturedRequest {
            method: method.clone(),
            path,
            version,
            headers: req_headers.clone(),
            body: make_body(req_body_raw, req_body_total),
        };

        // ── RESPONSE ──────────────────────────────────────────────────────

        let raw_resp = match read_until_crlf2(&mut server, MAX_HEADER).await {
            Ok(b) => b,
            Err(e) => return Err(e),
        };
        let t_resp_start = now_ms();

        let (status, reason, resp_version, resp_headers) = parse_response_head(&raw_resp)?;

        client.write_all(&raw_resp).await?;

        let mut resp_body_raw = Vec::new();
        // RFC 7230 §3.3: no body for 1xx, 204, 304 or HEAD responses.
        let resp_body_total = if matches!(status, 100..=199 | 204 | 304) || method == "HEAD" {
            0
        } else {
            forward_body(
                &mut server,
                &mut client,
                &resp_headers,
                &mut resp_body_raw,
                MAX_BODY,
            )
            .await?
        };
        let t_resp_end = now_ms();

        let response = CapturedResponse {
            status,
            reason,
            version: resp_version.clone(),
            headers: resp_headers.clone(),
            body: make_body(resp_body_raw, resp_body_total),
        };

        // ── EMIT ──────────────────────────────────────────────────────────

        if tx.receiver_count() > 0 {
            let _ = tx.send(Arc::new(CapturedSession {
                id: next_id(),
                host: host.clone(),
                port,
                decrypted,
                request,
                response: Some(response),
                timing: SessionTiming {
                    request_start_ms: t_req_start,
                    response_start_ms: Some(t_resp_start),
                    response_end_ms: Some(t_resp_end),
                },
            }));
        }

        // ── KEEP-ALIVE CHECK ──────────────────────────────────────────────

        let req_close = header_value(&req_headers, "connection").eq_ignore_ascii_case("close");
        let resp_close = header_value(&resp_headers, "connection").eq_ignore_ascii_case("close");
        let http10 = resp_version == "HTTP/1.0";

        if req_close || resp_close || http10 {
            break;
        }
    }

    Ok(())
}

// ── HTTP head parsers ─────────────────────────────────────────────────────

fn parse_request_head(
    buf: &[u8],
) -> std::io::Result<(String, String, String, Vec<(String, String)>)> {
    let mut storage = [httparse::EMPTY_HEADER; 96];
    let mut req = httparse::Request::new(&mut storage);
    req.parse(buf)
        .map_err(|e| invalid_data(format!("request parse: {e:?}")))?;

    let method = req.method.unwrap_or("").to_string();
    let path = req.path.unwrap_or("").to_string();
    let version = if req.version == Some(1) {
        "HTTP/1.1"
    } else {
        "HTTP/1.0"
    }
    .to_string();
    let headers = collect_headers(req.headers);

    Ok((method, path, version, headers))
}

fn parse_response_head(
    buf: &[u8],
) -> std::io::Result<(u16, String, String, Vec<(String, String)>)> {
    let mut storage = [httparse::EMPTY_HEADER; 96];
    let mut resp = httparse::Response::new(&mut storage);
    resp.parse(buf)
        .map_err(|e| invalid_data(format!("response parse: {e:?}")))?;

    let status = resp.code.unwrap_or(0);
    let reason = resp.reason.unwrap_or("").to_string();
    let version = if resp.version == Some(1) {
        "HTTP/1.1"
    } else {
        "HTTP/1.0"
    }
    .to_string();
    let headers = collect_headers(resp.headers);

    Ok((status, reason, version, headers))
}

fn collect_headers(raw: &[httparse::Header<'_>]) -> Vec<(String, String)> {
    raw.iter()
        .filter(|h| !h.name.is_empty())
        .map(|h| {
            (
                h.name.to_string(),
                String::from_utf8_lossy(h.value).into_owned(),
            )
        })
        .collect()
}

// ── Body I/O ──────────────────────────────────────────────────────────────

/// Read the body declared by `headers` from `reader`, forward every byte to
/// `writer`, and accumulate up to `max_capture` bytes into `cap`.
/// Returns the total number of body bytes transferred.
async fn forward_body<R, W>(
    reader: &mut R,
    writer: &mut W,
    headers: &[(String, String)],
    cap: &mut Vec<u8>,
    max_capture: usize,
) -> std::io::Result<usize>
where
    R: AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let is_chunked = header_value(headers, "transfer-encoding")
        .to_ascii_lowercase()
        .contains("chunked");

    let content_length = header_value(headers, "content-length")
        .trim()
        .parse::<usize>()
        .ok();

    if is_chunked {
        forward_chunked(reader, writer, cap, max_capture).await
    } else if let Some(len) = content_length {
        if len == 0 {
            return Ok(0);
        }
        forward_exact(reader, writer, len, cap, max_capture).await
    } else {
        Ok(0)
    }
}

/// Read exactly `len` bytes, forwarding each chunk and accumulating into `cap`.
async fn forward_exact<R, W>(
    reader: &mut R,
    writer: &mut W,
    len: usize,
    cap: &mut Vec<u8>,
    max_capture: usize,
) -> std::io::Result<usize>
where
    R: AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let mut buf = vec![0u8; 8192.min(len)];
    let mut done = 0;

    while done < len {
        let want = (len - done).min(buf.len());
        let n = reader.read(&mut buf[..want]).await?;
        if n == 0 {
            break;
        }
        writer.write_all(&buf[..n]).await?;
        done += n;
        capture_bytes(cap, &buf[..n], max_capture);
    }

    Ok(done)
}

/// Read HTTP/1.1 chunked encoding, forward raw wire bytes, accumulate decoded
/// body data into `cap`.
async fn forward_chunked<R, W>(
    reader: &mut R,
    writer: &mut W,
    cap: &mut Vec<u8>,
    max_capture: usize,
) -> std::io::Result<usize>
where
    R: AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let mut total = 0;

    loop {
        // Read chunk-size line (hex digits, optional extensions, terminated \r\n)
        let size_line = read_line(reader, 128).await?;
        writer.write_all(&size_line).await?;

        let hex = std::str::from_utf8(&size_line)
            .unwrap_or("")
            .trim()
            .split(';')
            .next()
            .unwrap_or("0")
            .trim();

        let chunk_size = usize::from_str_radix(hex, 16)
            .map_err(|_| invalid_data(format!("bad chunk size: {hex:?}")))?;

        if chunk_size == 0 {
            // trailing headers + final \r\n
            let trailer = read_until_crlf2(reader, 4096).await.unwrap_or_default();
            writer.write_all(&trailer).await?;
            break;
        }

        // Read chunk data
        total += forward_exact(reader, writer, chunk_size, cap, max_capture).await?;

        // Each chunk is followed by \r\n
        let mut crlf = [0u8; 2];
        reader.read_exact(&mut crlf).await?;
        writer.write_all(&crlf).await?;
    }

    Ok(total)
}

// ── Low-level readers ─────────────────────────────────────────────────────

/// Read bytes until `\r\n\r\n` (inclusive).  Returns the full header block.
pub(crate) async fn read_until_crlf2<R: AsyncRead + Unpin>(
    reader: &mut R,
    max: usize,
) -> std::io::Result<Vec<u8>> {
    let mut buf = Vec::with_capacity(512);
    let mut byte = [0u8; 1];

    loop {
        reader.read_exact(&mut byte).await?;
        buf.push(byte[0]);
        if buf.ends_with(b"\r\n\r\n") {
            return Ok(buf);
        }
        if buf.len() >= max {
            return Err(invalid_data("HTTP headers too large"));
        }
    }
}

/// Read bytes until `\r\n` (inclusive), up to `max` bytes.
async fn read_line<R: AsyncRead + Unpin>(reader: &mut R, max: usize) -> std::io::Result<Vec<u8>> {
    let mut buf = Vec::with_capacity(32);
    let mut byte = [0u8; 1];

    loop {
        reader.read_exact(&mut byte).await?;
        buf.push(byte[0]);
        if buf.ends_with(b"\r\n") {
            return Ok(buf);
        }
        if buf.len() >= max {
            return Err(invalid_data("chunk-size line too long"));
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────

fn header_value<'a>(headers: &'a [(String, String)], name: &str) -> &'a str {
    headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case(name))
        .map(|(_, v)| v.as_str())
        .unwrap_or("")
}

fn capture_bytes(cap: &mut Vec<u8>, data: &[u8], max: usize) {
    if cap.len() < max {
        let room = max - cap.len();
        cap.extend_from_slice(&data[..data.len().min(room)]);
    }
}

fn make_body(data: Vec<u8>, total: usize) -> CapturedBody {
    if total == 0 {
        return CapturedBody::Empty;
    }

    // If we captured the whole body, try UTF-8.
    if data.len() == total {
        match String::from_utf8(data) {
            Ok(s) => return CapturedBody::Text { content: s },
            Err(e) => {
                let raw = e.into_bytes();
                let preview_hex = raw
                    .iter()
                    .take(32)
                    .map(|b| format!("{b:02x}"))
                    .collect::<Vec<_>>()
                    .join(" ");
                return CapturedBody::Binary {
                    size: total,
                    preview_hex,
                };
            }
        }
    }

    // Truncated capture.
    let preview_hex = data
        .iter()
        .take(32)
        .map(|b| format!("{b:02x}"))
        .collect::<Vec<_>>()
        .join(" ");
    CapturedBody::Binary {
        size: total,
        preview_hex,
    }
}

fn invalid_data(msg: impl Into<String>) -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::InvalidData, msg.into())
}