Compare commits
4 commits
08b7292ac4
...
15f3fcb895
| Author | SHA1 | Date | |
|---|---|---|---|
| 15f3fcb895 | |||
| ca7ce8c58e | |||
| bcae8901c6 | |||
| 72c1f45d62 |
20 changed files with 1163 additions and 182 deletions
228
Cargo.lock
generated
228
Cargo.lock
generated
|
|
@ -26,6 +26,12 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anes"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
|
@ -304,6 +310,12 @@ version = "1.12.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cast"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "castaway"
|
name = "castaway"
|
||||||
version = "0.2.4"
|
version = "0.2.4"
|
||||||
|
|
@ -362,6 +374,33 @@ dependencies = [
|
||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ciborium"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
|
||||||
|
dependencies = [
|
||||||
|
"ciborium-io",
|
||||||
|
"ciborium-ll",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ciborium-io"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ciborium-ll"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
|
||||||
|
dependencies = [
|
||||||
|
"ciborium-io",
|
||||||
|
"half",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.6.7"
|
version = "4.6.7"
|
||||||
|
|
@ -494,12 +533,67 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "criterion"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
|
||||||
|
dependencies = [
|
||||||
|
"anes",
|
||||||
|
"cast",
|
||||||
|
"ciborium",
|
||||||
|
"clap",
|
||||||
|
"criterion-plot",
|
||||||
|
"is-terminal",
|
||||||
|
"itertools 0.10.5",
|
||||||
|
"num-traits",
|
||||||
|
"once_cell",
|
||||||
|
"oorandom",
|
||||||
|
"plotters",
|
||||||
|
"rayon",
|
||||||
|
"regex",
|
||||||
|
"serde",
|
||||||
|
"serde_derive",
|
||||||
|
"serde_json",
|
||||||
|
"tinytemplate",
|
||||||
|
"walkdir",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "criterion-plot"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
|
||||||
|
dependencies = [
|
||||||
|
"cast",
|
||||||
|
"itertools 0.10.5",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "critical-section"
|
name = "critical-section"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-utils"
|
name = "crossbeam-utils"
|
||||||
version = "0.8.23"
|
version = "0.8.23"
|
||||||
|
|
@ -533,6 +627,12 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crunchy"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crypto-common"
|
name = "crypto-common"
|
||||||
version = "0.1.7"
|
version = "0.1.7"
|
||||||
|
|
@ -951,6 +1051,17 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "half"
|
||||||
|
version = "2.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"crunchy",
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.14.5"
|
version = "0.14.5"
|
||||||
|
|
@ -974,6 +1085,12 @@ version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hex"
|
name = "hex"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
|
|
@ -1271,12 +1388,32 @@ version = "0.21.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf370abdafd54d13e54a620e8c3e1145f28e46cc9d704bc6d94414559df41763"
|
checksum = "cf370abdafd54d13e54a620e8c3e1145f28e46cc9d704bc6d94414559df41763"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is-terminal"
|
||||||
|
version = "0.4.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "is_terminal_polyfill"
|
name = "is_terminal_polyfill"
|
||||||
version = "1.70.2"
|
version = "1.70.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.10.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
|
|
@ -1703,6 +1840,12 @@ version = "1.70.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "oorandom"
|
||||||
|
version = "11.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl-probe"
|
name = "openssl-probe"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
|
@ -1904,6 +2047,34 @@ version = "0.3.34"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
|
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "plotters"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
"plotters-backend",
|
||||||
|
"plotters-svg",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"web-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "plotters-backend"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "plotters-svg"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
|
||||||
|
dependencies = [
|
||||||
|
"plotters-backend",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "portable-atomic"
|
name = "portable-atomic"
|
||||||
version = "1.15.0"
|
version = "1.15.0"
|
||||||
|
|
@ -2088,6 +2259,7 @@ dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
"criterion",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
"dashmap",
|
"dashmap",
|
||||||
"futures",
|
"futures",
|
||||||
|
|
@ -2186,7 +2358,7 @@ dependencies = [
|
||||||
"compact_str",
|
"compact_str",
|
||||||
"critical-section",
|
"critical-section",
|
||||||
"hashbrown 0.17.1",
|
"hashbrown 0.17.1",
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"kasuari",
|
"kasuari",
|
||||||
"lru",
|
"lru",
|
||||||
"palette",
|
"palette",
|
||||||
|
|
@ -2251,7 +2423,7 @@ dependencies = [
|
||||||
"hashbrown 0.17.1",
|
"hashbrown 0.17.1",
|
||||||
"indoc",
|
"indoc",
|
||||||
"instability",
|
"instability",
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"line-clipping",
|
"line-clipping",
|
||||||
"ratatui-core",
|
"ratatui-core",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
@ -2261,6 +2433,26 @@ dependencies = [
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redis"
|
name = "redis"
|
||||||
version = "1.7.1"
|
version = "1.7.1"
|
||||||
|
|
@ -3052,6 +3244,16 @@ dependencies = [
|
||||||
"zerovec",
|
"zerovec",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tinytemplate"
|
||||||
|
version = "1.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyvec"
|
name = "tinyvec"
|
||||||
version = "1.13.3"
|
version = "1.13.3"
|
||||||
|
|
@ -3333,7 +3535,7 @@ version = "2.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
|
checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
|
|
@ -3842,6 +4044,26 @@ dependencies = [
|
||||||
"synstructure",
|
"synstructure",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.8.63"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.8.63"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.119",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerofrom"
|
name = "zerofrom"
|
||||||
version = "0.1.8"
|
version = "0.1.8"
|
||||||
|
|
|
||||||
|
|
@ -80,3 +80,10 @@ libc = { version = "0.2", optional = true }
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# `ServiceExt::oneshot` drives the axum router in integration tests.
|
# `ServiceExt::oneshot` drives the axum router in integration tests.
|
||||||
tower = { version = "0.5", features = ["util"] }
|
tower = { version = "0.5", features = ["util"] }
|
||||||
|
# Micro-benchmarks of the per-connection hot path (`cargo bench --all-features`).
|
||||||
|
criterion = { version = "0.5", features = ["html_reports"] }
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "hot_paths"
|
||||||
|
harness = false
|
||||||
|
required-features = ["protocol-http"]
|
||||||
|
|
|
||||||
5
Makefile
5
Makefile
|
|
@ -3,7 +3,7 @@
|
||||||
CARGO = cargo
|
CARGO = cargo
|
||||||
TARGET_DIR = target
|
TARGET_DIR = target
|
||||||
|
|
||||||
.PHONY: all build release test test-all check check-all fmt fmt-check clippy clean deny audit fuzz
|
.PHONY: all build release test test-all check check-all fmt fmt-check clippy clean deny audit fuzz bench
|
||||||
.PHONY: ebpf ebpf-clean
|
.PHONY: ebpf ebpf-clean
|
||||||
.PHONY: docker docker-build docker-up docker-down docker-logs
|
.PHONY: docker docker-build docker-up docker-down docker-logs
|
||||||
.PHONY: ci ci-full repo-gates
|
.PHONY: ci ci-full repo-gates
|
||||||
|
|
@ -78,6 +78,9 @@ docker-logs:
|
||||||
checkstyle: fmt-check clippy
|
checkstyle: fmt-check clippy
|
||||||
@echo "✓ Checkstyle passed (rustfmt + clippy)"
|
@echo "✓ Checkstyle passed (rustfmt + clippy)"
|
||||||
|
|
||||||
|
bench:
|
||||||
|
$(CARGO) bench --all-features --bench hot_paths
|
||||||
|
|
||||||
# Parser fuzzing (nightly + cargo-fuzz). 60 s per target is a smoke run;
|
# Parser fuzzing (nightly + cargo-fuzz). 60 s per target is a smoke run;
|
||||||
# longer campaigns are run locally before a release.
|
# longer campaigns are run locally before a release.
|
||||||
fuzz:
|
fuzz:
|
||||||
|
|
|
||||||
7
TODO.md
7
TODO.md
|
|
@ -287,9 +287,10 @@ module-size, secrets, `cargo deny`, rustdoc без warning'ов, clang+verifier,
|
||||||
|
|
||||||
### Осталось
|
### Осталось
|
||||||
|
|
||||||
- [ ] Monitor (`add_cidr`) и `traffic/hook` autoban не сверяются с `trusted_cidrs`: если
|
- [x] Monitor (`add_cidr`) и `traffic/hook` autoban сверяются с `trusted_cidrs`: бан
|
||||||
доверенный прокси физически живёт в банимой подсети, его адрес может попасть под
|
подавляется, если префикс пересекается с доверенной сетью (`TrustedProxyCidr::overlaps_prefix`),
|
||||||
префиксный бан. Нужна общая проверка перед любым баном.
|
о чём пишется warn. Тесты: `tests/subnet_detection.rs`, `tests/traffic_intel.rs`,
|
||||||
|
`tests/proxy_identity.rs`.
|
||||||
|
|
||||||
### Сделано в этом раунде
|
### Сделано в этом раунде
|
||||||
|
|
||||||
|
|
|
||||||
107
benches/hot_paths.rs
Normal file
107
benches/hot_paths.rs
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
//! Micro-benchmarks for the per-connection hot path.
|
||||||
|
//!
|
||||||
|
//! Run with `cargo bench --all-features --bench hot_paths`. Baseline numbers
|
||||||
|
//! live in `docs/research/benchmark.md`; compare medians before and after a
|
||||||
|
//! change instead of absolute values, which depend on the machine.
|
||||||
|
|
||||||
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use criterion::{Criterion, black_box, criterion_group, criterion_main};
|
||||||
|
|
||||||
|
use rampart::filter::blacklist::Blacklist;
|
||||||
|
use rampart::filter::rate_limit::RateLimiter;
|
||||||
|
use rampart::protocol::http::parser::{Policy, evaluate};
|
||||||
|
use rampart::traffic::prefix::{PrefixSnapshot, SubnetDetector};
|
||||||
|
|
||||||
|
fn addr(last: u8) -> IpAddr {
|
||||||
|
IpAddr::V4(Ipv4Addr::new(198, 51, 100, last))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Token-bucket admission, the first filter on every accepted connection.
|
||||||
|
fn bench_rate_limiter(c: &mut Criterion) {
|
||||||
|
c.bench_function("rate_limiter/check", |b| {
|
||||||
|
let limiter = RateLimiter::new(1_000_000.0, 1_000_000.0);
|
||||||
|
let mut n = 0u32;
|
||||||
|
b.iter(|| {
|
||||||
|
n = n.wrapping_add(1);
|
||||||
|
black_box(limiter.check(addr((n % 200) as u8)));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exact-IP and CIDR lookups of the ban tables (both on the accept path).
|
||||||
|
fn bench_blacklist(c: &mut Criterion) {
|
||||||
|
let blacklist = Blacklist::new();
|
||||||
|
for octet in 1..=200u8 {
|
||||||
|
blacklist.add(addr(octet), Duration::from_secs(60), "bench");
|
||||||
|
}
|
||||||
|
blacklist.add_cidr(
|
||||||
|
IpAddr::V4(Ipv4Addr::new(203, 0, 113, 0)),
|
||||||
|
24,
|
||||||
|
Duration::from_secs(60),
|
||||||
|
"bench",
|
||||||
|
);
|
||||||
|
|
||||||
|
c.bench_function("blacklist/is_blocked_exact", |b| {
|
||||||
|
let mut n = 0u32;
|
||||||
|
b.iter(|| {
|
||||||
|
n = n.wrapping_add(1);
|
||||||
|
black_box(blacklist.is_blocked(addr((n % 200) as u8 + 1)));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
c.bench_function("blacklist/is_blocked_cidr", |b| {
|
||||||
|
b.iter(|| black_box(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 42)))));
|
||||||
|
});
|
||||||
|
|
||||||
|
c.bench_function("blacklist/is_blocked_miss", |b| {
|
||||||
|
b.iter(|| black_box(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)))));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Subnet detector over a full window (called once per monitor tick).
|
||||||
|
fn bench_detector(c: &mut Criterion) {
|
||||||
|
let snapshots: Vec<(IpAddr, PrefixSnapshot)> = (0..256u32)
|
||||||
|
.map(|i| {
|
||||||
|
let prefix = IpAddr::V4(Ipv4Addr::new(10, (i / 256) as u8, (i % 256) as u8, 0));
|
||||||
|
(
|
||||||
|
prefix,
|
||||||
|
PrefixSnapshot {
|
||||||
|
syn_count: 120,
|
||||||
|
pkt_count: 400,
|
||||||
|
last_seen_ns: 0,
|
||||||
|
unique_sources: 40,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let detector = SubnetDetector::new(100, 16);
|
||||||
|
|
||||||
|
c.bench_function("subnet_detector/evaluate_256", |b| {
|
||||||
|
b.iter(|| black_box(detector.evaluate(black_box(&snapshots))));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// HTTP request-head validation for every new HTTP connection.
|
||||||
|
fn bench_http_parser(c: &mut Criterion) {
|
||||||
|
let request = b"GET /api/v1/status HTTP/1.1\r\nHost: edge.example\r\nUser-Agent: bench/1.0\r\nAccept: */*\r\n\r\n";
|
||||||
|
let policy = Policy {
|
||||||
|
max_header_bytes: 8192,
|
||||||
|
blocked_paths: vec!["/admin".to_string()],
|
||||||
|
require_user_agent: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
c.bench_function("http_parser/evaluate", |b| {
|
||||||
|
b.iter(|| black_box(evaluate(black_box(request), &policy)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(
|
||||||
|
hot_paths,
|
||||||
|
bench_rate_limiter,
|
||||||
|
bench_blacklist,
|
||||||
|
bench_detector,
|
||||||
|
bench_http_parser
|
||||||
|
);
|
||||||
|
criterion_main!(hot_paths);
|
||||||
|
|
@ -31,6 +31,14 @@ cargo clippy --all-targets --all-features -- -D warnings
|
||||||
cargo deny check
|
cargo deny check
|
||||||
```
|
```
|
||||||
|
|
||||||
|
E2E-сценарии живут в `tests/edge_e2e.rs`: реальный TCP-клиент через продакшн-accept-loop и
|
||||||
|
pipeline до upstream на loopback (обычный HTTP, PoW с решённым и неверным nonce, rate-limit,
|
||||||
|
blacklist). Root и внешняя сеть не нужны, каждый тест укладывается в 5 с:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo test --all-features --test edge_e2e
|
||||||
|
```
|
||||||
|
|
||||||
XDP: smoke-check компиляции BPF-программы:
|
XDP: smoke-check компиляции BPF-программы:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|
@ -91,7 +99,30 @@ cargo +nightly fuzz run proxy_v2 -- -max_total_time=600
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 3. Локальный integration-стенд (Docker)
|
## 4. Бенчмарки горячего пути
|
||||||
|
|
||||||
|
Микробенчмарки (criterion) для того, что выполняется на каждое соединение:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo bench --all-features --bench hot_paths
|
||||||
|
# HTML-отчёт: target/criterion/report/index.html
|
||||||
|
```
|
||||||
|
|
||||||
|
Замеряются: `RateLimiter::check`, exact/CIDR/miss lookup в `Blacklist`, `SubnetDetector::evaluate`
|
||||||
|
на окне из 256 префиксов и разбор HTTP request-head. Сравнивай медианы до/после изменения на
|
||||||
|
одной машине; абсолютные числа зависят от CPU. Порядок величин на dev-машине (2 vCPU):
|
||||||
|
|
||||||
|
| Бенчмарк | Медиана |
|
||||||
|
|----------|---------|
|
||||||
|
| `rate_limiter/check` | ~110 ns |
|
||||||
|
| `blacklist/is_blocked_exact` | ~70 ns |
|
||||||
|
| `blacklist/is_blocked_cidr` | ~220 ns |
|
||||||
|
| `subnet_detector/evaluate_256` | ~2.6 µs |
|
||||||
|
| `http_parser/evaluate` | ~280 ns |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Локальный integration-стенд (Docker)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bash deploy/test/run_test.sh
|
bash deploy/test/run_test.sh
|
||||||
|
|
@ -102,7 +133,7 @@ attacker-контейнер. Сценарии — в [deploy/test/README.md](../
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 4. Нагрузочное тестирование
|
## 6. Нагрузочное тестирование
|
||||||
|
|
||||||
### Много-IP TCP flood на VDS (edge-only)
|
### Много-IP TCP flood на VDS (edge-only)
|
||||||
|
|
||||||
|
|
@ -130,7 +161,7 @@ hping3 -S --flood -p 25565 --rand-source EDGE_IP # только на свои
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 5. Метрики для проверки во время теста
|
## 7. Метрики для проверки во время теста
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -s http://localhost:9090/metrics | grep rampart_
|
curl -s http://localhost:9090/metrics | grep rampart_
|
||||||
|
|
|
||||||
|
|
@ -64,12 +64,15 @@ pub async fn run() -> anyhow::Result<RunExit> {
|
||||||
let blacklist = Arc::new(Blacklist::new());
|
let blacklist = Arc::new(Blacklist::new());
|
||||||
let reputation = Arc::new(IpReputation::new());
|
let reputation = Arc::new(IpReputation::new());
|
||||||
let detector = Arc::new(Mutex::new(AttackDetector::new()));
|
let detector = Arc::new(Mutex::new(AttackDetector::new()));
|
||||||
let hook = Arc::new(TrafficHook::new(
|
let hook = Arc::new(
|
||||||
reputation.clone(),
|
TrafficHook::new(
|
||||||
blacklist.clone(),
|
reputation.clone(),
|
||||||
config.detect.autoban.clone(),
|
blacklist.clone(),
|
||||||
config.ban.ban_duration_secs,
|
config.detect.autoban.clone(),
|
||||||
));
|
config.ban.ban_duration_secs,
|
||||||
|
)
|
||||||
|
.with_trusted(config.proxy_protocol.trusted_cidrs.clone()),
|
||||||
|
);
|
||||||
let alert_dispatcher = AlertDispatcher::new();
|
let alert_dispatcher = AlertDispatcher::new();
|
||||||
let webhook_url = config.detect.alert.webhook_url.clone();
|
let webhook_url = config.detect.alert.webhook_url.clone();
|
||||||
let allowed_1s = Arc::new(AtomicU64::new(0));
|
let allowed_1s = Arc::new(AtomicU64::new(0));
|
||||||
|
|
@ -132,11 +135,14 @@ pub async fn run() -> anyhow::Result<RunExit> {
|
||||||
let subnet_tracker = start_subnet_tracker(&config);
|
let subnet_tracker = start_subnet_tracker(&config);
|
||||||
monitor::spawn(
|
monitor::spawn(
|
||||||
&config.detect.prefix,
|
&config.detect.prefix,
|
||||||
Duration::from_secs(config.ban.ban_duration_secs),
|
monitor::MonitorSetup {
|
||||||
blacklist.clone(),
|
ban_ttl: Duration::from_secs(config.ban.ban_duration_secs),
|
||||||
!config.proxy_protocol.ingress_enabled,
|
blacklist: blacklist.clone(),
|
||||||
subnet_tracker.clone(),
|
trusted_peer: !config.proxy_protocol.ingress_enabled,
|
||||||
xdp_filter.clone(),
|
trusted_networks: config.proxy_protocol.trusted_cidrs.clone(),
|
||||||
|
tracker: subnet_tracker.clone(),
|
||||||
|
xdp: xdp_filter.clone(),
|
||||||
|
},
|
||||||
shutdown_rx.clone(),
|
shutdown_rx.clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
//! Owns the [`Config`] root type, its serde defaults and the semantic validation
|
//! Owns the [`Config`] root type, its serde defaults and the semantic validation
|
||||||
//! that runs before the daemon binds a socket.
|
//! that runs before the daemon binds a socket.
|
||||||
mod sections;
|
mod sections;
|
||||||
|
mod trusted;
|
||||||
|
|
||||||
#[cfg(feature = "protocol-http")]
|
#[cfg(feature = "protocol-http")]
|
||||||
pub use sections::HttpProtocolConfig;
|
pub use sections::HttpProtocolConfig;
|
||||||
|
|
@ -12,9 +13,9 @@ pub use sections::MinecraftProtocolConfig;
|
||||||
pub use sections::ProtocolConfig;
|
pub use sections::ProtocolConfig;
|
||||||
pub use sections::{
|
pub use sections::{
|
||||||
BackendConfig, BanConfig, BindConfig, DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig,
|
BackendConfig, BanConfig, BindConfig, DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig,
|
||||||
LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, ProxyProtocolConfig, StoreConfig, TrustedProxyCidr,
|
LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, ProxyProtocolConfig, StoreConfig, WorkerConfig, XdpConfig,
|
||||||
WorkerConfig, XdpConfig,
|
|
||||||
};
|
};
|
||||||
|
pub use trusted::TrustedProxyCidr;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
//! Edge sections: bind, backend, workers, limits, ban, PoW and PROXY protocol.
|
//! Edge sections: bind, backend, workers, limits, ban, PoW and PROXY protocol.
|
||||||
|
|
||||||
|
use crate::config::trusted::TrustedProxyCidr;
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
/// `[bind]` section: listen address and port of the edge node.
|
/// `[bind]` section: listen address and port of the edge node.
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
|
@ -159,66 +158,6 @@ fn default_pow_difficulty() -> u8 {
|
||||||
4
|
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 {
|
|
||||||
/// Returns whether `ip` belongs to this network.
|
|
||||||
#[must_use]
|
|
||||||
pub fn contains(&self, ip: IpAddr) -> bool {
|
|
||||||
// Masks are family-specific: a prefix above 32 would underflow the v4 shift.
|
|
||||||
let prefix = self.prefix_len;
|
|
||||||
match (self.network, ip) {
|
|
||||||
(IpAddr::V4(net), IpAddr::V4(ip)) => u32::from(net) & mask_v4(prefix) == u32::from(ip) & mask_v4(prefix),
|
|
||||||
(IpAddr::V6(net), IpAddr::V6(ip)) => u128::from(net) & mask_v6(prefix) == u128::from(ip) & mask_v6(prefix),
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for TrustedProxyCidr {
|
|
||||||
type Err = String;
|
|
||||||
|
|
||||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
|
||||||
let (address, prefix) = value.split_once('/').map_or((value, None), |(a, p)| (a, Some(p)));
|
|
||||||
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 = prefix
|
|
||||||
.map_or(Ok(max_prefix), |prefix| prefix.parse::<u8>())
|
|
||||||
.map_err(|_| format!("invalid trusted proxy CIDR prefix: {value}"))?;
|
|
||||||
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) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `[proxy_protocol]` section: inbound PROXY protocol ingestion.
|
/// `[proxy_protocol]` section: inbound PROXY protocol ingestion.
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct ProxyProtocolConfig {
|
pub struct ProxyProtocolConfig {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,5 @@ pub use detect::MinecraftProtocolConfig;
|
||||||
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
#[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))]
|
||||||
pub use detect::ProtocolConfig;
|
pub use detect::ProtocolConfig;
|
||||||
pub use detect::{DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig};
|
pub use detect::{DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig};
|
||||||
pub use edge::{
|
pub use edge::{BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, WorkerConfig};
|
||||||
BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, TrustedProxyCidr, WorkerConfig,
|
|
||||||
};
|
|
||||||
pub use platform::{LoggingConfig, MetricsConfig, StoreConfig, XdpConfig};
|
pub use platform::{LoggingConfig, MetricsConfig, StoreConfig, XdpConfig};
|
||||||
|
|
|
||||||
108
src/config/trusted.rs
Normal file
108
src/config/trusted.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
//! Trusted PROXY-protocol networks and CIDR helpers shared by the edge paths.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Deserializer};
|
||||||
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
/// 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 {
|
||||||
|
/// Returns whether `ip` belongs to this network.
|
||||||
|
#[must_use]
|
||||||
|
pub fn contains(&self, ip: IpAddr) -> bool {
|
||||||
|
// Masks are family-specific: a prefix above 32 would underflow the v4 shift.
|
||||||
|
let prefix = self.prefix_len;
|
||||||
|
match (self.network, normalize(ip)) {
|
||||||
|
(IpAddr::V4(net), IpAddr::V4(ip)) => u32::from(net) & mask_v4(prefix) == u32::from(ip) & mask_v4(prefix),
|
||||||
|
(IpAddr::V6(net), IpAddr::V6(ip)) => u128::from(net) & mask_v6(prefix) == u128::from(ip) & mask_v6(prefix),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether `ip/prefix_len` intersects this network.
|
||||||
|
///
|
||||||
|
/// Used before banning a prefix: a ban covering a trusted proxy address
|
||||||
|
/// would punish the proxy and every client behind it.
|
||||||
|
#[must_use]
|
||||||
|
pub fn overlaps_prefix(&self, ip: IpAddr, prefix_len: u8) -> bool {
|
||||||
|
match (self.network, normalize(ip)) {
|
||||||
|
(IpAddr::V4(net), IpAddr::V4(other)) => {
|
||||||
|
if prefix_len == 0 || prefix_len > 32 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let net_mask = mask_v4(self.prefix_len);
|
||||||
|
let other_mask = mask_v4(prefix_len);
|
||||||
|
let net_start = u32::from(net) & net_mask;
|
||||||
|
let net_end = net_start | !net_mask;
|
||||||
|
let other_start = u32::from(other) & other_mask;
|
||||||
|
let other_end = other_start | !other_mask;
|
||||||
|
net_start <= other_end && other_start <= net_end
|
||||||
|
},
|
||||||
|
(IpAddr::V6(net), IpAddr::V6(other)) => {
|
||||||
|
if prefix_len == 0 || prefix_len > 128 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let net_mask = mask_v6(self.prefix_len);
|
||||||
|
let other_mask = mask_v6(prefix_len);
|
||||||
|
let net_start = u128::from(net) & net_mask;
|
||||||
|
let net_end = net_start | !net_mask;
|
||||||
|
let other_start = u128::from(other) & other_mask;
|
||||||
|
let other_end = other_start | !other_mask;
|
||||||
|
net_start <= other_end && other_start <= net_end
|
||||||
|
},
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for TrustedProxyCidr {
|
||||||
|
type Err = String;
|
||||||
|
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
let (address, prefix) = value.split_once('/').map_or((value, None), |(a, p)| (a, Some(p)));
|
||||||
|
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 = prefix
|
||||||
|
.map_or(Ok(max_prefix), |prefix| prefix.parse::<u8>())
|
||||||
|
.map_err(|_| format!("invalid trusted proxy CIDR prefix: {value}"))?;
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Collapses IPv4-mapped IPv6 addresses to IPv4.
|
||||||
|
fn normalize(ip: IpAddr) -> IpAddr {
|
||||||
|
match ip {
|
||||||
|
IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, IpAddr::V4),
|
||||||
|
IpAddr::V4(_) => ip,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
108
src/engine/subnet/enforce.rs
Normal file
108
src/engine/subnet/enforce.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
//! Verdict enforcement: turning subnet `Block` verdicts into bans.
|
||||||
|
//!
|
||||||
|
//! Kept apart from [`super::monitor`] so the monitor task and the ban policy
|
||||||
|
//! stay within the module-size budget.
|
||||||
|
|
||||||
|
use crate::filter::blacklist::Blacklist;
|
||||||
|
use crate::traffic::prefix::{PrefixVerdict, SubnetVerdict};
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// Verdict enforcement settings shared by every snapshot tick.
|
||||||
|
pub(super) struct BanPolicy {
|
||||||
|
pub(super) blacklist: Arc<Blacklist>,
|
||||||
|
pub(super) ttl: Duration,
|
||||||
|
pub(super) trusted: bool,
|
||||||
|
/// Trusted PROXY-protocol networks; a prefix ban overlapping one of them is
|
||||||
|
/// suppressed, because it would punish the proxy and every client behind it.
|
||||||
|
pub(super) trusted_networks: Vec<crate::config::TrustedProxyCidr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies userspace `Block` verdicts to the shared blacklist.
|
||||||
|
///
|
||||||
|
/// Returns the number of banned prefixes. When `trusted` is false the observed
|
||||||
|
/// addresses belong to an unverified ingress path, so the verdict is only logged
|
||||||
|
/// at warn level and no ban is installed.
|
||||||
|
///
|
||||||
|
/// The ban is inserted as a CIDR range, so every address inside the detected
|
||||||
|
/// `/24` or `/64` is blocked, not just its network address.
|
||||||
|
pub fn enforce_tracker_verdicts(
|
||||||
|
verdicts: &[PrefixVerdict],
|
||||||
|
blacklist: &Blacklist,
|
||||||
|
ttl: Duration,
|
||||||
|
trusted: bool,
|
||||||
|
trusted_networks: &[crate::config::TrustedProxyCidr],
|
||||||
|
) -> usize {
|
||||||
|
let mut banned = 0;
|
||||||
|
for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) {
|
||||||
|
if !trusted {
|
||||||
|
tracing::warn!(
|
||||||
|
prefix = %pv.prefix,
|
||||||
|
"subnet block verdict on an untrusted peer path; not adding to the blacklist"
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if overlaps_trusted(pv.prefix, trusted_networks) {
|
||||||
|
tracing::warn!(
|
||||||
|
prefix = %pv.prefix,
|
||||||
|
"subnet ban suppressed: the prefix contains a trusted proxy network"
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
blacklist.add_cidr(pv.prefix, prefix_len(pv.prefix), ttl, "subnet_attack");
|
||||||
|
banned += 1;
|
||||||
|
tracing::warn!(
|
||||||
|
prefix = %pv.prefix,
|
||||||
|
ttl_secs = ttl.as_secs(),
|
||||||
|
"subnet blocked in the userspace blacklist"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
banned
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Installs XDP prefix bans for `Block` verdicts.
|
||||||
|
#[cfg(feature = "xdp")]
|
||||||
|
pub(super) fn ban_xdp_verdicts(
|
||||||
|
xdp: &crate::traffic::prefix::XdpPrefixStats,
|
||||||
|
verdicts: &[PrefixVerdict],
|
||||||
|
ttl: Duration,
|
||||||
|
trusted_networks: &[crate::config::TrustedProxyCidr],
|
||||||
|
) {
|
||||||
|
for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) {
|
||||||
|
if overlaps_trusted(pv.prefix, trusted_networks) {
|
||||||
|
tracing::warn!(
|
||||||
|
prefix = %pv.prefix,
|
||||||
|
"subnet kernel ban suppressed: the prefix contains a trusted proxy network"
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let result = {
|
||||||
|
let guard = xdp.filter_lock();
|
||||||
|
guard.ban_cidr(pv.prefix, prefix_len(pv.prefix), ttl.as_secs())
|
||||||
|
};
|
||||||
|
match result {
|
||||||
|
Ok(()) => tracing::warn!(
|
||||||
|
prefix = %pv.prefix,
|
||||||
|
ttl_secs = ttl.as_secs(),
|
||||||
|
"subnet blocked in XDP"
|
||||||
|
),
|
||||||
|
Err(e) => tracing::warn!("subnet block failed for {}: {e}", pv.prefix),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether a ban of `prefix/prefix_len` would cover a trusted proxy.
|
||||||
|
fn overlaps_trusted(prefix: IpAddr, trusted_networks: &[crate::config::TrustedProxyCidr]) -> bool {
|
||||||
|
trusted_networks
|
||||||
|
.iter()
|
||||||
|
.any(|net| net.overlaps_prefix(prefix, prefix_len(prefix)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prefix length matching the detection granularity (v4 /24, v6 /64).
|
||||||
|
pub(super) fn prefix_len(prefix: IpAddr) -> u8 {
|
||||||
|
match prefix {
|
||||||
|
IpAddr::V4(_) => 24,
|
||||||
|
IpAddr::V6(_) => 64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
//! Subnet-level detector: userspace prefix aggregator and verdict monitor.
|
//! Subnet-level detector: userspace prefix aggregator and verdict monitor.
|
||||||
|
mod enforce;
|
||||||
pub mod monitor;
|
pub mod monitor;
|
||||||
pub mod tracker;
|
pub mod tracker;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@
|
||||||
//! (feature `xdp` with a loaded filter) or the userspace aggregator
|
//! (feature `xdp` with a loaded filter) or the userspace aggregator
|
||||||
//! [`SubnetTracker`].
|
//! [`SubnetTracker`].
|
||||||
|
|
||||||
|
use super::enforce::BanPolicy;
|
||||||
|
#[cfg(feature = "xdp")]
|
||||||
|
use super::enforce::ban_xdp_verdicts;
|
||||||
use crate::config::DetectPrefixConfig;
|
use crate::config::DetectPrefixConfig;
|
||||||
use crate::engine::subnet_tracker::{SubnetTracker, TrackerSource};
|
use crate::engine::subnet_tracker::{SubnetTracker, TrackerSource};
|
||||||
use crate::filter::blacklist::Blacklist;
|
use crate::filter::blacklist::Blacklist;
|
||||||
|
|
@ -17,6 +20,8 @@ use std::sync::{Arc, Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
|
|
||||||
|
pub use super::enforce::enforce_tracker_verdicts;
|
||||||
|
|
||||||
enum Source {
|
enum Source {
|
||||||
#[cfg(feature = "xdp")]
|
#[cfg(feature = "xdp")]
|
||||||
Xdp(XdpPrefixStats),
|
Xdp(XdpPrefixStats),
|
||||||
|
|
@ -33,35 +38,37 @@ impl Source {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verdict enforcement settings shared by every snapshot tick.
|
/// Everything the monitor task needs besides the detection config.
|
||||||
struct BanPolicy {
|
pub struct MonitorSetup {
|
||||||
blacklist: Arc<Blacklist>,
|
/// Ban lifetime, from `[ban].ban_duration_secs`.
|
||||||
ttl: Duration,
|
pub ban_ttl: Duration,
|
||||||
trusted: bool,
|
/// Shared ban table.
|
||||||
|
pub blacklist: Arc<Blacklist>,
|
||||||
|
/// False when the observed address comes from an unverified ingress path
|
||||||
|
/// (inbound PROXY protocol), where banning it could punish the transport
|
||||||
|
/// proxy instead of the real client.
|
||||||
|
pub trusted_peer: bool,
|
||||||
|
/// Trusted proxy networks; a prefix ban overlapping one is suppressed.
|
||||||
|
pub trusted_networks: Vec<crate::config::TrustedProxyCidr>,
|
||||||
|
/// Userspace aggregator, used when XDP is not available.
|
||||||
|
pub tracker: Option<Arc<SubnetTracker>>,
|
||||||
|
/// Loaded XDP filter, preferred over the userspace tracker when present.
|
||||||
|
pub xdp: Option<Arc<Mutex<XdpFilter>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawns the background subnet monitor when `[detect.prefix] enabled`.
|
/// Spawns the background subnet monitor when `[detect.prefix] enabled`.
|
||||||
///
|
pub fn spawn(detect: &DetectPrefixConfig, setup: MonitorSetup, mut shutdown: watch::Receiver<bool>) {
|
||||||
/// Source priority: the XDP map when a filter is loaded, otherwise the userspace
|
|
||||||
/// aggregator. Without either source the monitor stays disabled and logs a
|
|
||||||
/// warning.
|
|
||||||
///
|
|
||||||
/// `ban_ttl` comes from `[ban].ban_duration_secs`. `trusted_peer` is false when
|
|
||||||
/// the observed address comes from an unverified ingress path (inbound PROXY
|
|
||||||
/// protocol), where banning it could punish the transport proxy instead of the
|
|
||||||
/// real client.
|
|
||||||
pub fn spawn(
|
|
||||||
detect: &DetectPrefixConfig,
|
|
||||||
ban_ttl: Duration,
|
|
||||||
blacklist: Arc<Blacklist>,
|
|
||||||
trusted_peer: bool,
|
|
||||||
tracker: Option<Arc<SubnetTracker>>,
|
|
||||||
xdp: Option<Arc<Mutex<XdpFilter>>>,
|
|
||||||
mut shutdown: watch::Receiver<bool>,
|
|
||||||
) {
|
|
||||||
if !detect.enabled {
|
if !detect.enabled {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let MonitorSetup {
|
||||||
|
ban_ttl,
|
||||||
|
blacklist,
|
||||||
|
trusted_peer,
|
||||||
|
trusted_networks,
|
||||||
|
tracker,
|
||||||
|
xdp,
|
||||||
|
} = setup;
|
||||||
let Some(source) = build_source(tracker, xdp) else {
|
let Some(source) = build_source(tracker, xdp) else {
|
||||||
tracing::warn!("detect.prefix enabled but no stats source available; monitor disabled");
|
tracing::warn!("detect.prefix enabled but no stats source available; monitor disabled");
|
||||||
return;
|
return;
|
||||||
|
|
@ -72,6 +79,7 @@ pub fn spawn(
|
||||||
blacklist,
|
blacklist,
|
||||||
ttl: ban_ttl,
|
ttl: ban_ttl,
|
||||||
trusted: trusted_peer,
|
trusted: trusted_peer,
|
||||||
|
trusted_networks,
|
||||||
};
|
};
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
|
@ -124,7 +132,7 @@ fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolic
|
||||||
}
|
}
|
||||||
match source {
|
match source {
|
||||||
#[cfg(feature = "xdp")]
|
#[cfg(feature = "xdp")]
|
||||||
Source::Xdp(xdp) if policy.trusted => ban_xdp_verdicts(xdp, verdicts, policy.ttl),
|
Source::Xdp(xdp) if policy.trusted => ban_xdp_verdicts(xdp, verdicts, policy.ttl, &policy.trusted_networks),
|
||||||
#[cfg(feature = "xdp")]
|
#[cfg(feature = "xdp")]
|
||||||
Source::Xdp(_) => {
|
Source::Xdp(_) => {
|
||||||
if verdicts.iter().any(|pv| pv.verdict == SubnetVerdict::Block) {
|
if verdicts.iter().any(|pv| pv.verdict == SubnetVerdict::Block) {
|
||||||
|
|
@ -132,68 +140,14 @@ fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolic
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Source::Tracker(_) => {
|
Source::Tracker(_) => {
|
||||||
enforce_tracker_verdicts(verdicts, &policy.blacklist, policy.ttl, policy.trusted);
|
enforce_tracker_verdicts(
|
||||||
},
|
verdicts,
|
||||||
}
|
&policy.blacklist,
|
||||||
}
|
policy.ttl,
|
||||||
|
policy.trusted,
|
||||||
/// Applies userspace `Block` verdicts to the shared blacklist.
|
&policy.trusted_networks,
|
||||||
///
|
|
||||||
/// Returns the number of banned prefixes. When `trusted` is false the observed
|
|
||||||
/// addresses belong to an unverified ingress path, so the verdict is only logged
|
|
||||||
/// at warn level and no ban is installed.
|
|
||||||
///
|
|
||||||
/// The ban is inserted as a CIDR range, so every address inside the detected
|
|
||||||
/// `/24` or `/64` is blocked, not just its network address.
|
|
||||||
pub fn enforce_tracker_verdicts(
|
|
||||||
verdicts: &[PrefixVerdict],
|
|
||||||
blacklist: &Blacklist,
|
|
||||||
ttl: Duration,
|
|
||||||
trusted: bool,
|
|
||||||
) -> usize {
|
|
||||||
let mut banned = 0;
|
|
||||||
for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) {
|
|
||||||
if !trusted {
|
|
||||||
tracing::warn!(
|
|
||||||
prefix = %pv.prefix,
|
|
||||||
"subnet block verdict on an untrusted peer path; not adding to the blacklist"
|
|
||||||
);
|
);
|
||||||
continue;
|
},
|
||||||
}
|
|
||||||
blacklist.add_cidr(pv.prefix, prefix_len(pv.prefix), ttl, "subnet_attack");
|
|
||||||
banned += 1;
|
|
||||||
tracing::warn!(
|
|
||||||
prefix = %pv.prefix,
|
|
||||||
ttl_secs = ttl.as_secs(),
|
|
||||||
"subnet blocked in the userspace blacklist"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
banned
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "xdp")]
|
|
||||||
fn ban_xdp_verdicts(xdp: &XdpPrefixStats, verdicts: &[PrefixVerdict], ttl: Duration) {
|
|
||||||
for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) {
|
|
||||||
let result = {
|
|
||||||
let guard = xdp.filter_lock();
|
|
||||||
guard.ban_cidr(pv.prefix, prefix_len(pv.prefix), ttl.as_secs())
|
|
||||||
};
|
|
||||||
match result {
|
|
||||||
Ok(()) => tracing::warn!(
|
|
||||||
prefix = %pv.prefix,
|
|
||||||
ttl_secs = ttl.as_secs(),
|
|
||||||
"subnet blocked in XDP"
|
|
||||||
),
|
|
||||||
Err(e) => tracing::warn!("subnet block failed for {}: {e}", pv.prefix),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Prefix length matching the detection granularity (v4 /24, v6 /64).
|
|
||||||
fn prefix_len(prefix: IpAddr) -> u8 {
|
|
||||||
match prefix {
|
|
||||||
IpAddr::V4(_) => 24,
|
|
||||||
IpAddr::V6(_) => 64,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ pub struct TrafficHook {
|
||||||
autoban: DetectAutobanConfig,
|
autoban: DetectAutobanConfig,
|
||||||
ban_ttl_secs: u64,
|
ban_ttl_secs: u64,
|
||||||
conn_window: AtomicU64,
|
conn_window: AtomicU64,
|
||||||
|
/// Trusted PROXY-protocol networks; addresses inside them are never banned.
|
||||||
|
trusted: Vec<crate::config::TrustedProxyCidr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TrafficHook {
|
impl TrafficHook {
|
||||||
|
|
@ -40,9 +42,17 @@ impl TrafficHook {
|
||||||
autoban,
|
autoban,
|
||||||
ban_ttl_secs,
|
ban_ttl_secs,
|
||||||
conn_window: AtomicU64::new(0),
|
conn_window: AtomicU64::new(0),
|
||||||
|
trusted: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Marks the networks whose addresses must never be banned (trusted proxies).
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_trusted(mut self, trusted: Vec<crate::config::TrustedProxyCidr>) -> Self {
|
||||||
|
self.trusted = trusted;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// A new connection was accepted by the accept loop.
|
/// A new connection was accepted by the accept loop.
|
||||||
///
|
///
|
||||||
/// Only bumps the cps window counter; the counter is drained by
|
/// Only bumps the cps window counter; the counter is drained by
|
||||||
|
|
@ -76,8 +86,9 @@ impl TrafficHook {
|
||||||
if self.blacklist.is_blocked(ip) {
|
if self.blacklist.is_blocked(ip) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
self.ban(ip, BAN_REASON_ATTACK);
|
if self.ban(ip, BAN_REASON_ATTACK) {
|
||||||
banned += 1;
|
banned += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
banned
|
banned
|
||||||
}
|
}
|
||||||
|
|
@ -91,12 +102,21 @@ impl TrafficHook {
|
||||||
if self.reputation.score(ip) > self.autoban.reputation_threshold {
|
if self.reputation.score(ip) > self.autoban.reputation_threshold {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.ban(ip, reason);
|
let _ = self.ban(ip, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ban(&self, ip: IpAddr, reason: &str) {
|
/// Bans `ip` unless it belongs to a trusted proxy network.
|
||||||
|
///
|
||||||
|
/// Returns whether the ban was actually installed.
|
||||||
|
fn ban(&self, ip: IpAddr, reason: &str) -> bool {
|
||||||
|
let host_prefix = if ip.is_ipv4() { 32 } else { 128 };
|
||||||
|
if self.trusted.iter().any(|net| net.overlaps_prefix(ip, host_prefix)) {
|
||||||
|
tracing::warn!(%ip, reason, "auto-ban suppressed: address belongs to a trusted proxy network");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
self.blacklist.add(ip, Duration::from_secs(self.ban_ttl_secs), reason);
|
self.blacklist.add(ip, Duration::from_secs(self.ban_ttl_secs), reason);
|
||||||
metrics::AUTO_BANS_TOTAL.with_label_values(&[reason]).inc();
|
metrics::AUTO_BANS_TOTAL.with_label_values(&[reason]).inc();
|
||||||
tracing::warn!(%ip, reason, ttl_secs = self.ban_ttl_secs, "auto-ban issued by traffic intelligence");
|
tracing::warn!(%ip, reason, ttl_secs = self.ban_ttl_secs, "auto-ban issued by traffic intelligence");
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
153
tests/edge_e2e.rs
Normal file
153
tests/edge_e2e.rs
Normal file
|
|
@ -0,0 +1,153 @@
|
||||||
|
//! End-to-end tests of the real edge connection path over loopback.
|
||||||
|
//!
|
||||||
|
//! A TCP client is driven through the production accept loop (`engine::listener`)
|
||||||
|
//! and pipeline (`engine::tunnel::Gateway`) to a real upstream socket on
|
||||||
|
//! `127.0.0.1`: framing, the PoW gate, the admission filters and the relay are
|
||||||
|
//! exercised as one system, without root and without any external network.
|
||||||
|
#![cfg(feature = "protocol-http")]
|
||||||
|
|
||||||
|
#[path = "edge_e2e/support.rs"]
|
||||||
|
mod support;
|
||||||
|
|
||||||
|
use rampart::engine::challenge::solve;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use std::net::Ipv4Addr;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::time::Duration;
|
||||||
|
use support::{
|
||||||
|
IO_BUDGET, POW_DIFFICULTY, REQUEST, RESPONSE, exchange, read_to_close, rejected_reply, start_edge, start_upstream,
|
||||||
|
};
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
|
/// Reads one newline-terminated line (the PoW challenge).
|
||||||
|
async fn read_line(stream: &mut TcpStream) -> anyhow::Result<String> {
|
||||||
|
let mut line = Vec::new();
|
||||||
|
for _ in 0..128 {
|
||||||
|
let mut byte = [0u8; 1];
|
||||||
|
if stream.read(&mut byte).await? == 0 || byte[0] == b'\n' {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
line.push(byte[0]);
|
||||||
|
}
|
||||||
|
Ok(String::from_utf8_lossy(&line).trim().to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_satisfies(challenge: &str, nonce: &str, difficulty: u8) -> bool {
|
||||||
|
let digest = Sha256::digest(format!("{challenge}{nonce}").as_bytes());
|
||||||
|
hex::encode(digest)
|
||||||
|
.as_bytes()
|
||||||
|
.iter()
|
||||||
|
.take(difficulty as usize)
|
||||||
|
.all(|byte| byte.is_ascii_digit() && *byte <= b'3')
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds a nonce the gate must reject; the caller re-checks the result.
|
||||||
|
fn invalid_nonce(challenge: &str, difficulty: u8) -> String {
|
||||||
|
// At difficulty 2 about fifteen of sixteen candidates fail, so this stops
|
||||||
|
// within a handful of iterations.
|
||||||
|
for candidate in 0u64..1_000_000 {
|
||||||
|
let nonce = candidate.to_string();
|
||||||
|
if !hash_satisfies(challenge, &nonce, difficulty) {
|
||||||
|
return nonce;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"0".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn plain_http_request_is_proxied_to_the_upstream() -> anyhow::Result<()> {
|
||||||
|
let (upstream, accepted, seen) = start_upstream().await;
|
||||||
|
let edge = start_edge(upstream, |_| {}).await?;
|
||||||
|
|
||||||
|
let response = exchange(edge.addr, REQUEST).await?;
|
||||||
|
assert_eq!(response.as_slice(), RESPONSE);
|
||||||
|
assert_eq!(accepted.load(Ordering::SeqCst), 1);
|
||||||
|
assert_eq!(*seen.lock().expect("requests lock"), vec![REQUEST.to_vec()]);
|
||||||
|
|
||||||
|
edge.stop().await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn pow_gate_accepts_a_solved_challenge_and_drops_a_wrong_nonce() -> anyhow::Result<()> {
|
||||||
|
let (upstream, accepted, seen) = start_upstream().await;
|
||||||
|
let edge = start_edge(upstream, |config| {
|
||||||
|
config.pow.enabled = true;
|
||||||
|
config.pow.difficulty = POW_DIFFICULTY;
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut client = TcpStream::connect(edge.addr).await?;
|
||||||
|
let challenge = tokio::time::timeout(IO_BUDGET, read_line(&mut client)).await??;
|
||||||
|
let nonce = solve(&challenge, POW_DIFFICULTY)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("no solution found for challenge {challenge}"))?;
|
||||||
|
// Coalesced write: the PoW reader must consume exactly the nonce line and
|
||||||
|
// leave the HTTP head buffered for the handshake that runs next.
|
||||||
|
let mut payload = format!("{nonce}\r\n").into_bytes();
|
||||||
|
payload.extend_from_slice(REQUEST);
|
||||||
|
client.write_all(&payload).await?;
|
||||||
|
let response = tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await??;
|
||||||
|
assert_eq!(response.as_slice(), RESPONSE);
|
||||||
|
assert_eq!(accepted.load(Ordering::SeqCst), 1);
|
||||||
|
|
||||||
|
let mut hostile = TcpStream::connect(edge.addr).await?;
|
||||||
|
let challenge = tokio::time::timeout(IO_BUDGET, read_line(&mut hostile)).await??;
|
||||||
|
let wrong = invalid_nonce(&challenge, POW_DIFFICULTY);
|
||||||
|
assert!(!hash_satisfies(&challenge, &wrong, POW_DIFFICULTY));
|
||||||
|
// Send the request after the wrong nonce: if the gate accepted it, the head
|
||||||
|
// would be buffered and proxied, and the upstream response would come back.
|
||||||
|
let mut payload = format!("{wrong}\r\n").into_bytes();
|
||||||
|
payload.extend_from_slice(REQUEST);
|
||||||
|
let _ = hostile.write_all(&payload).await;
|
||||||
|
let reply = tokio::time::timeout(IO_BUDGET, read_to_close(&mut hostile)).await??;
|
||||||
|
assert!(reply.is_empty(), "a wrong nonce must be dropped, got {reply:?}");
|
||||||
|
assert_eq!(
|
||||||
|
accepted.load(Ordering::SeqCst),
|
||||||
|
1,
|
||||||
|
"a failed PoW must not reach the upstream"
|
||||||
|
);
|
||||||
|
assert_eq!(seen.lock().expect("requests lock").len(), 1);
|
||||||
|
|
||||||
|
edge.stop().await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rate_limit_drops_the_second_connection_before_the_upstream() -> anyhow::Result<()> {
|
||||||
|
let (upstream, accepted, _seen) = start_upstream().await;
|
||||||
|
let edge = start_edge(upstream, |config| {
|
||||||
|
// One token, practically no refill: the second connection is over budget.
|
||||||
|
config.limits.rate_limit_pps = 0.001;
|
||||||
|
config.limits.rate_limit_burst = 1.0;
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let response = exchange(edge.addr, REQUEST).await?;
|
||||||
|
assert_eq!(response.as_slice(), RESPONSE);
|
||||||
|
assert_eq!(accepted.load(Ordering::SeqCst), 1);
|
||||||
|
|
||||||
|
let reply = rejected_reply(edge.addr, REQUEST).await?;
|
||||||
|
assert!(
|
||||||
|
reply.is_empty(),
|
||||||
|
"rate-limited connection must not be proxied, got {reply:?}"
|
||||||
|
);
|
||||||
|
// EOF above means the reject path already ran; it precedes the upstream
|
||||||
|
// stage, so a second accept cannot appear afterwards.
|
||||||
|
assert_eq!(accepted.load(Ordering::SeqCst), 1);
|
||||||
|
|
||||||
|
edge.stop().await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn blacklisted_client_never_reaches_the_upstream() -> anyhow::Result<()> {
|
||||||
|
let (upstream, accepted, seen) = start_upstream().await;
|
||||||
|
let edge = start_edge(upstream, |_| {}).await?;
|
||||||
|
edge.blacklist
|
||||||
|
.add(Ipv4Addr::LOCALHOST.into(), Duration::from_secs(60), "edge_e2e");
|
||||||
|
|
||||||
|
let reply = rejected_reply(edge.addr, REQUEST).await?;
|
||||||
|
assert!(reply.is_empty(), "a banned client must be dropped, got {reply:?}");
|
||||||
|
assert_eq!(accepted.load(Ordering::SeqCst), 0);
|
||||||
|
assert!(seen.lock().expect("requests lock").is_empty());
|
||||||
|
|
||||||
|
edge.stop().await
|
||||||
|
}
|
||||||
250
tests/edge_e2e/support.rs
Normal file
250
tests/edge_e2e/support.rs
Normal file
|
|
@ -0,0 +1,250 @@
|
||||||
|
//! Shared loopback harness for `tests/edge_e2e.rs`; cargo does not build it as
|
||||||
|
//! its own test binary because it lives in a subdirectory.
|
||||||
|
|
||||||
|
use rampart::config::Config;
|
||||||
|
use rampart::engine::challenge::DifficultyAdjuster;
|
||||||
|
use rampart::engine::listener;
|
||||||
|
use rampart::engine::tunnel::Gateway;
|
||||||
|
use rampart::filter::blacklist::Blacklist;
|
||||||
|
use rampart::filter::rate_limit::RateLimiter;
|
||||||
|
use rampart::protocol::ProtocolRegistry;
|
||||||
|
use rampart::protocol::http::HttpProtocolHandler;
|
||||||
|
use rampart::traffic::hook::TrafficHook;
|
||||||
|
use rampart::traffic::reputation::IpReputation;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::io::ErrorKind;
|
||||||
|
use std::net::{Ipv4Addr, SocketAddr};
|
||||||
|
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
use tokio::sync::watch;
|
||||||
|
use tokio::task::JoinHandle;
|
||||||
|
|
||||||
|
pub const REQUEST: &[u8] = b"GET / HTTP/1.1\r\nHost: edge\r\n\r\n";
|
||||||
|
pub const RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK";
|
||||||
|
pub const POW_DIFFICULTY: u8 = 2;
|
||||||
|
/// Upper bound of every I/O wait, so a regression fails instead of hanging.
|
||||||
|
pub const IO_BUDGET: Duration = Duration::from_secs(4);
|
||||||
|
const READY_BUDGET: Duration = Duration::from_secs(2);
|
||||||
|
const MAX_REPLY_BYTES: usize = 64 * 1024;
|
||||||
|
|
||||||
|
// --- upstream -------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Real upstream: returns its address, an accept counter and the heads it saw.
|
||||||
|
pub async fn start_upstream() -> (SocketAddr, Arc<AtomicUsize>, Arc<Mutex<Vec<Vec<u8>>>>) {
|
||||||
|
let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
|
||||||
|
.await
|
||||||
|
.expect("bind upstream");
|
||||||
|
let addr = listener.local_addr().expect("upstream address");
|
||||||
|
let accepted = Arc::new(AtomicUsize::new(0));
|
||||||
|
let requests = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let (count, seen) = (Arc::clone(&accepted), Arc::clone(&requests));
|
||||||
|
// Detached: the per-test runtime drops this server when the test ends.
|
||||||
|
tokio::spawn(async move {
|
||||||
|
while let Ok((mut stream, _)) = listener.accept().await {
|
||||||
|
count.fetch_add(1, Ordering::SeqCst);
|
||||||
|
let seen = Arc::clone(&seen);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let head = read_head(&mut stream).await;
|
||||||
|
seen.lock().expect("requests lock").push(head);
|
||||||
|
let _ = stream.write_all(RESPONSE).await;
|
||||||
|
let _ = stream.shutdown().await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
(addr, accepted, requests)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads until the request head ends (`\r\n\r\n`), the peer closes or a read fails.
|
||||||
|
async fn read_head(stream: &mut TcpStream) -> Vec<u8> {
|
||||||
|
let mut head = Vec::new();
|
||||||
|
let mut buf = [0u8; 512];
|
||||||
|
while head.len() < 4096 {
|
||||||
|
match stream.read(&mut buf).await {
|
||||||
|
Ok(0) | Err(_) => break,
|
||||||
|
Ok(n) => {
|
||||||
|
head.extend_from_slice(&buf[..n]);
|
||||||
|
if head.windows(4).any(|w| w == b"\r\n\r\n".as_slice()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
head
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- edge -----------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Production-shaped config from serde defaults, plus the per-test overrides.
|
||||||
|
fn test_config(port: u16, upstream: SocketAddr) -> Config {
|
||||||
|
let mut config = Config::default();
|
||||||
|
config.bind.address = Ipv4Addr::LOCALHOST.to_string();
|
||||||
|
config.bind.port = port;
|
||||||
|
config.backend.upstreams = vec![upstream.to_string()];
|
||||||
|
config.workers.count = 1;
|
||||||
|
config.limits.handshake_timeout_secs = 2;
|
||||||
|
config.limits.relay_idle_timeout_secs = 5;
|
||||||
|
config.limits.max_connections_per_ip = 8;
|
||||||
|
config.protocol.http.max_header_bytes = 4096;
|
||||||
|
config.protocol.http.timeout_secs = 2;
|
||||||
|
config
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Running accept loop plus the shared state a test may pre-seed.
|
||||||
|
pub struct Edge {
|
||||||
|
pub addr: SocketAddr,
|
||||||
|
pub blacklist: Arc<Blacklist>,
|
||||||
|
shutdown: watch::Sender<bool>,
|
||||||
|
task: JoinHandle<anyhow::Result<()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Edge {
|
||||||
|
/// Stops the accept loop and waits for it, so no task outlives the test.
|
||||||
|
pub async fn stop(mut self) -> anyhow::Result<()> {
|
||||||
|
let _ = self.shutdown.send(true);
|
||||||
|
match tokio::time::timeout(IO_BUDGET, &mut self.task).await {
|
||||||
|
Ok(result) => result?,
|
||||||
|
Err(_) => anyhow::bail!("edge accept loop did not stop within {IO_BUDGET:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Edge {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Panic path: still wake the accept loop instead of leaking it.
|
||||||
|
let _ = self.shutdown.send(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Starts `listener::run`, retrying once if the throwaway port was stolen.
|
||||||
|
///
|
||||||
|
/// The port comes from binding `:0` and dropping the probe, so another process
|
||||||
|
/// can claim it before the edge binds; `run` then fails immediately and a fresh
|
||||||
|
/// port is used. The retry is bounded, never a loop.
|
||||||
|
pub async fn start_edge(upstream: SocketAddr, tune: impl Fn(&mut Config)) -> anyhow::Result<Edge> {
|
||||||
|
let mut last_error = anyhow::anyhow!("edge listener did not bind");
|
||||||
|
for _ in 0..2 {
|
||||||
|
let port = free_port()?;
|
||||||
|
let mut config = test_config(port, upstream);
|
||||||
|
tune(&mut config);
|
||||||
|
let config = Arc::new(config);
|
||||||
|
let (gateway, blacklist, hook) = build_gateway(&config);
|
||||||
|
let (shutdown, shutdown_rx) = watch::channel(false);
|
||||||
|
let task = tokio::spawn(listener::run(config, gateway, hook, shutdown_rx));
|
||||||
|
let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port));
|
||||||
|
if wait_until_bound(addr, &task).await {
|
||||||
|
return Ok(Edge {
|
||||||
|
addr,
|
||||||
|
blacklist,
|
||||||
|
shutdown,
|
||||||
|
task,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let _ = shutdown.send(true);
|
||||||
|
if let Ok(Err(error)) = task.await {
|
||||||
|
last_error = error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(last_error.context("edge listener failed to bind a loopback port after two attempts"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Binds `127.0.0.1:0`, reads the assigned port and releases it.
|
||||||
|
fn free_port() -> anyhow::Result<u16> {
|
||||||
|
let probe = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?;
|
||||||
|
let port = probe.local_addr()?.port();
|
||||||
|
drop(probe);
|
||||||
|
Ok(port)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Waits until the edge owns `addr`. A second bind without `SO_REUSEPORT` fails
|
||||||
|
/// with `EADDRINUSE` exactly then, so no throwaway connection spends a
|
||||||
|
/// rate-limit token and pollutes the assertions.
|
||||||
|
async fn wait_until_bound(addr: SocketAddr, task: &JoinHandle<anyhow::Result<()>>) -> bool {
|
||||||
|
let deadline = Instant::now() + READY_BUDGET;
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
if task.is_finished() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if std::net::TcpListener::bind(addr).is_err() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(2)).await;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Assembles the edge state as `app::runtime` does, minus XDP, Redis and the
|
||||||
|
/// ClickHouse writer (all optional, all `None` here).
|
||||||
|
fn build_gateway(config: &Arc<Config>) -> (Arc<Gateway>, Arc<Blacklist>, Arc<TrafficHook>) {
|
||||||
|
let blacklist = Arc::new(Blacklist::new());
|
||||||
|
let reputation = Arc::new(IpReputation::new());
|
||||||
|
let mut registry = ProtocolRegistry::new();
|
||||||
|
registry.register(Box::new(HttpProtocolHandler::new(
|
||||||
|
&config.protocol.http,
|
||||||
|
&config.backend.upstreams,
|
||||||
|
)));
|
||||||
|
let gateway = Gateway::new(
|
||||||
|
Arc::clone(config),
|
||||||
|
Arc::new(RateLimiter::new(
|
||||||
|
config.limits.rate_limit_pps,
|
||||||
|
config.limits.rate_limit_burst,
|
||||||
|
)),
|
||||||
|
Arc::clone(&blacklist),
|
||||||
|
Arc::new(Mutex::new(DifficultyAdjuster::with_floor(config.pow.difficulty))),
|
||||||
|
Arc::new(HashSet::new()),
|
||||||
|
Arc::clone(&reputation),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Arc::new(AtomicU64::new(0)),
|
||||||
|
Arc::new(registry),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
// Auto-ban stays at its default (disabled), so a drop cannot leak a ban into
|
||||||
|
// a later assertion.
|
||||||
|
let hook = Arc::new(TrafficHook::new(
|
||||||
|
reputation,
|
||||||
|
Arc::clone(&blacklist),
|
||||||
|
config.detect.autoban.clone(),
|
||||||
|
config.ban.ban_duration_secs,
|
||||||
|
));
|
||||||
|
(Arc::new(gateway), blacklist, hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- client ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Reads until the peer closes; a reset also means "closed, no extra data".
|
||||||
|
pub async fn read_to_close(stream: &mut TcpStream) -> anyhow::Result<Vec<u8>> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
let mut buf = [0u8; 512];
|
||||||
|
loop {
|
||||||
|
match stream.read(&mut buf).await {
|
||||||
|
Ok(0) => return Ok(out),
|
||||||
|
Ok(n) => {
|
||||||
|
out.extend_from_slice(&buf[..n]);
|
||||||
|
if out.len() > MAX_REPLY_BYTES {
|
||||||
|
anyhow::bail!("peer kept sending past {MAX_REPLY_BYTES} bytes");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(error) if error.kind() == ErrorKind::ConnectionReset => return Ok(out),
|
||||||
|
Err(error) => return Err(error.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends `payload` on a fresh connection and returns everything until the close.
|
||||||
|
pub async fn exchange(addr: SocketAddr, payload: &[u8]) -> anyhow::Result<Vec<u8>> {
|
||||||
|
let mut client = TcpStream::connect(addr).await?;
|
||||||
|
client.write_all(payload).await?;
|
||||||
|
tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await?
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Best-effort request on a fresh connection: a rejected peer may already be
|
||||||
|
/// gone, so write errors are ignored. The reply is what the caller asserts on —
|
||||||
|
/// a gate that let the request through would return the upstream response.
|
||||||
|
pub async fn rejected_reply(addr: SocketAddr, payload: &[u8]) -> anyhow::Result<Vec<u8>> {
|
||||||
|
let mut client = TcpStream::connect(addr).await?;
|
||||||
|
let _ = client.write_all(payload).await;
|
||||||
|
tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await?
|
||||||
|
}
|
||||||
|
|
@ -237,3 +237,25 @@ async fn plain_connection_uses_the_transport_peer() {
|
||||||
let outcome = handle_plain(&gateway, peer).await;
|
let outcome = handle_plain(&gateway, peer).await;
|
||||||
assert_eq!(outcome.client_ip, Some(peer.ip()));
|
assert_eq!(outcome.client_ip, Some(peer.ip()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trusted_network_overlap_checks_prefix_intersection() {
|
||||||
|
let net: TrustedProxyCidr = "10.0.0.0/8".parse().expect("cidr");
|
||||||
|
assert!(net.overlaps_prefix("10.1.2.0".parse().expect("ip"), 24), "same range");
|
||||||
|
assert!(
|
||||||
|
net.overlaps_prefix("10.0.0.0".parse().expect("ip"), 7),
|
||||||
|
"wider prefix overlaps"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!net.overlaps_prefix("11.0.0.0".parse().expect("ip"), 8),
|
||||||
|
"sibling range"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!net.overlaps_prefix("10.0.0.0".parse().expect("ip"), 0),
|
||||||
|
"/0 is rejected"
|
||||||
|
);
|
||||||
|
|
||||||
|
let v6: TrustedProxyCidr = "fd00::/8".parse().expect("cidr");
|
||||||
|
assert!(v6.overlaps_prefix("fd12::".parse().expect("ip"), 16));
|
||||||
|
assert!(!v6.overlaps_prefix("2001:db8::".parse().expect("ip"), 32));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//! Integration tests for subnet-level detection: tracker, detector and verdicts.
|
//! Integration tests for subnet-level detection: tracker, detector and verdicts.
|
||||||
|
|
||||||
use rampart::config::Config;
|
use rampart::config::{Config, TrustedProxyCidr};
|
||||||
use rampart::engine::subnet::monitor::enforce_tracker_verdicts;
|
use rampart::engine::subnet::monitor::enforce_tracker_verdicts;
|
||||||
use rampart::engine::subnet_tracker::SubnetTracker;
|
use rampart::engine::subnet_tracker::SubnetTracker;
|
||||||
use rampart::filter::blacklist::Blacklist;
|
use rampart::filter::blacklist::Blacklist;
|
||||||
|
|
@ -299,7 +299,7 @@ fn block_verdict(prefix: IpAddr) -> PrefixVerdict {
|
||||||
fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() {
|
fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() {
|
||||||
let blacklist = Blacklist::new();
|
let blacklist = Blacklist::new();
|
||||||
let verdicts = [block_verdict(v4([203, 0, 113, 0]))];
|
let verdicts = [block_verdict(v4([203, 0, 113, 0]))];
|
||||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true);
|
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &[]);
|
||||||
assert_eq!(banned, 1);
|
assert_eq!(banned, 1);
|
||||||
assert!(blacklist.is_blocked(v4([203, 0, 113, 0])));
|
assert!(blacklist.is_blocked(v4([203, 0, 113, 0])));
|
||||||
}
|
}
|
||||||
|
|
@ -308,7 +308,7 @@ fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() {
|
||||||
fn untrusted_tracker_block_only_warns() {
|
fn untrusted_tracker_block_only_warns() {
|
||||||
let blacklist = Blacklist::new();
|
let blacklist = Blacklist::new();
|
||||||
let verdicts = [block_verdict(v4([198, 51, 100, 0]))];
|
let verdicts = [block_verdict(v4([198, 51, 100, 0]))];
|
||||||
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), false);
|
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), false, &[]);
|
||||||
assert_eq!(banned, 0);
|
assert_eq!(banned, 0);
|
||||||
assert!(blacklist.is_empty());
|
assert!(blacklist.is_empty());
|
||||||
}
|
}
|
||||||
|
|
@ -317,7 +317,7 @@ fn untrusted_tracker_block_only_warns() {
|
||||||
fn tracker_ban_uses_the_given_duration() {
|
fn tracker_ban_uses_the_given_duration() {
|
||||||
let blacklist = Blacklist::new();
|
let blacklist = Blacklist::new();
|
||||||
let verdicts = [block_verdict(v4([192, 0, 2, 0]))];
|
let verdicts = [block_verdict(v4([192, 0, 2, 0]))];
|
||||||
enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_millis(1), true);
|
enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_millis(1), true, &[]);
|
||||||
std::thread::sleep(Duration::from_millis(2));
|
std::thread::sleep(Duration::from_millis(2));
|
||||||
assert!(
|
assert!(
|
||||||
!blacklist.is_blocked(v4([192, 0, 2, 0])),
|
!blacklist.is_blocked(v4([192, 0, 2, 0])),
|
||||||
|
|
@ -331,7 +331,7 @@ fn non_block_verdicts_do_not_ban() {
|
||||||
let mut verdict = block_verdict(v4([203, 0, 113, 0]));
|
let mut verdict = block_verdict(v4([203, 0, 113, 0]));
|
||||||
verdict.verdict = SubnetVerdict::Challenge;
|
verdict.verdict = SubnetVerdict::Challenge;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
enforce_tracker_verdicts(&[verdict], &blacklist, Duration::from_secs(60), true),
|
enforce_tracker_verdicts(&[verdict], &blacklist, Duration::from_secs(60), true, &[]),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
assert!(blacklist.is_empty());
|
assert!(blacklist.is_empty());
|
||||||
|
|
@ -375,3 +375,23 @@ fn prefix_key_normalises_ipv4_mapped_addresses() {
|
||||||
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(prefix_of(mapped)));
|
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(prefix_of(mapped)));
|
||||||
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(v4([1, 2, 3, 0])));
|
assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(v4([1, 2, 3, 0])));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tracker_block_is_suppressed_when_a_trusted_proxy_lives_in_the_prefix() {
|
||||||
|
let blacklist = Blacklist::new();
|
||||||
|
let verdicts = [block_verdict(v4([10, 9, 0, 0]))];
|
||||||
|
let trusted: Vec<TrustedProxyCidr> = vec!["10.9.0.5/32".parse().expect("trusted cidr")];
|
||||||
|
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &trusted);
|
||||||
|
assert_eq!(banned, 0, "a ban covering a trusted proxy must be suppressed");
|
||||||
|
assert!(blacklist.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tracker_block_still_applies_when_trusted_networks_do_not_overlap() {
|
||||||
|
let blacklist = Blacklist::new();
|
||||||
|
let verdicts = [block_verdict(v4([10, 9, 0, 0]))];
|
||||||
|
let trusted: Vec<TrustedProxyCidr> = vec!["192.0.2.0/24".parse().expect("trusted cidr")];
|
||||||
|
let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &trusted);
|
||||||
|
assert_eq!(banned, 1);
|
||||||
|
assert!(blacklist.is_blocked(v4([10, 9, 0, 7])));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -446,3 +446,33 @@ fn alerts_fire_only_on_state_transitions() {
|
||||||
assert!(alerts[0].message.contains("attack started"));
|
assert!(alerts[0].message.contains("attack started"));
|
||||||
assert!(alerts[1].message.contains("attack ended"));
|
assert!(alerts[1].message.contains("attack ended"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn auto_ban_never_targets_a_trusted_proxy_network() {
|
||||||
|
let reputation = Arc::new(IpReputation::new());
|
||||||
|
let blacklist = Arc::new(Blacklist::new());
|
||||||
|
let trusted: Vec<rampart::config::TrustedProxyCidr> = vec!["10.0.0.0/8".parse().expect("trusted cidr")];
|
||||||
|
let hook =
|
||||||
|
TrafficHook::new(Arc::clone(&reputation), Arc::clone(&blacklist), autoban_cfg(true), 60).with_trusted(trusted);
|
||||||
|
let inside = ip([10, 1, 2, 3]);
|
||||||
|
|
||||||
|
for _ in 0..6 {
|
||||||
|
reputation.record_bad(inside);
|
||||||
|
hook.on_connection_end(inside);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
reputation.score(inside) <= -50,
|
||||||
|
"reputation must fall below the threshold"
|
||||||
|
);
|
||||||
|
assert_eq!(hook.ban_offenders(), 0, "trusted addresses are never banned");
|
||||||
|
|
||||||
|
let public = ip([198, 51, 100, 9]);
|
||||||
|
for _ in 0..6 {
|
||||||
|
reputation.record_bad(public);
|
||||||
|
hook.on_connection_end(public);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
blacklist.is_blocked(public),
|
||||||
|
"non-trusted offenders are still banned by the reputation path"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue