logprox 0.3.0

A blazing-fast HTTP proxy with conditional logging and request control
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
use axum::{
    body::Body,
    extract::State,
    http::{HeaderMap, HeaderName, HeaderValue, StatusCode},
    response::{IntoResponse, Response},
};
use axum::extract::Request;
use crate::config::{CaptureConfig, ConfigHolder, ResponseCaptureConfig};
use std::sync::Arc;
use std::sync::LazyLock;
use tracing::info;

/// Maximum request body size accepted before returning 413. Prevents OOM via large uploads.
const MAX_BODY_SIZE: usize = 10 * 1024 * 1024; // 10 MB

/// Errors that can occur during proxying. Each variant maps to a distinct HTTP error response.
#[derive(Debug)]
pub enum ProxyError {
    NoUpstreamUrl,
    InvalidUpstreamUrl,
    BlockedUpstream,
    UpstreamRequestFailed(String),
    TimeoutError,
    BodyReadError,
    BodyTooLarge,
}

impl IntoResponse for ProxyError {
    fn into_response(self) -> Response {
        let (status, error_msg) = match self {
            ProxyError::NoUpstreamUrl => (
                StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": "No upstream URL provided",
                    "message": "Path must contain upstream URL after the first slash"
                }),
            ),
            ProxyError::InvalidUpstreamUrl => (
                StatusCode::BAD_REQUEST,
                serde_json::json!({"error": "Invalid upstream URL"}),
            ),
            ProxyError::BlockedUpstream => (
                StatusCode::FORBIDDEN,
                serde_json::json!({"error": "Upstream request blocked"}),
            ),
            ProxyError::UpstreamRequestFailed(msg) => (
                StatusCode::BAD_GATEWAY,
                serde_json::json!({"error": "Upstream request failed", "details": msg}),
            ),
            ProxyError::TimeoutError => (
                StatusCode::GATEWAY_TIMEOUT,
                serde_json::json!({"error": "Upstream timeout"}),
            ),
            ProxyError::BodyReadError => (
                StatusCode::BAD_REQUEST,
                serde_json::json!({"error": "Failed to read request body"}),
            ),
            ProxyError::BodyTooLarge => (
                StatusCode::PAYLOAD_TOO_LARGE,
                serde_json::json!({"error": "Request body too large"}),
            ),
        };

        Response::builder()
            .status(status)
            .header("content-type", "application/json")
            .body(Body::from(serde_json::to_string(&error_msg).unwrap()))
            .unwrap()
    }
}

static HTTP_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
    reqwest::Client::builder()
        .build()
        .expect("Failed to create HTTP client")
});

/// Hop-by-hop headers that must not be forwarded per RFC 7230 §6.1.
const HOP_BY_HOP: &[&str] = &[
    "connection", "keep-alive", "proxy-authenticate",
    "proxy-authorization", "te", "trailers", "transfer-encoding",
    "upgrade",
];

fn filter_headers(headers: &HeaderMap) -> reqwest::header::HeaderMap {
    let mut result = reqwest::header::HeaderMap::new();
    for (name, value) in headers.iter() {
        let name_str = name.as_str();
        if !HOP_BY_HOP.iter().any(|h| h.eq_ignore_ascii_case(name_str)) {
            if let Ok(key) = reqwest::header::HeaderName::from_bytes(name.as_str().as_bytes()) {
                if let Ok(val) = reqwest::header::HeaderValue::from_bytes(value.as_bytes()) {
                    result.insert(key, val);
                }
            }
        }
    }
    result
}

