soth_mitm/handler.rs
1use std::future::Future;
2
3use crate::{HandlerDecision, ProcessInfo, RawRequest, RawResponse, StreamChunk};
4use uuid::Uuid;
5
6/// Trait for intercepting and inspecting proxy traffic.
7///
8/// Implement this trait to receive callbacks for requests, responses,
9/// streaming frames, and connection lifecycle events. All methods have
10/// default no-op implementations so you only override what you need.
11pub trait InterceptHandler: Send + Sync + 'static {
12 fn should_intercept_tls(&self, _host: &str, _process_info: Option<&ProcessInfo>) -> bool {
13 true
14 }
15
16 fn on_tls_failure(&self, _host: &str, _error: &str) {}
17
18 fn on_request(&self, _request: &RawRequest) -> impl Future<Output = HandlerDecision> + Send {
19 async { HandlerDecision::Allow }
20 }
21
22 /// Called when a WebSocket upgrade completes (server sent 101).
23 ///
24 /// The `response` carries the 101 status and upgrade headers (empty body).
25 /// Fires after `on_request` and before the first `on_stream_chunk`.
26 /// Ordering is guaranteed by the per-flow dispatch queue.
27 fn on_websocket_start(&self, _response: &RawResponse) -> impl Future<Output = ()> + Send {
28 async {}
29 }
30
31 fn on_stream_chunk(&self, _chunk: &StreamChunk) -> impl Future<Output = ()> + Send {
32 async {}
33 }
34
35 fn on_stream_end(&self, _connection_id: Uuid) -> impl Future<Output = ()> + Send {
36 async {}
37 }
38
39 fn on_response(&self, _response: &RawResponse) -> impl Future<Output = ()> + Send {
40 async {}
41 }
42
43 /// Called exactly once when the underlying connection is torn down.
44 ///
45 /// This fires *after* `on_stream_end` and is the correct place for
46 /// connection-scoped cleanup (session unbinding, pending state removal).
47 /// For HTTP/2 multiplexed connections, `on_stream_end` fires per-stream
48 /// while `on_connection_close` fires once for the connection.
49 fn on_connection_close(&self, _connection_id: Uuid) {}
50}