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
//! # axtra::bouncer
//!
//! Simple IP banning and malicious path filtering middleware for Tower.
//!
//! ## Overview
//!
//! The `bouncer` module provides middleware for:
//! - Automatically banning IP addresses that hit known malicious or unwanted paths.
//! - Blocking requests to preset or custom path rulesets.
//! - Configurable ban duration, response status, and response body for banned/blocked requests.
//! - Configurable log level for tracing blocked and banned events.
//! - Observability: expose the banlist for monitoring.
//!
//! ## Features
//!
//! - Ban IPs for a configurable duration when they access blocked paths.
//! - Use presets (e.g., "wordpress", "php", "config") or custom paths for filtering.
//! - Customize HTTP status and body for banned and blocked responses.
//! - Set log level for event tracing (`trace`, `debug`, `info`, etc).
//! - Expose the banlist for observability and monitoring.
//!
//! ## Usage Example
//!
//! ```rust, ignore
//! use axtra::bouncer::{BouncerConfig, BouncerLayer};
//! use axum::{Router, routing::get};
//! use axum::http::StatusCode;
//! use tracing::Level;
//! use std::time::Duration;
//!
//! // Create a config with presets and custom paths, and customize responses/logging
//! let config = BouncerConfig::from_rules(
//! &["wordpress", "config"],
//! &["/custom"]
//! )
//! .duration(Duration::from_secs(1800))
//! .banned_response(StatusCode::UNAUTHORIZED)
//! .blocked_response(StatusCode::NOT_FOUND)
//! .log_level(Level::INFO);
//! let layer = BouncerLayer::new(config);
//!
//! let app = Router::new()
//! .route("/", get(|| async { "Hello" }))
//! .layer(layer);
//! ```
//!
//! ## Presets
//!
//! Available presets for common hacker/scanner paths:
//! - `"wordpress"`
//! - `"php"`
//! - `"config"`
//!
//! ## Advanced Usage
//!
//! You can also pass only presets or only custom paths:
//! ```rust, ignore
//! let config = BouncerConfig::from_preset_rules(&["wordpress"]);
//! let config = BouncerConfig::from_custom_rules(&["/admin", "/hidden"]);
//! ```
//!
//! ## Re-exports
//!
//! - [`BouncerConfig`]: Configuration for the bouncer middleware.
//! - [`BouncerLayer`]: Axum layer for IP banning and path filtering.
//!
//! See the README and docs.rs for more details.
pub use ;