use crate::error::GarrisonResult;
use async_trait::async_trait;
#[cfg(feature = "firewall-anomalous")]
pub mod anomalous;
#[cfg(feature = "anomalous-detector-dual")]
pub mod anomalous_analyzer;
#[cfg(feature = "firewall-bruteforce")]
pub mod brute_force;
#[cfg(feature = "firewall-ratelimit")]
pub mod captcha_provider;
#[cfg(feature = "firewall-ddos")]
pub mod ddos;
#[cfg(any(feature = "firewall-anomalous", feature = "firewall-geoip"))]
pub mod geo;
#[cfg(feature = "firewall-geoip")]
pub mod geoip;
#[cfg(feature = "firewall-ratelimit")]
pub mod rate_limit;
#[cfg(feature = "firewall-waf")]
pub mod waf;
#[cfg(feature = "firewall-waf")]
pub mod waf_hooks;
#[cfg(feature = "firewall-bruteforce")]
pub use brute_force::{BruteForceConfig, BruteForceStrategy};
#[cfg(feature = "firewall-ratelimit")]
pub use rate_limit::{RateLimitConfig, RateLimitScope, RateLimitStrategy};
#[cfg(feature = "firewall-anomalous")]
pub use anomalous::{AnomalousConfig, AnomalousLoginStrategy};
#[cfg(feature = "firewall-ddos")]
pub use ddos::{DDoSConfig, DDoSStrategy};
#[cfg(feature = "firewall-geoip")]
pub use geoip::{GeoIPConfig, GeoIPStrategy};
#[cfg(any(feature = "firewall-anomalous", feature = "firewall-geoip"))]
pub use geo::{CountryLookup, GeoCoord, GeoLookup};
#[cfg(feature = "anomalous-detector-dual")]
pub use anomalous_analyzer::{AnomalousAnalyzerConfig, AnomalousLoginAnalyzer};
#[cfg(feature = "firewall-waf")]
pub use waf::{WafContext, WafHookChain};
#[cfg(feature = "firewall-waf")]
pub use waf_hooks::{BlackPathHook, DangerCharacterHook};
#[derive(Debug, Clone)]
pub struct FirewallContext {
pub ip: String,
pub login_id: Option<String>,
pub tenant_id: Option<i64>,
}
mod context_impl;
#[async_trait]
pub trait GarrisonFirewallStrategy: Send + Sync {
async fn check(&self, ctx: &FirewallContext) -> GarrisonResult<()>;
}
#[async_trait]
pub trait CaptchaChallenge: Send + Sync {
async fn should_challenge(&self, ctx: &FirewallContext) -> GarrisonResult<bool>;
async fn verify_challenge(&self, ctx: &FirewallContext, answer: &str) -> GarrisonResult<bool>;
}
pub struct StrategyRegistration {
pub name: &'static str,
}
inventory::collect!(StrategyRegistration);
#[cfg(test)]
mod tests {
#[test]
#[cfg(all(
feature = "firewall-bruteforce",
feature = "firewall-ratelimit",
feature = "firewall-anomalous",
feature = "firewall-ddos",
feature = "firewall-geoip"
))]
fn all_five_strategies_registered_via_inventory() {
use super::*;
use std::iter::Iterator;
use super::AnomalousLoginStrategy;
use super::BruteForceStrategy;
use super::DDoSStrategy;
use super::GeoIPStrategy;
use super::RateLimitStrategy;
let _ = std::any::TypeId::of::<AnomalousLoginStrategy>();
let _ = std::any::TypeId::of::<BruteForceStrategy>();
let _ = std::any::TypeId::of::<DDoSStrategy>();
let _ = std::any::TypeId::of::<GeoIPStrategy>();
let _ = std::any::TypeId::of::<RateLimitStrategy>();
let names: Vec<&'static str> = inventory::iter::<StrategyRegistration>()
.map(|r| r.name)
.collect();
let count = names.len();
assert!(
count >= 5,
"启用全部 5 个 firewall feature 时应注册至少 5 个 strategy,实际: {}",
count
);
let names: Vec<&'static str> = inventory::iter::<StrategyRegistration>()
.map(|r| r.name)
.collect();
for expected in &["bruteforce", "ratelimit", "anomalous", "ddos", "geoip"] {
assert!(
names.contains(expected),
"strategy {:?} 未注册,实际注册: {:?}",
expected,
names
);
}
}
}