diff --git a/Cargo.lock b/Cargo.lock index 7ec5f97..7cf05ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2938,7 +2938,7 @@ checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" [[package]] name = "telemt" -version = "3.4.16" +version = "3.4.17" dependencies = [ "aes", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 1d09431..c03fdb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "telemt" -version = "3.4.16" +version = "3.4.17" edition = "2024" [features] diff --git a/docs/Config_params/CONFIG_PARAMS.en.md b/docs/Config_params/CONFIG_PARAMS.en.md index 8fcb7d8..64e20fd 100644 --- a/docs/Config_params/CONFIG_PARAMS.en.md +++ b/docs/Config_params/CONFIG_PARAMS.en.md @@ -2219,6 +2219,10 @@ Note: This section also accepts the legacy alias `[server.admin_api]` (same sche | [`ip`](#ip) | `IpAddr` | — | `✘` | | [`port`](#port-serverlisteners) | `u16` | `server.port` | `✘` | | [`client_mss`](#client_mss-serverlisteners) | `String` | `[server].client_mss` | `✘` | +| [`synlimit`](#synlimit-serverlisteners) | `false`, `"iptables"`, or `"nftables"` | `false` | `✘` | +| [`synlimit_seconds`](#synlimit_seconds-serverlisteners) | `u32` | `1` | `✘` | +| [`synlimit_hitcount`](#synlimit_hitcount-serverlisteners) | `u32` | `1` | `✘` | +| [`synlimit_burst`](#synlimit_burst-serverlisteners) | `u32` | `3` | `✘` | | [`announce`](#announce) | `String` | — | `✘` | | [`announce_ip`](#announce_ip) | `IpAddr` | — | `✘` | | [`proxy_protocol`](#proxy_protocol) | `bool` | — | `✘` | @@ -2254,6 +2258,58 @@ Note: This section also accepts the legacy alias `[server.admin_api]` (same sche port = 443 client_mss = "256" ``` +## synlimit (server.listeners) + - **Constraints / validation**: `false`, `"iptables"`, or `"nftables"`. Omitted or `false` disables SYN limiting for this listener. + - **Description**: Installs per-listener Linux netfilter SYN limiter rules for the listener port. `"iptables"` uses `iptables`/`ip6tables` filter rules with the `hashlimit` match as a per-source token bucket. `"nftables"` uses per-source `meter` rules with `limit rate over` and auto-detects whether the host already uses `inet`, `ip`, or `ip6` table families before creating Telemt-owned tables. The token-bucket rate is `synlimit_hitcount / synlimit_seconds`; `synlimit_burst` controls the burst size. Rules are reconciled at runtime and removed during graceful Telemt shutdown; `SIGKILL` cannot be cleaned up by the process. Requires CAP_NET_ADMIN and listener restart/rebind for config changes. + - **Example**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + + [[server.listeners]] + ip = "::" + port = 443 + synlimit = "nftables" + ``` +## synlimit_seconds (server.listeners) + - **Constraints / validation**: `u32`, must be `> 0`. Default is `1`. + - **Description**: Token-bucket interval for both SYN limiter backends. The rate is `synlimit_hitcount / synlimit_seconds` and is rendered to native netfilter rate units (`second`, `minute`, `hour`, or `day`). + - **Example**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + synlimit_seconds = 1 + ``` +## synlimit_hitcount (server.listeners) + - **Constraints / validation**: `u32`, must be `> 0`. Default is `1`. + - **Description**: Token-bucket rate amount for both SYN limiter backends. Together with `synlimit_seconds`, it defines the allowed source-IP SYN rate before excess SYN packets are dropped. + - **Example**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + synlimit_hitcount = 1 + ``` +## synlimit_burst (server.listeners) + - **Constraints / validation**: `u32`, must be `> 0`. Default is `3`. + - **Description**: Token-bucket burst size for both SYN limiter backends. Higher values allow short connection bursts from the same source IP before the steady-state `synlimit_hitcount / synlimit_seconds` rate is enforced. + - **Example**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + synlimit_burst = 3 + ``` ## announce - **Constraints / validation**: `String` (optional). Must not be empty when set. - **Description**: Public IP/domain announced in proxy links for this listener. Takes precedence over `announce_ip`. diff --git a/docs/Config_params/CONFIG_PARAMS.ru.md b/docs/Config_params/CONFIG_PARAMS.ru.md index 7d473d8..9062236 100644 --- a/docs/Config_params/CONFIG_PARAMS.ru.md +++ b/docs/Config_params/CONFIG_PARAMS.ru.md @@ -2225,6 +2225,10 @@ | [`ip`](#ip) | `IpAddr` | — | `✘` | | [`port`](#port-serverlisteners) | `u16` | `server.port` | `✘` | | [`client_mss`](#client_mss-serverlisteners) | `String` | `[server].client_mss` | `✘` | +| [`synlimit`](#synlimit-serverlisteners) | `false`, `"iptables"` или `"nftables"` | `false` | `✘` | +| [`synlimit_seconds`](#synlimit_seconds-serverlisteners) | `u32` | `1` | `✘` | +| [`synlimit_hitcount`](#synlimit_hitcount-serverlisteners) | `u32` | `1` | `✘` | +| [`synlimit_burst`](#synlimit_burst-serverlisteners) | `u32` | `3` | `✘` | | [`announce`](#announce) | `String` | — | `✘` | | [`announce_ip`](#announce_ip) | `IpAddr` | — | `✘` | | [`proxy_protocol`](#proxy_protocol) | `bool` | — | `✘` | @@ -2260,6 +2264,58 @@ port = 443 client_mss = "256" ``` +## synlimit (server.listeners) + - **Ограничения / валидация**: `false`, `"iptables"` или `"nftables"`. Если параметр не задан или задан как `false`, SYN limiter для этого listener’а выключен. + - **Описание**: Устанавливает per-listener Linux netfilter SYN limiter rules для порта listener’а. `"iptables"` использует `iptables`/`ip6tables` filter rules с `hashlimit` match как per-source token bucket. `"nftables"` использует per-source `meter` rules с `limit rate over` и автоматически определяет, какие table families уже используются на хосте (`inet`, `ip`, `ip6`), перед созданием Telemt-owned tables. Token-bucket rate равен `synlimit_hitcount / synlimit_seconds`; `synlimit_burst` управляет burst size. Rules reconciled at runtime и удаляются при graceful shutdown Telemt; `SIGKILL` процессом не очищается. Требует CAP_NET_ADMIN и restart/rebind listener’а для изменений конфигурации. + - **Пример**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + + [[server.listeners]] + ip = "::" + port = 443 + synlimit = "nftables" + ``` +## synlimit_seconds (server.listeners) + - **Ограничения / валидация**: `u32`, должно быть `> 0`. Значение по умолчанию: `1`. + - **Описание**: Token-bucket interval для обоих SYN limiter backends. Rate равен `synlimit_hitcount / synlimit_seconds` и рендерится в native netfilter rate units (`second`, `minute`, `hour` или `day`). + - **Пример**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + synlimit_seconds = 1 + ``` +## synlimit_hitcount (server.listeners) + - **Ограничения / валидация**: `u32`, должно быть `> 0`. Значение по умолчанию: `1`. + - **Описание**: Token-bucket rate amount для обоих SYN limiter backends. Вместе с `synlimit_seconds` задает разрешенный source-IP SYN rate до того, как excess SYN packets начнут drop’аться. + - **Пример**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + synlimit_hitcount = 1 + ``` +## synlimit_burst (server.listeners) + - **Ограничения / валидация**: `u32`, должно быть `> 0`. Значение по умолчанию: `3`. + - **Описание**: Token-bucket burst size для обоих SYN limiter backends. Более высокие значения разрешают short connection bursts с одного source IP перед применением steady-state rate `synlimit_hitcount / synlimit_seconds`. + - **Пример**: + + ```toml + [[server.listeners]] + ip = "0.0.0.0" + port = 443 + synlimit = "iptables" + synlimit_burst = 3 + ``` ## announce - **Ограничения / валидация**: `String` (необязательный параметр). Не должен быть пустым, если задан. - **Описание**: Публичный IP-адрес или домен, объявляемый в proxy-ссылках для данного listener’а. Имеет приоритет над `announce_ip`. diff --git a/src/config/defaults.rs b/src/config/defaults.rs index da9e472..b0bafac 100644 --- a/src/config/defaults.rs +++ b/src/config/defaults.rs @@ -54,6 +54,9 @@ const DEFAULT_CONNTRACK_CONTROL_ENABLED: bool = true; const DEFAULT_CONNTRACK_PRESSURE_HIGH_WATERMARK_PCT: u8 = 85; const DEFAULT_CONNTRACK_PRESSURE_LOW_WATERMARK_PCT: u8 = 70; const DEFAULT_CONNTRACK_DELETE_BUDGET_PER_SEC: u64 = 4096; +const DEFAULT_SYNLIMIT_SECONDS: u32 = 1; +const DEFAULT_SYNLIMIT_HITCOUNT: u32 = 1; +const DEFAULT_SYNLIMIT_BURST: u32 = 3; const DEFAULT_UPSTREAM_CONNECT_RETRY_ATTEMPTS: u32 = 2; const DEFAULT_UPSTREAM_UNHEALTHY_FAIL_THRESHOLD: u32 = 5; const DEFAULT_UPSTREAM_CONNECT_BUDGET_MS: u64 = 3000; @@ -243,6 +246,18 @@ pub(crate) fn default_conntrack_delete_budget_per_sec() -> u64 { DEFAULT_CONNTRACK_DELETE_BUDGET_PER_SEC } +pub(crate) fn default_synlimit_seconds() -> u32 { + DEFAULT_SYNLIMIT_SECONDS +} + +pub(crate) fn default_synlimit_hitcount() -> u32 { + DEFAULT_SYNLIMIT_HITCOUNT +} + +pub(crate) fn default_synlimit_burst() -> u32 { + DEFAULT_SYNLIMIT_BURST +} + pub(crate) fn default_prefer_4() -> u8 { 4 } diff --git a/src/config/load.rs b/src/config/load.rs index b428079..7b976c8 100644 --- a/src/config/load.rs +++ b/src/config/load.rs @@ -346,6 +346,10 @@ const LISTENER_CONFIG_KEYS: &[&str] = &[ "ip", "port", "client_mss", + "synlimit", + "synlimit_seconds", + "synlimit_hitcount", + "synlimit_burst", "announce", "announce_ip", "proxy_protocol", @@ -1948,6 +1952,21 @@ impl ProxyConfig { ProxyError::Config(format!("server.listeners[{idx}].client_mss {error}")) })?; } + if listener.synlimit_seconds == 0 { + return Err(ProxyError::Config(format!( + "server.listeners[{idx}].synlimit_seconds must be > 0" + ))); + } + if listener.synlimit_hitcount == 0 { + return Err(ProxyError::Config(format!( + "server.listeners[{idx}].synlimit_hitcount must be > 0" + ))); + } + if listener.synlimit_burst == 0 { + return Err(ProxyError::Config(format!( + "server.listeners[{idx}].synlimit_burst must be > 0" + ))); + } } if config.server.accept_permit_timeout_ms > 60_000 { @@ -2186,6 +2205,10 @@ impl ProxyConfig { ip: ipv4, port: Some(config.server.port), client_mss: None, + synlimit: SynLimitMode::default(), + synlimit_seconds: default_synlimit_seconds(), + synlimit_hitcount: default_synlimit_hitcount(), + synlimit_burst: default_synlimit_burst(), announce: None, announce_ip: None, proxy_protocol: None, @@ -2199,6 +2222,10 @@ impl ProxyConfig { ip: ipv6, port: Some(config.server.port), client_mss: None, + synlimit: SynLimitMode::default(), + synlimit_seconds: default_synlimit_seconds(), + synlimit_hitcount: default_synlimit_hitcount(), + synlimit_burst: default_synlimit_burst(), announce: None, announce_ip: None, proxy_protocol: None, diff --git a/src/config/types.rs b/src/config/types.rs index d007428..e0f7b04 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -1369,6 +1369,77 @@ impl ConntrackPressureProfile { } } +/// Per-listener SYN limiter mode. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum SynLimitMode { + /// Disable SYN limiting for this listener. + #[default] + Off, + /// Use iptables/ip6tables filter rules with the hashlimit match. + Iptables, + /// Use nftables rules with per-source token-bucket meters. + Nftables, +} + +impl Serialize for SynLimitMode { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + match self { + Self::Off => serializer.serialize_bool(false), + Self::Iptables => serializer.serialize_str("iptables"), + Self::Nftables => serializer.serialize_str("nftables"), + } + } +} + +impl<'de> Deserialize<'de> for SynLimitMode { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'de>, + { + struct SynLimitModeVisitor; + + impl<'de> serde::de::Visitor<'de> for SynLimitModeVisitor { + type Value = SynLimitMode; + + fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("false, iptables, or nftables") + } + + fn visit_bool(self, value: bool) -> std::result::Result + where + E: serde::de::Error, + { + if value { + Err(E::custom( + "synlimit=true is ambiguous; use \"iptables\" or \"nftables\"", + )) + } else { + Ok(SynLimitMode::Off) + } + } + + fn visit_str(self, value: &str) -> std::result::Result + where + E: serde::de::Error, + { + match value.trim().to_ascii_lowercase().as_str() { + "false" | "off" | "disabled" | "none" => Ok(SynLimitMode::Off), + "iptables" => Ok(SynLimitMode::Iptables), + "nftables" => Ok(SynLimitMode::Nftables), + _ => Err(E::custom( + "synlimit must be false, \"iptables\", or \"nftables\"", + )), + } + } + } + + deserializer.deserialize_any(SynLimitModeVisitor) + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConntrackControlConfig { /// Enables runtime conntrack-control worker for pressure mitigation. @@ -2102,6 +2173,18 @@ pub struct ListenerConfig { /// Empty string disables MSS shaping for this listener. #[serde(default)] pub client_mss: Option, + /// Per-listener SYN limiter mode. + #[serde(default)] + pub synlimit: SynLimitMode, + /// Token-bucket rate interval for the per-listener SYN limiter. + #[serde(default = "default_synlimit_seconds")] + pub synlimit_seconds: u32, + /// Token-bucket rate amount for the per-listener SYN limiter. + #[serde(default = "default_synlimit_hitcount")] + pub synlimit_hitcount: u32, + /// Token-bucket burst size for the per-listener SYN limiter. + #[serde(default = "default_synlimit_burst")] + pub synlimit_burst: u32, /// IP address or hostname to announce in proxy links. /// Takes precedence over `announce_ip` if both are set. #[serde(default)] diff --git a/src/maestro/mod.rs b/src/maestro/mod.rs index 2d4fb54..590f421 100644 --- a/src/maestro/mod.rs +++ b/src/maestro/mod.rs @@ -45,6 +45,7 @@ use crate::stats::beobachten::BeobachtenStore; use crate::stats::telemetry::TelemetryPolicy; use crate::stats::{ReplayChecker, Stats}; use crate::stream::BufferPool; +use crate::synlimit_control; use crate::transport::UpstreamManager; use crate::transport::middle_proxy::MePool; use helpers::{ @@ -906,6 +907,9 @@ async fn run_telemt_core( std::process::exit(1); } + synlimit_control::reconcile_synlimit_rules(&config).await; + synlimit_control::spawn_synlimit_controller(config_rx.clone()); + // On Unix, caller supplies privilege drop after bind (may require root for port < 1024). drop_after_bind(); diff --git a/src/maestro/shutdown.rs b/src/maestro/shutdown.rs index 790b529..6677a5c 100644 --- a/src/maestro/shutdown.rs +++ b/src/maestro/shutdown.rs @@ -19,6 +19,7 @@ use tokio::signal::unix::{SignalKind, signal}; use tracing::{info, warn}; use crate::stats::Stats; +use crate::synlimit_control; use crate::transport::middle_proxy::MePool; use super::helpers::{format_uptime, unit_label}; @@ -102,6 +103,8 @@ async fn perform_shutdown( let uptime_secs = process_started_at.elapsed().as_secs(); info!("Uptime: {}", format_uptime(uptime_secs)); + synlimit_control::clear_synlimit_rules_all_backends().await; + // Graceful ME pool shutdown if let Some(pool) = &me_pool { match tokio::time::timeout(Duration::from_secs(2), pool.shutdown_send_close_conn_all()) diff --git a/src/main.rs b/src/main.rs index 7d0b377..98c9fd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ mod service; mod startup; mod stats; mod stream; +mod synlimit_control; mod tls_front; mod transport; mod util; diff --git a/src/protocol/tls.rs b/src/protocol/tls.rs index 5119805..b71d28a 100644 --- a/src/protocol/tls.rs +++ b/src/protocol/tls.rs @@ -638,15 +638,21 @@ fn build_server_hello_key_share_for_group( group: u16, rng: &SecureRandom, ) -> Option { + let expected_key_exchange_len = client_hello_key_share_group_len(group)?; + client_hello_key_share_group_entry(handshake, group, expected_key_exchange_len)?; + + // FakeTLS clients validate ServerHello shape and digest, not TLS traffic + // secrets, so the response must mirror the offered group without binding to + // the camouflage key bytes embedded in ClientHello. match group { - TLS_NAMED_GROUP_X25519MLKEM768 => { - let key_exchange = build_x25519mlkem768_server_key_share(handshake, rng)?; - Some(ServerHelloKeyShare::new(group, key_exchange)) - } - TLS_NAMED_GROUP_X25519 => { - let key_exchange = build_x25519_server_key_share(handshake, rng)?; - Some(ServerHelloKeyShare::new(group, key_exchange)) - } + TLS_NAMED_GROUP_X25519MLKEM768 => Some(ServerHelloKeyShare::new( + group, + gen_fake_x25519mlkem768_server_key_share(rng), + )), + TLS_NAMED_GROUP_X25519 => Some(ServerHelloKeyShare::new( + group, + gen_fake_x25519_key(rng).to_vec(), + )), _ => None, } } diff --git a/src/synlimit_control.rs b/src/synlimit_control.rs new file mode 100644 index 0000000..466c16e --- /dev/null +++ b/src/synlimit_control.rs @@ -0,0 +1,552 @@ +use std::collections::BTreeSet; +use std::net::IpAddr; +use std::path::PathBuf; +use std::sync::Arc; + +use tokio::io::AsyncWriteExt; +use tokio::process::Command; +use tokio::sync::watch; +use tracing::warn; + +use crate::config::{ProxyConfig, SynLimitMode}; + +const IPTABLES_CHAIN: &str = "TELEMT_SYNLIMIT"; +const IPTABLES_HASHLIMIT_NAME: &str = "TELEMT-BUMPER"; +const NFT_TABLE: &str = "telemt_synlimit"; +const NFT_CHAIN: &str = "input"; +type SynLimitTarget = (Option, u16, u32, u32, u32); + +#[derive(Default)] +struct SynLimitTargets { + iptables_v4: Vec, + iptables_v6: Vec, + nft_v4: Vec, + nft_v6: Vec, +} + +#[derive(Clone, Copy)] +struct NftTableFamilies { + inet: bool, + ip: bool, + ip6: bool, +} + +#[derive(Clone, Copy)] +enum NftFamily { + Inet, + Ip, + Ip6, +} + +struct NftApplyPlan<'a> { + family: NftFamily, + v4_targets: &'a [SynLimitTarget], + v6_targets: &'a [SynLimitTarget], +} + +impl SynLimitTargets { + fn is_empty(&self) -> bool { + self.iptables_v4.is_empty() + && self.iptables_v6.is_empty() + && self.nft_v4.is_empty() + && self.nft_v6.is_empty() + } + + fn has_iptables_targets(&self) -> bool { + !self.iptables_v4.is_empty() || !self.iptables_v6.is_empty() + } + + fn has_nft_targets(&self) -> bool { + !self.nft_v4.is_empty() || !self.nft_v6.is_empty() + } +} +impl NftFamily { + fn as_str(self) -> &'static str { + match self { + Self::Inet => "inet", + Self::Ip => "ip", + Self::Ip6 => "ip6", + } + } +} + +pub(crate) fn spawn_synlimit_controller(config_rx: watch::Receiver>) { + if !cfg!(target_os = "linux") { + if has_synlimit_config(&config_rx.borrow()) { + warn!("SYN limiter is configured but unsupported on this OS; skipping netfilter rules"); + } + return; + } + + tokio::spawn(async move { + wait_for_config_channel_close(config_rx).await; + clear_synlimit_rules_all_backends().await; + }); +} + +async fn wait_for_config_channel_close(mut config_rx: watch::Receiver>) { + while config_rx.changed().await.is_ok() { + config_rx.borrow_and_update(); + } +} + +pub(crate) async fn reconcile_synlimit_rules(cfg: &ProxyConfig) { + clear_synlimit_rules_all_backends().await; + + let targets = synlimit_targets(cfg); + if targets.is_empty() { + return; + } + if !has_cap_net_admin() { + warn!( + "SYN limiter configured but CAP_NET_ADMIN is not available; netfilter rules not applied" + ); + return; + } + + if targets.has_iptables_targets() + && let Err(error) = apply_iptables_synlimit_rules(&targets).await + { + warn!(error = %error, "Failed to apply iptables SYN limiter rules"); + } + if targets.has_nft_targets() + && let Err(error) = apply_nft_synlimit_rules(&targets).await + { + warn!(error = %error, "Failed to apply nftables SYN limiter rules"); + } +} + +pub(crate) async fn clear_synlimit_rules_all_backends() { + clear_nft_synlimit_rules_all_families().await; + clear_iptables_synlimit_rules_for_binary("iptables").await; + clear_iptables_synlimit_rules_for_binary("ip6tables").await; +} + +fn has_synlimit_config(cfg: &ProxyConfig) -> bool { + cfg.server + .listeners + .iter() + .any(|listener| !matches!(listener.synlimit, SynLimitMode::Off)) +} + +fn synlimit_targets(cfg: &ProxyConfig) -> SynLimitTargets { + let mut iptables_v4 = BTreeSet::new(); + let mut iptables_v6 = BTreeSet::new(); + let mut nft_v4 = BTreeSet::new(); + let mut nft_v6 = BTreeSet::new(); + + for listener in &cfg.server.listeners { + let backend = listener.synlimit; + if matches!(backend, SynLimitMode::Off) { + continue; + } + let port = listener.port.unwrap_or(cfg.server.port); + let ip = (!listener.ip.is_unspecified()).then_some(listener.ip); + let seconds = listener.synlimit_seconds; + let hitcount = listener.synlimit_hitcount; + let burst = listener.synlimit_burst; + + match (backend, listener.ip.is_ipv4()) { + (SynLimitMode::Iptables, true) => { + iptables_v4.insert((ip, port, seconds, hitcount, burst)); + } + (SynLimitMode::Iptables, false) => { + iptables_v6.insert((ip, port, seconds, hitcount, burst)); + } + (SynLimitMode::Nftables, true) => { + nft_v4.insert((ip, port, seconds, hitcount, burst)); + } + (SynLimitMode::Nftables, false) => { + nft_v6.insert((ip, port, seconds, hitcount, burst)); + } + (SynLimitMode::Off, _) => {} + } + } + + SynLimitTargets { + iptables_v4: iptables_v4.into_iter().collect(), + iptables_v6: iptables_v6.into_iter().collect(), + nft_v4: nft_v4.into_iter().collect(), + nft_v6: nft_v6.into_iter().collect(), + } +} + +async fn apply_iptables_synlimit_rules(targets: &SynLimitTargets) -> Result<(), String> { + apply_iptables_synlimit_rules_for_binary("iptables", &targets.iptables_v4).await?; + apply_iptables_synlimit_rules_for_binary("ip6tables", &targets.iptables_v6).await +} + +async fn apply_iptables_synlimit_rules_for_binary( + binary: &str, + targets: &[SynLimitTarget], +) -> Result<(), String> { + if targets.is_empty() { + return Ok(()); + } + if !command_exists(binary) { + return Err(format!("{binary} is not available")); + } + + let _ = run_command(binary, &["-t", "filter", "-N", IPTABLES_CHAIN], None).await; + run_command(binary, &["-t", "filter", "-F", IPTABLES_CHAIN], None).await?; + if run_command( + binary, + &["-t", "filter", "-C", "INPUT", "-j", IPTABLES_CHAIN], + None, + ) + .await + .is_err() + { + run_command( + binary, + &["-t", "filter", "-A", "INPUT", "-j", IPTABLES_CHAIN], + None, + ) + .await?; + } + + for (idx, (ip, port, seconds, hitcount, burst)) in targets.iter().enumerate() { + let hashlimit_name = format!("{IPTABLES_HASHLIMIT_NAME}-{idx}"); + let accept_args = iptables_hashlimit_accept_rule_args( + ip, + *port, + *seconds, + *hitcount, + *burst, + &hashlimit_name, + ); + let drop_args = iptables_synlimit_drop_rule_args(ip, *port); + let drop_refs: Vec<&str> = drop_args.iter().map(String::as_str).collect(); + let accept_refs: Vec<&str> = accept_args.iter().map(String::as_str).collect(); + run_command(binary, &accept_refs, None).await?; + run_command(binary, &drop_refs, None).await?; + } + run_command( + binary, + &["-t", "filter", "-A", IPTABLES_CHAIN, "-j", "RETURN"], + None, + ) + .await?; + + Ok(()) +} + +fn iptables_hashlimit_accept_rule_args( + ip: &Option, + port: u16, + seconds: u32, + hitcount: u32, + burst: u32, + hashlimit_name: &str, +) -> Vec { + let mut args = vec![ + "-t".to_string(), + "filter".to_string(), + "-A".to_string(), + IPTABLES_CHAIN.to_string(), + "-p".to_string(), + "tcp".to_string(), + "--syn".to_string(), + ]; + if let Some(ip) = ip { + args.push("-d".to_string()); + args.push(ip.to_string()); + } + let rate = synlimit_rate_arg(seconds, hitcount); + args.extend([ + "--dport".to_string(), + port.to_string(), + "-m".to_string(), + "hashlimit".to_string(), + "--hashlimit-name".to_string(), + hashlimit_name.to_string(), + "--hashlimit-mode".to_string(), + "srcip".to_string(), + "--hashlimit-upto".to_string(), + rate, + "--hashlimit-burst".to_string(), + burst.to_string(), + "--hashlimit-htable-expire".to_string(), + "15000".to_string(), + "-j".to_string(), + "ACCEPT".to_string(), + ]); + args +} + +fn iptables_synlimit_drop_rule_args(ip: &Option, port: u16) -> Vec { + let mut args = vec![ + "-t".to_string(), + "filter".to_string(), + "-A".to_string(), + IPTABLES_CHAIN.to_string(), + "-p".to_string(), + "tcp".to_string(), + "--syn".to_string(), + ]; + if let Some(ip) = ip { + args.push("-d".to_string()); + args.push(ip.to_string()); + } + args.extend([ + "--dport".to_string(), + port.to_string(), + "-j".to_string(), + "DROP".to_string(), + ]); + args +} + +fn synlimit_rate_arg(seconds: u32, hitcount: u32) -> String { + let seconds = u64::from(seconds.max(1)); + let hitcount = u64::from(hitcount.max(1)); + for (unit_seconds, unit_name) in [ + (1_u64, "second"), + (60_u64, "minute"), + (3_600_u64, "hour"), + (86_400_u64, "day"), + ] { + let amount = hitcount.saturating_mul(unit_seconds); + if amount >= seconds && amount % seconds == 0 { + return format!("{}/{}", amount / seconds, unit_name); + } + } + let amount = hitcount.saturating_mul(86_400).saturating_add(seconds - 1) / seconds; + format!("{}/day", amount.max(1)) +} + +async fn clear_iptables_synlimit_rules_for_binary(binary: &str) { + if !command_exists(binary) { + return; + } + for _ in 0..8 { + if run_command( + binary, + &["-t", "filter", "-D", "INPUT", "-j", IPTABLES_CHAIN], + None, + ) + .await + .is_err() + { + break; + } + } + let _ = run_command(binary, &["-t", "filter", "-F", IPTABLES_CHAIN], None).await; + let _ = run_command(binary, &["-t", "filter", "-X", IPTABLES_CHAIN], None).await; +} + +async fn apply_nft_synlimit_rules(targets: &SynLimitTargets) -> Result<(), String> { + if !command_exists("nft") { + return Err("nft is not available".to_string()); + } + + let families = detect_nft_table_families().await; + for plan in nft_apply_plan(families, &targets.nft_v4, &targets.nft_v6) { + let script = nft_synlimit_script(plan); + run_command("nft", &["-f", "-"], Some(script)).await?; + } + + Ok(()) +} + +async fn detect_nft_table_families() -> NftTableFamilies { + let Ok(output) = run_command_stdout("nft", &["list", "tables"]).await else { + return NftTableFamilies { + inet: false, + ip: false, + ip6: false, + }; + }; + + let mut families = NftTableFamilies { + inet: false, + ip: false, + ip6: false, + }; + for line in output.lines() { + let mut fields = line.split_whitespace(); + if fields.next() != Some("table") { + continue; + } + match fields.next() { + Some("inet") => families.inet = true, + Some("ip") => families.ip = true, + Some("ip6") => families.ip6 = true, + _ => {} + } + } + families +} +fn nft_apply_plan<'a>( + families: NftTableFamilies, + v4_targets: &'a [SynLimitTarget], + v6_targets: &'a [SynLimitTarget], +) -> Vec> { + if !v4_targets.is_empty() && !v6_targets.is_empty() { + return vec![NftApplyPlan { + family: NftFamily::Inet, + v4_targets, + v6_targets, + }]; + } + if !v4_targets.is_empty() { + return vec![NftApplyPlan { + family: if families.inet || !families.ip { + NftFamily::Inet + } else { + NftFamily::Ip + }, + v4_targets, + v6_targets: &[], + }]; + } + if !v6_targets.is_empty() { + return vec![NftApplyPlan { + family: if families.inet || !families.ip6 { + NftFamily::Inet + } else { + NftFamily::Ip6 + }, + v4_targets: &[], + v6_targets, + }]; + } + Vec::new() +} +fn nft_synlimit_script(plan: NftApplyPlan<'_>) -> String { + let mut script = String::new(); + script.push_str(&format!("table {} {NFT_TABLE} {{\n", plan.family.as_str())); + script.push_str(&format!(" chain {NFT_CHAIN} {{\n")); + script.push_str(" type filter hook input priority filter; policy accept;\n"); + for (idx, (ip, port, seconds, hitcount, burst)) in plan.v4_targets.iter().enumerate() { + let daddr = ip + .map(|ip| format!(" ip daddr {ip}")) + .unwrap_or_else(String::new); + let rate = synlimit_rate_arg(*seconds, *hitcount); + script.push_str(&format!( + " tcp flags & (fin|syn|rst|ack) == syn{daddr} tcp dport {port} meter telemt_synlimit_v4_{idx} {{ ip saddr limit rate over {rate} burst {burst} packets }} drop\n" + )); + script.push_str(&format!( + " tcp flags & (fin|syn|rst|ack) == syn{daddr} tcp dport {port} accept\n" + )); + } + for (idx, (ip, port, seconds, hitcount, burst)) in plan.v6_targets.iter().enumerate() { + let daddr = ip + .map(|ip| format!(" ip6 daddr {ip}")) + .unwrap_or_else(String::new); + let rate = synlimit_rate_arg(*seconds, *hitcount); + script.push_str(&format!( + " tcp flags & (fin|syn|rst|ack) == syn{daddr} tcp dport {port} meter telemt_synlimit_v6_{idx} {{ ip6 saddr limit rate over {rate} burst {burst} packets }} drop\n" + )); + script.push_str(&format!( + " tcp flags & (fin|syn|rst|ack) == syn{daddr} tcp dport {port} accept\n" + )); + } + script.push_str(" }\n"); + script.push_str("}\n"); + script +} + +async fn clear_nft_synlimit_rules_all_families() { + if !command_exists("nft") { + return; + } + for family in [NftFamily::Inet, NftFamily::Ip, NftFamily::Ip6] { + let _ = run_command( + "nft", + &["delete", "table", family.as_str(), NFT_TABLE], + None, + ) + .await; + } +} + +async fn run_command(binary: &str, args: &[&str], stdin: Option) -> Result<(), String> { + if !command_exists(binary) { + return Err(format!("{binary} is not available")); + } + let mut command = Command::new(binary); + command.args(args); + if stdin.is_some() { + command.stdin(std::process::Stdio::piped()); + } + command.stdout(std::process::Stdio::null()); + command.stderr(std::process::Stdio::piped()); + let mut child = command + .spawn() + .map_err(|e| format!("spawn {binary} failed: {e}"))?; + if let Some(blob) = stdin + && let Some(mut writer) = child.stdin.take() + { + writer + .write_all(blob.as_bytes()) + .await + .map_err(|e| format!("stdin write {binary} failed: {e}"))?; + } + let output = child + .wait_with_output() + .await + .map_err(|e| format!("wait {binary} failed: {e}"))?; + if output.status.success() { + return Ok(()); + } + let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); + Err(if stderr.is_empty() { + format!("{binary} exited with status {}", output.status) + } else { + stderr + }) +} + +async fn run_command_stdout(binary: &str, args: &[&str]) -> Result { + if !command_exists(binary) { + return Err(format!("{binary} is not available")); + } + let output = Command::new(binary) + .args(args) + .output() + .await + .map_err(|e| format!("wait {binary} failed: {e}"))?; + if output.status.success() { + return Ok(String::from_utf8_lossy(&output.stdout).to_string()); + } + let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); + Err(if stderr.is_empty() { + format!("{binary} exited with status {}", output.status) + } else { + stderr + }) +} + +fn command_exists(binary: &str) -> bool { + let Some(path_var) = std::env::var_os("PATH") else { + return false; + }; + std::env::split_paths(&path_var).any(|dir| { + let candidate: PathBuf = dir.join(binary); + candidate.exists() && candidate.is_file() + }) +} + +fn has_cap_net_admin() -> bool { + #[cfg(target_os = "linux")] + { + let Ok(status) = std::fs::read_to_string("/proc/self/status") else { + return false; + }; + for line in status.lines() { + if let Some(raw) = line.strip_prefix("CapEff:") { + let caps = raw.trim(); + if let Ok(bits) = u64::from_str_radix(caps, 16) { + const CAP_NET_ADMIN_BIT: u64 = 12; + return (bits & (1u64 << CAP_NET_ADMIN_BIT)) != 0; + } + } + } + false + } + #[cfg(not(target_os = "linux"))] + { + false + } +}