diff --git a/.github/workflows/platform-delivery.yml b/.github/workflows/platform-delivery.yml index 31e79c5..0a5df5c 100644 --- a/.github/workflows/platform-delivery.yml +++ b/.github/workflows/platform-delivery.yml @@ -1,4 +1,4 @@ -name: Release builds +name: Release builds and artifact identity on: workflow_dispatch: @@ -19,38 +19,41 @@ jobs: - name: apple label: Apple platform: apple - runner: macos-15 - targets: aarch64-apple-ios,aarch64-apple-ios-sim,x86_64-apple-ios,aarch64-apple-darwin,x86_64-apple-darwin - manifest: dist/apple/vcore-delivery.json + runner: macos-26 + targets: aarch64-apple-ios,aarch64-apple-ios-sim,aarch64-apple-darwin,x86_64-apple-darwin,aarch64-apple-tvos,aarch64-apple-tvos-sim - name: android label: Android platform: android runner: ubuntu-24.04 targets: aarch64-linux-android,x86_64-linux-android - manifest: dist/android/vcore-delivery.json - name: windows-x64 label: Windows x64 platform: windows runner: windows-2025 targets: x86_64-pc-windows-msvc - manifest: dist/windows/x64/vcore-delivery.json - name: windows-arm64 label: Windows ARM64 platform: windows runner: windows-11-arm targets: aarch64-pc-windows-msvc - manifest: dist/windows/arm64/vcore-delivery.json steps: - uses: actions/checkout@v7 - uses: dtolnay/rust-toolchain@stable with: + toolchain: stable targets: ${{ matrix.targets }} components: rustfmt - - uses: astral-sh/setup-uv@bec219d24cd3e171d82865faccec33120bb574f4 # v10.1.0 + - uses: astral-sh/setup-uv@v10.2.0 with: - version: "0.12.15" - python-version: "3.13" - - name: Platform artifact contracts + version: "0.12" + - name: Install latest stable Python 3 + shell: bash + env: + UV_PYTHON_INSTALL_DIR: ${{ runner.temp }}/python + run: | + uv python install 3 --no-bin --no-registry + echo "UV_PYTHON=$(uv python find --managed-python --no-project 3)" >> "$GITHUB_ENV" + - name: Build artifact helper contracts run: uv run --project scripts --locked python -m unittest discover -s scripts/tests -p test_platform_delivery.py - name: Locked source run: cargo fetch --locked @@ -58,21 +61,25 @@ jobs: if: matrix.platform == 'android' shell: bash run: | - "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;28.2.13676358" - echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/28.2.13676358" >> "$GITHUB_ENV" - - name: Build production artifacts and record identity + sdkmanager="$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" + ndk_package="$("$sdkmanager" --list --channel=0 | awk -F '|' ' + NF == 1 { split($0, fields, " "); $1 = fields[1]; $2 = fields[2] } + { gsub(/[[:space:]]/, "", $1); gsub(/[[:space:]]/, "", $2) } + $1 ~ /^ndk[;\/]30\.[0-9]+\.[0-9]+$/ && + $2 ~ /^30\.[0-9]+\.[0-9]+$/ { + sub(/^ndk\//, "ndk;", $1); print $1 + } + ' | sort -Vu | tail -n 1)" + test -n "$ndk_package" || { echo "Stable NDK r30 is unavailable" >&2; exit 1; } + "$sdkmanager" --channel=0 "$ndk_package" + echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/${ndk_package#ndk;}" >> "$GITHUB_ENV" + - name: Build production artifacts and validate identity run: uv run --project scripts --locked vcore-scripts build ${{ matrix.platform }} --delivery - - name: Independently verify packaged files - run: uv run --project scripts --locked vcore-scripts check platform-artifacts --manifest ${{ matrix.manifest }} - - name: Native C ABI and Windows snapshot/startup contracts - if: matrix.platform != 'android' - run: uv run --project scripts --locked vcore-scripts check platform-abi --manifest ${{ matrix.manifest }} - - name: Preserve unsigned artifacts and evidence - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 + - name: Preserve unsigned build artifacts + uses: actions/upload-artifact@v7 with: name: vcore-${{ matrix.name }}-${{ github.sha }} path: | dist/${{ matrix.platform }}/ - target/platform-delivery/abi/**/result.json if-no-files-found: error retention-days: 14 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0cbc86e..52d47bd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ permissions: jobs: client-contracts: name: ${{ matrix.name }} - runs-on: macos-15 + runs-on: macos-26 timeout-minutes: 90 strategy: fail-fast: false @@ -21,40 +21,209 @@ jobs: profile: debug - name: Core tests (Release) profile: release - - name: Quality and features + - name: Quality and compilation profile: features steps: - uses: actions/checkout@v7 - uses: dtolnay/rust-toolchain@stable with: + toolchain: stable components: rustfmt, clippy - - uses: astral-sh/setup-uv@bec219d24cd3e171d82865faccec33120bb574f4 # v10.1.0 + - uses: astral-sh/setup-uv@v10.2.0 with: - version: "0.12.15" - python-version: "3.13" - - name: Formatting, scripts, and dependency contract + version: "0.12" + - name: Install latest stable Python 3 + shell: bash + env: + UV_PYTHON_INSTALL_DIR: ${{ runner.temp }}/python + run: | + uv python install 3 --no-bin --no-registry + echo "UV_PYTHON=$(uv python find --managed-python --no-project 3)" >> "$GITHUB_ENV" + - name: Formatting and build helper contracts if: matrix.profile == 'features' run: | cargo fmt --all -- --check uv run --project scripts --locked python -m unittest discover -s scripts/tests uv run --project scripts --locked ruff check scripts uv run --project scripts --locked ruff format --check scripts - uv run --project scripts --locked vcore-scripts check c-header - uv run --project scripts --locked vcore-scripts check tls-dependencies - - name: Production, instrumented harness, and netstack clippy + - name: Compile the public C and C++ header + if: matrix.profile == 'features' + shell: bash + run: | + clang -x c -std=c11 -Wall -Wextra -Werror -fsyntax-only -include vcore.h -I include - <<'C' + int main(void) { + char *response = VCoreInvoke("{\"apiVersion\":5,\"method\":\"version\",\"payload\":{}}"); + VCoreFree(response); + VCoreFree((char *)0); + return 0; + } + C + clang++ -x c++ -std=c++17 -Wall -Wextra -Werror -fsyntax-only -include vcore.h -I include - <<'CPP' + int main() { + char *response = VCoreInvoke("{\"apiVersion\":5,\"method\":\"version\",\"payload\":{}}"); + VCoreFree(response); + VCoreFree(nullptr); + return 0; + } + CPP + - name: Audit the locked TLS dependency graph + if: matrix.profile == 'features' + shell: bash + run: | + cargo metadata --locked --all-features --format-version 1 > "$RUNNER_TEMP/vcore-metadata.json" + uv run --project scripts --locked python - "$RUNNER_TEMP/vcore-metadata.json" <<'PY' + import json + import sys + from pathlib import Path + + metadata = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) + packages = metadata["packages"] + nodes = {node["id"]: node for node in metadata["resolve"]["nodes"]} + registry = {"registry+https://github.com/rust-lang/crates.io-index", "registry+https://index.crates.io/"} + boring_source = "git+https://github.com/OneXray/boring?branch=release#43c1c1d5b9464b3f2d5204be8664778fada7dbaf" + + def require(condition, message): + if not condition: + raise SystemExit(message) + + def package(name, version=None, sources=registry): + matches = [item for item in packages if item["name"] == name] + require(len(matches) == 1, f"expected one resolved {name}") + item = matches[0] + require(version is None or item["version"] == version, f"unapproved {name} version") + require(item.get("source") in sources, f"unapproved {name} source") + require(item["id"] in nodes, f"{name} missing from resolved graph") + return item + + def features(item): + return set(nodes[item["id"]]["features"]) + + require(not any("watfaq" in (item.get("source") or "").lower() for item in packages), "retired dependency source is forbidden") + rustls = package("rustls", "0.23.45") + package("tokio-rustls", "0.26.5") + package("ring") + require("ring" in features(rustls) and not features(rustls) & {"reality", "aws_lc_rs", "fips"}, "rustls must use only the ring TLS provider") + hpke = package("hpke", "0.14.1") + require(features(hpke) == {"alloc", "aes", "chacha", "x25519", "hkdfsha2"}, "unapproved ECH algorithms") + native = {} + for name, required in (("boring", {"reality", "client-fingerprint", "shadow-tls-v3", "jls"}), ("boring-sys", {"reality", "shadow-tls-v3", "jls"}), ("tokio-boring", set())): + item = package(name, "5.2.0", {boring_source}) + native[name] = item + require(required <= features(item), f"{name} is missing required TLS features") + require(not features(item) & {"fips", "fips-precompiled", "rpk", "pq-experimental", "restls"}, f"{name} enables an unapproved TLS mode") + for parent, children in (("boring", {"boring-sys"}), ("tokio-boring", {"boring", "boring-sys"})): + dependencies = {dependency["pkg"] for dependency in nodes[native[parent]["id"]]["deps"]} + require(all(native[child]["id"] in dependencies for child in children), f"{parent} must use the approved native TLS packages") + aws = [item for item in packages if item["name"].lower().startswith("aws-lc")] + if aws: + require({item["name"] for item in aws} == {"aws-lc-rs", "aws-lc-sys"}, "unapproved AWS-LC package") + chain = {name: package(name, version) for name, version in (("shadowsocks", "1.25.0"), ("shadowsocks-crypto", "0.8.0"), ("aws-lc-rs", None), ("aws-lc-sys", None))} + parents = {} + for node in nodes.values(): + for dependency in node.get("deps", []): + parents.setdefault(dependency["pkg"], set()).add(node["id"]) + for child, parent in (("shadowsocks-crypto", "shadowsocks"), ("aws-lc-rs", "shadowsocks-crypto"), ("aws-lc-sys", "aws-lc-rs")): + require(parents.get(chain[child]["id"]) == {chain[parent]["id"]}, f"{child} must be consumed only by {parent}") + for name, required in (("shadowsocks", {"aead-cipher-2022"}), ("shadowsocks-crypto", {"v2", "aws-lc"})): + require(required <= features(chain[name]), f"{name} must enable SS 2022") + require(not any(features(item) & {"fips", "aead-cipher-2022-extra", "v2-extra"} for item in chain.values()), "unapproved SS 2022 or AWS-LC mode") + print("Locked TLS sources, providers, ECH algorithms and SS 2022 dependency chain passed") + PY + - name: Production and netstack clippy if: matrix.profile == 'features' run: | cargo clippy --locked --features ffi --lib --bins -- -D warnings cargo clippy --locked --all-features --lib --bins -- -D warnings - cargo clippy --locked --features ffi,interop-test --test mihomo_interop -- -D warnings cargo clippy --locked --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -- -D warnings - - name: Run ${{ matrix.name }} - run: uv run --project scripts --locked vcore-scripts check core --profile ${{ matrix.profile }} + - name: Run memory-only ${{ matrix.name }} + if: matrix.profile != 'features' + shell: bash + env: + CORE_TEST_PROFILE: ${{ matrix.profile }} + run: | + set -euo pipefail + test_command=(cargo test --locked --all-features) + if [[ "$CORE_TEST_PROFILE" == release ]]; then test_command+=(--release); fi + run_test() { + local expected_summaries="$1" + shift + local test_log + test_log="$(mktemp "$RUNNER_TEMP/vcore-tests.XXXXXX")" + "${test_command[@]}" "$@" | tee "$test_log" + test "$(grep -Ec '^test result: ok\. [0-9]+ passed; [0-9]+ failed; [0-9]+ ignored;' "$test_log")" -eq "$expected_summaries" + test "$(grep -Ec '^test result: ok\. [1-9][0-9]* passed; 0 failed;' "$test_log")" -eq "$expected_summaries" + rm -f "$test_log" + } + targets=( + ech_config ech_tls encryption_config reality_config jls_config + security_capabilities retired_security feature_foundations limit_foundations + trojan trojan_config vmess_config vless_config vless_lifecycle + xhttp_config xhttp_budget h2_stream_regression stream_foundations stream_shutdown + xhttp_h3_shutdown hysteria2_paths hysteria2_config shadowsocks_backpressure + shadowtls_config shadowtls_stream uot_config shadowsocks_uot hysteria2_packet_ids + tuic_config tuic_memory httpupgrade_config httpupgrade_memory vmess_lifecycle + ) + target_flags=() + for target in "${targets[@]}"; do target_flags+=(--test "$target"); done + run_test "${#targets[@]}" "${target_flags[@]}" + # Keep this explicit allowlist: unfiltered legacy tests may open host listeners. + filters=( + geodata:: routing:: security:: outbound::vless::encryption config:: + outbound::hysteria2:: outbound::shadowsocks::tests::official_tcp_ + outbound::anytls:: outbound::uot:: outbound::tuic:: transport::h2_write::tests:: + transport::sing_mux::smux_driver::tests:: transport::sing_mux::tests:: + platform::rust_tun_io::tests:: platform::windows_tun_io::tests:: tun_runtime::tests:: + ) + for filter in "${filters[@]}"; do run_test 1 --lib "$filter"; done + exact_tests=( + dialer::tests::socket_initialization_churn_reclaims_completed_work + dialer::tests::socket_initialization_cancellation_joins_started_and_queued_work + dialer::tests::socket_initialization_shared_pool_contention_reclaims_cancelled_work + outbound::connector::tests::authenticated_continuation_keeps_group_choice_but_has_a_new_io_deadline + runtime::tests::integration_feature_admission_is_explicit_without_opening_sockets + ffi::tests::runtime_thread_cannot_reenter_invoke + ffi::tests::engine_workers_preserve_invoke_admission_and_shutdown + dns::runtime::tests::concurrent_redir_host_hint_reads_and_dns_updates_complete + ffi::tests::null_invalid_utf8_and_oversized_input_return_json_failures + ffi::tests::same_instance_command_is_fail_fast + ffi::tests::android_protector_is_required_only_for_tun + ) + for test_name in "${exact_tests[@]}"; do run_test 1 --lib "$test_name" -- --exact; done + run_test 1 --test quic_datagram_foundations pending_send_is_not_restarted_and_stop_cancels_without_waiting_for_writable -- --exact + - name: Feature admission and complete compilation + if: matrix.profile == 'features' + shell: bash + run: | + set -euo pipefail + run_test() { + local expected_summaries="$1" + shift + local test_log + test_log="$(mktemp "$RUNNER_TEMP/vcore-features.XXXXXX")" + cargo test --locked --no-default-features "$@" | tee "$test_log" + test "$(grep -Ec '^test result: ok\. [0-9]+ passed; [0-9]+ failed; [0-9]+ ignored;' "$test_log")" -eq "$expected_summaries" + test "$(grep -Ec '^test result: ok\. [1-9][0-9]* passed; 0 failed;' "$test_log")" -eq "$expected_summaries" + rm -f "$test_log" + } + protocols=(socks5 anytls shadowsocks trojan vmess vless hysteria2 tuic) + cargo check --locked --no-default-features --lib + for protocol in "${protocols[@]}"; do + cargo check --locked --no-default-features --features "outbound-$protocol" --lib + run_test 1 --features "outbound-$protocol" --lib runtime::tests::integration_feature_admission_is_explicit_without_opening_sockets -- --exact + done + for feature in stream-transport quic-transport shadow-tls-v3 tun ffi; do + cargo check --locked --no-default-features --features "$feature" --lib + done + run_test 2 --test feature_foundations --test hysteria2_config + cargo check --locked --lib + # Compilation only: historical listener fixtures must not execute on CI hosts. + cargo test --locked --all-features --all-targets --no-run + production_features="$(uv run --project scripts --locked python -c 'from vcore_scripts.builds import DEFAULT_FEATURES; print(DEFAULT_FEATURES)')" + cargo build --locked --release --no-default-features --features "$production_features" --lib - name: Memory-only netstack tests if: matrix.profile == 'features' - run: | - cargo test --locked --manifest-path crates/vcore-netstack/Cargo.toml --all-targets + run: cargo test --locked --manifest-path crates/vcore-netstack/Cargo.toml --all-targets platform-build: - name: Release builds + name: Release builds and artifact identity uses: ./.github/workflows/platform-delivery.yml diff --git a/AGENTS.md b/AGENTS.md index 74f40f3..6cb9655 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -2,7 +2,7 @@ VCore is a standalone Rust proxy core. The current public contract is Invoke API v5 with internal schema revision 31. Runtime configuration uses the strict schema documented in `docs/config.yaml` and is passed inline as `configYaml` / `configYamls`; YAML contains neither `configVersion` nor `default-proxy`. Public lifecycle state is runtime-local and single-instance. -Apple and Android use host-owned TUN fds through the Unix `rust-tun` adapter. Windows uses `windows-rs` / `Windows.Networking.Vpn`; the packaged ARM64 foreground, AppContainer provider, per-session full-trust runtime, lifecycle, pressure, and packet-channel gates pass on Windows 11. Windows 10, native x64, physical IPv6, WACK, and Store publishing remain release gates. Linux remains unsupported. +iOS, tvOS 17+ (ARM64 device/simulator), macOS, Android and Linux use host-owned TUN fds through the Unix `rust-tun` adapter. Linux accepts a real single-queue raw-IP TUN; the host owns namespace/routing isolation and physical egress. Apple packetFlow does not publicly guarantee raw-fd access; Linux or simulator results do not sign off a physical Network Extension. Windows uses `windows-rs` / `Windows.Networking.Vpn`; the packaged ARM64 foreground, AppContainer provider, per-session full-trust runtime, lifecycle, pressure, and packet-channel gates pass on Windows 11. Windows 10, native x64, physical IPv6, WACK, and Store publishing remain release gates. # Sources of Truth @@ -21,8 +21,10 @@ Read the relevant document completely before changing that area: - TLS profiles, certificate policy or TLS dependencies: `docs/tls-client-fingerprint.md` and `docs/tls-dependencies.md`; REALITY also requires `docs/reality-wire-protocol.md`. Before local boring development or opening/updating a PR, follow the dependency-source workflow in `docs/tls-dependencies.md`. - Unix TUN fd ownership or packet I/O: `docs/tun-platform.md`. - Windows VPN/TUN, outbound binding, AppContainer packet buffers, or package lifecycle: `docs/windows-vpn.md` and `docs/tun-platform.md`. -- Build, validation, or interoperability tooling: `scripts/README.md`, `tests/README.md`, and the unified `vcore-scripts` interface. -- Server-side tests: `docs/testing-isolation.md`. All new server peers and network origins must run in isolated containers; never fall back to native host servers. +- Builds: `scripts/README.md`; `vcore-scripts build` is compile-only, including delivery artifact-integrity checks. +- Offline regressions: `tests/README.md`. Rust memory/configuration tests remain in VCore; run explicit pure-memory filters, with all-target coverage limited to `--no-run`. +- Protocol interoperability or pressure: the public [container-benchmark](https://github.com/OneXray/container-benchmark) owns all container orchestration and fixtures. Supply the checkout explicitly with `--source vcore=PATH`; VCore builds do not import it or infer workspace paths. +- Server-side tests: `docs/testing-isolation.md`. Use isolated benchmark containers for every server peer and network origin; container failure is not permission for a host fallback. - Claims that something passed: `docs/acceptance.md`. Record only commands and environments actually executed; host tests and cross-builds do not prove physical-device data paths. # Architecture Boundaries @@ -56,21 +58,20 @@ Choose the smallest relevant set, then expand for shared contracts: ```shell cargo fmt --all -- --check -uv run --project scripts --locked vcore-scripts check core --profile debug +# Select an explicit memory-only regression named in tests/README.md. +cargo test --locked --lib geodata::tests:: # All-target compilation only; network peers must be containerized. cargo test --locked --all-features --all-targets --no-run cargo clippy --locked --all-features --lib --bins -- -D warnings cargo test --manifest-path crates/vcore-netstack/Cargo.toml --all-targets cargo clippy --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -- -D warnings -uv run --project scripts --locked vcore-scripts check c-header -uv run --project scripts --locked vcore-scripts check tls-dependencies uv run --project scripts --locked python -m unittest discover -s scripts/tests uv run --project scripts --locked ruff check scripts uv run --project scripts --locked ruff format --check scripts git diff --check ``` -Changes to FFI, shared runtime, TUN, socket creation, TLS, or packaging boundaries also require the affected target build and repository integration checks. External interop and physical-device results remain `NOT RUN` unless they were executed in the current validation run. +Changes to FFI, shared runtime, TUN, socket creation, TLS, or packaging also require affected target builds and scoped regressions. Delivery checks artifact integrity, not native-consumer execution or device behavior. External interop and physical-device results remain `NOT RUN` unless executed in the current validation run. ## Agent skills diff --git a/Cargo.lock b/Cargo.lock index dbd7cb7..7a4f44e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,7 +279,7 @@ dependencies = [ [[package]] name = "boring" version = "5.2.0" -source = "git+https://github.com/OneXray/boring?branch=release#d5a5d41850886aef5b269015130ee6d6eb2129ba" +source = "git+https://github.com/OneXray/boring?branch=release#43c1c1d5b9464b3f2d5204be8664778fada7dbaf" dependencies = [ "bitflags 2.13.2", "boring-sys", @@ -293,7 +293,7 @@ dependencies = [ [[package]] name = "boring-sys" version = "5.2.0" -source = "git+https://github.com/OneXray/boring?branch=release#d5a5d41850886aef5b269015130ee6d6eb2129ba" +source = "git+https://github.com/OneXray/boring?branch=release#43c1c1d5b9464b3f2d5204be8664778fada7dbaf" dependencies = [ "bindgen", "cmake", @@ -2285,7 +2285,7 @@ dependencies = [ [[package]] name = "tokio-boring" version = "5.2.0" -source = "git+https://github.com/OneXray/boring?branch=release#d5a5d41850886aef5b269015130ee6d6eb2129ba" +source = "git+https://github.com/OneXray/boring?branch=release#43c1c1d5b9464b3f2d5204be8664778fada7dbaf" dependencies = [ "boring", "boring-sys", @@ -2542,7 +2542,8 @@ dependencies = [ "quinn-proto", "rand", "rcgen", - "regex-automata", + "regex", + "regex-syntax", "ring", "rustls", "serde", diff --git a/Cargo.toml b/Cargo.toml index 3018839..439f6a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,11 +23,6 @@ name = "vcore-windows-session-host" path = "src/bin/windows_session_host.rs" required-features = ["ffi"] -[[example]] -name = "protocol-stream-probe" -path = "tests/protocols/stream_probe.rs" -required-features = ["interop-test", "stream-transport"] - [features] default = [ "inbound-http", @@ -59,7 +54,9 @@ quic-transport = ["dep:quinn"] # Enables local interoperability-only trust-root injection. Release build # scripts must not enable this feature. interop-test = ["outbound-anytls", "outbound-vless"] -ffi = ["tun", "dep:windows", "dep:windows-collections", "dep:windows-core"] +# Allows plaintext GeoData fixtures for isolated benchmarks, never production. +benchmark-geodata-http = [] +ffi = ["tun", "tokio/rt-multi-thread", "dep:windows", "dep:windows-collections", "dep:windows-core"] tun = [ "dep:rust-tun", "dep:vcore-netstack", @@ -105,7 +102,8 @@ quinn = { version = "=0.11.12", default-features = false, features = ["runtime-t quinn-proto = { version = "=0.11.18", default-features = false, features = ["rustls-ring"], optional = true } rand = "0.10" ring = { version = "0.17.14", optional = true } -regex-automata = { version = "0.4", default-features = false, features = ["dfa-build", "dfa-search", "syntax", "unicode"] } +regex = "1.13" +regex-syntax = { version = "0.8", default-features = false, features = ["std", "unicode-case"] } rustls = { version = "=0.23.45", default-features = false, features = ["ring", "std", "tls12"] } serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -129,7 +127,7 @@ webpki-roots = "1" yamux = { version = "=0.14.1", optional = true } zeroize = { version = "=1.9.0", optional = true } -[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] +[target.'cfg(any(target_os = "ios", target_os = "tvos", target_os = "macos"))'.dependencies] oslog = { version = "0.2", default-features = false } [target.'cfg(unix)'.dependencies] @@ -180,7 +178,7 @@ tempfile = "3" [profile.release] codegen-units = 1 lto = "thin" -opt-level = "z" +opt-level = 3 panic = "unwind" strip = "symbols" diff --git a/README.md b/README.md index 4f50639..98041ae 100644 --- a/README.md +++ b/README.md @@ -4,142 +4,126 @@ English · 简体中文 · Русский

-VCore is a standalone, host-agnostic Rust client proxy core. It provides proxy graphs, static `select` proxy groups, DNS, routing rules, GeoData, HTTP/SOCKS5 listeners, a TUN data plane, and a loopback Controller through strict YAML configuration and Invoke API v5. The internal configuration schema revision is 31; the revision appears only in the `version` response and `buildIdentity`, not in YAML. +VCore is an embeddable Rust proxy core for VPN clients and local proxies. It routes TCP/UDP traffic through direct connections, proxy nodes, groups, and chains, with integrated DNS, GeoData, and a cross-platform TUN data plane. -## Features +Configuration uses **Mihomo-compatible YAML for the supported feature set**. VCore focuses on client-side capabilities rather than implementing every Mihomo field or its complete Dashboard API. -SS2022 additionally supports [strict ShadowTLS v3](docs/outbounds.md#shadowtls-v3) TCP wrapping and UoT v2; native UDP remains separate. [TUIC v5](docs/outbounds.md#tuic-v5) supports TCP and native/quic UDP over controlled QUIC. +## What VCore can do -- Outbounds: [VLESS TCP/WS/gRPC/HTTP/H2/XHTTP, TLS/REALITY and Vision](docs/vless.md), SOCKS5 CONNECT/UDP ASSOCIATE, AnyTLS TCP/UoT, Shadowsocks 2022, [Trojan TCP/UDP over TLS/WS/gRPC](docs/outbounds.md#trojan), [VMess AEAD over TCP/WS/gRPC/HTTP/H2](docs/outbounds.md#vmess-aead), [Hysteria2 TCP/UDP, bandwidth, Salamander and port hopping](docs/outbounds.md#hysteria2), and DIRECT. SS supports three standard 2022 algorithms and AES identity chains; see its [upstream risks and acceptance boundaries](docs/outbounds.md#shadowsocks-2022). -- Proxy chains: `dialer-proxy` forms a directed acyclic graph of arbitrary length. If node A points to B, the physical path is `client -> B -> A -> target`. -- Proxy groups: static `select` groups keep ordered members, including concrete nodes, nested groups, `DIRECT`, and `REJECT`; their current-session selection can be changed live through the Controller. `dialer-proxy` may reference nodes or groups; DIRECT in an upstream group connects the current node's prepared server. -- Routing: ordered `DOMAIN`, `DOMAIN-SUFFIX`, `DOMAIN-KEYWORD`, `GEOSITE`, `GEOIP`, `IP-CIDR`, `IP-CIDR6`, `DST-PORT`, `NETWORK`, and final `MATCH` rules. -- DNS: fixed-IP UDP/TCP nameservers, explicit outbounds, ordered policy/failover, typed and opaque caches, singleflight, and TUN UDP/TCP port 53 interception. -- TUN: raw IPv4/IPv6, TCP/UDP, local ICMPv4/ICMPv6 Echo replies, HTTP/TLS/QUIC sniffing, and four per-session traffic counters. -- Listeners: HTTP CONNECT/forward with per-request authentication/routing, streaming bodies, Keep-Alive and Upgrade; SOCKS5 CONNECT and TCP-authorized UDP ASSOCIATE. Local access is unauthenticated by default; opt-in LAN sharing requires one shared username/password. -- GeoData: VCore manages `geosite.dat` and `geoip.dat` under `dataDir/geodata`, loads them on demand, and can update them through a proxy chain. -- Delay measurement: `measureDelay` accepts 1–5 node-only configurations per call, uses up to five private workers, and preserves input order in its results. -- TLS: independent certificate pins and four opt-in ClientHello templates (Chrome120/133, Firefox120, Safari16.0) over TCP TLS/REALITY/JLS; see [public names, scope and limitations](docs/tls-client-fingerprint.md). [VLESS JLS](docs/vless.md#jls) uses separate shared credentials with complete native TLS authentication. [Static ECH](docs/vless.md#静态-ech) uses explicit VLESS main/download-leg configuration with no dynamic DNS lookup or fallback. +- **Accept application and VPN traffic:** HTTP forwarding, CONNECT and Upgrade; SOCKS5 CONNECT and UDP ASSOCIATE; host-provided IPv4/IPv6 TUN packets. +- **Route by destination:** domain, domain suffix/keyword, IP CIDR, destination port, TCP/UDP, GeoSite and GeoIP rules, with explicit DIRECT and REJECT actions. +- **Choose and chain proxies:** nested `select` groups, live group selection, and `dialer-proxy` chains that can reference nodes or groups. Selection changes apply to new physical transports without moving existing connections. +- **Handle DNS:** controlled UDP/TCP nameservers, GeoSite-based nameserver policies, sequential failover, caching and duplicate-query coalescing; intercept TCP/UDP port 53 in TUN mode. +- **Identify traffic for routing:** HTTP, TLS and QUIC domain sniffing, plus TUN DNS hints, without rewriting the actual destination. ICMPv4/ICMPv6 Echo is answered locally. +- **Manage routing assets:** load referenced categories from `geosite.dat` / `geoip.dat` on demand and update them through the configured route. GeoSite supports Domain, Full, Plain and Regex records, attribute intersections and inverted selectors; GeoIP supports inverted selectors. All platforms retain selected records without a count cap or truncation; GeoData has no total memory quota. See [GeoData boundaries](docs/geodata.md#内存与安全边界). +- **Expose client controls:** a loopback Controller for group selection and TUN traffic rates/totals, plus isolated node/chain delay measurement through Invoke API. -## Configuration +## Proxy protocols -[`docs/config.yaml`](docs/config.yaml) is the only complete example. Key constraints: +All eight protocols below are enabled in the default build. UDP support is configured per node. -- YAML is limited to 256 KiB and rejects unknown fields, anchors, aliases, custom tags, and obsolete structures. -- The top level must contain at least one proxy and an enabled `port`, `socks-port`, or `tun`. -- Proxy and group definition names share one exact, case-sensitive namespace. Names are 1–64 UTF-8 bytes, reject surrounding Unicode whitespace, controls, `, # / ? & = % \`, `.` and `..`, and reserve `DIRECT`, `REJECT`, and `RULES`; internal ordinary spaces, CJK, and emoji are allowed. -- `proxy-groups` accepts only `select`. Member order and duplicates are preserved; an omitted `default-selected` selects the first member, while an explicit value must name a direct member. All node upstreams and group members, including unselected members, must form one acyclic graph. -- `rules` is required and must end with exactly one `MATCH` targeting a configured proxy node or proxy group. -- `DIRECT` and `REJECT` are built-in actions and group members; every other route target must be a configured proxy node or proxy group. DNS alone also reserves `RULES`. -- Configuration is delivered inline through `configYaml` / `configYamls`; VCore does not read host configuration paths. -- Runtime values such as the Controller, TUN fd, Controller port, and secret are generated by the host and are not stored in user RAW YAML. +| Protocol | Capabilities | +| --- | --- | +| [VLESS](docs/vless.md) | TCP, WebSocket / HTTPUpgrade, gRPC, HTTP first-packet camouflage, legacy H2, [XHTTP H1/H2/H3](docs/xhttp.md); Vision, Encryption, REALITY, JLS, static ECH and sing-mux in supported combinations | +| [VMess AEAD](docs/outbounds.md#vmess-aead) | TCP, WebSocket / HTTPUpgrade, gRPC, HTTP first-packet camouflage and legacy H2; TCP/UDP, plaintext or standard TLS | +| [Trojan](docs/outbounds.md#trojan) | TCP/UDP over TLS TCP, WebSocket / HTTPUpgrade or gRPC | +| [Shadowsocks 2022](docs/outbounds.md#shadowsocks-2022) | TCP/UDP; AES-128-GCM, AES-256-GCM and ChaCha20-Poly1305; AES identity chains; optional strict ShadowTLS v3 TCP wrapping and UoT v2 | +| [AnyTLS](docs/outbounds.md#anytls) | TLS sessions and UDP over TCP v2 | +| [SOCKS5](docs/outbounds.md#socks5) | CONNECT and UDP ASSOCIATE, with optional username/password authentication | +| [Hysteria2](docs/outbounds.md#hysteria2) | QUIC TCP/UDP, bandwidth control, Salamander, port hopping and mTLS | +| [TUIC v5](docs/outbounds.md#tuic-v5) | QUIC TCP/UDP, native/quic UDP relay modes and selectable congestion control | -## Lifecycle and ABI +Standard TLS connections support certificate verification and SHA-256 certificate pins. Applicable TCP TLS paths can use Chrome, Firefox or Safari ClientHello templates; `client-fingerprint` is independent of the certificate `fingerprint`. See [TLS profiles and certificate policy](docs/tls-client-fingerprint.md) for the exact names and combinations. These features do not promise indistinguishability from a browser or unrestricted protocol combinations. -Cross-platform entry points: +SS2022 uses the unmodified official Rust library; its known empty-first-write/server-first and padding risks remain documented in the [Shadowsocks contract](docs/outbounds.md#shadowsocks-2022). + +## Mihomo-style configuration + +Use the familiar `proxies`, `proxy-groups`, `rules`, `dns`, `port`, `socks-port` and `tun` structures. For example: + +```yaml +socks-port: 1080 +allow-lan: false + +proxies: + - name: edge + type: anytls + server: proxy.example.com + port: 443 + password: replace-with-your-password + client-fingerprint: chrome + udp: true + +proxy-groups: + - name: Proxy + type: select + proxies: [edge, DIRECT] + +dns: + enable: true + nameserver: + - "udp://223.5.5.5:53#DIRECT" + +rules: + - GEOSITE,cn,DIRECT + - GEOIP,cn,DIRECT,no-resolve + - MATCH,Proxy +``` + +Replace the example endpoint and credentials. GeoSite/GeoIP rules require the corresponding assets under `/geodata`; missing assets leave those rule types unavailable. See the [complete configuration reference](docs/config.yaml) and [GeoData behavior](docs/geodata.md). + +Compatibility is scoped to documented fields and behavior, not arbitrary Mihomo configurations. Groups currently support static `select`; DNS nameservers use literal IPs over UDP/TCP. Providers, automatic group selection, encrypted DNS and fake-IP are outside the current feature set. Unknown fields and invalid combinations are rejected rather than silently ignored; VCore-specific semantics are called out in the relevant contracts. + +The host passes YAML inline through `configYaml`; TUN descriptors and platform callbacks are supplied separately through the runtime API. VCore does not read host configuration paths or configure Linux system routes automatically. + +## Platforms and integration + +| Platform | TUN integration | +| --- | --- | +| iOS / macOS | Host-provided utun file descriptor | +| tvOS 17+ | Host-provided utun file descriptor; ARM64 device and simulator targets | +| Android | `VpnService` file descriptor with outbound socket protection | +| Linux | Real single-queue raw-IP TUN; the host owns interface creation and routing isolation | +| Windows | Native `Windows.Networking.Vpn` Provider and a full-trust Session Host, without Wintun or fd emulation | + +VCore is a library, not a standalone VPN application. Unix hosts own the original TUN descriptor; VCore uses and closes its own duplicate. Apple's public packetFlow API does not guarantee raw-fd access, so actual Network Extension integration and device validation remain host responsibilities. See [TUN integration](docs/tun-platform.md) and [platform acceptance boundaries](docs/acceptance.md). + +The cross-platform C ABI accepts JSON requests through Invoke API v5: ```c char *VCoreInvoke(const char *request_json); void VCoreFree(char *response); ``` -Windows packages also use the revision-3 host bridge for the all-app VPN policy, profiles, Session Snapshots, and the optional session backend: +One public instance follows `initialize → createInstance → prepare(configYaml) → start → stop → destroyInstance`. The API also provides configuration validation, state queries, GeoData status and delay measurement. See [Invoke API](docs/invoke-api.md), [Controller API](docs/controller-api.md) and the [Windows integration example](example/windows-uwp/README.md). -```c -char *VCoreWindowsVpnInvoke(const char *request_json); -``` +## Benchmark -The public runtime has one instance: +[**VCore / Mihomo TUN benchmark**](https://github.com/OneXray/container-benchmark) contains the reproducible setup, measured results and comparison charts for both cores under the same native Linux TUN environment. -```text -initialize - -> createInstance - -> prepare(configYaml) - -> start - -> stop - -> destroyInstance -``` +The benchmark project also owns protocol interoperability (`interop`) and memory-pressure runs (`stress`), with an explicit `--source vcore=PATH` checkout. VCore's own scripts only compile core and platform artifacts. -`instanceId` is a generation token that is never reused by the current runtime. Commands for the same instance fail fast when another command is active; pure `validateConfig` calls may run concurrently. See [`docs/invoke-api.md`](docs/invoke-api.md) for the complete envelope, methods, fd ownership, and Android protect contract. +It measures **1 / 1.5 / 2 Gbps** mixed TCP/UDP traffic with **1,000 DNS queries/s** and enhanced `geosite:cn` / `geoip:cn` rules, reporting actual throughput, CPU, observed peak Linux RSS, UDP packet loss and successful DNS queries. It evaluates the TUN/DNS/routing path with DIRECT egress, not encrypted proxy throughput; Linux RSS is not Apple Network Extension footprint. -Runtime state is exposed through a session-local loopback Controller: - -```http -GET /traffic -GET /group -GET /group/{name} -GET /proxies/{name} -PUT /proxies/{name} -Authorization: Bearer -``` - -`GET /traffic` is a one-time `up/down/upTotal/downTotal` TUN snapshot. The group endpoints expose and change the selected direct member of static `select` groups; a successful change affects only new physical TCP, UDP, and DNS transports in the current session. It does not migrate existing connections, UDP associations, DNS state, or pooled TCP transports, and it never performs automatic failover. A Controller that manages groups requires one Bearer secret for all routes and may run without TUN. See [`docs/controller-api.md`](docs/controller-api.md). - -An authenticated Hysteria2 session keeps its upstream selection during port hopping. A replacement socket for that same QUIC session does not resample the group; only a new authenticated session does. - -## Platforms - -| Platform | Data plane | Status | -| --- | --- | --- | -| iOS / macOS | The host provides a utun fd; VCore duplicates it and uses a synchronous `rust-tun` device with Tokio `AsyncFd` | Implemented; the release iOS device footprint remains a release gate | -| Android | `VpnService` provides a raw-IP fd; every outbound socket must pass the protect callback first | Implemented; the physical-device matrix remains a release gate | -| Windows | `Windows.Networking.Vpn` AppContainer Provider plus one full-trust runtime per session; package-local named pipes carry raw IP | A development-signed Windows 11 ARM64 package passed functionality, lifecycle, pressure, and bounded-batching acceptance | -| Linux | — | Unsupported; startup fails closed | - -Windows does not use an fd emulation layer. The Provider owns only `VpnChannel`, buffers, routes, physical-network monitoring, the packet gateway, and fail-closed Stop. The complete VCore runtime, Controller, DNS, rules, and outbounds live in the Session Host. The packet channel keeps protocol-v1 framing and batches at most eight already-ready frames without waiting for future packets. - -## Resource Bounds - -Safety is enforced through per-object queues, buffers, parser limits, deadlines and owned cancellation, not a global business-flow quota. See the [resource policy](docs/runtime-resource-policy.md) for shared limits and protocol-specific contracts for local budgets. Memory telemetry does not change lifecycle results. +The separate `stress` command loads complete enhanced GeoData assets by default, with attribute/inversion witnesses and a 2 Gbps / 60-second / 1,000 DNS QPS workload. The benchmark README records actual input types, measurements and failures. Optional record sizing changes only the benchmark input, never production behavior. No observation is a 50,000,000-byte guarantee for arbitrary input or a substitute for Apple device validation. ## Documentation - [Documentation index](docs/README.md) -- [Configuration](docs/config.yaml) -- [Invoke API](docs/invoke-api.md) -- [Build and test](scripts/README.md) -- [Acceptance boundaries](docs/acceptance.md) - -## Example - -- [Minimal Windows UWP VPN integration](example/windows-uwp/README.md): a same-package Provider, Session Host, full-trust foreground host, MSIX manifest, and runnable command-line demo. - -## Validation - -```bash -cargo fmt --all -- --check -uv run --project scripts --locked vcore-scripts check core --profile debug -cargo clippy --locked --all-features --lib --bins -- -D warnings -cargo test --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -cargo clippy --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -- -D warnings -uv run --project scripts --locked vcore-scripts check c-header -uv run --project scripts --locked vcore-scripts check tls-dependencies -uv run --project scripts --locked python -m unittest discover -s scripts/tests -uv run --project scripts --locked ruff check scripts -uv run --project scripts --locked ruff format --check scripts -``` - -Platform artifacts (see [`scripts/README.md`](scripts/README.md) for complete commands and environment variables): - -```bash -uv run --project scripts --locked vcore-scripts build apple -uv run --project scripts --locked vcore-scripts build android -uv run --project scripts --locked vcore-scripts build windows -``` - -See [`docs/acceptance.md`](docs/acceptance.md) for the current validated scope and deferred physical-device and Windows release gates. +- [Configuration reference](docs/config.yaml) +- [Core and platform builds](scripts/README.md) +- [Core regression tests](tests/README.md) +- [Resource policy](docs/runtime-resource-policy.md) +- [Acceptance and known limitations](docs/acceptance.md) ## Credits -VCore's dependencies, maintained forks, public API/protocol references, architectural references, and interoperability counterparts include: +VCore builds on and learns from public dependencies, protocol implementations and platform references: -- [smoltcp](https://github.com/smoltcp-rs/smoltcp), [clash-rs](https://github.com/Watfaq/clash-rs), and [netstack-smoltcp](https://github.com/automesh-network/netstack-smoltcp): userspace IP stacks and TUN netstacks. -- [windows-rs](https://github.com/microsoft/windows-rs), [UWP VPN Plugin Sample](https://github.com/microsoft/UwpVpnPluginSample), [wireguard-uwp-rs](https://github.com/luqmana/wireguard-uwp-rs), [Maple](https://github.com/YtFlow/Maple), and [YtFlowCore](https://github.com/YtFlow/YtFlowCore): Windows VPN, WinRT activation, and packet flow. -- [Xray-core](https://github.com/XTLS/Xray-core), [Mihomo](https://github.com/MetaCubeX/mihomo), and [Leaf](https://github.com/eycorsican/leaf): proxy protocols, routing, TUN architecture, and interoperability references. -- [rustls](https://github.com/rustls/rustls): unprofiled TLS/QUIC and shared WebPKI certificate policy. -- [boring](https://github.com/cloudflare/boring) and [BoringSSL](https://boringssl.googlesource.com/boringssl/): upstreams of the maintained fork for named ClientHello profiles and classic / opt-in hybrid REALITY. -- [shadowsocks-rust](https://github.com/shadowsocks/shadowsocks-rust): the unmodified SS 2022 protocol dependency and source of the derived UDP replay window; see the [MIT notices in the source header](src/outbound/shadowsocks/packet_window.rs). +- Networking and routing: [smoltcp](https://github.com/smoltcp-rs/smoltcp), [clash-rs](https://github.com/Watfaq/clash-rs), [netstack-smoltcp](https://github.com/automesh-network/netstack-smoltcp), [Mihomo](https://github.com/MetaCubeX/mihomo), [Xray-core](https://github.com/XTLS/Xray-core) and [Leaf](https://github.com/eycorsican/leaf). +- TLS and Shadowsocks: [rustls](https://github.com/rustls/rustls), [boring](https://github.com/cloudflare/boring), [BoringSSL](https://boringssl.googlesource.com/boringssl/) and [shadowsocks-rust](https://github.com/shadowsocks/shadowsocks-rust). Derived replay-window code retains its [MIT notices](src/outbound/shadowsocks/packet_window.rs). +- Windows integration: [windows-rs](https://github.com/microsoft/windows-rs), [UWP VPN Plugin Sample](https://github.com/microsoft/UwpVpnPluginSample), [wireguard-uwp-rs](https://github.com/luqmana/wireguard-uwp-rs), [Maple](https://github.com/YtFlow/Maple) and [YtFlowCore](https://github.com/YtFlow/YtFlowCore). ## License -VCore is licensed under the [MIT License](LICENSE). +[MIT](LICENSE). diff --git a/crates/vcore-netstack/README.md b/crates/vcore-netstack/README.md index b62fe85..5dc89dc 100644 --- a/crates/vcore-netstack/README.md +++ b/crates/vcore-netstack/README.md @@ -16,7 +16,42 @@ VCore does not assume the host process role: The repository-root `LICENSE` covers this crate. Upstream projects are credited in the repository `README.md`. -## Main API +## TCP/ICMP driver and UDP codec + +`NetStack::start_tcp(config)` returns `TcpNetStackParts` directly. It creates +bounded raw ingress, raw output and TCP-accept endpoints, but no shared UDP +datagram queue. The TUN runtime can own UDP association admission and use the +pure codec without sending ordinary UDP through the TCP driver: + +```rust,ignore +let vcore_netstack::TcpNetStackParts { + packet_sink, + mut packet_stream, + tcp_listener, + control, + stats, +} = vcore_netstack::NetStack::start_tcp(config)?; + +if let Some(view) = vcore_netstack::parse_udp_packet_view(raw_ip_packet.data()) { + // Admit the association first; copy view.payload only for async ownership. +} else { + packet_sink.try_send(raw_ip_packet)?; +} + +let mut frame = Vec::with_capacity(mtu); +vcore_netstack::encode_udp_packet_into(&response_datagram, mtu, &mut frame)?; +// The platform's single writer can write this frame or a driver output packet. +let raw = packet_stream.try_recv(); +control.stop().await; +``` + +The borrowed parser checks IPv4/IPv6 and UDP header lengths and supports direct +UDP next headers only. It preserves the existing policy: no additional checksum, +fragment or IPv6 extension-header validation. Encoding emits checksums, reuses +the caller's `Vec`, and leaves it unchanged on address-family or MTU errors. +UDP mistakenly sent into the TCP-only driver is ignored. + +## Generic TCP and UDP API ```rust,ignore let stack = vcore_netstack::NetStack::start(config)?; @@ -36,4 +71,29 @@ control.stop().await; ``` `NetStackControl::stop()` returns only after the driver has released all -smoltcp sockets and woken pending TCP operations. +smoltcp sockets and woken pending TCP operations. Dropping the sole +`PacketStream` also stops the driver and releases all endpoints. + +## Driver buffering and scheduling + +Device ingress holds at most one pending packet, not another packet queue. +smoltcp TX tokens reserve capacity in the existing bounded raw output queue +before consuming ingress or allocating an output packet. Consuming a token +sends directly into that queue; dropping an unused token returns its permit. +There is no intermediate device TX backlog. TCP protocol state retains pending +output, including resets, until a token is available. Low-priority ICMP echo +replies are dropped when output is full, without waiting or retaining a reply. + +`PacketStream::recv`, `recv_batch` and `try_recv` wake the driver when they +restore output capacity. An immediate protocol timer is deferred while output +is full, avoiding a zero-delay retry loop; the wakeup resumes it promptly. +Bounded periodic polling still performs lifecycle maintenance. + +The driver consumes at most eight already-ready ingress packets before full +socket maintenance. TCP ingress still advances packet by packet; cancellation +or a full output queue leaves the unconsumed suffix in the existing ingress +queue. One TCP packet may remain in the device pending output capacity. +It neither waits to fill a batch nor introduces another packet queue. +Cooperative scheduling is charged per consumed packet, including the ready +suffix. TCP egress can be deferred to batch maintenance; relative ordering with +an immediate ICMP reply is not guaranteed. diff --git a/crates/vcore-netstack/src/device.rs b/crates/vcore-netstack/src/device.rs index 5ee07c4..dbfc78d 100644 --- a/crates/vcore-netstack/src/device.rs +++ b/crates/vcore-netstack/src/device.rs @@ -1,75 +1,59 @@ -use std::collections::VecDeque; - use smoltcp::{ phy::{Device, DeviceCapabilities, Medium, RxToken, TxToken}, time::Instant, }; +use tokio::sync::mpsc; use crate::Packet; -/// A raw-IP smoltcp device with fixed-depth ingress and egress queues. +/// A raw-IP device with one pending ingress and reserved bounded output slots. pub(crate) struct RawIpDevice { - rx: VecDeque, - tx: VecDeque, - queue_limit: usize, + rx: Option, + output: mpsc::Sender, + emitted: u64, capabilities: DeviceCapabilities, } impl RawIpDevice { - pub(crate) fn new(mtu: usize, queue_limit: usize) -> Self { + pub(crate) fn new(mtu: usize, output: mpsc::Sender) -> Self { let mut capabilities = DeviceCapabilities::default(); capabilities.max_transmission_unit = mtu; capabilities.medium = Medium::Ip; Self { - rx: VecDeque::with_capacity(queue_limit), - tx: VecDeque::with_capacity(queue_limit), - queue_limit, + rx: None, + output, + emitted: 0, capabilities, } } pub(crate) fn push_rx(&mut self, packet: Packet) -> Result<(), Packet> { - if self.rx.len() == self.queue_limit { + if self.rx.is_some() { Err(packet) } else { - self.rx.push_back(packet); + self.rx = Some(packet); Ok(()) } } pub(crate) fn rx_is_empty(&self) -> bool { - self.rx.is_empty() + self.rx.is_none() } - pub(crate) fn tx_checkpoint(&self) -> usize { - self.tx.len() + pub(crate) fn pending_rx(&self) -> Option<&Packet> { + self.rx.as_ref() } - pub(crate) fn pop_tx_after(&mut self, checkpoint: usize) -> Option { - debug_assert!(self.tx.len() <= checkpoint + 1); - if self.tx.len() > checkpoint { - self.tx.pop_back() - } else { - None - } + pub(crate) fn discard_rx(&mut self) { + self.rx = None; } - pub(crate) fn pop_tx(&mut self) -> Option { - self.tx.pop_front() - } - - pub(crate) fn push_tx_front(&mut self, packet: Packet) { - debug_assert!(self.tx.len() < self.queue_limit); - self.tx.push_front(packet); + pub(crate) fn emitted(&self) -> u64 { + self.emitted } pub(crate) fn tx_is_full(&self) -> bool { - self.tx.len() == self.queue_limit - } - - #[cfg(test)] - pub(crate) fn queue_lengths(&self) -> (usize, usize) { - (self.rx.len(), self.tx.len()) + self.output.capacity() == 0 } } @@ -78,23 +62,23 @@ impl Device for RawIpDevice { type TxToken<'a> = RawTxToken<'a>; fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { - if self.tx.len() == self.queue_limit { - return None; - } - let packet = self.rx.pop_front()?; + // Reserve before consuming ingress: smoltcp can need an immediate + // response, and concurrent generic UDP senders share this capacity. + let permit = self.output.try_reserve().ok()?; + let packet = self.rx.take()?; Some(( RawRxToken(packet), RawTxToken { - queue: &mut self.tx, - queue_limit: self.queue_limit, + permit, + emitted: &mut self.emitted, }, )) } fn transmit(&mut self, _timestamp: Instant) -> Option> { - (self.tx.len() < self.queue_limit).then_some(RawTxToken { - queue: &mut self.tx, - queue_limit: self.queue_limit, + Some(RawTxToken { + permit: self.output.try_reserve().ok()?, + emitted: &mut self.emitted, }) } @@ -115,8 +99,8 @@ impl RxToken for RawRxToken { } pub(crate) struct RawTxToken<'a> { - queue: &'a mut VecDeque, - queue_limit: usize, + permit: mpsc::Permit<'a, Packet>, + emitted: &'a mut u64, } impl TxToken for RawTxToken<'_> { @@ -124,10 +108,10 @@ impl TxToken for RawTxToken<'_> { where F: FnOnce(&mut [u8]) -> R, { - debug_assert!(self.queue.len() < self.queue_limit); let mut bytes = vec![0_u8; len]; let result = f(&mut bytes); - self.queue.push_back(Packet::new(bytes)); + self.permit.send(Packet::new(bytes)); + *self.emitted = self.emitted.wrapping_add(1); result } } @@ -138,11 +122,69 @@ mod tests { #[test] fn full_tx_queue_does_not_consume_rx() { - let mut device = RawIpDevice::new(1_500, 1); + let (output, mut receiver) = mpsc::channel(1); + let mut device = RawIpDevice::new(1_500, output.clone()); device.push_rx(Packet::new(vec![0x45; 40])).unwrap(); - device.tx.push_back(Packet::new(vec![0x45; 40])); + output.try_send(Packet::new(vec![0x45; 40])).unwrap(); assert!(device.receive(Instant::now()).is_none()); - assert_eq!(device.queue_lengths(), (1, 1)); + assert!(!device.rx_is_empty()); + assert!(device.tx_is_full()); + receiver.try_recv().unwrap(); + let (rx, tx) = device.receive(Instant::now()).unwrap(); + rx.consume(|bytes| assert_eq!(bytes, &[0x45; 40])); + drop(tx); + assert!(device.rx_is_empty()); + assert!(!device.tx_is_full()); + } + + #[test] + fn unused_tx_permit_returns_capacity_without_a_packet() { + let (output, mut receiver) = mpsc::channel(1); + let mut device = RawIpDevice::new(1_500, output.clone()); + let token = device.transmit(Instant::now()).unwrap(); + assert_eq!(output.capacity(), 0); + drop(token); + assert_eq!(output.capacity(), 1); + assert_eq!(device.emitted(), 0); + assert!(receiver.try_recv().is_err()); + } + + #[test] + fn consumed_tx_permit_sends_directly_to_the_only_output_queue() { + let (output, mut receiver) = mpsc::channel(1); + let mut device = RawIpDevice::new(1_500, output); + device + .transmit(Instant::now()) + .unwrap() + .consume(4, |bytes| bytes.copy_from_slice(&[0x45, 1, 2, 3])); + assert_eq!(device.emitted(), 1); + assert!(device.tx_is_full()); + assert_eq!(receiver.try_recv().unwrap().data(), &[0x45, 1, 2, 3]); + assert!(!device.tx_is_full()); + assert!(receiver.try_recv().is_err()); + } + + #[test] + fn one_pending_ingress_is_not_overwritten() { + let (output, _receiver) = mpsc::channel(1); + let mut device = RawIpDevice::new(1_500, output); + device.push_rx(Packet::new(vec![0x45, 1])).unwrap(); + let rejected = device.push_rx(Packet::new(vec![0x45, 2])).unwrap_err(); + assert_eq!(rejected.data(), &[0x45, 2]); + let (rx, tx) = device.receive(Instant::now()).unwrap(); + rx.consume(|bytes| assert_eq!(bytes, &[0x45, 1])); + drop(tx); + } + + #[test] + fn closed_output_never_consumes_pending_ingress() { + let (output, receiver) = mpsc::channel(1); + let mut device = RawIpDevice::new(1_500, output); + device.push_rx(Packet::new(vec![0x45; 40])).unwrap(); + drop(receiver); + assert!(device.receive(Instant::now()).is_none()); + assert!(device.transmit(Instant::now()).is_none()); + assert!(!device.rx_is_empty()); } } diff --git a/crates/vcore-netstack/src/lib.rs b/crates/vcore-netstack/src/lib.rs index 36a375a..6d4b310 100644 --- a/crates/vcore-netstack/src/lib.rs +++ b/crates/vcore-netstack/src/lib.rs @@ -17,7 +17,10 @@ pub use config::{ConfigError, NetStackConfig}; pub use packet::Packet; pub use stack::{ NetStack, NetStackControl, NetStackError, NetStackParts, NetStackStats, PacketSink, - PacketStream, ResourceSnapshot, + PacketStream, ResourceSnapshot, TcpNetStackParts, }; pub use tcp::{TcpListener, TcpStream}; -pub use udp::{UdpDatagram, UdpError, UdpSocket}; +pub use udp::{ + UdpDatagram, UdpError, UdpPacketView, UdpSender, UdpSocket, encode_udp_packet_into, + parse_udp_packet_view, +}; diff --git a/crates/vcore-netstack/src/stack.rs b/crates/vcore-netstack/src/stack.rs index 092a7a9..6a09b95 100644 --- a/crates/vcore-netstack/src/stack.rs +++ b/crates/vcore-netstack/src/stack.rs @@ -9,9 +9,7 @@ use std::{ }; use smoltcp::{ - iface::{ - Config as InterfaceConfig, Interface, PollIngressSingleResult, SocketHandle, SocketSet, - }, + iface::{Config as InterfaceConfig, Interface, SocketHandle, SocketSet}, socket::tcp, time::Instant, wire::{ @@ -32,6 +30,8 @@ use crate::{ udp::{UdpDatagram, UdpSocket, parse_udp_packet}, }; +const RAW_INGRESS_BATCH: usize = 8; + /// Running stack and all of its application-facing endpoints. pub struct NetStack { parts: NetStackParts, @@ -47,6 +47,15 @@ pub struct NetStackParts { pub stats: NetStackStats, } +/// TCP/ICMP endpoints without allocating a shared UDP ingress queue. +pub struct TcpNetStackParts { + pub packet_sink: PacketSink, + pub packet_stream: PacketStream, + pub tcp_listener: TcpListener, + pub control: NetStackControl, + pub stats: NetStackStats, +} + impl NetStack { /// Starts one netstack driver on the current Tokio runtime. /// @@ -55,6 +64,35 @@ impl NetStack { /// Returns [`NetStackError::Config`] for inconsistent bounds and /// [`NetStackError::NoRuntime`] when called outside a Tokio runtime. pub fn start(config: NetStackConfig) -> Result { + Self::start_parts(config, true).map(|(parts, udp_socket)| Self::with_udp(parts, udp_socket)) + } + + fn with_udp(parts: TcpNetStackParts, udp_socket: Option) -> Self { + Self { + parts: NetStackParts { + packet_sink: parts.packet_sink, + packet_stream: parts.packet_stream, + tcp_listener: parts.tcp_listener, + udp_socket: udp_socket.expect("UDP endpoints requested"), + control: parts.control, + stats: parts.stats, + }, + } + } + + /// Starts only the TCP/ICMP driver; callers handle UDP with the pure codec. + /// + /// # Errors + /// + /// Returns the same configuration and runtime errors as [`Self::start`]. + pub fn start_tcp(config: NetStackConfig) -> Result { + Self::start_parts(config, false).map(|(parts, _)| parts) + } + + fn start_parts( + config: NetStackConfig, + with_udp: bool, + ) -> Result<(TcpNetStackParts, Option), NetStackError> { config.validate()?; tokio::runtime::Handle::try_current().map_err(|_| NetStackError::NoRuntime)?; @@ -64,7 +102,20 @@ impl NetStack { let (raw_inbound_tx, raw_inbound_rx) = mpsc::channel(config.packet_queue); let (raw_outbound_tx, raw_outbound_rx) = mpsc::channel(config.packet_queue); let (tcp_accept_tx, tcp_accept_rx) = mpsc::channel(config.tcp_accept_queue); - let (udp_tx, udp_rx) = mpsc::channel(config.udp_queue); + let (udp_tx, udp_socket) = if with_udp { + let (sender, receiver) = mpsc::channel(config.udp_queue); + ( + Some(sender), + Some(UdpSocket { + receiver, + raw_outbound: raw_outbound_tx.clone(), + cancellation: cancellation.clone(), + mtu: config.mtu, + }), + ) + } else { + (None, None) + }; let (stopped_tx, stopped_rx) = watch::channel(false); let mtu = config.mtu; @@ -75,14 +126,14 @@ impl NetStack { tcp_accept_tx, udp_tx, cancellation.clone(), - notify, + notify.clone(), stats.clone(), stopped_tx, ); tokio::spawn(driver.run()); - Ok(Self { - parts: NetStackParts { + Ok(( + TcpNetStackParts { packet_sink: PacketSink { sender: raw_inbound_tx, cancellation: cancellation.clone(), @@ -91,24 +142,20 @@ impl NetStack { packet_stream: PacketStream { receiver: raw_outbound_rx, cancellation: cancellation.clone(), + notify, }, tcp_listener: TcpListener { receiver: tcp_accept_rx, cancellation: cancellation.clone(), }, - udp_socket: UdpSocket { - receiver: udp_rx, - raw_outbound: raw_outbound_tx, - cancellation: cancellation.clone(), - mtu, - }, control: NetStackControl { cancellation, stopped: stopped_rx, }, stats, }, - }) + udp_socket, + )) } #[must_use] @@ -167,15 +214,71 @@ impl PacketSink { pub struct PacketStream { receiver: mpsc::Receiver, cancellation: CancellationToken, + notify: Arc, } impl PacketStream { pub async fn recv(&mut self) -> Option { - tokio::select! { + let packet = tokio::select! { biased; () = self.cancellation.cancelled() => None, packet = self.receiver.recv() => packet, + }; + if packet.is_some() { + self.notify.notify_one(); } + packet + } + + /// Takes an already-ready packet and wakes the driver when capacity returns. + /// + /// # Errors + /// + /// Returns `Empty` when no packet is ready, or `Disconnected` after stop + /// begins or the output channel closes. Stop never consumes queued packets. + pub fn try_recv(&mut self) -> Result { + if self.cancellation.is_cancelled() { + return Err(mpsc::error::TryRecvError::Disconnected); + } + let packet = self.receiver.try_recv()?; + self.notify.notify_one(); + Ok(packet) + } + + /// Waits for one packet, then drains only packets that are already ready. + /// + /// Reuses and clears `packets` without creating another queue. A closed + /// queue returns any received prefix; cancellation discards that prefix + /// and returns zero because no more packets may cross the stopping TUN. + /// Dropping this future while waiting for the first packet is cancel-safe. + /// + /// # Panics + /// + /// Panics if `max_packets` is zero. + pub async fn recv_batch(&mut self, packets: &mut Vec, max_packets: usize) -> usize { + assert!(max_packets > 0, "packet batch limit must be positive"); + packets.clear(); + tokio::select! { + biased; + () = self.cancellation.cancelled() => {} + _ = self.receiver.recv_many(packets, max_packets) => {} + } + if !packets.is_empty() { + self.notify.notify_one(); + } + if self.cancellation.is_cancelled() { + packets.clear(); + } + packets.len() + } +} + +impl Drop for PacketStream { + fn drop(&mut self) { + // Receiver closure has no remaining consumer to return capacity. + // Wake the driver so it observes the closed output immediately. + self.receiver.close(); + self.notify.notify_one(); } } @@ -304,7 +407,7 @@ struct Driver { raw_inbound: mpsc::Receiver, raw_outbound: mpsc::Sender, tcp_accept: mpsc::Sender, - udp_outbound: mpsc::Sender, + udp_outbound: Option>, cancellation: CancellationToken, notify: Arc, stats: NetStackStats, @@ -323,7 +426,7 @@ impl Driver { raw_inbound: mpsc::Receiver, raw_outbound: mpsc::Sender, tcp_accept: mpsc::Sender, - udp_outbound: mpsc::Sender, + udp_outbound: Option>, cancellation: CancellationToken, notify: Arc, stats: NetStackStats, @@ -331,7 +434,7 @@ impl Driver { ) -> Self { let mut interface_config = InterfaceConfig::new(HardwareAddress::Ip); interface_config.random_seed = 0x4f_6e_65_56_43_6f_72_65; - let mut device = RawIpDevice::new(config.mtu, config.packet_queue); + let mut device = RawIpDevice::new(config.mtu, raw_outbound.clone()); let mut interface = Interface::new(interface_config, &mut device, Instant::now()); interface.set_any_ip(true); interface.update_ip_addrs(|addresses| { @@ -384,15 +487,62 @@ impl Driver { biased; () = self.cancellation.cancelled() => break, () = self.notify.notified() => {} - packet = self.raw_inbound.recv(), if !self.device.tx_is_full() => { + // A full output still allows one ingress classification: + // low-priority ICMP drops immediately, while TCP retains its + // one pending packet until output capacity returns. + packet = self.raw_inbound.recv(), if self.device.rx_is_empty() => { let Some(packet) = packet else { break; }; - self.handle_packet(packet); + let batch_count = self.handle_packet_batch(packet); + // recv() already charges the first packet. try_recv() + // does not, so retain the old per-packet cooperative + // accounting without repeating socket maintenance. + for _ in 1..batch_count { + tokio::task::consume_budget().await; + } } () = tokio::time::sleep(delay) => {} } } } + fn handle_packet_batch(&mut self, first: Packet) -> usize { + let mut packet = first; + let mut count = 0; + loop { + if self.cancellation.is_cancelled() { + break; + } + self.handle_packet(packet); + count += 1; + // TCP still enters smoltcp one packet at a time: the next SYN + // must see the previous socket's bound remote endpoint. This also + // keeps queued TCP ingress from spuriously suppressing ICMP echo. + // Socket/app-buffer maintenance and egress polling stay in drive(). + if !self.device.rx_is_empty() { + self.interface.poll_ingress_single( + Instant::now(), + &mut self.device, + &mut self.sockets, + ); + } + if count == RAW_INGRESS_BATCH + || self.cancellation.is_cancelled() + || self.device.tx_is_full() + || !self.device.rx_is_empty() + { + break; + } + // Never prefetch a suffix that might need another queue when TX + // fills. Empty or disconnected ends this batch, not the driver; + // the accepted prefix still receives the next maintenance pass. + let Ok(next) = self.raw_inbound.try_recv() else { + break; + }; + packet = next; + } + count + } + fn handle_packet(&mut self, packet: Packet) { if self.config.fake_icmp_echo { match classify_icmp(&packet) { @@ -410,8 +560,11 @@ impl Driver { match packet_protocol(&packet) { Some(IpProtocol::Tcp) => self.handle_tcp_packet(packet), Some(IpProtocol::Udp) => { + let Some(udp_outbound) = &self.udp_outbound else { + return; + }; if let Some(datagram) = parse_udp_packet(&packet) { - if self.udp_outbound.try_send(datagram).is_err() { + if udp_outbound.try_send(datagram).is_err() { self.stats.0.dropped_udp.fetch_add(1, Ordering::AcqRel); } } else { @@ -426,30 +579,22 @@ impl Driver { } fn handle_icmp_packet(&mut self, packet: Packet) { - if !self.device.rx_is_empty() - || self.device.tx_is_full() - || self.raw_outbound.capacity() == 0 - { + if !self.device.rx_is_empty() || self.device.tx_is_full() { self.stats.0.icmp_dropped.fetch_add(1, Ordering::AcqRel); return; } - // Keep low-priority fake echo replies out of the shared device TX backlog. - // One synchronous ingress poll gives this request at most one immediate reply. - let tx_checkpoint = self.device.tx_checkpoint(); + // Echo remains low priority: one immediate poll, no retained response + // when a concurrent sender consumes the last output slot. + let emitted = self.device.emitted(); if self.device.push_rx(packet).is_err() { self.stats.0.icmp_dropped.fetch_add(1, Ordering::AcqRel); return; } - let result = - self.interface - .poll_ingress_single(Instant::now(), &mut self.device, &mut self.sockets); - debug_assert_ne!(result, PollIngressSingleResult::None); - - let replied = self - .device - .pop_tx_after(tx_checkpoint) - .is_some_and(|reply| self.raw_outbound.try_send(reply).is_ok()); + self.interface + .poll_ingress_single(Instant::now(), &mut self.device, &mut self.sockets); + self.device.discard_rx(); + let replied = self.device.emitted() != emitted; let counter = if replied { &self.stats.0.icmp_replied } else { @@ -468,7 +613,7 @@ impl Driver { return; } if self.device.push_rx(packet).is_err() { - // This can only occur when the fixed device ingress queue is full. + // A pending ingress packet must never be overwritten. self.stats.0.rejected_tcp.fetch_add(1, Ordering::AcqRel); } } @@ -515,7 +660,10 @@ impl Driver { } fn drive(&mut self) { - self.flush_raw_output(); + if self.raw_outbound.is_closed() { + self.cancellation.cancel(); + return; + } if !self.device.tx_is_full() { self.interface .poll(Instant::now(), &mut self.device, &mut self.sockets); @@ -525,10 +673,14 @@ impl Driver { self.interface .poll(Instant::now(), &mut self.device, &mut self.sockets); } - self.flush_raw_output(); } fn drive_tcp_sockets(&mut self) { + let pending_flow = self + .device + .pending_rx() + .and_then(parse_tcp_flow) + .map(|(flow, _, _)| flow); let mut inactive = Vec::new(); for (flow, entry) in &self.tcp_entries { let socket = self.sockets.get_mut::(entry.socket); @@ -590,7 +742,19 @@ impl Driver { { socket.close(); } - if !socket.is_active() { + // smoltcp's abort enters Closed before dispatching RST. Its remote + // endpoint is cleared only once that packet reaches our bounded + // output queue. Keep it until then, including TX backpressure. + let reset_pending = + socket.state() == tcp::State::Closed && socket.remote_endpoint().is_some(); + // A SYN admitted while output is full still awaits device.receive + // and has not transitioned from Listen. Retain only that flow; + // a consumed/invalid SYN must not leave a dormant listener behind. + let ingress_pending = socket.state() == tcp::State::Listen + && pending_flow == Some(*flow) + && !handle.dropped.load(Ordering::Acquire) + && !self.cancellation.is_cancelled(); + if !socket.is_active() && !reset_pending && !ingress_pending { inactive.push(*flow); } } @@ -607,28 +771,19 @@ impl Driver { self.update_flow_stats(); } - fn flush_raw_output(&mut self) { - while let Some(packet) = self.device.pop_tx() { - match self.raw_outbound.try_send(packet) { - Ok(()) => {} - Err(mpsc::error::TrySendError::Full(packet)) => { - self.device.push_tx_front(packet); - break; - } - Err(mpsc::error::TrySendError::Closed(_)) => { - self.cancellation.cancel(); - break; - } - } - } - } - fn next_delay(&mut self) -> Duration { let smoltcp_delay = self .interface .poll_delay(Instant::now(), &self.sockets) .map_or(self.config.max_poll_interval, Into::into); - smoltcp_delay.min(self.config.max_poll_interval) + // A ready egress timer cannot make progress while output is full. + // PacketStream notifies on capacity recovery; retain a bounded timer + // for lifecycle maintenance instead of repeatedly sleeping for zero. + if smoltcp_delay.is_zero() && self.device.tx_is_full() { + self.config.max_poll_interval + } else { + smoltcp_delay.min(self.config.max_poll_interval) + } } fn update_flow_stats(&self) { @@ -651,6 +806,7 @@ impl Driver { fn shutdown(&mut self) { self.cancellation.cancel(); + self.device.discard_rx(); for entry in self.tcp_entries.values() { entry.handle.mark_stopped(); } @@ -707,8 +863,375 @@ fn parse_tcp_flow(packet: &Packet) -> Option<(FlowKey, bool, bool)> { #[cfg(test)] mod tests { + use futures_util::FutureExt; + use super::*; + fn packet_stream() -> (mpsc::Sender, PacketStream, CancellationToken) { + let (sender, receiver) = mpsc::channel(8); + let cancellation = CancellationToken::new(); + let stream = PacketStream { + receiver, + cancellation: cancellation.clone(), + notify: Arc::new(Notify::new()), + }; + (sender, stream, cancellation) + } + + fn packet(id: u8) -> Packet { + Packet::new(vec![0x45, id]) + } + + fn tcp_handshake_packet(sequence: i32, acknowledgement: Option) -> Packet { + let source = Ipv4Address::new(10, 0, 0, 2); + let destination = Ipv4Address::new(192, 0, 2, 1); + let mut ip = Ipv4Packet::new_unchecked(vec![0_u8; 40]); + ip.set_version(4); + ip.set_header_len(20); + ip.set_total_len(40); + ip.set_hop_limit(64); + ip.set_next_header(IpProtocol::Tcp); + ip.set_src_addr(source); + ip.set_dst_addr(destination); + { + let mut tcp = TcpPacket::new_unchecked(ip.payload_mut()); + tcp.set_src_port(12_000); + tcp.set_dst_port(12_001); + tcp.set_header_len(20); + tcp.set_seq_number(smoltcp::wire::TcpSeqNumber(sequence)); + tcp.set_syn(acknowledgement.is_none()); + if let Some(number) = acknowledgement { + tcp.set_ack(true); + tcp.set_ack_number(smoltcp::wire::TcpSeqNumber(number)); + } + tcp.set_window_len(u16::MAX); + tcp.fill_checksum(&source.into(), &destination.into()); + } + ip.fill_checksum(); + Packet::new(ip.into_inner()) + } + + #[tokio::test] + async fn driver_batch_preserves_queued_suffix_when_tcp_ingress_fills_tx() { + let config = NetStackConfig { + packet_queue: 1, + ..NetStackConfig::default() + }; + let (sender, inbound) = mpsc::channel(config.packet_queue); + let (outbound, mut packets) = mpsc::channel(config.packet_queue); + let (accept, _streams) = mpsc::channel(config.tcp_accept_queue); + let (udp, mut datagrams) = mpsc::channel(config.udp_queue); + let (stopped, _) = watch::channel(false); + let mut driver = Driver::new( + config, + inbound, + outbound, + accept, + Some(udp), + CancellationToken::new(), + Arc::new(Notify::new()), + NetStackStats::default(), + stopped, + ); + + sender.try_send(tcp_handshake_packet(100, None)).unwrap(); + let syn = driver.raw_inbound.try_recv().unwrap(); + assert_eq!(driver.handle_packet_batch(syn), 1); + driver.drive(); + let syn_ack = packets.try_recv().unwrap(); + let ip = Ipv4Packet::new_checked(syn_ack.data()).unwrap(); + let tcp = TcpPacket::new_checked(ip.payload()).unwrap(); + assert!(tcp.syn() && tcp.ack()); + // A wrong SYN-ACK acknowledgement produces an immediate ingress RST, + // unlike the initial SYN, whose response requires an egress poll. + let wrong_ack = tcp.seq_number().0.wrapping_add(2); + sender + .try_send(tcp_handshake_packet(101, Some(wrong_ack))) + .unwrap(); + let first = driver.raw_inbound.try_recv().unwrap(); + let witness = UdpDatagram::new( + "10.0.0.2:12000".parse().unwrap(), + "192.0.2.1:12001".parse().unwrap(), + b"retained-suffix".as_slice(), + ); + sender + .try_send(crate::udp::build_udp_packet(&witness, 1_500).unwrap()) + .unwrap(); + assert!(!driver.device.tx_is_full()); + + assert_eq!(driver.handle_packet_batch(first), 1); + assert!(driver.device.tx_is_full()); + assert!(driver.device.rx_is_empty()); + assert_eq!(driver.raw_inbound.len(), 1); + assert!(datagrams.try_recv().is_err()); + + driver.drive(); + let reset = packets.try_recv().unwrap(); + let ip = Ipv4Packet::new_checked(reset.data()).unwrap(); + assert!(TcpPacket::new_checked(ip.payload()).unwrap().rst()); + assert!(!driver.device.tx_is_full()); + let suffix = driver.raw_inbound.try_recv().unwrap(); + assert_eq!(driver.handle_packet_batch(suffix), 1); + assert_eq!(datagrams.try_recv().unwrap(), witness); + driver.drive(); + assert!(datagrams.try_recv().is_err()); + assert!(driver.raw_inbound.is_empty()); + assert!(packets.try_recv().is_err()); + assert_eq!(driver.stats.snapshot().invalid_packets, 0); + assert_eq!(driver.stats.snapshot().rejected_tcp, 0); + } + + #[tokio::test] + async fn driver_batch_drains_a_bounded_ready_prefix_in_order() { + let config = NetStackConfig { + packet_queue: 16, + ..NetStackConfig::default() + }; + let (sender, inbound) = mpsc::channel(config.packet_queue); + let (outbound, _packets) = mpsc::channel(config.packet_queue); + let (accept, _streams) = mpsc::channel(config.tcp_accept_queue); + let (udp, mut datagrams) = mpsc::channel(config.udp_queue); + let (stopped, _) = watch::channel(false); + let mut driver = Driver::new( + config, + inbound, + outbound, + accept, + Some(udp), + CancellationToken::new(), + Arc::new(Notify::new()), + NetStackStats::default(), + stopped, + ); + for id in 0..10_u8 { + let datagram = UdpDatagram::new( + "10.0.0.2:12000".parse().unwrap(), + "192.0.2.1:12001".parse().unwrap(), + vec![id], + ); + sender + .try_send(crate::udp::build_udp_packet(&datagram, 1_500).unwrap()) + .unwrap(); + } + let first = driver.raw_inbound.try_recv().unwrap(); + assert_eq!(driver.handle_packet_batch(first), 8); + assert_eq!(driver.raw_inbound.len(), 2); + assert!(driver.device.rx_is_empty()); + for id in 0..8_u8 { + assert_eq!(datagrams.try_recv().unwrap().payload.as_ref(), &[id]); + } + assert!(datagrams.try_recv().is_err()); + + let first = driver.raw_inbound.try_recv().unwrap(); + assert_eq!(driver.handle_packet_batch(first), 2); + assert!(driver.raw_inbound.is_empty()); + for id in 8..10_u8 { + assert_eq!(datagrams.try_recv().unwrap().payload.as_ref(), &[id]); + } + + // Closed input still has to deliver its ready prefix before exit. + sender.try_send(packet(20)).unwrap(); + drop(sender); + let first = driver.raw_inbound.try_recv().unwrap(); + assert_eq!(driver.handle_packet_batch(first), 1); + assert_eq!(driver.stats.snapshot().invalid_packets, 1); + + // Full output must not prefetch the batch suffix, even for UDP. + for _ in 0..16 { + driver.raw_outbound.try_send(packet(30)).unwrap(); + } + let (sender, receiver) = mpsc::channel(1); + driver.raw_inbound = receiver; + sender.try_send(packet(31)).unwrap(); + assert_eq!(driver.handle_packet_batch(packet(32)), 1); + assert_eq!(driver.raw_inbound.len(), 1); + assert_eq!(driver.stats.snapshot().invalid_packets, 2); + + driver.cancellation.cancel(); + assert_eq!(driver.handle_packet_batch(packet(33)), 0); + assert_eq!(driver.raw_inbound.len(), 1); + assert_eq!(driver.stats.snapshot().invalid_packets, 2); + } + + #[tokio::test] + async fn full_output_defers_an_immediate_reset_timer_without_losing_the_reset() { + let config = NetStackConfig { + packet_queue: 1, + max_poll_interval: Duration::from_secs(5), + ..NetStackConfig::default() + }; + let (_sender, inbound) = mpsc::channel(config.packet_queue); + let (outbound, mut packets) = mpsc::channel(config.packet_queue); + let (accept, mut streams) = mpsc::channel(config.tcp_accept_queue); + let (stopped, _) = watch::channel(false); + let mut driver = Driver::new( + config, + inbound, + outbound, + accept, + None, + CancellationToken::new(), + Arc::new(Notify::new()), + NetStackStats::default(), + stopped, + ); + assert_eq!( + driver.handle_packet_batch(tcp_handshake_packet(100, None)), + 1 + ); + driver.drive(); + packets.try_recv().unwrap(); + let stream = streams.try_recv().unwrap(); + driver.raw_outbound.try_send(packet(0)).unwrap(); + drop(stream); + driver.drive(); + assert!(driver.device.tx_is_full()); + assert_eq!(driver.stats.snapshot().active_tcp, 1); + assert_eq!(driver.next_delay(), driver.config.max_poll_interval); + + assert_eq!(packets.try_recv().unwrap(), packet(0)); + driver.drive(); + let reset = packets.try_recv().unwrap(); + let ip = Ipv4Packet::new_checked(reset.data()).unwrap(); + assert!(TcpPacket::new_checked(ip.payload()).unwrap().rst()); + driver.drive(); + assert_eq!(driver.stats.snapshot().active_tcp, 0); + assert!(packets.try_recv().is_err()); + } + + #[tokio::test] + async fn packet_batch_drains_ready_packets_in_order_and_reuses_storage() { + let (sender, mut stream, _) = packet_stream(); + for id in 0..3 { + sender.try_send(packet(id)).unwrap(); + } + let mut packets = Vec::with_capacity(8); + packets.push(packet(99)); + let storage = packets.as_ptr(); + + assert_eq!(stream.recv_batch(&mut packets, 8).now_or_never(), Some(3)); + assert_eq!(packets, vec![packet(0), packet(1), packet(2)]); + assert_eq!(packets.as_ptr(), storage); + } + + #[tokio::test] + async fn packet_batch_returns_one_ready_packet_without_waiting_to_fill() { + let (sender, mut stream, _) = packet_stream(); + sender.try_send(packet(0)).unwrap(); + let mut packets = Vec::new(); + + assert_eq!(stream.recv_batch(&mut packets, 8).now_or_never(), Some(1)); + assert_eq!(packets, vec![packet(0)]); + } + + #[tokio::test] + async fn packet_batch_respects_the_callers_limit() { + let (sender, mut stream, _) = packet_stream(); + for id in 0..4 { + sender.try_send(packet(id)).unwrap(); + } + let mut packets = Vec::new(); + + assert_eq!(stream.recv_batch(&mut packets, 2).await, 2); + assert_eq!(packets, vec![packet(0), packet(1)]); + assert_eq!(stream.recv_batch(&mut packets, 2).await, 2); + assert_eq!(packets, vec![packet(2), packet(3)]); + } + + #[tokio::test] + async fn packet_batch_returns_a_ready_prefix_when_the_queue_closes() { + let (sender, mut stream, _) = packet_stream(); + sender.try_send(packet(0)).unwrap(); + sender.try_send(packet(1)).unwrap(); + drop(sender); + let mut packets = Vec::new(); + + assert_eq!(stream.recv_batch(&mut packets, 8).await, 2); + assert_eq!(packets, vec![packet(0), packet(1)]); + assert_eq!(stream.recv_batch(&mut packets, 8).await, 0); + assert_eq!(packets, Vec::::new()); + } + + #[tokio::test] + async fn packet_batch_cancellation_takes_priority_over_queued_packets() { + let (sender, mut stream, cancellation) = packet_stream(); + sender.try_send(packet(0)).unwrap(); + cancellation.cancel(); + let mut packets = vec![packet(99)]; + + assert_eq!(stream.recv_batch(&mut packets, 8).await, 0); + assert_eq!(packets, Vec::::new()); + assert_eq!(stream.receiver.len(), 1); + } + + #[tokio::test] + async fn dropping_an_empty_packet_batch_receive_does_not_consume_a_later_packet() { + let (sender, mut stream, _) = packet_stream(); + let mut packets = Vec::new(); + assert!(stream.recv_batch(&mut packets, 8).now_or_never().is_none()); + + sender.try_send(packet(0)).unwrap(); + assert_eq!(stream.recv_batch(&mut packets, 8).await, 1); + assert_eq!(packets, vec![packet(0)]); + } + + #[tokio::test] + async fn cancellation_wakes_an_empty_packet_batch_receive() { + let (_sender, mut stream, cancellation) = packet_stream(); + let mut packets = Vec::new(); + let mut receive = Box::pin(stream.recv_batch(&mut packets, 8)); + assert!(futures_util::poll!(receive.as_mut()).is_pending()); + + cancellation.cancel(); + assert_eq!(receive.await, 0); + assert_eq!(packets, Vec::::new()); + } + + #[tokio::test] + async fn every_output_receive_api_notifies_capacity_recovery() { + let (sender, mut stream, _) = packet_stream(); + let notify = stream.notify.clone(); + assert!(notify.notified().now_or_never().is_none()); + assert_eq!(stream.try_recv(), Err(mpsc::error::TryRecvError::Empty)); + assert!(notify.notified().now_or_never().is_none()); + + sender.try_send(packet(0)).unwrap(); + assert_eq!(stream.try_recv().unwrap(), packet(0)); + assert_eq!(notify.notified().now_or_never(), Some(())); + + sender.try_send(packet(1)).unwrap(); + assert_eq!(stream.recv().await, Some(packet(1))); + assert_eq!(notify.notified().now_or_never(), Some(())); + + sender.try_send(packet(2)).unwrap(); + let mut packets = Vec::new(); + assert_eq!(stream.recv_batch(&mut packets, 8).await, 1); + assert_eq!(packets, vec![packet(2)]); + assert_eq!(notify.notified().now_or_never(), Some(())); + } + + #[tokio::test] + async fn output_try_receive_respects_stop_without_consuming_queued_packets() { + let (sender, mut stream, cancellation) = packet_stream(); + sender.try_send(packet(0)).unwrap(); + cancellation.cancel(); + assert_eq!( + stream.try_recv(), + Err(mpsc::error::TryRecvError::Disconnected) + ); + assert_eq!(stream.receiver.len(), 1); + assert!(stream.notify.notified().now_or_never().is_none()); + } + + #[tokio::test] + async fn output_receiver_drop_closes_capacity_and_wakes_the_driver() { + let (sender, stream, _) = packet_stream(); + let notify = stream.notify.clone(); + drop(stream); + assert!(sender.is_closed()); + assert_eq!(notify.notified().now_or_never(), Some(())); + } + #[test] fn validates_raw_packet_bounds() { assert!(matches!( diff --git a/crates/vcore-netstack/src/udp.rs b/crates/vcore-netstack/src/udp.rs index 1fedb4a..7691af0 100644 --- a/crates/vcore-netstack/src/udp.rs +++ b/crates/vcore-netstack/src/udp.rs @@ -19,6 +19,14 @@ pub struct UdpDatagram { pub payload: Bytes, } +/// Borrowed endpoints and payload of a checked IPv4 or IPv6 UDP packet. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct UdpPacketView<'a> { + pub source: SocketAddr, + pub destination: SocketAddr, + pub payload: &'a [u8], +} + impl UdpDatagram { #[must_use] pub fn new(source: SocketAddr, destination: SocketAddr, payload: impl Into) -> Self { @@ -38,7 +46,25 @@ pub struct UdpSocket { pub(crate) mtu: usize, } +/// A send-only handle sharing the socket's existing bounded TUN output queue. +#[derive(Clone)] +pub struct UdpSender { + raw_outbound: mpsc::Sender, + cancellation: CancellationToken, + mtu: usize, +} + impl UdpSocket { + /// Returns a send-only handle without adding a queue or receiving ownership. + #[must_use] + pub fn sender(&self) -> UdpSender { + UdpSender { + raw_outbound: self.raw_outbound.clone(), + cancellation: self.cancellation.clone(), + mtu: self.mtu, + } + } + /// Receives a datagram from the TUN side. Returns `None` after stop begins. pub async fn recv(&mut self) -> Option { tokio::select! { @@ -56,13 +82,33 @@ impl UdpSocket { /// [`UdpError::MtuExceeded`] for oversized datagrams, or /// [`UdpError::Stopped`] after shutdown begins. pub async fn send(&self, datagram: UdpDatagram) -> Result<(), UdpError> { - let packet = build_udp_packet(&datagram, self.mtu)?; - tokio::select! { - biased; - () = self.cancellation.cancelled() => Err(UdpError::Stopped), - result = self.raw_outbound.send(packet) => { - result.map_err(|_| UdpError::Stopped) - } + send_udp_datagram(&self.raw_outbound, &self.cancellation, self.mtu, datagram).await + } +} + +impl UdpSender { + /// Writes one datagram back to the TUN side with bounded backpressure. + /// + /// # Errors + /// + /// Returns the same address-family, MTU, and stop errors as [`UdpSocket::send`]. + pub async fn send(&self, datagram: UdpDatagram) -> Result<(), UdpError> { + send_udp_datagram(&self.raw_outbound, &self.cancellation, self.mtu, datagram).await + } +} + +async fn send_udp_datagram( + raw_outbound: &mpsc::Sender, + cancellation: &CancellationToken, + mtu: usize, + datagram: UdpDatagram, +) -> Result<(), UdpError> { + let packet = build_udp_packet(&datagram, mtu)?; + tokio::select! { + biased; + () = cancellation.cancelled() => Err(UdpError::Stopped), + result = raw_outbound.send(packet) => { + result.map_err(|_| UdpError::Stopped) } } } @@ -78,35 +124,68 @@ pub enum UdpError { } pub(crate) fn parse_udp_packet(packet: &Packet) -> Option { - match IpVersion::of_packet(packet.data()).ok()? { + let view = parse_udp_packet_view(packet.data())?; + Some(UdpDatagram::new( + view.source, + view.destination, + Bytes::copy_from_slice(view.payload), + )) +} + +/// Parses a UDP packet without copying its payload. +/// +/// Retains the netstack's checked-header policy: direct UDP next headers only, +/// without additional checksum or fragmentation validation. +#[must_use] +pub fn parse_udp_packet_view(bytes: &[u8]) -> Option> { + if bytes.is_empty() { + return None; + } + match IpVersion::of_packet(bytes).ok()? { IpVersion::Ipv4 => { - let ip = Ipv4Packet::new_checked(packet.data()).ok()?; + let ip = Ipv4Packet::new_checked(bytes).ok()?; if ip.next_header() != IpProtocol::Udp { return None; } let udp = UdpPacket::new_checked(ip.payload()).ok()?; - Some(UdpDatagram::new( - SocketAddr::new(IpAddr::from(ip.src_addr()), udp.src_port()), - SocketAddr::new(IpAddr::from(ip.dst_addr()), udp.dst_port()), - Bytes::copy_from_slice(udp.payload()), - )) + Some(UdpPacketView { + source: SocketAddr::new(IpAddr::from(ip.src_addr()), udp.src_port()), + destination: SocketAddr::new(IpAddr::from(ip.dst_addr()), udp.dst_port()), + payload: udp.payload(), + }) } IpVersion::Ipv6 => { - let ip = Ipv6Packet::new_checked(packet.data()).ok()?; + let ip = Ipv6Packet::new_checked(bytes).ok()?; if ip.next_header() != IpProtocol::Udp { return None; } let udp = UdpPacket::new_checked(ip.payload()).ok()?; - Some(UdpDatagram::new( - SocketAddr::new(IpAddr::from(ip.src_addr()), udp.src_port()), - SocketAddr::new(IpAddr::from(ip.dst_addr()), udp.dst_port()), - Bytes::copy_from_slice(udp.payload()), - )) + Some(UdpPacketView { + source: SocketAddr::new(IpAddr::from(ip.src_addr()), udp.src_port()), + destination: SocketAddr::new(IpAddr::from(ip.dst_addr()), udp.dst_port()), + payload: udp.payload(), + }) } } } pub(crate) fn build_udp_packet(datagram: &UdpDatagram, mtu: usize) -> Result { + let mut bytes = Vec::new(); + encode_udp_packet_into(datagram, mtu, &mut bytes)?; + Ok(Packet::new(bytes)) +} + +/// Encodes one checksummed IP/UDP packet, reusing the caller's allocation. +/// +/// # Errors +/// +/// Returns [`UdpError::AddressFamilyMismatch`] or [`UdpError::MtuExceeded`] +/// without changing `frame`. This pure codec is independent of stack shutdown. +pub fn encode_udp_packet_into( + datagram: &UdpDatagram, + mtu: usize, + frame: &mut Vec, +) -> Result<(), UdpError> { if datagram.source.is_ipv4() != datagram.destination.is_ipv4() { return Err(UdpError::AddressFamilyMismatch); } @@ -130,23 +209,151 @@ pub(crate) fn build_udp_packet(datagram: &UdpDatagram, mtu: usize) -> Result (UdpSocket, mpsc::Sender, mpsc::Receiver) { + let (inbound, receiver) = mpsc::channel(1); + let (raw_outbound, output) = mpsc::channel(1); + ( + UdpSocket { + receiver, + raw_outbound, + cancellation: CancellationToken::new(), + mtu: 1_500, + }, + inbound, + output, + ) + } + + fn valid_datagram() -> UdpDatagram { + UdpDatagram::new( + "192.0.2.1:53".parse().unwrap(), + "198.51.100.2:4000".parse().unwrap(), + &b"payload"[..], + ) + } + + #[tokio::test] + async fn sender_backpressure_does_not_block_socket_receive() { + let (mut socket, inbound, mut output) = socket_fixture(); + let sender = socket.sender(); + let datagram = valid_datagram(); + socket.send(datagram.clone()).await.unwrap(); + + let mut blocked_send = Box::pin(sender.send(datagram.clone())); + assert!(futures_util::poll!(&mut blocked_send).is_pending()); + + inbound.try_send(datagram.clone()).unwrap(); + assert_eq!( + futures_util::poll!(Box::pin(socket.recv())), + Poll::Ready(Some(datagram.clone())) + ); + assert!(futures_util::poll!(&mut blocked_send).is_pending()); + + assert_eq!( + parse_udp_packet(&output.try_recv().unwrap()), + Some(datagram.clone()) + ); + assert_eq!(futures_util::poll!(&mut blocked_send), Poll::Ready(Ok(()))); + assert_eq!( + parse_udp_packet(&output.try_recv().unwrap()), + Some(datagram) + ); + } + + #[tokio::test] + async fn stop_unblocks_pending_sender_and_socket_receive() { + let (mut socket, _inbound, _output) = socket_fixture(); + let sender = socket.sender(); + let cancellation = socket.cancellation.clone(); + let datagram = valid_datagram(); + socket.send(datagram.clone()).await.unwrap(); + + let mut blocked_send = Box::pin(sender.send(datagram)); + let mut blocked_receive = Box::pin(socket.recv()); + assert!(futures_util::poll!(&mut blocked_send).is_pending()); + assert!(futures_util::poll!(&mut blocked_receive).is_pending()); + + cancellation.cancel(); + assert_eq!( + futures_util::poll!(&mut blocked_send), + Poll::Ready(Err(UdpError::Stopped)) + ); + assert_eq!(futures_util::poll!(&mut blocked_receive), Poll::Ready(None)); + } + + #[tokio::test] + async fn sender_preserves_socket_validation_and_stop_errors() { + let (socket, _inbound, output) = socket_fixture(); + let sender = socket.sender(); + let mixed = UdpDatagram::new( + "192.0.2.1:53".parse().unwrap(), + "[2001:db8::1]:4000".parse().unwrap(), + &b"mixed"[..], + ); + let oversized = UdpDatagram::new( + "192.0.2.1:53".parse().unwrap(), + "198.51.100.2:4000".parse().unwrap(), + vec![0_u8; 1_473], + ); + + for stopped in [false, true] { + if stopped { + socket.cancellation.cancel(); + } + assert_eq!( + sender.send(mixed.clone()).await, + Err(UdpError::AddressFamilyMismatch) + ); + assert_eq!( + socket.send(mixed.clone()).await, + Err(UdpError::AddressFamilyMismatch) + ); + assert_eq!( + sender.send(oversized.clone()).await, + Err(UdpError::MtuExceeded { + packet_size: 1_501, + mtu: 1_500, + }) + ); + assert_eq!( + socket.send(oversized.clone()).await, + Err(UdpError::MtuExceeded { + packet_size: 1_501, + mtu: 1_500, + }) + ); + } + assert_eq!(sender.send(valid_datagram()).await, Err(UdpError::Stopped)); + assert_eq!(socket.send(valid_datagram()).await, Err(UdpError::Stopped)); + + let (socket, _inbound, output_on_close) = socket_fixture(); + let sender = socket.sender(); + drop(output_on_close); + assert_eq!(sender.send(valid_datagram()).await, Err(UdpError::Stopped)); + assert_eq!(socket.send(valid_datagram()).await, Err(UdpError::Stopped)); + drop(output); + } + #[test] fn builds_and_parses_both_ip_families() { for datagram in [ @@ -209,4 +416,77 @@ mod tests { }) ); } + + #[test] + fn borrowed_codec_reuses_frames_and_retains_payload_storage() { + let mut frame = Vec::with_capacity(1_500); + let storage = frame.as_ptr(); + for datagram in [ + valid_datagram(), + UdpDatagram::new( + "[2001:db8::1]:53".parse().unwrap(), + "[2001:db8::2]:4000".parse().unwrap(), + b"ipv6-payload".as_slice(), + ), + ] { + encode_udp_packet_into(&datagram, 1_500, &mut frame).unwrap(); + assert_eq!(frame.as_ptr(), storage); + let view = parse_udp_packet_view(&frame).unwrap(); + assert_eq!(view.source, datagram.source); + assert_eq!(view.destination, datagram.destination); + assert_eq!(view.payload, datagram.payload.as_ref()); + let payload_offset = frame.len() - datagram.payload.len(); + assert_eq!(view.payload.as_ptr(), frame[payload_offset..].as_ptr()); + assert_eq!(frame, build_udp_packet(&datagram, 1_500).unwrap().data()); + } + } + + #[test] + fn codec_validation_errors_leave_the_reusable_frame_unchanged() { + let mut frame = b"previous-packet".to_vec(); + let old = frame.clone(); + let mixed = UdpDatagram::new( + "192.0.2.1:53".parse().unwrap(), + "[2001:db8::1]:4000".parse().unwrap(), + b"mixed".as_slice(), + ); + assert_eq!( + encode_udp_packet_into(&mixed, 1_500, &mut frame), + Err(UdpError::AddressFamilyMismatch) + ); + assert_eq!(frame, old); + + let oversized = UdpDatagram::new( + "192.0.2.1:53".parse().unwrap(), + "198.51.100.2:4000".parse().unwrap(), + vec![0; 1_473], + ); + assert_eq!( + encode_udp_packet_into(&oversized, 1_500, &mut frame), + Err(UdpError::MtuExceeded { + packet_size: 1_501, + mtu: 1_500, + }) + ); + assert_eq!(frame, old); + } + + #[test] + fn borrowed_parser_keeps_the_existing_checked_header_policy() { + assert!(parse_udp_packet_view(&[]).is_none()); + let mut packet = build_udp_packet(&valid_datagram(), 1_500) + .unwrap() + .data() + .to_vec(); + assert!(parse_udp_packet_view(&packet[..27]).is_none()); + + // This refactor does not add IP/UDP checksum or fragment rejection. + packet[6] = 0x20; + packet[10] ^= 1; + packet[26] ^= 1; + packet.push(0xaa); + let view = parse_udp_packet_view(&packet).unwrap(); + assert_eq!(view.payload, valid_datagram().payload.as_ref()); + assert_eq!(view.source, valid_datagram().source); + } } diff --git a/crates/vcore-netstack/tests/netstack.rs b/crates/vcore-netstack/tests/netstack.rs index 7dc51b6..ca4590f 100644 --- a/crates/vcore-netstack/tests/netstack.rs +++ b/crates/vcore-netstack/tests/netstack.rs @@ -4,15 +4,90 @@ use std::{ }; use bytes::Bytes; -use tokio::io::AsyncWriteExt; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; use vcore_netstack::{NetStack, NetStackConfig, NetStackError, Packet, UdpDatagram}; const WAIT: Duration = Duration::from_secs(2); +#[tokio::test] +async fn ready_tcp_burst_preserves_flow_binding_and_neighboring_icmp() { + let mut parts = NetStack::start(NetStackConfig { + fake_icmp_echo: true, + ..NetStackConfig::default() + }) + .unwrap() + .into_parts(); + let flows = [Flow::v4(11_000), Flow::v4(11_001), Flow::v6(11_002)]; + // Queue the whole burst before yielding to the driver. The two IPv4 SYNs + // share a listen endpoint but must retain separate source/stream bindings. + for flow in &flows { + parts + .packet_sink + .try_send(build_tcp(flow, 100, 0, TcpFlags::SYN, &[])) + .unwrap(); + } + parts + .packet_sink + .try_send(build_icmpv4_echo(b"tcp-neighbor")) + .unwrap(); + let mut server_sequences = [None; 3]; + let mut echo = false; + for _ in 0..4 { + let reply = timeout_packet(&mut parts.packet_stream).await; + if let Some(index) = flows.iter().position(|flow| is_syn_ack(&reply, flow)) { + assert!( + server_sequences[index] + .replace(tcp_sequence(&reply)) + .is_none() + ); + } else { + let offset = ip_header_len(reply.data()); + assert_eq!(reply.data()[9], 1); + assert_eq!(reply.data()[offset], 0); + assert_eq!(&reply.data()[offset + 8..], b"tcp-neighbor"); + assert!(!echo); + echo = true; + } + } + assert!(echo); + let mut streams = Vec::new(); + for _ in &flows { + streams.push(parts.tcp_listener.accept().await.unwrap()); + } + for (index, flow) in flows.iter().enumerate() { + parts + .packet_sink + .try_send(build_tcp( + flow, + 101, + server_sequences[index].unwrap().wrapping_add(1), + TcpFlags::ACK, + &[u8::try_from(index).unwrap()], + )) + .unwrap(); + } + for mut stream in streams { + let index = flows + .iter() + .position(|flow| flow.source == stream.source_addr()) + .unwrap(); + assert_eq!(stream.destination_addr(), flows[index].destination); + let mut byte = [0]; + tokio::time::timeout(WAIT, stream.read_exact(&mut byte)) + .await + .expect("batched TCP payload was not delivered") + .unwrap(); + assert_eq!(byte, [u8::try_from(index).unwrap()]); + } + assert_eq!(parts.stats.snapshot().icmp_dropped, 0); + parts.control.stop().await; +} + #[tokio::test] async fn synthetic_tun_supports_ipv4_and_ipv6_tcp_and_udp() { let stack = NetStack::start(NetStackConfig::default()).unwrap(); let mut parts = stack.into_parts(); + let mut streams = Vec::new(); for flow in [Flow::v4(12_000), Flow::v6(12_001)] { parts @@ -28,6 +103,7 @@ async fn synthetic_tun_supports_ipv4_and_ipv6_tcp_and_udp() { .expect("TCP listener stopped"); assert_eq!(stream.source_addr(), flow.source); assert_eq!(stream.destination_addr(), flow.destination); + streams.push(stream); } for flow in [Flow::v4(13_000), Flow::v6(13_001)] { @@ -55,6 +131,186 @@ async fn synthetic_tun_supports_ipv4_and_ipv6_tcp_and_udp() { assert_eq!(parts.stats.snapshot().active_tcp, 0); } +#[tokio::test] +async fn tcp_only_mode_keeps_tcp_icmp_and_stop_without_a_udp_queue() { + let mut parts = NetStack::start_tcp(NetStackConfig { + // Would exceed Tokio's channel capacity limit if a UDP queue existed. + udp_queue: usize::MAX, + fake_icmp_echo: true, + ..NetStackConfig::default() + }) + .unwrap(); + let flow = Flow::v4(13_100); + parts + .packet_sink + .send(build_udp(&flow, b"handled-by-caller")) + .await + .unwrap(); + parts + .packet_sink + .send(build_icmpv4_echo(b"tcp-only")) + .await + .unwrap(); + let echo = timeout_packet(&mut parts.packet_stream).await; + assert_eq!(&echo.data()[28..], b"tcp-only"); + parts + .packet_sink + .send(build_tcp(&flow, 100, 0, TcpFlags::SYN, &[])) + .await + .unwrap(); + assert!(is_syn_ack( + &timeout_packet(&mut parts.packet_stream).await, + &flow + )); + let stream = parts.tcp_listener.accept().await.unwrap(); + assert_eq!(stream.source_addr(), flow.source); + assert_eq!(parts.stats.snapshot().dropped_udp, 0); + parts.control.stop().await; + assert!(parts.packet_stream.recv().await.is_none()); + assert!(stream.is_stopped()); + assert_eq!(parts.stats.snapshot().active_tcp, 0); +} + +#[tokio::test] +async fn output_capacity_recovery_wakes_a_pending_tcp_reset_before_its_timer() { + for flow in [Flow::v4(13_200), Flow::v6(13_201)] { + let mut parts = NetStack::start(NetStackConfig { + packet_queue: 1, + max_poll_interval: Duration::from_secs(5), + ..NetStackConfig::default() + }) + .unwrap() + .into_parts(); + parts + .packet_sink + .send(build_tcp(&flow, 100, 0, TcpFlags::SYN, &[])) + .await + .unwrap(); + let syn_ack = timeout_packet(&mut parts.packet_stream).await; + assert!(is_syn_ack(&syn_ack, &flow)); + let stream = parts.tcp_listener.accept().await.unwrap(); + + // Fill the sole raw output queue before asking smoltcp to emit RST. + let blocker = UdpDatagram::new(flow.destination, flow.source, b"blocker".as_slice()); + parts.udp_socket.send(blocker.clone()).await.unwrap(); + drop(stream); + tokio::time::sleep(Duration::from_millis(20)).await; + assert_eq!(parts.stats.snapshot().active_tcp, 1); + + let queued = parts.packet_stream.try_recv().unwrap(); + assert_udp_packet(&queued, &blocker); + let reset = tokio::time::timeout(Duration::from_millis(500), parts.packet_stream.recv()) + .await + .expect("output capacity recovery did not wake the driver") + .unwrap(); + let offset = ip_header_len(reset.data()); + assert_ne!(reset.data()[offset + 13] & 0x04, 0, "missing TCP RST"); + tokio::time::timeout(WAIT, async { + while parts.stats.snapshot().active_tcp != 0 { + tokio::task::yield_now().await; + } + }) + .await + .expect("reset flow was not reclaimed"); + assert!(parts.packet_stream.try_recv().is_err()); + parts.control.stop().await; + } +} + +#[tokio::test] +async fn tcp_only_full_output_retains_one_pending_syn_and_stops_without_draining() { + for recover_capacity in [false, true] { + let mut parts = NetStack::start_tcp(NetStackConfig { + packet_queue: 1, + fake_icmp_echo: true, + max_poll_interval: Duration::from_secs(5), + ..NetStackConfig::default() + }) + .unwrap(); + let flow = Flow::v4(13_300); + parts + .packet_sink + .send(build_icmpv4_echo(b"blocker")) + .await + .unwrap(); + parts + .packet_sink + .send(build_tcp(&flow, 100, 0, TcpFlags::SYN, &[])) + .await + .unwrap(); + let stream = tokio::time::timeout(WAIT, parts.tcp_listener.accept()) + .await + .expect("SYN was not retained behind full output") + .unwrap(); + assert_eq!(parts.stats.snapshot().active_tcp, 1); + assert_eq!(parts.stats.snapshot().rejected_tcp, 0); + if recover_capacity { + let echo = parts.packet_stream.try_recv().unwrap(); + assert_eq!(&echo.data()[28..], b"blocker"); + let syn_ack = + tokio::time::timeout(Duration::from_millis(500), parts.packet_stream.recv()) + .await + .expect("pending SYN did not resume when output capacity returned") + .unwrap(); + assert!(is_syn_ack(&syn_ack, &flow)); + } + tokio::time::timeout(Duration::from_millis(500), parts.control.stop()) + .await + .expect("full output or pending ingress blocked stop"); + assert!(stream.is_stopped()); + assert_eq!(parts.stats.snapshot().active_tcp, 0); + assert!(parts.packet_stream.recv().await.is_none()); + } +} + +#[tokio::test] +async fn dropping_the_only_output_consumer_stops_active_tcp_and_other_endpoints() { + let mut parts = NetStack::start(NetStackConfig { + max_poll_interval: Duration::from_secs(5), + ..NetStackConfig::default() + }) + .unwrap() + .into_parts(); + let flow = Flow::v4(13_400); + parts + .packet_sink + .send(build_tcp(&flow, 100, 0, TcpFlags::SYN, &[])) + .await + .unwrap(); + assert!(is_syn_ack( + &timeout_packet(&mut parts.packet_stream).await, + &flow + )); + let stream = parts.tcp_listener.accept().await.unwrap(); + assert_eq!(parts.stats.snapshot().active_tcp, 1); + + drop(parts.packet_stream); + tokio::time::timeout(Duration::from_millis(500), parts.control.wait_stopped()) + .await + .expect("closed output did not stop the driver promptly"); + assert!(stream.is_stopped()); + assert_eq!(parts.stats.snapshot().active_tcp, 0); + assert!(parts.tcp_listener.accept().await.is_none()); + assert!(parts.udp_socket.recv().await.is_none()); + assert!(matches!( + parts + .packet_sink + .try_send(build_tcp(&flow, 101, 1, TcpFlags::ACK, &[])), + Err(NetStackError::Stopped) + )); + assert_eq!( + parts + .udp_socket + .send(UdpDatagram::new( + flow.destination, + flow.source, + b"stopped".as_slice(), + )) + .await, + Err(vcore_netstack::UdpError::Stopped) + ); +} + #[tokio::test] async fn tcp_write_applies_backpressure_and_stop_unblocks_it() { let config = NetStackConfig { @@ -107,6 +363,50 @@ async fn tcp_write_applies_backpressure_and_stop_unblocks_it() { assert_eq!(parts.stats.snapshot().active_tcp, 0); } +#[tokio::test] +async fn rejecting_a_half_open_flow_emits_reset_before_reclaiming_it() { + for flow in [Flow::v4(14_100), Flow::v6(14_101)] { + let mut parts = NetStack::start(NetStackConfig { + packet_queue: 1, + max_poll_interval: Duration::from_millis(5), + ..NetStackConfig::default() + }) + .unwrap() + .into_parts(); + parts + .packet_sink + .send(build_tcp(&flow, 100, 0, TcpFlags::SYN, &[])) + .await + .unwrap(); + let syn_ack = timeout_packet(&mut parts.packet_stream).await; + assert!(is_syn_ack(&syn_ack, &flow)); + // The dispatcher can reject immediately, before the client's ACK. + drop(parts.tcp_listener.accept().await.unwrap()); + let reset = timeout_packet(&mut parts.packet_stream).await; + let offset = ip_header_len(reset.data()); + assert_ne!(reset.data()[offset + 13] & 0x04, 0, "missing TCP RST"); + assert_eq!( + u16::from_be_bytes(reset.data()[offset..offset + 2].try_into().unwrap()), + flow.destination.port() + ); + tokio::time::timeout(WAIT, async { + while parts.stats.snapshot().active_tcp != 0 { + tokio::task::yield_now().await; + } + }) + .await + .expect("rejected half-open flow was retained"); + assert_eq!(parts.stats.snapshot().half_open_tcp, 0); + assert!( + tokio::time::timeout(Duration::from_millis(20), parts.packet_stream.recv()) + .await + .is_err(), + "rejected flow emitted a duplicate reset" + ); + parts.control.stop().await; + } +} + #[tokio::test] async fn half_open_flows_are_not_count_limited_and_udp_queue_remains_bounded() { const FLOW_COUNT: usize = 40; diff --git a/docs/README.md b/docs/README.md index 5dc4c1c..478ee6a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -24,7 +24,7 @@ ## 开发与验证 -- [构建与检查](../scripts/README.md)、[测试入口](../tests/README.md)。 +- [平台编译](../scripts/README.md)、[离线回归与独立容器入口](../tests/README.md)。 - [测试隔离](testing-isolation.md):所有网络服务端必须容器化。 - [验收边界](acceptance.md):本地、CI、设备和发布证据分别记录。 - [领域上下文](../CONTEXT.md)、[架构决策](adr/)。 diff --git a/docs/acceptance.md b/docs/acceptance.md index 4c9b429..961856b 100644 --- a/docs/acceptance.md +++ b/docs/acceptance.md @@ -1,8 +1,57 @@ # 验证范围与发布边界 -测试入口见 [tests](../tests/README.md),命令见 [scripts](../scripts/README.md)。 +核心离线测试见 [tests](../tests/README.md),平台编译入口见 [scripts](../scripts/README.md)。 +容器互通、压力与性能比较由公开的 +[container-benchmark](https://github.com/OneXray/container-benchmark) 的 +`interop` / `stress` / `compare` 分别执行,通过 `--source vcore=PATH` +显式指定被测 checkout;VCore 编译入口不执行网络或压力验收。 本文定义证据边界,不是随源码自动续期的“全部通过”证明。 +## 核心压力测试 + +独立 benchmark 的 `stress` 面向指定 VCore 的原生 Linux TUN,默认 2 Gbps / +60 秒 / 1,000 QPS DNS,默认加载完整增强 DAT;可选条数只改变实验输入,不是生产上限。 +`compare` 保持完整增强 +CN 的 VCore/Mihomo 1/1.5/2 Gbps 对比。两者分别记录混合 TCP/UDP 与 DNS 负载 +及指定内核 PID 的内存峰值,实际输入和门槛以 benchmark README 为准。 +50,000,000 bytes 为宿主工程目标;报告实际吞吐、丢包/错误、CPU、RSS 和 DNS 完成数。 +未达到目标负载不能宣称该档内存通过,数据损坏、崩溃、观测或清理失败不能隐藏。 + +压测不以候选冻结、全协议/规则语义矩阵、对端零丢包容量校准、平台构建或真机准备 +作为前置。协议正确性、发布和物理设备验收保持独立,宿主结果不证明 iOS/tvOS +Network Extension 实机内存。当前保持 standard,不新增生产业务数量配额。 + +参数和范围由独立 benchmark README 维护;记录实际命令、源码身份、测量和失败, +结束清理本轮产物,仅保留脱敏文字结论与共享公共依赖。复验重新运行,旧结果不续期。 + +GeoData 属性/反选、真实 Regex 编译、合成 Plain 和双快照重叠由独立 builder probe +记录;它不包含生产 TUN/DNS 运行时,不能拼成更新与满速流量叠加验收。真实 DAT +没有的类型明确报告为零,不把合成记录计入真实压力规模。最新完整输入结果与历史 +128 万无 Regex 基线分别见 benchmark README;均不代表任意输入内存保证或 +iOS/tvOS 实机验收。 + +2026-10-06 切换常规 `regex::bytes::Regex` 后,执行两轮完整 CN 原生 Linux TUN +复测:`compare --core vcore --rates 2000 --seconds 60 --source vcore=PATH`, +仅加载 `geosite:cn` / `geoip:cn` 共 121,009 条,正常 Release + 生产 FFI, +Ubuntu 26.04.1 LTS、NAT、5 CPU / 8 GiB、1,000 QPS DNS,线程/队列保持默认。 +RSS 峰值为 28,008,448 / 30,543,872 bytes,低于 50,000,000 bytes;分流与 +观测有效、进程正常退出,容器和 scratch 已清理。 +实际带宽仅 1,966.61 / 1,959.93 Mbps,**两轮均未达到 2 Gbps 的 99% 负载门槛**; +发送速率也低于门槛,不能把差额全部归因于正则替换。UDP 丢包为 +7,077 / 28,190 包(每轮发送 6,249,984 包),DNS 成功 59,790 / 59,585 次 +(各计划 60,000),仍有超时和跳过;不宣称整体 2 Gbps、零丢包或全部查询通过。 +完整输入身份与两次文字证据见 benchmark README。本次未复跑完整资产或双快照 +probe,Linux RSS 不替代 Apple 实机 footprint,也不是任意输入的内存保证。 + +以下完整资产与双快照数据为 2026-10-06 旧 dense DFA 历史基线,尚未针对常规正则重跑: +完整增强资产共 1,572,166 条(GeoIP 1,054,987、GeoSite 517,179), +未截断的 60 秒 / 2 Gbps / 1,000 QPS DNS 混合压力取得有效观测:实际吞吐 +1,993.88 Mbps、CPU 154.06%、指定 VCore 进程 RSS 峰值 53,542,912 bytes。 +50,000,000 bytes 内存目标 **未通过**;UDP 丢包 2,911 / 6,249,984,DNS 成功 +59,843 / 60,000 次计划查询。负载门槛通过不等于零丢包或全部查询完成。 +同轮离线双快照 probe 峰值 64,045,056 bytes,也超出该目标;它不是运行中更新 +叠加满速压力的结果。所有容器已正常退出并清理,不据此恢复条数截断。 + ## 必须保留的验证 | 层次 | 验证内容 | 不能证明 | @@ -10,26 +59,49 @@ | 离线 / 纯内存 | 严格配置与 feature、DAG/组快照、协议向量、TLS 身份/签名/pin、取消与局部上限、FFI 所有权 | 网络互通、设备 | | 容器互通 | 公开配置和消费者、真实认证及负例、传输关闭、UDP 来源与边界、受控 DNS/上游 | 任意字段组合或公网服务 | | 集成 / 压力 | 八出站 64 有序两跳、SS v3/UoT/TUIC 强耦合链、运行时切组、Stop/回滚/测速、混合重建与长测 | 无扰动吞吐基准或整机内存保证 | -| 平台构建 / ABI | 同一锁文件、产物架构/身份/hash、原生 C/Swift 消费者与打包依赖 | 物理 TUN、签名安装 | +| 平台构建 / ABI | 同一锁文件、产物架构/身份/hash、打包依赖;原生 C/Swift 消费者另行验证 | 物理 TUN、签名安装;delivery 不执行原生消费者 | +| Apple 模拟器 | 生产库的 C ABI、生命周期、容器原站 SOCKS5 TCP/UDP、合成 utun TCP/UDP、fd 借用和错误路径 | 真机 Packet Tunnel、完整协议矩阵、整进程 50M / 1 Gbps 验收 | | 设备 / 发布 | 真机网络、protect/物理绑定、正式宿主生命周期、签名安装及商店门禁 | 其他平台或后续 revision | 保留独立 ClientHello golden、Encryption 密码向量、H2 完整响应后 RST、SS 背压/读先于写的刷新、 HY2 已完成分片 ID 重用等确定性回归。单纯复用实现生成期望值、声明字段数量或找到 PASS 文本,不能替代行为验证。ignored、未运行、基础设施失败和清理失败均不得计为通过。 -integration 另含 TUIC 双模式到 SS v3 三算法的六条链,复跑 TCP-only SOCKS5 到 -SS UoT/v3、SS UoT 到 TUIC 等既有消费者。100 次生命周期覆盖基础协议及扩展组合; -100 轮重建和至少 1800 秒长测每轮保持 20 TCP + 20 UDP,TCP 使用 SOCKS5、SS v3、 -TUIC、HTTPUpgrade 四组,UDP 使用 SS UoT、SS UoT+v3、TUIC、HTTPUpgrade 四组, -每组五条并轮换算法/模式。原七协议和 HY2 连续跳端口回归仍保留。 -Stop 返回时资源必须归零、FD 回基线,后续五秒静默不作清理宽限。预热五分钟后, -逐分钟独立记录堆、RSS、活对象和队列;后十分钟堆中位数增长上限为 max(1 MiB, 5%), -建链中位数上限为首次两倍。短 tracer 或单协议 suite 均不能替代整轮验收。 +## 当前容器互通入口 + +默认编排 64 个代表用例:Mihomo 26、Xray-core 20、Hysteria2 5、V2Ray 10、 +Caddy/Xray H3/mTLS 3。具体配置由 benchmark 的 `interop --list` 和源码定义, +可按 `--backend` / `--protocol` 筛选;命令存在或离线通过不等于当次互通通过。 +消费者统一使用生产 Invoke ABI;每轮构建一次,再顺序运行各组的三个 Linux +隔离容器(NAT、5 CPU / 8 GiB)。编排已移至独立 benchmark,VCore 只提供被测源码; +原入口的历史通过不能作为迁移后本轮重新运行的证明。 + +2026-10-06 迁移后仅重新执行 Mihomo SOCKS5 TCP/UDP 短测并通过:TCP 双向 +各 1,024 bytes、UDP 双向各两包(64 / 1,200 bytes),来源见证、正常 Stop 和 +容器清理通过。其余 63 用例本轮 **NOT RUN**,不继承旧完整矩阵的通过状态。 + +正例核对双向 payload 和原站所见实际协议对端来源;SS2022 是非空 client-first。 +Trojan 的域名 UDP 经 Xray TCP/WS/gRPC 补验;XHTTP H3 的四模式、独立下载腿和 +静态 ECH 由 Xray 补验,下载腿必须汇入同一个 handler。Hysteria2 的 mTLS 正/负例、 +16 端口跳跃、跳跃叠加 Salamander 与禁用 UDP 使用官方原生服务端;跳跃须保持 +同一关联至少 16.5 秒且观测至少两个实际目的端口。认证拒绝和禁用 UDP 负例必须 +有被拒绝业务的原站零交付证据。VMess/VLESS 的 HTTP 首包伪装、legacy H2 和 +扩展 WS ED 由 V2Ray 补验;H3/mTLS 由 Caddy 的真实 `require_and_verify` 终结后 +H2C 转发到同一 Xray handler,并验缺身份及错误 CA 拒绝;不宣称原生 Xray 支持 mTLS +或 legacy H2。AES EIH 对 Xray 验两算法正例及错误 identity/user 的 TCP/UDP 零交付; +H3 packetaddr 与三种 sing-mux 使用 Xray XHTTP 透传到同容器的回环 Mihomo 解码端。 + +Encryption、REALITY/JLS 等额外配置不在本入口覆盖内。这套代表入口不代表历史 +完整字段、代理链、切组/重建长测、ClientHello golden 或 +设备矩阵全部恢复或重新执行。独立密码/ClientHello 输入与纯内存回归继续保留, +不得由单协议短探测拼成完整验收。所有声明以当次源码/锁文件/对端 hash、容器内 +版本、实际业务及清理结论为准;历史完整矩阵与标准仅在下方链接查阅。 ## 对端与已知限制 - 所有协议端、原站、DNS 和提供入口的对照客户端遵守[隔离规则](testing-isolation.md)。 - 优先 Mihomo listener;缺少的能力由官方 Xray、V2Ray、Hysteria 或 Shadowsocks 服务端补验。 + 优先 Mihomo listener;当前代表缺口由官方 Xray、V2Ray、Hysteria 与 Caddy 网关补验。 + 历史官方 Shadowsocks 补充对照不意味着当前入口已恢复相同覆盖。 - 关闭行为按 Mihomo 的实际传输包装链验证;不要求所有传输在上传 EOF 后仍收到尾包。 - WS + REALITY 的数据/认证对 Mihomo listener 验证;关闭使用同种 WS 的标准 TLS 分层参照。 无指纹 JLS/gRPC、Safari ECH/gRPC 的官方对照缺口使用明确标注的 Chrome 关闭参照。 @@ -67,12 +139,14 @@ Stop 返回时资源必须归零、FD 回基线,后续五秒静默不作清理 该基线记录过本地协议集成、持续压力与 Apple/Android 构建通过;随后 boring release / Shadowsocks registry 接入只做了定向 JLS、SS 和离线回归,没有重签全部组合及长测。 平台交付仍未完整签收。这些是历史记录范围,不代表当前 checkout、远端 CI 或发布候选 -已经重新执行。后续结果放当次 PR、CI artifact 或发布记录,绑定源码、锁文件、对端、 -命令、结果及清理证据,不在本文累计包哈希和阶段流水账。 +已经重新执行。后续生产构建结果放当次 PR、CI artifact 或发布记录;容器实验仅保留脱敏文字结论, +绑定源码、锁文件、对端、命令、结果及清理状态,不在本文累计包哈希和阶段流水账。 ## 尚需独立签收 -- iOS 无 debugger 的 Release TUN 生命周期与整进程内存;Android 真机 TUN/protect、 +- iOS/tvOS 无 debugger 的 Release Packet Tunnel 生命周期、宿主 raw-fd/packetFlow 接入及 + 整进程内存;tvOS 以 17.0+ ARM64 为平台边界,模拟器与构建不证明真实扩展可交付。 + Android 真机 TUN/protect、 DNS/TCP/UDP、重复启停;macOS system extension 正式宿主安装和生命周期。 - Windows 10 20H2、原生 x64、真实物理 IPv6、物理网卡禁用、多用户/远程会话; session backend 包路径/argv/退出与 Job 清理、正式宿主 UI。 diff --git a/docs/config.yaml b/docs/config.yaml index 1a7b76e..3979845 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -5,6 +5,8 @@ # # 宿主先通过 Invoke API v5 调用 initialize({dataDir}),再通过 configYaml 内联 # 传入配置。GeoData 固定由 VCore 管理在 dataDir/geodata 中。 +# 所有平台均不设 GeoData 条数上限,不按数量截断,也不设总内存预算; +# 仅加载当前业务规则和 DNS policy 引用的分类/属性交集,详见 docs/geodata.md。 # IPv6 数据面总开关,省略时默认为 true。false 时拒绝 IPv6 字面量业务目标、过滤 # 本地解析和代理根节点的 IPv6 地址、禁止 IPv6 物理 socket,并在 TUN 入口丢弃 @@ -412,7 +414,10 @@ proxy-groups: # 默认出口含义,只有存在同名 route target 时才有效。DNS route 选中代理组后, # 仅新建物理 transport 解析当前成员;cache 与既有 TCP pool 不随切换清空。 # nameserver-policy 按顺序匹配 -# `geosite:[,...]`,命中后只在当前组内顺序故障转移。 +# `geosite:[,...]`,逗号项为 OR,命中后只在当前组内故障转移。 +# selector 可为 cn、cn@ads@cn、!cn@ads;多个属性为 key 存在性 AND(忽略 bool/int 值), +# 前置 ! 取反整个匹配结果,@!cn 是字面 key。相同归一 selector 不能跨项重复, +# 但相同基础分类的不同交集/正反选可共存;资产不可用不因反选变成全匹配。 dns: enable: true ipv6: true @@ -427,6 +432,10 @@ dns: # 动作必须精确使用实际节点名或代理组名。`PROXY` 没有特殊别名。rules 不能为空, # 必须恰好以一个指向实际节点或代理组的 MATCH 结束;不存在隐式 MATCH 或 DIRECT # 回退。选中组时使用当前成员,组本身不做 health check、测速或自动 failover。 +# GEOSITE 支持 [!][@...],GEOIP 支持 [!] 和可选 no-resolve。 +# 例如 GEOSITE,google@ads,REJECT / GEOSITE,!cn@ads,main-select / GEOIP,!CN,DIRECT。 +# geolocation-!cn 为字面分类名;GeoIP DAT reverse_match 接受但忽略,取反由 ! 控制。 +# GEOSITE 没有域名不匹配;分类缺失不匹配,而有效空属性交集的反选匹配非空域名。 rules: - GEOSITE,category-ads-all,REJECT - GEOSITE,cn,DIRECT diff --git a/docs/controller-api.md b/docs/controller-api.md index ec1f9f3..0fc4731 100644 --- a/docs/controller-api.md +++ b/docs/controller-api.md @@ -57,6 +57,8 @@ Apple utun 的四字节包信息头不计入统计。统计只覆盖跨过 TUN L 请求不会等待下一次采样,不保持分块响应,不升级 WebSocket,也不为调用方保存“上次读取”状态。首个采样窗口完成前和空闲窗口内,`up`/`down` 为 0;累计值在同一 session 内单调不减并采用饱和语义。没有 TUN 的代理组 Controller 对该路由返回 `404 Not Found`。 +TUN 按最多 8 包中有效消费/平台报告成功的包合并记账,取消或错误不会遗漏已经完成的前缀。批次恰好跨越一秒采样边界时,该批字节统一归入记账时的窗口,累计值不受影响;流量快照不是逐包时间戳统计。 + ## 代理组查询 `GET /group` 按 YAML 声明顺序返回全部代理组: diff --git a/docs/geodata.md b/docs/geodata.md index eb2d9d5..56d6759 100644 --- a/docs/geodata.md +++ b/docs/geodata.md @@ -1,6 +1,9 @@ # GeoData 规则与资产 -VCore 管理 `dataDir/geodata` 下的 `geosite.dat` 和 `geoip.dat`,只为当前配置实际引用的分类构建有界匹配器。资产缺失或更新失败时,对应种类暂时不可用;配置错误、资产格式错误和资源越界仍然失败关闭。 +VCore 管理 `dataDir/geodata` 下的 `geosite.dat` 和 `geoip.dat`,只为当前配置实际引用的 +分类与属性交集构建匹配器。所有平台均不设 GeoData 条数上限,不按数量截断记录, +也不设总内存预算。资产缺失或候选校验失败时,对应种类不可用或保持上一份有效快照; +配置、格式、整数表示和可恢复分配错误仍然失败关闭。 ## 规则 @@ -16,18 +19,31 @@ rules: 语法: ```text -GEOSITE,, -GEOIP,,[,no-resolve] +GEOSITE,[!][@...], +GEOIP,[!],[,no-resolve] ``` - 规则类型和 `code` 不区分 ASCII 大小写,route target 名称大小写敏感并精确匹配。 - `target` 只能是 `DIRECT`、`REJECT`、实际代理节点名或静态 `select` 组名。 -- `code` 必须匹配 `[A-Za-z0-9][A-Za-z0-9._+!-]{0,63}`,按 ASCII 大小写折叠后判重。 +- 基础 `code` 必须匹配 `[A-Za-z0-9][A-Za-z0-9._+!-]{0,63}`,按 ASCII 大小写折叠后定位。 - `GEOSITE` 恰好三段;`GEOIP` 只允许可选且大小写敏感的 `no-resolve`。 -- 不支持反选、属性选择器、单条规则多个 code、源地址 GeoIP 或隐式局域网分类。 +- `GEOSITE,cn@ads@cn,DIRECT` 只保留同时存在 `ads` 和 `cn` 属性 key 的记录,多个属性为 AND。 + 属性只检查 key 存在,不解释 bool/int 值;`false`、`0` 或无 typed value 均不代表 key 缺失。 +- 属性名按 Mihomo 的先小写、后 Unicode simple-fold 比较语义归一;交集顺序与重复 key 不改变 + 结果。空 `@` 项忽略,非空属性首尾空白移除,但内部空白、控制字符和逗号不允许。 +- 前置 `!` 对整个正向匹配结果取反:`!cn@ads` 是 NOT(匹配 `cn@ads`),不是“没有 ads 的记录”。 + `cn@!ads` 查找字面 key `!ads`;`geolocation-!cn` 是字面分类名。 +- GeoIP 支持前置 `!`,不支持属性。业务规则不支持单条多个 code、源地址 GeoIP 或隐式局域网分类。 - 规则按顺序首条命中,最终 `MATCH` 必须指向实际代理节点或代理组。 -DNS `nameserver-policy` 中的 `geosite:[,...]` 与业务规则共享同一份分类需求和匹配器。 +DNS `nameserver-policy` 中的 `geosite:[,...]` 接受相同的属性与反选语义, +逗号连接的 selector 为 OR。业务规则与 DNS policy 共享基础分类、value arena 与正则, +每个基础分类只解码一次;不同属性交集保留各自匹配索引,正反选不复制记录。 + +存在分类但属性筛选为空时,该 selector 仍可用:正选不匹配,反选匹配所有合法非空域名。 +分类缺失、资产不可用或未声明的 selector 不能通过反选变成全匹配;没有域名时所有 GeoSite +selector 均不命中。属性折叠使用依赖的 Unicode 16 simple-fold 表;不保证与采用其他 +Unicode 版本的 Mihomo 在所有新字符上完全等价。 ## 资产格式 @@ -37,10 +53,12 @@ DNS `nameserver-policy` 中的 `geosite:[,...]` 与业务规则共 | `GEOIP` | `geoip.dat` | `GeoIPList` | code 与 IPv4/IPv6 CIDR | - 文件路径固定,YAML 不能指定本地路径或环境变量。 -- 文件必须是普通文件;外层帧、code、分类引用、CIDR、正则表达式和资源上限全部校验。 -- 第一遍扫描只建立 code 到文件范围的有界索引;第二遍只解析被配置引用的分类。 +- 文件必须是普通文件;校验外层帧、code、分类引用、实际选中的 CIDR / Domain 记录。 +- 第一遍扫描建立 code 到文件范围的索引,容量按需增长;第二遍只解析被配置引用的分类。 +- 只引用属性交集时,先流式检查属性;未命中任何交集的记录按 wire/length 边界跳过, + 不校验 value 或编译 Regex。引用无属性 selector 时则包含该分类的全部记录。 - 目标 code 缺失、重复或损坏时,整份对应种类的快照不可用,不能当作空分类。 -- GeoSite 和 GeoIP 相互独立,一类不可用不影响另一类和基础规则。 +- GeoSite 和 GeoIP 分别校验和报告可用性,一类不可用不影响基础规则。 ## 自动更新 @@ -62,27 +80,48 @@ geo-update-interval: 24 - 下载固定使用最终 `MATCH` route target。若它是代理组,每次新建下载物理 transport 时沿嵌套组解析到当时选中的具体节点、`DIRECT` 或 `REJECT`;失败不自动换成员,也不隐式回退 DIRECT 或系统代理。 - 缺失资产立即检查;失败后按 1 分钟、5 分钟、15 分钟、1 小时退避,之后保持 1 小时上限。成功后恢复 24 小时周期。 - ETag、SHA-256、源 URL 和下次检查时间保存在 VCore 管理的状态中。合法 304 只推进调度。 -- 下载写入同目录暂存文件,通过大小、hash、wire、需求和资源校验后原子替换。失败保留上一份有效资产。 +- 下载以固定大小缓冲流式写入同目录暂存文件,不限制资产总字节;保留 90 秒期限、取消、HTTPS/HTTP 解析边界和字节计数溢出检查。文件大小必须与下载报告一致,通过 wire 和分类需求校验后原子替换,SHA-256 随状态保存;失败保留上一份有效资产。 - 跨进程更新锁和原子重命名只保护共享数据目录,不提供多实例调度协议。 +隔离实验可显式编译独立、非默认 `benchmark-geodata-http` feature,使 GeoData 更新 +接受以域名为主机、无 userinfo/fragment 的 HTTP fixture;仍通过同一个 dispatcher 和 +流式下载器,不建立 TLS。HTTP 重定向可保持 HTTP 或升级到 HTTPS,HTTPS 不允许降级。 +该入口没有环境变量或新的 YAML/FFI 开关;未编译该 feature 时仍强制 HTTPS,HTTPS 的 +公开根和完整证书验证不变。平台构建及 delivery 拒绝该测试 feature。HTTP 实验只验收 +更新逻辑与资源占用,不构成生产 HTTPS 信任链验收,不应启用 `interop-test`。 + ## GeoSite 匹配 -选路域名来源的优先级为: +TUN 的 IP 目标采用以下选路提示优先级: ```text -嗅探域名 > TUN DNS 提示 > 目标域名 > 无域名 +嗅探域名 > TUN DNS 提示 > 无域名 ``` -嗅探和 DNS 提示只参与选路,不改写实际出站目标。没有域名时,`GEOSITE` 不匹配,也不触发 DNS。 +目标本来是域名时使用目标域名,不受提示覆盖。嗅探和 DNS 提示只参与选路,不改写 +实际出站目标。没有域名时,`GEOSITE` 不匹配,也不触发 DNS。 -域名规范化: +运行时目标、嗅探和 DNS 提示的域名规范化(不是资产加载条件): 1. 移除一个末尾点; 2. 使用 UTS #46 non-transitional 和 STD3 生成 ASCII A-label; 3. 转为 ASCII 小写; 4. IDNA 失败时,只允许严格的 1–253 字节 ASCII DNS 名称,每个 label 为 1–63 字节且只含字母、数字和内部连字符。 -两条路径都失败时拒绝当前会话或数据报。规范化结果写入有界选路上下文供后续规则复用。 +目标域名两条路径都失败时拒绝当前会话或数据报;IP 目标的嗅探/DNS 提示则是尽力注解, +无法规范化时忽略提示,不使合法 IP 目标不可达。规范化结果写入有界选路上下文供后续规则复用。 + +资产中的 `Domain` / `Full` 是静态字面匹配值,不是将要解析的 DNS 主机名,不再以 +STD3、LDH 字符、63/253 字节或合法 Punycode 作为加载条件。移除一个末尾点、转小写; +ASCII 值直接处理,不为每条记录分配规范化字符串。Unicode 值尽可能转换为 ASCII +A-label,保持现有 IDN 匹配;转换失败则保留小写 UTF-8 字面值,不丢弃记录,也不使 +整个分类失效。引号、下划线、反斜杠等字符不擅自删除,`*` 不新增通配语义。 + +仍要求记录字段、类型、UTF-8 与规范化后的非空值合法,保留整数与 arena 表示边界。 +这些静态值不会直接传入 DNS、TLS 或连接接口;能加载不代表每个值都能成为合法的 +运行时目标。实际目标的现有规范化、DNS wire 长度检查和 TLS 身份验证不受此调整影响。 +Mihomo 也不以严格 DNS 主机名语法校验 GeoSite 值;其 Unicode/尾点模式行为不同, +本次保留 VCore 现有的合法 IDN 和单尾点规范化,不宣称所有模式语义完全一致。 支持的 Domain 记录: @@ -93,7 +132,19 @@ geo-update-interval: 24 | `Full` | 完全相等 | | `Regex` | 在规范化后的完整 ASCII 域名上执行搜索 | -`Regex` 只接受有界 ASCII 源和 RE2 类子集,不支持 look-around、backreference 或额外内联模式。每条正则独立编译为 dense DFA,关闭 accelerator,并同时受源码、编译预留和保留内存上限约束;运行期匹配不分配堆内存。 +Domain/Full 在加载时原地排序紧凑记录,运行时对完整名称及逐 label 后缀做二分查找, +未命中也不线性遍历全部 Domain/Full,不复制 value arena。Substr +与 Regex 仍按各自语义匹配,分类内各记录是集合并集,不影响外层规则的首命中顺序。 + +选中的 `Regex` 只接受 ASCII 源和 RE2 类子集,不支持 look-around、backreference 或 +额外内联模式,不宣称完整 Go 正则兼容。每条正则编译为共享的 +`regex::bytes::Regex`,使用 `RegexBuilder::unicode(false)` 保持 ASCII 匹配语义; +不显式构建 dense DFA,也不在 VCore 中配置 NFA、DFA 或 determinization 额度。 +保留 `regex` 库默认的编译大小、语法嵌套和搜索缓存保护,超出库边界的所选表达式 +使候选加载失败,而不是丢弃记录。VCore 不设置正则条数、累计源码或总内存预算。 +正则的编译与搜索状态由库管理;搜索可能分配 scratch、增长缓存或竞争共享缓存, +不再承诺 Regex 热路径无分配。Domain/Full 的紧凑无分配查找保持不变。 +库的局部保护不是全部正则合计内存或整进程峰值的上界。 ## GeoIP 与 `no-resolve` @@ -103,39 +154,68 @@ geo-update-interval: 24 - A 结果先于 AAAA;第一个命中 CIDR 的有效地址成为当前会话或数据报的固定目标。 - `dns.ipv6: false` 时不请求或采用 AAAA。 - 运行时 DNS 不可用或失败时继续下一条规则,不使用引导解析器替代。 -- `GEOIP` 不执行反向 DNS,也不接受 `reverse_match`。 +- `GEOIP,!cn,...` 对同一次 CIDR 匹配结果取反,不执行反向 DNS。DAT 的 `reverse_match` + 字段按 Mihomo loader 行为接受但忽略;是否取反只由 selector 的前置 `!` 决定。 ## 生命周期 -- `validateConfig` 只校验结构、URL、code、去重和引用上限,不读磁盘、不联网。 +- `validateConfig` 只校验结构、URL、selector 和去重,不读磁盘、不联网;不限制 GeoData 唯一引用数或 GeoSite DNS policy 数。 - `prepare` 注册去重后的需求并读取当时可用的本地快照,不等待更新。 - Manager 只为当前公共实例构建匹配器;停止或销毁实例时释放需求和快照。 - 后台更新通过完整校验后原子发布不可变快照。新流使用新快照,已经完成的选路不回溯。 +- 重载按已注册的 selector 重建属性索引与匹配器;候选失败保留旧整体快照及旧有效资产。 +- 分类表在加载时按 code 排序,运行时大小写无关二分定位;规则仍按配置顺序首命中。 + 每次业务选路捕获一份当前不可变快照,整次求值复用,不在每条 Geo 规则上重复读取 ArcSwap。 +- TUN UDP 的 IP 目标按五元组固定 action;源地址/端口、UDP 和入口由源关联固定, + 关联内按目的 IP/端口独立记录。首包完成嗅探与选路后,活动流不再逐包读取 DNS + 提示或匹配规则,DNS/GeoData 更新只影响新流。状态仅保留 action、活动时间和 + 可选的已认证 QUIC 连接标识,不持有域名、DNS 答案或 GeoData 快照。 +- 已启用的 QUIC 嗅探端口中,同五元组出现新认证 Initial 时重新选路;同标识重传、短头、未认证候选和 + 不支持版本不使既有流重新选路。每目的 30 秒空闲失效,10 秒间隔在关联活动时 + 回收,完全空闲的源仍由既有关联清理回收;不创建逐流任务、队列或 socket。 +- 显式域名目标和非 TUN UDP 继续逐包选路/解析,不把域名冻结为旧 DNS 答案。 + 固定 action 不是固定代理组叶子:同源关联仍复用每 route target 的物理 transport, + 已有 transport 保持原成员,只有新建物理 transport 读取新的组选择。 + 相比 Mihomo 的按源关联首包选路,本实现细分到五元组,不宣称两者完全同义。 - Controller 切换代理组不会重启正在进行的下载或迁移既有连接;只有之后新建的 GeoData 下载物理 transport 使用新选择。 - `measureDelay` 不注册 GeoData、匹配器或更新任务。 -## 资源上限 +## 内存与安全边界 -| 项目 | 上限 | -| --- | ---: | -| `geosite.dat` | 16 MiB | -| `geoip.dat` | 32 MiB | -| 每文件顶层分类 | 4,096 | -| 每实例 GeoSite + GeoIP 唯一 code | 16 | -| 选中 GeoSite Domain 记录 | 65,536 | -| GeoSite value 总字节 | 2 MiB | -| Regex 记录 / 源码总量 | 512 / 64 KiB | -| 原始 CIDR 记录 | 320,000 | -| GeoData 分配容量 | 8 MiB | -| Regex 保留内存 | 512 KiB | -| 单条 Regex 编译预留 | 最多 3 MiB,且不超过剩余预算 | +所有平台均不设置 GeoData 原始记录、分类、唯一引用、累计 value/Regex 源码字节或 +资产文件大小的独立额度,也不设置 GeoData 总内存预算;不因数量静默截断或丢弃记录。 +DNS GeoSite policy 没有独立项数/引用数上限。普通 YAML 256 KiB、显式 rules 1,024 条/ +合计 128 KiB/单条 1 KiB 仍是配置入口边界,不是 DAT 内记录数限制。 -每次扩容前执行 checked arithmetic 和同一分配账本检查。CIDR 可以去重、删除被覆盖前缀并无损合并对齐 sibling;原始记录数在压缩前检查。预算包括 I/O 暂存区、索引、规范化临时区、匹配器容量和 DFA 保留内存。 +基础分类、字面值和正则只存一份,属性交集使用共享 arena 的紧凑索引;多个交集仍会 +增加索引容量与加载成本,不能把共享存储理解为内存常量。保留 checked arithmetic、 +fallible Vec 扩容、合法 protobuf 帧/长度、code、选中匹配值的 UTF-8 与非空检查、CIDR +地址宽度/前缀和选中正则的语法检查。紧凑 value/Regex arena 的 offset/length 仍须 +能表示为 u32;这是存储表示边界,不是可配置的内存预算。CIDR 仍无损去重、删除被覆盖 +前缀并合并对齐 sibling。分配失败可能返回错误;系统 OOM 或第三方不可恢复分配失败 +不保证可恢复。 + +容量账本仅作诊断:统计 VCore 自有 matcher/index/value/临时 Vec 的可见容量,扩容时 +记录新旧缓冲可能重叠的容量;没有最大值、不控制准入。`regex::bytes::Regex` 不提供 +完整内部状态的准确 `memory_usage`,所以账本**不包含正则内部编译状态、搜索缓存、 +动态 scratch、配置需求集合、allocator 开销或整个进程 footprint**。这些未计入的 +内存并不是零;accounted peak 不能作为加载或运行期峰值上界。 +50,000,000 bytes 是限定输入与联合负载下的进程实测验收目标,不是加载器保证。 + +互通和内存/吞吐压力由公开 [container-benchmark](https://github.com/OneXray/container-benchmark) +维护;`interop`、`stress` 和 `compare` 以 `--source vcore=PATH` 显式指定被测 checkout。 +VCore 自有脚本仅负责编译。真实资产的类型分布、明确标识的四类型/复杂正则合成用例、 +属性/反选见证、加载与新旧快照重叠、联合吞吐/DNS/RSS 结果以 benchmark 的本轮文字结论为准。 +历史 Domain/Full 子集或单纯离线加载不能替代完整真实资产的 fd-TUN 压力,也不能证明 +任意正则的加载峰值。Linux RSS 不替代 Apple Network Extension physical footprint; +少量转发见证不等同逐条规则语义穷举。 ## 失败语义 -- 非法规则、URL 组合、code 或资源越界使配置校验失败。 +- 非法规则、URL 组合、selector 或通用配置入口越界使配置校验失败。 - 文件缺失、损坏、下载失败或检查超时不使实例生命周期失败;对应资产保持不可用或继续使用上一份有效快照。 +- 损坏的外层资产或无效的所选记录导致加载/更新失败;首次失败保持对应种类不可用, + 失败重载保留旧整体快照,失败更新保留旧整体快照和磁盘资产。 - 不可用的 GeoIP 规则不得触发惰性 DNS。 - 调度状态损坏时在更新锁内重置;异常退出留下的更新状态在锁释放后恢复。 diff --git a/docs/invoke-api.md b/docs/invoke-api.md index 4087403..f60fae9 100644 --- a/docs/invoke-api.md +++ b/docs/invoke-api.md @@ -1,6 +1,6 @@ # VCore Invoke API -业务接口版本为 5,配置结构修订版为 27。配置只通过内联的 `configYaml` 或 `configYamls` 传入;每份已加载的 VCore 运行时最多拥有一个公共实例。代理组实时选择沿用 Controller,不增加 Invoke method 或版本协商。静态 ECH 只使用节点内联配置,不新增 bootstrap DNS 入参。 +业务接口版本为 5,配置结构修订版为 31。配置只通过内联的 `configYaml` 或 `configYamls` 传入;每份已加载的 VCore 运行时最多拥有一个公共实例。代理组实时选择沿用 Controller,不增加 Invoke method 或版本协商。静态 ECH 只使用节点内联配置,不新增 bootstrap DNS 入参。 ## C ABI @@ -218,7 +218,7 @@ stopped -> preparing -> prepared -> starting -> running ### `start` -Apple 和 Android 的 TUN 启动参数: +Apple、Android 和 Linux 的 TUN 启动参数: ```json { @@ -229,11 +229,16 @@ Apple 和 Android 的 TUN 启动参数: } ``` -Android 使用 `rawIp`。非 TUN 配置必须省略 `tunFd` 和 `tunFraming`。 +Android 和 Linux 使用 `rawIp`。非 TUN 配置必须省略 `tunFd` 和 `tunFraming`。 - `tunFd` 由宿主借用,宿主必须预先设置 nonblocking。 - VCore 校验后建立带 `CLOEXEC` 的副本,只关闭副本。 -- Apple 只接受 `utun`,Android 只接受 `rawIp`。 +- Apple 只接受 `utun`,Android 和 Linux 只接受 `rawIp`。 +- Linux 要求真实单队列 TUN、关闭 PI/VNET header、实际 MTU 1500;普通 socket/pipe + 不能作为生产 TUN fd。校验失败保持 prepared,可修正宿主输入后重试。 +- Linux 宿主创建并保持 TUN 参数和路由隔离,保证 VCore 物理出站不重新进入 TUN; + VCore 不创建接口或修改系统路由。跨网络命名空间的参数校验需要宿主提供相应权限, + 完整 fd 契约见 [TUN 平台层](tun-platform.md)。 - 从 prepared 配置创建本次 session 的代理组选择状态。 - 所有监听器和关键数据面成功后才进入 running。 - GeoData 更新只在启动后按需后台运行,不属于启动关键路径。 @@ -307,6 +312,10 @@ ProtectFd(fd) -> bool - 含 TUN 的实例必须在 `prepare` 前注册;非 TUN 和 `measureDelay` 不需要。 - 每个出站 TCP/UDP socket 在 connect 前同步调用 protect。 +- socket 初始化和 protect 可以在受跟踪的 Tokio 阻塞线程执行;不保证回调来自某个 + 固定业务线程。取消不能中断已经开始的同步回调,Stop 返回前必须等待其完成。 +- TCP/UDP 共用每初始化作用域 64 个未完成提交的内部许可;提交前异步等待并继承 + 调用方取消/期限,不因繁忙拒绝业务连接。许可直到阻塞任务实际结束才释放。 - 返回 false、抛出异常或 controller 失效都会使当前连接失败关闭。 - 回调必须快速、同步、非阻塞,且不能重入 Invoke 或注册接口。 - TUN 租约存活期间不能替换或注销 controller;`stop`/`destroyInstance` 是释放屏障。 diff --git a/docs/runtime-resource-policy.md b/docs/runtime-resource-policy.md index ac1bcd0..ddbe626 100644 --- a/docs/runtime-resource-policy.md +++ b/docs/runtime-resource-policy.md @@ -5,34 +5,50 @@ ## 原则 1. 数据正确性、安全边界和同步停止优先。 -2. 队列、缓冲区、缓存、wire 大小、解析大小、超时和空闲清理必须有界。 +2. 数据面队列、缓冲区、缓存和解析器保留局部容量边界;空闲资源必须有回收机制。容量界与时间回收不是同一保证。GeoData 在所有平台均不设记录条数硬上限、截断或总内存预算。 3. TCP 会话、普通 UDP 关联、半开连接、出站握手和活动 DNS 传输不设置固定业务数量上限。 4. 队列满时按局部协议语义丢当前项目或失败,不能阻塞 TUN 回调或全局循环。 5. 当前值和峰值只用于诊断,不参与 admission。 6. 平台内存采样是尽力而为的遥测,不改变生命周期结果。 +长期业务引擎(FFI 的 TUN / 非 TUN 及 Windows Session Host)统一使用 +Tokio `new_multi_thread()`,不按 TUN 分叉,不自行读取 CPU 数或设置 worker 数。 +worker 数遵循 Tokio 默认行为(包括其 `TOKIO_WORKER_THREADS` 环境覆盖); +线程栈使用 Tokio / Rust 默认值,不固定为 1 MiB。 +短生命周期的 `prepare` / `measureDelay` 保留共用的单线程执行策略,不按 TUN 分叉。 +工作线程(含阻塞任务线程)继承 Invoke 重入保护, +Apple 平台另保留线程局部日志作用域;同步停止仍等待子任务与运行时线程退出。 +Release 构建优先吞吐(`opt-level = 3`),保留 thin LTO 与现有缓冲、队列边界。 + ## TUN 结构上限 ```text 原始包 / MTU 1,500 字节 最终代理 UDP 负载 1,452 字节(Windows 1,352) 包队列 256 -普通事件 / UDP 响应 128 -DNS 入站 / DNS 响应 128 / 128 +普通事件 / TCP accept 128 +每关联 UDP 入站 64 +普通 UDP 响应 4,096 +DNS 响应 128 每个 TCP 方向缓冲区 32 KiB TLS / XHTTP 缓冲区 64 KiB DNS 类型化缓存 256 项 DNS 原始响应缓存 64 项 / 256 KiB TUN 域名提示 256 项(按需) -GeoData 分配容量 8 MiB ``` Windows L3 接口及其 Session Host netstack 使用 1400 MTU,因此按 IPv6 UDP 头保守计算的响应负载上限是 1352;表中的 1500 是跨平台原始包解析上限和其他 TUN 平台的固定 MTU。 -- DNS 响应和普通 UDP 响应使用不同队列,但共享 netstack UDP 入站接收器。 +- 普通 UDP 在 reader 同步分流并直接提交到每源关联,不经过 TCP Driver 或共享 UDP 入站队列;DNS 查询独立提交受跟踪任务。 +- 每关联请求、普通响应和 DNS 响应使用独立内部容量,不扩大 128 项的 TCP accept,也不增加公开配置字段。 - TUN 域名提示只在 TUN 配置实际包含域名规则时创建,并从空容量按需增长。 +- 域名提示的读写使用短同步锁,临界区内不等待 IO 或执行异步操作;新 TUN IP 流读取当前提示,活动 UDP 五元组保持既定 action。 - ICMP 响应复用原始包出站队列,不创建独立任务或长期状态。 -- 每次扩容前执行 checked arithmetic 和局部预算检查。 +- TUN 读写每批最多处理 8 个已就绪包,不等待凑批。复用至多 8 个 1500 字节读缓冲区; + 唯一 writer 公平轮转 TCP/ICMP raw、普通 UDP/DNS 响应三通道,至多暂持 8 包, + UDP 在复用 MTU frame 中构包,不增加中转队列。非法输出也计入工作预算。批次中途取消或 + 失败仍按已完成结果逐包统计;非法包局部隔离,Unix 系统调用保持单包边界。 +- 有局部预算的结构在扩容前检查额度;GeoData 保留格式、语法、整数可表示性、正则库默认局部保护和实际分配失败检查,不按记录条数截断。 ## TCP 与握手 @@ -44,6 +60,26 @@ Windows L3 接口及其 Session Host netstack 使用 1400 MTU,因此按 IPv6 U - 超时、取消、EOF 和协议错误负责回收;停止必须等待全部已跟踪任务结束。 - 引导解析器最多使用四个工作线程;全部忙时在调用方既有期限内等待,不返回人为容量错误。 +### 物理 socket 初始化 + +TCP/UDP 共用 Dialer:创建 socket、nonblocking、源地址/Windows 接口绑定与 protect +在当前 Tokio runtime 的受跟踪阻塞任务中完成,避免 FD 表扩容等同步系统调用占满 +业务 worker。异步 connect、reactor 注册与收发仍在原 runtime,不新增独立线程池、 +CPU 数判断、业务数量配额、FD 预热或 DNS socket 复用。 + +每个初始化作用域最多持有 64 个未完成的阻塞提交,TCP/UDP 共用内部许可。 +在登记任务、创建 socket 和 `spawn_blocking` 前异步等待许可,不因繁忙返回容量错误; +等待继承调用方期限和取消,Stop 关闭准入并唤醒等待者。许可由阻塞任务持有到真正 +完成或被丢弃,调用方提前取消不会释放已开始工作的许可。它不限制已建立连接, +不增加独立线程池或公开配置;也不把等待调用的数量或整进程内存变成有界保证。 + +取消关闭结果接收器并跳过尚未开始的初始化;已开始的系统调用或同步 protect 不能 +强制中断,完成后丢弃未交付的 socket。每个 Running Session/测速图拥有独立的 +初始化作用域。Stop 先关闭 admission,再取消并等待业务/协议任务,最后等待已登记 +初始化任务完成,才释放平台回调租约。测速 runtime 的销毁也使用完整 join,不能 +以有限 shutdown timeout 脱离在途初始化。protect 仍必须快速、同步、非阻塞; +线程位置变化不放宽宿主回调契约,也不改变保护失败关闭或 Windows 双绑定策略。 + ## 共享流传输基础 `stream-transport` 仅包装传入 IO,不创建 socket 或 DNS。WS/HTTP响应首部最多16 KiB、100字段;WS用户请求头最多100字段,额外固定升级头和early-data仍计入16 KiB总预算。WS early-data最多2,048原始字节,头名称/路径后缀由类型化选项区分。WS消息和单帧最多64 KiB,写入切块16 KiB;HTTP首包正文直接写入,不整体复制或持续按HTTP正文定界。 @@ -63,12 +99,41 @@ XUDP现在只拥有已认证流上的帧编码;VLESS响应头由VLESS包装层 普通 TUN UDP 关联: - 关联表不设固定项数; -- 每个关联的入站队列有界,满时只丢当前数据报; +- 每个关联的入站队列最多 64 项,满时只丢当前数据报,不等待慢关联并阻塞其他来源; +- 普通响应队列最多 4,096 项,满时只丢当前响应;没有共享 UDP 入站中转队列; +- reader 拥有关联表和入站分发,唯一 TUN writer 直接、公平消费普通/DNS/TCP raw 三个通道;写回等待不阻塞 reader,不增加逐关联写回任务; +- TCP/ICMP ingress 满时非阻塞丢当前完整 IP 包,避免阻塞 UDP/DNS,记录 packet queue drop;TCP 重传恢复,不引入 pending FIFO; - 使用代次感知所有权和子取消令牌; - 空闲超时 30 秒,清理周期 10 秒; - 清理时先从表中删除,再取消子任务; - 只有成功入队的请求或响应刷新活动时间; -- 父运行时停止时删除、取消并等待全部子任务。 +- 源关联内的 IP 五元组仅保留规则 action、活动时间和可选 QUIC 连接标识, + 不创建逐流任务、队列或 socket;不设流数量上限。各目标独立 30 秒空闲失效, + 关联收发时最多每 10 秒扫描回收并收缩明显过大的表容量;关联关闭释放整个表。 + 响应只刷新自身已存在且未过期的目标,不创建状态或复活过期流。 + 五元组活动以接收请求或读到匹配出口响应为准;源关联仍只以成功入队刷新, + 响应队列满不会延长源关联所有者的生命周期。 +- reader EOF、错误或取消时,先取消其 UDP 子作用域,再删除、取消并等待全部关联和 DNS 任务;父运行时统一取消并等待唯一 writer 与 netstack。UDP 子作用域停止不取消调用者令牌。 + +### 高吞吐优化边界 + +普通响应的 4,096 项是所有 TUN 平台共用的内部默认值,用于吸收收包任务与唯一 +writer 之间的短时突发。Linux NAT 的无热路径探针 Release 容量对照支持从 +1,024 项扩大至此值;它不是逐关联容量、业务并发配额或无损吞吐保证。 +相对原值,多出的 3,072 项在 1,452 字节负载下最多多持有 4,460,544 字节 +payload,另有元数据与分配器开销;实测 RSS 增量不能作为最坏内存上界。 + +已采用分类二分定位、TUN UDP 五元组 action 复用、reader 直接分流、单 writer +三通道公平轮转和已就绪批次处理。netstack 每批执行维护,沿用单个待处理 RX 包 +和原有 TX 队列,不增加重复中转队列。每关联容量、DNS 保留容量、socket 缓冲、 +批次大小和线程策略不随普通响应容量扩大;队列 Full 不等待、重试或刷新源关联活动。 + +剩余丢包必须分别核对 VCore 队列、内核 TUN、物理出口和客户端 socket,不能以 +内部 drop 为零推断端到端无损。2 Gbps UDP 叠加 DNS 的端到端无损目标仍未通过。 +Linux TUN `txqueuelen` 属于宿主网络配置,VCore +不修改借用接口的队列长度或 qdisc。扩大宿主队列的实验结果不能直接推广到其他 +平台;Linux 吞吐与 RSS 结果也不替代 iOS/tvOS physical footprint 或真机验收。 +诊断计时探针、强制让步、Full 后重试和无协作预算发送不属于生产优化策略。 嵌套代理协议可以增加有界帧头,但最终解封装负载仍不得超过调用方按有效 MTU 给出的上限;其他 TUN 平台为 1,452 字节,Windows 为 1,352 字节。 @@ -115,17 +180,61 @@ IP-only协议接点持有`ResolutionContext`,runtime DNS通过Weak绑定,不 ## GeoData -- GeoSite 与 GeoIP 共享 8 MiB 分配容量。 +- 所有平台完整加载被引用分类的所选记录,不设记录条数硬上限或截断;GeoIP 与 GeoSite 没有优先额度分配。 +- 同种基础 code 按 ASCII 大小写归一,业务规则与 DNS policy 共用分类和值存储。属性取交集;正反选择器共享匹配器,多种属性筛选共享值及已编译正则,不重复解码同一分类。未引用分类不构建匹配器。 +- 各平台均不设独立 GeoData 分类、引用、文件大小、累计源码/值字节或总加载内存预算;DNS GeoSite policy 也不保留独立项数/引用数上限。 - Loader 只解析被引用的分类,其他分类按 wire 长度跳过。 -- 匹配器使用紧凑连续存储,运行期匹配不分配。 -- 正则源码、编译预留和保留 DFA 内存都有独立上限。 -- 资产缺失或更新失败只使对应种类不可用,不阻塞准备或启动。 +- Domain/Full 匹配器使用紧凑连续存储,运行期查找不分配。Regex 使用共享的 + `regex::bytes::Regex` 并关闭 Unicode 匹配模式;库管理编译状态和搜索缓存,搜索可能 + 分配 scratch、增长缓存或竞争共享缓存,不能将 Domain/Full 的无分配性质扩展到 Regex。 +- 分类按 code 二分定位;TUN UDP 五元组固定规则 action,不保留 GeoData 快照 + 或域名。没有全局目的缓存或旧快照引用;各目标独立空闲回收。 + 活动流的规则数据更新在新流生效;DNS 查询、域名目标和非 TUN 路径不受此固定策略影响。 +- VCore 不显式构建 dense DFA,也不配置 NFA、DFA 或 determinization 额度;保留正则库 + 默认的编译大小、嵌套和搜索缓存保护。不新增正则记录数、累计源码或总内存预算。 + 容量账本仅计 VCore 自有容器的可见容量,排除正则内部状态、动态 scratch 和缓存; + 未计入不代表零,账本不能充当进程峰值上界。局部库保护同样不是所有 Regex 的总量保证。 +- 格式、整数表示、值与 Regex 语法校验和可恢复分配错误仍失败关闭。属性筛选排除的记录不进入匹配器;有效分类的空筛选仍可反选,缺失/不可用分类不会反选为全匹配。 +- 首次缺失或无效资产使对应种类不可用,实例以 degraded 状态继续准备/启动;两种资源可用性独立。更新任一种资产都重新构建所需快照。失败重载保留旧整体快照,失败更新保留旧整体快照和资产,不回溯已完成的选路。 + +公开 [benchmark](https://github.com/OneXray/container-benchmark) 的 `stress` 默认加载 +完整增强 DAT,并叠加 2 Gbps / 60 秒 / 1,000 QPS DNS。可选 `--geodata-records` +只裁剪实验输入,不是生产限制;`compare` 保持 CN 对照。真实资产中的类型计数、 +正则编译/搜索实验、双快照重叠和 TUN 实测分别报告,不能互相替代。旧 dense DFA +实现的实验不直接证明常规正则实现的峰值、性能或复杂输入行为。 +实际结果以 benchmark README 为准,Linux RSS 不是 Apple 真机 footprint 或任意输入 +50,000,000 bytes 保证。VCore 自有脚本只负责编译。 完整边界见 [GeoData 规则与资产](geodata.md)。 +## 当前限制的保留判断 + +运行时维持 `standard` 行为,不新增 `resourceProfile` API 或活动业务流准入配额。 +GeoData 不设置平台条数硬上限或截断,也不设置总内存预算。完整 CN 的代表性协议冷/热、双栈入口、取消/重建及资源叠加 +短测曾有明显余量。旧 dense DFA 实现的完整增强资产 2 Gbps 混合压力 RSS 为 +53,542,912 bytes,离线双快照 probe 为 64,045,056 bytes,均超过 50,000,000 bytes +目标;它们是历史基线,不是当前常规正则实现的验收结果。 +这些不同范围的结果不能互相抵扣,也不是正式 Provider 的签收;后续优先评估 +匹配器存储与更新重叠的优化,不以数量截断达标。其他移动资源策略仍需同一最终 +候选的完整矩阵与真机结果。局部队列、解析和缓冲继续保持有界。 + +完整 CN、IPv4、16 条背景 TCP、300 秒、DIRECT/代理各承担一半流量的 1 Gbps 子集 +曾测得最坏 7,864,824 bytes;它不覆盖最大并发、DNS 冷查询风暴、UDP、TUN、协议池 +或 iOS/tvOS 实机,不能据此删除局部容量边界,也不能承诺任意负载低于 50M。 + +- 活动 DNS/TCP/UDP/握手已有无总数配额的语义,继续保持;4 个 bootstrap 工作者、 + DNS 空闲 TCP 池 4/2、TUN accept 队列 128 都不是活动业务并发上限。 +- 保留队列、缓存、单流缓冲、窗口与解析边界;若出现 VCore 瓶颈,依据队列高水位、 + 丢包、背压和峰值测量调整,不通过无界积压掩盖问题。对端 UDP 设施丢包不是核心归因。 +- AnyTLS idle 目前只有 30 秒空闲/30 秒扫描,没有数量或字节 cap;后续 churn 应重点 + 测量突发后的保留。TUIC 分片重组按关联持有,HY2 按物理会话共享,不能按相同总量估算。 +- 普通规则条数和 DNS nameserver 列表数是配置复杂度边界;当前小规则表、高速 TCP + 结果不足以证明可删除,本轮不改。若最终确需业务数量门禁,只在独立 iOS/tvOS + low-memory 策略中基于实测另行决定,不影响其他平台。 + ## Apple 内存遥测 -Apple 目标通过 `TASK_VM_INFO` 尽力记录当前 physical footprint、进程生命周期峰值和限频阈值事件,运行期间最多每 30 秒采样一次。 +iOS/tvOS TUN 通过 `TASK_VM_INFO` 尽力记录当前 physical footprint、进程生命周期峰值和限频阈值事件,运行期间最多每 30 秒采样一次。macOS 不启用该周期任务;平台接入与事件标识见 [TUN 平台层](tun-platform.md)。 - 采样失败只记录一次警告; - 遥测不写入 `lastError`; @@ -137,6 +246,12 @@ Apple 目标通过 `TASK_VM_INFO` 尽力记录当前 physical footprint、进程 运行时可记录 TCP、半开连接、UDP、握手和 DNS 的当前值/峰值,以及缓存命中、singleflight、队列丢弃、连接池回收、非法包和 ICMP 统计。 +独立 netstack 的 `udp_drops` 只记录通用 UDP endpoint 的入站丢弃;生产 TUN 使用 +TCP-only netstack,因此该值为零不代表 UDP 无丢包。运行时记录 +`udp_association_queue_drops` 和 `udp_response_queue_drops`,保留两者合计的 +`udp_queue_drops`;TCP/ICMP ingress Full 记录 `packet_queue_drops`。这些计数 +不包含物理 socket、内核 TUN 或对端设施中的丢包。 + 日志必须有界且脱敏,不记录目标、DNS question、UUID、凭据、密钥、负载或完整配置。 仅`cfg(test)`/`interop-test`启用的`ResourceProbe`按测试作用域记录RAII当前值/峰值;子任务显式继承作用域,不使用进程全局reset或更换生产allocator。当前接入物理TCP/UDP、共享流/QUIC驱动与session、数据报association、DNS池/等待者及既有运行时活动guard。Hysteria2 的待完成分片通过 `Reassembly` 登记,交付后释放 Packet ID 和重组资源;零计数必须与实际执行过的重组路径一起解释。 diff --git a/docs/testing-isolation.md b/docs/testing-isolation.md index 45c07e5..02a68b1 100644 --- a/docs/testing-isolation.md +++ b/docs/testing-isolation.md @@ -9,15 +9,49 @@ - 纯内存、编解码、离线配置或 mock 单元测试不属于外部服务端互通测试。不能把宿主网络服务改称 mock 来规避隔离。 - 尚未完成容器化、容器不可用或平台不支持的服务端用例记为 `BLOCKED` / `NOT RUN`,不得回退到宿主启动。Linux 测试对端不代表 VCore 新增 Linux 产品支持。 +## 工程边界与结果保留 + +协议互通的容器编排、对端下载、网络消费者和压力测试统一由公开的 +[container-benchmark](https://github.com/OneXray/container-benchmark) 管理。 +互通入口为 `container-benchmark interop --source vcore=PATH`,压力入口为 +`container-benchmark stress --source vcore=PATH`;真实执行必须显式指定 checkout, +不推断父项目或相邻路径。VCore 的 `vcore-scripts` 仅编译平台产物,Rust 的 +离线/纯内存回归仍保留在内核;独立工程不存在不影响 VCore 编译。 +优先 Mihomo listener;不支持的代表场景由官方 Xray-core、Hysteria2、V2Ray 或 +Caddy/Xray 分层拓扑补验。`--backend` / `--protocol` 筛选,`--list` 只作离线列举。 +当前入口在 Apple Silicon 的 Apple Container 上,每轮独立构建一次,再顺序运行 +各对端组的原站、协议对端、生产 ABI 消费者三个 Linux 容器;全部为 NAT、5 CPU / +8 GiB。此入口不代表历史完整字段、代理链、长测或设备矩阵已恢复。 +互通、压力和两核性能对比是分别执行的入口,不用其中一项通过抵扣另一项; +迁移代码或离线列表通过不代表本轮已经重新执行网络用例。 + +每轮结束(成功、失败、超时或中断)先回收本轮自有进程、容器和临时网络/镜像,再清理 +本轮生成的构建目录、下载包、规则、证书、日志、采样及原始流量数据,仅保留脱敏 +文字结论。结论记录实际命令、源码/锁文件/对端身份、指标、失败原因和清理结果; +后续复验重新取得输入并执行,不把已清除产物的记录作为可恢复候选。 +生产发布产物与受版本控制的测试源文件不属于本轮临时数据。带所有权标记的共享 NAT +网络及按日检查身份/校验值的公共依赖缓存可以跨轮保留;本轮配置、证书、构建和日志 +不得混入该缓存。 + ## 执行要求 -1. 使用本次任务独占、可识别所有权的容器和私有网络。macOS 使用 Apple Container 的 host-only 网络;不发布宿主端口,不改宿主 VPN、路由、DNS 或防火墙。 +1. 使用本次任务独占、可识别所有权的容器;测试网络仅使用 NAT 或 bridge,禁止 host-only。macOS 的 Apple Container 使用带所有权标签的共享 NAT 网络;不发布宿主端口,不改宿主 VPN、路由、DNS 或防火墙。共享网络可跨轮保留,不清理其他任务的网络。 2. 协议对端使用官方最新稳定发行二进制,通过官方 `latest` 下载链接获取;不查版本 API、不本地编译、不修改第三方源码、不静默复用旧缓存。镜像使用官方稳定标签,在当次运行记录解析后的 digest。记录容器内实际版本、二进制和下载包校验值。 3. 保留 Mihomo 回环保护和第三方默认 socket 行为;不能通过关闭保护、放宽数据正确性/超时或重试业务包掩盖失败。 4. 原站和协议对端的地址必须来自本次容器网络。配置、证书与测试凭据仅放临时目录,fixture 只读挂载,日志保持有界且脱敏。 5. 注册清理后再启动资源。成功、失败、超时、SIGINT 和部分启动都必须停止并删除本次拥有的容器,检查剩余资源;不得停止或清理其他任务的容器。 6. 报告分别记录隔离拓扑、真实流量结果、源码身份和清理结果。基础设施/清理失败不能记为互通通过;定向诊断通过不等于阶段签收。 -已批准的局部例外:H3/mTLS 测试网关可用官方最新稳定版 `xcaddy` 编译官方最新稳定版 Caddy,再导入隔离容器。保留 go.mod/go.sum、工具版本、构建日志、二进制 hash 和容器内版本;不修改第三方源码、不新增协议 decoder。Caddy 只终结 H3/TLS 并转发 HTTP,XHTTP 会话与 VLESS 解码仍由同一个原生 Xray handler 负责。此例外不允许编译 Mihomo/Xray 等对端,也不把本地构建称为官方预编译产物。 +当前 H3/mTLS 代表用例使用标准官方 Caddy 预编译二进制,真实 +`require_and_verify` 客户端认证后,通过 H2C 转发到同一个原生 Xray XHTTP handler; +Caddy 不实现代理协议 decoder,不将两个独立 XHTTP listener 当作共享下载会话。 +历史批准过的局部例外仍仅限必要时用官方 latest `xcaddy` 编译官方 latest Caddy, +记录 go.mod/go.sum 摘要、工具版本、构建结果和 hash;不修改第三方源码, +也不允许编译 Mihomo/Xray 等对端或将自编产物称为官方预编译产物。 + +正例须核对真实双向业务与原站所见对端来源。Hysteria2 跳跃保持同一 UDP 关联 +至少 16.5 秒,并观测至少两个实际目的端口;仅配置端口集合不是跳跃通过。 +mTLS 拒绝、服务端禁用 UDP 等负例须证明被拒绝业务在原站零交付,不能只以 +客户端超时作为认证或限制生效证据。所有这些见证也须保持容器隔离和本轮清理。 历史宿主测试保留为历史证据,不追改为容器测试。旧脚本即使有 `--container`,若原站或反向服务仍在宿主,也不满足本规则;迁移前不得用于新的服务端验收。 diff --git a/docs/tls-dependencies.md b/docs/tls-dependencies.md index 61d545e..2013881 100644 --- a/docs/tls-dependencies.md +++ b/docs/tls-dependencies.md @@ -1,7 +1,12 @@ # TLS 依赖与发布 版本、完整 Git revision 和 registry 校验值以 Cargo.toml/Cargo.lock 为准, -PR/发布前由 `check tls-dependencies` 审核,不在文档复制一份易失效的包清单。 +PR/发布前依据下述来源约束审查 manifest、lockfile 和 resolved graph, +不在文档复制一份易失效的包清单;编译成功不等于来源或许可证审查通过。 +[Tests workflow](../.github/workflows/test.yml) 的 +`Audit the locked TLS dependency graph` 步骤直接检查 +`cargo metadata --locked --all-features`,维护批准的版本/revision、同源依赖、 +TLS provider、ECH 算法和 SS2022/AWS-LC 消费链;没有独立的检查 CLI。 ## 来源与后端 @@ -28,9 +33,11 @@ SS 日志抑制与未修补风险见[出站](outbounds.md#shadowsocks-2022)。 feature 约束,更新 Cargo.lock 后执行 locked 构建与相关测试,不混用本地和 Git 来源。 2. 发起或更新 VCore PR 前,先将所需 fork 改动发布至 OneXray/boring 的 `release`, 再将三个 crate 一起切回 `git = "https://github.com/OneXray/boring", branch = "release"`。 - 更新 Cargo.lock;若 revision 前移,同步依赖审计中的批准 revision。 -3. 在不依赖本地 fork 的 checkout 通过 `check tls-dependencies` 和相关验证后再提交 PR。 - 该命令是 PR/发布门禁,本地 path 开发态不要求通过,也不为开发态放宽来源检查。 + 更新 Cargo.lock;若 revision 前移,对新的完整 revision 重新完成来源与能力审查。 +3. 在不依赖本地 fork 的 checkout 核对三个 crate 的 Git release 来源、完整锁定 + revision、registry 校验值与 provider/feature graph,再执行相关构建和定向回归。 + 同步上述 workflow 的批准身份并通过其 metadata 门禁;workflow 配置存在不等于 + CI 已运行或通过。本地 path 开发态不替代发布来源,也不放宽发布约束。 ## 身份与原生接口 @@ -70,11 +77,12 @@ boring MIT/Apache-2.0、BoringSSL 随源通知及 Android C++ runtime,不能 1. 选择官方最新稳定依赖;fork 同步上游并先完成普通 TLS 回归。 2. 本地按上述流程验证 fork 变更;PR 前发布至 release,同次修改 manifest、lockfile - 和依赖审计的批准 revision。 + 并重新审查锁定 revision。 3. 重跑确定性向量、ClientHello/share/证书/签名、恢复/取消/期限及受影响容器数据面; 分别验证 AnyTLS 标准 TLS 和 REALITY/JLS,不能互相抵扣。 4. 在没有相邻 fork 目录的干净 checkout 执行 locked fetch、离线测试、相关平台构建和 - 原生消费者。全目标 --no-run 只是编译,网络 peer 仍遵守[隔离规则](testing-isolation.md)。 + 原生消费者;平台脚本及 delivery 完整性检查不执行该消费者。 + 全目标 --no-run 只是编译,网络 peer 由独立 benchmark 按[隔离规则](testing-isolation.md)执行。 5. 按[验收边界](acceptance.md)完成对应设备/安装门禁并保存当次证据。 回退使用新提交恢复仍可获取的已验证 lockfile,不恢复已退役 fork。 diff --git a/docs/tun-icmp-dns.md b/docs/tun-icmp-dns.md index 0e9c8b2..feb203b 100644 --- a/docs/tun-icmp-dns.md +++ b/docs/tun-icmp-dns.md @@ -76,14 +76,20 @@ dns: `nameserver-policy` 是有序映射: -- 最多 16 项; -- selector 只接受 `geosite:[,...]`,同一项内为 OR; -- 同一 code 不能跨项重复;DNS policy 和业务规则合计最多引用 16 个唯一 code; +- 项数与 GeoSite 唯一引用数不设独立上限,仍受整个 YAML 输入大小约束; +- selector 只接受 `geosite:[,...]`,同一项内为 OR;每个 selector 可为 + `code`、`code@attr@attr` 或前置 `!` 的反选,属性为 key 存在性 AND; +- 归一后的相同 selector 不能跨项重复;相同基础 code 的不同属性交集或正反选可并存, + 与业务规则共享分类和记录;`@!cn` 是字面属性 key,不是属性排除; - value 必须是 1–4 个 nameserver; - 首项命中后只在该组内顺序故障转移,组内耗尽不查询主组; -- GeoSite 资产不可用时该 policy 不命中,查询主组。 +- GeoSite 资产或分类不可用时该 policy 不命中,不能因反选变成全匹配; +- 存在分类但属性筛选为空时仍可用,正选不命中、反选匹配合法非空名称。 -Policy 只决定当前 DNS exchange,不改写后续业务动作。 +Policy 只决定当前 DNS exchange,不改写后续业务动作。分类、属性归一和语义边界见 +[GeoData 规则](geodata.md#规则);所有平台均不按 GeoData 条数截断记录。 +Regex selector 记录与业务规则共用 `regex::bytes::Regex`;其搜索缓存由正则库管理, +可能分配 scratch 或发生缓存竞争,不承诺 DNS policy 正则搜索零分配。 ## Wire 校验 @@ -147,16 +153,25 @@ Controller 切换代理组后,只在后续需要创建新 DNS transport 时使 | 队列 | 容量 | | --- | ---: | -| TUN DNS 入站 | 128 | +| 每普通 UDP 关联入站 | 64 | | DNS 响应 | 128 | -| 普通 UDP 响应 | 128 | +| 普通 UDP 响应 | 4,096 | -DNS 和普通 UDP 响应使用不同队列,但共享 netstack UDP 入站接收器。队列满时只丢当前请求或响应,不阻塞全局 UDP 循环。TUN UDP 响应按有效 MTU 减去 48 字节保守限制;其他平台为 1,452 字节,Windows 为 1,352 字节。 +reader 在进入 TCP netstack 前分流 UDP,拥有按源地址建立的关联表,没有共享 UDP 入站接收器或独立 DNS 请求准入数。每普通 UDP 关联有独立请求队列;DNS 查询继续提交受跟踪的任务,不等待上游。队列满时只丢当前请求或响应,不阻塞 reader;TCP accept 仍为 128 项。TUN UDP 响应按有效 MTU 减去 48 字节保守限制;其他平台为 1,452 字节,Windows 为 1,352 字节。 + +唯一受跟踪的 TUN writer 公平读取普通 UDP、DNS 和 TCP/ICMP raw 三个通道,任一通道关闭不丢弃其他通道的待写响应。UDP 构包后直接写平台,不再转发到共享 raw output。DNS 查询观测 permit 保留至该包被平台接受、最终丢弃或取消,写回阻塞和 ENOBUFS 重试不提前释放;Windows Adapter 成功处理不等于框架最终交付。MTU/地址族错误只丢当前响应。 运行时停止会取消 open、send、receive、retry 和 response-send,释放全部传输并等待已跟踪任务结束。停止返回后不得再向 TUN 回包。 普通非 DNS UDP 关联不设固定总数,采用代次感知所有权、30 秒空闲超时和 10 秒清理周期。只有成功入队的请求或响应刷新活动时间。 +普通 TUN UDP 的 IP 目标在源关联内按目的 IP/端口固定规则 action,即五元组独立 +选路,不把整个源的所有目标固定到同一个动作。首包完成嗅探/选路后,活动流不随 +DNS 提示或 GeoData 更新改路;新流使用当前数据。每目标独立 30 秒空闲失效, +10 秒间隔借既有关联收发回收;无逐流任务/队列/socket。新认证 QUIC 连接的首包 +重新选路,同连接 Initial 重传不重选。具体边界见 [GeoData 生命周期](geodata.md#生命周期)。 +DNS/53 劫持仍先于普通关联,每个查询独立执行 nameserver-policy,不受五元组固定影响。 + ## IP-only协议与独立测速接点 公共`ResolutionContext`仅在协议必须取得IP地址的边界解析业务目标或逻辑上游;原始`Destination`保持不变,HTTP Host、TLS SNI和路由仍使用逻辑名称。代理endpoint的prepare bootstrap与该运行期解析分离,不递归使用尚未建立的同一出口。当前静态ECH不查询DNS;动态ECH及其可选宿主bootstrap入口尚未实现,不属于当前Invoke v5。 diff --git a/docs/tun-platform.md b/docs/tun-platform.md index 5e147f0..2107185 100644 --- a/docs/tun-platform.md +++ b/docs/tun-platform.md @@ -2,15 +2,16 @@ VCore 的 netstack、DNS、规则和出站只处理完整的原始 IPv4/IPv6 数据包。平台差异集中在编译期选择的 `platform::TunIo`。 -## Apple 与 Android +## Unix 借用 fd ```text TunRuntime -> RustTunIo -> 宿主持有的 TUN fd 副本 ``` -- iOS/macOS:宿主提供 utun 文件描述符,适配器处理四字节 packet-information 头。 +- iOS/tvOS/macOS:宿主提供 utun 文件描述符,适配器处理四字节 packet-information 头。 - Android:`VpnService` 提供 raw-IP 文件描述符。 -- Linux:产品入口不支持,即使依赖能够编译也会失败关闭。 +- Linux:宿主创建真实单队列 TUN,使用 `IFF_TUN | IFF_NO_PI`、无 VNET header、 + MTU 1500;宿主负责接口、网络命名空间和路由,VCore 不自动接管系统网络。 宿主始终拥有原始文件描述符。VCore 启动时: @@ -19,7 +20,34 @@ TunRuntime -> RustTunIo -> 宿主持有的 TUN fd 副本 3. 把副本交给同步 TUN device,并用 Tokio `AsyncFd` 驱动; 4. 停止时只关闭副本。 -VCore 不调用会修改共享 open-file-description 标志的异步构造器。`tunFraming` 是严格宿主协议:Apple 只接受 `utun`,Android 只接受 `rawIp`,不自动探测。 +VCore 不调用会修改共享 open-file-description 标志的异步构造器。`tunFraming` 是严格宿主协议:Apple 只接受 `utun`,Android 和 Linux 只接受 `rawIp`,不自动探测。 + +Linux 启动前查询真实 TUN 及其所属网络命名空间,验证内核链接参数、实际 MTU 和 +raw-IP 格式;普通 socket/pipe、TAP、PI、VNET header 或多队列设备失败关闭。 +需要可读 procfs 和 TUN 所属 user namespace 的 `CAP_NET_ADMIN`,即使 fd 与 +VCore 在同一网络 namespace,也必须能执行 `TUNGETDEVNETNS`;借用一个有效 fd +不能绕过此权限检查。 +跨命名空间检查仅在专属短线程切换 namespace,完成并 join 后再启动运行时, +不改变 Invoke 或业务工作线程的 namespace;宿主还需提供 `CAP_SYS_ADMIN` 等 +相应 `setns` 权限。同 namespace 不执行 `setns`。 +宿主原 fd 保持有效、非阻塞,运行期间不得改变接口格式或 MTU。 + +Linux 压测可以把 TUN 创建在客户端 namespace,再把借用 fd 交给正常出口 namespace +中的 VCore:客户端业务经 TUN,VCore 的普通 TCP/UDP socket 经正常物理出口。 +接口创建、路由隔离和清理由宿主完成;VCore 不隐式设置 default route 或保护标记。 +首轮验证为 GNU/glibc 原生构建与容器真实 TUN;musl、其他架构、安装交付和设备验收 +不由一次 Linux 容器压测推导。Linux RSS/VmHWM 与 Apple physical footprint 分开报告。 + +tvOS 首版为 17.0+、ARM64 真机和 Apple Silicon 模拟器,共用原有 TunIo、netstack 和 +Dialer;不新增低内存数量上限,不改变其他平台默认行为。iOS/tvOS 的 TUN 生命周期接入 +Apple Unified Logging、TASK_VM_INFO 当前/进程峰值观察及停止后的 allocator pressure relief。 +既有 35/40/45 MiB 阈值仅发出诊断,不是系统限额或业务准入条件;macOS 不启用这组 +移动扩展周期采样。tvOS 事件使用 tvos_memory_*,iOS 保留 ios_memory_*。 + +Apple 公开 NEPacketTunnelFlow API 是 packetFlow 的包读写,不承诺可取得 raw fd。 +VCore 的借用 fd 契约不能证明宿主 KVC 提取 fd 是稳定公开方案。模拟器 socketpair 只验证 +合成 utun 数据面;真实宿主的 fd 获取/桥接、签名与 entitlement、系统路由、设备生命周期 +及物理内存须独立验收。当前没有新增 packetFlow 回调 ABI,也不暗中回退其他接入路径。 包 I/O 规则: @@ -27,8 +55,47 @@ VCore 不调用会修改共享 open-file-description 标志的异步构造器。 - 首个半字节必须表示 IPv4 或 IPv6; - 单次读取缓冲区固定为 1,500 字节; - 写入必须一次完成整个包,部分写入立即失败; +- Apple 写入的瞬时 `ENOBUFS` 不终止整个运行时:只保留当前尚未接受的包,异步退让 + 1 ms 后重试,Stop/取消仍直接释放该 future;不新建后台重试任务、不扩大宿主 socket + 缓冲,也不重发已经成功写出的包。其他错误和部分写入仍失败关闭; - Apple PI 头不进入 netstack,也不计入流量。 +所有平台的公共 TUN 循环按最多 8 包的有界批次推进:等到首包后只收取已经就绪的包, +不等待计时器或未来数据。Unix 在同一次 readiness guard 下逐包读写;每次系统调用仍 +对应一个独立 IP 包,不使用 `readv/writev` 拼接多个包。Windows 在同一队列临界区内 +批量交接,保留非阻塞丢当前包与空到非空唤醒语义。 + +Unix 读缓冲区和公共批次容器复用;非法包只丢该槽位,不丢合法邻包。批次中途发生错误或 +取消时保留已经完成的逐包结果,流量只统计有效的已完成前缀,未完成包不得重放已完成 +前缀。批次预算和协作调度保证停止与其他任务可推进,不改变 fd 标志、线程策略 +或宿主 API;不新增 Apple packetFlow 接入或私有系统调用。 + +公共 reader 在进入 netstack 前同步分流 UDP,直接非阻塞提交到按源地址拥有的 +关联队列;DNS 劫持提交受跟踪的查询任务。普通 UDP 不进入 TCP Driver,也没有 +共享 UDP 入站中转队列。TCP/ICMP 使用独立有界 ingress;满时丢当前完整 IP 包并 +计数,TCP 可重传,不等待容量而阻塞其他来源的 UDP/DNS。 + +唯一 writer 公平轮转 TCP/ICMP raw、普通 UDP 响应和 DNS 响应三个独立通道。 +UDP 直接在复用的 MTU frame 中构包,不再经过 response writer 和 raw output +的二次交接;每批最多处理 8 个已就绪项目,非法响应也消耗工作预算。DNS permit +保留到对应包被平台接受、最终丢弃或取消,ENOBUFS 待重试时不释放;Windows +Adapter 成功处理仍不承诺 Windows 框架最终交付。 + +平台无关的 netstack 驱动同样最多连续消费 8 个已就绪入站包,再执行全量 TCP +socket / 应用缓冲维护及 egress polling。TCP 协议 ingress 仍逐包推进,保证相邻 +SYN 的连接归属,不让未推进的 TCP 包误抑制相邻 ICMP;不把 ingress 延迟到凑满批次。 +TCP egress 可延后到批次后的维护,不承诺 TCP 与即时 ICMP 响应之间的输出顺序不变。 +每次取下一包前检查取消与输出容量,输出满时后缀留在原 raw 入站队列; +不预取额外后缀、不新建积压队列,输入关闭时已消费前缀仍经过维护后退出。 +合作调度预算按实际消费包数记账,避免 `try_recv()` 绕过逐包扣账;最多先处理 +当前 8 包,再按已消费数量扣账和退让。 + +Device 仅保留一个尚未消费的 ingress 包,不再保留 RX/TX 双 deque。smoltcp +TxToken 在消费 RX 前预留现有 raw egress 的槽位,生成包后直接提交;未使用的 +token 归还容量。满输出保留尚未处理的 TCP 包和协议状态,低优先级 ICMP 仍可 +丢当前请求。writer 消费包会显式唤醒 Driver;满输出不以零延迟 timer 忙重试, +输出端关闭则停止 Driver、释放 socket 并唤醒全部等待者。 + ## Windows VPN ```text @@ -62,14 +129,28 @@ Windows 使用 `Windows.Networking.Vpn` 回调,不使用文件描述符或适 原始 TUN 包 1,500 字节 最终代理 UDP 负载 1,452 字节(Windows 1,352) 包队列 256 -普通事件 / UDP 响应 128 -DNS 入站 / DNS 响应 128 / 128 +普通事件 / TCP accept 128 +每关联 UDP 入站 64 +普通 UDP 响应 4,096 +DNS 响应 128 ``` TCP 会话、普通 UDP 关联、半开连接和出站握手不设固定业务数量上限。结构安全由有界队列、每流缓冲区、解析大小、超时、空闲清理和缓存提供。 +每关联入站、普通响应和 DNS 响应是独立内部队列预算,不扩大 TCP accept。 +满时只丢当前数据报;容量不构成公开配置或活动业务数量上限。TUN 使用 TCP-only +netstack 端点及纯 UDP codec,独立 crate 的通用 UDP endpoint 不在生产 TUN 路径上。 +写回受阻时 reader 仍可处理新请求。停止会取消并等待唯一 writer、reader 所有的 +关联/DNS 任务和 netstack,不依赖输出队列腾出容量。 + +规则/建链在 TCP 握手完成前拒绝连接时,netstack 保留已 abort 的 socket,直到 smoltcp +将 RST 交给现有有界发送队列,再回收逐流状态;不因直接移除 socket 而吞掉关闭报文。 +发送队列暂满仍等待原有驱动推进,Stop 可同步释放全部状态,不另建重试任务。 + ## 物理出口 +- Linux:宿主负责网络命名空间或等效路由隔离,确保被捕获的客户端流量进入 TUN, + VCore 出站绕过 TUN;不能把仅绑定 source IP 当作绕路保证。没有自动路由配置或降级路径。 - Android:每个出站 TCP/UDP socket 在 connect 前调用宿主 protect;失败则当前连接失败关闭。 - Windows:Provider 为当前会话选择不可变的物理网络绑定;每个地址族只从非 link-local 地址中选择一个源 IP 和接口索引交给 Session Host,同时独立保留物理适配器全部去重的 on-link prefixes(包括 link-local)用于 VPN 路由。普通出站 socket 必须同时绑定源地址和 WinSock 接口索引。 - Windows 只有配置中显式使用 `127.0.0.0/8` 范围内的 IPv4 字面量或 `::1` 的本地出站可以跳过物理绑定;物理代理服务器的域名解析到任何回环地址都会失败关闭。 diff --git a/readme/README.ru.md b/readme/README.ru.md index a97e7bc..2144670 100644 --- a/readme/README.ru.md +++ b/readme/README.ru.md @@ -4,157 +4,126 @@ English · 简体中文 · Русский

-VCore — независимое клиентское прокси-ядро на Rust, не привязанное к конкретному хост-приложению. Через строгую YAML-конфигурацию и Invoke API v5 оно предоставляет граф прокси, статические группы `select`, DNS, правила маршрутизации, GeoData, HTTP/SOCKS5 listeners, плоскость данных TUN и loopback Controller. Внутренняя ревизия схемы конфигурации — 28; она присутствует только в ответе `version` и `buildIdentity`, но не записывается в YAML. +VCore — встраиваемое прокси-ядро на Rust для VPN-клиентов и локальных прокси. Оно маршрутизирует TCP/UDP-трафик через прямые соединения, прокси-узлы, группы и цепочки, объединяя DNS, GeoData и кроссплатформенную плоскость данных TUN. -## Возможности +Конфигурация использует **совместимый с Mihomo YAML в пределах поддерживаемых возможностей**. VCore ориентирован на клиентские функции и не реализует все поля Mihomo или его полный Dashboard API. -SS2022 также поддерживает [strict ShadowTLS v3](../docs/outbounds.md#shadowtls-v3) для TCP; нативный UDP использует отдельный путь. +## Что умеет VCore -- Исходящие подключения: [VLESS TCP/WS/gRPC/HTTP/H2/XHTTP, TLS/REALITY и Vision](../docs/vless.md), SOCKS5 CONNECT/UDP ASSOCIATE, AnyTLS TCP/UoT, Shadowsocks 2022, [Trojan TCP/UDP через TLS/WS/gRPC](../docs/outbounds.md#trojan), [VMess AEAD через TCP/WS/gRPC/HTTP/H2](../docs/outbounds.md#vmess-aead), [Hysteria2 TCP/UDP, управление скоростью, Salamander и смена портов](../docs/outbounds.md#hysteria2) и DIRECT. -- Цепочки прокси: `dialer-proxy` образует ориентированный ациклический граф произвольной длины. Если узел A указывает на B, физический путь имеет вид `client -> B -> A -> target`. -- Группы прокси: статические группы `select` сохраняют порядок участников; участниками могут быть конкретные узлы, вложенные группы, `DIRECT` и `REJECT`. Выбор текущей session можно менять через Controller. `dialer-proxy` принимает узел или группу; DIRECT в группе верхнего уровня подключается к заранее разрешённому серверу текущего узла. -- Маршрутизация: последовательно применяются `DOMAIN`, `DOMAIN-SUFFIX`, `DOMAIN-KEYWORD`, `GEOSITE`, `GEOIP`, `IP-CIDR`, `IP-CIDR6`, `DST-PORT`, `NETWORK` и завершающее правило `MATCH`. -- DNS: UDP/TCP nameserver с фиксированным IP, явный outbound, последовательные policy/failover, typed/opaque cache, singleflight и перехват UDP/TCP-порта 53 в TUN. -- TUN: raw IPv4/IPv6, TCP/UDP, локальные ответы ICMPv4/ICMPv6 Echo, HTTP/TLS/QUIC sniffer и четыре счётчика трафика на session. -- Listeners: HTTP CONNECT/forward с аутентификацией и маршрутизацией каждого запроса, потоковыми телами, Keep-Alive и Upgrade; SOCKS5 CONNECT и UDP ASSOCIATE с авторизацией через TCP. По умолчанию доступ локальный без аутентификации; для LAN требуется общая пара имени пользователя и пароля. -- GeoData: VCore управляет `geosite.dat` и `geoip.dat` в `dataDir/geodata`, загружает их по запросу и может обновлять через цепочку прокси. -- Измерение задержки: `measureDelay` принимает за вызов 1–5 конфигураций node-only, использует до пяти частных worker и сохраняет порядок входных данных в результатах. -- TLS: независимый pin сертификата и четыре необязательных [шаблона ClientHello](../docs/tls-client-fingerprint.md); [VLESS JLS](../docs/vless.md#jls) сохраняет полную аутентификацию TLS. [Статический ECH](../docs/vless.md#静态-ech) использует явную конфигурацию основного и download-соединения VLESS, без динамических DNS-запросов или отката при ошибке. +- **Принимать трафик приложений и VPN:** пересылка HTTP, CONNECT и Upgrade; SOCKS5 CONNECT и UDP ASSOCIATE; IPv4/IPv6-пакеты TUN, предоставленные хостом. +- **Маршрутизировать по назначению:** правила по домену, суффиксу или ключевому слову домена, IP CIDR, порту назначения, TCP/UDP, GeoSite и GeoIP, с явными действиями DIRECT и REJECT. +- **Выбирать прокси и строить цепочки:** вложенные группы `select`, переключение групп во время работы и цепочки `dialer-proxy`, ссылающиеся на узлы или группы. Изменения выбора применяются к новым физическим транспортным соединениям, не перенося существующие. +- **Обрабатывать DNS:** управляемые UDP/TCP-серверы, политики выбора серверов по GeoSite, последовательное переключение при отказе, кеширование и объединение одинаковых запросов; перехват TCP/UDP-порта 53 в режиме TUN. +- **Определять трафик для маршрутизации:** извлечение домена из HTTP, TLS и QUIC, а также DNS-подсказки TUN, без изменения фактического назначения. На ICMPv4/ICMPv6 Echo ядро отвечает локально. +- **Управлять данными маршрутизации:** загрузка используемых категорий из `geosite.dat` / `geoip.dat` по запросу и обновление файлов через заданный маршрут. GeoSite поддерживает Domain, Full, Plain и Regex, пересечение атрибутов и инверсию; GeoIP поддерживает инверсию. На всех платформах выбранные записи сохраняются без жёсткого лимита количества или усечения; общая квота памяти GeoData не задана. См. [границы GeoData](../docs/geodata.md#内存与安全边界). +- **Предоставлять средства управления клиенту:** Controller на loopback-интерфейсе для выбора групп, текущей скорости и общего объёма TUN-трафика, а также изолированное измерение задержки узлов и цепочек через Invoke API. -## Конфигурация +## Прокси-протоколы -[`docs/config.yaml`](../docs/config.yaml) — единственный полный пример. Основные ограничения: +Все восемь протоколов ниже включены в сборку по умолчанию. Поддержка UDP настраивается для каждого узла. -- Размер YAML ограничен 256 KiB; неизвестные поля, anchors, aliases, пользовательские tags и устаревшие структуры отклоняются. -- На верхнем уровне должен быть хотя бы один proxy и включённый `port`, `socks-port` или `tun`. -- Имена proxy и proxy group используют общее пространство имён с точным совпадением и учётом регистра. Имя занимает 1–64 байта UTF-8; запрещены окружающие Unicode-пробелы, управляющие символы, `, # / ? & = % \`, `.` и `..`, а `DIRECT`, `REJECT` и `RULES` зарезервированы. Внутренние обычные пробелы, CJK и emoji разрешены. -- `proxy-groups` принимает только `select`. Порядок и повторы участников сохраняются; без `default-selected` выбирается первый участник, а явное значение должно быть прямым участником. Все ссылки `dialer-proxy` и участники групп, включая невыбранные, должны образовывать единый ациклический граф. -- `rules` обязателен и должен завершаться ровно одним `MATCH`, указывающим на настроенный proxy node или proxy group. -- `DIRECT` и `REJECT` — встроенные actions и участники групп; любой другой route target должен быть настроенным proxy node или proxy group. `RULES` зарезервирован только для DNS. -- Конфигурация передаётся inline через `configYaml` / `configYamls`; VCore не читает пути конфигурации хоста. -- Runtime-значения, включая Controller, TUN fd, порт и secret Controller, создаются хостом и не сохраняются в пользовательском RAW YAML. +| Протокол | Возможности | +| --- | --- | +| [VLESS](../docs/vless.md) | TCP, WebSocket / HTTPUpgrade, gRPC, HTTP-маскировка первого пакета, legacy H2, [XHTTP H1/H2/H3](../docs/xhttp.md); Vision, Encryption, REALITY, JLS, статический ECH и sing-mux в поддерживаемых сочетаниях | +| [VMess AEAD](../docs/outbounds.md#vmess-aead) | TCP, WebSocket / HTTPUpgrade, gRPC, HTTP-маскировка первого пакета и legacy H2; TCP/UDP, транспорт без TLS или со стандартным TLS | +| [Trojan](../docs/outbounds.md#trojan) | TCP/UDP через TLS TCP, WebSocket / HTTPUpgrade или gRPC | +| [Shadowsocks 2022](../docs/outbounds.md#shadowsocks-2022) | TCP/UDP; AES-128-GCM, AES-256-GCM и ChaCha20-Poly1305; цепочки идентификации AES; опциональный strict ShadowTLS v3 для TCP и UoT v2 | +| [AnyTLS](../docs/outbounds.md#anytls) | TLS-сессии и UDP over TCP v2 | +| [SOCKS5](../docs/outbounds.md#socks5) | CONNECT и UDP ASSOCIATE, с опциональной аутентификацией по имени пользователя и паролю | +| [Hysteria2](../docs/outbounds.md#hysteria2) | TCP/UDP через QUIC, управление пропускной способностью, Salamander, смена портов и mTLS | +| [TUIC v5](../docs/outbounds.md#tuic-v5) | TCP/UDP через QUIC, режимы передачи UDP native/quic и выбор алгоритма управления перегрузкой | -## Жизненный цикл и ABI +Стандартные TLS-соединения поддерживают проверку сертификатов и закрепление сертификата по SHA-256. Поддерживаемые TCP-пути с TLS могут использовать шаблоны ClientHello Chrome, Firefox или Safari; `client-fingerprint` не зависит от `fingerprint` сертификата. Точные имена и сочетания описаны в [профилях TLS и политике сертификатов](../docs/tls-client-fingerprint.md). Эти функции не гарантируют неотличимость от браузера или произвольные сочетания протоколов. -Кроссплатформенные точки входа: +SS2022 использует официальную Rust-библиотеку без изменений; известные риски пустой первой записи, сценария server-first и padding описаны в [контракте Shadowsocks](../docs/outbounds.md#shadowsocks-2022). + +## Конфигурация в стиле Mihomo + +Используйте привычные структуры `proxies`, `proxy-groups`, `rules`, `dns`, `port`, `socks-port` и `tun`. Например: + +```yaml +socks-port: 1080 +allow-lan: false + +proxies: + - name: edge + type: anytls + server: proxy.example.com + port: 443 + password: replace-with-your-password + client-fingerprint: chrome + udp: true + +proxy-groups: + - name: Proxy + type: select + proxies: [edge, DIRECT] + +dns: + enable: true + nameserver: + - "udp://223.5.5.5:53#DIRECT" + +rules: + - GEOSITE,cn,DIRECT + - GEOIP,cn,DIRECT,no-resolve + - MATCH,Proxy +``` + +Замените адрес сервера и учётные данные в примере. Для правил GeoSite/GeoIP нужны соответствующие файлы в `/geodata`; без них эти типы правил недоступны. См. [полный справочник конфигурации](../docs/config.yaml) и [поведение GeoData](../docs/geodata.md). + +Совместимость ограничена документированными полями и поведением и не распространяется на произвольные конфигурации Mihomo. Группы пока поддерживают статический `select`; DNS-серверы задаются буквальными IP-адресами и используют UDP/TCP. Providers, автоматический выбор в группах, зашифрованный DNS и fake-IP не входят в текущий набор возможностей. Неизвестные поля и недопустимые сочетания отклоняются, а не игнорируются; особенности семантики VCore отмечены в соответствующих контрактах. + +Хост передаёт YAML inline через `configYaml`; дескрипторы TUN и платформенные callbacks предоставляются отдельно через runtime API. VCore не читает пути конфигурации хоста и не настраивает системные маршруты Linux автоматически. + +## Платформы и интеграция + +| Платформа | Интеграция TUN | +| --- | --- | +| iOS / macOS | Дескриптор файла utun, предоставленный хостом | +| tvOS 17+ | Дескриптор файла utun, предоставленный хостом; ARM64-устройства и симулятор | +| Android | Дескриптор `VpnService` с защитой исходящих сокетов | +| Linux | Настоящий raw-IP TUN с одной очередью; создание интерфейса и изоляцию маршрутизации обеспечивает хост | +| Windows | Нативный Provider на `Windows.Networking.Vpn` и полностью доверенный Session Host, без Wintun или эмуляции fd | + +VCore — библиотека, а не самостоятельное VPN-приложение. На Unix исходный дескриптор TUN принадлежит хосту; VCore использует и закрывает собственную копию. Публичный API packetFlow от Apple не гарантирует доступ к raw fd, поэтому интеграция с Network Extension и проверка на устройствах остаются ответственностью хоста. См. [интеграцию TUN](../docs/tun-platform.md) и [границы приёмки платформ](../docs/acceptance.md). + +Кроссплатформенный C ABI принимает JSON-запросы через Invoke API v5: ```c char *VCoreInvoke(const char *request_json); void VCoreFree(char *response); ``` -Пакеты Windows также используют host bridge ревизии 3 для глобальной VPN policy, profile, Session Snapshot и необязательного session backend: +Один публичный экземпляр проходит жизненный цикл `initialize → createInstance → prepare(configYaml) → start → stop → destroyInstance`. API также предоставляет проверку конфигурации, запросы состояния, статус GeoData и измерение задержки. См. [Invoke API](../docs/invoke-api.md), [Controller API](../docs/controller-api.md) и [пример интеграции Windows](../example/windows-uwp/README.md). -```c -char *VCoreWindowsVpnInvoke(const char *request_json); -``` +## Benchmark -Публичный runtime содержит один экземпляр: +[**TUN-бенчмарк VCore / Mihomo**](https://github.com/OneXray/container-benchmark) содержит воспроизводимую методику, результаты измерений и сравнительные графики для обоих ядер в одинаковой среде с нативным Linux TUN. -```text -initialize - -> createInstance - -> prepare(configYaml) - -> start - -> stop - -> destroyInstance -``` +Проект benchmark также выполняет проверку совместимости протоколов (`interop`) и тесты нагрузки на память (`stress`), используя явно указанный checkout `--source vcore=PATH`. Собственные скрипты VCore только компилируют ядро и платформенные артефакты. -`instanceId` — generation token, который не используется повторно текущим runtime. Команды одного экземпляра завершаются fail-fast, если другая команда уже выполняется; чистые вызовы `validateConfig` могут выполняться параллельно. Полный envelope, методы, владение fd и контракт Android protect описаны в [`docs/invoke-api.md`](../docs/invoke-api.md). +Он измеряет смешанный TCP/UDP-трафик на **1 / 1,5 / 2 Гбит/с** с **1 000 DNS-запросов/с** и расширенными правилами `geosite:cn` / `geoip:cn`, показывая фактическую пропускную способность, CPU, наблюдаемый пиковый Linux RSS, потери UDP-пакетов и успешные DNS-запросы. Проверяется путь TUN/DNS/маршрутизации с выходом через DIRECT, а не пропускная способность зашифрованных прокси; Linux RSS не отражает потребление памяти Apple Network Extension. -Состояние runtime доступно через session-local loopback Controller: - -```http -GET /traffic -GET /group -GET /group/{name} -GET /proxies/{name} -PUT /proxies/{name} -Authorization: Bearer -``` - -`GET /traffic` возвращает одноразовый TUN snapshot `up/down/upTotal/downTotal`. Конечные точки групп читают и изменяют выбранного прямого участника статической группы `select`; успешное изменение влияет только на новые физические TCP-, UDP- и DNS-transports текущей session. Оно не переносит существующие соединения, UDP associations, состояние DNS или pooled TCP transports и не выполняет автоматический failover. Controller, управляющий группами, требует один Bearer secret для всех маршрутов и может работать без TUN. Подробности — в [`docs/controller-api.md`](../docs/controller-api.md). - -Аутентифицированная сессия Hysteria2 сохраняет выбранный upstream при смене портов. Замена socket в той же QUIC-сессии не считывает выбор группы заново; новый выбор применяется только к новой аутентифицированной сессии. - -## Платформы - -| Платформа | Плоскость данных | Статус | -| --- | --- | --- | -| iOS / macOS | Хост предоставляет utun fd; VCore дублирует его и использует синхронный device `rust-tun` с Tokio `AsyncFd` | Реализовано; footprint на реальном iOS-устройстве для Release остаётся release gate | -| Android | `VpnService` предоставляет raw-IP fd; каждый outbound socket сначала должен пройти protect callback | Реализовано; матрица реальных устройств остаётся release gate | -| Windows | AppContainer Provider на `Windows.Networking.Vpn` и отдельный full-trust runtime для каждой session; raw IP передаётся через named pipes внутри пакета | Пакет для Windows 11 ARM64 с тестовой подписью прошёл функциональные, lifecycle, pressure и bounded-batching проверки | -| Linux | — | Не поддерживается; запуск завершается fail closed | - -Windows не использует слой эмуляции fd. Provider владеет только `VpnChannel`, buffers, routes, мониторингом физической сети, packet gateway и fail-closed Stop. Полный runtime VCore, Controller, DNS, rules и outbounds находятся в Session Host. Packet channel сохраняет framing protocol v1 и объединяет не более восьми уже готовых frames, не ожидая будущие packets. - -## Ограничения ресурсов - -Текущий TUN profile сохраняет локальные структурные пределы вместо фиксированного admission limit на общее число прикладных flows: - -```text -raw packet / MTU 1,500 bytes -packet queue 256 -ordinary event / UDP response 128 -DNS ingress / DNS response 128 / 128 -TCP buffer 32 KiB per direction -rustls / XHTTP buffer 64 KiB -DNS typed cache 256 entries -DNS opaque cache 64 entries / 256 KiB -GeoData allocation capacity 8 MiB -``` - -Windows объявляет L3 MTU 1 400 байт согласно требованиям `StartWithMainTransport`; 1 500 байт остаётся межплатформенным пределом парсера. - -TCP sessions, обычные UDP associations, half-open connections, outbound handshakes и активные DNS transports создаются по запросу. Структурную безопасность обеспечивают bounded queues, buffers на flow, ограничения wire/parser, timeouts, idle cleanup и caches. Цели iOS 35/45 MiB являются best-effort наблюдениями и не меняют результаты жизненного цикла. +Отдельная команда `stress` по умолчанию загружает полные расширенные GeoData, проверяет атрибуты и инверсию и создаёт нагрузку 2 Гбит/с, 60 секунд и 1 000 DNS-запросов/с. Типы входных записей, измерения и ошибки приведены в README benchmark. Необязательный параметр количества меняет только экспериментальные данные, а не поведение ядра. Отдельное наблюдение не гарантирует память ниже 50 000 000 байт для произвольного входа и не заменяет проверку на устройствах Apple. ## Документация -- [Documentation index](../docs/README.md) -- [Configuration](../docs/config.yaml) -- [Invoke API](../docs/invoke-api.md) -- [Build and test](../scripts/README.md) -- [Acceptance boundaries](../docs/acceptance.md) +- [Указатель документации](../docs/README.md) +- [Справочник конфигурации](../docs/config.yaml) +- [Сборка ядра и платформенных артефактов](../scripts/README.md) +- [Регрессионные тесты ядра](../tests/README.md) +- [Политика ресурсов](../docs/runtime-resource-policy.md) +- [Приёмка и известные ограничения](../docs/acceptance.md) -## Пример +## Благодарности -- [Минимальная интеграция Windows UWP VPN](../example/windows-uwp/README.md): Provider и Session Host в одном пакете, full-trust foreground host, MSIX manifest и запускаемый command-line demo. +VCore использует публичные зависимости и опирается на реализации протоколов и примеры платформенной интеграции: -## Проверка - -```bash -cargo fmt --all -- --check -uv run --project scripts --locked vcore-scripts check core --profile debug -cargo clippy --locked --all-features --lib --bins -- -D warnings -cargo test --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -cargo clippy --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -- -D warnings -uv run --project scripts --locked vcore-scripts check c-header -uv run --project scripts --locked vcore-scripts check tls-dependencies -uv run --project scripts --locked python -m unittest discover -s scripts/tests -uv run --project scripts --locked ruff check scripts -uv run --project scripts --locked ruff format --check scripts -``` - -Артефакты платформ (полные команды и переменные окружения приведены в [`scripts/README.md`](../scripts/README.md)): - -```bash -uv run --project scripts --locked vcore-scripts build apple -uv run --project scripts --locked vcore-scripts build android -uv run --project scripts --locked vcore-scripts build windows -``` - -Текущая подтверждённая область проверки и отложенные release gates для физических устройств и Windows перечислены в [`docs/acceptance.md`](../docs/acceptance.md). - -## Credits - -К зависимостям VCore, сопровождаемым forks, справочным материалам по открытым API/протоколам, архитектурным ориентирам и объектам interoperability относятся: - -- [smoltcp](https://github.com/smoltcp-rs/smoltcp), [clash-rs](https://github.com/Watfaq/clash-rs) и [netstack-smoltcp](https://github.com/automesh-network/netstack-smoltcp): userspace IP stacks и TUN netstacks. -- [windows-rs](https://github.com/microsoft/windows-rs), [UWP VPN Plugin Sample](https://github.com/microsoft/UwpVpnPluginSample), [wireguard-uwp-rs](https://github.com/luqmana/wireguard-uwp-rs), [Maple](https://github.com/YtFlow/Maple) и [YtFlowCore](https://github.com/YtFlow/YtFlowCore): Windows VPN, активация WinRT и packet flow. -- [Xray-core](https://github.com/XTLS/Xray-core), [Mihomo](https://github.com/MetaCubeX/mihomo) и [Leaf](https://github.com/eycorsican/leaf): прокси-протоколы, маршрутизация, архитектура TUN и interoperability references. -- [rustls](https://github.com/rustls/rustls): TLS без fingerprint, QUIC и общая проверка сертификатов WebPKI. -- [boring](https://github.com/cloudflare/boring) / [BoringSSL](https://boringssl.googlesource.com/boringssl/): именованные профили TLS ClientHello и расширение classic REALITY в собственном fork VCore. +- Сеть и маршрутизация: [smoltcp](https://github.com/smoltcp-rs/smoltcp), [clash-rs](https://github.com/Watfaq/clash-rs), [netstack-smoltcp](https://github.com/automesh-network/netstack-smoltcp), [Mihomo](https://github.com/MetaCubeX/mihomo), [Xray-core](https://github.com/XTLS/Xray-core) и [Leaf](https://github.com/eycorsican/leaf). +- TLS и Shadowsocks: [rustls](https://github.com/rustls/rustls), [boring](https://github.com/cloudflare/boring), [BoringSSL](https://boringssl.googlesource.com/boringssl/) и [shadowsocks-rust](https://github.com/shadowsocks/shadowsocks-rust). Заимствованный код replay-window сохраняет [уведомления MIT](../src/outbound/shadowsocks/packet_window.rs). +- Интеграция Windows: [windows-rs](https://github.com/microsoft/windows-rs), [UWP VPN Plugin Sample](https://github.com/microsoft/UwpVpnPluginSample), [wireguard-uwp-rs](https://github.com/luqmana/wireguard-uwp-rs), [Maple](https://github.com/YtFlow/Maple) и [YtFlowCore](https://github.com/YtFlow/YtFlowCore). ## Лицензия -VCore распространяется по [лицензии MIT](../LICENSE). +[MIT](../LICENSE). diff --git a/readme/README.zh_CN.md b/readme/README.zh_CN.md index befa405..1f0119e 100644 --- a/readme/README.zh_CN.md +++ b/readme/README.zh_CN.md @@ -4,142 +4,126 @@ English · 简体中文 · Русский

-VCore 是独立且不绑定特定宿主应用的 Rust 客户端代理 core。它通过严格 YAML 配置和 Invoke API v5 提供代理图、静态 `select` 代理组、DNS、规则、GeoData、HTTP/SOCKS5 listener、TUN 数据面与回环 Controller。内部配置 schema revision 为 28;revision 只出现在 `version` 响应和 `buildIdentity` 中,不写入 YAML。 +VCore 是可嵌入 VPN 客户端和本地代理的 Rust 代理内核。它通过直连、代理节点、代理组和代理链转发 TCP/UDP 流量,并集成 DNS、GeoData 和跨平台 TUN 数据面。 -## 能力 +配置采用**与 Mihomo 兼容的 YAML,兼容范围限于 VCore 已支持的功能**。VCore 聚焦客户端能力,并未实现 Mihomo 的全部字段或完整 Dashboard API。 -SS2022 另支持 [strict ShadowTLS v3](../docs/outbounds.md#shadowtls-v3) TCP 包装;原生 UDP 仍走独立路径。 +## 内核能做什么 -- Outbound:[VLESS TCP/WS/gRPC/HTTP/H2/XHTTP、TLS/REALITY 与 Vision](../docs/vless.md)、SOCKS5 CONNECT/UDP ASSOCIATE、AnyTLS TCP/UoT、Shadowsocks 2022、[Trojan TCP/UDP(TLS/WS/gRPC)](../docs/outbounds.md#trojan)、[VMess AEAD(TCP/WS/gRPC/HTTP/H2)](../docs/outbounds.md#vmess-aead)、[Hysteria2 TCP/UDP、带宽、Salamander 与端口跳跃](../docs/outbounds.md#hysteria2)、DIRECT。 -- 代理链:`dialer-proxy` 组成任意长度的有向无环图;节点 A 指向 B 时,物理路径为 `client -> B -> A -> target`。 -- 代理组:静态 `select` 组保留有序成员,可包含具体节点、嵌套组、`DIRECT` 与 `REJECT`;当前 session 的选择可通过 Controller 实时修改。`dialer-proxy` 可引用节点或组;上游组的 DIRECT 连接当前节点预解析的服务器。 -- 路由:顺序执行 `DOMAIN`、`DOMAIN-SUFFIX`、`DOMAIN-KEYWORD`、`GEOSITE`、`GEOIP`、`IP-CIDR`、`IP-CIDR6`、`DST-PORT`、`NETWORK` 和最终 `MATCH`。 -- DNS:固定 IP 的 UDP/TCP nameserver、显式出口、顺序 policy/failover、typed/opaque cache、singleflight、TUN UDP/TCP 53 劫持。 -- TUN:raw IPv4/IPv6、TCP/UDP、ICMPv4/ICMPv6 Echo 本地响应、HTTP/TLS/QUIC sniffer、每 session 四字段流量统计。 -- Listener:HTTP CONNECT/forward,逐请求认证与选路,支持流式正文、Keep-Alive 和 Upgrade;SOCKS5 CONNECT 与 TCP 授权的 UDP ASSOCIATE。默认本机免认证,局域网共享强制使用一组共用账号密码。 -- GeoData:VCore 管理 `dataDir/geodata` 下的 `geosite.dat` 和 `geoip.dat`,按需求加载并可通过代理链后台更新。 -- 测速:`measureDelay` 单次接收 1–5 份 node-only 配置,使用最多五个私有 worker,结果保持输入顺序。 -- TLS:独立证书 pin 与四种可选 [ClientHello 模板](../docs/tls-client-fingerprint.md);[VLESS JLS](../docs/vless.md#jls)保留完整原生 TLS 认证。[静态 ECH](../docs/vless.md#静态-ech)接受显式的 VLESS 主/下载腿配置,不执行动态 DNS 查询或失败回落。 +- **接入应用与 VPN 流量:**HTTP 转发、CONNECT 和 Upgrade;SOCKS5 CONNECT 和 UDP ASSOCIATE;宿主提供的 IPv4/IPv6 TUN 数据包。 +- **按目标路由:**支持域名、域名后缀/关键字、IP CIDR、目标端口、TCP/UDP、GeoSite 和 GeoIP 规则,以及明确的 DIRECT 和 REJECT 动作。 +- **选择和串联代理:**支持嵌套 `select` 组、实时切换组选择,以及引用节点或组的 `dialer-proxy` 代理链。选择变化只影响新建的物理传输连接,不迁移既有连接。 +- **处理 DNS:**支持指定出口的 UDP/TCP 上游、基于 GeoSite 的上游策略、顺序故障转移、缓存与重复查询合并;TUN 模式下拦截 TCP/UDP 53 端口。 +- **识别流量用于路由:**通过 HTTP、TLS、QUIC 域名嗅探与 TUN DNS 提示辅助路由,不改写实际目标地址。ICMPv4/ICMPv6 Echo 在本地应答。 +- **管理路由数据:**按需从 `geosite.dat` / `geoip.dat` 加载被引用的类别,并通过配置的路由更新文件。GeoSite 支持 Domain、Full、Plain 和 Regex、属性交集及反选;GeoIP 支持反选。所有平台完整保留所选记录,不设条数硬上限或截断,也不设 GeoData 总内存预算。详见 [GeoData 边界](../docs/geodata.md#内存与安全边界)。 +- **提供客户端控制:**回环 Controller 支持代理组选择及 TUN 流量速率/累计量查询;Invoke API 提供隔离的节点/代理链延迟测量。 -## 配置 +## 代理协议 -[`docs/config.yaml`](../docs/config.yaml) 是唯一完整示例。核心约束: +默认构建启用下列全部八种协议。UDP 支持按节点配置。 -- YAML 最大 256 KiB,拒绝未知字段、anchor、alias、自定义 tag 和历史结构。 -- 顶层至少包含一个 proxy,并启用 `port`、`socks-port` 或 `tun`。 -- proxy 与 proxy group 定义名共享一个精确且大小写敏感的命名空间。名称为 1–64 UTF-8 字节,拒绝首尾 Unicode 空白、控制字符、`, # / ? & = % \`、`.`、`..`,并保留 `DIRECT`、`REJECT` 与 `RULES`;内部普通空格、CJK 和 emoji 可用。 -- `proxy-groups` 只接受 `select`。成员顺序与重复项均保留;省略 `default-selected` 时选择第一项,显式值必须是直接成员。节点上游与全部组成员(包括未选成员)必须组成同一张无环图。 -- `rules` 必填,必须恰好以一个指向已配置 proxy node 或 proxy group 的 `MATCH` 结束。 -- `DIRECT` 和 `REJECT` 是内置 action 与组成员;其他 route target 必须是已配置 proxy node 或 proxy group。`RULES` 只由 DNS 保留。 -- 配置通过 `configYaml` / `configYamls` 内联交付;VCore 不读取宿主配置路径。 -- Controller、TUN fd、Controller 端口/secret 等运行时值由宿主生成,不进入用户保存的 RAW YAML。 +| 协议 | 能力 | +| --- | --- | +| [VLESS](../docs/vless.md) | TCP、WebSocket / HTTPUpgrade、gRPC、HTTP 首包伪装、传统 H2、[XHTTP H1/H2/H3](../docs/xhttp.md);在支持的组合中提供 Vision、Encryption、REALITY、JLS、静态 ECH 和 sing-mux | +| [VMess AEAD](../docs/outbounds.md#vmess-aead) | TCP、WebSocket / HTTPUpgrade、gRPC、HTTP 首包伪装和传统 H2;TCP/UDP,明文或标准 TLS | +| [Trojan](../docs/outbounds.md#trojan) | 通过 TLS TCP、WebSocket / HTTPUpgrade 或 gRPC 传输 TCP/UDP | +| [Shadowsocks 2022](../docs/outbounds.md#shadowsocks-2022) | TCP/UDP;AES-128-GCM、AES-256-GCM 和 ChaCha20-Poly1305;AES 身份链;可选 strict ShadowTLS v3 TCP 包装和 UoT v2 | +| [AnyTLS](../docs/outbounds.md#anytls) | TLS 会话与 UDP over TCP v2 | +| [SOCKS5](../docs/outbounds.md#socks5) | CONNECT 和 UDP ASSOCIATE,可选用户名/密码认证 | +| [Hysteria2](../docs/outbounds.md#hysteria2) | QUIC TCP/UDP、带宽控制、Salamander、端口跳跃和 mTLS | +| [TUIC v5](../docs/outbounds.md#tuic-v5) | QUIC TCP/UDP、native/quic UDP 转发模式,以及可选的拥塞控制算法 | -## 生命周期与 ABI +标准 TLS 连接支持证书验证与 SHA-256 证书固定。适用的 TCP TLS 路径可使用 Chrome、Firefox 或 Safari ClientHello 模板;`client-fingerprint` 与证书 `fingerprint` 相互独立。准确的名称和支持组合见 [TLS 配置与证书策略](../docs/tls-client-fingerprint.md)。这些功能不保证与浏览器不可区分,也不支持任意协议组合。 -跨平台业务入口: +SS2022 使用未经修改的官方 Rust 库;已知的空首写入、服务端先发送和 padding 风险见 [Shadowsocks 契约](../docs/outbounds.md#shadowsocks-2022)。 + +## Mihomo 风格配置 + +使用熟悉的 `proxies`、`proxy-groups`、`rules`、`dns`、`port`、`socks-port` 和 `tun` 结构。例如: + +```yaml +socks-port: 1080 +allow-lan: false + +proxies: + - name: edge + type: anytls + server: proxy.example.com + port: 443 + password: replace-with-your-password + client-fingerprint: chrome + udp: true + +proxy-groups: + - name: Proxy + type: select + proxies: [edge, DIRECT] + +dns: + enable: true + nameserver: + - "udp://223.5.5.5:53#DIRECT" + +rules: + - GEOSITE,cn,DIRECT + - GEOIP,cn,DIRECT,no-resolve + - MATCH,Proxy +``` + +请替换示例中的服务器地址与凭据。GeoSite/GeoIP 规则需要 `/geodata` 下的对应数据文件;缺失文件时,相应规则类型不可用。详见[完整配置参考](../docs/config.yaml)与 [GeoData 行为](../docs/geodata.md)。 + +兼容范围限于文档列出的字段和行为,不覆盖任意 Mihomo 配置。代理组目前支持静态 `select`;DNS 上游使用固定 IP,通过 UDP/TCP 查询。Providers、自动代理组选择、加密 DNS 和 fake-IP 不在当前功能范围内。未知字段与无效组合会被拒绝,不会静默忽略;VCore 特有的语义会在相应契约中说明。 + +宿主通过 `configYaml` 内联传入 YAML;TUN 描述符和平台回调通过运行时 API 单独提供。VCore 不读取宿主配置路径,也不自动配置 Linux 系统路由。 + +## 平台与集成 + +| 平台 | TUN 集成 | +| --- | --- | +| iOS / macOS | 宿主提供 utun 文件描述符 | +| tvOS 17+ | 宿主提供 utun 文件描述符;支持 ARM64 真机和模拟器目标 | +| Android | `VpnService` 文件描述符与出站 socket 保护 | +| Linux | 真实的单队列 raw-IP TUN;宿主负责创建接口与路由隔离 | +| Windows | 原生 `Windows.Networking.Vpn` Provider 与完全信任的 Session Host,不使用 Wintun 或 fd 模拟 | + +VCore 是库,不是独立的 VPN 应用。Unix 宿主持有原始 TUN 描述符;VCore 使用并关闭自己复制的描述符。Apple 公开的 packetFlow API 不保证可获取 raw fd,因此实际 Network Extension 集成和设备验证仍由宿主负责。详见 [TUN 集成](../docs/tun-platform.md)与[平台验收边界](../docs/acceptance.md)。 + +跨平台 C ABI 通过 Invoke API v5 接受 JSON 请求: ```c char *VCoreInvoke(const char *request_json); void VCoreFree(char *response); ``` -Windows 安装包另提供 revision-3 host bridge,负责全应用 VPN policy、profile、Session Snapshot 和可选 session backend: +单个公共实例遵循 `initialize → createInstance → prepare(configYaml) → start → stop → destroyInstance` 生命周期。API 还提供配置验证、状态查询、GeoData 状态与延迟测量。详见 [Invoke API](../docs/invoke-api.md)、[Controller API](../docs/controller-api.md)与 [Windows 集成示例](../example/windows-uwp/README.md)。 -```c -char *VCoreWindowsVpnInvoke(const char *request_json); -``` +## Benchmark -业务生命周期为单公共实例: +[**VCore / Mihomo TUN benchmark**](https://github.com/OneXray/container-benchmark) 提供两个内核在相同原生 Linux TUN 环境下的可复现测试设置、实测结果与对比图表。 -```text -initialize - -> createInstance - -> prepare(configYaml) - -> start - -> stop - -> destroyInstance -``` +benchmark 工程同时负责协议互通(`interop`)与内存压力(`stress`),通过显式 `--source vcore=PATH` 提供被测 checkout;VCore 自有脚本只编译核心与平台产物。 -`instanceId` 是当前 runtime 内不可复用的 generation token。同实例命令 fail-fast;纯 `validateConfig` 可并发执行。完整 envelope、method、fd 所有权和 Android protect 契约见 [`docs/invoke-api.md`](../docs/invoke-api.md)。 +测试使用 **1 / 1.5 / 2 Gbps** 混合 TCP/UDP 流量、**每秒 1,000 次 DNS 查询**和增强的 `geosite:cn` / `geoip:cn` 规则,报告实际吞吐量、CPU、观察到的 Linux 峰值 RSS、UDP 丢包与成功的 DNS 查询数。它使用 DIRECT 出口评估 TUN/DNS/路由路径,不衡量加密代理吞吐量;Linux RSS 不等同于 Apple Network Extension 内存占用。 -运行时状态通过 session-local loopback Controller 访问: - -```http -GET /traffic -GET /group -GET /group/{name} -GET /proxies/{name} -PUT /proxies/{name} -Authorization: Bearer -``` - -`GET /traffic` 返回一次 TUN `up/down/upTotal/downTotal` snapshot。代理组端点读取或修改静态 `select` 组的当前直接成员;成功切换只影响当前 session 后续新建的物理 TCP、UDP 与 DNS transport,不迁移既有连接、UDP association、DNS 状态或 TCP 连接池,也不自动故障转移。Controller 管理代理组时,全部路由必须共用一个 Bearer secret,并且可以不启用 TUN。详见 [`docs/controller-api.md`](../docs/controller-api.md)。 - -已认证的 Hysteria2 会话在端口跳跃期间保留上游选择;同一 QUIC 会话替换物理 socket 不重新读取组选择,只有新建认证会话才使用新选择。 - -## 平台 - -| 平台 | 数据面 | 状态 | -| --- | --- | --- | -| iOS / macOS | 宿主提供 utun fd;VCore duplicate 后通过 `rust-tun` 同步 device + Tokio `AsyncFd` 收发 | 已实现;Release iOS 真机 footprint 仍是发布门禁 | -| Android | `VpnService` 提供 raw-IP fd;每个 outbound socket 必须先通过 protect callback | 已实现;真机矩阵仍需按发布计划执行 | -| Windows | `Windows.Networking.Vpn` AppContainer provider + 每 session full-trust runtime;同包命名管道传输 raw-IP | Windows 11 ARM64 开发签名包已通过功能、lifecycle、pressure 与有界 batching 验收 | -| Linux | — | 不支持,入口 fail closed | - -Windows 不使用 fd 模拟层。Provider 只拥有 `VpnChannel`、buffer、routes、物理网络监控、packet gateway 和 fail-closed Stop;完整 VCore runtime、Controller、DNS、rules 与 outbounds 位于 Session Host。packet channel 保持 protocol v1 framing,最多合并 8 个已经就绪的 frame,不等待未来 packet。 - -## 资源边界 - -使用局部队列、缓冲、解析上限、期限与所有者取消,不设置全局业务流准入数量。 -共享上限见[资源策略](../docs/runtime-resource-policy.md),专用预算见协议契约;内存遥测不改变生命周期结果。 +独立 `stress` 命令默认加载完整增强 GeoData,包含属性/反选见证,并叠加 2 Gbps / 60 秒 / 1,000 QPS DNS。实际输入类型、测量和失败见 benchmark README。可选条数参数只调整实验输入,不改变生产行为;任何单次观测都不是任意输入低于 50,000,000 字节的保证,也不替代 Apple 真机验收。 ## 文档 -- [Documentation index](../docs/README.md) -- [Configuration](../docs/config.yaml) -- [Invoke API](../docs/invoke-api.md) -- [Build and test](../scripts/README.md) -- [Acceptance boundaries](../docs/acceptance.md) - -## 示例 - -- [Windows UWP VPN 最小集成](../example/windows-uwp/README.md):同包 Provider、Session Host、完全信任前台、MSIX manifest 和可运行命令行 demo。 - -## 验证 - -```bash -cargo fmt --all -- --check -uv run --project scripts --locked vcore-scripts check core --profile debug -cargo clippy --locked --all-features --lib --bins -- -D warnings -cargo test --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -cargo clippy --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -- -D warnings -uv run --project scripts --locked vcore-scripts check c-header -uv run --project scripts --locked vcore-scripts check tls-dependencies -uv run --project scripts --locked python -m unittest discover -s scripts/tests -uv run --project scripts --locked ruff check scripts -uv run --project scripts --locked ruff format --check scripts -``` - -平台产物(完整命令和环境变量见 [`scripts/README.md`](../scripts/README.md)): - -```bash -uv run --project scripts --locked vcore-scripts build apple -uv run --project scripts --locked vcore-scripts build android -uv run --project scripts --locked vcore-scripts build windows -``` - -当前有效的验证范围与仍延期的物理设备、Windows 发布矩阵见 [`docs/acceptance.md`](../docs/acceptance.md)。 +- [文档索引](../docs/README.md) +- [配置参考](../docs/config.yaml) +- [核心与平台构建](../scripts/README.md) +- [核心回归测试](../tests/README.md) +- [资源策略](../docs/runtime-resource-policy.md) +- [验收与已知限制](../docs/acceptance.md) ## Credits -VCore 的依赖、维护中的 fork、公开 API/协议参考、架构参考与互操作对象包括: +VCore 使用并参考以下公开依赖、协议实现和平台资料: -- [smoltcp](https://github.com/smoltcp-rs/smoltcp)、[clash-rs](https://github.com/Watfaq/clash-rs) 与 [netstack-smoltcp](https://github.com/automesh-network/netstack-smoltcp):用户态 IP stack 与 TUN netstack。 -- [windows-rs](https://github.com/microsoft/windows-rs)、[UWP VPN Plugin Sample](https://github.com/microsoft/UwpVpnPluginSample)、[wireguard-uwp-rs](https://github.com/luqmana/wireguard-uwp-rs)、[Maple](https://github.com/YtFlow/Maple) 与 [YtFlowCore](https://github.com/YtFlow/YtFlowCore):Windows VPN、WinRT activation 与 packet flow。 -- [Xray-core](https://github.com/XTLS/Xray-core)、[Mihomo](https://github.com/MetaCubeX/mihomo) 与 [Leaf](https://github.com/eycorsican/leaf):代理协议、路由、TUN 架构与互操作参考。 -- [rustls](https://github.com/rustls/rustls):未启用指纹的 TLS、QUIC 与共享 WebPKI 证书验证。 -- [boring](https://github.com/cloudflare/boring) / [BoringSSL](https://boringssl.googlesource.com/boringssl/):命名 TLS ClientHello profile 与 VCore 自有 fork 中的 classic REALITY 扩展。 +- 网络与路由:[smoltcp](https://github.com/smoltcp-rs/smoltcp)、[clash-rs](https://github.com/Watfaq/clash-rs)、[netstack-smoltcp](https://github.com/automesh-network/netstack-smoltcp)、[Mihomo](https://github.com/MetaCubeX/mihomo)、[Xray-core](https://github.com/XTLS/Xray-core) 和 [Leaf](https://github.com/eycorsican/leaf)。 +- TLS 与 Shadowsocks:[rustls](https://github.com/rustls/rustls)、[boring](https://github.com/cloudflare/boring)、[BoringSSL](https://boringssl.googlesource.com/boringssl/) 和 [shadowsocks-rust](https://github.com/shadowsocks/shadowsocks-rust)。衍生的重放窗口代码保留了 [MIT 声明](../src/outbound/shadowsocks/packet_window.rs)。 +- Windows 集成:[windows-rs](https://github.com/microsoft/windows-rs)、[UWP VPN Plugin Sample](https://github.com/microsoft/UwpVpnPluginSample)、[wireguard-uwp-rs](https://github.com/luqmana/wireguard-uwp-rs)、[Maple](https://github.com/YtFlow/Maple) 和 [YtFlowCore](https://github.com/YtFlow/YtFlowCore)。 ## License -VCore 使用 MIT License,见 [`LICENSE`](../LICENSE)。 +[MIT](../LICENSE)。 diff --git a/scripts/README.md b/scripts/README.md index d547a48..00e8671 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -1,192 +1,83 @@ -# 构建与验证 +# 平台编译 -所有命令从 VCore 根目录运行,或为 `--project` 显式指定本仓库 scripts。 -Python 工程由 uv/uv.lock 管理,运行时只用标准库;不推断相邻研究仓库。 +`vcore-scripts` 只负责编译 Apple、Android、Windows 产物及记录交付身份。 +命令从 VCore 根目录运行,或通过 `--project` 显式指定本仓库 scripts;Python 工程由 +uv / uv.lock 管理,不推断外部工程。Rust 的定向离线回归见 [tests](../tests/README.md)。 -## 日常检查 - -```sh -uv run --project scripts --locked vcore-scripts check core --profile debug -uv run --project scripts --locked vcore-scripts check core --profile release -uv run --project scripts --locked vcore-scripts check core --profile features -uv run --project scripts --locked python -m unittest discover -s scripts/tests -cargo fmt --all -- --check -uv run --project scripts --locked ruff check scripts -uv run --project scripts --locked ruff format --check scripts -uv run --project scripts --locked vcore-scripts check c-header -# PR/发布来源门禁;本地 boring path 开发态不执行。 -uv run --project scripts --locked vcore-scripts check tls-dependencies -``` - -`check core --list` 只显示命令。Debug/Release 只执行明确的纯内存/配置/安全回归; -features 检查精简构建、准入和完整生产构建,全目标只编译 `--no-run`。 -每条真正执行的 Cargo 测试须有非空成功结果,失败、超时或未 join 均非零退出。 -**不要执行无过滤的 cargo test --all-features --all-targets**:部分历史 fixture 含宿主监听器。 - -额外生产/测试代码静态检查与 netstack: - -```sh -cargo clippy --locked --features ffi --lib --bins -- -D warnings -cargo clippy --locked --all-features --all-targets -- -D warnings -cargo test --locked --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -cargo clippy --locked --manifest-path crates/vcore-netstack/Cargo.toml --all-targets -- -D warnings -``` - -C header 检查只编译 C/C++;TLS 检查审查 resolved graph、provider、来源及批准 revision, -不是网络互通。边界见 [TLS 依赖](../docs/tls-dependencies.md)。 - -## 容器互通 - -```sh -uv run --project scripts --locked vcore-scripts check protocol-interop --suite integration --list -uv run --project scripts --locked vcore-scripts check protocol-interop --suite integration --preflight -uv run --project scripts --locked vcore-scripts check protocol-interop --suite integration -uv run --project scripts --locked vcore-scripts check protocol-coverage --suite integration --run-dir target/interop/runs/ -uv run --project scripts --locked vcore-scripts check protocol-coverage --catalog-only -``` - -可执行容器 suite:vmess、vless、xhttp、hysteria2、security、integration、shadowtls、uot、tuic、httpupgrade。 -`--case` 可重复、`--protocol` 可筛选;子集只证明实际执行的项目,不能签收整套。 -foundations/trojan 保留基础用例和断言,其旧服务端编排尚未全容器化,不能执行; -需要 Trojan 互通时用 integration 中的真实容器路径。 - -文件、用例、事件与报告按协议或验证用途命名,例如 INTEGRATION-PAIR-SS-SS、 -SECURITY-ECH-CHROME;不再提供阶段编号或 --stage 别名。旧报告保持原样,使用 -其记录的 Git revision 复核,不重写旧证据,也不将旧名称解释为新运行结果。 - -执行清单从 Python 的协议定义生成;不再提交重复 cases.json 或规划字段/组合表。 -`--catalog-only` 只检查可执行清单与上限引用,输出 VALID / NOT RUN,不冒充通过。 -`limits.json` 仍与 Rust 常量和边界断言校验。原始事件、命令/退出码、源/锁文件、 -对端身份、artifact hash、资源与清理证据独立复核;缺失/重复/部分结果不能 PASS。 - -每轮新建 target/interop/runs 子目录;不得运行中修改源码。合成凭据/私有日志放临时 -目录并清理,不进入报告。Stop 时和后续静默窗口分别采样;短 tracer 不代替完整长测。 -容器不可用即 BLOCKED,不能退回宿主。完整规则见[测试隔离](../docs/testing-isolation.md)。 - -shadowtls 包含配置/feature、真实 TLS 内存负例、三算法与五种握手模式、ALPN/HRR、 -记录故障、Stop/测速期限和原生 UDP 分流;主对端为 Mihomo,官方 ShadowTLS + -原样 ssserver 另作 TCP 对照。裸 SS、共享 TLS/REALITY/JLS、独立指纹 golden 与 -Apple/Android 编译是同轮必需组;不签收 UoT、实机或旧 SS 空首包风险。 - -uot 验证 SS v2 三算法 × 裸流/v3 的六组合,分别经直连、TCP-only SOCKS5 和 -嵌套 select;覆盖零包、三地址族、交替原站、16 KiB Mihomo 边界/超限和零原生 UDP -旁路。错误密钥/v3 身份及原样 ssserver 不支持是独立负例。内存检查另验 u16 上限、 -预算、首包门控、DNS、取消与 Stop;AnyTLS、裸 SS、精简 feature 和 Apple/Android -是同轮必需回归。使用 protocol-interop / check protocol-coverage 的 `--suite uot`。 - -tuic 覆盖 v5 的双向 TCP、native/quic UDP、三种官方拥塞算法、认证/TLS 负例和 -SOCKS5/SS UoT/AnyTLS 六种上游组合。服务端与上游分容器,避免触发 Mihomo 的 -同进程回环检测;不关闭对端检测或修改其源码。会话重用/重建、嵌套组、node-only 测速、 -回滚与 Stop 独立取证;本轮包含 HY2/H3 共享回归及 Apple/Android 构建。 -内存 u16 上限不当成对端端到端容量,旧七协议压力结果不替代 TUIC 混合压力。 - -httpupgrade 覆盖 VMess 明文/TLS、Trojan TLS × 普通/fast-open 六种模式; -包含双向数据、既有 UDP 编码、ED、身份负例、四个命名 TLS 模板及 Mihomo 关闭对照。 -域名 Trojan UDP 用原生 Xray 补验;VMess 与 Xray 的空包限制分别记录,不把超限 -回复截断计为成功。共享配置/内存握手、VLESS Upgrade/WS、其他 VMess/Trojan -传输、精简 feature 和 Apple/Android 构建均为同轮必需组。 -使用 `protocol-interop` / `protocol-coverage` 的 `--suite httpupgrade`。 - -integration 保留 64 个八协议有序配对,另加 TUIC 双模式 → SS v3 三算法六条链, -复跑 UoT 的 TCP-only 上游/嵌套组及 TUIC 上游消费者。公开入站/TUN、DNS/测速、 -回滚和 100 次生命周期与 100 轮 40-flow 重建、1800 秒混合长测属于同一完整门禁。 -TCP 为 SOCKS5/SS v3/TUIC/HTTPUpgrade 各五条,UDP 为 SS UoT/SS UoT+v3/ -TUIC/HTTPUpgrade 各五条,轮换算法与模式;独立核对逐轮实际配置、资源和静默证据。 -Debug/Release、精简 feature、脚本、netstack、HY2 跳端口和共享安全/传输回归同源重跑。 -integration 可在获准的本地 boring 开发态执行,不替代 release 来源审计;PR/交付候选 -仍须切回 release 分支,在干净 checkout 单独通过 `check tls-dependencies` 并重跑集成。 -SS TCP 数据用客户端预先提交的非空首段验证;SS 叶节点的配对记录明确包含 -`server_first:false` 和上游空首包限制,其他叶节点仍须 server-first。此范围调整不 -减小双向数据量、不移除确定性空首包拒绝回归,也不通过重试取得成功。 - -### 新协议对端能力预检 - -```sh -uv run --project scripts --locked vcore-scripts check protocol-peers --run-dir target/interop/runs/ -``` - -独立的原站/cover、Mihomo 服务端与 Mihomo 对照客户端都在容器中。当前检查 SS2022 -三算法的 v3/原生 UDP、裸流及 v3 上的 UoT v2、TUIC v5 双 UDP 模式、六种 -VMess/Trojan HTTPUpgrade 承载和 SOCKS5 上游;UoT 对照显式选择 v2,关闭服务端原生 UDP。 -`--case` 可选单项,语义 ID 由 `protocol_completion_peers.py` 生成。 -每项必须实际完成 TCP 回传与原站观测的 UDP 请求/回复;记录来源、官方版本/hash、 -原始观测及清理。该工具只证明对端能力,**VCore 新协议行为始终记 NOT RUN**, -不更新生产配置、不代替协议 suite、负例/边界、压力或发布验收。 - -### 对端下载 - -```sh -uv run --project scripts --locked vcore-scripts download mihomo -uv run --project scripts --locked vcore-scripts download mihomo --target linux-arm64 -``` - -使用官方 latest 下载链接;Mihomo 从 latest/download/version.txt 取得资产名所需版本, -再通过二进制 -v 确认实际版本。不查 API、不固定旧版、不本地编译或静默复用旧缓存。 -同轮对端使用同一 release;下载/解压大小与超时有界,失败终止。产物在 target/interop, -版本、URL、压缩包/二进制 SHA-256 和容器镜像 digest 随结果记录;本地摘要不是官方签名。 - -所有协议端、原站、DNS、提供入口的对照客户端在本轮独占容器中;仅清理本轮所有者, -不全局 stop/prune。默认 Mihomo,明确缺口由官方 Xray/V2Ray/Hysteria/ssserver 补验。 -唯一源码构建例外是获准的 xcaddy/Caddy H3/mTLS 网关,仍导入容器、不修改第三方。 - -### 定向安全验证 - -```sh -uv run --project scripts --locked python -m vcore_scripts.protocol_fingerprint_shape target/interop/runs/ -uv run --project scripts --locked python -m vcore_scripts.protocol_fingerprint target/interop/runs/ --client-fingerprint chrome -uv run --project scripts --locked python -m vcore_scripts.protocol_fingerprint_reference --list -uv run --project scripts --locked python -m vcore_scripts.protocol_fingerprint_reference --run-dir target/interop/runs/ -uv run --project scripts --locked python -m vcore_scripts.protocol_fingerprint_reference --check-run target/interop/runs/ -uv run --project scripts --locked python -m vcore_scripts.protocol_jls target/interop/runs/ -uv run --project scripts --locked python -m vcore_scripts.protocol_encryption target/interop/runs/ -uv run --project scripts --locked vcore-scripts check reality-hybrid --run-dir target/interop/runs/ -``` - -指纹验证的 reference、wire、interop 三层证据及 golden 比对规则见 -[指纹验证](../tests/fingerprints/README.md)。模板、主/下载身份、恢复/过期与容器数据面 -各有独立断言;不从别名、另一个模板或 fork 接口探针推导通过。更多参数读对应 --help。 - -## 平台构建与产物 +## 入口 ```sh uv run --project scripts --locked vcore-scripts build apple uv run --project scripts --locked vcore-scripts build android uv run --project scripts --locked vcore-scripts build windows -# 正式候选要求干净、已提交的 checkout: + +# 交付记录要求干净、已提交的 checkout;在对应构建宿主执行。 uv run --project scripts --locked vcore-scripts build apple --delivery -uv run --project scripts --locked vcore-scripts check platform-artifacts --manifest dist/apple/vcore-delivery.json -uv run --project scripts --locked vcore-scripts check platform-abi --manifest dist/apple/vcore-delivery.json +uv run --project scripts --locked vcore-scripts build android --delivery +uv run --project scripts --locked vcore-scripts build windows --delivery ``` -- Apple 在 macOS 构建,输出 dist/apple/LibVCore.xcframework,包含 iOS、模拟器、macOS。 - 最终链接 libc++;module map 已声明,直接 C 链接需 -lc++。 -- Android 在 macOS/Linux 构建,输出 dist/android 下两 ABI 的 libvcore.so 及配套 - libc++_shared.so,宿主一起打包。NDK 优先 ANDROID_NDK_HOME,否则 ANDROID_HOME/ndk。 -- Windows 在原生 ARM64/x64、Visual Studio C++ 环境构建,输出配套 DLL/Provider Host/ - Session Host 及架构/摘要。ARM64 需要 clang-cl/clang、Ninja;保持 BoringSSL 汇编启用。 - 契约见 [Windows VPN](../docs/windows-vpn.md)。 +原生依赖需要 C/C++、CMake、Perl 和 libclang;Rust 目标须预先安装。 +平台构建拒绝 `interop-test` / `benchmark-geodata-http` 等测试 feature。 -Apple/Android 可设置 VCORE_BUILD_PROFILE、VCORE_FEATURES、VCORE_APPLE_DIST_DIR、 -VCORE_IOS_DEPLOYMENT_TARGET、VCORE_MACOS_DEPLOYMENT_TARGET、VCORE_ANDROID_NDK_VERSION、 -VCORE_ANDROID_API、VCORE_ANDROID_TARGETS、VCORE_ANDROID_OUTPUT_DIR。默认值查看 builds.py; -交付禁用隐藏覆盖、测试 feature、非 Release profile,标准 feature 集合显式包含所有支持协议。 +## 平台产物 ---delivery 绑定 commit/tree、lockfile、API/schema、features、toolchain/SDK/NDK、架构及 -全部文件大小/hash;所有平台在清旧记录或构建前拒绝输出路径及其仓库内祖先目录的 -符号链接和 Windows reparse point(包括 junction),不沿链接删除或写入外部产物。 -开始前清旧记录,失败不签收。Android 交付还会清空标准 dist/android -构建输出,避免纳入开发构建遗留的额外 ABI。platform-artifacts 拒绝缺失/额外文件、 -错架构/身份及缺 C++ runtime;可重复 --manifest,--complete 要求 Apple、Android、 -原生 Windows ARM64/x64,仍只证明构建。--source-dir 须显式指定同一候选 checkout。 -platform-abi 在原生 macOS/Windows 链接/加载并执行 C ABI;Windows 另验 snapshot/ -未打包失败关闭,不创建业务运行时,不证明设备 VPN 或正式安装。 +| 平台 | 构建环境与产物 | +| --- | --- | +| Apple | macOS / Xcode;`dist/apple/LibVCore.xcframework`,含 iOS 真机/模拟器、macOS、tvOS 真机/模拟器五切片 | +| Android | macOS 或 Linux / Android NDK;`dist/android` 的 ARM64、x86_64 库及同 ABI / NDK 的 `libc++_shared.so` | +| Windows | 原生 ARM64 或 x64 Windows / Visual Studio C++;`dist/windows/` 的 DLL、Provider Host、Session Host 及身份摘要 | -## CI 与证据 +Apple 的 iOS/tvOS 真机和模拟器仅 ARM64,macOS 为 ARM64/x86_64 universal。 +iOS 最低 13.0、macOS 10.15、tvOS 17.0;ARM64 iOS 模拟器/macOS 切片分别至少 +14.0/11.0。构建检查每个 Rust/原生库对象的 Mach-O 平台、架构和最低版本。 +最终链接 libc++;module map 已声明,直接 C 链接需 `-lc++`。 -Tests 工作流只跑 core Debug/Release、quality/features 和 memory-only netstack; -通用 Python、格式检查只在 quality 执行一次。Release builds 复用 Apple、Android、 -Windows ARM64/x64 构建,每个平台保留产物路径/架构/ABI 检查和未签名 artifacts。 -CI 配置存在不等于已运行或通过;原始 run URL、commit、artifact 有效期随当次记录。 +Android NDK 优先使用 `ANDROID_NDK_HOME`;否则从 `ANDROID_HOME/ndk` 选择 +`VCORE_ANDROID_NDK_VERSION` 指定的完整版本或主版本内最新已安装正式版,默认主版本 +为 30,排除预览版。Windows ARM64 还需要 clang-cl / clang 和 Ninja,并保留 +BoringSSL 汇编;Windows 构建固定使用 Release 和完整生产 feature 集合。 -真实设备、正式宿主、签名安装和商店发布单独签收,见[验收边界](../docs/acceptance.md)。 -旧 Windows tun2socks demo 是显式平台人工验收工具,不是允许在宿主运行协议服务端的例外。 +Android 的 C/C++、CMake 与 bindgen 使用同一 API level(默认 24);绑定生成显式 +传入带 API 版本的 clang target,兼容 NDK 30 的版本要求,并保留生效的额外 clang 参数。 + +普通 Apple/Android 构建可通过 `VCORE_BUILD_PROFILE`、`VCORE_FEATURES` 和各平台 +输出/部署目标/NDK/API/ABI 环境变量定制,具体默认值以 `builds.py` 为准。 +`CARGO_TARGET_DIR` 改变普通构建的中间产物位置,相对路径从本 VCore checkout 解析; +交付模式禁用这些隐藏输出、编译选项和 feature 覆盖。 + +Linux 使用原生 GNU/glibc 工具链,不属于上述打包交付入口: + +```sh +cargo build --locked --release --lib --features ffi +``` + +## 交付边界 + +`--delivery` 在标准输出目录生成 `vcore-delivery.json`,绑定 VCore commit/tree、 +lockfile、API/schema、完整 features、工具链/SDK/NDK 和全部产物的大小/hash。 +本地 boring path 开发态要求 fork 同样干净并记录 commit/tree;PR/发布仍须切回 +Git release 依赖,按 [TLS 来源契约](../docs/tls-dependencies.md) 审查。 + +交付前拒绝输出路径中的符号链接/reparse point,清除旧记录;Android 同时清空标准 +输出以隔离旧 ABI。构建后的内部完整性检查核对文件集合、源码身份、架构与平台元数据, +包括 Android C++ runtime、Apple 全部切片和 Windows 配套进程。 +检查失败不保留交付 manifest。它不执行原生 C/Swift 消费者,不证明设备 VPN、 +签名安装、许可证审核或正式发布;这些按 [验收边界](../docs/acceptance.md) 独立取证。 + +## 独立容器验证 + +互通与压力编排位于公开的 [container-benchmark](https://github.com/OneXray/container-benchmark), +始终显式提供 VCore checkout;VCore 编译入口不导入该工程或猜测相邻路径。 + +```sh +uv run --project /path/to/container-benchmark --locked container-benchmark interop --list +uv run --project /path/to/container-benchmark --locked container-benchmark interop --source vcore=/path/to/VCore +uv run --project /path/to/container-benchmark --locked container-benchmark stress --source vcore=/path/to/VCore --geodata-records 1280000 +``` + +`interop` 保留 Mihomo listener 和 Xray-core / Hysteria2 / V2Ray / Caddy 补充对端; +`stress` 使用原生 Linux TUN、GeoData 与混合吞吐/DNS 负载,二者独立执行。 +所有服务端在隔离容器运行,规则见 [测试隔离](../docs/testing-isolation.md)。 +依赖身份、用例、指标与清理方式由 benchmark 维护;迁移代码或离线测试通过不等于 +当次网络互通或压力测试已经通过。 diff --git a/scripts/fixtures/platform_abi.c b/scripts/fixtures/platform_abi.c deleted file mode 100644 index f9f947c..0000000 --- a/scripts/fixtures/platform_abi.c +++ /dev/null @@ -1,47 +0,0 @@ -/* Production artifact consumer. No runtime instance, sockets, or test features. */ -#include -#include -#include "vcore.h" -#ifdef _WIN32 -#include -#endif - -int main(int argc, char **argv) { -#ifdef _WIN32 - if (argc != 2) return 10; - HMODULE library = LoadLibraryExA(argv[1], NULL, - LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); - if (!library) return 11; - /* memcpy avoids a nonportable data-pointer/function-pointer cast. */ - FARPROC invoke_address = GetProcAddress(library, "VCoreInvoke"); - FARPROC free_address = GetProcAddress(library, "VCoreFree"); - char *(*invoke)(const char *) = NULL; - void (*release)(char *) = NULL; - if (!invoke_address || !free_address) return 12; - memcpy(&invoke, &invoke_address, sizeof(invoke)); - memcpy(&release, &free_address, sizeof(release)); -#else - (void)argc; - (void)argv; - char *(*invoke)(const char *) = VCoreInvoke; - void (*release)(char *) = VCoreFree; -#endif - for (unsigned i = 0; i < 1000; i++) { - char *response = invoke("{\"apiVersion\":5,\"method\":\"version\",\"payload\":{}}"); - int valid = response && strstr(response, "\"success\":true") - && strstr(response, "\"apiVersion\":5") - && strstr(response, "\"configVersion\":31") - && strstr(response, "VCore;engine=rust;coreVersion=0.1.0;invokeApiVersion=5;configVersion=31"); - release(response); - if (!valid) return 20; - } - char *invalid = invoke("{\"apiVersion\":4,\"method\":\"version\",\"payload\":{}}"); - int rejected = invalid && strstr(invalid, "\"success\":false"); - release(invalid); - if (!rejected) return 21; - puts("PASS production ABI: 1000 version/free calls; incompatible API rejected"); -#ifdef _WIN32 - FreeLibrary(library); -#endif - return 0; -} diff --git a/scripts/pyproject.toml b/scripts/pyproject.toml index 0f21fe7..ad3ab93 100644 --- a/scripts/pyproject.toml +++ b/scripts/pyproject.toml @@ -10,7 +10,7 @@ dependencies = [] vcore-scripts = "vcore_scripts.cli:main" [dependency-groups] -dev = ["ruff>=0.15,<0.16"] +dev = ["ruff>=0,<1"] [build-system] requires = ["uv-build>=0.12,<0.13"] diff --git a/scripts/src/vcore_scripts/builds.py b/scripts/src/vcore_scripts/builds.py index f60b511..741b89f 100644 --- a/scripts/src/vcore_scripts/builds.py +++ b/scripts/src/vcore_scripts/builds.py @@ -6,6 +6,7 @@ import locale import mmap import os import platform +import re import shutil import subprocess import tempfile @@ -22,10 +23,106 @@ DEFAULT_FEATURES = ( ) +def tvos_deployment_target() -> str: + value = _env("VCORE_TVOS_DEPLOYMENT_TARGET", "17.0") + if not re.fullmatch(r"\d+\.\d+(?:\.\d+)?", value) or tuple( + map(int, value.split(".")) + ) < (17, 0): + raise ValueError("tvOS deployment target must be 17.0 or newer") + return value + + +def _check_apple_load_commands( + output: str, target_os: str, variant: str | None, architecture: str, minimum: str +) -> int: + """Check every archive member, including native crypto and Rust std objects.""" + expected = { + ("macos", None): "1", + ("ios", None): "2", + ("tvos", None): "3", + ("ios", "simulator"): "7", + ("tvos", "simulator"): "8", + }[target_os, variant] + legacy = {"macos": "MACOSX", "ios": "IPHONEOS", "tvos": "TVOS"}[target_os] + objects = re.split(r"(?m)^\S.*:\n", output)[1:] + if not objects: + raise ValueError("Apple artifact contains no Mach-O objects") + ceiling = tuple((list(map(int, minimum.split("."))) + [0, 0])[:3]) + # These architectures did not exist at the older product deployment floor. + # Keep iOS 13 for devices and macOS 10.15 for the Intel desktop slice. + architecture_floor = { + ("ios", "simulator", "arm64"): (14, 0, 0), + ("macos", None, "arm64"): (11, 0, 0), + }.get((target_os, variant, architecture), (0, 0, 0)) + ceiling = max(ceiling, architecture_floor) + for obj in objects: + versions = [] + for command in re.split(r"Load command \d+\n", obj): + fields = dict( + line.strip().split(maxsplit=1) + for line in command.splitlines() + if len(line.strip().split(maxsplit=1)) == 2 + ) + kind = fields.get("cmd", "") + if kind == "LC_BUILD_VERSION": + if fields.get("platform") != expected: + raise ValueError("wrong Apple Mach-O platform") + versions.append(fields.get("minos", "")) + elif kind.startswith("LC_VERSION_MIN_"): + if kind != "LC_VERSION_MIN_" + legacy or variant == "simulator": + raise ValueError("wrong legacy Apple Mach-O platform") + versions.append(fields.get("version", "")) + if len(versions) != 1 or not re.fullmatch(r"\d+(?:\.\d+){0,2}", versions[0]): + raise ValueError("missing or ambiguous Apple deployment version") + version = tuple((list(map(int, versions[0].split("."))) + [0, 0])[:3]) + if version > ceiling: + raise ValueError( + f"Apple {target_os}/{variant}/{architecture} object requires " + f"{versions[0]}, newer than deployment target {ceiling}" + ) + return len(objects) + + +def check_apple_binary( + path: Path, + target_os: str, + variant: str | None, + architectures: set[str], + minimum: str, +) -> dict[str, int]: + actual = set( + subprocess.check_output( + ["xcrun", "lipo", "-archs", str(path)], text=True, timeout=60 + ).split() + ) + if actual != architectures: + raise ValueError("wrong Apple binary architecture") + result = {} + for architecture in sorted(architectures): + output = subprocess.check_output( + ["xcrun", "otool", "-l", "-arch", architecture, str(path)], + text=True, + timeout=60, + ) + result[architecture] = _check_apple_load_commands( + output, target_os, variant, architecture, minimum + ) + return result + + def _env(name: str, default: str | os.PathLike[str]) -> str: return os.environ.get(name) or os.fspath(default) +def _cargo_target_dir(env: dict[str, str] | None = None) -> Path: + """Resolve the output used by Cargo commands executed in this checkout.""" + value = (os.environ if env is None else env).get("CARGO_TARGET_DIR") + if not value: + return CORE_DIR / "target" + directory = Path(value) + return directory if directory.is_absolute() else CORE_DIR / directory + + def _run( command: list[str | os.PathLike[str]], *, env: dict[str, str] | None = None ) -> None: @@ -41,6 +138,13 @@ def _profile() -> tuple[str, list[str]]: raise RuntimeError(f"unsupported VCORE_BUILD_PROFILE: {profile}") +def _production_features(features: str) -> str: + names = {name.rsplit("/", 1)[-1] for name in re.split(r"[,\s]+", features)} + if names & {"interop-test", "benchmark-geodata-http"}: + raise RuntimeError("platform builds cannot enable test-only features") + return features + + def _installed_rust_targets() -> set[str]: result = subprocess.run( ["rustup", "target", "list", "--installed"], @@ -64,6 +168,7 @@ def _cargo_build( features: str, env: dict[str, str], ) -> None: + _production_features(features) _run( [ "cargo", @@ -159,19 +264,48 @@ def _android_toolchain(ndk_home: Path) -> Path: raise RuntimeError(f"Android NDK toolchain not found under {ndk_home}") +def _android_ndk_home() -> Path: + """Resolve an explicit NDK path/version or the newest installed stable major.""" + if override := os.environ.get("ANDROID_NDK_HOME"): + return Path(override).resolve() + android_home = ( + Path(sdk_home) + if (sdk_home := os.environ.get("ANDROID_HOME")) + else Path.home() / "Library" / "Android" / "sdk" + ) + installed = android_home / "ndk" + selector = _env("VCORE_ANDROID_NDK_VERSION", "30") + if not selector.isdigit(): + return (installed / selector).resolve() + candidates = [] + for path in installed.glob(selector + ".*"): + properties = path / "source.properties" + if ( + not path.is_dir() + or not re.fullmatch(r"\d+\.\d+\.\d+", path.name) + or not properties.is_file() + ): + continue + revision = re.search( + r"(?m)^\s*Pkg\.Revision\s*=\s*(\S+)\s*$", + properties.read_text(encoding="utf-8"), + ) + # SDK preview directories can have numeric names. Their revision still + # carries a beta/rc suffix, so use package metadata to reject previews. + if revision and revision.group(1) == path.name: + candidates.append((tuple(map(int, path.name.split("."))), path)) + if not candidates: + raise RuntimeError(f"no installed stable Android NDK for major {selector}") + return max(candidates, key=lambda candidate: candidate[0])[1].resolve() + + def build_android() -> None: if os.name == "nt": raise RuntimeError("Android artifacts must be built on macOS or Linux") - android_home = Path( - _env("ANDROID_HOME", Path.home() / "Library" / "Android" / "sdk") - ) - ndk_version = _env("VCORE_ANDROID_NDK_VERSION", "28.2.13676358") - ndk_home = Path( - _env("ANDROID_NDK_HOME", android_home / "ndk" / ndk_version) - ).resolve() + ndk_home = _android_ndk_home() android_api = _env("VCORE_ANDROID_API", "24") profile_name, profile_flags = _profile() - features = _env("VCORE_FEATURES", DEFAULT_FEATURES) + features = _production_features(_env("VCORE_FEATURES", DEFAULT_FEATURES)) targets = _env( "VCORE_ANDROID_TARGETS", "aarch64-linux-android x86_64-linux-android" ).split() @@ -214,6 +348,14 @@ def build_android() -> None: if not cpp_runtime.is_file(): raise RuntimeError(f"Android C++ runtime not found: {cpp_runtime}") target_env = target.replace("-", "_") + bindgen_key = f"BINDGEN_EXTRA_CLANG_ARGS_{target}" + bindgen_extra = base_env.get( + bindgen_key, + base_env.get( + f"BINDGEN_EXTRA_CLANG_ARGS_{target_env}", + base_env.get("BINDGEN_EXTRA_CLANG_ARGS", ""), + ), + ) env = base_env | { f"CC_{target_env}": str(linker), f"CXX_{target_env}": str(cpp), @@ -225,9 +367,14 @@ def build_android() -> None: ), "VCORE_CMAKE_ANDROID_ABI": abi, "VCORE_CMAKE_ANDROID_API": android_api, + # NDK 30 rejects bindgen's default unversioned Rust target. Use + # the same API-qualified compiler triple as CC/CXX and CMake. + bindgen_key: ( + f"--target={clang.removesuffix('-clang')} {bindgen_extra}" + ).strip(), } _cargo_build(target, profile_flags, features, env) - artifact = CORE_DIR / "target" / target / profile_name / "libvcore.so" + artifact = _cargo_target_dir(env) / target / profile_name / "libvcore.so" _require_identity(artifact, "Android") destination = output / abi / "libvcore.so" destination.parent.mkdir(parents=True, exist_ok=True) @@ -243,51 +390,52 @@ def build_apple() -> None: if platform.system() != "Darwin": raise RuntimeError("Apple artifacts must be built on macOS") dist = Path(_env("VCORE_APPLE_DIST_DIR", CORE_DIR / "dist" / "apple")).resolve() - work = CORE_DIR / "target" / "vcore-apple" + work = _cargo_target_dir() / "vcore-apple" profile_name, profile_flags = _profile() - features = _env("VCORE_FEATURES", DEFAULT_FEATURES) + features = _production_features(_env("VCORE_FEATURES", DEFAULT_FEATURES)) targets = [ "aarch64-apple-ios", "aarch64-apple-ios-sim", - "x86_64-apple-ios", "aarch64-apple-darwin", "x86_64-apple-darwin", + "aarch64-apple-tvos", + "aarch64-apple-tvos-sim", ] _require_targets(targets) env = os.environ.copy() env["IPHONEOS_DEPLOYMENT_TARGET"] = _env("VCORE_IOS_DEPLOYMENT_TARGET", "13.0") env["MACOSX_DEPLOYMENT_TARGET"] = _env("VCORE_MACOS_DEPLOYMENT_TARGET", "10.15") + env["TVOS_DEPLOYMENT_TARGET"] = tvos_deployment_target() if profile_name == "release": env["CARGO_PROFILE_RELEASE_PANIC"] = "unwind" shutil.rmtree(work, ignore_errors=True) shutil.rmtree(dist / "LibVCore.xcframework", ignore_errors=True) - for directory in ("ios-device", "ios-simulator", "macos"): + for directory in ( + "ios-device", + "ios-simulator", + "macos", + "tvos-device", + "tvos-simulator", + ): (work / directory).mkdir(parents=True) dist.mkdir(parents=True, exist_ok=True) for target in targets: _cargo_build(target, profile_flags, features, env) artifacts = { - target: CORE_DIR / "target" / target / profile_name / "libvcore.a" + target: _cargo_target_dir(env) / target / profile_name / "libvcore.a" for target in targets } for artifact in artifacts.values(): _require_identity(artifact, "Apple") shutil.copy2(artifacts["aarch64-apple-ios"], work / "ios-device/libvcore.a") - _run( - [ - "xcrun", - "lipo", - "-create", - artifacts["aarch64-apple-ios-sim"], - artifacts["x86_64-apple-ios"], - "-output", - work / "ios-simulator/libvcore.a", - ], - env=env, + shutil.copy2(artifacts["aarch64-apple-ios-sim"], work / "ios-simulator/libvcore.a") + shutil.copy2(artifacts["aarch64-apple-tvos"], work / "tvos-device/libvcore.a") + shutil.copy2( + artifacts["aarch64-apple-tvos-sim"], work / "tvos-simulator/libvcore.a" ) _run( [ @@ -302,6 +450,28 @@ def build_apple() -> None: env=env, ) output = dist / "LibVCore.xcframework" + for directory, target_os, variant, architectures, minimum in ( + ("ios-device", "ios", None, {"arm64"}, env["IPHONEOS_DEPLOYMENT_TARGET"]), + ( + "ios-simulator", + "ios", + "simulator", + {"arm64"}, + env["IPHONEOS_DEPLOYMENT_TARGET"], + ), + ("macos", "macos", None, {"arm64", "x86_64"}, env["MACOSX_DEPLOYMENT_TARGET"]), + ("tvos-device", "tvos", None, {"arm64"}, env["TVOS_DEPLOYMENT_TARGET"]), + ( + "tvos-simulator", + "tvos", + "simulator", + {"arm64"}, + env["TVOS_DEPLOYMENT_TARGET"], + ), + ): + check_apple_binary( + work / directory / "libvcore.a", target_os, variant, architectures, minimum + ) _run( [ "xcodebuild", @@ -318,6 +488,14 @@ def build_apple() -> None: work / "macos/libvcore.a", "-headers", CORE_DIR / "include", + "-library", + work / "tvos-device/libvcore.a", + "-headers", + CORE_DIR / "include", + "-library", + work / "tvos-simulator/libvcore.a", + "-headers", + CORE_DIR / "include", "-output", output, ], @@ -419,6 +597,7 @@ def _windows_msvc_environment(architecture: str) -> dict[str, str]: def build_windows() -> None: if os.name != "nt": raise RuntimeError("Windows artifacts must be built on Windows") + _production_features(_env("VCORE_FEATURES", DEFAULT_FEATURES)) architecture = _windows_architecture() output = CORE_DIR / "dist" / "windows" / architecture shutil.rmtree(output, ignore_errors=True) @@ -443,7 +622,7 @@ def build_windows() -> None: ] _run([*base, "--lib", "--bins"], env=env) - release = CORE_DIR / "target" / target / "release" + release = _cargo_target_dir(env) / target / "release" artifacts = [ "vcore.dll", "vcore-windows-vpn-host.exe", diff --git a/scripts/src/vcore_scripts/caddy_build.py b/scripts/src/vcore_scripts/caddy_build.py deleted file mode 100644 index 48580d6..0000000 --- a/scripts/src/vcore_scripts/caddy_build.py +++ /dev/null @@ -1,128 +0,0 @@ -"""Explicit xcaddy-only exception for the isolated H3/mTLS test gateway.""" - -from __future__ import annotations - -import os -import re -import shutil -import urllib.parse -import urllib.request -from pathlib import Path - -from .native_release import PeerArtifact -from .protocol_inputs import redact, sha256 -from .protocol_peers import run_command - - -def latest_tag(project): - """Official latest redirect, not the GitHub API or a pinned old tag.""" - if project not in ("caddy", "xcaddy"): - raise ValueError("unsupported gateway project") - url = f"https://github.com/caddyserver/{project}/releases/latest" - request = urllib.request.Request( - url, method="HEAD", headers={"User-Agent": "VCore-interop-scripts"} - ) - with urllib.request.urlopen(request, timeout=30) as response: - final = urllib.parse.urlsplit(response.geturl()) - prefix = f"/caddyserver/{project}/releases/tag/" - tag = final.path.removeprefix(prefix) - if ( - final.scheme != "https" - or final.netloc != "github.com" - or final.query - or final.fragment - or not final.path.startswith(prefix) - or not re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+", tag) - ): - raise RuntimeError("cannot resolve official stable gateway release") - return tag - - -def build_caddy(directory: Path): - directory.mkdir(parents=True, exist_ok=False) - xcaddy, go = shutil.which("xcaddy"), shutil.which("go") - if not xcaddy or not go: - raise RuntimeError("xcaddy and Go are required for the approved gateway build") - versions = {project: latest_tag(project) for project in ("xcaddy", "caddy")} - tool = run_command([xcaddy, "version"], timeout=10, limit=4096) - if ( - tool.returncode - or not tool.cleanup - or tool.stdout.decode().split()[0] != versions["xcaddy"] - ): - raise RuntimeError( - "installed xcaddy must match the official latest stable release" - ) - compiler = run_command([go, "version"], timeout=10, limit=4096) - if compiler.returncode or not compiler.cleanup: - raise RuntimeError("cannot read Go build identity") - temporary = directory / "build-work" - temporary.mkdir() - binary = directory / "caddy" - environment = os.environ.copy() - # A fresh standard-module build, never a local source replacement or plugin. - for key in tuple(environment): - if key.startswith("XCADDY_") or key in ("CADDY_VERSION", "GOFLAGS", "GOWORK"): - environment.pop(key) - environment.update( - GOOS="linux", - GOARCH="arm64", - CGO_ENABLED="0", - GOWORK="off", - XCADDY_SKIP_CLEANUP="1", - XCADDY_PRINT_VERSION="0", - TMPDIR=str(temporary), - ) - print("Building official latest Caddy with xcaddy for linux/arm64", flush=True) - built = run_command( - [xcaddy, "build", "latest", "--output", str(binary)], - env=environment, - cwd=directory, - timeout=600, - limit=4 * 1024 * 1024, - ) - (directory / "build.log").write_text(redact(built.stdout.decode(errors="replace"))) - if built.returncode or not built.cleanup or not binary.is_file(): - raise RuntimeError("xcaddy gateway build failed") - metadata = run_command([go, "version", "-m", str(binary)], timeout=10, limit=65536) - info = metadata.stdout.decode(errors="replace") - expected = f"\tdep\tgithub.com/caddyserver/caddy/v2\t{versions['caddy']}\t" - if ( - metadata.returncode - or not metadata.cleanup - or expected not in info - or "=>" in info - ): - raise RuntimeError("gateway build is not the unmodified official latest Caddy") - # xcaddy deliberately uses cwd on macOS, TMPDIR on other hosts. - manifests = [ - *directory.glob("buildenv_*/go.mod"), - *temporary.glob("buildenv_*/go.mod"), - ] - if len(manifests) != 1: - raise RuntimeError("xcaddy build manifest is missing or ambiguous") - inputs = {} - for name in ("go.mod", "go.sum", "main.go"): - source = manifests[0].with_name(name) - shutil.copyfile(source, directory / name) - inputs[name] = sha256(source) - # Omit the host executable path from the retained module metadata. - (directory / "modules.txt").write_text("\n".join(info.splitlines()[1:]) + "\n") - return PeerArtifact( - binary, - dict( - kind="Caddy-xcaddy", - target="linux-arm64", - source_built=True, - source_url="https://github.com/caddyserver/caddy/releases/latest", - release=versions["caddy"], - xcaddy_version=tool.stdout.decode().strip(), - go_version=compiler.stdout.decode().strip(), - binary_sha256=sha256(binary), - build_seconds=built.seconds, - build_cleanup=built.cleanup, - inputs=inputs, - extra_plugins=[], - version=None, - ), - ) diff --git a/scripts/src/vcore_scripts/checks.py b/scripts/src/vcore_scripts/checks.py deleted file mode 100644 index fc451c6..0000000 --- a/scripts/src/vcore_scripts/checks.py +++ /dev/null @@ -1,342 +0,0 @@ -from __future__ import annotations - -import json -import platform -import shutil -import subprocess -import tempfile -from pathlib import Path -from typing import Any - -CORE_DIR = Path(__file__).resolve().parents[3] -CRATES_IO_SOURCES = { - "registry+https://github.com/rust-lang/crates.io-index", - "registry+https://index.crates.io/", -} -BORING_REVISION = "d5a5d41850886aef5b269015130ee6d6eb2129ba" -BORING_GIT_SOURCE = ( - f"git+https://github.com/OneXray/boring?branch=release#{BORING_REVISION}" -) - -_C_SOURCE = r"""#include "vcore.h" -int main(void) { - char *response = VCoreInvoke( - "{\"apiVersion\":5,\"method\":\"version\",\"payload\":{}}"); - VCoreFree(response); - VCoreFree((char *)0); - return 0; -} -""" - -_CPP_SOURCE = r"""#include "vcore.h" -int main() { - char *response = VCoreInvoke( - "{\"apiVersion\":5,\"method\":\"version\",\"payload\":{}}"); - VCoreFree(response); - VCoreFree(nullptr); - return 0; -} -""" - - -def check_c_header() -> None: - if platform.system() == "Darwin": - c_compiler = ["xcrun", "clang"] - cpp_compiler = ["xcrun", "clang++"] - else: - clang = shutil.which("clang") - clang_cpp = shutil.which("clang++") - if not clang or not clang_cpp: - raise RuntimeError("clang and clang++ are required to check vcore.h") - c_compiler = [clang] - cpp_compiler = [clang_cpp] - - with tempfile.TemporaryDirectory(prefix="vcore-header-") as directory: - root = Path(directory) - c_source = root / "header.c" - cpp_source = root / "header.cc" - c_source.write_text(_C_SOURCE, encoding="utf-8") - cpp_source.write_text(_CPP_SOURCE, encoding="utf-8") - subprocess.run( - [ - *c_compiler, - "-std=c11", - "-Wall", - "-Wextra", - "-Werror", - "-fsyntax-only", - "-I", - str(CORE_DIR / "include"), - str(c_source), - ], - check=True, - ) - subprocess.run( - [ - *cpp_compiler, - "-std=c++17", - "-Wall", - "-Wextra", - "-Werror", - "-fsyntax-only", - "-I", - str(CORE_DIR / "include"), - str(cpp_source), - ], - check=True, - ) - - -def _shadowsocks_aws_lc_errors(metadata: dict[str, Any]) -> list[str]: - """Allow only the pinned official SS -> crypto -> AWS-LC dependency chain.""" - packages = metadata["packages"] - aws = [p for p in packages if p["name"].lower().startswith("aws-lc")] - if not aws: - return [] - errors: list[str] = [] - allowed: dict[str, dict[str, Any]] = {} - expected = { - "shadowsocks": ("1.25.0", CRATES_IO_SOURCES), - "shadowsocks-crypto": ("0.8.0", CRATES_IO_SOURCES), - "aws-lc-rs": (None, CRATES_IO_SOURCES), - "aws-lc-sys": (None, CRATES_IO_SOURCES), - } - for name, (version, sources) in expected.items(): - matches = [p for p in packages if p["name"] == name] - if ( - len(matches) != 1 - or (version is not None and matches[0]["version"] != version) - or matches[0].get("source") not in sources - ): - errors.append( - f"AWS-LC package is forbidden without the official {name} graph" - ) - else: - allowed[name] = matches[0] - for package in aws: - if package["name"] not in {"aws-lc-rs", "aws-lc-sys"}: - errors.append(f"AWS-LC package is forbidden: {package['name']}") - if len(allowed) != len(expected): - return errors - - nodes = {n["id"]: n for n in metadata["resolve"]["nodes"]} - parents: dict[str, set[str]] = {} - for node in nodes.values(): - for dependency in node.get("deps", []): - parents.setdefault(dependency["pkg"], set()).add(node["id"]) - for child, parent in ( - ("shadowsocks-crypto", "shadowsocks"), - ("aws-lc-rs", "shadowsocks-crypto"), - ("aws-lc-sys", "aws-lc-rs"), - ): - child_id = allowed[child]["id"] - if child_id not in nodes or parents.get(child_id) != {allowed[parent]["id"]}: - errors.append( - f"AWS-LC exception requires {child} to be used only by {parent}" - ) - for name, required in ( - ("shadowsocks", {"aead-cipher-2022"}), - ("shadowsocks-crypto", {"v2", "aws-lc"}), - ): - features = set(nodes.get(allowed[name]["id"], {}).get("features", [])) - if not required <= features: - errors.append(f"AWS-LC exception requires the SS 2022 features of {name}") - if features & {"aead-cipher-2022-extra", "v2-extra"}: - errors.append( - f"only the three standard SS 2022 ciphers are allowed: {name}" - ) - return errors - - -def _tls_dependency_errors(metadata: dict[str, Any]) -> list[str]: - packages = metadata["packages"] - nodes = metadata["resolve"]["nodes"] - errors: list[str] = [] - - def named(name: str) -> list[dict[str, Any]]: - return [package for package in packages if package["name"] == name] - - def require_single(name: str, version: str) -> dict[str, Any] | None: - matches = named(name) - resolved = ( - ", ".join( - sorted( - f"{package['version']} ({package.get('source') or 'path'})" - for package in matches - ) - ) - or "none" - ) - if len(matches) != 1 or matches[0]["version"] != version: - errors.append( - f"expected exactly one {name} {version}; resolved: {resolved}" - ) - return None - return matches[0] - - rustls = require_single("rustls", "0.23.45") - tokio_rustls = require_single("tokio-rustls", "0.26.5") - ring = named("ring") - - if len(ring) != 1: - resolved = ", ".join(sorted(package["version"] for package in ring)) or "none" - errors.append( - f"expected exactly one ring provider package; resolved: {resolved}" - ) - else: - ring_source = ring[0].get("source") or "" - if ring_source not in CRATES_IO_SOURCES: - resolved = ring_source or "path" - errors.append(f"ring must come from crates.io; resolved source: {resolved}") - - if rustls is not None: - rustls_source = rustls.get("source") or "" - if rustls_source not in CRATES_IO_SOURCES: - errors.append( - "rustls must come from crates.io; " - f"resolved source: {rustls_source or 'path'}" - ) - - if tokio_rustls is not None: - source = tokio_rustls.get("source") or "" - if source not in CRATES_IO_SOURCES: - errors.append( - "tokio-rustls must be the crates.io release; " - f"resolved source: {source or 'path'}" - ) - - for package in packages: - source = (package.get("source") or "").lower() - if "watfaq" in source: - errors.append( - f"Watfaq dependency is forbidden: {package['name']} " - f"{package['version']} ({source})" - ) - - if rustls is not None: - rustls_node = next((node for node in nodes if node["id"] == rustls["id"]), None) - if rustls_node is None: - errors.append("rustls is missing from the resolved dependency graph") - else: - features = set(rustls_node["features"]) - missing = {"ring"} - features - forbidden = {"reality", "aws_lc_rs", "fips"} & features - if missing: - errors.append( - f"rustls is missing required features: {', '.join(sorted(missing))}" - ) - if forbidden: - errors.append( - "rustls enables forbidden provider features: " - f"{', '.join(sorted(forbidden))}" - ) - - native = {} - node_by_id = {node["id"]: node for node in nodes} - hpke = require_single("hpke", "0.14.1") - if hpke is not None: - if hpke.get("source") not in CRATES_IO_SOURCES: - errors.append("hpke must be the unmodified crates.io release") - hpke_node = node_by_id.get(hpke["id"]) - if hpke_node is None or set(hpke_node.get("features", [])) != { - "alloc", - "aes", - "chacha", - "x25519", - "hkdfsha2", - }: - errors.append("hpke must use only the approved ECH algorithms") - for name, required in ( - ("boring", {"reality", "client-fingerprint", "shadow-tls-v3", "jls"}), - ("boring-sys", {"reality", "shadow-tls-v3", "jls"}), - ("tokio-boring", set()), - ): - package = require_single(name, "5.2.0") - if package is None: - continue - native[name] = package - if package.get("source") != BORING_GIT_SOURCE: - errors.append( - f"{name} must use the approved boring release branch " - "and locked revision" - ) - node = node_by_id.get(package["id"]) - if node is None: - errors.append(f"{name} is missing from the resolved graph") - continue - features = set(node["features"]) - if not required <= features: - errors.append(f"{name} is missing required TLS features") - if features & { - "fips", - "fips-precompiled", - "rpk", - "pq-experimental", - "restls", - }: - errors.append(f"{name} enables an unapproved native TLS mode") - for parent, children in ( - ("boring", {"boring-sys"}), - ("tokio-boring", {"boring", "boring-sys"}), - ): - if parent in native: - deps = { - dep["pkg"] - for dep in node_by_id.get(native[parent]["id"], {}).get("deps", []) - } - for child in children: - if child not in native or native[child]["id"] not in deps: - errors.append(f"{parent} must directly use the locked {child}") - - errors.extend(_shadowsocks_aws_lc_errors(metadata)) - return errors - - -def check_tls_dependencies() -> None: - result = subprocess.run( - [ - "cargo", - "metadata", - "--manifest-path", - str(CORE_DIR / "Cargo.toml"), - "--locked", - "--all-features", - "--format-version", - "1", - ], - cwd=CORE_DIR, - check=True, - stdout=subprocess.PIPE, - text=True, - encoding="utf-8", - ) - metadata = json.loads(result.stdout) - errors = _tls_dependency_errors(metadata) - if errors: - raise RuntimeError( - "\n".join(f"TLS dependency check failed: {error}" for error in errors) - ) - - ring = next( - package for package in metadata["packages"] if package["name"] == "ring" - ) - rustls = next( - package for package in metadata["packages"] if package["name"] == "rustls" - ) - print("TLS dependency check passed:") - print(f"- one official crates.io rustls {rustls['version']}") - print("- one official tokio-rustls 0.26.5") - print("- one official hpke 0.14.1 with the approved static ECH algorithms") - print( - "- one boring/boring-sys/tokio-boring 5.2.0 release branch " - f"@ {BORING_REVISION[:12]}" - ) - print(f"- one registry ring {ring['version']} provider") - print( - "- unprofiled TLS/QUIC use rustls + ring; REALITY/named profiles use BoringSSL" - ) - print("- no Watfaq, second rustls source or rustls REALITY backend") - if any(p["name"] == "aws-lc-rs" for p in metadata["packages"]): - print("- AWS-LC is restricted to the pinned official Shadowsocks 2022 chain") - else: - print("- no AWS-LC packages") diff --git a/scripts/src/vcore_scripts/cli.py b/scripts/src/vcore_scripts/cli.py index 9b0934e..818d579 100644 --- a/scripts/src/vcore_scripts/cli.py +++ b/scripts/src/vcore_scripts/cli.py @@ -4,21 +4,14 @@ import argparse import subprocess import sys from collections.abc import Sequence -from pathlib import Path from .builds import build_android, build_apple, build_windows -from .checks import check_c_header, check_tls_dependencies -from .mihomo_release import SUPPORTED_TARGETS, download_mihomo -from .protocol_catalogs import CATALOG_DIR, SUITES, check_protocol_catalogs -from .protocol_evidence import check_run -from .protocol_harness import run_protocol_interop -from .tun2socks import run_demo def _parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( prog="vcore-scripts", - description="Build and validate VCore platform artifacts.", + description="Build VCore platform artifacts.", ) commands = parser.add_subparsers(dest="command", required=True) @@ -36,193 +29,22 @@ def _parser() -> argparse.ArgumentParser: help="record production artifact identity", ) - download = commands.add_parser("download", help="download official test peers") - downloads = download.add_subparsers(dest="download", required=True) - peer = downloads.add_parser("mihomo", help="download the latest stable mihomo") - peer.add_argument( - "--target", choices=SUPPORTED_TARGETS, help="default: host platform" - ) - - check = commands.add_parser("check", help="run repository checks") - checks = check.add_subparsers(dest="check", required=True) - core = checks.add_parser( - "core", help="run memory-only tests or feature/build checks" - ) - core.add_argument( - "--profile", choices=("debug", "release", "features"), default="debug" - ) - core.add_argument("--list", dest="list_only", action="store_true") - checks.add_parser("c-header", help="compile vcore.h as C and C++") - checks.add_parser("tls-dependencies", help="validate the locked TLS graph") - delivery = checks.add_parser( - "platform-artifacts", help="verify production artifact evidence, not devices" - ) - delivery.add_argument("--manifest", type=Path, action="append", required=True) - delivery.add_argument("--source-dir", type=Path) - delivery.add_argument( - "--complete", - action="store_true", - help="require every production platform group", - ) - abi = checks.add_parser("platform-abi", help="link/load native production artifact") - abi.add_argument("--manifest", type=Path, required=True) - reality = checks.add_parser( - "reality-hybrid", - help="verify isolated hybrid REALITY, not the complete security suite", - ) - reality.add_argument("--run-dir", type=Path, required=True) - reality.add_argument("--case", dest="identifiers", action="append") - xhttp = checks.add_parser( - "xhttp-peers", - help="probe isolated native XHTTP peer capabilities, not suite acceptance", - ) - xhttp.add_argument("--run-dir", type=Path, required=True) - xhttp.add_argument( - "--identities-only", - action="store_true", - help="probe real download-leg client-identity enforcement only", - ) - peers = checks.add_parser( - "protocol-peers", - help="verify isolated official peer capabilities, not VCore acceptance", - ) - peers.add_argument("--run-dir", type=Path, required=True) - peers.add_argument("--case", dest="identifiers", action="append") - gateway = checks.add_parser( - "xhttp-gateway", - help="build xcaddy and test isolated H3/mTLS native topology", - ) - gateway.add_argument("--run-dir", type=Path, required=True) - gateway.add_argument( - "--identities-only", - action="store_true", - help="small identity probes with native QUIC certificate-error observation", - ) - coverage = checks.add_parser( - "protocol-coverage", help="validate executable catalogs or persisted evidence" - ) - coverage_modes = coverage.add_mutually_exclusive_group(required=True) - coverage_modes.add_argument( - "--catalog-only", - action="store_true", - help="check declarations only, not implementation or behavior acceptance", - ) - coverage_modes.add_argument( - "--run-dir", type=Path, help="validate a complete persisted suite run" - ) - suites = [name.lower() for name in SUITES] - coverage.add_argument("--suite", choices=suites, default="foundations") - coverage.add_argument( - "--manifest", - type=Path, - help="explicit frozen executable manifest for evidence checks", - ) - protocol = checks.add_parser( - "protocol-interop", help="run a protocol or integration capability suite" - ) - protocol.add_argument( - "--suite", choices=suites, required=True, help="capability suite to run" - ) - protocol.add_argument("--case", dest="identifiers", action="append") - protocol.add_argument( - "--protocol", - choices=[ - "foundation", - "legacy", - "trojan", - "vmess", - "vless", - "hysteria2", - "tuic", - ], - ) - modes = protocol.add_mutually_exclusive_group() - modes.add_argument("--list", dest="list_only", action="store_true") - modes.add_argument("--preflight", dest="preflight_only", action="store_true") - protocol.add_argument( - "--run-dir", type=Path, help="fresh child directory of target/interop/runs" - ) - demo = commands.add_parser("demo", help="run opt-in interoperability demos") - demos = demo.add_subparsers(dest="demo", required=True) - tun2socks = demos.add_parser( - "windows-tun2socks", help="run VCore TUN through an external Xray SOCKS inbound" - ) - tun2socks.add_argument("config", type=Path) - tun2socks.add_argument("--xray-source", type=Path, required=True) return parser def main(argv: Sequence[str] | None = None) -> int: args = _parser().parse_args(argv) try: - if args.command == "build": - if args.delivery: - from .platform_delivery import build_delivery + if args.delivery: + from .platform_delivery import build_delivery - build_delivery(args.platform) - elif args.platform == "apple": - build_apple() - elif args.platform == "android": - build_android() - else: - build_windows() - elif args.command == "download": - download_mihomo(args.target) - elif args.command == "check": - if args.check == "core": - from .core_checks import run - - run(args.profile, list_only=args.list_only) - elif args.check == "c-header": - check_c_header() - elif args.check == "platform-artifacts": - from .platform_delivery import check_delivery - - check_delivery( - args.manifest, source_dir=args.source_dir, complete=args.complete - ) - elif args.check == "platform-abi": - from .platform_delivery import check_abi - - check_abi(args.manifest.resolve()) - elif args.check == "xhttp-peers": - from .protocol_xhttp_peers import main as xhttp_peers - - return xhttp_peers(args.run_dir, identities_only=args.identities_only) - elif args.check == "protocol-peers": - from .protocol_completion_peers import run - - run(args.run_dir, identifiers=args.identifiers) - elif args.check == "xhttp-gateway": - from .protocol_xhttp_gateway import main as xhttp_gateway - - return xhttp_gateway(args.run_dir, identities_only=args.identities_only) - elif args.check == "protocol-coverage": - if args.catalog_only: - check_protocol_catalogs(CATALOG_DIR) - else: - check_run( - args.run_dir.resolve(), - args.suite.upper(), - args.manifest, - ) - elif args.check == "protocol-interop": - run_protocol_interop( - stage=args.suite.upper(), - identifiers=args.identifiers, - protocol=args.protocol, - list_only=args.list_only, - preflight_only=args.preflight_only, - run_dir=args.run_dir, - ) - elif args.check == "reality-hybrid": - from .protocol_reality_hybrid import main as run_reality_hybrid - - return run_reality_hybrid(args.run_dir, args.identifiers) - else: - check_tls_dependencies() + build_delivery(args.platform) + elif args.platform == "apple": + build_apple() + elif args.platform == "android": + build_android() else: - run_demo(args.config, xray_source=args.xray_source) + build_windows() except (OSError, RuntimeError, ValueError, subprocess.SubprocessError) as error: print(f"vcore-scripts: {error}", file=sys.stderr) return 1 diff --git a/scripts/src/vcore_scripts/container_bandwidth_origin.py b/scripts/src/vcore_scripts/container_bandwidth_origin.py deleted file mode 100644 index d4adb2f..0000000 --- a/scripts/src/vcore_scripts/container_bandwidth_origin.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Bounded one-way bandwidth origin, only inside the owned HYSTERIA2 container. - -64-KiB records carry a sequence and deterministic payload. Upload progress is -timestamped at the actual origin, not inferred from the client's send buffer. -""" - -import hashlib -import os -import select -import socket -import struct -import sys -import threading -import time - -SIZE = 65536 -END = (1 << 64) - 1 -SLOTS = threading.BoundedSemaphore(4) - - -def exact(stream, size): - value = bytearray() - while len(value) < size: - part = stream.recv(size - len(value)) - if not part: - raise ValueError("truncated bandwidth record") - value.extend(part) - return bytes(value) - - -def record(sequence): - return struct.pack("!Q", sequence) + bytes([sequence % 251]) * (SIZE - 8) - - -def serve(control): - try: - with control, socket.socket() as listener: - control.settimeout(90) - direction = exact(control, 1) - if direction not in (b"U", b"D"): - raise ValueError("invalid bandwidth direction") - listener.bind(("0.0.0.0", 0)) - listener.listen(1) - listener.settimeout(15) - control.sendall(struct.pack("!H", listener.getsockname()[1])) - stream, _ = listener.accept() - with stream: - stream.settimeout(30) - stream.sendall(b"ready") - digest, sequence = hashlib.sha256(), 0 - started = time.monotonic_ns() - if direction == b"U": - while True: - data = exact(stream, SIZE) - number = struct.unpack("!Q", data[:8])[0] - if number == END: - break - if data != record(sequence): - raise ValueError("upload sequence or payload mismatch") - digest.update(data) - sequence += 1 - control.sendall( - b"P" - + struct.pack( - "!QQ", sequence, time.monotonic_ns() - started - ) - ) - stream.sendall(b"done!") - else: - if exact(stream, 1) != b"S": - raise ValueError("missing start") - while True: - ready, _, _ = select.select([stream], [], [], 0) - if ready: - if exact(stream, 1) != b"X": - raise ValueError("invalid stop") - break - data = record(sequence) - stream.sendall(data) - digest.update(data) - sequence += 1 - stream.sendall(record(END)) - control.sendall(b"D" + struct.pack("!Q", sequence) + digest.digest()) - except (OSError, ValueError) as error: - print(type(error).__name__, flush=True) - finally: - SLOTS.release() - - -def main(): - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit("isolated container required") - with socket.socket() as listener: - listener.bind(("0.0.0.0", 24002)) - listener.listen(4) - while True: - control, _ = listener.accept() - if SLOTS.acquire(blocking=False): - threading.Thread(target=serve, args=(control,), daemon=True).start() - else: - control.close() - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/container_close_client.py b/scripts/src/vcore_scripts/container_close_client.py deleted file mode 100644 index 1e4fe2e..0000000 --- a/scripts/src/vcore_scripts/container_close_client.py +++ /dev/null @@ -1,74 +0,0 @@ -"""Read a real official client's close behavior; execute only inside its VM lab.""" - -import json -import os -import socket -import ssl -import struct -import sys -from pathlib import Path - - -def exact(io, length): - data = bytearray() - while len(data) < length: - part = io.recv(length - len(data)) - if not part: - raise ValueError("truncated close control") - data.extend(part) - return bytes(data) - - -def main(proxy, origin, variant="plain"): - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise RuntimeError("isolated close consumer required") - with socket.create_connection((origin, 24000), timeout=10) as control: - vision = variant == "vision-direct" - if variant not in ("plain", "vision-direct"): - raise ValueError("unknown close variant") - control.sendall(b"\x15" if vision else b"\x0b") - port = struct.unpack("!H", exact(control, 2))[0] - with socket.create_connection((proxy, 23002), timeout=10) as client: - client.sendall(b"\x05\x01\x00") - assert exact(client, 2) == b"\x05\x00" - client.sendall( - b"\x05\x01\x00\x01" + socket.inet_aton(origin) + struct.pack("!H", port) - ) - header = exact(client, 4) - assert header[:3] == b"\x05\x00\x00" - exact(client, 6 if header[3] == 1 else 18) - io = client - if vision: - from tls_bio import TlsBio - - context = ssl.create_default_context( - cafile=str(Path(__file__).with_name("cert.pem")) - ) - context.minimum_version = ssl.TLSVersion.TLSv1_3 - context.maximum_version = ssl.TLSVersion.TLSv1_3 - io = TlsBio(client, context, server=False) - assert io.tls.version() == "TLSv1.3" - assert exact(control, 2) == b"A\x13" - assert exact(io, 5) == b"hello" - payload = b"Z" * 65536 if vision else b"ping" - io.sendall(payload) - assert exact(io, len(payload)) == payload - client.shutdown(socket.SHUT_WR) - tail = bytearray() - try: - while chunk := io.recv(1024): - tail.extend(chunk) - if len(tail) > 1024: - raise ValueError("close tail limit") - except ConnectionResetError: - pass - assert exact(control, 1 if vision else 2) == (b"D" if vision else b"AD") - print( - json.dumps( - {"tail_hex": tail.hex(), "terminated": True, "variant": variant} - ) - ) - - -if __name__ == "__main__": - main(*sys.argv[1:]) diff --git a/scripts/src/vcore_scripts/container_protocol_client.py b/scripts/src/vcore_scripts/container_protocol_client.py deleted file mode 100644 index 9d1dfe7..0000000 --- a/scripts/src/vcore_scripts/container_protocol_client.py +++ /dev/null @@ -1,110 +0,0 @@ -"""Container-only comparison client for matched official protocol peers.""" - -import json -import os -import socket -import struct -import sys -from pathlib import Path - - -def exact(stream, size): - data = bytearray() - while len(data) < size: - part = stream.recv(size - len(data)) - if not part: - raise ValueError("truncated peer response") - data.extend(part) - return bytes(data) - - -def socks(stream, command, host, port): - stream.sendall(b"\x05\x01\x00") - if exact(stream, 2) != b"\x05\x00": - raise ValueError("SOCKS authentication failed") - stream.sendall( - bytes([5, command, 0, 1]) + socket.inet_aton(host) + struct.pack("!H", port) - ) - header = exact(stream, 4) - if header[:3] != b"\x05\x00\x00": - raise ValueError("SOCKS request failed") - if header[3] == 1: - address = socket.inet_ntop(socket.AF_INET, exact(stream, 4)) - elif header[3] == 4: - address = socket.inet_ntop(socket.AF_INET6, exact(stream, 16)) - else: - raise ValueError("unexpected SOCKS bound address") - return address, struct.unpack("!H", exact(stream, 2))[0] - - -def probe(option, origin): - """One real TCP exchange and two origin-observed UDP exchanges, no retry.""" - ingress = (option["socks_host"], option["socks_port"]) - payload = bytes(range(256)) * 4 - with socket.create_connection((origin, 24000), timeout=10) as control: - control.sendall(b"\x0d") - port = struct.unpack("!H", exact(control, 2))[0] - with socket.create_connection(ingress, timeout=10) as stream: - socks(stream, 1, origin, port) - stream.sendall(payload) - if exact(control, 1) != b"A" or exact(stream, len(payload)) != payload: - raise ValueError("TCP origin exchange differs") - with ( - socket.create_connection((origin, 24000), timeout=10) as control, - socket.create_connection(ingress, timeout=10) as association, - socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp, - ): - control.sendall(b"\x04") - port = struct.unpack("!H", exact(control, 2))[0] - relay = socks(association, 3, "0.0.0.0", 0) - # The ingress explicitly listens on IPv4, so no host/system DNS is used. - if relay[0] == "0.0.0.0": - relay = (option["socks_host"], relay[1]) - udp.settimeout(10) - udp.connect(relay) - header = ( - b"\x00\x00\x00\x01" + socket.inet_aton(origin) + struct.pack("!H", port) - ) - for size in (64, 1200): - payload = bytes(index % 251 for index in range(size)) - if udp.send(header + payload) != len(header) + size: - raise ValueError("partial UDP submission") - observed, _source = struct.unpack("!HH", exact(control, 4)) - if observed != size or exact(control, observed) != payload: - raise ValueError("UDP origin exchange differs") - if udp.recv(65536) != header + payload: - raise ValueError("UDP reply differs") - return dict( - case_id=option["case_id"], - status="PASS", - tcp_bytes_each_direction=1024, - udp_origin_packets=2, - udp_reply_packets=2, - udp_sizes=[64, 1200], - ) - - -def main(): - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_CLIENT") != "1": - raise SystemExit("isolated container harness required") - fixture = json.loads(Path(sys.argv[1]).read_text()) - for option in fixture["cases"]: - try: - record = probe(option, fixture["origin"]) - except (OSError, ValueError) as error: - print( - json.dumps( - dict( - case_id=option["case_id"], - status="FAIL", - error=type(error).__name__, - ) - ), - flush=True, - ) - raise SystemExit(1) from None - print(json.dumps(record), flush=True) - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/container_quic_observer.py b/scripts/src/vcore_scripts/container_quic_observer.py deleted file mode 100644 index babdc75..0000000 --- a/scripts/src/vcore_scripts/container_quic_observer.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Read-only, bounded native qlog observer for the isolated identity diagnostic. - -Return certificate failure categories only, never frames, keys, connection IDs, -request headers or peer addresses. Raw qlogs stay in the disposable container. -""" - -from __future__ import annotations - -import hashlib -import json -from pathlib import Path - - -def summarize_trace(text): - failures = [] - for line in text.splitlines(keepends=True): - if not line.endswith("\n"): - continue # The native writer can still be appending the last record. - row = json.loads(line.lstrip("\x1e")) - if row.get("name") != "transport:connection_closed": - continue - data = row.get("data", {}) - code = data.get("connection_error", "") - if data.get("initiator") != "local" or not code.startswith("crypto_error_"): - continue - # RFC 9001 CRYPTO_ERROR = 0x100 + the RFC 8446 TLS alert number. - # quic-go's local crypto error deliberately leaves qlog.reason empty. - categories = { - "crypto_error_0x174": "certificate_required", - "crypto_error_0x12d": "certificate_expired", - "crypto_error_0x130": "unknown_ca", - } - category = categories.get(code) - if category: - failures.append({"tls_alert": code, "category": category}) - return failures - - -def main(): - files = sorted(Path("/data/qlog").glob("*.sqlog")) - if len(files) > 128 or sum(path.stat().st_size for path in files) > 16 * 1024**2: - raise ValueError("native identity trace exceeded diagnostic bound") - print( - json.dumps( - { - hashlib.sha256(path.name.encode()).hexdigest(): summarize_trace( - path.read_text() - ) - for path in files - } - ) - ) - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/container_reality_hybrid.py b/scripts/src/vcore_scripts/container_reality_hybrid.py deleted file mode 100644 index 0393963..0000000 --- a/scripts/src/vcore_scripts/container_reality_hybrid.py +++ /dev/null @@ -1,263 +0,0 @@ -"""Container-only OpenSSL cover sites, TCP origin, and transparent observation.""" - -import json -import os -import select -import socket -import ssl -import sys -import threading -import time -from pathlib import Path - -ROOT = Path("/data/fixture") -EVENTS = Path("/data/events.jsonl") -LOCK = threading.Lock() -SLOTS = threading.BoundedSemaphore(32) - - -def emit(**event): - with LOCK, EVENTS.open("a") as output: - if EVENTS.stat().st_size > 8 * 1024 * 1024: - raise ValueError("observer event limit") - output.write(json.dumps(event) + "\n") - - -def exact(stream, size): - data = bytearray() - while len(data) < size: - part = stream.recv(size - len(data)) - if not part: - raise EOFError() - data.extend(part) - return bytes(data) - - -def word(data, at): - return int.from_bytes(data[at : at + 2], "big") - - -def client_hello(stream): - wire, message = bytearray(), bytearray() - for _ in range(8): - head = exact(stream, 5) - size = word(head, 3) - if head[0] != 22 or not 1 <= size <= 16384: - raise ValueError("TLS record") - body = exact(stream, size) - wire.extend(head + body) - message.extend(body) - if len(message) > 65536: - raise ValueError("hello limit") - if len(message) >= 4: - expected = 4 + int.from_bytes(message[1:4], "big") - if message[0] != 1 or len(message) > expected: - raise ValueError("ClientHello length") - if len(message) == expected: - return bytes(wire), bytes(message) - raise ValueError("record count") - - -def extensions(message, client): - at = 39 + message[38] - if client: - at += 2 + word(message, at) - at += 1 + message[at] - else: - at += 3 - end = at + 2 + word(message, at) - at += 2 - if end != len(message): - raise ValueError("extension length") - while at < end: - kind, size = word(message, at), word(message, at + 2) - data = message[at + 4 : at + 4 + size] - if len(data) != size: - raise ValueError("truncated extension") - yield kind, data - at += 4 + size - - -def shares(message): - result = [] - for kind, data in extensions(message, True): - if kind == 51: - at = 2 - while at < len(data): - group, size = word(data, at), word(data, at + 2) - if group & 0x0F0F != 0x0A0A: - result.append([group, size]) - at += 4 + size - if at != len(data): - raise ValueError("key share length") - return result - - -def selected_group(wire): - if len(wire) < 5 or len(wire) < 5 + word(wire, 3): - return None - if wire[0] != 22 or wire[5] != 2: - return -1 - message = wire[5 : 5 + word(wire, 3)] - size = 4 + int.from_bytes(message[1:4], "big") - for kind, data in extensions(message[:size], False): - if kind == 51: - return word(data, 0) - return -1 - - -def handle(stream, port, context): - try: - with stream: - stream.settimeout(5) - if port == 9000: - # Readiness opens a connection without application data. - received = 0 - while part := stream.recv(16384): - if not received: - emit(port=port, event="origin-data") - received += len(part) - if received > 10 * 1024 * 1024: - raise ValueError("origin byte limit") - stream.sendall(part) - if received: - emit(port=port, event="origin-complete", bytes=received) - return - try: - raw, hello = client_hello(stream) - except EOFError: - return - emit( - port=port, - event="client-hello", - shares=shares(hello), - groups=[ - word(data, at) - for kind, data in extensions(hello, True) - if kind == 10 - for at in range(2, len(data), 2) - if word(data, at) & 0x0F0F != 0x0A0A - ], - ) - if context is None: - address = json.loads((ROOT / "routes.json").read_text())[str(port)] - with socket.create_connection(tuple(address), timeout=5) as upstream: - upstream.sendall(raw) - deadline, total = time.monotonic() + 240, len(raw) - flight, observed = bytearray(), False - while time.monotonic() < deadline: - ready, _, _ = select.select([stream, upstream], [], [], 0.2) - for source in ready: - data = source.recv(16384) - if not data: - if source is upstream: - emit( - port=port, - event="server-close", - bytes=len(flight), - ) - return - total += len(data) - if total > 128 * 1024 * 1024: - raise ValueError("relay byte limit") - if source is upstream and not observed: - flight.extend(data) - if len(flight) > 65536: - raise ValueError("server flight limit") - group = selected_group(flight) - if group is not None: - emit( - port=port, - event="server-hello" - if group >= 0 - else "tls-response", - group=group, - hrr=flight[11:43] - == bytes.fromhex( - "cf21ad74e59a6111be1d8c021e65b891" - "c2a211167abb8c5e079e09e2c8a8339c" - ), - ) - observed = True - (upstream if source is stream else stream).sendall(data) - raise TimeoutError("relay duration") - incoming, outgoing = ssl.MemoryBIO(), ssl.MemoryBIO() - tls = context.wrap_bio(incoming, outgoing, server_side=True) - incoming.write(raw) - for _ in range(32): - try: - tls.do_handshake() - stream.sendall(outgoing.read()) - emit(port=port, event="cover-complete", version=tls.version()) - return - except ssl.SSLWantReadError: - data = outgoing.read() - if data: - stream.sendall(data) - data = stream.recv(16384) - if not data: - return - incoming.write(data) - raise ValueError("cover iteration limit") - except ssl.SSLError as error: - emit( - port=port, - event="no-shared-group" - if error.reason == "NO_SUITABLE_KEY_SHARE" - else "closed", - ) - except (OSError, ValueError, EOFError): - emit(port=port, event="closed") - finally: - SLOTS.release() - - -def serve(port, context): - with socket.socket(socket.AF_INET6) as listener: - listener.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) - listener.bind(("::", port)) - listener.listen(32) - while True: - stream, _ = listener.accept() - SLOTS.acquire() - threading.Thread( - target=handle, args=(stream, port, context), daemon=True - ).start() - - -if __name__ == "__main__": - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit("isolated REALITY observer required") - EVENTS.touch() - contexts = {} - for port, group, version in [ - (24431, "X25519MLKEM768", ssl.TLSVersion.TLSv1_3), - (24432, "X25519", ssl.TLSVersion.TLSv1_3), - (24433, "secp384r1", ssl.TLSVersion.TLSv1_3), - (24434, "X25519", ssl.TLSVersion.TLSv1_2), - ]: - context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) - context.load_cert_chain(ROOT / "cert.pem", ROOT / "key.pem") - # Python's EC-NID setter rejects hybrid group names even with OpenSSL - # 3.5. Use the native default list for the hybrid cover, then verify the - # actual ServerHello/finished connection selected 4588 in the harness. - if group != "X25519MLKEM768": - context.set_ecdh_curve(group) - context.minimum_version = context.maximum_version = version - context.set_alpn_protocols(["h2", "http/1.1"]) - contexts[port] = context - emit(event="native-tls", version=ssl.OPENSSL_VERSION) - threads = [ - threading.Thread(target=serve, args=(port, context)) - for port, context in [ - *contexts.items(), - *( - (int(port), None) - for port in json.loads((ROOT / "routes.json").read_text()) - ), - ] - ] - for thread in threads: - thread.start() - for thread in threads: - thread.join() diff --git a/scripts/src/vcore_scripts/container_restart_peer.py b/scripts/src/vcore_scripts/container_restart_peer.py deleted file mode 100644 index 5eef0a9..0000000 --- a/scripts/src/vcore_scripts/container_restart_peer.py +++ /dev/null @@ -1,63 +0,0 @@ -"""Restart only this guest's owned, unmodified protocol child on a fixture command.""" - -from __future__ import annotations - -import os -import select -import socket -import subprocess -import sys -import time - - -def terminate(peer): - peer.terminate() - try: - peer.wait(timeout=3) - except subprocess.TimeoutExpired: - peer.kill() - peer.wait(timeout=3) - - -def start(argv): - peer = subprocess.Popen(argv) - try: - until = time.monotonic() + 10 - while peer.poll() is None and time.monotonic() < until: - try: - with socket.create_connection(("127.0.0.1", 23999), timeout=0.1): - return peer - except OSError: - time.sleep(0.05) - raise RuntimeError("owned protocol child not ready") - except BaseException: - terminate(peer) - raise - - -def main(): - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit("isolated restart peer required") - peer = start(sys.argv[1:]) - try: - with socket.socket() as control: - control.bind(("0.0.0.0", 23998)) - control.listen(4) - while peer.poll() is None: - ready, _, _ = select.select([control], [], [], 0.1) - if not ready: - continue - client, _ = control.accept() - with client: - client.settimeout(2) - if client.recv(1) == b"R": - terminate(peer) - peer = start(sys.argv[1:]) - client.sendall(b"R") - raise RuntimeError("owned protocol child exited") - finally: - terminate(peer) - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/container_shadowtls_fault.py b/scripts/src/vcore_scripts/container_shadowtls_fault.py deleted file mode 100644 index 990b219..0000000 --- a/scripts/src/vcore_scripts/container_shadowtls_fault.py +++ /dev/null @@ -1,203 +0,0 @@ -"""Bounded container TCP fault relay around an unmodified official ShadowTLS peer. - -Only observes record framing/tags to locate faults; never terminates TLS or SS. -""" - -from __future__ import annotations - -import contextlib -import hashlib -import hmac -import json -import os -import socket -import sys -import threading -from pathlib import Path - -ROOT = Path("/data/fixture") -EVENTS = Path("/data/fault-events.jsonl") -LOCK = threading.Lock() -SLOTS = threading.BoundedSemaphore(16) -STALL = {"hello_seen": 0, "client_closed": 0} -PASSWORD = b"synthetic-shadowtls-fixture" -HRR = "cf21ad74e59a6111be1d8c021e65b891c2a211167abb8c5e079e09e2c8a8339c" - - -def emit(mode, **fields): - with LOCK, EVENTS.open("a") as output: - output.write(json.dumps(dict(mode=mode, **fields)) + "\n") - if mode == "stall": - for key in STALL: - STALL[key] += fields.get(key) is True - - -def observe(): - with socket.socket(socket.AF_INET) as listener: - listener.bind(("0.0.0.0", 23998)) - listener.listen(4) - while True: - client, _ = listener.accept() - with client: - client.settimeout(2) - try: - if client.recv(1) == b"?": - with LOCK: - data = json.dumps(STALL).encode() - client.sendall(data) - except OSError: - pass - - -def exact(raw, count): - data = bytearray() - while len(data) < count: - part = raw.recv(count - len(data)) - if not part: - if data: - raise EOFError("truncated fixture record") - return None - data.extend(part) - return bytes(data) - - -def record(raw): - header = exact(raw, 5) - if header is None: - return None - size = int.from_bytes(header[3:5], "big") - if not 0 < size <= 18436: - raise ValueError("record bound") - body = exact(raw, size) - if body is None: - raise EOFError("record body missing") - return header + body - - -def upload(client, upstream): - try: - while data := client.recv(16384): - upstream.sendall(data) - upstream.shutdown(socket.SHUT_WR) - except OSError: - pass - - -def serve(client, mode, target): - upstream = None - worker = None - try: - with client: - client.settimeout(15) - if mode == "stall": - # Observe a real client hello, then await the client's deadline - # or Stop. No business or cover destination is contacted. - if record(client) is None: - return # Readiness probes are not real stalled handshakes. - emit(mode, hello_seen=True) - while client.recv(16384): - pass - emit(mode, client_closed=True) - return - upstream = socket.create_connection(target, timeout=10) - upstream.settimeout(15) - client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - worker = threading.Thread(target=upload, args=(client, upstream)) - worker.start() - cover = business = None - count = 0 - mutated = False - while (data := record(upstream)) is not None: - count += 1 - if data[0] == 22 and cover is None: - if len(data) < 43 or data[5] != 2: - raise ValueError("expected ServerHello") - seed = data[11:43] - cover = hmac.new(PASSWORD, seed, hashlib.sha1) - business = hmac.new(PASSWORD, seed + b"S", hashlib.sha1) - if seed.hex() == HRR: - emit(mode, hrr_seen=True) - if data[0] == 23: - if len(data) < 9 or cover is None or business is None: - raise ValueError("authenticated record expected") - trial = cover.copy() - trial.update(data[9:]) - is_cover = hmac.compare_digest(trial.digest()[:4], data[5:9]) - if is_cover: - cover = trial - else: - trial = business.copy() - trial.update(data[9:]) - if not hmac.compare_digest(trial.digest()[:4], data[5:9]): - raise ValueError("unclassified official record") - trial.update(data[5:9]) - business = trial - if not mutated and ( - (mode == "cover-mac" and is_cover) - or ( - mode in {"business-mac", "business-truncated"} - and not is_cover - ) - ): - mutated = True - emit( - mode, - injected=True, - phase="cover" if is_cover else "business", - ) - if mode == "business-truncated": - client.sendall(data[:8]) - client.shutdown(socket.SHUT_WR) - break - changed = bytearray(data) - changed[5] ^= 1 - data = bytes(changed) - if mode == "fragment" and count <= 8: - # The same authenticated records, with split headers and - # short writes; afterwards data remain bulk, not 10M syscalls. - for begin, end in ((0, 1), (1, 3), (3, 5), (5, 9), (9, len(data))): - if end > begin: - client.sendall(data[begin:end]) - emit(mode, split_record=True) - else: - client.sendall(data) - except (OSError, EOFError, ValueError): - pass - finally: - for raw in (client, upstream): - if raw is not None: - with contextlib.suppress(OSError): - raw.shutdown(socket.SHUT_RDWR) - raw.close() - if worker is not None: - worker.join(timeout=2) - if worker.is_alive(): - emit(mode, upload_join_failed=True) - SLOTS.release() - - -def listen(item): - with socket.socket(socket.AF_INET) as listener: - listener.bind(("0.0.0.0", item["port"])) - listener.listen(16) - while True: - client, _ = listener.accept() - if not SLOTS.acquire(blocking=False): - client.close() - continue - threading.Thread( - target=serve, - args=(client, item["mode"], (item["server"], item["server_port"])), - daemon=True, - ).start() - - -if __name__ == "__main__": - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit("isolated fault relay required") - EVENTS.touch() - threading.Thread(target=observe, daemon=True).start() - items = json.loads((ROOT / "config.json").read_text()) - for item in items[1:]: - threading.Thread(target=listen, args=(item,), daemon=True).start() - listen(items[0]) diff --git a/scripts/src/vcore_scripts/container_shadowtls_peer.py b/scripts/src/vcore_scripts/container_shadowtls_peer.py deleted file mode 100644 index 535ff08..0000000 --- a/scripts/src/vcore_scripts/container_shadowtls_peer.py +++ /dev/null @@ -1,70 +0,0 @@ -"""Container-only ShadowTLS cover variations; no proxy protocol implementation.""" - -from __future__ import annotations - -import json -import os -import socket -import ssl -import sys -import threading -from pathlib import Path - -import origin - -EVENTS = Path("/data/cover-events.jsonl") -LOCK = threading.Lock() -SLOTS = threading.BoundedSemaphore(16) - - -def event(**value): - with LOCK, EVENTS.open("a") as output: - output.write(json.dumps(value) + "\n") - - -def handle(raw, context, port): - try: - with raw: - raw.settimeout(5) - with context.wrap_socket(raw, server_side=True) as tls: - event( - port=port, version=tls.version(), alpn=tls.selected_alpn_protocol() - ) - tls.recv(1) - except OSError: - pass - finally: - SLOTS.release() - - -def cover(port, version, curve): - context = origin.tls_context(version) - context.set_ecdh_curve(curve) - context.set_alpn_protocols(["h2", "http/1.1", "fixture-custom"]) - with socket.socket(socket.AF_INET) as listener: - listener.bind(("0.0.0.0", port)) - listener.listen(16) - while True: - raw, _ = listener.accept() - if not SLOTS.acquire(blocking=False): - raw.close() - continue - threading.Thread( - target=handle, args=(raw, context, port), daemon=True - ).start() - - -if __name__ == "__main__": - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit("isolated ShadowTLS fixture required") - EVENTS.touch() - for port, version, curve in ( - (24001, ssl.TLSVersion.TLSv1_3, "X25519"), - (24002, ssl.TLSVersion.TLSv1_3, "secp384r1"), - (24003, ssl.TLSVersion.TLSv1_2, "X25519"), - ): - threading.Thread(target=cover, args=(port, version, curve), daemon=True).start() - # This fixture owns the cover workers; the shared origin still owns its - # independent TCP/UDP control channel and business-byte observations. - origin.camouflage = lambda: None - origin.main() diff --git a/scripts/src/vcore_scripts/container_tls_bio.py b/scripts/src/vcore_scripts/container_tls_bio.py deleted file mode 100644 index 466ad16..0000000 --- a/scripts/src/vcore_scripts/container_tls_bio.py +++ /dev/null @@ -1,48 +0,0 @@ -"""Fixture-only public OpenSSL MemoryBIO driver; never a TLS implementation.""" - -import ssl - - -class TlsBio: - def __init__(self, stream, context, *, server): - self.stream = stream - self.incoming = ssl.MemoryBIO() - self.outgoing = ssl.MemoryBIO() - self.tls = context.wrap_bio( - self.incoming, - self.outgoing, - server_side=server, - server_hostname=None if server else "localhost", - ) - self.perform(self.tls.do_handshake) - - def flush(self): - while self.outgoing.pending: - self.stream.sendall(self.outgoing.read()) - - def perform(self, operation, *args): - while True: - try: - result = operation(*args) - self.flush() - return result - except ssl.SSLWantWriteError: - self.flush() - except ssl.SSLWantReadError: - self.flush() - chunk = self.stream.recv(65536) - if chunk: - self.incoming.write(chunk) - else: - self.incoming.write_eof() - - def recv(self, length): - try: - return self.perform(self.tls.read, length) - except (ssl.SSLEOFError, ssl.SSLZeroReturnError): - return b"" - - def sendall(self, data): - offset = 0 - while offset < len(data): - offset += self.perform(self.tls.write, data[offset:]) diff --git a/scripts/src/vcore_scripts/container_tls_observer.py b/scripts/src/vcore_scripts/container_tls_observer.py deleted file mode 100644 index 8f20fc1..0000000 --- a/scripts/src/vcore_scripts/container_tls_observer.py +++ /dev/null @@ -1,179 +0,0 @@ -"""Container-only TLS observer; never decodes or serves a proxy protocol.""" - -from __future__ import annotations - -import base64 -import contextlib -import json -import os -import socket -import ssl -import threading -import time -from pathlib import Path - - -def capture_client_hello(receive): - """Read at most eight records and 64 KiB of ClientHello from injected IO.""" - - def exact(count): - result = bytearray() - while len(result) < count: - part = receive(count - len(result)) - if not part: - raise EOFError("incomplete TLS record") - result.extend(part) - return bytes(result) - - records, handshake, layout = bytearray(), bytearray(), [] - for _ in range(8): - header = exact(5) - size = int.from_bytes(header[3:], "big") - if header[0] != 22 or not 1 <= size <= 18432: - raise ValueError("expected bounded handshake record") - payload = exact(size) - records.extend(header + payload) - handshake.extend(payload) - layout.append( - dict(type=header[0], version=int.from_bytes(header[1:3], "big"), bytes=size) - ) - if len(handshake) >= 4: - hello_size = int.from_bytes(handshake[1:4], "big") + 4 - if handshake[0] != 1 or hello_size > 65536: - raise ValueError("expected bounded ClientHello") - if len(handshake) >= hello_size: - return bytes(records), bytes(handshake[:hello_size]), layout - raise ValueError("ClientHello record limit") - - -def main(): - if os.environ.get("VCORE_ISOLATED_TLS_OBSERVER") != "1": - raise RuntimeError("TLS observer requires an owned isolated container") - log = Path("/data/events.jsonl") - lock, stop = threading.Lock(), threading.Event() - deadline = time.monotonic() + 900 - log.touch() - - def emit(event): - with lock: - if log.stat().st_size > 8 * 1024 * 1024: - stop.set() - raise RuntimeError("observer output limit") - with log.open("a") as output: - output.write(json.dumps(event, separators=(",", ":")) + "\n") - - contexts = {24430: None} - for port, version in ( - (24431, ssl.TLSVersion.TLSv1_3), - (24432, ssl.TLSVersion.TLSv1_2), - ): - context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) - context.load_cert_chain("/data/fixture/cert.pem", "/data/fixture/key.pem") - context.minimum_version = context.maximum_version = version - context.set_alpn_protocols(["h2", "http/1.1"]) - contexts[port] = context - - def serve(port, context): - try: - with socket.socket() as listener: - listener.settimeout(0.5) - listener.bind(("0.0.0.0", port)) - listener.listen(8) - sequence = 0 - while not stop.is_set() and time.monotonic() < deadline: - try: - stream, _ = listener.accept() - except TimeoutError: - continue - sequence += 1 - if sequence > 512: - raise RuntimeError("observer connection limit") - with stream: - stream.settimeout(5) - event = dict(port=port, sequence=sequence) - try: - raw, hello, layout = capture_client_hello(stream.recv) - except EOFError: - continue # The readiness connect carries no TLS bytes. - except (OSError, ValueError) as error: - emit( - event - | dict( - status="capture-error", reason=type(error).__name__ - ) - ) - continue - event.update( - client_hello_b64=base64.b64encode(hello).decode(), - records_b64=base64.b64encode(raw).decode(), - records=layout, - ) - if context is None: - emit(event | dict(status="captured-only")) - with contextlib.suppress(OSError): - stream.sendall(bytes.fromhex("15030300020228")) - continue - incoming, outgoing = ssl.MemoryBIO(), ssl.MemoryBIO() - tls = context.wrap_bio(incoming, outgoing, server_side=True) - incoming.write(raw) - try: - consumed = len(raw) - for _ in range(32): - try: - tls.do_handshake() - stream.sendall(outgoing.read()) - emit( - event - | dict( - status="tls-handshake-complete", - version=tls.version(), - cipher=tls.cipher()[0], - alpn=tls.selected_alpn_protocol(), - resumed=tls.session_reused, - ) - ) - break - except ssl.SSLWantReadError: - stream.sendall(outgoing.read()) - chunk = stream.recv(16384) - if not chunk: - raise EOFError() from None - consumed += len(chunk) - if consumed > 131072: - raise ValueError( - "TLS handshake input limit" - ) from None - incoming.write(chunk) - else: - raise ValueError("TLS handshake iteration limit") - except (OSError, EOFError, ValueError) as error: - with contextlib.suppress(OSError): - stream.sendall(outgoing.read()) - emit( - event - | dict( - status="tls-handshake-rejected", - reason=getattr( - error, "reason", type(error).__name__ - ), - ) - ) - except BaseException as error: - emit(dict(status="observer-error", reason=type(error).__name__)) - stop.set() - - workers = [ - threading.Thread(target=serve, args=(port, ctx)) - for port, ctx in contexts.items() - ] - print( - json.dumps(dict(openssl=ssl.OPENSSL_VERSION, ports=list(contexts))), flush=True - ) - for worker in workers: - worker.start() - for worker in workers: - worker.join() - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/container_udp_origin.py b/scripts/src/vcore_scripts/container_udp_origin.py deleted file mode 100644 index fae46ba..0000000 --- a/scripts/src/vcore_scripts/container_udp_origin.py +++ /dev/null @@ -1,287 +0,0 @@ -"""Bounded UDP echo origin, run only inside the owned fixture container. - -A TCP observer allocates one UDP association with a one-byte address family. -The response starts with its two-byte UDP port. Each real datagram is reported -as length/source-port (two network-order u16s), then the exact payload. Echoes -are sent by this container, never by the host test driver. -""" - -from __future__ import annotations - -import contextlib -import os -import select -import socket -import ssl -import struct -import sys -import threading -import time - -CAPACITY = 20000 -INTEGRATION_PROFILE = os.environ.get("VCORE_ORIGIN_PROFILE") == "INTEGRATION" -# Explicit, still bounded pressure profile. Historical protocol fixtures keep -# their original limits; no production queue or socket policy is changed. -SLOTS = threading.BoundedSemaphore(96 if INTEGRATION_PROFILE else 8) -COVER_SLOTS = threading.BoundedSemaphore(8) - - -def receive_exact(stream, size): - data = bytearray() - while len(data) < size: - chunk = stream.recv(min(65536, size - len(data))) - if not chunk: - raise ValueError("truncated fixture request") - data.extend(chunk) - return bytes(data) - - -def serve_tcp(control, mode, ipv6=False): - """10=bulk/server-first, 22=bulk/client-first, 11=close, 12=identity probe. - - Observer A proves an actual accept; D proves exact request bytes and normal - completion. Rejected identities must produce neither. No host-side server. - """ - with socket.socket( - socket.AF_INET6 if ipv6 else socket.AF_INET, socket.SOCK_STREAM - ) as listener: - if ipv6: - listener.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) - listener.bind(("::" if ipv6 else "0.0.0.0", 0)) - listener.listen(8) - control.sendall(struct.pack("!H", listener.getsockname()[1])) - for _ in range(32): - ready, _, _ = select.select([control, listener], [], [], 30) - if not ready or control in ready: - return - stream, _ = listener.accept() - control.sendall(b"A") - if mode == 21: - # Generate the withheld tail with the real TLS engine before - # transport EOF. This tests FIN semantics without depending on - # OpenSSL accepting an application write after a ragged EOF. - from tls_bio import TlsBio - - with stream: - stream.settimeout(15) - tls = TlsBio( - stream, tls_context(ssl.TLSVersion.TLSv1_3), server=True - ) - assert tls.tls.version() == "TLSv1.3" - control.sendall(b"\x13") - tls.sendall(b"hello") - data = receive_exact(tls, 65536) - if data != b"Z" * 65536: - raise ValueError("TLS direct close data") - tls.sendall(data) - tail = b"native-after-upload-eof" - assert tls.tls.write(tail) == len(tail) - if stream.recv(1): - raise ValueError("unexpected data after TLS direct request") - with contextlib.suppress(OSError): - tls.flush() - control.sendall(b"D") - continue - if mode in (16, 18): - context = tls_context( - ssl.TLSVersion.TLSv1_3 if mode == 16 else ssl.TLSVersion.TLSv1_2 - ) - stream.settimeout(15) - stream = context.wrap_socket(stream, server_side=True) - control.sendall(b"\x13" if stream.version() == "TLSv1.3" else b"\x12") - with stream: - stream.settimeout(15) - if mode == 13: # bounded-lifetime echo; client-first and cancellation - while data := stream.recv(65536): - stream.sendall(data) - elif mode == 14: # measureDelay/HTTP entrypoint, not a proxy decoder - request = bytearray() - while not request.endswith(b"\r\n\r\n"): - request.extend(receive_exact(stream, 1)) - if len(request) > 16384: - raise ValueError("HTTP origin header limit") - stream.sendall( - b"HTTP/1.1 200 OK\r\nConnection: close\r\n" - b"Content-Length: 0\r\n\r\n" - ) - elif mode == 15: # deliberately withhold all handshake replies - while stream.recv(65536): - pass - elif mode == 12: - if receive_exact(stream, 15) != b"synthetic-probe": - raise ValueError("identity probe data") - stream.sendall(b"ok") - else: - if mode != 22: - stream.sendall(b"hello") - if mode in (10, 16, 18, 22): - data = receive_exact(stream, 10 * 1024 * 1024) - if data != b"\x5a" * len(data): - raise ValueError("bulk data") - stream.sendall(data + b"trailer") - else: - data = receive_exact(stream, 4) - if data != b"ping": - raise ValueError("close probe data") - stream.sendall(data) - if stream.recv(1): - raise ValueError("unexpected data after request") - if mode == 11: - # Whole-close peers need not accept a tail. - with contextlib.suppress(OSError): - stream.sendall(b"native-after-upload-eof") - control.sendall(b"D") - - -def serve(control): - try: - with control: - control.settimeout(15) - control.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - family = control.recv(1) - if family and (family[0] & 0x7F) in (*range(10, 17), 18, 21, 22): - serve_tcp(control, family[0] & 0x7F, bool(family[0] & 0x80)) - return - if family not in (b"\x04", b"\x06", b"\x11", b"\x13", b"\x14"): - return - ipv6 = family == b"\x06" - with socket.socket( - socket.AF_INET6 if ipv6 else socket.AF_INET, socket.SOCK_DGRAM - ) as udp: - if ipv6: - udp.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) - udp.bind(("::" if ipv6 else "0.0.0.0", 0)) - control.sendall(struct.pack("!H", udp.getsockname()[1])) - peer_ipv4 = ( - socket.inet_ntop(socket.AF_INET, receive_exact(control, 4)) - if family == b"\x13" - else None - ) - seen = False - dns = family in (b"\x11", b"\x13") - dns_deadline = time.monotonic() + 240 - for _ in range(100000 if INTEGRATION_PROFILE else 20000): - wait = ( - min(30, max(0, dns_deadline - time.monotonic())) if dns else 30 - ) - if not wait: - return - ready, _, _ = select.select([control, udp], [], [], wait) - if control in ready: - return # EOF/cancel closes this association's UDP socket. - if not ready: - if dns: - # Runtime-owned DNS can be quiet during TCP or an - # IP-only UDP phase. Keep it until control closes, - # within the same bounded 240-second fixture lifetime. - continue - return - packet, source = udp.recvfrom(CAPACITY + 1) - if len(packet) > CAPACITY: - return - if family == b"\x14": - # A native origin blackhole, not a protocol peer. Report - # only first receipt; all handshake bytes are discarded. - if not seen: - control.sendall(b"A") - seen = True - continue - control.sendall(struct.pack("!HH", len(packet), source[1]) + packet) - response = ( - dns_response(packet, control.getsockname()[0], peer_ipv4) - if family in (b"\x11", b"\x13") - else packet - ) - if udp.sendto(response, source) != len(response): - return - except (OSError, ValueError): - # Client cancellation/failed association is recorded by the test driver. - pass - finally: - SLOTS.release() - - -def dns_response(packet, ipv4, peer_ipv4=None): - """Only the synthetic business name is answerable; never system DNS.""" - question = b"\x0dvcore-fixture\x04test\x00" - peer_question = b"\x04peer\x07fixture\x04test\x00" - if peer_ipv4 and packet[12 : 12 + len(peer_question)] == peer_question: - question, ipv4 = peer_question, peer_ipv4 - if ( - len(packet) < 12 + len(question) + 4 - or packet[12 : 12 + len(question)] != question - ): - raise ValueError("unexpected DNS question (including magic names)") - qtype = struct.unpack("!H", packet[-4:-2])[0] - answer = socket.inet_pton(socket.AF_INET, ipv4) if qtype == 1 else b"" - response = bytearray(packet) - response[2:4] = b"\x81\x80" - response[6:8] = struct.pack("!H", int(bool(answer))) - if answer: - response.extend(b"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04" + answer) - return bytes(response) - - -def tls_context(version): - context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) - context.minimum_version = version - context.maximum_version = version - context.set_ecdh_curve("X25519") - context.load_cert_chain( - os.environ["VCORE_ORIGIN_CERT"], os.environ["VCORE_ORIGIN_KEY"] - ) - context.set_alpn_protocols(["h2", "http/1.1"]) - context.num_tickets = 0 - return context - - -def serve_camouflage(stream, context): - try: - with stream: - stream.settimeout(5) - with context.wrap_socket(stream, server_side=True) as secured: - secured.recv(1) - except OSError: - pass - finally: - COVER_SLOTS.release() - - -def camouflage(): - """Container-only TLS 1.3 target for REALITY authentication.""" - context = tls_context(ssl.TLSVersion.TLSv1_3) - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener: - listener.bind(("0.0.0.0", 24001)) - listener.listen(8) - while True: - stream, _ = listener.accept() - # Independent XHTTP legs need concurrent cover handshakes. A quiet - # first leg must not hold the accept loop for its five-second bound. - if not COVER_SLOTS.acquire(blocking=False): - stream.close() - continue - threading.Thread( - target=serve_camouflage, args=(stream, context), daemon=True - ).start() - - -def main(): - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit( - "BLOCKED: this origin must be launched by the container harness" - ) - if os.environ.get("VCORE_ORIGIN_CERT"): - threading.Thread(target=camouflage, daemon=True).start() - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener: - listener.bind(("0.0.0.0", 24000)) - listener.listen(8) - while True: - control, _ = listener.accept() - if not SLOTS.acquire(blocking=False): - control.close() - continue - threading.Thread(target=serve, args=(control,), daemon=True).start() - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/container_uot_watch.py b/scripts/src/vcore_scripts/container_uot_watch.py deleted file mode 100644 index 7997cfb..0000000 --- a/scripts/src/vcore_scripts/container_uot_watch.py +++ /dev/null @@ -1,67 +0,0 @@ -"""Run an unmodified peer with a bounded UDP-side-channel observer in its guest. - -The official server still owns every TCP listener. These otherwise unused UDP -ports detect accidental native SS fallback; the observer never replies to UDP. -""" - -from __future__ import annotations - -import contextlib -import json -import os -import select -import socket -import subprocess -import sys -from pathlib import Path - - -def main(): - if sys.platform != "linux" or os.environ.get("VCORE_ISOLATED_ORIGIN") != "1": - raise SystemExit("isolated UoT peer required") - config = json.loads(Path("/data/fixture/config.json").read_text()) - native = "servers" in config - ports = [ - item["server_port" if native else "port"] - for item in config["servers" if native else "listeners"] - ] - if not 1 <= len(ports) <= 32 or len(ports) != len(set(ports)): - raise ValueError("bounded unique peer ports required") - with contextlib.ExitStack() as stack: - watchers = [] - for port in ports: - udp = stack.enter_context(socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)) - udp.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) - udp.bind(("::", port)) - watchers.append(udp) - observer = stack.enter_context(socket.socket()) - observer.bind(("0.0.0.0", 23998)) - observer.listen(4) - counts = dict(ports=ports, native_udp_packets=0) - with subprocess.Popen(sys.argv[1:]) as peer: - try: - while peer.poll() is None: - ready, _, _ = select.select([*watchers, observer], [], [], 0.1) - # Drain UDP before answering a snapshot in the same cycle. - for udp in watchers: - if udp in ready: - udp.recvfrom(65536) - counts["native_udp_packets"] += 1 - if observer in ready: - client, _ = observer.accept() - with client: - client.settimeout(1) - if client.recv(1) == b"?": - client.sendall(json.dumps(counts).encode()) - raise RuntimeError("official UoT peer exited") - finally: - peer.terminate() - try: - peer.wait(timeout=3) - except subprocess.TimeoutExpired: - peer.kill() - peer.wait(timeout=3) - - -if __name__ == "__main__": - main() diff --git a/scripts/src/vcore_scripts/core_checks.py b/scripts/src/vcore_scripts/core_checks.py deleted file mode 100644 index 31211f2..0000000 --- a/scripts/src/vcore_scripts/core_checks.py +++ /dev/null @@ -1,189 +0,0 @@ -"""Memory-only checks shared by local development and CI. - -Network consumers remain explicit container suites. Keep this allowlist independent -of historical milestone runners: compiling all targets is safe; executing them is not. -""" - -from __future__ import annotations - -import re - -from .builds import CORE_DIR, DEFAULT_FEATURES -from .protocol_peers import run_command - -FEATURE_TEST = ( - "runtime::tests::integration_feature_admission_is_explicit_without_opening_sockets" -) -PROTOCOLS = ( - "socks5", - "anytls", - "shadowsocks", - "trojan", - "vmess", - "vless", - "hysteria2", - "tuic", -) -TARGETS = ( - "ech_config", - "ech_tls", - "encryption_config", - "reality_config", - "jls_config", - "security_capabilities", - "retired_security", - "feature_foundations", - "limit_foundations", - "trojan", - "trojan_config", - "vmess_config", - "vless_config", - "vless_lifecycle", - "xhttp_config", - "xhttp_budget", - "h2_stream_regression", - "stream_foundations", - "stream_shutdown", - "xhttp_h3_shutdown", - "hysteria2_paths", - "hysteria2_config", - "shadowsocks_backpressure", - "shadowtls_config", - "shadowtls_stream", - "uot_config", - "shadowsocks_uot", - "hysteria2_packet_ids", - "tuic_config", - "tuic_memory", - "httpupgrade_config", - "httpupgrade_memory", - "vmess_lifecycle", -) -LIB_FILTERS = ( - "security::", - "outbound::vless::encryption", - "config::", - "outbound::hysteria2::", - "outbound::shadowsocks::tests::official_tcp_", - "outbound::anytls::", - "outbound::uot::", - "outbound::tuic::", - "transport::h2_write::tests::", - "transport::sing_mux::smux_driver::tests::", - "transport::sing_mux::tests::", -) -EXACT_LIB_TESTS = ( - "outbound::connector::tests::authenticated_continuation_keeps_group_choice_but_has_a_new_io_deadline", - FEATURE_TEST, - "ffi::tests::runtime_thread_cannot_reenter_invoke", - "ffi::tests::null_invalid_utf8_and_oversized_input_return_json_failures", - "ffi::tests::same_instance_command_is_fail_fast", - "ffi::tests::android_protector_is_required_only_for_tun", -) - - -def commands(profile: str) -> list[list[str]]: - cargo = ["cargo", "test", "--locked", "--all-features"] - if profile in {"debug", "release"}: - if profile == "release": - cargo += ["--release"] - return [ - cargo + [arg for target in TARGETS for arg in ("--test", target)], - *(cargo + ["--lib", name] for name in LIB_FILTERS), - *(cargo + ["--lib", name, "--", "--exact"] for name in EXACT_LIB_TESTS), - cargo - + [ - "--test", - "quic_datagram_foundations", - "pending_send_is_not_restarted_and_stop_cancels_without_waiting_for_writable", - "--", - "--exact", - ], - ] - if profile == "features": - features = ["outbound-" + protocol for protocol in PROTOCOLS] - return ( - [ - ["cargo", "check", "--locked", "--no-default-features", "--lib"] - + (["--features", name] if name else []) - for name in [ - "", - *features, - "stream-transport", - "quic-transport", - "shadow-tls-v3", - "tun", - "ffi", - ] - ] - + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - name, - "--lib", - FEATURE_TEST, - "--", - "--exact", - ] - for name in features - ] - + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--test", - "feature_foundations", - "--test", - "hysteria2_config", - ], - ["cargo", "check", "--locked", "--lib"], - cargo + ["--all-targets", "--no-run"], - [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ], - ] - ) - raise ValueError("unknown core check profile") - - -def validate_execution( - command: list[str], output: str, returncode: int, cleanup: bool -) -> None: - if returncode != 0 or not cleanup: - raise RuntimeError("core check failed, timed out, or did not join its process") - if command[:2] != ["cargo", "test"] or "--no-run" in command: - return - summaries = re.findall( - r"test result: ok\. (\d+) passed; (\d+) failed; \d+ ignored;", output - ) - expected = max(1, command.count("--test")) - if len(summaries) != expected or any( - int(passed) == 0 or int(failed) for passed, failed in summaries - ): - raise RuntimeError( - "core check selected no tests or returned incomplete test results" - ) - - -def run(profile: str, *, list_only: bool = False) -> None: - for command in commands(profile): - print(" ".join(command), flush=True) - if list_only: - continue - result = run_command(command, cwd=CORE_DIR, timeout=3600, limit=4 * 1024 * 1024) - output = result.stdout.decode("utf-8", errors="replace") - print(output, end="", flush=True) - validate_execution(command, output, result.returncode, result.cleanup) diff --git a/scripts/src/vcore_scripts/mihomo.py b/scripts/src/vcore_scripts/mihomo.py deleted file mode 100644 index 96ce408..0000000 --- a/scripts/src/vcore_scripts/mihomo.py +++ /dev/null @@ -1,347 +0,0 @@ -"""Local, opt-in interoperability; this never changes system proxy or TUN state.""" - -from __future__ import annotations - -import base64 -import hashlib -import json -import os -import socket -import ssl -import subprocess -import tempfile -import time -from contextlib import ExitStack -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import reserve_port - - -def _stop_peer(peer: subprocess.Popen) -> None: - if peer.poll() is None: - peer.terminate() - try: - peer.wait(timeout=5) - except subprocess.TimeoutExpired: - peer.kill() - peer.wait(timeout=5) - - -def _wait_ready(peer, port: int, host: str = "127.0.0.1") -> None: - deadline = time.monotonic() + 10 - while time.monotonic() < deadline: - if peer.poll() is not None: - raise RuntimeError("mihomo exited before its local listener was ready") - try: - with socket.create_connection((host, port), timeout=0.2): - return - except OSError: - time.sleep(0.05) - raise RuntimeError("mihomo did not become ready within 10 seconds") - - -def _run_mihomo_interop( - binary: Path, - *, - extended: bool, - soak_seconds: int, - container_binary: Path | None = None, -) -> None: - version = subprocess.run( - [binary, "-v"], check=True, text=True, capture_output=True, timeout=5 - ) - with binary.open("rb") as artifact: - digest = hashlib.file_digest(artifact, "sha256").hexdigest() - print(version.stdout.strip(), flush=True) - print(f"mihomo binary SHA-256: {digest}", flush=True) - print( - "Local interop limits: readiness 10s; socket I/O 5s; " - f"cargo run {300 + soak_seconds}s; peer stop 5s", - flush=True, - ) - with ( - tempfile.TemporaryDirectory(prefix="vcore-mihomo-") as temporary, - ExitStack() as stack, - ): - directory = Path(temporary) - containers = None - if container_binary is not None: - from .mihomo_container import ContainerPeers - - containers = ContainerPeers(directory, stack, container_binary.resolve()) - reservations = [reserve_port(stack) for _ in range(10)] - ( - upstream_port, - downstream_port, - socks_downstream_port, - core_port, - socks_port, - anytls_port, - *ss_ports, - ss_health_port, - ) = [port for port, _ in reservations] - ss_fixtures = [ - { - "cipher": name, - "port": port, - "password": base64.b64encode(bytes([7]) * key_length).decode("ascii"), - } - for (name, key_length), port in zip( - [ - ("2022-blake3-aes-128-gcm", 16), - ("2022-blake3-aes-256-gcm", 32), - ("2022-blake3-chacha20-poly1305", 32), - ], - ss_ports, - strict=True, - ) - ] - # mihomo intentionally rejects certificate paths outside its dataDir. - certificate_directory = directory / "0" - certificate_directory.mkdir(exist_ok=True) - certificate = certificate_directory / "fixture.crt" - private_key = certificate_directory / "fixture.key" - subprocess.run( - [ - "openssl", - "req", - "-x509", - "-newkey", - "rsa:2048", - "-nodes", - "-keyout", - private_key, - "-out", - certificate, - "-days", - "2", - "-subj", - "/CN=fixture.invalid", - "-addext", - "subjectAltName=DNS:fixture.invalid", - ], - check=True, - capture_output=True, - timeout=10, - ) - fingerprint = hashlib.sha256( - ssl.PEM_cert_to_DER_cert(certificate.read_text(encoding="ascii")) - ).hexdigest() - for _, reservation in reservations[5:]: - reservation.release_ipv4() - base = { - "allow-lan": False, - "bind-address": "127.0.0.1", - "ipv6": True, - "mode": "rule", - "log-level": "warning", - "skip-auth-prefixes": [], - "dns": {"enable": False}, - "hosts": { - "vcore-fixture.test": "127.0.0.1", - "vcore-peer.test": "127.0.0.1", - }, - "profile": {"store-selected": False}, - } - configs = [ - { - **base, - "mixed-port": upstream_port, - "authentication": ["fixture:password"], - "listeners": [ - { - "name": "anytls-fixture", - "type": "anytls", - "listen": "127.0.0.1", - "port": anytls_port, - "users": {"fixture": "password"}, - "certificate": str(certificate), - "private-key": str(private_key), - }, - ], - "rules": ["MATCH,DIRECT"], - }, - { - **base, - "mixed-port": downstream_port, - "proxies": [ - { - "name": "vcore", - "type": "http", - "server": "127.0.0.1", - "port": core_port, - "username": "fixture", - "password": "password", - } - ], - "rules": ["MATCH,vcore"], - }, - { - **base, - "mixed-port": socks_downstream_port, - "proxies": [ - { - "name": "vcore", - "type": "socks5", - "server": "127.0.0.1", - "port": socks_port, - "username": "fixture", - "password": "password", - "udp": True, - } - ], - "rules": ["MATCH,vcore"], - }, - { - # Use a separate process for the second hop: mihomo correctly - # rejects a connection re-entering its own loopback detector. - **base, - "mixed-port": ss_health_port, - "listeners": [ - { - "name": f"ss-fixture-{index}", - "type": "shadowsocks", - "listen": "127.0.0.1", - "udp": True, - **fixture, - } - for index, fixture in enumerate(ss_fixtures) - ], - "rules": ["MATCH,DIRECT"], - }, - ] - extended_environment = {} - if extended: - from .mihomo_extended import prepare_extended - - extended_environment = prepare_extended( - directory, - stack, - configs, - ss_fixtures, - certificate, - private_key, - host_address=containers.host if containers else "127.0.0.1", - peer_hosts=containers.addresses if containers else None, - ) - peer_ids = [] - for index, config in enumerate(configs): - peer_directory = directory / str(index) - peer_directory.mkdir(exist_ok=True) - config_path = peer_directory / "config.yaml" - if containers: - containers.configure(index, config) - # JSON is a YAML subset; fixtures never contain real user credentials. - staging_path = peer_directory / "config.pending" - staging_path.write_text(json.dumps(config), encoding="utf-8") - staging_path.replace(config_path) - log = stack.enter_context((peer_directory / "peer.log").open("wb")) - reservations[[0, 1, 2, 9][index]][1].release_ipv4() - if containers and index in containers.peers: - peer = containers.peers[index] - else: - peer = subprocess.Popen( - [binary, "-d", peer_directory, "-f", config_path], - stdout=log, - stderr=subprocess.STDOUT, - ) - peer_ids.append(str(peer.pid)) - stack.callback(_stop_peer, peer) - peer_host = containers.addresses[index] if containers else "127.0.0.1" - try: - _wait_ready(peer, config["mixed-port"], peer_host) - if "external-controller" in config: - _wait_ready( - peer, - int(config["external-controller"].rsplit(":", 1)[1]), - peer_host, - ) - if index == 0: - _wait_ready(peer, anytls_port, peer_host) - if index == 3: - for ss_port in ss_ports: - _wait_ready(peer, ss_port, peer_host) - except RuntimeError: - # Only this harness's generated fixtures can reach this path. - print( - containers.peers[index].logs() - if containers and index in containers.peers - else (peer_directory / "peer.log").read_text(errors="replace")[ - -4096: - ], - flush=True, - ) - raise - # VCore itself binds both families. Its reservations must fully release; - # the IPv4-only external peers retain their IPv6 guards until exit. - for _, reservation in reservations[3:5]: - reservation.close() - environment = dict( - os.environ, - VCORE_MIHOMO_UPSTREAM=str(upstream_port), - VCORE_MIHOMO_DOWNSTREAM=str(downstream_port), - VCORE_MIHOMO_HTTP_PORT=str(core_port), - VCORE_MIHOMO_SOCKS_PORT=str(socks_port), - VCORE_MIHOMO_SOCKS_DOWNSTREAM=str(socks_downstream_port), - VCORE_MIHOMO_ANYTLS_PORT=str(anytls_port), - VCORE_MIHOMO_ANYTLS_PIN=fingerprint, - VCORE_MIHOMO_SS_FIXTURES=json.dumps(ss_fixtures), - VCORE_MIHOMO_SOAK_SECONDS=str(soak_seconds), - VCORE_MIHOMO_PEER_PIDS=",".join(peer_ids), - **extended_environment, - ) - if containers: - environment.update(containers.environment()) - try: - command = [ - "cargo", - "test", - "--locked", - "--features", - "ffi,interop-test" if os.environ.get("VCORE_CASE_EVENTS") else "ffi", - "--test", - "mihomo_interop", - "public_client_inbounds_interoperate_with_mihomo_in_both_directions", - "--", - "--exact", - "--ignored", - "--nocapture", - "--test-threads=1", - ] - if os.environ.get("VCORE_CASE_EVENTS"): - from .protocol_peers import run_command - - completed = run_command( - command, - cwd=CORE_DIR, - env=environment, - timeout=300 + soak_seconds, - limit=4 * 1024 * 1024, - ) - print(completed.stdout.decode("utf-8", errors="replace"), flush=True) - if completed.returncode != 0 or not completed.cleanup: - raise subprocess.CalledProcessError( - completed.returncode or 1, command - ) - else: - subprocess.run( - command, - cwd=CORE_DIR, - env=environment, - check=True, - timeout=300 + soak_seconds, - ) - except (subprocess.CalledProcessError, subprocess.TimeoutExpired): - for index in range(len(configs)): - print(f"Generated fixture peer {index} diagnostics (last 4096 bytes):") - print( - containers.peers[index].logs() - if containers and index in containers.peers - else (directory / str(index) / "peer.log") - .read_bytes()[-4096:] - .decode("utf-8", errors="replace"), - flush=True, - ) - raise - print("mihomo peers stopped; temporary configurations removed", flush=True) diff --git a/scripts/src/vcore_scripts/mihomo_container.py b/scripts/src/vcore_scripts/mihomo_container.py deleted file mode 100644 index f5e2ad2..0000000 --- a/scripts/src/vcore_scripts/mihomo_container.py +++ /dev/null @@ -1,230 +0,0 @@ -"""Apple Container peers on an explicitly owned, host-only network. - -The two upstream/terminal peers get separate Linux kernels. Reverse-direction -HTTP/SOCKS fixtures remain native so VCore's loopback-only listeners stay private. -No host forwarding, system DNS, proxy, TUN, or third-party changes are needed. -""" - -from __future__ import annotations - -import hashlib -import ipaddress -import json -import re -import shutil -import socket -import subprocess -import time -import uuid -from contextlib import ExitStack -from pathlib import Path - -NETWORK = "vcore-mihomo-interop" -IMAGE = ( - "docker.io/library/alpine@sha256:" - "fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40" -) - - -def command(*arguments: str, timeout: int = 30) -> str: - try: - return subprocess.run( - ["container", *arguments], - check=True, - capture_output=True, - text=True, - timeout=timeout, - ).stdout - except subprocess.CalledProcessError as error: - raise RuntimeError( - f"container {arguments[0]} failed: {error.stderr[-2048:].strip()}" - ) from error - - -def host_ipv6(text: str, gateway: str, subnet: str) -> str | None: - """Find the ULA on the same bridge as our gateway, never a LAN/utun address.""" - network = ipaddress.IPv6Network(subnet) - for interface in re.split(r"(?=^\S+:[ ]flags=)", text, flags=re.MULTILINE): - if not re.search(rf"\binet {re.escape(gateway)}\s", interface): - continue - for line in interface.splitlines(): - if re.search(r"\b(tentative|duplicated|deprecated|detached)\b", line): - continue - match = re.search(r"\binet6 ([0-9a-fA-F:]+) prefixlen", line) - if match and ipaddress.IPv6Address(match[1]) in network: - return match[1] - return None - - -class ContainerPeer: - def __init__(self, name: str) -> None: - self.name = name - - def poll(self) -> int | None: - state = json.loads(command("inspect", self.name))[0]["status"]["state"] - return None if state == "running" else 1 - - def stop(self) -> None: - # Only this invocation's unguessable names, never a prune or system stop. - owned = next( - ( - item - for item in json.loads(command("list", "--all", "--format", "json")) - if item["id"] == self.name - ), - None, - ) - if owned is None: - return - if owned["configuration"].get("labels", {}).get("purpose") != NETWORK: - raise RuntimeError( - "refusing to clean a container without fixture ownership" - ) - if owned["status"]["state"] == "running": - command("stop", "--time", "5", self.name, timeout=15) - command("delete", "--force", self.name) - - def logs(self) -> str: - return command("logs", self.name)[-4096:] - - -class ContainerPeers: - def __init__(self, directory: Path, stack: ExitStack, binary: Path) -> None: - if not binary.is_file(): - raise RuntimeError("Linux ARM64 mihomo binary missing (NOT RUN)") - network = json.loads(command("network", "inspect", NETWORK))[0] - if ( - network["configuration"]["mode"] != "hostOnly" - or network["configuration"]["labels"].get("purpose") != NETWORK - ): - raise RuntimeError("mihomo requires its labelled host-only network") - # An explicit setup step pulls the pinned image; test runs do not update it. - command("image", "inspect", IMAGE) - self.host = network["status"]["ipv4Gateway"] - self.addresses = ["127.0.0.1"] * 4 - self.peers: dict[int, ContainerPeer] = {} - prefix = f"vcore-mihomo-{uuid.uuid4().hex[:12]}" - with binary.open("rb") as artifact: - digest = hashlib.file_digest(artifact, "sha256").hexdigest() - print(f"Linux mihomo SHA-256: {digest}; image: {IMAGE}", flush=True) - for index in (0, 3): - fixture = directory / str(index) - fixture.mkdir(exist_ok=True) - shutil.copyfile(binary, fixture / "mihomo") - (fixture / "mihomo").chmod(0o755) - name = f"{prefix}-{index}" - peer = ContainerPeer(name) - # Register before launch: a failed/timed-out CLI can still create a VM. - stack.callback(peer.stop) - # Boot before writing config so we can discover the allocated IPs. - command( - "run", - "--detach", - "--name", - name, - "--label", - f"purpose={NETWORK}", - "--network", - NETWORK, - "--cpus", - "1", - "--arch", - "arm64", - "--memory", - "256M", - "--read-only", - "--no-dns", - "--tmpfs", - "/data", - "--mount", - f"type=bind,source={fixture},target=/data/fixture,readonly", - "--entrypoint", - "/bin/sh", - IMAGE, - "-c", - "while [ ! -f /data/fixture/config.yaml ]; do sleep 0.05; done; " - "exec /data/fixture/mihomo -d /data -f /data/fixture/config.yaml", - timeout=45, - ) - self.peers[index] = peer - if index == 0: - print( - command("exec", name, "/data/fixture/mihomo", "-v").strip(), - flush=True, - ) - state = json.loads(command("inspect", name))[0] - address = state["status"]["networks"][0]["ipv4Address"].split("/")[0] - if ipaddress.IPv4Address(address) not in ipaddress.IPv4Network( - network["status"]["ipv4Subnet"] - ): - raise RuntimeError("unexpected container network address") - self.addresses[index] = address - # SLAAC/DAD can finish shortly after the first VM boots. - deadline = time.monotonic() + 15 - while time.monotonic() < deadline: - interfaces = subprocess.run( - ["/sbin/ifconfig", "-a"], - check=True, - text=True, - capture_output=True, - timeout=5, - ).stdout - address = host_ipv6(interfaces, self.host, network["status"]["ipv6Subnet"]) - if address: - try: - with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as probe: - probe.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) - probe.bind((address, 0)) - # Container IPv6 also uses SLAAC/DAD. A readiness probe is - # not a retry of business traffic; tests have not started. - for peer in self.peers.values(): - command( - "exec", - peer.name, - "ping", - "-6", - "-c", - "1", - "-W", - "1", - address, - timeout=5, - ) - except (OSError, RuntimeError, subprocess.TimeoutExpired): - pass - else: - self.host_v6 = address - break - time.sleep(0.1) - else: - raise RuntimeError("host-only bridge IPv6 did not become ready (NOT RUN)") - print( - f"Apple Container upstreams: {self.addresses[0]}, {self.addresses[3]}; " - f"origins: {self.host}, {self.host_v6}; " - "reverse inbound peers: native loopback", - flush=True, - ) - - def configure(self, index: int, config: dict) -> None: - config["hosts"] = { - "vcore-fixture.test": self.host, - "vcore-peer.test": self.addresses[3], - } - if index not in self.peers: - return - config.update({"allow-lan": True, "bind-address": "0.0.0.0"}) - if "external-controller" in config: - port = config["external-controller"].rsplit(":", 1)[1] - config["external-controller"] = f"0.0.0.0:{port}" - for listener in config.get("listeners", []): - listener["listen"] = "0.0.0.0" - for key in ("certificate", "private-key"): - if key in listener: - listener[key] = f"/data/fixture/{Path(listener[key]).name}" - - def environment(self) -> dict[str, str]: - return { - "VCORE_MIHOMO_PEER_HOSTS": json.dumps(self.addresses), - "VCORE_MIHOMO_ORIGIN_V4": self.host, - "VCORE_MIHOMO_ORIGIN_V6": self.host_v6, - } diff --git a/scripts/src/vcore_scripts/mihomo_extended.py b/scripts/src/vcore_scripts/mihomo_extended.py deleted file mode 100644 index 406679c..0000000 --- a/scripts/src/vcore_scripts/mihomo_extended.py +++ /dev/null @@ -1,175 +0,0 @@ -"""Extra loopback-only peers for the explicit cross-protocol acceptance run.""" - -from __future__ import annotations - -import json -import os -import shutil -import subprocess -from contextlib import ExitStack -from pathlib import Path - -PRIVATE_KEY = "eNc1RW_wzi_qpuGZFwV-d6end6xbUyvVuKDrCpz5Z0Q" -PUBLIC_KEY = "TrotdL9Y_dMWo-eqNe5dGfx7AbY1vNJjuEvRs4WR2y4" -UUID = "b831381d-6324-4d53-ad4f-8cda48b30811" -SHORT_ID = "0123456789abcdef" - - -def prepare_extended( - directory: Path, - stack: ExitStack, - configs: list[dict], - ss_fixtures: list[dict], - certificate: Path, - private_key: Path, - *, - host_address: str = "127.0.0.1", - peer_hosts: list[str] | None = None, -) -> dict[str, str]: - from .mihomo import _stop_peer, _wait_ready, reserve_port - - openssl = next( - ( - candidate - for candidate in [ - os.environ.get("OPENSSL_BIN"), - "/opt/homebrew/opt/openssl@3/bin/openssl", - "/usr/local/opt/openssl@3/bin/openssl", - shutil.which("openssl3"), - ] - if candidate and Path(candidate).is_file() - ), - None, - ) - if openssl is None: - raise RuntimeError("OpenSSL 3 is required for the local REALITY camouflage") - reserved = [reserve_port(stack) for _ in range(7)] - ( - first_ss, - first_vless, - last_anytls, - last_vless, - decoy_port, - control_first, - control_last, - ) = [port for port, _ in reserved] - reserved[4][1].release_ipv4() - decoy_log = stack.enter_context((directory / "camouflage.log").open("wb")) - decoy = subprocess.Popen( - [ - openssl, - "s_server", - "-accept", - f"{host_address}:{decoy_port}", - "-cert", - str(certificate), - "-key", - str(private_key), - "-tls1_3", - "-groups", - "X25519", - "-alpn", - "h2,http/1.1", - "-quiet", - "-ign_eof", - ], - stdin=subprocess.DEVNULL, - stdout=decoy_log, - stderr=subprocess.STDOUT, - ) - stack.callback(_stop_peer, decoy) - _wait_ready(decoy, decoy_port, host_address) - last_directory = directory / "3" - last_directory.mkdir(exist_ok=True) - last_cert, last_key = last_directory / "fixture.crt", last_directory / "fixture.key" - shutil.copyfile(certificate, last_cert) - shutil.copyfile(private_key, last_key) - configs[3]["listeners"].append( - { - "name": "anytls-last", - "type": "anytls", - "listen": "127.0.0.1", - "port": last_anytls, - "users": {"fixture": "password"}, - "certificate": str(last_cert), - "private-key": str(last_key), - } - ) - configs[0]["listeners"].append( - { - **ss_fixtures[0], - "name": "ss-first", - "type": "shadowsocks", - "listen": "127.0.0.1", - "port": first_ss, - "udp": True, - } - ) - for index, port in [(0, first_vless), (3, last_vless)]: - configs[index]["listeners"].append( - { - "name": "vless-fixture", - "type": "vless", - "listen": "127.0.0.1", - "port": port, - "users": [{"username": "fixture", "uuid": UUID}], - "xhttp-config": {"path": "/onev", "mode": "auto"}, - "reality-config": { - "dest": f"{host_address}:{decoy_port}", - "private-key": PRIVATE_KEY, - "short-id": [SHORT_ID], - "server-names": ["fixture.invalid"], - }, - } - ) - - for index, port in [(0, control_first), (3, control_last)]: - configs[index]["external-controller"] = f"127.0.0.1:{port}" - configs[index]["secret"] = "fixture-controller-only" - - def nodes( - index: int, anytls_port: int, ss_port: int, vless_port: int - ) -> list[dict]: - common = { - "server": peer_hosts[index] if peer_hosts else "127.0.0.1", - "udp": True, - } - socks = {**common, "type": "socks5", "port": configs[index]["mixed-port"]} - if index == 0: - socks.update(username="fixture", password="password") - return [ - socks, - { - **common, - "type": "anytls", - "port": anytls_port, - "password": "password", - "sni": "fixture.invalid", - "skip-cert-verify": True, - }, - {**ss_fixtures[0], **common, "type": "ss", "port": ss_port}, - { - **common, - "type": "vless", - "port": vless_port, - "uuid": UUID, - "network": "xhttp", - "tls": True, - "servername": "fixture.invalid", - "reality-opts": {"public-key": PUBLIC_KEY, "short-id": SHORT_ID}, - "xhttp-opts": { - "host": "fixture.invalid", - "path": "/onev", - "mode": "stream-one", - }, - }, - ] - - value = { - "first": nodes(0, configs[0]["listeners"][0]["port"], first_ss, first_vless), - "last": nodes(3, last_anytls, ss_fixtures[0]["port"], last_vless), - "controllers": [control_first, control_last], - } - for _, sock in reserved: - sock.release_ipv4() - return {"VCORE_MIHOMO_CHAIN_FIXTURES": json.dumps(value)} diff --git a/scripts/src/vcore_scripts/mihomo_isolation.py b/scripts/src/vcore_scripts/mihomo_isolation.py deleted file mode 100644 index 91dba04..0000000 --- a/scripts/src/vcore_scripts/mihomo_isolation.py +++ /dev/null @@ -1,91 +0,0 @@ -"""Owned fixture ports and a cross-process, fail-fast harness lock.""" - -from __future__ import annotations - -import errno -import os -import socket -import tempfile -from contextlib import ExitStack, contextmanager -from pathlib import Path - - -class PortReservation: - def __init__(self) -> None: - self.ipv4 = ExitStack() - self.ipv6 = ExitStack() - - def release_ipv4(self) -> None: - # IPv4-only peers take these ports; IPv6 guards remain owned until exit. - self.ipv4.close() - - def release_ipv6(self) -> None: - self.ipv6.close() - - def close(self) -> None: - self.ipv4.close() - self.ipv6.close() - - -def reserve_port(stack: ExitStack) -> tuple[int, PortReservation]: - """Reserve TCP/UDP in both loopback families, before starting any traffic.""" - for _ in range(32): - reservation = PortReservation() - try: - tcp = reservation.ipv4.enter_context(socket.socket()) - tcp.bind(("127.0.0.1", 0)) - port = tcp.getsockname()[1] - udp = reservation.ipv4.enter_context(socket.socket(type=socket.SOCK_DGRAM)) - udp.bind(("127.0.0.1", port)) - for kind in (socket.SOCK_STREAM, socket.SOCK_DGRAM): - guard = reservation.ipv6.enter_context( - socket.socket(socket.AF_INET6, kind) - ) - guard.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) - guard.bind(("::1", port)) - except OSError as error: - reservation.close() - if error.errno == errno.EADDRINUSE: - continue - raise - stack.callback(reservation.close) - return port, reservation - raise RuntimeError("could not reserve a dual-family loopback TCP/UDP port") - - -@contextmanager -def exclusive_run(path: Path | None = None): - # Per-user temporary directory on macOS; uid also separates shared /tmp. - # Never unlink a lock file: doing so could create two lockable inodes. - suffix = f"-{os.getuid()}" if hasattr(os, "getuid") else "" - path = path or Path(tempfile.gettempdir()) / f"vcore-mihomo-interop{suffix}.lock" - flags = os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0) - with os.fdopen(os.open(path, flags, 0o600), "r+b", buffering=0) as lock: - # Windows byte-range locks need an existing byte. This is our lock file, - # not a PID file or user configuration, and is never truncated. - if os.fstat(lock.fileno()).st_size == 0: - lock.write(b"\0") - lock.seek(0) - try: - if os.name == "nt": - import msvcrt - - msvcrt.locking(lock.fileno(), msvcrt.LK_NBLCK, 1) - else: - import fcntl - - fcntl.flock(lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) - except OSError as error: - if error.errno not in (errno.EACCES, errno.EAGAIN, errno.EDEADLK): - raise - raise RuntimeError( - "another local mihomo harness is running; wait for that run to finish" - ) from error - try: - yield - finally: - if os.name == "nt": - lock.seek(0) - msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1) - else: - fcntl.flock(lock.fileno(), fcntl.LOCK_UN) diff --git a/scripts/src/vcore_scripts/mihomo_release.py b/scripts/src/vcore_scripts/mihomo_release.py deleted file mode 100644 index 92ea78b..0000000 --- a/scripts/src/vcore_scripts/mihomo_release.py +++ /dev/null @@ -1,231 +0,0 @@ -"""Download the latest official Mihomo release for interop tests without an API.""" - -from __future__ import annotations - -import gzip -import hashlib -import http.client -import io -import os -import platform -import re -import stat -import tempfile -import time -import urllib.parse -import urllib.request -import zipfile -import zlib -from contextlib import contextmanager -from pathlib import Path, PurePosixPath - -from .builds import CORE_DIR - -LATEST_VERSION_URL = ( - "https://github.com/MetaCubeX/mihomo/releases/latest/download/version.txt" -) -RELEASE_URL = "https://github.com/MetaCubeX/mihomo/releases/download" -SUPPORTED_TARGETS = ( - "darwin-arm64", - "darwin-amd64", - "linux-arm64", - "linux-amd64", - "windows-arm64", - "windows-amd64", -) -MAX_VERSION_BYTES = 128 -MAX_ARCHIVE_BYTES = 128 * 1024 * 1024 -MAX_BINARY_BYTES = 256 * 1024 * 1024 -CHUNK_BYTES = 64 * 1024 -TRANSFER_SECONDS = 180 - - -def _target(target: str | None) -> str: - if target is None: - system = platform.system().lower() - machine = platform.machine().lower() - architecture = { - "arm64": "arm64", - "aarch64": "arm64", - "amd64": "amd64", - "x86_64": "amd64", - }.get(machine, machine) - target = f"{system}-{architecture}" - if target not in SUPPORTED_TARGETS: - raise RuntimeError(f"unsupported official mihomo target: {target}") - return target - - -def _release_tag(tag: str) -> str: - if ( - not isinstance(tag, str) - or re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]{0,127}", tag) is None - ): - raise RuntimeError("invalid official mihomo download tag") - return tag - - -def _copy_and_hash(source, output, limit: int) -> str: - digest = hashlib.sha256() - total = 0 - deadline = time.monotonic() + TRANSFER_SECONDS - # read1 avoids waiting for a complete chunk before checking the deadline. - read = getattr(source, "read1", source.read) - while True: - if time.monotonic() > deadline: - raise RuntimeError("official mihomo transfer exceeded its time limit") - chunk = read(min(CHUNK_BYTES, limit - total + 1)) - if not chunk: - break - total += len(chunk) - if total > limit: - raise RuntimeError("official mihomo payload exceeded its size limit") - digest.update(chunk) - output.write(chunk) - if not total: - raise RuntimeError("official mihomo payload is empty") - return digest.hexdigest() - - -def _https_response(response) -> None: - if urllib.parse.urlsplit(response.geturl()).scheme != "https": - raise RuntimeError("official mihomo download redirected outside HTTPS") - - -def latest_release() -> str: - """Read the official latest/version.txt asset only to form download filenames.""" - request = urllib.request.Request( - LATEST_VERSION_URL, headers={"User-Agent": "VCore-interop-scripts"} - ) - try: - with urllib.request.urlopen(request, timeout=30) as response: - _https_response(response) - contents = io.BytesIO() - _copy_and_hash(response, contents, MAX_VERSION_BYTES) - tag = contents.getvalue().decode("utf-8").strip() - except (OSError, ValueError, http.client.HTTPException) as error: - raise RuntimeError( - f"cannot read official mihomo latest version.txt: {error}" - ) from error - return _release_tag(tag) - - -def _asset(tag: str, target: str) -> tuple[str, str]: - variant = "-v1" if target in {"linux-amd64", "windows-amd64"} else "" - suffix = "zip" if target.startswith("windows-") else "gz" - name = f"mihomo-{target}{variant}-{tag}.{suffix}" - return name, f"{RELEASE_URL}/{tag}/{name}" - - -@contextmanager -def _payload(archive: Path): - if archive.suffix == ".gz": - with gzip.open(archive, "rb") as payload: - yield payload - return - with zipfile.ZipFile(archive) as bundle: - files = [] - for member in bundle.infolist(): - name = member.filename - if ( - PurePosixPath(name).is_absolute() - or ".." in PurePosixPath(name).parts - or "\\" in name - or ":" in name - ): - raise RuntimeError("unsafe path in official mihomo ZIP") - if member.is_dir(): - continue - mode = stat.S_IFMT(member.external_attr >> 16) - if ( - mode not in {0, stat.S_IFREG} - or member.flag_bits & 1 - or len(PurePosixPath(name).parts) != 1 - or not name.endswith(".exe") - ): - raise RuntimeError("unsafe executable entry in official mihomo ZIP") - files.append(member) - if len(files) != 1: - raise RuntimeError( - "official mihomo ZIP must contain exactly one executable" - ) - if not 0 < files[0].file_size <= MAX_BINARY_BYTES: - raise RuntimeError("official mihomo executable exceeds its size limit") - with bundle.open(files[0]) as payload: - yield payload - - -def _report( - tag: str, asset: str, url: str, archive_digest: str, digest: str, binary: Path -): - print(f"Mihomo download release tag: {tag}", flush=True) - print(f"Official mihomo asset: {asset}", flush=True) - print(f"Official mihomo download URL: {url}", flush=True) - print(f"Mihomo archive SHA-256 (downloaded content): {archive_digest}", flush=True) - print(f"Mihomo binary SHA-256 (downloaded content): {digest}", flush=True) - print(f"Official mihomo binary: {binary}", flush=True) - - -def download_mihomo( - target: str | None = None, - *, - release: str | None = None, - directory: Path | None = None, - identity: dict | None = None, -) -> Path: - """Download a fresh official archive and atomically publish its binary. - - Callers can share a freshly read version.txt tag for multiple architectures. - Every call downloads again; failures neither replace nor return an old binary. - Hashes identify downloaded content, without claiming an upstream digest check. - The harness records the actual version by running the binary with -v. - """ - target = _target(target) - if release is None: - release = latest_release() - tag = _release_tag(release) - name, url = _asset(tag, target) - directory = ( - directory - if directory is not None - else CORE_DIR / "target/interop/mihomo" / tag / target - ) - binary = directory / ("mihomo.exe" if target.startswith("windows-") else "mihomo") - request = urllib.request.Request( - url, headers={"User-Agent": "VCore-interop-scripts"} - ) - try: - directory.mkdir(parents=True, exist_ok=True) - with tempfile.TemporaryDirectory(prefix=".download-", dir=directory) as staging: - staged_archive = Path(staging) / name - staged_binary = Path(staging) / binary.name - with ( - urllib.request.urlopen(request, timeout=30) as response, - staged_archive.open("wb") as output, - ): - _https_response(response) - archive_digest = _copy_and_hash(response, output, MAX_ARCHIVE_BYTES) - with _payload(staged_archive) as source, staged_binary.open("wb") as output: - digest = _copy_and_hash(source, output, MAX_BINARY_BYTES) - staged_binary.chmod(0o755) - os.replace(staged_binary, binary) - except ( - OSError, - EOFError, - http.client.HTTPException, - zipfile.BadZipFile, - zlib.error, - ) as error: - raise RuntimeError(f"cannot download official mihomo {tag}: {error}") from error - _report(tag, name, url, archive_digest, digest, binary) - if identity is not None: - identity.update( - kind="M", - target=target, - release=tag, - asset=name, - source_url=url, - archive_sha256=archive_digest, - binary_sha256=digest, - ) - return binary diff --git a/scripts/src/vcore_scripts/native_release.py b/scripts/src/vcore_scripts/native_release.py deleted file mode 100644 index b838af5..0000000 --- a/scripts/src/vcore_scripts/native_release.py +++ /dev/null @@ -1,234 +0,0 @@ -"""Fresh official native peers. No API lookup, source build or stale fallback.""" - -from __future__ import annotations - -import http.client -import os -import platform -import re -import stat -import subprocess -import tarfile -import tempfile -import urllib.request -import zipfile -from dataclasses import dataclass -from pathlib import Path, PurePosixPath - -from .mihomo_release import ( - MAX_ARCHIVE_BYTES, - MAX_BINARY_BYTES, - _copy_and_hash, - _https_response, -) -from .protocol_peers import run_command - - -@dataclass(frozen=True) -class PeerArtifact: - binary: Path - identity: dict - - -def download_native( - kind: str, - directory: Path, - target: str | None = None, - *, - defer_version: bool = False, -) -> PeerArtifact: - try: - return _download_native(kind, directory, target, defer_version) - except ( - OSError, - ValueError, - zipfile.BadZipFile, - tarfile.TarError, - http.client.HTTPException, - subprocess.SubprocessError, - ): - raise RuntimeError( - "official native peer download or version check failed" - ) from None - - -def _download_native( - kind: str, directory: Path, target: str | None, defer_version: bool -) -> PeerArtifact: - """Install only a named executable into this run's owned directory.""" - if target is None: - architecture = {"aarch64": "arm64", "x86_64": "amd64"}.get( - platform.machine().lower(), platform.machine().lower() - ) - target = f"{platform.system().lower()}-{architecture}" - assets = { - "ST": ( - "ihciah/shadow-tls", - "shadow-tls", - {"linux-arm64": "shadow-tls-aarch64-unknown-linux-musl"}, - ), - "SS": ( - "shadowsocks/shadowsocks-rust", - "ssserver", - {"linux-arm64": "aarch64-unknown-linux-musl"}, - ), - "V2": ( - "v2fly/v2ray-core", - "v2ray", - { - "darwin-arm64": "v2ray-macos-arm64-v8a.zip", - "darwin-amd64": "v2ray-macos-64.zip", - "linux-arm64": "v2ray-linux-arm64-v8a.zip", - "linux-amd64": "v2ray-linux-64.zip", - }, - ), - "XR": ( - "XTLS/Xray-core", - "xray", - { - "darwin-arm64": "Xray-macos-arm64-v8a.zip", - "darwin-amd64": "Xray-macos-64.zip", - "linux-arm64": "Xray-linux-arm64-v8a.zip", - "linux-amd64": "Xray-linux-64.zip", - }, - ), - "H": ( - "HyNetworks/hysteria", - "hysteria", - { - target: f"hysteria-{target}" - for target in [ - "darwin-arm64", - "darwin-amd64", - "linux-arm64", - "linux-amd64", - ] - }, - ), - } - if kind not in assets or target not in assets[kind][2]: - raise RuntimeError("unsupported official native peer target") - project, name, targets = assets[kind] - asset = targets[target] - if kind == "SS": - # SS assets include their tag; follow latest's HTTP redirect, not an API - # or a pinned version. The executable reports its own actual version. - request = urllib.request.Request( - f"https://github.com/{project}/releases/latest", - method="HEAD", - headers={"User-Agent": "VCore-interop-scripts"}, - ) - with urllib.request.urlopen(request, timeout=30) as response: - prefix = f"https://github.com/{project}/releases/tag/" - resolved = response.geturl() - if not resolved.startswith(prefix) or not re.fullmatch( - r"v\d+\.\d+\.\d+", resolved[len(prefix) :] - ): - raise RuntimeError("invalid official SS latest stable redirect") - tag = resolved[len(prefix) :] - asset = f"shadowsocks-{tag}.{asset}.tar.xz" - url = f"https://github.com/{project}/releases/latest/download/{asset}" - directory.mkdir(parents=True, exist_ok=True) - binary = directory / name - with tempfile.TemporaryDirectory(prefix=".download-", dir=directory) as temporary: - archive, executable = Path(temporary) / "archive.zip", Path(temporary) / name - request = urllib.request.Request( - url, headers={"User-Agent": "VCore-interop-scripts"} - ) - with ( - urllib.request.urlopen(request, timeout=30) as response, - archive.open("wb") as output, - ): - _https_response(response) - archive_hash = _copy_and_hash(response, output, MAX_ARCHIVE_BYTES) - if kind in {"H", "ST"}: - with archive.open("rb") as source, executable.open("wb") as output: - binary_hash = _copy_and_hash(source, output, MAX_BINARY_BYTES) - elif kind == "SS": - binary_hash = _extract_tar(archive, executable, name) - else: - binary_hash = _extract_zip(archive, executable, name) - executable.chmod(0o755) - # Foreign executables are verified inside the owned VM before traffic; - # a deferred artifact is not yet a ready/verified peer. - version = None - if not defer_version: - result = run_command( - [str(executable), "--version" if kind in {"SS", "ST"} else "version"], - timeout=10, - limit=4096, - ) - if result.returncode != 0 or not result.cleanup: - raise RuntimeError("official native peer version check failed") - version = result.stdout.decode("utf-8", errors="replace").strip() - if not version: - raise RuntimeError("invalid official native peer version output") - os.replace(executable, binary) - return PeerArtifact( - binary, - { - "kind": kind, - "target": target, - "source_url": url, - "archive_sha256": archive_hash, - "binary_sha256": binary_hash, - "version": version, - }, - ) - - -def _extract_zip(archive: Path, executable: Path, name: str) -> str: - with zipfile.ZipFile(archive) as bundle: - matches = [] - for member in bundle.infolist(): - path = PurePosixPath(member.filename) - mode = stat.S_IFMT(member.external_attr >> 16) - if ( - path.is_absolute() - or ".." in path.parts - or "\\" in member.filename - or ":" in member.filename - or mode not in {0, stat.S_IFREG, stat.S_IFDIR} - or member.flag_bits & 1 - ): - raise RuntimeError("unsafe official native peer archive") - if member.filename == name: - matches.append(member) - if len(matches) != 1 or not 0 < matches[0].file_size <= MAX_BINARY_BYTES: - raise RuntimeError("invalid official native peer executable") - with bundle.open(matches[0]) as source, executable.open("wb") as output: - binary_hash = _copy_and_hash(source, output, MAX_BINARY_BYTES) - return binary_hash - - -def _extract_tar(archive: Path, executable: Path, name: str) -> str: - """Stream a bounded official bundle; never extract archive paths or links.""" - found, total, digest = 0, 0, None - with tarfile.open(archive, "r|xz") as bundle: - for index, entry in enumerate(bundle): - path = PurePosixPath(entry.name) - total += entry.size - if ( - index >= 32 - or total > MAX_BINARY_BYTES - or path.is_absolute() - or ".." in path.parts - or "\\" in entry.name - or ":" in entry.name - or not (entry.isfile() or entry.isdir()) - ): - raise RuntimeError("unsafe official native peer archive") - if entry.name != name: - continue - found += 1 - if ( - found != 1 - or not entry.isfile() - or not 0 < entry.size <= MAX_BINARY_BYTES - ): - raise RuntimeError("invalid official native peer executable") - with bundle.extractfile(entry) as source, executable.open("wb") as output: - digest = _copy_and_hash(source, output, MAX_BINARY_BYTES) - if found != 1: - raise RuntimeError("missing official native peer executable") - return digest diff --git a/scripts/src/vcore_scripts/platform_delivery.py b/scripts/src/vcore_scripts/platform_delivery.py index 5df4fc0..bd28ec6 100644 --- a/scripts/src/vcore_scripts/platform_delivery.py +++ b/scripts/src/vcore_scripts/platform_delivery.py @@ -10,6 +10,7 @@ import plistlib import shutil import stat import subprocess +import tomllib from datetime import UTC, datetime from pathlib import Path, PurePosixPath @@ -18,8 +19,10 @@ from . import builds GROUPS = {"apple", "android", "windows-arm64", "windows-x64"} APPLE_LIBRARIES = { "ios-arm64": ("ios", None, {"arm64"}), - "ios-arm64_x86_64-simulator": ("ios", "simulator", {"arm64", "x86_64"}), + "ios-arm64-simulator": ("ios", "simulator", {"arm64"}), "macos-arm64_x86_64": ("macos", None, {"arm64", "x86_64"}), + "tvos-arm64": ("tvos", None, {"arm64"}), + "tvos-arm64-simulator": ("tvos", "simulator", {"arm64"}), } @@ -35,17 +38,36 @@ def _output(argv: list[str], root: Path) -> str: def _source(root: Path) -> dict: if _output(["git", "status", "--porcelain", "--untracked-files=normal"], root): raise ValueError("delivery requires a clean committed source checkout") - return { + source = { "commit": _output(["git", "rev-parse", "HEAD"], root), "tree": _output(["git", "rev-parse", "HEAD^{tree}"], root), "lockSha256": _sha(root / "Cargo.lock"), } + # Local development uses the sibling fork. A lock hash alone cannot identify + # its code; PR/release builds switch back to the locked Git release branch. + manifest = root / "Cargo.toml" + if manifest.exists(): + dependency = tomllib.loads(manifest.read_text())["dependencies"].get( + "boring", {} + ) + if "path" in dependency: + fork = (root / dependency["path"]).resolve() + if _output( + ["git", "status", "--porcelain", "--untracked-files=normal"], fork + ): + raise ValueError( + "artifact evidence requires a clean local boring checkout" + ) + source["localBoring"] = { + "commit": _output(["git", "rev-parse", "HEAD"], fork), + "tree": _output(["git", "rev-parse", "HEAD^{tree}"], fork), + } + return source -def check_delivery( - manifests: list[Path], *, source_dir: Path | None = None, complete: bool = False -) -> None: - source = _source(source_dir or builds.CORE_DIR) +def _check_delivery(manifests: list[Path]) -> None: + """Check newly built files before retaining their delivery manifest.""" + source = _source(builds.CORE_DIR) groups = set() if not manifests: raise ValueError("empty manifest set cannot pass") @@ -68,7 +90,17 @@ def check_delivery( required = {"rustc", "cargo"} | ( {"ndk", "clang", "androidApi"} if group == "android" - else {"xcode", "iphoneos", "iphonesimulator", "macosx"} + else { + "xcode", + "iphoneos", + "iphonesimulator", + "macosx", + "appletvos", + "appletvsimulator", + "iosDeploymentTarget", + "macosDeploymentTarget", + "tvosDeploymentTarget", + } if group == "apple" else {"msvc", "windowsSdk"} ) @@ -148,7 +180,7 @@ def check_delivery( "rb" ) as stream: libraries = plistlib.load(stream).get("AvailableLibraries", []) - if len(libraries) != 3: + if len(libraries) != len(APPLE_LIBRARIES): raise ValueError("incomplete Apple platform slices") identifiers = set() for library in libraries: @@ -168,13 +200,15 @@ def check_delivery( path = ( manifest.parent / "LibVCore.xcframework" / identifier / "libvcore.a" ) - actual_archs = set( - _output( - ["xcrun", "lipo", "-archs", str(path)], builds.CORE_DIR - ).split() + minimum = toolchain[target_os + "DeploymentTarget"] + if target_os == "tvos" and tuple(map(int, minimum.split("."))) < ( + 17, + 0, + ): + raise ValueError("tvOS artifact must target 17.0 or newer") + builds.check_apple_binary( + path, target_os, variant, architectures, minimum ) - if actual_archs != architectures: - raise ValueError("wrong Apple binary architecture") builds._require_identity(path, "Apple") else: arch = group.removeprefix("windows-") @@ -214,16 +248,6 @@ def check_delivery( print( f"PASS {record['group']} artifact integrity (not device/release acceptance)" ) - if complete and groups != GROUPS: - raise ValueError( - "complete platform artifacts require Apple, Android, " - "native Windows ARM64 and x64" - ) - if complete: - print( - "PASS production platform artifacts; " - "ABI, devices and release require separate evidence" - ) def build_delivery(platform_name: str) -> None: @@ -284,7 +308,13 @@ def build_delivery(platform_name: str) -> None: toolchain["assembly"] = "enabled" elif platform_name == "apple": toolchain["xcode"] = _output(["xcodebuild", "-version"], builds.CORE_DIR) - for sdk in ("iphoneos", "iphonesimulator", "macosx"): + for sdk in ( + "iphoneos", + "iphonesimulator", + "macosx", + "appletvos", + "appletvsimulator", + ): toolchain[sdk] = _output( ["xcrun", "--sdk", sdk, "--show-sdk-version"], builds.CORE_DIR ) @@ -294,18 +324,9 @@ def build_delivery(platform_name: str) -> None: toolchain["macosDeploymentTarget"] = os.environ.get( "VCORE_MACOS_DEPLOYMENT_TARGET", "10.15" ) + toolchain["tvosDeploymentTarget"] = builds.tvos_deployment_target() else: - android_home = Path( - os.environ.get("ANDROID_HOME", Path.home() / "Library/Android/sdk") - ) - ndk = Path( - os.environ.get( - "ANDROID_NDK_HOME", - android_home - / "ndk" - / os.environ.get("VCORE_ANDROID_NDK_VERSION", "28.2.13676358"), - ) - ) + ndk = builds._android_ndk_home() toolchain["ndk"] = (ndk / "source.properties").read_text().strip() toolchain["clang"] = _output( [str(builds._android_toolchain(ndk) / "bin/clang"), "--version"], @@ -377,119 +398,8 @@ def build_delivery(platform_name: str) -> None: json.dumps(record, indent=2, sort_keys=True) + "\n", encoding="utf-8" ) try: - check_delivery([manifest]) + _check_delivery([manifest]) except Exception: manifest.unlink(missing_ok=True) raise print(manifest) - - -def check_abi(manifest: Path) -> None: - """Compile a real C consumer; no replacement library and no protocol servers.""" - # Commands below run from an isolated output directory, not the caller cwd. - manifest = manifest.resolve() - check_delivery([manifest]) - record = json.loads(manifest.read_text(encoding="utf-8")) - group = record["group"] - root = builds.CORE_DIR - work = root / "target/platform-delivery/abi" / group - work.mkdir(parents=True, exist_ok=True) - (work / "result.json").unlink(missing_ok=True) - source = root / "scripts/fixtures/platform_abi.c" - binary = work / ("abi.exe" if os.name == "nt" else "abi") - environment = os.environ.copy() - if group == "apple" and platform.system() == "Darwin": - library = manifest.parent / "LibVCore.xcframework/macos-arm64_x86_64/libvcore.a" - command = [ - "xcrun", - "clang", - "-Wall", - "-Wextra", - "-Werror", - "-I", - str(root / "include"), - str(source), - str(library), - "-lc++", - "-lresolv", - "-framework", - "Security", - "-framework", - "SystemConfiguration", - "-framework", - "CoreFoundation", - "-o", - str(binary), - ] - execute = [str(binary)] - architecture = platform.machine() - elif group.startswith("windows-") and os.name == "nt": - architecture = builds._windows_architecture() - if group != "windows-" + architecture: - raise ValueError("ABI check cannot substitute emulation for native Windows") - environment = builds._windows_msvc_environment(architecture) - search_path = next( - value for key, value in environment.items() if key.upper() == "PATH" - ) - # CreateProcess does not use env[PATH] to resolve the executable. Resolve - # cl from vcvars explicitly rather than requiring it in the parent PATH. - compiler = shutil.which("cl", path=search_path) - if not compiler: - raise ValueError( - "MSVC C compiler is unavailable in the selected environment" - ) - command = [ - compiler, - "/nologo", - "/std:c11", - "/W4", - "/WX", - "/MT", - "/I" + str(root / "include"), - str(source), - "/Fe:" + str(binary), - "/Fo:" + str(work / "abi.obj"), - ] - execute = [str(binary), str(manifest.parent / "vcore.dll")] - else: - raise ValueError( - "native ABI runner requires a matching macOS or Windows artifact" - ) - subprocess.run(command, cwd=work, env=environment, check=True, timeout=120) - subprocess.run(execute, cwd=work, env=environment, check=True, timeout=60) - if group.startswith("windows-"): - target = {"arm64": "aarch64-pc-windows-msvc", "x64": "x86_64-pc-windows-msvc"}[ - architecture - ] - base = [ - "cargo", - "test", - "--locked", - "--release", - "--target", - target, - "--features", - "ffi", - ] - for suffix in ( - ["--lib", "windows::snapshot::tests"], - ["--test", "windows_session_startup"], - ): - subprocess.run( - base + suffix, cwd=root, env=environment, check=True, timeout=1200 - ) - if _source(root) != record["source"]: - raise ValueError("source changed during ABI check") - evidence = { - "kind": "native-production-abi", - "group": group, - "source": record["source"], - "manifestSha256": _sha(manifest), - "architecture": architecture, - "iterations": 1000, - "invalidApiRejected": True, - "finishedUtc": datetime.now(UTC).isoformat(), - } - (work / "result.json").write_text( - json.dumps(evidence, indent=2) + "\n", encoding="utf-8" - ) diff --git a/scripts/src/vcore_scripts/protocol_catalogs.py b/scripts/src/vcore_scripts/protocol_catalogs.py deleted file mode 100644 index 247731f..0000000 --- a/scripts/src/vcore_scripts/protocol_catalogs.py +++ /dev/null @@ -1,88 +0,0 @@ -"""Executable catalog integrity; production fields are documented in config.yaml.""" - -from __future__ import annotations - -import json -from pathlib import Path -from typing import Any - -CATALOG_DIR = Path(__file__).resolve().parents[3] / "tests" / "protocols" -PROTOCOLS = {"trojan", "vmess", "vless", "hysteria2"} -PEERS = {"M", "H", "XR", "V2"} -SUITES = { - "FOUNDATIONS": ( - "schema", - "security", - "streams", - "datagrams", - "features", - "regression", - ), - "TROJAN": ("codec", "configuration", "cancellation", "acceptance"), - "VMESS": ("identity", "codec", "udp", "close", "acceptance"), - "VLESS": ("tcp", "transports", "vision", "acceptance"), - "XHTTP": ("requests", "download", "mux", "http3", "acceptance"), - "HYSTERIA2": ("tcp", "udp", "bandwidth", "salamander", "hopping", "acceptance"), - "SECURITY": ("encryption", "reality", "ech", "jls", "acceptance"), - "INTEGRATION": ("routing", "lifecycle", "pressure", "regression"), - "SHADOWTLS": ("configuration", "stream", "interop", "regression"), - "UOT": ("configuration", "datagrams", "interop", "regression"), - "TUIC": ("configuration", "datagrams", "interop", "regression"), - "HTTPUPGRADE": ("configuration", "stream", "interop", "regression"), -} -CATEGORIES = { - f"{suite}.{category}" - for suite, categories in SUITES.items() - for category in categories -} -# Stable evidence tags; field semantics live in docs/config.yaml, not a planning mirror. -FIELD_IDS = { - f"{prefix}{index:02}" - for prefix, count in { - "C": 6, - "T": 8, - "W": 7, - "G": 6, - "H": 6, - "TR": 2, - "VM": 8, - "VL": 6, - "X": 29, - "D": 32, - "S": 13, - "M": 7, - "HY": 8, - }.items() - for index in range(1, count + 1) - if not ( - (prefix == "D" and 19 <= index <= 24) or (prefix == "S" and 6 <= index <= 11) - ) -} - - -def _unique_object(pairs: list[tuple[str, Any]]) -> dict[str, Any]: - result: dict[str, Any] = {} - for key, value in pairs: - if key in result: - raise ValueError("protocol catalog: duplicate JSON key") - result[key] = value - return result - - -def check_protocol_catalogs(directory: Path = CATALOG_DIR) -> None: - from .protocol_evidence import check_limit_references, load_manifest - - cases = load_manifest() - check_limit_references(cases, directory / "limits.json") - print( - json.dumps( - { - "kind": "executable-catalog-check", - "status": "VALID", - "behavior_status": "NOT RUN", - "cases": len(cases), - "fields": len(set().union(*(set(case["row_ids"]) for case in cases))), - }, - sort_keys=True, - ) - ) diff --git a/scripts/src/vcore_scripts/protocol_completion_peers.py b/scripts/src/vcore_scripts/protocol_completion_peers.py deleted file mode 100644 index fd69510..0000000 --- a/scripts/src/vcore_scripts/protocol_completion_peers.py +++ /dev/null @@ -1,366 +0,0 @@ -"""Official peer capability fixtures, never VCore protocol acceptance.""" - -from __future__ import annotations - -import base64 -import contextlib -import json -import shutil -import tempfile -from dataclasses import dataclass -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_containers import ContainerLab, frozen_image -from .protocol_fixtures import certificates -from .protocol_hysteria2_hop import guest -from .protocol_inputs import redact, same_source, source_identity - - -@dataclass(frozen=True) -class PeerCase: - identifier: str - protocol: str - cipher: str = "" - shadow_tls: bool = False - uot: bool = False - tls: bool = False - fast_open: bool = False - udp_mode: str = "" - - -CASES = ( - tuple( - PeerCase( - f"ss-{algorithm}-{mode}", - "ss", - f"2022-blake3-{algorithm}", - shadow_tls=shadow_tls, - uot=uot, - ) - for algorithm in ("aes-128-gcm", "aes-256-gcm", "chacha20-poly1305") - for mode, shadow_tls, uot in ( - ("shadowtls", True, False), - ("uot-v2", False, True), - ("shadowtls-uot-v2", True, True), - ) - ) - + tuple( - PeerCase(f"tuic-v5-{mode}", "tuic", tls=True, udp_mode=mode) - for mode in ("native", "quic") - ) - + tuple( - PeerCase( - f"{protocol}-upgrade-{'tls' if tls else 'plain'}" - f"-{'fast' if fast else 'normal'}", - protocol, - tls=tls, - fast_open=fast, - ) - for protocol, tls in (("vmess", False), ("vmess", True), ("trojan", True)) - for fast in (False, True) - ) - + (PeerCase("socks5-upstream", "socks5"),) -) - - -def peer_configuration(case, *, server, cover, port, certificate): - """Build matched Mihomo endpoints with explicit latest-only wire choices.""" - cert, key, pin = certificate - if case.protocol != "ss": - password = "synthetic-protocol-fixture" - identity = "07070707-0707-0707-0707-070707070707" - listener = dict( - name=case.identifier, type=case.protocol, listen="::", port=port - ) - node = dict( - name=case.identifier, type=case.protocol, server=server, port=port, udp=True - ) - if case.tls: - listener.update( - certificate=f"/data/fixture/{cert.name}", - **{"private-key": f"/data/fixture/{key.name}"}, - ) - node.update(fingerprint=pin) - if case.protocol == "socks5": - listener.update(type="socks", udp=True) - elif case.protocol == "tuic": - listener.update(users={identity: password}, alpn=["h3"]) - node.update(uuid=identity, password=password, sni="localhost", alpn=["h3"]) - node["udp-relay-mode"] = case.udp_mode - else: - if case.protocol == "vmess": - listener["users"] = [dict(username="fixture", uuid=identity, alterId=0)] - node.update(uuid=identity, alterId=0, cipher="auto", tls=case.tls) - node["packet-encoding"] = "xudp" - if case.tls: - node["servername"] = "localhost" - elif case.protocol == "trojan": - listener["users"] = [dict(username="fixture", password=password)] - node.update(password=password, sni="localhost") - else: - raise ValueError("unsupported peer capability") - listener["ws-path"] = "/protocol-upgrade" - node["network"] = "ws" - node["ws-opts"] = { - "path": "/protocol-upgrade", - "headers": {"Host": "localhost"}, - "v2ray-http-upgrade": True, - "v2ray-http-upgrade-fast-open": case.fast_open, - } - return node, listener - password = base64.b64encode( - bytes([7]) * (16 if case.cipher == "2022-blake3-aes-128-gcm" else 32) - ).decode() - listener = dict( - name=case.identifier, - type="shadowsocks", - listen="::", - port=port, - cipher=case.cipher, - password=password, - udp=not case.uot, - ) - node = dict( - name=case.identifier, - type="ss", - server=server, - port=port, - cipher=case.cipher, - password=password, - udp=True, - ) - if case.uot: - node.update({"udp-over-tcp": True, "udp-over-tcp-version": 2}) - if case.shadow_tls: - credential = "synthetic-shadowtls-fixture" - listener["shadow-tls"] = { - "enable": True, - "version": 3, - "users": [dict(name="fixture", password=credential)], - "handshake": dict(dest=cover), - "strict-mode": True, - } - node["plugin"] = "shadow-tls" - node["plugin-opts"] = dict( - host="localhost", password=credential, version=3, fingerprint=pin - ) - return node, listener - - -def run(output, *, identifiers=None): - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("peer evidence must stay inside target/interop/runs") - selected = ( - CASES - if identifiers is None - else tuple(case for case in CASES if case.identifier in identifiers) - ) - if not selected or ( - identifiers is not None - and ( - len(identifiers) != len(set(identifiers)) - or len(selected) != len(identifiers) - ) - ): - raise ValueError("unknown or repeated peer capability selection") - report = dict( - scope="official-peer-capability-only", - vcore_behavior="NOT RUN", - status="NOT RUN", - source=source_identity(), - peer={}, - isolation={}, - selected=[case.identifier for case in selected], - cases=[], - cleanup=False, - ) - output.mkdir(parents=True, exist_ok=False) - try: - with exclusive_run(), frozen_image(output / "image-pull.log") as image: - report["container_image"] = image - binary = download_mihomo( - "linux-arm64", directory=output / "binary", identity=report["peer"] - ) - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory( - prefix="private-", dir=output - ) as temporary: - root = Path(temporary) - directories, peers = {}, {} - try: - with contextlib.ExitStack() as stack: - for role in ("origin", "server", "client"): - directory = root / role - directory.mkdir() - directories[role] = directory - if role == "origin": - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - directory / "origin.py", - ) - argv = [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "VCORE_ORIGIN_CERT=/data/fixture/cert.pem", - "VCORE_ORIGIN_KEY=/data/fixture/key.pem", - "python", - "-B", - "/data/fixture/origin.py", - ] - else: - shutil.copy2(binary, directory / "mihomo") - argv = [ - "/data/fixture/mihomo", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - peers[role] = lab.start(stack, directory, role, argv) - origin, server, client = ( - peers[r] for r in ("origin", "server", "client") - ) - report["peer"]["version"] = guest( - server, "/data/fixture/mihomo", "-v" - ).strip() - for peer in (server, client): - if ( - guest( - peer, "sha256sum", "/data/fixture/mihomo" - ).split()[0] - != report["peer"]["binary_sha256"] - ): - raise RuntimeError( - "container peer differs from official download" - ) - certificate = certificates(directories["server"]) - for path, name in zip( - certificate[:2], ("cert.pem", "key.pem"), strict=True - ): - shutil.copy2(path, directories["origin"] / name) - nodes, listeners, ingresses, options = [], [], [], [] - for index, case in enumerate(selected): - node, listener = peer_configuration( - case, - server=server.ipv4, - cover=f"{origin.ipv4}:24001", - port=23000 + index, - certificate=certificate, - ) - nodes.append(node) - listeners.append(listener) - ingresses.append( - dict( - name=case.identifier, - type="socks", - listen=client.ipv4, - port=25000 + index, - udp=True, - proxy=case.identifier, - ) - ) - options.append( - dict( - case_id=case.identifier, - socks_host=client.ipv4, - socks_port=25000 + index, - ) - ) - server_config = { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "listeners": listeners, - "rules": ["MATCH,DIRECT"], - } - client_config = { - "ipv6": True, - "log-level": "warning", - "proxies": nodes, - "listeners": ingresses, - "rules": ["MATCH,REJECT"], - } - for role, configuration in ( - ("server", server_config), - ("client", client_config), - ): - (directories[role] / "config.json").write_text( - json.dumps(configuration) - ) - shutil.copy2( - Path(__file__).with_name("container_protocol_client.py"), - directories["client"] / "probe.py", - ) - (directories["client"] / "fixture.json").write_text( - json.dumps(dict(origin=origin.ipv4, cases=options)) - ) - for peer in peers.values(): - peer.release() - origin.wait_tcp(24000) - origin.wait_tcp(24001) - server.wait_tcp(23999) - client.wait_tcp(25000) - raw = guest( - client, - "env", - "VCORE_ISOLATED_CLIENT=1", - "python", - "-B", - "/data/fixture/probe.py", - "/data/fixture/fixture.json", - timeout=30 * len(selected), - ) - (output / "observations.jsonl").write_text(raw) - rows = [ - json.loads(line) - for line in raw.splitlines() - if line.strip() - ] - expected = [ - dict( - case_id=case.identifier, - status="PASS", - tcp_bytes_each_direction=1024, - udp_origin_packets=2, - udp_reply_packets=2, - udp_sizes=[64, 1200], - ) - for case in selected - ] - if rows != expected: - raise RuntimeError( - "official peer data evidence is incomplete" - ) - report["cases"] = rows - finally: - # A failed start may already own a VM/log before returning - # its peer. ExitStack has joined it; preserve that log too. - for role, directory in directories.items(): - log = directory / "peer.log" - if log.is_file(): - (output / f"{role}.log").write_text( - redact(log.read_text(errors="replace")) - ) - report["status"] = "PASS" - except BaseException as error: - report["status"] = "FAIL" - report["failure_kind"] = type(error).__name__ - (output / "failure.log").write_text(redact(str(error))) - raise - finally: - report["cleanup"] = all( - peer.get("joined") is True for peer in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - if report["status"] != "PASS": - raise RuntimeError("peer capability ownership or source evidence failed") - print(f"Official peer capabilities: {len(selected)} PASS; VCore behavior: NOT RUN") diff --git a/scripts/src/vcore_scripts/protocol_containers.py b/scripts/src/vcore_scripts/protocol_containers.py deleted file mode 100644 index 97d21e6..0000000 --- a/scripts/src/vcore_scripts/protocol_containers.py +++ /dev/null @@ -1,234 +0,0 @@ -"""Owned Apple Container test peers; no native-server fallback or host publishing.""" - -from __future__ import annotations - -import hashlib -import ipaddress -import json -import re -import socket -import time -import uuid -from contextlib import contextmanager -from contextvars import ContextVar -from pathlib import Path - -from .protocol_inputs import redact -from .protocol_peers import OwnedProcess, run_command - -NETWORK = "vcore-mihomo-interop" -IMAGE = "docker.io/library/python:3-alpine" -_FROZEN_IMAGE = ContextVar("vcore_frozen_container_image", default=None) - - -def _pull_image(log=None): - argv = ["container", "image", "pull", IMAGE] - result = run_command(argv, timeout=180) - if log is not None: - log.write_text(redact(result.stdout.decode(errors="replace"))) - if result.returncode != 0 or not result.cleanup: - raise RuntimeError("isolated container image pull failed") - inspection = json.loads(command("image", "inspect", IMAGE))[0] - digest = inspection["configuration"]["descriptor"]["digest"] - if not isinstance(digest, str) or not re.fullmatch(r"sha256:[0-9a-f]{64}", digest): - raise RuntimeError("invalid isolated container image identity") - return dict( - tag=IMAGE, - digest=digest, - command=argv, - exit_code=0, - cleanup=True, - log=log.name if log is not None else None, - ) - - -@contextmanager -def frozen_image(log): - """Refresh once per owned run, never a persistent/offline fallback cache.""" - if _FROZEN_IMAGE.get() is not None: - raise RuntimeError("container image scope is already frozen") - snapshot = _pull_image(log) - token = _FROZEN_IMAGE.set(snapshot["digest"]) - try: - yield snapshot - finally: - _FROZEN_IMAGE.reset(token) - - -def command(*args, timeout=30): - result = run_command(["container", *args], timeout=timeout) - if result.returncode != 0 or not result.cleanup: - # CLI output may contain fixture paths/config; retain a bounded operation only. - raise RuntimeError(f"isolated container operation failed: {args[0]}") - return result.stdout.decode(errors="replace") - - -def listing(): - return json.loads(command("list", "--all", "--format", "json")) - - -class ContainerLab: - def __init__(self, record, *, mtu=1280): - if type(mtu) is not int or mtu not in (1280, 1500): - raise ValueError("unsupported isolated guest MTU") - self.mtu = mtu - self.record = record - self.run_id = uuid.uuid4().hex[:12] - network = json.loads(command("network", "inspect", NETWORK))[0] - config = network["configuration"] - if ( - config["mode"] != "hostOnly" - or config.get("labels", {}).get("purpose") != NETWORK - ): - raise RuntimeError("owned host-only container network required") - self.v4 = ipaddress.ip_network(network["status"]["ipv4Subnet"]) - self.v6 = ipaddress.ip_network(network["status"]["ipv6Subnet"]) - digest = _FROZEN_IMAGE.get() - if digest is None: - digest = _pull_image()["digest"] - self.image = IMAGE.split(":")[0] + "@" + digest - record.update( - backend="Apple Container", - network_mode="hostOnly", - image_tag=IMAGE, - image_digest=digest, - run_id=self.run_id, - peers=[], - host_servers=False, - guest_mtu=mtu, - ) - - def start(self, stack, root: Path, role, argv, *, net_admin=False): - peer = ContainerPeer(self, root, role) - self.record["peers"].append(peer.record) - # Register before launching: CLI interruption/timeout can leave a live VM. - stack.callback(peer.stop) - peer.start(argv, net_admin=net_admin) - return peer - - -class ContainerPeer: - def __init__(self, lab, root, role): - self.lab, self.root = lab, root - self.name = f"vcore-interop-{lab.run_id}-{role}" - if len(self.name) > 63: - digest = hashlib.sha256(self.name.encode()).hexdigest()[:8] - self.name = self.name[:54] + "-" + digest - self.record = dict(role=role, name=self.name, joined=False, started=False) - self.capture = None - self.log = root / "peer.log" - - def start(self, argv, *, net_admin=False): - if type(net_admin) is not bool: - raise ValueError("isolated capability choice must be boolean") - self.record["net_admin"] = net_admin - command( - "run", - "--detach", - "--name", - self.name, - "--label", - f"purpose={NETWORK}", - "--label", - f"vcore-run={self.lab.run_id}", - "--network", - f"{NETWORK},mtu={self.lab.mtu}", - "--cpus", - "1", - "--memory", - "256M", - "--arch", - "arm64", - *(["--cap-add", "CAP_NET_ADMIN"] if net_admin else ["--read-only"]), - "--no-dns", - "--tmpfs", - "/data", - "--mount", - f"type=bind,source={self.root},target=/data/fixture,readonly", - "--entrypoint", - "/bin/sh", - self.lab.image, - "-ec", - "for i in $(seq 1 600); do " - 'if [ -f /data/fixture/ready ]; then exec "$@"; fi; ' - "sleep 0.1; done; exit 1", - "fixture", - *argv, - timeout=45, - ) - self.record["started"] = True - self.capture = OwnedProcess( - ["container", "logs", "--follow", self.name], - self.log, - {}, - limit=1024 * 1024, - ).__enter__() - state = json.loads(command("inspect", self.name))[0] - self.record["guest_mtu"] = state["status"]["networks"][0]["mtu"] - if self.record["guest_mtu"] != self.lab.mtu: - raise RuntimeError("isolated guest MTU differs from requested value") - self.ipv4 = state["status"]["networks"][0]["ipv4Address"].split("/")[0] - if ipaddress.ip_address(self.ipv4) not in self.lab.v4: - raise RuntimeError("container address outside owned network") - # Wait for SLAAC/DAD before any business traffic. No host IPv6 binds needed. - query = ( - "import pathlib,json; print(json.dumps([" - "[x[0],int(x[4],16)] for line in " - "pathlib.Path('/proc/net/if_inet6').read_text().splitlines() " - "if (x:=line.split())[5]=='eth0']))" - ) - deadline = time.monotonic() + 15 - while time.monotonic() < deadline: - rows = json.loads(command("exec", self.name, "python", "-c", query)) - for address, flags in rows: - ip = ipaddress.IPv6Address(int(address, 16)) - if ip in self.lab.v6 and not flags & (0x40 | 0x08): - self.ipv6 = str(ip) - return - time.sleep(0.1) - raise RuntimeError("container IPv6 readiness failed") - - def release(self): - (self.root / "ready").touch() - - def ensure_alive(self): - state = json.loads(command("inspect", self.name))[0]["status"]["state"] - if state != "running" or (self.capture and self.capture.overflow.is_set()): - raise RuntimeError("owned container peer exited or exceeded output bound") - - def wait_tcp(self, port): - deadline = time.monotonic() + 15 - while time.monotonic() < deadline: - self.ensure_alive() - try: - with socket.create_connection((self.ipv4, port), timeout=0.2): - self.record["ready"] = True - return - except OSError: - time.sleep(0.05) - raise TimeoutError("container service readiness failed") - - def stop(self): - try: - owned = next((item for item in listing() if item["id"] == self.name), None) - if owned is not None: - labels = owned["configuration"].get("labels", {}) - if ( - labels.get("purpose") != NETWORK - or labels.get("vcore-run") != self.lab.run_id - ): - raise RuntimeError("container ownership mismatch; refusing cleanup") - try: - if owned["status"]["state"] == "running": - command("stop", "--time", "5", self.name, timeout=15) - finally: - command("delete", "--force", self.name, timeout=15) - finally: - if self.capture is not None: - self.capture.__exit__(None, None, None) - self.record["log_cleanup"] = self.capture.record.get("joined", False) - self.record["joined"] = not any( - item["id"] == self.name for item in listing() - ) and self.record.get("log_cleanup", True) - if not self.record["joined"]: - raise RuntimeError("owned container cleanup incomplete") diff --git a/scripts/src/vcore_scripts/protocol_ech.py b/scripts/src/vcore_scripts/protocol_ech.py deleted file mode 100644 index a984b92..0000000 --- a/scripts/src/vcore_scripts/protocol_ech.py +++ /dev/null @@ -1,226 +0,0 @@ -"""SECURITY static ECH consumers against official latest isolated peers.""" - -from __future__ import annotations - -import base64 -import os -import shutil -import struct -import textwrap -from pathlib import Path - -from .protocol_peers import run_command - - -def catalog(): - cases = {} - - def add(mode, suffix, test): - cases[f"SECURITY-ECH-{mode.upper()}-{suffix}"] = ("M", mode, True, test) - - modes = ( - "tcp-tls", - "ws-tls", - "upgrade-tls", - "upgrade-fast-tls", - "grpc-tls", - ) + tuple( - f"xhttp-{mode}{version}" - for mode in ( - "stream-one", - "stream-up", - "packet-up", - "stream-up-download", - "packet-up-download", - ) - for version in ("", "-h1") - ) - for mode in modes: - add(mode, "BASE", "public_base") - add(mode, "CLOSE", "native_mihomo_close_alignment") - for mode in ( - "tcp-tls", - "ws-tls", - "grpc-tls", - "xhttp-stream-up-download", - "xhttp-stream-up-download-h1", - ): - add(mode, "REJECT", "native_ech_fail_closed") - for mode in modes: - if "download" in mode: - add(mode, "DOWNLOAD", "public_ech_download_identity") - for mode in ("tcp-mtls", "ws-mtls", "grpc-mtls"): - add(mode, "IDENTITY", "public_tls_identity_and_verification_name") - for suffix, test in ( - ("GRAPH", "runtime::public_graph"), - ("ENTRYPOINTS", "runtime::public_entrypoints"), - ("IPV6", "runtime::public_ipv6_and_gates"), - ("UDP-ISOLATION", "runtime::public_udp_isolation"), - ): - add("tcp-tls", suffix, test) - for mode in ("grpc-tls", "xhttp-stream-up-download"): - add(mode, "LIFE", "runtime::public_lifecycle") - add(mode, "OWNED", "runtime::owned_resources") - return cases - - -def material(directory: Path, key_id: int): - """Generate synthetic keys with official OpenSSL, never implement crypto here.""" - openssl = next( - ( - path - for path in ( - os.environ.get("OPENSSL_BIN"), - shutil.which("openssl3"), - "/opt/homebrew/opt/openssl@3/bin/openssl", - "/usr/local/opt/openssl@3/bin/openssl", - shutil.which("openssl"), - ) - if path and Path(path).is_file() - ), - None, - ) - if openssl is None: - raise RuntimeError("OpenSSL with X25519 support is required") - private = directory / f"ech-{key_id}.der" - generated = run_command( - [ - openssl, - "genpkey", - "-algorithm", - "X25519", - "-outform", - "DER", - "-out", - str(private), - ], - timeout=15, - ) - public = run_command( - [ - openssl, - "pkey", - "-inform", - "DER", - "-in", - str(private), - "-pubout", - "-outform", - "DER", - ], - timeout=15, - ) - if any(item.returncode or not item.cleanup for item in (generated, public)): - raise RuntimeError("synthetic ECH material generation failed") - secret = private.read_bytes() - if len(secret) != 48 or len(public.stdout) != 44: - raise RuntimeError("unexpected synthetic X25519 DER encoding") - name = b"public.invalid" - contents = bytes([key_id]) + struct.pack("!HH", 32, 32) + public.stdout[-32:] - contents += struct.pack("!HHHBB", 4, 1, 1, 0, len(name)) + name + b"\0\0" - config = struct.pack("!HH", 0xFE0D, len(contents)) + contents - key_set = ( - struct.pack("!H", 32) + secret[-32:] + struct.pack("!H", len(config)) + config - ) - return base64.b64encode(struct.pack("!H", len(config)) + config).decode(), key_set - - -def configuration(node, peer, directory, mode, *, kind="M"): - primary, first_key = material(directory, 7) - download, second_key = material(directory, 9) - encoded = base64.b64encode(first_key + second_key).decode() - pem = ( - "-----BEGIN ECH KEYS-----\n" - + "\n".join(textwrap.wrap(encoded, 64)) - + "\n-----END ECH KEYS-----\n" - ) - (directory / "ech-keys.pem").write_text(pem) - node["ech-opts"] = dict(enable=True, config=primary) - if mode.endswith("-h1"): - node["alpn"] = ["http/1.1"] - if kind == "M": - for listener in peer["listeners"]: - listener["ech-key"] = "/data/fixture/ech-keys.pem" - elif kind == "XR": - for inbound in peer["inbounds"]: - inbound["streamSettings"]["tlsSettings"]["echServerKeys"] = encoded - else: - raise ValueError("unsupported static ECH peer") - return dict(ech_download_config=download) - - -def wrong_key(options): - wire = bytearray(base64.b64decode(options["config"], validate=True)) - wire[11:43] = bytes([7]) * 32 - return dict(enable=True, config=base64.b64encode(wire).decode()) - - -def legacy_gateway_command(): - # Xray's dokodemo-door marks its inbound splice-capable even with TLS. - # Its default freedom response path then bypasses the TLS writer. Use - # the upstream runtime switch only for this synthetic TLS gateway. - return [ - "env", - "XRAY_BUF_SPLICE=disable", - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ] - - -def legacy_gateway_config(upstream, tls_options): - """Official Xray TLS ingress, no custom protocol server or V2Ray patches.""" - return dict( - log={"loglevel": "warning"}, - inbounds=[ - dict( - listen="::", - port=23000, - protocol="dokodemo-door", - settings=dict(address=upstream, port=23000, network="tcp"), - streamSettings=dict( - network="tcp", security="tls", tlsSettings=tls_options - ), - ) - ], - outbounds=[dict(protocol="freedom")], - ) - - -def field_variants(): - cases = {} - for version in ("h1", "h2", "h3"): - for action in ("reject-main", "reject-download", "replace", "clear"): - cases[f"{version}-ech-{action}"] = ( - { - "mode": "stream-up", - "download-settings": {}, - "_ech_action": action, - "_reject": action.startswith("reject-"), - }, - version, - ) - return cases - - -if __name__ == "__main__": - import argparse - - from .mihomo_isolation import exclusive_run - from .protocol_vless_container import CLIENT_FINGERPRINTS, run - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("cases", nargs="*") - parser.add_argument("--client-fingerprint", choices=CLIENT_FINGERPRINTS) - args = parser.parse_args() - with exclusive_run(): - raise SystemExit( - run( - args.output.resolve(), - args.cases or None, - ech=True, - client_fingerprint=args.client_fingerprint, - ) - ) diff --git a/scripts/src/vcore_scripts/protocol_encryption.py b/scripts/src/vcore_scripts/protocol_encryption.py deleted file mode 100644 index 6be795f..0000000 --- a/scripts/src/vcore_scripts/protocol_encryption.py +++ /dev/null @@ -1,347 +0,0 @@ -"""Encryption wire checks; not public YAML or complete security acceptance.""" - -from __future__ import annotations - -import base64 -import contextlib -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_containers import ContainerLab, command -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command - - -def cases(): - return { - f"{style}-{rtt}-{key}": (style, rtt, key) - for style in ("native", "xorpub", "random") - for rtt in ("1rtt", "0rtt") - for key in ("x25519", "mlkem", "mixed") - } - - -def encode(value): - return base64.urlsafe_b64encode(bytes.fromhex(value)).decode().rstrip("=") - - -def run( - output: Path, - selected=None, - *, - consumer=False, - chacha=False, - expiry=False, - padding="minimal", - supplied=None, -): - output = output.resolve() - required = cases() - if expiry: - required = { - name: value for name, value in required.items() if value[1] == "0rtt" - } - paddings = { - "minimal": ".100-35-35", - "default": "", - "fragmented": ".100-35-35.100-1-1.100-47-47", - "maximum": ".100-65553-65553", - } - selected = list(required) if selected is None else selected - if ( - not output.is_relative_to(CORE_DIR / "target/interop/runs") - or not selected - or len(set(selected)) != len(selected) - or not set(selected) <= required.keys() - or padding not in paddings - or (consumer and (chacha or expiry)) - ): - raise ValueError("fresh run directory and known unique cases required") - output.mkdir(parents=True, exist_ok=False) - if expiry: - test = "native_ticket_expiry" - marker = "SECURITY-ENCRYPTION-EXPIRY-PASS full-resumed-expired-full-resumed" - elif consumer: - test = "public_tcp_roundtrip" - marker = ( - "SECURITY-ENCRYPTION-CONSUMER-PASS rounds=4 bytes_per_direction=10485760" - ) - else: - test = "native_encryption_roundtrip" - marker = "SECURITY-ENCRYPTION-WIRE-PASS rounds=4 bytes_per_direction=10485760" - report = dict( - stage="SECURITY.encryption-wire", - scope="public-config-and-outbound" if consumer else "native-wire-only", - cipher="chacha20-poly1305" if chacha else "cpu-selected", - ticket_expiry=expiry, - padding=padding, - complete_selection=set(selected) == set(required), - source=source_identity(), - required=list(required), - cases=[], - isolation={}, - status="NOT RUN", - ) - try: - if supplied is None: - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "binaries", identity=identity - ) - else: - binary, identity = supplied["M"] - report["peer"] = identity - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "encryption_wire", - "--no-run", - ], - timeout=240, - ) - (output / "build.log").write_text(redact(built.stdout.decode(errors="replace"))) - if built.returncode or not built.cleanup: - raise RuntimeError("Encryption wire consumer build failed") - vectors = json.loads( - (CORE_DIR / "tests/protocols/encryption-crypto.json").read_text() - ) - pairs = dict( - x25519=( - encode(vectors["x25519_public"]), - encode(vectors["x25519_private"]), - ), - mlkem=(encode(vectors["mlkem_public"]), encode(vectors["mlkem_seed"])), - ) - pairs["mixed"] = tuple( - ".".join((pairs["x25519"][i], pairs["mlkem"][i], pairs["x25519"][i])) - for i in (0, 1) - ) - lab = ContainerLab(report["isolation"], mtu=1500) - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - server_dir, origin_dir = root / "server", root / "origin" - server_dir.mkdir() - origin_dir.mkdir() - shutil.copy2(binary, server_dir / "peer") - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - origin = lab.start( - stack, - origin_dir, - "security-e-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - server_dir, - "security-e-mihomo", - [ - "/data/fixture/peer", - "-d", - "/data/mihomo", - "-f", - "/data/fixture/config.json", - ], - ) - listeners = [] - for index, name in enumerate(selected): - style, _, key = required[name] - lifetime = "2-3s" if expiry else "600s" - listeners.append( - dict( - name=f"encryption-{index}", - type="vless", - listen="::", - port=23000 + index, - users=[ - dict( - username="fixture", - uuid="07070707-0707-0707-0707-070707070707", - ) - ], - decryption=( - f"mlkem768x25519plus.{style}.{lifetime}." - f"{pairs[key][1]}{paddings[padding]}" - ), - ) - ) - # A distinct native handler has no tickets from the primary. - # It exercises genuine unknown-ticket rejection without - # patching the peer, restarting it, or replaying business data. - listeners.append( - { - **listeners[-1], - "name": f"encryption-fresh-{index}", - "port": 23100 + index, - } - ) - (server_dir / "config.json").write_text( - json.dumps( - dict( - ipv6=True, - **{"log-level": "silent"}, - listeners=listeners, - rules=["MATCH,DIRECT"], - hosts={"vcore-fixture.test": origin.ipv4}, - ) - ) - ) - origin.release() - origin.wait_tcp(24000) - server.release() - for index in range(len(selected)): - server.wait_tcp(23000 + index) - server.wait_tcp(23100 + index) - identity["version"] = command( - "exec", server.name, "/data/fixture/peer", "-v" - ).strip() - if ( - command("exec", server.name, "sha256sum", "/data/fixture/peer").split()[ - 0 - ] - != identity["binary_sha256"] - ): - raise RuntimeError("Mihomo binary identity mismatch") - for index, name in enumerate(selected): - style, rtt, key = required[name] - encryption = ( - f"mlkem768x25519plus.{style}.{rtt}." - f"{pairs[key][0]}{paddings[padding]}" - ) - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - encryption=encryption, - force_chacha=chacha, - ticket_expiry=expiry, - node=dict( - name="edge", - type="vless", - server=server.ipv4, - port=23000 + index, - uuid="07070707-0707-0707-0707-070707070707", - udp=True, - encryption=encryption, - ), - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - server_ipv4=server.ipv4, - server_ipv6=server.ipv6, - port=23000 + index, - reject_port=23100 + index, - ) - ) - ) - argv = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "encryption_wire", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - result = run_command( - argv, - env={**os.environ, "VCORE_ENCRYPTION_FIXTURE": str(fixture)}, - timeout=150, - ) - text = result.stdout.decode(errors="replace") - (output / f"{name}.log").write_text(redact(text)) - passed = ( - result.returncode == 0 - and result.cleanup - and marker in text - and "1 passed; 0 failed; 0 ignored" in text - ) - report["cases"].append( - dict( - case_id=name, - command=argv, - returncode=result.returncode, - cleanup=result.cleanup, - status="PASS" if passed else "FAIL", - ) - ) - print(f"{name}: {'PASS' if passed else 'FAIL'}", flush=True) - if not passed: - raise RuntimeError("Encryption wire gate failed") - origin.ensure_alive() - server.ensure_alive() - report["status"] = "PASS" - except KeyboardInterrupt: - report["status"] = "INTERRUPTED" - except Exception as error: - report["status"] = "FAIL" - report["error"] = redact(str(error)) - finally: - report["source_after"] = source_identity() - report["source_unchanged"] = same_source( - report["source"], report["source_after"] - ) - report["cleanup"] = all( - peer.get("joined", False) for peer in report["isolation"].get("peers", []) - ) - if not report["source_unchanged"] or not report["cleanup"]: - report["status"] = "FAIL" - (output / "encryption-results.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return 0 if report["status"] == "PASS" else 1 - - -if __name__ == "__main__": - import argparse - import sys - - parser = argparse.ArgumentParser() - parser.add_argument("output", type=Path) - parser.add_argument("cases", nargs="*") - parser.add_argument("--consumer", action="store_true") - parser.add_argument("--chacha", action="store_true") - parser.add_argument("--expiry", action="store_true") - parser.add_argument( - "--padding", - choices=("minimal", "default", "fragmented", "maximum"), - default="minimal", - ) - args = parser.parse_args() - with exclusive_run(): - sys.exit( - run( - args.output, - args.cases or None, - consumer=args.consumer, - chacha=args.chacha, - expiry=args.expiry, - padding=args.padding, - ) - ) diff --git a/scripts/src/vcore_scripts/protocol_encryption_public.py b/scripts/src/vcore_scripts/protocol_encryption_public.py deleted file mode 100644 index 76a5b5e..0000000 --- a/scripts/src/vcore_scripts/protocol_encryption_public.py +++ /dev/null @@ -1,103 +0,0 @@ -"""Public Encryption consumers using the existing isolated VLESS lab.""" - -from __future__ import annotations - -import json -from pathlib import Path - -from .builds import CORE_DIR -from .protocol_encryption import cases, encode - - -def configuration(profile, node, peer): - style, rtt, key = cases()[profile] - vectors = json.loads( - (CORE_DIR / "tests/protocols/encryption-crypto.json").read_text() - ) - pairs = { - "x25519": (encode(vectors["x25519_public"]), encode(vectors["x25519_private"])), - "mlkem": (encode(vectors["mlkem_public"]), encode(vectors["mlkem_seed"])), - } - pairs["mixed"] = tuple( - ".".join((pairs["x25519"][i], pairs["mlkem"][i], pairs["x25519"][i])) - for i in (0, 1) - ) - # Nontrivial fragments/gaps exercise early-data continuation, not merely - # the smallest one-flight padding used by the wire/replay lab. - padding = "100-35-35.100-1-1.100-47-47" - node["encryption"] = f"mlkem768x25519plus.{style}.{rtt}.{pairs[key][0]}.{padding}" - for listener in peer["listeners"]: - listener["decryption"] = ( - f"mlkem768x25519plus.{style}.600s.{pairs[key][1]}.{padding}" - ) - - -def catalog(): - required = {} - modes = ( - "tcp", - "tcp-tls", - "tcp-reality", - "ws", - "ws-tls", - "ws-ed-1", - "ws-ed-2048", - "upgrade", - "upgrade-fast", - "grpc", - "grpc-tls", - "xhttp-stream-one", - "xhttp-stream-up", - "xhttp-packet-up", - "xhttp-stream-up-download", - "xhttp-packet-up-download", - ) - for mode in modes: - tls = mode.endswith(("-tls", "-reality")) or mode.startswith("xhttp-") - for suffix, test in ( - ("BASE", "public_base"), - ("CLOSE", "native_mihomo_close_alignment"), - ): - required[f"SECURITY-E-{mode.upper()}-{suffix}"] = ("M", mode, tls, test) - for mode in ("tcp-tls", "grpc-tls"): - for suffix, test in ( - ("GRAPH", "runtime::public_graph"), - ("IPV6", "runtime::public_ipv6_and_gates"), - ("ENTRYPOINTS", "runtime::public_entrypoints"), - ("LIFE", "runtime::public_lifecycle"), - ("OWNED", "runtime::owned_resources"), - ("UDP-ISOLATION", "runtime::public_udp_isolation"), - ): - required[f"SECURITY-E-{mode.upper()}-{suffix}"] = ("M", mode, True, test) - for mode in ("vision-encryption", "vision-tls", "vision-reality"): - for suffix, test in ( - ("BASE", "public_base"), - ("INNER-TLS", "native_vision_inner_tls"), - ("UDP", "native_vision_xudp"), - ("CLOSE", "native_mihomo_close_alignment"), - ("DIRECT-CLOSE", "native_vision_direct_close_alignment"), - ): - required[f"SECURITY-E-{mode.upper()}-{suffix}"] = ( - "M", - mode, - mode != "vision-encryption", - test, - ) - return required - - -if __name__ == "__main__": - import argparse - - from .mihomo_isolation import exclusive_run - from .protocol_vless_container import run - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("profile", choices=cases()) - parser.add_argument("cases", nargs="*") - args = parser.parse_args() - with exclusive_run(): - raise SystemExit( - run(args.output.resolve(), args.cases or None, encryption=args.profile) - ) diff --git a/scripts/src/vcore_scripts/protocol_evidence.py b/scripts/src/vcore_scripts/protocol_evidence.py deleted file mode 100644 index 03df3c5..0000000 --- a/scripts/src/vcore_scripts/protocol_evidence.py +++ /dev/null @@ -1,643 +0,0 @@ -"""Executable requirements and fail-closed structured result checks.""" - -from __future__ import annotations - -import hashlib -import json -import re -from pathlib import Path - -from .protocol_catalogs import ( - CATALOG_DIR, - CATEGORIES, - FIELD_IDS, - SUITES, - _unique_object, -) - -FOUNDATIONS_REQUIRED_IDS = { - "FOUNDATIONS-SCHEMA", - "FOUNDATIONS-LIMITS", - "FOUNDATIONS-STREAM", - "FOUNDATIONS-DATAGRAM", - "FOUNDATIONS-QUIC", - "FOUNDATIONS-RESOLUTION", - "FOUNDATIONS-RESOURCES", - "FOUNDATIONS-SECURITY", - "FOUNDATIONS-XUDP", - "FOUNDATIONS-REGRESSION", - "FOUNDATIONS-M-WS", - "FOUNDATIONS-M-WSS", - "FOUNDATIONS-M-GRPC", - "FOUNDATIONS-M-GRPC-TLS", - "FOUNDATIONS-M-WS-ED", - "FOUNDATIONS-V2-HTTP", - "FOUNDATIONS-V2-H2", - "FOUNDATIONS-V2-WS-HEADER", - "FOUNDATIONS-V2-WS-PATH", - "FOUNDATIONS-FEATURES", - "FOUNDATIONS-SCRIPTS", -} -HEX = re.compile(r"[0-9a-f]{64}") -RESOURCE_KINDS = { - "task", - "socket", - "session", - "association", - "reassembly", - "pool", - "waiter", - "handshake", -} - - -def idle_resources(snapshot) -> bool: - if not isinstance(snapshot, dict) or not isinstance(snapshot.get("counts"), list): - return False - counts = snapshot["counts"] - return ( - len(counts) == len(RESOURCE_KINDS) - and all(isinstance(count, dict) for count in counts) - and {count.get("kind") for count in counts} == RESOURCE_KINDS - and all( - type(count.get("current")) is int - and count["current"] == 0 - and type(count.get("peak")) is int - and count["peak"] >= 0 - for count in counts - ) - ) - - -def read_events(path: Path) -> list[dict]: - if path.stat().st_size > 4 * 1024 * 1024: - raise ValueError("case events exceeded bound") - events = [ - json.loads(line, object_pairs_hook=_unique_object) - for line in path.read_text().splitlines() - ] - if not all(isinstance(event, dict) for event in events): - raise ValueError("malformed structured case event") - return events - - -def read_json(path: Path): - if path.stat().st_size > 4 * 1024 * 1024: - raise ValueError("protocol evidence exceeds bounded document size") - return json.loads( - path.read_text(encoding="utf-8"), object_pairs_hook=_unique_object - ) - - -def executable_cases() -> list[dict]: - """Single source: retained foundation fixtures plus current protocol catalogs.""" - from .protocol_httpupgrade_catalog import definitions as httpupgrade - from .protocol_hysteria2_catalog import definitions as hysteria2 - from .protocol_integration_catalog import definitions as integration - from .protocol_security_catalog import definitions as security - from .protocol_shadowtls_catalog import definitions as shadowtls - from .protocol_tuic_catalog import definitions as tuic - from .protocol_uot_catalog import definitions as uot - from .protocol_vless_acceptance import definitions as vless - from .protocol_vmess_acceptance import definitions as vmess - from .protocol_xhttp_acceptance import definitions as xhttp - - foundation = read_json(CATALOG_DIR / "foundation-cases.json")["cases"] - return foundation + [ - case - for define in ( - vmess, - vless, - xhttp, - hysteria2, - security, - integration, - shadowtls, - uot, - tuic, - httpupgrade, - ) - for case in define() - ] - - -def load_manifest(path: Path | None = None) -> list[dict]: - expected = executable_cases() - data = ( - read_json(path) - if path is not None - else { - "schema_version": 1, - "kind": "executable-case-manifest", - "cases": expected, - } - ) - if ( - not isinstance(data, dict) - or type(data.get("schema_version")) is not int - or data.get("schema_version") != 1 - or data.get("kind") != "executable-case-manifest" - ): - raise ValueError("unsupported executable case manifest") - cases = data.get("cases") - if not isinstance(cases, list) or not cases: - raise ValueError("empty executable case manifest") - seen = set() - for case in cases: - required = { - "case_id", - "stage", - "substage", - "required", - "row_ids", - "protocol", - "network", - "security", - "udp_codec", - "field_values", - "outer_family", - "inner_family", - "target_type", - "upstream_graph", - "peer_kind", - "peer_config", - "gap_source", - "expected_observation", - "required_evidence", - "prerequisites", - "runner", - "timeout_seconds", - } - if not isinstance(case, dict) or not required <= case.keys(): - raise ValueError("incomplete executable case metadata") - identifier = case["case_id"] - if ( - not isinstance(identifier, str) - or not re.fullmatch(r"[A-Z0-9-]+", identifier) - or identifier in seen - ): - raise ValueError("invalid or duplicate executable case ID") - seen.add(identifier) - if ( - case["stage"] not in SUITES - or case["substage"] not in CATEGORIES - or not case["substage"].startswith(case["stage"] + ".") - ): - raise ValueError("invalid case ownership") - if ( - type(case["required"]) is not bool - or not isinstance(case["row_ids"], list) - or len(set(case["row_ids"])) != len(case["row_ids"]) - or not set(case["row_ids"]) <= FIELD_IDS - ): - raise ValueError("invalid required flag or field references") - if case["peer_kind"] not in {"M", "H", "XR", "V2", "SS", "unit"}: - raise ValueError("unknown peer kind") - for key in ["expected_observation", "required_evidence", "prerequisites"]: - items = case[key] - if ( - not isinstance(items, list) - or not items - or not all(isinstance(item, str) and item for item in items) - or len(items) != len(set(items)) - ): - raise ValueError("empty or duplicate executable observations") - if ( - not isinstance(case["field_values"], dict) - or not isinstance(case["peer_config"], dict) - or type(case["timeout_seconds"]) is not int - or not 1 <= case["timeout_seconds"] <= 3600 - ): - raise ValueError("invalid case bounds or configuration reference") - if { - case["case_id"] - for case in cases - if case["stage"] == "FOUNDATIONS" and case["required"] - } != FOUNDATIONS_REQUIRED_IDS: - raise ValueError("FOUNDATIONS required set was removed, renamed or downgraded") - from .protocol_trojan_acceptance import REQUIRED_IDS - - if { - case["case_id"] - for case in cases - if case["stage"] == "TROJAN" and case["required"] - } != REQUIRED_IDS: - raise ValueError("TROJAN required set was removed, renamed or downgraded") - if cases != expected: - raise ValueError("manifest differs from the current executable catalog") - return cases - - -def check_limit_references( - cases: list[dict], path: Path = CATALOG_DIR / "limits.json" -) -> None: - limits = read_json(path) - identifiers = {case["case_id"] for case in cases} - if ( - limits.get("schema_version") != 1 - or limits.get("kind") != "runtime-limit-registry" - ): - raise ValueError("unsupported runtime limit registry") - entries = limits.get("limits", []) - names = [entry.get("id") for entry in entries] - if not entries or len(names) != len(set(names)): - raise ValueError("empty or duplicate runtime limits") - for entry in entries: - if ( - not entry.get("boundary_cases") - or not set(entry["boundary_cases"]) <= identifiers - ): - raise ValueError("runtime limit has no executable boundary case") - - -def select_cases( - cases: list[dict], - *, - stage: str, - identifiers: list[str] | None = None, - protocol: str | None = None, -) -> list[dict]: - if identifiers and ( - len(identifiers) != len(set(identifiers)) - or not set(identifiers) <= {case["case_id"] for case in cases} - ): - raise ValueError("duplicate or unknown requested case") - selected = [ - case - for case in cases - if case["stage"] == stage - and case["required"] - and (not identifiers or case["case_id"] in identifiers) - and (protocol is None or case["protocol"] == protocol) - ] - if not selected: - raise ValueError("selection has no executable required cases; stage is NOT RUN") - if identifiers and {case["case_id"] for case in selected} != set(identifiers): - raise ValueError("case selection conflicts with stage or protocol") - return selected - - -def validate_results(required: list[dict], results: list[dict]) -> None: - if not required: - raise ValueError("empty required result set") - expected = {case["case_id"]: case for case in required} - identifiers = [result.get("case_id") for result in results] - if len(identifiers) != len(set(identifiers)) or set(identifiers) != set(expected): - raise ValueError("missing, duplicate or unknown case result") - for result in results: - case = expected[result["case_id"]] - if ( - result.get("status") != "PASS" - or result.get("cleanup") is not True - or type(result.get("command_exit_code")) is not int - or result["command_exit_code"] != 0 - ): - raise ValueError( - "required case failed, was blocked/not run, or did not clean up" - ) - observations = result.get("assertions", {}) - if ( - not isinstance(observations, dict) - or set(observations) != set(case["expected_observation"]) - or not all(value is True for value in observations.values()) - ): - raise ValueError("missing or false required behavioral assertion") - evidence = result.get("evidence", []) - if ( - not isinstance(evidence, list) - or len(evidence) != len(set(evidence)) - or set(evidence) != set(case["required_evidence"]) - ): - raise ValueError( - "incomplete behavior evidence; CFG alone is not acceptance" - ) - if ( - result.get("row_ids") != case["row_ids"] - or result.get("peer_kind") != case["peer_kind"] - or result.get("scope") - != ( - "protocol-consumer" - if case["stage"] - in { - "TROJAN", - "VMESS", - "VLESS", - "XHTTP", - "HYSTERIA2", - "SECURITY", - "INTEGRATION", - "SHADOWTLS", - "UOT", - "TUIC", - "HTTPUPGRADE", - } - else "foundation-only" - ) - ): - raise ValueError("case mapping or evidence scope differs from manifest") - - -def rust_results(case: dict, events: list[dict], returncode: int) -> dict: - observed = [ - event - for event in events - if event.get("suite") == case.get("event_suite", case["case_id"]) - ] - names = case["expected_observation"] - assertions = {name: False for name in names} - unknown = any( - event.get("assertion") not in assertions or event.get("schema_version") != 1 - for event in observed - ) - for name in names: - entries = [event for event in observed if event.get("assertion") == name] - assertions[name] = len(entries) == 2 and [ - entry.get("status") for entry in entries - ] == ["BEGIN", "PASS"] - if assertions[name] and "resources" in case["required_evidence"]: - assertions[name] = idle_resources(entries[-1].get("resources")) - result = new_result(case) - result.update(assertions=assertions, command_exit_code=returncode, cleanup=True) - result["status"] = ( - "PASS" - if not unknown and returncode == 0 and all(assertions.values()) - else "FAIL" - ) - if result["status"] == "PASS": - result["evidence"] = [ - item for item in case["required_evidence"] if item != "peer-identity" - ] - return result - - -def new_result(case: dict) -> dict: - return { - "case_id": case["case_id"], - "row_ids": case["row_ids"], - "peer_kind": case["peer_kind"], - "scope": "protocol-consumer" - if case["stage"] - in { - "TROJAN", - "VMESS", - "VLESS", - "XHTTP", - "HYSTERIA2", - "SECURITY", - "INTEGRATION", - "SHADOWTLS", - "UOT", - "TUIC", - "HTTPUPGRADE", - } - else "foundation-only", - "status": "NOT RUN", - "assertions": {}, - "evidence": [], - "cleanup": False, - "command_exit_code": None, - } - - -def artifact(run_dir: Path, relative: str, digest: str) -> Path: - path = run_dir / relative - if ( - Path(relative).is_absolute() - or ".." in Path(relative).parts - or path.is_symlink() - or not path.resolve().is_relative_to(run_dir.resolve()) - or not HEX.fullmatch(digest) - ): - raise ValueError("unsafe evidence path or invalid hash") - with path.open("rb") as stream: - actual = hashlib.file_digest(stream, "sha256").hexdigest() - if actual != digest: - raise ValueError("evidence content hash mismatch") - return path - - -def check_run(run_dir: Path, stage: str, manifest: Path | None = None) -> None: - if manifest is None and (run_dir / "manifest.json").is_file(): - manifest = run_dir / "manifest.json" - cases = load_manifest(manifest) - check_limit_references(cases) - required = select_cases(cases, stage=stage) - run = read_json(run_dir / "run.json") - result = read_json(run_dir / "cases.json") - peers = read_json(run_dir / "peers.json") - if ( - run.get("stage") != stage - or run.get("mode") != "execute" - or run.get("cleanup") is not True - or run.get("source_unchanged") is not True - or run.get("selected_cases") != [case["case_id"] for case in required] - ): - raise ValueError( - "partial/preflight run or changing source cannot sign off the stage" - ) - for key in ["source_tree_sha256", "lock_sha256", "dirty_patch_sha256"]: - if not isinstance(run.get(key), str) or not HEX.fullmatch(run[key]): - raise ValueError("missing immutable input identity") - validate_results(required, result) - artifacts = run.get("artifacts", []) - paths = [entry["path"] for entry in artifacts] - if len(paths) != len(set(paths)): - raise ValueError("duplicate evidence artifact") - if stage in { - "TROJAN", - "VMESS", - "VLESS", - "XHTTP", - "HYSTERIA2", - "SECURITY", - "INTEGRATION", - "SHADOWTLS", - "UOT", - "TUIC", - "HTTPUPGRADE", - }: - if stage == "TROJAN": - from .protocol_trojan_acceptance import check - elif stage == "VMESS": - from .protocol_vmess_acceptance import check - elif stage == "VLESS": - from .protocol_vless_acceptance import check - elif stage == "XHTTP": - from .protocol_xhttp_acceptance import check - elif stage == "HYSTERIA2": - from .protocol_hysteria2_acceptance import check - elif stage == "SECURITY": - from .protocol_security_acceptance import check - elif stage == "SHADOWTLS": - from .protocol_shadowtls_acceptance import check - elif stage == "UOT": - from .protocol_uot_acceptance import check - elif stage == "TUIC": - from .protocol_tuic_acceptance import check - elif stage == "HTTPUPGRADE": - from .protocol_httpupgrade_acceptance import check - else: - from .protocol_integration_acceptance import check - - for evidence in artifacts: - artifact(run_dir, evidence["path"], evidence["sha256"]) - check(run_dir, required, run, result, peers, paths) - return - necessary = { - "cases.json", - "peers.json", - "resources.jsonl", - "summary.md", - "rust-events.jsonl", - "legacy-events.jsonl", - "native/stream-cases.json", - "script-tests.json", - } - if not necessary <= set(paths): - raise ValueError("missing persisted behavior artifacts") - for evidence in artifacts: - artifact(run_dir, evidence["path"], evidence["sha256"]) - commands = run.get("commands", []) - command_names = [command.get("name") for command in commands] - required_commands = { - "rust-foundations", - "native-probe-build", - "legacy-mihomo", - "offline-scripts", - "feature-default", - "feature-minimal", - "feature-outbound-trojan", - "feature-outbound-vmess", - "feature-outbound-hysteria2", - "feature-stream-transport", - "feature-quic-transport", - } - if ( - len(command_names) != len(set(command_names)) - or set(command_names) != required_commands - or any( - type(command.get("exit_code")) is not int - or command["exit_code"] != 0 - or command.get("cleanup") is not True - for command in commands - ) - ): - raise ValueError("missing, failed or unjoined acceptance command") - for command in commands: - if command.get("log") and command["log"] not in paths: - raise ValueError("command log has no content identity") - rust_events = read_events(run_dir / "rust-events.jsonl") - legacy_events = read_events(run_dir / "legacy-events.jsonl") - native = read_json(run_dir / "native/stream-cases.json") - native_cases = [case for case in required if case["runner"] == "native-stream"] - if [item.get("case_id") for item in native.get("cases", [])] != [ - case["case_id"] for case in native_cases - ]: - raise ValueError("missing, duplicate or unknown native case evidence") - # Use exactly the same structured evaluators as execution, never console text. - from .protocol_harness import SCRIPT_OBSERVATIONS, _native_results - - recalculated = _native_results(native_cases, native) - for case in required: - if case["runner"] in {"rust", "mihomo-legacy"}: - calculated = rust_results( - case, - legacy_events if case["runner"] == "mihomo-legacy" else rust_events, - 0, - ) - if case["runner"] == "mihomo-legacy" and calculated["status"] == "PASS": - calculated["evidence"] = case["required_evidence"] - recalculated.append(calculated) - actual = {item["case_id"]: item for item in result} - for calculated in recalculated: - for key in ["status", "assertions", "evidence", "cleanup", "command_exit_code"]: - if actual[calculated["case_id"]].get(key) != calculated[key]: - raise ValueError( - "case result disagrees with original structured evidence" - ) - script = read_json(run_dir / "script-tests.json") - scripts = script.get("cases", []) - tests = {entry["test"]: entry["status"] for entry in scripts} - if ( - not scripts - or len(tests) != len(scripts) - or len(tests) != script.get("tests_run") - or any(status != "PASS" for status in tests.values()) - or any( - tests.get(identifier) != "PASS" - for identifier in SCRIPT_OBSERVATIONS.values() - ) - ): - raise ValueError("missing or failed offline harness failure-path proof") - resources = read_events(run_dir / "resources.jsonl") - if not resources or not all( - idle_resources(event.get("resources")) for event in resources - ): - raise ValueError("missing, nonidle or malformed owned resource evidence") - resource_passes = [ - event - for event in rust_events - if event.get("suite") == "FOUNDATIONS-RESOURCES" - and event.get("status") == "PASS" - ] - for event in resource_passes: - checkpoints = event.get("checkpoints", []) - by_phase = {point["phase"]: point["resources"] for point in checkpoints} - if ( - len(checkpoints) != len(by_phase) - or not {"baseline", "after-stop"} <= by_phase.keys() - or not idle_resources(by_phase["baseline"]) - or not idle_resources(by_phase["after-stop"]) - ): - raise ValueError("resource ownership has no baseline/Stop evidence") - if "remain_quiet" in event["assertion"] and ( - by_phase.get("quiet") != by_phase["after-stop"] - or event.get("seconds", 0) < 5 - ): - raise ValueError("resource quiet-window evidence missing") - for case in required: - if "peer-identity" in case["required_evidence"]: - matches = [ - peer - for peer in peers - if peer.get("kind") == case["peer_kind"] - and peer.get("status") == "READY" - ] - if ( - len(matches) != 1 - or not matches[0].get("version") - or not HEX.fullmatch(matches[0].get("binary_sha256", "")) - or not HEX.fullmatch(matches[0].get("archive_sha256", "")) - or not matches[0] - .get("source_url", "") - .startswith("https://github.com/") - ): - raise ValueError("missing official peer identity") - if case["runner"] == "native-stream": - native_peers = [ - peer - for peer in native.get("peers", []) - if peer.get("kind") == case["peer_kind"] - ] - if ( - len(native_peers) != 1 - or native_peers[0].get("binary_sha256") - != matches[0]["binary_sha256"] - ): - raise ValueError("case used a different peer from preflight") - if case["runner"] == "mihomo-legacy": - peer = matches[0] - if ( - peer.get("container_status") != "READY" - or not HEX.fullmatch( - peer.get("container_artifact", {}).get("binary_sha256", "") - ) - or peer["container_artifact"].get("release") != peer.get("release") - ): - raise ValueError( - "legacy peer did not use the same official release snapshot" - ) - print( - f"{stage}: PASS ({len(required)} required foundation cases); " - "production field consumers remain NOT RUN" - ) diff --git a/scripts/src/vcore_scripts/protocol_fingerprint.py b/scripts/src/vcore_scripts/protocol_fingerprint.py deleted file mode 100644 index 1ca5339..0000000 --- a/scripts/src/vcore_scripts/protocol_fingerprint.py +++ /dev/null @@ -1,177 +0,0 @@ -"""Named-profile VCore integration, reusing the owned container wire harness.""" - -import argparse -import hashlib -import json -import sys -import time -from datetime import UTC, datetime -from pathlib import Path - -from .mihomo_isolation import exclusive_run -from .protocol_inputs import same_source, source_identity -from .protocol_vless_container import CLIENT_FINGERPRINTS, run - -SELECTED_TIMEOUT_SECONDS = 3600 - -CASES = [ - "FINGERPRINT-ANYTLS", - *[ - f"VLESS-REGRESSION-{protocol}-{network}" - for protocol in ("TROJAN", "VMESS") - for network in ("TCP", "WS", "GRPC") - ], - *[ - f"VLESS-{mode}-BASE" - for mode in ( - "TCP-TLS", - "WS-TLS", - "WS-ED-1-TLS", - "WS-ED-2048-TLS", - "WS-HEADER-TLS", - "WS-PATH-TLS", - "UPGRADE-TLS", - "UPGRADE-FAST-TLS", - "HTTP-TLS", - "H2-TLS", - "GRPC-TLS", - "TCP-REALITY", - "WS-REALITY", - "UPGRADE-REALITY", - "UPGRADE-FAST-REALITY", - "GRPC-REALITY", - "XHTTP-STREAM-ONE", - "XHTTP-STREAM-UP", - "XHTTP-PACKET-UP", - "XHTTP-STREAM-ONE-REALITY", - "XHTTP-STREAM-UP-DOWNLOAD-REALITY", - "VISION-TLS", - "VISION-REALITY", - ) - ], - "VLESS-VISION-TLS-INNER-TLS", - "FINGERPRINT-VMESS-HTTP-TLS", - "FINGERPRINT-VMESS-H2-TLS", - "VLESS-VISION-REALITY-INNER-TLS", - "VLESS-TCP-REALITY-NEG", - "VLESS-TCP-TLS-NEG", - "VLESS-GRPC-MTLS-IDENTITY", - "VLESS-WS-ALPN-TLS-NEG", - "VLESS-TCP-TLS-CLOSE", - "VLESS-TCP-REALITY-CLOSE", - "VLESS-GRPC-REALITY-CLOSE", - "VLESS-VISION-REALITY-CLOSE", - "VLESS-GRPC-TLS-LIFE", - "VLESS-GRPC-TLS-OWNED", - "VLESS-VISION-REALITY-LIFE", - "VLESS-VISION-REALITY-OWNED", -] - - -def run_selected(output, profile): - from .builds import CORE_DIR - from .protocol_xhttp_fields import run as run_xhttp - from .protocol_xhttp_fields import selected_profile_variants - - if ( - profile not in CLIENT_FINGERPRINTS - or output.parent != CORE_DIR / "target/interop/runs" - ): - raise ValueError("use a supported profile and fresh target/interop/runs child") - output.mkdir(parents=True, exist_ok=False) - started = time.monotonic() - report = dict( - stage="FINGERPRINT-INTEROP", - scope="selected-transports", - profile=profile, - source=source_identity(), - status="NOT RUN", - parts=[], - started_utc=datetime.now(UTC).isoformat(), - timeout_seconds=SELECTED_TIMEOUT_SECONDS, - ) - try: - for phase in ("transports", "xhttp"): - if phase == "transports": - result = run(output / phase, CASES, client_fingerprint=profile) - path = output / phase / "vless-results.json" - else: - selected = [ - f"{version}-{mode}-headers" - for version in ("h1", "h2") - for mode in ("stream-one", "stream-up", "packet-up") - ] - selected.extend(selected_profile_variants(profile)) - jobs = { - name: [ - dict( - case_id=f"FINGERPRINT-{profile}-{name}", - test="security::native_xhttp_security" - if name.endswith("reject-pin") - else "public_base", - ) - ] - for name in selected - } - result = run_xhttp( - output / phase, selected, jobs=jobs, client_fingerprint=profile - ) - path = output / phase / "xhttp-fields-results.json" - part = json.loads(path.read_text()) - report["parts"].append( - dict( - phase=phase, - report=str(path.relative_to(output)), - sha256=hashlib.sha256(path.read_bytes()).hexdigest(), - status=part["status"], - cleanup=part["cleanup"], - source_unchanged=part["source_unchanged"], - cases=len(part["cases"]), - ) - ) - if result: - raise RuntimeError(f"{phase} gate failed") - report["status"] = "PASS" - except (OSError, ValueError, RuntimeError) as error: - report.update(status="FAIL", reason=str(error)) - finally: - report["finished_utc"] = datetime.now(UTC).isoformat() - report["seconds"] = round(time.monotonic() - started, 3) - report["source_after"] = source_identity() - # Staging an already captured untracked file changes the diagnostic - # Git patch, not the exhaustive path/content source-tree digest. - report["source_unchanged"] = same_source( - report["source"], report["source_after"] - ) - if not report["source_unchanged"]: - report["status"] = "FAIL" - if report["seconds"] > SELECTED_TIMEOUT_SECONDS: - report.update( - status="FAIL", reason="selected transport suite deadline exceeded" - ) - (output / "fingerprint-results.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return 0 if report["status"] == "PASS" else 1 - - -def main(argv=None): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("cases", nargs="*") - parser.add_argument( - "--client-fingerprint", choices=CLIENT_FINGERPRINTS, default="chrome120" - ) - args = parser.parse_args(argv) - with exclusive_run(): - if not args.cases: - return run_selected(args.output.resolve(), args.client_fingerprint) - return run( - args.output.resolve(), - args.cases or CASES, - client_fingerprint=args.client_fingerprint, - ) - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/scripts/src/vcore_scripts/protocol_fingerprint_reference.py b/scripts/src/vcore_scripts/protocol_fingerprint_reference.py deleted file mode 100644 index 37f73f2..0000000 --- a/scripts/src/vcore_scripts/protocol_fingerprint_reference.py +++ /dev/null @@ -1,551 +0,0 @@ -"""Selected-v1 reference ClientHello acquisition; not VCore interoperability.""" - -from __future__ import annotations - -import argparse -import base64 -import contextlib -import copy -import json -import secrets -import shutil -import signal -import sys -import tempfile -import time -import urllib.error -import urllib.parse -import urllib.request -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_containers import ContainerLab, command, listing -from .protocol_fixtures import certificates -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .tls_client_hello import is_grease, parse_client_hello, validate_capture - -PROFILES = ("chrome", "chrome120", "firefox", "firefox120", "safari", "safari16") -TEMPLATES = { - "chrome": "Chrome133", - "chrome120": "Chrome120", - "firefox": "Firefox120", - "firefox120": "Firefox120", - "safari": "Safari16.0", - "safari16": "Safari16.0", -} -SNIS = ("fingerprint.test", "f" * 63 + ".fingerprint.test") -PORTS = { - "tcp": 24430, - "ws": 24430, - "grpc": 24430, - "reality": 24430, - "tls13": 24431, - "tls12": 24432, -} - - -def reference_cases() -> list[dict]: - """The public reference-case catalog; a listing does not start a peer.""" - cases = [] - for profile in (*PROFILES, "none"): - contexts = ("tcp",) if profile == "none" else ("tcp", "ws", "grpc", "reality") - for context in contexts: - for sni in SNIS: - for attempt in range(2): - cases.append( - dict( - id=f"FINGERPRINT-REFERENCE-{profile}-{context}-" - f"{len(sni)}-{attempt}", - profile=profile, - template=TEMPLATES.get(profile), - context=context, - sni=sni, - sni_length=len(sni), - attempt=attempt, - ) - ) - for profile in ("chrome", "chrome120", "firefox", "safari"): - for context in ("tls12", "tls13"): - for attempt in range(2): - cases.append( - dict( - id=f"FINGERPRINT-REFERENCE-{profile}-{context}-16-{attempt}", - profile=profile, - template=TEMPLATES[profile], - context=context, - sni=SNIS[0], - sni_length=16, - attempt=attempt, - ) - ) - return cases - - -def _reference_shape(hello, case, *, expected_alpn=None, allow_psk=False): - """Apply only source-defined reference variations, checking before masking.""" - shape = copy.deepcopy(hello) - shape.pop("raw_sha256") - size = shape.pop("bytes") - padding = next((e for e in shape["extensions"] if e["type"] == 21), None) - psk = next((e for e in shape["extensions"] if e["type"] == 41), None) - if psk and (not allow_psk or shape["extensions"][-1] != psk): - raise ValueError("unexpected PSK or invalid last-extension position") - unpadded = size - (padding["bytes"] + 4 if padding else 0) - needs_padding = ( - case["template"] in {"Chrome120", "Safari16.0"} and 255 < unpadded < 512 - ) - if bool(padding) != needs_padding: - raise ValueError("reference padding presence differs from template") - if padding: - expected = max(1, 512 - unpadded - 4) - if ( - padding["bytes"] != expected - or padding["payload_hex"] != "00" * expected - or shape["extensions"][-2 if psk else -1] != padding - ): - raise ValueError("reference padding contents, position or size mismatch") - shape["extensions"].remove(padding) - groups = next(e["values"] for e in shape["extensions"] if e["type"] == 10) - shares = next(e["shares"] for e in shape["extensions"] if e["type"] == 51) - if any(s["group"] not in groups for s in shares): - raise ValueError("reference key share/group correlation mismatch") - for row in shape["extensions"]: - if row["type"] == 0: - if row["names"] != [dict(type=0, name=case["sni"])]: - raise ValueError("reference SNI mismatch") - row["names"], row["bytes"] = "synthetic-sni", "sni-dependent" - elif row["type"] == 16: - expected = expected_alpn or ( - ["http/1.1"] if case["context"] == "ws" else ["h2", "http/1.1"] - ) - if row["protocols"] != expected: - raise ValueError("reference transport ALPN mismatch") - row["protocols"], row["bytes"] = "transport-alpn", "alpn-dependent" - elif row["type"] == 65037: - firefox = case["template"] == "Firefox120" - if ( - (row["hello_type"], row["kdf"], row["enc_bytes"]) != (0, 1, 32) - or row["aead"] not in ({1, 3} if firefox else {1}) - or row["payload_bytes"] - not in ({239} if firefox else {144, 176, 208, 240}) - or row["bytes"] != 42 + row["payload_bytes"] - ): - raise ValueError( - "reference ECH GREASE differs from source-defined variants" - ) - row["aead"], row["payload_bytes"], row["bytes"] = ( - "checked-variant", - "checked-variant", - "checked-variant", - ) - for key in ("values",): - if key in row: - row[key] = ["GREASE" if is_grease(v) else v for v in row[key]] - if row["type"] == 51: - for share in row["shares"]: - if is_grease(share["group"]): - share["group"] = "GREASE" - if is_grease(row["type"]): - row["type"] = "GREASE" - shape["ciphers"] = ["GREASE" if is_grease(v) else v for v in shape["ciphers"]] - if case["template"].startswith("Chrome"): - rows = shape["extensions"] - if rows[0] != dict(type="GREASE", bytes=0, payload_hex="") or rows[ - -2 if psk else -1 - ] != dict(type="GREASE", bytes=1, payload_hex="00"): - raise ValueError("reference Chrome GREASE positions changed") - shape["extensions"] = [ - rows[0], - *sorted(rows[1 : -2 if psk else -1], key=lambda e: e["type"]), - *rows[-2 if psk else -1 :], - ] - return shape - - -def check_reference_report(report: dict) -> dict: - """Verify reference scope, raw observations and source-defined variations.""" - if ( - report.get("scope") != "selected-v1" - or report.get("status") != "CAPTURED" - or report.get("selection") != "full" - or report.get("cleanup") is not True - or report.get("source_unchanged") is not True - ): - raise ValueError("incomplete or unclean selected-profile reference run") - if report.get("source") != report.get("source_after"): - raise ValueError("reference source changed during capture") - catalog = {case["id"]: case for case in reference_cases()} - cases = report["cases"] - if len(cases) != len(catalog) or {c["id"] for c in cases} != catalog.keys(): - raise ValueError("missing or duplicate reference cases") - fixtures = json.loads( - (CORE_DIR / "tests/fingerprints/mihomo-selected-v1.json").read_text() - ) - baselines = {} - for sample in fixtures["samples"]: - case = catalog[sample["case_id"]] - baselines[(sample["profile"], sample["context"])] = _reference_shape( - validate_capture(sample), case - ) - observed, tls_handshakes = 0, 0 - for row in cases: - case = catalog[row["id"]] - if ( - any(row.get(key) != value for key, value in case.items()) - or row["status"] != "CAPTURED" - or not 1 <= len(row["observations"]) <= 4 - ): - raise ValueError("reference case metadata or observations changed") - for event in row["observations"]: - hello = validate_capture(event) - expected = ( - "tls-handshake-complete" - if case["context"] in {"tls12", "tls13"} - else "captured-only" - ) - if event["status"] != expected or event["port"] != PORTS[case["context"]]: - raise ValueError("reference observation has wrong outcome") - if expected == "tls-handshake-complete": - version = "TLSv1.2" if case["context"] == "tls12" else "TLSv1.3" - if event["version"] != version: - raise ValueError("reference negotiated wrong TLS version") - tls_handshakes += 1 - if case["profile"] != "none": - profile = {"firefox120": "firefox", "safari16": "safari"}.get( - case["profile"], case["profile"] - ) - context = "reality" if case["context"] == "reality" else "tcp" - if _reference_shape(hello, case) != baselines[(profile, context)]: - raise ValueError(f"reference structure changed: {case['id']}") - observed += 1 - return dict( - scope="selected-v1", - stage="FINGERPRINT-REFERENCE", - status="BASELINE VERIFIED", - cases=len(cases), - observations=observed, - tls_only_handshakes=tls_handshakes, - business_interoperability="NOT TESTED", - production_support="UNCHANGED", - ) - - -def _name(case): - return f"{case['profile']}-{case['context']}-{case['sni_length']}" - - -def _node(case, server): - context = case["context"] - node = dict( - name=_name(case), - type="vless", - server=server, - port=PORTS[context], - uuid="07070707-0707-0707-0707-070707070707", - tls=True, - servername=case["sni"], - network=context if context in {"ws", "grpc"} else "tcp", - alpn=["h2", "http/1.1"], - **{"client-fingerprint": case["profile"]}, - ) - if context == "reality": - # Public RFC 7748 test vector; observer never authenticates REALITY. - public = bytes.fromhex( - "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f" - ) - node["reality-opts"] = { - "public-key": base64.urlsafe_b64encode(public).decode().rstrip("="), - "short-id": "01020304", - } - else: - node["skip-cert-verify"] = True - if context == "ws": - node["ws-opts"] = {"path": "/capture"} - elif context == "grpc": - node["grpc-opts"] = {"grpc-service-name": "capture"} - return node - - -def _probe(client, case, target, secret): - query = urllib.parse.urlencode(dict(url=f"http://{target}:80/", timeout=3000)) - request = urllib.request.Request( - f"http://{client}:25000/proxies/{_name(case)}/delay?{query}", - headers={"Authorization": "Bearer " + secret}, - ) - opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) - try: - response = opener.open(request, timeout=5) - except urllib.error.HTTPError as error: - response = error - with response: - body = response.read(4097) - if len(body) > 4096: - raise ValueError("reference controller response limit") - # This observer cannot serve VLESS or a URL: delay failure is expected. - return dict(delay_http_status=response.status, business_result="NOT TESTED") - - -def _events(observer, after): - query = ( - "import json,pathlib,sys; p=pathlib.Path('/data/events.jsonl'); " - "assert p.stat().st_size <= 8*1024*1024; " - "print(json.dumps(p.read_text().splitlines()[int(sys.argv[1]):]))" - ) - lines = json.loads( - command("exec", observer.name, "python", "-c", query, str(after)) - ) - return [json.loads(line) for line in lines] - - -def run_reference(output: Path, selected=None) -> int: - catalog = {case["id"]: case for case in reference_cases()} - ids = list(catalog) if selected is None else selected - if not ids or len(set(ids)) != len(ids) or not set(ids) <= catalog.keys(): - raise ValueError("invalid reference capture selection") - output = output.resolve() - if output.parent != CORE_DIR / "target/interop/runs" or output.exists(): - raise ValueError("fresh in-repository reference run directory required") - output.mkdir(parents=True) - report = dict( - scope="selected-v1", - stage="FINGERPRINT-REFERENCE", - kind="reference-clienthello-only", - status="NOT RUN", - production_support="UNCHANGED", - business_interoperability="NOT TESTED", - selection="full" if set(ids) == catalog.keys() else "partial", - source=source_identity(), - cases=[], - cleanup=False, - source_unchanged=False, - ) - lab = None - try: - with exclusive_run(), contextlib.ExitStack() as stack: - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "official-mihomo", identity=identity - ) - report["mihomo"] = identity - build = run_command(["go", "version", "-m", str(binary)], timeout=10) - if build.returncode or not build.cleanup: - raise RuntimeError("official binary build identity query failed") - (output / "mihomo-build-info.txt").write_bytes(build.stdout) - report["utls_build_info"] = [ - line.strip() - for line in build.stdout.decode().splitlines() - if "utls" in line or "vcs.revision" in line - ] - if not any( - "github.com/metacubex/utls\tv1.8.7\t" in line - for line in report["utls_build_info"] - ): - raise RuntimeError( - "reference uTLS revision changed; recheck selected template mapping" - ) - root = Path( - stack.enter_context( - tempfile.TemporaryDirectory(prefix="private-", dir=output) - ) - ) - observer_root, client_root = root / "observer", root / "client" - observer_root.mkdir() - client_root.mkdir() - certificates(observer_root) - shutil.copy2( - Path(__file__).with_name("container_tls_observer.py"), - observer_root / "observer.py", - ) - shutil.copy2(binary, client_root / "mihomo") - lab = ContainerLab(report.setdefault("isolation", {}), mtu=1500) - observer = lab.start( - stack, - observer_root, - "fingerprint-reference-observer", - [ - "env", - "VCORE_ISOLATED_TLS_OBSERVER=1", - "python", - "-B", - "-u", - "/data/fixture/observer.py", - ], - ) - observer.release() - for port in sorted(set(PORTS.values())): - observer.wait_tcp(port) - secret = secrets.token_hex(24) - nodes = { - _name(catalog[case]): _node(catalog[case], observer.ipv4) - for case in ids - } - config = dict( - proxies=list(nodes.values()), - rules=["MATCH,REJECT"], - secret=secret, - **{"log-level": "warning", "external-controller": "0.0.0.0:25000"}, - ) - (client_root / "config.json").write_text(json.dumps(config)) - client = lab.start( - stack, - client_root, - "fingerprint-reference-mihomo", - [ - "/data/fixture/mihomo", - "-d", - "/data/mihomo", - "-f", - "/data/fixture/config.json", - ], - ) - - def retain_logs(): - for role, peer in (("observer", observer), ("client", client)): - if peer.log.exists(): - text = peer.log.read_text(errors="replace")[-16384:] - for value in (secret, observer.ipv4, client.ipv4): - text = text.replace(value, "") - (output / f"{role}.log").write_text(redact(text)) - - stack.callback(retain_logs) - identity["version"] = command( - "exec", client.name, "/data/fixture/mihomo", "-v" - ).strip() - digest = command( - "exec", client.name, "sha256sum", "/data/fixture/mihomo" - ).split()[0] - if digest != identity["binary_sha256"]: - raise RuntimeError("container official binary identity mismatch") - report["observer_tls"] = command( - "exec", - observer.name, - "python", - "-c", - "import ssl; print(ssl.OPENSSL_VERSION)", - ).strip() - client.release() - client.wait_tcp(25000) - observed_count = 0 - for case_id in ids: - case = catalog[case_id] - row = dict(case, status="NOT RUN", observations=[]) - report["cases"].append(row) - row["controller"] = _probe(client.ipv4, case, observer.ipv4, secret) - deadline = time.monotonic() + 6 - while True: - events = _events(observer, observed_count) - if events or time.monotonic() >= deadline: - break - time.sleep(0.05) - if not 1 <= len(events) <= 4: - raise RuntimeError( - f"reference observation count mismatch: {case_id}" - ) - observed_count += len(events) - expected = ( - "tls-handshake-complete" - if case["context"] in {"tls12", "tls13"} - else "captured-only" - ) - for event in events: - row["observations"].append(event) - if "client_hello_b64" not in event: - raise RuntimeError( - f"reference observer did not capture a hello: {case_id}" - ) - hello = parse_client_hello( - base64.b64decode(event["client_hello_b64"], validate=True) - ) - event["parsed"] = hello - sni = next( - (e["names"] for e in hello["extensions"] if e["type"] == 0), [] - ) - if ( - event["status"] != expected - or event["port"] != PORTS[case["context"]] - or sni != [dict(type=0, name=case["sni"])] - ): - raise RuntimeError( - f"reference TLS observation mismatch: {case_id}" - ) - row["status"] = "CAPTURED" - print( - json.dumps( - dict( - case_id=case_id, - status=row["status"], - observations=len(events), - ) - ), - flush=True, - ) - client.ensure_alive() - observer.ensure_alive() - report["status"] = "CAPTURED" - except BaseException as error: - report["status"] = "FAIL" - report["error"] = redact(f"{type(error).__name__}: {error}") - raise - finally: - report["source_after"] = source_identity() - report["source_unchanged"] = report["source_after"] == report["source"] - if lab is not None: - report["cleanup"] = not any( - item["configuration"].get("labels", {}).get("vcore-run") == lab.run_id - for item in listing() - ) and all(peer.get("joined") for peer in report["isolation"]["peers"]) - if report["status"] == "CAPTURED" and not ( - report["source_unchanged"] and report["cleanup"] - ): - report["status"] = "FAIL" - (output / "reference-results.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - print( - json.dumps( - {key: report[key] for key in ("status", "cleanup", "source_unchanged")} - ), - flush=True, - ) - return 0 if report["status"] == "CAPTURED" else 1 - - -def main(argv=None): - parser = argparse.ArgumentParser(description=__doc__) - mode = parser.add_mutually_exclusive_group(required=True) - mode.add_argument("--list", action="store_true") - mode.add_argument("--run-dir", type=Path) - mode.add_argument("--check-run", type=Path) - parser.add_argument("--case", action="append") - args = parser.parse_args(argv) - if args.check_run: - if args.case: - parser.error("--case requires --run-dir") - report = args.check_run / "reference-results.json" - if report.stat().st_size > 8 * 1024 * 1024: - raise ValueError("reference report size limit") - print( - json.dumps(check_reference_report(json.loads(report.read_text())), indent=2) - ) - return 0 - if args.list: - if args.case: - parser.error("--case requires --run-dir") - print(json.dumps(reference_cases(), indent=2)) - return 0 - - def stop_signal(_signum, _frame): - raise KeyboardInterrupt() - - previous = signal.signal(signal.SIGTERM, stop_signal) - try: - return run_reference(args.run_dir, args.case) - finally: - signal.signal(signal.SIGTERM, previous) - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/scripts/src/vcore_scripts/protocol_fingerprint_shape.py b/scripts/src/vcore_scripts/protocol_fingerprint_shape.py deleted file mode 100644 index 3f24944..0000000 --- a/scripts/src/vcore_scripts/protocol_fingerprint_shape.py +++ /dev/null @@ -1,260 +0,0 @@ -"""Public-config TLS wire checks over memory IO; no network observer.""" - -from __future__ import annotations - -import argparse -import base64 -import copy -import io -import json -import os -from pathlib import Path - -from .builds import CORE_DIR -from .container_tls_observer import capture_client_hello -from .protocol_fingerprint_reference import PROFILES, SNIS, TEMPLATES, _reference_shape -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command -from .tls_client_hello import parse_client_hello, validate_capture - -CONTEXTS = ("tcp", "ws", "grpc", "reality", "vision", "xhttp-h1", "xhttp-h2") - - -def expected_shape(sample, context): - """Transform an independent baseline only for documented caller policies.""" - case = dict(template=sample["template"], sni=SNIS[0], context=sample["context"]) - shape = _reference_shape(validate_capture(sample), case) - tls13 = context in {"vision", "xhttp-h1", "xhttp-h2"} - if tls13: - # Standard Vision/XHTTP require TLS1.3 before authentication. These - # TLS1.2-only fields are omitted by native version policy, not hidden - # from actual captures. REALITY instead enforces TLS1.3 natively while - # retaining the browser offer, exactly as the reference does. - shape["ciphers"] = [ - c for c in shape["ciphers"] if c in {"GREASE", 4865, 4866, 4867} - ] - shape["extensions"] = [ - e for e in shape["extensions"] if e["type"] not in {11, 23, 35, 65281} - ] - if context in {"ws", "xhttp-h1"}: - shape["extensions"] = [ - e for e in shape["extensions"] if e["type"] not in {17513, 17613} - ] - for extension in shape["extensions"]: - if extension["type"] == 43: - extension["values"] = [ - v - for v in extension["values"] - if v == "GREASE" or v >= (772 if tls13 else 771) - ] - extension["bytes"] = 1 + 2 * len(extension["values"]) - return shape - - -def check_captures(rows): - wanted = { - (p, c, s, a) for p in PROFILES for c in CONTEXTS for s in SNIS for a in range(2) - } - actual = [(r["profile"], r["context"], r["sni"], r["attempt"]) for r in rows] - if len(actual) != len(wanted) or set(actual) != wanted: - raise ValueError("incomplete or duplicate public-config wire matrix") - samples = json.loads( - (CORE_DIR / "tests/fingerprints/mihomo-selected-v1.json").read_text() - )["samples"] - baselines = {(s["profile"], s["context"]): s for s in samples} - results = [] - for row in rows: - profile, context = row["profile"], row["context"] - canonical = {"firefox120": "firefox", "safari16": "safari"}.get( - profile, profile - ) - reference = baselines[(canonical, "reality" if context == "reality" else "tcp")] - encoded = row["records_b64"] - if len(encoded) > 128 * 1024: - raise ValueError("wire capture size limit") - wire = base64.b64decode(encoded, validate=True) - records, raw, layout = capture_client_hello(io.BytesIO(wire).read) - if records != wire or any( - r["type"] != 22 or r["version"] != 769 for r in layout - ): - raise ValueError("unexpected initial TLS record envelope") - hello = parse_client_hello(raw) - alpn = ( - ["http/1.1"] - if context in {"ws", "xhttp-h1"} - else ["h2"] - if context == "xhttp-h2" - else ["h2", "http/1.1"] - ) - shape = _reference_shape( - hello, dict(row, template=TEMPLATES[profile]), expected_alpn=alpn - ) - expected = expected_shape(reference, context) - if shape != expected: - differences = { - k: dict(expected=expected.get(k), observed=shape.get(k)) - for k in expected.keys() | shape.keys() - if expected.get(k) != shape.get(k) - } - raise ValueError( - f"{profile}/{context}/{len(row['sni'])}/{row['attempt']}: " - f"{json.dumps(differences)}" - ) - results.append( - dict( - copy.deepcopy(row), - client_hello_b64=base64.b64encode(raw).decode(), - records=layout, - parsed=hello, - status="SHAPE VERIFIED", - ) - ) - return results - - -def check_warm_captures(rows): - wanted = { - (p, v, phase) - for p in ("chrome", "chrome120", "firefox", "safari") - for v in ("TLSv1_2", "TLSv1_3") - for phase in ("cold", "warm", "after-hint") - } - actual = [(r["profile"], r["version"], r["phase"]) for r in rows] - if len(actual) != len(wanted) or set(actual) != wanted: - raise ValueError("incomplete or duplicate warm/expiry matrix") - samples = json.loads( - (CORE_DIR / "tests/fingerprints/mihomo-selected-v1.json").read_text() - )["samples"] - baselines = {s["profile"]: s for s in samples if s["context"] == "tcp"} - results = [] - for row in rows: - profile, version, phase = row["profile"], row["version"], row["phase"] - stateful_id = profile == "safari" and version == "TLSv1_2" - resumed = phase == "warm" or (phase == "after-hint" and stateful_id) - if row["resumed"] != resumed: - raise ValueError("unexpected real peer resumption/expiry outcome") - wire = base64.b64decode(row["records_b64"], validate=True) - records, raw, layout = capture_client_hello(io.BytesIO(wire).read) - if wire[len(records) :] not in (b"", b"\x14\x03\x03\x00\x01\x01"): - raise ValueError("unexpected warm first flight beyond ClientHello/CCS") - hello = parse_client_hello(raw) - shape = _reference_shape( - hello, - dict(template=TEMPLATES[profile], sni="fixture.invalid", context="tcp"), - allow_psk=True, - ) - expected = expected_shape(baselines[profile], "tcp") - if resumed and not stateful_id: - length = row["prior_ticket_bytes"] - if not isinstance(length, int) or not 1 <= length <= 4096: - raise ValueError("independent peer did not issue a bounded ticket") - if version == "TLSv1_3": - expected["extensions"].append( - dict(type=41, bytes=length + 43, identities=[length], binders=[32]) - ) - else: - ticket = next(e for e in expected["extensions"] if e["type"] == 35) - ticket.update(bytes=length, ticket_bytes=length) - if shape != expected: - differences = { - k: dict(expected=expected.get(k), observed=shape.get(k)) - for k in expected.keys() | shape.keys() - if expected.get(k) != shape.get(k) - } - raise ValueError( - f"warm {profile}/{version}/{phase}: {json.dumps(differences)}" - ) - results.append( - dict( - row, - records_b64=base64.b64encode(records).decode(), - client_hello_b64=base64.b64encode(raw).decode(), - records=layout, - parsed=hello, - status="SHAPE VERIFIED", - ) - ) - return results - - -def run(output): - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("use a fresh directory under target/interop/runs") - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="FINGERPRINT-WIRE", - scope="memory-clienthello-structure", - source=source_identity(), - status="NOT RUN", - ) - try: - result = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "fingerprint_capture", - "--", - "--nocapture", - ], - timeout=180, - env=dict( - os.environ, VCORE_FINGERPRINT_CAPTURE=str(output / "captures.json") - ), - ) - (output / "command.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - report["cleanup"] = result.cleanup - if result.returncode or not result.cleanup: - raise RuntimeError("memory capture command failed") - report["cases"] = check_captures( - json.loads((output / "captures.json").read_text()) - ) - warm = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--lib", - "security::tls::tests::named_wire", - "--", - "--nocapture", - ], - timeout=180, - env=dict( - os.environ, VCORE_FINGERPRINT_WARM_CAPTURE=str(output / "warm.json") - ), - ) - (output / "warm-command.log").write_text( - redact(warm.stdout.decode(errors="replace")) - ) - report["cleanup"] = report["cleanup"] and warm.cleanup - if warm.returncode or not warm.cleanup: - raise RuntimeError("warm/expiry/version gate failed") - report["warm_cases"] = check_warm_captures( - json.loads((output / "warm.json").read_text()) - ) - report["status"] = "PASS" - except Exception as error: - report.update(status="FAIL", error=redact(str(error))) - finally: - report["source_after"] = source_identity() - report["source_unchanged"] = same_source( - report["source"], report["source_after"] - ) - if not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "shape-results.json").write_text(json.dumps(report, indent=2) + "\n") - print(report["status"], report.get("error", "")) - return 0 if report["status"] == "PASS" else 1 - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - raise SystemExit(run(parser.parse_args().output.resolve())) diff --git a/scripts/src/vcore_scripts/protocol_fixtures.py b/scripts/src/vcore_scripts/protocol_fixtures.py deleted file mode 100644 index d280ba7..0000000 --- a/scripts/src/vcore_scripts/protocol_fixtures.py +++ /dev/null @@ -1,181 +0,0 @@ -"""Shared synthetic certificates and peer configuration; never starts a listener.""" - -from __future__ import annotations - -import hashlib -from pathlib import Path - -from .protocol_peers import run_command - - -def certificates(directory: Path): - cert, key = directory / "cert.pem", directory / "key.pem" - generated = run_command( - [ - "openssl", - "req", - "-x509", - "-newkey", - "rsa:2048", - "-nodes", - "-days", - "2", - "-subj", - "/CN=localhost", - "-addext", - "subjectAltName=DNS:localhost", - "-keyout", - str(key), - "-out", - str(cert), - ], - timeout=20, - limit=65536, - ) - der = run_command( - ["openssl", "x509", "-in", str(cert), "-outform", "DER"], - timeout=10, - limit=65536, - ) - if ( - generated.returncode != 0 - or der.returncode != 0 - or not generated.cleanup - or not der.cleanup - ): - raise RuntimeError("synthetic certificate generation failed") - return cert, key, hashlib.sha256(der.stdout).hexdigest() - - -def certificate_chain(directory): - """Owned synthetic CA + leaf; root pin still verifies the leaf name.""" - root = directory / "root.pem" - root_key = directory / "root-key.pem" - key = directory / "key.pem" - csr = directory / "leaf.csr" - cert = directory / "cert.pem" - extensions = directory / "extensions.cnf" - extensions.write_text( - "basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth\nsubjectAltName=DNS:localhost\n" - ) - commands = [ - [ - "openssl", - "req", - "-x509", - "-newkey", - "rsa:2048", - "-nodes", - "-days", - "2", - "-subj", - "/CN=trojan-root", - "-addext", - "basicConstraints=critical,CA:TRUE", - "-keyout", - str(root_key), - "-out", - str(root), - ], - [ - "openssl", - "req", - "-new", - "-newkey", - "rsa:2048", - "-nodes", - "-subj", - "/CN=localhost", - "-keyout", - str(key), - "-out", - str(csr), - ], - [ - "openssl", - "x509", - "-req", - "-in", - str(csr), - "-CA", - str(root), - "-CAkey", - str(root_key), - "-set_serial", - "2", - "-days", - "2", - "-extfile", - str(extensions), - "-out", - str(cert), - ], - ["openssl", "x509", "-in", str(root), "-outform", "DER"], - ] - for command in commands: - result = run_command(command, timeout=20, limit=65536) - if result.returncode or not result.cleanup: - raise RuntimeError("synthetic chain generation failed") - cert.write_bytes(cert.read_bytes() + root.read_bytes()) - return cert, key, hashlib.sha256(result.stdout).hexdigest() - - -def trojan_peer_config(kind, mode, port, password, cert, key): - host = "::1" if mode.endswith("-ipv6") else "127.0.0.1" - mode = mode.removesuffix("-ipv6").removesuffix("-ca") - if kind == "M": - listener = { - "name": "trojan", - "type": "trojan", - "listen": host, - "port": port, - "users": [{"username": "fixture", "password": password}], - "certificate": str(cert), - "private-key": str(key), - } - if mode.startswith("ws"): - listener["ws-path"] = "cover.example/trojan-ws" - if mode.startswith("grpc"): - listener["grpc-service-name"] = "trojan-grpc" - if mode == "ws-alpn": - listener["grpc-service-name"] = "unrelated-service" - return { - "mode": "rule", - "log-level": "silent", - "ipv6": True, - "hosts": {"vcore-fixture.test": "127.0.0.1"}, - "listeners": [listener], - "rules": ["MATCH,DIRECT"], - } - stream = { - "network": "tcp", - "security": "tls", - "tlsSettings": { - "certificates": [{"certificateFile": str(cert), "keyFile": str(key)}] - }, - } - if mode.startswith("ws"): - stream.update(network="ws", wsSettings={"path": "/trojan-ws/"}) - stream["tlsSettings"]["alpn"] = ["http/1.1"] - if kind == "V2": - stream["wsSettings"].update( - maxEarlyData=2048, - earlyDataHeaderName="x-vcore-ed" if mode == "ws-header" else "", - ) - if mode == "grpc": - stream.update(network="grpc", grpcSettings={"serviceName": "trojan-grpc"}) - stream["tlsSettings"]["alpn"] = ["h2"] - return { - "log": {"loglevel": "none"}, - "dns": {"hosts": {"vcore-fixture.test": "127.0.0.1"}}, - "inbounds": [ - { - "listen": "127.0.0.1", - "port": port, - "protocol": "trojan", - "settings": {"clients": [{"password": password}]}, - "streamSettings": stream, - } - ], - "outbounds": [{"protocol": "freedom", "settings": {"domainStrategy": "UseIP"}}], - } diff --git a/scripts/src/vcore_scripts/protocol_harness.py b/scripts/src/vcore_scripts/protocol_harness.py deleted file mode 100644 index 5606b4c..0000000 --- a/scripts/src/vcore_scripts/protocol_harness.py +++ /dev/null @@ -1,701 +0,0 @@ -"""Unified foundation and implemented protocol-consumer acceptance runner.""" - -from __future__ import annotations - -import contextlib -import json -import os -import signal -import sys -import tempfile -import time -from datetime import UTC, datetime -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo import _run_mihomo_interop -from .mihomo_isolation import exclusive_run -from .protocol_catalogs import CATALOG_DIR, check_protocol_catalogs -from .protocol_evidence import ( - check_limit_references, - check_run, - idle_resources, - load_manifest, - new_result, - read_events, - read_json, - rust_results, - select_cases, - validate_results, -) -from .protocol_inputs import redact, run_identity, sha256, source_identity -from .protocol_peers import run_command -from .protocol_preflight import preflight -from .protocol_streams import run_streams - - -@contextlib.contextmanager -def deadline(seconds: float): - if not hasattr(signal, "SIGALRM"): - raise RuntimeError("whole-suite watchdog requires a supported Unix test host") - previous = signal.getsignal(signal.SIGALRM) - - def expired(*_): - raise TimeoutError("protocol suite deadline expired") - - signal.signal(signal.SIGALRM, expired) - previous_timer = signal.getitimer(signal.ITIMER_REAL) - started = time.monotonic() - signal.setitimer( - signal.ITIMER_REAL, - min(seconds, previous_timer[0]) if previous_timer[0] > 0 else seconds, - ) - try: - yield - finally: - remaining = ( - max(0.001, previous_timer[0] - (time.monotonic() - started)) - if previous_timer[0] > 0 - else 0 - ) - signal.setitimer(signal.ITIMER_REAL, remaining, previous_timer[1]) - signal.signal(signal.SIGALRM, previous) - - -def _events(path: Path) -> list[dict]: - if not path.exists(): - return [] - return read_events(path) - - -def _command( - run, - output: Path, - name: str, - command: list[str], - seconds: int, - *, - events: Path | None = None, -): - env = dict(os.environ) - env.pop("VCORE_CASE_EVENTS", None) - if events: - env["VCORE_CASE_EVENTS"] = str(events) - print(f"{run.get('stage', 'FOUNDATIONS')}: {name}", flush=True) - result = run_command( - command, cwd=CORE_DIR, env=env, timeout=seconds, limit=4 * 1024 * 1024 - ) - log = output / f"{name}.log" - log.write_text(redact(result.stdout.decode("utf-8", errors="replace"))) - run["commands"].append( - { - "name": name, - "command": [redact(str(part)) for part in command], - "exit_code": result.returncode, - "seconds": result.seconds, - "cleanup": result.cleanup, - "reason": result.reason, - "log": log.name, - } - ) - return result - - -def _features(case, run, output): - started = time.monotonic() - results = [] - names = [ - None, - "outbound-trojan", - "outbound-vmess", - "outbound-hysteria2", - "stream-transport", - "quic-transport", - ] - for name in names: - command = ["cargo", "check", "--locked", "--no-default-features", "--lib"] - if name: - command.extend(["--features", name]) - results.append( - _command( - run, - output, - "feature-" + (name or "minimal"), - command, - max(1, int(case["timeout_seconds"] - (time.monotonic() - started))), - ) - ) - results.append( - _command( - run, - output, - "feature-default", - ["cargo", "test", "--locked", "--test", "feature_foundations"], - max(1, int(case["timeout_seconds"] - (time.monotonic() - started))), - ) - ) - cargo = __import__("tomllib").loads((CORE_DIR / "Cargo.toml").read_text()) - for name in names[1:]: - pending = [name] - enabled = set() - while pending: - item = pending.pop() - if item not in enabled: - enabled.add(item) - pending.extend(cargo["features"].get(item, [])) - if enabled & {"outbound-vless", "tun"}: - raise RuntimeError( - "foundation feature silently enables an old protocol or TUN" - ) - passed = all(item.returncode == 0 and item.cleanup for item in results) - result = new_result(case) - result.update( - status="PASS" if passed else "FAIL", - assertions=dict.fromkeys(case["expected_observation"], passed), - evidence=case["required_evidence"] if passed else [], - cleanup=all(item.cleanup for item in results), - command_exit_code=0 if passed else 1, - ) - return result - - -SCRIPT_OBSERVATIONS = { - "offline-success": ( - "test_protocol_peers.OwnedPeerTest." - "test_success_is_bounded_and_records_exit_and_cleanup" - ), - "missing-duplicate-unknown-case": ( - "test_protocol_evidence.ProtocolEvidenceTest." - "test_missing_duplicate_unknown_and_empty_required_cases_fail" - ), - "download-failure": ( - "test_native_release.NativeReleaseTest." - "test_failed_download_never_uses_old_binary_or_leaks_network_details" - ), - "timeout": ( - "test_protocol_peers.OwnedPeerTest." - "test_timeout_and_output_overflow_fail_and_join" - ), - "sigint": ( - "test_protocol_peers.OwnedPeerTest." - "test_real_sigint_joins_nested_peer_without_stopping_unrelated_peer" - ), - "partial-start": ( - "test_protocol_peers.OwnedPeerTest." - "test_partial_start_and_permission_failure_do_not_leave_a_process" - ), - "permission": ( - "test_protocol_peers.OwnedPeerTest." - "test_partial_start_and_permission_failure_do_not_leave_a_process" - ), - "peer-exit": ("test_protocol_peers.OwnedPeerTest.test_peer_exit_is_not_readiness"), - "cleanup-failure": ( - "test_protocol_peers.OwnedPeerTest.test_cleanup_failure_is_explicit" - ), - "redaction": ( - "test_native_release.NativeReleaseTest." - "test_failed_download_never_uses_old_binary_or_leaks_network_details" - ), -} - - -def _scripts(case, run, output): - path = output / "script-tests.json" - command = _command( - run, - output, - "offline-scripts", - [sys.executable, "-m", "vcore_scripts.protocol_script_tests", str(path)], - case["timeout_seconds"], - ) - data = read_json(path) - tests = {entry["test"]: entry["status"] for entry in data["cases"]} - assertions = { - name: tests.get(identifier) == "PASS" - for name, identifier in SCRIPT_OBSERVATIONS.items() - } - passed = ( - command.returncode == 0 - and all(assertions.values()) - and len(tests) == len(data["cases"]) == data["tests_run"] - ) - result = new_result(case) - result.update( - status="PASS" if passed else "FAIL", - assertions=assertions, - evidence=case["required_evidence"] if passed else [], - cleanup=command.cleanup, - command_exit_code=command.returncode, - ) - return result - - -def _legacy(case, run, output, artifacts): - result = new_result(case) - if "M" not in artifacts or "M-container" not in artifacts: - result.update( - status="BLOCKED", - reason="M native/container prerequisites unavailable", - cleanup=True, - ) - return result - path = output / "legacy-events.jsonl" - previous = os.environ.get("VCORE_CASE_EVENTS") - os.environ["VCORE_CASE_EVENTS"] = str(path) - started = time.monotonic() - code = 1 - cleaned = False - try: - # The existing orchestrator owns exact peers/containers through ExitStack. - # SIGINT/timeout unwinds it in this process, not by orphaning a child CLI. - with ( - (output / "legacy.log").open("w") as log, - contextlib.redirect_stdout(log), - deadline(case["timeout_seconds"]), - ): - _run_mihomo_interop( - artifacts["M"].binary, - extended=True, - soak_seconds=0, - container_binary=artifacts["M-container"].binary, - ) - code = 0 - cleaned = True - except Exception as error: - result.update(status="FAIL", reason=type(error).__name__) - finally: - if previous is None: - os.environ.pop("VCORE_CASE_EVENTS", None) - else: - os.environ["VCORE_CASE_EVENTS"] = previous - run["commands"].append( - { - "name": "legacy-mihomo", - "command": ["check", "mihomo-interop", "--container", "--extended"], - "exit_code": code, - "seconds": round(time.monotonic() - started, 3), - "cleanup": cleaned, - } - ) - if (output / "legacy.log").exists(): - log = (output / "legacy.log").read_text() - (output / "legacy.log").write_text(redact(log)) - result = rust_results(case, _events(path), code) - result["cleanup"] = cleaned - if result["status"] == "PASS": - result["evidence"] = case["required_evidence"] - return result - - -def _native_results(cases, report): - actual = {case["case_id"]: case for case in report.get("cases", [])} - results = [] - for case in cases: - result = new_result(case) - source = actual.get(case["case_id"], {}) - event = source.get("rust_event", {}) - assertions = { - "server_first": event.get("server_first") is True, - "payload_65536": event.get("payload_bytes") == 65536, - "trailer_exact": event.get("tail_bytes") == 14, - "origin_complete": source.get("origin") - == {"finished": True, "accepted": 1, "received": 65536, "failed": False}, - "protect_once": event.get("protect_calls") == 1, - "driver_joined": event.get("driver_joined") is True, - "resources_idle": event.get("resources_idle") is True - and idle_resources(event.get("resources")), - } - passed = ( - source.get("status") == "PASS" - and all(assertions.values()) - and source.get("cleanup", {}).get("joined") is True - and report.get("cleanup") is True - ) - if case["peer_kind"] == "M": - cleanup = report.get("abnormal_cleanup", {}) - passed = passed and cleanup == { - "passed": True, - "kind": "M", - "failure_injected": True, - "joined": True, - "port_rebound": True, - } - result.update( - status="PASS" if passed else source.get("status", "NOT RUN"), - assertions=assertions, - evidence=case["required_evidence"] if passed else [], - cleanup=source.get("cleanup", {}).get("joined") is True - and report.get("cleanup") is True, - command_exit_code=source.get("command_exit_code"), - seconds=source.get("seconds"), - ) - if not passed and result["status"] == "PASS": - result["status"] = "FAIL" - results.append(result) - return results - - -def _execute(selected, run, output, records): - rust = [case for case in selected if case["runner"] == "rust"] - if rust: - path = output / "rust-events.jsonl" - result = _command( - run, - output, - "rust-foundations", - ["cargo", "test", "--locked", "--all-features", "--all-targets"], - max(case["timeout_seconds"] for case in rust), - events=path, - ) - events = _events(path) - for case in rust: - records[case["case_id"]] = rust_results(case, events, result.returncode) - records[case["case_id"]]["cleanup"] = result.cleanup - kinds = {case["peer_kind"] for case in selected if case["peer_kind"] != "unit"} - artifacts, peers = preflight( - output / "binaries", - kinds, - container=any(case["runner"] == "mihomo-legacy" for case in selected), - ) - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - native = [case for case in selected if case["runner"] == "native-stream"] - if native: - build = _command( - run, - output, - "native-probe-build", - [ - "cargo", - "build", - "--locked", - "--all-features", - "--example", - "protocol-stream-probe", - ], - 300, - ) - if build.returncode == 0: - report = {} - try: - report = run_streams( - output / "native", - [case["case_id"] for case in native], - artifacts=artifacts, - ) - except (OSError, ValueError, RuntimeError): - if (output / "native/stream-cases.json").exists(): - report = read_json(output / "native/stream-cases.json") - for result in _native_results(native, report): - records[result["case_id"]] = result - for case in selected: - if case["runner"] == "features": - records[case["case_id"]] = _features(case, run, output) - elif case["runner"] == "scripts": - records[case["case_id"]] = _scripts(case, run, output) - elif case["runner"] == "mihomo-legacy": - records[case["case_id"]] = _legacy(case, run, output, artifacts) - - -def run_protocol_interop( - *, - stage: str, - identifiers=None, - protocol=None, - list_only=False, - preflight_only=False, - run_dir: Path | None = None, -) -> Path | None: - selected = select_cases( - load_manifest(), stage=stage, identifiers=identifiers, protocol=protocol - ) - check_limit_references(load_manifest()) - if list_only: - for case in selected: - print( - f"{case['case_id']}\t{case['substage']}\t{case['peer_kind']}\t{case['protocol']}" - ) - return None - if stage in {"FOUNDATIONS", "TROJAN"}: - raise RuntimeError( - "legacy server orchestration is not fully containerized; " - "use check core or a current container suite" - ) - check_protocol_catalogs(CATALOG_DIR) - root = CORE_DIR / "target/interop/runs" - root.mkdir(parents=True, exist_ok=True) - if run_dir is None: - output = Path(tempfile.mkdtemp(prefix=f"{stage.lower()}-", dir=root)) - else: - output = run_dir.resolve() - if not output.is_relative_to(root.resolve()): - raise ValueError( - "run directory must be a fresh child of target/interop/runs" - ) - output.mkdir(parents=True, exist_ok=False) - print(f"Protocol evidence: {output.relative_to(CORE_DIR.resolve())}", flush=True) - (output / "manifest.json").write_text( - json.dumps( - { - "schema_version": 1, - "kind": "executable-case-manifest", - "cases": load_manifest(), - }, - indent=2, - ) - + "\n" - ) - run = { - "schema_version": 1, - "stage": stage, - "mode": "preflight" if preflight_only else "execute", - "selected_cases": [case["case_id"] for case in selected], - "commands": [], - "cleanup": False, - "source_unchanged": False, - } - records = {case["case_id"]: new_result(case) for case in selected} - (output / "peers.json").write_text("[]\n") - error = None - try: - run.update(run_identity(stage, selected, preflight_only)) - with exclusive_run(), deadline(run["suite_timeout_seconds"]): - if preflight_only and stage == "HTTPUPGRADE": - from .protocol_httpupgrade_acceptance import ( - preflight as upgrade_preflight, - ) - - upgrade_preflight(output) - elif preflight_only and stage == "TUIC": - from .protocol_tuic_acceptance import preflight as tuic_preflight - - tuic_preflight(output) - elif preflight_only and stage == "UOT": - from .protocol_uot_acceptance import preflight as uot_preflight - - uot_preflight(output) - elif preflight_only and stage == "SHADOWTLS": - from .protocol_shadowtls_acceptance import ( - preflight as shadowtls_preflight, - ) - - shadowtls_preflight(output) - elif preflight_only and stage == "INTEGRATION": - from .protocol_integration_acceptance import ( - preflight as integration_preflight, - ) - - integration_preflight(output) - elif preflight_only and stage == "SECURITY": - _, peers = preflight( - output / "binaries", {"M", "XR", "V2"}, container=True - ) - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - if any( - peer["status"] != "READY" or peer.get("container_status") != "READY" - for peer in peers - ): - raise RuntimeError("SECURITY native prerequisites are BLOCKED") - elif preflight_only and stage == "HYSTERIA2": - from .protocol_hysteria2_acceptance import preflight as preflight_hy2 - - preflight_hy2(output) - elif preflight_only and stage == "XHTTP": - from .protocol_xhttp_acceptance import preflight as preflight_xhttp - - preflight_xhttp(output) - elif preflight_only and stage in {"VMESS", "VLESS"}: - if stage == "VMESS": - from .protocol_vmess_container import run as preflight_vmess - else: - from .protocol_vless_container import run as preflight_vmess - protocol_name = "vmess" if stage == "VMESS" else "vless" - - native = [ - case["case_id"] - for case in selected - if case["runner"] == "native-" + protocol_name - ] - if preflight_vmess(output / "native", native, preflight_only=True): - raise RuntimeError("container protocol preflight failed") - peers = read_json(output / f"native/{protocol_name}-results.json")[ - "peers" - ] - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - elif preflight_only: - _, peers = preflight( - output / "binaries", {"M", "V2", "H", "XR"}, container=True - ) - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - if any( - peer["status"] != "READY" - or peer.get("container_status", "READY") != "READY" - for peer in peers - ): - raise RuntimeError( - "some independent peer prerequisites are BLOCKED" - ) - else: - if stage == "TROJAN": - from .protocol_trojan_acceptance import execute - - execute(selected, run, output, records) - elif stage == "VMESS": - from .protocol_vmess_acceptance import execute - - execute(selected, run, output, records) - elif stage == "VLESS": - from .protocol_vless_acceptance import execute - - execute(selected, run, output, records) - elif stage == "XHTTP": - from .protocol_xhttp_acceptance import execute - - execute(selected, run, output, records) - elif stage == "HYSTERIA2": - from .protocol_hysteria2_acceptance import execute - - execute(selected, run, output, records) - elif stage == "SECURITY": - from .protocol_security_acceptance import execute - - execute(selected, run, output, records) - elif stage == "INTEGRATION": - from .protocol_integration_acceptance import execute - - execute(selected, run, output, records) - elif stage == "SHADOWTLS": - from .protocol_shadowtls_acceptance import execute - - execute(selected, run, output, records) - elif stage == "UOT": - from .protocol_uot_acceptance import execute - - execute(selected, run, output, records) - elif stage == "TUIC": - from .protocol_tuic_acceptance import execute - - execute(selected, run, output, records) - elif stage == "HTTPUPGRADE": - from .protocol_httpupgrade_acceptance import execute - - execute(selected, run, output, records) - else: - _execute(selected, run, output, records) - except BaseException as caught: - error = caught - run["failure_kind"] = type(caught).__name__ - finally: - results = list(records.values()) - run["finished_utc"] = datetime.now(UTC).isoformat() - try: - current = source_identity() - run["source_unchanged"] = current == {key: run.get(key) for key in current} - except (OSError, RuntimeError, ValueError): - run["source_unchanged"] = False - run["identity_failure"] = True - run["cleanup"] = error is None and ( - preflight_only or all(result["cleanup"] for result in results) - ) - (output / "cases.json").write_text(json.dumps(results, indent=2) + "\n") - resources = [] - try: - for path in output.rglob("*-events.jsonl"): - for event in _events(path): - if event.get("resources") is not None: - resources.append(event) - if (output / "native/stream-cases.json").exists(): - for case in read_json(output / "native/stream-cases.json")["cases"]: - if case.get("rust_event", {}).get("resources") is not None: - resources.append( - { - "case_id": case["case_id"], - "phase": "after-stop", - "resources": case["rust_event"]["resources"], - } - ) - except (OSError, ValueError, KeyError, TypeError) as caught: - error = error or caught - run.update(cleanup=False, failure_kind=type(error).__name__) - (output / "resources.jsonl").write_text( - "".join(json.dumps(item) + "\n" for item in resources) - ) - status = ( - "PREFLIGHT" - if preflight_only - else "PASS" - if error is None - and run["source_unchanged"] - and all(result["status"] == "PASS" for result in results) - else "FAIL" - ) - (output / "summary.md").write_text( - f"# {stage}: {status}\n\n" - + ( - "Trojan protocol consumer evidence; other protocols remain NOT RUN.\n\n" - if stage == "TROJAN" - else "VMess protocol consumer evidence with " - "container-only servers and origins.\n\n" - if stage == "VMESS" - else "VLESS consumer evidence with container-only peers; " - "XHTTP and security remain separate suites.\n\n" - if stage == "VLESS" - else "VLESS XHTTP/sing-mux consumer evidence; " - "container-only native peers, security remains a separate suite.\n\n" - if stage == "XHTTP" - else "Hysteria2 consumer evidence with container-only peers; " - "native H server limits and platform/device gates remain explicit.\n\n" - if stage == "HYSTERIA2" - else "Selected VLESS security consumers; static ECH only, " - "container-only peers; integration, platforms and devices " - "remain separate.\n\n" - if stage == "SECURITY" - else "Seven-protocol integration; container-only peers. " - "Partial selections do not sign off lifecycle or soak gates.\n\n" - if stage == "INTEGRATION" - else "SS2022 ShadowTLS v3; official Mihomo and native peers " - "in isolated containers. " - "UoT and device delivery remain separate gates.\n\n" - if stage == "SHADOWTLS" - else "SS2022 UoT v2; TCP-only official peers in isolated containers. " - "Native unsupported peers are negative cases; " - "devices remain separate.\n\n" - if stage == "UOT" - else "TUIC v5 TCP/native UDP/quic UDP; isolated official Mihomo. " - "Shared Hysteria2 and XHTTP H3 regressions included; " - "devices remain separate.\n\n" - if stage == "TUIC" - else "VMess/Trojan HTTPUpgrade normal/fast-open; " - "official isolated peers and existing transport regressions. " - "Device delivery is separate.\n\n" - if stage == "HTTPUPGRADE" - else "Foundation-only evidence; " - "production protocol field consumers remain NOT RUN.\n\n" - ) - + "\n".join( - f"- {result['case_id']}: {result['status']}" for result in results - ) - + "\n" - ) - run["artifacts"] = [ - {"path": str(path.relative_to(output)), "sha256": sha256(path)} - for path in sorted(output.rglob("*")) - if path.is_file() - and path.suffix in {".json", ".jsonl", ".log", ".md"} - and "binaries" not in path.parts - and path.name != "run.json" - ] - (output / "run.json").write_text(json.dumps(run, indent=2) + "\n") - if error is not None: - raise RuntimeError( - f"protocol run incomplete ({type(error).__name__}); " - f"see {output.relative_to(CORE_DIR.resolve())}" - ) from None - if not preflight_only: - if not run["source_unchanged"]: - raise RuntimeError( - "source changed during the run; evidence cannot sign off this input" - ) - validate_results(selected, list(records.values())) - if not identifiers and protocol is None: - check_run(output, stage) - return output diff --git a/scripts/src/vcore_scripts/protocol_httpupgrade.py b/scripts/src/vcore_scripts/protocol_httpupgrade.py deleted file mode 100644 index c95dcf8..0000000 --- a/scripts/src/vcore_scripts/protocol_httpupgrade.py +++ /dev/null @@ -1,554 +0,0 @@ -"""Public HTTPUpgrade consumers against owned official release containers.""" - -from __future__ import annotations - -import contextlib -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import download_native -from .protocol_completion_peers import CASES, peer_configuration -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_evidence import read_events -from .protocol_fixtures import certificates -from .protocol_hysteria2_catalog import assertions_pass -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command -from .protocol_security_acceptance import rust_command - -CHECKS = ("tcp", "udp", "udp-domain", "early-data", "profiles", "negative", "close") - - -def selection(checks): - rows = [] - for case in CASES: - if case.protocol not in {"vmess", "trojan"}: - continue - if checks == "udp-domain" and case.protocol != "trojan": - continue - if checks == "profiles" and not case.tls: - continue - variants = ( - ("raw", "xudp", "packetaddr") - if checks == "udp" and case.protocol == "vmess" - else ("raw",) - if checks == "udp" - else ("domain",) - if checks == "udp-domain" - else ("ed1", "ed2048") - if checks == "early-data" - else ("chrome", "chrome120", "firefox", "safari") - if checks == "profiles" - else ("identity", "path") + (("certificate",) if case.tls else ()) - if checks == "negative" - else ("",) - ) - for variant in variants: - consumer = ( - "udp" - if checks.startswith("udp") - else "echo_and_stop" - if checks in {"early-data", "profiles"} - else "rejected" - if checks == "negative" - else checks - ) - rows.append( - ( - case.identifier + ("-" + variant if variant else ""), - case, - variant, - consumer, - ) - ) - return rows - - -def configure(node, checks, variant): - if checks == "udp" and node["type"] == "vmess": - node["packet-encoding"] = "" if variant == "raw" else variant - elif checks == "early-data": - node["ws-opts"].update( - {"max-early-data": int(variant[2:]), "path": "/protocol-upgrade?probe=once"} - ) - elif checks == "profiles": - node["client-fingerprint"] = variant - elif checks == "negative": - if variant == "identity": - node["uuid" if node["type"] == "vmess" else "password"] = ( - "08080808-0808-0808-0808-080808080808" - ) - elif variant == "certificate": - node["fingerprint"] = "00" * 32 - elif variant == "path": - node["ws-opts"]["path"] = "/wrong" - - -def families(node, kind): - return ( - ["domain"] - if kind == "XR" - else ["ipv4", "ipv6"] - if node["type"] == "trojan" - else ["ipv4", "ipv6", "domain"] - ) - - -def expected_observation(consumer, node=None, kind="M"): - if consumer == "echo_and_stop": - return dict(authenticated_echo=True, live_stream_cancelled=True, stop_idle=True) - if consumer == "rejected": - return dict(origin_connected=False, received_business_bytes=0, stop_idle=True) - if consumer == "tcp": - return dict( - families=["ipv4", "ipv6", "domain"], - tcp_bytes_each_family_each_direction=10 * 1024 * 1024, - server_first=True, - client_first=True, - stop_idle=True, - ) - if consumer == "udp": - vmess = node["type"] == "vmess" - rows = [] - for family in families(node, kind): - cap = ( - 15000 - - ( - (7 if family == "ipv4" else 19) - if node.get("packet-encoding") == "packetaddr" - else 0 - ) - if vmess - else 8166 - if kind == "XR" - else 8192 - ) - sizes = ([] if vmess or kind == "XR" else [0]) + [1, 64, 512, 1200, cap] - rows.append(dict(family=family, sizes=sizes, packets=len(sizes) * 100)) - return dict( - udp=rows, - zero_no_delivery=vmess or kind == "XR", - oversize_rejected=True, - tcp_sibling=True, - stop_idle=True, - ) - raise ValueError("unknown HTTPUpgrade consumer") - - -def measure_close(lab, root, output, binary, origin, node, name): - directory = root / (name + "-reference") - directory.mkdir() - shutil.copy2(binary, directory / "peer") - reference = dict(node, name="reference") - (directory / "config.json").write_text( - json.dumps( - { - "socks-port": 23002, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "proxies": [reference], - "rules": ["MATCH,reference"], - } - ) - ) - with contextlib.ExitStack() as stack: - peer = lab.start( - stack, - directory, - name + "-reference", - ["/data/fixture/peer", "-d", "/data", "-f", "/data/fixture/config.json"], - ) - peer.release() - peer.wait_tcp(23002) - measured = run_command( - [ - "container", - "exec", - origin.name, - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/close.py", - peer.ipv4, - origin.ipv4, - ], - timeout=30, - ) - (output / (name + "-close-command.log")).write_text( - redact(measured.stdout.decode(errors="replace")) - ) - if measured.returncode or not measured.cleanup: - raise RuntimeError("official HTTPUpgrade close comparison failed") - value = json.loads(measured.stdout) - if ( - set(value) != {"tail_hex", "terminated", "variant"} - or value["terminated"] is not True - or value["variant"] != "plain" - ): - raise RuntimeError("invalid official close observation") - (output / (name + "-reference.log")).write_text( - redact((directory / "peer.log").read_text(errors="replace")) - ) - (output / (name + "-close-reference.json")).write_text(json.dumps(value) + "\n") - return value - - -def events_pass(events, consumer): - expected = {("HTTPUPGRADE-PUBLIC", consumer): 1} - if consumer == "tcp": - expected[("HTTPUPGRADE-BASE", "tcp_10mib_both_directions")] = 3 - return assertions_pass(events, expected) - - -def native_configuration(rows, certificate, origin): - cert, key, _ = certificate - return dict( - log={"loglevel": "warning"}, - dns={"hosts": {"vcore-fixture.test": origin}}, - inbounds=[ - dict( - listen="::", - port=node["port"], - protocol="trojan", - settings={"clients": [{"password": node["password"]}]}, - streamSettings={ - "network": "httpupgrade", - "security": "tls", - "tlsSettings": { - "alpn": ["http/1.1"], - "certificates": [ - { - "certificateFile": "/data/fixture/" + cert.name, - "keyFile": "/data/fixture/" + key.name, - } - ], - }, - "httpupgradeSettings": { - "path": "/protocol-upgrade", - "host": "localhost", - }, - }, - ) - for _, node, _, _ in rows - ], - outbounds=[dict(protocol="freedom", settings={"domainStrategy": "UseIPv4"})], - ) - - -def run(output, *, checks="tcp", selected=None, supplied=None, image=None): - if checks not in CHECKS: - raise ValueError("unknown HTTPUpgrade check") - kind = "XR" if checks == "udp-domain" else "M" - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("HTTPUpgrade evidence must stay in target/interop/runs") - cases = selection(checks) - if selected is not None: - if ( - not selected - or len(set(selected)) != len(selected) - or not set(selected) <= {c[0] for c in cases} - ): - raise ValueError("unknown or repeated HTTPUpgrade selection") - cases = [c for c in cases if c[0] in selected] - output.mkdir(parents=True, exist_ok=False) - report = dict( - source=source_identity(), - status="NOT RUN", - peer={}, - peer_kind=kind, - isolation={}, - cases=[], - ) - try: - with ( - exclusive_run() if supplied is None else contextlib.nullcontext(), - frozen_image(output / "image-pull.log") - if image is None - else contextlib.nullcontext(image) as image, - ): - report["container_image"] = image - if supplied is None and kind == "M": - binary = download_mihomo( - "linux-arm64", directory=output / "binary", identity=report["peer"] - ) - elif supplied is None: - artifact = download_native( - kind, output / "binary", "linux-arm64", defer_version=True - ) - binary, report["peer"] = artifact.binary, artifact.identity - else: - binary, report["peer"] = supplied - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "--no-run", - ], - cwd=CORE_DIR, - timeout=180, - ) - (output / "build.log").write_text( - redact(built.stdout.decode(errors="replace")) - ) - if built.returncode or not built.cleanup: - raise RuntimeError("HTTPUpgrade consumer build failed") - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as tmp: - root = Path(tmp) - directories = {role: root / role for role in ("origin", "server")} - for directory in directories.values(): - directory.mkdir() - certificate = certificates(directories["server"]) - shutil.copy2(binary, directories["server"] / "peer") - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - directories["origin"] / "origin.py", - ) - shutil.copy2( - Path(__file__).with_name("container_close_client.py"), - directories["origin"] / "close.py", - ) - try: - with contextlib.ExitStack() as stack: - origin = lab.start( - stack, - directories["origin"], - "origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - directories["server"], - "server", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ], - ) - report["peer"]["version"] = command( - "exec", - server.name, - "/data/fixture/peer", - "-v" if kind == "M" else "version", - ).strip() - if ( - command( - "exec", server.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - != report["peer"]["binary_sha256"] - ): - raise RuntimeError("HTTPUpgrade peer identity mismatch") - rows = [] - for i, (name, case, variant, consumer) in enumerate(cases): - node, listener = peer_configuration( - case, - server=server.ipv4, - cover="", - port=23000 + i, - certificate=certificate, - ) - node["name"] = "peer" - listener["name"] = name - configure(node, checks, variant) - rows.append( - ( - name, - node, - listener, - consumer, - ) - ) - (directories["server"] / "config.json").write_text( - json.dumps( - { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "hosts": {"vcore-fixture.test": origin.ipv4}, - "listeners": [r[2] for r in rows], - "rules": ["MATCH,DIRECT"], - } - if kind == "M" - else native_configuration( - rows, certificate, origin.ipv4 - ) - ) - ) - checked = run_command( - [ - "container", - "exec", - server.name, - "/data/fixture/peer", - "-t", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "container", - "exec", - server.name, - "/data/fixture/peer", - "run", - "-test", - "-c", - "/data/fixture/config.json", - ], - timeout=30, - ) - (output / "peer-config.log").write_text( - redact(checked.stdout.decode(errors="replace")) - ) - if checked.returncode or not checked.cleanup: - raise RuntimeError("HTTPUpgrade peer configuration failed") - origin.release() - origin.wait_tcp(24000) - server.release() - server.wait_tcp(23999 if kind == "M" else rows[0][1]["port"]) - for name, node, _, consumer in rows: - reference = ( - measure_close( - lab, root, output, binary, origin, node, name - ) - if consumer == "close" - else None - ) - fixture = root / "fixture.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - node=node, - peer_kind=kind, - families=families(node, kind), - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - origin_control=f"{origin.ipv4}:24000", - data_dir=str(root / "data"), - close_reference=reference, - ) - ) - ) - observations = output / (name + "-observations.json") - events = output / (name + "-events.jsonl") - argv = rust_command( - "vless_public", "httpupgrade::" + consumer - ) - result = run_command( - argv, - cwd=CORE_DIR, - timeout=240, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_HTTPUPGRADE_OBSERVATIONS=str(observations), - VCORE_PROTOCOL_STAGE="HTTPUPGRADE", - VCORE_CASE_EVENTS=str(events), - ), - ) - (output / (name + ".log")).write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = ( - json.loads(observations.read_text()) - if observations.is_file() - else None - ) - good = ( - result.returncode == 0 - and result.cleanup - and observed - == ( - dict( - tail_hex=reference["tail_hex"], - terminated=True, - stop_idle=True, - ) - if consumer == "close" - else expected_observation(consumer, node, kind) - ) - and events_pass(read_events(events), consumer) - ) - report["cases"].append( - dict( - case_id=name, - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - observations=observed, - status="PASS" if good else "FAIL", - ) - ) - print(name + (": PASS" if good else ": FAIL"), flush=True) - if not good: - raise RuntimeError("HTTPUpgrade consumer failed") - finally: - for role, directory in directories.items(): - if (directory / "peer.log").is_file(): - (output / (role + ".log")).write_text( - redact( - (directory / "peer.log").read_text(errors="replace") - ) - ) - report["status"] = "PASS" - except BaseException as error: - report["status"] = "FAIL" - (output / "failure.log").write_text(redact(str(error))) - raise - finally: - report["cleanup"] = all( - p.get("joined") is True for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - if report["status"] != "PASS": - raise RuntimeError("HTTPUpgrade source or cleanup failed") - return report - - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("--case", action="append") - parser.add_argument("--checks", choices=CHECKS, default="tcp") - args = parser.parse_args() - run(args.output, checks=args.checks, selected=args.case) diff --git a/scripts/src/vcore_scripts/protocol_httpupgrade_acceptance.py b/scripts/src/vcore_scripts/protocol_httpupgrade_acceptance.py deleted file mode 100644 index b6e4a17..0000000 --- a/scripts/src/vcore_scripts/protocol_httpupgrade_acceptance.py +++ /dev/null @@ -1,253 +0,0 @@ -"""Run HTTPUpgrade gates and independently reconstruct persisted acceptance.""" - -from __future__ import annotations - -import json - -from .core_checks import validate_execution -from .protocol_evidence import read_events, read_json -from .protocol_httpupgrade import events_pass, expected_observation -from .protocol_httpupgrade_catalog import ( - LOCAL, - REGRESSION, - UNIT, - WIRE, - commands, - wire_cases, -) -from .protocol_hysteria2_catalog import assertions_pass -from .protocol_inputs import redact -from .protocol_integration_checks import envelope -from .protocol_security_acceptance import result, rust_command -from .protocol_shadowtls_acceptance import source - - -def wire_pass(name, directory, report): - expected = wire_cases(name) - rows = report.get("cases", []) - if [r.get("case_id") for r in rows] != [r[0] for r in expected]: - return False - for row, (identifier, consumer) in zip(rows, expected, strict=True): - observed = read_json(directory / (identifier + "-observations.json")) - if consumer == "close": - reference = read_json(directory / (identifier + "-close-reference.json")) - raw = json.loads( - (directory / (identifier + "-close-command.log")).read_text() - ) - if ( - reference != raw - or raw.get("terminated") is not True - or raw.get("variant") != "plain" - ): - return False - wanted = dict(tail_hex=raw["tail_hex"], terminated=True, stop_idle=True) - else: - node = dict(type="vmess" if identifier.startswith("vmess") else "trojan") - node["packet-encoding"] = identifier.rsplit("-", 1)[-1] - wanted = expected_observation( - consumer, node, "XR" if name == "UDP-DOMAIN" else "M" - ) - if not ( - row.get("command") - == rust_command("vless_public", "httpupgrade::" + consumer) - and type(row.get("exit_code")) is int - and row["exit_code"] == 0 - and row.get("command_cleanup") is True - and observed == row.get("observations") == wanted - and events_pass( - read_events(directory / (identifier + "-events.jsonl")), consumer - ) - ): - return False - return True - - -def local_pass(name, directory, run): - rows = [ - r for r in run["commands"] if r["name"].startswith("HTTPUPGRADE-" + name + "-") - ] - expected = commands(name) - if len(rows) != len(expected): - return False - events = [] - for i, (row, argv) in enumerate(zip(rows, expected, strict=True)): - label = f"HTTPUPGRADE-{name}-{i}" - if row["name"] != label or row["command"] != [redact(v) for v in argv]: - return False - validate_execution( - row["command"], - (directory / row["log"]).read_text(), - row["exit_code"], - row["cleanup"], - ) - path = directory / (label + "-events.jsonl") - if path.exists(): - events += read_events(path) - return name not in {"MEMORY", "FEATURES"} or assertions_pass( - [e for e in events if e.get("suite", "").startswith("HTTPUPGRADE-")], - {("HTTPUPGRADE-UNIT", n): 1 if name == "MEMORY" else 2 for n in UNIT}, - ) - - -def regression_pass(name, directory, report): - from .protocol_vless_public import PUBLIC - from .protocol_vless_public import events_pass as vless_events - from .protocol_vmess_public import CASES - from .protocol_vmess_public import events_pass as vmess_events - - selected = REGRESSION[name] - rows = report.get("cases", []) - if [r.get("case_id") for r in rows] != selected: - return False - for row in rows: - identifier = row["case_id"] - kind, mode, _, consumer = (PUBLIC if name == "VLESS" else CASES)[identifier] - observed = read_events(directory / (identifier + "-events.jsonl")) - if not ( - row.get("peer_kind") == kind - and row.get("command") == rust_command(name.lower() + "_public", consumer) - and type(row.get("exit_code")) is int - and row["exit_code"] == 0 - and row.get("command_cleanup") is True - and row.get("cleanup") is True - and ( - vless_events(observed, consumer, mode) - if name == "VLESS" - else vmess_events(observed, consumer) - ) - ): - return False - return True - - -def group_pass(name, directory, run, peers): - if name in LOCAL: - return local_pass(name, directory, run) - root = directory / name.lower() - report = read_json( - root / ("report.json" if name in WIRE else name.lower() + "-results.json") - ) - return envelope( - report, - peers, - source(run), - run["container_image"]["digest"], - single=("XR" if name == "UDP-DOMAIN" else "M") if name in WIRE else None, - ) and ( - wire_pass(name, root, report) - if name in WIRE - else regression_pass(name, root, report) - ) - - -def execute(cases, run, output, results): - from .protocol_containers import frozen_image - from .protocol_harness import _command - from .protocol_httpupgrade import run as upgrade - from .protocol_hysteria2_acceptance import prepare - from .protocol_vless_container import run as vless - from .protocol_vmess_container import run as vmess - - selected = {c["case_id"].removeprefix("HTTPUPGRADE-"): c for c in cases} - peers = {} - - def save(name): - good = group_pass(name, output, run, peers) - case = selected[name] - results[case["case_id"]] = result(case, good, good) - print(case["case_id"] + (": PASS" if good else ": FAIL"), flush=True) - if not good: - raise RuntimeError("HTTPUpgrade gate failed: " + name) - - try: - for name in selected: - if name not in LOCAL: - continue - for i, argv in enumerate(commands(name)): - label = f"HTTPUPGRADE-{name}-{i}" - completed = _command( - run, - output, - label, - argv, - 3600, - events=output / (label + "-events.jsonl"), - ) - validate_execution( - argv, - completed.stdout.decode(errors="replace"), - completed.returncode, - completed.cleanup, - ) - save(name) - if set(selected) <= LOCAL: - return - with frozen_image(output / "image-pull.log") as image: - run["container_image"] = image - kinds = ( - {"M"} - | ({"XR"} if "UDP-DOMAIN" in selected else set()) - | ({"V2"} if "VMESS" in selected else set()) - ) - supplied = prepare(output, kinds) - peers.update({k: v[1] for k, v in supplied.items()}) - for name in selected: - if name in LOCAL: - continue - root = output / name.lower() - if name in WIRE: - upgrade( - root, - checks=name.lower(), - supplied=supplied["XR" if name == "UDP-DOMAIN" else "M"], - image=image, - ) - elif name == "VLESS": - vless(root, selected=REGRESSION[name], supplied=supplied) - elif name == "VMESS": - vmess(root, selected=REGRESSION[name], supplied=supplied) - save(name) - finally: - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - - -def check(directory, cases, run, results, peers, paths): - if not {"cases.json", "peers.json", "resources.jsonl", "summary.md"} <= set(paths): - raise ValueError("missing HTTPUpgrade evidence") - expected = [ - f"{c['case_id']}-{i}" - for c in cases - for i, _ in enumerate(commands(c["case_id"].removeprefix("HTTPUPGRADE-"))) - ] - actual = [r["name"] for r in run["commands"]] - if ( - len(actual) != len(set(actual)) - or set(actual) != set(expected) - or set(peers) != {"M", "XR", "V2"} - ): - raise ValueError("HTTPUpgrade command or peer selection differs") - for case in cases: - name = case["case_id"].removeprefix("HTTPUPGRADE-") - if not group_pass(name, directory, run, peers): - raise ValueError("HTTPUpgrade raw evidence failed: " + name) - print("HTTPUpgrade coverage: PASS (all required groups; device delivery separate)") - - -def preflight(output): - from .protocol_containers import command, frozen_image - from .protocol_hysteria2_acceptance import prepare - - command("system", "status") - with frozen_image(output / "image-pull.log"): - supplied = prepare(output, {"M", "XR", "V2"}) - (output / "peers.json").write_text( - json.dumps( - dict( - scope="download-only", - wire_acceptance="NOT RUN", - peers={k: v[1] for k, v in supplied.items()}, - ), - indent=2, - ) - + "\n" - ) diff --git a/scripts/src/vcore_scripts/protocol_httpupgrade_catalog.py b/scripts/src/vcore_scripts/protocol_httpupgrade_catalog.py deleted file mode 100644 index 9e73659..0000000 --- a/scripts/src/vcore_scripts/protocol_httpupgrade_catalog.py +++ /dev/null @@ -1,174 +0,0 @@ -"""Independent required HTTPUpgrade consumers and regression selection.""" - -from .protocol_shadowtls_catalog import commands as shared_commands - -LOCAL = {"MEMORY", "FEATURES", "QUALITY", "PLATFORMS"} -WIRE = ("TCP", "UDP", "UDP-DOMAIN", "EARLY-DATA", "PROFILES", "NEGATIVE", "CLOSE") -GATES = ("MEMORY", "FEATURES", "QUALITY", "PLATFORMS", *WIRE, "VLESS", "VMESS") -BASES = tuple( - f"{protocol}-upgrade-{security}-{mode}" - for protocol, security in (("vmess", "plain"), ("vmess", "tls"), ("trojan", "tls")) - for mode in ("normal", "fast") -) -UNIT = ( - "config_dispatch", - "config_bounds", - "early_data", - "invalid_response", - "deadline_cancel", - "expired_deadline", - "late_response", - "late_write", - "established_io", -) -REGRESSION = { - "VLESS": [ - "VLESS-" + mode + "-BASE" - for mode in ( - "WS", - "WS-TLS", - "UPGRADE", - "UPGRADE-FAST", - "UPGRADE-TLS", - "UPGRADE-FAST-TLS", - ) - ] - + [ - "VLESS-REGRESSION-" + protocol + "-" + network - for protocol in ("VMESS", "TROJAN") - for network in ("TCP", "WS", "GRPC") - ], - "VMESS": ["VMESS-PUBLIC-" + mode + "-TLS-BASE" for mode in ("HTTP", "H2")], -} - - -def wire_cases(name): - rows = [] - for base in BASES: - if name == "UDP-DOMAIN" and not base.startswith("trojan"): - continue - if name == "PROFILES" and "-plain-" in base: - continue - variants = { - "TCP": ("",), - "CLOSE": ("",), - "UDP": ("raw", "xudp", "packetaddr") - if base.startswith("vmess") - else ("raw",), - "UDP-DOMAIN": ("domain",), - "EARLY-DATA": ("ed1", "ed2048"), - "PROFILES": ("chrome", "chrome120", "firefox", "safari"), - "NEGATIVE": ("identity", "path") - + (("certificate",) if "-tls-" in base else ()), - }[name] - consumer = { - "TCP": "tcp", - "CLOSE": "close", - "UDP": "udp", - "UDP-DOMAIN": "udp", - "EARLY-DATA": "echo_and_stop", - "PROFILES": "echo_and_stop", - "NEGATIVE": "rejected", - }[name] - rows += [(base + ("-" + v if v else ""), consumer) for v in variants] - return rows - - -def definitions(): - return [ - dict( - case_id="HTTPUPGRADE-" + name, - stage="HTTPUPGRADE", - substage="HTTPUPGRADE." - + ( - "configuration" - if name == "FEATURES" - else "stream" - if name == "MEMORY" - else "interop" - if name in WIRE - else "regression" - ), - required=True, - row_ids=[], - protocol="vmess/trojan", - network="httpupgrade", - security="plain/tls", - udp_codec="raw/xudp/packetaddr/trojan", - field_values={}, - outer_family="IPv4", - inner_family="IPv4/IPv6/domain", - target_type="memory-only/build" if name in LOCAL else "isolated origin", - upstream_graph="existing connector", - peer_kind="unit" - if name in LOCAL - else "XR" - if name == "UDP-DOMAIN" - else "V2" - if name == "VMESS" - else "M", - peer_config={"group": name}, - gap_source=None, - expected_observation=["HTTPUPGRADE-" + name], - required_evidence=["command", "structured-assertions", "cleanup"] - + ([] if name in LOCAL else ["peer-identity", "isolated-origins"]), - prerequisites=["rust-toolchain"] - if name in LOCAL - else ["owned-host-only-container-network", "official-latest-peer"], - runner="httpupgrade-gate", - timeout_seconds=3600, - ) - for name in GATES - ] - - -def commands(name): - cargo = ["cargo", "test", "--locked", "--all-features"] - if name == "MEMORY": - return [ - cargo - + [ - arg - for t in ( - "httpupgrade_config", - "httpupgrade_memory", - "vmess_lifecycle", - "vmess_config", - "trojan", - "trojan_config", - "vless_config", - "vless_lifecycle", - "stream_foundations", - "stream_shutdown", - ) - for arg in ("--test", t) - ] - ] - if name == "FEATURES": - return ( - [ - ["cargo", "check", "--locked", "--no-default-features"] - + (["--features", f] if f else []) - for f in ("", "stream-transport", "outbound-vmess", "outbound-trojan") - ] - + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - f + ",interop-test", - "--test", - "httpupgrade_config", - "--test", - "httpupgrade_memory", - ] - for f in ("outbound-vmess", "outbound-trojan") - ] - + [ - ["cargo", "test", "--locked", "--test", "feature_foundations"], - cargo + ["--all-targets", "--no-run"], - ] - ) - return shared_commands(name) if name in {"QUALITY", "PLATFORMS"} else [] diff --git a/scripts/src/vcore_scripts/protocol_hysteria2.py b/scripts/src/vcore_scripts/protocol_hysteria2.py deleted file mode 100644 index b1a885a..0000000 --- a/scripts/src/vcore_scripts/protocol_hysteria2.py +++ /dev/null @@ -1,480 +0,0 @@ -"""HYSTERIA2 incremental native gates; all servers/origins stay in owned containers.""" - -from __future__ import annotations - -import contextlib -import copy -import hashlib -import json -import os -import shutil -import ssl -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_release import download_mihomo -from .protocol_containers import ContainerLab, command -from .protocol_evidence import read_events -from .protocol_fixtures import certificate_chain, certificates -from .protocol_hysteria2_catalog import events_pass -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vless_security import client_identities - - -def security_cases(base, identities, leaf_pin): - cases = [] - - def case(name, changes=None, *, absent=(), success=True): - node = copy.deepcopy(base) - node.update(changes or {}) - for field in absent: - node.pop(field, None) - cases.append(dict(id=name, node=node, success=success)) - - case("root-pin") - case("leaf-pin", {"fingerprint": leaf_pin}) - case("root-pin-wrong-name", {"sni": "wrong.fixture.test"}, success=False) - case("leaf-pin-is-trust", {"fingerprint": leaf_pin, "sni": "wrong.fixture.test"}) - case("unknown-ca", absent=("fingerprint",), success=False) - case("explicit-skip", {"skip-cert-verify": True}, absent=("fingerprint",)) - case("wrong-pin", {"fingerprint": "00" * 32}, success=False) - case( - "skip-does-not-override-pin", - {"fingerprint": "00" * 32, "skip-cert-verify": True}, - success=False, - ) - case("wrong-password", {"password": "not-the-password"}, success=False) - case("empty-password", {"port": 23005}, absent=("password",)) - case("raw-password", {"port": 23006, "password": " untrimmed credential "}) - case("empty-alpn-default", {"alpn": []}) - case("custom-alpn", {"port": 23007, "alpn": ["h3-fixture"]}) - case("wrong-alpn", {"alpn": ["wrong-fixture"]}, success=False) - case("mtls-valid", {"port": 23008, **identities["valid"]}) - case("mtls-absent", {"port": 23008}, success=False) - for identity in ("expired", "wrong-ca"): - case("mtls-" + identity, {"port": 23008, **identities[identity]}, success=False) - obfs = {"obfs": "salamander", "obfs-password": "independent-obfs-fixture"} - case("salamander", {"port": 23009, **obfs}) - case("salamander-client-only", obfs, success=False) - case("salamander-server-only", {"port": 23009}, success=False) - case( - "salamander-wrong-key", - {"port": 23009, **obfs, "obfs-password": "wrong-obfs-key"}, - success=False, - ) - case( - "salamander-wrong-auth", - {"port": 23009, **obfs, "password": "wrong-auth"}, - success=False, - ) - return cases - - -def run(output: Path, test="hysteria2_tcp_base", *, obfs=False, supplied=None): - if test not in { - "hysteria2_tcp_base", - "hysteria2_client_first", - "hysteria2_udp_base", - "native_bandwidth_matrix", - "native_mihomo_close_alignment", - "native_security_matrix", - "native_owned_lifecycle", - "native_deadline_and_udp_budget", - "runtime::public_lifecycle", - "runtime::public_ipv6_and_gates", - "runtime::public_entrypoints", - "hysteria2::public_udp_boundaries", - "hysteria2::public_concrete_upstream", - "hysteria2::public_graph_and_hop_snapshot", - }: - raise ValueError("unknown HYSTERIA2 test") - output = output.resolve() - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="HYSTERIA2", - scope="incremental-native", - source=source_identity(), - status="NOT RUN", - isolation={}, - peers={}, - cases=[], - obfs=obfs, - ) - try: - if supplied is None: - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "binaries", identity=identity - ) - else: - binary, identity = supplied - if ( - hashlib.sha256(binary.read_bytes()).hexdigest() - != identity["binary_sha256"] - ): - raise RuntimeError("supplied native peer identity mismatch") - report["peers"]["M"] = identity - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary: - root = Path(temporary) - origin_dir, server_dir = root / "origin", root / "server" - origin_dir.mkdir() - server_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - shutil.copyfile( - Path(__file__).with_name("container_close_client.py"), - origin_dir / "close.py", - ) - shutil.copy2(binary, server_dir / "peer") - cert, key, pin = ( - certificate_chain if test == "native_security_matrix" else certificates - )(server_dir) - with contextlib.ExitStack() as stack: - origin = lab.start( - stack, - origin_dir, - "hysteria2-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - server_dir, - "hysteria2-mihomo", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - identity["version"] = command( - "exec", server.name, "/data/fixture/peer", "-v" - ).strip() - digest = command( - "exec", server.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if digest != identity["binary_sha256"]: - raise RuntimeError("native peer identity mismatch") - upstream = server - if test in { - "hysteria2::public_concrete_upstream", - "hysteria2::public_graph_and_hop_snapshot", - }: - # A separate native instance prevents its own outer UDP - # source tuple from tripping Mihomo's loopback detector. - hop_dir = root / "hop" - hop_dir.mkdir() - shutil.copy2(binary, hop_dir / "peer") - (hop_dir / "config.json").write_text( - json.dumps( - { - "socks-port": 23001, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "silent", - "rules": ["MATCH,DIRECT"], - } - ) - ) - upstream = lab.start( - stack, - hop_dir, - "hysteria2-hop", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - upstream.release() - upstream.wait_tcp(23001) - password = "synthetic-hysteria2-fixture" - (server_dir / "config.json").write_text( - json.dumps( - { - "socks-port": 23001, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "silent", - "hosts": {"vcore-fixture.test": origin.ipv4}, - "listeners": [ - { - "name": "hysteria2", - "type": "hysteria2", - "listen": "::", - "port": 23000, - "users": {"fixture": password}, - "certificate": f"/data/fixture/{cert.name}", - "private-key": f"/data/fixture/{key.name}", - } - ], - "rules": ["MATCH,DIRECT"], - } - ) - ) - fixture = root / "input.json" - bandwidth_peer = None - if test == "native_bandwidth_matrix": - bandwidth_dir = root / "bandwidth" - bandwidth_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_bandwidth_origin.py"), - bandwidth_dir / "origin.py", - ) - bandwidth_peer = lab.start( - stack, - bandwidth_dir, - "hysteria2-bandwidth", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - bandwidth_peer.release() - bandwidth_peer.wait_tcp(24002) - peer_config = json.loads((server_dir / "config.json").read_text()) - baseline = peer_config["listeners"][0] - for port, options in [ - (23002, {"down": "1 Mbps"}), - (23003, {"up": "1 Mbps"}), - (23004, {"ignore-client-bandwidth": True}), - ]: - peer_config["listeners"].append( - dict( - baseline, name=f"hysteria2-{port}", port=port, **options - ) - ) - (server_dir / "config.json").write_text(json.dumps(peer_config)) - fixture.write_text( - json.dumps( - dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - server_ipv6=server.ipv6, - data_dir=str(root / "data"), - hop=dict( - name="hop", - type="socks5", - server=upstream.ipv4, - port=23001, - udp=True, - ), - bandwidth_control=f"{bandwidth_peer.ipv4}:24002" - if bandwidth_peer - else None, - bandwidth_ipv4=bandwidth_peer.ipv4 - if bandwidth_peer - else None, - node=dict( - name="peer", - type="hysteria2", - server=server.ipv4, - port=23000, - password=password, - udp=True, - sni="localhost", - fingerprint=pin, - alpn=["h3"], - ), - ) - ) - ) - if test == "native_security_matrix": - identities = client_identities(server_dir) - peer_config = json.loads((server_dir / "config.json").read_text()) - baseline = peer_config["listeners"][0] - for port, options in [ - (23005, {"users": {"fixture": ""}}), - (23006, {"users": {"fixture": " untrimmed credential "}}), - (23007, {"alpn": ["h3-fixture"]}), - ( - 23008, - { - "client-auth-type": "require-and-verify", - "client-auth-cert": "/data/fixture/root.pem", - }, - ), - ( - 23009, - { - "obfs": "salamander", - "obfs-password": "independent-obfs-fixture", - }, - ), - ]: - peer_config["listeners"].append( - dict( - baseline, name=f"hysteria2-{port}", port=port, **options - ) - ) - (server_dir / "config.json").write_text(json.dumps(peer_config)) - value = json.loads(fixture.read_text()) - leaf = ( - cert.read_text().split("-----END CERTIFICATE-----", 1)[0] - + "-----END CERTIFICATE-----\n" - ) - leaf_pin = hashlib.sha256( - ssl.PEM_cert_to_DER_cert(leaf) - ).hexdigest() - value["security"] = security_cases( - value["node"], identities, leaf_pin - ) - value["invalid_identities"] = [ - { - "certificate": identities["valid"]["certificate"], - "private-key": identities["expired"]["private-key"], - }, - { - "certificate": "bad PEM", - "private-key": identities["valid"]["private-key"], - }, - ] - fixture.write_text(json.dumps(value)) - if obfs: - peer_config = json.loads((server_dir / "config.json").read_text()) - for listener in peer_config["listeners"]: - listener.update( - obfs="salamander", - **{"obfs-password": "independent-obfs-fixture"}, - ) - (server_dir / "config.json").write_text(json.dumps(peer_config)) - value = json.loads(fixture.read_text()) - value["node"].update( - obfs="salamander", - **{"obfs-password": "independent-obfs-fixture"}, - ) - fixture.write_text(json.dumps(value)) - origin.release() - server.release() - origin.wait_tcp(24000) - server.wait_tcp(23001) - if test == "native_mihomo_close_alignment": - from .protocol_xhttp_fields import close_reference - - value = json.loads(fixture.read_text()) - value["close_reference"] = close_reference( - lab, stack, root, "hy2", value["node"], origin, binary, output - ) - fixture.write_text(json.dumps(value)) - events = output / f"{test}-events.jsonl" - result = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "hysteria2_native" - if test.startswith("native_") - else "vless_public", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ], - cwd=CORE_DIR, - timeout=600 if test == "native_bandwidth_matrix" else 240, - limit=4 * 1024 * 1024, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_CASE_EVENTS=str(events), - VCORE_PROTOCOL_STAGE="HYSTERIA2", - VCORE_BANDWIDTH_OBSERVATIONS=str(output / "bandwidth.jsonl"), - ), - ) - (output / f"{test}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = read_events(events) if events.exists() else [] - passed = ( - result.returncode == 0 - and result.cleanup - and events_pass(observed, test) - ) - report["cases"].append( - dict( - case_id=test, - status="PASS" if passed else "FAIL", - exit_code=result.returncode, - seconds=result.seconds, - command_cleanup=result.cleanup, - command=test_command(test), - ) - ) - report["status"] = "PASS" if passed else "FAIL" - for role, peer in (("origin", origin), ("server", server)): - peer.ensure_alive() - if peer.log.exists(): - (output / f"{role}.log").write_text( - redact(peer.log.read_text(errors="replace")[-65536:]) - ) - except (OSError, RuntimeError, ValueError) as error: - report.update( - status="FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - finally: - report["cleanup"] = all( - peer["joined"] for peer in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - if not report["source_unchanged"] or not report["cleanup"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - return report - - -def test_command(test): - return [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "hysteria2_native" if test.startswith("native_") else "vless_public", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - - -if __name__ == "__main__": - import sys - - result = run( - Path(sys.argv[1]), - sys.argv[2] if len(sys.argv) > 2 else "hysteria2_tcp_base", - obfs="--salamander" in sys.argv[3:], - ) - print(json.dumps({key: result[key] for key in ("status", "cleanup", "cases")})) - raise SystemExit(0 if result["status"] == "PASS" and result["cleanup"] else 1) diff --git a/scripts/src/vcore_scripts/protocol_hysteria2_acceptance.py b/scripts/src/vcore_scripts/protocol_hysteria2_acceptance.py deleted file mode 100644 index b3c1fa1..0000000 --- a/scripts/src/vcore_scripts/protocol_hysteria2_acceptance.py +++ /dev/null @@ -1,670 +0,0 @@ -"""Hysteria2 execution and fail-closed reconstruction of persisted evidence.""" - -from __future__ import annotations - -import contextlib -import json -import sys - -from .builds import DEFAULT_FEATURES -from .protocol_evidence import HEX, new_result, read_events, read_json -from .protocol_hysteria2_catalog import ( - CFG, - FIELDS, - GATES, - H3, - UNIT, - H, - M, - assertions_pass, - definitions, - events_pass, -) -from .protocol_inputs import redact - - -def commands(identifier): - cargo = ["cargo", "test", "--locked", "--all-features"] - if identifier in {"HYSTERIA2-CFG", "HYSTERIA2-UNIT", "HYSTERIA2-RELEASE"}: - if identifier == "HYSTERIA2-RELEASE": - cargo += ["--release"] - result = [] - if identifier != "HYSTERIA2-UNIT": - result.append(cargo + ["--test", "hysteria2_config"]) - if identifier != "HYSTERIA2-CFG": - result += [ - cargo + ["--lib", "outbound::hysteria2::"], - cargo - + [ - "--lib", - "outbound::connector::tests::authenticated_continuation_keeps_group_choice_but_has_a_new_io_deadline", - "--", - "--exact", - ], - cargo + ["--test", "hysteria2_paths"], - ] - return result - if identifier == "HYSTERIA2-REGRESSION": - # Every chosen test uses memory IO/config, never a host server. - targets = [ - "feature_foundations", - "limit_foundations", - "trojan", - "trojan_config", - "vmess_config", - "vless_config", - "vless_lifecycle", - "xhttp_config", - "xhttp_budget", - "h2_stream_regression", - "stream_foundations", - ] - return [ - cargo + sum((["--test", name] for name in targets), []), - cargo - + [ - "--test", - "quic_datagram_foundations", - "pending_send_is_not_restarted_and_stop_cancels_without_waiting_for_writable", - "--", - "--exact", - ], - ] - if identifier == "HYSTERIA2-QUALITY": - from .protocol_vmess_acceptance import commands as previous - - return previous("VMESS-QUALITY")[:7] + [ - [ - "cargo", - "clippy", - "--locked", - "--all-features", - "--all-targets", - "--", - "-D", - "warnings", - ], - [ - "cargo", - "test", - "--locked", - "--manifest-path", - "crates/vcore-netstack/Cargo.toml", - "--all-targets", - ], - [ - "cargo", - "clippy", - "--locked", - "--manifest-path", - "crates/vcore-netstack/Cargo.toml", - "--all-targets", - "--", - "-D", - "warnings", - ], - ] - if identifier == "HYSTERIA2-PLATFORMS": - return [ - ["vcore-scripts", "build", "apple"], - ["vcore-scripts", "build", "android"], - ] - if identifier == "HYSTERIA2-FEATURES": - checks = [ - ["cargo", "check", "--locked", "--no-default-features", "--lib"] - + (["--features", feature] if feature else []) - for feature in ( - "", - "outbound-hysteria2", - "quic-transport", - "outbound-vless", - "tun", - ) - ] - return checks + [ - [ - "cargo", - "test", - "--locked", - "--test", - "feature_foundations", - "--test", - "hysteria2_config", - ], - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--test", - "hysteria2_config", - ], - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "outbound-hysteria2", - "--test", - "hysteria2_config", - ], - [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ], - ] - if identifier == "HYSTERIA2-SCRIPTS": - return [ - [ - sys.executable, - "-m", - "vcore_scripts.protocol_script_tests", - "{output}/script-tests.json", - ] - ] - return [] - - -def gate_result(case, records, events, script=None): - identifier = case["case_id"] - good = bool(records) and all( - r.get("exit_code") == 0 and r.get("cleanup") is True for r in records - ) - if identifier in {"HYSTERIA2-CFG", "HYSTERIA2-UNIT", "HYSTERIA2-RELEASE"}: - expected = {} - if identifier != "HYSTERIA2-UNIT": - expected.update({("HYSTERIA2-CFG", name): 1 for name in CFG}) - if identifier != "HYSTERIA2-CFG": - expected.update({("HYSTERIA2-UNIT", name): 1 for name in UNIT}) - good &= assertions_pass(events, expected) - if identifier == "HYSTERIA2-SCRIPTS": - required = { - "test_protocol_hysteria2.Hysteria2AcceptanceTest." + name - for name in ( - "test_catalog_covers_all_fields_and_eight_hop_combinations", - "test_partial_unknown_duplicate_and_wrong_suite_events_fail", - "test_native_identity_cleanup_or_missing_results_fail", - "test_bandwidth_and_hop_counters_cannot_be_forged_as_empty_success", - ) - } - samples = (script or {}).get("cases", []) - names = [s.get("test") for s in samples] - good &= ( - bool(samples) - and len(names) == len(set(names)) == (script or {}).get("tests_run") - and required <= set(names) - and all(s.get("status") == "PASS" for s in samples) - ) - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions=dict.fromkeys(case["expected_observation"], bool(good)), - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=bool(records) and all(r.get("cleanup") is True for r in records), - ) - return result - - -def bandwidth_pass(samples): - expected = [ - (d, u, down, port, controller) - for d in ("U", "D") - for u, down, port, controller in ( - (0, 0, 23000, 0), - *( - (rate, 100, 23000, rate * 125000) if d == "U" else (0, rate, 23000, 0) - for rate in (1, 2) - ), - (2, 100, 23002, 125000) if d == "U" else (0, 2, 23003, 0), - ) - ] + [("U", 2, 0, 23000, 0), ("U", 2, 2, 23004, 0)] - if len(samples) != len(expected): - return False - for sample, key in zip(samples, expected, strict=True): - if ( - tuple( - sample.get(k) - for k in ( - "direction", - "up_mbps", - "down_mbps", - "listener_port", - "controller_bps", - ) - ) - != key - ): - return False - buckets = sample.get("buckets", []) - if ( - len(buckets) != 30 - or any(type(v) is not int or v < 0 for v in buckets) - or sample.get("payload_bps", 0) <= 0 - or sample["payload_bps"] != sum(buckets) / 30 - or sample.get("udp_tx_bytes", 0) <= 0 - or sample.get("udp_rx_bytes", 0) <= 0 - ): - return False - for offset in (0, 4): - rates = [samples[offset + i]["payload_bps"] for i in range(4)] - if ( - rates[0] < 750000 - or rates[1] > 125000 * 1.1 - or rates[2] > 250000 * 1.1 - or rates[2] < rates[1] * 1.5 - or rates[3] > 125000 * 1.1 - ): - return False - return True - - -def hopping_pass(value): - return ( - value.get("seconds", 0) >= 60 - and value.get("protected_sockets", 0) >= 9 - and value.get("tcp_bytes_per_direction", 0) >= 10 * 1024 * 1024 - and HEX.fullmatch(value.get("tcp_sha256", "")) is not None - and value.get("udp_packets") == 1500 - and value.get("udp_target_kinds") == 3 - and len(value.get("udp_maximums", [])) == 3 - and all(1200 < n < 4096 for n in value["udp_maximums"]) - and value.get("socket_peak") == 2 - ) - - -def native_result(case, report, directory): - from .protocol_hysteria2 import test_command - - identifier = case["case_id"] - test = M[identifier][0] if identifier in M else H[identifier]["test"] - records = report.get("cases", []) - events = directory / ( - f"{test}-events.jsonl" if identifier in M else "hysteria2-events.jsonl" - ) - observed = read_events(events) if events.exists() else [] - isolation = report.get("isolation", {}) - peers = isolation.get("peers", []) - clean = ( - report.get("cleanup") is True - and bool(peers) - and all(p.get("joined") is True and p.get("started") is True for p in peers) - ) - good = ( - clean - and report.get("status") == "PASS" - and report.get("source_unchanged") is True - and isolation.get("network_mode") == "hostOnly" - and isolation.get("host_servers") is False - and isolation.get("guest_mtu") == 1500 - and bool(isolation.get("image_digest")) - ) - good &= ( - len(records) == 1 - and all( - r.get("case_id") == test - and r.get("status") == "PASS" - and r.get("exit_code") == 0 - and r.get("command_cleanup") is True - and r.get("command") == test_command(test) - for r in records - ) - and events_pass(observed, test) - ) - if identifier in M: - good &= report.get("obfs") is M[identifier][1] - identity = report.get("peers", {}).get("M", {}) - project = "MetaCubeX/mihomo" - else: - identity = report.get("peer", {}) - project = "HyNetworks/hysteria" - good &= ( - all( - report.get(k) is H[identifier][k] - for k in ("ipv6", "obfs", "random_interval") - ) - and report.get("auth_connections") == 1 - ) - counts = report.get("entry_port_packets", {}) - good &= ( - set(counts) == {"p23010", "p23011", "p23012"} - and all(type(v) is int and v >= 0 for v in counts.values()) - and sum(v > 0 for v in counts.values()) - >= (2 if test == "native_hopping" else 1) - ) - if test == "native_hopping": - good &= (directory / "hopping.json").exists() and hopping_pass( - read_json(directory / "hopping.json") - ) - good &= ( - bool(identity.get("version")) - and HEX.fullmatch(identity.get("binary_sha256", "")) is not None - and identity.get("source_url", "").startswith( - f"https://github.com/{project}/releases/" - ) - ) - if test == "native_bandwidth_matrix": - path = directory / "bandwidth.jsonl" - good &= path.exists() and bandwidth_pass(read_events(path)) - if test == "native_mihomo_close_alignment": - path = directory / "hy2-close-reference.json" - reference = read_json(path) if path.exists() else {} - good &= reference == {"tail_hex": "", "terminated": True, "scope": "same-mode"} - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions={test: bool(good)}, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=clean, - ) - return result - - -def fields_report(cases, results): - status = {r["case_id"]: r["status"] for r in results} - rows = [] - for field in sorted(FIELDS): - native = [ - c["case_id"] - for c in cases - if field in c["row_ids"] and c["case_id"] in M | H - ] - good = ( - status.get("HYSTERIA2-CFG") == "PASS" - and bool(native) - and all(status.get(i) == "PASS" for i in native) - ) - rows.append( - dict( - row_id=field, - configuration="HYSTERIA2-CFG", - behavior_cases=native, - status="PASS" if good else "NOT RUN", - ) - ) - return dict(stage="HYSTERIA2", scope="hysteria2-consumer", fields=rows) - - -def prepare(output, kinds): - from .protocol_containers import ContainerLab - from .protocol_hysteria2_hop import native_binary, packages - from .protocol_xhttp_acceptance import prepare_peers - - supplied = prepare_peers(output, kinds - {"H"}) - if "H" in kinds: - root = output / "binaries/H" - root.mkdir(parents=True) - binary, identity = native_binary(root) - record = {} - try: - lab = ContainerLab(record, mtu=1500) - with contextlib.ExitStack() as stack: - apks = packages(lab, stack, root / "apks") - supplied["H"] = binary, identity, apks, record["firewall_packages"] - finally: - (output / "package-preparation.json").write_text( - json.dumps(record, indent=2) + "\n" - ) - return supplied - - -def h3_jobs(): - return { - name: [dict(case_id=name, test="public_base")] - for name in ( - "h3-stream-one-headers", - "h3-stream-up-headers", - "h3-packet-up-headers", - ) - } - - -def h3_result(case, report, directory): - from .protocol_xhttp_fields import public_events_pass - - jobs = h3_jobs() - records = report.get("cases", []) - isolation = report.get("isolation", {}) - owned = isolation.get("peers", []) - good = ( - report.get("status") == "PASS" - and report.get("cleanup") is True - and report.get("source_unchanged") is True - and {r.get("case_id") for r in records} == set(jobs) - and len(records) == len(jobs) - and isolation.get("network_mode") == "hostOnly" - and isolation.get("host_servers") is False - and isolation.get("guest_mtu") == 1500 - and bool(isolation.get("image_digest")) - and bool(owned) - and all(p.get("started") is True and p.get("joined") is True for p in owned) - and set(report.get("peers", {})) == {"M", "XR"} - ) - for record in records: - path = directory / (record["case_id"] + "-events.jsonl") - good &= ( - record.get("exit_code") == 0 - and record.get("command_cleanup") is True - and record.get("status") == "PASS" - and record.get("assertion") == "public_base" - and record.get("command") - == [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "public_base", - "--", - "--exact", - "--ignored", - "--nocapture", - ] - and path.exists() - and public_events_pass(read_events(path), "public_base") - ) - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions={case["expected_observation"][0]: bool(good)}, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=report.get("cleanup") is True, - ) - return result - - -def execute(cases, run, output, results): - from .protocol_harness import _command - from .protocol_hysteria2 import run as run_m - from .protocol_hysteria2_hop import run as run_h - - supplied = {} - try: - for case in cases: - identifier = case["case_id"] - if identifier not in GATES: - continue - records, events = [], [] - for index, argv in enumerate(commands(identifier)): - argv = [v.replace("{output}", str(output)) for v in argv] - name = f"{identifier}-{index}" - path = output / f"{name}-events.jsonl" - _command(run, output, name, argv, case["timeout_seconds"], events=path) - records.append(run["commands"][-1]) - if path.exists(): - events += read_events(path) - script = ( - read_json(output / "script-tests.json") - if identifier == "HYSTERIA2-SCRIPTS" - else None - ) - results[identifier] = gate_result(case, records, events, script) - if results[identifier]["status"] != "PASS": - raise RuntimeError(f"HYSTERIA2 local gate failed: {identifier}") - kinds = {"M" for c in cases if c["case_id"] in M} | { - "H" for c in cases if c["case_id"] in H - } - if any(c["case_id"] == H3 for c in cases): - kinds |= {"M", "XR"} - supplied = prepare(output, kinds) - for case in cases: - identifier = case["case_id"] - directory = output / identifier - if identifier in M: - test, obfs, _ = M[identifier] - report = run_m(directory, test, obfs=obfs, supplied=supplied["M"]) - result = native_result(case, report, directory) - elif identifier in H: - report = run_h(directory, **H[identifier], supplied=supplied["H"]) - result = native_result(case, report, directory) - elif identifier == H3: - from .protocol_xhttp_fields import run as run_h3 - - jobs = h3_jobs() - run_h3( - directory, - list(jobs), - jobs=jobs, - supplied={k: supplied[k] for k in ("M", "XR")}, - ) - report = read_json(directory / "xhttp-fields-results.json") - result = h3_result(case, report, directory) - else: - continue - results[identifier] = result - print(f"{identifier}: {result['status']}", flush=True) - if result["status"] != "PASS": - raise RuntimeError(f"HYSTERIA2 native gate failed: {identifier}") - finally: - (output / "peers.json").write_text( - json.dumps( - {kind: artifact[1] for kind, artifact in supplied.items()}, indent=2 - ) - + "\n" - ) - (output / "fields.json").write_text( - json.dumps(fields_report(cases, list(results.values())), indent=2) + "\n" - ) - - -def check(run_dir, cases, run, results, peers, paths): - if ( - cases != definitions() - or set(peers) != {"M", "H", "XR"} - or not { - "fields.json", - "script-tests.json", - "package-preparation.json", - } - <= set(paths) - ): - raise ValueError("incomplete HYSTERIA2 stage artifacts or altered manifest") - actual = {r["case_id"]: r for r in results} - records = run.get("commands", []) - expected_names = { - f"{c['case_id']}-{i}" - for c in cases - for i, _ in enumerate(commands(c["case_id"])) - } - if ( - len(records) != len(expected_names) - or {r.get("name") for r in records} != expected_names - ): - raise ValueError("missing, duplicate or extra HYSTERIA2 validation command") - source = { - k: run[k] - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - for case in cases: - identifier = case["case_id"] - if identifier in GATES: - selected, events = [], [] - for i, argv in enumerate(commands(identifier)): - record = next(r for r in records if r["name"] == f"{identifier}-{i}") - if ( - record["command"] - != [redact(v.replace("{output}", str(run_dir))) for v in argv] - or record["log"] not in paths - ): - raise ValueError("HYSTERIA2 validation command/log changed") - selected.append(record) - path = run_dir / f"{identifier}-{i}-events.jsonl" - if path.exists(): - events += read_events(path) - recalculated = gate_result( - case, - selected, - events, - read_json(run_dir / "script-tests.json") - if identifier == "HYSTERIA2-SCRIPTS" - else None, - ) - else: - directory = run_dir / identifier - report = read_json( - directory - / ("xhttp-fields-results.json" if identifier == H3 else "report.json") - ) - if report.get("source") != source: - raise ValueError("HYSTERIA2 native source differs from stage input") - recalculated = ( - h3_result(case, report, directory) - if identifier == H3 - else native_result(case, report, directory) - ) - if identifier in H: - if report.get("peer") != peers.get("H"): - raise ValueError("H native identity changed") - preparation = read_json(run_dir / "package-preparation.json") - if report.get("isolation", {}).get( - "firewall_packages" - ) != preparation.get("firewall_packages"): - raise ValueError("H native firewall package identity changed") - elif set(report.get("peers", {})) != ( - {"M", "XR"} if identifier == H3 else {"M"} - ): - raise ValueError("missing or extra native peer identity") - elif any( - identity != peers.get(kind) - for kind, identity in report.get("peers", {}).items() - ): - raise ValueError("native identity changed between reports") - if recalculated != actual[identifier] or recalculated["status"] != "PASS": - raise ValueError("HYSTERIA2 results differ from structured evidence") - preparation = read_json(run_dir / "package-preparation.json") - if ( - not preparation.get("firewall_packages") - or not preparation.get("peers") - or any(p.get("joined") is not True for p in preparation["peers"]) - ): - raise ValueError("native firewall preparation identity/cleanup missing") - fields = fields_report(cases, results) - if read_json(run_dir / "fields.json") != fields or any( - f["status"] != "PASS" for f in fields["fields"] - ): - raise ValueError("missing HYSTERIA2 field behavior coverage") - print(f"HYSTERIA2: PASS ({len(cases)} required cases; {len(FIELDS)} field rows)") - - -def preflight(output): - # The normal native gates prove readiness as well as protocol behavior; - # this command deliberately never fabricates a protocol PASS. - supplied = prepare(output, {"M", "H", "XR"}) - (output / "peers.json").write_text( - json.dumps({k: v[1] for k, v in supplied.items()}, indent=2) + "\n" - ) diff --git a/scripts/src/vcore_scripts/protocol_hysteria2_catalog.py b/scripts/src/vcore_scripts/protocol_hysteria2_catalog.py deleted file mode 100644 index b43a7ee..0000000 --- a/scripts/src/vcore_scripts/protocol_hysteria2_catalog.py +++ /dev/null @@ -1,329 +0,0 @@ -"""Frozen HYSTERIA2 requirements and exact structured assertion identities.""" - -from __future__ import annotations - -from collections import Counter - -from .protocol_evidence import idle_resources - -FIELDS = { - *(f"C{i:02}" for i in range(1, 7)), - "T01", - "T03", - "T04", - "T05", - "T07", - "T08", - *(f"HY{i:02}" for i in range(1, 9)), -} -CFG = [ - "hysteria2_configuration_follows_its_protocol_feature", - "hysteria2_accepts_the_approved_tls_bandwidth_obfs_and_hopping_configuration", - "hysteria2_fields_are_strict_normalized_and_credentials_are_not_trimmed", -] -UNIT = [ - "tcp_wire_matches_the_protocol_and_preserves_server_first_bytes", - "udp_wire_round_trips_at_budget_and_bounds_malformed_fragments", - "udp_reassembly_isolates_sources_duplicates_counts_bytes_and_expiry", - "salamander_matches_independent_blake2b256_vector_and_rejects_short_packets", - "pacer_limits_actual_bytes_after_one_bounded_burst", - "bandwidth_negotiation_obeys_auto_and_the_lower_positive_limit", - "application_eof_closes_both_directions_without_consuming_a_tail", - "shutdown_wakes_an_already_pending_logical_reader", - "a_client_first_write_does_not_steal_a_pending_read_wakeup", - "a_new_path_packet_does_not_end_the_old_receive_window", - "authenticated_continuation_keeps_group_choice_but_has_a_new_io_deadline", - "controlled_path_maps_only_its_authorized_source_and_stops_without_replay", -] -SECURITY = ( - "root-pin", - "leaf-pin", - "root-pin-wrong-name", - "leaf-pin-is-trust", - "unknown-ca", - "explicit-skip", - "wrong-pin", - "skip-does-not-override-pin", - "wrong-password", - "empty-password", - "raw-password", - "empty-alpn-default", - "custom-alpn", - "wrong-alpn", - "mtls-valid", - "mtls-absent", - "mtls-expired", - "mtls-wrong-ca", - "salamander", - "salamander-client-only", - "salamander-server-only", - "salamander-wrong-key", - "salamander-wrong-auth", -) -M = { - "HYSTERIA2-TCP": ( - "hysteria2_tcp_base", - False, - ["C01", "C02", "C03", "C04", "T01", "T03", "HY01"], - ), - "HYSTERIA2-UDP": ("hysteria2_udp_base", False, ["C05", "HY06"]), - "HYSTERIA2-TCP-SALAMANDER": ("hysteria2_tcp_base", True, ["HY02", "HY03"]), - "HYSTERIA2-UDP-SALAMANDER": ("hysteria2_udp_base", True, ["HY02", "HY03", "HY06"]), - "HYSTERIA2-SECURITY": ( - "native_security_matrix", - False, - ["T01", "T03", "T04", "T05", "T07", "T08", "HY01", "HY02", "HY03"], - ), - "HYSTERIA2-BANDWIDTH": ("native_bandwidth_matrix", False, ["HY04", "HY05"]), - "HYSTERIA2-CLOSE": ("native_mihomo_close_alignment", False, []), - "HYSTERIA2-CLOSE-SALAMANDER": ( - "native_mihomo_close_alignment", - True, - ["HY02", "HY03"], - ), - "HYSTERIA2-OWNED": ("native_owned_lifecycle", False, []), - "HYSTERIA2-DEADLINE": ("native_deadline_and_udp_budget", False, ["HY06"]), - "HYSTERIA2-LIFE": ("runtime::public_lifecycle", False, []), - "HYSTERIA2-ENTRYPOINTS": ("runtime::public_entrypoints", False, ["C05"]), - "HYSTERIA2-IPV6-GATES": ("runtime::public_ipv6_and_gates", False, ["C03", "C05"]), - "HYSTERIA2-UDP-BOUNDARIES": ("hysteria2::public_udp_boundaries", False, ["HY06"]), - "HYSTERIA2-UPSTREAM": ("hysteria2::public_concrete_upstream", False, ["C06"]), - "HYSTERIA2-GRAPH": ( - "hysteria2::public_graph_and_hop_snapshot", - False, - ["C06", "HY07", "HY08"], - ), -} -H = { - "-".join( - ( - "HYSTERIA2-HOP", - "V6" if v6 else "V4", - "OBFS" if obfs else "PLAIN", - "RANDOM" if random else "FIXED", - ) - ): dict(test="native_hopping", ipv6=v6, obfs=obfs, random_interval=random) - for v6 in (False, True) - for obfs in (False, True) - for random in (False, True) -} -H.update( - { - "HYSTERIA2-UDP-DISABLED": dict( - test="native_udp_disabled", ipv6=False, obfs=False, random_interval=False - ), - "HYSTERIA2-HOP-PROTECT": dict( - test="native_hop_protect_rejection", - ipv6=False, - obfs=False, - random_interval=False, - ), - "HYSTERIA2-HOP-STOP": dict( - test="native_stop_during_hop", ipv6=True, obfs=True, random_interval=False - ), - } -) -GATES = { - "HYSTERIA2-CFG", - "HYSTERIA2-UNIT", - "HYSTERIA2-RELEASE", - "HYSTERIA2-REGRESSION", - "HYSTERIA2-QUALITY", - "HYSTERIA2-FEATURES", - "HYSTERIA2-SCRIPTS", - "HYSTERIA2-PLATFORMS", -} -H3 = "HYSTERIA2-H3-REGRESSION" - - -def expected_events(test): - public = { - "hysteria2_tcp_base": ("HYSTERIA2-BASE", "tcp_ipv4_ipv6_domain_and_measure"), - "hysteria2_udp_base": ("HYSTERIA2-BASE", "udp_ipv4_ipv6_domain_fragmentation"), - "hysteria2_client_first": ("HYSTERIA2-BASE", "client_first"), - } - if test in public: - expected = {public[test]: 1} - if test == "hysteria2_tcp_base": - expected["HYSTERIA2-BASE", "tcp_10mib_both_directions"] = 3 - return expected - if test.startswith("runtime::"): - result = {("HYSTERIA2-PUBLIC", test): 1} - if test == "runtime::public_lifecycle": - result["HYSTERIA2-LIFE", "stop_and_remain_quiet"] = 20 - return result - if test.startswith("hysteria2::"): - return {("HYSTERIA2-PUBLIC", test.removeprefix("hysteria2::")): 1} - if test == "native_security_matrix": - return { - ("HYSTERIA2-SECURITY", test): 1, - **{("HYSTERIA2-SECURITY-CASE", name): 1 for name in SECURITY}, - } - return { - ( - { - "native_bandwidth_matrix": "HYSTERIA2-BANDWIDTH", - "native_owned_lifecycle": "HYSTERIA2-LIFE", - "native_hopping": "HYSTERIA2-HOP", - "native_udp_disabled": "HYSTERIA2-UDP-DISABLED", - "native_hop_protect_rejection": "HYSTERIA2-HOP", - "native_stop_during_hop": "HYSTERIA2-HOP", - "native_deadline_and_udp_budget": "HYSTERIA2-NATIVE", - "native_mihomo_close_alignment": "HYSTERIA2-NATIVE", - }[test], - test, - ): 20 if test == "native_owned_lifecycle" else 1 - } - - -def assertions_pass(events, expected): - if not events or any( - e.get("schema_version") != 1 or e.get("status") not in {"BEGIN", "PASS"} - for e in events - ): - return False - actual = Counter((e.get("suite"), e.get("assertion")) for e in events) - if actual != Counter({key: count * 2 for key, count in expected.items()}): - return False - for key, count in expected.items(): - selected = [e for e in events if (e.get("suite"), e.get("assertion")) == key] - if [e["status"] for e in selected] != ["BEGIN", "PASS"] * count: - return False - for event in selected[1::2]: - if event.get("resources") is not None and not idle_resources( - event["resources"] - ): - return False - return True - - -def events_pass(events, test): - if not assertions_pass(events, expected_events(test)): - return False - passed = [e for e in events if e["status"] == "PASS"] - if test in { - "native_hopping", - "native_hop_protect_rejection", - "native_stop_during_hop", - "native_owned_lifecycle", - "native_deadline_and_udp_budget", - "native_security_matrix", - }: - owned = [e for e in passed if e["assertion"] == test] - if any(not idle_resources(e.get("resources")) for e in owned): - return False - if test == "native_owned_lifecycle" and any( - [p["phase"] for p in e["checkpoints"]] - != ["baseline", "after-stop", "quiet"] - or any(not idle_resources(p.get("resources")) for p in e["checkpoints"]) - or e["seconds"] < 5 - for e in owned - ): - return False - return True - - -def definitions(): - output = [] - for identifier in sorted(GATES | set(M) | set(H) | {H3}): - kind, test, rows, options = "unit", identifier, [], {} - if identifier in M: - test, obfs, rows = M[identifier] - kind, options = "M", dict(obfs=obfs) - elif identifier in H: - options = H[identifier] - kind, test = "H", options["test"] - rows = ["C04", "HY07", "HY08"] if test != "native_udp_disabled" else ["C05"] - elif identifier == H3: - kind, test = "XR", "shared-quic-h3-native-regression" - elif identifier == "HYSTERIA2-CFG": - rows = sorted(FIELDS) - native = kind != "unit" - substage = ( - "HYSTERIA2.hopping" - if kind == "H" and test != "native_udp_disabled" - else "HYSTERIA2.bandwidth" - if identifier == "HYSTERIA2-BANDWIDTH" - else "HYSTERIA2.udp" - if "UDP" in identifier - else "HYSTERIA2.tcp" - if identifier in M - else "HYSTERIA2.acceptance" - ) - expected = ( - CFG - if identifier == "HYSTERIA2-CFG" - else UNIT - if identifier == "HYSTERIA2-UNIT" - else CFG + UNIT - if identifier == "HYSTERIA2-RELEASE" - else [test] - ) - output.append( - dict( - case_id=identifier, - stage="HYSTERIA2", - substage=substage, - required=True, - row_ids=sorted(rows), - protocol="hysteria2" if identifier != H3 else "vless", - network="quic", - security="tls13", - udp_codec="hysteria2-datagram" if identifier != H3 else "vless", - field_values=options, - outer_family="IPv6" - if options.get("ipv6") - else "IPv4/IPv6" - if identifier == "HYSTERIA2-IPV6-GATES" - else "IPv4", - inner_family="IPv4/IPv6/domain", - target_type="isolated synthetic origin" - if native - else "memory-only/build", - upstream_graph="direct/concrete/nested-select" - if identifier in {"HYSTERIA2-GRAPH", "HYSTERIA2-UPSTREAM"} - else "direct", - peer_kind=kind, - peer_config=dict( - test=test, **{k: v for k, v in options.items() if k != "test"} - ), - gap_source=dict( - reason=( - "Mihomo HY2 listener cannot redirect one QUIC state " - "across a port set or disable authenticated business " - "UDP; official Hysteria provides these gates" - ), - source="https://github.com/HyNetworks/hysteria/blob/e1366b173ccf5706e1e4630fe8aa654a4b574085/core/server/server.go", - ) - if kind == "H" - else dict( - reason=( - "XHTTP H3 shared-adapter regression uses the XHTTP " - "official Xray handler" - ), - source="https://github.com/XTLS/Xray-core", - ) - if kind == "XR" - else None, - expected_observation=expected, - required_evidence=["structured-assertions", "command", "cleanup"] - + (["peer-identity", "isolated-origins"] if native else []), - prerequisites=[ - "owned-host-only-container-network", - "official-latest-peer", - ] - if native - else ["rust-toolchain"], - runner="native-hysteria2" if kind in {"M", "H"} else "hysteria2-gate", - timeout_seconds=3600 - if identifier == "HYSTERIA2-PLATFORMS" - else 900 - if identifier == H3 - else 1200 - if not native - else 600 - if identifier == "HYSTERIA2-BANDWIDTH" - else 300, - ) - ) - return output diff --git a/scripts/src/vcore_scripts/protocol_hysteria2_hop.py b/scripts/src/vcore_scripts/protocol_hysteria2_hop.py deleted file mode 100644 index 4a5252c..0000000 --- a/scripts/src/vcore_scripts/protocol_hysteria2_hop.py +++ /dev/null @@ -1,440 +0,0 @@ -"""Official Hysteria, with redirection confined to an owned Linux container.""" - -from __future__ import annotations - -import contextlib -import hashlib -import json -import os -import shutil -import tempfile -import time -import urllib.request -from pathlib import Path - -from .builds import CORE_DIR -from .protocol_containers import NETWORK, ContainerLab, ContainerPeer, command -from .protocol_evidence import read_events -from .protocol_fixtures import certificates -from .protocol_hysteria2_catalog import events_pass -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command - - -def guest(peer, *argv, timeout=30): - # Container CLI can exit zero even when its guest command fails. - result = run_command( - [ - "container", - "exec", - peer.name, - "/bin/sh", - "-c", - '"$@" 2>&1\ncode=$?\nprintf "\\nVCORE_STATUS=%s\\n" "$code"', - "fixture", - *argv, - ], - timeout=timeout, - ) - output = result.stdout.decode(errors="replace") - if result.returncode != 0 or not result.cleanup: - raise RuntimeError( - f"isolated guest {argv[0]} failed after {result.seconds:.1f}s " - f"(exit={result.returncode}, cleanup={result.cleanup}): " - f"{redact(output[-2048:])}" - ) - body, marker, code = output.rpartition("\nVCORE_STATUS=") - if not marker or code.strip() != "0": - raise RuntimeError( - f"isolated guest operation failed: {argv[0]}: {redact(body[-2048:])}" - ) - return body - - -def native_binary(output): - url = "https://github.com/HyNetworks/hysteria/releases/latest/download/hysteria-linux-arm64" - path = output / "hysteria" - digest, total, deadline = hashlib.sha256(), 0, time.monotonic() + 90 - request = urllib.request.Request(url, headers={"User-Agent": "VCore-interop"}) - with ( - urllib.request.urlopen(request, timeout=30) as response, - path.open("xb") as target, - ): - if not response.geturl().startswith("https://"): - raise RuntimeError("native download redirected outside HTTPS") - while chunk := response.read(1024 * 1024): - total += len(chunk) - if total > 128 * 1024 * 1024 or time.monotonic() > deadline: - raise RuntimeError("native binary download budget exceeded") - target.write(chunk) - digest.update(chunk) - path.chmod(0o755) - return path, {"source_url": url, "binary_sha256": digest.hexdigest()} - - -def packages(lab, stack, root): - """Only package preparation has external egress; no server runs there.""" - root.mkdir() - peer = ContainerPeer(lab, root, "hysteria2-packages") - lab.record["peers"].append(peer.record) - stack.callback(peer.stop) - command( - "run", - "--detach", - "--name", - peer.name, - "--label", - f"purpose={NETWORK}", - "--label", - f"vcore-run={lab.run_id}", - "--network", - "default", - "--arch", - "arm64", - "--cpus", - "1", - "--memory", - "256M", - "--mount", - f"type=bind,source={root},target=/packages", - "--entrypoint", - "/bin/sleep", - lab.image, - "360", - timeout=45, - ) - peer.record.update( - started=True, role="package-preparation-only", network_mode="default" - ) - guest(peer, "apk", "update", timeout=60) - guest( - peer, - "apk", - "fetch", - "--recursive", - "--output", - "/packages", - "nftables", - timeout=240, - ) - found = sorted(root.glob("*.apk")) - if not found: - raise RuntimeError("missing native firewall packages") - lab.record["firewall_packages"] = { - path.name: hashlib.sha256(path.read_bytes()).hexdigest() for path in found - } - peer.stop() - return found - - -def run( - output: Path, - *, - ipv6=False, - obfs=False, - random_interval=False, - test="native_hopping", - supplied=None, -): - if test not in { - "native_hopping", - "native_udp_disabled", - "native_hop_protect_rejection", - "native_stop_during_hop", - }: - raise ValueError("unknown native H gate") - output = output.resolve() - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="HYSTERIA2", - scope="incremental-native-H", - source=source_identity(), - status="NOT RUN", - isolation={}, - cases=[], - ipv6=ipv6, - obfs=obfs, - random_interval=random_interval, - ) - try: - if supplied is None: - binary, report["peer"] = native_binary(output) - else: - binary, report["peer"], _, _ = supplied - if ( - hashlib.sha256(binary.read_bytes()).hexdigest() - != report["peer"]["binary_sha256"] - ): - raise RuntimeError("supplied H artifact identity mismatch") - lab = ContainerLab(report["isolation"], mtu=1500) - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - root.chmod(0o700) - if supplied is None: - apks = packages(lab, stack, root / "apks") - else: - _, _, apks, digests = supplied - if { - p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in apks - } != digests: - raise RuntimeError("supplied firewall package identity mismatch") - report["isolation"]["firewall_packages"] = digests - origin_dir, server_dir = root / "origin", root / "server" - origin_dir.mkdir() - server_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - shutil.copy2(binary, server_dir / "hysteria") - (server_dir / "apks").mkdir() - for path in apks: - shutil.copy2(path, server_dir / "apks" / path.name) - cert, key, pin = certificates(server_dir) - origin = lab.start( - stack, - origin_dir, - "hysteria2-h-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - server_dir, - "hysteria2-h-peer", - [ - "env", - "HYSTERIA_FIREWALL_BACKEND=nftables", - "/data/fixture/hysteria", - "server", - "--disable-update-check", - "--log-format", - "json", - "-c", - "/data/fixture/config.json", - ], - net_admin=True, - ) - guest( - server, - "apk", - "add", - "--no-network", - *[f"/data/fixture/apks/{path.name}" for path in apks], - timeout=45, - ) - report["peer"]["version"] = guest( - server, "/data/fixture/hysteria", "version" - ).strip() - if ( - guest(server, "sha256sum", "/data/fixture/hysteria").split()[0] - != report["peer"]["binary_sha256"] - ): - raise RuntimeError("native binary identity mismatch") - report["isolation"]["nftables"] = guest(server, "nft", "--version").strip() - (server_dir / "hosts").write_text( - "127.0.0.1 localhost\n::1 localhost\n" - f"{origin.ipv4} vcore-fixture.test\n" - ) - guest(server, "cp", "/data/fixture/hosts", "/etc/hosts") - # Pre-DNAT observations: native Hysteria owns its separate redirect table. - rules = ( - "table inet vcore_hysteria2 { chain observe { " - "type filter hook prerouting priority -150; policy accept; " - ) - for port in range(23010, 23013): - rules += f'udp dport {port} counter comment "p{port}"; ' - rules += "}\n}\n" - (server_dir / "observe.nft").write_text(rules) - guest(server, "nft", "-f", "/data/fixture/observe.nft") - password = "synthetic-hysteria2-fixture" - config = dict( - # Native Hysteria deliberately treats [::] as IPv6-only. - listen=f"[{server.ipv6}]:23010-23012" - if ipv6 - else f"{server.ipv4}:23010-23012", - tls=dict( - cert=f"/data/fixture/{cert.name}", key=f"/data/fixture/{key.name}" - ), - auth=dict(type="password", password=password), - trafficStats=dict(listen="0.0.0.0:24003"), - ) - node = dict( - name="peer", - type="hysteria2", - server=server.ipv6 if ipv6 else server.ipv4, - ports="23010-23012", - **{"hop-interval": "5-7" if random_interval else 5}, - password=password, - udp=True, - sni="localhost", - fingerprint=pin, - ) - if obfs: - config["obfs"] = dict( - type="salamander", - salamander=dict(password="independent-obfs-fixture"), - ) - node.update( - obfs="salamander", **{"obfs-password": "independent-obfs-fixture"} - ) - if test == "native_udp_disabled": - config["disableUDP"] = True - (server_dir / "config.json").write_text(json.dumps(config)) - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - node=node, - ) - ) - ) - origin.release() - server.release() - origin.wait_tcp(24000) - server.wait_tcp(24003) - result = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "hysteria2_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ], - cwd=CORE_DIR, - timeout=180, - limit=4 * 1024 * 1024, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_CASE_EVENTS=str(output / "hysteria2-events.jsonl"), - VCORE_HOP_OBSERVATIONS=str(output / "hopping.json"), - ), - ) - (output / "test.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - raw_log = command("logs", server.name) - connected = sum( - '"msg":"client connected"' in line for line in raw_log.splitlines() - ) - report["auth_connections"] = connected - (output / "server.log").write_text( - redact(raw_log.replace(password, "")) - ) - rules = json.loads( - guest(server, "nft", "-j", "list", "table", "inet", "vcore_hysteria2") - ) - counts = {} - for entry in rules["nftables"]: - if "rule" in entry: - rule = entry["rule"] - counts[rule["comment"]] = next( - item["counter"]["packets"] - for item in rule["expr"] - if "counter" in item - ) - report["entry_port_packets"] = counts - passed = ( - result.returncode == 0 - and result.cleanup - and (output / "hysteria2-events.jsonl").exists() - and events_pass(read_events(output / "hysteria2-events.jsonl"), test) - and connected == 1 - and sum(value > 0 for value in counts.values()) - >= (2 if test == "native_hopping" else 1) - ) - report["cases"].append( - dict( - case_id=test, - exit_code=result.returncode, - seconds=result.seconds, - command_cleanup=result.cleanup, - command=[ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "hysteria2_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ], - status="PASS" if passed else "FAIL", - ) - ) - report["status"] = "PASS" if passed else "FAIL" - except (OSError, RuntimeError, ValueError) as error: - report.update( - status="FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - finally: - report["cleanup"] = all( - peer["joined"] for peer in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - if not report["source_unchanged"] or not report["cleanup"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - return report - - -if __name__ == "__main__": - import sys - - result = run( - Path(sys.argv[1]), - ipv6="--ipv6" in sys.argv, - obfs="--salamander" in sys.argv, - random_interval="--random" in sys.argv, - test=( - sys.argv[sys.argv.index("--test") + 1] - if "--test" in sys.argv - else "native_udp_disabled" - if "--udp-disabled" in sys.argv - else "native_hopping" - ), - ) - print( - json.dumps( - { - key: result.get(key) - for key in ( - "status", - "reason", - "cleanup", - "cases", - "auth_connections", - "entry_port_packets", - ) - } - ) - ) - raise SystemExit(0 if result["status"] == "PASS" and result["cleanup"] else 1) diff --git a/scripts/src/vcore_scripts/protocol_inputs.py b/scripts/src/vcore_scripts/protocol_inputs.py deleted file mode 100644 index a313f05..0000000 --- a/scripts/src/vcore_scripts/protocol_inputs.py +++ /dev/null @@ -1,133 +0,0 @@ -"""Reproducible, nonsensitive input identity for one local protocol run.""" - -from __future__ import annotations - -import hashlib -import platform -import re -import sys -import tomllib -from datetime import UTC, datetime -from pathlib import Path - -from .builds import CORE_DIR -from .protocol_peers import run_command - -CODE_PATHS = [ - "Cargo.toml", - "Cargo.lock", - "src", - "crates", - "tests", - "scripts", - "include", -] - - -def sha256(path: Path) -> str: - with path.open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() - - -def command_output(command: list[str]) -> bytes: - result = run_command(command, cwd=CORE_DIR, timeout=20, limit=8 * 1024 * 1024) - if result.returncode != 0: - raise RuntimeError("cannot record protocol input identity") - return result.stdout - - -def source_identity() -> dict: - paths = ( - command_output( - ["git", "ls-files", "-co", "--exclude-standard", "-z", "--", *CODE_PATHS] - ) - .decode() - .split("\0") - ) - digest = hashlib.sha256() - for name in sorted(set(paths) - {""}): - path = CORE_DIR / name - if not path.exists(): - continue - if path.is_symlink() or not path.is_file(): - raise ValueError("non-file source input") - digest.update(name.encode() + b"\0" + bytes.fromhex(sha256(path))) - patch = command_output(["git", "diff", "--binary", "HEAD", "--", *CODE_PATHS]) - return { - "parent_commit": command_output(["git", "rev-parse", "HEAD"]).decode().strip(), - "source_tree_sha256": digest.hexdigest(), - "dirty_patch_sha256": hashlib.sha256(patch).hexdigest(), - "lock_sha256": sha256(CORE_DIR / "Cargo.lock"), - } - - -def same_source(before: dict, after: dict) -> bool: - """Compare actual inputs, not a patch changed merely by staging new files.""" - return all( - before[key] == after[key] - for key in ("parent_commit", "source_tree_sha256", "lock_sha256") - ) - - -def run_identity(stage: str, selected: list[dict], preflight: bool) -> dict: - record = source_identity() - lock = tomllib.loads((CORE_DIR / "Cargo.lock").read_text()) - tls = [ - {key: package[key] for key in ["name", "version", "source"]} - for package in lock["package"] - if package["name"] in {"rustls", "tokio-rustls", "ring"} - ] - text = (CORE_DIR / "src/lib.rs").read_text() - record.update( - schema_version=1, - stage=stage, - mode="preflight" if preflight else "execute", - started_utc=datetime.now(UTC).isoformat(), - os=platform.system(), - os_version=platform.release(), - architecture=platform.machine(), - python=platform.python_version(), - rustls=tls, - invoke_api_version=int(re.search(r"INVOKE_API_VERSION: u32 = (\d+)", text)[1]), - config_version=int(re.search(r"CONFIG_VERSION: u8 = (\d+)", text)[1]), - features=["all-features", "independent-feature-smokes"], - selected_cases=[case["case_id"] for case in selected], - commands=[], - cleanup=False, - source_unchanged=False, - artifacts=[], - suite_timeout_seconds=sum(case["timeout_seconds"] for case in selected) + 1800, - ) - record["rustc"] = command_output(["rustc", "-Vv"]).decode().strip() - record["cargo"] = command_output(["cargo", "--version"]).decode().strip() - if sys.platform == "darwin": - record["xcode"] = command_output(["xcodebuild", "-version"]).decode().strip() - record["sdk"] = command_output(["xcrun", "--show-sdk-version"]).decode().strip() - return record - - -def redact(text: str) -> str: - text = text.replace(str(CORE_DIR), "") - text = re.sub( - r"(?i)(authorization\s*[:=]\s*)(?:Bearer|Basic)\s+[^\s,}]+", - r"\1", - text, - ) - text = re.sub( - r"(?i)-----BEGIN .*?PRIVATE KEY-----.*?-----END .*?PRIVATE KEY-----", - "", - text, - flags=re.S, - ) - text = re.sub( - r"[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}", "", text - ) - text = re.sub( - r"(?i)((?:password|authorization|private[-_]key|token)\s*[:=]\s*)([^\s,}]+)", - r"\1", - text, - ) - text = re.sub( - r"/(?:Users|home|private/var|var/folders)/[^\s\"']+", "", text - ) - return text diff --git a/scripts/src/vcore_scripts/protocol_integration_acceptance.py b/scripts/src/vcore_scripts/protocol_integration_acceptance.py deleted file mode 100644 index f294d8e..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_acceptance.py +++ /dev/null @@ -1,375 +0,0 @@ -"""INTEGRATION execution and independent reconstruction of raw integration evidence.""" - -from __future__ import annotations - -import json - -from .protocol_evidence import read_events, read_json -from .protocol_hysteria2_catalog import assertions_pass -from .protocol_integration_catalog import CHAINS, LOCAL, ORDERED, PAIRS, definitions -from .protocol_integration_checks import envelope, runtime_pass -from .protocol_integration_local import commands, passed -from .protocol_integration_shared import hops_pass, shared_pass -from .protocol_security_acceptance import result - - -def pair_pass(identifier, record, directory): - from .protocol_integration_native import rust_command - - first, last = ORDERED[identifier] - targets = 2 if last == "trojan" else 3 - events = read_events(directory / (identifier + "-events.jsonl")) - observation = read_json(directory / (identifier + "-observations.json")) - expected = [ - dict( - outer_ipv6=v6, - nested_select=group, - tcp_target_kinds=3, - tcp_bytes_each_direction=31457280, - udp_target_kinds=targets, - udp_packets=targets * 400, - udp_sizes=[1, 64, 512, 1200], - server_first=last != "ss", - client_first=True, - ports_rebound=True, - **( - {"server_first_limitation": "official-ss2022-empty-first-write"} - if last == "ss" - else {} - ), - ) - for v6 in (False, True) - for group in (False, True) - ] - return ( - record.get("case_id") == identifier - and record.get("command") == rust_command() - and type(record.get("exit_code")) is int - and record["exit_code"] == 0 - and record.get("command_cleanup") is True - and observation - == dict( - first=first, - last=last, - paths=expected, - domain_native=[ - dict( - outer_ipv6=v6, - nested_select=group, - peer="XR", - udp_packets=400, - target="domain", - ) - for v6 in (False, True) - for group in (False, True) - ] - if last == "trojan" - else [], - budgets=[ - dict( - transmit=tx, - receive=rx, - roundtrip_size=min(tx, rx), - packets=100, - oversize_rejected_before_origin=True, - ) - for tx, rx in ((128, 128), (512, 256)) - ], - routed_udp=dict( - upstream_business_udp_disabled=True, - carrier_tcp=True, - carrier_udp=True, - leaf_business_udp_rejected=True, - ), - carrier_capability=dict( - upstream_datagrams="rejected", - tcp_allowed=last not in {"hysteria2", "tuic"}, - udp_allowed=last not in {"ss", "socks5", "hysteria2", "tuic"}, - no_bypass=True, - ), - **({"variant": CHAINS[identifier]} if identifier in CHAINS else {}), - ) - and assertions_pass( - events, - { - ("INTEGRATION-PAIR", "ordered_pair"): 1, - ("INTEGRATION-BASE", "tcp_10mib_both_directions"): 12, - ("INTEGRATION-PAIR", "directional_budget"): 1, - ("INTEGRATION-PAIR", "routed_udp_permission"): 1, - ("INTEGRATION-PAIR", "carrier_capability"): 1, - **( - {("INTEGRATION-PAIR", "native_domain_terminal"): 1} - if last == "trojan" - else {} - ), - }, - ) - ) - - -def execute(cases, run, output, results): - from .protocol_containers import frozen_image - from .protocol_harness import _command - from .protocol_hysteria2_acceptance import prepare - from .protocol_integration_coupled import passed as coupled_pass - from .protocol_integration_coupled import run as coupled_run - from .protocol_integration_native import run as run_pairs - from .protocol_integration_shared import hops_run, shared_run - from .protocol_integration_suite import CONSUMERS - from .protocol_integration_suite import run as run_suite - - by_id = {c["case_id"]: c for c in cases} - supplied, peers = {}, {} - source = { - k: run[k] - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - - def save(identifier, good, cleanup=True): - results[identifier] = result(by_id[identifier], good, cleanup) - print(identifier + ": " + results[identifier]["status"], flush=True) - if not good: - raise RuntimeError("INTEGRATION gate failed: " + identifier) - - try: - for identifier in sorted(by_id.keys() & LOCAL): - records, events = [], [] - for i, argv in enumerate(commands(identifier)): - name = f"{identifier}-{i}" - path = output / (name + "-events.jsonl") - _command( - run, - output, - name, - [v.replace("{output}", str(output)) for v in argv], - 3600, - events=path, - ) - records.append(run["commands"][-1]) - if path.exists(): - events += read_events(path) - if records[-1]["exit_code"] != 0 or not records[-1]["cleanup"]: - save(identifier, False, records[-1]["cleanup"]) - script = ( - read_json(output / "script-tests.json") - if identifier == "INTEGRATION-SCRIPTS" - else None - ) - save(identifier, passed(identifier, records, events, script)) - if set(by_id) <= LOCAL: - return - with frozen_image(output / "container-image-pull.log") as snapshot: - run["container_image"] = snapshot - kinds = {"M"} - if "INTEGRATION-SHARED" in by_id: - kinds |= {"XR", "V2"} - if any(ORDERED[i][1] == "trojan" for i in by_id.keys() & ORDERED.keys()): - kinds.add("XR") - if "INTEGRATION-HY2-HOP" in by_id: - kinds.add("H") - supplied = prepare(output, kinds) - peers = {k: v[1] for k, v in supplied.items()} - digest = snapshot["digest"] - # Each group is rerun from this source/image/peer scope. Historical - # reports are never imported into results. - selected = sorted(by_id.keys() & ORDERED.keys()) - if selected: - directory = output / "pairs" - report = run_pairs( - directory, - selected, - supplied=supplied["M"], - domain_peer=supplied.get("XR"), - ) - if [r["case_id"] for r in report["cases"]] != selected: - raise RuntimeError("INTEGRATION ordered-pair selection differs") - for record in report["cases"]: - identifier = record["case_id"] - save( - identifier, - envelope(report, peers, source, digest) - and pair_pass(identifier, record, directory), - report["cleanup"], - ) - if "INTEGRATION-COUPLED" in by_id: - coupled_run(output / "coupled", supplied["M"], snapshot) - save( - "INTEGRATION-COUPLED", - coupled_pass(output / "coupled", peers, source, digest), - ) - selected = sorted( - by_id.keys() & CONSUMERS.keys(), - key=lambda k: (k == "INTEGRATION-SOAK", k), - ) - if selected: - directory = output / "runtime" - report = run_suite(directory, selected, supplied=supplied["M"]) - peers.update(report["peers"]) - if [r["case_id"] for r in report["cases"]] != selected: - raise RuntimeError("INTEGRATION runtime selection differs") - for record in report["cases"]: - identifier = record["case_id"] - save( - identifier, - envelope(report, peers, source, digest) - and runtime_pass(identifier, record, directory), - report["cleanup"], - ) - if "INTEGRATION-HY2-HOP" in by_id: - hops_run(output / "hop", supplied["H"]) - save( - "INTEGRATION-HY2-HOP", - hops_pass(output / "hop", peers, source, digest), - ) - if "INTEGRATION-SHARED" in by_id: - shared_run( - output / "shared", - {k: v for k, v in supplied.items() if k in {"M", "XR", "V2"}}, - ) - save( - "INTEGRATION-SHARED", - shared_pass(output / "shared", peers, source, digest), - ) - finally: - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - - -def check(run_dir, cases, run, results, peers, paths): - from .protocol_containers import IMAGE - from .protocol_evidence import HEX - from .protocol_inputs import redact - from .protocol_integration_coupled import passed as coupled_pass - from .protocol_integration_suite import CONSUMERS - - observed_paths = { - str(p.relative_to(run_dir)) - for p in run_dir.rglob("*") - if p.is_file() - and p.suffix in {".json", ".jsonl", ".log", ".md"} - and "binaries" not in p.parts - and p.name != "run.json" - } - if set(paths) != observed_paths: - raise ValueError("unhashed or missing INTEGRATION evidence artifact") - snapshot = run.get("container_image", {}) - digest = snapshot.get("digest", "") - if not ( - digest.startswith("sha256:") - and HEX.fullmatch(digest[7:]) - and snapshot - == dict( - tag=IMAGE, - digest=digest, - command=["container", "image", "pull", IMAGE], - exit_code=0, - cleanup=True, - log="container-image-pull.log", - ) - and snapshot["log"] in paths - and cases == definitions() - and set(peers) == {"M", "XR", "V2", "H", "SS"} - and {"script-tests.json", "package-preparation.json"} <= set(paths) - ): - raise ValueError("incomplete INTEGRATION scope, image or peer identities") - source = { - k: run[k] - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - records = run.get("commands", []) - names = { - f"{identifier}-{i}" - for identifier in LOCAL - for i, _ in enumerate(commands(identifier)) - } - if len(records) != len(names) or {r.get("name") for r in records} != names: - raise ValueError("missing, duplicate or extra INTEGRATION local command") - pair_report = read_json(run_dir / "pairs/integration-native.json") - runtime_report = read_json(run_dir / "runtime/integration-suite.json") - expected_runtime = sorted( - {c["case_id"] for c in cases} & CONSUMERS.keys(), - key=lambda k: (k == "INTEGRATION-SOAK", k), - ) - if ( - [r.get("case_id") for r in pair_report.get("cases", [])] != sorted(ORDERED) - or [r.get("case_id") for r in runtime_report.get("cases", [])] - != expected_runtime - or set(pair_report.get("peers", {})) != {"M", "XR"} - or set(runtime_report.get("peers", {})) != {"M", "SS"} - or runtime_report.get("ssserver_policy") - != dict( - outbound_udp_allow_fragmentation=True, - inbound_udp_allow_fragmentation=True, - guest_mtu=1500, - ) - ): - raise ValueError( - "missing INTEGRATION ordered pairs, runtime gates or native SS policy" - ) - actual = {r["case_id"]: r for r in results} - for case in cases: - identifier = case["case_id"] - if identifier in LOCAL: - selected, events = [], [] - for i, argv in enumerate(commands(identifier)): - record = next(r for r in records if r["name"] == f"{identifier}-{i}") - if ( - record.get("command") - != [redact(v.replace("{output}", str(run_dir))) for v in argv] - or record.get("log") not in paths - ): - raise ValueError("INTEGRATION command or log changed") - selected.append(record) - path = run_dir / f"{identifier}-{i}-events.jsonl" - if path.exists(): - events += read_events(path) - good = passed( - identifier, - selected, - events, - read_json(run_dir / "script-tests.json") - if identifier == "INTEGRATION-SCRIPTS" - else None, - ) - elif identifier in ORDERED or identifier in CONSUMERS: - pair = identifier in ORDERED - report = pair_report if pair else runtime_report - record = next(r for r in report["cases"] if r["case_id"] == identifier) - directory = run_dir / ("pairs" if pair else "runtime") - good = envelope(report, peers, source, digest) and ( - pair_pass if pair else runtime_pass - )(identifier, record, directory) - elif identifier == "INTEGRATION-HY2-HOP": - good = hops_pass(run_dir / "hop", peers, source, digest) - elif identifier == "INTEGRATION-COUPLED": - good = coupled_pass(run_dir / "coupled", peers, source, digest) - else: - good = shared_pass(run_dir / "shared", peers, source, digest) - if not good or result(case, good, True) != actual[identifier]: - raise ValueError("INTEGRATION raw evidence does not pass: " + identifier) - print( - f"INTEGRATION: PASS ({len(cases)} required gates; {len(PAIRS)} pairs, " - f"{len(CHAINS)} TUIC/ShadowTLS chains, retained UoT/TUIC chains, " - "100 lifetimes, 100 rebuilds, 1800-second soak)" - ) - - -def preflight(output): - from .protocol_containers import frozen_image - from .protocol_hysteria2_acceptance import prepare - - # Downloads and container package setup only; never reports behavior PASS. - with frozen_image(output / "container-image-pull.log"): - supplied = prepare(output, {"M", "XR", "V2", "H"}) - (output / "peers.json").write_text( - json.dumps({k: v[1] for k, v in supplied.items()}, indent=2) + "\n" - ) diff --git a/scripts/src/vcore_scripts/protocol_integration_catalog.py b/scripts/src/vcore_scripts/protocol_integration_catalog.py deleted file mode 100644 index ea97420..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_catalog.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Cross-protocol integration requirements and descriptive coverage categories.""" - -from __future__ import annotations - -PROTOCOLS = ("socks5", "anytls", "ss", "trojan", "vmess", "vless", "hysteria2", "tuic") -PAIRS = { - f"INTEGRATION-PAIR-{first.upper()}-{last.upper()}": (first, last) - for first in PROTOCOLS - for last in PROTOCOLS -} -CHAINS = { - f"INTEGRATION-CHAIN-TUIC-{mode.upper()}-SS-V3-{cipher.upper()}": { - "udp_mode": mode, - "cipher": "2022-blake3-" + cipher, - } - for mode in ("native", "quic") - for cipher in ("aes-128-gcm", "aes-256-gcm", "chacha20-poly1305") -} -ORDERED = PAIRS | dict.fromkeys(CHAINS, ("tuic", "ss")) -GATES = { - "INTEGRATION-ENTRYPOINTS": "routing", - "INTEGRATION-GRAPH": "routing", - "INTEGRATION-DNS-MEASURE": "routing", - "INTEGRATION-SS-ALGORITHMS": "routing", - "INTEGRATION-SS-EIH": "routing", - "INTEGRATION-LIFECYCLE": "lifecycle", - "INTEGRATION-REBUILD": "lifecycle", - "INTEGRATION-FAILURES": "lifecycle", - "INTEGRATION-SOAK": "pressure", - "INTEGRATION-HY2-HOP": "pressure", - "INTEGRATION-DEBUG": "regression", - "INTEGRATION-RELEASE": "regression", - "INTEGRATION-QUALITY": "regression", - "INTEGRATION-FEATURES": "regression", - "INTEGRATION-SCRIPTS": "regression", - "INTEGRATION-SHARED": "regression", - "INTEGRATION-COUPLED": "routing", -} -LOCAL = { - "INTEGRATION-DEBUG", - "INTEGRATION-RELEASE", - "INTEGRATION-QUALITY", - "INTEGRATION-FEATURES", - "INTEGRATION-SCRIPTS", -} - - -def definitions(): - result = [] - for identifier in sorted(ORDERED.keys() | GATES.keys()): - pair = ORDERED.get(identifier) - local = identifier in LOCAL - result.append( - dict( - case_id=identifier, - stage="INTEGRATION", - substage=f"INTEGRATION.{GATES.get(identifier, 'routing')}", - required=True, - row_ids=["C06"] if pair else [], - protocol="integration", - network="eight-protocol-graph", - security="per-frozen-consumer", - udp_codec="per-protocol", - field_values={ - "first": pair[0], - "last": pair[1], - **CHAINS.get(identifier, {}), - } - if pair - else {}, - outer_family="IPv4/IPv6", - inner_family="IPv4/IPv6/domain", - target_type="memory-only/build" if local else "isolated origin", - upstream_graph="concrete/nested-select" if pair else "per-gate", - peer_kind="unit" - if local - else "H" - if identifier == "INTEGRATION-HY2-HOP" - else "SS" - if identifier == "INTEGRATION-SS-EIH" - else "M", - peer_config={"group": identifier}, - gap_source=None, - expected_observation=[identifier], - required_evidence=["command", "structured-assertions", "cleanup"] - + ([] if local else ["peer-identity", "isolated-origins"]), - prerequisites=["rust-toolchain"] - if local - else ["owned-host-only-container-network", "official-latest-peer"], - runner="integration-pair" if pair else "integration-gate", - timeout_seconds=3600 if not pair else 300, - ) - ) - return result diff --git a/scripts/src/vcore_scripts/protocol_integration_checks.py b/scripts/src/vcore_scripts/protocol_integration_checks.py deleted file mode 100644 index 8a82a35..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_checks.py +++ /dev/null @@ -1,192 +0,0 @@ -"""Raw runtime observations and native isolation, independent of run status.""" - -from __future__ import annotations - -from .protocol_evidence import HEX, idle_resources, read_events, read_json -from .protocol_hysteria2_catalog import assertions_pass -from .protocol_integration_catalog import PROTOCOLS -from .protocol_integration_metrics import lifetimes, rebuild, soak -from .protocol_integration_native import rust_command -from .protocol_integration_suite import CONSUMERS - - -def envelope(report, peers, source, digest, *, single=None): - isolation = report.get("isolation", {}) - identities = {single: report.get("peer", {})} if single else report.get("peers", {}) - owned = isolation.get("peers", []) - urls = { - "M": "MetaCubeX/mihomo", - "XR": "XTLS/Xray-core", - "V2": "v2fly/v2ray-core", - "SS": "shadowsocks/shadowsocks-rust", - "ST": "ihciah/shadow-tls", - "H": "HyNetworks/hysteria", - } - return bool( - report.get("status") == "PASS" - and report.get("cleanup") is True - and report.get("source_unchanged") is True - and report.get("source") == source - and isolation.get("network_mode") == "hostOnly" - and isolation.get("host_servers") is False - and isolation.get("guest_mtu") == 1500 - and isolation.get("image_digest") == digest - and digest.startswith("sha256:") - and HEX.fullmatch(digest[7:]) - and owned - and all(p.get("started") is True and p.get("joined") is True for p in owned) - and identities - and all( - kind in urls - and identity == peers.get(kind) - and identity.get("version") - and HEX.fullmatch(identity.get("binary_sha256", "")) - and (kind == "H" or HEX.fullmatch(identity.get("archive_sha256", ""))) - and identity.get("source_url", "").startswith( - f"https://github.com/{urls[kind]}/releases/" - ) - for kind, identity in identities.items() - ) - ) - - -def runtime_pass(identifier, record, directory): - if not ( - record.get("case_id") == identifier - and record.get("command") == rust_command(CONSUMERS[identifier]) - and type(record.get("exit_code")) is int - and record["exit_code"] == 0 - and record.get("command_cleanup") is True - ): - return False - value = read_json(directory / (identifier + "-observations.json")) - events = read_events(directory / (identifier + "-events.jsonl")) - protocols = list(PROTOCOLS) - expected = {(identifier, p): 1 for p in protocols} - if identifier == "INTEGRATION-ENTRYPOINTS": - good = value == dict( - entrypoints=[ - dict( - protocol=p, - http_forward=True, - http_connect=True, - tun_tcp=True, - tun_udp=True, - tun_dns=True, - ) - for p in protocols - ] - ) - elif identifier == "INTEGRATION-GRAPH": - good = value == dict( - protocols=protocols, - nested_select=True, - old_transport_snapshot=True, - cold_reject=True, - direct=True, - unselected_cycle=True, - no_fallback=True, - ) - elif identifier == "INTEGRATION-DNS-MEASURE": - good = value == dict( - protocols=protocols, - controlled_dns_via_proxy=True, - ip_rule_consumer=True, - node_measure=True, - group_measure_rejected=True, - ) - elif identifier == "INTEGRATION-FAILURES": - expected = { - (suite, p): 1 - for suite in ( - identifier, - "INTEGRATION-FAILURES-AUTH", - "INTEGRATION-FAILURES-SOURCE", - ) - for p in protocols - } - good = value == dict( - protect_rejected=protocols, - auth_rejected=protocols, - certificate_pin_rejected=[ - "anytls", - "trojan", - "vmess", - "vless", - "hysteria2", - "tuic", - ], - source_and_oversize_isolated=protocols, - tuic_transmit_budget=dict(limit=128, rejected=129, following_roundtrip=128), - cancel_preserves_sibling=protocols, - no_origin_bytes=True, - stop_idle=True, - ) - good &= all( - idle_resources(e.get("resources")) - for e in events - if e.get("status") == "PASS" - and e.get("suite") != "INTEGRATION-FAILURES-AUTH" - ) - elif identifier in {"INTEGRATION-SS-ALGORITHMS", "INTEGRATION-SS-EIH"}: - eih = identifier == "INTEGRATION-SS-EIH" - ciphers = ["2022-blake3-aes-128-gcm", "2022-blake3-aes-256-gcm"] + ( - [] if eih else ["2022-blake3-chacha20-poly1305"] - ) - expected = {(identifier, c): 1 for c in ciphers} | { - ("INTEGRATION-BASE", "tcp_10mib_both_directions"): 8 if eih else 9 - } - good = value == ( - dict( - native_terminal_eih=[ - dict( - cipher=c, - identity_depth=1, - outer_families=2, - tcp_targets=2, - tcp_bytes_each_direction=41943040, - udp_targets=2, - udp_packets=2000, - wrong_identity_rejected=True, - wrong_user_rejected=True, - ) - for c in ciphers - ], - arbitrary_relay_claim=False, - ) - if eih - else dict( - algorithms=[ - dict( - cipher=c, - tcp_targets=3, - tcp_bytes_each_direction=31457280, - udp_targets=3, - udp_packets=1500, - wrong_key_rejected=True, - ) - for c in ciphers - ] - ) - ) - elif identifier == "INTEGRATION-LIFECYCLE": - expected = {(identifier, "stop_and_remain_quiet"): 100} - good = lifetimes(value, events) - else: - name = ( - "forty_flows_same_session" - if identifier == "INTEGRATION-REBUILD" - else "mixed_forty_flows" - ) - expected = {(identifier, name): 1} - passes = [e for e in events if e.get("status") == "PASS"] - good = len(passes) == 1 and ( - rebuild(value, passes[0]) - if identifier == "INTEGRATION-REBUILD" - else soak( - value, - passes[0], - read_events(directory / (identifier + "-observations.json.jsonl")), - ) - ) - return bool(good and assertions_pass(events, expected)) diff --git a/scripts/src/vcore_scripts/protocol_integration_coupled.py b/scripts/src/vcore_scripts/protocol_integration_coupled.py deleted file mode 100644 index d1d0c65..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_coupled.py +++ /dev/null @@ -1,41 +0,0 @@ -"""Rerun the retained UoT/TUIC chain consumers in one integration source scope.""" - -from .protocol_evidence import read_json -from .protocol_integration_checks import envelope - -GROUPS = ("MIHOMO-SOCKS5", "MIHOMO-GROUPS", "MIHOMO-PATHS") - - -def run(directory, supplied, image): - from .protocol_tuic import run as tuic - from .protocol_uot import run as uot - - for name in GROUPS: - print("INTEGRATION coupled: " + name, flush=True) - root = directory / name - if name == "MIHOMO-PATHS": - tuic(root, checks="paths", supplied=supplied, image=image) - else: - uot( - root, - checks="group" if name == "MIHOMO-GROUPS" else "data", - via_socks5=True, - supplied=supplied, - image=image, - ) - - -def passed(directory, peers, source, digest): - from .protocol_tuic_acceptance import wire_pass as tuic - from .protocol_uot_acceptance import wire_pass as uot - - for name in GROUPS: - root = directory / name - report = read_json(root / "report.json") - checker = tuic if name == "MIHOMO-PATHS" else uot - if not ( - envelope(report, peers, source, digest, single="M") - and checker(name, root, report) - ): - return False - return True diff --git a/scripts/src/vcore_scripts/protocol_integration_local.py b/scripts/src/vcore_scripts/protocol_integration_local.py deleted file mode 100644 index 9d625eb..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_local.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Explicit memory-only/build commands; never run historical host-server suites.""" - -from __future__ import annotations - -import sys - -from .core_checks import commands as core_commands -from .protocol_hysteria2_acceptance import commands as hysteria2_commands - - -def commands(identifier): - if identifier in { - "INTEGRATION-DEBUG", - "INTEGRATION-RELEASE", - "INTEGRATION-FEATURES", - }: - return core_commands(identifier.removeprefix("INTEGRATION-").lower()) - if identifier == "INTEGRATION-QUALITY": - # Behavioral integration also runs with the approved local boring fork. - # Strict release-source audit remains a separate mandatory PR/delivery gate. - return [ - row - for row in hysteria2_commands("HYSTERIA2-QUALITY") - if row != ["vcore-scripts", "check", "tls-dependencies"] - ] + [ - ["vcore-scripts", "check", "protocol-coverage", "--catalog-only"], - ["vcore-scripts", "build", "apple"], - ["vcore-scripts", "build", "android"], - ] - if identifier == "INTEGRATION-SCRIPTS": - return [ - [ - sys.executable, - "-m", - "vcore_scripts.protocol_script_tests", - "{output}/script-tests.json", - ] - ] - return [] - - -def passed(identifier, records, events, script=None): - from .protocol_hysteria2_catalog import CFG, UNIT, assertions_pass - from .protocol_integration_catalog import PROTOCOLS - from .protocol_security_units import TESTS - - good = bool(records) and all( - type(r.get("exit_code")) is int - and r["exit_code"] == 0 - and r.get("cleanup") is True - for r in records - ) - if identifier in {"INTEGRATION-DEBUG", "INTEGRATION-RELEASE"}: - expected = { - ("SECURITY-CONFIG-TLS", name): 1 - for names in TESTS.values() - for name in names - } - expected.update({("HYSTERIA2-CFG", name): 1 for name in CFG}) - expected.update({("HYSTERIA2-UNIT", name): 1 for name in UNIT}) - expected.update({("INTEGRATION-FEATURE", name): 1 for name in PROTOCOLS}) - expected[("INTEGRATION-ADAPTER", "growing_caller_buffer")] = 1 - expected[("INTEGRATION-ADAPTER", "server_first_buffered_upstream")] = 1 - expected[("INTEGRATION-ADAPTER", "hysteria2_fragment_id_reuse")] = 1 - selected = [ - e - for e in events - if e.get("suite") - in { - "SECURITY-CONFIG-TLS", - "HYSTERIA2-CFG", - "HYSTERIA2-UNIT", - "INTEGRATION-FEATURE", - "INTEGRATION-ADAPTER", - } - ] - good &= assertions_pass(selected, expected) and all( - e.get("status") in {"BEGIN", "PASS"} for e in events - ) - if identifier == "INTEGRATION-FEATURES": - good &= assertions_pass( - [e for e in events if e.get("suite") == "INTEGRATION-FEATURE"], - {("INTEGRATION-FEATURE", p): 8 for p in PROTOCOLS}, - ) - if identifier == "INTEGRATION-SCRIPTS": - rows = (script or {}).get("cases", []) - names = [r.get("test") for r in rows] - required = { - "test_protocol_integration.IntegrationAcceptanceTest." + name - for name in ( - "test_integration_consumer_results_keep_the_manifest_scope", - "test_pair_evidence_requires_each_real_path_and_original_assertions", - "test_list_contains_all_ordered_pairs_and_remaining_gates", - "test_pressure_evidence_is_recomputed_not_inferred", - "test_native_source_identity_and_cleanup_are_required", - "test_integration_quality_separates_release_source_policy", - ) - } - good &= ( - bool(rows) - and len(names) == len(set(names)) == (script or {}).get("tests_run") - and required <= set(names) - and all(r.get("status") == "PASS" for r in rows) - ) - return bool(good) diff --git a/scripts/src/vcore_scripts/protocol_integration_metrics.py b/scripts/src/vcore_scripts/protocol_integration_metrics.py deleted file mode 100644 index 347ce52..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_metrics.py +++ /dev/null @@ -1,237 +0,0 @@ -"""Recompute lifecycle/pressure gates from bounded, persisted observations.""" - -from __future__ import annotations - -from math import isfinite -from statistics import median - -from .protocol_evidence import RESOURCE_KINDS, idle_resources -from .protocol_integration_profiles import ( - LIFETIME_GROUPS, - TCP_GROUPS, - UDP_GROUPS, - expected_flows, - selected, -) - -QUEUES = { - "socks_udp": 16, - "hysteria2_udp": 0, - "tuic_udp": 32, - "quic_incoming": 32, - "quic_outgoing": 32, -} - - -def integer(value, minimum=0): - return type(value) is int and value >= minimum - - -def resources(value, *, active=False): - rows = value.get("counts", []) - if len(rows) != 8 or {r.get("kind") for r in rows} != RESOURCE_KINDS: - return False - if not all( - integer(r.get("current")) - and integer(r.get("peak")) - and r["current"] <= r["peak"] - for r in rows - ): - return False - counts = {r["kind"]: r["current"] for r in rows} - return not active or all( - counts[k] > 0 for k in ("task", "socket", "session", "association") - ) - - -def sample(value, *, active=True): - queues = value.get("queues", []) - return ( - integer(value.get("heap_in_use"), 1) - and integer(value.get("rss_kib"), 1) - and integer(value.get("fd"), 1) - and resources(value.get("resources", {}), active=active) - and len(queues) == len(QUEUES) - and {q.get("kind"): q.get("capacity") for q in queues} == QUEUES - and all( - integer(q.get("peak"), int(q["capacity"] > 0)) - and q["peak"] <= q["capacity"] - for q in queues - ) - ) - - -def lifetimes(value, events): - cycles = value.get("cycles", []) - passes = [e for e in events if e.get("status") == "PASS"] - if value.get("count") != 100 or len(cycles) != 100 or len(passes) != 100: - return False - for index, (row, event) in enumerate(zip(cycles, passes, strict=True)): - stop = row.get("after_stop", {}) - checkpoints = event.get("checkpoints", []) - mode = index % 5 - group = LIFETIME_GROUPS[(index // 5) % len(LIFETIME_GROUPS)] - retained = {0: 0, 1: 10, 2: 1, 3: 0, 4: 8}[mode] - if not ( - row.get("cycle") == index - and row.get("group") == group - and row.get("profile") - == selected(group, index // (5 * len(LIFETIME_GROUPS))) - and row.get("mode") == mode - and integer(row.get("baseline_fd"), 1) - and row.get("retained_fixture_fd") == retained - and integer(row.get("after_stop_fd"), 1) - and row["after_stop_fd"] <= row["baseline_fd"] + retained - and integer(row.get("final_fd"), 1) - and row["final_fd"] <= row["baseline_fd"] - and integer(row.get("stop_ms")) - and row["stop_ms"] < 5000 - and isfinite(row.get("quiet_seconds", 0)) - and row.get("quiet_seconds", 0) >= 5 - and row.get("ports_rebound") is True - and idle_resources(stop) - and row.get("quiet") == stop == event.get("resources") - and len(checkpoints) == 4 - and [p.get("phase") for p in checkpoints] - == ["baseline", "active", "after-stop", "quiet"] - and idle_resources(checkpoints[0].get("resources")) - and checkpoints[1].get("resources") == row.get("active") - and resources(row.get("active", {})) - and checkpoints[2].get("resources") - == stop - == checkpoints[3].get("resources") - ): - return False - if mode in (1, 4) and not resources(row["active"], active=True): - return False - return True - - -def stopped(value, event): - cold, baseline = value.get("cold_fd"), value.get("baseline_fd") - row = value.get("stopped", {}) - after, quiet = row.get("after_stop", {}), row.get("quiet", {}) - return ( - integer(cold, 1) - and integer(baseline, 1) - and 0 <= baseline - cold <= 2 - and integer(row.get("stop_ms")) - and row["stop_ms"] < 5000 - and isfinite(row.get("quiet_seconds", 0)) - and row.get("quiet_seconds", 0) >= 5 - and row.get("ports_rebound") is True - and sample(after, active=False) - and sample(quiet, active=False) - and idle_resources(after.get("resources")) - and quiet.get("resources") == after.get("resources") == event.get("resources") - and after["fd"] <= baseline - and quiet["fd"] <= baseline - ) - - -def setup(values): - return ( - isinstance(values, list) - and len(values) == 40 - and all(integer(v, 1) and v < 10_000_000 for v in values) - ) - - -def rebuild(value, event): - rows = value.get("cycles", []) - return ( - value.get("count") == 100 - and value.get("same_running_session") is True - and value.get("tcp_groups") == list(TCP_GROUPS) - and value.get("udp_groups") == list(UDP_GROUPS) - and len(rows) == 100 - and stopped(value, event) - and all( - row.get("generation") == i - and row.get("tcp") == row.get("udp") == 20 - and row.get("per_group_tcp") == row.get("per_group_udp") == 5 - and row.get("flow_profiles") == expected_flows(i) - and sample(row.get("active", {})) - and sample(row.get("after_clients", {}), active=False) - and setup(row.get("setup_us")) - and row.get("fault") == "owned-peer-connections-closed" - and row.get("new_clients") is True - for i, row in enumerate(rows) - ) - ) - - -def soak(value, event, curve): - points, faults, switches = ( - value.get("samples", []), - value.get("faults", []), - value.get("switches", []), - ) - if not ( - value.get("requested_seconds") == 1800 - and 1800 <= value.get("seconds", 0) < 1860 - and value.get("tcp_groups") == list(TCP_GROUPS) - and value.get("udp_groups") == list(UDP_GROUPS) - and value.get("tcp") == value.get("udp") == 20 - and value.get("per_group_tcp") == value.get("per_group_udp") == 5 - and value.get("first_profiles") == expected_flows(0) - and value.get("last_profiles") == expected_flows(len(faults)) - and integer(value.get("waves"), 1) - and value.get("normal_verified_bytes") == value["waves"] * 20 * 2 * 2 * 257 - and all( - value.get(k) == 0 - for k in ( - "normal_corruption", - "normal_misdirection", - "normal_unexpected_loss", - ) - ) - and len(points) >= 25 - and points == curve - and 29 <= len(faults) <= 30 - and len(switches) >= 1799 - and all(i <= t < i + 5 for i, t in enumerate(switches, 1)) - and all( - 60 * i <= row.get("start", 0) < 60 * i + 5 - and row["start"] <= row.get("end", 0) < row["start"] + 10 - and row.get("old_tcp_closed") - == row.get("new_tcp") - == row.get("new_udp") - == 20 - and row.get("replay") is False - for i, row in enumerate(faults, 1) - ) - and all( - sample(p) and 300 + 60 * i <= p.get("seconds", 0) < 310 + 60 * i - for i, p in enumerate(points) - ) - and stopped(value, event) - and sample(value.get("active_end", {})) - and setup(value.get("first_setup_us")) - and setup(value.get("last_setup_us")) - ): - return False - first_setup, last_setup = ( - int(median(value[k])) for k in ("first_setup_us", "last_setup_us") - ) - early, late = points[:10], points[-10:] - first, last = ( - int(median(p["heap_in_use"] for p in rows)) for rows in (early, late) - ) - allowed = max(1024 * 1024, first // 20) - - def counts(rows, kind): - return max( - next(r["current"] for r in p["resources"]["counts"] if r["kind"] == kind) - for p in rows - ) - - return ( - first_setup == value.get("first_setup_median_us") - and last_setup == value.get("last_setup_median_us") <= 2 * first_setup - and value.get("heap") - == dict(early_median=first, late_median=last, allowed_growth=allowed) - and last <= first + allowed - and all(counts(late, kind) <= counts(early, kind) for kind in RESOURCE_KINDS) - and max(p["fd"] for p in late) <= max(p["fd"] for p in early) - ) diff --git a/scripts/src/vcore_scripts/protocol_integration_native.py b/scripts/src/vcore_scripts/protocol_integration_native.py deleted file mode 100644 index 48b3fe3..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_native.py +++ /dev/null @@ -1,352 +0,0 @@ -"""Eight-protocol integration with owned official peers and container origins.""" - -from __future__ import annotations - -import base64 -import contextlib -import json -import os -import shutil -import sys -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import download_native -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_fixtures import certificates -from .protocol_inputs import redact, sha256, source_identity -from .protocol_integration_catalog import CHAINS, ORDERED -from .protocol_peers import run_command - - -def peer_configuration(protocol, server, origin, directory, *, certificate=None): - cert, key, pin = certificate or certificates(directory) - listener = dict(name="integration", type=protocol, listen="::", port=23000) - node = dict(name="peer", type=protocol, server=server, port=23000, udp=True) - password = "synthetic-integration-fixture" - identity = "07070707-0707-0707-0707-070707070707" - if protocol == "socks5": - listener.update( - type="socks", udp=True, users=[dict(username="fixture", password=password)] - ) - node.update(username="fixture", password=password) - elif protocol == "ss": - password = base64.b64encode(bytes([7]) * 16).decode() - cipher = "2022-blake3-aes-128-gcm" - listener.update(type="shadowsocks", udp=True, cipher=cipher, password=password) - node.update(cipher=cipher, password=password) - elif protocol == "tuic": - listener.update(users={identity: password}, alpn=["h3"]) - node.update( - uuid=identity, - password=password, - sni="localhost", - fingerprint=pin, - alpn=["h3"], - **{"udp-relay-mode": "native"}, - ) - elif protocol in {"trojan", "anytls", "hysteria2"}: - node.update(password=password, sni="localhost", fingerprint=pin) - listener["users"] = ( - [dict(username="fixture", password=password)] - if protocol == "trojan" - else {"fixture": password} - ) - else: - node.update(uuid=identity, tls=True, servername="localhost", fingerprint=pin) - listener["users"] = [dict(username="fixture", uuid=identity)] - if protocol == "vmess": - node.update(cipher="auto", alterId=0) - listener["users"][0]["alterId"] = 0 - node["packet-encoding"] = "xudp" - if protocol not in {"socks5", "ss"}: - listener.update( - certificate=f"/data/fixture/{cert.name}", - **{"private-key": f"/data/fixture/{key.name}"}, - ) - return node, { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "silent", - "hosts": {"vcore-fixture.test": origin}, - "listeners": [listener], - "rules": ["MATCH,DIRECT"], - } - - -def rust_command(consumer="ordered_pair"): - return [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - f"integration::{consumer}", - "--", - "--exact", - "--ignored", - "--nocapture", - ] - - -def run( - output: Path, selected, *, supplied=None, consumer="ordered_pair", domain_peer=None -): - if ( - not selected - or len(selected) != len(set(selected)) - or not set(selected) <= ORDERED.keys() - ): - raise ValueError("invalid INTEGRATION pair selection") - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError( - "INTEGRATION evidence must stay inside the owned run directory" - ) - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="INTEGRATION", - scope="ordered-pair-subset" if consumer == "ordered_pair" else "diagnostic", - source=source_identity(), - status="NOT RUN", - cases=[], - peers={}, - isolation={}, - ) - try: - if supplied is None: - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "binaries", identity=identity - ) - else: - binary, identity = supplied - if sha256(binary) != identity["binary_sha256"]: - raise RuntimeError("INTEGRATION native peer identity mismatch") - report["peers"]["M"] = identity - if any(ORDERED[key][1] == "trojan" for key in selected): - if domain_peer is None: - artifact = download_native( - "XR", output / "binary-xray", "linux-arm64", defer_version=True - ) - domain_peer = (artifact.binary, artifact.identity) - if sha256(domain_peer[0]) != domain_peer[1]["binary_sha256"]: - raise RuntimeError("INTEGRATION domain terminal identity mismatch") - report["peers"]["XR"] = domain_peer[1] - lab = ContainerLab(report["isolation"], mtu=1500) - for index, identifier in enumerate(selected): - first_kind, last_kind = ORDERED[identifier] - variant = CHAINS.get(identifier) - print(f"INTEGRATION: {identifier}", flush=True) - with tempfile.TemporaryDirectory( - prefix="private-", dir=output - ) as temporary: - root = Path(temporary) - with contextlib.ExitStack() as stack: - origin_dir = root / "origin" - origin_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - origin_command = ["env", "VCORE_ISOLATED_ORIGIN=1"] - cover_certificate = None - if variant: - cover_certificate = certificates(origin_dir) - shutil.copyfile( - Path(__file__).with_name("container_shadowtls_peer.py"), - origin_dir / "cover.py", - ) - origin_command += [ - "VCORE_ORIGIN_CERT=/data/fixture/cert.pem", - "VCORE_ORIGIN_KEY=/data/fixture/key.pem", - ] - origin_command += [ - "python", - "-B", - "/data/fixture/cover.py" - if variant - else "/data/fixture/origin.py", - ] - origin = lab.start( - stack, - origin_dir, - f"{index}-origin", - origin_command, - ) - origin.release() - origin.wait_tcp(24000) - if variant: - origin.wait_tcp(24001) - fixture = dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - data_dir=str(root / "core"), - ) - if variant: - fixture["variant"] = variant - for role, protocol in (("first", first_kind), ("last", last_kind)): - directory = root / role - directory.mkdir() - shutil.copy2(binary, directory / "peer") - peer = lab.start( - stack, - directory, - f"{index}-{role}", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - identity["version"] = command( - "exec", peer.name, "/data/fixture/peer", "-v" - ).strip() - if ( - command( - "exec", peer.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - != identity["binary_sha256"] - ): - raise RuntimeError( - "INTEGRATION container binary differs from download" - ) - node, config = peer_configuration( - protocol, peer.ipv4, origin.ipv4, directory - ) - if variant and role == "first": - node["udp-relay-mode"] = variant["udp_mode"] - elif variant: - from .protocol_completion_peers import PeerCase - from .protocol_completion_peers import ( - peer_configuration as profile, - ) - - node, listener = profile( - PeerCase( - identifier, "ss", variant["cipher"], shadow_tls=True - ), - server=peer.ipv4, - cover=f"{origin.ipv4}:24001", - port=23000, - certificate=cover_certificate, - ) - node["name"] = "peer" - config["listeners"] = [listener] - (directory / "config.json").write_text(json.dumps(config)) - fixture[role], fixture[role + "_ipv6"] = node, peer.ipv6 - peer.release() - peer.wait_tcp(23999) - if last_kind == "trojan": - from .protocol_fixtures import trojan_peer_config as peer_config - - directory = root / "domain" - directory.mkdir() - shutil.copy2(domain_peer[0], directory / "peer") - peer = lab.start( - stack, - directory, - f"{index}-domain", - [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ], - ) - domain_peer[1]["version"] = command( - "exec", peer.name, "/data/fixture/peer", "version" - ).strip() - if ( - command( - "exec", peer.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - != domain_peer[1]["binary_sha256"] - ): - raise RuntimeError( - "INTEGRATION domain container binary identity mismatch" - ) - certificate = certificates(directory) - node, _ = peer_configuration( - "trojan", - peer.ipv4, - origin.ipv4, - directory, - certificate=certificate, - ) - cert, key, _ = certificate - config = peer_config( - "XR", - "tcp", - 23000, - node["password"], - f"/data/fixture/{cert.name}", - f"/data/fixture/{key.name}", - ) - config["inbounds"][0]["listen"] = "::" - config["dns"]["hosts"] = {"vcore-fixture.test": origin.ipv4} - (directory / "config.json").write_text(json.dumps(config)) - fixture["domain_last"], fixture["domain_last_ipv6"] = ( - node, - peer.ipv6, - ) - peer.release() - peer.wait_tcp(23000) - path = root / "fixture.json" - path.write_text(json.dumps(fixture)) - events = output / (identifier + "-events.jsonl") - observations = output / (identifier + "-observations.json") - env = dict( - os.environ, - VCORE_VLESS_INPUT=str(path), - VCORE_PROTOCOL_STAGE="INTEGRATION", - VCORE_CASE_EVENTS=str(events), - VCORE_INTEGRATION_OBSERVATIONS=str(observations), - ) - argv = rust_command(consumer) - result = run_command(argv, cwd=CORE_DIR, env=env, timeout=300) - (output / (identifier + ".log")).write_text( - redact(result.stdout.decode(errors="replace")) - ) - report["cases"].append( - dict( - case_id=identifier, - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - seconds=result.seconds, - ) - ) - if result.returncode != 0 or not result.cleanup: - raise RuntimeError("INTEGRATION ordered-pair consumer failed") - report["status"] = "PASS" - except BaseException as error: - report.update(status="FAIL", failure_kind=type(error).__name__) - raise - finally: - report["source_unchanged"] = source_identity() == report["source"] - report["cleanup"] = all( - peer.get("joined") is True for peer in report["isolation"].get("peers", []) - ) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "integration-native.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return report - - -if __name__ == "__main__": - output = Path(sys.argv[1]).resolve() - with exclusive_run(), frozen_image(None): - run(output, sys.argv[2:] or list(ORDERED)) diff --git a/scripts/src/vcore_scripts/protocol_integration_profiles.py b/scripts/src/vcore_scripts/protocol_integration_profiles.py deleted file mode 100644 index f5abe40..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_profiles.py +++ /dev/null @@ -1,66 +0,0 @@ -"""Mixed-load profiles built from the implemented protocol peer fixtures.""" - -from .protocol_completion_peers import CASES, peer_configuration - -TCP_GROUPS = ("socks5", "ss-v3", "tuic", "httpupgrade") -UDP_GROUPS = ("ss-uot", "ss-uot-v3", "tuic", "httpupgrade") -LIFETIME_GROUPS = ( - "socks5", - "anytls", - "ss", - "trojan", - "vmess", - "vless", - "hysteria2", - "tuic", - "ss-v3", - "ss-uot", - "ss-uot-v3", - "httpupgrade", -) - - -def group(case): - if case.protocol == "ss": - return ( - "ss-uot-v3" - if case.uot and case.shadow_tls - else "ss-uot" - if case.uot - else "ss-v3" - ) - return "tuic" if case.protocol == "tuic" else "httpupgrade" - - -PROFILE_IDS = { - name: [c.identifier for c in CASES if c.protocol != "socks5" and group(c) == name] - for name in ("ss-v3", "ss-uot", "ss-uot-v3", "tuic", "httpupgrade") -} - - -def configuration(server, cover, certificate): - nodes, listeners = {}, [] - for i, case in enumerate(c for c in CASES if c.protocol != "socks5"): - node, listener = peer_configuration( - case, server=server, cover=cover, port=23100 + i, certificate=certificate - ) - node["name"] = "peer" - nodes[case.identifier] = node - listeners.append(listener) - return nodes, listeners - - -def selected(group_name, generation, slot=0): - options = PROFILE_IDS.get(group_name, [group_name]) - return options[(generation + slot) % len(options)] - - -def expected_flows(generation): - return { - kind: [ - selected(group_name, generation, slot) - for group_name in groups - for slot in range(5) - ] - for kind, groups in (("tcp", TCP_GROUPS), ("udp", UDP_GROUPS)) - } diff --git a/scripts/src/vcore_scripts/protocol_integration_shared.py b/scripts/src/vcore_scripts/protocol_integration_shared.py deleted file mode 100644 index 03f7174..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_shared.py +++ /dev/null @@ -1,91 +0,0 @@ -"""Frozen strongly-coupled TLS/transport and uninterrupted hopping regressions.""" - -from __future__ import annotations - -from .protocol_evidence import read_json -from .protocol_integration_checks import envelope -from .protocol_security_acceptance import CHECKERS, REPORTS -from .protocol_security_catalog import groups - -SHARED = ( - "SECURITY-SHARED-NONE", - "SECURITY-SHARED-CHROME", - "SECURITY-SHARED-XHTTP", - "SECURITY-HYBRID-RETAINED", - "SECURITY-JLS-RETAINED", - "SECURITY-ECH-CHROME", - "SECURITY-ECH-ENCRYPTION-NATIVE-0RTT-MIXED", - "SECURITY-JLS-ENCRYPTION-NATIVE-0RTT-MIXED", - "SECURITY-HYBRID-ENCRYPTION-NATIVE-0RTT-MIXED", -) -HOPS = ( - "HYSTERIA2-HOP-V4-PLAIN-FIXED", - "HYSTERIA2-HOP-V4-OBFS-RANDOM", - "HYSTERIA2-HOP-V6-PLAIN-RANDOM", - "HYSTERIA2-HOP-V6-OBFS-FIXED", -) - - -def shared_run(directory, supplied): - from .protocol_reality_hybrid import run as hybrid - from .protocol_vless_container import run as vless - from .protocol_xhttp_fields import run as xhttp - - catalog = groups() - for name in SHARED: - print("INTEGRATION shared: " + name, flush=True) - group = catalog[name] - options = {k: v for k, v in group.items() if k not in {"rows", "runner"}} - if group["runner"] == "xhttp": - options["selected"] = list(options["jobs"]) - {"vless": vless, "xhttp": xhttp, "hybrid": hybrid}[group["runner"]]( - directory / name, supplied=supplied, **options - ) - - -def shared_pass(directory, peers, source, digest): - catalog = groups() - for name in SHARED: - group = catalog[name] - root = directory / name - report = read_json(root / REPORTS[group["runner"]]) - if not envelope(report, peers, source, digest) or not CHECKERS[group["runner"]]( - group, report, root - ): - return False - return True - - -def hops_run(directory, supplied): - from .protocol_hysteria2_catalog import H - from .protocol_hysteria2_hop import run - - for name in HOPS: - print("INTEGRATION uninterrupted: " + name, flush=True) - run(directory / name, supplied=supplied, **H[name]) - - -def hops_pass(directory, peers, source, digest): - from .protocol_hysteria2_acceptance import native_result - from .protocol_hysteria2_catalog import definitions - - catalog = {c["case_id"]: c for c in definitions()} - prep = read_json(directory.parent / "package-preparation.json") - if not ( - prep.get("firewall_packages") - and prep.get("peers") - and all(p.get("joined") is True for p in prep["peers"]) - and prep.get("image_digest") == digest - ): - return False - for name in HOPS: - root = directory / name - report = read_json(root / "report.json") - if not ( - envelope(report, peers, source, digest, single="H") - and native_result(catalog[name], report, root)["status"] == "PASS" - and report["isolation"].get("firewall_packages") - == prep["firewall_packages"] - ): - return False - return True diff --git a/scripts/src/vcore_scripts/protocol_integration_suite.py b/scripts/src/vcore_scripts/protocol_integration_suite.py deleted file mode 100644 index 3f93bc0..0000000 --- a/scripts/src/vcore_scripts/protocol_integration_suite.py +++ /dev/null @@ -1,377 +0,0 @@ -"""INTEGRATION public-runtime and mixed-profile gates in owned native peers.""" - -from __future__ import annotations - -import base64 -import contextlib -import json -import os -import re -import shutil -import sys -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import download_native -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_fixtures import certificates -from .protocol_inputs import redact, sha256, source_identity -from .protocol_integration_catalog import PROTOCOLS -from .protocol_integration_native import peer_configuration, rust_command -from .protocol_integration_profiles import PROFILE_IDS -from .protocol_integration_profiles import configuration as profiles -from .protocol_peers import run_command - -CONSUMERS = { - "INTEGRATION-ENTRYPOINTS": "entrypoints", - "INTEGRATION-GRAPH": "graph", - "INTEGRATION-DNS-MEASURE": "dns_measure", - "INTEGRATION-RESOURCE-TRACER": "resource_tracer", - "INTEGRATION-LIFECYCLE": "lifecycle::public_lifetimes", - "INTEGRATION-LIFECYCLE-TRACER": "lifecycle::lifecycle_tracer", - "INTEGRATION-REBUILD": "pressure::rebuild", - "INTEGRATION-REBUILD-TRACER": "pressure::rebuild_tracer", - "INTEGRATION-SOAK": "pressure::soak", - "INTEGRATION-SOAK-TRACER": "pressure::soak_tracer", - "INTEGRATION-SS-ALGORITHMS": "shadowsocks::algorithms", - "INTEGRATION-SS-EIH": "shadowsocks::eih", - "INTEGRATION-FAILURES": "connector::protect_failure", -} - - -def run(output: Path, selected, *, supplied=None): - if ( - not selected - or len(selected) != len(set(selected)) - or not set(selected) <= CONSUMERS.keys() - ): - raise ValueError("invalid INTEGRATION public gate selection") - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError( - "INTEGRATION evidence must stay inside the owned run directory" - ) - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="INTEGRATION", - scope="public-runtime-subset", - source=source_identity(), - status="NOT RUN", - cases=[], - peers={}, - isolation={}, - ) - try: - if supplied is None: - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "binaries", identity=identity - ) - else: - binary, identity = supplied - if sha256(binary) != identity["binary_sha256"]: - raise RuntimeError("INTEGRATION native peer identity mismatch") - report["peers"]["M"] = identity - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary: - root = Path(temporary) - with contextlib.ExitStack() as stack: - origin_dir = root / "origin" - origin_dir.mkdir() - certificate = certificates(origin_dir) - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - shutil.copyfile( - Path(__file__).with_name("container_shadowtls_peer.py"), - origin_dir / "cover.py", - ) - origin = lab.start( - stack, - origin_dir, - "origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "VCORE_ORIGIN_PROFILE=INTEGRATION", - "VCORE_ORIGIN_CERT=/data/fixture/cert.pem", - "VCORE_ORIGIN_KEY=/data/fixture/key.pem", - "python", - "-B", - "/data/fixture/cover.py", - ], - ) - origin.release() - origin.wait_tcp(24000) - origin.wait_tcp(24001) - upstream_dir = root / "upstream" - upstream_dir.mkdir() - shutil.copy2(binary, upstream_dir / "peer") - upstream = lab.start( - stack, - upstream_dir, - "upstream", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - hop, upstream_config = peer_configuration( - "socks5", upstream.ipv4, origin.ipv4, upstream_dir - ) - hop["name"] = "hop" - (upstream_dir / "config.json").write_text(json.dumps(upstream_config)) - upstream.release() - upstream.wait_tcp(23999) - if ( - command( - "exec", upstream.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - != identity["binary_sha256"] - ): - raise RuntimeError( - "INTEGRATION upstream binary differs from download" - ) - directory = root / "native" - directory.mkdir() - shutil.copy2(binary, directory / "peer") - peer = lab.start( - stack, - directory, - "native", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - identity["version"] = command( - "exec", peer.name, "/data/fixture/peer", "-v" - ).strip() - if ( - command( - "exec", peer.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - != identity["binary_sha256"] - ): - raise RuntimeError( - "INTEGRATION container binary differs from download" - ) - for path in certificate[:2]: - shutil.copy2(path, directory / path.name) - nodes, listeners = {}, [] - config = None - for index, protocol in enumerate(PROTOCOLS): - node, current = peer_configuration( - protocol, - peer.ipv4, - origin.ipv4, - directory, - certificate=certificate, - ) - port = 23000 + index - node["port"] = port - current["listeners"][0].update(name=protocol, port=port) - nodes[protocol] = node - listeners.extend(current["listeners"]) - config = current - ss_nodes = [nodes["ss"].copy()] - for index, cipher in enumerate( - ("2022-blake3-aes-256-gcm", "2022-blake3-chacha20-poly1305") - ): - node = nodes["ss"].copy() - node.update( - port=23010 + index, - cipher=cipher, - password=base64.b64encode(bytes([7]) * 32).decode(), - ) - listeners.append( - dict( - name=cipher, - type="shadowsocks", - listen="::", - port=node["port"], - cipher=cipher, - password=node["password"], - udp=True, - ) - ) - ss_nodes.append(node) - extra_nodes, extra_listeners = profiles( - peer.ipv4, f"{origin.ipv4}:24001", certificate - ) - nodes.update(extra_nodes) - listeners.extend(extra_listeners) - config.update( - listeners=listeners, - **{ - "external-controller": "0.0.0.0:23998", - "secret": "synthetic-integration-control", - }, - ) - (directory / "config.json").write_text(json.dumps(config)) - peer.release() - peer.wait_tcp(23999) - fixture = dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - nodes=nodes, - profile_groups={ - name: PROFILE_IDS.get(name, [name]) - for name in ( - *PROTOCOLS, - "ss-v3", - "ss-uot", - "ss-uot-v3", - "httpupgrade", - ) - }, - ss_nodes=ss_nodes, - hop=hop, - server_ipv6=peer.ipv6, - peer_controller=f"{peer.ipv4}:23998", - data_dir=str(root / "core"), - ) - if "INTEGRATION-SS-EIH" in selected: - artifact = download_native( - "SS", output / "binaries-ss", "linux-arm64", defer_version=True - ) - ss_dir = root / "ssserver" - ss_dir.mkdir() - shutil.copy2(artifact.binary, ss_dir / "ssserver") - ss = lab.start( - stack, - ss_dir, - "ssserver", - ["/data/fixture/ssserver", "-c", "/data/fixture/config.json"], - ) - artifact.identity["version"] = command( - "exec", ss.name, "/data/fixture/ssserver", "--version" - ).strip() - if ( - command( - "exec", ss.name, "sha256sum", "/data/fixture/ssserver" - ).split()[0] - != artifact.identity["binary_sha256"] - ): - raise RuntimeError("INTEGRATION SS binary identity mismatch") - report["peers"]["SS"] = artifact.identity - servers, eih_nodes = [], [] - for index, length in enumerate((16, 32)): - cipher = f"2022-blake3-aes-{length * 8}-gcm" - identity_key, user_key = [ - base64.b64encode(bytes([v]) * length).decode() - for v in (9, 8) - ] - port = 23020 + index - servers.append( - dict( - server="::", - server_port=port, - method=cipher, - password=identity_key, - users=[dict(name="fixture", password=user_key)], - mode="tcp_and_udp", - ) - ) - eih_nodes.append( - dict( - name="peer", - type="ss", - server=ss.ipv4, - port=port, - cipher=cipher, - password=f"{identity_key}:{user_key}", - udp=True, - ) - ) - (ss_dir / "config.json").write_text( - json.dumps( - dict( - servers=servers, - outbound_udp_allow_fragmentation=True, - inbound_udp_allow_fragmentation=True, - ) - ) - ) - report["ssserver_policy"] = dict( - outbound_udp_allow_fragmentation=True, - inbound_udp_allow_fragmentation=True, - guest_mtu=1500, - ) - ss.release() - ss.wait_tcp(23020) - fixture["eih_nodes"] = eih_nodes - fixture["eih_ipv6"] = ss.ipv6 - path = root / "fixture.json" - path.write_text(json.dumps(fixture)) - for identifier in selected: - print(f"INTEGRATION: {identifier}", flush=True) - env = dict( - os.environ, - VCORE_VLESS_INPUT=str(path), - VCORE_PROTOCOL_STAGE="INTEGRATION", - VCORE_CASE_EVENTS=str(output / (identifier + "-events.jsonl")), - VCORE_INTEGRATION_OBSERVATIONS=str( - output / (identifier + "-observations.json") - ), - ) - argv = rust_command(CONSUMERS[identifier]) - result = run_command(argv, cwd=CORE_DIR, env=env, timeout=3600) - (output / (identifier + ".log")).write_text( - redact(result.stdout.decode(errors="replace")) - ) - report["cases"].append( - dict( - case_id=identifier, - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - seconds=result.seconds, - ) - ) - if "INTEGRATION-SS-EIH" in selected: - # The follow-log owner buffers while live; snapshot via - # the CLI so a short failed run retains real diagnostics. - text = command("logs", ss.name) - for server in servers: - for key in ( - server["password"], - server["users"][0]["password"], - ): - text = text.replace(key, "") - text = re.sub(r"(?:\d{1,3}\.){3}\d{1,3}", "", text) - (output / "ssserver.log").write_text(redact(text)) - if result.returncode != 0 or not result.cleanup: - raise RuntimeError("INTEGRATION public-runtime consumer failed") - report["status"] = "PASS" - except BaseException as error: - report.update(status="FAIL", failure_kind=type(error).__name__) - raise - finally: - report["source_unchanged"] = source_identity() == report["source"] - report["cleanup"] = all( - peer.get("joined") is True for peer in report["isolation"].get("peers", []) - ) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "integration-suite.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return report - - -if __name__ == "__main__": - with exclusive_run(), frozen_image(None): - run(Path(sys.argv[1]), sys.argv[2:] or list(CONSUMERS)) diff --git a/scripts/src/vcore_scripts/protocol_jls.py b/scripts/src/vcore_scripts/protocol_jls.py deleted file mode 100644 index 0a160aa..0000000 --- a/scripts/src/vcore_scripts/protocol_jls.py +++ /dev/null @@ -1,104 +0,0 @@ -"""JLS public consumers against official latest Mihomo in isolated containers.""" - -from __future__ import annotations - -from pathlib import Path - - -def catalog(): - cases = {} - - def add(mode, suffix, test): - cases[f"SECURITY-JLS-{mode.removesuffix('-tls').upper()}-{suffix}"] = ( - "M", - mode, - True, - test, - ) - - modes = ( - "tcp-tls", - "ws-tls", - "upgrade-tls", - "upgrade-fast-tls", - "grpc-tls", - ) + tuple( - f"xhttp-{mode}{version}" - for mode in ( - "stream-one", - "stream-up", - "packet-up", - "stream-up-download", - "packet-up-download", - ) - for version in ("", "-h1") - ) - for mode in modes: - add(mode, "BASE", "public_base") - add(mode, "CLOSE", "native_mihomo_close_alignment") - for mode in ( - "tcp-tls", - "ws-tls", - "grpc-tls", - "xhttp-stream-up-download", - "xhttp-stream-up-download-h1", - ): - add(mode, "AUTH", "native_jls_fail_closed") - for mode in modes: - if "download" in mode: - add(mode, "IDENTITY", "public_jls_download_identity") - for mode in ("tcp-tls", "grpc-tls"): - for suffix, test in ( - ("GRAPH", "runtime::public_graph"), - ("IPV6", "runtime::public_ipv6_and_gates"), - ("ENTRYPOINTS", "runtime::public_entrypoints"), - ("UDP-ISOLATION", "runtime::public_udp_isolation"), - ): - add(mode, suffix, test) - for mode in ("grpc-tls", "xhttp-stream-up-download"): - add(mode, "LIFE", "runtime::public_lifecycle") - add(mode, "OWNED", "runtime::owned_resources") - return cases - - -def configuration(node, peer, origin, mode): - credentials = dict(username="synthetic-jls-user", password="synthetic-jls-password") - download = dict( - username="synthetic-download-user", password="synthetic-download-password" - ) - node.pop("fingerprint", None) - node["jls-opts"] = credentials - if mode.endswith("-h1"): - node["alpn"] = ["http/1.1"] - for listener in peer["listeners"]: - listener.pop("certificate", None) - listener.pop("private-key", None) - listener["jls-config"] = dict( - enable=True, - sni="localhost", - dest=f"{origin}:24001", - users=[credentials, download], - ) - return dict(jls_download_credentials=download) - - -if __name__ == "__main__": - import argparse - - from .mihomo_isolation import exclusive_run - from .protocol_vless_container import CLIENT_FINGERPRINTS, run - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("cases", nargs="*") - parser.add_argument("--client-fingerprint", choices=CLIENT_FINGERPRINTS) - args = parser.parse_args() - with exclusive_run(): - raise SystemExit( - run( - args.output.resolve(), - args.cases or None, - jls=True, - client_fingerprint=args.client_fingerprint, - ) - ) diff --git a/scripts/src/vcore_scripts/protocol_peers.py b/scripts/src/vcore_scripts/protocol_peers.py deleted file mode 100644 index b385880..0000000 --- a/scripts/src/vcore_scripts/protocol_peers.py +++ /dev/null @@ -1,170 +0,0 @@ -"""Owned native-peer processes. Stop is a wait barrier, never a global prune.""" - -from __future__ import annotations - -import os -import signal -import socket -import subprocess -import tempfile -import threading -import time -from dataclasses import dataclass -from pathlib import Path - -from .builds import CORE_DIR - - -class OwnedProcess: - def __init__( - self, - command: list[str], - log: Path, - record: dict, - *, - env=None, - cwd=None, - limit=1024 * 1024, - ): - self.command, self.log_path, self.record = command, log, record - self.process = None - self.log = None - self.env, self.cwd, self.limit = env, cwd, limit - self.reader = None - self.overflow = threading.Event() - - def __enter__(self): - self.record.update(started=False, joined=False) - self.log = self.log_path.open("wb") - try: - self.process = subprocess.Popen( - self.command, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - start_new_session=os.name != "nt", - stdin=subprocess.DEVNULL, - env=self.env, - cwd=self.cwd, - ) - except BaseException: - self.log.close() - self.record["joined"] = True - raise - self.record["started"] = True - self.reader = threading.Thread(target=self._read, name="vcore-owned-output") - self.reader.start() - return self - - def _read(self): - stored = 0 - try: - while chunk := self.process.stdout.read(8192): - remaining = self.limit - stored - self.log.write(chunk[:remaining]) - stored += min(len(chunk), remaining) - if len(chunk) > remaining: - self.overflow.set() - self._signal(signal.SIGKILL) - except OSError: - self.overflow.set() - finally: - self.process.stdout.close() - - def _signal(self, value): - try: - if os.name == "nt": - if value == signal.SIGKILL: - self.process.kill() - else: - self.process.terminate() - else: - os.killpg(self.process.pid, value) - except ProcessLookupError: - pass - - def _terminate(self): - if self.process.poll() is None: - self._signal(signal.SIGTERM) - try: - self.process.wait(timeout=5) - except subprocess.TimeoutExpired: - self._signal(signal.SIGKILL) - self.process.wait(timeout=5) - - def _finish_reader(self): - if self.reader is not None: - self.reader.join(timeout=5) - if self.reader.is_alive(): - self._signal(signal.SIGKILL) - self.reader.join(timeout=5) - if self.reader.is_alive(): - raise RuntimeError("owned output reader did not stop") - if self.log is not None and not self.log.closed: - self.log.close() - - def ensure_alive(self): - if self.overflow.is_set(): - raise RuntimeError("native peer output exceeded its bound") - if self.process.poll() is not None: - self.record["unexpected_exit"] = self.process.returncode - raise RuntimeError("native peer exited before case completion") - - def wait_tcp(self, port: int, seconds: float = 10, *, host: str = "127.0.0.1"): - deadline = time.monotonic() + seconds - while time.monotonic() < deadline: - self.ensure_alive() - try: - with socket.create_connection((host, port), 0.1): - self.record["ready"] = True - return - except OSError: - time.sleep(0.02) - raise TimeoutError("native peer readiness timeout") - - def __exit__(self, *_): - try: - self._terminate() - self._finish_reader() - self.record.update(joined=True, exit_code=self.process.returncode) - finally: - if self.reader is None or not self.reader.is_alive(): - self.log.close() - - -@dataclass(frozen=True) -class CommandResult: - returncode: int - stdout: bytes - cleanup: bool - seconds: float - reason: str | None - - -def run_command( - command: list[str], *, timeout: float, env=None, cwd=None, limit=1024 * 1024 -) -> CommandResult: - """Bounded capture with process-group cleanup even on timeout or SIGINT.""" - started = time.monotonic() - record = {} - reason = None - root = CORE_DIR / "target/interop/commands" - root.mkdir(parents=True, exist_ok=True) - with tempfile.TemporaryDirectory(prefix="command-", dir=root) as directory: - log = Path(directory) / "private.log" - with OwnedProcess(command, log, record, env=env, cwd=cwd, limit=limit) as owner: - try: - code = owner.process.wait(timeout=timeout) - except subprocess.TimeoutExpired: - code = 124 - reason = "timeout" - if owner.overflow.is_set(): - code = 125 - reason = "output-limit" - output = log.read_bytes() - return CommandResult( - code, - output, - record.get("joined") is True, - round(time.monotonic() - started, 3), - reason, - ) diff --git a/scripts/src/vcore_scripts/protocol_preflight.py b/scripts/src/vcore_scripts/protocol_preflight.py deleted file mode 100644 index 9d66692..0000000 --- a/scripts/src/vcore_scripts/protocol_preflight.py +++ /dev/null @@ -1,67 +0,0 @@ -"""Independent native prerequisites. Artifact readiness is not wire acceptance.""" - -from __future__ import annotations - -from pathlib import Path - -from .mihomo_release import download_mihomo, latest_release -from .native_release import PeerArtifact, download_native -from .protocol_catalogs import PEERS -from .protocol_peers import run_command - - -def preflight(directory: Path, kinds: set[str], *, container: bool = False): - if not kinds <= PEERS: - raise ValueError("unsupported native peer kind") - directory.mkdir(parents=True, exist_ok=True) - artifacts, records = {}, [] - for kind in sorted(kinds): - record = { - "kind": kind, - "status": "BLOCKED", - "scope": "preflight-only", - "wire_acceptance": "NOT RUN", - } - records.append(record) - try: - if kind == "M": - identity = {} - release = latest_release() - binary = download_mihomo( - release=release, directory=directory / "M", identity=identity - ) - version = run_command([str(binary), "-v"], timeout=10, limit=4096) - if version.returncode != 0 or not version.stdout.strip(): - raise RuntimeError("official peer version query failed") - identity["version"] = version.stdout.decode( - "utf-8", errors="replace" - ).strip() - artifact = PeerArtifact(binary, identity) - artifacts[kind] = artifact - record.update(identity, status="READY") - if container: - record["container_status"] = "BLOCKED" - status = run_command( - ["container", "system", "status"], timeout=10, limit=4096 - ) - if status.returncode != 0: - raise RuntimeError("Apple Container is not running") - container_identity = {} - binary = download_mihomo( - "linux-arm64", - release=release, - directory=directory / "M-container", - identity=container_identity, - ) - artifacts["M-container"] = PeerArtifact(binary, container_identity) - record.update( - container_status="READY", container_artifact=container_identity - ) - else: - artifact = download_native(kind, directory / kind) - artifacts[kind] = artifact - record.update(artifact.identity, status="READY") - except (OSError, RuntimeError, ValueError) as error: - # Never expose URLs with userinfo, paths, config bodies or child output. - record["reason"] = type(error).__name__ - return artifacts, records diff --git a/scripts/src/vcore_scripts/protocol_reality_hybrid.py b/scripts/src/vcore_scripts/protocol_reality_hybrid.py deleted file mode 100644 index b4cacce..0000000 --- a/scripts/src/vcore_scripts/protocol_reality_hybrid.py +++ /dev/null @@ -1,536 +0,0 @@ -"""Hybrid REALITY checks; not complete security suite acceptance.""" - -from __future__ import annotations - -import contextlib -import copy -import hashlib -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_containers import ContainerLab, command -from .protocol_evidence import read_events -from .protocol_fixtures import certificates -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command -from .protocol_vless_peers import configuration -from .protocol_vless_public import events_pass - -MODES = { - "tcp": ("tcp-reality", None), - "ws": ("ws-reality", None), - "upgrade": ("upgrade-reality", None), - "upgrade-fast": ("upgrade-fast-reality", None), - "grpc": ("grpc-reality", None), - "vision": ("vision-reality", None), - **{ - f"{version}-{mode}": ( - f"xhttp-{mode}{'-download' if mode != 'stream-one' else ''}-reality", - alpn, - ) - for version, alpn in (("h1", "http/1.1"), ("h2", "h2")) - for mode in ("stream-one", "stream-up", "packet-up") - }, -} - - -def cases(): - result = {} - for profile in ("none", "chrome"): - for mode in MODES: - result[f"{profile}-{mode}-base"] = (profile, mode, "public_base", "hybrid") - for variant in ("main-classic", "download-classic"): - for version in ("h1", "h2"): - result[f"{profile}-{version}-{variant}"] = ( - profile, - f"{version}-stream-up", - "public_base", - variant, - ) - for mode in ("tcp", "h1-stream-up", "h2-stream-up"): - result[f"{profile}-{mode}-security"] = ( - profile, - mode, - "native_hybrid_fail_closed", - "hybrid", - ) - result[f"{profile}-vision-inner-tls"] = ( - profile, - "vision", - "native_vision_inner_tls", - "hybrid", - ) - for test in ( - "runtime::public_graph", - "runtime::public_ipv6_and_gates", - "runtime::public_entrypoints", - ): - result[f"{profile}-tcp-{test.split('::')[-1]}"] = ( - profile, - "tcp", - test, - "hybrid", - ) - for mode in ("tcp", "grpc", "vision", "h2-stream-up"): - for suffix, test in ( - ("life", "runtime::public_lifecycle"), - ("owned", "runtime::owned_resources"), - ): - result[f"chrome-{mode}-{suffix}"] = ("chrome", mode, test, "hybrid") - return result - - -def observed_events(observer): - raw = command( - "exec", - observer.name, - "python", - "-c", - "from pathlib import Path; p=Path('/data/events.jsonl'); " - "assert p.stat().st_size <= 8388608; print(p.read_text(),end='')", - ) - return [json.loads(line) for line in raw.splitlines()] - - -def wire_pass(events, expectations): - for port, (hybrid, profile) in expectations.items(): - shares = ( - [[4588, 1216], [29, 32]] - if hybrid and profile == "chrome" - else [[4588, 1216]] - if hybrid - else [[29, 32]] - ) - hellos = [ - e - for e in events - if e.get("port") == port and e.get("event") == "client-hello" - ] - selections = [ - e - for e in events - if e.get("port") == port and e.get("event") == "server-hello" - ] - if ( - not hellos - or not selections - or not all(e["shares"] == shares for e in hellos) - ): - return False - if not all(e["group"] == (4588 if hybrid else 29) for e in selections): - return False - return True - - -def negative_wire_pass(events, profile, front=25000, legs=1): - for offset, cover, group in ((2, 24432, 29), (3, 24433, 24)): - observed = [e for e in events if e.get("port") == front + offset] - hellos = [e for e in observed if e.get("event") == "client-hello"] - replies = [e for e in observed if e.get("event") == "server-hello"] - if len(hellos) != legs: - return False - if profile == "chrome": - if ( - len(replies) != legs - or not all( - e.get("group") == group and e.get("hrr") is (offset == 3) - for e in replies - ) - or not all( - e.get("shares") == [[4588, 1216], [29, 32]] - and e.get("groups") == [4588, 29, 23, 24] - for e in hellos - ) - ): - return False - else: - # A hybrid-only offer has no common group with these peers. A - # missing ServerHello alone is not proof: require the cover's - # native no-shared-group error and an empty, closed server flight. - rejected = [ - e - for e in events - if e.get("port") == cover and e.get("event") == "no-shared-group" - ] - closed = [ - e - for e in observed - if e.get("event") == "server-close" and e.get("bytes") == 0 - ] - if ( - replies - or len(rejected) != legs - or len(closed) != legs - or not all( - e.get("shares") == [[4588, 1216]] and e.get("groups") == [4588] - for e in hellos - ) - ): - return False - return True - - -def run(output: Path, selected=None, *, encryption=None, supplied=None): - output = output.resolve() - if not output.is_relative_to(CORE_DIR / "target/interop/runs"): - raise ValueError("use a fresh child of target/interop/runs") - required = cases() - selected = list(required) if selected is None else selected - if ( - not selected - or len(set(selected)) != len(selected) - or not set(selected) <= required.keys() - ): - raise ValueError("invalid SECURITY.reality selection") - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="SECURITY.reality", - scope="S03/D16-production", - encryption_profile=encryption, - complete_selection=set(selected) == set(required), - required=list(required), - source=source_identity(), - cases=[], - isolation={}, - peers={}, - status="NOT RUN", - ) - try: - if supplied is None: - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "binaries", identity=identity - ) - else: - binary, identity = supplied["M"] - report["peers"]["M"] = identity - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "--test", - "vless_native", - "--no-run", - ], - timeout=240, - ) - (output / "build.log").write_text(redact(built.stdout.decode(errors="replace"))) - if built.returncode or not built.cleanup: - raise RuntimeError("SECURITY.reality consumer build failed") - lab = ContainerLab(report["isolation"], mtu=1500) - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - origin_dir, server_dir, observer_dir, hop_dir = [ - root / p for p in ("origin", "server", "observer", "hop") - ] - for directory in (origin_dir, server_dir, observer_dir, hop_dir): - directory.mkdir() - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - shutil.copy2( - Path(__file__).with_name("container_reality_hybrid.py"), - observer_dir / "observer.py", - ) - shutil.copy2(binary, server_dir / "peer") - shutil.copy2(binary, hop_dir / "peer") - cert, key, pin = certificates(origin_dir) - certificates(observer_dir) - origin = lab.start( - stack, - origin_dir, - "security-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - f"VCORE_ORIGIN_CERT=/data/fixture/{cert.name}", - f"VCORE_ORIGIN_KEY=/data/fixture/{key.name}", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - observer = lab.start( - stack, - observer_dir, - "security-observer", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/observer.py", - ], - ) - server = lab.start( - stack, - server_dir, - "security-mihomo", - [ - "/data/fixture/peer", - "-d", - "/data/mihomo", - "-f", - "/data/fixture/config.json", - ], - ) - hop = lab.start( - stack, - hop_dir, - "security-upstream", - [ - "/data/fixture/peer", - "-d", - "/data/mihomo", - "-f", - "/data/fixture/config.json", - ], - ) - listeners, routes, nodes, ports = [], {}, {}, {} - for index, (name, (mode, alpn)) in enumerate(MODES.items()): - node, config = configuration( - mode, - observer.ipv4, - origin.ipv4, - Path("/data/fixture/cert.pem"), - Path("/data/fixture/key.pem"), - ) - backend, front = 23010 + index * 10, 25000 + index * 10 - listener = config["listeners"][0] - if encryption: - from .protocol_encryption_public import configuration as encrypt - - encrypt(encryption, node, config) - listener.update(name=name, port=backend) - listener["reality-config"]["dest"] = f"{observer.ipv4}:24431" - listeners.append(listener) - routes[str(front)] = routes[str(front + 1)] = [server.ipv4, backend] - if name == "tcp": - for offset, cover in ((2, 24432), (3, 24433)): - negative = copy.deepcopy(listener) - negative.update(name=f"reject-{cover}", port=backend + offset) - negative["reality-config"]["dest"] = f"{observer.ipv4}:{cover}" - listeners.append(negative) - routes[str(front + offset)] = [server.ipv4, backend + offset] - node.update(name="peer", port=front) - node["reality-opts"]["support-x25519mlkem768"] = True - if alpn: - node["alpn"] = [alpn] - if "download-settings" in node.get("xhttp-opts", {}): - node["xhttp-opts"]["download-settings"] = { - "server": observer.ipv4, - "port": front + 1, - } - nodes[name], ports[name] = node, front - (observer_dir / "routes.json").write_text(json.dumps(routes)) - (server_dir / "config.json").write_text( - json.dumps( - { - "ipv6": True, - "log-level": "silent", - "listeners": listeners, - "hosts": {"vcore-fixture.test": origin.ipv4}, - "rules": ["MATCH,DIRECT"], - } - ) - ) - (hop_dir / "config.json").write_text( - json.dumps( - { - "socks-port": 23001, - "allow-lan": True, - "bind-address": "*", - "log-level": "silent", - "ipv6": True, - "hosts": {"peer.fixture.test": observer.ipv4}, - "rules": ["MATCH,DIRECT"], - } - ) - ) - for peer, port in ( - (origin, 24000), - (observer, 24431), - (server, 23010), - (hop, 23001), - ): - peer.release() - peer.wait_tcp(port) - identity["version"] = command( - "exec", server.name, "/data/fixture/peer", "-v" - ).strip() - if ( - command("exec", server.name, "sha256sum", "/data/fixture/peer").split()[ - 0 - ] - != identity["binary_sha256"] - ): - raise RuntimeError("native peer identity mismatch") - report["openssl"] = command( - "exec", - observer.name, - "python", - "-c", - "import ssl; print(ssl.OPENSSL_VERSION)", - ).strip() - for case in selected: - profile, mode, test, variant = required[case] - node = copy.deepcopy(nodes[mode]) - node["client-fingerprint"] = profile - expectations = {ports[mode]: (True, profile)} - if node.get("xhttp-opts", {}).get("download-settings") is not None: - expectations[ports[mode] + 1] = (True, profile) - if variant != "hybrid": - down = node["xhttp-opts"]["download-settings"] - down["reality-opts"] = copy.deepcopy(node["reality-opts"]) - if variant == "main-classic": - node["reality-opts"]["support-x25519mlkem768"] = False - expectations[ports[mode]] = (False, profile) - else: - down["reality-opts"]["support-x25519mlkem768"] = False - expectations[ports[mode] + 1] = (False, profile) - f = dict( - isolation="containers", - node=node, - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - server_ipv6=observer.ipv6, - origin_pin=pin, - data_dir=str(root / "data"), - peer_kind="M", - vision_probe=mode == "vision", - hop=dict( - name="hop", type="socks5", server=hop.ipv4, port=23001, udp=True - ), - downgrade_port=ports["tcp"] + 2, - hrr_port=ports["tcp"] + 3, - ordinary_tls_port=24431, - tls12_port=24434, - ) - fixture = root / "input.json" - fixture.write_text(json.dumps(f)) - before = len(observed_events(observer)) - events_path = output / f"{case}-events.jsonl" - test_command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_native" if test.startswith("native_") else "vless_public", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - print(case, flush=True) - result = run_command( - test_command, - timeout=240, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_CASE_EVENTS=str(events_path), - VCORE_PROTOCOL_STAGE="SECURITY", - ), - ) - (output / f"{case}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = observed_events(observer)[before:] - wire_path = output / f"{case}-wire.json" - wire_path.write_text(json.dumps(observed, indent=2) + "\n") - events = read_events(events_path) if events_path.exists() else [] - remapped = [ - dict(e, suite=e.get("suite", "").replace("SECURITY-", "VLESS-", 1)) - for e in events - ] - assertions = ( - remapped - == [ - dict( - schema_version=1, - suite="VLESS-WIRE", - assertion=test, - status=status, - ) - for status in ("BEGIN", "PASS") - ] - if test.startswith("native_") - else events_pass(remapped, test, MODES[mode][0]) - ) - # Security matrices deliberately contain rejected peers; their - # strict per-attempt assertions require errors, never timeouts. - wire_valid = wire_pass(observed, expectations) - if test == "native_hybrid_fail_closed": - wire_valid &= negative_wire_pass( - observed, profile, ports["tcp"], len(expectations) - ) - passed = ( - result.returncode == 0 - and result.cleanup - and assertions - and wire_valid - ) - report["cases"].append( - dict( - id=case, - test=test, - variant=variant, - profile=profile, - status="PASS" if passed else "FAIL", - exit_code=result.returncode, - command_cleanup=result.cleanup, - seconds=result.seconds, - command=test_command, - wire_sha256=hashlib.sha256(wire_path.read_bytes()).hexdigest(), - assertions=assertions, - wire_valid=wire_valid, - ) - ) - print(f"{case}: {report['cases'][-1]['status']}", flush=True) - for peer in (origin, observer, server, hop): - peer.ensure_alive() - if not passed: - raise RuntimeError(f"SECURITY.reality case failed: {case}") - report["status"] = "PASS" - except KeyboardInterrupt: - report.update(status="INTERRUPTED", reason="user interruption") - except (OSError, RuntimeError, ValueError, KeyError) as error: - report.update( - status="FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - finally: - report["cleanup"] = all( - p["joined"] for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "reality-results.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return 0 if report["status"] == "PASS" else 1 - - -def main(output, selected=None): - with exclusive_run(): - return run(output, selected) diff --git a/scripts/src/vcore_scripts/protocol_script_tests.py b/scripts/src/vcore_scripts/protocol_script_tests.py deleted file mode 100644 index c0fa748..0000000 --- a/scripts/src/vcore_scripts/protocol_script_tests.py +++ /dev/null @@ -1,68 +0,0 @@ -"""Structured unittest results for the protocol harness; no stdout PASS parsing.""" - -from __future__ import annotations - -import contextlib -import io -import json -import sys -import unittest -from pathlib import Path - -from .builds import CORE_DIR - - -class Result(unittest.TestResult): - def __init__(self): - super().__init__() - self.cases = [] - - def addSuccess(self, test): - super().addSuccess(test) - self.cases.append({"test": test.id(), "status": "PASS"}) - - def addFailure(self, test, error): - super().addFailure(test, error) - self.cases.append({"test": test.id(), "status": "FAIL", "reason": "assertion"}) - - def addError(self, test, error): - super().addError(test, error) - self.cases.append( - {"test": test.id(), "status": "FAIL", "reason": error[0].__name__} - ) - - def addSkip(self, test, reason): - super().addSkip(test, reason) - self.cases.append({"test": test.id(), "status": "NOT RUN"}) - - -class BoundedOutput(io.TextIOBase): - def __init__(self): - self.size = 0 - - def write(self, text): - self.size += len(text) - if self.size > 128 * 1024: - raise RuntimeError("script test output exceeded bound") - return len(text) - - -def execute(path: Path) -> bool: - result = Result() - try: - suite = unittest.defaultTestLoader.discover(str(CORE_DIR / "scripts/tests")) - with ( - contextlib.redirect_stdout(BoundedOutput()), - contextlib.redirect_stderr(BoundedOutput()), - ): - suite.run(result) - finally: - path.write_text( - json.dumps({"tests_run": result.testsRun, "cases": result.cases}, indent=2) - + "\n" - ) - return result.testsRun > 0 and result.wasSuccessful() and not result.skipped - - -if __name__ == "__main__": - raise SystemExit(0 if execute(Path(sys.argv[1])) else 1) diff --git a/scripts/src/vcore_scripts/protocol_security_acceptance.py b/scripts/src/vcore_scripts/protocol_security_acceptance.py deleted file mode 100644 index 5e264fc..0000000 --- a/scripts/src/vcore_scripts/protocol_security_acceptance.py +++ /dev/null @@ -1,662 +0,0 @@ -"""Execute security cases and reconstruct results from commands and native events.""" - -from __future__ import annotations - -import json -import sys -from contextlib import ExitStack - -from .protocol_evidence import HEX, new_result, read_events, read_json -from .protocol_inputs import redact -from .protocol_security_catalog import FIELDS, GATES, definitions, groups - - -def commands(identifier): - from .protocol_hysteria2_acceptance import commands as previous - - cargo = ["cargo", "test", "--locked", "--all-features"] - targets = [ - "ech_config", - "ech_tls", - "encryption_config", - "reality_config", - "jls_config", - ] - if identifier in {"SECURITY-CFG", "SECURITY-RELEASE"}: - if identifier == "SECURITY-RELEASE": - cargo += ["--release"] - return [ - cargo + sum((["--test", name] for name in targets), []), - cargo + ["--lib", "security::"], - cargo + ["--lib", "outbound::vless::encryption"], - cargo + ["--lib", "config::"], - ] - if identifier == "SECURITY-FEATURES": - return previous("HYSTERIA2-FEATURES") + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "outbound-vless", - "--test", - "ech_config", - "--test", - "ech_tls", - ], - [ - "cargo", - "test", - "--locked", - "--all-features", - "--all-targets", - "--no-run", - ], - ] - if identifier == "SECURITY-SCRIPTS": - return [ - [ - sys.executable, - "-m", - "vcore_scripts.protocol_script_tests", - "{output}/script-tests.json", - ] - ] - return previous(identifier.replace("SECURITY-", "HYSTERIA2-")) - - -def gate_result(case, records, events, script=None): - from .protocol_hysteria2_catalog import assertions_pass - from .protocol_security_units import TESTS - - identifier = case["case_id"] - good = bool(records) and all( - r.get("exit_code") == 0 and r.get("cleanup") is True for r in records - ) - if identifier in {"SECURITY-CFG", "SECURITY-RELEASE"}: - # Config and TLS tests each have an exact BEGIN/PASS pair. Library - # regressions run separately without these assertion identities. - selected = [e for e in events if e.get("suite") == "SECURITY-CONFIG-TLS"] - good &= assertions_pass( - selected, - { - ("SECURITY-CONFIG-TLS", name): 1 - for names in TESTS.values() - for name in names - }, - ) - good &= all(e.get("status") in {"BEGIN", "PASS"} for e in events) - if identifier == "SECURITY-SCRIPTS": - samples = (script or {}).get("cases", []) - names = [s.get("test") for s in samples] - required = { - "test_protocol_security.SecurityAcceptanceTest." + name - for name in ( - "test_frozen_scope_and_compositions", - "test_native_missing_duplicate_wrong_events_fail", - "test_source_cleanup_and_identity_are_not_inferred", - "test_empty_gate_and_missing_script_proof_fail", - "test_legacy_ech_gateway_keeps_response_inside_tls", - "test_close_reference_cannot_hide_a_different_official_profile", - ) - } - required |= { - "test_protocol_containers.ContainerTests." + name - for name in ( - "test_frozen_latest_image_is_single_run_owned", - "test_image_refresh_failure_never_falls_back_to_a_cached_image", - ) - } - good &= ( - bool(samples) - and len(names) == len(set(names)) == (script or {}).get("tests_run") - and required <= set(names) - and all(s.get("status") == "PASS" for s in samples) - ) - return result( - case, good, bool(records) and all(r.get("cleanup") is True for r in records) - ) - - -def result(case, good, cleanup): - record = new_result(case) - record.update( - status="PASS" if good else "FAIL", - assertions={case["case_id"]: bool(good)}, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=cleanup, - ) - return record - - -def native_envelope(report, peers=None, source=None, *, image_digest=None): - isolation = report.get("isolation", {}) - owned = isolation.get("peers", []) - identities = report.get("peers", {}) - if "peer" in report: - identities = {"M": report["peer"]} - good = ( - report.get("status") == "PASS" - and report.get("cleanup") is True - and report.get("source_unchanged") is True - and isolation.get("network_mode") == "hostOnly" - and isolation.get("host_servers") is False - and isolation.get("guest_mtu") == 1500 - and isolation.get("image_digest", "").startswith("sha256:") - and bool(HEX.fullmatch(isolation.get("image_digest", "")[7:])) - and bool(owned) - and all(p.get("started") is True and p.get("joined") is True for p in owned) - and bool(identities) - ) - for kind, identity in identities.items(): - good &= ( - bool(identity.get("version")) - and bool(HEX.fullmatch(identity.get("binary_sha256", ""))) - and bool(HEX.fullmatch(identity.get("archive_sha256", ""))) - and identity.get("source_url", "").startswith( - { - "M": "https://github.com/MetaCubeX/mihomo/releases/", - "XR": "https://github.com/XTLS/Xray-core/releases/", - "V2": "https://github.com/v2fly/v2ray-core/releases/", - }.get(kind, "invalid-source") - ) - ) - if peers is not None: - good &= identity == peers.get(kind) - if source is not None: - good &= report.get("source") == source - if image_digest is not None: - good &= isolation.get("image_digest") == image_digest - return bool(good) - - -def rust_command(target, test, *, fields=False): - return [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - target, - test, - "--", - *(["--exact", "--ignored"] if fields else ["--ignored", "--exact"]), - "--nocapture", - ] - - -def vless_catalog(group): - if group.get("ech"): - from .protocol_ech import catalog - - return catalog() - if group.get("jls"): - from .protocol_jls import catalog - - return catalog() - from .protocol_vless_container import ALL_CASES - - return ALL_CASES | { - "FINGERPRINT-ANYTLS": ("M", "anytls", True, "public_legacy_regression") - } - - -def close_reference_pass(group, mode, directory): - profile = group.get("client_fingerprint") - expected_profile, scope = profile, "same-mode" - if mode == "grpc-tls": - if group.get("jls") and profile in (None, "", "none"): - expected_profile, scope = "chrome", "jls-grpc-chrome-baseline" - elif group.get("ech") and profile in ("safari", "safari16"): - expected_profile, scope = "chrome", "ech-safari-chrome-baseline" - try: - reference = read_json(directory / f"{mode}-tls-close-reference.json") - except (OSError, ValueError): - return False - return ( - isinstance(reference, dict) - and reference.get("scope") == scope - and reference.get("client_fingerprint") == expected_profile - and reference.get("dut_client_fingerprint") == profile - and reference.get("terminated") is True - ) - - -def vless_pass(group, report, directory): - from .protocol_vless_public import events_pass - - catalog = vless_catalog(group) - expected = group["selected"] - records = report.get("cases", []) - if len(records) != len(expected) or {r.get("case_id") for r in records} != set( - expected - ): - return False - if any(report.get(k) != group.get(k, False) for k in ("jls", "ech")): - return False - if report.get("encryption_profile") != group.get("encryption") or report.get( - "client_fingerprint" - ) != group.get("client_fingerprint"): - return False - stage = ( - "SECURITY" - if group.get("ech") or group.get("jls") or group.get("encryption") - else "VLESS" - ) - for record in records: - name = record["case_id"] - kind, mode, _, test = catalog[name] - native = test.startswith("native_") - events = read_events(directory / (name + "-events.jsonl")) - if not ( - record.get("status") == "PASS" - and record.get("exit_code") == 0 - and record.get("command_cleanup") is True - and record.get("cleanup") is True - and record.get("peer_kind") == kind - and record.get("command") - == rust_command("vless_native" if native else "vless_public", test) - ): - return False - if native: - if test == "native_mihomo_close_alignment" and not close_reference_pass( - group, mode, directory - ): - return False - if events != [ - dict( - schema_version=1, - suite=stage + "-WIRE", - assertion=test, - status=status, - ) - for status in ("BEGIN", "PASS") - ]: - return False - elif not events_pass(events, test, mode, stage=stage): - return False - return True - - -def xhttp_pass(group, report, directory): - from .protocol_xhttp_fields import PUBLIC_TESTS, events_pass, public_events_pass - - jobs = group["jobs"] - expected = { - entry["case_id"]: (variant, entry["test"]) - for variant, entries in jobs.items() - for entry in entries - } - records = report.get("cases", []) - if len(records) != len(expected) or {r.get("case_id") for r in records} != set( - expected - ): - return False - if ( - report.get("ech") != group.get("ech", False) - or report.get("jls") != group.get("jls", False) - or report.get("encryption_profile") != group.get("encryption") - ): - return False - for record in records: - variant, test = expected[record["case_id"]] - events = read_events(directory / (record["case_id"] + "-events.jsonl")) - if not ( - record.get("variant") == variant - and record.get("assertion") == test - and record.get("status") == "PASS" - and record.get("exit_code") == 0 - and record.get("command_cleanup") is True - and record.get("command") - == rust_command( - "vless_public" if test in PUBLIC_TESTS else "xhttp_native", - test, - fields=True, - ) - ): - return False - if not ( - public_events_pass(events, test) - if test in PUBLIC_TESTS - else events_pass(events, test, owned=test.startswith("lifecycle::")) - ): - return False - return True - - -def hybrid_pass(group, report, directory): - from .protocol_reality_hybrid import MODES, cases, negative_wire_pass, wire_pass - from .protocol_vless_public import events_pass - - catalog = cases() - records = report.get("cases", []) - selected = group["selected"] - if ( - len(records) != len(selected) - or {r.get("id") for r in records} != set(selected) - or report.get("encryption_profile") != group.get("encryption") - ): - return False - ports = {mode: 25000 + i * 10 for i, mode in enumerate(MODES)} - for record in records: - name = record["id"] - profile, mode, test, variant = catalog[name] - expected = {ports[mode]: (True, profile)} - if mode.startswith(("h1-", "h2-")) and not mode.endswith("stream-one"): - expected[ports[mode] + 1] = (True, profile) - if variant != "hybrid": - expected[ports[mode] + (variant == "download-classic")] = (False, profile) - wire = read_json(directory / (name + "-wire.json")) - events = read_events(directory / (name + "-events.jsonl")) - native = test.startswith("native_") - good = ( - record.get("status") == "PASS" - and record.get("exit_code") == 0 - and record.get("command_cleanup") is True - and (record.get("test"), record.get("variant"), record.get("profile")) - == (test, variant, profile) - and record.get("command") - == rust_command("vless_native" if native else "vless_public", test) - and wire_pass(wire, expected) - ) - if test == "native_hybrid_fail_closed": - good &= negative_wire_pass(wire, profile, ports["tcp"], len(expected)) - good &= ( - events - == [ - dict( - schema_version=1, - suite="SECURITY-WIRE", - assertion=test, - status=status, - ) - for status in ("BEGIN", "PASS") - ] - if native - else events_pass(events, test, MODES[mode][0], stage="SECURITY") - ) - if not good: - return False - return True - - -def encryption_pass(group, report, directory): - from .protocol_encryption import cases - - expected = [ - key - for key, (_, rtt, _) in cases().items() - if not group.get("expiry") or rtt == "0rtt" - ] - records = report.get("cases", []) - test = ( - "native_ticket_expiry" if group.get("expiry") else "native_encryption_roundtrip" - ) - marker = ( - "SECURITY-ENCRYPTION-EXPIRY-PASS full-resumed-expired-full-resumed" - if group.get("expiry") - else "SECURITY-ENCRYPTION-WIRE-PASS rounds=4 bytes_per_direction=10485760" - ) - if ( - len(records) != len(expected) - or {r.get("case_id") for r in records} != set(expected) - or report.get("ticket_expiry") != group.get("expiry", False) - or report.get("cipher") - != ("chacha20-poly1305" if group.get("chacha") else "cpu-selected") - or report.get("padding") != "minimal" - ): - return False - for record in records: - log = (directory / (record["case_id"] + ".log")).read_text() - if not ( - record.get("status") == "PASS" - and record.get("returncode") == 0 - and record.get("cleanup") is True - and marker in log - and "1 passed; 0 failed; 0 ignored" in log - and record.get("command") == rust_command("encryption_wire", test) - ): - return False - return True - - -REPORTS = { - "vless": "vless-results.json", - "xhttp": "xhttp-fields-results.json", - "hybrid": "reality-results.json", - "encryption": "encryption-results.json", -} -CHECKERS = { - "vless": vless_pass, - "xhttp": xhttp_pass, - "hybrid": hybrid_pass, - "encryption": encryption_pass, -} - - -def native_result( - case, group, report, directory, *, peers=None, source=None, image_digest=None -): - good = native_envelope(report, peers, source, image_digest=image_digest) - good &= CHECKERS[group["runner"]](group, report, directory) - return result(case, good, report.get("cleanup") is True) - - -def fields_report(cases, results): - by_id = {r["case_id"]: r for r in results} - rows = [] - for field in sorted(FIELDS): - owners = [c["case_id"] for c in cases if field in c["row_ids"]] - cfg = [c for c in owners if c in {"SECURITY-CFG", "SECURITY-RELEASE"}] - wire = [c for c in owners if c not in GATES] - good = ( - bool(cfg) - and bool(wire) - and all(by_id.get(c, {}).get("status") == "PASS" for c in owners) - ) - rows.append( - dict( - id=field, - cfg_cases=cfg, - wire_cases=wire, - status="PASS" if good else "NOT RUN", - ) - ) - return dict( - stage="SECURITY", scope="selected-vless-security-consumers", fields=rows - ) - - -def execute(cases, run, output, results): - from .protocol_containers import frozen_image - from .protocol_encryption import run as run_encryption - from .protocol_harness import _command - from .protocol_reality_hybrid import run as run_hybrid - from .protocol_vless_container import run as run_vless - from .protocol_xhttp_acceptance import prepare_peers - from .protocol_xhttp_fields import run as run_xhttp - - supplied = {} - image_scope = ExitStack() - catalog = groups() - source = { - k: run[k] - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - try: - for case in cases: - identifier = case["case_id"] - if identifier not in GATES: - continue - records, events = [], [] - for i, argv in enumerate(commands(identifier)): - name = f"{identifier}-{i}" - path = output / (name + "-events.jsonl") - _command( - run, - output, - name, - [v.replace("{output}", str(output)) for v in argv], - case["timeout_seconds"], - events=path, - ) - records.append(run["commands"][-1]) - if path.exists(): - events += read_events(path) - script = ( - read_json(output / "script-tests.json") - if identifier == "SECURITY-SCRIPTS" - else None - ) - results[identifier] = gate_result(case, records, events, script) - print(identifier + ": " + results[identifier]["status"], flush=True) - if results[identifier]["status"] != "PASS": - raise RuntimeError("SECURITY local gate failed: " + identifier) - if any(c["case_id"] in catalog for c in cases): - supplied = prepare_peers(output, {"M", "XR", "V2"}) - run["container_image"] = image_scope.enter_context( - frozen_image(output / "container-image-pull.log") - ) - runners = { - "vless": run_vless, - "xhttp": run_xhttp, - "hybrid": run_hybrid, - "encryption": run_encryption, - } - # Retained combinations first, followed by the longer new ECH matrix. - # Every required group still runs in this same frozen source/image scope. - for case in sorted( - cases, - key=lambda c: (c["case_id"].startswith("SECURITY-ECH-"), c["case_id"]), - ): - identifier = case["case_id"] - if identifier in GATES: - continue - group = catalog[identifier] - kwargs = {k: v for k, v in group.items() if k not in {"rows", "runner"}} - if group["runner"] == "xhttp": - kwargs["selected"] = list(kwargs["jobs"]) - directory = output / identifier - runners[group["runner"]](directory, supplied=supplied, **kwargs) - report = read_json(directory / REPORTS[group["runner"]]) - results[identifier] = native_result( - case, - group, - report, - directory, - peers={k: v[1] for k, v in supplied.items()}, - source=source, - image_digest=run["container_image"]["digest"], - ) - print(identifier + ": " + results[identifier]["status"], flush=True) - if results[identifier]["status"] != "PASS": - raise RuntimeError("SECURITY native gate failed: " + identifier) - finally: - image_scope.close() - (output / "peers.json").write_text( - json.dumps({k: v[1] for k, v in supplied.items()}, indent=2) + "\n" - ) - (output / "fields.json").write_text( - json.dumps(fields_report(cases, list(results.values())), indent=2) + "\n" - ) - - -def check(run_dir, cases, run, results, peers, paths): - from .protocol_containers import IMAGE - - snapshot = run.get("container_image", {}) - digest = snapshot.get("digest", "") - if ( - not isinstance(digest, str) - or not digest.startswith("sha256:") - or not HEX.fullmatch(digest[7:]) - or snapshot - != dict( - tag=IMAGE, - digest=digest, - command=["container", "image", "pull", IMAGE], - exit_code=0, - cleanup=True, - log="container-image-pull.log", - ) - or snapshot["log"] not in paths - ): - raise ValueError("missing or altered SECURITY frozen container image") - if ( - cases != definitions() - or set(peers) != {"M", "XR", "V2"} - or not {"fields.json", "script-tests.json"} <= set(paths) - ): - raise ValueError("incomplete SECURITY stage artifacts or altered manifest") - records = run.get("commands", []) - names = { - f"{c['case_id']}-{i}" - for c in cases - for i, _ in enumerate(commands(c["case_id"])) - if c["case_id"] in GATES - } - if len(records) != len(names) or {r.get("name") for r in records} != names: - raise ValueError("missing, duplicate or extra SECURITY gate command") - actual = {r["case_id"]: r for r in results} - source = { - k: run[k] - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - catalog = groups() - for case in cases: - identifier = case["case_id"] - if identifier in GATES: - selected, events = [], [] - for i, argv in enumerate(commands(identifier)): - record = next(r for r in records if r["name"] == f"{identifier}-{i}") - if ( - record["command"] - != [redact(v.replace("{output}", str(run_dir))) for v in argv] - or record["log"] not in paths - ): - raise ValueError("SECURITY gate command or log changed") - selected.append(record) - path = run_dir / f"{identifier}-{i}-events.jsonl" - if path.exists(): - events += read_events(path) - recalculated = gate_result( - case, - selected, - events, - read_json(run_dir / "script-tests.json") - if identifier == "SECURITY-SCRIPTS" - else None, - ) - else: - group = catalog[identifier] - directory = run_dir / identifier - report = read_json(directory / REPORTS[group["runner"]]) - recalculated = native_result( - case, - group, - report, - directory, - peers=peers, - source=source, - image_digest=digest, - ) - if recalculated != actual[identifier] or recalculated["status"] != "PASS": - raise ValueError("SECURITY results differ from independent evidence") - fields = fields_report(cases, results) - if read_json(run_dir / "fields.json") != fields or any( - f["status"] != "PASS" for f in fields["fields"] - ): - raise ValueError("missing SECURITY field behavior coverage") - print(f"SECURITY: PASS ({len(cases)} required groups; {len(FIELDS)} field rows)") diff --git a/scripts/src/vcore_scripts/protocol_security_catalog.py b/scripts/src/vcore_scripts/protocol_security_catalog.py deleted file mode 100644 index e2cbb9a..0000000 --- a/scripts/src/vcore_scripts/protocol_security_catalog.py +++ /dev/null @@ -1,331 +0,0 @@ -"""Selected security requirements, independent of runtime results. - -This is an explicit covering set, not a Cartesian-product support claim. Encryption, -hybrid REALITY and JLS have dedicated reports; this suite exercises their critical -consumers together with static ECH and composition checks. -""" - -from __future__ import annotations - -FIELDS = {"VL06", "S03", "S04", "S05", "S12", "S13", "D16", "D17", "D18", "D25", "D26"} -ECH_FIELDS = ["S04", "S05", "D17", "D18"] -JLS_FIELDS = ["S12", "S13", "D25", "D26"] -HYBRID_FIELDS = ["S03", "D16"] -GATES = { - "SECURITY-CFG", - "SECURITY-RELEASE", - "SECURITY-REGRESSION", - "SECURITY-QUALITY", - "SECURITY-FEATURES", - "SECURITY-SCRIPTS", - "SECURITY-PLATFORMS", -} -PROFILES = ("chrome", "chrome120", "firefox", "safari") -ENCRYPTION = tuple( - f"{style}-{rtt}-mixed" - for style in ("native", "xorpub", "random") - for rtt in ("1rtt", "0rtt") -) - - -def groups(): - from .protocol_ech import catalog as ech_catalog - from .protocol_jls import catalog as jls_catalog - - result = {} - - def add(name, runner, rows, **options): - result[name] = dict(runner=runner, rows=rows, **options) - - add( - "SECURITY-ECH-STANDARD", - "vless", - ECH_FIELDS, - selected=list(ech_catalog()), - ech=True, - ) - named = [ - "SECURITY-ECH-TCP-TLS-BASE", - "SECURITY-ECH-TCP-TLS-REJECT", - "SECURITY-ECH-WS-TLS-BASE", - "SECURITY-ECH-GRPC-TLS-BASE", - "SECURITY-ECH-GRPC-TLS-CLOSE", - "SECURITY-ECH-TCP-MTLS-IDENTITY", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-BASE", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-REJECT", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-DOWNLOAD", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-H1-BASE", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-H1-DOWNLOAD", - ] - for profile in PROFILES: - add( - "SECURITY-ECH-" + profile.upper(), - "vless", - ECH_FIELDS, - selected=named, - ech=True, - client_fingerprint=profile, - ) - - add( - "SECURITY-JLS-RETAINED", - "vless", - JLS_FIELDS, - jls=True, - selected=[ - key - for key, (_, mode, _, test) in jls_catalog().items() - if mode - in { - "tcp-tls", - "grpc-tls", - "xhttp-stream-up-download", - "xhttp-stream-up-download-h1", - } - and test - in { - "public_base", - "native_jls_fail_closed", - "public_jls_download_identity", - "runtime::owned_resources", - "runtime::public_lifecycle", - "native_mihomo_close_alignment", - } - ], - ) - add( - "SECURITY-HYBRID-RETAINED", - "hybrid", - HYBRID_FIELDS, - selected=[ - f"{profile}-{mode}-{test}" - for profile in ("none", "chrome") - for mode in ("tcp", "h1-stream-up", "h2-stream-up") - for test in ("base", "security") - ] - + [ - "chrome-vision-inner-tls", - "chrome-h2-stream-up-owned", - "chrome-h2-stream-up-life", - "chrome-h1-main-classic", - "chrome-h2-download-classic", - ], - ) - - for profile in ENCRYPTION: - suffix = profile.upper() - add( - "SECURITY-ECH-ENCRYPTION-" + suffix, - "vless", - ["VL06", *ECH_FIELDS], - ech=True, - encryption=profile, - client_fingerprint="chrome", - selected=[ - "SECURITY-ECH-TCP-TLS-BASE", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-BASE", - "SECURITY-ECH-XHTTP-STREAM-UP-DOWNLOAD-H1-BASE", - ], - ) - add( - "SECURITY-JLS-ENCRYPTION-" + suffix, - "vless", - ["VL06", *JLS_FIELDS], - jls=True, - encryption=profile, - selected=[ - "SECURITY-JLS-TCP-BASE", - "SECURITY-JLS-XHTTP-STREAM-UP-DOWNLOAD-BASE", - ], - ) - add( - "SECURITY-HYBRID-ENCRYPTION-" + suffix, - "hybrid", - ["VL06", *HYBRID_FIELDS], - encryption=profile, - selected=[ - "chrome-tcp-base", - "chrome-vision-base", - "chrome-h2-stream-up-base", - ], - ) - - def fields(name, rows, specs, **options): - jobs = {} - for variant, tests in specs: - jobs[variant] = [ - dict(case_id=variant + "-" + str(i), test=test) - for i, test in enumerate(tests) - ] - add(name, "xhttp", rows, jobs=jobs, **options) - - specs = [ - (f"h3-{mode}-headers", ["public_base"]) - for mode in ("stream-one", "stream-up", "packet-up") - ] - specs += [ - ( - f"h3-ech-{action}", - ["security::native_xhttp_security"] - if action.startswith("reject") - else ["public_base"], - ) - for action in ("reject-main", "reject-download", "replace", "clear") - ] - specs += [ - ( - "h3-stream-up-reuse", - [ - "runtime::public_graph", - "runtime::public_entrypoints", - "runtime::public_ipv6_and_gates", - "runtime::public_udp_isolation", - "runtime::public_lifecycle", - "lifecycle::native_xhttp_owned_resources", - ], - ), - ("h3-keepalive", ["keepalive::native_h3_keepalive_observes_each_leg_and_stop"]), - ] - fields("SECURITY-ECH-H3", ECH_FIELDS, specs, ech=True) - for security in ("ech", "jls"): - versions = ("h1", "h2", "h3") if security == "ech" else ("h1", "h2") - specs = [ - (f"{version}-mux-{mux}-padded{only}", ["public_base"]) - for version in versions - for mux in ("h2mux", "smux", "yamux") - for only in ("", "-only-tcp") - ] - fields( - "SECURITY-" + security.upper() + "-MUX", - ECH_FIELDS if security == "ech" else JLS_FIELDS, - specs, - **{security: True}, - encryption="native-0rtt-mixed", - ) - fields( - "SECURITY-ECH-NATIVE-TRANSPORT", - ECH_FIELDS, - [ - ("outer-" + transport + "-tls", ["public_base"]) - for transport in ("http", "h2", "ws-header", "ws-path") - ], - ech=True, - encryption="native-0rtt-mixed", - ) - - fields( - "SECURITY-ECH-H3-ENCRYPTION", - ["VL06", *ECH_FIELDS], - [ - (f"h3-{mode}-headers", ["public_base"]) - for mode in ("stream-one", "stream-up", "packet-up") - ] - + [("h3-ech-replace", ["public_base"])], - ech=True, - encryption="native-0rtt-mixed", - ) - - add("SECURITY-ENCRYPTION-WIRE", "encryption", ["VL06"]) - add("SECURITY-ENCRYPTION-CHACHA", "encryption", ["VL06"], chacha=True) - add("SECURITY-ENCRYPTION-EXPIRY", "encryption", ["VL06"], expiry=True) - for profile in ("none", "chrome"): - add( - "SECURITY-SHARED-" + profile.upper(), - "vless", - [], - client_fingerprint=profile, - selected=[ - "VLESS-TCP-TLS-BASE", - "VLESS-TCP-TLS-NEG", - "VLESS-TCP-REALITY-BASE", - "VLESS-TCP-REALITY-NEG", - "VLESS-VISION-REALITY-BASE", - "VLESS-GRPC-TLS-BASE", - "VLESS-HTTP-TLS-BASE", - "VLESS-H2-TLS-BASE", - "VLESS-REGRESSION-TROJAN-TCP", - "VLESS-REGRESSION-VMESS-GRPC", - "FINGERPRINT-ANYTLS", - ], - ) - fields( - "SECURITY-SHARED-XHTTP", - [], - [ - ("h1-stream-up-split-reuse", ["public_base"]), - ("h2-stream-up-split-reuse", ["public_base"]), - ("h3-stream-up-split-reuse", ["public_base"]), - ("h2r-stream-up-split-reuse", ["public_base"]), - ("h3-mux-yamux-padded", ["public_base"]), - ], - ) - return result - - -def definitions(): - native = groups() - result = [] - for identifier in sorted(GATES | native.keys()): - group = native.get(identifier, {}) - runner = group.get("runner", "gate") - rows = sorted( - FIELDS - if identifier in {"SECURITY-CFG", "SECURITY-RELEASE"} - else group.get("rows", []) - ) - options = {k: v for k, v in group.items() if k not in {"rows", "runner"}} - kind = ( - "unit" - if not group - else ( - "XR" if "H3" in identifier or "NATIVE-TRANSPORT" in identifier else "M" - ) - ) - result.append( - dict( - case_id=identifier, - stage="SECURITY", - substage="SECURITY.acceptance", - required=True, - row_ids=rows, - protocol="vless", - network="explicit-selected-compositions", - security="static-ech/encryption/hybrid-reality/jls", - udp_codec="raw/xudp/packetaddr/sing-mux", - field_values=options, - outer_family="IPv4/IPv6", - inner_family="IPv4/IPv6/domain", - target_type=( - "memory-only/build" - if kind == "unit" - else "isolated synthetic origin" - ), - upstream_graph="direct/concrete/nested-select", - peer_kind=kind, - peer_config=dict(runner=runner, **options), - gap_source=dict( - reason="Mihomo v1.19.31 Safari ECH fails before business data. " - "Only the gRPC close reference uses Chrome with the same ECH " - "listener; VCore remains Safari, not a same-profile comparison.", - source="https://github.com/MetaCubeX/utls/blob/v1.8.7/u_handshake_client.go", - ) - if identifier == "SECURITY-ECH-SAFARI" - else None - if kind != "XR" - else dict( - reason="Mihomo has no H3 or legacy HTTP/H2/extended WS listener; " - "official Xray/V2Ray terminate that transport, with " - "Mihomo VLESS for packetaddr, Encryption and sing-mux.", - source="https://github.com/XTLS/Xray-core", - ), - expected_observation=[identifier], - required_evidence=["command", "cleanup", "structured-assertions"] - + ([] if kind == "unit" else ["peer-identity", "isolated-origins"]), - prerequisites=["rust-toolchain"] - if kind == "unit" - else ["owned-host-only-container-network", "official-latest-peer"], - runner="vless-security-" + runner, - timeout_seconds=3600, - ) - ) - return result diff --git a/scripts/src/vcore_scripts/protocol_security_units.py b/scripts/src/vcore_scripts/protocol_security_units.py deleted file mode 100644 index b49398c..0000000 --- a/scripts/src/vcore_scripts/protocol_security_units.py +++ /dev/null @@ -1,42 +0,0 @@ -# Exact configuration and in-memory TLS assertion identities for SECURITY. -TESTS = { - "ech_config": [ - "static_ech_builds_both_tls_backends_without_dns_or_io", - "static_ech_is_strict_and_never_enables_dynamic_lookup", - "unsupported_ech_configs_fail_at_parse_before_a_client_can_emit_inner_sni", - "ech_selection_skips_unknown_or_mandatory_configs_and_keeps_exact_supported_bytes", - "download_ech_inherits_replaces_or_clears_as_a_whole", - "ech_is_standard_tls_only_with_explicit_download_clear_and_dns_inner_name", - ], - "ech_tls": [ - "rejected_or_wrong_key_ech_never_reaches_application_data_even_with_a_matching_pin", - "client_identity_is_sent_only_after_ech_acceptance", - "cancelled_ech_emits_only_public_sni_and_releases_io_for_every_backend", - "each_backend_and_hpke_suite_requires_real_ech_acceptance_before_data", - ], - "encryption_config": [ - "public_encryption_accepts_all_six_modes_without_exposing_key_material", - "vision_encryption_is_independent_of_outer_tls_but_keeps_transport_and_udp_limits", - ], - "reality_config": [ - "public_reality_accepts_explicit_hybrid_without_replacing_the_fingerprint", - "public_reality_rejects_profiles_without_the_required_hybrid_share", - "download_inherits_or_replaces_the_entire_reality_object_without_leaf_merging", - "hybrid_flag_is_a_strict_nonnullable_boolean_on_both_legs", - "classic_default_and_explicit_false_preserve_all_existing_profiles", - "inherited_profile_is_validated_after_download_reality_replacement", - "hybrid_reality_cannot_be_combined_with_h3_or_plaintext_on_either_leg", - ], - "jls_config": [ - "public_jls_builds_an_authenticated_security_client_without_exposing_credentials", - "independent_download_inherits_jls_identity_and_applies_its_own_tls_fields", - "download_jls_credentials_are_replaced_as_a_whole_or_explicitly_cleared", - "malformed_or_partial_credentials_never_inherit_a_missing_secret", - "invalid_jls_objects_do_not_echo_credentials_in_public_configuration_errors", - "jls_rejects_incompatible_security_at_configuration_time", - "download_security_switch_requires_explicit_clearing_of_inherited_identity", - "selected_profiles_build_jls_without_changing_its_identity", - "cancelled_jls_handshakes_release_supplied_io_and_never_reuse_randoms", - "jls_security_client_rejects_ordinary_tls_before_application_data", - ], -} diff --git a/scripts/src/vcore_scripts/protocol_shadowtls.py b/scripts/src/vcore_scripts/protocol_shadowtls.py deleted file mode 100644 index b20bf90..0000000 --- a/scripts/src/vcore_scripts/protocol_shadowtls.py +++ /dev/null @@ -1,568 +0,0 @@ -"""SS2022 ShadowTLS v3 through public consumers and isolated official peers.""" - -from __future__ import annotations - -import contextlib -import copy -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_completion_peers import CASES, peer_configuration -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_fixtures import certificates -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command - -PROFILES = ("none", "chrome120", "chrome", "firefox", "safari") -POLICIES = ( - "alpn-default", - "alpn-empty", - "alpn-custom", - "hrr", - "wrong-password", - "wrong-pin", - "wrong-pin-with-skip", - "untrusted", - "tls12", -) - - -def policy_inputs(node, listener): - for index, policy in enumerate(POLICIES): - client, peer = copy.deepcopy(node), copy.deepcopy(listener) - client["port"] = peer["port"] = 23500 + index - peer["name"] += "-" + policy - port, alpn = 24001, "h2" - if policy == "alpn-empty": - client["plugin-opts"]["alpn"] = [] - alpn = None - elif policy == "alpn-custom": - client["plugin-opts"]["alpn"] = ["fixture-custom"] - alpn = "fixture-custom" - elif policy == "hrr": - port = 24002 - elif policy == "wrong-password": - client["plugin-opts"]["password"] = "wrong-synthetic-password" - elif policy.startswith("wrong-pin"): - client["plugin-opts"]["fingerprint"] = "07" * 32 - client["plugin-opts"]["skip-cert-verify"] = policy.endswith("skip") - elif policy == "untrusted": - del client["plugin-opts"]["fingerprint"] - elif policy == "tls12": - port = 24003 - host = peer["shadow-tls"]["handshake"]["dest"].rsplit(":", 1)[0] - peer["shadow-tls"]["handshake"]["dest"] = f"{host}:{port}" - yield ( - policy, - client, - peer, - policy.startswith("alpn-") or policy == "hrr", - port, - alpn, - ) - - -def run( - output: Path, - *, - profiles=("none",), - selected=None, - checks="data", - supplied=None, - image=None, -): - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("ShadowTLS evidence must stay inside target/interop/runs") - cases = tuple(c for c in CASES if c.shadow_tls and not c.uot) - if ( - checks not in {"data", "policy", "fault"} - or not profiles - or len(set(profiles)) != len(profiles) - or not set(profiles) <= set(PROFILES) - ): - raise ValueError("invalid ShadowTLS checks or profiles") - if selected is not None: - if len(set(selected)) != len(selected) or not set(selected) <= { - c.identifier for c in cases - }: - raise ValueError("unknown or duplicate ShadowTLS case") - cases = tuple(c for c in cases if c.identifier in selected) - if checks != "data": - cases = cases[:1] - if not cases: - raise ValueError("empty ShadowTLS selection") - report = dict( - source=source_identity(), status="NOT RUN", peer={}, isolation={}, cases=[] - ) - output.mkdir(parents=True, exist_ok=False) - try: - with ( - exclusive_run() if supplied is None else contextlib.nullcontext(), - ( - frozen_image(output / "image-pull.log") - if image is None - else contextlib.nullcontext(image) - ) as image, - ): - report["container_image"] = image - if supplied is None: - binary = download_mihomo( - "linux-arm64", directory=output / "binary", identity=report["peer"] - ) - else: - binary, report["peer"] = supplied - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "--no-run", - ], - cwd=CORE_DIR, - timeout=180, - ) - (output / "build.log").write_text( - redact(built.stdout.decode(errors="replace")) - ) - if built.returncode or not built.cleanup: - raise RuntimeError("ShadowTLS consumer build failed") - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temp: - root = Path(temp) - directories = {} - try: - with contextlib.ExitStack() as stack: - for role in ("origin", "server"): - directory = root / role - directory.mkdir() - directories[role] = directory - cert = certificates(directories["server"]) - for source, name in zip( - cert[:2], ("cert.pem", "key.pem"), strict=True - ): - shutil.copy2(source, directories["origin"] / name) - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - directories["origin"] / "origin.py", - ) - shutil.copy2( - Path(__file__).with_name("container_shadowtls_peer.py"), - directories["origin"] / "peer.py", - ) - origin = lab.start( - stack, - directories["origin"], - "origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "VCORE_ORIGIN_CERT=/data/fixture/cert.pem", - "VCORE_ORIGIN_KEY=/data/fixture/key.pem", - "python", - "-B", - "/data/fixture/peer.py", - ], - ) - shutil.copy2(binary, directories["server"] / "mihomo") - server = lab.start( - stack, - directories["server"], - "server", - [ - "/data/fixture/mihomo", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - report["peer"]["version"] = command( - "exec", server.name, "/data/fixture/mihomo", "-v" - ).strip() - if ( - command( - "exec", server.name, "sha256sum", "/data/fixture/mihomo" - ).split()[0] - != report["peer"]["binary_sha256"] - ): - raise RuntimeError("ShadowTLS peer identity mismatch") - nodes, listeners = [], [] - for index, case in enumerate(cases): - node, listener = peer_configuration( - case, - server=server.ipv4, - cover=f"{origin.ipv4}:24001", - port=23000 + index, - certificate=cert, - ) - node["name"] = "peer" - if checks == "data": - nodes.append((case.identifier, node, True, 24001, "h2")) - listeners.append(listener) - elif checks == "policy": - for ( - label, - client, - peer, - success, - port, - alpn, - ) in policy_inputs(node, listener): - nodes.append((label, client, success, port, alpn)) - listeners.append(peer) - else: - listeners.append(listener) - hrr = copy.deepcopy(listener) - hrr["name"] += "-hrr" - hrr["port"] = 23001 - hrr["shadow-tls"]["handshake"]["dest"] = ( - f"{origin.ipv4}:24002" - ) - listeners.append(hrr) - directory = root / "fault" - directory.mkdir() - directories["fault"] = directory - shutil.copy2( - Path(__file__).with_name( - "container_shadowtls_fault.py" - ), - directory / "fault.py", - ) - fault = lab.start( - stack, - directory, - "fault", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/fault.py", - ], - ) - routes = [] - for index, label in enumerate( - ( - "fragment", - "hrr-wire", - "cover-mac", - "business-mac", - "business-truncated", - "stall", - "native-udp-disabled", - ) - ): - port = 23600 + index - routes.append( - dict( - port=port, - mode="fragment" - if label == "hrr-wire" - else label, - server=server.ipv4, - server_port=23001 - if label == "hrr-wire" - else 23000, - ) - ) - client = copy.deepcopy(node) - client.update(server=fault.ipv4, port=port) - nodes.append( - ( - label, - client, - label not in {"cover-mac", "stall"}, - 24002 if label == "hrr-wire" else 24001, - "h2", - ) - ) - (directory / "config.json").write_text( - json.dumps(routes) - ) - (directories["server"] / "config.json").write_text( - json.dumps( - { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "listeners": listeners, - "rules": ["MATCH,DIRECT"], - } - ) - ) - origin.release() - server.release() - origin.wait_tcp(24000) - origin.wait_tcp(24001) - origin.wait_tcp(24002) - origin.wait_tcp(24003) - server.wait_tcp(23999) - if checks == "fault": - fault.release() - fault.wait_tcp(23998) - for route in routes: - fault.wait_tcp(route["port"]) - for label, node, success, cover_port, alpn in nodes: - for profile in profiles: - identifier = f"{label}-{profile}" - before = len( - command( - "exec", - origin.name, - "cat", - "/data/cover-events.jsonl", - ).splitlines() - ) - fault_before = ( - len( - command( - "exec", - fault.name, - "cat", - "/data/fault-events.jsonl", - ).splitlines() - ) - if checks == "fault" - else 0 - ) - consumer = ( - checks - if checks != "fault" - else { - "fragment": "native_tcp", - "hrr-wire": "native_tcp", - "cover-mac": "policy", - "stall": "stall", - "native-udp-disabled": "native_udp_disabled", - }.get(label, "corrupt") - ) - node["client-fingerprint"] = profile - fixture = root / "fixture.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - node=node, - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - origin_control=f"{origin.ipv4}:24000", - data_dir=str(root / "data"), - expected_success=success, - fault_control=f"{fault.ipv4}:23998" - if checks == "fault" - else None, - ) - ) - ) - observations = ( - output / f"{identifier}-observations.json" - ) - argv = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - f"shadowtls::{consumer}", - "--", - "--ignored", - "--exact", - "--nocapture", - ] - result = run_command( - argv, - cwd=CORE_DIR, - timeout=240, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_SHADOWTLS_OBSERVATIONS=str(observations), - VCORE_CASE_EVENTS=str( - output / f"{identifier}-events.jsonl" - ), - ), - ) - (output / f"{identifier}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = ( - json.loads(observations.read_text()) - if observations.is_file() - else None - ) - expected = ( - dict( - tcp_bytes_each_direction=10 * 1024 * 1024, - udp_packets=500 if consumer == "data" else 0, - udp_sizes=[1, 64, 512, 1200, 4096] - if consumer == "data" - else [], - stop_idle=True, - ) - if consumer in {"data", "native_tcp"} - else dict( - origin_connected=True, - delivered_bytes=0, - stop_idle=True, - ) - if consumer == "corrupt" - else dict( - stop_idle=True, - stop_handshake_cancelled=True, - measure_deadline=True, - origin_connected=False, - closed_handshakes=2, - ) - if consumer == "stall" - else dict( - tcp_accepted=True, - udp_delivered=False, - stop_idle=True, - ) - if consumer == "native_udp_disabled" - else dict( - accepted=success, - origin_connected=success, - stop_idle=True, - ) - ) - cover = [ - json.loads(line) - for line in command( - "exec", - origin.name, - "cat", - "/data/cover-events.jsonl", - ).splitlines()[before:] - ] - cover_pass = ( - not success - or dict( - port=cover_port, version="TLSv1.3", alpn=alpn - ) - in cover - ) - fault_events = ( - [ - json.loads(line) - for line in command( - "exec", - fault.name, - "cat", - "/data/fault-events.jsonl", - ).splitlines()[fault_before:] - ] - if checks == "fault" - else [] - ) - fault_pass = True - if checks == "fault": - required = ( - "split_record" - if label == "fragment" - else "hrr_seen" - if label == "hrr-wire" - else "injected" - ) - fault_pass = any( - event.get(required) is True - for event in fault_events - ) and not any( - event.get("upload_join_failed") - for event in fault_events - ) - if label == "stall": - fault_pass = not cover and all( - sum( - event.get(key) is True - for event in fault_events - ) - == 2 - for key in ("hello_seen", "client_closed") - ) - elif label == "native-udp-disabled": - fault_pass = not any( - event.get("upload_join_failed") - for event in fault_events - ) - passed = ( - result.returncode == 0 - and result.cleanup - and observed == expected - and cover_pass - and fault_pass - ) - report["cases"].append( - dict( - case_id=identifier, - status="PASS" if passed else "FAIL", - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - observations=observed, - cover=cover, - fault=fault_events, - ) - ) - print( - f"{identifier}: {'PASS' if passed else 'FAIL'}", - flush=True, - ) - if not passed: - raise RuntimeError( - "ShadowTLS public data validation failed" - ) - finally: - for role, directory in directories.items(): - if (directory / "peer.log").is_file(): - (output / f"{role}.log").write_text( - redact( - (directory / "peer.log").read_text(errors="replace") - ) - ) - report["status"] = "PASS" - except BaseException as error: - report["status"] = "FAIL" - (output / "failure.log").write_text(redact(str(error))) - raise - finally: - report["cleanup"] = all( - p.get("joined") is True for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - if report["status"] != "PASS": - raise RuntimeError("ShadowTLS source or cleanup validation failed") - return report - - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("--profile", action="append") - parser.add_argument("--case", action="append") - parser.add_argument("--checks", choices=("data", "policy", "fault"), default="data") - args = parser.parse_args() - run( - args.output, - profiles=args.profile or ["none"], - selected=args.case, - checks=args.checks, - ) diff --git a/scripts/src/vcore_scripts/protocol_shadowtls_acceptance.py b/scripts/src/vcore_scripts/protocol_shadowtls_acceptance.py deleted file mode 100644 index 6b72720..0000000 --- a/scripts/src/vcore_scripts/protocol_shadowtls_acceptance.py +++ /dev/null @@ -1,376 +0,0 @@ -"""ShadowTLS execution and reconstruction from raw, source-bound evidence.""" - -from __future__ import annotations - -import json - -from .core_checks import validate_execution -from .protocol_evidence import read_events, read_json -from .protocol_hysteria2_catalog import assertions_pass -from .protocol_inputs import redact -from .protocol_integration_checks import envelope, runtime_pass -from .protocol_security_acceptance import REPORTS, result, rust_command, vless_pass -from .protocol_shadowtls import POLICIES, PROFILES -from .protocol_shadowtls_catalog import ( - FAULTS, - LOCAL, - MEMORY_EVENTS, - SHARED, - SS_CASES, - commands, -) - - -def expected_observation(consumer, success): - if consumer in {"data", "native_tcp"}: - return dict( - tcp_bytes_each_direction=10 * 1024 * 1024, - udp_packets=500 if consumer == "data" else 0, - udp_sizes=[1, 64, 512, 1200, 4096] if consumer == "data" else [], - stop_idle=True, - ) - if consumer == "policy": - return dict(accepted=success, origin_connected=success, stop_idle=True) - if consumer == "corrupt": - return dict(origin_connected=True, delivered_bytes=0, stop_idle=True) - if consumer == "stall": - return dict( - stop_idle=True, - stop_handshake_cancelled=True, - measure_deadline=True, - origin_connected=False, - closed_handshakes=2, - ) - if consumer == "native_udp_disabled": - return dict(tcp_accepted=True, udp_delivered=False, stop_idle=True) - raise ValueError("unknown ShadowTLS consumer") - - -def fault_pass(label, events, cover): - if any(e.get("upload_join_failed") for e in events): - return False - if label == "stall": - return not cover and all( - sum(e.get(key) is True for e in events) == 2 - for key in ("hello_seen", "client_closed") - ) - if label == "native-udp-disabled": - return True - required = ( - "split_record" - if label == "fragment" - else "hrr_seen" - if label == "hrr-wire" - else "injected" - ) - return any(e.get(required) is True for e in events) - - -def wire_pass(name, directory, report): - if name == "MIHOMO-DATA": - labels = [ - (f"{c}-{p}", "data", True, 24001, "h2", None) - for c in SS_CASES - for p in PROFILES - ] - elif name == "MIHOMO-POLICY": - labels = [ - ( - f"{p}-chrome", - "policy", - p.startswith("alpn-") or p == "hrr", - 24002 if p == "hrr" else 24003 if p == "tls12" else 24001, - None - if p == "alpn-empty" - else "fixture-custom" - if p == "alpn-custom" - else "h2", - None, - ) - for p in POLICIES - ] - elif name == "MIHOMO-FAULT": - consumers = { - "fragment": "native_tcp", - "hrr-wire": "native_tcp", - "cover-mac": "policy", - "stall": "stall", - "native-udp-disabled": "native_udp_disabled", - } - labels = [ - ( - f"{f}-chrome", - consumers.get(f, "corrupt"), - f not in {"cover-mac", "stall"}, - 24002 if f == "hrr-wire" else 24001, - "h2", - f, - ) - for f in FAULTS - ] - elif name == "NATIVE": - labels = [(c, "native_tcp", True, None, None, None) for c in SS_CASES] - else: - raise ValueError("unknown wire group") - rows = report.get("cases", []) - if [r.get("case_id") for r in rows] != [item[0] for item in labels]: - return False - for row, (identifier, consumer, success, port, alpn, fault) in zip( - rows, labels, strict=True - ): - observed = read_json(directory / (identifier + "-observations.json")) - events = read_events(directory / (identifier + "-events.jsonl")) - if not ( - row.get("command") == rust_command("vless_public", "shadowtls::" + consumer) - and type(row.get("exit_code")) is int - and row["exit_code"] == 0 - and row.get("command_cleanup") is True - and observed - == row.get("observations") - == expected_observation(consumer, success) - and assertions_pass(events, {("SHADOWTLS", consumer): 1}) - ): - return False - cover = row.get("cover", []) - if ( - port is not None - and success - and dict(port=port, version="TLSv1.3", alpn=alpn) not in cover - ): - return False - if fault is not None and not fault_pass(fault, row.get("fault", []), cover): - return False - return True - - -def local_pass(name, directory, run): - expected = commands(name) - records = [ - r for r in run["commands"] if r["name"].startswith("SHADOWTLS-" + name + "-") - ] - if len(records) != len(expected): - return False - events = [] - for i, (row, argv) in enumerate(zip(records, expected, strict=True)): - label = f"SHADOWTLS-{name}-{i}" - if row["name"] != label or row["command"] != [ - redact(v.replace("{output}", str(directory))) for v in argv - ]: - return False - validate_execution( - row["command"], - (directory / row["log"]).read_text(), - row["exit_code"], - row["cleanup"], - ) - path = directory / (label + "-events.jsonl") - if path.exists(): - events += read_events(path) - if name == "MEMORY": - return assertions_pass( - [e for e in events if e.get("suite", "").startswith("SHADOWTLS-")], - MEMORY_EVENTS, - ) - if name == "FEATURES": - return assertions_pass( - [e for e in events if e.get("suite") == "SHADOWTLS-CFG"], - {("SHADOWTLS-CFG", "feature"): 1}, - ) - if name == "FINGERPRINT": - from .protocol_fingerprint_shape import check_captures, check_warm_captures - - root = directory / "fingerprint" - report = read_json(root / "shape-results.json") - return ( - report.get("source") == source(run) - and report.get("source_unchanged") is True - and report.get("cleanup") is True - and report.get("cases") == check_captures(read_json(root / "captures.json")) - and report.get("warm_cases") - == check_warm_captures(read_json(root / "warm.json")) - ) - return True - - -def source(run): - return { - k: run[k] - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - - -def group_pass(name, directory, run, peers): - if name in LOCAL: - return local_pass(name, directory, run) - root = directory / name.lower() - digest = run["container_image"]["digest"] - if name == "SHARED": - return all( - envelope( - report := read_json(root / label / REPORTS["vless"]), - peers, - source(run), - digest, - ) - and vless_pass(group, report, root / label) - for label, group in SHARED.items() - ) - if name == "BARE-SS": - report = read_json(root / "integration-suite.json") - rows = report.get("cases", []) - return ( - envelope(report, peers, source(run), digest) - and len(rows) == 1 - and runtime_pass("INTEGRATION-SS-ALGORITHMS", rows[0], root) - ) - report = read_json(root / "report.json") - identities = report.get("peers") if name == "NATIVE" else {"M": report.get("peer")} - return ( - set(identities) == ({"SS", "ST"} if name == "NATIVE" else {"M"}) - and envelope( - report, peers, source(run), digest, single=None if name == "NATIVE" else "M" - ) - and wire_pass(name, root, report) - ) - - -def prepare(output, *, native): - from .native_release import download_native - from .protocol_hysteria2_acceptance import prepare as prepare_mihomo - - supplied = prepare_mihomo(output, {"M"}) - artifacts = ( - { - kind: download_native( - kind, output / "binaries" / kind, "linux-arm64", defer_version=True - ) - for kind in ("ST", "SS") - } - if native - else {} - ) - return supplied, artifacts - - -def execute(cases, run, output, results): - from .protocol_containers import frozen_image - from .protocol_harness import _command - from .protocol_integration_suite import run as integration - from .protocol_shadowtls import run as mihomo - from .protocol_shadowtls_native import run as native - from .protocol_vless_container import run as vless - - selected = {c["case_id"].removeprefix("SHADOWTLS-"): c for c in cases} - peers = {} - - def save(name): - good = group_pass(name, output, run, peers) - case = selected[name] - results[case["case_id"]] = result(case, good, good) - print(case["case_id"] + (": PASS" if good else ": FAIL"), flush=True) - if not good: - raise RuntimeError("ShadowTLS gate failed: " + name) - - try: - for name in selected: - if name not in LOCAL: - continue - for i, argv in enumerate(commands(name)): - label = f"SHADOWTLS-{name}-{i}" - command = _command( - run, - output, - label, - [v.replace("{output}", str(output)) for v in argv], - 3600, - events=output / (label + "-events.jsonl"), - ) - validate_execution( - argv, - command.stdout.decode(errors="replace"), - command.returncode, - command.cleanup, - ) - save(name) - if set(selected) <= LOCAL: - return - with frozen_image(output / "image-pull.log") as image: - run["container_image"] = image - supplied, artifacts = prepare(output, native="NATIVE" in selected) - peers.update({k: v[1] for k, v in supplied.items()}) - peers.update({k: v.identity for k, v in artifacts.items()}) - for name in selected: - if name in LOCAL: - continue - root = output / name.lower() - if name.startswith("MIHOMO-"): - mihomo( - root, - profiles=PROFILES if name == "MIHOMO-DATA" else ("chrome",), - checks=name.removeprefix("MIHOMO-").lower(), - supplied=supplied["M"], - image=image, - ) - elif name == "NATIVE": - native(root, supplied=artifacts, image=image) - elif name == "SHARED": - for label, group in SHARED.items(): - vless(root / label, supplied=supplied, **group) - elif name == "BARE-SS": - integration( - root, ["INTEGRATION-SS-ALGORITHMS"], supplied=supplied["M"] - ) - else: - raise ValueError("unknown ShadowTLS gate") - save(name) - finally: - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - - -def check(directory, cases, run, results, peers, paths): - if not {"cases.json", "peers.json", "resources.jsonl", "summary.md"} <= set(paths): - raise ValueError("missing ShadowTLS evidence files") - expected_commands = [ - f"SHADOWTLS-{c['case_id'].removeprefix('SHADOWTLS-')}-{i}" - for c in cases - for i, _ in enumerate(commands(c["case_id"].removeprefix("SHADOWTLS-"))) - ] - actual = [r["name"] for r in run["commands"]] - if ( - len(actual) != len(set(actual)) - or set(actual) != set(expected_commands) - or set(peers) != {"M", "SS", "ST"} - ): - raise ValueError("ShadowTLS command or peer selection differs") - for case in cases: - name = case["case_id"].removeprefix("SHADOWTLS-") - if not group_pass(name, directory, run, peers): - raise ValueError("ShadowTLS raw evidence failed: " + name) - print("SHADOWTLS coverage: PASS (all required groups; device delivery separate)") - - -def preflight(output): - from .protocol_containers import command, frozen_image - - command("system", "status") - with frozen_image(output / "image-pull.log"): - supplied, artifacts = prepare(output, native=True) - (output / "peers.json").write_text( - json.dumps( - dict( - scope="download-only", - wire_acceptance="NOT RUN", - peers={ - **{k: v[1] for k, v in supplied.items()}, - **{k: v.identity for k, v in artifacts.items()}, - }, - ), - indent=2, - ) - + "\n" - ) diff --git a/scripts/src/vcore_scripts/protocol_shadowtls_catalog.py b/scripts/src/vcore_scripts/protocol_shadowtls_catalog.py deleted file mode 100644 index da9701a..0000000 --- a/scripts/src/vcore_scripts/protocol_shadowtls_catalog.py +++ /dev/null @@ -1,179 +0,0 @@ -"""Frozen ShadowTLS v3 acceptance groups, not a duplicate field matrix.""" - -from __future__ import annotations - -import sys - -from .protocol_completion_peers import CASES - -SS_CASES = tuple(c.identifier for c in CASES if c.shadow_tls and not c.uot) -FAULTS = ( - "fragment", - "hrr-wire", - "cover-mac", - "business-mac", - "business-truncated", - "stall", - "native-udp-disabled", -) -GATES = { - "MEMORY": "stream", - "FEATURES": "configuration", - "QUALITY": "regression", - "PLATFORMS": "regression", - "FINGERPRINT": "regression", - "MIHOMO-DATA": "interop", - "MIHOMO-POLICY": "interop", - "MIHOMO-FAULT": "interop", - "NATIVE": "interop", - "SHARED": "regression", - "BARE-SS": "regression", -} -LOCAL = {"MEMORY", "FEATURES", "QUALITY", "PLATFORMS", "FINGERPRINT"} -SHARED = { - **{ - p: dict( - client_fingerprint=p, - selected=[ - "VLESS-TCP-TLS-BASE", - "VLESS-TCP-TLS-NEG", - "VLESS-TCP-REALITY-BASE", - "VLESS-TCP-REALITY-NEG", - "FINGERPRINT-ANYTLS", - ], - ) - for p in ("none", "chrome") - }, - "jls": dict( - jls=True, - client_fingerprint="chrome", - selected=["SECURITY-JLS-TCP-BASE", "SECURITY-JLS-TCP-AUTH"], - ), -} -MEMORY_EVENTS = { - **{("SHADOWTLS-CFG", n): 1 for n in ("policy", "reject", "feature")}, - **{("SHADOWTLS-STREAM", n): 1 for n in ("cancelled_hello", "invalid_record")}, - **{ - ("SHADOWTLS-UNIT", n): 1 - for n in ( - "native_signature", - "native_finished", - "stream_io", - "read_cancel", - "close_deadline", - ) - }, -} - - -def definitions(): - return [ - dict( - case_id="SHADOWTLS-" + name, - stage="SHADOWTLS", - substage="SHADOWTLS." + category, - required=True, - row_ids=[], - protocol="ss", - network="tcp/native-udp", - security="shadowtls-v3", - udp_codec="ss2022", - field_values={}, - outer_family="IPv4", - inner_family="IPv4/IPv6/domain", - target_type="memory-only/build" if name in LOCAL else "isolated origin", - upstream_graph="existing-controlled-connector", - peer_kind="unit" if name in LOCAL else "SS" if name == "NATIVE" else "M", - peer_config={"group": name}, - gap_source=None, - expected_observation=["SHADOWTLS-" + name], - required_evidence=["command", "structured-assertions", "cleanup"] - + ([] if name in LOCAL else ["peer-identity", "isolated-origins"]), - prerequisites=["rust-toolchain"] - if name in LOCAL - else ["owned-host-only-container-network", "official-latest-peer"], - runner="shadowtls-gate", - timeout_seconds=3600, - ) - for name, category in GATES.items() - ] - - -def commands(name): - cargo = ["cargo", "test", "--locked", "--all-features"] - if name == "MEMORY": - return [ - cargo - + [ - arg - for target in ( - "shadowtls_config", - "shadowtls_stream", - "shadowsocks_backpressure", - "jls_config", - "reality_config", - ) - for arg in ("--test", target) - ], - cargo + ["--lib", "security::shadow_tls_tests::"], - cargo + ["--lib", "security::tls::tests::"], - ] - if name == "FEATURES": - return [ - [ - "cargo", - "check", - "--locked", - "--no-default-features", - "--features", - features, - ] - for features in ( - "outbound-shadowsocks", - "shadow-tls-v3", - "outbound-shadowsocks,shadow-tls-v3", - ) - ] + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "outbound-shadowsocks,interop-test", - "--test", - "shadowtls_config", - ], - ["cargo", "test", "--locked", "--test", "feature_foundations"], - cargo + ["--all-targets", "--no-run"], - ] - if name == "QUALITY": - return [ - ["cargo", "fmt", "--all", "--", "--check"], - [ - "cargo", - "clippy", - "--locked", - "--all-features", - "--all-targets", - "--", - "-D", - "warnings", - ], - [sys.executable, "-m", "unittest", "discover", "-s", "scripts/tests"], - ["ruff", "check", "scripts"], - ["ruff", "format", "--check", "scripts"], - ["git", "diff", "--check"], - ] - if name == "PLATFORMS": - return [["vcore-scripts", "build", target] for target in ("apple", "android")] - if name == "FINGERPRINT": - return [ - [ - sys.executable, - "-m", - "vcore_scripts.protocol_fingerprint_shape", - "{output}/fingerprint", - ] - ] - return [] diff --git a/scripts/src/vcore_scripts/protocol_shadowtls_native.py b/scripts/src/vcore_scripts/protocol_shadowtls_native.py deleted file mode 100644 index ecf34a3..0000000 --- a/scripts/src/vcore_scripts/protocol_shadowtls_native.py +++ /dev/null @@ -1,306 +0,0 @@ -"""Supplementary native ShadowTLS + unmodified ssserver TCP acceptance.""" - -from __future__ import annotations - -import contextlib -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .native_release import download_native -from .protocol_completion_peers import CASES, peer_configuration -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_fixtures import certificates -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command - - -def run(output: Path, *, supplied=None, image=None): - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("native ShadowTLS evidence must stay in the owned run root") - output.mkdir(parents=True, exist_ok=False) - report = dict( - source=source_identity(), status="NOT RUN", peers={}, isolation={}, cases=[] - ) - directories = {} - try: - with ( - exclusive_run() if supplied is None else contextlib.nullcontext(), - ( - frozen_image(output / "image-pull.log") - if image is None - else contextlib.nullcontext(image) - ) as image, - ): - report["container_image"] = image - artifacts = supplied or { - kind: download_native( - kind, output / kind, "linux-arm64", defer_version=True - ) - for kind in ("ST", "SS") - } - for kind, artifact in artifacts.items(): - report["peers"][kind] = artifact.identity - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "--no-run", - ], - cwd=CORE_DIR, - timeout=180, - ) - (output / "build.log").write_text( - redact(built.stdout.decode(errors="replace")) - ) - if built.returncode or not built.cleanup: - raise RuntimeError("native ShadowTLS consumer build failed") - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temp: - root = Path(temp) - try: - with contextlib.ExitStack() as stack: - for role in ( - "origin", - "ssserver", - "shadow0", - "shadow1", - "shadow2", - ): - directories[role] = root / role - directories[role].mkdir() - cert = certificates(directories["origin"]) - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - directories["origin"] / "origin.py", - ) - origin = lab.start( - stack, - directories["origin"], - "origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "VCORE_ORIGIN_CERT=/data/fixture/cert.pem", - "VCORE_ORIGIN_KEY=/data/fixture/key.pem", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - shutil.copy2( - artifacts["SS"].binary, directories["ssserver"] / "ssserver" - ) - ss = lab.start( - stack, - directories["ssserver"], - "ssserver", - [ - "/data/fixture/ssserver", - "-c", - "/data/fixture/config.json", - ], - ) - peers = {"SS": ss} - servers, nodes, relays = [], [], [] - for index, case in enumerate( - c for c in CASES if c.shadow_tls and not c.uot - ): - directory = directories[f"shadow{index}"] - shutil.copy2( - artifacts["ST"].binary, directory / "shadow-tls" - ) - relay = lab.start( - stack, - directory, - f"shadow{index}", - [ - "env", - "RUST_LOG=error", - "/data/fixture/shadow-tls", - "--threads", - "1", - "--v3", - "--strict", - "server", - "--listen", - "0.0.0.0:23000", - "--server", - f"{ss.ipv4}:{23100 + index}", - "--tls", - f"{origin.ipv4}:24001", - "--password", - "synthetic-shadowtls-fixture", - ], - ) - peers.setdefault("ST", relay) - node, _ = peer_configuration( - case, - server=relay.ipv4, - cover=f"{origin.ipv4}:24001", - port=23000, - certificate=cert, - ) - node.update(name="peer", udp=False) - node["client-fingerprint"] = "chrome" - nodes.append((case.identifier, node)) - relays.append(relay) - servers.append( - dict( - server="::", - server_port=23100 + index, - method=node["cipher"], - password=node["password"], - mode="tcp_only", - ) - ) - for kind, peer in peers.items(): - name = artifacts[kind].binary.name - identity = report["peers"][kind] - identity["version"] = command( - "exec", peer.name, f"/data/fixture/{name}", "--version" - ).strip() - if ( - not identity["version"] - or command( - "exec", - peer.name, - "sha256sum", - f"/data/fixture/{name}", - ).split()[0] - != identity["binary_sha256"] - ): - raise RuntimeError( - "native ShadowTLS peer identity mismatch" - ) - (directories["ssserver"] / "config.json").write_text( - json.dumps(dict(servers=servers)) - ) - origin.release() - origin.wait_tcp(24000) - origin.wait_tcp(24001) - ss.release() - for index in range(3): - ss.wait_tcp(23100 + index) - for relay in relays: - relay.release() - relay.wait_tcp(23000) - for identifier, node in nodes: - fixture = root / "fixture.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - node=node, - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - origin_control=f"{origin.ipv4}:24000", - data_dir=str(root / "data"), - ) - ) - ) - observations = output / f"{identifier}-observations.json" - argv = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "shadowtls::native_tcp", - "--", - "--ignored", - "--exact", - "--nocapture", - ] - result = run_command( - argv, - cwd=CORE_DIR, - timeout=240, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_SHADOWTLS_OBSERVATIONS=str(observations), - VCORE_CASE_EVENTS=str( - output / f"{identifier}-events.jsonl" - ), - ), - ) - (output / f"{identifier}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = ( - json.loads(observations.read_text()) - if observations.is_file() - else None - ) - passed = ( - result.returncode == 0 - and result.cleanup - and observed - == dict( - tcp_bytes_each_direction=10 * 1024 * 1024, - udp_packets=0, - udp_sizes=[], - stop_idle=True, - ) - ) - report["cases"].append( - dict( - case_id=identifier, - status="PASS" if passed else "FAIL", - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - observations=observed, - ) - ) - print( - f"{identifier}: {'PASS' if passed else 'FAIL'}", - flush=True, - ) - if not passed: - raise RuntimeError( - "native ShadowTLS TCP validation failed" - ) - finally: - for role, directory in directories.items(): - if (directory / "peer.log").is_file(): - (output / f"{role}.log").write_text( - redact( - (directory / "peer.log").read_text(errors="replace") - ) - ) - report["status"] = "PASS" - except BaseException as error: - report["status"] = "FAIL" - (output / "failure.log").write_text(redact(str(error))) - raise - finally: - report["cleanup"] = all( - p.get("joined") is True for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - if report["status"] != "PASS": - raise RuntimeError("native ShadowTLS source or cleanup gate failed") - return report - - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - run(parser.parse_args().output) diff --git a/scripts/src/vcore_scripts/protocol_streams.py b/scripts/src/vcore_scripts/protocol_streams.py deleted file mode 100644 index 30e6202..0000000 --- a/scripts/src/vcore_scripts/protocol_streams.py +++ /dev/null @@ -1,310 +0,0 @@ -"""Transport-only native cases; synthetic VLESS is not public YAML support.""" - -from __future__ import annotations - -import contextlib -import json -import socket -import socketserver -import subprocess -import tempfile -import threading -import time -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import reserve_port -from .protocol_fixtures import certificates -from .protocol_peers import OwnedProcess, run_command -from .protocol_preflight import preflight - -CLIENT_ID = "b831381d-6324-4d53-ad4f-8cda48b30811" # Public synthetic identity. -GREETING, TRAILER = b"FOUNDATIONS-server-first\n", b"FOUNDATIONS-half-close\n" -STREAM_CASES = { - "FOUNDATIONS-M-WS": ("M", "ws"), - "FOUNDATIONS-M-WSS": ("M", "wss"), - "FOUNDATIONS-M-GRPC": ("M", "grpc"), - "FOUNDATIONS-M-GRPC-TLS": ("M", "grpc-tls"), - "FOUNDATIONS-M-WS-ED": ("M", "ws-header"), - "FOUNDATIONS-V2-HTTP": ("V2", "http"), - "FOUNDATIONS-V2-H2": ("V2", "h2"), - "FOUNDATIONS-V2-WS-HEADER": ("V2", "ws-header"), - "FOUNDATIONS-V2-WS-PATH": ("V2", "ws-path"), -} - - -def abnormal_cleanup(binary: Path, directory: Path, cert: Path, key: Path) -> dict: - """A real native peer must be joined even when its calling case unwinds.""" - - class InjectedFailure(Exception): - pass - - record = {} - with contextlib.ExitStack() as stack: - port, reservation = reserve_port(stack) - config = directory / "cleanup-peer.json" - config.write_text(json.dumps(peer_config("M", "ws", port, cert, key))) - reservation.release_ipv4() - owner = OwnedProcess( - [str(binary), "-d", str(directory), "-f", str(config)], - directory / "cleanup-peer.log", - record, - ) - try: - with owner: - owner.wait_tcp(port) - raise InjectedFailure - except InjectedFailure: - pass - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as rebind: - rebind.bind(("127.0.0.1", port)) - if not record.get("joined") or owner.process.poll() is None: - raise RuntimeError("native abnormal cleanup failed") - return { - "passed": True, - "kind": "M", - "failure_injected": True, - "joined": True, - "port_rebound": True, - } - - -class Echo(socketserver.BaseRequestHandler): - def handle(self): - self.server.accepted += 1 - self.request.settimeout(15) - try: - self.request.sendall(GREETING) - while data := self.request.recv(8192): - self.server.received += len(data) - self.request.sendall(data) - if self.server.received >= 65536: - break - self.request.sendall(TRAILER) - except OSError: - self.server.failed = True - finally: - self.server.finished.set() - - -@contextlib.contextmanager -def origin(): - with socketserver.TCPServer(("127.0.0.1", 0), Echo) as server: - server.accepted, server.received, server.failed = 0, 0, False - server.finished = threading.Event() - thread = threading.Thread(target=server.serve_forever) - thread.start() - try: - yield server - finally: - server.shutdown() - thread.join(timeout=20) - if thread.is_alive(): - raise RuntimeError("origin cleanup failed") - - -def peer_config(kind, mode, port, cert, key): - encrypted = mode in {"wss", "grpc-tls", "h2"} - if kind == "M": - listener = { - "name": "foundations-stream", - "type": "vless", - "listen": "127.0.0.1", - "port": port, - "users": [{"uuid": CLIENT_ID}], - } - if encrypted: - listener.update(certificate=str(cert), **{"private-key": str(key)}) - else: - listener["allow-insecure"] = True # Explicit loopback-only plaintext. - if mode.startswith("ws"): - listener["ws-path"] = "/foundations-ws" - if mode.startswith("grpc"): - listener["grpc-service-name"] = "foundations-grpc" - return { - "mode": "rule", - "log-level": "silent", - "allow-lan": False, - "bind-address": "127.0.0.1", - "listeners": [listener], - "rules": ["MATCH,DIRECT"], - } - stream = {"network": "tcp", "security": "tls" if encrypted else "none"} - if mode == "http": - stream["tcpSettings"] = { - "header": {"type": "http", "request": {"path": ["/foundations-http"]}} - } - elif mode == "h2": - stream.update( - network="http", - httpSettings={"host": ["localhost"], "path": "/foundations-h2"}, - ) - else: - stream.update( - network="ws", - wsSettings={ - "path": "/foundations-ws", - "maxEarlyData": 2048, - "earlyDataHeaderName": "x-vcore-ed" if mode == "ws-header" else "", - }, - ) - if encrypted: - stream["tlsSettings"] = { - "alpn": ["h2"], - "certificates": [{"certificateFile": str(cert), "keyFile": str(key)}], - } - return { - "log": {"loglevel": "none"}, - "inbounds": [ - { - "listen": "127.0.0.1", - "port": port, - "protocol": "vless", - "settings": {"clients": [{"id": CLIENT_ID}], "decryption": "none"}, - "streamSettings": stream, - } - ], - "outbounds": [{"protocol": "freedom", "settings": {}}], - } - - -def run_streams(output: Path, selected: list[str] | None = None, *, artifacts=None): - selected = list(STREAM_CASES) if selected is None else selected - if ( - not selected - or len(set(selected)) != len(selected) - or not set(selected) <= STREAM_CASES.keys() - ): - raise RuntimeError("invalid native stream case selection") - output.mkdir(parents=True, exist_ok=False) - report = { - "scope": "FOUNDATIONS-shared-transport-only", - "cases": [], - "peers": [], - "cleanup": False, - } - try: - kinds = {STREAM_CASES[case][0] for case in selected} - if artifacts is None: - artifacts, report["preflight"] = preflight(output / "binaries", kinds) - peers = {kind: item.binary for kind, item in artifacts.items() if kind in kinds} - report["peers"] = [ - item.identity for kind, item in artifacts.items() if kind in kinds - ] - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary: - temporary = Path(temporary) - cert, key, pin = certificates(temporary) - if "M" in peers: - report["abnormal_cleanup"] = abnormal_cleanup( - peers["M"], temporary, cert, key - ) - for case_id in selected: - kind, mode = STREAM_CASES[case_id] - record = { - "case_id": case_id, - "peer_kind": kind, - "mode": mode, - "status": "NOT RUN", - "cleanup": {}, - } - report["cases"].append(record) - if kind not in peers: - record.update( - status="BLOCKED", - reason="required peer unavailable", - cleanup={"joined": True, "started": False}, - ) - continue - started = time.monotonic() - try: - with contextlib.ExitStack() as stack: - port, reservation = reserve_port(stack) - config = temporary / "peer.json" - config.write_text( - json.dumps(peer_config(kind, mode, port, cert, key)) - ) - command = ( - [str(peers[kind]), "-d", str(temporary), "-f", str(config)] - if kind == "M" - else [str(peers[kind]), "run", "-c", str(config)] - ) - reservation.release_ipv4() - peer = stack.enter_context( - OwnedProcess( - command, temporary / "peer.log", record["cleanup"] - ) - ) - peer.wait_tcp(port) - with origin() as echo: - fixture = { - "mode": mode, - "server": f"127.0.0.1:{port}", - "target": f"127.0.0.1:{echo.server_address[1]}", - "pin": pin, - "ed_header": "sec-websocket-protocol" - if kind == "M" - else "x-vcore-ed", - } - probe_config = temporary / "probe.json" - probe_config.write_text(json.dumps(fixture)) - completed = run_command( - [ - str( - CORE_DIR - / "target/debug/examples/protocol-stream-probe" - ), - str(probe_config), - ], - timeout=20, - limit=65536, - ) - result = json.loads(completed.stdout) - record["rust_event"] = result - record["command_exit_code"] = completed.returncode - record["origin"] = { - "finished": echo.finished.wait(3), - "accepted": echo.accepted, - "received": echo.received, - "failed": echo.failed, - } - peer.ensure_alive() - record["status"] = ( - "PASS" - if completed.returncode == 0 - and result["outcome"] == "pass" - and result["driver_joined"] - and result["protect_calls"] == 1 - and result.get("resources_idle") is True - and result.get("server_first") is True - and result.get("payload_bytes") == 65536 - and result.get("tail_bytes") == len(TRAILER) - and completed.cleanup - and record["origin"] - == { - "finished": True, - "accepted": 1, - "received": 65536, - "failed": False, - } - else "FAIL" - ) - except ( - OSError, - ValueError, - RuntimeError, - subprocess.SubprocessError, - ) as error: - record.update(status="FAIL", reason=type(error).__name__) - finally: - record["seconds"] = round(time.monotonic() - started, 3) - if not record["cleanup"].get("joined"): - record["status"] = "FAIL" - print(json.dumps(record), flush=True) - report["cleanup"] = True - finally: - (output / "stream-cases.json").write_text(json.dumps(report, indent=2) + "\n") - if not report["cleanup"] or any( - case["status"] != "PASS" for case in report["cases"] - ): - raise RuntimeError("native stream cases did not pass; see structured report") - return report diff --git a/scripts/src/vcore_scripts/protocol_trojan.py b/scripts/src/vcore_scripts/protocol_trojan.py deleted file mode 100644 index f44ae82..0000000 --- a/scripts/src/vcore_scripts/protocol_trojan.py +++ /dev/null @@ -1,363 +0,0 @@ -"""TROJAN public Trojan consumer acceptance against owned official native peers.""" - -from __future__ import annotations - -import contextlib -import json -import os -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import reserve_port -from .protocol_evidence import idle_resources, read_events -from .protocol_fixtures import certificate_chain, certificates -from .protocol_fixtures import trojan_peer_config as peer_config -from .protocol_inputs import redact, source_identity -from .protocol_peers import OwnedProcess, run_command -from .protocol_preflight import preflight - -CASES = { - "TROJAN-M-TCP": ("M", "tcp", "public_trojan_native_base"), - "TROJAN-M-WS": ("M", "ws", "public_trojan_native_base"), - "TROJAN-M-GRPC": ("M", "grpc", "public_trojan_native_base"), - "TROJAN-M-WS-ED-1": ("M", "ws-ed-1", "public_trojan_native_base"), - "TROJAN-M-WS-ED-2048": ("M", "ws-ed-2048", "public_trojan_native_base"), - "TROJAN-M-WS-POLICY": ( - "M", - "ws", - "public_trojan_native_policy_and_group_snapshots", - ), - "TROJAN-M-GRPC-POLICY": ( - "M", - "grpc", - "public_trojan_native_policy_and_group_snapshots", - ), - "TROJAN-M-WS-NEGATIVE": ("M", "ws", "public_trojan_native_transport_negative"), - "TROJAN-M-GRPC-NEGATIVE": ("M", "grpc", "public_trojan_native_transport_negative"), - "TROJAN-M-ALPN-NEGATIVE": ( - "M", - "ws-alpn", - "public_trojan_native_transport_negative", - ), - "TROJAN-V2-WS-HEADER": ( - "V2", - "ws-header", - "public_trojan_native_extended_early_data", - ), - "TROJAN-V2-WS-PATH": ("V2", "ws-path", "public_trojan_native_extended_early_data"), - "TROJAN-M-TCP-POLICY": ( - "M", - "tcp", - "public_trojan_native_policy_and_group_snapshots", - ), - "TROJAN-XR-UDP-DOMAIN": ("XR", "tcp", "public_trojan_native_udp_domain"), - "TROJAN-XR-WS-UDP-DOMAIN": ("XR", "ws", "public_trojan_native_udp_domain"), - "TROJAN-XR-GRPC-UDP-DOMAIN": ("XR", "grpc", "public_trojan_native_udp_domain"), -} -for _mode in ("tcp", "ws", "grpc"): - for _suffix, _test in ( - ("LIFE", "runtime::public_trojan_native_lifecycle"), - ("ENTRYPOINTS", "runtime::public_trojan_native_entrypoints"), - ("UDP-ISOLATION", "runtime::public_trojan_native_udp_isolation_and_limit"), - ): - CASES[f"TROJAN-M-{_mode.upper()}-{_suffix}"] = ("M", _mode, _test) - CASES[f"TROJAN-M-{_mode.upper()}-IPV6"] = ( - "M", - _mode + "-ipv6", - "public_trojan_native_base", - ) - CASES[f"TROJAN-M-{_mode.upper()}-OWNED"] = ( - "M", - _mode, - "trojan_native_owned_resources", - ) - CASES[f"TROJAN-M-{_mode.upper()}-CERTIFICATE"] = ( - "M", - _mode + "-ca", - "runtime::public_trojan_native_certificate_names", - ) -CASES["TROJAN-M-GRPC-CUSTOM"] = ("M", "grpc-custom", "public_trojan_native_base") - - -def native_events_pass(events, test): - suites = {"TROJAN-NATIVE"} - if test == "runtime::public_trojan_native_lifecycle": - suites.add("TROJAN-LIFE-CYCLE") - elif test == "trojan_native_owned_resources": - suites.add("TROJAN-OWNED-CYCLE") - main = [event for event in events if event.get("suite") == "TROJAN-NATIVE"] - if ( - len(main) != 2 - or any( - event.get("assertion") != test or event.get("schema_version") != 1 - for event in main - ) - or [event.get("status") for event in main] != ["BEGIN", "PASS"] - ): - return False - if test in { - "runtime::public_trojan_native_lifecycle", - "trojan_native_owned_resources", - }: - suite = ( - "TROJAN-OWNED-CYCLE" - if test == "trojan_native_owned_resources" - else "TROJAN-LIFE-CYCLE" - ) - cycles = [event for event in events if event.get("suite") == suite] - if len(cycles) != 40: - return False - for begin, end in zip(cycles[::2], cycles[1::2], strict=True): - if ( - begin.get("status") != "BEGIN" - or end.get("status") != "PASS" - or end.get("seconds", 0) < 5 - or any( - event.get("assertion") != "stop_and_remain_quiet" - or event.get("schema_version") != 1 - for event in (begin, end) - ) - ): - return False - if suite == "TROJAN-OWNED-CYCLE": - points = end.get("checkpoints", []) - phases = {point["phase"]: point["resources"] for point in points} - if ( - len(points) != 3 - or set(phases) != {"baseline", "after-stop", "quiet"} - or not all(idle_resources(value) for value in phases.values()) - or phases["after-stop"] != phases["quiet"] - or not idle_resources(end.get("resources")) - ): - return False - return all( - event.get("status") in {"BEGIN", "PASS"} - and event.get("schema_version") == 1 - and event.get("suite") in suites - for event in events - ) - - -def run(output: Path, selected=None, *, artifacts=None): - selected = list(CASES) if selected is None else selected - if ( - not selected - or len(selected) != len(set(selected)) - or not set(selected) <= CASES.keys() - ): - raise ValueError("invalid TROJAN case selection") - output.mkdir(parents=True, exist_ok=False) - report = { - "stage": "TROJAN", - "scope": "protocol-consumer", - "source": source_identity(), - "selected_cases": selected, - "cases": [], - "cleanup": False, - } - try: - if artifacts is None: - artifacts, report["preflight"] = preflight( - output / "binaries", {CASES[case][0] for case in selected} - ) - for case_id in selected: - kind, mode, test = CASES[case_id] - record = { - "case_id": case_id, - "mode": mode, - "cleanup": {}, - "status": "NOT RUN", - } - report["cases"].append(record) - if kind not in artifacts: - record.update( - status="BLOCKED", reason="required official native peer unavailable" - ) - continue - record["peer"] = artifacts[kind].identity - print(f"TROJAN: {case_id}", flush=True) - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as directory, - contextlib.ExitStack() as stack, - ): - directory = Path(directory) - cert, key, pin = ( - certificate_chain if mode.endswith("-ca") else certificates - )(directory) - port, reservation = reserve_port(stack) - password = " synthetic TROJAN 密码 " - config = peer_config(kind, mode, port, password, cert, key) - path = directory / "peer.json" - path.write_text(json.dumps(config)) - node = { - "name": "peer", - "type": "trojan", - "server": "127.0.0.1", - "port": port, - "password": password, - "udp": True, - "sni": "localhost", - "fingerprint": pin, - } - if mode.startswith("ws"): - node.update( - network="ws", - **{ - "ws-opts": { - "path": "/trojan-ws?q=1", - "headers": { - "Host": "cover.example:443", - "X-TROJAN": "fixture", - }, - } - }, - ) - if mode.startswith("grpc"): - node.update( - network="grpc", - **{"grpc-opts": {"grpc-service-name": "trojan-grpc"}}, - ) - if mode.startswith("ws") and kind != "M": - node["ws-opts"]["path"] = "/trojan-ws/" - if mode in {"ws-ed-1", "ws-ed-2048", "ws-header", "ws-path"}: - node["ws-opts"]["max-early-data"] = 1 if mode == "ws-ed-1" else 2048 - if mode in {"ws-header", "ws-path"}: - node["ws-opts"]["early-data-header-name"] = ( - "x-vcore-ed" if mode == "ws-header" else "" - ) - if mode == "ws-alpn": - node["alpn"] = ["h2", "http/1.1"] - if mode == "grpc-custom": - node["grpc-opts"]["grpc-service-name"] = "/trojan-grpc/Tun" - if mode.endswith("-ipv6"): - node["server"] = "::1" - command = ( - [str(artifacts[kind].binary), "-d", str(directory), "-f", str(path)] - if kind == "M" - else [str(artifacts[kind].binary), "run", "-c", str(path)] - ) - if mode.endswith("-ipv6"): - reservation.release_ipv6() - else: - reservation.release_ipv4() - with OwnedProcess( - command, directory / "peer.log", record["cleanup"] - ) as peer: - peer.wait_tcp(port, host=node["server"]) - hop = None - if test == "public_trojan_native_policy_and_group_snapshots": - # Two independently owned peers, not one process proxying - # back into its own loopback detector. - hop_dir = directory / "hop" - hop_dir.mkdir() - hop_cert, hop_key, hop_pin = certificates(hop_dir) - hop_port, hop_reservation = reserve_port(stack) - hop_path = hop_dir / "peer.json" - hop_path.write_text( - json.dumps( - peer_config( - kind, mode, hop_port, password, hop_cert, hop_key - ) - ) - ) - record["upstream_cleanup"] = {} - hop_reservation.release_ipv4() - hop_peer = stack.enter_context( - OwnedProcess( - [ - str(artifacts[kind].binary), - "-d", - str(hop_dir), - "-f", - str(hop_path), - ], - hop_dir / "peer.log", - record["upstream_cleanup"], - ) - ) - hop_peer.wait_tcp(hop_port) - hop = dict(node, name="hop", port=hop_port, fingerprint=hop_pin) - env = dict( - os.environ, - VCORE_CASE_EVENTS=str(output / f"{case_id}-events.jsonl"), - VCORE_TROJAN_FIXTURE=json.dumps( - { - "node": node, - "hop": hop, - "mode": mode, - "udp_payload_max": 2048 - if kind == "V2" - else 8166 - if kind == "XR" - else 8192, - "data_dir": str(directory / "core"), - } - ), - ) - target = ( - "trojan_lifecycle" - if test == "trojan_native_owned_resources" - else "mihomo_interop" - ) - test_name = ( - test if target == "trojan_lifecycle" else f"trojan::{test}" - ) - command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - target, - test_name, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - result = run_command( - command, - cwd=CORE_DIR, - env=env, - timeout=180, - limit=4 * 1024 * 1024, - ) - (output / f"{case_id}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - record.update( - status="PASS" - if result.returncode == 0 - and result.cleanup - and native_events_pass( - read_events(output / f"{case_id}-events.jsonl") - if (output / f"{case_id}-events.jsonl").exists() - else [], - test, - ) - else "FAIL", - exit_code=result.returncode, - command_cleanup=result.cleanup, - seconds=result.seconds, - command=command, - ) - peer.ensure_alive() - report["cleanup"] = all( - case["cleanup"].get("joined") - and case.get("upstream_cleanup", {"joined": True}).get("joined") - for case in report["cases"] - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - return ( - 0 - if report["cleanup"] - and report["source_unchanged"] - and all(case["status"] == "PASS" for case in report["cases"]) - else 1 - ) - finally: - (output / "trojan-results.json").write_text(json.dumps(report, indent=2) + "\n") diff --git a/scripts/src/vcore_scripts/protocol_trojan_acceptance.py b/scripts/src/vcore_scripts/protocol_trojan_acceptance.py deleted file mode 100644 index c3120b8..0000000 --- a/scripts/src/vcore_scripts/protocol_trojan_acceptance.py +++ /dev/null @@ -1,329 +0,0 @@ -"""Trojan stage execution and independent, fail-closed persisted evidence checks.""" - -from __future__ import annotations - -import json - -from .protocol_evidence import HEX, new_result, read_events, read_json, rust_results -from .protocol_preflight import preflight -from .protocol_trojan import native_events_pass -from .protocol_trojan import run as run_native - -# Frozen independently of cases.json: removing or downgrading a case is not signoff. -REQUIRED_IDS = { - "TROJAN-CODEC", - "TROJAN-CFG", - "TROJAN-CANCEL", - "TROJAN-M-TCP", - "TROJAN-M-WS", - "TROJAN-M-GRPC", - "TROJAN-M-WS-ED-1", - "TROJAN-M-WS-ED-2048", - "TROJAN-M-WS-POLICY", - "TROJAN-M-GRPC-POLICY", - "TROJAN-M-WS-NEGATIVE", - "TROJAN-M-GRPC-NEGATIVE", - "TROJAN-M-ALPN-NEGATIVE", - "TROJAN-V2-WS-HEADER", - "TROJAN-V2-WS-PATH", - "TROJAN-M-TCP-POLICY", - "TROJAN-XR-UDP-DOMAIN", - "TROJAN-XR-WS-UDP-DOMAIN", - "TROJAN-XR-GRPC-UDP-DOMAIN", - "TROJAN-M-TCP-LIFE", - "TROJAN-M-TCP-ENTRYPOINTS", - "TROJAN-M-TCP-UDP-ISOLATION", - "TROJAN-M-TCP-IPV6", - "TROJAN-M-TCP-OWNED", - "TROJAN-M-TCP-CERTIFICATE", - "TROJAN-M-WS-LIFE", - "TROJAN-M-WS-ENTRYPOINTS", - "TROJAN-M-WS-UDP-ISOLATION", - "TROJAN-M-WS-IPV6", - "TROJAN-M-WS-OWNED", - "TROJAN-M-WS-CERTIFICATE", - "TROJAN-M-GRPC-LIFE", - "TROJAN-M-GRPC-ENTRYPOINTS", - "TROJAN-M-GRPC-UDP-ISOLATION", - "TROJAN-M-GRPC-IPV6", - "TROJAN-M-GRPC-OWNED", - "TROJAN-M-GRPC-CERTIFICATE", - "TROJAN-M-GRPC-CUSTOM", - "TROJAN-FEATURES", - "TROJAN-SCRIPTS", - "TROJAN-REGRESSION", -} -FIELD_IDS = { - "C01", - "C02", - "C03", - "C04", - "C05", - "C06", - "T01", - "T03", - "T04", - "T05", - "W01", - "W02", - "W03", - "W04", - "W05", - "G01", - "TR01", - "TR02", -} - - -def native_results(cases, report, directory): - records = report.get("cases", []) - if [entry.get("case_id") for entry in records] != [ - case["case_id"] for case in cases - ]: - raise ValueError("missing, duplicate or reordered Trojan native evidence") - results = [] - for case, record in zip(cases, records, strict=True): - path = directory / (case["case_id"] + "-events.jsonl") - events = read_events(path) if path.exists() else [] - observed = native_events_pass(events, case["peer_config"]["test"]) - clean = ( - record.get("command_cleanup") is True - and record.get("cleanup", {}).get("joined") is True - and record.get("upstream_cleanup", {"joined": True}).get("joined") is True - ) - passed = ( - observed - and clean - and record.get("exit_code") == 0 - and record.get("status") == "PASS" - and record.get("mode") == case["peer_config"]["mode"] - and report.get("source_unchanged") is True - ) - result = new_result(case) - result.update( - status="PASS" if passed else "FAIL", - assertions={name: observed for name in case["expected_observation"]}, - evidence=case["required_evidence"] if passed else [], - command_exit_code=record.get("exit_code"), - cleanup=clean, - ) - results.append(result) - return results - - -def fields_report(cases, results): - statuses = {result["case_id"]: result["status"] for result in results} - complete = {case["case_id"] for case in cases} == REQUIRED_IDS - return { - "schema_version": 1, - "stage": "TROJAN", - "protocol": "trojan", - "scope": "protocol-consumer; other protocols remain NOT RUN", - "fields": [ - { - "row_id": row, - "status": "PASS" - if complete - and owners - and all(statuses.get(case_id) == "PASS" for case_id in owners) - else "NOT RUN", - "required_cases": owners, - } - for row in sorted(FIELD_IDS) - for owners in [ - [case["case_id"] for case in cases if row in case["row_ids"]] - ] - ], - "peer_limitations": [ - "Mihomo v1.19.31 domain UDP FAIL is retained; " - "Xray TCP/WS/gRPC supplies separate domain evidence.", - "V2Ray ED supplement UDP path is bounded by its 2048-byte return buffer; " - "8192-byte truncation is retained as peer failure.", - "Xray domain UDP return frame has 8192 total bytes, " - "leaving 8166 payload for the controlled domain; " - "larger peer failure retained.", - ], - } - - -def execute(cases, run, output, records): - from .protocol_harness import _command, _events, _features, _legacy, _scripts - - rust = [case for case in cases if case["runner"] == "rust"] - if rust: - command = _command( - run, - output, - "rust-consumer", - ["cargo", "test", "--locked", "--all-features", "--all-targets"], - 600, - events=output / "rust-events.jsonl", - ) - events = _events(output / "rust-events.jsonl") - for case in rust: - result = rust_results(case, events, command.returncode) - result["cleanup"] = command.cleanup - records[case["case_id"]] = result - kinds = {case["peer_kind"] for case in cases if case["peer_kind"] != "unit"} - artifacts, peers = preflight( - output / "binaries", - kinds, - container=any(case["runner"] == "mihomo-legacy" for case in cases), - ) - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - native = [case for case in cases if case["runner"] == "native-trojan"] - if native: - directory = output / "native" - run_native(directory, [case["case_id"] for case in native], artifacts=artifacts) - report = read_json(directory / "trojan-results.json") - for result in native_results(native, report, directory): - records[result["case_id"]] = result - for case in cases: - if case["runner"] == "features": - records[case["case_id"]] = _features(case, run, output) - elif case["runner"] == "scripts": - records[case["case_id"]] = _scripts(case, run, output) - elif case["runner"] == "mihomo-legacy": - records[case["case_id"]] = _legacy(case, run, output, artifacts) - (output / "fields.json").write_text( - json.dumps(fields_report(cases, list(records.values())), indent=2) + "\n" - ) - - -def check(run_dir, required, run, results, peers, paths): - from .protocol_harness import SCRIPT_OBSERVATIONS - - necessary = { - "cases.json", - "peers.json", - "resources.jsonl", - "summary.md", - "rust-events.jsonl", - "legacy-events.jsonl", - "script-tests.json", - "native/trojan-results.json", - "fields.json", - } - if not necessary <= set(paths): - raise ValueError("missing Trojan acceptance artifacts") - expected_commands = { - "rust-consumer", - "legacy-mihomo", - "offline-scripts", - "feature-default", - "feature-minimal", - "feature-outbound-trojan", - "feature-outbound-vmess", - "feature-outbound-hysteria2", - "feature-stream-transport", - "feature-quic-transport", - } - commands = run.get("commands", []) - if ( - len(commands) != len(expected_commands) - or {command.get("name") for command in commands} != expected_commands - or any( - type(command.get("exit_code")) is not int - or command["exit_code"] != 0 - or command.get("cleanup") is not True - or (command.get("log") and command["log"] not in paths) - for command in commands - ) - ): - raise ValueError("missing, failed or unjoined Trojan acceptance command") - native = [case for case in required if case["runner"] == "native-trojan"] - report = read_json(run_dir / "native/trojan-results.json") - if ( - report.get("source_unchanged") is not True - or report.get("cleanup") is not True - or report.get("source", {}).get("source_tree_sha256") - != run["source_tree_sha256"] - or report.get("source", {}).get("lock_sha256") != run["lock_sha256"] - ): - raise ValueError("Trojan peer tests used another source input") - for record, case in zip(report.get("cases", []), native, strict=True): - for suffix in (".log", "-events.jsonl"): - if "native/" + case["case_id"] + suffix not in paths: - raise ValueError("native log/events missing immutable identity") - identity = [peer for peer in peers if peer.get("kind") == case["peer_kind"]] - if ( - len(identity) != 1 - or identity[0].get("status") != "READY" - or not identity[0].get("version") - or not HEX.fullmatch(identity[0].get("binary_sha256", "")) - or not HEX.fullmatch(identity[0].get("archive_sha256", "")) - or not identity[0].get("source_url", "").startswith("https://github.com/") - or any( - record.get("peer", {}).get(key) != identity[0].get(key) - for key in ("version", "binary_sha256", "archive_sha256", "source_url") - ) - ): - raise ValueError("Trojan used a peer different from official preflight") - test = case["peer_config"]["test"] - target = ( - "trojan_lifecycle" - if test == "trojan_native_owned_resources" - else "mihomo_interop" - ) - name = test if target == "trojan_lifecycle" else "trojan::" + test - if record.get("command") != [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - target, - name, - "--", - "--ignored", - "--exact", - "--nocapture", - ]: - raise ValueError("Trojan case did not execute its declared public consumer") - calculated = native_results(native, report, run_dir / "native") - rust = read_events(run_dir / "rust-events.jsonl") - legacy = read_events(run_dir / "legacy-events.jsonl") - for case in required: - if case["runner"] in {"rust", "mihomo-legacy"}: - entry = rust_results( - case, legacy if case["runner"] == "mihomo-legacy" else rust, 0 - ) - if case["runner"] == "mihomo-legacy" and entry["status"] == "PASS": - entry["evidence"] = case["required_evidence"] - calculated.append(entry) - actual = {entry["case_id"]: entry for entry in results} - for entry in calculated: - if actual[entry["case_id"]] != entry: - raise ValueError( - "Trojan result disagrees with original structured assertions" - ) - script = read_json(run_dir / "script-tests.json") - tests = {entry["test"]: entry["status"] for entry in script.get("cases", [])} - if ( - not tests - or len(tests) != len(script.get("cases", [])) - or len(tests) != script.get("tests_run") - or any(status != "PASS" for status in tests.values()) - or any(tests.get(name) != "PASS" for name in SCRIPT_OBSERVATIONS.values()) - or not any("test_protocol_trojan." in name for name in tests) - ): - raise ValueError("Trojan harness failure paths were not exercised") - mihomo = next(peer for peer in peers if peer["kind"] == "M") - if ( - mihomo.get("container_status") != "READY" - or not HEX.fullmatch( - mihomo.get("container_artifact", {}).get("binary_sha256", "") - ) - or mihomo["container_artifact"].get("release") != mihomo.get("release") - ): - raise ValueError("old regression did not use the same official Mihomo snapshot") - field_report = fields_report(required, results) - if read_json(run_dir / "fields.json") != field_report or any( - row["status"] != "PASS" for row in field_report["fields"] - ): - raise ValueError("incomplete or inconsistent Trojan field report") - print( - f"TROJAN: PASS ({len(required)} required cases, " - f"{len(FIELD_IDS)} Trojan fields); " - "other protocols remain NOT RUN" - ) diff --git a/scripts/src/vcore_scripts/protocol_tuic.py b/scripts/src/vcore_scripts/protocol_tuic.py deleted file mode 100644 index 21a2596..0000000 --- a/scripts/src/vcore_scripts/protocol_tuic.py +++ /dev/null @@ -1,543 +0,0 @@ -"""TUIC v5 public consumers against freshly downloaded, isolated Mihomo.""" - -from __future__ import annotations - -import contextlib -import copy -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_completion_peers import CASES, peer_configuration -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_fixtures import certificate_chain -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command -from .protocol_security_acceptance import rust_command - - -def expected_observation(consumer): - if consumer == "udp_disabled": - return dict(udp_packets=0, tcp_works=True, stop_idle=True) - if consumer == "group": - return dict( - nested_select=True, - pooled_path_retained=True, - explicit_rebuild=True, - reject_no_fallback=True, - new_direct=True, - node_measure=True, - group_measure_rejected=True, - rollback=True, - stop_idle=True, - ) - if consumer == "udp": - return dict( - udp_packets=2100, - udp_sizes=[0, 1, 64, 512, 1200, 4096, 16384], - families=["ipv4", "ipv6", "domain"], - alternating_origins=True, - tcp_sibling=True, - stop_idle=True, - ) - if consumer == "tcp": - return dict( - families=["ipv4", "ipv6", "domain"], - tcp_bytes_each_family_each_direction=10 * 1024 * 1024, - sibling_survives=True, - stop_idle=True, - ) - if consumer == "rejected": - return dict(origin_connected=False, received_business_bytes=0, stop_idle=True) - raise ValueError("unknown TUIC consumer") - - -def inputs(node, listener, checks): - if checks == "negative": - for index, (kind, mode) in enumerate( - (kind, mode) - for kind in ("udp-disabled", "socks5-tcp-only") - for mode in ("native", "quic") - ): - client, peer = copy.deepcopy(node), copy.deepcopy(listener) - client["udp-relay-mode"] = mode - client["udp"] = kind != "udp-disabled" - client["port"] = peer["port"] = 23000 + index - peer["name"] = kind + "-" + mode - yield ( - peer["name"], - client, - peer, - ("udp_disabled" if kind == "udp-disabled" else "rejected"), - ) - return - if checks == "paths": - for index, (upstream, mode) in enumerate( - (upstream, mode) - for upstream in ("socks5", "ss-uot", "anytls") - for mode in ("native", "quic") - ): - client, peer = copy.deepcopy(node), copy.deepcopy(listener) - client["udp-relay-mode"] = mode - client["port"] = peer["port"] = 23000 + index - peer["name"] = upstream + "-" + mode - yield peer["name"], client, peer, "group" - return - if checks == "udp": - for index, mode in enumerate(("native", "quic")): - client, peer = copy.deepcopy(node), copy.deepcopy(listener) - client["udp-relay-mode"] = mode - client["port"] = peer["port"] = 23000 + index - peer["name"] = "udp-" + mode - yield peer["name"], client, peer, "udp" - return - names = ( - ("tcp-cubic", "tcp-new_reno", "tcp-bbr") - if checks == "tcp" - else ( - "alpn-default", - "alpn-custom", - "empty-password", - "raw-password", - "skip", - "verify-name", - "wrong-uuid", - "wrong-password", - "wrong-pin", - "wrong-pin-skip", - "untrusted", - "wrong-name-skip", - "wrong-alpn", - ) - ) - for index, name in enumerate(names): - client, peer = copy.deepcopy(node), copy.deepcopy(listener) - client["udp"] = False - client.pop("udp-relay-mode") - client["port"] = peer["port"] = 23000 + index - peer["name"] = name - if name.startswith("tcp-"): - client["congestion-controller"] = name.removeprefix("tcp-") - elif name == "alpn-default": - del client["alpn"] - elif name == "alpn-custom": - client["alpn"] = ["not-selected", "fixture-custom"] - peer["alpn"] = ["fixture-custom"] - elif name in {"empty-password", "raw-password"}: - client["password"] = ( - "" if name == "empty-password" else " synthetic-原样\t\0 " - ) - peer["users"] = {client["uuid"]: client["password"]} - elif name == "skip": - del client["fingerprint"] - client["skip-cert-verify"] = True - elif name == "verify-name": - client["sni"] = "unrelated.invalid" - client["name-cert-verify"] = "localhost" - elif name == "wrong-uuid": - client["uuid"] = "09090909-0909-0909-0909-090909090909" - elif name == "wrong-password": - client["password"] = "incorrect-synthetic-auth" - elif name.startswith("wrong-pin"): - client["fingerprint"] = "09" * 32 - client["skip-cert-verify"] = name.endswith("skip") - elif name == "untrusted": - del client["fingerprint"] - elif name == "wrong-name-skip": - client["name-cert-verify"] = "unrelated.invalid" - client["skip-cert-verify"] = True - elif name == "wrong-alpn": - client["alpn"] = ["unrelated"] - yield ( - name, - client, - peer, - "rejected" if name.startswith("wrong-") or name == "untrusted" else "tcp", - ) - - -def upstream_pair(name, node, certificate, index): - cert, key, pin = certificate - base = dict(name="hop", server=node["server"], port=23100 + index, udp=True) - listener = dict(name="hop-" + str(index), listen="::", port=base["port"]) - if name.startswith("socks5-"): - return dict(base, type="socks5"), dict(listener, type="socks", udp=True) - if name.startswith("ss-uot-"): - keytext = "BwcHBwcHBwcHBwcHBwcHBw==" - return dict( - base, - type="ss", - cipher="2022-blake3-aes-128-gcm", - password=keytext, - **{"udp-over-tcp": True}, - ), dict( - listener, - type="shadowsocks", - cipher="2022-blake3-aes-128-gcm", - password=keytext, - udp=False, - ) - if name.startswith("anytls-"): - password = "synthetic-hop" - return dict( - base, type="anytls", password=password, sni="localhost", fingerprint=pin - ), dict( - listener, - type="anytls", - users={"fixture": password}, - certificate="/data/fixture/" + cert.name, - **{"private-key": "/data/fixture/" + key.name}, - ) - raise ValueError("unknown TUIC upstream") - - -def run(output, *, checks="tcp", selected=None, supplied=None, image=None): - if checks not in {"tcp", "policy", "udp", "paths", "negative"}: - raise ValueError("unknown TUIC check") - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("TUIC evidence must stay in target/interop/runs") - output.mkdir(parents=True, exist_ok=False) - report = dict( - source=source_identity(), status="NOT RUN", peer={}, isolation={}, cases=[] - ) - try: - with ( - exclusive_run() if supplied is None else contextlib.nullcontext(), - frozen_image(output / "image-pull.log") - if image is None - else contextlib.nullcontext(image) as image, - ): - report["container_image"] = image - if supplied is None: - binary = download_mihomo( - "linux-arm64", directory=output / "binary", identity=report["peer"] - ) - else: - binary, report["peer"] = supplied - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "--no-run", - ], - cwd=CORE_DIR, - timeout=180, - ) - (output / "build.log").write_text( - redact(built.stdout.decode(errors="replace")) - ) - if built.returncode or not built.cleanup: - raise RuntimeError("TUIC consumer build failed") - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as tmp: - root = Path(tmp) - directories = { - role: root / role - for role in ( - "origin", - "server", - *(["upstream"] if checks in {"paths", "negative"} else []), - ) - } - for d in directories.values(): - d.mkdir() - cert = certificate_chain(directories["server"]) - shutil.copy2(binary, directories["server"] / "mihomo") - if checks in {"paths", "negative"}: - for asset in (binary, *cert[:2]): - shutil.copy2( - asset, - directories["upstream"] - / ("mihomo" if asset == binary else asset.name), - ) - shutil.copy2( - Path(__file__).with_name("container_restart_peer.py"), - directories["server"] / "restart.py", - ) - shutil.copy2( - Path(__file__).with_name("container_udp_origin.py"), - directories["origin"] / "origin.py", - ) - try: - with contextlib.ExitStack() as stack: - origin = lab.start( - stack, - directories["origin"], - "origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - directories["server"], - "server", - [ - *( - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/restart.py", - ] - if checks == "paths" - else [] - ), - "/data/fixture/mihomo", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - upstream = ( - lab.start( - stack, - directories["upstream"], - "upstream", - [ - "/data/fixture/mihomo", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - if checks in {"paths", "negative"} - else None - ) - report["peer"]["version"] = command( - "exec", server.name, "/data/fixture/mihomo", "-v" - ).strip() - if ( - command( - "exec", server.name, "sha256sum", "/data/fixture/mihomo" - ).split()[0] - != report["peer"]["binary_sha256"] - ): - raise RuntimeError("TUIC peer binary identity mismatch") - case = next( - c for c in CASES if c.identifier == "tuic-v5-native" - ) - node, listener = peer_configuration( - case, - server=server.ipv4, - cover="", - port=23000, - certificate=cert, - ) - node["name"] = "peer" - rows = list(inputs(node, listener, checks)) - if selected is not None: - if len(set(selected)) != len(selected) or not set( - selected - ) <= {r[0] for r in rows}: - raise ValueError("unknown or repeated TUIC case") - rows = [r for r in rows if r[0] in selected] - if not rows: - raise ValueError("empty TUIC selection") - upstreams = ( - { - r[0]: upstream_pair( - r[0], dict(r[1], server=upstream.ipv4), cert, i - ) - for i, r in enumerate(rows) - if checks == "paths" or r[0].startswith("socks5-") - } - if checks in {"paths", "negative"} - else {} - ) - if checks == "negative": - for client, peer in upstreams.values(): - client["udp"] = peer["udp"] = False - (directories["server"] / "config.json").write_text( - json.dumps( - { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "hosts": {"vcore-fixture.test": origin.ipv4}, - "listeners": [r[2] for r in rows], - "rules": ["MATCH,DIRECT"], - } - ) - ) - if upstream is not None: - (directories["upstream"] / "config.json").write_text( - json.dumps( - { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "listeners": [r[1] for r in upstreams.values()], - "rules": ["MATCH,DIRECT"], - } - ) - ) - if ( - command( - "exec", - upstream.name, - "sha256sum", - "/data/fixture/mihomo", - ).split()[0] - != report["peer"]["binary_sha256"] - or command( - "exec", upstream.name, "/data/fixture/mihomo", "-v" - ).strip() - != report["peer"]["version"] - ): - raise RuntimeError("TUIC upstream identity mismatch") - upstream.release() - upstream.wait_tcp(23999) - origin.release() - origin.wait_tcp(24000) - checked = run_command( - [ - "container", - "exec", - server.name, - "/data/fixture/mihomo", - "-t", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - timeout=30, - ) - (output / "peer-config.log").write_text( - redact(checked.stdout.decode(errors="replace")) - ) - if checked.returncode or not checked.cleanup: - raise RuntimeError( - "TUIC isolated peer configuration rejected" - ) - server.release() - server.wait_tcp(23999) - if checks == "paths": - server.wait_tcp(23998) - for name, node, _, consumer in rows: - fixture = root / "fixture.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - node=node, - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - origin_control=f"{origin.ipv4}:24000", - data_dir=str(root / "data"), - **( - dict(upstream=upstreams[name][0]) - if name in upstreams - else {} - ), - **( - dict(restart_control=f"{server.ipv4}:23998") - if checks == "paths" - else {} - ), - ) - ) - ) - observations = output / (name + "-observations.json") - events = output / (name + "-events.jsonl") - argv = rust_command("vless_public", "tuic::" + consumer) - result = run_command( - argv, - cwd=CORE_DIR, - timeout=240, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_TUIC_OBSERVATIONS=str(observations), - VCORE_CASE_EVENTS=str(events), - ), - ) - (output / (name + ".log")).write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = ( - json.loads(observations.read_text()) - if observations.is_file() - else None - ) - from .protocol_evidence import read_events - from .protocol_hysteria2_catalog import assertions_pass - - good = ( - result.returncode == 0 - and result.cleanup - and observed == expected_observation(consumer) - and assertions_pass( - read_events(events), {("TUIC-PUBLIC", consumer): 1} - ) - ) - report["cases"].append( - dict( - case_id=name, - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - observations=observed, - status="PASS" if good else "FAIL", - ) - ) - print(name + (": PASS" if good else ": FAIL"), flush=True) - if not good: - raise RuntimeError("TUIC public consumer failed") - finally: - for role, d in directories.items(): - if (d / "peer.log").is_file(): - (output / (role + ".log")).write_text( - redact((d / "peer.log").read_text(errors="replace")) - ) - report["status"] = "PASS" - except BaseException as error: - report["status"] = "FAIL" - (output / "failure.log").write_text(redact(str(error))) - raise - finally: - report["cleanup"] = all( - p.get("joined") is True for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - if report["status"] != "PASS": - raise RuntimeError("TUIC source or cleanup failed") - return report - - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument( - "--checks", choices=("tcp", "policy", "udp", "paths", "negative"), default="tcp" - ) - parser.add_argument("--case", action="append") - args = parser.parse_args() - run(args.output, checks=args.checks, selected=args.case) diff --git a/scripts/src/vcore_scripts/protocol_tuic_acceptance.py b/scripts/src/vcore_scripts/protocol_tuic_acceptance.py deleted file mode 100644 index cfc939d..0000000 --- a/scripts/src/vcore_scripts/protocol_tuic_acceptance.py +++ /dev/null @@ -1,230 +0,0 @@ -"""Execute TUIC gates; reconstruct acceptance from raw local and container evidence.""" - -from __future__ import annotations - -import json - -from .core_checks import validate_execution -from .protocol_evidence import read_events, read_json -from .protocol_hysteria2_catalog import M, assertions_pass -from .protocol_hysteria2_catalog import definitions as hy2_definitions -from .protocol_inputs import redact -from .protocol_integration_checks import envelope -from .protocol_security_acceptance import result, rust_command -from .protocol_shadowtls_acceptance import source -from .protocol_tuic import expected_observation -from .protocol_tuic_catalog import ( - CFG_EVENTS, - LOCAL, - MEMORY_EVENTS, - commands, - wire_cases, -) - - -def wire_pass(name, directory, report): - selected = wire_cases(name) - rows = report.get("cases", []) - if [r.get("case_id") for r in rows] != [c[0] for c in selected]: - return False - for row, (identifier, consumer) in zip(rows, selected, strict=True): - if not ( - row.get("command") == rust_command("vless_public", "tuic::" + consumer) - and type(row.get("exit_code")) is int - and row["exit_code"] == 0 - and row.get("command_cleanup") is True - and row.get("observations") - == read_json(directory / (identifier + "-observations.json")) - == expected_observation(consumer) - and assertions_pass( - read_events(directory / (identifier + "-events.jsonl")), - {("TUIC-PUBLIC", consumer): 1}, - ) - ): - return False - return True - - -def local_pass(name, directory, run): - expected = commands(name) - rows = [r for r in run["commands"] if r["name"].startswith("TUIC-" + name + "-")] - if len(rows) != len(expected): - return False - events = [] - for index, (row, argv) in enumerate(zip(rows, expected, strict=True)): - label = f"TUIC-{name}-{index}" - if row["name"] != label or row["command"] != [redact(v) for v in argv]: - return False - validate_execution( - row["command"], - (directory / row["log"]).read_text(), - row["exit_code"], - row["cleanup"], - ) - path = directory / (label + "-events.jsonl") - if path.exists(): - events += read_events(path) - if name in {"MEMORY", "FEATURES"}: - required = ( - MEMORY_EVENTS - if name == "MEMORY" - else { - **{ - key: value - for key, value in MEMORY_EVENTS.items() - if key[1] - not in {"reassembly_order", "reassembly_limits", "delivery_limits"} - }, - **{ - key: 2 if key[1] in {"identity", "redaction"} else value - for key, value in CFG_EVENTS.items() - }, - } - ) - return assertions_pass( - [e for e in events if e.get("suite", "").startswith("TUIC-")], required - ) - return True - - -def group_pass(name, directory, run, peers): - from .protocol_hysteria2_acceptance import h3_result, native_result - - if name in LOCAL: - return local_pass(name, directory, run) - root = directory / name.lower() - digest = run["container_image"]["digest"] - if name.startswith("MIHOMO-"): - report = read_json(root / "report.json") - return envelope(report, peers, source(run), digest, single="M") and wire_pass( - name, root, report - ) - if name.startswith("HYSTERIA2-"): - case = next(c for c in hy2_definitions() if c["case_id"] == name) - report = read_json(root / "report.json") - return ( - envelope(report, peers, source(run), digest) - and native_result(case, report, root)["status"] == "PASS" - ) - if name == "XHTTP-H3": - from .protocol_hysteria2_catalog import H3 - - case = next(c for c in hy2_definitions() if c["case_id"] == H3) - report = read_json(root / "xhttp-fields-results.json") - return ( - envelope(report, peers, source(run), digest) - and h3_result(case, report, root)["status"] == "PASS" - ) - raise ValueError("unknown TUIC gate") - - -def execute(cases, run, output, results): - from .protocol_containers import frozen_image - from .protocol_harness import _command - from .protocol_hysteria2 import run as hysteria2 - from .protocol_hysteria2_acceptance import h3_jobs, prepare - from .protocol_tuic import run as tuic - from .protocol_xhttp_fields import run as h3 - - selected = {c["case_id"].removeprefix("TUIC-"): c for c in cases} - peers = {} - - def save(name): - good = group_pass(name, output, run, peers) - case = selected[name] - results[case["case_id"]] = result(case, good, good) - print(case["case_id"] + (": PASS" if good else ": FAIL"), flush=True) - if not good: - raise RuntimeError("TUIC gate failed: " + name) - - try: - for name in selected: - if name not in LOCAL: - continue - for i, argv in enumerate(commands(name)): - label = f"TUIC-{name}-{i}" - completed = _command( - run, - output, - label, - argv, - 3600, - events=output / (label + "-events.jsonl"), - ) - validate_execution( - argv, - completed.stdout.decode(errors="replace"), - completed.returncode, - completed.cleanup, - ) - save(name) - if set(selected) <= LOCAL: - return - with frozen_image(output / "image-pull.log") as image: - run["container_image"] = image - supplied = prepare(output, {"M", "XR"} if "XHTTP-H3" in selected else {"M"}) - peers.update({k: v[1] for k, v in supplied.items()}) - for name in selected: - if name in LOCAL: - continue - root = output / name.lower() - if name.startswith("MIHOMO-"): - tuic( - root, - checks=name.removeprefix("MIHOMO-").lower(), - supplied=supplied["M"], - image=image, - ) - elif name in M: - test, obfs, _ = M[name] - hysteria2(root, test, obfs=obfs, supplied=supplied["M"]) - elif name == "XHTTP-H3": - jobs = h3_jobs() - h3(root, list(jobs), jobs=jobs, supplied=supplied) - else: - raise ValueError("unknown TUIC group") - save(name) - finally: - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - - -def check(directory, cases, run, results, peers, paths): - if not {"cases.json", "peers.json", "resources.jsonl", "summary.md"} <= set(paths): - raise ValueError("missing TUIC evidence") - expected = [ - f"{c['case_id']}-{i}" - for c in cases - for i, _ in enumerate(commands(c["case_id"].removeprefix("TUIC-"))) - ] - actual = [r["name"] for r in run["commands"]] - if ( - len(actual) != len(set(actual)) - or set(actual) != set(expected) - or set(peers) != {"M", "XR"} - ): - raise ValueError("TUIC command or peer selection differs") - for case in cases: - name = case["case_id"].removeprefix("TUIC-") - if not group_pass(name, directory, run, peers): - raise ValueError("TUIC raw evidence failed: " + name) - print("TUIC coverage: PASS (all required groups; device delivery separate)") - - -def preflight(output): - from .protocol_containers import command, frozen_image - from .protocol_hysteria2_acceptance import prepare - - command("system", "status") - with frozen_image(output / "image-pull.log"): - supplied = prepare(output, {"M", "XR"}) - (output / "peers.json").write_text( - json.dumps( - dict( - scope="download-only", - wire_acceptance="NOT RUN", - peers={k: v[1] for k, v in supplied.items()}, - ), - indent=2, - ) - + "\n" - ) diff --git a/scripts/src/vcore_scripts/protocol_tuic_catalog.py b/scripts/src/vcore_scripts/protocol_tuic_catalog.py deleted file mode 100644 index acaab1b..0000000 --- a/scripts/src/vcore_scripts/protocol_tuic_catalog.py +++ /dev/null @@ -1,183 +0,0 @@ -"""TUIC v5 required groups and independently enumerated wire consumers.""" - -from .protocol_shadowtls_catalog import commands as shared_commands - -LOCAL = {"MEMORY", "FEATURES", "QUALITY", "PLATFORMS", "SHARED-QUIC"} -HY2 = ("TCP", "UDP", "SECURITY", "OWNED", "DEADLINE") -GATES = { - "MEMORY": "datagrams", - "FEATURES": "configuration", - "QUALITY": "regression", - "PLATFORMS": "regression", - "SHARED-QUIC": "regression", - **{ - "MIHOMO-" + name: "interop" - for name in ("TCP", "POLICY", "UDP", "PATHS", "NEGATIVE") - }, - **{"HYSTERIA2-" + name: "regression" for name in HY2}, - "XHTTP-H3": "regression", -} -CFG_EVENTS = {("TUIC-CFG", n): 1 for n in ("identity", "redaction", "udp", "bounds")} -MEMORY_EVENTS = CFG_EVENTS | { - ("TUIC-UNIT", n): 1 - for n in ( - "tcp_owner", - "credit_deadline", - "path_requirements", - "handshake_stop", - "udp_wire", - "unused_association", - "session_retirement", - "partial_control_expiry", - "udp_credit_cancel", - "protect_fail_closed", - "heartbeat_full_queue_stop", - "receive_windows", - "send_buffers", - "reassembly_order", - "reassembly_limits", - "delivery_limits", - ) -} - - -def definitions(): - return [ - dict( - case_id="TUIC-" + name, - stage="TUIC", - substage="TUIC." + category, - required=True, - row_ids=[], - protocol="tuic", - network="quic", - security="tls13", - udp_codec="native/quic", - field_values={}, - outer_family="IPv4", - inner_family="IPv4/IPv6/domain", - target_type="memory-only/build" if name in LOCAL else "isolated origin", - upstream_graph="controlled-datagrams/optional-nested-select", - peer_kind="unit" if name in LOCAL else "M", - peer_config={"group": name}, - gap_source=None, - expected_observation=["TUIC-" + name], - required_evidence=["command", "structured-assertions", "cleanup"] - + ([] if name in LOCAL else ["peer-identity", "isolated-origins"]), - prerequisites=["rust-toolchain"] - if name in LOCAL - else ["owned-host-only-container-network", "official-latest-peer"], - runner="tuic-gate", - timeout_seconds=3600, - ) - for name, category in GATES.items() - ] - - -def wire_cases(name): - if name == "MIHOMO-TCP": - return [("tcp-" + n, "tcp") for n in ("cubic", "new_reno", "bbr")] - if name == "MIHOMO-POLICY": - return [ - (n, "tcp" if i < 6 else "rejected") - for i, n in enumerate( - ( - "alpn-default", - "alpn-custom", - "empty-password", - "raw-password", - "skip", - "verify-name", - "wrong-uuid", - "wrong-password", - "wrong-pin", - "wrong-pin-skip", - "untrusted", - "wrong-name-skip", - "wrong-alpn", - ) - ) - ] - if name == "MIHOMO-UDP": - return [("udp-" + mode, "udp") for mode in ("native", "quic")] - if name == "MIHOMO-PATHS": - return [ - (hop + "-" + mode, "group") - for hop in ("socks5", "ss-uot", "anytls") - for mode in ("native", "quic") - ] - if name == "MIHOMO-NEGATIVE": - return [ - ( - kind + "-" + mode, - "udp_disabled" if kind == "udp-disabled" else "rejected", - ) - for kind in ("udp-disabled", "socks5-tcp-only") - for mode in ("native", "quic") - ] - raise ValueError("unknown TUIC wire group") - - -def commands(name): - cargo = ["cargo", "test", "--locked", "--all-features"] - if name == "MEMORY": - return [ - cargo + ["--test", "tuic_config", "--test", "tuic_memory"], - cargo + ["--lib", "outbound::tuic::"], - ] - if name == "FEATURES": - return [ - ["cargo", "check", "--locked", "--no-default-features"] - + (["--features", f] if f else []) - for f in ("", "outbound-tuic", "quic-transport") - ] + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "interop-test", - "--test", - "tuic_config", - ], - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "outbound-tuic,interop-test", - "--test", - "tuic_config", - "--test", - "tuic_memory", - ], - ["cargo", "test", "--locked", "--test", "feature_foundations"], - cargo + ["--all-targets", "--no-run"], - ] - if name == "SHARED-QUIC": - return [ - cargo - + [ - "--test", - "hysteria2_paths", - "--test", - "hysteria2_packet_ids", - "--test", - "xhttp_h3_shutdown", - "--test", - "limit_foundations", - ], - cargo - + [ - "--test", - "quic_datagram_foundations", - "pending_send_is_not_restarted_and_stop_cancels_without_waiting_for_writable", - "--", - "--exact", - ], - cargo + ["--lib", "outbound::hysteria2::"], - cargo + ["--lib", "security::tls::tests::"], - ] - return shared_commands(name) if name in {"QUALITY", "PLATFORMS"} else [] diff --git a/scripts/src/vcore_scripts/protocol_uot.py b/scripts/src/vcore_scripts/protocol_uot.py deleted file mode 100644 index 07aa629..0000000 --- a/scripts/src/vcore_scripts/protocol_uot.py +++ /dev/null @@ -1,439 +0,0 @@ -"""SS UoT v2 public consumers against unmodified TCP-only Mihomo listeners.""" - -from __future__ import annotations - -import base64 -import contextlib -import copy -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .protocol_completion_peers import CASES, peer_configuration -from .protocol_containers import ContainerLab, command, frozen_image -from .protocol_fixtures import certificates -from .protocol_inputs import redact, same_source, source_identity -from .protocol_peers import run_command -from .protocol_security_acceptance import rust_command - - -def expected_observation(consumer, via_socks5=False): - if consumer == "group": - return dict( - nested_select=True, - old_association_snapshot=True, - new_direct=True, - reject_no_fallback=True, - native_udp_packets=0, - stop_idle=True, - ) - if consumer == "rejected": - return dict( - origin_packets=0, received_packets=0, native_udp_packets=0, stop_idle=True - ) - if consumer != "data": - raise ValueError("unknown UoT consumer") - return dict( - udp_packets=2100, - udp_sizes=[0, 1, 64, 512, 1200, 4096, 16384], - families=["ipv4", "ipv6", "domain"], - alternating_origins=True, - controlled_dns=True, - tcp_regression=True, - stop_idle=True, - tcp_only_upstream=via_socks5, - peer_oversize_rejected=True, - native_udp_packets=0, - ) - - -def run( - output, - *, - selected=None, - via_socks5=False, - supplied=None, - image=None, - checks="data", - native=False, -): - if ( - checks not in {"data", "negative", "group"} - or (checks == "group" and not via_socks5) - or (native and (checks != "negative" or via_socks5)) - ): - raise ValueError("unsupported UoT peer checks") - output = output.resolve() - if not output.is_relative_to((CORE_DIR / "target/interop/runs").resolve()): - raise ValueError("UoT evidence must stay inside target/interop/runs") - cases = tuple(c for c in CASES if c.uot and (not native or not c.shadow_tls)) - if selected is not None: - if len(set(selected)) != len(selected) or not set(selected) <= { - c.identifier for c in cases - }: - raise ValueError("unknown or duplicate UoT selection") - cases = tuple(c for c in cases if c.identifier in selected) - if not cases: - raise ValueError("empty UoT selection") - output.mkdir(parents=True, exist_ok=False) - report = dict( - source=source_identity(), - status="NOT RUN", - peer={}, - isolation={}, - cases=[], - tcp_only_upstream=via_socks5, - ) - try: - with ( - exclusive_run() if supplied is None else contextlib.nullcontext(), - ( - frozen_image(output / "image-pull.log") - if image is None - else contextlib.nullcontext(image) - ) as image, - ): - report["container_image"] = image - if supplied is None: - if native: - from .native_release import download_native - - artifact = download_native( - "SS", output / "binary", "linux-arm64", defer_version=True - ) - binary, report["peer"] = artifact.binary, artifact.identity - else: - binary = download_mihomo( - "linux-arm64", - directory=output / "binary", - identity=report["peer"], - ) - else: - binary, report["peer"] = supplied - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public", - "--no-run", - ], - cwd=CORE_DIR, - timeout=180, - ) - (output / "build.log").write_text( - redact(built.stdout.decode(errors="replace")) - ) - if built.returncode or not built.cleanup: - raise RuntimeError("UoT public consumer build failed") - lab = ContainerLab(report["isolation"], mtu=1500) - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temp: - root = Path(temp) - directories = { - role: root / role - for role in ( - "origin", - "server", - *(["upstream"] if via_socks5 else []), - ) - } - for d in directories.values(): - d.mkdir() - certificate = certificates(directories["server"]) - for source, name in zip( - certificate[:2], ("cert.pem", "key.pem"), strict=True - ): - shutil.copy2(source, directories["origin"] / name) - for source, target in ( - ("container_udp_origin.py", "origin.py"), - ("container_shadowtls_peer.py", "peer.py"), - ): - shutil.copy2( - Path(__file__).with_name(source), directories["origin"] / target - ) - try: - with contextlib.ExitStack() as stack: - origin = lab.start( - stack, - directories["origin"], - "origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "VCORE_ORIGIN_CERT=/data/fixture/cert.pem", - "VCORE_ORIGIN_KEY=/data/fixture/key.pem", - "python", - "-B", - "/data/fixture/peer.py", - ], - ) - peers = {} - peer_name = "ssserver" if native else "mihomo" - for role in ("server", *(["upstream"] if via_socks5 else [])): - shutil.copy2(binary, directories[role] / peer_name) - shutil.copy2( - Path(__file__).with_name("container_uot_watch.py"), - directories[role] / "watch.py", - ) - peer_argv = ( - [ - f"/data/fixture/{peer_name}", - "-c", - "/data/fixture/config.json", - ] - if native - else [ - "/data/fixture/mihomo", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - ) - peers[role] = lab.start( - stack, - directories[role], - role, - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/watch.py", - *peer_argv, - ], - ) - if ( - command( - "exec", - peers[role].name, - "sha256sum", - f"/data/fixture/{peer_name}", - ).split()[0] - != report["peer"]["binary_sha256"] - ): - raise RuntimeError( - "UoT peer identity differs from official download" - ) - server = peers["server"] - report["peer"]["version"] = command( - "exec", - server.name, - f"/data/fixture/{peer_name}", - "--version" if native else "-v", - ).strip() - nodes, listeners = [], [] - for index, case in enumerate(cases): - node, listener = peer_configuration( - case, - server=server.ipv4, - cover=f"{origin.ipv4}:24001", - port=23000 + index, - certificate=certificate, - ) - node["name"] = "peer" - if case.shadow_tls: - node["client-fingerprint"] = "chrome" - else: - del node["udp-over-tcp-version"] - assert listener["udp"] is False - if native: - nodes.append((case.identifier + "-unsupported", node)) - elif checks == "negative": - bad_key = copy.deepcopy(node) - bad_key["password"] = base64.b64encode( - bytes([9]) - * (16 if "aes-128" in node["cipher"] else 32) - ).decode() - nodes.append((case.identifier + "-key", bad_key)) - if case.shadow_tls: - bad_cover = copy.deepcopy(node) - bad_cover["plugin-opts"]["password"] = ( - "incorrect-synthetic-cover" - ) - nodes.append( - (case.identifier + "-identity", bad_cover) - ) - else: - nodes.append((case.identifier, node)) - listeners.append( - dict( - server="::", - server_port=node["port"], - method=node["cipher"], - password=node["password"], - mode="tcp_only", - ) - if native - else listener - ) - - def configuration(listeners): - return { - "socks-port": 23999, - "allow-lan": True, - "bind-address": "*", - "ipv6": True, - "log-level": "warning", - "listeners": listeners, - "rules": ["MATCH,DIRECT"], - } - - (directories["server"] / "config.json").write_text( - json.dumps( - dict(servers=listeners) - if native - else configuration(listeners) - ) - ) - upstream = None - if via_socks5: - upstream = dict( - name="hop", - type="socks5", - server=peers["upstream"].ipv4, - port=23050, - udp=False, - ) - (directories["upstream"] / "config.json").write_text( - json.dumps( - configuration( - [ - dict( - name="hop", - type="socks", - listen="::", - port=23050, - udp=False, - ) - ] - ) - ) - ) - origin.release() - origin.wait_tcp(24000) - origin.wait_tcp(24001) - for peer in peers.values(): - peer.release() - peer.wait_tcp(23998) - peer.wait_tcp(23000 if native else 23999) - for identifier, node in nodes: - fixture = root / "fixture.json" - value = dict( - isolation="containers", - node=node, - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - origin_control=f"{origin.ipv4}:24000", - udp_observer=f"{server.ipv4}:23998", - data_dir=str(root / "data"), - ) - if upstream is not None: - value["upstream"] = upstream - fixture.write_text(json.dumps(value)) - observations = output / (identifier + "-observations.json") - consumer = "rejected" if checks == "negative" else checks - argv = rust_command("vless_public", "uot::" + consumer) - result = run_command( - argv, - cwd=CORE_DIR, - timeout=240, - env=dict( - os.environ, - VCORE_VLESS_INPUT=str(fixture), - VCORE_UOT_OBSERVATIONS=str(observations), - VCORE_CASE_EVENTS=str( - output / (identifier + "-events.jsonl") - ), - ), - ) - (output / (identifier + ".log")).write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = ( - json.loads(observations.read_text()) - if observations.is_file() - else None - ) - expected = expected_observation(consumer, via_socks5) - from .protocol_evidence import read_events - from .protocol_hysteria2_catalog import assertions_pass - - good = ( - result.returncode == 0 - and result.cleanup - and observed == expected - and assertions_pass( - read_events( - output / (identifier + "-events.jsonl") - ), - {("UOT-PUBLIC", consumer): 1}, - ) - ) - report["cases"].append( - dict( - case_id=identifier, - command=argv, - exit_code=result.returncode, - command_cleanup=result.cleanup, - observations=observed, - listener_native_udp=False, - status="PASS" if good else "FAIL", - ) - ) - print( - identifier + (": PASS" if good else ": FAIL"), - flush=True, - ) - if not good: - raise RuntimeError("UoT public data failed") - finally: - for role, d in directories.items(): - if (d / "peer.log").is_file(): - (output / (role + ".log")).write_text( - redact((d / "peer.log").read_text(errors="replace")) - ) - report["status"] = "PASS" - except BaseException as error: - report["status"] = "FAIL" - (output / "failure.log").write_text(redact(str(error))) - raise - finally: - report["cleanup"] = all( - p.get("joined") is True for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = same_source(report["source"], source_identity()) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - if report["status"] != "PASS": - raise RuntimeError("UoT source or cleanup failed") - return report - - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("--case", action="append") - parser.add_argument("--via-socks5", action="store_true") - parser.add_argument( - "--checks", choices=("data", "negative", "group"), default="data" - ) - parser.add_argument("--native", action="store_true") - args = parser.parse_args() - run( - args.output, - selected=args.case, - via_socks5=args.via_socks5, - checks=args.checks, - native=args.native, - ) diff --git a/scripts/src/vcore_scripts/protocol_uot_acceptance.py b/scripts/src/vcore_scripts/protocol_uot_acceptance.py deleted file mode 100644 index 9dbafd8..0000000 --- a/scripts/src/vcore_scripts/protocol_uot_acceptance.py +++ /dev/null @@ -1,247 +0,0 @@ -"""Execute UoT gates and independently verify their raw evidence.""" - -from __future__ import annotations - -import json - -from .core_checks import validate_execution -from .protocol_evidence import read_events, read_json -from .protocol_hysteria2_catalog import assertions_pass -from .protocol_inputs import redact -from .protocol_integration_checks import envelope, runtime_pass -from .protocol_security_acceptance import REPORTS, result, rust_command, vless_pass -from .protocol_shadowtls_acceptance import source -from .protocol_uot import expected_observation -from .protocol_uot_catalog import LOCAL, MEMORY_EVENTS, commands, wire_cases - - -def wire_pass(name, directory, report): - selected = wire_cases(name) - rows = report.get("cases", []) - if [r.get("case_id") for r in rows] != [c[0] for c in selected]: - return False - via_socks5 = name in {"MIHOMO-SOCKS5", "MIHOMO-GROUPS"} - if report.get("tcp_only_upstream") is not via_socks5: - return False - for row, (identifier, consumer) in zip(rows, selected, strict=True): - if not ( - row.get("command") == rust_command("vless_public", "uot::" + consumer) - and type(row.get("exit_code")) is int - and row["exit_code"] == 0 - and row.get("command_cleanup") is True - and row.get("listener_native_udp") is False - and row.get("observations") - == read_json(directory / (identifier + "-observations.json")) - == expected_observation(consumer, via_socks5) - and assertions_pass( - read_events(directory / (identifier + "-events.jsonl")), - {("UOT-PUBLIC", consumer): 1}, - ) - ): - return False - return True - - -def local_pass(name, directory, run): - expected = commands(name) - rows = [r for r in run["commands"] if r["name"].startswith("UOT-" + name + "-")] - if len(rows) != len(expected): - return False - events = [] - for index, (row, argv) in enumerate(zip(rows, expected, strict=True)): - label = f"UOT-{name}-{index}" - if row["name"] != label or row["command"] != [redact(v) for v in argv]: - return False - validate_execution( - row["command"], - (directory / row["log"]).read_text(), - row["exit_code"], - row["cleanup"], - ) - path = directory / (label + "-events.jsonl") - if path.exists(): - events += read_events(path) - if name == "MEMORY": - return assertions_pass( - [e for e in events if e.get("suite", "").startswith("UOT-")], MEMORY_EVENTS - ) - if name == "FEATURES": - return assertions_pass( - [e for e in events if e.get("suite", "").startswith("UOT-")], - MEMORY_EVENTS | {("UOT-CFG", "strict_v2"): 2}, - ) - return True - - -ANYTLS = dict(selected=["FINGERPRINT-ANYTLS"], client_fingerprint="none") - - -def group_pass(name, directory, run, peers): - if name in LOCAL: - return local_pass(name, directory, run) - root = directory / name.lower() - digest = run["container_image"]["digest"] - if name == "ANYTLS": - report = read_json(root / REPORTS["vless"]) - return envelope(report, peers, source(run), digest) and vless_pass( - ANYTLS, report, root - ) - if name == "BARE-SS": - report = read_json(root / "integration-suite.json") - rows = report.get("cases", []) - return ( - envelope(report, peers, source(run), digest) - and len(rows) == 1 - and runtime_pass("INTEGRATION-SS-ALGORITHMS", rows[0], root) - ) - report = read_json(root / "report.json") - return envelope( - report, - peers, - source(run), - digest, - single="SS" if name == "NATIVE-UNSUPPORTED" else "M", - ) and wire_pass(name, root, report) - - -def prepare(output, *, native): - from .native_release import download_native - from .protocol_hysteria2_acceptance import prepare as mihomo - - supplied = mihomo(output, {"M"}) - artifact = ( - download_native("SS", output / "binaries/SS", "linux-arm64", defer_version=True) - if native - else None - ) - return supplied, artifact - - -def execute(cases, run, output, results): - from .protocol_containers import frozen_image - from .protocol_harness import _command - from .protocol_integration_suite import run as integration - from .protocol_uot import run as uot - from .protocol_vless_container import run as vless - - selected = {c["case_id"].removeprefix("UOT-"): c for c in cases} - peers = {} - - def save(name): - good = group_pass(name, output, run, peers) - case = selected[name] - results[case["case_id"]] = result(case, good, good) - print(case["case_id"] + (": PASS" if good else ": FAIL"), flush=True) - if not good: - raise RuntimeError("UoT gate failed: " + name) - - try: - for name in selected: - if name not in LOCAL: - continue - for i, argv in enumerate(commands(name)): - label = f"UOT-{name}-{i}" - completed = _command( - run, - output, - label, - argv, - 3600, - events=output / (label + "-events.jsonl"), - ) - validate_execution( - argv, - completed.stdout.decode(errors="replace"), - completed.returncode, - completed.cleanup, - ) - save(name) - if set(selected) <= LOCAL: - return - with frozen_image(output / "image-pull.log") as image: - run["container_image"] = image - supplied, artifact = prepare( - output, native="NATIVE-UNSUPPORTED" in selected - ) - peers.update({k: v[1] for k, v in supplied.items()}) - if artifact is not None: - peers["SS"] = artifact.identity - for name in selected: - if name in LOCAL: - continue - root = output / name.lower() - if name.startswith("MIHOMO-"): - uot( - root, - via_socks5=name in {"MIHOMO-SOCKS5", "MIHOMO-GROUPS"}, - checks="negative" - if name == "MIHOMO-NEGATIVE" - else "group" - if name == "MIHOMO-GROUPS" - else "data", - supplied=supplied["M"], - image=image, - ) - elif name == "NATIVE-UNSUPPORTED": - uot( - root, - native=True, - checks="negative", - supplied=(artifact.binary, artifact.identity), - image=image, - ) - elif name == "ANYTLS": - vless(root, supplied=supplied, **ANYTLS) - elif name == "BARE-SS": - integration( - root, ["INTEGRATION-SS-ALGORITHMS"], supplied=supplied["M"] - ) - else: - raise ValueError("unknown UoT gate") - save(name) - finally: - (output / "peers.json").write_text(json.dumps(peers, indent=2) + "\n") - - -def check(directory, cases, run, results, peers, paths): - if not {"cases.json", "peers.json", "resources.jsonl", "summary.md"} <= set(paths): - raise ValueError("missing UoT evidence") - expected = [ - f"UOT-{c['case_id'].removeprefix('UOT-')}-{i}" - for c in cases - for i, _ in enumerate(commands(c["case_id"].removeprefix("UOT-"))) - ] - actual = [r["name"] for r in run["commands"]] - if ( - len(actual) != len(set(actual)) - or set(actual) != set(expected) - or set(peers) != {"M", "SS"} - ): - raise ValueError("UoT command or peer selection differs") - for case in cases: - name = case["case_id"].removeprefix("UOT-") - if not group_pass(name, directory, run, peers): - raise ValueError("UoT raw evidence failed: " + name) - print("UOT coverage: PASS (all required groups; device delivery separate)") - - -def preflight(output): - from .protocol_containers import command, frozen_image - - command("system", "status") - with frozen_image(output / "image-pull.log"): - supplied, artifact = prepare(output, native=True) - (output / "peers.json").write_text( - json.dumps( - dict( - scope="download-only", - wire_acceptance="NOT RUN", - peers={ - **{k: v[1] for k, v in supplied.items()}, - "SS": artifact.identity, - }, - ), - indent=2, - ) - + "\n" - ) diff --git a/scripts/src/vcore_scripts/protocol_uot_catalog.py b/scripts/src/vcore_scripts/protocol_uot_catalog.py deleted file mode 100644 index 19f8b55..0000000 --- a/scripts/src/vcore_scripts/protocol_uot_catalog.py +++ /dev/null @@ -1,149 +0,0 @@ -"""SS UoT v2 gates, reconstructed from existing runner evidence conventions.""" - -from .protocol_completion_peers import CASES -from .protocol_shadowtls_catalog import commands as shared_commands - -UOT_CASES = tuple(c for c in CASES if c.uot) -GATES = { - "MEMORY": "datagrams", - "FEATURES": "configuration", - "QUALITY": "regression", - "PLATFORMS": "regression", - "MIHOMO-DIRECT": "interop", - "MIHOMO-SOCKS5": "interop", - "MIHOMO-GROUPS": "interop", - "MIHOMO-NEGATIVE": "interop", - "NATIVE-UNSUPPORTED": "interop", - "ANYTLS": "regression", - "BARE-SS": "regression", -} -LOCAL = {"MEMORY", "FEATURES", "QUALITY", "PLATFORMS"} -MEMORY_EVENTS = { - ("UOT-CFG", "strict_v2"): 1, - **{ - ("UOT-SS", n): 1 - for n in ( - "first_packet", - "never_sent", - "deadline", - "cancel_write", - "budget_read_cancel", - "dns", - "stop_full_queue", - ) - }, -} - - -def definitions(): - return [ - dict( - case_id="UOT-" + name, - stage="UOT", - substage="UOT." + category, - required=True, - row_ids=[], - protocol="ss", - network="tcp", - security="ss2022/optional-v3", - udp_codec="uot-v2", - field_values={}, - outer_family="IPv4", - inner_family="IPv4/IPv6/domain", - target_type="memory-only/build" if name in LOCAL else "isolated origin", - upstream_graph="controlled-stream/optional-socks5-or-select", - peer_kind="unit" - if name in LOCAL - else "SS" - if name == "NATIVE-UNSUPPORTED" - else "M", - peer_config={"group": name}, - gap_source=None, - expected_observation=["UOT-" + name], - required_evidence=["command", "structured-assertions", "cleanup"] - + ([] if name in LOCAL else ["peer-identity", "isolated-origins"]), - prerequisites=["rust-toolchain"] - if name in LOCAL - else ["owned-host-only-container-network", "official-latest-peer"], - runner="uot-gate", - timeout_seconds=3600, - ) - for name, category in GATES.items() - ] - - -def wire_cases(name): - if name in {"MIHOMO-DIRECT", "MIHOMO-SOCKS5", "MIHOMO-GROUPS"}: - return [ - (c.identifier, "group" if name == "MIHOMO-GROUPS" else "data") - for c in UOT_CASES - ] - if name == "NATIVE-UNSUPPORTED": - return [ - (c.identifier + "-unsupported", "rejected") - for c in UOT_CASES - if not c.shadow_tls - ] - if name == "MIHOMO-NEGATIVE": - return [ - (c.identifier + "-" + failure, "rejected") - for c in UOT_CASES - for failure in (("key", "identity") if c.shadow_tls else ("key",)) - ] - raise ValueError("unknown UoT wire group") - - -def commands(name): - cargo = ["cargo", "test", "--locked", "--all-features"] - if name == "MEMORY": - return [ - cargo - + [ - arg - for target in ( - "uot_config", - "shadowsocks_uot", - "shadowsocks_backpressure", - ) - for arg in ("--test", target) - ], - cargo + ["--lib", "outbound::uot::"], - cargo + ["--lib", "outbound::anytls::"], - ] - if name == "FEATURES": - return [ - ["cargo", "check", "--locked", "--no-default-features"] - + (["--features", feature] if feature else []) - for feature in ( - "", - "outbound-shadowsocks", - "outbound-anytls", - "outbound-shadowsocks,shadow-tls-v3", - ) - ] + [ - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "interop-test", - "--test", - "uot_config", - ], - [ - "cargo", - "test", - "--locked", - "--no-default-features", - "--features", - "outbound-shadowsocks,interop-test", - "--test", - "uot_config", - "--test", - "shadowsocks_uot", - ], - ["cargo", "test", "--locked", "--test", "feature_foundations"], - cargo + ["--all-targets", "--no-run"], - ] - return shared_commands(name) if name in {"QUALITY", "PLATFORMS"} else [] diff --git a/scripts/src/vcore_scripts/protocol_vless.py b/scripts/src/vcore_scripts/protocol_vless.py deleted file mode 100644 index 363f1a3..0000000 --- a/scripts/src/vcore_scripts/protocol_vless.py +++ /dev/null @@ -1,27 +0,0 @@ -"""VLESS development mode selection, using the same container fixtures as acceptance. - -This entry does not execute local/build gates or sign off the complete stage. -""" - -from pathlib import Path - -from .protocol_vless_container import run as run_cases -from .protocol_vless_peers import MODES -from .protocol_vless_public import CASES - - -def run(output: Path, modes=None): - modes = MODES if modes is None else modes - if not modes or not set(modes) <= set(MODES) or len(set(modes)) != len(modes): - raise ValueError("invalid VLESS development mode") - selected = [case for case, (_, mode, _, _) in CASES.items() if mode in modes] - return run_cases(output, selected) - - -if __name__ == "__main__": - import sys - - from .mihomo_isolation import exclusive_run - - with exclusive_run(): - sys.exit(run(Path(sys.argv[1]).resolve(), sys.argv[2:] or None)) diff --git a/scripts/src/vcore_scripts/protocol_vless_acceptance.py b/scripts/src/vcore_scripts/protocol_vless_acceptance.py deleted file mode 100644 index 8b80e85..0000000 --- a/scripts/src/vcore_scripts/protocol_vless_acceptance.py +++ /dev/null @@ -1,652 +0,0 @@ -"""VLESS container-only public consumer acceptance and independent coverage.""" - -from __future__ import annotations - -import json -import sys - -from .builds import DEFAULT_FEATURES -from .protocol_evidence import HEX, idle_resources, new_result, read_events, read_json -from .protocol_inputs import redact -from .protocol_vless_container import run as native_run -from .protocol_vless_public import PUBLIC, WIRE, events_pass -from .protocol_vmess_acceptance import OBSERVATIONS as PREVIOUS_OBSERVATIONS -from .protocol_vmess_acceptance import SCRIPT_ASSERTIONS - -NATIVE = WIRE | PUBLIC -FIELD_IDS = { - *(f"C{i:02}" for i in range(1, 7)), - *(f"T{i:02}" for i in range(2, 9)), - *(f"W{i:02}" for i in range(1, 8)), - *(f"G{i:02}" for i in range(1, 7)), - *(f"H{i:02}" for i in range(1, 7)), - *(f"VL{i:02}" for i in range(1, 6)), - "S01", - "S02", -} -GATES = { - "VLESS-CFG", - "VLESS-CODEC", - "VLESS-TRANSPORT", - "VLESS-VISION", - "VLESS-REGRESSION", - "VLESS-RELEASE", - "VLESS-FEATURES", - "VLESS-SCRIPTS", - "VLESS-QUALITY", -} -REQUIRED_IDS = set(NATIVE) | GATES -OBSERVATIONS = { - "VLESS-CFG": [ - "inline_client_identity_is_validated_and_redacted_before_io", - "vision_requires_tcp_tls13_and_xudp_before_any_io", - "default_tcp_and_three_udp_encodings_are_accepted_without_io", - "tcp_and_existing_xhttp_reject_mismatched_and_future_options", - "stream_transports_and_explicit_tls_policy_have_strict_public_fields", - "extended_ws_and_grpc_fields_are_scoped_and_bounded", - ], - "VLESS-CODEC": [ - "stopping_before_first_udp_send_wakes_receive_and_releases_io", - "raw_and_packetaddr_consume_wire_and_keep_cancelled_receive_progress", - "udp_cancelled_send_and_bad_response_header_close_owned_io", - "tcp_bad_response_header_poisoning_and_absolute_response_deadline", - "vless_all_handshakes_keep_the_original_deadline_and_join_cancelled_io", - "vless_expired_deadline_and_protect_failure_never_fall_back", - ], - "VLESS-TRANSPORT": [ - "http_camouflage_shutdown_releases_both_directions_without_waiting_for_peer_eof", - "grpc_server_first_flushes_vless_request_without_an_application_write", - "http_upgrade_consumes_early_prefix_and_requires_valid_101_even_fast_open", - "grpc_idle_ping_is_observable_disabled_by_zero_and_joined_at_stop", - "grpc_pool_matches_both_threshold_policies_and_keeps_other_streams_alive", - ], - "VLESS-VISION": [ - "vision_fragmented_headers_content_padding_and_cancelled_reads_keep_raw_tail", - "vision_invalid_uuid_command_and_truncation_poison_the_stream", - "vision_bounded_partial_writes_flush_and_close_preserve_all_plaintext", - "fragmented_server_hello_distinguishes_tls12_tls13_and_non_tls", - "record_boundary_preserves_buffered_plaintext_and_coalesced_raw_tail_and_flush_order", - ], -} -OBSERVATIONS["VLESS-REGRESSION"] = ( - PREVIOUS_OBSERVATIONS["VMESS-REGRESSION"] - + PREVIOUS_OBSERVATIONS["VMESS-CFG"] - + PREVIOUS_OBSERVATIONS["VMESS-CODEC"] - + PREVIOUS_OBSERVATIONS["VMESS-CANCEL"] - + [ - "all_two_hop_protocol_combinations_build_as_connector_graphs", - "direct_download_constructor_reuses_or_requires_the_precise_prepared_endpoint", - "split_node_over_a_proxy_automatically_reuses_its_parent_for_both_legs", - ] -) -OBSERVATIONS["VLESS-RELEASE"] = sum( - ( - OBSERVATIONS[key] - for key in ("VLESS-CFG", "VLESS-CODEC", "VLESS-TRANSPORT", "VLESS-VISION") - ), - [], -) - - -def rows(identifier): - if identifier == "VLESS-CFG": - return sorted(FIELD_IDS) - if identifier == "VLESS-CODEC": - return ["VL01", "VL02", "VL04"] - if identifier == "VLESS-TRANSPORT": - return [*(f"G{i:02}" for i in range(1, 7)), "W06", "W07"] - if identifier == "VLESS-VISION": - return ["VL03", "VL04", "VL05", "S01", "S02"] - if identifier in GATES or identifier.startswith("VLESS-REGRESSION"): - return [] - _, mode, tls, test = NATIVE[identifier] - result = {"VL01", "VL02", "VL04", "VL05"} - if identifier in PUBLIC: - result |= {*(f"C{i:02}" for i in range(1, 6))} - if "graph" in test or "selection" in test: - result.add("C06") - if mode.endswith("-reality"): - result |= {"S01", "S02", "T02"} - elif tls: - result |= {"T02", "T03", "T04", "T05"} - if mode.endswith("-mtls"): - result |= {"T06", "T07", "T08"} - if mode.startswith("vision-"): - result.add("VL03") - if mode.startswith(("ws", "upgrade")): - result |= {*(f"W{i:02}" for i in range(1, 6))} - if mode.startswith("upgrade"): - result |= {"W06", "W07"} - if mode.startswith("grpc"): - result |= {*(f"G{i:02}" for i in range(1, 7))} - if mode.startswith("http"): - result |= {"H01", "H02", "H03", "H04"} - if mode.startswith("h2"): - result |= {"H05", "H06"} - return sorted(result) - - -def definitions(): - cases = [] - for identifier in sorted(REQUIRED_IDS): - kind, mode, tls, test = NATIVE.get( - identifier, ("unit", "memory-or-build", False, identifier) - ) - native = identifier in NATIVE - substage = ( - "VLESS.vision" - if mode.startswith("vision-") or identifier == "VLESS-VISION" - else "VLESS.tcp" - if mode in {"tcp", "tcp-tls"} or identifier == "VLESS-CODEC" - else "VLESS.transports" - if mode.startswith(("ws", "grpc", "http", "h2", "upgrade")) - or identifier == "VLESS-TRANSPORT" - else "VLESS.acceptance" - ) - cases.append( - dict( - case_id=identifier, - stage="VLESS", - substage=substage, - required=True, - row_ids=rows(identifier), - protocol="vless", - network=mode, - security="tls" if tls else "plain", - udp_codec="raw/xudp/packetaddr" - if "udp" in test or test == "public_base" - else "per-assertion", - field_values={ - "network": mode, - "tls": tls, - "scope": "protocol-consumer", - "close_reference": "ws-standard-tls-baseline" - if test == "native_ws_reality_close_boundary" - else "same-mode" - if test == "native_mihomo_close_alignment" - else "not-applicable", - }, - outer_family="IPv4/IPv6" if "ipv6" in test else "IPv4", - inner_family="IPv4/IPv6/domain" - if "base" in test or "udp_boundaries" in test - else "per-assertion", - target_type="isolated synthetic origin" if native else "memory-only", - upstream_graph="direct/concrete/nested-select" - if "graph" in test - else "per-assertion", - peer_kind=kind, - peer_config=dict(mode=mode, tls=tls, test=test), - gap_source={ - "reason": "Mihomo WS client ignores RealityOpts; " - "REALITY listener data " - "is native-tested, but close uses the separate standard-TLS WS " - "adapter reference plus independent REALITY close cases, not a " - "same-combination client differential. Original failures retained.", - "source": "https://github.com/MetaCubeX/mihomo/blob/ab405bad5beeeac8b003bb01f60f134f6df54471/adapter/outbound/vless.go", - } - if test == "native_ws_reality_close_boundary" - else { - "reason": "Mihomo VLESS listener has no HTTP header/legacy H2 " - "or custom ED field; official V2Ray supplements only these paths", - "source": "https://github.com/MetaCubeX/mihomo/blob/v1.19.31/listener/inbound/vless.go", - } - if kind == "V2" - else None, - expected_observation=[test] - if native - else SCRIPT_ASSERTIONS - if identifier == "VLESS-SCRIPTS" - else OBSERVATIONS.get(identifier, [identifier]), - required_evidence=["structured-assertions", "command", "cleanup"] - + (["peer-identity", "isolated-origins"] if native else []), - prerequisites=[ - "owned-host-only-container-network", - "official-latest-peer", - ] - if native - else ["rust-toolchain"], - runner="native-vless" if native else "vless-gate", - timeout_seconds=240 if native else 1200, - ) - ) - return cases - - -def commands(identifier): - cargo = ["cargo", "test", "--locked", "--all-features"] - if identifier == "VLESS-CFG": - return [cargo + ["--test", "vless_config"], cargo + ["--lib", "config::"]] - if identifier == "VLESS-CODEC": - return [cargo + ["--test", "vless_codec", "--test", "vless_lifecycle"]] - if identifier == "VLESS-TRANSPORT": - return [cargo + ["--test", "vless_transports", "--test", "grpc_pool"]] - if identifier == "VLESS-VISION": - return [cargo + ["--lib", "vision"]] - if identifier == "VLESS-RELEASE": - return [ - cargo - + [ - "--release", - "--test", - "vless_config", - "--test", - "vless_codec", - "--test", - "vless_lifecycle", - "--test", - "vless_transports", - "--test", - "grpc_pool", - ], - cargo + ["--release", "--lib", "vision"], - ] - if identifier == "VLESS-REGRESSION": - from .protocol_vmess_acceptance import commands as previous - - return ( - previous("VMESS-REGRESSION") - + previous("VMESS-CFG") - + previous("VMESS-CODEC") - + previous("VMESS-CANCEL") - + [ - cargo - + ["--lib", "outbound::vless::outbound::connector_composition_tests::"] - ] - + [ - ["cargo", "test", "--locked", "--no-default-features"] - + features - + [ - "--test", - "feature_foundations", - "vless_yaml_follows_its_own_feature", - "--", - "--exact", - ] - for features in ([], ["--features", "outbound-trojan"]) - ] - ) - if identifier == "VLESS-QUALITY": - from .protocol_vmess_acceptance import commands as previous - - return previous("VMESS-QUALITY") - return [] - - -def gate_result(case, records, events): - result = new_result(case) - good = bool(records) and all( - r.get("exit_code") == 0 and r.get("cleanup") is True for r in records - ) - identifier = case["case_id"] - assertions = {} - if identifier in OBSERVATIONS: - for name in case["expected_observation"]: - observed = [e for e in events if e.get("assertion") == name] - assertions[name] = len(observed) == 2 and [ - e.get("status") for e in observed - ] == ["BEGIN", "PASS"] - good &= ( - bool(assertions) - and all(assertions.values()) - and all( - e.get("schema_version") == 1 and e.get("status") in {"BEGIN", "PASS"} - for e in events - ) - ) - else: - assertions = dict.fromkeys(case["expected_observation"], good) - result.update( - status="PASS" if good else "FAIL", - assertions=assertions, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=all(r.get("cleanup") is True for r in records), - ) - return result - - -def native_results(cases, report, directory): - records = report.get("cases", []) - expected = {case["case_id"] for case in cases} - if len(records) != len(expected) or {r.get("case_id") for r in records} != expected: - raise ValueError("missing, duplicate or unknown VLESS native case") - by_id = {record["case_id"]: record for record in records} - output = [] - for case in cases: - record = by_id[case["case_id"]] - path = directory / (case["case_id"] + "-events.jsonl") - observed = read_events(path) if path.exists() else [] - test = NATIVE[case["case_id"]][3] - observed_ok = ( - events_pass(observed, test, NATIVE[case["case_id"]][1]) - if case["case_id"] in PUBLIC - else observed - == [ - dict( - schema_version=1, suite="VLESS-WIRE", assertion=test, status=status - ) - for status in ("BEGIN", "PASS") - ] - ) - clean = ( - record.get("command_cleanup") is True - and record.get("cleanup") is True - and report.get("cleanup") is True - ) - expected_command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public" if case["case_id"] in PUBLIC else "vless_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - good = ( - observed_ok - and clean - and report.get("source_unchanged") is True - and record.get("exit_code") == 0 - and record.get("status") == "PASS" - and record.get("command") == expected_command - and record.get("peer_kind") == case["peer_kind"] - ) - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions={test: observed_ok}, - evidence=case["required_evidence"] if good else [], - command_exit_code=record.get("exit_code"), - cleanup=clean, - ) - output.append(result) - return output - - -def fields_report(cases, results): - statuses = {result["case_id"]: result["status"] for result in results} - complete = set(statuses) == REQUIRED_IDS and all( - value == "PASS" for value in statuses.values() - ) - fields = [] - for row in sorted(FIELD_IDS): - owners = [c["case_id"] for c in cases if row in c["row_ids"]] - fields.append( - dict( - row_id=row, - required_cases=owners, - status="PASS" if complete and len(owners) > 1 else "NOT RUN", - ) - ) - return dict(schema_version=1, stage="VLESS", protocol="vless", fields=fields) - - -def execute(cases, run, output, results): - from .protocol_harness import _command, _features, _scripts - - for case in cases: - identifier = case["case_id"] - if identifier in NATIVE: - continue - if identifier == "VLESS-FEATURES": - result = _features(case, run, output) - independent = _command( - run, - output, - "feature-outbound-vless", - [ - "cargo", - "check", - "--locked", - "--no-default-features", - "--lib", - "--features", - "outbound-vless", - ], - 1200, - ) - if independent.returncode or not independent.cleanup: - result.update( - status="FAIL", - command_exit_code=independent.returncode, - cleanup=independent.cleanup, - ) - build = _command( - run, - output, - "production-build", - [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ], - 1200, - ) - if build.returncode or not build.cleanup: - result.update( - status="FAIL", - command_exit_code=build.returncode, - cleanup=build.cleanup, - ) - results[identifier] = result - elif identifier == "VLESS-SCRIPTS": - results[identifier] = _scripts(case, run, output) - else: - events = [] - start = len(run["commands"]) - for index, argv in enumerate(commands(identifier)): - path = output / f"{identifier}-{index}-events.jsonl" - _command(run, output, f"{identifier}-{index}", argv, 1200, events=path) - if path.exists(): - events.extend(read_events(path)) - results[identifier] = gate_result(case, run["commands"][start:], events) - native = [case for case in cases if case["case_id"] in NATIVE] - if native: - directory = output / "native" - native_run(directory, [case["case_id"] for case in native]) - report = read_json(directory / "vless-results.json") - (output / "peers.json").write_text(json.dumps(report["peers"], indent=2) + "\n") - for result in native_results(native, report, directory): - results[result["case_id"]] = result - (output / "fields.json").write_text( - json.dumps(fields_report(cases, list(results.values())), indent=2) + "\n" - ) - - -def required_command_names(): - # The shared feature smoke includes VMess; VLESS separately adds VLESS. - return { - f"{identifier}-{i}" - for identifier in GATES - for i in range(len(commands(identifier))) - } | { - "production-build", - "offline-scripts", - "feature-default", - "feature-minimal", - "feature-outbound-vless", - *( - "feature-" + name - for name in ( - "outbound-trojan", - "outbound-vmess", - "outbound-hysteria2", - "stream-transport", - "quic-transport", - ) - ), - } - - -def check(run_dir, cases, run, results, peers, paths): - from .protocol_harness import SCRIPT_OBSERVATIONS - - needed = { - "native/vless-results.json", - "fields.json", - "script-tests.json", - "resources.jsonl", - "peers.json", - "cases.json", - "summary.md", - } - if not needed <= set(paths): - raise ValueError("missing VLESS acceptance artifacts") - expected = {c["case_id"]: c for c in definitions()} - if len(cases) != len(expected) or any( - c != expected.get(c["case_id"]) for c in cases - ): - raise ValueError("VLESS frozen case metadata changed") - report = read_json(run_dir / "native/vless-results.json") - source_keys = ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - if ( - report.get("source") != {key: run.get(key) for key in source_keys} - or report.get("status") != "PASS" - ): - raise ValueError("VLESS peer evidence input mismatch") - isolation = report.get("isolation", {}) - if ( - isolation.get("host_servers") is not False - or isolation.get("network_mode") != "hostOnly" - or not isolation.get("peers") - or not all( - p.get("joined") is True and p.get("started") is True - for p in isolation["peers"] - ) - ): - raise ValueError("VLESS peers not isolated or cleaned") - if peers != report.get("peers") or set(peers) != {"M", "V2"}: - raise ValueError("missing VLESS official peers") - for kind, peer in peers.items(): - project = "MetaCubeX/mihomo" if kind == "M" else "v2fly/v2ray-core" - if ( - peer.get("kind") != kind - or peer.get("target") != "linux-arm64" - or not peer.get("version") - or not peer.get("source_url", "").startswith( - f"https://github.com/{project}/releases/" - ) - or not all( - HEX.fullmatch(peer.get(key, "")) - for key in ("archive_sha256", "binary_sha256") - ) - ): - raise ValueError("invalid official VLESS peer identity") - actual = {r["case_id"]: r for r in results} - recalculated = native_results( - [c for c in cases if c["case_id"] in NATIVE], report, run_dir / "native" - ) - for case in cases: - identifier = case["case_id"] - argv = commands(identifier) - if not argv: - continue - records = [] - events = [] - for i, command in enumerate(argv): - matching = [r for r in run["commands"] if r["name"] == f"{identifier}-{i}"] - if len(matching) != 1 or matching[0]["command"] != [ - redact(part) for part in command - ]: - raise ValueError("missing VLESS gate command") - records.extend(matching) - path = run_dir / f"{identifier}-{i}-events.jsonl" - if path.exists(): - events.extend(read_events(path)) - recalculated.append(gate_result(case, records, events)) - for item in recalculated: - if item != actual.get(item["case_id"]): - raise ValueError("VLESS results disagree with structured observations") - names = [c["name"] for c in run["commands"]] - needed_commands = required_command_names() - if ( - len(names) != len(set(names)) - or set(names) != needed_commands - or any( - c.get("exit_code") != 0 - or c.get("cleanup") is not True - or c.get("log") not in paths - for c in run["commands"] - ) - ): - raise ValueError("missing or failed VLESS validation command") - for record in run["commands"]: - name = record["name"] - if name == "feature-default": - expected_command = [ - "cargo", - "test", - "--locked", - "--test", - "feature_foundations", - ] - elif name.startswith("feature-"): - expected_command = [ - "cargo", - "check", - "--locked", - "--no-default-features", - "--lib", - ] - if name != "feature-minimal": - expected_command += ["--features", name.removeprefix("feature-")] - elif name == "production-build": - expected_command = [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ] - elif name == "offline-scripts": - expected_command = [ - sys.executable, - "-m", - "vcore_scripts.protocol_script_tests", - str(run_dir / "script-tests.json"), - ] - else: - continue - if record["command"] != [redact(part) for part in expected_command]: - raise ValueError("VLESS feature or script command changed") - script = read_json(run_dir / "script-tests.json") - tests = {t["test"]: t["status"] for t in script["cases"]} - if ( - len(tests) != script["tests_run"] - or any(tests.get(name) != "PASS" for name in SCRIPT_OBSERVATIONS.values()) - or any(status != "PASS" for status in tests.values()) - ): - raise ValueError("missing harness failure-path tests") - fields = fields_report(cases, results) - if read_json(run_dir / "fields.json") != fields or any( - f["status"] != "PASS" for f in fields["fields"] - ): - raise ValueError("incomplete VLESS field coverage") - resources = read_events(run_dir / "resources.jsonl") - owned = [ - e - for e in resources - if e.get("suite") == "VLESS-OWNED" and e.get("status") == "PASS" - ] - if len(owned) != 80 or not all(idle_resources(e.get("resources")) for e in owned): - raise ValueError("missing 20-round owned resource evidence per selected mode") - print( - f"VLESS: PASS ({len(cases)} required cases; {len(FIELD_IDS)} VLESS field rows)" - ) diff --git a/scripts/src/vcore_scripts/protocol_vless_container.py b/scripts/src/vcore_scripts/protocol_vless_container.py deleted file mode 100644 index d91120c..0000000 --- a/scripts/src/vcore_scripts/protocol_vless_container.py +++ /dev/null @@ -1,759 +0,0 @@ -"""VLESS wire cases against official latest peers and entirely isolated origins.""" - -from __future__ import annotations - -import contextlib -import json -import os -import shutil -import ssl -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_release import download_mihomo -from .native_release import PeerArtifact, download_native -from .protocol_containers import ContainerLab, command -from .protocol_evidence import read_events -from .protocol_fixtures import certificates -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vless_peers import configuration -from .protocol_vless_public import PUBLIC as PUBLIC_CASES -from .protocol_vless_public import WIRE as CASES -from .protocol_vless_public import events_pass -from .protocol_vless_security import client_identities -from .protocol_vmess import peer_config as legacy_peer -from .protocol_vmess_public import node_config as legacy_node - -ALL_CASES = CASES | PUBLIC_CASES -CLOSE_TESTS = { - "native_mihomo_close_alignment", - "native_ws_reality_close_boundary", - "native_vision_direct_close_alignment", -} -CLIENT_FINGERPRINTS = ( - "none", - "chrome", - "chrome120", - "firefox", - "firefox120", - "safari", - "safari16", -) - - -def close_reference(mode, node, config, certificate, private_key, pin): - reference = dict(node, name="reference") - if reference.get("network") == "ws": - options = dict(reference.get("ws-opts", {})) - if options.get("max-early-data", 0) > 0: - # VCore defaults ED to this header; Mihomo's raw option defaults - # to path ED. Explicit empty/custom values must stay unchanged. - options.setdefault("early-data-header-name", "Sec-WebSocket-Protocol") - reference["ws-opts"] = options - scope = "same-mode" - if mode.endswith("-reality") and reference.get("network") == "ws": - # Mihomo's WS client never consumes RealityOpts. Its REALITY listener - # does support WS, so preserve VCore's real peer and compare the WS - # close layer with a separate TLS entrance. Never call this same-mode. - baseline = json.loads(json.dumps(config["listeners"][0])) - baseline.pop("reality-config") - baseline.update( - name="vless-ws-close-baseline", - port=23003, - certificate=str(certificate), - **{"private-key": str(private_key)}, - ) - config["listeners"].append(baseline) - reference.pop("reality-opts") - reference.update(port=23003, fingerprint=pin) - scope = "ws-standard-tls-baseline" - elif mode.endswith("-reality") and reference.get("client-fingerprint") in ( - None, - "", - "none", - ): - reference["client-fingerprint"] = "chrome" - elif ( - reference.get("jls-opts") - and reference.get("network") == "grpc" - and reference.get("client-fingerprint") in (None, "", "none") - ): - # Official Mihomo v1.19.31's gRPC ALPN reader does not recognize the - # ordinary jls-tls ConnectionState type. uTLS JLS does work. Keep the - # VCore node unchanged and label this different-profile close baseline. - reference["client-fingerprint"] = "chrome" - scope = "jls-grpc-chrome-baseline" - elif ( - reference.get("network") == "grpc" - and reference.get("ech-opts", {}).get("enable") is True - and reference.get("client-fingerprint") in ("safari", "safari16") - ): - # Official Mihomo v1.19.31/uTLS rejects Safari ECH before business - # data with "malformed outer client hello". Compare the same ECH - # listener's transport close using Chrome; the VCore node stays Safari. - reference["client-fingerprint"] = "chrome" - scope = "ech-safari-chrome-baseline" - return reference, scope - - -def run( - output: Path, - selected=None, - *, - preflight_only=False, - client_fingerprint=None, - encryption=None, - jls=False, - ech=False, - supplied=None, -): - if client_fingerprint is not None and client_fingerprint not in CLIENT_FINGERPRINTS: - raise ValueError("unsupported named client profile") - public_cases = PUBLIC_CASES | ( - { - "FINGERPRINT-ANYTLS": ("M", "anytls", True, "public_legacy_regression"), - **{ - f"FINGERPRINT-VMESS-{network.upper()}-TLS": ( - "V2", - f"vmess-{network}", - True, - "public_legacy_tcp", - ) - for network in ("http", "h2") - }, - } - if client_fingerprint - else {} - ) - all_cases = CASES | public_cases - if encryption is not None: - from .protocol_encryption import cases as encryption_profiles - from .protocol_encryption_public import catalog - - if encryption not in encryption_profiles(): - raise ValueError("unsupported Encryption fixture") - all_cases = catalog() - public_cases = { - key: value - for key, value in all_cases.items() - if not value[3].startswith("native_") - } - if jls: - from .protocol_jls import catalog - - all_cases = catalog() - public_cases = { - key: value - for key, value in all_cases.items() - if not value[3].startswith("native_") - } - if ech: - if jls: - raise ValueError("ECH and JLS are mutually exclusive") - from .protocol_ech import catalog - - all_cases = catalog() - public_cases = { - key: value - for key, value in all_cases.items() - if not value[3].startswith("native_") - } - selected = list(all_cases) if selected is None else selected - if ( - not selected - or len(set(selected)) != len(selected) - or not set(selected) <= all_cases.keys() - ): - raise ValueError("invalid VLESS native selection") - if client_fingerprint and any(not all_cases[case][2] for case in selected): - raise ValueError("named client profiles require TLS cases") - output.mkdir(parents=True, exist_ok=False) - if preflight_only: - # One listener/origin group per official implementation, not a traffic run. - kinds = {all_cases[case][0] for case in selected} - selected = [ - next(case for case in selected if all_cases[case][0] == kind) - for kind in sorted(kinds) - ] - report = dict( - stage="SECURITY.ech" - if ech - else "SECURITY.jls" - if jls - else "SECURITY.encryption" - if encryption - else "FINGERPRINT-INTEROP" - if client_fingerprint - else "VLESS", - jls=jls, - ech=ech, - encryption_profile=encryption, - client_fingerprint=client_fingerprint, - scope="container-wire-and-public-consumer", - source=source_identity(), - status="NOT RUN", - phase="preflight", - cases=[], - peers={}, - isolation={}, - ) - try: - artifacts = {} - for kind in sorted({all_cases[case][0] for case in selected} | {"M"}): - directory = output / "binaries" / kind - if supplied is not None: - artifacts[kind] = PeerArtifact(*supplied[kind]) - elif kind == "M": - identity = {} - binary = download_mihomo( - "linux-arm64", directory=directory, identity=identity - ) - artifacts[kind] = PeerArtifact(binary, identity) - else: - artifacts[kind] = download_native( - kind, directory, "linux-arm64", defer_version=True - ) - report["peers"][kind] = artifacts[kind].identity - lab = ContainerLab(report["isolation"], mtu=1500) - report["phase"] = "build" - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_native", - "--test", - "vless_public", - "--no-run", - ], - timeout=180, - ) - (output / "build.log").write_text(redact(built.stdout.decode(errors="replace"))) - if built.returncode != 0 or not built.cleanup: - raise RuntimeError("container wire build failed") - # Reuse a peer only within one transport; every case gets fresh origin - # controls/ports and sessions. One downloaded release snapshot per run. - groups = {} - for case in selected: - kind, mode, encrypted, test = all_cases[case] - groups.setdefault((kind, mode, encrypted), []).append((case, test)) - for (kind, mode, encrypted), cases in groups.items(): - report["phase"] = "peer-start" - with tempfile.TemporaryDirectory( - prefix="private-", dir=output - ) as temporary: - root = Path(temporary) - with contextlib.ExitStack() as stack: - origin_dir, server_dir = root / "origin", root / "server" - origin_dir.mkdir() - server_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - shutil.copyfile( - Path(__file__).with_name("container_close_client.py"), - origin_dir / "close.py", - ) - shutil.copyfile( - Path(__file__).with_name("container_tls_bio.py"), - origin_dir / "tls_bio.py", - ) - artifact = artifacts[kind] - shutil.copy2(artifact.binary, server_dir / "peer") - tag = f"{mode}-{'tls' if encrypted else 'plain'}" - origin_cert, origin_key, origin_pin = certificates(origin_dir) - origin = lab.start( - stack, - origin_dir, - tag + "-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - f"VCORE_ORIGIN_CERT=/data/fixture/{origin_cert.name}", - f"VCORE_ORIGIN_KEY=/data/fixture/{origin_key.name}", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - argv = ( - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ] - ) - server = lab.start(stack, server_dir, tag + "-server", argv) - - def retain_logs(origin=origin, server=server, tag=tag): - for role, peer in (("origin", origin), ("server", server)): - if peer.log.exists(): - (output / f"{tag}-{role}.log").write_text( - redact( - # Capture before cleanup: live log - # following may buffer short messages. - command("logs", peer.name)[-65536:] - ) - ) - - stack.callback(retain_logs) - version = command( - "exec", - server.name, - "/data/fixture/peer", - "-v" if kind == "M" else "version", - ).strip() - digest = command( - "exec", server.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if not version or digest != artifact.identity["binary_sha256"]: - raise RuntimeError("container peer identity mismatch") - artifact.identity["version"] = version - # Non-leaf pin still exercises actual certificate name checks. - from .protocol_fixtures import certificate_chain - - cert, key, pin = ( - certificate_chain(server_dir) - if encrypted - else certificates(server_dir) - ) - identities = {} - if mode == "anytls": - node = dict( - name="peer", - type="anytls", - server=server.ipv4, - port=23000, - password="fixture", - sni="localhost", - udp=True, - fingerprint=pin, - ) - config = { - "listeners": [ - { - "name": "anytls", - "type": "anytls", - "listen": "::", - "port": 23000, - "users": {"fixture": "fixture"}, - "certificate": f"/data/fixture/{cert.name}", - "private-key": f"/data/fixture/{key.name}", - } - ], - "ipv6": True, - "log-level": "silent", - "hosts": {"vcore-fixture.test": origin.ipv4}, - "rules": ["MATCH,DIRECT"], - } - elif mode.startswith(("vmess-", "trojan-")): - legacy_mode = mode.removeprefix("vmess-") - node = legacy_node(legacy_mode, True, server.ipv4, pin) - config = legacy_peer( - kind, - legacy_mode, - True, - 23000, - Path("/data/fixture") / cert.name, - Path("/data/fixture") / key.name, - ) - if kind == "M": - config["hosts"] = {"vcore-fixture.test": origin.ipv4} - config["listeners"][0]["listen"] = "::" - else: - config["dns"]["hosts"] = {"vcore-fixture.test": origin.ipv4} - config["inbounds"][0]["listen"] = "::" - else: - node, config = configuration( - mode.removesuffix("-h1") if jls or ech else mode, - server.ipv4, - origin.ipv4, - Path("/data/fixture") / cert.name, - Path("/data/fixture") / key.name, - ) - node["name"] = "peer" - if encrypted and not mode.endswith("-reality"): - node["fingerprint"] = pin - if mode.endswith("-mtls"): - identities = client_identities(server_dir) - node.update(identities["valid"]) - config["listeners"][0].update( - { - "client-auth-type": "require-and-verify", - "client-auth-cert": "/data/fixture/root.pem", - } - ) - if client_fingerprint: - node["client-fingerprint"] = client_fingerprint - extra_fixture = {} - if ech: - from .protocol_ech import configuration as ech_config - - extra_fixture = ech_config(node, config, server_dir, mode) - if jls: - from .protocol_jls import configuration as jls_config - - extra_fixture = jls_config(node, config, origin.ipv4, mode) - if encryption: - from .protocol_encryption_public import ( - configuration as encrypted_config, - ) - - encrypted_config(encryption, node, config) - if mode == "vision-encryption": - node.pop("tls", None) - node.pop("servername", None) - for listener in config["listeners"]: - listener.pop("certificate", None) - listener.pop("private-key", None) - listener["allow-insecure"] = True - reference_node = None - reference_scope = None - if any(test in CLOSE_TESTS for _, test in cases): - reference_node, reference_scope = close_reference( - mode, - node, - config, - Path("/data/fixture") / cert.name, - Path("/data/fixture") / key.name, - pin, - ) - (server_dir / "config.json").write_text(json.dumps(config)) - upstream = None - hop = None - if any(case in public_cases for case, _ in cases): - upstream_dir = root / "upstream" - upstream_dir.mkdir() - shutil.copy2(artifacts["M"].binary, upstream_dir / "peer") - upstream = lab.start( - stack, - upstream_dir, - tag + "-upstream", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - (upstream_dir / "config.json").write_text( - json.dumps( - { - "socks-port": 23001, - "allow-lan": True, - "bind-address": "*", - "log-level": "silent", - "ipv6": True, - "hosts": {"peer.fixture.test": server.ipv4}, - "rules": ["MATCH,DIRECT"], - } - ) - ) - hop = dict( - name="hop", - type="socks5", - server=upstream.ipv4, - port=23001, - udp=True, - ) - upstream.release() - upstream.wait_tcp(23001) - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - server_ipv6=server.ipv6, - data_dir=str(root / "data"), - node=node, - vision_probe=mode.startswith("vision-"), - flow_reject_port=23004 - if mode.startswith("vision-") - else None, - origin_pin=origin_pin, - origin_root_der=list( - ssl.PEM_cert_to_DER_cert(origin_cert.read_text()) - ), - identities=identities, - root_der=list( - ssl.PEM_cert_to_DER_cert( - ( - server_dir / "root.pem" - if encrypted - else cert - ).read_text() - ) - ), - hop=hop, - peer_kind=kind, - **extra_fixture, - ) - ) - ) - origin.release() - server.release() - origin.wait_tcp(24000) - if mode.endswith("-reality") or jls: - origin.wait_tcp(24001) - server.wait_tcp(23000) - comparison = None - if reference_node is not None: - comparison_dir = root / "comparison" - comparison_dir.mkdir() - shutil.copy2(artifacts["M"].binary, comparison_dir / "peer") - (comparison_dir / "config.json").write_text( - json.dumps( - { - "socks-port": 23002, - "allow-lan": True, - "bind-address": "*", - "log-level": "warning", - "ipv6": True, - "proxies": [reference_node], - "rules": ["MATCH,reference"], - } - ) - ) - comparison = lab.start( - stack, - comparison_dir, - tag + "-comparison", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - comparison.release() - comparison.wait_tcp(23002) - stack.callback( - lambda peer=comparison, tag=tag: ( - output / f"{tag}-comparison.log" - ).write_text(redact(command("logs", peer.name)[-65536:])) - ) - observed_close = run_command( - [ - "container", - "exec", - origin.name, - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/close.py", - comparison.ipv4, - origin.ipv4, - ], - timeout=30, - ) - (output / f"{tag}-close-command.log").write_text( - redact(observed_close.stdout.decode(errors="replace")) - ) - if observed_close.returncode or not observed_close.cleanup: - raise RuntimeError("official close comparison failed") - reference = json.loads(observed_close.stdout) - reference["scope"] = reference_scope - reference["client_fingerprint"] = reference_node.get( - "client-fingerprint" - ) - reference["dut_client_fingerprint"] = node.get( - "client-fingerprint" - ) - (output / f"{tag}-close-reference.json").write_text( - json.dumps(reference) + "\n" - ) - data = json.loads(fixture.read_text()) - data["close_reference"] = reference - if any( - test == "native_vision_direct_close_alignment" - for _, test in cases - ): - direct = run_command( - [ - "container", - "exec", - origin.name, - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/close.py", - comparison.ipv4, - origin.ipv4, - "vision-direct", - ], - timeout=30, - ) - (output / f"{tag}-direct-close-command.log").write_text( - redact(direct.stdout.decode(errors="replace")) - ) - if direct.returncode or not direct.cleanup: - raise RuntimeError( - "official direct close comparison failed" - ) - direct_reference = json.loads(direct.stdout) - direct_reference["scope"] = reference_scope - (output / f"{tag}-direct-close-reference.json").write_text( - json.dumps(direct_reference) + "\n" - ) - data["direct_close_reference"] = direct_reference - fixture.write_text(json.dumps(data)) - report["phase"] = "execute" - for case, test in cases: - if preflight_only: - continue - print(case, flush=True) - record = dict(case_id=case, status="NOT RUN", peer_kind=kind) - report["cases"].append(record) - events = output / f"{case}-events.jsonl" - test_command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public" if case in public_cases else "vless_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - result = run_command( - test_command, - cwd=CORE_DIR, - timeout=240, - limit=4 * 1024 * 1024, - env=dict( - os.environ, - VCORE_VLESS_PEER=f"{server.ipv4}:23000", - VCORE_VLESS_TRANSPORT=json.dumps( - dict( - mode=mode, - tls=encrypted, - pin=pin, - peer_kind=kind, - udp_path_limit=15000, - ) - ), - VCORE_VLESS_ORIGIN_V4=origin.ipv4, - VCORE_VLESS_INPUT=str(fixture), - VCORE_CASE_EVENTS=str(events), - VCORE_PROTOCOL_STAGE="SECURITY" - if encryption or jls or ech - else "VLESS", - ), - ) - (output / f"{case}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = read_events(events) if events.exists() else [] - record.update( - command=test_command, - exit_code=result.returncode, - seconds=result.seconds, - command_cleanup=result.cleanup, - status="PASS" - if result.returncode == 0 - and result.cleanup - and ( - events_pass( - observed, - test, - mode, - stage="SECURITY" - if encryption or jls or ech - else "VLESS", - ) - if case in public_cases - else observed - == [ - dict( - schema_version=1, - suite="SECURITY-WIRE" - if encryption or jls or ech - else "VLESS-WIRE", - assertion=test, - status=status, - ) - for status in ("BEGIN", "PASS") - ] - ) - else "FAIL", - ) - server.ensure_alive() - origin.ensure_alive() - print(f"{case}: {record['status']}", flush=True) - for case, _ in cases: - if preflight_only: - continue - next(item for item in report["cases"] if item["case_id"] == case)[ - "cleanup" - ] = all( - peer.record["joined"] - for peer in [server, origin] + ([upstream] if upstream else []) - ) - report["status"] = ( - "READY" - if preflight_only - else "PASS" - if (preflight_only or len(report["cases"]) == len(selected)) - and all(case["status"] == "PASS" for case in report["cases"]) - else "FAIL" - ) - except KeyboardInterrupt: - report.update(status="INTERRUPTED", reason="user interruption") - except (OSError, RuntimeError, ValueError, KeyError) as error: - report.update( - status="BLOCKED" if report["phase"] == "preflight" else "FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - finally: - report["cleanup"] = all( - peer["joined"] for peer in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - if report["status"] in {"PASS", "READY"} and not ( - report["cleanup"] and report["source_unchanged"] - ): - report["status"] = "FAIL" - (output / "vless-results.json").write_text(json.dumps(report, indent=2) + "\n") - return ( - 0 - if report["status"] in {"PASS", "READY"} - else (130 if report["status"] == "INTERRUPTED" else 1) - ) - - -if __name__ == "__main__": - import sys - - from .mihomo_isolation import exclusive_run - - with exclusive_run(): - sys.exit(run(Path(sys.argv[1]).resolve(), sys.argv[2:] or None)) diff --git a/scripts/src/vcore_scripts/protocol_vless_peers.py b/scripts/src/vcore_scripts/protocol_vless_peers.py deleted file mode 100644 index 236aee9..0000000 --- a/scripts/src/vcore_scripts/protocol_vless_peers.py +++ /dev/null @@ -1,219 +0,0 @@ -"""VLESS isolated native fixtures. No host service or reference checkout is used.""" - -from pathlib import Path - -MODES = [ - mode + suffix - for mode in ( - "tcp", - "ws", - "ws-ed-1", - "ws-ed-2048", - "upgrade", - "upgrade-fast", - "grpc", - "http", - "h2", - "ws-header", - "ws-path", - ) - for suffix in ("", "-tls") -] + [ - "xhttp-stream-one", - "xhttp-stream-up", - "xhttp-packet-up", - "vision-tls", - "vision-reality", - "tcp-reality", - "ws-reality", - "grpc-reality", - "upgrade-reality", - "upgrade-fast-reality", - "tcp-mtls", - "ws-mtls", - "grpc-mtls", - "xhttp-stream-one-reality", - "xhttp-stream-up-reality", - "xhttp-packet-up-reality", - "xhttp-stream-up-download", - "xhttp-packet-up-download", - "xhttp-stream-up-download-reality", - "xhttp-packet-up-download-reality", - "grpc-path-tls", -] - - -def peer_kind(mode): - base = mode.removesuffix("-tls").removesuffix("-reality").removesuffix("-mtls") - return "V2" if base in {"http", "h2", "ws-header", "ws-path"} else "M" - - -def configuration(mode, server, origin, cert: Path, key: Path): - encrypted = mode.endswith(("-tls", "-reality", "-mtls")) or mode.startswith( - ("xhttp-", "vision-") - ) - base = mode.removesuffix("-tls").removesuffix("-reality").removesuffix("-mtls") - separate_download = base.endswith("-download") - base = base.removesuffix("-download") - kind = peer_kind(mode) - identity = "07070707-0707-0707-0707-070707070707" - node = dict( - name="edge", type="vless", server=server, port=23000, uuid=identity, udp=True - ) - if encrypted: - node.update(tls=True, servername="localhost") - if base.startswith("xhttp-"): - node.update( - network="xhttp", - **{ - "xhttp-opts": dict( - path="/vless", host="localhost", mode=base.removeprefix("xhttp-") - ) - }, - ) - if separate_download: - node["xhttp-opts"]["download-settings"] = {} - elif base.startswith(("ws", "upgrade")): - ws = dict(path="/vless-ws", headers={"Host": "localhost"}) - if base in {"ws-header", "ws-path"}: - ws.update( - path="/vless-ws/", - **{ - "max-early-data": 2048, - "early-data-header-name": "X-Vcore-Ed" - if base == "ws-header" - else "", - }, - ) - elif base in {"ws-ed-1", "ws-ed-2048"}: - ws["max-early-data"] = int(base.removeprefix("ws-ed-")) - elif base.startswith("upgrade"): - ws.update( - **{ - "v2ray-http-upgrade": True, - "v2ray-http-upgrade-fast-open": base == "upgrade-fast", - "max-early-data": 1, - } - ) - node.update(network="ws", **{"ws-opts": ws}) - elif base in {"grpc", "grpc-path"}: - node.update( - network="grpc", - **{ - "grpc-opts": dict( - **{ - "grpc-service-name": "/vless-grpc/Tun" - if base == "grpc-path" - else "vless-grpc", - "grpc-user-agent": "VCore-VLESS", - "ping-interval": 1, - } - ) - }, - ) - elif base == "http": - node.update( - network="http", - **{ - "http-opts": dict( - method="POST", path=["/vless-http"], headers={"Host": ["localhost"]} - ) - }, - ) - elif base == "h2": - node.update( - network="h2", **{"h2-opts": dict(host=["localhost"], path="/vless-h2")} - ) - if kind == "M": - listener = dict( - name="vless", - type="vless", - listen="::", - port=23000, - users=[dict(username="fixture", uuid=identity)], - ) - if mode.startswith("vision-"): - listener["users"][0]["flow"] = "xtls-rprx-vision" - node["flow"] = "xtls-rprx-vision" - if encrypted: - listener.update(certificate=str(cert), **{"private-key": str(key)}) - else: - listener["allow-insecure"] = True - if mode.endswith("-reality"): - from .mihomo_extended import PRIVATE_KEY, PUBLIC_KEY, SHORT_ID - - listener.pop("certificate", None) - listener.pop("private-key", None) - listener["reality-config"] = { - "dest": f"{origin}:24001", - "private-key": PRIVATE_KEY, - "short-id": [SHORT_ID], - "server-names": ["localhost"], - } - node["reality-opts"] = {"public-key": PUBLIC_KEY, "short-id": SHORT_ID} - if node.get("network") == "ws": - listener["ws-path"] = "/vless-ws" - if base in {"grpc", "grpc-path"}: - listener["grpc-service-name"] = "vless-grpc" - if base == "ws-alpn": - node["alpn"] = ["h2", "http/1.1"] - listener["grpc-service-name"] = "vless-alpn" - if base.startswith("xhttp-"): - listener["xhttp-config"] = dict(path="/vless", mode="auto") - listeners = [listener] - if mode.startswith("vision-"): - # Mihomo permits an explicit empty flow even for a Vision user. - # Rejection requires a Vision request against an ordinary user. - listeners.append( - dict( - listener, - name="vless-flow-rejection", - port=23004, - users=[dict(user, flow="") for user in listener["users"]], - ) - ) - return node, dict( - ipv6=True, - mode="rule", - **{"log-level": "silent"}, - hosts={"vcore-fixture.test": origin}, - listeners=listeners, - rules=["MATCH,DIRECT"], - ) - stream = dict(network="tcp", security="tls" if encrypted else "none") - if base == "http": - stream["tcpSettings"] = dict( - header=dict(type="http", request=dict(path=["/vless-http"])) - ) - elif base == "h2": - stream.update( - network="http", httpSettings=dict(host=["localhost"], path="/vless-h2") - ) - else: - stream.update( - network="ws", - wsSettings=dict( - path="/vless-ws/", - maxEarlyData=2048, - earlyDataHeaderName="X-Vcore-Ed" if base == "ws-header" else "", - ), - ) - if encrypted: - stream["tlsSettings"] = dict( - alpn=["h2"] if base == "h2" else ["http/1.1"], - certificates=[dict(certificateFile=str(cert), keyFile=str(key))], - ) - return node, dict( - log={"loglevel": "none"}, - dns={"hosts": {"vcore-fixture.test": origin}}, - inbounds=[ - dict( - listen="::", - port=23000, - protocol="vless", - settings=dict(clients=[dict(id=identity)], decryption="none"), - streamSettings=stream, - ) - ], - outbounds=[dict(protocol="freedom", settings={"domainStrategy": "UseIP"})], - ) diff --git a/scripts/src/vcore_scripts/protocol_vless_public.py b/scripts/src/vcore_scripts/protocol_vless_public.py deleted file mode 100644 index 6ee59ee..0000000 --- a/scripts/src/vcore_scripts/protocol_vless_public.py +++ /dev/null @@ -1,154 +0,0 @@ -"""Frozen VLESS native/public consumer requirements; no result-derived catalog.""" - -from .protocol_evidence import idle_resources -from .protocol_vless_peers import MODES, peer_kind -from .protocol_vmess_public import pairs - -PUBLIC = {} -WIRE = {} - - -def add(mode, suffix, test, *, wire=False): - kind = peer_kind(mode) - encrypted = mode.endswith(("-tls", "-reality", "-mtls")) or mode.startswith( - ("xhttp-", "vision-") - ) - (WIRE if wire else PUBLIC)[f"VLESS-{mode.upper()}-{suffix}"] = ( - kind, - mode, - encrypted, - test, - ) - - -for mode in MODES: - add(mode, "BASE", "public_base") -add("ws-alpn-tls", "NEG", "public_alpn_rejection") -for mode in ("tcp", "ws", "grpc", "http", "h2"): - for suffix in ("", "-tls"): - add(mode + suffix, "NEG", "public_negative") - for suffix, test in ( - ("GRAPH", "runtime::public_graph"), - ("IPV6", "runtime::public_ipv6_and_gates"), - ("ENTRYPOINTS", "runtime::public_entrypoints"), - ): - add(mode + "-tls", suffix, test) -for mode in ( - "upgrade-tls", - "upgrade-fast-tls", - "upgrade-reality", - "upgrade-fast-reality", - "tcp-reality", - "ws-reality", - "grpc-reality", - "vision-tls", - "vision-reality", - "xhttp-stream-one", - "xhttp-stream-up", - "xhttp-packet-up", -): - add(mode, "NEG", "public_negative") -for mode in ("tcp-tls", "grpc-tls", "vision-tls", "vision-reality"): - add(mode, "LIFE", "runtime::public_lifecycle") - add(mode, "OWNED", "runtime::owned_resources") -for mode in ("tcp-tls", "grpc-tls", "vision-tls"): - add(mode, "UDP-ISOLATION", "runtime::public_udp_isolation") -for mode in ("vision-tls", "vision-reality"): - add(mode, "INNER-TLS", "native_vision_inner_tls", wire=True) - add(mode, "GRAPH", "runtime::public_graph") - add(mode, "ENTRYPOINTS", "runtime::public_entrypoints") - add(mode, "IPV6", "runtime::public_ipv6_and_gates") -for mode in ("tcp", "grpc-tls", "http", "h2-tls"): - add(mode, "UDP-BOUNDARIES", "native_three_udp_encodings", wire=True) -for mode in ("tcp-mtls", "ws-mtls", "grpc-mtls"): - add(mode, "IDENTITY", "public_tls_identity_and_verification_name") -add( - "grpc-tls", - "POOL-SELECTION", - "runtime::grpc_pool_keeps_physical_selection_until_new_transport", -) -for protocol in ("vmess", "trojan"): - for network in ("tcp", "ws", "grpc"): - PUBLIC[f"VLESS-REGRESSION-{protocol.upper()}-{network.upper()}"] = ( - "M", - f"{protocol}-{network}", - True, - "public_legacy_regression", - ) -for mode in ( - "tcp", - "tcp-tls", - "tcp-reality", - "ws-reality", - "grpc-reality", - "ws", - "ws-tls", - "grpc", - "grpc-tls", - "http", - "http-tls", - "h2", - "h2-tls", - "upgrade-fast-tls", - "upgrade-reality", - "upgrade-fast-reality", - "vision-tls", - "vision-reality", - "xhttp-stream-one", - "xhttp-stream-up", - "xhttp-packet-up", -): - add( - mode, - "CLOSE", - "native_ws_reality_close_boundary" - if mode in {"ws-reality", "upgrade-reality", "upgrade-fast-reality"} - else "native_mihomo_close_alignment", - wire=True, - ) -add("grpc-tls", "POOL-COUNTS", "native_grpc_pool_thresholds", wire=True) -CASES = WIRE | PUBLIC - - -def events_pass(events, test, mode, *, stage="VLESS"): - main = [event for event in events if event.get("suite") == f"{stage}-PUBLIC"] - if len(main) != 2 or not pairs(main, f"{stage}-PUBLIC", test, 1): - return False - if any( - event.get("schema_version") != 1 or event.get("status") not in {"BEGIN", "PASS"} - for event in events - ): - return False - if test == "public_base" and not ( - pairs(events, f"{stage}-BASE", "tcp_10mib_both_directions", 3) - and pairs( - events, - f"{stage}-BASE", - "udp_each_codec_and_family", - 1 if mode.startswith("vision-") else 3, - ) - ): - return False - if test in {"runtime::public_lifecycle", "runtime::owned_resources"}: - suite = ( - f"{stage}-LIFE" if test.endswith("public_lifecycle") else f"{stage}-OWNED" - ) - if not pairs(events, suite, "stop_and_remain_quiet", 20): - return False - for event in [ - e for e in events if e.get("suite") == suite and e["status"] == "PASS" - ]: - if event.get("seconds", 0) < 5: - return False - if suite == f"{stage}-OWNED": - points = event.get("checkpoints", []) - phases = {p["phase"]: p["resources"] for p in points} - if ( - len(points) != 3 - or set(phases) != {"baseline", "after-stop", "quiet"} - or not all(idle_resources(r) for r in phases.values()) - or phases["after-stop"] != phases["quiet"] - or not idle_resources(event.get("resources")) - ): - return False - return True diff --git a/scripts/src/vcore_scripts/protocol_vless_security.py b/scripts/src/vcore_scripts/protocol_vless_security.py deleted file mode 100644 index acc6bd7..0000000 --- a/scripts/src/vcore_scripts/protocol_vless_security.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Ephemeral public-test identities. Keys never leave the private fixture.""" - -from pathlib import Path - -from .protocol_fixtures import certificates -from .protocol_peers import run_command - - -def client_identities(directory: Path): - result = {} - extensions = directory / "client-ext.cnf" - extensions.write_text( - "basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature\nextendedKeyUsage=clientAuth\n" - ) - (directory / "index.txt").write_text("") - (directory / "serial.txt").write_text("10\n") - ca = directory / "client-ca.cnf" - ca.write_text( - "[ca]\ndefault_ca=fixture\n[fixture]\n" - f"database={directory / 'index.txt'}\nserial={directory / 'serial.txt'}\n" - f"new_certs_dir={directory}\ncertificate={directory / 'root.pem'}\n" - f"private_key={directory / 'root-key.pem'}\n" - "default_md=sha256\npolicy=names\n[names]\ncommonName=supplied\n" - ) - for name, days in (("valid", "2"), ("expired", "2")): - key, csr, cert = ( - directory / f"client-{name}.{suffix}" for suffix in ("key", "csr", "pem") - ) - for argv in ( - [ - "openssl", - "req", - "-new", - "-newkey", - "rsa:2048", - "-nodes", - "-subj", - "/CN=synthetic-client", - "-keyout", - str(key), - "-out", - str(csr), - ], - [ - "openssl", - "x509", - "-req", - "-in", - str(csr), - "-CA", - str(directory / "root.pem"), - "-CAkey", - str(directory / "root-key.pem"), - "-set_serial", - "3" if name == "valid" else "4", - "-days", - days, - "-extfile", - str(extensions), - "-out", - str(cert), - ], - ): - if name == "expired" and argv[1] == "x509": - argv = [ - "openssl", - "ca", - "-batch", - "-notext", - "-config", - str(ca), - "-in", - str(csr), - "-out", - str(cert), - "-extfile", - str(extensions), - "-startdate", - "20000101000000Z", - "-enddate", - "20010101000000Z", - ] - command = run_command(argv, timeout=20, limit=65536) - if command.returncode or not command.cleanup: - raise RuntimeError("synthetic client identity generation failed") - result[name] = {"certificate": cert.read_text(), "private-key": key.read_text()} - wrong = directory / "wrong-ca" - wrong.mkdir() - cert, key, _ = certificates(wrong) - result["wrong-ca"] = { - "certificate": cert.read_text(), - "private-key": key.read_text(), - } - return result diff --git a/scripts/src/vcore_scripts/protocol_vmess.py b/scripts/src/vcore_scripts/protocol_vmess.py deleted file mode 100644 index 8723aea..0000000 --- a/scripts/src/vcore_scripts/protocol_vmess.py +++ /dev/null @@ -1,132 +0,0 @@ -"""VMESS wire gate; all peers and origins run in isolated containers.""" - -from __future__ import annotations - -import sys -from pathlib import Path - -from .mihomo_isolation import exclusive_run - -CASES = { - "VMESS-M-IDENTITY": ("M", "tcp", False, "native_identity_time_replay_rejection") -} -CASES["VMESS-M-RAW-UDP"] = ("M", "tcp", False, "native_raw_udp_boundaries") -CASES["VMESS-M-ENCODED-UDP"] = ("M", "tcp", False, "native_encoded_udp_boundaries") -for _mode in ("tcp", "ws", "grpc", "http", "h2"): - for _tls in (False, True): - _kind = "V2" if _mode in {"http", "h2"} else "M" - CASES[f"VMESS-{_kind}-{_mode.upper()}-{'TLS' if _tls else 'PLAIN'}"] = ( - _kind, - _mode, - _tls, - "native_cipher_matrix", - ) - CASES[f"VMESS-{_kind}-{_mode.upper()}-{'TLS' if _tls else 'PLAIN'}-CLOSE"] = ( - _kind, - _mode, - _tls, - "native_mihomo_close_alignment", - ) - if _mode != "tcp" or _tls: - for _encoding in ("raw", "encoded"): - CASES[ - f"VMESS-{_kind}-{_mode.upper()}-{'TLS' if _tls else 'PLAIN'}" - f"-{_encoding.upper()}-UDP" - ] = ( - _kind, - _mode, - _tls, - f"native_{_encoding}_udp_boundaries", - ) - - -def udp_path_limit(): - # Both packet sender and origin are now Linux VM services. - return 15000 - - -def peer_config(kind, mode, encrypted, port, cert, key): - if mode.startswith("trojan-"): - config = peer_config( - kind, mode.removeprefix("trojan-"), encrypted, port, cert, key - ) - config["listeners"][0].update( - type="trojan", - users=[dict(username="fixture", password="synthetic-regression-only")], - ) - return config - variant = mode - mode = mode.split("-")[0] - identity = "07070707-0707-0707-0707-070707070707" - if kind == "M": - listener = dict( - name="vmess", - type="vmess", - listen="127.0.0.1", - port=port, - users=[dict(username="fixture", uuid=identity, alterId=0)], - ) - if encrypted: - listener.update(certificate=str(cert), **{"private-key": str(key)}) - if mode == "ws": - listener["ws-path"] = "/vmess-ws" - if mode == "grpc": - listener["grpc-service-name"] = "vmess-grpc" - if variant == "ws-alpn": - listener["grpc-service-name"] = "vmess-alpn" - return { - "mode": "rule", - "log-level": "silent", - "ipv6": True, - "hosts": {"vcore-fixture.test": "127.0.0.1"}, - "listeners": [listener], - "rules": ["MATCH,DIRECT"], - } - stream = dict(network="tcp", security="tls" if encrypted else "none") - if mode == "http": - stream["tcpSettings"] = { - "header": {"type": "http", "request": {"path": ["/vmess-http"]}} - } - elif mode == "h2": - stream.update( - network="http", httpSettings=dict(host=["localhost"], path="/vmess-h2") - ) - elif mode == "ws": - stream.update( - network="ws", - wsSettings={ - "path": "/vmess-ws/", - "maxEarlyData": 2048, - "earlyDataHeaderName": "X-Vcore-Ed" if variant == "ws-header" else "", - }, - ) - if encrypted: - stream["tlsSettings"] = { - "alpn": ["h2"] if mode == "h2" else ["http/1.1"], - "certificates": [{"certificateFile": str(cert), "keyFile": str(key)}], - } - return { - "log": {"loglevel": "none"}, - "dns": {"hosts": {"vcore-fixture.test": "127.0.0.1"}}, - "inbounds": [ - { - "listen": "127.0.0.1", - "port": port, - "protocol": "vmess", - "settings": {"clients": [{"id": identity, "alterId": 0}]}, - "streamSettings": stream, - } - ], - "outbounds": [{"protocol": "freedom", "settings": {"domainStrategy": "UseIP"}}], - } - - -def run(output: Path, selected=None): - from .protocol_vmess_container import run as run_container - - return run_container(output, selected) - - -if __name__ == "__main__": - with exclusive_run(): - sys.exit(run(Path(sys.argv[1]).resolve(), sys.argv[2:] or None)) diff --git a/scripts/src/vcore_scripts/protocol_vmess_acceptance.py b/scripts/src/vcore_scripts/protocol_vmess_acceptance.py deleted file mode 100644 index d3bfdec..0000000 --- a/scripts/src/vcore_scripts/protocol_vmess_acceptance.py +++ /dev/null @@ -1,691 +0,0 @@ -"""VMESS container-only execution and independently recomputed stage acceptance.""" - -from __future__ import annotations - -import json -import sys - -from .builds import DEFAULT_FEATURES -from .protocol_evidence import HEX, idle_resources, new_result, read_events, read_json -from .protocol_inputs import redact -from .protocol_vmess import CASES as WIRE -from .protocol_vmess_container import run as native_run -from .protocol_vmess_public import CASES as PUBLIC -from .protocol_vmess_public import events_pass - -NATIVE = WIRE | PUBLIC -FIELD_IDS = { - *(f"C{i:02}" for i in range(1, 7)), - *(f"T{i:02}" for i in range(2, 6)), - *(f"W{i:02}" for i in range(1, 6)), - "G01", - *(f"H{i:02}" for i in range(1, 7)), - *(f"VM{i:02}" for i in range(1, 9)), -} -GATES = { - "VMESS-CFG", - "VMESS-CODEC", - "VMESS-CANCEL", - "VMESS-REGRESSION", - "VMESS-RELEASE", - "VMESS-FEATURES", - "VMESS-SCRIPTS", - "VMESS-QUALITY", -} -SCRIPT_ASSERTIONS = [ - "offline-success", - "missing-duplicate-unknown-case", - "download-failure", - "timeout", - "sigint", - "partial-start", - "permission", - "peer-exit", - "cleanup-failure", - "redaction", -] -REQUIRED_IDS = set(NATIVE) | GATES -# Frozen names, not a regex over test output and not read from the manifest. -OBSERVATIONS = { - "VMESS-CFG": [ - "vmess_default_node_and_explicit_cipher_aliases_are_accepted_without_io", - "vmess_transport_and_security_combinations_are_strict", - "vmess_field_boundaries_and_normalized_transport_values", - "vmess_normalized_websocket_fields_preserve_headers_and_early_data_order", - ], - "VMESS-CODEC": [ - "xudp_cancelled_send_and_bad_frame_release_io_and_cannot_resume", - "xudp_zero_global_id_omits_the_optional_extension_like_mihomo", - "packetaddr_has_ip_only_address_first_wire_and_rejects_truncation", - "vmess_aead_requests_are_fresh_bounded_and_redacted", - "vmess_response_authentication_cannot_be_skipped_by_none_cipher", - "vmess_whole_close_wakes_reader_and_releases_io_before_response", - "xudp_explicit_budget_rejects_max_plus_one_before_writing", - "independently_published_nested_hmac_vectors", - "chunks_preserve_boundaries_masks_padding_and_authenticated_eof", - "tags_lengths_and_nonce_exhaustion_fail_closed", - "auto_uses_detected_hardware_not_architecture_name", - ], - "VMESS-CANCEL": [ - "vmess_all_handshakes_keep_the_original_deadline_and_join_cancelled_io", - "vmess_expired_deadline_and_protect_failure_never_fall_back", - ], - "VMESS-REGRESSION": [ - "shared_xudp_receives_frames_without_a_vless_response_header", - "first_frame_matches_xray_mux_wire_format", - "followup_frame_carries_each_datagrams_destination", - "destination_codec_round_trips_all_address_families", - "normal_and_error_end_frames_are_distinguishable", - "oversized_response_is_rejected_before_payload_read", - "fragmented_receive_survives_cancellation_and_resumes", - "keepalive_data_is_consumed_without_becoming_an_udp_response", - "full_wire_payload_remains_available_to_proxy_inbounds", - "request_matches_the_official_sha224_and_socks_address_wire_format", - "authentication_and_requests_are_strict_and_never_debug_credentials", - "invalid_truncated_and_oversized_frames_fail_closed_without_payload_leaks", - "datagram_limits_preserve_messages_and_drain_over_budget_responses", - "datagram_sends_one_frame_and_receives_fragmented_consecutive_frames", - "cancelled_receive_preserves_partial_frames_and_does_not_block_send", - "cancelled_partial_send_poison_closes_without_replaying_or_waiting", - "trojan_tcp_configuration_and_node_graph_accept_the_approved_fields", - "trojan_tcp_defaults_preserve_credentials_and_address_policy", - "trojan_invalid_configuration_is_rejected_without_exposing_credentials", - "trojan_ws_and_grpc_configuration_applies_transport_specific_defaults", - "trojan_transport_boundaries_fail_before_runtime_io", - "transport_options_reject_ambiguous_headers_before_io", - "http_first_header_preserves_prefix_raw_continuation_and_half_close_tail", - "legacy_h2_keeps_unframed_bytes_server_first_and_owned_whole_close", - "websocket_rejects_invalid_upgrade_responses_and_bounded_header_overflows", - "websocket_early_data_and_remaining_frames_keep_the_original_byte_order", - "setup_deadline_releases_supplied_io_in_stream_adapters", - "cancelled_setup_releases_supplied_io_without_a_detached_task", - "ws_rejects_oversized_and_truncated_frames_instead_of_reporting_eof", - "ws_accepts_mihomo_clean_underlay_eof_only_at_complete_message_boundaries", - "dropping_an_established_ws_releases_its_only_io_owner", - "grpc_rejects_oversized_truncated_and_invalid_records", - "grpc_large_writes_obey_small_http2_windows_and_keep_byte_integrity", - "ws_partial_writes_empty_frames_ping_and_half_close_preserve_every_byte", - "grpc_handles_response_after_upload_fragmented_records_and_owned_stop", - "websocket_supplied_io_preserves_server_first_and_partial_writes", - "xhttp_keeps_complete_response_when_peer_resets_after_end_stream", - "xhttp_does_not_hide_reset_before_end_stream", - "feature_skeletons_do_not_open_unimplemented_yaml_or_measurement_protocols", - "future_fields_and_over_limit_documents_remain_rejected", - "registry_matches_live_production_constants_and_has_owned_boundary_cases", - "webpki_rejects_untrusted_wrong_name_and_expired_unless_explicitly_skipped", - "leaf_pin_is_trust_but_nonleaf_pin_checks_chain_name_and_expiry", - "ticket_storage_obeys_exact_node_budget_and_consumes_each_ticket_once", - "every_certificate_policy_verifies_tls12_and_tls13_handshake_signatures", - "vless_keeps_webpki_tls13_and_required_h2_after_anytls_connections", - "alpn_and_tls_resumption_are_isolated_between_node_policies", - "explicit_verification_name_does_not_change_sni_or_allow_skip_to_override_it", - "mutual_tls_identity_is_required_verified_and_not_shared_between_clients", - "tls_options_reject_invalid_alpn_identity_and_budget_before_using_a_stream", - "certificate_rejection_delivers_no_business_bytes_and_diagnostics_are_redacted", - "tls_close_write_sends_notify_without_closing_the_supplied_transport", - "tls_close_notify_flush_has_a_five_second_bound", - "cancelling_tls_handshake_releases_the_caller_supplied_stream", - ], - "VMESS-RELEASE": [ - "vmess_default_node_and_explicit_cipher_aliases_are_accepted_without_io", - "vmess_transport_and_security_combinations_are_strict", - "vmess_field_boundaries_and_normalized_transport_values", - "vmess_normalized_websocket_fields_preserve_headers_and_early_data_order", - "xudp_cancelled_send_and_bad_frame_release_io_and_cannot_resume", - "xudp_zero_global_id_omits_the_optional_extension_like_mihomo", - "packetaddr_has_ip_only_address_first_wire_and_rejects_truncation", - "vmess_aead_requests_are_fresh_bounded_and_redacted", - "vmess_response_authentication_cannot_be_skipped_by_none_cipher", - "vmess_whole_close_wakes_reader_and_releases_io_before_response", - "xudp_explicit_budget_rejects_max_plus_one_before_writing", - "independently_published_nested_hmac_vectors", - "chunks_preserve_boundaries_masks_padding_and_authenticated_eof", - "tags_lengths_and_nonce_exhaustion_fail_closed", - "auto_uses_detected_hardware_not_architecture_name", - "vmess_all_handshakes_keep_the_original_deadline_and_join_cancelled_io", - "vmess_expired_deadline_and_protect_failure_never_fall_back", - ], -} - - -def rows(identifier): - if identifier == "VMESS-CFG": - return sorted(FIELD_IDS) - if identifier == "VMESS-CODEC": - return ["VM01", "VM03", "VM06", "VM07", "VM08"] - if identifier in GATES or identifier.startswith("VMESS-REGRESSION"): - return [] - _, mode, tls, test = NATIVE[identifier] - result = {"VM04", "VM05"} - if identifier in WIRE: - if test == "native_identity_time_replay_rejection": - result |= {"VM01", "VM02"} - elif test == "native_cipher_matrix": - result |= {"VM03", "VM07", "VM08"} - elif test.endswith("udp_boundaries"): - result.add("VM06") - else: - result |= {"C01", "C02", "C03", "C04", "VM01", "VM02"} - if test == "public_body_options": - result |= {"VM03", "VM07", "VM08"} - if "graph" in test: - result.add("C06") - if "base" in test or "udp" in test or "gates" in test: - result |= {"C05", "VM06"} - if tls: - result |= {"T02", "T03", "T05"} - if test == "public_negative": - result.add("T04") - if mode.startswith("ws"): - result |= {"W01", "W02", "W03"} - if mode != "ws": - result |= {"W04", "W05"} - if mode.startswith("grpc"): - result.add("G01") - if mode == "http": - result |= {"H01", "H02", "H03", "H04"} - if mode == "h2": - result |= {"H05", "H06"} - return sorted(result) - - -def definitions(): - cases = [] - for identifier in sorted(REQUIRED_IDS): - kind, mode, tls, test = NATIVE.get( - identifier, ("unit", "memory-or-build", False, identifier) - ) - native = identifier in NATIVE - substage = "VMESS.acceptance" - if identifier in WIRE: - substage = ( - "VMESS.identity" - if "identity" in test - else "VMESS.codec" - if "cipher" in test - else "VMESS.udp" - if "udp" in test - else "VMESS.close" - ) - elif identifier == "VMESS-CODEC": - substage = "VMESS.codec" - cases.append( - dict( - case_id=identifier, - stage="VMESS", - substage=substage, - required=True, - row_ids=rows(identifier), - protocol="vmess", - network=mode, - security="tls" if tls else "plain", - udp_codec="raw/xudp/packetaddr" - if "udp" in test or test == "public_base" - else "per-assertion", - field_values={ - "network": mode, - "tls": tls, - "scope": "protocol-consumer", - }, - outer_family="IPv4/IPv6" if "ipv6" in test else "IPv4", - inner_family="IPv4/IPv6/domain" - if "base" in test or "udp_boundaries" in test - else "per-assertion", - target_type="isolated synthetic origin" if native else "memory-only", - upstream_graph="direct/concrete/nested-select" - if "graph" in test - else "per-assertion", - peer_kind=kind, - peer_config=dict(mode=mode, tls=tls, test=test), - gap_source={ - "reason": "Mihomo VMess listener has no HTTP header/legacy H2 " - "or custom ED field; official V2Ray supplements only these paths", - "source": "https://github.com/MetaCubeX/mihomo/blob/v1.19.31/listener/inbound/vmess.go", - } - if kind == "V2" - else None, - expected_observation=[test] - if native - else SCRIPT_ASSERTIONS - if identifier == "VMESS-SCRIPTS" - else OBSERVATIONS.get(identifier, [identifier]), - required_evidence=["structured-assertions", "command", "cleanup"] - + (["peer-identity", "isolated-origins"] if native else []), - prerequisites=[ - "owned-host-only-container-network", - "official-latest-peer", - ] - if native - else ["rust-toolchain"], - runner="native-vmess" if native else "vmess-gate", - timeout_seconds=240 if native else 1200, - ) - ) - return cases - - -def commands(identifier): - cargo = ["cargo", "test", "--locked", "--all-features"] - if identifier == "VMESS-CFG": - return [cargo + ["--test", "vmess_config"], cargo + ["--lib", "config::"]] - if identifier == "VMESS-CODEC": - return [cargo + ["--test", "vmess_codec"], cargo + ["--lib", "outbound::vmess"]] - if identifier == "VMESS-CANCEL": - return [cargo + ["--test", "vmess_lifecycle"]] - if identifier == "VMESS-RELEASE": - return [ - cargo - + [ - "--release", - "--test", - "vmess_config", - "--test", - "vmess_codec", - "--test", - "vmess_lifecycle", - ], - cargo + ["--release", "--lib", "outbound::vmess"], - ] - if identifier == "VMESS-REGRESSION": - return [ - cargo + ["--lib", "xudp::"], - cargo + ["--lib", "security::tls::tests::"], - cargo - + [ - "--test", - "trojan", - "--test", - "trojan_config", - "--test", - "stream_foundations", - "--test", - "h2_stream_regression", - "--test", - "feature_foundations", - "--test", - "limit_foundations", - ], - cargo - + [ - "--lib", - "inbound::listen::tests::socks_udp_send_buffer_covers_the_declared_wire_limit", - "--", - "--exact", - ], - ] - if identifier == "VMESS-QUALITY": - return [ - ["cargo", "fmt", "--all", "--", "--check"], - [ - "cargo", - "clippy", - "--locked", - "--all-features", - "--lib", - "--bins", - "--", - "-D", - "warnings", - ], - ["vcore-scripts", "check", "c-header"], - ["vcore-scripts", "check", "tls-dependencies"], - ["ruff", "check", "scripts"], - ["ruff", "format", "--check", "scripts"], - ["git", "diff", "--check"], - ["vcore-scripts", "build", "apple"], - ["vcore-scripts", "build", "android"], - ] - return [] - - -def gate_result(case, records, events): - result = new_result(case) - good = bool(records) and all( - r.get("exit_code") == 0 and r.get("cleanup") is True for r in records - ) - identifier = case["case_id"] - assertions = {} - if identifier in OBSERVATIONS: - for name in case["expected_observation"]: - observed = [e for e in events if e.get("assertion") == name] - assertions[name] = len(observed) == 2 and [ - e.get("status") for e in observed - ] == ["BEGIN", "PASS"] - good &= ( - bool(assertions) - and all(assertions.values()) - and all( - e.get("schema_version") == 1 and e.get("status") in {"BEGIN", "PASS"} - for e in events - ) - ) - else: - assertions = dict.fromkeys(case["expected_observation"], good) - result.update( - status="PASS" if good else "FAIL", - assertions=assertions, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=all(r.get("cleanup") is True for r in records), - ) - return result - - -def native_results(cases, report, directory): - records = report.get("cases", []) - expected = {case["case_id"] for case in cases} - if len(records) != len(expected) or {r.get("case_id") for r in records} != expected: - raise ValueError("missing, duplicate or unknown VMess native case") - by_id = {record["case_id"]: record for record in records} - output = [] - for case in cases: - record = by_id[case["case_id"]] - path = directory / (case["case_id"] + "-events.jsonl") - observed = read_events(path) if path.exists() else [] - test = NATIVE[case["case_id"]][3] - observed_ok = ( - events_pass(observed, test) - if case["case_id"] in PUBLIC - else observed - == [ - dict( - schema_version=1, suite="VMESS-WIRE", assertion=test, status=status - ) - for status in ("BEGIN", "PASS") - ] - ) - clean = ( - record.get("command_cleanup") is True - and record.get("cleanup") is True - and report.get("cleanup") is True - ) - expected_command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vmess_public" if case["case_id"] in PUBLIC else "vmess_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - good = ( - observed_ok - and clean - and report.get("source_unchanged") is True - and record.get("exit_code") == 0 - and record.get("status") == "PASS" - and record.get("command") == expected_command - and record.get("peer_kind") == case["peer_kind"] - ) - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions={test: observed_ok}, - evidence=case["required_evidence"] if good else [], - command_exit_code=record.get("exit_code"), - cleanup=clean, - ) - output.append(result) - return output - - -def fields_report(cases, results): - statuses = {result["case_id"]: result["status"] for result in results} - complete = set(statuses) == REQUIRED_IDS and all( - value == "PASS" for value in statuses.values() - ) - fields = [] - for row in sorted(FIELD_IDS): - owners = [c["case_id"] for c in cases if row in c["row_ids"]] - fields.append( - dict( - row_id=row, - required_cases=owners, - status="PASS" if complete and len(owners) > 1 else "NOT RUN", - ) - ) - return dict(schema_version=1, stage="VMESS", protocol="vmess", fields=fields) - - -def execute(cases, run, output, results): - from .protocol_harness import _command, _features, _scripts - - for case in cases: - identifier = case["case_id"] - if identifier in NATIVE: - continue - if identifier == "VMESS-FEATURES": - result = _features(case, run, output) - build = _command( - run, - output, - "production-build", - [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ], - 1200, - ) - if build.returncode or not build.cleanup: - result.update( - status="FAIL", - command_exit_code=build.returncode, - cleanup=build.cleanup, - ) - results[identifier] = result - elif identifier == "VMESS-SCRIPTS": - results[identifier] = _scripts(case, run, output) - else: - events = [] - start = len(run["commands"]) - for index, argv in enumerate(commands(identifier)): - path = output / f"{identifier}-{index}-events.jsonl" - _command(run, output, f"{identifier}-{index}", argv, 1200, events=path) - if path.exists(): - events.extend(read_events(path)) - results[identifier] = gate_result(case, run["commands"][start:], events) - native = [case for case in cases if case["case_id"] in NATIVE] - if native: - directory = output / "native" - native_run(directory, [case["case_id"] for case in native]) - report = read_json(directory / "vmess-results.json") - (output / "peers.json").write_text(json.dumps(report["peers"], indent=2) + "\n") - for result in native_results(native, report, directory): - results[result["case_id"]] = result - (output / "fields.json").write_text( - json.dumps(fields_report(cases, list(results.values())), indent=2) + "\n" - ) - - -def check(run_dir, cases, run, results, peers, paths): - from .protocol_harness import SCRIPT_OBSERVATIONS - - needed = { - "native/vmess-results.json", - "fields.json", - "script-tests.json", - "resources.jsonl", - "peers.json", - "cases.json", - "summary.md", - } - if not needed <= set(paths): - raise ValueError("missing VMESS acceptance artifacts") - expected = {c["case_id"]: c for c in definitions()} - if len(cases) != len(expected) or any( - c != expected.get(c["case_id"]) for c in cases - ): - raise ValueError("VMess frozen case metadata changed") - report = read_json(run_dir / "native/vmess-results.json") - source_keys = ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - if ( - report.get("source") != {key: run.get(key) for key in source_keys} - or report.get("status") != "PASS" - ): - raise ValueError("VMess peer evidence input mismatch") - isolation = report.get("isolation", {}) - if ( - isolation.get("host_servers") is not False - or isolation.get("network_mode") != "hostOnly" - or not isolation.get("peers") - or not all( - p.get("joined") is True and p.get("started") is True - for p in isolation["peers"] - ) - ): - raise ValueError("VMess peers not isolated or cleaned") - if peers != report.get("peers") or set(peers) != {"M", "V2"}: - raise ValueError("missing VMess official peers") - for kind, peer in peers.items(): - project = "MetaCubeX/mihomo" if kind == "M" else "v2fly/v2ray-core" - if ( - peer.get("kind") != kind - or peer.get("target") != "linux-arm64" - or not peer.get("version") - or not peer.get("source_url", "").startswith( - f"https://github.com/{project}/releases/" - ) - or not all( - HEX.fullmatch(peer.get(key, "")) - for key in ("archive_sha256", "binary_sha256") - ) - ): - raise ValueError("invalid official VMess peer identity") - actual = {r["case_id"]: r for r in results} - recalculated = native_results( - [c for c in cases if c["case_id"] in NATIVE], report, run_dir / "native" - ) - for case in cases: - identifier = case["case_id"] - argv = commands(identifier) - if not argv: - continue - records = [] - events = [] - for i, command in enumerate(argv): - matching = [r for r in run["commands"] if r["name"] == f"{identifier}-{i}"] - if len(matching) != 1 or matching[0]["command"] != [ - redact(part) for part in command - ]: - raise ValueError("missing VMess gate command") - records.extend(matching) - path = run_dir / f"{identifier}-{i}-events.jsonl" - if path.exists(): - events.extend(read_events(path)) - recalculated.append(gate_result(case, records, events)) - for item in recalculated: - if item != actual.get(item["case_id"]): - raise ValueError("VMess results disagree with structured observations") - names = [c["name"] for c in run["commands"]] - needed_commands = { - f"{identifier}-{i}" - for identifier in GATES - for i in range(len(commands(identifier))) - } | { - "production-build", - "offline-scripts", - "feature-default", - "feature-minimal", - *( - "feature-" + name - for name in ( - "outbound-trojan", - "outbound-vmess", - "outbound-hysteria2", - "stream-transport", - "quic-transport", - ) - ), - } - if ( - len(names) != len(set(names)) - or set(names) != needed_commands - or any( - c.get("exit_code") != 0 - or c.get("cleanup") is not True - or c.get("log") not in paths - for c in run["commands"] - ) - ): - raise ValueError("missing or failed VMess validation command") - for record in run["commands"]: - name = record["name"] - if name == "feature-default": - expected_command = [ - "cargo", - "test", - "--locked", - "--test", - "feature_foundations", - ] - elif name.startswith("feature-"): - expected_command = [ - "cargo", - "check", - "--locked", - "--no-default-features", - "--lib", - ] - if name != "feature-minimal": - expected_command += ["--features", name.removeprefix("feature-")] - elif name == "production-build": - expected_command = [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ] - elif name == "offline-scripts": - expected_command = [ - sys.executable, - "-m", - "vcore_scripts.protocol_script_tests", - str(run_dir / "script-tests.json"), - ] - else: - continue - if record["command"] != [redact(part) for part in expected_command]: - raise ValueError("VMess feature or script command changed") - script = read_json(run_dir / "script-tests.json") - tests = {t["test"]: t["status"] for t in script["cases"]} - if ( - len(tests) != script["tests_run"] - or any(tests.get(name) != "PASS" for name in SCRIPT_OBSERVATIONS.values()) - or any(status != "PASS" for status in tests.values()) - ): - raise ValueError("missing harness failure-path tests") - fields = fields_report(cases, results) - if read_json(run_dir / "fields.json") != fields or any( - f["status"] != "PASS" for f in fields["fields"] - ): - raise ValueError("incomplete VMess field coverage") - resources = read_events(run_dir / "resources.jsonl") - owned = [ - e - for e in resources - if e.get("suite") == "VMESS-OWNED" and e.get("status") == "PASS" - ] - if len(owned) != 40 or not all(idle_resources(e.get("resources")) for e in owned): - raise ValueError("missing 20-round owned resource evidence per selected mode") - print( - f"VMESS: PASS ({len(cases)} required cases; {len(FIELD_IDS)} VMess field rows)" - ) diff --git a/scripts/src/vcore_scripts/protocol_vmess_container.py b/scripts/src/vcore_scripts/protocol_vmess_container.py deleted file mode 100644 index 501cda7..0000000 --- a/scripts/src/vcore_scripts/protocol_vmess_container.py +++ /dev/null @@ -1,355 +0,0 @@ -"""VMESS wire cases against official latest peers and entirely isolated origins.""" - -from __future__ import annotations - -import contextlib -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_release import download_mihomo -from .native_release import PeerArtifact, download_native -from .protocol_containers import ContainerLab, command -from .protocol_evidence import read_events -from .protocol_fixtures import certificates -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vmess import CASES, peer_config -from .protocol_vmess_public import CASES as PUBLIC_CASES -from .protocol_vmess_public import events_pass, node_config - -ALL_CASES = CASES | PUBLIC_CASES - - -def run(output: Path, selected=None, *, preflight_only=False, supplied=None): - selected = list(ALL_CASES) if selected is None else selected - if ( - not selected - or len(set(selected)) != len(selected) - or not set(selected) <= ALL_CASES.keys() - ): - raise ValueError("invalid VMESS native selection") - output.mkdir(parents=True, exist_ok=False) - if preflight_only: - # One listener/origin group per official implementation, not a traffic run. - kinds = {ALL_CASES[case][0] for case in selected} - selected = [ - next(case for case in selected if ALL_CASES[case][0] == kind) - for kind in sorted(kinds) - ] - report = dict( - stage="VMESS", - scope="container-wire-and-public-consumer", - source=source_identity(), - status="NOT RUN", - phase="preflight", - cases=[], - peers={}, - isolation={}, - ) - try: - artifacts = {} - for kind in sorted({ALL_CASES[case][0] for case in selected} | {"M"}): - directory = output / "binaries" / kind - if supplied is not None and kind in supplied: - binary, identity = supplied[kind] - artifacts[kind] = PeerArtifact(binary, identity) - elif kind == "M": - identity = {} - binary = download_mihomo( - "linux-arm64", directory=directory, identity=identity - ) - artifacts[kind] = PeerArtifact(binary, identity) - else: - artifacts[kind] = download_native( - kind, directory, "linux-arm64", defer_version=True - ) - report["peers"][kind] = artifacts[kind].identity - lab = ContainerLab(report["isolation"], mtu=1500) - report["phase"] = "build" - built = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vmess_native", - "--test", - "vmess_public", - "--no-run", - ], - timeout=180, - ) - (output / "build.log").write_text(redact(built.stdout.decode(errors="replace"))) - if built.returncode != 0 or not built.cleanup: - raise RuntimeError("container wire build failed") - # Reuse a peer only within one transport; every case gets fresh origin - # controls/ports and sessions. One downloaded release snapshot per run. - groups = {} - for case in selected: - kind, mode, encrypted, test = ALL_CASES[case] - groups.setdefault((kind, mode, encrypted), []).append((case, test)) - for (kind, mode, encrypted), cases in groups.items(): - report["phase"] = "peer-start" - with tempfile.TemporaryDirectory( - prefix="private-", dir=output - ) as temporary: - root = Path(temporary) - with contextlib.ExitStack() as stack: - origin_dir, server_dir = root / "origin", root / "server" - origin_dir.mkdir() - server_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - artifact = artifacts[kind] - shutil.copy2(artifact.binary, server_dir / "peer") - tag = f"{mode}-{'tls' if encrypted else 'plain'}" - origin = lab.start( - stack, - origin_dir, - tag + "-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - argv = ( - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ] - ) - server = lab.start(stack, server_dir, tag + "-server", argv) - version = command( - "exec", - server.name, - "/data/fixture/peer", - "-v" if kind == "M" else "version", - ).strip() - digest = command( - "exec", server.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if not version or digest != artifact.identity["binary_sha256"]: - raise RuntimeError("container peer identity mismatch") - artifact.identity["version"] = version - # Non-leaf pin still exercises actual certificate name checks. - from .protocol_fixtures import certificate_chain - - cert, key, pin = ( - certificate_chain(server_dir) - if encrypted - else certificates(server_dir) - ) - config = peer_config( - kind, - mode, - encrypted, - 23000, - Path("/data/fixture") / cert.name, - Path("/data/fixture") / key.name, - ) - if kind == "M": - config["hosts"] = {"vcore-fixture.test": origin.ipv4} - config["listeners"][0]["listen"] = "::" - else: - config["dns"]["hosts"] = {"vcore-fixture.test": origin.ipv4} - config["inbounds"][0]["listen"] = "::" - (server_dir / "config.json").write_text(json.dumps(config)) - upstream = None - node = node_config(mode, encrypted, server.ipv4, pin) - hop = None - if any(case in PUBLIC_CASES for case, _ in cases): - upstream_dir = root / "upstream" - upstream_dir.mkdir() - shutil.copy2(artifacts["M"].binary, upstream_dir / "peer") - upstream = lab.start( - stack, - upstream_dir, - tag + "-upstream", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - (upstream_dir / "config.json").write_text( - json.dumps( - { - "socks-port": 23001, - "allow-lan": True, - "bind-address": "*", - "log-level": "silent", - "ipv6": True, - "hosts": {"peer.fixture.test": server.ipv4}, - "rules": ["MATCH,DIRECT"], - } - ) - ) - hop = dict( - name="hop", - type="socks5", - server=upstream.ipv4, - port=23001, - udp=True, - ) - upstream.release() - upstream.wait_tcp(23001) - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - server_ipv6=server.ipv6, - data_dir=str(root / "data"), - node=node, - hop=hop, - peer_kind=kind, - ) - ) - ) - origin.release() - server.release() - origin.wait_tcp(24000) - server.wait_tcp(23000) - report["phase"] = "execute" - for case, test in cases: - if preflight_only: - continue - print(case, flush=True) - record = dict(case_id=case, status="NOT RUN", peer_kind=kind) - report["cases"].append(record) - events = output / f"{case}-events.jsonl" - test_command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vmess_public" if case in PUBLIC_CASES else "vmess_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ] - result = run_command( - test_command, - cwd=CORE_DIR, - timeout=240, - limit=4 * 1024 * 1024, - env=dict( - os.environ, - VCORE_VMESS_PEER=f"{server.ipv4}:23000", - VCORE_VMESS_TRANSPORT=json.dumps( - dict( - mode=mode, - tls=encrypted, - pin=pin, - peer_kind=kind, - udp_path_limit=15000, - ) - ), - VCORE_VMESS_ORIGIN_V4=origin.ipv4, - VCORE_VMESS_AB_INPUT=str(fixture), - VCORE_CASE_EVENTS=str(events), - ), - ) - (output / f"{case}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = read_events(events) if events.exists() else [] - record.update( - command=test_command, - exit_code=result.returncode, - seconds=result.seconds, - command_cleanup=result.cleanup, - status="PASS" - if result.returncode == 0 - and result.cleanup - and ( - events_pass(observed, test) - if case in PUBLIC_CASES - else observed - == [ - dict( - schema_version=1, - suite="VMESS-WIRE", - assertion=test, - status=status, - ) - for status in ("BEGIN", "PASS") - ] - ) - else "FAIL", - ) - server.ensure_alive() - origin.ensure_alive() - print(f"{case}: {record['status']}", flush=True) - for case, _ in cases: - if preflight_only: - continue - next(item for item in report["cases"] if item["case_id"] == case)[ - "cleanup" - ] = all( - peer.record["joined"] - for peer in [server, origin] + ([upstream] if upstream else []) - ) - report["status"] = ( - "READY" - if preflight_only - else "PASS" - if (preflight_only or len(report["cases"]) == len(selected)) - and all(case["status"] == "PASS" for case in report["cases"]) - else "FAIL" - ) - except KeyboardInterrupt: - report.update(status="INTERRUPTED", reason="user interruption") - except (OSError, RuntimeError, ValueError, KeyError) as error: - report.update( - status="BLOCKED" if report["phase"] == "preflight" else "FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - finally: - report["cleanup"] = all( - peer["joined"] for peer in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - if report["status"] in {"PASS", "READY"} and not ( - report["cleanup"] and report["source_unchanged"] - ): - report["status"] = "FAIL" - (output / "vmess-results.json").write_text(json.dumps(report, indent=2) + "\n") - return ( - 0 - if report["status"] in {"PASS", "READY"} - else (130 if report["status"] == "INTERRUPTED" else 1) - ) diff --git a/scripts/src/vcore_scripts/protocol_vmess_public.py b/scripts/src/vcore_scripts/protocol_vmess_public.py deleted file mode 100644 index 580cf4f..0000000 --- a/scripts/src/vcore_scripts/protocol_vmess_public.py +++ /dev/null @@ -1,153 +0,0 @@ -"""Required public-consumer cases, independent of the persisted manifest.""" - -from .protocol_evidence import idle_resources - -CASES = { - "VMESS-PUBLIC-UDP-FIRST": ("M", "tcp", False, "public_udp_first_response"), - "VMESS-PUBLIC-BODY-OPTIONS": ("M", "tcp", False, "public_body_options"), -} -CASES["VMESS-PUBLIC-WS-ALPN"] = ("M", "ws-alpn", True, "public_alpn_rejection") -for mode in ("tcp", "ws", "grpc", "http", "h2"): - kind = "V2" if mode in {"http", "h2"} else "M" - for tls in (False, True): - label = f"VMESS-PUBLIC-{mode.upper()}-{'TLS' if tls else 'PLAIN'}" - for suffix, test in ( - ("BASE", "public_base"), - ("NEG", "public_negative"), - ("GRAPH", "runtime::public_graph"), - ("IPV6", "runtime::public_ipv6_and_gates"), - ("ENTRYPOINTS", "runtime::public_entrypoints"), - ): - CASES[label + "-" + suffix] = (kind, mode, tls, test) -for mode in ("tcp", "grpc"): - for suffix, test in ( - ("LIFE", "runtime::public_lifecycle"), - ("OWNED", "runtime::owned_resources"), - ): - CASES[f"VMESS-PUBLIC-{mode.upper()}-{suffix}"] = ("M", mode, True, test) -for mode in ("tcp", "ws", "grpc"): - CASES[f"VMESS-REGRESSION-TROJAN-{mode.upper()}"] = ( - "M", - "trojan-" + mode, - True, - "public_legacy_regression", - ) - CASES[f"VMESS-PUBLIC-{mode.upper()}-UDP-ISOLATION"] = ( - "M", - mode, - True, - "runtime::public_udp_isolation", - ) -for mode in ("ws-ed-1", "ws-ed-2048", "ws-header", "ws-path", "grpc-custom"): - kind = "V2" if mode in {"ws-header", "ws-path"} else "M" - CASES[f"VMESS-PUBLIC-{mode.upper()}"] = (kind, mode, True, "public_base") - - -def node_config(mode, tls, host, pin): - if mode.startswith("trojan-"): - node = node_config(mode.removeprefix("trojan-"), tls, host, pin) - node.update( - type="trojan", - password="synthetic-regression-only", - sni=node.pop("servername"), - ) - for key in ("uuid", "tls"): - node.pop(key) - return node - network = mode.split("-")[0] - node = dict( - name="peer", - type="vmess", - server=host, - port=23000, - uuid="07070707-0707-0707-0707-070707070707", - udp=True, - network=network, - tls=tls, - ) - if tls: - node.update(servername="localhost", fingerprint=pin) - node["alpn"] = ["h2" if network in {"grpc", "h2"} else "http/1.1"] - if network == "ws": - node["ws-opts"] = { - "path": "/vmess-ws", - "headers": {"Host": "localhost", "X-Fixture": "exact-value"}, - } - if mode == "ws-alpn": - node["alpn"] = ["h2", "http/1.1"] - if mode.startswith("ws-ed"): - node["ws-opts"]["max-early-data"] = int(mode.split("-")[-1]) - elif mode in {"ws-header", "ws-path"}: - node["ws-opts"].update( - { - "path": "/vmess-ws/", - "max-early-data": 2048, - "early-data-header-name": "X-Vcore-Ed" - if mode == "ws-header" - else "", - } - ) - elif network == "grpc": - node["grpc-opts"] = { - "grpc-service-name": "/vmess-grpc/Tun" - if mode == "grpc-custom" - else "vmess-grpc" - } - elif network == "http": - node["http-opts"] = { - "method": "POST", - "path": ["/vmess-http"], - "headers": {"Host": ["localhost"], "X-Fixture": ["exact-value"]}, - } - elif network == "h2": - node["h2-opts"] = {"host": ["localhost"], "path": "/vmess-h2"} - return node - - -def pairs(events, suite, assertion, count): - selected = [ - e for e in events if e.get("suite") == suite and e.get("assertion") == assertion - ] - return len(selected) == count * 2 and all( - e.get("status") == ("BEGIN" if i % 2 == 0 else "PASS") - for i, e in enumerate(selected) - ) - - -def events_pass(events, test): - main = [event for event in events if event.get("suite") == "VMESS-PUBLIC"] - if len(main) != 2 or not pairs(main, "VMESS-PUBLIC", test, 1): - return False - if any( - event.get("schema_version") != 1 or event.get("status") not in {"BEGIN", "PASS"} - for event in events - ): - return False - if test == "public_base" and not ( - pairs(events, "VMESS-BASE", "tcp_10mib_both_directions", 3) - and pairs(events, "VMESS-BASE", "udp_each_codec_and_family", 3) - ): - return False - if test in {"runtime::public_lifecycle", "runtime::owned_resources"}: - suite = "VMESS-LIFE" if test.endswith("public_lifecycle") else "VMESS-OWNED" - if not pairs(events, suite, "stop_and_remain_quiet", 20): - return False - for end in [ - e for e in events if e.get("suite") == suite and e["status"] == "PASS" - ]: - if end.get("seconds", 0) < 5: - return False - if suite == "VMESS-OWNED": - checkpoints = end.get("checkpoints", []) - points = {p["phase"]: p["resources"] for p in checkpoints} - if ( - len(checkpoints) != 3 - or set(points) != {"baseline", "after-stop", "quiet"} - or not all(idle_resources(p) for p in points.values()) - or points["after-stop"] != points["quiet"] - or not idle_resources(end.get("resources")) - ): - return False - return test != "public_body_options" or pairs( - events, "VMESS-BODY", "config_controls_aead_body", 14 - ) diff --git a/scripts/src/vcore_scripts/protocol_vmess_udp_ab.py b/scripts/src/vcore_scripts/protocol_vmess_udp_ab.py deleted file mode 100644 index 484954c..0000000 --- a/scripts/src/vcore_scripts/protocol_vmess_udp_ab.py +++ /dev/null @@ -1,88 +0,0 @@ -"""Container-only matched UDP diagnostic, not VMESS stage acceptance.""" - -from __future__ import annotations - -import argparse -import itertools -import re -import sys -from pathlib import Path - -from .mihomo_isolation import exclusive_run - - -def options(codecs): - return [ - dict(codec=codec, cipher=cipher, padding=padding, length=length) - for codec, cipher, padding, length in itertools.product( - codecs, - ["none", "auto", "aes-128-gcm", "chacha20-poly1305"], - [False, True], - [False, True], - ) - if cipher != "none" or not (padding or length) - ] - - -def warning_summary(path): - lines = path.read_text(errors="replace").splitlines() - loopbacks = [] - for line in lines: - if "reject loopback connection" in line: - endpoints = re.findall( - r"(?:\[[0-9a-fA-F:]+\]|(?:[a-zA-Z0-9_-]+\.)+[a-zA-Z0-9_-]+):(\d+)\b", - line, - ) - loopbacks.append(dict(ports=[int(port) for port in endpoints])) - return dict(lines=len(lines), loopback_rejections=loopbacks) - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument( - "--modes", - nargs="+", - choices=["tcp", "ws", "grpc"], - default=["tcp", "ws", "grpc"], - ) - parser.add_argument( - "--tls", nargs="+", choices=["plain", "tls"], default=["plain", "tls"] - ) - parser.add_argument( - "--codecs", - nargs="+", - choices=["raw", "xudp", "packetaddr"], - default=["raw", "xudp", "packetaddr"], - ) - parser.add_argument( - "--families", - nargs="+", - choices=["ipv4", "ipv6", "domain"], - default=["ipv4", "ipv6", "domain"], - ) - parser.add_argument("--rounds", type=int, choices=range(1, 21), default=2) - parser.add_argument("--packets", type=int, choices=range(1, 101), default=100) - parser.add_argument("--sizes", nargs="+", type=int) - parser.add_argument("--include-boundary", action="store_true") - parser.add_argument("--collision-probe", action="store_true") - parser.add_argument("--socket-probe", action="store_true") - parser.add_argument("--nat-reuse-probe", action="store_true") - args = parser.parse_args() - if args.collision_probe or args.socket_probe: - parser.error("BLOCKED: archived host-server probes require container migration") - if args.sizes and any(size < 1 or size > 15000 for size in args.sizes): - parser.error("sizes must be between 1 and the 15000-byte fixture budget") - args.tls = [value == "tls" for value in args.tls] - if args.nat_reuse_probe: - args.modes, args.tls, args.codecs = ["tcp"], [False], ["raw", "xudp"] - args.rounds, args.packets, args.sizes, args.families = 1, 1, [1], ["ipv4"] - args.include_boundary = False - from .protocol_vmess_udp_container import run as container_run - - with exclusive_run(): - return container_run(args) - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/scripts/src/vcore_scripts/protocol_vmess_udp_container.py b/scripts/src/vcore_scripts/protocol_vmess_udp_container.py deleted file mode 100644 index bab7145..0000000 --- a/scripts/src/vcore_scripts/protocol_vmess_udp_container.py +++ /dev/null @@ -1,382 +0,0 @@ -"""Container-only VMESS UDP differential; not public-runtime or stage acceptance.""" - -from __future__ import annotations - -import contextlib -import itertools -import json -import os -import shutil -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .mihomo_release import download_mihomo -from .protocol_containers import ContainerLab, command -from .protocol_fixtures import certificates -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vmess import peer_config -from .protocol_vmess_udp_ab import options, warning_summary - - -def client_config(matrix, mode, encrypted, server, origin, client, pin): - listeners, nodes, selected = [], [], [] - for index, option in enumerate(matrix): - port = 25000 + index - selected.append(dict(option, socks_port=port, socks_host=client)) - node = dict( - name=f"edge-{index}", - type="vmess", - server=server, - port=23000, - uuid="07070707-0707-0707-0707-070707070707", - alterId=0, - cipher=option["cipher"], - network=mode, - tls=encrypted, - udp=True, - ) - node.update( - { - "global-padding": option["padding"], - "authenticated-length": option["length"], - } - ) - if option["codec"] != "raw": - node["packet-encoding"] = option["codec"] - if encrypted: - node.update(servername="localhost", fingerprint=pin) - if mode == "ws": - node["ws-opts"] = dict(path="/vmess-ws", headers={"Host": "localhost"}) - if mode == "grpc": - node["grpc-opts"] = {"grpc-service-name": "vmess-grpc"} - nodes.append(node) - listeners.append( - dict( - name=f"fixture-{index}", - type="socks", - listen=client, - port=port, - udp=True, - proxy=node["name"], - ) - ) - return { - "mode": "rule", - "log-level": "warning", - "ipv6": True, - "hosts": {"vcore-fixture.test": origin}, - "listeners": listeners, - "proxies": nodes, - "rules": ["MATCH,REJECT"], - }, selected - - -def run(args): - output = args.output.resolve() - output.mkdir(parents=True, exist_ok=False) - matrix = options(args.codecs) - sizes = args.sizes or [1, 64, 512, 1200] - boundary = args.include_boundary or args.sizes is None - report = dict( - scope="container-matched-mihomo-vcore-udp-diagnostic", - status="NOT RUN", - phase="preflight", - source=source_identity(), - cases=[], - isolation={}, - peer={}, - cleanup=False, - rounds=args.rounds, - packets_per_size=args.packets, - sizes=sizes, - include_boundary=boundary, - boundary_policy="raw/XUDP: 15000; packetaddr: 15000 minus 7/19 address bytes", - families=args.families, - options=matrix, - nat_reuse_probe=args.nat_reuse_probe, - fixture_source_policy="distinct SOCKS UDP sockets retained until transport end", - notes=[ - "Separate owned VMs: protocol server, official client ingress, UDP origin.", - "Host runs VCore and test driver only; no published host/server ports.", - "Same body flags, families, sizes, packet count; order alternates.", - "Send, origin observation and reply each retain their 1s deadline.", - "Origin now echoes autonomously and reports exact received bytes via TCP.", - "Failed associations stop without retries; loopback protection unchanged.", - "Virtual IPv6 and wire diagnostic do not prove device or VMESS acceptance.", - ], - ) - try: - binary = download_mihomo( - "linux-arm64", directory=output / "binary", identity=report["peer"] - ) - lab = ContainerLab(report["isolation"]) - report["phase"] = "build" - build = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vmess_native", - "--no-run", - ], - cwd=CORE_DIR, - timeout=180, - ) - (output / "build.log").write_text(redact(build.stdout.decode(errors="replace"))) - if build.returncode != 0 or not build.cleanup: - raise RuntimeError("diagnostic build failed") - for mode, encrypted in itertools.product(args.modes, args.tls): - name = f"{mode}-{'tls' if encrypted else 'plain'}" - record = dict(name=name, status="NOT RUN") - report["cases"].append(record) - report["phase"] = "peer-start" - print(f"UDP-CONTAINERS: {name}", flush=True) - with tempfile.TemporaryDirectory( - prefix="private-", dir=output - ) as temporary: - root = Path(temporary) - with contextlib.ExitStack() as stack: - directories = {} - peers = {} - for role in ["origin", "server", "client"]: - directory = root / role - directory.mkdir() - directories[role] = directory - if role == "origin": - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - directory / "origin.py", - ) - argv = [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ] - else: - shutil.copy2(binary, directory / "mihomo") - argv = [ - "/data/fixture/mihomo", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - peers[role] = lab.start( - stack, directory, f"{name}-{role}", argv - ) - origin, server, client = ( - peers[key] for key in ["origin", "server", "client"] - ) - version = command( - "exec", server.name, "/data/fixture/mihomo", "-v" - ).strip() - digest = command( - "exec", server.name, "sha256sum", "/data/fixture/mihomo" - ).split()[0] - if digest != report["peer"]["binary_sha256"]: - raise RuntimeError( - "container peer identity differs from download" - ) - report["peer"]["version"] = version - record["peer_version"] = version - record["origin_python"] = command( - "exec", origin.name, "python", "--version" - ).strip() - cert, key, pin = certificates(directories["server"]) - config = peer_config( - "M", - mode, - encrypted, - 23000, - Path("/data/fixture") / cert.name, - Path("/data/fixture") / key.name, - ) - config["log-level"] = "warning" - config["hosts"] = {"vcore-fixture.test": origin.ipv4} - config["listeners"][0]["listen"] = server.ipv4 - (directories["server"] / "config.json").write_text( - json.dumps(config) - ) - config, selected = client_config( - matrix, - mode, - encrypted, - server.ipv4, - origin.ipv4, - client.ipv4, - pin, - ) - (directories["client"] / "config.json").write_text( - json.dumps(config) - ) - for peer in peers.values(): - peer.release() - origin.wait_tcp(24000) - server.wait_tcp(23000) - for option in selected: - client.wait_tcp(option["socks_port"]) - report["phase"] = "execute" - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - options=selected, - rounds=args.rounds, - packets_per_size=args.packets, - sizes=sizes, - include_boundary=boundary, - families=args.families, - ) - ) - ) - events = output / f"{name}.jsonl" - test_command = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vmess_native", - "udp_ab::socks_nat_source_reuse" - if args.nat_reuse_probe - else "udp_ab::matched_clients", - "--", - "--ignored", - "--exact", - "--nocapture", - ] - execution = run_command( - test_command, - cwd=CORE_DIR, - timeout=600, - limit=4 * 1024 * 1024, - env=dict( - os.environ, - VCORE_VMESS_PEER=f"{server.ipv4}:23000", - VCORE_VMESS_TRANSPORT=json.dumps( - dict(mode=mode, tls=encrypted, pin=pin) - ), - VCORE_VMESS_ORIGIN_V4=origin.ipv4, - VCORE_VMESS_AB_INPUT=str(fixture), - VCORE_VMESS_AB_EVENTS=str(events), - VCORE_VMESS_AB_PRIVATE=str(root), - ), - ) - (output / f"{name}.log").write_text( - redact(execution.stdout.decode(errors="replace")) - ) - observations = ( - [json.loads(line) for line in events.read_text().splitlines()] - if events.exists() - else [] - ) - expected = ( - 3 - if args.nat_reuse_probe - else args.rounds * len(matrix) * len(args.families) * 2 - ) - record.update( - command=test_command, - exit_code=execution.returncode, - seconds=execution.seconds, - command_cleanup=execution.cleanup, - expected_associations=expected, - observed_associations=len(observations), - failures=[ - item for item in observations if item["status"] != "PASS" - ], - clients={ - kind: dict( - associations=sum( - item["client"] == kind for item in observations - ), - failed=sum( - item["client"] == kind and item["status"] != "PASS" - for item in observations - ), - completed_packets=sum( - item["completed"] - for item in observations - if item["client"] == kind - ), - ) - for kind in ["vcore", "mihomo"] - }, - ) - for peer in peers.values(): - peer.ensure_alive() - # Stop/delete VMs and join bounded log readers before inspection. - for role in ["server", "client"]: - record[f"{role}_warnings"] = warning_summary( - directories[role] / "peer.log" - ) - record["cleanup"] = all( - peer.record["joined"] for peer in peers.values() - ) - expected_outcome = ( - [item["status"] for item in observations] - == ["PASS", "REPRODUCED", "PASS"] - if args.nat_reuse_probe - else not record["failures"] - ) - record["status"] = ( - ("REPRODUCED" if args.nat_reuse_probe else "PASS") - if ( - execution.returncode == 0 - and execution.cleanup - and record["cleanup"] - and len(observations) == expected - and expected_outcome - and all( - item.get("driver_joined", True) for item in observations - ) - ) - else "FAIL" - ) - print( - json.dumps( - {key: record[key] for key in ["name", "status", "clients"]} - ), - flush=True, - ) - expected_status = "REPRODUCED" if args.nat_reuse_probe else "PASS" - report["status"] = ( - expected_status - if report["cases"] - and all(case["status"] == expected_status for case in report["cases"]) - else "FAIL" - ) - except KeyboardInterrupt: - report.update(status="INTERRUPTED", reason="user interruption") - except (OSError, RuntimeError, ValueError, KeyError) as error: - status = "BLOCKED" if report["phase"] == "preflight" else "FAIL" - report.update(status=status, reason=type(error).__name__) - print(f"UDP-CONTAINERS {status}: {type(error).__name__}", flush=True) - finally: - report["cleanup"] = all( - peer["joined"] for peer in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - if report["status"] in {"PASS", "REPRODUCED"} and not ( - report["cleanup"] and report["source_unchanged"] - ): - report["status"] = "FAIL" - (output / "udp-ab.json").write_text(json.dumps(report, indent=2) + "\n") - return ( - 0 - if report["status"] == "PASS" - else (130 if report["status"] == "INTERRUPTED" else 1) - ) diff --git a/scripts/src/vcore_scripts/protocol_xhttp_acceptance.py b/scripts/src/vcore_scripts/protocol_xhttp_acceptance.py deleted file mode 100644 index da32975..0000000 --- a/scripts/src/vcore_scripts/protocol_xhttp_acceptance.py +++ /dev/null @@ -1,660 +0,0 @@ -"""XHTTP execution and independently recomputed, fail-closed stage evidence.""" - -from __future__ import annotations - -import json -import sys - -from .builds import DEFAULT_FEATURES -from .protocol_evidence import HEX, idle_resources, new_result, read_events, read_json -from .protocol_inputs import redact -from .protocol_vless_acceptance import commands as previous_commands -from .protocol_xhttp_catalog import ( - FIELD_IDS, - GATES, - NATIVE, - OBSERVATIONS, - REPRESENTATIVES, - SECURITY, - UNIT_TESTS, - definitions, -) -from .protocol_xhttp_fields import PUBLIC_TESTS, events_pass, public_events_pass - - -def test_command(test): - return [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public" if test in PUBLIC_TESTS else "xhttp_native", - test, - "--", - "--exact", - "--ignored", - "--nocapture", - ] - - -def commands(identifier): - cargo = ["cargo", "test", "--locked", "--all-features"] - if identifier in {"XHTTP-CFG", "XHTTP-UNIT", "XHTTP-RELEASE"}: - names = list(UNIT_TESTS) - if identifier == "XHTTP-CFG": - names = names[:2] - elif identifier == "XHTTP-UNIT": - names = names[2:] - flags = ["--release"] if identifier == "XHTTP-RELEASE" else [] - selected = sum((["--test", name] for name in names), []) - result = [cargo + flags + selected] - if identifier != "XHTTP-CFG": - result += [ - cargo + flags + ["--lib", "transport::xhttp::tests::"], - cargo + flags + ["--lib", "transport::sing_mux::"], - cargo + flags + ["--test", "vless_lifecycle"], - ] - return result - if identifier == "XHTTP-REGRESSION": - return sum( - ( - previous_commands("VLESS-" + part) - for part in ("REGRESSION", "CODEC", "TRANSPORT", "VISION") - ), - [], - ) - if identifier == "XHTTP-QUALITY": - return previous_commands("VLESS-QUALITY") + [ - [ - "cargo", - "test", - "--locked", - "--manifest-path", - "crates/vcore-netstack/Cargo.toml", - "--all-targets", - ], - [ - "cargo", - "clippy", - "--locked", - "--manifest-path", - "crates/vcore-netstack/Cargo.toml", - "--all-targets", - "--", - "-D", - "warnings", - ], - ] - return [] - - -def gate_result(case, records, events): - result = new_result(case) - good = bool(records) and all( - r.get("exit_code") == 0 and r.get("cleanup") is True for r in records - ) - assertions = {} - for name in case["expected_observation"]: - observed = [e for e in events if e.get("assertion") == name] - assertions[name] = len(observed) == 2 and [ - e.get("status") for e in observed - ] == ["BEGIN", "PASS"] - if case["case_id"] not in OBSERVATIONS: - assertions = dict.fromkeys(case["expected_observation"], good) - else: - good &= all(assertions.values()) and all( - e.get("schema_version") == 1 and e.get("status") in {"BEGIN", "PASS"} - for e in events - ) - if case["case_id"] != "XHTTP-REGRESSION": - unit = [e for e in events if e.get("suite") == "XHTTP-UNIT"] - good &= len(unit) == 2 * len(assertions) and all( - e["assertion"] in assertions for e in unit - ) - result.update( - status="PASS" if good else "FAIL", - assertions=assertions, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=bool(records) and all(r.get("cleanup") is True for r in records), - ) - return result - - -def native_results(cases, report, directory, *, allow_partial=False): - records = report.get("cases", []) - expected = {case["case_id"] for case in cases} - actual = {r.get("case_id") for r in records} - if ( - len(records) != len(actual) - or not actual <= expected - or (not allow_partial and actual != expected) - ): - raise ValueError("missing, duplicate or unknown XHTTP native case") - by_id = {record["case_id"]: record for record in records} - output = [] - for case in cases: - identifier = case["case_id"] - if identifier not in by_id: - continue - record = by_id[identifier] - variant, test = NATIVE[identifier] - path = directory / f"{identifier}-events.jsonl" - observed = read_events(path) if path.exists() else [] - observed_ok = ( - public_events_pass(observed, test) - if test in PUBLIC_TESTS - else events_pass(observed, test, owned=test.startswith("lifecycle::")) - ) - clean = record.get("command_cleanup") is True and report.get("cleanup") is True - good = ( - observed_ok - and clean - and report.get("source_unchanged") is True - and record.get("status") == "PASS" - and record.get("exit_code") == 0 - and record.get("command") == test_command(test) - and record.get("assertion") == test - and record.get("variant") == variant - ) - if test.startswith("close::"): - path = directory / f"{variant}-close-reference.json" - reference = read_json(path) if path.exists() else {} - good &= ( - reference.get("scope") == "same-mode" - and reference.get("terminated") is True - and reference.get("tail_hex") == "" - ) - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions={test: observed_ok}, - evidence=case["required_evidence"] if good else [], - command_exit_code=record.get("exit_code"), - cleanup=clean, - ) - output.append(result) - return output - - -def security_results(cases, report, directory, *, allow_partial=False): - from .protocol_xhttp_security import security_cases - - expected = {} - identity = { - k: {"certificate": "fixture", "private-key": "fixture"} - for k in ("valid", "absent", "expired", "wrong-ca") - } - for case in cases: - version = SECURITY[case["case_id"]] - base = dict( - alpn=["http/1.1" if version == "h1" else version], - certificate="fixture", - **{"private-key": "fixture", "xhttp-opts": {}}, - ) - expected[version] = { - f"{version}-{name}": failure - for name, _, _, failure in security_cases( - base, identity, identity["valid"], "2001:db8::1" - ) - } - records = report.get("cases", []) - ids = set().union(*(set(e) for e in expected.values())) - actual = {r.get("case_id") for r in records} - if ( - len(records) != len(actual) - or not actual <= ids - or (not allow_partial and actual != ids) - ): - raise ValueError("missing, duplicate or unknown XHTTP security case") - results = [] - by_id = {r["case_id"]: r for r in records} - for case in cases: - version = SECURITY[case["case_id"]] - if allow_partial and not actual.intersection(expected[version]): - continue - clean = report.get("cleanup") is True and report.get("owned_remaining") == [] - good = clean and report.get("source_unchanged") is True - for name, failure in expected[version].items(): - record = by_id.get(name, {}) - path = directory / f"{name}-events.jsonl" - events = read_events(path) if path.exists() else [] - good &= ( - events_pass(events, "security::native_xhttp_security") - and record.get("exit_code") == 0 - and record.get("command_cleanup") is True - and record.get("command") - == test_command("security::native_xhttp_security") - and record.get("status") == "PASS" - ) - if version == "h3" and failure: - category = { - "absent": "certificate_required", - "expired": "certificate_expired", - "wrong-ca": "unknown_ca", - }[failure] - good &= record.get("identity_enforced") is True and any( - e.get("category") == category - for e in record.get("certificate_errors", []) - ) - result = new_result(case) - result.update( - status="PASS" if good else "FAIL", - assertions={case["case_id"]: good}, - evidence=case["required_evidence"] if good else [], - command_exit_code=0 if good else 1, - cleanup=clean, - ) - results.append(result) - return results - - -def fields_report(cases, results): - statuses = {r["case_id"]: r["status"] for r in results} - complete = set(statuses) == {c["case_id"] for c in definitions()} and all( - s == "PASS" for s in statuses.values() - ) - fields = [] - for row in sorted(FIELD_IDS): - owners = [c["case_id"] for c in cases if row in c["row_ids"]] - behavior = [c for c in owners if c in NATIVE or c in SECURITY] - fields.append( - dict( - row_id=row, - required_cases=owners, - status="PASS" - if complete and behavior and "XHTTP-CFG" in owners - else "NOT RUN", - ) - ) - return dict(schema_version=1, stage="XHTTP", protocol="vless", fields=fields) - - -def prepare_peers(output, kinds): - from .caddy_build import build_caddy - from .mihomo_release import download_mihomo - from .native_release import download_native - - supplied = {} - for kind in sorted(kinds): - directory = output / "binaries" / kind - if kind == "M": - identity = {} - binary = download_mihomo( - "linux-arm64", directory=directory, identity=identity - ) - supplied[kind] = binary, identity - else: - artifact = ( - build_caddy(directory) - if kind == "Caddy" - else download_native(kind, directory, "linux-arm64", defer_version=True) - ) - supplied[kind] = artifact.binary, artifact.identity - return supplied - - -def preflight(output): - """Only verify the XHTTP native artifacts inside an owned isolated guest.""" - import shutil - import tempfile - from contextlib import ExitStack - - from .protocol_containers import ContainerLab, command - from .protocol_inputs import CORE_DIR - - supplied = prepare_peers(output, {"M", "XR", "V2", "Caddy"}) - isolation = {} - lab = ContainerLab(isolation, mtu=1500) - try: - with tempfile.TemporaryDirectory( - prefix="xhttp-preflight-", dir=CORE_DIR / "target" - ) as tmp: - from pathlib import Path - - root = Path(tmp) - for kind, (binary, _) in supplied.items(): - shutil.copy2(binary, root / kind) - with ExitStack() as stack: - peer = lab.start( - stack, - root, - "preflight", - ["python", "-c", "import time; time.sleep(600)"], - ) - peer.release() - for kind, (_, identity) in supplied.items(): - binary = "/data/fixture/" + kind - version = command( - "exec", peer.name, binary, "-v" if kind == "M" else "version" - ).strip() - digest = command("exec", peer.name, "sha256sum", binary).split()[0] - if not version or digest != identity["binary_sha256"]: - raise RuntimeError("isolated XHTTP artifact identity mismatch") - identity["version"] = version - finally: - (output / "peers.json").write_text( - json.dumps({k: i for k, (_, i) in supplied.items()}, indent=2) + "\n" - ) - (output / "preflight.json").write_text( - json.dumps( - dict( - scope="preflight-only", - wire_acceptance="NOT RUN", - isolation=isolation, - ), - indent=2, - ) - + "\n" - ) - - -def execute(cases, run, output, results): - from .protocol_harness import _command, _features, _scripts - from .protocol_xhttp_fields import run as fields_run - from .protocol_xhttp_security import run as security_run - - for case in cases: - identifier = case["case_id"] - if identifier not in GATES: - continue - if identifier == "XHTTP-FEATURES": - result = _features(case, run, output) - for name, command in ( - ( - "feature-outbound-vless", - [ - "cargo", - "check", - "--locked", - "--no-default-features", - "--lib", - "--features", - "outbound-vless", - ], - ), - ( - "production-build", - [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ], - ), - ): - observed = _command(run, output, name, command, 1200) - if observed.returncode or not observed.cleanup: - result.update( - status="FAIL", - command_exit_code=observed.returncode, - cleanup=observed.cleanup, - ) - results[identifier] = result - elif identifier == "XHTTP-SCRIPTS": - results[identifier] = _scripts(case, run, output) - else: - start = len(run["commands"]) - events = [] - for i, command in enumerate(commands(identifier)): - path = output / f"{identifier}-{i}-events.jsonl" - observed = _command( - run, output, f"{identifier}-{i}", command, 1200, events=path - ) - if path.exists(): - events.extend(read_events(path)) - if observed.returncode or not observed.cleanup: - break - results[identifier] = gate_result(case, run["commands"][start:], events) - if results[identifier]["status"] != "PASS": - raise RuntimeError(f"{identifier} failed; later cases remain NOT RUN") - native = [c for c in cases if c["case_id"] in NATIVE] - security = [c for c in cases if c["case_id"] in SECURITY] - kinds = {c["peer_kind"] for c in native + security} - if native or security: - kinds.add("M") - if any(c["peer_kind"] == "XR" for c in security): - kinds.add("Caddy") - supplied = prepare_peers(output, kinds) - try: - if native: - jobs = {} - for case in native: - identifier = case["case_id"] - variant, test = NATIVE[identifier] - jobs.setdefault(variant, []).append(dict(case_id=identifier, test=test)) - directory = output / "native" - # Exercise native-gap transports before the long Mihomo matrix. - # Ordering changes neither the frozen case set nor its evidence. - ordered = sorted( - jobs, key=lambda name: (not name.startswith(("h3-", "outer-")), name) - ) - fields_run(directory, ordered, jobs=jobs, supplied=supplied) - report = read_json(directory / "xhttp-fields-results.json") - for result in native_results(native, report, directory, allow_partial=True): - results[result["case_id"]] = result - if report["status"] != "PASS": - raise RuntimeError("native XHTTP cases failed") - if security: - directory = output / "security" - security_run( - directory, [SECURITY[c["case_id"]] for c in security], supplied=supplied - ) - report = read_json(directory / "xhttp-security-results.json") - for result in security_results( - security, report, directory, allow_partial=True - ): - results[result["case_id"]] = result - if report["status"] != "PASS": - raise RuntimeError("native XHTTP security failed") - finally: - (output / "peers.json").write_text( - json.dumps({k: identity for k, (_, identity) in supplied.items()}, indent=2) - + "\n" - ) - (output / "fields.json").write_text( - json.dumps(fields_report(cases, list(results.values())), indent=2) + "\n" - ) - - -def required_command_names(): - return { - f"{identifier}-{i}" - for identifier in GATES - for i in range(len(commands(identifier))) - } | { - "production-build", - "offline-scripts", - "feature-default", - "feature-minimal", - "feature-outbound-vless", - *( - "feature-" + name - for name in ( - "outbound-trojan", - "outbound-vmess", - "outbound-hysteria2", - "stream-transport", - "quic-transport", - ) - ), - } - - -def check(run_dir, cases, run, results, peers, paths): - from .protocol_harness import SCRIPT_OBSERVATIONS - - needed = { - "native/xhttp-fields-results.json", - "security/xhttp-security-results.json", - "fields.json", - "script-tests.json", - "resources.jsonl", - "peers.json", - "cases.json", - "summary.md", - } - if not needed <= set(paths) or cases != definitions(): - raise ValueError("missing XHTTP artifacts or altered frozen metadata") - recalculated = [] - source_keys = ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - for name, file, evaluator, wanted in ( - ("native", "xhttp-fields-results.json", native_results, NATIVE), - ("security", "xhttp-security-results.json", security_results, SECURITY), - ): - report = read_json(run_dir / name / file) - if ( - report.get("source") != {key: run.get(key) for key in source_keys} - or report.get("status") != "PASS" - ): - raise ValueError("XHTTP native source identity mismatch") - isolation = report.get("isolation", {}) - if ( - isolation.get("host_servers") is not False - or isolation.get("network_mode") != "hostOnly" - or not isolation.get("peers") - or any( - p.get("joined") is not True or p.get("started") is not True - for p in isolation["peers"] - ) - ): - raise ValueError("XHTTP native isolation or cleanup missing") - if any( - identity != peers.get(kind) - for kind, identity in report.get("peers", {}).items() - ): - raise ValueError("XHTTP peer identity changed between reports") - recalculated += evaluator( - [c for c in cases if c["case_id"] in wanted], report, run_dir / name - ) - if set(peers) != {"M", "XR", "V2", "Caddy"}: - raise ValueError("XHTTP required official peers missing") - for kind, peer in peers.items(): - project = { - "M": "MetaCubeX/mihomo", - "XR": "XTLS/Xray-core", - "V2": "v2fly/v2ray-core", - "Caddy": "caddyserver/caddy", - }[kind] - if ( - peer.get("target") != "linux-arm64" - or not peer.get("version") - or not peer.get("source_url", "").startswith( - f"https://github.com/{project}/releases/" - ) - or not HEX.fullmatch(peer.get("binary_sha256", "")) - ): - raise ValueError("invalid official XHTTP peer identity") - if kind != "Caddy" and not HEX.fullmatch(peer.get("archive_sha256", "")): - raise ValueError("missing native download digest") - if kind == "Caddy" and ( - peer.get("source_built") is not True - or peer.get("build_cleanup") is not True - or peer.get("extra_plugins") != [] - or set(peer.get("inputs", {})) != {"go.mod", "go.sum", "main.go"} - or any(not HEX.fullmatch(value) for value in peer["inputs"].values()) - ): - raise ValueError("gateway is outside the approved xcaddy build exception") - records = run.get("commands", []) - if ( - len(records) != len(required_command_names()) - or {r.get("name") for r in records} != required_command_names() - or any( - r.get("exit_code") != 0 - or r.get("cleanup") is not True - or r.get("log") not in paths - for r in records - ) - ): - raise ValueError("missing, duplicate or failed XHTTP command") - for case in cases: - identifier = case["case_id"] - expected_commands = commands(identifier) - if not expected_commands: - continue - selected, events = [], [] - for i, argv in enumerate(expected_commands): - record = next(r for r in records if r["name"] == f"{identifier}-{i}") - if record["command"] != [redact(part) for part in argv]: - raise ValueError("XHTTP validation command changed") - selected.append(record) - path = run_dir / f"{identifier}-{i}-events.jsonl" - if path.exists(): - events.extend(read_events(path)) - recalculated.append(gate_result(case, selected, events)) - actual = {r["case_id"]: r for r in results} - if any(r != actual.get(r["case_id"]) for r in recalculated): - raise ValueError("XHTTP results disagree with structured evidence") - for record in records: - name = record["name"] - if name == "feature-default": - argv = ["cargo", "test", "--locked", "--test", "feature_foundations"] - elif name.startswith("feature-"): - argv = ["cargo", "check", "--locked", "--no-default-features", "--lib"] - if name != "feature-minimal": - argv += ["--features", name.removeprefix("feature-")] - elif name == "production-build": - argv = [ - "cargo", - "build", - "--locked", - "--release", - "--no-default-features", - "--features", - DEFAULT_FEATURES, - "--lib", - ] - elif name == "offline-scripts": - argv = [ - sys.executable, - "-m", - "vcore_scripts.protocol_script_tests", - str(run_dir / "script-tests.json"), - ] - else: - continue - if record["command"] != [redact(part) for part in argv]: - raise ValueError("XHTTP feature or script command changed") - script = read_json(run_dir / "script-tests.json") - tests = {t["test"]: t["status"] for t in script["cases"]} - required_tests = set(SCRIPT_OBSERVATIONS.values()) | { - "test_protocol_xhttp.XhttpAcceptanceTest." + name - for name in ( - "test_frozen_catalog_has_all_rows_modes_and_mux_wires", - "test_unknown_duplicate_wrong_suite_and_partial_events_fail", - "test_unit_gate_requires_every_frozen_assertion_and_successful_command", - "test_native_missing_and_duplicate_records_are_not_success", - ) - } - if ( - len(tests) != script["tests_run"] - or not required_tests <= set(tests) - or any(v != "PASS" for v in tests.values()) - ): - raise ValueError("XHTTP offline failure-path proof missing") - fields = fields_report(cases, results) - if read_json(run_dir / "fields.json") != fields or any( - f["status"] != "PASS" for f in fields["fields"] - ): - raise ValueError("incomplete XHTTP field behavior coverage") - resources = read_events(run_dir / "resources.jsonl") - owned = [ - e - for e in resources - if e.get("suite") == "XHTTP-OWNED" and e.get("status") == "PASS" - ] - if len(owned) != 20 * len(REPRESENTATIVES) or any( - not idle_resources(e.get("resources")) for e in owned - ): - raise ValueError("missing XHTTP twenty-round owned resource proof") - print(f"XHTTP: PASS ({len(cases)} required cases; {len(FIELD_IDS)} field rows)") diff --git a/scripts/src/vcore_scripts/protocol_xhttp_catalog.py b/scripts/src/vcore_scripts/protocol_xhttp_catalog.py deleted file mode 100644 index a618fa1..0000000 --- a/scripts/src/vcore_scripts/protocol_xhttp_catalog.py +++ /dev/null @@ -1,394 +0,0 @@ -"""Frozen XHTTP cases. Independent tuning is pairwise; coupled modes remain explicit.""" - -from __future__ import annotations - -import hashlib - -from .protocol_vless_acceptance import OBSERVATIONS as PREVIOUS -from .protocol_vmess_acceptance import SCRIPT_ASSERTIONS -from .protocol_xhttp_fields import native_kind, variants - -UNIT_TESTS = { - "xhttp_config": [ - "xhttp_fields_reject_wrong_types_null_and_every_range_boundary_before_io", - "empty_http_authority_falls_back_to_each_legs_authentication_name", - "download_security_changes_do_not_silently_discard_inherited_certificate_policy", - "h3_requires_exclusive_alpn_and_standard_tls_on_each_leg", - "custom_headers_inherit_replace_clear_and_do_not_leak_values", - "conflicting_request_fields_are_rejected_on_both_legs_before_io", - "http_version_selects_h1_only_for_its_single_alpn_and_supports_plaintext", - "download_certificate_policy_inherits_and_explicit_false_or_empty_clears", - "download_mtls_identity_is_inherited_or_replaced_and_cleared_as_a_pair", - "download_reality_object_replaces_inherits_or_clears_without_merging_keys", - "reuse_object_presence_and_download_whole_object_replacement_are_preserved", - ], - "sing_mux_config": [ - "sing_mux_accepts_three_protocols_and_rejects_invalid_or_ignored_options" - ], - "xhttp_requests": [ - "packet_up_coalesces_small_writes_and_flushes_without_more_caller_io", - "packet_up_backpressures_bounded_batches_and_stop_cancels_the_timer", - "first_vless_read_never_acknowledges_or_replays_a_concurrent_packet_write", - "response_codes_follow_mihomo_streaming_and_packet_rules", - "streaming_content_type_can_be_disabled_without_disabling_padding", - "custom_semantic_headers_are_allowed_when_no_generated_field_overwrites_them", - "padding_uses_the_configured_http_location_and_encoding", - "packet_metadata_and_upload_methods_are_sent_in_each_supported_location", - "packet_upload_can_move_payload_into_bounded_header_or_cookie_chunks", - "http1_stream_one_is_chunked_duplex_and_shutdown_closes_both_directions", - "http1_packet_up_shares_a_session_across_two_connections", - "http1_response_body_can_arrive_after_the_response_headers", - "custom_request_headers_reach_the_http_peer_without_changing_body", - ], - "xhttp_reuse": [ - "enabled_reuse_shares_physical_h2_without_one_close_killing_its_sibling", - "reuse_thresholds_expand_and_retire_without_closing_active_sessions", - "expired_transport_is_not_assigned_again_while_old_session_stays_live", - "pooled_packet_posts_do_not_each_consume_a_transport_lease", - "h2_keepalive_uses_default_explicit_and_disabled_idle_periods", - "download_uses_its_own_reuse_counters_and_keeps_shared_sessions_alive", - "h1_packet_upload_reuses_its_idle_connection_but_reopens_the_cancelled_get", - "stopping_during_handshake_prevents_late_driver_admission", - "stop_wakes_a_handshake_even_when_the_peer_never_responds", - ], - "sing_mux": [ - "yamux_replacing_dropped_streams_at_capacity_keeps_the_sibling_alive", - "sing_mux_scheduling_uses_the_selected_branch_not_a_global_stream_quota", - "h2mux_sends_idle_ping_and_retires_a_peer_that_never_acknowledges", - "only_tcp_preserves_all_three_vless_udp_wire_commands", - "h2mux_reuses_physical_vless_and_cancels_only_one_logical_stream", - "yamux_reuses_physical_vless_and_cancels_only_one_logical_stream", - "smux_reuses_physical_vless_and_cancels_only_one_logical_stream", - "sing_mux_udp_preserves_addresses_and_cancellation_safe_partial_frames", - ], - "xhttp_budget": [ - "h3_rejects_incapable_or_small_budget_upstreams_before_any_datagram" - ], -} - -DRIVER_TESTS = ["malformed_smux_headers_close_the_owned_driver_before_reading_a_body"] - -FIELD_IDS = { - *(f"X{i:02}" for i in range(1, 30)), - *(f"D{i:02}" for i in range(1, 16)), - *(f"D{i:02}" for i in range(27, 33)), - *(f"M{i:02}" for i in range(1, 8)), -} -OBSERVATIONS = { - "XHTTP-CFG": UNIT_TESTS["xhttp_config"] + UNIT_TESTS["sing_mux_config"], - "XHTTP-UNIT": sum( - ( - UNIT_TESTS[n] - for n in ("xhttp_requests", "xhttp_reuse", "sing_mux", "xhttp_budget") - ), - [], - ) - + DRIVER_TESTS, - "XHTTP-REGRESSION": PREVIOUS["VLESS-REGRESSION"] - + PREVIOUS["VLESS-CODEC"] - + PREVIOUS["VLESS-TRANSPORT"] - + PREVIOUS["VLESS-VISION"], -} -OBSERVATIONS["XHTTP-RELEASE"] = OBSERVATIONS["XHTTP-CFG"] + OBSERVATIONS["XHTTP-UNIT"] -GATES = { - "XHTTP-CFG", - "XHTTP-UNIT", - "XHTTP-REGRESSION", - "XHTTP-RELEASE", - "XHTTP-QUALITY", - "XHTTP-FEATURES", - "XHTTP-SCRIPTS", -} -SECURITY = {f"XHTTP-SECURITY-{v.upper()}": v for v in ("h1", "h2", "h3")} -NATIVE = {} -VERSIONS = ("h1", "h2", "h1c", "h2c", "h1r", "h2r", "h3") -ALL_VARIANTS = variants() - - -def add(variant, suffix, test): - if variant not in ALL_VARIANTS: - raise ValueError("missing frozen XHTTP variant") - # Case-sensitive table names need distinct stable identifiers. - digest = hashlib.sha256(variant.encode()).hexdigest()[:8].upper() - identifier = f"XHTTP-{variant.upper()}-{suffix}-{digest}" - if identifier in NATIVE: - raise ValueError("duplicate XHTTP requirement") - NATIVE[identifier] = (variant, test) - - -for version in VERSIONS: - for mode in ("stream-one", "stream-up", "packet-up"): - variant = f"{version}-{mode}-headers" - add(variant, "BASE", "public_base") - add(variant, "CLOSE", "close::native_mihomo_xhttp_close") - for mode in ("stream-up", "packet-up"): - variant = f"{version}-{mode}-split-reuse" - add(variant, "BASE", "public_base") - add(variant, "CLOSE", "close::native_mihomo_xhttp_close") - for suffix in ("auto", "auto-download"): - add(f"{version}-{suffix}", "AUTO", "native_xhttp_request_fields") - if version.endswith("r"): - for suffix in ( - "download-reality-replacement", - "reject-download-short-id", - "reject-download-public-key", - ): - add( - f"{version}-{suffix}", - "SECURITY", - "security::native_xhttp_security" - if suffix.startswith("reject") - else "native_xhttp_request_fields", - ) - -# Every finite request enum is consumed on H1, H2 and H3. Security is orthogonal -# here; every plain/TLS/REALITY + mode + download branch has a public BASE above. -for version in ("h1", "h2", "h3"): - for name, (options, v) in ALL_VARIANTS.items(): - if v != version or "_mux" in options or "_keepalive" in options: - continue - suffix = name.removeprefix(version + "-") - if suffix.startswith( - ("padding-", "metadata-", "payload-", "session-", "post-", "reject-") - ) or suffix in ( - "stream-up-no-grpc", - "stream-one-no-grpc", - "body-auto", - ): - add( - name, - "FIELDS", - "security::native_xhttp_security" - if options.get("_reject") - else "native_xhttp_request_fields", - ) -add( - "h3-keepalive", - "KEEPALIVE", - "keepalive::native_h3_keepalive_observes_each_leg_and_stop", -) - -# All three wire protocols and both only-tcp branches for every outer transport. -# Padding parity alternates between branches; H2 additionally expands all padding -# values. Padding and the outer framing are independent, tested separately above. -for outer in ( - *VERSIONS, - *( - f"outer-{m}" - for m in ( - "tcp", - "tcp-tls", - "tcp-reality", - "ws", - "ws-tls", - "ws-reality", - "grpc", - "grpc-tls", - "grpc-reality", - "http", - "http-tls", - "h2", - "h2-tls", - "ws-header", - "ws-header-tls", - "ws-path", - "ws-path-tls", - ) - ), -): - for index, protocol in enumerate(("h2mux", "smux", "yamux")): - for only in (False, True): - paddings = ( - (False, True) if outer == "h2" else ((index + int(only)) % 2 == 0,) - ) - for padding in paddings: - variant = ( - f"{outer}-mux-{protocol}-{'padded' if padding else 'plain'}" - + ("-only-tcp" if only else "") - ) - add(variant, "BASE", "public_base") - -for version in ("h1", "h2", "h3"): - for protocol in ("h2mux", "smux", "yamux"): - add( - f"{version}-mux-{protocol}-padding-required", - "REJECT", - "security::native_xhttp_security", - ) - -REPRESENTATIVES = ( - "h1-packet-up-split-reuse", - "h2-stream-up-split-reuse", - "h3-packet-up-split-reuse", - "h2-mux-h2mux-padded", - "h2-mux-smux-padded", - "h2-mux-yamux-padded", -) -for variant in REPRESENTATIVES: - for suffix, test in ( - ("GRAPH", "runtime::public_graph"), - ("IPV6", "runtime::public_ipv6_and_gates"), - ("ENTRYPOINTS", "runtime::public_entrypoints"), - ("LIFE", "runtime::public_lifecycle"), - ("ISOLATION", "runtime::public_udp_isolation"), - ("OWNED", "lifecycle::native_xhttp_owned_resources"), - ("NEG", "public_negative"), - ): - add(variant, suffix, test) -for variant in ( - "h1-packet-up-reuse", - "h2-stream-up-reuse", - "h3-packet-up-reuse", - "h2-mux-h2mux-padded", - "h2-mux-smux-padded", - "h2-mux-yamux-padded", -): - add( - variant, - "POOL-SELECTION", - "runtime::grpc_pool_keeps_physical_selection_until_new_transport", - ) - -X_NAMES = ( - "path", - "host", - "mode", - "headers", - "no-grpc-header", - "x-padding-bytes", - "x-padding-obfs-mode", - "x-padding-key", - "x-padding-header", - "x-padding-placement", - "x-padding-method", - "uplink-http-method", - "session-placement", - "session-key", - "session-table", - "session-length", - "seq-placement", - "seq-key", - "uplink-data-placement", - "uplink-data-key", - "uplink-chunk-size", - "sc-max-each-post-bytes", - "sc-min-posts-interval-ms", -) -REUSE_ROWS = { - *(f"X{i:02}" for i in range(24, 30)), - *(f"D{i:02}" for i in range(27, 33)), -} - - -def rows(identifier): - if identifier in {"XHTTP-CFG", "XHTTP-UNIT"}: - return sorted(FIELD_IDS) - if identifier in GATES: - return [] - if identifier in SECURITY: - return [f"D{i:02}" for i in range(1, 14)] - variant, test = NATIVE[identifier] - options, version = ALL_VARIANTS[variant] - if version.startswith("outer-"): - return [f"M{i:02}" for i in range(1, 8)] - result = {"X01", "X02", "X03", "X06"} - result |= {f"X{i:02}" for i, name in enumerate(X_NAMES, 1) if name in options} - if "download-settings" in options: - result |= {"D01", "D02", "D03", "D04", "D05", "D06", "D07", "D08"} - if version.endswith("r"): - result |= {"D14", "D15"} - if "reuse-settings" in options or "reuse" in variant: - result |= REUSE_ROWS - if "_mux" in options: - result |= {f"M{i:02}" for i in range(1, 8)} - if options.get("_keepalive"): - result |= {"X29", "D32"} - return sorted(result) - - -def definitions(): - result = [] - for identifier in sorted(GATES | set(SECURITY) | set(NATIVE)): - variant, test = NATIVE.get( - identifier, (SECURITY.get(identifier, "memory-or-build"), identifier) - ) - options, version = ALL_VARIANTS.get(variant, ({}, variant)) - native = identifier not in GATES - kind = native_kind(version) if native else "unit" - if identifier in SECURITY: - kind = "XR" if version == "h3" else "M" - encrypted = not version.endswith("c") and ( - not version.startswith("outer-") or version.endswith(("-tls", "-reality")) - ) - gap = ( - { - "reason": "Xray H3 forwards to the official Mihomo VLESS decoder " - "for packetaddr/sing-mux. mTLS uses the approved unmodified " - "xcaddy/Caddy gateway to one Xray handler.", - "source": "https://github.com/XTLS/Xray-core/tree/v26.3.27/transport/internet/splithttp", - } - if kind == "XR" - else { - "reason": "Official V2Ray supplies HTTP/H2/custom-ED framing; " - "sing-mux is decoded by official Mihomo, not V2Ray CommandMux.", - "source": "https://github.com/MetaCubeX/mihomo/blob/v1.19.31/listener/inbound/vless.go", - } - if kind == "V2" - else None - ) - result.append( - dict( - case_id=identifier, - stage="XHTTP", - substage="XHTTP.http3" - if version == "h3" - else "XHTTP.mux" - if "_mux" in options - else "XHTTP.download" - if identifier in SECURITY or "download-settings" in options - else "XHTTP.requests" - if native - else "XHTTP.acceptance", - required=True, - row_ids=rows(identifier), - protocol="vless", - network=variant, - security="tls-or-reality" if encrypted else "plain", - udp_codec="raw/xudp/packetaddr/sing-mux", - field_values=dict( - scope="protocol-consumer", variant=variant, **options - ), - outer_family="IPv4/IPv6", - inner_family="IPv4/IPv6/domain", - target_type="isolated synthetic origin" - if native - else "memory-only/build", - upstream_graph="direct/concrete/nested-select" - if "graph" in test - else "per-assertion", - peer_kind=kind, - peer_config=dict(variant=variant, test=test), - gap_source=gap, - expected_observation=OBSERVATIONS.get( - identifier, - SCRIPT_ASSERTIONS if identifier == "XHTTP-SCRIPTS" else [test], - ), - required_evidence=["structured-assertions", "command", "cleanup"] - + (["official-peer-identity", "isolated-origins"] if native else []), - prerequisites=[ - "owned-host-only-container-network", - "official-latest-peer", - ] - if native - else ["rust-toolchain"], - runner="native-xhttp" - if identifier in NATIVE - else "xhttp-security" - if identifier in SECURITY - else "xhttp-gate", - timeout_seconds=3600 - if identifier in GATES or identifier in SECURITY - else 300, - ) - ) - return result diff --git a/scripts/src/vcore_scripts/protocol_xhttp_fields.py b/scripts/src/vcore_scripts/protocol_xhttp_fields.py deleted file mode 100644 index a9fb4d2..0000000 --- a/scripts/src/vcore_scripts/protocol_xhttp_fields.py +++ /dev/null @@ -1,1334 +0,0 @@ -"""XHTTP request-field development checks, not the complete XHTTP stage gate.""" - -from __future__ import annotations - -import contextlib -import copy -import json -import os -import shutil -import ssl -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .caddy_build import build_caddy -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import download_native -from .protocol_containers import ContainerLab, command -from .protocol_evidence import idle_resources, read_events -from .protocol_fixtures import certificate_chain, certificates -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vless_container import CLIENT_FINGERPRINTS -from .protocol_vless_peers import ( - configuration as vless_configuration, -) -from .protocol_vless_peers import ( - peer_kind as vless_peer_kind, -) -from .protocol_vless_public import events_pass as vless_events_pass -from .protocol_vless_security import client_identities -from .protocol_xhttp_gateway import gateway_config, sanitized_gateway_log -from .protocol_xhttp_peers import CLIENT_ID, peer_config - -PUBLIC_TESTS = ( - "public_base", - "public_negative", - "runtime::public_entrypoints", - "runtime::public_graph", - "runtime::public_ipv6_and_gates", - "runtime::public_lifecycle", - "runtime::public_udp_isolation", - "runtime::grpc_pool_keeps_physical_selection_until_new_transport", -) - - -def native_kind(version): - return ( - vless_peer_kind(version.removeprefix("outer-")) - if version.startswith("outer-") - else "XR" - if version == "h3" - else "M" - ) - - -def public_events_pass(events, assertion): - if assertion not in PUBLIC_TESTS or any( - e.get("suite") not in {"XHTTP-PUBLIC", "XHTTP-BASE", "XHTTP-LIFE"} - for e in events - ): - return False - expected = { - ("XHTTP-PUBLIC", assertion): 1, - } - if assertion == "public_base": - expected.update( - { - ("XHTTP-BASE", "tcp_10mib_both_directions"): 3, - ("XHTTP-BASE", "udp_each_codec_and_family"): 3, - } - ) - if assertion == "runtime::public_lifecycle": - expected[("XHTTP-LIFE", "stop_and_remain_quiet")] = 20 - if len(events) != 2 * sum(expected.values()): - return False - for key, count in expected.items(): - matched = [e for e in events if (e.get("suite"), e.get("assertion")) == key] - if [e.get("status") for e in matched] != ["BEGIN", "PASS"] * count: - return False - remapped = [ - dict(e, suite=e["suite"].replace("XHTTP-", "VLESS-", 1)) for e in events - ] - return vless_events_pass(remapped, assertion, "xhttp") - - -def variants(): - result = {} - result["auto"] = {"mode": "auto"} - result["auto-download"] = {"mode": "auto", "download-settings": {}} - for fault, peer_overrides in { - "session": {"session-placement": "header", "session-key": "X-Required-Session"}, - "sequence": {"seq-placement": "header", "seq-key": "X-Required-Sequence"}, - "payload": { - "uplink-data-placement": "header", - "uplink-data-key": "required-data", - }, - "post-size": {"sc-max-each-post-bytes": "1"}, - }.items(): - result[f"reject-{fault}"] = { - "mode": "packet-up", - "_reject": True, - "_server": peer_overrides, - } - result["post-minimum"] = { - "mode": "packet-up", - "sc-max-each-post-bytes": "1", - "sc-min-posts-interval-ms": "1", - "_tiny": True, - } - result["post-maximum"] = { - "mode": "packet-up", - "sc-max-each-post-bytes": "16777216", - "sc-min-posts-interval-ms": "0-1", - } - for table in ( - "uuid", - "ALPHABET", - "Alphabet", - "BASE36", - "Base62", - "HEX", - "alphabet", - "base36", - "hex", - "number", - "abAB0123", - ): - result[f"session-{table}"] = { - "mode": "packet-up", - "session-table": table, - "sc-min-posts-interval-ms": "1", - "_tiny": True, - } - for mode in ("stream-one", "stream-up", "packet-up"): - result[f"{mode}-headers"] = dict(mode=mode, headers={"X-Probe": "fixture"}) - result["stream-up-no-grpc"] = {"mode": "stream-up", "no-grpc-header": True} - result["stream-one-no-grpc"] = {"mode": "stream-one", "no-grpc-header": True} - result["body-auto"] = { - "mode": "packet-up", - "uplink-data-placement": "auto", - "sc-min-posts-interval-ms": "1", - } - for protocol in ("h2mux", "smux", "yamux"): - for padding in (False, True): - result[f"mux-{protocol}-{'padded' if padding else 'plain'}"] = { - "mode": "packet-up", - "sc-min-posts-interval-ms": "1", - "_mux": { - "enabled": True, - "protocol": protocol, - "padding": padding, - "max-connections": 1, - }, - } - for mode in ("stream-one", "stream-up", "packet-up"): - result[f"{mode}-reuse"] = {"mode": mode, "reuse-settings": {}} - if mode == "packet-up": - result[f"{mode}-reuse"]["sc-min-posts-interval-ms"] = "1" - for mode in ("stream-up", "packet-up"): - result[f"{mode}-split-reuse"] = { - "mode": mode, - "reuse-settings": {"h-max-request-times": "1"}, - "download-settings": { - "alpn": ["http/1.1"], - "headers": {"X-Down": "fixture"}, - "reuse-settings": {}, - }, - } - if mode == "packet-up": - result[f"{mode}-split-reuse"]["sc-min-posts-interval-ms"] = "1" - for placement in ("queryInHeader", "header", "query", "cookie"): - for method in ("repeat-x", "tokenish"): - options = { - "mode": "packet-up", - "x-padding-bytes": "100", - "x-padding-obfs-mode": True, - "x-padding-placement": placement, - "x-padding-method": method, - "sc-min-posts-interval-ms": "1", - } - if placement != "header": - options["x-padding-key"] = "pad" - if placement in ("queryInHeader", "header"): - options["x-padding-header"] = "X-Pad" - result[f"padding-{placement}-{method}"] = options - for placement, method in zip( - ("path", "query", "header", "cookie"), - ("POST", "PUT", "PATCH", "DELETE"), - strict=True, - ): - options = { - "mode": "packet-up", - "session-placement": placement, - "seq-placement": placement, - "uplink-http-method": method, - "session-table": "number", - "session-length": "10", - "sc-min-posts-interval-ms": "1", - } - if placement != "path": - options.update({"session-key": "sid", "seq-key": "seq"}) - result[f"metadata-{placement}-{method}"] = options - for placement in ("header", "cookie"): - result[f"payload-{placement}"] = { - "mode": "packet-up", - "uplink-data-placement": placement, - "uplink-data-key": "data", - "uplink-chunk-size": "64-128", - "sc-min-posts-interval-ms": "1", - } - result[f"payload-{placement}-auto"] = dict( - result[f"payload-{placement}"], **{"uplink-chunk-size": "0"} - ) - for protocol in ("h2mux", "smux", "yamux"): - result[f"mux-{protocol}-padding-required"] = { - "mode": "stream-one", - "_mux": {"enabled": True, "protocol": protocol, "padding": False}, - "_require-padding": True, - "_reject": True, - } - expanded = {} - for version in ("h2", "h1", "h2c", "h1c", "h3", "h2r", "h1r"): - for name, fields in result.items(): - options = copy.deepcopy(fields) - if version == "h3" and "download-settings" in options: - options["download-settings"]["alpn"] = ["h3"] - expanded[f"{version}-{name}"] = (options, version) - if "_mux" in options: - tcp_only = copy.deepcopy(options) - tcp_only["_mux"]["only-tcp"] = True - expanded[f"{version}-{name}-only-tcp"] = (tcp_only, version) - if version == "h3": - expanded["h3-keepalive"] = ( - {"mode": "packet-up", "_keepalive": True}, - version, - ) - if version.endswith("r"): - from .mihomo_extended import PUBLIC_KEY - - replacement = {"public-key": PUBLIC_KEY} - expanded[f"{version}-download-reality-replacement"] = ( - { - "mode": "stream-up", - "download-settings": {"reality-opts": replacement}, - }, - version, - ) - expanded[f"{version}-reject-download-short-id"] = ( - { - "mode": "stream-up", - "download-settings": { - "reality-opts": dict(replacement, **{"short-id": "ff"}) - }, - "_reject": True, - }, - version, - ) - expanded[f"{version}-reject-download-public-key"] = ( - { - "mode": "stream-up", - "download-settings": { - "reality-opts": { - # Valid X25519 encoding, not this server's public key. - "public-key": "CQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", - } - }, - "_reject": True, - }, - version, - ) - for mode in ( - "tcp", - "tcp-tls", - "tcp-reality", - "ws", - "ws-tls", - "ws-reality", - "grpc", - "grpc-tls", - "grpc-reality", - "http", - "http-tls", - "h2", - "h2-tls", - "ws-header", - "ws-header-tls", - "ws-path", - "ws-path-tls", - ): - for name, options in result.items(): - if "_mux" in options: - expanded[f"outer-{mode}-{name}"] = ( - copy.deepcopy(options), - "outer-" + mode, - ) - tcp_only = copy.deepcopy(options) - tcp_only["_mux"]["only-tcp"] = True - expanded[f"outer-{mode}-{name}-only-tcp"] = (tcp_only, "outer-" + mode) - return expanded - - -def server_fields(fields, kind): - if kind == "M": - return fields - names = { - "path": "path", - "mode": "mode", - "host": "host", - "no-grpc-header": "noGRPCHeader", - "x-padding-bytes": "xPaddingBytes", - "x-padding-obfs-mode": "xPaddingObfsMode", - "x-padding-key": "xPaddingKey", - "x-padding-header": "xPaddingHeader", - "x-padding-placement": "xPaddingPlacement", - "x-padding-method": "xPaddingMethod", - "uplink-http-method": "uplinkHTTPMethod", - "session-placement": "sessionPlacement", - "session-key": "sessionKey", - "seq-placement": "seqPlacement", - "seq-key": "seqKey", - "uplink-data-placement": "uplinkDataPlacement", - "uplink-data-key": "uplinkDataKey", - "uplink-chunk-size": "uplinkChunkSize", - "sc-max-each-post-bytes": "scMaxEachPostBytes", - } - if not fields.keys() <= names.keys(): - raise ValueError("missing explicit Xray field translation") - return {names[key]: value for key, value in fields.items()} - - -def events_pass(events, assertion, *, owned=False): - if any(e.get("schema_version") != 1 for e in events): - return False - main = [event for event in events if event.get("suite") == "XHTTP-XHTTP"] - if ( - len(main) != 2 - or [e.get("status") for e in main] != ["BEGIN", "PASS"] - or any(e.get("assertion") != assertion for e in main) - ): - return False - if not owned: - return len(events) == 2 - cycles = [e for e in events if e.get("suite") == "XHTTP-OWNED"] - if len(events) != 42 or len(cycles) != 40: - return False - for start, finish in zip(cycles[::2], cycles[1::2], strict=True): - if ( - start.get("status") != "BEGIN" - or start.get("assertion") != "stop_and_remain_quiet" - or finish.get("status") != "PASS" - or finish.get("assertion") != "stop_and_remain_quiet" - or finish.get("seconds", 0) < 5 - or not idle_resources(finish.get("resources")) - ): - return False - points = finish.get("checkpoints", []) - if len(points) != 3 or [p.get("phase") for p in points] != [ - "baseline", - "after-stop", - "quiet", - ]: - return False - if not all(idle_resources(p.get("resources")) for p in points): - return False - if points[1]["resources"] != points[2]["resources"]: - return False - return True - - -def close_reference(lab, stack, root, name, node, origin, binary, output): - comparison_dir = root / "comparison" - comparison_dir.mkdir() - shutil.copy2(binary, comparison_dir / "peer") - reference = copy.deepcopy(node) - reference["name"] = "reference" - reference.pop("smux", None) - if reference.get("reality-opts"): - # Required by the official Mihomo client's uTLS REALITY backend. This - # belongs only to the reference fixture, not VCore's public config. - reference["client-fingerprint"] = "chrome" - config = { - "socks-port": 23002, - "allow-lan": True, - "bind-address": "*", - "log-level": "warning", - "ipv6": True, - "proxies": [reference], - "rules": ["MATCH,reference"], - } - (comparison_dir / "config.json").write_text(json.dumps(config)) - comparison = lab.start( - stack, - comparison_dir, - name + "-comparison", - ["/data/fixture/peer", "-d", "/data", "-f", "/data/fixture/config.json"], - ) - comparison.release() - comparison.wait_tcp(23002) - result = run_command( - [ - "container", - "exec", - origin.name, - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/close.py", - comparison.ipv4, - origin.ipv4, - ], - timeout=30, - ) - (output / f"{name}-close-command.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - (output / f"{name}-comparison.log").write_text( - redact(command("logs", comparison.name)[-65536:]) - ) - if result.returncode or not result.cleanup: - raise RuntimeError("official XHTTP close comparison failed") - observed = json.loads(result.stdout) - observed["scope"] = "same-mode" - (output / f"{name}-close-reference.json").write_text(json.dumps(observed) + "\n") - comparison.ensure_alive() - return observed - - -def selected_profile_variants(profile): - """Named-profile download-leg crossings; ordinary XHTTP variants are unchanged.""" - other = "firefox" if profile in {"chrome", "chrome120"} else "chrome" - result = {} - for label, override in ( - ("inherit", {}), - ("different", {"client-fingerprint": other}), - ("clear", {"client-fingerprint": "none"}), - ("reject-pin", {"client-fingerprint": other, "fingerprint": "11" * 32}), - ): - options = { - "mode": "stream-up", - "reuse-settings": {}, - "download-settings": { - "alpn": ["http/1.1"], - "reuse-settings": {}, - **override, - }, - } - if label == "reject-pin": - options["_reject"] = True - result[f"h2-selected-download-{label}"] = (options, "h2") - result["h3-selected-download-clear"] = ( - { - "mode": "stream-up", - "_main_alpn": "h2", - "download-settings": {"alpn": ["h3"], "client-fingerprint": "none"}, - }, - "h3", - ) - return result - - -def selected_gateway_config(upstream): - """Both HTTP versions reach one native XHTTP session table, with real mTLS.""" - config = gateway_config(upstream) - config["apps"]["http"]["servers"]["fixture"]["protocols"] = ["h2", "h3"] - return config - - -def run( - output: Path, - selected: list[str], - *, - udp: bool = False, - owned: bool = False, - public: str | None = None, - jobs: dict | None = None, - supplied: dict | None = None, - client_fingerprint: str | None = None, - encryption: str | None = None, - ech: bool = False, - jls: bool = False, -): - if ech and jls: - raise ValueError("ECH and JLS are mutually exclusive") - if client_fingerprint is not None and client_fingerprint not in CLIENT_FINGERPRINTS: - raise ValueError("unsupported named client profile") - if public is not None and (public not in PUBLIC_TESTS or udp or owned): - raise ValueError("invalid public consumer selection") - cases = variants() - if ech: - from .protocol_ech import field_variants - - cases.update(field_variants()) - if encryption is not None: - from .protocol_encryption import cases as encryption_profiles - - if encryption not in encryption_profiles(): - raise ValueError("unsupported Encryption fixture") - if encryption is not None or ech: - # Official V2Ray terminates only the legacy transport. A separate - # official Mihomo handler authenticates Encryption and decodes VLESS. - for mode in ("http", "h2", "ws-header", "ws-path"): - for suffix in ("", "-tls"): - name = f"outer-{mode}{suffix}" - cases[name] = ({}, name) - if client_fingerprint: - cases.update(selected_profile_variants(client_fingerprint)) - if jobs is not None: - if set(jobs) != set(selected) or any(not entries for entries in jobs.values()): - raise ValueError("invalid frozen native jobs") - allowed = set(PUBLIC_TESTS) | { - "native_xhttp_request_fields", - "native_xhttp_udp_encodings", - "lifecycle::native_xhttp_owned_resources", - "keepalive::native_h3_keepalive_observes_each_leg_and_stop", - "security::native_xhttp_security", - "close::native_mihomo_xhttp_close", - } - ids = [entry["case_id"] for entries in jobs.values() for entry in entries] - if len(ids) != len(set(ids)) or any( - entry["test"] not in allowed - for entries in jobs.values() - for entry in entries - ): - raise ValueError("unknown or duplicate frozen native job") - if not selected: - selected = list(cases) - if len(selected) != len(set(selected)) or not set(selected) <= cases.keys(): - raise ValueError("invalid XHTTP field case selection") - if ech and any( - ( - cases[name][1] not in {"h1", "h2", "h3"} - and not ( - cases[name][1].startswith("outer-") and cases[name][1].endswith("-tls") - ) - ) - or "_main_alpn" in cases[name][0] - for name in selected - ): - raise ValueError("static ECH requires a native standard TLS peer") - if jls and any(cases[name][1] not in {"h1", "h2"} for name in selected): - raise ValueError("JLS field gate requires a Mihomo stream TLS peer") - if client_fingerprint is not None: - for name in selected: - options, version = cases[name] - if version.startswith("outer-") or version.endswith("c"): - raise ValueError("profile field requires a TLS XHTTP node") - if ( - client_fingerprint != "none" - and version == "h3" - and "_main_alpn" not in options - ): - raise ValueError("named client profiles require stream TLS") - if ( - not output.is_relative_to(CORE_DIR / "target/interop/runs") - or output == CORE_DIR / "target/interop/runs" - ): - raise ValueError("new run directory must be directly under target/interop/runs") - output.mkdir(exist_ok=False) - report = dict( - stage="SECURITY.ech" - if ech - else "SECURITY.encryption" - if encryption - else "XHTTP", - ech=ech, - jls=jls, - encryption_profile=encryption, - client_fingerprint=client_fingerprint, - scope="protocol-consumer" - if jobs is not None - else "request-fields-development-only", - traffic=public - or ("owned-resources" if owned else "udp-three-encodings" if udp else "tcp"), - source=source_identity(), - status="NOT RUN", - cases=[], - peers={}, - isolation={}, - ) - try: - artifacts = {} - if any("_main_alpn" in cases[name][0] for name in selected): - gateway_artifact = build_caddy(output / "Caddy") - artifacts["Caddy"] = gateway_artifact.binary - report["peers"]["Caddy"] = gateway_artifact.identity - kinds = {native_kind(cases[name][1]) for name in selected} - if ech and any(cases[name][1].startswith("outer-") for name in selected): - kinds.add("XR") - if ( - encryption - or jobs is not None - or any( - native_kind(cases[name][1]) != "M" - and (udp or owned or public or "_mux" in cases[name][0]) - for name in selected - ) - ): - kinds.add("M") - for kind in sorted(kinds): - if supplied is not None: - binary, identity = supplied[kind] - elif kind == "M": - identity = {} - binary = download_mihomo( - "linux-arm64", directory=output / "binary", identity=identity - ) - else: - artifact = download_native( - kind, output / kind, "linux-arm64", defer_version=True - ) - binary, identity = artifact.binary, artifact.identity - artifacts[kind] = binary - report["peers"][kind] = identity - lab = ContainerLab(report["isolation"], mtu=1500) - build = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "xhttp_native", - *(["--test", "vless_public"] if public or jobs is not None else []), - "--no-run", - ], - timeout=180, - ) - (output / "build.log").write_text(redact(build.stdout.decode(errors="replace"))) - if build.returncode or not build.cleanup: - raise RuntimeError("XHTTP field test build failed") - for name in selected: - print(name, flush=True) - fields, version = cases[name] - kind = native_kind(version) - entries = jobs[name] if jobs is not None else None - needs_public = bool(public) or bool( - entries and any(e["test"] in PUBLIC_TESTS for e in entries) - ) - hop_tests = { - "runtime::public_graph", - "runtime::grpc_pool_keeps_physical_selection_until_new_transport", - } - needs_hop = public in hop_tests or bool( - entries and any(e["test"] in hop_tests for e in entries) - ) - needs_decoder = ( - bool(encryption) - or udp - or owned - or needs_public - or bool( - entries - and any( - e["test"] == "native_xhttp_udp_encodings" - or e["test"].startswith("lifecycle::") - for e in entries - ) - ) - ) - needs_close = bool( - entries and any(e["test"].startswith("close::") for e in entries) - ) - outer_mode = ( - version.removeprefix("outer-") if version.startswith("outer-") else None - ) - identity = report["peers"][kind] - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - server_dir, origin_dir = root / "server", root / "origin" - server_dir.mkdir() - origin_dir.mkdir() - shutil.copy2(artifacts[kind], server_dir / "peer") - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - if needs_close: - shutil.copyfile( - Path(__file__).with_name("container_close_client.py"), - origin_dir / "close.py", - ) - origin_security = [] - if version.endswith(("r", "-reality")) or jls: - origin_cert, origin_key, _ = certificates(origin_dir) - origin_security = [ - f"VCORE_ORIGIN_CERT=/data/fixture/{origin_cert.name}", - f"VCORE_ORIGIN_KEY=/data/fixture/{origin_key.name}", - ] - origin = lab.start( - stack, - origin_dir, - name + "-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - *origin_security, - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - server_dir, - name + "-server", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ], - ) - version_text = command( - "exec", - server.name, - "/data/fixture/peer", - "-v" if kind == "M" else "version", - ).strip() - digest = command( - "exec", server.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if not version_text or digest != identity["binary_sha256"]: - raise RuntimeError("XHTTP peer identity mismatch") - identity["version"] = version_text - cert, key, pin = certificate_chain(server_dir) - # Xray always normalizes its handler path with a trailing slash, - # unlike Mihomo when both metadata fields are outside the path. - # Configure an explicit compatible path, without changing VCore. - options = dict(fields, path="/xhttp/" if kind == "XR" else "/xhttp") - plain = ( - not outer_mode.endswith(("-tls", "-reality")) - if outer_mode - else version.endswith("c") - ) - reality = version.endswith(("r", "-reality")) - tiny = options.pop("_tiny", False) - keepalive = options.pop("_keepalive", False) - reject = options.pop("_reject", False) - peer_overrides = options.pop("_server", {}) - main_alpn = options.pop("_main_alpn", None) - mux = options.pop("_mux", None) - require_padding = options.pop("_require-padding", False) - ech_action = options.pop("_ech_action", None) - decoder = None - encryption_node = {} - if kind != "M" and (needs_decoder or mux is not None): - decoder_dir = root / "decoder" - decoder_dir.mkdir() - shutil.copy2(artifacts["M"], decoder_dir / "peer") - listener = dict( - name="decoder", - type="vless", - listen="::", - port=23001, - users=[dict(uuid=CLIENT_ID)], - **{"allow-insecure": True}, - ) - if mux is not None: - listener["mux-option"] = { - "padding": require_padding or mux["padding"] - } - if encryption: - from .protocol_encryption_public import ( - configuration as encryption_config, - ) - - encryption_config( - encryption, encryption_node, {"listeners": [listener]} - ) - (decoder_dir / "config.json").write_text( - json.dumps( - dict( - ipv6=True, - hosts={ - "vcore-fixture.test": origin.ipv4, - "peer.fixture.test": server.ipv4, - }, - listeners=[listener], - rules=["MATCH,DIRECT"], - **{"log-level": "warning"}, - ) - ) - ) - decoder = lab.start( - stack, - decoder_dir, - name + "-decoder", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - decoded_version = command( - "exec", decoder.name, "/data/fixture/peer", "-v" - ).strip() - decoded_digest = command( - "exec", decoder.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if ( - not decoded_version - or decoded_digest != report["peers"]["M"]["binary_sha256"] - ): - raise RuntimeError("layered decoder identity mismatch") - report["peers"]["M"]["version"] = decoded_version - decoder.release() - decoder.wait_tcp(23001) - node = dict( - name="peer", - type="vless", - server=server.ipv4, - port=23000, - uuid=CLIENT_ID, - tls=not plain, - alpn=[ - "http/1.1" - if version.startswith("h1") - else "h3" - if version == "h3" - else "h2" - ], - **{"xhttp-opts": options}, - network="xhttp", - udp=True, - ) - if mux is not None: - node["smux"] = mux - if not plain: - node.update(servername="localhost", fingerprint=pin) - if client_fingerprint: - node["client-fingerprint"] = client_fingerprint - if main_alpn: - node["alpn"] = [main_alpn] - config = peer_config( - kind, - f"/data/fixture/{cert.name}", - f"/data/fixture/{key.name}", - plain=plain, - decoder=decoder.ipv4 if decoder else None, - ) - gateway = None - edge = server - if main_alpn: - # Xray's native listener is either TCP or QUIC, not both. - # The approved gateway terminates both TLS legs and feeds - # ONE unmodified Xray h2c handler/session table. - stream = config["inbounds"][0]["streamSettings"] - stream["security"] = "none" - del stream["tlsSettings"] - gateway_dir = root / "gateway" - gateway_dir.mkdir() - shutil.copy2(artifacts["Caddy"], gateway_dir / "peer") - for filename in ("cert.pem", "key.pem", "root.pem", "root-key.pem"): - shutil.copyfile(server_dir / filename, gateway_dir / filename) - node.update(client_identities(gateway_dir)["valid"]) - options["host"] = "localhost" - (gateway_dir / "config.json").write_text( - json.dumps(selected_gateway_config(server.ipv4)) - ) - gateway = lab.start( - stack, - gateway_dir, - name + "-gateway", - [ - "env", - "XDG_CONFIG_HOME=/data/config", - "XDG_DATA_HOME=/data/share", - "/data/fixture/peer", - "run", - "--config", - "/data/fixture/config.json", - ], - ) - gateway_version = command( - "exec", gateway.name, "/data/fixture/peer", "version" - ).strip() - gateway_digest = command( - "exec", gateway.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if ( - not gateway_version - or gateway_digest != report["peers"]["Caddy"]["binary_sha256"] - ): - raise RuntimeError("selected gateway identity mismatch") - report["peers"]["Caddy"]["version"] = gateway_version - - def preserve_gateway(peer=gateway, variant=name): - (output / f"{variant}-gateway.log").write_text( - sanitized_gateway_log(command("logs", peer.name)[-65536:]) - ) - - stack.callback(preserve_gateway) - edge = gateway - node["server"] = edge.ipv4 - if outer_mode: - node, config = vless_configuration( - outer_mode, - server.ipv4, - origin.ipv4, - Path("/data/fixture") / cert.name, - Path("/data/fixture") / key.name, - ) - node["name"] = "peer" - if mux is not None: - node["smux"] = mux - if not plain and not reality: - node["fingerprint"] = pin - if decoder: - config["inbounds"][0].update( - protocol="dokodemo-door", - settings=dict( - address=decoder.ipv4, port=23001, network="tcp" - ), - ) - if kind == "M": - config["hosts"] = { - "vcore-fixture.test": origin.ipv4, - "peer.fixture.test": server.ipv4, - } - if mux is not None: - config["listeners"][0]["mux-option"] = { - "padding": require_padding or mux["padding"] - } - if reality: - from .mihomo_extended import PRIVATE_KEY, PUBLIC_KEY, SHORT_ID - - listener = config["listeners"][0] - listener.pop("certificate", None) - listener.pop("private-key", None) - listener["reality-config"] = { - "dest": f"{origin.ipv4}:24001", - "private-key": PRIVATE_KEY, - "short-id": [SHORT_ID, ""], - "server-names": ["localhost"], - } - node.pop("fingerprint", None) - node["reality-opts"] = { - "public-key": PUBLIC_KEY, - "short-id": SHORT_ID, - } - else: - config["dns"] = {"hosts": {"vcore-fixture.test": origin.ipv4}} - config["outbounds"][0]["settings"] = {"domainStrategy": "UseIP"} - if encryption: - if decoder: - node.update(encryption_node) - else: - from .protocol_encryption_public import ( - configuration as encryption_config, - ) - - encryption_config(encryption, node, config) - if jls: - from .protocol_jls import configuration as jls_config - - jls_config(node, config, origin.ipv4, name) - if ech: - from .protocol_ech import configuration as ech_config - from .protocol_ech import ( - legacy_gateway_command, - legacy_gateway_config, - wrong_key, - ) - - ech_peer, ech_dir, ech_kind = config, server_dir, kind - if outer_mode: - # ECH is terminated by official Xray, not a custom TLS - # server. V2Ray remains the native legacy transport; - # the existing Mihomo decoder owns VLESS/Encryption. - stream = config["inbounds"][0]["streamSettings"] - tls_options = stream.pop("tlsSettings") - stream["security"] = "none" - gateway_dir = root / "gateway" - gateway_dir.mkdir() - shutil.copy2(artifacts["XR"], gateway_dir / "peer") - for filename in (cert.name, key.name): - shutil.copyfile( - server_dir / filename, gateway_dir / filename - ) - ech_peer = legacy_gateway_config(server.ipv4, tls_options) - ech_dir, ech_kind = gateway_dir, "XR" - ech_material = ech_config( - node, ech_peer, ech_dir, name, kind=ech_kind - ) - if outer_mode: - (gateway_dir / "config.json").write_text(json.dumps(ech_peer)) - gateway = lab.start( - stack, - gateway_dir, - name + "-ech-gateway", - legacy_gateway_command(), - ) - version_text = command( - "exec", gateway.name, "/data/fixture/peer", "version" - ).strip() - digest = command( - "exec", gateway.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if ( - not version_text - or digest != report["peers"]["XR"]["binary_sha256"] - ): - raise RuntimeError("ECH gateway identity mismatch") - report["peers"]["XR"]["version"] = version_text - edge = gateway - node["server"] = edge.ipv4 - - def preserve_ech_gateway(peer=gateway, variant=name): - (output / f"{variant}-gateway.log").write_text( - redact(command("logs", peer.name)[-65536:]) - ) - - stack.callback(preserve_ech_gateway) - if ech_action in {"replace", "clear", "reject-download"}: - replacement = ( - {} - if ech_action == "clear" - else dict( - enable=True, - config=ech_material["ech_download_config"], - ) - ) - if ech_action == "reject-download": - replacement = wrong_key(replacement) - options["download-settings"]["ech-opts"] = replacement - elif ech_action == "reject-main": - node["ech-opts"] = wrong_key(node["ech-opts"]) - # Client-only session generator and pacing have no server counterpart. - peer_options = { - k: v - for k, v in options.items() - if k - not in { - "session-table", - "session-length", - "sc-min-posts-interval-ms", - "headers", - "reuse-settings", - "download-settings", - } - } - peer_options.update(peer_overrides) - if outer_mode: - pass # Native outer transport options were supplied above. - elif kind == "M": - config["listeners"][0]["xhttp-config"].update( - server_fields(peer_options, kind) - ) - else: - config["inbounds"][0]["streamSettings"]["xhttpSettings"].update( - server_fields(peer_options, kind) - ) - (server_dir / "config.json").write_text(json.dumps(config)) - hop = None - if needs_hop: - # An upstream must not dial a VLESS listener in its own - # Mihomo process: that correctly triggers loopback protection. - upstream_dir = root / "upstream" - upstream_dir.mkdir() - shutil.copy2(artifacts["M"], upstream_dir / "peer") - (upstream_dir / "config.json").write_text( - json.dumps( - dict( - ipv6=True, - hosts={ - "peer.fixture.test": server.ipv4, - "vcore-fixture.test": origin.ipv4, - }, - listeners=[ - dict( - name="hop", - type="socks", - listen="::", - port=23004, - udp=True, - ) - ], - rules=["MATCH,DIRECT"], - **{"log-level": "warning"}, - ) - ) - ) - upstream = lab.start( - stack, - upstream_dir, - name + "-upstream", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - upstream.release() - upstream.wait_tcp(23004) - hop = dict( - name="hop", - type="socks5", - server=upstream.ipv4, - port=23004, - udp=True, - ) - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - node=node, - tiny=tiny, - reject=reject, - peer_kind=f"{kind}-to-M" - if encryption and decoder - else kind, - data_dir=str(root / "data"), - server_ipv4=edge.ipv4, - server_ipv6=edge.ipv6, - hop=hop, - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - root_der=list( - ssl.PEM_cert_to_DER_cert( - (server_dir / "root.pem").read_text() - ) - ), - ) - ) - ) - origin.release() - server.release() - if gateway: - gateway.release() - origin.wait_tcp(24000) - if kind == "M" or gateway: - server.wait_tcp(23000) - if gateway: - gateway.wait_tcp(23000) - default_assertion = ( - public - or ( - "keepalive::native_h3_keepalive_observes_each_leg_and_stop" - if keepalive - else None - ) - or ("security::native_xhttp_security" if reject else None) - or ( - "lifecycle::native_xhttp_owned_resources" - if owned - else "native_xhttp_udp_encodings" - if udp - else "native_xhttp_request_fields" - ) - ) - if needs_close: - try: - reference = close_reference( - lab, stack, root, name, node, origin, artifacts["M"], output - ) - finally: - # A failed reference setup precedes the VCore cases; - # preserve its peer evidence before ExitStack cleanup. - for role, peer in (("server", server), ("origin", origin)): - (output / f"{name}-{role}.log").write_text( - redact(command("logs", peer.name)[-65536:]) - ) - data = json.loads(fixture.read_text()) - data["close_reference"] = reference - fixture.write_text(json.dumps(data)) - for entry in entries or [dict(case_id=name, test=default_assertion)]: - case_id, assertion = entry["case_id"], entry["test"] - events = output / f"{case_id}-events.jsonl" - command_line = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_public" if assertion in PUBLIC_TESTS else "xhttp_native", - assertion, - "--", - "--exact", - "--ignored", - "--nocapture", - ] - result = run_command( - command_line, - timeout=240 - if assertion.startswith("lifecycle::") - or assertion in PUBLIC_TESTS - else 150, - env=dict( - os.environ, - VCORE_XHTTP_INPUT=str(fixture), - VCORE_VLESS_INPUT=str(fixture), - VCORE_PROTOCOL_STAGE="XHTTP", - VCORE_CASE_EVENTS=str(events), - ), - ) - (output / f"{case_id}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observed = read_events(events) if events.exists() else [] - passed = ( - result.returncode == 0 - and result.cleanup - and ( - public_events_pass(observed, assertion) - if assertion in PUBLIC_TESTS - else events_pass( - observed, - assertion, - owned=assertion.startswith("lifecycle::"), - ) - ) - ) - report["cases"].append( - dict( - case_id=case_id, - variant=name, - assertion=assertion, - status="PASS" if passed else "FAIL", - command=command_line, - exit_code=result.returncode, - seconds=result.seconds, - command_cleanup=result.cleanup, - decoder_path=( - ( - "Xray ECH -> " - if ech and outer_mode - else "Caddy H2/H3 -> " - if gateway - else "" - ) - + f"{kind} {outer_mode or 'XHTTP'} -> Mihomo VLESS" - ) - if decoder - else kind, - ) - ) - print(f"{case_id}: {'PASS' if passed else 'FAIL'}", flush=True) - if not passed: - break - for role, peer in (("server", server), ("origin", origin)): - peer.ensure_alive() - (output / f"{name}-{role}.log").write_text( - redact(command("logs", peer.name)[-65536:]) - ) - if decoder: - decoder.ensure_alive() - if gateway: - gateway.ensure_alive() - if needs_hop: - upstream.ensure_alive() - (output / f"{name}-upstream.log").write_text( - redact(command("logs", upstream.name)[-65536:]) - ) - if not passed: - break - report["status"] = ( - "PASS" - if len(report["cases"]) - == (sum(map(len, jobs.values())) if jobs is not None else len(selected)) - and all(c["status"] == "PASS" for c in report["cases"]) - else "FAIL" - ) - except (OSError, ValueError, RuntimeError) as error: - report.update( - status="FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - except KeyboardInterrupt: - report.update(status="INTERRUPTED") - finally: - report["cleanup"] = all( - p["joined"] for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = ( - source_identity()["source_tree_sha256"] - == report["source"]["source_tree_sha256"] - ) - if not report["cleanup"] or not report["source_unchanged"]: - report["status"] = "FAIL" - (output / "xhttp-fields-results.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return 0 if report["status"] == "PASS" else 1 - - -if __name__ == "__main__": - import argparse - import sys - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("cases", nargs="*") - parser.add_argument("--udp", action="store_true") - parser.add_argument("--owned", action="store_true") - parser.add_argument("--public", choices=PUBLIC_TESTS) - parser.add_argument("--encryption") - parser.add_argument("--ech", action="store_true") - args = parser.parse_args() - if sum((args.udp, args.owned, args.public is not None)) > 1: - parser.error("select UDP or owned-resource checks") - with exclusive_run(): - sys.exit( - run( - args.output.resolve(), - args.cases, - udp=args.udp, - owned=args.owned, - public=args.public, - encryption=args.encryption, - ech=args.ech, - ) - ) diff --git a/scripts/src/vcore_scripts/protocol_xhttp_gateway.py b/scripts/src/vcore_scripts/protocol_xhttp_gateway.py deleted file mode 100644 index 4068eaf..0000000 --- a/scripts/src/vcore_scripts/protocol_xhttp_gateway.py +++ /dev/null @@ -1,564 +0,0 @@ -"""Isolated xcaddy H3/mTLS -> one native Xray XHTTP handler probe. - -Native comparator evidence only: this never claims VCore XHTTP acceptance. -""" - -from __future__ import annotations - -import contextlib -import hashlib -import ipaddress -import json -import select -import shutil -import socket -import struct -import tempfile -import time -from pathlib import Path - -from .builds import CORE_DIR -from .caddy_build import build_caddy -from .container_close_client import exact -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import PeerArtifact, download_native -from .protocol_containers import ContainerLab, command, listing -from .protocol_fixtures import certificate_chain -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vless_security import client_identities -from .protocol_xhttp_peers import client_config, exercise, peer_config, socks - - -def sanitized_gateway_log(text): - """Keep native protocol evidence, not HTTP headers, identities or destinations.""" - entries = [] - events = { - "aborting with incomplete response": "response_canceled", - "strict SNI-Host enforcement": "strict_sni_host", - "admin endpoint disabled": "admin_disabled", - "unable to create folder for config autosave": "autosave_failed", - "unable to get instance ID": "storage_id_failed", - } - for line in text.splitlines(): - try: - row = json.loads(line) - except ValueError: - entries.append({"event": "non_json_log", "omitted": True}) - continue - message = row.get("msg", "") - entry = { - "level": row.get("level"), - "event": next( - (name for pattern, name in events.items() if pattern in message), - "other_native_event", - ), - } - request = row.get("request", {}) - if request: - entry.update( - http_version=request.get("proto"), - alpn=request.get("tls", {}).get("proto"), - client_certificate_present=bool( - request.get("tls", {}).get("client_common_name") - ), - ) - entries.append(entry) - return "".join(json.dumps(entry) + "\n" for entry in entries) - - -def gateway_config(upstream): - address = ipaddress.ip_address(upstream) - authority = f"[{address}]" if address.version == 6 else str(address) - return { - "admin": {"disabled": True}, - "storage": {"module": "file_system", "root": "/data/caddy"}, - "logging": {"logs": {"default": {"level": "WARN"}}}, - "apps": { - "tls": { - "certificates": { - "load_files": [ - { - "certificate": "/data/fixture/cert.pem", - "key": "/data/fixture/key.pem", - } - ] - } - }, - "http": { - "servers": { - "fixture": { - "listen": [":23000"], - "protocols": ["h3"], - "automatic_https": {"disable": True}, - "tls_connection_policies": [ - { - "client_authentication": { - "mode": "require_and_verify", - "ca": { - "provider": "file", - "pem_files": ["/data/fixture/root.pem"], - }, - } - } - ], - "routes": [ - { - "handle": [ - { - "handler": "reverse_proxy", - "upstreams": [{"dial": f"{authority}:23000"}], - "transport": { - "protocol": "http", - "versions": ["h2c"], - "compression": False, - }, - } - ] - } - ], - } - } - }, - }, - } - - -def cases(*, identities_only=False): - for mode in ("packet-up", "stream-up", "stream-one"): - for split in (False, True) if mode != "stream-one" else (False,): - prefix = mode + ("-download" if split else "") - checks = ( - ("echo",) - if identities_only - else ("echo", "bulk-server-first", "close", "udp") - ) - for check in checks: - yield dict(id=f"{prefix}-{check}", mode=mode, split=split, check=check) - if split or identities_only: - for leg in ("upload", "download") if split else ("shared",): - for identity in ("absent", "expired", "wrong-ca"): - yield dict( - id=f"{prefix}-{leg}-{identity}", - mode=mode, - split=split, - check="reject", - leg=leg, - identity=identity, - ) - - -def native_config(): - config = peer_config("XR", "unused", "unused") - stream = config["inbounds"][0]["streamSettings"] - stream["security"] = "none" - del stream["tlsSettings"] - # This ONE handler owns the upload and download session table. Caddy does - # not decode XHTTP or VLESS; h2c avoids H1 full-duplex/buffering differences. - return config - - -def stream_check(proxy, port, origin, *, bulk): - with socket.create_connection((origin, 24000), timeout=15) as control: - control.sendall(b"\x0a" if bulk else b"\x0b") - target = struct.unpack("!H", exact(control, 2))[0] - stream, _ = socks(proxy, port, 1, origin, target) - with stream: - stream.settimeout(15) - if exact(stream, 5) != b"hello": - raise ValueError("missing native server-first data") - size = 10 * 1024 * 1024 if bulk else 4 - payload = b"\x5a" * size if bulk else b"ping" - stream.sendall(payload) - observed = hashlib.sha256() - remaining = size - while remaining: - data = exact(stream, min(65536, remaining)) - observed.update(data) - remaining -= len(data) - expected = hashlib.sha256(payload).hexdigest() - if observed.hexdigest() != expected: - raise ValueError("gateway data mismatch") - if bulk and exact(stream, 7) != b"trailer": - raise ValueError("missing complete native reply") - stream.settimeout(5) - started = time.monotonic() - stream.shutdown(socket.SHUT_WR) - tail = bytearray() - try: - while data := stream.recv(1024): - tail.extend(data) - if len(tail) > 1024: - raise ValueError("gateway close exceeded response bound") - except ConnectionResetError: - pass - control.settimeout(5) - if exact(control, 2) != b"AD": - raise ValueError("native origin did not finish") - return dict( - bytes_each_way=size, - sha256=expected, - server_first=True, - closed=True, - close_seconds=round(time.monotonic() - started, 3), - tail_after_upload_eof=len(tail), - origin_finished=True, - ) - - -def rejected_identity(proxy, port, origin): - with socket.create_connection((origin, 24000), timeout=5) as control: - control.sendall(b"\x0c") - target = struct.unpack("!H", exact(control, 2))[0] - connected = False - outcome = "closed" - try: - stream, _ = socks(proxy, port, 1, origin, target) - with stream: - stream.sendall(b"synthetic-probe") - connected = exact(stream, 2) == b"ok" - except TimeoutError: - outcome = "timeout" # Never infer rejection from a blackhole. - except (OSError, ValueError): - pass - ready, _, _ = select.select([control], [], [], 0.3) - return dict( - rejected=not connected and not ready and outcome == "closed", - data_exchanged=connected, - origin_accepted=bool(ready), - client_outcome=outcome, - quiet_seconds=0.3, - ) - - -def run(output: Path, *, identities_only=False): - output = output.resolve() - if output.parent != (CORE_DIR / "target/interop/runs").resolve(): - raise ValueError("gateway output must be a fresh target/interop/runs child") - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="XHTTP-PEERS", - purpose="XHTTP-xcaddy-H3-mTLS-prerequisite", - vcore_acceptance=False, - identities_only=identities_only, - source=source_identity(), - status="NOT RUN", - phase="build", - cases=[], - peers={}, - isolation={}, - topology=( - "Mihomo-H3-client -> Caddy-H3-mTLS -> one-Xray-h2c-XHTTP-handler -> origin" - ), - ) - try: - built = build_caddy(output / "binaries/Caddy") - report["phase"] = "download" - identity = {} - mihomo = PeerArtifact( - download_mihomo( - "linux-arm64", - directory=output / "binaries/M", - identity=identity, - ), - identity, - ) - xray = download_native( - "XR", output / "binaries/XR", "linux-arm64", defer_version=True - ) - artifacts = {"Caddy": built, "M": mihomo, "XR": xray} - report["peers"] = { - kind: artifact.identity for kind, artifact in artifacts.items() - } - lab = ContainerLab(report["isolation"], mtu=1500) - report["phase"] = "start" - with tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary: - root = Path(temporary) - directories = { - role: root / role for role in ("origin", "gateway", "server", "client") - } - for directory in directories.values(): - directory.mkdir() - origin_dir, gateway_dir, server_dir, client_dir = directories.values() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - shutil.copyfile( - Path(__file__).with_name("container_quic_observer.py"), - gateway_dir / "observe.py", - ) - for role, artifact in ( - ("gateway", built), - ("server", xray), - ("client", mihomo), - ): - shutil.copyfile(artifact.binary, directories[role] / "peer") - (directories[role] / "peer").chmod(0o755) - _, _, pin = certificate_chain(gateway_dir) - identities = client_identities(gateway_dir) - identities["absent"] = {"certificate": "", "private-key": ""} - second = root / "second-identity" - second.mkdir() - for name in ("root.pem", "root-key.pem"): - shutil.copyfile(gateway_dir / name, second / name) - download_identity = client_identities(second)["valid"] - (server_dir / "config.json").write_text(json.dumps(native_config())) - with contextlib.ExitStack() as stack: - origin = lab.start( - stack, - origin_dir, - "xhttp-gateway-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - server = lab.start( - stack, - server_dir, - "xhttp-gateway-xray", - [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ], - ) - (gateway_dir / "config.json").write_text( - json.dumps(gateway_config(server.ipv4)) - ) - gateway = lab.start( - stack, - gateway_dir, - "xhttp-gateway-caddy", - [ - "env", - "XDG_CONFIG_HOME=/data/config", - "XDG_DATA_HOME=/data/share", - *(["QLOGDIR=/data/qlog"] if identities_only else []), - "/data/fixture/peer", - "run", - "--config", - "/data/fixture/config.json", - ], - ) - selected = list(cases(identities_only=identities_only)) - config = dict( - ipv6=True, - **{"log-level": "warning"}, - proxies=[], - listeners=[], - rules=["MATCH,REJECT"], - ) - for index, case in enumerate(selected): - candidate = client_config( - gateway.ipv4, pin, "h3", case["mode"], download=case["split"] - ) - node, listener = candidate["proxies"][0], candidate["listeners"][0] - node.update(identities["valid"]) - node["xhttp-opts"]["host"] = "localhost" - if case["split"]: - node["xhttp-opts"]["download-settings"].update( - download_identity - ) - if case["check"] == "reject": - target = ( - node - if case["leg"] in ("upload", "shared") - else node["xhttp-opts"]["download-settings"] - ) - target.update(identities[case["identity"]]) - node["name"] = listener["name"] = listener["proxy"] = case["id"] - listener["port"] = 23100 + index - config["proxies"].append(node) - config["listeners"].append(listener) - (client_dir / "config.json").write_text(json.dumps(config)) - client = lab.start( - stack, - client_dir, - "xhttp-gateway-mihomo", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - - def preserve_logs(): - # Snapshot before deletion, independent of log-follow buffering. - for role, peer in ( - ("gateway", gateway), - ("server", server), - ("client", client), - ): - (output / f"{role}.log").write_text( - sanitized_gateway_log(command("logs", peer.name)) - if role == "gateway" - else redact(command("logs", peer.name)) - ) - - stack.callback(preserve_logs) - for kind, peer in (("Caddy", gateway), ("XR", server), ("M", client)): - version = command( - "exec", - peer.name, - "/data/fixture/peer", - "-v" if kind == "M" else "version", - ).strip() - digest = command( - "exec", peer.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if not version or digest != report["peers"][kind]["binary_sha256"]: - raise RuntimeError("container binary identity mismatch") - if kind == "Caddy" and not version.startswith( - report["peers"][kind]["release"] + " " - ): - raise RuntimeError( - "container Caddy version differs from latest source build" - ) - report["peers"][kind]["version"] = version - validation = run_command( - [ - "container", - "exec", - gateway.name, - "env", - "XDG_CONFIG_HOME=/data/config", - "XDG_DATA_HOME=/data/share", - "/data/fixture/peer", - "validate", - "--config", - "/data/fixture/config.json", - ], - timeout=30, - limit=65536, - ) - (output / "gateway-validation.log").write_text( - redact(validation.stdout.decode(errors="replace")) - ) - if validation.returncode or not validation.cleanup: - raise RuntimeError("native Caddy configuration validation failed") - for peer in (origin, server, gateway, client): - peer.release() - origin.wait_tcp(24000) - server.wait_tcp(23000) - client.wait_tcp(23100) - report["phase"] = "traffic" - - def trace_snapshot(): - return json.loads( - command( - "exec", - gateway.name, - "python", - "-B", - "/data/fixture/observe.py", - ) - ) - - for index, case in enumerate(selected): - result = dict(case, status="FAIL") - before = trace_snapshot() if identities_only else {} - started = time.monotonic() - try: - check = case["check"] - args = (client.ipv4, 23100 + index, origin.ipv4) - observation = ( - rejected_identity(*args) - if check == "reject" - else stream_check(*args, bulk=check == "bulk-server-first") - if check in ("bulk-server-first", "close") - else exercise(*args, udp=check == "udp") - ) - result.update(observation, status="PASS") - if check == "reject" and not observation["rejected"]: - result.update( - status="FAIL", - failure_kind=( - "TimeoutError" - if observation["client_outcome"] == "timeout" - else "OriginAccepted" - ), - ) - except (OSError, ValueError) as error: - result["failure_kind"] = type(error).__name__ - if identities_only: - after = trace_snapshot() - result["gateway_trace_files"] = len(after) - result["gateway_new_trace_files"] = len( - after.keys() - before.keys() - ) - evidence = [ - event - for key, events in after.items() - if key not in before - for event in events - ] - result["gateway_certificate_errors"] = evidence - if case["check"] == "reject": - expected = { - "absent": "certificate_required", - "expired": "certificate_expired", - "wrong-ca": "unknown_ca", - }[case["identity"]] - result["identity_enforced"] = any( - event["category"] == expected for event in evidence - ) and ( - result.get("origin_accepted") is False - and result.get("data_exchanged") is False - ) - if not result["identity_enforced"]: - result.update(status="FAIL", evidence_missing=True) - result["seconds"] = round(time.monotonic() - started, 3) - report["cases"].append(result) - print(case["id"], result["status"], flush=True) - (output / "result.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - for peer in (origin, server, gateway, client): - peer.ensure_alive() - report["status"] = ( - "PASS" - if all(case["status"] == "PASS" for case in report["cases"]) - else "FAIL" - ) - report["phase"] = "complete" - except BaseException as error: - report.update(status="FAIL", failure_kind=type(error).__name__) - raise - finally: - report["source_unchanged"] = report["source"] == source_identity() - report["cleanup"] = all( - peer.get("joined") for peer in report["isolation"].get("peers", []) - ) - run_id = report["isolation"].get("run_id") - report["owned_remaining"] = ( - [ - item["id"] - for item in listing() - if item["configuration"].get("labels", {}).get("vcore-run") == run_id - ] - if run_id - else [] - ) - (output / "result.json").write_text(json.dumps(report, indent=2) + "\n") - return ( - 0 - if report["status"] == "PASS" - and report["source_unchanged"] - and report["cleanup"] - and not report["owned_remaining"] - else 1 - ) - - -def main(output, *, identities_only=False): - with exclusive_run(): - return run(output, identities_only=identities_only) diff --git a/scripts/src/vcore_scripts/protocol_xhttp_peers.py b/scripts/src/vcore_scripts/protocol_xhttp_peers.py deleted file mode 100644 index 8a79382..0000000 --- a/scripts/src/vcore_scripts/protocol_xhttp_peers.py +++ /dev/null @@ -1,524 +0,0 @@ -"""XHTTP-PEERS native XHTTP/mux capability probes, not VCore XHTTP acceptance. - -Official clients, servers and origins run in owned containers. The host only -drives SOCKS clients and observes the origin. Direct and layered native decoder -topologies have separate results; a layered pass never overwrites a direct fail. -""" - -from __future__ import annotations - -import contextlib -import json -import shutil -import socket -import struct -import tempfile -import time -from pathlib import Path - -from .builds import CORE_DIR -from .container_close_client import exact -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import PeerArtifact, download_native -from .protocol_containers import ContainerLab, command, listing -from .protocol_fixtures import certificate_chain, certificates -from .protocol_inputs import redact, source_identity -from .protocol_vless_security import client_identities - -CLIENT_ID = "07070707-0707-0707-0707-070707070707" - - -def peer_config(kind, cert, key, *, decoder=None, plain=False): - if kind == "M": - listener = dict( - name="xhttp", - type="vless", - listen="::", - port=23000, - users=[dict(uuid=CLIENT_ID)], - **{"xhttp-config": dict(path="/xhttp", mode="auto")}, - ) - if plain: - listener["allow-insecure"] = True - else: - listener.update(certificate=cert, **{"private-key": key}) - return dict( - ipv6=True, - **{"log-level": "warning"}, - listeners=[listener], - rules=["MATCH,DIRECT"], - ) - inbound = dict( - listen="::", - port=23000, - protocol="vless", - settings=dict(clients=[dict(id=CLIENT_ID)], decryption="none"), - streamSettings=dict( - network="xhttp", - security="tls", - tlsSettings=dict( - alpn=["h3"], certificates=[dict(certificateFile=cert, keyFile=key)] - ), - xhttpSettings=dict(path="/xhttp", mode="auto"), - ), - ) - if decoder: - # The same Xray XHTTP handler owns both legs. It forwards the resulting - # byte stream to a real Mihomo VLESS decoder, without parsing its bytes. - inbound.update( - protocol="dokodemo-door", - settings=dict(address=decoder, port=23001, network="tcp"), - ) - return dict( - log=dict(loglevel="warning"), - inbounds=[inbound], - outbounds=[dict(protocol="freedom")], - ) - - -def client_config( - server, pin, version, mode, *, download=False, mux=None, codec="xudp", plain=False -): - node = dict( - name="probe", - type="vless", - server=server, - port=23000, - uuid=CLIENT_ID, - tls=not plain, - udp=True, - network="xhttp", - servername="localhost", - alpn=["http/1.1" if version == "h1" else version], - **{"packet-encoding": codec, "xhttp-opts": dict(path="/xhttp", mode=mode)}, - ) - if not plain: - node["fingerprint"] = pin - if download: - node["xhttp-opts"]["download-settings"] = {} - if mux: - node["smux"] = dict(enabled=True, protocol=mux) - return dict( - ipv6=True, - **{"log-level": "warning"}, - proxies=[node], - listeners=[ - dict( - name="consumer", - type="socks", - listen="::", - port=23002, - udp=True, - proxy="probe", - ) - ], - rules=["MATCH,REJECT"], - ) - - -def identity_peer_config(kind, cert, key, ca): - config = peer_config(kind, cert, key) - if kind == "M": - config["listeners"][0].update( - {"client-auth-type": "require-and-verify", "client-auth-cert": ca} - ) - else: - # Xray's published verify usage adds RootCAs; it is NOT client auth. - # This probe checks whether a client identity is actually enforced, - # without inventing an unsupported client-auth option for that peer. - tls = config["inbounds"][0]["streamSettings"]["tlsSettings"] - tls["certificates"].append(dict(certificateFile=ca, usage="verify")) - return config - - -def variants(): - for mode in ("packet-up", "stream-up", "stream-one"): - yield dict(name=mode, mode=mode) - if mode != "stream-one": - yield dict(name=mode + "-download", mode=mode, download=True) - yield dict(name="packetaddr", mode="packet-up", codec="packetaddr") - for mux in ("h2mux", "smux", "yamux"): - yield dict(name=mux, mode="packet-up", mux=mux) - - -def socks(proxy, port, command_id, target, target_port): - connection = socket.create_connection((proxy, port), timeout=5) - try: - connection.sendall(b"\x05\x01\x00") - if exact(connection, 2) != b"\x05\x00": - raise ValueError("SOCKS greeting rejected") - connection.sendall( - bytes([5, command_id, 0, 1]) - + socket.inet_aton(target) - + struct.pack("!H", target_port) - ) - reply = exact(connection, 4) - if reply[:3] != b"\x05\x00\x00" or reply[3] != 1: - raise ValueError("SOCKS request rejected") - address = socket.inet_ntoa(exact(connection, 4)) - relay_port = struct.unpack("!H", exact(connection, 2))[0] - return connection, (proxy if address == "0.0.0.0" else address, relay_port) - except BaseException: - connection.close() - raise - - -def exercise(proxy, port, origin, *, udp=False): - """One small positive data probe; no retry, no full stage coverage claim.""" - payload = b"xhttp-native-decoder-probe" * 3 - with socket.create_connection((origin, 24000), timeout=5) as control: - control.sendall(b"\x04" if udp else b"\x0d") - target_port = struct.unpack("!H", exact(control, 2))[0] - connection, relay = socks( - proxy, - port, - 3 if udp else 1, - "0.0.0.0" if udp else origin, - 0 if udp else target_port, - ) - with connection: - if not udp: - connection.sendall(payload) - if ( - exact(connection, len(payload)) != payload - or exact(control, 1) != b"A" - ): - raise ValueError("native TCP mismatch") - else: - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as datagram: - datagram.settimeout(5) - datagram.bind(("0.0.0.0", 0)) - address = ( - b"\x01" - + socket.inet_aton(origin) - + struct.pack("!H", target_port) - ) - datagram.sendto(b"\x00\x00\x00" + address + payload, relay) - reply, source = datagram.recvfrom(2048) - if source != relay or reply != b"\x00\x00\x00" + address + payload: - raise ValueError("native UDP mismatch") - observed = exact(control, 4) - length = struct.unpack("!H", observed[:2])[0] - if length != len(payload) or exact(control, length) != payload: - raise ValueError("native origin mismatch") - return dict(bytes=len(payload), exact_echo=True, origin_observed=True) - - -def run(output: Path, *, identities_only=False): - output = output.resolve() - if output.parent != (CORE_DIR / "target/interop/runs").resolve(): - raise ValueError( - "native probe output must be a fresh target/interop/runs child" - ) - output.mkdir(parents=True, exist_ok=False) - report = dict( - stage="XHTTP-PEERS", - purpose="XHTTP-H3-client-identity-prerequisite" - if identities_only - else "XHTTP-native-prerequisites", - vcore_acceptance=False, - source=source_identity(), - status="NOT RUN", - cases=[], - peers={}, - isolation={}, - ) - try: - artifacts = {} - for kind in ("M", "XR"): - directory = output / "binaries" / kind - if kind == "M": - identity = {} - artifacts[kind] = PeerArtifact( - download_mihomo( - "linux-arm64", directory=directory, identity=identity - ), - identity, - ) - else: - artifacts[kind] = download_native( - kind, directory, "linux-arm64", defer_version=True - ) - report["peers"][kind] = artifacts[kind].identity - # Go QUIC peers send 1280-byte Initial UDP payloads. A 1280-byte guest - # L3 interface cannot carry those plus IP/UDP headers. This changes only - # these disposable guests, never the shared network or host interface. - lab = ContainerLab(report["isolation"], mtu=1500) - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - origin_dir = root / "origin" - origin_dir.mkdir() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - origin = lab.start( - stack, - origin_dir, - "xhttp-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - origin.release() - origin.wait_tcp(24000) - decoder_dir = root / "decoder" - decoder_dir.mkdir() - shutil.copy2(artifacts["M"].binary, decoder_dir / "peer") - (decoder_dir / "config.json").write_text( - json.dumps( - dict( - ipv6=True, - **{"log-level": "warning"}, - listeners=[ - dict( - name="decoder", - type="vless", - listen="::", - port=23001, - users=[dict(uuid=CLIENT_ID)], - **{"allow-insecure": True}, - ) - ], - rules=["MATCH,DIRECT"], - ) - ) - ) - decoder = lab.start( - stack, - decoder_dir, - "xhttp-decoder", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - decoder.release() - decoder.wait_tcp(23001) - groups = ( - ("M", "h1", True, False), - ("M", "h2", True, False), - ("M", "h1", False, False), - ("M", "h2", False, False), - ("XR", "h3", False, False), - ("XR", "h3", False, True), - ) - if identities_only: - groups = (("M", "h2", False, False), ("XR", "h3", False, False)) - for kind, version, plain, layered in groups: - security = "plain" if plain else "tls" - topology = "layered" if layered else "direct" - label = f"{kind}-{version}-{security}-{topology}" - with contextlib.ExitStack() as group: - server_dir, client_dir = ( - root / (label + "-server"), - root / (label + "-client"), - ) - server_dir.mkdir() - client_dir.mkdir() - - def preserve_logs( - server_dir=server_dir, client_dir=client_dir, label=label - ): - for role, directory in ( - ("server", server_dir), - ("client", client_dir), - ): - log = directory / "peer.log" - if log.exists(): - (output / f"{label}-{role}.log").write_text( - redact(log.read_text(errors="replace")) - ) - - # Runs after peer stop/join, before temporary keys/configs - # are removed. No credentials or configs are copied out. - group.callback(preserve_logs) - shutil.copy2(artifacts[kind].binary, server_dir / "peer") - shutil.copy2(artifacts["M"].binary, client_dir / "peer") - cert, key, pin = ( - certificate_chain(server_dir) - if identities_only - else certificates(server_dir) - ) - config = peer_config( - kind, - "/data/fixture/" + cert.name, - "/data/fixture/" + key.name, - decoder=decoder.ipv4 if layered else None, - plain=plain, - ) - identities = {} - if identities_only: - identities = client_identities(server_dir) - identities["absent"] = {"certificate": "", "private-key": ""} - config = identity_peer_config( - kind, - "/data/fixture/" + cert.name, - "/data/fixture/" + key.name, - "/data/fixture/root.pem", - ) - (server_dir / "config.json").write_text(json.dumps(config)) - argv = ( - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ] - ) - server = lab.start(group, server_dir, label + "-server", argv) - version_text = command( - "exec", - server.name, - "/data/fixture/peer", - "-v" if kind == "M" else "version", - ).strip() - report["peers"][kind]["version"] = version_text - # One immutable client config gives every variant its own - # listener, node and pool; no API mutation or NAT-key reuse. - clients = dict( - ipv6=True, - **{"log-level": "warning"}, - proxies=[], - listeners=[], - rules=["MATCH,REJECT"], - ) - selected = list(variants()) - if identities_only: - selected = [ - dict(name=name, mode="packet-up", download=True) - for name in ("valid", "absent", "expired", "wrong-ca") - ] - for index, variant in enumerate(selected): - kwargs = {k: v for k, v in variant.items() if k != "name"} - client = client_config( - server.ipv4, pin, version, plain=plain, **kwargs - ) - node, listener = client["proxies"][0], client["listeners"][0] - if identities_only: - node.update(identities["valid"]) - node["xhttp-opts"]["download-settings"].update( - identities[variant["name"]] - ) - node["name"] = listener["name"] = listener["proxy"] = variant[ - "name" - ] - listener["port"] = 23100 + index - clients["proxies"].append(node) - clients["listeners"].append(listener) - (client_dir / "config.json").write_text(json.dumps(clients)) - client = lab.start( - group, - client_dir, - label + "-client", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ], - ) - server.release() - client.release() - if kind == "M": - server.wait_tcp(23000) - client.wait_tcp(23100) - for index, variant in enumerate(selected): - for udp in (False,) if identities_only else (False, True): - business = "udp" if udp else "tcp" - case = dict( - id=f"{label}-{variant['name']}-{business}", - topology="Xray-XHTTP-to-Mihomo-VLESS" - if layered - else "single-native-handler", - status="FAIL", - ) - started = time.monotonic() - should_connect = ( - not identities_only or variant["name"] == "valid" - ) - case["expected_data_connected"] = should_connect - try: - case.update( - exercise( - client.ipv4, 23100 + index, origin.ipv4, udp=udp - ), - data_connected=True, - status="PASS" if should_connect else "FAIL", - ) - if not should_connect: - case["failure_kind"] = "ClientIdentityNotEnforced" - except (OSError, ValueError) as error: - case["failure_kind"] = type(error).__name__ - case["data_connected"] = False - # A rejected exchange alone is only a peer - # prerequisite observation, not an XHTTP negative - # gate proving zero business bytes/resources. - if not should_connect: - case["status"] = "PASS" - case["seconds"] = round(time.monotonic() - started, 3) - report["cases"].append(case) - print(case["id"], case["status"], flush=True) - (output / "result.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - server.ensure_alive() - client.ensure_alive() - report["status"] = ( - "PASS" - if all(case["status"] == "PASS" for case in report["cases"]) - else "FAIL" - ) - except BaseException as error: - report["status"] = "FAIL" - report["failure_kind"] = type(error).__name__ - raise - finally: - report["source_unchanged"] = report["source"] == source_identity() - report["cleanup"] = all( - peer.get("joined") for peer in report["isolation"].get("peers", []) - ) - run_id = report["isolation"].get("run_id") - report["owned_remaining"] = ( - [ - item["id"] - for item in listing() - if item["configuration"].get("labels", {}).get("vcore-run") == run_id - ] - if run_id - else [] - ) - (output / "result.json").write_text(json.dumps(report, indent=2) + "\n") - return ( - 0 - if report["status"] == "PASS" - and report["source_unchanged"] - and report["cleanup"] - and not report["owned_remaining"] - else 1 - ) - - -def main(output, *, identities_only=False): - with exclusive_run(): - return run(output, identities_only=identities_only) diff --git a/scripts/src/vcore_scripts/protocol_xhttp_security.py b/scripts/src/vcore_scripts/protocol_xhttp_security.py deleted file mode 100644 index 2c201ee..0000000 --- a/scripts/src/vcore_scripts/protocol_xhttp_security.py +++ /dev/null @@ -1,494 +0,0 @@ -"""XHTTP VCore upload/download security against owned native handlers. - -H1/H2 use one Mihomo listener with two ports. H3 uses the approved xcaddy -gateway (two bindings, one Xray h2c handler). Neither topology invents a decoder. -""" - -from __future__ import annotations - -import contextlib -import copy -import json -import os -import shutil -import ssl -import tempfile -from pathlib import Path - -from .builds import CORE_DIR -from .caddy_build import build_caddy -from .mihomo_isolation import exclusive_run -from .mihomo_release import download_mihomo -from .native_release import download_native -from .protocol_containers import ContainerLab, command, listing -from .protocol_evidence import read_events -from .protocol_fixtures import certificate_chain -from .protocol_inputs import redact, source_identity -from .protocol_peers import run_command -from .protocol_vless_security import client_identities -from .protocol_xhttp_fields import events_pass -from .protocol_xhttp_gateway import gateway_config, sanitized_gateway_log -from .protocol_xhttp_peers import CLIENT_ID, peer_config - - -def security_cases(base, identities, download_identity, server_ipv6): - """Frozen cases, selected before observing results. No retry or skip-on-error.""" - for mode in ("packet-up", "stream-up", "stream-one"): - node = copy.deepcopy(base) - options = node["xhttp-opts"] - options["mode"] = mode - if mode == "packet-up": - options["sc-min-posts-interval-ms"] = "1" - yield f"{mode}-shared", node, False, None - for identity in ("absent", "expired", "wrong-ca"): - invalid = copy.deepcopy(node) - invalid.update(identities[identity]) - if identity == "absent": - del invalid["certificate"], invalid["private-key"] - yield f"{mode}-shared-{identity}", invalid, True, identity - if mode == "stream-one": - continue - options["download-settings"] = {} - yield f"{mode}-inherit", copy.deepcopy(node), False, None - download = options["download-settings"] - download.update(download_identity) - download.update( - server=server_ipv6, - port=23002, - tls=True, - servername="localhost", - host="localhost", - path="/xhttp/", - headers={"X-Download": "fixture"}, - **{"reuse-settings": {}, "fingerprint": "", "name-cert-verify": ""}, - ) - yield f"{mode}-replace-dualstack-port", copy.deepcopy(node), False, None - download["server"] = "peer.fixture.test" - yield f"{mode}-download-domain", copy.deepcopy(node), False, None - node["servername"] = "sni.fixture.test" - node["name-cert-verify"] = "localhost" - if base["alpn"] != ["h3"]: - # One native handler accepts both ALPNs; the download has its own policy. - download["alpn"] = ["http/1.1"] if base["alpn"] == ["h2"] else ["h2"] - yield f"{mode}-name-clear-alpn", copy.deepcopy(node), False, None - node["servername"] = "localhost" - del node["name-cert-verify"] - for leg in ("upload", "download"): - for identity in ("absent", "expired", "wrong-ca"): - invalid = copy.deepcopy(node) - target = ( - invalid - if leg == "upload" - else invalid["xhttp-opts"]["download-settings"] - ) - target.update(identities[identity]) - if identity == "absent" and leg == "upload": - del target["certificate"], target["private-key"] - yield f"{mode}-{leg}-{identity}", invalid, True, identity - for key, value in ( - ("fingerprint", "00" * 32), - ("name-cert-verify", "wrong.fixture.test"), - ): - invalid = copy.deepcopy(node) - invalid["xhttp-opts"]["download-settings"][key] = value - yield f"{mode}-download-wrong-{key}", invalid, True, None - invalid = copy.deepcopy(node) - invalid["skip-cert-verify"] = True - invalid["xhttp-opts"]["download-settings"].update( - {"skip-cert-verify": False, "servername": "wrong.fixture.test"} - ) - yield f"{mode}-download-false-restores-verify", invalid, True, None - - -def run(output: Path, versions: list[str], *, supplied: dict | None = None): - if ( - not versions - or len(versions) != len(set(versions)) - or not set(versions) <= {"h1", "h2", "h3"} - ): - raise ValueError("select unique H1/H2/H3 security versions") - if ( - not output.is_relative_to(CORE_DIR / "target/interop/runs") - or output == CORE_DIR / "target/interop/runs" - ): - raise ValueError("security output must be a fresh target/interop/runs child") - output.mkdir(exist_ok=False) - report = dict( - stage="XHTTP", - scope="security-development-only", - source=source_identity(), - status="NOT RUN", - cases=[], - peers={}, - isolation={}, - ) - try: - artifacts = {} - if supplied is not None: - for kind in ({"M"} if any(v != "h3" for v in versions) else set()) | ( - {"XR", "Caddy"} if "h3" in versions else set() - ): - artifacts[kind], report["peers"][kind] = supplied[kind] - elif any(v != "h3" for v in versions): - identity = {} - artifacts["M"] = download_mihomo( - "linux-arm64", directory=output / "binaries/M", identity=identity - ) - report["peers"]["M"] = identity - if "h3" in versions and supplied is None: - for kind, artifact in ( - ("Caddy", build_caddy(output / "binaries/Caddy")), - ( - "XR", - download_native( - "XR", output / "binaries/XR", "linux-arm64", defer_version=True - ), - ), - ): - artifacts[kind] = artifact.binary - report["peers"][kind] = artifact.identity - lab = ContainerLab(report["isolation"], mtu=1500) - build = run_command( - [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "xhttp_native", - "--no-run", - ], - timeout=180, - ) - (output / "build.log").write_text(redact(build.stdout.decode(errors="replace"))) - if build.returncode or not build.cleanup: - raise RuntimeError("native security driver build failed") - groups = [ - (version, negative) - for version in versions - for negative in ((False, True) if version == "h3" else (None,)) - ] - for version, negative in groups: - with ( - tempfile.TemporaryDirectory(prefix="private-", dir=output) as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - directories = { - role: root / role for role in ("origin", "server", "gateway") - } - for directory in directories.values(): - directory.mkdir() - origin_dir, server_dir, gateway_dir = directories.values() - shutil.copyfile( - Path(__file__).with_name("container_udp_origin.py"), - origin_dir / "origin.py", - ) - origin = lab.start( - stack, - origin_dir, - version + "-origin", - [ - "env", - "VCORE_ISOLATED_ORIGIN=1", - "python", - "-B", - "/data/fixture/origin.py", - ], - ) - cert_dir = gateway_dir if version == "h3" else server_dir - cert, key, pin = certificate_chain(cert_dir) - identities = client_identities(cert_dir) - identities["absent"] = {"certificate": "", "private-key": ""} - second = root / "second" - second.mkdir() - for filename in ("root.pem", "root-key.pem"): - shutil.copyfile(cert_dir / filename, second / filename) - download_identity = client_identities(second)["valid"] - kind = "XR" if version == "h3" else "M" - shutil.copy2(artifacts[kind], server_dir / "peer") - config = peer_config( - kind, f"/data/fixture/{cert.name}", f"/data/fixture/{key.name}" - ) - if kind == "M": - config["hosts"] = {"vcore-fixture.test": origin.ipv4} - config["listeners"][0].update( - { - "port": "23000,23002", - "client-auth-type": "require-and-verify", - "client-auth-cert": "/data/fixture/root.pem", - } - ) - else: - stream = config["inbounds"][0]["streamSettings"] - stream["security"] = "none" - del stream["tlsSettings"] - config["dns"] = {"hosts": {"vcore-fixture.test": origin.ipv4}} - (server_dir / "config.json").write_text(json.dumps(config)) - server = lab.start( - stack, - server_dir, - version + "-server", - [ - "/data/fixture/peer", - "-d", - "/data", - "-f", - "/data/fixture/config.json", - ] - if kind == "M" - else [ - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ], - ) - peers = [(kind, server)] - gateway = None - edge = server - if version == "h3": - shutil.copy2(artifacts["Caddy"], gateway_dir / "peer") - shutil.copyfile( - Path(__file__).with_name("container_quic_observer.py"), - gateway_dir / "observe.py", - ) - gateway = lab.start( - stack, - gateway_dir, - version + "-gateway", - [ - "env", - "XDG_CONFIG_HOME=/data/config", - "XDG_DATA_HOME=/data/share", - *(["QLOGDIR=/data/qlog"] if negative else []), - "/data/fixture/peer", - "run", - "--config", - "/data/fixture/config.json", - ], - ) - conf = gateway_config(server.ipv4) - conf["apps"]["http"]["servers"]["fixture"]["listen"].append( - ":23002" - ) - (gateway_dir / "config.json").write_text(json.dumps(conf)) - peers.append(("Caddy", gateway)) - edge = gateway - for implementation, peer in peers: - identity = report["peers"][implementation] - actual = command( - "exec", - peer.name, - "/data/fixture/peer", - "-v" if implementation == "M" else "version", - ).strip() - digest = command( - "exec", peer.name, "sha256sum", "/data/fixture/peer" - ).split()[0] - if not actual or digest != identity["binary_sha256"]: - raise RuntimeError("native security binary identity mismatch") - identity["version"] = actual - - def preserve_logs(peers=peers, version=version, negative=negative): - for implementation, peer in peers: - text = command("logs", peer.name)[-65536:] - ( - output / f"{version}-{negative}-{implementation}.log" - ).write_text( - sanitized_gateway_log(text) - if implementation == "Caddy" - else redact(text) - ) - - stack.callback(preserve_logs) - origin.release() - server.release() - if gateway: - gateway.release() - origin.wait_tcp(24000) - server.wait_tcp(23000) - base = dict( - name="peer", - type="vless", - server=edge.ipv4, - port=23000, - uuid=CLIENT_ID, - udp=True, - tls=True, - servername="localhost", - fingerprint=pin, - alpn=["http/1.1" if version == "h1" else version], - network="xhttp", - **identities["valid"], - **{ - "xhttp-opts": { - "path": "/xhttp", - "host": "localhost", - "headers": {"X-Upload": "fixture"}, - } - }, - ) - selected = [ - case - for case in security_cases( - base, identities, download_identity, edge.ipv6 - ) - if negative is None or case[2] == negative - ] - - def trace_snapshot(gateway=gateway, negative=negative): - return ( - json.loads( - command( - "exec", - gateway.name, - "python", - "-B", - "/data/fixture/observe.py", - ) - ) - if gateway and negative - else {} - ) - - for name, node, reject, expected_identity in selected: - case_id = f"{version}-{name}" - print(case_id, flush=True) - fixture = root / "input.json" - fixture.write_text( - json.dumps( - dict( - isolation="containers", - node=node, - reject=reject, - server_ipv4=edge.ipv4, - origin_control=f"{origin.ipv4}:24000", - origin_ipv4=origin.ipv4, - origin_ipv6=origin.ipv6, - root_der=list( - ssl.PEM_cert_to_DER_cert( - (cert_dir / "root.pem").read_text() - ) - ), - ) - ) - ) - events = output / f"{case_id}-events.jsonl" - assertion = "security::native_xhttp_security" - cmd = [ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "xhttp_native", - assertion, - "--", - "--exact", - "--ignored", - "--nocapture", - ] - before = trace_snapshot() - result = run_command( - cmd, - timeout=140, - env=dict( - os.environ, - VCORE_XHTTP_INPUT=str(fixture), - VCORE_CASE_EVENTS=str(events), - ), - ) - (output / f"{case_id}.log").write_text( - redact(result.stdout.decode(errors="replace")) - ) - observation = read_events(events) if events.exists() else [] - passed = ( - result.returncode == 0 - and result.cleanup - and events_pass(observation, assertion) - ) - row = dict( - case_id=case_id, - status="PASS" if passed else "FAIL", - command=cmd, - exit_code=result.returncode, - seconds=result.seconds, - command_cleanup=result.cleanup, - topology="Caddy H3 -> one Xray h2c handler" - if gateway - else "one Mihomo handler, two ports", - ) - if gateway and negative: - after = trace_snapshot() - # QUIC trace files contain only lifecycle records at this - # level; retain categories, never identities or packets. - evidence = [ - event - for key, records in after.items() - if key not in before - for event in records - ] - row["certificate_errors"] = evidence - if expected_identity: - expected = { - "absent": "certificate_required", - "expired": "certificate_expired", - "wrong-ca": "unknown_ca", - }[expected_identity] - row["identity_enforced"] = any( - e["category"] == expected for e in evidence - ) - if not row["identity_enforced"]: - row["status"] = "FAIL" - report["cases"].append(row) - print(case_id, row["status"], flush=True) - if row["status"] != "PASS": - raise RuntimeError("native security case failed") - for _, peer in peers: - peer.ensure_alive() - report["status"] = "PASS" - except (OSError, ValueError, RuntimeError) as error: - report.update( - status="FAIL", - reason=str(error) - if isinstance(error, RuntimeError) - else type(error).__name__, - ) - finally: - report["cleanup"] = all( - p.get("joined") for p in report["isolation"].get("peers", []) - ) - report["source_unchanged"] = report["source"] == source_identity() - run_id = report["isolation"].get("run_id") - report["owned_remaining"] = ( - [ - p["id"] - for p in listing() - if p["configuration"].get("labels", {}).get("vcore-run") == run_id - ] - if run_id - else [] - ) - if ( - not report["cleanup"] - or not report["source_unchanged"] - or report["owned_remaining"] - ): - report["status"] = "FAIL" - (output / "xhttp-security-results.json").write_text( - json.dumps(report, indent=2) + "\n" - ) - return 0 if report["status"] == "PASS" else 1 - - -if __name__ == "__main__": - import argparse - import sys - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output", type=Path) - parser.add_argument("versions", nargs="+", choices=("h1", "h2", "h3")) - args = parser.parse_args() - with exclusive_run(): - sys.exit(run(args.output.resolve(), args.versions)) diff --git a/scripts/src/vcore_scripts/tls_client_hello.py b/scripts/src/vcore_scripts/tls_client_hello.py deleted file mode 100644 index ae03977..0000000 --- a/scripts/src/vcore_scripts/tls_client_hello.py +++ /dev/null @@ -1,186 +0,0 @@ -"""Bounded, offline TLS ClientHello inspection, not a TLS implementation.""" - -from __future__ import annotations - -import base64 -import hashlib -import io - -from .container_tls_observer import capture_client_hello - - -class Cursor: - def __init__(self, data: bytes): - self.data, self.at = data, 0 - - def take(self, size: int) -> bytes: - if size < 0 or self.at + size > len(self.data): - raise ValueError("truncated ClientHello") - result = self.data[self.at : self.at + size] - self.at += size - return result - - def number(self, size: int) -> int: - return int.from_bytes(self.take(size), "big") - - def vector(self, size: int) -> bytes: - return self.take(self.number(size)) - - def done(self): - if self.at != len(self.data): - raise ValueError("unparsed ClientHello data") - - -def _codes(data: bytes) -> list[int]: - if len(data) % 2: - raise ValueError("odd TLS uint16 vector") - return [int.from_bytes(data[i : i + 2], "big") for i in range(0, len(data), 2)] - - -def is_grease(value: int) -> bool: - return value & 0x0F0F == 0x0A0A and value >> 8 == value & 0xFF - - -def _shape(hello: dict, shuffle_extensions: bool) -> dict: - def normalize(value, key=""): - if isinstance(value, dict): - return {k: normalize(v, k) for k, v in value.items() if k != "raw_sha256"} - if isinstance(value, list): - return [normalize(v, key) for v in value] - if key in {"ciphers", "values", "type", "group"} and isinstance(value, int): - return "GREASE" if is_grease(value) else value - return value - - result = normalize(hello) - groups = next((e["values"] for e in hello["extensions"] if e["type"] == 10), []) - shares = next((e["shares"] for e in hello["extensions"] if e["type"] == 51), []) - result["grease_share_group_links"] = [ - [i for i, group in enumerate(groups) if group == share["group"]] - for share in shares - if is_grease(share["group"]) - ] - if shuffle_extensions: - # Preserve GREASE/padding/PSK positions, sort ONLY the shuffled slots. - rows = result["extensions"] - slots = [ - i for i, row in enumerate(rows) if row["type"] not in {"GREASE", 21, 41} - ] - ordered = sorted((rows[i] for i in slots), key=lambda row: row["type"]) - for index, row in zip(slots, ordered, strict=True): - rows[index] = row - return result - - -def compare_client_hellos( - expected: bytes, observed: bytes, *, shuffle_extensions: bool = False -) -> list[str]: - """Return differing structural fields; no blanket JA3/ECH/PSK exemption. - - Random values are normalized, not their widths or positions. A caller may - separately classify an observed ECH/padding length variant using its pinned - template; it is intentionally still reported here. - """ - left = _shape(parse_client_hello(expected), shuffle_extensions) - right = _shape(parse_client_hello(observed), shuffle_extensions) - return [ - key - for key in sorted(left.keys() | right.keys()) - if left.get(key) != right.get(key) - ] - - -def validate_capture(record: dict) -> dict: - """Recompute the retained observation from raw records, not PASS text.""" - if any( - len(record[key]) > 256 * 1024 for key in ("client_hello_b64", "records_b64") - ): - raise ValueError("capture size limit") - wire = base64.b64decode(record["records_b64"], validate=True) - raw = base64.b64decode(record["client_hello_b64"], validate=True) - records, handshake, layout = capture_client_hello(io.BytesIO(wire).read) - parsed = parse_client_hello(raw) - if records != wire or handshake != raw or layout != record["records"]: - raise ValueError("TLS records differ from retained ClientHello") - if parsed != record["parsed"]: - raise ValueError("ClientHello evidence differs from raw capture") - return parsed - - -def parse_client_hello(data: bytes) -> dict: - """Observe an entire handshake message, including its four-byte envelope.""" - if len(data) > 65536: - raise ValueError("ClientHello size limit") - cursor = Cursor(data) - if cursor.number(1) != 1 or cursor.number(3) != len(data) - 4: - raise ValueError("invalid ClientHello envelope") - result = {"bytes": len(data), "legacy_version": cursor.number(2)} - cursor.take(32) - result["session_id_bytes"] = len(cursor.vector(1)) - result["ciphers"] = _codes(cursor.vector(2)) - result["compression_methods"] = list(cursor.vector(1)) - extensions = Cursor(cursor.vector(2)) - cursor.done() - result["extensions"] = [] - seen = set() - while extensions.at < len(extensions.data): - kind, payload = extensions.number(2), extensions.vector(2) - if kind in seen or len(seen) >= 128: - raise ValueError("duplicate or excessive ClientHello extensions") - seen.add(kind) - row = {"type": kind, "bytes": len(payload)} - value = Cursor(payload) - if kind in (10, 13, 34, 50): - row["values"] = _codes(value.vector(2)) - elif kind in (27, 43): - row["values"] = _codes(value.vector(1)) - elif kind == 0: - names = Cursor(value.vector(2)) - row["names"] = [] - while names.at < len(names.data): - row["names"].append( - {"type": names.number(1), "name": names.vector(2).decode("ascii")} - ) - elif kind in (16, 17513, 17613): - protocols = Cursor(value.vector(2)) - row["protocols"] = [] - while protocols.at < len(protocols.data): - row["protocols"].append(protocols.vector(1).decode("ascii")) - elif kind == 51: - shares = Cursor(value.vector(2)) - row["shares"] = [] - while shares.at < len(shares.data): - group, key = shares.number(2), shares.vector(2) - share = {"group": group, "bytes": len(key)} - if is_grease(group): - share["grease_data"] = key.hex() - elif group in (23, 24, 25) and key: - share["point_format"] = key[0] - row["shares"].append(share) - elif kind == 65037: - row["hello_type"] = value.number(1) - if row["hello_type"] != 0: - raise ValueError("expected outer ECH structure") - row["kdf"], row["aead"] = value.number(2), value.number(2) - value.take(1) # Random GREASE config_id remains in the raw evidence. - row["enc_bytes"] = len(value.vector(2)) - row["payload_bytes"] = len(value.vector(2)) - elif kind == 35: - row["ticket_bytes"] = len(value.take(len(payload))) - elif kind == 41: - identities = Cursor(value.vector(2)) - row["identities"] = [] - while identities.at < len(identities.data): - row["identities"].append(len(identities.vector(2))) - identities.take(4) # Obfuscated age is time-dependent. - binders = Cursor(value.vector(2)) - row["binders"] = [] - while binders.at < len(binders.data): - row["binders"].append(len(binders.vector(1))) - if not row["identities"] or len(row["identities"]) != len(row["binders"]): - raise ValueError("invalid PSK identity/binder counts") - else: - row["payload_hex"] = value.take(len(payload)).hex() - value.done() - result["extensions"].append(row) - result["raw_sha256"] = hashlib.sha256(data).hexdigest() - return result diff --git a/scripts/src/vcore_scripts/tun2socks.py b/scripts/src/vcore_scripts/tun2socks.py deleted file mode 100644 index 6adf350..0000000 --- a/scripts/src/vcore_scripts/tun2socks.py +++ /dev/null @@ -1,470 +0,0 @@ -#!/usr/bin/env python3 -"""Windows demo: VCore TUN -> SOCKS5 -> a temporary Xray process. - -The source config is not modified; its selected outbounds are copied only to a -user-temporary directory and deleted with the locally built Xray executable. -""" - -from __future__ import annotations - -import copy -import ctypes -import json -import os -import socket -import struct -import subprocess -import sys -import tempfile -import time -from pathlib import Path -from urllib.parse import urlparse - -PACKAGE_NAME = "VCore.UwpDemo.Dev" -SOCKS_PORT = 19080 -TUN_IPV4 = "192.168.8.1" -TUN_IPV6 = "fd00:8::2" -DNS_IPV4 = "223.5.5.5" -DNS_IPV6 = "2400:3200::1" -NTP_IPV4 = "203.107.6.88" -DIRECT_URL = "http://www.baidu.com/" -PROXY_URL = "https://www.cloudflare.com/cdn-cgi/trace" - - -def bridge_main(dll_path: str, request_path: str, response_path: str) -> None: - dll = ctypes.CDLL(dll_path) - invoke = dll.VCoreWindowsVpnInvoke - invoke.argtypes = [ctypes.c_char_p] - invoke.restype = ctypes.c_void_p - free = dll.VCoreFree - free.argtypes = [ctypes.c_void_p] - free.restype = None - response = invoke(Path(request_path).read_bytes()) - if not response: - raise RuntimeError("VCoreWindowsVpnInvoke returned NULL") - try: - Path(response_path).write_bytes(ctypes.string_at(response)) - finally: - free(response) - - -def powershell(script: str) -> subprocess.CompletedProcess[str]: - return subprocess.run( - [ - "powershell.exe", - "-NoProfile", - "-ExecutionPolicy", - "Bypass", - "-Command", - script, - ], - check=True, - capture_output=True, - text=True, - encoding="utf-8", - ) - - -def ps_quote(value: str) -> str: - return "'" + value.replace("'", "''") + "'" - - -def package_info() -> dict[str, str]: - result = powershell( - f"$p=Get-AppxPackage -Name {ps_quote(PACKAGE_NAME)} | Select-Object -First 1; " - "if($null -eq $p){throw 'VCore UWP demo package is not installed'}; " - "$p | Select-Object PackageFamilyName,InstallLocation | " - "ConvertTo-Json -Compress" - ) - return json.loads(result.stdout) - - -def invoke_bridge( - temp: Path, package: dict[str, str], method: str, payload: dict -) -> dict: - request = temp / "bridge-request.json" - response = temp / "bridge-response.json" - command = temp / "bridge.cmd" - request.write_text( - json.dumps( - {"bridgeVersion": 3, "method": method, "payload": payload}, - separators=(",", ":"), - ), - encoding="utf-8", - ) - response.unlink(missing_ok=True) - command.write_bytes( - ( - "@echo off\r\n" - + subprocess.list2cmdline( - [ - sys.executable, - "-m", - "vcore_scripts.tun2socks", - "--bridge", - str(Path(package["InstallLocation"]) / "vcore.dll"), - str(request), - str(response), - ] - ) - + "\r\n" - ).encode() - ) - args = f'/d /c "{command}"' - powershell( - "Invoke-CommandInDesktopPackage " - f"-PackageFamilyName {ps_quote(package['PackageFamilyName'])} " - '-AppId App -Command "$env:SystemRoot\\System32\\cmd.exe" ' - f"-Args {ps_quote(args)} -PreventBreakaway" - ) - deadline = time.monotonic() + 60 - while not response.exists() and time.monotonic() < deadline: - time.sleep(0.1) - if not response.exists(): - raise TimeoutError(f"packaged Windows bridge {method} timed out") - result = json.loads(response.read_text(encoding="utf-8")) - if not result["success"]: - raise RuntimeError(f"Windows bridge {method} failed: {result['error']}") - return result["data"] - - -def derive_xray_config(source_path: Path, temp: Path) -> tuple[dict, str, str]: - source = json.loads(source_path.read_text(encoding="utf-8")) - try: - inbound = copy.deepcopy( - next(item for item in source["inbounds"] if item["protocol"] == "socks") - ) - inbound["listen"] = "127.0.0.1" - inbound["port"] = SOCKS_PORT - inbound["settings"]["udp"] = True - inbound["sniffing"] = { - "enabled": True, - "destOverride": ["http", "tls", "quic"], - "routeOnly": True, - } - inbound_tag = inbound["tag"] - proxy = copy.deepcopy( - next(item for item in source["outbounds"] if item.get("tag") == inbound_tag) - ) - direct = copy.deepcopy( - next(item for item in source["outbounds"] if item["protocol"] == "freedom") - ) - # Xray reads outbound sockopt only from streamSettings; the reference config - # currently places the freedom binding at the ignored top level. - if "sockopt" in direct: - direct.setdefault("streamSettings", {})["sockopt"] = direct.pop("sockopt") - direct_tag = direct["tag"] - except (KeyError, StopIteration, TypeError) as error: - raise ValueError( - "source Xray config must contain a tagged SOCKS inbound, its matching " - "outbound, and a tagged freedom outbound" - ) from error - direct_host = urlparse(DIRECT_URL).hostname - rules = [] - if source.get("dns", {}).get("tag"): - rules.append({"inboundTag": [source["dns"]["tag"]], "outboundTag": direct_tag}) - rules.extend( - [ - { - "domain": [f"full:{direct_host}"], - "outboundTag": direct_tag, - "ruleTag": "tun2socks-sniff-direct", - }, - {"ip": [DNS_IPV4, NTP_IPV4], "outboundTag": direct_tag}, - {"inboundTag": [inbound_tag], "outboundTag": inbound_tag}, - ] - ) - config = { - "log": { - "access": str(temp / "xray-access.log"), - "error": "none", - "loglevel": "warning", - }, - "inbounds": [inbound], - "outbounds": [proxy, direct], - "routing": {"domainStrategy": "AsIs", "rules": rules}, - } - if "dns" in source: - config["dns"] = copy.deepcopy(source["dns"]) - return config, inbound_tag, direct_tag - - -def wait_for_socks(process: subprocess.Popen[bytes]) -> None: - deadline = time.monotonic() + 10 - while time.monotonic() < deadline: - if process.poll() is not None: - raise RuntimeError("temporary Xray process exited during startup") - try: - with socket.create_connection(("127.0.0.1", SOCKS_PORT), timeout=0.2): - return - except OSError: - time.sleep(0.1) - raise TimeoutError("temporary Xray SOCKS listener did not start") - - -def dns_probe(host: str) -> str: - query_id = 0x4F56 - question = ( - b"".join(bytes([len(part)]) + part.encode("ascii") for part in host.split(".")) - + b"\0" - ) - packet = ( - struct.pack("!HHHHHH", query_id, 0x0100, 1, 0, 0, 0) - + question - + struct.pack("!HH", 1, 1) - ) - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: - sock.settimeout(15) - sock.connect((DNS_IPV4, 53)) - sock.send(packet) - local_ip = sock.getsockname()[0] - response = sock.recv(4096) - if ( - len(response) < 12 - or struct.unpack("!H", response[:2])[0] != query_id - or not response[2] & 0x80 - ): - raise RuntimeError("invalid DNS response") - if response[3] & 0x0F: - raise RuntimeError(f"DNS returned rcode {response[3] & 0x0F}") - return local_ip - - -def ntp_probe() -> str: - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: - sock.settimeout(15) - sock.connect((NTP_IPV4, 123)) - sock.send(b"\x1b" + b"\0" * 47) - local_ip = sock.getsockname()[0] - response = sock.recv(512) - if len(response) < 48: - raise RuntimeError("truncated NTP response") - return local_ip - - -def curl_probe(url: str) -> tuple[str, str]: - result = subprocess.run( - [ - "curl.exe", - "-4", - "--noproxy", - "*", - "--silent", - "--show-error", - "--max-time", - "30", - "--output", - "NUL", - "--write-out", - "%{http_code}|%{local_ip}", - url, - ], - check=True, - capture_output=True, - text=True, - ) - status, local_ip = result.stdout.strip().split("|", 1) - if status == "000": - raise RuntimeError( - f"curl did not receive an HTTP response from {urlparse(url).hostname}" - ) - return status, local_ip - - -def require_tun_source(label: str, address: str) -> None: - if address != TUN_IPV4: - raise RuntimeError(f"{label} used {address}, expected TUN source {TUN_IPV4}") - - -def wait_for_access_evidence(path: Path, inbound_tag: str, direct_tag: str) -> None: - direct_marker = f"[{inbound_tag} -> {direct_tag}]" - proxy_marker = f"[{inbound_tag} -> {inbound_tag}]" - deadline = time.monotonic() + 5 - while time.monotonic() < deadline: - text = ( - path.read_text(encoding="utf-8", errors="replace") if path.exists() else "" - ) - lines = text.splitlines() - if ( - any( - direct_marker in line and "tcp:" in line and ":80 " in line - for line in lines - ) - and any( - proxy_marker in line and "tcp:" in line and ":443 " in line - for line in lines - ) - and any( - direct_marker in line and f"udp:{DNS_IPV4}:53" in line for line in lines - ) - and any( - direct_marker in line and f"udp:{NTP_IPV4}:123" in line - for line in lines - ) - ): - return - time.sleep(0.1) - raise RuntimeError( - "Xray access log did not prove sniffed DIRECT, proxied TCP, DNS, and UDP routes" - ) - - -def vcore_yaml() -> str: - return f"""tun: - enable: true - mtu: 1500 -proxies: - - name: xray - type: socks5 - server: 127.0.0.1 - port: {SOCKS_PORT} - udp: true -dns: - enable: false -rules: - - MATCH,xray -""" - - -def run_demo(source_config: Path, *, xray_source: Path) -> None: - if os.name != "nt": - raise RuntimeError("This demo requires Windows") - source_config = source_config.resolve() - xray_source = xray_source.resolve() - if not source_config.is_file() or not (xray_source / "go.mod").is_file(): - raise RuntimeError("Xray source checkout or source config is missing") - - package = package_info() - with tempfile.TemporaryDirectory(prefix="vcore-tun2socks-") as directory: - temp = Path(directory) - initial = invoke_bridge(temp, package, "getVpnStatus", {}) - if initial["status"] != "disconnected": - raise RuntimeError("Stop the existing VCore VPN before running this demo") - - xray_exe = temp / "xray.exe" - env = os.environ.copy() - env["CGO_ENABLED"] = "0" - subprocess.run( - [ - "go", - "build", - "-trimpath", - "-buildvcs=false", - "-ldflags=-s -w -buildid=", - "-o", - str(xray_exe), - "./main", - ], - cwd=xray_source, - env=env, - check=True, - ) - revision = subprocess.run( - ["git", "rev-parse", "--short", "HEAD"], - cwd=xray_source, - check=True, - capture_output=True, - text=True, - ).stdout.strip() - config, inbound_tag, direct_tag = derive_xray_config(source_config, temp) - xray_config = temp / "xray.json" - xray_config.write_text(json.dumps(config, ensure_ascii=False), encoding="utf-8") - subprocess.run( - [ - str(xray_exe), - "run", - "-test", - "-format", - "json", - "-config", - str(xray_config), - ], - check=True, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - - xray = subprocess.Popen( - [str(xray_exe), "run", "-format", "json", "-config", str(xray_config)], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - start_attempted = False - try: - wait_for_socks(xray) - start_attempted = True - state = invoke_bridge( - temp, - package, - "startVpn", - { - "configYaml": vcore_yaml(), - "networkSettings": { - "ipv4Address": TUN_IPV4, - "ipv6Address": TUN_IPV6, - "dnsIpv4Address": DNS_IPV4, - "dnsIpv6Address": DNS_IPV6, - }, - "policy": { - "alwaysOn": False, - "allowLocalNetwork": True, - "excludedCidrs": [], - }, - }, - ) - if state["status"] != "connected": - raise RuntimeError(f"unexpected VPN status: {state['status']}") - - direct_host = urlparse(DIRECT_URL).hostname or "" - dns_source = dns_probe(direct_host) - direct_status, direct_source = curl_probe(DIRECT_URL) - proxy_status, proxy_source = curl_probe(PROXY_URL) - ntp_source = ntp_probe() - for label, source in [ - ("DNS", dns_source), - ("sniffed HTTP", direct_source), - ("proxied HTTPS", proxy_source), - ("NTP", ntp_source), - ]: - require_tun_source(label, source) - wait_for_access_evidence(temp / "xray-access.log", inbound_tag, direct_tag) - - print(f"Xray source {revision}: built and config-tested") - print(f"DNS ordinary UDP: PASS ({dns_source} -> {DNS_IPV4}:53)") - print( - "HTTP sniffing route: PASS " - f"({direct_status}, {inbound_tag} -> {direct_tag})" - ) - print( - "HTTPS proxy route: PASS " - f"({proxy_status}, {inbound_tag} -> {inbound_tag})" - ) - print(f"SOCKS5 UDP/NTP: PASS ({ntp_source} -> {NTP_IPV4}:123)") - print( - "VERDICT: VCore can serve as tun2socks in front of an external " - "Xray SOCKS inbound." - ) - finally: - if start_attempted: - try: - invoke_bridge(temp, package, "stopVpn", {}) - except ( - OSError, - subprocess.SubprocessError, - ValueError, - KeyError, - ) as error: - print(f"warning: VPN cleanup failed: {error}", file=sys.stderr) - if xray.poll() is None: - xray.terminate() - try: - xray.wait(timeout=5) - except subprocess.TimeoutExpired: - xray.kill() - xray.wait() - - -if __name__ == "__main__": - if len(sys.argv) != 5 or sys.argv[1] != "--bridge": - raise SystemExit("This module is an internal packaged bridge helper") - bridge_main(*sys.argv[2:]) diff --git a/scripts/tests/test_apple_platform.py b/scripts/tests/test_apple_platform.py new file mode 100644 index 0000000..0c337a5 --- /dev/null +++ b/scripts/tests/test_apple_platform.py @@ -0,0 +1,291 @@ +"""Offline regressions for Apple artifact identity; no simulator or network.""" + +import copy +import hashlib +import json +import os +import plistlib +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +from vcore_scripts import builds, platform_delivery + + +class ApplePlatformTest(unittest.TestCase): + def test_apple_build_reads_and_stages_the_configured_cargo_output(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + checkout, target = root / "core", root / "cargo-output" + targets = ( + "aarch64-apple-ios", + "aarch64-apple-ios-sim", + "aarch64-apple-darwin", + "x86_64-apple-darwin", + "aarch64-apple-tvos", + "aarch64-apple-tvos-sim", + ) + for triple in targets: + library = target / triple / "release/libvcore.a" + library.parent.mkdir(parents=True) + library.write_bytes(builds.EXPECTED_IDENTITY + triple.encode()) + + def run(command, **_): + if command[:2] == ["xcrun", "lipo"]: + Path(command[-1]).write_bytes(builds.EXPECTED_IDENTITY) + + with ( + patch.object(builds, "CORE_DIR", checkout), + patch.dict( + builds.os.environ, + { + "CARGO_TARGET_DIR": str(target), + "VCORE_APPLE_DIST_DIR": str(root / "package"), + }, + clear=True, + ), + patch.object(builds.platform, "system", return_value="Darwin"), + patch.object(builds, "_require_targets") as require, + patch.object(builds, "_cargo_build") as cargo, + patch.object(builds, "_run", side_effect=run) as commands, + patch.object(builds, "check_apple_binary") as check, + ): + builds.build_apple() + require.assert_called_once_with(list(targets)) + self.assertEqual( + [invocation.args[0] for invocation in cargo.call_args_list], + list(targets), + ) + self.assertEqual( + (target / "vcore-apple/ios-simulator/libvcore.a").read_bytes(), + builds.EXPECTED_IDENTITY + b"aarch64-apple-ios-sim", + ) + lipo = [ + invocation.args[0] + for invocation in commands.call_args_list + if invocation.args[0][:2] == ["xcrun", "lipo"] + ] + self.assertEqual( + lipo, + [ + [ + "xcrun", + "lipo", + "-create", + target / "aarch64-apple-darwin/release/libvcore.a", + target / "x86_64-apple-darwin/release/libvcore.a", + "-output", + target / "vcore-apple/macos/libvcore.a", + ] + ], + ) + self.assertEqual(check.call_count, 5) + self.assertEqual( + check.call_args_list[1].args[1:], + ("ios", "simulator", {"arm64"}, "13.0"), + ) + self.assertEqual( + check.call_args_list[2].args[1:], + ("macos", None, {"arm64", "x86_64"}, "10.15"), + ) + for invocation in check.call_args_list: + self.assertTrue(invocation.args[0].is_relative_to(target)) + self.assertFalse((checkout / "target").exists()) + + def test_all_native_archive_members_have_the_right_platform_and_floor(self): + first = ( + "lib.a(rust.o):\nLoad command 0\n cmd LC_BUILD_VERSION\n" + " platform 3\n minos 17.0\n" + ) + second = ( + "lib.a(crypto.o):\nLoad command 0\n cmd LC_VERSION_MIN_TVOS\n" + " version 10.0\n" + ) + self.assertEqual( + builds._check_apple_load_commands( + first + second, "tvos", None, "arm64", "17.0" + ), + 2, + ) + for bad in ( + first + second.replace("TVOS", "IPHONEOS"), + first.replace("platform 3", "platform 2") + second, + first.replace("minos 17.0", "minos 18.0") + second, + first + "lib.a(unmarked.o):\nLoad command 0\n cmd LC_SEGMENT_64\n", + first + second + second.replace("version 10.0", "version corrupt"), + "", + ): + with self.subTest(output=bad), self.assertRaises(ValueError): + builds._check_apple_load_commands(bad, "tvos", None, "arm64", "17.0") + simulator = first.replace("platform 3", "platform 8") + self.assertEqual( + builds._check_apple_load_commands( + simulator, "tvos", "simulator", "arm64", "17.0" + ), + 1, + ) + with self.assertRaises(ValueError): + builds._check_apple_load_commands( + first, "tvos", "simulator", "arm64", "17.0" + ) + + def test_ios_simulator_binary_rejects_intel_architectures(self): + for architectures in ("x86_64", "arm64 x86_64"): + with ( + self.subTest(architectures=architectures), + patch.object( + builds.subprocess, "check_output", return_value=architectures + ) as inspect, + self.assertRaisesRegex(ValueError, "architecture"), + ): + builds.check_apple_binary( + Path("ios-simulator/libvcore.a"), + "ios", + "simulator", + {"arm64"}, + "13.0", + ) + self.assertEqual(inspect.call_count, 1) + + def test_retained_architecture_floors_do_not_raise_product_minimums(self): + for system, variant, architecture, platform, version, product in ( + ("ios", None, "arm64", 2, "13.0", "13.0"), + ("ios", "simulator", "arm64", 7, "14.0", "13.0"), + ("macos", None, "arm64", 1, "11.0", "10.15"), + ("macos", None, "x86_64", 1, "10.15", "10.15"), + ): + output = ( + "lib.a(std.o):\nLoad command 0\n cmd LC_BUILD_VERSION\n" + f" platform {platform}\n minos {version}\n" + ) + self.assertEqual( + builds._check_apple_load_commands( + output, system, variant, architecture, product + ), + 1, + ) + with ( + patch.dict(os.environ, {"VCORE_TVOS_DEPLOYMENT_TARGET": "16.0"}), + self.assertRaisesRegex(ValueError, "17.0"), + ): + builds.tvos_deployment_target() + + def test_five_slice_manifest_rejects_missing_tvos_or_mislabelled_ios(self): + slices = { + "ios-arm64": ("ios", None, ["arm64"]), + "ios-arm64-simulator": ("ios", "simulator", ["arm64"]), + "macos-arm64_x86_64": ("macos", None, ["arm64", "x86_64"]), + "tvos-arm64": ("tvos", None, ["arm64"]), + "tvos-arm64-simulator": ("tvos", "simulator", ["arm64"]), + } + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + framework = root / "LibVCore.xcframework" + libraries = [] + for identifier, (platform, variant, architectures) in slices.items(): + folder = framework / identifier + (folder / "Headers").mkdir(parents=True) + (folder / "libvcore.a").write_bytes(builds.EXPECTED_IDENTITY) + (folder / "Headers/vcore.h").write_text("header fixture") + (folder / "Headers/module.modulemap").write_text("module fixture") + library = dict( + LibraryIdentifier=identifier, + SupportedPlatform=platform, + SupportedArchitectures=architectures, + LibraryPath="libvcore.a", + HeadersPath="Headers", + ) + if variant: + library["SupportedPlatformVariant"] = variant + libraries.append(library) + manifest = root / "vcore-delivery.json" + + def write(rows): + (framework / "Info.plist").write_bytes( + plistlib.dumps({"AvailableLibraries": rows}) + ) + record = dict( + formatVersion=1, + profile="release", + group="apple", + source={"fixture": True}, + features=builds.DEFAULT_FEATURES.split(","), + buildIdentity=builds.EXPECTED_IDENTITY.decode(), + toolchain={ + key: "fixture" + for key in ( + "rustc", + "cargo", + "xcode", + "iphoneos", + "iphonesimulator", + "macosx", + "appletvos", + "appletvsimulator", + ) + }, + ) + record["toolchain"].update( + iosDeploymentTarget="13.0", + macosDeploymentTarget="10.15", + tvosDeploymentTarget="17.0", + ) + record["artifacts"] = [ + dict( + path=p.relative_to(root).as_posix(), + size=p.stat().st_size, + sha256=hashlib.sha256(p.read_bytes()).hexdigest(), + ) + for p in sorted(framework.rglob("*")) + if p.is_file() + ] + manifest.write_text(json.dumps(record)) + + with ( + patch.object( + platform_delivery, "_source", return_value={"fixture": True} + ), + patch.object(builds, "check_apple_binary") as check, + ): + write(libraries) + platform_delivery._check_delivery([manifest]) + self.assertEqual(check.call_count, 5) + self.assertEqual( + check.call_args_list[1].args[1:], + ("ios", "simulator", {"arm64"}, "13.0"), + ) + self.assertEqual( + check.call_args.args[1:], ("tvos", "simulator", {"arm64"}, "17.0") + ) + for mutation in ( + "missing", + "platform", + "variant", + "architecture", + "ios-intel", + "ios-universal", + "ios-old-identifier", + ): + rows = copy.deepcopy(libraries) + if mutation == "missing": + rows.pop() + elif mutation == "platform": + rows[-1]["SupportedPlatform"] = "ios" + elif mutation == "variant": + del rows[-1]["SupportedPlatformVariant"] + elif mutation == "architecture": + rows[-1]["SupportedArchitectures"] = ["x86_64"] + elif mutation == "ios-intel": + rows[1]["SupportedArchitectures"] = ["x86_64"] + elif mutation == "ios-universal": + rows[1]["SupportedArchitectures"].append("x86_64") + else: + rows[1]["LibraryIdentifier"] = "ios-arm64_x86_64-simulator" + write(rows) + with self.subTest(mutation=mutation), self.assertRaises(ValueError): + platform_delivery._check_delivery([manifest]) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/tests/test_core_checks.py b/scripts/tests/test_core_checks.py deleted file mode 100644 index e65b634..0000000 --- a/scripts/tests/test_core_checks.py +++ /dev/null @@ -1,87 +0,0 @@ -"""Offline checks of the supported validation entrypoint, not milestone counts.""" - -import contextlib -import io -import unittest -from unittest.mock import patch - -from vcore_scripts import cli -from vcore_scripts.core_checks import commands, validate_execution -from vcore_scripts.protocol_catalogs import check_protocol_catalogs -from vcore_scripts.protocol_evidence import check_limit_references, load_manifest - - -class CoreChecksTest(unittest.TestCase): - def test_legacy_server_suites_fail_before_acquiring_resources(self): - for suite in ("foundations", "trojan"): - with ( - self.subTest(suite=suite), - patch("vcore_scripts.protocol_harness.tempfile.mkdtemp") as create_run, - patch("subprocess.Popen") as spawn, - contextlib.redirect_stderr(io.StringIO()) as error, - ): - self.assertEqual( - cli.main(["check", "protocol-interop", "--suite", suite]), 1 - ) - self.assertIn("not fully containerized", error.getvalue()) - create_run.assert_not_called() - spawn.assert_not_called() - - def test_catalog_validity_does_not_claim_executed_behavior(self): - with contextlib.redirect_stdout(io.StringIO()) as output: - check_protocol_catalogs() - self.assertIn('"behavior_status": "NOT RUN"', output.getvalue()) - check_limit_references(load_manifest()) - - def test_profiles_select_tests_without_running_all_target_servers(self): - for profile in ("debug", "release", "features"): - selected = commands(profile) - self.assertTrue(selected) - for command in selected: - if command[:2] == ["cargo", "test"]: - self.assertTrue( - "--all-targets" not in command or "--no-run" in command - ) - self.assertNotIn("--ignored", command) - if profile != "features": - self.assertTrue( - all( - ("--release" in command) == (profile == "release") - for command in selected - ) - ) - with self.assertRaises(ValueError): - commands("missing") - - def test_empty_partial_failed_or_unjoined_execution_fails(self): - command = ["cargo", "test", "--test", "one", "--test", "two"] - ok = "test result: ok. 2 passed; 0 failed; 0 ignored;" - validate_execution(command, ok + "\n" + ok, 0, True) - for output, code, cleanup in ( - ("", 0, True), - (ok, 0, True), - (ok + "\ntest result: ok. 0 passed; 0 failed; 3 ignored;", 0, True), - (ok + "\n" + ok, 1, True), - (ok + "\n" + ok, 0, False), - ): - with ( - self.subTest(output=output, code=code, cleanup=cleanup), - self.assertRaises(RuntimeError), - ): - validate_execution(command, output, code, cleanup) - - def test_list_mode_is_read_only_and_capability_names_resolve(self): - with contextlib.redirect_stdout(io.StringIO()) as output: - self.assertEqual(cli.main(["check", "core", "--list"]), 0) - self.assertEqual( - cli.main( - ["check", "protocol-interop", "--suite", "integration", "--list"] - ), - 0, - ) - self.assertIn("shadowsocks_backpressure", output.getvalue()) - self.assertIn("INTEGRATION-PAIR-SS-SS", output.getvalue()) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_fingerprint_cli.py b/scripts/tests/test_fingerprint_cli.py deleted file mode 100644 index 5b18af5..0000000 --- a/scripts/tests/test_fingerprint_cli.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Profile CLI help/listing never acquires a network or starts a test peer.""" - -import contextlib -import io -import tempfile -import unittest -from pathlib import Path -from unittest.mock import patch - -from vcore_scripts.mihomo_isolation import exclusive_run -from vcore_scripts.protocol_fingerprint import main - - -class FingerprintCliTest(unittest.TestCase): - def test_help_exposes_explicit_profile_selection_without_running(self): - output = io.StringIO() - with contextlib.redirect_stdout(output), self.assertRaises(SystemExit) as error: - main(["--help"]) - self.assertEqual(error.exception.code, 0) - self.assertIn("--client-fingerprint", output.getvalue()) - for name in ( - "none", - "chrome", - "chrome120", - "firefox", - "firefox120", - "safari", - "safari16", - ): - self.assertIn(name, output.getvalue()) - - def test_option_between_output_and_cases_reaches_pre_io_selection_check(self): - for name in ( - "none", - "chrome", - "chrome120", - "firefox", - "firefox120", - "safari", - "safari16", - ): - # This argument-only test can run inside a locked native suite. - # Exercise the real lock on its own temporary filesystem boundary. - with ( - tempfile.TemporaryDirectory() as temporary, - patch( - "vcore_scripts.protocol_fingerprint.exclusive_run", - lambda: exclusive_run(Path(temporary) / "cli.lock"), - ), - self.assertRaisesRegex(ValueError, "invalid VLESS native selection"), - ): - main(["unused-output", "--client-fingerprint", name, "missing"]) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_fingerprint_reference.py b/scripts/tests/test_fingerprint_reference.py deleted file mode 100644 index 3e2c6ef..0000000 --- a/scripts/tests/test_fingerprint_reference.py +++ /dev/null @@ -1,82 +0,0 @@ -"""Offline checks of the selected-profile reference driver interface.""" - -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.builds import CORE_DIR -from vcore_scripts.protocol_fingerprint_reference import ( - check_reference_report, - reference_cases, - run_reference, -) - - -class FingerprintReferenceTest(unittest.TestCase): - def test_partial_or_unclean_reference_is_not_a_completed_baseline(self): - for changed in ( - {"selection": "partial"}, - {"cleanup": False}, - {"source_unchanged": False}, - ): - report = dict( - scope="selected-v1", - status="CAPTURED", - selection="full", - cleanup=True, - source_unchanged=True, - cases=[], - ) - report.update(changed) - with self.assertRaises(ValueError): - check_reference_report(report) - - def test_invalid_selection_has_no_filesystem_or_network_effects(self): - with tempfile.TemporaryDirectory() as directory: - output = Path(directory) / "not-created" - for cases in ( - [], - ["missing"], - ["FINGERPRINT-REFERENCE-chrome-tcp-16-0"] * 2, - ): - with self.assertRaises(ValueError): - run_reference(output, cases) - self.assertFalse(output.exists()) - - def test_catalog_covers_selected_names_contexts_and_successive_connections(self): - cases = reference_cases() - self.assertEqual(len({case["id"] for case in cases}), len(cases)) - self.assertEqual( - {case["profile"] for case in cases}, - { - "none", - "chrome", - "chrome120", - "firefox", - "firefox120", - "safari", - "safari16", - }, - ) - for profile in ("chrome", "chrome120", "firefox", "safari"): - selected = [c for c in cases if c["profile"] == profile] - self.assertEqual( - {c["context"] for c in selected}, - {"tcp", "ws", "grpc", "reality", "tls12", "tls13"}, - ) - self.assertEqual({c["attempt"] for c in selected}, {0, 1}) - self.assertEqual({c["sni_length"] for c in cases}, {16, 80}) - catalog = {case["id"]: case for case in cases} - samples = json.loads( - (CORE_DIR / "tests/fingerprints/mihomo-selected-v1.json").read_text() - )["samples"] - for sample in samples: - with self.subTest(sample=sample["case_id"]): - case = catalog[sample["case_id"]] - for field in ("profile", "context", "template"): - self.assertEqual(sample[field], case[field]) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_mihomo_release.py b/scripts/tests/test_mihomo_release.py deleted file mode 100644 index b16d835..0000000 --- a/scripts/tests/test_mihomo_release.py +++ /dev/null @@ -1,416 +0,0 @@ -from __future__ import annotations - -import gzip -import hashlib -import http.client -import io -import stat -import tempfile -import unittest -import urllib.error -import zipfile -from contextlib import redirect_stdout -from pathlib import Path -from unittest.mock import patch - -from vcore_scripts import mihomo_release - -LATEST_VERSION_URL = ( - "https://github.com/MetaCubeX/mihomo/releases/latest/download/version.txt" -) -RELEASE_BASE = "https://github.com/MetaCubeX/mihomo/releases/download" -TARGET_ASSET_PREFIXES = { - "darwin-arm64": "mihomo-darwin-arm64", - "darwin-amd64": "mihomo-darwin-amd64", - "linux-arm64": "mihomo-linux-arm64", - "linux-amd64": "mihomo-linux-amd64-v1", - "windows-arm64": "mihomo-windows-arm64", - "windows-amd64": "mihomo-windows-amd64-v1", -} - - -class Response(io.BytesIO): - def __init__(self, contents: bytes, url: str): - super().__init__(contents) - self.url = url - - def geturl(self): - return self.url - - def read(self, size=-1): - if size <= 0: - raise AssertionError("Network responses must be read in bounded chunks") - return super().read(size) - - def read1(self, size=-1): - return self.read(size) - - -def zip_contents(entries: list[tuple[str | zipfile.ZipInfo, bytes]]) -> bytes: - output = io.BytesIO() - with zipfile.ZipFile(output, "w", compression=zipfile.ZIP_DEFLATED) as archive: - for name, contents in entries: - archive.writestr(name, contents) - return output.getvalue() - - -def archive_fixture(target="darwin-arm64", contents=b"fixture executable\n"): - if target.startswith("windows-"): - return zip_contents([("mihomo.exe", contents)]) - return gzip.compress(contents, mtime=0) - - -def asset_name(target, tag): - suffix = "zip" if target.startswith("windows-") else "gz" - return f"{TARGET_ASSET_PREFIXES[target]}-{tag}.{suffix}" - - -class MihomoReleaseTest(unittest.TestCase): - def setUp(self): - directory = self.enterContext(tempfile.TemporaryDirectory()) - self.root = Path(directory) / "core" - self.enterContext(patch.object(mihomo_release, "CORE_DIR", self.root)) - self.urlopen = self.enterContext( - patch( - "urllib.request.urlopen", - side_effect=AssertionError("Unexpected network request"), - ) - ) - self.output = self.enterContext(redirect_stdout(io.StringIO())) - - def binary_path(self, target="darwin-arm64", tag="v1.19.31"): - return ( - self.root - / "target" - / "interop" - / "mihomo" - / tag - / target - / ("mihomo.exe" if target.startswith("windows-") else "mihomo") - ) - - def archive_url(self, target="darwin-arm64", tag="v1.19.31"): - return f"{RELEASE_BASE}/{tag}/{asset_name(target, tag)}" - - def asset_response(self, archive, target="darwin-arm64", tag="v1.19.31"): - return Response(archive, self.archive_url(target, tag)) - - def version_response(self, tag="v1.19.31"): - return Response(f"{tag}\n".encode(), LATEST_VERSION_URL) - - def provide_archive(self, archive, target="darwin-arm64", tag="v1.19.31"): - self.urlopen.side_effect = None - self.urlopen.return_value = self.asset_response(archive, target, tag) - - def assert_download_rejected(self, archive, target="darwin-arm64"): - self.provide_archive(archive, target) - with self.assertRaises(RuntimeError): - mihomo_release.download_mihomo(target, release="v1.19.31") - binary = self.binary_path(target) - self.assertFalse(binary.exists()) - self.assertFalse(any(binary.parent.glob(".download-*"))) - - def test_target_mapping_is_pure_and_supports_host_aliases(self): - self.assertEqual( - set(mihomo_release.SUPPORTED_TARGETS), set(TARGET_ASSET_PREFIXES) - ) - for system in ("Darwin", "Linux", "Windows"): - for machine, architecture in ( - ("arm64", "arm64"), - ("aarch64", "arm64"), - ("amd64", "amd64"), - ("x86_64", "amd64"), - ("AMD64", "amd64"), - ): - with ( - self.subTest(system=system, machine=machine), - patch.object( - mihomo_release.platform, "system", return_value=system - ), - patch.object( - mihomo_release.platform, "machine", return_value=machine - ), - ): - self.assertEqual( - mihomo_release._target(None), f"{system.lower()}-{architecture}" - ) - for target in TARGET_ASSET_PREFIXES: - self.assertEqual(mihomo_release._target(target), target) - self.urlopen.assert_not_called() - self.assertFalse(self.root.exists()) - - def test_unsupported_target_fails_before_network_or_filesystem_changes(self): - for target in ("freebsd-amd64", "linux-386", "../../outside"): - with self.subTest(target=target), self.assertRaises(RuntimeError): - mihomo_release.download_mihomo(target) - for system, machine in (("FreeBSD", "amd64"), ("Linux", "riscv64")): - with ( - self.subTest(system=system, machine=machine), - patch.object(mihomo_release.platform, "system", return_value=system), - patch.object(mihomo_release.platform, "machine", return_value=machine), - self.assertRaises(RuntimeError), - ): - mihomo_release.download_mihomo() - self.urlopen.assert_not_called() - self.assertFalse(self.root.exists()) - - def test_downloads_expected_gzip_and_zip_asset_for_each_target(self): - payload = b"offline executable fixture\x00\xff" - for target in TARGET_ASSET_PREFIXES: - with self.subTest(target=target): - archive = archive_fixture(target, payload) - self.provide_archive(archive, target) - binary = mihomo_release.download_mihomo(target, release="v1.19.31") - self.assertEqual(binary, self.binary_path(target)) - self.assertEqual(binary.read_bytes(), payload) - self.assertEqual(list(binary.parent.iterdir()), [binary]) - request = self.urlopen.call_args.args[0] - self.assertEqual(request.full_url, self.archive_url(target)) - self.assertEqual(request.get_method(), "GET") - self.assertEqual(self.urlopen.call_args.kwargs["timeout"], 30) - if not target.startswith("windows-"): - self.assertTrue(binary.stat().st_mode & stat.S_IXUSR) - report = self.output.getvalue() - for value in ( - "v1.19.31", - asset_name(target, "v1.19.31"), - self.archive_url(target), - hashlib.sha256(archive).hexdigest(), - hashlib.sha256(payload).hexdigest(), - str(binary), - ): - self.assertIn(value, report) - - def test_latest_release_gets_version_text_on_every_call(self): - self.urlopen.side_effect = [ - Response(b" v1.19.31\r\n", LATEST_VERSION_URL), - self.version_response("v1.19.32"), - ] - self.assertEqual(mihomo_release.latest_release(), "v1.19.31") - self.assertEqual(mihomo_release.latest_release(), "v1.19.32") - self.assertEqual(self.urlopen.call_count, 2) - for call in self.urlopen.call_args_list: - self.assertEqual(call.args[0].full_url, LATEST_VERSION_URL) - self.assertEqual(call.args[0].get_method(), "GET") - self.assertEqual(call.kwargs["timeout"], 30) - self.assertFalse(self.root.exists()) - - def test_latest_version_change_downloads_into_new_version_directory(self): - old_archive = archive_fixture(contents=b"old") - new_archive = archive_fixture(contents=b"new") - self.urlopen.side_effect = [ - self.version_response("v1.19.31"), - self.asset_response(old_archive, tag="v1.19.31"), - self.version_response("v1.19.32"), - self.asset_response(new_archive, tag="v1.19.32"), - ] - old_binary = mihomo_release.download_mihomo("darwin-arm64") - new_binary = mihomo_release.download_mihomo("darwin-arm64") - self.assertNotEqual(old_binary, new_binary) - self.assertEqual(old_binary.read_bytes(), b"old") - self.assertEqual(new_binary.read_bytes(), b"new") - self.assertEqual(new_binary, self.binary_path(tag="v1.19.32")) - self.assertEqual( - [call.args[0].full_url for call in self.urlopen.call_args_list], - [ - LATEST_VERSION_URL, - self.archive_url(tag="v1.19.31"), - LATEST_VERSION_URL, - self.archive_url(tag="v1.19.32"), - ], - ) - - def test_same_release_is_downloaded_again_even_when_binary_exists(self): - self.urlopen.side_effect = [ - self.asset_response(archive_fixture(contents=b"first download")), - self.asset_response(archive_fixture(contents=b"second download")), - ] - first = mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - self.assertEqual(first.read_bytes(), b"first download") - second = mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - self.assertEqual(first, second) - self.assertEqual(second.read_bytes(), b"second download") - self.assertEqual(self.urlopen.call_count, 2) - - def test_version_lookup_failure_does_not_return_existing_binary(self): - binary = self.binary_path() - binary.parent.mkdir(parents=True) - binary.write_bytes(b"previous valid binary") - self.urlopen.side_effect = urllib.error.URLError("offline fixture") - with self.assertRaises(RuntimeError): - mihomo_release.download_mihomo("darwin-arm64") - self.urlopen.assert_called_once() - self.assertEqual(self.urlopen.call_args.args[0].full_url, LATEST_VERSION_URL) - self.assertEqual(binary.read_bytes(), b"previous valid binary") - - def test_latest_release_rejects_invalid_utf8_unsafe_or_oversized_versions(self): - for contents in ( - b"\xff", - b"", - b" \n", - b"../outside", - b"v1/other", - b"v1\\other", - b".", - b"v1\nv2", - b'{"tag_name":"v1.19.31"}', - b"v" * 129, - ): - self.urlopen.side_effect = None - self.urlopen.return_value = Response(contents, LATEST_VERSION_URL) - with self.subTest(contents=contents), self.assertRaises(RuntimeError): - mihomo_release.latest_release() - self.urlopen.return_value = self.version_response() - with ( - patch.object(mihomo_release, "MAX_VERSION_BYTES", 4), - self.assertRaises(RuntimeError), - ): - mihomo_release.latest_release() - self.assertFalse(self.root.exists()) - - def test_explicit_unsafe_release_fails_before_network_or_filesystem_changes(self): - for tag in ("../outside", "v1/other", "v1\\other", ".", "", "v" * 129): - with self.subTest(tag=tag), self.assertRaises(RuntimeError): - mihomo_release.download_mihomo("darwin-arm64", release=tag) - self.urlopen.assert_not_called() - self.assertFalse(self.root.exists()) - - def test_network_or_archive_failure_preserves_existing_binary(self): - binary = self.binary_path() - binary.parent.mkdir(parents=True) - binary.write_bytes(b"previous valid binary") - for failure in ("network", "archive"): - if failure == "network": - self.urlopen.side_effect = urllib.error.URLError("offline fixture") - else: - self.provide_archive(b"invalid gzip") - with self.subTest(failure=failure), self.assertRaises(RuntimeError): - mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - self.assertEqual(binary.read_bytes(), b"previous valid binary") - self.assertEqual(list(binary.parent.iterdir()), [binary]) - - def test_corrupt_gzip_or_zip_never_installs_binary(self): - for target in ("darwin-arm64", "windows-amd64"): - with self.subTest(target=target): - self.assert_download_rejected(b"not an archive", target) - - def test_gzip_crc_failure_after_payload_read_preserves_existing_binary(self): - binary = self.binary_path() - binary.parent.mkdir(parents=True) - binary.write_bytes(b"previous valid binary") - archive = bytearray(archive_fixture(contents=b"new binary" * 10000)) - archive[-8] ^= 0xFF - self.provide_archive(bytes(archive)) - decoded_sizes = [] - read1 = gzip.GzipFile.read1 - - def record_decoded_chunk(source, size=-1): - chunk = read1(source, size) - decoded_sizes.append(len(chunk)) - return chunk - - with ( - patch.object(gzip.GzipFile, "read1", record_decoded_chunk), - self.assertRaises(RuntimeError), - ): - mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - self.assertGreater(sum(decoded_sizes), 0) - self.assertEqual(binary.read_bytes(), b"previous valid binary") - self.assertEqual(list(binary.parent.iterdir()), [binary]) - - def test_archive_download_is_bounded(self): - archive = archive_fixture() - with patch.object(mihomo_release, "MAX_ARCHIVE_BYTES", len(archive) - 1): - self.assert_download_rejected(archive) - - def test_gzip_and_zip_decompressed_binary_sizes_are_bounded(self): - for target in ("darwin-arm64", "windows-amd64"): - archive = archive_fixture(target, b"A" * 4096) - with ( - self.subTest(target=target), - patch.object(mihomo_release, "MAX_BINARY_BYTES", 128), - ): - self.assert_download_rejected(archive, target) - - def test_interrupted_http_stream_is_reported_without_installing_binary(self): - for version in (True, False): - response = ( - self.version_response() - if version - else self.asset_response(archive_fixture()) - ) - self.urlopen.side_effect = None - self.urlopen.return_value = response - with ( - self.subTest(version=version), - patch.object( - response, - "read1", - side_effect=http.client.IncompleteRead(b"partial", 100), - ), - self.assertRaises(RuntimeError), - ): - if version: - mihomo_release.latest_release() - else: - mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - binary = self.binary_path() - self.assertFalse(binary.exists()) - self.assertFalse(any(binary.parent.glob(".download-*"))) - - def test_http_redirect_downgrade_is_rejected(self): - for version in (True, False): - self.urlopen.side_effect = None - self.urlopen.return_value = Response( - b"v1.19.31" if version else archive_fixture(), - "http://example.com/download", - ) - with self.subTest(version=version), self.assertRaises(RuntimeError): - if version: - mihomo_release.latest_release() - else: - mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - self.assertFalse(self.binary_path().exists()) - - def test_interrupted_binary_publication_preserves_previous_binary(self): - binary = self.binary_path() - binary.parent.mkdir(parents=True) - binary.write_bytes(b"previous valid binary") - self.provide_archive(archive_fixture(contents=b"new binary")) - with ( - patch.object( - mihomo_release.os, "replace", side_effect=OSError("publication failure") - ), - self.assertRaises(RuntimeError), - ): - mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31") - self.assertEqual(binary.read_bytes(), b"previous valid binary") - self.assertEqual(list(binary.parent.iterdir()), [binary]) - self.provide_archive(archive_fixture(contents=b"new binary")) - self.assertEqual( - mihomo_release.download_mihomo("darwin-arm64", release="v1.19.31"), binary - ) - self.assertEqual(binary.read_bytes(), b"new binary") - - def test_zip_rejects_traversal_and_non_regular_executables(self): - symlink = zipfile.ZipInfo("mihomo.exe") - symlink.create_system = 3 - symlink.external_attr = (stat.S_IFLNK | 0o777) << 16 - for name in ("../mihomo.exe", "/mihomo.exe", "..\\mihomo.exe", symlink): - archive = zip_contents([(name, b"outside")]) - with self.subTest(name=str(name)): - self.assert_download_rejected(archive, "windows-amd64") - self.assertFalse((self.root.parent / "mihomo.exe").exists()) - - def test_zip_rejects_missing_or_multiple_executables(self): - for entries in ( - [("readme.txt", b"no executable")], - [("mihomo.exe", b"first"), ("other.exe", b"second")], - ): - archive = zip_contents(entries) - with self.subTest(entries=entries): - self.assert_download_rejected(archive, "windows-amd64") - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_native_release.py b/scripts/tests/test_native_release.py deleted file mode 100644 index 9e958e4..0000000 --- a/scripts/tests/test_native_release.py +++ /dev/null @@ -1,137 +0,0 @@ -from __future__ import annotations - -import hashlib -import io -import tarfile -import tempfile -import unittest -import urllib.error -import zipfile -from pathlib import Path -from unittest.mock import patch - -from vcore_scripts import native_release - - -class Response(io.BytesIO): - def geturl(self): - return "https://release-assets.githubusercontent.com/fixture" - - -class NativeReleaseTest(unittest.TestCase): - def test_shadowtls_uses_latest_unmodified_binary_with_deferred_guest_version(self): - with ( - tempfile.TemporaryDirectory() as directory, - patch( - "urllib.request.urlopen", return_value=Response(b"official-st-binary") - ) as request, - patch("vcore_scripts.native_release.run_command") as run, - ): - peer = native_release.download_native( - "ST", Path(directory), "linux-arm64", defer_version=True - ) - self.assertEqual( - request.call_args.args[0].full_url, - "https://github.com/ihciah/shadow-tls/releases/latest/download/shadow-tls-aarch64-unknown-linux-musl", - ) - self.assertEqual(peer.binary.name, "shadow-tls") - self.assertEqual(peer.binary.read_bytes(), b"official-st-binary") - self.assertEqual( - peer.identity["binary_sha256"], - hashlib.sha256(b"official-st-binary").hexdigest(), - ) - self.assertIsNone(peer.identity["version"]) - run.assert_not_called() - - def test_ss_tar_is_bounded_named_only_and_rejects_links(self): - def archive(unsafe=False): - data = io.BytesIO() - with tarfile.open(fileobj=data, mode="w:xz") as bundle: - entry = tarfile.TarInfo("ssserver") - entry.size = 7 - bundle.addfile(entry, io.BytesIO(b"fixture")) - entry = tarfile.TarInfo("sslocal") - if unsafe: - entry.type, entry.linkname = tarfile.SYMTYPE, "/tmp/forbidden" - bundle.addfile(entry) - return data.getvalue() - - with tempfile.TemporaryDirectory() as directory: - path, binary = Path(directory) / "peer.tar.xz", Path(directory) / "ssserver" - path.write_bytes(archive()) - self.assertEqual( - native_release._extract_tar(path, binary, "ssserver"), - hashlib.sha256(b"fixture").hexdigest(), - ) - self.assertEqual(binary.read_bytes(), b"fixture") - path.write_bytes(archive(True)) - with self.assertRaises(RuntimeError): - native_release._extract_tar(path, binary, "ssserver") - - def test_foreign_binary_version_is_deferred_to_its_container(self): - archive = io.BytesIO() - with zipfile.ZipFile(archive, "w") as bundle: - bundle.writestr("v2ray", b"linux-fixture") - with ( - tempfile.TemporaryDirectory() as directory, - patch("urllib.request.urlopen", return_value=Response(archive.getvalue())), - patch("vcore_scripts.native_release.run_command") as run, - ): - peer = native_release.download_native( - "V2", Path(directory), "linux-arm64", defer_version=True - ) - run.assert_not_called() - self.assertIsNone(peer.identity["version"]) - self.assertEqual(peer.binary.read_bytes(), b"linux-fixture") - - def test_failed_download_never_uses_old_binary_or_leaks_network_details(self): - with ( - tempfile.TemporaryDirectory() as directory, - patch( - "urllib.request.urlopen", - side_effect=urllib.error.URLError("synthetic-private-marker"), - ), - ): - binary = Path(directory) / "v2ray" - binary.write_bytes(b"older executable") - with self.assertRaises(RuntimeError) as caught: - native_release.download_native("V2", Path(directory), "darwin-arm64") - self.assertNotIn("synthetic-private-marker", str(caught.exception)) - self.assertEqual(binary.read_bytes(), b"older executable") - self.assertEqual(list(Path(directory).iterdir()), [binary]) - - def test_v2ray_uses_latest_binary_and_records_content_identity(self): - archive = io.BytesIO() - with zipfile.ZipFile(archive, "w") as bundle: - bundle.writestr("v2ray", b"fixture-executable") - bundle.writestr("geoip.dat", b"unused") - with ( - tempfile.TemporaryDirectory() as directory, - patch( - "urllib.request.urlopen", return_value=Response(archive.getvalue()) - ) as request, - patch("vcore_scripts.native_release.run_command") as run, - ): - run.return_value.stdout = b"V2Ray 5.53.0 fixture\n" - run.return_value.returncode = 0 - run.return_value.cleanup = True - peer = native_release.download_native("V2", Path(directory), "darwin-arm64") - self.assertEqual( - request.call_args.args[0].full_url, - "https://github.com/v2fly/v2ray-core/releases/latest/download/v2ray-macos-arm64-v8a.zip", - ) - self.assertEqual(peer.binary.read_bytes(), b"fixture-executable") - self.assertEqual( - peer.identity["binary_sha256"], - hashlib.sha256(b"fixture-executable").hexdigest(), - ) - self.assertEqual( - peer.identity["archive_sha256"], - hashlib.sha256(archive.getvalue()).hexdigest(), - ) - self.assertEqual(peer.identity["version"], "V2Ray 5.53.0 fixture") - self.assertFalse((Path(directory) / "geoip.dat").exists()) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_platform_delivery.py b/scripts/tests/test_platform_delivery.py index c0c2a3f..af82aae 100644 --- a/scripts/tests/test_platform_delivery.py +++ b/scripts/tests/test_platform_delivery.py @@ -18,6 +18,23 @@ from vcore_scripts.cli import main class PlatformDeliveryTest(unittest.TestCase): + def test_delivery_rejects_benchmark_http_feature_before_reading_source_or_building( + self, + ): + with ( + patch.dict( + os.environ, + {"VCORE_FEATURES": builds.DEFAULT_FEATURES + ",benchmark-geodata-http"}, + clear=True, + ), + patch.object(platform_delivery, "_source") as source, + patch.object(builds, "build_apple") as build, + ): + with self.assertRaisesRegex(ValueError, "production feature set"): + platform_delivery.build_delivery("apple") + source.assert_not_called() + build.assert_not_called() + def test_all_delivery_platforms_reject_linked_output_before_mutation(self): for platform_name in ("apple", "windows", "android"): relative = Path("dist") / platform_name @@ -98,10 +115,34 @@ class PlatformDeliveryTest(unittest.TestCase): def test_android_delivery_replaces_stale_abis_without_touching_other_outputs(self): with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - ndk = root / "ndk" - ndk.mkdir() - (ndk / "source.properties").write_text("Pkg.Revision = fixture") + root = Path(directory).resolve() + ndk = root / "Library/Android/sdk/ndk/30.0.16248370" + ndk.mkdir(parents=True) + (ndk / "source.properties").write_text("Pkg.Revision = 30.0.16248370") + for version, revision in ( + ("30.0.9", "30.0.9"), + ("30.99.99", "30.99.99-beta1"), + ): + other = ndk.parent / version + other.mkdir() + (other / "source.properties").write_text(f"Pkg.Revision = {revision}") + (ndk / "bin").mkdir() + (ndk / "bin/llvm-ar").touch() + for target, machine in ( + ("aarch64-linux-android", 183), + ("x86_64-linux-android", 62), + ): + _, clang, _ = builds._android_target(target, "24") + (ndk / "bin" / clang).touch() + (ndk / "bin" / (clang + "++")).touch() + runtime = ndk / "sysroot/usr/lib" / target / "libc++_shared.so" + runtime.parent.mkdir(parents=True) + runtime.write_bytes( + b"\x7fELF\x02\x01\x01" + + bytes(9) + + b"\x03\x00" + + machine.to_bytes(2, "little") + ) output = root / "dist/android" stale = output / "armeabi-v7a/libvcore.so" stale.parent.mkdir(parents=True) @@ -111,43 +152,139 @@ class PlatformDeliveryTest(unittest.TestCase): unrelated.parent.mkdir(parents=True) unrelated.write_bytes(b"keep") - def build(): + def build(target, _profile, _features, environment): self.assertFalse(stale.exists()) self.assertFalse((output / "vcore-delivery.json").exists()) - for abi, machine in [("arm64-v8a", 183), ("x86_64", 62)]: - for name in ("libvcore.so", "libc++_shared.so"): - path = output / abi / name - path.parent.mkdir(parents=True, exist_ok=True) - path.write_bytes( - b"\x7fELF\x02\x01\x01" - + bytes(9) - + b"\x03\x00" - + machine.to_bytes(2, "little") - + builds.EXPECTED_IDENTITY - ) + self.assertEqual(environment["ANDROID_NDK_HOME"], str(ndk)) + self.assertEqual(environment["VCORE_CMAKE_ANDROID_API"], "24") + path = root / "target" / target / "release/libvcore.so" + path.parent.mkdir(parents=True) + path.write_bytes( + (ndk / "sysroot/usr/lib" / target / "libc++_shared.so").read_bytes() + + builds.EXPECTED_IDENTITY + ) with ( - patch.dict(os.environ, {"ANDROID_NDK_HOME": str(ndk)}, clear=True), + patch.dict(os.environ, {}, clear=True), # Windows resolves home from environment variables that this # hermetic fixture clears; do not depend on a runner profile. patch.object(Path, "home", return_value=root), patch.object(builds, "CORE_DIR", root), - patch.object(builds, "build_android", side_effect=build), - patch.object(builds, "_android_toolchain", return_value=ndk), + patch.object( + builds, + "os", + SimpleNamespace(name="posix", environ=os.environ, fspath=os.fspath), + ), + patch.object(builds, "_cargo_build", side_effect=build), + patch.object(builds, "_require_targets") as require, + patch.object( + builds, "_android_toolchain", return_value=ndk + ) as toolchain, patch.object( platform_delivery, "_source", return_value={"fixture": True} ), patch.object(platform_delivery, "_output", return_value="fixture"), ): platform_delivery.build_delivery("android") - platform_delivery.check_delivery([output / "vcore-delivery.json"]) + platform_delivery._check_delivery([output / "vcore-delivery.json"]) + require.assert_called_once_with( + ["aarch64-linux-android", "x86_64-linux-android"] + ) + self.assertEqual(toolchain.call_count, 2) + for invocation in toolchain.call_args_list: + self.assertEqual(invocation.args, (ndk,)) + record = json.loads((output / "vcore-delivery.json").read_text()) + self.assertEqual(record["toolchain"]["androidApi"], "24") + self.assertEqual(record["toolchain"]["ndk"], "Pkg.Revision = 30.0.16248370") self.assertEqual(unrelated.read_bytes(), b"keep") + def test_android_ndk_major_uses_numeric_stable_revisions_and_honors_overrides(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory).resolve() + installed = root / "sdk/ndk" + for version, revision in ( + ("30.2.9", "30.2.9"), + ("30.2.10", "30.2.10"), + ("30.10.1", "30.10.1"), + ("31.1.1", "31.1.1"), + ("30.99.99", "30.99.99-beta1"), + ("30.99.99-beta1", "30.99.99"), + ("30.88.1", "30.88.2"), + ("30.77.1", None), + ("32.1.1", "32.1.1-rc1"), + ): + ndk = installed / version + ndk.mkdir(parents=True) + if revision: + (ndk / "source.properties").write_text( + f"Pkg.Desc = Android NDK\nPkg.Revision = {revision}\n" + ) + for overrides, expected in ( + ({}, installed / "30.10.1"), + ({"VCORE_ANDROID_NDK_VERSION": "30"}, installed / "30.10.1"), + ({"VCORE_ANDROID_NDK_VERSION": "31"}, installed / "31.1.1"), + ({"VCORE_ANDROID_NDK_VERSION": "30.2.9"}, installed / "30.2.9"), + ( + { + "ANDROID_NDK_HOME": str(root / "explicit-ndk"), + "VCORE_ANDROID_NDK_VERSION": "32", + }, + root / "explicit-ndk", + ), + ): + with ( + self.subTest(overrides=overrides), + patch.dict( + os.environ, + {"ANDROID_HOME": str(root / "sdk")} | overrides, + clear=True, + ), + patch.object( + Path, + "home", + side_effect=RuntimeError("Could not determine home directory"), + ) as home, + ): + self.assertEqual(builds._android_ndk_home(), expected) + home.assert_not_called() + with ( + patch.dict( + os.environ, + { + "ANDROID_HOME": str(root / "sdk"), + "VCORE_ANDROID_NDK_VERSION": "32", + }, + clear=True, + ), + patch.object( + Path, + "home", + side_effect=RuntimeError("Could not determine home directory"), + ), + self.assertRaisesRegex(RuntimeError, "stable Android NDK.*32"), + ): + builds._android_ndk_home() + + def test_android_ndk_default_sdk_uses_home_only_without_sdk_override(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory).resolve() + ndk = root / "Library/Android/sdk/ndk/30.1.1" + ndk.mkdir(parents=True) + (ndk / "source.properties").write_text("Pkg.Revision = 30.1.1\n") + for sdk_environment in ({}, {"ANDROID_HOME": ""}): + with ( + self.subTest(environment=sdk_environment), + patch.dict(os.environ, sdk_environment, clear=True), + patch.object(Path, "home", return_value=root) as home, + ): + self.assertEqual(builds._android_ndk_home(), ndk) + home.assert_called_once_with() + def test_delivery_rejects_debug_before_starting_a_build(self): with patch.dict(os.environ, {"VCORE_BUILD_PROFILE": "debug"}): self.assertEqual(main(["build", "android", "--delivery"]), 1) - def test_android_evidence_checks_real_files_not_claimed_success(self): + def test_android_build_self_check_rejects_wrong_artifacts_and_identity(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) source = root / "source" @@ -242,15 +379,8 @@ class PlatformDeliveryTest(unittest.TestCase): "artifacts": artifacts, } manifest.write_text(json.dumps(record)) - command = [ - "check", - "platform-artifacts", - "--manifest", - str(manifest), - "--source-dir", - str(source), - ] - self.assertEqual(main(command), 0) + with patch.object(builds, "CORE_DIR", source): + platform_delivery._check_delivery([manifest]) for mutation in ( "profile", "features", @@ -293,14 +423,21 @@ class PlatformDeliveryTest(unittest.TestCase): ).hexdigest() manifest.write_text(json.dumps(bad)) try: - self.assertEqual(main(command), 1) + with ( + patch.object(builds, "CORE_DIR", source), + self.assertRaises((ValueError, RuntimeError)), + ): + platform_delivery._check_delivery([manifest]) finally: if mutation == "architecture": file.write_bytes(original) manifest.write_text(json.dumps(record)) - self.assertEqual(main(command + ["--complete"]), 1) (output / "arm64-v8a/libc++_shared.so").unlink() - self.assertEqual(main(command), 1) + with ( + patch.object(builds, "CORE_DIR", source), + self.assertRaisesRegex(ValueError, "missing or non-regular"), + ): + platform_delivery._check_delivery([manifest]) if __name__ == "__main__": diff --git a/scripts/tests/test_protocol_completion_peers.py b/scripts/tests/test_protocol_completion_peers.py deleted file mode 100644 index 6dd5a1c..0000000 --- a/scripts/tests/test_protocol_completion_peers.py +++ /dev/null @@ -1,70 +0,0 @@ -"""Fixture contracts; real peer traffic is checked only in owned containers.""" - -import unittest -from pathlib import Path -from unittest.mock import patch - -from vcore_scripts.protocol_completion_peers import CASES, peer_configuration - - -class CompletionPeerTests(unittest.TestCase): - def test_comparison_client_refuses_host_network_io(self): - from vcore_scripts.container_protocol_client import main - - with ( - patch.dict("os.environ", {}, clear=True), - self.assertRaisesRegex(SystemExit, "container harness"), - ): - main() - - def test_tuic_v5_and_upgrade_fixtures_select_real_wire_modes(self): - tuic = [case for case in CASES if case.protocol == "tuic"] - upgrades = [case for case in CASES if case.protocol in {"vmess", "trojan"}] - self.assertEqual({case.udp_mode for case in tuic}, {"native", "quic"}) - self.assertEqual(len(upgrades), 6) - for case in tuic + upgrades: - with self.subTest(case=case.identifier): - node, listener = peer_configuration( - case, - server="192.0.2.2", - cover="192.0.2.3:24001", - port=23000, - certificate=(Path("cert.pem"), Path("key.pem"), "a" * 64), - ) - if case.protocol == "tuic": - self.assertNotIn("token", node) - self.assertEqual(listener["users"][node["uuid"]], node["password"]) - self.assertEqual(node["udp-relay-mode"], case.udp_mode) - else: - self.assertEqual(node["network"], "ws") - self.assertIs(node["ws-opts"]["v2ray-http-upgrade"], True) - self.assertEqual( - node["ws-opts"]["v2ray-http-upgrade-fast-open"], case.fast_open - ) - self.assertEqual(node["ws-opts"]["path"], listener["ws-path"]) - self.assertEqual("certificate" in listener, case.tls) - - def test_uot_fixtures_force_v2_and_do_not_offer_native_udp(self): - selected = [case for case in CASES if case.uot] - self.assertEqual(len(selected), 6) - for case in selected: - with self.subTest(case=case.identifier): - node, listener = peer_configuration( - case, - server="192.0.2.2", - cover="192.0.2.3:24001", - port=23000, - certificate=(Path("cert.pem"), Path("key.pem"), "a" * 64), - ) - self.assertEqual(node["type"], "ss") - self.assertIs(node["udp-over-tcp"], True) - self.assertEqual(node["udp-over-tcp-version"], 2) - self.assertIs(listener["udp"], False) - if case.shadow_tls: - self.assertEqual(node["plugin"], "shadow-tls") - self.assertEqual(node["plugin-opts"]["version"], 3) - self.assertIs(listener["shadow-tls"]["strict-mode"], True) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_containers.py b/scripts/tests/test_protocol_containers.py deleted file mode 100644 index c0bb78b..0000000 --- a/scripts/tests/test_protocol_containers.py +++ /dev/null @@ -1,281 +0,0 @@ -"""Offline ownership/config checks; no server is launched on the host.""" - -import contextlib -import io -import json -import tempfile -import unittest -from pathlib import Path -from types import SimpleNamespace -from unittest.mock import MagicMock, patch - -from vcore_scripts.container_udp_origin import main as origin_main -from vcore_scripts.container_udp_origin import serve as origin_serve -from vcore_scripts.protocol_containers import NETWORK, ContainerLab, ContainerPeer -from vcore_scripts.protocol_vmess_udp_ab import main, options -from vcore_scripts.protocol_vmess_udp_container import client_config - - -class ContainerTests(unittest.TestCase): - def test_frozen_latest_image_is_single_run_owned(self): - from vcore_scripts.protocol_containers import IMAGE, frozen_image - - network = dict( - configuration=dict(mode="hostOnly", labels={"purpose": NETWORK}), - status=dict(ipv4Subnet="192.0.2.0/24", ipv6Subnet="fd00::/64"), - ) - digest = "sha256:" + "a" * 64 - inspection = dict(configuration=dict(descriptor=dict(digest=digest))) - - def command(*args, **_): - return json.dumps([network if args[0] == "network" else inspection]) - - with ( - tempfile.TemporaryDirectory() as root, - patch("vcore_scripts.protocol_containers.command", side_effect=command), - patch("vcore_scripts.protocol_containers.run_command") as pull, - ): - pull.return_value = SimpleNamespace( - returncode=0, cleanup=True, stdout=b"ok" - ) - with frozen_image(Path(root) / "image.log") as snapshot: - first, second = ContainerLab({}), ContainerLab({}) - self.assertEqual(first.image, second.image) - self.assertTrue(first.image.endswith("@" + digest)) - self.assertEqual(snapshot["digest"], digest) - self.assertEqual(snapshot["tag"], IMAGE) - self.assertEqual(pull.call_count, 1) - ContainerLab({}) - self.assertEqual( - pull.call_count, 2, "no image cache across independent runs" - ) - - def test_image_refresh_failure_never_falls_back_to_a_cached_image(self): - from vcore_scripts.protocol_containers import frozen_image - - with ( - tempfile.TemporaryDirectory() as root, - patch("vcore_scripts.protocol_containers.command") as inspect, - patch("vcore_scripts.protocol_containers.run_command") as pull, - ): - pull.return_value = SimpleNamespace( - returncode=1, - cleanup=True, - stdout=b"registry unavailable token=synthetic-secret", - ) - log = Path(root) / "image.log" - with self.assertRaisesRegex(RuntimeError, "image pull"), frozen_image(log): - self.fail("failed refresh must not establish a frozen image scope") - inspect.assert_not_called() - self.assertIn("registry unavailable", log.read_text()) - self.assertNotIn("synthetic-secret", log.read_text()) - - def test_dns_fixture_remains_owned_across_idle_business_phases(self): - control, udp = MagicMock(), MagicMock() - control.recv.return_value = b"\x11" - control.getsockname.return_value = ("192.0.2.55", 24000) - udp.getsockname.return_value = ("0.0.0.0", 53001) - query = b"\x00" * 12 + b"\x0dvcore-fixture\x04test\x00\x00\x01\x00\x01" - udp.recvfrom.return_value = (query, ("192.0.2.2", 40001)) - udp.sendto.side_effect = lambda data, _peer: len(data) - with ( - patch("vcore_scripts.container_udp_origin.socket.socket") as socket, - patch("vcore_scripts.container_udp_origin.SLOTS"), - patch( - "vcore_scripts.container_udp_origin.select.select", - side_effect=[ - ([], [], []), - ([udp], [], []), - ([control], [], []), - ], - ), - ): - socket.return_value.__enter__.return_value = udp - origin_serve(control) - udp.sendto.assert_called_once() - self.assertEqual(udp.sendto.call_args.args[1], ("192.0.2.2", 40001)) - with ( - patch("vcore_scripts.container_udp_origin.socket.socket") as socket, - patch("vcore_scripts.container_udp_origin.SLOTS"), - patch( - "vcore_scripts.container_udp_origin.time.monotonic", - side_effect=[0, 241], - ), - patch("vcore_scripts.container_udp_origin.select.select") as wait, - ): - socket.return_value.__enter__.return_value = udp - origin_serve(control) - wait.assert_not_called() - - def test_quic_mtu_applies_only_to_the_owned_guest_network(self): - lab = SimpleNamespace( - run_id="fixture", mtu=1500, image="fixture@sha256:synthetic" - ) - peer = ContainerPeer(lab, Path("fixture"), "quic") - with ( - patch( - "vcore_scripts.protocol_containers.command", - side_effect=RuntimeError("captured launch"), - ) as launch, - self.assertRaisesRegex(RuntimeError, "captured launch"), - ): - peer.start(["peer"]) - arguments = launch.call_args.args - self.assertEqual( - arguments[arguments.index("--network") + 1], NETWORK + ",mtu=1500" - ) - self.assertNotIn("--cap-add", arguments) - self.assertNotIn("--publish", arguments) - - def test_long_roles_keep_bounded_distinct_owned_container_names(self): - lab = SimpleNamespace(run_id="0123456789ab") - roles = [ - "xhttp-stream-up-download-reality-tls-origin", - "xhttp-stream-up-download-reality-tls-server", - "xhttp-stream-up-download-reality-tls-upstream", - ] - peers = [ContainerPeer(lab, Path("fixture"), role) for role in roles] - self.assertEqual(len({peer.name for peer in peers}), len(roles)) - for peer, role in zip(peers, roles, strict=True): - self.assertLessEqual(len(peer.name), 63) - self.assertIn(lab.run_id, peer.name) - self.assertEqual(peer.record["role"], role) - - def test_non_private_network_fails_before_image_or_launch(self): - network = dict(configuration=dict(mode="shared", labels={"purpose": NETWORK})) - with ( - patch( - "vcore_scripts.protocol_containers.command", - return_value=json.dumps([network]), - ) as command, - self.assertRaisesRegex(RuntimeError, "host-only"), - ): - ContainerLab({}) - command.assert_called_once_with("network", "inspect", NETWORK) - - def test_ownership_mismatch_never_deletes(self): - lab = SimpleNamespace(run_id="fixture") - with tempfile.TemporaryDirectory() as root: - peer = ContainerPeer(lab, Path(root), "server") - unrelated = dict( - id=peer.name, configuration=dict(labels={"purpose": "other"}) - ) - with ( - patch( - "vcore_scripts.protocol_containers.listing", - return_value=[unrelated], - ), - patch("vcore_scripts.protocol_containers.command") as command, - self.assertRaisesRegex(RuntimeError, "ownership"), - ): - peer.stop() - command.assert_not_called() - - def test_inventory_failure_still_closes_log_capture(self): - for error in [ - RuntimeError("isolated container operation failed: list"), - TimeoutError("inventory query timed out"), - json.JSONDecodeError("invalid inventory", "not-json", 0), - KeyboardInterrupt(), - ]: - with self.subTest(error=type(error).__name__): - peer = ContainerPeer( - SimpleNamespace(run_id="fixture"), Path("fixture"), "server" - ) - peer.capture = MagicMock(record={"joined": True}) - with ( - patch( - "vcore_scripts.protocol_containers.listing", side_effect=error - ) as listing, - patch("vcore_scripts.protocol_containers.command") as command, - self.assertRaises(type(error)) as raised, - ): - peer.stop() - self.assertIs(raised.exception, error) - peer.capture.__exit__.assert_called_once_with(None, None, None) - self.assertTrue(peer.record["log_cleanup"]) - self.assertFalse(peer.record["joined"]) - listing.assert_called_once_with() - command.assert_not_called() - - def test_cleanup_only_owned_vm_and_verifies_absence(self): - lab = SimpleNamespace(run_id="fixture") - with tempfile.TemporaryDirectory() as root: - peer = ContainerPeer(lab, Path(root), "server") - owned = dict( - id=peer.name, - configuration=dict(labels={"purpose": NETWORK, "vcore-run": "fixture"}), - status=dict(state="running"), - ) - other = dict(id="another-task") - with ( - patch( - "vcore_scripts.protocol_containers.listing", - side_effect=[[owned, other], [other]], - ), - patch("vcore_scripts.protocol_containers.command") as command, - ): - peer.stop() - self.assertEqual( - [call.args for call in command.call_args_list], - [ - ("stop", "--time", "5", peer.name), - ("delete", "--force", peer.name), - ], - ) - self.assertTrue(peer.record["joined"]) - - def test_failed_launch_still_registers_cleanup(self): - lab = ContainerLab.__new__(ContainerLab) - lab.run_id, lab.record = "fixture", {"peers": []} - with ( - patch.object( - ContainerPeer, "start", side_effect=RuntimeError("partial launch") - ), - patch.object(ContainerPeer, "stop") as stop, - ): - with ( - self.assertRaisesRegex(RuntimeError, "partial launch"), - contextlib.ExitStack() as stack, - ): - lab.start(stack, Path("fixture"), "server", []) - stop.assert_called_once() - - def test_no_native_origin_entry_or_archived_host_probe(self): - with ( - patch.dict("os.environ", {}, clear=True), - self.assertRaisesRegex(SystemExit, "container harness"), - ): - origin_main() - for option in ["--collision-probe", "--socket-probe"]: - with ( - patch("sys.argv", ["diagnostic", "unused", option]), - contextlib.redirect_stderr(io.StringIO()), - ): - with self.assertRaises(SystemExit) as error: - main() - self.assertEqual(error.exception.code, 2) - - def test_client_server_and_origins_use_distinct_container_addresses(self): - config, selected = client_config( - options(["raw", "xudp", "packetaddr"]), - "ws", - True, - "192.0.2.2", - "192.0.2.3", - "192.0.2.4", - "fixture-pin", - ) - self.assertEqual(len(selected), 39) - self.assertEqual({item["socks_host"] for item in selected}, {"192.0.2.4"}) - self.assertEqual( - {item["listen"] for item in config["listeners"]}, {"192.0.2.4"} - ) - self.assertEqual({item["server"] for item in config["proxies"]}, {"192.0.2.2"}) - self.assertEqual(config["hosts"]["vcore-fixture.test"], "192.0.2.3") - self.assertEqual(config["rules"], ["MATCH,REJECT"]) - self.assertNotIn("packet-encoding", config["proxies"][0]) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_evidence.py b/scripts/tests/test_protocol_evidence.py deleted file mode 100644 index 4291ff9..0000000 --- a/scripts/tests/test_protocol_evidence.py +++ /dev/null @@ -1,126 +0,0 @@ -from __future__ import annotations - -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import ( - load_manifest, - rust_results, - select_cases, - validate_results, -) - - -class ProtocolEvidenceTest(unittest.TestCase): - def setUp(self): - self.cases = select_cases(load_manifest(), stage="FOUNDATIONS") - self.results = [ - { - "case_id": case["case_id"], - "status": "PASS", - "scope": "foundation-only", - "assertions": dict.fromkeys(case["expected_observation"], True), - "evidence": list(case["required_evidence"]), - "cleanup": True, - "command_exit_code": 0, - "row_ids": case["row_ids"], - "peer_kind": case["peer_kind"], - } - for case in self.cases - ] - - def test_complete_results_are_accepted_as_foundation_not_protocol_field_signoff( - self, - ): - validate_results(self.cases, self.results) - - def test_missing_duplicate_unknown_and_empty_required_cases_fail(self): - for results in [ - self.results[:-1], - self.results + [self.results[0]], - self.results + [{"case_id": "unknown"}], - ]: - with self.subTest(results=len(results)), self.assertRaises(ValueError): - validate_results(self.cases, results) - with self.assertRaises(ValueError): - validate_results([], []) - with self.assertRaises(ValueError): - select_cases(load_manifest(), stage="WIREGUARD") - - def test_failure_blocked_not_run_timeout_and_cleanup_never_become_pass(self): - for update in [ - {"status": status} for status in ["FAIL", "BLOCKED", "NOT RUN"] - ] + [{"cleanup": False}, {"command_exit_code": 124}]: - results = copy.deepcopy(self.results) - results[0].update(update) - with self.subTest(update=update), self.assertRaises(ValueError): - validate_results(self.cases, results) - - def test_cfg_only_incomplete_or_false_assertions_are_not_behavior_evidence(self): - for update in [ - {"evidence": ["CFG"]}, - {"assertions": {}}, - {"assertions": {key: False for key in self.results[0]["assertions"]}}, - {"row_ids": ["T08"]}, - ]: - results = copy.deepcopy(self.results) - results[0].update(update) - with self.subTest(update=update), self.assertRaises(ValueError): - validate_results(self.cases, results) - - def test_manifest_rejects_duplicate_ids_and_unknown_rows(self): - manifest = load_manifest() - for mutation in ["duplicate", "unknown-row"]: - data = { - "schema_version": 1, - "kind": "executable-case-manifest", - "cases": copy.deepcopy(manifest), - } - if mutation == "duplicate": - data["cases"].append(data["cases"][0]) - else: - data["cases"][0]["row_ids"] = ["not-a-field"] - with tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "cases.json" - path.write_text(json.dumps(data)) - with self.assertRaises(ValueError): - load_manifest(path) - - def test_resource_claims_require_idle_structured_snapshots(self): - case = next( - case for case in self.cases if case["case_id"] == "FOUNDATIONS-RESOURCES" - ) - events = [ - { - "schema_version": 1, - "suite": case["case_id"], - "assertion": name, - "status": status, - } - for name in case["expected_observation"] - for status in ["BEGIN", "PASS"] - ] - self.assertEqual(rust_results(case, events, 0)["status"], "FAIL") - - def test_malformed_or_unknown_assertions_never_become_pass(self): - case = self.cases[0] - events = [ - { - "schema_version": 1, - "suite": case["case_id"], - "assertion": name, - "status": status, - } - for name in case["expected_observation"] - for status in ["BEGIN", "PASS"] - ] - self.assertEqual(rust_results(case, events, 0)["status"], "PASS") - events[0]["schema_version"] = 99 - self.assertEqual(rust_results(case, events, 0)["status"], "FAIL") - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_harness.py b/scripts/tests/test_protocol_harness.py deleted file mode 100644 index 437459b..0000000 --- a/scripts/tests/test_protocol_harness.py +++ /dev/null @@ -1,173 +0,0 @@ -from __future__ import annotations - -import contextlib -import io -import json -import tempfile -import unittest -from pathlib import Path -from unittest.mock import patch - -from vcore_scripts import cli, protocol_harness, protocol_preflight -from vcore_scripts.native_release import PeerArtifact -from vcore_scripts.protocol_inputs import redact - - -class ProtocolHarnessTest(unittest.TestCase): - def test_cli_freezes_list_case_protocol_and_preflight_selection(self): - with patch("vcore_scripts.cli.run_protocol_interop") as run: - self.assertEqual( - cli.main( - [ - "check", - "protocol-interop", - "--suite", - "foundations", - "--case", - "FOUNDATIONS-QUIC", - "--protocol", - "foundation", - "--list", - ] - ), - 0, - ) - self.assertEqual(run.call_args.kwargs["identifiers"], ["FOUNDATIONS-QUIC"]) - self.assertTrue(run.call_args.kwargs["list_only"]) - with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit): - cli.main( - [ - "check", - "protocol-interop", - "--suite", - "foundations", - "--list", - "--preflight", - ] - ) - - def test_coverage_requires_explicit_catalog_or_result_mode(self): - with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit): - cli.main(["check", "protocol-coverage"]) - with patch("vcore_scripts.cli.check_run") as check: - self.assertEqual( - cli.main( - [ - "check", - "protocol-coverage", - "--suite", - "foundations", - "--run-dir", - "target/interop/runs/fixture", - ] - ), - 0, - ) - self.assertEqual(check.call_args.args[1], "FOUNDATIONS") - self.assertEqual( - check.call_args.args[0], - Path("target/interop/runs/fixture").resolve(), - ) - - def test_redaction_removes_paths_uuid_authorization_and_private_key(self): - output = redact( - "/Users/example/private/input password=secret " - "Authorization: Bearer secret-token\n" - "12345678-1234-1234-1234-123456789abc " - "-----BEGIN PRIVATE KEY-----secret-material-----END PRIVATE KEY-----" - ) - for secret in [ - "secret", - "Bearer-token", - "secret-token", - "12345678", - "/Users/example", - "secret-material", - ]: - self.assertNotIn(secret, output) - - def test_peer_preflight_failure_does_not_prevent_other_peer_capabilities(self): - with tempfile.TemporaryDirectory() as temporary: - - def download(kind, directory): - if kind == "H": - raise RuntimeError("private-marker") - return PeerArtifact( - Path(temporary) / kind, {"version": "fixture", "kind": kind} - ) - - with patch.object( - protocol_preflight, "download_native", side_effect=download - ): - artifacts, records = protocol_preflight.preflight( - Path(temporary), {"H", "XR", "V2"} - ) - self.assertEqual(set(artifacts), {"XR", "V2"}) - self.assertEqual( - {record["kind"]: record["status"] for record in records}, - {"H": "BLOCKED", "V2": "READY", "XR": "READY"}, - ) - self.assertNotIn("private-marker", json.dumps(records)) - - def test_sigint_still_writes_incomplete_report_without_promoting_not_run(self): - identity = { - "source_tree_sha256": "0" * 64, - "lock_sha256": "1" * 64, - "dirty_patch_sha256": "2" * 64, - "parent_commit": "3" * 40, - } - with ( - tempfile.TemporaryDirectory() as temporary, - contextlib.ExitStack() as stack, - ): - root = Path(temporary) - stack.enter_context(patch.object(protocol_harness, "CORE_DIR", root)) - stack.enter_context( - patch.object( - protocol_harness, - "run_identity", - return_value=identity - | { - "stage": "VMESS", - "mode": "execute", - "suite_timeout_seconds": 30, - "commands": [], - }, - ) - ) - stack.enter_context( - patch.object(protocol_harness, "source_identity", return_value=identity) - ) - stack.enter_context( - patch.object( - protocol_harness, - "exclusive_run", - side_effect=contextlib.nullcontext, - ) - ) - stack.enter_context( - patch.object(protocol_harness, "check_protocol_catalogs") - ) - stack.enter_context( - patch( - "vcore_scripts.protocol_vmess_acceptance.execute", - side_effect=KeyboardInterrupt, - ) - ) - output = root / "target/interop/runs/fixture" - with ( - contextlib.redirect_stdout(io.StringIO()), - self.assertRaises(RuntimeError), - ): - protocol_harness.run_protocol_interop( - stage="VMESS", identifiers=["VMESS-CFG"], run_dir=output - ) - self.assertEqual( - json.loads((output / "cases.json").read_text())[0]["status"], "NOT RUN" - ) - self.assertFalse(json.loads((output / "run.json").read_text())["cleanup"]) - self.assertTrue((output / "resources.jsonl").is_file()) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_httpupgrade.py b/scripts/tests/test_protocol_httpupgrade.py deleted file mode 100644 index b30f5f3..0000000 --- a/scripts/tests/test_protocol_httpupgrade.py +++ /dev/null @@ -1,105 +0,0 @@ -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import load_manifest, validate_results -from vcore_scripts.protocol_httpupgrade import expected_observation, selection -from vcore_scripts.protocol_httpupgrade_acceptance import wire_pass -from vcore_scripts.protocol_httpupgrade_catalog import WIRE, definitions, wire_cases -from vcore_scripts.protocol_security_acceptance import result, rust_command - - -class HttpUpgradeAcceptanceTest(unittest.TestCase): - def test_required_groups_are_registered_and_cannot_be_omitted(self): - cases = definitions() - self.assertEqual(len(cases), 13) - self.assertEqual( - cases, [c for c in load_manifest() if c["stage"] == "HTTPUPGRADE"] - ) - rows = [result(c, True, True) for c in cases] - validate_results(cases, rows) - with self.assertRaises(ValueError): - validate_results(cases, rows[:-1]) - rows[0]["assertions"] = {} - with self.assertRaises(ValueError): - validate_results(cases, rows) - - def test_runner_matches_independent_six_mode_wire_matrix(self): - counts = [6, 14, 2, 12, 16, 16, 6] - for name, count in zip(WIRE, counts, strict=True): - self.assertEqual(len(wire_cases(name)), count) - self.assertEqual( - [(r[0], r[3]) for r in selection(name.lower())], wire_cases(name) - ) - - def test_raw_evidence_rejects_missing_cases_forged_bytes_and_wrong_consumer(self): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary) - for name in WIRE: - rows = [] - for identifier, consumer in wire_cases(name): - node = dict( - type="vmess" if identifier.startswith("vmess") else "trojan" - ) - node["packet-encoding"] = identifier.rsplit("-", 1)[-1] - if consumer == "close": - reference = dict(tail_hex="", terminated=True, variant="plain") - for suffix in ("-close-reference.json", "-close-command.log"): - (root / (identifier + suffix)).write_text( - json.dumps(reference) - ) - observed = dict(tail_hex="", terminated=True, stop_idle=True) - else: - observed = expected_observation( - consumer, node, "XR" if name == "UDP-DOMAIN" else "M" - ) - (root / (identifier + "-observations.json")).write_text( - json.dumps(observed) - ) - events = [] - for suite, assertion in [("HTTPUPGRADE-PUBLIC", consumer)] + ( - [("HTTPUPGRADE-BASE", "tcp_10mib_both_directions")] * 3 - if consumer == "tcp" - else [] - ): - events += [ - dict( - schema_version=1, - suite=suite, - assertion=assertion, - status=s, - ) - for s in ("BEGIN", "PASS") - ] - (root / (identifier + "-events.jsonl")).write_text( - "".join(json.dumps(e) + "\n" for e in events) - ) - rows.append( - dict( - case_id=identifier, - command=rust_command( - "vless_public", "httpupgrade::" + consumer - ), - exit_code=0, - command_cleanup=True, - observations=observed, - ) - ) - report = dict(cases=rows) - self.assertTrue(wire_pass(name, root, report)) - self.assertFalse(wire_pass(name, root, dict(cases=rows[:-1]))) - for key, value in ( - ("exit_code", True), - ("command_cleanup", False), - ("observations", {}), - ("command", []), - ): - broken = copy.deepcopy(report) - broken["cases"][0][key] = value - self.assertFalse(wire_pass(name, root, broken)) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_hysteria2.py b/scripts/tests/test_protocol_hysteria2.py deleted file mode 100644 index 0f55c04..0000000 --- a/scripts/tests/test_protocol_hysteria2.py +++ /dev/null @@ -1,157 +0,0 @@ -"""Offline checks of HYSTERIA2's evidence boundary; no sockets or protocol peers.""" - -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_hysteria2 import test_command -from vcore_scripts.protocol_hysteria2_acceptance import ( - bandwidth_pass, - hopping_pass, - native_result, -) -from vcore_scripts.protocol_hysteria2_catalog import ( - FIELDS, - H, - definitions, - events_pass, -) - - -def pair(suite, assertion): - return [ - dict(schema_version=1, suite=suite, assertion=assertion, status=s) - for s in ("BEGIN", "PASS") - ] - - -def tcp_events(): - outer = pair("HYSTERIA2-BASE", "tcp_ipv4_ipv6_domain_and_measure") - return ( - outer[:1] + pair("HYSTERIA2-BASE", "tcp_10mib_both_directions") * 3 + outer[1:] - ) - - -class Hysteria2AcceptanceTest(unittest.TestCase): - def test_catalog_covers_all_fields_and_eight_hop_combinations(self): - cases = definitions() - self.assertEqual(len(cases), 36) - self.assertEqual(len(cases), len({c["case_id"] for c in cases})) - self.assertEqual(len(FIELDS), 20) - self.assertEqual(FIELDS, set().union(*(set(c["row_ids"]) for c in cases))) - self.assertEqual( - { - (v["ipv6"], v["obfs"], v["random_interval"]) - for v in H.values() - if v["test"] == "native_hopping" - }, - { - (a, b, c) - for a in (False, True) - for b in (False, True) - for c in (False, True) - }, - ) - - def test_partial_unknown_duplicate_and_wrong_suite_events_fail(self): - # The public caller records one outer case plus three nested bulk - # transfers. Mirror that real trace, not only its outer success event. - observed = tcp_events() - self.assertTrue(events_pass(observed, "hysteria2_tcp_base")) - wrong = copy.deepcopy(observed) - wrong[1]["suite"] = "XHTTP-BASE" - for bad in ( - [], - observed[:1], - observed * 2, - wrong, - observed + pair("HYSTERIA2-BASE", "unknown"), - pair("HYSTERIA2-BASE", "tcp_ipv4_ipv6_domain_and_measure"), - observed[:1] + observed[3:], - observed + pair("HYSTERIA2-BASE", "tcp_10mib_both_directions"), - ): - self.assertFalse(events_pass(bad, "hysteria2_tcp_base")) - - def test_native_identity_cleanup_or_missing_results_fail(self): - case = next(c for c in definitions() if c["case_id"] == "HYSTERIA2-TCP") - test = "hysteria2_tcp_base" - report = dict( - status="PASS", - cleanup=True, - source_unchanged=True, - obfs=False, - cases=[ - dict( - case_id=test, - status="PASS", - exit_code=0, - command_cleanup=True, - command=test_command(test), - ) - ], - isolation=dict( - network_mode="hostOnly", - host_servers=False, - guest_mtu=1500, - image_digest="sha256:" + "a" * 64, - peers=[dict(started=True, joined=True)], - ), - peers={ - "M": dict( - version="test", - binary_sha256="b" * 64, - source_url="https://github.com/MetaCubeX/mihomo/" - "releases/download/test/fixture", - ) - }, - ) - with tempfile.TemporaryDirectory() as temporary: - directory = Path(temporary) - path = directory / f"{test}-events.jsonl" - path.write_text("".join(json.dumps(e) + "\n" for e in tcp_events())) - self.assertEqual(native_result(case, report, directory)["status"], "PASS") - for key, bad in ( - ("peers", {}), - ("cleanup", False), - ("cases", []), - ("source_unchanged", False), - ): - altered = copy.deepcopy(report) - altered[key] = bad - self.assertEqual( - native_result(case, altered, directory)["status"], "FAIL" - ) - path.unlink() - self.assertEqual(native_result(case, report, directory)["status"], "FAIL") - - def test_bandwidth_and_hop_counters_cannot_be_forged_as_empty_success(self): - self.assertFalse(bandwidth_pass([])) - self.assertFalse(bandwidth_pass([{}] * 10)) - self.assertFalse(hopping_pass({})) - hop = dict( - seconds=60, - protected_sockets=9, - tcp_bytes_per_direction=10485760, - tcp_sha256="c" * 64, - udp_packets=1500, - udp_target_kinds=3, - udp_maximums=[4071, 4052, 4060], - socket_peak=2, - ) - self.assertTrue(hopping_pass(hop)) - for key, value in ( - ("seconds", 59), - ("protected_sockets", 8), - ("udp_packets", 0), - ("tcp_sha256", ""), - ("socket_peak", 3), - ("udp_maximums", []), - ): - altered = dict(hop, **{key: value}) - self.assertFalse(hopping_pass(altered)) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_integration.py b/scripts/tests/test_protocol_integration.py deleted file mode 100644 index 64d3e19..0000000 --- a/scripts/tests/test_protocol_integration.py +++ /dev/null @@ -1,420 +0,0 @@ -from __future__ import annotations - -import copy -import io -import json -import tempfile -import unittest -from contextlib import redirect_stdout -from pathlib import Path - -from vcore_scripts import cli - - -class IntegrationAcceptanceTest(unittest.TestCase): - def test_integration_quality_separates_release_source_policy(self): - from vcore_scripts.protocol_integration_local import commands - - rows = commands("INTEGRATION-QUALITY") - self.assertNotIn(["vcore-scripts", "check", "tls-dependencies"], rows) - self.assertIn(["vcore-scripts", "check", "c-header"], rows) - self.assertIn( - [ - "cargo", - "test", - "--locked", - "--manifest-path", - "crates/vcore-netstack/Cargo.toml", - "--all-targets", - ], - rows, - ) - self.assertIn(["vcore-scripts", "build", "apple"], rows) - self.assertIn(["vcore-scripts", "build", "android"], rows) - - def test_native_source_identity_and_cleanup_are_required(self): - from vcore_scripts.protocol_integration_checks import envelope - - identity = dict( - version="official-test", - binary_sha256="a" * 64, - archive_sha256="b" * 64, - source_url="https://github.com/MetaCubeX/mihomo/releases/latest/download/test", - ) - source = { - k: "c" * 64 - for k in ( - "parent_commit", - "source_tree_sha256", - "dirty_patch_sha256", - "lock_sha256", - ) - } - digest = "sha256:" + "d" * 64 - report = dict( - status="PASS", - cleanup=True, - source_unchanged=True, - source=source, - peers={"M": identity}, - isolation=dict( - network_mode="hostOnly", - host_servers=False, - guest_mtu=1500, - image_digest=digest, - peers=[dict(started=True, joined=True)], - ), - ) - self.assertTrue(envelope(report, {"M": identity}, source, digest)) - for mutate in ( - lambda d: d.update(cleanup=False), - lambda d: d.update(source_unchanged=False), - lambda d: d["source"].update(source_tree_sha256="e" * 64), - lambda d: d["peers"]["M"].update(binary_sha256="e" * 64), - lambda d: d["isolation"].update(host_servers=True), - lambda d: d["isolation"].update(guest_mtu=1200), - lambda d: d["isolation"]["peers"][0].update(joined=False), - lambda d: d["isolation"].update(peers=[]), - ): - broken = copy.deepcopy(report) - mutate(broken) - self.assertFalse(envelope(broken, {"M": identity}, source, digest)) - - def test_pressure_evidence_is_recomputed_not_inferred(self): - from vcore_scripts.protocol_evidence import RESOURCE_KINDS - from vcore_scripts.protocol_integration_metrics import ( - QUEUES, - lifetimes, - rebuild, - soak, - ) - from vcore_scripts.protocol_integration_profiles import ( - TCP_GROUPS, - UDP_GROUPS, - expected_flows, - ) - - def snapshot(current): - return dict( - counts=[ - dict(kind=k, current=current, peak=1) - for k in sorted(RESOURCE_KINDS) - ] - ) - - def sample(active=True): - return dict( - heap_in_use=2_000_000, - rss_kib=50_000, - fd=100 if active else 6, - resources=snapshot(int(active)), - queues=[ - dict(kind=k, capacity=v, peak=int(v > 0)) for k, v in QUEUES.items() - ], - ) - - event = dict(resources=snapshot(0)) - stop = dict( - stop_ms=4, - after_stop=sample(False), - quiet_seconds=5.1, - quiet=sample(False), - ports_rebound=True, - ) - rows = [ - dict( - generation=i, - tcp=20, - udp=20, - per_group_tcp=5, - per_group_udp=5, - flow_profiles=expected_flows(i), - active=sample(), - setup_us=[1000] * 40, - fault="owned-peer-connections-closed", - new_clients=True, - after_clients=sample(), - ) - for i in range(100) - ] - value = dict( - count=100, - same_running_session=True, - tcp_groups=list(TCP_GROUPS), - udp_groups=list(UDP_GROUPS), - cold_fd=4, - baseline_fd=6, - cycles=rows, - stopped=stop, - ) - self.assertTrue(rebuild(value, event)) - for mutate in ( - lambda d: d["cycles"].pop(), - lambda d: d.update(same_running_session=False), - lambda d: d["cycles"][0].update(tcp=0), - lambda d: d["cycles"][0].update(setup_us=[]), - lambda d: d["cycles"][0]["flow_profiles"]["tcp"].pop(), - lambda d: d["cycles"][1].update(flow_profiles=expected_flows(0)), - lambda d: d["stopped"].update(stop_ms=5001), - lambda d: d["stopped"].update(quiet_seconds=0), - lambda d: d["stopped"]["after_stop"].update(fd=7), - ): - broken = copy.deepcopy(value) - mutate(broken) - self.assertFalse(rebuild(broken, event)) - self.assertFalse(lifetimes(dict(count=99, cycles=[]), [])) - points = [ - dict(sample(), seconds=300 + i * 60, waves=i + 1, faults=5 + i) - for i in range(25) - ] - value = dict( - requested_seconds=1800, - seconds=1800.2, - tcp_groups=list(TCP_GROUPS), - udp_groups=list(UDP_GROUPS), - tcp=20, - udp=20, - per_group_tcp=5, - per_group_udp=5, - first_profiles=expected_flows(0), - last_profiles=expected_flows(29), - waves=1000, - normal_verified_bytes=1000 * 20 * 2 * 2 * 257, - normal_corruption=0, - normal_misdirection=0, - normal_unexpected_loss=0, - switches=list(range(1, 1800)), - faults=[ - dict( - start=60 * i, - end=60 * i + 0.5, - old_tcp_closed=20, - new_tcp=20, - new_udp=20, - replay=False, - ) - for i in range(1, 30) - ], - samples=points, - cold_fd=4, - baseline_fd=6, - stopped=stop, - active_end=sample(), - first_setup_us=[1000] * 40, - last_setup_us=[1100] * 40, - first_setup_median_us=1000, - last_setup_median_us=1100, - heap=dict( - early_median=2_000_000, late_median=2_000_000, allowed_growth=1048576 - ), - ) - self.assertTrue(soak(value, event, points)) - for mutate in ( - lambda d: d.update(requested_seconds=65), - lambda d: d.update(seconds=1799), - lambda d: d["faults"].pop(), - lambda d: d.update(switches=[]), - lambda d: d["samples"][0].update(heap_in_use=0), - lambda d: d["samples"][-1]["queues"][0].update(peak=10000), - lambda d: d["heap"].update(late_median=4_000_000), - lambda d: d.update(last_setup_us=[3000] * 40, last_setup_median_us=3000), - lambda d: d.update(normal_unexpected_loss=1), - ): - broken = copy.deepcopy(value) - mutate(broken) - self.assertFalse(soak(broken, event, broken["samples"])) - self.assertFalse(soak(value, event, points[:-1])) - - def test_integration_consumer_results_keep_the_manifest_scope(self): - from vcore_scripts.protocol_evidence import load_manifest, validate_results - from vcore_scripts.protocol_security_acceptance import result - - case = next( - c for c in load_manifest() if c["case_id"] == "INTEGRATION-PAIR-SS-SS" - ) - validate_results([case], [result(case, True, True)]) - - def test_pair_evidence_requires_each_real_path_and_original_assertions(self): - from vcore_scripts.protocol_integration_acceptance import pair_pass - from vcore_scripts.protocol_integration_native import rust_command - - identifier = "INTEGRATION-PAIR-SOCKS5-VLESS" - record = dict( - case_id=identifier, - command=rust_command(), - exit_code=0, - command_cleanup=True, - ) - paths = [ - dict( - outer_ipv6=v6, - nested_select=group, - tcp_target_kinds=3, - tcp_bytes_each_direction=31457280, - udp_target_kinds=3, - udp_packets=1200, - udp_sizes=[1, 64, 512, 1200], - server_first=True, - client_first=True, - ports_rebound=True, - ) - for v6 in (False, True) - for group in (False, True) - ] - observation = dict( - first="socks5", - last="vless", - paths=paths, - domain_native=[], - budgets=[ - dict( - transmit=tx, - receive=rx, - roundtrip_size=min(tx, rx), - packets=100, - oversize_rejected_before_origin=True, - ) - for tx, rx in ((128, 128), (512, 256)) - ], - routed_udp=dict( - upstream_business_udp_disabled=True, - carrier_tcp=True, - carrier_udp=True, - leaf_business_udp_rejected=True, - ), - carrier_capability=dict( - upstream_datagrams="rejected", - tcp_allowed=True, - udp_allowed=True, - no_bypass=True, - ), - ) - events = [ - dict(schema_version=1, suite=suite, assertion=assertion, status=status) - for suite, assertion, count in ( - ("INTEGRATION-PAIR", "ordered_pair", 1), - ("INTEGRATION-BASE", "tcp_10mib_both_directions", 12), - ("INTEGRATION-PAIR", "directional_budget", 1), - ("INTEGRATION-PAIR", "routed_udp_permission", 1), - ("INTEGRATION-PAIR", "carrier_capability", 1), - ) - for _ in range(count) - for status in ("BEGIN", "PASS") - ] - with tempfile.TemporaryDirectory() as temp: - root = Path(temp) - event_path = root / (identifier + "-events.jsonl") - data_path = root / (identifier + "-observations.json") - event_path.write_text("".join(json.dumps(e) + "\n" for e in events)) - data_path.write_text(json.dumps(observation)) - self.assertTrue(pair_pass(identifier, record, root)) - for mutate in ( - lambda d: d["paths"].pop(), - lambda d: d["paths"].append(d["paths"][0]), - lambda d: d["paths"][0].update(udp_packets=799), - lambda d: d["paths"][0].update(ports_rebound=False), - lambda d: d["paths"][0].update(server_first=False), - lambda d: d["paths"][0].update( - server_first_limitation="official-ss2022-empty-first-write" - ), - lambda d: d.update(last="vmess"), - lambda d: d["budgets"][0].update(packets=99), - lambda d: d["carrier_capability"].update(no_bypass=False), - ): - broken = copy.deepcopy(observation) - mutate(broken) - data_path.write_text(json.dumps(broken)) - self.assertFalse(pair_pass(identifier, record, root)) - data_path.write_text(json.dumps(observation)) - event_path.write_text("".join(json.dumps(e) + "\n" for e in events[:-1])) - self.assertFalse(pair_pass(identifier, record, root)) - identifier = "INTEGRATION-CHAIN-TUIC-QUIC-SS-V3-AES-256-GCM" - record["case_id"] = identifier - observation.update( - first="tuic", - last="ss", - variant=dict(udp_mode="quic", cipher="2022-blake3-aes-256-gcm"), - ) - observation["carrier_capability"]["udp_allowed"] = False - for path in observation["paths"]: - path.update( - server_first=False, - server_first_limitation="official-ss2022-empty-first-write", - ) - event_path = root / (identifier + "-events.jsonl") - data_path = root / (identifier + "-observations.json") - event_path.write_text("".join(json.dumps(e) + "\n" for e in events)) - data_path.write_text(json.dumps(observation)) - self.assertTrue(pair_pass(identifier, record, root)) - for mutate in ( - lambda d: d.pop("variant"), - lambda d: d["variant"].update(udp_mode="native"), - lambda d: d["variant"].update(cipher="2022-blake3-aes-128-gcm"), - lambda d: d["paths"][0].update(server_first=True), - lambda d: d["paths"][0].pop("server_first_limitation"), - lambda d: d["paths"][0].update(client_first=False), - lambda d: d["paths"][0].update(tcp_bytes_each_direction=1), - ): - broken = copy.deepcopy(observation) - mutate(broken) - data_path.write_text(json.dumps(broken)) - self.assertFalse(pair_pass(identifier, record, root)) - - def test_list_contains_all_ordered_pairs_and_remaining_gates(self): - output = io.StringIO() - with redirect_stdout(output): - self.assertEqual( - cli.main( - ["check", "protocol-interop", "--suite", "integration", "--list"] - ), - 0, - ) - identifiers = [line.split("\t")[0] for line in output.getvalue().splitlines()] - protocols = { - "SOCKS5", - "ANYTLS", - "SS", - "TROJAN", - "VMESS", - "VLESS", - "HYSTERIA2", - "TUIC", - } - expected = { - f"INTEGRATION-PAIR-{first}-{last}" - for first in protocols - for last in protocols - } - self.assertEqual(len(expected), 64) - self.assertEqual({key for key in identifiers if "-PAIR-" in key}, expected) - self.assertEqual( - {key for key in identifiers if "-CHAIN-" in key}, - { - f"INTEGRATION-CHAIN-TUIC-{mode}-SS-V3-{cipher}" - for mode in ("NATIVE", "QUIC") - for cipher in ("AES-128-GCM", "AES-256-GCM", "CHACHA20-POLY1305") - }, - ) - self.assertEqual(len(identifiers), len(set(identifiers))) - self.assertTrue( - { - "INTEGRATION-ENTRYPOINTS", - "INTEGRATION-GRAPH", - "INTEGRATION-DNS-MEASURE", - "INTEGRATION-SS-ALGORITHMS", - "INTEGRATION-SS-EIH", - "INTEGRATION-LIFECYCLE", - "INTEGRATION-REBUILD", - "INTEGRATION-FAILURES", - "INTEGRATION-SOAK", - "INTEGRATION-HY2-HOP", - "INTEGRATION-DEBUG", - "INTEGRATION-RELEASE", - "INTEGRATION-QUALITY", - "INTEGRATION-FEATURES", - "INTEGRATION-SCRIPTS", - "INTEGRATION-SHARED", - "INTEGRATION-COUPLED", - } - <= set(identifiers) - ) diff --git a/scripts/tests/test_protocol_peers.py b/scripts/tests/test_protocol_peers.py deleted file mode 100644 index 597531b..0000000 --- a/scripts/tests/test_protocol_peers.py +++ /dev/null @@ -1,149 +0,0 @@ -from __future__ import annotations - -import os -import select -import signal -import subprocess -import sys -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_peers import OwnedProcess, run_command - - -class OwnedPeerTest(unittest.TestCase): - def test_success_is_bounded_and_records_exit_and_cleanup(self): - result = run_command([sys.executable, "-c", "print('fixture')"], timeout=2) - self.assertEqual(result.stdout, b"fixture\n") - self.assertEqual(result.returncode, 0) - self.assertTrue(result.cleanup) - - def test_timeout_and_output_overflow_fail_and_join(self): - for source, limit in [ - ("import time; time.sleep(60)", 128), - ("print('x'*10000)", 64), - ]: - result = run_command( - [sys.executable, "-c", source], timeout=0.2, limit=limit - ) - self.assertNotEqual(result.returncode, 0) - self.assertTrue(result.cleanup) - self.assertLessEqual(len(result.stdout), limit) - - def test_partial_start_and_permission_failure_do_not_leave_a_process(self): - from unittest.mock import patch - - for error in [FileNotFoundError(), PermissionError()]: - with ( - tempfile.TemporaryDirectory() as directory, - patch("subprocess.Popen", side_effect=error), - ): - record = {} - with ( - self.assertRaises(type(error)), - OwnedProcess(["fixture"], Path(directory) / "private.log", record), - ): - pass - self.assertTrue(record["joined"]) - self.assertFalse(record["started"]) - - def test_peer_exit_is_not_readiness(self): - with tempfile.TemporaryDirectory() as directory: - record = {} - with OwnedProcess( - [sys.executable, "-c", "raise SystemExit(3)"], - Path(directory) / "private.log", - record, - ) as peer: - peer.process.wait(timeout=2) - with self.assertRaises(RuntimeError): - peer.ensure_alive() - self.assertEqual(record["unexpected_exit"], 3) - self.assertTrue(record["joined"]) - - def test_cleanup_failure_is_explicit(self): - from unittest.mock import patch - - with tempfile.TemporaryDirectory() as directory: - record = {} - peer = OwnedProcess( - [sys.executable, "-c", "import time; time.sleep(60)"], - Path(directory) / "private.log", - record, - ) - peer.__enter__() - try: - with ( - patch.object( - peer, - "_terminate", - side_effect=OSError("synthetic-private-marker"), - ), - self.assertRaises(OSError), - ): - peer.__exit__(None, None, None) - self.assertFalse(record["joined"]) - finally: - peer._terminate() - peer._finish_reader() - - def test_cancellation_joins_only_the_owned_peer(self): - with tempfile.TemporaryDirectory() as directory: - record = {} - with ( - self.assertRaises(KeyboardInterrupt), - OwnedProcess( - [sys.executable, "-c", "import time; time.sleep(60)"], - Path(directory) / "peer.log", - record, - ) as peer, - ): - pid = peer.process.pid - raise KeyboardInterrupt - self.assertTrue(record["joined"]) - self.assertIsNotNone(peer.process.poll()) - with self.assertRaises(ProcessLookupError): - os.kill(pid, 0) - - @unittest.skipIf(os.name == "nt", "Unix process-group signal contract") - def test_real_sigint_joins_nested_peer_without_stopping_unrelated_peer(self): - with tempfile.TemporaryDirectory() as directory: - source = ( - "import signal, sys\nfrom pathlib import Path\n" - "from vcore_scripts.protocol_peers import OwnedProcess\n" - "with OwnedProcess([sys.executable, '-c', " - "'import time; time.sleep(60)'], " - "Path(sys.argv[1]), {}) as peer:\n" - " print(peer.process.pid, flush=True)\n signal.pause()\n" - ) - unrelated = {} - with OwnedProcess( - [sys.executable, "-c", "import time; time.sleep(60)"], - Path(directory) / "unrelated.log", - unrelated, - ) as sentinel: - worker = subprocess.Popen( - [sys.executable, "-c", source, str(Path(directory) / "nested.log")], - stdout=subprocess.PIPE, - stderr=subprocess.DEVNULL, - start_new_session=True, - ) - try: - ready, _, _ = select.select([worker.stdout], [], [], 5) - self.assertTrue(ready, "signal fixture did not start") - pid = int(worker.stdout.readline()) - worker.send_signal(signal.SIGINT) - self.assertNotEqual(worker.wait(timeout=10), 0) - with self.assertRaises(ProcessLookupError): - os.kill(pid, 0) - sentinel.ensure_alive() - finally: - if worker.poll() is None: - worker.kill() - worker.wait(timeout=5) - worker.stdout.close() - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_reality_hybrid.py b/scripts/tests/test_protocol_reality_hybrid.py deleted file mode 100644 index ef13861..0000000 --- a/scripts/tests/test_protocol_reality_hybrid.py +++ /dev/null @@ -1,125 +0,0 @@ -import unittest - -from vcore_scripts.cli import _parser -from vcore_scripts.protocol_reality_hybrid import ( - MODES, - cases, - negative_wire_pass, - wire_pass, -) - - -class HybridRealityGateTests(unittest.TestCase): - def test_browser_downgrade_and_retry_require_each_legs_actual_server_hello(self): - events = [] - for port, group, hrr in ((25002, 29, False), (25003, 24, True)): - events.extend( - [ - { - "port": port, - "event": "client-hello", - "shares": [[4588, 1216], [29, 32]], - "groups": [4588, 29, 23, 24], - }, - {"port": port, "event": "server-hello", "group": group, "hrr": hrr}, - ] - ) - self.assertTrue(negative_wire_pass(events, "chrome")) - self.assertTrue(negative_wire_pass(events * 2, "chrome", legs=2)) - self.assertFalse(negative_wire_pass(events, "chrome", legs=2)) - self.assertFalse(negative_wire_pass(events[:-1], "chrome")) - self.assertFalse( - negative_wire_pass(events[:-1] + [dict(events[-1], hrr=False)], "chrome") - ) - self.assertFalse( - negative_wire_pass( - [dict(events[0], shares=[[29, 32]])] + events[1:], "chrome" - ) - ) - - def test_hybrid_only_peer_rejection_needs_positive_no_shared_group_evidence(self): - events = [] - for front, cover in ((25002, 24432), (25003, 24433)): - events.extend( - [ - { - "port": front, - "event": "client-hello", - "shares": [[4588, 1216]], - "groups": [4588], - }, - {"port": cover, "event": "no-shared-group"}, - {"port": front, "event": "server-close", "bytes": 0}, - ] - ) - self.assertTrue(negative_wire_pass(events, "none")) - self.assertFalse(negative_wire_pass(events[:-1], "none")) - self.assertFalse(negative_wire_pass(events, "none", legs=2)) - self.assertFalse(negative_wire_pass(events, "chrome")) - self.assertFalse( - negative_wire_pass( - events + [{"port": 25002, "event": "server-hello", "group": 29}], - "none", - ) - ) - - def test_required_scope_includes_profiles_download_and_lifecycle(self): - required = cases() - self.assertEqual(len(required), 54) - self.assertEqual(len(MODES), 12) - for profile in ("none", "chrome"): - for version in ("h1", "h2"): - for mode in ("stream-up", "packet-up"): - self.assertIn(f"{profile}-{version}-{mode}-base", required) - for variant in ("main-classic", "download-classic"): - self.assertIn(f"{profile}-{version}-{variant}", required) - self.assertIn(f"{profile}-{version}-stream-up-security", required) - for mode in ("tcp", "grpc", "vision", "h2-stream-up"): - for test in ("life", "owned"): - self.assertIn(f"chrome-{mode}-{test}", required) - - def test_observation_requires_both_real_shares_and_actual_negotiation(self): - events = [ - { - "port": 25000, - "event": "client-hello", - "shares": [[4588, 1216], [29, 32]], - }, - {"port": 25000, "event": "server-hello", "group": 4588}, - ] - expected = {25000: (True, "chrome")} - self.assertTrue(wire_pass(events, expected)) - self.assertFalse(wire_pass(events[:1], expected)) - self.assertFalse(wire_pass(events[1:], expected)) - self.assertFalse(wire_pass(events, {**expected, 25001: (True, "chrome")})) - self.assertFalse(wire_pass(events + [dict(events[1], group=29)], expected)) - self.assertFalse( - wire_pass([dict(events[0], shares=[[29, 32]]), events[1]], expected) - ) - - def test_classic_and_hybrid_legs_have_independent_requirements(self): - events = [ - {"port": 25000, "event": "client-hello", "shares": [[4588, 1216]]}, - {"port": 25000, "event": "server-hello", "group": 4588}, - {"port": 25001, "event": "client-hello", "shares": [[29, 32]]}, - {"port": 25001, "event": "server-hello", "group": 29}, - ] - self.assertTrue( - wire_pass(events, {25000: (True, "none"), 25001: (False, "chrome")}) - ) - self.assertFalse( - wire_pass(events, {25000: (True, "none"), 25001: (True, "chrome")}) - ) - - def test_cli_exposes_a_subpackage_not_full_stage_acceptance(self): - args = _parser().parse_args( - [ - "check", - "reality-hybrid", - "--run-dir", - "target/interop/runs/new", - "--case", - "chrome-tcp-base", - ] - ) - self.assertEqual(args.identifiers, ["chrome-tcp-base"]) diff --git a/scripts/tests/test_protocol_retirement.py b/scripts/tests/test_protocol_retirement.py deleted file mode 100644 index f40e653..0000000 --- a/scripts/tests/test_protocol_retirement.py +++ /dev/null @@ -1,55 +0,0 @@ -from __future__ import annotations - -import io -import tempfile -import tomllib -import unittest -from contextlib import redirect_stderr -from pathlib import Path -from unittest.mock import patch - -from vcore_scripts import cli -from vcore_scripts.protocol_catalogs import FIELD_IDS, PEERS, PROTOCOLS, SUITES -from vcore_scripts.protocol_preflight import preflight - - -class ProtocolRetirementTest(unittest.TestCase): - def test_wireguard_is_not_a_feature_protocol_peer_or_field_target(self): - root = Path(__file__).resolve().parents[2] - features = tomllib.loads((root / "Cargo.toml").read_text())["features"] - self.assertNotIn("outbound-wireguard", features) - self.assertNotIn("wireguard", PROTOCOLS) - self.assertNotIn("W", PEERS) - self.assertNotIn("WIREGUARD", SUITES) - self.assertFalse(any(identifier.startswith("WG") for identifier in FIELD_IDS)) - - def test_retired_cli_selections_fail_before_any_harness_work(self): - for arguments in ( - ["protocol-interop", "--suite", "wireguard", "--list"], - ["protocol-coverage", "--suite", "wireguard", "--catalog-only"], - [ - "protocol-interop", - "--suite", - "security", - "--protocol", - "wireguard", - "--list", - ], - ): - with self.subTest(arguments=arguments), redirect_stderr(io.StringIO()): - with self.assertRaises(SystemExit) as error: - cli.main(["check", *arguments]) - self.assertEqual(error.exception.code, 2) - - def test_retired_peer_preflight_cannot_download_or_create_a_container(self): - with tempfile.TemporaryDirectory() as temporary: - directory = Path(temporary) / "not-created" - with ( - patch("vcore_scripts.protocol_preflight.run_command") as command, - patch("vcore_scripts.protocol_preflight.download_native") as download, - ): - with self.assertRaisesRegex(ValueError, "unsupported native peer"): - preflight(directory, {"M", "W"}, container=True) - command.assert_not_called() - download.assert_not_called() - self.assertFalse(directory.exists()) diff --git a/scripts/tests/test_protocol_run_evidence.py b/scripts/tests/test_protocol_run_evidence.py deleted file mode 100644 index 0370ee6..0000000 --- a/scripts/tests/test_protocol_run_evidence.py +++ /dev/null @@ -1,235 +0,0 @@ -"""Offline evidence fixtures; these synthetic passes are never stage reports.""" - -from __future__ import annotations - -import contextlib -import copy -import hashlib -import io -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import RESOURCE_KINDS, check_run, load_manifest -from vcore_scripts.protocol_harness import SCRIPT_OBSERVATIONS - - -class RunEvidenceTest(unittest.TestCase): - def fixture(self): - cases = [case for case in load_manifest() if case["stage"] == "FOUNDATIONS"] - idle = { - "counts": [ - {"kind": name, "current": 0, "peak": 1} - for name in sorted(RESOURCE_KINDS) - ] - } - events = [] - legacy = [] - native = [] - results = [] - for case in cases: - results.append( - { - "case_id": case["case_id"], - "row_ids": case["row_ids"], - "peer_kind": case["peer_kind"], - "scope": "foundation-only", - "status": "PASS", - "cleanup": True, - "command_exit_code": 0, - "assertions": dict.fromkeys(case["expected_observation"], True), - "evidence": case["required_evidence"], - } - ) - if case["runner"] in {"rust", "mihomo-legacy"}: - target = legacy if case["runner"] == "mihomo-legacy" else events - for assertion in case["expected_observation"]: - for status in ["BEGIN", "PASS"]: - target.append( - { - "schema_version": 1, - "suite": case["case_id"], - "assertion": assertion, - "status": status, - "seconds": 5, - "resources": idle - if case["case_id"] == "FOUNDATIONS-RESOURCES" - and status == "PASS" - else None, - "checkpoints": [ - {"phase": phase, "resources": idle} - for phase in ["baseline", "after-stop", "quiet"] - ], - } - ) - if case["runner"] == "native-stream": - native.append( - { - "case_id": case["case_id"], - "status": "PASS", - "cleanup": {"joined": True}, - "command_exit_code": 0, - "rust_event": { - "server_first": True, - "payload_bytes": 65536, - "tail_bytes": 14, - "protect_calls": 1, - "driver_joined": True, - "resources_idle": True, - "resources": idle, - }, - "origin": { - "finished": True, - "accepted": 1, - "received": 65536, - "failed": False, - }, - } - ) - peers = [ - { - "kind": kind, - "status": "READY", - "version": "offline-fixture", - "source_url": "https://github.com/fixture/project/releases/latest/download/fixture", - "archive_sha256": "0" * 64, - "binary_sha256": "1" * 64, - "release": "fixture", - "container_status": "READY", - "container_artifact": {"binary_sha256": "2" * 64, "release": "fixture"}, - } - for kind in ["M", "V2"] - ] - commands = [ - "rust-foundations", - "native-probe-build", - "legacy-mihomo", - "offline-scripts", - "feature-default", - "feature-minimal", - "feature-outbound-trojan", - "feature-outbound-vmess", - "feature-outbound-hysteria2", - "feature-stream-transport", - "feature-quic-transport", - ] - return { - "run.json": { - "stage": "FOUNDATIONS", - "mode": "execute", - "source_unchanged": True, - "cleanup": True, - "selected_cases": [case["case_id"] for case in cases], - "source_tree_sha256": "0" * 64, - "lock_sha256": "1" * 64, - "dirty_patch_sha256": "2" * 64, - "commands": [ - {"name": name, "exit_code": 0, "cleanup": True} for name in commands - ], - }, - "cases.json": results, - "peers.json": peers, - "rust-events.jsonl": events, - "legacy-events.jsonl": legacy, - "resources.jsonl": [{"resources": idle}], - "summary.md": "Offline fixture only", - "native/stream-cases.json": { - "cases": native, - "cleanup": True, - "peers": peers, - "abnormal_cleanup": { - "passed": True, - "kind": "M", - "failure_injected": True, - "joined": True, - "port_rebound": True, - }, - }, - "script-tests.json": { - "tests_run": len(set(SCRIPT_OBSERVATIONS.values())), - "cases": [ - {"test": name, "status": "PASS"} - for name in sorted(set(SCRIPT_OBSERVATIONS.values())) - ], - }, - } - - def write_fixture(self, directory, data): - artifacts = [] - for name, value in data.items(): - if name == "run.json": - continue - path = directory / name - path.parent.mkdir(parents=True, exist_ok=True) - text = ( - "".join(json.dumps(item) + "\n" for item in value) - if name.endswith(".jsonl") - else value - if name.endswith(".md") - else json.dumps(value) - ) - path.write_text(text) - artifacts.append( - {"path": name, "sha256": hashlib.sha256(path.read_bytes()).hexdigest()} - ) - (directory / "run.json").write_text( - json.dumps(data["run.json"] | {"artifacts": artifacts}) - ) - - def test_complete_original_evidence_is_required_even_when_summary_claims_pass(self): - fixture = self.fixture() - with ( - tempfile.TemporaryDirectory() as temporary, - contextlib.redirect_stdout(io.StringIO()), - ): - directory = Path(temporary) - self.write_fixture(directory, fixture) - check_run(directory, "FOUNDATIONS") - for mutation in [ - "raw-event", - "nonidle", - "command", - "peer", - "artifact", - "quiet", - "script", - ]: - data = copy.deepcopy(fixture) - if mutation == "raw-event": - data["rust-events.jsonl"][1]["status"] = "FAIL" - elif mutation == "nonidle": - data["resources.jsonl"][0]["resources"]["counts"][0]["current"] = 1 - elif mutation == "command": - data["run.json"]["commands"][0]["exit_code"] = 124 - elif mutation == "peer": - data["peers.json"][0]["status"] = "BLOCKED" - elif mutation == "artifact": - del data["rust-events.jsonl"] - elif mutation == "quiet": - for event in data["rust-events.jsonl"]: - if event["suite"] == "FOUNDATIONS-RESOURCES": - event["checkpoints"] = [] - else: - data["script-tests.json"]["cases"][0]["status"] = "NOT RUN" - self.write_fixture(directory, data) - with self.subTest(mutation=mutation), self.assertRaises(ValueError): - check_run(directory, "FOUNDATIONS") - - def test_content_tampering_partial_runs_and_preflight_cannot_sign_off(self): - with tempfile.TemporaryDirectory() as temporary: - directory = Path(temporary) - data = self.fixture() - self.write_fixture(directory, data) - (directory / "summary.md").write_text("tampered") - with self.assertRaises(ValueError): - check_run(directory, "FOUNDATIONS") - for change in [ - {"selected_cases": []}, - {"mode": "preflight"}, - {"source_unchanged": False}, - ]: - data["run.json"].update(change) - self.write_fixture(directory, data) - with self.assertRaises(ValueError): - check_run(directory, "FOUNDATIONS") diff --git a/scripts/tests/test_protocol_security.py b/scripts/tests/test_protocol_security.py deleted file mode 100644 index 3f845da..0000000 --- a/scripts/tests/test_protocol_security.py +++ /dev/null @@ -1,202 +0,0 @@ -from __future__ import annotations - -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import load_manifest -from vcore_scripts.protocol_security_acceptance import ( - gate_result, - native_envelope, - rust_command, - vless_pass, -) -from vcore_scripts.protocol_security_catalog import FIELDS, definitions, groups - - -class SecurityAcceptanceTest(unittest.TestCase): - def test_legacy_ech_gateway_keeps_response_inside_tls(self): - from vcore_scripts.protocol_ech import legacy_gateway_command - - self.assertEqual( - legacy_gateway_command(), - [ - "env", - "XRAY_BUF_SPLICE=disable", - "/data/fixture/peer", - "run", - "-c", - "/data/fixture/config.json", - ], - ) - - def test_frozen_scope_and_compositions(self): - cases = definitions() - self.assertEqual( - [c for c in load_manifest() if c["stage"] == "SECURITY"], cases - ) - self.assertEqual(len(cases), 43) - self.assertEqual(set().union(*(set(c["row_ids"]) for c in cases)), FIELDS) - self.assertEqual(len(FIELDS), 11) - catalog = groups() - self.assertEqual(len(catalog["SECURITY-ECH-STANDARD"]["selected"]), 50) - for profile in ("chrome", "chrome120", "firefox", "safari"): - self.assertEqual( - catalog["SECURITY-ECH-" + profile.upper()]["client_fingerprint"], - profile, - ) - for security in ("ECH", "JLS", "HYBRID"): - self.assertEqual( - sum(k.startswith(f"SECURITY-{security}-ENCRYPTION-") for k in catalog), - 6, - ) - self.assertFalse( - any("shadow" in str(c).lower() or "restls" in str(c).lower() for c in cases) - ) - - def fixture(self, directory): - name, test = "SECURITY-ECH-TCP-TLS-REJECT", "native_ech_fail_closed" - group = dict(runner="vless", selected=[name], ech=True) - report = dict( - status="PASS", - cleanup=True, - source_unchanged=True, - source=dict( - parent_commit="7" * 40, - source_tree_sha256="a" * 64, - dirty_patch_sha256="b" * 64, - lock_sha256="c" * 64, - ), - ech=True, - jls=False, - encryption_profile=None, - client_fingerprint=None, - cases=[ - dict( - case_id=name, - command=rust_command("vless_native", test), - exit_code=0, - cleanup=True, - command_cleanup=True, - status="PASS", - peer_kind="M", - ) - ], - isolation=dict( - network_mode="hostOnly", - host_servers=False, - guest_mtu=1500, - image_digest="sha256:" + "d" * 64, - peers=[dict(started=True, joined=True)], - ), - peers={ - "M": dict( - version="synthetic test metadata", - binary_sha256="a" * 64, - archive_sha256="b" * 64, - source_url="https://github.com/MetaCubeX/mihomo/releases/latest/download/fixture", - ) - }, - ) - path = directory / (name + "-events.jsonl") - events = [ - dict(schema_version=1, suite="SECURITY-WIRE", assertion=test, status=s) - for s in ("BEGIN", "PASS") - ] - path.write_text("".join(json.dumps(e) + "\n" for e in events)) - return group, report, path, events - - def test_native_missing_duplicate_wrong_events_fail(self): - with tempfile.TemporaryDirectory() as temp: - directory = Path(temp) - group, report, path, events = self.fixture(directory) - self.assertTrue(vless_pass(group, report, directory)) - for samples in ([], report["cases"] * 2): - invalid = dict(report, cases=samples) - self.assertFalse(vless_pass(group, invalid, directory)) - for samples in ( - events[:1], - events * 2, - [dict(e, suite="VLESS-WIRE") for e in events], - [dict(e, status="PASS") for e in events], - ): - path.write_text("".join(json.dumps(e) + "\n" for e in samples)) - self.assertFalse(vless_pass(group, report, directory)) - - def test_source_cleanup_and_identity_are_not_inferred(self): - with tempfile.TemporaryDirectory() as temp: - _, report, _, _ = self.fixture(Path(temp)) - self.assertTrue(native_envelope(report, report["peers"], report["source"])) - self.assertTrue(native_envelope(report, image_digest="sha256:" + "d" * 64)) - self.assertFalse(native_envelope(report, image_digest="sha256:" + "e" * 64)) - for field in ("cleanup", "source_unchanged"): - self.assertFalse(native_envelope(dict(report, **{field: False}))) - self.assertFalse(native_envelope(report, {}, report["source"])) - self.assertFalse(native_envelope(report, report["peers"], {})) - for mutate in ( - lambda r: r["isolation"].update(host_servers=True), - lambda r: r["isolation"].update(peers=[]), - lambda r: r["isolation"]["peers"][0].update(joined=False), - lambda r: r["peers"]["M"].update(binary_sha256=""), - ): - invalid = copy.deepcopy(report) - mutate(invalid) - self.assertFalse(native_envelope(invalid)) - - def test_close_reference_cannot_hide_a_different_official_profile(self): - with tempfile.TemporaryDirectory() as temp: - directory = Path(temp) - group, report, _, _ = self.fixture(directory) - name, test = "SECURITY-ECH-GRPC-TLS-CLOSE", "native_mihomo_close_alignment" - group.update(selected=[name], client_fingerprint="safari") - report.update(client_fingerprint="safari") - report["cases"][0].update( - case_id=name, command=rust_command("vless_native", test) - ) - (directory / (name + "-events.jsonl")).write_text( - "".join( - json.dumps( - dict( - schema_version=1, - suite="SECURITY-WIRE", - assertion=test, - status=s, - ) - ) - + "\n" - for s in ("BEGIN", "PASS") - ) - ) - path = directory / "grpc-tls-tls-close-reference.json" - self.assertFalse(vless_pass(group, report, directory)) - valid = dict( - scope="ech-safari-chrome-baseline", - client_fingerprint="chrome", - dut_client_fingerprint="safari", - terminated=True, - tail_hex="", - ) - path.write_text(json.dumps(valid)) - self.assertTrue(vless_pass(group, report, directory)) - for field, value in ( - ("scope", "same-mode"), - ("client_fingerprint", "safari"), - ("dut_client_fingerprint", "chrome"), - ("terminated", False), - ): - path.write_text(json.dumps(dict(valid, **{field: value}))) - self.assertFalse(vless_pass(group, report, directory)) - - def test_empty_gate_and_missing_script_proof_fail(self): - cases = {c["case_id"]: c for c in definitions()} - for name in ("SECURITY-CFG", "SECURITY-RELEASE", "SECURITY-SCRIPTS"): - self.assertEqual(gate_result(cases[name], [], [])["status"], "FAIL") - record = [dict(exit_code=0, cleanup=True)] - self.assertEqual( - gate_result(cases["SECURITY-CFG"], record, [])["status"], "FAIL" - ) - self.assertEqual( - gate_result(cases["SECURITY-SCRIPTS"], record, [], {})["status"], "FAIL" - ) diff --git a/scripts/tests/test_protocol_shadowtls.py b/scripts/tests/test_protocol_shadowtls.py deleted file mode 100644 index 942d2da..0000000 --- a/scripts/tests/test_protocol_shadowtls.py +++ /dev/null @@ -1,125 +0,0 @@ -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import load_manifest, validate_results -from vcore_scripts.protocol_inputs import redact -from vcore_scripts.protocol_security_acceptance import rust_command -from vcore_scripts.protocol_shadowtls_acceptance import ( - expected_observation, - fault_pass, - local_pass, - result, - wire_pass, -) -from vcore_scripts.protocol_shadowtls_catalog import SS_CASES, commands, definitions - - -class ShadowTlsAcceptanceTest(unittest.TestCase): - def test_local_command_identity_uses_the_same_redaction_as_the_runner(self): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary) - records = [] - for index, argv in enumerate(commands("QUALITY")): - name = f"SHADOWTLS-QUALITY-{index}" - (root / (name + ".log")).write_text("") - records.append( - dict( - name=name, - command=[redact(v) for v in argv], - exit_code=0, - cleanup=True, - log=name + ".log", - ) - ) - self.assertTrue(local_pass("QUALITY", root, {"commands": records})) - records[0]["command"] = ["true"] - self.assertFalse(local_pass("QUALITY", root, {"commands": records})) - - def test_wire_proof_requires_all_algorithms_raw_events_and_actual_counts(self): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary) - rows = [] - for name in SS_CASES: - observed = expected_observation("native_tcp", True) - (root / (name + "-observations.json")).write_text(json.dumps(observed)) - (root / (name + "-events.jsonl")).write_text( - "".join( - json.dumps( - dict( - schema_version=1, - suite="SHADOWTLS", - assertion="native_tcp", - status=status, - ) - ) - + "\n" - for status in ("BEGIN", "PASS") - ) - ) - rows.append( - dict( - case_id=name, - command=rust_command("vless_public", "shadowtls::native_tcp"), - exit_code=0, - command_cleanup=True, - observations=observed, - ) - ) - self.assertTrue(wire_pass("NATIVE", root, {"cases": rows})) - self.assertFalse(wire_pass("NATIVE", root, {"cases": rows[:-1]})) - observed = dict(rows[0]["observations"], tcp_bytes_each_direction=0) - rows[0]["observations"] = observed - (root / (SS_CASES[0] + "-observations.json")).write_text( - json.dumps(observed) - ) - self.assertFalse(wire_pass("NATIVE", root, {"cases": rows})) - - def test_suite_is_registered_and_cannot_accept_a_partial_or_forged_result(self): - cases = definitions() - self.assertEqual( - cases, [c for c in load_manifest() if c["stage"] == "SHADOWTLS"] - ) - self.assertEqual(len(cases), 11) - records = [] - for case in cases: - record = result(case, True, True) - self.assertEqual(record["scope"], "protocol-consumer") - records.append(record) - validate_results(cases, records) - with self.assertRaises(ValueError): - validate_results(cases, records[:-1]) - forged = copy.deepcopy(records) - forged[0]["assertions"] = {} - with self.assertRaises(ValueError): - validate_results(cases, forged) - - def test_fault_proof_requires_actual_injection_hrr_or_closed_handshakes(self): - for label, field in [ - ("fragment", "split_record"), - ("hrr-wire", "hrr_seen"), - ("business-mac", "injected"), - ("cover-mac", "injected"), - ]: - self.assertFalse(fault_pass(label, [], [])) - self.assertTrue(fault_pass(label, [{field: True}], [])) - self.assertFalse( - fault_pass(label, [{field: True, "upload_join_failed": True}], []) - ) - events = [{"hello_seen": True}, {"client_closed": True}] * 2 - self.assertTrue(fault_pass("stall", events, [])) - self.assertFalse(fault_pass("stall", events[:-1], [])) - self.assertFalse(fault_pass("stall", events, [{"version": "TLSv1.3"}])) - - def test_no_implicit_uot_and_authentication_evidence_are_distinct_from_bulk(self): - self.assertEqual(expected_observation("data", True)["udp_packets"], 500) - self.assertEqual(expected_observation("native_tcp", True)["udp_packets"], 0) - self.assertFalse(expected_observation("policy", False)["origin_connected"]) - self.assertEqual(expected_observation("corrupt", True)["delivered_bytes"], 0) - self.assertFalse( - expected_observation("native_udp_disabled", True)["udp_delivered"] - ) - with self.assertRaises(ValueError): - expected_observation("unknown", True) diff --git a/scripts/tests/test_protocol_trojan.py b/scripts/tests/test_protocol_trojan.py deleted file mode 100644 index bc76938..0000000 --- a/scripts/tests/test_protocol_trojan.py +++ /dev/null @@ -1,161 +0,0 @@ -from __future__ import annotations - -import copy -import json -import tempfile -import tomllib -import unittest -from pathlib import Path - -from vcore_scripts.builds import CORE_DIR, DEFAULT_FEATURES -from vcore_scripts.protocol_evidence import load_manifest, select_cases -from vcore_scripts.protocol_trojan import CASES, native_events_pass -from vcore_scripts.protocol_trojan_acceptance import fields_report, native_results - - -class TrojanEvidenceTest(unittest.TestCase): - def test_removed_or_downgraded_required_case_is_rejected(self): - original = load_manifest() - index = next(i for i, case in enumerate(original) if case["stage"] == "TROJAN") - for downgrade in (True, False): - cases = copy.deepcopy(original) - if downgrade: - cases[index]["required"] = False - else: - cases.pop(index) - with tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "cases.json" - path.write_text( - json.dumps( - { - "schema_version": 1, - "kind": "executable-case-manifest", - "cases": cases, - } - ) - ) - with self.assertRaises(ValueError): - load_manifest(path) - - def test_native_summary_cannot_hide_missing_events_or_unjoined_processes(self): - case = next( - case for case in load_manifest() if case["case_id"] == "TROJAN-M-TCP" - ) - record = { - "case_id": case["case_id"], - "status": "PASS", - "mode": "tcp", - "exit_code": 0, - "command_cleanup": True, - "cleanup": {"joined": True}, - } - report = {"source_unchanged": True, "cases": [record]} - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - self.assertEqual(native_results([case], report, root)[0]["status"], "FAIL") - events = [ - { - "suite": "TROJAN-NATIVE", - "assertion": "public_trojan_native_base", - "schema_version": 1, - "status": status, - } - for status in ("BEGIN", "PASS") - ] - (root / (case["case_id"] + "-events.jsonl")).write_text( - "".join(json.dumps(event) + "\n" for event in events) - ) - self.assertEqual(native_results([case], report, root)[0]["status"], "PASS") - for update in [ - {"exit_code": 124}, - {"command_cleanup": False}, - {"cleanup": {"joined": False}}, - {"status": "BLOCKED"}, - ]: - invalid = copy.deepcopy(report) - invalid["cases"][0].update(update) - self.assertEqual( - native_results([case], invalid, root)[0]["status"], "FAIL" - ) - report["source_unchanged"] = False - self.assertEqual(native_results([case], report, root)[0]["status"], "FAIL") - report["cases"].append(record) - with self.assertRaises(ValueError): - native_results([case], report, root) - - def test_partial_case_selection_never_signs_off_fields(self): - case = next(case for case in load_manifest() if case["case_id"] == "TROJAN-CFG") - result = fields_report([case], [{"case_id": case["case_id"], "status": "PASS"}]) - self.assertEqual(len(result["fields"]), 18) - self.assertTrue(all(field["status"] == "NOT RUN" for field in result["fields"])) - - def test_default_and_packaged_builds_enable_trojan(self): - features = tomllib.loads((CORE_DIR / "Cargo.toml").read_text())["features"] - self.assertIn("outbound-trojan", features["default"]) - self.assertIn("outbound-trojan", features["tun"]) - self.assertIn("outbound-trojan", DEFAULT_FEATURES.split(",")) - - def test_every_native_case_is_required_and_mapped_to_fields(self): - cases = select_cases(load_manifest(), stage="TROJAN") - native = { - case["case_id"]: case for case in cases if case["runner"] == "native-trojan" - } - self.assertEqual(set(native), set(CASES)) - for identifier, (kind, mode, test) in CASES.items(): - self.assertEqual(native[identifier]["peer_kind"], kind) - self.assertEqual( - native[identifier]["peer_config"], {"mode": mode, "test": test} - ) - self.assertTrue(native[identifier]["row_ids"]) - if kind != "M": - self.assertTrue(native[identifier]["gap_source"]) - - def test_zero_tests_partial_duplicate_wrong_or_failed_events_fail(self): - test = "public_trojan_native_base" - events = [ - { - "suite": "TROJAN-NATIVE", - "assertion": test, - "schema_version": 1, - "status": status, - } - for status in ("BEGIN", "PASS") - ] - self.assertTrue(native_events_pass(events, test)) - for invalid in [[], events[:1], events + events, events[::-1]]: - self.assertFalse(native_events_pass(invalid, test)) - for update in [ - {"status": "FAIL"}, - {"assertion": "other"}, - {"schema_version": 2}, - ]: - invalid = copy.deepcopy(events) - invalid[1].update(update) - self.assertFalse(native_events_pass(invalid, test)) - - def test_lifecycle_requires_twenty_full_quiet_cycles(self): - test = "runtime::public_trojan_native_lifecycle" - events = [ - { - "suite": "TROJAN-NATIVE", - "assertion": test, - "schema_version": 1, - "status": status, - } - for status in ("BEGIN", "PASS") - ] - cycles = [ - { - "suite": "TROJAN-LIFE-CYCLE", - "assertion": "stop_and_remain_quiet", - "schema_version": 1, - "status": status, - "seconds": 5.1 if status == "PASS" else 0, - } - for _ in range(20) - for status in ("BEGIN", "PASS") - ] - self.assertTrue(native_events_pass(events + cycles, test)) - self.assertFalse(native_events_pass(events + cycles[:-2], test)) - cycles[-1]["seconds"] = 4.9 - self.assertFalse(native_events_pass(events + cycles, test)) diff --git a/scripts/tests/test_protocol_tuic.py b/scripts/tests/test_protocol_tuic.py deleted file mode 100644 index bba2f4f..0000000 --- a/scripts/tests/test_protocol_tuic.py +++ /dev/null @@ -1,145 +0,0 @@ -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import load_manifest, validate_results -from vcore_scripts.protocol_security_acceptance import result, rust_command -from vcore_scripts.protocol_tuic import expected_observation, inputs, upstream_pair -from vcore_scripts.protocol_tuic_acceptance import wire_pass -from vcore_scripts.protocol_tuic_catalog import definitions, wire_cases - - -class TuicAcceptanceTest(unittest.TestCase): - def test_registered_gates_cannot_omit_assertions_or_required_groups(self): - cases = definitions() - self.assertEqual(len(cases), 16) - self.assertEqual(cases, [c for c in load_manifest() if c["stage"] == "TUIC"]) - results = [result(c, True, True) for c in cases] - validate_results(cases, results) - with self.assertRaises(ValueError): - validate_results(cases, results[:-1]) - broken = copy.deepcopy(results) - broken[0]["assertions"] = {} - with self.assertRaises(ValueError): - validate_results(cases, broken) - - def test_runner_matches_independent_wire_matrix(self): - node = dict( - udp=True, - uuid="synthetic", - password="synthetic", - fingerprint="00" * 32, - alpn=["h3"], - **{"udp-relay-mode": "native"}, - ) - for name in ("TCP", "POLICY", "UDP", "PATHS", "NEGATIVE"): - rows = inputs(node, {}, name.lower()) - self.assertEqual([(r[0], r[3]) for r in rows], wire_cases("MIHOMO-" + name)) - - def test_raw_wire_evidence_rejects_truncation_forged_observation_and_wrong_consumer( - self, - ): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary) - for name in ( - "MIHOMO-TCP", - "MIHOMO-POLICY", - "MIHOMO-UDP", - "MIHOMO-PATHS", - "MIHOMO-NEGATIVE", - ): - rows = [] - for identifier, consumer in wire_cases(name): - observed = expected_observation(consumer) - (root / (identifier + "-observations.json")).write_text( - json.dumps(observed) - ) - (root / (identifier + "-events.jsonl")).write_text( - "".join( - json.dumps( - dict( - schema_version=1, - suite="TUIC-PUBLIC", - assertion=consumer, - status=status, - ) - ) - + "\n" - for status in ("BEGIN", "PASS") - ) - ) - rows.append( - dict( - case_id=identifier, - command=rust_command("vless_public", "tuic::" + consumer), - exit_code=0, - command_cleanup=True, - observations=observed, - ) - ) - report = dict(cases=rows) - self.assertTrue(wire_pass(name, root, report)) - self.assertFalse(wire_pass(name, root, dict(cases=rows[:-1]))) - for key, value in ( - ("observations", {}), - ("command_cleanup", False), - ("exit_code", True), - ( - "command", - rust_command( - "vless_public", - "tuic::tcp" - if rows[0]["command"] - != rust_command("vless_public", "tuic::tcp") - else "tuic::udp", - ), - ), - ): - broken = copy.deepcopy(report) - broken["cases"][0][key] = value - self.assertFalse(wire_pass(name, root, broken)) - - def test_mixed_pressure_requires_active_tuic_and_inactive_hysteria2(self): - from vcore_scripts.protocol_evidence import RESOURCE_KINDS - from vcore_scripts.protocol_integration_metrics import QUEUES, sample - - value = dict( - heap_in_use=1, - rss_kib=1, - fd=1, - resources=dict( - counts=[dict(kind=k, current=1, peak=1) for k in RESOURCE_KINDS] - ), - queues=[ - dict(kind=k, capacity=v, peak=int(v > 0)) for k, v in QUEUES.items() - ], - ) - self.assertTrue(sample(value)) - for kind, mutation in ( - ("tuic_udp", dict(capacity=0, peak=0)), - ("tuic_udp", dict(peak=33)), - ("hysteria2_udp", dict(capacity=32)), - ("hysteria2_udp", dict(peak=1)), - ("quic_incoming", dict(kind="unknown")), - ): - broken = copy.deepcopy(value) - next(q for q in broken["queues"] if q["kind"] == kind).update(mutation) - self.assertFalse(sample(broken)) - value["queues"].pop() - self.assertFalse(sample(value)) - - def test_anytls_fixture_uses_listener_password_map(self): - client, peer = upstream_pair( - "anytls-native", - {"server": "192.0.2.1"}, - (Path("cert.pem"), Path("key.pem"), "00" * 32), - 0, - ) - self.assertEqual(peer["users"], {"fixture": client["password"]}) - self.assertTrue(client["udp"]) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_uot.py b/scripts/tests/test_protocol_uot.py deleted file mode 100644 index 0be33e0..0000000 --- a/scripts/tests/test_protocol_uot.py +++ /dev/null @@ -1,81 +0,0 @@ -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_evidence import load_manifest, validate_results -from vcore_scripts.protocol_security_acceptance import result, rust_command -from vcore_scripts.protocol_uot import expected_observation -from vcore_scripts.protocol_uot_acceptance import wire_pass -from vcore_scripts.protocol_uot_catalog import definitions, wire_cases - - -class UotAcceptanceTest(unittest.TestCase): - def test_registered_groups_use_real_result_keys_and_require_every_group(self): - cases = definitions() - self.assertEqual(cases, [c for c in load_manifest() if c["stage"] == "UOT"]) - self.assertEqual(len(cases), 11) - results = [result(c, True, True) for c in cases] - validate_results(cases, results) - with self.assertRaises(ValueError): - validate_results(cases, results[:-1]) - forged = copy.deepcopy(results) - forged[0]["assertions"] = {} - with self.assertRaises(ValueError): - validate_results(cases, forged) - - def test_all_six_combinations_need_raw_packets_events_and_no_native_fallback(self): - with tempfile.TemporaryDirectory() as temp: - root = Path(temp) - rows = [] - for identifier, consumer in wire_cases("MIHOMO-DIRECT"): - observation = expected_observation(consumer) - (root / (identifier + "-observations.json")).write_text( - json.dumps(observation) - ) - (root / (identifier + "-events.jsonl")).write_text( - "".join( - json.dumps( - dict( - schema_version=1, - suite="UOT-PUBLIC", - assertion=consumer, - status=status, - ) - ) - + "\n" - for status in ("BEGIN", "PASS") - ) - ) - rows.append( - dict( - case_id=identifier, - command=rust_command("vless_public", "uot::" + consumer), - exit_code=0, - command_cleanup=True, - observations=observation, - listener_native_udp=False, - ) - ) - report = dict(cases=rows, tcp_only_upstream=False) - self.assertEqual(len(rows), 6) - self.assertTrue(wire_pass("MIHOMO-DIRECT", root, report)) - self.assertFalse(wire_pass("MIHOMO-SOCKS5", root, report)) - self.assertFalse( - wire_pass("MIHOMO-DIRECT", root, dict(report, cases=rows[:-1])) - ) - rows[0]["listener_native_udp"] = True - self.assertFalse(wire_pass("MIHOMO-DIRECT", root, report)) - rows[0]["listener_native_udp"] = False - rows[0]["observations"]["native_udp_packets"] = 1 - self.assertFalse(wire_pass("MIHOMO-DIRECT", root, report)) - - def test_negative_and_group_cases_are_not_counted_as_data(self): - self.assertEqual(len(wire_cases("MIHOMO-NEGATIVE")), 9) - self.assertEqual(len(wire_cases("NATIVE-UNSUPPORTED")), 3) - self.assertTrue(all(test == "group" for _, test in wire_cases("MIHOMO-GROUPS"))) - self.assertEqual(expected_observation("rejected")["origin_packets"], 0) - self.assertEqual(expected_observation("data")["udp_sizes"][-1], 16384) - with self.assertRaises(ValueError): - expected_observation("unsupported") diff --git a/scripts/tests/test_protocol_vless.py b/scripts/tests/test_protocol_vless.py deleted file mode 100644 index bace8de..0000000 --- a/scripts/tests/test_protocol_vless.py +++ /dev/null @@ -1,389 +0,0 @@ -"""Offline evidence validation: no protocol listener or origin is launched.""" - -from __future__ import annotations - -import copy -import json -import tempfile -import tomllib -import unittest -from pathlib import Path -from types import SimpleNamespace -from unittest.mock import patch - -from vcore_scripts.builds import CORE_DIR, DEFAULT_FEATURES -from vcore_scripts.protocol_evidence import RESOURCE_KINDS, load_manifest -from vcore_scripts.protocol_vless_acceptance import ( - FIELD_IDS, - NATIVE, - OBSERVATIONS, - REQUIRED_IDS, - definitions, - execute, - fields_report, - gate_result, - native_results, - required_command_names, -) -from vcore_scripts.protocol_vless_container import close_reference, run -from vcore_scripts.protocol_vless_peers import configuration -from vcore_scripts.protocol_vless_public import events_pass - - -def pair(suite, name): - return [ - dict(schema_version=1, suite=suite, assertion=name, status=status) - for status in ("BEGIN", "PASS") - ] - - -class VlessEvidenceTest(unittest.TestCase): - def test_fingerprint_runner_rejects_unknown_profile_or_plaintext_before_io(self): - with tempfile.TemporaryDirectory() as directory: - output = Path(directory) / "must-not-exist" - for profile, cases in [ - ("chrome133", ["VLESS-TCP-TLS-BASE"]), - ("chrome120", ["VLESS-TCP-BASE"]), - ]: - with self.assertRaises(ValueError): - run(output, cases, client_fingerprint=profile) - self.assertFalse(output.exists()) - - def test_official_ws_reference_preserves_vcore_default_and_explicit_ed_locations( - self, - ): - for specified in (None, "", "X-Synthetic-ED"): - options = {"max-early-data": 1, "v2ray-http-upgrade-fast-open": True} - if specified is not None: - options["early-data-header-name"] = specified - node = {"network": "ws", "ws-opts": options} - reference, scope = close_reference( - "upgrade-fast-tls", node, {}, Path("cert"), Path("key"), "pin" - ) - self.assertEqual(scope, "same-mode") - self.assertEqual( - reference["ws-opts"]["early-data-header-name"], - "Sec-WebSocket-Protocol" if specified is None else specified, - ) - self.assertEqual( - "early-data-header-name" in node["ws-opts"], specified is not None - ) - - def test_vision_flow_rejection_uses_plain_user_without_mutating_valid_peer(self): - for mode in ("vision-tls", "vision-reality"): - node, config = configuration( - mode, "server", "origin", Path("cert"), Path("key") - ) - self.assertEqual(node["flow"], "xtls-rprx-vision") - valid, plain = config["listeners"] - self.assertEqual(valid["users"][0]["flow"], "xtls-rprx-vision") - self.assertEqual(plain["users"][0]["flow"], "") - self.assertEqual((valid["port"], plain["port"]), (23000, 23004)) - self.assertEqual(valid.get("reality-config"), plain.get("reality-config")) - self.assertEqual(valid.get("certificate"), plain.get("certificate")) - - def test_ws_reality_close_reference_is_explicitly_layered_not_same_mode(self): - node = { - "network": "ws", - "tls": True, - "port": 23000, - "reality-opts": {"public-key": "synthetic"}, - } - config = { - "listeners": [{"port": 23000, "reality-config": {"dest": "synthetic"}}] - } - reference, scope = close_reference( - "ws-reality", node, config, Path("cert"), Path("key"), "pin" - ) - self.assertEqual(scope, "ws-standard-tls-baseline") - self.assertIn("reality-opts", node) - self.assertNotIn("reality-opts", reference) - self.assertEqual(reference["port"], 23003) - self.assertIn("reality-config", config["listeners"][0]) - self.assertNotIn("reality-config", config["listeners"][1]) - case = next( - c for c in definitions() if c["case_id"] == "VLESS-WS-REALITY-CLOSE" - ) - self.assertEqual(case["field_values"]["close_reference"], scope) - self.assertIn("not a same-combination", case["gap_source"]["reason"]) - - def test_jls_grpc_close_reference_labels_the_official_alpn_type_gap(self): - for profile in (None, "", "none", "chrome", "chrome120", "firefox", "safari"): - node = { - "network": "grpc", - "tls": True, - "jls-opts": {"username": "synthetic", "password": "synthetic"}, - } - if profile is not None: - node["client-fingerprint"] = profile - original = copy.deepcopy(node) - reference, scope = close_reference( - "grpc-tls", node, {}, Path("cert"), Path("key"), "pin" - ) - baseline = profile in (None, "", "none") - self.assertEqual( - scope, "jls-grpc-chrome-baseline" if baseline else "same-mode" - ) - self.assertEqual( - reference["client-fingerprint"], "chrome" if baseline else profile - ) - self.assertEqual(reference["jls-opts"], node["jls-opts"]) - self.assertEqual(node, original) - - for node in ({"network": "grpc"}, {"network": "tcp", "jls-opts": {}}): - reference, scope = close_reference( - "tcp-tls", node, {}, Path("cert"), Path("key"), "pin" - ) - self.assertEqual(scope, "same-mode") - self.assertNotIn("client-fingerprint", reference) - - def test_reality_close_reference_keeps_explicit_browser_profiles(self): - for profile in (None, "none", "chrome120", "firefox", "safari"): - node = {"network": "grpc"} - if profile is not None: - node["client-fingerprint"] = profile - reference, scope = close_reference( - "grpc-reality", node, {}, Path("cert"), Path("key"), "pin" - ) - self.assertEqual(scope, "same-mode") - self.assertEqual( - reference["client-fingerprint"], - "chrome" if profile in (None, "none") else profile, - ) - - def test_safari_ech_close_reference_labels_the_official_client_gap(self): - for profile in ("safari", "safari16", "chrome", "firefox", "none"): - for enabled in (False, True): - node = { - "network": "grpc", - "tls": True, - "client-fingerprint": profile, - "ech-opts": {"enable": enabled, "config": "synthetic"}, - } - original = copy.deepcopy(node) - reference, scope = close_reference( - "grpc-tls", node, {}, Path("cert"), Path("key"), "pin" - ) - baseline = enabled and profile in ("safari", "safari16") - self.assertEqual( - scope, "ech-safari-chrome-baseline" if baseline else "same-mode" - ) - self.assertEqual( - reference["client-fingerprint"], "chrome" if baseline else profile - ) - self.assertEqual(reference["ech-opts"], node["ech-opts"]) - self.assertEqual(node, original) - - def test_independent_features_match_the_executed_command_set(self): - case = next(c for c in definitions() if c["case_id"] == "VLESS-FEATURES") - with ( - tempfile.TemporaryDirectory() as root, - patch( - "vcore_scripts.protocol_harness._command", - return_value=SimpleNamespace(returncode=0, cleanup=True), - ) as command, - ): - execute([case], {"commands": []}, Path(root), {}) - actual = {call.args[2] for call in command.call_args_list} - expected = { - name - for name in required_command_names() - if name.startswith("feature-") or name == "production-build" - } - self.assertEqual(actual, expected) - - def test_required_manifest_cannot_drop_or_downgrade_behavior(self): - original = load_manifest() - index = next(i for i, case in enumerate(original) if case["stage"] == "VLESS") - for mutation in ("drop", "required", "observation", "row"): - cases = copy.deepcopy(original) - if mutation == "drop": - cases.pop(index) - elif mutation == "required": - cases[index]["required"] = False - elif mutation == "observation": - cases[index]["expected_observation"] = ["always-green"] - else: - cases[index]["row_ids"] = ["VL01"] - with tempfile.TemporaryDirectory() as root: - path = Path(root) / "cases.json" - path.write_text( - json.dumps( - dict( - schema_version=1, - kind="executable-case-manifest", - cases=cases, - ) - ) - ) - with self.assertRaises(ValueError): - load_manifest(path) - - def test_fields_require_complete_native_and_public_evidence(self): - cases = definitions() - self.assertEqual({c["case_id"] for c in cases}, REQUIRED_IDS) - self.assertEqual( - {c["case_id"] for c in cases if c["runner"] == "native-vless"}, set(NATIVE) - ) - results = [dict(case_id=c["case_id"], status="PASS") for c in cases] - fields = fields_report(cases, results)["fields"] - self.assertEqual({f["row_id"] for f in fields}, FIELD_IDS) - self.assertTrue(all(f["status"] == "PASS" for f in fields)) - for bad in ( - results[:-1], - results[:-1] + [dict(case_id=results[-1]["case_id"], status="NOT RUN")], - ): - self.assertTrue( - all( - f["status"] == "NOT RUN" - for f in fields_report(cases, bad)["fields"] - ) - ) - features = tomllib.loads((CORE_DIR / "Cargo.toml").read_text())["features"] - for enabled in ( - features["default"], - features["tun"], - DEFAULT_FEATURES.split(","), - ): - self.assertIn("outbound-vless", enabled) - - def test_zero_partial_duplicate_failed_or_unjoined_rust_gate_is_not_pass(self): - cases = {c["case_id"]: c for c in definitions()} - for identifier, names in OBSERVATIONS.items(): - case = cases[identifier] - events = [e for name in names for e in pair(identifier, name)] - commands = [dict(exit_code=0, cleanup=True)] - self.assertEqual(gate_result(case, commands, events)["status"], "PASS") - for bad in ( - [], - events[:-1], - events + events[:1], - events[:-1] + [events[-1] | {"status": "FAIL"}], - ): - self.assertEqual(gate_result(case, commands, bad)["status"], "FAIL") - for record in ( - dict(exit_code=124, cleanup=True), - dict(exit_code=0, cleanup=False), - ): - self.assertEqual(gate_result(case, [record], events)["status"], "FAIL") - - def test_native_summary_cannot_hide_absent_events_changed_source_or_cleanup(self): - case = next( - c for c in definitions() if c["case_id"] == "VLESS-VISION-TLS-INNER-TLS" - ) - test = case["peer_config"]["test"] - record = dict( - case_id=case["case_id"], - status="PASS", - exit_code=0, - command_cleanup=True, - cleanup=True, - peer_kind="M", - command=[ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vless_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ], - ) - report = dict(cases=[record], source_unchanged=True, cleanup=True) - with tempfile.TemporaryDirectory() as root: - path = Path(root) - self.assertEqual(native_results([case], report, path)[0]["status"], "FAIL") - (path / (case["case_id"] + "-events.jsonl")).write_text( - "".join(json.dumps(e) + "\n" for e in pair("VLESS-WIRE", test)) - ) - self.assertEqual(native_results([case], report, path)[0]["status"], "PASS") - for key in ("cleanup", "source_unchanged"): - self.assertEqual( - native_results([case], report | {key: False}, path)[0]["status"], - "FAIL", - ) - for change in ( - dict(exit_code=124), - dict(command_cleanup=False), - dict(cleanup=False), - dict(command=[]), - dict(peer_kind="V2"), - ): - self.assertEqual( - native_results([case], report | {"cases": [record | change]}, path)[ - 0 - ]["status"], - "FAIL", - ) - with self.assertRaises(ValueError): - native_results([case], report | {"cases": [record, record]}, path) - - def test_public_base_requires_every_family_codec_and_body_option(self): - test = "public_base" - events = pair("VLESS-PUBLIC", test) - self.assertFalse(events_pass(events, test, "tcp")) - for name in ("tcp_10mib_both_directions", "udp_each_codec_and_family"): - events += [e for _ in range(3) for e in pair("VLESS-BASE", name)] - self.assertTrue(events_pass(events, test, "tcp")) - self.assertFalse(events_pass(events[:-2], test, "tcp")) - self.assertFalse(events_pass(events + events[-2:], test, "tcp")) - security = [ - dict(event, suite=event["suite"].replace("VLESS-", "SECURITY-")) - for event in events - ] - self.assertTrue(events_pass(security, test, "tcp", stage="SECURITY")) - self.assertFalse(events_pass(security[:-2], test, "tcp", stage="SECURITY")) - self.assertFalse(events_pass(security, test, "tcp")) - vision = ( - pair("VLESS-PUBLIC", test) - + [ - e - for _ in range(3) - for e in pair("VLESS-BASE", "tcp_10mib_both_directions") - ] - + pair("VLESS-BASE", "udp_each_codec_and_family") - ) - self.assertTrue(events_pass(vision, test, "vision-tls")) - self.assertFalse(events_pass(vision, test, "tcp")) - - def test_owned_resources_need_twenty_idle_stop_and_quiet_cycles(self): - snapshot = dict( - counts=[ - dict(kind=kind, current=0, peak=1) for kind in sorted(RESOURCE_KINDS) - ] - ) - test = "runtime::owned_resources" - events = pair("VLESS-PUBLIC", test) - for _ in range(20): - cycle = pair("VLESS-OWNED", "stop_and_remain_quiet") - cycle[-1].update( - seconds=5, - resources=snapshot, - checkpoints=[ - dict(phase=phase, resources=snapshot) - for phase in ("baseline", "after-stop", "quiet") - ], - ) - events += cycle - self.assertTrue(events_pass(events, test, "tcp")) - for change in ( - {"seconds": 4.9}, - {"checkpoints": []}, - {"resources": {}}, - {"status": "FAIL"}, - ): - self.assertFalse( - events_pass(events[:-1] + [events[-1] | change], test, "tcp") - ) - self.assertFalse(events_pass(events[:-2], test, "tcp")) - busy = copy.deepcopy(events) - busy[-1]["resources"]["counts"][0]["current"] = 1 - self.assertFalse(events_pass(busy, test, "tcp")) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_vmess.py b/scripts/tests/test_protocol_vmess.py deleted file mode 100644 index c6a0c88..0000000 --- a/scripts/tests/test_protocol_vmess.py +++ /dev/null @@ -1,214 +0,0 @@ -"""Offline evidence validation: no protocol listener or origin is launched.""" - -from __future__ import annotations - -import copy -import json -import tempfile -import tomllib -import unittest -from pathlib import Path - -from vcore_scripts.builds import CORE_DIR, DEFAULT_FEATURES -from vcore_scripts.protocol_evidence import RESOURCE_KINDS, load_manifest -from vcore_scripts.protocol_vmess_acceptance import ( - FIELD_IDS, - NATIVE, - OBSERVATIONS, - REQUIRED_IDS, - definitions, - fields_report, - gate_result, - native_results, -) -from vcore_scripts.protocol_vmess_public import events_pass - - -def pair(suite, name): - return [ - dict(schema_version=1, suite=suite, assertion=name, status=status) - for status in ("BEGIN", "PASS") - ] - - -class VmessEvidenceTest(unittest.TestCase): - def test_required_manifest_cannot_drop_or_downgrade_behavior(self): - original = load_manifest() - index = next(i for i, case in enumerate(original) if case["stage"] == "VMESS") - for mutation in ("drop", "required", "observation", "row"): - cases = copy.deepcopy(original) - if mutation == "drop": - cases.pop(index) - elif mutation == "required": - cases[index]["required"] = False - elif mutation == "observation": - cases[index]["expected_observation"] = ["always-green"] - else: - cases[index]["row_ids"] = ["VM01"] - with tempfile.TemporaryDirectory() as root: - path = Path(root) / "cases.json" - path.write_text( - json.dumps( - dict( - schema_version=1, - kind="executable-case-manifest", - cases=cases, - ) - ) - ) - with self.assertRaises(ValueError): - load_manifest(path) - - def test_fields_require_complete_native_and_public_evidence(self): - cases = definitions() - self.assertEqual({c["case_id"] for c in cases}, REQUIRED_IDS) - self.assertEqual( - {c["case_id"] for c in cases if c["runner"] == "native-vmess"}, set(NATIVE) - ) - results = [dict(case_id=c["case_id"], status="PASS") for c in cases] - fields = fields_report(cases, results)["fields"] - self.assertEqual({f["row_id"] for f in fields}, FIELD_IDS) - self.assertTrue(all(f["status"] == "PASS" for f in fields)) - for bad in ( - results[:-1], - results[:-1] + [dict(case_id=results[-1]["case_id"], status="NOT RUN")], - ): - self.assertTrue( - all( - f["status"] == "NOT RUN" - for f in fields_report(cases, bad)["fields"] - ) - ) - features = tomllib.loads((CORE_DIR / "Cargo.toml").read_text())["features"] - for enabled in ( - features["default"], - features["tun"], - DEFAULT_FEATURES.split(","), - ): - self.assertIn("outbound-vmess", enabled) - - def test_zero_partial_duplicate_failed_or_unjoined_rust_gate_is_not_pass(self): - cases = {c["case_id"]: c for c in definitions()} - for identifier, names in OBSERVATIONS.items(): - case = cases[identifier] - events = [e for name in names for e in pair(identifier, name)] - commands = [dict(exit_code=0, cleanup=True)] - self.assertEqual(gate_result(case, commands, events)["status"], "PASS") - for bad in ( - [], - events[:-1], - events + events[:1], - events[:-1] + [events[-1] | {"status": "FAIL"}], - ): - self.assertEqual(gate_result(case, commands, bad)["status"], "FAIL") - for record in ( - dict(exit_code=124, cleanup=True), - dict(exit_code=0, cleanup=False), - ): - self.assertEqual(gate_result(case, [record], events)["status"], "FAIL") - - def test_native_summary_cannot_hide_absent_events_changed_source_or_cleanup(self): - case = next(c for c in definitions() if c["case_id"] == "VMESS-M-IDENTITY") - test = case["peer_config"]["test"] - record = dict( - case_id=case["case_id"], - status="PASS", - exit_code=0, - command_cleanup=True, - cleanup=True, - peer_kind="M", - command=[ - "cargo", - "test", - "--locked", - "--all-features", - "--test", - "vmess_native", - test, - "--", - "--ignored", - "--exact", - "--nocapture", - ], - ) - report = dict(cases=[record], source_unchanged=True, cleanup=True) - with tempfile.TemporaryDirectory() as root: - path = Path(root) - self.assertEqual(native_results([case], report, path)[0]["status"], "FAIL") - (path / (case["case_id"] + "-events.jsonl")).write_text( - "".join(json.dumps(e) + "\n" for e in pair("VMESS-WIRE", test)) - ) - self.assertEqual(native_results([case], report, path)[0]["status"], "PASS") - for key in ("cleanup", "source_unchanged"): - self.assertEqual( - native_results([case], report | {key: False}, path)[0]["status"], - "FAIL", - ) - for change in ( - dict(exit_code=124), - dict(command_cleanup=False), - dict(cleanup=False), - dict(command=[]), - dict(peer_kind="V2"), - ): - self.assertEqual( - native_results([case], report | {"cases": [record | change]}, path)[ - 0 - ]["status"], - "FAIL", - ) - with self.assertRaises(ValueError): - native_results([case], report | {"cases": [record, record]}, path) - - def test_public_base_requires_every_family_codec_and_body_option(self): - test = "public_base" - events = pair("VMESS-PUBLIC", test) - self.assertFalse(events_pass(events, test)) - for name in ("tcp_10mib_both_directions", "udp_each_codec_and_family"): - events += [e for _ in range(3) for e in pair("VMESS-BASE", name)] - self.assertTrue(events_pass(events, test)) - self.assertFalse(events_pass(events[:-2], test)) - self.assertFalse(events_pass(events + events[-2:], test)) - body = pair("VMESS-PUBLIC", "public_body_options") + [ - e - for _ in range(14) - for e in pair("VMESS-BODY", "config_controls_aead_body") - ] - self.assertTrue(events_pass(body, "public_body_options")) - self.assertFalse(events_pass(body[:-2], "public_body_options")) - - def test_owned_resources_need_twenty_idle_stop_and_quiet_cycles(self): - snapshot = dict( - counts=[ - dict(kind=kind, current=0, peak=1) for kind in sorted(RESOURCE_KINDS) - ] - ) - test = "runtime::owned_resources" - events = pair("VMESS-PUBLIC", test) - for _ in range(20): - cycle = pair("VMESS-OWNED", "stop_and_remain_quiet") - cycle[-1].update( - seconds=5, - resources=snapshot, - checkpoints=[ - dict(phase=phase, resources=snapshot) - for phase in ("baseline", "after-stop", "quiet") - ], - ) - events += cycle - self.assertTrue(events_pass(events, test)) - for change in ( - {"seconds": 4.9}, - {"checkpoints": []}, - {"resources": {}}, - {"status": "FAIL"}, - ): - self.assertFalse(events_pass(events[:-1] + [events[-1] | change], test)) - self.assertFalse(events_pass(events[:-2], test)) - busy = copy.deepcopy(events) - busy[-1]["resources"]["counts"][0]["current"] = 1 - self.assertFalse(events_pass(busy, test)) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_protocol_vmess_udp_ab.py b/scripts/tests/test_protocol_vmess_udp_ab.py deleted file mode 100644 index acb1a12..0000000 --- a/scripts/tests/test_protocol_vmess_udp_ab.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Small offline checks for diagnostic evidence extraction, not interop.""" - -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.protocol_vmess_udp_ab import options, warning_summary - - -class UdpDiagnosticTests(unittest.TestCase): - def test_matched_body_options(self): - matrix = options(["raw", "xudp", "packetaddr"]) - self.assertEqual(len(matrix), 39) - self.assertEqual(sum(item["cipher"] == "none" for item in matrix), 3) - - def test_loopback_ports_include_domain_and_exclude_timestamp(self): - with tempfile.TemporaryDirectory() as root: - log = Path(root) / "peer.log" - log.write_text( - 'time="2026-09-23T10:27:13+08:00" level=warning ' - 'msg="[UDP] 127.0.0.1:51243 --> vcore-fixture.test:63254 ' - 'error: reject loopback connection to: vcore-fixture.test:63254"\n' - 'time="2026-09-23T10:27:14+08:00" level=warning ' - 'msg="[UDP] 127.0.0.1:51386 --> [::1]:64969 ' - 'error: reject loopback connection to: [::1]:64969"\n' - ) - self.assertEqual( - warning_summary(log)["loopback_rejections"], - [{"ports": [51243, 63254, 63254]}, {"ports": [51386, 64969, 64969]}], - ) diff --git a/scripts/tests/test_protocol_xhttp.py b/scripts/tests/test_protocol_xhttp.py deleted file mode 100644 index 473e805..0000000 --- a/scripts/tests/test_protocol_xhttp.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Fail-closed XHTTP coverage and fixture checks; no host listeners.""" - -import copy -import unittest - -from vcore_scripts.protocol_xhttp_acceptance import ( - FIELD_IDS, - NATIVE, - OBSERVATIONS, - definitions, - gate_result, - native_results, -) -from vcore_scripts.protocol_xhttp_fields import events_pass, public_events_pass - - -def pair(suite, assertion): - return [ - dict(schema_version=1, suite=suite, assertion=assertion, status=s) - for s in ("BEGIN", "PASS") - ] - - -class XhttpAcceptanceTest(unittest.TestCase): - def test_frozen_catalog_has_all_rows_modes_and_mux_wires(self): - cases = definitions() - self.assertEqual(len(FIELD_IDS), 57) - self.assertEqual(len(cases), len({c["case_id"] for c in cases})) - self.assertEqual(FIELD_IDS, set().union(*(set(c["row_ids"]) for c in cases))) - for version in ("h1", "h2", "h1c", "h2c", "h1r", "h2r", "h3"): - for mode in ("stream-one", "stream-up", "packet-up"): - self.assertTrue( - any( - v == f"{version}-{mode}-headers" and test == "public_base" - for v, test in NATIVE.values() - ) - ) - for protocol in ("h2mux", "smux", "yamux"): - self.assertTrue( - any( - v.startswith(f"{version}-mux-{protocol}-") - and test == "public_base" - for v, test in NATIVE.values() - ) - ) - for version in ("h1r", "h2r"): - for field in ("public-key", "short-id"): - self.assertIn( - ( - f"{version}-reject-download-{field}", - "security::native_xhttp_security", - ), - NATIVE.values(), - ) - - def test_unknown_duplicate_wrong_suite_and_partial_events_fail(self): - observed = pair("XHTTP-XHTTP", "native_xhttp_request_fields") - self.assertTrue(events_pass(observed, "native_xhttp_request_fields")) - for bad in ( - [], - observed[:1], - observed * 2, - observed + pair("extra", "unknown"), - ): - self.assertFalse(events_pass(bad, "native_xhttp_request_fields")) - public = pair("XHTTP-PUBLIC", "runtime::public_graph") - self.assertTrue(public_events_pass(public, "runtime::public_graph")) - self.assertFalse( - public_events_pass( - public + pair("XHTTP-BASE", "unknown"), "runtime::public_graph" - ) - ) - bad = copy.deepcopy(public) - bad[1]["schema_version"] = 2 - self.assertFalse(public_events_pass(bad, "runtime::public_graph")) - - def test_unit_gate_requires_every_frozen_assertion_and_successful_command(self): - case = next(c for c in definitions() if c["case_id"] == "XHTTP-CFG") - events = sum( - (pair("XHTTP-UNIT", name) for name in OBSERVATIONS["XHTTP-CFG"]), [] - ) - records = [dict(exit_code=0, cleanup=True)] - self.assertEqual(gate_result(case, records, events)["status"], "PASS") - for bad in ([], events[:-1], events + events[:2]): - self.assertEqual(gate_result(case, records, bad)["status"], "FAIL") - self.assertEqual( - gate_result(case, [dict(exit_code=1, cleanup=True)], events)["status"], - "FAIL", - ) - - def test_native_missing_and_duplicate_records_are_not_success(self): - case = next(c for c in definitions() if c["case_id"] in NATIVE) - for records in ([], [dict(case_id=case["case_id"])] * 2): - with self.assertRaises(ValueError): - native_results([case], dict(cases=records), None) - self.assertEqual( - native_results([case], dict(cases=[]), None, allow_partial=True), [] - ) - for records in ([dict(case_id="unknown")], [dict(case_id=case["case_id"])] * 2): - with self.assertRaises(ValueError): - native_results([case], dict(cases=records), None, allow_partial=True) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_scripts.py b/scripts/tests/test_scripts.py index 8c99ef4..27a88ed 100644 --- a/scripts/tests/test_scripts.py +++ b/scripts/tests/test_scripts.py @@ -1,36 +1,21 @@ from __future__ import annotations -import copy +import contextlib import hashlib +import io import json -import subprocess -import sys +import os +import shlex import tempfile import tomllib import unittest import xml.etree.ElementTree as ET -from contextlib import ExitStack from pathlib import Path from types import SimpleNamespace -from unittest.mock import MagicMock, call, patch +from unittest.mock import MagicMock, patch -from vcore_scripts import builds, cli, mihomo +from vcore_scripts import builds, cli from vcore_scripts.builds import EXPECTED_IDENTITY, _android_target, _require_identity -from vcore_scripts.checks import ( - BORING_GIT_SOURCE, - BORING_REVISION, - CRATES_IO_SOURCES, - _shadowsocks_aws_lc_errors, - _tls_dependency_errors, -) -from vcore_scripts.mihomo_container import ( - NETWORK, - ContainerPeer, - ContainerPeers, - host_ipv6, -) -from vcore_scripts.mihomo_isolation import exclusive_run -from vcore_scripts.tun2socks import derive_xray_config def _windows_pe(machine: int) -> bytes: @@ -51,180 +36,73 @@ class ScriptTest(unittest.TestCase): features, set(manifest["features"]["default"]) | {"ffi", "tun"} ) self.assertNotIn("interop-test", features) + self.assertNotIn("benchmark-geodata-http", features) - def test_download_cli_dispatches_host_and_container_target(self): - with patch("vcore_scripts.cli.download_mihomo") as fetch: - self.assertEqual(cli.main(["download", "mihomo"]), 0) - self.assertEqual( - cli.main(["download", "mihomo", "--target", "linux-arm64"]), 0 - ) - self.assertEqual(fetch.call_args_list, [call(None), call("linux-arm64")]) - - def test_legacy_demo_requires_explicit_config_and_source(self): - with patch("vcore_scripts.cli.run_demo") as run: - self.assertEqual( - cli.main( - [ - "demo", - "windows-tun2socks", - "fixture.json", - "--xray-source", - "fixture-xray", - ] - ), - 0, - ) - run.assert_called_once_with( - Path("fixture.json"), xray_source=Path("fixture-xray") - ) - - def test_container_bridge_ipv6_does_not_select_lan_or_utun(self): - interfaces = """en0: flags=1 - inet 192.168.1.2 netmask 0xffffff00 - inet6 fd01::123 prefixlen 64 -utun5: flags=1 - inet 198.18.0.1 netmask 0xffffff00 - inet6 fd02::1 prefixlen 64 -bridge100: flags=1 - inet 192.168.128.1 netmask 0xffffff00 - inet6 fe80::1%bridge100 prefixlen 64 - inet6 fd03::11 prefixlen 64 autoconf tentative - inet6 fd03::12 prefixlen 64 duplicated - inet6 fd03::13 prefixlen 64 deprecated - inet6 fd03::22 prefixlen 64 -""" + def test_platform_cargo_build_rejects_test_features_before_spawn(self): + for features in ( + "ffi,benchmark-geodata-http", + "ffi interop-test", + "ffi vcore/benchmark-geodata-http", + ): + with self.subTest(features=features), patch.object(builds, "_run") as run: + with self.assertRaisesRegex(RuntimeError, "test-only"): + builds._cargo_build("fixture-target", ["--release"], features, {}) + run.assert_not_called() self.assertEqual( - host_ipv6(interfaces, "192.168.128.1", "fd03::/64"), "fd03::22" + builds._production_features(builds.DEFAULT_FEATURES), + builds.DEFAULT_FEATURES, ) - self.assertIsNone(host_ipv6(interfaces, "192.168.128.1", "fd01::/64")) - self.assertIsNone(host_ipv6(interfaces, "192.168.129.1", "fd03::/64")) - def test_container_config_only_exposes_owned_vm_listeners(self): - peers = object.__new__(ContainerPeers) - peers.host = "192.168.128.1" - peers.addresses = ["192.168.128.2", "127.0.0.1", "127.0.0.1", "192.168.128.3"] - peers.peers = {0: object(), 3: object()} - native = {"allow-lan": False, "bind-address": "127.0.0.1"} - peers.configure(1, native) - self.assertFalse(native["allow-lan"]) - self.assertEqual(native["bind-address"], "127.0.0.1") - vm = { - "external-controller": "127.0.0.1:9900", - "listeners": [ - { - "listen": "127.0.0.1", - "certificate": "/fixture/fixture.crt", - } - ], - } - peers.configure(0, vm) - self.assertEqual(vm["external-controller"], "0.0.0.0:9900") - self.assertEqual(vm["listeners"][0]["certificate"], "/data/fixture/fixture.crt") - self.assertEqual(vm["hosts"]["vcore-fixture.test"], peers.host) - self.assertEqual(vm["hosts"]["vcore-peer.test"], peers.addresses[3]) - - def test_container_cleanup_is_scoped_and_handles_failed_launch(self): - peer = ContainerPeer("vcore-mihomo-test-0") - with patch("vcore_scripts.mihomo_container.command", return_value="[]") as run: - peer.stop() - self.assertEqual(run.call_count, 1) - owned = { - "id": peer.name, - "configuration": {"labels": {"purpose": NETWORK}}, - "status": {"state": "running"}, - } - with patch( - "vcore_scripts.mihomo_container.command", - side_effect=[json.dumps([owned]), "", ""], - ) as run: - peer.stop() - self.assertEqual( - run.call_args_list[1].args, ("stop", "--time", "5", peer.name) - ) - self.assertEqual( - run.call_args_list[2].args, ("delete", "--force", peer.name) - ) - owned["configuration"]["labels"] = {} - with patch( - "vcore_scripts.mihomo_container.command", return_value=json.dumps([owned]) - ) as run: - with self.assertRaisesRegex(RuntimeError, "ownership"): - peer.stop() - self.assertEqual(run.call_count, 1) - - def test_mihomo_reservations_cover_tcp_and_udp_and_release_together(self): - with ExitStack() as stack: - port, reservation = mihomo.reserve_port(stack) - for family, host in [ - (mihomo.socket.AF_INET, "127.0.0.1"), - (mihomo.socket.AF_INET6, "::1"), - ]: - for kind in [mihomo.socket.SOCK_STREAM, mihomo.socket.SOCK_DGRAM]: - with ( - mihomo.socket.socket(family, kind) as candidate, - self.assertRaises(OSError), - ): - candidate.bind((host, port)) - reservation.release_ipv4() - for kind in [mihomo.socket.SOCK_STREAM, mihomo.socket.SOCK_DGRAM]: - with mihomo.socket.socket(type=kind) as candidate: - candidate.bind(("127.0.0.1", port)) - with ( - mihomo.socket.socket(mihomo.socket.AF_INET6, kind) as guard, - self.assertRaises(OSError), - ): - guard.bind(("::1", port)) - reservation.close() - for family, host in [ - (mihomo.socket.AF_INET, "127.0.0.1"), - (mihomo.socket.AF_INET6, "::1"), - ]: - for kind in [mihomo.socket.SOCK_STREAM, mihomo.socket.SOCK_DGRAM]: - with mihomo.socket.socket(family, kind) as candidate: - candidate.bind((host, port)) - - def test_mihomo_lock_rejects_other_process_and_releases_after_failure(self): - child = """ -import sys -from pathlib import Path -from vcore_scripts.mihomo_isolation import exclusive_run -try: - with exclusive_run(Path(sys.argv[1])): - pass -except RuntimeError as error: - print(error) - sys.exit(23) -""" + def test_cargo_target_directory_uses_cargo_environment_and_checkout_cwd(self): with tempfile.TemporaryDirectory() as directory: - lock = Path(directory) / "fixture.lock" - command = [sys.executable, "-c", child, str(lock)] - with ( - self.assertRaisesRegex(ValueError, "fixture failure"), - exclusive_run(lock), + root = Path(directory) + for environment, expected in ( + ({}, root / "target"), + ({"CARGO_TARGET_DIR": ""}, root / "target"), + ({"CARGO_TARGET_DIR": str(root / "cache")}, root / "cache"), + ({"CARGO_TARGET_DIR": "cache"}, root / "cache"), ): - blocked = subprocess.run( - command, capture_output=True, text=True, timeout=5 - ) - self.assertEqual(blocked.returncode, 23, blocked.stderr) - self.assertIn("another local mihomo harness", blocked.stdout) - raise ValueError("fixture failure") - released = subprocess.run( - command, capture_output=True, text=True, timeout=5 - ) - self.assertEqual(released.returncode, 0, released.stderr) + with ( + self.subTest(environment=environment), + patch.object(builds, "CORE_DIR", root), + patch.dict(builds.os.environ, environment, clear=True), + ): + self.assertEqual(builds._cargo_target_dir(), expected) + self.assertEqual(builds._cargo_target_dir(environment), expected) - def test_mihomo_peer_cleanup_waits_and_escalates_only_after_timeout(self): - peer = MagicMock() - peer.poll.return_value = None - mihomo._stop_peer(peer) - peer.terminate.assert_called_once_with() - peer.wait.assert_called_once_with(timeout=5) - peer.kill.assert_not_called() - peer.reset_mock() - peer.wait.side_effect = [mihomo.subprocess.TimeoutExpired("mihomo", 5), 0] - mihomo._stop_peer(peer) - peer.kill.assert_called_once_with() - self.assertEqual(peer.wait.call_count, 2) + def test_cli_only_dispatches_platform_builds(self): + for platform_name in ("apple", "android", "windows"): + with ( + self.subTest(platform=platform_name), + patch(f"vcore_scripts.cli.build_{platform_name}") as build, + ): + self.assertEqual(cli.main(["build", platform_name]), 0) + build.assert_called_once_with() + with ( + self.subTest(platform=platform_name, delivery=True), + patch("vcore_scripts.platform_delivery.build_delivery") as delivery, + ): + self.assertEqual(cli.main(["build", platform_name, "--delivery"]), 0) + delivery.assert_called_once_with(platform_name) + + def test_cli_rejects_removed_validation_and_demo_commands_before_building(self): + for arguments in ( + ["check", "core"], + ["check", "protocol-interop"], + ["check", "platform-artifacts"], + ["check", "platform-abi"], + ["demo", "windows-tun2socks"], + ["build", "linux"], + ): + with ( + self.subTest(arguments=arguments), + contextlib.redirect_stderr(io.StringIO()), + patch("subprocess.Popen") as spawn, + ): + with self.assertRaises(SystemExit) as error: + cli.main(arguments) + self.assertEqual(error.exception.code, 2) + spawn.assert_not_called() def test_cli_dispatches_windows_build_without_architecture(self): with patch("vcore_scripts.cli.build_windows") as build: @@ -285,55 +163,113 @@ except RuntimeError as error: _android_target("mips-linux-android", "24") def test_android_build_packages_the_matching_ndk_cpp_runtime(self): - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - toolchain = root / "ndk/toolchain" - (toolchain / "bin").mkdir(parents=True) - (toolchain / "bin/llvm-ar").touch() - targets = ("aarch64-linux-android", "x86_64-linux-android") - for target in targets: - abi, clang, _ = _android_target(target, "24") - (toolchain / "bin" / clang).touch() - (toolchain / "bin" / (clang + "++")).touch() - runtime = toolchain / "sysroot/usr/lib" / target / "libc++_shared.so" - runtime.parent.mkdir(parents=True) - runtime.write_bytes(abi.encode()) - artifact = root / "target" / target / "release/libvcore.so" - artifact.parent.mkdir(parents=True) - artifact.write_bytes(EXPECTED_IDENTITY) + for configured, api, bindgen_overrides, expected_extra in ( + (False, "24", {}, []), + (True, "28", {"BINDGEN_EXTRA_CLANG_ARGS": "-DGLOBAL=1"}, ["-DGLOBAL=1"]), + ( + False, + "28", + { + "BINDGEN_EXTRA_CLANG_ARGS": "-DGLOBAL=1", + "BINDGEN_EXTRA_CLANG_ARGS_aarch64_linux_android": "-DARM64=1", + }, + ["-DARM64=1"], + ), + ( + True, + "24", + { + "BINDGEN_EXTRA_CLANG_ARGS": "-DGLOBAL=1", + "BINDGEN_EXTRA_CLANG_ARGS_aarch64_linux_android": "-DLOWER=1", + "BINDGEN_EXTRA_CLANG_ARGS_aarch64-linux-android": ( + '-I"fixture include"' + ), + }, + ["-Ifixture include"], + ), + ( + False, + "24", + { + "BINDGEN_EXTRA_CLANG_ARGS": "-DGLOBAL=1", + "BINDGEN_EXTRA_CLANG_ARGS_aarch64-linux-android": "", + }, + [], + ), + ): with ( - patch.dict( - builds.os.environ, - {"ANDROID_NDK_HOME": str(root / "ndk")}, - clear=True, - ), - patch.object(builds, "CORE_DIR", root), - patch.object(builds, "_android_toolchain", return_value=toolchain), - patch.object(builds, "_require_targets"), - patch.object(builds, "_cargo_build") as cargo, + self.subTest(configured=configured, api=api, bindgen=bindgen_overrides), + tempfile.TemporaryDirectory() as directory, ): - builds.build_android() - self.assertEqual(cargo.call_count, 2) - for invocation, target in zip(cargo.call_args_list, targets, strict=True): - abi, clang, _ = _android_target(target, "24") - env = invocation.args[3] - self.assertEqual(env["VCORE_CMAKE_ANDROID_ABI"], abi) - self.assertEqual(env["VCORE_CMAKE_ANDROID_API"], "24") - self.assertEqual( - env[f"CMAKE_TOOLCHAIN_FILE_{target.replace('-', '_')}"], - str(root / "scripts/cmake/android.toolchain.cmake"), - ) - self.assertEqual( - env[f"CXX_{target.replace('-', '_')}"], - str(toolchain / "bin" / (clang + "++")), - ) - output = root / "dist/android" / abi - self.assertEqual( - (output / "libvcore.so").read_bytes(), EXPECTED_IDENTITY - ) - self.assertEqual( - (output / "libc++_shared.so").read_bytes(), abi.encode() - ) + root = Path(directory) + target_dir = root / ("cache" if configured else "target") + toolchain = root / "ndk/toolchain" + (toolchain / "bin").mkdir(parents=True) + (toolchain / "bin/llvm-ar").touch() + targets = ("aarch64-linux-android", "x86_64-linux-android") + for target in targets: + abi, clang, _ = _android_target(target, api) + (toolchain / "bin" / clang).touch() + (toolchain / "bin" / (clang + "++")).touch() + runtime = ( + toolchain / "sysroot/usr/lib" / target / "libc++_shared.so" + ) + runtime.parent.mkdir(parents=True) + runtime.write_bytes(abi.encode()) + artifact = target_dir / target / "release/libvcore.so" + artifact.parent.mkdir(parents=True) + artifact.write_bytes(EXPECTED_IDENTITY) + with ( + patch.dict( + builds.os.environ, + { + "ANDROID_NDK_HOME": str(root / "ndk"), + "VCORE_ANDROID_API": api, + } + | bindgen_overrides + | ({"CARGO_TARGET_DIR": str(target_dir)} if configured else {}), + clear=True, + ), + patch.object(builds, "CORE_DIR", root), + patch.object(builds, "_android_toolchain", return_value=toolchain), + patch.object(builds, "_require_targets"), + patch.object(builds, "_cargo_build") as cargo, + ): + builds.build_android() + self.assertEqual(cargo.call_count, 2) + for invocation, target in zip( + cargo.call_args_list, targets, strict=True + ): + abi, clang, _ = _android_target(target, api) + env = invocation.args[3] + self.assertEqual(env["VCORE_CMAKE_ANDROID_ABI"], abi) + self.assertEqual(env["VCORE_CMAKE_ANDROID_API"], api) + extra = ( + expected_extra + if target == "aarch64-linux-android" + else shlex.split( + bindgen_overrides.get("BINDGEN_EXTRA_CLANG_ARGS", "") + ) + ) + self.assertEqual( + shlex.split(env[f"BINDGEN_EXTRA_CLANG_ARGS_{target}"]), + [f"--target={clang.removesuffix('-clang')}", *extra], + ) + self.assertEqual( + env[f"CMAKE_TOOLCHAIN_FILE_{target.replace('-', '_')}"], + str(root / "scripts/cmake/android.toolchain.cmake"), + ) + self.assertEqual( + env[f"CXX_{target.replace('-', '_')}"], + str(toolchain / "bin" / (clang + "++")), + ) + output = root / "dist/android" / abi + self.assertEqual( + (output / "libvcore.so").read_bytes(), EXPECTED_IDENTITY + ) + self.assertEqual( + (output / "libc++_shared.so").read_bytes(), abi.encode() + ) def test_artifact_identity_check_reads_binary_directly(self): with tempfile.TemporaryDirectory() as directory: @@ -345,357 +281,98 @@ except RuntimeError as error: _require_identity(artifact, "test") def test_windows_release_build_uses_production_features_and_checks_identity(self): - with tempfile.TemporaryDirectory() as directory: - root = Path(directory) - release = root / "target/aarch64-pc-windows-msvc/release" - release.mkdir(parents=True) - artifacts = ( - "vcore.dll", - "vcore-windows-vpn-host.exe", - "vcore-windows-session-host.exe", - ) - for name in artifacts: - (release / name).write_bytes(_windows_pe(0xAA64)) - + for configured in (False, True): with ( - patch.object(builds, "CORE_DIR", root), - patch.object(builds, "os", SimpleNamespace(name="nt")), - patch.object(builds, "_windows_architecture", return_value="arm64"), - patch.object(builds, "_windows_msvc_environment", return_value={}), - patch.object(builds, "_run") as run, + self.subTest(configured=configured), + tempfile.TemporaryDirectory() as directory, ): - builds.build_windows() - self.assertEqual( - run.call_args_list[1].args[0], - [ - "cargo", - "build", - "--locked", - "--release", - "--target", - "aarch64-pc-windows-msvc", - "--no-default-features", - "--features", - builds.DEFAULT_FEATURES, - "--lib", - "--bins", - ], - ) - manifest = json.loads( - ( - root / "dist/windows/arm64/vcore-windows-artifacts.json" - ).read_text() - ) - expected_digest = hashlib.sha256(_windows_pe(0xAA64)).hexdigest() - self.assertEqual( - manifest, - { - "architecture": "arm64", - "artifacts": { - "vcore-windows-session-host.exe": expected_digest, - "vcore-windows-vpn-host.exe": expected_digest, - "vcore.dll": expected_digest, - }, - "buildIdentity": EXPECTED_IDENTITY.decode("ascii"), - "formatVersion": 1, - "windowsPackageIntegrationRevision": 3, - }, + root = Path(directory) + target_dir = root / ("cache" if configured else "target") + release = target_dir / "aarch64-pc-windows-msvc/release" + release.mkdir(parents=True) + artifacts = ( + "vcore.dll", + "vcore-windows-vpn-host.exe", + "vcore-windows-session-host.exe", ) + for name in artifacts: + (release / name).write_bytes(_windows_pe(0xAA64)) - provider = release / "vcore-windows-vpn-host.exe" - provider.write_bytes(_windows_pe(0x8664)) - with self.assertRaisesRegex(RuntimeError, "wrong architecture"): + with ( + patch.object(builds, "CORE_DIR", root), + patch.object( + builds, + "os", + SimpleNamespace(name="nt", environ={}, fspath=os.fspath), + ), + patch.object(builds, "_windows_architecture", return_value="arm64"), + patch.object( + builds, + "_windows_msvc_environment", + return_value={"CARGO_TARGET_DIR": str(target_dir)} + if configured + else {}, + ), + patch.object(builds, "_run") as run, + ): builds.build_windows() - self.assertFalse( - (root / "dist/windows/arm64/vcore-windows-artifacts.json").exists() - ) - - provider.write_bytes(_windows_pe(0xAA64)) - dll = bytearray(_windows_pe(0xAA64)) - dll[0x100 : 0x100 + len(EXPECTED_IDENTITY)] = bytes( - len(EXPECTED_IDENTITY) - ) - (release / "vcore.dll").write_bytes(dll) - with self.assertRaisesRegex(RuntimeError, "incompatible Rust identity"): - builds.build_windows() - - def test_tls_metadata_accepts_the_locked_graph(self): - registry = next(iter(CRATES_IO_SOURCES)) - metadata = { - "packages": [ - { - "id": "rustls-id", - "name": "rustls", - "version": "0.23.45", - "source": registry, - }, - { - "id": "tokio-rustls-id", - "name": "tokio-rustls", - "version": "0.26.5", - "source": registry, - }, - { - "id": "ring-id", - "name": "ring", - "version": "0.17.14", - "source": registry, - }, - *[ - dict(id=name, name=name, version="5.2.0", source=BORING_GIT_SOURCE) - for name in ("boring", "boring-sys", "tokio-boring") - ], - dict(id="hpke", name="hpke", version="0.14.1", source=registry), - ], - "resolve": { - "nodes": [ - { - "id": "rustls-id", - "features": ["ring", "std", "tls12"], - "deps": [], - }, - { - "id": "boring", - "features": [ - "reality", - "client-fingerprint", - "shadow-tls-v3", - "jls", + self.assertEqual( + run.call_args_list[1].args[0], + [ + "cargo", + "build", + "--locked", + "--release", + "--target", + "aarch64-pc-windows-msvc", + "--no-default-features", + "--features", + builds.DEFAULT_FEATURES, + "--lib", + "--bins", ], - "deps": [{"pkg": "boring-sys"}], - }, - { - "id": "boring-sys", - "features": ["reality", "shadow-tls-v3", "jls"], - }, - { - "id": "tokio-boring", - "features": [], - "deps": [{"pkg": "boring"}, {"pkg": "boring-sys"}], - }, - { - "id": "hpke", - "features": ["alloc", "aes", "chacha", "x25519", "hkdfsha2"], - }, - ] - }, - } - self.assertEqual(_tls_dependency_errors(metadata), []) - - for invalid_source in (None, BORING_GIT_SOURCE): - invalid = copy.deepcopy(metadata) - invalid["packages"][-1]["source"] = invalid_source - self.assertTrue(_tls_dependency_errors(invalid)) - for feature in ("alloc", "aes", "chacha", "x25519"): - invalid = copy.deepcopy(metadata) - invalid["resolve"]["nodes"][-1]["features"].remove(feature) - self.assertTrue(_tls_dependency_errors(invalid)) - - for index, old_version in [(0, "0.23.43"), (1, "0.26.4"), (3, "5.1.0")]: - with self.subTest(outdated_version=old_version): - outdated = copy.deepcopy(metadata) - outdated["packages"][index]["version"] = old_version - self.assertTrue(_tls_dependency_errors(outdated)) - - for source in [ - None, - registry, - BORING_GIT_SOURCE.rsplit("#", 1)[0] + "#" + "f" * 40, - BORING_GIT_SOURCE.replace("?branch=release", "?branch=main"), - BORING_GIT_SOURCE.replace("?branch=release", f"?rev={BORING_REVISION}"), - BORING_GIT_SOURCE.replace("OneXray/boring", "example/boring"), - ]: - for index in (3, 4, 5): - with self.subTest(boring_source=source, package=index): - invalid = copy.deepcopy(metadata) - invalid["packages"][index]["source"] = source - self.assertTrue(_tls_dependency_errors(invalid)) - - for required in ["reality", "client-fingerprint", "shadow-tls-v3", "jls"]: - with self.subTest(boring_feature=required): - invalid = copy.deepcopy(metadata) - invalid["resolve"]["nodes"][1]["features"].remove(required) - self.assertTrue(_tls_dependency_errors(invalid)) - - for missing in ["edge", "node", "package"]: - with self.subTest(boring_missing=missing): - invalid = copy.deepcopy(metadata) - if missing == "edge": - invalid["resolve"]["nodes"][1]["deps"] = [] - elif missing == "node": - invalid["resolve"]["nodes"].pop() - else: - invalid["packages"].pop() - self.assertTrue(_tls_dependency_errors(invalid)) - - for forbidden in ["reality", "aws_lc_rs", "fips"]: - invalid = copy.deepcopy(metadata) - invalid["resolve"]["nodes"][0]["features"].append(forbidden) - self.assertTrue(_tls_dependency_errors(invalid)) - - for forbidden in ("fips", "restls"): - for index in (1, 2, 3): - with self.subTest(native_feature=forbidden, node=index): - invalid = copy.deepcopy(metadata) - invalid["resolve"]["nodes"][index]["features"].append(forbidden) - self.assertTrue(_tls_dependency_errors(invalid)) - - for source in CRATES_IO_SOURCES: - with self.subTest(rustls_registry=source): - official = copy.deepcopy(metadata) - official["packages"][0]["source"] = source - self.assertEqual(_tls_dependency_errors(official), []) - - for source in ( - None, - "git+https://example.invalid/rustls?branch=custom#" + "a" * 40, - "git+https://github.com/rustls/rustls#" + "a" * 40, - "registry+https://example.invalid/index", - ): - with self.subTest(rustls_source=source): - invalid = copy.deepcopy(metadata) - invalid["packages"][0]["source"] = source - self.assertTrue( - any( - "rustls must come from crates.io" in error - for error in _tls_dependency_errors(invalid) ) - ) - - duplicate = copy.deepcopy(metadata) - duplicate["packages"].append( - dict( - id="second-rustls", - name="rustls", - version="0.23.45", - source="git+https://github.com/rustls/rustls#" + "a" * 40, - ) - ) - self.assertTrue(_tls_dependency_errors(duplicate)) - - metadata["packages"].append( - { - "id": "aws-id", - "name": "aws-lc-rs", - "version": "1.0.0", - "source": registry, - } - ) - self.assertTrue( - any( - "AWS-LC package is forbidden" in error - for error in _tls_dependency_errors(metadata) - ) - ) - - metadata["packages"][0]["source"] = None - self.assertTrue( - any( - "rustls must come from crates.io" in error - for error in _tls_dependency_errors(metadata) - ) - ) - - def test_aws_lc_exception_is_restricted_to_official_shadowsocks_chain(self): - registry = next(iter(CRATES_IO_SOURCES)) - names = ["shadowsocks", "shadowsocks-crypto", "aws-lc-rs", "aws-lc-sys"] - metadata = { - "packages": [ - {"id": name, "name": name, "version": version, "source": source} - for name, version, source in zip( - names, - ["1.25.0", "0.8.0", "1.18.1", "0.45.0"], - [registry] * 4, - strict=True, - ) - ], - "resolve": { - "nodes": [ - { - "id": name, - "features": features, - "deps": [{"pkg": names[i + 1]}] if i < 3 else [], - } - for i, (name, features) in enumerate( - zip( - names, - [["aead-cipher-2022"], ["v2", "aws-lc"], [], []], - strict=True, - ) + manifest = json.loads( + ( + root / "dist/windows/arm64/vcore-windows-artifacts.json" + ).read_text() + ) + expected_digest = hashlib.sha256(_windows_pe(0xAA64)).hexdigest() + self.assertEqual( + manifest, + { + "architecture": "arm64", + "artifacts": { + "vcore-windows-session-host.exe": expected_digest, + "vcore-windows-vpn-host.exe": expected_digest, + "vcore.dll": expected_digest, + }, + "buildIdentity": EXPECTED_IDENTITY.decode("ascii"), + "formatVersion": 1, + "windowsPackageIntegrationRevision": 3, + }, ) - ] - }, - } - self.assertEqual(_shadowsocks_aws_lc_errors(metadata), []) - for source in CRATES_IO_SOURCES: - with self.subTest(shadowsocks_registry=source): - official = copy.deepcopy(metadata) - official["packages"][0]["source"] = source - self.assertEqual(_shadowsocks_aws_lc_errors(official), []) - for source in ( - "git+https://github.com/shadowsocks/shadowsocks-rust.git?rev=" - "ab388c7466d21f979430e33cc9ef10e22fb05955#" - "ab388c7466d21f979430e33cc9ef10e22fb05955", - "registry+https://example.invalid/index", - ): - with self.subTest(shadowsocks_source=source): - invalid = copy.deepcopy(metadata) - invalid["packages"][0]["source"] = source - self.assertTrue(_shadowsocks_aws_lc_errors(invalid)) - for index, feature in [(0, "aead-cipher-2022-extra"), (1, "v2-extra")]: - invalid = copy.deepcopy(metadata) - invalid["resolve"]["nodes"][index]["features"].append(feature) - self.assertTrue(_shadowsocks_aws_lc_errors(invalid)) - for target in names[1:]: - with self.subTest(extra_consumer=target): - invalid = copy.deepcopy(metadata) - invalid["resolve"]["nodes"].append( - {"id": "another-consumer", "deps": [{"pkg": target}]} - ) - self.assertTrue(_shadowsocks_aws_lc_errors(invalid)) - for index in range(4): - with self.subTest(unofficial_source=names[index]): - invalid = copy.deepcopy(metadata) - invalid["packages"][index]["source"] = None - self.assertTrue(_shadowsocks_aws_lc_errors(invalid)) - with self.subTest(missing_node=names[index]): - invalid = copy.deepcopy(metadata) - del invalid["resolve"]["nodes"][index] - self.assertTrue(_shadowsocks_aws_lc_errors(invalid)) - invalid = copy.deepcopy(metadata) - invalid["packages"].append({"id": "fips", "name": "aws-lc-fips-sys"}) - self.assertTrue(_shadowsocks_aws_lc_errors(invalid)) - def test_tun2socks_config_moves_direct_sockopt(self): - source = { - "inbounds": [ - { - "tag": "proxy", - "protocol": "socks", - "listen": "0.0.0.0", - "port": 1080, - "settings": {"udp": False}, - } - ], - "outbounds": [ - {"tag": "proxy", "protocol": "vless", "settings": {}}, - { - "tag": "direct", - "protocol": "freedom", - "settings": {}, - "sockopt": {"interface": "Ethernet"}, - }, - ], - } - with tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "source.json" - path.write_text(json.dumps(source), encoding="utf-8") - config, inbound_tag, direct_tag = derive_xray_config(path, Path(directory)) - self.assertEqual((inbound_tag, direct_tag), ("proxy", "direct")) - direct = next(item for item in config["outbounds"] if item["tag"] == "direct") - self.assertNotIn("sockopt", direct) - self.assertEqual(direct["streamSettings"]["sockopt"], {"interface": "Ethernet"}) - self.assertTrue(config["inbounds"][0]["settings"]["udp"]) + provider = release / "vcore-windows-vpn-host.exe" + provider.write_bytes(_windows_pe(0x8664)) + with self.assertRaisesRegex(RuntimeError, "wrong architecture"): + builds.build_windows() + self.assertFalse( + ( + root / "dist/windows/arm64/vcore-windows-artifacts.json" + ).exists() + ) + + provider.write_bytes(_windows_pe(0xAA64)) + dll = bytearray(_windows_pe(0xAA64)) + dll[0x100 : 0x100 + len(EXPECTED_IDENTITY)] = bytes( + len(EXPECTED_IDENTITY) + ) + (release / "vcore.dll").write_bytes(dll) + with self.assertRaisesRegex( + RuntimeError, "incompatible Rust identity" + ): + builds.build_windows() if __name__ == "__main__": diff --git a/scripts/tests/test_selected_fingerprint_gates.py b/scripts/tests/test_selected_fingerprint_gates.py deleted file mode 100644 index ab7c777..0000000 --- a/scripts/tests/test_selected_fingerprint_gates.py +++ /dev/null @@ -1,119 +0,0 @@ -"""Offline guardrails for selected-profile coverage and explicit differences.""" - -import copy -import json -import tempfile -import unittest -from pathlib import Path - -from vcore_scripts.builds import CORE_DIR -from vcore_scripts.protocol_fingerprint import CASES -from vcore_scripts.protocol_fingerprint_reference import _reference_shape -from vcore_scripts.protocol_fingerprint_shape import ( - check_captures, - check_warm_captures, - expected_shape, -) -from vcore_scripts.protocol_inputs import same_source -from vcore_scripts.protocol_xhttp_fields import ( - run, - selected_gateway_config, - selected_profile_variants, -) -from vcore_scripts.tls_client_hello import validate_capture - - -class SelectedFingerprintGatesTest(unittest.TestCase): - def test_staging_metadata_is_not_a_source_change(self): - source = dict( - parent_commit="parent", - source_tree_sha256="tree", - lock_sha256="lock", - dirty_patch_sha256="unstaged", - ) - self.assertTrue(same_source(source, dict(source, dirty_patch_sha256="staged"))) - for field in ("parent_commit", "source_tree_sha256", "lock_sha256"): - self.assertFalse(same_source(source, dict(source, **{field: "changed"}))) - - def test_partial_wire_and_warm_matrices_never_pass(self): - for check in (check_captures, check_warm_captures): - with self.assertRaises(ValueError): - check([]) - - def test_reality_does_not_inherit_standard_tls13_template_pruning(self): - samples = json.loads( - (CORE_DIR / "tests/fingerprints/mihomo-selected-v1.json").read_text() - )["samples"] - sample = next( - s - for s in samples - if s["profile"] == "firefox" and s["context"] == "reality" - ) - shape = expected_shape(sample, "reality") - self.assertEqual(shape["ciphers"], validate_capture(sample)["ciphers"]) - self.assertEqual(len(shape["ciphers"]), 17) - hello = copy.deepcopy(validate_capture(sample)) - psk = dict(type=41, bytes=50, identities=[7], binders=[32]) - hello["extensions"].append(psk) - hello["bytes"] += 54 - case = dict(template="Firefox120", sni="fingerprint.test", context="reality") - with self.assertRaises(ValueError): - _reference_shape(hello, case) - self.assertEqual( - _reference_shape(hello, case, allow_psk=True)["extensions"][-1], psk - ) - hello["extensions"].insert(0, hello["extensions"].pop()) - with self.assertRaises(ValueError): - _reference_shape(hello, case, allow_psk=True) - - def test_transport_catalog_includes_existing_native_and_upgrade_paths(self): - for name in ( - "VLESS-UPGRADE-TLS-BASE", - "VLESS-UPGRADE-FAST-TLS-BASE", - "VLESS-H2-TLS-BASE", - "VLESS-HTTP-TLS-BASE", - "VLESS-WS-ED-2048-TLS-BASE", - "FINGERPRINT-VMESS-H2-TLS", - "FINGERPRINT-VMESS-HTTP-TLS", - ): - self.assertIn(name, CASES) - self.assertEqual(len(CASES), len(set(CASES))) - - def test_download_crossings_are_explicit_and_h3_requires_clear(self): - cases = selected_profile_variants("chrome") - - def get(suffix): - return cases[f"h2-selected-download-{suffix}"][0]["download-settings"] - - self.assertNotIn("client-fingerprint", get("inherit")) - self.assertEqual(get("different")["client-fingerprint"], "firefox") - self.assertEqual(get("clear")["client-fingerprint"], "none") - h3 = cases["h3-selected-download-clear"][0] - self.assertEqual(h3["_main_alpn"], "h2") - self.assertEqual(h3["download-settings"]["client-fingerprint"], "none") - parent = CORE_DIR / "target/interop/runs" - parent.mkdir(parents=True, exist_ok=True) - with tempfile.TemporaryDirectory(dir=parent) as directory: - output = Path(directory) / "not-created" - for profile, selection in [ - ("chrome133", ["h2-auto"]), - ("chrome", ["h3-auto"]), - ("chrome", ["h2c-auto"]), - ("none", ["h2c-auto"]), - ]: - with self.assertRaises(ValueError): - run(output, selection, client_fingerprint=profile) - self.assertFalse(output.exists()) - - def test_split_h2_h3_uses_a_gateway_with_one_native_handler(self): - fixture = selected_gateway_config("192.0.2.1")["apps"]["http"]["servers"][ - "fixture" - ] - self.assertEqual(fixture["protocols"], ["h2", "h3"]) - handler = fixture["routes"][0]["handle"][0] - self.assertEqual(handler["upstreams"], [{"dial": "192.0.2.1:23000"}]) - self.assertEqual(handler["transport"]["versions"], ["h2c"]) - self.assertEqual( - fixture["tls_connection_policies"][0]["client_authentication"]["mode"], - "require_and_verify", - ) diff --git a/scripts/tests/test_tls_client_hello.py b/scripts/tests/test_tls_client_hello.py deleted file mode 100644 index 5fc8c88..0000000 --- a/scripts/tests/test_tls_client_hello.py +++ /dev/null @@ -1,146 +0,0 @@ -"""RFC-shaped, in-memory ClientHello observations; no sockets or peers.""" - -import copy -import json -import unittest - -from vcore_scripts.builds import CORE_DIR -from vcore_scripts.tls_client_hello import ( - compare_client_hellos, - parse_client_hello, - validate_capture, -) - -# RFC 8446 vectors with synthetic random bytes, in their encoded order. -HELLO = bytes.fromhex( - "010000590303" - + "00" * 32 - + "0000041301c02f0100002c" - + "000a00060004001d0017" - + "000d0006000404030804" - + "002b00050403040303" - + "0010000b000908687474702f312e31" -) - - -def extensions_hello(extensions): - body = HELLO[4:47] + len(extensions).to_bytes(2, "big") + extensions - return b"\x01" + len(body).to_bytes(3, "big") + body - - -class ClientHelloTest(unittest.TestCase): - def test_raw_official_fixture_detects_tampered_parsed_evidence(self): - samples = json.loads( - (CORE_DIR / "tests/fingerprints/mihomo-selected-v1.json").read_text() - )["samples"] - for sample in samples: - self.assertEqual(validate_capture(sample), sample["parsed"]) - corrupted = copy.deepcopy(samples[0]) - corrupted["parsed"]["ciphers"].reverse() - with self.assertRaises(ValueError): - validate_capture(corrupted) - - def test_preserves_ordered_tls_vectors(self): - hello = parse_client_hello(HELLO) - self.assertEqual(hello["ciphers"], [0x1301, 0xC02F]) - self.assertEqual( - hello["extensions"], - [ - {"type": 10, "bytes": 6, "values": [29, 23]}, - {"type": 13, "bytes": 6, "values": [0x0403, 0x0804]}, - {"type": 43, "bytes": 5, "values": [0x0304, 0x0303]}, - {"type": 16, "bytes": 11, "protocols": ["http/1.1"]}, - ], - ) - - def test_retains_key_share_and_ech_layout_without_random_key_bytes(self): - data = extensions_hello( - bytes.fromhex("0033002b00290a0a000100001d0020" + "11" * 32) - + bytes.fromhex("fe0d000f0000010001120004abababab0001ac") - ) - - hello = parse_client_hello(data) - self.assertEqual( - hello["extensions"], - [ - { - "type": 51, - "bytes": 43, - "shares": [ - {"group": 0x0A0A, "bytes": 1, "grease_data": "00"}, - {"group": 29, "bytes": 32}, - ], - }, - { - "type": 65037, - "bytes": 15, - "hello_type": 0, - "kdf": 1, - "aead": 1, - "enc_bytes": 4, - "payload_bytes": 1, - }, - ], - ) - - def test_comparison_ignores_random_bytes_but_not_order_or_lengths(self): - random_changed = bytearray(HELLO) - random_changed[6:38] = b"\x12" * 32 - self.assertEqual(compare_client_hellos(HELLO, bytes(random_changed)), []) - cipher_changed = bytearray(HELLO) - cipher_changed[41:45] = bytes.fromhex("c02f1301") - self.assertIn("ciphers", compare_client_hellos(HELLO, bytes(cipher_changed))) - reordered = extensions_hello(HELLO[59:69] + HELLO[49:59] + HELLO[69:]) - self.assertIn("extensions", compare_client_hellos(HELLO, reordered)) - self.assertEqual( - compare_client_hellos(HELLO, reordered, shuffle_extensions=True), [] - ) - - def test_reports_ticket_binder_sni_and_compression_structure(self): - hello = parse_client_hello( - extensions_hello( - bytes.fromhex("00000009000700000474657374") - + bytes.fromhex("001b00050400010002") - + bytes.fromhex("00290030000b0005616263646500000000002120" + "55" * 32) - ) - ) - self.assertEqual( - hello["extensions"], - [ - {"type": 0, "bytes": 9, "names": [{"type": 0, "name": "test"}]}, - {"type": 27, "bytes": 5, "values": [1, 2]}, - {"type": 41, "bytes": 48, "identities": [5], "binders": [32]}, - ], - ) - - def test_shuffle_keeps_grease_slots_and_share_group_relationships(self): - grease = bytes.fromhex("0a0a0000") - alpn = HELLO[78:] - first = extensions_hello(grease + alpn) - moved = extensions_hello(alpn + grease) - self.assertIn( - "extensions", - compare_client_hellos(first, moved, shuffle_extensions=True), - ) - group = bytes.fromhex("000a000400020a0a") - share = bytes.fromhex("0033000700050a0a000100") - linked = extensions_hello(group + share) - unlinked = extensions_hello(group + share.replace(b"\x0a\x0a", b"\x1a\x1a")) - self.assertIn( - "grease_share_group_links", compare_client_hellos(linked, unlinked) - ) - - def test_rejects_truncated_duplicate_and_oversized_envelopes(self): - malformed = [HELLO[:size] for size in range(len(HELLO))] - malformed += [ - HELLO + b"\x00", - b"\x00" * 65537, - extensions_hello(bytes.fromhex("0012000000120000")), - ] - for data in malformed: - with self.subTest(size=len(data)), self.assertRaises(ValueError): - parse_client_hello(data) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_tls_observer.py b/scripts/tests/test_tls_observer.py deleted file mode 100644 index 56dc340..0000000 --- a/scripts/tests/test_tls_observer.py +++ /dev/null @@ -1,31 +0,0 @@ -"""The observer is tested over injected byte streams, never host listeners.""" - -import io -import os -import unittest -from unittest.mock import patch - -from vcore_scripts.container_tls_observer import capture_client_hello, main - - -class TlsObserverTest(unittest.TestCase): - def test_observer_refuses_host_execution_before_any_listener(self): - with patch.dict(os.environ, {}, clear=True), self.assertRaises(RuntimeError): - main() - - def test_captures_fragmented_hello_and_retains_record_layout(self): - raw = bytes.fromhex("160301000301000016030300020100") - records, hello, layout = capture_client_hello(io.BytesIO(raw).read) - self.assertEqual(records, raw) - self.assertEqual(hello, bytes.fromhex("0100000100")) - self.assertEqual( - layout, - [ - {"type": 22, "version": 0x0301, "bytes": 3}, - {"type": 22, "version": 0x0303, "bytes": 2}, - ], - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/tests/test_xhttp_gateway.py b/scripts/tests/test_xhttp_gateway.py deleted file mode 100644 index 1529d2c..0000000 --- a/scripts/tests/test_xhttp_gateway.py +++ /dev/null @@ -1,79 +0,0 @@ -"""Offline external fixture contracts; real peers are container-only.""" - -import json -import unittest - -from vcore_scripts.container_quic_observer import summarize_trace -from vcore_scripts.protocol_xhttp_gateway import gateway_config, sanitized_gateway_log - - -class XhttpGatewayTest(unittest.TestCase): - def test_gateway_requires_client_auth_and_only_forwards_to_one_native_handler(self): - config = gateway_config("192.0.2.10") - self.assertTrue(config["admin"]["disabled"]) - server = config["apps"]["http"]["servers"]["fixture"] - self.assertEqual(server["protocols"], ["h3"]) - self.assertEqual(server["listen"], [":23000"]) - self.assertTrue(server["automatic_https"]["disable"]) - auth = server["tls_connection_policies"][0]["client_authentication"] - self.assertEqual(auth["mode"], "require_and_verify") - self.assertEqual( - auth["ca"], - { - "provider": "file", - "pem_files": ["/data/fixture/root.pem"], - }, - ) - handler = server["routes"][0]["handle"][0] - self.assertEqual(handler["handler"], "reverse_proxy") - self.assertEqual(handler["upstreams"], [{"dial": "192.0.2.10:23000"}]) - self.assertEqual(handler["transport"]["versions"], ["h2c"]) - # Negative flush_interval disables upstream cancellation on disconnect. - self.assertNotIn("flush_interval", handler) - - def test_retained_gateway_log_has_protocol_evidence_without_request_identity(self): - retained = sanitized_gateway_log( - json.dumps( - { - "level": "warn", - "msg": "aborting with incomplete response", - "upstream": "192.0.2.10:23000", - "error": "context canceled", - "request": { - "remote_ip": "192.0.2.20", - "uri": "/private-session?token=secret", - "headers": {"Authorization": ["private-value"]}, - "proto": "HTTP/3.0", - "method": "GET", - "tls": {"proto": "h3", "client_common_name": "private-name"}, - }, - } - ) - ) - evidence = json.loads(retained) - self.assertEqual(evidence["http_version"], "HTTP/3.0") - self.assertEqual(evidence["alpn"], "h3") - self.assertTrue(evidence["client_certificate_present"]) - for forbidden in ("192.0.2", "private-", "secret", "Authorization"): - self.assertNotIn(forbidden, retained) - - def test_native_trace_evidence_requires_local_certificate_rejection(self): - trace = { - "name": "transport:connection_closed", - "data": { - "initiator": "local", - "connection_error": "crypto_error_0x12d", - # Native quic-go records local TLS alerts with an empty reason. - "reason": "", - }, - } - observed = summarize_trace("\x1e" + json.dumps(trace) + "\n") - self.assertEqual( - observed, - [{"tls_alert": "crypto_error_0x12d", "category": "certificate_expired"}], - ) - trace["data"]["initiator"] = "remote" - self.assertEqual(summarize_trace(json.dumps(trace) + "\n"), []) - trace["data"]["initiator"] = "local" - trace["data"]["connection_error"] = "idle_timeout" - self.assertEqual(summarize_trace(json.dumps(trace) + "\n"), []) diff --git a/scripts/tests/test_xhttp_peers.py b/scripts/tests/test_xhttp_peers.py deleted file mode 100644 index 0fd5399..0000000 --- a/scripts/tests/test_xhttp_peers.py +++ /dev/null @@ -1,61 +0,0 @@ -"""Offline fixture contracts; no host listener is used.""" - -import unittest - -from vcore_scripts.protocol_xhttp_peers import ( - client_config, - identity_peer_config, - peer_config, -) - - -class XhttpPeerTest(unittest.TestCase): - def test_identity_probe_distinguishes_client_auth_from_root_trust(self): - mihomo = identity_peer_config("M", "cert", "key", "ca") - listener = mihomo["listeners"][0] - self.assertEqual(listener["client-auth-type"], "require-and-verify") - self.assertEqual(listener["client-auth-cert"], "ca") - xray = identity_peer_config("XR", "cert", "key", "ca") - tls = xray["inbounds"][0]["streamSettings"]["tlsSettings"] - self.assertEqual( - tls["certificates"][-1], - { - "certificateFile": "ca", - "usage": "verify", - }, - ) - self.assertEqual(tls["alpn"], ["h3"]) - - def test_http_versions_are_explicit_and_download_uses_the_same_handler(self): - for version in ("h1", "h2", "h3"): - config = client_config( - "192.0.2.1", "pin", version, "packet-up", download=True - ) - node = config["proxies"][0] - self.assertEqual(node["alpn"], ["http/1.1" if version == "h1" else version]) - self.assertEqual(node["xhttp-opts"]["download-settings"], {}) - self.assertNotIn("skip-cert-verify", node) - self.assertEqual(config["listeners"][0]["proxy"], node["name"]) - - def test_mux_is_not_substituted_for_packet_encoding(self): - for protocol in ("h2mux", "smux", "yamux"): - config = client_config("192.0.2.1", "pin", "h2", "packet-up", mux=protocol) - node = config["proxies"][0] - self.assertEqual(node["smux"], {"enabled": True, "protocol": protocol}) - self.assertEqual(node["packet-encoding"], "xudp") - - def test_h3_bridge_does_not_replace_the_native_vless_decoder(self): - direct = peer_config("XR", "cert", "key") - bridge = peer_config("XR", "cert", "key", decoder="192.0.2.2") - self.assertEqual(direct["inbounds"][0]["protocol"], "vless") - self.assertEqual(bridge["inbounds"][0]["protocol"], "dokodemo-door") - self.assertEqual(bridge["inbounds"][0]["settings"]["port"], 23001) - self.assertEqual( - direct["inbounds"][0]["streamSettings"], - bridge["inbounds"][0]["streamSettings"], - ) - for config in (direct, bridge): - self.assertEqual(len(config["inbounds"]), 1) - stream = config["inbounds"][0]["streamSettings"] - self.assertEqual(stream["tlsSettings"]["alpn"], ["h3"]) - self.assertEqual(stream["xhttpSettings"]["mode"], "auto") diff --git a/scripts/uv.lock b/scripts/uv.lock index 0fc6031..4ff0c2c 100644 --- a/scripts/uv.lock +++ b/scripts/uv.lock @@ -1,30 +1,30 @@ version = 1 -revision = 3 +revision = 5 requires-python = ">=3.11" [[package]] name = "ruff" -version = "0.15.22" +version = "0.16.10" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/3a/06/ae069393fc66e8ff33036d4b368003833bf6e88ccf182e17e7a2f1c754fd/ruff-0.15.22.tar.gz", hash = "sha256:3f15175b1fb580126f58285a5dae6b2ea89000136d980c64499211f116b54809", size = 4785063, upload-time = "2026-07-16T15:14:13.244Z" } +sdist = { url = "https://files.pythonhosted.org/packages/c4/49/23802c45f093eb14bde54b141d2b2f058edfa63a06db7beed047308cc08f/ruff-0.16.10.tar.gz", hash = "sha256:eff4728c4eaae93f0955cd264d24b2ab348e74bf59986ccf282ba6dc16b3b017", size = 4958724, upload-time = "2026-10-01T18:03:21.697Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/23/18/ee54b7ae1e121be7a28ea6da4b67564ebb0530e183a54415ab7e3bcd2c4e/ruff-0.15.22-py3-none-linux_armv6l.whl", hash = "sha256:44423e73493737f5e7c5b41d475483898ff37afcdae38bc3da5085e29af1c2d8", size = 10781258, upload-time = "2026-07-16T15:13:19.452Z" }, - { url = "https://files.pythonhosted.org/packages/2f/d2/2520cb14761ddbeaf57642a76942fc36adcbdbe53b4532241995f6fc485c/ruff-0.15.22-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:b82c6482946e9eda7ff2e091d25b8bad3f718684e1916d41bd56873cee05b697", size = 10999477, upload-time = "2026-07-16T15:13:23.318Z" }, - { url = "https://files.pythonhosted.org/packages/c9/10/74e53572aa758dfaa678c2a2646b5c5515d884b7ca56be4d2ce03ca4b560/ruff-0.15.22-py3-none-macosx_11_0_arm64.whl", hash = "sha256:11c1c715af53a09f714e011106bffc419751ec8232fcb5da42173284ea3fec6f", size = 10466716, upload-time = "2026-07-16T15:13:26.162Z" }, - { url = "https://files.pythonhosted.org/packages/1e/cc/44eaaf0844e028182f2d0a8f2190d0f359159aed0a9e5ab861d892f1ae2a/ruff-0.15.22-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:742a29cf29bddb7c8327895d6a10e0e6c5b38a96dd407af9b5d0857f809c0576", size = 10892644, upload-time = "2026-07-16T15:13:29.229Z" }, - { url = "https://files.pythonhosted.org/packages/9f/21/8edf559014d2b0f82beea19cfb713993ad802ccda16868769979c6090a84/ruff-0.15.22-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:72af58b951b0ae395935ae79763dc349bc0eb706319d28f7a33ad2cfb3cfc178", size = 10576719, upload-time = "2026-07-16T15:13:32.35Z" }, - { url = "https://files.pythonhosted.org/packages/bf/1e/3a13abd392a3b50b62e5938a831f9ab6e588358cacad5c18545b716d2182/ruff-0.15.22-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:62d425005c1835eb24e2ee4161cb90e8db263415f4a71c8c72c33abaa6c0c224", size = 11376494, upload-time = "2026-07-16T15:13:35.958Z" }, - { url = "https://files.pythonhosted.org/packages/bf/3e/422d3d95bcf04dd78e1aeac22184d4f9a8fb2c01865d39d44618484a0317/ruff-0.15.22-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e8b9b3f8779a4f08c969defc3c8c35abffaa757e601ed5ae66d6d1db6519969a", size = 12208370, upload-time = "2026-07-16T15:13:39.185Z" }, - { url = "https://files.pythonhosted.org/packages/1e/91/5d065a0e0a02bf4813f5119ad278462eed081d2b832eb7c021ade0ec9e65/ruff-0.15.22-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:1e0dd1b2e4d3d585f897a0d137cbf4eaf6223bef4e8ce34d6bb12556c5f9249e", size = 11581098, upload-time = "2026-07-16T15:13:42.132Z" }, - { url = "https://files.pythonhosted.org/packages/f6/f9/a0d4871d12fae702eb1f41b686caf05f1f8b124dc6db6f784f53d74918fa/ruff-0.15.22-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:365523eb91d9224e1bcb03b022fbf0facb8f9e23792a2c53d9d4b3924bdbdebb", size = 11399422, upload-time = "2026-07-16T15:13:45.2Z" }, - { url = "https://files.pythonhosted.org/packages/18/80/c843a5176cddbceb0b7e8dd41cf9993490796c1c469348d384f5a5c13c56/ruff-0.15.22-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:fabfd168afdf29fee5be98b831efa9683c94d7c5a3b58b9ce5a2e38444589a74", size = 11381683, upload-time = "2026-07-16T15:13:48.46Z" }, - { url = "https://files.pythonhosted.org/packages/d4/00/8485de0ae92239438a36cfc51350db9b9e85c9ebdfaea91b18e422706662/ruff-0.15.22-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:225dbf095a87f1d9f90f5fd7924d2613ee452a75a4308c63a8f50f761787aa7c", size = 10850295, upload-time = "2026-07-16T15:13:51.655Z" }, - { url = "https://files.pythonhosted.org/packages/fa/91/24977ec2ec72eaf15e4394ace2959fdff2dd1e14f03e005e838023407169/ruff-0.15.22-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:1877d63b9d24ed278744f1523fd11b85540566d54641f97c566d7d9dc5ca5296", size = 10579640, upload-time = "2026-07-16T15:13:54.79Z" }, - { url = "https://files.pythonhosted.org/packages/9c/47/9b51216951974df1f263ac19da550d34252e0ed7218c25f10c5ef9ed7517/ruff-0.15.22-py3-none-musllinux_1_2_i686.whl", hash = "sha256:a1606c510bd7215680d32efab38965f7cdec3ef69f5170a3f4791404ffdd5262", size = 11105077, upload-time = "2026-07-16T15:13:57.915Z" }, - { url = "https://files.pythonhosted.org/packages/c2/47/20e9d4a3b8016778acea5fc32bb50d35d207500a17ddb529ffa6996feef8/ruff-0.15.22-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:630479b18625f5ffc373f77603a22a9f8ac0acd7ff0501178b5db28ec71e9c64", size = 11490980, upload-time = "2026-07-16T15:14:01.032Z" }, - { url = "https://files.pythonhosted.org/packages/4d/76/3f72d8fc38c1cb77b38c56a70da9d0c17700cc1cc50f9649c9d3c8f5ba71/ruff-0.15.22-py3-none-win32.whl", hash = "sha256:e5ba0e4a13fd14abbed2a77b517a3911290c6c6c59ef67784328d1668fab76cf", size = 10789165, upload-time = "2026-07-16T15:14:04.16Z" }, - { url = "https://files.pythonhosted.org/packages/cb/46/4965251734c2b6fcdca1b1b187d20bcac3af0ee5b083b89c910bb961ce3a/ruff-0.15.22-py3-none-win_amd64.whl", hash = "sha256:9be63ba1eb936acd2d1342fb8337c356353706fce233b2a15a09a97037e6acde", size = 11938297, upload-time = "2026-07-16T15:14:07.316Z" }, - { url = "https://files.pythonhosted.org/packages/57/c9/e69b1ff4c8b69093ef08b8919ab767af0569666865b39c30a8795d88d3c6/ruff-0.15.22-py3-none-win_arm64.whl", hash = "sha256:e1168075b72158510839f250027659cdd78476f40507dd517892304c41318661", size = 11298172, upload-time = "2026-07-16T15:14:10.51Z" }, + { url = "https://files.pythonhosted.org/packages/2f/21/ebce22e1d90cdb2cd691026b9c6e9bec6499f0b396089481755b5d49efff/ruff-0.16.10-py3-none-linux_armv6l.whl", hash = "sha256:488b0fe3f3574210e5cf80d9f59b9e3ab17a127a8155de3f307b392589cfb511", size = 10095557, upload-time = "2026-10-01T18:02:36.072Z" }, + { url = "https://files.pythonhosted.org/packages/cb/98/a54de85876a8b2612bfa0d84c7b9abfb39c6a3354aee7800b09c1649b9e3/ruff-0.16.10-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:e748ff95c934c4e978783b8e687bc174e7bd84e8ad24e3243e1ecfcda5e0282d", size = 10340577, upload-time = "2026-10-01T18:02:39.258Z" }, + { url = "https://files.pythonhosted.org/packages/9f/16/1a5a4a2657effe4806110f2b907313802f1367fcdbb29e8122766e407fab/ruff-0.16.10-py3-none-macosx_11_0_arm64.whl", hash = "sha256:3031a4a2e8e7b8a46f70be45f198c35a11ece509a94b80334d8d397a33c67550", size = 9774282, upload-time = "2026-10-01T18:02:41.79Z" }, + { url = "https://files.pythonhosted.org/packages/6e/fb/470085af734da396e68cd80fb0a3e7c109a459716ae59588c0f6fab8a17d/ruff-0.16.10-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:494401c86df4c4c25f69b9419605d944467ee98c42fb6ad405ef4fa40b8fb67d", size = 9920895, upload-time = "2026-10-01T18:02:44.458Z" }, + { url = "https://files.pythonhosted.org/packages/57/de/f10cffe4f88a37ea6615ec460f01bf76f0bc1477737a35d9ee61a630a78b/ruff-0.16.10-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:d203abc0ff2b773ee33d00ab8df0bb67046fbc7c07b119332f08b9b345cf8221", size = 9902707, upload-time = "2026-10-01T18:02:47.041Z" }, + { url = "https://files.pythonhosted.org/packages/ff/44/3fdcedf83ae60ef837dd239170e480dce606a9142cfa2db911afdf855fd9/ruff-0.16.10-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:bd83d1235a5258d318477bc5b576303974cbdef5df0c01a1bff14efcc23bd12a", size = 10619287, upload-time = "2026-10-01T18:02:49.485Z" }, + { url = "https://files.pythonhosted.org/packages/c1/62/02e76a5574002153618eafbb70e159468addc72a5d2c488e65cbb4639d2d/ruff-0.16.10-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:bc2610fb269fa56dd8a68669ae470fa6272902668c0fc2ebc3aa112b2633d5b8", size = 11339942, upload-time = "2026-10-01T18:02:52.008Z" }, + { url = "https://files.pythonhosted.org/packages/6b/c4/cde27d47ad8d4126c587e608c67c46e7feac11763f606d261a1bc8a489e6/ruff-0.16.10-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:3e70175e29cc94c26ea296c80e470180b744b7419026898e58f520c6ab32578e", size = 10934316, upload-time = "2026-10-01T18:02:54.811Z" }, + { url = "https://files.pythonhosted.org/packages/e4/03/17234145f302a645a123e8c3bb2411ecf4669fbc350de3b0d803230a1729/ruff-0.16.10-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f33f43a864a8483eebd160e713336c8bab02c934feaff0a33cf5ccb41546d09a", size = 10387968, upload-time = "2026-10-01T18:02:57.497Z" }, + { url = "https://files.pythonhosted.org/packages/71/29/2493af60240ee7644b38a4b821f5f9c3b5a4fa3178770fe1d0c685217395/ruff-0.16.10-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:1dfc6f0088149fb6a362c1c446bcbb3fd2157b3852fe2fa68409276eab9ad9b3", size = 10537906, upload-time = "2026-10-01T18:03:00.006Z" }, + { url = "https://files.pythonhosted.org/packages/2e/9a/f56b28f3b143bb9e518c34fb89ab191b626bca8b70dbf8af0d2e2d473572/ruff-0.16.10-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:6553498afc35f580f036030795810b9e6bcea31604b0fd9e8d352795473042e3", size = 10012938, upload-time = "2026-10-01T18:03:03.006Z" }, + { url = "https://files.pythonhosted.org/packages/c2/c4/fca37362848ea4d4d80712df13632e645e7c7cfb1bdedf140699ac7a090b/ruff-0.16.10-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:a3b8471dea115d37f123882be852bed13403746d3a76c11de4a19ec5f9ff5a03", size = 9897945, upload-time = "2026-10-01T18:03:05.818Z" }, + { url = "https://files.pythonhosted.org/packages/7d/c7/e0bc57664d6e0c61fd22f620af260fe9662d7e1ddb320ea7f160e76ef165/ruff-0.16.10-py3-none-musllinux_1_2_i686.whl", hash = "sha256:92e59a70bcbd9d3a5483656da906ec28edfdacfce00afd99edb8b4e9d15644be", size = 10333134, upload-time = "2026-10-01T18:03:08.25Z" }, + { url = "https://files.pythonhosted.org/packages/21/aa/5c9f3b68737c0e4a33a1db5dfab44f7b786d96ca91a7233112ddab1e6dc2/ruff-0.16.10-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:7ae7375f803b5520dc9f546bed7e3a0acb70b91812e9bb4b19927de22f25b77d", size = 10741702, upload-time = "2026-10-01T18:03:10.775Z" }, + { url = "https://files.pythonhosted.org/packages/78/fa/0f9c2020dc316c53d983be011720b8157cc052b97e3f4dfa7db6d0f880a6/ruff-0.16.10-py3-none-win32.whl", hash = "sha256:2a12e01cb9156c10c466f63b46eaae5ecea28dfbd21b5836353ae498e7d1349a", size = 10139176, upload-time = "2026-10-01T18:03:13.267Z" }, + { url = "https://files.pythonhosted.org/packages/99/29/cfb0df9448d4d4ad48c2de029ada9ebd71baa6da983a6c77ee6c6cd0fe82/ruff-0.16.10-py3-none-win_amd64.whl", hash = "sha256:97f2015c92aa97105b0eab19eb5d224884399281cfc5da86a92db4ab5e7fb2ca", size = 10584734, upload-time = "2026-10-01T18:03:16.006Z" }, + { url = "https://files.pythonhosted.org/packages/fc/05/c16957eb287c3fc062e032619a25868d93d408a844b725bcf514f7a378ff/ruff-0.16.10-py3-none-win_arm64.whl", hash = "sha256:25a65fe998c4e6861ec079ada5826a2fc605e6cbccbe9dcd7fac1f54e791621b", size = 10366440, upload-time = "2026-10-01T18:03:19.04Z" }, ] [[package]] @@ -40,4 +40,4 @@ dev = [ [package.metadata] [package.metadata.requires-dev] -dev = [{ name = "ruff", specifier = ">=0.15,<0.16" }] +dev = [{ name = "ruff", specifier = ">=0,<1" }] diff --git a/src/config/mod.rs b/src/config/mod.rs index 890b2de..4b38980 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -50,8 +50,6 @@ pub const MAX_RULES: usize = 1_024; pub const MAX_RULE_BYTES: usize = 1_024; pub const MAX_RULES_TOTAL_BYTES: usize = 128 * 1024; pub const MAX_DNS_NAMESERVERS: usize = 4; -pub const MAX_DNS_NAMESERVER_POLICIES: usize = 16; -pub const MAX_DNS_POLICY_GEOSITE_CODES: usize = 16; pub const MAX_SNIFFER_PORT_ITEMS: usize = 64; pub const MAX_GEOX_URL_BYTES: usize = 4_096; pub const MAX_CONTROLLER_SECRET_BYTES: usize = 255; @@ -1526,7 +1524,10 @@ fn normalize_geox_url(raw: String, field: &str) -> Result { } let url = Url::parse(&raw) .map_err(|error| VCoreError::InvalidConfig(format!("{field} is invalid: {error}")))?; - if url.scheme() != "https" { + let allowed_scheme = url.scheme() == "https"; + #[cfg(feature = "benchmark-geodata-http")] + let allowed_scheme = allowed_scheme || url.scheme() == "http"; + if !allowed_scheme { return invalid(format!("{field} must use HTTPS")); } let authority = raw @@ -2594,12 +2595,6 @@ fn normalize_dns_nameserver_policies( route_targets: &RouteTargetsByName, default_route: DnsRoute, ) -> Result> { - if raw_policies.len() > MAX_DNS_NAMESERVER_POLICIES { - return invalid(format!( - "dns.nameserver-policy exceeds the {MAX_DNS_NAMESERVER_POLICIES}-entry limit" - )); - } - let mut normalized = Vec::with_capacity(raw_policies.len()); let mut seen_codes = Vec::::new(); for raw_policy in raw_policies { @@ -2630,11 +2625,6 @@ fn normalize_dns_nameserver_policies( "dns.nameserver-policy contains duplicate GeoSite code `{code}`" )); } - if seen_codes.len() == MAX_DNS_POLICY_GEOSITE_CODES { - return invalid(format!( - "dns.nameserver-policy exceeds the {MAX_DNS_POLICY_GEOSITE_CODES}-code limit" - )); - } seen_codes.push(code.clone()); geosite_codes.push(code); } @@ -2949,16 +2939,13 @@ fn normalize_rule_keyword(input: &str) -> Result { } fn normalize_geo_code(input: &str, rule_type: &str) -> Result { - let bytes = input.as_bytes(); - if !(1..=64).contains(&bytes.len()) - || !bytes[0].is_ascii_alphanumeric() - || !bytes.iter().skip(1).all(|byte| { - byte.is_ascii_alphanumeric() || matches!(*byte, b'.' | b'_' | b'+' | b'!' | b'-') - }) - { - return invalid(format!("invalid {rule_type} code")); - } - Ok(input.to_ascii_lowercase()) + let kind = if rule_type == "GEOIP" { + crate::geodata::GeoDataKind::GeoIp + } else { + crate::geodata::GeoDataKind::GeoSite + }; + crate::geodata::normalize_selector(kind, input) + .map_err(|_| VCoreError::InvalidConfig(format!("invalid {rule_type} selector"))) } fn parse_ip_cidr(input: &str, require_v6: bool) -> Result { @@ -3507,7 +3494,7 @@ geo-update-interval: 24"#, "24.0", ), complete( - "http://geo.example.test/geoip.dat", + "ftp://geo.example.test/geoip.dat", "https://geo.example.test/geosite.dat", "24", ), @@ -3557,6 +3544,28 @@ geo-update-interval: 24"#, assert!(error.to_string().contains("4096-byte limit"), "{error}"); } + #[test] + fn geodata_update_http_is_benchmark_only() { + for url in [ + "http://geo.example.test/geoip.dat", + "http://geo.example.test:8080/geosite.dat", + ] { + assert_eq!( + normalize_geox_url(url.to_owned(), "geox-url.geoip").is_ok(), + cfg!(feature = "benchmark-geodata-http") + ); + } + for url in [ + "http://127.0.0.1/geoip.dat", + "http://[::1]/geoip.dat", + "http://user:password@geo.example.test/geoip.dat", + "http://@geo.example.test/geoip.dat", + "http://geo.example.test/geoip.dat#latest", + ] { + assert!(normalize_geox_url(url.to_owned(), "geox-url.geoip").is_err()); + } + } + #[test] fn rejects_incomplete_or_extended_geox_url_maps() { for fields in [ @@ -5748,6 +5757,40 @@ dns: ); } + #[test] + fn current_config_parses_geodata_attributes_and_inversion() { + let yaml = current_yaml( + r#"port: 1080 +authentication: + - measure:secret +dns: + enable: true + nameserver: [1.1.1.1] + nameserver-policy: + "geosite:GOOGLE@ads@CN,!GOOGLE@ads": ["tcp://223.5.5.5"] +rules: + - GEOSITE,!GOOGLE@CN@ADS@cn,DIRECT + - GEOSITE,geolocation-!cn,DIRECT + - GEOIP,!CN,DIRECT,no-resolve + - MATCH,proxy"#, + ); + let config = Config::parse_yaml(yaml.as_bytes()).unwrap(); + assert_eq!( + config.rules[0].kind, + RuleKind::GeoSite("!google@ads@cn".to_owned()) + ); + assert_eq!( + config.rules[1].kind, + RuleKind::GeoSite("geolocation-!cn".to_owned()) + ); + assert_eq!(config.rules[2].kind, RuleKind::GeoIp("!cn".to_owned())); + assert!(config.rules[2].no_resolve); + assert_eq!( + &*config.dns.nameserver_policies[0].geosite_codes, + ["google@ads@cn", "!google@ads"] + ); + } + #[test] fn current_config_strictly_validates_nameserver_policy_schema_and_limits() { for policy in [ @@ -5782,27 +5825,42 @@ dns: ); assert!(Config::parse_yaml(duplicate.as_bytes()).is_err()); - let too_many_codes = (0..=MAX_DNS_POLICY_GEOSITE_CODES) + let many_codes = (0..=16) .map(|index| format!("code{index}")) .collect::>() .join(","); let yaml = current_yaml(&format!( "port: 1080 authentication: - - measure:secret\ndns:\n enable: true\n nameserver: [1.1.1.1]\n nameserver-policy:\n \"geosite:{too_many_codes}\": [tcp://223.5.5.5]" + - measure:secret\ndns:\n enable: true\n nameserver: [1.1.1.1]\n nameserver-policy:\n \"geosite:{many_codes}\": [tcp://223.5.5.5]" )); - assert!(Config::parse_yaml(yaml.as_bytes()).is_err()); + assert_eq!( + Config::parse_yaml(yaml.as_bytes()) + .unwrap() + .dns + .nameserver_policies[0] + .geosite_codes + .len(), + 17 + ); - let too_many_policies = (0..=MAX_DNS_NAMESERVER_POLICIES) + let many_policies = (0..=16) .map(|index| format!(" \"geosite:code{index}\": [tcp://223.5.5.5]")) .collect::>() .join("\n"); let yaml = current_yaml(&format!( "port: 1080 authentication: - - measure:secret\ndns:\n enable: true\n nameserver: [1.1.1.1]\n nameserver-policy:\n{too_many_policies}" + - measure:secret\ndns:\n enable: true\n nameserver: [1.1.1.1]\n nameserver-policy:\n{many_policies}" )); - assert!(Config::parse_yaml(yaml.as_bytes()).is_err()); + assert_eq!( + Config::parse_yaml(yaml.as_bytes()) + .unwrap() + .dns + .nameserver_policies + .len(), + 17 + ); let disabled = current_yaml( r#"port: 1080 @@ -5887,6 +5945,9 @@ authentication: " - DOMAIN,example.com,unknown\n - MATCH,proxy", " - DOMAIN,example.com,DIRECT,no-resolve\n - MATCH,proxy", " - GEOIP,CN,DIRECT,NO-RESOLVE\n - MATCH,proxy", + " - GEOIP,CN@ads,DIRECT\n - MATCH,proxy", + " - GEOSITE,!,DIRECT\n - MATCH,proxy", + " - GEOSITE,@ads,DIRECT\n - MATCH,proxy", " - IP-CIDR,2001:db8::/32,DIRECT\n - MATCH,proxy", " - IP-CIDR6,192.0.2.0/24,DIRECT\n - MATCH,proxy", " - IP-CIDR,192.0.2.0/33,DIRECT\n - MATCH,proxy", diff --git a/src/dialer.rs b/src/dialer.rs index 796e01e..a06c864 100644 --- a/src/dialer.rs +++ b/src/dialer.rs @@ -19,20 +19,20 @@ use std::{ }; use async_trait::async_trait; -#[cfg(all(windows, feature = "ffi"))] use socket2::{Domain, Protocol, Socket, Type}; use tokio::{ net::{TcpSocket, UdpSocket}, - sync::Notify, + sync::{Notify, Semaphore}, time::timeout, }; +use tokio_util::{sync::CancellationToken, task::TaskTracker}; #[cfg(all(windows, feature = "ffi"))] use windows::Win32::Networking::WinSock::{ IP_UNICAST_IF, IPPROTO_IP, IPPROTO_IPV6, IPV6_UNICAST_IF, SOCKET, SOCKET_ERROR, WSAGetLastError, setsockopt, }; -use crate::limits::{DNS_WORKER_STACK_BYTES, MAX_DNS_WORKERS}; +use crate::limits::{DNS_WORKER_STACK_BYTES, MAX_DNS_WORKERS, MAX_SOCKET_INITIALIZATIONS}; use crate::resources::observation::{ObservedIo, ResourceKind, track}; pub type PhysicalStream = ObservedIo; @@ -246,6 +246,110 @@ pub trait SocketProtector: Send + Sync { fn protect(&self, socket: i32) -> io::Result<()>; } +/// Owns synchronous socket setup independently of the async I/O workers. +/// A started syscall cannot be aborted; shutdown must wait for its result to +/// be delivered or destroyed before releasing any platform callback lease. +#[derive(Debug)] +struct SocketInitialization { + closed: Mutex, + cancellation: CancellationToken, + tasks: TaskTracker, + submissions: Arc, +} + +impl Default for SocketInitialization { + fn default() -> Self { + Self { + closed: Mutex::new(false), + cancellation: CancellationToken::new(), + tasks: TaskTracker::new(), + submissions: Arc::new(Semaphore::new(MAX_SOCKET_INITIALIZATIONS)), + } + } +} + +impl SocketInitialization { + async fn run(&self, operation: F) -> io::Result + where + T: Send + 'static, + F: FnOnce(&dyn Fn() -> io::Result<()>) -> io::Result + Send + 'static, + { + // Await before registration/submission. Dropping the caller future + // cancels this wait; scope shutdown also wakes every waiting caller. + let permit = tokio::select! { + biased; + () = self.cancellation.cancelled() => return Err(socket_initialization_cancelled()), + permit = Arc::clone(&self.submissions).acquire_owned() => { + permit.map_err(|_| socket_initialization_cancelled())? + } + }; + let (response, receiver) = tokio::sync::oneshot::channel(); + let token = { + let closed = self + .closed + .lock() + .map_err(|_| io::Error::other("socket initialization lock is poisoned"))?; + if *closed { + return Err(socket_initialization_cancelled()); + } + // close() alone does not prohibit TaskTracker from admitting tasks. + // Register while holding the same short lock as begin_shutdown(). + self.tasks.token() + }; + let cancellation = self.cancellation.clone(); + let guard = track(ResourceKind::Task); + let task = + tokio::task::spawn_blocking(crate::resources::observation::inherit_thread(move || { + // Return capacity only after the tracking guard/token. A + // cancelled receiver must not release a started job's permit. + let _permit = permit; + let _token = token; + let _guard = guard; + let check = || { + if cancellation.is_cancelled() || response.is_closed() { + Err(socket_initialization_cancelled()) + } else { + Ok(()) + } + }; + let result = check().and_then(|()| operation(&check)); + // Do not return a socket as the blocking task's output: Tokio + // could retain it after our tracking token has been released. + // A cancelled receiver destroys the result inside this scope. + drop(response.send(result)); + })); + let _abort = tokio_util::task::AbortOnDropHandle::new(task); + tokio::select! { + biased; + () = self.cancellation.cancelled() => Err(socket_initialization_cancelled()), + result = receiver => result.map_err(|_| io::Error::other("socket initialization task failed"))?, + } + } + + fn begin_shutdown(&self) { + let mut closed = self + .closed + .lock() + .unwrap_or_else(|error| error.into_inner()); + *closed = true; + self.cancellation.cancel(); + self.submissions.close(); + self.tasks.close(); + } + + async fn shutdown(&self) { + self.begin_shutdown(); + self.tasks.wait().await; + } +} + +fn socket_initialization_cancelled() -> io::Error { + io::Error::new( + io::ErrorKind::Interrupted, + "socket initialization cancelled", + ) +} + #[derive(Debug, Clone, Copy)] struct SourceBinding { ipv4: Option, @@ -261,6 +365,7 @@ struct InterfaceBinding { #[derive(Clone)] pub struct Dialer { + initialization: Arc, protector: Option>, source_binding: Option, #[cfg(all(windows, feature = "ffi"))] @@ -287,6 +392,7 @@ impl std::fmt::Debug for Dialer { impl Default for Dialer { fn default() -> Self { Self { + initialization: Arc::new(SocketInitialization::default()), protector: None, source_binding: None, #[cfg(all(windows, feature = "ffi"))] @@ -298,6 +404,21 @@ impl Default for Dialer { } impl Dialer { + /// Start a new owner for a Running Session or private measurement. Policy + /// is retained, but stopping one graph must not close another session. + pub(crate) fn with_fresh_initialization(mut self) -> Self { + self.initialization = Arc::new(SocketInitialization::default()); + self + } + + pub(crate) fn begin_shutdown(&self) { + self.initialization.begin_shutdown(); + } + + pub(crate) async fn shutdown(&self) { + self.initialization.shutdown().await; + } + #[must_use] pub(crate) const fn with_ipv6(mut self, ipv6: bool) -> Self { self.ipv6 = ipv6; @@ -384,32 +505,28 @@ impl Dialer { let bind_address = self .source_address_for(destination)? .unwrap_or_else(|| wildcard_address(ipv6)); - #[cfg(all(windows, feature = "ffi"))] - let socket = { - let socket = Socket::new( - if ipv6 { Domain::IPV6 } else { Domain::IPV4 }, - Type::DGRAM, - Some(Protocol::UDP), - )?; - self.apply_interface_binding(socket.as_raw_socket(), destination)?; - socket.bind(&bind_address.into())?; - socket.set_nonblocking(true)?; - UdpSocket::from_std(socket.into())? - }; - #[cfg(not(all(windows, feature = "ffi")))] - let socket = UdpSocket::bind(bind_address).await?; - #[cfg(unix)] - if let Some(protector) = &self.protector { - protector.protect(socket.as_raw_fd())?; - } - #[cfg(not(unix))] - if self.protector.is_some() { - return Err(io::Error::new( - io::ErrorKind::Unsupported, - "socket protection is only supported on Unix platforms", - )); - } - Ok(ObservedIo::new(socket, ResourceKind::Socket)) + let dialer = self.clone(); + let (socket, guard) = self + .initialization + .run(move |check| { + let socket = Socket::new( + if ipv6 { Domain::IPV6 } else { Domain::IPV4 }, + Type::DGRAM, + Some(Protocol::UDP), + )?; + let guard = track(ResourceKind::Socket); + check()?; + #[cfg(all(windows, feature = "ffi"))] + dialer.apply_interface_binding(socket.as_raw_socket(), destination)?; + socket.bind(&bind_address.into())?; + socket.set_nonblocking(true)?; + check()?; + dialer.protect_socket(&socket)?; + check()?; + Ok((std::net::UdpSocket::from(socket), guard)) + }) + .await?; + Ok(ObservedIo::with_guard(UdpSocket::from_std(socket)?, guard)) } fn source_address_for(&self, destination: SocketAddr) -> io::Result> { @@ -500,28 +617,29 @@ impl Dialer { async fn connect_one(&self, address: SocketAddr) -> io::Result { self.require_permitted(address)?; let ipv6 = address.is_ipv6(); - let socket = if ipv6 { - TcpSocket::new_v6()? - } else { - TcpSocket::new_v4()? - }; - let guard = track(ResourceKind::Socket); - #[cfg(unix)] - if let Some(protector) = &self.protector { - protector.protect(socket.as_raw_fd())?; - } - #[cfg(not(unix))] - if self.protector.is_some() { - return Err(io::Error::new( - io::ErrorKind::Unsupported, - "socket protection is only supported on Unix platforms", - )); - } - #[cfg(all(windows, feature = "ffi"))] - self.apply_interface_binding(socket.as_raw_socket(), address)?; - if let Some(source_address) = self.source_address_for(address)? { - socket.bind(source_address)?; - } + let source_address = self.source_address_for(address)?; + let dialer = self.clone(); + let (socket, guard) = self + .initialization + .run(move |check| { + let socket = if ipv6 { + TcpSocket::new_v6()? + } else { + TcpSocket::new_v4()? + }; + let guard = track(ResourceKind::Socket); + check()?; + dialer.protect_socket(&socket)?; + check()?; + #[cfg(all(windows, feature = "ffi"))] + dialer.apply_interface_binding(socket.as_raw_socket(), address)?; + if let Some(source_address) = source_address { + socket.bind(source_address)?; + } + check()?; + Ok((socket, guard)) + }) + .await?; let stream = timeout(self.connect_timeout, socket.connect(address)) .await .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "connect timed out"))??; @@ -529,6 +647,25 @@ impl Dialer { Ok(ObservedIo::with_guard(stream, guard)) } + #[cfg(unix)] + fn protect_socket(&self, socket: &impl AsRawFd) -> io::Result<()> { + self.protector + .as_ref() + .map_or(Ok(()), |protector| protector.protect(socket.as_raw_fd())) + } + + #[cfg(not(unix))] + fn protect_socket(&self, _socket: &T) -> io::Result<()> { + if self.protector.is_some() { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "socket protection is only supported on Unix platforms", + )) + } else { + Ok(()) + } + } + fn require_permitted(&self, address: SocketAddr) -> io::Result<()> { if !self.ipv6 && address.is_ipv6() { return Err(io::Error::new( @@ -866,4 +1003,346 @@ mod tests { MAX_RESOLVED_ADDRESSES as u16 ); } + + #[cfg(all(unix, feature = "ffi"))] + #[test] + fn socket_initialization_churn_reclaims_completed_work() { + use crate::resources::observation::ResourceProbe; + + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .max_blocking_threads(4) + .enable_all() + .build() + .unwrap(); + let probe = ResourceProbe::default(); + runtime.block_on(probe.scope(async { + let protector = Arc::new(CountingProtector(AtomicUsize::new(0))); + let dialer = Dialer::default().with_protector(protector.clone()); + let destination = SocketAddr::from(([192, 0, 2, 1], 443)); + // Client-only socket setup: no listener, send, or network peer. + for _ in 0..32 { + let mut attempts = tokio::task::JoinSet::new(); + for _ in 0..256 { + let dialer = dialer.clone(); + let probe = probe.clone(); + attempts.spawn(async move { + probe + .scope(async move { + drop(dialer.bind_udp_for(destination).await.unwrap()); + }) + .await; + }); + } + while let Some(result) = attempts.join_next().await { + result.unwrap(); + } + } + dialer.shutdown().await; + assert_eq!(protector.0.load(Ordering::Relaxed), 8192); + assert!(probe.snapshot().peak(ResourceKind::Task) <= 64); + assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); + println!("socket initialization churn: {:?}", probe.snapshot()); + })); + } + + #[cfg(all(unix, feature = "ffi"))] + #[test] + fn socket_initialization_cancellation_joins_started_and_queued_work() { + use crate::resources::observation::ResourceProbe; + use std::sync::Condvar; + + #[derive(Default)] + struct GatedProtector { + released: Mutex, + wake: Condvar, + entered: AtomicUsize, + } + impl GatedProtector { + fn release(&self) { + *self.released.lock().unwrap() = true; + self.wake.notify_all(); + } + } + impl SocketProtector for GatedProtector { + fn protect(&self, _socket: i32) -> io::Result<()> { + self.entered.fetch_add(1, Ordering::Release); + let released = self.released.lock().unwrap(); + // A timeout keeps even a failing regression from trapping the + // runtime's synchronous destructor indefinitely. + let (_released, _) = self + .wake + .wait_timeout_while(released, Duration::from_secs(5), |released| !*released) + .unwrap(); + Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "test protector rejected before network connect", + )) + } + } + struct ReleaseOnDrop(Arc); + impl Drop for ReleaseOnDrop { + fn drop(&mut self) { + self.0.release(); + } + } + + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .max_blocking_threads(4) + .enable_all() + .build() + .unwrap(); + let probe = ResourceProbe::default(); + runtime.block_on(probe.scope(async { + let protector = Arc::new(GatedProtector::default()); + let _release = ReleaseOnDrop(protector.clone()); + let dialer = Dialer::default().with_protector(protector.clone()); + let destination = SocketAddr::from(([192, 0, 2, 1], 443)); + let callers_started = Arc::new(AtomicUsize::new(0)); + let mut attempts = tokio::task::JoinSet::new(); + for index in 0..4096 { + let dialer = dialer.clone(); + let probe = probe.clone(); + let callers_started = callers_started.clone(); + attempts.spawn(async move { + probe + .scope(async move { + callers_started.fetch_add(1, Ordering::Release); + if index % 2 == 0 { + dialer.connect_address(destination).await.map(drop) + } else { + dialer.bind_udp_for(destination).await.map(drop) + } + }) + .await + }); + } + timeout(Duration::from_secs(3), async { + while callers_started.load(Ordering::Acquire) != 4096 + || probe.snapshot().current(ResourceKind::Task) < 64 + || protector.entered.load(Ordering::Acquire) < 4 + { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + assert!(probe.snapshot().current(ResourceKind::Task) <= 64); + assert!(probe.snapshot().peak(ResourceKind::Task) <= 64); + println!("socket initialization backlog: {:?}", probe.snapshot()); + + attempts.abort_all(); + while let Some(result) = attempts.join_next().await { + assert!(result.unwrap_err().is_cancelled()); + } + println!( + "socket initialization backlog after caller cancellation: {:?}", + probe.snapshot() + ); + let mut stopped = Box::pin(dialer.shutdown()); + assert!( + timeout(Duration::from_millis(20), &mut stopped) + .await + .is_err(), + "Stop must not detach a started protect callback" + ); + let release_started = std::time::Instant::now(); + protector.release(); + timeout(Duration::from_secs(3), &mut stopped).await.unwrap(); + println!( + "socket initialization release-to-join: {:?}", + release_started.elapsed() + ); + drop(stopped); + assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); + assert_eq!(protector.entered.load(Ordering::Acquire), 4); + assert_eq!( + dialer.bind_udp_for(destination).await.unwrap_err().kind(), + io::ErrorKind::Interrupted + ); + + let fresh = dialer + .with_fresh_initialization() + .with_protector(Arc::new(CountingProtector(AtomicUsize::new(0)))); + drop(fresh.bind_udp_for(destination).await.unwrap()); + fresh.shutdown().await; + assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); + println!("socket initialization reclaimed: {:?}", probe.snapshot()); + })); + } + + #[cfg(all(unix, feature = "ffi"))] + #[test] + fn socket_initialization_shared_pool_contention_reclaims_cancelled_work() { + use crate::resources::observation::ResourceProbe; + use std::sync::Condvar; + + #[derive(Default)] + struct BlockingGate { + released: Mutex, + wake: Condvar, + entered: AtomicUsize, + } + impl BlockingGate { + fn wait(&self) -> bool { + self.entered.fetch_add(1, Ordering::Release); + let released = self.released.lock().unwrap(); + // Test-only unrelated work, not a slow protect callback. The + // timeout and RAII release also cover assertion failures. + let (released, _) = self + .wake + .wait_timeout_while(released, Duration::from_secs(10), |released| !*released) + .unwrap(); + *released + } + + fn release(&self) { + *self.released.lock().unwrap() = true; + self.wake.notify_all(); + } + } + struct ReleaseOnDrop(Arc); + impl Drop for ReleaseOnDrop { + fn drop(&mut self) { + self.0.release(); + } + } + struct FastRejectingProtector(AtomicUsize); + impl SocketProtector for FastRejectingProtector { + fn protect(&self, _socket: i32) -> io::Result<()> { + self.0.fetch_add(1, Ordering::Relaxed); + Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "test protector rejected before network connect", + )) + } + } + + let runtime = tokio::runtime::Builder::new_multi_thread() + .max_blocking_threads(4) + .enable_all() + .build() + .unwrap(); + runtime.block_on(async { + for submitted in [512, 4096] { + let probe = ResourceProbe::default(); + let gate = Arc::new(BlockingGate::default()); + let _release = ReleaseOnDrop(gate.clone()); + let mut blockers = Vec::new(); + for _ in 0..4 { + let gate = gate.clone(); + // Deliberately do not inherit the initialization probe: + // these jobs belong to the test, not to this Dialer. + blockers.push(tokio::task::spawn_blocking(move || gate.wait())); + } + timeout(Duration::from_secs(3), async { + while gate.entered.load(Ordering::Acquire) != 4 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + + let protector = Arc::new(FastRejectingProtector(AtomicUsize::new(0))); + let protector_lease = Arc::downgrade(&protector); + let dialer = Dialer::default().with_protector(protector.clone()); + let destination = SocketAddr::from(([192, 0, 2, 1], 443)); + let callers_started = Arc::new(AtomicUsize::new(0)); + let mut attempts = tokio::task::JoinSet::new(); + for index in 0..submitted { + let dialer = dialer.clone(); + let probe = probe.clone(); + let callers_started = callers_started.clone(); + attempts.spawn(async move { + probe + .scope(async move { + callers_started.fetch_add(1, Ordering::Release); + if index % 2 == 0 { + dialer.connect_address(destination).await.map(drop) + } else { + dialer.bind_udp_for(destination).await.map(drop) + } + }) + .await + }); + } + timeout(Duration::from_secs(3), async { + while callers_started.load(Ordering::Acquire) != submitted + || probe.snapshot().current(ResourceKind::Task) < 64 + { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + let before_cancel = probe.snapshot(); + assert!(before_cancel.current(ResourceKind::Task) <= 64); + assert!(before_cancel.peak(ResourceKind::Task) <= 64); + assert_eq!(before_cancel.current(ResourceKind::Socket), 0); + assert_eq!(protector.0.load(Ordering::Relaxed), 0); + println!( + "socket initialization shared-pool submitted={submitted} before cancel: {before_cancel:?}" + ); + + // Scope Stop must wake permit waiters without waiting for + // unrelated blocking jobs or requiring caller aborts. + dialer.begin_shutdown(); + timeout(Duration::from_secs(3), async { + while let Some(result) = attempts.join_next().await { + assert_eq!(result.unwrap().unwrap_err().kind(), io::ErrorKind::Interrupted); + } + }) + .await + .unwrap(); + let after_cancel = probe.snapshot(); + assert!(after_cancel.current(ResourceKind::Task) <= 64); + assert!(after_cancel.peak(ResourceKind::Task) <= 64); + assert_eq!(after_cancel.current(ResourceKind::Socket), 0); + assert_eq!(protector.0.load(Ordering::Relaxed), 0); + println!( + "socket initialization shared-pool submitted={submitted} after Stop cancellation: {after_cancel:?}" + ); + + let release_started = std::time::Instant::now(); + gate.release(); + timeout(Duration::from_secs(3), dialer.shutdown()) + .await + .unwrap(); + let release_to_join = release_started.elapsed(); + for blocker in blockers { + assert!(timeout(Duration::from_secs(3), blocker).await.unwrap().unwrap()); + } + assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); + assert_eq!(protector.0.load(Ordering::Relaxed), 0); + assert_eq!( + dialer.bind_udp_for(destination).await.unwrap_err().kind(), + io::ErrorKind::Interrupted + ); + + // A new lifecycle owner works after the shared pool recovers; + // the rejecting protector still prevents any TCP connect. + let fresh = dialer.clone().with_fresh_initialization(); + assert_eq!( + probe + .scope(fresh.bind_udp_for(destination)) + .await + .unwrap_err() + .kind(), + io::ErrorKind::PermissionDenied + ); + fresh.shutdown().await; + assert_eq!(protector.0.load(Ordering::Relaxed), 1); + assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); + drop(fresh); + drop(dialer); + drop(protector); + assert!(protector_lease.upgrade().is_none()); + println!( + "socket initialization shared-pool submitted={submitted} release-to-join={release_to_join:?} reclaimed: {:?}", + probe.snapshot() + ); + } + }); + } } diff --git a/src/dispatch.rs b/src/dispatch.rs index 86d1d7c..a376943 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -82,6 +82,20 @@ pub trait DatagramTransport: Send { async fn send(&mut self, datagram: Datagram) -> Result<(), DispatchError>; + /// Sends the first packet of an authenticated logical connection. + /// + /// The routing layer may replace this peer's fixed action when this exact + /// opaque identity changes. Replays with the same identity preserve it. + /// IDs are at most 25 bytes and are never forwarded to an outbound or logged. + /// Ordinary outbounds ignore the metadata; this does not reopen transport IO. + async fn send_with_flow_id( + &mut self, + datagram: Datagram, + _flow_id: &[u8], + ) -> Result<(), DispatchError> { + self.send(datagram).await + } + /// Receives one complete datagram. /// /// Implementations must be cancellation-safe: inbound relays and the @@ -245,6 +259,14 @@ impl DatagramTransport for ObservedDatagramTransport { self.inner.send(datagram).await } + async fn send_with_flow_id( + &mut self, + datagram: Datagram, + flow_id: &[u8], + ) -> Result<(), DispatchError> { + self.inner.send_with_flow_id(datagram, flow_id).await + } + async fn receive(&mut self) -> Result { self.inner.receive().await } @@ -344,6 +366,49 @@ mod tests { ) } + #[tokio::test] + async fn session_observer_preserves_authenticated_flow_identity() { + struct InitialTransport(Arc); + #[async_trait] + impl DatagramTransport for InitialTransport { + async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { + panic!("observer must preserve first-flow metadata"); + } + async fn send_with_flow_id( + &mut self, + _datagram: Datagram, + flow_id: &[u8], + ) -> Result<(), DispatchError> { + assert_eq!(flow_id, b"authenticated"); + self.0.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + async fn receive(&mut self) -> Result { + std::future::pending().await + } + } + let sends = Arc::new(AtomicUsize::new(0)); + let stats = RuntimeResourceStats::new("flow_metadata_test"); + let mut observed = ObservedDatagramTransport { + inner: Box::new(InitialTransport(sends.clone())), + _activity: stats.begin(ResourceActivity::UdpAssociation), + }; + observed + .send_with_flow_id( + Datagram { + remote: Destination::Ip("192.0.2.1:443".parse().unwrap()), + payload: bytes::Bytes::from_static(b"initial"), + sniffed_domain: None, + }, + b"authenticated", + ) + .await + .unwrap(); + assert_eq!(sends.load(Ordering::Relaxed), 1); + drop(observed); + assert_eq!(stats.snapshot().udp_current, 0); + } + #[tokio::test] async fn session_observation_does_not_reject_former_tcp_or_udp_boundaries() { const TCP_SESSIONS: usize = 160; diff --git a/src/dns/runtime.rs b/src/dns/runtime.rs index a7ca9bb..eb13953 100644 --- a/src/dns/runtime.rs +++ b/src/dns/runtime.rs @@ -93,7 +93,9 @@ pub struct RuntimeDns { egress: DnsEgress, cache: Mutex, opaque_cache: Mutex, - hints: Option>, + // Hint lookup and insertion are bounded synchronous work. Keep this + // shared TUN hot path free of asynchronous mutex scheduling. + hints: Option>, singleflight: Arc, tcp_pool: Arc, next_id: AtomicU16, @@ -872,7 +874,7 @@ impl RuntimeDns { cache: Mutex::new(DnsCache::with_max_entries(address_cache_entries)), opaque_cache: Mutex::new(OpaqueDnsCache::new()), hints: (redir_host_entries != 0) - .then(|| Mutex::new(RedirHostHints::with_max_entries(redir_host_entries))), + .then(|| StdMutex::new(RedirHostHints::with_max_entries(redir_host_entries))), singleflight: SingleflightRegistry::new(resource_stats.clone()), tcp_pool: TcpConnectionPool::new(), next_id: AtomicU16::new(1), @@ -1225,7 +1227,10 @@ impl RuntimeDns { return None; } let hints = self.hints.as_ref()?; - hints.lock().await.get(address, Instant::now()) + hints + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .get(address, Instant::now()) } async fn resolve_type( @@ -1536,7 +1541,9 @@ impl RuntimeDns { .insert_positive(name, query_type, &answers.addresses, ttl, now) .map_err(RuntimeDnsError::Cache)?; if let Some(hints) = &self.hints { - let mut hints = hints.lock().await; + let mut hints = hints + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); for address in answers.addresses { hints .insert(address, name, ttl, now) @@ -3620,6 +3627,99 @@ mod tests { ); } + #[test] + fn concurrent_redir_host_hint_reads_and_dns_updates_complete() { + fn require_send(value: T) -> T { + value + } + + let address = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 13)); + let domains = Arc::new( + (0..64) + .map(|index| format!("concurrent-{index}.example")) + .collect::>(), + ); + let dispatcher = Arc::new(MockDispatcher::with_replies( + (0..domains.len()) + .map(|_| MockReply::Response(ResponseSpec::answer(address))) + .collect(), + vec![], + )); + let resolver = Arc::new(RuntimeDns::new( + &config(false, vec![nameserver(1, DnsTransport::Udp)]), + dispatcher.clone(), + )); + let seeded = Arc::new(Notify::new()); + let reader_started = Arc::new(Notify::new()); + let writer_done = Arc::new(AtomicBool::new(false)); + let (completed, completion) = std::sync::mpsc::channel(); + let deadline = Instant::now() + Duration::from_secs(2); + let mut workers = Vec::new(); + for writer in [true, false] { + let resolver = resolver.clone(); + let domains = domains.clone(); + let seeded = seeded.clone(); + let reader_started = reader_started.clone(); + let writer_done = writer_done.clone(); + let completed = completed.clone(); + workers.push(std::thread::spawn(move || { + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + runtime.block_on(require_send(async { + tokio::time::timeout(Duration::from_secs(1), async { + if writer { + assert_eq!(resolver.resolve(&domains[0]).await.unwrap(), [address]); + seeded.notify_one(); + reader_started.notified().await; + for domain in domains.iter().skip(1) { + assert_eq!(resolver.resolve(domain).await.unwrap(), [address]); + assert_eq!( + resolver.domain_hint(address).await, + Some(domain.clone()) + ); + std::thread::yield_now(); + } + writer_done.store(true, Ordering::Release); + } else { + seeded.notified().await; + assert_eq!( + resolver.domain_hint(address).await, + Some(domains[0].clone()) + ); + reader_started.notify_one(); + while !writer_done.load(Ordering::Acquire) { + let hint = resolver.domain_hint(address).await.unwrap(); + assert!(domains.contains(&hint)); + tokio::task::yield_now().await; + } + } + }) + .await + .expect("concurrent DNS hint operations did not complete"); + })); + })); + let _ = completed.send(result); + })); + } + drop(completed); + for _ in 0..workers.len() { + let result = completion + .recv_timeout(deadline.saturating_duration_since(Instant::now())) + .expect("concurrent DNS hint workers exceeded the test deadline"); + if let Err(panic) = result { + std::panic::resume_unwind(panic); + } + } + for worker in workers { + worker.join().unwrap(); + } + assert_eq!(dispatcher.udp_calls.load(Ordering::Acquire), domains.len()); + assert_eq!(dispatcher.tcp_calls.load(Ordering::Acquire), 0); + } + #[tokio::test] async fn zero_redir_host_capacity_disables_only_hints_not_typed_cache() { let address = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 14)); diff --git a/src/ffi/measure_delay.rs b/src/ffi/measure_delay.rs index f82a3ca..45d7fc3 100644 --- a/src/ffi/measure_delay.rs +++ b/src/ffi/measure_delay.rs @@ -115,7 +115,9 @@ pub(super) fn measure_delay( .build() .map_err(InvokeFailure::from)?; let results = runtime.block_on(run_batch(request)); - runtime.shutdown_timeout(Duration::from_millis(100)); + // A probe panic skips its async cleanup. Runtime Drop is the final join + // barrier for synchronous physical setup; never detach it with a timeout. + drop(runtime); Ok(results) } diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index d9ed36f..152f929 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -1,4 +1,4 @@ -//! Runtime-local JSON Invoke boundary for Apple and Android hosts. +//! Runtime-local JSON Invoke boundary for platform hosts. //! //! `VCoreInvoke` is the only public business entry point. A runtime-local //! per-request size limit bounds parsing memory. One public lifecycle @@ -38,12 +38,30 @@ use crate::{ runtime::PreparedCore, }; -#[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] +#[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +))] use crate::platform::{TunFd, TunIo}; -#[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] +#[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +))] type InvokeTun = (TunFd, TunFraming); -#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))] +#[cfg(not(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +)))] type InvokeTun = (); #[cfg(target_os = "android")] @@ -123,7 +141,7 @@ struct CoreInner { engine: Option, last_error: String, tun_lease: Option, - ios_tun_allocator_relief: Option, + apple_tun_allocator_relief: Option, } struct PreparedState { @@ -152,20 +170,20 @@ impl Drop for TunLease { } #[derive(Clone)] -struct IosTunAllocatorRelief { - state: Arc, +struct AppleTunAllocatorRelief { + state: Arc, } -struct IosTunAllocatorReliefState { +struct AppleTunAllocatorReliefState { relieved: AtomicBool, } -impl IosTunAllocatorRelief { +impl AppleTunAllocatorRelief { fn new(has_tun: bool) -> Option { - #[cfg(target_os = "ios")] + #[cfg(any(target_os = "ios", target_os = "tvos"))] if has_tun { return Some(Self { - state: Arc::new(IosTunAllocatorReliefState { + state: Arc::new(AppleTunAllocatorReliefState { relieved: AtomicBool::new(false), }), }); @@ -176,15 +194,15 @@ impl IosTunAllocatorRelief { fn relieve(&self) { if !self.state.relieved.swap(true, Ordering::AcqRel) { - relieve_ios_tun_allocator(); + relieve_apple_tun_allocator(); } } } -impl Drop for IosTunAllocatorReliefState { +impl Drop for AppleTunAllocatorReliefState { fn drop(&mut self) { if !self.relieved.swap(true, Ordering::AcqRel) { - relieve_ios_tun_allocator(); + relieve_apple_tun_allocator(); } } } @@ -203,10 +221,10 @@ impl Drop for InstanceRemovalGuard<'_> { } } -struct RuntimeThreadGuard; +pub(crate) struct RuntimeThreadGuard; impl RuntimeThreadGuard { - fn enter() -> Self { + pub(crate) fn enter() -> Self { IS_RUNTIME_THREAD.with(|marker| { // Admission state must also be set when debug assertions are disabled. let was_runtime_thread = marker.replace(true); @@ -222,6 +240,44 @@ impl Drop for RuntimeThreadGuard { } } +// Logging is restored before Invoke admission is released on worker exit. +struct RuntimeWorkerScope { + #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "macos"))] + _logging: tracing::dispatcher::DefaultGuard, + _admission: RuntimeThreadGuard, +} + +impl RuntimeWorkerScope { + fn enter() -> Self { + let admission = RuntimeThreadGuard::enter(); + Self { + #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "macos"))] + _logging: crate::platform::apple_logging::enter(), + _admission: admission, + } + } +} + +thread_local! { + static RUNTIME_WORKER_SCOPE: std::cell::RefCell> = + const { std::cell::RefCell::new(None) }; +} + +pub(crate) fn engine_runtime_builder() -> tokio::runtime::Builder { + let mut builder = tokio::runtime::Builder::new_multi_thread(); + builder + .on_thread_start(|| { + RUNTIME_WORKER_SCOPE.with(|scope| { + *scope.borrow_mut() = Some(RuntimeWorkerScope::enter()); + }); + }) + .on_thread_stop(|| { + let scope = RUNTIME_WORKER_SCOPE.with(|scope| scope.borrow_mut().take()); + drop(scope); + }); + builder +} + impl Engine { fn stop(&mut self) -> Result<(), InvokeFailure> { if let Some(stop) = self.stop.take() { @@ -445,7 +501,7 @@ impl RuntimeRegistry { } let id = self .next_id - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |current| { current.checked_add(1) }) .map_err(|_| InvokeFailure::internal("instance ID space is exhausted"))?; @@ -616,7 +672,7 @@ pub(super) fn invoke_bytes(request: &[u8]) -> Vec { /// It intentionally returns bytes so JNI never routes arbitrary JSON through /// Modified UTF-8 strings. pub(super) fn invoke_bytes_admitted(request: &[u8]) -> Vec { - #[cfg(any(target_os = "ios", target_os = "macos"))] + #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "macos"))] let _logging = crate::platform::apple_logging::enter(); invoke_guarded(|| dispatch_bytes(request)) } @@ -863,7 +919,7 @@ impl CoreController { tun_lease, protector, } = acquisition; - let allocator_relief = IosTunAllocatorRelief::new(has_tun); + let allocator_relief = AppleTunAllocatorRelief::new(has_tun); { let mut inner = lock(&self.inner); inner.last_error.clear(); @@ -872,7 +928,7 @@ impl CoreController { .transition(LifecycleState::Preparing) .map_err(InvokeFailure::from)?; inner.tun_lease = tun_lease; - inner.ios_tun_allocator_relief = allocator_relief; + inner.apple_tun_allocator_relief = allocator_relief; } let prepared = (|| { @@ -911,7 +967,7 @@ impl CoreController { .map_err(InvokeFailure::from) { Ok(()) => { - observe_ios_tun_memory(has_tun, "prepare-complete"); + observe_apple_tun_memory(has_tun, "prepare-complete"); Ok(()) } Err(error) => reset_failed_operation(&mut inner, "prepare-transition-failed") @@ -927,11 +983,6 @@ impl CoreController { payload: StartPayload, presence: StartFieldPresence, ) -> Result<(), InvokeFailure> { - if cfg!(target_os = "linux") { - return Err(InvokeFailure::new( - "VCore runtime startup is unsupported on Linux", - )); - } let _command = self.try_command()?; let (prepared, tun, has_tun, allocator_relief) = { let mut inner = lock(&self.inner); @@ -960,7 +1011,7 @@ impl CoreController { .lifecycle .transition(LifecycleState::Starting) .map_err(InvokeFailure::from)?; - let allocator_relief = inner.ios_tun_allocator_relief.clone(); + let allocator_relief = inner.apple_tun_allocator_relief.clone(); (prepared, tun, has_tun, allocator_relief) }; @@ -980,7 +1031,6 @@ impl CoreController { }; let spawned = thread::Builder::new() .name(format!("vcore-runtime-{instance_id}")) - .stack_size(1024 * 1024) .spawn(crate::resources::observation::inherit_thread(move || { let _runtime_thread = RuntimeThreadGuard::enter(); run_engine(context, prepared.core, tun, dialer, stop_rx, startup_tx) @@ -1013,7 +1063,7 @@ impl CoreController { .and(Err(error)); } inner.engine = Some(engine); - observe_ios_tun_memory(has_tun, "start-complete"); + observe_apple_tun_memory(has_tun, "start-complete"); Ok(()) } Ok(Err(error)) => { @@ -1053,7 +1103,7 @@ impl CoreController { LifecycleState::Stopped => { let had_tun = inner.tun_lease.is_some(); inner.last_error.clear(); - observe_ios_tun_memory(had_tun, "stop-complete"); + observe_apple_tun_memory(had_tun, "stop-complete"); clear_instance_leases(&mut inner); return Ok(()); } @@ -1065,7 +1115,7 @@ impl CoreController { .lifecycle .transition(LifecycleState::Stopped) .map_err(InvokeFailure::from); - observe_ios_tun_memory(had_tun, "stop-complete"); + observe_apple_tun_memory(had_tun, "stop-complete"); clear_instance_leases(&mut inner); return transitioned; } @@ -1162,7 +1212,7 @@ impl CoreController { let mut inner = lock(&self.inner); inner.lifecycle = Lifecycle::default(); inner.engine = None; - observe_ios_tun_memory(had_tun, "panic-recovery"); + observe_apple_tun_memory(had_tun, "panic-recovery"); clear_instance_leases(&mut inner); Ok(()) } @@ -1171,20 +1221,20 @@ impl CoreController { fn clear_instance_leases(inner: &mut CoreInner) { // Prepared-only paths own the final allocator-relief handle here. Running // paths share it with the engine, whose completion performs relief first. - inner.ios_tun_allocator_relief = None; + inner.apple_tun_allocator_relief = None; inner.tun_lease = None; } -fn observe_ios_tun_memory(has_tun: bool, stage: &'static str) { - #[cfg(target_os = "ios")] +fn observe_apple_tun_memory(has_tun: bool, stage: &'static str) { + #[cfg(any(target_os = "ios", target_os = "tvos"))] if has_tun { crate::platform::process_memory::observe(stage); } let _ = (has_tun, stage); } -fn relieve_ios_tun_allocator() { - #[cfg(target_os = "ios")] +fn relieve_apple_tun_allocator() { + #[cfg(any(target_os = "ios", target_os = "tvos"))] crate::platform::process_memory::relieve_allocator_pressure(); } @@ -1229,18 +1279,30 @@ fn geodata_resource_data(state: GeoResourceState) -> Value { }) } -#[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] +#[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +))] fn duplicate_start_tun(tun: Option<(i32, TunFraming)>) -> Result, InvokeFailure> { tun.map(|(fd, framing)| TunFd::duplicate(fd).map(|fd| (fd, framing))) .transpose() .map_err(InvokeFailure::from) } -#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))] +#[cfg(not(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +)))] fn duplicate_start_tun(tun: Option<(i32, TunFraming)>) -> Result, InvokeFailure> { if tun.is_some() { return Err(InvokeFailure::invalid_request( - "TUN fd is unsupported on this target; only iOS, macOS, and Android are supported", + "TUN fd is unsupported on this target", )); } Ok(None) @@ -1291,7 +1353,17 @@ fn validate_platform_tun_framing(framing: TunFraming) -> Result<(), InvokeFailur Ok(()) } -#[cfg(any(target_os = "ios", target_os = "macos"))] +#[cfg(target_os = "linux")] +fn validate_platform_tun_framing(framing: TunFraming) -> Result<(), InvokeFailure> { + if framing != TunFraming::RawIp { + return Err(InvokeFailure::invalid_request( + "Linux TUN requires rawIp framing", + )); + } + Ok(()) +} + +#[cfg(any(target_os = "ios", target_os = "tvos", target_os = "macos"))] fn validate_platform_tun_framing(framing: TunFraming) -> Result<(), InvokeFailure> { if framing != TunFraming::Utun { return Err(InvokeFailure::invalid_request( @@ -1301,18 +1373,24 @@ fn validate_platform_tun_framing(framing: TunFraming) -> Result<(), InvokeFailur Ok(()) } -#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))] +#[cfg(not(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +)))] fn validate_platform_tun_framing(framing: TunFraming) -> Result<(), InvokeFailure> { let _ = framing; Err(InvokeFailure::invalid_request( - "TUN fd is unsupported on this target; only iOS, macOS, and Android are supported", + "TUN fd is unsupported on this target", )) } struct EngineContext { instance_id: u64, has_tun: bool, - allocator_relief: Option, + allocator_relief: Option, } fn run_engine( @@ -1328,11 +1406,11 @@ fn run_engine( has_tun, allocator_relief, } = context; - #[cfg(any(target_os = "ios", target_os = "macos"))] + #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "macos"))] let _logging = crate::platform::apple_logging::enter(); tracing::info!(instance_id, has_tun, "VCore runtime engine starting"); let result = run_engine_inner(instance_id, has_tun, prepared, tun, dialer, stop, startup); - observe_ios_tun_memory(has_tun, "stop-complete"); + observe_apple_tun_memory(has_tun, "stop-complete"); if let Some(relief) = allocator_relief { relief.relieve(); } @@ -1358,11 +1436,7 @@ fn run_engine_inner( stop: oneshot::Receiver<()>, startup: SyncSender>, ) -> io::Result<()> { - let runtime = match tokio::runtime::Builder::new_current_thread() - .enable_io() - .enable_time() - .build() - { + let runtime = match engine_runtime_builder().enable_io().enable_time().build() { Ok(runtime) => runtime, Err(error) => { let message = error.to_string(); @@ -1371,7 +1445,13 @@ fn run_engine_inner( } }; runtime.block_on(async move { - #[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] + #[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" + ))] let started = match tun { Some((duplicate, framing)) => { tracing::info!(instance_id, ?framing, "VCore attaching TUN descriptor"); @@ -1380,7 +1460,13 @@ fn run_engine_inner( } None => prepared.start_local(dialer).await, }; - #[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))] + #[cfg(not(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" + )))] let started = { debug_assert!(tun.is_none()); prepared.start_local(dialer).await @@ -1405,9 +1491,9 @@ fn run_engine_inner( } async fn wait_for_engine_shutdown(stop: oneshot::Receiver<()>, has_tun: bool) -> io::Result<()> { - #[cfg(target_os = "ios")] + #[cfg(any(target_os = "ios", target_os = "tvos"))] let mut stop = stop; - #[cfg(target_os = "ios")] + #[cfg(any(target_os = "ios", target_os = "tvos"))] if has_tun { let interval = crate::platform::process_memory::TELEMETRY_INTERVAL; let first_tick = tokio::time::Instant::now() + interval; @@ -1416,7 +1502,7 @@ async fn wait_for_engine_shutdown(stop: oneshot::Receiver<()>, has_tun: bool) -> loop { tokio::select! { _ = &mut stop => return Ok(()), - _ = telemetry.tick() => observe_ios_tun_memory(true, "running"), + _ = telemetry.tick() => observe_apple_tun_memory(true, "running"), } } } @@ -1471,12 +1557,18 @@ fn reset_failed_operation( } LifecycleState::Stopped => {} } - observe_ios_tun_memory(had_tun, telemetry_stage); + observe_apple_tun_memory(had_tun, telemetry_stage); clear_instance_leases(inner); Ok(()) } -#[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] +#[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" +))] fn vcore_to_io(error: VCoreError) -> io::Error { match error { VCoreError::Io(error) => error, @@ -1549,7 +1641,13 @@ mod tests { sync::{Arc, Barrier, Mutex}, }; - #[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] + #[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" + ))] use std::os::{fd::AsRawFd, unix::net::UnixDatagram}; use super::*; @@ -1928,6 +2026,89 @@ rules: assert!(json["error"].as_str().unwrap().contains("runtime thread")); } + #[test] + fn engine_workers_preserve_invoke_admission_and_shutdown() { + use crate::resources::observation::{self, ResourceKind, ResourceProbe}; + + let (sent, received) = std::sync::mpsc::channel(); + let thread = thread::spawn(move || { + let result = catch_unwind(AssertUnwindSafe(|| { + let defaults = tokio::runtime::Builder::new_multi_thread().build().unwrap(); + let default_workers = defaults.metrics().num_workers(); + drop(defaults); + + let _engine_admission = RuntimeThreadGuard::enter(); + let runtime = engine_runtime_builder() + .enable_io() + .enable_time() + .build() + .unwrap(); + assert_eq!( + runtime.handle().runtime_flavor(), + tokio::runtime::RuntimeFlavor::MultiThread, + ); + assert_eq!(runtime.metrics().num_workers(), default_workers); + let rejected = || { + assert!(is_runtime_thread()); + let response: Value = serde_json::from_slice(&invoke_bytes( + br#"{"apiVersion":5,"method":"version","payload":{}}"#, + )) + .unwrap(); + assert_failure(&response); + assert!( + response["error"] + .as_str() + .unwrap() + .contains("runtime thread") + ); + }; + let probe = ResourceProbe::default(); + // Keep the handle outside block_on so Runtime::drop, rather + // than awaiting the deliberately pending task, proves shutdown. + #[allow(clippy::async_yields_async)] + let pending = probe.scope_sync(|| { + runtime.block_on(async { + rejected(); + let asynchronous = tokio::spawn(async move { rejected() }); + let blocking = tokio::task::spawn_blocking(rejected); + tokio::time::timeout(Duration::from_secs(1), async { + asynchronous.await.unwrap(); + blocking.await.unwrap(); + }) + .await + .unwrap(); + let (started, ready) = oneshot::channel(); + let pending = observation::spawn(async move { + let _session = observation::track(ResourceKind::Session); + let _ = started.send(()); + std::future::pending::<()>().await; + }); + tokio::time::timeout(Duration::from_secs(1), ready) + .await + .unwrap() + .unwrap(); + pending + }) + }); + assert_eq!(probe.snapshot().current(ResourceKind::Task), 1); + assert_eq!(probe.snapshot().current(ResourceKind::Session), 1); + drop(runtime); + assert!(pending.is_finished()); + drop(pending); + assert!(probe.snapshot().is_idle()); + assert!(is_runtime_thread(), "worker exit cleared the engine marker"); + })); + let _ = sent.send(result); + }); + let result = received + .recv_timeout(Duration::from_secs(3)) + .expect("runtime admission/shutdown regression exceeded three seconds"); + thread.join().unwrap(); + if let Err(panic) = result { + std::panic::resume_unwind(panic); + } + } + #[test] fn null_invalid_utf8_and_oversized_input_return_json_failures() { let _guard = TEST_LOCK.lock().unwrap(); @@ -1983,7 +2164,11 @@ rules: let presence = start_field_presence(&null_value).unwrap(); assert!(validate_start_tun(false, null, presence).is_err()); - let tun_framing = if cfg!(any(target_os = "ios", target_os = "macos")) { + let tun_framing = if cfg!(any( + target_os = "ios", + target_os = "tvos", + target_os = "macos" + )) { "utun" } else { "rawIp" @@ -1991,24 +2176,46 @@ rules: let tun_value = json!({"tunFd": 7, "tunFraming": tun_framing}); let tun: StartPayload = decode_payload(tun_value.clone()).unwrap(); let presence = start_field_presence(&tun_value).unwrap(); - #[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] + #[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" + ))] assert_eq!( validate_start_tun(true, tun, presence).unwrap(), Some(( 7, - if cfg!(any(target_os = "ios", target_os = "macos")) { + if cfg!(any( + target_os = "ios", + target_os = "tvos", + target_os = "macos" + )) { TunFraming::Utun } else { TunFraming::RawIp } )) ); - #[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))] + #[cfg(not(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" + )))] assert!(validate_start_tun(true, tun, presence).is_err()); - #[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] + #[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos", + target_os = "linux" + ))] { - let wrong_framing = if cfg!(target_os = "android") { + let wrong_framing = if cfg!(any(target_os = "android", target_os = "linux")) { "utun" } else { "rawIp" @@ -2233,7 +2440,7 @@ rules: #[cfg(target_os = "linux")] #[test] - fn linux_runtime_start_fails_closed() { + fn linux_tun_start_rejects_non_tun_fd_and_preserves_prepared_state() { let _guard = TEST_LOCK.lock().unwrap(); reset_registry(); let _directory = initialize_test_data_directory(); @@ -2241,29 +2448,56 @@ rules: let prepared = request( "prepare", Some(&instance_id), - json!({"configYaml": http_config(free_ports(1)[0])}), + json!({"configYaml": tun_config()}), ); assert_eq!(prepared["success"], true, "{prepared}"); - let started = request("start", Some(&instance_id), json!({})); - assert_failure(&started); - assert!( - started["error"] - .as_str() - .unwrap() - .contains("unsupported on Linux") - ); - assert_eq!(state(&instance_id)["data"]["state"], "prepared"); + let (original, peer) = UnixDatagram::pair().unwrap(); + original.set_nonblocking(true).unwrap(); + // SAFETY: original stays open throughout the failed starts and Stop. + let before = unsafe { libc::fcntl(original.as_raw_fd(), libc::F_GETFL) }; + for framing in ["utun", "rawIp", "rawIp"] { + let started = request( + "start", + Some(&instance_id), + json!({"tunFd": original.as_raw_fd(), "tunFraming": framing}), + ); + assert_failure(&started); + let error = started["error"].as_str().unwrap(); + assert!( + error.contains(if framing == "utun" { + "Linux TUN requires rawIp" + } else { + "Linux TUNGETIFF" + }), + "{started}" + ); + assert_eq!(state(&instance_id)["data"]["state"], "prepared"); + } assert_eq!( request("stop", Some(&instance_id), json!({}))["success"], true ); destroy_instance(&instance_id); + // SAFETY: VCore never owns or changes the borrowed host descriptor. + assert_eq!( + unsafe { libc::fcntl(original.as_raw_fd(), libc::F_GETFL) }, + before + ); + original.send(b"ok").unwrap(); + let mut bytes = [0; 2]; + assert_eq!(peer.recv(&mut bytes).unwrap(), 2); + assert_eq!(&bytes, b"ok"); assert_registry_is_idle(); } - #[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))] + #[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "tvos", + target_os = "macos" + ))] #[test] fn prepare_start_stop_is_instance_scoped_and_borrows_tun_fd() { let _guard = TEST_LOCK.lock().unwrap(); @@ -2295,7 +2529,11 @@ rules: let (original, peer) = UnixDatagram::pair().unwrap(); original.set_nonblocking(true).unwrap(); - let tun_framing = if cfg!(any(target_os = "ios", target_os = "macos")) { + let tun_framing = if cfg!(any( + target_os = "ios", + target_os = "tvos", + target_os = "macos" + )) { "utun" } else { "rawIp" diff --git a/src/geodata/manager.rs b/src/geodata/manager.rs index 6657aec..557e365 100644 --- a/src/geodata/manager.rs +++ b/src/geodata/manager.rs @@ -75,6 +75,10 @@ impl std::fmt::Debug for DynamicGeoData { } impl GeoMatcher for DynamicGeoData { + fn routing_snapshot(&self) -> Option> { + Some(self.current.load_full()) + } + fn geosite_available(&self, code: &str) -> bool { self.current.load().geosite_available(code) } @@ -199,12 +203,6 @@ pub enum GeoDataManagerError { UpdateBusy, #[error("GeoData manager already has an active registration")] RegistrationActive, - #[error("GeoData update for {kind} is {actual} bytes; limit is {maximum} bytes")] - FileTooLarge { - kind: GeoDataKind, - actual: u64, - maximum: u64, - }, #[error("GeoData updater reported {reported} bytes but staged file contains {actual} bytes")] SizeMismatch { reported: u64, actual: u64 }, #[error("GeoData update ETag exceeds {MAX_ETAG_BYTES} bytes")] @@ -884,13 +882,6 @@ impl GeoUpdateSession { actual: actual_size, }); } - if actual_size > self.kind.file_limit() { - return Err(GeoDataManagerError::FileTooLarge { - kind: self.kind, - actual: actual_size, - maximum: self.kind.file_limit(), - }); - } self.manager.validate_candidate( self.kind, @@ -953,6 +944,23 @@ fn load_snapshot( let mut site_report = GeoDataResourceReport::not_required(); let mut ip_report = GeoDataResourceReport::not_required(); + if requirements.requires(GeoDataKind::GeoIp) { + match load_ips_for_requirements( + asset_dir, + requirements.code_set(GeoDataKind::GeoIp), + used, + peak, + ) { + Ok(loaded) => { + ips = loaded.values; + used = loaded.used; + peak = loaded.peak; + ip_report = GeoDataResourceReport::available(); + } + // A failed kind does not disable independent GeoSite routing. + Err(error) => ip_report = GeoDataResourceReport::degraded(error), + } + } if requirements.requires(GeoDataKind::GeoSite) { match load_sites_for_requirements( asset_dir, @@ -969,29 +977,8 @@ fn load_snapshot( Err(error) => site_report = GeoDataResourceReport::degraded(error), } } - if requirements.requires(GeoDataKind::GeoIp) { - match load_ips_for_requirements( - asset_dir, - requirements.code_set(GeoDataKind::GeoIp), - used, - peak, - ) { - Ok(loaded) => { - ips = loaded.values; - used = loaded.used; - peak = loaded.peak; - ip_report = GeoDataResourceReport::available(); - } - Err(error) => ip_report = GeoDataResourceReport::degraded(error), - } - } - let snapshot = Arc::new(GeoData { - sites, - ips, - allocation_capacity: used, - peak_allocation_capacity: peak, - }); + let snapshot = Arc::new(GeoData::from_loaded(sites, ips, used, peak)); ( snapshot, GeoDataLoadReport { @@ -1327,7 +1314,7 @@ mod tests { use tempfile::tempdir; - use super::super::{GENERAL_ALLOCATION_BUDGET_BYTES, GeoDataError}; + use super::super::GeoDataError; use super::*; use crate::config::{DnsNameserverPolicy, RuleAction, RuleKind, RuleSpec}; @@ -1948,8 +1935,6 @@ mod tests { .register(GeoRequirements::collect(&[], &[]).unwrap()) .unwrap(); assert_eq!(registration.initial_report().allocation_capacity, 0); - assert!( - registration.initial_report().allocation_capacity <= GENERAL_ALLOCATION_BUDGET_BYTES - ); + assert_eq!(registration.initial_report().peak_allocation_capacity, 0); } } diff --git a/src/geodata/mod.rs b/src/geodata/mod.rs index 68ca2c5..f9b9d72 100644 --- a/src/geodata/mod.rs +++ b/src/geodata/mod.rs @@ -1,4 +1,4 @@ -//! Bounded Xray GeoData loading and allocation-free matching. +//! Selective Xray GeoData loading with compact, shared category matchers. //! //! The loader deliberately scans protobuf wire framing itself. It indexes only //! category ranges during the first pass and decodes records only for codes @@ -14,22 +14,23 @@ use std::{ str, }; -use regex_automata::{ - Input, - dfa::{Automaton, StartKind, dense}, -}; +use regex::bytes::{Regex, RegexBuilder}; use thiserror::Error; use crate::{ VCoreError, config::{DnsNameserverPolicy, RuleKind, RuleSpec}, - routing::{GeoMatcher, normalize_domain_name}, + routing::GeoMatcher, }; mod manager; +mod selectors; pub(crate) mod service; pub(crate) mod updater; +pub(crate) use selectors::normalize_selector; +use selectors::{fold_attribute, selector_attributes, selector_code}; + pub use manager::{ DynamicGeoData, GeoDataLoadReport, GeoDataManager, GeoDataManagerError, GeoDataRegistration, GeoDataReloadReport, GeoDataResourceReport, GeoDataStatus, GeoResourceState, GeoUpdateSession, @@ -37,18 +38,6 @@ pub use manager::{ pub const GEOSITE_FILE_NAME: &str = "geosite.dat"; pub const GEOIP_FILE_NAME: &str = "geoip.dat"; -pub const MAX_GEOSITE_FILE_BYTES: u64 = 16 * 1024 * 1024; -pub const MAX_GEOIP_FILE_BYTES: u64 = 32 * 1024 * 1024; -pub const MAX_CATEGORIES_PER_FILE: usize = 4_096; -pub const MAX_REFERENCED_CODES: usize = 16; -pub const MAX_DOMAIN_RECORDS: usize = 65_536; -pub const MAX_DOMAIN_VALUE_BYTES: usize = 2 * 1024 * 1024; -pub const MAX_REGEX_RECORDS: usize = 512; -pub const MAX_REGEX_SOURCE_BYTES: usize = 64 * 1024; -pub const MAX_REGEX_MEMORY_BYTES: usize = 512 * 1024; -pub const MAX_REGEX_DETERMINIZE_MEMORY_BYTES: usize = 3 * 1024 * 1024; -pub const MAX_CIDR_RECORDS: usize = 320_000; -pub const GENERAL_ALLOCATION_BUDGET_BYTES: usize = 8 * 1024 * 1024; const MAX_CODE_BYTES: usize = 64; @@ -67,14 +56,6 @@ impl GeoDataKind { Self::GeoIp => GEOIP_FILE_NAME, } } - - #[must_use] - pub const fn file_limit(self) -> u64 { - match self { - Self::GeoSite => MAX_GEOSITE_FILE_BYTES, - Self::GeoIp => MAX_GEOIP_FILE_BYTES, - } - } } impl std::fmt::Display for GeoDataKind { @@ -96,16 +77,6 @@ pub enum GeoDataError { }, #[error("GeoData asset is not a regular file: {0}")] NotRegularFile(PathBuf), - #[error("{kind} asset is {actual} bytes; limit is {maximum} bytes")] - FileTooLarge { - kind: GeoDataKind, - actual: u64, - maximum: u64, - }, - #[error("{kind} contains more than {maximum} top-level categories")] - TooManyCategories { kind: GeoDataKind, maximum: usize }, - #[error("rules reference more than {maximum} unique GeoData codes")] - TooManyReferencedCodes { maximum: usize }, #[error("invalid {kind} category code `{code}`")] InvalidCode { kind: GeoDataKind, code: String }, #[error("duplicate {kind} category code `{code}`")] @@ -114,8 +85,6 @@ pub enum GeoDataError { MissingCode { kind: GeoDataKind, code: String }, #[error("malformed {kind} protobuf: {detail}")] Malformed { kind: GeoDataKind, detail: String }, - #[error("{kind} category `{code}` enables unsupported reverse_match")] - ReverseMatch { kind: GeoDataKind, code: String }, #[error("invalid GeoSite record in `{code}`: {detail}")] InvalidDomain { code: String, detail: String }, #[error("invalid GeoSite Regex in `{code}`: {detail}")] @@ -128,10 +97,6 @@ pub enum GeoDataError { actual: usize, maximum: usize, }, - #[error( - "GeoData allocation capacity would reach {requested} bytes; applicable budget is {maximum} bytes" - )] - AllocationBudgetExceeded { requested: usize, maximum: usize }, #[error("GeoData allocation failed while reserving {bytes} bytes")] AllocationFailed { bytes: usize }, } @@ -142,42 +107,24 @@ impl From for VCoreError { } } -/// Explicit capacity ledger shared by every owned buffer built by one load. +/// Diagnostic capacity accounting, never a memory admission policy. /// -/// `used` counts live requested capacities and compiled DFA memory estimates; -/// `peak` proves that transient indexes and builders share the same budget as -/// the final matcher. -#[derive(Debug)] -pub struct AllocationBudget { - maximum: usize, +/// Includes VCore-owned matcher/index vectors and arenas, with old/new buffer +/// overlap during growth. Compiled regex programs and search caches are opaque +/// third-party allocations and are not counted, nor are allocator overhead or +/// the process peak; those require independent measurements. +#[derive(Debug, Default)] +struct AllocationLedger { used: usize, peak: usize, } -impl AllocationBudget { - #[must_use] - pub const fn new(maximum: usize) -> Self { - Self { - maximum, - used: 0, - peak: 0, - } - } - +impl AllocationLedger { fn reserve(&mut self, bytes: usize) -> Result<(), GeoDataError> { - let requested = - self.used - .checked_add(bytes) - .ok_or(GeoDataError::AllocationBudgetExceeded { - requested: usize::MAX, - maximum: self.maximum, - })?; - if requested > self.maximum { - return Err(GeoDataError::AllocationBudgetExceeded { - requested, - maximum: self.maximum, - }); - } + let requested = self + .used + .checked_add(bytes) + .ok_or(GeoDataError::AllocationFailed { bytes: usize::MAX })?; self.used = requested; self.peak = self.peak.max(requested); Ok(()) @@ -187,10 +134,6 @@ impl AllocationBudget { debug_assert!(bytes <= self.used); self.used = self.used.saturating_sub(bytes); } - - const fn available(&self) -> usize { - self.maximum.saturating_sub(self.used) - } } /// Prepared matchers for all GeoData codes referenced by one instance. @@ -229,67 +172,65 @@ impl GeoData { /// Loads only categories referenced by `rules` from the two fixed sibling /// assets in `config_dir`. - pub fn load( - config_dir: &Path, - rules: &[RuleSpec], - budget_bytes: usize, - ) -> Result { - Self::load_with_dns_policies(config_dir, rules, &[], budget_bytes) + pub fn load(config_dir: &Path, rules: &[RuleSpec]) -> Result { + Self::load_with_dns_policies(config_dir, rules, &[]) } /// Loads categories referenced by both business routing rules and DNS - /// nameserver policies. Duplicate codes share one prepared category and - /// the existing per-instance GeoData allocation ledger. + /// nameserver policies. Selectors with the same base code share the value + /// arena and expressions; identical intersections share compact indexes. pub fn load_with_dns_policies( config_dir: &Path, rules: &[RuleSpec], dns_policies: &[DnsNameserverPolicy], - budget_bytes: usize, ) -> Result { let requested = RequestedCodes::collect(rules, dns_policies)?; if requested.total() == 0 { return Ok(Self::EMPTY); } - let mut budget = AllocationBudget::new(budget_bytes.min(GENERAL_ALLOCATION_BUDGET_BYTES)); - let mut counters = Counters::default(); + let mut ledger = AllocationLedger::default(); let mut sites = Vec::new(); let mut ips = Vec::new(); - ensure_vec_capacity(&mut sites, requested.sites.len, &mut budget)?; - ensure_vec_capacity(&mut ips, requested.ips.len, &mut budget)?; + ensure_vec_capacity(&mut sites, requested.sites.values.len(), &mut ledger)?; + ensure_vec_capacity(&mut ips, requested.ips.values.len(), &mut ledger)?; - if requested.sites.len != 0 { - load_site_file( - config_dir, - &requested.sites, - &mut sites, - &mut counters, - &mut budget, - )?; + if !requested.ips.values.is_empty() { + load_ip_file(config_dir, &requested.ips, &mut ips, &mut ledger)?; } - if requested.ips.len != 0 { - load_ip_file( - config_dir, - &requested.ips, - &mut ips, - &mut counters, - &mut budget, - )?; + if !requested.sites.values.is_empty() { + load_site_file(config_dir, &requested.sites, &mut sites, &mut ledger)?; } - - Ok(Self { - sites, - ips, - allocation_capacity: budget.used, - peak_allocation_capacity: budget.peak, - }) + Ok(Self::from_loaded(sites, ips, ledger.used, ledger.peak)) } + fn from_loaded( + mut sites: Vec, + mut ips: Vec, + allocation_capacity: usize, + peak_allocation_capacity: usize, + ) -> Self { + // Sort categories, never routing rules. Both standalone loading and + // manager reloads share this allocation-free indexing step. + sites.sort_unstable_by_key(|category| category.code); + ips.sort_unstable_by_key(|category| category.code); + Self { + sites, + ips, + allocation_capacity, + peak_allocation_capacity, + } + } + + /// Accounted retained matcher capacity, not allocator or process memory. #[must_use] pub const fn allocation_capacity(&self) -> usize { self.allocation_capacity } + /// Peak of accounted capacities, excluding compiled regexes/search caches and + /// configuration requirements. Neither an admission quota nor a bound on + /// actual loading or process memory. #[must_use] pub const fn peak_allocation_capacity(&self) -> usize { self.peak_allocation_capacity @@ -302,12 +243,28 @@ impl GeoData { #[must_use] pub fn geosite_available(&self, code: &str) -> bool { - self.sites.iter().any(|category| category.code.eq_str(code)) + self.site_category(code).is_some() } #[must_use] pub fn geoip_available(&self, code: &str) -> bool { - self.ips.iter().any(|category| category.code.eq_str(code)) + self.ip_category(code).is_some() + } + + fn site_category(&self, code: &str) -> Option<&SiteCategory> { + self.sites + .binary_search_by(|category| category.code.cmp_str(selector_code(code))) + .ok() + .map(|index| &self.sites[index]) + .filter(|category| category.has_selector(code)) + } + + fn ip_category(&self, code: &str) -> Option<&IpCategory> { + self.ips + .binary_search_by(|category| category.code.cmp_str(selector_code(code))) + .ok() + .map(|index| &self.ips[index]) + .filter(|category| category.has_selector(code)) } } @@ -321,17 +278,13 @@ impl GeoMatcher for GeoData { } fn matches_geosite(&self, code: &str, domain: &str) -> bool { - self.sites - .iter() - .find(|category| category.code.eq_str(code)) - .is_some_and(|category| category.matches(domain)) + self.site_category(code) + .is_some_and(|category| !domain.is_empty() && category.matches_selector(code, domain)) } fn matches_geoip(&self, code: &str, address: IpAddr) -> bool { - self.ips - .iter() - .find(|category| category.code.eq_str(code)) - .is_some_and(|category| category.matches(address)) + self.ip_category(code) + .is_some_and(|category| category.matches(address) != code.starts_with('!')) } } @@ -367,44 +320,56 @@ impl Code { Ok(code) } - fn parse_rule(kind: GeoDataKind, raw: &str) -> Result { - Self::parse(kind, raw.as_bytes()) - } - fn as_str(&self) -> &str { // Code::parse admits ASCII only. str::from_utf8(&self.bytes[..usize::from(self.len)]).expect("validated ASCII GeoData code") } - fn eq_str(&self, other: &str) -> bool { - self.as_str().eq_ignore_ascii_case(other) + fn cmp_str(&self, other: &str) -> std::cmp::Ordering { + self.as_str() + .bytes() + .cmp(other.bytes().map(|byte| byte.to_ascii_lowercase())) } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] struct CodeSet { - values: [Code; MAX_REFERENCED_CODES], - len: usize, + values: Vec, + selectors: Vec, } impl CodeSet { const EMPTY: Self = Self { - values: [Code::EMPTY; MAX_REFERENCED_CODES], - len: 0, + values: Vec::new(), + selectors: Vec::new(), }; - fn insert(&mut self, code: Code) -> Result { - if self.values[..self.len].contains(&code) { - return Ok(false); + fn insert(&mut self, kind: GeoDataKind, raw: &str) -> Result<(), GeoDataError> { + let selector = normalize_selector(kind, raw)?; + if self.selectors.contains(&selector) { + return Ok(()); } - if self.len == MAX_REFERENCED_CODES { - return Err(GeoDataError::TooManyReferencedCodes { - maximum: MAX_REFERENCED_CODES, - }); + self.selectors + .try_reserve(1) + .map_err(|_| GeoDataError::AllocationFailed { + bytes: mem::size_of::(), + })?; + let code = Code::parse(kind, selector_code(&selector).as_bytes())?; + self.selectors.push(selector); + if self.values.contains(&code) { + return Ok(()); } - self.values[self.len] = code; - self.len += 1; - Ok(true) + self.values + .try_reserve(1) + .map_err(|_| GeoDataError::AllocationFailed { + bytes: self + .values + .len() + .saturating_add(1) + .saturating_mul(mem::size_of::()), + })?; + self.values.push(code); + Ok(()) } } @@ -423,53 +388,30 @@ impl RequestedCodes { sites: CodeSet::EMPTY, ips: CodeSet::EMPTY, }; - let mut total = 0_usize; for rule in rules { - let inserted = match &rule.kind { - RuleKind::GeoSite(code) => requested - .sites - .insert(Code::parse_rule(GeoDataKind::GeoSite, code)?)?, - RuleKind::GeoIp(code) => requested - .ips - .insert(Code::parse_rule(GeoDataKind::GeoIp, code)?)?, - _ => false, - }; - if inserted { - total += 1; - if total > MAX_REFERENCED_CODES { - return Err(GeoDataError::TooManyReferencedCodes { - maximum: MAX_REFERENCED_CODES, - }); - } + match &rule.kind { + RuleKind::GeoSite(code) => requested.sites.insert(GeoDataKind::GeoSite, code)?, + RuleKind::GeoIp(code) => requested.ips.insert(GeoDataKind::GeoIp, code)?, + _ => (), } } for policy in dns_policies { for code in &policy.geosite_codes { - let inserted = requested - .sites - .insert(Code::parse_rule(GeoDataKind::GeoSite, code)?)?; - if inserted { - total += 1; - if total > MAX_REFERENCED_CODES { - return Err(GeoDataError::TooManyReferencedCodes { - maximum: MAX_REFERENCED_CODES, - }); - } - } + requested.sites.insert(GeoDataKind::GeoSite, code)?; } } Ok(requested) } const fn total(&self) -> usize { - self.sites.len + self.ips.len + self.sites.selectors.len() + self.ips.selectors.len() } } /// Normalized GeoData categories referenced by one VCore configuration. /// -/// Collection performs only configuration-owned validation: code syntax, -/// case-insensitive de-duplication, and the shared 16-code ceiling. Asset +/// Collection validates base code/selector syntax and canonicalizes attribute +/// intersections and case, without a reference-count quota. Asset /// availability and contents are deliberately handled later by /// [`GeoDataManager`], where one unavailable resource can become dormant /// without disabling the other kind. @@ -480,8 +422,8 @@ pub struct GeoRequirements { } impl GeoRequirements { - /// Collects all GeoSite and GeoIP codes used by business rules and DNS - /// nameserver policies. + /// Collects all GeoSite and GeoIP selectors used by business rules and DNS + /// nameserver policies. Each underlying category is decoded only once. pub fn collect( rules: &[RuleSpec], dns_policies: &[DnsNameserverPolicy], @@ -495,28 +437,29 @@ impl GeoRequirements { #[must_use] pub const fn is_empty(&self) -> bool { - self.sites.len == 0 && self.ips.len == 0 + self.sites.values.is_empty() && self.ips.values.is_empty() } #[must_use] pub const fn requires(&self, kind: GeoDataKind) -> bool { match kind { - GeoDataKind::GeoSite => self.sites.len != 0, - GeoDataKind::GeoIp => self.ips.len != 0, + GeoDataKind::GeoSite => !self.sites.values.is_empty(), + GeoDataKind::GeoIp => !self.ips.values.is_empty(), } } #[must_use] pub const fn total_codes(&self) -> usize { - self.sites.len + self.ips.len + self.sites.selectors.len() + self.ips.selectors.len() } + /// Returns canonical selector keys, which may include inversion/attributes. pub fn codes(&self, kind: GeoDataKind) -> impl Iterator { let set = match kind { GeoDataKind::GeoSite => &self.sites, GeoDataKind::GeoIp => &self.ips, }; - set.values[..set.len].iter().map(Code::as_str) + set.selectors.iter().map(String::as_str) } fn code_set(&self, kind: GeoDataKind) -> &CodeSet { @@ -539,24 +482,14 @@ fn load_sites_for_requirements( used: usize, peak: usize, ) -> Result, GeoDataError> { - let mut budget = AllocationBudget { - maximum: GENERAL_ALLOCATION_BUDGET_BYTES, - used, - peak, - }; + let mut ledger = AllocationLedger { used, peak }; let mut values = Vec::new(); - ensure_vec_capacity(&mut values, requested.len, &mut budget)?; - load_site_file( - asset_dir, - requested, - &mut values, - &mut Counters::default(), - &mut budget, - )?; + ensure_vec_capacity(&mut values, requested.values.len(), &mut ledger)?; + load_site_file(asset_dir, requested, &mut values, &mut ledger)?; Ok(KindLoad { values, - used: budget.used, - peak: budget.peak, + used: ledger.used, + peak: ledger.peak, }) } @@ -566,49 +499,30 @@ fn load_ips_for_requirements( used: usize, peak: usize, ) -> Result, GeoDataError> { - let mut budget = AllocationBudget { - maximum: GENERAL_ALLOCATION_BUDGET_BYTES, - used, - peak, - }; + let mut ledger = AllocationLedger { used, peak }; let mut values = Vec::new(); - ensure_vec_capacity(&mut values, requested.len, &mut budget)?; - load_ip_file( - asset_dir, - requested, - &mut values, - &mut Counters::default(), - &mut budget, - )?; + ensure_vec_capacity(&mut values, requested.values.len(), &mut ledger)?; + load_ip_file(asset_dir, requested, &mut values, &mut ledger)?; Ok(KindLoad { values, - used: budget.used, - peak: budget.peak, + used: ledger.used, + peak: ledger.peak, }) } fn validate_asset_structure(asset_dir: &Path, kind: GeoDataKind) -> Result<(), GeoDataError> { let (mut file, len) = open_asset(asset_dir, kind)?; - let mut budget = AllocationBudget::new(GENERAL_ALLOCATION_BUDGET_BYTES); - let _ = index_selected(&mut file, len, kind, &CodeSet::EMPTY, &mut budget)?; + let mut ledger = AllocationLedger::default(); + let _ = index_selected(&mut file, len, kind, &CodeSet::EMPTY, &mut ledger)?; Ok(()) } -#[derive(Debug, Default)] -struct Counters { - domain_records: usize, - domain_value_bytes: usize, - regex_records: usize, - regex_source_bytes: usize, - regex_memory: usize, - cidr_records: usize, -} - #[derive(Debug, Clone, Copy)] struct IndexEntry { code: Code, offset: u64, len: u64, + record_count: usize, } #[derive(Debug, Clone, Copy)] @@ -616,29 +530,28 @@ struct SelectedRange { code: Code, offset: u64, len: u64, -} - -impl SelectedRange { - const EMPTY: Self = Self { - code: Code::EMPTY, - offset: 0, - len: 0, - }; + record_count: usize, } fn load_site_file( config_dir: &Path, requested: &CodeSet, output: &mut Vec, - counters: &mut Counters, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result<(), GeoDataError> { let kind = GeoDataKind::GeoSite; let (mut file, len) = open_asset(config_dir, kind)?; - let selected = index_selected(&mut file, len, kind, requested, budget)?; - for range in &selected[..requested.len] { - output.push(parse_site_category(&mut file, *range, counters, budget)?); + let mut selected = index_selected(&mut file, len, kind, requested, ledger)?; + selected.sort_unstable_by_key(|range| range.code); + for range in &selected { + let selectors = requested + .selectors + .iter() + .filter(|selector| range.code.cmp_str(selector_code(selector)).is_eq()); + let category = parse_site_category(&mut file, *range, ledger, selectors)?; + output.push(category); } + release_vec(&selected, ledger); Ok(()) } @@ -646,18 +559,48 @@ fn load_ip_file( config_dir: &Path, requested: &CodeSet, output: &mut Vec, - counters: &mut Counters, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result<(), GeoDataError> { - let kind = GeoDataKind::GeoIp; - let (mut file, len) = open_asset(config_dir, kind)?; - let selected = index_selected(&mut file, len, kind, requested, budget)?; - for range in &selected[..requested.len] { - output.push(parse_ip_category(&mut file, *range, counters, budget)?); + load_selected_file( + GeoDataKind::GeoIp, + config_dir, + requested, + output, + ledger, + parse_ip_category, + )?; + for category in output.iter_mut() { + for selector in requested + .selectors + .iter() + .filter(|selector| category.code.cmp_str(selector_code(selector)).is_eq()) + { + ensure_vec_capacity(&mut category.selectors, 1, ledger)?; + ledger.reserve(selector.len())?; + category.selectors.push(selector.clone()); + } } Ok(()) } +fn load_selected_file( + kind: GeoDataKind, + config_dir: &Path, + requested: &CodeSet, + output: &mut Vec, + ledger: &mut AllocationLedger, + parse_category: fn(&mut File, SelectedRange, &mut AllocationLedger) -> Result, +) -> Result<(), GeoDataError> { + let (mut file, len) = open_asset(config_dir, kind)?; + let mut selected = index_selected(&mut file, len, kind, requested, ledger)?; + selected.sort_unstable_by_key(|range| range.code); + for range in &selected { + output.push(parse_category(&mut file, *range, ledger)?); + } + release_vec(&selected, ledger); + Ok(()) +} + fn open_asset(config_dir: &Path, kind: GeoDataKind) -> Result<(File, u64), GeoDataError> { let path = config_dir.join(kind.file_name()); let symlink_metadata = fs::symlink_metadata(&path).map_err(|source| GeoDataError::Io { @@ -686,13 +629,6 @@ fn open_asset(config_dir: &Path, kind: GeoDataKind) -> Result<(File, u64), GeoDa if !metadata.file_type().is_file() { return Err(GeoDataError::NotRegularFile(path)); } - if metadata.len() > kind.file_limit() { - return Err(GeoDataError::FileTooLarge { - kind, - actual: metadata.len(), - maximum: kind.file_limit(), - }); - } Ok((file, metadata.len())) } @@ -701,8 +637,8 @@ fn index_selected( file_len: u64, kind: GeoDataKind, requested: &CodeSet, - budget: &mut AllocationBudget, -) -> Result<[SelectedRange; MAX_REFERENCED_CODES], GeoDataError> { + ledger: &mut AllocationLedger, +) -> Result, GeoDataError> { seek(file, 0, kind)?; let mut index = Vec::::new(); while position(file, kind)? < file_len { @@ -716,18 +652,13 @@ fn index_selected( let entry_len = read_length(file, file_len, kind)?; let offset = position(file, kind)?; let end = checked_end(offset, entry_len, file_len, kind)?; - let code = scan_category_header(file, end, kind)?; - if index.len() == MAX_CATEGORIES_PER_FILE { - return Err(GeoDataError::TooManyCategories { - kind, - maximum: MAX_CATEGORIES_PER_FILE, - }); - } - ensure_vec_capacity(&mut index, 1, budget)?; + let (code, record_count) = scan_category_header(file, end, kind)?; + ensure_vec_capacity(&mut index, 1, ledger)?; index.push(IndexEntry { code, offset, len: entry_len, + record_count, }); seek(file, end, kind)?; } @@ -739,13 +670,14 @@ fn index_selected( kind, code: pair[0].code.as_str().to_owned(), }; - release_vec(&index, budget); + release_vec(&index, ledger); return Err(error); } } - let mut selected = [SelectedRange::EMPTY; MAX_REFERENCED_CODES]; - for (slot, code) in selected.iter_mut().zip(&requested.values[..requested.len]) { + let mut selected = Vec::new(); + ensure_vec_capacity(&mut selected, requested.values.len(), ledger)?; + for code in &requested.values { let entry = index .binary_search_by_key(code, |entry| entry.code) .ok() @@ -756,19 +688,21 @@ fn index_selected( }); match entry { Ok(entry) => { - *slot = SelectedRange { + selected.push(SelectedRange { code: entry.code, offset: entry.offset, len: entry.len, - }; + record_count: entry.record_count, + }); } Err(error) => { - release_vec(&index, budget); + release_vec(&index, ledger); + release_vec(&selected, ledger); return Err(error); } } } - release_vec(&index, budget); + release_vec(&index, ledger); Ok(selected) } @@ -776,9 +710,9 @@ fn scan_category_header( file: &mut File, end: u64, kind: GeoDataKind, -) -> Result { +) -> Result<(Code, usize), GeoDataError> { let mut code = None; - let mut reverse_match = false; + let mut record_count = 0_usize; while position(file, kind)? < end { let (field, wire) = read_key(file, end, kind)?; match (field, kind) { @@ -801,10 +735,19 @@ fn scan_category_header( (2, _) => { require_wire(kind, wire, 2, "category record")?; skip_field_payload(file, wire, end, kind)?; + record_count = + record_count + .checked_add(1) + .ok_or_else(|| GeoDataError::Malformed { + kind, + detail: "category record count does not fit usize".to_owned(), + })?; } (3, GeoDataKind::GeoIp) => { require_wire(kind, wire, 0, "reverse_match")?; - reverse_match |= read_varint(file, end, kind)? != 0; + // Mihomo GeoIP selectors get inversion from `!code`, not the + // legacy DAT flag, which its CIDR matcher does not inspect. + let _ = read_varint(file, end, kind)?; } _ => skip_field_payload(file, wire, end, kind)?, } @@ -813,16 +756,10 @@ fn scan_category_header( kind, detail: "category is missing code field 1".to_owned(), })?; - if reverse_match { - return Err(GeoDataError::ReverseMatch { - kind, - code: code.as_str().to_owned(), - }); - } - Ok(code) + Ok((code, record_count)) } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] enum SitePatternKind { Substr, Domain, @@ -836,39 +773,110 @@ struct SitePattern { len: u32, } +impl SitePattern { + fn value<'a>(&self, values: &'a [u8]) -> &'a [u8] { + let start = self.start as usize; + &values[start..start + self.len as usize] + } +} + struct SiteCategory { code: Code, patterns: Vec, values: Vec, - regexes: Vec>>, + regexes: Vec, + selectors: Vec, + filters: Vec, +} + +/// A filtered matcher owns only compact indexes. The category's values and +/// compiled expressions are shared by all filters and positive/negative aliases. +struct SiteFilter { + attributes: String, + all_records: bool, + patterns: Vec, + regex_indices: Vec, } impl SiteCategory { - fn matches(&self, domain: &str) -> bool { - for pattern in &self.patterns { - let start = pattern.start as usize; - let end = start + pattern.len as usize; - let value = str::from_utf8(&self.values[start..end]) - .expect("prepared GeoSite values are UTF-8"); - let matched = match pattern.kind { - SitePatternKind::Substr => domain.contains(value), - SitePatternKind::Domain => { - domain == value - || (domain.len() > value.len() - && domain.ends_with(value) - && domain.as_bytes()[domain.len() - value.len() - 1] == b'.') - } - SitePatternKind::Full => domain == value, + fn has_selector(&self, selector: &str) -> bool { + self.selectors + .iter() + .any(|requested| requested.eq_ignore_ascii_case(selector)) + } + + fn contains(&self, patterns: &[SitePattern], kind: SitePatternKind, value: &[u8]) -> bool { + patterns + .binary_search_by(|pattern| { + pattern + .kind + .cmp(&kind) + .then_with(|| pattern.value(&self.values).cmp(value)) + }) + .is_ok() + } + + fn matches_selector(&self, selector: &str, domain: &str) -> bool { + let attributes = selector_attributes(selector); + let filter = self + .filters + .iter() + .find(|filter| filter.attributes.eq_ignore_ascii_case(attributes)); + let matched = if attributes.is_empty() { + self.matches(domain, &self.patterns, None) + } else if let Some(filter) = filter { + if filter.all_records { + self.matches(domain, &self.patterns, None) + } else { + self.matches(domain, &filter.patterns, Some(&filter.regex_indices)) + } + } else { + return false; + }; + matched != selector.starts_with('!') + } + + fn matches( + &self, + domain: &str, + patterns: &[SitePattern], + regex_indices: Option<&[usize]>, + ) -> bool { + // The loader validates/normalizes each value once. Reuse the compact + // ranges as a sorted index; a miss must not scan all Domain/Full records + // for every UDP packet. Category membership is a union, not rule order. + let bytes = domain.as_bytes(); + if self.contains(patterns, SitePatternKind::Full, bytes) { + return true; + } + let mut suffix = bytes; + loop { + if self.contains(patterns, SitePatternKind::Domain, suffix) { + return true; + } + let Some(dot) = suffix.iter().position(|byte| *byte == b'.') else { + break; }; - if matched { + suffix = &suffix[dot + 1..]; + } + for pattern in patterns + .iter() + .take_while(|pattern| pattern.kind == SitePatternKind::Substr) + { + let value = pattern.value(&self.values); + if bytes.windows(value.len()).any(|window| window == value) { return true; } } - self.regexes.iter().any(|regex| { - regex - .try_search_fwd(&Input::new(domain.as_bytes())) - .is_ok_and(|matched| matched.is_some()) - }) + // The regex library owns its compiled representation and search cache; + // matching is not guaranteed to be allocation-free on first use. + let matches_regex = |regex: &Regex| regex.is_match(domain.as_bytes()); + match regex_indices { + Some(indices) => indices + .iter() + .any(|&index| matches_regex(&self.regexes[index])), + None => self.regexes.iter().any(matches_regex), + } } } @@ -878,11 +886,11 @@ struct ValueRange { len: u32, } -fn parse_site_category( +fn parse_site_category<'a>( file: &mut File, range: SelectedRange, - counters: &mut Counters, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, + requested: impl Iterator, ) -> Result { let kind = GeoDataKind::GeoSite; let end = checked_end(range.offset, range.len, u64::MAX, kind)?; @@ -893,6 +901,51 @@ fn parse_site_category( let mut regex_ranges = Vec::new(); let mut scratch = Vec::new(); let mut seen_code = false; + let mut remaining_records = range.record_count; + let mut selectors = Vec::new(); + let mut filters: Vec = Vec::new(); + for selector in requested { + ensure_vec_capacity(&mut selectors, 1, ledger)?; + ledger.reserve(selector.len())?; + selectors.push(selector.clone()); + let attributes = selector_attributes(selector); + if !attributes.is_empty() && !filters.iter().any(|filter| filter.attributes == attributes) { + ensure_vec_capacity(&mut filters, 1, ledger)?; + ledger.reserve(attributes.len())?; + filters.push(SiteFilter { + attributes: attributes.to_owned(), + all_records: false, + patterns: Vec::new(), + regex_indices: Vec::new(), + }); + } + } + let unfiltered = selectors + .iter() + .any(|selector| selector_attributes(selector).is_empty()); + if filters.len() == 1 && !unfiltered { + // The union equals this one intersection. Its positive and negative + // aliases need no second literal or expression index at all. + filters[0].all_records = true; + } + // One reusable membership bitmap per category, not attribute objects + // retained for every record. Different selector intersections share values. + let mut attribute_keys = Vec::::new(); + for filter in &filters { + for attribute in filter.attributes.split('@') { + if !attribute_keys.iter().any(|key| key == attribute) { + ensure_vec_capacity(&mut attribute_keys, 1, ledger)?; + ledger.reserve(attribute.len())?; + attribute_keys.push(attribute.to_owned()); + } + } + } + let mut present = Vec::new(); + ensure_vec_capacity(&mut present, attribute_keys.len(), ledger)?; + present.resize(attribute_keys.len(), false); + let mut filter_matches = Vec::new(); + ensure_vec_capacity(&mut filter_matches, filters.len(), ledger)?; + filter_matches.resize(filters.len(), false); let result = (|| { while position(file, kind)? < end { @@ -911,21 +964,64 @@ fn parse_site_category( } 2 => { require_wire(kind, wire, 2, "Domain")?; + remaining_records = remaining_records.checked_sub(1).ok_or_else(|| { + GeoDataError::Malformed { + kind, + detail: "GeoSite record count changed between scan passes".to_owned(), + } + })?; let len = read_length(file, end, kind)?; let start = position(file, kind)?; let record_end = checked_end(start, len, end, kind)?; - parse_domain_record( - file, - record_end, - range.code, - &mut patterns, - &mut values, - &mut regex_bytes, - &mut regex_ranges, - &mut scratch, - counters, - budget, - )?; + if !filters.is_empty() { + present.fill(false); + scan_domain_attributes( + file, + record_end, + &attribute_keys, + &mut present, + &mut scratch, + ledger, + )?; + for (filter, matched) in filters.iter().zip(&mut filter_matches) { + *matched = filter.attributes.split('@').all(|attribute| { + let at = attribute_keys + .iter() + .position(|key| key == attribute) + .expect("collected selector attribute"); + present[at] + }); + } + seek(file, start, kind)?; + } + if unfiltered || filter_matches.iter().any(|matched| *matched) { + let pattern_start = patterns.len(); + let regex_start = regex_ranges.len(); + parse_domain_record( + file, + record_end, + range.code, + &mut patterns, + &mut values, + &mut regex_bytes, + &mut regex_ranges, + &mut scratch, + ledger, + )?; + for (filter, matched) in filters.iter_mut().zip(&filter_matches) { + if !matched || filter.all_records { + continue; + } + if let Some(pattern) = patterns.get(pattern_start) { + ensure_vec_capacity(&mut filter.patterns, 1, ledger)?; + filter.patterns.push(*pattern); + } + if regex_ranges.len() > regex_start { + ensure_vec_capacity(&mut filter.regex_indices, 1, ledger)?; + filter.regex_indices.push(regex_start); + } + } + } seek(file, record_end, kind)?; } _ => skip_field_payload(file, wire, end, kind)?, @@ -934,22 +1030,59 @@ fn parse_site_category( if !seen_code { return malformed(kind, "GeoSite is missing code"); } - compile_regex_set(range.code, ®ex_bytes, ®ex_ranges, counters, budget) + if remaining_records != 0 { + return malformed(kind, "GeoSite record count changed between scan passes"); + } + compile_regex_set(range.code, ®ex_bytes, ®ex_ranges, ledger) })(); - release_vec(&scratch, budget); - release_vec(®ex_ranges, budget); - release_vec(®ex_bytes, budget); + release_vec(&scratch, ledger); + release_vec(®ex_ranges, ledger); + release_vec(®ex_bytes, ledger); + for attribute in &attribute_keys { + ledger.release(attribute.capacity()); + } + release_vec(&attribute_keys, ledger); + release_vec(&present, ledger); + release_vec(&filter_matches, ledger); match result { - Ok(regexes) => Ok(SiteCategory { - code: range.code, - patterns, - values, - regexes, - }), + Ok(regexes) => { + // In-place sort does not duplicate the arena or retain a second + // index. Keep every record, including overlapping types/duplicates. + patterns.sort_unstable_by(|left, right| { + left.kind + .cmp(&right.kind) + .then_with(|| left.value(&values).cmp(right.value(&values))) + }); + for filter in &mut filters { + filter.patterns.sort_unstable_by(|left, right| { + left.kind + .cmp(&right.kind) + .then_with(|| left.value(&values).cmp(right.value(&values))) + }); + } + Ok(SiteCategory { + code: range.code, + patterns, + values, + regexes, + selectors, + filters, + }) + } Err(error) => { - release_vec(&patterns, budget); - release_vec(&values, budget); + release_vec(&patterns, ledger); + release_vec(&values, ledger); + for selector in &selectors { + ledger.release(selector.capacity()); + } + release_vec(&selectors, ledger); + for filter in &filters { + ledger.release(filter.attributes.capacity()); + release_vec(&filter.patterns, ledger); + release_vec(&filter.regex_indices, ledger); + } + release_vec(&filters, ledger); Err(error) } } @@ -965,16 +1098,8 @@ fn parse_domain_record( regex_bytes: &mut Vec, regex_ranges: &mut Vec, scratch: &mut Vec, - counters: &mut Counters, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result<(), GeoDataError> { - counters.domain_records = checked_limit_increment( - "GeoSite Domain records", - counters.domain_records, - 1, - MAX_DOMAIN_RECORDS, - )?; - let kind = GeoDataKind::GeoSite; let mut domain_type = 0_u64; let mut seen_type = false; @@ -1016,13 +1141,7 @@ fn parse_domain_record( value_range.ok_or_else(|| invalid_domain_error(code, "Domain.value is missing"))?; let value_len = usize::try_from(value_len) .map_err(|_| invalid_domain_error(code, "Domain.value length does not fit usize"))?; - counters.domain_value_bytes = checked_limit_increment( - "GeoSite value bytes", - counters.domain_value_bytes, - value_len, - MAX_DOMAIN_VALUE_BYTES, - )?; - read_scratch(file, value_offset, value_len, scratch, budget, kind)?; + read_scratch(file, value_offset, value_len, scratch, ledger, kind)?; let raw = str::from_utf8(scratch) .map_err(|_| invalid_domain_error(code, "Domain.value is not UTF-8"))?; @@ -1032,27 +1151,15 @@ fn parse_domain_record( return invalid_domain(code, "Substr value must be non-empty ASCII"); } scratch.make_ascii_lowercase(); - append_site_value(SitePatternKind::Substr, scratch, patterns, values, budget)?; + append_site_value(SitePatternKind::Substr, scratch, patterns, values, ledger)?; } 1 => { - counters.regex_records = checked_limit_increment( - "GeoSite Regex records", - counters.regex_records, - 1, - MAX_REGEX_RECORDS, - )?; - counters.regex_source_bytes = checked_limit_increment( - "GeoSite Regex source bytes", - counters.regex_source_bytes, - value_len, - MAX_REGEX_SOURCE_BYTES, - )?; validate_regex_source(&code, raw)?; let start = u32::try_from(regex_bytes.len()) .map_err(|_| invalid_domain_error(code, "Regex source offset overflow"))?; - ensure_vec_capacity(regex_bytes, scratch.len(), budget)?; + ensure_vec_capacity(regex_bytes, scratch.len(), ledger)?; regex_bytes.extend_from_slice(scratch); - ensure_vec_capacity(regex_ranges, 1, budget)?; + ensure_vec_capacity(regex_ranges, 1, ledger)?; regex_ranges.push(ValueRange { start, len: u32::try_from(scratch.len()) @@ -1060,27 +1167,41 @@ fn parse_domain_record( }); } 2 | 3 => { - // Account a bounded normalization output while both the reusable - // input scratch and final compact byte arena are live. - budget.reserve(253)?; - let normalized = normalize_domain_name(raw).map_err(|error| { - invalid_domain_error(code, &format!("invalid domain value: {error}")) + // These are static literal match values, not DNS lookup targets. + // Keep opaque ASCII (including non-hostname labels) without STD3 + // validation or per-record allocation. Preserve valid IDN support, + // but an IDNA failure must not discard a record or its category. + let raw = raw.strip_suffix('.').unwrap_or(raw); + if raw.is_empty() { + return invalid_domain(code, "Domain/Full value must be non-empty"); + } + let normalized = (!raw.is_ascii()).then(|| { + idna::domain_to_ascii(raw) + .ok() + .filter(|value| !value.is_empty()) + .unwrap_or_else(|| raw.to_lowercase()) }); - budget.release(253); - let normalized = normalized?; - budget.reserve(normalized.capacity())?; + let temporary_capacity = normalized.as_ref().map_or(0, String::capacity); + let value = if let Some(normalized) = normalized.as_deref() { + normalized.as_bytes() + } else { + scratch.truncate(raw.len()); + scratch.make_ascii_lowercase(); + scratch.as_slice() + }; + ledger.reserve(temporary_capacity)?; let result = append_site_value( if domain_type == 2 { SitePatternKind::Domain } else { SitePatternKind::Full }, - normalized.as_bytes(), + value, patterns, values, - budget, + ledger, ); - budget.release(normalized.capacity()); + ledger.release(temporary_capacity); result?; } other => { @@ -1109,7 +1230,7 @@ fn validate_regex_source(code: &Code, pattern: &str) -> Result<(), GeoDataError> /// Finds Rust-only inline flags without interpreting escaped text or character /// classes. Go and Rust share `i`, `m`, `s`, and `U`; the remaining syntax is -/// still validated by `regex-automata` during compilation. +/// still validated by the regex library during compilation. fn has_unsupported_inline_regex_flag(pattern: &[u8]) -> bool { let mut escaped = false; let mut in_class = false; @@ -1147,6 +1268,67 @@ fn has_unsupported_inline_regex_flag(pattern: &[u8]) -> bool { false } +fn scan_domain_attributes( + file: &mut File, + end: u64, + requested: &[String], + present: &mut [bool], + scratch: &mut Vec, + ledger: &mut AllocationLedger, +) -> Result<(), GeoDataError> { + let kind = GeoDataKind::GeoSite; + while position(file, kind)? < end { + let (field, wire) = read_key(file, end, kind)?; + if field != 3 { + skip_field_payload(file, wire, end, kind)?; + continue; + } + require_wire(kind, wire, 2, "Domain.attribute")?; + let len = read_length(file, end, kind)?; + let start = position(file, kind)?; + let attribute_end = checked_end(start, len, end, kind)?; + let mut key_range = None; + while position(file, kind)? < attribute_end { + let (field, wire) = read_key(file, attribute_end, kind)?; + match field { + 1 => { + require_wire(kind, wire, 2, "Attribute.key")?; + let len = read_length(file, attribute_end, kind)?; + let start = position(file, kind)?; + validate_utf8_bytes(file, len, attribute_end, kind)?; + // Protobuf singular string fields use the last occurrence. + key_range = Some((start, len)); + } + 2 | 3 => { + require_wire(kind, wire, 0, "Attribute value")?; + let _ = read_varint(file, attribute_end, kind)?; + } + _ => skip_field_payload(file, wire, attribute_end, kind)?, + } + } + if let Some((offset, len)) = key_range { + let len = usize::try_from(len) + .map_err(|_| malformed_error(kind, "attribute key length does not fit usize"))?; + read_scratch(file, offset, len, scratch, ledger, kind)?; + let key = str::from_utf8(scratch).expect("validated attribute UTF-8"); + if key.is_ascii() { + for (wanted, present) in requested.iter().zip(present.iter_mut()) { + *present |= wanted.eq_ignore_ascii_case(key); + } + } else { + let key = fold_attribute(key); + ledger.reserve(key.capacity())?; + for (wanted, present) in requested.iter().zip(present.iter_mut()) { + *present |= *wanted == key; + } + ledger.release(key.capacity()); + } + } + seek(file, attribute_end, kind)?; + } + Ok(()) +} + fn validate_attribute(file: &mut File, end: u64, kind: GeoDataKind) -> Result<(), GeoDataError> { while position(file, kind)? < end { let (field, wire) = read_key(file, end, kind)?; @@ -1231,16 +1413,16 @@ fn append_site_value( value: &[u8], patterns: &mut Vec, values: &mut Vec, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result<(), GeoDataError> { let start = u32::try_from(values.len()).map_err(|_| GeoDataError::ResourceLimit { resource: "GeoSite compact value offset", actual: values.len(), maximum: u32::MAX as usize, })?; - ensure_vec_capacity(values, value.len(), budget)?; + ensure_vec_capacity(values, value.len(), ledger)?; values.extend_from_slice(value); - ensure_vec_capacity(patterns, 1, budget)?; + ensure_vec_capacity(patterns, 1, ledger)?; patterns.push(SitePattern { kind, start, @@ -1257,112 +1439,48 @@ fn compile_regex_set( code: Code, bytes: &[u8], ranges: &[ValueRange], - counters: &mut Counters, - budget: &mut AllocationBudget, -) -> Result>>, GeoDataError> { - if ranges.is_empty() { - return Ok(Vec::new()); - } + ledger: &mut AllocationLedger, +) -> Result, GeoDataError> { let mut regexes = Vec::new(); - ensure_vec_capacity(&mut regexes, ranges.len(), budget)?; + ensure_vec_capacity(&mut regexes, ranges.len(), ledger)?; + // Use the library's normal byte-regex interface and default compiler/cache + // protections. VCore adds no expression count, source or memory quota, and + // does not request eager complete-DFA construction. Programs/search caches + // are opaque to the ledger; only this owned vector is accounted. for range in ranges { - let remaining = MAX_REGEX_MEMORY_BYTES.saturating_sub(counters.regex_memory); - if remaining == 0 { - let actual = counters.regex_memory.saturating_add(1); - release_regexes(&mut regexes, budget); - return Err(GeoDataError::ResourceLimit { - resource: "compiled GeoSite Regex memory", - actual, - maximum: MAX_REGEX_MEMORY_BYTES, - }); - } - let build_memory = budget.available().min(MAX_REGEX_DETERMINIZE_MEMORY_BYTES); - let dfa_build_limit = remaining.min(build_memory / 2); - if dfa_build_limit == 0 { - let requested = budget.used.saturating_add(1); - release_regexes(&mut regexes, budget); - return Err(GeoDataError::AllocationBudgetExceeded { - requested, - maximum: budget.maximum, - }); - } - let auxiliary_build_limit = build_memory - dfa_build_limit; - // Charge the whole compiler allowance before construction. Building - // each expression independently avoids a multi-pattern state-product - // explosion while runtime matching remains allocation-free. - budget.reserve(build_memory)?; - let mut builder = dense::Builder::new(); - builder.configure( - dense::Config::new() - .dfa_size_limit(Some(dfa_build_limit)) - .determinize_size_limit(Some(auxiliary_build_limit)) - .accelerate(false) - .start_kind(StartKind::Unanchored), - ); - // Routing domains are normalized to ASCII before matching. Compiling - // in byte mode preserves behavior on that alphabet and avoids states - // for Unicode characters that cannot occur in the haystack. - builder.syntax( - regex_automata::util::syntax::Config::new() - .unicode(false) - .utf8(false), - ); let start = range.start as usize; let end = start + range.len as usize; let pattern = str::from_utf8(&bytes[start..end]).expect("validated Regex UTF-8"); - let regex = builder - .build(pattern) + // Routing names are ASCII; keep byte-mode matching semantics while + // retaining the library defaults for compilation and lazy caches. + let result = RegexBuilder::new(pattern) + .unicode(false) + .build() .map_err(|error| GeoDataError::InvalidRegex { code: code.as_str().to_owned(), - detail: error.to_string(), + // Syntax diagnostics contain the original expression. Do not + // expose asset values through prepare/update errors or logs. + detail: match error { + regex::Error::Syntax(_) => "invalid regex syntax", + regex::Error::CompiledTooBig(_) => { + "compiled regex exceeds the library default size limit" + } + _ => "regex compilation failed", + } + .to_owned(), }); - let regex = match regex { - Ok(regex) => regex, + match result { + Ok(regex) => regexes.push(regex), Err(error) => { - budget.release(build_memory); - release_regexes(&mut regexes, budget); + release_vec(®exes, ledger); return Err(error); } - }; - let memory = regex.memory_usage(); - let regex_memory = checked_limit_increment( - "compiled GeoSite Regex memory", - counters.regex_memory, - memory, - MAX_REGEX_MEMORY_BYTES, - ); - let regex_memory = match regex_memory { - Ok(regex_memory) if memory <= build_memory => regex_memory, - Ok(_) => { - let requested = budget.used.saturating_add(memory - build_memory); - budget.release(build_memory); - release_regexes(&mut regexes, budget); - return Err(GeoDataError::AllocationBudgetExceeded { - requested, - maximum: budget.maximum, - }); - } - Err(error) => { - budget.release(build_memory); - release_regexes(&mut regexes, budget); - return Err(error); - } - }; - counters.regex_memory = regex_memory; - budget.release(build_memory - memory); - regexes.push(regex); + } } Ok(regexes) } -fn release_regexes(regexes: &mut Vec>>, budget: &mut AllocationBudget) { - for regex in regexes.iter() { - budget.release(regex.memory_usage()); - } - release_vec(regexes, budget); -} - #[derive(Debug, Clone, Copy)] #[repr(C, packed)] struct Cidr4 { @@ -1401,9 +1519,16 @@ struct IpCategory { code: Code, v4: Vec, v6: Vec, + selectors: Vec, } impl IpCategory { + fn has_selector(&self, selector: &str) -> bool { + self.selectors + .iter() + .any(|requested| requested.eq_ignore_ascii_case(selector)) + } + fn matches(&self, address: IpAddr) -> bool { match address { IpAddr::V4(address) => { @@ -1423,8 +1548,7 @@ impl IpCategory { fn parse_ip_category( file: &mut File, range: SelectedRange, - counters: &mut Counters, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result { let kind = GeoDataKind::GeoIp; let end = checked_end(range.offset, range.len, u64::MAX, kind)?; @@ -1432,7 +1556,7 @@ fn parse_ip_category( let mut v4 = Vec::new(); let mut v6 = Vec::new(); let mut seen_code = false; - let mut reverse_match = false; + let mut remaining_records = range.record_count; let result = (|| { while position(file, kind)? < end { let (field, wire) = read_key(file, end, kind)?; @@ -1450,21 +1574,18 @@ fn parse_ip_category( } 2 => { require_wire(kind, wire, 2, "CIDR")?; - counters.cidr_records = checked_limit_increment( - "GeoIP raw CIDR records", - counters.cidr_records, - 1, - MAX_CIDR_RECORDS, - )?; + remaining_records = remaining_records.checked_sub(1).ok_or_else(|| { + malformed_error(kind, "GeoIP record count changed between scan passes") + })?; let len = read_length(file, end, kind)?; let start = position(file, kind)?; let record_end = checked_end(start, len, end, kind)?; - parse_cidr_record(file, record_end, range.code, &mut v4, &mut v6, budget)?; + parse_cidr_record(file, record_end, range.code, &mut v4, &mut v6, ledger)?; seek(file, record_end, kind)?; } 3 => { require_wire(kind, wire, 0, "GeoIP.reverse_match")?; - reverse_match |= read_varint(file, end, kind)? != 0; + let _ = read_varint(file, end, kind)?; } _ => skip_field_payload(file, wire, end, kind)?, } @@ -1472,11 +1593,8 @@ fn parse_ip_category( if !seen_code { return malformed(kind, "GeoIP is missing code"); } - if reverse_match { - return Err(GeoDataError::ReverseMatch { - kind, - code: range.code.as_str().to_owned(), - }); + if remaining_records != 0 { + return malformed(kind, "GeoIP record count changed between scan passes"); } compact_v4(&mut v4); compact_v6(&mut v6); @@ -1487,10 +1605,11 @@ fn parse_ip_category( code: range.code, v4, v6, + selectors: Vec::new(), }), Err(error) => { - release_vec(&v4, budget); - release_vec(&v6, budget); + release_vec(&v4, ledger); + release_vec(&v6, ledger); Err(error) } } @@ -1502,7 +1621,7 @@ fn parse_cidr_record( code: Code, v4: &mut Vec, v6: &mut Vec, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result<(), GeoDataError> { let kind = GeoDataKind::GeoIp; let mut ip = [0_u8; 16]; @@ -1541,7 +1660,7 @@ fn parse_cidr_record( u32::from_be_bytes(ip[..4].try_into().expect("four-byte slice")), prefix as u8, ); - ensure_vec_capacity(v4, 1, budget)?; + ensure_vec_capacity(v4, 1, ledger)?; v4.push(Cidr4 { network: numeric.to_be_bytes(), prefix: prefix as u8, @@ -1549,7 +1668,7 @@ fn parse_cidr_record( } Some(16) if prefix <= 128 => { let numeric = mask_v6(u128::from_be_bytes(ip), prefix as u8); - ensure_vec_capacity(v6, 1, budget)?; + ensure_vec_capacity(v6, 1, ledger)?; v6.push(Cidr6 { network: numeric.to_be_bytes(), prefix: prefix as u8, @@ -1666,10 +1785,10 @@ fn read_scratch( offset: u64, len: usize, scratch: &mut Vec, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, kind: GeoDataKind, ) -> Result<(), GeoDataError> { - ensure_vec_capacity(scratch, len.saturating_sub(scratch.len()), budget)?; + ensure_vec_capacity(scratch, len.saturating_sub(scratch.len()), ledger)?; scratch.resize(len, 0); seek(file, offset, kind)?; let end = offset @@ -1678,27 +1797,10 @@ fn read_scratch( read_exact(file, scratch, end, kind) } -fn checked_limit_increment( - resource: &'static str, - current: usize, - additional: usize, - maximum: usize, -) -> Result { - let actual = current.saturating_add(additional); - if actual > maximum { - return Err(GeoDataError::ResourceLimit { - resource, - actual, - maximum, - }); - } - Ok(actual) -} - fn ensure_vec_capacity( values: &mut Vec, additional: usize, - budget: &mut AllocationBudget, + ledger: &mut AllocationLedger, ) -> Result<(), GeoDataError> { let required = values .len() @@ -1709,33 +1811,38 @@ fn ensure_vec_capacity( } let desired = required.checked_next_power_of_two().unwrap_or(required); let old_capacity = values.capacity(); - let requested_elements = desired - old_capacity; - let requested_bytes = requested_elements + // An allocator can move the buffer. Charge the entire new capacity while + // the old allocation is still live, not only the retained-size delta. + let requested_bytes = desired .checked_mul(mem::size_of::()) .ok_or(GeoDataError::AllocationFailed { bytes: usize::MAX })?; - budget.reserve(requested_bytes)?; + ledger.reserve(requested_bytes)?; let reserve_additional = desired - values.len(); if values.try_reserve_exact(reserve_additional).is_err() { - budget.release(requested_bytes); + ledger.release(requested_bytes); return Err(GeoDataError::AllocationFailed { bytes: requested_bytes, }); } - let actual_elements = values.capacity() - old_capacity; - match actual_elements.cmp(&requested_elements) { + let actual_elements = values.capacity(); + match actual_elements.cmp(&desired) { std::cmp::Ordering::Greater => { - budget.reserve((actual_elements - requested_elements) * mem::size_of::())?; + let extra = (actual_elements - desired) + .checked_mul(mem::size_of::()) + .ok_or(GeoDataError::AllocationFailed { bytes: usize::MAX })?; + ledger.reserve(extra)?; } std::cmp::Ordering::Less => { - budget.release((requested_elements - actual_elements) * mem::size_of::()); + ledger.release((desired - actual_elements) * mem::size_of::()); } std::cmp::Ordering::Equal => {} } + ledger.release(old_capacity * mem::size_of::()); Ok(()) } -fn release_vec(values: &Vec, budget: &mut AllocationBudget) { - budget.release(values.capacity().saturating_mul(mem::size_of::())); +fn release_vec(values: &Vec, ledger: &mut AllocationLedger) { + ledger.release(values.capacity().saturating_mul(mem::size_of::())); } fn read_key(file: &mut File, end: u64, kind: GeoDataKind) -> Result<(u32, u8), GeoDataError> { diff --git a/src/geodata/selectors.rs b/src/geodata/selectors.rs new file mode 100644 index 0000000..3090e9b --- /dev/null +++ b/src/geodata/selectors.rs @@ -0,0 +1,92 @@ +//! Configuration-owned GeoData selectors; asset category codes stay separate. + +use regex_syntax::hir::{ClassUnicode, ClassUnicodeRange}; + +use super::{Code, GeoDataError, GeoDataKind}; + +/// Canonicalizes the Mihomo `!code@attribute@attribute` selector subset. +/// Attribute order and duplicates do not change its set-intersection meaning. +pub(crate) fn normalize_selector(kind: GeoDataKind, raw: &str) -> Result { + let (reversed, positive) = raw + .strip_prefix('!') + .map_or((false, raw), |raw| (true, raw)); + let mut parts = positive.split('@'); + let code = Code::parse(kind, parts.next().unwrap_or_default().as_bytes())?; + let mut attributes = Vec::new(); + for attribute in parts { + if kind == GeoDataKind::GeoIp { + return Err(GeoDataError::InvalidCode { + kind, + code: raw.to_owned(), + }); + } + let attribute = attribute.trim(); + if attribute.is_empty() { + continue; + } + if attribute + .chars() + .any(|ch| ch.is_control() || ch.is_whitespace() || matches!(ch, '@' | ',')) + { + return Err(GeoDataError::InvalidCode { + kind, + code: raw.to_owned(), + }); + } + // Mihomo lowercases the selector first, then compares the DAT key + // with Unicode simple folding. Go's lowercase is one scalar (not + // Rust's full lowercase expansion, notably for U+0130). + let lowered: String = attribute + .chars() + .map(|ch| { + ch.to_lowercase() + .next() + .expect("non-empty lowercase mapping") + }) + .collect(); + attributes.push(fold_attribute(&lowered)); + } + attributes.sort_unstable(); + attributes.dedup(); + let mut normalized = String::new(); + if reversed { + normalized.push('!'); + } + normalized.push_str(code.as_str()); + for attribute in attributes { + normalized.push('@'); + normalized.push_str(&attribute); + } + Ok(normalized) +} + +/// Uses the dependency's official Unicode simple-fold tables, not lowercase +/// expansion: e.g. final sigma and sigma are equivalent, but dotted capital I +/// is not a simple-fold alias of ASCII i. ASCII canonical keys stay lowercase. +pub(super) fn fold_attribute(raw: &str) -> String { + raw.chars() + .map(|ch| { + if ch.is_ascii() && !matches!(ch, 'k' | 'K' | 's' | 'S') { + return ch.to_ascii_lowercase(); + } + let mut class = ClassUnicode::new([ClassUnicodeRange::new(ch, ch)]); + class.case_fold_simple(); + let representative = class.iter().next().expect("non-empty fold class").start(); + representative.to_ascii_lowercase() + }) + .collect() +} + +pub(super) fn positive_selector(raw: &str) -> &str { + raw.strip_prefix('!').unwrap_or(raw) +} + +pub(super) fn selector_code(raw: &str) -> &str { + positive_selector(raw).split('@').next().unwrap_or_default() +} + +pub(super) fn selector_attributes(raw: &str) -> &str { + positive_selector(raw) + .split_once('@') + .map_or("", |(_, attributes)| attributes) +} diff --git a/src/geodata/service.rs b/src/geodata/service.rs index 5488872..7ca09d2 100644 --- a/src/geodata/service.rs +++ b/src/geodata/service.rs @@ -151,7 +151,6 @@ impl GeoDataUpdateService { url: source_url.to_owned(), etag: etag.clone(), temporary_path: session.temporary_path().to_path_buf(), - size_limit: kind.file_limit(), timeout: DEFAULT_DOWNLOAD_TIMEOUT, cancellation, }; diff --git a/src/geodata/tests.rs b/src/geodata/tests.rs index 36e616b..774a398 100644 --- a/src/geodata/tests.rs +++ b/src/geodata/tests.rs @@ -3,7 +3,10 @@ use std::{fs, net::IpAddr}; use tempfile::tempdir; use super::*; -use crate::config::{DnsNameserverPolicy, RuleAction, RuleKind, RuleSpec}; +use crate::{ + config::{DnsNameserverPolicy, RuleAction, RuleKind, RuleSpec}, + routing::normalize_domain_name, +}; fn varint(mut value: u64) -> Vec { let mut output = Vec::new(); @@ -42,6 +45,16 @@ fn domain(domain_type: u64, value: &str) -> Vec { output } +fn attributed_domain(domain_type: u64, value: &str, attributes: &[(&str, u32, u64)]) -> Vec { + let mut record = domain(domain_type, value); + for (key, field, value) in attributes { + let mut attribute = field_bytes(1, key.as_bytes()); + attribute.extend(field_varint(*field, *value)); + record.extend(field_bytes(3, &attribute)); + } + record +} + fn site(code: &str, domains: &[Vec]) -> Vec { let mut output = field_bytes(1, code.as_bytes()); for domain in domains { @@ -113,12 +126,503 @@ fn write_asset(dir: &Path, name: &str, contents: &[u8]) { #[test] fn empty_rules_do_not_open_assets() { let dir = tempdir().unwrap(); - let data = GeoData::load(dir.path(), &[], 1).unwrap(); + let data = GeoData::load(dir.path(), &[]).unwrap(); assert!(data.is_empty()); assert_eq!(data.allocation_capacity(), 0); assert_eq!(data.peak_allocation_capacity(), 0); } +#[test] +fn selector_normalization_deduplicates_and_folds_unicode_attributes() { + assert_eq!( + normalize_selector(GeoDataKind::GeoSite, "!CN@ ads @@CN@Ads").unwrap(), + "!cn@ads@cn" + ); + for (left, right) in [("Σ", "ς"), ("σ", "ς"), ("ſ", "S"), ("K", "K"), ("µ", "Μ")] { + assert_eq!( + normalize_selector(GeoDataKind::GeoSite, &format!("cn@{left}")).unwrap(), + normalize_selector(GeoDataKind::GeoSite, &format!("cn@{right}")).unwrap(), + "{left} / {right}", + ); + } + assert_eq!( + normalize_selector(GeoDataKind::GeoSite, "cn@İ").unwrap(), + normalize_selector(GeoDataKind::GeoSite, "cn@i").unwrap(), + ); + assert_ne!(fold_attribute("İ"), fold_attribute("i")); + assert_eq!( + normalize_selector(GeoDataKind::GeoIp, "!CN").unwrap(), + "!cn" + ); + assert_eq!( + normalize_selector(GeoDataKind::GeoSite, "GOOGLE@!CN").unwrap(), + "google@!cn" + ); + for (kind, selector) in [ + (GeoDataKind::GeoIp, "cn@ads"), + (GeoDataKind::GeoSite, "!"), + (GeoDataKind::GeoSite, "!!cn"), + (GeoDataKind::GeoSite, "cn@a b"), + (GeoDataKind::GeoSite, "cn@a\u{0}b"), + ] { + assert!(normalize_selector(kind, selector).is_err(), "{selector:?}"); + } +} + +#[test] +fn attribute_intersection_inversion_and_all_pattern_kinds_share_one_base() { + let dir = tempdir().unwrap(); + let both = [("ADS", 2, 0), ("cn", 3, 0)]; + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[ + attributed_domain(0, "keyword", &both), + attributed_domain(1, "^regex[0-9]+\\.example$", &both), + attributed_domain(2, "domain.example", &both), + attributed_domain(3, "full.example", &both), + attributed_domain(3, "ads-only.example", &[("ads", 2, 1)]), + attributed_domain(3, "plain.example", &[]), + ], + )]), + ); + let rules = [ + rule(RuleKind::GeoSite("CN@ADS@CN".into())), + rule(RuleKind::GeoSite("!cn@cn@ads".into())), + rule(RuleKind::GeoSite("cn".into())), + rule(RuleKind::GeoSite("!cn".into())), + rule(RuleKind::GeoSite("cn@missing".into())), + rule(RuleKind::GeoSite("!cn@missing".into())), + ]; + let data = GeoData::load(dir.path(), &rules).unwrap(); + assert_eq!(data.sites.len(), 1); + assert_eq!(data.sites[0].patterns.len(), 5); + assert_eq!(data.sites[0].regexes.len(), 1); + assert_eq!(data.sites[0].filters.len(), 2); + let filter = data.sites[0] + .filters + .iter() + .find(|filter| filter.attributes == "ads@cn") + .unwrap(); + assert_eq!(filter.patterns.len(), 3); + assert_eq!(filter.regex_indices, [0]); + for name in [ + "keyword.example", + "regex42.example", + "domain.example", + "sub.domain.example", + "full.example", + ] { + assert!(data.matches_geosite("cn@ads@cn", name), "{name}"); + assert!(!data.matches_geosite("!cn@ads@cn", name), "{name}"); + } + for name in ["ads-only.example", "plain.example", "absent.example"] { + assert!(!data.matches_geosite("cn@ads@cn", name), "{name}"); + assert!(data.matches_geosite("!cn@ads@cn", name), "{name}"); + } + assert!(!data.matches_geosite("!cn", "")); + assert!(!data.matches_geosite("!cn@missing", "")); + assert!(data.geosite_available("cn@missing")); + assert!(!data.matches_geosite("cn@missing", "any.example")); + assert!(data.matches_geosite("!cn@missing", "any.example")); + assert!(!data.geosite_available("cn@not-requested")); + assert!(!data.matches_geosite("!absent", "any.example")); +} + +#[test] +fn attribute_filtered_load_skips_unselected_invalid_regex_and_shares_record_union() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[ + attributed_domain(1, "(?=unsupported)", &[("other", 2, 1)]), + attributed_domain(3, "both.example", &[("a", 2, 0), ("b", 3, 0)]), + attributed_domain(3, "a.example", &[("a", 2, 1)]), + attributed_domain(3, "b.example", &[("b", 2, 1)]), + ], + )]), + ); + let rules = [ + rule(RuleKind::GeoSite("cn@a".into())), + rule(RuleKind::GeoSite("!cn@a".into())), + rule(RuleKind::GeoSite("CN@A@A".into())), + rule(RuleKind::GeoSite("cn@b".into())), + ]; + let data = GeoData::load(dir.path(), &rules).unwrap(); + assert_eq!(data.sites.len(), 1); + assert_eq!(data.sites[0].patterns.len(), 3); + assert_eq!(data.sites[0].filters.len(), 2); + assert_eq!(data.sites[0].selectors.len(), 3); + assert!(data.matches_geosite("cn@a", "both.example")); + assert!(data.matches_geosite("cn@b", "both.example")); + assert!(data.matches_geosite("cn@a", "a.example")); + assert!(!data.matches_geosite("cn@b", "a.example")); + assert!(data.matches_geosite("cn@b", "b.example")); + assert!(data.matches_geosite("!cn@a", "b.example")); +} + +#[test] +fn dns_selectors_share_attributes_with_business_rules_and_reload() { + let dir = tempdir().unwrap(); + let key = "Σ"; + let selector = normalize_selector(GeoDataKind::GeoSite, "cn@ς").unwrap(); + let inverted = format!("!{selector}"); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[ + attributed_domain(3, "old.example", &[(key, 2, 0)]), + attributed_domain(3, "unselected.example", &[]), + ], + )]), + ); + let rules = [rule(RuleKind::GeoSite(selector.clone()))]; + let policies = [dns_policy(&[&inverted, &selector])]; + let requirements = GeoRequirements::collect(&rules, &policies).unwrap(); + assert_eq!(requirements.total_codes(), 2); + assert_eq!(requirements.sites.values.len(), 1); + let manager = GeoDataManager::open(dir.path(), std::time::Duration::from_secs(60)).unwrap(); + let registration = manager.register(requirements).unwrap(); + let matcher = registration.matcher(); + assert!(matcher.matches_geosite(&selector, "old.example")); + assert!(matcher.matches_geosite(&inverted, "unselected.example")); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[attributed_domain(3, "new.example", &[("σ", 3, 0)])], + )]), + ); + assert!(manager.reload().geosite_available); + assert!(!matcher.matches_geosite(&selector, "old.example")); + assert!(matcher.matches_geosite(&selector, "new.example")); + assert!(!matcher.matches_geosite(&inverted, "new.example")); +} + +#[test] +fn literal_bang_attribute_is_not_an_attribute_complement() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "google", + &[ + attributed_domain(3, "tagged.example", &[("!cn", 2, 0)]), + attributed_domain(3, "cn.example", &[("cn", 3, 0)]), + attributed_domain(3, "untagged.example", &[]), + ], + )]), + ); + let rules = [ + rule(RuleKind::GeoSite("google@!cn".into())), + rule(RuleKind::GeoSite("!google@!cn".into())), + ]; + let data = GeoData::load(dir.path(), &rules).unwrap(); + assert_eq!(data.sites[0].filters.len(), 1); + assert!(data.sites[0].filters[0].all_records); + assert!(data.sites[0].filters[0].patterns.is_empty()); + assert!(data.sites[0].filters[0].regex_indices.is_empty()); + assert!(data.matches_geosite("google@!cn", "tagged.example")); + for name in ["cn.example", "untagged.example"] { + assert!(!data.matches_geosite("google@!cn", name)); + assert!(data.matches_geosite("!google@!cn", name)); + } + assert!(!data.matches_geosite("!google@!cn", "tagged.example")); +} + +#[test] +fn selector_lowercase_and_attribute_key_simple_fold_follow_mihomo_order() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[ + attributed_domain(3, "ascii-i.example", &[("i", 2, 0)]), + attributed_domain(3, "dotted-i.example", &[("İ", 2, 0)]), + ], + )]), + ); + let selector = normalize_selector(GeoDataKind::GeoSite, "cn@İ").unwrap(); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite(selector.clone()))]).unwrap(); + assert!(data.matches_geosite(&selector, "ascii-i.example")); + assert!(!data.matches_geosite(&selector, "dotted-i.example")); +} + +#[test] +fn missing_inverted_category_is_unavailable_but_empty_filter_is_available() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("cn", &[attributed_domain(3, "untagged.example", &[])])]), + ); + let manager = GeoDataManager::open(dir.path(), std::time::Duration::from_secs(60)).unwrap(); + let registration = manager + .register( + GeoRequirements::collect(&[rule(RuleKind::GeoSite("!missing".into()))], &[]).unwrap(), + ) + .unwrap(); + let matcher = registration.matcher(); + assert!(!registration.initial_report().geosite.available); + assert!(!matcher.geosite_available("!missing")); + assert!(!matcher.matches_geosite("!missing", "any.example")); + drop(registration); + let registration = manager + .register( + GeoRequirements::collect(&[rule(RuleKind::GeoSite("!cn@missing".into()))], &[]) + .unwrap(), + ) + .unwrap(); + let matcher = registration.matcher(); + assert!(registration.initial_report().geosite.available); + assert!(matcher.geosite_available("!cn@missing")); + assert!(matcher.matches_geosite("!cn@missing", "any.example")); +} + +#[test] +fn inverted_ip_aliases_share_one_cidr_matcher_and_keep_all_sites() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOIP_FILE_NAME, + &geoip_list(&[geoip("cn", &[cidr(&[10, 0, 0, 0], 8)], true)]), + ); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[ + attributed_domain(3, "kept.example", &[("ads", 2, 0)]), + attributed_domain(3, "last.example", &[("ads", 2, 1)]), + ], + )]), + ); + let rules = [ + rule(RuleKind::GeoIp("!CN".into())), + rule(RuleKind::GeoIp("cn".into())), + rule(RuleKind::GeoSite("cn@ads".into())), + ]; + let data = GeoData::load(dir.path(), &rules).unwrap(); + assert_eq!(data.ips.len(), 1); + assert_eq!(data.ips[0].v4.len(), 1); + assert!(data.matches_geoip("!cn", "2001:db8::1".parse().unwrap())); + assert!(!data.matches_geoip("!cn", "10.2.3.4".parse().unwrap())); + assert!(data.matches_geosite("cn@ads", "kept.example")); + assert!(data.matches_geosite("cn@ads", "last.example")); +} + +#[test] +fn category_lookup_preserves_independent_codes_through_load_and_reload() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[ + site("Az", &[domain(3, "az.example")]), + site("AA", &[domain(3, "aa.example")]), + site("Z", &[domain(3, "z.example")]), + site("a-b", &[domain(3, "hyphen.example")]), + site("a", &[domain(3, "a.example")]), + ]), + ); + write_asset( + dir.path(), + GEOIP_FILE_NAME, + &geoip_list(&[ + geoip("a-B", &[cidr(&[10, 4, 0, 0], 16)], false), + geoip("z", &[cidr(&[10, 5, 0, 0], 16)], false), + geoip("a", &[cidr(&[10, 1, 0, 0], 16)], false), + geoip("AZ", &[cidr(&[10, 3, 0, 0], 16)], false), + geoip("aa", &[cidr(&[10, 2, 0, 0], 16)], false), + ]), + ); + let rules: Vec<_> = ["z", "a-b", "a", "az", "aa"] + .into_iter() + .flat_map(|code| { + [ + rule(RuleKind::GeoSite(code.to_owned())), + rule(RuleKind::GeoIp(code.to_owned())), + ] + }) + .collect(); + let data = GeoData::load(dir.path(), &rules).unwrap(); + let manager = GeoDataManager::open(dir.path(), std::time::Duration::from_secs(60)).unwrap(); + let registration = manager + .register(GeoRequirements::collect(&rules, &[]).unwrap()) + .unwrap(); + assert!(registration.initial_report().geosite.available); + assert!(registration.initial_report().geoip.available); + let matcher = registration.matcher(); + for loaded in [&data as &dyn GeoMatcher, matcher.as_ref()] { + for (code, domain, address) in [ + ("A", "a.example", "10.1.0.1"), + ("aA", "aa.example", "10.2.0.1"), + ("AZ", "az.example", "10.3.0.1"), + ("A-b", "hyphen.example", "10.4.0.1"), + ("z", "z.example", "10.5.0.1"), + ] { + assert!(loaded.geosite_available(code), "{code}"); + assert!(loaded.geoip_available(code), "{code}"); + assert!(loaded.matches_geosite(code, domain), "{code}"); + assert!( + loaded.matches_geoip(code, address.parse().unwrap()), + "{code}" + ); + assert!(!loaded.matches_geosite(code, "absent.example"), "{code}"); + assert!( + !loaded.matches_geoip(code, "192.0.2.1".parse().unwrap()), + "{code}" + ); + } + for code in ["", "0", "a-", "a-bb", "aaa", "ay", "zz", "a?", " a"] { + assert!(!loaded.geosite_available(code), "{code}"); + assert!(!loaded.geoip_available(code), "{code}"); + assert!(!loaded.matches_geosite(code, "a.example"), "{code}"); + assert!( + !loaded.matches_geoip(code, "10.1.0.1".parse().unwrap()), + "{code}" + ); + } + } + + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[ + site("a", &[domain(3, "new-a.example")]), + site("a-b", &[domain(3, "new-hyphen.example")]), + site("z", &[domain(3, "new-z.example")]), + site("aa", &[domain(3, "new-aa.example")]), + site("az", &[domain(3, "new-az.example")]), + ]), + ); + write_asset( + dir.path(), + GEOIP_FILE_NAME, + &geoip_list(&[ + geoip("aa", &[cidr(&[172, 2, 0, 0], 16)], false), + geoip("az", &[cidr(&[172, 3, 0, 0], 16)], false), + geoip("a", &[cidr(&[172, 1, 0, 0], 16)], false), + geoip("z", &[cidr(&[172, 5, 0, 0], 16)], false), + geoip("a-b", &[cidr(&[172, 4, 0, 0], 16)], false), + ]), + ); + let report = manager.reload(); + assert!(report.geosite_available); + assert!(report.geoip_available); + for (code, old_domain, new_domain, old_address, new_address) in [ + ("A", "a.example", "new-a.example", "10.1.0.1", "172.1.0.1"), + ( + "aA", + "aa.example", + "new-aa.example", + "10.2.0.1", + "172.2.0.1", + ), + ( + "AZ", + "az.example", + "new-az.example", + "10.3.0.1", + "172.3.0.1", + ), + ( + "A-b", + "hyphen.example", + "new-hyphen.example", + "10.4.0.1", + "172.4.0.1", + ), + ("z", "z.example", "new-z.example", "10.5.0.1", "172.5.0.1"), + ] { + assert!(!matcher.matches_geosite(code, old_domain), "{code}"); + assert!(matcher.matches_geosite(code, new_domain), "{code}"); + assert!( + !matcher.matches_geoip(code, old_address.parse().unwrap()), + "{code}" + ); + assert!( + matcher.matches_geoip(code, new_address.parse().unwrap()), + "{code}" + ); + } +} + +#[test] +fn geosite_loads_beyond_former_value_and_memory_quotas() { + let dir = tempdir().unwrap(); + let mut selected = field_bytes(1, b"cn"); + // Exceeds the former 2 MiB values and 8 MiB ledger budgets; selected + // records are not truncated on any platform. + for index in 0..131_073 { + selected.extend(field_bytes( + 2, + &domain( + 2, + &format!("long-domain-that-crosses-the-old-byte-budget-{index:x}.example.test"), + ), + )); + } + write_asset(dir.path(), GEOSITE_FILE_NAME, &site_list(&[selected])); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("cn".to_owned()))]).unwrap(); + for index in [0, 65_536, 131_071, 131_072] { + let value = format!("long-domain-that-crosses-the-old-byte-budget-{index:x}.example.test"); + assert!(data.matches_geosite("cn", &value)); + assert!(data.matches_geosite("cn", &format!("sub.{value}"))); + assert!(!data.matches_geosite("cn", &format!("not{value}"))); + } + assert!(data.allocation_capacity() > 8 * 1024 * 1024); + assert!(data.peak_allocation_capacity() >= data.allocation_capacity()); + drop(data); + + // Runtime preparation uses the manager's independently loaded snapshot. + let manager = GeoDataManager::open( + dir.path().join("managed"), + std::time::Duration::from_secs(60), + ) + .unwrap(); + fs::copy( + dir.path().join(GEOSITE_FILE_NAME), + manager.store_dir().join(GEOSITE_FILE_NAME), + ) + .unwrap(); + let registration = manager + .register(GeoRequirements::collect(&[rule(RuleKind::GeoSite("cn".into()))], &[]).unwrap()) + .unwrap(); + assert!(registration.initial_report().geosite.available); + assert!(registration.initial_report().allocation_capacity > 8 * 1024 * 1024); + assert!(registration.matcher().matches_geosite( + "cn", + "long-domain-that-crosses-the-old-byte-budget-20000.example.test" + )); +} + +#[test] +fn allocation_ledger_observes_growth_without_admission_limits() { + let mut ledger = AllocationLedger::default(); + let mut bytes = Vec::::new(); + ensure_vec_capacity(&mut bytes, 8, &mut ledger).unwrap(); + bytes.extend_from_slice(b"12345678"); + ensure_vec_capacity(&mut bytes, 1, &mut ledger).unwrap(); + assert_eq!(ledger.used, 16); + assert_eq!(ledger.peak, 24); + assert_eq!(bytes, b"12345678"); + assert!(ensure_vec_capacity(&mut bytes, usize::MAX, &mut ledger).is_err()); + assert_eq!(ledger.used, 16); +} + #[test] fn nameserver_policy_alone_loads_geosite_and_shares_rule_categories() { let dir = tempdir().unwrap(); @@ -129,43 +633,31 @@ fn nameserver_policy_alone_loads_geosite_and_shares_rule_categories() { write_asset(dir.path(), GEOSITE_FILE_NAME, &fixture); let policy = dns_policy(&["private", "cn"]); - let data = GeoData::load_with_dns_policies( - dir.path(), - &[], - std::slice::from_ref(&policy), - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap(); + let data = + GeoData::load_with_dns_policies(dir.path(), &[], std::slice::from_ref(&policy)).unwrap(); assert!(data.matches_geosite("private", "host.internal.example")); assert!(data.matches_geosite("cn", "www.example.cn")); assert_eq!(data.sites.len(), 2); - assert!(data.peak_allocation_capacity() <= GENERAL_ALLOCATION_BUDGET_BYTES); let data = GeoData::load_with_dns_policies( dir.path(), &[rule(RuleKind::GeoSite("private".to_owned()))], &[policy], - GENERAL_ALLOCATION_BUDGET_BYTES, ) .unwrap(); assert_eq!(data.sites.len(), 2); } #[test] -fn nameserver_policy_missing_category_and_combined_code_limit_fail_closed() { +fn nameserver_policy_missing_category_still_fails_closed() { let dir = tempdir().unwrap(); write_asset( dir.path(), GEOSITE_FILE_NAME, - &site_list(&[site("private", &[domain(2, "internal.example")])]), + &site_list(&[site("private", &[])]), ); - let error = GeoData::load_with_dns_policies( - dir.path(), - &[], - &[dns_policy(&["missing"])], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); + let error = + GeoData::load_with_dns_policies(dir.path(), &[], &[dns_policy(&["missing"])]).unwrap_err(); assert!(matches!( error, GeoDataError::MissingCode { @@ -173,22 +665,41 @@ fn nameserver_policy_missing_category_and_combined_code_limit_fail_closed() { .. } )); +} - let codes = (0..MAX_REFERENCED_CODES) - .map(|index| format!("site{index}")) - .collect::>(); - let policy = DnsNameserverPolicy { - geosite_codes: codes.into_boxed_slice(), - nameservers: Vec::new().into_boxed_slice(), - }; - let error = GeoData::load_with_dns_policies( +#[test] +fn references_and_asset_categories_grow_without_fixed_count_limits() { + let dir = tempdir().unwrap(); + let sites: Vec<_> = (0..4097) + .map(|index| { + site( + &format!("code{index}"), + &[domain(3, &format!("site{index}.test"))], + ) + }) + .collect(); + write_asset(dir.path(), GEOSITE_FILE_NAME, &site_list(&sites)); + write_asset( dir.path(), - &[rule(RuleKind::GeoIp("extra".to_owned()))], - &[policy], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); - assert!(matches!(error, GeoDataError::TooManyReferencedCodes { .. })); + GEOIP_FILE_NAME, + &geoip_list(&[geoip("cn", &[cidr(&[10, 0, 0, 0], 8)], false)]), + ); + let codes: Vec<_> = (0..17) + .map(|index| format!("code{index}")) + .chain(["code4096".to_owned()]) + .collect(); + let policy = dns_policy(&codes.iter().map(String::as_str).collect::>()); + let rules = [ + rule(RuleKind::GeoIp("cn".into())), + rule(RuleKind::GeoSite("CODE0".into())), + ]; + let requirements = GeoRequirements::collect(&rules, std::slice::from_ref(&policy)).unwrap(); + assert_eq!(requirements.total_codes(), 19); + let data = GeoData::load_with_dns_policies(dir.path(), &rules, &[policy]).unwrap(); + for index in (0..17).chain([4096]) { + assert!(data.matches_geosite(&format!("code{index}"), &format!("site{index}.test"))); + } + assert!(data.matches_geoip("cn", "10.1.2.3".parse().unwrap())); } #[test] @@ -212,12 +723,7 @@ fn geosite_supports_all_domain_types_and_attributes() { )]); write_asset(dir.path(), GEOSITE_FILE_NAME, &fixture); - let data = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoSite("test".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap(); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("test".to_owned()))]).unwrap(); assert!(data.matches_geosite("TEST", "has-needle.example")); assert!(data.matches_geosite("test", "example.com")); assert!(data.matches_geosite("test", "a.example.com")); @@ -227,8 +733,6 @@ fn geosite_supports_all_domain_types_and_attributes() { assert!(!data.matches_geosite("test", "R42.example")); assert!(data.matches_geosite("test", "prefix.other.test.example")); assert!(!data.matches_geosite("missing", "example.com")); - assert!(data.allocation_capacity() <= GENERAL_ALLOCATION_BUDGET_BYTES); - assert!(data.peak_allocation_capacity() <= GENERAL_ALLOCATION_BUDGET_BYTES); } #[test] @@ -236,16 +740,148 @@ fn geosite_normalizes_unicode_domain_values() { let dir = tempdir().unwrap(); let fixture = site_list(&[site("idna", &[domain(2, "例子.测试")])]); write_asset(dir.path(), GEOSITE_FILE_NAME, &fixture); - let data = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoSite("IDNA".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap(); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("IDNA".to_owned()))]).unwrap(); let normalized = normalize_domain_name("子.例子.测试").unwrap(); assert!(data.matches_geosite("idna", &normalized)); } +#[test] +fn geosite_literal_patterns_do_not_require_dns_hostname_syntax() { + let dir = tempdir().unwrap(); + let long_label = format!("{}.test", "x".repeat(64)); + let records = [ + domain(2, "EXAMPLE.TEST"), + domain(2, "\"QUOTED.TEST"), + domain(2, "FOO_BAR.TEST"), + domain(3, "FULL_VALUE.TEST"), + domain(2, "-EDGE-.TEST"), + domain(2, "PERCENT%VALUE.TEST"), + domain(2, "BACK\\SLASH.TEST"), + domain(2, &long_label), + domain(2, "*.LITERAL.TEST"), + ]; + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("compat", &records)]), + ); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("compat".into()))]).unwrap(); + assert!(data.geosite_available("compat")); + assert_eq!(data.sites[0].patterns.len(), records.len()); + for value in [ + "example.test", + "sub.example.test", + "\"quoted.test", + "sub.\"quoted.test", + "foo_bar.test", + "sub.foo_bar.test", + "full_value.test", + "-edge-.test", + "percent%value.test", + "back\\slash.test", + long_label.as_str(), + "*.literal.test", + ] { + assert!(data.matches_geosite("compat", value), "{value}"); + } + for value in [ + "quoted.test", + "notfoo_bar.test", + "sub.full_value.test", + "host.literal.test", + "absent.test", + ] { + assert!(!data.matches_geosite("compat", value), "{value}"); + } +} + +#[test] +fn geosite_preserves_utf8_patterns_when_idna_is_not_applicable() { + let dir = tempdir().unwrap(); + // A joiner without a valid IDNA context is still a static literal pattern. + let value = "\u{200d}.EXAMPLE"; + assert!(idna::domain_to_ascii(value).is_err()); + // Successful IDNA conversion can also erase an ignored-only value. + assert_eq!(idna::domain_to_ascii("\u{ad}").unwrap(), ""); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("opaque", &[domain(2, value), domain(3, "\u{ad}")])]), + ); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("opaque".into()))]).unwrap(); + assert!(data.matches_geosite("opaque", "\u{200d}.example")); + assert!(data.matches_geosite("opaque", "\u{ad}")); + assert!(!data.matches_geosite("opaque", "")); + assert!(!data.matches_geosite("opaque", "example")); +} + +#[test] +fn geosite_pattern_compatibility_does_not_accept_invalid_records() { + let dir = tempdir().unwrap(); + for record in [ + domain(2, ""), + domain(3, ""), + field_varint(1, 2), + [field_varint(1, 2), field_bytes(2, &[0xff])].concat(), + domain(4, "example.test"), + ] { + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("invalid", &[record])]), + ); + assert!(matches!( + GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("invalid".into()))]), + Err(GeoDataError::InvalidDomain { .. }) + )); + } +} + +#[test] +fn geosite_membership_preserves_overlapping_unsorted_record_types() { + let dir = tempdir().unwrap(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site( + "cn", + &[ + domain(3, "z.example"), + domain(2, "shared.example"), + domain(0, "needle"), + domain(3, "shared.example"), + domain(2, "a.example"), + domain(3, "only.example"), + domain(2, "shared.example"), + domain(1, "^r[0-9]+\\.test$"), + ], + )]), + ); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("cn".into()))]).unwrap(); + for value in [ + "z.example", + "shared.example", + "sub.shared.example", + "a.example", + "deep.sub.a.example", + "only.example", + "has-needle.test", + "r12.test", + ] { + assert!(data.matches_geosite("cn", value), "{value}"); + } + for value in [ + "sub.z.example", + "sub.only.example", + "notshared.example", + "shared.example.test", + "not-a.example", + "absent.test", + ] { + assert!(!data.matches_geosite("cn", value), "{value}"); + } +} + #[test] fn geoip_matches_v4_v6_and_compacts_siblings() { let dir = tempdir().unwrap(); @@ -264,12 +900,7 @@ fn geoip_matches_v4_v6_and_compacts_siblings() { false, )]); write_asset(dir.path(), GEOIP_FILE_NAME, &fixture); - let data = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoIp("PRIVATE".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap(); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoIp("PRIVATE".to_owned()))]).unwrap(); assert_eq!(data.ips[0].v4.len(), 1); assert!(data.matches_geoip("private", "10.255.4.1".parse().unwrap())); @@ -289,7 +920,6 @@ fn unselected_category_payload_is_not_decoded() { let data = GeoData::load( dir.path(), &[rule(RuleKind::GeoSite("selected".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, ) .unwrap(); assert!(data.matches_geosite("selected", "ok.example")); @@ -305,7 +935,6 @@ fn selected_corrupt_payload_fails_closed() { let error = GeoData::load( dir.path(), &[rule(RuleKind::GeoSite("selected".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, ) .unwrap_err(); assert!(matches!(error, GeoDataError::Malformed { .. })); @@ -319,12 +948,7 @@ fn duplicate_codes_are_ascii_case_insensitive() { GEOSITE_FILE_NAME, &site_list(&[site("CN", &[]), site("cn", &[])]), ); - let error = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoSite("cn".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); + let error = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("cn".to_owned()))]).unwrap_err(); assert!(matches!(error, GeoDataError::DuplicateCode { .. })); } @@ -336,12 +960,8 @@ fn missing_code_and_invalid_regex_are_errors() { GEOSITE_FILE_NAME, &site_list(&[site("other", &[])]), ); - let error = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoSite("missing".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); + let error = + GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("missing".to_owned()))]).unwrap_err(); assert!(matches!(error, GeoDataError::MissingCode { .. })); write_asset( @@ -349,12 +969,8 @@ fn missing_code_and_invalid_regex_are_errors() { GEOSITE_FILE_NAME, &site_list(&[site("broken", &[domain(1, "(?=lookaround)")])]), ); - let error = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoSite("broken".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); + let error = + GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("broken".to_owned()))]).unwrap_err(); assert!(matches!(error, GeoDataError::InvalidRegex { .. })); for unsupported in ["(?u:\\w+)", "(?x:example)", "例子"] { @@ -366,7 +982,6 @@ fn missing_code_and_invalid_regex_are_errors() { let error = GeoData::load( dir.path(), &[rule(RuleKind::GeoSite("unsupported".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, ) .unwrap_err(); assert!(matches!(error, GeoDataError::InvalidRegex { .. })); @@ -374,65 +989,211 @@ fn missing_code_and_invalid_regex_are_errors() { } #[test] -fn reverse_match_and_invalid_cidr_are_rejected() { +fn dat_reverse_match_is_ignored_and_invalid_cidr_is_rejected() { let dir = tempdir().unwrap(); write_asset( dir.path(), GEOIP_FILE_NAME, - &geoip_list(&[geoip("reverse", &[], true)]), + &geoip_list(&[geoip("reverse", &[cidr(&[10, 0, 0, 0], 8)], true)]), ); - let error = GeoData::load( + let data = GeoData::load( dir.path(), - &[rule(RuleKind::GeoIp("reverse".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, + &[ + rule(RuleKind::GeoIp("reverse".to_owned())), + rule(RuleKind::GeoIp("!reverse".to_owned())), + ], ) - .unwrap_err(); - assert!(matches!(error, GeoDataError::ReverseMatch { .. })); + .unwrap(); + assert!(data.matches_geoip("reverse", "10.1.2.3".parse().unwrap())); + assert!(!data.matches_geoip("reverse", "192.0.2.1".parse().unwrap())); + assert!(!data.matches_geoip("!reverse", "10.1.2.3".parse().unwrap())); + assert!(data.matches_geoip("!reverse", "192.0.2.1".parse().unwrap())); write_asset( dir.path(), GEOIP_FILE_NAME, &geoip_list(&[geoip("bad", &[cidr(&[10, 0, 0, 0], 33)], false)]), ); - let error = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoIp("bad".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); + let error = GeoData::load(dir.path(), &[rule(RuleKind::GeoIp("bad".to_owned()))]).unwrap_err(); assert!(matches!(error, GeoDataError::InvalidCidr { .. })); } #[test] -fn tiny_allocation_budget_fails_before_loading_records() { +fn geosite_regexes_load_without_vcore_record_or_cumulative_source_quotas() { + let dir = tempdir().unwrap(); + let records: Vec<_> = (0..513) + .map(|index| domain(1, &format!("^{}r{index}\\.test$", "a".repeat(130)))) + .collect(); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("regex", &records)]), + ); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("regex".into()))]).unwrap(); + for index in [0, 512] { + assert!(data.matches_geosite("regex", &format!("{}r{index}.test", "a".repeat(130)))); + } + assert!(!data.matches_geosite("regex", "no-match.test")); + // Only the owned vector capacity is visible to this diagnostic ledger; + // the library's compiled programs and search caches are intentionally not. + assert!(data.allocation_capacity() >= records.len() * mem::size_of::()); +} + +#[test] +fn geosite_byte_regex_preserves_search_classes_and_supported_flags() { + let dir = tempdir().unwrap(); + for (pattern, matched, not_matched) in [ + (r"^a.b$", "a-b", "a\nb"), + (r"^[^.]+\.test$", "abc.test", "a.b.test"), + (r"^\w+\.test$", "abc_42.test", "a-b.test"), + (r"^\w+\.test$", "abc.test", "é.test"), + (r"needle", "has-needle.example", "absent.example"), + (r"(?i:FLAG)", "flag.example", "quiet.example"), + (r"(?m:^line$)", "first\nline\nlast", "first\nnot-line\nlast"), + (r"(?s:^a.b$)", "a\nb", "ab"), + (r"(?U:a.+b)", "prefix-a---b-suffix", "prefix-a-suffix"), + ] { + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("byte-regex", &[domain(1, pattern)])]), + ); + let data = + GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("byte-regex".into()))]).unwrap(); + assert!(data.matches_geosite("byte-regex", matched), "{pattern}"); + assert!( + !data.matches_geosite("byte-regex", not_matched), + "{pattern}" + ); + } +} + +#[test] +fn geosite_byte_regex_loads_short_pattern_with_exponential_complete_dfa() { let dir = tempdir().unwrap(); write_asset( dir.path(), GEOSITE_FILE_NAME, - &site_list(&[site("small", &[domain(3, "example.com")])]), + &site_list(&[site("binary", &[domain(1, r"[01]*1[01]{20}")])]), ); - let error = GeoData::load( + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("binary".into()))]).unwrap(); + assert!(data.matches_geosite("binary", &format!("1{}", "0".repeat(20)))); + assert!(!data.matches_geosite("binary", &format!("1{}", "0".repeat(19)))); + assert!(!data.matches_geosite("binary", &"0".repeat(21))); +} + +#[test] +fn geosite_byte_regex_library_rejections_do_not_expose_expression_source() { + let dir = tempdir().unwrap(); + for (pattern, expected_detail) in [ + ("private-expression([", "invalid regex syntax"), + ( + "private-expression(?:ab){1000000}", + "compiled regex exceeds the library default size limit", + ), + ] { + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("rejected", &[domain(1, pattern)])]), + ); + let error = + GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("rejected".into()))]).unwrap_err(); + let GeoDataError::InvalidRegex { ref detail, .. } = error else { + panic!("unexpected error kind"); + }; + assert_eq!(detail, expected_detail); + for formatted in [error.to_string(), format!("{error:?}")] { + assert!(!formatted.contains(pattern)); + assert!(!formatted.contains("private-expression")); + } + } +} + +#[test] +fn rejected_byte_regex_reload_keeps_the_active_snapshot() { + let dir = tempdir().unwrap(); + write_asset( dir.path(), - &[rule(RuleKind::GeoSite("small".to_owned()))], - 1, - ) - .unwrap_err(); - assert!(matches!( - error, - GeoDataError::AllocationBudgetExceeded { .. } - )); + GEOSITE_FILE_NAME, + &site_list(&[site("regex", &[domain(1, r"^kept\.example$")])]), + ); + let manager = GeoDataManager::open(dir.path(), std::time::Duration::from_secs(60)).unwrap(); + let registration = manager + .register( + GeoRequirements::collect(&[rule(RuleKind::GeoSite("regex".into()))], &[]).unwrap(), + ) + .unwrap(); + let matcher = registration.matcher(); + assert!(matcher.matches_geosite("regex", "kept.example")); + write_asset( + dir.path(), + GEOSITE_FILE_NAME, + &site_list(&[site("regex", &[domain(1, "private-expression([")])]), + ); + assert!(manager.reload().geosite_available); + assert!(matcher.matches_geosite("regex", "kept.example")); + assert!(!matcher.matches_geosite("regex", "new.example")); +} + +#[test] +fn geoip_records_load_without_count_limits_before_cidr_compaction() { + let dir = tempdir().unwrap(); + let mut category = field_bytes(1, b"cn"); + for _ in 0..320_000 { + category.extend(field_bytes(2, &cidr(&[10, 0, 0, 1], 32))); + } + category.extend(field_bytes(2, &cidr(&[203, 0, 113, 9], 32))); + write_asset(dir.path(), GEOIP_FILE_NAME, &geoip_list(&[category])); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoIp("cn".into()))]).unwrap(); + assert!(data.matches_geoip("cn", "10.0.0.1".parse().unwrap())); + assert!(data.matches_geoip("cn", "203.0.113.9".parse().unwrap())); + assert!(!data.matches_geoip("cn", "203.0.113.10".parse().unwrap())); +} + +#[test] +fn large_assets_skip_unselected_payloads_without_file_size_quotas() { + use std::io::Write; + let dir = tempdir().unwrap(); + for kind in [GeoDataKind::GeoSite, GeoDataKind::GeoIp] { + // A valid unselected category with a sparse length-delimited unknown + // field beyond both former file limits. No giant heap fixture needed. + let padding = 33 * 1024 * 1024; + let mut category = field_bytes(1, b"unused"); + category.extend(varint((100 << 3) | 2)); + category.extend(varint(padding)); + let mut file = File::create(dir.path().join(kind.file_name())).unwrap(); + file.write_all(&varint((1 << 3) | 2)).unwrap(); + file.write_all(&varint(category.len() as u64 + padding)) + .unwrap(); + file.write_all(&category).unwrap(); + file.seek(SeekFrom::Current(padding as i64)).unwrap(); + let (selected, rule_kind) = match kind { + GeoDataKind::GeoSite => ( + site("selected", &[domain(3, "last.test")]), + RuleKind::GeoSite("selected".into()), + ), + GeoDataKind::GeoIp => ( + geoip("selected", &[cidr(&[10, 0, 0, 1], 32)], false), + RuleKind::GeoIp("selected".into()), + ), + }; + file.write_all(&field_bytes(1, &selected)).unwrap(); + let data = GeoData::load(dir.path(), &[rule(rule_kind)]).unwrap(); + match kind { + GeoDataKind::GeoSite => assert!(data.matches_geosite("selected", "last.test")), + GeoDataKind::GeoIp => { + assert!(data.matches_geoip("selected", "10.0.0.1".parse().unwrap())) + } + } + } } #[test] fn malformed_outer_framing_is_rejected() { let dir = tempdir().unwrap(); write_asset(dir.path(), GEOSITE_FILE_NAME, &[0x0a, 0x05, 0x0a]); - let error = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoSite("cn".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_err(); + let error = GeoData::load(dir.path(), &[rule(RuleKind::GeoSite("cn".to_owned()))]).unwrap_err(); assert!(matches!(error, GeoDataError::Malformed { .. })); } @@ -442,27 +1203,12 @@ fn matcher_is_send_and_sync() { require_send_sync::(); } -#[test] -fn more_than_sixteen_referenced_codes_fails_without_panicking() { - let dir = tempdir().unwrap(); - let rules: Vec<_> = (0..=MAX_REFERENCED_CODES) - .map(|index| rule(RuleKind::GeoSite(format!("code{index}")))) - .collect(); - let error = GeoData::load(dir.path(), &rules, GENERAL_ALLOCATION_BUDGET_BYTES).unwrap_err(); - assert!(matches!(error, GeoDataError::TooManyReferencedCodes { .. })); -} - #[test] fn literal_ip_family_does_not_cross_match() { let dir = tempdir().unwrap(); let fixture = geoip_list(&[geoip("v4", &[cidr(&[0, 0, 0, 0], 0)], false)]); write_asset(dir.path(), GEOIP_FILE_NAME, &fixture); - let data = GeoData::load( - dir.path(), - &[rule(RuleKind::GeoIp("v4".to_owned()))], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap(); + let data = GeoData::load(dir.path(), &[rule(RuleKind::GeoIp("v4".to_owned()))]).unwrap(); assert!(data.matches_geoip("v4", IpAddr::from([203, 0, 113, 1]))); assert!(!data.matches_geoip("v4", "::ffff:203.0.113.1".parse().unwrap())); } @@ -472,7 +1218,7 @@ fn literal_ip_family_does_not_cross_match() { /// `VCORE_GEODATA_DIR=/path/to/assets/dat cargo test real_xray_geodata -- --ignored --nocapture`. #[test] #[ignore = "requires VCORE_GEODATA_DIR containing real geosite.dat and geoip.dat"] -fn real_xray_geodata_loads_common_codes_with_shared_budget() { +fn real_xray_geodata_loads_common_codes_without_memory_quotas() { let dir = std::env::var_os("VCORE_GEODATA_DIR") .map(PathBuf::from) .expect("VCORE_GEODATA_DIR must point to the directory containing both .dat files"); @@ -485,14 +1231,12 @@ fn real_xray_geodata_loads_common_codes_with_shared_budget() { let baseline = GeoData::load( &dir, &[rules[0].clone(), rules[2].clone(), rules[3].clone()], - GENERAL_ALLOCATION_BUDGET_BYTES, ) .unwrap(); eprintln!( - "real GeoData baseline allocation without GEOLOCATION-!CN: retained={} bytes, construction_peak={} bytes, budget={} bytes", + "real GeoData baseline allocation without GEOLOCATION-!CN: retained={} bytes, accounted_peak={} bytes", baseline.allocation_capacity(), baseline.peak_allocation_capacity(), - GENERAL_ALLOCATION_BUDGET_BYTES, ); assert!(baseline.matches_geosite("cn", "baidu.com")); assert!(baseline.matches_geoip("private", "10.0.0.1".parse().unwrap())); @@ -507,40 +1251,33 @@ fn real_xray_geodata_loads_common_codes_with_shared_budget() { rules[2].clone(), rules[3].clone(), ], - GENERAL_ALLOCATION_BUDGET_BYTES, ) .unwrap_or_else(|error| panic!("documented GeoData combination failed: {error}")); eprintln!( - "documented GeoData allocation: retained={} bytes, construction_peak={} bytes, budget={} bytes", + "documented GeoData allocation: retained={} bytes, accounted_peak={} bytes", documented.allocation_capacity(), documented.peak_allocation_capacity(), - GENERAL_ALLOCATION_BUDGET_BYTES, ); assert!(documented.matches_geosite("cn", "baidu.com")); assert!(documented.matches_geoip("private", "10.0.0.1".parse().unwrap())); drop(documented); let policy = dns_policy(&["private", "cn", "apple"]); - let policy_data = GeoData::load_with_dns_policies( - &dir, - &[rules[2].clone(), rules[3].clone()], - &[policy], - GENERAL_ALLOCATION_BUDGET_BYTES, - ) - .unwrap_or_else(|error| panic!("Simple Profile DNS policy GeoData failed: {error}")); + let policy_data = + GeoData::load_with_dns_policies(&dir, &[rules[2].clone(), rules[3].clone()], &[policy]) + .unwrap_or_else(|error| panic!("Simple Profile DNS policy GeoData failed: {error}")); assert!(policy_data.matches_geosite("private", "localhost")); assert!(policy_data.matches_geosite("cn", "baidu.com")); assert!(policy_data.matches_geosite("apple", "apple.com")); drop(policy_data); - let data = GeoData::load(&dir, &rules, GENERAL_ALLOCATION_BUDGET_BYTES) + let data = GeoData::load(&dir, &rules) .unwrap_or_else(|error| panic!("real GeoData compatibility failed: {error}")); eprintln!( - "real GeoData allocation: retained={} bytes, construction_peak={} bytes, budget={} bytes", + "real GeoData allocation: retained={} bytes, accounted_peak={} bytes", data.allocation_capacity(), data.peak_allocation_capacity(), - GENERAL_ALLOCATION_BUDGET_BYTES, ); assert!(data.matches_geosite("cn", "baidu.com")); assert!(data.matches_geosite("geolocation-!cn", "google.com")); diff --git a/src/geodata/updater.rs b/src/geodata/updater.rs index 085d9e8..cc12405 100644 --- a/src/geodata/updater.rs +++ b/src/geodata/updater.rs @@ -1,4 +1,4 @@ -//! Bounded GeoData downloads forced through one raw proxy dispatcher. +//! Streaming GeoData downloads forced through one raw proxy dispatcher. //! //! This module deliberately does not know about routing or DIRECT. The caller //! must pass the raw dispatcher for the selected default proxy. Every hop, @@ -48,7 +48,6 @@ pub(crate) struct GeoDataDownloadRequest { /// A new temporary file. The downloader refuses to replace an existing /// path; publishing the completed file is the manager's responsibility. pub temporary_path: PathBuf, - pub size_limit: u64, pub timeout: Duration, pub cancellation: CancellationToken, } @@ -88,8 +87,6 @@ pub(crate) enum GeoDataDownloadError { HttpStatus(u16), #[error("GeoData download followed more than {MAX_REDIRECTS} redirects")] TooManyRedirects, - #[error("GeoData response body is at least {actual} bytes; limit is {maximum} bytes")] - BodyTooLarge { actual: u64, maximum: u64 }, #[error("GeoData download timed out after {0:?}")] TimedOut(Duration), #[error("GeoData download was cancelled")] @@ -167,7 +164,7 @@ async fn download_with_connector( request: GeoDataDownloadRequest, connector: &dyn HttpsConnector, ) -> Result { - let initial_url = parse_https_url(&request.url)?; + let initial_url = parse_geodata_url(&request.url)?; validate_etag(request.etag.as_deref())?; let temporary_path = request.temporary_path.clone(); @@ -213,7 +210,7 @@ async fn download_inner( let mut file = Some(file); for redirects_followed in 0..=MAX_REDIRECTS { - let endpoint = HttpsEndpoint::from_url(¤t_url)?; + let endpoint = DownloadEndpoint::from_url(¤t_url)?; let session = StreamSession { inbound: InboundKind::InternalGeoData, source: SocketAddr::from(([0, 0, 0, 0], 0)), @@ -226,6 +223,13 @@ async fn download_inner( .connect_tcp(session) .await .map_err(GeoDataDownloadError::Dispatch)?; + #[cfg(feature = "benchmark-geodata-http")] + let stream = if current_url.scheme() == "http" { + stream + } else { + connector.connect(&endpoint.host, stream).await? + }; + #[cfg(not(feature = "benchmark-geodata-http"))] let stream = connector.connect(&endpoint.host, stream).await?; let mut response = HttpReader::new(stream); @@ -266,11 +270,11 @@ async fn download_inner( } validate_content_encoding(head.content_encoding.as_deref())?; - let framing = body_framing(&head, request.size_limit)?; + let framing = body_framing(&head)?; let target = file .take() .expect("temporary file must be consumed by one final response"); - let mut sink = BodySink::new(target, request.size_limit); + let mut sink = BodySink::new(target); match framing { BodyFraming::ContentLength(length) => { response.copy_exact_body(length, &mut sink).await?; @@ -295,14 +299,14 @@ async fn download_inner( } #[derive(Debug)] -struct HttpsEndpoint { +struct DownloadEndpoint { host: String, port: u16, authority: String, origin_form: String, } -impl HttpsEndpoint { +impl DownloadEndpoint { fn from_url(url: &Url) -> Result { let host = match url.host() { Some(Host::Domain(host)) => host.to_owned(), @@ -310,8 +314,9 @@ impl HttpsEndpoint { }; let port = url .port_or_known_default() - .ok_or_else(|| GeoDataDownloadError::InvalidUrl("missing HTTPS port".to_owned()))?; - let authority = if port == 443 { + .ok_or_else(|| GeoDataDownloadError::InvalidUrl("missing HTTP(S) port".to_owned()))?; + let default_port = if url.scheme() == "http" { 80 } else { 443 }; + let authority = if port == default_port { host.clone() } else { format!("{host}:{port}") @@ -334,7 +339,7 @@ impl HttpsEndpoint { } } -fn parse_https_url(raw: &str) -> Result { +fn parse_geodata_url(raw: &str) -> Result { if raw.len() > MAX_URL_BYTES { return Err(GeoDataDownloadError::InvalidUrl(format!( "URL exceeds {MAX_URL_BYTES} bytes" @@ -342,7 +347,10 @@ fn parse_https_url(raw: &str) -> Result { } let mut url = Url::parse(raw).map_err(|error| GeoDataDownloadError::InvalidUrl(error.to_string()))?; - if url.scheme() != "https" { + let allowed_scheme = url.scheme() == "https"; + #[cfg(feature = "benchmark-geodata-http")] + let allowed_scheme = allowed_scheme || url.scheme() == "http"; + if !allowed_scheme { return Err(GeoDataDownloadError::HttpsRequired); } if !url.username().is_empty() || url.password().is_some() { @@ -351,6 +359,22 @@ fn parse_https_url(raw: &str) -> Result { if !matches!(url.host(), Some(Host::Domain(_))) { return Err(GeoDataDownloadError::DomainRequired); } + #[cfg(feature = "benchmark-geodata-http")] + if url.scheme() == "http" { + let authority = raw + .split_once("://") + .map(|(_, suffix)| suffix) + .and_then(|suffix| suffix.split(['/', '?', '#']).next()) + .unwrap_or_default(); + if authority.contains('@') { + return Err(GeoDataDownloadError::CredentialsNotAllowed); + } + if url.fragment().is_some() { + return Err(GeoDataDownloadError::InvalidUrl( + "URL must not contain a fragment".to_owned(), + )); + } + } url.set_fragment(None); Ok(url) } @@ -364,7 +388,10 @@ fn resolve_redirect(base: &Url, location: &str) -> Result) -> Result<(), GeoDataDownloadError> { @@ -376,7 +403,7 @@ fn validate_etag(etag: Option<&str>) -> Result<(), GeoDataDownloadError> { Ok(()) } -fn build_request(endpoint: &HttpsEndpoint, etag: Option<&str>) -> Vec { +fn build_request(endpoint: &DownloadEndpoint, etag: Option<&str>) -> Vec { let conditional = etag.map_or_else(String::new, |etag| format!("If-None-Match: {etag}\r\n")); format!( "GET {} HTTP/1.1\r\nHost: {}\r\nAccept: application/octet-stream\r\nAccept-Encoding: identity\r\nUser-Agent: VCore/0.1\r\nConnection: close\r\n{}\r\n", @@ -406,7 +433,7 @@ enum BodyFraming { UntilEof, } -fn body_framing(head: &ResponseHead, size_limit: u64) -> Result { +fn body_framing(head: &ResponseHead) -> Result { if head.transfer_encoding.is_some() && head.content_length.is_some() { return Err(GeoDataDownloadError::Protocol( "response contains both Transfer-Encoding and Content-Length".to_owned(), @@ -421,12 +448,6 @@ fn body_framing(head: &ResponseHead, size_limit: u64) -> Result size_limit { - return Err(GeoDataDownloadError::BodyTooLarge { - actual: length, - maximum: size_limit, - }); - } return Ok(BodyFraming::ContentLength(length)); } Ok(BodyFraming::UntilEof) @@ -876,16 +897,14 @@ struct BodySink { file: File, hasher: Sha256, size: u64, - maximum: u64, } impl BodySink { - fn new(file: File, maximum: u64) -> Self { + fn new(file: File) -> Self { Self { file, hasher: Sha256::new(), size: 0, - maximum, } } @@ -894,16 +913,7 @@ impl BodySink { let next = self .size .checked_add(added) - .ok_or(GeoDataDownloadError::BodyTooLarge { - actual: u64::MAX, - maximum: self.maximum, - })?; - if next > self.maximum { - return Err(GeoDataDownloadError::BodyTooLarge { - actual: next, - maximum: self.maximum, - }); - } + .ok_or_else(|| GeoDataDownloadError::Protocol("body size overflows u64".to_owned()))?; self.file .write_all(bytes) .map_err(GeoDataDownloadError::NetworkIo)?; @@ -956,6 +966,23 @@ mod tests { } } + #[cfg(feature = "benchmark-geodata-http")] + #[derive(Default)] + struct RecordingHttpsConnector(Mutex>); + + #[cfg(feature = "benchmark-geodata-http")] + #[async_trait] + impl HttpsConnector for RecordingHttpsConnector { + async fn connect( + &self, + server_name: &str, + stream: BoxStream, + ) -> Result { + self.0.lock().unwrap().push(server_name.to_owned()); + Ok(stream) + } + } + struct ScriptedDispatcher { responses: Mutex>>, sessions: Mutex>, @@ -1040,19 +1067,142 @@ mod tests { dispatcher: Arc, url: &str, temporary_path: PathBuf, - size_limit: u64, ) -> GeoDataDownloadRequest { GeoDataDownloadRequest { dispatcher, url: url.to_owned(), etag: Some("\"old\"".to_owned()), temporary_path, - size_limit, timeout: Duration::from_secs(2), cancellation: CancellationToken::new(), } } + #[cfg(feature = "benchmark-geodata-http")] + #[tokio::test] + async fn benchmark_http_uses_the_raw_stream_and_existing_download_outcomes() { + for (name, response) in [ + ( + "download", + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\ndata\r\n0\r\n\r\n" + .as_slice(), + ), + ( + "not-modified", + b"HTTP/1.1 304 Not Modified\r\n\r\n".as_slice(), + ), + ( + "truncated", + b"HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\nshort".as_slice(), + ), + ] { + let directory = tempdir().unwrap(); + let path = directory.path().join("fixture.new"); + let dispatcher = Arc::new(ScriptedDispatcher::new([response.to_vec()])); + // The real connector would reject this plaintext in-memory peer if + // HTTP accidentally attempted a TLS handshake. + let outcome = download_geodata_via_proxy(request( + dispatcher.clone(), + "http://rules.example.test/geoip.dat", + path.clone(), + )) + .await; + match name { + "download" => { + assert!(matches!( + outcome, + Ok(GeoDataDownloadOutcome::Downloaded { size: 4, .. }) + )); + assert_eq!(fs::read(&path).unwrap(), b"data"); + } + "not-modified" => { + assert_eq!(outcome.unwrap(), GeoDataDownloadOutcome::NotModified); + assert!(!path.exists()); + } + "truncated" => { + assert!(matches!(outcome, Err(GeoDataDownloadError::Protocol(_)))); + assert!(!path.exists()); + } + _ => unreachable!(), + } + let sessions = dispatcher.sessions(); + assert_eq!(sessions.len(), 1); + assert_eq!(sessions[0].inbound, InboundKind::InternalGeoData); + assert_eq!( + sessions[0].destination, + Destination::domain("rules.example.test", 80).unwrap() + ); + assert!( + std::str::from_utf8(&dispatcher.requests()[0]) + .unwrap() + .contains("\r\nHost: rules.example.test\r\n") + ); + } + } + + #[cfg(feature = "benchmark-geodata-http")] + #[tokio::test] + async fn benchmark_http_redirects_allow_plaintext_hops_and_https_upgrade() { + let directory = tempdir().unwrap(); + let path = directory.path().join("redirect.new"); + let dispatcher = Arc::new(ScriptedDispatcher::new([ + b"HTTP/1.1 302 Found\r\nLocation: http://cdn.example.test:8080/next.dat\r\nContent-Length: 0\r\n\r\n".to_vec(), + b"HTTP/1.1 302 Found\r\nLocation: https://secure.example.test/final.dat\r\nContent-Length: 0\r\n\r\n".to_vec(), + b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata".to_vec(), + ])); + let connector = RecordingHttpsConnector::default(); + let outcome = download_with_connector( + request( + dispatcher.clone(), + "http://rules.example.test/start.dat", + path, + ), + &connector, + ) + .await + .unwrap(); + assert!( + matches!(outcome, GeoDataDownloadOutcome::Downloaded { final_url, .. } if final_url == "https://secure.example.test/final.dat") + ); + assert_eq!(*connector.0.lock().unwrap(), ["secure.example.test"]); + let destinations: Vec<_> = dispatcher + .sessions() + .into_iter() + .map(|session| session.destination) + .collect(); + assert_eq!( + destinations, + [ + Destination::domain("rules.example.test", 80).unwrap(), + Destination::domain("cdn.example.test", 8080).unwrap(), + Destination::domain("secure.example.test", 443).unwrap(), + ] + ); + assert!( + std::str::from_utf8(&dispatcher.requests()[1]) + .unwrap() + .contains("\r\nHost: cdn.example.test:8080\r\n") + ); + } + + #[cfg(not(feature = "benchmark-geodata-http"))] + #[tokio::test] + async fn plaintext_geodata_is_rejected_before_dispatch() { + let directory = tempdir().unwrap(); + let path = directory.path().join("http.new"); + let dispatcher = Arc::new(ScriptedDispatcher::new([])); + let error = download_geodata_via_proxy(request( + dispatcher.clone(), + "http://rules.example.test/geoip.dat", + path.clone(), + )) + .await + .unwrap_err(); + assert!(matches!(error, GeoDataDownloadError::HttpsRequired)); + assert!(dispatcher.sessions().is_empty()); + assert!(!path.exists()); + } + #[tokio::test] async fn follows_https_redirect_and_streams_chunked_body_through_raw_proxy() { let directory = tempdir().expect("tempdir"); @@ -1066,7 +1216,6 @@ mod tests { dispatcher.clone(), "https://rules.example.test/geosite.dat", temporary_path.clone(), - 1024, ), &PlainHttpsConnector, ) @@ -1131,7 +1280,6 @@ mod tests { dispatcher, "https://rules.example.test/geoip.dat", temporary_path.clone(), - 1024, ), &PlainHttpsConnector, ) @@ -1154,7 +1302,6 @@ mod tests { content_length_dispatcher, "https://rules.example.test/content-length", content_length_path.clone(), - 5, ), &PlainHttpsConnector, ) @@ -1178,7 +1325,6 @@ mod tests { eof_dispatcher, "https://rules.example.test/eof", eof_path.clone(), - 32, ), &PlainHttpsConnector, ) @@ -1192,32 +1338,47 @@ mod tests { } #[tokio::test] - async fn rejects_oversized_body_and_removes_partial_file() { - let directory = tempdir().expect("tempdir"); - let temporary_path = directory.path().join("oversized.new"); + async fn streams_body_beyond_former_file_size_limits() { + let directory = tempdir().unwrap(); + let temporary_path = directory.path().join("large.new"); + let length = 32 * 1024 * 1024 + 1; + let mut response = + format!("HTTP/1.1 200 OK\r\nContent-Length: {length}\r\n\r\n").into_bytes(); + response.resize(response.len() + length, b'x'); + let dispatcher = Arc::new(ScriptedDispatcher::new([response])); + let mut request = request( + dispatcher, + "https://rules.example.test/geoip.dat", + temporary_path.clone(), + ); + request.timeout = Duration::from_secs(15); + let outcome = download_with_connector(request, &PlainHttpsConnector) + .await + .unwrap(); + assert!( + matches!(outcome, GeoDataDownloadOutcome::Downloaded { size, .. } if size == length as u64) + ); + assert_eq!(fs::metadata(temporary_path).unwrap().len(), length as u64); + } + + #[tokio::test] + async fn truncated_body_still_removes_partial_file() { + let directory = tempdir().unwrap(); + let temporary_path = directory.path().join("truncated.new"); let dispatcher = Arc::new(ScriptedDispatcher::new([ - b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n8\r\n12345678\r\n0\r\n\r\n" - .to_vec(), + b"HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\nshort".to_vec(), ])); let error = download_with_connector( request( dispatcher, "https://rules.example.test/geoip.dat", temporary_path.clone(), - 4, ), &PlainHttpsConnector, ) .await - .expect_err("body must be rejected"); - - assert!(matches!( - error, - GeoDataDownloadError::BodyTooLarge { - actual: 8, - maximum: 4 - } - )); + .unwrap_err(); + assert!(matches!(error, GeoDataDownloadError::Protocol(_))); assert!(!temporary_path.exists()); } @@ -1233,7 +1394,6 @@ mod tests { dispatcher.clone(), "https://rules.example.test/geoip.dat", temporary_path.clone(), - 1024, ), &PlainHttpsConnector, ) @@ -1256,7 +1416,6 @@ mod tests { dispatcher.clone(), "https://rules.example.test/geoip.dat", temporary_path.clone(), - 1024, ); request.cancellation.cancel(); let error = download_with_connector(request, &PlainHttpsConnector) @@ -1276,7 +1435,6 @@ mod tests { Arc::new(HangingDispatcher), "https://rules.example.test/geoip.dat", temporary_path.clone(), - 1024, ); request.timeout = Duration::from_millis(10); let error = download_with_connector(request, &PlainHttpsConnector) @@ -1298,38 +1456,94 @@ mod tests { ) .expect("head parses"); assert!(matches!( - body_framing(&both, 10), + body_framing(&both), Err(GeoDataDownloadError::Protocol(_)) )); - let length = - parse_response_head(b"HTTP/1.1 200 OK\r\nContent-Length: 11").expect("head parses"); - assert!(matches!( - body_framing(&length, 10), - Err(GeoDataDownloadError::BodyTooLarge { - actual: 11, - maximum: 10 - }) - )); + let length = parse_response_head(b"HTTP/1.1 200 OK\r\nContent-Length: 33554433") + .expect("head parses"); + assert_eq!( + body_framing(&length).unwrap(), + BodyFraming::ContentLength(33_554_433) + ); + assert!( + parse_response_head(b"HTTP/1.1 200 OK\r\nContent-Length: 18446744073709551616") + .is_err() + ); } #[test] - fn url_and_etag_validation_preserve_domain_only_https_contract() { + fn url_and_etag_validation_preserve_the_geodata_contract() { + #[cfg(not(feature = "benchmark-geodata-http"))] assert!(matches!( - parse_https_url("http://example.test/file"), + parse_geodata_url("http://example.test/file"), Err(GeoDataDownloadError::HttpsRequired) )); assert!(matches!( - parse_https_url("https://127.0.0.1/file"), + parse_geodata_url("https://127.0.0.1/file"), Err(GeoDataDownloadError::DomainRequired) )); assert!(matches!( - parse_https_url("https://user@example.test/file"), + parse_geodata_url("https://user@example.test/file"), Err(GeoDataDownloadError::CredentialsNotAllowed) )); + assert_eq!( + parse_geodata_url("https://example.test/file#fragment") + .unwrap() + .as_str(), + "https://example.test/file" + ); + assert!(parse_geodata_url("ftp://example.test/file").is_err()); assert!(matches!( validate_etag(Some("\"ok\"\r\nX-Evil: yes")), Err(GeoDataDownloadError::InvalidEtag) )); } + + #[test] + fn download_endpoints_use_scheme_specific_ports_and_authorities() { + for (raw, port, authority) in [ + ("https://example.test/file", 443, "example.test"), + ("https://example.test:80/file", 80, "example.test:80"), + ("https://example.test:8443/file", 8443, "example.test:8443"), + ] { + let endpoint = DownloadEndpoint::from_url(&parse_geodata_url(raw).unwrap()).unwrap(); + assert_eq!(endpoint.port, port); + assert_eq!(endpoint.authority, authority); + } + #[cfg(feature = "benchmark-geodata-http")] + { + for (raw, port, authority) in [ + ("http://example.test/file", 80, "example.test"), + ("http://example.test:443/file", 443, "example.test:443"), + ] { + let endpoint = + DownloadEndpoint::from_url(&parse_geodata_url(raw).unwrap()).unwrap(); + assert_eq!(endpoint.port, port); + assert_eq!(endpoint.authority, authority); + } + for raw in [ + "http://127.0.0.1/file", + "http://[::1]/file", + "http://user@example.test/file", + "http://@example.test/file", + "http://example.test/file#fragment", + ] { + assert!(parse_geodata_url(raw).is_err()); + } + let base = parse_geodata_url("http://example.test/start").unwrap(); + assert_eq!( + resolve_redirect(&base, "/next").unwrap().as_str(), + "http://example.test/next" + ); + for location in [ + "ftp://example.test/file", + "http://127.0.0.1/file", + "http://user@example.test/file", + "/file#fragment", + ] { + assert!(resolve_redirect(&base, location).is_err()); + } + } + } } diff --git a/src/limits.rs b/src/limits.rs index fc6181f..6259c80 100644 --- a/src/limits.rs +++ b/src/limits.rs @@ -79,6 +79,11 @@ pub(crate) const DNS_WORKER_STACK_BYTES: usize = 512 * 1024; /// Process-wide ceiling for lazily created bootstrap resolver workers. pub(crate) const MAX_DNS_WORKERS: usize = 4; +/// Unfinished physical socket setup jobs per lifecycle owner, shared by TCP +/// and UDP. Callers wait for submission capacity; established connections are +/// not counted and receive no business-concurrency quota. +pub(crate) const MAX_SOCKET_INITIALIZATIONS: usize = 64; + /// Retained-memory, queue and per-object safety boundaries. /// /// These values deliberately do not cap concurrent TCP sessions, UDP @@ -99,8 +104,11 @@ pub struct ResourceLimits { pub dns_address_cache_entries: usize, /// Retained address-to-domain hints used by redir-host routing. pub dns_redir_host_entries: usize, - /// Netstack UDP ingress capacity while DNS hijacking is enabled. - pub tun_dns_ingress_queue_capacity: usize, + /// Pending request datagrams retained by each ordinary TUN UDP association. + pub tun_udp_association_queue_capacity: usize, + /// Bounded burst headroom for ordinary TUN UDP responses waiting for the + /// shared packet writer. Saturation drops only the current response. + pub tun_udp_response_queue_capacity: usize, /// Reserved TUN DNS response capacity. The isolated response path consumes /// this value once that path is enabled. pub tun_dns_response_queue_capacity: usize, @@ -119,7 +127,8 @@ impl Default for ResourceLimits { tcp_buffer_per_direction: 32 * 1024, dns_address_cache_entries: 256, dns_redir_host_entries: 256, - tun_dns_ingress_queue_capacity: 128, + tun_udp_association_queue_capacity: 64, + tun_udp_response_queue_capacity: 4_096, tun_dns_response_queue_capacity: 128, tls_buffer_limit: 64 * 1024, xhttp_send_buffer_size: 64 * 1024, @@ -139,8 +148,12 @@ impl ResourceLimits { ("dns_address_cache_entries", self.dns_address_cache_entries), ("dns_redir_host_entries", self.dns_redir_host_entries), ( - "tun_dns_ingress_queue_capacity", - self.tun_dns_ingress_queue_capacity, + "tun_udp_association_queue_capacity", + self.tun_udp_association_queue_capacity, + ), + ( + "tun_udp_response_queue_capacity", + self.tun_udp_response_queue_capacity, ), ( "tun_dns_response_queue_capacity", @@ -189,7 +202,11 @@ mod tests { ..ResourceLimits::default() }, ResourceLimits { - tun_dns_ingress_queue_capacity: 0, + tun_udp_association_queue_capacity: 0, + ..ResourceLimits::default() + }, + ResourceLimits { + tun_udp_response_queue_capacity: 0, ..ResourceLimits::default() }, ResourceLimits { @@ -222,7 +239,8 @@ mod tests { assert_eq!(limits.tcp_buffer_per_direction, 32 * 1024); assert_eq!(limits.dns_address_cache_entries, 256); assert_eq!(limits.dns_redir_host_entries, 256); - assert_eq!(limits.tun_dns_ingress_queue_capacity, 128); + assert_eq!(limits.tun_udp_association_queue_capacity, 64); + assert_eq!(limits.tun_udp_response_queue_capacity, 4_096); assert_eq!(limits.tun_dns_response_queue_capacity, 128); assert_eq!(limits.tls_buffer_limit, 64 * 1024); assert_eq!(limits.xhttp_send_buffer_size, 64 * 1024); diff --git a/src/outbound/anytls/client.rs b/src/outbound/anytls/client.rs index 2436607..b722aed 100644 --- a/src/outbound/anytls/client.rs +++ b/src/outbound/anytls/client.rs @@ -135,7 +135,7 @@ impl AnyTlsClient { ) -> Result { let sequence = self .next_sequence - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |current| { current.checked_add(1) }) .map_err(|_| DispatchError::Other("AnyTLS session sequence exhausted".to_owned()))? diff --git a/src/outbound/anytls/session.rs b/src/outbound/anytls/session.rs index 842aebb..fd75526 100644 --- a/src/outbound/anytls/session.rs +++ b/src/outbound/anytls/session.rs @@ -251,7 +251,7 @@ impl Session { let stream_id = match self .next_stream_id - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |current| { current.checked_add(1) }) { Ok(previous) => previous + 1, diff --git a/src/outbound/direct.rs b/src/outbound/direct.rs index 5bdb9c4..cc91539 100644 --- a/src/outbound/direct.rs +++ b/src/outbound/direct.rs @@ -4,6 +4,7 @@ use crate::dialer::PhysicalDatagram as UdpSocket; use async_trait::async_trait; use bytes::Bytes; use futures_util::future::select_all; +use tokio::io::ReadBuf; use crate::{ dialer::Dialer, @@ -141,6 +142,25 @@ impl DirectDatagramTransport { "direct UDP receive requested before the first datagram".to_owned(), )); } + // A single physical socket has only this reader. Poll it directly + // instead of rebuilding boxed readiness waiters after cancellation. + if self.sockets.len() == 1 { + let socket = &self.sockets[0].1; + let mut receive_buffer = ReadBuf::new(&mut self.receive_buffer); + let remote = std::future::poll_fn(|context| { + socket.poll_recv_from(context, &mut receive_buffer) + }) + .await + .map_err(DispatchError::from)?; + if receive_buffer.filled().len() > self.max_response_payload_size { + continue; + } + return Ok(Datagram { + remote: Destination::Ip(remote), + payload: Bytes::copy_from_slice(receive_buffer.filled()), + sniffed_domain: None, + }); + } let readiness: Vec> + Send + '_>>> = self .sockets .iter() diff --git a/src/outbound/hysteria2/datagram.rs b/src/outbound/hysteria2/datagram.rs index e8dd090..7931b91 100644 --- a/src/outbound/hysteria2/datagram.rs +++ b/src/outbound/hysteria2/datagram.rs @@ -47,7 +47,7 @@ impl Registry { ) -> Result, DispatchError> { let id = self .next - .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |value| { + .try_update(Ordering::Relaxed, Ordering::Relaxed, |value| { value.checked_add(1) }) .map_err(|_| DispatchError::NotAllowed)?; diff --git a/src/platform/apple_logging.rs b/src/platform/apple_logging.rs index 3a1ec87..312013c 100644 --- a/src/platform/apple_logging.rs +++ b/src/platform/apple_logging.rs @@ -191,7 +191,7 @@ fn is_vcore_target(target: &str) -> bool { } fn os_log_level(level: &Level) -> OsLogLevel { - #[cfg(target_os = "ios")] + #[cfg(any(target_os = "ios", target_os = "tvos"))] { // Xcode does not reliably forward Default/Info messages from a Packet // Tunnel Extension unless it is attached as the debug executable. diff --git a/src/platform/linux_tun.rs b/src/platform/linux_tun.rs new file mode 100644 index 0000000..d4e8e8c --- /dev/null +++ b/src/platform/linux_tun.rs @@ -0,0 +1,374 @@ +//! Read-only validation of a borrowed Linux kernel TUN, including cross-netns +//! descriptors. No interface, route, file-status flag or caller namespace is +//! changed here. + +use std::{ + fs::File, + io, + os::{ + fd::{AsRawFd, BorrowedFd, FromRawFd, OwnedFd}, + unix::fs::MetadataExt, + }, + thread, +}; + +use super::rust_tun_io::TUN_MTU; + +const NETLINK_HEADER: usize = 16; +const LINK_HEADER: usize = 16; +const MAX_LINK_MESSAGE: usize = 8192; +const QUERY_SEQUENCE: u32 = 1; +// Linux UAPI linux/if_link.h, tun section (not exposed by libc). +const IFLA_TUN_TYPE: u16 = 3; +const IFLA_TUN_PI: u16 = 4; +const IFLA_TUN_VNET_HDR: u16 = 5; +const IFLA_TUN_MULTI_QUEUE: u16 = 7; + +pub(super) fn validate(fd: BorrowedFd<'_>) -> io::Result<()> { + // SAFETY: all-zero ifreq is valid storage for the kernel's output. + let mut request: libc::ifreq = unsafe { std::mem::zeroed() }; + // SAFETY: fd is borrowed and request is writable for a complete ifreq. + if unsafe { libc::ioctl(fd.as_raw_fd(), libc::TUNGETIFF, &mut request) } < 0 { + return Err(os_error("Linux TUNGETIFF")); + } + // SAFETY: successful TUNGETIFF initializes the flags union member. + validate_flags(unsafe { request.ifr_ifru.ifru_flags } as u16)?; + if request.ifr_name[0] == 0 || !request.ifr_name.contains(&0) { + return Err(invalid("Linux TUN returned an invalid interface name")); + } + + // TUNGETIFF aliases IFF_NOFILTER with IFF_NO_PI and therefore cannot alone + // prove raw-IP framing. Query the actual tun attributes via rtnetlink in + // the device's namespace, not the physical-egress namespace of the core. + // SAFETY: this ioctl returns a newly owned namespace fd, not a pointer. + let namespace_fd = unsafe { libc::ioctl(fd.as_raw_fd(), libc::TUNGETDEVNETNS) }; + if namespace_fd < 0 { + return Err(os_error("Linux TUNGETDEVNETNS")); + } + // SAFETY: a successful TUNGETDEVNETNS returns a fresh owned descriptor. + let namespace = File::from(unsafe { OwnedFd::from_raw_fd(namespace_fd) }); + let current_namespace = File::open("/proc/thread-self/ns/net")?; + let target_identity = namespace.metadata()?; + let current_identity = current_namespace.metadata()?; + let name = request.ifr_name; + if target_identity.dev() == current_identity.dev() + && target_identity.ino() == current_identity.ino() + { + return validate_link(name); + } + + // setns is thread-local. Only this short-lived thread enters the device + // namespace; Invoke, Tokio and outbound workers retain physical egress. + // Joining completes validation (and closes its fds) before startup proceeds. + thread::Builder::new() + .name("vcore-tun-validate".into()) + .spawn(move || { + // SAFETY: namespace remains open; CLONE_NEWNET changes only this + // disposable thread. No caller-owned socket is moved into it. + if unsafe { libc::setns(namespace.as_raw_fd(), libc::CLONE_NEWNET) } < 0 { + return Err(os_error("Linux TUN namespace entry")); + } + validate_link(name) + })? + .join() + .map_err(|_| io::Error::other("Linux TUN validation thread panicked"))? +} + +fn validate_flags(flags: u16) -> io::Result<()> { + if flags != (libc::IFF_TUN | libc::IFF_NO_PI) as u16 { + return Err(invalid( + "Linux TUN requires IFF_TUN | IFF_NO_PI without additional modes", + )); + } + Ok(()) +} + +fn validate_link(name: [libc::c_char; libc::IFNAMSIZ]) -> io::Result<()> { + let control = socket(libc::AF_INET, libc::SOCK_DGRAM, 0)?; + // SAFETY: all-zero ifreq is valid before writing its interface name. + let mut request: libc::ifreq = unsafe { std::mem::zeroed() }; + request.ifr_name = name; + // SAFETY: control is open in the owning netns and request is writable. + if unsafe { libc::ioctl(control.as_raw_fd(), libc::SIOCGIFINDEX, &mut request) } < 0 { + return Err(os_error("Linux TUN interface index query")); + } + // SAFETY: successful SIOCGIFINDEX initializes the index union member. + let index = unsafe { request.ifr_ifru.ifru_ifindex }; + if index <= 0 { + return Err(invalid("Linux TUN returned an invalid interface index")); + } + + let netlink = socket(libc::AF_NETLINK, libc::SOCK_RAW, libc::NETLINK_ROUTE)?; + let timeout = libc::timeval { + tv_sec: 1, + tv_usec: 0, + }; + for option in [libc::SO_RCVTIMEO, libc::SO_SNDTIMEO] { + // SAFETY: timeout is readable for the supplied size and netlink is open. + if unsafe { + libc::setsockopt( + netlink.as_raw_fd(), + libc::SOL_SOCKET, + option, + (&raw const timeout).cast(), + std::mem::size_of_val(&timeout) as libc::socklen_t, + ) + } < 0 + { + return Err(os_error("Linux TUN link query deadline")); + } + } + let mut query = [0_u8; NETLINK_HEADER + LINK_HEADER]; + let query_len = query.len() as u32; + query[0..4].copy_from_slice(&query_len.to_ne_bytes()); + query[4..6].copy_from_slice(&libc::RTM_GETLINK.to_ne_bytes()); + query[6..8].copy_from_slice(&(libc::NLM_F_REQUEST as u16).to_ne_bytes()); + query[8..12].copy_from_slice(&QUERY_SEQUENCE.to_ne_bytes()); + query[NETLINK_HEADER + 4..NETLINK_HEADER + 8].copy_from_slice(&index.to_ne_bytes()); + // SAFETY: zeroed sockaddr_nl has kernel destination pid/groups zero. + let mut address: libc::sockaddr_nl = unsafe { std::mem::zeroed() }; + address.nl_family = libc::AF_NETLINK as libc::sa_family_t; + // SAFETY: query and address are readable for their declared lengths. + let sent = unsafe { + libc::sendto( + netlink.as_raw_fd(), + query.as_ptr().cast(), + query.len(), + 0, + (&raw const address).cast(), + std::mem::size_of_val(&address) as libc::socklen_t, + ) + }; + if sent < 0 { + return Err(os_error("Linux TUN link query")); + } + if sent as usize != query.len() { + return Err(invalid("Linux TUN link query was incomplete")); + } + let mut response = [0_u8; MAX_LINK_MESSAGE]; + let mut address_len = std::mem::size_of_val(&address) as libc::socklen_t; + // SAFETY: response/address are writable for their declared capacities. + // MSG_TRUNC reports the full datagram length so oversized data fails closed. + let received = unsafe { + libc::recvfrom( + netlink.as_raw_fd(), + response.as_mut_ptr().cast(), + response.len(), + libc::MSG_TRUNC, + (&raw mut address).cast(), + &mut address_len, + ) + }; + if received < 0 { + return Err(os_error("Linux TUN link query response")); + } + if received as usize > response.len() + || address_len as usize != std::mem::size_of_val(&address) + || address.nl_family != libc::AF_NETLINK as libc::sa_family_t + || address.nl_pid != 0 + || address.nl_groups != 0 + { + return Err(invalid("Linux TUN link query returned an invalid response")); + } + validate_link_message(&response[..received as usize], index) +} + +fn validate_link_message(message: &[u8], index: i32) -> io::Result<()> { + if message.len() < NETLINK_HEADER + || native_u32(&message[0..4])? as usize != message.len() + || native_u32(&message[8..12])? != QUERY_SEQUENCE + { + return Err(invalid("Linux TUN link response header is invalid")); + } + let kind = native_u16(&message[4..6])?; + if kind == libc::NLMSG_ERROR as u16 { + let error = message + .get(NETLINK_HEADER..NETLINK_HEADER + 4) + .ok_or_else(|| invalid("Linux TUN link error response is incomplete"))?; + let error = i32::from_ne_bytes(error.try_into().map_err(|_| invalid("invalid errno"))?); + return Err(if error < 0 && error != i32::MIN { + io::Error::from_raw_os_error(-error) + } else { + invalid("Linux TUN link query did not return device parameters") + }); + } + if kind != libc::RTM_NEWLINK + || message.len() < NETLINK_HEADER + LINK_HEADER + || native_u16(&message[6..8])? & libc::NLM_F_MULTI as u16 != 0 + || native_u32(&message[NETLINK_HEADER + 4..NETLINK_HEADER + 8])? != index as u32 + { + return Err(invalid( + "Linux TUN link response is not the requested device", + )); + } + let attributes = &message[NETLINK_HEADER + LINK_HEADER..]; + let mtu = attribute(attributes, libc::IFLA_MTU)? + .ok_or_else(|| invalid("Linux TUN link response is missing MTU"))?; + if native_u32(mtu)? != TUN_MTU as u32 { + return Err(invalid("Linux TUN interface MTU must be 1500")); + } + let info = attribute(attributes, libc::IFLA_LINKINFO)? + .ok_or_else(|| invalid("Linux TUN link response is missing device information"))?; + if attribute(info, libc::IFLA_INFO_KIND)? != Some(&b"tun\0"[..]) { + return Err(invalid("Linux TUN link response is not a TUN device")); + } + let data = attribute(info, libc::IFLA_INFO_DATA)? + .ok_or_else(|| invalid("Linux TUN link response is missing TUN parameters"))?; + for (kind, expected) in [ + (IFLA_TUN_TYPE, libc::IFF_TUN as u8), + (IFLA_TUN_PI, 0), + (IFLA_TUN_VNET_HDR, 0), + (IFLA_TUN_MULTI_QUEUE, 0), + ] { + if attribute(data, kind)? != Some(&[expected][..]) { + return Err(invalid( + "Linux TUN requires raw-IP single-queue parameters without VNET headers", + )); + } + } + Ok(()) +} + +// Native-endian, four-byte-aligned UAPI attributes. Unknown attributes are +// skipped for kernel-version independence; malformed and duplicate matches +// fail closed. There is no recursive parsing or heap allocation. +fn attribute(mut bytes: &[u8], expected: u16) -> io::Result> { + let mut found = None; + while !bytes.is_empty() { + if bytes.len() < 4 { + return Err(invalid("Linux TUN link attribute is incomplete")); + } + let len = native_u16(&bytes[..2])? as usize; + let kind = native_u16(&bytes[2..4])?; + let aligned = (len + 3) & !3; + if len < 4 || aligned > bytes.len() { + return Err(invalid("Linux TUN link attribute length is invalid")); + } + if kind & 0x3fff == expected { + if found.is_some() || kind & 0x4000 != 0 { + return Err(invalid("Linux TUN link attribute is ambiguous")); + } + found = Some(&bytes[4..len]); + } + bytes = &bytes[aligned..]; + } + Ok(found) +} + +fn native_u16(bytes: &[u8]) -> io::Result { + bytes + .try_into() + .map(u16::from_ne_bytes) + .map_err(|_| invalid("Linux TUN link integer length is invalid")) +} + +fn native_u32(bytes: &[u8]) -> io::Result { + bytes + .try_into() + .map(u32::from_ne_bytes) + .map_err(|_| invalid("Linux TUN link integer length is invalid")) +} + +fn socket(domain: i32, kind: i32, protocol: i32) -> io::Result { + // SAFETY: socket returns a fresh descriptor or an error without ownership. + let fd = unsafe { libc::socket(domain, kind | libc::SOCK_CLOEXEC, protocol) }; + if fd < 0 { + return Err(os_error("Linux TUN metadata socket")); + } + // SAFETY: a successful socket call returns a newly owned descriptor. + Ok(unsafe { OwnedFd::from_raw_fd(fd) }) +} + +fn invalid(message: &'static str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidInput, message) +} + +fn os_error(operation: &'static str) -> io::Error { + let error = io::Error::last_os_error(); + io::Error::new(error.kind(), format!("{operation} failed: {error}")) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn attr(kind: u16, value: &[u8]) -> Vec { + let len = 4 + value.len(); + let mut bytes = Vec::new(); + bytes.extend_from_slice(&(len as u16).to_ne_bytes()); + bytes.extend_from_slice(&kind.to_ne_bytes()); + bytes.extend_from_slice(value); + bytes.resize((len + 3) & !3, 0); + bytes + } + + fn link_message(mtu: u32, pi: u8, vnet: u8, multi: u8) -> Vec { + let mut data = attr(IFLA_TUN_TYPE, &[libc::IFF_TUN as u8]); + data.extend(attr(IFLA_TUN_PI, &[pi])); + data.extend(attr(IFLA_TUN_VNET_HDR, &[vnet])); + data.extend(attr(IFLA_TUN_MULTI_QUEUE, &[multi])); + let mut info = attr(libc::IFLA_INFO_KIND, b"tun\0"); + info.extend(attr(libc::IFLA_INFO_DATA, &data)); + let mut message = vec![0; NETLINK_HEADER + LINK_HEADER]; + message[4..6].copy_from_slice(&libc::RTM_NEWLINK.to_ne_bytes()); + message[8..12].copy_from_slice(&QUERY_SEQUENCE.to_ne_bytes()); + message[NETLINK_HEADER + 4..NETLINK_HEADER + 8].copy_from_slice(&7_i32.to_ne_bytes()); + message.extend(attr(libc::IFLA_MTU, &mtu.to_ne_bytes())); + message.extend(attr(libc::IFLA_LINKINFO, &info)); + let len = message.len() as u32; + message[..4].copy_from_slice(&len.to_ne_bytes()); + message + } + + #[test] + fn raw_ip_single_queue_requires_exact_flags_and_kernel_parameters() { + let required = (libc::IFF_TUN | libc::IFF_NO_PI) as u16; + assert!(validate_flags(required).is_ok()); + for flags in [ + libc::IFF_TUN, + libc::IFF_TAP | libc::IFF_NO_PI, + i32::from(required) | libc::IFF_VNET_HDR, + i32::from(required) | libc::IFF_MULTI_QUEUE, + i32::from(required) | libc::IFF_NAPI, + i32::from(required) | libc::IFF_PERSIST, + ] { + assert!(validate_flags(flags as u16).is_err()); + } + assert!(validate_link_message(&link_message(1500, 0, 0, 0), 7).is_ok()); + // TUNGETIFF can falsely expose NO_PI via the NOFILTER alias. The + // authoritative IFLA_TUN_PI field must still reject this device. + for parameters in [ + (1500, 1, 0, 0), + (1500, 0, 1, 0), + (1500, 0, 0, 1), + (1400, 0, 0, 0), + ] { + let (mtu, pi, vnet, multi) = parameters; + assert!(validate_link_message(&link_message(mtu, pi, vnet, multi), 7).is_err()); + } + } + + #[test] + fn link_metadata_rejects_truncation_duplicates_and_wrong_identity() { + let message = link_message(1500, 0, 0, 0); + for end in 0..message.len() { + assert!(validate_link_message(&message[..end], 7).is_err()); + } + assert!(validate_link_message(&message, 8).is_err()); + let mut wrong_sequence = message.clone(); + wrong_sequence[8..12].copy_from_slice(&2_u32.to_ne_bytes()); + assert!(validate_link_message(&wrong_sequence, 7).is_err()); + let mut malformed = message.clone(); + malformed.extend_from_slice(&[8, 0, 99, 0]); + let len = malformed.len() as u32; + malformed[..4].copy_from_slice(&len.to_ne_bytes()); + assert!(validate_link_message(&malformed, 7).is_err()); + let mut duplicate = message; + duplicate.extend(attr(libc::IFLA_MTU, &1500_u32.to_ne_bytes())); + let len = duplicate.len() as u32; + duplicate[..4].copy_from_slice(&len.to_ne_bytes()); + assert!(validate_link_message(&duplicate, 7).is_err()); + assert!(attribute(&[3, 0, 4, 0], 4).is_err()); + assert!(attribute(&[8, 0, 4, 0], 4).is_err()); + } +} diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 037e704..8c6d1f5 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -1,15 +1,28 @@ -#[cfg(all(feature = "ffi", any(target_os = "ios", target_os = "macos")))] +#[cfg(all( + feature = "ffi", + any(target_os = "ios", target_os = "tvos", target_os = "macos") +))] pub(crate) mod apple_logging; -#[cfg(all(feature = "ffi", any(target_os = "ios", target_os = "macos")))] +#[cfg(all(target_os = "linux", feature = "tun"))] +mod linux_tun; +#[cfg(all( + feature = "ffi", + any(target_os = "ios", target_os = "tvos", target_os = "macos") +))] #[cfg_attr(target_os = "macos", allow(dead_code))] pub(crate) mod process_memory; #[cfg(all(unix, feature = "tun"))] mod rust_tun_io; #[cfg(all(unix, feature = "tun"))] mod tun_fd; -#[cfg(all(windows, feature = "tun"))] +#[cfg(all(feature = "tun", any(windows, test)))] +#[cfg_attr(not(windows), allow(dead_code))] mod windows_tun_io; +/// Ready packets processed per turn; never wait to fill this batch. +#[cfg(feature = "tun")] +pub(crate) const TUN_PACKET_BATCH_SIZE: usize = 8; + #[cfg(all(unix, feature = "tun"))] pub use rust_tun_io::RustTunIo as TunIo; #[cfg(all(unix, feature = "tun"))] diff --git a/src/platform/process_memory.rs b/src/platform/process_memory.rs index 0ae1cf2..ffd4f6b 100644 --- a/src/platform/process_memory.rs +++ b/src/platform/process_memory.rs @@ -33,6 +33,22 @@ const THRESHOLDS: [MemoryThreshold; 3] = [ static TELEMETRY_POLICY: MemoryTelemetryPolicy = MemoryTelemetryPolicy::new(); +// Preserve existing iOS event identifiers for consumers, without labelling +// tvOS samples as iOS. These are diagnostics, not platform memory quotas. +const MEMORY_EVENTS: [&str; 3] = if cfg!(target_os = "tvos") { + [ + "tvos_memory_snapshot", + "tvos_memory_observation_target_crossed", + "tvos_memory_measurement_failed", + ] +} else { + [ + "ios_memory_snapshot", + "ios_memory_observation_target_crossed", + "ios_memory_measurement_failed", + ] +}; + /// Public `TASK_VM_INFO` layout through revision 3. Revision 3 is the first /// revision that contains `ledger_phys_footprint_peak`; using that fixed, /// documented prefix avoids depending on newer SDK-only tail fields. @@ -136,35 +152,38 @@ pub(crate) fn observe(stage: &'static str) { newly_crossed, } => { tracing::info!( - event = "ios_memory_snapshot", + event = MEMORY_EVENTS[0], + platform = std::env::consts::OS, stage, current_phys_footprint_bytes = snapshot.current_phys_footprint, process_lifetime_peak_phys_footprint_bytes = snapshot.lifetime_peak_phys_footprint, - "iOS TUN memory telemetry" + "Apple TUN memory telemetry" ); for threshold in THRESHOLDS .iter() .filter(|threshold| newly_crossed & threshold.bit != 0) { tracing::warn!( - event = "ios_memory_observation_target_crossed", + event = MEMORY_EVENTS[1], + platform = std::env::consts::OS, stage, current_phys_footprint_bytes = snapshot.current_phys_footprint, process_lifetime_peak_phys_footprint_bytes = snapshot.lifetime_peak_phys_footprint, threshold_bytes = threshold.bytes, threshold_mib = threshold.mib, - "iOS TUN current memory crossed an observation threshold" + "Apple TUN current memory crossed an observation threshold" ); } } TelemetryObservation::MeasurementFailed { error, first } => { if first { tracing::warn!( - event = "ios_memory_measurement_failed", + event = MEMORY_EVENTS[2], + platform = std::env::consts::OS, stage, error = %error, - "unable to sample iOS TUN memory telemetry" + "unable to sample Apple TUN memory telemetry" ); } } @@ -219,7 +238,7 @@ pub(crate) fn relieve_allocator_pressure() { fn malloc_zone_pressure_relief(zone: *mut c_void, goal: usize) -> usize; } // SAFETY: a null zone asks the allocator to inspect all zones; goal zero - // requests best-effort pressure relief. The API is available on iOS 4.3+. + // requests best-effort pressure relief. Available on supported iOS/tvOS targets. let _released = unsafe { malloc_zone_pressure_relief(ptr::null_mut(), 0) }; } diff --git a/src/platform/rust_tun_io.rs b/src/platform/rust_tun_io.rs index 5772479..4e296b2 100644 --- a/src/platform/rust_tun_io.rs +++ b/src/platform/rust_tun_io.rs @@ -4,13 +4,13 @@ use tokio::io::unix::AsyncFd; use crate::{IpVersion, Result, TunFraming, VCoreError}; -use super::TunFd; +use super::{TUN_PACKET_BATCH_SIZE, TunFd}; // The current config protocol accepts only MTU 1500. Keeping the slice passed // to rust-tun at exactly that size is also important on Apple: its PI adapter // uses a fixed 1504-byte stack buffer at this size, but allocates a temporary // Vec for larger reads and writes. -const TUN_MTU: usize = 1_500; +pub(super) const TUN_MTU: usize = 1_500; /// Non-blocking raw-IP packet I/O backed by rust-tun. /// @@ -35,6 +35,14 @@ impl fmt::Debug for RustTunIo { impl RustTunIo { pub fn new(fd: TunFd, framing: TunFraming) -> Result { + #[cfg(target_os = "linux")] + if framing != TunFraming::RawIp { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Linux TUN requires rawIp framing", + ) + .into()); + } let mut configuration = rust_tun::Configuration::default(); configuration .raw_fd(fd.into_raw_fd()) @@ -78,18 +86,107 @@ impl RustTunIo { Ok(version) } + /// Waits for the first packet, then drains only immediately ready packets. + /// Each consumed packet has its own outcome, so an invalid packet cannot + /// discard valid neighbours. A fatal error preserves the consumed prefix. + pub(crate) async fn read_packets( + &self, + packets: &mut [Vec], + outcomes: &mut Vec>, + ) -> Result<()> { + debug_assert!(!packets.is_empty() && packets.len() <= TUN_PACKET_BATCH_SIZE); + outcomes.clear(); + while outcomes.len() < packets.len() { + let mut ready = self.device.readable().await?; + loop { + let packet = &mut packets[outcomes.len()]; + packet.clear(); + packet.resize(TUN_MTU, 0); + match ready.try_io(|inner| inner.get_ref().recv(packet)) { + Ok(Ok(0)) => { + return Err( + io::Error::new(io::ErrorKind::UnexpectedEof, "TUN closed").into() + ); + } + Ok(Ok(size)) => { + packet.truncate(size); + outcomes.push(TunFraming::RawIp.decode(packet).map(|(version, _)| version)); + if outcomes.len() == packets.len() { + return Ok(()); + } + } + Ok(Err(error)) => return Err(error.into()), + Err(_would_block) if !outcomes.is_empty() => return Ok(()), + Err(_would_block) => break, + } + } + } + Ok(()) + } + + /// Writes independent packets while sharing a readiness observation. + /// Outcomes are recorded immediately, including local invalid-packet + /// drops, so failure or cancellation never hides an accepted prefix. + pub(crate) async fn write_packets( + &self, + packets: &[&[u8]], + outcomes: &mut Vec>, + ) -> Result<()> { + debug_assert!(!packets.is_empty() && packets.len() <= TUN_PACKET_BATCH_SIZE); + outcomes.clear(); + let mut readiness = None; + for packet in packets { + let version = match validate_write_packet(packet) { + Ok(version) => version, + Err(error) => { + outcomes.push(Err(error)); + continue; + } + }; + let written = loop { + let ready = match &mut readiness { + Some(ready) => ready, + None => readiness.insert(self.device.writable().await?), + }; + match ready.try_io(|inner| inner.get_ref().send(packet)) { + #[cfg(target_vendor = "apple")] + Ok(Err(error)) if error.raw_os_error() == Some(libc::ENOBUFS) => { + // Only this unaccepted packet is retried. Drop the + // guard across the same cancellable Darwin backoff + // used by the single-packet path. + readiness = None; + tokio::time::sleep(std::time::Duration::from_millis(1)).await; + } + Ok(result) => break result?, + Err(_would_block) => readiness = None, + } + }; + if written != packet.len() { + return Err( + io::Error::new(io::ErrorKind::WriteZero, "partial TUN packet write").into(), + ); + } + outcomes.push(Ok(version)); + } + Ok(()) + } + /// Writes one complete packet. Partial writes are rejected because retrying /// a suffix would create a second malformed TUN packet. pub async fn write_packet(&self, packet: &[u8]) -> Result { - if packet.len() > TUN_MTU { - return Err(VCoreError::InvalidPacket( - "TUN packet exceeds configured MTU", - )); - } - let (version, _) = TunFraming::RawIp.decode(packet)?; + let version = validate_write_packet(packet)?; let written = loop { let mut ready = self.device.writable().await?; match ready.try_io(|inner| inner.get_ref().send(packet)) { + #[cfg(target_vendor = "apple")] + Ok(Err(error)) if error.raw_os_error() == Some(libc::ENOBUFS) => { + // Darwin can exhaust packet/mbuf space after a cached + // writable event. No packet was accepted: retain this one + // packet, yield without spinning, and let Stop cancel the + // future. Do not resize the host's shared socket buffers. + drop(ready); + tokio::time::sleep(std::time::Duration::from_millis(1)).await; + } Ok(result) => break result?, Err(_would_block) => continue, } @@ -104,6 +201,15 @@ impl RustTunIo { } } +fn validate_write_packet(packet: &[u8]) -> Result { + if packet.len() > TUN_MTU { + return Err(VCoreError::InvalidPacket( + "TUN packet exceeds configured MTU", + )); + } + TunFraming::RawIp.decode(packet).map(|(version, _)| version) +} + #[cfg(target_vendor = "apple")] fn configure_platform_framing(configuration: &mut rust_tun::Configuration, framing: TunFraming) { configuration.platform_config(|platform| { @@ -140,7 +246,7 @@ mod tests { async fn raw_ip_read_reuses_caller_buffer_for_ipv4_and_ipv6() { let (host, peer) = UnixDatagram::pair().unwrap(); host.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); let io = RustTunIo::new(fd, TunFraming::RawIp).unwrap(); let mut packet = Vec::with_capacity(1500); @@ -152,13 +258,147 @@ mod tests { } } + #[tokio::test] + async fn batch_read_preserves_valid_neighbours_of_an_invalid_packet() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::RawIp, + ) + .unwrap(); + for packet in [&IPV4[..], &[0x70][..], &IPV6[..]] { + peer.send(packet).unwrap(); + } + let mut packets = + std::array::from_fn::<_, TUN_PACKET_BATCH_SIZE, _>(|_| Vec::with_capacity(TUN_MTU)); + let mut outcomes = Vec::with_capacity(TUN_PACKET_BATCH_SIZE); + io.read_packets(&mut packets, &mut outcomes).await.unwrap(); + + assert_eq!(outcomes.len(), 3); + assert_eq!(outcomes[0].as_ref().unwrap(), &IpVersion::V4); + assert!(matches!(outcomes[1], Err(VCoreError::InvalidPacket(_)))); + assert_eq!(outcomes[2].as_ref().unwrap(), &IpVersion::V6); + assert_eq!(packets[0], IPV4); + assert_eq!(packets[1], [0x70]); + assert_eq!(packets[2], IPV6); + assert!(packets.iter().all(|packet| packet.capacity() == TUN_MTU)); + } + + #[tokio::test] + async fn batch_read_is_bounded_and_does_not_wait_to_fill() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::RawIp, + ) + .unwrap(); + for _ in 0..=TUN_PACKET_BATCH_SIZE { + peer.send(&IPV4).unwrap(); + } + let mut packets = std::array::from_fn::<_, TUN_PACKET_BATCH_SIZE, _>(|_| Vec::new()); + let mut outcomes = Vec::new(); + io.read_packets(&mut packets, &mut outcomes).await.unwrap(); + assert_eq!(outcomes.len(), TUN_PACKET_BATCH_SIZE); + tokio::time::timeout( + std::time::Duration::from_secs(1), + io.read_packets(&mut packets, &mut outcomes), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(outcomes.len(), 1); + assert_eq!(packets[0], IPV4); + } + + #[tokio::test] + async fn batch_read_waits_for_first_packet_and_can_be_cancelled() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::RawIp, + ) + .unwrap(); + let mut packets = [Vec::new()]; + let mut outcomes = vec![Ok(IpVersion::V6)]; + assert!( + tokio::time::timeout( + std::time::Duration::from_millis(5), + io.read_packets(&mut packets, &mut outcomes), + ) + .await + .is_err() + ); + assert!(outcomes.is_empty()); + peer.send(&IPV4).unwrap(); + io.read_packets(&mut packets, &mut outcomes).await.unwrap(); + assert_eq!(outcomes.len(), 1); + assert_eq!(packets[0], IPV4); + } + + #[tokio::test] + async fn batch_read_preserves_consumed_prefix_on_eof() { + let (host, mut peer) = UnixStream::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::RawIp, + ) + .unwrap(); + peer.write_all(&IPV4).unwrap(); + drop(peer); + let mut packets = [Vec::new(), Vec::new()]; + let mut outcomes = Vec::new(); + assert!(matches!( + io.read_packets(&mut packets, &mut outcomes).await, + Err(VCoreError::Io(ref error)) if error.kind() == io::ErrorKind::UnexpectedEof + )); + assert_eq!(outcomes.len(), 1); + assert_eq!(outcomes[0].as_ref().unwrap(), &IpVersion::V4); + assert_eq!(packets[0], IPV4); + } + + #[tokio::test] + async fn batch_write_keeps_packet_boundaries_and_isolates_invalid_packets() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::RawIp, + ) + .unwrap(); + let oversized = vec![0x45; TUN_MTU + 1]; + let mut outcomes = Vec::new(); + io.write_packets(&[&IPV4, &[0x70], &oversized, &IPV6], &mut outcomes) + .await + .unwrap(); + assert_eq!(outcomes.len(), 4); + assert_eq!(outcomes[0].as_ref().unwrap(), &IpVersion::V4); + assert!(matches!(outcomes[1], Err(VCoreError::InvalidPacket(_)))); + assert!(matches!(outcomes[2], Err(VCoreError::InvalidPacket(_)))); + assert_eq!(outcomes[3].as_ref().unwrap(), &IpVersion::V6); + let mut received = [0_u8; TUN_MTU]; + for packet in [&IPV4[..], &IPV6[..]] { + let size = peer.recv(&mut received).unwrap(); + assert_eq!(&received[..size], packet); + } + assert_eq!( + peer.recv(&mut received).unwrap_err().kind(), + io::ErrorKind::WouldBlock + ); + } + #[tokio::test] async fn rust_tun_drop_closes_only_duplicate_and_preserves_host_flags() { let (mut host, mut peer) = UnixStream::pair().unwrap(); host.set_nonblocking(true).unwrap(); // SAFETY: host remains open for both flag reads. let before = unsafe { libc::fcntl(host.as_raw_fd(), libc::F_GETFL) }; - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); let io = RustTunIo::new(fd, TunFraming::RawIp).unwrap(); drop(io); // SAFETY: rust-tun owns only the duplicate; host remains open. @@ -175,7 +415,7 @@ mod tests { async fn raw_ip_rejects_invalid_version_and_oversized_write() { let (host, peer) = UnixDatagram::pair().unwrap(); host.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); let io = RustTunIo::new(fd, TunFraming::RawIp).unwrap(); peer.send(&[0x70]).unwrap(); @@ -195,7 +435,7 @@ mod tests { async fn zero_length_read_is_tun_eof() { let (host, peer) = UnixStream::pair().unwrap(); host.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); let io = RustTunIo::new(fd, TunFraming::RawIp).unwrap(); drop(peer); @@ -206,12 +446,176 @@ mod tests { )); } + #[cfg(target_vendor = "apple")] + #[tokio::test] + async fn utun_full_packet_queue_waits_without_losing_or_replaying_a_packet() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::Utun, + ) + .unwrap(); + let mut frame = 2_u32.to_be_bytes().to_vec(); + frame.extend_from_slice(&IPV4); + // Cache a writable readiness observation before the peer queue fills. + // Starting with an already-full fd would only test the initial wait. + io.write_packet(&IPV4).await.unwrap(); + let mut queued = 1; + loop { + match host.send(&frame) { + Ok(_) => { + queued += 1; + assert!(queued < 65536); + } + Err(error) + if error.kind() == io::ErrorKind::WouldBlock + || error.raw_os_error() == Some(libc::ENOBUFS) => + { + break; + } + Err(error) => panic!("unexpected local queue error: {error}"), + } + } + let write = io.write_packet(&IPV6); + tokio::pin!(write); + assert!( + tokio::time::timeout(std::time::Duration::from_millis(5), &mut write) + .await + .is_err(), + "a full local packet queue must apply backpressure, not terminate TUN" + ); + let mut received = [0; 64]; + assert_eq!(peer.recv(&mut received).unwrap(), frame.len()); + assert_eq!( + tokio::time::timeout(std::time::Duration::from_secs(1), &mut write) + .await + .unwrap() + .unwrap(), + IpVersion::V6 + ); + for _ in 1..queued { + assert_eq!(peer.recv(&mut received).unwrap(), frame.len()); + assert_eq!(&received[..frame.len()], frame); + } + assert_eq!(peer.recv(&mut received).unwrap(), IPV6.len() + 4); + assert_eq!(&received[..4], 30_u32.to_be_bytes()); + assert_eq!(&received[4..IPV6.len() + 4], IPV6); + assert_eq!( + peer.recv(&mut received).unwrap_err().kind(), + io::ErrorKind::WouldBlock + ); + // Cancelling a blocked write must not leave a retry task behind. + io.write_packet(&IPV4).await.unwrap(); + while host.send(&frame).is_ok() {} + assert!( + tokio::time::timeout(std::time::Duration::from_millis(5), io.write_packet(&IPV6)) + .await + .is_err() + ); + while let Ok(size) = peer.recv(&mut received) { + assert_eq!(&received[..size], frame); + } + tokio::time::sleep(std::time::Duration::from_millis(5)).await; + assert_eq!( + peer.recv(&mut received).unwrap_err().kind(), + io::ErrorKind::WouldBlock + ); + } + + #[cfg(target_vendor = "apple")] + #[tokio::test] + async fn cancelling_batch_write_preserves_accepted_prefix_without_replaying_it() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::Utun, + ) + .unwrap(); + let mut frame = 2_u32.to_be_bytes().to_vec(); + frame.extend_from_slice(&IPV4); + io.write_packet(&IPV4).await.unwrap(); + let mut queued = 1; + loop { + match host.send(&frame) { + Ok(_) => { + queued += 1; + assert!(queued < 65536); + } + Err(error) + if error.kind() == io::ErrorKind::WouldBlock + || error.raw_os_error() == Some(libc::ENOBUFS) => + { + break; + } + Err(error) => panic!("unexpected local queue error: {error}"), + } + } + let mut received = [0_u8; 64]; + peer.recv(&mut received).unwrap(); + let mut outcomes = Vec::new(); + assert!( + tokio::time::timeout( + std::time::Duration::from_millis(10), + io.write_packets(&[&IPV4, &IPV6], &mut outcomes), + ) + .await + .is_err() + ); + assert_eq!(outcomes.len(), 1); + assert_eq!(outcomes[0].as_ref().unwrap(), &IpVersion::V4); + // One original frame was removed, and the first batch packet replaced + // it. Cancelling the pending second packet leaves exactly this count. + for _ in 0..queued { + let size = peer.recv(&mut received).unwrap(); + assert_eq!(&received[..size], frame); + } + tokio::time::sleep(std::time::Duration::from_millis(5)).await; + assert_eq!( + peer.recv(&mut received).unwrap_err().kind(), + io::ErrorKind::WouldBlock + ); + } + + #[cfg(target_vendor = "apple")] + #[tokio::test] + async fn utun_batches_keep_each_packet_information_header_independent() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + let io = RustTunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + TunFraming::Utun, + ) + .unwrap(); + let mut outcomes = Vec::new(); + io.write_packets(&[&IPV4, &IPV6], &mut outcomes) + .await + .unwrap(); + let mut received = [0_u8; 64]; + for (packet, family) in [(&IPV4[..], 2_u32), (&IPV6[..], 30_u32)] { + let size = peer.recv(&mut received).unwrap(); + assert_eq!(&received[..4], family.to_be_bytes()); + assert_eq!(&received[4..size], packet); + peer.send(&received[..size]).unwrap(); + } + let mut packets = [Vec::new(), Vec::new()]; + io.read_packets(&mut packets, &mut outcomes).await.unwrap(); + assert_eq!(outcomes.len(), 2); + assert_eq!(outcomes[0].as_ref().unwrap(), &IpVersion::V4); + assert_eq!(outcomes[1].as_ref().unwrap(), &IpVersion::V6); + assert_eq!(packets[0], IPV4); + assert_eq!(packets[1], IPV6); + } + #[cfg(target_vendor = "apple")] #[tokio::test] async fn utun_write_adds_darwin_family_header_for_ipv4_and_ipv6() { let (host, peer) = UnixDatagram::pair().unwrap(); host.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); let io = RustTunIo::new(fd, TunFraming::Utun).unwrap(); for (packet, family, version) in [ @@ -231,7 +635,7 @@ mod tests { async fn utun_read_strips_darwin_family_header_for_ipv4_and_ipv6() { let (host, peer) = UnixDatagram::pair().unwrap(); host.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); let io = RustTunIo::new(fd, TunFraming::Utun).unwrap(); let mut packet = Vec::new(); diff --git a/src/platform/tun_fd.rs b/src/platform/tun_fd.rs index 33870bb..ee8de80 100644 --- a/src/platform/tun_fd.rs +++ b/src/platform/tun_fd.rs @@ -14,8 +14,24 @@ impl TunFd { /// host must supply a nonblocking descriptor because `dup` shares file /// status flags with the original open-file description; VCore never /// changes those shared flags behind the host's back. - /// + /// Linux additionally validates a real, raw-IP, single-queue TUN with MTU + /// 1500 in its owning network namespace before accepting the duplicate. pub fn duplicate(borrowed_fd: RawFd) -> Result { + let fd = Self::duplicate_descriptor(borrowed_fd)?; + #[cfg(target_os = "linux")] + super::linux_tun::validate(fd.as_fd())?; + Ok(fd) + } + + // Pure packet-I/O fixtures use a Unix socketpair, not a kernel TUN. This + // ownership-only seam does not exist in production and is never Linux + // real-TUN acceptance evidence. + #[cfg(test)] + pub(crate) fn duplicate_mock(borrowed_fd: RawFd) -> Result { + Self::duplicate_descriptor(borrowed_fd) + } + + fn duplicate_descriptor(borrowed_fd: RawFd) -> Result { // SAFETY: F_GETFL reads flags without taking ownership of the borrowed // descriptor and reports EBADF for an invalid FFI argument. let status_flags = unsafe { libc::fcntl(borrowed_fd, libc::F_GETFL) }; @@ -79,7 +95,7 @@ mod tests { fn duplicate_does_not_take_original_ownership() { let (mut original, mut peer) = UnixStream::pair().unwrap(); original.set_nonblocking(true).unwrap(); - let duplicate = TunFd::duplicate(original.as_raw_fd()).unwrap(); + let duplicate = TunFd::duplicate_mock(original.as_raw_fd()).unwrap(); assert_ne!(duplicate.as_raw_fd(), original.as_raw_fd()); // SAFETY: both descriptors are open for the duration of these calls. @@ -114,4 +130,22 @@ mod tests { let result = TunFd::duplicate(-1); assert!(result.is_err()); } + + #[cfg(target_os = "linux")] + #[test] + fn non_tun_descriptor_is_rejected_without_changing_host_ownership_or_flags() { + let (mut original, mut peer) = UnixStream::pair().unwrap(); + original.set_nonblocking(true).unwrap(); + // SAFETY: original stays open for both status reads. + let before = unsafe { libc::fcntl(original.as_raw_fd(), libc::F_GETFL) }; + let error = TunFd::duplicate(original.as_raw_fd()).unwrap_err(); + assert!(error.to_string().contains("Linux TUNGETIFF")); + // SAFETY: rejection closes only VCore's temporary duplicate. + let after = unsafe { libc::fcntl(original.as_raw_fd(), libc::F_GETFL) }; + assert_eq!(after, before); + original.write_all(b"ok").unwrap(); + let mut bytes = [0; 2]; + peer.read_exact(&mut bytes).unwrap(); + assert_eq!(&bytes, b"ok"); + } } diff --git a/src/platform/windows_tun_io.rs b/src/platform/windows_tun_io.rs index 3d3e5a0..93c225f 100644 --- a/src/platform/windows_tun_io.rs +++ b/src/platform/windows_tun_io.rs @@ -1,16 +1,21 @@ use std::{ collections::VecDeque, - fmt, io, + fmt, + future::poll_fn, + io, sync::{ Arc, Mutex, atomic::{AtomicU64, Ordering}, }, + task::Poll, }; use tokio::sync::{Mutex as AsyncMutex, mpsc}; use crate::{IpVersion, Result, TunFraming, VCoreError}; +use super::TUN_PACKET_BATCH_SIZE; + const TUN_MTU: usize = 1_500; type Wake = Arc io::Result<()> + Send + Sync>; @@ -60,6 +65,7 @@ impl WindowsTunIo { ) } + #[cfg(test)] pub(crate) async fn read_packet(&self, packet: &mut Vec) -> Result { let received = self.ingress.lock().await.recv().await.ok_or_else(closed)?; let version = packet_version(&received)?; @@ -67,31 +73,88 @@ impl WindowsTunIo { Ok(version) } + pub(crate) async fn read_packets( + &self, + packets: &mut [Vec], + outcomes: &mut Vec>, + ) -> Result<()> { + outcomes.clear(); + validate_batch_size(packets.len())?; + let mut ingress = self.ingress.lock().await; + packets[0] = ingress.recv().await.ok_or_else(closed)?; + outcomes.push(packet_version(&packets[0])); + for packet in &mut packets[1..] { + // Return the prefix instead of parking a worker or waiting for a + // producer when the next receive is not immediately ready. + match poll_fn(|cx| Poll::Ready(ingress.poll_recv(cx))).await { + Poll::Ready(Some(received)) => { + *packet = received; + outcomes.push(packet_version(packet)); + } + Poll::Ready(None) | Poll::Pending => break, + } + } + Ok(()) + } + pub(crate) async fn read_packet_batch( &self, packets: &mut Vec>, max_packets: usize, ) -> Result<()> { - debug_assert!(max_packets > 0); packets.clear(); + validate_batch_size(max_packets)?; let mut ingress = self.ingress.lock().await; let first = ingress.recv().await.ok_or_else(closed)?; - packet_version(&first)?; - packets.push(first); - while packets.len() < max_packets { - match ingress.try_recv() { - Ok(packet) => { - packet_version(&packet)?; - packets.push(packet); - } - Err(mpsc::error::TryRecvError::Empty | mpsc::error::TryRecvError::Disconnected) => { - break; + push_valid_frame(packets, first)?; + for _ in 1..max_packets { + match poll_fn(|cx| Poll::Ready(ingress.poll_recv(cx))).await { + Poll::Ready(Some(packet)) => { + push_valid_frame(packets, packet)?; } + Poll::Ready(None) | Poll::Pending => break, } } Ok(()) } + pub(crate) async fn write_packets( + &self, + packets: &[&[u8]], + outcomes: &mut Vec>, + ) -> Result<()> { + outcomes.clear(); + validate_batch_size(packets.len())?; + let wake = { + let mut egress = + self.shared.egress.lock().map_err(|_| { + VCoreError::Platform("Windows packet queue lock poisoned".into()) + })?; + let mut wake = false; + for packet in packets { + let version = match packet_version(packet) { + Ok(version) => version, + Err(error) => { + outcomes.push(Err(error)); + continue; + } + }; + if egress.len() == self.shared.capacity { + saturating_increment(&self.shared.egress_dropped); + } else { + wake |= egress.is_empty(); + egress.push_back(packet.to_vec()); + } + outcomes.push(Ok(version)); + } + wake + }; + if wake { + (self.shared.wake)()?; + } + Ok(()) + } + pub(crate) async fn write_packet(&self, packet: &[u8]) -> Result { let version = packet_version(packet)?; let wake = { @@ -164,6 +227,30 @@ fn packet_version(packet: &[u8]) -> Result { TunFraming::RawIp.decode(packet).map(|(version, _)| version) } +fn validate_batch_size(size: usize) -> Result<()> { + if !(1..=TUN_PACKET_BATCH_SIZE).contains(&size) { + return Err(VCoreError::Platform( + "invalid Windows TUN packet batch size".into(), + )); + } + Ok(()) +} + +fn push_valid_frame(packets: &mut Vec>, packet: Vec) -> Result<()> { + if packet.is_empty() || packet.len() > TUN_MTU { + return Err(VCoreError::Io(io::Error::new( + io::ErrorKind::InvalidData, + "invalid Windows packet frame size", + ))); + } + match packet_version(&packet) { + Ok(_) => packets.push(packet), + Err(VCoreError::InvalidPacket(_)) => {} + Err(error) => return Err(error), + } + Ok(()) +} + fn closed() -> VCoreError { VCoreError::Io(io::Error::new( io::ErrorKind::UnexpectedEof, @@ -172,7 +259,7 @@ fn closed() -> VCoreError { } fn saturating_increment(counter: &AtomicU64) { - _ = counter.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |value| { + _ = counter.try_update(Ordering::Relaxed, Ordering::Relaxed, |value| { Some(value.saturating_add(1)) }); } @@ -218,6 +305,176 @@ mod tests { assert_eq!(packets, [IPV6]); } + #[tokio::test] + async fn common_ingress_batch_keeps_invalid_packets_isolated_and_bounds_ready_drain() { + let (io, adapter) = WindowsTunIo::new(TUN_PACKET_BATCH_SIZE + 1, || Ok(())); + for index in 0..=TUN_PACKET_BATCH_SIZE { + let packet = match index { + 1 => vec![0xff], + 2 => IPV6.to_vec(), + _ => IPV4.to_vec(), + }; + assert!(adapter.try_send(packet)); + } + let mut packets = vec![Vec::new(); TUN_PACKET_BATCH_SIZE]; + let mut outcomes = Vec::new(); + io.read_packets(&mut packets, &mut outcomes).await.unwrap(); + assert_eq!(outcomes.len(), TUN_PACKET_BATCH_SIZE); + assert!(matches!(outcomes[0], Ok(IpVersion::V4))); + assert!(matches!(outcomes[1], Err(VCoreError::InvalidPacket(_)))); + assert!(matches!(outcomes[2], Ok(IpVersion::V6))); + assert_eq!(packets[0], IPV4); + assert_eq!(packets[1], [0xff]); + assert_eq!(packets[2], IPV6); + + tokio::time::timeout( + std::time::Duration::from_millis(100), + io.read_packets(&mut packets, &mut outcomes), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(outcomes.len(), 1); + assert!(matches!(outcomes[0], Ok(IpVersion::V4))); + assert_eq!(packets[0], IPV4); + } + + #[tokio::test] + async fn common_ingress_batch_keeps_received_prefix_before_channel_close() { + let (io, adapter) = WindowsTunIo::new(2, || Ok(())); + assert!(adapter.try_send(IPV4.to_vec())); + assert!(adapter.try_send(IPV6.to_vec())); + drop(adapter); + let mut packets = vec![Vec::new(); TUN_PACKET_BATCH_SIZE]; + let mut outcomes = Vec::new(); + io.read_packets(&mut packets, &mut outcomes).await.unwrap(); + assert_eq!(outcomes.len(), 2); + assert!(matches!(outcomes[0], Ok(IpVersion::V4))); + assert!(matches!(outcomes[1], Ok(IpVersion::V6))); + assert!(matches!( + io.read_packets(&mut packets, &mut outcomes).await, + Err(VCoreError::Io(error)) if error.kind() == io::ErrorKind::UnexpectedEof + )); + assert!(outcomes.is_empty()); + } + + #[tokio::test] + async fn common_egress_batch_wakes_once_and_keeps_drop_current_semantics() { + let wakes = Arc::new(AtomicUsize::new(0)); + let observed = wakes.clone(); + let (io, adapter) = WindowsTunIo::new(2, move || { + observed.fetch_add(1, Ordering::Relaxed); + Ok(()) + }); + let mut outcomes = Vec::new(); + io.write_packets(&[IPV4, &[0xff], IPV6, IPV4], &mut outcomes) + .await + .unwrap(); + assert_eq!(outcomes.len(), 4); + assert!(matches!(outcomes[0], Ok(IpVersion::V4))); + assert!(matches!(outcomes[1], Err(VCoreError::InvalidPacket(_)))); + assert!(matches!(outcomes[2], Ok(IpVersion::V6))); + assert!(matches!(outcomes[3], Ok(IpVersion::V4))); + assert_eq!(wakes.load(Ordering::Relaxed), 1); + assert_eq!(adapter.pop_egress().as_deref(), Some(IPV4)); + assert_eq!(adapter.pop_egress().as_deref(), Some(IPV6)); + assert!(adapter.pop_egress().is_none()); + assert_eq!(adapter.stats().egress_queue_dropped, 1); + + io.write_packets(&[IPV6], &mut outcomes).await.unwrap(); + assert_eq!(outcomes.len(), 1); + assert_eq!(wakes.load(Ordering::Relaxed), 2); + io.write_packets(&[IPV4], &mut outcomes).await.unwrap(); + assert_eq!(wakes.load(Ordering::Relaxed), 2); + } + + #[tokio::test] + async fn common_egress_batch_reports_accepted_packets_before_wake_failure() { + let (io, adapter) = WindowsTunIo::new(3, || { + Err(io::Error::new(io::ErrorKind::BrokenPipe, "wake failed")) + }); + let mut outcomes = Vec::new(); + assert!(matches!( + io.write_packets(&[IPV4, &[0xff], IPV6], &mut outcomes).await, + Err(VCoreError::Io(error)) if error.kind() == io::ErrorKind::BrokenPipe + )); + assert_eq!(outcomes.len(), 3); + assert!(matches!(outcomes[0], Ok(IpVersion::V4))); + assert!(matches!(outcomes[1], Err(VCoreError::InvalidPacket(_)))); + assert!(matches!(outcomes[2], Ok(IpVersion::V6))); + assert_eq!(adapter.pop_egress().as_deref(), Some(IPV4)); + assert_eq!(adapter.pop_egress().as_deref(), Some(IPV6)); + assert!(adapter.pop_egress().is_none()); + } + + #[tokio::test] + async fn packet_channel_ingress_skips_invalid_ip_with_a_bounded_attempt_count() { + let (io, adapter) = WindowsTunIo::new(4, || Ok(())); + assert!(adapter.try_send(IPV4.to_vec())); + assert!(adapter.try_send(vec![0xff])); + assert!(adapter.try_send(IPV6.to_vec())); + let mut packets = Vec::new(); + io.read_packet_batch(&mut packets, 2).await.unwrap(); + assert_eq!(packets, [IPV4]); + io.read_packet_batch(&mut packets, 2).await.unwrap(); + assert_eq!(packets, [IPV6]); + + assert!(adapter.try_send(vec![0xff])); + assert!(adapter.try_send(vec![0xff])); + assert!(adapter.try_send(IPV4.to_vec())); + io.read_packet_batch(&mut packets, 2).await.unwrap(); + assert!(packets.is_empty()); + io.read_packet_batch(&mut packets, 2).await.unwrap(); + assert_eq!(packets, [IPV4]); + } + + #[tokio::test] + async fn packet_channel_ingress_rejects_invalid_frame_lengths() { + for packet in [Vec::new(), vec![0x45; TUN_MTU + 1]] { + let (io, adapter) = WindowsTunIo::new(1, || Ok(())); + assert!(adapter.try_send(packet)); + assert!(matches!( + io.read_packet_batch(&mut Vec::new(), TUN_PACKET_BATCH_SIZE).await, + Err(VCoreError::Io(error)) if error.kind() == io::ErrorKind::InvalidData + )); + } + } + + #[tokio::test] + async fn common_batches_reject_empty_and_oversized_slices_without_dequeuing() { + let (io, adapter) = WindowsTunIo::new(1, || Ok(())); + assert!(adapter.try_send(IPV4.to_vec())); + let mut outcomes = vec![Ok(IpVersion::V4)]; + assert!(matches!( + io.read_packets(&mut [], &mut outcomes).await, + Err(VCoreError::Platform(_)) + )); + assert!(outcomes.is_empty()); + let mut packets = vec![Vec::new(); TUN_PACKET_BATCH_SIZE + 1]; + assert!(matches!( + io.read_packets(&mut packets, &mut outcomes).await, + Err(VCoreError::Platform(_)) + )); + assert!(outcomes.is_empty()); + assert!(matches!( + io.write_packets(&[], &mut outcomes).await, + Err(VCoreError::Platform(_)) + )); + assert!(matches!( + io.write_packets(&[IPV4; TUN_PACKET_BATCH_SIZE + 1], &mut outcomes) + .await, + Err(VCoreError::Platform(_)) + )); + assert!(outcomes.is_empty()); + assert!(adapter.pop_egress().is_none()); + + io.read_packets(&mut packets[..1], &mut outcomes) + .await + .unwrap(); + assert_eq!(packets[0], IPV4); + assert_eq!(outcomes.len(), 1); + } + #[tokio::test] async fn packets_cross_the_windows_packet_adapter_and_wake_once() { let wakes = Arc::new(AtomicUsize::new(0)); diff --git a/src/quic_sniffer.rs b/src/quic_sniffer.rs index f33a7b3..cc06b7b 100644 --- a/src/quic_sniffer.rs +++ b/src/quic_sniffer.rs @@ -82,6 +82,19 @@ pub(crate) struct QuicConnectionKey { destination_connection_id: Box<[u8]>, } +impl QuicConnectionKey { + /// An exact, allocation-free identity for an authenticated Initial flow. + /// Length is explicit so zero padding cannot alias a different DCID. + pub(crate) fn routing_identity(&self) -> [u8; 25] { + let mut identity = [0; 25]; + identity[..4].copy_from_slice(&self.wire_version.to_be_bytes()); + let length = self.destination_connection_id.len(); + identity[4] = u8::try_from(length).expect("parsed QUIC DCIDs fit in 20 bytes"); + identity[5..5 + length].copy_from_slice(&self.destination_connection_id); + identity + } +} + enum QuicConnectionObservation { Initial(QuicConnectionKey), UnsupportedVersion, diff --git a/src/resources.rs b/src/resources.rs index cc37625..07d8924 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -10,10 +10,11 @@ use std::time::{Duration, Instant}; const RESOURCE_SUMMARY_INTERVAL: Duration = Duration::from_secs(30); const FIRST_PACKET_QUEUE_DROP: u64 = 1 << 0; -const FIRST_UDP_QUEUE_DROP: u64 = 1 << 1; +const FIRST_UDP_ASSOCIATION_QUEUE_DROP: u64 = 1 << 1; const FIRST_DNS_QUEUE_DROP: u64 = 1 << 2; const FIRST_SINGLEFLIGHT_JOIN: u64 = 1 << 3; const FIRST_DNS_CACHE_HIT: u64 = 1 << 4; +const FIRST_UDP_RESPONSE_QUEUE_DROP: u64 = 1 << 5; /// Fixed-schema, runtime-owned resource telemetry. /// @@ -45,6 +46,8 @@ struct RuntimeResourceStatsInner { dns_cache_hits: AtomicU64, packet_queue_drops: AtomicU64, udp_queue_drops: AtomicU64, + udp_association_queue_drops: AtomicU64, + udp_response_queue_drops: AtomicU64, dns_queue_drops: AtomicU64, } @@ -66,7 +69,8 @@ pub(crate) enum ResourceActivity { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum ResourceQueue { Packet, - Udp, + UdpAssociation, + UdpResponse, Dns, } @@ -95,6 +99,8 @@ pub(crate) struct RuntimeResourceSnapshot { pub(crate) dns_cache_hits: u64, pub(crate) packet_queue_drops: u64, pub(crate) udp_queue_drops: u64, + pub(crate) udp_association_queue_drops: u64, + pub(crate) udp_response_queue_drops: u64, pub(crate) dns_queue_drops: u64, } @@ -117,6 +123,8 @@ impl RuntimeResourceStats { dns_cache_hits: AtomicU64::new(0), packet_queue_drops: AtomicU64::new(0), udp_queue_drops: AtomicU64::new(0), + udp_association_queue_drops: AtomicU64::new(0), + udp_response_queue_drops: AtomicU64::new(0), dns_queue_drops: AtomicU64::new(0), }), } @@ -177,9 +185,22 @@ impl RuntimeResourceStats { } pub(crate) fn queue_drop(&self, queue: ResourceQueue, limit: usize) { + if matches!( + queue, + ResourceQueue::UdpAssociation | ResourceQueue::UdpResponse + ) { + self.inner.udp_queue_drops.fetch_add(1, Ordering::Relaxed); + } let (counter, first) = match queue { ResourceQueue::Packet => (&self.inner.packet_queue_drops, FIRST_PACKET_QUEUE_DROP), - ResourceQueue::Udp => (&self.inner.udp_queue_drops, FIRST_UDP_QUEUE_DROP), + ResourceQueue::UdpAssociation => ( + &self.inner.udp_association_queue_drops, + FIRST_UDP_ASSOCIATION_QUEUE_DROP, + ), + ResourceQueue::UdpResponse => ( + &self.inner.udp_response_queue_drops, + FIRST_UDP_RESPONSE_QUEUE_DROP, + ), ResourceQueue::Dns => (&self.inner.dns_queue_drops, FIRST_DNS_QUEUE_DROP), }; counter.fetch_add(1, Ordering::Relaxed); @@ -191,7 +212,13 @@ impl RuntimeResourceStats { limit, "runtime queue dropped an item for the first time" ), - ResourceQueue::Udp => tracing::warn!( + ResourceQueue::UdpAssociation => tracing::warn!( + event = "tun_udp_association_drop", + scope = self.inner.scope, + limit, + "runtime queue dropped an item for the first time" + ), + ResourceQueue::UdpResponse => tracing::warn!( event = "tun_udp_response_drop", scope = self.inner.scope, limit, @@ -232,6 +259,11 @@ impl RuntimeResourceStats { dns_cache_hits: self.inner.dns_cache_hits.load(Ordering::Acquire), packet_queue_drops: self.inner.packet_queue_drops.load(Ordering::Acquire), udp_queue_drops: self.inner.udp_queue_drops.load(Ordering::Acquire), + udp_association_queue_drops: self + .inner + .udp_association_queue_drops + .load(Ordering::Acquire), + udp_response_queue_drops: self.inner.udp_response_queue_drops.load(Ordering::Acquire), dns_queue_drops: self.inner.dns_queue_drops.load(Ordering::Acquire), } } @@ -308,6 +340,8 @@ impl RuntimeResourceStatsInner { handshake_peak = self.handshakes.peak.load(Ordering::Acquire), packet_queue_drops = self.packet_queue_drops.load(Ordering::Acquire), udp_queue_drops = self.udp_queue_drops.load(Ordering::Acquire), + udp_association_queue_drops = self.udp_association_queue_drops.load(Ordering::Acquire), + udp_response_queue_drops = self.udp_response_queue_drops.load(Ordering::Acquire), dns_queue_drops = self.dns_queue_drops.load(Ordering::Acquire), "runtime resource statistics" ); @@ -348,7 +382,8 @@ mod tests { stats.singleflight_join(); stats.dns_cache_hit(); stats.queue_drop(ResourceQueue::Packet, 32); - stats.queue_drop(ResourceQueue::Udp, 16); + stats.queue_drop(ResourceQueue::UdpAssociation, 64); + stats.queue_drop(ResourceQueue::UdpResponse, 1024); stats.queue_drop(ResourceQueue::Dns, 16); let snapshot = stats.snapshot(); @@ -365,7 +400,9 @@ mod tests { assert_eq!(snapshot.singleflight_joins, 1); assert_eq!(snapshot.dns_cache_hits, 1); assert_eq!(snapshot.packet_queue_drops, 1); - assert_eq!(snapshot.udp_queue_drops, 1); + assert_eq!(snapshot.udp_queue_drops, 2); + assert_eq!(snapshot.udp_association_queue_drops, 1); + assert_eq!(snapshot.udp_response_queue_drops, 1); assert_eq!(snapshot.dns_queue_drops, 1); drop((dns, tcp, udp, handshake, singleflight)); diff --git a/src/resources/observation.rs b/src/resources/observation.rs index 19bdde5..9e49450 100644 --- a/src/resources/observation.rs +++ b/src/resources/observation.rs @@ -72,7 +72,6 @@ mod enabled { .or_else(|| SYNCHRONOUS.with(|slot| slot.borrow().clone())) } - #[cfg(feature = "ffi")] pub fn inherit_thread T, T>(operation: F) -> impl FnOnce() -> T { let probe = current(); move || match probe { @@ -202,7 +201,7 @@ mod enabled { } } -#[cfg(all(feature = "ffi", any(test, feature = "interop-test")))] +#[cfg(any(test, feature = "interop-test"))] pub(crate) use enabled::inherit_thread; #[cfg(any(test, feature = "interop-test"))] pub(crate) use enabled::{Guard, bind, observe_queue, track}; @@ -230,7 +229,7 @@ pub(crate) fn bind(future: F) -> F { future } -#[cfg(all(feature = "ffi", not(any(test, feature = "interop-test"))))] +#[cfg(not(any(test, feature = "interop-test")))] pub(crate) fn inherit_thread T, T>(operation: F) -> F { operation } diff --git a/src/routing/dispatcher.rs b/src/routing/dispatcher.rs index efd6326..55f5e0c 100644 --- a/src/routing/dispatcher.rs +++ b/src/routing/dispatcher.rs @@ -1,10 +1,11 @@ use std::{ collections::{HashMap, HashSet}, io, - net::IpAddr, + net::{IpAddr, SocketAddr}, pin::Pin, sync::Arc, task::{Context, Poll}, + time::Duration, }; use async_trait::async_trait; @@ -13,6 +14,7 @@ use tokio::{ io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt as _, DuplexStream, ReadBuf}, sync::Notify, task::JoinHandle, + time::Instant, }; use crate::{ @@ -30,6 +32,8 @@ use super::{ }; const TCP_DNS_DUPLEX_CAPACITY: usize = 8 * 1024; +const UDP_FLOW_IDLE_TIMEOUT: Duration = Duration::from_secs(30); +const UDP_FLOW_CLEANUP_INTERVAL: Duration = Duration::from_secs(10); #[async_trait] trait RoutingDns: Send + Sync { @@ -172,8 +176,8 @@ pub(crate) type RouteTargetDispatchers = ProxyDispatchers; /// /// The outer session observer records returned transport lifetimes without /// admitting or rejecting work. This layer creates at most one transport per -/// proxy id and one direct transport for each association and chooses between -/// them independently for every datagram. +/// route target and one direct transport for each source association. TUN IP +/// flows independently pin a rule action, without owning additional IO. pub struct RoutingDispatcher { route_targets: ProxyDispatchers, direct: Arc, @@ -269,11 +273,12 @@ impl RoutingDispatcher { let mut context = RoutingContext::with_domain_hint(network, destination, domain_hint) .map_err(|_| DispatchError::HostUnreachable)?; + // All GeoData rules in this decision see the same immutable view. + let snapshot = self.geo_matcher.routing_snapshot(); + let geo_matcher = snapshot.as_deref().unwrap_or(self.geo_matcher.as_ref()); + let mut resolved = None; - let evaluation = match self - .rules - .evaluate_with_geo(&context, self.geo_matcher.as_ref()) - { + let evaluation = match self.rules.evaluate_with_geo(&context, geo_matcher) { RuleEvaluation::NeedsIpResolution { rule_index } => { let addresses = match (destination, &self.dns) { (Destination::Domain { host, .. }, Some(dns)) => { @@ -285,7 +290,7 @@ impl RoutingDispatcher { resolved = Some(addresses); self.rules.evaluate_with_resolved_ips( &mut context, - self.geo_matcher.as_ref(), + geo_matcher, rule_index, resolved.as_deref().unwrap_or_default(), ) @@ -348,7 +353,7 @@ impl RoutingDispatcher { fn dns_tcp_stream(&self, dns: Arc) -> BoxStream { let (client, mut server) = tokio::io::duplex(TCP_DNS_DUPLEX_CAPACITY); - let relay = tokio::spawn(async move { + let relay = crate::resources::observation::spawn(async move { let mut query = vec![0_u8; MAX_MESSAGE_SIZE]; let mut length = [0_u8; 2]; loop { @@ -525,6 +530,7 @@ impl Dispatcher for RoutingDispatcher { session, route_transports: HashMap::new(), direct_transport: None, + flows: DatagramRoutes::new(), logged_route_actions: HashSet::new(), receive_wakeup: Arc::new(Notify::new()), })) @@ -549,6 +555,7 @@ struct RoutedDatagramTransport { session: DatagramSession, route_transports: HashMap, direct_transport: Option>, + flows: DatagramRoutes, logged_route_actions: HashSet, receive_wakeup: Arc, } @@ -558,6 +565,65 @@ struct CachedRouteTransport { direct: bool, } +/// Source, UDP protocol and inbound are fixed by the association owner; the +/// destination IP/port completes the five-tuple. Entries retain no DNS/GeoData. +struct DatagramRoute { + action: RuleAction, + last_used: Instant, + flow_id: Option>, +} + +struct DatagramRoutes { + entries: HashMap, + next_cleanup: Instant, +} + +impl DatagramRoutes { + fn new() -> Self { + Self { + entries: HashMap::new(), + next_cleanup: Instant::now() + UDP_FLOW_CLEANUP_INTERVAL, + } + } + + fn expire(&mut self, now: Instant) { + if now < self.next_cleanup { + return; + } + self.next_cleanup = now + UDP_FLOW_CLEANUP_INTERVAL; + self.entries + .retain(|_, route| now.duration_since(route.last_used) < UDP_FLOW_IDLE_TIMEOUT); + // Entry removal alone retains buckets. Reclaim burst capacity without + // resizing on the hot path or imposing a business-flow count ceiling. + if self.entries.capacity() > self.entries.len().saturating_mul(4).max(8) { + self.entries.shrink_to(self.entries.len().saturating_mul(2)); + } + } + + fn action(&mut self, destination: SocketAddr, now: Instant) -> Option { + self.expire(now); + let route = self.entries.get_mut(&destination)?; + if now.duration_since(route.last_used) >= UDP_FLOW_IDLE_TIMEOUT { + self.entries.remove(&destination); + return None; + } + route.last_used = now; + Some(route.action) + } + + fn observe_response(&mut self, destination: SocketAddr, now: Instant) { + self.expire(now); + if let Some(route) = self.entries.get_mut(&destination) { + // An already expired entry is not revived by a late response. + if now.duration_since(route.last_used) < UDP_FLOW_IDLE_TIMEOUT { + route.last_used = now; + } else { + self.entries.remove(&destination); + } + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] enum LoggedRouteAction { Route(RouteTargetId), @@ -641,6 +707,23 @@ impl RoutedDatagramTransport { async fn receive_inner(&mut self) -> Result { loop { + // Only send/close can change these caches, and they also require + // &mut self. The sole backend cannot change during this receive; + // cancellation releases the borrow before another route is opened. + if self.route_transports.is_empty() { + if let Some(direct) = self.direct_transport.as_mut() { + return direct.receive().await; + } + } else if self.route_transports.len() == 1 && self.direct_transport.is_none() { + return self + .route_transports + .values_mut() + .next() + .expect("the only route transport exists") + .transport + .receive() + .await; + } let mut receives = self .route_transports .values_mut() @@ -664,15 +747,43 @@ impl RoutedDatagramTransport { #[async_trait] impl DatagramTransport for RoutedDatagramTransport { async fn send(&mut self, mut datagram: Datagram) -> Result<(), DispatchError> { - let decision = self - .router - .route( - Network::Udp, - self.session.inbound, - &datagram.remote, - datagram.sniffed_domain.as_deref(), - ) - .await?; + let now = Instant::now(); + if self.session.inbound == InboundKind::Tun { + self.flows.expire(now); + } + let flow = match (self.session.inbound, &datagram.remote) { + (InboundKind::Tun, Destination::Ip(address)) => Some(*address), + _ => None, + }; + let action = flow.and_then(|destination| self.flows.action(destination, now)); + let decision = if let Some(action) = action { + RouteDecision { + action, + destination: datagram.remote.clone(), + resolved_addresses: None, + } + } else { + let decision = self + .router + .route( + Network::Udp, + self.session.inbound, + &datagram.remote, + datagram.sniffed_domain.as_deref(), + ) + .await?; + if let Some(destination) = flow { + self.flows.entries.insert( + destination, + DatagramRoute { + action: decision.action, + last_used: Instant::now(), + flow_id: None, + }, + ); + } + decision + }; if self.should_log_route_action(decision.action) { log_route_decision(Network::Udp, &decision); } @@ -689,8 +800,49 @@ impl DatagramTransport for RoutedDatagramTransport { } } + async fn send_with_flow_id( + &mut self, + datagram: Datagram, + flow_id: &[u8], + ) -> Result<(), DispatchError> { + if flow_id.is_empty() || flow_id.len() > 25 { + return Err(DispatchError::NotAllowed); + } + let flow = match (self.session.inbound, &datagram.remote) { + (InboundKind::Tun, Destination::Ip(address)) => Some(*address), + _ => None, + }; + if let Some(destination) = flow { + self.flows.expire(Instant::now()); + if self + .flows + .entries + .get(&destination) + .is_some_and(|route| route.flow_id.as_deref() != Some(flow_id)) + { + self.flows.entries.remove(&destination); + } + } + self.send(datagram).await?; + if let Some(destination) = flow + && let Some(route) = self.flows.entries.get_mut(&destination) + && route.flow_id.as_deref() != Some(flow_id) + { + route.flow_id = Some(flow_id.into()); + } + Ok(()) + } + async fn receive(&mut self) -> Result { - self.receive_inner().await + let datagram = self.receive_inner().await?; + if self.session.inbound == InboundKind::Tun { + let now = Instant::now(); + self.flows.expire(now); + if let Destination::Ip(destination) = datagram.remote { + self.flows.observe_response(destination, now); + } + } + Ok(datagram) } async fn close(&mut self) -> Result<(), DispatchError> { @@ -710,6 +862,7 @@ impl DatagramTransport for RoutedDatagramTransport { } self.route_transports.clear(); self.direct_transport = None; + self.flows.entries = HashMap::new(); if let Some(error) = first_error { Err(error) } else { @@ -735,6 +888,7 @@ mod tests { IpCidr, PortRange, ProxyGroupId, ProxyGroupMemberConfig, ProxyGroupMemberTarget, ProxyId, RuleKind, RuleSpec, SelectProxyGroupConfig, }, + geodata::{DynamicGeoData, GeoData}, routing::EmptyGeoMatcher, }; use bytes::Bytes; @@ -796,6 +950,27 @@ mod tests { hint_calls: AtomicUsize, } + #[derive(Default)] + struct UpdatingDns { + addresses: Mutex>, + hint: Mutex>, + } + + #[async_trait] + impl RoutingDns for UpdatingDns { + async fn resolve(&self, _host: &str) -> Result, DispatchError> { + Ok(self.addresses.lock().unwrap().clone()) + } + + async fn exchange(&self, _query: &[u8]) -> Result { + Err(DispatchError::HostUnreachable) + } + + async fn domain_hint(&self, _address: IpAddr) -> Option { + self.hint.lock().unwrap().clone() + } + } + impl MockDns { fn new(addresses: Vec) -> Self { Self { @@ -944,7 +1119,7 @@ mod tests { dns, ipv6, RuleSet::compile(rules).unwrap(), - Arc::new(EmptyGeoMatcher), + Arc::new(DynamicGeoData::empty()), ) } @@ -1064,7 +1239,7 @@ mod tests { direct, None, RuleSet::compile(vec![rule(RuleKind::Match, group_action(0))]).unwrap(), - Arc::new(EmptyGeoMatcher), + Arc::new(DynamicGeoData::empty()), ); let mut old_association = router .open_datagram(association(InboundKind::Http)) @@ -1696,6 +1871,148 @@ mod tests { assert_eq!(proxy.udp_opens.load(Ordering::Relaxed), 1); } + #[tokio::test] + async fn cancelled_udp_receive_can_add_another_route_and_receive_both() { + tokio::time::timeout(std::time::Duration::from_secs(1), async { + for direct_first in [true, false] { + let proxy = Arc::new(RecordingDispatcher::default()); + let direct = Arc::new(RecordingDispatcher::default()); + let router = dispatcher( + proxy.clone(), + direct.clone(), + None, + vec![ + rule( + RuleKind::IpCidr(IpCidr { + network: IpAddr::V4(Ipv4Addr::new(10, 0, 0, 0)), + prefix_len: 8, + }), + RuleAction::Direct, + ), + rule(RuleKind::Match, proxy_action(0)), + ], + ); + let mut transport = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + // No backend exists yet. Dropping this receive must leave + // the association available for its first send. + { + let mut receive = transport.receive(); + assert!(futures_util::poll!(receive.as_mut()).is_pending()); + } + let direct_address = "10.0.0.1:1234".parse().unwrap(); + let proxy_address = "192.0.2.1:1234".parse().unwrap(); + let (first_address, next_address) = if direct_first { + (direct_address, proxy_address) + } else { + (proxy_address, direct_address) + }; + let first = datagram(first_address, b"first"); + transport.send(first.clone()).await.unwrap(); + assert_eq!(transport.receive().await.unwrap(), first); + + // Poll the only cached backend until pending, then cancel + // before a send opens the other route. + { + let mut receive = transport.receive(); + assert!(futures_util::poll!(receive.as_mut()).is_pending()); + } + let next = datagram(next_address, b"next-route"); + let previous = datagram(first_address, b"previous-route"); + transport.send(next.clone()).await.unwrap(); + transport.send(previous.clone()).await.unwrap(); + let responses = [ + transport.receive().await.unwrap(), + transport.receive().await.unwrap(), + ]; + assert!(responses.contains(&next)); + assert!(responses.contains(&previous)); + assert_eq!(direct.udp_opens.load(Ordering::Relaxed), 1); + assert_eq!(proxy.udp_opens.load(Ordering::Relaxed), 1); + transport.close().await.unwrap(); + } + }) + .await + .expect("cancelled UDP receives did not preserve dynamic routing"); + } + + #[tokio::test] + async fn single_udp_route_propagates_receive_errors() { + struct FailingReceiveDispatcher; + struct FailingReceiveDatagrams { + fail_next: bool, + responses: VecDeque, + } + + #[async_trait] + impl Dispatcher for FailingReceiveDispatcher { + async fn connect_tcp( + &self, + _session: StreamSession, + ) -> Result { + unreachable!("UDP-only mock") + } + + async fn open_datagram( + &self, + _session: DatagramSession, + ) -> Result, DispatchError> { + Ok(Box::new(FailingReceiveDatagrams { + fail_next: true, + responses: VecDeque::new(), + })) + } + } + + #[async_trait] + impl DatagramTransport for FailingReceiveDatagrams { + async fn send(&mut self, datagram: Datagram) -> Result<(), DispatchError> { + self.responses.push_back(datagram); + Ok(()) + } + + async fn receive(&mut self) -> Result { + if std::mem::take(&mut self.fail_next) { + return Err(DispatchError::ConnectionRefused); + } + if let Some(response) = self.responses.pop_front() { + return Ok(response); + } + std::future::pending().await + } + } + + tokio::time::timeout(std::time::Duration::from_secs(1), async { + for action in [RuleAction::Direct, proxy_action(0)] { + let backend = Arc::new(FailingReceiveDispatcher); + let router = RoutingDispatcher::with_dns_service( + backend.clone(), + backend, + None, + true, + RuleSet::compile(vec![rule(RuleKind::Match, action)]).unwrap(), + Arc::new(EmptyGeoMatcher), + ); + let mut transport = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let request = datagram("192.0.2.1:1234".parse().unwrap(), b"request"); + transport.send(request.clone()).await.unwrap(); + assert!(matches!( + transport.receive().await, + Err(DispatchError::ConnectionRefused) + )); + assert_eq!(transport.receive().await.unwrap(), request); + transport.close().await.unwrap(); + } + }) + .await + .expect("the only UDP backend's receive error was not propagated"); + } + #[test] fn udp_associations_share_the_immutable_rule_set() { let router = dispatcher( @@ -1743,6 +2060,326 @@ mod tests { assert_eq!(proxy.datagrams.lock().unwrap().len(), 1); } + #[tokio::test] + async fn tun_udp_pins_each_destination_while_observing_new_flows() { + let proxy = Arc::new(RecordingDispatcher::default()); + let direct = Arc::new(RecordingDispatcher::default()); + let dns = Arc::new(UpdatingDns::default()); + let router = dispatcher( + proxy.clone(), + direct.clone(), + Some(dns.clone()), + vec![ + rule(RuleKind::Domain("blocked.test".into()), RuleAction::Reject), + rule(RuleKind::Domain("direct.test".into()), RuleAction::Direct), + rule(RuleKind::Match, proxy_action(0)), + ], + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let first = "198.51.100.7:443".parse().unwrap(); + let second = "198.51.100.7:8443".parse().unwrap(); + let third = "198.51.100.8:443".parse().unwrap(); + *dns.hint.lock().unwrap() = Some("direct.test".into()); + udp.send(datagram(first, b"initial")).await.unwrap(); + *dns.hint.lock().unwrap() = Some("blocked.test".into()); + udp.send(datagram(second, b"reject")).await.unwrap(); + udp.send(datagram(first, b"pinned")).await.unwrap(); + *dns.hint.lock().unwrap() = None; + udp.send(datagram(third, b"proxy")).await.unwrap(); + udp.send(datagram(second, b"still-reject")).await.unwrap(); + assert_eq!(direct.datagrams.lock().unwrap().len(), 2); + assert_eq!(proxy.datagrams.lock().unwrap().len(), 1); + assert_eq!(direct.udp_opens.load(Ordering::Relaxed), 1); + assert_eq!(proxy.udp_opens.load(Ordering::Relaxed), 1); + udp.close().await.unwrap(); + } + + #[tokio::test] + async fn tun_udp_uses_new_hints_only_for_a_new_authenticated_flow() { + let proxy = Arc::new(RecordingDispatcher::default()); + let direct = Arc::new(RecordingDispatcher::default()); + let dns = Arc::new(UpdatingDns::default()); + let router = dispatcher( + proxy.clone(), + direct.clone(), + Some(dns.clone()), + vec![ + rule(RuleKind::Domain("blocked.test".into()), RuleAction::Reject), + rule(RuleKind::Domain("direct.test".into()), RuleAction::Direct), + rule(RuleKind::Match, proxy_action(0)), + ], + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let destination = "198.51.100.7:443".parse().unwrap(); + for hint in [Some("direct.test"), Some("blocked.test"), None] { + *dns.hint.lock().unwrap() = hint.map(Into::into); + for _ in 0..2 { + udp.send(datagram(destination, b"current")).await.unwrap(); + } + } + assert_eq!(direct.datagrams.lock().unwrap().len(), 6); + assert!(proxy.datagrams.lock().unwrap().is_empty()); + + *dns.hint.lock().unwrap() = Some("blocked.test".into()); + for (id, hint) in [ + (b"first".as_slice(), "blocked.test"), + (b"second".as_slice(), "Direct.TEST."), + (b"third".as_slice(), "not a domain"), + ] { + let mut request = datagram(destination, b"sniffed"); + request.sniffed_domain = Some(Arc::from(hint)); + for _ in 0..2 { + udp.send_with_flow_id(request.clone(), id).await.unwrap(); + } + } + assert_eq!(direct.datagrams.lock().unwrap().len(), 8); + assert_eq!(proxy.datagrams.lock().unwrap().len(), 2); + // A retransmitted authenticated Initial keeps its established action, + // even if the tiny sniff table had to reconstruct its domain hint. + let mut replay = datagram(destination, b"replay"); + replay.sniffed_domain = Some(Arc::from("blocked.test")); + udp.send_with_flow_id(replay, b"third").await.unwrap(); + assert_eq!(proxy.datagrams.lock().unwrap().len(), 3); + for request in direct.datagrams.lock().unwrap().iter() { + assert_eq!(request.remote, Destination::Ip(destination)); + assert!(request.sniffed_domain.is_none()); + } + } + + #[tokio::test] + async fn udp_geo_activation_affects_new_flows_without_retaining_old_data() { + let dir = tempfile::tempdir().unwrap(); + // Independent Xray GeoIP fixture: cn -> 192.0.2.0/24. + std::fs::write( + dir.path().join("geoip.dat"), + [ + 0x0a, 14, 0x0a, 2, b'c', b'n', 0x12, 8, 0x0a, 4, 192, 0, 2, 0, 0x10, 24, + ], + ) + .unwrap(); + let rules = vec![ + rule(RuleKind::GeoIp("cn".into()), RuleAction::Reject), + rule(RuleKind::Match, proxy_action(0)), + ]; + let initial = Arc::new(GeoData::load(dir.path(), &rules).unwrap()); + let old_data = Arc::downgrade(&initial); + let geo = Arc::new(DynamicGeoData::new(initial)); + let proxy = Arc::new(RecordingDispatcher::default()); + let router = RoutingDispatcher::new( + ProxyDispatchers::new(vec![proxy.clone()]).unwrap(), + Arc::new(RecordingDispatcher::default()), + None, + RuleSet::compile(rules).unwrap(), + geo.clone(), + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let destination = "192.0.2.8:443".parse().unwrap(); + for _ in 0..2 { + udp.send(datagram(destination, b"blocked")).await.unwrap(); + } + assert!(proxy.datagrams.lock().unwrap().is_empty()); + geo.activate(Arc::new(GeoData::EMPTY)); + assert!( + old_data.upgrade().is_none(), + "active UDP must release obsolete GeoData" + ); + for _ in 0..2 { + udp.send(datagram(destination, b"still-blocked")) + .await + .unwrap(); + } + assert!(proxy.datagrams.lock().unwrap().is_empty()); + for _ in 0..2 { + udp.send(datagram("192.0.2.8:8443".parse().unwrap(), b"allowed")) + .await + .unwrap(); + } + assert_eq!(proxy.datagrams.lock().unwrap().len(), 2); + udp.send_with_flow_id(datagram(destination, b"new-connection"), b"new") + .await + .unwrap(); + assert_eq!(proxy.datagrams.lock().unwrap().len(), 3); + udp.close().await.unwrap(); + } + + #[tokio::test] + async fn udp_domain_destinations_observe_fresh_resolution() { + let dns = Arc::new(UpdatingDns::default()); + let direct = Arc::new(RecordingDispatcher::default()); + let router = dispatcher( + Arc::new(RecordingDispatcher::default()), + direct.clone(), + Some(dns.clone()), + vec![rule(RuleKind::Match, RuleAction::Direct)], + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + for ip in ["192.0.2.1", "192.0.2.2"] { + *dns.addresses.lock().unwrap() = vec![ip.parse().unwrap()]; + udp.send(Datagram { + remote: Destination::domain("example.test", 443).unwrap(), + payload: Bytes::from_static(b"fresh"), + sniffed_domain: None, + }) + .await + .unwrap(); + } + let sent = direct.datagrams.lock().unwrap(); + assert_eq!( + sent[0].remote, + Destination::Ip("192.0.2.1:443".parse().unwrap()) + ); + assert_eq!( + sent[1].remote, + Destination::Ip("192.0.2.2:443".parse().unwrap()) + ); + } + + #[tokio::test] + async fn tun_udp_mutable_matcher_changes_apply_to_new_flows() { + struct MutableGeo(AtomicBool); + + impl GeoMatcher for MutableGeo { + fn matches_geosite(&self, _code: &str, _domain: &str) -> bool { + false + } + fn matches_geoip(&self, _code: &str, _address: IpAddr) -> bool { + self.0.load(Ordering::Relaxed) + } + } + + let geo = Arc::new(MutableGeo(AtomicBool::new(false))); + let proxy = Arc::new(RecordingDispatcher::default()); + let router = RoutingDispatcher::new( + ProxyDispatchers::new(vec![proxy.clone()]).unwrap(), + Arc::new(RecordingDispatcher::default()), + None, + RuleSet::compile(vec![ + rule(RuleKind::GeoIp("cn".into()), RuleAction::Reject), + rule(RuleKind::Match, proxy_action(0)), + ]) + .unwrap(), + geo.clone(), + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let destination = "192.0.2.8:443".parse().unwrap(); + udp.send(datagram(destination, b"allowed")).await.unwrap(); + geo.0.store(true, Ordering::Relaxed); + udp.send(datagram(destination, b"pinned")).await.unwrap(); + udp.send(datagram("192.0.2.8:8443".parse().unwrap(), b"blocked")) + .await + .unwrap(); + assert_eq!(proxy.datagrams.lock().unwrap().len(), 2); + } + + #[tokio::test(start_paused = true)] + async fn udp_idle_sweep_reclaims_burst_capacity_without_a_count_limit() { + let start = Instant::now(); + let mut routes = DatagramRoutes::new(); + for port in 1..=16_384 { + routes.entries.insert( + SocketAddr::from(([192, 0, 2, 1], port)), + DatagramRoute { + action: RuleAction::Reject, + last_used: start, + flow_id: None, + }, + ); + } + let burst_capacity = routes.entries.capacity(); + assert!(burst_capacity >= 16_384); + let busy = SocketAddr::from(([192, 0, 2, 1], 1)); + assert_eq!( + routes.action(busy, start + Duration::from_secs(25)), + Some(RuleAction::Reject) + ); + routes.expire(start + Duration::from_secs(35)); + assert_eq!(routes.entries.len(), 1); + assert!(routes.entries.capacity() < burst_capacity / 4); + routes.expire(start + Duration::from_secs(60)); + assert!(routes.entries.is_empty()); + assert!(routes.entries.capacity() <= 8); + } + + #[tokio::test(start_paused = true)] + async fn tun_udp_expires_idle_destinations_while_the_source_stays_active() { + let proxy = Arc::new(RecordingDispatcher::default()); + let direct = Arc::new(RecordingDispatcher::default()); + let dns = Arc::new(UpdatingDns::default()); + let router = dispatcher( + proxy.clone(), + direct.clone(), + Some(dns.clone()), + vec![ + rule(RuleKind::Domain("blocked.test".into()), RuleAction::Reject), + rule(RuleKind::Domain("direct.test".into()), RuleAction::Direct), + rule(RuleKind::Match, proxy_action(0)), + ], + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let idle = "198.51.100.7:443".parse().unwrap(); + let busy = "198.51.100.7:8443".parse().unwrap(); + *dns.hint.lock().unwrap() = Some("direct.test".into()); + udp.send(datagram(idle, b"old")).await.unwrap(); + udp.send(datagram(busy, b"busy")).await.unwrap(); + *dns.hint.lock().unwrap() = Some("blocked.test".into()); + tokio::time::advance(Duration::from_secs(25)).await; + udp.send(datagram(busy, b"refresh")).await.unwrap(); + tokio::time::advance(Duration::from_secs(5)).await; + udp.send(datagram(idle, b"expired")).await.unwrap(); + udp.send(datagram(busy, b"still-pinned")).await.unwrap(); + assert_eq!(direct.datagrams.lock().unwrap().len(), 4); + assert!(proxy.datagrams.lock().unwrap().is_empty()); + udp.close().await.unwrap(); + } + + #[tokio::test(start_paused = true)] + async fn tun_udp_responses_refresh_only_their_own_flow() { + let direct = Arc::new(RecordingDispatcher::default()); + let dns = Arc::new(UpdatingDns::default()); + let router = dispatcher( + Arc::new(RecordingDispatcher::default()), + direct.clone(), + Some(dns.clone()), + vec![ + rule(RuleKind::Domain("blocked.test".into()), RuleAction::Reject), + rule(RuleKind::Match, RuleAction::Direct), + ], + ); + let mut udp = router + .open_datagram(association(InboundKind::Tun)) + .await + .unwrap(); + let reply = "198.51.100.7:443".parse().unwrap(); + let idle = "198.51.100.8:443".parse().unwrap(); + udp.send(datagram(reply, b"reply")).await.unwrap(); + udp.send(datagram(idle, b"idle")).await.unwrap(); + tokio::time::advance(Duration::from_secs(20)).await; + assert_eq!(udp.receive().await.unwrap().remote, Destination::Ip(reply)); + *dns.hint.lock().unwrap() = Some("blocked.test".into()); + tokio::time::advance(Duration::from_secs(20)).await; + udp.send(datagram(reply, b"still-pinned")).await.unwrap(); + udp.send(datagram(idle, b"expired")).await.unwrap(); + assert_eq!(direct.datagrams.lock().unwrap().len(), 3); + } + #[tokio::test] async fn tcp_dns_hijack_is_tun_only_while_udp_uses_normal_routing() { let proxy = Arc::new(RecordingDispatcher::default()); @@ -1878,6 +2515,76 @@ mod tests { .expect("dropping the caller stream did not abort the DNS relay"); } + #[cfg(feature = "ffi")] + #[test] + fn tun_dns_tcp_relay_preserves_probe_across_workers_and_releases_on_drop() { + use crate::resources::observation::{self, ResourceKind, ResourceProbe}; + + struct ProbeDns { + started: std::sync::mpsc::SyncSender, + } + + #[async_trait] + impl RoutingDns for ProbeDns { + async fn resolve(&self, _host: &str) -> Result, DispatchError> { + Ok(Vec::new()) + } + + async fn exchange(&self, _query: &[u8]) -> Result { + let _query = observation::track(ResourceKind::Waiter); + self.started.send(std::thread::current().id()).unwrap(); + std::future::pending().await + } + + async fn domain_hint(&self, _address: IpAddr) -> Option { + None + } + } + + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + .unwrap(); + let probe = ResourceProbe::default(); + let (started, ready) = std::sync::mpsc::sync_channel(1); + let dns = Arc::new(ProbeDns { started }); + let router = dispatcher( + Arc::new(RecordingDispatcher::default()), + Arc::new(RecordingDispatcher::default()), + Some(dns), + vec![rule(RuleKind::Match, proxy_action(0))], + ); + let caller = std::thread::current().id(); + let stream = runtime.block_on(probe.scope(async { + let mut stream = router + .connect_tcp(stream("1.1.1.1:53".parse().unwrap(), InboundKind::Tun)) + .await + .unwrap(); + stream.write_u16(5).await.unwrap(); + stream.write_all(b"query").await.unwrap(); + stream + })); + let relay = ready + .recv_timeout(std::time::Duration::from_secs(1)) + .expect("DNS relay did not enter a worker thread"); + assert_ne!(caller, relay); + + assert_eq!(probe.snapshot().current(ResourceKind::Task), 1); + assert_eq!(probe.snapshot().current(ResourceKind::Waiter), 1); + assert_eq!(probe.snapshot().peak(ResourceKind::Waiter), 1); + drop(stream); + runtime.block_on(async { + tokio::time::timeout(std::time::Duration::from_secs(1), async { + while !probe.snapshot().is_idle() { + tokio::task::yield_now().await; + } + }) + .await + .expect("dropped DNS relay did not release its observed resources"); + }); + } + #[tokio::test] async fn lazy_resolution_preserves_rule_priority_and_pins_the_match() { let proxy = Arc::new(RecordingDispatcher::default()); diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 8c9841e..f52e15f 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -5,7 +5,7 @@ //! match values are normalized once and the resulting ordered rule set can be //! evaluated without allocating for each rule. -use std::{fmt, net::IpAddr}; +use std::{fmt, net::IpAddr, sync::Arc}; use crate::{ config::{IpCidr, Network, RuleAction, RuleKind, RuleSpec}, @@ -188,6 +188,14 @@ fn valid_opaque_ascii_dns_name(value: &str) -> bool { /// matches, allowing the base rule engine to ship before the bounded GeoData /// loader without treating absent data as an error or wildcard. pub trait GeoMatcher: Send + Sync { + /// Captures one immutable view for a whole routing decision. Changed + /// contents require a new view, never interior mutation of the snapshot. + /// Unknown or mutable providers may return None. Established TUN IP UDP + /// flows pin their action independently of later matcher updates. + fn routing_snapshot(&self) -> Option> { + None + } + /// Whether a prepared GeoSite category is currently available. /// /// The default preserves existing complete matchers. Dynamic or empty @@ -375,12 +383,21 @@ impl RuleSet { value.make_ascii_lowercase(); text_bytes = checked_text_bytes(text_bytes, value.len())?; } - RuleKind::GeoSite(code) | RuleKind::GeoIp(code) => { - if !valid_geo_code(code) { - return Err(RuleSetError::InvalidGeoCode { rule_index: index }); - } - code.make_ascii_lowercase(); - text_bytes = checked_text_bytes(text_bytes, code.len())?; + RuleKind::GeoSite(selector) => { + *selector = crate::geodata::normalize_selector( + crate::geodata::GeoDataKind::GeoSite, + selector, + ) + .map_err(|_| RuleSetError::InvalidGeoCode { rule_index: index })?; + text_bytes = checked_text_bytes(text_bytes, selector.len())?; + } + RuleKind::GeoIp(selector) => { + *selector = crate::geodata::normalize_selector( + crate::geodata::GeoDataKind::GeoIp, + selector, + ) + .map_err(|_| RuleSetError::InvalidGeoCode { rule_index: index })?; + text_bytes = checked_text_bytes(text_bytes, selector.len())?; } RuleKind::IpCidr(cidr) => { if !valid_cidr(cidr) { @@ -446,8 +463,8 @@ impl RuleSet { self.evaluate_with_geo(context, &EmptyGeoMatcher) } - /// Evaluates from top to bottom and stops at the first match. The hot path - /// performs no heap allocation. + /// Evaluates from top to bottom and stops at the first match. Rule traversal + /// does not allocate; GeoSite regexes may allocate internal search caches. #[must_use] pub fn evaluate_with_geo( &self, @@ -610,15 +627,6 @@ fn checked_text_bytes(current: usize, additional: usize) -> Result bool { - let bytes = code.as_bytes(); - (1..=64).contains(&bytes.len()) - && bytes[0].is_ascii_alphanumeric() - && bytes.iter().skip(1).all(|byte| { - byte.is_ascii_alphanumeric() || matches!(*byte, b'.' | b'_' | b'+' | b'!' | b'-') - }) -} - fn valid_cidr(cidr: &IpCidr) -> bool { match cidr.network { IpAddr::V4(_) => cidr.prefix_len <= 32, @@ -691,6 +699,31 @@ mod tests { } } + #[test] + fn geodata_selectors_are_canonicalized_at_the_routing_boundary() { + let rules = RuleSet::compile(vec![ + spec( + RuleKind::GeoSite("!GOOGLE@CN@ADS@cn".to_owned()), + RuleAction::Direct, + ), + spec(RuleKind::GeoIp("!CN".to_owned()), RuleAction::Reject), + spec(RuleKind::Match, proxy_action(0)), + ]) + .unwrap(); + assert_eq!( + rules.rules[0].kind, + RuleKind::GeoSite("!google@ads@cn".to_owned()) + ); + assert_eq!(rules.rules[1].kind, RuleKind::GeoIp("!cn".to_owned())); + assert!( + RuleSet::compile(vec![ + spec(RuleKind::GeoIp("cn@ads".to_owned()), RuleAction::Direct), + spec(RuleKind::Match, proxy_action(0)), + ]) + .is_err() + ); + } + #[test] fn first_matching_rule_wins() { let rules = RuleSet::compile(vec![ diff --git a/src/runtime.rs b/src/runtime.rs index da1f53b..6370089 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -149,6 +149,7 @@ struct BuiltRuntimeParts { } struct BuiltProxyGraph { + dialer: Dialer, resolution: ResolutionContext, nodes: Vec>, lifecycle_order: Vec, @@ -175,6 +176,7 @@ impl BuiltProxyGraph { } fn begin_shutdown(&self) { + self.dialer.begin_shutdown(); self.resolution.close(); for target in self.lifecycle_order.iter().rev() { if let BuiltRouteTarget::Proxy(connector) = target { @@ -189,6 +191,7 @@ impl BuiltProxyGraph { connector.shutdown().await; } } + self.dialer.shutdown().await; } } @@ -311,7 +314,9 @@ impl PreparedCore { } fn build_dispatcher(&self, dialer: Dialer) -> io::Result { - let dialer = dialer.with_ipv6(self.config.ipv6); + let dialer = dialer + .with_ipv6(self.config.ipv6) + .with_fresh_initialization(); let handshake_stats = RuntimeResourceStats::new("runtime_handshake_observation"); let proxy_graph = self.build_proxy_graph(dialer.clone())?; let proxy_dispatchers = self.wrap_proxy_dispatchers( @@ -551,6 +556,7 @@ impl PreparedMeasurement { dialer: Dialer, resolver: Arc, ) -> io::Result { + let dialer = dialer.with_fresh_initialization(); let proxy_graph = build_proxy_graph( &self.config.proxies, &[], @@ -632,6 +638,7 @@ fn build_proxy_graph( // failure they drop first, then this guard releases the DAG in reverse // dependency order without recursive destruction of configuration-sized chains. let mut graph = BuiltProxyGraph { + dialer: dialer.clone(), resolution, nodes: Vec::new(), lifecycle_order: Vec::with_capacity(order.len()), @@ -2388,7 +2395,7 @@ rules: fn test_tun() -> (TunIo, UnixDatagram) { let (host, peer) = UnixDatagram::pair().unwrap(); host.set_nonblocking(true).unwrap(); - let fd = crate::platform::TunFd::duplicate(host.as_raw_fd()).unwrap(); + let fd = crate::platform::TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); (TunIo::new(fd, crate::TunFraming::RawIp).unwrap(), peer) } } diff --git a/src/traffic.rs b/src/traffic.rs index e9b901e..04ae9b6 100644 --- a/src/traffic.rs +++ b/src/traffic.rs @@ -94,7 +94,7 @@ impl TunTrafficStats { } fn saturating_add(counter: &AtomicU64, value: u64) { - let _ = counter.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| { + let _ = counter.try_update(Ordering::Relaxed, Ordering::Relaxed, |current| { Some(current.saturating_add(value).min(MAX_PUBLIC_TRAFFIC_BYTES)) }); } @@ -149,4 +149,31 @@ mod tests { stats.record_up(8); assert_eq!(stats.snapshot().up_total, MAX_PUBLIC_TRAFFIC_BYTES); } + + #[test] + fn batch_byte_totals_preserve_rotation_and_saturation() { + let individual = TunTrafficStats::default(); + let batched = TunTrafficStats::default(); + for stats in [&individual, &batched] { + stats + .down_pending + .store(MAX_PUBLIC_TRAFFIC_BYTES - 1, Ordering::Relaxed); + stats + .down_total + .store(MAX_PUBLIC_TRAFFIC_BYTES - 1, Ordering::Relaxed); + } + let sizes = [20, 40, 1_500, 0, 28, 1_200, 36, 1_400]; + for bytes in sizes { + individual.record_up(bytes); + individual.record_down(bytes); + } + batched.record_up(sizes.iter().sum()); + batched.record_down(sizes.iter().sum()); + assert_eq!(individual.snapshot(), batched.snapshot()); + individual.rotate_rate_bucket(); + batched.rotate_rate_bucket(); + assert_eq!(individual.snapshot(), batched.snapshot()); + assert_eq!(batched.snapshot().down, MAX_PUBLIC_TRAFFIC_BYTES); + assert_eq!(batched.snapshot().down_total, MAX_PUBLIC_TRAFFIC_BYTES); + } } diff --git a/src/transport/grpc_pool.rs b/src/transport/grpc_pool.rs index c824439..6b8de59 100644 --- a/src/transport/grpc_pool.rs +++ b/src/transport/grpc_pool.rs @@ -186,7 +186,7 @@ struct Connection { impl Connection { fn reserve(&self, pool: &Arc>>) -> io::Result<(Sender, Lease)> { self.active - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |count| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |count| { count.checked_add(1) }) .map_err(|_| io::Error::from(io::ErrorKind::OutOfMemory))?; diff --git a/src/tun_runtime.rs b/src/tun_runtime.rs index 622e820..34368bf 100644 --- a/src/tun_runtime.rs +++ b/src/tun_runtime.rs @@ -20,7 +20,8 @@ use tokio::{ use tokio_util::sync::CancellationToken; use vcore_netstack::{ NetStack, NetStackConfig, NetStackError, NetStackStats, Packet, PacketSink, PacketStream, - TcpListener, TcpStream, UdpDatagram, UdpSocket, + TcpListener, TcpStream, UdpDatagram, UdpPacketView, encode_udp_packet_into, + parse_udp_packet_view, }; use crate::{ @@ -31,7 +32,7 @@ use crate::{ classify_query, runtime::{DnsQueryPermit, RuntimeDns}, }, - platform::TunIo, + platform::{TUN_PACKET_BATCH_SIZE, TunIo}, quic_sniffer::{ QuicConnectionKey, QuicSniffOutcome, QuicSniffer, quic_connection_key, quic_has_unsupported_version, @@ -49,7 +50,6 @@ use crate::dns::{ClassifiedDnsQuery, synthesize_servfail_response}; const TUN_MTU: usize = 1_500; const TCP_RELAY_BUFFER: usize = 4 * 1024; -const UDP_ASSOCIATION_QUEUE_MAX: usize = 16; const QUIC_SNIFF_FLOW_MAX: usize = 4; const QUIC_SNIFF_PENDING_DATAGRAM_MAX: usize = 8; const QUIC_SNIFF_PENDING_BYTES_MAX: usize = 32 * 1024; @@ -70,24 +70,11 @@ fn effective_tun_mtu(limits: ResourceLimits) -> usize { TUN_MTU.min(limits.tun_max_datagram_size) } -fn tun_udp_ingress_queue_capacity(limits: ResourceLimits, dns_enabled: bool) -> usize { - if dns_enabled { - limits.tun_dns_ingress_queue_capacity - } else { - limits.event_queue_capacity - } -} - -fn tun_netstack_config( - limits: ResourceLimits, - dns_enabled: bool, - fake_icmp_echo: bool, -) -> NetStackConfig { +fn tun_netstack_config(limits: ResourceLimits, fake_icmp_echo: bool) -> NetStackConfig { NetStackConfig { mtu: effective_tun_mtu(limits), packet_queue: limits.packet_queue_capacity, tcp_accept_queue: limits.event_queue_capacity, - udp_queue: tun_udp_ingress_queue_capacity(limits, dns_enabled), tcp_buffer_per_direction: limits.tcp_buffer_per_direction, fake_icmp_echo, ..NetStackConfig::default() @@ -161,25 +148,34 @@ impl TunRuntime { } pub(crate) async fn run(self, cancellation: CancellationToken) -> io::Result<()> { - let config = tun_netstack_config(self.limits, self.dns.is_some(), self.fake_icmp_echo); + let config = tun_netstack_config(self.limits, self.fake_icmp_echo); let resource_stats = RuntimeResourceStats::new("tun_runtime"); tracing::info!( mtu = config.mtu, packet_queue = self.limits.packet_queue_capacity, event_queue = self.limits.event_queue_capacity, - udp_ingress_queue = config.udp_queue, + tun_udp_association_queue_capacity = self.limits.tun_udp_association_queue_capacity, + tun_udp_response_queue_capacity = self.limits.tun_udp_response_queue_capacity, dns_hijack = self.dns.is_some(), ipv6 = self.ipv6, fake_icmp_echo = self.fake_icmp_echo, domain_sniffing = self.sniffer.is_some(), "TUN runtime starting" ); - let parts = NetStack::start(config) - .map_err(netstack_to_io)? - .into_parts(); + let parts = NetStack::start_tcp(config).map_err(netstack_to_io)?; let control = parts.control.clone(); let netstack_stats = parts.stats.clone(); let mut tasks = JoinSet::new(); + let (udp, responses, dns_responses) = UdpIngress::new( + UdpIngressContext { + dispatcher: self.dispatcher.clone(), + dns: self.dns, + sniffer: self.sniffer.clone(), + limits: self.limits, + resource_stats: resource_stats.clone(), + }, + cancellation.clone(), + ); tasks.spawn(observation::task(netstack_stats_loop( netstack_stats.clone(), @@ -194,14 +190,16 @@ impl TunRuntime { self.tun.clone(), parts.packet_sink, self.ipv6, - self.limits.packet_queue_capacity, - resource_stats.clone(), + udp, self.traffic_stats.clone(), cancellation.clone(), ))); tasks.spawn(observation::task(tun_write_loop( self.tun, parts.packet_stream, + responses, + dns_responses, + effective_tun_mtu(self.limits), self.traffic_stats, cancellation.clone(), ))); @@ -213,15 +211,6 @@ impl TunRuntime { resource_stats.clone(), cancellation.clone(), ))); - tasks.spawn(observation::task(udp_loop( - parts.udp_socket, - self.dispatcher, - self.dns, - sniffer, - self.limits, - resource_stats.clone(), - cancellation.clone(), - ))); let mut first_error = tokio::select! { biased; @@ -311,46 +300,127 @@ async fn tun_read_loop( tun: Arc, packet_sink: PacketSink, ipv6: bool, - packet_queue_limit: usize, - resource_stats: RuntimeResourceStats, + mut udp: UdpIngress, traffic_stats: Arc, cancellation: CancellationToken, ) -> io::Result<()> { - let mut packet = Vec::with_capacity(TUN_MTU); + let result = tun_read_inner( + tun, + packet_sink, + ipv6, + &mut udp, + traffic_stats, + cancellation, + ) + .await; + // The reader owns all associations and DNS queries. EOF, error and + // cancellation all join them without cancelling the caller's token. + udp.stop().await; + result +} + +async fn tun_read_inner( + tun: Arc, + packet_sink: PacketSink, + ipv6: bool, + udp: &mut UdpIngress, + traffic_stats: Arc, + cancellation: CancellationToken, +) -> io::Result<()> { + let mtu = effective_tun_mtu(udp.context.limits); + let mut packets: [Vec; TUN_PACKET_BATCH_SIZE] = + std::array::from_fn(|_| Vec::with_capacity(TUN_MTU)); + let mut outcomes = Vec::with_capacity(TUN_PACKET_BATCH_SIZE); + let mut cleanup = interval_at( + TokioInstant::now() + UDP_CLEANUP_INTERVAL, + UDP_CLEANUP_INTERVAL, + ); + cleanup.set_missed_tick_behavior(MissedTickBehavior::Skip); let mut first_read_logged = false; let mut first_ingress_logged = false; loop { - tokio::select! { + let result = tokio::select! { biased; - () = cancellation.cancelled() => return Ok(()), - result = tun.read_packet(&mut packet) => { - match result { - Ok(_) => {} - Err(VCoreError::InvalidPacket(reason)) => { - tracing::debug!(%reason, "dropping invalid packet read from TUN"); - continue; - } - Err(error) => return Err(vcore_to_io(error)), - } + () = cancellation.cancelled() => None, + _ = cleanup.tick() => { + udp.cleanup(); + continue; } + joined = udp.tasks.join_next(), if !udp.tasks.is_empty() => { + udp.complete_association(joined); + continue; + } + joined = udp.dns_tasks.join_next(), if !udp.dns_tasks.is_empty() => { + if let Some(Err(error)) = joined { + tracing::warn!( + cancelled = error.is_cancelled(), + panicked = error.is_panic(), + "TUN DNS query task failed" + ); + } + continue; + } + result = tun.read_packets(&mut packets, &mut outcomes) => Some(result), + }; + // Progress is outside the cancellable future: account for every valid + // consumed packet, even an EOF/cancelled batch's completed prefix. + let up_bytes = packets + .iter() + .zip(&outcomes) + .fold(0usize, |bytes, (packet, outcome)| { + if outcome.is_ok() { + bytes.saturating_add(packet.len()) + } else { + bytes + } + }); + if up_bytes != 0 { + traffic_stats.record_up(up_bytes); } - traffic_stats.record_up(packet.len()); - if !first_read_logged { - tracing::info!( - packet_bytes = packet.len(), - ip_version = packet_ip_version(&packet), - "TUN received first packet" - ); - first_read_logged = true; - } - if !tun_ingress_allowed(ipv6, &packet) { - continue; - } - let raw = Packet::new(Bytes::copy_from_slice(&packet)); - tokio::select! { - biased; - () = cancellation.cancelled() => return Ok(()), - result = packet_sink.send(raw) => match result { + let Some(result) = result else { + return Ok(()); + }; + let processed = outcomes.len(); + for (packet, outcome) in packets.iter().zip(outcomes.drain(..)) { + match outcome { + Ok(_) => {} + Err(VCoreError::InvalidPacket(reason)) => { + tracing::debug!(%reason, "dropping invalid packet read from TUN"); + continue; + } + Err(error) => return Err(vcore_to_io(error)), + } + if cancellation.is_cancelled() { + return Ok(()); + } + if !first_read_logged { + tracing::info!( + packet_bytes = packet.len(), + ip_version = packet_ip_version(packet), + "TUN received first packet" + ); + first_read_logged = true; + } + if !tun_ingress_allowed(ipv6, packet) { + continue; + } + // UDP bypasses PacketSink, so retain the same effective MTU check + // before creating an association or admitting a DNS query. + if packet.len() > mtu { + tracing::debug!( + packet_bytes = packet.len(), + mtu, + "dropping TUN packet exceeding effective MTU" + ); + continue; + } + if let Some(datagram) = parse_udp_packet_view(packet) { + udp.offer(datagram); + continue; + } + // A full TCP/ICMP handoff drops this complete IP packet. Awaiting + // capacity here would also stall unrelated UDP and DNS ingress. + match packet_sink.try_send(Packet::new(Bytes::copy_from_slice(packet))) { Ok(()) => { if !first_ingress_logged { tracing::info!("netstack accepted first TUN packet"); @@ -358,48 +428,189 @@ async fn tun_read_loop( } } Err(NetStackError::Stopped) => return Ok(()), - Err(error @ (NetStackError::EmptyPacket + Err( + error @ (NetStackError::EmptyPacket | NetStackError::InvalidIpVersion | NetStackError::MtuExceeded { .. } - | NetStackError::Backpressure)) => { + | NetStackError::Backpressure), + ) => { if matches!(error, NetStackError::Backpressure) { - resource_stats.queue_drop(ResourceQueue::Packet, packet_queue_limit); + udp.context.resource_stats.queue_drop( + ResourceQueue::Packet, + udp.context.limits.packet_queue_capacity, + ); } - tracing::debug!(error_code = ?error, "dropping packet rejected by netstack ingress"); + tracing::debug!( + error_code = ?error, "dropping packet rejected by netstack ingress" + ); } Err(error) => return Err(netstack_to_io(error)), - }, + } + } + result.map_err(vcore_to_io)?; + // try_send / borrowed UDP classification have no channel await. Charge + // actual packets, including locally dropped ones, after each <=8 batch. + for _ in 0..processed { + tokio::task::consume_budget().await; } } } +enum TunOutput { + Raw(Packet), + Udp(QueuedUdpResponse), +} + +/// One bounded scheduling owner, not another packet queue. The cursor persists +/// across batches and closed lanes never prevent the remaining lanes draining. +struct TunOutputMux { + raw: PacketStream, + ordinary: mpsc::Receiver, + dns: mpsc::Receiver, + open: [bool; 3], + next_lane: usize, +} + +impl TunOutputMux { + fn new( + raw: PacketStream, + ordinary: mpsc::Receiver, + dns: mpsc::Receiver, + ) -> Self { + Self { + raw, + ordinary, + dns, + open: [true; 3], + next_lane: 0, + } + } + + fn try_next(&mut self) -> Option { + for offset in 0..3 { + let lane = (self.next_lane + offset) % 3; + if !self.open[lane] { + continue; + } + let result = match lane { + 0 => self.raw.try_recv().map(TunOutput::Raw), + 1 => self.ordinary.try_recv().map(TunOutput::Udp), + _ => self.dns.try_recv().map(TunOutput::Udp), + }; + match result { + Ok(output) => { + self.next_lane = (lane + 1) % 3; + return Some(output); + } + Err(mpsc::error::TryRecvError::Disconnected) => self.open[lane] = false, + Err(mpsc::error::TryRecvError::Empty) => {} + } + } + None + } + + async fn recv(&mut self) -> Option { + loop { + if let Some(output) = self.try_next() { + return Some(output); + } + if !self.open.iter().any(|open| *open) { + return None; + } + let (lane, output) = tokio::select! { + packet = self.raw.recv(), if self.open[0] => (0, packet.map(TunOutput::Raw)), + response = self.ordinary.recv(), if self.open[1] => (1, response.map(TunOutput::Udp)), + response = self.dns.recv(), if self.open[2] => (2, response.map(TunOutput::Udp)), + }; + if let Some(output) = output { + self.next_lane = (lane + 1) % 3; + return Some(output); + } + self.open[lane] = false; + } + } +} + +#[allow(clippy::too_many_arguments)] async fn tun_write_loop( tun: Arc, - mut packet_stream: PacketStream, + packet_stream: PacketStream, + responses: mpsc::Receiver, + dns_responses: mpsc::Receiver, + mtu: usize, traffic_stats: Arc, cancellation: CancellationToken, ) -> io::Result<()> { + let mut mux = TunOutputMux::new(packet_stream, responses, dns_responses); let mut first_write_logged = false; + let mut frames: [Vec; TUN_PACKET_BATCH_SIZE] = + std::array::from_fn(|_| Vec::with_capacity(mtu)); + let mut raw: [Option; TUN_PACKET_BATCH_SIZE] = std::array::from_fn(|_| None); + let mut permits: [Option; TUN_PACKET_BATCH_SIZE] = + std::array::from_fn(|_| None); + let mut outcomes = Vec::with_capacity(TUN_PACKET_BATCH_SIZE); loop { - let packet = tokio::select! { + let first = tokio::select! { biased; () = cancellation.cancelled() => return Ok(()), - packet = packet_stream.recv() => packet, + output = mux.recv() => output, }; - let Some(packet) = packet else { + let Some(first) = first else { return Ok(()); }; - tokio::select! { - biased; - () = cancellation.cancelled() => return Ok(()), - result = tun.write_packet(packet.data()) => { - match result { + let mut first = Some(first); + let mut count = 0; + let mut processed = 0; + // Invalid UDP output also consumes work budget. A malformed producer + // cannot keep us in an unlimited ready-drain loop. + for _ in 0..TUN_PACKET_BATCH_SIZE { + if cancellation.is_cancelled() { + return Ok(()); + } + let Some(output) = first.take().or_else(|| mux.try_next()) else { + break; + }; + processed += 1; + match output { + TunOutput::Raw(packet) => raw[count] = Some(packet), + TunOutput::Udp(QueuedUdpResponse { + datagram, + dns_permit, + }) => { + if let Err(error) = encode_udp_packet_into(&datagram, mtu, &mut frames[count]) { + tracing::debug!( + error_code = ?error, "dropping UDP response that cannot be emitted to TUN" + ); + continue; + } + permits[count] = dns_permit; + } + } + count += 1; + } + if count != 0 { + let slices: [&[u8]; TUN_PACKET_BATCH_SIZE] = std::array::from_fn(|index| { + raw[index] + .as_ref() + .map_or(frames[index].as_slice(), Packet::data) + }); + let result = tokio::select! { + biased; + () = cancellation.cancelled() => None, + result = tun.write_packets(&slices[..count], &mut outcomes) => Some(result), + }; + // Platform completion is the ownership boundary for each DNS + // permit. Unaccepted suffixes stay owned until error/cancel/drop. + let mut down_bytes = 0usize; + for (index, outcome) in outcomes.drain(..).enumerate() { + permits[index].take(); + match outcome { Ok(_) => { - traffic_stats.record_down(packet.data().len()); + down_bytes = down_bytes.saturating_add(slices[index].len()); if !first_write_logged { tracing::info!( - packet_bytes = packet.data().len(), - ip_version = packet_ip_version(packet.data()), + packet_bytes = slices[index].len(), + ip_version = packet_ip_version(slices[index]), "TUN emitted first packet" ); first_write_logged = true; @@ -408,9 +619,26 @@ async fn tun_write_loop( Err(VCoreError::InvalidPacket(reason)) => { tracing::debug!(%reason, "dropping invalid packet emitted by netstack"); } - Err(error) => return Err(vcore_to_io(error)), + Err(error) => { + traffic_stats.record_down(down_bytes); + return Err(vcore_to_io(error)); + } } } + if down_bytes != 0 { + traffic_stats.record_down(down_bytes); + } + let Some(result) = result else { + return Ok(()); + }; + result.map_err(vcore_to_io)?; + } + for index in 0..count { + raw[index] = None; + permits[index] = None; + } + for _ in 0..processed { + tokio::task::consume_budget().await; } } } @@ -736,203 +964,214 @@ enum AssociationInputResult { fn try_queue_association_input( association: &UdpAssociation, - datagram: UdpDatagram, + datagram: UdpPacketView<'_>, now: u64, association_queue: usize, resource_stats: &RuntimeResourceStats, ) -> AssociationInputResult { - match association.sender.try_send(datagram) { - Ok(()) => { + match association.sender.try_reserve() { + Ok(permit) => { + permit.send(UdpDatagram::new( + datagram.source, + datagram.destination, + Bytes::copy_from_slice(datagram.payload), + )); association.touch(now); AssociationInputResult::Queued } - Err(mpsc::error::TrySendError::Full(_)) => { - resource_stats.queue_drop(ResourceQueue::Udp, association_queue); + Err(mpsc::error::TrySendError::Full(())) => { + resource_stats.queue_drop(ResourceQueue::UdpAssociation, association_queue); AssociationInputResult::Full } - Err(mpsc::error::TrySendError::Closed(_)) => AssociationInputResult::Closed, + Err(mpsc::error::TrySendError::Closed(())) => AssociationInputResult::Closed, } } -async fn udp_loop( - mut socket: UdpSocket, +struct UdpIngressContext { dispatcher: Arc, dns: Option>, sniffer: Option>, limits: ResourceLimits, resource_stats: RuntimeResourceStats, - cancellation: CancellationToken, -) -> io::Result<()> { - let tun_mtu = effective_tun_mtu(limits); - let association_queue = limits - .event_queue_capacity - .clamp(1, UDP_ASSOCIATION_QUEUE_MAX); - let (responses_tx, mut responses_rx) = mpsc::channel(limits.event_queue_capacity); - let (dns_responses_tx, mut dns_responses_rx) = - mpsc::channel(limits.tun_dns_response_queue_capacity); - let mut associations: HashMap = HashMap::new(); - let mut tasks: JoinSet<(SocketAddr, u64, io::Result<()>)> = JoinSet::new(); - let mut dns_tasks: JoinSet<()> = JoinSet::new(); - let association_clock = AssociationClock::realtime(); - let mut cleanup = interval_at( - TokioInstant::now() + UDP_CLEANUP_INTERVAL, - UDP_CLEANUP_INTERVAL, - ); +} - loop { - tokio::select! { - () = cancellation.cancelled() => break, - _ = cleanup.tick() => { - let removed = - take_expired_or_closed_associations(&mut associations, association_clock.now()); - cancel_removed_associations(removed); +type UdpAssociationCompletion = (SocketAddr, u64, io::Result<()>); + +/// Owned by the TUN reader; there is no all-source UDP handoff. +struct UdpIngress { + context: UdpIngressContext, + associations: HashMap, + tasks: JoinSet, + dns_tasks: JoinSet<()>, + responses: mpsc::Sender, + dns_responses: mpsc::Sender, + cancellation: CancellationToken, + association_clock: AssociationClock, +} + +impl UdpIngress { + fn new( + context: UdpIngressContext, + cancellation: CancellationToken, + ) -> ( + Self, + mpsc::Receiver, + mpsc::Receiver, + ) { + let (responses, response_rx) = + mpsc::channel(context.limits.tun_udp_response_queue_capacity); + let (dns_responses, dns_rx) = mpsc::channel(context.limits.tun_dns_response_queue_capacity); + ( + Self { + context, + associations: HashMap::new(), + tasks: JoinSet::new(), + dns_tasks: JoinSet::new(), + responses, + dns_responses, + cancellation: cancellation.child_token(), + association_clock: AssociationClock::realtime(), + }, + response_rx, + dns_rx, + ) + } + + fn offer(&mut self, datagram: UdpPacketView<'_>) { + if self.cancellation.is_cancelled() { + return; + } + if datagram.destination.port() == 53 + && let Some(dns) = &self.context.dns + { + if let Err(error) = classify_query(datagram.payload) { + tracing::debug!(%error, "dropping invalid TUN DNS datagram"); + return; } - joined = tasks.join_next(), if !tasks.is_empty() => { - match joined { - Some(Ok((source, association_id, result))) => { - remove_completed_association(&mut associations, source, association_id); - match result { - Ok(()) => tracing::debug!(association_id, "TUN UDP association closed"), - Err(error) => tracing::warn!( - association_id, - error_kind = ?error.kind(), - "TUN UDP association failed" - ), - } - } - Some(Err(error)) => { - tracing::warn!( - cancelled = error.is_cancelled(), - panicked = error.is_panic(), - "TUN UDP association task failed" - ); - } - None => {} - } - } - joined = dns_tasks.join_next(), if !dns_tasks.is_empty() => { - match joined { - Some(Ok(())) | None => {} - Some(Err(error)) => { - tracing::warn!( - cancelled = error.is_cancelled(), - panicked = error.is_panic(), - "TUN DNS query task failed" - ); - } - } - } - response = responses_rx.recv() => { - let Some(response) = response else { break; }; - if !emit_udp_response(&socket, response).await { - break; - } - } - response = dns_responses_rx.recv() => { - let Some(response) = response else { break; }; - if !emit_udp_response(&socket, response).await { - break; - } - } - datagram = socket.recv() => { - let Some(datagram) = datagram else { break; }; - if datagram.destination.port() == 53 - && let Some(dns) = &dns + let permit = dns.begin_query(); + let request = UdpDatagram::new( + datagram.source, + datagram.destination, + Bytes::copy_from_slice(datagram.payload), + ); + self.dns_tasks.spawn(observation::task(run_tun_dns_query( + dns.clone(), + permit, + request, + self.dns_responses.clone(), + effective_tun_mtu(self.context.limits), + self.context.resource_stats.clone(), + self.cancellation.clone(), + ))); + return; + } + let source = datagram.source; + if let Entry::Vacant(entry) = self.associations.entry(source) { + let association_id = next_diagnostic_session_id(); + tracing::debug!(association_id, "TUN UDP association created"); + let (sender, receiver) = + mpsc::channel(self.context.limits.tun_udp_association_queue_capacity); + let child_cancellation = self.cancellation.child_token(); + let last_activity = Arc::new(AtomicU64::new(self.association_clock.now())); + entry.insert(UdpAssociation { + generation: association_id, + sender, + cancellation: child_cancellation.clone(), + last_activity: last_activity.clone(), + }); + let activity = self + .context + .resource_stats + .begin(ResourceActivity::UdpAssociation); + self.tasks.spawn(observation::task(run_udp_association( + receiver, + activity, + UdpAssociationTaskContext { + association_id, + source, + responses: self.responses.clone(), + dispatcher: self.context.dispatcher.clone(), + resource_stats: self.context.resource_stats.clone(), + association_clock: self.association_clock.clone(), + last_activity, + tun_mtu: effective_tun_mtu(self.context.limits), + sniffer: self.context.sniffer.clone(), + cancellation: child_cancellation, + }, + ))); + } + let Some(association) = self.associations.get(&source) else { + return; + }; + let generation = association.generation; + match try_queue_association_input( + association, + datagram, + self.association_clock.now(), + self.context.limits.tun_udp_association_queue_capacity, + &self.context.resource_stats, + ) { + AssociationInputResult::Queued | AssociationInputResult::Full => {} + AssociationInputResult::Closed => { + if let Some(association) = + remove_completed_association(&mut self.associations, source, generation) { - match classify_query(&datagram.payload) { - Ok(_) => {} - Err(error) => { - tracing::debug!(%error, "dropping invalid TUN DNS datagram"); - continue; - } - } - let permit = dns.begin_query(); - dns_tasks.spawn(observation::task(run_tun_dns_query( - dns.clone(), - permit, - datagram, - dns_responses_tx.clone(), - tun_mtu, - resource_stats.clone(), - cancellation.clone(), - ))); - continue; - } - let source = datagram.source; - if let Entry::Vacant(entry) = associations.entry(source) { - let association_id = next_diagnostic_session_id(); - tracing::debug!(association_id, "TUN UDP association created"); - let (sender, receiver) = mpsc::channel(association_queue); - let child_cancellation = cancellation.child_token(); - let last_activity = Arc::new(AtomicU64::new(association_clock.now())); - entry.insert(UdpAssociation { - generation: association_id, - sender, - cancellation: child_cancellation.clone(), - last_activity: last_activity.clone(), - }); - let activity = - resource_stats.begin(ResourceActivity::UdpAssociation); - tasks.spawn(observation::task(run_udp_association( - receiver, - activity, - UdpAssociationTaskContext { - association_id, - source, - responses: responses_tx.clone(), - dispatcher: dispatcher.clone(), - resource_stats: resource_stats.clone(), - association_clock: association_clock.clone(), - last_activity, - tun_mtu, - sniffer: sniffer.clone(), - cancellation: child_cancellation, - }, - ))); - } - let Some(association) = associations.get(&source) else { - continue; - }; - match try_queue_association_input( - association, - datagram, - association_clock.now(), - association_queue, - &resource_stats, - ) { - AssociationInputResult::Queued => {} - AssociationInputResult::Full => { - // One slow source must not apply backpressure to the - // global TUN UDP loop. UDP loss is local to this one - // datagram; other associations and ready responses - // remain able to advance. - } - AssociationInputResult::Closed => { - let generation = association.generation; - let removed = - remove_completed_association(&mut associations, source, generation) - .into_iter() - .map(|association| (source, association)) - .collect(); - cancel_removed_associations(removed); - tracing::debug!("dropping TUN UDP datagram for a closing association"); - } + association.cancellation.cancel(); } + // Do not retry this packet against a replacement generation. + tracing::debug!("dropping TUN UDP datagram for a closing association"); } } } - drop(responses_tx); - drop(dns_responses_tx); - dns_tasks.abort_all(); - while dns_tasks.join_next().await.is_some() {} - let removed = associations.drain().collect::>(); - for (_, association) in &removed { - association.cancellation.cancel(); + fn cleanup(&mut self) { + cancel_removed_associations(take_expired_or_closed_associations( + &mut self.associations, + self.association_clock.now(), + )); + } + + fn complete_association( + &mut self, + joined: Option>, + ) { + match joined { + Some(Ok((source, association_id, result))) => { + remove_completed_association(&mut self.associations, source, association_id); + match result { + Ok(()) => tracing::debug!(association_id, "TUN UDP association closed"), + Err(error) => tracing::warn!( + association_id, error_kind = ?error.kind(), "TUN UDP association failed" + ), + } + } + Some(Err(error)) => tracing::warn!( + cancelled = error.is_cancelled(), + panicked = error.is_panic(), + "TUN UDP association task failed" + ), + None => {} + } + } + + async fn stop(&mut self) { + self.cancellation.cancel(); + self.dns_tasks.abort_all(); + while self.dns_tasks.join_next().await.is_some() {} + for (_, association) in self.associations.drain() { + association.cancellation.cancel(); + } + while self.tasks.join_next().await.is_some() {} + } +} + +impl Drop for UdpIngress { + fn drop(&mut self) { + self.cancellation.cancel(); + // Only a cancellation fallback: normal stop explicitly joins children. + self.tasks.abort_all(); + self.dns_tasks.abort_all(); } - drop(removed); - while tasks.join_next().await.is_some() {} - Ok(()) } struct QueuedUdpResponse { @@ -964,29 +1203,6 @@ impl Deref for QueuedUdpResponse { } } -async fn emit_udp_response(socket: &UdpSocket, response: QueuedUdpResponse) -> bool { - let QueuedUdpResponse { - datagram, - dns_permit, - } = response; - let result = socket.send(datagram).await; - drop(dns_permit); - match result { - Ok(()) => true, - Err(vcore_netstack::UdpError::Stopped) => false, - Err( - error @ (vcore_netstack::UdpError::MtuExceeded { .. } - | vcore_netstack::UdpError::AddressFamilyMismatch), - ) => { - tracing::debug!( - error_code = ?error, - "dropping UDP response that cannot be emitted to TUN" - ); - true - } - } -} - async fn run_tun_dns_query( dns: Arc, permit: DnsQueryPermit, @@ -1052,7 +1268,7 @@ fn try_queue_tun_udp_response( ResponseQueueResult::Queued } Err(mpsc::error::TrySendError::Full(_)) => { - resource_stats.queue_drop(ResourceQueue::Udp, responses.max_capacity()); + resource_stats.queue_drop(ResourceQueue::UdpResponse, responses.max_capacity()); ResponseQueueResult::Dropped } Err(mpsc::error::TrySendError::Closed(_)) => ResponseQueueResult::Closed, @@ -1116,6 +1332,8 @@ impl QuicSniffEngine for QuicSniffer { struct PreparedTunUdpDatagram { datagram: UdpDatagram, sniffed_domain: Option>, + /// Carried once, on the first released datagram of an authenticated Initial flight. + flow_id: Option, } impl PreparedTunUdpDatagram { @@ -1123,6 +1341,7 @@ impl PreparedTunUdpDatagram { Self { datagram, sniffed_domain: None, + flow_id: None, } } @@ -1130,8 +1349,14 @@ impl PreparedTunUdpDatagram { Self { datagram, sniffed_domain: Some(domain), + flow_id: None, } } + + fn with_flow_id(mut self, flow_id: Option) -> Self { + self.flow_id = flow_id; + self + } } struct PendingQuicFlow { @@ -1225,7 +1450,13 @@ where fn ingest_datagram(&mut self, datagram: UdpDatagram, now: TokioInstant) -> QuicIngressResult { let destination = datagram.destination; let connection_key = quic_connection_key(&datagram.payload); - let state = self.flows.remove(&destination); + let state = self.flows.remove(&destination).filter(|state| match state { + QuicFlowState::Matched { completed, .. } | QuicFlowState::NoDomain(completed) => { + now.saturating_duration_since(completed.last_used) + < Duration::from_secs(UDP_IDLE_TIMEOUT_SECONDS) + } + QuicFlowState::Pending(_) => true, + }); if connection_key.is_none() && quic_has_unsupported_version(&datagram.payload) { return self.fail_open_unsupported_version(datagram, state, now); } @@ -1283,6 +1514,16 @@ where if starts_new_quic_connection(&pending.connection_key, &connection_key) { let outcome = pending.sniffer.ingest(&datagram.payload); if pending.sniffer.authenticated_initial_in_last_ingest() { + // A non-Initial prefix can have entered the pending + // state before any Initial keys existed. Adopt only + // its first authenticated identity; a later header + // DCID accepted by existing keys is the same flow. + if pending.connection_key.is_none() { + pending.connection_key = connection_key; + if let Some(first) = pending.datagrams.front_mut() { + first.flow_id = pending.connection_key.clone(); + } + } return self.apply_pending_outcome( destination, pending, @@ -1382,11 +1623,20 @@ where outcome: QuicSniffOutcome, ) -> QuicIngressResult { let destination = datagram.destination; + // A parseable header alone must not suppress a later authenticated + // Initial with the same DCID, or claim a connection identity. + let connection_key = sniffer + .authenticated_initial_in_last_ingest() + .then_some(connection_key) + .flatten(); + let flow_id = connection_key.clone(); match outcome { QuicSniffOutcome::NeedMoreData if self.can_buffer(datagram.payload.len()) => { let buffered_bytes = datagram.payload.len(); let mut datagrams = VecDeque::new(); - datagrams.push_back(PreparedTunUdpDatagram::without_domain(datagram)); + datagrams.push_back( + PreparedTunUdpDatagram::without_domain(datagram).with_flow_id(flow_id), + ); self.pending_datagrams += 1; self.pending_bytes += buffered_bytes; self.flows.insert( @@ -1413,7 +1663,9 @@ where }, }, ); - QuicIngressResult::Forward(PreparedTunUdpDatagram::with_domain(datagram, domain)) + QuicIngressResult::Forward( + PreparedTunUdpDatagram::with_domain(datagram, domain).with_flow_id(flow_id), + ) } QuicSniffOutcome::NeedMoreData | QuicSniffOutcome::EchExtensionPresent @@ -1426,7 +1678,9 @@ where last_used: now, }), ); - QuicIngressResult::Forward(PreparedTunUdpDatagram::without_domain(datagram)) + QuicIngressResult::Forward( + PreparedTunUdpDatagram::without_domain(datagram).with_flow_id(flow_id), + ) } } } @@ -1607,15 +1861,27 @@ async fn send_tun_udp_datagram( let PreparedTunUdpDatagram { datagram, sniffed_domain, + flow_id, } = prepared; + let datagram = Datagram { + remote: Destination::Ip(datagram.destination), + payload: datagram.payload, + sniffed_domain, + }; + let send_datagram = async { + match flow_id { + Some(flow_id) => { + transport + .send_with_flow_id(datagram, &flow_id.routing_identity()) + .await + } + None => transport.send(datagram).await, + } + }; let send = tokio::select! { biased; () = context.cancellation.cancelled() => return Ok(false), - result = timeout(OUTBOUND_SEND_TIMEOUT, transport.send(Datagram { - remote: Destination::Ip(datagram.destination), - payload: datagram.payload, - sniffed_domain, - })) => result, + result = timeout(OUTBOUND_SEND_TIMEOUT, send_datagram) => result, }; match send { Ok(Ok(())) => Ok(true), @@ -1644,19 +1910,6 @@ fn append_ready_datagrams( Ok(()) } -fn queue_quic_ingress_result( - ready: &mut VecDeque, - result: QuicIngressResult, -) -> io::Result<()> { - match result { - QuicIngressResult::Buffered => Ok(()), - QuicIngressResult::Forward(prepared) => { - append_ready_datagrams(ready, VecDeque::from([prepared])) - } - QuicIngressResult::Replay(replay) => append_ready_datagrams(ready, replay), - } -} - async fn run_udp_association_inner_with_quic_factory( inbound: &mut mpsc::Receiver, context: &UdpAssociationTaskContext, @@ -1692,7 +1945,10 @@ where }; let mut quic_sniff = UdpQuicSniffState::new(new_sniffer); - let mut ready = VecDeque::with_capacity(QUIC_SNIFF_READY_DATAGRAM_MAX); + // Ordinary UDP needs only one pending send, not an allocated replay queue. + // The deque grows only for actual QUIC replay and keeps its existing bound. + let mut ready = VecDeque::new(); + let mut pending_send = None; let mut first_response_logged = false; loop { let sniff_deadline = quic_sniff.next_deadline(); @@ -1704,9 +1960,9 @@ where } // Drain a ready response before accepting another datagram from // this same source. Fairness between the ordinary and DNS TUN - // response queues is handled independently by `udp_loop`. + // response queues is handled independently by the sole TUN writer. datagram = transport.receive() => AssociationEvent::Outbound(datagram), - () = std::future::ready(()), if !ready.is_empty() => AssociationEvent::ReadySend, + () = std::future::ready(()), if pending_send.is_some() || !ready.is_empty() => AssociationEvent::ReadySend, datagram = inbound.recv() => AssociationEvent::Inbound(datagram), }; match event { @@ -1715,8 +1971,9 @@ where append_ready_datagrams(&mut ready, quic_sniff.expire(TokioInstant::now()))?; } AssociationEvent::ReadySend => { - let prepared = ready - .pop_front() + let prepared = pending_send + .take() + .or_else(|| ready.pop_front()) .expect("ready-send event requires a queued datagram"); if !send_tun_udp_datagram(transport.as_mut(), prepared, context).await? { break; @@ -1732,7 +1989,16 @@ where } else { QuicIngressResult::Forward(PreparedTunUdpDatagram::without_domain(datagram)) }; - queue_quic_ingress_result(&mut ready, result)?; + match result { + QuicIngressResult::Buffered => {} + QuicIngressResult::Forward(prepared) => { + debug_assert!(pending_send.is_none()); + pending_send = Some(prepared); + } + QuicIngressResult::Replay(replay) => { + append_ready_datagrams(&mut ready, replay)? + } + } } AssociationEvent::Outbound(Err(error)) => { return Err(dispatch_to_io(error)); @@ -1813,2531 +2079,4 @@ fn dispatch_to_io(error: DispatchError) -> io::Error { } #[cfg(all(test, unix))] -mod tests { - use std::{ - net::{Ipv4Addr, SocketAddr}, - os::fd::AsRawFd, - os::unix::net::UnixDatagram, - sync::{ - Mutex, - atomic::{AtomicUsize, Ordering}, - }, - }; - - use async_trait::async_trait; - use tokio::{ - io::AsyncReadExt as _, - sync::{Notify, mpsc}, - }; - - use super::*; - use crate::{ - config::{ - DnsConfig, DnsNameserver, DnsRoute, DnsTransport, PortRange, RuleAction, RuleKind, - RuleSpec, SnifferConfig, - }, - dispatch::{BoxStream, DatagramTransport}, - dns::{QueryType, build_query, synthesize_empty_response}, - platform::TunFd, - routing::{EmptyGeoMatcher, ProxyDispatchers, RuleSet}, - }; - - fn test_sniffer( - http_ports: &[PortRange], - tls_ports: &[PortRange], - quic_ports: &[PortRange], - ) -> Arc { - Arc::new(SnifferConfig { - enable: true, - http_ports: http_ports.into(), - tls_ports: tls_ports.into(), - quic_ports: quic_ports.into(), - }) - } - - #[test] - fn tun_netstack_resource_event_contract_is_stable() { - assert_eq!(TUN_NETSTACK_STATS_INTERVAL, Duration::from_secs(30)); - assert_eq!( - TUN_NETSTACK_STATS_PERIODIC_EVENT, - "tun_netstack_stats_periodic" - ); - assert_eq!(TUN_NETSTACK_STATS_FINAL_EVENT, "tun_netstack_stats_final"); - } - - #[test] - fn ipv6_ingress_policy_drops_only_ipv6_when_disabled() { - assert!(!tun_ingress_allowed(false, &[0x60])); - assert!(tun_ingress_allowed(false, &[0x45])); - assert!(tun_ingress_allowed(false, &[])); - assert!(tun_ingress_allowed(true, &[0x60])); - } - - #[test] - fn configured_sniffer_selects_custom_http_and_tls_ports() { - let config = test_sniffer( - &[PortRange { - start: 8_080, - end: 8_088, - }], - &[PortRange { - start: 8_443, - end: 8_443, - }], - &[], - ); - assert_eq!( - configured_sniff_protocol(&config, 8_084), - Some(SniffProtocol::Http) - ); - assert_eq!( - configured_sniff_protocol(&config, 8_443), - Some(SniffProtocol::Tls) - ); - assert_eq!(configured_sniff_protocol(&config, 80), None); - assert_eq!(configured_sniff_protocol(&config, 443), None); - } - - struct ScriptedQuicSniffer { - outcomes: VecDeque, - authentications: VecDeque, - authenticated_initial_in_last_ingest: bool, - } - - impl QuicSniffEngine for ScriptedQuicSniffer { - fn ingest(&mut self, _packet: &[u8]) -> QuicSniffOutcome { - self.authenticated_initial_in_last_ingest = - self.authentications.pop_front().unwrap_or(true); - self.outcomes - .pop_front() - .expect("scripted QUIC sniffer outcome exhausted") - } - - fn authenticated_initial_in_last_ingest(&self) -> bool { - self.authenticated_initial_in_last_ingest - } - } - - fn scripted_quic_state( - scripts: Vec>, - ) -> UdpQuicSniffState ScriptedQuicSniffer> { - let mut scripts = scripts - .into_iter() - .map(VecDeque::from) - .collect::>(); - UdpQuicSniffState::new(move || ScriptedQuicSniffer { - outcomes: scripts - .pop_front() - .expect("scripted QUIC flow factory exhausted"), - authentications: VecDeque::new(), - authenticated_initial_in_last_ingest: false, - }) - } - - fn scripted_quic_state_with_authentication( - scripts: Vec>, - ) -> UdpQuicSniffState ScriptedQuicSniffer> { - let mut sniffers = scripts - .into_iter() - .map(|script| { - let (outcomes, authentications) = script.into_iter().unzip(); - ScriptedQuicSniffer { - outcomes, - authentications, - authenticated_initial_in_last_ingest: false, - } - }) - .collect::>(); - UdpQuicSniffState::new(move || { - sniffers - .pop_front() - .expect("scripted QUIC flow factory exhausted") - }) - } - - fn test_udp_datagram( - source: SocketAddr, - destination: SocketAddr, - payload: &'static [u8], - ) -> UdpDatagram { - UdpDatagram::new(source, destination, Bytes::from_static(payload)) - } - - fn quic_connection_marker(destination_connection_id: &[u8]) -> Bytes { - assert!((1..=20).contains(&destination_connection_id.len())); - let mut datagram = vec![ - 0xc0, // QUIC v1 long header, Initial packet. - 0x00, - 0x00, - 0x00, - 0x01, - u8::try_from(destination_connection_id.len()).unwrap(), - ]; - datagram.extend_from_slice(destination_connection_id); - datagram.extend_from_slice(&[ - 0x00, // Empty source connection ID. - 0x00, // Empty token. - 0x11, // One packet-number byte plus a 16-byte AEAD tag. - ]); - datagram.extend_from_slice(&[0; 17]); - let datagram = Bytes::from(datagram); - assert!(quic_connection_key(&datagram).is_some()); - datagram - } - - fn quic_non_initial_marker(packet_type: u8, destination_connection_id: &[u8]) -> Bytes { - assert!((1..=3).contains(&packet_type)); - assert!((1..=20).contains(&destination_connection_id.len())); - let mut datagram = vec![ - 0xc0 | (packet_type << 4), - 0x00, - 0x00, - 0x00, - 0x01, - u8::try_from(destination_connection_id.len()).unwrap(), - ]; - datagram.extend_from_slice(destination_connection_id); - datagram.extend_from_slice(&[ - 0x00, // Empty source connection ID. - 0x11, // Protected payload length. - ]); - datagram.extend_from_slice(&[0; 17]); - let datagram = Bytes::from(datagram); - assert!(quic_connection_key(&datagram).is_none()); - assert!(!quic_has_unsupported_version(&datagram)); - datagram - } - - fn unsupported_quic_version_marker(destination_connection_id: &[u8]) -> Bytes { - assert!((1..=20).contains(&destination_connection_id.len())); - let mut datagram = vec![ - 0xc0, - 0xfa, - 0xce, - 0xb0, - 0x0c, - u8::try_from(destination_connection_id.len()).unwrap(), - ]; - datagram.extend_from_slice(destination_connection_id); - let datagram = Bytes::from(datagram); - assert!(quic_connection_key(&datagram).is_none()); - assert!(quic_has_unsupported_version(&datagram)); - datagram - } - - #[test] - fn quic_fragmentation_sends_nothing_early_then_replays_every_datagram_in_order() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![vec![ - QuicSniffOutcome::NeedMoreData, - QuicSniffOutcome::Matched("api.example.com".to_owned()), - ]]); - - assert!(matches!( - state.ingest_datagram(test_udp_datagram(source, destination, b"initial-one"), now,), - QuicIngressResult::Buffered - )); - assert_eq!(state.pending_datagrams, 1); - assert_eq!(state.pending_bytes, b"initial-one".len()); - - let QuicIngressResult::Replay(replay) = state.ingest_datagram( - test_udp_datagram(source, destination, b"initial-two"), - now + Duration::from_millis(1), - ) else { - panic!("the completed ClientHello must release the buffered Initial flight"); - }; - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - assert_eq!( - replay - .iter() - .map(|prepared| prepared.datagram.payload.as_ref()) - .collect::>(), - vec![b"initial-one".as_slice(), b"initial-two".as_slice()] - ); - assert!(replay.iter().all(|prepared| { - prepared.sniffed_domain.as_deref() == Some("api.example.com") - && prepared.datagram.destination == destination - })); - - let QuicIngressResult::Forward(next) = state.ingest_datagram( - test_udp_datagram(source, destination, b"short-header"), - now + Duration::from_millis(2), - ) else { - panic!("a resolved QUIC flow must forward later datagrams immediately"); - }; - assert_eq!(next.sniffed_domain.as_deref(), Some("api.example.com")); - } - - #[test] - fn quic_completed_flow_lru_makes_room_for_a_fifth_sniffer() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destinations = (0..=QUIC_SNIFF_FLOW_MAX) - .map(|index| { - SocketAddr::new( - "198.51.100.20".parse().unwrap(), - 443 + u16::try_from(index).unwrap(), - ) - }) - .collect::>(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state( - (0..=QUIC_SNIFF_FLOW_MAX) - .map(|index| { - vec![QuicSniffOutcome::Matched(format!( - "node-{index}.example.com" - ))] - }) - .collect(), - ); - - for (index, destination) in destinations[..QUIC_SNIFF_FLOW_MAX].iter().enumerate() { - let result = state.ingest_datagram( - UdpDatagram::new( - source, - *destination, - quic_connection_marker(&[u8::try_from(index + 1).unwrap()]), - ), - now + Duration::from_millis(u64::try_from(index).unwrap()), - ); - assert!(matches!(result, QuicIngressResult::Forward(_))); - } - assert_eq!(state.flows.len(), QUIC_SNIFF_FLOW_MAX); - - let touched = state.ingest_datagram( - test_udp_datagram(source, destinations[0], b"short-header"), - now + Duration::from_millis(10), - ); - assert!(matches!(touched, QuicIngressResult::Forward(_))); - - let QuicIngressResult::Forward(fifth) = state.ingest_datagram( - UdpDatagram::new( - source, - destinations[QUIC_SNIFF_FLOW_MAX], - quic_connection_marker(b"fifth"), - ), - now + Duration::from_millis(11), - ) else { - panic!("a completed flow must be evicted so the fifth flow can be sniffed"); - }; - assert_eq!(fifth.sniffed_domain.as_deref(), Some("node-4.example.com")); - assert_eq!(state.flows.len(), QUIC_SNIFF_FLOW_MAX); - assert!(state.flows.contains_key(&destinations[0])); - assert!(!state.flows.contains_key(&destinations[1])); - assert!(state.flows.contains_key(&destinations[2])); - assert!(state.flows.contains_key(&destinations[3])); - assert!(state.flows.contains_key(&destinations[4])); - } - - #[test] - fn quic_new_dcid_replaces_a_matched_domain_for_the_same_destination() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![ - vec![QuicSniffOutcome::Matched("old.example.com".to_owned())], - vec![QuicSniffOutcome::Matched("new.example.com".to_owned())], - ]); - - let QuicIngressResult::Forward(old) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"old")), - now, - ) else { - panic!("the first QUIC connection must be sniffed"); - }; - assert_eq!(old.sniffed_domain.as_deref(), Some("old.example.com")); - - let QuicIngressResult::Forward(new) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"new")), - now + Duration::from_millis(1), - ) else { - panic!("a new DCID must create a fresh QUIC sniffer"); - }; - assert_eq!(new.sniffed_domain.as_deref(), Some("new.example.com")); - - let QuicIngressResult::Forward(short_header) = state.ingest_datagram( - test_udp_datagram(source, destination, b"short-header"), - now + Duration::from_millis(2), - ) else { - panic!("the replacement domain must be retained for short-header traffic"); - }; - assert_eq!( - short_header.sniffed_domain.as_deref(), - Some("new.example.com") - ); - } - - #[test] - fn quic_unauthenticated_new_dcid_keeps_the_completed_domain_without_waiting() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state_with_authentication(vec![ - vec![( - QuicSniffOutcome::Matched("stable.example.com".to_owned()), - true, - )], - vec![(QuicSniffOutcome::NeedMoreData, false)], - ]); - - let QuicIngressResult::Forward(first) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"old")), - now, - ) else { - panic!("the first QUIC connection must be sniffed"); - }; - assert_eq!(first.sniffed_domain.as_deref(), Some("stable.example.com")); - - let QuicIngressResult::Forward(candidate) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"new")), - now + Duration::from_millis(1), - ) else { - panic!("an unauthenticated candidate must not enter the pending state"); - }; - assert_eq!( - candidate.sniffed_domain.as_deref(), - Some("stable.example.com") - ); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - - let QuicIngressResult::Forward(short_header) = state.ingest_datagram( - test_udp_datagram(source, destination, b"short-header"), - now + Duration::from_millis(2), - ) else { - panic!("the authenticated completed hint must be retained"); - }; - assert_eq!( - short_header.sniffed_domain.as_deref(), - Some("stable.example.com") - ); - } - - #[test] - fn quic_pending_flow_accepts_a_new_header_dcid_authenticated_by_its_old_keys() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state_with_authentication(vec![vec![ - (QuicSniffOutcome::NeedMoreData, true), - ( - QuicSniffOutcome::Matched("same-connection.example.com".to_owned()), - true, - ), - ]]); - - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-a")), - now, - ), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Replay(replay) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-b")), - now + Duration::from_millis(1), - ) else { - panic!("old Initial keys must be tried before treating a new DCID as a new flow"); - }; - assert_eq!(replay.len(), 2); - assert!(replay.iter().all(|prepared| { - prepared.sniffed_domain.as_deref() == Some("same-connection.example.com") - })); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - } - - #[test] - fn quic_pending_flow_keeps_old_state_when_neither_key_authenticates_a_candidate() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state_with_authentication(vec![ - vec![ - (QuicSniffOutcome::NeedMoreData, true), - (QuicSniffOutcome::NotMatched, false), - ( - QuicSniffOutcome::Matched("old-flow.example.com".to_owned()), - true, - ), - ], - vec![(QuicSniffOutcome::NeedMoreData, false)], - ]); - - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-a")), - now, - ), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Forward(unverified) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-b")), - now + Duration::from_millis(1), - ) else { - panic!("an unverified candidate must fail open independently"); - }; - assert!(unverified.sniffed_domain.is_none()); - assert_eq!(state.pending_datagrams, 1); - - let QuicIngressResult::Replay(old_flow) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-a")), - now + Duration::from_millis(2), - ) else { - panic!("the old pending parser and buffered flight must be retained"); - }; - assert_eq!(old_flow.len(), 2); - assert!( - old_flow - .iter() - .all(|prepared| prepared.sniffed_domain.as_deref() == Some("old-flow.example.com")) - ); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - } - - #[test] - fn quic_zero_rtt_and_handshake_dcid_changes_do_not_reset_a_completed_hint() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![vec![QuicSniffOutcome::Matched( - "stable.example.com".to_owned(), - )]]); - - let QuicIngressResult::Forward(initial) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"initial")), - now, - ) else { - panic!("the Initial must establish a completed hint"); - }; - assert_eq!( - initial.sniffed_domain.as_deref(), - Some("stable.example.com") - ); - - for (index, datagram) in [ - quic_non_initial_marker(1, b"zero-rtt"), - quic_non_initial_marker(2, b"handshake"), - ] - .into_iter() - .enumerate() - { - let QuicIngressResult::Forward(forwarded) = state.ingest_datagram( - UdpDatagram::new(source, destination, datagram), - now + Duration::from_millis(u64::try_from(index + 1).unwrap()), - ) else { - panic!("a non-Initial long header must not trigger a new sniffer"); - }; - assert_eq!( - forwarded.sniffed_domain.as_deref(), - Some("stable.example.com") - ); - } - } - - #[test] - fn unsupported_quic_version_clears_completed_hint_and_releases_pending_flight() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let completed_destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let pending_destination: SocketAddr = "198.51.100.21:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![ - vec![QuicSniffOutcome::Matched( - "must-not-leak.example.com".to_owned(), - )], - vec![QuicSniffOutcome::NeedMoreData], - ]); - - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new( - source, - completed_destination, - quic_connection_marker(b"completed"), - ), - now, - ), - QuicIngressResult::Forward(_) - )); - let QuicIngressResult::Forward(unsupported) = state.ingest_datagram( - UdpDatagram::new( - source, - completed_destination, - unsupported_quic_version_marker(b"unknown"), - ), - now + Duration::from_millis(1), - ) else { - panic!("an unsupported version must fail open without waiting"); - }; - assert!(unsupported.sniffed_domain.is_none()); - let QuicIngressResult::Forward(short_header) = state.ingest_datagram( - test_udp_datagram(source, completed_destination, b"short-header"), - now + Duration::from_millis(2), - ) else { - panic!("the stale completed hint must remain cleared"); - }; - assert!(short_header.sniffed_domain.is_none()); - - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new( - source, - pending_destination, - quic_connection_marker(b"pending"), - ), - now, - ), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Replay(replay) = state.ingest_datagram( - UdpDatagram::new( - source, - pending_destination, - unsupported_quic_version_marker(b"unknown"), - ), - now + Duration::from_millis(1), - ) else { - panic!("an unsupported version must release a pending flight"); - }; - assert_eq!(replay.len(), 2); - assert!( - replay - .iter() - .all(|prepared| prepared.sniffed_domain.is_none()) - ); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - } - - #[test] - fn quic_new_dcid_retries_sniffing_after_a_no_domain_result() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![ - vec![QuicSniffOutcome::NotMatched], - vec![QuicSniffOutcome::Matched( - "recovered.example.com".to_owned(), - )], - ]); - - let QuicIngressResult::Forward(first) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"old")), - now, - ) else { - panic!("the first QUIC connection must fail open"); - }; - assert!(first.sniffed_domain.is_none()); - - let QuicIngressResult::Forward(recovered) = state.ingest_datagram( - UdpDatagram::new(source, destination, quic_connection_marker(b"new")), - now + Duration::from_millis(1), - ) else { - panic!("a new DCID must retry sniffing after a terminal result"); - }; - assert_eq!( - recovered.sniffed_domain.as_deref(), - Some("recovered.example.com") - ); - } - - #[test] - fn quic_new_dcid_releases_an_old_pending_flight_before_the_new_connection() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let old_payload = quic_connection_marker(b"old"); - let new_payload = quic_connection_marker(b"new"); - let mut state = scripted_quic_state_with_authentication(vec![ - vec![ - (QuicSniffOutcome::NeedMoreData, true), - (QuicSniffOutcome::NotMatched, false), - ], - vec![( - QuicSniffOutcome::Matched("new.example.com".to_owned()), - true, - )], - ]); - - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new(source, destination, old_payload.clone()), - now, - ), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Replay(replay) = state.ingest_datagram( - UdpDatagram::new(source, destination, new_payload.clone()), - now + Duration::from_millis(1), - ) else { - panic!("a new DCID must release the stale flight before forwarding itself"); - }; - assert_eq!(replay.len(), 2); - assert_eq!(replay[0].datagram.payload, old_payload); - assert!(replay[0].sniffed_domain.is_none()); - assert_eq!(replay[1].datagram.payload, new_payload); - assert_eq!(replay[1].sniffed_domain.as_deref(), Some("new.example.com")); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - } - - #[tokio::test] - async fn quic_association_holds_the_first_fragment_then_sends_the_original_flight() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let datagrams = Arc::new(Mutex::new(Vec::new())); - let sent = Arc::new(Notify::new()); - let dispatcher = Arc::new(RecordingUdpDispatcher { - datagrams: datagrams.clone(), - sent: sent.clone(), - }); - let (inbound_tx, mut inbound_rx) = mpsc::channel(4); - let (responses, _responses_rx) = mpsc::channel(4); - let cancellation = CancellationToken::new(); - let child = cancellation.clone(); - let mut scripts = VecDeque::from([VecDeque::from([ - QuicSniffOutcome::NeedMoreData, - QuicSniffOutcome::Matched("api.example.com".to_owned()), - ])]); - let task = tokio::spawn(async move { - run_udp_association_inner_with_quic_factory( - &mut inbound_rx, - &UdpAssociationTaskContext { - association_id: 1, - source, - responses, - dispatcher, - resource_stats: RuntimeResourceStats::new("tun_runtime_quic_test"), - association_clock: AssociationClock::realtime(), - last_activity: Arc::new(AtomicU64::new(0)), - tun_mtu: TUN_MTU, - sniffer: Some(test_sniffer( - &[], - &[], - &[PortRange { - start: 443, - end: 443, - }], - )), - cancellation: child, - }, - move || ScriptedQuicSniffer { - outcomes: scripts - .pop_front() - .expect("scripted QUIC flow factory exhausted"), - authentications: VecDeque::new(), - authenticated_initial_in_last_ingest: false, - }, - ) - .await - }); - - inbound_tx - .send(test_udp_datagram(source, destination, b"initial-one")) - .await - .unwrap(); - assert!( - timeout(Duration::from_millis(30), sent.notified()) - .await - .is_err(), - "the first incomplete Initial was sent before sniffing completed" - ); - assert!(datagrams.lock().unwrap().is_empty()); - - inbound_tx - .send(test_udp_datagram(source, destination, b"initial-two")) - .await - .unwrap(); - timeout(Duration::from_secs(1), async { - while datagrams.lock().unwrap().len() != 2 { - sent.notified().await; - } - }) - .await - .expect("the completed QUIC Initial flight was not replayed"); - { - let recorded = datagrams.lock().unwrap(); - assert_eq!( - recorded - .iter() - .map(|datagram| datagram.payload.as_ref()) - .collect::>(), - vec![b"initial-one".as_slice(), b"initial-two".as_slice()] - ); - assert!(recorded.iter().all(|datagram| { - datagram.remote == Destination::Ip(destination) - && datagram.sniffed_domain.as_deref() == Some("api.example.com") - })); - } - - cancellation.cancel(); - timeout(Duration::from_secs(1), task) - .await - .expect("QUIC association did not stop") - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn quic_ready_response_is_processed_before_the_next_replay_send_blocks() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let send_count = Arc::new(AtomicUsize::new(0)); - let blocked_send_started = Arc::new(Notify::new()); - let dispatcher = Arc::new(ReplayFairDispatcher { - response: Datagram { - remote: Destination::Ip(destination), - payload: Bytes::from_static(b"ready-response"), - sniffed_domain: None, - }, - send_count: send_count.clone(), - blocked_send_started: blocked_send_started.clone(), - }); - let (inbound_tx, mut inbound_rx) = mpsc::channel(4); - let (responses, mut responses_rx) = mpsc::channel(4); - let cancellation = CancellationToken::new(); - let child = cancellation.clone(); - let mut scripts = VecDeque::from([VecDeque::from([ - QuicSniffOutcome::NeedMoreData, - QuicSniffOutcome::Matched("api.example.com".to_owned()), - ])]); - let task = tokio::spawn(async move { - run_udp_association_inner_with_quic_factory( - &mut inbound_rx, - &UdpAssociationTaskContext { - association_id: 1, - source, - responses, - dispatcher, - resource_stats: RuntimeResourceStats::new("tun_runtime_quic_fairness_test"), - association_clock: AssociationClock::realtime(), - last_activity: Arc::new(AtomicU64::new(0)), - tun_mtu: TUN_MTU, - sniffer: Some(test_sniffer( - &[], - &[], - &[PortRange { - start: 443, - end: 443, - }], - )), - cancellation: child, - }, - move || ScriptedQuicSniffer { - outcomes: scripts - .pop_front() - .expect("scripted QUIC flow factory exhausted"), - authentications: VecDeque::new(), - authenticated_initial_in_last_ingest: false, - }, - ) - .await - }); - - let marker = quic_connection_marker(b"flow"); - inbound_tx - .send(UdpDatagram::new(source, destination, marker.clone())) - .await - .unwrap(); - inbound_tx - .send(UdpDatagram::new(source, destination, marker)) - .await - .unwrap(); - - let response = timeout(Duration::from_secs(1), responses_rx.recv()) - .await - .expect("a blocked second replay send starved the ready response") - .expect("response channel closed"); - assert_eq!(&response.payload[..], b"ready-response"); - timeout(Duration::from_secs(1), blocked_send_started.notified()) - .await - .expect("the second replay send did not enter its blocked state"); - assert_eq!(send_count.load(Ordering::Relaxed), 2); - - cancellation.cancel(); - timeout(Duration::from_secs(1), task) - .await - .expect("QUIC association did not stop") - .unwrap() - .unwrap(); - } - - #[test] - fn quic_failure_and_timeout_fail_open_with_exact_buffered_payloads() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let failed_destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let timed_out_destination: SocketAddr = "198.51.100.21:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![ - vec![QuicSniffOutcome::NeedMoreData, QuicSniffOutcome::NotMatched], - vec![QuicSniffOutcome::NeedMoreData], - ]); - - assert!(matches!( - state.ingest_datagram( - test_udp_datagram(source, failed_destination, b"failed-one"), - now, - ), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Replay(failed) = state.ingest_datagram( - test_udp_datagram(source, failed_destination, b"failed-two"), - now + Duration::from_millis(1), - ) else { - panic!("a parse failure must release the original datagrams"); - }; - assert_eq!( - failed - .iter() - .map(|prepared| prepared.datagram.payload.as_ref()) - .collect::>(), - vec![b"failed-one".as_slice(), b"failed-two".as_slice()] - ); - assert!( - failed - .iter() - .all(|prepared| prepared.sniffed_domain.is_none()) - ); - - assert!(matches!( - state.ingest_datagram( - test_udp_datagram(source, timed_out_destination, b"timed-out"), - now, - ), - QuicIngressResult::Buffered - )); - assert!( - state - .expire(now + QUIC_SNIFF_TIMEOUT - Duration::from_millis(1)) - .is_empty() - ); - let timed_out = state.expire(now + QUIC_SNIFF_TIMEOUT); - assert_eq!(timed_out.len(), 1); - assert_eq!(&timed_out[0].datagram.payload[..], b"timed-out"); - assert!(timed_out[0].sniffed_domain.is_none()); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - } - - #[test] - fn quic_ech_and_parser_limit_release_buffered_datagrams_without_a_domain() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let now = TokioInstant::now(); - for (index, terminal) in [ - QuicSniffOutcome::EchExtensionPresent, - QuicSniffOutcome::LimitReached, - ] - .into_iter() - .enumerate() - { - let destination = SocketAddr::new( - "198.51.100.20".parse().unwrap(), - 443 + u16::try_from(index).unwrap(), - ); - let mut state = - scripted_quic_state(vec![vec![QuicSniffOutcome::NeedMoreData, terminal]]); - assert!(matches!( - state.ingest_datagram(test_udp_datagram(source, destination, b"initial-one"), now,), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Replay(replay) = - state.ingest_datagram(test_udp_datagram(source, destination, b"initial-two"), now) - else { - panic!("a terminal QUIC outcome must release buffered datagrams"); - }; - assert_eq!(replay.len(), 2); - assert!( - replay - .iter() - .all(|prepared| prepared.sniffed_domain.is_none()) - ); - } - } - - #[test] - fn quic_pending_datagram_and_flow_tables_are_hard_bounded() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![vec![ - QuicSniffOutcome::NeedMoreData; - QUIC_SNIFF_PENDING_DATAGRAM_MAX + 1 - ]]); - for index in 0..QUIC_SNIFF_PENDING_DATAGRAM_MAX { - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new(source, destination, vec![u8::try_from(index).unwrap()]), - now, - ), - QuicIngressResult::Buffered - )); - } - let QuicIngressResult::Replay(replay) = state.ingest_datagram( - UdpDatagram::new(source, destination, Bytes::from_static(b"overflow")), - now, - ) else { - panic!("the ninth pending datagram must fail open"); - }; - assert_eq!(replay.len(), QUIC_SNIFF_PENDING_DATAGRAM_MAX + 1); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - - let scripts = (0..QUIC_SNIFF_FLOW_MAX) - .map(|_| vec![QuicSniffOutcome::NeedMoreData]) - .collect(); - let mut state = scripted_quic_state(scripts); - for index in 0..QUIC_SNIFF_FLOW_MAX { - let destination = SocketAddr::new( - "198.51.100.20".parse().unwrap(), - 443 + u16::try_from(index).unwrap(), - ); - assert!(matches!( - state.ingest_datagram(test_udp_datagram(source, destination, b"pending"), now,), - QuicIngressResult::Buffered - )); - } - let overflow_destination: SocketAddr = "198.51.100.30:8443".parse().unwrap(); - let QuicIngressResult::Forward(overflow) = state.ingest_datagram( - test_udp_datagram(source, overflow_destination, b"fifth-flow"), - now, - ) else { - panic!("a fifth QUIC flow must fail open without creating a parser"); - }; - assert!(overflow.sniffed_domain.is_none()); - assert_eq!(state.flows.len(), QUIC_SNIFF_FLOW_MAX); - } - - #[test] - fn quic_pending_byte_budget_accepts_32_kib_and_replays_the_overflow_datagram() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![vec![ - QuicSniffOutcome::NeedMoreData, - QuicSniffOutcome::NeedMoreData, - ]]); - - assert!(matches!( - state.ingest_datagram( - UdpDatagram::new( - source, - destination, - vec![0xaa; QUIC_SNIFF_PENDING_BYTES_MAX], - ), - now, - ), - QuicIngressResult::Buffered - )); - assert_eq!(state.pending_datagrams, 1); - assert_eq!(state.pending_bytes, QUIC_SNIFF_PENDING_BYTES_MAX); - - let QuicIngressResult::Replay(replay) = state.ingest_datagram( - UdpDatagram::new(source, destination, Bytes::from_static(b"x")), - now, - ) else { - panic!("a datagram above the aggregate byte budget must fail open"); - }; - assert_eq!(replay.len(), 2); - assert_eq!( - replay[0].datagram.payload.len(), - QUIC_SNIFF_PENDING_BYTES_MAX - ); - assert_eq!(&replay[1].datagram.payload[..], b"x"); - assert_eq!(state.pending_datagrams, 0); - assert_eq!(state.pending_bytes, 0); - } - - #[test] - fn quic_flow_state_is_isolated_by_destination_and_unconfigured_ports_are_skipped() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination_a: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let destination_b: SocketAddr = "198.51.100.21:443".parse().unwrap(); - let now = TokioInstant::now(); - let mut state = scripted_quic_state(vec![ - vec![ - QuicSniffOutcome::NeedMoreData, - QuicSniffOutcome::Matched("a.example.com".to_owned()), - ], - vec![QuicSniffOutcome::Matched("b.example.com".to_owned())], - ]); - - assert!(matches!( - state.ingest_datagram(test_udp_datagram(source, destination_a, b"a-one"), now,), - QuicIngressResult::Buffered - )); - let QuicIngressResult::Forward(b) = - state.ingest_datagram(test_udp_datagram(source, destination_b, b"b-one"), now) - else { - panic!("one destination must not wait for another destination"); - }; - assert_eq!(b.sniffed_domain.as_deref(), Some("b.example.com")); - let QuicIngressResult::Replay(a) = - state.ingest_datagram(test_udp_datagram(source, destination_a, b"a-two"), now) - else { - panic!("the first destination must retain its independent parser"); - }; - assert!( - a.iter() - .all(|prepared| prepared.sniffed_domain.as_deref() == Some("a.example.com")) - ); - - let config = test_sniffer( - &[], - &[], - &[PortRange { - start: 443, - end: 443, - }], - ); - assert!(configured_quic_sniffing(Some(&config), 443)); - assert!(!configured_quic_sniffing(Some(&config), 8_443)); - assert!(!configured_quic_sniffing(None, 443)); - } - - #[test] - fn tun_dns_ingress_capacity_is_an_independent_queue_boundary() { - let tun = ResourceLimits::default(); - assert_eq!(tun_udp_ingress_queue_capacity(tun, false), 128); - assert_eq!(tun_udp_ingress_queue_capacity(tun, true), 128); - let altered = ResourceLimits { - tun_dns_ingress_queue_capacity: 96, - ..tun - }; - assert_eq!(tun_udp_ingress_queue_capacity(altered, true), 96); - assert_eq!( - tun_udp_ingress_queue_capacity(ResourceLimits::default(), true), - ResourceLimits::default().tun_dns_ingress_queue_capacity - ); - } - - #[test] - fn tun_netstack_keeps_queue_and_per_flow_bounds_without_a_flow_count_ceiling() { - let limits = ResourceLimits::default(); - let config = tun_netstack_config(limits, true, false); - - assert_eq!(config.tcp_accept_queue, limits.event_queue_capacity); - assert_eq!( - config.tcp_buffer_per_direction, - limits.tcp_buffer_per_direction - ); - assert_eq!(config.mtu, 1_500); - assert_eq!(limits.max_datagram_size, 65_535); - } - - fn test_association( - generation: u64, - last_activity: u64, - ) -> ( - UdpAssociation, - mpsc::Receiver, - CancellationToken, - ) { - let (sender, receiver) = mpsc::channel(1); - let cancellation = CancellationToken::new(); - ( - UdpAssociation { - generation, - sender, - cancellation: cancellation.clone(), - last_activity: Arc::new(AtomicU64::new(last_activity)), - }, - receiver, - cancellation, - ) - } - - #[test] - fn stale_association_completion_cannot_remove_a_replacement_generation() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let (replacement, _receiver, _) = test_association(2, 0); - let mut associations = HashMap::from([(source, replacement)]); - - assert!(remove_completed_association(&mut associations, source, 1).is_none()); - assert_eq!(associations.get(&source).unwrap().generation, 2); - assert!(remove_completed_association(&mut associations, source, 2).is_some()); - assert!(associations.is_empty()); - } - - #[test] - fn association_activity_clock_refreshes_only_successfully_queued_work() { - let tick = Arc::new(AtomicU64::new(100)); - let clock = AssociationClock::injected(tick.clone()); - let (association, mut receiver, _) = test_association(1, 5); - let stats = RuntimeResourceStats::new("tun_runtime_test"); - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - - assert!(matches!( - try_queue_association_input( - &association, - UdpDatagram::new(source, destination, b"queued".as_slice()), - clock.now(), - 1, - &stats, - ), - AssociationInputResult::Queued - )); - assert_eq!(association.last_activity.load(Ordering::Acquire), 100); - - tick.store(110, Ordering::Release); - assert!(matches!( - try_queue_association_input( - &association, - UdpDatagram::new(source, destination, b"full".as_slice()), - clock.now(), - 1, - &stats, - ), - AssociationInputResult::Full - )); - assert_eq!(association.last_activity.load(Ordering::Acquire), 100); - assert_eq!(stats.snapshot().udp_queue_drops, 1); - - receiver.try_recv().unwrap(); - drop(receiver); - tick.store(120, Ordering::Release); - assert!(matches!( - try_queue_association_input( - &association, - UdpDatagram::new(source, destination, b"closed".as_slice()), - clock.now(), - 1, - &stats, - ), - AssociationInputResult::Closed - )); - assert_eq!(association.last_activity.load(Ordering::Acquire), 100); - } - - #[test] - fn periodic_cleanup_removes_expired_and_closed_but_preserves_active_entries() { - let now = 100; - let expired_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let closed_source: SocketAddr = "192.0.2.11:12001".parse().unwrap(); - let active_source: SocketAddr = "192.0.2.12:12002".parse().unwrap(); - let (expired, _expired_receiver, expired_cancellation) = test_association(1, 70); - let (closed, closed_receiver, closed_cancellation) = test_association(2, 99); - let (active, _active_receiver, _) = test_association(3, 80); - drop(closed_receiver); - let mut associations = HashMap::from([ - (expired_source, expired), - (closed_source, closed), - (active_source, active), - ]); - - let removed = take_expired_or_closed_associations(&mut associations, now); - assert_eq!(removed.len(), 2); - assert!(associations.contains_key(&active_source)); - assert!(!expired_cancellation.is_cancelled()); - assert!(!closed_cancellation.is_cancelled()); - cancel_removed_associations(removed); - assert!(expired_cancellation.is_cancelled()); - assert!(closed_cancellation.is_cancelled()); - } - - #[derive(Default)] - struct MockDispatcher { - tcp_sessions: Mutex>, - udp_sessions: Mutex>, - tcp_called: Notify, - } - - #[async_trait] - impl Dispatcher for MockDispatcher { - async fn connect_tcp(&self, session: StreamSession) -> Result { - self.tcp_sessions.lock().unwrap().push(session); - self.tcp_called.notify_one(); - let (client, mut server) = tokio::io::duplex(1_024); - tokio::spawn(async move { - let mut buffer = [0_u8; 256]; - while let Ok(size) = server.read(&mut buffer).await { - if size == 0 || server.write_all(&buffer[..size]).await.is_err() { - break; - } - } - }); - Ok(Box::new(client)) - } - - async fn open_datagram( - &self, - session: DatagramSession, - ) -> Result, DispatchError> { - self.udp_sessions.lock().unwrap().push(session); - Ok(Box::new(EchoDatagrams::new())) - } - } - - struct EchoDatagrams { - sender: mpsc::Sender, - receiver: mpsc::Receiver, - } - - struct BlockingDispatcher { - send_started: Arc, - send_count: Arc, - open_count: Arc, - } - - struct RecordingUdpDispatcher { - datagrams: Arc>>, - sent: Arc, - } - - struct RecordingUdpTransport { - datagrams: Arc>>, - sent: Arc, - } - - struct ReplayFairDispatcher { - response: Datagram, - send_count: Arc, - blocked_send_started: Arc, - } - - struct ReplayFairDatagrams { - response: Option, - send_count: Arc, - blocked_send_started: Arc, - } - - #[async_trait] - impl Dispatcher for RecordingUdpDispatcher { - async fn connect_tcp(&self, _session: StreamSession) -> Result { - Err(DispatchError::Other("unused TCP path".to_owned())) - } - - async fn open_datagram( - &self, - _session: DatagramSession, - ) -> Result, DispatchError> { - Ok(Box::new(RecordingUdpTransport { - datagrams: self.datagrams.clone(), - sent: self.sent.clone(), - })) - } - } - - #[async_trait] - impl DatagramTransport for RecordingUdpTransport { - async fn send(&mut self, datagram: Datagram) -> Result<(), DispatchError> { - self.datagrams.lock().unwrap().push(datagram); - self.sent.notify_one(); - Ok(()) - } - - async fn receive(&mut self) -> Result { - std::future::pending().await - } - } - - #[async_trait] - impl Dispatcher for ReplayFairDispatcher { - async fn connect_tcp(&self, _session: StreamSession) -> Result { - Err(DispatchError::Other("unused TCP path".to_owned())) - } - - async fn open_datagram( - &self, - _session: DatagramSession, - ) -> Result, DispatchError> { - Ok(Box::new(ReplayFairDatagrams { - response: Some(self.response.clone()), - send_count: self.send_count.clone(), - blocked_send_started: self.blocked_send_started.clone(), - })) - } - } - - #[async_trait] - impl DatagramTransport for ReplayFairDatagrams { - async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { - let send_index = self.send_count.fetch_add(1, Ordering::Relaxed); - if send_index == 0 { - return Ok(()); - } - self.blocked_send_started.notify_one(); - std::future::pending().await - } - - async fn receive(&mut self) -> Result { - if self.send_count.load(Ordering::Relaxed) != 0 - && let Some(response) = self.response.take() - { - return Ok(response); - } - std::future::pending().await - } - } - - #[async_trait] - impl Dispatcher for BlockingDispatcher { - async fn connect_tcp(&self, _session: StreamSession) -> Result { - Err(DispatchError::Other("unused TCP path".to_owned())) - } - - async fn open_datagram( - &self, - _session: DatagramSession, - ) -> Result, DispatchError> { - self.open_count.fetch_add(1, Ordering::Relaxed); - Ok(Box::new(BlockingDatagrams { - send_started: self.send_started.clone(), - send_count: Some(self.send_count.clone()), - })) - } - } - - struct BlockingDatagrams { - send_started: Arc, - send_count: Option>, - } - - #[derive(Default)] - struct DnsReplyDispatcher { - udp_sessions: Mutex>, - } - - #[async_trait] - impl Dispatcher for DnsReplyDispatcher { - async fn connect_tcp(&self, _session: StreamSession) -> Result { - Err(DispatchError::Other("unused TCP path".to_owned())) - } - - async fn open_datagram( - &self, - session: DatagramSession, - ) -> Result, DispatchError> { - self.udp_sessions.lock().unwrap().push(session); - Ok(Box::new(DnsReplyDatagrams::new())) - } - } - - struct DnsReplyDatagrams { - sender: mpsc::Sender, - receiver: mpsc::Receiver, - } - - impl DnsReplyDatagrams { - fn new() -> Self { - let (sender, receiver) = mpsc::channel(1); - Self { sender, receiver } - } - } - - #[async_trait] - impl DatagramTransport for DnsReplyDatagrams { - async fn send(&mut self, mut datagram: Datagram) -> Result<(), DispatchError> { - let query = classify_query(&datagram.payload) - .map_err(|error| DispatchError::Other(error.to_string()))?; - datagram.payload = synthesize_empty_response(&query, 0) - .map_err(|error| DispatchError::Other(error.to_string()))? - .into(); - self.sender - .send(datagram) - .await - .map_err(|_| DispatchError::Other("DNS reply transport stopped".to_owned())) - } - - async fn receive(&mut self) -> Result { - self.receiver - .recv() - .await - .ok_or_else(|| DispatchError::Other("DNS reply transport stopped".to_owned())) - } - } - - struct ResponseFirstDispatcher { - response: Datagram, - } - - struct SourceSelectiveDispatcher { - blocked_source: SocketAddr, - send_started: Arc, - } - - #[async_trait] - impl Dispatcher for ResponseFirstDispatcher { - async fn connect_tcp(&self, _session: StreamSession) -> Result { - Err(DispatchError::Other("unused TCP path".to_owned())) - } - - async fn open_datagram( - &self, - _session: DatagramSession, - ) -> Result, DispatchError> { - Ok(Box::new(ResponseFirstDatagrams { - response: Some(self.response.clone()), - })) - } - } - - #[async_trait] - impl Dispatcher for SourceSelectiveDispatcher { - async fn connect_tcp(&self, _session: StreamSession) -> Result { - Err(DispatchError::Other("unused TCP path".to_owned())) - } - - async fn open_datagram( - &self, - session: DatagramSession, - ) -> Result, DispatchError> { - if session.source == self.blocked_source { - Ok(Box::new(BlockingDatagrams { - send_started: self.send_started.clone(), - send_count: None, - })) - } else { - Ok(Box::new(EchoDatagrams::new())) - } - } - } - - struct ResponseFirstDatagrams { - response: Option, - } - - #[async_trait] - impl DatagramTransport for ResponseFirstDatagrams { - async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { - std::future::pending().await - } - - async fn receive(&mut self) -> Result { - if let Some(response) = self.response.take() { - return Ok(response); - } - std::future::pending().await - } - } - - #[async_trait] - impl DatagramTransport for BlockingDatagrams { - async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { - if let Some(send_count) = &self.send_count { - send_count.fetch_add(1, Ordering::Relaxed); - } - self.send_started.notify_one(); - std::future::pending().await - } - - async fn receive(&mut self) -> Result { - std::future::pending().await - } - } - - impl EchoDatagrams { - fn new() -> Self { - let (sender, receiver) = mpsc::channel(1); - Self { sender, receiver } - } - } - - #[async_trait] - impl DatagramTransport for EchoDatagrams { - async fn send(&mut self, mut datagram: Datagram) -> Result<(), DispatchError> { - if datagram.payload.as_ref() == b"oversize-response" { - datagram.payload = Bytes::from(vec![0_u8; TUN_MTU]); - } - self.sender - .send(datagram) - .await - .map_err(|_| DispatchError::Other("echo transport stopped".to_owned())) - } - - async fn receive(&mut self) -> Result { - self.receiver - .recv() - .await - .ok_or_else(|| DispatchError::Other("echo transport stopped".to_owned())) - } - } - - fn test_runtime_dns(dispatcher: Arc) -> Arc { - let config = DnsConfig { - enable: true, - ipv6: true, - nameservers: vec![DnsNameserver { - transport: DnsTransport::Udp, - address: Ipv4Addr::new(198, 51, 100, 53).into(), - port: 53, - route: DnsRoute::Direct, - }], - nameserver_policies: Vec::new(), - }; - let proxies = ProxyDispatchers::new(vec![dispatcher.clone()]).unwrap(); - let rules = RuleSet::compile(vec![RuleSpec { - kind: RuleKind::Match, - action: RuleAction::Direct, - no_resolve: false, - }]) - .unwrap(); - let limits = ResourceLimits::default(); - Arc::new(RuntimeDns::new_routed_proxies_with_cache_limits( - &config, - proxies, - dispatcher, - rules, - Arc::new(EmptyGeoMatcher), - limits.dns_address_cache_entries, - limits.dns_redir_host_entries, - )) - } - - #[tokio::test] - async fn synthetic_fd_with_dns_disabled_dispatches_tcp_and_reuses_udp_association() { - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let dispatcher = Arc::new(MockDispatcher::default()); - let limits = ResourceLimits { - packet_queue_capacity: 8, - event_queue_capacity: 4, - tun_max_datagram_size: 1_400, - ..ResourceLimits::default() - }; - let runtime = TunRuntime::new( - tun, - limits, - dispatcher.clone(), - None, - true, - false, - Some(test_sniffer( - &[PortRange { - start: 8_080, - end: 8_080, - }], - &[], - &[], - )), - ) - .unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - - let udp_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let udp_destination: SocketAddr = "198.51.100.20:53".parse().unwrap(); - - let mut oversized_ingress = vec![0_u8; TUN_MTU + 1]; - oversized_ingress[0] = 0x45; - peer.send(&oversized_ingress).await.unwrap(); - - peer.send(&build_udp( - udp_source, - udp_destination, - b"oversize-response", - )) - .await - .unwrap(); - let mut dropped = [0_u8; TUN_MTU]; - assert!( - timeout(Duration::from_millis(100), peer.recv(&mut dropped)) - .await - .is_err(), - "oversized UDP response unexpectedly reached TUN", - ); - - for destination in ["198.51.100.20:53", "203.0.113.30:443"] { - let destination: SocketAddr = destination.parse().unwrap(); - peer.send(&build_udp(udp_source, destination, b"query")) - .await - .unwrap(); - let mut response = [0_u8; TUN_MTU]; - let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) - .await - .expect("UDP response timed out") - .unwrap(); - assert_udp_response(&response[..size], destination, udp_source, b"query"); - } - assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); - assert_eq!( - dispatcher.udp_sessions.lock().unwrap()[0], - DatagramSession::for_tun(udp_source, 1_400) - ); - - let tcp_source: SocketAddr = "192.0.2.11:13000".parse().unwrap(); - let tcp_destination: SocketAddr = "198.51.100.21:8080".parse().unwrap(); - peer.send(&build_tcp_syn(tcp_source, tcp_destination)) - .await - .unwrap(); - let mut syn_ack = [0_u8; TUN_MTU]; - let size = timeout(Duration::from_secs(2), peer.recv(&mut syn_ack)) - .await - .expect("TCP SYN-ACK timed out") - .unwrap(); - assert!(size >= 40); - assert_eq!(syn_ack[20 + 13] & 0x12, 0x12); - let server_sequence = u32::from_be_bytes(syn_ack[24..28].try_into().unwrap()); - let request = b"GET / HTTP/1.1\r\nHost: Sniff.Example.COM\r\n\r\n"; - peer.send(&build_tcp_segment( - tcp_source, - tcp_destination, - 2, - server_sequence.wrapping_add(1), - 0x18, - request, - )) - .await - .unwrap(); - timeout(Duration::from_secs(2), async { - loop { - if !dispatcher.tcp_sessions.lock().unwrap().is_empty() { - break; - } - dispatcher.tcp_called.notified().await; - } - }) - .await - .expect("TCP dispatcher was not called"); - assert_eq!( - dispatcher.tcp_sessions.lock().unwrap()[0], - StreamSession { - inbound: InboundKind::Tun, - source: tcp_source, - destination: Destination::Ip(tcp_destination), - sniffed_domain: Some("sniff.example.com".to_owned()), - } - ); - let echoed = timeout(Duration::from_secs(2), async { - let mut echoed = Vec::with_capacity(request.len()); - while echoed.len() < request.len() { - let size = peer.recv(&mut syn_ack).await.unwrap(); - if size < 40 || syn_ack[9] != 6 { - continue; - } - let ip_header_length = usize::from(syn_ack[0] & 0x0f) * 4; - let tcp_header_length = usize::from(syn_ack[ip_header_length + 12] >> 4) * 4; - let payload_offset = ip_header_length + tcp_header_length; - if payload_offset < size { - echoed.extend_from_slice(&syn_ack[payload_offset..size]); - } - } - echoed - }) - .await - .expect("sniffed TCP prefix was not replayed through the outbound"); - assert_eq!(echoed, request); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("TUN runtime stop timed out") - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn unconfigured_tls_port_dispatches_without_reading_a_prefix() { - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let dispatcher = Arc::new(MockDispatcher::default()); - let limits = ResourceLimits { - packet_queue_capacity: 4, - event_queue_capacity: 2, - tun_max_datagram_size: TUN_MTU, - ..ResourceLimits::default() - }; - let runtime = TunRuntime::new( - tun, - limits, - dispatcher.clone(), - None, - true, - false, - Some(test_sniffer( - &[], - &[PortRange { - start: 8_443, - end: 8_443, - }], - &[], - )), - ) - .unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - - let source: SocketAddr = "192.0.2.12:14000".parse().unwrap(); - let destination: SocketAddr = "198.51.100.22:443".parse().unwrap(); - peer.send(&build_tcp_syn(source, destination)) - .await - .unwrap(); - let mut syn_ack = [0_u8; TUN_MTU]; - let size = timeout(Duration::from_secs(2), peer.recv(&mut syn_ack)) - .await - .expect("TCP SYN-ACK timed out") - .unwrap(); - assert!(size >= 40); - assert_eq!(syn_ack[20 + 13] & 0x12, 0x12); - let server_sequence = u32::from_be_bytes(syn_ack[24..28].try_into().unwrap()); - - // Complete the handshake without sending any TLS bytes. A mistakenly - // enabled sniffer would wait for its 200 ms read deadline here. - peer.send(&build_tcp_segment( - source, - destination, - 2, - server_sequence.wrapping_add(1), - 0x10, - &[], - )) - .await - .unwrap(); - timeout(Duration::from_millis(100), async { - loop { - if !dispatcher.tcp_sessions.lock().unwrap().is_empty() { - break; - } - dispatcher.tcp_called.notified().await; - } - }) - .await - .expect("unconfigured TLS port delayed dispatch for a prefix read"); - assert_eq!( - dispatcher.tcp_sessions.lock().unwrap()[0], - StreamSession { - inbound: InboundKind::Tun, - source, - destination: Destination::Ip(destination), - sniffed_domain: None, - } - ); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("TUN runtime stop timed out") - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn enabled_dns_fast_path_replies_without_opening_a_tun_udp_association() { - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let dispatcher = Arc::new(MockDispatcher::default()); - let dns_dispatcher = Arc::new(DnsReplyDispatcher::default()); - let dns = test_runtime_dns(dns_dispatcher.clone()); - let limits = ResourceLimits { - packet_queue_capacity: 16, - event_queue_capacity: 8, - tun_max_datagram_size: TUN_MTU, - ..ResourceLimits::default() - }; - let runtime = TunRuntime::new( - tun, - limits, - dispatcher.clone(), - Some(dns), - true, - false, - Some(test_sniffer(&[], &[], &[PortRange { start: 53, end: 53 }])), - ) - .unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); - peer.send(&build_udp(source, requested_server, b"invalid")) - .await - .unwrap(); - let mut response = [0_u8; TUN_MTU]; - assert!( - timeout(Duration::from_millis(100), peer.recv(&mut response)) - .await - .is_err(), - "malformed DNS query unexpectedly produced a response", - ); - assert!(dispatcher.udp_sessions.lock().unwrap().is_empty()); - assert!(dns_dispatcher.udp_sessions.lock().unwrap().is_empty()); - - let query = build_query(0x1234, "example.com", QueryType::A).unwrap(); - peer.send(&build_udp(source, requested_server, &query)) - .await - .unwrap(); - let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) - .await - .expect("TUN DNS response timed out") - .unwrap(); - assert!(size > 30); - assert_eq!(response[9], 17); - assert_eq!( - u16::from_be_bytes(response[20..22].try_into().unwrap()), - requested_server.port() - ); - assert_eq!( - u16::from_be_bytes(response[22..24].try_into().unwrap()), - source.port() - ); - assert_ne!(response[30] & 0x80, 0); - assert_eq!(&response[28..30], &0x1234_u16.to_be_bytes()); - assert!(dispatcher.udp_sessions.lock().unwrap().is_empty()); - assert_eq!(dns_dispatcher.udp_sessions.lock().unwrap().len(), 1); - assert_eq!( - dns_dispatcher.udp_sessions.lock().unwrap()[0].inbound, - InboundKind::InternalDns - ); - - let ordinary_destination: SocketAddr = "203.0.113.30:443".parse().unwrap(); - peer.send(&build_udp(source, ordinary_destination, b"ordinary")) - .await - .unwrap(); - let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) - .await - .expect("ordinary UDP response timed out") - .unwrap(); - assert_udp_response(&response[..size], ordinary_destination, source, b"ordinary"); - assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("TUN runtime stop timed out") - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn queued_dns_response_is_bounded_and_a_full_queue_drops_the_new_response() { - let dns = test_runtime_dns(Arc::new(DnsReplyDispatcher::default())); - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); - let request = UdpDatagram::new( - source, - requested_server, - build_query(0x2100, "queued.example", QueryType::A).unwrap(), - ); - let (responses, mut queued) = mpsc::channel(1); - let resource_stats = RuntimeResourceStats::new("tun_runtime_test"); - - let permit = dns.begin_query(); - run_tun_dns_query( - dns.clone(), - permit, - request, - responses.clone(), - TUN_MTU, - resource_stats.clone(), - CancellationToken::new(), - ) - .await; - let response = queued.recv().await.unwrap(); - assert_eq!(response.source, requested_server); - assert_eq!(response.destination, source); - drop(response); - - responses - .send(QueuedUdpResponse::ordinary(UdpDatagram::new( - requested_server, - source, - b"occupied".as_slice(), - ))) - .await - .unwrap(); - let request = UdpDatagram::new( - source, - requested_server, - build_query(0x2101, "full.example", QueryType::A).unwrap(), - ); - let permit = dns.begin_query(); - run_tun_dns_query( - dns.clone(), - permit, - request, - responses, - TUN_MTU, - resource_stats.clone(), - CancellationToken::new(), - ) - .await; - assert_eq!(resource_stats.snapshot().dns_queue_drops, 1); - assert_eq!(&queued.recv().await.unwrap().payload[..], b"occupied"); - } - - #[tokio::test] - async fn ordinary_and_dns_responses_have_independent_queue_capacity() { - let dns = test_runtime_dns(Arc::new(DnsReplyDispatcher::default())); - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let server: SocketAddr = "198.51.100.20:53".parse().unwrap(); - let stats = RuntimeResourceStats::new("tun_runtime_test"); - let (ordinary_tx, mut ordinary_rx) = mpsc::channel(1); - let (dns_tx, mut dns_rx) = mpsc::channel(1); - - ordinary_tx - .send(QueuedUdpResponse::ordinary(UdpDatagram::new( - server, - source, - b"ordinary-occupied".as_slice(), - ))) - .await - .unwrap(); - let permit = dns.begin_query(); - try_queue_tun_dns_response( - &dns_tx, - UdpDatagram::new(server, source, b"dns-independent".as_slice()), - Some(permit), - &stats, - ); - - let dns_response = dns_rx.recv().await.unwrap(); - assert_eq!(&dns_response.payload[..], b"dns-independent"); - drop(dns_response); - assert_eq!( - &ordinary_rx.recv().await.unwrap().payload[..], - b"ordinary-occupied" - ); - } - - #[tokio::test] - async fn full_ordinary_response_queue_drops_without_refreshing_activity() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let server: SocketAddr = "198.51.100.20:443".parse().unwrap(); - let stats = RuntimeResourceStats::new("tun_runtime_test"); - let last_activity = AtomicU64::new(5); - let (responses, mut queued) = mpsc::channel(1); - - assert!(matches!( - try_queue_tun_udp_response( - &responses, - UdpDatagram::new(server, source, b"queued".as_slice()), - &last_activity, - 100, - &stats, - ), - ResponseQueueResult::Queued - )); - assert_eq!(last_activity.load(Ordering::Acquire), 100); - assert!(matches!( - try_queue_tun_udp_response( - &responses, - UdpDatagram::new(server, source, b"dropped".as_slice()), - &last_activity, - 110, - &stats, - ), - ResponseQueueResult::Dropped - )); - assert_eq!(last_activity.load(Ordering::Acquire), 100); - assert_eq!(stats.snapshot().udp_queue_drops, 1); - assert_eq!(&queued.recv().await.unwrap().payload[..], b"queued"); - } - - #[tokio::test] - async fn dns_fast_path_allows_sixteen_concurrent_sources_and_keeps_udp_responsive() { - const QUERY_COUNT: usize = 16; - - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let dispatcher = Arc::new(MockDispatcher::default()); - let send_started = Arc::new(Notify::new()); - let send_count = Arc::new(AtomicUsize::new(0)); - let open_count = Arc::new(AtomicUsize::new(0)); - let dns_dispatcher = Arc::new(BlockingDispatcher { - send_started, - send_count: send_count.clone(), - open_count: open_count.clone(), - }); - let dns = test_runtime_dns(dns_dispatcher); - let limits = ResourceLimits { - packet_queue_capacity: 64, - event_queue_capacity: 32, - tun_max_datagram_size: TUN_MTU, - ..ResourceLimits::default() - }; - let runtime = TunRuntime::new( - tun, - limits, - dispatcher.clone(), - Some(dns), - true, - false, - None, - ) - .unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); - - let malformed_source: SocketAddr = "192.0.2.10:11999".parse().unwrap(); - peer.send(&build_udp(malformed_source, requested_server, b"invalid")) - .await - .unwrap(); - - for index in 0..QUERY_COUNT { - let source = SocketAddr::new( - Ipv4Addr::new(192, 0, 2, 10).into(), - 12_000 + u16::try_from(index).unwrap(), - ); - let domain = format!("stall-{index}.example"); - let query = build_query( - 0x1000 + u16::try_from(index).unwrap(), - &domain, - QueryType::A, - ) - .unwrap(); - peer.send(&build_udp(source, requested_server, &query)) - .await - .unwrap(); - } - timeout(Duration::from_secs(2), async { - while send_count.load(Ordering::Relaxed) < QUERY_COUNT { - tokio::task::yield_now().await; - } - }) - .await - .expect("the DNS queries did not reach their stalled upstreams"); - assert!(dispatcher.udp_sessions.lock().unwrap().is_empty()); - assert_eq!(open_count.load(Ordering::Relaxed), QUERY_COUNT); - - let ordinary_source: SocketAddr = "192.0.2.99:13000".parse().unwrap(); - let ordinary_destination: SocketAddr = "203.0.113.30:443".parse().unwrap(); - peer.send(&build_udp( - ordinary_source, - ordinary_destination, - b"still-responsive", - )) - .await - .unwrap(); - let mut response = [0_u8; TUN_MTU]; - let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) - .await - .expect("stalled DNS queries blocked ordinary UDP") - .unwrap(); - assert_udp_response( - &response[..size], - ordinary_destination, - ordinary_source, - b"still-responsive", - ); - - assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); - assert_eq!(open_count.load(Ordering::Relaxed), QUERY_COUNT); - assert_eq!(send_count.load(Ordering::Relaxed), QUERY_COUNT); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("stalled DNS tasks prevented the TUN stop barrier") - .unwrap() - .unwrap(); - } - - #[test] - fn oversized_tun_dns_response_is_dropped() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); - let query = build_query(0x3456, "example.com", QueryType::A).unwrap(); - let request = UdpDatagram::new(source, requested_server, query.clone()); - let tun_mtu = 1_400; - let ceiling = - usize::from(DatagramSession::for_tun(source, tun_mtu).max_response_payload_size()); - assert!(complete_tun_dns_response(&request, vec![0_u8; ceiling], tun_mtu).is_some()); - assert!(complete_tun_dns_response(&request, vec![0_u8; ceiling + 1], tun_mtu).is_none()); - } - - #[test] - fn ipv6_tun_dns_responses_preserve_the_requested_server_and_client_endpoints() { - let source: SocketAddr = "[2001:db8::10]:12000".parse().unwrap(); - let requested_server: SocketAddr = "[2001:db8::53]:53".parse().unwrap(); - let query = build_query(0x4567, "example.com", QueryType::Aaaa).unwrap(); - let classified = classify_query(&query).unwrap(); - let request = UdpDatagram::new(source, requested_server, query); - let response = complete_tun_dns_response( - &request, - synthesize_empty_response(&classified, 0).unwrap(), - TUN_MTU, - ) - .unwrap(); - assert_eq!(response.source, requested_server); - assert_eq!(response.destination, source); - let parsed = crate::dns::parse_response(&response.payload).unwrap(); - assert_eq!(parsed.id, classified.id); - - let servfail = tun_dns_servfail_response(&request, &classified); - assert_eq!(servfail.source, requested_server); - assert_eq!(servfail.destination, source); - assert_eq!( - u16::from_be_bytes([servfail.payload[2], servfail.payload[3]]) & 0x000f, - 2 - ); - } - - #[tokio::test] - async fn cancellation_interrupts_a_blocked_udp_send_and_completes_the_stop_barrier() { - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let send_started = Arc::new(Notify::new()); - let dispatcher = Arc::new(BlockingDispatcher { - send_started: send_started.clone(), - send_count: Arc::new(AtomicUsize::new(0)), - open_count: Arc::new(AtomicUsize::new(0)), - }); - let runtime = TunRuntime::new( - tun, - ResourceLimits::default(), - dispatcher, - None, - true, - false, - None, - ) - .unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - - peer.send(&build_udp( - "192.0.2.10:12000".parse().unwrap(), - "198.51.100.20:53".parse().unwrap(), - b"block", - )) - .await - .unwrap(); - timeout(Duration::from_secs(2), send_started.notified()) - .await - .expect("UDP transport send was not polled"); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("blocked UDP send prevented the TUN stop barrier") - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn fresh_udp_source_beyond_the_old_sixty_four_limit_is_accepted() { - const OLD_UDP_LIMIT: usize = 64; - - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let send_started = Arc::new(Notify::new()); - let send_count = Arc::new(AtomicUsize::new(0)); - let open_count = Arc::new(AtomicUsize::new(0)); - let dispatcher = Arc::new(BlockingDispatcher { - send_started, - send_count: send_count.clone(), - open_count: open_count.clone(), - }); - let limits = ResourceLimits { - packet_queue_capacity: 128, - event_queue_capacity: 128, - tun_max_datagram_size: TUN_MTU, - ..ResourceLimits::default() - }; - let runtime = TunRuntime::new(tun, limits, dispatcher, None, true, false, None).unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); - - for index in 0..OLD_UDP_LIMIT { - let source = SocketAddr::new( - Ipv4Addr::new(192, 0, 2, 10).into(), - 12_000 + u16::try_from(index).unwrap(), - ); - peer.send(&build_udp(source, destination, b"held")) - .await - .unwrap(); - timeout(Duration::from_secs(2), async { - while open_count.load(Ordering::Acquire) <= index { - tokio::task::yield_now().await; - } - }) - .await - .expect("a UDP association was not opened"); - } - assert_eq!(send_count.load(Ordering::Acquire), OLD_UDP_LIMIT); - assert_eq!(open_count.load(Ordering::Acquire), OLD_UDP_LIMIT); - - let sixty_fifth_source: SocketAddr = "192.0.2.99:13000".parse().unwrap(); - peer.send(&build_udp(sixty_fifth_source, destination, b"accepted")) - .await - .unwrap(); - timeout(Duration::from_secs(2), async { - while open_count.load(Ordering::Acquire) == OLD_UDP_LIMIT { - tokio::task::yield_now().await; - } - }) - .await - .expect("the sixty-fifth fresh UDP source was not accepted"); - assert_eq!(open_count.load(Ordering::Acquire), OLD_UDP_LIMIT + 1); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("TUN runtime stop timed out") - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn queued_udp_response_is_drained_before_a_same_source_ingress_burst() { - let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let server: SocketAddr = "198.51.100.20:53".parse().unwrap(); - let dispatcher = Arc::new(ResponseFirstDispatcher { - response: Datagram { - remote: Destination::Ip(server), - payload: Bytes::from_static(b"response"), - sniffed_domain: None, - }, - }); - let (inbound_tx, mut inbound_rx) = mpsc::channel(1); - inbound_tx - .send(UdpDatagram::new(source, server, b"next-query".as_slice())) - .await - .unwrap(); - let (responses_tx, mut responses_rx) = mpsc::channel(1); - let cancellation = CancellationToken::new(); - let child = cancellation.clone(); - let task = tokio::spawn(async move { - run_udp_association_inner( - &mut inbound_rx, - &UdpAssociationTaskContext { - association_id: 1, - source, - responses: responses_tx, - dispatcher, - resource_stats: RuntimeResourceStats::new("tun_runtime_test"), - association_clock: AssociationClock::realtime(), - last_activity: Arc::new(AtomicU64::new(0)), - tun_mtu: TUN_MTU, - sniffer: None, - cancellation: child, - }, - ) - .await - }); - - let response = timeout(Duration::from_secs(1), responses_rx.recv()) - .await - .expect("ready response was starved behind the same-source query") - .expect("response channel closed"); - assert_eq!(response.source, server); - assert_eq!(response.destination, source); - assert_eq!(&response.payload[..], b"response"); - - cancellation.cancel(); - timeout(Duration::from_secs(1), task) - .await - .expect("association did not stop") - .unwrap() - .unwrap(); - drop(inbound_tx); - } - - #[tokio::test] - async fn full_udp_association_queue_does_not_block_other_sources() { - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_nonblocking(true).unwrap(); - let fd = TunFd::duplicate(host.as_raw_fd()).unwrap(); - let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); - let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); - let blocked_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); - let responsive_source: SocketAddr = "192.0.2.11:12001".parse().unwrap(); - let destination: SocketAddr = "198.51.100.20:53".parse().unwrap(); - let send_started = Arc::new(Notify::new()); - let dispatcher = Arc::new(SourceSelectiveDispatcher { - blocked_source, - send_started: send_started.clone(), - }); - let limits = ResourceLimits { - packet_queue_capacity: 64, - event_queue_capacity: 64, - tun_max_datagram_size: TUN_MTU, - ..ResourceLimits::default() - }; - let runtime = TunRuntime::new(tun, limits, dispatcher, None, true, false, None).unwrap(); - let cancellation = CancellationToken::new(); - let task = tokio::spawn(runtime.run(cancellation.clone())); - - peer.send(&build_udp(blocked_source, destination, b"block")) - .await - .unwrap(); - timeout(Duration::from_secs(2), send_started.notified()) - .await - .expect("blocked association did not enter outbound send"); - - for _ in 0..=UDP_ASSOCIATION_QUEUE_MAX { - peer.send(&build_udp(blocked_source, destination, b"queued")) - .await - .unwrap(); - tokio::task::yield_now().await; - } - peer.send(&build_udp( - responsive_source, - destination, - b"still-responsive", - )) - .await - .unwrap(); - - let mut response = [0_u8; TUN_MTU]; - let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) - .await - .expect("a full source queue blocked another UDP association") - .unwrap(); - assert_udp_response( - &response[..size], - destination, - responsive_source, - b"still-responsive", - ); - - cancellation.cancel(); - timeout(Duration::from_secs(2), task) - .await - .expect("TUN runtime stop timed out") - .unwrap() - .unwrap(); - } - - fn build_udp(source: SocketAddr, destination: SocketAddr, payload: &[u8]) -> Vec { - let (SocketAddr::V4(source), SocketAddr::V4(destination)) = (source, destination) else { - panic!("test helper requires IPv4"); - }; - let mut udp = vec![0_u8; 8 + payload.len()]; - udp[..2].copy_from_slice(&source.port().to_be_bytes()); - udp[2..4].copy_from_slice(&destination.port().to_be_bytes()); - let udp_len = u16::try_from(udp.len()).unwrap(); - udp[4..6].copy_from_slice(&udp_len.to_be_bytes()); - udp[8..].copy_from_slice(payload); - let checksum = transport_checksum(*source.ip(), *destination.ip(), 17, &udp); - udp[6..8].copy_from_slice(&checksum.to_be_bytes()); - build_ipv4(*source.ip(), *destination.ip(), 17, &udp) - } - - fn build_tcp_syn(source: SocketAddr, destination: SocketAddr) -> Vec { - build_tcp_segment(source, destination, 1, 0, 0x02, &[]) - } - - fn build_tcp_segment( - source: SocketAddr, - destination: SocketAddr, - sequence: u32, - acknowledgement: u32, - flags: u8, - payload: &[u8], - ) -> Vec { - let (SocketAddr::V4(source), SocketAddr::V4(destination)) = (source, destination) else { - panic!("test helper requires IPv4"); - }; - let mut tcp = vec![0_u8; 20 + payload.len()]; - tcp[..2].copy_from_slice(&source.port().to_be_bytes()); - tcp[2..4].copy_from_slice(&destination.port().to_be_bytes()); - tcp[4..8].copy_from_slice(&sequence.to_be_bytes()); - tcp[8..12].copy_from_slice(&acknowledgement.to_be_bytes()); - tcp[12] = 5 << 4; - tcp[13] = flags; - tcp[14..16].copy_from_slice(&u16::MAX.to_be_bytes()); - tcp[20..].copy_from_slice(payload); - let checksum = transport_checksum(*source.ip(), *destination.ip(), 6, &tcp); - tcp[16..18].copy_from_slice(&checksum.to_be_bytes()); - build_ipv4(*source.ip(), *destination.ip(), 6, &tcp) - } - - fn build_ipv4( - source: Ipv4Addr, - destination: Ipv4Addr, - protocol: u8, - transport: &[u8], - ) -> Vec { - let mut packet = vec![0_u8; 20 + transport.len()]; - let packet_len = u16::try_from(packet.len()).unwrap(); - packet[0] = 0x45; - packet[2..4].copy_from_slice(&packet_len.to_be_bytes()); - packet[6..8].copy_from_slice(&0x4000_u16.to_be_bytes()); - packet[8] = 64; - packet[9] = protocol; - packet[12..16].copy_from_slice(&source.octets()); - packet[16..20].copy_from_slice(&destination.octets()); - let checksum = checksum(&packet[..20]); - packet[10..12].copy_from_slice(&checksum.to_be_bytes()); - packet[20..].copy_from_slice(transport); - packet - } - - fn assert_udp_response( - packet: &[u8], - source: SocketAddr, - destination: SocketAddr, - payload: &[u8], - ) { - assert_eq!(packet[9], 17); - assert_eq!( - u16::from_be_bytes(packet[20..22].try_into().unwrap()), - source.port() - ); - assert_eq!( - u16::from_be_bytes(packet[22..24].try_into().unwrap()), - destination.port() - ); - assert_eq!(&packet[28..], payload); - } - - fn transport_checksum( - source: Ipv4Addr, - destination: Ipv4Addr, - protocol: u8, - transport: &[u8], - ) -> u16 { - let mut sum = 0_u32; - add_bytes(&mut sum, &source.octets()); - add_bytes(&mut sum, &destination.octets()); - sum += u32::from(protocol); - sum += u32::try_from(transport.len()).unwrap(); - add_bytes(&mut sum, transport); - fold(sum) - } - - fn checksum(bytes: &[u8]) -> u16 { - let mut sum = 0_u32; - add_bytes(&mut sum, bytes); - fold(sum) - } - - fn add_bytes(sum: &mut u32, bytes: &[u8]) { - let (chunks, remainder) = bytes.as_chunks::<2>(); - for chunk in chunks { - *sum += u32::from(u16::from_be_bytes(*chunk)); - } - if let Some(byte) = remainder.first() { - *sum += u32::from(*byte) << 8; - } - } - - fn fold(mut sum: u32) -> u16 { - while sum >> 16 != 0 { - sum = (sum & 0xffff) + (sum >> 16); - } - let checksum = !u16::try_from(sum).unwrap(); - if checksum == 0 { u16::MAX } else { checksum } - } -} +mod tests; diff --git a/src/tun_runtime/tests.rs b/src/tun_runtime/tests.rs new file mode 100644 index 0000000..1f60c3f --- /dev/null +++ b/src/tun_runtime/tests.rs @@ -0,0 +1,3777 @@ +use std::{ + future::Future as _, + net::{Ipv4Addr, SocketAddr}, + os::fd::AsRawFd, + os::unix::net::UnixDatagram, + sync::{ + Mutex, + atomic::{AtomicUsize, Ordering}, + }, + task::Poll, +}; + +use async_trait::async_trait; +use tokio::{ + io::AsyncReadExt as _, + sync::{Notify, mpsc}, +}; + +use super::*; +use crate::{ + config::{ + DnsConfig, DnsNameserver, DnsRoute, DnsTransport, PortRange, RuleAction, RuleKind, + RuleSpec, SnifferConfig, + }, + dispatch::{BoxStream, DatagramTransport}, + dns::{QueryType, build_query, synthesize_empty_response}, + platform::TunFd, + routing::{EmptyGeoMatcher, ProxyDispatchers, RuleSet}, +}; + +fn test_sniffer( + http_ports: &[PortRange], + tls_ports: &[PortRange], + quic_ports: &[PortRange], +) -> Arc { + Arc::new(SnifferConfig { + enable: true, + http_ports: http_ports.into(), + tls_ports: tls_ports.into(), + quic_ports: quic_ports.into(), + }) +} + +#[test] +fn tun_netstack_resource_event_contract_is_stable() { + assert_eq!(TUN_NETSTACK_STATS_INTERVAL, Duration::from_secs(30)); + assert_eq!( + TUN_NETSTACK_STATS_PERIODIC_EVENT, + "tun_netstack_stats_periodic" + ); + assert_eq!(TUN_NETSTACK_STATS_FINAL_EVENT, "tun_netstack_stats_final"); +} + +#[test] +fn ipv6_ingress_policy_drops_only_ipv6_when_disabled() { + assert!(!tun_ingress_allowed(false, &[0x60])); + assert!(tun_ingress_allowed(false, &[0x45])); + assert!(tun_ingress_allowed(false, &[])); + assert!(tun_ingress_allowed(true, &[0x60])); +} + +#[tokio::test] +async fn batch_tun_runtime_preserves_order_stats_and_invalid_packet_isolation() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(); + let source = "192.0.2.10:12000".parse().unwrap(); + let destination = "198.51.100.20:443".parse().unwrap(); + let mut bytes = 0; + // Nine queued frames cross the batch boundary. The invalid middle + // frame must not hide its neighbours, and IPv6 policy stays per packet. + for index in 0..7 { + let packet = build_udp(source, destination, &[index]); + bytes += packet.len(); + peer.send(&packet).unwrap(); + if index == 2 { + peer.send(&[0x70]).unwrap(); + peer.send(&[0x60; 40]).unwrap(); + } + } + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let stats = Arc::new(TunTrafficStats::default()); + let runtime = TunRuntime::new_with_stats( + tun, + ResourceLimits::default(), + dispatcher.clone(), + None, + false, + false, + None, + stats.clone(), + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + let mut response = [0; TUN_MTU]; + for index in 0..7 { + let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) + .await + .unwrap() + .unwrap(); + assert_udp_response(&response[..size], destination, source, &[index]); + } + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .unwrap() + .unwrap() + .unwrap(); + assert_eq!(stats.snapshot().up_total, (bytes + 40) as u64); + assert_eq!(stats.snapshot().down_total, bytes as u64); + assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert!(peer.try_recv(&mut response).is_err(), "packet replayed"); +} + +#[tokio::test] +async fn batch_tun_read_loop_counts_consumed_prefix_before_eof() { + use std::{io::Write as _, os::unix::net::UnixStream}; + + let (host, mut peer) = UnixStream::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + let tun = TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(); + let packet = build_udp( + "192.0.2.10:12000".parse().unwrap(), + "198.51.100.20:443".parse().unwrap(), + b"prefix", + ); + peer.write_all(&packet).unwrap(); + drop(peer); + let stats = Arc::new(TunTrafficStats::default()); + let runtime = TunRuntime::new_with_stats( + tun, + ResourceLimits::default(), + Arc::new(MockDispatcher::default()), + None, + true, + false, + None, + stats.clone(), + ) + .unwrap(); + let error = timeout( + Duration::from_secs(2), + runtime.run(CancellationToken::new()), + ) + .await + .unwrap() + .unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof); + assert_eq!(stats.snapshot().up_total, packet.len() as u64); +} + +#[tokio::test] +async fn effective_mtu_drops_oversized_udp_before_association_and_keeps_neighbor_responsive() { + assert_effective_mtu_drops_oversized_udp(false).await; +} + +#[tokio::test] +async fn effective_mtu_drops_oversized_dns_before_query_and_keeps_neighbor_responsive() { + assert_effective_mtu_drops_oversized_udp(true).await; +} + +async fn assert_effective_mtu_drops_oversized_udp(dns_query: bool) { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let neighbor_source: SocketAddr = "192.0.2.10:12001".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let oversized_destination = if dns_query { + "198.51.100.20:53".parse().unwrap() + } else { + destination + }; + let oversized_payload = if dns_query { + let mut query = build_query(0x1234, "oversized.example", QueryType::A).unwrap(); + query[10..12].copy_from_slice(&1_u16.to_be_bytes()); + // A legal EDNS OPT record with padding makes a 1373-byte DNS query, + // not trailing garbage that the DNS parser would already reject. + let option_data_len = 1373 - query.len() - 11; + query.push(0); + query.extend_from_slice(&41_u16.to_be_bytes()); + query.extend_from_slice(&1232_u16.to_be_bytes()); + query.extend_from_slice(&0_u32.to_be_bytes()); + query.extend_from_slice(&u16::try_from(option_data_len).unwrap().to_be_bytes()); + query.extend_from_slice(&12_u16.to_be_bytes()); + query.extend_from_slice(&u16::try_from(option_data_len - 4).unwrap().to_be_bytes()); + query.resize(1373, 0); + classify_query(&query).expect("oversized DNS fixture must be structurally valid"); + query + } else { + vec![0x5a; 1373] + }; + let oversized = build_udp(source, oversized_destination, &oversized_payload); + assert_eq!(oversized.len(), 1401); + let neighbor = build_udp(neighbor_source, destination, b"neighbor"); + peer.send(&oversized).unwrap(); + peer.send(&neighbor).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let dns_dispatcher = Arc::new(DnsReplyDispatcher::default()); + let stats = Arc::new(TunTrafficStats::default()); + let runtime = TunRuntime::new_with_stats( + tun, + ResourceLimits { + tun_max_datagram_size: 1400, + ..ResourceLimits::default() + }, + dispatcher.clone(), + dns_query.then(|| test_runtime_dns(dns_dispatcher.clone())), + true, + false, + None, + stats.clone(), + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let probe = observation::ResourceProbe::default(); + let task_probe = probe.clone(); + let task_cancellation = cancellation.clone(); + let task = tokio::spawn(async move { task_probe.scope(runtime.run(task_cancellation)).await }); + let mut response = [0_u8; TUN_MTU]; + let received = timeout(Duration::from_secs(2), async { + loop { + let size = peer.recv(&mut response).await.unwrap(); + if u16::from_be_bytes(response[22..24].try_into().unwrap()) == neighbor_source.port() { + assert_udp_response(&response[..size], destination, neighbor_source, b"neighbor"); + return size; + } + } + }) + .await; + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("MTU rejection prevented the TUN stop barrier") + .unwrap() + .unwrap(); + let neighbor_size = received.expect("oversized ingress hid its legal neighbor"); + let observed = probe.snapshot(); + assert!(observed.is_idle(), "TUN stop retained a child resource"); + assert_eq!(observed.peak(observation::ResourceKind::Association), 1); + assert_eq!(observed.peak(observation::ResourceKind::Waiter), 0); + assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert_eq!( + dispatcher.udp_sessions.lock().unwrap()[0].source, + neighbor_source + ); + assert!(dns_dispatcher.udp_sessions.lock().unwrap().is_empty()); + assert_eq!( + stats.snapshot().up_total, + (oversized.len() + neighbor.len()) as u64 + ); + assert_eq!(stats.snapshot().down_total, neighbor_size as u64); + assert!( + peer.try_recv(&mut response).is_err(), + "oversized response escaped" + ); +} + +#[test] +fn configured_sniffer_selects_custom_http_and_tls_ports() { + let config = test_sniffer( + &[PortRange { + start: 8_080, + end: 8_088, + }], + &[PortRange { + start: 8_443, + end: 8_443, + }], + &[], + ); + assert_eq!( + configured_sniff_protocol(&config, 8_084), + Some(SniffProtocol::Http) + ); + assert_eq!( + configured_sniff_protocol(&config, 8_443), + Some(SniffProtocol::Tls) + ); + assert_eq!(configured_sniff_protocol(&config, 80), None); + assert_eq!(configured_sniff_protocol(&config, 443), None); +} + +struct ScriptedQuicSniffer { + outcomes: VecDeque, + authentications: VecDeque, + authenticated_initial_in_last_ingest: bool, +} + +impl QuicSniffEngine for ScriptedQuicSniffer { + fn ingest(&mut self, _packet: &[u8]) -> QuicSniffOutcome { + self.authenticated_initial_in_last_ingest = + self.authentications.pop_front().unwrap_or(true); + self.outcomes + .pop_front() + .expect("scripted QUIC sniffer outcome exhausted") + } + + fn authenticated_initial_in_last_ingest(&self) -> bool { + self.authenticated_initial_in_last_ingest + } +} + +fn scripted_quic_state( + scripts: Vec>, +) -> UdpQuicSniffState ScriptedQuicSniffer> { + let mut scripts = scripts + .into_iter() + .map(VecDeque::from) + .collect::>(); + UdpQuicSniffState::new(move || ScriptedQuicSniffer { + outcomes: scripts + .pop_front() + .expect("scripted QUIC flow factory exhausted"), + authentications: VecDeque::new(), + authenticated_initial_in_last_ingest: false, + }) +} + +fn scripted_quic_state_with_authentication( + scripts: Vec>, +) -> UdpQuicSniffState ScriptedQuicSniffer> { + let mut sniffers = scripts + .into_iter() + .map(|script| { + let (outcomes, authentications) = script.into_iter().unzip(); + ScriptedQuicSniffer { + outcomes, + authentications, + authenticated_initial_in_last_ingest: false, + } + }) + .collect::>(); + UdpQuicSniffState::new(move || { + sniffers + .pop_front() + .expect("scripted QUIC flow factory exhausted") + }) +} + +fn test_udp_datagram( + source: SocketAddr, + destination: SocketAddr, + payload: &'static [u8], +) -> UdpDatagram { + UdpDatagram::new(source, destination, Bytes::from_static(payload)) +} + +fn quic_connection_marker(destination_connection_id: &[u8]) -> Bytes { + assert!((1..=20).contains(&destination_connection_id.len())); + let mut datagram = vec![ + 0xc0, // QUIC v1 long header, Initial packet. + 0x00, + 0x00, + 0x00, + 0x01, + u8::try_from(destination_connection_id.len()).unwrap(), + ]; + datagram.extend_from_slice(destination_connection_id); + datagram.extend_from_slice(&[ + 0x00, // Empty source connection ID. + 0x00, // Empty token. + 0x11, // One packet-number byte plus a 16-byte AEAD tag. + ]); + datagram.extend_from_slice(&[0; 17]); + let datagram = Bytes::from(datagram); + assert!(quic_connection_key(&datagram).is_some()); + datagram +} + +fn quic_non_initial_marker(packet_type: u8, destination_connection_id: &[u8]) -> Bytes { + assert!((1..=3).contains(&packet_type)); + assert!((1..=20).contains(&destination_connection_id.len())); + let mut datagram = vec![ + 0xc0 | (packet_type << 4), + 0x00, + 0x00, + 0x00, + 0x01, + u8::try_from(destination_connection_id.len()).unwrap(), + ]; + datagram.extend_from_slice(destination_connection_id); + datagram.extend_from_slice(&[ + 0x00, // Empty source connection ID. + 0x11, // Protected payload length. + ]); + datagram.extend_from_slice(&[0; 17]); + let datagram = Bytes::from(datagram); + assert!(quic_connection_key(&datagram).is_none()); + assert!(!quic_has_unsupported_version(&datagram)); + datagram +} + +fn unsupported_quic_version_marker(destination_connection_id: &[u8]) -> Bytes { + assert!((1..=20).contains(&destination_connection_id.len())); + let mut datagram = vec![ + 0xc0, + 0xfa, + 0xce, + 0xb0, + 0x0c, + u8::try_from(destination_connection_id.len()).unwrap(), + ]; + datagram.extend_from_slice(destination_connection_id); + let datagram = Bytes::from(datagram); + assert!(quic_connection_key(&datagram).is_none()); + assert!(quic_has_unsupported_version(&datagram)); + datagram +} + +#[test] +fn quic_fragmentation_sends_nothing_early_then_replays_every_datagram_in_order() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![vec![ + QuicSniffOutcome::NeedMoreData, + QuicSniffOutcome::Matched("api.example.com".to_owned()), + ]]); + + assert!(matches!( + state.ingest_datagram(test_udp_datagram(source, destination, b"initial-one"), now,), + QuicIngressResult::Buffered + )); + assert_eq!(state.pending_datagrams, 1); + assert_eq!(state.pending_bytes, b"initial-one".len()); + + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + test_udp_datagram(source, destination, b"initial-two"), + now + Duration::from_millis(1), + ) else { + panic!("the completed ClientHello must release the buffered Initial flight"); + }; + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); + assert_eq!( + replay + .iter() + .map(|prepared| prepared.datagram.payload.as_ref()) + .collect::>(), + vec![b"initial-one".as_slice(), b"initial-two".as_slice()] + ); + assert!(replay.iter().all(|prepared| { + prepared.sniffed_domain.as_deref() == Some("api.example.com") + && prepared.datagram.destination == destination + })); + + let QuicIngressResult::Forward(next) = state.ingest_datagram( + test_udp_datagram(source, destination, b"short-header"), + now + Duration::from_millis(2), + ) else { + panic!("a resolved QUIC flow must forward later datagrams immediately"); + }; + assert_eq!(next.sniffed_domain.as_deref(), Some("api.example.com")); +} + +#[test] +fn quic_completed_flow_lru_makes_room_for_a_fifth_sniffer() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destinations = (0..=QUIC_SNIFF_FLOW_MAX) + .map(|index| { + SocketAddr::new( + "198.51.100.20".parse().unwrap(), + 443 + u16::try_from(index).unwrap(), + ) + }) + .collect::>(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state( + (0..=QUIC_SNIFF_FLOW_MAX) + .map(|index| { + vec![QuicSniffOutcome::Matched(format!( + "node-{index}.example.com" + ))] + }) + .collect(), + ); + + for (index, destination) in destinations[..QUIC_SNIFF_FLOW_MAX].iter().enumerate() { + let result = state.ingest_datagram( + UdpDatagram::new( + source, + *destination, + quic_connection_marker(&[u8::try_from(index + 1).unwrap()]), + ), + now + Duration::from_millis(u64::try_from(index).unwrap()), + ); + assert!(matches!(result, QuicIngressResult::Forward(_))); + } + assert_eq!(state.flows.len(), QUIC_SNIFF_FLOW_MAX); + + let touched = state.ingest_datagram( + test_udp_datagram(source, destinations[0], b"short-header"), + now + Duration::from_millis(10), + ); + assert!(matches!(touched, QuicIngressResult::Forward(_))); + + let QuicIngressResult::Forward(fifth) = state.ingest_datagram( + UdpDatagram::new( + source, + destinations[QUIC_SNIFF_FLOW_MAX], + quic_connection_marker(b"fifth"), + ), + now + Duration::from_millis(11), + ) else { + panic!("a completed flow must be evicted so the fifth flow can be sniffed"); + }; + assert_eq!(fifth.sniffed_domain.as_deref(), Some("node-4.example.com")); + assert_eq!(state.flows.len(), QUIC_SNIFF_FLOW_MAX); + assert!(state.flows.contains_key(&destinations[0])); + assert!(!state.flows.contains_key(&destinations[1])); + assert!(state.flows.contains_key(&destinations[2])); + assert!(state.flows.contains_key(&destinations[3])); + assert!(state.flows.contains_key(&destinations[4])); +} + +#[test] +fn quic_pending_unverified_prefix_adopts_its_first_authenticated_initial_identity() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let initial = quic_connection_marker(b"authenticated"); + let now = TokioInstant::now(); + let mut state = scripted_quic_state_with_authentication(vec![vec![ + (QuicSniffOutcome::NeedMoreData, false), + ( + QuicSniffOutcome::Matched("real.example.com".to_owned()), + true, + ), + ]]); + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, quic_non_initial_marker(1, b"prefix")), + now, + ), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new(source, destination, initial.clone()), + now + Duration::from_millis(1), + ) else { + panic!("the first authenticated Initial must release the pending flight"); + }; + assert_eq!(replay.len(), 2); + assert_eq!( + replay[0].flow_id.as_ref(), + quic_connection_key(&initial).as_ref() + ); + assert!(replay[1].flow_id.is_none()); + assert!( + replay + .iter() + .all(|prepared| { prepared.sniffed_domain.as_deref() == Some("real.example.com") }) + ); +} + +#[test] +fn quic_unauthenticated_first_initial_cannot_suppress_a_later_authenticated_flow() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let marker = quic_connection_marker(b"shared-dcid"); + let now = TokioInstant::now(); + let mut state = scripted_quic_state_with_authentication(vec![ + vec![(QuicSniffOutcome::NotMatched, false)], + vec![( + QuicSniffOutcome::Matched("real.example.com".to_owned()), + true, + )], + ]); + let QuicIngressResult::Forward(unverified) = + state.ingest_datagram(UdpDatagram::new(source, destination, marker.clone()), now) + else { + panic!("an unauthenticated Initial must fail open without creating a flow identity"); + }; + assert!(unverified.flow_id.is_none()); + let QuicIngressResult::Forward(authenticated) = state.ingest_datagram( + UdpDatagram::new(source, destination, marker.clone()), + now + Duration::from_millis(1), + ) else { + panic!("a genuine Initial must retry sniffing even with the same observed DCID"); + }; + assert_eq!( + authenticated.sniffed_domain.as_deref(), + Some("real.example.com") + ); + assert_eq!( + authenticated.flow_id.as_ref(), + quic_connection_key(&marker).as_ref() + ); +} + +#[test] +fn quic_flow_identity_is_exact_and_survives_sniff_table_eviction() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let original_destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![QuicSniffOutcome::Matched( + "stable.example.com".to_owned() + )]; + QUIC_SNIFF_FLOW_MAX + 2 + ]); + let marker = quic_connection_marker(b"stable"); + let expected = quic_connection_key(&marker).unwrap().routing_identity(); + let QuicIngressResult::Forward(first) = state.ingest_datagram( + UdpDatagram::new(source, original_destination, marker.clone()), + now, + ) else { + panic!("the first authenticated Initial must identify its flow"); + }; + assert_eq!(first.flow_id.unwrap().routing_identity(), expected); + for index in 1..=QUIC_SNIFF_FLOW_MAX { + let destination = SocketAddr::new( + original_destination.ip(), + 443 + u16::try_from(index).unwrap(), + ); + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(&[index as u8])), + now + Duration::from_millis(u64::try_from(index).unwrap()), + ), + QuicIngressResult::Forward(_) + )); + } + assert!(!state.flows.contains_key(&original_destination)); + let QuicIngressResult::Forward(revisited) = state.ingest_datagram( + UdpDatagram::new(source, original_destination, marker), + now + Duration::from_millis(10), + ) else { + panic!("a revisited Initial must retain its exact identity after sniff eviction"); + }; + assert_eq!(revisited.flow_id.unwrap().routing_identity(), expected); + let zero_extended = quic_connection_key(&quic_connection_marker(b"stable\0")) + .unwrap() + .routing_identity(); + assert_ne!(expected, zero_extended, "the DCID length must be preserved"); + let mut v2_marker = quic_connection_marker(b"stable").to_vec(); + v2_marker[0] = 0xd0; + v2_marker[1..5].copy_from_slice(&0x6b33_43cf_u32.to_be_bytes()); + assert_ne!( + expected, + quic_connection_key(&v2_marker).unwrap().routing_identity(), + "wire version must be part of the exact flow identity" + ); +} + +#[test] +fn quic_pending_flow_marks_only_first_replayed_packet_on_match_failure_and_timeout() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let now = TokioInstant::now(); + for outcome in [ + QuicSniffOutcome::Matched("matched.example.com".to_owned()), + QuicSniffOutcome::NotMatched, + ] { + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let marker = quic_connection_marker(b"replay"); + let mut state = scripted_quic_state(vec![vec![QuicSniffOutcome::NeedMoreData, outcome]]); + assert!(matches!( + state.ingest_datagram(UdpDatagram::new(source, destination, marker.clone()), now), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new(source, destination, marker.clone()), + now + Duration::from_millis(1), + ) else { + panic!("a completed sniff flight must be released in order"); + }; + assert_eq!(replay.len(), 2); + assert_eq!( + replay[0].flow_id.as_ref(), + quic_connection_key(&marker).as_ref() + ); + assert!(replay[1].flow_id.is_none()); + } + let destination: SocketAddr = "198.51.100.21:443".parse().unwrap(); + let marker = quic_connection_marker(b"timeout"); + let mut state = scripted_quic_state(vec![vec![QuicSniffOutcome::NeedMoreData]]); + assert!(matches!( + state.ingest_datagram(UdpDatagram::new(source, destination, marker.clone()), now), + QuicIngressResult::Buffered + )); + let replay = state.expire(now + QUIC_SNIFF_TIMEOUT); + assert_eq!(replay.len(), 1); + assert_eq!( + replay[0].flow_id.as_ref(), + quic_connection_key(&marker).as_ref() + ); + assert!(replay[0].sniffed_domain.is_none()); + assert!(state.expire(now + QUIC_SNIFF_TIMEOUT).is_empty()); +} + +#[tokio::test] +async fn quic_prepared_datagrams_forward_flow_identity_only_on_the_marked_send() { + #[derive(Default)] + struct RecordingFlowTransport { + sent: Vec<(Datagram, Option>)>, + } + + #[async_trait] + impl DatagramTransport for RecordingFlowTransport { + async fn send(&mut self, datagram: Datagram) -> Result<(), DispatchError> { + self.sent.push((datagram, None)); + Ok(()) + } + + async fn send_with_flow_id( + &mut self, + datagram: Datagram, + flow_id: &[u8], + ) -> Result<(), DispatchError> { + self.sent.push((datagram, Some(flow_id.to_vec()))); + Ok(()) + } + + async fn receive(&mut self) -> Result { + std::future::pending().await + } + } + + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let (responses, _responses_rx) = mpsc::channel(1); + let context = UdpAssociationTaskContext { + association_id: 1, + source, + responses, + dispatcher: Arc::new(MockDispatcher::default()), + resource_stats: RuntimeResourceStats::new("tun_quic_flow_identity_test"), + association_clock: AssociationClock::realtime(), + last_activity: Arc::new(AtomicU64::new(0)), + tun_mtu: TUN_MTU, + sniffer: None, + cancellation: CancellationToken::new(), + }; + let mut transport = RecordingFlowTransport::default(); + let marker = quic_connection_marker(b"flow"); + let identity = quic_connection_key(&marker).unwrap(); + let expected = identity.routing_identity(); + assert!( + send_tun_udp_datagram( + &mut transport, + PreparedTunUdpDatagram::with_domain( + UdpDatagram::new(source, destination, marker.clone()), + Arc::from("sniffed.example.com"), + ) + .with_flow_id(Some(identity)), + &context, + ) + .await + .unwrap() + ); + assert!( + send_tun_udp_datagram( + &mut transport, + PreparedTunUdpDatagram::without_domain(test_udp_datagram( + source, + destination, + b"unmarked", + )), + &context, + ) + .await + .unwrap() + ); + assert_eq!(transport.sent.len(), 2); + assert_eq!(transport.sent[0].0.remote, Destination::Ip(destination)); + assert_eq!(transport.sent[0].0.payload, marker); + assert_eq!( + transport.sent[0].0.sniffed_domain.as_deref(), + Some("sniffed.example.com") + ); + assert_eq!(transport.sent[0].1.as_deref(), Some(expected.as_slice())); + assert_eq!(transport.sent[1].0.payload.as_ref(), b"unmarked"); + assert!(transport.sent[1].1.is_none()); +} + +#[test] +fn quic_completed_hint_expires_independently_while_other_destinations_stay_active() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let expired_destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let active_destination: SocketAddr = "198.51.100.21:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![QuicSniffOutcome::Matched("old.example.com".to_owned())], + vec![QuicSniffOutcome::Matched("active.example.com".to_owned())], + vec![QuicSniffOutcome::NotMatched], + ]); + for (destination, identity) in [ + (expired_destination, b"old".as_slice()), + (active_destination, b"active".as_slice()), + ] { + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(identity)), + now, + ), + QuicIngressResult::Forward(_) + )); + } + assert!(matches!( + state.ingest_datagram( + test_udp_datagram(source, active_destination, b"keep-active"), + now + Duration::from_secs(20), + ), + QuicIngressResult::Forward(_) + )); + let QuicIngressResult::Forward(expired) = state.ingest_datagram( + test_udp_datagram(source, expired_destination, b"short-header"), + now + Duration::from_secs(30), + ) else { + panic!("an idle completed hint must fail open without reusing its old SNI"); + }; + assert!(expired.sniffed_domain.is_none()); + let QuicIngressResult::Forward(active) = state.ingest_datagram( + test_udp_datagram(source, active_destination, b"short-header"), + now + Duration::from_secs(30), + ) else { + panic!("another destination's activity must preserve only its own hint"); + }; + assert_eq!(active.sniffed_domain.as_deref(), Some("active.example.com")); +} + +#[test] +fn quic_new_dcid_replaces_a_matched_domain_for_the_same_destination() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![QuicSniffOutcome::Matched("old.example.com".to_owned())], + vec![QuicSniffOutcome::Matched("new.example.com".to_owned())], + ]); + + let QuicIngressResult::Forward(old) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"old")), + now, + ) else { + panic!("the first QUIC connection must be sniffed"); + }; + assert_eq!(old.sniffed_domain.as_deref(), Some("old.example.com")); + assert_eq!( + old.flow_id.as_ref(), + quic_connection_key(&quic_connection_marker(b"old")).as_ref() + ); + + let QuicIngressResult::Forward(new) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"new")), + now + Duration::from_millis(1), + ) else { + panic!("a new DCID must create a fresh QUIC sniffer"); + }; + assert_eq!(new.sniffed_domain.as_deref(), Some("new.example.com")); + assert_eq!( + new.flow_id.as_ref(), + quic_connection_key(&quic_connection_marker(b"new")).as_ref() + ); + + let QuicIngressResult::Forward(short_header) = state.ingest_datagram( + test_udp_datagram(source, destination, b"short-header"), + now + Duration::from_millis(2), + ) else { + panic!("the replacement domain must be retained for short-header traffic"); + }; + assert_eq!( + short_header.sniffed_domain.as_deref(), + Some("new.example.com") + ); + assert!(short_header.flow_id.is_none()); +} + +#[test] +fn quic_unauthenticated_new_dcid_keeps_the_completed_domain_without_waiting() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state_with_authentication(vec![ + vec![( + QuicSniffOutcome::Matched("stable.example.com".to_owned()), + true, + )], + vec![(QuicSniffOutcome::NeedMoreData, false)], + ]); + + let QuicIngressResult::Forward(first) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"old")), + now, + ) else { + panic!("the first QUIC connection must be sniffed"); + }; + assert_eq!(first.sniffed_domain.as_deref(), Some("stable.example.com")); + assert!(first.flow_id.is_some()); + + let QuicIngressResult::Forward(candidate) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"new")), + now + Duration::from_millis(1), + ) else { + panic!("an unauthenticated candidate must not enter the pending state"); + }; + assert_eq!( + candidate.sniffed_domain.as_deref(), + Some("stable.example.com") + ); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); + assert!(candidate.flow_id.is_none()); + + let QuicIngressResult::Forward(short_header) = state.ingest_datagram( + test_udp_datagram(source, destination, b"short-header"), + now + Duration::from_millis(2), + ) else { + panic!("the authenticated completed hint must be retained"); + }; + assert_eq!( + short_header.sniffed_domain.as_deref(), + Some("stable.example.com") + ); +} + +#[test] +fn quic_pending_flow_accepts_a_new_header_dcid_authenticated_by_its_old_keys() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state_with_authentication(vec![vec![ + (QuicSniffOutcome::NeedMoreData, true), + ( + QuicSniffOutcome::Matched("same-connection.example.com".to_owned()), + true, + ), + ]]); + + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-a")), + now, + ), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-b")), + now + Duration::from_millis(1), + ) else { + panic!("old Initial keys must be tried before treating a new DCID as a new flow"); + }; + assert_eq!(replay.len(), 2); + assert_eq!( + replay[0].flow_id.as_ref(), + quic_connection_key(&quic_connection_marker(b"dcid-a")).as_ref() + ); + assert!(replay[1].flow_id.is_none()); + assert!(replay.iter().all(|prepared| { + prepared.sniffed_domain.as_deref() == Some("same-connection.example.com") + })); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); +} + +#[test] +fn quic_pending_flow_keeps_old_state_when_neither_key_authenticates_a_candidate() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state_with_authentication(vec![ + vec![ + (QuicSniffOutcome::NeedMoreData, true), + (QuicSniffOutcome::NotMatched, false), + ( + QuicSniffOutcome::Matched("old-flow.example.com".to_owned()), + true, + ), + ], + vec![(QuicSniffOutcome::NeedMoreData, false)], + ]); + + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-a")), + now, + ), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Forward(unverified) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-b")), + now + Duration::from_millis(1), + ) else { + panic!("an unverified candidate must fail open independently"); + }; + assert!(unverified.sniffed_domain.is_none()); + assert!(unverified.flow_id.is_none()); + assert_eq!(state.pending_datagrams, 1); + + let QuicIngressResult::Replay(old_flow) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"dcid-a")), + now + Duration::from_millis(2), + ) else { + panic!("the old pending parser and buffered flight must be retained"); + }; + assert_eq!(old_flow.len(), 2); + assert!( + old_flow + .iter() + .all(|prepared| prepared.sniffed_domain.as_deref() == Some("old-flow.example.com")) + ); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); +} + +#[test] +fn quic_zero_rtt_and_handshake_dcid_changes_do_not_reset_a_completed_hint() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![vec![QuicSniffOutcome::Matched( + "stable.example.com".to_owned(), + )]]); + + let QuicIngressResult::Forward(initial) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"initial")), + now, + ) else { + panic!("the Initial must establish a completed hint"); + }; + assert_eq!( + initial.sniffed_domain.as_deref(), + Some("stable.example.com") + ); + + for (index, datagram) in [ + quic_non_initial_marker(1, b"zero-rtt"), + quic_non_initial_marker(2, b"handshake"), + ] + .into_iter() + .enumerate() + { + let QuicIngressResult::Forward(forwarded) = state.ingest_datagram( + UdpDatagram::new(source, destination, datagram), + now + Duration::from_millis(u64::try_from(index + 1).unwrap()), + ) else { + panic!("a non-Initial long header must not trigger a new sniffer"); + }; + assert_eq!( + forwarded.sniffed_domain.as_deref(), + Some("stable.example.com") + ); + assert!(forwarded.flow_id.is_none()); + } +} + +#[test] +fn unsupported_quic_version_clears_completed_hint_and_releases_pending_flight() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let completed_destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let pending_destination: SocketAddr = "198.51.100.21:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![QuicSniffOutcome::Matched( + "must-not-leak.example.com".to_owned(), + )], + vec![QuicSniffOutcome::NeedMoreData], + ]); + + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new( + source, + completed_destination, + quic_connection_marker(b"completed"), + ), + now, + ), + QuicIngressResult::Forward(_) + )); + let QuicIngressResult::Forward(unsupported) = state.ingest_datagram( + UdpDatagram::new( + source, + completed_destination, + unsupported_quic_version_marker(b"unknown"), + ), + now + Duration::from_millis(1), + ) else { + panic!("an unsupported version must fail open without waiting"); + }; + assert!(unsupported.sniffed_domain.is_none()); + assert!(unsupported.flow_id.is_none()); + let QuicIngressResult::Forward(short_header) = state.ingest_datagram( + test_udp_datagram(source, completed_destination, b"short-header"), + now + Duration::from_millis(2), + ) else { + panic!("the stale completed hint must remain cleared"); + }; + assert!(short_header.sniffed_domain.is_none()); + + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new( + source, + pending_destination, + quic_connection_marker(b"pending"), + ), + now, + ), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new( + source, + pending_destination, + unsupported_quic_version_marker(b"unknown"), + ), + now + Duration::from_millis(1), + ) else { + panic!("an unsupported version must release a pending flight"); + }; + assert_eq!(replay.len(), 2); + assert!( + replay + .iter() + .all(|prepared| prepared.sniffed_domain.is_none()) + ); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); +} + +#[test] +fn quic_new_dcid_retries_sniffing_after_a_no_domain_result() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![QuicSniffOutcome::NotMatched], + vec![QuicSniffOutcome::Matched( + "recovered.example.com".to_owned(), + )], + ]); + + let QuicIngressResult::Forward(first) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"old")), + now, + ) else { + panic!("the first QUIC connection must fail open"); + }; + assert!(first.sniffed_domain.is_none()); + + let QuicIngressResult::Forward(recovered) = state.ingest_datagram( + UdpDatagram::new(source, destination, quic_connection_marker(b"new")), + now + Duration::from_millis(1), + ) else { + panic!("a new DCID must retry sniffing after a terminal result"); + }; + assert_eq!( + recovered.sniffed_domain.as_deref(), + Some("recovered.example.com") + ); +} + +#[test] +fn quic_new_dcid_releases_an_old_pending_flight_before_the_new_connection() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let old_payload = quic_connection_marker(b"old"); + let new_payload = quic_connection_marker(b"new"); + let mut state = scripted_quic_state_with_authentication(vec![ + vec![ + (QuicSniffOutcome::NeedMoreData, true), + (QuicSniffOutcome::NotMatched, false), + ], + vec![( + QuicSniffOutcome::Matched("new.example.com".to_owned()), + true, + )], + ]); + + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, old_payload.clone()), + now, + ), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new(source, destination, new_payload.clone()), + now + Duration::from_millis(1), + ) else { + panic!("a new DCID must release the stale flight before forwarding itself"); + }; + assert_eq!(replay.len(), 2); + assert_eq!(replay[0].datagram.payload, old_payload); + assert!(replay[0].sniffed_domain.is_none()); + assert_eq!( + replay[0].flow_id.as_ref(), + quic_connection_key(&old_payload).as_ref() + ); + assert_eq!(replay[1].datagram.payload, new_payload); + assert_eq!(replay[1].sniffed_domain.as_deref(), Some("new.example.com")); + assert_eq!( + replay[1].flow_id.as_ref(), + quic_connection_key(&new_payload).as_ref() + ); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); +} + +#[tokio::test] +async fn quic_association_holds_the_first_fragment_then_sends_the_original_flight() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let datagrams = Arc::new(Mutex::new(Vec::new())); + let sent = Arc::new(Notify::new()); + let dispatcher = Arc::new(RecordingUdpDispatcher { + datagrams: datagrams.clone(), + sent: sent.clone(), + }); + let (inbound_tx, mut inbound_rx) = mpsc::channel(4); + let (responses, _responses_rx) = mpsc::channel(4); + let cancellation = CancellationToken::new(); + let child = cancellation.clone(); + let mut scripts = VecDeque::from([VecDeque::from([ + QuicSniffOutcome::NeedMoreData, + QuicSniffOutcome::Matched("api.example.com".to_owned()), + ])]); + let task = tokio::spawn(async move { + run_udp_association_inner_with_quic_factory( + &mut inbound_rx, + &UdpAssociationTaskContext { + association_id: 1, + source, + responses, + dispatcher, + resource_stats: RuntimeResourceStats::new("tun_runtime_quic_test"), + association_clock: AssociationClock::realtime(), + last_activity: Arc::new(AtomicU64::new(0)), + tun_mtu: TUN_MTU, + sniffer: Some(test_sniffer( + &[], + &[], + &[PortRange { + start: 443, + end: 443, + }], + )), + cancellation: child, + }, + move || ScriptedQuicSniffer { + outcomes: scripts + .pop_front() + .expect("scripted QUIC flow factory exhausted"), + authentications: VecDeque::new(), + authenticated_initial_in_last_ingest: false, + }, + ) + .await + }); + + inbound_tx + .send(test_udp_datagram(source, destination, b"initial-one")) + .await + .unwrap(); + assert!( + timeout(Duration::from_millis(30), sent.notified()) + .await + .is_err(), + "the first incomplete Initial was sent before sniffing completed" + ); + assert!(datagrams.lock().unwrap().is_empty()); + + inbound_tx + .send(test_udp_datagram(source, destination, b"initial-two")) + .await + .unwrap(); + timeout(Duration::from_secs(1), async { + while datagrams.lock().unwrap().len() != 2 { + sent.notified().await; + } + }) + .await + .expect("the completed QUIC Initial flight was not replayed"); + { + let recorded = datagrams.lock().unwrap(); + assert_eq!( + recorded + .iter() + .map(|datagram| datagram.payload.as_ref()) + .collect::>(), + vec![b"initial-one".as_slice(), b"initial-two".as_slice()] + ); + assert!(recorded.iter().all(|datagram| { + datagram.remote == Destination::Ip(destination) + && datagram.sniffed_domain.as_deref() == Some("api.example.com") + })); + } + + cancellation.cancel(); + timeout(Duration::from_secs(1), task) + .await + .expect("QUIC association did not stop") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn quic_ready_response_is_processed_before_the_next_replay_send_blocks() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let send_count = Arc::new(AtomicUsize::new(0)); + let blocked_send_started = Arc::new(Notify::new()); + let dispatcher = Arc::new(ReplayFairDispatcher { + response: Datagram { + remote: Destination::Ip(destination), + payload: Bytes::from_static(b"ready-response"), + sniffed_domain: None, + }, + send_count: send_count.clone(), + blocked_send_started: blocked_send_started.clone(), + }); + let (inbound_tx, mut inbound_rx) = mpsc::channel(4); + let (responses, mut responses_rx) = mpsc::channel(4); + let cancellation = CancellationToken::new(); + let child = cancellation.clone(); + let mut scripts = VecDeque::from([VecDeque::from([ + QuicSniffOutcome::NeedMoreData, + QuicSniffOutcome::Matched("api.example.com".to_owned()), + ])]); + let task = tokio::spawn(async move { + run_udp_association_inner_with_quic_factory( + &mut inbound_rx, + &UdpAssociationTaskContext { + association_id: 1, + source, + responses, + dispatcher, + resource_stats: RuntimeResourceStats::new("tun_runtime_quic_fairness_test"), + association_clock: AssociationClock::realtime(), + last_activity: Arc::new(AtomicU64::new(0)), + tun_mtu: TUN_MTU, + sniffer: Some(test_sniffer( + &[], + &[], + &[PortRange { + start: 443, + end: 443, + }], + )), + cancellation: child, + }, + move || ScriptedQuicSniffer { + outcomes: scripts + .pop_front() + .expect("scripted QUIC flow factory exhausted"), + authentications: VecDeque::new(), + authenticated_initial_in_last_ingest: false, + }, + ) + .await + }); + + let marker = quic_connection_marker(b"flow"); + inbound_tx + .send(UdpDatagram::new(source, destination, marker.clone())) + .await + .unwrap(); + inbound_tx + .send(UdpDatagram::new(source, destination, marker)) + .await + .unwrap(); + + let response = timeout(Duration::from_secs(1), responses_rx.recv()) + .await + .expect("a blocked second replay send starved the ready response") + .expect("response channel closed"); + assert_eq!(&response.payload[..], b"ready-response"); + timeout(Duration::from_secs(1), blocked_send_started.notified()) + .await + .expect("the second replay send did not enter its blocked state"); + assert_eq!(send_count.load(Ordering::Relaxed), 2); + + cancellation.cancel(); + timeout(Duration::from_secs(1), task) + .await + .expect("QUIC association did not stop") + .unwrap() + .unwrap(); +} + +#[test] +fn quic_failure_and_timeout_fail_open_with_exact_buffered_payloads() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let failed_destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let timed_out_destination: SocketAddr = "198.51.100.21:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![QuicSniffOutcome::NeedMoreData, QuicSniffOutcome::NotMatched], + vec![QuicSniffOutcome::NeedMoreData], + ]); + + assert!(matches!( + state.ingest_datagram( + test_udp_datagram(source, failed_destination, b"failed-one"), + now, + ), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(failed) = state.ingest_datagram( + test_udp_datagram(source, failed_destination, b"failed-two"), + now + Duration::from_millis(1), + ) else { + panic!("a parse failure must release the original datagrams"); + }; + assert_eq!( + failed + .iter() + .map(|prepared| prepared.datagram.payload.as_ref()) + .collect::>(), + vec![b"failed-one".as_slice(), b"failed-two".as_slice()] + ); + assert!( + failed + .iter() + .all(|prepared| prepared.sniffed_domain.is_none()) + ); + + assert!(matches!( + state.ingest_datagram( + test_udp_datagram(source, timed_out_destination, b"timed-out"), + now, + ), + QuicIngressResult::Buffered + )); + assert!( + state + .expire(now + QUIC_SNIFF_TIMEOUT - Duration::from_millis(1)) + .is_empty() + ); + let timed_out = state.expire(now + QUIC_SNIFF_TIMEOUT); + assert_eq!(timed_out.len(), 1); + assert_eq!(&timed_out[0].datagram.payload[..], b"timed-out"); + assert!(timed_out[0].sniffed_domain.is_none()); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); +} + +#[test] +fn quic_ech_and_parser_limit_release_buffered_datagrams_without_a_domain() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let now = TokioInstant::now(); + for (index, terminal) in [ + QuicSniffOutcome::EchExtensionPresent, + QuicSniffOutcome::LimitReached, + ] + .into_iter() + .enumerate() + { + let destination = SocketAddr::new( + "198.51.100.20".parse().unwrap(), + 443 + u16::try_from(index).unwrap(), + ); + let mut state = scripted_quic_state(vec![vec![QuicSniffOutcome::NeedMoreData, terminal]]); + assert!(matches!( + state.ingest_datagram(test_udp_datagram(source, destination, b"initial-one"), now,), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Replay(replay) = + state.ingest_datagram(test_udp_datagram(source, destination, b"initial-two"), now) + else { + panic!("a terminal QUIC outcome must release buffered datagrams"); + }; + assert_eq!(replay.len(), 2); + assert!( + replay + .iter() + .all(|prepared| prepared.sniffed_domain.is_none()) + ); + } +} + +#[test] +fn quic_pending_datagram_and_flow_tables_are_hard_bounded() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![vec![ + QuicSniffOutcome::NeedMoreData; + QUIC_SNIFF_PENDING_DATAGRAM_MAX + 1 + ]]); + for index in 0..QUIC_SNIFF_PENDING_DATAGRAM_MAX { + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new(source, destination, vec![u8::try_from(index).unwrap()]), + now, + ), + QuicIngressResult::Buffered + )); + } + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new(source, destination, Bytes::from_static(b"overflow")), + now, + ) else { + panic!("the ninth pending datagram must fail open"); + }; + assert_eq!(replay.len(), QUIC_SNIFF_PENDING_DATAGRAM_MAX + 1); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); + + let scripts = (0..QUIC_SNIFF_FLOW_MAX) + .map(|_| vec![QuicSniffOutcome::NeedMoreData]) + .collect(); + let mut state = scripted_quic_state(scripts); + for index in 0..QUIC_SNIFF_FLOW_MAX { + let destination = SocketAddr::new( + "198.51.100.20".parse().unwrap(), + 443 + u16::try_from(index).unwrap(), + ); + assert!(matches!( + state.ingest_datagram(test_udp_datagram(source, destination, b"pending"), now,), + QuicIngressResult::Buffered + )); + } + let overflow_destination: SocketAddr = "198.51.100.30:8443".parse().unwrap(); + let QuicIngressResult::Forward(overflow) = state.ingest_datagram( + test_udp_datagram(source, overflow_destination, b"fifth-flow"), + now, + ) else { + panic!("a fifth QUIC flow must fail open without creating a parser"); + }; + assert!(overflow.sniffed_domain.is_none()); + assert_eq!(state.flows.len(), QUIC_SNIFF_FLOW_MAX); +} + +#[test] +fn quic_pending_byte_budget_accepts_32_kib_and_replays_the_overflow_datagram() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![vec![ + QuicSniffOutcome::NeedMoreData, + QuicSniffOutcome::NeedMoreData, + ]]); + + assert!(matches!( + state.ingest_datagram( + UdpDatagram::new( + source, + destination, + vec![0xaa; QUIC_SNIFF_PENDING_BYTES_MAX], + ), + now, + ), + QuicIngressResult::Buffered + )); + assert_eq!(state.pending_datagrams, 1); + assert_eq!(state.pending_bytes, QUIC_SNIFF_PENDING_BYTES_MAX); + + let QuicIngressResult::Replay(replay) = state.ingest_datagram( + UdpDatagram::new(source, destination, Bytes::from_static(b"x")), + now, + ) else { + panic!("a datagram above the aggregate byte budget must fail open"); + }; + assert_eq!(replay.len(), 2); + assert_eq!( + replay[0].datagram.payload.len(), + QUIC_SNIFF_PENDING_BYTES_MAX + ); + assert_eq!(&replay[1].datagram.payload[..], b"x"); + assert_eq!(state.pending_datagrams, 0); + assert_eq!(state.pending_bytes, 0); +} + +#[test] +fn quic_flow_state_is_isolated_by_destination_and_unconfigured_ports_are_skipped() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination_a: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let destination_b: SocketAddr = "198.51.100.21:443".parse().unwrap(); + let now = TokioInstant::now(); + let mut state = scripted_quic_state(vec![ + vec![ + QuicSniffOutcome::NeedMoreData, + QuicSniffOutcome::Matched("a.example.com".to_owned()), + ], + vec![QuicSniffOutcome::Matched("b.example.com".to_owned())], + ]); + + assert!(matches!( + state.ingest_datagram(test_udp_datagram(source, destination_a, b"a-one"), now,), + QuicIngressResult::Buffered + )); + let QuicIngressResult::Forward(b) = + state.ingest_datagram(test_udp_datagram(source, destination_b, b"b-one"), now) + else { + panic!("one destination must not wait for another destination"); + }; + assert_eq!(b.sniffed_domain.as_deref(), Some("b.example.com")); + let QuicIngressResult::Replay(a) = + state.ingest_datagram(test_udp_datagram(source, destination_a, b"a-two"), now) + else { + panic!("the first destination must retain its independent parser"); + }; + assert!( + a.iter() + .all(|prepared| prepared.sniffed_domain.as_deref() == Some("a.example.com")) + ); + + let config = test_sniffer( + &[], + &[], + &[PortRange { + start: 443, + end: 443, + }], + ); + assert!(configured_quic_sniffing(Some(&config), 443)); + assert!(!configured_quic_sniffing(Some(&config), 8_443)); + assert!(!configured_quic_sniffing(None, 443)); +} + +#[test] +fn tun_response_capacities_are_independent_of_tcp_accept_and_each_other() { + let tun = ResourceLimits::default(); + assert_eq!(tun.tun_udp_response_queue_capacity, 4_096); + assert_eq!(tun.tun_dns_response_queue_capacity, 128); + let altered = ResourceLimits { + event_queue_capacity: 3, + tun_udp_association_queue_capacity: 2, + tun_udp_response_queue_capacity: 5, + tun_dns_response_queue_capacity: 7, + ..tun + }; + let config = tun_netstack_config(altered, false); + assert_eq!(config.tcp_accept_queue, 3); + assert_eq!(altered.tun_udp_association_queue_capacity, 2); + assert_eq!(altered.tun_udp_response_queue_capacity, 5); + assert_eq!(altered.tun_dns_response_queue_capacity, 7); +} + +#[tokio::test] +async fn default_udp_response_queue_absorbs_bounded_bursts_without_blocking_dns() { + let stats = RuntimeResourceStats::new("tun_response_capacity_test"); + let cancellation = CancellationToken::new(); + let (mut ingress, mut ordinary, mut dns) = UdpIngress::new( + UdpIngressContext { + dispatcher: Arc::new(MockDispatcher::default()), + dns: None, + sniffer: None, + limits: ResourceLimits::default(), + resource_stats: stats.clone(), + }, + cancellation.clone(), + ); + assert_eq!(ordinary.max_capacity(), 4_096); + assert_eq!(dns.max_capacity(), 128); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let server: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let response = + |sequence: u32| UdpDatagram::new(server, source, sequence.to_be_bytes().to_vec()); + let last_activity = AtomicU64::new(5); + for sequence in 0..4_096 { + assert!(matches!( + try_queue_tun_udp_response( + &ingress.responses, + response(sequence), + &last_activity, + u64::from(sequence) + 100, + &stats, + ), + ResponseQueueResult::Queued + )); + } + assert_eq!(ordinary.len(), 4_096); + assert!(matches!( + try_queue_tun_udp_response( + &ingress.responses, + response(4_096), + &last_activity, + 5_000, + &stats, + ), + ResponseQueueResult::Dropped + )); + assert_eq!(last_activity.load(Ordering::Acquire), 4_195); + assert_eq!(stats.snapshot().udp_response_queue_drops, 1); + assert_eq!(stats.snapshot().udp_association_queue_drops, 0); + assert_eq!(stats.snapshot().dns_queue_drops, 0); + + // Saturating the ordinary channel neither consumes DNS capacity nor + // overwrites any accepted response. Freeing one slot admits only new work. + try_queue_tun_dns_response(&ingress.dns_responses, response(7), None, &stats); + assert_eq!(&dns.try_recv().unwrap().payload[..], 7_u32.to_be_bytes()); + assert_eq!( + &ordinary.try_recv().unwrap().payload[..], + 0_u32.to_be_bytes() + ); + assert!(matches!( + try_queue_tun_udp_response( + &ingress.responses, + response(4_097), + &last_activity, + 5_001, + &stats, + ), + ResponseQueueResult::Queued + )); + for sequence in 1_u32..4_096 { + assert_eq!( + &ordinary.try_recv().unwrap().payload[..], + sequence.to_be_bytes() + ); + } + assert_eq!( + &ordinary.try_recv().unwrap().payload[..], + 4_097_u32.to_be_bytes() + ); + assert!(matches!( + ordinary.try_recv(), + Err(mpsc::error::TryRecvError::Empty) + )); + ordinary.close(); + assert!(matches!( + try_queue_tun_udp_response( + &ingress.responses, + response(4_098), + &last_activity, + 6_000, + &stats, + ), + ResponseQueueResult::Closed + )); + assert_eq!(last_activity.load(Ordering::Acquire), 5_001); + assert_eq!(stats.snapshot().udp_queue_drops, 1); + assert_eq!(stats.snapshot().dns_queue_drops, 0); + ingress.stop().await; + assert!(!cancellation.is_cancelled()); +} + +#[test] +fn tun_netstack_keeps_queue_and_per_flow_bounds_without_a_flow_count_ceiling() { + let limits = ResourceLimits::default(); + let config = tun_netstack_config(limits, false); + + assert_eq!(config.tcp_accept_queue, limits.event_queue_capacity); + assert_eq!( + config.tcp_buffer_per_direction, + limits.tcp_buffer_per_direction + ); + assert_eq!(config.mtu, 1_500); + assert_eq!(limits.max_datagram_size, 65_535); +} + +fn test_association( + generation: u64, + last_activity: u64, +) -> ( + UdpAssociation, + mpsc::Receiver, + CancellationToken, +) { + let (sender, receiver) = mpsc::channel(1); + let cancellation = CancellationToken::new(); + ( + UdpAssociation { + generation, + sender, + cancellation: cancellation.clone(), + last_activity: Arc::new(AtomicU64::new(last_activity)), + }, + receiver, + cancellation, + ) +} + +#[test] +fn stale_association_completion_cannot_remove_a_replacement_generation() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let (replacement, _receiver, _) = test_association(2, 0); + let mut associations = HashMap::from([(source, replacement)]); + + assert!(remove_completed_association(&mut associations, source, 1).is_none()); + assert_eq!(associations.get(&source).unwrap().generation, 2); + assert!(remove_completed_association(&mut associations, source, 2).is_some()); + assert!(associations.is_empty()); +} + +#[test] +fn association_activity_clock_refreshes_only_successfully_queued_work() { + let tick = Arc::new(AtomicU64::new(100)); + let clock = AssociationClock::injected(tick.clone()); + let (association, mut receiver, _) = test_association(1, 5); + let stats = RuntimeResourceStats::new("tun_runtime_test"); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + + assert!(matches!( + try_queue_association_input( + &association, + UdpPacketView { + source, + destination, + payload: b"queued" + }, + clock.now(), + 1, + &stats, + ), + AssociationInputResult::Queued + )); + assert_eq!(association.last_activity.load(Ordering::Acquire), 100); + + tick.store(110, Ordering::Release); + assert!(matches!( + try_queue_association_input( + &association, + UdpPacketView { + source, + destination, + payload: b"full" + }, + clock.now(), + 1, + &stats, + ), + AssociationInputResult::Full + )); + assert_eq!(association.last_activity.load(Ordering::Acquire), 100); + assert_eq!(stats.snapshot().udp_queue_drops, 1); + assert_eq!(stats.snapshot().udp_association_queue_drops, 1); + assert_eq!(stats.snapshot().udp_response_queue_drops, 0); + + receiver.try_recv().unwrap(); + drop(receiver); + tick.store(120, Ordering::Release); + assert!(matches!( + try_queue_association_input( + &association, + UdpPacketView { + source, + destination, + payload: b"closed" + }, + clock.now(), + 1, + &stats, + ), + AssociationInputResult::Closed + )); + assert_eq!(association.last_activity.load(Ordering::Acquire), 100); +} + +#[test] +fn periodic_cleanup_removes_expired_and_closed_but_preserves_active_entries() { + let now = 100; + let expired_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let closed_source: SocketAddr = "192.0.2.11:12001".parse().unwrap(); + let active_source: SocketAddr = "192.0.2.12:12002".parse().unwrap(); + let (expired, _expired_receiver, expired_cancellation) = test_association(1, 70); + let (closed, closed_receiver, closed_cancellation) = test_association(2, 99); + let (active, _active_receiver, _) = test_association(3, 80); + drop(closed_receiver); + let mut associations = HashMap::from([ + (expired_source, expired), + (closed_source, closed), + (active_source, active), + ]); + + let removed = take_expired_or_closed_associations(&mut associations, now); + assert_eq!(removed.len(), 2); + assert!(associations.contains_key(&active_source)); + assert!(!expired_cancellation.is_cancelled()); + assert!(!closed_cancellation.is_cancelled()); + cancel_removed_associations(removed); + assert!(expired_cancellation.is_cancelled()); + assert!(closed_cancellation.is_cancelled()); +} + +#[derive(Default)] +struct MockDispatcher { + tcp_sessions: Mutex>, + udp_sessions: Mutex>, + tcp_called: Notify, + udp_response_ready: Arc, +} + +#[async_trait] +impl Dispatcher for MockDispatcher { + async fn connect_tcp(&self, session: StreamSession) -> Result { + self.tcp_sessions.lock().unwrap().push(session); + self.tcp_called.notify_one(); + let (client, mut server) = tokio::io::duplex(1_024); + tokio::spawn(async move { + let mut buffer = [0_u8; 256]; + while let Ok(size) = server.read(&mut buffer).await { + if size == 0 || server.write_all(&buffer[..size]).await.is_err() { + break; + } + } + }); + Ok(Box::new(client)) + } + + async fn open_datagram( + &self, + session: DatagramSession, + ) -> Result, DispatchError> { + self.udp_sessions.lock().unwrap().push(session); + let mut transport = EchoDatagrams::new(); + transport.response_ready = Some(self.udp_response_ready.clone()); + Ok(Box::new(transport)) + } +} + +struct EchoDatagrams { + sender: mpsc::Sender, + receiver: mpsc::Receiver, + response_ready: Option>, +} + +struct BlockingDispatcher { + send_started: Arc, + send_count: Arc, + open_count: Arc, +} + +struct RecordingUdpDispatcher { + datagrams: Arc>>, + sent: Arc, +} + +struct RecordingUdpTransport { + datagrams: Arc>>, + sent: Arc, +} + +struct ReplayFairDispatcher { + response: Datagram, + send_count: Arc, + blocked_send_started: Arc, +} + +struct ReplayFairDatagrams { + response: Option, + send_count: Arc, + blocked_send_started: Arc, +} + +#[async_trait] +impl Dispatcher for RecordingUdpDispatcher { + async fn connect_tcp(&self, _session: StreamSession) -> Result { + Err(DispatchError::Other("unused TCP path".to_owned())) + } + + async fn open_datagram( + &self, + _session: DatagramSession, + ) -> Result, DispatchError> { + Ok(Box::new(RecordingUdpTransport { + datagrams: self.datagrams.clone(), + sent: self.sent.clone(), + })) + } +} + +#[async_trait] +impl DatagramTransport for RecordingUdpTransport { + async fn send(&mut self, datagram: Datagram) -> Result<(), DispatchError> { + self.datagrams.lock().unwrap().push(datagram); + self.sent.notify_one(); + Ok(()) + } + + async fn receive(&mut self) -> Result { + std::future::pending().await + } +} + +#[async_trait] +impl Dispatcher for ReplayFairDispatcher { + async fn connect_tcp(&self, _session: StreamSession) -> Result { + Err(DispatchError::Other("unused TCP path".to_owned())) + } + + async fn open_datagram( + &self, + _session: DatagramSession, + ) -> Result, DispatchError> { + Ok(Box::new(ReplayFairDatagrams { + response: Some(self.response.clone()), + send_count: self.send_count.clone(), + blocked_send_started: self.blocked_send_started.clone(), + })) + } +} + +#[async_trait] +impl DatagramTransport for ReplayFairDatagrams { + async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { + let send_index = self.send_count.fetch_add(1, Ordering::Relaxed); + if send_index == 0 { + return Ok(()); + } + self.blocked_send_started.notify_one(); + std::future::pending().await + } + + async fn receive(&mut self) -> Result { + if self.send_count.load(Ordering::Relaxed) != 0 + && let Some(response) = self.response.take() + { + return Ok(response); + } + std::future::pending().await + } +} + +#[async_trait] +impl Dispatcher for BlockingDispatcher { + async fn connect_tcp(&self, _session: StreamSession) -> Result { + Err(DispatchError::Other("unused TCP path".to_owned())) + } + + async fn open_datagram( + &self, + _session: DatagramSession, + ) -> Result, DispatchError> { + self.open_count.fetch_add(1, Ordering::Relaxed); + Ok(Box::new(BlockingDatagrams { + send_started: self.send_started.clone(), + send_count: Some(self.send_count.clone()), + })) + } +} + +struct BlockingDatagrams { + send_started: Arc, + send_count: Option>, +} + +#[derive(Default)] +struct DnsReplyDispatcher { + udp_sessions: Mutex>, +} + +#[async_trait] +impl Dispatcher for DnsReplyDispatcher { + async fn connect_tcp(&self, _session: StreamSession) -> Result { + Err(DispatchError::Other("unused TCP path".to_owned())) + } + + async fn open_datagram( + &self, + session: DatagramSession, + ) -> Result, DispatchError> { + self.udp_sessions.lock().unwrap().push(session); + Ok(Box::new(DnsReplyDatagrams::new())) + } +} + +struct DnsReplyDatagrams { + sender: mpsc::Sender, + receiver: mpsc::Receiver, +} + +impl DnsReplyDatagrams { + fn new() -> Self { + let (sender, receiver) = mpsc::channel(1); + Self { sender, receiver } + } +} + +#[async_trait] +impl DatagramTransport for DnsReplyDatagrams { + async fn send(&mut self, mut datagram: Datagram) -> Result<(), DispatchError> { + let query = classify_query(&datagram.payload) + .map_err(|error| DispatchError::Other(error.to_string()))?; + datagram.payload = synthesize_empty_response(&query, 0) + .map_err(|error| DispatchError::Other(error.to_string()))? + .into(); + self.sender + .send(datagram) + .await + .map_err(|_| DispatchError::Other("DNS reply transport stopped".to_owned())) + } + + async fn receive(&mut self) -> Result { + self.receiver + .recv() + .await + .ok_or_else(|| DispatchError::Other("DNS reply transport stopped".to_owned())) + } +} + +struct ResponseFirstDispatcher { + response: Datagram, +} + +struct SourceSelectiveDispatcher { + blocked_source: SocketAddr, + send_started: Arc, +} + +#[async_trait] +impl Dispatcher for ResponseFirstDispatcher { + async fn connect_tcp(&self, _session: StreamSession) -> Result { + Err(DispatchError::Other("unused TCP path".to_owned())) + } + + async fn open_datagram( + &self, + _session: DatagramSession, + ) -> Result, DispatchError> { + Ok(Box::new(ResponseFirstDatagrams { + response: Some(self.response.clone()), + })) + } +} + +#[async_trait] +impl Dispatcher for SourceSelectiveDispatcher { + async fn connect_tcp(&self, _session: StreamSession) -> Result { + Err(DispatchError::Other("unused TCP path".to_owned())) + } + + async fn open_datagram( + &self, + session: DatagramSession, + ) -> Result, DispatchError> { + if session.source == self.blocked_source { + Ok(Box::new(BlockingDatagrams { + send_started: self.send_started.clone(), + send_count: None, + })) + } else { + Ok(Box::new(EchoDatagrams::new())) + } + } +} + +struct ResponseFirstDatagrams { + response: Option, +} + +#[async_trait] +impl DatagramTransport for ResponseFirstDatagrams { + async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { + std::future::pending().await + } + + async fn receive(&mut self) -> Result { + if let Some(response) = self.response.take() { + return Ok(response); + } + std::future::pending().await + } +} + +#[async_trait] +impl DatagramTransport for BlockingDatagrams { + async fn send(&mut self, _datagram: Datagram) -> Result<(), DispatchError> { + if let Some(send_count) = &self.send_count { + send_count.fetch_add(1, Ordering::Relaxed); + } + self.send_started.notify_one(); + std::future::pending().await + } + + async fn receive(&mut self) -> Result { + std::future::pending().await + } +} + +impl EchoDatagrams { + fn new() -> Self { + let (sender, receiver) = mpsc::channel(1); + Self { + sender, + receiver, + response_ready: None, + } + } +} + +#[async_trait] +impl DatagramTransport for EchoDatagrams { + async fn send(&mut self, mut datagram: Datagram) -> Result<(), DispatchError> { + if datagram.payload.as_ref() == b"oversize-response" { + datagram.payload = Bytes::from(vec![0_u8; TUN_MTU]); + } + self.sender + .send(datagram) + .await + .map_err(|_| DispatchError::Other("echo transport stopped".to_owned())) + } + + async fn receive(&mut self) -> Result { + let datagram = self + .receiver + .recv() + .await + .ok_or_else(|| DispatchError::Other("echo transport stopped".to_owned()))?; + if let Some(ready) = &self.response_ready { + ready.notify_one(); + } + Ok(datagram) + } +} + +fn test_runtime_dns(dispatcher: Arc) -> Arc { + let config = DnsConfig { + enable: true, + ipv6: true, + nameservers: vec![DnsNameserver { + transport: DnsTransport::Udp, + address: Ipv4Addr::new(198, 51, 100, 53).into(), + port: 53, + route: DnsRoute::Direct, + }], + nameserver_policies: Vec::new(), + }; + let proxies = ProxyDispatchers::new(vec![dispatcher.clone()]).unwrap(); + let rules = RuleSet::compile(vec![RuleSpec { + kind: RuleKind::Match, + action: RuleAction::Direct, + no_resolve: false, + }]) + .unwrap(); + let limits = ResourceLimits::default(); + Arc::new(RuntimeDns::new_routed_proxies_with_cache_limits( + &config, + proxies, + dispatcher, + rules, + Arc::new(EmptyGeoMatcher), + limits.dns_address_cache_entries, + limits.dns_redir_host_entries, + )) +} + +#[tokio::test] +async fn synthetic_fd_with_dns_disabled_dispatches_tcp_and_reuses_udp_association() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let limits = ResourceLimits { + packet_queue_capacity: 8, + event_queue_capacity: 4, + tun_max_datagram_size: 1_400, + ..ResourceLimits::default() + }; + let runtime = TunRuntime::new( + tun, + limits, + dispatcher.clone(), + None, + true, + false, + Some(test_sniffer( + &[PortRange { + start: 8_080, + end: 8_080, + }], + &[], + &[], + )), + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + + let udp_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let udp_destination: SocketAddr = "198.51.100.20:53".parse().unwrap(); + + let mut oversized_ingress = vec![0_u8; TUN_MTU + 1]; + oversized_ingress[0] = 0x45; + peer.send(&oversized_ingress).await.unwrap(); + + peer.send(&build_udp( + udp_source, + udp_destination, + b"oversize-response", + )) + .await + .unwrap(); + let mut dropped = [0_u8; TUN_MTU]; + assert!( + timeout(Duration::from_millis(100), peer.recv(&mut dropped)) + .await + .is_err(), + "oversized UDP response unexpectedly reached TUN", + ); + + for destination in ["198.51.100.20:53", "203.0.113.30:443"] { + let destination: SocketAddr = destination.parse().unwrap(); + peer.send(&build_udp(udp_source, destination, b"query")) + .await + .unwrap(); + let mut response = [0_u8; TUN_MTU]; + let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) + .await + .expect("UDP response timed out") + .unwrap(); + assert_udp_response(&response[..size], destination, udp_source, b"query"); + } + assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert_eq!( + dispatcher.udp_sessions.lock().unwrap()[0], + DatagramSession::for_tun(udp_source, 1_400) + ); + + let tcp_source: SocketAddr = "192.0.2.11:13000".parse().unwrap(); + let tcp_destination: SocketAddr = "198.51.100.21:8080".parse().unwrap(); + peer.send(&build_tcp_syn(tcp_source, tcp_destination)) + .await + .unwrap(); + let mut syn_ack = [0_u8; TUN_MTU]; + let size = timeout(Duration::from_secs(2), peer.recv(&mut syn_ack)) + .await + .expect("TCP SYN-ACK timed out") + .unwrap(); + assert!(size >= 40); + assert_eq!(syn_ack[20 + 13] & 0x12, 0x12); + let server_sequence = u32::from_be_bytes(syn_ack[24..28].try_into().unwrap()); + let request = b"GET / HTTP/1.1\r\nHost: Sniff.Example.COM\r\n\r\n"; + peer.send(&build_tcp_segment( + tcp_source, + tcp_destination, + 2, + server_sequence.wrapping_add(1), + 0x18, + request, + )) + .await + .unwrap(); + timeout(Duration::from_secs(2), async { + loop { + if !dispatcher.tcp_sessions.lock().unwrap().is_empty() { + break; + } + dispatcher.tcp_called.notified().await; + } + }) + .await + .expect("TCP dispatcher was not called"); + assert_eq!( + dispatcher.tcp_sessions.lock().unwrap()[0], + StreamSession { + inbound: InboundKind::Tun, + source: tcp_source, + destination: Destination::Ip(tcp_destination), + sniffed_domain: Some("sniff.example.com".to_owned()), + } + ); + let echoed = timeout(Duration::from_secs(2), async { + let mut echoed = Vec::with_capacity(request.len()); + while echoed.len() < request.len() { + let size = peer.recv(&mut syn_ack).await.unwrap(); + if size < 40 || syn_ack[9] != 6 { + continue; + } + let ip_header_length = usize::from(syn_ack[0] & 0x0f) * 4; + let tcp_header_length = usize::from(syn_ack[ip_header_length + 12] >> 4) * 4; + let payload_offset = ip_header_length + tcp_header_length; + if payload_offset < size { + echoed.extend_from_slice(&syn_ack[payload_offset..size]); + } + } + echoed + }) + .await + .expect("sniffed TCP prefix was not replayed through the outbound"); + assert_eq!(echoed, request); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("TUN runtime stop timed out") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn unconfigured_tls_port_dispatches_without_reading_a_prefix() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let limits = ResourceLimits { + packet_queue_capacity: 4, + event_queue_capacity: 2, + tun_max_datagram_size: TUN_MTU, + ..ResourceLimits::default() + }; + let runtime = TunRuntime::new( + tun, + limits, + dispatcher.clone(), + None, + true, + false, + Some(test_sniffer( + &[], + &[PortRange { + start: 8_443, + end: 8_443, + }], + &[], + )), + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + + let source: SocketAddr = "192.0.2.12:14000".parse().unwrap(); + let destination: SocketAddr = "198.51.100.22:443".parse().unwrap(); + peer.send(&build_tcp_syn(source, destination)) + .await + .unwrap(); + let mut syn_ack = [0_u8; TUN_MTU]; + let size = timeout(Duration::from_secs(2), peer.recv(&mut syn_ack)) + .await + .expect("TCP SYN-ACK timed out") + .unwrap(); + assert!(size >= 40); + assert_eq!(syn_ack[20 + 13] & 0x12, 0x12); + let server_sequence = u32::from_be_bytes(syn_ack[24..28].try_into().unwrap()); + + // Complete the handshake without sending any TLS bytes. A mistakenly + // enabled sniffer would wait for its 200 ms read deadline here. + peer.send(&build_tcp_segment( + source, + destination, + 2, + server_sequence.wrapping_add(1), + 0x10, + &[], + )) + .await + .unwrap(); + timeout(Duration::from_millis(100), async { + loop { + if !dispatcher.tcp_sessions.lock().unwrap().is_empty() { + break; + } + dispatcher.tcp_called.notified().await; + } + }) + .await + .expect("unconfigured TLS port delayed dispatch for a prefix read"); + assert_eq!( + dispatcher.tcp_sessions.lock().unwrap()[0], + StreamSession { + inbound: InboundKind::Tun, + source, + destination: Destination::Ip(destination), + sniffed_domain: None, + } + ); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("TUN runtime stop timed out") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn enabled_dns_fast_path_replies_without_opening_a_tun_udp_association() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let dns_dispatcher = Arc::new(DnsReplyDispatcher::default()); + let dns = test_runtime_dns(dns_dispatcher.clone()); + let limits = ResourceLimits { + packet_queue_capacity: 16, + event_queue_capacity: 8, + tun_max_datagram_size: TUN_MTU, + ..ResourceLimits::default() + }; + let runtime = TunRuntime::new( + tun, + limits, + dispatcher.clone(), + Some(dns), + true, + false, + Some(test_sniffer(&[], &[], &[PortRange { start: 53, end: 53 }])), + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + peer.send(&build_udp(source, requested_server, b"invalid")) + .await + .unwrap(); + let mut response = [0_u8; TUN_MTU]; + assert!( + timeout(Duration::from_millis(100), peer.recv(&mut response)) + .await + .is_err(), + "malformed DNS query unexpectedly produced a response", + ); + assert!(dispatcher.udp_sessions.lock().unwrap().is_empty()); + assert!(dns_dispatcher.udp_sessions.lock().unwrap().is_empty()); + + let query = build_query(0x1234, "example.com", QueryType::A).unwrap(); + peer.send(&build_udp(source, requested_server, &query)) + .await + .unwrap(); + let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) + .await + .expect("TUN DNS response timed out") + .unwrap(); + assert!(size > 30); + assert_eq!(response[9], 17); + assert_eq!( + u16::from_be_bytes(response[20..22].try_into().unwrap()), + requested_server.port() + ); + assert_eq!( + u16::from_be_bytes(response[22..24].try_into().unwrap()), + source.port() + ); + assert_ne!(response[30] & 0x80, 0); + assert_eq!(&response[28..30], &0x1234_u16.to_be_bytes()); + assert!(dispatcher.udp_sessions.lock().unwrap().is_empty()); + assert_eq!(dns_dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert_eq!( + dns_dispatcher.udp_sessions.lock().unwrap()[0].inbound, + InboundKind::InternalDns + ); + + let ordinary_destination: SocketAddr = "203.0.113.30:443".parse().unwrap(); + peer.send(&build_udp(source, ordinary_destination, b"ordinary")) + .await + .unwrap(); + let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) + .await + .expect("ordinary UDP response timed out") + .unwrap(); + assert_udp_response(&response[..size], ordinary_destination, source, b"ordinary"); + assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("TUN runtime stop timed out") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn queued_dns_response_is_bounded_and_a_full_queue_drops_the_new_response() { + let dns = test_runtime_dns(Arc::new(DnsReplyDispatcher::default())); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let request = UdpDatagram::new( + source, + requested_server, + build_query(0x2100, "queued.example", QueryType::A).unwrap(), + ); + let (responses, mut queued) = mpsc::channel(1); + let resource_stats = RuntimeResourceStats::new("tun_runtime_test"); + + let permit = dns.begin_query(); + run_tun_dns_query( + dns.clone(), + permit, + request, + responses.clone(), + TUN_MTU, + resource_stats.clone(), + CancellationToken::new(), + ) + .await; + let response = queued.recv().await.unwrap(); + assert_eq!(response.source, requested_server); + assert_eq!(response.destination, source); + drop(response); + + responses + .send(QueuedUdpResponse::ordinary(UdpDatagram::new( + requested_server, + source, + b"occupied".as_slice(), + ))) + .await + .unwrap(); + let request = UdpDatagram::new( + source, + requested_server, + build_query(0x2101, "full.example", QueryType::A).unwrap(), + ); + let permit = dns.begin_query(); + run_tun_dns_query( + dns.clone(), + permit, + request, + responses, + TUN_MTU, + resource_stats.clone(), + CancellationToken::new(), + ) + .await; + assert_eq!(resource_stats.snapshot().dns_queue_drops, 1); + assert_eq!(&queued.recv().await.unwrap().payload[..], b"occupied"); +} + +#[tokio::test] +async fn ordinary_and_dns_responses_have_independent_queue_capacity() { + let dns = test_runtime_dns(Arc::new(DnsReplyDispatcher::default())); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let stats = RuntimeResourceStats::new("tun_runtime_test"); + let (ordinary_tx, mut ordinary_rx) = mpsc::channel(1); + let (dns_tx, mut dns_rx) = mpsc::channel(1); + + ordinary_tx + .send(QueuedUdpResponse::ordinary(UdpDatagram::new( + server, + source, + b"ordinary-occupied".as_slice(), + ))) + .await + .unwrap(); + let permit = dns.begin_query(); + try_queue_tun_dns_response( + &dns_tx, + UdpDatagram::new(server, source, b"dns-independent".as_slice()), + Some(permit), + &stats, + ); + + let dns_response = dns_rx.recv().await.unwrap(); + assert_eq!(&dns_response.payload[..], b"dns-independent"); + drop(dns_response); + assert_eq!( + &ordinary_rx.recv().await.unwrap().payload[..], + b"ordinary-occupied" + ); +} + +#[tokio::test] +async fn full_ordinary_response_queue_drops_without_refreshing_activity() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let server: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let stats = RuntimeResourceStats::new("tun_runtime_test"); + let last_activity = AtomicU64::new(5); + let (responses, mut queued) = mpsc::channel(1); + + assert!(matches!( + try_queue_tun_udp_response( + &responses, + UdpDatagram::new(server, source, b"queued".as_slice()), + &last_activity, + 100, + &stats, + ), + ResponseQueueResult::Queued + )); + assert_eq!(last_activity.load(Ordering::Acquire), 100); + assert!(matches!( + try_queue_tun_udp_response( + &responses, + UdpDatagram::new(server, source, b"dropped".as_slice()), + &last_activity, + 110, + &stats, + ), + ResponseQueueResult::Dropped + )); + assert_eq!(last_activity.load(Ordering::Acquire), 100); + assert_eq!(stats.snapshot().udp_queue_drops, 1); + assert_eq!(stats.snapshot().udp_association_queue_drops, 0); + assert_eq!(stats.snapshot().udp_response_queue_drops, 1); + assert_eq!(&queued.recv().await.unwrap().payload[..], b"queued"); +} + +#[tokio::test] +async fn dns_fast_path_allows_sixteen_concurrent_sources_and_keeps_udp_responsive() { + const QUERY_COUNT: usize = 16; + + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let send_started = Arc::new(Notify::new()); + let send_count = Arc::new(AtomicUsize::new(0)); + let open_count = Arc::new(AtomicUsize::new(0)); + let dns_dispatcher = Arc::new(BlockingDispatcher { + send_started, + send_count: send_count.clone(), + open_count: open_count.clone(), + }); + let dns = test_runtime_dns(dns_dispatcher); + let limits = ResourceLimits { + packet_queue_capacity: 64, + event_queue_capacity: 32, + tun_max_datagram_size: TUN_MTU, + ..ResourceLimits::default() + }; + let runtime = TunRuntime::new( + tun, + limits, + dispatcher.clone(), + Some(dns), + true, + false, + None, + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + + let malformed_source: SocketAddr = "192.0.2.10:11999".parse().unwrap(); + peer.send(&build_udp(malformed_source, requested_server, b"invalid")) + .await + .unwrap(); + + for index in 0..QUERY_COUNT { + let source = SocketAddr::new( + Ipv4Addr::new(192, 0, 2, 10).into(), + 12_000 + u16::try_from(index).unwrap(), + ); + let domain = format!("stall-{index}.example"); + let query = build_query( + 0x1000 + u16::try_from(index).unwrap(), + &domain, + QueryType::A, + ) + .unwrap(); + peer.send(&build_udp(source, requested_server, &query)) + .await + .unwrap(); + } + timeout(Duration::from_secs(2), async { + while send_count.load(Ordering::Relaxed) < QUERY_COUNT { + tokio::task::yield_now().await; + } + }) + .await + .expect("the DNS queries did not reach their stalled upstreams"); + assert!(dispatcher.udp_sessions.lock().unwrap().is_empty()); + assert_eq!(open_count.load(Ordering::Relaxed), QUERY_COUNT); + + let ordinary_source: SocketAddr = "192.0.2.99:13000".parse().unwrap(); + let ordinary_destination: SocketAddr = "203.0.113.30:443".parse().unwrap(); + peer.send(&build_udp( + ordinary_source, + ordinary_destination, + b"still-responsive", + )) + .await + .unwrap(); + let mut response = [0_u8; TUN_MTU]; + let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) + .await + .expect("stalled DNS queries blocked ordinary UDP") + .unwrap(); + assert_udp_response( + &response[..size], + ordinary_destination, + ordinary_source, + b"still-responsive", + ); + + assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert_eq!(open_count.load(Ordering::Relaxed), QUERY_COUNT); + assert_eq!(send_count.load(Ordering::Relaxed), QUERY_COUNT); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("stalled DNS tasks prevented the TUN stop barrier") + .unwrap() + .unwrap(); +} + +#[test] +fn oversized_tun_dns_response_is_dropped() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let requested_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let query = build_query(0x3456, "example.com", QueryType::A).unwrap(); + let request = UdpDatagram::new(source, requested_server, query.clone()); + let tun_mtu = 1_400; + let ceiling = + usize::from(DatagramSession::for_tun(source, tun_mtu).max_response_payload_size()); + assert!(complete_tun_dns_response(&request, vec![0_u8; ceiling], tun_mtu).is_some()); + assert!(complete_tun_dns_response(&request, vec![0_u8; ceiling + 1], tun_mtu).is_none()); +} + +#[test] +fn ipv6_tun_dns_responses_preserve_the_requested_server_and_client_endpoints() { + let source: SocketAddr = "[2001:db8::10]:12000".parse().unwrap(); + let requested_server: SocketAddr = "[2001:db8::53]:53".parse().unwrap(); + let query = build_query(0x4567, "example.com", QueryType::Aaaa).unwrap(); + let classified = classify_query(&query).unwrap(); + let request = UdpDatagram::new(source, requested_server, query); + let response = complete_tun_dns_response( + &request, + synthesize_empty_response(&classified, 0).unwrap(), + TUN_MTU, + ) + .unwrap(); + assert_eq!(response.source, requested_server); + assert_eq!(response.destination, source); + let parsed = crate::dns::parse_response(&response.payload).unwrap(); + assert_eq!(parsed.id, classified.id); + + let servfail = tun_dns_servfail_response(&request, &classified); + assert_eq!(servfail.source, requested_server); + assert_eq!(servfail.destination, source); + assert_eq!( + u16::from_be_bytes([servfail.payload[2], servfail.payload[3]]) & 0x000f, + 2 + ); +} + +#[tokio::test] +async fn cancellation_interrupts_a_blocked_udp_send_and_completes_the_stop_barrier() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let send_started = Arc::new(Notify::new()); + let dispatcher = Arc::new(BlockingDispatcher { + send_started: send_started.clone(), + send_count: Arc::new(AtomicUsize::new(0)), + open_count: Arc::new(AtomicUsize::new(0)), + }); + let runtime = TunRuntime::new( + tun, + ResourceLimits::default(), + dispatcher, + None, + true, + false, + None, + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + + peer.send(&build_udp( + "192.0.2.10:12000".parse().unwrap(), + "198.51.100.20:53".parse().unwrap(), + b"block", + )) + .await + .unwrap(); + timeout(Duration::from_secs(2), send_started.notified()) + .await + .expect("UDP transport send was not polled"); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("blocked UDP send prevented the TUN stop barrier") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn fresh_udp_source_beyond_the_old_sixty_four_limit_is_accepted() { + const OLD_UDP_LIMIT: usize = 64; + + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let send_started = Arc::new(Notify::new()); + let send_count = Arc::new(AtomicUsize::new(0)); + let open_count = Arc::new(AtomicUsize::new(0)); + let dispatcher = Arc::new(BlockingDispatcher { + send_started, + send_count: send_count.clone(), + open_count: open_count.clone(), + }); + let limits = ResourceLimits { + packet_queue_capacity: 128, + event_queue_capacity: 128, + tun_max_datagram_size: TUN_MTU, + ..ResourceLimits::default() + }; + let runtime = TunRuntime::new(tun, limits, dispatcher, None, true, false, None).unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + + for index in 0..OLD_UDP_LIMIT { + let source = SocketAddr::new( + Ipv4Addr::new(192, 0, 2, 10).into(), + 12_000 + u16::try_from(index).unwrap(), + ); + peer.send(&build_udp(source, destination, b"held")) + .await + .unwrap(); + timeout(Duration::from_secs(2), async { + while open_count.load(Ordering::Acquire) <= index { + tokio::task::yield_now().await; + } + }) + .await + .expect("a UDP association was not opened"); + } + assert_eq!(send_count.load(Ordering::Acquire), OLD_UDP_LIMIT); + assert_eq!(open_count.load(Ordering::Acquire), OLD_UDP_LIMIT); + + let sixty_fifth_source: SocketAddr = "192.0.2.99:13000".parse().unwrap(); + peer.send(&build_udp(sixty_fifth_source, destination, b"accepted")) + .await + .unwrap(); + timeout(Duration::from_secs(2), async { + while open_count.load(Ordering::Acquire) == OLD_UDP_LIMIT { + tokio::task::yield_now().await; + } + }) + .await + .expect("the sixty-fifth fresh UDP source was not accepted"); + assert_eq!(open_count.load(Ordering::Acquire), OLD_UDP_LIMIT + 1); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("TUN runtime stop timed out") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn queued_udp_response_is_drained_before_a_same_source_ingress_burst() { + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let dispatcher = Arc::new(ResponseFirstDispatcher { + response: Datagram { + remote: Destination::Ip(server), + payload: Bytes::from_static(b"response"), + sniffed_domain: None, + }, + }); + let (inbound_tx, mut inbound_rx) = mpsc::channel(1); + inbound_tx + .send(UdpDatagram::new(source, server, b"next-query".as_slice())) + .await + .unwrap(); + let (responses_tx, mut responses_rx) = mpsc::channel(1); + let cancellation = CancellationToken::new(); + let child = cancellation.clone(); + let task = tokio::spawn(async move { + run_udp_association_inner( + &mut inbound_rx, + &UdpAssociationTaskContext { + association_id: 1, + source, + responses: responses_tx, + dispatcher, + resource_stats: RuntimeResourceStats::new("tun_runtime_test"), + association_clock: AssociationClock::realtime(), + last_activity: Arc::new(AtomicU64::new(0)), + tun_mtu: TUN_MTU, + sniffer: None, + cancellation: child, + }, + ) + .await + }); + + let response = timeout(Duration::from_secs(1), responses_rx.recv()) + .await + .expect("ready response was starved behind the same-source query") + .expect("response channel closed"); + assert_eq!(response.source, server); + assert_eq!(response.destination, source); + assert_eq!(&response.payload[..], b"response"); + + cancellation.cancel(); + timeout(Duration::from_secs(1), task) + .await + .expect("association did not stop") + .unwrap() + .unwrap(); + drop(inbound_tx); +} + +#[tokio::test] +async fn full_udp_association_queue_does_not_block_other_sources() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let fd = TunFd::duplicate_mock(host.as_raw_fd()).unwrap(); + let tun = TunIo::new(fd, crate::TunFraming::RawIp).unwrap(); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let blocked_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let responsive_source: SocketAddr = "192.0.2.11:12001".parse().unwrap(); + let destination: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let send_started = Arc::new(Notify::new()); + let dispatcher = Arc::new(SourceSelectiveDispatcher { + blocked_source, + send_started: send_started.clone(), + }); + let limits = ResourceLimits { + packet_queue_capacity: 64, + event_queue_capacity: 64, + tun_udp_association_queue_capacity: 2, + tun_udp_response_queue_capacity: 4, + tun_max_datagram_size: TUN_MTU, + ..ResourceLimits::default() + }; + let runtime = TunRuntime::new(tun, limits, dispatcher, None, true, false, None).unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + + peer.send(&build_udp(blocked_source, destination, b"block")) + .await + .unwrap(); + timeout(Duration::from_secs(2), send_started.notified()) + .await + .expect("blocked association did not enter outbound send"); + + for _ in 0..=limits.tun_udp_association_queue_capacity { + peer.send(&build_udp(blocked_source, destination, b"queued")) + .await + .unwrap(); + tokio::task::yield_now().await; + } + peer.send(&build_udp( + responsive_source, + destination, + b"still-responsive", + )) + .await + .unwrap(); + + let mut response = [0_u8; TUN_MTU]; + let size = timeout(Duration::from_secs(2), peer.recv(&mut response)) + .await + .expect("a full source queue blocked another UDP association") + .unwrap(); + assert_udp_response( + &response[..size], + destination, + responsive_source, + b"still-responsive", + ); + + cancellation.cancel(); + timeout(Duration::from_secs(2), task) + .await + .expect("TUN runtime stop timed out") + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn blocked_udp_response_does_not_block_ingress_or_stop() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(); + let limits = ResourceLimits { + packet_queue_capacity: 1, + ..ResourceLimits::default() + }; + let destination: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let first_source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let second_source: SocketAddr = "192.0.2.11:12001".parse().unwrap(); + let held = build_udp(destination, first_source, b"held"); + let held_count = fill_tun_egress(&host, &held); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let dispatcher = Arc::new(MockDispatcher::default()); + let stats = Arc::new(TunTrafficStats::default()); + let runtime = TunRuntime::new_with_stats( + tun, + limits, + dispatcher.clone(), + None, + true, + false, + None, + stats.clone(), + ) + .unwrap(); + let cancellation = CancellationToken::new(); + let task = tokio::spawn(runtime.run(cancellation.clone())); + peer.send(&build_udp(first_source, destination, b"first")) + .await + .unwrap(); + let first_reply = timeout( + Duration::from_secs(1), + dispatcher.udp_response_ready.notified(), + ) + .await; + peer.send(&build_udp(second_source, destination, b"second")) + .await + .unwrap(); + let ingress_progressed = timeout(Duration::from_secs(1), async { + while dispatcher.udp_sessions.lock().unwrap().len() < 2 { + tokio::task::yield_now().await; + } + }) + .await + .is_ok(); + + cancellation.cancel(); + timeout(Duration::from_secs(1), task) + .await + .expect("full platform output prevented the TUN stop barrier") + .unwrap() + .unwrap(); + assert!( + first_reply.is_ok(), + "first source did not produce a response" + ); + assert!(ingress_progressed, "blocked reply starved UDP ingress"); + assert_eq!(stats.snapshot().down_total, 0); + let mut received = [0_u8; TUN_MTU]; + for _ in 0..held_count { + let size = peer.try_recv(&mut received).unwrap(); + assert_eq!(&received[..size], held.as_slice()); + } + assert!( + peer.try_recv(&mut received).is_err(), + "packet emitted after Stop" + ); +} + +fn fill_tun_egress(host: &UnixDatagram, packet: &[u8]) -> usize { + let mut count = 0; + loop { + match host.send(packet) { + Ok(size) => { + assert_eq!(size, packet.len()); + count += 1; + assert!(count < 65_536, "local packet queue did not become full"); + } + Err(error) + if error.kind() == io::ErrorKind::WouldBlock + || error.raw_os_error() == Some(libc::ENOBUFS) => + { + return count; + } + Err(error) => panic!("unexpected local packet queue error: {error}"), + } + } +} + +#[tokio::test] +async fn full_tcp_ingress_drops_only_that_packet_and_keeps_udp_and_dns_responsive() { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = Arc::new( + TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(), + ); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let server: SocketAddr = "198.51.100.20:443".parse().unwrap(); + // Cache readable readiness before creating the spawned driver. The next + // reader poll can then consume real packets synchronously on this thread. + peer.send(&build_udp(source, server, b"warmup")).unwrap(); + tun.read_packet(&mut Vec::new()).await.unwrap(); + let limits = ResourceLimits { + packet_queue_capacity: 1, + ..ResourceLimits::default() + }; + let mut parts = NetStack::start_tcp(tun_netstack_config(limits, false)).unwrap(); + let held_source: SocketAddr = "192.0.2.20:13000".parse().unwrap(); + let retry_source: SocketAddr = "192.0.2.21:13001".parse().unwrap(); + // There is deliberately no await from start_tcp through the first reader + // poll: the current-thread driver cannot free this single ingress slot. + parts + .packet_sink + .try_send(build_tcp_syn(held_source, server)) + .unwrap(); + let dropped = build_tcp_syn(retry_source, server); + let ordinary = build_udp(source, server, b"responsive"); + let dns_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let query = build_query(0x2810, "responsive.example", QueryType::A).unwrap(); + let dns_packet = build_udp(source, dns_server, &query); + for packet in [&dropped, &ordinary, &dns_packet] { + peer.send(packet).unwrap(); + } + let resources = RuntimeResourceStats::new("full_tcp_ingress_test"); + let traffic = Arc::new(TunTrafficStats::default()); + let dispatcher = Arc::new(MockDispatcher::default()); + let dns_dispatcher = Arc::new(DnsReplyDispatcher::default()); + let cancellation = CancellationToken::new(); + let (udp, mut ordinary_rx, mut dns_rx) = UdpIngress::new( + UdpIngressContext { + dispatcher: dispatcher.clone(), + dns: Some(test_runtime_dns(dns_dispatcher.clone())), + sniffer: None, + limits, + resource_stats: resources.clone(), + }, + cancellation.clone(), + ); + let mut reader = Box::pin(tun_read_loop( + tun, + parts.packet_sink.clone(), + true, + udp, + traffic.clone(), + cancellation.clone(), + )); + let pending = + std::future::poll_fn(|cx| Poll::Ready(reader.as_mut().poll(cx).is_pending())).await; + let dropped_packets = resources.snapshot().packet_queue_drops; + let consumed_bytes = traffic.snapshot().up_total; + let task = tokio::spawn(reader); + let responses = timeout(Duration::from_secs(1), async { + ( + ordinary_rx.recv().await.unwrap(), + dns_rx.recv().await.unwrap(), + ) + }) + .await; + cancellation.cancel(); + let stopped = timeout(Duration::from_secs(1), task).await; + let driver_still_running = !parts.control.is_stopped(); + // Re-offering the dropped SYN after capacity is freed is an ordinary TCP + // retransmission; no extra pending queue or association is required. + let retry = if responses.is_ok() && stopped.is_ok() { + timeout(Duration::from_secs(1), async { + let first = parts.packet_stream.recv().await.unwrap(); + assert_eq!(first.data()[22..24], held_source.port().to_be_bytes()); + parts.packet_sink.send(dropped.clone()).await.unwrap(); + parts.packet_stream.recv().await.unwrap() + }) + .await + .ok() + } else { + None + }; + parts.control.stop().await; + stopped + .expect("reader Stop waited for TCP capacity or driver shutdown") + .unwrap() + .unwrap(); + assert!(pending, "reader exited before its cancellation"); + assert_eq!(dropped_packets, 1); + assert_eq!( + consumed_bytes, + (dropped.len() + ordinary.len() + dns_packet.len()) as u64 + ); + let (ordinary_response, dns_response) = responses.expect("TCP Full blocked UDP or DNS"); + assert_eq!(&ordinary_response.payload[..], b"responsive"); + assert_eq!(ordinary_response.source, server); + assert_eq!(dns_response.source, dns_server); + assert_eq!(dns_response.destination, source); + assert_eq!( + crate::dns::parse_response(&dns_response.payload) + .unwrap() + .id, + 0x2810 + ); + assert_eq!(dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert_eq!(dns_dispatcher.udp_sessions.lock().unwrap().len(), 1); + assert!( + driver_still_running, + "fixture stopped TCP before reader cleanup" + ); + let retried = retry.expect("retransmitted SYN did not resume its handshake"); + assert_eq!(retried.data()[9], 6); + assert_eq!(retried.data()[22..24], retry_source.port().to_be_bytes()); + assert_eq!(retried.data()[33] & 0x12, 0x12); +} + +#[tokio::test] +async fn tun_writer_drains_remaining_lanes_and_drops_only_invalid_datagrams() { + let server: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + for close_ordinary in [true, false] { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = Arc::new( + TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(), + ); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let parts = NetStack::start_tcp(NetStackConfig::default()).unwrap(); + // A closed raw lane must not discard either remaining UDP lane. + parts.control.stop().await; + let (ordinary_tx, ordinary_rx) = mpsc::channel(3); + let (dns_tx, dns_rx) = mpsc::channel(3); + let remaining = if close_ordinary { + drop(ordinary_tx); + dns_tx + } else { + drop(dns_tx); + ordinary_tx + }; + for datagram in [ + UdpDatagram::new(server, source, Bytes::from(vec![0_u8; TUN_MTU])), + UdpDatagram::new( + server, + "[2001:db8::1]:12000".parse().unwrap(), + &b"mixed"[..], + ), + UdpDatagram::new(server, source, &b"valid"[..]), + ] { + remaining + .try_send(QueuedUdpResponse::ordinary(datagram)) + .unwrap(); + } + drop(remaining); + let result = timeout( + Duration::from_secs(1), + tun_write_loop( + tun, + parts.packet_stream, + ordinary_rx, + dns_rx, + TUN_MTU, + Arc::new(TunTrafficStats::default()), + CancellationToken::new(), + ), + ) + .await; + result + .expect("writer did not drain its remaining queue") + .unwrap(); + let mut response = [0_u8; TUN_MTU]; + let size = timeout(Duration::from_secs(1), peer.recv(&mut response)) + .await + .expect("valid response was discarded") + .unwrap(); + assert_udp_response(&response[..size], server, source, b"valid"); + assert_eq!(size, 28 + b"valid".len()); + assert!(peer.try_recv(&mut response).is_err(), "response replayed"); + } +} + +#[tokio::test] +async fn tun_writer_services_ready_raw_ordinary_and_dns_lanes_fairly_across_batches() { + const PER_LANE: u8 = 9; + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = Arc::new( + TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(), + ); + let peer = tokio::net::UnixDatagram::from_std(peer).unwrap(); + let parts = NetStack::start_tcp(NetStackConfig { + fake_icmp_echo: true, + ..NetStackConfig::default() + }) + .unwrap(); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + let ordinary_server: SocketAddr = "198.51.100.20:443".parse().unwrap(); + let dns_server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let (ordinary_tx, ordinary_rx) = mpsc::channel(usize::from(PER_LANE)); + let (dns_tx, dns_rx) = mpsc::channel(usize::from(PER_LANE)); + for index in 0..PER_LANE { + parts + .packet_sink + .send(build_icmp_echo(index, &[0, index])) + .await + .unwrap(); + ordinary_tx + .try_send(QueuedUdpResponse::ordinary(UdpDatagram::new( + ordinary_server, + source, + Bytes::from(vec![1, index]), + ))) + .unwrap(); + dns_tx + .try_send(QueuedUdpResponse::ordinary(UdpDatagram::new( + dns_server, + source, + Bytes::from(vec![2, index]), + ))) + .unwrap(); + } + timeout(Duration::from_secs(1), async { + while parts.stats.snapshot().icmp_replied < usize::from(PER_LANE) { + tokio::task::yield_now().await; + } + }) + .await + .expect("raw responses were not all ready before writer admission"); + drop((ordinary_tx, dns_tx)); + let cancellation = CancellationToken::new(); + let stats = Arc::new(TunTrafficStats::default()); + let task = tokio::spawn(tun_write_loop( + tun, + parts.packet_stream, + ordinary_rx, + dns_rx, + TUN_MTU, + stats.clone(), + cancellation.clone(), + )); + let received = timeout(Duration::from_secs(2), async { + let mut packets = Vec::new(); + let mut packet = [0_u8; TUN_MTU]; + for _ in 0..usize::from(PER_LANE) * 3 { + let size = peer.recv(&mut packet).await.unwrap(); + packets.push(packet[..size].to_vec()); + } + packets + }) + .await; + parts.control.stop().await; + if received.is_err() { + cancellation.cancel(); + } + timeout(Duration::from_secs(1), task) + .await + .expect("writer did not drain its remaining lanes") + .unwrap() + .unwrap(); + let packets = received.expect("a ready output lane was starved"); + for (offset, packet) in packets.iter().enumerate() { + let index = u8::try_from(offset / 3).unwrap(); + match offset % 3 { + 0 => { + assert_eq!(packet[9], 1, "raw lane lost its turn"); + assert_eq!(packet[20], 0); + assert_eq!(&packet[28..], &[0, index]); + } + 1 => assert_udp_response(packet, ordinary_server, source, &[1, index]), + _ => assert_udp_response(packet, dns_server, source, &[2, index]), + } + } + assert_eq!( + stats.snapshot().down_total, + packets + .iter() + .map(|packet| packet.len() as u64) + .sum::() + ); + let mut packet = [0_u8; TUN_MTU]; + assert!(peer.try_recv(&mut packet).is_err(), "output replayed"); +} + +#[tokio::test] +async fn tun_writer_retains_dns_permit_until_platform_completion_or_cancelled_send_is_joined() { + let server: SocketAddr = "198.51.100.20:53".parse().unwrap(); + let source: SocketAddr = "192.0.2.10:12000".parse().unwrap(); + for (cancel_send, accepted_prefix) in [(true, false), (false, false), (true, true)] { + let (host, peer) = UnixDatagram::pair().unwrap(); + host.set_nonblocking(true).unwrap(); + peer.set_nonblocking(true).unwrap(); + let tun = Arc::new( + TunIo::new( + TunFd::duplicate_mock(host.as_raw_fd()).unwrap(), + crate::TunFraming::RawIp, + ) + .unwrap(), + ); + let held = build_udp(server, source, b"held"); + let held_count = if accepted_prefix { + // Keep a cached writable observation before filling the peer. One + // freed packet slot will accept the ordinary prefix, not the DNS + // suffix, when the same writer future is polled below. + tun.write_packet(&held).await.unwrap(); + 1 + fill_tun_egress(&host, &held) + } else { + fill_tun_egress(&host, &held) + }; + if accepted_prefix { + let mut removed = [0_u8; TUN_MTU]; + let size = peer.recv(&mut removed).unwrap(); + assert_eq!(&removed[..size], held.as_slice()); + } + let parts = NetStack::start_tcp(NetStackConfig::default()).unwrap(); + let dns = test_runtime_dns(Arc::new(DnsReplyDispatcher::default())); + let probe = observation::ResourceProbe::default(); + let permit = probe.scope_sync(|| dns.begin_query()); + let (ordinary_tx, ordinary_rx) = mpsc::channel(1); + let (dns_tx, dns_rx) = mpsc::channel(1); + if accepted_prefix { + ordinary_tx + .try_send(QueuedUdpResponse::ordinary(UdpDatagram::new( + server, + source, + &b"sent"[..], + ))) + .unwrap(); + } + dns_tx + .try_send(QueuedUdpResponse::dns( + UdpDatagram::new(server, source, &b"response"[..]), + permit, + )) + .unwrap(); + let cancellation = CancellationToken::new(); + let stats = Arc::new(TunTrafficStats::default()); + let mut writer = Box::pin(tun_write_loop( + tun, + parts.packet_stream, + ordinary_rx, + dns_rx, + TUN_MTU, + stats.clone(), + cancellation.clone(), + )); + let pending = + std::future::poll_fn(|cx| Poll::Ready(writer.as_mut().poll(cx).is_pending())).await; + let active_waiters = probe.snapshot().current(observation::ResourceKind::Waiter); + let mut received = [0_u8; TUN_MTU]; + if cancel_send { + cancellation.cancel(); + } else { + // Capacity becoming available completes the very same send; + // neither raw enqueue nor dequeue may release its permit early. + for _ in 0..held_count { + let size = peer.recv(&mut received).unwrap(); + assert_eq!(&received[..size], held.as_slice()); + } + drop((ordinary_tx, dns_tx)); + parts.control.stop().await; + } + let joined = timeout(Duration::from_secs(1), writer).await; + parts.control.stop().await; + assert!(pending, "writer did not wait for platform output capacity"); + assert_eq!(active_waiters, 1, "DNS permit was released before delivery"); + joined + .expect("writer cancellation waited for netstack stop") + .unwrap(); + assert!(probe.snapshot().is_idle(), "writer retained a DNS permit"); + if cancel_send { + for _ in 0..held_count - usize::from(accepted_prefix) { + let size = peer.recv(&mut received).unwrap(); + assert_eq!(&received[..size], held.as_slice()); + } + if accepted_prefix { + let size = peer.recv(&mut received).unwrap(); + assert_udp_response(&received[..size], server, source, b"sent"); + assert_eq!(stats.snapshot().down_total, 32); + } else { + assert_eq!(stats.snapshot().down_total, 0); + } + } else { + let size = peer.recv(&mut received).unwrap(); + assert_udp_response(&received[..size], server, source, b"response"); + assert_eq!(stats.snapshot().down_total, size as u64); + } + assert!(peer.recv(&mut received).is_err(), "response replayed"); + } +} + +#[tokio::test] +async fn udp_ingress_stop_releases_associations_without_cancelling_caller() { + let parts = NetStack::start_tcp(NetStackConfig::default()).unwrap(); + let send_started = Arc::new(Notify::new()); + let dispatcher = Arc::new(BlockingDispatcher { + send_started: send_started.clone(), + send_count: Arc::new(AtomicUsize::new(0)), + open_count: Arc::new(AtomicUsize::new(0)), + }); + let stats = RuntimeResourceStats::new("tun_runtime_test"); + let cancellation = CancellationToken::new(); + let probe = observation::ResourceProbe::default(); + let (mut udp, ordinary, dns) = UdpIngress::new( + UdpIngressContext { + dispatcher, + dns: None, + sniffer: None, + limits: ResourceLimits::default(), + resource_stats: stats.clone(), + }, + cancellation.clone(), + ); + // The output consumers are already gone. Scope cleanup must still cancel + // and join this blocked transport without stopping the caller or TCP. + drop((ordinary, dns)); + probe.scope_sync(|| { + udp.offer(UdpPacketView { + source: "192.0.2.10:12000".parse().unwrap(), + destination: "198.51.100.20:443".parse().unwrap(), + payload: b"blocked", + }) + }); + let started = timeout(Duration::from_secs(1), send_started.notified()) + .await + .is_ok(); + let joined = timeout(Duration::from_secs(1), udp.stop()).await; + let finished_within_scope = joined.is_ok(); + let netstack_was_running = !parts.control.is_stopped(); + parts.control.stop().await; + assert!(started, "association did not enter its blocked transport"); + assert!( + finished_within_scope, + "UDP stop did not join the blocked transport" + ); + assert!( + !cancellation.is_cancelled(), + "UDP scope cancelled its caller" + ); + assert!( + netstack_was_running, + "fixture stopped netstack before UDP cleanup" + ); + assert_eq!(stats.snapshot().udp_current, 0); + assert_eq!(stats.snapshot().udp_peak, 1); + assert!( + probe.snapshot().is_idle(), + "UDP scope returned before its tasks were joined" + ); +} + +fn build_udp(source: SocketAddr, destination: SocketAddr, payload: &[u8]) -> Vec { + let (SocketAddr::V4(source), SocketAddr::V4(destination)) = (source, destination) else { + panic!("test helper requires IPv4"); + }; + let mut udp = vec![0_u8; 8 + payload.len()]; + udp[..2].copy_from_slice(&source.port().to_be_bytes()); + udp[2..4].copy_from_slice(&destination.port().to_be_bytes()); + let udp_len = u16::try_from(udp.len()).unwrap(); + udp[4..6].copy_from_slice(&udp_len.to_be_bytes()); + udp[8..].copy_from_slice(payload); + let checksum = transport_checksum(*source.ip(), *destination.ip(), 17, &udp); + udp[6..8].copy_from_slice(&checksum.to_be_bytes()); + build_ipv4(*source.ip(), *destination.ip(), 17, &udp) +} + +fn build_icmp_echo(sequence: u8, payload: &[u8]) -> Vec { + let mut icmp = vec![0_u8; 8 + payload.len()]; + icmp[0] = 8; + icmp[4..6].copy_from_slice(&17_u16.to_be_bytes()); + icmp[6..8].copy_from_slice(&u16::from(sequence).to_be_bytes()); + icmp[8..].copy_from_slice(payload); + let checksum = checksum(&icmp); + icmp[2..4].copy_from_slice(&checksum.to_be_bytes()); + build_ipv4( + Ipv4Addr::new(192, 0, 2, 10), + Ipv4Addr::new(198, 51, 100, 20), + 1, + &icmp, + ) +} + +fn build_tcp_syn(source: SocketAddr, destination: SocketAddr) -> Vec { + build_tcp_segment(source, destination, 1, 0, 0x02, &[]) +} + +fn build_tcp_segment( + source: SocketAddr, + destination: SocketAddr, + sequence: u32, + acknowledgement: u32, + flags: u8, + payload: &[u8], +) -> Vec { + let (SocketAddr::V4(source), SocketAddr::V4(destination)) = (source, destination) else { + panic!("test helper requires IPv4"); + }; + let mut tcp = vec![0_u8; 20 + payload.len()]; + tcp[..2].copy_from_slice(&source.port().to_be_bytes()); + tcp[2..4].copy_from_slice(&destination.port().to_be_bytes()); + tcp[4..8].copy_from_slice(&sequence.to_be_bytes()); + tcp[8..12].copy_from_slice(&acknowledgement.to_be_bytes()); + tcp[12] = 5 << 4; + tcp[13] = flags; + tcp[14..16].copy_from_slice(&u16::MAX.to_be_bytes()); + tcp[20..].copy_from_slice(payload); + let checksum = transport_checksum(*source.ip(), *destination.ip(), 6, &tcp); + tcp[16..18].copy_from_slice(&checksum.to_be_bytes()); + build_ipv4(*source.ip(), *destination.ip(), 6, &tcp) +} + +fn build_ipv4(source: Ipv4Addr, destination: Ipv4Addr, protocol: u8, transport: &[u8]) -> Vec { + let mut packet = vec![0_u8; 20 + transport.len()]; + let packet_len = u16::try_from(packet.len()).unwrap(); + packet[0] = 0x45; + packet[2..4].copy_from_slice(&packet_len.to_be_bytes()); + packet[6..8].copy_from_slice(&0x4000_u16.to_be_bytes()); + packet[8] = 64; + packet[9] = protocol; + packet[12..16].copy_from_slice(&source.octets()); + packet[16..20].copy_from_slice(&destination.octets()); + let checksum = checksum(&packet[..20]); + packet[10..12].copy_from_slice(&checksum.to_be_bytes()); + packet[20..].copy_from_slice(transport); + packet +} + +fn assert_udp_response(packet: &[u8], source: SocketAddr, destination: SocketAddr, payload: &[u8]) { + assert_eq!(packet[9], 17); + assert_eq!( + u16::from_be_bytes(packet[20..22].try_into().unwrap()), + source.port() + ); + assert_eq!( + u16::from_be_bytes(packet[22..24].try_into().unwrap()), + destination.port() + ); + assert_eq!(&packet[28..], payload); +} + +fn transport_checksum( + source: Ipv4Addr, + destination: Ipv4Addr, + protocol: u8, + transport: &[u8], +) -> u16 { + let mut sum = 0_u32; + add_bytes(&mut sum, &source.octets()); + add_bytes(&mut sum, &destination.octets()); + sum += u32::from(protocol); + sum += u32::try_from(transport.len()).unwrap(); + add_bytes(&mut sum, transport); + fold(sum) +} + +fn checksum(bytes: &[u8]) -> u16 { + let mut sum = 0_u32; + add_bytes(&mut sum, bytes); + fold(sum) +} + +fn add_bytes(sum: &mut u32, bytes: &[u8]) { + let (chunks, remainder) = bytes.as_chunks::<2>(); + for chunk in chunks { + *sum += u32::from(u16::from_be_bytes(*chunk)); + } + if let Some(byte) = remainder.first() { + *sum += u32::from(*byte) << 8; + } +} + +fn fold(mut sum: u32) -> u16 { + while sum >> 16 != 0 { + sum = (sum & 0xffff) + (sum >> 16); + } + let checksum = !u16::try_from(sum).unwrap(); + if checksum == 0 { u16::MAX } else { checksum } +} diff --git a/src/windows/packet_channel.rs b/src/windows/packet_channel.rs index 23960d3..7557dbb 100644 --- a/src/windows/packet_channel.rs +++ b/src/windows/packet_channel.rs @@ -42,7 +42,7 @@ const MAX_CONTROL_BYTES: usize = 16 * 1024; const MAX_RENDEZVOUS_BYTES: usize = 4 * 1024; const MAX_ERROR_BYTES: usize = 4 * 1024; const MAX_PACKET_BYTES: usize = 1_500; -pub(crate) const MAX_PACKET_BATCH_PACKETS: usize = 8; +pub(crate) const MAX_PACKET_BATCH_PACKETS: usize = crate::platform::TUN_PACKET_BATCH_SIZE; pub(crate) const DATA_PIPE_READ_BUFFER_BYTES: usize = 64 * 1024; const MAX_PACKET_BATCH_BYTES: usize = MAX_PACKET_BATCH_PACKETS * (MAX_PACKET_BYTES + 2); const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400; @@ -387,6 +387,9 @@ async fn run_provider_session( .read_packet_batch(&mut packets, MAX_PACKET_BATCH_PACKETS) .await .map_err(io::Error::other)?; + if packets.is_empty() { + continue; + } write_packet_batch_async(&mut data_write, &packets, &mut frame_buffer).await?; } }); diff --git a/src/windows/session.rs b/src/windows/session.rs index 776d06a..4bf2383 100644 --- a/src/windows/session.rs +++ b/src/windows/session.rs @@ -75,7 +75,8 @@ pub fn run() -> io::Result<()> { fn run_initialized() -> io::Result<()> { let (local_folder, installed_folder) = package_folders()?; log::append(&local_folder, "session", "Session Host starting"); - let runtime = tokio::runtime::Builder::new_current_thread() + let _runtime_thread = crate::ffi::RuntimeThreadGuard::enter(); + let runtime = crate::ffi::engine_runtime_builder() .enable_io() .enable_time() .build()?; diff --git a/tests/README.md b/tests/README.md index 3a11993..7d58df0 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,69 +1,84 @@ # 测试入口 -测试按验证目的维护,不按开发阶段累积副本。当前命令见 [scripts](../scripts/README.md)。 - -文件名与用例 ID 使用协议或验证用途:ech_config、encryption_wire、 -protocol_security、protocol_integration;事件标签与报告采用同一套名称。 +本仓库保留生产核心的配置、内存 IO、安全和生命周期回归,直接以定向 Rust 测试 +执行。[VCore scripts](../scripts/README.md) 只编译核心与平台产物。协议互通、对端 +下载、容器消费者以及内存/吞吐压力由独立的 [container-benchmark](https://github.com/OneXray/container-benchmark) +维护,通过显式 VCore checkout 指定被测核心;编译不依赖 benchmark 的安装或路径。 | 层次 | 保留内容 | 入口 | | --- | --- | --- | -| 日常 | 严格配置、feature、协议/TLS 内存 IO、局部上限、取消、FFI 边界和确定性回归 | check core --profile debug/release | -| 构建 | 精简 feature、生产 feature、全目标编译 | check core --profile features | -| 工具 | 子进程/容器清理、下载、原始证据完整性、平台产物 | scripts/tests | -| 对端设施 | 官方客户端到官方服务端的真实 TCP/UDP 能力,不计 VCore 支持 | check protocol-peers | -| 网络 | 公开消费者、真实原生认证/传输/UDP/上游和资源 | protocol-interop --suite … | -| 发布候选 | 有序两跳、故障/重建/长测、平台产物/ABI | integration suite 与平台构建 | +| 核心回归 | 严格配置、协议/TLS 内存 IO、局部上限、取消、FFI 边界和确定性回归 | 定向 cargo test | +| 编译 | 精简 feature、生产 feature、平台架构与全目标编译 | VCore scripts build | +| 编译工具回归 | 平台构建、产物身份与构建参数回归 | scripts/tests | +| 协议互通 | 官方 listener、生产 ABI 消费者、TCP/UDP 内容与代理路径 | 独立 container-benchmark interop | +| 性能评估 | Linux 原生 TUN、完整 CN 分流、吞吐/CPU/RSS/UDP 丢包/DNS | 独立 container-benchmark compare | +| 内存压力 | 完整真实 GeoData、加载与联合流量下的进程峰值 | 独立 container-benchmark stress | -所有服务端遵守[隔离规则](../docs/testing-isolation.md),包括原站和对照客户端入口。 -默认测试命令不执行历史宿主 listener。全目标仅 --no-run;ignored 不算通过。 -物理设备和正式安装不由本地测试推导。 +在独立 benchmark 工程中执行,`PATH` 为显式 VCore checkout: + +```sh +container-benchmark interop --source vcore=PATH +container-benchmark stress --source vcore=PATH +``` + +`interop --list` 可不提供 source,只列出测试,不运行互通。 + +所有服务端遵守[隔离规则](../docs/testing-isolation.md)。定向核心回归只运行不启服务的 +内存用例;全目标测试编译使用 `--no-run`,ignored 不算通过。物理设备和正式安装 +不由本地测试或 Linux 内存压力推导。 ## 必要回归与独立输入 +- Dialer 物理初始化:client-only TCP/UDP 的快速创建、64 个未完成提交、共享阻塞池 + 占用、慢 protect 故障注入、调用方取消、Stop 等待和新作用域恢复。mock protect + 在 connect 前拒绝,不启动宿主 listener 或发送业务;有限负载回归不证明任意 + 等待者数量有界、完整 TUN 压力或 Apple 真机内存。 - h2_stream_regression:完整 END_STREAM 后 RST 不丢响应,未完成响应仍报错。 -- stream_foundations 与 sing-mux 单元回归:延迟响应已就绪仍遵守原建链期限, - 先前 Pending 或首次延迟读取均不能绕过;确认建立后允许继续读取。 -- stream_shutdown:gRPC/legacy H2/池化 gRPC 和 XHTTP H1/H2 在有界、带缓冲 IO 上先送完 - 再关闭;Stop 可取消待写,XHTTP 关闭有一秒上限。 -- xhttp_h3_shutdown:纯内存 QUIC 背压下的上传完成屏障,不用下载尾包证明关闭。 - 覆盖延迟握手;测试对端显式取消并 join 后台任务,不把随 RTT 变化的 QUIC 关闭保留期 - 当作客户端关闭期限。上传完整性、一秒关闭上限和 Stop 验证保持独立。 -- shadowsocks_backpressure:三算法 Pending 重试长度、读先于写时的 codec 刷新,以及 - 官方服务端对空首包零 padding 的确定性拒绝;不修改官方库,不把 codec 夹具当作互通。 - SS/SS v3/EIH TCP 容器数据验收使用非空首段的 client-first,保留完整数据量; - server-first/空首包为已知限制,其他协议仍验证服务器先发。 +- stream_foundations 与 sing-mux:延迟响应遵守原建链期限;确认建立后允许继续读取。 +- stream_shutdown:gRPC/legacy H2/池化 gRPC 和 XHTTP H1/H2 先送完再关闭; + Stop 可取消待写,XHTTP 关闭有一秒上限。 +- xhttp_h3_shutdown:纯内存 QUIC 背压下上传完成屏障、延迟握手、一秒关闭上限和 Stop。 + 测试对端取消并 join 后台任务,不把 QUIC 关闭保留期当作客户端关闭期限。 +- shadowsocks_backpressure:三算法 Pending 重试长度、读先于写时 codec 刷新, + 以及官方服务端对空首包零 padding 的确定性拒绝。不修改官方库。 - hysteria2_packet_ids:完成后重用 16 位分片 ID,不误丢后续业务包。 -- shadowtls_config/stream 与 security::shadow_tls_tests:严格 v3、原生签名/Finished、 - 残留 cover、背压/flush、读取取消和关闭期限;shadowtls suite 另验官方容器对端。 -- uot_config、shadowsocks_uot 与共享 outbound::uot:仅 v2、SS 首包/读取门控、 - 三算法 u16 边界、收发预算、受控 DNS、取消/Stop;uot suite 验 TCP-only 官方 - Mihomo 六组合、上游/切组及无原生 UDP 回退,不把 ssserver 作为 UoT 正向服务端。 -- security_capabilities:公开配置经真实 SecurityClient 在主/下载腿产生实际混合 share; - 不再重复测试 fork 的纯 API 准入。 -- tuic_config/tuic_memory 与 outbound::tuic:严格 v5、当前 TLS exporter、无 ACK、 - 双 UDP wire、分片/重组、关联 ID 退役、窗口/credit、Heartbeat 和 Stop;tuic suite - 验官方 Mihomo 数据与身份负例、独立上游容器、嵌套组/重建/测速,并回归 HY2/H3。 -- httpupgrade_config/httpupgrade_memory:类型化普通/fast-open、ED 边界、严格 101、 - 部分写、首包恰好一次和原期限;等待后的就绪响应/写入不能越过期限,已建连接不受限。 - httpupgrade suite 另验 VMess/Trojan 真实消费者、 - UDP、Mihomo 关闭对照、命名 TLS 模板及既有 Upgrade/WS/其他传输。 -- vless_public::integration:64 个基础有序两跳与 SS v3/UoT/TUIC 强耦合链、公开入站/ - 合成 TUN、DNS/测速和旧连接组快照;100 次生命周期、100 轮混合重建、1800 秒长测。 - 新组合不替代七协议回归;资源归零、五秒静默、堆/队列与实际轮换配置独立复核。 -- fingerprints/mihomo-selected-v1.json:官方独立 ClientHello golden,不从待测实现重新生成期望; - [指纹验证](fingerprints/README.md)分别维护独立基线、内存报文与容器互通。 -- protocols/encryption-crypto.json 及 Go 生成器:独立密码向量;许可证和来源必须保留。 -- protocols/limits.json:实际常量、边界与越界行为;不另建一套产品配置。 +- shadowtls_config/stream:严格 v3、原生签名/Finished、残留 cover、背压/flush、 + 读取取消和关闭期限;不能由内存回归推导真实官方对端互通。 +- uot_config、shadowsocks_uot 与共享 outbound::uot:仅 v2、首包/读取门控、 + 三算法 u16 边界、收发预算、受控 DNS、取消和 Stop。codec 上限不代表对端容量。 +- security_capabilities:公开配置经真实 SecurityClient 在主/下载腿产生实际混合 share。 +- tuic_config/tuic_memory 与 outbound::tuic:严格 v5、TLS exporter、无 ACK、 + 双 UDP wire、分片/重组、关联 ID 退役、窗口/credit、Heartbeat 和 Stop。 +- httpupgrade_config/httpupgrade_memory:普通/fast-open、ED、严格 101、部分写、 + 首包恰好一次及原期限;已建连接不受建链期限限制。 +- GeoData:所有平台均无记录数上限、数量截断或总内存预算;超旧数量/文件额度的完整 + 加载、整数溢出、缺失/损坏和原子快照。四种 Domain 类型的正/负匹配、属性 key 存在性 + (bool false/int 0)、多属性 AND、顺序/重复/空项归一、Unicode simple-fold、整 selector + 反选与字面 `@!cn`;缺失/未声明 selector 不变全匹配,有效空交集与无域名保持独立。 + GeoIP IPv4/IPv6 与 `!code`、DAT reverse_match 忽略;业务规则与 DNS policy 共享基础 + 分类和记录,属性重叠不重复 value/Regex。属性筛选在 value 解析/Regex 编译前执行, + 未选中正则不编译。真正失败的重载/更新保留旧整体快照/资产。 + 分类异序/大小写定位与重载保持独立;TUN UDP 五元组独立固定 action,提示/GeoData + 更新只影响新流并释放旧快照;认证 QUIC 连接标识变更、同标识重传、逐目标空闲回收 + 与响应刷新保持独立。域名 DNS 答案和非 TUN 路径继续更新,已有组 transport 不迁移。 + 真实 fd-TUN 联合流量、完整资产、合成四类型/复杂正则与双快照实验由独立 benchmark + 验证;输入身份、实测数据和证据边界见其 README。Regex 使用常规 + `regex::bytes::Regex`,保持 ASCII 语义与库默认编译/嵌套/缓存保护;编译失败仍保留 + 旧有效快照。无 VCore 自设 NFA/DFA/determinization 额度、正则条数或总内存预算。 + 容量账本不含正则库内部状态或搜索 scratch;Regex 搜索可能分配,不报告其内存为零。 + 旧 dense DFA 输入实验不能替代当前实现的回归与压力结果,单次特定输入的 RSS + 也不能扩展为任意输入的内存保证。 + 不把少量选路见证当作逐条规则语义证明,Linux RSS 不替代 Apple 真机 footprint。 +- TUN UDP:reader 直接分流、慢关联隔离、TCP ingress Full 不阻塞 UDP/DNS;唯一 writer + 三通道公平/关闭/非法包隔离、平台接受前 DNS permit 生命周期、取消及关联/DNS 任务 + 同步回收。纯 codec 的 MTU/族边界、TCP-only 与通用 endpoint 回归在 netstack 内。 +- TUN 批次:首包等待后仅收已就绪包、最多 8 包、独立包边界、IPv6 策略及逐包流量、 + 非法包邻居保留、EOF/取消前缀与不重放;Windows 队列/唤醒纯内存回归不替代设备验证。 + netstack 入站维护按有界批次摊薄,TCP 仍逐包 ingress;同目标端口 SYN 的流绑定、 + 相邻 ICMP、输出满时未消费后缀及取消/关闭保持独立回归。 +- 独立 ClientHello golden、Encryption 密码向量与 limits 输入保留, + 不能用待测实现生成期望或以声明清单替代行为。 -## 保留而未自动执行的 fixture - -anytls_interop、xray_interop、mihomo_interop 仍含会话复用计数、双向 HTTP/SOCKS5、 -CONNECT 预读/Upgrade 等独有断言。保留这些 Rust 消费者用于迁移,不能把“其他 echo 通过” -当作已覆盖而删除。旧宿主启动入口不再开放,迁移前只编译,不计为当前容器验收。 -协议容器消费者已覆盖其共享数据、安全、组快照和 Stop 路径。 - -其他 tests 中含宿主 listener 的旧 fixture 同样不在 core 白名单;新增网络断言放容器 -suite,或在能真实覆盖行为时改为纯内存 IO。不要把宿主服务端改称 mock 绕过规则。 - -阶段实验、重复规划表和只测试 BLOCKED 文案的诊断已删除;原始记录在 Git 历史。 -必要负例、资源归零/静默检查和尚未替代的独有断言不按行数裁剪。 +历史宿主 listener fixture 只编译;实际网络互通只在独立 benchmark 的隔离容器中执行。 +容器和实验生成文件在每轮结束后清理,仅保存脱敏文字结论。版本、源码/锁文件 hash、 +命令、实测数据及清理结果必须写明;文字结论不能抵扣未经执行的设备或发布门禁。 diff --git a/tests/anytls_interop.rs b/tests/anytls_interop.rs deleted file mode 100644 index 7da095a..0000000 --- a/tests/anytls_interop.rs +++ /dev/null @@ -1,396 +0,0 @@ -//! Legacy AnyTLS session-count assertions retained for container migration. -//! Compile only until all server roles are isolated; see tests/README.md. -//! This fixture only compiles with `interop-test`. Its verifier intentionally -//! trusts the reference server's dynamically generated self-signed certificate, -//! but still verifies the TLS handshake signature. This verifier lives only in -//! this integration-test binary and cannot enter the VCore release library. - -#![cfg(feature = "interop-test")] - -use std::{ - io, - net::{IpAddr, Ipv4Addr, SocketAddr}, - sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, - }, - time::Duration, -}; - -use async_trait::async_trait; -use bytes::Bytes; -use rustls::{ - ClientConfig, DigitallySignedStruct, SignatureScheme, - client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}, - crypto::{CryptoProvider, verify_tls12_signature, verify_tls13_signature}, - pki_types::{CertificateDer, ServerName, UnixTime}, - version::{TLS12, TLS13}, -}; -use tokio::{ - io::{AsyncReadExt as _, AsyncWriteExt as _}, - net::{TcpListener, TcpStream, UdpSocket}, - task::{JoinHandle, JoinSet}, -}; -use tokio_rustls::TlsConnector; -use tokio_util::sync::CancellationToken; -use vcore::{ - dialer::{Dialer, ResolvedEndpoint}, - dispatch::{BoxStream, Dispatcher as _}, - outbound::{AnyTlsOutbound, AnyTlsTlsConnector, ConnectorDispatcher, UpstreamPath}, - session::{Datagram, DatagramSession, Destination, InboundKind, StreamSession}, -}; - -const IO_TIMEOUT: Duration = Duration::from_secs(8); - -#[derive(Debug)] -struct InteropOnlyCertificateVerifier { - provider: Arc, -} - -impl ServerCertVerifier for InteropOnlyCertificateVerifier { - fn verify_server_cert( - &self, - _end_entity: &CertificateDer<'_>, - _intermediates: &[CertificateDer<'_>], - _server_name: &ServerName<'_>, - _ocsp_response: &[u8], - _now: UnixTime, - ) -> Result { - Ok(ServerCertVerified::assertion()) - } - - fn verify_tls12_signature( - &self, - message: &[u8], - certificate: &CertificateDer<'_>, - signature: &DigitallySignedStruct, - ) -> Result { - verify_tls12_signature( - message, - certificate, - signature, - &self.provider.signature_verification_algorithms, - ) - } - - fn verify_tls13_signature( - &self, - message: &[u8], - certificate: &CertificateDer<'_>, - signature: &DigitallySignedStruct, - ) -> Result { - verify_tls13_signature( - message, - certificate, - signature, - &self.provider.signature_verification_algorithms, - ) - } - - fn supported_verify_schemes(&self) -> Vec { - self.provider - .signature_verification_algorithms - .supported_schemes() - } -} - -#[derive(Clone)] -struct InteropOnlyTlsConnector { - connector: TlsConnector, -} - -impl InteropOnlyTlsConnector { - fn new() -> io::Result { - let provider = Arc::new(rustls::crypto::ring::default_provider()); - let config = ClientConfig::builder_with_provider(provider.clone()) - .with_protocol_versions(&[&TLS13, &TLS12]) - .map_err(io::Error::other)? - .dangerous() - .with_custom_certificate_verifier(Arc::new(InteropOnlyCertificateVerifier { provider })) - .with_no_client_auth(); - assert!( - config.alpn_protocols.is_empty(), - "AnyTLS interoperability TLS must not advertise ALPN" - ); - Ok(Self { - connector: TlsConnector::from(Arc::new(config)), - }) - } -} - -#[async_trait] -impl AnyTlsTlsConnector for InteropOnlyTlsConnector { - async fn connect(&self, stream: BoxStream) -> io::Result { - let server_name = ServerName::try_from("anytls-interop.invalid".to_owned()) - .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?; - self.connector - .connect(server_name, stream) - .await - .map(|stream| Box::new(stream) as BoxStream) - .map_err(io::Error::other) - } -} - -struct CountingForwarder { - address: SocketAddr, - accepts: Arc, - cancellation: CancellationToken, - task: JoinHandle>, -} - -impl CountingForwarder { - async fn start(upstream: SocketAddr) -> io::Result { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let address = listener.local_addr()?; - let accepts = Arc::new(AtomicUsize::new(0)); - let cancellation = CancellationToken::new(); - let task_accepts = accepts.clone(); - let task_cancellation = cancellation.clone(); - let task = tokio::spawn(async move { - let mut relays = JoinSet::new(); - loop { - tokio::select! { - biased; - () = task_cancellation.cancelled() => break, - accepted = listener.accept() => { - let (mut inbound, _) = accepted?; - task_accepts.fetch_add(1, Ordering::AcqRel); - relays.spawn(async move { - let mut outbound = TcpStream::connect(upstream).await?; - let _ = tokio::io::copy_bidirectional(&mut inbound, &mut outbound).await?; - Ok::<_, io::Error>(()) - }); - } - completed = relays.join_next(), if !relays.is_empty() => { - if let Some(result) = completed { - result.map_err(io::Error::other)??; - } - } - } - } - relays.abort_all(); - while relays.join_next().await.is_some() {} - Ok(()) - }); - Ok(Self { - address, - accepts, - cancellation, - task, - }) - } - - fn accepted_connections(&self) -> usize { - self.accepts.load(Ordering::Acquire) - } - - async fn shutdown(self) -> io::Result<()> { - self.cancellation.cancel(); - self.task.await.map_err(io::Error::other)? - } -} - -fn stream_session(destination: Destination, source_port: u16) -> StreamSession { - StreamSession { - inbound: InboundKind::Http, - source: SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), source_port), - destination, - sniffed_domain: None, - } -} - -fn require_one_session(forwarder: &CountingForwarder, stage: &str) -> io::Result<()> { - let connections = forwarder.accepted_connections(); - if connections == 1 { - return Ok(()); - } - Err(io::Error::other(format!( - "AnyTLS did not reuse one physical session after {stage}: observed {connections} connections" - ))) -} - -async fn check_tcp_echo( - dispatcher: &ConnectorDispatcher, - payload: &'static [u8], - source_port: u16, -) -> io::Result<()> { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let target = listener.local_addr()?; - - let server = async move { - let (mut stream, _) = listener.accept().await?; - let mut received = vec![0_u8; payload.len()]; - stream.read_exact(&mut received).await?; - if received != payload { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "TCP echo target received a different payload", - )); - } - stream.write_all(&received).await?; - stream.shutdown().await - }; - let client = async { - let mut stream = dispatcher - .connect_tcp(stream_session(Destination::Ip(target), source_port)) - .await - .map_err(io::Error::other)?; - stream.write_all(payload).await?; - stream.flush().await?; - let mut received = vec![0_u8; payload.len()]; - stream.read_exact(&mut received).await?; - if received != payload { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "AnyTLS TCP echo response differs from the request", - )); - } - stream.shutdown().await - }; - - tokio::time::timeout(IO_TIMEOUT, async { - tokio::try_join!(server, client)?; - Ok::<_, io::Error>(()) - }) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "AnyTLS TCP echo timed out"))? -} - -async fn check_uot_v2_echo(dispatcher: &ConnectorDispatcher) -> io::Result<()> { - let socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let target = socket.local_addr()?; - let expected = [ - Bytes::from_static(b"vcore-anytls-uot-v2-one"), - Bytes::from_static(b"vcore-anytls-uot-v2-two"), - ]; - let server_expected = expected.clone(); - - let server = async move { - let mut buffer = [0_u8; 256]; - for payload in &server_expected { - let (length, peer) = socket.recv_from(&mut buffer).await?; - if &buffer[..length] != payload.as_ref() { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "UDP echo target received a different payload", - )); - } - socket.send_to(&buffer[..length], peer).await?; - } - Ok::<_, io::Error>(()) - }; - let client = async { - let mut transport = dispatcher - .open_datagram(DatagramSession::new( - InboundKind::Http, - SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 19003), - )) - .await - .map_err(io::Error::other)?; - for payload in &expected { - let sent = Datagram { - remote: Destination::Ip(target), - payload: payload.clone(), - sniffed_domain: None, - }; - transport - .send(sent.clone()) - .await - .map_err(io::Error::other)?; - let received = transport.receive().await.map_err(io::Error::other)?; - if received != sent { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "AnyTLS UoT v2 echo response differs from the request", - )); - } - } - transport.close().await.map_err(io::Error::other) - }; - - tokio::time::timeout(IO_TIMEOUT, async { - tokio::try_join!(server, client)?; - Ok::<_, io::Error>(()) - }) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "AnyTLS UoT v2 echo timed out"))? -} - -async fn run_interop() -> io::Result<()> { - let server = std::env::var("ANYTLS_INTEROP_ADDRESS") - .map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidInput, - "ANYTLS_INTEROP_ADDRESS is missing; legacy fixture; see tests/README.md", - ) - })? - .parse::() - .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?; - let password = std::env::var("ANYTLS_INTEROP_PASSWORD").map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidInput, - "ANYTLS_INTEROP_PASSWORD is missing; legacy fixture; see tests/README.md", - ) - })?; - - let forwarder = CountingForwarder::start(server).await?; - let endpoint = ResolvedEndpoint { - logical_host: forwarder.address.ip().to_string(), - port: forwarder.address.port(), - addresses: vec![forwarder.address], - }; - let outbound = Arc::new(AnyTlsOutbound::new( - Destination::Ip(forwarder.address), - UpstreamPath::direct( - endpoint, - Dialer::default().with_timeout(Duration::from_secs(3)), - ), - password, - Arc::new(InteropOnlyTlsConnector::new()?), - 64 * 1024, - )?); - let lifecycle = outbound.lifecycle(); - let dispatcher = ConnectorDispatcher::new(outbound); - - check_tcp_echo(&dispatcher, b"vcore-anytls-first-stream", 19001).await?; - require_one_session(&forwarder, "the first TCP stream")?; - check_tcp_echo(&dispatcher, b"vcore-anytls-reused-stream", 19002).await?; - require_one_session(&forwarder, "the reused TCP stream")?; - check_uot_v2_echo(&dispatcher).await?; - require_one_session(&forwarder, "the UoT v2 association")?; - check_tcp_echo(&dispatcher, b"vcore-anytls-after-uot", 19004).await?; - require_one_session(&forwarder, "the TCP stream following UoT")?; - - lifecycle.begin_shutdown(); - tokio::time::timeout(Duration::from_secs(3), lifecycle.shutdown()) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "AnyTLS shutdown timed out"))?; - - let post_shutdown = dispatcher - .connect_tcp(stream_session( - Destination::domain("example.invalid", 443)?, - 19005, - )) - .await; - if post_shutdown.is_ok() { - return Err(io::Error::other( - "AnyTLS accepted a new stream after shutdown", - )); - } - if forwarder.accepted_connections() != 1 { - return Err(io::Error::other( - "AnyTLS opened a physical connection during shutdown", - )); - } - - forwarder.shutdown().await -} - -#[tokio::test] -#[ignore = "requires the local anytls-go reference server; legacy fixture; see tests/README.md"] -async fn anytls_go_tcp_uot_v2_reuse_and_shutdown() { - tokio::time::timeout(Duration::from_secs(45), run_interop()) - .await - .expect("AnyTLS interoperability test timed out") - .expect("AnyTLS interoperability test failed"); -} diff --git a/tests/encryption_wire.rs b/tests/encryption_wire.rs deleted file mode 100644 index 04e9886..0000000 --- a/tests/encryption_wire.rs +++ /dev/null @@ -1,464 +0,0 @@ -#![cfg(all(feature = "outbound-vless", feature = "interop-test"))] - -use std::{ - io, - net::{IpAddr, SocketAddr}, - pin::Pin, - sync::{ - Arc, Mutex, - atomic::{AtomicUsize, Ordering}, - }, - task::{Context, Poll}, - time::Duration, -}; -use tokio::{ - io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}, - time::{Instant, timeout, timeout_at}, -}; -use vcore::{ - dialer::Dialer, - dispatch::BoxStream, - outbound::{VlessCommand, VlessEncryptionClient as Client, VlessStream, encode_request_header}, - session::Destination, -}; - -struct CountWrites(BoxStream, Arc, Arc>>); -fn encryption_client(fixture: &serde_json::Value, settings: &str) -> Client { - let client = Client::parse(settings).unwrap(); - if fixture["force_chacha"] == true { - client.with_chacha20_poly1305_for_interop() - } else { - client - } -} -impl AsyncRead for CountWrites { - fn poll_read( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - b: &mut ReadBuf<'_>, - ) -> Poll> { - Pin::new(&mut self.0).poll_read(cx, b) - } -} - -#[tokio::test] -#[ignore = "requires the owned SECURITY Encryption container harness"] -async fn public_tcp_roundtrip() { - use vcore::{ - config::{Config, ProxyProtocol}, - dialer::ResolvedEndpoint, - outbound::{EstablishContext, OutboundConnector, VlessOutbound}, - session::{InboundKind, StreamSession}, - }; - let fixture: serde_json::Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_ENCRYPTION_FIXTURE").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - let config=Config::parse_yaml(&serde_json::to_vec(&serde_json::json!({"socks-port":1080,"proxies":[fixture["node"]],"rules":["MATCH,edge"]})).unwrap()).unwrap(); - let ProxyProtocol::Vless(node) = &config.proxies[0].protocol else { - panic!() - }; - let outbound = VlessOutbound::new( - node, - ResolvedEndpoint { - logical_host: node.address.clone(), - port: node.port, - addresses: vec![SocketAddr::new(node.address.parse().unwrap(), node.port)], - }, - Dialer::default(), - ) - .unwrap(); - for round in 0..4 { - timeout(Duration::from_secs(25), async { - let family = if round % 2 == 0 { "ipv4" } else { "ipv6" }; - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control - .write_u8(if round % 2 == 0 { 10 } else { 138 }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - let destination = Destination::Ip(SocketAddr::new( - fixture[format!("origin_{family}")] - .as_str() - .unwrap() - .parse() - .unwrap(), - port, - )); - let mut stream = outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination, - sniffed_domain: None, - }, - &EstablishContext::default(), - ) - .await - .unwrap() - .io; - let mut hello = [0; 5]; - stream.read_exact(&mut hello).await.unwrap(); - assert_eq!(hello, *b"hello"); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - let payload = vec![0x5a; 10 * 1024 * 1024]; - stream.write_all(&payload).await.unwrap(); - stream.flush().await.unwrap(); - let mut received = vec![0; payload.len() + 7]; - stream.read_exact(&mut received).await.unwrap(); - assert_eq!(&received[..payload.len()], &payload); - assert_eq!(&received[payload.len()..], b"trailer"); - stream.shutdown().await.unwrap(); - drop(stream); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - }) - .await - .unwrap(); - } - outbound.shutdown().await; - println!("SECURITY-ENCRYPTION-CONSUMER-PASS rounds=4 bytes_per_direction=10485760"); -} -impl AsyncWrite for CountWrites { - fn poll_write( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - b: &[u8], - ) -> Poll> { - let result = Pin::new(&mut self.0).poll_write(cx, b); - if let Poll::Ready(Ok(n)) = result { - self.1.fetch_add(n, Ordering::SeqCst); - let mut prefix = self.2.lock().unwrap(); - let count = n.min(4096 - prefix.len()); - prefix.extend_from_slice(&b[..count]); - } - result - } - fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.0).poll_flush(cx) - } - fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.0).poll_shutdown(cx) - } -} - -/// Fault injection at caller-owned IO, not a custom peer or decoder. -struct CorruptRead(BoxStream, usize, usize); -impl AsyncRead for CorruptRead { - fn poll_read( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - b: &mut ReadBuf<'_>, - ) -> Poll> { - let start = b.filled().len(); - let result = Pin::new(&mut self.0).poll_read(cx, b); - if let Poll::Ready(Ok(())) = result { - let count = b.filled().len() - start; - if self.1 >= self.2 && self.1 < self.2 + count { - b.filled_mut()[start + self.1 - self.2] ^= 1; - } - self.2 += count; - } - result - } -} -impl AsyncWrite for CorruptRead { - fn poll_write( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - b: &[u8], - ) -> Poll> { - Pin::new(&mut self.0).poll_write(cx, b) - } - fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.0).poll_flush(cx) - } - fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.0).poll_shutdown(cx) - } -} - -#[tokio::test] -#[ignore = "requires the owned SECURITY Encryption container harness"] -async fn native_encryption_roundtrip() { - let fixture: serde_json::Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_ENCRYPTION_FIXTURE").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - let client = encryption_client(&fixture, fixture["encryption"].as_str().unwrap()); - let mut full_flight = 0; - for round in 0..4 { - let family = if round % 2 == 0 { "ipv4" } else { "ipv6" }; - let deadline = Instant::now() + Duration::from_secs(25); - timeout_at(deadline, async { - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control - .write_u8(if family == "ipv4" { 10 } else { 10 | 0x80 }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - let origin: IpAddr = fixture[format!("origin_{family}")] - .as_str() - .unwrap() - .parse() - .unwrap(); - let peer: IpAddr = fixture[format!("server_{family}")] - .as_str() - .unwrap() - .parse() - .unwrap(); - let raw = Dialer::default() - .connect_address(SocketAddr::new( - peer, - fixture["port"].as_u64().unwrap() as u16, - )) - .await - .unwrap(); - let sent = Arc::new(AtomicUsize::new(0)); - let capture = Arc::new(Mutex::new(Vec::new())); - let encrypted = client - .connect( - Box::new(CountWrites(Box::new(raw), sent.clone(), capture.clone())), - deadline, - ) - .await - .unwrap(); - let handshake_bytes = sent.load(Ordering::SeqCst); - let resumed = round != 0 && fixture["encryption"].as_str().unwrap().contains(".0rtt."); - if resumed { - assert_eq!( - handshake_bytes, 0, - "0-RTT must return without waiting for or sending a full handshake" - ); - } else { - assert!( - handshake_bytes >= 1333, - "a fresh client must perform the complete authenticated handshake" - ); - full_flight = handshake_bytes; - } - let destination = Destination::Ip(SocketAddr::new(origin, port)); - let header = encode_request_header( - uuid::Uuid::from_bytes([7; 16]), - VlessCommand::Tcp, - Some(&destination), - ) - .unwrap(); - let mut stream = VlessStream::with_deadline(encrypted, header, deadline); - let mut hello = [0; 5]; - stream.read_exact(&mut hello).await.unwrap(); - assert_eq!(hello, *b"hello"); - if resumed { - assert!( - sent.load(Ordering::SeqCst) + 1000 < full_flight, - "resumed wire prefix must not contain a full PFS exchange" - ); - } - assert_eq!(control.read_u8().await.unwrap(), b'A'); - let replay = if resumed && round == 1 { - Some(capture.lock().unwrap().clone()) - } else { - None - }; - let payload = vec![0x5a; 10 * 1024 * 1024]; - stream.write_all(&payload).await.unwrap(); - stream.flush().await.unwrap(); - let mut received = vec![0; payload.len() + 7]; - stream.read_exact(&mut received).await.unwrap(); - assert_eq!(&received[..payload.len()], &payload); - assert_eq!(&received[payload.len()..], b"trailer"); - stream.shutdown().await.unwrap(); - drop(stream); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - if let Some(replay) = replay { - let mut raw = Dialer::default() - .connect_address(SocketAddr::new( - peer, - fixture["port"].as_u64().unwrap() as u16, - )) - .await - .unwrap(); - raw.write_all(&replay).await.unwrap(); - raw.flush().await.unwrap(); - let mut byte = [0; 1]; - let result = timeout(Duration::from_secs(3), raw.read(&mut byte)) - .await - .expect("replayed NFS flight must be rejected promptly"); - assert!( - matches!(result, Ok(0)) - || result.as_ref().is_err_and(|e| matches!( - e.kind(), - io::ErrorKind::ConnectionReset | io::ErrorKind::BrokenPipe - )) - ); - assert!( - timeout(Duration::from_millis(150), control.read_u8()) - .await - .is_err(), - "replay must not reach the original live origin listener" - ); - } - }) - .await - .unwrap(); - } - let encryption = fixture["encryption"].as_str().unwrap(); - // A separately constructed node must never inherit this node's ticket. - identity_probe( - &encryption_client(&fixture, encryption), - &fixture, - false, - true, - false, - None, - ) - .await; - if encryption.contains(".0rtt.") { - identity_probe(&client, &fixture, true, false, true, None).await; - identity_probe(&client, &fixture, false, true, false, None).await; - identity_probe(&client, &fixture, false, true, true, None).await; - } - use base64::Engine as _; - let mut parts: Vec = encryption.split('.').map(str::to_owned).collect(); - let key = base64::engine::general_purpose::URL_SAFE_NO_PAD - .decode(&parts[3]) - .unwrap(); - parts[3] = if key.len() == 32 { - base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([9; 32]) - } else { - let (public, _) = - boring::mlkem::MlKemPrivateKey::generate(boring::mlkem::Algorithm::MlKem768).unwrap(); - base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(public.as_bytes()) - }; - identity_probe( - &encryption_client(&fixture, &parts.join(".")), - &fixture, - false, - false, - false, - None, - ) - .await; - for offset in [0, 1136, 1168] { - identity_probe( - &encryption_client(&fixture, encryption), - &fixture, - false, - false, - false, - Some(offset), - ) - .await; - } - client.close(); - println!("SECURITY-ENCRYPTION-WIRE-PASS rounds=4 bytes_per_direction=10485760"); -} - -#[tokio::test] -#[ignore = "requires the owned SECURITY Encryption container harness with short-lived tickets"] -async fn native_ticket_expiry() { - let fixture: serde_json::Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_ENCRYPTION_FIXTURE").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - assert_eq!(fixture["ticket_expiry"], true); - let encryption = fixture["encryption"].as_str().unwrap(); - assert!(encryption.contains(".0rtt.")); - let client = encryption_client(&fixture, encryption); - identity_probe(&client, &fixture, false, true, false, None).await; - identity_probe(&client, &fixture, false, true, true, None).await; - // The independent peer advertises two seconds. Real monotonic time, no - // mutation of the cache or server clock, and no automatic business retry. - tokio::time::sleep(Duration::from_secs(3)).await; - identity_probe(&client, &fixture, false, true, false, None).await; - identity_probe(&client, &fixture, false, true, true, None).await; - client.close(); - println!("SECURITY-ENCRYPTION-EXPIRY-PASS full-resumed-expired-full-resumed"); -} - -async fn identity_probe( - client: &Client, - fixture: &serde_json::Value, - other_handler: bool, - should_succeed: bool, - resumed: bool, - corrupt: Option, -) { - let deadline = Instant::now() + Duration::from_secs(5); - let mut control = tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.write_u8(12).await.unwrap(); - let port = control.read_u16().await.unwrap(); - let origin: IpAddr = fixture["origin_ipv4"].as_str().unwrap().parse().unwrap(); - let server: IpAddr = fixture["server_ipv4"].as_str().unwrap().parse().unwrap(); - let peer_port = fixture[if other_handler { "reject_port" } else { "port" }] - .as_u64() - .unwrap() as u16; - let bytes = Arc::new(AtomicUsize::new(0)); - let result = timeout_at(deadline, async { - let raw = Dialer::default() - .connect_address(SocketAddr::new(server, peer_port)) - .await?; - let raw: BoxStream = if let Some(offset) = corrupt { - Box::new(CorruptRead(Box::new(raw), offset, 0)) - } else { - Box::new(raw) - }; - let raw = client - .connect( - Box::new(CountWrites(raw, bytes.clone(), Arc::default())), - deadline, - ) - .await?; - assert_eq!( - bytes.load(Ordering::SeqCst) == 0, - resumed, - "cache must be node-local and rejected tickets must be replaced by a full handshake" - ); - let header = encode_request_header( - uuid::Uuid::from_bytes([7; 16]), - VlessCommand::Tcp, - Some(&Destination::Ip(SocketAddr::new(origin, port))), - )?; - let mut stream = VlessStream::with_deadline(raw, header, deadline); - stream.write_all(b"synthetic-probe").await?; - stream.flush().await?; - let mut response = [0; 2]; - stream.read_exact(&mut response).await?; - assert_eq!(response, *b"ok"); - stream.shutdown().await?; - Ok::<_, io::Error>(()) - }) - .await - .expect("authentication outcome must be explicit, not timeout"); - if should_succeed { - result.unwrap(); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - } else { - assert!(result.is_err(), "invalid authentication must not succeed"); - if corrupt.is_some() { - assert_eq!( - result.unwrap_err().kind(), - io::ErrorKind::InvalidData, - "modified handshake must fail authentication" - ); - } - assert!( - timeout(Duration::from_millis(150), control.read_u8()) - .await - .is_err(), - "failed authentication must produce zero origin accepts" - ); - } -} diff --git a/tests/fingerprints/README.md b/tests/fingerprints/README.md index 946f653..0c84073 100644 --- a/tests/fingerprints/README.md +++ b/tests/fingerprints/README.md @@ -4,14 +4,15 @@ ## 验证分层 -| 报告标签 | 脚本入口 | 证明范围 | +| 历史报告标签 | 历史脚本入口 | 证明范围 | | --- | --- | --- | | FINGERPRINT-REFERENCE | protocol_fingerprint_reference | 官方客户端的独立基线,不是 VCore 互通 | | FINGERPRINT-WIRE | protocol_fingerprint_shape | 公开配置的内存 TLS 报文、恢复与过期 | | FINGERPRINT-INTEROP | protocol_fingerprint | 命名指纹在真实容器协议链路中的行为 | -命令统一见 [定向安全验证](../../scripts/README.md#定向安全验证)。用例、容器与报告 -按用途命名,不使用开发阶段编号。golden 中的 `case_id` 是当前用例标签;原始 TLS +上述专用运行器可从 Git 历史查阅,未随当前精简 Mihomo 互通入口恢复;不得把它们 +当作现存命令。协议验证归 VCore,container-benchmark 只做性能评估;当前入口和 +范围见 [构建与验证](../../scripts/README.md)。golden 中的 `case_id` 是用例标签;原始 TLS 字节、解析结果、来源版本及原报告 hash 保持独立采集时的值,不表示重新抓包。 历史运行报告不改写,按其记录的 Git revision 复核;各层结果不能互相代替。 @@ -21,15 +22,17 @@ four templates, ordinary TCP TLS and classic REALITY. The official latest Mihomo binary was downloaded, not compiled from the reference source. Binary identity, uTLS build dependency, raw record layout, handshake bytes and parsed fields are -retained. `validate_capture` re-parses raw bytes and detects altered derived data. +retained. The historical collector's `validate_capture` re-parsed raw bytes and +detected altered derived data; that collector is not a current script entry. These are public ephemeral key shares and synthetic names, not private keys, proxy credentials, application traffic or user destinations. -The full 116-case run stays in ignored `target/interop/runs/`: six enabled names +The historical independent baseline used 116 cases: six enabled names across TCP/WS/gRPC/REALITY, two SNI lengths and two successive connections; four `none` controls; four actual templates with TLS1.2/1.3 handshakes on a native OpenSSL observer. All server roles and the official client run in owned isolated -containers. Success means **reference captured / baseline verified**, not VCore +containers. Generated run data is deleted after each test; the committed golden +remains a source fixture. Success means **reference captured / baseline verified**, not VCore support, REALITY authentication or proxy interoperability. A capture-only observer intentionally aborts; OpenSSL handshake cases do not implement VLESS or HTTP. diff --git a/tests/hysteria2_native.rs b/tests/hysteria2_native.rs deleted file mode 100644 index bb46756..0000000 --- a/tests/hysteria2_native.rs +++ /dev/null @@ -1,816 +0,0 @@ -#![cfg(all(feature = "outbound-hysteria2", feature = "interop-test"))] -use serde_json::{Value, json}; -use sha2::{Digest, Sha256}; -use std::{net::SocketAddr, time::Duration}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint}, - outbound::{EstablishContext, OutboundConnector, UpstreamPath, hysteria2::Hysteria2Outbound}, - session::{InboundKind, StreamSession}, -}; - -fn fixture() -> Value { - let value: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_VLESS_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(value["isolation"], "containers"); - value -} -fn node(raw: Value) -> Hysteria2Outbound { - node_with_dialer(raw, Dialer::default()) -} -fn node_with_dialer(raw: Value, dialer: Dialer) -> Hysteria2Outbound { - let config = Config::parse_yaml( - json!({"socks-port":1080, "proxies":[raw], "rules":["MATCH,peer"]}) - .to_string() - .as_bytes(), - ) - .unwrap(); - let ProxyProtocol::Hysteria2(node) = &config.proxies[0].protocol else { - panic!("wrong protocol") - }; - Hysteria2Outbound::new_with_path( - node, - UpstreamPath::direct( - ResolvedEndpoint { - logical_host: node.address.clone(), - port: node.port, - addresses: vec![SocketAddr::new(node.address.parse().unwrap(), node.port)], - }, - dialer, - ), - ) - .unwrap() -} - -struct Protector { - count: std::sync::atomic::AtomicUsize, - reject_after: usize, -} -impl vcore::dialer::SocketProtector for Protector { - fn protect(&self, _: i32) -> std::io::Result<()> { - let index = self.count.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - if index >= self.reject_after { - return Err(std::io::ErrorKind::PermissionDenied.into()); - } - Ok(()) - } -} -async fn origin(f: &Value, mode: u8, ipv6: bool) -> (tokio::net::TcpStream, SocketAddr) { - let mut control = tokio::net::TcpStream::connect(f["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control - .write_u8(if mode >= 10 && ipv6 { mode | 128 } else { mode }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - ( - control, - SocketAddr::new( - f[if ipv6 { "origin_ipv6" } else { "origin_ipv4" }] - .as_str() - .unwrap() - .parse() - .unwrap(), - port, - ), - ) -} -fn stream_request(target: SocketAddr) -> StreamSession { - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: target.into(), - sniffed_domain: None, - } -} -fn datagram_request() -> vcore::outbound::DatagramRequest { - vcore::outbound::DatagramRequest::new(vcore::session::DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )) -} - -#[tokio::test] -#[ignore = "official Hysteria in an owned NET_ADMIN container required"] -async fn native_hopping() { - use std::sync::{Arc, atomic::Ordering}; - use vcore::resources::observation::{ResourceKind, ResourceProbe}; - let mut event = vcore::resources::case_events::Case::new("HYSTERIA2-HOP", "native_hopping"); - let f = fixture(); - let probe = ResourceProbe::default(); - probe - .scope(async { - let protector = Arc::new(Protector { - count: 0.into(), - reject_after: usize::MAX, - }); - let outbound = node_with_dialer( - f["node"].clone(), - Dialer::default().with_protector(protector.clone()), - ); - let (mut tcp_control, target) = origin(&f, 13, false).await; - let mut stream = outbound - .connect_stream(stream_request(target), &EstablishContext::default()) - .await - .unwrap() - .io; - assert_eq!(tcp_control.read_u8().await.unwrap(), b'A'); - let mut associations = Vec::new(); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - let (control, target) = origin(&f, if ipv6 { 6 } else { 4 }, ipv6).await; - let remote = if domain { - vcore::session::Destination::domain("vcore-fixture.test", target.port()) - .unwrap() - } else { - target.into() - }; - let udp = outbound - .open_datagram(datagram_request(), &EstablishContext::default()) - .await - .unwrap(); - // Native Hysteria's response buffer includes the protocol header. - // Mihomo separately passes the full 4096-byte application payload. - let peer_maximum = 4096 - 9 - target.to_string().len(); - associations.push((control, target, remote, peer_maximum, udp)); - } - let started = tokio::time::Instant::now(); - let mut sent_hash = Sha256::new(); - let mut recv_hash = Sha256::new(); - let mut bytes = vec![0; SIZE]; - for sequence in 0..1500 { - let payload = record(sequence); - tokio::time::timeout(Duration::from_secs(5), async { - stream.write_all(&payload).await.unwrap(); - stream.read_exact(&mut bytes).await.unwrap(); - }) - .await - .unwrap(); - sent_hash.update(&payload); - recv_hash.update(&bytes); - let (udp_control, udp_target, remote, peer_maximum, udp) = - &mut associations[sequence as usize % 3]; - let size = [1, 64, 512, 1200, *peer_maximum][sequence as usize / 3 % 5]; - let payload = bytes::Bytes::from(vec![(sequence % 251) as u8; size]); - udp.send(vcore::session::Datagram { - remote: remote.clone(), - payload: payload.clone(), - sniffed_domain: None, - }) - .await - .unwrap(); - let reply = tokio::time::timeout(Duration::from_secs(5), udp.receive()) - .await - .unwrap_or_else(|_| { - panic!("UDP response deadline: sequence={sequence}, size={size}") - }) - .unwrap(); - assert_eq!(reply.remote, (*udp_target).into()); - assert_eq!(reply.payload, payload); - assert_eq!(udp_control.read_u16().await.unwrap() as usize, size); - assert_ne!(udp_control.read_u16().await.unwrap(), 0); - let mut observed = vec![0; size]; - udp_control.read_exact(&mut observed).await.unwrap(); - assert_eq!(observed, payload); - if started.elapsed() > Duration::from_secs(9) { - assert!( - protector.count.load(Ordering::SeqCst) > 1, - "port hopping has not created a protected new socket" - ); - } - tokio::time::sleep_until(started + Duration::from_millis((sequence + 1) * 40)) - .await; - } - assert!(started.elapsed() >= Duration::from_secs(60)); - let digest = sent_hash.finalize(); - assert_eq!(digest, recv_hash.finalize()); - assert!(protector.count.load(Ordering::SeqCst) >= 9); - if let Ok(path) = std::env::var("VCORE_HOP_OBSERVATIONS") { - let hash: String = digest.iter().map(|byte| format!("{byte:02x}")).collect(); - let data = json!({"seconds":started.elapsed().as_secs_f64(),"protected_sockets":protector.count.load(Ordering::SeqCst),"tcp_bytes_per_direction":1500*SIZE,"tcp_sha256":hash,"udp_packets":1500,"udp_target_kinds":3,"udp_maximums":associations.iter().map(|v| v.3).collect::>(),"socket_peak":probe.snapshot().peak(ResourceKind::Socket)}); - std::fs::write(path, data.to_string()).unwrap(); - } - assert!( - probe.snapshot().peak(ResourceKind::Socket) <= 2, - "old socket not bounded" - ); - for (mut observer, _, remote, maximum, mut udp) in associations { - // Preserve the native peer limitation as an explicit observation: - // max+1 reaches the origin but its reply is silently discarded. - let payload = bytes::Bytes::from(vec![9; maximum + 1]); - udp.send(vcore::session::Datagram { - remote, - payload: payload.clone(), - sniffed_domain: None, - }) - .await - .unwrap(); - assert_eq!(observer.read_u16().await.unwrap() as usize, maximum + 1); - assert_ne!(observer.read_u16().await.unwrap(), 0); - let mut observed = vec![0; maximum + 1]; - observer.read_exact(&mut observed).await.unwrap(); - assert_eq!(observed, payload); - assert!( - tokio::time::timeout(Duration::from_millis(150), udp.receive()) - .await - .is_err() - ); - udp.close().await.unwrap(); - } - drop(stream); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - drop(outbound); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - tokio::time::sleep(Duration::from_secs(5)).await; - assert!(probe.snapshot().is_idle()); - }) - .await; - event.resources(probe.snapshot()); -} -const SIZE: usize = 65536; -fn record(sequence: u64) -> Vec { - let mut bytes = vec![(sequence % 251) as u8; SIZE]; - bytes[..8].copy_from_slice(&sequence.to_be_bytes()); - bytes -} - -async fn bandwidth(f: &Value, direction: u8, up: u64, down: u64, port: u16, expected: u64) -> f64 { - let mut raw = f["node"].clone(); - raw["up"] = json!(up); - raw["down"] = json!(down); - raw["port"] = json!(port); - let outbound = node(raw); - let mut control = tokio::net::TcpStream::connect(f["bandwidth_control"].as_str().unwrap()) - .await - .unwrap(); - control.write_u8(direction).await.unwrap(); - let target_port = control.read_u16().await.unwrap(); - let mut stream = outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: SocketAddr::new( - f["bandwidth_ipv4"].as_str().unwrap().parse().unwrap(), - target_port, - ) - .into(), - sniffed_domain: None, - }, - &EstablishContext::default(), - ) - .await - .unwrap() - .io; - let mut hello = [0; 5]; - stream.read_exact(&mut hello).await.unwrap(); - assert_eq!(&hello, b"ready"); - assert_eq!(outbound.congestion_observation().await.unwrap().0, expected); - let before = outbound.congestion_observation().await.unwrap().1; - let observation = tokio::spawn(async move { - let mut buckets = [0_u64; 30]; - let mut last = 0; - loop { - match control.read_u8().await.unwrap() { - b'P' => { - let sequence = control.read_u64().await.unwrap(); - assert_eq!(sequence, last + 1); - last = sequence; - let second = control.read_u64().await.unwrap() / 1_000_000_000; - if (10..40).contains(&second) { - buckets[(second - 10) as usize] += SIZE as u64; - } - } - b'D' => { - let sequence = control.read_u64().await.unwrap(); - let mut digest = [0; 32]; - control.read_exact(&mut digest).await.unwrap(); - return (buckets, sequence, digest); - } - _ => panic!("invalid origin observation"), - } - } - }); - let started = tokio::time::Instant::now(); - let mut digest = Sha256::new(); - let mut sequence = 0; - let mut download_buckets = [0_u64; 30]; - if direction == b'U' { - while started.elapsed() < Duration::from_secs(40) { - let bytes = record(sequence); - stream.write_all(&bytes).await.unwrap(); - digest.update(&bytes); - sequence += 1; - } - stream.write_all(&record(u64::MAX)).await.unwrap(); - let mut done = [0; 5]; - stream.read_exact(&mut done).await.unwrap(); - assert_eq!(&done, b"done!"); - } else { - stream.write_u8(b'S').await.unwrap(); - let mut stopped = false; - let mut bytes = vec![0; SIZE]; - loop { - stream.read_exact(&mut bytes).await.unwrap(); - let number = u64::from_be_bytes(bytes[..8].try_into().unwrap()); - if number == u64::MAX { - break; - } - assert_eq!(bytes, record(sequence)); - digest.update(&bytes); - sequence += 1; - let second = started.elapsed().as_secs(); - if (10..40).contains(&second) { - download_buckets[(second - 10) as usize] += SIZE as u64; - } - if second >= 40 && !stopped { - stream.write_u8(b'X').await.unwrap(); - stopped = true; - } - } - } - let (upload_buckets, received, remote_digest) = observation.await.unwrap(); - assert_eq!(received, sequence); - assert_eq!(digest.finalize().as_slice(), remote_digest); - let after = outbound.congestion_observation().await.unwrap().1; - let buckets = if direction == b'U' { - upload_buckets - } else { - download_buckets - }; - let throughput = buckets.iter().sum::() as f64 / 30.0; - let evidence = json!({"direction":char::from(direction), "up_mbps":up, "down_mbps":down, - "listener_port":port,"controller_bps":expected,"payload_bps":throughput,"buckets":buckets, - "udp_tx_bytes":after.udp_tx.bytes - before.udp_tx.bytes,"udp_rx_bytes":after.udp_rx.bytes - before.udp_rx.bytes, - "rtt_ms":after.path.rtt.as_secs_f64()*1000.0,"cwnd":after.path.cwnd,"lost_packets":after.path.lost_packets}); - println!("HYSTERIA2-BANDWIDTH {evidence}"); - if let Ok(path) = std::env::var("VCORE_BANDWIDTH_OBSERVATIONS") { - use std::io::Write; - let mut file = std::fs::OpenOptions::new() - .create(true) - .append(true) - .open(path) - .unwrap(); - writeln!(file, "{evidence}").unwrap(); - file.flush().unwrap(); - } - drop(stream); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - throughput -} - -#[tokio::test] -#[ignore = "owned isolated HYSTERIA2 peers required"] -async fn native_bandwidth_matrix() { - let _case = - vcore::resources::case_events::Case::new("HYSTERIA2-BANDWIDTH", "native_bandwidth_matrix"); - let f = fixture(); - for direction in *b"UD" { - let baseline = bandwidth(&f, direction, 0, 0, 23000, 0).await; - assert!( - baseline >= 750000.0, - "environment cannot measure: baseline below 6 Mbit/s" - ); - let mut measured = Vec::new(); - for rate in [1, 2] { - let (up, down, expected) = if direction == b'U' { - (rate, 100, rate * 125000) - } else { - (0, rate, 0) - }; - let value = bandwidth(&f, direction, up, down, 23000, expected).await; - assert!( - value <= rate as f64 * 125000.0 * 1.1, - "configured bandwidth is not enforced" - ); - measured.push(value); - } - assert!( - measured[1] >= measured[0] * 1.5, - "two configured rates do not scale" - ); - let (up, down, port, expected) = if direction == b'U' { - (2, 100, 23002, 125000) - } else { - (0, 2, 23003, 0) - }; - let lower = bandwidth(&f, direction, up, down, port, expected).await; - assert!( - lower <= 125000.0 * 1.1, - "lower server bandwidth is not enforced" - ); - } - // Mihomo returns RxAuto when client down=0; a configured up must not override it. - assert!(bandwidth(&f, b'U', 2, 0, 23000, 0).await > 0.0); - assert!(bandwidth(&f, b'U', 2, 2, 23004, 0).await > 0.0); -} - -#[tokio::test] -#[ignore = "owned isolated Mihomo client and server required"] -async fn native_mihomo_close_alignment() { - let _case = vcore::resources::case_events::Case::new( - "HYSTERIA2-NATIVE", - "native_mihomo_close_alignment", - ); - let f = fixture(); - assert_eq!(f["close_reference"]["scope"], "same-mode"); - assert_eq!(f["close_reference"]["terminated"], true); - let outbound = node(f["node"].clone()); - let (mut observer, target) = origin(&f, 11, false).await; - let mut io = outbound - .connect_stream(stream_request(target), &EstablishContext::default()) - .await - .unwrap() - .io; - let mut hello = [0; 5]; - io.read_exact(&mut hello).await.unwrap(); - assert_eq!(&hello, b"hello"); - io.write_all(b"ping").await.unwrap(); - io.flush().await.unwrap(); - let mut ping = [0; 4]; - io.read_exact(&mut ping).await.unwrap(); - assert_eq!(&ping, b"ping"); - io.shutdown().await.unwrap(); - let mut tail = Vec::new(); - let _terminal = tokio::time::timeout(Duration::from_secs(5), io.read_to_end(&mut tail)) - .await - .expect("close must end pending reads"); - let hex: String = tail.iter().map(|byte| format!("{byte:02x}")).collect(); - assert_eq!(hex, f["close_reference"]["tail_hex"].as_str().unwrap()); - assert_eq!(observer.read_u8().await.unwrap(), b'A'); - assert_eq!(observer.read_u8().await.unwrap(), b'D'); - drop(io); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); -} - -#[tokio::test] -#[ignore = "owned isolated HYSTERIA2 peers required"] -async fn native_security_matrix() { - use vcore::resources::{case_events::Case, observation::ResourceProbe}; - let f = fixture(); - let mut case = Case::new("HYSTERIA2-SECURITY", "native_security_matrix"); - let probe = ResourceProbe::default(); - const IDS: &[&str] = &[ - "root-pin", - "leaf-pin", - "root-pin-wrong-name", - "leaf-pin-is-trust", - "unknown-ca", - "explicit-skip", - "wrong-pin", - "skip-does-not-override-pin", - "wrong-password", - "empty-password", - "raw-password", - "empty-alpn-default", - "custom-alpn", - "wrong-alpn", - "mtls-valid", - "mtls-absent", - "mtls-expired", - "mtls-wrong-ca", - "salamander", - "salamander-client-only", - "salamander-server-only", - "salamander-wrong-key", - "salamander-wrong-auth", - ]; - assert_eq!(f["security"].as_array().unwrap().len(), IDS.len()); - for sample in f["security"].as_array().unwrap() { - let id = sample["id"].as_str().unwrap(); - let _sample = Case::new( - "HYSTERIA2-SECURITY-CASE", - IDS.iter().copied().find(|name| *name == id).unwrap(), - ); - let success = sample["success"].as_bool().unwrap(); - probe - .scope(async { - let outbound = node(sample["node"].clone()); - let (mut control, target) = origin(&f, 12, false).await; - let outcome = tokio::time::timeout(Duration::from_secs(5), async { - let mut io = outbound - .connect_stream( - stream_request(target), - &EstablishContext::with_timeout(Duration::from_secs(4)), - ) - .await - .map_err(std::io::Error::other)? - .io; - io.write_all(b"synthetic-probe").await?; - let mut reply = [0; 2]; - io.read_exact(&mut reply).await?; - assert_eq!(&reply, b"ok"); - Ok::<_, std::io::Error>(()) - }) - .await - .expect("bounded security operation"); - assert_eq!(outcome.is_ok(), success, "{id}"); - if success { - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - } else { - assert!( - tokio::time::timeout(Duration::from_millis(100), control.read_u8()) - .await - .is_err(), - "rejected security reached origin: {id}" - ); - } - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - }) - .await; - assert!(probe.snapshot().is_idle(), "{id}: {:?}", probe.snapshot()); - println!("HYSTERIA2-SECURITY {id} PASS"); - } - for identity in f["invalid_identities"].as_array().unwrap() { - let mut raw = f["node"].clone(); - raw["certificate"] = identity["certificate"].clone(); - raw["private-key"] = identity["private-key"].clone(); - assert!( - Config::parse_yaml( - json!({"socks-port":1080,"proxies":[raw],"rules":["MATCH,peer"]}) - .to_string() - .as_bytes() - ) - .is_err() - ); - } - case.resources(probe.snapshot()); -} - -#[tokio::test] -#[ignore = "official Hysteria with disableUDP required"] -async fn native_udp_disabled() { - let _case = - vcore::resources::case_events::Case::new("HYSTERIA2-UDP-DISABLED", "native_udp_disabled"); - let f = fixture(); - let outbound = node(f["node"].clone()); - let (mut control, target) = origin(&f, 10, false).await; - let mut io = outbound - .connect_stream(stream_request(target), &EstablishContext::default()) - .await - .unwrap() - .io; - let mut hello = [0; 5]; - io.read_exact(&mut hello).await.unwrap(); - assert_eq!(&hello, b"hello"); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - let sent = vec![0x5a; 10 * 1024 * 1024]; - io.write_all(&sent).await.unwrap(); - let mut received = vec![0; sent.len()]; - io.read_exact(&mut received).await.unwrap(); - assert_eq!(Sha256::digest(&sent), Sha256::digest(&received)); - let mut tail = [0; 7]; - io.read_exact(&mut tail).await.unwrap(); - assert_eq!(&tail, b"trailer"); - assert!(matches!( - outbound - .open_datagram(datagram_request(), &EstablishContext::default()) - .await, - Err(vcore::dispatch::DispatchError::NotAllowed) - )); - drop(io); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); -} - -async fn hop_interrupt(reject: bool) { - use std::sync::{Arc, atomic::Ordering}; - use vcore::resources::{ - case_events::Case, - observation::{ResourceKind, ResourceProbe}, - }; - let mut event = Case::new( - "HYSTERIA2-HOP", - if reject { - "native_hop_protect_rejection" - } else { - "native_stop_during_hop" - }, - ); - let f = fixture(); - let probe = ResourceProbe::default(); - event.checkpoint("baseline", probe.snapshot()); - probe - .scope(async { - let protector = Arc::new(Protector { - count: 0.into(), - reject_after: if reject { 1 } else { usize::MAX }, - }); - let outbound = node_with_dialer( - f["node"].clone(), - Dialer::default().with_protector(protector.clone()), - ); - let (mut control, target) = origin(&f, 13, false).await; - let mut io = outbound - .connect_stream(stream_request(target), &EstablishContext::default()) - .await - .unwrap() - .io; - io.write_all(b"before-hop").await.unwrap(); - let mut bytes = [0; 10]; - io.read_exact(&mut bytes).await.unwrap(); - assert_eq!(&bytes, b"before-hop"); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - tokio::time::timeout(Duration::from_secs(9), async { - while protector.count.load(Ordering::SeqCst) < 2 { - tokio::time::sleep(Duration::from_millis(1)).await; - } - }) - .await - .unwrap(); - if reject { - assert!( - tokio::time::timeout(Duration::from_secs(2), io.read_u8()) - .await - .unwrap() - .is_err() - ); - } else { - // The second protected socket exists, inside the one-second old - // socket grace window. Stop must join both rather than abort/leak. - assert!(probe.snapshot().current(ResourceKind::Socket) >= 2); - outbound.begin_shutdown(); - assert!(io.read_u8().await.is_err()); - } - drop(io); - let count = protector.count.load(Ordering::SeqCst); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - event.checkpoint("after-stop", probe.snapshot()); - tokio::time::sleep(Duration::from_secs(5)).await; - assert_eq!( - protector.count.load(Ordering::SeqCst), - count, - "late unrequested reconnect" - ); - assert!(probe.snapshot().is_idle()); - event.checkpoint("quiet", probe.snapshot()); - }) - .await; - event.resources(probe.snapshot()); -} - -#[tokio::test] -#[ignore = "official Hysteria in an owned NET_ADMIN container required"] -async fn native_hop_protect_rejection() { - hop_interrupt(true).await; -} - -#[tokio::test] -#[ignore = "official Hysteria in an owned NET_ADMIN container required"] -async fn native_stop_during_hop() { - hop_interrupt(false).await; -} - -#[tokio::test] -#[ignore = "owned isolated HYSTERIA2 peers required"] -async fn native_deadline_and_udp_budget() { - use vcore::resources::{case_events::Case, observation::ResourceProbe}; - let mut event = Case::new("HYSTERIA2-NATIVE", "native_deadline_and_udp_budget"); - let f = fixture(); - let probe = ResourceProbe::default(); - probe - .scope(async { - let (mut observer, blackhole) = origin(&f, 20, false).await; - let mut raw = f["node"].clone(); - raw["server"] = json!(blackhole.ip().to_string()); - raw["port"] = json!(blackhole.port()); - let outbound = node(raw); - let context = EstablishContext::default(); - let started = tokio::time::Instant::now(); - // Consume part of the same ten-second budget before the outer handshake. - tokio::time::sleep(Duration::from_secs(2)).await; - let result = tokio::time::timeout( - Duration::from_secs(9), - outbound.connect_stream(stream_request(blackhole), &context), - ) - .await - .unwrap(); - assert!(matches!( - result, - Err(vcore::dispatch::DispatchError::TimedOut) - )); - assert!( - (Duration::from_millis(9900)..Duration::from_secs(11)).contains(&started.elapsed()) - ); - assert_eq!(observer.read_u8().await.unwrap(), b'A'); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - assert!(probe.snapshot().is_idle()); - let outbound = node(f["node"].clone()); - let (mut observer, target) = origin(&f, 4, false).await; - let mut udp = outbound - .open_datagram( - datagram_request().with_budget(vcore::dispatch::DatagramBudget::new(128, 128)), - &EstablishContext::default(), - ) - .await - .unwrap(); - let packet = |size| vcore::session::Datagram { - remote: target.into(), - payload: bytes::Bytes::from(vec![7; size]), - sniffed_domain: None, - }; - assert_eq!(udp.payload_budget(&target.into()).transmit(), 128); - assert!(udp.send(packet(129)).await.is_err()); - assert!( - tokio::time::timeout(Duration::from_millis(100), observer.read_u8()) - .await - .is_err() - ); - udp.send(packet(128)).await.unwrap(); - assert_eq!(udp.receive().await.unwrap().payload, vec![7; 128]); - assert_eq!(observer.read_u16().await.unwrap(), 128); - assert_ne!(observer.read_u16().await.unwrap(), 0); - let mut observed = [0; 128]; - observer.read_exact(&mut observed).await.unwrap(); - assert_eq!(observed, [7; 128]); - udp.close().await.unwrap(); - drop(udp); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - }) - .await; - assert!(probe.snapshot().is_idle()); - event.resources(probe.snapshot()); -} - -#[tokio::test] -#[ignore = "owned isolated HYSTERIA2 peers required"] -async fn native_owned_lifecycle() { - use std::sync::{Arc, atomic::Ordering}; - use vcore::resources::{ - case_events::Case, - observation::{ResourceKind, ResourceProbe}, - }; - let f = fixture(); - for cycle in 0..20 { - let mut case = Case::new("HYSTERIA2-LIFE", "native_owned_lifecycle"); - let probe = ResourceProbe::default(); - case.checkpoint("baseline", probe.snapshot()); - probe.scope(async { - let protector=Arc::new(Protector {count:0.into(),reject_after:usize::MAX}); - if cycle%5==2 { - let (mut control,blackhole)=origin(&f,20,false).await; - let mut raw=f["node"].clone();raw["server"]=json!(blackhole.ip().to_string());raw["port"]=json!(blackhole.port()); - let outbound=node_with_dialer(raw,Dialer::default().with_protector(protector.clone())); - // Dropping the user's future cancels only the pending auth owner. - let context = EstablishContext::default(); - tokio::select! { - result=outbound.connect_stream(stream_request(blackhole),&context) => panic!("unexpected blackhole outcome: {:?}",result.err()), - marker=control.read_u8()=>assert_eq!(marker.unwrap(),b'A'), - } - tokio::time::timeout(Duration::from_secs(5),outbound.shutdown()).await.unwrap(); - assert!(tokio::time::timeout(Duration::from_millis(100),control.read_u8()).await.is_err()); - } else { - let outbound=node_with_dialer(f["node"].clone(),Dialer::default().with_protector(protector.clone())); - let (mut a,target)=origin(&f,13,false).await; - if cycle%5==3 { - assert!(matches!(outbound.connect_stream(stream_request(target),&EstablishContext::with_timeout(Duration::ZERO)).await,Err(vcore::dispatch::DispatchError::TimedOut))); - assert_eq!(protector.count.load(Ordering::SeqCst),0); - } else { - let mut first=outbound.connect_stream(stream_request(target),&EstablishContext::default()).await.unwrap().io; - first.write_all(b"a").await.unwrap();assert_eq!(first.read_u8().await.unwrap(),b'a');assert_eq!(a.read_u8().await.unwrap(),b'A'); - let (mut b,target)=origin(&f,13,false).await; - let mut second=outbound.connect_stream(stream_request(target),&EstablishContext::default()).await.unwrap().io; - second.write_all(b"b").await.unwrap();assert_eq!(second.read_u8().await.unwrap(),b'b');assert_eq!(b.read_u8().await.unwrap(),b'A'); - drop(first);assert_eq!(a.read_u8().await.unwrap(),b'D'); - second.write_all(b"sibling").await.unwrap();let mut reply=[0;7];second.read_exact(&mut reply).await.unwrap();assert_eq!(&reply,b"sibling"); - let (mut u,target)=origin(&f,4,false).await; - let mut udp=outbound.open_datagram(datagram_request(),&EstablishContext::default()).await.unwrap(); - udp.send(vcore::session::Datagram {remote:target.into(),payload:bytes::Bytes::from_static(b"udp"),sniffed_domain:None}).await.unwrap(); - assert_eq!(udp.receive().await.unwrap().payload,b"udp"[..]); - assert_eq!(u.read_u16().await.unwrap(),3);assert_ne!(u.read_u16().await.unwrap(),0); - let mut observed=[0;3];u.read_exact(&mut observed).await.unwrap();assert_eq!(&observed,b"udp"); - for kind in [ResourceKind::Socket,ResourceKind::Session,ResourceKind::Task,ResourceKind::Pool,ResourceKind::Handshake,ResourceKind::Association] {assert!(probe.snapshot().peak(kind)>0);} - outbound.begin_shutdown();assert!(second.read_u8().await.is_err());drop(second); - assert!(udp.receive().await.is_err());udp.close().await.unwrap();drop(udp); - } - tokio::time::timeout(Duration::from_secs(5),outbound.shutdown()).await.unwrap(); - } - assert!(probe.snapshot().is_idle(),"cycle {cycle}: {:?}",probe.snapshot()); - case.checkpoint("after-stop",probe.snapshot()); - let snapshot=probe.snapshot();let until=tokio::time::Instant::now()+Duration::from_secs(5); - while tokio::time::Instant::now() {}", - fixture["type"].as_str().unwrap() - ); - } - for round in 0..8 { - for upstream in first { - for endpoint in last { - close_peer_connections(fixtures); - println!( - "RUN I02 round {round} nested upstream: {} -> {}", - upstream["type"].as_str().unwrap(), - endpoint["type"].as_str().unwrap() - ); - let mut hop = upstream.clone(); - hop["name"] = json!("hop"); - let mut node = endpoint.clone(); - node["name"] = json!("peer"); - node["dialer-proxy"] = json!("outer"); - let config = json!({"socks-port":socks_port,"proxies":[node,hop],"proxy-groups":[{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop"]}],"rules":["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - probe_socks_tcp(proxy, false, false); - probe_socks_udp(proxy, false, false); - core.stop(); - println!( - "PASS I02 nested upstream: {} -> {}, TCP + UDP", - upstream["type"].as_str().unwrap(), - endpoint["type"].as_str().unwrap() - ); - } - } - } - close_peer_connections(fixtures); -} - -pub(super) fn close_peer_connections(fixtures: &Value) { - // A long-lived mihomo UDP socket can have the same numeric port as a new - // TCP/UoT source. Its loopback guard deliberately compares only source port. - // Reclaim the *owned fixture's* old connections between cases; retain the - // guard and never retry a failed packet or change production behavior. - for (index, port) in fixtures["controllers"] - .as_array() - .unwrap() - .iter() - .enumerate() - { - let address = SocketAddr::from(( - peer_ip(if index == 0 { 0 } else { 3 }), - port.as_u64().unwrap() as u16, - )); - let mut client = buffered(TcpStream::connect_timeout(&address, IO_TIMEOUT).unwrap()); - client.get_mut().write_all(b"DELETE /connections HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-controller-only\r\nConnection: close\r\n\r\n").unwrap(); - assert!(head(&mut client).starts_with("HTTP/1.1 204")); - assert_eq!( - connection_count(address), - 0, - "generated peer retained connections between cases" - ); - } -} - -pub(super) fn peer_connection_count(fixtures: &Value, index: usize) -> usize { - connection_count(SocketAddr::from(( - peer_ip(if index == 0 { 0 } else { 3 }), - fixtures["controllers"][index].as_u64().unwrap() as u16, - ))) -} - -fn connection_count(address: SocketAddr) -> usize { - let mut client = buffered(TcpStream::connect_timeout(&address, IO_TIMEOUT).unwrap()); - client.get_mut().write_all(b"GET /connections HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-controller-only\r\nConnection: close\r\n\r\n").unwrap(); - assert!(head(&mut client).starts_with("HTTP/1.1 200")); - let mut body = Vec::new(); - client.take(65_536).read_to_end(&mut body).unwrap(); - let snapshot: Value = serde_json::from_slice(&body).unwrap(); - let connections = snapshot - .get("connections") - .expect("mihomo connection snapshot"); - if connections.is_null() { - 0 - } else { - connections.as_array().unwrap().len() - } -} diff --git a/tests/mihomo/eih.rs b/tests/mihomo/eih.rs deleted file mode 100644 index 6611071..0000000 --- a/tests/mihomo/eih.rs +++ /dev/null @@ -1,261 +0,0 @@ -//! Test-only SIP022 EIH relay. It verifies and strips identity layers without -//! decrypting the payload; the final payload is handled by independent mihomo. -//! This is a wire-format fixture, not a VCore inbound or a production codec. -//! Specification: Shadowsocks-NET/shadowsocks-specs, 2022-2 extensible identity headers. -use super::*; -use aes::{ - Aes128, Aes256, Block, - cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit}, -}; -use base64::{Engine as _, engine::general_purpose::STANDARD}; - -fn block(key: &[u8], input: &[u8], decrypt: bool) -> [u8; 16] { - let mut value = Block::default(); - value.copy_from_slice(input); - match key.len() { - 16 => { - let cipher = Aes128::new_from_slice(key).unwrap(); - if decrypt { - cipher.decrypt_block(&mut value); - } else { - cipher.encrypt_block(&mut value); - } - } - 32 => { - let cipher = Aes256::new_from_slice(key).unwrap(); - if decrypt { - cipher.decrypt_block(&mut value); - } else { - cipher.encrypt_block(&mut value); - } - } - _ => panic!("fixture only accepts AES keys"), - } - value[..].try_into().unwrap() -} - -fn peel_tcp(wire: &mut Vec, keys: &[Vec]) -> bool { - let salt_size = keys[0].len(); - for pair in keys.windows(2) { - let material = [&pair[0][..], &wire[..salt_size]].concat(); - let subkey = blake3::derive_key("shadowsocks 2022 identity subkey", &material); - let identity = block(&subkey[..salt_size], &wire[salt_size..salt_size + 16], true); - if identity != blake3::hash(&pair[1]).as_bytes()[..16] { - return false; - } - wire.drain(salt_size..salt_size + 16); - } - true -} - -fn peel_udp(wire: &mut Vec, keys: &[Vec]) -> bool { - if wire.len() < 16 + 16 * (keys.len() - 1) { - return false; - } - for pair in keys.windows(2) { - let session_packet = block(&pair[0], &wire[..16], true); - let mut identity = block(&pair[0], &wire[16..32], true); - for (value, mask) in identity.iter_mut().zip(session_packet) { - *value ^= mask; - } - if identity != blake3::hash(&pair[1]).as_bytes()[..16] { - return false; - } - wire.drain(16..32); - wire[..16].copy_from_slice(&block(&pair[1], &session_packet, false)); - } - true -} - -struct Relay { - port: u16, - tasks: Vec>, -} - -impl Relay { - fn start(peer_port: u16, keys: &[Vec], tcp: bool, udp: bool) -> Self { - // TCP ephemeral allocation does not reserve the same UDP port. Select - // a jointly free pair before starting either task; do not retry traffic. - let (listener, socket) = reserve_port_pair(); - let port = listener.local_addr().unwrap().port(); - listener.set_nonblocking(true).unwrap(); - socket.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - socket.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let peer = SocketAddr::from((peer_ip(3), peer_port)); - let mut tasks = Vec::new(); - if tcp { - let keys = keys.to_vec(); - tasks.push(thread::spawn(move || { - let mut client = accept_until(&listener); - let mut prefix = vec![0; keys[0].len() + 16 * (keys.len() - 1) + 27]; - client.read_exact(&mut prefix).unwrap(); - if !peel_tcp(&mut prefix, &keys) { - return false; // No final socket is created on identity failure. - } - let mut remote = - buffered(TcpStream::connect_timeout(&peer, IO_TIMEOUT).unwrap()).into_inner(); - // Forward the complete fixed header in one write (SIP022). - remote.write_all(&prefix).unwrap(); - let mut remote_read = remote.try_clone().unwrap(); - let mut client_write = client.try_clone().unwrap(); - let receive = thread::spawn(move || { - io::copy(&mut remote_read, &mut client_write).unwrap(); - let _ = client_write.shutdown(std::net::Shutdown::Write); - }); - io::copy(&mut client, &mut remote).unwrap(); - let _ = remote.shutdown(std::net::Shutdown::Write); - receive.join().unwrap(); - true - })); - } - if udp { - let keys = keys.to_vec(); - tasks.push(thread::spawn(move || { - let mut buffer = vec![0; 65_507]; - let (length, client) = socket.recv_from(&mut buffer).unwrap(); - let mut wire = buffer[..length].to_vec(); - if !peel_udp(&mut wire, &keys) { - return false; - } - socket.send_to(&wire, peer).unwrap(); - let (length, source) = socket.recv_from(&mut buffer).unwrap(); - assert_eq!(source, peer); - socket.send_to(&buffer[..length], client).unwrap(); - true - })); - } - Self { port, tasks } - } - - fn finish(mut self, accepted: bool) { - for task in self.tasks.drain(..) { - assert_eq!(task.join().unwrap(), accepted); - } - } -} - -fn reserve_port_pair() -> (TcpListener, UdpSocket) { - for _ in 0..32 { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - match UdpSocket::bind(listener.local_addr().unwrap()) { - Ok(socket) => return (listener, socket), - Err(error) if error.kind() == io::ErrorKind::AddrInUse => continue, - Err(error) => panic!("EIH fixture UDP bind: {error}"), - } - } - panic!("EIH fixture could not reserve a TCP/UDP port pair"); -} - -impl Drop for Relay { - fn drop(&mut self) { - // Each owner has bounded I/O even if a public-path assertion fails. - for task in self.tasks.drain(..) { - let _ = task.join(); - } - } -} - -pub(super) fn probe(socks_port: u16, fixtures: &[Value]) { - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - for fixture in &fixtures[..2] { - let cipher = fixture["cipher"].as_str().unwrap(); - let final_key = STANDARD - .decode(fixture["password"].as_str().unwrap()) - .unwrap(); - for identities in [1, 2] { - let keys: Vec> = (1..=identities) - .map(|id| vec![id; final_key.len()]) - .chain([final_key.clone()]) - .collect(); - let relay = Relay::start(fixture["port"].as_u64().unwrap() as u16, &keys, true, true); - let node = json!({"name":"peer", "type":"ss", "server":origin_address(false).ip(), "port":relay.port, "cipher":cipher, "udp":true, "password":keys.iter().map(|key| STANDARD.encode(key)).collect::>().join(":")}); - let config = json!({"socks-port":socks_port, "proxies":[node], "rules":["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - probe_socks_tcp(proxy, false, false); - probe_socks_udp(proxy, false, false); - core.stop(); - relay.finish(true); - println!( - "PASS EIH {cipher}: {identities} verified identity layers -> final mihomo, TCP + UDP" - ); - } - for udp in [false, true] { - let keys = vec![vec![1; final_key.len()], final_key.clone()]; - let relay = Relay::start(fixture["port"].as_u64().unwrap() as u16, &keys, !udp, udp); - let node = json!({"name":"peer", "type":"ss", "server":origin_address(false).ip(), "port":relay.port, "cipher":cipher, "udp":true, "password":format!("{}:{}", STANDARD.encode(vec![2;final_key.len()]), STANDARD.encode(&final_key))}); - let config = json!({"socks-port":socks_port, "proxies":[node], "rules":["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - probe_denied_payload(proxy, udp); - core.stop(); - relay.finish(false); - } - println!("PASS EIH {cipher}: incorrect identity rejects TCP + UDP without forwarding"); - } - for fixture in fixtures { - let key_length = STANDARD - .decode(fixture["password"].as_str().unwrap()) - .unwrap() - .len(); - for bad_cipher in [false, true] { - let mut node = fixture.clone(); - node["name"] = json!("peer"); - node["type"] = json!("ss"); - node["server"] = json!(peer_ip(3)); - node["udp"] = json!(true); - if bad_cipher { - node["cipher"] = json!(if key_length == 16 { - "2022-blake3-aes-256-gcm" - } else { - "2022-blake3-aes-128-gcm" - }); - node["password"] = - json!(STANDARD.encode(vec![7; if key_length == 16 { 32 } else { 16 }])); - } else { - node["password"] = json!(STANDARD.encode(vec![8; key_length])); - } - let config = json!({"socks-port":socks_port, "proxies":[node], "rules":["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - probe_denied_payload(proxy, false); - probe_denied_payload(proxy, true); - core.stop(); - } - println!( - "PASS SS {}: incorrect key / cipher gives no TCP or UDP response", - fixture["cipher"].as_str().unwrap() - ); - } -} - -fn probe_denied_payload(proxy: SocketAddr, udp: bool) { - if udp { - let origin = UdpSocket::bind(origin_address(false)).unwrap(); - origin - .set_read_timeout(Some(Duration::from_millis(200))) - .unwrap(); - let mut control = socks_login(proxy, false); - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap(); - let relay = socks_reply(&mut control); - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - let mut request = vec![0, 0, 0]; - request.extend_from_slice(&socks_address(origin.local_addr().unwrap())); - request.extend_from_slice(b"private-negative-payload"); - socket.send_to(&request, relay).unwrap(); - assert!( - matches!(origin.recv_from(&mut [0;128]), Err(e) if matches!(e.kind(), io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut)) - ); - } else { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let mut client = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(listener.local_addr().unwrap())); - request.extend_from_slice(b"private-negative-payload"); - client.write_all(&request).unwrap(); - socks_reply(&mut client); - client - .set_read_timeout(Some(Duration::from_millis(200))) - .unwrap(); - assert!(!matches!(client.read(&mut [0;128]), Ok(n) if n > 0)); - assert!(matches!(listener.accept(), Err(e) if e.kind() == io::ErrorKind::WouldBlock)); - } -} diff --git a/tests/mihomo/groups.rs b/tests/mihomo/groups.rs deleted file mode 100644 index 523b980..0000000 --- a/tests/mihomo/groups.rs +++ /dev/null @@ -1,198 +0,0 @@ -//! Public Controller selection against real, separately owned mihomo hops. -use super::*; - -pub(super) struct TcpFlow { - client: TcpStream, - remote: TcpStream, -} - -impl TcpFlow { - pub(super) fn open(proxy: SocketAddr) -> Self { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let mut client = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(listener.local_addr().unwrap())); - request.extend_from_slice(b"ready"); - client.write_all(&request).unwrap(); - socks_reply(&mut client); - let mut remote = accept_until(&listener); - let mut payload = [0; 5]; - remote.read_exact(&mut payload).unwrap(); - assert_eq!(&payload, b"ready"); - remote.write_all(&payload).unwrap(); - client.read_exact(&mut payload).unwrap(); - assert_eq!(&payload, b"ready"); - Self { client, remote } - } - - pub(super) fn exchange(&mut self, marker: u8) { - let data = [marker; 257]; - self.client.write_all(&data).unwrap(); - let mut received = [0; 257]; - self.remote.read_exact(&mut received).unwrap(); - assert_eq!(received, data); - received.reverse(); - self.remote.write_all(&received).unwrap(); - self.client.read_exact(&mut received).unwrap(); - assert_eq!(received, data); - } - - fn finish(mut self) { - self.client.shutdown(std::net::Shutdown::Write).unwrap(); - assert_eq!(self.remote.read(&mut [0; 1]).unwrap(), 0); - } - - pub(super) fn assert_closed(&mut self) { - match self.client.read(&mut [0; 1]) { - Ok(0) => {} - Err(error) - if matches!( - error.kind(), - io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionAborted - ) => {} - other => panic!("closed peer left a live TCP flow: {other:?}"), - } - } -} - -pub(super) struct UdpFlow { - _control: TcpStream, - client: UdpSocket, - remote: UdpSocket, - relay: SocketAddr, -} - -impl UdpFlow { - pub(super) fn open(proxy: SocketAddr) -> Self { - let remote = UdpSocket::bind(origin_address(false)).unwrap(); - remote.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - remote.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let client = UdpSocket::bind("127.0.0.1:0").unwrap(); - client.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - client.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let mut control = socks_login(proxy, false); - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap(); - let relay = socks_reply(&mut control); - Self { - _control: control, - client, - remote, - relay, - } - } - - pub(super) fn exchange(&mut self, marker: u8) { - let data = [marker; 257]; - let mut packet = vec![0, 0, 0]; - packet.extend_from_slice(&socks_address(self.remote.local_addr().unwrap())); - packet.extend_from_slice(&data); - self.client.send_to(&packet, self.relay).unwrap(); - let mut bytes = [0; 512]; - let (length, peer) = self.remote.recv_from(&mut bytes).unwrap_or_else(|error| { - sockets::report_udp_sockets(); - panic!( - "fixture UDP request: marker={marker}, client={}, relay={}, origin={}: {error}", - self.client.local_addr().unwrap(), - self.relay, - self.remote.local_addr().unwrap(), - ) - }); - assert_eq!(&bytes[..length], data); - self.remote.send_to(&bytes[..length], peer).unwrap(); - let (length, source) = self.client.recv_from(&mut bytes).unwrap_or_else(|error| { - sockets::report_udp_sockets(); - panic!( - "fixture UDP response: marker={marker}, client={}, relay={}, origin={}, origin_observed_peer={peer}: {error}", - self.client.local_addr().unwrap(), - self.relay, - self.remote.local_addr().unwrap(), - ) - }); - assert_eq!( - source, - self.relay, - "fixture UDP response: client={}, origin={}, origin_observed_peer={peer}, length={length}", - self.client.local_addr().unwrap(), - self.remote.local_addr().unwrap(), - ); - assert_eq!(&bytes[..length], packet); - } -} - -pub(super) fn select(controller: SocketAddr, name: &str) { - let body = json!({"name":name}).to_string(); - let mut client = buffered(TcpStream::connect_timeout(&controller, IO_TIMEOUT).unwrap()); - write!(client.get_mut(), "PUT /proxies/inner HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-controller-only\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap(); - assert!(head(&mut client).starts_with("HTTP/1.1 204")); -} - -pub(super) fn probe(controller_port: u16, socks_port: u16, fixtures: &Value) { - combinations::close_peer_connections(fixtures); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, controller_port)); - let mut hop = fixtures["first"][0].clone(); - hop["name"] = json!("hop"); - let mut node = fixtures["last"][1].clone(); - node["name"] = json!("peer"); - node["dialer-proxy"] = json!("outer"); - let config = json!({"socks-port":socks_port,"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":[node,hop],"proxy-groups":[{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]}],"rules":["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - let mut first = TcpFlow::open(proxy); - let mut old_udp = UdpFlow::open(proxy); - old_udp.exchange(1); - assert_eq!( - combinations::peer_connection_count(fixtures, 0), - 2, - "the first mihomo hop must own both transports" - ); - select(controller, "REJECT"); - first.exchange(2); - old_udp.exchange(3); - // No idle session exists: a new physical connection must honor REJECT. - let origin = TcpListener::bind(origin_address(false)).unwrap(); - origin.set_nonblocking(true).unwrap(); - let mut denied = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(origin.local_addr().unwrap())); - denied.write_all(&request).unwrap(); - let mut response = [0; 10]; - denied.read_exact(&mut response).unwrap(); - assert_ne!( - response[1], 0, - "fresh session must not bypass upstream REJECT" - ); - assert_eq!( - origin.accept().unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - first.finish(); - // FIN was delivered to the origin; the physical AnyTLS session stays on - // its old SOCKS path. REJECT must not invalidate reuse of that transport. - let mut reused = TcpFlow::open(proxy); - reused.exchange(4); - select(controller, "DIRECT"); - let mut fresh = TcpFlow::open(proxy); - let mut new_udp = UdpFlow::open(proxy); - fresh.exchange(5); - new_udp.exchange(6); - reused.exchange(7); - old_udp.exchange(8); - assert_eq!( - combinations::peer_connection_count(fixtures, 0), - 2, - "DIRECT must not create extra first-hop transports" - ); - // Stop while all four flows remain owned by the test. - core.stop(); - assert_eq!(reused.client.read(&mut [0; 1]).unwrap(), 0); - assert_eq!(fresh.client.read(&mut [0; 1]).unwrap(), 0); - drop((reused, fresh, old_udp, new_udp, denied, origin)); - drop(TcpListener::bind(proxy).unwrap()); - drop(UdpSocket::bind(proxy).unwrap()); - drop(TcpListener::bind(controller).unwrap()); - combinations::close_peer_connections(fixtures); - println!( - "PASS I03/I04 public nested-group switch: live TCP/UDP, REJECT, AnyTLS idle reuse, fresh DIRECT and active Stop" - ); -} diff --git a/tests/mihomo/host.rs b/tests/mihomo/host.rs deleted file mode 100644 index 160d918..0000000 --- a/tests/mihomo/host.rs +++ /dev/null @@ -1,120 +0,0 @@ -//! HTTP-only host lifecycle and independent, concrete measurement snapshots. -use super::*; - -fn controller_get(controller: SocketAddr, path: &str) -> (String, Vec) { - let mut client = buffered(TcpStream::connect_timeout(&controller, IO_TIMEOUT).unwrap()); - write!(client.get_mut(), "GET {path} HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-controller-only\r\nConnection: close\r\n\r\n").unwrap(); - let header = head(&mut client); - let mut body = Vec::new(); - client.take(4096).read_to_end(&mut body).unwrap(); - (header, body) -} - -fn selected(controller: SocketAddr) -> String { - let (header, body) = controller_get(controller, "/proxies/inner"); - assert!(header.starts_with("HTTP/1.1 200")); - let group: Value = serde_json::from_slice(&body).unwrap(); - group["now"].as_str().unwrap().to_owned() -} - -pub(super) fn probe(http_port: u16, controller_port: u16, fixtures: &Value) { - combinations::close_peer_connections(fixtures); - let baseline = ss_lifecycle::open_fd_count(); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, http_port)); - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, controller_port)); - let mut hop = fixtures["first"][0].clone(); - hop["name"] = json!("hop"); - let mut node = fixtures["last"][2].clone(); - node["name"] = json!("peer"); - node["dialer-proxy"] = json!("inner"); - let config = json!({"port":http_port,"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":[node,hop],"proxy-groups":[{"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]}],"rules":["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - let instance = core.0.as_ref().unwrap().clone(); - assert_eq!( - invoke("getState", Some(&instance), json!({}))["state"], - "running" - ); - assert!( - controller_get(controller, "/traffic") - .0 - .starts_with("HTTP/1.1 404") - ); - assert_eq!(selected(controller), "hop"); - for mode in [Mode::Forward, Mode::Connect] { - super::probe(proxy, false, mode); - } - - // The host resolves the selected group into an immutable concrete chain. - let mut concrete = node.clone(); - concrete["dialer-proxy"] = json!(selected(controller)); - let chained = json!({"proxies":[concrete,hop]}).to_string(); - groups::select(controller, "DIRECT"); - assert_eq!(selected(controller), "DIRECT"); - let mut direct = node.clone(); - direct.as_object_mut().unwrap().remove("dialer-proxy"); - let direct = json!({"proxies":[direct]}).to_string(); - super::probe(proxy, false, Mode::Connect); - groups::select(controller, "REJECT"); - assert_eq!(selected(controller), "REJECT"); - - // New public traffic is denied, while previously captured node-only - // snapshots remain independent from the Running Session's selection. - { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let target = listener.local_addr().unwrap(); - let mut client = buffered(TcpStream::connect_timeout(&proxy, IO_TIMEOUT).unwrap()); - write!( - client.get_mut(), - "CONNECT {target} HTTP/1.1\r\nHost: {target}\r\n\r\n" - ) - .unwrap(); - assert!(head(&mut client).starts_with("HTTP/1.1 502")); - assert_eq!( - listener.accept().unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - } - { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let address = listener.local_addr().unwrap(); - let task = thread::spawn(move || { - for _ in 0..2 { - let mut remote = buffered(accept_until(&listener)); - assert!(head(&mut remote).starts_with("HEAD /snapshot ")); - remote - .get_mut() - .write_all(b"HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n") - .unwrap(); - assert_eq!(remote.read(&mut [0; 1]).unwrap(), 0); - } - }); - let data = invoke( - "measureDelay", - None, - json!({"configYamls":[chained,"proxies: []",direct],"timeout":5,"url":format!("http://{address}/snapshot")}), - ); - task.join().unwrap(); - let results = data["results"].as_array().unwrap(); - assert_eq!(results.len(), 3); - assert_eq!(results[0]["success"], true); - assert_eq!(results[1]["success"], false); - assert_eq!(results[2]["success"], true); - assert!(results[0]["delay"].as_u64().is_some()); - assert!(results[2]["delay"].as_u64().is_some()); - assert_eq!(selected(controller), "REJECT"); - } - core.stop(); - assert_eq!( - invoke_response("getState", Some(&instance), json!({}))["success"], - false - ); - drop(TcpListener::bind(proxy).unwrap()); - drop(TcpListener::bind(controller).unwrap()); - ss_lifecycle::assert_fd_returned(baseline); - combinations::close_peer_connections(fixtures); - println!( - "PASS I04 HTTP-only host lifecycle + Controller; concrete chained/DIRECT measurement snapshots, ordered per-item failure and cleanup" - ); -} diff --git a/tests/mihomo/soak.rs b/tests/mihomo/soak.rs deleted file mode 100644 index 5fbc84e..0000000 --- a/tests/mihomo/soak.rs +++ /dev/null @@ -1,228 +0,0 @@ -//! Explicit wall-clock soak, separate from fast deterministic regression tests. -use super::*; -use groups::{TcpFlow, UdpFlow}; - -fn rss_kib() -> u64 { - let output = std::process::Command::new("/bin/ps") - .args(["-o", "rss=", "-p", &std::process::id().to_string()]) - .output() - .unwrap(); - assert!(output.status.success()); - std::str::from_utf8(&output.stdout) - .unwrap() - .trim() - .parse() - .unwrap() -} - -#[cfg(target_os = "macos")] -fn heap_in_use() -> usize { - // macOS SDK malloc/malloc.h: NULL sums every malloc zone. This reads - // allocator counters only; it does not walk or log payload allocations. - #[repr(C)] - #[derive(Default)] - struct Statistics { - blocks_in_use: std::ffi::c_uint, - size_in_use: usize, - max_size_in_use: usize, - size_allocated: usize, - } - unsafe extern "C" { - fn malloc_zone_statistics(zone: *mut std::ffi::c_void, statistics: *mut Statistics); - } - let mut statistics = Statistics::default(); - // SAFETY: the SDK's C layout matches Statistics, the output is writable, - // and a null zone explicitly requests aggregate process statistics. - unsafe { malloc_zone_statistics(std::ptr::null_mut(), &mut statistics) }; - statistics.size_in_use -} - -fn open_flows(proxy: SocketAddr, controller: SocketAddr) -> (Vec, Vec, Duration) { - let started = Instant::now(); - let mut tcp = Vec::with_capacity(16); - let mut udp = Vec::with_capacity(16); - for index in 0..16 { - groups::select(controller, &format!("edge-{}", index % 4)); - tcp.push(TcpFlow::open(proxy)); - let mut flow = UdpFlow::open(proxy); - flow.exchange(index as u8); - udp.push(flow); - } - (tcp, udp, started.elapsed()) -} - -#[cfg(target_os = "macos")] -pub(super) fn recovery_probe(controller_port: u16, socks_port: u16, fixtures: &Value) { - combinations::close_peer_connections(fixtures); - let baseline_fd = ss_lifecycle::open_fd_count(); - let baseline_heap = heap_in_use(); - let baseline_rss = rss_kib(); - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, controller_port)); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - let nodes: Vec = fixtures["last"] - .as_array() - .unwrap() - .iter() - .enumerate() - .map(|(index, node)| { - let mut node = node.clone(); - node["name"] = json!(format!("edge-{index}")); - node - }) - .collect(); - let config = json!({"socks-port":socks_port,"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":nodes,"proxy-groups":[{"name":"inner","type":"select","proxies":["edge-0","edge-1","edge-2","edge-3"]}],"rules":["MATCH,inner"]}); - let core = Core::start(&config.to_string()); - println!( - "RECOVERY baseline: heap_in_use={baseline_heap}, rss_kib={baseline_rss}, fd={baseline_fd:?}" - ); - let started = Instant::now(); - for round in 0..100 { - println!("RUN recovery round {round}"); - let (mut tcp, mut udp, _) = open_flows(proxy, controller); - for (index, (tcp, udp)) in tcp.iter_mut().zip(&mut udp).enumerate() { - tcp.exchange(index as u8); - udp.exchange(index as u8); - } - if round % 10 == 0 || round == 99 { - println!( - "RECOVERY active: round={round}, heap_in_use={}, rss_kib={}, fd={:?}", - heap_in_use(), - rss_kib(), - ss_lifecycle::open_fd_count() - ); - } - combinations::close_peer_connections(fixtures); - for flow in &mut tcp { - flow.assert_closed(); - } - drop((tcp, udp)); - combinations::close_peer_connections(fixtures); - assert!( - started.elapsed() < Duration::from_secs(120), - "recovery fixture exceeded 120-second watchdog" - ); - } - core.stop(); - ss_lifecycle::assert_fd_returned(baseline_fd); - drop(TcpListener::bind(proxy).unwrap()); - drop(UdpSocket::bind(proxy).unwrap()); - drop(TcpListener::bind(controller).unwrap()); - println!( - "RECOVERY result: cycles=100, elapsed_ms={}, heap_in_use={}, rss_kib={}, fd={:?}; inspect resource trend", - started.elapsed().as_millis(), - heap_in_use(), - rss_kib(), - ss_lifecycle::open_fd_count() - ); -} - -pub(super) fn probe(controller_port: u16, socks_port: u16, fixtures: &Value, seconds: u64) { - assert!((1..=7200).contains(&seconds)); - combinations::close_peer_connections(fixtures); - let baseline_fd = ss_lifecycle::open_fd_count(); - let baseline_rss = rss_kib(); - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, controller_port)); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - let nodes: Vec = fixtures["last"] - .as_array() - .unwrap() - .iter() - .enumerate() - .map(|(index, node)| { - let mut node = node.clone(); - node["name"] = json!(format!("edge-{index}")); - node - }) - .collect(); - let config = json!({"socks-port":socks_port,"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":nodes,"proxy-groups":[{"name":"inner","type":"select","proxies":["edge-0","edge-1","edge-2","edge-3"]}],"rules":["MATCH,inner"]}); - let core = Core::start(&config.to_string()); - let (mut tcp, mut udp, latency) = open_flows(proxy, controller); - let warm_rss = rss_kib(); - let warm_fd = ss_lifecycle::open_fd_count(); - let mut peak_rss = warm_rss; - let mut peak_fd = warm_fd; - println!( - "SOAK baseline: idle_rss_kib={baseline_rss}, idle_fd={baseline_fd:?}, active_rss_kib={warm_rss}, active_fd={warm_fd:?}, open_32_flows_ms={}", - latency.as_millis() - ); - let started = Instant::now(); - let mut last_switch = Instant::now(); - let mut last_sample = Instant::now(); - let mut last_disconnect = Instant::now(); - let mut waves: u64 = 0; - let mut switches: u64 = 0; - let mut disconnects: u64 = 0; - let mut max_wave = Duration::ZERO; - let mut last_latency = latency; - while started.elapsed() < Duration::from_secs(seconds) { - let wave = Instant::now(); - for (index, (tcp, udp)) in tcp.iter_mut().zip(&mut udp).enumerate() { - let marker = (waves as u8).wrapping_add(index as u8); - tcp.exchange(marker); - udp.exchange(marker.wrapping_add(32)); - } - max_wave = max_wave.max(wave.elapsed()); - waves += 1; - if last_switch.elapsed() >= Duration::from_secs(1) { - groups::select(controller, &format!("edge-{}", switches % 4)); - switches += 1; - last_switch = Instant::now(); - } - if last_disconnect.elapsed() >= Duration::from_secs(60) { - // Close this harness's peers' accepted connections, not listeners. - // Existing TCP must terminate; recovery uses explicitly new clients. - combinations::close_peer_connections(fixtures); - for flow in &mut tcp { - flow.assert_closed(); - } - drop(tcp); - drop(udp); - combinations::close_peer_connections(fixtures); - (tcp, udp, last_latency) = open_flows(proxy, controller); - disconnects += 1; - last_disconnect = Instant::now(); - } - if last_sample.elapsed() >= Duration::from_secs(30) { - let rss = rss_kib(); - let fd = ss_lifecycle::open_fd_count(); - peak_rss = peak_rss.max(rss); - peak_fd = peak_fd.max(fd); - println!( - "SOAK sample: elapsed_s={}, waves={waves}, rss_kib={rss}, fd={fd:?}, switches={switches}, disconnects={disconnects}, max_wave_ms={}", - started.elapsed().as_secs(), - max_wave.as_millis() - ); - last_sample = Instant::now(); - } - thread::sleep(Duration::from_millis(50)); - } - let active_end_rss = rss_kib(); - let active_end_fd = ss_lifecycle::open_fd_count(); - core.stop(); - for flow in &mut tcp { - flow.assert_closed(); - } - drop((tcp, udp)); - combinations::close_peer_connections(fixtures); - ss_lifecycle::assert_fd_returned(baseline_fd); - drop(TcpListener::bind(proxy).unwrap()); - drop(UdpSocket::bind(proxy).unwrap()); - drop(TcpListener::bind(controller).unwrap()); - let bytes = waves * 16 * 2 * 2 * 257; - println!( - "SOAK result: elapsed_s={}, waves={waves}, verified_payload_bytes={bytes}, payload_bytes_per_s={}, switches={switches}, disconnects={disconnects}, peak_rss_kib={peak_rss}, active_end_rss_kib={active_end_rss}, after_stop_rss_kib={}, peak_fd={peak_fd:?}, active_end_fd={active_end_fd:?}, after_stop_fd={:?}, last_open_32_ms={}", - started.elapsed().as_secs(), - bytes / started.elapsed().as_secs().max(1), - rss_kib(), - ss_lifecycle::open_fd_count(), - last_latency.as_millis() - ); - if seconds < 1800 { - println!("PASS short soak fixture only; 30-minute acceptance NOT RUN"); - } else { - assert!(disconnects > 0 && switches > 0); - println!( - "PASS I07 wall-clock mixed soak; inspect resource samples before phase acceptance" - ); - } -} diff --git a/tests/mihomo/sockets.rs b/tests/mihomo/sockets.rs deleted file mode 100644 index 15738b9..0000000 --- a/tests/mihomo/sockets.rs +++ /dev/null @@ -1,159 +0,0 @@ -//! Test-only socket ownership. No production dialer or third-party hooks. -use socket2::{Domain, Protocol, Socket, Type}; -use std::{ - io, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs, UdpSocket}, - ops::Deref, -}; - -fn bind_one(address: SocketAddr) -> io::Result { - let socket = Socket::new( - Domain::for_address(address), - Type::DGRAM, - Some(Protocol::UDP), - )?; - if address.is_ipv6() { - socket.set_only_v6(true)?; - } - socket.bind(&address.into())?; - Ok(socket.into()) -} - -/// Reserve the same UDP port in both families for the socket's whole lifetime. -/// On Darwin, an automatically allocated IPv6 dual-stack port can otherwise -/// alias an already bound IPv4 endpoint. Neither socket enables address reuse. -pub(super) struct GuardedUdpSocket { - socket: UdpSocket, - _other_family: UdpSocket, -} - -impl GuardedUdpSocket { - pub(super) fn bind(address: impl ToSocketAddrs) -> io::Result { - let address = address - .to_socket_addrs()? - .next() - .filter(|address| { - address.ip().is_loopback() - || address.ip() == super::origin_address(address.is_ipv6()).ip() - }) - .ok_or_else(|| { - io::Error::new( - io::ErrorKind::InvalidInput, - "fixture must use loopback or owned bridge", - ) - })?; - for _ in 0..32 { - let socket = bind_one(address)?; - let other_ip = if address.is_ipv4() { - IpAddr::V6(Ipv6Addr::LOCALHOST) - } else { - IpAddr::V4(Ipv4Addr::LOCALHOST) - }; - let other = SocketAddr::new(other_ip, socket.local_addr()?.port()); - match bind_one(other) { - Ok(guard) => { - return Ok(Self { - socket, - _other_family: guard, - }); - } - Err(error) if address.port() == 0 && error.kind() == io::ErrorKind::AddrInUse => {} - Err(error) => return Err(error), - } - // Only port reservation retries; no traffic has started. - } - Err(io::Error::new( - io::ErrorKind::AddrInUse, - "could not reserve a dual-family fixture UDP port", - )) - } -} - -impl Deref for GuardedUdpSocket { - type Target = UdpSocket; - - fn deref(&self) -> &Self::Target { - &self.socket - } -} - -pub(super) fn report_udp_sockets() { - #[cfg(target_os = "macos")] - { - // Only this test process and the peer PIDs supplied by its owner. - // Do not inspect unrelated services or read packet/config contents. - let Ok(peers) = std::env::var("VCORE_MIHOMO_PEER_PIDS") else { - return; - }; - if peers.split(',').any(|pid| pid.parse::().is_err()) { - return; - } - let pids = format!("{},{}", std::process::id(), peers); - let Ok(mut child) = std::process::Command::new("/usr/sbin/lsof") - .args(["-nP", "-a", "-p", &pids, "-iUDP"]) - .stdout(std::process::Stdio::inherit()) - .stderr(std::process::Stdio::null()) - .spawn() - else { - return; - }; - let deadline = std::time::Instant::now() + super::IO_TIMEOUT; - loop { - match child.try_wait() { - Ok(Some(_)) => return, - Ok(None) if std::time::Instant::now() < deadline => { - std::thread::sleep(std::time::Duration::from_millis(10)); - } - _ => { - let _ = child.kill(); - let _ = child.wait(); - return; - } - } - } - } -} - -#[test] -fn fixture_ports_block_both_families_and_release_together() { - for address in ["127.0.0.1:0", "[::1]:0"] { - let fixture = GuardedUdpSocket::bind(address).unwrap(); - let port = fixture.local_addr().unwrap().port(); - for address in [ - SocketAddr::from((Ipv4Addr::LOCALHOST, port)), - SocketAddr::from((Ipv6Addr::LOCALHOST, port)), - ] { - assert_eq!( - bind_one(address).unwrap_err().kind(), - io::ErrorKind::AddrInUse - ); - } - let dual = Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::UDP)).unwrap(); - dual.set_only_v6(false).unwrap(); - assert_eq!( - dual.bind(&SocketAddr::from((Ipv6Addr::UNSPECIFIED, port)).into()) - .unwrap_err() - .kind(), - io::ErrorKind::AddrInUse - ); - drop(fixture); - drop(bind_one(SocketAddr::from((Ipv4Addr::LOCALHOST, port))).unwrap()); - drop(bind_one(SocketAddr::from((Ipv6Addr::LOCALHOST, port))).unwrap()); - } -} - -#[test] -fn fixture_socket_preserves_real_udp_source_and_payload() { - for address in ["127.0.0.1:0", "[::1]:0"] { - let sender = GuardedUdpSocket::bind(address).unwrap(); - let receiver = GuardedUdpSocket::bind(address).unwrap(); - receiver.set_read_timeout(Some(super::IO_TIMEOUT)).unwrap(); - sender - .send_to(b"guarded-fixture", receiver.local_addr().unwrap()) - .unwrap(); - let mut bytes = [0; 32]; - let (length, source) = receiver.recv_from(&mut bytes).unwrap(); - assert_eq!(source, sender.local_addr().unwrap()); - assert_eq!(&bytes[..length], b"guarded-fixture"); - } -} diff --git a/tests/mihomo/ss_lifecycle.rs b/tests/mihomo/ss_lifecycle.rs deleted file mode 100644 index bebf5c8..0000000 --- a/tests/mihomo/ss_lifecycle.rs +++ /dev/null @@ -1,136 +0,0 @@ -//! SS-specific public lifecycle evidence. No platform/device claims. -use super::*; - -pub(super) fn probe(socks_port: u16, fixtures: &[Value]) { - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - for fixture in fixtures { - let kind = fixture["type"].as_str().unwrap_or("ss"); - let mut node = fixture.clone(); - node["name"] = json!("peer"); - node["type"] = json!(kind); - if node.get("server").is_none() { - node["server"] = json!(peer_ip(3)); - } - node["udp"] = json!(true); - let config = - json!({"socks-port":socks_port, "proxies":[node], "rules":["MATCH,peer"]}).to_string(); - let baseline = open_fd_count(); - { - let core = Core::start(&config); - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let mut client = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(listener.local_addr().unwrap())); - request.extend_from_slice(b"alive"); - client.write_all(&request).unwrap(); - socks_reply(&mut client); - let mut remote = accept_until(&listener); - let mut bytes = [0; 5]; - remote.read_exact(&mut bytes).unwrap(); - assert_eq!(&bytes, b"alive"); - remote.write_all(b"alive").unwrap(); - client.read_exact(&mut bytes).unwrap(); - assert_eq!(&bytes, b"alive"); - let origin = UdpSocket::bind(origin_address(false)).unwrap(); - origin.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - socket.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let mut control = socks_login(proxy, false); - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap(); - let relay = socks_reply(&mut control); - let mut request = vec![0, 0, 0]; - request.extend_from_slice(&socks_address(origin.local_addr().unwrap())); - request.extend_from_slice(b"alive"); - socket.send_to(&request, relay).unwrap(); - let mut bytes = [0; 128]; - let (size, peer) = origin.recv_from(&mut bytes).unwrap(); - assert_eq!(&bytes[..size], b"alive"); - origin.send_to(&bytes[..size], peer).unwrap(); - let (size, _) = socket.recv_from(&mut bytes).unwrap(); - assert_eq!(&bytes[..size], request); - core.stop(); - assert_eq!(client.read(&mut bytes).unwrap(), 0); - assert_eq!(control.read(&mut bytes).unwrap(), 0); - assert_eq!(remote.read(&mut bytes).unwrap(), 0); - // A late response must not reach the old association after Stop. - origin.send_to(b"late", peer).unwrap(); - socket - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - assert!(socket.recv_from(&mut bytes).is_err()); - drop(TcpListener::bind(proxy).unwrap()); - drop(UdpSocket::bind(proxy).unwrap()); - } - assert_fd_returned(baseline); - // Reserve the SOCKS UDP port so startup must roll back its TCP listener. - { - let occupied = UdpSocket::bind(proxy).unwrap(); - let id = invoke("createInstance", None, json!({}))["instanceId"] - .as_str() - .unwrap() - .to_owned(); - let owner = Core(Some(id)); - invoke("prepare", owner.0.as_deref(), json!({"configYaml":config})); - assert_eq!( - invoke_response("start", owner.0.as_deref(), json!({}))["success"], - false - ); - assert_ne!( - invoke("getState", owner.0.as_deref(), json!({}))["state"], - "running" - ); - owner.stop(); - drop(TcpListener::bind(proxy).unwrap()); - drop(occupied); - drop(UdpSocket::bind(proxy).unwrap()); - } - assert_fd_returned(baseline); - { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let address = listener.local_addr().unwrap(); - let task = thread::spawn(move || { - let mut remote = buffered(accept_until(&listener)); - assert!(head(&mut remote).starts_with("HEAD /")); - remote - .get_mut() - .write_all(b"HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n") - .unwrap(); - assert_eq!(remote.read(&mut [0; 1]).unwrap(), 0); - }); - let data = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[node]}).to_string()],"timeout":5,"url":format!("http://{address}/")}), - ); - task.join().unwrap(); - assert_eq!(data["results"][0]["success"], true); - } - assert_fd_returned(baseline); - println!( - "PASS lifecycle {}: active TCP/UDP Stop, failed-start rollback, measurement cleanup; FD baseline={baseline:?}", - fixture["cipher"].as_str().unwrap_or(kind) - ); - } -} - -pub(super) fn open_fd_count() -> Option { - #[cfg(any(target_os = "macos", target_os = "ios"))] - { - Some(std::fs::read_dir("/dev/fd").unwrap().count()) - } - #[cfg(not(any(target_os = "macos", target_os = "ios")))] - { - None - } -} - -pub(super) fn assert_fd_returned(baseline: Option) { - if let Some(baseline) = baseline { - assert!( - open_fd_count().unwrap() <= baseline, - "VCore process retained descriptors after cleanup" - ); - } -} diff --git a/tests/mihomo/trojan.rs b/tests/mihomo/trojan.rs deleted file mode 100644 index fce3ae3..0000000 --- a/tests/mihomo/trojan.rs +++ /dev/null @@ -1,402 +0,0 @@ -//! Trojan consumer checks through public YAML and Invoke, owned by the TROJAN runner. -use super::*; -use sha2::{Digest, Sha256}; -use vcore::resources::case_events::Case; - -#[path = "trojan_runtime.rs"] -mod runtime; - -fn fixture() -> Value { - serde_json::from_str(&env::var("VCORE_TROJAN_FIXTURE").expect("use the TROJAN runner")).unwrap() -} - -fn free_port() -> u16 { - for _ in 0..32 { - let tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let port = tcp.local_addr().unwrap().port(); - match reserve_runtime_families(tcp) { - Ok(_reservation) => return port, - Err(error) if error.kind() == io::ErrorKind::AddrInUse => {} - Err(error) => panic!("runtime fixture port reservation: {error}"), - } - // Only pre-traffic reservation retries, never failed runtime starts. - } - panic!("could not reserve a dual-family TCP/UDP runtime fixture port"); -} - -fn reserve_runtime_families(tcp: TcpListener) -> io::Result<(TcpListener, TcpListener, UdpSocket)> { - let address = tcp.local_addr()?; - let tcp_v6 = TcpListener::bind((Ipv6Addr::LOCALHOST, address.port()))?; - let udp = UdpSocket::bind(address)?; - Ok((tcp, tcp_v6, udp)) -} - -#[test] -fn fixture_runtime_port_rejects_occupied_udp_and_ipv6_tcp() { - for udp_busy in [true, false] { - let tcp = TcpListener::bind("127.0.0.1:0").unwrap(); - let address = tcp.local_addr().unwrap(); - if udp_busy { - let _occupied = UdpSocket::bind(address).unwrap(); - assert!(reserve_runtime_families(tcp).is_err()); - } else { - let _occupied = TcpListener::bind((Ipv6Addr::LOCALHOST, address.port())).unwrap(); - assert!(reserve_runtime_families(tcp).is_err()); - } - } -} - -fn config(node: Value, port: u16) -> Value { - json!({"socks-port":port,"ipv6":true,"proxies":[node],"rules":["MATCH,peer"]}) -} - -fn tcp_base(proxy: SocketAddr, ipv6: bool, domain: bool) { - let listener = TcpListener::bind(origin_address(ipv6)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let target = listener.local_addr().unwrap(); - let mut client = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&fixture_address(target, domain)); - client.write_all(&request).unwrap(); - socks_reply(&mut client); - let mut remote = accept_until(&listener); - remote.write_all(b"server-first").unwrap(); - let mut greeting = [0; 12]; - client.read_exact(&mut greeting).unwrap(); - assert_eq!(&greeting, b"server-first"); - let payload: Vec = (0..10 * 1024 * 1024).map(|n| (n % 251) as u8).collect(); - let expected = Sha256::digest(&payload); - let origin = thread::spawn(move || { - let mut hash = Sha256::new(); - let mut received = 0; - let mut buffer = [0; 8191]; - while received < 10 * 1024 * 1024 { - let n = remote.read(&mut buffer).unwrap(); - assert_ne!(n, 0, "truncated upload"); - received += n; - hash.update(&buffer[..n]); - remote.write_all(&buffer[..n]).unwrap(); - } - assert_eq!(hash.finalize(), expected); - assert_eq!(remote.read(&mut [0; 1]).unwrap(), 0, "upload EOF"); - remote.write_all(b"half-close-tail").unwrap(); - remote.shutdown(std::net::Shutdown::Write).unwrap(); - }); - let mut upload = client.try_clone().unwrap(); - let writer = thread::spawn(move || { - for fragment in payload.chunks(4093) { - upload.write_all(fragment).unwrap(); - } - upload.shutdown(std::net::Shutdown::Write).unwrap(); - }); - let mut hash = Sha256::new(); - let mut remaining = 10 * 1024 * 1024; - let mut buffer = [0; 6151]; - while remaining > 0 { - let size = buffer.len().min(remaining); - let n = client.read(&mut buffer[..size]).unwrap(); - assert_ne!(n, 0, "truncated download"); - remaining -= n; - hash.update(&buffer[..n]); - } - assert_eq!(hash.finalize(), expected); - let mut tail = Vec::new(); - client.read_to_end(&mut tail).unwrap(); - assert_eq!(tail, b"half-close-tail"); - writer.join().unwrap(); - origin.join().unwrap(); -} - -fn udp_base(proxy: SocketAddr, ipv6: bool, domain: bool) { - let remote = UdpSocket::bind(origin_address(ipv6)).unwrap(); - remote.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let target = remote.local_addr().unwrap(); - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - socket.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let mut control = socks_login(proxy, false); - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap(); - let relay = socks_reply(&mut control); - let mut request_header = vec![0, 0, 0]; - request_header.extend_from_slice(&fixture_address(target, domain)); - let mut response_header = vec![0, 0, 0]; - // Native Xray preserves the domain-form source; Mihomo literal cases - // preserve the literal source. Neither may be silently rewritten by VCore. - response_header.extend_from_slice(&fixture_address(target, domain)); - let mut buffer = [0; 9000]; - // Xray's Trojan writer uses an 8192-byte total-frame buffer (address + - // length + CRLF + payload). Mihomo's literal path permits 8192 payload. - // Exercise each real path's maximum; VCore's 8192/8193 codec boundary is - // separately tested, never inferred from a peer's smaller buffer. - let maximum = fixture()["udp_payload_max"].as_u64().unwrap() as usize; - for size in [1, 64, 512, 1200, maximum] { - for sequence in 0..100u8 { - let payload: Vec = (0..size) - .map(|n| (n as u8).wrapping_add(sequence)) - .collect(); - let mut request = request_header.clone(); - request.extend_from_slice(&payload); - socket.send_to(&request, relay).unwrap(); - let (n, peer) = remote.recv_from(&mut buffer).unwrap_or_else(|error| panic!("Trojan UDP upload ipv6={ipv6} domain={domain} size={size} sequence={sequence}: {error}")); - assert_eq!(&buffer[..n], payload); - remote.send_to(&buffer[..n], peer).unwrap(); - let (n, from) = socket.recv_from(&mut buffer).unwrap_or_else(|error| panic!("Trojan UDP download ipv6={ipv6} domain={domain} size={size} sequence={sequence}: {error}")); - assert_eq!(from, relay); - assert_eq!(&buffer[..response_header.len()], response_header); - assert_eq!( - n - response_header.len(), - payload.len(), - "UDP response length size={size} sequence={sequence}" - ); - assert_eq!( - Sha256::digest(&buffer[response_header.len()..n]), - Sha256::digest(&payload), - "UDP response content size={size} sequence={sequence}" - ); - } - } -} - -fn measurement(node: &Value) { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let address = listener.local_addr().unwrap(); - let origin = thread::spawn(move || { - let mut peer = buffered(accept_until(&listener)); - assert!(head(&mut peer).starts_with("HEAD /")); - peer.get_mut() - .write_all(b"HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n") - .unwrap(); - }); - let response = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[node]}).to_string()],"timeout":5,"url":format!("http://{address}/")}), - ); - origin.join().unwrap(); - assert_eq!(response["results"][0]["success"], true); -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_base() { - let _case = Case::new("TROJAN-NATIVE", "public_trojan_native_base"); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let port = free_port(); - let node = &fixture["node"]; - let core = Core::start(&config(node.clone(), port).to_string()); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - tcp_base(proxy, ipv6, domain); - if !domain { - udp_base(proxy, ipv6, domain); - } - } - core.stop(); - drop(TcpListener::bind(proxy).unwrap()); - drop(UdpSocket::bind(proxy).unwrap()); - measurement(node); -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_udp_domain() { - let _case = Case::new("TROJAN-NATIVE", "public_trojan_native_udp_domain"); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let port = free_port(); - let core = Core::start(&config(fixture["node"].clone(), port).to_string()); - udp_base(SocketAddr::from((Ipv4Addr::LOCALHOST, port)), false, true); - core.stop(); - drop(TcpListener::bind((Ipv4Addr::LOCALHOST, port)).unwrap()); - drop(UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap()); -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_extended_early_data() { - let _case = Case::new("TROJAN-NATIVE", "public_trojan_native_extended_early_data"); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let port = free_port(); - let core = Core::start(&config(fixture["node"].clone(), port).to_string()); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - for server_first in [false, true] { - probe_socks_tcp_target(proxy, false, ipv6, domain, server_first); - } - udp_base(proxy, ipv6, domain); - } - core.stop(); - measurement(&fixture["node"]); -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_transport_negative() { - let _case = Case::new("TROJAN-NATIVE", "public_trojan_native_transport_negative"); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let mut node = fixture["node"].clone(); - match fixture["mode"].as_str().unwrap() { - "ws" => { - node["ws-opts"]["path"] = json!("/wrong"); - assert_no_origin_bytes(node.clone()); - node = fixture["node"].clone(); - node["ws-opts"]["headers"]["Host"] = json!("wrong.example"); - } - "grpc" => { - node["grpc-opts"]["grpc-service-name"] = json!("wrong"); - } - "ws-alpn" => {} - _ => panic!("invalid negative fixture"), - } - assert_no_origin_bytes(node); -} - -fn assert_no_origin_bytes(node: Value) { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let port = free_port(); - let core = Core::start(&config(node, port).to_string()); - let mut client = socks_login(SocketAddr::from((Ipv4Addr::LOCALHOST, port)), false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(listener.local_addr().unwrap())); - request.extend_from_slice(b"must-not-reach-origin"); - client.write_all(&request).unwrap(); - let mut response = [0; 10]; - client.read_exact(&mut response).unwrap(); - // Trojan has no authentication ACK; a successful local CONNECT is not an - // authenticated-peer claim. Both a rejected CONNECT and subsequent EOF - // must fail closed without delivering business bytes to the origin. - if response[1] == 0 { - match client.read(&mut [0; 1]) { - Ok(0) => {} - Err(error) - if matches!( - error.kind(), - io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionAborted - ) => {} - other => panic!("authentication failure remained usable: {other:?}"), - } - } - assert_eq!( - listener.accept().unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - core.stop(); -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_policy_and_group_snapshots() { - let _case = Case::new( - "TROJAN-NATIVE", - "public_trojan_native_policy_and_group_snapshots", - ); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let original = fixture["node"].clone(); - let mut wrong_password = original.clone(); - wrong_password["password"] = json!("wrong-credential"); - assert_no_origin_bytes(wrong_password); - let mut untrusted = original.clone(); - untrusted.as_object_mut().unwrap().remove("fingerprint"); - assert_no_origin_bytes(untrusted.clone()); - let mut wrong_pin = original.clone(); - wrong_pin["fingerprint"] = json!("00".repeat(32)); - wrong_pin["skip-cert-verify"] = json!(true); - assert_no_origin_bytes(wrong_pin); - // skip only bypasses PKI; a supplied pin continues to be mandatory. - for mut node in [original.clone(), untrusted] { - node["skip-cert-verify"] = json!(true); - let port = free_port(); - let core = Core::start(&config(node, port).to_string()); - probe_socks_tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port)), false, false); - core.stop(); - } - println!( - "TROJAN policy: wrong password, untrusted certificate, wrong pin, skip and pin passed" - ); - // A concrete Trojan upstream is also usable as the physical first hop. - let port = free_port(); - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, free_port())); - let hop = fixture["hop"].clone(); - let mut leaf = original.clone(); - leaf["dialer-proxy"] = json!("hop"); - // Only the concrete upstream resolves this synthetic server name. VCore - // must not turn it into a local direct socket or invoke system DNS. - leaf["server"] = json!("vcore-fixture.test"); - let mut document = config(leaf.clone(), port); - document["proxies"] - .as_array_mut() - .unwrap() - .push(hop.clone()); - let core = Core::start(&document.to_string()); - println!("TROJAN policy: checking concrete upstream"); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - probe_socks_tcp(proxy, false, false); - probe_socks_udp(proxy, false, false); - core.stop(); - leaf["server"] = original["server"].clone(); - leaf["dialer-proxy"] = json!("outer"); - let document = json!({"socks-port":port,"ipv6":true,"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":[leaf,hop],"proxy-groups":[{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]}],"rules":["MATCH,peer"]}); - let core = Core::start(&document.to_string()); - println!("TROJAN policy: checking nested group snapshots"); - let mut tcp = groups::TcpFlow::open(proxy); - let mut udp = groups::UdpFlow::open(proxy); - udp.exchange(1); - groups::select(controller, "REJECT"); - tcp.exchange(2); - udp.exchange(3); - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let mut denied = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(listener.local_addr().unwrap())); - denied.write_all(&request).unwrap(); - let mut response = [0; 10]; - denied.read_exact(&mut response).unwrap(); - assert_ne!(response[1], 0); - assert_eq!( - listener.accept().unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - groups::select(controller, "DIRECT"); - probe_socks_tcp(proxy, false, false); - probe_socks_udp(proxy, false, false); - tcp.exchange(4); - udp.exchange(5); - core.stop(); - tcp.assert_closed(); - drop((tcp, udp)); - drop(TcpListener::bind(proxy).unwrap()); - drop(UdpSocket::bind(proxy).unwrap()); - // Public protocol gates do not become dependent on the selected transport. - let mut disabled = original; - disabled["udp"] = json!(false); - let mut document = config(disabled, port); - document["ipv6"] = json!(false); - let core = Core::start(&document.to_string()); - probe_socks_tcp(proxy, false, false); - let origin = UdpSocket::bind("127.0.0.1:0").unwrap(); - origin - .set_read_timeout(Some(Duration::from_millis(150))) - .unwrap(); - let client = UdpSocket::bind("127.0.0.1:0").unwrap(); - let mut control = socks_login(proxy, false); - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap(); - let relay = socks_reply(&mut control); - let mut packet = vec![0, 0, 0]; - packet.extend_from_slice(&socks_address(origin.local_addr().unwrap())); - packet.push(1); - client.send_to(&packet, relay).unwrap(); - assert!(origin.recv_from(&mut [0; 32]).is_err()); - let mut denied = socks_login(proxy, false); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&socks_address(SocketAddr::from((Ipv6Addr::LOCALHOST, 9)))); - denied.write_all(&request).unwrap(); - denied.read_exact(&mut response).unwrap(); - assert_ne!(response[1], 0); - core.stop(); -} diff --git a/tests/mihomo/trojan_runtime.rs b/tests/mihomo/trojan_runtime.rs deleted file mode 100644 index a74d24a..0000000 --- a/tests/mihomo/trojan_runtime.rs +++ /dev/null @@ -1,337 +0,0 @@ -//! Consumer entrypoints and lifecycle against a real owned Mihomo listener. -use super::*; -#[cfg(any(target_os = "macos", target_os = "ios"))] -use std::os::{fd::AsRawFd, unix::net::UnixDatagram}; - -struct Association { - control: TcpStream, - socket: UdpSocket, - relay: SocketAddr, - origin: UdpSocket, - peer: Option, -} - -impl Association { - fn open(proxy: SocketAddr) -> Self { - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - socket.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let mut control = socks_login(proxy, false); - let mut request = vec![5, 3, 0]; - request.extend_from_slice(&socks_address(socket.local_addr().unwrap())); - control.write_all(&request).unwrap(); - let relay = socks_reply(&mut control); - let origin = UdpSocket::bind("127.0.0.1:0").unwrap(); - origin.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - Self { - control, - socket, - relay, - origin, - peer: None, - } - } - - fn request(&self, payload: &[u8]) -> Vec { - let mut packet = vec![0, 0, 0]; - packet.extend_from_slice(&socks_address(self.origin.local_addr().unwrap())); - packet.extend_from_slice(payload); - packet - } - - fn exchange(&mut self, payload: &[u8]) { - let packet = self.request(payload); - self.socket.send_to(&packet, self.relay).unwrap(); - let mut buffer = [0; 9000]; - let (n, peer) = self.origin.recv_from(&mut buffer).unwrap(); - assert_eq!(&buffer[..n], payload); - self.peer = Some(peer); - self.origin.send_to(&buffer[..n], peer).unwrap(); - let (n, source) = self.socket.recv_from(&mut buffer).unwrap(); - assert_eq!(source, self.relay); - assert_eq!(&buffer[..n], packet); - } - - fn assert_quiet(&self) { - let mut bytes = [0; 9000]; - self.socket.set_nonblocking(true).unwrap(); - self.origin.set_nonblocking(true).unwrap(); - assert_eq!( - self.socket.recv_from(&mut bytes).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - assert_eq!( - self.origin.recv_from(&mut bytes).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - } -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_lifecycle() { - let _case = Case::new("TROJAN-NATIVE", "runtime::public_trojan_native_lifecycle"); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - // The process-wide Invoke worker is lazily created by the first instance; - // it is not owned by a Running Session. Establish that baseline once. - Core::start(&config(fixture["node"].clone(), free_port()).to_string()).stop(); - for cycle in 0..20 { - let cycle_case = Case::new("TROJAN-LIFE-CYCLE", "stop_and_remain_quiet"); - let baseline = ss_lifecycle::open_fd_count(); - let port = free_port(); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - let yaml = config(fixture["node"].clone(), port).to_string(); - let mut associations = Vec::new(); - let mut stalled = None; - if cycle % 5 == 0 { - let core = Core::start(&yaml); - probe_socks_tcp(proxy, false, false); - probe_socks_udp(proxy, false, false); - core.stop(); - } else if cycle % 5 == 3 { - let occupied = UdpSocket::bind(proxy).unwrap(); - let id = invoke("createInstance", None, json!({}))["instanceId"] - .as_str() - .unwrap() - .to_owned(); - let core = Core(Some(id)); - invoke("prepare", core.0.as_deref(), json!({"configYaml":yaml})); - assert_eq!( - invoke_response("start", core.0.as_deref(), json!({}))["success"], - false - ); - assert_ne!( - invoke("getState", core.0.as_deref(), json!({}))["state"], - "running" - ); - core.stop(); - drop(occupied); - } else if cycle % 5 == 2 { - let blackhole = TcpListener::bind("127.0.0.1:0").unwrap(); - blackhole.set_nonblocking(true).unwrap(); - let mut node = fixture["node"].clone(); - node["port"] = json!(blackhole.local_addr().unwrap().port()); - let core = Core::start(&config(node, port).to_string()); - let mut client = socks_login(proxy, false); - client.write_all(&[5, 1, 0, 1, 127, 0, 0, 1, 0, 9]).unwrap(); - let mut remote = accept_until(&blackhole); - assert!(remote.read(&mut [0; 8192]).unwrap() > 0); - core.stop(); - let mut bytes = Vec::new(); - remote.read_to_end(&mut bytes).unwrap(); - drop((remote, client)); - // Keep the blackhole listening: any resumed/retried handshake is - // detected during the post-Stop window, not merely dropped. - stalled = Some(blackhole); - } else { - let core = Core::start(&yaml); - let mut first = groups::TcpFlow::open(proxy); - let mut second = groups::TcpFlow::open(proxy); - first.exchange(1); - second.exchange(2); - let mut udp1 = Association::open(proxy); - let mut udp2 = Association::open(proxy); - udp1.exchange(b"association-one"); - udp2.exchange(b"association-two"); - if cycle % 5 == 4 { - drop(first); - second.exchange(3); - udp1.exchange(b"after-one-tcp-cancelled"); - udp2.exchange(b"other-association-still-live"); - } else { - first.exchange(4); - drop(first); - } - core.stop(); - second.assert_closed(); - for udp in [&mut udp1, &mut udp2] { - assert_eq!(udp.control.read(&mut [0; 1]).unwrap(), 0); - udp.origin - .send_to(b"late-after-stop", udp.peer.unwrap()) - .unwrap(); - } - drop(second); - associations.extend([udp1, udp2]); - } - // Only caller-owned fixture fds remain: each association has two - // dual-family UDP sockets (four fds) and its TCP control (one fd). - let retained_fixture_fds = associations.len() * 5 + usize::from(stalled.is_some()); - ss_lifecycle::assert_fd_returned(baseline.map(|count| count + retained_fixture_fds)); - // Port rebind occurs immediately at Stop return, before any quiet wait. - let tcp_guard = TcpListener::bind(proxy).unwrap(); - let udp_guard = UdpSocket::bind(proxy).unwrap(); - let quiet = Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - ss_lifecycle::assert_fd_returned( - baseline.map(|count| count + retained_fixture_fds + 3), - ); - for association in &associations { - association.assert_quiet(); - } - if let Some(listener) = &stalled { - assert_eq!( - listener.accept().unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - } - thread::sleep(Duration::from_millis(25)); - } - drop((tcp_guard, udp_guard, associations, stalled)); - ss_lifecycle::assert_fd_returned(baseline); - drop(cycle_case); - } -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_certificate_names() { - let _case = Case::new( - "TROJAN-NATIVE", - "runtime::public_trojan_native_certificate_names", - ); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let node = fixture["node"].clone(); - let port = free_port(); - let core = Core::start(&config(node.clone(), port).to_string()); - probe_socks_tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port)), false, false); - core.stop(); - let mut wrong_name = node.clone(); - wrong_name["sni"] = json!("wrong.example"); - assert_no_origin_bytes(wrong_name.clone()); - wrong_name["skip-cert-verify"] = json!(true); - // A non-leaf pin keeps chain/name validation even when skip is enabled. - assert_no_origin_bytes(wrong_name.clone()); - wrong_name.as_object_mut().unwrap().remove("fingerprint"); - let core = Core::start(&config(wrong_name, port).to_string()); - probe_socks_tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port)), false, false); - core.stop(); - let mut wrong_pin = node; - wrong_pin["fingerprint"] = json!("11".repeat(32)); - assert_no_origin_bytes(wrong_pin); -} - -#[test] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_udp_isolation_and_limit() { - let _case = Case::new( - "TROJAN-NATIVE", - "runtime::public_trojan_native_udp_isolation_and_limit", - ); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let port = free_port(); - let core = Core::start(&config(fixture["node"].clone(), port).to_string()); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - let mut first = Association::open(proxy); - let mut second = Association::open(proxy); - for n in 0..100u8 { - first.exchange(&[1, n]); - second.exchange(&[2, n]); - } - // Unauthorised source must not enter either association. - let rogue = UdpSocket::bind("127.0.0.1:0").unwrap(); - rogue - .send_to(&first.request(b"rogue"), first.relay) - .unwrap(); - first - .origin - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - assert!(first.origin.recv_from(&mut [0; 9000]).is_err()); - first - .socket - .send_to(&first.request(&[7; 8193]), first.relay) - .unwrap(); - assert!(first.origin.recv_from(&mut [0; 9000]).is_err()); - second.exchange(b"oversize-on-other-association-did-not-leak"); - core.stop(); -} - -#[test] -#[cfg(any(target_os = "macos", target_os = "ios"))] -#[ignore = "requires the owned TROJAN native-peer runner"] -fn public_trojan_native_entrypoints() { - let _case = Case::new("TROJAN-NATIVE", "runtime::public_trojan_native_entrypoints"); - let fixture = fixture(); - invoke("initialize", None, json!({"dataDir":fixture["data_dir"]})); - let port = free_port(); - let yaml = json!({"port":port,"proxies":[fixture["node"]],"rules":["MATCH,peer"]}); - let core = Core::start(&yaml.to_string()); - for mode in [Mode::Forward, Mode::Chunked, Mode::Connect, Mode::Upgrade] { - probe(SocketAddr::from((Ipv4Addr::LOCALHOST, port)), false, mode); - } - core.stop(); - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, free_port())); - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let dns = UdpSocket::bind("127.0.0.1:0").unwrap(); - dns.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - let yaml = json!({"tun":{"enable":true},"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":[fixture["node"]],"rules":["MATCH,peer"],"dns":{"enable":true,"ipv6":false,"nameserver":[format!("udp://{}#peer",dns.local_addr().unwrap())]}}); - let core = Core::start_with( - &yaml.to_string(), - json!({"tunFd":host.as_raw_fd(),"tunFraming":"utun"}), - ); - let origin = UdpSocket::bind("127.0.0.1:0").unwrap(); - origin.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - peer.send(&tun::packet( - origin.local_addr().unwrap(), - 17, - (0, 0, 0), - b"tun-udp", - )) - .unwrap(); - let mut bytes = [0; 1500]; - let (n, source) = origin.recv_from(&mut bytes).unwrap(); - assert_eq!(&bytes[..n], b"tun-udp"); - origin.send_to(b"tun-reply", source).unwrap(); - assert_eq!(&tun::receive(&peer, 17)[28..], b"tun-reply"); - let listener = TcpListener::bind("127.0.0.1:0").unwrap(); - listener.set_nonblocking(true).unwrap(); - let target = listener.local_addr().unwrap(); - peer.send(&tun::packet(target, 6, (1, 0, 2), &[])).unwrap(); - let syn_ack = tun::receive(&peer, 6); - assert_eq!(syn_ack[33] & 0x12, 0x12); - let seq = u32::from_be_bytes(syn_ack[24..28].try_into().unwrap()).wrapping_add(1); - peer.send(&tun::packet(target, 6, (2, seq, 0x18), b"tun-tcp")) - .unwrap(); - let mut remote = accept_until(&listener); - remote.read_exact(&mut bytes[..7]).unwrap(); - assert_eq!(&bytes[..7], b"tun-tcp"); - remote.write_all(b"tun-tcp-reply").unwrap(); - let mut received = Vec::new(); - while received.len() < 13 { - let packet = tun::receive(&peer, 6); - let offset = 20 + usize::from(packet[32] >> 4) * 4; - received.extend_from_slice(&packet[offset..]); - } - assert_eq!(received, b"tun-tcp-reply"); - // Synthetic DNS A question through the TUN DNS interception path. No host - // resolver, route, firewall or network service is modified. - let query = b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07fixture\x07invalid\x00\x00\x01\x00\x01"; - peer.send(&tun::packet( - "198.18.0.1:53".parse().unwrap(), - 17, - (0, 0, 0), - query, - )) - .unwrap(); - let (n, source) = dns.recv_from(&mut bytes).unwrap(); - assert_eq!(&bytes[12..n], &query[12..]); - let mut response = bytes[..n].to_vec(); - response[2..4].copy_from_slice(&[0x81, 0x80]); - response[6..8].copy_from_slice(&[0, 1]); - response.extend_from_slice(b"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04\xc0\x00\x02\x01"); - dns.send_to(&response, source).unwrap(); - let response = tun::receive(&peer, 17); - assert_eq!(&response[28..30], &query[..2]); - assert!(response.ends_with(&[192, 0, 2, 1])); - let traffic = tun::traffic(controller); - assert!(traffic["upTotal"].as_u64().unwrap() > 0); - assert!(traffic["downTotal"].as_u64().unwrap() > 0); - core.stop(); - assert_eq!(remote.read(&mut [0; 1]).unwrap(), 0); - assert!(unsafe { libc::fcntl(host.as_raw_fd(), libc::F_GETFL) } & libc::O_NONBLOCK != 0); - drop(TcpListener::bind(controller).unwrap()); -} diff --git a/tests/mihomo/tun.rs b/tests/mihomo/tun.rs deleted file mode 100644 index abb5992..0000000 --- a/tests/mihomo/tun.rs +++ /dev/null @@ -1,186 +0,0 @@ -//! Host-fd/netstack interop only: no real interface, route or system proxy. -use super::*; -use std::os::{fd::AsRawFd as _, unix::net::UnixDatagram}; - -fn checksum(bytes: &[u8]) -> u16 { - let mut sum: u32 = bytes - .chunks(2) - .map(|pair| u32::from(u16::from_be_bytes([pair[0], *pair.get(1).unwrap_or(&0)]))) - .sum(); - while sum >> 16 != 0 { - sum = (sum & 0xffff) + (sum >> 16); - } - !(sum as u16) -} - -pub(super) fn packet( - target: SocketAddr, - protocol: u8, - seq_ack_flags: (u32, u32, u8), - payload: &[u8], -) -> Vec { - let SocketAddr::V4(target) = target else { - panic!("IPv4 fixture"); - }; - let source = Ipv4Addr::new(192, 0, 2, 10); - let mut segment = vec![0; if protocol == 6 { 20 } else { 8 }]; - segment[..2].copy_from_slice(&12000_u16.to_be_bytes()); - segment[2..4].copy_from_slice(&target.port().to_be_bytes()); - if protocol == 6 { - segment[4..8].copy_from_slice(&seq_ack_flags.0.to_be_bytes()); - segment[8..12].copy_from_slice(&seq_ack_flags.1.to_be_bytes()); - segment[12] = 0x50; - segment[13] = seq_ack_flags.2; - segment[14..16].copy_from_slice(&u16::MAX.to_be_bytes()); - } else { - segment[4..6].copy_from_slice(&((8 + payload.len()) as u16).to_be_bytes()); - } - segment.extend_from_slice(payload); - let mut pseudo = source.octets().to_vec(); - pseudo.extend_from_slice(&target.ip().octets()); - pseudo.extend_from_slice(&[0, protocol]); - pseudo.extend_from_slice(&(segment.len() as u16).to_be_bytes()); - pseudo.extend_from_slice(&segment); - let offset = if protocol == 6 { 16 } else { 6 }; - let value = checksum(&pseudo); - segment[offset..offset + 2].copy_from_slice(&value.to_be_bytes()); - let mut ip = vec![0; 20]; - ip[0] = 0x45; - ip[2..4].copy_from_slice(&((20 + segment.len()) as u16).to_be_bytes()); - ip[6] = 0x40; - ip[8] = 64; - ip[9] = protocol; - ip[12..16].copy_from_slice(&source.octets()); - ip[16..20].copy_from_slice(&target.ip().octets()); - let value = checksum(&ip); - ip[10..12].copy_from_slice(&value.to_be_bytes()); - ip.extend_from_slice(&segment); - let mut framed = 2_u32.to_be_bytes().to_vec(); // Darwin AF_INET PI header. - framed.extend_from_slice(&ip); - framed -} - -pub(super) fn receive(peer: &UnixDatagram, protocol: u8) -> Vec { - let deadline = Instant::now() + IO_TIMEOUT; - loop { - assert!(Instant::now() < deadline, "TUN packet deadline"); - let mut bytes = [0; 1504]; - let size = peer.recv(&mut bytes).unwrap(); - assert!(size >= 24); - assert_eq!(&bytes[..4], 2_u32.to_be_bytes()); - let ip = &bytes[4..size]; - assert_eq!(checksum(&ip[..20]), 0); - if ip[9] == protocol { - return ip.to_vec(); - } - } -} - -pub(super) fn traffic(controller: SocketAddr) -> Value { - let mut stream = buffered(TcpStream::connect_timeout(&controller, IO_TIMEOUT).unwrap()); - stream.get_mut().write_all(b"GET /traffic HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-controller-only\r\nConnection: close\r\n\r\n").unwrap(); - assert!(head(&mut stream).starts_with("HTTP/1.1 200")); - let mut body = Vec::new(); - stream.take(1024).read_to_end(&mut body).unwrap(); - serde_json::from_slice(&body).unwrap() -} - -pub(super) fn probe(controller_port: u16, socks_port: u16, fixtures: &Value) { - let controller = SocketAddr::from((Ipv4Addr::LOCALHOST, controller_port)); - let IpAddr::V4(origin_ip) = origin_address(false).ip() else { - panic!("IPv4 fixture origin"); - }; - for fixture in fixtures["last"].as_array().unwrap() { - combinations::close_peer_connections(fixtures); - let baseline = ss_lifecycle::open_fd_count(); - let mut node = fixture.clone(); - node["name"] = json!("peer"); - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - peer.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let config = json!({"tun":{"enable":true},"socks-port":socks_port,"external-controller":controller.to_string(),"secret":"fixture-controller-only","proxies":[node],"proxy-groups":[{"name":"inner","type":"select","proxies":["peer","REJECT"]}],"rules":["MATCH,inner"]}); - let core = Core::start_with( - &config.to_string(), - json!({"tunFd":host.as_raw_fd(),"tunFraming":"utun"}), - ); - groups::select(controller, "REJECT"); - groups::select(controller, "peer"); - // Business-proxy traffic must not be included in TUN counters. - probe_socks_tcp( - SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)), - false, - false, - ); - probe_socks_udp( - SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)), - false, - false, - ); - let before = traffic(controller); - assert_eq!(before, json!({"up":0,"down":0,"upTotal":0,"downTotal":0})); - let origin = UdpSocket::bind(origin_address(false)).unwrap(); - origin.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - origin.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let target = origin.local_addr().unwrap(); - peer.send(&packet(target, 17, (0, 0, 0), b"tun-udp-forward")) - .unwrap(); - let mut bytes = [0; 128]; - let (length, source) = origin.recv_from(&mut bytes).unwrap(); - assert_eq!(&bytes[..length], b"tun-udp-forward"); - origin.send_to(b"tun-udp-reply", source).unwrap(); - let response = receive(&peer, 17); - assert_eq!(&response[12..16], origin_ip.octets()); - assert_eq!(&response[16..20], [192, 0, 2, 10]); - assert_eq!(&response[20..22], target.port().to_be_bytes()); - assert_eq!(&response[22..24], 12000_u16.to_be_bytes()); - assert_eq!(&response[28..], b"tun-udp-reply"); - let listener = TcpListener::bind(origin_address(false)).unwrap(); - listener.set_nonblocking(true).unwrap(); - let target = listener.local_addr().unwrap(); - peer.send(&packet(target, 6, (1, 0, 2), &[])).unwrap(); - let syn_ack = receive(&peer, 6); - assert_eq!(syn_ack[33] & 0x12, 0x12); - let server_seq = u32::from_be_bytes(syn_ack[24..28].try_into().unwrap()).wrapping_add(1); - peer.send(&packet( - target, - 6, - (2, server_seq, 0x18), - b"tun-tcp-forward", - )) - .unwrap(); - let mut remote = accept_until(&listener); - remote.read_exact(&mut bytes[..15]).unwrap(); - assert_eq!(&bytes[..15], b"tun-tcp-forward"); - remote.write_all(b"tun-tcp-reply").unwrap(); - let deadline = Instant::now() + IO_TIMEOUT; - let mut received = Vec::new(); - while received.len() < 13 { - assert!(Instant::now() < deadline, "TUN TCP payload deadline"); - let response = receive(&peer, 6); - assert_eq!(&response[12..16], origin_ip.octets()); - assert_eq!(&response[16..20], [192, 0, 2, 10]); - assert_eq!(&response[20..22], target.port().to_be_bytes()); - let offset = 20 + usize::from(response[32] >> 4) * 4; - received.extend_from_slice(&response[offset..]); - } - assert_eq!(received, b"tun-tcp-reply"); - let totals = traffic(controller); - assert!(totals["upTotal"].as_u64().unwrap() > 0); - assert!(totals["downTotal"].as_u64().unwrap() > 0); - core.stop(); - assert_eq!(remote.read(&mut [0; 1]).unwrap(), 0); - // Caller-owned fd survives Stop and still has its original flags. - assert!(unsafe { libc::fcntl(host.as_raw_fd(), libc::F_GETFL) } & libc::O_NONBLOCK != 0); - drop((host, peer, origin, listener, remote)); - drop(TcpListener::bind(controller).unwrap()); - drop(TcpListener::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - drop(UdpSocket::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - ss_lifecycle::assert_fd_returned(baseline); - println!( - "PASS I01/I04/I05 synthetic utun fd -> {} TCP/UDP; proxy traffic excluded, fd ownership and Stop", - fixture["type"].as_str().unwrap() - ); - } - combinations::close_peer_connections(fixtures); -} diff --git a/tests/mihomo_interop.rs b/tests/mihomo_interop.rs deleted file mode 100644 index ea526d3..0000000 --- a/tests/mihomo_interop.rs +++ /dev/null @@ -1,747 +0,0 @@ -//! Actual process interop through public YAML + Invoke, not a test-only dispatcher. -//! Compile-only legacy fixture pending full container migration; see tests/README.md. -#![cfg(all( - feature = "ffi", - feature = "inbound-http", - feature = "inbound-socks5", - feature = "outbound-socks5" -))] - -use serde_json::{Value, json}; -use std::{ - env, - ffi::{CStr, CString}, - io::{self, BufRead, BufReader, Read, Write}, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, TcpStream}, - thread::{self, JoinHandle}, - time::{Duration, Instant}, -}; - -const IO_TIMEOUT: Duration = Duration::from_secs(5); -const BASIC: &str = "Basic Zml4dHVyZTpwYXNzd29yZA=="; - -fn peer_ip(index: usize) -> Ipv4Addr { - env::var("VCORE_MIHOMO_PEER_HOSTS").map_or(Ipv4Addr::LOCALHOST, |value| { - let hosts: Vec = serde_json::from_str(&value).unwrap(); - hosts[index] - }) -} - -fn origin_address(ipv6: bool) -> SocketAddr { - let variable = if ipv6 { - "VCORE_MIHOMO_ORIGIN_V6" - } else { - "VCORE_MIHOMO_ORIGIN_V4" - }; - let fallback = if ipv6 { - IpAddr::V6(Ipv6Addr::LOCALHOST) - } else { - IpAddr::V4(Ipv4Addr::LOCALHOST) - }; - SocketAddr::new( - env::var(variable).map_or(fallback, |value| value.parse().unwrap()), - 0, - ) -} - -#[path = "mihomo/combinations.rs"] -mod combinations; -#[path = "mihomo/eih.rs"] -mod eih; -#[path = "mihomo/groups.rs"] -mod groups; -#[path = "mihomo/host.rs"] -mod host; -#[path = "mihomo/soak.rs"] -mod soak; -#[path = "mihomo/sockets.rs"] -mod sockets; -#[path = "mihomo/ss_lifecycle.rs"] -mod ss_lifecycle; -#[cfg(feature = "outbound-trojan")] -#[path = "mihomo/trojan.rs"] -mod trojan; -use sockets::GuardedUdpSocket as UdpSocket; -#[cfg(any(target_os = "macos", target_os = "ios"))] -#[path = "mihomo/tun.rs"] -mod tun; - -fn accept_until(listener: &TcpListener) -> TcpStream { - let deadline = Instant::now() + IO_TIMEOUT; - loop { - match listener.accept() { - Ok((stream, _)) => return buffered(stream).into_inner(), - Err(error) - if error.kind() == io::ErrorKind::WouldBlock && Instant::now() < deadline => - { - thread::sleep(Duration::from_millis(5)); - } - Err(error) => panic!("fixture accept: {error}"), - } - } -} - -fn invoke(method: &str, instance: Option<&str>, payload: Value) -> Value { - let result = invoke_response(method, instance, payload); - assert_eq!( - result["success"], true, - "Invoke {method}: {}", - result["error"] - ); - result["data"].clone() -} - -fn invoke_response(method: &str, instance: Option<&str>, payload: Value) -> Value { - let mut message = - json!({"apiVersion": vcore::INVOKE_API_VERSION, "method": method, "payload": payload}); - if let Some(instance) = instance { - message["instanceId"] = json!(instance); - } - let input = CString::new(message.to_string()).unwrap(); - // SAFETY: the input remains a live NUL-terminated allocation for this call. - let output = unsafe { vcore::ffi::VCoreInvoke(input.as_ptr()) }; - assert!(!output.is_null()); - // SAFETY: Invoke returns a live NUL-terminated allocation, freed once below. - let bytes = unsafe { CStr::from_ptr(output) }.to_bytes().to_vec(); - // SAFETY: output came from Invoke and has not yet been freed. - unsafe { vcore::ffi::VCoreFree(output) }; - serde_json::from_slice(&bytes).unwrap() -} - -struct Core(Option); - -impl Core { - fn start(yaml: &str) -> Self { - Self::start_with(yaml, json!({})) - } - fn start_with(yaml: &str, payload: Value) -> Self { - invoke("validateConfig", None, json!({"configYaml": yaml})); - let id = invoke("createInstance", None, json!({}))["instanceId"] - .as_str() - .unwrap() - .to_owned(); - let owner = Self(Some(id)); - invoke("prepare", owner.0.as_deref(), json!({"configYaml": yaml})); - invoke("start", owner.0.as_deref(), payload); - owner - } - fn stop(mut self) { - let id = self.0.take().unwrap(); - cleanup(id); - } -} - -impl Drop for Core { - fn drop(&mut self) { - if let Some(id) = self.0.take() { - // Exercise the same cleanup on failed assertions without double panic. - let _ = std::panic::catch_unwind(|| { - cleanup(id); - }); - } - } -} - -fn cleanup(id: String) { - let (sender, receiver) = std::sync::mpsc::sync_channel(1); - let worker = thread::spawn(move || { - let result = std::panic::catch_unwind(|| { - invoke("stop", Some(&id), json!({})); - invoke("destroyInstance", Some(&id), json!({})); - }); - let _ = sender.send(result); - }); - let result = receiver - .recv_timeout(IO_TIMEOUT) - .expect("Invoke cleanup exceeded 5-second watchdog (FAIL)"); - worker.join().unwrap(); - if let Err(panic) = result { - std::panic::resume_unwind(panic); - } -} - -#[derive(Clone, Copy, Debug)] -enum Mode { - Forward, - Chunked, - Connect, - Upgrade, -} - -struct Origin { - address: SocketAddr, - task: Option>, -} - -impl Origin { - fn start(mode: Mode) -> Self { - let listener = TcpListener::bind(origin_address(false)).unwrap(); - let address = listener.local_addr().unwrap(); - listener.set_nonblocking(true).unwrap(); - let task = thread::spawn(move || { - let deadline = Instant::now() + IO_TIMEOUT; - let stream = loop { - match listener.accept() { - Ok((stream, _)) => break stream, - Err(error) - if error.kind() == io::ErrorKind::WouldBlock - && Instant::now() < deadline => - { - thread::sleep(Duration::from_millis(5)) - } - Err(error) => panic!("origin accept: {error}"), - } - }; - let mut peer = buffered(stream); - let request = head(&mut peer); - assert!(!request.to_ascii_lowercase().contains("proxy-authorization")); - assert!(!request.to_ascii_lowercase().contains("x-vcore-")); - assert!(request.starts_with(if matches!(mode, Mode::Chunked) { - "POST /probe " - } else { - "GET /probe " - })); - if matches!(mode, Mode::Upgrade) { - peer.get_mut().write_all(b"HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\nhello").unwrap(); - exact(&mut peer, b"echo"); - peer.get_mut().write_all(b"echo").unwrap(); - } else if matches!(mode, Mode::Chunked) { - // Independent framing oracle accepts a client's legal chunk sizes. - let mut total = Vec::new(); - loop { - let mut line = String::new(); - peer.read_line(&mut line).unwrap(); - let size = - usize::from_str_radix(line.trim().split(';').next().unwrap(), 16).unwrap(); - if size == 0 { - loop { - line.clear(); - peer.read_line(&mut line).unwrap(); - if line == "\r\n" { - break; - } - } - break; - } - assert!(size <= 1024); - let mut body = vec![0; size]; - peer.read_exact(&mut body).unwrap(); - total.extend_from_slice(&body); - exact(&mut peer, b"\r\n"); - } - assert_eq!(total, b"ping"); - peer.get_mut() - .write_all( - b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\npong", - ) - .unwrap(); - } else { - peer.get_mut() - .write_all( - b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\npong", - ) - .unwrap(); - } - }); - Self { - address, - task: Some(task), - } - } - fn finish(mut self) { - self.task.take().unwrap().join().unwrap(); - } -} - -impl Drop for Origin { - fn drop(&mut self) { - if let Some(task) = self.task.take() { - let _ = task.join(); - } - } -} - -fn buffered(stream: TcpStream) -> BufReader { - // Darwin can inherit O_NONBLOCK from the listening socket; this fixture uses - // blocking I/O with finite OS timeouts after the bounded accept loop. - stream.set_nonblocking(false).unwrap(); - stream.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - stream.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - BufReader::new(stream) -} - -fn head(peer: &mut BufReader) -> String { - let mut result = String::new(); - loop { - let mut line = String::new(); - assert!( - peer.read_line(&mut line).unwrap() > 0, - "truncated response: {result}" - ); - result.push_str(&line); - assert!(result.len() <= 32768); - if line == "\r\n" { - return result; - } - } -} - -fn exact(peer: &mut BufReader, expected: &[u8]) { - let mut bytes = vec![0; expected.len()]; - peer.read_exact(&mut bytes).unwrap(); - assert_eq!(bytes, expected); -} - -fn probe(proxy: SocketAddr, authenticated: bool, mode: Mode) { - let origin = Origin::start(mode); - let target = origin.address; - let mut peer = buffered(TcpStream::connect_timeout(&proxy, IO_TIMEOUT).unwrap()); - let auth = if authenticated { - format!("Proxy-Authorization: {BASIC}\r\n") - } else { - String::new() - }; - if matches!(mode, Mode::Connect) { - // The inner request is sent before the CONNECT response to exercise read-ahead. - peer.get_mut().write_all(format!("CONNECT {target} HTTP/1.1\r\nHost: {target}\r\n{auth}\r\nGET /probe HTTP/1.1\r\nHost: {target}\r\nConnection: close\r\n\r\n").as_bytes()).unwrap(); - assert!(head(&mut peer).starts_with("HTTP/1.1 200")); - } else { - let (method, fields, body) = match mode { - Mode::Chunked => ( - "POST", - "Transfer-Encoding: chunked\r\nConnection: close\r\n", - "4\r\nping\r\n0\r\n\r\n", - ), - Mode::Upgrade => ("GET", "Connection: Upgrade\r\nUpgrade: websocket\r\n", ""), - _ => ("GET", "Connection: close\r\n", ""), - }; - peer.get_mut().write_all(format!("{method} http://{target}/probe HTTP/1.1\r\nHost: {target}\r\n{auth}{fields}\r\n{body}").as_bytes()).unwrap(); - } - if matches!(mode, Mode::Upgrade) { - assert!(head(&mut peer).starts_with("HTTP/1.1 101")); - exact(&mut peer, b"hello"); - peer.get_mut().write_all(b"echo").unwrap(); - exact(&mut peer, b"echo"); - } else { - assert!(head(&mut peer).starts_with("HTTP/1.1 200")); - exact(&mut peer, b"pong"); - } - drop(peer); - origin.finish(); -} - -fn port(variable: &str) -> u16 { - env::var(variable) - .expect("legacy fixture; see tests/README.md") - .parse() - .unwrap() -} - -#[test] -#[ignore = "requires managed mihomo processes; legacy fixture; see tests/README.md"] -fn public_client_inbounds_interoperate_with_mihomo_in_both_directions() { - #[cfg(feature = "interop-test")] - let mut _case = vcore::resources::case_events::Case::new( - "FOUNDATIONS-REGRESSION", - "public_client_inbounds_interoperate_with_mihomo_in_both_directions", - ); - let upstream = port("VCORE_MIHOMO_UPSTREAM"); - let upstream_host = peer_ip(0); - let downstream = port("VCORE_MIHOMO_DOWNSTREAM"); - let core_port = port("VCORE_MIHOMO_HTTP_PORT"); - let directory = tempfile::tempdir().unwrap(); - invoke("initialize", None, json!({"dataDir": directory.path()})); - for via_upstream in [true, false] { - let route = if via_upstream { "mihomo" } else { "local" }; - let yaml = format!( - "port: {core_port}\nipv6: false\nauthentication: [fixture:password]\nproxies:\n - {{name: mihomo, type: socks5, server: {upstream_host}, port: {upstream}, username: fixture, password: password}}\nproxy-groups:\n - {{name: local, type: select, proxies: [DIRECT]}}\nrules: ['MATCH,{route}']\n" - ); - let core = Core::start(&yaml); - let proxy = SocketAddr::from(( - Ipv4Addr::LOCALHOST, - if via_upstream { core_port } else { downstream }, - )); - for mode in [Mode::Forward, Mode::Chunked, Mode::Connect, Mode::Upgrade] { - probe(proxy, via_upstream, mode); - println!( - "PASS {mode:?}: {}", - if via_upstream { - "VCore HTTP -> VCore SOCKS5 -> mihomo -> origin" - } else { - "mihomo HTTP -> mihomo HTTP outbound -> VCore CONNECT -> origin" - } - ); - } - core.stop(); - drop(TcpListener::bind((Ipv4Addr::LOCALHOST, core_port)).unwrap()); - } - let socks_port = port("VCORE_MIHOMO_SOCKS_PORT"); - let downstream = port("VCORE_MIHOMO_SOCKS_DOWNSTREAM"); - for via_upstream in [true, false] { - let route = if via_upstream { "mihomo" } else { "local" }; - let yaml = format!( - "socks-port: {socks_port}\nipv6: true\nauthentication: [fixture:password]\nproxies:\n - {{name: mihomo, type: socks5, server: {upstream_host}, port: {upstream}, username: fixture, password: password, udp: true}}\nproxy-groups:\n - {{name: local, type: select, proxies: [DIRECT]}}\nrules: ['MATCH,{route}']\n" - ); - let core = Core::start(&yaml); - let proxy = SocketAddr::from(( - Ipv4Addr::LOCALHOST, - if via_upstream { socks_port } else { downstream }, - )); - for ipv6 in [false, true] { - probe_socks_tcp(proxy, via_upstream, ipv6); - probe_socks_udp(proxy, via_upstream, ipv6); - println!( - "PASS SOCKS5 TCP + UDP (IPv6={ipv6}): {}", - if via_upstream { - "VCore SOCKS5 -> mihomo -> origin" - } else { - "mihomo SOCKS5 -> VCore SOCKS5 -> origin" - } - ); - } - core.stop(); - drop(TcpListener::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - drop(UdpSocket::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - } - probe_anytls(socks_port); - probe_shadowsocks(socks_port); - if let Ok(fixtures) = env::var("VCORE_MIHOMO_CHAIN_FIXTURES") { - let fixtures = serde_json::from_str(&fixtures).unwrap(); - combinations::probe(core_port, socks_port, &fixtures); - groups::probe(core_port, socks_port, &fixtures); - host::probe(core_port, socks_port, &fixtures); - #[cfg(any(target_os = "macos", target_os = "ios"))] - tun::probe(core_port, socks_port, &fixtures); - for round in 0..25 { - for node in fixtures["last"].as_array().unwrap() { - combinations::close_peer_connections(&fixtures); - ss_lifecycle::probe(socks_port, std::slice::from_ref(node)); - } - println!( - "PASS lifecycle round {round}: four protocols, active Stop + failed-start + measurement" - ); - } - combinations::close_peer_connections(&fixtures); - #[cfg(target_os = "macos")] - soak::recovery_probe(core_port, socks_port, &fixtures); - let seconds = env::var("VCORE_MIHOMO_SOAK_SECONDS") - .unwrap() - .parse() - .unwrap(); - if seconds > 0 { - soak::probe(core_port, socks_port, &fixtures, seconds); - } - } -} - -fn probe_shadowsocks(socks_port: u16) { - let fixtures: Vec = - serde_json::from_str(&env::var("VCORE_MIHOMO_SS_FIXTURES").unwrap()).unwrap(); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - for fixture in &fixtures { - let mut node = fixture.clone(); - node["name"] = json!("peer"); - node["type"] = json!("ss"); - node["server"] = json!(peer_ip(3)); - node["udp"] = json!(true); - let config = json!({"socks-port": socks_port, "proxies": [node], "rules": ["MATCH,peer"]}); - let core = Core::start(&config.to_string()); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - probe_socks_tcp_target(proxy, false, ipv6, domain, false); - println!( - "PASS SS {} TCP: IPv6={ipv6}, domain={domain}", - fixture["cipher"].as_str().unwrap() - ); - probe_socks_udp_target(proxy, false, ipv6, domain); - println!( - "PASS SS {} UDP: IPv6={ipv6}, domain={domain}", - fixture["cipher"].as_str().unwrap() - ); - } - probe_socks_tcp_target(proxy, false, false, false, true); - core.stop(); - drop(TcpListener::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - drop(UdpSocket::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - println!( - "PASS SS {}: TCP + UDP, IPv4 + IPv6 + domain, server-first", - fixture["cipher"].as_str().unwrap() - ); - } - let upstream = port("VCORE_MIHOMO_UPSTREAM"); - for nested in [false, true] { - for direct in [false, true] { - if !nested && direct { - continue; // DIRECT is exercised as a group leaf, not a duplicate hop. - } - let mut node = fixtures[0].clone(); - node["name"] = json!("peer"); - node["type"] = json!("ss"); - node["server"] = json!(if direct { - peer_ip(3).to_string() - } else { - "vcore-peer.test".to_owned() - }); - node["udp"] = json!(true); - node["dialer-proxy"] = json!(if nested { "outer" } else { "hop" }); - let hop = json!({"name":"hop", "type":"socks5", "server":peer_ip(0), "port":upstream, "username":"fixture", "password":"password", "udp":true}); - let leaf = if direct { "DIRECT" } else { "hop" }; - let config = json!({ - "socks-port":socks_port, "proxies":[node, hop], - "proxy-groups":[{"name":"outer", "type":"select", "proxies":["inner"]}, {"name":"inner", "type":"select", "proxies":[leaf]}], - "rules":["MATCH,peer"] - }); - let core = Core::start(&config.to_string()); - probe_socks_tcp(proxy, false, false); - probe_socks_udp(proxy, false, false); - core.stop(); - println!( - "PASS SS chained TCP + UDP: nested={nested}, direct={}", - nested && direct - ); - } - } - eih::probe(socks_port, &fixtures); - ss_lifecycle::probe(socks_port, &fixtures); -} - -fn probe_anytls(socks_port: u16) { - let peer_port = port("VCORE_MIHOMO_ANYTLS_PORT"); - let peer_host = peer_ip(0); - let fingerprint = env::var("VCORE_MIHOMO_ANYTLS_PIN").unwrap(); - for fields in [ - "skip-cert-verify: true".to_owned(), - format!("fingerprint: '{fingerprint}'"), - format!("fingerprint: '{fingerprint}', skip-cert-verify: true, alpn: [h2, http/1.1]"), - ] { - let node = format!( - "{{name: peer, type: anytls, server: {peer_host}, port: {peer_port}, password: password, sni: fixture.invalid, udp: true, {fields}}}" - ); - let yaml = format!( - "socks-port: {socks_port}\nauthentication: [fixture:password]\nproxies: [{node}]\nrules: ['MATCH,peer']\n" - ); - let core = Core::start(&yaml); - let proxy = SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)); - for ipv6 in [false, true] { - probe_socks_tcp(proxy, true, ipv6); - probe_socks_udp(proxy, true, ipv6); - } - core.stop(); - drop(TcpListener::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - drop(UdpSocket::bind((Ipv4Addr::LOCALHOST, socks_port)).unwrap()); - println!("PASS AnyTLS public config: TCP + UoT v2, IPv4 + IPv6"); - // Measurement constructs a private graph through the same normalized - // fields and must finish its pool/reader tasks before returning. - let listener = TcpListener::bind(origin_address(false)).unwrap(); - let address = listener.local_addr().unwrap(); - listener.set_nonblocking(true).unwrap(); - let task = thread::spawn(move || { - let deadline = Instant::now() + IO_TIMEOUT; - let stream = loop { - match listener.accept() { - Ok((stream, _)) => break stream, - Err(error) - if error.kind() == io::ErrorKind::WouldBlock - && Instant::now() < deadline => - { - thread::sleep(Duration::from_millis(5)) - } - Err(error) => panic!("measurement origin accept: {error}"), - } - }; - let mut stream = buffered(stream); - assert!(head(&mut stream).starts_with("HEAD /")); - stream - .get_mut() - .write_all(b"HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n") - .unwrap(); - }); - let data = invoke( - "measureDelay", - None, - json!({"configYamls": [format!("proxies: [{node}]")], "timeout": 5, "url": format!("http://{address}/")}), - ); - task.join().unwrap(); - assert_eq!(data["results"][0]["success"], true); - } - for fields in [ - "".to_owned(), - format!( - ", fingerprint: '{}', skip-cert-verify: true", - "00".repeat(32) - ), - ] { - let yaml = format!( - "socks-port: {socks_port}\nproxies: [{{name: peer, type: anytls, server: {peer_host}, port: {peer_port}, password: password, sni: fixture.invalid{fields}}}]\nrules: ['MATCH,peer']\n" - ); - let core = Core::start(&yaml); - let mut client = socks_login(SocketAddr::from((Ipv4Addr::LOCALHOST, socks_port)), false); - client.write_all(&[5, 1, 0, 1, 127, 0, 0, 1, 0, 9]).unwrap(); - let mut reply = [0; 10]; - client.read_exact(&mut reply).unwrap(); - assert_ne!(reply[1], 0, "untrusted or wrong pin must not CONNECT"); - core.stop(); - } - println!( - "PASS AnyTLS public config rejects untrusted certificate and incorrect pin even with skip-cert-verify" - ); -} - -fn socks_login(proxy: SocketAddr, authenticated: bool) -> TcpStream { - let mut stream = buffered(TcpStream::connect_timeout(&proxy, IO_TIMEOUT).unwrap()).into_inner(); - let method = if authenticated { 2 } else { 0 }; - stream.write_all(&[5, 1, method]).unwrap(); - let mut reply = [0; 2]; - stream.read_exact(&mut reply).unwrap(); - assert_eq!(reply, [5, method]); - if authenticated { - stream.write_all(b"\x01\x07fixture\x08password").unwrap(); - stream.read_exact(&mut reply).unwrap(); - assert_eq!(reply, [1, 0]); - } - stream -} - -fn socks_address(address: SocketAddr) -> Vec { - let mut bytes = match address.ip() { - std::net::IpAddr::V4(ip) => { - let mut b = vec![1]; - b.extend_from_slice(&ip.octets()); - b - } - std::net::IpAddr::V6(ip) => { - let mut b = vec![4]; - b.extend_from_slice(&ip.octets()); - b - } - }; - bytes.extend_from_slice(&address.port().to_be_bytes()); - bytes -} - -fn socks_reply(stream: &mut TcpStream) -> SocketAddr { - let mut header = [0; 4]; - stream.read_exact(&mut header).unwrap(); - assert_eq!(&header[..3], &[5, 0, 0]); - let ip = match header[3] { - 1 => { - let mut b = [0; 4]; - stream.read_exact(&mut b).unwrap(); - std::net::IpAddr::from(b) - } - 4 => { - let mut b = [0; 16]; - stream.read_exact(&mut b).unwrap(); - std::net::IpAddr::from(b) - } - _ => panic!("expected literal SOCKS5 reply"), - }; - let mut port = [0; 2]; - stream.read_exact(&mut port).unwrap(); - SocketAddr::new(ip, u16::from_be_bytes(port)) -} - -fn probe_socks_tcp(proxy: SocketAddr, authenticated: bool, ipv6: bool) { - probe_socks_tcp_target(proxy, authenticated, ipv6, false, false); -} - -fn fixture_address(target: SocketAddr, domain: bool) -> Vec { - if !domain { - return socks_address(target); - } - let name = b"vcore-fixture.test"; - let mut address = vec![3, name.len() as u8]; - address.extend_from_slice(name); - address.extend_from_slice(&target.port().to_be_bytes()); - address -} - -fn probe_socks_tcp_target( - proxy: SocketAddr, - authenticated: bool, - ipv6: bool, - domain: bool, - server_first: bool, -) { - let listener = TcpListener::bind(origin_address(ipv6)).unwrap(); - let target = listener.local_addr().unwrap(); - let mut client = socks_login(proxy, authenticated); - let mut request = vec![5, 1, 0]; - request.extend_from_slice(&fixture_address(target, domain)); - if !server_first { - request.extend_from_slice(b"early-data"); - } - client.write_all(&request).unwrap(); - socks_reply(&mut client); - // A successful SOCKS reply can precede the ultimate server's accept in - // mihomo; bound the accept independently and always join/drop its owner. - listener.set_nonblocking(true).unwrap(); - let deadline = Instant::now() + IO_TIMEOUT; - let (remote, _) = loop { - match listener.accept() { - Ok(pair) => break pair, - Err(error) - if error.kind() == io::ErrorKind::WouldBlock && Instant::now() < deadline => - { - thread::sleep(Duration::from_millis(5)) - } - Err(error) => panic!("SOCKS origin accept: {error}"), - } - }; - let mut remote = buffered(remote); - if server_first { - remote.get_mut().write_all(b"hello").unwrap(); - let mut greeting = [0; 5]; - client.read_exact(&mut greeting).unwrap(); - assert_eq!(&greeting, b"hello"); - client.write_all(b"early-data").unwrap(); - } - exact(&mut remote, b"early-data"); - remote.get_mut().write_all(b"reply-data").unwrap(); - exact(&mut buffered(client), b"reply-data"); -} - -fn probe_socks_udp(proxy: SocketAddr, authenticated: bool, ipv6: bool) { - probe_socks_udp_target(proxy, authenticated, ipv6, false); -} - -fn probe_socks_udp_target(proxy: SocketAddr, authenticated: bool, ipv6: bool, domain: bool) { - let origin = UdpSocket::bind(origin_address(ipv6)).unwrap(); - origin.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - origin.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let target = origin.local_addr().unwrap(); - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - socket.set_read_timeout(Some(IO_TIMEOUT)).unwrap(); - socket.set_write_timeout(Some(IO_TIMEOUT)).unwrap(); - let mut control = socks_login(proxy, authenticated); - // Port-zero learning exercises ordinary SOCKS clients, including mihomo. - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap(); - let relay = socks_reply(&mut control); - let mut request = vec![0, 0, 0]; - request.extend_from_slice(&fixture_address(target, domain)); - request.extend_from_slice(b"udp-wire"); - socket.send_to(&request, relay).unwrap(); - let mut buffer = [0; 512]; - let (length, peer) = origin - .recv_from(&mut buffer) - .expect("mihomo did not deliver the UDP request to the fixture origin"); - assert_eq!(&buffer[..length], b"udp-wire"); - origin.send_to(&buffer[..length], peer).unwrap(); - let (length, from) = socket.recv_from(&mut buffer).unwrap_or_else(|error| { - panic!( - "fixture UDP response: client={}, relay={relay}, origin={target}, origin_observed_peer={peer}: {error}", - socket.local_addr().unwrap(), - ) - }); - assert_eq!( - from, - relay, - "fixture UDP response: client={}, origin={target}, origin_observed_peer={peer}, length={length}", - socket.local_addr().unwrap(), - ); - let mut expected = vec![0, 0, 0]; - expected.extend_from_slice(&socks_address(target)); - expected.extend_from_slice(b"udp-wire"); - assert_eq!(&buffer[..length], expected); - // The control socket remains owned until all UDP replies are validated. - drop(control); -} diff --git a/tests/protocols/README.md b/tests/protocols/README.md index 5a61943..c8c6433 100644 --- a/tests/protocols/README.md +++ b/tests/protocols/README.md @@ -1,34 +1,25 @@ -# 协议验证输入 +# 核心协议回归输入 -生产字段只以 [config.yaml](../../docs/config.yaml) 和协议契约为准。 -本目录保留真实执行需要的输入,不维护开发阶段规划、候选库实验或重复 JSON 清单。 +生产字段以 [config.yaml](../../docs/config.yaml) 和协议契约为准。本目录只保留 +VCore 纯核心回归直接使用的独立输入,不包含容器 catalog、消费者或测试编排。 -- foundation-cases.json:尚未替代的基础/Trojan 稳定证据标识与断言。旧服务端编排 - 未全容器化,不作为可执行宿主入口;本地基础测试使用 check core。 -- 其他可执行用例由 scripts/src/vcore_scripts 下各协议 catalog/definitions 生成, - protocol_evidence.load_manifest 统一校验。用例 ID 以协议或验证用途为前缀, - 分组使用 tcp、udp、transports、lifecycle 等能力名称,不绑定开发阶段。 -- limits.json:局部资源上限及边界 case;Rust 测试对照真实常量。 -- encryption-crypto.json 与 encryption-vectors:独立官方 Go 原语生成的密码向量, - 被生产模块的确定性测试直接使用。 -- stream_probe.rs:旧基础消费者仍有引用,保留编译;不是四个已删除的独立 spike。 +- `limits.json`:局部资源上限、实际常量引用和边界断言; + `tests/limit_foundations.rs` 直接读取并核对。 +- `encryption-crypto.json`:独立官方 Go 原语生成的 Encryption 密码向量; + `src/outbound/vless/encryption/crypto.rs` 和 `records.rs` 的确定性回归直接使用。 + 不能从待测实现重生成期望;历史生成来源可在 Git 中追溯。 -```sh -uv run --project scripts --locked vcore-scripts check protocol-coverage --catalog-only -uv run --project scripts --locked vcore-scripts check protocol-interop --suite vless --list -uv run --project scripts --locked vcore-scripts check protocol-interop --suite vless -uv run --project scripts --locked vcore-scripts check protocol-coverage --suite vless --run-dir target/interop/runs/ -``` +核心离线回归见 [tests](../README.md),平台编译见 [scripts](../../scripts/README.md)。 +互通消费者、官方对端下载与编排位于公开的 +[container-benchmark](https://github.com/OneXray/container-benchmark),入口为 +`container-benchmark interop --source vcore=PATH`,默认使用 Mihomo、Xray-core、 +Hysteria2、V2Ray 与 Caddy/Xray 的 64 个代表用例;Mihomo listener 的缺口保留原生 +或明确分层验证。`--backend` / `--protocol` 可筛选,`--list` 离线列举。 +原生 TUN 压力与两核性能比较使用该工程的 `stress` / `compare`;真实执行均显式 +提供 VCore checkout,不推断工程路径。网络互通不混入离线检查或平台构建。 +此入口不恢复历史全部字段、代理链、长测或 ClientHello golden 的网络矩阵, +本目录独立输入的离线回归也不能冒充当前网络通过。 -catalog-only 仅 VALID / NOT RUN。运行记录的原始事件、命令/退出、源/锁文件/对端身份、 -hash、所有者清理和资源快照共同决定结果;空、部分、重复、改源、超时、CFG-only 或 -未清理都不能 PASS。保留完整通过与原始失败,不拼接旧报告。case 子集不签收整套。 - -Mihomo 是默认 listener;缺口由官方 Xray、V2Ray、Hysteria、ssserver 补验。 -共享字段按实际整个模式选择对端,XHTTP 两腿须到同一原生会话表;分层网关不是单端 -原生支持。对端最新下载策略与命令见 [scripts](../../scripts/README.md); -所有服务端遵守[隔离规则](../../docs/testing-isolation.md)。 - -integration 保留七协议有序两跳、100 次生命周期、100 轮 40-flow 重建、1800 秒持续 -流量和实际 HY2 跳跃。短测不代替长测,编译不代替物理设备;[验收边界](../../docs/acceptance.md) -单独列出对端限制、参照差异和未完成发布条件。 +输入文件属于受版本控制的测试源,不是每轮产生的测试数据。每轮容器实验仅保留 +脱敏文字结论,清理规则见[测试隔离](../../docs/testing-isolation.md); +行为、互通和设备的证据边界见[验收范围](../../docs/acceptance.md)。 diff --git a/tests/protocols/encryption-vectors/go.mod b/tests/protocols/encryption-vectors/go.mod deleted file mode 100644 index 691dad5..0000000 --- a/tests/protocols/encryption-vectors/go.mod +++ /dev/null @@ -1,10 +0,0 @@ -module vcore-encryption-vectors - -go 1.27.0 - -require ( - github.com/metacubex/blake3 v0.1.0 - golang.org/x/crypto v0.57.0 -) - -require golang.org/x/sys v0.48.0 // indirect diff --git a/tests/protocols/encryption-vectors/go.sum b/tests/protocols/encryption-vectors/go.sum deleted file mode 100644 index e1009d3..0000000 --- a/tests/protocols/encryption-vectors/go.sum +++ /dev/null @@ -1,6 +0,0 @@ -github.com/metacubex/blake3 v0.1.0 h1:KGnjh/56REO7U+cgZA8dnBhxdP7jByrG7hTP+bu6cqY= -github.com/metacubex/blake3 v0.1.0/go.mod h1:CCkLdzFrqf7xmxCdhQFvJsRRV2mwOLDoSPg6vUTB9Uk= -golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= -golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= -golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= diff --git a/tests/protocols/encryption-vectors/main.go b/tests/protocols/encryption-vectors/main.go deleted file mode 100644 index a2d7250..0000000 --- a/tests/protocols/encryption-vectors/main.go +++ /dev/null @@ -1,158 +0,0 @@ -// Independent primitive fixtures. Every key is synthetic test material. -// No network service, protocol decoder, or third-party source modification. -package main - -import ( - "crypto/aes" - "crypto/cipher" - "crypto/ecdh" - "crypto/mlkem" - "encoding/hex" - "encoding/json" - "os" - "runtime" - - "github.com/metacubex/blake3" - "golang.org/x/crypto/chacha20poly1305" -) - -func pattern(n, step, start int) []byte { - b := make([]byte, n) - for i := range b { - b[i] = byte(i*step + start) - } - return b -} - -func check(err error) { - if err != nil { - panic(err) - } -} - -func aeadFor(name string, ctx, material []byte) cipher.AEAD { - key := make([]byte, 32) - blake3.DeriveKey(key, string(ctx), material) - if name == "aes256gcm" { - block, err := aes.NewCipher(key) - check(err) - aead, err := cipher.NewGCM(block) - check(err) - return aead - } - aead, err := chacha20poly1305.New(key) - check(err) - return aead -} - -func main() { - seed := pattern(64, 7, 1) - kem, err := mlkem.NewDecapsulationKey768(seed) - check(err) - shared, ciphertext := kem.EncapsulationKey().Encapsulate() - // Passing a previous JSON reproduces its random encapsulation exactly. - if len(os.Args) == 2 { - data, err := os.ReadFile(os.Args[1]) - check(err) - var old map[string]json.RawMessage - check(json.Unmarshal(data, &old)) - var value string - check(json.Unmarshal(old["mlkem_ciphertext"], &value)) - ciphertext, err = hex.DecodeString(value) - check(err) - shared, err = kem.Decapsulate(ciphertext) - check(err) - } - secret := pattern(32, 3, 5) - x, err := ecdh.X25519().NewPrivateKey(secret) - check(err) - peer, err := ecdh.X25519().NewPrivateKey(pattern(32, 11, 9)) - check(err) - xShared, err := x.ECDH(peer.PublicKey()) - check(err) - context, material := pattern(16, 37, 255), pattern(96, 13, 7) - derived := make([]byte, 32) - blake3.DeriveKey(derived, string(context), material) - block, err := aes.NewCipher(derived) - check(err) - gcm, err := cipher.NewGCM(block) - check(err) - chacha, err := chacha20poly1305.New(derived) - check(err) - plain := pattern(37, 17, 3) - aad := []byte{23, 3, 3, 0, byte(len(plain) + 16)} - var records []map[string]string - for _, item := range []struct { - name string - aead cipher.AEAD - }{{"aes256gcm", gcm}, {"chacha20poly1305", chacha}} { - for _, last := range []byte{1, 2, 255} { - nonce := make([]byte, 12) - if last == 255 { - for i := range nonce { - nonce[i] = 255 - } - } else { - nonce[11] = last - } - records = append(records, map[string]string{ - "cipher": item.name, "context": hex.EncodeToString(context), "key": hex.EncodeToString(material), - "nonce": hex.EncodeToString(nonce), "aad": hex.EncodeToString(aad), "plaintext": hex.EncodeToString(plain), - "ciphertext": hex.EncodeToString(item.aead.Seal(nil, nonce, plain, aad)), - }) - } - } - ctrKey := make([]byte, 32) - blake3.DeriveKey(ctrKey, "VLESS", material) - ctrBlock, err := aes.NewCipher(ctrKey) - check(err) - ctrOut := make([]byte, len(plain)) - cipher.NewCTR(ctrBlock, context).XORKeyStream(ctrOut, plain) - var wrapped []map[string]string - for _, name := range []string{"aes256gcm", "chacha20poly1305"} { - aead := aeadFor(name, context, material) - var wire []byte - for index := range 3 { - nonce := make([]byte, 12) - if index == 0 { - for i := range nonce { - nonce[i] = 255 - } - } else if index == 2 { - nonce[11] = 1 - } - frame := append(append([]byte{}, aad...), aead.Seal(nil, nonce, plain, aad)...) - wire = append(wire, frame...) - if index == 1 { - aead = aeadFor(name, frame, material) - } - } - wrapped = append(wrapped, map[string]string{ - "cipher": name, "context": hex.EncodeToString(context), "key": hex.EncodeToString(material), - "plaintext": hex.EncodeToString(append(append(append([]byte{}, plain...), plain...), plain...)), - "wire": hex.EncodeToString(wire), - }) - } - var headersPlain, headersCipher []byte - headerCTR := cipher.NewCTR(ctrBlock, context) - for range 3 { - // An application-data record with an independently generated AEAD body. - body := gcm.Seal(nil, make([]byte, 12), plain, aad) - header := append([]byte{}, aad...) - headersPlain = append(append(headersPlain, header...), body...) - headerCTR.XORKeyStream(header, header) - headersCipher = append(append(headersCipher, header...), body...) - } - encoder := json.NewEncoder(os.Stdout) - encoder.SetIndent("", " ") - check(encoder.Encode(map[string]any{ - "generator": "Go official crypto + metacubex/blake3; synthetic keys only", "go_version": runtime.Version(), - "mlkem_seed": hex.EncodeToString(seed), "mlkem_public": hex.EncodeToString(kem.EncapsulationKey().Bytes()), - "mlkem_ciphertext": hex.EncodeToString(ciphertext), "mlkem_shared": hex.EncodeToString(shared), - "x25519_private": hex.EncodeToString(secret), "x25519_public": hex.EncodeToString(x.PublicKey().Bytes()), - "x25519_peer": hex.EncodeToString(peer.PublicKey().Bytes()), "x25519_shared": hex.EncodeToString(xShared), - "ctr_context": hex.EncodeToString(context), "ctr_key": hex.EncodeToString(material), "ctr_plaintext": hex.EncodeToString(plain), "ctr_ciphertext": hex.EncodeToString(ctrOut), - "records": records, "wrap_records": wrapped, - "headers_plaintext": hex.EncodeToString(headersPlain), "headers_ciphertext": hex.EncodeToString(headersCipher), - })) -} diff --git a/tests/protocols/foundation-cases.json b/tests/protocols/foundation-cases.json deleted file mode 100644 index 1733fec..0000000 --- a/tests/protocols/foundation-cases.json +++ /dev/null @@ -1,3210 +0,0 @@ -{ - "schema_version": 1, - "kind": "executable-case-manifest", - "scope": "Frozen FOUNDATIONS foundations, TROJAN-SECURITY consumer requirements and INTEGRATION integration. Declarations contain no run results; each stage requires its own complete evidence.", - "cases": [ - { - "case_id": "FOUNDATIONS-SCHEMA", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.schema", - "required": true, - "row_ids": [ - "C01" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "feature_foundations" - }, - "gap_source": null, - "expected_observation": [ - "feature_skeletons_do_not_open_unimplemented_yaml_or_measurement_protocols", - "future_fields_and_over_limit_documents_remain_rejected", - "measurement_rejects_future_protocols_and_oversized_documents" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-LIMITS", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.schema", - "required": true, - "row_ids": [], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "limit_foundations" - }, - "gap_source": null, - "expected_observation": [ - "registry_matches_live_production_constants_and_has_owned_boundary_cases" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-STREAM", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "W01", - "W02", - "W03", - "W04", - "W05", - "G01", - "H01", - "H02", - "H03", - "H04", - "H05", - "H06" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "stream_foundations" - }, - "gap_source": null, - "expected_observation": [ - "transport_options_reject_ambiguous_headers_before_io", - "http_first_header_preserves_prefix_raw_continuation_and_half_close_tail", - "legacy_h2_keeps_unframed_bytes_server_first_and_owned_whole_close", - "websocket_rejects_invalid_upgrade_responses_and_bounded_header_overflows", - "setup_deadline_releases_supplied_io_in_stream_adapters", - "cancelled_setup_releases_supplied_io_without_a_detached_task", - "ws_rejects_oversized_and_truncated_frames_instead_of_reporting_eof", - "ws_accepts_mihomo_clean_underlay_eof_only_at_complete_message_boundaries", - "dropping_an_established_ws_releases_its_only_io_owner", - "grpc_rejects_oversized_truncated_and_invalid_records", - "grpc_large_writes_obey_small_http2_windows_and_keep_byte_integrity", - "ws_partial_writes_empty_frames_ping_and_half_close_preserve_every_byte", - "grpc_handles_response_after_upload_fragmented_records_and_owned_stop", - "websocket_supplied_io_preserves_server_first_and_partial_writes", - "websocket_early_data_and_remaining_frames_keep_the_original_byte_order" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-DATAGRAM", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.datagrams", - "required": true, - "row_ids": [ - "C05", - "C06", - "HY06" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "datagram_foundations" - }, - "gap_source": null, - "expected_observation": [ - "shadowsocks_budget_accounts_for_cipher_headers_and_identity_chain", - "socks5_budget_retains_distinct_payload_and_envelope_limits", - "direct_budget_rejects_large_sends_and_drops_oversize_responses", - "layered_directional_budgets_account_for_ip_headers_and_exact_minima" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-QUIC", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.datagrams", - "required": true, - "row_ids": [ - "C06", - "HY06" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "quic_datagram_foundations" - }, - "gap_source": null, - "expected_observation": [ - "quic_minimum_budget_is_checked_before_io_and_exact_minimum_connects", - "pending_send_is_not_restarted_and_stop_cancels_without_waiting_for_writable", - "controlled_quic_datagram_seam_roundtrips_and_stop_closes_owned_io", - "mapped_peer_sends_to_one_physical_port_and_reports_the_logical_peer", - "incoming_queue_backpressures_without_discarding_a_controlled_burst", - "controlled_direct_datagrams_roundtrip_and_stop" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-RESOLUTION", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.datagrams", - "required": true, - "row_ids": [ - "C03", - "C06" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "resolution_foundations" - }, - "gap_source": null, - "expected_observation": [ - "resolver_dependency_depth_is_bounded_independently_of_concurrent_lookups", - "measurement_domain_resolution_reaches_both_final_and_upstream_ip_boundaries", - "runtime_uses_configured_dns_and_weak_binding_releases_owner", - "recursive_dns_dependency_fails_before_connecting_or_joining_its_own_flight", - "lookup_deadline_and_stop_cancel_pending_resolver_without_retaining_waiters", - "standalone_measurement_resolves_only_at_ip_boundary_with_address_policy_and_stop", - "unbound_and_disabled_runtime_resolution_never_fall_back_to_system_dns" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-RESOURCES", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.schema", - "required": true, - "row_ids": [], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "resource_foundations" - }, - "gap_source": null, - "expected_observation": [ - "twenty_quic_lifetimes_return_owned_resources_to_zero_then_remain_quiet", - "twenty_stream_lifetimes_and_cancelled_setups_remain_quiet", - "physical_tcp_guard_survives_connect_and_releases_on_stream_drop" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "resources" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-SECURITY", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.security", - "required": true, - "row_ids": [ - "T01", - "T02", - "T03", - "T04", - "T05" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "security::tls::tests" - }, - "gap_source": null, - "expected_observation": [ - "webpki_rejects_untrusted_wrong_name_and_expired_unless_explicitly_skipped", - "leaf_pin_is_trust_but_nonleaf_pin_checks_chain_name_and_expiry", - "ticket_storage_obeys_exact_node_budget_and_consumes_each_ticket_once", - "every_certificate_policy_verifies_tls12_and_tls13_handshake_signatures", - "vless_keeps_webpki_tls13_and_required_h2_after_anytls_connections", - "alpn_and_tls_resumption_are_isolated_between_node_policies", - "explicit_verification_name_does_not_change_sni_or_allow_skip_to_override_it", - "mutual_tls_identity_is_required_verified_and_not_shared_between_clients", - "tls_options_reject_invalid_alpn_identity_and_budget_before_using_a_stream", - "certificate_rejection_delivers_no_business_bytes_and_diagnostics_are_redacted", - "tls_close_write_sends_notify_without_closing_the_supplied_transport", - "tls_close_notify_flush_has_a_five_second_bound", - "cancelling_tls_handshake_releases_the_caller_supplied_stream" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-XUDP", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "VM06", - "VL04" - ], - "protocol": "foundation", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "unit", - "peer_config": { - "selector": "xudp::tests" - }, - "gap_source": null, - "expected_observation": [ - "shared_xudp_receives_frames_without_a_vless_response_header", - "first_frame_matches_xray_mux_wire_format", - "followup_frame_carries_each_datagrams_destination", - "destination_codec_round_trips_all_address_families", - "normal_and_error_end_frames_are_distinguishable", - "oversized_response_is_rejected_before_payload_read", - "fragmented_receive_survives_cancellation_and_resumes", - "keepalive_data_is_consumed_without_becoming_an_udp_response", - "full_wire_payload_remains_available_to_proxy_inbounds" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "rust", - "timeout_seconds": 300 - }, - { - "case_id": "FOUNDATIONS-REGRESSION", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.regression", - "required": true, - "row_ids": [], - "protocol": "legacy", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "M", - "peer_config": { - "selector": "mihomo_interop" - }, - "gap_source": null, - "expected_observation": [ - "public_client_inbounds_interoperate_with_mihomo_in_both_directions" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "rust-toolchain", - "M", - "apple-container" - ], - "runner": "mihomo-legacy", - "timeout_seconds": 900 - }, - { - "case_id": "FOUNDATIONS-M-WS", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "W01", - "W02", - "W03" - ], - "protocol": "foundation", - "network": "ws", - "security": "plain", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "ws" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "M", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "ws" - }, - "gap_source": null, - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "M", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-M-WSS", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "W01", - "W02", - "W03" - ], - "protocol": "foundation", - "network": "wss", - "security": "tls-pin", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "wss" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "M", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "wss" - }, - "gap_source": null, - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "M", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-M-GRPC", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "G01" - ], - "protocol": "foundation", - "network": "grpc", - "security": "plain", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "grpc" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "M", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "grpc" - }, - "gap_source": null, - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "M", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-M-GRPC-TLS", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "G01" - ], - "protocol": "foundation", - "network": "grpc-tls", - "security": "tls-pin", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "grpc-tls" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "M", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "grpc-tls" - }, - "gap_source": null, - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "M", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-M-WS-ED", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "foundation", - "network": "ws-header", - "security": "plain", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "ws-header" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "M", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "ws-header" - }, - "gap_source": null, - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "M", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-V2-HTTP", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "H01", - "H02", - "H03", - "H04" - ], - "protocol": "foundation", - "network": "http", - "security": "plain", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "http" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "V2", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "http" - }, - "gap_source": "tests/protocols/combinations.json", - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "V2", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-V2-H2", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "H05", - "H06" - ], - "protocol": "foundation", - "network": "h2", - "security": "tls-pin", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "h2" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "V2", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "h2" - }, - "gap_source": "tests/protocols/combinations.json", - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "V2", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-V2-WS-HEADER", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "foundation", - "network": "ws-header", - "security": "plain", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "ws-header" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "V2", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "ws-header" - }, - "gap_source": "tests/protocols/combinations.json", - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "V2", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-V2-WS-PATH", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.streams", - "required": true, - "row_ids": [ - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "foundation", - "network": "ws-path", - "security": "plain", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only", - "mode": "ws-path" - }, - "outer_family": "ipv4-loopback", - "inner_family": "ipv4-loopback", - "target_type": "controlled-echo", - "upstream_graph": "direct", - "peer_kind": "V2", - "peer_config": { - "factory": "scripts/src/vcore_scripts/protocol_streams.py", - "mode": "ws-path" - }, - "gap_source": "tests/protocols/combinations.json", - "expected_observation": [ - "server_first", - "payload_65536", - "trailer_exact", - "origin_complete", - "protect_once", - "driver_joined", - "resources_idle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources" - ], - "prerequisites": [ - "V2", - "openssl", - "rust-toolchain" - ], - "runner": "native-stream", - "timeout_seconds": 60 - }, - { - "case_id": "FOUNDATIONS-FEATURES", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.features", - "required": true, - "row_ids": [], - "protocol": "foundation", - "network": "none", - "security": "none", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "none", - "inner_family": "none", - "target_type": "none", - "upstream_graph": "none", - "peer_kind": "unit", - "peer_config": { - "runner": "features" - }, - "gap_source": null, - "expected_observation": [ - "independent-features", - "default-feature", - "no-default-feature", - "build-identity" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "features", - "timeout_seconds": 1200 - }, - { - "case_id": "FOUNDATIONS-SCRIPTS", - "stage": "FOUNDATIONS", - "substage": "FOUNDATIONS.regression", - "required": true, - "row_ids": [], - "protocol": "foundation", - "network": "none", - "security": "none", - "udp_codec": "none", - "field_values": { - "scope": "foundation-only" - }, - "outer_family": "none", - "inner_family": "none", - "target_type": "none", - "upstream_graph": "none", - "peer_kind": "unit", - "peer_config": { - "runner": "scripts" - }, - "gap_source": null, - "expected_observation": [ - "offline-success", - "missing-duplicate-unknown-case", - "download-failure", - "timeout", - "sigint", - "partial-start", - "permission", - "peer-exit", - "cleanup-failure", - "redaction" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "python-toolchain" - ], - "runner": "scripts", - "timeout_seconds": 300 - }, - { - "case_id": "TROJAN-CODEC", - "stage": "TROJAN", - "substage": "TROJAN.codec", - "required": true, - "row_ids": [ - "TR01", - "TR02", - "C03", - "C04" - ], - "protocol": "trojan", - "network": "per-assertion", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "scope": "protocol-consumer" - }, - "outer_family": "per-assertion", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "unit", - "peer_config": { - "selector": "trojan" - }, - "gap_source": null, - "expected_observation": [ - "request_matches_the_official_sha224_and_socks_address_wire_format", - "authentication_and_requests_are_strict_and_never_debug_credentials", - "invalid_truncated_and_oversized_frames_fail_closed_without_payload_leaks", - "datagram_limits_preserve_messages_and_drain_over_budget_responses", - "datagram_sends_one_frame_and_receives_fragmented_consecutive_frames", - "cancelled_receive_preserves_partial_frames_and_does_not_block_send", - "cancelled_partial_send_poison_closes_without_replaying_or_waiting" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "rust", - "timeout_seconds": 600 - }, - { - "case_id": "TROJAN-CFG", - "stage": "TROJAN", - "substage": "TROJAN.configuration", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "C06", - "T01", - "T03", - "T04", - "T05", - "W01", - "W02", - "W03", - "W04", - "W05", - "G01", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "per-assertion", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "scope": "protocol-consumer" - }, - "outer_family": "per-assertion", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "unit", - "peer_config": { - "selector": "trojan_config" - }, - "gap_source": null, - "expected_observation": [ - "trojan_tcp_configuration_and_node_graph_accept_the_approved_fields", - "trojan_tcp_defaults_preserve_credentials_and_address_policy", - "trojan_invalid_configuration_is_rejected_without_exposing_credentials", - "trojan_ws_and_grpc_configuration_applies_transport_specific_defaults", - "trojan_transport_boundaries_fail_before_runtime_io" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "rust", - "timeout_seconds": 600 - }, - { - "case_id": "TROJAN-CANCEL", - "stage": "TROJAN", - "substage": "TROJAN.cancellation", - "required": true, - "row_ids": [ - "C06", - "T01", - "T03", - "W01", - "W02", - "W03", - "G01", - "TR02" - ], - "protocol": "trojan", - "network": "per-assertion", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "scope": "protocol-consumer" - }, - "outer_family": "per-assertion", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "unit", - "peer_config": { - "selector": "trojan_lifecycle" - }, - "gap_source": null, - "expected_observation": [ - "trojan_handshake_deadlines_cancel_tls_ws_and_grpc_without_retaining_io", - "trojan_protect_rejection_and_expired_deadline_send_no_network_bytes" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "resources" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "rust", - "timeout_seconds": 600 - }, - { - "case_id": "TROJAN-M-TCP", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-ED-1", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-ed-1", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 1, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws-ed-1", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-ED-2048", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-ed-2048", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 2048, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws-ed-2048", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-POLICY", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "C06", - "T01", - "T04", - "T05", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "public_trojan_native_policy_and_group_snapshots" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_policy_and_group_snapshots" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-POLICY", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "C06", - "T01", - "T04", - "T05", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "public_trojan_native_policy_and_group_snapshots" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_policy_and_group_snapshots" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-NEGATIVE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "public_trojan_native_transport_negative" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_transport_negative" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-NEGATIVE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "public_trojan_native_transport_negative" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_transport_negative" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-ALPN-NEGATIVE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-alpn", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws-alpn", - "test": "public_trojan_native_transport_negative" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_transport_negative" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-V2-WS-HEADER", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-header", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 2048, - "udp_payload_max": 2048 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "V2", - "peer_config": { - "mode": "ws-header", - "test": "public_trojan_native_extended_early_data" - }, - "gap_source": { - "url": "https://github.com/v2fly/v2ray-core/blob/master/transport/internet/websocket/config.proto", - "limitation": "M listener only decodes Sec-WebSocket-Protocol ED; V2Ray maxEarlyData/earlyDataHeaderName covers custom-header and path suffix." - }, - "expected_observation": [ - "public_trojan_native_extended_early_data" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-V2-WS-PATH", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03", - "W04", - "W05" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-path", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 2048, - "udp_payload_max": 2048 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "V2", - "peer_config": { - "mode": "ws-path", - "test": "public_trojan_native_extended_early_data" - }, - "gap_source": { - "url": "https://github.com/v2fly/v2ray-core/blob/master/transport/internet/websocket/config.proto", - "limitation": "M listener only decodes Sec-WebSocket-Protocol ED; V2Ray maxEarlyData/earlyDataHeaderName covers custom-header and path suffix." - }, - "expected_observation": [ - "public_trojan_native_extended_early_data" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-POLICY", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "C06", - "T01", - "T04", - "T05" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp", - "test": "public_trojan_native_policy_and_group_snapshots" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_policy_and_group_snapshots" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-XR-UDP-DOMAIN", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8166 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "XR", - "peer_config": { - "mode": "tcp", - "test": "public_trojan_native_udp_domain" - }, - "gap_source": { - "url": "https://github.com/MetaCubeX/mihomo/blob/Meta/transport/trojan/trojan.go", - "limitation": "WaitReadFrom calls socks5.Addr.UDPAddr(), which returns nil for a domain; actual M failure retained in TROJAN-tcp.md; Xray owns domain UDP supplement." - }, - "expected_observation": [ - "public_trojan_native_udp_domain" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-XR-WS-UDP-DOMAIN", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8166 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "XR", - "peer_config": { - "mode": "ws", - "test": "public_trojan_native_udp_domain" - }, - "gap_source": { - "url": "https://github.com/MetaCubeX/mihomo/blob/Meta/transport/trojan/trojan.go", - "limitation": "WaitReadFrom calls socks5.Addr.UDPAddr(), which returns nil for a domain; actual M failure retained in TROJAN-tcp.md; Xray owns domain UDP supplement." - }, - "expected_observation": [ - "public_trojan_native_udp_domain" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-XR-GRPC-UDP-DOMAIN", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8166 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "XR", - "peer_config": { - "mode": "grpc", - "test": "public_trojan_native_udp_domain" - }, - "gap_source": { - "url": "https://github.com/MetaCubeX/mihomo/blob/Meta/transport/trojan/trojan.go", - "limitation": "WaitReadFrom calls socks5.Addr.UDPAddr(), which returns nil for a domain; actual M failure retained in TROJAN-tcp.md; Xray owns domain UDP supplement." - }, - "expected_observation": [ - "public_trojan_native_udp_domain" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-LIFE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp", - "test": "runtime::public_trojan_native_lifecycle" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_lifecycle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "twenty-quiet-cycles" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-ENTRYPOINTS", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp", - "test": "runtime::public_trojan_native_entrypoints" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_entrypoints" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-UDP-ISOLATION", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp", - "test": "runtime::public_trojan_native_udp_isolation_and_limit" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_udp_isolation_and_limit" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-IPV6", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp-ipv6", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv6", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp-ipv6", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-OWNED", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp", - "test": "trojan_native_owned_resources" - }, - "gap_source": null, - "expected_observation": [ - "trojan_native_owned_resources" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources", - "twenty-quiet-cycles" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-TCP-CERTIFICATE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T01", - "T04", - "T05" - ], - "protocol": "trojan", - "network": "tcp", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "tcp-ca", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "tcp-ca", - "test": "runtime::public_trojan_native_certificate_names" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_certificate_names" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-LIFE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "runtime::public_trojan_native_lifecycle" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_lifecycle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "twenty-quiet-cycles" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-ENTRYPOINTS", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "runtime::public_trojan_native_entrypoints" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_entrypoints" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-UDP-ISOLATION", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "runtime::public_trojan_native_udp_isolation_and_limit" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_udp_isolation_and_limit" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-IPV6", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-ipv6", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv6", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws-ipv6", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-OWNED", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws", - "test": "trojan_native_owned_resources" - }, - "gap_source": null, - "expected_observation": [ - "trojan_native_owned_resources" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources", - "twenty-quiet-cycles" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-WS-CERTIFICATE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T01", - "T04", - "T05", - "T03", - "W01", - "W02", - "W03" - ], - "protocol": "trojan", - "network": "ws", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "ws-ca", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "ws-ca", - "test": "runtime::public_trojan_native_certificate_names" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_certificate_names" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-LIFE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "runtime::public_trojan_native_lifecycle" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_lifecycle" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "twenty-quiet-cycles" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-ENTRYPOINTS", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "runtime::public_trojan_native_entrypoints" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_entrypoints" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-UDP-ISOLATION", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "runtime::public_trojan_native_udp_isolation_and_limit" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_udp_isolation_and_limit" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-IPV6", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc-ipv6", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv6", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc-ipv6", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-OWNED", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc", - "test": "trojan_native_owned_resources" - }, - "gap_source": null, - "expected_observation": [ - "trojan_native_owned_resources" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity", - "resources", - "twenty-quiet-cycles" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-CERTIFICATE", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T01", - "T04", - "T05", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc-ca", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc-ca", - "test": "runtime::public_trojan_native_certificate_names" - }, - "gap_source": null, - "expected_observation": [ - "runtime::public_trojan_native_certificate_names" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-M-GRPC-CUSTOM", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [ - "C01", - "C02", - "C03", - "C04", - "C05", - "TR01", - "TR02", - "T03", - "G01" - ], - "protocol": "trojan", - "network": "grpc", - "security": "standard-tls", - "udp_codec": "trojan-native", - "field_values": { - "network": "grpc-custom", - "udp": true, - "password": "raw-UTF8-with-surrounding-whitespace", - "early_data": 0, - "udp_payload_max": 8192 - }, - "outer_family": "IPv4", - "inner_family": "IPv4/IPv6/domain", - "target_type": "controlled-fixture", - "upstream_graph": "direct/concrete/select/DIRECT/REJECT", - "peer_kind": "M", - "peer_config": { - "mode": "grpc-custom", - "test": "public_trojan_native_base" - }, - "gap_source": null, - "expected_observation": [ - "public_trojan_native_base" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "FOUNDATIONS-shared-foundations" - ], - "runner": "native-trojan", - "timeout_seconds": 240, - "event_suite": "TROJAN-NATIVE" - }, - { - "case_id": "TROJAN-FEATURES", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [], - "protocol": "trojan", - "network": "none", - "security": "none", - "udp_codec": "none", - "field_values": { - "scope": "protocol-consumer" - }, - "outer_family": "none", - "inner_family": "none", - "target_type": "none", - "upstream_graph": "none", - "peer_kind": "unit", - "peer_config": { - "runner": "features" - }, - "gap_source": null, - "expected_observation": [ - "independent-features", - "default-feature", - "no-default-feature", - "build-identity" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "rust-toolchain" - ], - "runner": "features", - "timeout_seconds": 1200, - "event_suite": "FOUNDATIONS-FEATURES" - }, - { - "case_id": "TROJAN-SCRIPTS", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [], - "protocol": "trojan", - "network": "none", - "security": "none", - "udp_codec": "none", - "field_values": { - "scope": "protocol-consumer" - }, - "outer_family": "none", - "inner_family": "none", - "target_type": "none", - "upstream_graph": "none", - "peer_kind": "unit", - "peer_config": { - "runner": "scripts" - }, - "gap_source": null, - "expected_observation": [ - "offline-success", - "missing-duplicate-unknown-case", - "download-failure", - "timeout", - "sigint", - "partial-start", - "permission", - "peer-exit", - "cleanup-failure", - "redaction" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup" - ], - "prerequisites": [ - "python-toolchain" - ], - "runner": "scripts", - "timeout_seconds": 300, - "event_suite": "FOUNDATIONS-SCRIPTS" - }, - { - "case_id": "TROJAN-REGRESSION", - "stage": "TROJAN", - "substage": "TROJAN.acceptance", - "required": true, - "row_ids": [], - "protocol": "trojan", - "network": "supplied-io-or-loopback", - "security": "per-assertion", - "udp_codec": "per-assertion", - "field_values": { - "scope": "protocol-consumer" - }, - "outer_family": "per-assertion", - "inner_family": "per-assertion", - "target_type": "controlled-fixture", - "upstream_graph": "per-assertion", - "peer_kind": "M", - "peer_config": { - "selector": "mihomo_interop" - }, - "gap_source": null, - "expected_observation": [ - "public_client_inbounds_interoperate_with_mihomo_in_both_directions" - ], - "required_evidence": [ - "structured-assertions", - "command", - "cleanup", - "peer-identity" - ], - "prerequisites": [ - "rust-toolchain", - "M", - "apple-container" - ], - "runner": "mihomo-legacy", - "timeout_seconds": 900, - "event_suite": "FOUNDATIONS-REGRESSION" - } - ] -} diff --git a/tests/protocols/limits.json b/tests/protocols/limits.json index 8941dc3..16e8c7b 100644 --- a/tests/protocols/limits.json +++ b/tests/protocols/limits.json @@ -622,11 +622,22 @@ ] }, { - "id": "tun-dns-ingress", - "value": 128, + "id": "tun-udp-association", + "value": 64, + "unit": "items-per-association", + "owner": "ResourceLimits::tun_udp_association_queue_capacity", + "saturation": "drop only the current request without blocking other sources or refreshing activity", + "cancellation": "association child cancellation and runtime Stop join all owned tasks", + "boundary_cases": [ + "FOUNDATIONS-REGRESSION" + ] + }, + { + "id": "tun-udp-response", + "value": 4096, "unit": "items", - "owner": "ResourceLimits::tun_dns_ingress_queue_capacity", - "saturation": "retain existing per-object queue/cache/buffer policy in docs/runtime-resource-policy.md", + "owner": "ResourceLimits::tun_udp_response_queue_capacity", + "saturation": "drop only the current ordinary UDP response without refreshing association activity", "cancellation": "existing runtime-owned cancellation and Stop barrier", "boundary_cases": [ "FOUNDATIONS-REGRESSION" diff --git a/tests/protocols/stream_probe.rs b/tests/protocols/stream_probe.rs deleted file mode 100644 index 55b4f73..0000000 --- a/tests/protocols/stream_probe.rs +++ /dev/null @@ -1,316 +0,0 @@ -//! FOUNDATIONS shared transport probe. Synthetic VLESS framing is test-only; this is -//! not a production protocol/configuration registration. - -use std::{ - io, - net::SocketAddr, - sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, - }, - time::Duration, -}; - -use serde_json::{Value, json}; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::timeout, -}; -use vcore::{ - config::Socks5OutboundConfig, - dialer::{Dialer, ResolvedEndpoint, SocketProtector}, - outbound::{DirectOutbound, EstablishContext, OutboundConnector, Socks5Outbound}, - session::{InboundKind, StreamSession}, -}; -use vcore::{ - dispatch::BoxStream, - security::{SecurityContext, StandardTlsClient, TlsCertificatePolicy, TlsClientOptions}, - transport::{ - HttpObfsOptions, WebSocketEarlyData, WebSocketOptions, connect_websocket, grpc, http_obfs, - legacy_h2, - }, -}; - -const GREETING: &[u8] = b"FOUNDATIONS-server-first\n"; -const TRAILER: &[u8] = b"FOUNDATIONS-half-close\n"; -const UUID: [u8; 16] = [ - 0xb8, 0x31, 0x38, 0x1d, 0x63, 0x24, 0x4d, 0x53, 0xad, 0x4f, 0x8c, 0xda, 0x48, 0xb3, 0x08, 0x11, -]; - -struct Protector { - calls: AtomicUsize, - reject: bool, -} -impl SocketProtector for Protector { - fn protect(&self, _socket: i32) -> io::Result<()> { - self.calls.fetch_add(1, Ordering::SeqCst); - if self.reject { - Err(io::ErrorKind::PermissionDenied.into()) - } else { - Ok(()) - } - } -} - -fn field<'a>(config: &'a Value, name: &str) -> Result<&'a str, &'static str> { - config[name].as_str().ok_or("invalid_fixture") -} - -fn tls_client(config: &Value, mode: &str) -> Result { - let pin = field(config, "pin")?; - if pin.len() != 64 { - return Err("invalid_fixture"); - } - let mut fingerprint = [0; 32]; - for (index, byte) in fingerprint.iter_mut().enumerate() { - *byte = u8::from_str_radix(&pin[index * 2..index * 2 + 2], 16) - .map_err(|_| "invalid_fixture")?; - } - let alpn = if matches!(mode, "grpc-tls" | "h2") { - b"h2".to_vec() - } else { - b"http/1.1".to_vec() - }; - StandardTlsClient::with_options( - &SecurityContext::new(), - "localhost", - TlsClientOptions { - alpn: vec![alpn.clone()], - required_alpn: Some(alpn), - certificate: TlsCertificatePolicy { - fingerprint: Some(fingerprint), - ..Default::default() - }, - ..Default::default() - }, - 0, - 65536, - ) - .map_err(|_| "tls_options") -} - -fn preface(config: &Value) -> Result, &'static str> { - let target: SocketAddr = field(config, "target")? - .parse() - .map_err(|_| "invalid_fixture")?; - let mut preface = vec![0]; - preface.extend_from_slice(&UUID); - preface.extend_from_slice(&[0, 1]); - preface.extend_from_slice(&target.port().to_be_bytes()); - preface.push(1); - preface.extend_from_slice(&[127, 0, 0, 1]); - Ok(preface) -} - -async fn exchange( - stream: &mut BoxStream, - config: &Value, - mode: &str, -) -> Result { - if !matches!(mode, "ws" | "wss" | "ws-header" | "ws-path" | "http") { - stream - .write_all(&preface(config)?) - .await - .map_err(|_| "preface_write")?; - stream.flush().await.map_err(|_| "preface_flush")?; - } - let mut response = [0; 2]; - stream - .read_exact(&mut response) - .await - .map_err(|_| "response")?; - if response != [0, 0] { - return Err("response"); - } - let mut greeting = vec![0; GREETING.len()]; - stream - .read_exact(&mut greeting) - .await - .map_err(|_| "greeting")?; - if greeting != GREETING { - return Err("greeting"); - } - let payload: Vec<_> = (0..65536).map(|n| (n % 251) as u8).collect(); - for fragment in payload.chunks(4096) { - stream.write_all(fragment).await.map_err(|_| "upload")?; - stream.flush().await.map_err(|_| "flush")?; - let mut echo = vec![0; fragment.len()]; - stream.read_exact(&mut echo).await.map_err(|_| "download")?; - if echo != fragment { - return Err("integrity"); - } - } - let close = config["close"].as_bool().unwrap_or(false); - let mut tail = Vec::new(); - if close { - stream.shutdown().await.map_err(|_| "shutdown")?; - timeout( - Duration::from_secs(3), - stream.take(256).read_to_end(&mut tail), - ) - .await - .map_err(|_| "close_timeout")? - .map_err(|_| "close_read")?; - if mode.starts_with("grpc") || mode == "h2" { - if !tail.is_empty() { - return Err("unexpected_tail"); - } - } else if tail != TRAILER { - return Err("tail"); - } - } else { - tail.resize(TRAILER.len(), 0); - stream - .read_exact(&mut tail) - .await - .map_err(|_| "tail_read")?; - if tail != TRAILER { - return Err("tail"); - } - } - Ok( - json!({"outcome":"pass", "payload_bytes":payload.len(), "server_first":true, "tail_bytes":tail.len(), "close":close}), - ) -} - -async fn run(config: Value) -> Value { - let protector = Arc::new(Protector { - calls: AtomicUsize::new(0), - reject: config["reject"].as_bool().unwrap_or(false), - }); - let mut driver = None; - let result = timeout(Duration::from_secs(12), async { - let mode = field(&config, "mode")?; - let server: SocketAddr = field(&config, "server")? - .parse() - .map_err(|_| "invalid_fixture")?; - let dialer = Dialer::default().with_protector(protector.clone()); - let connector: Box = - if let Some(upstream) = config["socks5"].as_str() { - let address: SocketAddr = upstream.parse().map_err(|_| "invalid_fixture")?; - Box::new( - Socks5Outbound::new( - &Socks5OutboundConfig { - address: address.ip().to_string(), - port: address.port(), - username: None, - password: None, - }, - ResolvedEndpoint { - logical_host: address.ip().to_string(), - port: address.port(), - addresses: vec![address], - }, - dialer, - ) - .map_err(|_| "socks_config")?, - ) - } else { - Box::new(DirectOutbound::new(dialer)) - }; - let context = EstablishContext::with_timeout(Duration::from_secs(4)); - let mut stream = connector - .connect_stream( - StreamSession { - inbound: InboundKind::Socks5, - source: "127.0.0.1:0".parse().unwrap(), - destination: server.into(), - sniffed_domain: None, - }, - &context, - ) - .await - .map_err(|_| "connect")? - .io; - if matches!(mode, "tls" | "wss" | "grpc-tls" | "h2") { - stream = context - .run_io("TLS", tls_client(&config, mode)?.connect(stream)) - .await - .map_err(|_| "tls")?; - } - if matches!(mode, "ws" | "wss" | "ws-header" | "ws-path") { - let path = config["path"].as_str().unwrap_or("/foundations-ws"); - let early = match mode { - "ws-header" => Some(WebSocketEarlyData::Header { - name: config["ed_header"] - .as_str() - .unwrap_or("x-vcore-ed") - .parse() - .map_err(|_| "invalid_fixture")?, - max_bytes: config["ed_max"].as_u64().unwrap_or(2048) as usize, - }), - "ws-path" => Some(WebSocketEarlyData::Path { - max_bytes: config["ed_max"].as_u64().unwrap_or(2048) as usize, - }), - _ => None, - }; - let options = - WebSocketOptions::new(&format!("ws://localhost{path}"), Default::default(), early) - .map_err(|_| "ws_options")?; - stream = connect_websocket(stream, &options, &preface(&config)?, context.deadline()) - .await - .map_err(|_| "ws")?; - } - if mode == "http" { - let options = HttpObfsOptions::new( - http::Method::GET, - "http://localhost/foundations-http", - Default::default(), - ) - .map_err(|_| "http_options")?; - stream = http_obfs(stream, &options, &preface(&config)?, context.deadline()) - .await - .map_err(|_| "http")?; - } - if mode.starts_with("grpc") || mode == "h2" { - let uri = if mode == "h2" { - "https://localhost/foundations-h2" - } else { - "https://localhost/foundations-grpc/Tun" - }; - let (io, owner) = if mode == "h2" { - legacy_h2(stream, uri, context.deadline()).await - } else { - grpc(stream, uri, context.deadline()).await - } - .map_err(|_| "h2")?; - stream = io; - driver = Some(owner); - } - exchange(&mut stream, &config, mode).await - }) - .await - .unwrap_or(Err("case_timeout")); - let stopped = if let Some(owner) = driver { - owner.stop().await.is_ok() - } else { - true - }; - let mut report = result.unwrap_or_else(|stage| json!({"outcome":stage})); - report["driver_joined"] = json!(stopped); - report["protect_calls"] = json!(protector.calls.load(Ordering::SeqCst)); - report -} - -#[tokio::main(flavor = "current_thread")] -async fn main() { - let config = std::env::args() - .nth(1) - .and_then(|path| std::fs::read(path).ok()) - .and_then(|bytes| serde_json::from_slice(&bytes).ok()); - let resources = vcore::resources::observation::ResourceProbe::default(); - let mut report = match config { - Some(config) => resources.scope(run(config)).await, - None => json!({"outcome":"invalid_fixture"}), - }; - let stopped = resources.snapshot(); - report["resources_idle"] = json!(stopped.is_idle()); - report["resources"] = serde_json::to_value(stopped).unwrap(); - println!("{report}"); - if report["outcome"] != "pass" - || report["driver_joined"] != true - || report["resources_idle"] != true - { - std::process::exit(1); - } -} diff --git a/tests/trojan_lifecycle.rs b/tests/trojan_lifecycle.rs deleted file mode 100644 index 9f4c542..0000000 --- a/tests/trojan_lifecycle.rs +++ /dev/null @@ -1,290 +0,0 @@ -// These probes exercise the Unix fd protector. Windows uses its separate -// physical-interface binding contract, not this callback. -#![cfg(all(unix, feature = "outbound-trojan", feature = "interop-test"))] - -use serde_json::json; -use sha2::{Digest, Sha256}; -use std::{ - io, - sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, - }, - time::Duration, -}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint, SocketProtector}, - outbound::{EstablishContext, OutboundConnector, UpstreamPath, trojan::TrojanOutbound}, - resources::{ - case_events::Case, - observation::{ResourceKind, ResourceProbe}, - }, - session::{Destination, InboundKind, StreamSession}, -}; - -#[derive(Default)] -struct Protect { - calls: AtomicUsize, - reject: bool, -} - -#[tokio::test] -#[ignore = "requires the owned TROJAN native-peer runner"] -async fn trojan_native_owned_resources() { - use bytes::Bytes; - use vcore::{ - outbound::DatagramRequest, - session::{Datagram, DatagramSession}, - }; - let _case = Case::new("TROJAN-NATIVE", "trojan_native_owned_resources"); - let fixture: serde_json::Value = - serde_json::from_str(&std::env::var("VCORE_TROJAN_FIXTURE").unwrap()).unwrap(); - let node = &fixture["node"]; - let config = Config::parse_yaml( - json!({"socks-port":1080,"proxies":[node],"rules":["MATCH,peer"]}) - .to_string() - .as_bytes(), - ) - .unwrap(); - let ProxyProtocol::Trojan(config) = &config.proxies[0].protocol else { - unreachable!() - }; - for _ in 0..20 { - let mut cycle = Case::new("TROJAN-OWNED-CYCLE", "stop_and_remain_quiet"); - let probe = ResourceProbe::default(); - cycle.checkpoint("baseline", probe.snapshot()); - probe - .scope(async { - let address = std::net::SocketAddr::new( - node["server"].as_str().unwrap().parse().unwrap(), - node["port"].as_u64().unwrap() as u16, - ); - let endpoint = ResolvedEndpoint { - logical_host: node["server"].as_str().unwrap().into(), - port: address.port(), - addresses: vec![address], - }; - let protect = Arc::new(Protect::default()); - let outbound = TrojanOutbound::new_with_path( - config, - UpstreamPath::direct( - endpoint, - Dialer::default().with_protector(protect.clone()), - ), - ) - .unwrap(); - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let mut request = session(); - request.destination = Destination::Ip(listener.local_addr().unwrap()); - let mut first = outbound - .connect_stream(request.clone(), &EstablishContext::default()) - .await - .unwrap() - .io; - first.write_all(b"one").await.unwrap(); - first.flush().await.unwrap(); - let (mut remote1, _) = listener.accept().await.unwrap(); - assert_eq!(&remote1.read_u8().await.unwrap(), &b'o'); - let mut second = outbound - .connect_stream(request, &EstablishContext::default()) - .await - .unwrap() - .io; - second.write_all(b"two").await.unwrap(); - second.flush().await.unwrap(); - let (mut remote2, _) = listener.accept().await.unwrap(); - let mut bytes = [0; 3]; - remote2.read_exact(&mut bytes).await.unwrap(); - assert_eq!(&bytes, b"two"); - drop(first); - remote2.write_all(b"live").await.unwrap(); - let mut reply = [0; 4]; - second.read_exact(&mut reply).await.unwrap(); - assert_eq!(&reply, b"live"); - let origin = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap(); - let datagram = DatagramRequest::new(DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )); - let mut udp = outbound - .open_datagram(datagram, &EstablishContext::default()) - .await - .unwrap(); - udp.send(Datagram { - remote: Destination::Ip(origin.local_addr().unwrap()), - payload: Bytes::from_static(b"udp"), - sniffed_domain: None, - }) - .await - .unwrap(); - let (n, source) = origin.recv_from(&mut bytes).await.unwrap(); - assert_eq!(&bytes[..n], b"udp"); - origin.send_to(b"udp", source).await.unwrap(); - assert_eq!(udp.receive().await.unwrap().payload.as_ref(), b"udp"); - assert!(probe.snapshot().peak(ResourceKind::Association) > 0); - assert!(probe.snapshot().peak(ResourceKind::Session) > 0); - assert!(probe.snapshot().peak(ResourceKind::Socket) > 0); - if node["network"] == "grpc" { - assert!(probe.snapshot().peak(ResourceKind::Task) > 0); - } - // RunningCore first cancels and joins inbound owners, dropping - // their streams/associations, then joins protocol-owned drivers. - outbound.begin_shutdown(); - assert!(second.read_u8().await.is_err()); - drop(second); - udp.close().await.unwrap(); - drop(udp); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - assert!( - probe.snapshot().is_idle(), - "Stop retained owned resources: {:?}", - probe.snapshot() - ); - cycle.checkpoint("after-stop", probe.snapshot()); - let calls = protect.calls.load(Ordering::SeqCst); - assert_eq!(calls, 3); - let stopped = probe.snapshot(); - let quiet = tokio::time::Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert_eq!(probe.snapshot(), stopped); - assert_eq!(protect.calls.load(Ordering::SeqCst), calls); - tokio::time::sleep(Duration::from_millis(25)).await; - } - cycle.checkpoint("quiet", probe.snapshot()); - drop(outbound); - assert_eq!(Arc::strong_count(&protect), 1); - }) - .await; - cycle.resources(probe.snapshot()); - } -} -impl SocketProtector for Protect { - fn protect(&self, _: i32) -> io::Result<()> { - self.calls.fetch_add(1, Ordering::SeqCst); - if self.reject { - Err(io::ErrorKind::PermissionDenied.into()) - } else { - Ok(()) - } - } -} - -fn session() -> StreamSession { - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: Destination::Ip("127.0.0.1:9".parse().unwrap()), - sniffed_domain: None, - } -} - -#[tokio::test] -async fn trojan_handshake_deadlines_cancel_tls_ws_and_grpc_without_retaining_io() { - let mut case = Case::new( - "TROJAN-CANCEL", - "trojan_handshake_deadlines_cancel_tls_ws_and_grpc_without_retaining_io", - ); - for mode in ["tls", "ws", "grpc"] { - let probe = ResourceProbe::default(); - probe.scope(async { - let key = rcgen::KeyPair::generate().unwrap(); - let cert = rcgen::CertificateParams::new(vec!["fixture.invalid".into()]).unwrap().self_signed(&key).unwrap(); - let pin: String = Sha256::digest(cert.der().as_ref()).iter().map(|byte|format!("{byte:02x}")).collect(); - let mut tls = rustls::ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) - .with_safe_default_protocol_versions().unwrap().with_no_client_auth() - .with_single_cert(vec![cert.der().clone()],rustls::pki_types::PrivateKeyDer::Pkcs8(key.serialize_der().into())).unwrap(); - tls.alpn_protocols = vec![if mode == "grpc" {b"h2".to_vec()} else {b"http/1.1".to_vec()}]; - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let address = listener.local_addr().unwrap(); - let peer = tokio::spawn(async move { - let (mut raw,_) = listener.accept().await.unwrap(); - if mode == "tls" { - let mut bytes = [0;4096]; - assert!(raw.read(&mut bytes).await.unwrap() > 0); - assert_eq!(raw.read(&mut bytes).await.unwrap(),0); - return; - } - let mut stream = tokio_rustls::TlsAcceptor::from(Arc::new(tls)).accept(raw).await.unwrap(); - if mode == "ws" { - let mut request = Vec::new(); - while !request.ends_with(b"\r\n\r\n") { request.push(stream.read_u8().await.unwrap()); } - let request = String::from_utf8(request).unwrap().to_ascii_lowercase(); - assert!(request.starts_with("get /edge?q=1 http/1.1")); - assert!(request.contains("host: cover.example:443\r\n")); - assert!(request.contains("x-fixture: exact-value\r\n")); - // Withhold the Upgrade, exercising cancellation after the - // actual protocol consumer wrote its configured request. - let mut bytes = Vec::new(); - let _ = stream.read_to_end(&mut bytes).await; - } else { - let mut h2 = h2::server::handshake(stream).await.unwrap(); - let (request,_response) = h2.accept().await.unwrap().unwrap(); - assert_eq!(request.uri().path(),"/custom/Tun"); - // No response headers: the gRPC response deadline must - // still be the original setup deadline, not a fresh one. - while let Some(result) = h2.accept().await { if result.is_err() { break; } } - } - }); - let mut node = json!({"name":"peer","type":"trojan","server":"127.0.0.1","port":address.port(),"password":"fixture","sni":"fixture.invalid","fingerprint":pin}); - match mode { - "ws" => { node["network"] = json!("ws"); node["ws-opts"] = json!({"path":"/edge?q=1","headers":{"Host":"cover.example:443","X-Fixture":"exact-value"}}); }, - "grpc" => { node["network"] = json!("grpc"); node["grpc-opts"] = json!({"grpc-service-name":"/custom/Tun"}); }, - _ => {}, - } - let parsed = Config::parse_yaml(json!({"socks-port":1080,"proxies":[node],"rules":["MATCH,peer"]}).to_string().as_bytes()).unwrap(); - let ProxyProtocol::Trojan(config) = &parsed.proxies[0].protocol else { unreachable!() }; - let protect = Arc::new(Protect::default()); - let endpoint = ResolvedEndpoint { logical_host:"127.0.0.1".into(),port:address.port(),addresses:vec![address] }; - let outbound = TrojanOutbound::new_with_path(config,UpstreamPath::direct(endpoint,Dialer::default().with_protector(protect.clone()))).unwrap(); - let started = tokio::time::Instant::now(); - let context = EstablishContext::with_timeout(Duration::from_millis(150)); - let connected = outbound.connect_stream(session(),&context).await; - if mode == "grpc" { - let mut stream = connected.unwrap().io; - assert!(tokio::time::timeout(Duration::from_secs(1),stream.read_u8()).await.unwrap().is_err()); - drop(stream); - } else { assert!(connected.is_err()); } - assert!(started.elapsed() < Duration::from_secs(1)); - tokio::time::timeout(Duration::from_secs(5),outbound.shutdown()).await.unwrap(); - drop(outbound); - tokio::time::timeout(Duration::from_secs(1),peer).await.unwrap().unwrap(); - assert_eq!(protect.calls.load(Ordering::SeqCst),1); - assert_eq!(Arc::strong_count(&protect),1); - assert!(probe.snapshot().is_idle(),"mode={mode}: {:?}",probe.snapshot()); - assert_eq!(probe.snapshot().peak(ResourceKind::Socket),1); - }).await; - case.checkpoint("after-stop", probe.snapshot()); - case.resources(probe.snapshot()); - } -} - -#[tokio::test] -async fn trojan_protect_rejection_and_expired_deadline_send_no_network_bytes() { - let mut case = Case::new( - "TROJAN-CANCEL", - "trojan_protect_rejection_and_expired_deadline_send_no_network_bytes", - ); - let probe = ResourceProbe::default(); - probe.scope(async { - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let address = listener.local_addr().unwrap(); - let parsed = Config::parse_yaml(json!({"socks-port":1080,"proxies":[{"name":"peer","type":"trojan","server":"127.0.0.1","port":address.port(),"password":"fixture"}],"rules":["MATCH,peer"]}).to_string().as_bytes()).unwrap(); - let ProxyProtocol::Trojan(config) = &parsed.proxies[0].protocol else { unreachable!() }; - let protect = Arc::new(Protect {reject:true,..Default::default()}); - let endpoint = ResolvedEndpoint {logical_host:"127.0.0.1".into(),port:address.port(),addresses:vec![address]}; - let outbound = TrojanOutbound::new_with_path(config,UpstreamPath::direct(endpoint,Dialer::default().with_protector(protect.clone()))).unwrap(); - assert!(outbound.connect_stream(session(),&EstablishContext::default()).await.is_err()); - assert_eq!(protect.calls.load(Ordering::SeqCst),1); - assert!(outbound.connect_stream(session(),&EstablishContext::with_timeout(Duration::ZERO)).await.is_err()); - assert_eq!(protect.calls.load(Ordering::SeqCst),1); - assert!(tokio::time::timeout(Duration::from_millis(20),listener.accept()).await.is_err()); - outbound.shutdown().await; - drop(outbound); - }).await; - assert!(probe.snapshot().is_idle()); - case.resources(probe.snapshot()); -} diff --git a/tests/vless_lifecycle.rs b/tests/vless_lifecycle.rs index 7082046..e6b2b04 100644 --- a/tests/vless_lifecycle.rs +++ b/tests/vless_lifecycle.rs @@ -121,10 +121,10 @@ impl SocketProtector for RejectProtect { } #[test] -fn split_xhttp_construction_fits_the_runtime_stack_and_protect_failure_has_no_fallback() { - // Same stack budget as the Invoke runtime. No server or outbound packet: +fn split_xhttp_construction_uses_default_stack_and_protect_failure_has_no_fallback() { + // Use the default Rust thread stack. No server or outbound packet: // the first socket is rejected by the host-owned protection boundary. - std::thread::Builder::new().stack_size(1024 * 1024).spawn(|| { + std::thread::Builder::new().spawn(|| { tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { for alpn in ["h3", "h2", "http/1.1"] { let raw=serde_json::json!({"socks-port":1080,"proxies":[{"name":"edge","type":"vless","server":"192.0.2.1","port":443,"uuid":"07070707-0707-0707-0707-070707070707","network":"xhttp","tls":true,"alpn":[alpn],"xhttp-opts":{"mode":"packet-up","reuse-settings":{},"download-settings":{"reuse-settings":{}}}}],"rules":["MATCH,edge"]}); diff --git a/tests/vless_native.rs b/tests/vless_native.rs deleted file mode 100644 index fb8d263..0000000 --- a/tests/vless_native.rs +++ /dev/null @@ -1,822 +0,0 @@ -#![cfg(all(feature = "outbound-vless", feature = "interop-test"))] -use serde_json::{Value, json}; - -#[tokio::test] -#[ignore = "requires the owned container harness"] -async fn native_vision_inner_tls() { - use vcore::security::{ - SecurityContext, StandardTlsClient, TlsCertificatePolicy, TlsClientOptions, - }; - let name = "native_vision_inner_tls"; - event(name, "BEGIN"); - let fixture = fixture(); - assert_eq!(fixture["vision_probe"], true); - let mut fingerprint = [0; 32]; - let pin = fixture["origin_pin"].as_str().unwrap(); - for (index, byte) in fingerprint.iter_mut().enumerate() { - *byte = u8::from_str_radix(&pin[index * 2..index * 2 + 2], 16).unwrap(); - } - tokio::time::timeout(Duration::from_secs(120), async { - for (mode, version) in [(16, 0x13), (18, 0x12)] { - // Keep one real node across destination families, so Encryption's - // 0-RTT mode also exercises Vision on reused native tickets. - let outbound = node(&fixture, None); - for family in ["ipv4", "ipv6", "domain"] { - let (mut control, target) = origin(&fixture, mode, family).await; - let before = outbound.vision_raw_bytes(); - let stream = outbound - .connect_stream(session(target), &EstablishContext::default()) - .await - .unwrap() - .io; - let inner = StandardTlsClient::with_options( - &SecurityContext::new(), - "localhost", - TlsClientOptions { - certificate: TlsCertificatePolicy { - fingerprint: Some(fingerprint), - ..Default::default() - }, - ..Default::default() - }, - 0, - 65536, - ) - .unwrap(); - let mut stream = - tokio::time::timeout(Duration::from_secs(10), inner.connect(stream)) - .await - .unwrap() - .unwrap(); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(control.read_u8().await.unwrap(), version); - let mut hello = [0; 5]; - stream.read_exact(&mut hello).await.unwrap(); - assert_eq!(&hello, b"hello"); - let payload = vec![0x5a; 10 * 1024 * 1024]; - stream.write_all(&payload).await.unwrap(); - stream.flush().await.unwrap(); - let mut response = vec![0; payload.len() + 7]; - stream.read_exact(&mut response).await.unwrap(); - assert_eq!(&response[..payload.len()], payload.as_slice()); - assert_eq!(&response[payload.len()..], b"trailer"); - let (read, written) = outbound.vision_raw_bytes(); - let (read, written) = (read - before.0, written - before.1); - if version == 0x13 { - assert!( - read > 9 * 1024 * 1024 && written > 9 * 1024 * 1024, - "no actual bidirectional direct-mode IO" - ); - } else { - assert_eq!( - (read, written), - (0, 0), - "TLS 1.2 must remain inside outer TLS" - ); - } - stream.shutdown().await.unwrap(); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - drop(stream); - } - outbound.shutdown().await; - } - }) - .await - .unwrap(); - event(name, "PASS"); -} - -#[tokio::test] -#[ignore = "official isolated container peer required"] -async fn native_vision_direct_close_alignment() { - use rustls::pki_types::{CertificateDer, ServerName}; - let name = "native_vision_direct_close_alignment"; - event(name, "BEGIN"); - let fixture = fixture(); - assert_eq!(fixture["vision_probe"], true); - let reference = &fixture["direct_close_reference"]; - assert_eq!(reference["scope"], "same-mode"); - assert_eq!(reference["variant"], "vision-direct"); - assert_eq!(reference["terminated"], true); - let cert: Vec = serde_json::from_value(fixture["origin_root_der"].clone()).unwrap(); - let mut roots = rustls::RootCertStore::empty(); - roots.add(CertificateDer::from(cert)).unwrap(); - let config = rustls::ClientConfig::builder_with_provider(Arc::new( - rustls::crypto::ring::default_provider(), - )) - .with_protocol_versions(&[&rustls::version::TLS13]) - .unwrap() - .with_root_certificates(roots) - .with_no_client_auth(); - let connector = tokio_rustls::TlsConnector::from(Arc::new(config)); - tokio::time::timeout(Duration::from_secs(40), async { - let outbound = node(&fixture, None); - for family in ["ipv4", "ipv6"] { - let (mut control, target) = origin(&fixture, 21, family).await; - let before = outbound.vision_raw_bytes(); - let io = outbound - .connect_stream(session(target), &EstablishContext::default()) - .await - .unwrap() - .io; - let mut tls = connector - .connect(ServerName::try_from("localhost").unwrap(), io) - .await - .unwrap(); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(control.read_u8().await.unwrap(), 0x13); - let mut greeting = [0; 5]; - tls.read_exact(&mut greeting).await.unwrap(); - assert_eq!(&greeting, b"hello"); - let payload = vec![b'Z'; 65536]; - tls.write_all(&payload).await.unwrap(); - tls.flush().await.unwrap(); - let mut echo = vec![0; payload.len()]; - tls.read_exact(&mut echo).await.unwrap(); - assert_eq!(echo, payload); - let after = outbound.vision_raw_bytes(); - assert!(after.0 - before.0 > 32768 && after.1 - before.1 > 32768); - // Upload EOF below the real inner TLS session, without adding a - // TLS close_notify. The independent client does exactly the same. - tls.get_mut().0.shutdown().await.unwrap(); - let mut tail = Vec::new(); - let _terminal = tls.read_to_end(&mut tail).await; - let hex: String = tail.iter().map(|byte| format!("{byte:02x}")).collect(); - assert_eq!(hex, reference["tail_hex"].as_str().unwrap()); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - drop(tls); - } - outbound.shutdown().await; - }) - .await - .expect("direct close did not terminate"); - event(name, "PASS"); -} - -#[tokio::test] -#[ignore = "official isolated container peer required"] -async fn native_mihomo_close_alignment() { - let fixture = fixture(); - let node = &fixture["node"]; - let jls_grpc_without_profile = node["network"] == "grpc" - && node["jls-opts"].is_object() - && matches!( - node["client-fingerprint"].as_str(), - None | Some("" | "none") - ); - let ech_safari_grpc = node["network"] == "grpc" - && node["ech-opts"]["enable"] == true - && matches!( - node["client-fingerprint"].as_str(), - Some("safari" | "safari16") - ); - let scope = if jls_grpc_without_profile || ech_safari_grpc { - assert_eq!(fixture["close_reference"]["client_fingerprint"], "chrome"); - assert_eq!( - fixture["close_reference"]["dut_client_fingerprint"], - node["client-fingerprint"] - ); - if jls_grpc_without_profile { - "jls-grpc-chrome-baseline" - } else { - "ech-safari-chrome-baseline" - } - } else { - "same-mode" - }; - compare_native_close("native_mihomo_close_alignment", scope).await; -} - -#[tokio::test] -#[ignore = "official isolated peers required; layered reference, not same-mode"] -async fn native_ws_reality_close_boundary() { - compare_native_close( - "native_ws_reality_close_boundary", - "ws-standard-tls-baseline", - ) - .await; -} - -async fn compare_native_close(name: &str, scope: &str) { - event(name, "BEGIN"); - let fixture = fixture(); - assert_eq!(fixture["close_reference"]["scope"], scope); - tokio::time::timeout(Duration::from_secs(15), async { - let (mut control, target) = origin(&fixture, 11, "ipv4").await; - let outbound = node(&fixture, None); - let mut io = outbound - .connect_stream(session(target), &EstablishContext::default()) - .await - .unwrap() - .io; - let mut hello = [0; 5]; - io.read_exact(&mut hello).await.unwrap(); - assert_eq!(&hello, b"hello"); - io.write_all(b"ping").await.unwrap(); - io.flush().await.unwrap(); - let mut echo = [0; 4]; - io.read_exact(&mut echo).await.unwrap(); - assert_eq!(&echo, b"ping"); - io.shutdown().await.unwrap(); - let mut tail = Vec::new(); - let _terminal = io.read_to_end(&mut tail).await; - let hex: String = tail.iter().map(|b| format!("{b:02x}")).collect(); - assert_eq!(fixture["close_reference"]["terminated"], true); - assert_eq!( - hex, - fixture["close_reference"]["tail_hex"].as_str().unwrap(), - "explicitly scoped close reference" - ); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - drop(io); - outbound.shutdown().await; - }) - .await - .expect("close did not terminate"); - event(name, "PASS"); -} - -#[tokio::test] -#[ignore = "official isolated container peer required"] -async fn native_grpc_pool_thresholds() { - use vcore::resources::observation::{ResourceKind, ResourceProbe}; - let name = "native_grpc_pool_thresholds"; - event(name, "BEGIN"); - let original = fixture(); - for (max, min, streams, expected) in [ - (1, 0, 0, [1, 1, 1, 1]), - (2, 2, 0, [1, 1, 2, 2]), - (2, 0, 0, [1, 2, 2, 2]), - (0, 0, 2, [1, 1, 2, 2]), - (0, 2, 0, [1, 2, 3, 4]), - ] { - let probe = ResourceProbe::default(); - probe - .scope(async { - let mut fixture = original.clone(); - fixture["node"]["grpc-opts"]["max-connections"] = json!(max); - fixture["node"]["grpc-opts"]["min-streams"] = json!(min); - fixture["node"]["grpc-opts"]["max-streams"] = json!(streams); - let outbound = node(&fixture, None); - let mut held = Vec::new(); - for connections in expected { - let (mut observer, target) = origin(&fixture, 13, "ipv4").await; - let mut io = outbound - .connect_stream(session(target), &EstablishContext::default()) - .await - .unwrap() - .io; - io.write_all(b"a").await.unwrap(); - io.flush().await.unwrap(); - assert_eq!(io.read_u8().await.unwrap(), b'a'); - assert_eq!(observer.read_u8().await.unwrap(), b'A'); - assert_eq!( - probe.snapshot().current(ResourceKind::Socket), - connections, - "actual native physical socket count" - ); - held.push((io, observer)); - } - for (mut io, mut observer) in held { - io.shutdown().await.unwrap(); - drop(io); - assert_eq!(observer.read_u8().await.unwrap(), b'D'); - } - outbound.shutdown().await; - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - }) - .await; - } - event(name, "PASS"); -} -use std::{ - io, - net::{IpAddr, SocketAddr}, - sync::Arc, - time::Duration, -}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint, Resolver}, - dispatch::DatagramBudget, - dns::resolution::ResolutionContext, - outbound::{DatagramRequest, EstablishContext, OutboundConnector, VlessOutbound}, - session::{Datagram, DatagramSession, Destination, InboundKind, StreamSession}, -}; - -fn fixture() -> Value { - let value: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_VLESS_INPUT").expect("container harness required")) - .unwrap(), - ) - .unwrap(); - assert_eq!(value["isolation"], "containers"); - value -} -fn event(name: &str, status: &str) { - use std::io::Write; - let mut file = std::fs::OpenOptions::new() - .append(true) - .create(true) - .open(std::env::var("VCORE_CASE_EVENTS").unwrap()) - .unwrap(); - writeln!( - file, - "{}", - json!({"schema_version":1,"suite":if std::env::var("VCORE_PROTOCOL_STAGE").as_deref() == Ok("SECURITY") {"SECURITY-WIRE"} else {"VLESS-WIRE"},"assertion":name,"status":status}) - ) - .unwrap(); -} -fn node(fixture: &Value, encoding: Option<&str>) -> VlessOutbound { - let mut node = fixture["node"].clone(); - if let Some(encoding) = encoding { - node["packet-encoding"] = json!(encoding); - } - let config = Config::parse_yaml( - json!({"socks-port":1080,"proxies":[node],"rules":[format!("MATCH,{}",node["name"].as_str().unwrap())]}) - .to_string() - .as_bytes(), - ) - .unwrap(); - let ProxyProtocol::Vless(config) = &config.proxies[0].protocol else { - unreachable!() - }; - let config = config.clone(); - let endpoint = ResolvedEndpoint { - logical_host: config.address.clone(), - port: config.port, - addresses: vec![SocketAddr::new( - config.address.parse().unwrap(), - config.port, - )], - }; - let download_endpoint = config.download().map(|download| ResolvedEndpoint { - logical_host: download.address.clone(), - port: download.port, - addresses: vec![SocketAddr::new( - download.address.parse().unwrap(), - download.port, - )], - }); - if matches!(config.security, vcore::config::SecurityConfig::Tls(_)) { - let root: Vec = serde_json::from_value(fixture["root_der"].clone()).unwrap(); - VlessOutbound::new_with_test_tls_roots_and_endpoints( - &config, - endpoint, - download_endpoint, - Dialer::default(), - [root], - ) - .unwrap() - } else { - VlessOutbound::new_with_endpoints(&config, endpoint, download_endpoint, Dialer::default()) - .unwrap() - } -} - -#[test] -fn native_fixture_prepares_the_independent_download_endpoint() { - let f = json!({"node": { - "name": "peer", "type": "vless", "server": "192.0.2.1", "port": 443, - "uuid": "00000000-0000-4000-8000-000000000001", "network": "xhttp", - "tls": true, "servername": "fixture.test", - "reality-opts": { - "public-key": "CQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", - "support-x25519mlkem768": true - }, - "xhttp-opts": { - "mode": "stream-up", "path": "/x", - "download-settings": {"port": 444} - } - }}); - let _outbound = node(&f, None); -} - -async fn origin(fixture: &Value, mode: u8, family: &str) -> (tokio::net::TcpStream, Destination) { - let mut control = tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.set_nodelay(true).unwrap(); - control - .write_u8(if family == "ipv6" && mode >= 10 { - mode | 0x80 - } else if mode < 10 { - if family == "ipv6" { 6 } else { 4 } - } else { - mode - }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - let ip: IpAddr = fixture[if family == "ipv6" { - "origin_ipv6" - } else { - "origin_ipv4" - }] - .as_str() - .unwrap() - .parse() - .unwrap(); - assert!(!ip.is_loopback() && !ip.is_unspecified()); - let target = if family == "domain" { - Destination::domain("vcore-fixture.test", port).unwrap() - } else { - SocketAddr::new(ip, port).into() - }; - (control, target) -} -fn session(target: Destination) -> StreamSession { - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: target, - sniffed_domain: None, - } -} - -#[tokio::test] -#[ignore = "isolated SECURITY JLS runner"] -async fn native_jls_fail_closed() { - let name = "native_jls_fail_closed"; - event(name, "BEGIN"); - let original = fixture(); - let split = original["node"]["xhttp-opts"]["download-settings"].is_object(); - for download in [false, true] - .into_iter() - .filter(|download| !download || split) - { - for field in ["username", "password"] { - let mut f = original.clone(); - let credentials = f["node"]["jls-opts"].clone(); - let leg = if download { - let leg = &mut f["node"]["xhttp-opts"]["download-settings"]; - leg["jls-opts"] = credentials; - leg - } else { - &mut f["node"] - }; - leg["jls-opts"][field] = json!("incorrect-credential"); - let (mut control, destination) = origin(&f, 13, "ipv4").await; - let outbound = node(&f, None); - let failure = tokio::time::timeout(Duration::from_secs(5), async { - let mut io = outbound - .connect_stream(session(destination), &EstablishContext::default()) - .await - .map_err(|_| std::io::Error::other("connection rejected"))? - .io; - io.write_all(b"must-not-arrive").await?; - io.flush().await?; - io.read_u8().await - }) - .await - .expect("JLS rejection must not be a timeout"); - assert!( - failure.is_err(), - "invalid JLS identity admitted a business stream" - ); - outbound.shutdown().await; - assert!( - tokio::time::timeout(Duration::from_millis(200), control.read_u8()) - .await - .is_err(), - "JLS rejection must open no origin connection" - ); - } - } - // Use the same public node factory after failures; no fallback or stale identity. - let (mut control, destination) = origin(&original, 13, "ipv4").await; - let outbound = node(&original, None); - let mut io = outbound - .connect_stream(session(destination), &EstablishContext::default()) - .await - .unwrap() - .io; - io.write_all(b"authenticated").await.unwrap(); - io.flush().await.unwrap(); - let mut response = [0; 13]; - io.read_exact(&mut response).await.unwrap(); - assert_eq!(&response, b"authenticated"); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - drop(io); - outbound.shutdown().await; - event(name, "PASS"); -} - -#[tokio::test] -#[ignore = "isolated SECURITY ECH runner"] -async fn native_ech_fail_closed() { - use base64::{Engine as _, engine::general_purpose::STANDARD}; - let name = "native_ech_fail_closed"; - event(name, "BEGIN"); - let original = fixture(); - let split = original["node"]["xhttp-opts"]["download-settings"].is_object(); - for download in [false, true] - .into_iter() - .filter(|download| !download || split) - { - let mut f = original.clone(); - let ech = f["node"]["ech-opts"].clone(); - if split { - f["node"]["xhttp-opts"]["download-settings"]["ech-opts"] = ech.clone(); - } - let leg = if download { - &mut f["node"]["xhttp-opts"]["download-settings"] - } else { - &mut f["node"] - }; - let mut wrong = STANDARD.decode(ech["config"].as_str().unwrap()).unwrap(); - // A well-formed but stale public key must fail during ECH, not parsing. - wrong[11..43].fill(7); - leg["ech-opts"] = json!({"enable":true,"config":STANDARD.encode(wrong)}); - let (mut control, destination) = origin(&f, 13, "ipv4").await; - let outbound = node(&f, None); - let failure = tokio::time::timeout(Duration::from_secs(5), async { - let mut io = outbound - .connect_stream(session(destination), &EstablishContext::default()) - .await - .map_err(|_| std::io::Error::other("connection rejected"))? - .io; - io.write_all(b"must-not-arrive").await?; - io.flush().await?; - io.read_u8().await - }) - .await - .expect("ECH rejection must not be a timeout"); - assert!(failure.is_err(), "rejected ECH admitted a business stream"); - outbound.shutdown().await; - assert!( - tokio::time::timeout(Duration::from_millis(200), control.read_u8()) - .await - .is_err(), - "ECH rejection opened an origin connection" - ); - } - event(name, "PASS"); -} - -#[tokio::test] -#[ignore = "isolated SECURITY hybrid REALITY runner"] -async fn native_hybrid_fail_closed() { - use vcore::{config::SecurityConfig, security::SecurityClient}; - let name = "native_hybrid_fail_closed"; - event(name, "BEGIN"); - let original = fixture(); - let (mut observer, target) = origin(&original, 13, "ipv4").await; - let outbound = node(&original, None); - let mut io = outbound - .connect_stream(session(target), &EstablishContext::default()) - .await - .unwrap() - .io; - io.write_all(b"control").await.unwrap(); - io.flush().await.unwrap(); - let mut reply = [0; 7]; - io.read_exact(&mut reply).await.unwrap(); - assert_eq!(&reply, b"control"); - assert_eq!(observer.read_u8().await.unwrap(), b'A'); - drop(io); - outbound.shutdown().await; - assert_eq!(observer.read_u8().await.unwrap(), b'D'); - - let split = original["node"]["xhttp-opts"]["download-settings"].is_object(); - for download in [false, true] - .into_iter() - .filter(|download| !download || split) - { - for fault in ["public-key", "short-id", "servername"] { - let mut f = original.clone(); - let base_reality = f["node"]["reality-opts"].clone(); - let target = if download { - let leg = &mut f["node"]["xhttp-opts"]["download-settings"]; - leg["reality-opts"] = base_reality; - leg - } else { - &mut f["node"] - }; - match fault { - "public-key" => { - target["reality-opts"][fault] = - json!("CQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") - } - "short-id" => target["reality-opts"][fault] = json!("ffffffffffffffff"), - _ => target[fault] = json!("wrong.fixture.test"), - } - let (mut control, destination) = origin(&f, 13, "ipv4").await; - let outbound = node(&f, None); - let failed = tokio::time::timeout(Duration::from_secs(5), async { - let mut io = outbound - .connect_stream(session(destination), &EstablishContext::default()) - .await - .map_err(|_| std::io::Error::other("connection rejected"))? - .io; - io.write_all(b"must-not-arrive").await?; - io.flush().await?; - io.read_u8().await - }) - .await - .expect("authentication failure must not be a timeout"); - assert!( - failed.is_err(), - "REALITY {fault} accepted on download={download}" - ); - outbound.shutdown().await; - assert!( - tokio::time::timeout(Duration::from_millis(200), control.read_u8()) - .await - .is_err(), - "rejected identity reached the origin" - ); - } - } - - let config = Config::parse_yaml( - json!({"socks-port":1080,"proxies":[original["node"]],"rules":["MATCH,peer"]}) - .to_string() - .as_bytes(), - ) - .unwrap(); - let ProxyProtocol::Vless(proxy) = &config.proxies[0].protocol else { - panic!() - }; - let mut legs = vec![&proxy.security]; - if let Some(download) = proxy.download() { - legs.push(&download.security); - } - for security in legs { - assert!(matches!(security, SecurityConfig::Reality(r) if r.support_x25519mlkem768)); - for field in [ - "downgrade_port", - "hrr_port", - "ordinary_tls_port", - "tls12_port", - ] { - let port = original[field].as_u64().unwrap() as u16; - let stream = tokio::net::TcpStream::connect(( - original["node"]["server"].as_str().unwrap(), - port, - )) - .await - .unwrap(); - let client = SecurityClient::from_security(security).unwrap(); - let result = - tokio::time::timeout(Duration::from_secs(5), client.connect(Box::new(stream))) - .await - .expect("peer rejection must not be a timeout"); - assert!(result.is_err(), "required hybrid security accepted {field}"); - } - } - event(name, "PASS"); -} - -#[tokio::test] -#[ignore = "official isolated container peer required"] -async fn native_tcp_integrity_and_server_first() { - let name = "native_tcp_integrity_and_server_first"; - event(name, "BEGIN"); - let fixture = fixture(); - tokio::time::timeout(Duration::from_secs(120), async { - for family in ["ipv4", "ipv6", "domain"] { - let (mut control, target) = origin(&fixture, 10, family).await; - let outbound = node(&fixture, None); - let mut stream = outbound - .connect_stream(session(target), &EstablishContext::default()) - .await - .unwrap() - .io; - let mut first = [0; 5]; - stream.read_exact(&mut first).await.unwrap(); - assert_eq!(&first, b"hello"); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - let body = vec![0x5a; 10 * 1024 * 1024]; - stream.write_all(&body).await.unwrap(); - stream.flush().await.unwrap(); - let mut reply = vec![0; body.len() + 7]; - stream.read_exact(&mut reply).await.unwrap(); - assert_eq!(&reply[..body.len()], body); - assert_eq!(&reply[body.len()..], b"trailer"); - stream.shutdown().await.unwrap(); - drop(stream); - outbound.shutdown().await; - assert_eq!(control.read_u8().await.unwrap(), b'D'); - } - }) - .await - .unwrap(); - event(name, "PASS"); -} - -struct FixtureResolver(IpAddr); -#[async_trait::async_trait] -impl Resolver for FixtureResolver { - async fn resolve(&self, host: &str, port: u16) -> io::Result { - if host != "vcore-fixture.test" { - return Err(io::ErrorKind::NotFound.into()); - } - Ok(ResolvedEndpoint { - logical_host: host.into(), - port, - addresses: vec![SocketAddr::new(self.0, port)], - }) - } -} -#[tokio::test] -#[ignore = "official isolated container peer required"] -async fn native_three_udp_encodings() { - udp_encodings("native_three_udp_encodings", false).await; -} - -#[tokio::test] -#[ignore = "requires the owned container harness"] -async fn native_vision_xudp() { - udp_encodings("native_vision_xudp", true).await; -} - -async fn udp_encodings(name: &str, vision: bool) { - event(name, "BEGIN"); - let fixture = fixture(); - assert_eq!(fixture["vision_probe"].as_bool().unwrap_or(false), vision); - tokio::time::timeout(Duration::from_secs(120), async { - let resolution = ResolutionContext::measurement( - Arc::new(FixtureResolver( - fixture["origin_ipv4"].as_str().unwrap().parse().unwrap(), - )), - true, - ); - for encoding in [None, Some("none"), Some("packetaddr")] { - if vision && encoding.is_some() { - continue; - } - for family in ["ipv4", "ipv6", "domain"] { - let (mut control, target) = origin(&fixture, 4, family).await; - let outbound = node(&fixture, encoding); - // V2Ray's UDP response writer uses a 2048-byte buffer and - // needs room for the length prefix / packetaddr address. - // This fixture-specific cap does not reduce VCore's budget. - let cap = if fixture["peer_kind"] == "V2" { - if encoding.is_none() { - 2048 - } else if encoding == Some("packetaddr") { - 2027 - } else { - 2046 - } - } else { - 15000 - }; - let budget = DatagramBudget::new(cap, cap); - let request = DatagramRequest::new(DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )) - .with_budget(budget); - let mut io = outbound - .open_datagram( - request, - &EstablishContext::with_resolution( - Duration::from_secs(10), - resolution.clone(), - ), - ) - .await - .unwrap(); - for size in [1, 64, 512, 1200, cap as usize] { - for index in 0..100 { - let payload: Vec = (0..size).map(|i| (i + index) as u8).collect(); - io.send(Datagram { - remote: target.clone(), - payload: payload.clone().into(), - sniffed_domain: None, - }) - .await - .unwrap(); - let count = control.read_u16().await.unwrap() as usize; - let _source = control.read_u16().await.unwrap(); - assert_eq!(count, size); - let mut observed = vec![0; count]; - control.read_exact(&mut observed).await.unwrap(); - assert_eq!(observed, payload); - let reply = io.receive().await.unwrap(); - assert_eq!(reply.payload, payload); - assert_eq!(reply.remote.port(), target.port()); - } - } - assert!( - io.send(Datagram { - remote: target, - payload: vec![0; cap as usize + 1].into(), - sniffed_domain: None - }) - .await - .is_err() - ); - io.close().await.unwrap(); - outbound.shutdown().await; - } - } - }) - .await - .unwrap(); - event(name, "PASS"); -} diff --git a/tests/vless_public.rs b/tests/vless_public.rs deleted file mode 100644 index a673f87..0000000 --- a/tests/vless_public.rs +++ /dev/null @@ -1,871 +0,0 @@ -//! Public VLESS consumer gate. Every origin/peer is owned by the container runner. -#![cfg(all(feature = "ffi", feature = "outbound-vless", feature = "interop-test"))] -use serde_json::{Value, json}; -use sha2::{Digest, Sha256}; -use std::{ - ffi::{CStr, CString}, - io::{self, Read, Write}, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, TcpStream, UdpSocket}, - thread, - time::{Duration, Instant}, -}; -use vcore::resources::case_events::Case as RecordedCase; -// The same public consumers are reused by XHTTP. Keep their evidence separate -// instead of letting a current XHTTP run masquerade as historical VLESS evidence. -struct Case; -impl Case { - fn start(suite: &'static str, assertion: &'static str) -> RecordedCase { - let suite = if std::env::var("VCORE_PROTOCOL_STAGE").as_deref() == Ok("XHTTP") { - match suite { - "VLESS-PUBLIC" => "XHTTP-PUBLIC", - "VLESS-BASE" => "XHTTP-BASE", - "VLESS-LIFE" => "XHTTP-LIFE", - "VLESS-OWNED" => "XHTTP-OWNED", - _ => panic!("unknown public consumer suite"), - } - } else if std::env::var("VCORE_PROTOCOL_STAGE").as_deref() == Ok("HYSTERIA2") { - match suite { - "VLESS-PUBLIC" => "HYSTERIA2-PUBLIC", - "VLESS-BASE" => "HYSTERIA2-BASE", - "VLESS-LIFE" => "HYSTERIA2-LIFE", - "VLESS-OWNED" => "HYSTERIA2-OWNED", - _ => panic!("unknown public consumer suite"), - } - } else if std::env::var("VCORE_PROTOCOL_STAGE").as_deref() == Ok("SECURITY") { - match suite { - "VLESS-PUBLIC" => "SECURITY-PUBLIC", - "VLESS-BASE" => "SECURITY-BASE", - "VLESS-LIFE" => "SECURITY-LIFE", - "VLESS-OWNED" => "SECURITY-OWNED", - _ => panic!("unknown public consumer suite"), - } - } else if std::env::var("VCORE_PROTOCOL_STAGE").as_deref() == Ok("INTEGRATION") { - match suite { - "VLESS-PUBLIC" => "INTEGRATION-PUBLIC", - "VLESS-BASE" => "INTEGRATION-BASE", - "VLESS-LIFE" => "INTEGRATION-LIFE", - "VLESS-OWNED" => "INTEGRATION-OWNED", - _ => panic!("unknown public consumer suite"), - } - } else if std::env::var("VCORE_PROTOCOL_STAGE").as_deref() == Ok("HTTPUPGRADE") { - match suite { - "VLESS-PUBLIC" => "HTTPUPGRADE-PUBLIC", - "VLESS-BASE" => "HTTPUPGRADE-BASE", - "VLESS-LIFE" => "HTTPUPGRADE-LIFE", - "VLESS-OWNED" => "HTTPUPGRADE-OWNED", - _ => panic!("unknown public consumer suite"), - } - } else { - suite - }; - RecordedCase::new(suite, assertion) - } -} -const TIMEOUT: Duration = Duration::from_secs(10); -const DOMAIN: &str = "vcore-fixture.test"; - -#[cfg(all(feature = "outbound-vmess", feature = "outbound-trojan"))] -#[path = "vless_public/httpupgrade.rs"] -mod httpupgrade; - -#[cfg(feature = "outbound-hysteria2")] -#[path = "vless_public/hysteria2.rs"] -mod hysteria2; -#[path = "vless_public/integration/mod.rs"] -mod integration; -#[path = "vless_public/runtime.rs"] -mod runtime; -#[cfg(feature = "shadow-tls-v3")] -#[path = "vless_public/shadowtls.rs"] -mod shadowtls; -#[cfg(feature = "outbound-tuic")] -#[path = "vless_public/tuic.rs"] -mod tuic; -#[cfg(target_os = "macos")] -#[path = "vmess_public/tun.rs"] -mod tun; -#[cfg(feature = "outbound-shadowsocks")] -#[path = "vless_public/uot.rs"] -mod uot; - -fn fixture() -> Value { - let value: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_VLESS_INPUT").expect("use isolated VLESS runner")) - .unwrap(), - ) - .unwrap(); - assert_eq!(value["isolation"], "containers"); - value -} - -#[cfg(feature = "outbound-hysteria2")] -#[test] -#[ignore = "isolated HYSTERIA2 runner"] -fn hysteria2_client_first() { - let _case = RecordedCase::new("HYSTERIA2-BASE", "client_first"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - echo(port, &f); - core.stop(); -} - -#[cfg(feature = "outbound-hysteria2")] -#[test] -#[ignore = "isolated HYSTERIA2 runner"] -fn hysteria2_udp_base() { - let _case = RecordedCase::new("HYSTERIA2-BASE", "udp_ipv4_ipv6_domain_fragmentation"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - let mut association = Association::new(&f, port, ipv6, domain); - for size in [1, 64, 512, 1200, 4096] { - for sequence in 0..100_u8 { - let mut bytes = vec![sequence; size]; - if size > 1 { - bytes[1] = size as u8; - } - association.exchange(&bytes); - } - } - drop(association); - } - core.stop(); -} - -#[cfg(feature = "outbound-hysteria2")] -#[test] -#[ignore = "isolated HYSTERIA2 runner"] -fn hysteria2_tcp_base() { - let _case = RecordedCase::new("HYSTERIA2-BASE", "tcp_ipv4_ipv6_domain_and_measure"); - let f = fixture(); - assert_eq!(f["node"]["type"], "hysteria2"); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - bulk(port, &f, ipv6, domain); - } - echo(port, &f); - core.stop(); - let mut origin = Origin::new(&f, 14, false); - let result = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[f["node"]]}).to_string()], "timeout":5, - "url":format!("http://{}/",origin.target)}), - ); - assert_eq!(result["results"][0]["success"], true); - origin.marker(b'A'); - origin.marker(b'D'); -} -fn initialize(f: &Value) { - invoke("initialize", None, json!({"dataDir": f["data_dir"]})); -} -fn invoke(method: &str, id: Option<&str>, payload: Value) -> Value { - let response = invoke_response(method, id, payload); - assert_eq!( - response["success"], true, - "Invoke {method}: {}", - response["error"] - ); - response["data"].clone() -} -fn invoke_response(method: &str, id: Option<&str>, payload: Value) -> Value { - let mut input = - json!({"apiVersion":vcore::INVOKE_API_VERSION,"method":method,"payload":payload}); - if let Some(id) = id { - input["instanceId"] = json!(id); - } - let input = CString::new(input.to_string()).unwrap(); - // SAFETY: NUL-terminated input lives for the call; returned allocation is freed once. - let result = unsafe { vcore::ffi::VCoreInvoke(input.as_ptr()) }; - assert!(!result.is_null()); - let bytes = unsafe { CStr::from_ptr(result) }.to_bytes().to_vec(); - unsafe { vcore::ffi::VCoreFree(result) }; - serde_json::from_slice(&bytes).unwrap() -} -struct Core(Option); -impl Core { - fn prepare(config: &Value) -> Self { - invoke( - "validateConfig", - None, - json!({"configYaml":config.to_string()}), - ); - let id = invoke("createInstance", None, json!({}))["instanceId"] - .as_str() - .unwrap() - .to_owned(); - let core = Self(Some(id)); - invoke( - "prepare", - core.0.as_deref(), - json!({"configYaml":config.to_string()}), - ); - core - } - fn start(config: &Value) -> Self { - let core = Self::prepare(config); - invoke("start", core.0.as_deref(), json!({})); - core - } - fn stop(mut self) { - stop(self.0.take().unwrap()); - } -} -fn stop(id: String) { - let (tx, rx) = std::sync::mpsc::sync_channel(1); - let worker = thread::spawn(move || { - let result = std::panic::catch_unwind(|| { - invoke("stop", Some(&id), json!({})); - invoke("destroyInstance", Some(&id), json!({})); - }); - tx.send(result).unwrap(); - }); - let result = rx - .recv_timeout(Duration::from_secs(5)) - .expect("Stop exceeds 5 seconds"); - worker.join().unwrap(); - if let Err(panic) = result { - std::panic::resume_unwind(panic); - } -} -impl Drop for Core { - fn drop(&mut self) { - if let Some(id) = self.0.take() { - let _ = std::panic::catch_unwind(|| stop(id)); - } - } -} -fn free_port() -> u16 { - for _ in 0..32 { - let tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let port = tcp.local_addr().unwrap().port(); - if let (Ok(_v6), Ok(_udp), Ok(_udp6)) = ( - TcpListener::bind((Ipv6Addr::LOCALHOST, port)), - UdpSocket::bind((Ipv4Addr::LOCALHOST, port)), - UdpSocket::bind((Ipv6Addr::LOCALHOST, port)), - ) { - return port; - } - } - panic!("cannot reserve fixture port"); -} -fn socket(address: SocketAddr) -> TcpStream { - let io = TcpStream::connect_timeout(&address, TIMEOUT).unwrap(); - io.set_read_timeout(Some(TIMEOUT)).unwrap(); - io.set_write_timeout(Some(TIMEOUT)).unwrap(); - io.set_nodelay(true).unwrap(); - io -} -fn config(node: Value, port: u16) -> Value { - json!({"socks-port":port,"ipv6":true,"proxies":[node],"rules":["MATCH,peer"]}) -} -fn login(port: u16) -> TcpStream { - let mut client = socket((Ipv4Addr::LOCALHOST, port).into()); - client.write_all(&[5, 1, 0]).unwrap(); - let mut reply = [0; 2]; - client.read_exact(&mut reply).unwrap(); - assert_eq!(reply, [5, 0]); - client -} -fn address(target: SocketAddr, domain: bool) -> Vec { - let mut bytes = Vec::new(); - if domain { - bytes.extend([3, DOMAIN.len() as u8]); - bytes.extend(DOMAIN.as_bytes()); - } else { - match target.ip() { - IpAddr::V4(ip) => { - bytes.push(1); - bytes.extend(ip.octets()); - } - IpAddr::V6(ip) => { - bytes.push(4); - bytes.extend(ip.octets()); - } - } - } - bytes.extend(target.port().to_be_bytes()); - bytes -} -fn reply(client: &mut TcpStream) -> SocketAddr { - let mut header = [0; 4]; - client.read_exact(&mut header).unwrap(); - assert_eq!(&header[..3], &[5, 0, 0]); - let ip = match header[3] { - 1 => { - let mut bytes = [0; 4]; - client.read_exact(&mut bytes).unwrap(); - IpAddr::from(bytes) - } - 4 => { - let mut bytes = [0; 16]; - client.read_exact(&mut bytes).unwrap(); - IpAddr::from(bytes) - } - _ => panic!("SOCKS response address"), - }; - let mut port = [0; 2]; - client.read_exact(&mut port).unwrap(); - SocketAddr::new(ip, u16::from_be_bytes(port)) -} -fn connect(port: u16, target: SocketAddr, domain: bool) -> TcpStream { - connect_with_initial(port, target, domain, &[]) -} -fn connect_with_initial(port: u16, target: SocketAddr, domain: bool, initial: &[u8]) -> TcpStream { - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(target, domain)); - // Queue actual application bytes with CONNECT before the relay can poll - // its read side. Waiting for the SOCKS reply first is not a guaranteed - // nonempty first write at the SS adapter, even for a client-first origin. - request.extend(initial); - client.write_all(&request).unwrap(); - reply(&mut client); - client -} -struct Origin { - observer: TcpStream, - target: SocketAddr, -} -impl Origin { - fn new(f: &Value, mode: u8, ipv6: bool) -> Self { - let mut observer = socket(f["origin_control"].as_str().unwrap().parse().unwrap()); - observer - .write_all(&[mode | if ipv6 && mode >= 10 { 128 } else { 0 }]) - .unwrap(); - let mut port = [0; 2]; - observer.read_exact(&mut port).unwrap(); - let ip: IpAddr = f[if ipv6 { "origin_ipv6" } else { "origin_ipv4" }] - .as_str() - .unwrap() - .parse() - .unwrap(); - assert!(!ip.is_loopback() && !ip.is_unspecified()); - Self { - observer, - target: SocketAddr::new(ip, u16::from_be_bytes(port)), - } - } - fn marker(&mut self, expected: u8) { - let mut byte = [0]; - self.observer.read_exact(&mut byte).unwrap(); - assert_eq!(byte[0], expected); - } - fn quiet(&mut self) { - self.observer - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let error = self.observer.read(&mut [0; 1]).unwrap_err(); - assert!(matches!( - error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut - )); - self.observer.set_read_timeout(Some(TIMEOUT)).unwrap(); - } - fn udp(&mut self, expected: &[u8]) { - let mut header = [0; 4]; - self.observer.read_exact(&mut header).unwrap(); - assert_ne!(&header[2..], &[0, 0]); - let length = u16::from_be_bytes([header[0], header[1]]) as usize; - assert_eq!(length, expected.len()); - let mut bytes = vec![0; length]; - self.observer.read_exact(&mut bytes).unwrap(); - assert_eq!(bytes, expected); - } -} -struct Association { - control: TcpStream, - client: UdpSocket, - relay: SocketAddr, - origin: Origin, - domain: bool, -} -impl Association { - fn new(f: &Value, port: u16, ipv6: bool, domain: bool) -> Self { - let client = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - client.set_read_timeout(Some(TIMEOUT)).unwrap(); - socket2::SockRef::from(&client) - .set_send_buffer_size(65536) - .unwrap(); - let mut control = login(port); - let mut request = vec![5, 3, 0]; - request.extend(address(client.local_addr().unwrap(), false)); - control.write_all(&request).unwrap(); - let relay = reply(&mut control); - Self { - control, - client, - relay, - origin: Origin::new(f, if ipv6 { 6 } else { 4 }, ipv6), - domain, - } - } - fn packet(&self, payload: &[u8]) -> Vec { - let mut packet = vec![0, 0, 0]; - packet.extend(address(self.origin.target, self.domain)); - packet.extend(payload); - packet - } - fn exchange(&mut self, payload: &[u8]) { - let packet = self.packet(payload); - assert_eq!( - self.client.send_to(&packet, self.relay).unwrap(), - packet.len() - ); - self.origin.udp(payload); - let mut bytes = [0; 20000]; - let (size, source) = self.client.recv_from(&mut bytes).unwrap(); - assert_eq!(source, self.relay); - assert_eq!(&bytes[..3], &[0, 0, 0]); - let offset = match bytes[3] { - 1 => 8, - 4 => 20, - 3 => 5 + bytes[4] as usize, - _ => panic!("UDP response type"), - }; - assert_eq!( - &bytes[offset..offset + 2], - &self.origin.target.port().to_be_bytes() - ); - let literal = address(self.origin.target, false); - let named = address(self.origin.target, self.domain); - assert!(bytes[3..offset + 2] == literal || bytes[3..offset + 2] == named); - assert_eq!(&bytes[offset + 2..size], payload); - } -} -fn echo(port: u16, f: &Value) { - let mut origin = Origin::new(f, 13, false); - let mut client = connect_with_initial(port, origin.target, false, b"client-first"); - let mut bytes = [0; 12]; - client.read_exact(&mut bytes).unwrap(); - assert_eq!(&bytes, b"client-first"); - origin.marker(b'A'); - drop(client); - origin.marker(b'D'); -} -fn bulk(port: u16, f: &Value, ipv6: bool, domain: bool) { - bulk_start(port, f, ipv6, domain, true); -} -fn bulk_client_first(port: u16, f: &Value, ipv6: bool, domain: bool) { - bulk_start(port, f, ipv6, domain, false); -} -fn bulk_start(port: u16, f: &Value, ipv6: bool, domain: bool, server_first: bool) { - let _case = Case::start("VLESS-BASE", "tcp_10mib_both_directions"); - let mut origin = Origin::new(f, if server_first { 10 } else { 22 }, ipv6); - let data = vec![0x5a; 10 * 1024 * 1024]; - let initial_length = if server_first { 0 } else { 4093 }; - let mut client = connect_with_initial(port, origin.target, domain, &data[..initial_length]); - if server_first { - let mut hello = [0; 5]; - client.read_exact(&mut hello).unwrap(); - assert_eq!(&hello, b"hello"); - } - origin.marker(b'A'); - for fragment in data[initial_length..].chunks(4093) { - client.write_all(fragment).unwrap(); - } - let mut hash = Sha256::new(); - let mut remaining = data.len(); - let mut bytes = [0; 6151]; - while remaining > 0 { - let size = bytes.len().min(remaining); - let n = client.read(&mut bytes[..size]).unwrap_or_else(|error| { - panic!("bulk receive failed with {remaining} bytes remaining: {error}") - }); - assert_ne!(n, 0); - hash.update(&bytes[..n]); - remaining -= n; - } - assert_eq!(hash.finalize(), Sha256::digest(&data)); - let mut trailer = [0; 7]; - client.read_exact(&mut trailer).unwrap(); - assert_eq!(&trailer, b"trailer"); - client.shutdown(std::net::Shutdown::Write).unwrap(); - assert_eq!(client.read(&mut [0]).unwrap(), 0); - origin.marker(b'D'); -} -fn dns(config: &mut Value, origin: &Origin, via: &str) { - config["dns"] = - json!({"enable":true,"ipv6":false,"nameserver":[format!("udp://{}#{via}",origin.target)]}); -} -#[test] -#[ignore = "isolated VLESS runner"] -fn public_base() { - let _case = Case::start("VLESS-PUBLIC", "public_base"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let dns_origin = Origin::new(&f, 17, false); - let mut yaml = config(f["node"].clone(), port); - dns(&mut yaml, &dns_origin, "DIRECT"); - let core = Core::start(&yaml); - for (v6, domain) in [(false, false), (true, false), (false, true)] { - bulk(port, &f, v6, domain); - } - echo(port, &f); - core.stop(); - drop(dns_origin); - for codec in if f["node"]["flow"] == "xtls-rprx-vision" { - vec!["xudp"] - } else { - vec!["none", "xudp", "packetaddr"] - } { - let _codec = Case::start("VLESS-BASE", "udp_each_codec_and_family"); - // Each runtime owns a fresh DNS fixture. A previous TCP bulk transfer - // must not consume this container origin's bounded idle lifetime. - let mut dns_origin = Origin::new(&f, 17, false); - let mut node = f["node"].clone(); - node["packet-encoding"] = json!(codec); - let mut yaml = config(node, port); - dns(&mut yaml, &dns_origin, "DIRECT"); - let core = Core::start(&yaml); - // Keep source sockets alive across all three cases, preventing peer NAT - // source-tuple reuse from aliasing a previous association. - let mut associations = Vec::new(); - for (v6, domain) in [(false, false), (true, false), (false, true)] { - let mut association = Association::new(&f, port, v6, domain); - let cap = if f["peer_kind"] == "V2" { - if codec == "xudp" { - 2048 - } else { - 2046 - (if codec == "packetaddr" { - if v6 || domain { 19 } else { 7 } - } else { - 0 - }) - } - } else { - 15000 - - (if codec == "packetaddr" { - if v6 || domain { 19 } else { 7 } - } else { - 0 - }) - }; - for size in [1, 64, 512, 1200, cap] { - for sequence in 0..100u8 { - let payload = vec![sequence; size]; - association.exchange(&payload); - } - } - associations.push(association); - } - core.stop(); - drop(associations); - // packetaddr's domain lookup must use this controlled endpoint. Magic - // names close the fixture; there is no system resolver fallback. - if codec == "packetaddr" { - let mut header = [0; 4]; - dns_origin.observer.read_exact(&mut header).unwrap(); - let n = u16::from_be_bytes([header[0], header[1]]) as usize; - let mut query = vec![0; n]; - dns_origin.observer.read_exact(&mut query).unwrap(); - assert_eq!(&query[12..], b"\x0dvcore-fixture\x04test\0\0\x01\0\x01"); - } - } - let mut origin = Origin::new(&f, 14, false); - let result = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[f["node"]]}).to_string()],"timeout":5,"url":format!("http://{}/",origin.target)}), - ); - assert_eq!(result["results"][0]["success"], true); - origin.marker(b'A'); - origin.marker(b'D'); -} - -fn denied(node: Value, f: &Value) { - let mut origin = Origin::new(f, 13, false); - let port = free_port(); - let core = Core::start(&config(node, port)); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - request.extend(b"no-origin-bytes"); - client.write_all(&request).unwrap(); - client - .set_read_timeout(Some(Duration::from_millis(500))) - .unwrap(); - let mut response = [0; 4]; - if client.read_exact(&mut response).is_ok() && response[1] == 0 { - let mut tail = vec![0; if response[3] == 1 { 6 } else { 18 }]; - client.read_exact(&mut tail).unwrap(); - assert!( - !matches!(client.read(&mut [0;32]),Ok(n) if n>0), - "rejected session delivered business data" - ); - } - drop(client); - origin.quiet(); - core.stop(); -} - -#[test] -#[ignore = "isolated SECURITY JLS runner"] -fn public_jls_download_identity() { - let _case = Case::start("VLESS-PUBLIC", "public_jls_download_identity"); - let f = fixture(); - initialize(&f); - let original = f["node"].clone(); - assert!(original["xhttp-opts"]["download-settings"].is_object()); - let port = free_port(); - let core = Core::start(&config(original.clone(), port)); - echo(port, &f); - core.stop(); - let mut replaced = original.clone(); - replaced["xhttp-opts"]["download-settings"]["jls-opts"] = f["jls_download_credentials"].clone(); - replaced["xhttp-opts"]["download-settings"]["client-fingerprint"] = json!("firefox"); - let core = Core::start(&config(replaced.clone(), port)); - bulk(port, &f, false, false); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"independent-jls-download"); - core.stop(); - for field in ["username", "password"] { - let mut bad = replaced.clone(); - bad["xhttp-opts"]["download-settings"]["jls-opts"][field] = json!("incorrect-credential"); - denied(bad, &f); - } - // An explicit clear must remove JLS, even though the peer still requires it. - let mut cleared = original.clone(); - cleared["xhttp-opts"]["download-settings"]["jls-opts"] = json!({}); - denied(cleared, &f); - // A failed independently authenticated leg cannot poison a new valid node. - let core = Core::start(&config(original, port)); - echo(port, &f); - core.stop(); -} - -#[test] -#[ignore = "isolated SECURITY ECH runner"] -fn public_ech_download_identity() { - let _case = Case::start("VLESS-PUBLIC", "public_ech_download_identity"); - let f = fixture(); - initialize(&f); - let original = f["node"].clone(); - assert!(original["xhttp-opts"]["download-settings"].is_object()); - let port = free_port(); - let core = Core::start(&config(original.clone(), port)); - echo(port, &f); - core.stop(); - let mut replaced = original.clone(); - replaced["xhttp-opts"]["download-settings"]["ech-opts"] = - json!({"enable":true,"config":f["ech_download_config"]}); - replaced["xhttp-opts"]["download-settings"]["client-fingerprint"] = json!("firefox"); - let core = Core::start(&config(replaced.clone(), port)); - bulk(port, &f, false, false); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"independent-ech-download"); - core.stop(); - // Only an explicit clear permits the download leg to use ordinary TLS. - replaced["xhttp-opts"]["download-settings"]["ech-opts"] = json!({}); - let core = Core::start(&config(replaced, port)); - echo(port, &f); - core.stop(); - let core = Core::start(&config(original, port)); - echo(port, &f); - core.stop(); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_negative() { - let _case = Case::start("VLESS-PUBLIC", "public_negative"); - let f = fixture(); - initialize(&f); - let original = f["node"].clone(); - let port = free_port(); - let core = Core::start(&config(original.clone(), port)); - echo(port, &f); - core.stop(); - let mut bad = original.clone(); - bad["uuid"] = json!("08080808-0808-0808-0808-080808080808"); - denied(bad, &f); - if original["tls"] == true && original["network"].as_str().unwrap_or("tcp") == "tcp" { - let mut bad = original.clone(); - if original["flow"] == "xtls-rprx-vision" { - bad["port"] = json!(f["flow_reject_port"].as_u64().unwrap()); - denied(bad, &f); - // The official listener accepts an explicitly empty flow for a - // Vision user. This is a user choice, never automatic fallback. - let mut plain = original.clone(); - plain["flow"] = json!(""); - let port = free_port(); - let core = Core::start(&config(plain, port)); - echo(port, &f); - core.stop(); - } else { - bad["flow"] = json!("xtls-rprx-vision"); - denied(bad, &f); - } - } - if original.get("reality-opts").is_some() { - let mut bad = original.clone(); - bad["reality-opts"]["public-key"] = json!("CQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - denied(bad, &f); - let mut bad = original.clone(); - bad["reality-opts"]["short-id"] = json!("ffffffffffffffff"); - denied(bad, &f); - let mut bad = original.clone(); - bad["servername"] = json!("rejected.fixture.test"); - denied(bad, &f); - } - if original["tls"] == true && original.get("reality-opts").is_none() { - let mut untrusted = original.clone(); - untrusted.as_object_mut().unwrap().remove("fingerprint"); - denied(untrusted.clone(), &f); - let mut bad = original.clone(); - bad["fingerprint"] = json!("00".repeat(32)); - bad["skip-cert-verify"] = json!(true); - denied(bad, &f); - let mut bad = original.clone(); - bad["servername"] = json!("wrong.invalid"); - denied(bad.clone(), &f); - bad["skip-cert-verify"] = json!(true); - denied(bad, &f); - if original["network"] == "ws" { - let mut fallback = original.clone(); - fallback.as_object_mut().unwrap().remove("servername"); - let port = free_port(); - let core = Core::start(&config(fallback, port)); - echo(port, &f); - core.stop(); - } - // No pin: skip-cert-verify is observable, not silently ignored. - untrusted["skip-cert-verify"] = json!(true); - let port = free_port(); - let core = Core::start(&config(untrusted, port)); - echo(port, &f); - core.stop(); - // A matching leaf pin intentionally replaces PKI/name checks, per the - // existing StandardTlsClient contract. A nonmatching pin cannot bypass. - } - let mut bad = original.clone(); - match original["network"].as_str().unwrap_or("tcp") { - "ws" => bad["ws-opts"]["path"] = json!("/wrong"), - "grpc" => bad["grpc-opts"]["grpc-service-name"] = json!("wrong"), - "h2" => bad["h2-opts"]["host"] = json!(["wrong.invalid"]), - "http" => bad["http-opts"]["path"] = json!(["/wrong"]), - "xhttp" => bad["xhttp-opts"]["path"] = json!("/wrong"), - _ => return, - }; - denied(bad, &f); - if original["network"] == "h2" { - let mut bad = original; - bad["h2-opts"]["path"] = json!("/wrong"); - denied(bad, &f); - } -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_tls_identity_and_verification_name() { - let _case = Case::start("VLESS-PUBLIC", "public_tls_identity_and_verification_name"); - let f = fixture(); - initialize(&f); - let original = f["node"].clone(); - let port = free_port(); - let core = Core::start(&config(original.clone(), port)); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"valid-client-identity"); - core.stop(); - // The CA pin checks certificate names; SNI can differ independently. - let mut named = original.clone(); - named["servername"] = json!("independent-sni.invalid"); - named["name-cert-verify"] = json!("localhost"); - let core = Core::start(&config(named, port)); - echo(port, &f); - core.stop(); - let mut bad = original.clone(); - bad["name-cert-verify"] = json!("wrong.invalid"); - bad["skip-cert-verify"] = json!(true); - denied(bad, &f); - for name in ["missing", "wrong-ca", "expired"] { - let mut bad = original.clone(); - bad.as_object_mut().unwrap().remove("certificate"); - bad.as_object_mut().unwrap().remove("private-key"); - if name != "missing" { - bad.as_object_mut() - .unwrap() - .extend(f["identities"][name].as_object().unwrap().clone()); - } - denied(bad, &f); - } - // New node/client after bad identities cannot inherit another node's TLS - // session or mTLS credentials, and a failure does not poison valid identity. - let core = Core::start(&config(original, port)); - echo(port, &f); - core.stop(); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_alpn_rejection() { - let _case = Case::start("VLESS-PUBLIC", "public_alpn_rejection"); - let f = fixture(); - initialize(&f); - // The official listener advertises h2 when its gRPC entrance is enabled. - // A valid gRPC control proves authentication/origin readiness first. - let mut control = f["node"].clone(); - control["network"] = json!("grpc"); - control["alpn"] = json!(["h2"]); - control.as_object_mut().unwrap().remove("ws-opts"); - control["grpc-opts"] = json!({"grpc-service-name":"vless-alpn"}); - let port = free_port(); - let core = Core::start(&config(control, port)); - echo(port, &f); - core.stop(); - denied(f["node"].clone(), &f); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_udp_first_response() { - let _case = Case::start("VLESS-PUBLIC", "public_udp_first_response"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - let mut association = Association::new(&f, port, false, false); - association.exchange(&vec![1; 15000]); - core.stop(); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_legacy_regression() { - let _case = Case::start("VLESS-PUBLIC", "public_legacy_regression"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - echo(port, &f); - bulk(port, &f, false, false); - let mut udp = Association::new(&f, port, false, false); - for size in [1, 64, 512, 1200, 8192] { - for n in 0..100u8 { - udp.exchange(&vec![n; size]); - } - } - core.stop(); -} - -#[test] -#[ignore = "owned isolated native peer required"] -fn public_legacy_tcp() { - let _case = Case::start("VLESS-PUBLIC", "public_legacy_tcp"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - echo(port, &f); - // The gate is the legacy TLS transport, not a new UDP size contract for V2Ray. - bulk(port, &f, false, false); - core.stop(); -} diff --git a/tests/vless_public/httpupgrade.rs b/tests/vless_public/httpupgrade.rs deleted file mode 100644 index 64faa6f..0000000 --- a/tests/vless_public/httpupgrade.rs +++ /dev/null @@ -1,247 +0,0 @@ -//! Shared public consumers; every protocol peer and origin is container-owned. -use super::*; -use vcore::resources::observation::ResourceProbe; - -fn save(value: Value) { - std::fs::write( - std::env::var("VCORE_HTTPUPGRADE_OBSERVATIONS").unwrap(), - value.to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "isolated HTTPUpgrade peer and origin required"] -fn tcp() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("HTTPUPGRADE-PUBLIC", "tcp"); - let probe = ResourceProbe::default(); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - bulk(port, &f, ipv6, domain); - } - echo(port, &f); - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - save( - json!({"families":["ipv4","ipv6","domain"],"tcp_bytes_each_family_each_direction":10*1024*1024,"server_first":true,"client_first":true,"stop_idle":true}), - ); -} - -fn no_udp(association: &mut Association, payload: &[u8]) { - association - .client - .send_to(&association.packet(payload), association.relay) - .unwrap(); - association - .client - .set_read_timeout(Some(Duration::from_millis(150))) - .unwrap(); - assert!( - matches!(association.client.recv_from(&mut [0;64]), Err(e) if matches!(e.kind(),io::ErrorKind::WouldBlock|io::ErrorKind::TimedOut)) - ); - association.origin.quiet(); - association.client.set_read_timeout(Some(TIMEOUT)).unwrap(); -} - -#[test] -#[ignore = "isolated HTTPUpgrade UDP peer and origin required"] -fn udp() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("HTTPUPGRADE-PUBLIC", "udp"); - let vmess = f["node"]["type"] == "vmess"; - let native = f["peer_kind"] == "XR"; - let codec = if vmess { - f["node"]["packet-encoding"].as_str().unwrap() - } else { - "trojan" - }; - let probe = ResourceProbe::default(); - let port = free_port(); - let mut dns_origin = Origin::new(&f, 17, false); - let mut yaml = config(f["node"].clone(), port); - dns(&mut yaml, &dns_origin, "DIRECT"); - let core = probe.scope_sync(|| Core::start(&yaml)); - let mut live = Vec::new(); - let mut rows = Vec::new(); - for (family, ipv6, domain) in [ - ("ipv4", false, false), - ("ipv6", true, false), - ("domain", false, true), - ] { - if !f["families"] - .as_array() - .unwrap() - .iter() - .any(|v| v == family) - { - continue; - } - if vmess || native { - let mut empty = Association::new(&f, port, ipv6, domain); - no_udp(&mut empty, b""); - live.push(empty); - } - let mut association = Association::new(&f, port, ipv6, domain); - let cap = if vmess { - 15000 - - if codec == "packetaddr" { - if ipv6 || domain { 19 } else { 7 } - } else { - 0 - } - } else if native { - 8166 // Native reply's 8192 total-frame budget minus this domain header. - } else { - 8192 - }; - let mut sizes = vec![1, 64, 512, 1200, cap]; - if !vmess && !native { - sizes.insert(0, 0); - } - for &size in &sizes { - for sequence in 0..100_u8 { - association.exchange(&vec![sequence; size]); - } - } - if native { - // Upload fits VCore, but exceeds the native reply frame. The - // incomplete reply must not become a successful shortened UDP. - let mut overflow = Association::new(&f, port, ipv6, domain); - let payload = vec![7; cap + 1]; - overflow - .client - .send_to(&overflow.packet(&payload), overflow.relay) - .unwrap(); - overflow.origin.udp(&payload); - overflow - .client - .set_read_timeout(Some(Duration::from_millis(150))) - .unwrap(); - assert!( - matches!(overflow.client.recv_from(&mut [0;20000]),Err(e) if matches!(e.kind(),io::ErrorKind::WouldBlock|io::ErrorKind::TimedOut)) - ); - no_udp(&mut association, &vec![7; 8193]); - live.push(overflow); - } else { - no_udp(&mut association, &vec![7; cap + 1]); - } - rows.push(json!({"family":family,"sizes":sizes,"packets":sizes.len()*100})); - live.push(association); - } - if codec == "packetaddr" { - let mut header = [0; 4]; - dns_origin.observer.read_exact(&mut header).unwrap(); - let mut question = vec![0; u16::from_be_bytes([header[0], header[1]]) as usize]; - dns_origin.observer.read_exact(&mut question).unwrap(); - assert_eq!(&question[12..], b"\x0dvcore-fixture\x04test\0\0\x01\0\x01"); - } - echo(port, &f); - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - for association in &mut live { - association.origin.quiet(); - } - save( - json!({"udp":rows,"zero_no_delivery":vmess || native,"oversize_rejected":true,"tcp_sibling":true,"stop_idle":true}), - ); -} - -#[test] -#[ignore = "isolated HTTPUpgrade peer and origin required"] -fn echo_and_stop() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("HTTPUPGRADE-PUBLIC", "echo_and_stop"); - let probe = ResourceProbe::default(); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - echo(port, &f); - let (mut client, mut origin) = runtime::live(port, &f); - runtime::exchange(&mut client, b"before-stop"); - let start = Instant::now(); - core.stop(); - assert!(start.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - runtime::assert_closed(&mut client); - origin.marker(b'D'); - origin.quiet(); - save(json!({"authenticated_echo":true,"live_stream_cancelled":true,"stop_idle":true})); -} - -#[test] -#[ignore = "isolated HTTPUpgrade negative fixture required"] -fn rejected() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("HTTPUPGRADE-PUBLIC", "rejected"); - let probe = ResourceProbe::default(); - probe.scope_sync(|| denied(f["node"].clone(), &f)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - save(json!({"origin_connected":false,"received_business_bytes":0,"stop_idle":true})); -} - -#[tokio::test] -#[ignore = "isolated HTTPUpgrade peer and official close reference required"] -async fn close() { - use std::sync::Arc; - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint}, - outbound::{ - EstablishContext, OutboundConnector, UpstreamPath, trojan::TrojanOutbound, - vmess::VmessOutbound, - }, - session::{InboundKind, StreamSession}, - }; - let f = fixture(); - let _case = RecordedCase::new("HTTPUPGRADE-PUBLIC", "close"); - let parsed = - Config::parse_yaml(config(f["node"].clone(), 1080).to_string().as_bytes()).unwrap(); - let proxy = &parsed.proxies[0]; - let probe = ResourceProbe::default(); - let mut origin = Origin::new(&f, 11, false); - let tail = probe.scope(async { - let path = UpstreamPath::direct(ResolvedEndpoint { - logical_host: proxy.address().into(), port: proxy.port(), - addresses: vec![SocketAddr::new(proxy.address().parse().unwrap(), proxy.port())], - }, Dialer::default()); - let outbound: Arc = match &proxy.protocol { - ProxyProtocol::Vmess(c) => Arc::new(VmessOutbound::new_with_path(c,path).unwrap()), - ProxyProtocol::Trojan(c) => Arc::new(TrojanOutbound::new_with_path(c,path).unwrap()), - _ => unreachable!(), - }; - let mut io = outbound.connect_stream(StreamSession { - inbound: InboundKind::InternalMeasure, source:"127.0.0.1:1".parse().unwrap(), - destination: origin.target.into(), sniffed_domain:None, - }, &EstablishContext::default()).await.unwrap().io; - let tail = tokio::time::timeout(TIMEOUT, async { - let mut hello=[0;5]; io.read_exact(&mut hello).await.unwrap(); assert_eq!(&hello,b"hello"); - io.write_all(b"ping").await.unwrap(); io.flush().await.unwrap(); - let mut echo=[0;4]; io.read_exact(&mut echo).await.unwrap(); assert_eq!(&echo,b"ping"); - io.shutdown().await.unwrap(); - let mut tail=Vec::new(); - let terminal=io.read_to_end(&mut tail).await; - assert!(terminal.is_ok() || matches!(terminal,Err(ref e) if matches!(e.kind(),io::ErrorKind::ConnectionReset|io::ErrorKind::UnexpectedEof|io::ErrorKind::BrokenPipe))); - tail - }).await.expect("close did not terminate"); - drop(io); - tokio::time::timeout(Duration::from_secs(5),outbound.shutdown()).await.unwrap(); - tail - }).await; - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - origin.marker(b'A'); - origin.marker(b'D'); - let hex: String = tail.iter().map(|b| format!("{b:02x}")).collect(); - assert_eq!(f["close_reference"]["terminated"], true); - assert_eq!(hex, f["close_reference"]["tail_hex"].as_str().unwrap()); - save(json!({"tail_hex":hex,"terminated":true,"stop_idle":true})); -} diff --git a/tests/vless_public/hysteria2.rs b/tests/vless_public/hysteria2.rs deleted file mode 100644 index 5e79618..0000000 --- a/tests/vless_public/hysteria2.rs +++ /dev/null @@ -1,195 +0,0 @@ -//! Hysteria2 consumers through public YAML/Invoke and real container peers. -use super::*; -use runtime::{assert_closed, exchange, live, select}; - -#[test] -#[ignore = "isolated HYSTERIA2 runner"] -fn public_concrete_upstream() { - let _case = RecordedCase::new("HYSTERIA2-PUBLIC", "public_concrete_upstream"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let mut node = f["node"].clone(); - node["dialer-proxy"] = json!("hop"); - let mut yaml = config(node, port); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["hop"].clone()); - let core = Core::start(&yaml); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"upstream-udp"); - core.stop(); -} - -#[test] -#[ignore = "isolated HYSTERIA2 runner"] -fn public_udp_boundaries() { - let _case = RecordedCase::new("HYSTERIA2-PUBLIC", "public_udp_boundaries"); - let f = fixture(); - initialize(&f); - let port = free_port(); - for mtu in [64, 1197, 65535] { - let mut node = f["node"].clone(); - node["udp-mtu"] = json!(mtu); - let core = Core::start(&config(node, port)); - let mut first = Association::new(&f, port, false, false); - let mut second = Association::new(&f, port, true, false); - for n in 0..100 { - first.exchange(&[1, n]); - second.exchange(&[2, n]); - } - first.exchange(&vec![1; 4096]); - // Wire format: eight fixed bytes, one-byte authority length, then - // the authority. A tiny MTU and long IPv6 target can exhaust the - // protocol's 255-fragment count before the 4096-byte business cap. - let second_max = ((mtu - 9 - second.origin.target.to_string().len()) * 255).min(4096); - second.exchange(&vec![2; second_max]); - first - .client - .send_to(&first.packet(&vec![3; 4097]), first.relay) - .unwrap(); - first.origin.quiet(); - second.exchange(b"oversize-does-not-kill-sibling"); - let rogue = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - rogue - .send_to(&first.packet(b"unauthorized"), first.relay) - .unwrap(); - first.origin.quiet(); - let mut other = Origin::new(&f, 4, false); - let mut packet = vec![0, 0, 0]; - packet.extend(address(other.target, false)); - packet.extend(b"other-target"); - second.client.send_to(&packet, second.relay).unwrap(); - other.udp(b"other-target"); - let mut response = [0; 128]; - let (n, _) = second.client.recv_from(&mut response).unwrap(); - assert!(response[..n].ends_with(b"other-target")); - second.origin.quiet(); - // A local send error may close its SOCKS association, not siblings or - // the authenticated QUIC session. Check this association last. - second - .client - .send_to(&second.packet(&vec![3; second_max + 1]), second.relay) - .unwrap(); - second.origin.quiet(); - let mut third = Association::new(&f, port, false, false); - third.exchange(b"shared-session-survives"); - core.stop(); - } -} - -fn denied(port: u16, f: &Value, label: &str) { - let mut unused = Origin::new(f, 13, false); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(unused.target, false)); - client.write_all(&request).unwrap(); - let mut reply = [0; 10]; - client.read_exact(&mut reply).unwrap(); - assert_ne!(reply[1], 0, "{label}"); - unused.quiet(); -} - -#[test] -#[ignore = "isolated HYSTERIA2 runner"] -fn public_graph_and_hop_snapshot() { - let _case = RecordedCase::new("HYSTERIA2-PUBLIC", "public_graph_and_hop_snapshot"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let controller = free_port(); - let mut node = f["node"].clone(); - node["dialer-proxy"] = json!("hop"); - node["server"] = json!("peer.fixture.test"); - let mut yaml = config(node.clone(), port); - let mut dns_origin = Origin::new(&f, 19, false); - dns_origin - .observer - .write_all( - &f["node"]["server"] - .as_str() - .unwrap() - .parse::() - .unwrap() - .octets(), - ) - .unwrap(); - dns(&mut yaml, &dns_origin, "DIRECT"); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["hop"].clone()); - let core = Core::start(&yaml); - echo(port, &f); - let mut udp = Association::new(&f, port, false, true); - udp.exchange(b"concrete-upstream"); - core.stop(); - drop(udp); - - node["server"] = f["node"]["server"].clone(); - // A one-port set still creates new physical sockets. It tests selection - // freezing independently of the native multi-port DNAT acceptance matrix. - node["ports"] = json!(f["node"]["port"].as_u64().unwrap().to_string()); - node["hop-interval"] = json!(5); - node["dialer-proxy"] = json!("outer"); - yaml["proxies"][0] = node; - yaml["external-controller"] = json!(format!("127.0.0.1:{controller}")); - yaml["secret"] = json!("fixture-only"); - yaml["proxy-groups"] = json!([ - {"name":"outer","type":"select","proxies":["inner"]}, - {"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]} - ]); - let core = Core::start(&yaml); - let (mut old, mut origin) = live(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"before-switch"); - select(controller, "REJECT"); - let until = Instant::now() + Duration::from_secs(12); - while Instant::now() < until { - exchange(&mut old, b"same-authenticated-session"); - udp.exchange(b"same-upstream-snapshot"); - thread::sleep(Duration::from_millis(100)); - } - // New logical streams can reuse the old authenticated session, including - // after its physical socket has hopped. They are not a new group choice. - echo(port, &f); - core.stop(); - assert_closed(&mut old); - origin.marker(b'D'); - drop((old, origin, udp)); - - // A fresh physical session must honor the newly selected REJECT. - let core = Core::start(&yaml); - select(controller, "REJECT"); - denied(port, &f, "fresh session observes REJECT"); - select(controller, "DIRECT"); - echo(port, &f); - core.stop(); - // Unselected cycles are rejected before creating any socket. - yaml["proxy-groups"][1]["proxies"] = json!(["DIRECT", "peer"]); - assert_eq!( - invoke_response( - "validateConfig", - None, - json!({"configYaml":yaml.to_string()}) - )["success"], - false - ); - // A broken selected member must not fall back to DIRECT. - yaml["proxy-groups"][1]["proxies"] = json!(["hop", "DIRECT"]); - let mut broken = f["hop"].clone(); - broken["port"] = json!(1); - yaml["proxies"][1] = broken; - let core = Core::start(&yaml); - denied(port, &f, "broken member has no DIRECT fallback"); - core.stop(); - // The business UDP flag is not the raw upstream carrier capability. - let mut hop = f["hop"].clone(); - hop["udp"] = json!(false); - yaml["proxies"][1] = hop; - let core = Core::start(&yaml); - echo(port, &f); - core.stop(); -} diff --git a/tests/vless_public/integration/connector.rs b/tests/vless_public/integration/connector.rs deleted file mode 100644 index 66a4752..0000000 --- a/tests/vless_public/integration/connector.rs +++ /dev/null @@ -1,387 +0,0 @@ -use super::*; -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, -}; -use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint, SocketProtector}, - dispatch::{DatagramBudget, DatagramTransport, DispatchError}, - outbound::{ - AnyTlsOutbound, ConnectedStream, DatagramRequest, EstablishContext, OutboundConnector, - ShadowsocksOutbound, Socks5Outbound, UpstreamPath, VlessOutbound, - hysteria2::Hysteria2Outbound, trojan::TrojanOutbound, vmess::VmessOutbound, - }, - security::{SecurityContext, StandardTlsClient, TlsCertificatePolicy, TlsClientOptions}, - session::{Datagram, DatagramSession, Destination, InboundKind, StreamSession}, -}; - -pub(super) fn node( - raw: &Value, - upstream: Option>, - dialer: Dialer, -) -> Arc { - let mut raw = raw.clone(); - raw["name"] = json!("peer"); - raw.as_object_mut().unwrap().remove("dialer-proxy"); - let parsed = Config::parse_yaml(config(raw, 1080).to_string().as_bytes()).unwrap(); - let proxy = &parsed.proxies[0]; - let path = upstream.map(UpstreamPath::proxy).unwrap_or_else(|| { - UpstreamPath::direct( - ResolvedEndpoint { - logical_host: proxy.address().into(), - port: proxy.port(), - addresses: vec![SocketAddr::new( - proxy.address().parse().unwrap(), - proxy.port(), - )], - }, - dialer, - ) - }); - match &proxy.protocol { - ProxyProtocol::Socks5(c) => Arc::new(Socks5Outbound::new_with_path(c, path).unwrap()), - ProxyProtocol::Shadowsocks(c) => { - Arc::new(ShadowsocksOutbound::new_with_path(c, path).unwrap()) - } - ProxyProtocol::Trojan(c) => Arc::new(TrojanOutbound::new_with_path(c, path).unwrap()), - ProxyProtocol::Vmess(c) => Arc::new(VmessOutbound::new_with_path(c, path).unwrap()), - ProxyProtocol::Vless(c) => Arc::new(VlessOutbound::new_with_path(c, path).unwrap()), - ProxyProtocol::Hysteria2(c) => Arc::new(Hysteria2Outbound::new_with_path(c, path).unwrap()), - ProxyProtocol::Tuic(c) => { - #[cfg(feature = "outbound-tuic")] - { - Arc::new(vcore::outbound::tuic::TuicOutbound::new_with_path(c, path).unwrap()) - } - #[cfg(not(feature = "outbound-tuic"))] - { - let _ = c; - panic!("TUIC is not compiled"); - } - } - ProxyProtocol::AnyTls(c) => { - let tls = StandardTlsClient::with_options( - &SecurityContext::new(), - &c.server_name, - TlsClientOptions { - alpn: c.tls.alpn.clone(), - client_fingerprint: c.tls.client_fingerprint, - certificate: TlsCertificatePolicy { - verification_name: None, - skip_cert_verify: c.tls.skip_cert_verify, - fingerprint: c.tls.fingerprint, - }, - ..Default::default() - }, - 0, - 65536, - ) - .unwrap(); - Arc::new( - AnyTlsOutbound::new( - vcore::outbound::server_destination(&c.address, c.port).unwrap(), - path, - &c.password, - Arc::new(tls), - 65536, - ) - .unwrap(), - ) - } - } -} - -fn request() -> DatagramRequest { - DatagramRequest::new(DatagramSession::new( - InboundKind::Socks5, - "127.0.0.1:1".parse().unwrap(), - )) -} -fn session(destination: Destination) -> StreamSession { - StreamSession { - inbound: InboundKind::Socks5, - source: "127.0.0.1:1".parse().unwrap(), - destination, - sniffed_domain: None, - } -} - -pub(super) fn budget_pair(f: &Value) -> Value { - let mut case = RecordedCase::new("INTEGRATION-PAIR", "directional_budget"); - let probe = ResourceProbe::default(); - let runtime = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - let result = runtime.block_on(probe.scope(async { - let first = node(&f["first"],None,Dialer::default()); - let last = node(&f["last"],Some(first.clone()),Dialer::default()); - let mut observations = Vec::new(); - for (transmit,receive) in [(128,128),(512,256)] { - let mut udp = last.open_datagram(request().with_budget(DatagramBudget::new(transmit,receive)),&EstablishContext::default()).await.unwrap(); - let mut origin = Origin::new(f,4,false); - let remote = Destination::Ip(origin.target); - let budget = udp.payload_budget(&remote); - assert_eq!(budget,DatagramBudget::new(transmit,receive)); - let packet = |size| Datagram { remote:remote.clone(),payload:vec![7;size].into(),sniffed_domain:None }; - assert!(udp.send(packet(usize::from(transmit)+1)).await.is_err()); - origin.quiet(); - let size = usize::from(transmit.min(receive)); - for _ in 0..100 { - udp.send(packet(size)).await.unwrap(); - let reply = tokio::time::timeout(TIMEOUT,udp.receive()).await.unwrap().unwrap(); - assert_eq!(reply.remote,remote); - assert_eq!(reply.payload,vec![7;size]); - origin.udp(&vec![7;size]); - } - udp.close().await.unwrap(); - observations.push(json!({"transmit":transmit,"receive":receive,"roundtrip_size":size,"packets":100,"oversize_rejected_before_origin":true})); - } - last.begin_shutdown(); first.begin_shutdown(); - last.shutdown().await; first.shutdown().await; - drop((last,first)); - observations - })); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - case.resources(probe.snapshot()); - json!(result) -} - -struct RejectProtect(AtomicUsize); - -struct NoDatagrams { - inner: Arc, - rejected: AtomicUsize, -} -#[async_trait::async_trait] -impl OutboundConnector for NoDatagrams { - async fn connect_stream( - &self, - session: StreamSession, - context: &EstablishContext, - ) -> Result { - self.inner.connect_stream(session, context).await - } - async fn open_datagram( - &self, - _: DatagramRequest, - _: &EstablishContext, - ) -> Result, DispatchError> { - self.rejected.fetch_add(1, Ordering::SeqCst); - Err(DispatchError::NotAllowed) - } -} - -pub(super) fn carrier_pair(f: &Value) -> Value { - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - let mut case = RecordedCase::new("INTEGRATION-PAIR", "carrier_capability"); - let probe = ResourceProbe::default(); - let runtime = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - let kind = f["last"]["type"].as_str().unwrap(); - let tcp_allowed = !matches!(kind, "hysteria2" | "tuic"); - let udp_allowed = !matches!(kind, "socks5" | "ss" | "hysteria2" | "tuic"); - runtime.block_on(probe.scope(async { - let first = node(&f["first"], None, Dialer::default()); - let controlled = Arc::new(NoDatagrams { - inner: first.clone(), - rejected: AtomicUsize::new(0), - }); - let last = node(&f["last"], Some(controlled.clone()), Dialer::default()); - let mut origin = Origin::new(f, 13, false); - let stream = last - .connect_stream(session(origin.target.into()), &EstablishContext::default()) - .await; - if tcp_allowed { - let mut stream = stream.unwrap().io; - stream.write_all(b"tcp-carrier").await.unwrap(); - // AsyncWrite may buffer (notably VMess); this direct caller must - // flush before waiting for the peer, just like the public relay. - stream.flush().await.unwrap(); - let mut bytes = [0; 11]; - stream.read_exact(&mut bytes).await.unwrap(); - assert_eq!(&bytes, b"tcp-carrier"); - origin.marker(b'A'); - drop(stream); - } else { - assert!(stream.is_err()); - origin.quiet(); - } - let mut origin = Origin::new(f, 4, false); - let datagram = last - .open_datagram(request(), &EstablishContext::default()) - .await; - if udp_allowed { - let mut datagram = datagram.unwrap(); - datagram - .send(Datagram { - remote: origin.target.into(), - payload: bytes::Bytes::from_static(b"stream-carried-udp"), - sniffed_domain: None, - }) - .await - .unwrap(); - let response = tokio::time::timeout(TIMEOUT, datagram.receive()) - .await - .unwrap() - .unwrap(); - assert_eq!(&response.payload[..], b"stream-carried-udp"); - origin.udp(b"stream-carried-udp"); - datagram.close().await.unwrap(); - } else { - assert!(datagram.is_err()); - origin.quiet(); - } - let rejected = controlled.rejected.load(Ordering::SeqCst); - assert_eq!( - rejected, - usize::from(!tcp_allowed) + usize::from(!udp_allowed) - ); - last.begin_shutdown(); - first.begin_shutdown(); - last.shutdown().await; - first.shutdown().await; - drop((last, controlled, first)); - })); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - case.resources(probe.snapshot()); - json!({"upstream_datagrams":"rejected","tcp_allowed":tcp_allowed,"udp_allowed":udp_allowed,"no_bypass":true}) -} -impl SocketProtector for RejectProtect { - fn protect(&self, _: i32) -> io::Result<()> { - self.0.fetch_add(1, Ordering::SeqCst); - Err(io::ErrorKind::PermissionDenied.into()) - } -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn protect_failure() { - use base64::{Engine as _, engine::general_purpose::STANDARD}; - let f = fixture(); - initialize(&f); - let runtime = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - for protocol in PROTOCOLS { - let mut case = RecordedCase::new("INTEGRATION-FAILURES", protocol); - let probe = ResourceProbe::default(); - let mut origin = Origin::new(&f, 13, false); - runtime.block_on(probe.scope(async { - let protector = Arc::new(RejectProtect(AtomicUsize::new(0))); - let outbound = node( - &f["nodes"][protocol], - None, - Dialer::default().with_protector(protector.clone()), - ); - let rejected = outbound - .connect_stream(session(origin.target.into()), &EstablishContext::default()) - .await; - assert!(rejected.is_err()); - assert!(protector.0.load(Ordering::SeqCst) > 0); - origin.quiet(); - outbound.begin_shutdown(); - outbound.shutdown().await; - drop(outbound); - })); - assert!(probe.snapshot().is_idle()); - case.resources(probe.snapshot()); - { - let _case = RecordedCase::new("INTEGRATION-FAILURES-AUTH", protocol); - let mut bad = f["nodes"][protocol].clone(); - if matches!(protocol, "vmess" | "vless") { - bad["uuid"] = json!("08080808-0808-0808-0808-080808080808"); - } else if protocol == "ss" { - bad["password"] = json!(STANDARD.encode([23; 16])); - } else { - bad["password"] = json!("invalid-synthetic-identity"); - } - shadowsocks::rejected(bad, &f); - if !matches!(protocol, "socks5" | "ss") { - let mut bad = f["nodes"][protocol].clone(); - bad["fingerprint"] = json!("00".repeat(32)); - bad["skip-cert-verify"] = json!(true); - shadowsocks::rejected(bad, &f); - } - } - { - let mut case = RecordedCase::new("INTEGRATION-FAILURES-SOURCE", protocol); - let port = free_port(); - let probe = ResourceProbe::default(); - if protocol == "tuic" { - runtime.block_on(probe.scope(transmit_budget(&f["nodes"][protocol], &f))); - assert!(probe.snapshot().is_idle()); - } - let core = - probe.scope_sync(|| Core::start(&config(f["nodes"][protocol].clone(), port))); - let mut first = Association::new(&f, port, false, false); - let mut second = Association::new(&f, port, false, false); - first.exchange(b"first-association"); - second.exchange(b"sibling-association"); - let rogue = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - rogue - .send_to(&first.packet(b"unauthorized-source"), first.relay) - .unwrap(); - first.origin.quiet(); - // Retain the older codecs' wire-limit negative. TUIC fragments - // this otherwise valid payload: 65245 is the SOCKS reply limit, - // not a universal transmit cap. Its explicit budget is tested above. - if protocol != "tuic" { - first - .client - .send_to(&first.packet(&vec![0; 65246]), first.relay) - .unwrap(); - first.origin.quiet(); - } - second.exchange(b"survives-foreign-and-oversize"); - drop(first); - second.exchange(b"survives-single-association-cancel"); - core.stop(); - runtime::assert_closed(&mut second.control); - assert!(probe.snapshot().is_idle()); - case.resources(probe.snapshot()); - } - } - observe( - json!({"protect_rejected":PROTOCOLS,"auth_rejected":PROTOCOLS,"certificate_pin_rejected":["anytls","trojan","vmess","vless","hysteria2","tuic"],"source_and_oversize_isolated":PROTOCOLS,"tuic_transmit_budget":{"limit":128,"rejected":129,"following_roundtrip":128},"cancel_preserves_sibling":PROTOCOLS,"no_origin_bytes":true,"stop_idle":true}), - ); -} - -async fn transmit_budget(raw: &Value, f: &Value) { - let outbound = node(raw, None, Dialer::default()); - let mut udp = outbound - .open_datagram( - request().with_budget(DatagramBudget::new(128, 128)), - &EstablishContext::default(), - ) - .await - .unwrap(); - let mut origin = Origin::new(f, 4, false); - let target = origin.target; - let packet = |size| Datagram { - remote: target.into(), - payload: vec![7; size].into(), - sniffed_domain: None, - }; - assert_eq!( - udp.payload_budget(&origin.target.into()), - DatagramBudget::new(128, 128) - ); - assert!(udp.send(packet(129)).await.is_err()); - origin.quiet(); - udp.send(packet(128)).await.unwrap(); - let response = tokio::time::timeout(TIMEOUT, udp.receive()) - .await - .unwrap() - .unwrap(); - assert_eq!(response.remote, origin.target.into()); - assert_eq!(&response.payload[..], &[7; 128]); - origin.udp(&[7; 128]); - udp.close().await.unwrap(); - drop(udp); - outbound.begin_shutdown(); - outbound.shutdown().await; -} diff --git a/tests/vless_public/integration/lifecycle.rs b/tests/vless_public/integration/lifecycle.rs deleted file mode 100644 index 086d547..0000000 --- a/tests/vless_public/integration/lifecycle.rs +++ /dev/null @@ -1,194 +0,0 @@ -use super::*; - -const GROUPS: [&str; 12] = [ - "socks5", - "anytls", - "ss", - "trojan", - "vmess", - "vless", - "hysteria2", - "tuic", - "ss-v3", - "ss-uot", - "ss-uot-v3", - "httpupgrade", -]; - -pub(super) fn fd_count() -> usize { - std::fs::read_dir("/dev/fd").unwrap().count() -} - -fn quiet_reader(stream: &mut TcpStream) { - stream.set_nonblocking(true).unwrap(); - let result = stream.read(&mut [0; 8]); - assert!( - matches!(result, Err(error) if error.kind() == io::ErrorKind::WouldBlock), - "origin emitted data after Stop" - ); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required; at least 500 seconds"] -fn public_lifetimes() { - lifetimes(100); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required; development subset only"] -fn lifecycle_tracer() { - lifetimes(20); -} - -fn lifetimes(count: usize) { - let f = fixture(); - initialize(&f); - for group in GROUPS { - let protocol = f["profile_groups"][group][0].as_str().unwrap(); - let port = free_port(); - let core = Core::start(&config(f["nodes"][protocol].clone(), port)); - echo(port, &f); - core.stop(); - } - let mut observations = Vec::new(); - for cycle in 0..count { - let group = GROUPS[(cycle / 5) % GROUPS.len()]; - let options = f["profile_groups"][group].as_array().unwrap(); - let protocol = options[(cycle / (5 * GROUPS.len())) % options.len()] - .as_str() - .unwrap(); - let mode = cycle % 5; - let mut case = RecordedCase::new("INTEGRATION-LIFECYCLE", "stop_and_remain_quiet"); - let probe = ResourceProbe::default(); - case.checkpoint("baseline", probe.snapshot()); - let baseline = fd_count(); - let port = free_port(); - let mut tcp = Vec::new(); - let mut udp = Vec::new(); - let mut blackhole = None; - let mut pending_client = None; - let mut occupied = None; - let mut node = f["nodes"][protocol].clone(); - if mode == 2 { - let origin = Origin::new( - &f, - if matches!(group, "hysteria2" | "tuic") { - 20 - } else { - 15 - }, - false, - ); - node["server"] = json!(origin.target.ip().to_string()); - node["port"] = json!(origin.target.port()); - blackhole = Some(origin); - } - let yaml = config(node, port); - let core = probe.scope_sync(|| { - if mode == 3 { - occupied = Some(UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap()); - let core = Core::prepare(&yaml); - assert_eq!( - invoke_response("start", core.0.as_deref(), json!({}))["success"], - false - ); - core - } else { - Core::start(&yaml) - } - }); - match mode { - 0 => echo(port, &f), - 2 => { - let origin = blackhole.as_mut().unwrap(); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - client.write_all(&request).unwrap(); - origin.marker(b'A'); - pending_client = Some(client); - } - 3 => {} - _ => { - let (first, mut first_origin) = runtime::live(port, &f); - let (mut second, second_origin) = runtime::live(port, &f); - let mut one = Association::new(&f, port, false, false); - let mut two = Association::new(&f, port, false, false); - one.exchange(b"first-association"); - two.exchange(b"second-association"); - if mode == 4 { - drop(first); - first_origin.marker(b'D'); - runtime::exchange(&mut second, b"sibling-survives-cancel"); - one.exchange(b"udp-sibling-one"); - two.exchange(b"udp-sibling-two"); - } else { - tcp.push((first, first_origin)); - } - tcp.push((second, second_origin)); - udp.extend([one, two]); - } - } - let active = probe.snapshot(); - case.checkpoint("active", active.clone()); - let stopping = Instant::now(); - core.stop(); - let stop_ms = stopping.elapsed().as_millis(); - assert!(stop_ms < 5000); - let stopped = probe.snapshot(); - assert!(stopped.is_idle(), "cycle {cycle}: {stopped:?}"); - case.checkpoint("after-stop", stopped.clone()); - drop(occupied); - drop(pending_client); - for (client, origin) in &mut tcp { - runtime::assert_closed(client); - origin.marker(b'D'); - } - for association in &mut udp { - runtime::assert_closed(&mut association.control); - association.client.set_nonblocking(true).unwrap(); - } - if let Some(origin) = &mut blackhole - && !matches!(group, "hysteria2" | "tuic") - { - origin.marker(b'D'); - } - let retained = 2 * tcp.len() + 3 * udp.len() + usize::from(blackhole.is_some()); - let after_stop = fd_count(); - assert!( - after_stop <= baseline + retained, - "cycle {cycle}: FD retained at Stop: {after_stop} > {}", - baseline + retained - ); - let rebound_tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let rebound_udp = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let quiet = Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert_eq!(probe.snapshot(), stopped, "resource activity after Stop"); - assert!(fd_count() <= baseline + retained + 2); - for (_, origin) in &mut tcp { - quiet_reader(&mut origin.observer); - } - for association in &mut udp { - assert_eq!( - association.client.recv(&mut [0; 32]).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - quiet_reader(&mut association.origin.observer); - } - if let Some(origin) = &mut blackhole { - quiet_reader(&mut origin.observer); - } - thread::sleep(Duration::from_millis(25)); - } - let quiet_seconds = quiet.elapsed().as_secs_f64(); - case.checkpoint("quiet", probe.snapshot()); - case.resources(probe.snapshot()); - drop((tcp, udp, blackhole, rebound_tcp, rebound_udp)); - let end = fd_count(); - assert!(end <= baseline); - observations.push(json!({"cycle":cycle,"group":group,"profile":protocol,"mode":mode,"baseline_fd":baseline,"retained_fixture_fd":retained,"after_stop_fd":after_stop,"final_fd":end,"stop_ms":stop_ms,"quiet_seconds":quiet_seconds,"active":active,"after_stop":stopped,"quiet":probe.snapshot(),"ports_rebound":true})); - println!("INTEGRATION lifecycle: {}/{count}", cycle + 1); - } - observe(json!({"count":count,"cycles":observations})); -} diff --git a/tests/vless_public/integration/mod.rs b/tests/vless_public/integration/mod.rs deleted file mode 100644 index 95c6059..0000000 --- a/tests/vless_public/integration/mod.rs +++ /dev/null @@ -1,402 +0,0 @@ -//! INTEGRATION consumers share the established public Invoke/SOCKS/origin boundaries. -use super::*; -use vcore::resources::observation::{QueueKind, ResourceKind, ResourceProbe}; - -mod connector; -mod lifecycle; -#[cfg(target_os = "macos")] -mod pressure; -mod shadowsocks; - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn resource_tracer() { - let f = fixture(); - initialize(&f); - for protocol in PROTOCOLS { - let mut case = RecordedCase::new("INTEGRATION-RESOURCE-TRACER", protocol); - let probe = ResourceProbe::default(); - case.checkpoint("baseline", probe.snapshot()); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["nodes"][protocol].clone(), port))); - let (mut tcp, mut origin) = runtime::live(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"observable"); - let active = probe.snapshot(); - assert!( - active.current(ResourceKind::Socket) > 0, - "missing Invoke resource scope: {protocol}" - ); - assert!( - active.current(ResourceKind::Task) > 0, - "runtime tasks are not observed: {protocol}" - ); - let queues = probe.queues(); - let udp_queue = &queues[QueueKind::SocksUdp as usize]; - assert_eq!(udp_queue.capacity, 16); - assert!((1..=16).contains(&udp_queue.peak)); - if matches!(protocol, "hysteria2" | "tuic") { - let inactive = if protocol == "tuic" { - QueueKind::Hysteria2Udp - } else { - QueueKind::TuicUdp - }; - for (index, queue) in queues.iter().enumerate() { - if index == inactive as usize { - assert_eq!((queue.capacity, queue.peak), (0, 0)); - } else { - assert!(queue.peak > 0 && queue.peak <= queue.capacity); - } - } - } - case.checkpoint("active", active); - core.stop(); - assert!( - probe.snapshot().is_idle(), - "{protocol}: {:?}", - probe.snapshot() - ); - case.checkpoint("after-stop", probe.snapshot()); - runtime::assert_closed(&mut tcp); - origin.marker(b'D'); - runtime::assert_closed(&mut udp.control); - case.resources(probe.snapshot()); - } - observe(json!({"protocols":PROTOCOLS,"active_observed":true,"stop_idle":true})); -} - -const PROTOCOLS: [&str; 8] = [ - "socks5", - "anytls", - "ss", - "trojan", - "vmess", - "vless", - "hysteria2", - "tuic", -]; - -fn observe(value: Value) { - std::fs::write( - std::env::var("VCORE_INTEGRATION_OBSERVATIONS").unwrap(), - value.to_string(), - ) - .unwrap(); -} - -fn denied_target(port: u16, f: &Value) { - let mut origin = Origin::new(f, 13, false); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - client.write_all(&request).unwrap(); - let mut response = [0; 10]; - client.read_exact(&mut response).unwrap(); - assert_ne!(response[1], 0); - origin.quiet(); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn graph() { - let f = fixture(); - initialize(&f); - for protocol in PROTOCOLS { - let _case = RecordedCase::new("INTEGRATION-GRAPH", protocol); - let port = free_port(); - let controller = free_port(); - let mut node = f["nodes"][protocol].clone(); - node["dialer-proxy"] = json!("outer"); - let mut yaml = json!({"socks-port":port,"external-controller":format!("127.0.0.1:{controller}"),"secret":"fixture-only","proxies":[node,f["hop"]],"proxy-groups":[{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]}],"rules":["MATCH,peer"]}); - let core = Core::start(&yaml); - let (mut old, mut origin) = runtime::live(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"before-group-switch"); - runtime::select(controller, "REJECT"); - runtime::exchange(&mut old, b"old-transport-snapshot"); - udp.exchange(b"old-udp-snapshot"); - if matches!(protocol, "hysteria2" | "tuic") { - echo(port, &f); // Reuse is allowed only on the old physical session. - } else { - // AnyTLS reuses idle sessions, not a session with an active stream; - // both its TCP and UoT sessions are deliberately held active here. - denied_target(port, &f); - } - runtime::select(controller, "DIRECT"); - echo(port, &f); - runtime::exchange(&mut old, b"still-old-transport"); - core.stop(); - runtime::assert_closed(&mut old); - origin.marker(b'D'); - drop((old, origin, udp)); - // No pool exists in a fresh session: REJECT must deny every protocol. - yaml["proxy-groups"][1]["proxies"] = json!(["REJECT", "hop", "DIRECT"]); - let core = Core::start(&yaml); - denied_target(port, &f); - core.stop(); - yaml["proxy-groups"][1]["proxies"] = json!(["DIRECT", "peer"]); - assert_eq!( - invoke_response( - "validateConfig", - None, - json!({"configYaml":yaml.to_string()}) - )["success"], - false - ); - yaml["proxy-groups"][1]["proxies"] = json!(["hop", "DIRECT"]); - yaml["proxies"][1]["port"] = json!(1); - let core = Core::start(&yaml); - denied_target(port, &f); - core.stop(); - } - observe( - json!({"protocols":PROTOCOLS,"nested_select":true,"old_transport_snapshot":true,"cold_reject":true,"direct":true,"unselected_cycle":true,"no_fallback":true}), - ); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn dns_measure() { - let f = fixture(); - initialize(&f); - for protocol in PROTOCOLS { - let _case = RecordedCase::new("INTEGRATION-DNS-MEASURE", protocol); - let node = f["nodes"][protocol].clone(); - let port = free_port(); - let mut dns_origin = Origin::new(&f, 17, false); - let mut yaml = config(node.clone(), port); - dns(&mut yaml, &dns_origin, "peer"); - yaml["proxy-groups"] = json!([{"name":"denied","type":"select","proxies":["REJECT"]}]); - yaml["rules"] = json!([ - format!("IP-CIDR,{}/32,peer", f["origin_ipv4"].as_str().unwrap()), - "MATCH,denied" - ]); - let core = Core::start(&yaml); - let mut origin = Origin::new(&f, 13, false); - let payload = b"controlled-dns-route"; - let mut client = connect_with_initial(port, origin.target, true, payload); - let mut response = vec![0; payload.len()]; - client.read_exact(&mut response).unwrap(); - assert_eq!(&response, payload); - origin.marker(b'A'); - let mut header = [0; 4]; - dns_origin.observer.read_exact(&mut header).unwrap(); - let mut query = vec![0; u16::from_be_bytes([header[0], header[1]]) as usize]; - dns_origin.observer.read_exact(&mut query).unwrap(); - assert_eq!(&query[12..], b"\x0dvcore-fixture\x04test\0\0\x01\0\x01"); - core.stop(); - runtime::assert_closed(&mut client); - origin.marker(b'D'); - drop((client, origin, dns_origin)); - let mut origin = Origin::new(&f, 14, false); - let result = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[node]}).to_string()],"timeout":5,"url":format!("http://{}/",origin.target)}), - ); - assert_eq!(result["results"][0]["success"], true); - origin.marker(b'A'); - origin.marker(b'D'); - let rejected = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[node],"proxy-groups":[{"name":"group","type":"select","proxies":["peer"]}]}).to_string()],"timeout":5,"url":format!("http://{}/",origin.target)}), - ); - assert_eq!(rejected["results"][0]["success"], false); - origin.quiet(); - } - observe( - json!({"protocols":PROTOCOLS,"controlled_dns_via_proxy":true,"ip_rule_consumer":true,"node_measure":true,"group_measure_rejected":true}), - ); -} - -#[test] -#[cfg(target_os = "macos")] -#[ignore = "owned INTEGRATION container fixture required"] -fn entrypoints() { - let f = fixture(); - let mut observations = Vec::new(); - for protocol in PROTOCOLS { - let _case = RecordedCase::new("INTEGRATION-ENTRYPOINTS", protocol); - let mut single = f.clone(); - single["node"] = f["nodes"][protocol].clone(); - runtime::entrypoints(&single); - observations.push(json!({"protocol":protocol,"http_forward":true,"http_connect":true,"tun_tcp":true,"tun_udp":true,"tun_dns":true})); - } - std::fs::write( - std::env::var("VCORE_INTEGRATION_OBSERVATIONS").unwrap(), - json!({"entrypoints":observations}).to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn ss_bulk_stress() { - let _case = RecordedCase::new("INTEGRATION-SS", "bulk_stress"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let mut first = f["first"].clone(); - let mut last = f["last"].clone(); - assert_eq!(first["type"], "ss"); - assert_eq!(last["type"], "ss"); - first["name"] = json!("hop"); - last["dialer-proxy"] = json!("hop"); - let core = - Core::start(&json!({"socks-port":port,"proxies":[last,first],"rules":["MATCH,peer"]})); - for _ in 0..50 { - bulk_client_first(port, &f, false, false); - } - core.stop(); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn carrier_tracer() { - let f = fixture(); - initialize(&f); - observe(connector::carrier_pair(&f)); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn ordered_pair() { - let _case = RecordedCase::new("INTEGRATION-PAIR", "ordered_pair"); - let f = fixture(); - initialize(&f); - if let Some(variant) = f.get("variant") { - assert_eq!(f["first"]["udp-relay-mode"], variant["udp_mode"]); - assert_eq!(f["last"]["cipher"], variant["cipher"]); - assert_eq!(f["last"]["plugin"], "shadow-tls"); - assert_eq!(f["last"]["plugin-opts"]["version"], 3); - assert!(f["last"].get("udp-over-tcp").is_none()); - } - let mut observations = Vec::new(); - for outer_ipv6 in [false, true] { - for grouped in [false, true] { - let port = free_port(); - let mut first = f["first"].clone(); - let mut last = f["last"].clone(); - first["name"] = json!("hop"); - last["name"] = json!("peer"); - if outer_ipv6 { - first["server"] = f["first_ipv6"].clone(); - last["server"] = f["last_ipv6"].clone(); - } - last["dialer-proxy"] = json!(if grouped { "outer" } else { "hop" }); - let mut yaml = json!({"socks-port":port,"proxies":[last,first],"rules":["MATCH,peer"]}); - if grouped { - yaml["proxy-groups"] = json!([ - {"name":"outer","type":"select","proxies":["inner"]}, - {"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]} - ]); - } - let core = Core::start(&yaml); - let server_first = f["last"]["type"] != "ss"; - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - bulk_start(port, &f, ipv6, domain, server_first); - } - echo(port, &f); - // Keep source tuples alive until Stop. Trojan's documented Mihomo - // domain-UDP gap is separately exercised with the official Xray - // terminal below, retaining this very same first-hop protocol. - let mut associations = Vec::new(); - let targets = if f["last"]["type"] == "trojan" { - vec![(false, false), (true, false)] - } else { - vec![(false, false), (true, false), (false, true)] - }; - let target_count = targets.len(); - for (ipv6, domain) in targets { - let mut association = Association::new(&f, port, ipv6, domain); - for size in [1, 64, 512, 1200] { - for sequence in 0..100_u8 { - association.exchange(&vec![sequence; size]); - } - } - associations.push(association); - } - core.stop(); - drop(associations); - let _tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let _udp = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let mut path = json!({ - "outer_ipv6":outer_ipv6,"nested_select":grouped, - "tcp_target_kinds":3,"tcp_bytes_each_direction":3*10*1024*1024, - "udp_target_kinds":target_count,"udp_packets":target_count*400,"udp_sizes":[1,64,512,1200], - "server_first":server_first,"client_first":true,"ports_rebound":true - }); - if !server_first { - path["server_first_limitation"] = json!("official-ss2022-empty-first-write"); - } - observations.push(path); - } - } - let mut value = json!({"first":f["first"]["type"],"last":f["last"]["type"],"paths":observations,"domain_native":domain_terminal(&f),"budgets":connector::budget_pair(&f),"routed_udp":routed_udp_pair(&f),"carrier_capability":connector::carrier_pair(&f)}); - if let Some(variant) = f.get("variant") { - value["variant"] = variant.clone(); - } - observe(value); -} - -fn routed_udp_pair(f: &Value) -> Value { - let _case = RecordedCase::new("INTEGRATION-PAIR", "routed_udp_permission"); - for enabled in [true, false] { - let port = free_port(); - let mut first = f["first"].clone(); - first["name"] = json!("hop"); - first["udp"] = json!(false); - let mut last = f["last"].clone(); - last["dialer-proxy"] = json!("hop"); - last["udp"] = json!(enabled); - let core = - Core::start(&json!({"socks-port":port,"proxies":[last,first],"rules":["MATCH,peer"]})); - echo(port, f); - let mut udp = Association::new(f, port, false, false); - if enabled { - udp.exchange(b"carrier-is-not-routed-business-udp"); - } else { - let packet = udp.packet(b"routed-udp-denied"); - udp.client.send_to(&packet, udp.relay).unwrap(); - udp.origin.quiet(); - } - core.stop(); - } - json!({"upstream_business_udp_disabled":true,"carrier_tcp":true,"carrier_udp":true,"leaf_business_udp_rejected":true}) -} - -fn domain_terminal(f: &Value) -> Vec { - if f["last"]["type"] != "trojan" { - return vec![]; - } - let _case = RecordedCase::new("INTEGRATION-PAIR", "native_domain_terminal"); - let mut observations = Vec::new(); - for outer_ipv6 in [false, true] { - for grouped in [false, true] { - let port = free_port(); - let mut first = f["first"].clone(); - let mut last = f["domain_last"].clone(); - first["name"] = json!("hop"); - if outer_ipv6 { - first["server"] = f["first_ipv6"].clone(); - last["server"] = f["domain_last_ipv6"].clone(); - } - last["dialer-proxy"] = json!(if grouped { "outer" } else { "hop" }); - let mut yaml = json!({"socks-port":port,"proxies":[last,first],"rules":["MATCH,peer"]}); - if grouped { - yaml["proxy-groups"] = json!([{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop"]}]); - } - let core = Core::start(&yaml); - let mut udp = Association::new(f, port, false, true); - for size in [1, 64, 512, 1200] { - for sequence in 0..100_u8 { - udp.exchange(&vec![sequence; size]); - } - } - core.stop(); - observations.push(json!({"outer_ipv6":outer_ipv6,"nested_select":grouped,"peer":"XR","udp_packets":400,"target":"domain"})); - } - } - observations -} diff --git a/tests/vless_public/integration/pressure.rs b/tests/vless_public/integration/pressure.rs deleted file mode 100644 index c4a0529..0000000 --- a/tests/vless_public/integration/pressure.rs +++ /dev/null @@ -1,385 +0,0 @@ -use super::*; - -const TCP_GROUPS: [&str; 4] = ["socks5", "ss-v3", "tuic", "httpupgrade"]; -const UDP_GROUPS: [&str; 4] = ["ss-uot", "ss-uot-v3", "tuic", "httpupgrade"]; - -struct Flows { - tcp: Vec<(TcpStream, Origin)>, - udp: Vec, - setup_us: Vec, - profiles: Value, -} - -impl Flows { - fn open(f: &Value, port: u16, controller: u16, generation: usize) -> Self { - let mut flows = Self { - tcp: Vec::with_capacity(20), - udp: Vec::with_capacity(20), - setup_us: Vec::with_capacity(40), - profiles: json!({"tcp":[],"udp":[]}), - }; - for (tcp_group, udp_group) in TCP_GROUPS.into_iter().zip(UDP_GROUPS) { - for index in 0..5 { - let profile = |group| { - let options = f["profile_groups"][group].as_array().unwrap(); - options[(generation + index) % options.len()] - .as_str() - .unwrap() - }; - let tcp_profile = profile(tcp_group); - runtime::select(controller, tcp_profile); - let start = Instant::now(); - flows.tcp.push(runtime::live(port, f)); - flows.setup_us.push(start.elapsed().as_micros() as u64); - flows.profiles["tcp"] - .as_array_mut() - .unwrap() - .push(json!(tcp_profile)); - let udp_profile = profile(udp_group); - runtime::select(controller, udp_profile); - let start = Instant::now(); - let mut udp = Association::new(f, port, false, false); - udp.exchange(&payload(generation, flows.udp.len(), 0, true)); - flows.udp.push(udp); - flows.setup_us.push(start.elapsed().as_micros() as u64); - flows.profiles["udp"] - .as_array_mut() - .unwrap() - .push(json!(udp_profile)); - } - } - assert_eq!((flows.tcp.len(), flows.udp.len()), (20, 20)); - flows - } - - fn exchange(&mut self, generation: usize, wave: usize) { - for (index, ((tcp, _), udp)) in self.tcp.iter_mut().zip(&mut self.udp).enumerate() { - runtime::exchange(tcp, &payload(generation, index, wave, false)); - udp.exchange(&payload(generation, index, wave, true)); - } - } - - fn assert_tcp_closed(&mut self) { - for (tcp, _) in &mut self.tcp { - runtime::assert_closed(tcp); - } - } -} - -fn payload(generation: usize, index: usize, wave: usize, udp: bool) -> Vec { - let mut bytes = vec![u8::from(udp); 257]; - for (slot, value) in bytes - .as_chunks_mut::<8>() - .0 - .iter_mut() - .zip([generation, index, wave]) - { - slot.copy_from_slice(&(value as u64).to_be_bytes()); - } - bytes -} - -fn close_peer(f: &Value) { - let mut client = socket(f["peer_controller"].as_str().unwrap().parse().unwrap()); - client.write_all(b"DELETE /connections HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer synthetic-integration-control\r\nConnection: close\r\n\r\n").unwrap(); - let mut response = Vec::new(); - client.read_to_end(&mut response).unwrap(); - assert!(response.starts_with(b"HTTP/1.1 204")); -} - -fn start(f: &Value, probe: &ResourceProbe) -> (Core, u16, u16) { - let port = free_port(); - let controller = free_port(); - let mut members = Vec::new(); - for group in TCP_GROUPS.into_iter().chain(UDP_GROUPS) { - for profile in f["profile_groups"][group].as_array().unwrap() { - let name = profile.as_str().unwrap(); - if !members.contains(&name) { - members.push(name); - } - } - } - let nodes: Vec<_> = members - .iter() - .map(|protocol| { - let mut node = f["nodes"][protocol].clone(); - node["name"] = json!(protocol); - node - }) - .collect(); - let yaml = json!({"socks-port":port,"external-controller":format!("127.0.0.1:{controller}"),"secret":"fixture-only","proxies":nodes,"proxy-groups":[{"name":"inner","type":"select","proxies":members}],"rules":["MATCH,inner"]}); - (probe.scope_sync(|| Core::start(&yaml)), port, controller) -} - -fn sample(probe: &ResourceProbe) -> Value { - json!({"heap_in_use":heap_in_use(),"rss_kib":rss_kib(),"fd":lifecycle::fd_count(),"resources":probe.snapshot(),"queues":probe.queues()}) -} - -fn rss_kib() -> u64 { - let output = std::process::Command::new("/bin/ps") - .args(["-o", "rss=", "-p", &std::process::id().to_string()]) - .output() - .unwrap(); - assert!(output.status.success()); - std::str::from_utf8(&output.stdout) - .unwrap() - .trim() - .parse() - .unwrap() -} - -fn heap_in_use() -> usize { - #[repr(C)] - #[derive(Default)] - struct Statistics { - blocks_in_use: std::ffi::c_uint, - size_in_use: usize, - max_size_in_use: usize, - size_allocated: usize, - } - unsafe extern "C" { - fn malloc_zone_statistics(zone: *mut std::ffi::c_void, statistics: *mut Statistics); - } - let mut statistics = Statistics::default(); - // SAFETY: macOS SDK C layout; NULL requests aggregate allocator statistics. - unsafe { malloc_zone_statistics(std::ptr::null_mut(), &mut statistics) }; - statistics.size_in_use -} - -fn stop_quiet( - core: Core, - probe: &ResourceProbe, - port: u16, - controller: u16, - baseline: usize, -) -> Value { - let start = Instant::now(); - core.stop(); - let stop_ms = start.elapsed().as_millis(); - let resources = probe.snapshot(); - assert!(stop_ms < 5000 && resources.is_idle()); - let after_fd = lifecycle::fd_count(); - assert!( - after_fd <= baseline, - "FD baseline {baseline}, after Stop {after_fd}" - ); - let after_stop = sample(probe); - let rebound = ( - TcpListener::bind((Ipv4Addr::LOCALHOST, port)).unwrap(), - UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(), - TcpListener::bind((Ipv4Addr::LOCALHOST, controller)).unwrap(), - ); - let quiet = Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert_eq!(probe.snapshot(), resources); - assert!(lifecycle::fd_count() <= baseline + 3); - thread::sleep(Duration::from_millis(25)); - } - drop(rebound); - json!({"stop_ms":stop_ms,"after_stop":after_stop,"quiet_seconds":quiet.elapsed().as_secs_f64(),"quiet":sample(probe),"ports_rebound":true}) -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required"] -fn rebuild() { - run_rebuild(100); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required; development subset only"] -fn rebuild_tracer() { - run_rebuild(3); -} - -fn idle_baseline() -> (usize, usize) { - let cold = lifecycle::fd_count(); - // Tokio's process-global Unix signal pair survives individual runtimes. - // Initialize ONLY an empty IO driver before freezing the baseline; never - // warm away leaked protocol resources or grant post-Stop cleanup grace. - drop( - tokio::runtime::Builder::new_current_thread() - .enable_io() - .enable_time() - .build() - .unwrap(), - ); - let idle = lifecycle::fd_count(); - assert!(idle >= cold && idle - cold <= 2); - (cold, idle) -} - -fn run_rebuild(count: usize) { - let f = fixture(); - initialize(&f); - let probe = ResourceProbe::default(); - let (cold_fd, baseline) = idle_baseline(); - let mut case = RecordedCase::new("INTEGRATION-REBUILD", "forty_flows_same_session"); - case.checkpoint("baseline", probe.snapshot()); - let (core, port, controller) = start(&f, &probe); - let mut cycles = Vec::new(); - for generation in 0..count { - let mut flows = Flows::open(&f, port, controller, generation); - flows.exchange(generation, 1); - let active = sample(&probe); - close_peer(&f); - flows.assert_tcp_closed(); - let setup = flows.setup_us.clone(); - let profiles = flows.profiles.clone(); - drop(flows); - cycles.push(json!({"generation":generation,"tcp":20,"udp":20,"per_group_tcp":5,"per_group_udp":5,"flow_profiles":profiles,"active":active,"setup_us":setup,"fault":"owned-peer-connections-closed","new_clients":true,"after_clients":sample(&probe)})); - println!("INTEGRATION rebuild: {}/{count}", generation + 1); - } - let stopped = stop_quiet(core, &probe, port, controller, baseline); - case.resources(probe.snapshot()); - observe( - json!({"count":count,"same_running_session":true,"tcp_groups":TCP_GROUPS,"udp_groups":UDP_GROUPS,"cold_fd":cold_fd,"baseline_fd":baseline,"cycles":cycles,"stopped":stopped}), - ); -} - -fn median(values: impl Iterator) -> u64 { - let mut values: Vec<_> = values.collect(); - values.sort_unstable(); - assert!(!values.is_empty()); - (values[(values.len() - 1) / 2] + values[values.len() / 2]) / 2 -} - -fn append_sample(value: &Value) { - let path = std::env::var("VCORE_INTEGRATION_OBSERVATIONS").unwrap() + ".jsonl"; - let mut file = std::fs::OpenOptions::new() - .append(true) - .create(true) - .open(path) - .unwrap(); - writeln!(file, "{value}").unwrap(); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required; at least 1800 seconds"] -fn soak() { - run_soak(1800); -} - -#[test] -#[ignore = "owned INTEGRATION container fixture required; development subset only"] -fn soak_tracer() { - run_soak(65); -} - -fn run_soak(seconds: u64) { - let f = fixture(); - initialize(&f); - let (cold_fd, baseline) = idle_baseline(); - let probe = ResourceProbe::default(); - let mut case = RecordedCase::new("INTEGRATION-SOAK", "mixed_forty_flows"); - case.checkpoint("baseline", probe.snapshot()); - let (core, port, controller) = start(&f, &probe); - let mut flows = Flows::open(&f, port, controller, 0); - let first_setup = flows.setup_us.clone(); - let first_profiles = flows.profiles.clone(); - let first_median = median(first_setup.iter().copied()); - let start = Instant::now(); - let mut waves = 0; - let mut switches = Vec::new(); - let mut faults = Vec::new(); - let mut samples = Vec::new(); - let mut next_switch = 1; - let mut next_fault = 60; - let mut next_sample = if seconds >= 1800 { 300 } else { 0 }; - while start.elapsed() < Duration::from_secs(seconds) { - flows.exchange(faults.len(), waves); - waves += 1; - while start.elapsed().as_secs() >= next_switch { - let options = f["profile_groups"][TCP_GROUPS[switches.len() % 4]] - .as_array() - .unwrap(); - runtime::select( - controller, - options[(switches.len() / 4) % options.len()] - .as_str() - .unwrap(), - ); - switches.push(start.elapsed().as_secs_f64()); - next_switch += 1; - } - if start.elapsed().as_secs() >= next_fault { - let fault = start.elapsed().as_secs_f64(); - close_peer(&f); - flows.assert_tcp_closed(); - drop(flows); - flows = Flows::open(&f, port, controller, faults.len() + 1); - faults.push(json!({"start":fault,"end":start.elapsed().as_secs_f64(),"old_tcp_closed":20,"new_tcp":20,"new_udp":20,"replay":false})); - next_fault += 60; - } - if start.elapsed().as_secs() >= next_sample { - let mut point = sample(&probe); - point["seconds"] = json!(start.elapsed().as_secs_f64()); - point["waves"] = json!(waves); - point["faults"] = json!(faults.len()); - append_sample(&point); - samples.push(point); - next_sample += 60; - println!( - "INTEGRATION soak: {:.1}/{seconds}s, waves={waves}, faults={}", - start.elapsed().as_secs_f64(), - faults.len() - ); - } - thread::sleep(Duration::from_millis(100)); - } - let elapsed = start.elapsed().as_secs_f64(); - let last_setup = flows.setup_us.clone(); - let last_profiles = flows.profiles.clone(); - let last_median = median(last_setup.iter().copied()); - assert!(last_median <= first_median * 2); - let active_end = sample(&probe); - drop(flows); - let stopped = stop_quiet(core, &probe, port, controller, baseline); - case.resources(probe.snapshot()); - let mut report = json!({"seconds":elapsed,"requested_seconds":seconds,"tcp_groups":TCP_GROUPS,"udp_groups":UDP_GROUPS,"tcp":20,"udp":20,"per_group_tcp":5,"per_group_udp":5,"first_profiles":first_profiles,"last_profiles":last_profiles,"waves":waves,"normal_verified_bytes":waves*20*2*2*257,"normal_corruption":0,"normal_misdirection":0,"normal_unexpected_loss":0,"switches":switches,"faults":faults,"cold_fd":cold_fd,"baseline_fd":baseline,"samples":samples,"first_setup_us":first_setup,"last_setup_us":last_setup,"first_setup_median_us":first_median,"last_setup_median_us":last_median,"active_end":active_end,"stopped":stopped}); - if seconds >= 1800 { - assert!(samples.len() >= 25 && faults.len() >= 29 && switches.len() >= 1799); - let early = &samples[..10]; - let late = &samples[samples.len() - 10..]; - let first_heap = median(early.iter().map(|p| p["heap_in_use"].as_u64().unwrap())); - let last_heap = median(late.iter().map(|p| p["heap_in_use"].as_u64().unwrap())); - let allowed = (1024 * 1024).max(first_heap / 20); - report["heap"] = - json!({"early_median":first_heap,"late_median":last_heap,"allowed_growth":allowed}); - // Persist the complete curve even if its final acceptance fails. - observe(report.clone()); - assert!( - last_heap <= first_heap + allowed, - "heap growth exceeds INTEGRATION bound" - ); - for kind in 0..8 { - let maximum = |points: &[Value]| { - points - .iter() - .map(|p| p["resources"]["counts"][kind]["current"].as_u64().unwrap()) - .max() - .unwrap() - }; - assert!( - maximum(late) <= maximum(early), - "growing live resource kind {kind}" - ); - } - assert!( - late.iter().map(|p| p["fd"].as_u64().unwrap()).max() - <= early.iter().map(|p| p["fd"].as_u64().unwrap()).max() - ); - assert!( - samples - .iter() - .all(|p| p["queues"].as_array().unwrap().iter().all(|q| { - if q["kind"] == "hysteria2_udp" { - q["peak"] == 0 && q["capacity"] == 0 - } else { - q["peak"].as_u64().unwrap() > 0 - && q["peak"].as_u64() <= q["capacity"].as_u64() - } - })) - ); - } - observe(report); -} diff --git a/tests/vless_public/integration/shadowsocks.rs b/tests/vless_public/integration/shadowsocks.rs deleted file mode 100644 index c675812..0000000 --- a/tests/vless_public/integration/shadowsocks.rs +++ /dev/null @@ -1,110 +0,0 @@ -use super::*; -use base64::{Engine as _, engine::general_purpose::STANDARD}; - -fn cipher(node: &Value) -> &'static str { - match node["cipher"].as_str().unwrap() { - "2022-blake3-aes-128-gcm" => "2022-blake3-aes-128-gcm", - "2022-blake3-aes-256-gcm" => "2022-blake3-aes-256-gcm", - "2022-blake3-chacha20-poly1305" => "2022-blake3-chacha20-poly1305", - _ => panic!("unexpected SS algorithm"), - } -} - -pub(super) fn rejected(node: Value, f: &Value) { - let port = free_port(); - let probe = ResourceProbe::default(); - let core = probe.scope_sync(|| Core::start(&config(node, port))); - let mut origin = Origin::new(f, 13, false); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - client.write_all(&request).unwrap(); - let mut response = [0; 10]; - client.read_exact(&mut response).unwrap(); - if response[1] == 0 { - let _ = client.write_all(b"invalid-identity-must-not-reach-origin"); - runtime::assert_closed(&mut client); - } - origin.quiet(); - core.stop(); - assert!(probe.snapshot().is_idle()); - origin.quiet(); -} - -#[test] -#[ignore = "owned INTEGRATION official Mihomo container required"] -fn algorithms() { - let f = fixture(); - initialize(&f); - let nodes = f["ss_nodes"].as_array().unwrap(); - assert_eq!(nodes.len(), 3); - let mut observations = Vec::new(); - for node in nodes { - let _case = RecordedCase::new("INTEGRATION-SS-ALGORITHMS", cipher(node)); - let port = free_port(); - let core = Core::start(&config(node.clone(), port)); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - bulk_client_first(port, &f, ipv6, domain); - let mut udp = Association::new(&f, port, ipv6, domain); - for size in [1, 64, 512, 1200, 4096] { - for sequence in 0..100_u8 { - udp.exchange(&vec![sequence; size]); - } - } - } - core.stop(); - let mut bad = node.clone(); - let length = if node["cipher"] == "2022-blake3-aes-128-gcm" { - 16 - } else { - 32 - }; - bad["password"] = json!(STANDARD.encode(vec![23; length])); - rejected(bad, &f); - observations.push(json!({"cipher":node["cipher"],"tcp_targets":3,"tcp_bytes_each_direction":31457280,"udp_targets":3,"udp_packets":1500,"wrong_key_rejected":true})); - } - observe(json!({"algorithms":observations})); -} - -#[test] -#[ignore = "owned INTEGRATION official ssserver container required"] -fn eih() { - let f = fixture(); - initialize(&f); - let nodes = f["eih_nodes"].as_array().unwrap(); - assert_eq!(nodes.len(), 2); - let mut observations = Vec::new(); - for node in nodes { - let _case = RecordedCase::new("INTEGRATION-SS-EIH", cipher(node)); - for outer_ipv6 in [false, true] { - let port = free_port(); - let mut node = node.clone(); - if outer_ipv6 { - node["server"] = f["eih_ipv6"].clone(); - } - let core = Core::start(&config(node, port)); - for ipv6 in [false, true] { - bulk_client_first(port, &f, ipv6, false); - let mut udp = Association::new(&f, port, ipv6, false); - for size in [1, 64, 512, 1200, 4096] { - println!("EIH UDP: outer_ipv6={outer_ipv6}, inner_ipv6={ipv6}, size={size}"); - for sequence in 0..100_u8 { - udp.exchange(&vec![sequence; size]); - } - } - } - core.stop(); - } - let keys: Vec<_> = node["password"].as_str().unwrap().split(':').collect(); - let length = STANDARD.decode(keys[0]).unwrap().len(); - for index in 0..2 { - let mut keys: Vec<_> = keys.iter().map(ToString::to_string).collect(); - keys[index] = STANDARD.encode(vec![23; length]); - let mut bad = node.clone(); - bad["password"] = json!(keys.join(":")); - rejected(bad, &f); - } - observations.push(json!({"cipher":node["cipher"],"identity_depth":1,"outer_families":2,"tcp_targets":2,"tcp_bytes_each_direction":41943040,"udp_targets":2,"udp_packets":2000,"wrong_identity_rejected":true,"wrong_user_rejected":true})); - } - observe(json!({"native_terminal_eih":observations,"arbitrary_relay_claim":false})); -} diff --git a/tests/vless_public/runtime.rs b/tests/vless_public/runtime.rs deleted file mode 100644 index 2755cac..0000000 --- a/tests/vless_public/runtime.rs +++ /dev/null @@ -1,680 +0,0 @@ -use super::*; - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_udp_isolation() { - let _case = Case::start("VLESS-PUBLIC", "runtime::public_udp_isolation"); - let f = fixture(); - initialize(&f); - let port = free_port(); - for codec in if f["node"]["flow"] == "xtls-rprx-vision" { - vec!["xudp"] - } else { - vec!["none", "xudp", "packetaddr"] - } { - let mut node = f["node"].clone(); - node["packet-encoding"] = json!(codec); - let core = Core::start(&config(node, port)); - let mut first = Association::new(&f, port, false, false); - let mut second = Association::new(&f, port, false, false); - for n in 0..100 { - first.exchange(&[1, n]); - second.exchange(&[2, n]); - } - let rogue = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - rogue - .send_to(&first.packet(b"unauthorized"), first.relay) - .unwrap(); - first.origin.quiet(); - // Public SOCKS ingress budget is independent of the native peer's - // smaller path limit. Reject before contacting the real origin. - let cap = 65245; - first - .client - .send_to(&first.packet(&vec![0; cap + 1]), first.relay) - .unwrap(); - first.origin.quiet(); - second.exchange(b"oversize-isolated"); - // A different target in the same association must not be delivered to - // the original raw target. Encoded transports carry the new target. - let mut other = Origin::new(&f, 4, false); - let mut packet = vec![0, 0, 0]; - packet.extend(address(other.target, false)); - packet.extend(b"other-target"); - second.client.send_to(&packet, second.relay).unwrap(); - if codec == "none" - && !(f["node"]["smux"]["enabled"] == true && f["node"]["smux"]["only-tcp"] != true) - { - other.quiet(); - } else { - other.udp(b"other-target"); - let mut response = [0; 128]; - let (n, _) = second.client.recv_from(&mut response).unwrap(); - assert!(response[..n].ends_with(b"other-target")); - } - second.origin.quiet(); - core.stop(); - } -} - -#[test] -#[cfg(target_os = "macos")] -#[ignore = "isolated VLESS runner"] -fn public_entrypoints() { - let _case = Case::start("VLESS-PUBLIC", "runtime::public_entrypoints"); - let f = fixture(); - entrypoints(&f); -} - -#[cfg(target_os = "macos")] -pub(super) fn entrypoints(f: &Value) { - use std::os::{fd::AsRawFd, unix::net::UnixDatagram}; - initialize(f); - let port = free_port(); - let yaml = json!({"port":port,"proxies":[f["node"]],"rules":["MATCH,peer"]}); - let core = Core::start(&yaml); - let mut origin = Origin::new(f, 14, false); - let mut client = socket((Ipv4Addr::LOCALHOST, port).into()); - write!( - client, - "GET http://{}/ HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n", - origin.target, origin.target - ) - .unwrap(); - let mut response = Vec::new(); - client.read_to_end(&mut response).unwrap(); - assert!(response.starts_with(b"HTTP/1.1 200")); - origin.marker(b'A'); - origin.marker(b'D'); - let mut origin = Origin::new(f, 13, false); - let mut client = socket((Ipv4Addr::LOCALHOST, port).into()); - write!( - client, - "CONNECT {} HTTP/1.1\r\nHost: {}\r\n\r\nhttp-connect", - origin.target, origin.target - ) - .unwrap(); - let mut header = Vec::new(); - while !header.ends_with(b"\r\n\r\n") { - let mut byte = [0]; - client.read_exact(&mut byte).unwrap(); - header.extend(byte); - assert!(header.len() < 4096); - } - assert!(header.starts_with(b"HTTP/1.1 200")); - let mut response = [0; 12]; - client.read_exact(&mut response).unwrap(); - assert_eq!(&response, b"http-connect"); - origin.marker(b'A'); - core.stop(); - assert_closed(&mut client); - origin.marker(b'D'); - drop((client, origin)); - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_read_timeout(Some(TIMEOUT)).unwrap(); - let mut dns_origin = Origin::new(f, 17, false); - let mut yaml = json!({"tun":{"enable":true},"proxies":[f["node"]],"rules":["MATCH,peer"]}); - dns(&mut yaml, &dns_origin, "peer"); - let core = Core::prepare(&yaml); - invoke( - "start", - core.0.as_deref(), - json!({"tunFd":host.as_raw_fd(),"tunFraming":"utun"}), - ); - let mut udp = Origin::new(f, 4, false); - peer.send(&tun::packet(udp.target, 17, (0, 0, 0), b"tun-udp")) - .unwrap(); - udp.udp(b"tun-udp"); - assert_eq!(&tun::receive(&peer, 17)[28..], b"tun-udp"); - let mut origin = Origin::new(f, 13, false); - peer.send(&tun::packet(origin.target, 6, (1, 0, 2), &[])) - .unwrap(); - let syn = tun::receive(&peer, 6); - assert_eq!(syn[33] & 0x12, 0x12); - let ack = u32::from_be_bytes(syn[24..28].try_into().unwrap()).wrapping_add(1); - peer.send(&tun::packet(origin.target, 6, (2, ack, 0x18), b"tun-tcp")) - .unwrap(); - let mut reply = Vec::new(); - while reply.len() < 7 { - let packet = tun::receive(&peer, 6); - let offset = 20 + usize::from(packet[32] >> 4) * 4; - reply.extend_from_slice(&packet[offset..]); - } - assert_eq!(reply, b"tun-tcp"); - origin.marker(b'A'); - let query=b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0dvcore-fixture\x04test\x00\x00\x01\x00\x01"; - peer.send(&tun::packet( - "198.18.0.1:53".parse().unwrap(), - 17, - (0, 0, 0), - query, - )) - .unwrap(); - // DNS rewrites the query ID; observe the question on the native peer path. - let mut size = [0; 4]; - dns_origin.observer.read_exact(&mut size).unwrap(); - let mut observed = vec![0; u16::from_be_bytes([size[0], size[1]]) as usize]; - dns_origin.observer.read_exact(&mut observed).unwrap(); - assert_eq!(&observed[12..], &query[12..]); - let response = tun::receive(&peer, 17); - assert_eq!(&response[28..30], &query[..2]); - assert!(response.ends_with(&match udp.target.ip() { - IpAddr::V4(ip) => ip.octets(), - _ => unreachable!(), - })); - core.stop(); - origin.marker(b'D'); - assert!(unsafe { libc::fcntl(host.as_raw_fd(), libc::F_GETFL) } & libc::O_NONBLOCK != 0); -} - -fn fd_count() -> usize { - std::fs::read_dir("/dev/fd").unwrap().count() -} -pub(super) fn assert_closed(client: &mut TcpStream) { - match client.read(&mut [0; 32]) { - Ok(0) => {} - Err(error) - if matches!( - error.kind(), - io::ErrorKind::ConnectionReset | io::ErrorKind::BrokenPipe - ) => {} - other => panic!("stream not closed: {other:?}"), - } -} -pub(super) fn live(port: u16, f: &Value) -> (TcpStream, Origin) { - let mut origin = Origin::new(f, 13, false); - let mut client = connect_with_initial(port, origin.target, false, b"live"); - let mut response = [0; 4]; - client.read_exact(&mut response).unwrap(); - assert_eq!(&response, b"live"); - origin.marker(b'A'); - (client, origin) -} -pub(super) fn exchange(client: &mut TcpStream, bytes: &[u8]) { - client.write_all(bytes).unwrap(); - let mut result = vec![0; bytes.len()]; - client.read_exact(&mut result).unwrap(); - assert_eq!(result, bytes); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_lifecycle() { - let _case = Case::start("VLESS-PUBLIC", "runtime::public_lifecycle"); - let f = fixture(); - initialize(&f); - Core::start(&config(f["node"].clone(), free_port())).stop(); - for cycle in 0..20 { - let _cycle = Case::start("VLESS-LIFE", "stop_and_remain_quiet"); - let baseline = fd_count(); - let port = free_port(); - let mut held = None; - let mut udp = None; - let mut blackholes = Vec::new(); - match cycle % 5 { - 0 => { - let core = Core::start(&config(f["node"].clone(), port)); - echo(port, &f); - core.stop(); - } - 3 => { - let occupied = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let core = Core::prepare(&config(f["node"].clone(), port)); - assert_eq!( - invoke_response("start", core.0.as_deref(), json!({}))["success"], - false - ); - core.stop(); - drop(occupied); - } - 2 => { - let h3 = f["node"]["alpn"] == json!(["h3"]); - let origin = Origin::new(&f, if h3 { 20 } else { 15 }, false); - let mut node = f["node"].clone(); - node["server"] = json!(origin.target.ip().to_string()); - node["port"] = json!(origin.target.port()); - let target = origin.target; - blackholes.push((origin, h3)); - // Explicit XHTTP legs start concurrently. Give each its own - // observer: the serial TCP blackhole otherwise reports the - // second queued accept only after the first connection closes. - if node["network"] == "xhttp" && node["xhttp-opts"]["download-settings"].is_object() - { - let download = &mut node["xhttp-opts"]["download-settings"]; - let down_h3 = download.get("alpn").map_or(h3, |v| *v == json!(["h3"])); - let origin = Origin::new(&f, if down_h3 { 20 } else { 15 }, false); - download["server"] = json!(origin.target.ip().to_string()); - download["port"] = json!(origin.target.port()); - blackholes.push((origin, down_h3)); - } - let core = Core::start(&config(node, port)); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(target, false)); - client.write_all(&request).unwrap(); - // Prove every physical handshake is in flight before Stop. - for (origin, _) in &mut blackholes { - origin.marker(b'A'); - } - core.stop(); - let _ = client.read(&mut [0; 32]); - drop(client); - for (origin, quic) in &mut blackholes { - if !*quic { - origin.marker(b'D'); - } - } - } - _ => { - let core = Core::start(&config(f["node"].clone(), port)); - let (first, mut origin1) = live(port, &f); - let (mut second, origin2) = live(port, &f); - let mut one = Association::new(&f, port, false, false); - let mut two = Association::new(&f, port, false, false); - one.exchange(b"association-1"); - two.exchange(b"association-2"); - drop(first); - origin1.marker(b'D'); - exchange(&mut second, b"sibling-survives"); - one.exchange(b"udp-survives"); - two.exchange(b"udp-other"); - drop(origin1); - core.stop(); - assert_closed(&mut second); - let mut origin2 = origin2; - origin2.marker(b'D'); - assert_closed(&mut one.control); - assert_closed(&mut two.control); - held = Some((second, origin2)); - udp = Some((one, two)); - } - } - // All retained descriptors belong to the test, never the runtime. Check - // immediately on Stop return, not after giving cleanup a grace period. - let retained = - usize::from(held.is_some()) * 2 + usize::from(udp.is_some()) * 6 + blackholes.len(); - assert!( - fd_count() <= baseline + retained, - "descriptors retained at Stop return" - ); - let tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let udp_guard = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let quiet = Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert!(fd_count() <= baseline + retained + 2); - if let Some((first, second)) = &mut udp { - for item in [first, second] { - item.client.set_nonblocking(true).unwrap(); - assert_eq!( - item.client.recv(&mut [0; 64]).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - item.origin.observer.set_nonblocking(true).unwrap(); - assert_eq!( - item.origin.observer.read(&mut [0]).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - } - } - for (origin, _) in &mut blackholes { - origin.observer.set_nonblocking(true).unwrap(); - let mut marker = [0; 2]; - let result = origin.observer.read(&mut marker); - assert!( - matches!(&result, Err(error) if error.kind() == io::ErrorKind::WouldBlock), - "blackhole event after Stop: {result:?}, {marker:?}" - ); - } - thread::sleep(Duration::from_millis(25)); - } - drop((held, udp, blackholes, tcp, udp_guard)); - assert!(fd_count() <= baseline); - } -} - -pub(super) fn select(controller: u16, name: &str) { - let body = json!({"name":name}).to_string(); - let mut client = socket((Ipv4Addr::LOCALHOST, controller).into()); - write!(client,"PUT /proxies/inner HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-only\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",body.len()).unwrap(); - let mut response = Vec::new(); - client.read_to_end(&mut response).unwrap(); - assert!(response.starts_with(b"HTTP/1.1 204")); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn grpc_pool_keeps_physical_selection_until_new_transport() { - let _case = Case::start( - "VLESS-PUBLIC", - "runtime::grpc_pool_keeps_physical_selection_until_new_transport", - ); - let f = fixture(); - initialize(&f); - let port = free_port(); - let controller = free_port(); - let mut node = f["node"].clone(); - node["dialer-proxy"] = json!("inner"); - let mut yaml = config(node, port); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["hop"].clone()); - yaml["external-controller"] = json!(format!("127.0.0.1:{controller}")); - yaml["secret"] = json!("fixture-only"); - yaml["proxy-groups"] = json!([{"name":"inner","type":"select","proxies":["hop","REJECT"]}]); - let core = Core::start(&yaml); - let (mut old, mut origin) = live(port, &f); - select(controller, "REJECT"); - if f["node"]["network"] == "xhttp" - && f["node"]["alpn"] == json!(["http/1.1"]) - && f["node"]["smux"]["enabled"] != true - { - // H1 can recycle a completed POST, but an active download GET cannot - // carry another logical session. Its new physical connection must see - // the new REJECT selection; the old session keeps its original path. - let mut unused = Origin::new(&f, 13, false); - let mut denied = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(unused.target, false)); - denied.write_all(&request).unwrap(); - let mut reply = [0; 10]; - denied.read_exact(&mut reply).unwrap(); - if reply[1] == 0 { - // A lazily opened XHTTP request can report its failure after the - // local SOCKS handshake, but must never reach the target. - denied.write_all(b"must-not-reach-origin").unwrap(); - assert_closed(&mut denied); - } - unused.quiet(); - } else { - // Multiplexed transports can reuse the original authenticated hop. - echo(port, &f); - } - exchange(&mut old, b"same-transport"); - core.stop(); - assert_closed(&mut old); - origin.marker(b'D'); -} -#[test] -#[ignore = "isolated VLESS runner"] -fn public_graph() { - let _case = Case::start("VLESS-PUBLIC", "runtime::public_graph"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let controller = free_port(); - let mut node = f["node"].clone(); - if node["network"] == "grpc" { - // Force expansion for an active stream to distinguish new physical - // selection from existing-pool snapshot reuse (tested separately). - node["grpc-opts"]["max-connections"] = json!(0); - node["grpc-opts"]["max-streams"] = json!(1); - } - if node["network"] == "xhttp" { - node["xhttp-opts"]["reuse-settings"] = json!({"h-max-request-times":"1"}); - if node["xhttp-opts"]["download-settings"].is_object() { - node["xhttp-opts"]["download-settings"]["reuse-settings"] = - json!({"h-max-request-times":"1"}); - } - } - if node["smux"]["enabled"] == true { - node["smux"]["max-connections"] = json!(0); - node["smux"]["max-streams"] = json!(1); - } - node["dialer-proxy"] = json!("hop"); - node["server"] = json!("peer.fixture.test"); - let mut yaml = config(node.clone(), port); - let mut dns_origin = Origin::new(&f, 19, false); - dns_origin - .observer - .write_all( - &f["node"]["server"] - .as_str() - .unwrap() - .parse::() - .unwrap() - .octets(), - ) - .unwrap(); - dns(&mut yaml, &dns_origin, "DIRECT"); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["hop"].clone()); - let core = Core::start(&yaml); - echo(port, &f); - let mut a = Association::new(&f, port, false, false); - a.exchange(b"concrete-hop"); - core.stop(); - drop(a); - node["server"] = f["node"]["server"].clone(); - node["dialer-proxy"] = json!("outer"); - yaml["proxies"][0] = node; - yaml["external-controller"] = json!(format!("127.0.0.1:{controller}")); - yaml["secret"] = json!("fixture-only"); - yaml["proxy-groups"] = json!([{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]}]); - let core = Core::start(&yaml); - let (mut stream, mut origin) = live(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"before-switch"); - select(controller, "REJECT"); - exchange(&mut stream, b"old-stream-snapshot"); - udp.exchange(b"old-udp-snapshot"); - let mut unused = Origin::new(&f, 13, false); - let mut denied = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(unused.target, false)); - denied.write_all(&request).unwrap(); - let mut result = [0; 10]; - denied.read_exact(&mut result).unwrap(); - assert_ne!(result[1], 0); - unused.quiet(); - drop((denied, unused)); - select(controller, "DIRECT"); - echo(port, &f); - exchange(&mut stream, b"still-old-snapshot"); - core.stop(); - assert_closed(&mut stream); - origin.marker(b'D'); - drop((stream, origin, udp)); - // Even an unselected cycle is rejected before any start/socket creation. - yaml["proxy-groups"][1]["proxies"] = json!(["DIRECT", "peer"]); - assert_eq!( - invoke_response( - "validateConfig", - None, - json!({"configYaml":yaml.to_string()}) - )["success"], - false - ); - // A failing selected concrete upstream never switches to DIRECT. - let mut broken = f["hop"].clone(); - broken["port"] = json!(1); - yaml["proxies"][1] = broken; - yaml["proxy-groups"][1]["proxies"] = json!(["hop", "DIRECT"]); - let core = Core::start(&yaml); - let mut unused = Origin::new(&f, 13, false); - let mut denied = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(unused.target, false)); - denied.write_all(&request).unwrap(); - denied.read_exact(&mut result).unwrap(); - assert_ne!(result[1], 0); - unused.quiet(); - core.stop(); -} - -#[test] -#[ignore = "isolated VLESS runner"] -fn public_ipv6_and_gates() { - let _case = Case::start("VLESS-PUBLIC", "runtime::public_ipv6_and_gates"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let mut node = f["node"].clone(); - node["server"] = f["server_ipv6"].clone(); - let core = Core::start(&config(node, port)); - echo(port, &f); - let mut udp = Association::new(&f, port, true, false); - udp.exchange(b"outer-inner-ipv6"); - core.stop(); - drop(udp); - let mut node = f["node"].clone(); - node["udp"] = json!(false); - let mut yaml = config(node, port); - yaml["ipv6"] = json!(false); - let core = Core::start(&yaml); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.client - .send_to(&udp.packet(b"disabled"), udp.relay) - .unwrap(); - udp.origin.quiet(); - let mut origin = Origin::new(&f, 13, true); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - client.write_all(&request).unwrap(); - let mut response = [0; 10]; - client.read_exact(&mut response).unwrap(); - assert_ne!(response[1], 0); - origin.quiet(); - core.stop(); -} - -#[tokio::test] -#[ignore = "isolated VLESS runner"] -async fn owned_resources() { - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint}, - outbound::{DatagramRequest, EstablishContext, OutboundConnector, VlessOutbound}, - resources::observation::{ResourceKind, ResourceProbe}, - session::{Datagram, DatagramSession, InboundKind, StreamSession}, - }; - let _case = Case::start("VLESS-PUBLIC", "runtime::owned_resources"); - let f = fixture(); - let parsed = - Config::parse_yaml(config(f["node"].clone(), 1080).to_string().as_bytes()).unwrap(); - let ProxyProtocol::Vless(node) = &parsed.proxies[0].protocol else { - unreachable!() - }; - for _ in 0..20 { - let mut case = Case::start("VLESS-OWNED", "stop_and_remain_quiet"); - let probe = ResourceProbe::default(); - case.checkpoint("baseline", probe.snapshot()); - probe - .scope(async { - let endpoint = ResolvedEndpoint { - logical_host: node.address.clone(), - port: node.port, - addresses: vec![SocketAddr::new(node.address.parse().unwrap(), node.port)], - }; - let download_endpoint = node.download().map(|download| ResolvedEndpoint { - logical_host: download.address.clone(), - port: download.port, - addresses: vec![SocketAddr::new( - download.address.parse().unwrap(), - download.port, - )], - }); - let outbound = VlessOutbound::new_with_endpoints( - node, - endpoint, - download_endpoint, - Dialer::default(), - ) - .unwrap(); - let mut origin1 = Origin::new(&f, 13, false); - let mut origin2 = Origin::new(&f, 13, false); - let mut udp_origin = Origin::new(&f, 4, false); - let session = |target| StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: vcore::session::Destination::Ip(target), - sniffed_domain: None, - }; - let mut first = outbound - .connect_stream(session(origin1.target), &EstablishContext::default()) - .await - .unwrap() - .io; - first.write_all(b"a").await.unwrap(); - first.flush().await.unwrap(); - assert_eq!(first.read_u8().await.unwrap(), b'a'); - origin1.marker(b'A'); - let mut second = outbound - .connect_stream(session(origin2.target), &EstablishContext::default()) - .await - .unwrap() - .io; - second.write_all(b"b").await.unwrap(); - second.flush().await.unwrap(); - assert_eq!(second.read_u8().await.unwrap(), b'b'); - origin2.marker(b'A'); - drop(first); - tokio::task::spawn_blocking(move || origin1.marker(b'D')) - .await - .unwrap(); - second.write_all(b"c").await.unwrap(); - second.flush().await.unwrap(); - assert_eq!(second.read_u8().await.unwrap(), b'c'); - let request = DatagramRequest::new(DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )); - let mut udp = outbound - .open_datagram(request, &EstablishContext::default()) - .await - .unwrap(); - udp.send(Datagram { - remote: udp_origin.target.into(), - payload: bytes::Bytes::from_static(b"owned"), - sniffed_domain: None, - }) - .await - .unwrap(); - assert_eq!(udp.receive().await.unwrap().payload.as_ref(), b"owned"); - udp_origin.udp(b"owned"); - for kind in [ - ResourceKind::Association, - ResourceKind::Session, - ResourceKind::Socket, - ] { - assert!(probe.snapshot().peak(kind) > 0); - } - if matches!( - &node.transport, - vcore::config::VlessTransport::Stream( - vcore::config::StreamTransport::Grpc { .. } - | vcore::config::StreamTransport::H2 { .. } - ) - ) { - assert!(probe.snapshot().peak(ResourceKind::Task) > 0); - } - outbound.begin_shutdown(); - assert!(second.read_u8().await.is_err()); - drop(second); - // Stop already cancelled the owned IO; XUDP's final END write - // may report that cancellation. Resource release is mandatory. - let _ = udp.close().await; - drop(udp); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - case.checkpoint("after-stop", probe.snapshot()); - let snapshot = probe.snapshot(); - let quiet = tokio::time::Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert_eq!(probe.snapshot(), snapshot); - tokio::time::sleep(Duration::from_millis(25)).await; - } - case.checkpoint("quiet", probe.snapshot()); - }) - .await; - case.resources(probe.snapshot()); - } -} diff --git a/tests/vless_public/shadowtls.rs b/tests/vless_public/shadowtls.rs deleted file mode 100644 index c0ec074..0000000 --- a/tests/vless_public/shadowtls.rs +++ /dev/null @@ -1,257 +0,0 @@ -//! Public SS + ShadowTLS consumers; peers and origins belong to the container lab. -use super::*; -use vcore::resources::observation::ResourceProbe; - -fn fault_counts(f: &Value) -> Value { - let mut io = socket(f["fault_control"].as_str().unwrap().parse().unwrap()); - io.write_all(b"?").unwrap(); - serde_json::from_reader(io.take(1024)).unwrap() -} - -fn await_count(f: &Value, field: &str, expected: u64) { - let until = Instant::now() + Duration::from_secs(3); - loop { - let count = fault_counts(f)[field].as_u64().unwrap(); - if count == expected { - return; - } - assert!( - count < expected && Instant::now() < until, - "{field}: {count}" - ); - thread::sleep(Duration::from_millis(10)); - } -} - -#[test] -#[ignore = "owned ShadowTLS stalled handshake container fixture required"] -fn stall() { - let f = fixture(); - initialize(&f); - let probe = ResourceProbe::default(); - let _case = RecordedCase::new("SHADOWTLS", "stall"); - let baseline = fault_counts(&f); - let seen = baseline["hello_seen"].as_u64().unwrap(); - let closed = baseline["client_closed"].as_u64().unwrap(); - assert_eq!(seen, closed); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - let mut origin = Origin::new(&f, 13, false); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - request.extend_from_slice(b"must-not-reach-origin"); - client.write_all(&request).unwrap(); - await_count(&f, "hello_seen", seen + 1); - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - await_count(&f, "client_closed", closed + 1); - // A SOCKS failure response may precede EOF. It must never be a success. - let mut tail = Vec::new(); - match client.take(64).read_to_end(&mut tail) { - Ok(_) => {} - Err(error) => assert_eq!(error.kind(), io::ErrorKind::ConnectionReset), - } - assert!(tail.is_empty() || tail.len() >= 2 && tail[0] == 5 && tail[1] != 0); - origin.quiet(); - - let before = Instant::now(); - let response = probe.scope_sync(|| { - invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[f["node"]]}).to_string()], - "timeout":1,"url":format!("http://{}/",origin.target)}), - ) - }); - assert_eq!(response["results"][0]["success"], false); - assert!((Duration::from_millis(800)..Duration::from_secs(3)).contains(&before.elapsed())); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - await_count(&f, "hello_seen", seen + 2); - await_count(&f, "client_closed", closed + 2); - origin.quiet(); - std::fs::write( - std::env::var("VCORE_SHADOWTLS_OBSERVATIONS").unwrap(), - json!({"stop_idle":true,"stop_handshake_cancelled":true, - "measure_deadline":true,"origin_connected":false,"closed_handshakes":2}) - .to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned TCP-only ShadowTLS container fixture required"] -fn native_udp_disabled() { - let f = fixture(); - initialize(&f); - let probe = ResourceProbe::default(); - let _case = RecordedCase::new("SHADOWTLS", "native_udp_disabled"); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - let packet = udp.packet(b"no-implicit-uot"); - udp.client.send_to(&packet, udp.relay).unwrap(); - udp.client - .set_read_timeout(Some(Duration::from_millis(500))) - .unwrap(); - let error = udp.client.recv_from(&mut [0; 128]).unwrap_err(); - assert!(matches!( - error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut - )); - udp.origin.quiet(); - core.stop(); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - udp.origin.quiet(); - std::fs::write( - std::env::var("VCORE_SHADOWTLS_OBSERVATIONS").unwrap(), - json!({"tcp_accepted":true,"udp_delivered":false,"stop_idle":true}).to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned ShadowTLS fault container fixture required"] -fn corrupt() { - let f = fixture(); - initialize(&f); - let probe = ResourceProbe::default(); - let _case = RecordedCase::new("SHADOWTLS", "corrupt"); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - let mut origin = Origin::new(&f, 13, false); - let mut client = connect(port, origin.target, false); - client.write_all(b"must-not-be-delivered-back").unwrap(); - origin.marker(b'A'); - match client.read(&mut [0; 1]) { - Ok(0) => {} - Err(error) => assert!(matches!( - error.kind(), - io::ErrorKind::ConnectionReset - | io::ErrorKind::UnexpectedEof - | io::ErrorKind::BrokenPipe - )), - other => panic!("corrupted authenticated response was delivered: {other:?}"), - } - core.stop(); - assert!(probe.snapshot().is_idle()); - origin.marker(b'D'); - origin.quiet(); - std::fs::write( - std::env::var("VCORE_SHADOWTLS_OBSERVATIONS").unwrap(), - json!({"origin_connected":true, "delivered_bytes":0, "stop_idle":true}).to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned ShadowTLS container fixture required"] -fn policy() { - let f = fixture(); - initialize(&f); - let probe = ResourceProbe::default(); - let _case = RecordedCase::new("SHADOWTLS", "policy"); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - let mut origin = Origin::new(&f, 13, false); - let success = f["expected_success"].as_bool().unwrap(); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - // Pipeline business bytes to prove that failed identity cannot reach the - // business origin. A transport timeout is never an authentication success. - request.extend_from_slice(b"authenticated-business"); - client.write_all(&request).unwrap(); - if success { - reply(&mut client); - let mut bytes = [0; 22]; - client.read_exact(&mut bytes).unwrap(); - assert_eq!(&bytes, b"authenticated-business"); - origin.marker(b'A'); - } else { - let mut header = [0; 3]; - client.read_exact(&mut header).unwrap(); - assert_eq!(header[0], 5); - assert_ne!( - header[1], 0, - "authentication must fail before SOCKS success" - ); - assert_eq!(header[2], 0); - origin.quiet(); - } - core.stop(); - assert!(probe.snapshot().is_idle()); - if success { - origin.marker(b'D'); - } - origin.quiet(); - std::fs::write( - std::env::var("VCORE_SHADOWTLS_OBSERVATIONS").unwrap(), - json!({"accepted":success,"origin_connected":success,"stop_idle":true}).to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned ShadowTLS container fixture required"] -fn data() { - data_gate(true); -} - -#[test] -#[ignore = "owned native ShadowTLS and SS container fixture required"] -fn native_tcp() { - data_gate(false); -} - -fn data_gate(native_udp: bool) { - let f = fixture(); - initialize(&f); - let probe = ResourceProbe::default(); - let _case = RecordedCase::new("SHADOWTLS", if native_udp { "data" } else { "native_tcp" }); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - // Client-first avoids the documented official SS empty-first-write padding - // limitation. This is not used as evidence of server-first support. - let mut origin = Origin::new(&f, 13, false); - let mut client = connect(port, origin.target, false); - let mut upload = Sha256::new(); - let mut download = Sha256::new(); - for sequence in 0..2560_u32 { - let bytes: Vec = (0..4096).map(|n| ((n + sequence) % 251) as u8).collect(); - client.write_all(&bytes).unwrap(); - if sequence == 0 { - origin.marker(b'A'); - } - let mut reply = vec![0; bytes.len()]; - client.read_exact(&mut reply).unwrap(); - assert_eq!(reply, bytes); - upload.update(&bytes); - download.update(&reply); - } - assert_eq!(upload.finalize(), download.finalize()); - let mut udp = native_udp.then(|| Association::new(&f, port, false, false)); - if let Some(udp) = udp.as_mut() { - for size in [1, 64, 512, 1200, 4096] { - for sequence in 0..100_u8 { - udp.exchange(&vec![sequence; size]); - } - } - } - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - runtime::assert_closed(&mut client); - origin.marker(b'D'); - origin.quiet(); - let observation = json!({"tcp_bytes_each_direction":10*1024*1024,"udp_packets":if native_udp {500} else {0},"udp_sizes":if native_udp {vec![1,64,512,1200,4096]} else {vec![]},"stop_idle":true}); - std::fs::write( - std::env::var("VCORE_SHADOWTLS_OBSERVATIONS").unwrap(), - observation.to_string(), - ) - .unwrap(); -} diff --git a/tests/vless_public/tuic.rs b/tests/vless_public/tuic.rs deleted file mode 100644 index 392a5f7..0000000 --- a/tests/vless_public/tuic.rs +++ /dev/null @@ -1,292 +0,0 @@ -//! TUIC public consumers. No protocol server is started on the host. -use super::*; -use vcore::resources::observation::ResourceProbe; - -fn save(value: Value) { - std::fs::write( - std::env::var("VCORE_TUIC_OBSERVATIONS").unwrap(), - value.to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned TUIC UDP container fixture required"] -fn udp() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("TUIC-PUBLIC", "udp"); - let probe = ResourceProbe::default(); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - let mut associations = Vec::new(); - let mut alternates = Vec::new(); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - let mut udp = Association::new(&f, port, ipv6, domain); - let mut other = Origin::new(&f, if ipv6 { 6 } else { 4 }, ipv6); - for size in [0, 1, 64, 512, 1200, 4096, 16384] { - for sequence in 0..100_u8 { - std::mem::swap(&mut udp.origin, &mut other); - udp.exchange(&vec![sequence; size]); - } - } - associations.push(udp); - alternates.push(other); - } - echo(port, &f); - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - for udp in &mut associations { - udp.origin.quiet(); - } - for origin in &mut alternates { - origin.quiet(); - } - save( - json!({"udp_packets":2100,"udp_sizes":[0,1,64,512,1200,4096,16384],"families":["ipv4","ipv6","domain"],"alternating_origins":true,"tcp_sibling":true,"stop_idle":true}), - ); -} - -#[test] -#[ignore = "owned TUIC container fixture required"] -fn tcp() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("TUIC-PUBLIC", "tcp"); - let probe = ResourceProbe::default(); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - // Keep several logical streams live on one node, across address families. - let mut origins = Vec::new(); - let mut clients = Vec::new(); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - let mut origin = Origin::new(&f, 13, ipv6); - let mut io = connect(port, origin.target, domain); - let mut upload = Sha256::new(); - let mut download = Sha256::new(); - for sequence in 0..2560_u32 { - let bytes: Vec = (0..4096).map(|i| ((i + sequence) % 251) as u8).collect(); - io.write_all(&bytes).unwrap(); - if sequence == 0 { - origin.marker(b'A'); - } - let mut reply = vec![0; bytes.len()]; - io.read_exact(&mut reply).unwrap(); - assert_eq!(reply, bytes); - upload.update(bytes); - download.update(reply); - } - assert_eq!(upload.finalize(), download.finalize()); - origins.push(origin); - clients.push(io); - } - // One client closes, its siblings remain functional on the same pool. - clients - .remove(0) - .shutdown(std::net::Shutdown::Both) - .unwrap(); - origins.remove(0).marker(b'D'); - for client in &mut clients { - client.write_all(b"sibling").unwrap(); - let mut bytes = [0; 7]; - client.read_exact(&mut bytes).unwrap(); - assert_eq!(&bytes, b"sibling"); - } - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - for client in &mut clients { - runtime::assert_closed(client); - } - for origin in &mut origins { - origin.marker(b'D'); - origin.quiet(); - } - save( - json!({"families":["ipv4","ipv6","domain"],"tcp_bytes_each_family_each_direction":10*1024*1024,"sibling_survives":true,"stop_idle":true}), - ); -} - -#[test] -#[ignore = "owned TUIC rejection container fixture required"] -fn rejected() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("TUIC-PUBLIC", "rejected"); - let probe = ResourceProbe::default(); - let port = free_port(); - let mut yaml = config(f["node"].clone(), port); - if let Some(upstream) = f.get("upstream") { - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(upstream.clone()); - yaml["proxies"][0]["dialer-proxy"] = json!("hop"); - } - let core = probe.scope_sync(|| Core::start(&yaml)); - let mut origin = Origin::new(&f, 13, false); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - request.extend_from_slice(b"must-not-reach-origin"); - client.write_all(&request).unwrap(); - let mut header = [0; 3]; - match client.read_exact(&mut header) { - Ok(_) => { - assert_eq!(header[0], 5); - assert_eq!(header[2], 0); - if header[1] == 0 { - // TUIC has no auth ACK: local SOCKS success can precede rejection. - let mut atyp = [0]; - client.read_exact(&mut atyp).unwrap(); - let length = match atyp[0] { - 1 => 6, - 4 => 18, - _ => panic!("invalid SOCKS reply"), - }; - client.read_exact(&mut vec![0; length]).unwrap(); - runtime::assert_closed(&mut client); - } - } - Err(error) => assert!(matches!( - error.kind(), - io::ErrorKind::UnexpectedEof | io::ErrorKind::ConnectionReset - )), - } - origin.quiet(); - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - origin.quiet(); - save(json!({"origin_connected":false,"received_business_bytes":0,"stop_idle":true})); -} - -#[test] -#[ignore = "owned TUIC business UDP policy fixture required"] -fn udp_disabled() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("TUIC-PUBLIC", "udp_disabled"); - assert_eq!(f["node"]["udp"], false); - let probe = ResourceProbe::default(); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.client - .send_to(&udp.packet(b"disabled"), udp.relay) - .unwrap(); - udp.client - .set_read_timeout(Some(Duration::from_millis(500))) - .unwrap(); - let error = udp.client.recv_from(&mut [0; 128]).unwrap_err(); - assert!(matches!( - error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut - )); - udp.origin.quiet(); - core.stop(); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - udp.origin.quiet(); - save(json!({"udp_packets":0,"tcp_works":true,"stop_idle":true})); -} - -#[test] -#[ignore = "owned TUIC upstream and restart fixture required"] -fn group() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("TUIC-PUBLIC", "group"); - let probe = ResourceProbe::default(); - let port = free_port(); - let controller = free_port(); - let mut yaml = config(f["node"].clone(), port); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["upstream"].clone()); - yaml["proxies"][0]["dialer-proxy"] = json!("outer"); - yaml["proxy-groups"] = json!([ - {"name":"outer","type":"select","proxies":["inner"]}, - {"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]} - ]); - yaml["external-controller"] = json!(format!("127.0.0.1:{controller}")); - yaml["secret"] = json!("fixture-only"); - let core = probe.scope_sync(|| Core::start(&yaml)); - let (mut old, mut origin) = runtime::live(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"old-path"); - runtime::select(controller, "REJECT"); - runtime::exchange(&mut old, b"old-stream"); - // Logical flows on the already established QUIC pool retain its path. - echo(port, &f); - let mut pooled = Association::new(&f, port, false, false); - pooled.exchange(b"new-logical-existing-pool"); - let mut control = socket(f["restart_control"].as_str().unwrap().parse().unwrap()); - control.write_all(b"R").unwrap(); - let mut ack = [0]; - control.read_exact(&mut ack).unwrap(); - assert_eq!(ack, [b'R']); - old.set_read_timeout(Some(Duration::from_secs(35))).unwrap(); - runtime::assert_closed(&mut old); - origin.marker(b'D'); - let mut denied_origin = Origin::new(&f, 13, false); - let mut denied = login(port); - let mut connect = vec![5, 1, 0]; - connect.extend(address(denied_origin.target, false)); - connect.extend_from_slice(b"no-fallback"); - denied.write_all(&connect).unwrap(); - let mut reply = [0; 3]; - denied.read_exact(&mut reply).unwrap(); - assert_eq!(reply[0], 5); - assert_ne!(reply[1], 0); - denied_origin.quiet(); - runtime::select(controller, "DIRECT"); - echo(port, &f); - let mut fresh = Association::new(&f, port, false, false); - fresh.exchange(b"new-physical-direct"); - let start = Instant::now(); - core.stop(); - assert!(start.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - udp.origin.quiet(); - pooled.origin.quiet(); - fresh.origin.quiet(); - denied_origin.quiet(); - drop((old, origin, udp, pooled, fresh, denied, denied_origin)); - - let mut measure_origin = Origin::new(&f, 14, false); - let result=probe.scope_sync(||invoke("measureDelay",None,json!({"configYamls":[json!({"proxies":[f["node"]]}).to_string()],"timeout":5,"url":format!("http://{}/",measure_origin.target)}))); - assert_eq!(result["results"][0]["success"], true); - measure_origin.marker(b'A'); - measure_origin.marker(b'D'); - let rejected = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[f["node"]],"proxy-groups":[{"name":"group","type":"select","proxies":["peer"]}]}).to_string()],"timeout":5,"url":format!("http://{}/",measure_origin.target)}), - ); - assert_eq!(rejected["results"][0]["success"], false); - measure_origin.quiet(); - assert!(probe.snapshot().is_idle()); - drop(measure_origin); - let occupied = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let prepared = probe.scope_sync(|| Core::prepare(&yaml)); - assert_eq!( - probe.scope_sync(|| invoke_response("start", prepared.0.as_deref(), json!({})))["success"], - false - ); - prepared.stop(); - drop(occupied); - assert!(probe.snapshot().is_idle()); - let rebuilt = probe.scope_sync(|| Core::start(&yaml)); - echo(port, &f); - rebuilt.stop(); - assert!(probe.snapshot().is_idle()); - save( - json!({"nested_select":true,"pooled_path_retained":true,"explicit_rebuild":true,"reject_no_fallback":true,"new_direct":true,"node_measure":true,"group_measure_rejected":true,"rollback":true,"stop_idle":true}), - ); -} diff --git a/tests/vless_public/uot.rs b/tests/vless_public/uot.rs deleted file mode 100644 index c7a52b4..0000000 --- a/tests/vless_public/uot.rs +++ /dev/null @@ -1,186 +0,0 @@ -//! Public UoT consumers. Every protocol peer, DNS and origin is container-owned. -use super::*; -use vcore::resources::observation::ResourceProbe; - -fn no_native_udp(f: &Value) { - let mut observer = socket(f["udp_observer"].as_str().unwrap().parse().unwrap()); - observer.write_all(b"?").unwrap(); - let counts: Value = serde_json::from_reader(observer.take(4096)).unwrap(); - assert_eq!(counts["native_udp_packets"], 0); - assert!( - counts["ports"] - .as_array() - .unwrap() - .contains(&f["node"]["port"]) - ); -} - -fn save(value: Value) { - std::fs::write( - std::env::var("VCORE_UOT_OBSERVATIONS").unwrap(), - value.to_string(), - ) - .unwrap(); -} - -#[test] -#[ignore = "owned UoT container fixture required"] -fn data() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("UOT-PUBLIC", "data"); - let probe = ResourceProbe::default(); - let port = free_port(); - let mut dns_origin = Origin::new(&f, 17, false); - let mut yaml = config(f["node"].clone(), port); - if let Some(upstream) = f.get("upstream") { - assert_eq!(upstream["udp"], false); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(upstream.clone()); - yaml["proxies"][0]["dialer-proxy"] = json!("hop"); - } - dns(&mut yaml, &dns_origin, "DIRECT"); - let core = probe.scope_sync(|| Core::start(&yaml)); - echo(port, &f); - let mut associations = Vec::new(); - let mut alternates = Vec::new(); - for (ipv6, domain) in [(false, false), (true, false), (false, true)] { - let mut association = Association::new(&f, port, ipv6, domain); - let mut alternate = Origin::new(&f, if ipv6 { 6 } else { 4 }, ipv6); - for size in [0, 1, 64, 512, 1200, 4096, 16_384] { - for sequence in 0..100_u8 { - std::mem::swap(&mut association.origin, &mut alternate); - association.exchange(&vec![sequence; size]); - } - } - // The official Mihomo UoT reader uses a 16 KiB packet buffer. One byte - // beyond it fails at that peer; this is not VCore's u16 codec ceiling. - let oversize = vec![7; 16_385]; - association - .client - .send_to(&association.packet(&oversize), association.relay) - .unwrap(); - association.origin.quiet(); - association - .client - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let error = association.client.recv_from(&mut [0; 32]).unwrap_err(); - assert!(matches!( - error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut - )); - association.client.set_read_timeout(Some(TIMEOUT)).unwrap(); - associations.push(association); - alternates.push(alternate); - } - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - // The controlled resolver accepts only this business name. Internal UoT - // magic reaching DNS would make the fixture reject the request. - let mut header = [0; 4]; - dns_origin.observer.read_exact(&mut header).unwrap(); - let mut question = vec![0; u16::from_be_bytes([header[0], header[1]]) as usize]; - dns_origin.observer.read_exact(&mut question).unwrap(); - assert_eq!(&question[12..], b"\x0dvcore-fixture\x04test\0\0\x01\0\x01"); - for association in &mut associations { - association.origin.quiet(); - } - for origin in &mut alternates { - origin.quiet(); - } - no_native_udp(&f); - save(json!({ - "udp_packets":2100,"udp_sizes":[0,1,64,512,1200,4096,16384], - "families":["ipv4","ipv6","domain"],"alternating_origins":true, - "controlled_dns":true,"tcp_regression":true,"stop_idle":true, - "tcp_only_upstream":f.get("upstream").is_some(), - "peer_oversize_rejected":true,"native_udp_packets":0 - })); -} - -#[test] -#[ignore = "owned UoT TCP-only upstream fixture required"] -fn group() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("UOT-PUBLIC", "group"); - let probe = ResourceProbe::default(); - let port = free_port(); - let controller = free_port(); - let mut yaml = config(f["node"].clone(), port); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["upstream"].clone()); - yaml["proxies"][0]["dialer-proxy"] = json!("outer"); - yaml["proxy-groups"] = json!([ - {"name":"outer","type":"select","proxies":["inner"]}, - {"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]} - ]); - yaml["external-controller"] = json!(format!("127.0.0.1:{controller}")); - yaml["secret"] = json!("fixture-only"); - let core = probe.scope_sync(|| Core::start(&yaml)); - let mut existing = Association::new(&f, port, false, false); - existing.exchange(b"initial-hop"); - runtime::select(controller, "REJECT"); - existing.exchange(b"old-association-snapshot"); - let mut rejected = Association::new(&f, port, false, false); - rejected - .client - .send_to(&rejected.packet(b"must-not-fallback"), rejected.relay) - .unwrap(); - rejected.origin.quiet(); - runtime::select(controller, "DIRECT"); - let mut direct = Association::new(&f, port, false, false); - direct.exchange(b"new-direct-association"); - existing.exchange(b"still-original-hop"); - core.stop(); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - existing.origin.quiet(); - direct.origin.quiet(); - rejected.origin.quiet(); - no_native_udp(&f); - save( - json!({"nested_select":true,"old_association_snapshot":true,"new_direct":true, - "reject_no_fallback":true,"native_udp_packets":0,"stop_idle":true}), - ); -} - -#[test] -#[ignore = "owned UoT negative peer fixture required"] -fn rejected() { - let f = fixture(); - initialize(&f); - let _case = RecordedCase::new("UOT-PUBLIC", "rejected"); - let probe = ResourceProbe::default(); - let port = free_port(); - let core = probe.scope_sync(|| Core::start(&config(f["node"].clone(), port))); - let mut association = Association::new(&f, port, false, false); - let bytes = association.packet(b"must-not-reach-origin"); - association - .client - .send_to(&bytes, association.relay) - .unwrap(); - association - .client - .set_read_timeout(Some(Duration::from_millis(500))) - .unwrap(); - let error = association.client.recv_from(&mut [0; 128]).unwrap_err(); - assert!(matches!( - error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut - )); - association.origin.quiet(); - let before = Instant::now(); - core.stop(); - assert!(before.elapsed() < Duration::from_secs(5)); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - association.origin.quiet(); - no_native_udp(&f); - save(json!({"origin_packets":0,"received_packets":0,"native_udp_packets":0,"stop_idle":true})); -} diff --git a/tests/vmess_native.rs b/tests/vmess_native.rs deleted file mode 100644 index 01ceeb5..0000000 --- a/tests/vmess_native.rs +++ /dev/null @@ -1,625 +0,0 @@ -#![cfg(all(feature = "outbound-vmess", feature = "interop-test"))] -use std::{io, net::SocketAddr, time::Duration}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use vcore::{ - dialer::{Dialer, ResolvedEndpoint}, - outbound::vmess::{ - BodyCipher, BodyOptions, ClientHandshake, Command, VmessIdentity, VmessStream, - }, - security::{SecurityContext, StandardTlsClient, TlsCertificatePolicy, TlsClientOptions}, - session::Destination, - transport::{ - HttpObfsOptions, StreamDriver, WebSocketOptions, connect_websocket, grpc, http_obfs, - legacy_h2, - }, -}; - -const UUID: uuid::Uuid = uuid::Uuid::from_bytes([7; 16]); -const PAYLOAD_BYTES: usize = 10 * 1024 * 1024; - -#[path = "vmess_native/origin.rs"] -mod origin; -#[path = "vmess_native/udp_ab.rs"] -mod udp_ab; - -fn native_udp_budget( - fixture: &serde_json::Value, - codec: &str, - cipher: BodyCipher, - padding: bool, - length: bool, - family: &str, -) -> usize { - let address = if codec == "packetaddr" { - if family == "ipv4" { 7 } else { 19 } - } else { - 0 - }; - let host = (fixture["udp_path_limit"].as_u64().unwrap() as usize).min(15000 - address); - if fixture["peer_kind"] != "V2" { - return host; - } - // Official V2Ray 5.53's packet writer is bounded by its 2048-byte - // buffer. Mux data is framed outside the body packet boundary; raw and - // packetaddr must leave room for VMess length, tag and maximum padding. - // This is a native-fixture budget, never a production protocol limit. - if codec == "xudp" { - return host.min(2048); - } - let overhead = if cipher == BodyCipher::None { - 2 - } else { - 16 + if length { 18 } else { 2 } + if padding { 63 } else { 0 } - }; - host.min(2048 - overhead - address) -} - -fn event_for(assertion: &str, status: &str) { - use std::io::Write; - let path = std::env::var("VCORE_CASE_EVENTS").expect("owned harness events"); - let mut file = std::fs::OpenOptions::new() - .create(true) - .append(true) - .open(path) - .unwrap(); - writeln!(file, "{}", serde_json::json!({"schema_version":1,"suite":"VMESS-WIRE","assertion":assertion,"status":status})).unwrap(); -} - -async fn stream( - peer: SocketAddr, - target: Destination, - cipher: BodyCipher, - padding: bool, - length: bool, -) -> io::Result<(VmessStream, Option)> { - wire_stream(peer, target, cipher, padding, length, Command::Tcp).await -} - -async fn wire_stream( - peer: SocketAddr, - target: Destination, - cipher: BodyCipher, - padding: bool, - length: bool, - command: Command, -) -> io::Result<(VmessStream, Option)> { - let deadline = tokio::time::Instant::now() + Duration::from_secs(10); - let endpoint = ResolvedEndpoint { - logical_host: peer.ip().to_string(), - port: peer.port(), - addresses: vec![peer], - }; - let raw = tokio::time::timeout_at(deadline, Dialer::default().connect(&endpoint)).await??; - let handshake = ClientHandshake::new( - &VmessIdentity::new(UUID), - command, - &target, - BodyOptions::new(cipher, padding, length)?, - )?; - let mut raw = Box::new(raw) as vcore::dispatch::BoxStream; - let fixture: serde_json::Value = serde_json::from_str( - &std::env::var("VCORE_VMESS_TRANSPORT").unwrap_or_else(|_| "{}".into()), - ) - .unwrap(); - let mode = fixture["mode"].as_str().unwrap_or("tcp"); - let encrypted = fixture["tls"].as_bool().unwrap_or(false); - let scheme = if encrypted { "https" } else { "http" }; - if encrypted { - let pin = fixture["pin"].as_str().unwrap(); - let mut fingerprint = [0; 32]; - for (index, byte) in fingerprint.iter_mut().enumerate() { - *byte = u8::from_str_radix(&pin[2 * index..2 * index + 2], 16).unwrap(); - } - let alpn = match mode { - "grpc" | "h2" => vec![b"h2".to_vec()], - "ws" => vec![b"http/1.1".to_vec()], - _ => vec![], - }; - let tls = StandardTlsClient::with_options( - &SecurityContext::new(), - "localhost", - TlsClientOptions { - required_alpn: alpn.first().cloned(), - alpn, - certificate: TlsCertificatePolicy { - fingerprint: Some(fingerprint), - ..Default::default() - }, - ..Default::default() - }, - 0, - 65536, - )?; - raw = tokio::time::timeout_at(deadline, tls.connect(raw)).await??; - } - let mut driver = None; - match mode { - "tcp" => {} - "ws" => { - let scheme = if encrypted { "wss" } else { "ws" }; - let options = WebSocketOptions::new( - &format!("{scheme}://localhost:{}/vmess-ws", peer.port()), - http::HeaderMap::new(), - None, - )?; - raw = connect_websocket(raw, &options, handshake.request(), deadline).await?; - } - "grpc" | "h2" => { - let uri = format!( - "{scheme}://localhost/{}", - if mode == "grpc" { - "vmess-grpc/Tun" - } else { - "vmess-h2" - } - ); - let connected = if mode == "grpc" { - grpc(raw, &uri, deadline).await? - } else { - legacy_h2(raw, &uri, deadline).await? - }; - raw = connected.0; - driver = Some(connected.1); - } - "http" => { - let options = HttpObfsOptions::new( - http::Method::GET, - &format!("{scheme}://localhost:{}/vmess-http", peer.port()), - http::HeaderMap::new(), - )?; - raw = http_obfs(raw, &options, handshake.request(), deadline).await?; - } - _ => panic!("unknown owned transport"), - } - if !matches!(mode, "ws" | "http") { - raw.write_all(handshake.request()).await?; - raw.flush().await?; - } - let stream = VmessStream::new(raw, handshake, deadline); - Ok(( - if matches!(mode, "grpc" | "http" | "h2") { - stream.with_whole_close() - } else { - stream - }, - driver, - )) -} - -#[tokio::test] -#[ignore = "requires owned official native peer"] -async fn native_cipher_matrix() { - event_for("native_cipher_matrix", "BEGIN"); - let peer: SocketAddr = std::env::var("VCORE_VMESS_PEER").unwrap().parse().unwrap(); - for cipher in [ - BodyCipher::None, - BodyCipher::Auto, - BodyCipher::Aes128Gcm, - BodyCipher::Chacha20Poly1305, - ] { - for padding in [false, true] { - for length in [false, true] { - if cipher == BodyCipher::None && (padding || length) { - continue; - } - println!("VMess fixture: {cipher:?} padding={padding} length={length}"); - tokio::time::timeout(Duration::from_secs(30), async { - let mut origin = origin::TcpOrigin::new(10).await; - let target = origin.target.clone(); - let (mut client, driver) = - stream(peer, target, cipher, padding, length).await.unwrap(); - let mut greeting = [0; 5]; - tokio::time::timeout(Duration::from_secs(3), client.read_exact(&mut greeting)) - .await - .expect("server-first greeting deadline") - .unwrap(); - assert_eq!(&greeting, b"hello"); - tokio::time::timeout( - Duration::from_secs(3), - client.write_all(&vec![0x5a; PAYLOAD_BYTES]), - ) - .await - .expect("upload deadline") - .unwrap(); - client.flush().await.unwrap(); - let mut echo = vec![0; PAYLOAD_BYTES]; - let echoed = - tokio::time::timeout(Duration::from_secs(3), client.read_exact(&mut echo)) - .await; - assert!( - echoed.is_ok(), - "native echo deadline: echo_bytes={}", - echo.iter().filter(|byte| **byte == 0x5a).count() - ); - echoed.unwrap().unwrap(); - assert!(echo.iter().all(|byte| *byte == 0x5a)); - let mut tail = [0; 7]; - tokio::time::timeout(Duration::from_secs(3), client.read_exact(&mut tail)) - .await - .expect("normal response tail deadline") - .unwrap(); - assert_eq!(&tail, b"trailer"); - client.shutdown().await.unwrap(); - let mut remaining = Vec::new(); - tokio::time::timeout( - Duration::from_secs(3), - client.read_to_end(&mut remaining), - ) - .await - .expect("close deadline") - .unwrap(); - assert!(remaining.is_empty()); - origin.accepted().await; - origin.completed().await; - drop(client); - if let Some(driver) = driver { - driver.stop().await.unwrap(); - } - }) - .await - .expect("native exchange deadline"); - } - } - } - event_for("native_cipher_matrix", "PASS"); -} - -#[tokio::test] -#[ignore = "requires owned official native peer"] -async fn native_raw_udp_boundaries() { - use vcore::{ - dispatch::{DatagramBudget, DatagramTransport}, - outbound::vmess::VmessDatagram, - session::Datagram, - }; - event_for("native_raw_udp_boundaries", "BEGIN"); - let peer: SocketAddr = std::env::var("VCORE_VMESS_PEER").unwrap().parse().unwrap(); - let fixture: serde_json::Value = - serde_json::from_str(&std::env::var("VCORE_VMESS_TRANSPORT").unwrap()).unwrap(); - for cipher in [ - BodyCipher::None, - BodyCipher::Auto, - BodyCipher::Aes128Gcm, - BodyCipher::Chacha20Poly1305, - ] { - for padding in [false, true] { - for length in [false, true] { - if cipher == BodyCipher::None && (padding || length) { - continue; - } - for family in ["ipv4", "ipv6", "domain"] { - let path_limit = - native_udp_budget(&fixture, "raw", cipher, padding, length, family); - println!( - "raw UDP: {cipher:?} padding={padding} length={length} target={family}" - ); - tokio::time::timeout(Duration::from_secs(15), async { - let mut origin = udp_ab::ContainerOrigin::connect(&origin::fixture(), family).await.unwrap(); - let address = origin.address; - let target = if family == "domain" { - Destination::domain("vcore-fixture.test", address.port()).unwrap() - } else { - address.into() - }; - let (client, driver) = - wire_stream(peer, target.clone(), cipher, padding, length, Command::Udp) - .await - .unwrap(); - let mut client = VmessDatagram::raw(client, target.clone(), DatagramBudget::new(path_limit as u16, path_limit as u16)); - for size in [1, 64, 512, 1200, path_limit] { - println!("raw UDP size={size}"); - for sequence in 0..100 { - let packet = vec![sequence as u8; size]; - client.send(Datagram { remote: target.clone(), payload: packet.clone().into(), sniffed_domain: None }).await.unwrap(); - let (received, _) = tokio::time::timeout( - Duration::from_secs(1), - origin.receive(), - ) - .await - .unwrap_or_else(|_| panic!("native UDP origin deadline: size={size} sequence={sequence}")) - .unwrap(); - assert_eq!(received.len(), size, "raw UDP origin length, sequence={sequence}"); - assert!(received == packet, "raw UDP origin content, size={size} sequence={sequence}"); - let echo = tokio::time::timeout( - Duration::from_secs(1), - client.receive(), - ) - .await - .expect("native UDP response deadline") - .unwrap(); - assert_eq!(echo.remote, target); - assert_eq!(echo.payload.len(), size, "raw UDP response length, sequence={sequence}"); - assert!(echo.payload == packet, "raw UDP response content, size={size} sequence={sequence}"); - } - } - assert!(client.send(Datagram { remote: target, payload: vec![0; path_limit + 1].into(), sniffed_domain: None }).await.is_err()); - assert!( - tokio::time::timeout( - Duration::from_millis(30), - origin.receive() - ) - .await - .is_err() - ); - drop(client); - if let Some(driver) = driver { - driver.stop().await.unwrap(); - } - }) - .await - .expect("native UDP matrix deadline"); - } - } - } - } - event_for("native_raw_udp_boundaries", "PASS"); -} - -#[tokio::test] -#[ignore = "requires owned official native peer"] -async fn native_encoded_udp_boundaries() { - use bytes::Bytes; - use vcore::{ - dispatch::{DatagramBudget, DatagramTransport}, - dns::resolution::ResolutionContext, - outbound::vmess::VmessDatagram, - session::Datagram, - }; - event_for("native_encoded_udp_boundaries", "BEGIN"); - let peer: SocketAddr = std::env::var("VCORE_VMESS_PEER").unwrap().parse().unwrap(); - let fixture: serde_json::Value = - serde_json::from_str(&std::env::var("VCORE_VMESS_TRANSPORT").unwrap()).unwrap(); - for codec in ["xudp", "packetaddr"] { - for cipher in [ - BodyCipher::None, - BodyCipher::Auto, - BodyCipher::Aes128Gcm, - BodyCipher::Chacha20Poly1305, - ] { - for padding in [false, true] { - for length in [false, true] { - if cipher == BodyCipher::None && (padding || length) { - continue; - } - for family in ["ipv4", "ipv6", "domain"] { - let maximum = - native_udp_budget(&fixture, codec, cipher, padding, length, family); - println!( - "encoded UDP: {codec} {cipher:?} padding={padding} length={length} target={family}" - ); - tokio::time::timeout(Duration::from_secs(15), async { - let mut origin = udp_ab::ContainerOrigin::connect(&origin::fixture(), family).await.unwrap(); - let address = origin.address; - let target = if family == "domain" { - Destination::domain("vcore-fixture.test", address.port()).unwrap() - } else { - address.into() - }; - let (command, magic) = if codec == "xudp" { - (Command::Mux, "v1.mux.cool") - } else { - (Command::Udp, "sp.packet-addr.v2fly.arpa") - }; - let (client, driver) = wire_stream( - peer, - Destination::domain(magic, 443).unwrap(), - cipher, - padding, - length, - command, - ) - .await - .unwrap(); - let resolver = std::sync::Arc::new(FixtureResolver( - std::sync::atomic::AtomicUsize::new(0), - )); - let mut transport: Box = if codec == "xudp" { - Box::new(vcore::xudp::XudpTransport::with_budget( - Box::new(client), - [0; 8], - DatagramBudget::new(maximum as u16, maximum as u16), - )) - } else { - Box::new(VmessDatagram::packet_addr( - client, - DatagramBudget::new(maximum as u16, maximum as u16), - ResolutionContext::measurement(resolver.clone(), true), - )) - }; - for size in [1, 64, 512, 1200, maximum] { - println!("encoded UDP size={size}"); - for sequence in 0..100 { - let packet = vec![sequence as u8; size]; - transport - .send(Datagram { - remote: target.clone(), - payload: Bytes::from(packet.clone()), - sniffed_domain: None, - }) - .await - .unwrap(); - let (incoming, _) = tokio::time::timeout( - Duration::from_secs(1), - origin.receive(), - ) - .await - .unwrap_or_else(|_| panic!("encoded UDP origin deadline: size={size} sequence={sequence}")) - .unwrap(); - assert_eq!(incoming.len(), size, "encoded UDP origin length, sequence={sequence}"); - assert!(incoming == packet, "encoded UDP origin content, size={size} sequence={sequence}"); - let response = tokio::time::timeout( - Duration::from_secs(1), - transport.receive(), - ) - .await - .unwrap_or_else(|_| panic!("encoded UDP response deadline: size={size} sequence={sequence}")) - .unwrap(); - assert_eq!(response.payload.len(), size, "encoded UDP response length, sequence={sequence}"); - assert!(response.payload == packet, "encoded UDP response content, size={size} sequence={sequence}"); - assert_eq!(response.remote.port(), address.port()); - if family != "domain" || codec == "packetaddr" { - assert_eq!(response.remote, address.into()); - } - } - } - assert_eq!( - resolver.0.load(std::sync::atomic::Ordering::SeqCst), - if family == "domain" && codec == "packetaddr" { - 500 - } else { - 0 - } - ); - let oversized = transport.send(Datagram { - remote: target.clone(), payload: vec![0; maximum + 1].into(), sniffed_domain: None, - }).await; - assert!(oversized.is_err()); - assert!(tokio::time::timeout(Duration::from_millis(30), origin.receive()).await.is_err()); - transport.close().await.unwrap(); - drop(transport); - if let Some(driver) = driver { - driver.stop().await.unwrap(); - } - }) - .await - .expect("encoded UDP matrix deadline"); - } - } - } - } - } - event_for("native_encoded_udp_boundaries", "PASS"); -} - -struct FixtureResolver(std::sync::atomic::AtomicUsize); -#[async_trait::async_trait] -impl vcore::dialer::Resolver for FixtureResolver { - async fn resolve(&self, host: &str, port: u16) -> io::Result { - assert_eq!( - host, "vcore-fixture.test", - "internal magic names must not be resolved" - ); - self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - let address = std::env::var("VCORE_VMESS_ORIGIN_V4") - .unwrap_or_else(|_| "127.0.0.1".into()) - .parse::() - .map_err(|_| io::Error::other("invalid fixture resolver address"))?; - Ok(ResolvedEndpoint { - logical_host: host.into(), - port, - addresses: vec![SocketAddr::new(address, port)], - }) - } -} - -#[tokio::test] -#[ignore = "requires owned official native peer"] -async fn native_mihomo_close_alignment() { - event_for("native_mihomo_close_alignment", "BEGIN"); - tokio::time::timeout(Duration::from_secs(10), async { - let peer: SocketAddr = std::env::var("VCORE_VMESS_PEER").unwrap().parse().unwrap(); - let fixture: serde_json::Value = - serde_json::from_str(&std::env::var("VCORE_VMESS_TRANSPORT").unwrap()).unwrap(); - let mode = fixture["mode"].as_str().unwrap(); - let expected_tail = matches!(mode, "tcp" | "ws"); - let mut origin = origin::TcpOrigin::new(11).await; - let target = origin.target.clone(); - let (mut client, driver) = stream(peer, target, BodyCipher::Auto, false, false) - .await - .unwrap(); - let mut greeting = [0; 5]; - client.read_exact(&mut greeting).await.unwrap(); - assert_eq!(&greeting, b"hello"); - client.write_all(b"ping").await.unwrap(); - client.flush().await.unwrap(); - let mut echo = [0; 4]; - client.read_exact(&mut echo).await.unwrap(); - assert_eq!(&echo, b"ping"); - client.shutdown().await.unwrap(); - let mut tail = Vec::new(); - client.read_to_end(&mut tail).await.unwrap(); - assert_eq!( - tail.as_slice(), - if expected_tail { - b"native-after-upload-eof".as_slice() - } else { - b"" - }, - "close behavior differs from the official Mihomo client" - ); - origin.accepted().await; - origin.completed().await; - drop(client); - if let Some(driver) = driver { - driver.stop().await.unwrap(); - } - }) - .await - .expect("native close differential deadline"); - event_for("native_mihomo_close_alignment", "PASS"); -} - -#[tokio::test] -#[ignore = "requires owned official native peer"] -async fn native_identity_time_replay_rejection() { - event_for("native_identity_time_replay_rejection", "BEGIN"); - let peer: SocketAddr = std::env::var("VCORE_VMESS_PEER").unwrap().parse().unwrap(); - let mut origin = origin::TcpOrigin::new(12).await; - let destination = origin.target.clone(); - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_secs(); - let options = BodyOptions::new(BodyCipher::None, false, false).unwrap(); - let mut replay: Option> = None; - for (id, time, accepted) in [ - (UUID, now, true), - (uuid::Uuid::from_bytes([8; 16]), now, false), - (UUID, now - 600, false), - (UUID, now + 600, false), - (UUID, now, false), - ] { - let handshake = ClientHandshake::with_test_timestamp( - &VmessIdentity::new(id), - Command::Tcp, - &destination, - options, - time, - ) - .unwrap(); - let request = if id == UUID && time == now && !accepted { - replay.as_ref().unwrap() - } else { - handshake.request() - }; - let mut raw = Dialer::default().connect_address(peer).await.unwrap(); - raw.write_all(request).await.unwrap(); - raw.write_all(b"synthetic-probe").await.unwrap(); - if accepted { - replay = Some(handshake.request().to_vec()); - origin.accepted().await; - origin.completed().await; - let mut client = VmessStream::new( - Box::new(raw), - handshake, - tokio::time::Instant::now() + Duration::from_secs(2), - ); - let mut ok = [0; 2]; - client.read_exact(&mut ok).await.unwrap(); - assert_eq!(&ok, b"ok"); - } else { - origin.not_accepted().await; - let mut client = VmessStream::new( - Box::new(raw), - handshake, - tokio::time::Instant::now() + Duration::from_millis(350), - ); - let mut reply = [0; 1]; - assert!( - client.read(&mut reply).await.is_err(), - "rejected authentication returned business data" - ); - } - } - event_for("native_identity_time_replay_rejection", "PASS"); -} diff --git a/tests/vmess_native/origin.rs b/tests/vmess_native/origin.rs deleted file mode 100644 index a4ae2af..0000000 --- a/tests/vmess_native/origin.rs +++ /dev/null @@ -1,62 +0,0 @@ -//! Client-only control for origins owned by the isolated-container harness. -use super::*; - -pub fn fixture() -> serde_json::Value { - let path = std::env::var("VCORE_VMESS_AB_INPUT").expect("isolated origin fixture required"); - let value: serde_json::Value = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap(); - assert_eq!(value["isolation"], "containers"); - value -} - -pub struct TcpOrigin { - control: tokio::net::TcpStream, - pub target: Destination, -} - -impl TcpOrigin { - pub async fn new(mode: u8) -> Self { - let fixture = fixture(); - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.set_nodelay(true).unwrap(); - control.write_u8(mode).await.unwrap(); - let port = control.read_u16().await.unwrap(); - let ip: std::net::IpAddr = fixture["origin_ipv4"].as_str().unwrap().parse().unwrap(); - assert!(!ip.is_loopback() && !ip.is_unspecified()); - Self { - control, - target: SocketAddr::new(ip, port).into(), - } - } - - pub async fn accepted(&mut self) { - assert_eq!( - tokio::time::timeout(Duration::from_secs(2), self.control.read_u8()) - .await - .unwrap() - .unwrap(), - b'A' - ); - } - - pub async fn completed(&mut self) { - assert_eq!( - tokio::time::timeout(Duration::from_secs(2), self.control.read_u8()) - .await - .unwrap() - .unwrap(), - b'D' - ); - } - - pub async fn not_accepted(&mut self) { - assert!( - tokio::time::timeout(Duration::from_millis(350), self.control.read_u8()) - .await - .is_err(), - "rejected authentication reached origin" - ); - } -} diff --git a/tests/vmess_native/udp_ab.rs b/tests/vmess_native/udp_ab.rs deleted file mode 100644 index d1e6038..0000000 --- a/tests/vmess_native/udp_ab.rs +++ /dev/null @@ -1,477 +0,0 @@ -//! Diagnostic only: the same packet loop drives VCore and an official Mihomo client. -//! SOCKS5 below is a small fixture adapter, not VCore's SOCKS implementation. -use super::*; -use serde_json::{Value, json}; -use std::{fs::OpenOptions, io::Write}; -use vcore::{ - dispatch::{DatagramBudget, DatagramTransport}, - dns::resolution::ResolutionContext, - outbound::vmess::VmessDatagram, - session::Datagram, -}; - -enum Client { - Vcore(Box), - Mihomo { - _control: tokio::net::TcpStream, - socket: tokio::net::UdpSocket, - relay: SocketAddr, - }, -} - -pub(super) struct ContainerOrigin { - observer: tokio::net::TcpStream, - pub(super) address: SocketAddr, -} - -impl ContainerOrigin { - pub(super) async fn connect(fixture: &Value, family: &str) -> io::Result { - let control = fixture["origin_control"] - .as_str() - .ok_or_else(|| io::Error::other("isolated origin required"))?; - let mut observer = tokio::net::TcpStream::connect(control).await?; - observer.set_nodelay(true)?; - let ipv6 = family == "ipv6"; - observer.write_u8(if ipv6 { 6 } else { 4 }).await?; - let port = observer.read_u16().await?; - let ip = fixture[if ipv6 { "origin_ipv6" } else { "origin_ipv4" }] - .as_str() - .ok_or_else(|| io::Error::other("isolated origin address missing"))? - .parse::() - .map_err(|_| io::Error::other("invalid isolated origin address"))?; - if ip.is_loopback() || ip.is_unspecified() { - return Err(io::Error::other("host origin fallback forbidden")); - } - Ok(Self { - observer, - address: SocketAddr::new(ip, port), - }) - } - - pub(super) async fn receive(&mut self) -> io::Result<(Vec, u16)> { - let length = self.observer.read_u16().await? as usize; - let source_port = self.observer.read_u16().await?; - if length > 20000 { - return Err(io::Error::other("origin observation exceeds bound")); - } - let mut packet = vec![0; length]; - self.observer.read_exact(&mut packet).await?; - Ok((packet, source_port)) - } -} - -impl Client { - async fn send(&mut self, target: &Destination, payload: &[u8]) -> io::Result<()> { - match self { - Self::Vcore(io) => io - .send(Datagram { - remote: target.clone(), - payload: payload.to_vec().into(), - sniffed_domain: None, - }) - .await - .map_err(io::Error::other), - Self::Mihomo { socket, relay, .. } => { - let mut frame = vec![0, 0, 0]; - match target { - Destination::Ip(address) => match address.ip() { - std::net::IpAddr::V4(ip) => { - frame.push(1); - frame.extend(ip.octets()); - } - std::net::IpAddr::V6(ip) => { - frame.push(4); - frame.extend(ip.octets()); - } - }, - Destination::Domain { host, .. } => { - frame.extend([3, host.len() as u8]); - frame.extend(host.as_bytes()); - } - } - frame.extend(target.port().to_be_bytes()); - frame.extend(payload); - let sent = socket.send_to(&frame, *relay).await?; - if sent != frame.len() { - return Err(io::Error::other("fixture short UDP send")); - } - Ok(()) - } - } - } - - async fn receive(&mut self, target_port: u16) -> io::Result> { - match self { - Self::Vcore(io) => { - let packet = io.receive().await.map_err(io::Error::other)?; - if packet.remote.port() != target_port { - return Err(io::Error::other("response target port")); - } - Ok(packet.payload.to_vec()) - } - Self::Mihomo { socket, relay, .. } => { - let mut frame = vec![0; 20000]; - let (length, source) = socket.recv_from(&mut frame).await?; - frame.truncate(length); - if source != *relay || frame.len() < 4 || frame[..3] != [0, 0, 0] { - return Err(io::Error::other("fixture invalid SOCKS UDP response")); - } - let offset = match frame[3] { - 1 => 8, - 4 => 20, - 3 if frame.len() >= 5 => 5 + frame[4] as usize, - _ => return Err(io::Error::other("fixture address type")), - }; - if frame.len() < offset + 2 - || u16::from_be_bytes([frame[offset], frame[offset + 1]]) != target_port - { - return Err(io::Error::other("fixture response port")); - } - Ok(frame[offset + 2..].to_vec()) - } - } - } -} - -async fn connect( - kind: &str, - option: &Value, - target: &Destination, - maximum: u16, -) -> io::Result<(Client, Option)> { - if kind == "mihomo" { - let port = option["socks_port"].as_u64().unwrap() as u16; - let host = option["socks_host"].as_str().unwrap_or("127.0.0.1"); - let mut control = tokio::net::TcpStream::connect((host, port)).await?; - control.write_all(&[5, 1, 0]).await?; - let mut reply = [0; 2]; - control.read_exact(&mut reply).await?; - if reply != [5, 0] { - return Err(io::Error::other("SOCKS fixture method")); - } - control.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).await?; - let mut reply = [0; 4]; - control.read_exact(&mut reply).await?; - if reply[..3] != [5, 0, 0] || reply[3] != 1 { - return Err(io::Error::other("SOCKS fixture associate")); - } - let mut address = [0; 6]; - control.read_exact(&mut address).await?; - let peer_ip = control.peer_addr()?.ip(); - let bound_ip = std::net::IpAddr::from([address[0], address[1], address[2], address[3]]); - if !bound_ip.is_unspecified() && bound_ip != peer_ip { - return Err(io::Error::other("unexpected SOCKS fixture relay address")); - } - let relay = SocketAddr::new(peer_ip, u16::from_be_bytes([address[4], address[5]])); - let socket = std::net::UdpSocket::bind("0.0.0.0:0")?; - // Only the probe-owned ingress socket: leave peer and host settings alone. - socket2::SockRef::from(&socket).set_send_buffer_size(65536)?; - socket.set_nonblocking(true)?; - return Ok(( - Client::Mihomo { - _control: control, - socket: tokio::net::UdpSocket::from_std(socket)?, - relay, - }, - None, - )); - } - let cipher = match option["cipher"].as_str().unwrap() { - "none" => BodyCipher::None, - "auto" => BodyCipher::Auto, - "aes-128-gcm" => BodyCipher::Aes128Gcm, - "chacha20-poly1305" => BodyCipher::Chacha20Poly1305, - _ => panic!("fixture cipher"), - }; - let codec = option["codec"].as_str().unwrap(); - let (command, destination) = match codec { - "raw" => (Command::Udp, target.clone()), - "xudp" => (Command::Mux, Destination::domain("v1.mux.cool", 443)?), - "packetaddr" => ( - Command::Udp, - Destination::domain("sp.packet-addr.v2fly.arpa", 443)?, - ), - _ => panic!("fixture codec"), - }; - let peer = std::env::var("VCORE_VMESS_PEER").unwrap().parse().unwrap(); - let (stream, driver) = wire_stream( - peer, - destination, - cipher, - option["padding"].as_bool().unwrap(), - option["length"].as_bool().unwrap(), - command, - ) - .await?; - let budget = DatagramBudget::new(maximum, maximum); - let io: Box = match codec { - "raw" => Box::new(VmessDatagram::raw(stream, target.clone(), budget)), - "xudp" => Box::new(vcore::xudp::XudpTransport::new( - Box::new(stream), - [0; 8], - maximum, - )), - _ => Box::new(VmessDatagram::packet_addr( - stream, - budget, - ResolutionContext::measurement( - std::sync::Arc::new(FixtureResolver(std::sync::atomic::AtomicUsize::new(0))), - true, - ), - )), - }; - Ok((Client::Vcore(io), driver)) -} - -#[tokio::test] -#[ignore = "requires owned official Mihomo client and server; diagnostic only"] -async fn matched_clients() { - let fixture: Value = serde_json::from_str( - &std::fs::read_to_string(std::env::var("VCORE_VMESS_AB_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!( - fixture["isolation"], "containers", - "native server fallback forbidden" - ); - let events = std::env::var("VCORE_VMESS_AB_EVENTS").unwrap(); - let mut events = OpenOptions::new() - .create_new(true) - .write(true) - .open(events) - .unwrap(); - let sizes = fixture["sizes"].as_array().unwrap(); - let maximum = if fixture["include_boundary"].as_bool().unwrap_or(false) { - vcore::outbound::vmess::MAX_PACKET_BYTES as u16 - } else { - sizes.iter().map(|v| v.as_u64().unwrap()).max().unwrap() as u16 - }; - let repeats = fixture["packets_per_size"].as_u64().unwrap(); - let rounds = fixture["rounds"].as_u64().unwrap(); - let mut ordinal = 0usize; - let mut failures = 0; - // Mihomo's SOCKS NAT key is only the incoming UDP source tuple. Keep each - // probe-owned socket reserved until this transport ends so another case - // cannot inherit an earlier case's codec/destination. No peer setting changes. - let mut retired_sources = Vec::new(); - for round in 0..rounds { - for (index, option) in fixture["options"].as_array().unwrap().iter().enumerate() { - for (family_index, family) in fixture["families"].as_array().unwrap().iter().enumerate() - { - let family = family.as_str().unwrap(); - let mut case_sizes: Vec = sizes - .iter() - .map(|size| size.as_u64().unwrap() as usize) - .collect(); - if fixture["include_boundary"].as_bool().unwrap_or(false) { - let overhead = if option["codec"] == "packetaddr" { - if family == "ipv4" { 7 } else { 19 } - } else { - 0 - }; - case_sizes.push(vcore::outbound::vmess::MAX_PACKET_BYTES - overhead); - } - let clients = if (round as usize + index + family_index).is_multiple_of(2) { - ["vcore", "mihomo"] - } else { - ["mihomo", "vcore"] - }; - for kind in clients { - ordinal += 1; - let mut origin = tokio::time::timeout( - Duration::from_secs(5), - ContainerOrigin::connect(&fixture, family), - ) - .await - .unwrap() - .unwrap(); - let address = origin.address; - let target = if family == "domain" { - Destination::domain("vcore-fixture.test", address.port()).unwrap() - } else { - address.into() - }; - let mut record = json!({"ordinal":ordinal,"round":round,"client":kind,"option":index,"family":family,"origin_port":address.port(),"completed":0,"status":"FAIL","phase":"setup"}); - let mut client = None; - let mut driver = None; - let started = std::time::Instant::now(); - let outcome: io::Result<()> = async { - let connected = tokio::time::timeout( - Duration::from_secs(10), - connect(kind, option, &target, maximum), - ) - .await??; - client = Some(connected.0); - driver = connected.1; - let client = client.as_mut().unwrap(); - if let Client::Mihomo { socket, .. } = &*client { - record["fixture_source_port"] = socket.local_addr()?.port().into(); - } - for &size in &case_sizes { - for sequence in 0..repeats { - record["size"] = size.into(); - record["sequence"] = sequence.into(); - let packet = vec![sequence as u8; size]; - record["phase"] = "send".into(); - tokio::time::timeout( - Duration::from_secs(1), - client.send(&target, &packet), - ) - .await??; - record["phase"] = "origin".into(); - let (received, source_port) = - tokio::time::timeout(Duration::from_secs(1), origin.receive()) - .await??; - record["server_udp_port"] = source_port.into(); - if received != packet { - return Err(io::Error::other("origin content")); - } - // The isolated origin sends its own echo, not the host. - record["phase"] = "reply".into(); - let echo = tokio::time::timeout( - Duration::from_secs(1), - client.receive(address.port()), - ) - .await??; - if echo != packet { - return Err(io::Error::other("response content")); - } - record["completed"] = - (record["completed"].as_u64().unwrap() + 1).into(); - } - } - Ok(()) - } - .await; - if let Err(error) = outcome { - failures += 1; - record["error_kind"] = format!("{:?}", error.kind()).into(); - println!( - "UDP-AB failure: client={kind} round={round} option={index} family={family} phase={} size={} sequence={}", - record["phase"], record["size"], record["sequence"] - ); - } else { - record["status"] = "PASS".into(); - record["phase"] = "complete".into(); - } - record["elapsed_ms"] = (started.elapsed().as_millis() as u64).into(); - if let Some(Client::Mihomo { socket, .. }) = client.take() { - retired_sources.push(socket); - } - if let Some(driver) = driver { - record["driver_joined"] = driver.stop().await.is_ok().into(); - } - writeln!(events, "{record}").unwrap(); - events.flush().unwrap(); - } - } - } - } - let ports: std::collections::HashSet<_> = retired_sources - .iter() - .map(|socket| socket.local_addr().unwrap().port()) - .collect(); - assert_eq!( - ports.len(), - retired_sources.len(), - "fixture source port reused" - ); - drop(retired_sources); - assert_eq!( - failures, 0, - "matched UDP client associations failed; inspect structured observations" - ); -} - -/// Change only the SOCKS ingress source tuple: a retained native NAT entry can -/// send a later fixture case to the earlier raw-UDP destination. All service -/// roles and both origins remain in containers. -#[tokio::test] -#[ignore = "requires isolated official peer and origins; expected-failure diagnostic"] -async fn socks_nat_source_reuse() { - let fixture: Value = serde_json::from_str( - &std::fs::read_to_string(std::env::var("VCORE_VMESS_AB_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - let mut events = OpenOptions::new() - .create_new(true) - .write(true) - .open(std::env::var("VCORE_VMESS_AB_EVENTS").unwrap()) - .unwrap(); - tokio::time::timeout(Duration::from_secs(15), async { - let mut first = ContainerOrigin::connect(&fixture, "ipv4").await.unwrap(); - let mut second = ContainerOrigin::connect(&fixture, "ipv4").await.unwrap(); - let a: Destination = first.address.into(); - let b: Destination = second.address.into(); - let (mut seed, _) = connect("mihomo", &fixture["options"][0], &a, 1200) - .await - .unwrap(); - seed.send(&a, &[0x41]).await.unwrap(); - assert_eq!(first.receive().await.unwrap().0, [0x41]); - assert_eq!(seed.receive(first.address.port()).await.unwrap(), [0x41]); - let seed_port = match &seed { - Client::Mihomo { socket, .. } => socket.local_addr().unwrap().port(), - _ => unreachable!(), - }; - writeln!( - events, - "{}", - json!({"ordinal":1,"client":"mihomo","phase":"seed", - "status":"PASS","completed":1,"fixture_source_port":seed_port}) - ) - .unwrap(); - - let (mut reused, _) = connect("mihomo", &fixture["options"][13], &b, 1200) - .await - .unwrap(); - match (&mut seed, &mut reused) { - (Client::Mihomo { socket: old, .. }, Client::Mihomo { socket: new, .. }) => { - std::mem::swap(old, new) - } - _ => unreachable!(), - } - reused.send(&b, &[0x42]).await.unwrap(); - let stale = tokio::time::timeout(Duration::from_secs(1), first.receive()).await; - let misdirected = matches!(stale, Ok(Ok((ref packet, _))) if packet == &[0x42]); - let absent = tokio::time::timeout(Duration::from_secs(1), second.receive()) - .await - .is_err(); - writeln!( - events, - "{}", - json!({"ordinal":2,"client":"mihomo","phase":"source-reused", - "status":if misdirected && absent {"REPRODUCED"} else {"FAIL"}, - "completed":0,"fixture_source_port":seed_port, - "old_origin_received_new_payload":misdirected,"new_origin_received_nothing":absent}) - ) - .unwrap(); - events.flush().unwrap(); - assert!( - misdirected && absent, - "native NAT source reuse was not reproduced" - ); - - let (mut fresh, _) = connect("mihomo", &fixture["options"][13], &b, 1200) - .await - .unwrap(); - let fresh_port = match &fresh { - Client::Mihomo { socket, .. } => socket.local_addr().unwrap().port(), - _ => unreachable!(), - }; - assert_ne!(fresh_port, seed_port); - fresh.send(&b, &[0x43]).await.unwrap(); - assert_eq!(second.receive().await.unwrap().0, [0x43]); - assert_eq!(fresh.receive(second.address.port()).await.unwrap(), [0x43]); - writeln!( - events, - "{}", - json!({"ordinal":3,"client":"mihomo","phase":"unique-source-control", - "status":"PASS","completed":1,"fixture_source_port":fresh_port}) - ) - .unwrap(); - events.flush().unwrap(); - }) - .await - .unwrap(); -} diff --git a/tests/vmess_public.rs b/tests/vmess_public.rs deleted file mode 100644 index c046f17..0000000 --- a/tests/vmess_public.rs +++ /dev/null @@ -1,584 +0,0 @@ -//! Public VMess consumer gate. Every origin/peer is owned by the container runner. -#![cfg(all(feature = "ffi", feature = "outbound-vmess", feature = "interop-test"))] -use serde_json::{Value, json}; -use sha2::{Digest, Sha256}; -use std::{ - ffi::{CStr, CString}, - io::{self, Read, Write}, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, TcpStream, UdpSocket}, - thread, - time::{Duration, Instant}, -}; -use vcore::resources::case_events::Case; -const TIMEOUT: Duration = Duration::from_secs(10); -const DOMAIN: &str = "vcore-fixture.test"; - -#[path = "vmess_public/runtime.rs"] -mod runtime; -#[cfg(target_os = "macos")] -#[path = "vmess_public/tun.rs"] -mod tun; - -fn fixture() -> Value { - let value: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_VMESS_AB_INPUT").expect("use isolated VMESS runner")) - .unwrap(), - ) - .unwrap(); - assert_eq!(value["isolation"], "containers"); - value -} -fn initialize(f: &Value) { - invoke("initialize", None, json!({"dataDir": f["data_dir"]})); -} -fn invoke(method: &str, id: Option<&str>, payload: Value) -> Value { - let response = invoke_response(method, id, payload); - assert_eq!( - response["success"], true, - "Invoke {method}: {}", - response["error"] - ); - response["data"].clone() -} -fn invoke_response(method: &str, id: Option<&str>, payload: Value) -> Value { - let mut input = - json!({"apiVersion":vcore::INVOKE_API_VERSION,"method":method,"payload":payload}); - if let Some(id) = id { - input["instanceId"] = json!(id); - } - let input = CString::new(input.to_string()).unwrap(); - // SAFETY: NUL-terminated input lives for the call; returned allocation is freed once. - let result = unsafe { vcore::ffi::VCoreInvoke(input.as_ptr()) }; - assert!(!result.is_null()); - let bytes = unsafe { CStr::from_ptr(result) }.to_bytes().to_vec(); - unsafe { vcore::ffi::VCoreFree(result) }; - serde_json::from_slice(&bytes).unwrap() -} -struct Core(Option); -impl Core { - fn prepare(config: &Value) -> Self { - invoke( - "validateConfig", - None, - json!({"configYaml":config.to_string()}), - ); - let id = invoke("createInstance", None, json!({}))["instanceId"] - .as_str() - .unwrap() - .to_owned(); - let core = Self(Some(id)); - invoke( - "prepare", - core.0.as_deref(), - json!({"configYaml":config.to_string()}), - ); - core - } - fn start(config: &Value) -> Self { - let core = Self::prepare(config); - invoke("start", core.0.as_deref(), json!({})); - core - } - fn stop(mut self) { - stop(self.0.take().unwrap()); - } -} -fn stop(id: String) { - let (tx, rx) = std::sync::mpsc::sync_channel(1); - let worker = thread::spawn(move || { - let result = std::panic::catch_unwind(|| { - invoke("stop", Some(&id), json!({})); - invoke("destroyInstance", Some(&id), json!({})); - }); - tx.send(result).unwrap(); - }); - let result = rx - .recv_timeout(Duration::from_secs(5)) - .expect("Stop exceeds 5 seconds"); - worker.join().unwrap(); - if let Err(panic) = result { - std::panic::resume_unwind(panic); - } -} -impl Drop for Core { - fn drop(&mut self) { - if let Some(id) = self.0.take() { - let _ = std::panic::catch_unwind(|| stop(id)); - } - } -} -fn free_port() -> u16 { - for _ in 0..32 { - let tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let port = tcp.local_addr().unwrap().port(); - if let (Ok(_v6), Ok(_udp), Ok(_udp6)) = ( - TcpListener::bind((Ipv6Addr::LOCALHOST, port)), - UdpSocket::bind((Ipv4Addr::LOCALHOST, port)), - UdpSocket::bind((Ipv6Addr::LOCALHOST, port)), - ) { - return port; - } - } - panic!("cannot reserve fixture port"); -} -fn socket(address: SocketAddr) -> TcpStream { - let io = TcpStream::connect_timeout(&address, TIMEOUT).unwrap(); - io.set_read_timeout(Some(TIMEOUT)).unwrap(); - io.set_write_timeout(Some(TIMEOUT)).unwrap(); - io.set_nodelay(true).unwrap(); - io -} -fn config(node: Value, port: u16) -> Value { - json!({"socks-port":port,"ipv6":true,"proxies":[node],"rules":["MATCH,peer"]}) -} -fn login(port: u16) -> TcpStream { - let mut client = socket((Ipv4Addr::LOCALHOST, port).into()); - client.write_all(&[5, 1, 0]).unwrap(); - let mut reply = [0; 2]; - client.read_exact(&mut reply).unwrap(); - assert_eq!(reply, [5, 0]); - client -} -fn address(target: SocketAddr, domain: bool) -> Vec { - let mut bytes = Vec::new(); - if domain { - bytes.extend([3, DOMAIN.len() as u8]); - bytes.extend(DOMAIN.as_bytes()); - } else { - match target.ip() { - IpAddr::V4(ip) => { - bytes.push(1); - bytes.extend(ip.octets()); - } - IpAddr::V6(ip) => { - bytes.push(4); - bytes.extend(ip.octets()); - } - } - } - bytes.extend(target.port().to_be_bytes()); - bytes -} -fn reply(client: &mut TcpStream) -> SocketAddr { - let mut header = [0; 4]; - client.read_exact(&mut header).unwrap(); - assert_eq!(&header[..3], &[5, 0, 0]); - let ip = match header[3] { - 1 => { - let mut bytes = [0; 4]; - client.read_exact(&mut bytes).unwrap(); - IpAddr::from(bytes) - } - 4 => { - let mut bytes = [0; 16]; - client.read_exact(&mut bytes).unwrap(); - IpAddr::from(bytes) - } - _ => panic!("SOCKS response address"), - }; - let mut port = [0; 2]; - client.read_exact(&mut port).unwrap(); - SocketAddr::new(ip, u16::from_be_bytes(port)) -} -fn connect(port: u16, target: SocketAddr, domain: bool) -> TcpStream { - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(target, domain)); - client.write_all(&request).unwrap(); - reply(&mut client); - client -} -struct Origin { - observer: TcpStream, - target: SocketAddr, -} -impl Origin { - fn new(f: &Value, mode: u8, ipv6: bool) -> Self { - let mut observer = socket(f["origin_control"].as_str().unwrap().parse().unwrap()); - observer - .write_all(&[mode | if ipv6 && mode >= 10 { 128 } else { 0 }]) - .unwrap(); - let mut port = [0; 2]; - observer.read_exact(&mut port).unwrap(); - let ip: IpAddr = f[if ipv6 { "origin_ipv6" } else { "origin_ipv4" }] - .as_str() - .unwrap() - .parse() - .unwrap(); - assert!(!ip.is_loopback() && !ip.is_unspecified()); - Self { - observer, - target: SocketAddr::new(ip, u16::from_be_bytes(port)), - } - } - fn marker(&mut self, expected: u8) { - let mut byte = [0]; - self.observer.read_exact(&mut byte).unwrap(); - assert_eq!(byte[0], expected); - } - fn quiet(&mut self) { - self.observer - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let error = self.observer.read(&mut [0; 1]).unwrap_err(); - assert!(matches!( - error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut - )); - self.observer.set_read_timeout(Some(TIMEOUT)).unwrap(); - } - fn udp(&mut self, expected: &[u8]) { - let mut header = [0; 4]; - self.observer.read_exact(&mut header).unwrap(); - assert_ne!(&header[2..], &[0, 0]); - let length = u16::from_be_bytes([header[0], header[1]]) as usize; - assert_eq!(length, expected.len()); - let mut bytes = vec![0; length]; - self.observer.read_exact(&mut bytes).unwrap(); - assert_eq!(bytes, expected); - } -} -struct Association { - control: TcpStream, - client: UdpSocket, - relay: SocketAddr, - origin: Origin, - domain: bool, -} -impl Association { - fn new(f: &Value, port: u16, ipv6: bool, domain: bool) -> Self { - let client = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - client.set_read_timeout(Some(TIMEOUT)).unwrap(); - socket2::SockRef::from(&client) - .set_send_buffer_size(65536) - .unwrap(); - let mut control = login(port); - let mut request = vec![5, 3, 0]; - request.extend(address(client.local_addr().unwrap(), false)); - control.write_all(&request).unwrap(); - let relay = reply(&mut control); - Self { - control, - client, - relay, - origin: Origin::new(f, if ipv6 { 6 } else { 4 }, ipv6), - domain, - } - } - fn packet(&self, payload: &[u8]) -> Vec { - let mut packet = vec![0, 0, 0]; - packet.extend(address(self.origin.target, self.domain)); - packet.extend(payload); - packet - } - fn exchange(&mut self, payload: &[u8]) { - let packet = self.packet(payload); - assert_eq!( - self.client.send_to(&packet, self.relay).unwrap(), - packet.len() - ); - self.origin.udp(payload); - let mut bytes = [0; 20000]; - let (size, source) = self.client.recv_from(&mut bytes).unwrap(); - assert_eq!(source, self.relay); - assert_eq!(&bytes[..3], &[0, 0, 0]); - let offset = match bytes[3] { - 1 => 8, - 4 => 20, - 3 => 5 + bytes[4] as usize, - _ => panic!("UDP response type"), - }; - assert_eq!( - &bytes[offset..offset + 2], - &self.origin.target.port().to_be_bytes() - ); - let literal = address(self.origin.target, false); - let named = address(self.origin.target, self.domain); - assert!(bytes[3..offset + 2] == literal || bytes[3..offset + 2] == named); - assert_eq!(&bytes[offset + 2..size], payload); - } -} -fn echo(port: u16, f: &Value) { - let mut origin = Origin::new(f, 13, false); - let mut client = connect(port, origin.target, false); - client.write_all(b"client-first").unwrap(); - let mut bytes = [0; 12]; - client.read_exact(&mut bytes).unwrap(); - assert_eq!(&bytes, b"client-first"); - origin.marker(b'A'); - drop(client); - origin.marker(b'D'); -} -fn bulk(port: u16, f: &Value, ipv6: bool, domain: bool) { - let _case = Case::new("VMESS-BASE", "tcp_10mib_both_directions"); - let mut origin = Origin::new(f, 10, ipv6); - let mut client = connect(port, origin.target, domain); - let mut hello = [0; 5]; - client.read_exact(&mut hello).unwrap(); - assert_eq!(&hello, b"hello"); - origin.marker(b'A'); - let data = vec![0x5a; 10 * 1024 * 1024]; - for fragment in data.chunks(4093) { - client.write_all(fragment).unwrap(); - } - let mut hash = Sha256::new(); - let mut remaining = data.len(); - let mut bytes = [0; 6151]; - while remaining > 0 { - let size = bytes.len().min(remaining); - let n = client.read(&mut bytes[..size]).unwrap(); - assert_ne!(n, 0); - hash.update(&bytes[..n]); - remaining -= n; - } - assert_eq!(hash.finalize(), Sha256::digest(&data)); - let mut trailer = [0; 7]; - client.read_exact(&mut trailer).unwrap(); - assert_eq!(&trailer, b"trailer"); - client.shutdown(std::net::Shutdown::Write).unwrap(); - assert_eq!(client.read(&mut [0]).unwrap(), 0); - origin.marker(b'D'); -} -fn dns(config: &mut Value, origin: &Origin, via: &str) { - config["dns"] = - json!({"enable":true,"ipv6":false,"nameserver":[format!("udp://{}#{via}",origin.target)]}); -} -#[test] -#[ignore = "isolated VMESS runner"] -fn public_base() { - let _case = Case::new("VMESS-PUBLIC", "public_base"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let mut dns_origin = Origin::new(&f, 17, false); - let mut yaml = config(f["node"].clone(), port); - dns(&mut yaml, &dns_origin, "DIRECT"); - let core = Core::start(&yaml); - for (v6, domain) in [(false, false), (true, false), (false, true)] { - bulk(port, &f, v6, domain); - } - echo(port, &f); - core.stop(); - for codec in ["", "xudp", "packetaddr"] { - let _codec = Case::new("VMESS-BASE", "udp_each_codec_and_family"); - let mut node = f["node"].clone(); - node["packet-encoding"] = json!(codec); - let mut yaml = config(node, port); - dns(&mut yaml, &dns_origin, "DIRECT"); - let core = Core::start(&yaml); - // Keep source sockets alive across all three cases, preventing peer NAT - // source-tuple reuse from aliasing a previous association. - let mut associations = Vec::new(); - for (v6, domain) in [(false, false), (true, false), (false, true)] { - let mut association = Association::new(&f, port, v6, domain); - let cap = if f["peer_kind"] == "V2" { - if codec == "xudp" { - 2048 - } else { - 2030 - (if codec == "packetaddr" { - if v6 || domain { 19 } else { 7 } - } else { - 0 - }) - } - } else { - 15000 - - (if codec == "packetaddr" { - if v6 || domain { 19 } else { 7 } - } else { - 0 - }) - }; - for size in [1, 64, 512, 1200, cap] { - for sequence in 0..100u8 { - let payload = vec![sequence; size]; - association.exchange(&payload); - } - } - associations.push(association); - } - core.stop(); - drop(associations); - } - // packetaddr's business-domain lookup was answered by this controlled DNS - // endpoint. Magic names cause the fixture to close, never a system lookup. - let mut header = [0; 4]; - dns_origin.observer.read_exact(&mut header).unwrap(); - let n = u16::from_be_bytes([header[0], header[1]]) as usize; - let mut query = vec![0; n]; - dns_origin.observer.read_exact(&mut query).unwrap(); - assert_eq!(&query[12..], b"\x0dvcore-fixture\x04test\0\0\x01\0\x01"); - let mut origin = Origin::new(&f, 14, false); - let result = invoke( - "measureDelay", - None, - json!({"configYamls":[json!({"proxies":[f["node"]]}).to_string()],"timeout":5,"url":format!("http://{}/",origin.target)}), - ); - assert_eq!(result["results"][0]["success"], true); - origin.marker(b'A'); - origin.marker(b'D'); -} - -fn denied(node: Value, f: &Value) { - let mut origin = Origin::new(f, 13, false); - let port = free_port(); - let core = Core::start(&config(node, port)); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - request.extend(b"no-origin-bytes"); - client.write_all(&request).unwrap(); - client - .set_read_timeout(Some(Duration::from_millis(500))) - .unwrap(); - let mut response = [0; 4]; - if client.read_exact(&mut response).is_ok() && response[1] == 0 { - let mut tail = vec![0; if response[3] == 1 { 6 } else { 18 }]; - client.read_exact(&mut tail).unwrap(); - assert!( - !matches!(client.read(&mut [0;32]),Ok(n) if n>0), - "rejected session delivered business data" - ); - } - drop(client); - origin.quiet(); - core.stop(); -} -#[test] -#[ignore = "isolated VMESS runner"] -fn public_negative() { - let _case = Case::new("VMESS-PUBLIC", "public_negative"); - let f = fixture(); - initialize(&f); - let original = f["node"].clone(); - let port = free_port(); - let core = Core::start(&config(original.clone(), port)); - echo(port, &f); - core.stop(); - let mut bad = original.clone(); - bad["uuid"] = json!("08080808-0808-0808-0808-080808080808"); - denied(bad, &f); - if original["tls"] == true { - let mut untrusted = original.clone(); - untrusted.as_object_mut().unwrap().remove("fingerprint"); - denied(untrusted.clone(), &f); - let mut bad = original.clone(); - bad["fingerprint"] = json!("00".repeat(32)); - bad["skip-cert-verify"] = json!(true); - denied(bad, &f); - let mut bad = original.clone(); - bad["servername"] = json!("wrong.invalid"); - denied(bad.clone(), &f); - bad["skip-cert-verify"] = json!(true); - denied(bad, &f); - if original["network"] == "ws" { - let mut fallback = original.clone(); - fallback.as_object_mut().unwrap().remove("servername"); - let port = free_port(); - let core = Core::start(&config(fallback, port)); - echo(port, &f); - core.stop(); - } - // No pin: skip-cert-verify is observable, not silently ignored. - untrusted["skip-cert-verify"] = json!(true); - let port = free_port(); - let core = Core::start(&config(untrusted, port)); - echo(port, &f); - core.stop(); - // A matching leaf pin intentionally replaces PKI/name checks, per the - // existing StandardTlsClient contract. A nonmatching pin cannot bypass. - } - let mut bad = original.clone(); - match original["network"].as_str().unwrap() { - "ws" => bad["ws-opts"]["path"] = json!("/wrong"), - "grpc" => bad["grpc-opts"]["grpc-service-name"] = json!("wrong"), - "h2" => bad["h2-opts"]["host"] = json!(["wrong.invalid"]), - "http" => bad["http-opts"]["path"] = json!(["/wrong"]), - _ => return, - }; - denied(bad, &f); - if original["network"] == "h2" { - let mut bad = original; - bad["h2-opts"]["path"] = json!("/wrong"); - denied(bad, &f); - } -} - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_alpn_rejection() { - let _case = Case::new("VMESS-PUBLIC", "public_alpn_rejection"); - let f = fixture(); - initialize(&f); - // The official listener advertises h2 when its gRPC entrance is enabled. - // A valid gRPC control proves authentication/origin readiness first. - let mut control = f["node"].clone(); - control["network"] = json!("grpc"); - control["alpn"] = json!(["h2"]); - control.as_object_mut().unwrap().remove("ws-opts"); - control["grpc-opts"] = json!({"grpc-service-name":"vmess-alpn"}); - let port = free_port(); - let core = Core::start(&config(control, port)); - echo(port, &f); - core.stop(); - denied(f["node"].clone(), &f); -} - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_udp_first_response() { - let _case = Case::new("VMESS-PUBLIC", "public_udp_first_response"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - let mut association = Association::new(&f, port, false, false); - association.exchange(&vec![1; 15000]); - core.stop(); -} - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_body_options() { - let _case = Case::new("VMESS-PUBLIC", "public_body_options"); - let f = fixture(); - initialize(&f); - let port = free_port(); - for cipher in ["auto", "aes-128-gcm", "chacha20-poly1305", "none", "zero"] { - for flags in 0..if cipher == "none" || cipher == "zero" { - 1 - } else { - 4 - } { - let _body = Case::new("VMESS-BODY", "config_controls_aead_body"); - let mut node = f["node"].clone(); - node["cipher"] = json!(cipher); - node["global-padding"] = json!(flags & 1 != 0); - node["authenticated-length"] = json!(flags & 2 != 0); - let core = Core::start(&config(node, port)); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"configured-body-options"); - core.stop(); - } - } -} - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_legacy_regression() { - let _case = Case::new("VMESS-PUBLIC", "public_legacy_regression"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let core = Core::start(&config(f["node"].clone(), port)); - echo(port, &f); - bulk(port, &f, false, false); - let mut udp = Association::new(&f, port, false, false); - for size in [1, 64, 512, 1200, 8192] { - for n in 0..100u8 { - udp.exchange(&vec![n; size]); - } - } - core.stop(); -} diff --git a/tests/vmess_public/runtime.rs b/tests/vmess_public/runtime.rs deleted file mode 100644 index ef18d9f..0000000 --- a/tests/vmess_public/runtime.rs +++ /dev/null @@ -1,541 +0,0 @@ -use super::*; - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_udp_isolation() { - let _case = Case::new("VMESS-PUBLIC", "runtime::public_udp_isolation"); - let f = fixture(); - initialize(&f); - let port = free_port(); - for codec in ["", "xudp", "packetaddr"] { - let mut node = f["node"].clone(); - node["packet-encoding"] = json!(codec); - let core = Core::start(&config(node, port)); - let mut first = Association::new(&f, port, false, false); - let mut second = Association::new(&f, port, false, false); - for n in 0..100 { - first.exchange(&[1, n]); - second.exchange(&[2, n]); - } - let rogue = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - rogue - .send_to(&first.packet(b"unauthorized"), first.relay) - .unwrap(); - first.origin.quiet(); - let cap = if codec == "packetaddr" { 14993 } else { 15000 }; - first - .client - .send_to(&first.packet(&vec![0; cap + 1]), first.relay) - .unwrap(); - first.origin.quiet(); - second.exchange(b"oversize-isolated"); - // A different target in the same association must not be delivered to - // the original raw target. Encoded transports carry the new target. - let mut other = Origin::new(&f, 4, false); - let mut packet = vec![0, 0, 0]; - packet.extend(address(other.target, false)); - packet.extend(b"other-target"); - second.client.send_to(&packet, second.relay).unwrap(); - if codec.is_empty() { - other.quiet(); - } else { - other.udp(b"other-target"); - let mut response = [0; 128]; - let (n, _) = second.client.recv_from(&mut response).unwrap(); - assert!(response[..n].ends_with(b"other-target")); - } - second.origin.quiet(); - core.stop(); - } -} - -#[test] -#[cfg(target_os = "macos")] -#[ignore = "isolated VMESS runner"] -fn public_entrypoints() { - use std::os::{fd::AsRawFd, unix::net::UnixDatagram}; - let _case = Case::new("VMESS-PUBLIC", "runtime::public_entrypoints"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let yaml = json!({"port":port,"proxies":[f["node"]],"rules":["MATCH,peer"]}); - let core = Core::start(&yaml); - let mut origin = Origin::new(&f, 14, false); - let mut client = socket((Ipv4Addr::LOCALHOST, port).into()); - write!( - client, - "GET http://{}/ HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n", - origin.target, origin.target - ) - .unwrap(); - let mut response = Vec::new(); - client.read_to_end(&mut response).unwrap(); - assert!(response.starts_with(b"HTTP/1.1 200")); - origin.marker(b'A'); - origin.marker(b'D'); - let mut origin = Origin::new(&f, 13, false); - let mut client = socket((Ipv4Addr::LOCALHOST, port).into()); - write!( - client, - "CONNECT {} HTTP/1.1\r\nHost: {}\r\n\r\n", - origin.target, origin.target - ) - .unwrap(); - let mut header = Vec::new(); - while !header.ends_with(b"\r\n\r\n") { - let mut byte = [0]; - client.read_exact(&mut byte).unwrap(); - header.extend(byte); - assert!(header.len() < 4096); - } - assert!(header.starts_with(b"HTTP/1.1 200")); - exchange(&mut client, b"http-connect"); - origin.marker(b'A'); - core.stop(); - assert_closed(&mut client); - origin.marker(b'D'); - drop((client, origin)); - let (host, peer) = UnixDatagram::pair().unwrap(); - host.set_nonblocking(true).unwrap(); - peer.set_read_timeout(Some(TIMEOUT)).unwrap(); - let mut dns_origin = Origin::new(&f, 17, false); - let mut yaml = json!({"tun":{"enable":true},"proxies":[f["node"]],"rules":["MATCH,peer"]}); - dns(&mut yaml, &dns_origin, "peer"); - let core = Core::prepare(&yaml); - invoke( - "start", - core.0.as_deref(), - json!({"tunFd":host.as_raw_fd(),"tunFraming":"utun"}), - ); - let mut udp = Origin::new(&f, 4, false); - peer.send(&tun::packet(udp.target, 17, (0, 0, 0), b"tun-udp")) - .unwrap(); - udp.udp(b"tun-udp"); - assert_eq!(&tun::receive(&peer, 17)[28..], b"tun-udp"); - let mut origin = Origin::new(&f, 13, false); - peer.send(&tun::packet(origin.target, 6, (1, 0, 2), &[])) - .unwrap(); - let syn = tun::receive(&peer, 6); - assert_eq!(syn[33] & 0x12, 0x12); - let ack = u32::from_be_bytes(syn[24..28].try_into().unwrap()).wrapping_add(1); - peer.send(&tun::packet(origin.target, 6, (2, ack, 0x18), b"tun-tcp")) - .unwrap(); - let mut reply = Vec::new(); - while reply.len() < 7 { - let packet = tun::receive(&peer, 6); - let offset = 20 + usize::from(packet[32] >> 4) * 4; - reply.extend_from_slice(&packet[offset..]); - } - assert_eq!(reply, b"tun-tcp"); - origin.marker(b'A'); - let query=b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0dvcore-fixture\x04test\x00\x00\x01\x00\x01"; - peer.send(&tun::packet( - "198.18.0.1:53".parse().unwrap(), - 17, - (0, 0, 0), - query, - )) - .unwrap(); - // DNS rewrites the query ID; observe the question on the native peer path. - let mut size = [0; 4]; - dns_origin.observer.read_exact(&mut size).unwrap(); - let mut observed = vec![0; u16::from_be_bytes([size[0], size[1]]) as usize]; - dns_origin.observer.read_exact(&mut observed).unwrap(); - assert_eq!(&observed[12..], &query[12..]); - let response = tun::receive(&peer, 17); - assert_eq!(&response[28..30], &query[..2]); - assert!(response.ends_with(&match udp.target.ip() { - IpAddr::V4(ip) => ip.octets(), - _ => unreachable!(), - })); - core.stop(); - origin.marker(b'D'); - assert!(unsafe { libc::fcntl(host.as_raw_fd(), libc::F_GETFL) } & libc::O_NONBLOCK != 0); -} - -fn fd_count() -> usize { - std::fs::read_dir("/dev/fd").unwrap().count() -} -fn assert_closed(client: &mut TcpStream) { - match client.read(&mut [0; 32]) { - Ok(0) => {} - Err(error) - if matches!( - error.kind(), - io::ErrorKind::ConnectionReset | io::ErrorKind::BrokenPipe - ) => {} - other => panic!("stream not closed: {other:?}"), - } -} -fn live(port: u16, f: &Value) -> (TcpStream, Origin) { - let mut origin = Origin::new(f, 13, false); - let mut client = connect(port, origin.target, false); - exchange(&mut client, b"live"); - origin.marker(b'A'); - (client, origin) -} -fn exchange(client: &mut TcpStream, bytes: &[u8]) { - client.write_all(bytes).unwrap(); - let mut result = vec![0; bytes.len()]; - client.read_exact(&mut result).unwrap(); - assert_eq!(result, bytes); -} - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_lifecycle() { - let _case = Case::new("VMESS-PUBLIC", "runtime::public_lifecycle"); - let f = fixture(); - initialize(&f); - Core::start(&config(f["node"].clone(), free_port())).stop(); - for cycle in 0..20 { - let _cycle = Case::new("VMESS-LIFE", "stop_and_remain_quiet"); - let baseline = fd_count(); - let port = free_port(); - let mut held = None; - let mut udp = None; - let mut blackhole = None; - match cycle % 5 { - 0 => { - let core = Core::start(&config(f["node"].clone(), port)); - echo(port, &f); - core.stop(); - } - 3 => { - let occupied = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let core = Core::prepare(&config(f["node"].clone(), port)); - assert_eq!( - invoke_response("start", core.0.as_deref(), json!({}))["success"], - false - ); - core.stop(); - drop(occupied); - } - 2 => { - let mut origin = Origin::new(&f, 15, false); - let mut node = f["node"].clone(); - node["server"] = json!(origin.target.ip().to_string()); - node["port"] = json!(origin.target.port()); - let core = Core::start(&config(node, port)); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - client.write_all(&request).unwrap(); - origin.marker(b'A'); - core.stop(); - let _ = client.read(&mut [0; 32]); - drop(client); - origin.marker(b'D'); - blackhole = Some(origin); - } - _ => { - let core = Core::start(&config(f["node"].clone(), port)); - let (first, mut origin1) = live(port, &f); - let (mut second, origin2) = live(port, &f); - let mut one = Association::new(&f, port, false, false); - let mut two = Association::new(&f, port, false, false); - one.exchange(b"association-1"); - two.exchange(b"association-2"); - drop(first); - origin1.marker(b'D'); - exchange(&mut second, b"sibling-survives"); - one.exchange(b"udp-survives"); - two.exchange(b"udp-other"); - drop(origin1); - core.stop(); - assert_closed(&mut second); - let mut origin2 = origin2; - origin2.marker(b'D'); - assert_closed(&mut one.control); - assert_closed(&mut two.control); - held = Some((second, origin2)); - udp = Some((one, two)); - } - } - // All retained descriptors belong to the test, never the runtime. Check - // immediately on Stop return, not after giving cleanup a grace period. - let retained = usize::from(held.is_some()) * 2 - + usize::from(udp.is_some()) * 6 - + usize::from(blackhole.is_some()); - assert!( - fd_count() <= baseline + retained, - "descriptors retained at Stop return" - ); - let tcp = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let udp_guard = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).unwrap(); - let quiet = Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert!(fd_count() <= baseline + retained + 2); - if let Some((first, second)) = &mut udp { - for item in [first, second] { - item.client.set_nonblocking(true).unwrap(); - assert_eq!( - item.client.recv(&mut [0; 64]).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - item.origin.observer.set_nonblocking(true).unwrap(); - assert_eq!( - item.origin.observer.read(&mut [0]).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - } - } - if let Some(origin) = &mut blackhole { - origin.observer.set_nonblocking(true).unwrap(); - assert_eq!( - origin.observer.read(&mut [0]).unwrap_err().kind(), - io::ErrorKind::WouldBlock - ); - } - thread::sleep(Duration::from_millis(25)); - } - drop((held, udp, blackhole, tcp, udp_guard)); - assert!(fd_count() <= baseline); - } -} - -fn select(controller: u16, name: &str) { - let body = json!({"name":name}).to_string(); - let mut client = socket((Ipv4Addr::LOCALHOST, controller).into()); - write!(client,"PUT /proxies/inner HTTP/1.1\r\nHost: fixture\r\nAuthorization: Bearer fixture-only\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",body.len()).unwrap(); - let mut response = Vec::new(); - client.read_to_end(&mut response).unwrap(); - assert!(response.starts_with(b"HTTP/1.1 204")); -} -#[test] -#[ignore = "isolated VMESS runner"] -fn public_graph() { - let _case = Case::new("VMESS-PUBLIC", "runtime::public_graph"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let controller = free_port(); - let mut node = f["node"].clone(); - node["dialer-proxy"] = json!("hop"); - node["server"] = json!("peer.fixture.test"); - let mut yaml = config(node.clone(), port); - yaml["proxies"] - .as_array_mut() - .unwrap() - .push(f["hop"].clone()); - let core = Core::start(&yaml); - echo(port, &f); - let mut a = Association::new(&f, port, false, false); - a.exchange(b"concrete-hop"); - core.stop(); - drop(a); - node["server"] = f["node"]["server"].clone(); - node["dialer-proxy"] = json!("outer"); - yaml["proxies"][0] = node; - yaml["external-controller"] = json!(format!("127.0.0.1:{controller}")); - yaml["secret"] = json!("fixture-only"); - yaml["proxy-groups"] = json!([{"name":"outer","type":"select","proxies":["inner"]},{"name":"inner","type":"select","proxies":["hop","DIRECT","REJECT"]}]); - let core = Core::start(&yaml); - let (mut stream, mut origin) = live(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.exchange(b"before-switch"); - select(controller, "REJECT"); - exchange(&mut stream, b"old-stream-snapshot"); - udp.exchange(b"old-udp-snapshot"); - let mut unused = Origin::new(&f, 13, false); - let mut denied = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(unused.target, false)); - denied.write_all(&request).unwrap(); - let mut result = [0; 10]; - denied.read_exact(&mut result).unwrap(); - assert_ne!(result[1], 0); - unused.quiet(); - drop((denied, unused)); - select(controller, "DIRECT"); - echo(port, &f); - exchange(&mut stream, b"still-old-snapshot"); - core.stop(); - assert_closed(&mut stream); - origin.marker(b'D'); - drop((stream, origin, udp)); - // Even an unselected cycle is rejected before any start/socket creation. - yaml["proxy-groups"][1]["proxies"] = json!(["DIRECT", "peer"]); - assert_eq!( - invoke_response( - "validateConfig", - None, - json!({"configYaml":yaml.to_string()}) - )["success"], - false - ); - // A failing selected concrete upstream never switches to DIRECT. - let mut broken = f["hop"].clone(); - broken["port"] = json!(1); - yaml["proxies"][1] = broken; - yaml["proxy-groups"][1]["proxies"] = json!(["hop", "DIRECT"]); - let core = Core::start(&yaml); - let mut unused = Origin::new(&f, 13, false); - let mut denied = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(unused.target, false)); - denied.write_all(&request).unwrap(); - denied.read_exact(&mut result).unwrap(); - assert_ne!(result[1], 0); - unused.quiet(); - core.stop(); -} - -#[test] -#[ignore = "isolated VMESS runner"] -fn public_ipv6_and_gates() { - let _case = Case::new("VMESS-PUBLIC", "runtime::public_ipv6_and_gates"); - let f = fixture(); - initialize(&f); - let port = free_port(); - let mut node = f["node"].clone(); - node["server"] = f["server_ipv6"].clone(); - let core = Core::start(&config(node, port)); - echo(port, &f); - let mut udp = Association::new(&f, port, true, false); - udp.exchange(b"outer-inner-ipv6"); - core.stop(); - drop(udp); - let mut node = f["node"].clone(); - node["udp"] = json!(false); - let mut yaml = config(node, port); - yaml["ipv6"] = json!(false); - let core = Core::start(&yaml); - echo(port, &f); - let mut udp = Association::new(&f, port, false, false); - udp.client - .send_to(&udp.packet(b"disabled"), udp.relay) - .unwrap(); - udp.origin.quiet(); - let mut origin = Origin::new(&f, 13, true); - let mut client = login(port); - let mut request = vec![5, 1, 0]; - request.extend(address(origin.target, false)); - client.write_all(&request).unwrap(); - let mut response = [0; 10]; - client.read_exact(&mut response).unwrap(); - assert_ne!(response[1], 0); - origin.quiet(); - core.stop(); -} - -#[tokio::test] -#[ignore = "isolated VMESS runner"] -async fn owned_resources() { - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint}, - outbound::{ - DatagramRequest, EstablishContext, OutboundConnector, UpstreamPath, - vmess::VmessOutbound, - }, - resources::observation::{ResourceKind, ResourceProbe}, - session::{Datagram, DatagramSession, InboundKind, StreamSession}, - }; - let _case = Case::new("VMESS-PUBLIC", "runtime::owned_resources"); - let f = fixture(); - let parsed = - Config::parse_yaml(config(f["node"].clone(), 1080).to_string().as_bytes()).unwrap(); - let ProxyProtocol::Vmess(node) = &parsed.proxies[0].protocol else { - unreachable!() - }; - for _ in 0..20 { - let mut case = Case::new("VMESS-OWNED", "stop_and_remain_quiet"); - let probe = ResourceProbe::default(); - case.checkpoint("baseline", probe.snapshot()); - probe - .scope(async { - let endpoint = ResolvedEndpoint { - logical_host: node.address.clone(), - port: node.port, - addresses: vec![SocketAddr::new(node.address.parse().unwrap(), node.port)], - }; - let outbound = VmessOutbound::new_with_path( - node, - UpstreamPath::direct(endpoint, Dialer::default()), - ) - .unwrap(); - let mut origin1 = Origin::new(&f, 13, false); - let mut origin2 = Origin::new(&f, 13, false); - let mut udp_origin = Origin::new(&f, 4, false); - let session = |target| StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: vcore::session::Destination::Ip(target), - sniffed_domain: None, - }; - let mut first = outbound - .connect_stream(session(origin1.target), &EstablishContext::default()) - .await - .unwrap() - .io; - first.write_all(b"a").await.unwrap(); - first.flush().await.unwrap(); - assert_eq!(first.read_u8().await.unwrap(), b'a'); - origin1.marker(b'A'); - let mut second = outbound - .connect_stream(session(origin2.target), &EstablishContext::default()) - .await - .unwrap() - .io; - second.write_all(b"b").await.unwrap(); - second.flush().await.unwrap(); - assert_eq!(second.read_u8().await.unwrap(), b'b'); - origin2.marker(b'A'); - drop(first); - tokio::task::spawn_blocking(move || origin1.marker(b'D')) - .await - .unwrap(); - second.write_all(b"c").await.unwrap(); - second.flush().await.unwrap(); - assert_eq!(second.read_u8().await.unwrap(), b'c'); - let request = DatagramRequest::new(DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )); - let mut udp = outbound - .open_datagram(request, &EstablishContext::default()) - .await - .unwrap(); - udp.send(Datagram { - remote: udp_origin.target.into(), - payload: bytes::Bytes::from_static(b"owned"), - sniffed_domain: None, - }) - .await - .unwrap(); - assert_eq!(udp.receive().await.unwrap().payload.as_ref(), b"owned"); - udp_origin.udp(b"owned"); - for kind in [ - ResourceKind::Association, - ResourceKind::Session, - ResourceKind::Socket, - ] { - assert!(probe.snapshot().peak(kind) > 0); - } - if node.transport.required_alpn() == Some(b"h2") { - assert!(probe.snapshot().peak(ResourceKind::Task) > 0); - } - outbound.begin_shutdown(); - assert!(second.read_u8().await.is_err()); - drop(second); - udp.close().await.unwrap(); - drop(udp); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - assert!(probe.snapshot().is_idle(), "{:?}", probe.snapshot()); - case.checkpoint("after-stop", probe.snapshot()); - let snapshot = probe.snapshot(); - let quiet = tokio::time::Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert_eq!(probe.snapshot(), snapshot); - tokio::time::sleep(Duration::from_millis(25)).await; - } - case.checkpoint("quiet", probe.snapshot()); - }) - .await; - case.resources(probe.snapshot()); - } -} diff --git a/tests/vmess_public/tun.rs b/tests/vmess_public/tun.rs deleted file mode 100644 index 68aa9f7..0000000 --- a/tests/vmess_public/tun.rs +++ /dev/null @@ -1,77 +0,0 @@ -//! Host-fd/netstack interop only: no real interface, route or system proxy. -use super::*; -use std::os::unix::net::UnixDatagram; - -fn checksum(bytes: &[u8]) -> u16 { - let mut sum: u32 = bytes - .chunks(2) - .map(|pair| u32::from(u16::from_be_bytes([pair[0], *pair.get(1).unwrap_or(&0)]))) - .sum(); - while sum >> 16 != 0 { - sum = (sum & 0xffff) + (sum >> 16); - } - !(sum as u16) -} - -pub(crate) fn packet( - target: SocketAddr, - protocol: u8, - seq_ack_flags: (u32, u32, u8), - payload: &[u8], -) -> Vec { - let SocketAddr::V4(target) = target else { - panic!("IPv4 fixture"); - }; - let source = Ipv4Addr::new(192, 0, 2, 10); - let mut segment = vec![0; if protocol == 6 { 20 } else { 8 }]; - segment[..2].copy_from_slice(&12000_u16.to_be_bytes()); - segment[2..4].copy_from_slice(&target.port().to_be_bytes()); - if protocol == 6 { - segment[4..8].copy_from_slice(&seq_ack_flags.0.to_be_bytes()); - segment[8..12].copy_from_slice(&seq_ack_flags.1.to_be_bytes()); - segment[12] = 0x50; - segment[13] = seq_ack_flags.2; - segment[14..16].copy_from_slice(&u16::MAX.to_be_bytes()); - } else { - segment[4..6].copy_from_slice(&((8 + payload.len()) as u16).to_be_bytes()); - } - segment.extend_from_slice(payload); - let mut pseudo = source.octets().to_vec(); - pseudo.extend_from_slice(&target.ip().octets()); - pseudo.extend_from_slice(&[0, protocol]); - pseudo.extend_from_slice(&(segment.len() as u16).to_be_bytes()); - pseudo.extend_from_slice(&segment); - let offset = if protocol == 6 { 16 } else { 6 }; - let value = checksum(&pseudo); - segment[offset..offset + 2].copy_from_slice(&value.to_be_bytes()); - let mut ip = vec![0; 20]; - ip[0] = 0x45; - ip[2..4].copy_from_slice(&((20 + segment.len()) as u16).to_be_bytes()); - ip[6] = 0x40; - ip[8] = 64; - ip[9] = protocol; - ip[12..16].copy_from_slice(&source.octets()); - ip[16..20].copy_from_slice(&target.ip().octets()); - let value = checksum(&ip); - ip[10..12].copy_from_slice(&value.to_be_bytes()); - ip.extend_from_slice(&segment); - let mut framed = 2_u32.to_be_bytes().to_vec(); // Darwin AF_INET PI header. - framed.extend_from_slice(&ip); - framed -} - -pub(crate) fn receive(peer: &UnixDatagram, protocol: u8) -> Vec { - let deadline = Instant::now() + TIMEOUT; - loop { - assert!(Instant::now() < deadline, "TUN packet deadline"); - let mut bytes = [0; 1504]; - let size = peer.recv(&mut bytes).unwrap(); - assert!(size >= 24); - assert_eq!(&bytes[..4], 2_u32.to_be_bytes()); - let ip = &bytes[4..size]; - assert_eq!(checksum(&ip[..20]), 0); - if ip[9] == protocol { - return ip.to_vec(); - } - } -} diff --git a/tests/xhttp_native.rs b/tests/xhttp_native.rs deleted file mode 100644 index fd8f59d..0000000 --- a/tests/xhttp_native.rs +++ /dev/null @@ -1,276 +0,0 @@ -#![cfg(all(feature = "outbound-vless", feature = "interop-test"))] - -use serde_json::{Value, json}; -use std::{ - net::{IpAddr, SocketAddr}, - sync::Arc, - time::Duration, -}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use vcore::{ - config::{Config, ProxyProtocol}, - dialer::{Dialer, ResolvedEndpoint, Resolver}, - dispatch::DatagramBudget, - outbound::{DatagramRequest, EstablishContext, OutboundConnector, VlessOutbound}, - session::{Datagram, DatagramSession, Destination, InboundKind, StreamSession}, -}; - -#[path = "xhttp_native/close.rs"] -mod close; -#[path = "xhttp_native/keepalive.rs"] -mod keepalive; -#[path = "xhttp_native/lifecycle.rs"] -mod lifecycle; -#[path = "xhttp_native/security.rs"] -mod security; - -fn node(fixture: &Value, encoding: Option<&str>) -> VlessOutbound { - let mut raw = fixture["node"].clone(); - if let Some(encoding) = encoding { - raw["packet-encoding"] = encoding.into(); - } - let config = Config::parse_yaml( - json!({"socks-port":1080,"proxies":[raw],"rules":["MATCH,peer"]}) - .to_string() - .as_bytes(), - ) - .unwrap(); - let ProxyProtocol::Vless(config) = &config.proxies[0].protocol else { - unreachable!() - }; - let endpoint_for = |address: &str, port| ResolvedEndpoint { - logical_host: address.into(), - port, - addresses: vec![SocketAddr::new( - address - .parse() - .unwrap_or_else(|_| fixture["server_ipv4"].as_str().unwrap().parse().unwrap()), - port, - )], - }; - let endpoint = endpoint_for(&config.address, config.port); - let download = config - .download() - .map(|leg| endpoint_for(&leg.address, leg.port)); - if fixture["root_der"].is_array() { - let root: Vec = serde_json::from_value(fixture["root_der"].clone()).unwrap(); - VlessOutbound::new_with_test_tls_roots_and_endpoints( - config, - endpoint, - download, - Dialer::default(), - [root], - ) - .unwrap() - } else { - VlessOutbound::new_with_endpoints(config, endpoint, download, Dialer::default()).unwrap() - } -} - -#[tokio::test] -#[ignore = "owned isolated Mihomo and origin containers required"] -async fn native_xhttp_request_fields() { - let fixture: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_XHTTP_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - let case = - vcore::resources::case_events::Case::new("XHTTP-XHTTP", "native_xhttp_request_fields"); - tokio::time::timeout(Duration::from_secs(120), async { - let outbound = node(&fixture, None); - let tiny = fixture["tiny"].as_bool().unwrap_or(false); - for family in ["ipv4", "ipv6", "domain"] { - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.set_nodelay(true).unwrap(); - control - .write_u8((if tiny { 13 } else { 10 }) | if family == "ipv6" { 0x80 } else { 0 }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - let ip: IpAddr = fixture[if family == "ipv6" { - "origin_ipv6" - } else { - "origin_ipv4" - }] - .as_str() - .unwrap() - .parse() - .unwrap(); - assert!(!ip.is_loopback() && !ip.is_unspecified()); - let target = if family == "domain" { - Destination::domain("vcore-fixture.test", port).unwrap() - } else { - SocketAddr::new(ip, port).into() - }; - let mut io = outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: target, - sniffed_domain: None, - }, - &EstablishContext::default(), - ) - .await - .unwrap() - .io; - if !tiny { - let mut greeting = [0; 5]; - io.read_exact(&mut greeting).await.unwrap(); - assert_eq!(&greeting, b"hello"); - } - let payload = vec![0x5a; if tiny { 256 } else { 10 * 1024 * 1024 }]; - let mut reply = vec![0; payload.len() + if tiny { 0 } else { 7 }]; - // The tiny-POST origin echoes as it reads. Drain both directions, - // like the real runtime: intentionally withholding all reads can - // exhaust h2's bounded count of buffered small DATA frames. - let (mut reader, mut writer) = tokio::io::split(&mut io); - tokio::try_join!( - async { - writer.write_all(&payload).await?; - writer.flush().await - }, - async { reader.read_exact(&mut reply).await.map(|_| ()) } - ) - .unwrap(); - drop((reader, writer)); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(&reply[..payload.len()], payload); - if !tiny { - assert_eq!(&reply[payload.len()..], b"trailer"); - } - io.shutdown().await.unwrap(); - drop(io); - assert_eq!( - tokio::time::timeout(Duration::from_secs(5), control.read_u8()) - .await - .expect("origin did not observe whole-connection close") - .unwrap(), - b'D' - ); - } - outbound.shutdown().await; - }) - .await - .unwrap(); - drop(case); -} - -struct FixtureResolver(IpAddr); -#[async_trait::async_trait] -impl Resolver for FixtureResolver { - async fn resolve(&self, host: &str, port: u16) -> std::io::Result { - if host != "vcore-fixture.test" { - return Err(std::io::ErrorKind::NotFound.into()); - } - Ok(ResolvedEndpoint { - logical_host: host.into(), - port, - addresses: vec![SocketAddr::new(self.0, port)], - }) - } -} - -#[tokio::test] -#[ignore = "owned isolated Mihomo and origin containers required"] -async fn native_xhttp_udp_encodings() { - let fixture: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_XHTTP_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - let case = - vcore::resources::case_events::Case::new("XHTTP-XHTTP", "native_xhttp_udp_encodings"); - tokio::time::timeout(Duration::from_secs(120), async { - let resolution = vcore::dns::resolution::ResolutionContext::measurement( - Arc::new(FixtureResolver( - fixture["origin_ipv4"].as_str().unwrap().parse().unwrap(), - )), - true, - ); - for encoding in [None, Some("none"), Some("packetaddr")] { - let outbound = node(&fixture, encoding); - for family in ["ipv4", "ipv6", "domain"] { - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.set_nodelay(true).unwrap(); - control - .write_u8(if family == "ipv6" { 6 } else { 4 }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - let ip: IpAddr = fixture[if family == "ipv6" { - "origin_ipv6" - } else { - "origin_ipv4" - }] - .as_str() - .unwrap() - .parse() - .unwrap(); - assert!(!ip.is_loopback() && !ip.is_unspecified()); - let target = if family == "domain" { - Destination::domain("vcore-fixture.test", port).unwrap() - } else { - SocketAddr::new(ip, port).into() - }; - let request = DatagramRequest::new(DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )) - .with_budget(DatagramBudget::new(15000, 15000)); - let mut io = outbound - .open_datagram( - request, - &EstablishContext::with_resolution( - Duration::from_secs(10), - resolution.clone(), - ), - ) - .await - .unwrap(); - for size in [1, 64, 512, 1200, 15000] { - for index in 0..100 { - let payload: Vec = (0..size).map(|i| (i + index) as u8).collect(); - io.send(Datagram { - remote: target.clone(), - payload: payload.clone().into(), - sniffed_domain: None, - }) - .await - .unwrap(); - assert_eq!(usize::from(control.read_u16().await.unwrap()), size); - let _source = control.read_u16().await.unwrap(); - let mut received = vec![0; size]; - control.read_exact(&mut received).await.unwrap(); - assert_eq!(received, payload); - let reply = io.receive().await.unwrap(); - assert_eq!(reply.payload, payload); - assert_eq!(reply.remote.port(), port); - } - } - assert!( - io.send(Datagram { - remote: target, - payload: vec![0; 15001].into(), - sniffed_domain: None - }) - .await - .is_err() - ); - io.close().await.unwrap(); - } - outbound.shutdown().await; - } - }) - .await - .unwrap(); - drop(case); -} diff --git a/tests/xhttp_native/close.rs b/tests/xhttp_native/close.rs deleted file mode 100644 index 9a2516c..0000000 --- a/tests/xhttp_native/close.rs +++ /dev/null @@ -1,62 +0,0 @@ -use super::*; - -#[tokio::test] -#[ignore = "owned isolated native handler and Mihomo client required"] -async fn native_mihomo_xhttp_close() { - let fixture: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_XHTTP_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - let _case = - vcore::resources::case_events::Case::new("XHTTP-XHTTP", "close::native_mihomo_xhttp_close"); - assert_eq!(fixture["close_reference"]["scope"], "same-mode"); - assert_eq!(fixture["close_reference"]["terminated"], true); - tokio::time::timeout(Duration::from_secs(15), async { - let outbound = node(&fixture, None); - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.write_u8(11).await.unwrap(); - let port = control.read_u16().await.unwrap(); - let mut stream = outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: SocketAddr::new( - fixture["origin_ipv4"].as_str().unwrap().parse().unwrap(), - port, - ) - .into(), - sniffed_domain: None, - }, - &EstablishContext::default(), - ) - .await - .unwrap() - .io; - let mut hello = [0; 5]; - stream.read_exact(&mut hello).await.unwrap(); - assert_eq!(&hello, b"hello"); - stream.write_all(b"ping").await.unwrap(); - stream.flush().await.unwrap(); - let mut reply = [0; 4]; - stream.read_exact(&mut reply).await.unwrap(); - assert_eq!(&reply, b"ping"); - stream.shutdown().await.unwrap(); - let mut tail = Vec::new(); - let _terminal = stream.read_to_end(&mut tail).await; - let hex: String = tail.iter().map(|b| format!("{b:02x}")).collect(); - assert_eq!( - hex, - fixture["close_reference"]["tail_hex"].as_str().unwrap() - ); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - assert_eq!(control.read_u8().await.unwrap(), b'D'); - drop(stream); - outbound.shutdown().await; - }) - .await - .unwrap(); -} diff --git a/tests/xhttp_native/keepalive.rs b/tests/xhttp_native/keepalive.rs deleted file mode 100644 index dd6b547..0000000 --- a/tests/xhttp_native/keepalive.rs +++ /dev/null @@ -1,82 +0,0 @@ -use super::*; - -#[tokio::test] -#[ignore = "owned isolated Xray H3 handler required"] -async fn native_h3_keepalive_observes_each_leg_and_stop() { - let fixture: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_XHTTP_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - let _case = vcore::resources::case_events::Case::new( - "XHTTP-XHTTP", - "keepalive::native_h3_keepalive_observes_each_leg_and_stop", - ); - for (upload, download, seconds) in [(0, -1, 11), (-1, 1, 3), (1, 0, 11)] { - let mut f = fixture.clone(); - f["node"]["xhttp-opts"]["reuse-settings"] = json!({"h-keep-alive-period":upload}); - f["node"]["xhttp-opts"]["download-settings"] = - json!({"reuse-settings":{"h-keep-alive-period":download}}); - let outbound = node(&f, None); - let mut control = tokio::net::TcpStream::connect(f["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.write_u8(13).await.unwrap(); - let port = control.read_u16().await.unwrap(); - let mut io = outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: SocketAddr::new( - f["origin_ipv4"].as_str().unwrap().parse().unwrap(), - port, - ) - .into(), - sniffed_domain: None, - }, - &EstablishContext::default(), - ) - .await - .unwrap() - .io; - io.write_all(b"ping").await.unwrap(); - io.flush().await.unwrap(); - let mut data = [0; 4]; - io.read_exact(&mut data).await.unwrap(); - assert_eq!(&data, b"ping"); - assert_eq!(control.read_u8().await.unwrap(), b'A'); - tokio::time::sleep(Duration::from_millis(500)).await; - let before = outbound.xhttp_quic_ping_counts(); - assert_eq!((before.0.len(), before.1.len()), (1, 1)); - tokio::time::sleep(Duration::from_secs(seconds)).await; - let after = outbound.xhttp_quic_ping_counts(); - for (period, first, last) in [ - (upload, &before.0, &after.0), - (download, &before.1, &after.1), - ] { - assert_eq!(first.len(), last.len()); - if period < 0 { - assert_eq!(first, last, "disabled leg sent idle PING"); - } else { - assert!(last[0] > first[0], "configured/default leg sent no PING"); - } - } - io.write_all(b"pong").await.unwrap(); - io.flush().await.unwrap(); - io.read_exact(&mut data).await.unwrap(); - assert_eq!(&data, b"pong"); - io.shutdown().await.unwrap(); - drop(io); - assert_eq!( - tokio::time::timeout(Duration::from_secs(5), control.read_u8()) - .await - .unwrap() - .unwrap(), - b'D' - ); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - assert_eq!(outbound.xhttp_quic_ping_counts(), (vec![], vec![])); - } -} diff --git a/tests/xhttp_native/lifecycle.rs b/tests/xhttp_native/lifecycle.rs deleted file mode 100644 index 6c9fb71..0000000 --- a/tests/xhttp_native/lifecycle.rs +++ /dev/null @@ -1,142 +0,0 @@ -use super::*; -use vcore::resources::{ - case_events::Case, - observation::{ResourceKind, ResourceProbe}, -}; - -async fn origin(f: &Value, mode: u8) -> (tokio::net::TcpStream, Destination) { - let mut control = tokio::net::TcpStream::connect(f["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.set_nodelay(true).unwrap(); - control.write_u8(mode).await.unwrap(); - let port = control.read_u16().await.unwrap(); - let ip = f["origin_ipv4"].as_str().unwrap().parse().unwrap(); - (control, Destination::Ip(SocketAddr::new(ip, port))) -} - -async fn tcp(outbound: &VlessOutbound, target: Destination) -> vcore::dispatch::BoxStream { - outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination: target, - sniffed_domain: None, - }, - &EstablishContext::default(), - ) - .await - .unwrap() - .io -} -async fn exchange(io: &mut vcore::dispatch::BoxStream, byte: u8) { - io.write_u8(byte).await.unwrap(); - io.flush().await.unwrap(); - assert_eq!(io.read_u8().await.unwrap(), byte); -} - -#[tokio::test] -#[ignore = "owned isolated protocol peers and origins required"] -async fn native_xhttp_owned_resources() { - let f: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_XHTTP_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(f["isolation"], "containers"); - let _case = Case::new("XHTTP-XHTTP", "lifecycle::native_xhttp_owned_resources"); - tokio::time::timeout(Duration::from_secs(220), async { - for _ in 0..20 { - let mut cycle = Case::new("XHTTP-OWNED", "stop_and_remain_quiet"); - let probe = ResourceProbe::default(); - cycle.checkpoint("baseline", probe.snapshot()); - probe - .scope(async { - let outbound = node(&f, None); - let (mut control1, target1) = origin(&f, 13).await; - let (mut control2, target2) = origin(&f, 13).await; - let (mut control_udp, target_udp) = origin(&f, 4).await; - let mut first = tcp(&outbound, target1).await; - let mut sibling = tcp(&outbound, target2).await; - exchange(&mut first, b'a').await; - exchange(&mut sibling, b'b').await; - assert_eq!(control1.read_u8().await.unwrap(), b'A'); - assert_eq!(control2.read_u8().await.unwrap(), b'A'); - first.shutdown().await.unwrap(); - drop(first); - assert_eq!( - tokio::time::timeout(Duration::from_secs(5), control1.read_u8()) - .await - .unwrap() - .unwrap(), - b'D' - ); - exchange(&mut sibling, b'c').await; - let mut udp = outbound - .open_datagram( - DatagramRequest::new(DatagramSession::new( - InboundKind::InternalMeasure, - "127.0.0.1:1".parse().unwrap(), - )), - &EstablishContext::default(), - ) - .await - .unwrap(); - udp.send(Datagram { - remote: target_udp, - payload: bytes::Bytes::from_static(b"owned"), - sniffed_domain: None, - }) - .await - .unwrap(); - assert_eq!(udp.receive().await.unwrap().payload.as_ref(), b"owned"); - assert_eq!(control_udp.read_u16().await.unwrap(), 5); - assert_ne!(control_udp.read_u16().await.unwrap(), 0); - let mut observed = [0; 5]; - control_udp.read_exact(&mut observed).await.unwrap(); - assert_eq!(&observed, b"owned"); - for kind in [ - ResourceKind::Association, - ResourceKind::Session, - ResourceKind::Socket, - ResourceKind::Task, - ] { - assert!( - probe.snapshot().peak(kind) > 0, - "unobserved resource: {kind:?}" - ); - } - outbound.begin_shutdown(); - assert!( - tokio::time::timeout(Duration::from_secs(5), sibling.read_u8()) - .await - .unwrap() - .is_err() - ); - drop(sibling); - let _ = udp.close().await; - drop(udp); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .expect("node Stop exceeded five seconds"); - assert!( - probe.snapshot().is_idle(), - "Stop retained owned resources: {:?}", - probe.snapshot() - ); - cycle.checkpoint("after-stop", probe.snapshot()); - let stopped = probe.snapshot(); - let quiet = tokio::time::Instant::now(); - while quiet.elapsed() < Duration::from_secs(5) { - assert_eq!(probe.snapshot(), stopped); - tokio::time::sleep(Duration::from_millis(25)).await; - } - cycle.checkpoint("quiet", probe.snapshot()); - }) - .await; - cycle.resources(probe.snapshot()); - } - }) - .await - .unwrap(); -} diff --git a/tests/xhttp_native/security.rs b/tests/xhttp_native/security.rs deleted file mode 100644 index 9dd5d94..0000000 --- a/tests/xhttp_native/security.rs +++ /dev/null @@ -1,100 +0,0 @@ -//! Authentication is checked against native container peers, never a host TLS server. -use super::*; -use vcore::resources::{case_events::Case, observation::ResourceProbe}; - -#[tokio::test] -#[ignore = "owned isolated authentication peers required"] -async fn native_xhttp_security() { - let fixture: Value = serde_json::from_slice( - &std::fs::read(std::env::var("VCORE_XHTTP_INPUT").unwrap()).unwrap(), - ) - .unwrap(); - assert_eq!(fixture["isolation"], "containers"); - let case = Case::new("XHTTP-XHTTP", "security::native_xhttp_security"); - let probe = ResourceProbe::default(); - probe - .scope(async { - let outbound = node(&fixture, None); - let mut control = - tokio::net::TcpStream::connect(fixture["origin_control"].as_str().unwrap()) - .await - .unwrap(); - control.set_nodelay(true).unwrap(); - // Mode 12 records any connection before returning an acknowledgement; - // mode 10 sends first and checks both directions independently. - let reject = fixture["reject"].as_bool().unwrap(); - control - .write_u8(if reject { 12 } else { 10 }) - .await - .unwrap(); - let port = control.read_u16().await.unwrap(); - let destination = SocketAddr::new( - fixture["origin_ipv4"].as_str().unwrap().parse().unwrap(), - port, - ) - .into(); - let context = EstablishContext::with_timeout(Duration::from_secs(4)); - let outcome = - tokio::time::timeout(Duration::from_secs(if reject { 5 } else { 120 }), async { - let connected = outbound - .connect_stream( - StreamSession { - inbound: InboundKind::InternalMeasure, - source: "127.0.0.1:1".parse().unwrap(), - destination, - sniffed_domain: None, - }, - &context, - ) - .await - .map_err(std::io::Error::other)?; - let mut io = connected.io; - if reject { - io.write_all(b"synthetic-probe").await?; - io.flush().await?; - let mut reply = [0; 2]; - io.read_exact(&mut reply).await?; - return Ok::<_, std::io::Error>(()); - } - let mut first = [0; 5]; - io.read_exact(&mut first).await?; - assert_eq!(&first, b"hello"); - assert_eq!(control.read_u8().await?, b'A'); - let payload = vec![0x5a; 10 * 1024 * 1024]; - io.write_all(&payload).await?; - io.flush().await?; - let mut reply = vec![0; payload.len() + 7]; - io.read_exact(&mut reply).await?; - assert_eq!(&reply[..payload.len()], payload); - assert_eq!(&reply[payload.len()..], b"trailer"); - io.shutdown().await?; - drop(io); - assert_eq!(control.read_u8().await?, b'D'); - Ok(()) - }) - .await - .expect("native authentication/data path exceeded its deadline"); - if reject { - assert!(outcome.is_err(), "invalid native identity exchanged data"); - assert!( - tokio::time::timeout(Duration::from_millis(300), control.read_u8()) - .await - .is_err(), - "origin accepted traffic on a rejected path" - ); - } else { - outcome.unwrap(); - } - outbound.begin_shutdown(); - tokio::time::timeout(Duration::from_secs(5), outbound.shutdown()) - .await - .unwrap(); - drop(outbound); - assert!( - probe.snapshot().is_idle(), - "resources remain after shutdown" - ); - }) - .await; - drop(case); -} diff --git a/tests/xray_interop.rs b/tests/xray_interop.rs deleted file mode 100644 index 65ce6bf..0000000 --- a/tests/xray_interop.rs +++ /dev/null @@ -1,949 +0,0 @@ -//! Opt-in interoperability coverage against an installed Xray-core binary. -//! -//! Compile-only legacy fixture pending full container migration; see tests/README.md. -//! The REALITY phase uses a local ECDSA-only TLS 1.3 camouflage target and all -//! proxied echo targets are local, so the test does not depend on public nodes. - -#![cfg(feature = "outbound-vless")] - -use std::{ - env, io, - net::{IpAddr, Ipv4Addr, SocketAddr}, - sync::Arc, - time::Duration, -}; - -#[cfg(feature = "ffi")] -use std::{ - ffi::{CStr, CString}, - net::TcpListener as StdTcpListener, -}; - -use bytes::Bytes; -use tokio::{ - io::{AsyncReadExt as _, AsyncWriteExt as _}, - net::{TcpListener, UdpSocket}, - sync::Barrier, -}; -use vcore::{ - config::{Config, ProxyProtocol, VlessOutboundConfig}, - dialer::{Dialer, ResolvedEndpoint}, - dispatch::Dispatcher as _, - outbound::VlessOutbound, - security::SecurityClient, - session::{Datagram, DatagramSession, Destination, InboundKind, StreamSession}, -}; - -const REALITY_PUBLIC_KEY: &str = "TrotdL9Y_dMWo-eqNe5dGfx7AbY1vNJjuEvRs4WR2y4"; -const REALITY_SHORT_ID: &str = "0123456789abcdef"; - -fn sole_vless(config: &Config) -> &VlessOutboundConfig { - match &config.proxies[0].protocol { - ProxyProtocol::Vless(vless) => vless, - ProxyProtocol::Socks5(_) => panic!("interop config must contain VLESS"), - ProxyProtocol::AnyTls(_) => panic!("interop config must contain VLESS"), - ProxyProtocol::Shadowsocks(_) => panic!("interop config must contain VLESS"), - ProxyProtocol::Trojan(_) => panic!("interop config must contain VLESS"), - ProxyProtocol::Vmess(_) | ProxyProtocol::Hysteria2(_) | ProxyProtocol::Tuic(_) => { - panic!("interop config must contain VLESS") - } - } -} - -fn reality_config( - server: SocketAddr, - mode: &str, - split_download: bool, - public_key: &str, - short_id: &str, -) -> Config { - let download_settings = if split_download { - " download-settings: {}\n" - } else { - "" - }; - let yaml = format!( - r#"port: 18080 -authentication: - - measure:secret -proxies: - - name: xray-interop - type: vless - server: 127.0.0.1 - port: {} - uuid: b831381d-6324-4d53-ad4f-8cda48b30811 - udp: true - tls: true - network: xhttp - encryption: none - servername: www.cloudflare.com - alpn: [h2] - reality-opts: - public-key: {public_key} - short-id: "{short_id}" - xhttp-opts: - path: /onev - host: www.cloudflare.com - mode: {mode} -{download_settings} -rules: - - MATCH,xray-interop -"#, - server.port() - ); - Config::parse_yaml(yaml.as_bytes()).expect("interop config must be valid") -} - -fn reality_outbound( - server: SocketAddr, - mode: &str, - split_download: bool, - public_key: &str, - short_id: &str, -) -> VlessOutbound { - let config = reality_config(server, mode, split_download, public_key, short_id); - let endpoint = ResolvedEndpoint { - logical_host: "127.0.0.1".to_owned(), - port: server.port(), - addresses: vec![server], - }; - VlessOutbound::new( - sole_vless(&config), - endpoint, - Dialer::default().with_timeout(Duration::from_secs(3)), - ) - .expect("interop outbound must build") -} - -async fn check_shared_reality_connector_concurrency( - config: &VlessOutboundConfig, - xray_address: SocketAddr, -) -> io::Result<()> { - const HANDSHAKE_COUNT: usize = 8; - let security = SecurityClient::from_proxy(config)?; - let sockets = tokio::time::timeout( - Duration::from_secs(5), - futures_util::future::try_join_all( - (0..HANDSHAKE_COUNT).map(|_| tokio::net::TcpStream::connect(xray_address)), - ), - ) - .await - .map_err(|_| { - io::Error::new( - io::ErrorKind::TimedOut, - "shared REALITY connector TCP setup timed out", - ) - })??; - let barrier = Arc::new(Barrier::new(HANDSHAKE_COUNT)); - let handshakes = sockets.into_iter().map(|socket| { - let barrier = Arc::clone(&barrier); - let security = &security; - async move { - barrier.wait().await; - let stream = security.connect(Box::new(socket)).await?; - drop(stream); - Ok::<_, io::Error>(()) - } - }); - let results = tokio::time::timeout( - Duration::from_secs(10), - futures_util::future::join_all(handshakes), - ) - .await - .map_err(|_| { - io::Error::new( - io::ErrorKind::TimedOut, - "eight shared REALITY connector handshakes timed out", - ) - })?; - - let mut failures = Vec::new(); - for (index, result) in results.into_iter().enumerate() { - if let Err(error) = result { - failures.push(format!("handshake {index}: {error}")); - } - } - if failures.is_empty() { - Ok(()) - } else { - Err(io::Error::other(format!( - "shared REALITY connector completed with {} failure(s): {}", - failures.len(), - failures.join("; ") - ))) - } -} - -async fn check_cancelled_reality_handshake_can_reconnect( - config: &VlessOutboundConfig, - xray_address: SocketAddr, -) -> io::Result<()> { - let security = SecurityClient::from_proxy(config)?; - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let stalled_address = listener.local_addr()?; - let (client, (mut server, _)) = tokio::try_join!( - tokio::net::TcpStream::connect(stalled_address), - listener.accept() - )?; - let cancelled_security = security.clone(); - let handshake = tokio::spawn(async move { cancelled_security.connect(Box::new(client)).await }); - - let mut record_header = [0_u8; 5]; - tokio::time::timeout( - Duration::from_secs(5), - server.read_exact(&mut record_header), - ) - .await - .map_err(|_| { - io::Error::new( - io::ErrorKind::TimedOut, - "cancelled REALITY handshake did not send a ClientHello", - ) - })??; - if record_header[0] != 0x16 { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "cancelled REALITY handshake did not start with a TLS handshake record", - )); - } - handshake.abort(); - let cancelled = handshake.await; - if !matches!(cancelled, Err(error) if error.is_cancelled()) { - return Err(io::Error::other( - "stalled REALITY handshake was not cancelled", - )); - } - drop(server); - - let socket = tokio::net::TcpStream::connect(xray_address).await?; - let stream = tokio::time::timeout(Duration::from_secs(5), security.connect(Box::new(socket))) - .await - .map_err(|_| { - io::Error::new( - io::ErrorKind::TimedOut, - "REALITY reconnect after cancellation timed out", - ) - })??; - drop(stream); - Ok(()) -} - -#[cfg(feature = "interop-test")] -fn tls_outbound( - server: SocketAddr, - mode: &str, - split_download: bool, - test_root_der: Option>, -) -> VlessOutbound { - let download_settings = if split_download { - " download-settings: {}\n" - } else { - "" - }; - let yaml = format!( - r#"port: 18080 -authentication: - - measure:secret -proxies: - - name: xray-tls-interop - type: vless - server: 127.0.0.1 - port: {} - uuid: b831381d-6324-4d53-ad4f-8cda48b30811 - udp: true - tls: true - network: xhttp - encryption: none - servername: vcore.test - alpn: [h2] - xhttp-opts: - path: /onev - host: vcore.test - mode: {mode} -{download_settings} -rules: - - MATCH,xray-tls-interop -"#, - server.port() - ); - let config = Config::parse_yaml(yaml.as_bytes()).expect("TLS interop config must be valid"); - let endpoint = ResolvedEndpoint { - logical_host: "127.0.0.1".to_owned(), - port: server.port(), - addresses: vec![server], - }; - let dialer = Dialer::default().with_timeout(Duration::from_secs(3)); - match test_root_der { - Some(root) => { - VlessOutbound::new_with_test_tls_roots(sole_vless(&config), endpoint, dialer, [root]) - } - None => VlessOutbound::new(sole_vless(&config), endpoint, dialer), - } - .expect("TLS interop outbound must build") -} - -async fn check_tcp_echo(outbound: &VlessOutbound) -> io::Result<()> { - const IO_TIMEOUT: Duration = Duration::from_secs(5); - - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let target = listener.local_addr()?; - - let server = async move { - let (mut stream, _) = listener.accept().await?; - let mut payload = [0_u8; 14]; - stream.read_exact(&mut payload).await?; - stream.write_all(&payload).await?; - Ok::<_, io::Error>(()) - }; - let client = async { - let mut stream = tokio::time::timeout( - IO_TIMEOUT, - outbound.connect_tcp(StreamSession { - inbound: InboundKind::Http, - source: SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 12345), - destination: Destination::Ip(target), - sniffed_domain: None, - }), - ) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "VLESS connect timed out"))? - .map_err(io::Error::other)?; - tokio::time::timeout(IO_TIMEOUT, stream.write_all(b"vcore-tcp-echo")) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "VLESS write timed out"))??; - stream.flush().await?; - let mut payload = [0_u8; 14]; - tokio::time::timeout(IO_TIMEOUT, stream.read_exact(&mut payload)) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "VLESS read timed out"))??; - assert_eq!(&payload, b"vcore-tcp-echo"); - stream.shutdown().await - }; - - tokio::try_join!(server, client)?; - Ok(()) -} - -async fn check_udp_echo(outbound: &VlessOutbound) -> io::Result<()> { - let socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let target = socket.local_addr()?; - - let server = async move { - let mut payload = [0_u8; 256]; - let (length, peer) = socket.recv_from(&mut payload).await?; - socket.send_to(&payload[..length], peer).await?; - Ok::<_, io::Error>(()) - }; - let client = async { - let mut transport = outbound - .open_datagram(DatagramSession::new( - InboundKind::Http, - SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 12346), - )) - .await - .map_err(io::Error::other)?; - let expected = Datagram { - remote: Destination::Ip(target), - payload: Bytes::from_static(b"vcore-udp-echo"), - sniffed_domain: None, - }; - transport - .send(expected.clone()) - .await - .map_err(io::Error::other)?; - let received = transport.receive().await.map_err(io::Error::other)?; - assert_eq!(received, expected); - transport.close().await.map_err(io::Error::other) - }; - - tokio::try_join!(server, client)?; - Ok(()) -} - -async fn check_echoes(outbound: &VlessOutbound, label: &str) -> io::Result<()> { - tokio::time::timeout(Duration::from_secs(7), check_tcp_echo(outbound)) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, format!("{label} TCP timed out")))??; - tokio::time::timeout(Duration::from_secs(7), check_udp_echo(outbound)) - .await - .map_err(|_| { - io::Error::new(io::ErrorKind::TimedOut, format!("{label} XUDP timed out")) - })??; - Ok(()) -} - -async fn expect_connect_rejected(outbound: &VlessOutbound, label: &str) -> io::Result<()> { - let result = tokio::time::timeout( - Duration::from_secs(5), - outbound.connect_tcp(StreamSession { - inbound: InboundKind::Http, - source: SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 12344), - destination: Destination::domain("example.com", 80)?, - sniffed_domain: None, - }), - ) - .await - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, format!("{label} timed out")))?; - if result.is_ok() { - return Err(io::Error::other(format!( - "{label} was unexpectedly accepted" - ))); - } - Ok(()) -} - -#[cfg(feature = "ffi")] -fn ffi_invoke(request: serde_json::Value) -> io::Result { - let request = CString::new(request.to_string()) - .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "Invoke JSON contains NUL"))?; - // SAFETY: CString provides a live NUL-terminated request for the duration - // of the call. The returned allocation is copied and freed exactly once. - let response = unsafe { vcore::ffi::VCoreInvoke(request.as_ptr()) }; - if response.is_null() { - return Err(io::Error::other("VCoreInvoke returned NULL")); - } - // SAFETY: a non-null VCoreInvoke response is a live NUL-terminated string. - let bytes = unsafe { CStr::from_ptr(response) }.to_bytes().to_vec(); - // SAFETY: response came from VCoreInvoke and has not been freed. - unsafe { vcore::ffi::VCoreFree(response) }; - let response: serde_json::Value = serde_json::from_slice(&bytes) - .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?; - if response["success"] != true { - return Err(io::Error::other(format!( - "VCore Invoke failed: {}", - response["error"].as_str().unwrap_or("unknown error") - ))); - } - Ok(response) -} - -#[cfg(feature = "ffi")] -fn ffi_method( - method: &str, - instance_id: Option<&str>, - payload: serde_json::Value, -) -> io::Result { - let mut request = serde_json::json!({ - "apiVersion": vcore::INVOKE_API_VERSION, - "method": method, - "payload": payload, - }); - if let Some(instance_id) = instance_id { - request["instanceId"] = serde_json::Value::String(instance_id.to_owned()); - } - ffi_invoke(request).map(|response| response["data"].clone()) -} - -#[cfg(feature = "ffi")] -fn ffi_measure_delay_batch( - config_yamls: &[String], - timeout_seconds: u64, - target_url: String, -) -> io::Result> { - let data = ffi_method( - "measureDelay", - None, - serde_json::json!({ - "configYamls": config_yamls, - "timeout": timeout_seconds, - "url": target_url, - }), - )?; - let results = data["results"].as_array().ok_or_else(|| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("measureDelay returned no results array: {data}"), - ) - })?; - if results.len() != config_yamls.len() { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!( - "measureDelay returned {} results, expected {}", - results.len(), - config_yamls.len() - ), - )); - } - results - .iter() - .enumerate() - .map(|(index, result)| { - if result["success"] != true { - return Err(io::Error::other(format!( - "measureDelay result {index} failed: {}", - result["error"].as_str().unwrap_or("unknown error") - ))); - } - result["delay"].as_u64().ok_or_else(|| { - io::Error::new( - io::ErrorKind::InvalidData, - format!( - "measureDelay result {index} returned a non-integer or negative delay: {result}" - ), - ) - }) - }) - .collect() -} - -#[cfg(feature = "ffi")] -fn ffi_stop_instance(instance_id: &str) -> io::Result<()> { - let stop = ffi_method("stop", Some(instance_id), serde_json::json!({})); - let destroy = ffi_method("destroyInstance", Some(instance_id), serde_json::json!({})); - stop.and(destroy).map(drop) -} - -#[cfg(feature = "ffi")] -fn ffi_start_single_instance(config_yaml: &str) -> io::Result { - let data = ffi_method("createInstance", None, serde_json::json!({}))?; - let instance_id = data["instanceId"] - .as_str() - .ok_or_else(|| io::Error::other("createInstance returned no instanceId"))? - .to_owned(); - - match ffi_method("createInstance", None, serde_json::json!({})) { - Err(error) if error.to_string().contains("already exists") => {} - Err(error) => { - let _ = ffi_stop_instance(&instance_id); - return Err(io::Error::other(format!( - "second createInstance failed for the wrong reason: {error}" - ))); - } - Ok(second) => { - if let Some(second_id) = second["instanceId"].as_str() { - let _ = ffi_stop_instance(second_id); - } - let _ = ffi_stop_instance(&instance_id); - return Err(io::Error::other( - "second createInstance unexpectedly succeeded", - )); - } - } - - if let Err(error) = ffi_method( - "prepare", - Some(&instance_id), - serde_json::json!({"configYaml": config_yaml}), - ) { - let _ = ffi_stop_instance(&instance_id); - return Err(error); - } - if let Err(error) = ffi_method("start", Some(&instance_id), serde_json::json!({})) { - let _ = ffi_stop_instance(&instance_id); - return Err(error); - } - Ok(instance_id) -} - -#[cfg(feature = "ffi")] -async fn proxy_tcp_echo(proxy: SocketAddr) -> io::Result<()> { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let target = listener.local_addr()?; - let payload = b"vcore-public-lifecycle".to_vec(); - let expected = payload.clone(); - - let server = async move { - let (mut stream, _) = listener.accept().await?; - let mut received = vec![0_u8; expected.len()]; - stream.read_exact(&mut received).await?; - if received != expected { - return Err(io::Error::other("echo target received unexpected payload")); - } - stream.write_all(&received).await?; - Ok::<_, io::Error>(()) - }; - let client = async move { - let mut stream = tokio::net::TcpStream::connect(proxy).await?; - stream - .write_all( - format!( - "CONNECT {target} HTTP/1.1\r\nHost: {target}\r\nProxy-Authorization: Basic bWVhc3VyZTpzZWNyZXQ=\r\nConnection: close\r\n\r\n" - ) - .as_bytes(), - ) - .await?; - let mut response = Vec::with_capacity(128); - while !response.ends_with(b"\r\n\r\n") { - if response.len() >= 1024 { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "HTTP proxy response head is too large", - )); - } - let byte = stream.read_u8().await?; - response.push(byte); - } - if !response.starts_with(b"HTTP/1.1 200") { - return Err(io::Error::other(format!( - "HTTP proxy CONNECT failed: {}", - String::from_utf8_lossy(&response) - ))); - } - stream.write_all(&payload).await?; - stream.flush().await?; - let mut echoed = vec![0_u8; payload.len()]; - stream.read_exact(&mut echoed).await?; - if echoed != payload { - return Err(io::Error::other("HTTP proxy returned unexpected payload")); - } - stream.shutdown().await - }; - - tokio::try_join!(server, client)?; - Ok(()) -} - -#[cfg(feature = "ffi")] -async fn serve_measure_delay_target( - listener: TcpListener, - expected_requests: usize, -) -> io::Result<()> { - const MAX_REQUEST_HEAD: usize = 8 * 1024; - let mut clients = Vec::with_capacity(expected_requests); - for _ in 0..expected_requests { - let (mut stream, _) = listener.accept().await?; - let mut head = Vec::with_capacity(256); - while !head.ends_with(b"\r\n\r\n") { - if head.len() >= MAX_REQUEST_HEAD { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "measureDelay request head is too large", - )); - } - head.push(stream.read_u8().await?); - } - let request_line = head.split(|byte| *byte == b'\r').next().unwrap_or_default(); - if !request_line.starts_with(b"HEAD ") || !request_line.ends_with(b" HTTP/1.1") { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!( - "measureDelay target expected an HTTP/1.1 HEAD request, got: {}", - String::from_utf8_lossy(request_line) - ), - )); - } - clients.push(stream); - } - - // Hold every request until the full batch has arrived. This proves that - // one Invoke owns multiple overlapping private measurement workers. - for mut stream in clients { - stream - .write_all(b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") - .await?; - stream.shutdown().await?; - } - Ok(()) -} - -#[cfg(feature = "ffi")] -async fn check_ffi_batch_measure_and_single_lifecycle( - xray_address: SocketAddr, - mode: &str, - split_download: bool, -) -> io::Result<()> { - const MEASURE_COUNT: usize = 5; - const MEASURE_TIMEOUT_SECONDS: u64 = 10; - let target_listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?; - let target_address = target_listener.local_addr()?; - let directory = tempfile::tempdir()?; - ffi_method( - "initialize", - None, - serde_json::json!({"dataDir": directory.path().to_string_lossy().into_owned()}), - )?; - let download_settings = if split_download { - " download-settings: {}\n" - } else { - "" - }; - let mut measure_config_yamls = Vec::with_capacity(MEASURE_COUNT); - for _ in 0..MEASURE_COUNT { - measure_config_yamls.push(format!( - r#"proxies: - - name: proxy - type: vless - server: {} - port: {} - uuid: b831381d-6324-4d53-ad4f-8cda48b30811 - udp: true - tls: true - network: xhttp - encryption: none - servername: www.cloudflare.com - alpn: [h2] - reality-opts: - public-key: TrotdL9Y_dMWo-eqNe5dGfx7AbY1vNJjuEvRs4WR2y4 - short-id: "0123456789abcdef" - xhttp-opts: - path: /onev - host: www.cloudflare.com - mode: {mode} -{download_settings} -"#, - xray_address.ip(), - xray_address.port(), - )); - } - - let target_task = tokio::spawn(async move { - match tokio::time::timeout( - Duration::from_secs(20), - serve_measure_delay_target(target_listener, MEASURE_COUNT), - ) - .await - { - Ok(result) => result, - Err(_) => Err(io::Error::new( - io::ErrorKind::TimedOut, - "measureDelay target timed out waiting for five concurrent requests", - )), - } - }); - let measurement_yamls = measure_config_yamls.clone(); - let measurement = tokio::task::spawn_blocking(move || { - ffi_measure_delay_batch( - &measurement_yamls, - MEASURE_TIMEOUT_SECONDS, - format!("http://{target_address}/measure"), - ) - }) - .await; - let delays = match measurement { - Ok(Ok(delays)) => delays, - Ok(Err(error)) => { - target_task.abort(); - let _ = target_task.await; - return Err(error); - } - Err(error) => { - target_task.abort(); - let _ = target_task.await; - return Err(io::Error::other(format!( - "VCore measureDelay task failed: {error}" - ))); - } - }; - if delays.len() != MEASURE_COUNT { - return Err(io::Error::other(format!( - "measureDelay returned {} results, expected {MEASURE_COUNT}", - delays.len() - ))); - } - target_task - .await - .map_err(|error| io::Error::other(format!("measureDelay target task failed: {error}")))??; - - let reservation = StdTcpListener::bind((Ipv4Addr::LOCALHOST, 0))?; - let http_port = reservation.local_addr()?.port(); - drop(reservation); - let runtime_config_yaml = format!( - r#"port: {http_port} -authentication: - - measure:secret -proxies: - - name: proxy - type: vless - server: {} - port: {} - uuid: b831381d-6324-4d53-ad4f-8cda48b30811 - udp: true - tls: true - network: xhttp - encryption: none - servername: www.cloudflare.com - alpn: [h2] - reality-opts: - public-key: TrotdL9Y_dMWo-eqNe5dGfx7AbY1vNJjuEvRs4WR2y4 - short-id: "0123456789abcdef" - xhttp-opts: - path: /onev - host: www.cloudflare.com - mode: {mode} -{download_settings} -rules: - - MATCH,proxy -"#, - xray_address.ip(), - xray_address.port(), - ); - - // A successful batch must leave the one public lifecycle slot untouched. - // While that lifecycle is live, a second create must still fail closed. - let instance_id = - tokio::task::spawn_blocking(move || ffi_start_single_instance(&runtime_config_yaml)) - .await - .map_err(|error| io::Error::other(format!("VCore setup task failed: {error}")))??; - - let proxy = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), http_port); - let probe_result = proxy_tcp_echo(proxy).await; - let cleanup_result = tokio::task::spawn_blocking(move || ffi_stop_instance(&instance_id)) - .await - .map_err(|error| io::Error::other(format!("VCore cleanup task failed: {error}")))?; - probe_result?; - cleanup_result -} - -async fn run_interop() -> io::Result<()> { - let xray_address = env::var("XRAY_INTEROP_ADDRESS") - .map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_ADDRESS is missing; legacy fixture; see tests/README.md", - ) - })? - .parse::() - .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?; - let mode = env::var("XRAY_INTEROP_MODE").map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_MODE is missing; legacy fixture; see tests/README.md", - ) - })?; - if !matches!(mode.as_str(), "auto" | "packet-up" | "stream-one") { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_MODE must be auto, packet-up, or stream-one", - )); - } - let split_download = match env::var("XRAY_INTEROP_SPLIT").as_deref() { - Ok("1") => true, - Ok("0") | Err(env::VarError::NotPresent) => false, - Ok(_) | Err(env::VarError::NotUnicode(_)) => { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_SPLIT must be 0 or 1", - )); - } - }; - if split_download != (mode == "auto") { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "the interop harness reserves mode auto for split download-settings", - )); - } - let security = env::var("XRAY_INTEROP_SECURITY").map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_SECURITY is missing; legacy fixture; see tests/README.md", - ) - })?; - - match security.as_str() { - "reality" => { - let first = reality_outbound( - xray_address, - &mode, - split_download, - REALITY_PUBLIC_KEY, - REALITY_SHORT_ID, - ); - let second = reality_outbound( - xray_address, - &mode, - split_download, - REALITY_PUBLIC_KEY, - REALITY_SHORT_ID, - ); - let suffix = if split_download { - " + split download" - } else { - "" - }; - let first_label = format!("REALITY {mode}{suffix} instance 1"); - let second_label = format!("REALITY {mode}{suffix} instance 2"); - tokio::try_join!( - check_echoes(&first, &first_label), - check_echoes(&second, &second_label), - )?; - - let shared_config = reality_config( - xray_address, - &mode, - split_download, - REALITY_PUBLIC_KEY, - REALITY_SHORT_ID, - ); - check_shared_reality_connector_concurrency(sole_vless(&shared_config), xray_address) - .await?; - check_cancelled_reality_handshake_can_reconnect( - sole_vless(&shared_config), - xray_address, - ) - .await?; - - #[cfg(feature = "ffi")] - check_ffi_batch_measure_and_single_lifecycle(xray_address, &mode, split_download) - .await?; - - // Run these last: Xray deliberately keeps camouflage fallbacks - // alive, which can occupy XHTTP accept workers. - let invalid_key = reality_outbound( - xray_address, - &mode, - split_download, - "MFcIAGWIFW5SAUDTxj4W2UpuaH70MS71vq4DlxhRzTM", - REALITY_SHORT_ID, - ); - expect_connect_rejected(&invalid_key, "invalid REALITY public key").await?; - let invalid_short_id = reality_outbound( - xray_address, - &mode, - split_download, - REALITY_PUBLIC_KEY, - "0123456789abcdee", - ); - expect_connect_rejected(&invalid_short_id, "invalid REALITY short ID").await - } - "tls" => { - #[cfg(feature = "interop-test")] - { - let root_path = env::var("XRAY_INTEROP_CA_DER").map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_CA_DER is missing", - ) - })?; - let root = std::fs::read(root_path)?; - let first = tls_outbound(xray_address, &mode, split_download, Some(root.clone())); - let second = tls_outbound(xray_address, &mode, split_download, Some(root)); - let suffix = if split_download { - " + split download" - } else { - "" - }; - let first_label = format!("TLS {mode}{suffix} instance 1"); - let second_label = format!("TLS {mode}{suffix} instance 2"); - tokio::try_join!( - check_echoes(&first, &first_label), - check_echoes(&second, &second_label), - )?; - - // The normal constructor must continue to reject the local CA. - let production = tls_outbound(xray_address, &mode, split_download, None); - expect_connect_rejected(&production, "untrusted standard TLS certificate").await - } - #[cfg(not(feature = "interop-test"))] - { - Err(io::Error::new( - io::ErrorKind::Unsupported, - "TLS interop requires the interop-test feature", - )) - } - } - _ => Err(io::Error::new( - io::ErrorKind::InvalidInput, - "XRAY_INTEROP_SECURITY must be reality or tls", - )), - } -} - -#[tokio::test] -#[ignore = "requires installed Xray-core and OpenSSL binaries"] -async fn xray_vless_xhttp_tcp_and_xudp_echo() { - tokio::time::timeout(Duration::from_secs(60), run_interop()) - .await - .expect("Xray interoperability test timed out") - .expect("Xray interoperability test failed"); -}