#[axum::debug_handler]
pub async fn proxy_handler(State(config): State<Arc<ConfigHolder>>, req: Request) -> impl IntoResponse {
    let start_time = std::time::Instant::now();

    // --- Extract request metadata before consuming the body ---
    let method_str = req.method().as_str().to_string();
    let headers = req.headers().clone();
    let req_path = req.uri().path().to_string();

    // --- Read body (with size cap) before any rule evaluation ---
    // Rules with body conditions need the real body to match correctly.
    let body_bytes = match axum::body::to_bytes(req.into_body(), MAX_BODY_SIZE).await {
        Ok(b) => b,
        Err(_) => return ProxyError::BodyTooLarge.into_response(),
    };
    let body_content = String::from_utf8_lossy(&body_bytes).to_string();

    // --- Drop check (with real body, before URL extraction so drop rules apply to all paths) ---
    let drop_response = {
        let cfg = config.get();
        cfg.should_drop_request_parts(&method_str, &req_path, &headers, &body_content)
    };

    if let Some(drop_resp) = drop_response {
        let response = Response::builder()
            .status(drop_resp.status_code)
            .body(Body::from(drop_resp.body.unwrap_or_default()))
            .unwrap();

        // Log the drop response if response_logging is configured
        let cfg = config.get();
        if let Some(capture) = cfg.should_log_response(response.status().as_u16(), response.headers(), "") {
            log_response(&method_str, &req_path, response.status().as_u16(), response.headers(), capture, start_time.elapsed(), "");
        }

        return response;
    }

    // --- Extract upstream URL (after drop check so drop rules apply to any path) ---
    let upstream_url = match extract_upstream_url(&req_path) {
        Ok(url) => url,
        Err(e) => return e.into_response(),
    };

    // --- SSRF validation ---
    {
        let cfg = config.get();
        if let Err(reason) = validate_upstream_ssrf(&upstream_url, &cfg.upstream) {
            tracing::warn!(upstream = %upstream_url, reason = %reason, "upstream blocked");
            return ProxyError::BlockedUpstream.into_response();
        }
    }

    // --- Get timeout and log config (with real body) ---
    let (timeout, log_request_config) = {
        let cfg = config.get();

        let timeout = cfg.logging.rules.iter()
            .find(|rule| cfg.matches_rule_parts(&method_str, &req_path, &headers, &body_content, &rule.match_conditions))
            .and_then(|rule| rule.timeout.as_deref().and_then(parse_duration_string));

        let log_cfg = cfg.should_log_request_parts(&method_str, &req_path, &headers, &body_content)
            .cloned();

        (timeout, log_cfg)
    };

    // --- Log request if configured ---
    if let Some(ref capture_config) = log_request_config {
        log_request(&method_str, &req_path, &headers, capture_config, std::time::Duration::default(), &body_content, timeout);
    }

    // --- Build and send upstream request ---
    let method = match reqwest::Method::from_bytes(method_str.as_bytes()) {
        Ok(m) => m,
        Err(_) => return ProxyError::UpstreamRequestFailed("Invalid method".to_string()).into_response(),
    };

    let filtered_headers = filter_headers(&headers);
    let mut request_builder = HTTP_CLIENT.request(method, &upstream_url).headers(filtered_headers);

    if !body_bytes.is_empty() {
        request_builder = request_builder.body(reqwest::Body::from(body_bytes));
    }
    if let Some(t) = timeout {
        request_builder = request_builder.timeout(t);
    }

    let upstream_resp = match request_builder.send().await {
        Ok(resp) => resp,
        Err(e) if e.is_timeout() => return ProxyError::TimeoutError.into_response(),
        Err(e) => return ProxyError::UpstreamRequestFailed(e.to_string()).into_response(),
    };

    // --- Build response, forwarding upstream headers ---
    let status = StatusCode::from_u16(upstream_resp.status().as_u16())
        .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);

    let mut response_builder = Response::builder().status(status);
    let mut resp_headers = HeaderMap::new();

    for (name, value) in upstream_resp.headers() {
        let name_str = name.as_str();
        if !HOP_BY_HOP.iter().any(|h| h.eq_ignore_ascii_case(name_str)) {
            if let Ok(header_name) = HeaderName::from_bytes(name_str.as_bytes()) {
                if let Ok(header_value) = HeaderValue::from_bytes(value.as_bytes()) {
                    resp_headers.insert(header_name.clone(), header_value.clone());
                    response_builder = response_builder.header(header_name, header_value);
                }
            }
        }
    }

    let resp_body_bytes = match upstream_resp.bytes().await {
        Ok(b) => b,
        Err(e) => return ProxyError::UpstreamRequestFailed(
            format!("Failed to read response body: {}", e)
        ).into_response(),
    };

    // Decode response body only if response_logging is active (avoids allocation otherwise)
    let response_logging_active = {
        let cfg = config.get();
        cfg.response_logging.default || !cfg.response_logging.rules.is_empty()
    };
    let resp_body_content = if response_logging_active {
        String::from_utf8_lossy(&resp_body_bytes).into_owned()
    } else {
        String::new()
    };

    let final_resp = response_builder.body(Body::from(resp_body_bytes)).unwrap();

    // --- Log response if configured ---
    {
        let cfg = config.get();
        if let Some(capture) = cfg.should_log_response(
            final_resp.status().as_u16(),
            &resp_headers,
            &resp_body_content,
        ) {
            log_response(
                &method_str, &req_path,
                final_resp.status().as_u16(), &resp_headers,
                capture, start_time.elapsed(), &resp_body_content,
            );
        }
    }

    final_resp
}

pub fn extract_upstream_url(path: &str) -> Result<String, ProxyError> {
    let url_str = path.strip_prefix('/').ok_or(ProxyError::NoUpstreamUrl)?;

    if url_str.is_empty() {
        return Err(ProxyError::NoUpstreamUrl);
    }

    if url_str.parse::<reqwest::Url>().is_err() {
        return Err(ProxyError::InvalidUpstreamUrl);
    }

    Ok(url_str.to_string())
}

