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
420
421
422
423
424
425
426
427
428
429
use parking_lot::{RwLock, RwLockReadGuard};
use std::sync::LazyLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

pub mod request;
pub mod response;

pub use request::*;
pub use response::*;

// ---------------------------------------------------------------------------
// Global regex cache — compiled once, reused across all requests and threads.
// parking_lot::RwLock: many concurrent readers, rare writes (only on first
// encounter of a new pattern after a config reload).
// ---------------------------------------------------------------------------
static REGEX_CACHE: LazyLock<RwLock<HashMap<String, Arc<regex::Regex>>>> =
    LazyLock::new(|| RwLock::new(HashMap::new()));

/// Returns a cached compiled regex for `pattern`, compiling and caching it on
/// first call. Returns `None` for invalid patterns (same behaviour as before:
/// silently non-matching rather than panicking).
fn get_cached_regex(pattern: &str) -> Option<Arc<regex::Regex>> {
    // Fast path: pattern already compiled.
    {
        let cache = REGEX_CACHE.read();
        if let Some(re) = cache.get(pattern) {
            return Some(Arc::clone(re));
        }
    }
    // Slow path: compile and insert.
    let re = Arc::new(regex::Regex::new(pattern).ok()?);
    REGEX_CACHE.write().insert(pattern.to_string(), Arc::clone(&re));
    Some(re)
}

// ---------------------------------------------------------------------------
// Config structs
// ---------------------------------------------------------------------------

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ServerConfig {
    #[serde(default = "default_port")]
    pub port: u16,
}

fn default_port() -> u16 {
    3000
}

/// Controls which upstream targets the proxy is allowed to reach.
/// Default: http/https only, private/loopback IPs blocked (secure default).
/// Set `allow_private_networks: true` when proxying to internal services.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UpstreamConfig {
    /// Permit requests to private/loopback/link-local IP ranges.
    /// Default: false. Enable when proxying to internal services.
    #[serde(default)]
    pub allow_private_networks: bool,
    /// URL schemes allowed. Default: ["http", "https"].
    #[serde(default = "default_allowed_schemes")]
    pub allowed_schemes: Vec<String>,
    /// If non-empty, only these hostnames/IPs are permitted (exact match).
    #[serde(default)]
    pub allowed_hosts: Vec<String>,
    /// Hostnames/IPs always blocked regardless of other settings.
    #[serde(default)]
    pub denied_hosts: Vec<String>,
}

fn default_allowed_schemes() -> Vec<String> {
    vec!["http".to_string(), "https".to_string()]
}

impl Default for UpstreamConfig {
    fn default() -> Self {
        Self {
            allow_private_networks: false,
            allowed_schemes: default_allowed_schemes(),
            allowed_hosts: vec![],
            denied_hosts: vec![],
        }
    }
}

/// Top-level configuration loaded from a YAML file.
///
/// Load with [`Config::from_file`], then wrap in [`ConfigHolder`] to serve traffic.
/// All sections except `logging` and `drop` are optional and default to safe values.
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    #[serde(default)]
    pub server: ServerConfig,
    pub logging: LoggingConfig,
    pub drop: DropConfig,
    #[serde(default)]
    pub response_logging: ResponseLoggingConfig,
    /// Upstream access controls (SSRF protection).
    #[serde(default)]
    pub upstream: UpstreamConfig,
}

/// Thread-safe wrapper around [`Config`] that supports hot reload.
///
/// Uses a `parking_lot::RwLock` internally — many concurrent readers, rare writes
/// (only during [`reload`](ConfigHolder::reload)). Pass as `Arc<ConfigHolder>` axum state.
#[derive(Debug)]
pub struct ConfigHolder {
    config: RwLock<Config>,
}

impl ConfigHolder {
    /// Creates a new `ConfigHolder`, pre-warming the regex cache for all patterns.
    pub fn new(config: Config) -> Self {
        // Pre-warm the global regex cache for all patterns in this config so
        // that the first live request does not pay compilation cost.
        prewarm_regex_cache(&config);
        Self {
            config: RwLock::new(config),
        }
    }

    pub fn reload(&self) -> Result<(), Box<dyn std::error::Error>> {
        let config_file =
            std::env::var("CONFIG_FILE").unwrap_or_else(|_| "config.yaml".to_string());
        let new_config = Config::from_file(&config_file)?;
        let mut config = self.config.write();
        *config = new_config;
        Ok(())
    }

    pub fn get(&self) -> RwLockReadGuard<'_, Config> {
        self.config.read()
    }
}

