feat: add Minecraft proxy and refresh dependencies
This commit is contained in:
parent
a4d0880a0a
commit
cfaad47948
47 changed files with 2671 additions and 1005 deletions
1196
Cargo.lock
generated
1196
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
21
Cargo.toml
21
Cargo.toml
|
|
@ -40,6 +40,7 @@ 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"] }
|
||||
|
|
@ -51,14 +52,14 @@ thiserror = "2"
|
|||
anyhow = "1"
|
||||
dashmap = "6"
|
||||
hex = "0.4"
|
||||
sha2 = "0.10"
|
||||
sha2 = "0.11"
|
||||
subtle = "2"
|
||||
socket2 = { version = "0.5", features = ["all"] }
|
||||
socket2 = { version = "0.6", features = ["all"] }
|
||||
futures = "0.3"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
reqwest = { version = "0.13", default-features = false, features = ["json", "query", "rustls"] }
|
||||
prometheus = { version = "0.14", features = ["process"] }
|
||||
toml = "0.8"
|
||||
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }
|
||||
toml = "1.1"
|
||||
rand = { version = "0.10", default-features = false, features = ["std", "std_rng", "thread_rng"] }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
|
|
@ -67,11 +68,11 @@ ratatui = "0.30"
|
|||
crossterm = "0.29"
|
||||
|
||||
axum = "0.8"
|
||||
tower-http = { version = "0.6", features = ["cors"] }
|
||||
jsonwebtoken = "9"
|
||||
tower-http = { version = "0.7", features = ["cors"] }
|
||||
jsonwebtoken = "11"
|
||||
|
||||
redis = { version = "0.27", optional = true, features = ["tokio-comp", "connection-manager"] }
|
||||
maxminddb = { version = "0.30", optional = true }
|
||||
redis = { version = "1.7", optional = true, features = ["tokio-comp", "connection-manager"] }
|
||||
maxminddb = { version = "0.32", optional = true }
|
||||
tokio-splice = { version = "0.2", optional = true }
|
||||
libbpf-rs = { version = "0.24", optional = true }
|
||||
libbpf-rs = { version = "0.27", 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 (feature protocol-http) · grpc (planned) │
|
||||
│ http (protocol-http) · Minecraft Java (protocol-minecraft) · grpc (planned) │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
Traffic Intel (EWMA thresholds, profiling, reputation)
|
||||
runs across all layers
|
||||
|
|
@ -75,6 +75,7 @@ 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 |
|
||||
|
||||
|
|
@ -93,17 +94,26 @@ Full benchmark suite in progress.
|
|||
### Quick Start
|
||||
|
||||
```bash
|
||||
# Build (the HTTP protocol handler is a feature)
|
||||
# Build the HTTP edge handler
|
||||
cargo build --release --features protocol-http
|
||||
|
||||
# Install the default config
|
||||
sudo mkdir -p /etc/rampart
|
||||
sudo cp deploy/config/edge.toml /etc/rampart/config.toml
|
||||
sudo cp deploy/config/edge-http.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 |
|
||||
|
|
@ -113,6 +123,7 @@ RAMPART_CONFIG=/etc/rampart/config.toml ./target/release/rampart
|
|||
| [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 |
|
||||
|
||||
|
|
@ -153,7 +164,7 @@ Rampart фильтрует трафик на трёх уровнях до тог
|
|||
│ L7 handshake analysis · rate limit · HMAC · death-code паттерны │
|
||||
├────────────────────────────────────────────────────────────────────┤
|
||||
│ Слой 4: Протокол-плагины (feature crates) │
|
||||
│ http (feature protocol-http) · grpc (в планах) │
|
||||
│ http (protocol-http) · Minecraft Java (protocol-minecraft) · grpc (в планах) │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
Traffic Intel (EWMA thresholds, профилирование, репутация)
|
||||
работает поперёк всех слоёв
|
||||
|
|
@ -174,6 +185,7 @@ 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 |
|
||||
|
||||
|
|
@ -192,17 +204,27 @@ Rampart фильтрует трафик на трёх уровнях до тог
|
|||
### Быстрый старт
|
||||
|
||||
```bash
|
||||
# Сборка (HTTP-обработчик собирается фичей)
|
||||
# Сборка HTTP-обработчика
|
||||
cargo build --release --features protocol-http
|
||||
|
||||
# Установка дефолтного конфига
|
||||
sudo mkdir -p /etc/rampart
|
||||
sudo cp deploy/config/edge.toml /etc/rampart/config.toml
|
||||
sudo cp deploy/config/edge-http.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.
|
||||
|
||||
### Документация
|
||||
|
||||
| Путь | Описание |
|
||||
|
|
@ -212,6 +234,7 @@ RAMPART_CONFIG=/etc/rampart/config.toml ./target/release/rampart
|
|||
| [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,6 +98,8 @@ 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()`).
|
||||
|
||||
|
|
@ -149,6 +151,10 @@ guard/
|
|||
bin-файлы тонкие (rampart.rs: 330 → 6 строк).
|
||||
|
||||
### Новые найденные проблемы (чинить в v0.4)
|
||||
- [ ] Redis blacklist: сохранять срок/причину бана и восстанавливать edge-состояние после reconnect;
|
||||
Pub/Sub сейчас только для подключённых узлов.
|
||||
- [ ] Minecraft Java: Velocity modern forwarding, игровые netns smoke-тесты и нагрузочная проверка.
|
||||
- [ ] Принимать client IP от внешнего DDoS-прокси через доверенный inbound PROXY protocol.
|
||||
- [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` гейтом.
|
||||
|
|
|
|||
56
deploy/config/edge-docker.toml
Normal file
56
deploy/config/edge-docker.toml
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# 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
|
||||
42
deploy/config/edge-http.toml
Normal file
42
deploy/config/edge-http.toml
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# 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,13 +1,23 @@
|
|||
# Rampart edge — единый конфиг (схема: src/config/sections.rs)
|
||||
# Rampart edge для Minecraft Java (схема: src/config/sections/)
|
||||
# whitelist — корневое поле, поэтому оно задаётся до любых TOML-секций.
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
|
||||
[bind]
|
||||
address = "0.0.0.0"
|
||||
port = 25565
|
||||
|
||||
[backend]
|
||||
# Generic TCP-бэкенды за edge-нодой (addr:port)
|
||||
# Адрес Paper за доверенным прокси; закрой этот порт firewall-ом от интернета.
|
||||
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
|
||||
|
||||
|
|
@ -41,4 +51,10 @@ port = 9090
|
|||
enabled = false
|
||||
difficulty = 4
|
||||
|
||||
whitelist = ["127.0.0.1", "::1"]
|
||||
# Inbound: включай только если входящий TCP-прокси передаёт PROXY v2.
|
||||
# Укажи точные адреса/CIDR этого прокси; не добавляй сюда адреса игроков.
|
||||
[proxy_protocol]
|
||||
ingress_enabled = false
|
||||
trusted_cidrs = []
|
||||
ingress_timeout_ms = 750
|
||||
max_header_bytes = 512
|
||||
|
|
|
|||
|
|
@ -44,11 +44,13 @@ services:
|
|||
dockerfile: deploy/docker/Dockerfile.edge
|
||||
ports:
|
||||
- "25565:25565"
|
||||
- "9090:9090"
|
||||
- "127.0.0.1:9090:9090"
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
environment:
|
||||
RAMPART_CONFIG: /etc/rampart/config.toml
|
||||
volumes:
|
||||
- ./config/edge.toml:/etc/rampart/config.toml:ro
|
||||
- ./config/edge-docker.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 --bin rampart && \
|
||||
RUN cargo build --release --features xdp,protocol-minecraft --bin rampart && \
|
||||
cp target/release/rampart /app/rampart && \
|
||||
strip /app/rampart
|
||||
|
||||
|
|
|
|||
28
docs/api.md
28
docs/api.md
|
|
@ -56,7 +56,7 @@ Authorization: Bearer <token>
|
|||
}
|
||||
```
|
||||
|
||||
Redis недоступен → пустой список.
|
||||
Если Redis недоступен, API возвращает `503 Service Unavailable`.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -68,13 +68,16 @@ Redis недоступен → пустой список.
|
|||
{
|
||||
"items": [
|
||||
{"target": "1.2.3.4", "type": "ip", "reason": "manual_ban",
|
||||
"created_at": "...", "expires_at": null}
|
||||
"created_at": "2026-10-16T12:00:00+00:00", "expires_at": "2026-10-16T13:00:00+00:00"}
|
||||
],
|
||||
"total": 42
|
||||
}
|
||||
```
|
||||
|
||||
Источник: Redis set `rampart:blacklist`. Пагинации и фильтров нет.
|
||||
Источник: Redis set `rampart:blacklist` и hash `rampart:blacklist:metadata`.
|
||||
Метаданные содержат причину, время создания и абсолютный срок истечения; API
|
||||
скрывает истёкшие записи. Старые set-записи без метаданных получают один
|
||||
ограниченный срок в пять минут при первом чтении или восстановлении edge.
|
||||
|
||||
### `POST /api/v1/blacklist` 🔒
|
||||
|
||||
|
|
@ -82,10 +85,24 @@ Redis недоступен → пустой список.
|
|||
// Request
|
||||
{"target": "1.2.3.4", "type": "ip", "reason": "manual_ban", "duration_secs": 3600}
|
||||
|
||||
// Response 200/201 — запись добавлена в Redis set
|
||||
// Response 200 — запись и метаданные атомарно сохранены в Redis, а событие
|
||||
// с duration_secs и expires_at_unix опубликовано в rampart:blacklist:events.
|
||||
```
|
||||
|
||||
Edge-ноды подхватывают ban при следующей синхронизации блэклиста.
|
||||
Edge подписывается на Pub/Sub перед загрузкой снимка из Redis. Поэтому события,
|
||||
пришедшие во время загрузки, будут применены после снимка. При старте и после
|
||||
переподключения активные Redis-баны восстанавливаются с оставшимся TTL; новые
|
||||
события используют абсолютный срок и игнорируются, если он уже прошёл. Старый
|
||||
формат события только с `duration_secs` продолжает поддерживаться.
|
||||
|
||||
### `DELETE /api/v1/blacklist` 🔒
|
||||
|
||||
```json
|
||||
{"target": "1.2.3.4"}
|
||||
```
|
||||
|
||||
Удаляет только Redis-бан и его метаданные, затем публикует событие unban.
|
||||
Локальные баны детекторов хранятся отдельно и этим запросом не снимаются.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -110,7 +127,6 @@ Edge-ноды подхватывают ban при следующей синхр
|
|||
|
||||
Честно, на текущий момент в API **нет**:
|
||||
|
||||
- удаления записей blacklist (только add/list);
|
||||
- drain нод через API (есть только CLI `rampart-cli drain`);
|
||||
- webhooks, metrics summary, challenge management;
|
||||
- регистрации нод через `POST /api/v1/nodes`;
|
||||
|
|
|
|||
|
|
@ -6,14 +6,23 @@
|
|||
## 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). Обязателен непустой, адреса валидируются.
|
||||
# Список TCP-бэкендов (addr:port). Для Minecraft нужен ровно один backend.
|
||||
upstreams = ["127.0.0.1:25566"]
|
||||
|
||||
[protocol.minecraft]
|
||||
enabled = true
|
||||
max_frame_bytes = 8192
|
||||
timeout_secs = 5
|
||||
send_proxy_protocol = false
|
||||
|
||||
[workers]
|
||||
count = 4
|
||||
|
||||
|
|
@ -47,10 +56,24 @@ port = 9090
|
|||
enabled = false # PoW challenge, OFF по умолчанию
|
||||
difficulty = 4
|
||||
|
||||
whitelist = ["127.0.0.1", "::1"] # только IP-адреса, валидируются при парсинге
|
||||
# Входящий PROXY v2 от известного TCP DDoS-прокси; включай только если
|
||||
# прокси действительно отправляет этот заголовок.
|
||||
[proxy_protocol]
|
||||
ingress_enabled = false
|
||||
trusted_cidrs = []
|
||||
ingress_timeout_ms = 750
|
||||
max_header_bytes = 512
|
||||
|
||||
```
|
||||
|
||||
Этот пример выбирает 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 (переменные окружения)
|
||||
|
||||
|
|
@ -73,7 +96,8 @@ Manager всегда слушает `0.0.0.0:8080`.
|
|||
| `geoip` | ❌ | GeoIP lookup (maxminddb) |
|
||||
| `xdp` | ❌ | Загрузка XDP-программы через libbpf-rs |
|
||||
| `io-uring` | ❌ | splice/io_uring I/O |
|
||||
| `protocol-http` | ❌ | Резерв под HTTP-плагин (пока пустой) |
|
||||
| `protocol-http` | ❌ | HTTP/1.1 handler; включай только для HTTP upstream |
|
||||
| `protocol-minecraft` | ❌ | Minecraft Java TCP handshake и проксирование |
|
||||
|
||||
Пример: `cargo build --release --features xdp --bin rampart`.
|
||||
|
||||
|
|
@ -87,4 +111,4 @@ clang -O2 -g -target bpf -c xdp/core/universal_filter.c -o /tmp/universal_filter
|
|||
|
||||
---
|
||||
|
||||
*Версия: 2.0 | Август 2026*
|
||||
*Версия: 2.1 | Октябрь 2026*
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@
|
|||
## 3. Быстрый старт — Edge
|
||||
|
||||
```bash
|
||||
# Сборка
|
||||
# Сборка edge для Minecraft Java
|
||||
git clone https://github.com/loki5512344/rampart.git && cd rampart
|
||||
cargo build --release --bin rampart
|
||||
cargo build --release --features protocol-minecraft --bin rampart
|
||||
|
||||
# Установка
|
||||
sudo cp target/release/rampart /usr/local/bin/
|
||||
|
|
@ -64,10 +64,10 @@ systemctl enable --now rampart
|
|||
journalctl -u rampart -f
|
||||
```
|
||||
|
||||
> ⚠️ В ядре нет ни одной реализации протокольного обработчика: реестр протоколов
|
||||
> пуст, и `rampart` при старте требует feature `protocol-http` или внешний
|
||||
> ProtocolHandler (см. src/bin/rampart.rs). До появления плагинов edge-нода
|
||||
> запускается только в исследовательских целях.
|
||||
Minecraft-конфиг рассчитан на один Paper backend. Если включён
|
||||
`protocol.minecraft.send_proxy_protocol`, включи `proxies.proxy-protocol: true` в
|
||||
Paper и закрой backend-порт firewall-ом от всех, кроме Rampart. Для подробной
|
||||
схемы с домашним сервером и защищённым VDS см. [Minecraft Java guide](minecraft-java.md).
|
||||
|
||||
## 4. Быстрый старт — Manager
|
||||
|
||||
|
|
@ -107,4 +107,4 @@ docker compose -f deploy/docker-compose.yml up -d
|
|||
|
||||
---
|
||||
|
||||
*Версия: 2.0 | Август 2026*
|
||||
*Версия: 2.1 | Октябрь 2026*
|
||||
|
|
|
|||
95
docs/minecraft-java.md
Normal file
95
docs/minecraft-java.md
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
# 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.
|
||||
|
||||
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.
|
||||
|
|
@ -19,6 +19,15 @@ 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();
|
||||
|
|
@ -60,10 +69,17 @@ 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);
|
||||
// Порядок load-bearing: `load()` захватывает blacklist и политику
|
||||
// авто-бана в ringbuf-колбэк (XdpFilter::load -> build_ringbuf),
|
||||
// поэтому set_autoban обязан отработать ДО load(), а не после.
|
||||
// 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"
|
||||
);
|
||||
}
|
||||
guard.load()?;
|
||||
}
|
||||
let xdp_metrics = XdpMetrics::register()?;
|
||||
|
|
|
|||
|
|
@ -69,10 +69,12 @@ 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),
|
||||
get(api::blacklist::list_blacklist)
|
||||
.post(api::blacklist::add_blacklist)
|
||||
.delete(api::blacklist::remove_blacklist),
|
||||
)
|
||||
.route("/api/v1/nodes", get(api::inventory::nodes::list_nodes));
|
||||
let protected = protected.route_layer(middleware::from_fn(auth::auth_middleware));
|
||||
let protected = protected.route_layer(middleware::from_fn_with_state(state.clone(), 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,4 +1,5 @@
|
|||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AddResponse {
|
||||
|
|
@ -18,8 +19,33 @@ struct BlacklistResponse {
|
|||
total: usize,
|
||||
}
|
||||
|
||||
pub async fn add(target: String, reason: Option<String>) -> anyhow::Result<()> {
|
||||
#[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 body = serde_json::json!({
|
||||
"target": target,
|
||||
|
|
@ -27,43 +53,47 @@ pub async fn add(target: String, reason: Option<String>) -> anyhow::Result<()> {
|
|||
"reason": reason.unwrap_or_else(|| "manual".to_string()),
|
||||
});
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
match client
|
||||
let resp = client
|
||||
.post(format!("{manager_url}/api/v1/blacklist"))
|
||||
.bearer_auth(token)
|
||||
.json(&body)
|
||||
.send()
|
||||
.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}"),
|
||||
}
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<AddResponse>()
|
||||
.await?;
|
||||
println!("[OK] {}: {}", resp.status, resp.target);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove(target: String) -> anyhow::Result<()> {
|
||||
println!("Removing {target} from blacklist...");
|
||||
println!("(not implemented in v0.1)");
|
||||
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);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn list() -> anyhow::Result<()> {
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => println!("[FAIL] {e}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,17 @@
|
|||
//! Единый конфиг платформы 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, StoreConfig, WorkerConfig, XdpConfig,
|
||||
LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, ProxyProtocolConfig, StoreConfig, TrustedProxyCidr,
|
||||
WorkerConfig, XdpConfig,
|
||||
};
|
||||
#[cfg(feature = "protocol-http")]
|
||||
pub use sections::{HttpProtocolConfig, ProtocolConfig};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
|
|
@ -22,6 +27,8 @@ pub struct Config {
|
|||
#[serde(default)]
|
||||
pub limits: LimitsConfig,
|
||||
#[serde(default)]
|
||||
pub proxy_protocol: ProxyProtocolConfig,
|
||||
#[serde(default)]
|
||||
pub ban: BanConfig,
|
||||
#[serde(default)]
|
||||
pub store: StoreConfig,
|
||||
|
|
@ -35,7 +42,7 @@ pub struct Config {
|
|||
pub pow: PowConfig,
|
||||
#[serde(default)]
|
||||
pub detect: DetectConfig,
|
||||
#[cfg(feature = "protocol-http")]
|
||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||
#[serde(default)]
|
||||
pub protocol: ProtocolConfig,
|
||||
#[serde(default)]
|
||||
|
|
@ -80,6 +87,26 @@ 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 {
|
||||
|
|
@ -89,6 +116,21 @@ 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,11 +78,55 @@ fn default_prefix_min_unique_sources() -> u64 {
|
|||
}
|
||||
|
||||
/// Секция `[protocol]`: протокольные обработчики (plugin-by-feature).
|
||||
#[cfg(feature = "protocol-http")]
|
||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||
#[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,4 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct BindConfig {
|
||||
|
|
@ -133,3 +135,110 @@ 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,8 +4,14 @@ mod detect;
|
|||
mod edge;
|
||||
mod platform;
|
||||
|
||||
pub use detect::{DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig};
|
||||
#[cfg(feature = "protocol-http")]
|
||||
pub use detect::{HttpProtocolConfig, ProtocolConfig};
|
||||
pub use edge::{BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, WorkerConfig};
|
||||
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,
|
||||
};
|
||||
pub use platform::{LoggingConfig, MetricsConfig, StoreConfig, XdpConfig};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use rand::RngCore;
|
||||
use rand::Rng;
|
||||
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::thread_rng().fill_bytes(&mut token);
|
||||
rand::rng().fill_bytes(&mut token);
|
||||
Self {
|
||||
token,
|
||||
created_at: Instant::now(),
|
||||
|
|
|
|||
|
|
@ -77,10 +77,13 @@ async fn accept_loop(
|
|||
tokio::spawn(async move {
|
||||
// Hot path Traffic Intelligence: только атомики и DashMap.
|
||||
hook.on_accept();
|
||||
if let Err(e) = gateway.handle(stream, peer_addr).await {
|
||||
let outcome = gateway.handle(stream, peer_addr).await;
|
||||
if let Err(e) = outcome.result {
|
||||
tracing::debug!("connection from {peer_addr}: {e}");
|
||||
}
|
||||
hook.on_connection_end(peer_addr.ip());
|
||||
if let Some(client_ip) = outcome.client_ip {
|
||||
hook.on_connection_end(client_ip);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,30 @@
|
|||
//! Generic TCP proxy: фильтры входа → protocol handshake → upstream.
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::engine::challenge::{DifficultyAdjuster, enforce as enforce_pow};
|
||||
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::store::clickhouse::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, Ordering};
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize};
|
||||
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>>,
|
||||
|
|
@ -35,166 +37,9 @@ pub struct Gateway {
|
|||
upstream_cursor: AtomicUsize,
|
||||
}
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
/// 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<()>,
|
||||
}
|
||||
|
|
|
|||
230
src/engine/tunnel/gateway.rs
Normal file
230
src/engine/tunnel/gateway.rs
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/engine/tunnel/gateway/challenge.rs
Normal file
34
src/engine/tunnel/gateway/challenge.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
40
src/engine/tunnel/proxy.rs
Normal file
40
src/engine/tunnel/proxy.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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)
|
||||
}
|
||||
70
src/engine/tunnel/proxy/inbound.rs
Normal file
70
src/engine/tunnel/proxy/inbound.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
//! 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}"),
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ struct BanEntry {
|
|||
|
||||
pub struct Blacklist {
|
||||
entries: Arc<DashMap<IpAddr, BanEntry>>,
|
||||
redis_entries: Arc<DashMap<IpAddr, BanEntry>>,
|
||||
}
|
||||
|
||||
impl Default for Blacklist {
|
||||
|
|
@ -22,18 +23,12 @@ impl Blacklist {
|
|||
pub fn new() -> Self {
|
||||
Self {
|
||||
entries: Arc::new(DashMap::new()),
|
||||
redis_entries: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_blocked(&self, ip: IpAddr) -> bool {
|
||||
if let Some(entry) = self.entries.get(&ip) {
|
||||
if entry.expires > Instant::now() {
|
||||
return true;
|
||||
}
|
||||
drop(entry);
|
||||
self.entries.remove(&ip);
|
||||
}
|
||||
false
|
||||
Self::is_blocked_in(&self.entries, ip) || Self::is_blocked_in(&self.redis_entries, ip)
|
||||
}
|
||||
|
||||
pub fn add(&self, ip: IpAddr, duration: Duration, reason: &str) {
|
||||
|
|
@ -50,16 +45,53 @@ impl Blacklist {
|
|||
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.entries.len() + self.redis_entries.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.entries.is_empty()
|
||||
self.entries.is_empty() && self.redis_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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
59
src/filter/connection_limit/mod.rs
Normal file
59
src/filter/connection_limit/mod.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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,5 +1,6 @@
|
|||
//! Фильтры входящего трафика.
|
||||
|
||||
pub mod blacklist;
|
||||
pub mod connection_limit;
|
||||
pub mod geo;
|
||||
pub mod rate_limit;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,4 @@
|
|||
#[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 {
|
||||
|
|
@ -32,68 +26,19 @@ pub struct AddBlacklistRequest {
|
|||
pub duration_secs: Option<u64>,
|
||||
}
|
||||
|
||||
#[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 })
|
||||
#[derive(Deserialize)]
|
||||
pub struct RemoveBlacklistRequest {
|
||||
pub target: String,
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
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"})),
|
||||
};
|
||||
mod common;
|
||||
#[cfg(feature = "store-redis")]
|
||||
mod list;
|
||||
#[cfg(feature = "store-redis")]
|
||||
mod mutate;
|
||||
|
||||
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
|
||||
}))
|
||||
}
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub use list::list_blacklist;
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub use mutate::{add_blacklist, remove_blacklist};
|
||||
|
|
|
|||
75
src/manager/api/blacklist/common.rs
Normal file
75
src/manager/api/blacklist/common.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
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")
|
||||
}
|
||||
79
src/manager/api/blacklist/list.rs
Normal file
79
src/manager/api/blacklist/list.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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 }))
|
||||
}
|
||||
98
src/manager/api/blacklist/mutate.rs
Normal file
98
src/manager/api/blacklist/mutate.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
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,5 +1,6 @@
|
|||
use axum::Json;
|
||||
use axum::body::Body;
|
||||
use axum::extract::State;
|
||||
use axum::http::{Request, StatusCode};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::Response;
|
||||
|
|
@ -36,6 +37,7 @@ 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>)> {
|
||||
|
|
@ -55,16 +57,6 @@ 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 для туннелирования.
|
||||
//! Голова читается через `peek`, чтобы байты остались в сокете и движок
|
||||
//! проксировал запрос на backend без потерь.
|
||||
//! Прочитанная голова передаётся upstream как начальные байты туннеля.
|
||||
|
||||
mod parser;
|
||||
|
||||
use crate::config::HttpProtocolConfig;
|
||||
use crate::protocol::{ProtocolHandler, Upstream};
|
||||
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;
|
||||
|
||||
/// Обработчик HTTP/1.1: verdict Allow → tunnel на upstream, Reject → drop.
|
||||
|
|
@ -37,31 +37,27 @@ impl HttpProtocolHandler {
|
|||
}
|
||||
}
|
||||
|
||||
/// Накапливает request-head через `peek` до терминатора или лимита.
|
||||
/// Накапливает request-head без повторного чтения одного и того же префикса.
|
||||
///
|
||||
/// # Errors
|
||||
/// Таймаут, закрытие клиента, превышение `max_header_bytes` или ошибка I/O.
|
||||
async fn read_head(&self, stream: &TcpStream) -> anyhow::Result<Vec<u8>> {
|
||||
async fn read_head(&self, stream: &mut 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![0u8; limit];
|
||||
let mut len = 0usize;
|
||||
let mut buf = Vec::with_capacity(limit);
|
||||
let accumulate = async {
|
||||
loop {
|
||||
if let Some(head_len) = parser::scan_head(&buf[..len]) {
|
||||
buf.truncate(head_len);
|
||||
if parser::scan_head(&buf).is_some() {
|
||||
return Ok(buf);
|
||||
}
|
||||
if len >= limit {
|
||||
if buf.len() >= limit {
|
||||
anyhow::bail!("request head exceeds {limit} bytes");
|
||||
}
|
||||
let n = stream.peek(&mut buf[len..]).await?;
|
||||
if n == 0 {
|
||||
anyhow::bail!("client closed connection before head was complete");
|
||||
}
|
||||
len += n;
|
||||
let mut byte = [0u8; 1];
|
||||
stream.read_exact(&mut byte).await?;
|
||||
buf.push(byte[0]);
|
||||
}
|
||||
};
|
||||
match tokio::time::timeout(self.timeout, accumulate).await {
|
||||
|
|
@ -79,11 +75,14 @@ impl ProtocolHandler for HttpProtocolHandler {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>> {
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
let head = self.read_head(stream).await?;
|
||||
match parser::evaluate(&head, &self.policy) {
|
||||
parser::Verdict::Allow { .. } => Ok(Upstream::new(self.upstream.clone())),
|
||||
parser::Verdict::Allow { .. } => Ok(HandshakeResult::with_initial_data(
|
||||
Upstream::new(self.upstream.clone()),
|
||||
head,
|
||||
)),
|
||||
parser::Verdict::Reject { reason } => Err(anyhow::anyhow!("http: {reason}")),
|
||||
}
|
||||
})
|
||||
|
|
@ -104,9 +103,9 @@ mod tests {
|
|||
let client = tokio::net::TcpStream::connect(listener.local_addr().expect("addr"))
|
||||
.await
|
||||
.expect("connect");
|
||||
let (server, _) = listener.accept().await.expect("accept");
|
||||
let (mut server, _) = listener.accept().await.expect("accept");
|
||||
drop(listener);
|
||||
assert!(handler.read_head(&server).await.is_err());
|
||||
assert!(handler.read_head(&mut server).await.is_err());
|
||||
drop(client);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
85
src/protocol/minecraft/mod.rs
Normal file
85
src/protocol/minecraft/mod.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
//! 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,
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
94
src/protocol/minecraft/parser.rs
Normal file
94
src/protocol/minecraft/parser.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
//! 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,5 +1,7 @@
|
|||
#[cfg(feature = "protocol-http")]
|
||||
pub mod http;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
pub mod minecraft;
|
||||
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
|
@ -19,6 +21,30 @@ 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 с клиентом
|
||||
|
|
@ -39,7 +65,7 @@ pub trait ProtocolHandler: Send + Sync {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>>;
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>>;
|
||||
}
|
||||
|
||||
/// Реестр скомпилированных протокольных обработчиков.
|
||||
|
|
@ -108,8 +134,8 @@ mod tests {
|
|||
fn handshake<'a>(
|
||||
&'a self,
|
||||
_stream: &'a mut TcpStream,
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(Upstream::new("127.0.0.1:9000")) })
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(HandshakeResult::new(Upstream::new("127.0.0.1:9000"))) })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,29 +1,13 @@
|
|||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::store::StateStore;
|
||||
use futures::StreamExt;
|
||||
use redis::AsyncCommands;
|
||||
use redis::Msg;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
|
||||
mod sync;
|
||||
pub use sync::start_blacklist_sync;
|
||||
|
||||
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)?;
|
||||
|
|
@ -31,140 +15,6 @@ 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?;
|
||||
|
|
@ -189,23 +39,3 @@ 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
134
src/store/redis/sync.rs
Normal file
134
src/store/redis/sync.rs
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
mod event;
|
||||
mod snapshot;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
57
src/store/redis/sync/event.rs
Normal file
57
src/store/redis/sync/event.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
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(())
|
||||
}
|
||||
77
src/store/redis/sync/snapshot.rs
Normal file
77
src/store/redis/sync/snapshot.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
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,3 +1,8 @@
|
|||
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)
|
||||
|
|
@ -20,6 +25,35 @@ 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,7 +1,5 @@
|
|||
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;
|
||||
|
||||
|
|
@ -13,7 +11,6 @@ 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;
|
||||
|
|
@ -168,36 +165,6 @@ 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::{ProtocolHandler, ProtocolRegistry, Upstream};
|
||||
use rampart::protocol::{HandshakeResult, 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<Upstream>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(Upstream::new("127.0.0.1:9000")) })
|
||||
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
|
||||
Box::pin(async { Ok(HandshakeResult::new(Upstream::new("127.0.0.1:9000"))) })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue