Skip to main content

brainwires_proxy/inspector/
mod.rs

1//! Traffic inspection — event capture, storage, broadcast, and query API.
2
3#[cfg(feature = "inspector-api")]
4pub mod api;
5pub mod broadcast;
6pub mod store;
7
8use crate::request_id::RequestId;
9use std::collections::HashMap;
10
11pub use broadcast::EventBroadcaster;
12pub use store::EventStore;
13
14/// Direction of a traffic event.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16pub enum EventDirection {
17    Inbound,
18    Outbound,
19}
20
21/// Kind-specific payload for a traffic event.
22#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
23#[serde(tag = "type", rename_all = "snake_case")]
24pub enum TrafficEventKind {
25    Request {
26        method: String,
27        uri: String,
28        headers: HashMap<String, String>,
29        body_size: usize,
30    },
31    Response {
32        status: u16,
33        headers: HashMap<String, String>,
34        body_size: usize,
35    },
36    SseEvent {
37        event_type: Option<String>,
38        data_preview: String,
39    },
40    WebSocketMessage {
41        is_binary: bool,
42        size: usize,
43    },
44    Error {
45        message: String,
46    },
47    Connection {
48        peer: String,
49        connected: bool,
50    },
51    Conversion {
52        source_format: String,
53        target_format: String,
54        body_size: usize,
55    },
56}
57
58/// A captured traffic event.
59#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
60pub struct TrafficEvent {
61    pub id: uuid::Uuid,
62    pub request_id: RequestId,
63    pub timestamp: chrono::DateTime<chrono::Utc>,
64    pub direction: EventDirection,
65    pub kind: TrafficEventKind,
66}
67
68/// Convert `HeaderMap` to a simple `HashMap<String, String>` for serialization.
69pub fn headers_to_map(headers: &http::HeaderMap) -> HashMap<String, String> {
70    let mut map = HashMap::new();
71    for (name, value) in headers.iter() {
72        if let Ok(v) = value.to_str() {
73            map.insert(name.to_string(), v.to_string());
74        }
75    }
76    map
77}