/// Pre-warm the global regex cache with every pattern in the config.
/// Invalid patterns are silently skipped (they will never match).
fn prewarm_regex_cache(config: &Config) {
    let all_patterns = config.logging.rules.iter()
        .flat_map(|r| {
            r.match_conditions.path.patterns.iter()
                .chain(r.match_conditions.body.patterns.iter())
                .chain(r.match_conditions.headers.values())
        })
        .chain(config.drop.rules.iter().flat_map(|r| {
            r.match_conditions.path.patterns.iter()
                .chain(r.match_conditions.body.patterns.iter())
                .chain(r.match_conditions.headers.values())
        }))
        .chain(config.response_logging.rules.iter().flat_map(|r| {
            r.match_conditions.body.patterns.iter()
                .chain(r.match_conditions.headers.values())
        }));

    let mut cache = REGEX_CACHE.write();
    for pattern in all_patterns {
        if !cache.contains_key(pattern) {
            if let Ok(re) = regex::Regex::new(pattern) {
                cache.insert(pattern.clone(), Arc::new(re));
            }
        }
    }
}

impl Config {
    pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let f = std::fs::File::open(path)?;
        let mut config: Config = serde_norway::from_reader(f)?;
        config.substitute_env_vars();
        // Validate all patterns at startup to surface bad regex before serving traffic.
        config.validate_patterns()?;
        // Pre-warm cache so first request pays no compilation cost.
        prewarm_regex_cache(&config);
        Ok(config)
    }

    fn validate_patterns(&self) -> Result<(), Box<dyn std::error::Error>> {
        for rule in &self.logging.rules {
            for p in &rule.match_conditions.path.patterns {
                regex::Regex::new(p).map_err(|e| format!("Invalid path pattern '{}': {}", p, e))?;
            }
            for p in &rule.match_conditions.body.patterns {
                regex::Regex::new(p).map_err(|e| format!("Invalid body pattern '{}': {}", p, e))?;
            }
            for p in rule.match_conditions.headers.values() {
                regex::Regex::new(p).map_err(|e| format!("Invalid header pattern '{}': {}", p, e))?;
            }
        }
        for rule in &self.drop.rules {
            for p in &rule.match_conditions.path.patterns {
                regex::Regex::new(p).map_err(|e| format!("Invalid path pattern '{}': {}", p, e))?;
            }
            for p in &rule.match_conditions.body.patterns {
                regex::Regex::new(p).map_err(|e| format!("Invalid body pattern '{}': {}", p, e))?;
            }
            for p in rule.match_conditions.headers.values() {
                regex::Regex::new(p).map_err(|e| format!("Invalid header pattern '{}': {}", p, e))?;
            }
        }
        for rule in &self.response_logging.rules {
            for p in &rule.match_conditions.body.patterns {
                regex::Regex::new(p).map_err(|e| format!("Invalid body pattern '{}': {}", p, e))?;
            }
            for p in rule.match_conditions.headers.values() {
                regex::Regex::new(p).map_err(|e| format!("Invalid header pattern '{}': {}", p, e))?;
            }
        }
        Ok(())
    }

    /// Match `text` against `pattern` using the global compiled-regex cache.
    fn match_pattern(pattern: &str, text: &str) -> bool {
        get_cached_regex(pattern)
            .map(|re| re.is_match(text))
            .unwrap_or(false)
    }

    fn substitute_env_vars(&mut self) {
        for rule in &mut self.drop.rules {
            if let Some(ref mut body) = rule.response.body {
                *body = Self::substitute_env_in_string(body);
            }
        }
    }

    pub fn substitute_env_in_string(s: &str) -> String {
        let re = regex::Regex::new(r"\$\{([^}]+)\}").unwrap();
        re.replace_all(s, |caps: &regex::Captures| {
            let var_name = &caps[1];
            std::env::var(var_name).unwrap_or_else(|_| format!("${{{}}}", var_name))
        })
        .to_string()
    }

    // -----------------------------------------------------------------------
    // Public matching methods — Request-based (used by tests and direct callers)
    // -----------------------------------------------------------------------

    pub fn should_log_request(
        &self,
        req: &axum::extract::Request,
        body_content: &str,
    ) -> Option<&CaptureConfig> {
        self.should_log_request_parts(req.method().as_str(), req.uri().path(), req.headers(), body_content)
    }

    pub fn should_drop_request(
        &self,
        req: &axum::extract::Request,
        body_content: &str,
    ) -> Option<DropResponse> {
        self.should_drop_request_parts(req.method().as_str(), req.uri().path(), req.headers(), body_content)
    }

    pub fn matches_rule(
        &self,
        req: &axum::extract::Request,
        conditions: &MatchConditions,
        body_content: &str,
    ) -> bool {
        self.matches_conditions_parts(req.method().as_str(), req.uri().path(), req.headers(), body_content, conditions)
    }

    // -----------------------------------------------------------------------
    // Part-based variants — used by proxy_handler after the body is consumed
    // -----------------------------------------------------------------------

    pub fn should_log_request_parts(
        &self,
        method: &str,
        path: &str,
        headers: &axum::http::HeaderMap,
        body_content: &str,
    ) -> Option<&CaptureConfig> {
        for rule in &self.logging.rules {
            if self.matches_conditions_parts(method, path, headers, body_content, &rule.match_conditions) {
                return Some(&rule.capture);
            }
        }
        if self.logging.default {
            static DEFAULT_CAPTURE: CaptureConfig = CaptureConfig {
                headers: vec![],
                body: true,
                method: true,
                path: true,
                timing: true,
            };
            Some(&DEFAULT_CAPTURE)
        } else {
            None
        }
    }

    pub fn should_drop_request_parts(
        &self,
        method: &str,
        path: &str,
        headers: &axum::http::HeaderMap,
        body_content: &str,
    ) -> Option<DropResponse> {
        for rule in &self.drop.rules {
            if self.matches_conditions_parts(method, path, headers, body_content, &rule.match_conditions) {
                return Some(rule.response.clone());
            }
        }
        if self.drop.default {
            Some(DropResponse {
                status_code: 403,
                body: Some("Request dropped by default".to_string()),
            })
        } else {
            None
        }
    }

    pub fn matches_rule_parts(
        &self,
        method: &str,
        path: &str,
        headers: &axum::http::HeaderMap,
        body_content: &str,
        conditions: &MatchConditions,
    ) -> bool {
        self.matches_conditions_parts(method, path, headers, body_content, conditions)
    }

    fn matches_conditions_parts(
        &self,
        method: &str,
        path: &str,
        headers: &axum::http::HeaderMap,
        body_content: &str,
        conditions: &MatchConditions,
    ) -> bool {
        // Method check
        if !conditions.methods.is_empty()
            && !conditions.methods.iter().any(|m| m.eq_ignore_ascii_case(method))
        {
            return false;
        }

        // Path check
        if !conditions.path.patterns.is_empty()
            && !conditions.path.patterns.iter().any(|p| Self::match_pattern(p, path))
        {
            return false;
        }

        // Header check (all specified headers must match)
        for (header_name, pattern) in &conditions.headers {
            if let Some(value) = headers.get(header_name) {
                if let Ok(s) = value.to_str() {
                    if !Self::match_pattern(pattern, s) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }

        // Body check
        if !conditions.body.patterns.is_empty()
            && !conditions.body.patterns.iter().any(|p| Self::match_pattern(p, body_content))
        {
            return false;
        }

        true
    }

    pub fn should_log_response(
        &self,
        status_code: u16,
        headers: &axum::http::HeaderMap,
        body_content: &str,
    ) -> Option<&ResponseCaptureConfig> {
        for rule in &self.response_logging.rules {
            if self.matches_response_rule(status_code, headers, body_content, &rule.match_conditions) {
                return Some(&rule.capture);
            }
        }
        if self.response_logging.default {
            static DEFAULT_RESPONSE_CAPTURE: ResponseCaptureConfig = ResponseCaptureConfig {
                headers: vec![],
                body: true,
                status_code: true,
                timing: true,
            };
            Some(&DEFAULT_RESPONSE_CAPTURE)
        } else {
            None
        }
    }

    pub fn matches_response_rule(
        &self,
        status_code: u16,
        headers: &axum::http::HeaderMap,
        body_content: &str,
        conditions: &ResponseMatchConditions,
    ) -> bool {
        if !conditions.status_codes.is_empty() && !conditions.status_codes.contains(&status_code) {
            return false;
        }

        for (header_name, pattern) in &conditions.headers {
            if let Some(value) = headers.get(header_name) {
                if let Ok(s) = value.to_str() {
                    if !Self::match_pattern(pattern, s) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }

        if !conditions.body.patterns.is_empty()
            && !conditions.body.patterns.iter().any(|p| Self::match_pattern(p, body_content))
        {
            return false;
        }

        true
    }
}