Compare commits
No commits in common. "c42392dd0e987e176ff5281ff97b352edf2a0af5" and "a6a6fc7348ed7cb59e2e01f4b69ad216a5ee93d8" have entirely different histories.
c42392dd0e
...
a6a6fc7348
56 changed files with 1030 additions and 2895 deletions
1160
Cargo.lock
generated
1160
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
21
Cargo.toml
21
Cargo.toml
|
|
@ -40,7 +40,6 @@ io-uring = ["dep:tokio-splice"]
|
|||
# HTTP/1.1 protocol handler: парсинг request-head, verdict Allow/Reject,
|
||||
# туннелирование на upstream (см. src/protocol/http/ и [protocol.http] в конфиге).
|
||||
protocol-http = []
|
||||
protocol-minecraft = []
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
|
|
@ -52,14 +51,14 @@ thiserror = "2"
|
|||
anyhow = "1"
|
||||
dashmap = "6"
|
||||
hex = "0.4"
|
||||
sha2 = "0.11"
|
||||
sha2 = "0.10"
|
||||
subtle = "2"
|
||||
socket2 = { version = "0.6", features = ["all"] }
|
||||
socket2 = { version = "0.5", features = ["all"] }
|
||||
futures = "0.3"
|
||||
reqwest = { version = "0.13", default-features = false, features = ["json", "query", "rustls"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
prometheus = { version = "0.14", features = ["process"] }
|
||||
toml = "1.1"
|
||||
rand = { version = "0.10", default-features = false, features = ["std", "std_rng", "thread_rng"] }
|
||||
toml = "0.8"
|
||||
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
|
|
@ -68,11 +67,11 @@ ratatui = "0.30"
|
|||
crossterm = "0.29"
|
||||
|
||||
axum = "0.8"
|
||||
tower-http = { version = "0.7", features = ["cors"] }
|
||||
jsonwebtoken = "11"
|
||||
tower-http = { version = "0.6", features = ["cors"] }
|
||||
jsonwebtoken = "9"
|
||||
|
||||
redis = { version = "1.7", optional = true, features = ["tokio-comp", "connection-manager"] }
|
||||
maxminddb = { version = "0.32", optional = true }
|
||||
redis = { version = "0.27", optional = true, features = ["tokio-comp", "connection-manager"] }
|
||||
maxminddb = { version = "0.30", optional = true }
|
||||
tokio-splice = { version = "0.2", optional = true }
|
||||
libbpf-rs = { version = "0.27", optional = true }
|
||||
libbpf-rs = { version = "0.24", optional = true }
|
||||
libc = { version = "0.2", optional = true }
|
||||
|
|
|
|||
35
README.md
35
README.md
|
|
@ -54,7 +54,7 @@ Protocol-specific logic lives in **modular protocol plugins**, so the same platf
|
|||
│ L7 handshake analysis · rate limit · HMAC · death-code patterns │
|
||||
├────────────────────────────────────────────────────────────────────┤
|
||||
│ Layer 4: Protocol Plugins (feature crates) │
|
||||
│ http (protocol-http) · Minecraft Java (protocol-minecraft) · grpc (planned) │
|
||||
│ http (feature protocol-http) · grpc (planned) │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
Traffic Intel (EWMA thresholds, profiling, reputation)
|
||||
runs across all layers
|
||||
|
|
@ -75,7 +75,6 @@ Attacker → [XDP/eBPF] → [PoW] → [Userspace Core] → [Plugin] → Your Ser
|
|||
| **rampart-tui** | Live metrics terminal dashboard, polls the Prometheus `/metrics` endpoint | Rust (ratatui) |
|
||||
| **protocol plugins** | Protocol-aware filtering as feature crates | Rust |
|
||||
| ↳ `http` | HTTP/1.1 handler, compiled with the `protocol-http` feature | Rust |
|
||||
| ↳ `minecraft` | Minecraft Java TCP handshake and tunnel, compiled with `protocol-minecraft` | Rust |
|
||||
| ↳ `grpc` | Planned | Rust |
|
||||
| **docs/kb** | Bilingual knowledge base: attack anatomy, defense levels, practice guides | Markdown |
|
||||
|
||||
|
|
@ -94,26 +93,17 @@ Full benchmark suite in progress.
|
|||
### Quick Start
|
||||
|
||||
```bash
|
||||
# Build the HTTP edge handler
|
||||
# Build (the HTTP protocol handler is a feature)
|
||||
cargo build --release --features protocol-http
|
||||
|
||||
# Install the default config
|
||||
sudo mkdir -p /etc/rampart
|
||||
sudo cp deploy/config/edge-http.toml /etc/rampart/config.toml
|
||||
sudo cp deploy/config/edge.toml /etc/rampart/config.toml
|
||||
|
||||
# Run edge node (config path comes from $RAMPART_CONFIG, default /etc/rampart/config.toml)
|
||||
RAMPART_CONFIG=/etc/rampart/config.toml ./target/release/rampart
|
||||
```
|
||||
|
||||
### Minecraft Java Edge
|
||||
|
||||
Rampart also has an early Minecraft Java TCP handler. Build it with
|
||||
`cargo build --release --features protocol-minecraft --bin rampart`, copy
|
||||
`deploy/config/edge.toml` as its config, and follow
|
||||
[docs/minecraft-java.md](docs/minecraft-java.md) for Paper PROXY-protocol and
|
||||
private-backend setup. This handler does not protect an already-saturated home
|
||||
uplink; use a DDoS-filtering game proxy/provider in front of the edge.
|
||||
|
||||
### Documentation
|
||||
|
||||
| Path | Description |
|
||||
|
|
@ -123,7 +113,6 @@ uplink; use a DDoS-filtering game proxy/provider in front of the edge.
|
|||
| [docs/research/load-test-report.md](docs/research/load-test-report.md) | VDS stress test report (2026-08-04) |
|
||||
| [docs/research/](docs/research/) | Research notes: eBPF, anti-bot, DDoS vectors, networking |
|
||||
| [docs/deployment.md](docs/deployment.md) | Deployment guide |
|
||||
| [docs/minecraft-java.md](docs/minecraft-java.md) | Minecraft Java edge and Paper setup |
|
||||
| [docs/configuration.md](docs/configuration.md) | Configuration reference |
|
||||
| [docs/runbook.md](docs/runbook.md) | Operations runbook |
|
||||
|
||||
|
|
@ -164,7 +153,7 @@ Rampart фильтрует трафик на трёх уровнях до тог
|
|||
│ L7 handshake analysis · rate limit · HMAC · death-code паттерны │
|
||||
├────────────────────────────────────────────────────────────────────┤
|
||||
│ Слой 4: Протокол-плагины (feature crates) │
|
||||
│ http (protocol-http) · Minecraft Java (protocol-minecraft) · grpc (в планах) │
|
||||
│ http (feature protocol-http) · grpc (в планах) │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
Traffic Intel (EWMA thresholds, профилирование, репутация)
|
||||
работает поперёк всех слоёв
|
||||
|
|
@ -185,7 +174,6 @@ Rampart фильтрует трафик на трёх уровнях до тог
|
|||
| **rampart-tui** | Терминальный дашборд live-метрик, опрашивает Prometheus `/metrics` | Rust (ratatui) |
|
||||
| **Протокол-плагины** | Протоколозависимая фильтрация в виде feature crates | Rust |
|
||||
| ↳ `http` | HTTP/1.1-обработчик, собирается с фичей `protocol-http` | Rust |
|
||||
| ↳ `minecraft` | TCP-handshake и туннель Minecraft Java, фича `protocol-minecraft` | Rust |
|
||||
| ↳ `grpc` | В планах | Rust |
|
||||
| **docs/kb** | Двуязычная база знаний: анатомия атак, уровни защиты, практические руководства | Markdown |
|
||||
|
||||
|
|
@ -204,27 +192,17 @@ Rampart фильтрует трафик на трёх уровнях до тог
|
|||
### Быстрый старт
|
||||
|
||||
```bash
|
||||
# Сборка HTTP-обработчика
|
||||
# Сборка (HTTP-обработчик собирается фичей)
|
||||
cargo build --release --features protocol-http
|
||||
|
||||
# Установка дефолтного конфига
|
||||
sudo mkdir -p /etc/rampart
|
||||
sudo cp deploy/config/edge-http.toml /etc/rampart/config.toml
|
||||
sudo cp deploy/config/edge.toml /etc/rampart/config.toml
|
||||
|
||||
# Запуск edge ноды (путь конфига берётся из $RAMPART_CONFIG, по умолчанию /etc/rampart/config.toml)
|
||||
RAMPART_CONFIG=/etc/rampart/config.toml ./target/release/rampart
|
||||
```
|
||||
|
||||
### Minecraft Java Edge
|
||||
|
||||
В проекте появился ранний TCP-обработчик Minecraft Java. Соберите его командой
|
||||
`cargo build --release --features protocol-minecraft --bin rampart`, скопируйте
|
||||
`deploy/config/edge.toml` как конфиг и настройте
|
||||
Paper и закрытый backend по инструкции
|
||||
[docs/minecraft-java.md](docs/minecraft-java.md). Сам Rampart не спасёт домашний
|
||||
канал, если атака уже забила его: перед edge нужен игровой прокси или провайдер
|
||||
с фильтрацией DDoS.
|
||||
|
||||
### Документация
|
||||
|
||||
| Путь | Описание |
|
||||
|
|
@ -234,7 +212,6 @@ Paper и закрытый backend по инструкции
|
|||
| [docs/research/load-test-report.md](docs/research/load-test-report.md) | Отчёт по VDS stress test (2026-08-04) |
|
||||
| [docs/research/](docs/research/) | Research-заметки: eBPF, антибот, векторы DDoS, сети |
|
||||
| [docs/deployment.md](docs/deployment.md) | Руководство по деплою |
|
||||
| [docs/minecraft-java.md](docs/minecraft-java.md) | Minecraft Java edge и настройка Paper |
|
||||
| [docs/configuration.md](docs/configuration.md) | Справочник конфигурации |
|
||||
| [docs/runbook.md](docs/runbook.md) | Операционный runbook |
|
||||
|
||||
|
|
|
|||
6
TODO.md
6
TODO.md
|
|
@ -98,8 +98,6 @@ guard/
|
|||
### Движок без протоколов — сделать полезным — ✅ готово
|
||||
- [x] **Первый протокол-плагин**: `protocol-http` — HTTP/1.1 request-head анализ
|
||||
(src/protocol/http/, tests/http_protocol.rs).
|
||||
- [x] **Minecraft Java TCP MVP**: ограниченный handshake status/login, передача исходных
|
||||
байтов backend и опциональный PROXY protocol v2 (`protocol-minecraft`).
|
||||
- [x] **TCP-proxy режим**: tunnel.rs + ProtocolHandler интегрированы в listener/app wiring.
|
||||
- [x] **Fail-fast сообщение** при пустом registry (`ProtocolRegistry::primary()`).
|
||||
|
||||
|
|
@ -151,10 +149,6 @@ guard/
|
|||
bin-файлы тонкие (rampart.rs: 330 → 6 строк).
|
||||
|
||||
### Новые найденные проблемы (чинить в v0.4)
|
||||
- [x] Redis blacklist: срок/причина сохраняются; edge восстанавливает снимок после reconnect;
|
||||
локальные autoban-события публикуются в Redis с retry и absolute expiry.
|
||||
- [ ] Minecraft Java: Velocity modern forwarding, игровые netns smoke-тесты и нагрузочная проверка.
|
||||
- [x] Принимать client IP от внешнего DDoS-прокси через доверенные CIDR и ограниченный PROXY v2 parser.
|
||||
- [x] `cargo check --no-default-features` починен (2026-09-15, раунд 2): redis-код в
|
||||
manager/sync/api/store/app закрыт `#[cfg(feature = "store-redis")]`, менеджер без фичи fail-fast'ит на старте;
|
||||
CI теперь гоняет `cargo check --no-default-features --all-targets` гейтом.
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
# Minecraft Java edge configuration for deploy/docker-compose.yml.
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
|
||||
[bind]
|
||||
address = "0.0.0.0"
|
||||
port = 25565
|
||||
|
||||
[backend]
|
||||
# Docker host address; expose this backend port only to trusted Rampart nodes.
|
||||
upstreams = ["host.docker.internal:25566"]
|
||||
|
||||
[protocol.minecraft]
|
||||
enabled = true
|
||||
max_frame_bytes = 8192
|
||||
timeout_secs = 5
|
||||
send_proxy_protocol = false
|
||||
|
||||
[workers]
|
||||
count = 4
|
||||
|
||||
[limits]
|
||||
handshake_timeout_secs = 5
|
||||
max_connections_per_ip = 10
|
||||
rate_limit_pps = 5.0
|
||||
rate_limit_burst = 10.0
|
||||
|
||||
[ban]
|
||||
ban_duration_secs = 3600
|
||||
|
||||
[store]
|
||||
redis_url = "redis://redis:6379/0"
|
||||
blacklist_cache_ttl_secs = 300
|
||||
clickhouse_url = ""
|
||||
|
||||
[xdp]
|
||||
enabled = false
|
||||
interface = "eth0"
|
||||
|
||||
[logging]
|
||||
level = "info"
|
||||
format = "text"
|
||||
|
||||
[metrics]
|
||||
enabled = true
|
||||
port = 9090
|
||||
|
||||
[pow]
|
||||
enabled = false
|
||||
difficulty = 4
|
||||
|
||||
# Enable only when a trusted upstream TCP proxy sends PROXY protocol v2.
|
||||
[proxy_protocol]
|
||||
ingress_enabled = false
|
||||
trusted_cidrs = []
|
||||
ingress_timeout_ms = 750
|
||||
max_header_bytes = 512
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
# Generic HTTP/1.1 edge configuration.
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
|
||||
[bind]
|
||||
address = "0.0.0.0"
|
||||
port = 25565
|
||||
|
||||
[backend]
|
||||
upstreams = ["127.0.0.1:25566"]
|
||||
|
||||
[workers]
|
||||
count = 4
|
||||
|
||||
[limits]
|
||||
handshake_timeout_secs = 5
|
||||
max_connections_per_ip = 10
|
||||
rate_limit_pps = 5.0
|
||||
rate_limit_burst = 10.0
|
||||
|
||||
[ban]
|
||||
ban_duration_secs = 3600
|
||||
|
||||
[store]
|
||||
redis_url = ""
|
||||
blacklist_cache_ttl_secs = 300
|
||||
clickhouse_url = ""
|
||||
|
||||
[xdp]
|
||||
enabled = false
|
||||
interface = "eth0"
|
||||
|
||||
[logging]
|
||||
level = "info"
|
||||
format = "text"
|
||||
|
||||
[metrics]
|
||||
enabled = true
|
||||
port = 9090
|
||||
|
||||
[pow]
|
||||
enabled = false
|
||||
difficulty = 4
|
||||
|
|
@ -1,23 +1,13 @@
|
|||
# Rampart edge для Minecraft Java (схема: src/config/sections/)
|
||||
# whitelist — корневое поле, поэтому оно задаётся до любых TOML-секций.
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
# Rampart edge — единый конфиг (схема: src/config/sections.rs)
|
||||
|
||||
[bind]
|
||||
address = "0.0.0.0"
|
||||
port = 25565
|
||||
|
||||
[backend]
|
||||
# Адрес Paper за доверенным прокси; закрой этот порт firewall-ом от интернета.
|
||||
# Generic TCP-бэкенды за edge-нодой (addr:port)
|
||||
upstreams = ["127.0.0.1:25566"]
|
||||
|
||||
[protocol.minecraft]
|
||||
enabled = true
|
||||
max_frame_bytes = 8192
|
||||
timeout_secs = 5
|
||||
# Outbound: включай вместе с proxies.proxy-protocol: true на Paper и firewall,
|
||||
# разрешающим backend-порт только от этого Rampart.
|
||||
send_proxy_protocol = false
|
||||
|
||||
[workers]
|
||||
count = 4
|
||||
|
||||
|
|
@ -51,10 +41,4 @@ port = 9090
|
|||
enabled = false
|
||||
difficulty = 4
|
||||
|
||||
# Inbound: включай только если входящий TCP-прокси передаёт PROXY v2.
|
||||
# Укажи точные адреса/CIDR этого прокси; не добавляй сюда адреса игроков.
|
||||
[proxy_protocol]
|
||||
ingress_enabled = false
|
||||
trusted_cidrs = []
|
||||
ingress_timeout_ms = 750
|
||||
max_header_bytes = 512
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
|
|
|
|||
|
|
@ -44,13 +44,11 @@ services:
|
|||
dockerfile: deploy/docker/Dockerfile.edge
|
||||
ports:
|
||||
- "25565:25565"
|
||||
- "127.0.0.1:9090:9090"
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
- "9090:9090"
|
||||
environment:
|
||||
RAMPART_CONFIG: /etc/rampart/config.toml
|
||||
volumes:
|
||||
- ./config/edge-docker.toml:/etc/rampart/config.toml:ro
|
||||
- ./config/edge.toml:/etc/rampart/config.toml:ro
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ RUN if command -v clang >/dev/null; then \
|
|||
clang -O2 -g -target bpf -c xdp/core/universal_filter.c -o /app/target/xdp/universal_filter.o; \
|
||||
fi
|
||||
|
||||
RUN cargo build --release --features xdp,protocol-minecraft --bin rampart && \
|
||||
RUN cargo build --release --features xdp --bin rampart && \
|
||||
cp target/release/rampart /app/rampart && \
|
||||
strip /app/rampart
|
||||
|
||||
|
|
|
|||
30
docs/api.md
30
docs/api.md
|
|
@ -56,7 +56,7 @@ Authorization: Bearer <token>
|
|||
}
|
||||
```
|
||||
|
||||
Если Redis недоступен, API возвращает `503 Service Unavailable`.
|
||||
Redis недоступен → пустой список.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -68,16 +68,13 @@ Authorization: Bearer <token>
|
|||
{
|
||||
"items": [
|
||||
{"target": "1.2.3.4", "type": "ip", "reason": "manual_ban",
|
||||
"created_at": "2026-10-16T12:00:00+00:00", "expires_at": "2026-10-16T13:00:00+00:00"}
|
||||
"created_at": "...", "expires_at": null}
|
||||
],
|
||||
"total": 42
|
||||
}
|
||||
```
|
||||
|
||||
Источник: Redis set `rampart:blacklist` и hash `rampart:blacklist:metadata`.
|
||||
Метаданные содержат причину, время создания и абсолютный срок истечения; API
|
||||
скрывает истёкшие записи. Старые set-записи без метаданных получают один
|
||||
ограниченный срок в пять минут при первом чтении или восстановлении edge.
|
||||
Источник: Redis set `rampart:blacklist`. Пагинации и фильтров нет.
|
||||
|
||||
### `POST /api/v1/blacklist` 🔒
|
||||
|
||||
|
|
@ -85,24 +82,10 @@ Authorization: Bearer <token>
|
|||
// Request
|
||||
{"target": "1.2.3.4", "type": "ip", "reason": "manual_ban", "duration_secs": 3600}
|
||||
|
||||
// Response 200 — запись и метаданные атомарно сохранены в Redis, а событие
|
||||
// с duration_secs и expires_at_unix опубликовано в rampart:blacklist:events.
|
||||
// Response 200/201 — запись добавлена в Redis set
|
||||
```
|
||||
|
||||
Edge подписывается на Pub/Sub перед загрузкой снимка из Redis. Поэтому события,
|
||||
пришедшие во время загрузки, будут применены после снимка. При старте и после
|
||||
переподключения активные Redis-баны восстанавливаются с оставшимся TTL; новые
|
||||
события используют абсолютный срок и игнорируются, если он уже прошёл. Старый
|
||||
формат события только с `duration_secs` продолжает поддерживаться.
|
||||
|
||||
### `DELETE /api/v1/blacklist` 🔒
|
||||
|
||||
```json
|
||||
{"target": "1.2.3.4"}
|
||||
```
|
||||
|
||||
Удаляет только Redis-бан и его метаданные, затем публикует событие unban.
|
||||
Локальные баны детекторов хранятся отдельно и этим запросом не снимаются.
|
||||
Edge-ноды подхватывают ban при следующей синхронизации блэклиста.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -127,7 +110,8 @@ Edge подписывается на Pub/Sub перед загрузкой сн
|
|||
|
||||
Честно, на текущий момент в API **нет**:
|
||||
|
||||
- drain нод через API или CLI;
|
||||
- удаления записей blacklist (только add/list);
|
||||
- drain нод через API (есть только CLI `rampart-cli drain`);
|
||||
- webhooks, metrics summary, challenge management;
|
||||
- регистрации нод через `POST /api/v1/nodes`;
|
||||
- rate limiting на сами запросы API (кроме login).
|
||||
|
|
|
|||
|
|
@ -6,23 +6,14 @@
|
|||
## 1. Edge (`config.toml`, путь задаётся `RAMPART_CONFIG`)
|
||||
|
||||
```toml
|
||||
# Корневые поля должны стоять до первой TOML-секции.
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
|
||||
[bind]
|
||||
address = "0.0.0.0"
|
||||
port = 25565
|
||||
|
||||
[backend]
|
||||
# Список TCP-бэкендов (addr:port). Для Minecraft нужен ровно один backend.
|
||||
# Список TCP-бэкендов (addr:port). Обязателен непустой, адреса валидируются.
|
||||
upstreams = ["127.0.0.1:25566"]
|
||||
|
||||
[protocol.minecraft]
|
||||
enabled = true
|
||||
max_frame_bytes = 8192
|
||||
timeout_secs = 5
|
||||
send_proxy_protocol = false
|
||||
|
||||
[workers]
|
||||
count = 4
|
||||
|
||||
|
|
@ -56,24 +47,10 @@ port = 9090
|
|||
enabled = false # PoW challenge, OFF по умолчанию
|
||||
difficulty = 4
|
||||
|
||||
# Входящий PROXY v2 от известного TCP DDoS-прокси; включай только если
|
||||
# прокси действительно отправляет этот заголовок.
|
||||
[proxy_protocol]
|
||||
ingress_enabled = false
|
||||
trusted_cidrs = []
|
||||
ingress_timeout_ms = 750
|
||||
max_header_bytes = 512
|
||||
|
||||
whitelist = ["127.0.0.1", "::1"] # только IP-адреса, валидируются при парсинге
|
||||
```
|
||||
|
||||
Этот пример выбирает Minecraft Java и требует сборки с `protocol-minecraft`.
|
||||
Для HTTP используй сборку `protocol-http` и удали `[protocol.minecraft]`.
|
||||
|
||||
Все секции опциональны — при отсутствии используются дефолты из таблицы выше.
|
||||
Для ingress PROXY v2 укажи сети именно прокси в `trusted_cidrs`; включение без
|
||||
доверенной сети отклоняется при чтении конфига.
|
||||
`send_proxy_protocol` отправляет заголовок на Paper backend; прежнее имя
|
||||
`proxy_protocol` внутри `[protocol.minecraft]` остаётся допустимым alias.
|
||||
|
||||
## 2. Manager (переменные окружения)
|
||||
|
||||
|
|
@ -96,8 +73,7 @@ Manager всегда слушает `0.0.0.0:8080`.
|
|||
| `geoip` | ❌ | GeoIP lookup (maxminddb) |
|
||||
| `xdp` | ❌ | Загрузка XDP-программы через libbpf-rs |
|
||||
| `io-uring` | ❌ | splice/io_uring I/O |
|
||||
| `protocol-http` | ❌ | HTTP/1.1 handler; включай только для HTTP upstream |
|
||||
| `protocol-minecraft` | ❌ | Minecraft Java TCP handshake и проксирование |
|
||||
| `protocol-http` | ❌ | Резерв под HTTP-плагин (пока пустой) |
|
||||
|
||||
Пример: `cargo build --release --features xdp --bin rampart`.
|
||||
|
||||
|
|
@ -111,4 +87,4 @@ clang -O2 -g -target bpf -c xdp/core/universal_filter.c -o /tmp/universal_filter
|
|||
|
||||
---
|
||||
|
||||
*Версия: 2.1 | Октябрь 2026*
|
||||
*Версия: 2.0 | Август 2026*
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@
|
|||
## 3. Быстрый старт — Edge
|
||||
|
||||
```bash
|
||||
# Сборка edge для Minecraft Java
|
||||
# Сборка
|
||||
git clone https://github.com/loki5512344/rampart.git && cd rampart
|
||||
cargo build --release --features protocol-minecraft --bin rampart
|
||||
cargo build --release --bin rampart
|
||||
|
||||
# Установка
|
||||
sudo cp target/release/rampart /usr/local/bin/
|
||||
|
|
@ -64,10 +64,10 @@ systemctl enable --now rampart
|
|||
journalctl -u rampart -f
|
||||
```
|
||||
|
||||
Minecraft-конфиг рассчитан на один Paper backend. Если включён
|
||||
`protocol.minecraft.send_proxy_protocol`, включи `proxies.proxy-protocol: true` в
|
||||
Paper и закрой backend-порт firewall-ом от всех, кроме Rampart. Для подробной
|
||||
схемы с домашним сервером и защищённым VDS см. [Minecraft Java guide](minecraft-java.md).
|
||||
> ⚠️ В ядре нет ни одной реализации протокольного обработчика: реестр протоколов
|
||||
> пуст, и `rampart` при старте требует feature `protocol-http` или внешний
|
||||
> ProtocolHandler (см. src/bin/rampart.rs). До появления плагинов edge-нода
|
||||
> запускается только в исследовательских целях.
|
||||
|
||||
## 4. Быстрый старт — Manager
|
||||
|
||||
|
|
@ -79,27 +79,12 @@ cargo run --release --bin rampart-manager
|
|||
# API на 0.0.0.0:8080
|
||||
```
|
||||
|
||||
Manager listens on all interfaces by default. Permit `8080` only from an admin
|
||||
VPN or specific admin addresses; do not expose the API to the public Internet.
|
||||
The same applies to Redis (`6379`) and monitoring endpoints such as Prometheus
|
||||
(`9090`), ClickHouse (`8123`/`9000`) and Grafana (`3000`). The sample Compose
|
||||
file publishes ClickHouse and Grafana ports on the host, so restrict them with
|
||||
the host firewall or change their port bindings before deploying on a public
|
||||
VDS. Redis has no published host port in that Compose file and is reachable
|
||||
only by containers on its Docker network unless you deliberately expose it.
|
||||
|
||||
Инфраструктура (Redis, ClickHouse, Grafana):
|
||||
|
||||
```bash
|
||||
docker compose up -d redis clickhouse grafana
|
||||
```
|
||||
|
||||
Для нескольких edge и Manager укажи один и тот же защищённый Redis endpoint в
|
||||
`store.redis_url` каждого edge и `REDIS_URL` Manager. Edge передаёт туда
|
||||
локальные автобаны и восстанавливает общие bans после переподключения. Открой
|
||||
Redis только через private network/VPN; при пустом `redis_url` edge остаётся
|
||||
автономным, и автобаны между узлами не распространяются.
|
||||
|
||||
## 5. Docker
|
||||
|
||||
```bash
|
||||
|
|
@ -122,4 +107,4 @@ docker compose -f deploy/docker-compose.yml up -d
|
|||
|
||||
---
|
||||
|
||||
*Версия: 2.1 | Октябрь 2026*
|
||||
*Версия: 2.0 | Август 2026*
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
```
|
||||
Redis упал:
|
||||
Edge: продолжает автономно на локальном кэше блэклиста
|
||||
Manager: Redis-зависимые API отвечают 503 → восстановить Redis
|
||||
Manager: API отдаёт пустые списки / ошибки → рестарт Redis
|
||||
|
||||
Manager упал:
|
||||
Edge: продолжает фильтрацию (фильтрация не зависит от Manager)
|
||||
|
|
@ -20,7 +20,7 @@ Edge нода упала:
|
|||
|
||||
Полный сбой дата-центра:
|
||||
Edge в других ДЦ продолжают работу
|
||||
Общие баны/ноды не синхронизируются до восстановления Redis
|
||||
Баны/ноды не синхронизируются до восстановления Redis
|
||||
```
|
||||
|
||||
---
|
||||
|
|
|
|||
|
|
@ -1,120 +0,0 @@
|
|||
# Rampart перед Minecraft Java
|
||||
|
||||
Rampart can proxy Minecraft Java Edition over TCP. It checks the bounded Java
|
||||
handshake, forwards the original bytes unchanged, and then tunnels the
|
||||
connection to one Paper backend. This is an early integration: it does not
|
||||
replace an upstream DDoS scrubbing network.
|
||||
|
||||
## Build and configure
|
||||
|
||||
Build the edge binary with the Minecraft handler:
|
||||
|
||||
```sh
|
||||
cargo build --release --features protocol-minecraft --bin rampart
|
||||
```
|
||||
|
||||
Use one backend per edge node. For a Paper server on the same host, the sample
|
||||
uses port `25566`; change it to the address reachable over your private network
|
||||
or WireGuard tunnel.
|
||||
|
||||
```toml
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
|
||||
[bind]
|
||||
address = "0.0.0.0"
|
||||
port = 25565
|
||||
|
||||
[backend]
|
||||
upstreams = ["10.20.0.5:25566"]
|
||||
|
||||
[protocol.minecraft]
|
||||
enabled = true
|
||||
max_frame_bytes = 8192
|
||||
timeout_secs = 5
|
||||
send_proxy_protocol = true
|
||||
|
||||
[pow]
|
||||
enabled = false
|
||||
```
|
||||
|
||||
PoW must stay disabled for Minecraft Java: the current PoW exchange is plain
|
||||
text and is not part of the Minecraft protocol. Configure the edge firewall to
|
||||
allow public TCP `25565`, and keep Prometheus `9090` and the Paper backend port
|
||||
private.
|
||||
|
||||
## Preserve player IPs safely
|
||||
|
||||
When `send_proxy_protocol` is enabled, Rampart prepends a PROXY protocol v2 header
|
||||
before the Minecraft handshake. On Paper, enable `proxies.proxy-protocol` in
|
||||
`config/paper-global.yml`. Paper documents this option for HAProxy or similar
|
||||
proxies: [global configuration](https://docs.papermc.io/paper/reference/global-configuration/).
|
||||
|
||||
The PROXY header is not authenticated. Put the edge-to-Paper connection on a
|
||||
private network or WireGuard and firewall Paper's backend port to accept only
|
||||
the trusted edge IPs. Never expose a PROXY-enabled Paper port to arbitrary
|
||||
clients: they could forge the address in the header.
|
||||
|
||||
If Rampart runs in Docker, the container needs access to the configured backend
|
||||
address. The included sample image builds `protocol-minecraft`; the sample
|
||||
config keeps PROXY protocol disabled until Paper and the backend firewall are
|
||||
configured together.
|
||||
|
||||
## DDoS boundary
|
||||
|
||||
Rampart can reject connections and apply local rate limits after packets reach
|
||||
the edge host. It cannot keep a home uplink available when an attack saturates
|
||||
that link. Put a Minecraft-aware DDoS proxy or a provider with game DDoS
|
||||
filtering in front of the VDS, hide the home's public address, and carry
|
||||
accepted traffic to the home server through a restricted tunnel. Two ordinary
|
||||
VDS instances without upstream filtering do not provide that protection.
|
||||
|
||||
## Network layout and firewall
|
||||
|
||||
Use the VDS edge as the only public Minecraft endpoint. The provider should
|
||||
send filtered TCP `25565` traffic to that edge; the edge then reaches Paper over
|
||||
a private link (for example, WireGuard). Use addresses from your own VPN and
|
||||
replace the example roles below with the actual interfaces and addresses.
|
||||
|
||||
| Service | Allowed source | Publicly reachable? |
|
||||
| --- | --- | --- |
|
||||
| VDS edge TCP `25565` | DDoS provider ranges, or the Internet only if the provider requires it | Yes, through the provider |
|
||||
| Paper TCP `25566` | Edge VPN address only | No |
|
||||
| Redis TCP `6379` | Manager and edge VPN addresses only | No |
|
||||
| Manager API TCP `8080` | Admin VPN or explicitly allowed admin IPs | No |
|
||||
| Prometheus TCP `9090` | Monitoring VPN / localhost | No |
|
||||
|
||||
Do not forward Paper, Redis, or management ports from the home router to the
|
||||
Internet. On Proxmox, allow the Paper port only on the tunnel/private interface
|
||||
and restrict it to the edge addresses. If Manager and Redis run on a VDS, bind
|
||||
or firewall Redis to the private interface and let each edge reach it through
|
||||
the VPN. Use the same protected Redis endpoint in Manager and every edge
|
||||
configuration; do not publish Redis with a Docker `ports` mapping. Protect the
|
||||
Manager API and monitoring UI with the admin VPN or firewall as well. A private
|
||||
tunnel limits who can reach these services; it does not add DDoS scrubbing to
|
||||
the public edge.
|
||||
|
||||
For a DDoS provider that sends PROXY protocol v2, enable inbound parsing only
|
||||
for that provider's TCP source ranges:
|
||||
|
||||
```toml
|
||||
[proxy_protocol]
|
||||
ingress_enabled = true
|
||||
# Replace these documentation ranges with the provider's actual ranges.
|
||||
trusted_cidrs = ["198.51.100.0/24", "2001:db8:1234::/48"]
|
||||
ingress_timeout_ms = 750
|
||||
max_header_bytes = 512
|
||||
```
|
||||
|
||||
Rampart rejects connections from unlisted peers when ingress mode is enabled,
|
||||
and it applies per-IP limits and bans using the verified header source address.
|
||||
Keep the allowlist current with the provider's published ranges. The header is
|
||||
not cryptographically authenticated, so never trust arbitrary internet peers.
|
||||
This MVP accepts PROXY v2 TCP4/TCP6 address headers without TLVs; confirm that
|
||||
format with the provider before enabling ingress mode.
|
||||
XDP operates on the network peer address and is not used to ban the claimed
|
||||
player address on this path.
|
||||
|
||||
The Minecraft handler supports status and login handshake states and forwards
|
||||
the connection as a TCP tunnel. It does not inspect encrypted login/game
|
||||
packets or implement Velocity forwarding. Bedrock Edition uses UDP and is not
|
||||
supported by this handler.
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
curl -s http://EDGE_IP:9090/metrics | grep -E "rampart_(connections|rate_limit|attack_status)"
|
||||
|
||||
# rampart_attack_status: 0 = normal, 1 = suspicious, 2 = under_attack
|
||||
# (детектор по порогу pps, см. src/traffic/intel/detector.rs)
|
||||
# (детектор по порогу pps, см. src/traffic/detector.rs)
|
||||
|
||||
# ── ШАГ 2: Определить тип атаки ──
|
||||
|
||||
|
|
@ -39,11 +39,19 @@ rampart-cli blacklist list
|
|||
# C) Доверенные IP (мониторинг, админы) → whitelist в config.toml
|
||||
# (только IP-адреса), затем systemctl restart rampart
|
||||
|
||||
# D) Для Minecraft Java PoW не включай: он несовместим с протоколом.
|
||||
# Проверь фильтрацию трафика у upstream game-proxy и его лимиты.
|
||||
# D) Повысить стоимость флуда → включить PoW:
|
||||
# [pow]
|
||||
# enabled = true
|
||||
# difficulty = 5
|
||||
systemctl restart rampart
|
||||
|
||||
# ── ШАГ 4: Если не помогает ──
|
||||
|
||||
# Emergency mode (через CLI):
|
||||
rampart-cli emergency enable
|
||||
# ... и обратно после атаки:
|
||||
rampart-cli emergency disable
|
||||
|
||||
# В крайнем случае — закрыть порт на firewall и разбираться:
|
||||
iptables -A INPUT -p tcp --dport 25565 -j DROP
|
||||
|
||||
|
|
@ -58,8 +66,6 @@ clickhouse-client --query "SELECT count() FROM rampart.events WHERE event_type='
|
|||
> Честно: hot-reload конфига не реализован — изменения применяются рестартом.
|
||||
> ASN/CIDR-баны, GeoIP-блокировки стран и выбор типов challenge через CLI не
|
||||
> реализованы; blacklist принимает цели в том виде, в котором их хранит Manager.
|
||||
> Команды `emergency`, `drain` и `config set` пока не реализованы и завершатся
|
||||
> явной ошибкой вместо изменения состояния.
|
||||
|
||||
## 2. Edge нода не стартует
|
||||
|
||||
|
|
@ -78,9 +84,8 @@ cargo test --test config_parse
|
|||
RAMPART_CONFIG=/etc/rampart/config.toml /usr/local/bin/rampart
|
||||
|
||||
# C) Нет ни одного protocol handler
|
||||
# Для Minecraft Java пересобери edge с --features protocol-minecraft и проверь
|
||||
# секцию [protocol.minecraft]. Для HTTP используй --features protocol-http.
|
||||
# Если активной protocol feature нет, edge завершится с ошибкой при запуске.
|
||||
# rampart требует зарегистрированный ProtocolHandler (feature protocol-http
|
||||
# или внешний крейт). Пока плагинов нет — edge в исследовательском режиме.
|
||||
|
||||
# D) XDP не загрузился
|
||||
journalctl -u rampart | grep -i xdp
|
||||
|
|
@ -137,9 +142,13 @@ clickhouse-client --query "ALTER TABLE rampart.events MODIFY TTL ts + INTERVAL 9
|
|||
```bash
|
||||
rampart-cli status # Общее состояние системы
|
||||
rampart-cli doctor # Полная диагностика
|
||||
rampart-cli config get <key> # Получить параметр
|
||||
rampart-cli config set <key> <value> # Установить параметр
|
||||
rampart-cli blacklist add <target> [reason]
|
||||
rampart-cli blacklist remove <target>
|
||||
rampart-cli blacklist list
|
||||
rampart-cli emergency enable|disable # Emergency mode
|
||||
rampart-cli drain <node> # Плавно вывести ноду
|
||||
```
|
||||
|
||||
Полный список: `rampart-cli --help` (источник — src/bin/rampart-cli.rs).
|
||||
|
|
|
|||
|
|
@ -44,11 +44,9 @@ perf top -p $(pgrep rampart)
|
|||
|
||||
### "Edge не стартует: no protocol plugins compiled"
|
||||
|
||||
Реестр протоколов пуст: `rampart` требует хотя бы один ProtocolHandler.
|
||||
Для Minecraft Java собери edge с `--features protocol-minecraft` и включи
|
||||
`[protocol.minecraft]` в конфиге; для HTTP используй `--features protocol-http`.
|
||||
Без зарегистрированного обработчика edge завершится с ошибкой при запуске.
|
||||
Подробная инструкция: [Minecraft Java](minecraft-java.md).
|
||||
Реестр протоколов пуст: `rampart` требует хотя бы один ProtocolHandler
|
||||
(feature `protocol-http` или внешний крейт-плагин). Пока плагинов нет,
|
||||
edge запускается только в исследовательских целях. См. src/bin/rampart.rs.
|
||||
|
||||
---
|
||||
|
||||
|
|
|
|||
|
|
@ -93,19 +93,6 @@ pub async fn run() -> anyhow::Result<RunExit> {
|
|||
return;
|
||||
},
|
||||
};
|
||||
let local_events = bl.subscribe_local_bans();
|
||||
let publisher_client = client.clone();
|
||||
let publisher_blacklist = bl.clone();
|
||||
let publisher_shutdown = sd.clone();
|
||||
tokio::spawn(async move {
|
||||
crate::store::start_local_ban_publisher(
|
||||
publisher_client,
|
||||
publisher_blacklist,
|
||||
local_events,
|
||||
publisher_shutdown,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
crate::store::start_blacklist_sync(&client, bl, sd).await;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,15 +19,6 @@ use tokio::sync::watch;
|
|||
/// HTTP/1.1-обработчик с политикой из секции `[protocol.http]`.
|
||||
/// Внешние крейты регистрируют свои обработчики в этом же месте.
|
||||
pub(crate) fn build_registry(config: &Config) -> ProtocolRegistry {
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
if config.protocol.minecraft.enabled {
|
||||
let mut registry = ProtocolRegistry::new();
|
||||
registry.register(Box::new(crate::protocol::minecraft::MinecraftProtocolHandler::new(
|
||||
&config.protocol.minecraft,
|
||||
&config.backend.upstreams,
|
||||
)));
|
||||
return registry;
|
||||
}
|
||||
#[cfg(feature = "protocol-http")]
|
||||
{
|
||||
let mut registry = ProtocolRegistry::new();
|
||||
|
|
@ -69,17 +60,10 @@ pub(crate) fn start_xdp(
|
|||
let mut guard = shared.lock().expect("xdp lock poisoned");
|
||||
let globals = XdpGlobals::from_config(&config.xdp)?;
|
||||
guard.set_globals(globals);
|
||||
// With ingress PROXY protocol, XDP sees only the provider's transport
|
||||
// address, so its events must not trigger per-player or provider bans.
|
||||
if !config.proxy_protocol.ingress_enabled {
|
||||
// load() captures the blacklist and auto-ban policy in its ringbuf
|
||||
// callback, so set_autoban must happen first.
|
||||
guard.set_autoban(blacklist.clone(), &config.xdp);
|
||||
} else if config.xdp.auto_ban_enabled {
|
||||
tracing::warn!(
|
||||
"XDP auto-ban is disabled while inbound PROXY protocol is enabled; XDP only sees transport peers"
|
||||
);
|
||||
}
|
||||
// Порядок load-bearing: `load()` захватывает blacklist и политику
|
||||
// авто-бана в ringbuf-колбэк (XdpFilter::load -> build_ringbuf),
|
||||
// поэтому set_autoban обязан отработать ДО load(), а не после.
|
||||
guard.set_autoban(blacklist.clone(), &config.xdp);
|
||||
guard.load()?;
|
||||
}
|
||||
let xdp_metrics = XdpMetrics::register()?;
|
||||
|
|
|
|||
|
|
@ -69,12 +69,10 @@ async fn main() -> anyhow::Result<()> {
|
|||
.route("/api/v1/servers", get(api::inventory::servers::list_servers))
|
||||
.route(
|
||||
"/api/v1/blacklist",
|
||||
get(api::blacklist::list_blacklist)
|
||||
.post(api::blacklist::add_blacklist)
|
||||
.delete(api::blacklist::remove_blacklist),
|
||||
get(api::blacklist::list_blacklist).post(api::blacklist::add_blacklist),
|
||||
)
|
||||
.route("/api/v1/nodes", get(api::inventory::nodes::list_nodes));
|
||||
let protected = protected.route_layer(middleware::from_fn_with_state(state.clone(), auth::auth_middleware));
|
||||
let protected = protected.route_layer(middleware::from_fn(auth::auth_middleware));
|
||||
|
||||
let cors = match std::env::var("CORS_ORIGIN") {
|
||||
Ok(origin) if origin.is_empty() || origin == "*" => CorsLayer::new().allow_origin(tower_http::cors::Any),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AddResponse {
|
||||
|
|
@ -19,33 +18,8 @@ struct BlacklistResponse {
|
|||
total: usize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LoginResponse {
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct RemoveRequest<'a> {
|
||||
target: &'a str,
|
||||
}
|
||||
|
||||
async fn authenticated_client() -> anyhow::Result<(reqwest::Client, String, String)> {
|
||||
let manager_url = std::env::var("RAMPART_MANAGER").unwrap_or_else(|_| "http://localhost:8080".to_string());
|
||||
let password = std::env::var("RAMPART_API_PASSWORD")
|
||||
.map_err(|_| anyhow::anyhow!("RAMPART_API_PASSWORD must be set to use the manager blacklist API"))?;
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.post(format!("{manager_url}/api/v1/auth/login"))
|
||||
.json(&serde_json::json!({"password": password}))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
let token = response.json::<LoginResponse>().await?.token;
|
||||
Ok((client, manager_url, token))
|
||||
}
|
||||
|
||||
pub async fn add(target: String, reason: Option<String>) -> anyhow::Result<()> {
|
||||
let (client, manager_url, token) = authenticated_client().await?;
|
||||
let manager_url = std::env::var("RAMPART_MANAGER").unwrap_or_else(|_| "http://localhost:8080".to_string());
|
||||
|
||||
let body = serde_json::json!({
|
||||
"target": target,
|
||||
|
|
@ -53,47 +27,43 @@ pub async fn add(target: String, reason: Option<String>) -> anyhow::Result<()> {
|
|||
"reason": reason.unwrap_or_else(|| "manual".to_string()),
|
||||
});
|
||||
|
||||
let resp = client
|
||||
let client = reqwest::Client::new();
|
||||
match client
|
||||
.post(format!("{manager_url}/api/v1/blacklist"))
|
||||
.bearer_auth(token)
|
||||
.json(&body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<AddResponse>()
|
||||
.await?;
|
||||
println!("[OK] {}: {}", resp.status, resp.target);
|
||||
.await
|
||||
{
|
||||
Ok(resp) => {
|
||||
if let Ok(add_resp) = resp.json::<AddResponse>().await {
|
||||
println!("[OK] {}: {}", add_resp.status, add_resp.target);
|
||||
}
|
||||
},
|
||||
Err(e) => println!("[FAIL] {e}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove(target: String) -> anyhow::Result<()> {
|
||||
let (client, manager_url, token) = authenticated_client().await?;
|
||||
let response = client
|
||||
.delete(format!("{manager_url}/api/v1/blacklist"))
|
||||
.bearer_auth(token)
|
||||
.json(&RemoveRequest { target: &target })
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
let result: serde_json::Value = response.json().await?;
|
||||
println!("[OK] {}: {}", result["status"].as_str().unwrap_or("removed"), target);
|
||||
println!("Removing {target} from blacklist...");
|
||||
println!("(not implemented in v0.1)");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn list() -> anyhow::Result<()> {
|
||||
let (client, manager_url, token) = authenticated_client().await?;
|
||||
let list = client
|
||||
.get(format!("{manager_url}/api/v1/blacklist"))
|
||||
.bearer_auth(token)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<BlacklistResponse>()
|
||||
.await?;
|
||||
println!("Blacklist ({} entries)", list.total);
|
||||
println!("----------------------");
|
||||
for item in &list.items {
|
||||
println!(" {} ({})", item.target, item.reason);
|
||||
let manager_url = std::env::var("RAMPART_MANAGER").unwrap_or_else(|_| "http://localhost:8080".to_string());
|
||||
|
||||
match reqwest::get(format!("{manager_url}/api/v1/blacklist")).await {
|
||||
Ok(resp) => {
|
||||
if let Ok(list) = resp.json::<BlacklistResponse>().await {
|
||||
println!("Blacklist ({} entries)", list.total);
|
||||
println!("----------------------");
|
||||
for item in &list.items {
|
||||
println!(" {} ({})", item.target, item.reason);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => println!("[FAIL] {e}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
pub async fn run(key: Option<String>, value: Option<String>) -> anyhow::Result<()> {
|
||||
match (key, value) {
|
||||
(Some(_), Some(_)) => {
|
||||
anyhow::bail!("config updates are not implemented; edit the edge config and restart Rampart")
|
||||
(Some(k), Some(v)) => {
|
||||
println!("Setting {k} = {v}");
|
||||
Ok(())
|
||||
},
|
||||
(Some(k), None) => {
|
||||
println!("Reading config key: {k}");
|
||||
println!("(not implemented in v0.1)");
|
||||
Ok(())
|
||||
},
|
||||
(Some(_), None) => anyhow::bail!("config lookup is not implemented"),
|
||||
(None, Some(_)) | (None, None) => {
|
||||
println!("Configuration");
|
||||
println!("=============\n");
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
pub async fn run(node: &str) -> anyhow::Result<()> {
|
||||
anyhow::bail!("graceful drain is not implemented for node {node}")
|
||||
println!("Draining node: {node}");
|
||||
println!("Waiting for active connections to drain...");
|
||||
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||
println!("Node {node} drained successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
pub async fn enable() -> anyhow::Result<()> {
|
||||
anyhow::bail!("emergency mode is not implemented; change the edge policy and restart Rampart")
|
||||
println!("Emergency mode ENABLED");
|
||||
println!("Only whitelisted IPs will be allowed through.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn disable() -> anyhow::Result<()> {
|
||||
anyhow::bail!("emergency mode is not implemented; change the edge policy and restart Rampart")
|
||||
println!("Emergency mode DISABLED");
|
||||
println!("Normal filtering resumed.");
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,12 @@
|
|||
//! Единый конфиг платформы Rampart.
|
||||
mod sections;
|
||||
|
||||
#[cfg(feature = "protocol-http")]
|
||||
pub use sections::HttpProtocolConfig;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
pub use sections::MinecraftProtocolConfig;
|
||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||
pub use sections::ProtocolConfig;
|
||||
pub use sections::{
|
||||
BackendConfig, BanConfig, BindConfig, DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig,
|
||||
LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, ProxyProtocolConfig, StoreConfig, TrustedProxyCidr,
|
||||
WorkerConfig, XdpConfig,
|
||||
LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, StoreConfig, WorkerConfig, XdpConfig,
|
||||
};
|
||||
#[cfg(feature = "protocol-http")]
|
||||
pub use sections::{HttpProtocolConfig, ProtocolConfig};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
|
|
@ -27,8 +22,6 @@ pub struct Config {
|
|||
#[serde(default)]
|
||||
pub limits: LimitsConfig,
|
||||
#[serde(default)]
|
||||
pub proxy_protocol: ProxyProtocolConfig,
|
||||
#[serde(default)]
|
||||
pub ban: BanConfig,
|
||||
#[serde(default)]
|
||||
pub store: StoreConfig,
|
||||
|
|
@ -42,7 +35,7 @@ pub struct Config {
|
|||
pub pow: PowConfig,
|
||||
#[serde(default)]
|
||||
pub detect: DetectConfig,
|
||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||
#[cfg(feature = "protocol-http")]
|
||||
#[serde(default)]
|
||||
pub protocol: ProtocolConfig,
|
||||
#[serde(default)]
|
||||
|
|
@ -87,26 +80,6 @@ impl Config {
|
|||
if self.detect.prefix.enabled && self.detect.prefix.syn_threshold == 0 {
|
||||
anyhow::bail!("detect.prefix.syn_threshold must be positive when enabled");
|
||||
}
|
||||
if self.limits.max_connections_per_ip == 0 {
|
||||
anyhow::bail!("limits.max_connections_per_ip must be positive");
|
||||
}
|
||||
if self.proxy_protocol.ingress_enabled {
|
||||
if self.proxy_protocol.trusted_cidrs.is_empty() {
|
||||
anyhow::bail!("proxy_protocol.ingress_enabled requires at least one trusted_cidrs entry");
|
||||
}
|
||||
if self.proxy_protocol.ingress_timeout_ms == 0 || self.proxy_protocol.ingress_timeout_ms > 5_000 {
|
||||
anyhow::bail!("proxy_protocol.ingress_timeout_ms must be between 1 and 5000");
|
||||
}
|
||||
if self.proxy_protocol.max_header_bytes < 52 || self.proxy_protocol.max_header_bytes > 4096 {
|
||||
anyhow::bail!("proxy_protocol.max_header_bytes must be between 52 and 4096");
|
||||
}
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
if !self.protocol.minecraft.enabled {
|
||||
anyhow::bail!("proxy_protocol.ingress_enabled requires protocol.minecraft.enabled");
|
||||
}
|
||||
#[cfg(not(feature = "protocol-minecraft"))]
|
||||
anyhow::bail!("proxy_protocol.ingress_enabled requires the protocol-minecraft feature");
|
||||
}
|
||||
#[cfg(feature = "protocol-http")]
|
||||
{
|
||||
if self.protocol.http.max_header_bytes == 0 {
|
||||
|
|
@ -116,21 +89,6 @@ impl Config {
|
|||
anyhow::bail!("protocol.http.timeout_secs must be positive");
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
if self.protocol.minecraft.enabled {
|
||||
if self.pow.enabled {
|
||||
anyhow::bail!("pow.enabled is incompatible with protocol.minecraft");
|
||||
}
|
||||
if self.backend.upstreams.len() != 1 {
|
||||
anyhow::bail!("protocol.minecraft requires exactly one backend.upstreams entry");
|
||||
}
|
||||
if self.protocol.minecraft.max_frame_bytes == 0 {
|
||||
anyhow::bail!("protocol.minecraft.max_frame_bytes must be positive");
|
||||
}
|
||||
if self.protocol.minecraft.timeout_secs == 0 {
|
||||
anyhow::bail!("protocol.minecraft.timeout_secs must be positive");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,55 +78,11 @@ fn default_prefix_min_unique_sources() -> u64 {
|
|||
}
|
||||
|
||||
/// Секция `[protocol]`: протокольные обработчики (plugin-by-feature).
|
||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||
#[cfg(feature = "protocol-http")]
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
pub struct ProtocolConfig {
|
||||
#[cfg(feature = "protocol-http")]
|
||||
#[serde(default)]
|
||||
pub http: HttpProtocolConfig,
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
#[serde(default)]
|
||||
pub minecraft: MinecraftProtocolConfig,
|
||||
}
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct MinecraftProtocolConfig {
|
||||
#[serde(default = "default_minecraft_enabled")]
|
||||
pub enabled: bool,
|
||||
#[serde(default = "default_minecraft_max_frame_bytes")]
|
||||
pub max_frame_bytes: usize,
|
||||
#[serde(default = "default_minecraft_timeout_secs")]
|
||||
pub timeout_secs: u64,
|
||||
#[serde(default, alias = "proxy_protocol")]
|
||||
pub send_proxy_protocol: bool,
|
||||
}
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
impl Default for MinecraftProtocolConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: default_minecraft_enabled(),
|
||||
max_frame_bytes: default_minecraft_max_frame_bytes(),
|
||||
timeout_secs: default_minecraft_timeout_secs(),
|
||||
send_proxy_protocol: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
fn default_minecraft_enabled() -> bool {
|
||||
!cfg!(feature = "protocol-http")
|
||||
}
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
fn default_minecraft_max_frame_bytes() -> usize {
|
||||
8192
|
||||
}
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
fn default_minecraft_timeout_secs() -> u64 {
|
||||
5
|
||||
}
|
||||
|
||||
/// Секция `[protocol.http]`: политика HTTP/1.1-обработчика на edge-ноде.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
use serde::{Deserialize, Deserializer};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use std::str::FromStr;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct BindConfig {
|
||||
|
|
@ -135,110 +133,3 @@ impl Default for PowConfig {
|
|||
fn default_pow_difficulty() -> u8 {
|
||||
4
|
||||
}
|
||||
|
||||
/// CIDR network allowed to send inbound PROXY protocol headers.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TrustedProxyCidr {
|
||||
network: IpAddr,
|
||||
prefix_len: u8,
|
||||
}
|
||||
|
||||
impl TrustedProxyCidr {
|
||||
#[must_use]
|
||||
pub fn contains(&self, ip: IpAddr) -> bool {
|
||||
match (self.network, ip) {
|
||||
(IpAddr::V4(network), IpAddr::V4(ip)) => {
|
||||
let mask = if self.prefix_len == 0 {
|
||||
0
|
||||
} else {
|
||||
u32::MAX << (32 - self.prefix_len)
|
||||
};
|
||||
u32::from(network) & mask == u32::from(ip) & mask
|
||||
},
|
||||
(IpAddr::V6(network), IpAddr::V6(ip)) => {
|
||||
let mask = if self.prefix_len == 0 {
|
||||
0
|
||||
} else {
|
||||
u128::MAX << (128 - self.prefix_len)
|
||||
};
|
||||
u128::from(network) & mask == u128::from(ip) & mask
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for TrustedProxyCidr {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
let (address, prefix) = match value.split_once('/') {
|
||||
Some((address, prefix)) => (address, Some(prefix)),
|
||||
None => (value, None),
|
||||
};
|
||||
let network = IpAddr::from_str(address).map_err(|_| format!("invalid trusted proxy CIDR: {value}"))?;
|
||||
let max_prefix = if network.is_ipv4() { 32 } else { 128 };
|
||||
let prefix_len = match prefix {
|
||||
Some(prefix) => prefix
|
||||
.parse::<u8>()
|
||||
.map_err(|_| format!("invalid trusted proxy CIDR prefix: {value}"))?,
|
||||
None => max_prefix,
|
||||
};
|
||||
if prefix_len > max_prefix {
|
||||
return Err(format!("trusted proxy CIDR prefix is too large: {value}"));
|
||||
}
|
||||
let network = match network {
|
||||
IpAddr::V4(ip) => IpAddr::V4(Ipv4Addr::from(u32::from(ip) & mask_v4(prefix_len))),
|
||||
IpAddr::V6(ip) => IpAddr::V6(Ipv6Addr::from(u128::from(ip) & mask_v6(prefix_len))),
|
||||
};
|
||||
Ok(Self { network, prefix_len })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for TrustedProxyCidr {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = String::deserialize(deserializer)?;
|
||||
value.parse().map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
fn mask_v4(prefix: u8) -> u32 {
|
||||
if prefix == 0 { 0 } else { u32::MAX << (32 - prefix) }
|
||||
}
|
||||
|
||||
fn mask_v6(prefix: u8) -> u128 {
|
||||
if prefix == 0 { 0 } else { u128::MAX << (128 - prefix) }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ProxyProtocolConfig {
|
||||
#[serde(default)]
|
||||
pub ingress_enabled: bool,
|
||||
#[serde(default)]
|
||||
pub trusted_cidrs: Vec<TrustedProxyCidr>,
|
||||
#[serde(default = "default_proxy_protocol_timeout_ms")]
|
||||
pub ingress_timeout_ms: u64,
|
||||
#[serde(default = "default_proxy_protocol_max_header_bytes")]
|
||||
pub max_header_bytes: usize,
|
||||
}
|
||||
|
||||
impl Default for ProxyProtocolConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ingress_enabled: false,
|
||||
trusted_cidrs: Vec::new(),
|
||||
ingress_timeout_ms: default_proxy_protocol_timeout_ms(),
|
||||
max_header_bytes: default_proxy_protocol_max_header_bytes(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn default_proxy_protocol_timeout_ms() -> u64 {
|
||||
750
|
||||
}
|
||||
fn default_proxy_protocol_max_header_bytes() -> usize {
|
||||
512
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,14 +4,8 @@ mod detect;
|
|||
mod edge;
|
||||
mod platform;
|
||||
|
||||
#[cfg(feature = "protocol-http")]
|
||||
pub use detect::HttpProtocolConfig;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
pub use detect::MinecraftProtocolConfig;
|
||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||
pub use detect::ProtocolConfig;
|
||||
pub use detect::{DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig};
|
||||
pub use edge::{
|
||||
BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, TrustedProxyCidr, WorkerConfig,
|
||||
};
|
||||
#[cfg(feature = "protocol-http")]
|
||||
pub use detect::{HttpProtocolConfig, ProtocolConfig};
|
||||
pub use edge::{BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, WorkerConfig};
|
||||
pub use platform::{LoggingConfig, MetricsConfig, StoreConfig, XdpConfig};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use rand::Rng;
|
||||
use rand::RngCore;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::net::IpAddr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
|
@ -22,7 +22,7 @@ impl Challenge {
|
|||
#[must_use]
|
||||
pub fn generate(difficulty: u8) -> Self {
|
||||
let mut token = [0u8; 32];
|
||||
rand::rng().fill_bytes(&mut token);
|
||||
rand::thread_rng().fill_bytes(&mut token);
|
||||
Self {
|
||||
token,
|
||||
created_at: Instant::now(),
|
||||
|
|
|
|||
|
|
@ -77,13 +77,10 @@ async fn accept_loop(
|
|||
tokio::spawn(async move {
|
||||
// Hot path Traffic Intelligence: только атомики и DashMap.
|
||||
hook.on_accept();
|
||||
let outcome = gateway.handle(stream, peer_addr).await;
|
||||
if let Err(e) = outcome.result {
|
||||
if let Err(e) = gateway.handle(stream, peer_addr).await {
|
||||
tracing::debug!("connection from {peer_addr}: {e}");
|
||||
}
|
||||
if let Some(client_ip) = outcome.client_ip {
|
||||
hook.on_connection_end(client_ip);
|
||||
}
|
||||
hook.on_connection_end(peer_addr.ip());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,28 @@
|
|||
//! Generic TCP proxy: фильтры входа → protocol handshake → upstream.
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::engine::challenge::DifficultyAdjuster;
|
||||
use crate::engine::challenge::{DifficultyAdjuster, enforce as enforce_pow};
|
||||
use crate::engine::subnet_tracker::SubnetTracker;
|
||||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::filter::connection_limit::ConnectionLimiter;
|
||||
use crate::filter::rate_limit::RateLimiter;
|
||||
use crate::metrics;
|
||||
use crate::protocol::ProtocolRegistry;
|
||||
use crate::store::clickhouse::ClickHouseWriter;
|
||||
use crate::store::clickhouse::{ClickHouseEvent, ClickHouseWriter};
|
||||
use crate::traffic::reputation::IpReputation;
|
||||
use crate::xdp::XdpFilter;
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize};
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
mod gateway;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
mod proxy;
|
||||
|
||||
/// Общее состояние edge-движка, разделяемое между accept-loop'ами.
|
||||
pub struct Gateway {
|
||||
pub config: Arc<Config>,
|
||||
pub rate_limiter: Arc<RateLimiter>,
|
||||
pub connection_limiter: ConnectionLimiter,
|
||||
pub blacklist: Arc<Blacklist>,
|
||||
pub adjuster: Arc<Mutex<DifficultyAdjuster>>,
|
||||
pub whitelist: Arc<HashSet<IpAddr>>,
|
||||
|
|
@ -37,9 +35,166 @@ pub struct Gateway {
|
|||
upstream_cursor: AtomicUsize,
|
||||
}
|
||||
|
||||
/// Result of a connection attempt, retaining the verified client identity for
|
||||
/// post-connection traffic accounting.
|
||||
pub struct GatewayOutcome {
|
||||
pub client_ip: Option<IpAddr>,
|
||||
pub result: anyhow::Result<()>,
|
||||
impl Gateway {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
config: Arc<Config>,
|
||||
rate_limiter: Arc<RateLimiter>,
|
||||
blacklist: Arc<Blacklist>,
|
||||
adjuster: Arc<Mutex<DifficultyAdjuster>>,
|
||||
whitelist: Arc<HashSet<IpAddr>>,
|
||||
reputation: Arc<IpReputation>,
|
||||
xdp: Option<Arc<Mutex<XdpFilter>>>,
|
||||
clickhouse: Option<Arc<TokioMutex<ClickHouseWriter>>>,
|
||||
allowed_1s: Arc<AtomicU64>,
|
||||
registry: Arc<ProtocolRegistry>,
|
||||
subnet_tracker: Option<Arc<SubnetTracker>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
config,
|
||||
rate_limiter,
|
||||
blacklist,
|
||||
adjuster,
|
||||
whitelist,
|
||||
reputation,
|
||||
xdp,
|
||||
clickhouse,
|
||||
allowed_1s,
|
||||
registry,
|
||||
subnet_tracker,
|
||||
upstream_cursor: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Полный конвейер обработки входящего соединения.
|
||||
///
|
||||
/// # Errors
|
||||
/// Возвращает ошибку только при сбое ввода-вывода апстрима; блокировки
|
||||
/// клиента ошибкой не считаются.
|
||||
pub async fn handle(&self, mut client: TcpStream, peer_addr: std::net::SocketAddr) -> anyhow::Result<()> {
|
||||
let peer_ip = peer_addr.ip();
|
||||
|
||||
if let Some(tracker) = &self.subnet_tracker {
|
||||
tracker.record_connection(peer_ip);
|
||||
}
|
||||
|
||||
if self.blacklist.is_blocked(peer_ip) {
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !self.pass_pow_gate(&mut client, peer_ip).await? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !self.rate_limiter.check(peer_ip) {
|
||||
self.block_rate_limit(peer_ip).await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let handler = self.registry.primary()?;
|
||||
let upstream = match handler.handshake(&mut client).await {
|
||||
Ok(upstream) => upstream,
|
||||
Err(e) => {
|
||||
tracing::debug!("handshake rejected from {peer_addr}: {e}");
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
|
||||
if !self.rate_limiter.check(peer_ip) {
|
||||
self.block_rate_limit(peer_ip).await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["allowed"]).inc();
|
||||
self.allowed_1s.fetch_add(1, Ordering::Relaxed);
|
||||
self.reputation.record_good(peer_ip);
|
||||
|
||||
let mut backend = TcpStream::connect(self.next_upstream(&upstream.addr)).await?;
|
||||
tokio::io::copy_bidirectional(&mut client, &mut backend).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn pass_pow_gate(&self, client: &mut TcpStream, peer_ip: IpAddr) -> anyhow::Result<bool> {
|
||||
let pow_config = &self.config.pow;
|
||||
if !(pow_config.enabled && pow_config.difficulty > 0) {
|
||||
return Ok(true);
|
||||
}
|
||||
if self.whitelist.contains(&peer_ip) {
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["skipped"]).inc();
|
||||
metrics::POW_CURRENT_DIFFICULTY.set(pow_config.difficulty as i64);
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let diff = {
|
||||
let mut adjuster = self.adjuster.lock().expect("adjuster lock poisoned");
|
||||
adjuster.record_connection();
|
||||
adjuster.current_difficulty()
|
||||
};
|
||||
let result = enforce_pow(client, peer_ip, diff).await?;
|
||||
if !result {
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["failed"]).inc();
|
||||
tracing::debug!("pow: failed for {peer_ip}, dropping connection");
|
||||
return Ok(false);
|
||||
}
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["passed"]).inc();
|
||||
metrics::POW_CURRENT_DIFFICULTY.set(diff as i64);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn next_upstream<'a>(&'a self, negotiated: &'a str) -> &'a str {
|
||||
let upstreams = &self.config.backend.upstreams;
|
||||
if upstreams.len() > 1 {
|
||||
let idx = self.upstream_cursor.fetch_add(1, Ordering::Relaxed) % upstreams.len();
|
||||
return upstreams[idx].as_str();
|
||||
}
|
||||
if let Some(first) = upstreams.first() {
|
||||
return first.as_str();
|
||||
}
|
||||
negotiated
|
||||
}
|
||||
|
||||
async fn block_rate_limit(&self, ip: IpAddr) {
|
||||
metrics::RATE_LIMIT_HITS.with_label_values(&["hit"]).inc();
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
self.reputation.record_bad(ip);
|
||||
if self.reputation.score(ip) < -40 {
|
||||
let duration_secs = Duration::from_secs(self.config.ban.ban_duration_secs);
|
||||
self.blacklist.add(ip, duration_secs, "low_reputation");
|
||||
self.xdp_ban(ip, duration_secs.as_secs());
|
||||
self.push_event("block", ip, "low_reputation").await;
|
||||
tracing::info!("low reputation ban {ip}: rate-limit abuse");
|
||||
}
|
||||
}
|
||||
|
||||
fn xdp_ban(&self, ip: IpAddr, duration_secs: u64) {
|
||||
let Some(xdp) = &self.xdp else {
|
||||
return;
|
||||
};
|
||||
let IpAddr::V4(ip_v4) = ip else {
|
||||
return;
|
||||
};
|
||||
match xdp.lock().expect("xdp lock poisoned").ban_ip(ip_v4, duration_secs) {
|
||||
Ok(()) => tracing::debug!("xdp ban {ip_v4} for {duration_secs}s"),
|
||||
Err(e) => tracing::warn!("xdp ban failed for {ip_v4}: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn push_event(&self, event_type: &str, ip: IpAddr, data_string: &str) {
|
||||
let Some(writer) = &self.clickhouse else {
|
||||
return;
|
||||
};
|
||||
let event = ClickHouseEvent {
|
||||
timestamp: chrono::Utc::now(),
|
||||
event_type: event_type.to_string(),
|
||||
ip: ip.to_string(),
|
||||
data_float: 0.0,
|
||||
data_int: 0,
|
||||
data_string: data_string.to_string(),
|
||||
};
|
||||
if let Err(e) = writer.lock().await.push(event).await {
|
||||
tracing::debug!("clickhouse push error: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,230 +0,0 @@
|
|||
mod challenge;
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
use super::proxy;
|
||||
use super::{Gateway, GatewayOutcome};
|
||||
use crate::config::Config;
|
||||
use crate::engine::challenge::DifficultyAdjuster;
|
||||
use crate::engine::subnet::tracker::SubnetTracker;
|
||||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::filter::connection_limit::ConnectionLimiter;
|
||||
use crate::filter::rate_limit::RateLimiter;
|
||||
use crate::metrics;
|
||||
use crate::protocol::ProtocolRegistry;
|
||||
use crate::store::clickhouse::{ClickHouseEvent, ClickHouseWriter};
|
||||
use crate::traffic::reputation::IpReputation;
|
||||
use crate::xdp::XdpFilter;
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
|
||||
impl Gateway {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
config: Arc<Config>,
|
||||
rate_limiter: Arc<RateLimiter>,
|
||||
blacklist: Arc<Blacklist>,
|
||||
adjuster: Arc<Mutex<DifficultyAdjuster>>,
|
||||
whitelist: Arc<HashSet<IpAddr>>,
|
||||
reputation: Arc<IpReputation>,
|
||||
xdp: Option<Arc<Mutex<XdpFilter>>>,
|
||||
clickhouse: Option<Arc<TokioMutex<ClickHouseWriter>>>,
|
||||
allowed_1s: Arc<AtomicU64>,
|
||||
registry: Arc<ProtocolRegistry>,
|
||||
subnet_tracker: Option<Arc<SubnetTracker>>,
|
||||
) -> Self {
|
||||
let max_connections = config.limits.max_connections_per_ip as usize;
|
||||
Self {
|
||||
config,
|
||||
rate_limiter,
|
||||
connection_limiter: ConnectionLimiter::new(max_connections),
|
||||
blacklist,
|
||||
adjuster,
|
||||
whitelist,
|
||||
reputation,
|
||||
xdp,
|
||||
clickhouse,
|
||||
allowed_1s,
|
||||
registry,
|
||||
subnet_tracker,
|
||||
upstream_cursor: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Полный конвейер обработки входящего соединения.
|
||||
///
|
||||
/// # Errors
|
||||
/// Возвращает ошибку только при сбое ввода-вывода апстрима; блокировки
|
||||
/// клиента ошибкой не считаются.
|
||||
#[allow(unused_mut)]
|
||||
pub async fn handle(&self, mut client: TcpStream, peer_addr: std::net::SocketAddr) -> GatewayOutcome {
|
||||
let client_addr = if self.config.proxy_protocol.ingress_enabled {
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
{
|
||||
if !self
|
||||
.config
|
||||
.proxy_protocol
|
||||
.trusted_cidrs
|
||||
.iter()
|
||||
.any(|cidr| cidr.contains(peer_addr.ip()))
|
||||
{
|
||||
return GatewayOutcome {
|
||||
client_ip: None,
|
||||
result: Err(anyhow::anyhow!("untrusted peer attempted inbound PROXY protocol")),
|
||||
};
|
||||
}
|
||||
match proxy::read_v2_header(
|
||||
&mut client,
|
||||
Duration::from_millis(self.config.proxy_protocol.ingress_timeout_ms),
|
||||
self.config.proxy_protocol.max_header_bytes,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(client_addr) => client_addr,
|
||||
Err(error) => {
|
||||
return GatewayOutcome {
|
||||
client_ip: None,
|
||||
result: Err(error),
|
||||
};
|
||||
},
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "protocol-minecraft"))]
|
||||
{
|
||||
return GatewayOutcome {
|
||||
client_ip: None,
|
||||
result: Err(anyhow::anyhow!("inbound PROXY protocol requires protocol-minecraft")),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
peer_addr
|
||||
};
|
||||
let client_ip = client_addr.ip();
|
||||
let result = self.handle_client(client, peer_addr, client_addr).await;
|
||||
GatewayOutcome {
|
||||
client_ip: Some(client_ip),
|
||||
result,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_client(
|
||||
&self,
|
||||
mut client: TcpStream,
|
||||
peer_addr: std::net::SocketAddr,
|
||||
client_addr: std::net::SocketAddr,
|
||||
) -> anyhow::Result<()> {
|
||||
let peer_ip = client_addr.ip();
|
||||
|
||||
if let Some(tracker) = &self.subnet_tracker {
|
||||
tracker.record_connection(peer_ip);
|
||||
}
|
||||
|
||||
if self.blacklist.is_blocked(peer_ip) {
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let Some(_connection_permit) = self.connection_limiter.try_acquire(peer_ip) else {
|
||||
self.block_rate_limit(peer_ip).await;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if !self.pass_pow_gate(&mut client, peer_ip).await? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !self.rate_limiter.check(peer_ip) {
|
||||
self.block_rate_limit(peer_ip).await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let handler = self.registry.primary()?;
|
||||
let handshake = match handler.handshake(&mut client).await {
|
||||
Ok(handshake) => handshake,
|
||||
Err(e) => {
|
||||
tracing::debug!("handshake rejected from {peer_addr}: {e}");
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["allowed"]).inc();
|
||||
self.allowed_1s.fetch_add(1, Ordering::Relaxed);
|
||||
self.reputation.record_good(peer_ip);
|
||||
|
||||
let mut backend = TcpStream::connect(self.next_upstream(&handshake.upstream.addr)).await?;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
if self.config.protocol.minecraft.enabled && self.config.protocol.minecraft.send_proxy_protocol {
|
||||
let source = client_addr;
|
||||
let destination = client.local_addr()?;
|
||||
backend.write_all(&proxy::protocol_v2(source, destination)?).await?;
|
||||
}
|
||||
if !handshake.initial_backend_data.is_empty() {
|
||||
backend.write_all(&handshake.initial_backend_data).await?;
|
||||
}
|
||||
tokio::io::copy_bidirectional(&mut client, &mut backend).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn next_upstream<'a>(&'a self, negotiated: &'a str) -> &'a str {
|
||||
let upstreams = &self.config.backend.upstreams;
|
||||
if upstreams.len() > 1 {
|
||||
let idx = self.upstream_cursor.fetch_add(1, Ordering::Relaxed) % upstreams.len();
|
||||
return upstreams[idx].as_str();
|
||||
}
|
||||
if let Some(first) = upstreams.first() {
|
||||
return first.as_str();
|
||||
}
|
||||
negotiated
|
||||
}
|
||||
|
||||
async fn block_rate_limit(&self, ip: IpAddr) {
|
||||
metrics::RATE_LIMIT_HITS.with_label_values(&["hit"]).inc();
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
self.reputation.record_bad(ip);
|
||||
if self.reputation.score(ip) < -40 {
|
||||
let duration_secs = Duration::from_secs(self.config.ban.ban_duration_secs);
|
||||
self.blacklist.add(ip, duration_secs, "low_reputation");
|
||||
if !self.config.proxy_protocol.ingress_enabled {
|
||||
self.xdp_ban(ip, duration_secs.as_secs());
|
||||
}
|
||||
self.push_event("block", ip, "low_reputation").await;
|
||||
tracing::info!("low reputation ban {ip}: rate-limit abuse");
|
||||
}
|
||||
}
|
||||
|
||||
fn xdp_ban(&self, ip: IpAddr, duration_secs: u64) {
|
||||
let Some(xdp) = &self.xdp else {
|
||||
return;
|
||||
};
|
||||
let IpAddr::V4(ip_v4) = ip else {
|
||||
return;
|
||||
};
|
||||
match xdp.lock().expect("xdp lock poisoned").ban_ip(ip_v4, duration_secs) {
|
||||
Ok(()) => tracing::debug!("xdp ban {ip_v4} for {duration_secs}s"),
|
||||
Err(e) => tracing::warn!("xdp ban failed for {ip_v4}: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn push_event(&self, event_type: &str, ip: IpAddr, data_string: &str) {
|
||||
let Some(writer) = &self.clickhouse else {
|
||||
return;
|
||||
};
|
||||
let event = ClickHouseEvent {
|
||||
timestamp: chrono::Utc::now(),
|
||||
event_type: event_type.to_string(),
|
||||
ip: ip.to_string(),
|
||||
data_float: 0.0,
|
||||
data_int: 0,
|
||||
data_string: data_string.to_string(),
|
||||
};
|
||||
if let Err(e) = writer.lock().await.push(event).await {
|
||||
tracing::debug!("clickhouse push error: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
use super::Gateway;
|
||||
use crate::engine::challenge::enforce as enforce_pow;
|
||||
use crate::metrics;
|
||||
use std::net::IpAddr;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
impl Gateway {
|
||||
pub(super) async fn pass_pow_gate(&self, client: &mut TcpStream, peer_ip: IpAddr) -> anyhow::Result<bool> {
|
||||
let pow_config = &self.config.pow;
|
||||
if !(pow_config.enabled && pow_config.difficulty > 0) {
|
||||
return Ok(true);
|
||||
}
|
||||
if self.whitelist.contains(&peer_ip) {
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["skipped"]).inc();
|
||||
metrics::POW_CURRENT_DIFFICULTY.set(pow_config.difficulty as i64);
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let diff = {
|
||||
let mut adjuster = self.adjuster.lock().expect("adjuster lock poisoned");
|
||||
adjuster.record_connection();
|
||||
adjuster.current_difficulty()
|
||||
};
|
||||
let result = enforce_pow(client, peer_ip, diff).await?;
|
||||
if !result {
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["failed"]).inc();
|
||||
tracing::debug!("pow: failed for {peer_ip}, dropping connection");
|
||||
return Ok(false);
|
||||
}
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["passed"]).inc();
|
||||
metrics::POW_CURRENT_DIFFICULTY.set(diff as i64);
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
mod inbound;
|
||||
pub(super) use inbound::read_v2_header;
|
||||
|
||||
/// Encodes a PROXY protocol v2 TCP header for the backend.
|
||||
/// The backend must accept connections only from trusted Rampart nodes.
|
||||
pub(super) fn protocol_v2(source: SocketAddr, destination: SocketAddr) -> anyhow::Result<Vec<u8>> {
|
||||
const SIGNATURE: [u8; 12] = *b"\r\n\r\n\0\r\nQUIT\n";
|
||||
if source.is_ipv4() != destination.is_ipv4() {
|
||||
anyhow::bail!("PROXY protocol source/destination address families differ");
|
||||
}
|
||||
|
||||
let (family, address_len) = if source.is_ipv4() {
|
||||
(0x11, 12usize)
|
||||
} else {
|
||||
(0x21, 36usize)
|
||||
};
|
||||
let mut header = Vec::with_capacity(16 + address_len);
|
||||
header.extend_from_slice(&SIGNATURE);
|
||||
header.push(0x21);
|
||||
header.push(family);
|
||||
header.extend_from_slice(&(address_len as u16).to_be_bytes());
|
||||
match (source, destination) {
|
||||
(SocketAddr::V4(src), SocketAddr::V4(dst)) => {
|
||||
header.extend_from_slice(&src.ip().octets());
|
||||
header.extend_from_slice(&dst.ip().octets());
|
||||
header.extend_from_slice(&src.port().to_be_bytes());
|
||||
header.extend_from_slice(&dst.port().to_be_bytes());
|
||||
},
|
||||
(SocketAddr::V6(src), SocketAddr::V6(dst)) => {
|
||||
header.extend_from_slice(&src.ip().octets());
|
||||
header.extend_from_slice(&dst.ip().octets());
|
||||
header.extend_from_slice(&src.port().to_be_bytes());
|
||||
header.extend_from_slice(&dst.port().to_be_bytes());
|
||||
},
|
||||
_ => anyhow::bail!("PROXY protocol source/destination address families differ"),
|
||||
}
|
||||
Ok(header)
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
//! Strict, bounded parser for trusted inbound PROXY protocol v2 headers.
|
||||
|
||||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
const SIGNATURE: [u8; 12] = *b"\r\n\r\n\0\r\nQUIT\n";
|
||||
const FIXED_HEADER_BYTES: usize = 16;
|
||||
|
||||
pub(crate) async fn read_v2_header(
|
||||
stream: &mut TcpStream,
|
||||
timeout: Duration,
|
||||
max_header_bytes: usize,
|
||||
) -> anyhow::Result<SocketAddr> {
|
||||
tokio::time::timeout(timeout, read_v2_header_inner(stream, max_header_bytes))
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("timed out reading inbound PROXY v2 header"))?
|
||||
}
|
||||
|
||||
async fn read_v2_header_inner(stream: &mut TcpStream, max_header_bytes: usize) -> anyhow::Result<SocketAddr> {
|
||||
let mut fixed = [0u8; FIXED_HEADER_BYTES];
|
||||
stream.read_exact(&mut fixed).await?;
|
||||
|
||||
if fixed[..12] != SIGNATURE {
|
||||
anyhow::bail!("invalid inbound PROXY protocol signature");
|
||||
}
|
||||
if fixed[12] != 0x21 {
|
||||
anyhow::bail!("inbound PROXY protocol requires version 2 PROXY command");
|
||||
}
|
||||
|
||||
let payload_len = usize::from(u16::from_be_bytes([fixed[14], fixed[15]]));
|
||||
let total_len = FIXED_HEADER_BYTES
|
||||
.checked_add(payload_len)
|
||||
.ok_or_else(|| anyhow::anyhow!("inbound PROXY header length overflow"))?;
|
||||
if total_len > max_header_bytes {
|
||||
anyhow::bail!("inbound PROXY header exceeds configured limit");
|
||||
}
|
||||
|
||||
let address_len = match fixed[13] {
|
||||
0x11 => 12,
|
||||
0x21 => 36,
|
||||
family => anyhow::bail!("unsupported inbound PROXY address family/protocol 0x{family:02x}"),
|
||||
};
|
||||
if payload_len < address_len {
|
||||
anyhow::bail!("inbound PROXY header is shorter than its address block");
|
||||
}
|
||||
if payload_len != address_len {
|
||||
anyhow::bail!("inbound PROXY v2 TLVs are not supported");
|
||||
}
|
||||
|
||||
let mut payload = vec![0u8; payload_len];
|
||||
stream.read_exact(&mut payload).await?;
|
||||
match fixed[13] {
|
||||
0x11 => {
|
||||
let source = Ipv4Addr::new(payload[0], payload[1], payload[2], payload[3]);
|
||||
let port = u16::from_be_bytes([payload[8], payload[9]]);
|
||||
Ok(SocketAddr::V4(SocketAddrV4::new(source, port)))
|
||||
},
|
||||
0x21 => {
|
||||
let source_bytes: [u8; 16] = payload[..16]
|
||||
.try_into()
|
||||
.map_err(|_| anyhow::anyhow!("invalid inbound PROXY IPv6 source address"))?;
|
||||
let source = Ipv6Addr::from(source_bytes);
|
||||
let port = u16::from_be_bytes([payload[32], payload[33]]);
|
||||
Ok(SocketAddr::V6(SocketAddrV6::new(source, port, 0, 0)))
|
||||
},
|
||||
family => anyhow::bail!("unsupported inbound PROXY address family/protocol 0x{family:02x}"),
|
||||
}
|
||||
}
|
||||
|
|
@ -2,16 +2,6 @@ use dashmap::DashMap;
|
|||
use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
const LOCAL_BAN_EVENT_CAPACITY: usize = 4096;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LocalBanEvent {
|
||||
pub ip: IpAddr,
|
||||
pub expires_at_unix: i64,
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
struct BanEntry {
|
||||
expires: Instant,
|
||||
|
|
@ -20,8 +10,6 @@ struct BanEntry {
|
|||
|
||||
pub struct Blacklist {
|
||||
entries: Arc<DashMap<IpAddr, BanEntry>>,
|
||||
redis_entries: Arc<DashMap<IpAddr, BanEntry>>,
|
||||
local_ban_events: broadcast::Sender<LocalBanEvent>,
|
||||
}
|
||||
|
||||
impl Default for Blacklist {
|
||||
|
|
@ -32,24 +20,23 @@ impl Default for Blacklist {
|
|||
|
||||
impl Blacklist {
|
||||
pub fn new() -> Self {
|
||||
let (local_ban_events, _) = broadcast::channel(LOCAL_BAN_EVENT_CAPACITY);
|
||||
Self {
|
||||
entries: Arc::new(DashMap::new()),
|
||||
redis_entries: Arc::new(DashMap::new()),
|
||||
local_ban_events,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_blocked(&self, ip: IpAddr) -> bool {
|
||||
Self::is_blocked_in(&self.entries, ip) || Self::is_blocked_in(&self.redis_entries, ip)
|
||||
if let Some(entry) = self.entries.get(&ip) {
|
||||
if entry.expires > Instant::now() {
|
||||
return true;
|
||||
}
|
||||
drop(entry);
|
||||
self.entries.remove(&ip);
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn add(&self, ip: IpAddr, duration: Duration, reason: &str) {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
let expires_at_unix = now.saturating_add(duration.as_secs()).min(i64::MAX as u64) as i64;
|
||||
self.entries.insert(
|
||||
ip,
|
||||
BanEntry {
|
||||
|
|
@ -57,85 +44,22 @@ impl Blacklist {
|
|||
_reason: reason.to_string(),
|
||||
},
|
||||
);
|
||||
let _ = self.local_ban_events.send(LocalBanEvent {
|
||||
ip,
|
||||
expires_at_unix,
|
||||
reason: reason.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn subscribe_local_bans(&self) -> broadcast::Receiver<LocalBanEvent> {
|
||||
self.local_ban_events.subscribe()
|
||||
}
|
||||
|
||||
pub fn local_snapshot(&self) -> Vec<LocalBanEvent> {
|
||||
self.entries
|
||||
.iter()
|
||||
.filter(|entry| entry.expires > Instant::now())
|
||||
.map(|entry| LocalBanEvent {
|
||||
ip: *entry.key(),
|
||||
expires_at_unix: std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs()
|
||||
.saturating_add(entry.expires.saturating_duration_since(Instant::now()).as_secs())
|
||||
.min(i64::MAX as u64) as i64,
|
||||
reason: entry._reason.clone(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn remove(&self, ip: IpAddr) {
|
||||
self.entries.remove(&ip);
|
||||
}
|
||||
|
||||
pub fn add_redis(&self, ip: IpAddr, duration: Duration, reason: &str) {
|
||||
self.redis_entries.insert(
|
||||
ip,
|
||||
BanEntry {
|
||||
expires: Instant::now() + duration,
|
||||
_reason: reason.to_string(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn remove_redis(&self, ip: IpAddr) {
|
||||
self.redis_entries.remove(&ip);
|
||||
}
|
||||
|
||||
/// Replace Redis-managed bans while preserving local detector bans.
|
||||
pub fn replace_redis<I>(&self, entries: I)
|
||||
where
|
||||
I: IntoIterator<Item = (IpAddr, Duration, String)>,
|
||||
{
|
||||
self.redis_entries.clear();
|
||||
for (ip, duration, reason) in entries {
|
||||
self.add_redis(ip, duration, &reason);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.entries.len() + self.redis_entries.len()
|
||||
self.entries.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.entries.is_empty() && self.redis_entries.is_empty()
|
||||
self.entries.is_empty()
|
||||
}
|
||||
|
||||
pub fn clear_expired(&self) {
|
||||
self.entries.retain(|_, entry| entry.expires > Instant::now());
|
||||
self.redis_entries.retain(|_, entry| entry.expires > Instant::now());
|
||||
}
|
||||
|
||||
fn is_blocked_in(entries: &DashMap<IpAddr, BanEntry>, ip: IpAddr) -> bool {
|
||||
if let Some(entry) = entries.get(&ip) {
|
||||
if entry.expires > Instant::now() {
|
||||
return true;
|
||||
}
|
||||
drop(entry);
|
||||
entries.remove(&ip);
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,59 +0,0 @@
|
|||
use dashmap::DashMap;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
/// Caps simultaneous in-flight connections per source IP.
|
||||
pub struct ConnectionLimiter {
|
||||
counts: Arc<DashMap<IpAddr, Arc<AtomicUsize>>>,
|
||||
limit: usize,
|
||||
}
|
||||
|
||||
impl ConnectionLimiter {
|
||||
#[must_use]
|
||||
pub fn new(limit: usize) -> Self {
|
||||
Self {
|
||||
counts: Arc::new(DashMap::new()),
|
||||
limit: limit.max(1),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_acquire(&self, ip: IpAddr) -> Option<ConnectionPermit> {
|
||||
let entry = self.counts.entry(ip).or_insert_with(|| Arc::new(AtomicUsize::new(0)));
|
||||
let count = Arc::clone(entry.value());
|
||||
let current = count.fetch_add(1, Ordering::AcqRel);
|
||||
drop(entry);
|
||||
if current >= self.limit {
|
||||
count.fetch_sub(1, Ordering::AcqRel);
|
||||
self.remove_if_idle(ip, &count);
|
||||
return None;
|
||||
}
|
||||
Some(ConnectionPermit {
|
||||
ip,
|
||||
count,
|
||||
counts: Arc::clone(&self.counts),
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_if_idle(&self, ip: IpAddr, count: &Arc<AtomicUsize>) {
|
||||
self.counts.remove_if(&ip, |_, current| {
|
||||
Arc::ptr_eq(current, count) && current.load(Ordering::Acquire) == 0
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ConnectionPermit {
|
||||
ip: IpAddr,
|
||||
count: Arc<AtomicUsize>,
|
||||
counts: Arc<DashMap<IpAddr, Arc<AtomicUsize>>>,
|
||||
}
|
||||
|
||||
impl Drop for ConnectionPermit {
|
||||
fn drop(&mut self) {
|
||||
if self.count.fetch_sub(1, Ordering::AcqRel) == 1 {
|
||||
self.counts.remove_if(&self.ip, |_, current| {
|
||||
Arc::ptr_eq(current, &self.count) && current.load(Ordering::Acquire) == 0
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
//! Фильтры входящего трафика.
|
||||
|
||||
pub mod blacklist;
|
||||
pub mod connection_limit;
|
||||
pub mod geo;
|
||||
pub mod rate_limit;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,10 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
use crate::manager::AppState;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use axum::{Json, extract::State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "store-redis")]
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct BlacklistEntry {
|
||||
|
|
@ -26,19 +32,68 @@ pub struct AddBlacklistRequest {
|
|||
pub duration_secs: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct RemoveBlacklistRequest {
|
||||
pub target: String,
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub async fn list_blacklist(State(state): State<Arc<AppState>>) -> Json<BlacklistResponse> {
|
||||
let mut conn = match state.redis_client.get_multiplexed_async_connection().await {
|
||||
Ok(c) => c,
|
||||
Err(_) => {
|
||||
return Json(BlacklistResponse {
|
||||
items: vec![],
|
||||
total: 0,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
let members: Vec<String> = match redis::cmd("SMEMBERS")
|
||||
.arg("rampart:blacklist")
|
||||
.query_async(&mut conn)
|
||||
.await
|
||||
{
|
||||
Ok(m) => m,
|
||||
Err(_) => {
|
||||
return Json(BlacklistResponse {
|
||||
items: vec![],
|
||||
total: 0,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
let items: Vec<BlacklistEntry> = members
|
||||
.into_iter()
|
||||
.map(|target| BlacklistEntry {
|
||||
target,
|
||||
entry_type: "ip".to_string(),
|
||||
reason: "manual".to_string(),
|
||||
created_at: chrono::Utc::now().to_rfc3339(),
|
||||
expires_at: None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let total = items.len();
|
||||
Json(BlacklistResponse { items, total })
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
mod common;
|
||||
#[cfg(feature = "store-redis")]
|
||||
mod list;
|
||||
#[cfg(feature = "store-redis")]
|
||||
mod mutate;
|
||||
pub async fn add_blacklist(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(req): Json<AddBlacklistRequest>,
|
||||
) -> Json<serde_json::Value> {
|
||||
let mut conn = match state.redis_client.get_multiplexed_async_connection().await {
|
||||
Ok(c) => c,
|
||||
Err(_) => return Json(serde_json::json!({"error": "redis unavailable"})),
|
||||
};
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub use list::list_blacklist;
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub use mutate::{add_blacklist, remove_blacklist};
|
||||
let _: () = redis::cmd("SADD")
|
||||
.arg("rampart:blacklist")
|
||||
.arg(&req.target)
|
||||
.query_async(&mut conn)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
tracing::info!("Added to blacklist: {} ({})", req.target, req.reason);
|
||||
|
||||
Json(serde_json::json!({
|
||||
"status": "added",
|
||||
"target": req.target,
|
||||
"reason": req.reason
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
use axum::{Json, http::StatusCode};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct BlacklistEvent<'a> {
|
||||
ip: &'a str,
|
||||
action: &'a str,
|
||||
duration_secs: u64,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
expires_at_unix: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub(super) struct BlacklistMetadata {
|
||||
pub created_at_unix: i64,
|
||||
pub expires_at_unix: i64,
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
pub(super) const BLACKLIST_KEY: &str = "rampart:blacklist";
|
||||
pub(super) const BLACKLIST_METADATA_KEY: &str = "rampart:blacklist:metadata";
|
||||
pub(super) const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
|
||||
pub(super) const DEFAULT_BAN_DURATION_SECS: u64 = 300;
|
||||
pub(super) const LEGACY_BAN_DURATION_SECS: u64 = 300;
|
||||
pub(super) const MAX_BAN_DURATION_SECS: u64 = 31_536_000;
|
||||
pub(super) const ADD_BAN_SCRIPT: &str = "redis.call('HSET', KEYS[2], ARGV[1], ARGV[2]); redis.call('SADD', KEYS[1], ARGV[1]); redis.call('PUBLISH', KEYS[3], ARGV[3]); return 1";
|
||||
pub(super) const REMOVE_BAN_SCRIPT: &str = "redis.call('SREM', KEYS[1], ARGV[1]); redis.call('HDEL', KEYS[2], ARGV[1]); redis.call('PUBLISH', KEYS[3], ARGV[2]); return 1";
|
||||
|
||||
pub(super) fn legacy_metadata(now: i64) -> BlacklistMetadata {
|
||||
BlacklistMetadata {
|
||||
created_at_unix: now,
|
||||
expires_at_unix: now.saturating_add(LEGACY_BAN_DURATION_SECS as i64),
|
||||
reason: "legacy_unverified".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn format_timestamp(timestamp: i64) -> String {
|
||||
chrono::DateTime::from_timestamp(timestamp, 0)
|
||||
.map(|value| value.to_rfc3339())
|
||||
.unwrap_or_else(|| chrono::Utc::now().to_rfc3339())
|
||||
}
|
||||
|
||||
pub(super) fn event_payload(
|
||||
ip: &str,
|
||||
action: &str,
|
||||
duration_secs: u64,
|
||||
expires_at_unix: Option<i64>,
|
||||
) -> Result<String, (StatusCode, Json<serde_json::Value>)> {
|
||||
serde_json::to_string(&BlacklistEvent {
|
||||
ip,
|
||||
action,
|
||||
duration_secs,
|
||||
expires_at_unix,
|
||||
})
|
||||
.map_err(|_| api_error(StatusCode::INTERNAL_SERVER_ERROR, "failed to serialize blacklist event"))
|
||||
}
|
||||
|
||||
pub(super) fn parse_ip(target: &str) -> Result<std::net::IpAddr, (StatusCode, Json<serde_json::Value>)> {
|
||||
target
|
||||
.parse()
|
||||
.map_err(|_| api_error(StatusCode::BAD_REQUEST, "target must be an IP address"))
|
||||
}
|
||||
|
||||
pub(super) fn api_error(status: StatusCode, message: &str) -> (StatusCode, Json<serde_json::Value>) {
|
||||
(status, Json(serde_json::json!({"error": message})))
|
||||
}
|
||||
|
||||
pub(super) fn redis_unavailable(_: redis::RedisError) -> (StatusCode, Json<serde_json::Value>) {
|
||||
api_error(StatusCode::SERVICE_UNAVAILABLE, "redis unavailable")
|
||||
}
|
||||
|
||||
pub(super) fn redis_error(error: redis::RedisError) -> (StatusCode, Json<serde_json::Value>) {
|
||||
tracing::error!("Blacklist Redis operation failed: {error}");
|
||||
api_error(StatusCode::SERVICE_UNAVAILABLE, "redis operation failed")
|
||||
}
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
use super::common::{
|
||||
BLACKLIST_KEY, BLACKLIST_METADATA_KEY, BlacklistMetadata, format_timestamp, legacy_metadata, redis_error,
|
||||
redis_unavailable,
|
||||
};
|
||||
use super::{BlacklistEntry, BlacklistResponse};
|
||||
use crate::manager::AppState;
|
||||
use axum::{Json, extract::State, http::StatusCode};
|
||||
use redis::AsyncCommands;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn list_blacklist(
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<BlacklistResponse>, (StatusCode, Json<serde_json::Value>)> {
|
||||
let mut conn = state
|
||||
.redis_client
|
||||
.get_multiplexed_async_connection()
|
||||
.await
|
||||
.map_err(redis_unavailable)?;
|
||||
let members: Vec<String> = redis::cmd("SMEMBERS")
|
||||
.arg(BLACKLIST_KEY)
|
||||
.query_async(&mut conn)
|
||||
.await
|
||||
.map_err(redis_error)?;
|
||||
let metadata: HashMap<String, String> = conn.hgetall(BLACKLIST_METADATA_KEY).await.map_err(redis_error)?;
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let mut items = Vec::with_capacity(members.len());
|
||||
|
||||
for target in members {
|
||||
let Ok(ip) = target.parse::<std::net::IpAddr>() else {
|
||||
continue;
|
||||
};
|
||||
let record = match metadata.get(&target) {
|
||||
Some(encoded) => match serde_json::from_str::<BlacklistMetadata>(encoded) {
|
||||
Ok(record) => record,
|
||||
Err(_) => continue,
|
||||
},
|
||||
None => {
|
||||
let legacy = legacy_metadata(now);
|
||||
let encoded = serde_json::to_string(&legacy).map_err(|_| {
|
||||
super::common::api_error(StatusCode::INTERNAL_SERVER_ERROR, "failed to encode blacklist metadata")
|
||||
})?;
|
||||
let inserted: i64 = redis::cmd("HSETNX")
|
||||
.arg(BLACKLIST_METADATA_KEY)
|
||||
.arg(&target)
|
||||
.arg(encoded)
|
||||
.query_async(&mut conn)
|
||||
.await
|
||||
.map_err(redis_error)?;
|
||||
if inserted == 1 {
|
||||
legacy
|
||||
} else {
|
||||
let current: Option<String> =
|
||||
conn.hget(BLACKLIST_METADATA_KEY, &target).await.map_err(redis_error)?;
|
||||
match current.and_then(|value| serde_json::from_str(&value).ok()) {
|
||||
Some(record) => record,
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
if record.expires_at_unix <= now
|
||||
|| chrono::DateTime::from_timestamp(record.created_at_unix, 0).is_none()
|
||||
|| chrono::DateTime::from_timestamp(record.expires_at_unix, 0).is_none()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
items.push(BlacklistEntry {
|
||||
target: ip.to_string(),
|
||||
entry_type: "ip".to_string(),
|
||||
reason: record.reason,
|
||||
created_at: format_timestamp(record.created_at_unix),
|
||||
expires_at: Some(format_timestamp(record.expires_at_unix)),
|
||||
});
|
||||
}
|
||||
|
||||
let total = items.len();
|
||||
Ok(Json(BlacklistResponse { items, total }))
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
use super::common::{
|
||||
ADD_BAN_SCRIPT, BLACKLIST_CHANNEL, BLACKLIST_KEY, BLACKLIST_METADATA_KEY, DEFAULT_BAN_DURATION_SECS,
|
||||
MAX_BAN_DURATION_SECS, REMOVE_BAN_SCRIPT, api_error, event_payload, parse_ip, redis_error, redis_unavailable,
|
||||
};
|
||||
use super::{AddBlacklistRequest, RemoveBlacklistRequest};
|
||||
use crate::manager::AppState;
|
||||
use axum::{Json, extract::State, http::StatusCode};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn add_blacklist(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(req): Json<AddBlacklistRequest>,
|
||||
) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
|
||||
let ip = parse_ip(&req.target)?;
|
||||
if req.entry_type != "ip" {
|
||||
return Err(api_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"only IP blacklist entries are supported",
|
||||
));
|
||||
}
|
||||
let duration_secs = req.duration_secs.unwrap_or(DEFAULT_BAN_DURATION_SECS);
|
||||
if duration_secs == 0 {
|
||||
return Err(api_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"duration_secs must be greater than zero",
|
||||
));
|
||||
}
|
||||
if duration_secs > MAX_BAN_DURATION_SECS {
|
||||
return Err(api_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"duration_secs must not exceed one year",
|
||||
));
|
||||
}
|
||||
|
||||
let target = ip.to_string();
|
||||
let created_at_unix = chrono::Utc::now().timestamp();
|
||||
let metadata = super::common::BlacklistMetadata {
|
||||
created_at_unix,
|
||||
expires_at_unix: created_at_unix + duration_secs as i64,
|
||||
reason: req.reason.clone(),
|
||||
};
|
||||
let encoded_metadata = serde_json::to_string(&metadata)
|
||||
.map_err(|_| api_error(StatusCode::INTERNAL_SERVER_ERROR, "failed to encode blacklist metadata"))?;
|
||||
let event = event_payload(&target, "ban", duration_secs, Some(metadata.expires_at_unix))?;
|
||||
let mut conn = state
|
||||
.redis_client
|
||||
.get_multiplexed_async_connection()
|
||||
.await
|
||||
.map_err(redis_unavailable)?;
|
||||
let _: i64 = redis::cmd("EVAL")
|
||||
.arg(ADD_BAN_SCRIPT)
|
||||
.arg(3)
|
||||
.arg(BLACKLIST_KEY)
|
||||
.arg(BLACKLIST_METADATA_KEY)
|
||||
.arg(BLACKLIST_CHANNEL)
|
||||
.arg(&target)
|
||||
.arg(encoded_metadata)
|
||||
.arg(event)
|
||||
.query_async(&mut conn)
|
||||
.await
|
||||
.map_err(redis_error)?;
|
||||
|
||||
tracing::info!("Added to blacklist: {} ({})", target, req.reason);
|
||||
Ok(Json(serde_json::json!({
|
||||
"status": "added",
|
||||
"target": target,
|
||||
"reason": req.reason,
|
||||
"duration_secs": duration_secs
|
||||
})))
|
||||
}
|
||||
|
||||
pub async fn remove_blacklist(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(req): Json<RemoveBlacklistRequest>,
|
||||
) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
|
||||
let ip = parse_ip(&req.target)?;
|
||||
let target = ip.to_string();
|
||||
let event = event_payload(&target, "unban", 0, None)?;
|
||||
let mut conn = state
|
||||
.redis_client
|
||||
.get_multiplexed_async_connection()
|
||||
.await
|
||||
.map_err(redis_unavailable)?;
|
||||
let _: i64 = redis::cmd("EVAL")
|
||||
.arg(REMOVE_BAN_SCRIPT)
|
||||
.arg(3)
|
||||
.arg(BLACKLIST_KEY)
|
||||
.arg(BLACKLIST_METADATA_KEY)
|
||||
.arg(BLACKLIST_CHANNEL)
|
||||
.arg(&target)
|
||||
.arg(event)
|
||||
.query_async(&mut conn)
|
||||
.await
|
||||
.map_err(redis_error)?;
|
||||
|
||||
tracing::info!("Removed from blacklist: {target}");
|
||||
Ok(Json(serde_json::json!({"status": "removed", "target": target})))
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
use axum::Json;
|
||||
use axum::body::Body;
|
||||
use axum::extract::State;
|
||||
use axum::http::{Request, StatusCode};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::Response;
|
||||
|
|
@ -37,7 +36,6 @@ pub fn verify_token(token: &str, secret: &str, audience: &str) -> Result<Claims,
|
|||
}
|
||||
|
||||
pub async fn auth_middleware(
|
||||
State(state): State<Arc<crate::manager::AppState>>,
|
||||
request: Request<Body>,
|
||||
next: Next,
|
||||
) -> Result<Response, (StatusCode, Json<serde_json::Value>)> {
|
||||
|
|
@ -57,6 +55,16 @@ pub async fn auth_middleware(
|
|||
},
|
||||
};
|
||||
|
||||
let state = match request.extensions().get::<Arc<crate::manager::AppState>>() {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
return Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({"error": "internal error"})),
|
||||
));
|
||||
},
|
||||
};
|
||||
|
||||
if verify_token(token, &state.jwt_secret, &state.jwt_audience).is_err() {
|
||||
return Err((
|
||||
StatusCode::UNAUTHORIZED,
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ async fn collect_node_keys(conn: &mut redis::aio::MultiplexedConnection) -> anyh
|
|||
let mut iter = conn.scan_options::<String>(opts).await?;
|
||||
let mut keys = Vec::new();
|
||||
while let Some(key) = iter.next().await {
|
||||
keys.push(key?);
|
||||
keys.push(key);
|
||||
}
|
||||
Ok(keys)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,16 +3,16 @@
|
|||
//! Первый plugin-реализаций [`ProtocolHandler`]: читает request-head
|
||||
//! постепенно (TCP приходит фрагментами), проверяет его по политике из
|
||||
//! секции `[protocol.http]` и возвращает upstream для туннелирования.
|
||||
//! Прочитанная голова передаётся upstream как начальные байты туннеля.
|
||||
//! Голова читается через `peek`, чтобы байты остались в сокете и движок
|
||||
//! проксировал запрос на backend без потерь.
|
||||
|
||||
mod parser;
|
||||
|
||||
use crate::config::HttpProtocolConfig;
|
||||
use crate::protocol::{HandshakeResult, ProtocolHandler, Upstream};
|
||||
use crate::protocol::{ProtocolHandler, Upstream};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
/// Обработчик HTTP/1.1: verdict Allow → tunnel на upstream, Reject → drop.
|
||||
|
|
@ -37,27 +37,31 @@ impl HttpProtocolHandler {
|
|||
}
|
||||
}
|
||||
|
||||
/// Накапливает request-head без повторного чтения одного и того же префикса.
|
||||
/// Накапливает request-head через `peek` до терминатора или лимита.
|
||||
///
|
||||
/// # Errors
|
||||
/// Таймаут, закрытие клиента, превышение `max_header_bytes` или ошибка I/O.
|
||||
async fn read_head(&self, stream: &mut TcpStream) -> anyhow::Result<Vec<u8>> {
|
||||
async fn read_head(&self, stream: &TcpStream) -> anyhow::Result<Vec<u8>> {
|
||||
let limit = self.policy.max_header_bytes;
|
||||
if limit == 0 {
|
||||
anyhow::bail!("protocol.http.max_header_bytes must be positive");
|
||||
}
|
||||
let mut buf = Vec::with_capacity(limit);
|
||||
let mut buf = vec![0u8; limit];
|
||||
let mut len = 0usize;
|
||||
let accumulate = async {
|
||||
loop {
|
||||
if parser::scan_head(&buf).is_some() {
|
||||
if let Some(head_len) = parser::scan_head(&buf[..len]) {
|
||||
buf.truncate(head_len);
|
||||
return Ok(buf);
|
||||
}
|
||||
if buf.len() >= limit {
|
||||
if len >= limit {
|
||||
anyhow::bail!("request head exceeds {limit} bytes");
|
||||
}
|
||||
let mut byte = [0u8; 1];
|
||||
stream.read_exact(&mut byte).await?;
|
||||
buf.push(byte[0]);
|
||||
let n = stream.peek(&mut buf[len..]).await?;
|
||||
if n == 0 {
|
||||
anyhow::bail!("client closed connection before head was complete");
|
||||
}
|
||||
len += n;
|
||||
}
|
||||
};
|
||||
match tokio::time::timeout(self.timeout, accumulate).await {
|
||||
|
|
@ -75,14 +79,11 @@ impl ProtocolHandler for HttpProtocolHandler {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
let head = self.read_head(stream).await?;
|
||||
match parser::evaluate(&head, &self.policy) {
|
||||
parser::Verdict::Allow { .. } => Ok(HandshakeResult::with_initial_data(
|
||||
Upstream::new(self.upstream.clone()),
|
||||
head,
|
||||
)),
|
||||
parser::Verdict::Allow { .. } => Ok(Upstream::new(self.upstream.clone())),
|
||||
parser::Verdict::Reject { reason } => Err(anyhow::anyhow!("http: {reason}")),
|
||||
}
|
||||
})
|
||||
|
|
@ -103,9 +104,9 @@ mod tests {
|
|||
let client = tokio::net::TcpStream::connect(listener.local_addr().expect("addr"))
|
||||
.await
|
||||
.expect("connect");
|
||||
let (mut server, _) = listener.accept().await.expect("accept");
|
||||
let (server, _) = listener.accept().await.expect("accept");
|
||||
drop(listener);
|
||||
assert!(handler.read_head(&mut server).await.is_err());
|
||||
assert!(handler.read_head(&server).await.is_err());
|
||||
drop(client);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,85 +0,0 @@
|
|||
//! Minecraft Java TCP handshake validation (feature `protocol-minecraft`).
|
||||
|
||||
mod parser;
|
||||
|
||||
use crate::config::MinecraftProtocolConfig;
|
||||
use crate::protocol::{HandshakeResult, ProtocolHandler, Upstream};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
pub struct MinecraftProtocolHandler {
|
||||
upstream: String,
|
||||
max_frame_bytes: usize,
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl MinecraftProtocolHandler {
|
||||
#[must_use]
|
||||
pub fn new(config: &MinecraftProtocolConfig, upstreams: &[String]) -> Self {
|
||||
Self {
|
||||
// Config validation requires exactly one backend when this handler is enabled.
|
||||
upstream: upstreams.first().cloned().unwrap_or_default(),
|
||||
max_frame_bytes: config.max_frame_bytes,
|
||||
timeout: Duration::from_secs(config.timeout_secs),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_handshake(&self, stream: &mut TcpStream) -> anyhow::Result<Vec<u8>> {
|
||||
let mut frame = Vec::with_capacity(64);
|
||||
let mut frame_len = 0usize;
|
||||
for index in 0..5 {
|
||||
let mut byte = [0u8; 1];
|
||||
stream.read_exact(&mut byte).await?;
|
||||
frame.push(byte[0]);
|
||||
if index == 4 && byte[0] & 0xf0 != 0 {
|
||||
anyhow::bail!("Minecraft frame length VarInt exceeds 32 bits");
|
||||
}
|
||||
if byte[0] & 0x80 == 0 {
|
||||
let (decoded, prefix_len) = parser::decode_frame_length(&frame)?;
|
||||
frame_len = decoded;
|
||||
if frame_len == 0 || frame_len > self.max_frame_bytes {
|
||||
anyhow::bail!("Minecraft handshake frame length {frame_len} is outside allowed range");
|
||||
}
|
||||
if prefix_len != frame.len() {
|
||||
anyhow::bail!("invalid Minecraft frame length prefix");
|
||||
}
|
||||
break;
|
||||
}
|
||||
if index == 4 {
|
||||
anyhow::bail!("Minecraft frame length VarInt exceeds five bytes");
|
||||
}
|
||||
}
|
||||
if frame_len == 0 {
|
||||
anyhow::bail!("invalid empty Minecraft handshake frame");
|
||||
}
|
||||
let prefix_len = frame.len();
|
||||
frame.resize(prefix_len + frame_len, 0);
|
||||
stream.read_exact(&mut frame[prefix_len..]).await?;
|
||||
parser::parse_frame(&frame, self.max_frame_bytes)?;
|
||||
Ok(frame)
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtocolHandler for MinecraftProtocolHandler {
|
||||
fn name(&self) -> &'static str {
|
||||
"minecraft"
|
||||
}
|
||||
|
||||
fn handshake<'a>(
|
||||
&'a self,
|
||||
stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
let frame = tokio::time::timeout(self.timeout, self.read_handshake(stream))
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("timed out reading Minecraft handshake"))??;
|
||||
Ok(HandshakeResult::with_initial_data(
|
||||
Upstream::new(self.upstream.clone()),
|
||||
frame,
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
//! Bounded parser for the Minecraft Java handshake packet.
|
||||
|
||||
const HANDSHAKE_PACKET_ID: i32 = 0;
|
||||
const MAX_VARINT_BYTES: usize = 5;
|
||||
const MAX_HOST_BYTES: usize = 255;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum NextState {
|
||||
Status,
|
||||
Login,
|
||||
}
|
||||
|
||||
pub(super) fn decode_frame_length(prefix: &[u8]) -> anyhow::Result<(usize, usize)> {
|
||||
let (length, bytes_read) = decode_varint(prefix, 0)?;
|
||||
let length = usize::try_from(length).map_err(|_| anyhow::anyhow!("negative frame length"))?;
|
||||
Ok((length, bytes_read))
|
||||
}
|
||||
|
||||
/// Validates a complete length-prefixed handshake frame and returns its state.
|
||||
pub(super) fn parse_frame(frame: &[u8], max_frame_bytes: usize) -> anyhow::Result<NextState> {
|
||||
let (frame_len, prefix_len) = decode_varint(frame, 0)?;
|
||||
let frame_len = usize::try_from(frame_len).map_err(|_| anyhow::anyhow!("negative frame length"))?;
|
||||
if frame_len == 0 || frame_len > max_frame_bytes {
|
||||
anyhow::bail!("Minecraft handshake frame length {frame_len} is outside allowed range");
|
||||
}
|
||||
if frame.len() != prefix_len + frame_len {
|
||||
anyhow::bail!("Minecraft handshake frame length does not match packet");
|
||||
}
|
||||
|
||||
let body = &frame[prefix_len..];
|
||||
let (packet_id, mut cursor) = decode_varint(body, 0)?;
|
||||
if packet_id != HANDSHAKE_PACKET_ID {
|
||||
anyhow::bail!("unexpected Minecraft packet id {packet_id} in handshake state");
|
||||
}
|
||||
|
||||
let (protocol_version, next) = decode_varint(body, cursor)?;
|
||||
if protocol_version < 0 {
|
||||
anyhow::bail!("Minecraft protocol version must be non-negative");
|
||||
}
|
||||
cursor = next;
|
||||
|
||||
let (host_len, next) = decode_varint(body, cursor)?;
|
||||
let host_len = usize::try_from(host_len).map_err(|_| anyhow::anyhow!("negative host length"))?;
|
||||
if host_len == 0 || host_len > MAX_HOST_BYTES {
|
||||
anyhow::bail!("Minecraft handshake hostname length is outside allowed range");
|
||||
}
|
||||
cursor = next;
|
||||
let host_end = cursor
|
||||
.checked_add(host_len)
|
||||
.ok_or_else(|| anyhow::anyhow!("hostname length overflow"))?;
|
||||
let host_bytes = body
|
||||
.get(cursor..host_end)
|
||||
.ok_or_else(|| anyhow::anyhow!("truncated Minecraft handshake hostname"))?;
|
||||
std::str::from_utf8(host_bytes).map_err(|_| anyhow::anyhow!("Minecraft handshake hostname is not UTF-8"))?;
|
||||
cursor = host_end;
|
||||
|
||||
let port_end = cursor
|
||||
.checked_add(2)
|
||||
.ok_or_else(|| anyhow::anyhow!("port length overflow"))?;
|
||||
if body.get(cursor..port_end).is_none() {
|
||||
anyhow::bail!("truncated Minecraft handshake port");
|
||||
}
|
||||
cursor = port_end;
|
||||
|
||||
let (state, next) = decode_varint(body, cursor)?;
|
||||
cursor = next;
|
||||
if cursor != body.len() {
|
||||
anyhow::bail!("unexpected trailing bytes in Minecraft handshake");
|
||||
}
|
||||
|
||||
match state {
|
||||
1 => Ok(NextState::Status),
|
||||
2 => Ok(NextState::Login),
|
||||
_ => anyhow::bail!("unsupported Minecraft next state {state}; expected status or login"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes signed Minecraft VarInt from a packet, returning value and next offset.
|
||||
fn decode_varint(bytes: &[u8], start: usize) -> anyhow::Result<(i32, usize)> {
|
||||
let mut value = 0u32;
|
||||
for index in 0..MAX_VARINT_BYTES {
|
||||
let byte = *bytes
|
||||
.get(start + index)
|
||||
.ok_or_else(|| anyhow::anyhow!("truncated Minecraft VarInt"))?;
|
||||
if index == 4 && byte & 0xf0 != 0 {
|
||||
anyhow::bail!("Minecraft VarInt exceeds 32 bits");
|
||||
}
|
||||
value |= u32::from(byte & 0x7f) << (index * 7);
|
||||
if byte & 0x80 == 0 {
|
||||
return Ok((value as i32, start + index + 1));
|
||||
}
|
||||
}
|
||||
anyhow::bail!("Minecraft VarInt exceeds five bytes")
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
#[cfg(feature = "protocol-http")]
|
||||
pub mod http;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
pub mod minecraft;
|
||||
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
|
@ -21,30 +19,6 @@ impl Upstream {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct HandshakeResult {
|
||||
pub upstream: Upstream,
|
||||
pub initial_backend_data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl HandshakeResult {
|
||||
#[must_use]
|
||||
pub fn new(upstream: Upstream) -> Self {
|
||||
Self {
|
||||
upstream,
|
||||
initial_backend_data: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_initial_data(upstream: Upstream, initial_backend_data: Vec<u8>) -> Self {
|
||||
Self {
|
||||
upstream,
|
||||
initial_backend_data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Точка расширения платформы: реализация для конкретного L7-протокола.
|
||||
///
|
||||
/// Контракт минимальный: обработчик проводит handshake с клиентом
|
||||
|
|
@ -65,7 +39,7 @@ pub trait ProtocolHandler: Send + Sync {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>>;
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>>;
|
||||
}
|
||||
|
||||
/// Реестр скомпилированных протокольных обработчиков.
|
||||
|
|
@ -134,8 +108,8 @@ mod tests {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
_stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(HandshakeResult::new(Upstream::new("127.0.0.1:9000"))) })
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(Upstream::new("127.0.0.1:9000")) })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
pub mod redis;
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub use redis::{start_blacklist_sync, start_local_ban_publisher};
|
||||
pub use redis::start_blacklist_sync;
|
||||
|
||||
pub mod clickhouse;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,29 @@
|
|||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::store::StateStore;
|
||||
use futures::StreamExt;
|
||||
use redis::AsyncCommands;
|
||||
|
||||
mod sync;
|
||||
pub use sync::{start_blacklist_sync, start_local_ban_publisher};
|
||||
use redis::Msg;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
|
||||
pub struct RedisStore {
|
||||
client: redis::Client,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct BlacklistEvent {
|
||||
ip: String,
|
||||
action: String,
|
||||
#[serde(default = "default_duration")]
|
||||
duration_secs: u64,
|
||||
}
|
||||
|
||||
fn default_duration() -> u64 {
|
||||
300
|
||||
}
|
||||
|
||||
impl RedisStore {
|
||||
pub fn new(url: &str) -> anyhow::Result<Self> {
|
||||
let client = redis::Client::open(url)?;
|
||||
|
|
@ -15,6 +31,140 @@ impl RedisStore {
|
|||
}
|
||||
}
|
||||
|
||||
const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
|
||||
const BACKOFF_MIN: Duration = Duration::from_secs(1);
|
||||
const BACKOFF_MAX: Duration = Duration::from_secs(30);
|
||||
|
||||
/// Capped exponential backoff step: `prev` doubled, clamped to `max`.
|
||||
fn next_backoff(prev: Duration, max: Duration) -> Duration {
|
||||
prev.saturating_mul(2).min(max)
|
||||
}
|
||||
|
||||
/// What the outer reconnect loop should do after one pubsub session.
|
||||
enum Flow {
|
||||
/// Shutdown requested — exit the task.
|
||||
Stop,
|
||||
/// Connection dropped or session failed — reconnect with backoff.
|
||||
Retry,
|
||||
}
|
||||
|
||||
/// Resolves only when shutdown is truly requested (value `true`) or every
|
||||
/// sender has been dropped; spurious non-shutdown notifications keep it pending
|
||||
/// so it never cancels in-flight connect/subscribe work.
|
||||
async fn wait_for_shutdown(shutdown: &mut watch::Receiver<bool>) {
|
||||
loop {
|
||||
if shutdown.changed().await.is_err() || *shutdown.borrow_and_update() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Blacklist pubsub subscriber with real reconnect: on connect/subscribe
|
||||
/// failure or stream end it retries with capped exponential backoff (reset to
|
||||
/// `BACKOFF_MIN` after each successfully applied message). Every await is
|
||||
/// raced against the shutdown watch so the task stays cancellable.
|
||||
pub async fn start_blacklist_sync(
|
||||
client: &redis::Client,
|
||||
blacklist: Arc<Blacklist>,
|
||||
mut shutdown: watch::Receiver<bool>,
|
||||
) {
|
||||
let mut backoff = BACKOFF_MIN;
|
||||
loop {
|
||||
match subscribe_once(client, &blacklist, &mut shutdown, &mut backoff).await {
|
||||
Flow::Stop => {
|
||||
tracing::info!("shutting down blacklist subscriber");
|
||||
return;
|
||||
},
|
||||
Flow::Retry => {},
|
||||
}
|
||||
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(backoff) => {},
|
||||
() = wait_for_shutdown(&mut shutdown) => {
|
||||
tracing::info!("shutting down blacklist subscriber");
|
||||
return;
|
||||
},
|
||||
}
|
||||
backoff = next_backoff(backoff, BACKOFF_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
/// One connect+subscribe+consume session. Sets `*backoff` to `BACKOFF_MIN`
|
||||
/// whenever a message is applied, proving the link is healthy. Each blocking
|
||||
/// await is raced against shutdown. (Connect+subscribe stay inline: the
|
||||
/// `PubSub` type produced by `into_pubsub` is awkward to name in a split
|
||||
/// helper's return type.)
|
||||
async fn subscribe_once(
|
||||
client: &redis::Client,
|
||||
blacklist: &Blacklist,
|
||||
shutdown: &mut watch::Receiver<bool>,
|
||||
backoff: &mut Duration,
|
||||
) -> Flow {
|
||||
#[allow(deprecated)]
|
||||
let conn = tokio::select! {
|
||||
connected = client.get_async_connection() => match connected {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
};
|
||||
|
||||
let mut pubsub = conn.into_pubsub();
|
||||
tokio::select! {
|
||||
subscribed = pubsub.subscribe(BLACKLIST_CHANNEL) => match subscribed {
|
||||
Ok(()) => tracing::info!("subscribed to {BLACKLIST_CHANNEL}"),
|
||||
Err(e) => {
|
||||
tracing::error!("failed to subscribe to blacklist events: {e}");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
}
|
||||
|
||||
let mut stream = pubsub.on_message();
|
||||
loop {
|
||||
tokio::select! {
|
||||
maybe_msg = stream.next() => match maybe_msg {
|
||||
Some(msg) => {
|
||||
if let Err(e) = handle_event(&msg, blacklist) {
|
||||
tracing::error!("blacklist event error: {e}");
|
||||
} else {
|
||||
*backoff = BACKOFF_MIN;
|
||||
}
|
||||
},
|
||||
None => {
|
||||
tracing::error!("blacklist pubsub stream ended; resubscribing");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_event(msg: &Msg, blacklist: &Blacklist) -> anyhow::Result<()> {
|
||||
let payload: String = msg.get_payload()?;
|
||||
let event: BlacklistEvent = serde_json::from_str(&payload)?;
|
||||
|
||||
let ip: std::net::IpAddr = event.ip.parse()?;
|
||||
|
||||
match event.action.as_str() {
|
||||
"ban" => {
|
||||
blacklist.add(ip, Duration::from_secs(event.duration_secs), "redis");
|
||||
tracing::info!("blacklist add via Redis: {}", event.ip);
|
||||
},
|
||||
"unban" => {
|
||||
blacklist.remove(ip);
|
||||
tracing::info!("blacklist remove via Redis: {}", event.ip);
|
||||
},
|
||||
a => anyhow::bail!("unknown action: {a}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl StateStore for RedisStore {
|
||||
async fn get(&self, key: &str) -> anyhow::Result<Option<String>> {
|
||||
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
||||
|
|
@ -39,3 +189,23 @@ impl StateStore for RedisStore {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn backoff_doubles_until_cap() {
|
||||
let mut b = BACKOFF_MIN;
|
||||
for expected_secs in [2, 4, 8, 16, 30, 30] {
|
||||
b = next_backoff(b, BACKOFF_MAX);
|
||||
assert_eq!(b, Duration::from_secs(expected_secs));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backoff_never_exceeds_max_even_with_huge_prev() {
|
||||
let b = next_backoff(Duration::from_secs(u64::MAX / 2), BACKOFF_MAX);
|
||||
assert!(b <= BACKOFF_MAX);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,137 +0,0 @@
|
|||
mod event;
|
||||
mod publisher;
|
||||
mod snapshot;
|
||||
|
||||
pub use publisher::start_local_ban_publisher;
|
||||
|
||||
use crate::filter::blacklist::Blacklist;
|
||||
use futures::StreamExt;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
|
||||
const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
|
||||
const BACKOFF_MIN: Duration = Duration::from_secs(1);
|
||||
const BACKOFF_MAX: Duration = Duration::from_secs(30);
|
||||
|
||||
fn next_backoff(prev: Duration, max: Duration) -> Duration {
|
||||
prev.saturating_mul(2).min(max)
|
||||
}
|
||||
|
||||
enum Flow {
|
||||
Stop,
|
||||
Retry,
|
||||
}
|
||||
|
||||
async fn wait_for_shutdown(shutdown: &mut watch::Receiver<bool>) {
|
||||
loop {
|
||||
if shutdown.changed().await.is_err() || *shutdown.borrow_and_update() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_blacklist_sync(
|
||||
client: &redis::Client,
|
||||
blacklist: Arc<Blacklist>,
|
||||
mut shutdown: watch::Receiver<bool>,
|
||||
) {
|
||||
let mut backoff = BACKOFF_MIN;
|
||||
loop {
|
||||
match subscribe_once(client, &blacklist, &mut shutdown, &mut backoff).await {
|
||||
Flow::Stop => {
|
||||
tracing::info!("shutting down blacklist subscriber");
|
||||
return;
|
||||
},
|
||||
Flow::Retry => {},
|
||||
}
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(backoff) => {},
|
||||
() = wait_for_shutdown(&mut shutdown) => {
|
||||
tracing::info!("shutting down blacklist subscriber");
|
||||
return;
|
||||
},
|
||||
}
|
||||
backoff = next_backoff(backoff, BACKOFF_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
async fn subscribe_once(
|
||||
client: &redis::Client,
|
||||
blacklist: &Blacklist,
|
||||
shutdown: &mut watch::Receiver<bool>,
|
||||
backoff: &mut Duration,
|
||||
) -> Flow {
|
||||
let mut pubsub = tokio::select! {
|
||||
connected = client.get_async_pubsub() => match connected {
|
||||
Ok(pubsub) => pubsub,
|
||||
Err(e) => {
|
||||
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
};
|
||||
|
||||
tokio::select! {
|
||||
subscribed = pubsub.subscribe(BLACKLIST_CHANNEL) => match subscribed {
|
||||
Ok(()) => tracing::info!("subscribed to {BLACKLIST_CHANNEL}"),
|
||||
Err(e) => {
|
||||
tracing::error!("failed to subscribe to blacklist events: {e}");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
}
|
||||
|
||||
tokio::select! {
|
||||
loaded = snapshot::reconcile(client, blacklist) => match loaded {
|
||||
Ok(()) => tracing::info!("blacklist snapshot reconciled from Redis"),
|
||||
Err(e) => {
|
||||
tracing::error!("failed to reconcile blacklist snapshot: {e}");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
}
|
||||
|
||||
let mut stream = pubsub.on_message();
|
||||
loop {
|
||||
tokio::select! {
|
||||
maybe_msg = stream.next() => match maybe_msg {
|
||||
Some(msg) => {
|
||||
if let Err(e) = event::handle(&msg, blacklist) {
|
||||
tracing::error!("blacklist event error: {e}");
|
||||
} else {
|
||||
*backoff = BACKOFF_MIN;
|
||||
}
|
||||
},
|
||||
None => {
|
||||
tracing::error!("blacklist pubsub stream ended; resubscribing");
|
||||
return Flow::Retry;
|
||||
},
|
||||
},
|
||||
() = wait_for_shutdown(shutdown) => return Flow::Stop,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn backoff_doubles_until_cap() {
|
||||
let mut b = BACKOFF_MIN;
|
||||
for expected_secs in [2, 4, 8, 16, 30, 30] {
|
||||
b = next_backoff(b, BACKOFF_MAX);
|
||||
assert_eq!(b, Duration::from_secs(expected_secs));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backoff_never_exceeds_max_even_with_huge_prev() {
|
||||
let b = next_backoff(Duration::from_secs(u64::MAX / 2), BACKOFF_MAX);
|
||||
assert!(b <= BACKOFF_MAX);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
use crate::filter::blacklist::Blacklist;
|
||||
use redis::Msg;
|
||||
use serde::Deserialize;
|
||||
use std::time::Duration;
|
||||
|
||||
const MAX_BAN_DURATION_SECS: u64 = 31_536_000;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct BlacklistEvent {
|
||||
ip: String,
|
||||
action: String,
|
||||
#[serde(default = "default_duration")]
|
||||
duration_secs: u64,
|
||||
#[serde(default)]
|
||||
expires_at_unix: Option<i64>,
|
||||
}
|
||||
|
||||
fn default_duration() -> u64 {
|
||||
300
|
||||
}
|
||||
|
||||
pub(super) fn handle(msg: &Msg, blacklist: &Blacklist) -> anyhow::Result<()> {
|
||||
let payload: String = msg.get_payload()?;
|
||||
let event: BlacklistEvent = serde_json::from_str(&payload)?;
|
||||
let ip: std::net::IpAddr = event.ip.parse()?;
|
||||
|
||||
match event.action.as_str() {
|
||||
"ban" => {
|
||||
let duration = match event.expires_at_unix {
|
||||
Some(expires_at) => {
|
||||
if chrono::DateTime::from_timestamp(expires_at, 0).is_none() {
|
||||
anyhow::bail!("invalid blacklist event expiry timestamp");
|
||||
}
|
||||
let remaining = expires_at.saturating_sub(chrono::Utc::now().timestamp());
|
||||
if remaining <= 0 {
|
||||
blacklist.remove_redis(ip);
|
||||
return Ok(());
|
||||
}
|
||||
let remaining = u64::try_from(remaining)?;
|
||||
if remaining > MAX_BAN_DURATION_SECS {
|
||||
anyhow::bail!("blacklist event expiry exceeds maximum duration");
|
||||
}
|
||||
Duration::from_secs(remaining)
|
||||
},
|
||||
None => Duration::from_secs(event.duration_secs),
|
||||
};
|
||||
blacklist.add_redis(ip, duration, "manager_event");
|
||||
tracing::info!("blacklist add via Redis: {}", event.ip);
|
||||
},
|
||||
"unban" => {
|
||||
blacklist.remove_redis(ip);
|
||||
tracing::info!("blacklist remove via Redis: {}", event.ip);
|
||||
},
|
||||
action => anyhow::bail!("unknown action: {action}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,119 +0,0 @@
|
|||
use crate::filter::blacklist::{Blacklist, LocalBanEvent};
|
||||
use serde::Serialize;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::{broadcast, watch};
|
||||
|
||||
const BLACKLIST_KEY: &str = "rampart:blacklist";
|
||||
const BLACKLIST_METADATA_KEY: &str = "rampart:blacklist:metadata";
|
||||
const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
|
||||
const STORE_BAN_SCRIPT: &str = "redis.call('HSET', KEYS[2], ARGV[1], ARGV[2]); redis.call('SADD', KEYS[1], ARGV[1]); redis.call('PUBLISH', KEYS[3], ARGV[3]); return 1";
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct BanMetadata<'a> {
|
||||
created_at_unix: i64,
|
||||
expires_at_unix: i64,
|
||||
reason: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct BanEvent<'a> {
|
||||
ip: String,
|
||||
action: &'static str,
|
||||
duration_secs: u64,
|
||||
expires_at_unix: i64,
|
||||
reason: &'a str,
|
||||
}
|
||||
|
||||
pub async fn start_local_ban_publisher(
|
||||
client: redis::Client,
|
||||
blacklist: std::sync::Arc<Blacklist>,
|
||||
mut events: broadcast::Receiver<LocalBanEvent>,
|
||||
mut shutdown: watch::Receiver<bool>,
|
||||
) {
|
||||
for event in blacklist.local_snapshot() {
|
||||
if !publish_with_retry(&client, event, &mut shutdown).await {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
received = events.recv() => match received {
|
||||
Ok(event) => {
|
||||
if !publish_with_retry(&client, event, &mut shutdown).await {
|
||||
return;
|
||||
}
|
||||
},
|
||||
Err(broadcast::error::RecvError::Lagged(skipped)) => {
|
||||
tracing::warn!(skipped, "local ban publisher lagged; reconciling active local bans");
|
||||
for event in blacklist.local_snapshot() {
|
||||
if !publish_with_retry(&client, event, &mut shutdown).await {
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(broadcast::error::RecvError::Closed) => return,
|
||||
},
|
||||
changed = shutdown.changed() => {
|
||||
if changed.is_err() || *shutdown.borrow_and_update() {
|
||||
return;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn publish_with_retry(
|
||||
client: &redis::Client,
|
||||
event: LocalBanEvent,
|
||||
shutdown: &mut watch::Receiver<bool>,
|
||||
) -> bool {
|
||||
let mut delay = Duration::from_secs(1);
|
||||
loop {
|
||||
match publish_ban(client, &event).await {
|
||||
Ok(()) => return true,
|
||||
Err(error) => tracing::warn!(%error, ip = %event.ip, "failed to persist local ban; retrying"),
|
||||
}
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(delay) => delay = (delay * 2).min(Duration::from_secs(30)),
|
||||
changed = shutdown.changed() => {
|
||||
if changed.is_err() || *shutdown.borrow_and_update() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn publish_ban(client: &redis::Client, event: &LocalBanEvent) -> anyhow::Result<()> {
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let remaining = event.expires_at_unix.saturating_sub(now);
|
||||
if remaining <= 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let metadata = serde_json::to_string(&BanMetadata {
|
||||
created_at_unix: now,
|
||||
expires_at_unix: event.expires_at_unix,
|
||||
reason: &event.reason,
|
||||
})?;
|
||||
let payload = serde_json::to_string(&BanEvent {
|
||||
ip: event.ip.to_string(),
|
||||
action: "ban",
|
||||
duration_secs: u64::try_from(remaining)?,
|
||||
expires_at_unix: event.expires_at_unix,
|
||||
reason: &event.reason,
|
||||
})?;
|
||||
let mut conn = client.get_multiplexed_async_connection().await?;
|
||||
let _: i64 = redis::cmd("EVAL")
|
||||
.arg(STORE_BAN_SCRIPT)
|
||||
.arg(3)
|
||||
.arg(BLACKLIST_KEY)
|
||||
.arg(BLACKLIST_METADATA_KEY)
|
||||
.arg(BLACKLIST_CHANNEL)
|
||||
.arg(event.ip.to_string())
|
||||
.arg(metadata)
|
||||
.arg(payload)
|
||||
.query_async(&mut conn)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
use crate::filter::blacklist::Blacklist;
|
||||
use redis::AsyncCommands;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
|
||||
const BLACKLIST_KEY: &str = "rampart:blacklist";
|
||||
const BLACKLIST_METADATA_KEY: &str = "rampart:blacklist:metadata";
|
||||
const LEGACY_BAN_DURATION_SECS: i64 = 300;
|
||||
const MAX_BAN_DURATION_SECS: i64 = 31_536_000;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct BlacklistMetadata {
|
||||
created_at_unix: i64,
|
||||
expires_at_unix: i64,
|
||||
reason: String,
|
||||
}
|
||||
|
||||
/// Restore current durable bans after subscribing to the event channel.
|
||||
pub(super) async fn reconcile(client: &redis::Client, blacklist: &Blacklist) -> anyhow::Result<()> {
|
||||
let mut conn = client.get_multiplexed_async_connection().await?;
|
||||
let members: Vec<String> = redis::cmd("SMEMBERS").arg(BLACKLIST_KEY).query_async(&mut conn).await?;
|
||||
let metadata: HashMap<String, String> = conn.hgetall(BLACKLIST_METADATA_KEY).await?;
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let mut active = Vec::with_capacity(members.len());
|
||||
|
||||
for target in members {
|
||||
let ip: std::net::IpAddr = match target.parse() {
|
||||
Ok(ip) => ip,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let record = match metadata.get(&target) {
|
||||
Some(encoded) => match serde_json::from_str::<BlacklistMetadata>(encoded) {
|
||||
Ok(record) => record,
|
||||
Err(_) => continue,
|
||||
},
|
||||
None => match legacy_record(&mut conn, &target, now).await? {
|
||||
Some(record) => record,
|
||||
None => continue,
|
||||
},
|
||||
};
|
||||
if record.expires_at_unix <= now
|
||||
|| chrono::DateTime::from_timestamp(record.created_at_unix, 0).is_none()
|
||||
|| chrono::DateTime::from_timestamp(record.expires_at_unix, 0).is_none()
|
||||
|| record.expires_at_unix.saturating_sub(now) > MAX_BAN_DURATION_SECS
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let remaining = u64::try_from(record.expires_at_unix - now)?;
|
||||
active.push((ip, Duration::from_secs(remaining), record.reason));
|
||||
}
|
||||
blacklist.replace_redis(active);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn legacy_record(
|
||||
conn: &mut redis::aio::MultiplexedConnection,
|
||||
target: &str,
|
||||
now: i64,
|
||||
) -> anyhow::Result<Option<BlacklistMetadata>> {
|
||||
let legacy = BlacklistMetadata {
|
||||
created_at_unix: now,
|
||||
expires_at_unix: now.saturating_add(LEGACY_BAN_DURATION_SECS),
|
||||
reason: "legacy_unverified".to_string(),
|
||||
};
|
||||
let inserted: i64 = redis::cmd("HSETNX")
|
||||
.arg(BLACKLIST_METADATA_KEY)
|
||||
.arg(target)
|
||||
.arg(serde_json::to_string(&legacy)?)
|
||||
.query_async(conn)
|
||||
.await?;
|
||||
if inserted == 1 {
|
||||
return Ok(Some(legacy));
|
||||
}
|
||||
let current: Option<String> = conn.hget(BLACKLIST_METADATA_KEY, target).await?;
|
||||
Ok(current.and_then(|value| serde_json::from_str(&value).ok()))
|
||||
}
|
||||
|
|
@ -1,8 +1,3 @@
|
|||
use super::XdpFilter;
|
||||
use anyhow::{Result, bail};
|
||||
use libbpf_rs::{MapCore, MapFlags};
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
pub(super) fn unix_ns() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
|
|
@ -25,35 +20,6 @@ pub(super) fn blacklist_expiry_ns(now_ns: u64, duration_secs: u64) -> u64 {
|
|||
now_ns.saturating_add(duration_secs.saturating_mul(1_000_000_000))
|
||||
}
|
||||
|
||||
impl XdpFilter {
|
||||
pub fn ban_ip(&self, ip: Ipv4Addr, duration_secs: u64) -> Result<()> {
|
||||
self.blacklist_update(32, &ip.octets(), duration_secs)
|
||||
}
|
||||
|
||||
/// Adds a CIDR ban to the LPM trie. IPv6 enforcement is not supported yet.
|
||||
pub fn ban_cidr(&self, prefix: IpAddr, prefix_len: u8, duration_secs: u64) -> Result<()> {
|
||||
let IpAddr::V4(net) = prefix else {
|
||||
bail!("ipv6 cidr bans not supported yet");
|
||||
};
|
||||
self.blacklist_update(prefix_len, &net.octets(), duration_secs)
|
||||
}
|
||||
|
||||
fn blacklist_update(&self, prefix_len: u8, v4: &[u8; 4], duration_secs: u64) -> Result<()> {
|
||||
let map = self.find_map("blacklist_map")?;
|
||||
let key = blacklist_key(prefix_len, v4);
|
||||
let expiry_ns = blacklist_expiry_ns(unix_ns(), duration_secs);
|
||||
map.update(&key, &expiry_ns.to_le_bytes(), MapFlags::ANY)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unban_ip(&self, ip: Ipv4Addr) -> Result<()> {
|
||||
let map = self.find_map("blacklist_map")?;
|
||||
let key = blacklist_key(32, &ip.octets());
|
||||
map.delete(&key)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use anyhow::{Context, Result, bail};
|
||||
use libbpf_rs::{MapCore, MapFlags, Object, ObjectBuilder, RingBuffer, Xdp, XdpFlags};
|
||||
use std::net::IpAddr;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::os::unix::io::AsFd;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
|
@ -11,6 +13,7 @@ use crate::traffic::prefix::{PrefixKey, PrefixStatsVal};
|
|||
|
||||
use attach::{build_ringbuf, interface_cstr, patch_rodata, xdp_attach_mode_name};
|
||||
use events::AutoBanConfig;
|
||||
use maps::{blacklist_expiry_ns, blacklist_key, unix_ns};
|
||||
|
||||
mod attach;
|
||||
pub(crate) mod events;
|
||||
|
|
@ -165,6 +168,36 @@ impl XdpFilter {
|
|||
.with_context(|| format!("map '{}' not found", name))
|
||||
}
|
||||
|
||||
pub fn ban_ip(&self, ip: Ipv4Addr, duration_secs: u64) -> Result<()> {
|
||||
self.blacklist_update(32, &ip.octets(), duration_secs)
|
||||
}
|
||||
|
||||
/// Бан CIDR через `blacklist_map` (LPM trie): длина префикса в key[0].
|
||||
///
|
||||
/// # Errors
|
||||
/// XDP не загружен, IPv6-префиксы (ядро их пока не банит) или ошибка карты.
|
||||
pub fn ban_cidr(&self, prefix: IpAddr, prefix_len: u8, duration_secs: u64) -> Result<()> {
|
||||
let IpAddr::V4(net) = prefix else {
|
||||
bail!("ipv6 cidr bans not supported yet");
|
||||
};
|
||||
self.blacklist_update(prefix_len, &net.octets(), duration_secs)
|
||||
}
|
||||
|
||||
fn blacklist_update(&self, prefix_len: u8, v4: &[u8; 4], duration_secs: u64) -> Result<()> {
|
||||
let map = self.find_map("blacklist_map")?;
|
||||
let key = blacklist_key(prefix_len, v4);
|
||||
let expiry_ns = blacklist_expiry_ns(unix_ns(), duration_secs);
|
||||
map.update(&key, &expiry_ns.to_le_bytes(), MapFlags::ANY)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unban_ip(&self, ip: Ipv4Addr) -> Result<()> {
|
||||
let map = self.find_map("blacklist_map")?;
|
||||
let key = blacklist_key(32, &ip.octets());
|
||||
map.delete(&key)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Читает карту `prefix_stats` (агрегаты по префиксам /24 и /64).
|
||||
///
|
||||
/// # Errors
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use rampart::protocol::{HandshakeResult, ProtocolHandler, ProtocolRegistry, Upstream};
|
||||
use rampart::protocol::{ProtocolHandler, ProtocolRegistry, Upstream};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
struct NoopHandler;
|
||||
|
|
@ -14,8 +14,8 @@ impl ProtocolHandler for NoopHandler {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
_stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(HandshakeResult::new(Upstream::new("127.0.0.1:9000"))) })
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(Upstream::new("127.0.0.1:9000")) })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue