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
//! Tower HTTP adapter for the portable path denylist in [`towershield_core`].
//!
//! [`ShieldLayer`] compiles a [`RuleSet`] once and wraps any Tower
//! [`Service`](tower_service::Service). On each request [`ShieldService`]
//! builds an [`towershield_core::InspectionPath`] from `uri.path()`, evaluates
//! the compiled rules, and either forwards the request **unchanged** or returns
//! a generic empty blocked response without polling the inner service.
//!
//! # Domain map
//!
//! | Concept | Types |
//! |---|---|
//! | Layer configuration | [`ShieldBuilder`], [`BlockedResponse`], [`OnBlock`] |
//! | Middleware | [`ShieldLayer`], [`ShieldService`] |
//! | Rule authoring (re-exported) | [`Rule`], [`RuleSet`], [`PathMatcher`], [`DEFAULT_RULES`] |
//! | Decision surface (re-exported) | [`ShieldDecision`], [`ShieldMatch`], [`CompiledRuleSet`] |
//!
//! Rule compilation errors surface as [`towershield_core::ruleset::CompileError`]
//! from [`ShieldBuilder::try_build`]. Prefer that at process startup when
//! rules come from external config; [`ShieldBuilder::build`] panics on failure.
//!
//! ## Placement (critical for Axum)
//!
//! Wrap the **complete** `axum::Router` with `Layer::layer` so the shield runs
//! *before* route matching. `router.layer(ShieldLayer::…)` runs *after*
//! matching, so unmatched scanner probes never hit the denylist.
//!
//! ```rust,no_run
//! use tower::Layer;
//! use towershield::ShieldLayer;
//! // let app = ShieldLayer::default().layer(router);
//! ```
//!
//! ## Middleware ordering
//!
//! Keep the shield outermost (applied last → runs first):
//!
//! ```text
//! ShieldLayer ← reject probes before the rest of the stack
//! TracingLayer
//! RequestBodyLimitLayer
//! AuthLayer
//! Router
//! ```
//!
//! ```rust,no_run
//! # use tower::ServiceBuilder;
//! # use towershield::ShieldLayer;
//! # let router = tower::service_fn(|_: http::Request<()>| async { Ok::<_, std::convert::Infallible>(http::Response::new(())) });
//! let app = ServiceBuilder::new()
//! .layer(ShieldLayer::default())
//! .service(router);
//! ```
//!
//! Core rule types are re-exported so application crates can depend only on
//! `towershield` for common configuration. Offline Cloudflare export lives in
//! the separate `towershield-cloudflare` crate and does not run on the request path.
//!
//! # Features
//!
//! | Feature | Effect |
//! |---|---|
//! | `tracing` (default) | Emit a debug log line on each block |
//! | `regex` (default) | Regex matchers and the broader built-in rule tier |
//! | `rayon` | Forwarded to `towershield-core` for parallel rule compile |
pub use ;
pub use ShieldService;
pub use ;