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
use async_trait::async_trait;
use bytes::Bytes;
use http_body_util::{BodyExt, Limited};
use hyper::{Request, Response};
use proxyapi_models::{ProxiedRequest, ProxiedResponse};
use std::sync::Arc;
use tokio::sync::mpsc;
use crate::body::{self, ProxyBody};
use crate::event::{next_id, ProxyEvent};
use crate::intercept::{InterceptConfig, InterceptDecision};
use crate::{HttpContext, HttpHandler, RequestOrResponse};
/// Maximum body size the proxy will collect (100 MB).
///
/// Bodies exceeding this limit are replaced with an empty body in the
/// captured event. The proxied traffic itself is unaffected.
const MAX_BODY_SIZE: usize = 100 * 1024 * 1024;
fn now_millis() -> i64 {
chrono::Local::now().timestamp_millis()
}
/// Collect the response body, emit a [`ProxyEvent`], and return the reconstructed response.
///
/// This is the single source of truth for response capture, used by both
/// forward and reverse proxy paths. When scripting is enabled, the Lua
/// `on_response` hook is called here before building the final response.
pub fn collect_and_emit(
handler: &mut CapturingHandler,
#[allow(unused_mut)] mut parts: http::response::Parts,
#[allow(unused_mut)] mut body_bytes: Bytes,
) -> Response<ProxyBody> {
// Run Lua on_response hook (if scripting is enabled and a script is loaded).
#[cfg(feature = "scripting")]
if let Some(ref engine) = handler.script_engine {
let (req_method, req_url) = handler
.captured_request
.as_ref()
.map(|r| (r.method().as_str().to_owned(), r.uri().to_string()))
.unwrap_or_default();
match engine.on_response(
&req_method,
&req_url,
parts.status.as_u16(),
&parts.headers,
&body_bytes,
) {
Ok(crate::scripting::ScriptResponseAction::Modified {
status,
headers,
body,
}) => {
if let Ok(s) = http::StatusCode::from_u16(status) {
parts.status = s;
}
parts.headers = headers;
body_bytes = body;
}
Ok(crate::scripting::ScriptResponseAction::PassThrough) => {}
Err(e) => {
tracing::warn!("Lua on_response error (passing through): {e}");
}
}
}
let proxied_response = ProxiedResponse::new(
parts.status,
parts.version,
parts.headers.clone(),
body_bytes.clone(),
now_millis(),
);
if let Some(request) = handler.take_captured_request() {
// Use the ID assigned at the start of handle_request (intercept flow)
// so that RequestIntercepted and RequestComplete share the same ID.
// Fall back to next_id() for the normal (non-intercept) path.
let id = handler.pending_id.take().unwrap_or_else(next_id);
let event = ProxyEvent::RequestComplete {
id,
request: Box::new(request),
response: Box::new(proxied_response),
};
handler.send_event(event);
}
Response::from_parts(parts, body::full(body_bytes))
}
/// Collect response body bytes up to [`MAX_BODY_SIZE`], logging a warning on failure.
pub async fn collect_body(body: hyper::body::Incoming) -> Bytes {
Limited::new(body, MAX_BODY_SIZE)
.collect()
.await
.map(http_body_util::Collected::to_bytes)
.unwrap_or_else(|e| {
tracing::warn!(
"Failed to collect response body (possibly exceeds {}MB limit): {e}",
MAX_BODY_SIZE / (1024 * 1024)
);
Bytes::new()
})
}
/// Default handler that captures request/response pairs and emits [`ProxyEvent`]s.
///
/// When the `scripting` feature is enabled and a [`ScriptEngine`] is attached,
/// Lua `on_request` / `on_response` hooks are called for every request/response.
#[derive(Clone)]
pub struct CapturingHandler {
event_tx: mpsc::Sender<ProxyEvent>,
captured_request: Option<ProxiedRequest>,
/// ID assigned at the start of `handle_request`. Carried through to
/// `collect_and_emit` so that `RequestIntercepted` and `RequestComplete`
/// events share the same ID and the UI can correlate them.
pending_id: Option<u64>,
intercept: Option<Arc<InterceptConfig>>,
#[cfg(feature = "scripting")]
script_engine: Option<Arc<crate::scripting::ScriptEngine>>,
}
impl std::fmt::Debug for CapturingHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CapturingHandler")
.field("event_tx", &self.event_tx)
.field("captured_request", &self.captured_request)
.field("pending_id", &self.pending_id)
.finish_non_exhaustive()
}
}
impl CapturingHandler {
/// Create a new handler that sends events to the given channel.
#[must_use]
pub fn new(event_tx: mpsc::Sender<ProxyEvent>) -> Self {
Self {
event_tx,
captured_request: None,
pending_id: None,
intercept: None,
#[cfg(feature = "scripting")]
script_engine: None,
}
}
/// Attach an intercept controller for interactive request/response editing.
#[must_use]
pub fn with_intercept(mut self, cfg: Arc<InterceptConfig>) -> Self {
self.intercept = Some(cfg);
self
}
/// Attach a Lua script engine for request/response transformation.
#[cfg(feature = "scripting")]
#[must_use]
pub fn with_script_engine(mut self, engine: Arc<crate::scripting::ScriptEngine>) -> Self {
self.script_engine = Some(engine);
self
}
pub(crate) fn take_captured_request(&mut self) -> Option<ProxiedRequest> {
self.captured_request.take()
}
/// Run intercept logic for a replayed request and return it ready to forward.
///
/// Unlike [`HttpHandler::handle_request`], this takes an already-captured
/// [`ProxiedRequest`] so no body collection is needed. Returns `None` if the
/// request was blocked or the intercept timed out.
pub(crate) async fn handle_replayed_request(
&mut self,
req: ProxiedRequest,
) -> Option<Request<ProxyBody>> {
let id = next_id();
self.pending_id = Some(id);
let mut method = req.method().clone();
let mut uri = req.uri().clone();
let version = req.version();
let mut headers = req.headers().clone();
let mut body_bytes = req.body().clone();
self.captured_request = Some(ProxiedRequest::new(
method.clone(),
uri.clone(),
version,
headers.clone(),
body_bytes.clone(),
now_millis(),
));
if let Some(ref cfg) = self.intercept {
if cfg.is_enabled() {
let snapshot = self.captured_request.clone().unwrap();
let rx = cfg.register(id);
if self
.event_tx
.try_send(ProxyEvent::RequestIntercepted {
id,
request: Box::new(snapshot),
})
.is_err()
{
cfg.resolve(id, InterceptDecision::Forward);
tracing::warn!("Event channel full, skipping intercept for replay id={id}");
} else {
match tokio::time::timeout(std::time::Duration::from_secs(300), rx).await {
Ok(Ok(InterceptDecision::Forward)) => {}
Ok(Ok(InterceptDecision::Modified {
method: m,
uri: u,
headers: h,
body: b,
})) => {
if let Ok(m) = m.parse() {
method = m;
}
if let Ok(u) = u.parse() {
uri = u;
}
headers = h;
body_bytes = b;
self.captured_request = Some(ProxiedRequest::new(
method.clone(),
uri.clone(),
version,
headers.clone(),
body_bytes.clone(),
now_millis(),
));
}
Ok(Ok(InterceptDecision::Block { status, body })) => {
let status_code = http::StatusCode::from_u16(status)
.unwrap_or(http::StatusCode::BAD_GATEWAY);
let (parts, _) = Response::<()>::builder()
.status(status_code)
.body(())
.unwrap()
.into_parts();
collect_and_emit(self, parts, body);
return None;
}
_ => {
tracing::warn!("Intercept timed out for replay id={id}");
return None;
}
}
}
}
}
let mut builder = Request::builder().method(method).uri(uri).version(version);
if let Some(h) = builder.headers_mut() {
*h = headers;
}
builder.body(body::full(body_bytes)).ok()
}
pub(crate) fn send_event(&self, event: ProxyEvent) {
match self.event_tx.try_send(event) {
Ok(()) => {}
Err(mpsc::error::TrySendError::Full(_)) => {
tracing::warn!("Event channel full, dropping event");
}
Err(mpsc::error::TrySendError::Closed(_)) => {
tracing::debug!("Event channel closed");
}
}
}
}
#[async_trait]
impl HttpHandler for CapturingHandler {
async fn handle_request(
&mut self,
_ctx: &HttpContext,
req: Request<hyper::body::Incoming>,
) -> RequestOrResponse {
// Assign a stable ID at request start so that RequestIntercepted and
// RequestComplete events for the same flow share the same ID.
let id = next_id();
self.pending_id = Some(id);
let (mut parts, incoming) = req.into_parts();
let mut body_bytes = Limited::new(incoming, MAX_BODY_SIZE)
.collect()
.await
.map(http_body_util::Collected::to_bytes)
.unwrap_or_else(|e| {
tracing::warn!("Failed to collect request body: {e}");
Bytes::new()
});
// Run Lua on_request hook (if scripting is enabled and a script is loaded).
// This runs synchronously — the body has already been collected above.
#[cfg(feature = "scripting")]
if let Some(ref engine) = self.script_engine {
match engine.on_request(
parts.method.as_str(),
&parts.uri.to_string(),
&parts.headers,
&body_bytes,
) {
Ok(crate::scripting::ScriptRequestAction::Forward {
method,
url,
headers,
body,
}) => {
if let Ok(m) = method.parse() {
parts.method = m;
}
if let Ok(u) = url.parse() {
parts.uri = u;
}
parts.headers = headers;
body_bytes = body;
}
Ok(crate::scripting::ScriptRequestAction::ShortCircuit {
status,
headers,
body,
}) => {
// Capture the original request before short-circuiting
let proxied_request = ProxiedRequest::new(
parts.method.clone(),
parts.uri.clone(),
parts.version,
parts.headers.clone(),
body_bytes.clone(),
now_millis(),
);
self.captured_request = Some(proxied_request);
let status_code = http::StatusCode::from_u16(status)
.unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR);
let mut builder = Response::builder().status(status_code);
if let Some(h) = builder.headers_mut() {
*h = headers;
}
let response = builder
.body(body::full(body))
.unwrap_or_else(|_| Response::new(body::empty()));
return RequestOrResponse::Response(response);
}
Ok(crate::scripting::ScriptRequestAction::PassThrough) => {}
Err(e) => {
tracing::warn!("Lua on_request error (passing through): {e}");
}
}
}
// Intercept mode: pause the request and wait for a UI decision.
if let Some(ref cfg) = self.intercept {
if cfg.is_enabled() {
let snapshot = ProxiedRequest::new(
parts.method.clone(),
parts.uri.clone(),
parts.version,
parts.headers.clone(),
body_bytes.clone(),
now_millis(),
);
// Register before sending the event so the UI can always resolve.
let rx = cfg.register(id);
let event = ProxyEvent::RequestIntercepted {
id,
request: Box::new(snapshot.clone()),
};
// Non-blocking send: if the channel is full, skip interception
// and fall through to normal forwarding so the request isn't lost.
if self.event_tx.try_send(event).is_err() {
cfg.resolve(id, InterceptDecision::Forward);
tracing::warn!("Event channel full, skipping intercept for id={id}");
} else {
self.captured_request = Some(snapshot);
match tokio::time::timeout(std::time::Duration::from_secs(300), rx).await {
Ok(Ok(InterceptDecision::Forward)) => {
// Pass through unchanged — fall to the return below.
}
Ok(Ok(InterceptDecision::Modified {
method,
uri,
headers,
body,
})) => {
if let Ok(m) = method.parse() {
parts.method = m;
}
if let Ok(u) = uri.parse() {
parts.uri = u;
}
parts.headers = headers;
body_bytes = body;
// Update the captured snapshot to reflect the edits.
self.captured_request = Some(ProxiedRequest::new(
parts.method.clone(),
parts.uri.clone(),
parts.version,
parts.headers.clone(),
body_bytes.clone(),
now_millis(),
));
}
Ok(Ok(InterceptDecision::Block { status, body })) => {
// Short-circuit: captured_request is already set above.
let status_code = http::StatusCode::from_u16(status)
.unwrap_or(http::StatusCode::BAD_GATEWAY);
let response = Response::builder()
.status(status_code)
.body(body::full(body))
.unwrap_or_else(|_| Response::new(body::empty()));
return RequestOrResponse::Response(response);
}
_ => {
// Timeout or sender dropped (intercept turned off):
// return 504 so the client gets a clear error.
tracing::warn!("Intercept timed out for id={id}, returning 504");
let response = Response::builder()
.status(http::StatusCode::GATEWAY_TIMEOUT)
.body(body::empty())
.unwrap_or_else(|_| Response::new(body::empty()));
return RequestOrResponse::Response(response);
}
}
let req = Request::from_parts(parts, body::full(body_bytes));
return RequestOrResponse::Request(req);
}
}
}
let proxied_request = ProxiedRequest::new(
parts.method.clone(),
parts.uri.clone(),
parts.version,
parts.headers.clone(),
body_bytes.clone(),
now_millis(),
);
self.captured_request = Some(proxied_request);
let req = Request::from_parts(parts, body::full(body_bytes));
RequestOrResponse::Request(req)
}
async fn handle_response(
&mut self,
_ctx: &HttpContext,
res: Response<hyper::body::Incoming>,
) -> Response<ProxyBody> {
let (parts, incoming) = res.into_parts();
let body_bytes = collect_body(incoming).await;
collect_and_emit(self, parts, body_bytes)
}
}