fn log_request(
    method: &str,
    path: &str,
    req_headers: &HeaderMap,
    capture_config: &CaptureConfig,
    duration: std::time::Duration,
    body_content: &str,
    timeout: Option<std::time::Duration>,
) {
    let mut log_entry = serde_json::json!({
        "type": "request",
        "timestamp": chrono::Utc::now().to_rfc3339(),
    });

    if capture_config.method {
        log_entry["method"] = method.into();
    }
    if capture_config.path {
        log_entry["path"] = path.into();
    }
    if capture_config.timing {
        log_entry["duration_ms"] = (duration.as_millis() as u64).into();
    }
    if let Some(timeout) = timeout {
        log_entry["timeout_ms"] = (timeout.as_millis() as u64).into();
    }
    if !capture_config.headers.is_empty() {
        let mut headers_obj = serde_json::Map::new();
        for header_name in &capture_config.headers {
            if let Some(value) = req_headers.get(header_name) {
                if let Ok(value_str) = value.to_str() {
                    headers_obj.insert(header_name.clone(), value_str.into());
                }
            }
        }
        if !headers_obj.is_empty() {
            log_entry["headers"] = headers_obj.into();
        }
    }
    if capture_config.body {
        log_entry["body"] = body_content.into();
    }

    info!("{}", serde_json::to_string(&log_entry).unwrap_or_else(|_| "Failed to serialize request log".to_string()));
}

fn log_response(
    req_method: &str,
    req_path: &str,
    resp_status: u16,
    resp_headers: &HeaderMap,
    capture_config: &ResponseCaptureConfig,
    duration: std::time::Duration,
    body_content: &str,
) {
    let mut log_entry = serde_json::json!({
        "type": "response",
        "timestamp": chrono::Utc::now().to_rfc3339(),
        "request_method": req_method,
        "request_path": req_path,
    });

    if capture_config.status_code {
        log_entry["status_code"] = resp_status.into();
    }
    if capture_config.timing {
        log_entry["duration_ms"] = (duration.as_millis() as u64).into();
    }
    if !capture_config.headers.is_empty() {
        let mut headers_obj = serde_json::Map::new();
        for header_name in &capture_config.headers {
            if let Some(value) = resp_headers.get(header_name) {
                if let Ok(value_str) = value.to_str() {
                    headers_obj.insert(header_name.clone(), value_str.into());
                }
            }
        }
        if !headers_obj.is_empty() {
            log_entry["headers"] = headers_obj.into();
        }
    }
    if capture_config.body {
        log_entry["body"] = body_content.into();
    }

    info!("{}", serde_json::to_string(&log_entry).unwrap_or_else(|_| "Failed to serialize response log".to_string()));
}

fn is_private_ipv6(ip: std::net::Ipv6Addr) -> bool {
    let b = ip.octets();
    ip.is_loopback()               // ::1
    || ip.is_unspecified()         // ::
    || (b[0] & 0xfe) == 0xfc      // fc00::/7 unique-local
    || (b[0] == 0xfe && (b[1] & 0xc0) == 0x80) // fe80::/10 link-local
}

/// Returns Err with a reason string if the upstream URL should be blocked.
fn validate_upstream_ssrf(
    url_str: &str,
    cfg: &crate::config::UpstreamConfig,
) -> Result<(), &'static str> {
    let url = url_str.parse::<reqwest::Url>().map_err(|_| "invalid URL")?;

    // Scheme check
    if !cfg.allowed_schemes.iter().any(|s| s.eq_ignore_ascii_case(url.scheme())) {
        return Err("scheme not allowed");
    }

    let host_str = url.host_str().ok_or("no host")?;

    // Allowlist: if set, host must be in it (allowlist takes priority)
    if !cfg.allowed_hosts.is_empty() {
        return if cfg.allowed_hosts.iter().any(|h| h == host_str) {
            Ok(())
        } else {
            Err("host not in allowlist")
        };
    }

    // Denylist
    if cfg.denied_hosts.iter().any(|h| h == host_str) {
        return Err("host explicitly denied");
    }

    // Private-network block (literal IPs only; domain names require DNS to verify)
    if !cfg.allow_private_networks {
        if let Ok(ipv4) = host_str.parse::<std::net::Ipv4Addr>() {
            if ipv4.is_loopback() || ipv4.is_private() || ipv4.is_link_local() || ipv4.is_unspecified() {
                return Err("private/loopback address blocked");
            }
        } else if let Ok(ipv6) = host_str.parse::<std::net::Ipv6Addr>() {
            if is_private_ipv6(ipv6) {
                return Err("private/loopback address blocked");
            }
        }
        // Domain → allow; attacker can bypass via DNS rebinding (documented limitation)
    }

    Ok(())
}

pub fn parse_duration_string(s: &str) -> Option<std::time::Duration> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }
    if let Some(suffix) = s.strip_suffix("ms") {
        suffix.trim().parse::<u64>().ok().map(std::time::Duration::from_millis)
    } else if let Some(suffix) = s.strip_suffix('s') {
        suffix.trim().parse::<u64>().ok().map(std::time::Duration::from_secs)
    } else {
        None
    }
}