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 `[,...]`,命中后只在当前组内顺序故障转移。
+# `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