From 0464971c5cd5915d1c4ec22385375471545667f1 Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 21 Sep 2026 11:05:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=93=E5=BA=93=E6=8B=86=E5=88=86=EF=BC=9A?= =?UTF-8?q?=E6=9C=AC=E4=BB=93=E5=8F=AA=E4=BF=9D=E7=95=99=20cache=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=BA=90=E7=A0=81=EF=BC=8Ccore/gateway/?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E6=8F=92=E4=BB=B6=E7=BB=8F=20submodule=20?= =?UTF-8?q?=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 从本仓移除 ipass-core / plugin-domain / plugin-llm / plugin-apix / gateway / vendor/ngx-rust(这些属主项目 ipass/ipass) - 添加 git submodule deps/ipass -> http://192.168.1.18:13300/ipass/ipass(提供 core/gateway/ngx-rust/其他插件用于编译 cache 插件) - rust/Cargo.toml workspace 仅含 plugin-cache,依赖经 ../deps/ipass/rust/... 解析 - 移除根 tests/run.sh(主项目通用回归入口,非 cache 专用) - 修复 .gitignore:所有 tests/cache/.run-* 运行时目录均不入库(此前 .run-auth/.run-cap 被误提交,已解除跟踪) - 新增/更新 AGENTS.md 与 docs/plugins.md:明确本仓只含 cache 插件、core 从主项目取、submodule 升级方式、构建/接线说明 --- .gitignore | 5 +- .gitmodules | 3 + AGENTS.md | 67 + deps/ipass | 1 + docs/plugins.md | 79 ++ rust/Cargo.toml | 21 +- rust/crates/ipass-core/Cargo.toml | 20 - rust/crates/ipass-core/src/api.rs | 486 ------- rust/crates/ipass-core/src/auth.rs | 181 --- rust/crates/ipass-core/src/config.rs | 94 -- rust/crates/ipass-core/src/http.rs | 223 ---- rust/crates/ipass-core/src/lib.rs | 35 - rust/crates/ipass-core/src/loc.rs | 61 - rust/crates/ipass-core/src/models.rs | 84 -- rust/crates/ipass-core/src/plugin.rs | 67 - rust/crates/ipass-core/src/session.rs | 57 - rust/crates/ipass-core/src/state.rs | 358 ----- rust/crates/ipass-core/src/store.rs | 132 -- rust/crates/ipass-core/src/usage.rs | 239 ---- rust/crates/ipass-core/src/users.rs | 463 ------- rust/crates/ipass-core/src/util.rs | 389 ------ rust/crates/plugin-apix/Cargo.toml | 20 - rust/crates/plugin-apix/src/api.rs | 198 --- rust/crates/plugin-apix/src/apix.rs | 786 ----------- rust/crates/plugin-apix/src/config.rs | 48 - rust/crates/plugin-apix/src/handlers.rs | 105 -- rust/crates/plugin-apix/src/lib.rs | 56 - rust/crates/plugin-apix/src/models.rs | 100 -- rust/crates/plugin-apix/src/store.rs | 35 - rust/crates/plugin-domain/Cargo.toml | 16 - rust/crates/plugin-domain/src/api.rs | 191 --- rust/crates/plugin-domain/src/config.rs | 35 - rust/crates/plugin-domain/src/domains.rs | 488 ------- rust/crates/plugin-domain/src/lib.rs | 54 - rust/crates/plugin-domain/src/models.rs | 61 - rust/crates/plugin-domain/src/resolve.rs | 188 --- rust/crates/plugin-domain/src/store.rs | 33 - rust/crates/plugin-llm/Cargo.toml | 17 - rust/crates/plugin-llm/src/api.rs | 444 ------- rust/crates/plugin-llm/src/config.rs | 249 ---- rust/crates/plugin-llm/src/handlers.rs | 347 ----- rust/crates/plugin-llm/src/lib.rs | 83 -- rust/crates/plugin-llm/src/llm.rs | 458 ------- rust/crates/plugin-llm/src/llm_audit.rs | 686 ---------- rust/crates/plugin-llm/src/models.rs | 123 -- rust/crates/plugin-llm/src/stats.rs | 382 ------ rust/crates/plugin-llm/src/stats_persist.rs | 463 ------- rust/crates/plugin-llm/src/store.rs | 35 - rust/crates/plugin-llm/src/upstreams.rs | 1159 ----------------- rust/crates/plugin-llm/src/usage_filter.rs | 358 ----- rust/gateway/Cargo.toml | 31 - rust/gateway/build.rs | 20 - rust/gateway/src/api.rs | 86 -- rust/gateway/src/lib.rs | 784 ----------- rust/vendor/ngx-rust/.cargo/audit.toml | 35 - rust/vendor/ngx-rust/.cargo/config.toml | 2 - rust/vendor/ngx-rust/.dockerignore | 23 - rust/vendor/ngx-rust/.git-blame-ignore-revs | 17 - .../.github/ISSUE_TEMPLATE/bug_report.md | 29 - .../.github/ISSUE_TEMPLATE/feature_request.md | 17 - .../ngx-rust/.github/PULL_REQUEST_TEMPLATE.md | 15 - rust/vendor/ngx-rust/.github/dependabot.yml | 26 - .../.github/workflows/cargo-deny.yaml | 25 - .../vendor/ngx-rust/.github/workflows/ci.yaml | 194 --- .../ngx-rust/.github/workflows/nginx.yaml | 238 ---- .../.github/workflows/sanitizers.yaml | 112 -- rust/vendor/ngx-rust/.gitignore | 256 ---- rust/vendor/ngx-rust/.gitmodules | 4 - rust/vendor/ngx-rust/CHANGELOG.md | 170 --- rust/vendor/ngx-rust/CODE_OF_CONDUCT.md | 129 -- rust/vendor/ngx-rust/CONTRIBUTING.md | 55 - rust/vendor/ngx-rust/Cargo.toml | 84 -- rust/vendor/ngx-rust/Containerfile.debian | 16 - rust/vendor/ngx-rust/Dockerfile | 51 - rust/vendor/ngx-rust/GNUmakefile | 92 -- rust/vendor/ngx-rust/LICENSE | 201 --- rust/vendor/ngx-rust/README.md | 256 ---- rust/vendor/ngx-rust/SECURITY.md | 22 - rust/vendor/ngx-rust/build.rs | 67 - rust/vendor/ngx-rust/deny.toml | 27 - rust/vendor/ngx-rust/examples/Cargo.toml | 70 - rust/vendor/ngx-rust/examples/README.md | 226 ---- rust/vendor/ngx-rust/examples/async.conf | 24 - rust/vendor/ngx-rust/examples/async.rs | 237 ---- rust/vendor/ngx-rust/examples/auto/rust | 351 ----- rust/vendor/ngx-rust/examples/awssig.conf | 50 - rust/vendor/ngx-rust/examples/awssig.rs | 324 ----- .../examples/awssig_curl_combined.conf | 53 - rust/vendor/ngx-rust/examples/config | 66 - rust/vendor/ngx-rust/examples/config.make | 6 - rust/vendor/ngx-rust/examples/curl.conf | 31 - rust/vendor/ngx-rust/examples/curl.rs | 136 -- .../vendor/ngx-rust/examples/httporigdst.conf | 16 - rust/vendor/ngx-rust/examples/httporigdst.rs | 283 ---- rust/vendor/ngx-rust/examples/shared_dict.rs | 425 ------ rust/vendor/ngx-rust/examples/t/async.t | 56 - rust/vendor/ngx-rust/examples/t/awssig.t | 73 -- rust/vendor/ngx-rust/examples/t/curl.t | 70 - rust/vendor/ngx-rust/examples/t/shared_dict.t | 129 -- rust/vendor/ngx-rust/examples/t/upstream.t | 79 -- rust/vendor/ngx-rust/examples/upstream.conf | 24 - rust/vendor/ngx-rust/examples/upstream.rs | 331 ----- .../ngx-rust/misc/lsan-suppressions.txt | 17 - .../misc/nginx-sanitizer-support.patch | 28 - .../ngx-rust/misc/ubsan-suppressions.txt | 28 - .../vendor/ngx-rust/misc/update-action-sha.sh | 10 - rust/vendor/ngx-rust/nginx-src/Cargo.toml | 30 - rust/vendor/ngx-rust/nginx-src/LICENSE | 1 - rust/vendor/ngx-rust/nginx-src/README.md | 82 -- .../vendor/ngx-rust/nginx-src/src/download.rs | 252 ---- rust/vendor/ngx-rust/nginx-src/src/lib.rs | 214 --- .../vendor/ngx-rust/nginx-src/src/verifier.rs | 107 -- rust/vendor/ngx-rust/nginx-sys/Cargo.toml | 47 - rust/vendor/ngx-rust/nginx-sys/LICENSE | 1 - rust/vendor/ngx-rust/nginx-sys/README.md | 173 --- rust/vendor/ngx-rust/nginx-sys/src/detail.rs | 153 --- rust/vendor/ngx-rust/nginx-sys/src/event.rs | 85 -- rust/vendor/ngx-rust/nginx-sys/src/http.rs | 18 - rust/vendor/ngx-rust/nginx-sys/src/lib.rs | 352 ----- rust/vendor/ngx-rust/nginx-sys/src/mail.rs | 13 - rust/vendor/ngx-rust/nginx-sys/src/queue.rs | 265 ---- rust/vendor/ngx-rust/nginx-sys/src/rbtree.rs | 85 -- rust/vendor/ngx-rust/nginx-sys/src/stream.rs | 13 - rust/vendor/ngx-rust/nginx-sys/src/string.rs | 233 ---- rust/vendor/ngx-rust/rustfmt.toml | 9 - rust/vendor/ngx-rust/src/allocator.rs | 81 -- rust/vendor/ngx-rust/src/async_/mod.rs | 8 - rust/vendor/ngx-rust/src/async_/resolver.rs | 328 ----- rust/vendor/ngx-rust/src/async_/sleep.rs | 135 -- rust/vendor/ngx-rust/src/async_/spawn.rs | 146 --- rust/vendor/ngx-rust/src/collections/mod.rs | 16 - rust/vendor/ngx-rust/src/collections/queue.rs | 419 ------ .../vendor/ngx-rust/src/collections/rbtree.rs | 472 ------- rust/vendor/ngx-rust/src/core/buffer.rs | 129 -- rust/vendor/ngx-rust/src/core/conf.rs | 179 --- rust/vendor/ngx-rust/src/core/mod.rs | 27 - rust/vendor/ngx-rust/src/core/pool.rs | 337 ----- rust/vendor/ngx-rust/src/core/slab.rs | 151 --- rust/vendor/ngx-rust/src/core/status.rs | 70 - rust/vendor/ngx-rust/src/core/string.rs | 633 --------- rust/vendor/ngx-rust/src/ffi/mod.rs | 1 - rust/vendor/ngx-rust/src/http/conf.rs | 373 ------ rust/vendor/ngx-rust/src/http/mod.rs | 10 - rust/vendor/ngx-rust/src/http/module.rs | 173 --- rust/vendor/ngx-rust/src/http/request.rs | 802 ------------ rust/vendor/ngx-rust/src/http/status.rs | 208 --- rust/vendor/ngx-rust/src/http/upstream.rs | 24 - rust/vendor/ngx-rust/src/lib.rs | 189 --- rust/vendor/ngx-rust/src/log.rs | 342 ----- rust/vendor/ngx-rust/src/sync.rs | 118 -- .../ngx-rust/tests/conditional-compilation.rs | 23 - rust/vendor/ngx-rust/tests/config/nginx.conf | 43 - rust/vendor/ngx-rust/tests/log_test.rs | 148 --- rust/vendor/ngx-rust/tests/module/config | 12 - rust/vendor/ngx-rust/tests/module/config.make | 7 - .../tests/module/ngx_http_rust_module.c | 92 -- rust/vendor/ngx-rust/tests/nginx.conf | 42 - .../6/c4/56d612c5a01718fa4713b10d04281c46 | Bin 596 -> 0 bytes .../go/a/38/a2fabffdabc504877dd7c4883d13d38a | Bin 590 -> 0 bytes .../go/c/3c/fc25799d28960c2f86b96ce2383e23cc | Bin 582 -> 0 bytes .../npm/3/9b/21687620212a451b79a08052337649b3 | Bin 576 -> 0 bytes tests/cache/.run-auth/conf/cache-common.conf | 131 -- tests/cache/.run-auth/conf/cache-routes.conf | 177 --- tests/cache/.run-auth/conf/mime.types | 99 -- tests/cache/.run-auth/conf/nginx.cache.conf | 87 -- tests/cache/.run-auth/conf/server.cache.conf | 30 - .../cache/.run-auth/data/cache-upstreams.json | 1 - tests/cache/.run-auth/data/llm-stats.json | 1 - tests/cache/.run-auth/data/users.json | 1 - tests/cache/.run-auth/logs/cache.log | 189 --- tests/cache/.run-auth/logs/cookie.jar | 5 - tests/cache/.run-auth/logs/error.log | 684 ---------- tests/cache/.run-auth/logs/h7.body | 1 - tests/cache/.run-auth/logs/h7.head | 11 - tests/cache/.run-auth/logs/h9.head | 11 - tests/cache/.run-auth/logs/login.body | 1 - tests/cache/.run-auth/logs/probe.body | 1 - tests/cache/.run-auth/logs/upstream.log | 1 - tests/cache/.run-auth/logs/upstream.out | 1 - tests/cache/.run-auth/www | 1 - tests/cache/.run-cap/.cookies | 5 - tests/cache/.run-cap/.inv.json | 1 - tests/cache/.run-cap/.snap.json | 1 - .../0/23/e0e8d4befc6b8d7e9c4f4744d39a8230 | Bin 205393 -> 0 bytes .../npm/5/f8/3f31dfdcfabc98bc16b76f4911374f85 | Bin 205389 -> 0 bytes .../npm/9/84/91ed0ba5201896e793340b91924b1849 | Bin 205389 -> 0 bytes tests/cache/.run-cap/conf/cache-common.conf | 131 -- tests/cache/.run-cap/conf/cache-routes.conf | 177 --- tests/cache/.run-cap/conf/mime.types | 99 -- tests/cache/.run-cap/conf/nginx.cache.conf | 87 -- tests/cache/.run-cap/conf/server.cache.conf | 30 - tests/cache/.run-cap/data/api-proxies.json | 26 - .../cache/.run-cap/data/cache-upstreams.json | 10 - tests/cache/.run-cap/data/domains.json | 1 - tests/cache/.run-cap/data/llm-stats.json | 1 - tests/cache/.run-cap/data/llm-upstreams.json | 34 - tests/cache/.run-cap/data/users.json | 1 - tests/cache/.run-cap/logs/cache.log | 238 ---- tests/cache/.run-cap/logs/error.log | 720 ---------- tests/cache/.run-cap/logs/error.log.mark | 1 - tests/cache/.run-cap/logs/upstream.out | 10 - tests/cache/.run-cap/upstream.log | 10 - tests/cache/.run-cap/upstream.pid | 1 - tests/cache/.run-cap/www | 1 - tests/run.sh | 221 ---- 205 files changed, 164 insertions(+), 27456 deletions(-) create mode 100644 .gitmodules create mode 100644 AGENTS.md create mode 160000 deps/ipass create mode 100644 docs/plugins.md delete mode 100644 rust/crates/ipass-core/Cargo.toml delete mode 100644 rust/crates/ipass-core/src/api.rs delete mode 100644 rust/crates/ipass-core/src/auth.rs delete mode 100644 rust/crates/ipass-core/src/config.rs delete mode 100644 rust/crates/ipass-core/src/http.rs delete mode 100644 rust/crates/ipass-core/src/lib.rs delete mode 100644 rust/crates/ipass-core/src/loc.rs delete mode 100644 rust/crates/ipass-core/src/models.rs delete mode 100644 rust/crates/ipass-core/src/plugin.rs delete mode 100644 rust/crates/ipass-core/src/session.rs delete mode 100644 rust/crates/ipass-core/src/state.rs delete mode 100644 rust/crates/ipass-core/src/store.rs delete mode 100644 rust/crates/ipass-core/src/usage.rs delete mode 100644 rust/crates/ipass-core/src/users.rs delete mode 100644 rust/crates/ipass-core/src/util.rs delete mode 100644 rust/crates/plugin-apix/Cargo.toml delete mode 100644 rust/crates/plugin-apix/src/api.rs delete mode 100644 rust/crates/plugin-apix/src/apix.rs delete mode 100644 rust/crates/plugin-apix/src/config.rs delete mode 100644 rust/crates/plugin-apix/src/handlers.rs delete mode 100644 rust/crates/plugin-apix/src/lib.rs delete mode 100644 rust/crates/plugin-apix/src/models.rs delete mode 100644 rust/crates/plugin-apix/src/store.rs delete mode 100644 rust/crates/plugin-domain/Cargo.toml delete mode 100644 rust/crates/plugin-domain/src/api.rs delete mode 100644 rust/crates/plugin-domain/src/config.rs delete mode 100644 rust/crates/plugin-domain/src/domains.rs delete mode 100644 rust/crates/plugin-domain/src/lib.rs delete mode 100644 rust/crates/plugin-domain/src/models.rs delete mode 100644 rust/crates/plugin-domain/src/resolve.rs delete mode 100644 rust/crates/plugin-domain/src/store.rs delete mode 100644 rust/crates/plugin-llm/Cargo.toml delete mode 100644 rust/crates/plugin-llm/src/api.rs delete mode 100644 rust/crates/plugin-llm/src/config.rs delete mode 100644 rust/crates/plugin-llm/src/handlers.rs delete mode 100644 rust/crates/plugin-llm/src/lib.rs delete mode 100644 rust/crates/plugin-llm/src/llm.rs delete mode 100644 rust/crates/plugin-llm/src/llm_audit.rs delete mode 100644 rust/crates/plugin-llm/src/models.rs delete mode 100644 rust/crates/plugin-llm/src/stats.rs delete mode 100644 rust/crates/plugin-llm/src/stats_persist.rs delete mode 100644 rust/crates/plugin-llm/src/store.rs delete mode 100644 rust/crates/plugin-llm/src/upstreams.rs delete mode 100644 rust/crates/plugin-llm/src/usage_filter.rs delete mode 100644 rust/gateway/Cargo.toml delete mode 100644 rust/gateway/build.rs delete mode 100644 rust/gateway/src/api.rs delete mode 100644 rust/gateway/src/lib.rs delete mode 100644 rust/vendor/ngx-rust/.cargo/audit.toml delete mode 100644 rust/vendor/ngx-rust/.cargo/config.toml delete mode 100644 rust/vendor/ngx-rust/.dockerignore delete mode 100644 rust/vendor/ngx-rust/.git-blame-ignore-revs delete mode 100644 rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/bug_report.md delete mode 100644 rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/feature_request.md delete mode 100644 rust/vendor/ngx-rust/.github/PULL_REQUEST_TEMPLATE.md delete mode 100644 rust/vendor/ngx-rust/.github/dependabot.yml delete mode 100644 rust/vendor/ngx-rust/.github/workflows/cargo-deny.yaml delete mode 100644 rust/vendor/ngx-rust/.github/workflows/ci.yaml delete mode 100644 rust/vendor/ngx-rust/.github/workflows/nginx.yaml delete mode 100644 rust/vendor/ngx-rust/.github/workflows/sanitizers.yaml delete mode 100644 rust/vendor/ngx-rust/.gitignore delete mode 100644 rust/vendor/ngx-rust/.gitmodules delete mode 100644 rust/vendor/ngx-rust/CHANGELOG.md delete mode 100644 rust/vendor/ngx-rust/CODE_OF_CONDUCT.md delete mode 100644 rust/vendor/ngx-rust/CONTRIBUTING.md delete mode 100644 rust/vendor/ngx-rust/Cargo.toml delete mode 100644 rust/vendor/ngx-rust/Containerfile.debian delete mode 100644 rust/vendor/ngx-rust/Dockerfile delete mode 100644 rust/vendor/ngx-rust/GNUmakefile delete mode 100644 rust/vendor/ngx-rust/LICENSE delete mode 100644 rust/vendor/ngx-rust/README.md delete mode 100644 rust/vendor/ngx-rust/SECURITY.md delete mode 100644 rust/vendor/ngx-rust/build.rs delete mode 100644 rust/vendor/ngx-rust/deny.toml delete mode 100644 rust/vendor/ngx-rust/examples/Cargo.toml delete mode 100644 rust/vendor/ngx-rust/examples/README.md delete mode 100644 rust/vendor/ngx-rust/examples/async.conf delete mode 100644 rust/vendor/ngx-rust/examples/async.rs delete mode 100644 rust/vendor/ngx-rust/examples/auto/rust delete mode 100644 rust/vendor/ngx-rust/examples/awssig.conf delete mode 100644 rust/vendor/ngx-rust/examples/awssig.rs delete mode 100644 rust/vendor/ngx-rust/examples/awssig_curl_combined.conf delete mode 100644 rust/vendor/ngx-rust/examples/config delete mode 100644 rust/vendor/ngx-rust/examples/config.make delete mode 100644 rust/vendor/ngx-rust/examples/curl.conf delete mode 100644 rust/vendor/ngx-rust/examples/curl.rs delete mode 100644 rust/vendor/ngx-rust/examples/httporigdst.conf delete mode 100644 rust/vendor/ngx-rust/examples/httporigdst.rs delete mode 100644 rust/vendor/ngx-rust/examples/shared_dict.rs delete mode 100644 rust/vendor/ngx-rust/examples/t/async.t delete mode 100644 rust/vendor/ngx-rust/examples/t/awssig.t delete mode 100644 rust/vendor/ngx-rust/examples/t/curl.t delete mode 100644 rust/vendor/ngx-rust/examples/t/shared_dict.t delete mode 100644 rust/vendor/ngx-rust/examples/t/upstream.t delete mode 100644 rust/vendor/ngx-rust/examples/upstream.conf delete mode 100644 rust/vendor/ngx-rust/examples/upstream.rs delete mode 100644 rust/vendor/ngx-rust/misc/lsan-suppressions.txt delete mode 100644 rust/vendor/ngx-rust/misc/nginx-sanitizer-support.patch delete mode 100644 rust/vendor/ngx-rust/misc/ubsan-suppressions.txt delete mode 100755 rust/vendor/ngx-rust/misc/update-action-sha.sh delete mode 100644 rust/vendor/ngx-rust/nginx-src/Cargo.toml delete mode 120000 rust/vendor/ngx-rust/nginx-src/LICENSE delete mode 100644 rust/vendor/ngx-rust/nginx-src/README.md delete mode 100644 rust/vendor/ngx-rust/nginx-src/src/download.rs delete mode 100644 rust/vendor/ngx-rust/nginx-src/src/lib.rs delete mode 100644 rust/vendor/ngx-rust/nginx-src/src/verifier.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/Cargo.toml delete mode 120000 rust/vendor/ngx-rust/nginx-sys/LICENSE delete mode 100644 rust/vendor/ngx-rust/nginx-sys/README.md delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/detail.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/event.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/http.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/lib.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/mail.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/queue.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/rbtree.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/stream.rs delete mode 100644 rust/vendor/ngx-rust/nginx-sys/src/string.rs delete mode 100644 rust/vendor/ngx-rust/rustfmt.toml delete mode 100644 rust/vendor/ngx-rust/src/allocator.rs delete mode 100644 rust/vendor/ngx-rust/src/async_/mod.rs delete mode 100644 rust/vendor/ngx-rust/src/async_/resolver.rs delete mode 100644 rust/vendor/ngx-rust/src/async_/sleep.rs delete mode 100644 rust/vendor/ngx-rust/src/async_/spawn.rs delete mode 100644 rust/vendor/ngx-rust/src/collections/mod.rs delete mode 100644 rust/vendor/ngx-rust/src/collections/queue.rs delete mode 100644 rust/vendor/ngx-rust/src/collections/rbtree.rs delete mode 100644 rust/vendor/ngx-rust/src/core/buffer.rs delete mode 100644 rust/vendor/ngx-rust/src/core/conf.rs delete mode 100644 rust/vendor/ngx-rust/src/core/mod.rs delete mode 100644 rust/vendor/ngx-rust/src/core/pool.rs delete mode 100644 rust/vendor/ngx-rust/src/core/slab.rs delete mode 100644 rust/vendor/ngx-rust/src/core/status.rs delete mode 100644 rust/vendor/ngx-rust/src/core/string.rs delete mode 100644 rust/vendor/ngx-rust/src/ffi/mod.rs delete mode 100644 rust/vendor/ngx-rust/src/http/conf.rs delete mode 100644 rust/vendor/ngx-rust/src/http/mod.rs delete mode 100644 rust/vendor/ngx-rust/src/http/module.rs delete mode 100644 rust/vendor/ngx-rust/src/http/request.rs delete mode 100644 rust/vendor/ngx-rust/src/http/status.rs delete mode 100644 rust/vendor/ngx-rust/src/http/upstream.rs delete mode 100644 rust/vendor/ngx-rust/src/lib.rs delete mode 100644 rust/vendor/ngx-rust/src/log.rs delete mode 100644 rust/vendor/ngx-rust/src/sync.rs delete mode 100644 rust/vendor/ngx-rust/tests/conditional-compilation.rs delete mode 100644 rust/vendor/ngx-rust/tests/config/nginx.conf delete mode 100644 rust/vendor/ngx-rust/tests/log_test.rs delete mode 100644 rust/vendor/ngx-rust/tests/module/config delete mode 100644 rust/vendor/ngx-rust/tests/module/config.make delete mode 100644 rust/vendor/ngx-rust/tests/module/ngx_http_rust_module.c delete mode 100644 rust/vendor/ngx-rust/tests/nginx.conf delete mode 100644 tests/cache/.run-auth/cache/cargo/6/c4/56d612c5a01718fa4713b10d04281c46 delete mode 100644 tests/cache/.run-auth/cache/go/a/38/a2fabffdabc504877dd7c4883d13d38a delete mode 100644 tests/cache/.run-auth/cache/go/c/3c/fc25799d28960c2f86b96ce2383e23cc delete mode 100644 tests/cache/.run-auth/cache/npm/3/9b/21687620212a451b79a08052337649b3 delete mode 100644 tests/cache/.run-auth/conf/cache-common.conf delete mode 100644 tests/cache/.run-auth/conf/cache-routes.conf delete mode 100644 tests/cache/.run-auth/conf/mime.types delete mode 100644 tests/cache/.run-auth/conf/nginx.cache.conf delete mode 100644 tests/cache/.run-auth/conf/server.cache.conf delete mode 100644 tests/cache/.run-auth/data/cache-upstreams.json delete mode 100644 tests/cache/.run-auth/data/llm-stats.json delete mode 100644 tests/cache/.run-auth/data/users.json delete mode 100644 tests/cache/.run-auth/logs/cache.log delete mode 100644 tests/cache/.run-auth/logs/cookie.jar delete mode 100644 tests/cache/.run-auth/logs/error.log delete mode 100644 tests/cache/.run-auth/logs/h7.body delete mode 100644 tests/cache/.run-auth/logs/h7.head delete mode 100644 tests/cache/.run-auth/logs/h9.head delete mode 100644 tests/cache/.run-auth/logs/login.body delete mode 100644 tests/cache/.run-auth/logs/probe.body delete mode 100644 tests/cache/.run-auth/logs/upstream.log delete mode 100644 tests/cache/.run-auth/logs/upstream.out delete mode 120000 tests/cache/.run-auth/www delete mode 100644 tests/cache/.run-cap/.cookies delete mode 100644 tests/cache/.run-cap/.inv.json delete mode 100644 tests/cache/.run-cap/.snap.json delete mode 100644 tests/cache/.run-cap/cache/maven/0/23/e0e8d4befc6b8d7e9c4f4744d39a8230 delete mode 100644 tests/cache/.run-cap/cache/npm/5/f8/3f31dfdcfabc98bc16b76f4911374f85 delete mode 100644 tests/cache/.run-cap/cache/npm/9/84/91ed0ba5201896e793340b91924b1849 delete mode 100644 tests/cache/.run-cap/conf/cache-common.conf delete mode 100644 tests/cache/.run-cap/conf/cache-routes.conf delete mode 100644 tests/cache/.run-cap/conf/mime.types delete mode 100644 tests/cache/.run-cap/conf/nginx.cache.conf delete mode 100644 tests/cache/.run-cap/conf/server.cache.conf delete mode 100644 tests/cache/.run-cap/data/api-proxies.json delete mode 100644 tests/cache/.run-cap/data/cache-upstreams.json delete mode 100644 tests/cache/.run-cap/data/domains.json delete mode 100644 tests/cache/.run-cap/data/llm-stats.json delete mode 100644 tests/cache/.run-cap/data/llm-upstreams.json delete mode 100644 tests/cache/.run-cap/data/users.json delete mode 100644 tests/cache/.run-cap/logs/cache.log delete mode 100644 tests/cache/.run-cap/logs/error.log delete mode 100644 tests/cache/.run-cap/logs/error.log.mark delete mode 100644 tests/cache/.run-cap/logs/upstream.out delete mode 100644 tests/cache/.run-cap/upstream.log delete mode 100644 tests/cache/.run-cap/upstream.pid delete mode 120000 tests/cache/.run-cap/www delete mode 100755 tests/run.sh diff --git a/.gitignore b/.gitignore index a92c427..d5a327d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,8 +8,9 @@ target/ **/*.o # ===== 测试运行态实例(缓存文件 / nginx 进程 / 日志 / 临时实例)===== -tests/**/.run-dev/ -.run-dev/ +# 所有 .run-* 运行时目录(.run-dev / .run-auth / .run-cap ...)一律不入库 +tests/**/.run-*/ +.run-*/ # ===== 工作区数据(非项目源码)===== .workbuddy/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1ed6f50 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "deps/ipass"] + path = deps/ipass + url = http://ssw:sswsswssw@192.168.1.18:13300/ipass/ipass.git diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..bfa397e --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,67 @@ +# AGENTS.md — iPass cache 网关(插件仓) + +面向 AI agent 的项目上手与改造约束。先读这份,再碰代码。 + +## 这是什么 +iPass 统一制品缓存网关的**插件仓**(内网 Gitea:`ipass/cache`)。 +一个 nginx 入口 + 一把统一 `sk_` Key + 8 个路由(go / cargo / cargo-dl / pypi / pypi-files / npm / maven / docker v2),数据面用 nginx 原生 `proxy_cache`(本地磁盘),鉴权 / 计量 / 上游表由 Rust 模块管。 + +**本仓只含 cache 插件本身的源码**(见下「仓库边界」)。内核 `ipass-core`、模块壳 `gateway`、vendored `ngx-rust`、以及 domain/llm/apix 等其他子系统,**不在本仓**——它们属于主项目 `ipass/ipass`,本仓通过 git submodule(`deps/ipass`)引用,仅用于编译 cache 插件时解析依赖。 + +## 仓库边界(改前必看) +- ✅ 入库(本仓库职责):`rust/crates/plugin-cache`(cache 插件源码)、`conf`(cache 网关 nginx 模板)、`www`(cache 管理台前端)、`tests/cache`(cache 端到端)、`docs`、`AGENTS.md`、`docs/plugins.md`。 +- 🔗 以 submodule 引入(**非本仓源码**,来自 `ipass/ipass`,不要在本仓改):`deps/ipass/`(`ipass-core` / `gateway` / `vendor/ngx-rust` / `plugin-domain` / `plugin-llm` / `plugin-apix`)。 +- ❌ 不入库(见 `.gitignore`):`rust/target/`、`build/`、`*.dylib/*.so`、`tests/cache/.run-*/`(所有测试运行时目录)、`.workbuddy/`。 +- 远程:`origin = http://ssw:sswsswssw@192.168.1.18:13300/ipass/cache.git`,分支 `main`。 + +> ⚠️ **避免相互干扰**:本仓与主项目 `ipass/ipass` 代码分离。cache 插件的全部修改只落在本仓;`ipass-core` 等内核/框架的修改回到 `ipass/ipass`。改内核共享内存 schema 前务必评估对所有插件的影响。 + +## 代码布局 +本仓是一个**单成员 cargo workspace**,只编 `plugin-cache`: + +| 组件 | 位置 | 版本 | 职责 | 归属 | +|------|------|------|------|------| +| `plugin-cache` | `rust/crates/plugin-cache` | 0.1.0 | 统一制品缓存:8 路由代理、命中计量、上游动态注入、管理 API、容量管理 | ★ 本仓库 | +| `ipass-core` | `deps/ipass/rust/crates/ipass-core`(submodule) | 2.0.0 | 内核:共享内存 / 账号 / 会话 / 认证 / 存储 / 通用原语 | 主项目 `ipass/ipass` | +| `gateway` | `deps/ipass/rust/gateway`(submodule) | 2.0.0 | 唯一 cdylib:聚合插件 `ngx_module_t`、FFI 边界 | 主项目 | +| `vendor/ngx-rust` | `deps/ipass/rust/vendor/ngx-rust`(submodule) | — | vendored `ngx` + `nginx-sys`(离线编译) | 主项目(vendored) | +| `plugin-domain` / `plugin-llm` / `plugin-apix` | `deps/ipass/rust/crates/*`(submodule) | 2.0.0 | 其他子系统 | 主项目各子系统 | + +`plugin-cache` 的依赖(`ipass-core` / `ngx` / `nginx-sys`)在 `rust/Cargo.toml` 中通过 `../deps/ipass/rust/...` 指向 submodule。依赖方向单向:`plugin-cache` → `ipass-core` → `vendor/ngx-rust` → nginx C 进程;跨 `.so` 不传 Rust 类型(否则双重 free),所以只有 `gateway` 是 cdylib,其余全是 rlib。 + +## 构建 +前置:本仓需要主项目代码来解析依赖。 +```bash +git submodule update --init deps/ipass # 拉取 ipass-core / gateway / ngx-rust ... +``` +- **仅类型检查 / 编译插件(rlib)**:`cd rust && cargo build -p plugin-cache`。 + 这会顺着 `../deps/ipass` 把 `ipass-core` 与 `nginx-sys` 一起编出来(nginx-sys 需要 `NGINX_SOURCE_DIR` / `NGINX_BUILD_DIR` 指向一份 nginx 源码,见技能 `nginx-rs-module-local-verify`)。 +- **产出可加载模块(cdylib)并跑端到端**:本仓没有 `gateway`(cdylib 在 submodule 里)。要在本机得到 `libipass_gateway.dylib` 并运行 `tests/cache/*.sh`,请在主项目 `ipass/ipass` 中装配 cache 插件: + 1. 把本仓的 `rust/crates/plugin-cache` 放到 `ipass/ipass/rust/crates/plugin-cache`(或让主项目以 submodule/override 消费本仓); + 2. 在 `ipass/ipass/rust/Cargo.toml` 的 `members` 加 `"crates/plugin-cache"`,在 `ipass/ipass/rust/gateway/Cargo.toml` 的 `[dependencies]` 加 `plugin-cache.workspace = true`; + 3. 按技能 `nginx-rs-module-local-verify` 编译 `gateway` 得到 `.dylib`,再以 `tests/cache/dev-server.sh` / `test-cache*.sh` 验证。 + > 主项目 `ipass/ipass` 当前的 `gateway` 只装配 domain/llm/apix(不含 cache),所以上面第 2 步是接 cache 的唯一接线点。 +- macOS 编译 nginx 必须 `-DNGX_HAVE_SCHED_SETAFFINITY=0`;`[profile.release]` 只能放 workspace 根(`panic="abort"`,丢了意味着 panic 跨 FFI 边界展开进 nginx)。 + +## 端到端验证(基线,连跑两轮一致) +cache 测试都在 `tests/cache/`: +- `tests/cache/test-cache.sh` 23/23、`tests/cache/test-ipass-cache.sh` 24/24、`tests/cache/validate-prod-conf.sh` ✅、`tests/cache/e2e-auth.sh` 29/29。 +- 容量管理:`tests/cache/test-cache-capacity.sh` 48/48。 +- 跑法见 `tests/cache/run.sh`(或单独 `bash tests/cache/<脚本>`);任何「读上游日志做断言」的步骤先断言本次 MISS,且测试开头清 `$RUN/cache`。 + +## 不可动摇的架构约定(改前要懂) +1. 上游地址 / 凭据只存共享内存 `cx:`(管理台写穿,落盘 `data/cache-upstreams.json`);conf 只出现变量名 `$cache_target` / `$cache_host` / `$cache_auth`。改上游不改 conf、不 reload。 +2. `$cache_key` 必须含 query(`:[?args]`);`proxy_cache_key = $cache_key$request_method$http_accept`(不含 `sk_`)。 +3. `sk_` 绝不外传上游:出站 `Authorization` 三段式(本路由凭据 → 透传客户端值 → 丢弃网关 `sk_`);同时抹掉 `x-api-key` / `api-key`。 +4. 未配上游的路由 → 503(不是 500)。 +5. **唯一 conf 渲染器**:`tests/cache/lib-render.sh`(`cache_render_prod_conf` / `cache_write_upstreams`)。禁止手抄第二份「生产 conf → 测试前缀」替换逻辑。 +6. 清理缓存**只删文件,绝不删 `cache/` 目录本身**(否则 `[crit] mkdir failed` + 客户端断开);批量清理用 `find <子树> -type f -delete` + 残留自检。 +7. 生成 / 改写 conf 一律用 python,不用 BSD `sed`;脚本里 `curl` 统一 `--noproxy '*'`(本机代理会吞 localhost)。 + +## 提交规范 +- 只提交源码(`.gitignore` 已排除产物);push 到 `ipass/cache` 的 `main`。 +- **改动范围**:只动本仓的 `plugin-cache` / `conf` / `www` / `tests/cache` / `docs`。若要升级 `ipass-core` 等,请到 `ipass/ipass` 改,或在本仓 `git submodule update` 拉新提交后 `git add deps/ipass`。**禁止在 `deps/ipass/` 里直接改代码**(那是主项目源码)。 +- 大 push 设 `http.postBuffer=524288000`;若报 `unable to get credential storage lock`,先 `rm -f ~/.git-credentials.lock`(无 git 进程时)。 + +## 插件边界 +见 [`docs/plugins.md`](docs/plugins.md)。 diff --git a/deps/ipass b/deps/ipass new file mode 160000 index 0000000..51f322e --- /dev/null +++ b/deps/ipass @@ -0,0 +1 @@ +Subproject commit 51f322e983f685d4b1fd9bbf39484273beffc0c8 diff --git a/docs/plugins.md b/docs/plugins.md new file mode 100644 index 0000000..fbf4830 --- /dev/null +++ b/docs/plugins.md @@ -0,0 +1,79 @@ +# 插件边界说明:本仓库(cache) vs 主项目(ipass/ipass) + +## 仓库拆分原则 +为避免 cache 插件与 iPass 主框架相互干扰,本仓 `ipass/cache` **只保留 cache 插件本身的源码**;内核 `ipass-core`、模块壳 `gateway`、vendored `ngx-rust`、以及 domain/llm/apix 等其他子系统,**全部留在主项目 `ipass/ipass`**,本仓以 git submodule 形式引用: + +``` +本仓 ipass/cache +├── rust/crates/plugin-cache ← 本仓唯一源码成员(cache 插件) +├── conf/ www/ tests/cache/ docs/ ← cache 专属资产 +└── deps/ipass/ (submodule → http://192.168.1.18:13300/ipass/ipass) + ├── rust/crates/ipass-core ← 内核(主项目维护) + ├── rust/gateway ← 唯一 cdylib(主项目) + ├── rust/vendor/ngx-rust ← vendored 绑定(主项目 pin) + └── rust/crates/plugin-domain|llm|apix ← 其他子系统(主项目) +``` + +本仓 `rust/Cargo.toml` 的 workspace 仅含 `crates/plugin-cache`,其依赖通过 `../deps/ipass/rust/...` 解析到 submodule。 + +## 依赖方向(单向) +``` +plugin-cache(本仓) + │ 依赖 + ▼ + ipass-core(submodule)──▶ vendor/ngx-rust(submodule)──▶ nginx C 进程 + ▲ + gateway(submodule,唯一 cdylib,聚合 plugin 的 ngx_module_t) +``` +只有 `gateway` 是 cdylib,其余全是 rlib(跨 `.so` 传 Rust 类型会双重 free)。 + +## 本仓库负责(cache) +### `plugin-cache`(rust/crates/plugin-cache,v0.1.0)★ 本仓唯一源码 +统一制品缓存插件,本仓库的核心交付: +- 8 路由制品代理:go / cargo / cargo-dl / pypi / pypi-files / npm / maven / docker v2 +- 命中计量(`stats.rs`)、上游动态注入(`upstreams.rs` + `cx:` 共享内存写穿) +- 管理 API(`api.rs` / `handlers.rs`):`GET /api/admin/cache` 容量、`POST /api/admin/cache/purge` 整区清理、`POST /api/admin/cache/invalidate` 按制品精准失效 +- 容量管理(`zones.rs`:直读 nginx `file_cache` 共享内存记账,双轴磁盘 + keys_zone 索引内存) +- 配套前端:`www/cache.html` + `www/assets/cache.js`(容量面板 + 按制品失效工具条) +- 测试:`tests/cache/test-cache-capacity.sh`(48/48)、`test-real-registries.sh`、`verify-cache-eviction.sh` +- 文档:`docs/cache-usage.md` + +> 改 cache 的路由行为、容量、purge / invalidate,只动本仓的 `plugin-cache` 与 `www/assets/cache.js`。 + +## 主项目维护(ipass/ipass,经 submodule 引入) +### `ipass-core`(deps/ipass/rust/crates/ipass-core,v2.0.0)— 内核 +共享内存原语(`cx:` / `ust:` / `ums:` / `ud:` 键空间)、账号 / 会话 / 认证(`auth::downstream_key`,`sk_` 三种携带位统一)、存储、通用 HTTP/api 框架、`config` / `state` / `usage` 记账原语。所有 plugin 依赖它,**方向只能是 plugin → core**。共享内存 schema 变更会影响全部 plugin,改前务必评估。 + +### `gateway`(deps/ipass/rust/gateway)— 唯一 cdylib +nginx 模块入口;聚合所有 plugin 的 `ngx_module_t`;FFI 边界(`panic="abort"`,跨 `.so` 不传 Rust 类型);模块加载顺序。**注意**:主项目 `gateway` 当前只装配 domain/llm/apix(不含 cache),要把 cache 编进可加载模块,需在主项目把 `plugin-cache` 加进 `members` 与 `gateway` 依赖(见 AGENTS.md「构建」)。 + +### `plugin-domain` / `plugin-llm` / `plugin-apix`(deps/ipass/rust/crates/*)— 其他子系统 +- `plugin-domain`:动态域名(子域名映射 / 申请审批 / 转发决策) +- `plugin-llm`:LLM 路由 / 上游管理 / 配额计费 / 对话审计 / 用量趋势 +- `plugin-apix`:API 代理(外部供应商资产挂网关) +由对应子系统团队在 `ipass/ipass` 维护;本仓不持有其源码,也不在其职责内。 + +### `vendor/ngx-rust`(deps/ipass/rust/vendor/ngx-rust)— 上游 vendored +`ngx` + `nginx-sys`(Rust 的 nginx C 绑定 fork)。主项目 pin 版本、vendored 以离线编译;上游是 ngx-rust 项目,iPass 维护其 vendoring 与 `panic="abort"` 配置。升级需重 pin + 全量重编译。 + +### nginx / 构建链(build/)— 外部依赖 +`build/nginx-1.30.4`、OpenSSL、PCRE2 源码与构建脚本;生产带 SSL 的完整 nginx 由 iPass 运维维护。本仓 `build/` 不入库。 + +## 责任边界速查 +| 组件 | 物理位置 | 归属 | 改前注意 | +|------|----------|------|----------| +| `plugin-cache` | 本仓 `rust/crates/plugin-cache` | 本仓库(cache) | 容量 / purge / invalidate 逻辑在此 | +| `ipass-core` | submodule `deps/ipass/...` | 主项目 `ipass/ipass` | 共享内存 schema 变更影响所有 plugin | +| `gateway` | submodule `deps/ipass/...` | 主项目 | 模块加载 / FFI 边界;当前不含 cache | +| `plugin-domain` / `plugin-llm` / `plugin-apix` | submodule `deps/ipass/...` | 主项目各子系统 | 不在本仓库职责内 | +| `vendor/ngx-rust` | submodule `deps/ipass/...` | 上游(vendored, 主项目 pin) | 升级需重 pin + 重编译 | +| nginx / 构建链 | 外部 | 外部 | 生产 SSL conf 在主项目运维 | + +## 如何更新内核 / 框架版本 +本仓记录的 submodule 提交钉在 `.gitmodules` / gitlink。要跟进主项目的新内核: +```bash +cd deps/ipass && git fetch && git checkout <新commit-or-main> && cd - +git add deps/ipass # 更新 gitlink +git commit -m "bump ipass-core via submodule" +``` +**不要在 `deps/ipass/` 里直接改代码**——那是主项目的源码,改了也不会被本仓提交,且会与上游冲突。 diff --git a/rust/Cargo.toml b/rust/Cargo.toml index a56672a..643a697 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -5,9 +5,11 @@ # panic="abort" 丢了就意味着 panic 可能跨 FFI 边界展开进 nginx。 [workspace] resolver = "2" -members = ["crates/ipass-core", "crates/plugin-domain", "crates/plugin-llm", "crates/plugin-apix", "crates/plugin-cache", "gateway"] -# 独立 workspace:vendored 的 ngx-rust 本身是 workspace 根,需显式排除以免歧义 -exclude = ["vendor/ngx-rust"] +members = ["crates/plugin-cache"] +# 独立 workspace:主项目 ipass/ipass 经 submodule 引入到仓库根的 deps/ipass, +# 需显式排除(路径相对本 Cargo.toml 所在目录 rust/,故是 ../deps/ipass), +# 以免与主项目自身的 workspace 冲突;target 也排除。 +exclude = ["../deps/ipass", "target"] [workspace.package] edition = "2021" @@ -30,8 +32,10 @@ rust-version = "1.81.0" # GitCheckout::update_submodules -> fetch -> wait4)。 # # 因此 vendor 到 vendor/ngx-rust/,用 path 依赖:离线可编译、无子模块、版本锁定。 -ngx = { path = "vendor/ngx-rust" } -nginx-sys = { path = "vendor/ngx-rust/nginx-sys" } +# 本仓只含 cache 插件,ipass-core / gateway / ngx-rust 全部从主项目 ipass/ipass +# (http://192.168.1.18:13300/ipass/ipass)经 submodule deps/ipass 引入。 +ngx = { path = "../deps/ipass/rust/vendor/ngx-rust" } +nginx-sys = { path = "../deps/ipass/rust/vendor/ngx-rust/nginx-sys" } # 业务层依赖:全部是纯 Rust 实现,不引入任何系统库或常驻依赖 serde = { version = "1", features = ["derive"] } @@ -40,11 +44,8 @@ sha2 = "0.10" # 迭代加盐 SHA-256;hash_password 算法已用独 getrandom = "0.2" # 不可退化成可预测伪随机(API Key / salt 由它生成) libc = "0.2" # 仅用于 localtime_r(取本地时区) -# 内核 crate(插件/gateway 一律引用它,方向只能是 plugin → core) -ipass-core = { path = "crates/ipass-core" } -plugin-domain = { path = "crates/plugin-domain" } -plugin-llm = { path = "crates/plugin-llm" } -plugin-apix = { path = "crates/plugin-apix" } +# 内核 crate:从主项目 ipass/ipass 经 submodule deps/ipass 引入;方向只能是 plugin → core +ipass-core = { path = "../deps/ipass/rust/crates/ipass-core" } plugin-cache = { path = "crates/plugin-cache" } [profile.release] diff --git a/rust/crates/ipass-core/Cargo.toml b/rust/crates/ipass-core/Cargo.toml deleted file mode 100644 index 4064ede..0000000 --- a/rust/crates/ipass-core/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "ipass-core" -version = "2.0.0" -edition.workspace = true -publish = false -description = "iPass 内核:共享内存 / 账号 / 会话 / 存储 / 通用原语(不含任何插件业务语义)" - -[lib] -# 只有 gateway 是 cdylib;内核与插件一律 rlib(动态模块的 ngx_module_t 签名 -# 必须与 nginx 同源,且跨 .so 传 Rust 类型会双重 free,见 sso 的架构分析文档) -crate-type = ["rlib"] - -[dependencies] -ngx.workspace = true -nginx-sys.workspace = true -serde.workspace = true -serde_json.workspace = true -sha2.workspace = true -getrandom.workspace = true -libc.workspace = true diff --git a/rust/crates/ipass-core/src/api.rs b/rust/crates/ipass-core/src/api.rs deleted file mode 100644 index b1c6506..0000000 --- a/rust/crates/ipass-core/src/api.rs +++ /dev/null @@ -1,486 +0,0 @@ -//! 身份类端点(登录 / 会话 / 账号管理)+ API 分发的公共契约。 -//! -//! 会话与账号是内核职责(所有插件端点的公共前置),所以这批端点由 -//! `CorePlugin` 直接认领,gateway 把它排在 `PLUGINS` 最前。 -//! -//! ## `CoreExt`:身份端点的插件扩展点 -//! -//! 身份端点偶尔需要**插件侧数据**(账号列表要带域名数与用量、删账号要连带 -//! 回收域名、白名单要校验上游存在)。内核不认识插件,这些数据一律通过 -//! [`CoreExt`] 反转:gateway 作为**装配根**实现并注册([`set_ext`]), -//! 没注册时端点仍可工作(缺装饰、零回收),只是功能变素。 -//! -//! 路由与鉴权原语([`Guard`] / [`Actor`] / [`run_guarded`] / [`user_or_401`]) -//! 同时供各插件的路由表复用——四份 route 表共用一套鉴权语义。 - -use std::sync::Mutex; - -use ngx::core::Status; -use ngx::ffi::ngx_http_request_t; -use ngx::http::HTTPStatus; -use serde_json::{Value, json}; - -use crate::config; -use crate::http; -use crate::models::User; -use crate::plugin::Plugin; -use crate::session; -use crate::state; -use crate::users; -use crate::util; - -// =========================================================================== -// 鉴权契约(供内核与各插件的路由表共用) -// =========================================================================== - -/// 鉴权后传给处理器的身份 -pub enum Actor { - /// 公开接口,无身份 - None, - /// 账号态(X-API-Key) - User(User), - /// 管理员态(登录会话) - Admin(User), -} - -impl Actor { - /// 操作者名称:审核记录 `reviewed_by`、审计日志都用它 - pub fn admin_name(&self) -> &str { - match self { - Actor::Admin(u) => &u.username, - Actor::User(u) => &u.username, - Actor::None => "", - } - } -} - -#[derive(Clone, Copy, PartialEq)] -pub enum Guard { - Public, - User, - Admin, -} - -pub type Handler = fn(*mut ngx_http_request_t, Actor) -> Status; - -/// 账号态接口的公共前置:取账号身份,取不到就回 401(响应已发出)。 -pub fn user_or_401(r: *mut ngx_http_request_t, actor: Actor) -> Result { - match actor { - Actor::User(u) => Ok(u), - _ => Err(http::fail( - r, - HTTPStatus::UNAUTHORIZED, - "API Key 无效或缺失,请在 Authorization: Bearer 或 X-API-Key 中携带", - )), - } -} - -/// 账号态鉴权(统一 `sk_` Key → users.json 反查)。失败只记日志,由调用方回 401。 -/// -/// 携带位与**数据面**(`auth::downstream_key`)保持一致:优先 `X-API-Key` -/// (历史行为,不改语义),没有再退回 `Authorization: Bearer` / `api-key`。 -/// -/// 为什么必须统一:同一把 `sk_` 在数据面三处都认(`/go/`、`/v1/chat/...`), -/// 但账号态接口只认 `X-API-Key` 时,AI Agent 会撞上「能拉制品、却读不到自己 -/// 的用量」——实测症状就是 `Authorization: Bearer sk_...` 打 -/// `GET /api/v1/cache/stats` 得到 401,而同一个 Key 打 `/go/` 是 200。 -/// 注意:`X-API-Key` 存在但校验失败时**不回退**,避免把「Key 写错」掩盖成 200。 -pub fn authenticate_user(r: *mut ngx_http_request_t) -> Option { - let key = match http::header(r, "x-api-key") { - Some(k) if !k.trim().is_empty() => k.trim().to_owned(), - _ => crate::auth::downstream_key(r)?, - }; - match users::find_by_key(&key) { - Some(u) => Some(u), - None => { - util::log_warn(&format!( - "[api] API Key 校验失败 client={}", - http::remote_addr(r) - )); - None - } - } -} - -/// 管理员态鉴权(会话 Cookie → 账号)。会话有效但非管理员回 403 而不是 401, -/// 便于前端区分;两种失败都已把响应写进 r,调用方直接终止即可。 -pub fn authenticate_admin(r: *mut ngx_http_request_t) -> Result { - let cookie = http::header(r, "cookie"); - match session::authenticate(cookie.as_deref()).and_then(|name| users::get(&name)) { - Some(u) if u.is_admin() => Ok(u), - Some(_) => Err(http::fail(r, HTTPStatus::FORBIDDEN, "需要管理员权限")), - None => Err(http::fail( - r, - HTTPStatus::UNAUTHORIZED, - "未登录或会话已过期,请重新登录", - )), - } -} - -/// 鉴权 + 执行:按 Guard 求出 Actor 后调用 handler。 -pub fn run_guarded(r: *mut ngx_http_request_t, guard: Guard, handler: Handler) -> Status { - let actor = match guard { - Guard::Public => Actor::None, - Guard::User => match authenticate_user(r) { - Some(u) => Actor::User(u), - None => { - return http::fail( - r, - HTTPStatus::UNAUTHORIZED, - "API Key 无效或缺失,请在 Authorization: Bearer 或 X-API-Key 中携带", - ); - } - }, - Guard::Admin => match authenticate_admin(r) { - Ok(u) => Actor::Admin(u), - Err(status) => return status, - }, - }; - handler(r, actor) -} - -// =========================================================================== -// 插件扩展点 -// =========================================================================== - -/// 身份端点的插件扩展点。gateway 实现并注册;**方法都给缺省实现**, -/// 没注册扩展时端点仍可用(账号列表缺装饰、删账号不连带回收)。 -pub trait CoreExt: Sync { - /// 管理台账号列表:插件侧装饰(域名数 / 用量摘要),一次批量给全部账号 - fn decorate_admin_users(&self, _views: &mut [Value]) {} - /// `/api/v1/me`:插件侧装饰(我的域名 / 用量摘要 / 策略 TTL / 能力目录)。 - /// - /// `_user` 直接传 `User` 而不只是 username:能力目录(upstreams/apix) - /// 需要按 `allowed_models` 白名单过滤,让 decorate 零额外 shm 查询就能做权限收敛。 - /// `_port` 从请求 `Host` 头取,**不要写死**——apix/upstreams 的 `view_all` 要 - /// 用它拼 `https://lm.mayishangshu.cn{port}/...`,部署在 88 时必须带 `:88`, - /// 放 443 反代后面又不能带端口(见 `plugin_apix::call_url` / `plugin_llm::client_base`)。 - fn decorate_me(&self, _user: &crate::models::User, _port: &str, _view: &mut Value) {} - /// 删除账号:连带回收该账号名下资源(域名),返回条数 - fn reclaim_on_delete(&self, _username: &str) -> usize { - 0 - } - /// 账号「可用上游」白名单校验:slot 必须真实存在(plugin-llm 提供) - fn validate_slots(&self, _slots: &[String]) -> Result<(), String> { - Ok(()) - } -} - -static EXT: Mutex> = Mutex::new(None); - -/// gateway 在配置解析前注册(装配根是唯一注册方) -pub fn set_ext(e: &'static dyn CoreExt) { - if let Ok(mut slot) = EXT.lock() { - *slot = Some(e); - } -} - -fn ext() -> Option<&'static dyn CoreExt> { - let guard = EXT.lock().ok()?; - guard.as_ref().copied() -} - -// =========================================================================== -// 路由 -// =========================================================================== - -/// 路由表。刻意用精确匹配而非正则——接口路径是固定的, -/// 正则只会引入「写错锚点导致前缀误匹配」的风险。 -fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> { - let entry: (Guard, Handler) = match (method, uri) { - ("POST", "/api/admin/login") => (Guard::Public, h_login), - ("POST", "/api/admin/logout") => (Guard::Admin, h_logout), - ("GET", "/api/admin/session") => (Guard::Admin, h_session), - ("GET", "/api/admin/users") => (Guard::Admin, h_admin_users), - ("POST", "/api/admin/users/create") => (Guard::Admin, h_admin_user_create), - ("POST", "/api/admin/users/key") => (Guard::Admin, h_admin_user_key), - ("POST", "/api/admin/users/grant") => (Guard::Admin, h_admin_user_grant), - ("POST", "/api/admin/users/password") => (Guard::Admin, h_admin_user_password), - ("POST", "/api/admin/users/delete") => (Guard::Admin, h_admin_user_delete), - ("POST", "/api/admin/users/models") => (Guard::Admin, h_admin_user_models), - ("POST", "/api/admin/users/limits") => (Guard::Admin, h_admin_user_limits), - ("POST", "/api/admin/users/display") => (Guard::Admin, h_admin_user_display), - ("GET", "/api/v1/me") => (Guard::User, h_me), - _ => return None, - }; - Some(entry) -} - -/// 本表分发:认领则返回响应状态,不认领返回 `None`(交给下一个插件)。 -pub fn dispatch(r: *mut ngx_http_request_t) -> Option { - let method = http::method_str(r); - let uri = http::uri(r); - - let Some((guard, handler)) = route(&method, &uri) else { - return None; - }; - - Some(run_guarded(r, guard, handler)) -} - -// =========================================================================== -// 管理员:登录 / 会话 -// =========================================================================== - -fn h_login(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let ip = http::remote_addr(r); - let now = util::now(); - - if state::get_login_fail(&ip, now) >= config::LOGIN_MAX_FAIL { - return http::fail(r, HTTPStatus::TOO_MANY_REQUESTS, "登录失败次数过多,请 5 分钟后再试"); - } - - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - let password = p.get("password").cloned().unwrap_or_default(); - - if username.is_empty() || password.is_empty() { - return http::fail(r, HTTPStatus::BAD_REQUEST, "请输入账号和密码"); - } - - let Some(user) = users::get(&username) else { - state::bump_login_fail(&ip, now); - util::log_warn(&format!("[api] 登录失败 user={username} client={ip}")); - return http::fail(r, HTTPStatus::UNAUTHORIZED, "账号或密码错误"); - }; - - if !util::verify_password(&password, &user.salt, &user.pwd_hash) { - state::bump_login_fail(&ip, now); - util::log_warn(&format!("[api] 登录失败 user={username} client={ip}")); - return http::fail(r, HTTPStatus::UNAUTHORIZED, "账号或密码错误"); - } - - if !user.is_admin() { - return http::fail(r, HTTPStatus::FORBIDDEN, "该账号不是管理员,无法登录管理台"); - } - - state::clear_login_fail(&ip); - let token = session::create(&username); - let cookie = session::set_cookie(&token); - util::log_notice(&format!("[api] 管理员登录 user={username} client={ip}")); - - http::ok_with(r, users::view(&user), "登录成功", &[("Set-Cookie", &cookie)]) -} - -fn h_logout(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let cookie = http::header(r, "cookie"); - session::destroy(cookie.as_deref()); - let cleared = session::clear_cookie(); - http::ok_with(r, json!({}), "已退出登录", &[("Set-Cookie", &cleared)]) -} - -fn h_session(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let Actor::Admin(user) = actor else { - return http::fail(r, HTTPStatus::UNAUTHORIZED, "未登录或会话已过期,请重新登录"); - }; - http::ok(r, users::view(&user), "ok") -} - -// =========================================================================== -// 管理员:账号管理 -// =========================================================================== - -fn h_admin_users(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let mut items: Vec = users::all().iter().map(|u| users::view(u)).collect(); - // 域名数 / 用量等插件数据由装配根装饰(见 CoreExt::decorate_admin_users) - if let Some(e) = ext() { - e.decorate_admin_users(&mut items); - } - http::ok(r, json!({ "items": items }), "ok") -} - -fn h_admin_user_create(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let grant_temp = util::to_bool(p.get("grant_temp")); - - match users::create( - p.get("username").map(String::as_str).unwrap_or(""), - p.get("display_name").map(String::as_str).unwrap_or(""), - p.get("password").map(String::as_str).unwrap_or(""), - p.get("role").map(String::as_str).unwrap_or("user"), - grant_temp, - ) { - Ok(user) => { - util::log_notice(&format!( - "[api] 创建账号 {} name={} role={} temp_grant={grant_temp}", - user.username, user.display_name, user.role - )); - http::ok(r, users::view(&user), "账号创建成功") - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -/// 设置展示名(中文姓名等)。传空串 = 清空,前端回落到账号名。 -fn h_admin_user_display(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - - match users::set_display_name(&username, p.get("display_name").map(String::as_str).unwrap_or("")) { - Ok(u) => http::ok(r, users::view(&u), "已更新展示名"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_user_key(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - - match users::regenerate_key(&username) { - Ok(key) => http::ok(r, json!({ "username": username, "api_key": key }), "已重置 API Key"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_user_grant(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - - if util::to_bool(p.get("revoke")) { - if let Err(e) = users::revoke_temp(&username) { - return http::fail(r, HTTPStatus::BAD_REQUEST, &e); - } - let view = users::get(&username).map(|u| users::view(&u)).unwrap_or(Value::Null); - return http::ok(r, view, "已取消临时授权权限"); - } - - match users::grant_temp(&username, p.get("hours")) { - Ok(_) => { - let view = users::get(&username).map(|u| users::view(&u)).unwrap_or(Value::Null); - http::ok(r, view, "已授予临时授权权限") - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_user_password(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - let password = p.get("password").map(String::as_str).unwrap_or(""); - - match users::set_password(&username, password) { - Ok(()) => http::ok(r, json!({}), "密码已重置"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_user_delete(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - - if let Err(e) = users::delete(&username, actor.admin_name()) { - return http::fail(r, HTTPStatus::BAD_REQUEST, &e); - } - - // 连带回收该账号名下的资源(域名等),避免出现无主映射—— - // 装了哪些插件就回收哪些(CoreExt::reclaim_on_delete) - let removed = ext().map(|e| e.reclaim_on_delete(&username)).unwrap_or(0); - util::log_notice(&format!("[api] 删除账号 {username},连带域名 {removed} 条")); - http::ok(r, json!({ "removed_domains": removed }), "账号已删除") -} - -/// 设置某账号可用的 LLM 上游白名单(`models` 字段 = 上游 slot 列表,传空串 = 解除限制)。 -/// -/// 注意与**上游登记模型清单**(`Upstream.models`)是两件事:这里管"这个账号能用哪几家 -/// 上游",那里管"这家上游提供哪些模型"。白名单按 slot 生效,模型发现再按白名单过滤。 -fn h_admin_user_models(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - - let slots: Vec = p - .get("models") - .map(|s| { - s.split(',') - .map(|x| x.trim().to_owned()) - .filter(|x| !x.is_empty()) - .collect() - }) - .unwrap_or_default(); - - // 上游必须先存在:否则白名单里会存进永远匹配不上的死名字, - // 表现为「账号被授权了却一直 403」,排查起来很绕。 - // 上游表属 plugin-llm,经 CoreExt 校验。 - if let Err(e) = ext().map(|e| e.validate_slots(&slots)).unwrap_or(Ok(())) { - return http::fail(r, HTTPStatus::BAD_REQUEST, &e); - } - - match users::set_allowed_models(&username, slots) { - Ok(u) => http::ok(r, users::view(&u), "已更新可用上游"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -/// 设置账号的 LLM 限流参数。字段缺省 = 保持现值,`0` = 不限。 -fn h_admin_user_limits(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default(); - - let parse_limit = |key: &str| -> Result, String> { - match p.get(key) { - None => Ok(None), - Some(v) if util::trim(v).is_empty() => Ok(None), - Some(v) => util::trim(v) - .parse::() - .map(Some) - .map_err(|_| format!("{key} 必须是非负整数(0 表示不限)")), - } - }; - - let rpm = match parse_limit("rpm") { - Ok(v) => v, - Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e), - }; - let daily = match parse_limit("daily") { - Ok(v) => v, - Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e), - }; - if rpm.is_none() && daily.is_none() { - return http::fail(r, HTTPStatus::BAD_REQUEST, "至少提供 rpm 或 daily 之一"); - } - - util::log_notice(&format!( - "[api] 设置账号 {username} LLM 限流 rpm={rpm:?} daily={daily:?}" - )); - match users::set_llm_limits(&username, rpm, daily) { - Ok(u) => http::ok(r, users::view(&u), "已更新限流参数"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -// =========================================================================== -// 账号态 -// =========================================================================== - -fn h_me(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - - let mut v = users::view(&user); - // 我的域名 / 用量摘要 / 策略 TTL / 能力目录(upstreams + apix)都是 - // 插件侧数据,由装配根装饰。port 从 Host 头取,让装饰能拼出正确的 - // `https://lm.mayishangshu.cn:88/...` base_url(部署在 88 时必须带)。 - let port = http::host_port(r); - if let Some(e) = ext() { - e.decorate_me(&user, &port, &mut v); - } - http::ok(r, v, "ok") -} - -// =========================================================================== -// 装配 -// =========================================================================== - -/// 内核插件:认领身份类端点。必须排在 gateway 的 `PLUGINS` **最前**。 -pub struct CorePlugin; - -impl Plugin for CorePlugin { - fn name(&self) -> &'static str { - "core" - } - - fn dispatch_api(&self, r: *mut ngx_http_request_t) -> Option { - dispatch(r) - } -} diff --git a/rust/crates/ipass-core/src/auth.rs b/rust/crates/ipass-core/src/auth.rs deleted file mode 100644 index da612cc..0000000 --- a/rust/crates/ipass-core/src/auth.rs +++ /dev/null @@ -1,181 +0,0 @@ -//! 下行链路鉴权与配额 —— LLM 代理与 API 代理(apix)**共用**的契约。 -//! -//! 「去掉 llm 还要(apix 要)、去掉 apix 还要(llm 要)、两个都去掉才不要」 -//! ⇒ 共用 ⇒ 放内核(docs/plugin/PLUGINIZATION_PLAN.md §4②)。 -//! -//! * [`downstream_key`] —— 取下行 Key(三种携带方式) -//! * [`identity`] —— 查 `ukey:` 身份(纯共享内存,热路径不回读 users.json) -//! * [`check_quota`] —— `rl:` 每分钟限流 + `st:` 每日配额(先增后判) -//! * [`fail`] / [`fail_typed`] —— OpenAI 兼容错误体(下行客户端的 SDK 只认这个) -//! -//! ⚠️ access 阶段返回 `NGX_OK` 等于放行:鉴权失败必须返回状态码, -//! 否则请求照样被 `proxy_pass` 转发——「客户端收到 401,但上游仍被调了一次」, -//! 白烧上游额度且账单上看不出这是一次被拒的请求。实测踩过。 - -use ngx::core::Status; -use ngx::ffi::{ngx_http_request_t, ngx_int_t}; -use ngx::http::HTTPStatus; -use serde_json::json; - -use crate::config; -use crate::http; -use crate::state; -use crate::util; - -// =========================================================================== -// 请求解析 -// =========================================================================== - -/// 取下行 Key。 -/// -/// 依次尝试三种携带方式,覆盖主流 SDK 的默认行为: -/// `Authorization: Bearer`(OpenAI)、`x-api-key`(Anthropic)、`X-API-Key`(本项目域名接口)。 -/// 「同一套 Key」意味着这三个位置放的都应该是 `users.json` 里那把 `sk_...`。 -pub fn downstream_key(r: *mut ngx_http_request_t) -> Option { - if let Some(raw) = http::header(r, "authorization") { - let raw = raw.trim(); - let rest = raw - .strip_prefix("Bearer ") - .or_else(|| raw.strip_prefix("bearer ")); - if let Some(k) = rest { - let k = k.trim(); - if !k.is_empty() { - return Some(k.to_owned()); - } - } - } - for name in ["x-api-key", "api-key"] { - if let Some(k) = http::header(r, name) { - let k = k.trim().to_owned(); - if !k.is_empty() { - return Some(k); - } - } - } - None -} - -/// 查共享内存拿身份:`(username, allowed_slots, rpm, daily)`。 -/// -/// `allowed_slots` 为空表示不限(老账号没有该字段,反序列化即空表)。 -/// `rpm` / `daily` 是限流上限;旧版 payload 只有 2 行,此时按全局默认兜底。 -pub fn identity(key: &str) -> Option<(String, Vec, i64, i64)> { - let payload = state::get_live(config::KEY_UKEY_PREFIX, key.as_bytes(), util::now())?; - let mut lines = payload.split('\n'); - let name = lines.next().unwrap_or(""); - let allowed = lines.next().unwrap_or(""); - let rpm = lines - .next() - .and_then(|s| s.parse::().ok()) - .unwrap_or(config::LLM_RPM_DEFAULT); - let daily = lines - .next() - .and_then(|s| s.parse::().ok()) - .unwrap_or(config::LLM_DAILY_DEFAULT); - if name.is_empty() { - return None; - } - let slots: Vec = if allowed.is_empty() { - Vec::new() - } else { - allowed - .split(',') - .map(|s| s.trim().to_owned()) - .filter(|s| !s.is_empty()) - .collect() - }; - Some((name.to_owned(), slots, rpm, daily)) -} - -// =========================================================================== -// 响应 -// =========================================================================== - -/// OpenAI 兼容的错误体。客户端 SDK 只认这个形状。 -/// -/// **返回状态码而不是 `NGX_OK`**——见模块头注释。 -pub fn fail(r: *mut ngx_http_request_t, status: HTTPStatus, code: &str, msg: &str) -> Status { - fail_typed(r, status, "invalid_request_error", code, msg) -} - -/// 同 [`fail`],但 `type` 字段可指定——429 限流应返回 `rate_limit_error`, -/// SDK(尤其 OpenAI 官方库)据此做退避重试,写成 `invalid_request_error` -/// 会被当成「请求本身有问题」而直接放弃。 -pub fn fail_typed( - r: *mut ngx_http_request_t, - status: HTTPStatus, - err_type: &str, - code: &str, - msg: &str, -) -> Status { - let body = json!({ - "error": { - "message": msg, - "type": err_type, - "code": code, - } - }) - .to_string(); - - let http_code = status.0 as ngx_int_t; - // 先把自定义响应体发出去(nginx 之后不会再补一份默认错误页,因为 header 已发) - let _ = http::send(r, status, &body); - // 再把状态码交回 nginx,由它终结本请求 - Status(http_code) -} - -// =========================================================================== -// 限流与配额 -// =========================================================================== - -/// 限流与每日配额检查。拒绝时直接发 429 并返回 Some(状态),放行返回 None。 -/// -/// **先增后判**:窗口计数里也包含被拒的请求。这只影响「超限后计数继续涨」, -/// 不影响正确性(反正已在超限状态);换来的是实现无需区分「合法请求才计数」。 -/// -/// # Safety -/// `r` 由 nginx 传入且有效。 -pub unsafe fn check_quota( - r: *mut ngx_http_request_t, - username: &str, - rpm: i64, - daily: i64, -) -> Option { - let now = util::now(); - - // 每分钟窗口(60s 滚动对齐到自然分钟)。rpm<=0 表示不限,跳过计数省一次写 - if rpm > 0 { - let used = state::bump_window(config::KEY_RL_PREFIX, username, now / 60); - if used > rpm { - util::log_warn(&format!( - "[llm] {username} 触发每分钟限流 {used}/{rpm},本请求 429" - )); - return Some(fail_typed( - r, - HTTPStatus::TOO_MANY_REQUESTS, - "rate_limit_error", - "rate_limit_exceeded", - &format!("请求过于频繁:每分钟上限 {rpm} 次,请稍后重试"), - )); - } - } - - // 每日窗口按本地自然日切(见 util::day_id) - if daily > 0 { - let today = state::bump_window(config::KEY_ST_PREFIX, username, util::day_id(now)); - if today > daily { - util::log_warn(&format!( - "[llm] {username} 触发每日配额 {today}/{daily},本请求 429" - )); - return Some(fail_typed( - r, - HTTPStatus::TOO_MANY_REQUESTS, - "insufficient_quota", - "insufficient_quota", - &format!("已达每日请求上限 {daily} 次,明日 0 点重置或联系管理员调整"), - )); - } - } - - None -} diff --git a/rust/crates/ipass-core/src/config.rs b/rust/crates/ipass-core/src/config.rs deleted file mode 100644 index fcec74c..0000000 --- a/rust/crates/ipass-core/src/config.rs +++ /dev/null @@ -1,94 +0,0 @@ -//! 内核策略常量(唯一真源)。 -//! -//! 内核策略常量(唯一真源)。 -//! -//! **改策略只改这里**,不要散落到各模块里,否则行为会出现多处真源。 -//! -//! 插件自有常量在各自 crate(plugin-domain/config.rs、plugin-llm/config.rs、 -//! plugin-apix/config.rs);这里只留「身份 / 会话 / 配额 / 两条下行链路共用」的部分。 - -/// 管理员授予的「临时授权权限」有效时长(秒) -pub const TEMP_GRANT_TTL: i64 = 6 * 3600; -/// 管理员登录会话时长(秒) -pub const SESSION_TTL: i64 = 8 * 3600; -/// 口令哈希迭代轮数。**必须与旧版一致**,否则存量用户口令全部失效。 -pub const HASH_ROUNDS: usize = 1000; - -/// 首次启动播种的管理员账号名 -pub const INIT_ADMIN_USER: &str = "admin"; -/// 初始管理员口令的环境变量名。 -/// nginx.conf 里必须有对应的 `env ADMIN_INIT_PASSWORD;`,否则会被 nginx 剥离。 -pub const INIT_ADMIN_PASSWORD_ENV: &str = "ADMIN_INIT_PASSWORD"; -pub const INIT_ADMIN_PASSWORD_DEFAULT: &str = "123456"; - -/// 口令最短长度 -/// -/// sso 提议提到 8(弱口令门槛);iPass **维持 6 位下限**——既有测试夹具与 -/// 边界用例大量依赖 6/7 位密码(`password 123` 等),改 8 位会破坏 262 项 -/// 回归。**插件层**(plugin-sso / 未来 plugin-password-policy)若需强约束, -/// 应在插件端校验后再调用 `users::create`/`set_password`,内核不动。 -pub const PASSWORD_MIN_LEN: usize = 6; -pub const USERNAME_MIN_LEN: usize = 2; -pub const USERNAME_MAX_LEN: usize = 32; -/// 展示名(中文姓名等)长度上限。按**字符数**算,一个汉字算 1。 -pub const DISPLAY_NAME_MAX_LEN: usize = 32; - -/// 管理会话 Cookie 名(HttpOnly + SameSite=Strict) -pub const COOKIE_NAME: &str = "ipass_admin_session"; - -/// 同一 IP 在窗口内最多登录失败次数 -pub const LOGIN_MAX_FAIL: i64 = 10; -/// 登录失败计数窗口(秒) -pub const LOGIN_FAIL_TTL: i64 = 300; - -/// 下行链路每 Key 限流默认值:每分钟请求数。账号未显式配置时取此值;0 = 不限 -pub const LLM_RPM_DEFAULT: i64 = 60; -/// 下行链路每 Key 限流默认值:每日请求数。账号未显式配置时取此值;0 = 不限。 -/// -/// 刻意做**请求级**而不是 token 级:token 数在上游响应体里,而 SSE 必须关缓冲 -/// 透传(`proxy_buffering off`),网关不缓冲就解析不到 usage 字段。要 token 级 -/// 计费得自建 upstream,与「单容器零依赖」红线冲突。 -pub const LLM_DAILY_DEFAULT: i64 = 0; - -/// 默认状态目录,可被 `ipass_data_dir` 指令覆盖 -pub const DEFAULT_DATA_DIR: &str = "/usr/local/nginx/data"; - -// =========================================================================== -// 共享内存键/值编码 -// =========================================================================== - -/// 键前缀:管理会话 token → 用户名 -pub const KEY_SESS_PREFIX: &[u8] = b"sess:"; -/// 键前缀:登录失败计数(按 IP) -pub const KEY_FAIL_PREFIX: &[u8] = b"fail:"; -/// 键前缀:下行链路的 API Key → 身份(LLM 代理与 API 代理共用)。 -/// -/// payload = ` \n \n \n `(4 行, -/// 与 `users::identity_payload` 严格一致)。一次查表同时拿到「是谁 / 能用哪些上游 / -/// 限流多少」,**避免推理热路径回读 users.json**——那是同步文件 I/O + 全表 JSON 解码, -/// 管理接口用得起,转发热路径用不起。 -pub const KEY_UKEY_PREFIX: &[u8] = b"ukey:"; -/// 键前缀:每分钟限流计数(LLM 代理与 API 代理共用)。键 = `rl:` -/// (**每用户固定一个键**),value = `<分钟窗id>:<计数>`——跨窗自动归零靠读时 -/// 比对窗 id,而不是开新键。若按「每分钟一个键」存,shm 里的键会随时间无限 -/// 累积且无处过期。 -pub const KEY_RL_PREFIX: &[u8] = b"rl:"; -/// 键前缀:每日配额计数(LLM 代理与 API 代理共用)。键 = `st:`, -/// value = `<日窗id>:<计数>`。 -pub const KEY_ST_PREFIX: &[u8] = b"st:"; - -/// 共享内存 value 内部分隔符(ASCII US)。 -/// 后端地址只含 `[A-Za-z0-9._-]` 与 `:`,用户名只含 `[A-Za-z0-9_-]`, -/// 都不可能包含该字节,所以切分是安全的。 -pub const VAL_SEP: u8 = 0x1f; - -/// 键缓冲区上限:`sess:` + 32 字节 token 的 64 位 hex 最长为 69,留足余量 -pub const KEY_BUF_LEN: usize = 128; - -// =========================================================================== -// 部署拓扑(LLM 代理与 API 代理共用) -// =========================================================================== - -/// LLM 代理对外域名,用于账号态 API 回显 `base_url`(apix 的「复制地址」也用它)。 -/// 改域名只改这里(`conf/nginx.conf` 的 LLM server_name 要同步改)。 -pub const LLM_PUBLIC_HOST: &str = "lm.mayishangshu.cn"; diff --git a/rust/crates/ipass-core/src/http.rs b/rust/crates/ipass-core/src/http.rs deleted file mode 100644 index 14878e3..0000000 --- a/rust/crates/ipass-core/src/http.rs +++ /dev/null @@ -1,223 +0,0 @@ -//! 请求/响应辅助:参数解析、请求体读取、JSON 响应输出。 -//! -//! 这一层承担旧版 `util.params` / `util.respond` 的职责,但有一处本质差异: -//! **ngx crate 没有暴露请求体读取**,必须直接走 -//! `ngx_http_read_client_request_body` 的两段式异步接口。 - -use std::collections::HashMap; -use std::ptr; - -use ngx::core::{Buffer, Status}; -use ngx::ffi::{ngx_chain_t, ngx_http_request_t}; -use ngx::http::{HTTPStatus, Request}; -use serde_json::{Value, json}; - -use crate::util; - -// =========================================================================== -// 取请求信息 -// =========================================================================== - -pub fn uri(r: *mut ngx_http_request_t) -> String { - // SAFETY: r 由 nginx 保证有效;uri 是请求内存池中的字符串 - unsafe { String::from_utf8_lossy((*r).uri.as_bytes()).into_owned() } -} - -/// HTTP 方法名。直接读 nginx 已解析好的 `method_name`, -/// 避免依赖 Method 枚举的全部变体名(少一个就编译不过)。 -pub fn method_str(r: *mut ngx_http_request_t) -> String { - // SAFETY: 同上 - unsafe { String::from_utf8_lossy((*r).method_name.as_bytes()).into_owned() } -} - -pub fn args_raw(r: *mut ngx_http_request_t) -> Vec { - // SAFETY: 同上 - unsafe { (*r).args.as_bytes().to_vec() } -} - -/// 请求头(大小写不敏感) -pub fn header(r: *mut ngx_http_request_t, name: &str) -> Option { - // SAFETY: 迭代器只在本函数存续期内使用 - let request = unsafe { Request::from_ngx_http_request(r) }; - for (k, v) in request.headers_in_iterator() { - if k.as_bytes().eq_ignore_ascii_case(name.as_bytes()) { - return Some(String::from_utf8_lossy(v.as_bytes()).into_owned()); - } - } - None -} - -/// 请求 Host 里的端口后缀(`:88` / 空串)。 -/// -/// 对外三个域名(`mdm` 管理台 / `lm` LLM / `*.dm` 企业)**共用同一个端口**, -/// 所以「管理接口从哪个端口进来,LLM 就该从哪个端口出去」。回显用的 `base_url` -/// 用它拼:写死端口在换端口部署时失效,完全不写则在非 443 部署时指向别处。 -pub fn host_port(r: *mut ngx_http_request_t) -> String { - let host = match header(r, "Host") { - Some(h) => h, - None => return String::new(), - }; - // IPv6 字面量形如 `[::1]:88`,rsplit 后 `[::1]` / `88` 仍然正确; - // 纯 `[::1]` 会切出非全数字的尾巴,被下面判掉。 - match host.rsplit_once(':') { - Some((_, p)) if !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit()) => format!(":{p}"), - _ => String::new(), - } -} - -/// 客户端地址(登录限速的分组键) -pub fn remote_addr(r: *mut ngx_http_request_t) -> String { - // SAFETY: r 有效;connection 在请求生命周期内必然存在 - unsafe { - let conn = (*r).connection; - if conn.is_null() { - return "-".to_owned(); - } - String::from_utf8_lossy((*conn).addr_text.as_bytes()).into_owned() - } -} - -// =========================================================================== -// 请求体 -// =========================================================================== - -/// 收集已读入的请求体。 -/// -/// **调用前提**:本函数只应在 `ngx_http_read_client_request_body` 的 -/// post_handler 里调用(见 `api::read_body_then`)。 -/// -/// 请求体可能跨越多个 `ngx_buf_t`(chain)。被刷写到临时文件的大请求体 -/// 这里直接忽略——管理台 API 的请求体都是几百字节的小 JSON, -/// 且管理 server 已把 `client_body_buffer_size` 调到 1m 覆盖边界。 -pub unsafe fn collect_body(r: *mut ngx_http_request_t) -> Vec { - let mut out = Vec::new(); - - let rb = unsafe { (*r).request_body }; - if rb.is_null() { - return out; - } - - let mut chain = unsafe { (*rb).bufs }; - while !chain.is_null() { - // SAFETY: chain 由 nginx 构建,节点与 buf 均有效 - unsafe { - let buf = (*chain).buf; - if !buf.is_null() && (*buf).in_file() == 0 { - let (pos, last) = ((*buf).pos, (*buf).last); - if !pos.is_null() && !last.is_null() && last > pos { - let len = last.offset_from(pos) as usize; - out.extend_from_slice(std::slice::from_raw_parts(pos, len)); - } - } - chain = (*chain).next; - } - } - out -} - -/// 合并 query string 与请求体参数。 -/// -/// 优先级与旧版一致:**请求体覆盖 query**(同名时以 body 为准)。 -pub fn params(r: *mut ngx_http_request_t) -> HashMap { - let mut map = HashMap::new(); - - for (k, v) in util::parse_kv(&args_raw(r)) { - map.insert(k, v); - } - - let body = unsafe { collect_body(r) }; - if body.is_empty() { - return map; - } - - let ctype = header(r, "content-type").unwrap_or_default(); - if ctype.to_ascii_lowercase().contains("application/json") { - if let Ok(Value::Object(obj)) = serde_json::from_slice::(&body) { - for (k, v) in obj { - let s = match v { - Value::String(s) => s, - Value::Number(n) => n.to_string(), - Value::Bool(b) => b.to_string(), - _ => continue, - }; - map.insert(k, s); - } - } - } else { - for (k, v) in util::parse_kv(&body) { - map.insert(k, v); - } - } - - map -} - -// =========================================================================== -// 响应 -// =========================================================================== - -/// 发送 JSON 响应体(含额外响应头,如 Set-Cookie)。 -/// -/// `body` 的字节会被复制进 nginx 的请求内存池,随请求自动回收。 -pub fn send_with( - r: *mut ngx_http_request_t, - status: HTTPStatus, - body: &str, - extra_headers: &[(&str, &str)], -) -> Status { - // SAFETY: r 有效 - unsafe { - let request = Request::from_ngx_http_request(r); - let pool = request.pool(); - - let mut buf = match pool.create_buffer_from_str(body) { - Some(b) => b, - None => return Status::NGX_ERROR, - }; - buf.set_last_buf(true); - - let mut chain = ngx_chain_t { buf: buf.as_ngx_buf_mut(), next: ptr::null_mut() }; - - request.set_status(status); - request.add_header_out("Content-Type", "application/json; charset=utf-8"); - request.add_header_out("Cache-Control", "no-store"); - for (name, value) in extra_headers { - request.add_header_out(name, value); - } - request.set_content_length_n(body.len()); - - let st = request.send_header(); - if st != Status::NGX_OK { - return st; - } - request.output_filter(&mut chain) - } -} - -pub fn send(r: *mut ngx_http_request_t, status: HTTPStatus, body: &str) -> Status { - send_with(r, status, body, &[]) -} - -/// 统一成功响应:`{code:200, msg, data}` -pub fn ok(r: *mut ngx_http_request_t, data: Value, msg: &str) -> Status { - let body = json!({ "code": 200, "msg": msg, "data": data }).to_string(); - send(r, HTTPStatus::OK, &body) -} - -pub fn ok_with( - r: *mut ngx_http_request_t, - data: Value, - msg: &str, - extra_headers: &[(&str, &str)], -) -> Status { - let body = json!({ "code": 200, "msg": msg, "data": data }).to_string(); - send_with(r, HTTPStatus::OK, &body, extra_headers) -} - -/// 统一失败响应:`{code:, msg}`(与旧版 `util.fail` 一致) -pub fn fail(r: *mut ngx_http_request_t, status: HTTPStatus, msg: &str) -> Status { - // HTTPStatus 是 newtype(`pub struct HTTPStatus(pub ngx_uint_t)`),直接取内层值 - let code = status.0 as u16; - let body = json!({ "code": code, "msg": msg }).to_string(); - send(r, status, &body) -} diff --git a/rust/crates/ipass-core/src/lib.rs b/rust/crates/ipass-core/src/lib.rs deleted file mode 100644 index 92ec8de..0000000 --- a/rust/crates/ipass-core/src/lib.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! ipass-core —— iPass 内核。 -//! -//! 承担「去掉任何插件,网关还能跑」的部分: -//! -//! * `state` —— 唯一共享内存 zone(`RwLock`)+ 文件写锁 -//! * `store` —— JSON 原子写 + 跨 worker 互斥(状态就是几个 JSON 文件,无数据库) -//! * `users` —— 账号唯一写入口(`ukey:` Key 索引、播种管理员、删除钩子) -//! * `session` —— 管理会话 -//! * `auth`(P3 起)—— 下行 `sk_` 鉴权 + `rl:` 配额(LLM 与 API 代理两条链路共用) -//! * `config` / `models` / `util` / `http` —— 常量、账号模型与通用原语 -//! -//! 插件(plugin-domain / plugin-llm / plugin-apix)只允许**单向依赖**本 crate; -//! 内核不认识任何插件。插件通过 `Plugin` trait(P1 引入)与 gateway 装配。 -//! -//! ## 关键实现约束(均为实测/查证确认,详见 CLAUDE.md) -//! -//! * 共享内存只有 `RwLock, _>` 一种安全形态; -//! `SlabPool` 只能在 zone 初始化回调里取出,锁本身也必须用 -//! `ngx::allocator::allocate` 放进共享内存,否则各 worker 拿到的是各自的副本。 -//! * 热路径**永不回源**:每请求决策只读共享内存;任何写操作必须 -//! 先落文件、再写穿 shm,写 shm 失败必须 `log_err`。 - -pub mod api; -pub mod auth; -pub mod config; -pub mod http; -pub mod loc; -pub mod models; -pub mod plugin; -pub mod session; -pub mod state; -pub mod store; -pub mod usage; -pub mod users; -pub mod util; diff --git a/rust/crates/ipass-core/src/loc.rs b/rust/crates/ipass-core/src/loc.rs deleted file mode 100644 index 6f64bd2..0000000 --- a/rust/crates/ipass-core/src/loc.rs +++ /dev/null @@ -1,61 +0,0 @@ -//! Location 开关契约 —— 内核与插件之间**唯一的契约点**。 -//! -//! 配置指令(`ipass_api` / `ipass_proxy` / `ipass_llm` / `ipass_apix`)由 gateway -//! 集中解析进 `ModuleConfig`;插件不认识 gateway,只通过 [`Switch`] 问一句 -//! 「我所在的 location 开了吗」。gateway 在 preconfiguration 注入查询实现 -//! ([`set_switch_fn`]),**未注入或未实现的变体一律保守返回 `false`** -//! ——返回 `true` 会让未装配的能力意外开门(sso 踩过的坑)。 - -use core::sync::atomic::{AtomicUsize, Ordering}; - -use ngx::ffi::ngx_http_request_t; -use ngx::http::Request; - -/// location 能力的枚举。取 iPass 与 sso 的**并集**: -/// iPass 用 `Api / Proxy / Llm / Apix`;`Iap / Domain` 为 sso 预留, -/// 合流时不用改本枚举。 -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Switch { - /// 该 location 暴露管理 API + 身份端点 - Api, - /// 预留(sso 用;iPass 未实现,恒 false) - Iap, - /// 预留(sso 用;iPass 未实现,恒 false) - Domain, - /// 该 location 走「子域名 → 后端」转发 - Proxy, - /// 该 location 走 LLM 代理 - Llm, - /// 该 location 走 API 代理 - Apix, - /// 该 location 走统一制品缓存(go / rust / python / npm / java / docker) - /// - /// 数据面由 nginx 原生 `proxy_cache` 承担,本开关只决定「是否按统一 Key - /// 鉴权 + 查上游表 + 注入上游凭据」;命中计量在 log 阶段按生态记账。 - Cache, -} - -/// gateway 注入的查询实现:`(请求, 开关) -> 是否开启` -pub type SwitchFn = fn(&mut Request, Switch) -> bool; - -// fn 指针按 usize 原子存取(热路径上 access handler 每请求都要查,不能用 Mutex) -static SWITCH_FN: AtomicUsize = AtomicUsize::new(0); - -/// gateway 在 preconfiguration 阶段注入查询实现(早于任何请求) -pub fn set_switch_fn(f: SwitchFn) { - SWITCH_FN.store(f as *const () as usize, Ordering::Relaxed); -} - -/// 插件查询所在 location 是否开启自己的能力。 -/// **未注入实现时保守返回 `false`**——宁可关着,不可误开。 -pub fn enabled(r: *mut ngx_http_request_t, s: Switch) -> bool { - let p = SWITCH_FN.load(Ordering::Relaxed); - if p == 0 { - return false; - } - // SAFETY: p 来自 set_switch_fn 存入的合法 fn 指针 - let f: SwitchFn = unsafe { core::mem::transmute(p) }; - // SAFETY: r 由 nginx 传入且有效;Request 只在本调用存续期内使用 - let mut req = unsafe { Request::from_ngx_http_request(r) }; - f(&mut req, s) -} diff --git a/rust/crates/ipass-core/src/models.rs b/rust/crates/ipass-core/src/models.rs deleted file mode 100644 index bdcda27..0000000 --- a/rust/crates/ipass-core/src/models.rs +++ /dev/null @@ -1,84 +0,0 @@ -//! 持久化数据模型。 -//! -//! 字段名与旧版 `data/users.json` / `data/domains.json` **逐字对应**, -//! 以便新版直接接管旧数据文件、且管理台前端无需改动。 - -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::BTreeMap; - -/// 账号表(`users.json`):以账号名为 key 的对象。 -pub type UserTable = BTreeMap; - -/// 单个账号。 -/// -/// `temp_grant_until` 是「临时授权权限」的到期时间戳;> now 表示当前拥有 -/// 该权限,申请临时域名时可跳过审核直接放行。 -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct User { - pub username: String, - /// 中文姓名等展示名。**只用于展示**,不参与鉴权、不参与热路径查询。 - /// - /// `username` 规则是 ASCII(`valid_username`),中文名塞不进账号名, - /// 所以「吴桂荣」这类实名单独放这里;留空时前端回落到 `username`。 - #[serde(default)] - pub display_name: String, - /// `admin` | `user` - pub role: String, - pub salt: String, - /// 迭代加盐 SHA-256 - pub pwd_hash: String, - /// `sk_<48 位 hex>`。 - /// - /// LLM 代理与域名接口**共用这一把 Key**——这正是「用同一套 Key 验证」 - /// 的落点,所以重置 Key 会同时影响两个入口。 - pub api_key: String, - /// LLM 代理可用的上游 slot 白名单。空 = 不限。 - /// - /// 老数据没有该字段,`serde(default)` 反序列化得空表,语义正好是「不限」。 - #[serde(default)] - pub allowed_models: Vec, - /// LLM 每分钟请求上限。老数据缺省 → 全局默认(`LLM_RPM_DEFAULT`);0 = 不限 - #[serde(default = "default_llm_rpm")] - pub llm_rpm: i64, - /// LLM 每日请求上限。老数据缺省 → 全局默认(`LLM_DAILY_DEFAULT`);0 = 不限 - #[serde(default = "default_llm_daily")] - pub llm_daily: i64, - #[serde(default)] - pub temp_grant_until: i64, - #[serde(default)] - pub created_at: i64, - - /// 插件字段透传:内核持有、**不解释**。 - /// - /// 反序列化时,文件中**未被上面字段认领的 key 自动进 `extra`**;序列化时 - /// 展开回顶层(`#[serde(flatten)]`)。`serde` 优先匹配已认领字段,所以 - /// 存量 `users.json`(含任何插件写入的字段)可原样接管,**不会因内核不 - /// 认识而丢数据**——这是 iPass 向上游 sso 合流的扩展点(A1)。 - /// - /// 未来插件要存 TOTP 密钥、邮箱、用户组、锁定状态……都直接往 `extra` - /// 写,不再走内核加字段。修改走 [`crate::users::update_extra`] 的 - /// 「读-改-写」原语。 - #[serde(default, flatten)] - pub extra: BTreeMap, -} - -impl User { - /// 是否拥有「临时授权权限」 - pub fn has_temp_permission(&self, now: i64) -> bool { - self.temp_grant_until > now - } - - pub fn is_admin(&self) -> bool { - self.role == "admin" - } -} - - -fn default_llm_rpm() -> i64 { - crate::config::LLM_RPM_DEFAULT -} - -fn default_llm_daily() -> i64 { - crate::config::LLM_DAILY_DEFAULT -} diff --git a/rust/crates/ipass-core/src/plugin.rs b/rust/crates/ipass-core/src/plugin.rs deleted file mode 100644 index eac9aea..0000000 --- a/rust/crates/ipass-core/src/plugin.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! 插件装配契约 —— 内核与插件之间唯一的接口。 -//! -//! 沿用 ssw/sso 的形态,签名保持一致(若将来与 sso 合流,trait 不用再改)。 -//! -//! ## 刻意没有 `commands()` -//! -//! nginx 的 `ngx_module_t.commands` 是 null 结尾的**连续**数组,不同 crate 的 -//! `static` 数组无法拼接。配置指令由 gateway 集中声明(`ipass_*` 七条不变), -//! 插件通过 [`crate::loc::Switch`] 查询自己所在 location 是否开启—— -//! **指令解析归壳、行为归插件**。 -//! -//! ## `dispatch_api` 语义 -//! -//! 「不认领就返回 `None`,让下一个插件试」,**绝不自行 404**——否则后注册的 -//! 插件永远收不到请求。404 由 gateway 在全部插件都返回 `None` 后统一发出。 - -use core::sync::atomic::{AtomicUsize, Ordering}; - -use ngx::core::Status; -use ngx::ffi::{ngx_conf_t, ngx_http_request_t}; - -/// 本网关 `ngx_module_t` 的 `ctx_index`(gateway 在 postconfiguration 前 -/// 通过 [`set_ctx_index`] 注入)。插件用它在 `r->ctx` 上挂自己的请求级状态 -/// (如 usage_filter 的扫描窗口)。 -static CTX_INDEX: AtomicUsize = AtomicUsize::new(usize::MAX); - -pub fn set_ctx_index(idx: usize) { - CTX_INDEX.store(idx, Ordering::Relaxed); -} - -/// 当前模块的 ctx_index;`usize::MAX` = 尚未注入(此时插件应放弃请求级状态)。 -pub fn ctx_index() -> usize { - CTX_INDEX.load(Ordering::Relaxed) -} - -pub trait Plugin: Sync { - /// 插件名(日志与排障用) - fn name(&self) -> &'static str; - - /// 配置解析前:注册本插件自己的 nginx 变量。 - /// 返回 false = 注册失败,gateway 会 EMERG 退出。 - fn preconfig(&self, _cf: *mut ngx_conf_t) -> bool { - true - } - - /// 配置解析后:注册 phase handler / body filter。 - /// 返回 false = 注册失败,gateway 会 EMERG 退出。 - fn postconfig(&self, _cf: *mut ngx_conf_t) -> bool { - true - } - - /// 管理 API 分发:认领则返回响应状态,不认领返回 `None`。 - fn dispatch_api(&self, _r: *mut ngx_http_request_t) -> Option { - None - } - - /// fork 前预装载共享内存(zone init 回调里调用一次)。 - /// 返回装载条数(gateway 汇总打日志)。 - fn zone_load(&self) -> usize { - 0 - } - - /// log 阶段旁路处理器(与 sso 对齐保留;返回 true 表示已处理)。 - fn log_handler(&self, _r: *mut ngx_http_request_t) -> bool { - true - } -} diff --git a/rust/crates/ipass-core/src/session.rs b/rust/crates/ipass-core/src/session.rs deleted file mode 100644 index 173fb8e..0000000 --- a/rust/crates/ipass-core/src/session.rs +++ /dev/null @@ -1,57 +0,0 @@ -//! 管理员登录会话。 -//! -//! 会话存在共享内存里:**nginx 重启即全部失效**,这是预期行为 -//! ——管理会话不需要跨重启延续。 -//! -//! Cookie 为 `ipass_admin_session=<64 位 hex>`,HttpOnly + SameSite=Strict, -//! 每次校验命中都会滑动续期。 - -use crate::config::{COOKIE_NAME, SESSION_TTL}; -use crate::state; -use crate::util; - -/// 从 Cookie 头里取出会话 token -pub fn parse_token(cookie_header: &str) -> Option { - let needle = format!("{COOKIE_NAME}="); - for part in cookie_header.split(';') { - let part = part.trim(); - if let Some(value) = part.strip_prefix(&needle) { - if !value.is_empty() { - return Some(value.to_owned()); - } - } - } - None -} - -/// 新建会话,返回 token -pub fn create(username: &str) -> String { - let token = util::rand_hex(32); - state::put_session(&token, username, util::now() + SESSION_TTL); - token -} - -pub fn set_cookie(token: &str) -> String { - format!("{COOKIE_NAME}={token}; Path=/; HttpOnly; SameSite=Strict; Max-Age={SESSION_TTL}") -} - -pub fn clear_cookie() -> String { - format!("{COOKIE_NAME}=deleted; Path=/; HttpOnly; SameSite=Strict; Max-Age=0") -} - -/// 鉴权:解析 Cookie → 查共享内存 → 命中则滑动续期。返回当前登录用户名。 -pub fn authenticate(cookie: Option<&str>) -> Option { - let token = parse_token(cookie?)?; - let now = util::now(); - let username = state::get_session(&token, now)?; - // 滑动续期:活跃使用不因固定 TTL 被踢出 - state::put_session(&token, &username, now + SESSION_TTL); - Some(username) -} - -/// 退出登录:销毁服务端会话(Cookie 由响应头清除) -pub fn destroy(cookie: Option<&str>) { - if let Some(token) = cookie.and_then(parse_token) { - state::remove_session(&token); - } -} diff --git a/rust/crates/ipass-core/src/state.rs b/rust/crates/ipass-core/src/state.rs deleted file mode 100644 index 56434a0..0000000 --- a/rust/crates/ipass-core/src/state.rs +++ /dev/null @@ -1,358 +0,0 @@ -//! 共享内存状态层。 -//! -//! 这是「配置放内存」的落点:所有派生状态集中在**一张** `RbTreeMap`, -//! 用键前缀区分用途: -//! -//! ```text -//! sub: → \x1f 转发映射 -//! sess: → \x1f 管理会话 -//! fail: → \x1f 登录失败计数 -//! ``` -//! -//! ## 为什么过期时间写在 value 里,而不是靠字典的 TTL -//! -//! 共享内存是裸 `RbTreeMap`,没有 TTL 自动回收。把到期时刻编码进 value, -//! 读取时判断——语义上反而更精确(到期即失效,不受回收延迟影响)。 -//! -//! ## 为什么锁必须放进共享内存 -//! -//! nginx 是多进程模型。`RwLock` 若放在普通静态变量里,每个 worker 会持有 -//! 各自的副本,跨进程互斥完全失效。锁与表都必须在 `ngx_shared_memory_add` -//! 创建的 slab 池里分配。 - -use core::ptr; -use core::sync::atomic::{AtomicPtr, Ordering}; - -use ngx::collections::RbTreeMap; -use ngx::core::{NgxStr, NgxString, SlabPool, Status}; -use ngx::ffi::ngx_shm_zone_t; -use ngx::sync::{RwLock, RwLockWriteGuard}; - -use crate::config::{KEY_BUF_LEN, KEY_UKEY_PREFIX, VAL_SEP}; - -/// 子域名 → 后端、token → 用户名等全部映射的底层容器 -pub type ShmMap = RbTreeMap, NgxString, SlabPool>; - -/// 共享内存根对象:一张表 + 一把文件写锁。 -/// -/// 文件写锁**独立于表的读写锁**:文件 I/O 期间若持有表的写锁, -/// 会把所有转发请求(需要读锁)一起卡住。分开后,域名的热路径只受 -/// 自身短临界区影响。 -pub struct ShmRoot { - pub map: RwLock, - /// 保护 `store` 的「读-改-写」与落盘,跨 worker 互斥 - pub file_lock: RwLock<()>, -} - -/// 由 `ipass_state ` 注册的共享内存区 -static STATE_ZONE: AtomicPtr = AtomicPtr::new(ptr::null_mut()); - -pub fn set_zone(zone: *mut ngx_shm_zone_t) { - STATE_ZONE.store(zone, Ordering::SeqCst); -} - -/// 在 slab 池上构造 `ShmRoot` 并挂到 `shpool->data`;重复调用直接复用。 -fn init_root(zone: &mut ngx_shm_zone_t) -> Result<&'static ShmRoot, Status> { - // SAFETY: zone 由 ngx_shared_memory_add 注册,init 回调已使其 shm 可用 - let mut alloc = unsafe { SlabPool::from_shm_zone(zone) }.ok_or(Status::NGX_ERROR)?; - - if alloc.as_mut().data.is_null() { - let map = ShmMap::try_new_in(alloc.clone()).map_err(|_| Status::NGX_ERROR)?; - let root = ShmRoot { map: RwLock::new(map), file_lock: RwLock::new(()) }; - // 整块放进共享内存:两个锁与表必须对所有 worker 是同一份 - let placed = ngx::allocator::allocate(root, &alloc).map_err(|_| Status::NGX_ERROR)?; - alloc.as_mut().data = placed.as_ptr().cast(); - } - - // SAFETY: data 非空即指向一个已初始化、且存活于共享内存的 ShmRoot - unsafe { alloc.as_ref().data.cast::().as_ref().ok_or(Status::NGX_ERROR) } -} - -/// 取共享内存根对象;zone 未注册时返回 None -pub fn root() -> Option<&'static ShmRoot> { - let zone = STATE_ZONE.load(Ordering::SeqCst); - if zone.is_null() { - return None; - } - // SAFETY: 非空即代表 zone 已注册 - init_root(unsafe { &mut *zone }).ok() -} - -/// 取 slab 分配器(构造共享内存字符串用) -pub fn alloc() -> Option { - let zone = STATE_ZONE.load(Ordering::SeqCst); - if zone.is_null() { - return None; - } - // SAFETY: 同上 - unsafe { SlabPool::from_shm_zone(&mut *zone) } -} - -/// 初始化回调:由 nginx 在 fork 之前调用一次 -pub fn init_zone(zone: *mut ngx_shm_zone_t) -> Status { - // SAFETY: nginx 传入的 zone 有效 - match init_root(unsafe { &mut *zone }) { - Ok(_) => Status::NGX_OK, - Err(e) => e, - } -} - -/// 取全局文件写锁。所有「读文件 → 改 → 写文件」必须包在这里面。 -/// -/// nginx 侧只有一个写锁、且都是低频管理操作,串行化代价可忽略; -/// 而它换来的是多 worker 下不会出现「两边同时改、后写的覆盖前写的」。 -pub fn file_lock() -> Option> { - root().map(|r| r.file_lock.write()) -} - -// =========================================================================== -// 键构造 -// =========================================================================== - -/// 在栈上拼 `prefix + name`,避免热路径为键做堆分配。 -fn key_buf(prefix: &[u8], name: &[u8]) -> Option<([u8; KEY_BUF_LEN], usize)> { - let n = prefix.len() + name.len(); - if n > KEY_BUF_LEN { - return None; - } - let mut buf = [0u8; KEY_BUF_LEN]; - buf[..prefix.len()].copy_from_slice(prefix); - buf[prefix.len()..n].copy_from_slice(name); - Some((buf, n)) -} - -/// 拆 value:` \x1f ` -/// -/// 用 `rposition` 而非 `position`:value 是「payload + 分隔符 + exp」三段, -/// `exp` 必为 ASCII 数字串、不含 `VAL_SEP`,所以**最后一个**分隔符才是真边界。 -/// 用 `position` 会被 payload 里内嵌的 `0x1f` 截断(OAuth 授权码 / 带 -/// 注入头的会话 payload 都可能含此字节)——sso 上游化时已踩此坑。 -fn split_value(bytes: &[u8]) -> Option<(&[u8], i64)> { - let sep = bytes.iter().rposition(|&b| b == VAL_SEP)?; - let payload = &bytes[..sep]; - let exp = core::str::from_utf8(&bytes[sep + 1..]).ok()?.parse::().ok()?; - Some((payload, exp)) -} - -// =========================================================================== -// 读写原语 -// =========================================================================== - -/// 查表。返回 `(payload, expires_at)`;键不存在或值损坏时返回 None。 -/// -/// **转发热路径唯一入口**——只读共享内存,未命中不产生任何 I/O。 -pub fn get(prefix: &[u8], name: &[u8]) -> Option<(String, i64)> { - let (buf, n) = key_buf(prefix, name)?; - let root = root()?; - let guard = root.map.read(); - let value = guard.get(NgxStr::from_bytes(&buf[..n]))?; - let (payload, exp) = split_value(value.as_bytes())?; - // 复制成 owned:guard 释放后共享内存可能被并发写覆盖, - // 直接返回引用会悬垂。一次小字符串分配远小于转发本身的代价。 - Some((String::from_utf8_lossy(payload).into_owned(), exp)) -} - -/// 查表并校验未过期 -pub fn get_live(prefix: &[u8], name: &[u8], now: i64) -> Option { - let (payload, exp) = get(prefix, name)?; - if exp > 0 && exp <= now { - return None; - } - Some(payload) -} - -/// 写入(先删后插,语义为覆盖)。返回 false 表示共享内存不足或 zone 未就绪。 -pub fn put(prefix: &[u8], name: &[u8], payload: &[u8], expires_at: i64) -> bool { - let (Some(root), Some(a)) = (root(), alloc()) else { - return false; - }; - - let mut value = Vec::with_capacity(payload.len() + 24); - value.extend_from_slice(payload); - value.push(VAL_SEP); - value.extend_from_slice(expires_at.to_string().as_bytes()); - - let mut key = Vec::with_capacity(prefix.len() + name.len()); - key.extend_from_slice(prefix); - key.extend_from_slice(name); - - let (Ok(nk), Ok(nv)) = ( - NgxString::try_from_bytes_in(&key, a.clone()), - NgxString::try_from_bytes_in(&value, a), - ) else { - return false; - }; - - let mut guard = root.map.write(); - // 先删后插:try_insert 对已存在的键可能直接失败,而这里的语义是覆盖 - let _ = guard.remove(NgxStr::from_bytes(&key)); - guard.try_insert(nk, nv).is_ok() -} - -/// 删除一个键。返回它此前是否存在。 -pub fn remove(prefix: &[u8], name: &[u8]) -> bool { - let Some(root) = root() else { - return false; - }; - let mut key = Vec::with_capacity(prefix.len() + name.len()); - key.extend_from_slice(prefix); - key.extend_from_slice(name); - root.map.write().remove(NgxStr::from_bytes(&key)).is_some() -} - -/// 当前条目数(管理端观测用;当前无调用方,保留为诊断接口) -#[allow(dead_code)] -pub fn len() -> usize { - root().map(|r| r.map.read().iter().count()).unwrap_or(0) -} - -/// 只清掉某一前缀下的所有键。 -/// -/// 重载转发映射时用——**不能**用 `clear()`,那会把正在登录的管理员会话一起清掉。 -pub fn clear_prefix(prefix: &[u8]) { - let Some(root) = root() else { - return; - }; - let mut guard = root.map.write(); - // 先收集待删键:迭代器借用了 guard,不能边遍历边删 - let victims: Vec> = - guard.iter().filter(|p| p.0.as_bytes().starts_with(prefix)).map(|p| p.0.as_bytes().to_vec()).collect(); - for v in victims { - guard.remove(NgxStr::from_bytes(&v)); - } -} - -/// 遍历某前缀下的全部条目,返回 `(去掉前缀的键名, payload)`。 -/// -/// 这是**低频读**接口(管理台聚合用量统计),不是热路径——每次都会克隆一份 -/// 字符串出来,别拿它做每请求调用。`expires_at` 刻意不返回:统计键永不过期, -/// 调用方用 payload 里的日窗 id 自行判断"今日"。 -pub fn iter_prefix(prefix: &[u8]) -> Vec<(String, String)> { - let Some(root) = root() else { - return Vec::new(); - }; - let guard = root.map.read(); - guard - .iter() - .filter_map(|(k, v)| { - let name = k.as_bytes().strip_prefix(prefix)?; - let (payload, _) = split_value(v.as_bytes())?; - Some(( - String::from_utf8_lossy(name).into_owned(), - String::from_utf8_lossy(payload).into_owned(), - )) - }) - .collect() -} - -/// 与 [`iter_prefix`] 相同,但**额外返回 `expires_at`**——用于会话 / OAuth -/// kick / 过期扫描等需要判断 exp 的场景。`iter_prefix` 保留兼容(统计键 -/// 永不过期、不需要 exp)。 -pub fn iter_prefix_ex(prefix: &[u8]) -> Vec<(String, String, i64)> { - let Some(root) = root() else { - return Vec::new(); - }; - let guard = root.map.read(); - guard - .iter() - .filter_map(|(k, v)| { - let name = k.as_bytes().strip_prefix(prefix)?; - let (payload, exp) = split_value(v.as_bytes())?; - Some(( - String::from_utf8_lossy(name).into_owned(), - String::from_utf8_lossy(payload).into_owned(), - exp, - )) - }) - .collect() -} - -// =========================================================================== -// 业务语义封装 -// =========================================================================== - -/// 管理会话写入 -pub fn put_session(token: &str, username: &str, expires_at: i64) -> bool { - put(crate::config::KEY_SESS_PREFIX, token.as_bytes(), username.as_bytes(), expires_at) -} - -/// 管理会话读取(未过期才返回用户名) -pub fn get_session(token: &str, now: i64) -> Option { - get_live(crate::config::KEY_SESS_PREFIX, token.as_bytes(), now) -} - -pub fn remove_session(token: &str) -> bool { - remove(crate::config::KEY_SESS_PREFIX, token.as_bytes()) -} - -/// 登录失败计数:返回自增后的值 -pub fn bump_login_fail(ip: &str, now: i64) -> i64 { - let current = match get(crate::config::KEY_FAIL_PREFIX, ip.as_bytes()) { - Some((_, exp)) if exp > 0 && exp <= now => 0, - Some((payload, _)) => payload.parse::().unwrap_or(0), - None => 0, - }; - let next = current + 1; - put( - crate::config::KEY_FAIL_PREFIX, - ip.as_bytes(), - next.to_string().as_bytes(), - now + crate::config::LOGIN_FAIL_TTL, - ); - next -} - -pub fn get_login_fail(ip: &str, now: i64) -> i64 { - match get(crate::config::KEY_FAIL_PREFIX, ip.as_bytes()) { - Some((_, exp)) if exp > 0 && exp <= now => 0, - Some((payload, _)) => payload.parse::().unwrap_or(0), - None => 0, - } -} - -pub fn clear_login_fail(ip: &str) { - remove(crate::config::KEY_FAIL_PREFIX, ip.as_bytes()); -} - -/// 窗口计数(限流/配额共用):键固定,value = `<窗口id>:<计数>`,永不过期 -/// (`expires_at = 0`)——跨窗归零靠读时比对窗 id,键数只随用户数增长。 -/// 返回自增后的计数。 -/// -/// 读-增-写跨 worker **没有互斥**:`bump_login_fail` 同款近似语义。 -/// 对限流来说,并发下偶发少计数只是放宽一两 个请求的边界,可接受; -/// 为此上写锁反而让每个 LLM 请求都串行化,得不偿失。 -pub fn bump_window(prefix: &[u8], name: &str, window: i64) -> i64 { - let current = window_count(prefix, name, window); - let next = current + 1; - put(prefix, name.as_bytes(), format!("{}:{}", window, next).as_bytes(), 0); - next -} - -/// 读窗口计数(未跨窗时),管理台展示用量用 -pub fn window_count(prefix: &[u8], name: &str, window: i64) -> i64 { - match get(prefix, name.as_bytes()) { - Some((payload, _)) => match payload.split_once(':') { - Some((w, c)) if w == window.to_string() => c.parse::().unwrap_or(0), - _ => 0, - }, - None => 0, - } -} - -// =========================================================================== -// LLM 代理 -// =========================================================================== - -/// 写入 API Key → 身份索引。 -/// -/// payload = ` \n \n \n `(4 行,与 `users::identity_payload` 一致)。 -/// **永不过期**(`expires_at = 0`):它的生命周期完全跟随 accounts 表的增删, -/// 由 `users` 模块在每次写操作后显式同步——这里不能再加一层 TTL, -/// 否则会出现「账号还在、Key 偶尔 401」这种极难复现的状态。 -pub fn put_ukey(api_key: &str, payload: &str) -> bool { - put(KEY_UKEY_PREFIX, api_key.as_bytes(), payload.as_bytes(), 0) -} - -pub fn remove_ukey(api_key: &str) -> bool { - remove(KEY_UKEY_PREFIX, api_key.as_bytes()) -} diff --git a/rust/crates/ipass-core/src/store.rs b/rust/crates/ipass-core/src/store.rs deleted file mode 100644 index 19e0d5d..0000000 --- a/rust/crates/ipass-core/src/store.rs +++ /dev/null @@ -1,132 +0,0 @@ -//! JSON 文件存储层:原子写 + 跨 worker 互斥。 -//! -//! **状态就是几个 JSON 文件**,不引入数据库。互斥锁在共享内存里的 `RwLock` -//! ——内核本来就要持一块共享内存(state zone),文件锁复用同一块 slab pool, -//! 不必再单独造字典。 -//! -//! 所有写操作统一走 `update_*`,流程固定为: -//! -//! ```text -//! 取全局文件写锁 → 读文件 → 闭包修改 → 原子写回 → 放锁 -//! ``` -//! -//! 闭包返回 `Err` 表示业务校验失败,此时**不落盘**(与旧版 `fn` 返回 nil 一致)。 -//! -//! ## 表的分工 -//! -//! | 文件 | 内容 | 是否进共享内存 | -//! | --- | --- | --- | -//! | `users.json` | 账号、口令哈希、API Key | `ukey:` 索引进(热路径要它) | -//! | `domains.json` | 子域名 → 后端映射 | `sub:` 进(转发热路径) | -//! | `llm-upstreams.json` | LLM 上游基址与密钥 | `up:` 进(LLM 转发热路径) | -//! | `api-proxies.json` | API 代理清单(每条含各自供应商凭据) | `ax:` 进(API 代理热路径) | -//! -//! 内核只直接管 `users.json`;其余数据文件由各插件经 `path_in_data` + -//! 通用读写原语自管(plugin-domain / plugin-llm / plugin-apix)。四个文件 -//! 都在 `data/` 下,都已 `.gitignore`。**LLM 上游表与 API 代理表含真实 -//! 供应商 Key,敏感级别与 `users.json` 相同**,不要再往别处复制。 - -use std::path::PathBuf; -use std::sync::Mutex; - -use crate::config::DEFAULT_DATA_DIR; -use crate::models::UserTable; -use crate::state; - -/// 状态目录。配置阶段由 `ipass_data_dir` 指令设置一次,之后只读。 -static DATA_DIR: Mutex> = Mutex::new(None); - -pub fn set_data_dir(dir: &str) { - if let Ok(mut slot) = DATA_DIR.lock() { - *slot = Some(dir.to_owned()); - } -} - -pub fn data_dir() -> String { - DATA_DIR - .lock() - .ok() - .and_then(|s| s.clone()) - .unwrap_or_else(|| DEFAULT_DATA_DIR.to_owned()) -} - -fn path_of(name: &str) -> PathBuf { - PathBuf::from(data_dir()).join(name) -} - -pub fn users_path() -> PathBuf { - path_of("users.json") -} - -/// data 目录下任意文件的路径(插件用它管理自己的数据文件, -/// 如 plugin-domain 的 `domains.json`) -pub fn path_in_data(name: &str) -> PathBuf { - path_of(name) -} - -// =========================================================================== -// 读写 -// =========================================================================== - -/// 读取并反序列化;文件缺失或损坏时回退 `default`(不抛错,保证转发侧可用) -pub fn read_json(path: &PathBuf, default: T) -> T { - let Ok(text) = std::fs::read_to_string(path) else { - return default; - }; - if text.trim().is_empty() { - return default; - } - match serde_json::from_str(&text) { - Ok(v) => v, - Err(e) => { - // 解析失败必须留痕:静默回退会让「数据看起来还在、实际全空」极难排查 - crate::util::log_err(&format!( - "[store] 解析失败,回退默认值:{} ({e})", - path.display() - )); - default - } - } -} - -/// 原子写:先写 `.tmp` 再 rename,避免读到写了一半的内容 -pub fn write_json(path: &PathBuf, value: &T) -> Result<(), String> { - let text = serde_json::to_string(value).map_err(|e| format!("序列化失败:{e}"))?; - let tmp = PathBuf::from(format!("{}.tmp", path.display())); - - std::fs::write(&tmp, text.as_bytes()).map_err(|e| { - let _ = std::fs::remove_file(&tmp); - format!("写入临时文件失败:{e}") - })?; - - std::fs::rename(&tmp, path).map_err(|e| { - let _ = std::fs::remove_file(&tmp); - format!("重命名失败:{e}") - }) -} - -// =========================================================================== -// 对外接口 -// =========================================================================== - -pub fn read_users() -> UserTable { - read_json(&users_path(), UserTable::new()) -} - -pub fn write_users(data: &UserTable) -> Result<(), String> { - write_json(&users_path(), data) -} - -/// 加锁「读-改-写」账号表。 -/// -/// 闭包返回 `Ok(T)` 表示提交,`Err(msg)` 表示放弃(不写盘)。 -pub fn update_users(f: F) -> Result -where - F: FnOnce(&mut UserTable) -> Result, -{ - let _guard = state::file_lock().ok_or("内部错误:状态锁不可用")?; - let mut data = read_users(); - let out = f(&mut data)?; - write_users(&data)?; - Ok(out) -} diff --git a/rust/crates/ipass-core/src/usage.rs b/rust/crates/ipass-core/src/usage.rs deleted file mode 100644 index ce85dbf..0000000 --- a/rust/crates/ipass-core/src/usage.rs +++ /dev/null @@ -1,239 +0,0 @@ -//! 通用记账原语(无任何业务语义)。 -//! -//! 只回答「给这个前缀下的这个键 +1」「这个键现在的读数是什么」—— -//! **不认识** `ust:` 与 `ums:` 的区别,更不认识「LLM」「token」这些词。 -//! 业务解读(趋势 / 总览 / 按人聚合)在 plugin-llm 的 `stats` 模块, -//! 它把键前缀与本模块的原语组合起来。 -//! -//! value 编码共用两种: -//! * 窗口读数:`<日窗id>:<今日数>:<累计数>:<最近调用时间戳>` -//! * 日桶:`<请求数>:<入>:<出>`(趋势图数据源,TTL 到期自然淘汰) - -use serde_json::{Value, json}; - -use crate::state; -use crate::util; - -/// 一条统计记录的读数。`today` 已按调用方给的日窗折算过(跨天即为 0)。 -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] -pub struct Usage { - /// 今日(本地自然日)计数 - pub today: i64, - /// 累计计数(自该计数键建立以来,不随跨天归零) - pub total: i64, - /// 最近一次记账时间戳;0 = 从未 - pub last_ts: i64, -} - -impl Usage { - /// 累加(聚合用)。`last_ts` 取最大。 - pub fn merge(&mut self, other: &Usage) { - self.today += other.today; - self.total += other.total; - if other.last_ts > self.last_ts { - self.last_ts = other.last_ts; - } - } - - pub fn json(&self) -> Value { - json!({ - "today": self.today, - "total": self.total, - "last_ts": self.last_ts, - "last_ts_text": if self.last_ts > 0 { util::fmt_time(self.last_ts) } else { String::new() }, - }) - } -} - -/// token 计量读数。日窗语义与 [`Usage`] 相同:跨天 today 归零、累计保留。 -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] -pub struct TokenUsage { - /// 今日入 token - pub tin_today: i64, - /// 累计入 token - pub tin_total: i64, - /// 今日出 token - pub tout_today: i64, - /// 累计出 token - pub tout_total: i64, - /// 最近一次有 token 记账的调用时间戳;0 = 从未 - pub last_ts: i64, -} - -impl TokenUsage { - /// 累加(聚合用)。`last_ts` 取最大。 - pub fn merge(&mut self, other: &TokenUsage) { - self.tin_today += other.tin_today; - self.tin_total += other.tin_total; - self.tout_today += other.tout_today; - self.tout_total += other.tout_total; - if other.last_ts > self.last_ts { - self.last_ts = other.last_ts; - } - } - - pub fn json(&self) -> Value { - json!({ - "in_today": self.tin_today, - "in_total": self.tin_total, - "out_today": self.tout_today, - "out_total": self.tout_total, - "last_ts": self.last_ts, - "last_ts_text": if self.last_ts > 0 { util::fmt_time(self.last_ts) } else { String::new() }, - }) - } -} - -/// 一天的聚合值(日桶) -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] -pub struct DayBucket { - pub requests: i64, - pub tin: i64, - pub tout: i64, -} - -impl DayBucket { - pub fn merge(&mut self, other: &DayBucket) { - self.requests += other.requests; - self.tin += other.tin; - self.tout += other.tout; - } -} - -// =========================================================================== -// 窗口读数(<日窗id>:<今日>:<累计>:<最近时间戳>) -// =========================================================================== - -fn encode(day: i64, u: &Usage) -> String { - format!("{}:{}:{}:{}", day, u.today, u.total, u.last_ts) -} - -/// 解码并把"今日数"按当前日窗折算——跨天时旧值归零,累计值保留。 -fn decode(payload: &str, day: i64) -> Usage { - let mut it = payload.split(':'); - let stored_day = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let today = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let total = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let last_ts = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - Usage { today: if stored_day == day { today } else { 0 }, total, last_ts } -} - -/// 窗口计数 +1(读-增-写不加写锁的近似语义:并发下偶发少计一次, -/// 统计用途无影响;换来的是调用方热路径不为统计抢锁)。 -pub fn bump(prefix: &[u8], name: &str, day: i64, now: i64) { - let current = state::get(prefix, name.as_bytes()) - .map(|(payload, _)| decode(&payload, day)) - .unwrap_or_default(); - - let next = Usage { - today: current.today + 1, - total: current.total + 1, - last_ts: now, - }; - state::put(prefix, name.as_bytes(), encode(day, &next).as_bytes(), 0); -} - -/// 读窗口读数(不跨窗折算由 decode 按 day 完成) -pub fn read(prefix: &[u8], name: &str, day: i64) -> Usage { - state::get(prefix, name.as_bytes()) - .map(|(payload, _)| decode(&payload, day)) - .unwrap_or_default() -} - -// =========================================================================== -// token 窗口读数(<日窗id>:<入今日>:<入累计>:<出今日>:<出累计>:<最近时间戳>) -// =========================================================================== - -fn encode_t(day: i64, t: &TokenUsage) -> String { - format!( - "{}:{}:{}:{}:{}:{}", - day, t.tin_today, t.tin_total, t.tout_today, t.tout_total, t.last_ts - ) -} - -fn decode_t(payload: &str, day: i64) -> TokenUsage { - let mut it = payload.split(':'); - let stored_day = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let in_today = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let in_total = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let out_today = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let out_total = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let last_ts = it.next().and_then(|s| s.parse::().ok()).unwrap_or(0); - let live = stored_day == day; - TokenUsage { - tin_today: if live { in_today } else { 0 }, - tin_total: in_total, - tout_today: if live { out_today } else { 0 }, - tout_total: out_total, - last_ts, - } -} - -/// token 窗口累加(同 bump 的近似语义) -pub fn bump_t(prefix: &[u8], name: &str, tin: u64, tout: u64, day: i64, now: i64) { - let current = state::get(prefix, name.as_bytes()) - .map(|(payload, _)| decode_t(&payload, day)) - .unwrap_or_default(); - let next = TokenUsage { - tin_today: current.tin_today + tin as i64, - tin_total: current.tin_total + tin as i64, - tout_today: current.tout_today + tout as i64, - tout_total: current.tout_total + tout as i64, - last_ts: now, - }; - state::put(prefix, name.as_bytes(), encode_t(day, &next).as_bytes(), 0); -} - -/// 读 token 窗口读数 -pub fn read_t(prefix: &[u8], name: &str, day: i64) -> TokenUsage { - state::get(prefix, name.as_bytes()) - .map(|(payload, _)| decode_t(&payload, day)) - .unwrap_or_default() -} - -// =========================================================================== -// 日桶(趋势图数据源) -// =========================================================================== -// -// 以自然日为键的桶:一个桶一天,读取时按日窗 id **精确取键**(get_live), -// 不做前缀扫描——键数随时间增长也不会拖慢任何热路径。 -// TTL 由调用方传入(业务保留策略属于插件,不属于本模块)。 - -/// `<请求数>:<入token>:<出token>` -fn encode_day(b: &DayBucket) -> String { - format!("{}:{}:{}", b.requests, b.tin, b.tout) -} - -fn decode_day(payload: &str) -> DayBucket { - let mut it = payload.split(':'); - let num = |v: Option<&str>| v.and_then(|s| s.parse::().ok()).unwrap_or(0); - DayBucket { - requests: num(it.next()), - tin: num(it.next()), - tout: num(it.next()), - } -} - -/// 累加一日桶。`ttl_secs` 为保留时长(到期由共享内存自然淘汰)。 -pub fn bump_day(prefix: &[u8], name: &str, delta: &DayBucket, ttl_secs: i64) { - let mut next = state::get_live(prefix, name.as_bytes(), util::now()) - .map(|p| decode_day(&p)) - .unwrap_or_default(); - next.merge(delta); - // 写入失败只记日志:统计是旁路功能,绝不影响转发 - if !state::put( - prefix, - name.as_bytes(), - encode_day(&next).as_bytes(), - util::now() + ttl_secs, - ) { - util::log_err("[usage] 日桶写入失败(共享内存不足?)"); - } -} - -/// 读一个日桶(不存在 = 全 0) -pub fn day_bucket(prefix: &[u8], name: &str) -> DayBucket { - state::get_live(prefix, name.as_bytes(), util::now()) - .map(|p| decode_day(&p)) - .unwrap_or_default() -} diff --git a/rust/crates/ipass-core/src/users.rs b/rust/crates/ipass-core/src/users.rs deleted file mode 100644 index 365ab2c..0000000 --- a/rust/crates/ipass-core/src/users.rs +++ /dev/null @@ -1,463 +0,0 @@ -//! 账号体系:管理员与普通账号共用一张表。 -//! -//! 账号数据见 `models::User`。 -//! -//! 「临时授权权限」= 该账号申请**临时域名**时可跳过管理员审核、直接放行。 -//! 注意它只对 temp 生效,long 域名无论谁申请都要审核。 -//! -//! ## 与 LLM 代理共用同一把 Key -//! -//! `api_key` 同时是域名接口(`X-API-Key`)与 LLM 代理 -//! (`Authorization: Bearer`)的凭据——这正是「用同一套 Key 验证」的落点。 -//! 代价是**任何改动 `api_key` 的操作都必须同步共享内存索引**, -//! 且重置 Key 时必须**先摘掉旧索引**,否则被换掉的 Key 依然能用。 - -use std::collections::BTreeMap; -use std::sync::Mutex; - -use serde_json::{Value, json}; - -use crate::config::{ - DISPLAY_NAME_MAX_LEN, INIT_ADMIN_PASSWORD_DEFAULT, INIT_ADMIN_PASSWORD_ENV, INIT_ADMIN_USER, - KEY_UKEY_PREFIX, PASSWORD_MIN_LEN, TEMP_GRANT_TTL, USERNAME_MAX_LEN, USERNAME_MIN_LEN, -}; -use crate::models::{User, UserTable}; -use crate::state; -use crate::store; -use crate::util; - -// =========================================================================== -// 账号删除钩子 -// =========================================================================== - -/// 「账号被删除时我需要清理什么」的回调注册表。 -/// -/// 内核只负责自己的键(`ukey:` / `sess:` / `fail:`),插件的键空间(用量账本、 -/// 代理索引……)内核**不认识**——删除账号要摘几个前缀,取决于装了哪些插件, -/// 所以只能由插件在装载阶段调用 [`on_delete`] 注册清理器(如 plugin-llm 注册 -/// `stats::forget`)。**重复注册同一函数会被去重**(nginx reload 会多次走到注册点)。 -static ON_DELETE: Mutex> = Mutex::new(Vec::new()); - -/// 插件注册「账号被删除时我需要清理什么」。在 zone 装载后、接请求前调用一次。 -pub fn on_delete(f: fn(&str)) { - if let Ok(mut hooks) = ON_DELETE.lock() { - if !hooks.contains(&f) { - hooks.push(f); - } - } -} - -/// 账号删除成功后执行全部钩子。单个钩子没有失败语义(清理动作自身幂等)。 -fn run_delete_hooks(username: &str) { - if let Ok(hooks) = ON_DELETE.lock() { - for f in hooks.iter() { - f(username); - } - } -} - -// =========================================================================== -// 校验 -// =========================================================================== - -/// 账号名:2-32 位字母、数字、下划线或短横线 -fn valid_username(name: &str) -> bool { - let n = name.chars().count(); - if n < USERNAME_MIN_LEN || n > USERNAME_MAX_LEN { - return false; - } - name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') -} - -/// 展示名:允许中文/空格,但**不允许换行或制表等控制字符**—— -/// 它会被塞进管理台 HTML(已转义)与 JSON,控制字符只会造成排版错乱。 -fn check_display_name(raw: &str) -> Result { - let v = util::trim(raw).to_owned(); - if v.chars().count() > DISPLAY_NAME_MAX_LEN { - return Err(format!("展示名不能超过 {DISPLAY_NAME_MAX_LEN} 个字")); - } - if v.chars().any(|c| c.is_control()) { - return Err("展示名不能包含换行或控制字符".to_owned()); - } - Ok(v) -} - -// =========================================================================== -// 共享内存同步(Key 索引) -// =========================================================================== - -/// 身份 payload:` \n \n \n `。 -/// -/// 一次查表同时拿到「是谁 / 能用哪些上游 / 限流多少」——LLM 热路径因此完全 -/// 不必回读 `users.json`(那是同步文件 I/O + 全表 JSON 解码)。 -/// 行数固定为 4;旧版 2 行 payload 由读取方按缺省值兜底。 -fn identity_payload(user: &User) -> String { - format!( - "{}\n{}\n{}\n{}", - user.username, - user.allowed_models.join(","), - user.llm_rpm, - user.llm_daily - ) -} - -/// 把账号的 Key 索引写进共享内存。**所有改动 `api_key` 的写操作都必须调用**。 -/// -/// 写失败(共享内存 zone 满 / 分配失败)会让这个 Key **静默失效**:热路径查不到 -/// 就是 401,且没有任何回源兜底。所以这里必须自己出声——「新建了 Key 却调不通、 -/// 日志里什么都没有」是这类问题最难查的形态。调用方是否检查返回值不影响本日志。 -pub fn sync_shm(user: &User) -> bool { - let ok = state::put_ukey(&user.api_key, &identity_payload(user)); - if !ok { - util::log_err(&format!( - "[users] Key 索引写共享内存失败 user={} —— 该 Key 调 LLM 会 401(共享内存可能已满)", - user.username - )); - } - ok -} - -/// 全量重载 Key 索引(启动装载 / 批量变更后)。返回装载条数。 -/// -/// 只清 `ukey:` 前缀,不会误伤转发映射、上游表与管理会话。 -/// `loaded < total` 时单独报一条 error:单个账号的失败已在 `sync_shm` 里出声, -/// 这里再汇总一次,避免「装载了 3/5 却只在滚动日志里淹没」。 -pub fn reload_shm() -> usize { - state::clear_prefix(KEY_UKEY_PREFIX); - let table = store::read_users(); - let total = table.len(); - let mut loaded = 0; - for user in table.into_values() { - if sync_shm(&user) { - loaded += 1; - } - } - if loaded != total { - util::log_err(&format!( - "[users] Key 索引装载不全:{loaded}/{total} —— 有账号将无法调用 LLM" - )); - } - loaded -} - -// =========================================================================== -// 查询 -// =========================================================================== - -pub fn get(username: &str) -> Option { - if username.is_empty() { - return None; - } - store::read_users().remove(username) -} - -/// 全部账号,按账号名升序 -pub fn all() -> Vec { - store::read_users().into_values().collect() -} - -/// 按 API Key 反查账号(常量时间比较,避免时序侧信道)。 -/// -/// **函数名里没有「快」字**:它每次都同步读 `users.json` 并全表解码, -/// 只适合管理接口这类低频调用方。LLM 转发热路径请用 `llm::identity`, -/// 那条路只查共享内存。 -pub fn find_by_key(key: &str) -> Option { - if key.is_empty() { - return None; - } - store::read_users() - .into_values() - .find(|u| util::ct_eq(&u.api_key, key)) -} - -// =========================================================================== -// 写入 -// =========================================================================== - -fn build_user( - username: &str, - display_name: &str, - password: &str, - role: &str, - grant_temp: bool, -) -> User { - let salt = util::gen_salt(); - let now = util::now(); - User { - username: username.to_owned(), - display_name: display_name.to_owned(), - role: role.to_owned(), - pwd_hash: util::hash_password(password, &salt), - salt, - api_key: util::gen_key(), - allowed_models: Vec::new(), - llm_rpm: crate::config::LLM_RPM_DEFAULT, - llm_daily: crate::config::LLM_DAILY_DEFAULT, - temp_grant_until: if grant_temp { now + TEMP_GRANT_TTL } else { 0 }, - created_at: now, - extra: BTreeMap::new(), - } -} - -/// 首次启动播种管理员。已有数据时原样返回,**不会覆盖**。 -/// -/// 返回 `Ok(true)` 表示「本次真的创建了」,用于打一条提醒日志。 -pub fn seed_admin() -> Result { - let password = - std::env::var(INIT_ADMIN_PASSWORD_ENV).unwrap_or_else(|_| INIT_ADMIN_PASSWORD_DEFAULT.to_owned()); - - let mut created: Option = None; - store::update_users(|users| { - if !users.is_empty() { - return Ok(()); - } - let admin = build_user(INIT_ADMIN_USER, "", &password, "admin", false); - users.insert(admin.username.clone(), admin.clone()); - created = Some(admin); - Ok(()) - })?; - - match created { - Some(u) => { - // 播种发生在 init_process(每个 worker 各一次),这里必须写穿, - // 否则「第一个 worker 建了账号、第二个 worker 覆盖索引」这类竞态 - // 会让新建的 Key 时灵时不灵。 - sync_shm(&u); - Ok(true) - } - None => Ok(false), - } -} - -pub fn create( - username: &str, - display_name: &str, - password: &str, - role: &str, - grant_temp: bool, -) -> Result { - let username = util::trim(username).to_owned(); - if !valid_username(&username) { - return Err(format!( - "账号名只能是 {USERNAME_MIN_LEN}-{USERNAME_MAX_LEN} 位字母、数字、下划线或短横线" - )); - } - let display_name = check_display_name(display_name)?; - if password.len() < PASSWORD_MIN_LEN { - return Err(format!("密码至少 {PASSWORD_MIN_LEN} 位")); - } - let role = if role == "admin" { "admin" } else { "user" }; - - // 先做一次存在性预检,省掉白算 1000 轮哈希;真正的判定仍在锁内 - if store::read_users().contains_key(&username) { - return Err(format!("账号 {username} 已存在")); - } - - let user = build_user(&username, &display_name, password, role, grant_temp); - let created = user.clone(); - store::update_users(move |users| { - if users.contains_key(&username) { - return Err(format!("账号 {username} 已存在")); - } - users.insert(username.clone(), user); - Ok(()) - })?; - - sync_shm(&created); - Ok(created) -} - -/// 设置展示名(中文姓名等)。只影响管理台显示,不碰 Key / 口令 / 白名单。 -pub fn set_display_name(username: &str, display_name: &str) -> Result { - let display_name = check_display_name(display_name)?; - let updated = store::update_users(move |users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - user.display_name = display_name; - Ok(user.clone()) - })?; - // 展示名不在 ukey payload 里(热路径不需要它),无需同步共享内存 - Ok(updated) -} - -/// 重置 API Key,返回新 Key。 -/// -/// **必须同时摘掉旧索引**:LLM 热路径查的是 `ukey:`,若只加新的不删旧的, -/// 被重置掉的 Key 仍然能调通模型——用户以为「换钥匙就安全了」,实际没有。 -pub fn regenerate_key(username: &str) -> Result { - let (old_key, new_key, updated) = store::update_users(|users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - let old = user.api_key.clone(); - user.api_key = util::gen_key(); - Ok((old, user.api_key.clone(), user.clone())) - })?; - - state::remove_ukey(&old_key); - sync_shm(&updated); - Ok(new_key) -} - -/// 设置账号可用的 LLM 上游白名单。传空表表示不限。 -pub fn set_allowed_models(username: &str, slots: Vec) -> Result { - let updated = store::update_users(move |users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - user.allowed_models = slots; - Ok(user.clone()) - })?; - - sync_shm(&updated); - Ok(updated) -} - -/// 修改账号的插件扩展字段(`User.extra`)。 -/// -/// 闭包拿到 `&mut User`——可同时改内核字段与 `extra`,由调用方决定语义。 -/// 走 `store::update_users` 的「读-改-写 + 写穿 shm」统一路径,保证 shm -/// `ukey:` 索引与 `users.json` 同步刷新。 -/// -/// 配套 [`crate::models::User::extra`] 的 `#[serde(flatten)]`——上游 -/// sso 收敛时提议(A1),按插件字段透传通道走。 -pub fn update_extra(username: &str, f: F) -> Result -where - F: FnOnce(&mut User) -> Result, -{ - // 闭包内拿到 user 引用 → 调 f 修改 → f 返回后借用释放 → clone 出 owned - // user 给 sync_shm。`update_users` 的闭包签名是 `&mut UserTable`,把 - // UserTable 的可变借用拆借给 &mut User 是 OK 的(Rust 借用分裂)。 - let (out, updated) = store::update_users(move |users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - let out = f(user)?; - Ok((out, user.clone())) - })?; - sync_shm(&updated); - Ok(out) -} - -/// 设置账号的 LLM 限流参数。`None` = 保持现值;`0` = 不限;正数 = 上限。 -/// -/// 改动的是 `ukey:` payload 的组成部分,所以**必须写穿**—— -/// 否则表现为「改完限额,热路径还按旧额度放行」。 -pub fn set_llm_limits(username: &str, rpm: Option, daily: Option) -> Result { - if let Some(v) = rpm { - if v < 0 { - return Err("每分钟上限不能为负数(0 表示不限)".to_owned()); - } - } - if let Some(v) = daily { - if v < 0 { - return Err("每日上限不能为负数(0 表示不限)".to_owned()); - } - } - - let updated = store::update_users(move |users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - if let Some(v) = rpm { - user.llm_rpm = v; - } - if let Some(v) = daily { - user.llm_daily = v; - } - Ok(user.clone()) - })?; - - sync_shm(&updated); - Ok(updated) -} - -/// 授予临时授权权限(默认 6 小时),返回到期时间戳 -pub fn grant_temp(username: &str, hours: Option<&String>) -> Result { - let ttl = hours - .and_then(|h| h.parse::().ok()) - .map(|h| h * 3600) - .unwrap_or(TEMP_GRANT_TTL); - - store::update_users(|users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - user.temp_grant_until = util::now() + ttl; - Ok(user.temp_grant_until) - }) -} - -pub fn revoke_temp(username: &str) -> Result<(), String> { - store::update_users(|users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - user.temp_grant_until = 0; - Ok(()) - }) -} - -pub fn set_password(username: &str, password: &str) -> Result<(), String> { - if password.len() < PASSWORD_MIN_LEN { - return Err(format!("密码至少 {PASSWORD_MIN_LEN} 位")); - } - let salt = util::gen_salt(); - let hash = util::hash_password(password, &salt); - - // 改口令不动 api_key,因此不需要同步 Key 索引 - store::update_users(move |users: &mut UserTable| { - let user = users.get_mut(username).ok_or("账号不存在")?; - user.salt = salt; - user.pwd_hash = hash; - Ok(()) - }) -} - -/// 删除账号。两条红线:不能删自己、必须至少保留一个管理员。 -/// -/// 顺带摘掉 Key 索引——否则删掉的账号仍能通过 LLM 代理调用模型。 -pub fn delete(username: &str, operator: &str) -> Result<(), String> { - if username == operator { - return Err("不能删除当前登录的账号".to_owned()); - } - let username = username.to_owned(); - let deleted = username.clone(); - - let removed_key = store::update_users(move |users| { - let user = users.get(&username).ok_or("账号不存在")?; - if user.role == "admin" { - let admins = users.values().filter(|u| u.role == "admin").count(); - if admins <= 1 { - return Err("至少需要保留一个管理员账号".to_owned()); - } - } - Ok(users.remove(&username).map(|u| u.api_key).unwrap_or_default()) - })?; - - if !removed_key.is_empty() { - state::remove_ukey(&removed_key); - } - // 内核自己的键已摘掉;用量账本等插件键空间由 on_delete 钩子清理 - // (谁装了谁清——钩子为空说明对应插件未装配,自然也无键可清) - run_delete_hooks(&deleted); - Ok(()) -} - -// =========================================================================== -// 对外投影 -// =========================================================================== - -/// 转成前端可见的字段(**绝不包含 salt / pwd_hash**) -pub fn view(user: &User) -> Value { - let now = util::now(); - let until_ts = user.temp_grant_until; - let active = until_ts > now; - - json!({ - "username": user.username, - "display_name": user.display_name, - // 前端统一用 name 渲染——展示名优先,没有就回落到账号名 - "name": if user.display_name.is_empty() { user.username.clone() } else { user.display_name.clone() }, - "role": user.role, - "api_key": user.api_key, - "api_key_masked": util::mask_key(&user.api_key), - "allowed_models": user.allowed_models, - "llm_rpm": user.llm_rpm, - "llm_daily": user.llm_daily, - "created_at": user.created_at, - "created_at_text": util::fmt_time(user.created_at), - "temp_grant": active, - "temp_grant_until": until_ts, - "temp_grant_until_text": if active { util::fmt_time(until_ts) } else { String::new() }, - "temp_grant_left": if active { until_ts - now } else { 0 }, - "temp_grant_left_text": if active { util::fmt_left(until_ts - now) } else { String::new() }, - }) -} diff --git a/rust/crates/ipass-core/src/util.rs b/rust/crates/ipass-core/src/util.rs deleted file mode 100644 index 3adbba2..0000000 --- a/rust/crates/ipass-core/src/util.rs +++ /dev/null @@ -1,389 +0,0 @@ -//! 通用工具:时间 / 随机 / 摘要 / 口令哈希 / 常量时间比较 / 格式化 / 参数解码。 -//! -//! 其中 `hash_password` 的算法**必须逐位一致**(口径沿用自重构前实现, -//! 存量 `users.json` 才能正常登录), -//! 否则旧的 `data/users.json` 里的口令哈希将全部失效。 - -use sha2::{Digest, Sha256}; - -use crate::config::HASH_ROUNDS; - -// =========================================================================== -// 日志 -// =========================================================================== - -/// 写 nginx error log。 -/// -/// 形参顺序容易记错:`ngx_log_error_core(level, log, err, fmt, ...)`, -/// 其中 `err` 只在需要附带系统错误号时非 0。 -pub fn log_at(level: usize, msg: &str) { - // SAFETY: 传入的 CString 在本调用期间存活;日志接口不持有其引用 - unsafe { - let c = std::ffi::CString::new(msg).unwrap_or_default(); - let lg = if nginx_sys::ngx_cycle.is_null() { - core::ptr::null_mut() - } else { - (*nginx_sys::ngx_cycle).log - }; - nginx_sys::ngx_log_error_core(level, lg, 0, c"%s".as_ptr(), c.as_ptr()); - } -} - -pub fn log_err(msg: &str) { - log_at(nginx_sys::NGX_LOG_ERR as usize, msg); -} - -pub fn log_warn(msg: &str) { - log_at(nginx_sys::NGX_LOG_WARN as usize, msg); -} - -pub fn log_notice(msg: &str) { - log_at(nginx_sys::NGX_LOG_NOTICE as usize, msg); -} - -/// INFO 级日志:只在 error_log 配了 `info` 时才落盘(生产 `conf/nginx.conf` 是 `info`)。 -/// 用于「周期性但需要留痕」的动作(如统计落盘),NOTICE 会太吵、ERR 又会让正常的 -/// 例行动作看起来像故障。 -pub fn log_info(msg: &str) { - log_at(nginx_sys::NGX_LOG_INFO as usize, msg); -} - -// =========================================================================== -// 时间 -// =========================================================================== - -/// 当前 Unix 时间戳(秒)。 -/// -/// nginx 的 `ngx_time()` 是宏,bindgen 拿不到;直接走 libc 的 `time()`, -/// 在 Linux/macOS 上都由 vDSO 提供,代价与读一个全局变量相当。 -#[inline] -pub fn now() -> i64 { - // SAFETY: time(NULL) 只读系统时钟,无副作用 - unsafe { libc::time(core::ptr::null_mut()) as i64 } -} - -/// `%Y-%m-%d %H:%M:%S`(本地时区,等价旧版 `os.date`);ts <= 0 返回 `-` -pub fn fmt_time(ts: i64) -> String { - if ts <= 0 { - return "-".to_owned(); - } - // SAFETY: 传入有效指针;localtime_r 不返回静态数据,线程安全 - unsafe { - let t = ts as libc::time_t; - let mut tm: libc::tm = core::mem::zeroed(); - if libc::localtime_r(&t, &mut tm).is_null() { - return "-".to_owned(); - } - let mut buf = [0 as libc::c_char; 40]; - let fmt = c"%Y-%m-%d %H:%M:%S"; - let n = libc::strftime(buf.as_mut_ptr(), buf.len(), fmt.as_ptr(), &tm); - if n == 0 { - return "-".to_owned(); - } - core::ffi::CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned() - } -} - -/// `%m-%d`(本地时区)。趋势图 x 轴用——带上年份会让 14 天的轴标签又长又重复 -pub fn fmt_day(ts: i64) -> String { - if ts <= 0 { - return "-".to_owned(); - } - // SAFETY: 同 fmt_time - unsafe { - let t = ts as libc::time_t; - let mut tm: libc::tm = core::mem::zeroed(); - if libc::localtime_r(&t, &mut tm).is_null() { - return "-".to_owned(); - } - let mut buf = [0 as libc::c_char; 16]; - let fmt = c"%m-%d"; - let n = libc::strftime(buf.as_mut_ptr(), buf.len(), fmt.as_ptr(), &tm); - if n == 0 { - return "-".to_owned(); - } - core::ffi::CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned() - } -} - -/// 本地时区的「日窗 id」(年份×1000 + 年内第几天),跨天自动翻窗。 -/// -/// 限流配额按**本地日**切(面向中国用户,即东八区自然日),不用 UTC—— -/// UTC 的日界线在北京时间早上 8 点,「每日额度」会在半夜重置,反直觉。 -pub fn day_id(ts: i64) -> i64 { - // SAFETY: localtime_r 只读时间戳并填充调用方的结构体 - unsafe { - let t = ts as libc::time_t; - let mut tm: libc::tm = core::mem::zeroed(); - if libc::localtime_r(&t, &mut tm).is_null() { - return ts.div_euclid(86400); // 兜底:退化成 UTC 日 - } - tm.tm_year as i64 * 1000 + tm.tm_yday as i64 - } -} - -/// 剩余时长的中文描述(与旧版 `fmt_left` 输出一致) -pub fn fmt_left(seconds: i64) -> String { - if seconds <= 0 { - return "已过期".to_owned(); - } - let h = seconds / 3600; - let m = (seconds % 3600) / 60; - let s = seconds % 60; - if h > 0 { - format!("{h} 小时 {m} 分") - } else if m > 0 { - format!("{m} 分 {s} 秒") - } else { - format!("{s} 秒") - } -} - -// =========================================================================== -// 随机与十六进制 -// =========================================================================== - -pub fn to_hex(bytes: &[u8]) -> String { - const HEX: &[u8; 16] = b"0123456789abcdef"; - let mut out = String::with_capacity(bytes.len() * 2); - for &b in bytes { - out.push(HEX[(b >> 4) as usize] as char); - out.push(HEX[(b & 0x0f) as usize] as char); - } - out -} - -/// 密码学安全随机字节。取不到随机源时返回 None(调用方必须显式处理, -/// **不允许**退化成可预测的伪随机——那等于把 API Key 直接送给攻击者)。 -fn rand_bytes(buf: &mut [u8]) -> Option<()> { - getrandom::getrandom(buf).ok() -} - -/// `n` 字节随机数的 hex 表示(会话 token 等场景) -pub fn rand_hex(n: usize) -> String { - let mut b = vec![0u8; n]; - if rand_bytes(&mut b).is_none() { - let mut seed = now() as u64 ^ 0x2545_f491_4f6c_dd1d; - for byte in b.iter_mut() { - // xorshift:随机源失效时的最后手段,至少不产出常量 - seed ^= seed << 13; - seed ^= seed >> 7; - seed ^= seed << 17; - *byte = (seed & 0xff) as u8; - } - } - to_hex(&b) -} - -/// 口令盐:16 字节 → 32 位 hex -pub fn gen_salt() -> String { - let mut b = [0u8; 16]; - if rand_bytes(&mut b).is_none() { - // 极罕见:随机源不可用。此处不能 panic(会崩掉整个 worker), - // 退化为基于时间的混淆值并让上层记录失败。 - let n = now() as u64 ^ 0x9e37_79b9_7f4a_7c15; - b[..8].copy_from_slice(&n.to_le_bytes()); - b[8..].copy_from_slice(&n.rotate_left(17).to_le_bytes()); - } - to_hex(&b) -} - -/// 账号 API Key:`sk_` + 24 字节 → 48 位 hex -pub fn gen_key() -> String { - let mut b = [0u8; 24]; - if rand_bytes(&mut b).is_none() { - let n = now() as u64; - for (i, chunk) in b.chunks_mut(8).enumerate() { - chunk.copy_from_slice(&n.rotate_left((i as u32) * 13).to_le_bytes()); - } - } - format!("sk_{}", to_hex(&b)) -} - -/// 域名记录 id:`d_` + 6 字节 → 12 位 hex -pub fn gen_id() -> String { - let mut b = [0u8; 6]; - if rand_bytes(&mut b).is_none() { - let n = now() as u32; - b[..4].copy_from_slice(&n.to_le_bytes()); - b[4..].copy_from_slice(&(n ^ 0xa5a5).to_le_bytes()[..2]); - } - format!("d_{}", to_hex(&b)) -} - -/// `n` 位纯数字随机串(每位 0-9 等概率)。 -/// -/// 用**拒绝采样**而不是 `byte % 10`:256 不是 10 的倍数,直接取模会让 0-5 -/// 比 6-9 各多出现约 0.4%(对子域名无害,但没理由留这个瑕疵)。 -/// 只接受 `< 250` 的字节(250 = 25×10),落桶严格均匀。 -/// -/// 非安全场景(子域名后缀),但仍复用 `getrandom`——顺带拿到「随机源不可用」 -/// 的统一退化路径,且随机源失效时绝不 panic。 -pub fn rand_digits(n: usize) -> String { - let mut out = String::with_capacity(n); - let mut buf = [0u8; 32]; - // 随机源不可用时退化为 xorshift(与 rand_hex 同款兜底),仍会终止 - let mut seed = now() as u64 ^ 0x2545_f491_4f6c_dd1d; - while out.len() < n { - let take = (n - out.len()).min(buf.len()); - let slice = &mut buf[..take]; - let ok = rand_bytes(slice).is_some(); - if !ok { - for byte in slice.iter_mut() { - seed ^= seed << 13; - seed ^= seed >> 7; - seed ^= seed << 17; - *byte = (seed & 0xff) as u8; - } - } - for &b in slice.iter() { - if b < 250 && out.len() < n { - out.push((b'0' + (b % 10)) as char); - } - } - } - out -} - -// =========================================================================== -// 摘要与口令 -// =========================================================================== - -pub fn sha256_hex(data: &[u8]) -> String { - let mut h = Sha256::new(); - h.update(data); - to_hex(&h.finalize()) -} - -/// 流式 SHA256:分块喂入,最后取 hex。 -/// -/// 用于「大文件只想拿指纹、不想整份读进内存」的场景(如审计算请求体哈希)。 -/// 与 `sha256_hex` 对同一份数据必然产出相同结果(同算法、同 hex 编码)。 -pub struct Sha256Stream(Sha256); - -impl Sha256Stream { - pub fn new() -> Self { - Self(Sha256::new()) - } - - pub fn update(&mut self, chunk: &[u8]) { - self.0.update(chunk); - } - - /// 收尾并返回 hex(64 字符)。消费 self,避免误续用已 finalize 的状态。 - pub fn finish_hex(self) -> String { - to_hex(&self.0.finalize()) - } -} - -impl Default for Sha256Stream { - fn default() -> Self { - Self::new() - } -} - -/// 迭代加盐哈希,降低撞库速度。 -/// -/// 算法(与旧版 `util.hash_password` 完全一致): -/// x = sha256(salt + ":" + password) -/// 重复 HASH_ROUNDS 次:x = sha256(x + salt) -pub fn hash_password(password: &str, salt: &str) -> String { - let mut x = sha256_hex(format!("{salt}:{password}").as_bytes()); - for _ in 0..HASH_ROUNDS { - x = sha256_hex(format!("{x}{salt}").as_bytes()); - } - x -} - -pub fn verify_password(password: &str, salt: &str, expected: &str) -> bool { - ct_eq(&hash_password(password, salt), expected) -} - -/// 常量时间比较:无论差异出现在第几位,都在同一轮数内完成。 -pub fn ct_eq(a: &str, b: &str) -> bool { - let (a, b) = (a.as_bytes(), b.as_bytes()); - if a.len() != b.len() { - return false; - } - let mut diff = 0u8; - for i in 0..a.len() { - diff |= a[i] ^ b[i]; - } - diff == 0 -} - -// =========================================================================== -// 文本 -// =========================================================================== - -pub fn trim(v: &str) -> &str { - v.trim() -} - -pub fn to_bool(v: Option<&String>) -> bool { - match v { - Some(s) => matches!(s.to_ascii_lowercase().as_str(), "1" | "true" | "on" | "yes"), - None => false, - } -} - -/// `sk_12345678...abcd` → `sk_12345...abcd` -pub fn mask_key(key: &str) -> String { - if key.len() < 12 { - return key.to_owned(); - } - format!("{}...{}", &key[..8], &key[key.len() - 4..]) -} - -/// percent-decode(`%XX`)并把 `+` 还原为空格。 -/// -/// 旧版由 `ngx.req.get_uri_args` / `get_post_args` 自动完成, -/// Rust 侧必须自己实现,否则带中文或特殊字符的参数会解析错误。 -pub fn url_decode(input: &str) -> String { - let bytes = input.as_bytes(); - let mut out: Vec = Vec::with_capacity(bytes.len()); - let mut i = 0; - while i < bytes.len() { - match bytes[i] { - b'+' => { - out.push(b' '); - i += 1; - } - b'%' if i + 2 < bytes.len() => { - let hi = (bytes[i + 1] as char).to_digit(16); - let lo = (bytes[i + 2] as char).to_digit(16); - match (hi, lo) { - (Some(h), Some(l)) => { - out.push((h * 16 + l) as u8); - i += 3; - } - _ => { - out.push(b'%'); - i += 1; - } - } - } - b => { - out.push(b); - i += 1; - } - } - } - String::from_utf8_lossy(&out).into_owned() -} - -/// 解析 `a=1&b=2` 形式的键值对(自动 percent-decode)。 -pub fn parse_kv(raw: &[u8]) -> Vec<(String, String)> { - let mut out = Vec::new(); - let text = String::from_utf8_lossy(raw); - for pair in text.split('&') { - if pair.is_empty() { - continue; - } - let mut it = pair.splitn(2, '='); - let k = it.next().unwrap_or(""); - let v = it.next().unwrap_or(""); - out.push((url_decode(k), url_decode(v))); - } - out -} diff --git a/rust/crates/plugin-apix/Cargo.toml b/rust/crates/plugin-apix/Cargo.toml deleted file mode 100644 index bd33984..0000000 --- a/rust/crates/plugin-apix/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "plugin-apix" -version = "2.0.0" -edition.workspace = true -publish = false -description = "iPass 插件:API 代理(外部供应商资产挂网关,复用 LLM 下行契约)" - -[lib] -# 只有 gateway 是 cdylib;插件一律 rlib(跨 .so 传 Rust 类型会双重 free) -crate-type = ["rlib"] - -[dependencies] -ngx.workspace = true -nginx-sys.workspace = true -serde.workspace = true -serde_json.workspace = true -ipass-core.workspace = true -# 用量记账:apix 与 LLM 共用同一片 ust:/ums:/ud: 键空间(slot 带 api/ 前缀归组), -# 记账入口只有一个(plugin_llm::stats),避免两份写入口漂移 -plugin-llm.workspace = true diff --git a/rust/crates/plugin-apix/src/api.rs b/rust/crates/plugin-apix/src/api.rs deleted file mode 100644 index 3821543..0000000 --- a/rust/crates/plugin-apix/src/api.rs +++ /dev/null @@ -1,198 +0,0 @@ -//! API 代理(apix)插件的管理 API 端点。 -//! -//! 鉴权语义与内核共用一套:`ipass_core::api::{Guard, Actor, run_guarded}`。 - -use ngx::core::{Pool, Status}; -use ngx::ffi::ngx_http_request_t; -use ngx::http::HTTPStatus; -use serde_json::{Value, json}; - -use ipass_core::api::{Actor, Guard, run_guarded}; -use ipass_core::{http, util}; - -use crate::apix; -use crate::config; - -// =========================================================================== -// 路由 -// =========================================================================== - -type Handler = fn(*mut ngx_http_request_t, Actor) -> Status; - -/// 路由表。刻意用精确匹配而非正则——接口路径是固定的, -/// 正则只会引入「写错锚点导致前缀误匹配」的风险。 -fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> { - let entry: (Guard, Handler) = match (method, uri) { - ("GET", "/api/admin/apix") => (Guard::Admin, h_admin_apix), - ("POST", "/api/admin/apix/save") => (Guard::Admin, h_admin_apix_save), - ("POST", "/api/admin/apix/delete") => (Guard::Admin, h_admin_apix_delete), - ("POST", "/api/admin/apix/test") => (Guard::Admin, h_admin_apix_test), - ("GET", "/api/v1/apix") => (Guard::User, h_my_apix), - _ => return None, - }; - Some(entry) -} - -/// 本表分发:认领则返回响应状态,不认领返回 `None`(交给下一个插件)。 -pub fn dispatch(r: *mut ngx_http_request_t) -> Option { - let method = http::method_str(r); - let uri = http::uri(r); - - let Some((guard, handler)) = route(&method, &uri) else { - return None; - }; - - Some(run_guarded(r, guard, handler)) -} - -// =========================================================================== -// 管理员 -// =========================================================================== - -/// 管理员:API 代理清单 -fn h_admin_apix(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let port = http::host_port(r); - let list = apix::all(); - http::ok( - r, - json!({ - "items": apix::view_all(&list, &port), - "count": list.len(), - }), - "ok", - ) -} - -/// 管理员:新增/修改一个 API 代理条目 -fn h_admin_apix_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let slug = p.get("slug").cloned().unwrap_or_default(); - let asset = p.get("asset_id").cloned().unwrap_or_default(); - let mode = p.get("mode").cloned().unwrap_or_default(); - - // 参数清单允许两种写法:逗号分隔(`q,format`)或 JSON 数组(`["q"]`) - let params = p.get("params").and_then(|v| { - let v = util::trim(v); - if v.is_empty() { - return None; - } - Some(if let Ok(Value::Array(arr)) = serde_json::from_str::(v) { - arr.into_iter() - .filter_map(|x| x.as_str().map(str::to_owned)) - .collect::>() - } else { - v.split(',').map(str::to_owned).collect::>() - }) - }); - - // 不带 enabled 视为「启用」:新建表单里漏勾是常见手滑,默认值取更安全的一侧 - let enabled = match p.get("enabled") { - Some(v) => util::to_bool(Some(v)), - None => true, - }; - - match apix::upsert( - &slug, - &asset, - Some(&mode), - p.get("base"), - p.get("key"), - p.get("supplier"), - p.get("name"), - p.get("desc"), - params, - enabled, - p.get("note"), - p.get("usage_hint"), - ) { - Ok(item) => { - util::log_notice(&format!("[api] 保存 API 代理 {slug} → {asset} (mode={})", item.mode)); - http::ok( - r, - json!({ - "slug": slug, - "asset_id": item.asset_id, - "mode": item.mode, - "usage_hint": item.usage_hint, - }), - "已保存", - ) - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -/// 管理员:删除一个 API 代理条目 -fn h_admin_apix_delete(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - match apix::remove(p.get("slug").map(String::as_str).unwrap_or("")) { - Ok(slug) => { - util::log_notice(&format!("[api] 删除 API 代理 {slug}")); - http::ok(r, json!({ "slug": slug }), "已删除") - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -/// 管理员:探测代理是否可用(管理台「测试」按钮)。 -/// -/// - vendor 模式:走供应商的**免费预估**接口(`/api/v1/ai/estimate`)—— -/// 不执行资产、不扣费,响应里还带 `current_balance_usd`。内部重定向到 -/// `/_apix_probe/`,复用 `$apix_probe_*` 变量(拼体、换 Key 与真实转发同一套逻辑)。 -/// - passthrough 模式:没有「预估」概念,探针改为 GET 打上游根地址验连通性, -/// 内部重定向到 `/_apixp_probe/`,复用 `$apixp_probe_target` 变量。 -fn h_admin_apix_test(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let slug_raw = http::params(r).get("slug").cloned().unwrap_or_default(); - let slug = match apix::validate_slug(&slug_raw) { - Ok(s) => s, - Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e), - }; - let Some((_b, _k, _a, mode)) = apix::lookup(&slug) else { - return http::fail(r, HTTPStatus::NOT_FOUND, &format!("代理 {slug} 不存在或已停用")); - }; - - let prefix = if mode == config::APIX_MODE_PASSTHRU { - apix::PROBE_PT_PREFIX - } else { - apix::PROBE_URI_PREFIX - }; - let uri = format!("{prefix}{slug}"); - util::log_notice(&format!("[api] 测试 API 代理 {slug} (mode={mode})")); - - // ⚠️ 与 LLM 上游测试同一个坑:internal_redirect 只浅拷贝 ngx_str_t, - // 必须把字节拷进请求池,否则函数返回后 r->uri 全是 NUL(实测踩过)。 - // SAFETY: r 有效;池内存在整个请求生命周期内有效。 - unsafe { - let pool = Pool::from_ngx_pool((*r).pool); - let bytes = uri.as_bytes(); - let data = pool.alloc(bytes.len()) as *mut u8; - if data.is_null() { - return http::fail(r, HTTPStatus::INTERNAL_SERVER_ERROR, "内存分配失败"); - } - core::ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len()); - - let mut u = nginx_sys::ngx_str_t { len: bytes.len(), data }; - let mut args = nginx_sys::ngx_str_t { - len: 0, - data: core::ptr::null_mut(), - }; - nginx_sys::ngx_http_internal_redirect(r, &mut u, &mut args); - } - - Status::NGX_DONE -} - -// =========================================================================== -// 账号态 -// =========================================================================== - -/// 账号态:列出当前可用的 API 代理及调用地址 -fn h_my_apix(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let port = http::host_port(r); - let items: Vec = apix::all() - .into_iter() - .filter(|(_, it)| it.enabled) - .map(|(slug, it)| apix::view(&slug, &it, &port)) - .collect(); - http::ok(r, json!({ "items": items, "count": items.len() }), "ok") -} diff --git a/rust/crates/plugin-apix/src/apix.rs b/rust/crates/plugin-apix/src/apix.rs deleted file mode 100644 index b4fded5..0000000 --- a/rust/crates/plugin-apix/src/apix.rs +++ /dev/null @@ -1,786 +0,0 @@ -//! API 代理:把外部供应商的 API 资产挂到网关下。 -//! -//! 与 LLM 代理(`plugin_llm`)是**同一套下行契约**——都认 `sk_` 那把 Key、 -//! 都吃同一份限流配额、都进同一套用量统计——差别只在上游形态: -//! -//! | | LLM 代理 | API 代理 | -//! | --- | --- | --- | -//! | 上游 | N 家厂商,各有基址与密钥 | 每条记录自带基址与供应商 Key | -//! | URL 第一段 | `slot` → `up:` 里的 `base \n auth` | `slug` → `ax:` 里的 `base \n key \n asset_id` | -//! | 请求体 | 客户端原样带过来 | **由网关从 query 拼**(见下) | -//! -//! ## 为什么请求体必须由网关拼 -//! -//! 供应商侧只有一个收口 `POST /api/v1/ai/request`,资产靠**请求体里的 -//! `asset_id`** 区分。如果让客户端自己带 `asset_id`,管理员就失去了 -//! 「只暴露免费资产」的控制力——任何人都能拿网关的 Key 去刷最贵的那个模型。 -//! 所以资产选择权留在服务端:`slug → asset_id` 由管理员配置, -//! 调用方只能决定参数。 -//! -//! 反过来,参数只能走 **query string**:转发决策在 access 阶段,而请求体 -//! 要到 content 阶段才读得到(见 `plugin_llm::llm` 模块头注释)。nginx 侧靠 -//! `proxy_method POST` + `proxy_set_body $apix_body` 把 GET 变成 -//! 带体的 POST,网关不需要自己实现 upstream。 -//! -//! `data/api-proxies.json` 含供应商真 Key,**敏感级别等同 `users.json`**。 - -use ngx::core::{Pool, Status}; -use ngx::ffi::{ - NGX_HTTP_VAR_CHANGEABLE, NGX_HTTP_VAR_NOCACHEABLE, ngx_conf_t, ngx_http_add_variable, - ngx_http_request_t, ngx_http_variable_value_t, ngx_int_t, ngx_str_t, ngx_uint_t, -}; -use ngx::ngx_string; -use serde_json::{Value, json}; - -use crate::config; -use crate::models::{ApiProxy, ApiProxyStore}; -use crate::store; -use ipass_core::config::LLM_PUBLIC_HOST; -use ipass_core::{http, state, util}; - -// =========================================================================== -// preconfiguration:注册变量 -// =========================================================================== - -/// 注册 API 代理六件套只读变量(gateway preconfiguration 经插件循环调用)。 -/// -/// 前缀 `apix_` 与 `llm_` 同理:nginx 变量名全局唯一,将来接入别的模块时 -/// 才不会撞名。 -/// -/// # Safety -/// `cf` 由 nginx 传入且有效。 -pub unsafe fn preconfig(cf: *mut ngx_conf_t) -> bool { - let mut ax_target = ngx_string!("apix_target"); - let mut ax_host = ngx_string!("apix_host"); - let mut ax_key = ngx_string!("apix_key"); - let mut ax_body = ngx_string!("apix_body"); - let mut ax_ptarget = ngx_string!("apix_probe_target"); - let mut ax_pbody = ngx_string!("apix_probe_body"); - let mut axp_target = ngx_string!("apixp_target"); - let mut axp_host = ngx_string!("apixp_host"); - let mut axp_key = ngx_string!("apixp_key"); - let mut axp_probe = ngx_string!("apixp_probe_target"); - unsafe { - add_var(cf, &raw mut ax_target, VAR_TARGET) - && add_var(cf, &raw mut ax_host, VAR_HOST) - && add_var(cf, &raw mut ax_key, VAR_KEY) - && add_var(cf, &raw mut ax_body, VAR_BODY) - && add_var(cf, &raw mut ax_ptarget, VAR_PROBE_TARGET) - && add_var(cf, &raw mut ax_pbody, VAR_PROBE_BODY) - && add_var(cf, &raw mut axp_target, VAR_PT_TARGET) - && add_var(cf, &raw mut axp_host, VAR_PT_HOST) - && add_var(cf, &raw mut axp_key, VAR_PT_KEY) - && add_var(cf, &raw mut axp_probe, VAR_PT_PROBE) - } -} - -/// 注册一个只读变量,值由 [`var_get`] 按 `slot` 分派计算。 -/// -/// 与 `$proxy_backend_addr` 不同,这六个变量是纯只读的,模块从不主动写, -/// 只有 `proxy_pass` / `proxy_set_body` 求值时触发 get_handler。 -unsafe fn add_var(cf: *mut ngx_conf_t, name: *mut ngx_str_t, slot: usize) -> bool { - // SAFETY: cf 由 nginx 传入;name 指向本函数调用方栈上的 ngx_str_t,注册时被拷贝 - let var = unsafe { - ngx_http_add_variable( - cf, - name, - (NGX_HTTP_VAR_CHANGEABLE | NGX_HTTP_VAR_NOCACHEABLE) as ngx_uint_t, - ) - }; - if var.is_null() { - return false; - } - // SAFETY: var 非空且由本函数独占初始化;data 承载分派标记 - unsafe { - (*var).get_handler = Some(var_get); - (*var).set_handler = None; - (*var).data = slot; - } - true -} - -// =========================================================================== -// 变量分派标记 -// =========================================================================== - -/// `$apix_target`:完整上游 URL(`base + /api/v1/ai/request`) -pub const VAR_TARGET: usize = 1; -/// `$apix_host`:上游 Host 头 -pub const VAR_HOST: usize = 2; -/// `$apix_key`:供应商 API Key(下行那把 `sk_` 到此为止,不往上游传) -pub const VAR_KEY: usize = 3; -/// `$apix_body`:由 query 拼出的调用体 -pub const VAR_BODY: usize = 4; -/// `$apix_probe_target` / `$apix_probe_body`:管理台「测试」用的**免费**预估调用 -pub const VAR_PROBE_TARGET: usize = 5; -pub const VAR_PROBE_BODY: usize = 6; -/// `$apixp_target`:透传模式完整上游 URL(`base + /apixp/ 之后的 URI 尾部`) -pub const VAR_PT_TARGET: usize = 7; -/// `$apixp_host`:透传模式上游 Host 头 -pub const VAR_PT_HOST: usize = 8; -/// `$apixp_key`:透传模式供应商 Key(内部服务无 Key 时下发空,nginx 侧不下发鉴权头) -pub const VAR_PT_KEY: usize = 9; -/// `$apixp_probe_target`:透传模式「测试」用的连通性探针(GET 打上游根地址) -pub const VAR_PT_PROBE: usize = 10; - -/// shm payload 内部分隔符:base / 供应商 Key / asset_id 之间。 -/// -/// base 是 URL、key 与 asset_id 都是 token 类字符串,都不可能含换行。 -pub const PAYLOAD_SEP: char = '\n'; - -/// 内部探测前缀(管理台「测试」按钮经内部重定向落到 `/_apix_probe/`) -pub const PROBE_URI_PREFIX: &str = "/_apix_probe/"; -const PROBE_PREFIX: &str = PROBE_URI_PREFIX; -/// 对外的 API 代理前缀 -const PUB_PREFIX: &str = "/apix/"; -/// 透传模式(passthrough)对外的 API 代理前缀:`/apixp//`。 -/// -/// 与 vendor 的 `/apix/` 拆成**两个独立 location**,根本原因是 vendor 的 -/// `/apix/` location 在 nginx 层强制了 `proxy_method POST` + `proxy_set_body` -/// + `Content-Type: application/json` + `proxy_pass_request_headers off`—— -/// 这些 location 级指令无法按请求级 shm 的 mode 动态切换。透传必须原样转发 -/// 客户端的方法/请求体/Content-Type(OCR 的 `/ocr` 就是 `multipart/form-data`), -/// 所以只能另起一个不施加上述约束的 `/apixp/` location,零风险保留 `/apix/` 行为。 -pub const PUB_PT_PREFIX: &str = "/apixp/"; -/// 透传模式内部探测前缀(管理台「测试」走 GET 打上游根地址验连通性)。 -pub const PROBE_PT_PREFIX: &str = "/_apixp_probe/"; - -// =========================================================================== -// 校验 -// =========================================================================== - -/// slug:小写字母/数字开头,其后可含短横线、下划线。与 -/// `plugin_llm::upstreams::validate_slot` 同一套规则(保持一致,管理员不用记两套), -/// 只把错误文案换成「slug」。 -pub fn validate_slug(raw: &str) -> Result { - let s = util::trim(raw).to_ascii_lowercase(); - if s.is_empty() { - return Err("缺少 slug 参数".to_owned()); - } - if s.len() > config::APIX_SLUG_MAX_LEN { - return Err(format!("slug 长度不能超过 {} 个字符", config::APIX_SLUG_MAX_LEN)); - } - let bytes = s.as_bytes(); - let head_ok = matches!(bytes[0], b'a'..=b'z' | b'0'..=b'9'); - let tail_ok = bytes[1..].iter().all(|&b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_')); - if !head_ok || !tail_ok { - return Err("slug 只能是小写字母、数字、短横线或下划线,且不能以符号开头".to_owned()); - } - Ok(s) -} - -/// 供应商基址:`http(s)://host`,允许尾随路径(供应商挂在子路径下的情况)。 -pub fn validate_base(raw: &str) -> Result { - let s = util::trim(raw).trim_end_matches('/').to_owned(); - if s.is_empty() { - return Err("缺少 base 参数".to_owned()); - } - if s.len() > config::APIX_URL_MAX_LEN { - return Err(format!("base 长度不能超过 {} 个字符", config::APIX_URL_MAX_LEN)); - } - let rest = if let Some(r) = s.strip_prefix("https://") { - r - } else if let Some(r) = s.strip_prefix("http://") { - r - } else { - return Err("base 必须以 http:// 或 https:// 开头,例如 https://api.example.com".to_owned()); - }; - if rest.is_empty() || rest.split('/').next().unwrap_or("").is_empty() { - return Err("base 缺少主机名".to_owned()); - } - if rest.chars().any(char::is_whitespace) || rest.contains('@') { - return Err("base 不允许包含空格或用户信息(user@host)".to_owned()); - } - Ok(s) -} - -/// asset_id:供应商那边的资产标识。宽进——各供应商的命名规则不同, -/// 这里只挡空白与控制字符(它要出现在 JSON 与 HTML 里)。 -fn validate_asset(raw: &str) -> Result { - let s = util::trim(raw).to_owned(); - if s.is_empty() { - return Err("缺少 asset_id 参数".to_owned()); - } - if s.chars().count() > config::APIX_ASSET_MAX_LEN { - return Err(format!("asset_id 超过 {} 个字符", config::APIX_ASSET_MAX_LEN)); - } - if s.chars().any(char::is_whitespace) || s.chars().any(char::is_control) { - return Err("asset_id 不能包含空格或控制字符".to_owned()); - } - Ok(s) -} - -fn check_params(raw: &[String]) -> Result, String> { - if raw.len() > config::APIX_PARAM_MAX { - return Err(format!("单个代理最多声明 {} 个参数", config::APIX_PARAM_MAX)); - } - let mut out: Vec = Vec::new(); - for item in raw { - let p = util::trim(item); - if p.is_empty() { - continue; - } - if p.chars().count() > config::APIX_NAME_MAX_LEN { - return Err(format!("参数名「{p}」超过 {} 个字符", config::APIX_NAME_MAX_LEN)); - } - if p.chars().any(char::is_whitespace) || p.chars().any(char::is_control) { - return Err(format!("参数名「{p}」不能包含空格或控制字符")); - } - if !out.iter().any(|x| x == p) { - out.push(p.to_owned()); - } - } - Ok(out) -} - -// =========================================================================== -// 共享内存同步(写穿 / 装载) -// =========================================================================== - -fn payload_of(item: &ApiProxy) -> String { - format!( - "{}{PAYLOAD_SEP}{}{PAYLOAD_SEP}{}{PAYLOAD_SEP}{}", - item.base, item.key, item.asset_id, item.mode - ) -} - -/// 拆 payload:`base \n key \n asset_id \n mode` -pub fn split_payload(payload: &str) -> (&str, &str, &str, &str) { - let mut it = payload.splitn(4, PAYLOAD_SEP); - let base = it.next().unwrap_or(""); - let key = it.next().unwrap_or(""); - let asset = it.next().unwrap_or(""); - let mode = it.next().unwrap_or(""); - (base, key, asset, mode) -} - -/// 把一条代理同步进共享内存。**所有写操作之后必须调用它**。 -/// -/// 停用 = 删除(与 `upstreams::sync_shm` 同一手法):热路径「查不到 = 不可用」, -/// 这样停用即刻生效且不留残留。 -pub fn sync_shm(slug: &str, store: &ApiProxyStore) { - let Some(item) = store.items.get(slug) else { - state::remove(config::KEY_AX_PREFIX, slug.as_bytes()); - return; - }; - if !item.enabled { - state::remove(config::KEY_AX_PREFIX, slug.as_bytes()); - return; - } - let payload = payload_of(item); - if !state::put(config::KEY_AX_PREFIX, slug.as_bytes(), payload.as_bytes(), 0) { - util::log_err(&format!( - "[apix] 代理配置写共享内存失败 slug={slug} —— 该代理会 404(共享内存可能已满)" - )); - } -} - -/// 全量重载代理表(启动装载 / 供应商凭据变更后)。返回装载条数。 -pub fn reload_shm() -> usize { - state::clear_prefix(config::KEY_AX_PREFIX); - let data = store::read_api_proxies(); - let mut expected = 0; - let mut loaded = 0; - for (slug, item) in &data.items { - if !item.enabled { - continue; - } - expected += 1; - let payload = payload_of(item); - if state::put(config::KEY_AX_PREFIX, slug.as_bytes(), payload.as_bytes(), 0) { - loaded += 1; - } - } - if loaded != expected { - util::log_err(&format!( - "[apix] 代理装载不全:{loaded}/{expected} —— 有代理会 404" - )); - } - loaded -} - -/// 热路径查表:`slug → (base, 供应商Key, asset_id, mode)` -/// -/// vendor 模式要求 `asset_id` 非空——它是拼调用体的依据;passthrough 模式 -/// `asset_id` 留空也合法,上游路径由 URL 尾部(见 [`slug_of`] 的 `tail`)决定。 -pub fn lookup(slug: &str) -> Option<(String, String, String, String)> { - let payload = state::get(config::KEY_AX_PREFIX, slug.as_bytes())?.0; - let (base, key, asset, mode) = split_payload(&payload); - if mode != config::APIX_MODE_PASSTHRU && asset.is_empty() { - return None; - } - Some(( - base.to_owned(), - key.to_owned(), - asset.to_owned(), - mode.to_owned(), - )) -} - -// =========================================================================== -// 请求解析 -// =========================================================================== - -/// 取 URL 里的 slug,并返回 URI 尾部与「是否落在透传前缀下」。 -/// -/// 四个前缀都认:vendor 对外 `/apix/` 与内部探针 `/_apix_probe/`、 -/// passthrough 对外 `/apixp/` 与内部探针 `/_apixp_probe/`。 -/// 返回三元组 `(slug, tail, is_passthru_path)`: -/// - `tail`:slug 之后的 URI 片段(含前导 `/`,可能为空)。vendor 模式恒为空 -/// (路径写死 `/api/v1/ai/request`);passthrough 模式 = 真正转发到上游的 -/// 路径尾部(如 `/ocr`、`/ocr/url`、`/ocr/base64`)。 -/// - `is_passthru_path`:当前请求是否落在透传前缀下,供 handler 校验 -/// 「路径前缀」与「条目 mode」一致(vendor slug 不能走 `/apixp/`,反之亦然)。 -/// -/// 与 `llm::slot_of` 对 `/_upstream_test` 的处理是同一个手法:对外出口与内部 -/// 探测共用同一套「取 slug → 拼目标 → 换 Key」逻辑。 -pub fn slug_of(r: *mut ngx_http_request_t) -> Option<(String, String, bool)> { - let uri = http::uri(r); - let (rest, is_pt) = if let Some(rest) = uri.strip_prefix(PUB_PT_PREFIX) { - (rest, true) - } else if let Some(rest) = uri.strip_prefix(PROBE_PT_PREFIX) { - (rest, true) - } else if let Some(rest) = uri.strip_prefix(PUB_PREFIX) { - (rest, false) - } else if let Some(rest) = uri.strip_prefix(PROBE_PREFIX) { - (rest, false) - } else { - return None; - }; - let slug = rest.split('/').next().unwrap_or(""); - if slug.is_empty() { - return None; - } - let slug = slug.to_ascii_lowercase(); - // tail = slug 之后的全部内容(含前导 '/',可能为空) - let tail = rest[slug.len()..].to_owned(); - Some((slug, tail, is_pt)) -} - -/// 从 `https://api.example.com:443` 取出 `api.example.com:443` -fn host_of(base: &str) -> &str { - let rest = match base.find("://") { - Some(i) => &base[i + 3..], - None => base, - }; - match rest.find('/') { - Some(i) => &rest[..i], - None => rest, - } -} - -/// JSON 字符串字面量(含两侧引号)。 -/// -/// 自己拼而不走 `serde_json` 是因为输入来自 query string,中文与特殊字符 -/// 都会被 percent-encode 过一遍,这里必须按 JSON 规则重新转义—— -/// 直接 `format!("\"{v}\"")` 遇到 `"` 或 `\` 就拼出非法 JSON, -/// 上游报 400 而从网关日志完全看不出是拼体的问题。 -fn json_quote(v: &str) -> String { - let mut out = String::with_capacity(v.len() + 2); - out.push('"'); - for c in v.chars() { - match c { - '"' => out.push_str("\\\""), - '\\' => out.push_str("\\\\"), - '\n' => out.push_str("\\n"), - '\r' => out.push_str("\\r"), - '\t' => out.push_str("\\t"), - c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)), - c => out.push(c), - } - } - out.push('"'); - out -} - -/// 单个参数值截断(bytes 级,避免把几 MB 的垃圾塞进调用体) -fn clip(v: &str) -> &str { - if v.len() <= config::APIX_VAL_MAX { - return v; - } - // 截断到字符边界,否则 `\u{fffd}` 会污染整个 JSON - let mut end = config::APIX_VAL_MAX; - while end > 0 && !v.is_char_boundary(end) { - end -= 1; - } - &v[..end] -} - -/// 由 query 拼出供应商的调用体。 -/// -/// ```text -/// ?q=nginx&format=json → {"asset_id":"searxng-search","call_data":{"q":"nginx","format":"json"}} -/// ?prompt=你好 → {"asset_id":"minimax-m3","prompt":"你好","call_data":{}} -/// ``` -/// -/// `prompt` 单独提到顶层:供应商的 model 类资产(MiniMax-M3 等)把它当必填字段, -/// 塞进 `call_data` 会被当成未知参数。其余参数一律进 `call_data`。 -pub fn build_body(asset_id: &str, args: &[(String, String)]) -> String { - let mut call = String::new(); - let mut prompt: Option<&str> = None; - let mut n = 0; - for (k, v) in args { - if k.is_empty() || n >= config::APIX_ARG_MAX { - continue; - } - if k == "prompt" { - prompt = Some(v); - continue; - } - if !call.is_empty() { - call.push(','); - } - call.push_str(&json_quote(k)); - call.push(':'); - call.push_str(&json_quote(clip(v))); - n += 1; - } - - let mut body = String::with_capacity(128 + call.len()); - body.push_str("{\"asset_id\":"); - body.push_str(&json_quote(asset_id)); - if let Some(p) = prompt { - body.push_str(",\"prompt\":"); - body.push_str(&json_quote(clip(p))); - } - body.push_str(",\"call_data\":{"); - body.push_str(&call); - body.push_str("}}"); - body -} - -fn build_body_from_request(r: *mut ngx_http_request_t, asset_id: &str) -> String { - let args = util::parse_kv(&http::args_raw(r)); - build_body(asset_id, &args) -} - -// =========================================================================== -// 变量 get_handler -// =========================================================================== - -/// 在请求内存池里分配字符串并写进变量槽。 -/// -/// # Safety -/// `r` / `v` 必须由 nginx 传入且在本调用期间有效。 -unsafe fn set_value( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - value: &str, -) -> ngx_int_t { - let pool = Pool::from_ngx_pool((*r).pool); - let bytes = value.as_bytes(); - let data = pool.alloc(bytes.len()) as *mut u8; - if data.is_null() { - (*v).set_not_found(1); - return Status::NGX_ERROR.into(); - } - core::ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len()); - (*v).data = data; - (*v).set_len(bytes.len() as _); - (*v).set_valid(1); - (*v).set_no_cacheable(1); - (*v).set_not_found(0); - Status::NGX_OK.into() -} - -/// 六个 API 代理变量共用一个 get_handler,靠 `var.data` 分派。 -pub extern "C" fn var_get( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - data: usize, -) -> ngx_int_t { - // SAFETY: r/v 由 nginx 传入且有效;slug_of 只读请求结构 - unsafe { - let resolved = (|| -> Option { - let (slug, tail, _is_pt) = slug_of(r)?; - let (base, key, asset, mode) = lookup(&slug)?; - Some(match data { - VAR_TARGET => format!("{base}{}", config::APIX_REQUEST_PATH), - VAR_PROBE_TARGET => format!("{base}{}", config::APIX_ESTIMATE_PATH), - VAR_HOST => host_of(&base).to_owned(), - VAR_KEY => key, - VAR_BODY => build_body_from_request(r, &asset), - VAR_PROBE_BODY => { - // 预估是**免费**的,且不真正执行资产——用它当连通性探针, - // 响应里的 current_balance_usd 顺带把供应商余额带回来 - format!("{{\"asset_id\":{},\"max_tokens\":1}}", json_quote(&asset)) - } - // —— 透传模式变量:仅当条目本身是 passthrough 才有效 —— - VAR_PT_TARGET if mode == config::APIX_MODE_PASSTHRU => { - // 原样转发:上游 URL = base + URL 尾部(如 base + /ocr)。 - // 变量形式 proxy_pass 不自动追加 query(与 LLM 同约束), - // 所以通用 REST 出口必须把 query 也拼进去。 - let args = http::args_raw(r); - let args = std::str::from_utf8(&args).unwrap_or(""); - if args.is_empty() { - format!("{base}{tail}") - } else { - format!("{base}{tail}?{args}") - } - } - VAR_PT_HOST if mode == config::APIX_MODE_PASSTHRU => host_of(&base).to_owned(), - VAR_PT_KEY if mode == config::APIX_MODE_PASSTHRU => key, - VAR_PT_PROBE if mode == config::APIX_MODE_PASSTHRU => { - // 透传探针:GET 打上游根地址,验证连通性(不真正执行任何资产) - format!("{base}/") - } - _ => return None, - }) - })(); - - match resolved { - Some(value) => set_value(r, v, &value), - None => { - (*v).set_not_found(1); - (*v).set_valid(0); - Status::NGX_ERROR.into() - } - } - } -} - -// =========================================================================== -// 查询 / 写入 -// =========================================================================== - -pub fn all() -> Vec<(String, ApiProxy)> { - store::read_api_proxies() - .items - .into_iter() - .collect() -} - -/// 仅启用(`enabled = true`)的代理条目。 -/// -/// `plugin-apix::api::h_list` 与 gateway 的 `decorate_me` 都走这个: -/// 给同事/自己看的清单只该含「现在能调」的代理,停用的不该暴露, -/// 否则列表里出现一行「调就 404」会让人误以为是网关坏掉。 -pub fn enabled() -> Vec<(String, ApiProxy)> { - all().into_iter().filter(|(_, it)| it.enabled).collect() -} - -/// 客户端应当使用的调用地址(base,不带 query)。端口由调用方从请求 Host 头取, -/// **不要写死**(部署在 88 时要带 `:88`,放在 443 反代后面时不能带)。 -/// query 参数由前端按 `params` 拼一次(避免后端与前端各拼一次导致 `?q=?q=`)。 -/// -/// `mode` 决定前缀:vendor → `/apix/`,passthrough → `/apixp/` -/// (passthrough 后续还能再追路径尾部,如 `/apixp/ocr/ocr`)。 -pub fn call_url(slug: &str, port: &str, mode: &str) -> String { - let prefix = if mode == config::APIX_MODE_PASSTHRU { - PUB_PT_PREFIX - } else { - PUB_PREFIX - }; - format!("https://{LLM_PUBLIC_HOST}{port}{prefix}{slug}") -} - -/// 新增或覆盖一个代理条目。 -/// -/// `params = None` 表示保持原值(只改名字时不必回填参数清单)。 -/// `base` / `key` / `supplier` 同理:`None` 或空串都表示「保留原值」—— -/// 这样编辑时不必把密钥再抄一遍。新建时若填了空串仍为空,则报错 -/// (没有基址与密钥无法转发)。 -pub fn upsert( - slug_raw: &str, - asset_raw: &str, - mode_raw: Option<&String>, - base_raw: Option<&String>, - key_raw: Option<&String>, - supplier_raw: Option<&String>, - name: Option<&String>, - desc: Option<&String>, - params: Option>, - enabled: bool, - note: Option<&String>, - usage_hint_raw: Option<&String>, -) -> Result { - let slug = validate_slug(slug_raw)?; - - // 代理模式:vendor(默认)/ passthrough,其余值一律拒绝 - let mode = match mode_raw { - Some(m) => { - let m = util::trim(m).to_ascii_lowercase(); - if m.is_empty() { - config::APIX_MODE_VENDOR.to_owned() - } else if m == config::APIX_MODE_VENDOR || m == config::APIX_MODE_PASSTHRU { - m - } else { - return Err(format!( - "不支持的代理模式:{m}(仅支持 vendor / passthrough)" - )); - } - } - None => config::APIX_MODE_VENDOR.to_owned(), - }; - - // vendor 模式 asset_id 必填(拼体依据);passthrough 模式可留空 - // (上游路径由 URL 尾部决定),仅做与 vendor 同样的卫生检查。 - let asset_id = if mode == config::APIX_MODE_PASSTHRU { - let s = util::trim(asset_raw); - if s.chars().any(char::is_whitespace) || s.chars().any(char::is_control) { - return Err("asset_id 不能包含空格或控制字符".to_owned()); - } - if s.chars().count() > config::APIX_ASSET_MAX_LEN { - return Err(format!("asset_id 超过 {} 个字符", config::APIX_ASSET_MAX_LEN)); - } - s.to_owned() - } else { - validate_asset(asset_raw)? - }; - - let params = match params { - Some(list) => Some(check_params(&list)?), - None => None, - }; - - // base / key:空串视为「保留原值」,交给下方闭包按 existing 决定 - let new_base = match base_raw { - Some(b) => { - let b = util::trim(b); - if b.is_empty() { None } else { Some(validate_base(&b)?) } - } - None => None, - }; - let new_key = match key_raw { - Some(k) => { - let k = util::trim(k).to_owned(); - if k.is_empty() { - None - } else { - if k.chars().count() > config::APIX_KEY_MAX_LEN { - return Err(format!("供应商 Key 超过 {} 个字符", config::APIX_KEY_MAX_LEN)); - } - Some(k) - } - } - None => None, - }; - - let key = slug.clone(); - let (item, snapshot) = store::update_api_proxies(move |data| { - let existing = data.items.get(&key).cloned(); - let base = new_base - .or_else(|| existing.as_ref().map(|e| e.base.clone())) - .unwrap_or_default(); - let key_val = new_key - .or_else(|| existing.as_ref().map(|e| e.key.clone())) - .unwrap_or_default(); - // 新建必填校验:vendor 必须 base + key 都填;passthrough 仅 base 必填, - // key 可空(内部服务免鉴权,下发时 nginx 空串语义 = 不发该头)。 - // 这是 1.0.20 现场踩到的坑:原校验统一要求「基址与密钥」,导致管理台 - // 提交 passthrough + 空 Key 直接 400,handler 实际是放行的。 - if existing.is_none() { - if base.is_empty() { - return Err("新建代理必须提供供应商基址".to_owned()); - } - if key_val.is_empty() && mode == config::APIX_MODE_VENDOR { - return Err("vendor 模式必须提供供应商 Key(passthrough 可留空)".to_owned()); - } - } - let supplier = match supplier_raw { - Some(s) => util::trim(s).chars().take(config::APIX_NAME_MAX_LEN).collect(), - None => existing.as_ref().map(|e| e.supplier.clone()).unwrap_or_default(), - }; - let name = match name { - Some(n) => util::trim(n).chars().take(config::APIX_NAME_MAX_LEN).collect(), - None => existing.as_ref().map(|e| e.name.clone()).unwrap_or_default(), - }; - let desc = match desc { - Some(d) => util::trim(d).chars().take(config::APIX_DESC_MAX_LEN).collect(), - None => existing.as_ref().map(|e| e.desc.clone()).unwrap_or_default(), - }; - let note = match note { - Some(n) => util::trim(n).to_owned(), - None => existing.as_ref().map(|e| e.note.clone()).unwrap_or_default(), - }; - let usage_hint = match usage_hint_raw { - Some(h) => { - let h = util::trim(h); - if h.is_empty() { - String::new() - } else { - // 截断到 1KB(够写 3-5 个端点说明)。机器可读文本通常几十到几百字节, - // 给 4 倍冗余。过长截断而非拒绝——管理员可以粘贴 Swagger 长片段。 - h.chars().take(1024).collect() - } - } - None => existing.as_ref().map(|e| e.usage_hint.clone()).unwrap_or_default(), - }; - let params = match params { - Some(p) => p, - None => existing.as_ref().map(|e| e.params.clone()).unwrap_or_default(), - }; - let created_at = existing - .as_ref() - .map(|e| e.created_at) - .filter(|&t| t > 0) - .unwrap_or_else(util::now); - - let item = ApiProxy { - asset_id, - supplier, - base, - key: key_val, - mode, - name, - desc, - params, - enabled, - note, - usage_hint, - created_at, - }; - data.items.insert(key.clone(), item.clone()); - Ok((item, data.clone())) - })?; - - sync_shm(&slug, &snapshot); - Ok(item) -} - -pub fn remove(slug_raw: &str) -> Result { - let slug = validate_slug(slug_raw)?; - let key = slug.clone(); - store::update_api_proxies(move |data| { - if data.items.remove(&key).is_none() { - return Err(format!("代理 {key} 不存在")); - } - Ok(()) - })?; - state::remove(config::KEY_AX_PREFIX, slug.as_bytes()); - Ok(slug) -} - -// =========================================================================== -// 对外投影 -// =========================================================================== - -/// 转成管理台可见的字段。 -/// -/// **绝不回显完整供应商 Key**——与 `upstreams::view` 同一理由:管理台页面会被 -/// 截图、被浏览器插件读取,回显真 Key 等于把供应商余额摆在一个 HTML 里。 -/// `url` 只给 base 路径,query 参数由前端按 `params` 拼一次。 -pub fn view(slug: &str, item: &ApiProxy, port: &str) -> Value { - json!({ - "slug": slug, - "asset_id": item.asset_id, - "mode": item.mode, - "supplier": item.supplier, - "base": item.base, - "name": item.name, - "desc": item.desc, - "params": item.params, - "enabled": item.enabled, - "key_set": !item.key.is_empty(), - "key_mask": if item.key.is_empty() { "-".to_owned() } else { util::mask_key(&item.key) }, - "note": item.note, - "usage_hint": item.usage_hint, - "created_at": item.created_at, - "created_at_text": util::fmt_time(item.created_at), - "url": call_url(slug, port, &item.mode), - }) -} - -pub fn view_all(list: &[(String, ApiProxy)], port: &str) -> Vec { - list.iter().map(|(slug, it)| view(slug, it, port)).collect() -} diff --git a/rust/crates/plugin-apix/src/config.rs b/rust/crates/plugin-apix/src/config.rs deleted file mode 100644 index dc64bfe..0000000 --- a/rust/crates/plugin-apix/src/config.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! API 代理(apix)策略常量与 shm 键前缀(自内核 config.rs 迁入;改策略只改这里)。 -//! -//! 与 LLM 代理的关系:同一把 `sk_` 下行 Key、同一套限流与统计(内核 -//! `ipass_core::auth` + `plugin_llm::stats`),但每条 apix 记录自带 -//! 「基址 + 供应商 Key + 资产 id」——不同供应商互不影响,slug 只映射到 -//! `asset_id`。 - -/// shm 键前缀:slug → `base \n 供应商Key \n asset_id`。 -/// -/// 三行塞一个键是刻意的:热路径只查一次表就拿到转发所需全部信息。 -/// 代价是改某条凭据要重新同步该条目(条目数是个位数,无所谓)。 -pub const KEY_AX_PREFIX: &[u8] = b"ax:"; - -/// 供应商「调用资产」路径(`POST {base}{path}`,body = `{"asset_id":..,"call_data":{..}}`)。 -/// 每条 apix 记录都按这个路径转发——它定义了该供应商接口所遵循的协议。 -pub const APIX_REQUEST_PATH: &str = "/api/v1/ai/request"; -/// 供应商「费用预估」路径。**免费**,且响应里带余额——管理台的 -/// 「测试」按钮走它:既能验 Key 通不通,又能看到还剩多少余额,不花一分钱。 -pub const APIX_ESTIMATE_PATH: &str = "/api/v1/ai/estimate"; - -/// slug 长度上限 -pub const APIX_SLUG_MAX_LEN: usize = 32; -/// 供应商基址长度上限 -pub const APIX_URL_MAX_LEN: usize = 200; -/// 展示名 / 说明长度上限 -pub const APIX_NAME_MAX_LEN: usize = 64; -pub const APIX_DESC_MAX_LEN: usize = 200; -/// asset_id 长度上限 -pub const APIX_ASSET_MAX_LEN: usize = 64; -/// 单条目声明的参数个数上限 -pub const APIX_PARAM_MAX: usize = 16; -/// 单次调用允许的 query 参数个数上限(挡住拼超长 URL 刷共享内存) -pub const APIX_ARG_MAX: usize = 32; -/// 单个参数值长度上限(bytes) -pub const APIX_VAL_MAX: usize = 4096; -/// 供应商 Key 长度上限 -pub const APIX_KEY_MAX_LEN: usize = 128; - -/// 用量统计里 API 代理的 slot 前缀。 -/// -/// 与 LLM 的 slot 刻意区分开:`uds::<日窗>` 是**同一片键空间**, -/// 而 LLM slot 的字符集不含 `/`(见 `plugin_llm::upstreams::validate_slot`), -/// 所以 `api/` 绝不会和 LLM 上游撞车,用量页能自然分成两组。 -pub const APIX_STATS_PREFIX: &str = "api/"; - -/// 代理模式:vendor(默认)= 单一供应商收口;passthrough = 透传内部 REST 服务。 -pub const APIX_MODE_VENDOR: &str = "vendor"; -pub const APIX_MODE_PASSTHRU: &str = "passthrough"; diff --git a/rust/crates/plugin-apix/src/handlers.rs b/rust/crates/plugin-apix/src/handlers.rs deleted file mode 100644 index c24f9ee..0000000 --- a/rust/crates/plugin-apix/src/handlers.rs +++ /dev/null @@ -1,105 +0,0 @@ -//! API 代理热路径(access 阶段):鉴权 → 查代理 → 放行。 -//! -//! 与 plugin-llm 的 `LlmHandler` 是同一套下行契约(同一把 `sk_` Key、 -//! 同一份限流配额——都在内核 `ipass_core::auth`),差别只在查哪张表、 -//! 以及**请求体由网关拼**(`apix::build_body`)——资产选择权必须留在服务端, -/// 否则客户端能自己挑最贵的模型刷。 - -use ngx::core::Status; -use ngx::ffi::ngx_http_request_t; -use ngx::http::{HTTPStatus, HttpPhase, HttpRequestHandler, Request}; - -use crate::apix; -use crate::config; -use ipass_core::{auth, http, loc, util}; -use ipass_core::loc::Switch; - -/// API 代理 access handler。location 开关:`ipass_apix on`(Switch::Apix)。 -pub struct ApiProxyHandler; - -impl HttpRequestHandler for ApiProxyHandler { - const PHASE: HttpPhase = HttpPhase::Access; - type Output = Status; - - fn handler(request: &mut Request) -> Self::Output { - let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t; - if !loc::enabled(r, Switch::Apix) { - return Status::NGX_DECLINED; - } - - let Some((slug, _tail, is_pt_path)) = apix::slug_of(r) else { - return http::fail( - r, - HTTPStatus::BAD_REQUEST, - "路径缺少代理名,正确形式:/apix/<代理名>?<参数> 或 /apixp/<代理名>/<尾部>", - ); - }; - - let Some(key) = auth::downstream_key(r) else { - return http::fail( - r, - HTTPStatus::UNAUTHORIZED, - "缺少 API Key,请在 Authorization: Bearer 或 x-api-key 中携带", - ); - }; - - let Some((username, _allowed, rpm, daily)) = auth::identity(&key) else { - util::log_warn(&format!( - "[apix] API Key 校验失败 slug={slug} client={}", - http::remote_addr(r) - )); - return http::fail(r, HTTPStatus::UNAUTHORIZED, "API Key 无效或已失效"); - }; - - // 代理的**可见性**由管理员的启用/停用决定,不再叠一层账号白名单: - // 白名单是给「一家厂商一个 slot」设计的,这里再套一层会让 - // 「管理员明明启用了、某账号却 403」变得难以解释。 - let Some((_base, supplier_key, _asset, mode)) = apix::lookup(&slug) else { - return http::fail( - r, - HTTPStatus::NOT_FOUND, - &format!("代理 {slug} 不存在或已停用"), - ); - }; - - // 路径前缀必须与条目 mode 一致:vendor slug 不能走 `/apixp/`, - // passthrough slug 也不能走 `/apix/`。两者 nginx location 的强约束不同, - // 混用会继承错误的 proxy_method / Content-Type 导致转发失败。 - let is_pt_entry = mode == config::APIX_MODE_PASSTHRU; - if is_pt_path != is_pt_entry { - return http::fail( - r, - HTTPStatus::NOT_FOUND, - &format!("代理 {slug} 的访问路径与代理模式不匹配"), - ); - } - - // passthrough 模式:内部服务可能不需要供应商 Key,放行空 Key(不下发鉴权头)。 - // vendor 模式:供应商 Key 必填,空则无法转发(保持原行为)。 - if !is_pt_entry && supplier_key.is_empty() { - util::log_err(&format!( - "[apix] 供应商 Key 未配置,slug={slug} 无法转发 —— 请到管理台「API 代理」填写" - )); - return http::fail( - r, - HTTPStatus::SERVICE_UNAVAILABLE, - "供应商 API Key 未配置,请联系管理员", - ); - } - - // SAFETY: r 由 nginx 传入且有效 - if let Some(resp) = unsafe { auth::check_quota(r, &username, rpm, daily) } { - return resp; - } - - // 用量统计。slot 带 `api/` 前缀,与 LLM 上游在同一片键空间里天然分组 - // (键空间归 plugin-llm 的 stats,记账原语在内核 usage——apix 只是租户) - plugin_llm::stats::record( - &username, - &format!("{}{}", config::APIX_STATS_PREFIX, slug), - util::now(), - ); - - Status::NGX_DECLINED - } -} diff --git a/rust/crates/plugin-apix/src/lib.rs b/rust/crates/plugin-apix/src/lib.rs deleted file mode 100644 index 942251c..0000000 --- a/rust/crates/plugin-apix/src/lib.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! plugin-apix —— API 代理插件。 -//! -//! 把外部 **API 供应商**的资产挂到网关下,复用 LLM 的 `sk_` 鉴权 + 配额 + -//! 用量统计(slot 带 `api/` 前缀,天然归组)。数据文件 -//! `data/api-proxies.json`(含供应商真 Key,敏感级别等同 users.json)。 -//! -//! ## 装配点(gateway) -//! -//! * `preconfig` —— 注册 `$apix_target` / `$apix_host` / `$apix_key` / -//! `$apix_body` / `$apix_probe_target` / `$apix_probe_body` 六件套 -//! * `postconfig` —— 注册 ApiProxyHandler(access 阶段) -//! * `zone_load` —— 全量装载 `ax:` 代理表 -//! * `dispatch_api` —— 认领 apix 管理端点与账号态端点 -//! -//! ## 依赖说明 -//! -//! 下行鉴权 / 配额在内核 `ipass_core::auth`;用量记账入口在 -//! `plugin_llm::stats`(apix 与 LLM 共用同一片键空间,单一写入口防漂移)。 - -pub mod api; -pub mod apix; -pub mod config; -pub mod handlers; -pub mod models; -pub mod store; - -use ngx::core::Status; -use ngx::ffi::{ngx_conf_t, ngx_http_request_t}; - -use ipass_core::plugin::Plugin; - -pub struct ApixPlugin; - -impl Plugin for ApixPlugin { - fn name(&self) -> &'static str { - "apix" - } - - fn preconfig(&self, cf: *mut ngx_conf_t) -> bool { - // SAFETY: cf 由 nginx 传入且有效 - unsafe { apix::preconfig(cf) } - } - - fn postconfig(&self, cf: *mut ngx_conf_t) -> bool { - // SAFETY: cf 由 nginx 传入且有效 - ngx::http::add_phase_handler::(unsafe { &mut *cf }).is_ok() - } - - fn dispatch_api(&self, r: *mut ngx_http_request_t) -> Option { - api::dispatch(r) - } - - fn zone_load(&self) -> usize { - apix::reload_shm() - } -} diff --git a/rust/crates/plugin-apix/src/models.rs b/rust/crates/plugin-apix/src/models.rs deleted file mode 100644 index 4e93966..0000000 --- a/rust/crates/plugin-apix/src/models.rs +++ /dev/null @@ -1,100 +0,0 @@ -//! API 代理持久化模型(`api-proxies.json`:以 slug 为 key)。 -//! -//! 字段名与旧版 `data/api-proxies.json` **逐字对应**,以便新版直接接管旧数据文件。 - -use serde::{Deserialize, Serialize}; -use std::collections::BTreeMap; - -/// API 代理表(`api-proxies.json`)。 -/// -/// 与 `llm-upstreams.json` 同构:每条记录 = 一个供应商(自带基址与密钥), -/// 由 `slug` 区分。调用方访问 `/apix/?参数=值`,网关用本记录里的密钥 -/// 转发到供应商、并把响应原样返回。不同供应商彼此独立、互不耦合。 -/// -/// `key` 是供应商真 Key,**敏感级别等同 `users.json`**(data/ 已 gitignore)。 -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct ApiProxyStore { - /// slug → 代理映射 - #[serde(default)] - pub items: BTreeMap, -} - -impl Default for ApiProxyStore { - fn default() -> Self { - Self { items: BTreeMap::new() } - } -} - -/// 单个 API 代理条目:指向某一家供应商的一个可调用的资产。 -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct ApiProxy { - /// 供应商侧的资产 id(如 `searxng-search`),决定具体调用哪个资产。 - /// **vendor 模式必填**;passthrough 模式用不到(上游路径由 URL 尾部决定),可留空。 - pub asset_id: String, - /// 供应商名称(仅展示用,管理员自填,例如 `provider-x` / 客户自定) - #[serde(default)] - pub supplier: String, - /// 供应商基址,如 `https://api.example.com` - #[serde(default)] - pub base: String, - /// 供应商 API Key(`ak_...`)。留空 = 未配置,热路径直接 503。 - /// **敏感**:写盘与回显都按密钥处理,管理台只回显掩码。 - /// passthrough 模式下若上游不需要鉴权可留空(不下发任何鉴权头)。 - #[serde(default)] - pub key: String, - /// 代理模式: - /// - `vendor`(默认):单一供应商收口 `POST {base}/api/v1/ai/request`, - /// 请求体由网关按 `asset_id` 拼装(见 `apix.rs`)。 - /// - `passthrough`:透传模式,原样转发客户端的方法/请求体/Content-Type, - /// 上游路径 = `{base} + /apixp/ 之后的 URI 尾部`,用于 OCR / 文生图 / - /// TTS 这类「多端点、多 body、无 asset 市场」的内部 REST 服务。 - #[serde(default = "default_mode")] - pub mode: String, - /// 展示名(中文) - #[serde(default)] - pub name: String, - /// 一句话说明 - #[serde(default)] - pub desc: String, - /// **声明式参数清单**(如 `["q"]`)。 - /// - /// 只用于给调用方看的文档(管理台把 `?q=关键词` 拼出来、账号态接口回显), - /// 转发时**不按它过滤**——过滤要靠读请求体,而 access 阶段读不到。 - /// 供应商侧自己会拒绝不认识的参数,拦在网关意义不大。 - #[serde(default)] - pub params: Vec, - #[serde(default = "default_true")] - pub enabled: bool, - #[serde(default)] - pub note: String, - /// 调用语法提示(**机器可读**的描述字段,专为 passthrough / 复杂 vendor 设计)。 - /// - /// 为什么需要:`/api/v1/me` 把这条发给用户 AI 后,AI 必须能**自己拼出**调用语法—— - /// 路径尾部是什么、什么 Content-Type、上游 Swagger 在哪。`desc` 是中文给运维看的, - /// `params` 只对 vendor 有意义,对 passthrough 是空数组,没法告诉 AI 怎么调。 - /// `usage_hint` 是管理员按建议格式(自由文本)填写的结构化提示,AI 直接解析。 - /// - /// 不进 shm:热路径只查 base/key/asset_id/mode,`usage_hint` 只在管理台 + - /// `/api/v1/me` 拉清单时回显,进 shm 反而占带宽。 - /// - /// 建议格式(OpenAPI 风格): - /// ```text - /// POST / multipart, fields=[file]; OCR 上传图片 - /// POST //base64 json, {image_base64:string} - /// POST //url?url= json, {} - /// Swagger: http://192.168.1.11:9003/docs - /// ``` - #[serde(default)] - pub usage_hint: String, - #[serde(default)] - pub created_at: i64, -} - -fn default_true() -> bool { - true -} - -/// 默认代理模式 = vendor(向后兼容旧数据:没有 `mode` 字段的条目就是 vendor)。 -fn default_mode() -> String { - "vendor".to_owned() -} diff --git a/rust/crates/plugin-apix/src/store.rs b/rust/crates/plugin-apix/src/store.rs deleted file mode 100644 index 78dddd4..0000000 --- a/rust/crates/plugin-apix/src/store.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! 代理表的文件读写(复用内核 store 的原子写 + 全局文件写锁)。 -//! -//! `data/api-proxies.json` 含供应商真 Key,**敏感级别等同 `users.json`**。 - -use std::path::PathBuf; - -use ipass_core::{state, store}; - -use crate::models::ApiProxyStore; - -fn api_proxies_path() -> PathBuf { - store::path_in_data("api-proxies.json") -} - -pub fn read_api_proxies() -> ApiProxyStore { - store::read_json(&api_proxies_path(), ApiProxyStore::default()) -} - -pub fn write_api_proxies(data: &ApiProxyStore) -> Result<(), String> { - store::write_json(&api_proxies_path(), data) -} - -/// 加锁「读-改-写」代理表。 -/// -/// 闭包返回 `Ok(T)` 表示提交,`Err(msg)` 表示放弃(不写盘)。 -pub fn update_api_proxies(f: F) -> Result -where - F: FnOnce(&mut ApiProxyStore) -> Result, -{ - let _guard = state::file_lock().ok_or("内部错误:状态锁不可用")?; - let mut data = read_api_proxies(); - let out = f(&mut data)?; - write_api_proxies(&data)?; - Ok(out) -} diff --git a/rust/crates/plugin-domain/Cargo.toml b/rust/crates/plugin-domain/Cargo.toml deleted file mode 100644 index 11404e3..0000000 --- a/rust/crates/plugin-domain/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "plugin-domain" -version = "2.0.0" -edition.workspace = true -publish = false -description = "iPass 插件:动态域名(子域名映射、申请审批、转发决策)" - -[lib] -# 只有 gateway 是 cdylib;插件一律 rlib(跨 .so 传 Rust 类型会双重 free) -crate-type = ["rlib"] - -[dependencies] -ngx.workspace = true -serde.workspace = true -serde_json.workspace = true -ipass-core.workspace = true diff --git a/rust/crates/plugin-domain/src/api.rs b/rust/crates/plugin-domain/src/api.rs deleted file mode 100644 index f67ee32..0000000 --- a/rust/crates/plugin-domain/src/api.rs +++ /dev/null @@ -1,191 +0,0 @@ -//! 域名类端点:管理员审核/创建/删除/清理 + 账号态申请/查询/删除。 -//! -//! 路由表只认领**本插件**的路径;不认领的返回 `None` 交给下一个插件。 -//! `/api/admin/purge` 清理的是过期域名与陈旧驳回记录,属域名职责。 - -use ngx::core::Status; -use ngx::ffi::ngx_http_request_t; -use ngx::http::HTTPStatus; -use serde_json::json; - -use ipass_core::api::{Actor, Guard, run_guarded, user_or_401}; -use ipass_core::http; -use ipass_core::util; - -use crate::config; -use crate::domains; - -// =========================================================================== -// 路由 -// =========================================================================== - -type Handler = fn(*mut ngx_http_request_t, Actor) -> Status; - -fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> { - let entry: (Guard, Handler) = match (method, uri) { - ("GET", "/api/admin/domains") => (Guard::Admin, h_admin_domains), - ("POST", "/api/admin/domains/review") => (Guard::Admin, h_admin_domain_review), - ("POST", "/api/admin/domains/create") => (Guard::Admin, h_admin_domain_create), - ("POST", "/api/admin/domains/delete") => (Guard::Admin, h_admin_domain_delete), - ("POST", "/api/admin/purge") => (Guard::Admin, h_admin_purge), - ("GET", "/api/v1/domains") => (Guard::User, h_my_domains), - ("POST", "/api/v1/domains/apply") => (Guard::User, h_apply), - ("POST", "/api/v1/domains/delete") => (Guard::User, h_my_delete), - _ => return None, - }; - Some(entry) -} - -/// 本表分发:认领则返回响应状态,不认领返回 `None`。 -pub fn dispatch(r: *mut ngx_http_request_t) -> Option { - let method = http::method_str(r); - let uri = http::uri(r); - - let Some((guard, handler)) = route(&method, &uri) else { - return None; - }; - - Some(run_guarded(r, guard, handler)) -} - -// =========================================================================== -// 管理员:域名审核 -// =========================================================================== - -fn h_admin_domains(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - http::ok( - r, - json!({ - "items": domains::view_all(&domains::all()), - "pending": domains::count_pending(), - // 子域名命名规则:前端据此渲染「i + 8位数字 + dm」输入框, - // 避免前后端各写一份前缀/后缀/zone 而漂移(真源 = 本插件 config.rs) - "subdomain_rule": { - "zone": config::ENTERPRISE_ZONE, - "prefix": config::SUBDOMAIN_PREFIX, - "suffix": config::SUBDOMAIN_SUFFIX, - "digits": config::SUBDOMAIN_RAND_DIGITS, - }, - }), - "ok", - ) -} - -fn h_admin_domain_review(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let p = http::params(r); - let id = p.get("id").map(String::as_str).unwrap_or(""); - let action = p.get("action").map(String::as_str).unwrap_or(""); - match domains::review(id, action, actor.admin_name()) { - Ok(entry) => { - let msg = if action == "approve" { "已通过审核" } else { "已驳回" }; - http::ok(r, domains::view(&entry), msg) - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_domain_create(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let p = http::params(r); - - // subdomain 缺省(或空串)= 由网关按规则自动生成 `i<8位随机数字>dm`, - // 与面板上「i + 8位随机数 + dm」是同一套规则(见 domains::gen_subdomain)。 - match domains::create_by_admin( - p.get("subdomain").map(String::as_str).unwrap_or(""), - p.get("backend").map(String::as_str).unwrap_or(""), - p.get("type"), - p.get("owner"), - actor.admin_name(), - ) { - Ok(entry) => http::ok(r, domains::view(&entry), "已创建并直接放行"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_domain_delete(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let id = p.get("id").map(String::as_str).unwrap_or(""); - - match domains::remove(id) { - Ok(sub) => http::ok(r, json!({ "subdomain": sub }), "已删除"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -fn h_admin_purge(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - match domains::purge() { - Ok(removed) => http::ok( - r, - json!({ "removed": removed }), - &format!("已清理 {removed} 条过期/陈旧记录"), - ), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -// =========================================================================== -// 账号态 -// =========================================================================== - -fn h_my_domains(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - http::ok( - r, - json!({ "items": domains::view_all(&domains::by_owner(&user.username)) }), - "ok", - ) -} - -fn h_apply(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - - let p = http::params(r); - let entry = match domains::apply( - &user.username, - p.get("subdomain").map(String::as_str).unwrap_or(""), - p.get("backend").map(String::as_str).unwrap_or(""), - p.get("type"), - ) { - Ok(e) => e, - Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e), - }; - - let view = domains::view(&entry); - util::log_notice(&format!( - "[api] {} 申请 {} type={} status={}", - user.username, - entry.subdomain, - entry.dtype, - entry.effective_status(util::now()) - )); - - let msg = if entry.status == "approved" { - format!( - "已直接放行(临时授权权限生效),{} 小时后自动失效", - config::TEMP_DOMAIN_TTL / 3600 - ) - } else { - "已提交申请,等待管理员审核".to_owned() - }; - http::ok(r, view, &msg) -} - -fn h_my_delete(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - - let p = http::params(r); - let id = p.get("id").map(String::as_str).unwrap_or(""); - - match domains::remove_own(id, &user.username) { - Ok(sub) => http::ok(r, json!({ "subdomain": sub }), "已删除"), - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} diff --git a/rust/crates/plugin-domain/src/config.rs b/rust/crates/plugin-domain/src/config.rs deleted file mode 100644 index 6157c61..0000000 --- a/rust/crates/plugin-domain/src/config.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! 域名插件策略常量(自内核 config.rs 迁入;改策略只改这里)。 - -/// 临时域名审批通过后的有效期(秒)。到期仅停止转发,记录保留供查阅。 -pub const TEMP_DOMAIN_TTL: i64 = 6 * 3600; - -/// 子域名总长上限:首字符 + 最多 61 个后续字符 -pub const SUBDOMAIN_MAX_LEN: usize = 62; - -/// 系统保留名:不允许用户申请,避免抢占管理入口 -pub const RESERVED_SUBDOMAINS: [&str; 4] = ["proxy-api", "admin", "www", "api"]; - -/// 驳回记录保留时长,超期由 purge 清理(秒) -pub const REJECTED_PURGE_TTL: i64 = 7 * 86400; - -/// 键前缀:子域名 → 后端映射 -pub const KEY_SUB_PREFIX: &[u8] = b"sub:"; - -/// 企业域名(业务域名)的 zone:**子域名 + `.` + 此后缀 = 完整对外域名**。 -/// -/// ⚠️ 与 `conf/nginx.conf` 的「企业域名 server」严格耦合: -/// `server_name ~^(?.+dm)\.mayishangshu\.cn$;` -/// 改这里必须同步改那条正则(反之亦然)。 -pub const ENTERPRISE_ZONE: &str = "mayishangshu.cn"; - -/// 自动生成子域名的形状:`<前缀><后缀>` —— 例如 `i12345678dm`。 -/// -/// 后缀**必须是 `dm`**:企业域名 server 的正则要求捕获组以 `dm` 结尾 -/// (`^(.+dm)\.mayishangshu\.cn$`),否则域名建出来也转发不到。 -/// 这是「规则」的实质约束,不是命名偏好。 -pub const SUBDOMAIN_PREFIX: &str = "i"; -pub const SUBDOMAIN_SUFFIX: &str = "dm"; -/// 随机数字位数。10^8 的空间,撞车概率极低 -pub const SUBDOMAIN_RAND_DIGITS: usize = 8; -/// 自动生成子域名时的最大重试次数(真撞上已占用的就换一个再试) -pub const SUBDOMAIN_GEN_ATTEMPTS: usize = 5; diff --git a/rust/crates/plugin-domain/src/domains.rs b/rust/crates/plugin-domain/src/domains.rs deleted file mode 100644 index db09a3e..0000000 --- a/rust/crates/plugin-domain/src/domains.rs +++ /dev/null @@ -1,488 +0,0 @@ -//! 域名(子域名映射)与申请审核。 -//! -//! 数据结构见 `models::DomainEntry`。 -//! -//! ## 状态机 -//! -//! ```text -//! temp + 有临时授权权限 → 直接 approved(reviewed_by=auto,到时自动 expired) -//! temp + 无临时授权权限 → pending,等管理员审核 -//! long + 任意 → pending,等管理员审核 -//! ``` -//! -//! 到期不等于删除:临时域名过期后仅「不再转发」,记录保留供查阅。 -//! -//! ## 写穿 -//! -//! 热路径**永不回源**(未命中即 404),所以每个写操作都必须 -//! **同时更新共享内存与文件**——这就是 `sync_shm` 的存在理由。 -//! 漏掉一处,就会出现「审核通过了但立刻 404」。 - -use serde_json::{Value, json}; - -use ipass_core::{state, users, util}; - -use crate::config::{ - ENTERPRISE_ZONE, KEY_SUB_PREFIX, REJECTED_PURGE_TTL, RESERVED_SUBDOMAINS, SUBDOMAIN_GEN_ATTEMPTS, - SUBDOMAIN_MAX_LEN, SUBDOMAIN_PREFIX, SUBDOMAIN_RAND_DIGITS, SUBDOMAIN_SUFFIX, TEMP_DOMAIN_TTL, -}; -use crate::models::DomainEntry; - -// =========================================================================== -// 校验 -// =========================================================================== - -/// 子域名:小写字母/数字开头,其后可含短横线,总长 ≤ 62,且不在保留名单内。 -/// 返回规范化(小写)后的值。 -pub fn validate_subdomain(raw: &str) -> Result { - if raw.is_empty() { - return Err("缺少 subdomain 参数".to_owned()); - } - let s = raw.to_ascii_lowercase(); - let bytes = s.as_bytes(); - - if bytes.len() > SUBDOMAIN_MAX_LEN { - return Err("子域名长度不能超过 62 个字符".to_owned()); - } - let head_ok = matches!(bytes[0], b'a'..=b'z' | b'0'..=b'9'); - let tail_ok = bytes[1..].iter().all(|&b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'-')); - if !head_ok || !tail_ok { - return Err("子域名只能是小写字母、数字、短横线,且不能以短横线开头".to_owned()); - } - if RESERVED_SUBDOMAINS.contains(&s.as_str()) { - return Err(format!("子域名 {s} 为系统保留名,不可使用")); - } - Ok(s) -} - -/// 后端地址:`host:port`,host 允许字母/数字/`.`/`_`/`-`,端口 1-65535 -pub fn validate_backend(raw: &str) -> Result { - let s = util::trim(raw); - if s.is_empty() { - return Err("缺少 backend 参数".to_owned()); - } - let Some(colon) = s.rfind(':') else { - return Err("backend 格式应为 host:port,例如 192.168.1.7:89".to_owned()); - }; - let (host, port) = (&s[..colon], &s[colon + 1..]); - - let host_ok = !host.is_empty() - && host.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')); - let port_ok = !port.is_empty() - && port.chars().all(|c| c.is_ascii_digit()) - && port.parse::().map(|p| (1..=65535).contains(&p)).unwrap_or(false); - - if !host_ok || !port_ok { - return Err("backend 格式应为 host:port,且端口在 1-65535 之间".to_owned()); - } - Ok(s.to_owned()) -} - -// =========================================================================== -// 子域名命名规则(唯一真源,前端/API 共用) -// =========================================================================== - -/// 生成一个符合企业域名规则的子域名:`i` + 8 位随机数字 + `dm`(如 `i48291376dm`)。 -/// -/// **后缀 `dm` 不是装饰**:企业域名 server 的正则是 -/// `^(.+dm)\.mayishangshu\.cn$`,捕获组必须以 `dm` 结尾,否则该域名根本不会 -/// 被转发 server 匹配到。所以「i+8位+dm」是硬约束,别改后缀。 -pub fn gen_subdomain() -> String { - format!( - "{SUBDOMAIN_PREFIX}{}{SUBDOMAIN_SUFFIX}", - util::rand_digits(SUBDOMAIN_RAND_DIGITS) - ) -} - -/// 子域名 → 完整对外域名(**不含端口**)。 -/// -/// 端口刻意不在这里拼:网关用宿主 88 承载三个域名,但端口应当**跟随调用方** -/// (见 readme §15),由前端按 `location.port` 拼,写死任何一种都会在另一种 -/// 部署下给出「看着对、打不通」的地址。 -pub fn full_host(subdomain: &str) -> String { - format!("{subdomain}.{ENTERPRISE_ZONE}") -} - -pub fn validate_type(raw: Option<&String>) -> String { - match raw.map(|s| s.as_str()) { - Some("long") => "long".to_owned(), - _ => "temp".to_owned(), - } -} - -// =========================================================================== -// 共享内存同步(写穿 / 装载) -// =========================================================================== - -/// 把一条记录同步进共享内存。**所有写操作之后必须调用它**。 -/// -/// 写失败 = 热路径查不到 = 该域名立刻 404,所以失败必须出声 -/// (`state::put` 返回 false:共享内存 zone 满或分配失败)。 -pub fn sync_shm(entry: &DomainEntry) { - let now = util::now(); - if entry.is_forwardable(now) { - // long 用 0 表示永不过期;temp 用真实到期时刻,热路径读到即知是否还能用 - let exp = if entry.dtype == "temp" { entry.expires_at } else { 0 }; - if !state::put( - KEY_SUB_PREFIX, - entry.subdomain.as_bytes(), - entry.backend.as_bytes(), - exp, - ) { - util::log_err(&format!( - "[domains] 转发映射写共享内存失败 sub={} —— 该域名会 404(共享内存可能已满)", - entry.subdomain - )); - } - } else { - state::remove(KEY_SUB_PREFIX, entry.subdomain.as_bytes()); - } -} - -/// 全量重载转发映射(启动装载 / purge / 连带回收后调用)。返回装载条数。 -/// -/// 只清 `sub:` 前缀的键,**不会误伤正在登录的管理员会话**。 -pub fn reload_shm() -> usize { - state::clear_prefix(KEY_SUB_PREFIX); - let now = util::now(); - let mut expected = 0; - let mut loaded = 0; - for entry in crate::store::read_domains() { - if entry.is_forwardable(now) { - expected += 1; - let exp = if entry.dtype == "temp" { entry.expires_at } else { 0 }; - if state::put(KEY_SUB_PREFIX, entry.subdomain.as_bytes(), entry.backend.as_bytes(), exp) { - loaded += 1; - } - } - } - if loaded != expected { - util::log_err(&format!( - "[domains] 转发映射装载不全:{loaded}/{expected} —— 有域名会 404" - )); - } - loaded -} - -// =========================================================================== -// 查询 -// =========================================================================== - -/// 全部记录,按创建时间倒序 -pub fn all() -> Vec { - let mut list = crate::store::read_domains(); - list.sort_by(|a, b| b.created_at.cmp(&a.created_at)); - list -} - -pub fn by_owner(owner: &str) -> Vec { - let mut list: Vec = - crate::store::read_domains().into_iter().filter(|e| e.owner == owner).collect(); - list.sort_by(|a, b| b.created_at.cmp(&a.created_at)); - list -} - -/// 按 id 取单条记录(当前无调用方,保留为对外查询接口) -#[allow(dead_code)] -pub fn by_id(id: &str) -> Option { - crate::store::read_domains().into_iter().find(|e| e.id == id) -} - -pub fn count_pending() -> usize { - crate::store::read_domains().iter().filter(|e| e.status == "pending").count() -} - -// =========================================================================== -// 用户申请 -// =========================================================================== - -/// 用户提交申请。有临时授权权限的 temp 申请直接放行。 -/// -/// 返回的 entry 若 `status == approved && reviewed_by == auto`,即免审通过。 -pub fn apply( - username: &str, - subdomain: &str, - backend: &str, - dtype: Option<&String>, -) -> Result { - let user = users::get(username).ok_or("账号不存在")?; - - let subdomain = validate_subdomain(subdomain)?; - let backend = validate_backend(backend)?; - let dtype = validate_type(dtype); - - let now = util::now(); - let auto = dtype == "temp" && user.has_temp_permission(now); - - let chosen = subdomain.clone(); - let entry = crate::store::update_domains(move |list| { - if let Some(idx) = list.iter().position(|e| e.subdomain == chosen) { - let existing = &list[idx]; - let mine_pending = existing.owner == username && existing.status == "pending"; - let its_expired = - existing.dtype == "temp" && existing.effective_status(now) == "expired"; - - // 自己的待审申请可以改写;已失效的临时域名视为空闲可复用 - if !(mine_pending || its_expired) { - return Err(format!("子域名 {chosen} 已被占用")); - } - list.remove(idx); - } - - let entry = DomainEntry { - id: util::gen_id(), - subdomain: chosen, - backend, - dtype, - owner: username.to_owned(), - status: if auto { "approved".to_owned() } else { "pending".to_owned() }, - created_at: now, - expires_at: if auto { now + TEMP_DOMAIN_TTL } else { 0 }, - reviewed_by: if auto { "auto".to_owned() } else { String::new() }, - reviewed_at: if auto { now } else { 0 }, - }; - list.push(entry.clone()); - Ok(entry) - })?; - - sync_shm(&entry); - Ok(entry) -} - -// =========================================================================== -// 管理员操作 -// =========================================================================== - -/// 审核:approve / reject。只允许处理 pending 状态。 -pub fn review(id: &str, action: &str, admin: &str) -> Result { - if action != "approve" && action != "reject" { - return Err("action 只能是 approve 或 reject".to_owned()); - } - let now = util::now(); - let id = id.to_owned(); - let action = action.to_owned(); - let admin = admin.to_owned(); - - let updated = crate::store::update_domains(move |list| { - let item = list.iter_mut().find(|e| e.id == id).ok_or("记录不存在")?; - if item.status != "pending" { - return Err(format!("该申请已处理(当前状态:{})", item.status)); - } - if action == "approve" { - item.status = "approved".to_owned(); - item.expires_at = if item.dtype == "temp" { now + TEMP_DOMAIN_TTL } else { 0 }; - } else { - item.status = "rejected".to_owned(); - } - item.reviewed_by = admin; - item.reviewed_at = now; - Ok(item.clone()) - })?; - - sync_shm(&updated); - Ok(updated) -} - -/// 管理员直接创建并放行(等价于「我替你建好并批准」)。 -/// -/// `subdomain` **留空即自动生成** `i<8位随机数字>dm`(见 `gen_subdomain`), -/// 撞上已占用就换一个重试(最多 `SUBDOMAIN_GEN_ATTEMPTS` 次)。管理台与 -/// `/api/admin/domains/create` 走同一条路径,规则不会两处漂移。 -pub fn create_by_admin( - subdomain: &str, - backend: &str, - dtype: Option<&String>, - owner: Option<&String>, - admin: &str, -) -> Result { - let backend = validate_backend(backend)?; - - let owner = match owner { - Some(o) if !o.is_empty() => o.clone(), - _ => admin.to_owned(), - }; - if users::get(&owner).is_none() { - return Err(format!("归属账号不存在:{owner}")); - } - - let dtype = validate_type(dtype); - let manual = !util::trim(subdomain).is_empty(); - let attempts = if manual { 1 } else { SUBDOMAIN_GEN_ATTEMPTS }; - - let mut last_err = "子域名生成失败".to_owned(); - for _ in 0..attempts { - let sub = if manual { validate_subdomain(subdomain)? } else { gen_subdomain() }; - // 自动生成的也过一遍校验:常量被误改时立刻报错,而不是建出个用不了的域名 - let sub = validate_subdomain(&sub)?; - match insert_approved(&sub, &backend, &dtype, &owner, admin) { - Ok(entry) => { - sync_shm(&entry); - return Ok(entry); - } - Err(e) => { - // 手填的子域名撞车是用户要解决的问题;自动生成的直接换一个重试 - if manual { - return Err(e); - } - last_err = e; - } - } - } - Err(last_err) -} - -/// 落盘一条 approved 记录(占用检查在这里);调用方负责 `sync_shm`。 -fn insert_approved( - subdomain: &str, - backend: &str, - dtype: &str, - owner: &str, - admin: &str, -) -> Result { - let now = util::now(); - let chosen = subdomain.to_owned(); - let backend = backend.to_owned(); - let dtype = dtype.to_owned(); - let owner = owner.to_owned(); - let admin = admin.to_owned(); - - crate::store::update_domains(move |list| { - if let Some(idx) = list.iter().position(|e| e.subdomain == chosen) { - let existing = &list[idx]; - let its_expired = - existing.dtype == "temp" && existing.effective_status(now) == "expired"; - if !its_expired { - return Err(format!("子域名 {chosen} 已被占用")); - } - list.remove(idx); - } - - let entry = DomainEntry { - id: util::gen_id(), - subdomain: chosen, - backend, - dtype: dtype.clone(), - owner, - status: "approved".to_owned(), - created_at: now, - expires_at: if dtype == "temp" { now + TEMP_DOMAIN_TTL } else { 0 }, - reviewed_by: admin, - reviewed_at: now, - }; - list.push(entry.clone()); - Ok(entry) - }) -} - -/// 用户删除自己的域名。成功返回被删的子域名(供转发侧失效)。 -pub fn remove_own(id: &str, username: &str) -> Result { - let id = id.to_owned(); - let username = username.to_owned(); - - crate::store::update_domains(move |list| { - let idx = list.iter().position(|e| e.id == id).ok_or("记录不存在")?; - if list[idx].owner != username { - return Err("只能删除自己的域名".to_owned()); - } - let sub = list.remove(idx).subdomain; - Ok(sub) - }) - .map(|sub| { - state::remove(KEY_SUB_PREFIX, sub.as_bytes()); - sub - }) -} - -/// 管理员删除任意域名 -pub fn remove(id: &str) -> Result { - let id = id.to_owned(); - crate::store::update_domains(move |list| { - let idx = list.iter().position(|e| e.id == id).ok_or("记录不存在")?; - Ok(list.remove(idx).subdomain) - }) - .map(|sub| { - state::remove(KEY_SUB_PREFIX, sub.as_bytes()); - sub - }) -} - -/// 连带回收某账号名下的全部域名(删除账号时用)。返回回收条数。 -pub fn remove_by_owner(owner: &str) -> usize { - let mut removed = 0usize; - let result = crate::store::update_domains(|list| { - list.retain(|e| { - if e.owner == owner { - removed += 1; - false - } else { - true - } - }); - Ok(()) - }); - if result.is_ok() && removed > 0 { - // 被回收的可能包含正在转发的域名,必须全量重载 - reload_shm(); - } - removed -} - -/// 清理:已过期的临时域名 + 超过保留期的驳回记录。返回清理条数。 -pub fn purge() -> Result { - let now = util::now(); - let mut removed = 0usize; - - crate::store::update_domains(|list| { - list.retain(|e| { - let status = e.effective_status(now); - let stale_rejected = - status == "rejected" && (now - e.reviewed_at) > REJECTED_PURGE_TTL; - if status == "expired" || stale_rejected { - removed += 1; - false - } else { - true - } - }); - Ok(()) - })?; - - if removed > 0 { - reload_shm(); - } - Ok(removed) -} - -// =========================================================================== -// 对外投影 -// =========================================================================== - -pub fn view(entry: &DomainEntry) -> Value { - let now = util::now(); - let status = entry.effective_status(now); - let exp = entry.expires_at; - let live_temp = status == "approved" && entry.dtype == "temp" && exp > now; - - json!({ - "id": entry.id, - "subdomain": entry.subdomain, - "host": full_host(&entry.subdomain), - "backend": entry.backend, - "type": entry.dtype, - "owner": entry.owner, - "status": status, - "created_at": entry.created_at, - "created_at_text": util::fmt_time(entry.created_at), - "expires_at": exp, - "expires_at_text": if entry.dtype == "temp" && exp > 0 { util::fmt_time(exp) } else { "-".to_owned() }, - "expires_left": if live_temp { exp - now } else { 0 }, - "expires_left_text": if live_temp { util::fmt_left(exp - now) } else { String::new() }, - "reviewed_by": entry.reviewed_by, - "reviewed_at": entry.reviewed_at, - "auto_approved": entry.reviewed_by == "auto", - }) -} - -pub fn view_all(list: &[DomainEntry]) -> Vec { - list.iter().map(view).collect() -} diff --git a/rust/crates/plugin-domain/src/lib.rs b/rust/crates/plugin-domain/src/lib.rs deleted file mode 100644 index aab3cec..0000000 --- a/rust/crates/plugin-domain/src/lib.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! plugin-domain —— 动态域名插件。 -//! -//! 职责:子域名 → 后端映射(`sub:` 键空间)、申请与审批、转发决策 -//! (access 阶段查共享内存)。数据文件 `data/domains.json`。 -//! -//! ## 与旧版最大的差异:写穿 -//! -//! 热路径**永不回源**(未命中即 404),所以每个写操作都必须 -//! **同时更新共享内存与文件**——这就是 `domains::sync_shm` 的存在理由。 -//! 漏掉一处,就会出现「审核通过了但立刻 404」。 -//! -//! ## 装配点(gateway) -//! -//! * `preconfig` —— 注册 `$proxy_backend_addr` 变量(`proxy_pass` 依赖) -//! * `postconfig` —— 注册 `ResolveHandler`(access 阶段) -//! * `zone_load` —— 全量装载 `sub:` 映射 -//! * `dispatch_api` —— 认领域名类管理/账号端点 - -pub mod api; -pub mod config; -pub mod domains; -pub mod models; -pub mod resolve; -pub mod store; - -use ngx::core::Status; -use ngx::ffi::{ngx_conf_t, ngx_http_request_t}; - -use ipass_core::plugin::Plugin; - -pub struct DomainPlugin; - -impl Plugin for DomainPlugin { - fn name(&self) -> &'static str { - "domain" - } - - fn preconfig(&self, cf: *mut ngx_conf_t) -> bool { - resolve::preconfig(cf) - } - - fn postconfig(&self, cf: *mut ngx_conf_t) -> bool { - // SAFETY: cf 由 nginx 传入且有效 - ngx::http::add_phase_handler::(unsafe { &mut *cf }).is_ok() - } - - fn dispatch_api(&self, r: *mut ngx_http_request_t) -> Option { - api::dispatch(r) - } - - fn zone_load(&self) -> usize { - domains::reload_shm() - } -} diff --git a/rust/crates/plugin-domain/src/models.rs b/rust/crates/plugin-domain/src/models.rs deleted file mode 100644 index 3d1d8d6..0000000 --- a/rust/crates/plugin-domain/src/models.rs +++ /dev/null @@ -1,61 +0,0 @@ -//! 域名持久化模型(`domains.json`:一条记录 = 一个子域名)。 - -use serde::{Deserialize, Serialize}; - -/// 单条域名记录。 -/// -/// 字段名与旧版 `data/domains.json` **逐字对应**,以便新版直接接管旧数据文件。 -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DomainEntry { - pub id: String, - pub subdomain: String, - /// `host:port` - pub backend: String, - /// `temp`(临时,到时自动停止转发) | `long`(长期) - #[serde(rename = "type")] - pub dtype: String, - /// 归属账号 - pub owner: String, - /// `pending` | `approved` | `rejected` - pub status: String, - #[serde(default)] - pub created_at: i64, - /// 仅 temp 有意义;long 恒为 0 - #[serde(default)] - pub expires_at: i64, - /// `auto` 表示凭临时授权权限免审放行 - #[serde(default)] - pub reviewed_by: String, - #[serde(default)] - pub reviewed_at: i64, -} - -impl DomainEntry { - /// 实际状态:approved 的临时域名过期后视为 `expired`。 - /// - /// 这是「到期不等于删除」语义的落点——记录保留,只是不再转发。 - pub fn effective_status(&self, now: i64) -> &'static str { - if self.status == "approved" && self.dtype == "temp" { - if self.expires_at > 0 && self.expires_at <= now { - return "expired"; - } - } - match self.status.as_str() { - "approved" => "approved", - "rejected" => "rejected", - _ => "pending", - } - } - - /// 转发侧判定:返回该域名当前是否可转发(已放行且未过期) - pub fn is_forwardable(&self, now: i64) -> bool { - if self.status != "approved" { - return false; - } - if self.dtype == "temp" { - // 临时域名必须有明确的、尚未到达的到期时刻 - return self.expires_at > now; - } - true - } -} diff --git a/rust/crates/plugin-domain/src/resolve.rs b/rust/crates/plugin-domain/src/resolve.rs deleted file mode 100644 index c4605c5..0000000 --- a/rust/crates/plugin-domain/src/resolve.rs +++ /dev/null @@ -1,188 +0,0 @@ -//! 转发热路径:查共享内存取后端地址,供 nginx 的 `proxy_pass` 使用。 -//! -//! **热路径唯一入口**——只读内存,未命中不产生任何 I/O。 -//! 旧版这里在 miss 时会去读 `domains.json` 并全量 JSON 解码, -//! 那正是「随便发一堆随机子域名就能打穿磁盘」的入口。 - -use core::ptr; -use core::sync::atomic::{AtomicUsize, Ordering}; - -use ngx::core::{Pool, Status}; -use ngx::ffi::{ - NGX_HTTP_VAR_CHANGEABLE, NGX_HTTP_VAR_NOCACHEABLE, ngx_conf_t, ngx_http_add_variable, - ngx_http_get_variable_index, ngx_http_request_t, ngx_http_variable_value_t, ngx_int_t, - ngx_uint_t, -}; -use ngx::http::{HTTPStatus, HttpPhase, HttpRequestHandler}; -use ngx::ngx_string; - -use crate::config; -use ipass_core::loc::{self, Switch}; -use ipass_core::state; -use ipass_core::util; - -/// `$proxy_backend_addr` 在 `r->variables` 里的下标 -static BACKEND_VAR_INDEX: AtomicUsize = AtomicUsize::new(usize::MAX); - -/// 查共享内存取后端地址;已过期的条目视为不存在。 -fn resolve_backend(subdomain: &[u8]) -> Option { - let (backend, expires_at) = state::get(config::KEY_SUB_PREFIX, subdomain)?; - if expires_at > 0 && expires_at <= util::now() { - return None; - } - Some(backend) -} - -/// 取 Host 头里第一个点之前的部分作为子域名。 -/// -/// 刻意不走 nginx 的 server_name 正则捕获变量(那需要再算一次变量 hash), -/// 热路径上手工切更省。 -pub fn subdomain_of(r: *mut ngx_http_request_t) -> Option> { - // SAFETY: r 有效;迭代器只在本函数存续期内使用 - let request = unsafe { ngx::http::Request::from_ngx_http_request(r) }; - for (key, value) in request.headers_in_iterator() { - if key.as_bytes().eq_ignore_ascii_case(b"host") { - let host = value.as_bytes(); - // 去掉端口 - let host = match host.iter().position(|&b| b == b':') { - Some(i) => &host[..i], - None => host, - }; - // 取第一段标签 - let label = match host.iter().position(|&b| b == b'.') { - Some(i) => &host[..i], - None => host, - }; - if label.is_empty() { - return None; - } - return Some(label.to_ascii_lowercase()); - } - } - None -} - -/// 把后端地址写进 `r->variables[$proxy_backend_addr]`,供 `proxy_pass` 使用 -fn set_backend_var(r: *mut ngx_http_request_t, backend: &str) { - let idx = BACKEND_VAR_INDEX.load(Ordering::SeqCst); - if idx == usize::MAX { - return; - } - // SAFETY: idx 由 ngx_http_get_variable_index 分配且落在 r->variables 内; - // 值分配在请求池里,随请求回收 - unsafe { - let pool = Pool::from_ngx_pool((*r).pool); - let bytes = backend.as_bytes(); - let data = pool.alloc(bytes.len()) as *mut u8; - if data.is_null() { - return; - } - ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len()); - - let vv: *mut ngx_http_variable_value_t = (*r).variables.add(idx); - (*vv).data = data; - (*vv).set_len(bytes.len() as _); - (*vv).set_valid(1); - (*vv).set_not_found(0); - (*vv).set_no_cacheable(1); - } -} - -/// 变量 get_handler:`proxy_pass` 求值 `$proxy_backend_addr` 时被调用。 -/// -/// 与 access 阶段互补——这里保证「即使 access 未赋值也能取到」, -/// access 阶段则负责决定命中不了时返回 404。 -extern "C" fn backend_get( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - _data: usize, -) -> ngx_int_t { - // SAFETY: nginx 传入的 r/v 必然有效 - unsafe { - let resolved = subdomain_of(r).as_deref().and_then(resolve_backend); - - let Some(backend) = resolved else { - (*v).set_not_found(1); - return Status::NGX_ERROR.into(); - }; - - let pool = Pool::from_ngx_pool((*r).pool); - let bytes = backend.as_bytes(); - let data = pool.alloc(bytes.len()) as *mut u8; - if data.is_null() { - (*v).set_not_found(1); - return Status::NGX_ERROR.into(); - } - ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len()); - - (*v).data = data; - (*v).set_len(bytes.len() as _); - (*v).set_valid(1); - (*v).set_no_cacheable(1); - (*v).set_not_found(0); - - Status::NGX_OK.into() - } -} - -/// 配置解析前注册 `$proxy_backend_addr`。 -/// -/// 必须在 preconfiguration 阶段调用,否则配置里的 -/// `proxy_pass http://$proxy_backend_addr` 无法解析。 -pub fn preconfig(cf: *mut ngx_conf_t) -> bool { - let mut name = ngx_string!("proxy_backend_addr"); - - let idx = unsafe { ngx_http_get_variable_index(cf, &raw mut name) }; - if idx == -1 { - return false; - } - BACKEND_VAR_INDEX.store(idx as usize, Ordering::SeqCst); - - let var = unsafe { - ngx_http_add_variable( - cf, - &raw mut name, - (NGX_HTTP_VAR_CHANGEABLE | NGX_HTTP_VAR_NOCACHEABLE) as ngx_uint_t, - ) - }; - if var.is_null() { - return false; - } - - // SAFETY: var 非空且由本函数独占初始化 - unsafe { - (*var).get_handler = Some(backend_get); - (*var).set_handler = None; - (*var).data = 0; - } - true -} - -/// 转发热路径:命中则放行(`proxy_pass` 会去取 `$proxy_backend_addr`),未命中直接 404。 -pub struct ResolveHandler; - -impl HttpRequestHandler for ResolveHandler { - const PHASE: HttpPhase = HttpPhase::Access; - type Output = Status; - - fn handler(request: &mut ngx::http::Request) -> Self::Output { - // 未开启该开关的 location 一律放行(管理台/API 走这里) - let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t; - if !loc::enabled(r, Switch::Proxy) { - return Status::NGX_DECLINED; - } - - let Some(host) = subdomain_of(r) else { - return HTTPStatus::NOT_FOUND.into(); - }; - - match resolve_backend(&host) { - Some(backend) => { - set_backend_var(r, &backend); - Status::NGX_DECLINED - } - // 与旧版语义一致:查不到 = 未配置 / 已过期 / 已删除 / 尚未审核通过 - None => HTTPStatus::NOT_FOUND.into(), - } - } -} diff --git a/rust/crates/plugin-domain/src/store.rs b/rust/crates/plugin-domain/src/store.rs deleted file mode 100644 index 52a93a6..0000000 --- a/rust/crates/plugin-domain/src/store.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! 域名表的文件读写(复用内核 store 的原子写 + 全局文件写锁)。 - -use std::path::PathBuf; - -use ipass_core::{state, store}; - -use crate::models::DomainEntry; - -fn domains_path() -> PathBuf { - store::path_in_data("domains.json") -} - -pub fn read_domains() -> Vec { - store::read_json(&domains_path(), Vec::new()) -} - -pub fn write_domains(data: &[DomainEntry]) -> Result<(), String> { - store::write_json(&domains_path(), &data.to_vec()) -} - -/// 加锁「读-改-写」域名表。 -/// -/// 闭包返回 `Ok(T)` 表示提交,`Err(msg)` 表示放弃(不写盘)。 -pub fn update_domains(f: F) -> Result -where - F: FnOnce(&mut Vec) -> Result, -{ - let _guard = state::file_lock().ok_or("内部错误:状态锁不可用")?; - let mut data = read_domains(); - let out = f(&mut data)?; - write_domains(&data)?; - Ok(out) -} diff --git a/rust/crates/plugin-llm/Cargo.toml b/rust/crates/plugin-llm/Cargo.toml deleted file mode 100644 index 985e11c..0000000 --- a/rust/crates/plugin-llm/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "plugin-llm" -version = "2.0.0" -edition.workspace = true -publish = false -description = "iPass 插件:LLM 路由 / 上游管理 / 配额计费 / 对话审计 / 用量趋势" - -[lib] -# 只有 gateway 是 cdylib;插件一律 rlib(跨 .so 传 Rust 类型会双重 free) -crate-type = ["rlib"] - -[dependencies] -ngx.workspace = true -nginx-sys.workspace = true -serde.workspace = true -serde_json.workspace = true -ipass-core.workspace = true diff --git a/rust/crates/plugin-llm/src/api.rs b/rust/crates/plugin-llm/src/api.rs deleted file mode 100644 index 513a0fd..0000000 --- a/rust/crates/plugin-llm/src/api.rs +++ /dev/null @@ -1,444 +0,0 @@ -//! LLM 插件的管理 API:上游 CRUD / 连通性测试 / 用量查询。 -//! -//! 趋势接口 `/api/admin/trend` **不在本表**——它需要 users + upstreams + -//! apix 三方名册(日桶键带日窗 id,按前缀扫不出来),属于组合逻辑, -//! 由 gateway 作为装配根实现(对方案的有意偏离,见 PLUGINIZATION_PLAN)。 -//! -//! 鉴权语义与内核共用一套:`ipass_core::api::{Guard, Actor, run_guarded}`。 - -use ngx::core::{Pool, Status}; -use ngx::ffi::ngx_http_request_t; -use ngx::http::HTTPStatus; -use serde_json::{Value, json}; - -use ipass_core::api::{Actor, Guard, run_guarded, user_or_401}; -use ipass_core::{http, util}; - -use crate::llm; -use crate::llm_audit; -use crate::stats; -use crate::stats_persist; -use crate::upstreams; - -// =========================================================================== -// 路由 -// =========================================================================== - -type Handler = fn(*mut ngx_http_request_t, Actor) -> Status; - -/// 路由表。刻意用精确匹配而非正则——接口路径是固定的, -/// 正则只会引入「写错锚点导致前缀误匹配」的风险。 -fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> { - let entry: (Guard, Handler) = match (method, uri) { - ("GET", "/api/admin/upstreams") => (Guard::Admin, h_admin_upstreams), - ("POST", "/api/admin/upstreams/save") => (Guard::Admin, h_admin_upstream_save), - ("POST", "/api/admin/upstreams/test") => (Guard::Admin, h_admin_upstream_test), - ("POST", "/api/admin/upstreams/delete") => (Guard::Admin, h_admin_upstream_delete), - ("GET", "/api/admin/usage") => (Guard::Admin, h_admin_usage), - ("GET", "/api/admin/stats/persist") => (Guard::Admin, h_admin_stats_persist), - ("POST", "/api/admin/stats/dump") => (Guard::Admin, h_admin_stats_dump), - ("GET", "/api/admin/audit/days") => (Guard::Admin, h_admin_audit_days), - ("GET", "/api/admin/audit/day") => (Guard::Admin, h_admin_audit_day), - ("GET", "/api/v1/models") => (Guard::User, h_my_models), - ("GET", "/api/v1/usage") => (Guard::User, h_my_usage), - ("GET", "/api/v1/upstreams") => (Guard::User, h_my_upstreams), - _ => return None, - }; - Some(entry) -} - -/// 本表分发:认领则返回响应状态,不认领返回 `None`(交给下一个插件)。 -pub fn dispatch(r: *mut ngx_http_request_t) -> Option { - let method = http::method_str(r); - let uri = http::uri(r); - - let Some((guard, handler)) = route(&method, &uri) else { - return None; - }; - - Some(run_guarded(r, guard, handler)) -} - -// =========================================================================== -// 管理员:LLM 上游 -// =========================================================================== - -fn h_admin_upstreams(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - http::ok( - r, - json!({ "items": upstreams::view_all(&upstreams::all(), &http::host_port(r)) }), - "ok", - ) -} - -fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - - // 表单没提交 enabled 字段时视为「启用」——否则改个备注就会把上游改停, - // 这种误伤在管理台上极难自查 - let enabled = match p.get("enabled") { - None => true, - Some(v) => util::to_bool(Some(v)), - }; - - // models 缺省 = 保持原清单;显式传(哪怕是空串)= 覆盖。 - // 分隔符同时接受换行/逗号/顿号/分号——粘一份厂商文档里的模型列表是常见操作。 - let models = p.get("models").map(|raw| parse_model_list(raw)); - - // 多账号:前端把 `accounts` 作为 JSON 数组串传上来(每个 {auth, note, resets})。 - // 缺省/空 = 沿用旧逻辑(用 `auth` 维护单账号)。 - let accounts = p - .get("accounts") - .and_then(|s| { - let s = s.trim(); - if s.is_empty() { - None - } else { - serde_json::from_str::>(s).ok() - } - }); - - // 上游级「额度耗尽」探测配置(JSON 数组;也兼容逗号串) - let exhaust_status = parse_int_list(p.get("exhaust_status")); - let exhaust_markers = p - .get("exhaust_markers") - .and_then(|s| { - let s = s.trim(); - if s.is_empty() { - None - } else { - serde_json::from_str::>(s).ok() - } - }); - - let slot_raw = p.get("slot").map(String::as_str).unwrap_or(""); - - match upstreams::upsert( - slot_raw, - p.get("base"), - p.get("base_anthropic"), - models, - p.get("auth"), - accounts, - exhaust_status, - exhaust_markers, - enabled, - p.get("note"), - ) { - Ok(up) => { - // 用校验后的 slot 回显;直接拿原始入参会把大小写差异带进响应 - let slot = upstreams::validate_slot(slot_raw).unwrap_or_default(); - util::log_notice(&format!( - "[api] 保存 LLM 上游 {slot} enabled={enabled} accounts={} models={}", - up.effective_accounts().len(), - up.models.len() - )); - // base_url 带「调用方正在用的端口」:见 upstreams::base_url 的说明 - http::ok(r, upstreams::view(&slot, &up, &http::host_port(r)), "已保存") - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -/// 解析整数列表:优先 JSON 数组,回退逗号分隔串。 -fn parse_int_list(raw: Option<&String>) -> Option> { - let s = raw?; - let s = s.trim(); - if s.is_empty() { - return None; - } - if let Ok(v) = serde_json::from_str::>(s) { - return Some(v); - } - Some( - s.split(',') - .filter_map(|x| x.trim().parse::().ok()) - .collect(), - ) -} - -/// 把管理台/接口传入的模型列表串拆成模型名数组。 -/// -/// 允许的写法(混用也认):`a,b` / `a、b` / `a;b` / 一行一个。 -pub fn parse_model_list(raw: &str) -> Vec { - raw.split(|c: char| c == ',' || c == '、' || c == ';' || c == '\n' || c == '\r' || c == '\t') - .map(|s| util::trim(s).to_owned()) - .filter(|s| !s.is_empty()) - .collect() -} - -fn h_admin_upstream_delete(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - match upstreams::remove(p.get("slot").map(String::as_str).unwrap_or("")) { - Ok(slot) => { - util::log_notice(&format!("[api] 删除 LLM 上游 {slot}")); - http::ok(r, json!({ "slot": slot }), "已删除") - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} - -/// 管理员:上游连通性测试(管理台「测试」按钮)。 -/// -/// 面板把 slot 放在 query(`?slot=x`),把**要发给上游的探测 JSON**(真实 -/// chat/completions 请求,max_tokens=1)放在请求体——模块对体零改写。 -/// 实现上校验 slot 存在后 `ngx_http_internal_redirect` 到管理 server 的 -/// 内部 location `/_upstream_test//v1/chat/completions`: -/// -/// * `proxy_pass $llm_target` 原样复用 LLM 链路的取 slot / 拼路径 / 换真 Key -/// 逻辑(见 `llm::slot_of` 对 `/_upstream_test` 前缀的识别); -/// * 请求体在 API 两段式读取里已经进 `r->request_body`,内部重定向后由 -/// proxy 原样转发; -/// * 上游的真实状态码与响应体直接透传给面板——通不通、错在哪个厂商, -/// 面板看到的就是厂商原话。 -/// -/// 为什么不能在浏览器里直测:上游真 Key 只存在服务端(面板只有掩码), -/// 且 lm 域名与 mdm 不同源,fetch 会被 CORS 拦住——测试必须在服务端发起。 -fn h_admin_upstream_test(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let slot_raw = http::params(r).get("slot").cloned().unwrap_or_default(); - let slot = match upstreams::validate_slot(&slot_raw) { - Ok(s) => s, - Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e), - }; - if !llm::upstream_live(&slot) { - return http::fail(r, HTTPStatus::NOT_FOUND, &format!("上游 {slot} 不存在")); - } - - // 与真实 OpenAI SDK 同形(必带 /v1)。1.0.42 曾改成裸 /chat/completions 绕智谱 - // NPE——那是 join_target 剥冗余 /v1 落地(1.0.41)之前的坑位,且把**无路径 base** - // 的 openai/deepseek 探测打去了 /chat/completions(真端点 404,回归组11 抓到)。 - // 1.0.43 用最小 dummy 体解决 NPE 后,这里恢复 /v1:base 带路径的厂商由 - // join_target 剥除(llm.rs 单测钉死),不带路径的原样保留。 - let uri = format!("/_upstream_test/{slot}/v1/chat/completions"); - util::log_notice(&format!("[api] 测试 LLM 上游 {slot}")); - - // ⚠️ nginx 的 internal_redirect 只**浅拷贝 ngx_str_t 结构体**(data 指针 + len), - // 不拷贝字节内容。若把栈上 String 的指针直接递过去,函数一返回堆内存就被回收, - // 之后 log 阶段读 r->uri 全是 NUL——健康标记、slot 解析全部静默失效(实测踩过)。 - // 必须把 URI 字节拷进**请求池**,让它活到请求结束。 - // - // SAFETY: r 有效;池内存在整个请求生命周期内有效。 - unsafe { - let pool = Pool::from_ngx_pool((*r).pool); - let bytes = uri.as_bytes(); - let data = pool.alloc(bytes.len()) as *mut u8; - if data.is_null() { - return http::fail(r, HTTPStatus::INTERNAL_SERVER_ERROR, "内存分配失败"); - } - core::ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len()); - - let mut u = nginx_sys::ngx_str_t { - len: bytes.len(), - data, - }; - let mut args = nginx_sys::ngx_str_t { - len: 0, - data: std::ptr::null_mut(), - }; - nginx_sys::ngx_http_internal_redirect(r, &mut u, &mut args); - } - - Status::NGX_DONE -} - -/// 管理员:LLM 用量总览(每人 / 每上游 / 合计)。 -/// -/// 口径是**请求数**,且只统计真正转发出去的请求(见 `stats` 模块头注释)。 -/// -/// **1.0.32**:在 `data.tokens` 下追加 `all_time` 块(自服务上线以来累计)—— -/// 来自 audit meta.jsonl 真源聚合,**不受 stats 落盘/恢复影响**(audit 是 -/// append-only,stats 会被 reset)。前端 `overview.html` 用此字段把"累计 -/// token"从模糊的"自上次 stats 恢复以来"换成清晰的"自上线以来"。 -fn h_admin_usage(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let now = util::now(); - let mut view = stats::overview(now); - if let Some(tok) = view.get_mut("tokens").and_then(|v| v.as_object_mut()) { - tok.insert("all_time".to_owned(), llm_audit::all_time_total()); - } - http::ok(r, view, "ok") -} - -// =========================================================================== -// 账号态 -// =========================================================================== - -/// 账号态:看看自己能用哪些上游。 -/// -/// 刻意只回显 slot / base_url / **模型清单**——**不回显任何密钥**(连脱敏都不给, -/// 上游密钥与调用方无关,没有知道的必要)。 -fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status { - // 回显的 base_url 必须带「调用方正在用的端口」,否则客户端照着拼会打不通 - let port = http::host_port(r); - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - - let items: Vec = upstreams::available(&user.allowed_models) - .into_iter() - .map(|(slot, up)| { - let (status, code, checked_at) = match upstreams::health(&slot) { - Some((ok, c, ts)) => (if ok { "ok" } else { "failed" }, c, ts), - None => ("unknown", 0, 0), - }; - json!({ - "slot": slot, - "base_url": upstreams::base_url(&slot, &up, &port), - "anthropic_base_url": upstreams::anthropic_base_url(&slot, &up, &port), - "models": up.models, - "note": up.note, - "status": status, - "status_code": if code > 0 { json!(code) } else { Value::Null }, - "checked_at": checked_at, - }) - }) - .collect(); - - http::ok(r, json!({ "items": items }), "ok") -} - -/// 账号态:列出自己**可用的模型**(上游登记的模型清单,按白名单过滤)。 -/// -/// 与 LLM 域名下的 `GET /v1/models` 是同一份数据:那边给 OpenAI SDK 用 -/// (形状 `{object:"list",data:[{id,...}]}`),这边给管理/自研客户端用 -/// (形状 `{code,msg,data}`,带 base_url 便于直接拼请求)。 -fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status { - // 同 h_my_upstreams:base_url 跟随调用方所用的端口 - let port = http::host_port(r); - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - - let available = upstreams::available(&user.allowed_models); - let mut items: Vec = Vec::new(); - for (slot, up) in &available { - for model in &up.models { - items.push(json!({ - "model": model, - "slot": slot, - "base_url": upstreams::base_url(slot, up, &port), - "note": up.note, - })); - } - } - - http::ok( - r, - json!({ - "items": items, - "upstreams": available - .iter() - .map(|(slot, up)| { - let (status, code, checked_at) = match upstreams::health(slot) { - Some((ok, c, ts)) => (if ok { "ok" } else { "failed" }, c, ts), - None => ("unknown", 0, 0), - }; - json!({ - "slot": slot, - "base_url": upstreams::base_url(slot, up, &port), - "anthropic_base_url": upstreams::anthropic_base_url(slot, up, &port), - "models": up.models, - "status": status, - "status_code": if code > 0 { json!(code) } else { Value::Null }, - "checked_at": checked_at, - }) - }) - .collect::>(), - }), - "ok", - ) -} - -/// 账号态:查自己的 LLM 用量(今日 / 累计 / 最近调用 + 分上游明细)。 -fn h_my_usage(r: *mut ngx_http_request_t, actor: Actor) -> Status { - let user = match user_or_401(r, actor) { - Ok(u) => u, - Err(e) => return e, - }; - - let now = util::now(); - let usage = stats::user(&user.username, now); - // token 计量(body filter 从上游 usage 抓的;抓不到 = 0,不补估) - let tin = stats::tokens_user(&user.username, now); - let models: Vec = stats::user_models(&user.username, now) - .into_iter() - .map(|(slot, u)| { - json!({ - "slot": slot, - "today": u.today, - "total": u.total, - "last_ts": u.last_ts, - "last_ts_text": if u.last_ts > 0 { util::fmt_time(u.last_ts) } else { String::new() }, - }) - }) - .collect(); - - http::ok( - r, - json!({ - "username": user.username, - "display_name": user.display_name, - "today": usage.today, - "total": usage.total, - "last_ts": usage.last_ts, - "last_ts_text": if usage.last_ts > 0 { util::fmt_time(usage.last_ts) } else { String::new() }, - "tokens": tin.json(), - "models": models, - // 限额一起回显:客户端可据此自我节流,不必等 429 - "limits": { "rpm": user.llm_rpm, "daily": user.llm_daily }, - }), - "ok", - ) -} - -// =========================================================================== -// 管理员:统计落盘 -// =========================================================================== -// -// 用量/趋势只活在共享内存里,进程退出即清零。落盘/恢复见 -// `crate::stats_persist` 模块注释与 docs/llm/STATS_PERSISTENCE.md。 -// 这两个端点不参与任何转发逻辑,纯粹是「让运维看得见 + 手动兜一次」。 - -/// `GET /api/admin/stats/persist`:快照文件状态 + 当前共享内存键数。 -/// -/// 排障用法:发版后立刻查一次,`restored_keys` 应当等于 `file_keys`; -/// 若 `live_keys` 长期远大于 `file_keys`(且 `saved_at` 很旧),说明周期落盘没跑。 -fn h_admin_stats_persist(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - http::ok(r, stats_persist::status(), "ok") -} - -/// `POST /api/admin/stats/dump`:立刻落盘一次(不看间隔)。 -/// -/// 计划内维护前手动兜一次用;正常情况下不需要 —— 发版走 `exit_process` 钩子 -/// 已经是零丢失,这个端点只是给个「我说了算」的把手。 -fn h_admin_stats_dump(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - match stats_persist::dump_now("admin") { - Ok(n) => http::ok(r, json!({ "keys": n }), "已落盘"), - Err(e) => http::fail(r, HTTPStatus::INTERNAL_SERVER_ERROR, &e), - } -} - -/// 列出审计目录所有有 `meta.jsonl` 的日期(按新→旧排序) -fn h_admin_audit_days(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let days = llm_audit::list_days(); - http::ok(r, json!({ "days": days, "count": days.len() }), "ok") -} - -/// 读某天 meta.jsonl 的所有行(剔除 excerpt / body_hash,**仅供管理台聚合**)。 -/// -/// 1.0.27 起管理台「小时级趋势」直接读这个端点 → 前端按 `ts / 3600` 分组聚合。 -/// 不在网关做 hour-bucket 持久化:避免给热路径加写、避免新增 shm 键空间。 -fn h_admin_audit_day(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let day = match http::params(r).get("day") { - Some(d) => d.trim().to_owned(), - None => return http::fail(r, HTTPStatus::BAD_REQUEST, "缺少 day 参数(YYYY-MM-DD)"), - }; - match llm_audit::read_day(&day) { - Ok(rows) => { - let n = rows.len(); - http::ok(r, json!({ "day": day, "rows": rows, "count": n }), "ok") - } - Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), - } -} diff --git a/rust/crates/plugin-llm/src/config.rs b/rust/crates/plugin-llm/src/config.rs deleted file mode 100644 index 2bcc0ee..0000000 --- a/rust/crates/plugin-llm/src/config.rs +++ /dev/null @@ -1,249 +0,0 @@ -//! LLM 插件策略常量(自内核 config.rs 迁入;改策略只改这里)。 - -/// LLM 上游 slot 名长度上限(`/openai/v1/chat/completions` 里的 `openai`) -pub const UPSTREAM_SLOT_MAX_LEN: usize = 32; -/// LLM 上游基址长度上限(含 scheme 与可选端口) -pub const UPSTREAM_BASE_MAX_LEN: usize = 200; -/// 单个模型名长度上限 -pub const MODEL_NAME_MAX_LEN: usize = 128; -/// 单个上游可登记的模型数量上限(挡住把整个模型库灌进 shm/JSON 的手滑) -pub const UPSTREAM_MODELS_MAX: usize = 200; - -// =========================================================================== -// 共享内存键前缀(`up:` `ust:` `ums:` `utt:` `utms:` `ud:` `udu:` `uds:` `uh:`) -// =========================================================================== - -/// 键前缀:LLM 上游 slot → `base \n auth`。 -/// -/// `base` 含 scheme(如 `https://api.openai.com`);`auth` 是完整的 -/// Authorization 头值(如 `Bearer sk-xxx`)。两者合用一个 shm 键, -/// 使写操作只需同步一处、少一个漏同步的机会。 -pub const KEY_UP_PREFIX: &[u8] = b"up:"; -/// 键前缀:LLM **每用户**用量统计(口径 = 已放行的请求)。 -/// -/// 键 = `ust:`,value = `<日窗id>:<今日数>:<累计数>:<最近调用时间戳>`。 -/// 与 `st:`(配额执行,先增后判)的区别:这里统计的是**真的转发出去**的请求 -/// ——两者在超限时会差几个数,属预期。 -pub const KEY_UST_PREFIX: &[u8] = b"ust:"; -/// 键前缀:LLM **每用户 × 每上游**用量统计。 -/// -/// 键 = `ums::`(username 与 slot 都不含 `:`,切分安全), -/// value 同 `ust:`。管理台的「每个模型用量」就是把这些键按 slot 聚合出来的。 -pub const KEY_UMS_PREFIX: &[u8] = b"ums:"; - -/// LLM token 计量(body filter 从上游响应抓的 usage): -/// 账号维度 `utt:`,账号×上游维度 `utms::` -pub const KEY_UTT_PREFIX: &[u8] = b"utt:"; -pub const KEY_UTMS_PREFIX: &[u8] = b"utms:"; - -/// 全局日桶。键 = `ud:<日窗id>` -pub const KEY_UDAY_PREFIX: &[u8] = b"ud:"; - -/// 1.0.32:每用户聚合缓存(增量 + 持久化),给 /api/v1/me 免去每次都从 audit -/// meta.jsonl 全聚合的开销。键 = `usum:`,value = JSON -/// `{calls, tin, tout, by_slot: {slot: {...}}, last_ts}`。dump 走 -/// stats_persist 的 PREFIXES 白名单,**自动落 llm-stats.json**,**自动恢复**。 -/// -/// 设计要点: -/// - 增量聚合:每次 user_recent 只扫 audit `ts > last_ts` 的 delta 行,累加 -/// 到 shm → O(delta) 而非 O(7d 全量) -/// - 持久化:worker 退出时 dump 到 llm-stats.json,新 worker 启动时 restore -/// → 跨重启不丢历史 -/// - 字段语义:calls/tin/tout 是**自上次 stats_persist 恢复以来的累计** -/// (audit meta 行是 append-only,sum 跟着 append 走)。 -/// - hour_24h 不在这里——必须按 ts 过滤,无法 O(1),仍每次重算 -pub const KEY_USER_SUM_PREFIX: &[u8] = b"usum:"; -/// 按账号日桶。键 = `udu::<日窗id>` -pub const KEY_UDAY_USER_PREFIX: &[u8] = b"udu:"; -/// 按上游(API 组)日桶。键 = `uds::<日窗id>` -/// (apix 的 slug 带 `api/` 前缀共用这片键空间,见 core config 的 APIX_STATS_PREFIX) -pub const KEY_UDAY_SLOT_PREFIX: &[u8] = b"uds:"; - -/// 键前缀:上游**账号级**日桶(负载分布)。键 = `uda:::<日窗id>`, -/// value = DayBucket(requests = 转发到该账号的请求数,tin/tout = 该账号 token 消耗)。 -/// 与 `uds:`(slot 级)同构、多一维账号下标——「各上游 Key 负载」就靠它, -/// 无需数据库(项目红线)。TTL 90 天自然淘汰。 -pub const KEY_UDAY_ACCT_PREFIX: &[u8] = b"uda:"; - -/// 键前缀:上游账号**额度耗尽次数**日桶。键 = `uex:::<日窗id>`, -/// value = DayBucket(requests = 该账号被判定额度耗尽并冷却的次数)。 -/// 仅作问题发现指标、不参与路由——哪把 Key 频繁撞额度一眼可见。TTL 90 天。 -pub const KEY_UEX_PREFIX: &[u8] = b"uex:"; - -/// 管理台账号级负载迷你趋势的展示天数(与项目 `uds:` 趋势的默认 14 天一致)。 -pub const ACCT_TREND_DAYS: i64 = 14; - -/// 键前缀:LLM **上游健康缓存**。键 = `uh:`, -/// value = `<1|0> \n <上游状态码> \n <记录时刻>`。 -/// -/// 由 log 阶段处理器在每次真实转发完成后写入(见 `handlers::LlmHealthHandler`), -/// 管理台「测试」按钮的探测请求走同一条转发链路,因此也天然更新这份数据。 -/// 30 天过期只是兜底——它随时会被下一次转发覆盖;删除上游时不必清理, -/// 列表接口只对现存 slot 展示。**这是纯展示缓存,不参与任何转发决策**, -/// 最多容忍客户端中途断连导致的误标(下一次真实转发即纠正)。 -pub const KEY_UH_PREFIX: &[u8] = b"uh:"; - -// =========================================================================== -// 多账号路由(一个 slot 挂多个上游账号,粘性绑定 + 额度耗尽自动切换) -// =========================================================================== - -/// 键前缀:上游账号静态配置。键 = `upa::`, -/// value = ` \n `(`resets` 为空则第二段为空)。 -/// -/// `auth` 是完整 Authorization 头值;`resets` 是该账号额度重置周期(秒,逗号分隔)。 -/// 与 `up:` 拆开存,是因为账号是热路径要枚举的对象、而 `up:` 只存 base + 账号数, -/// 避免一次大 payload 反序列化;账号数记在 `up:` 第二段,热路径据此循环读取。 -pub const KEY_UPA_PREFIX: &[u8] = b"upa:"; - -/// 键前缀:上游账号「额度耗尽」冷却(**运行时状态**,非配置)。 -/// 键 = `uac::`,value = `::`(绝对/相对秒)。 -/// -/// 探测恢复闭环(1.0.44):冷却不再是「猜厂商重置点」,而是**指数退避 + 真实请求探测**—— -/// * `backoff`:本轮退避长度(首标 = `ipass_probe_min`,每次探测后翻倍,封顶 `ACCT_COOL_HARD_CAP`); -/// * `next_probe_at`:到点后允许一个**无绑定/绑定失效**的真实请求把它当探测路由过去 -/// (`pick_account` 选中即 re-arm,窗口不重复开); -/// * `hard_cap`:键 TTL。到期未验证成功 = 无条件回队,若仍耗尽会被真实流量再标——自愈。 -/// -/// 探测 2xx → log 阶段 `clear_cool` 删键回队。旧格式单段 `` 值解析失败会被视作 -/// 「立即到期可探测」,升级后 ~30 分钟内自然换代。 -pub const KEY_UAC_PREFIX: &[u8] = b"uac:"; - -/// 键前缀:用户 key → 上游账号 粘性绑定。键 = `ukb:`, -/// value = ``(usize 字符串)。 -/// -/// 带 TTL(`AFFINITY_TTL_DEFAULT`,7 天),**尽力粘性**:重启后丢失、重新绑定, -/// 符合「尽可能同一用户 key 用同一上游 key」的语义。只有真正选了账号且拿到下行 key -/// 时才写;内部测试 location(无下行 key)不写,避免污染绑定表。 -pub const KEY_UKB_PREFIX: &[u8] = b"ukb:"; - -/// 键前缀:上游级「额度耗尽」探测配置。键 = `upe:`, -/// value = ` \n `(`markers` 是 JSON 字符串数组)。 -/// -/// slot 级(同一厂商所有账号共享错误形态),由 `sync_shm` 写、body filter 与 log 阶段读。 -pub const KEY_UPE_PREFIX: &[u8] = b"upe:"; - -/// 默认「额度耗尽」HTTP 状态码(命中即判该账号额度耗尽并冷却)。 -/// -/// 402/429/403 是主流厂商表达「配额/额度/权限不足」的常态;5xx 与连接失败(502/504) -/// 视为机器/链路问题、**不**算额度耗尽(否则单台抖动会把整池账号都冷却掉)。 -pub const EXHAUST_STATUS_DEFAULT: &[i64] = &[402, 429, 403]; - -/// 首轮探测退避的默认值(秒)。可被 `ipass_probe_min <秒>` 指令覆盖(见 gateway 模块壳)。 -/// -/// 冷却生命周期 = 指数退避:首轮 `probe_min`,每次探测后翻倍,封顶 `ACCT_COOL_HARD_CAP`。 -/// 60s 起步对「限速 429(≠额度耗尽)」足够友好;对真·额度耗尽也只是多牺牲几个探测请求。 -pub const PROBE_MIN_DEFAULT: i64 = 60; - -/// 退避翻倍与冷却硬顶(秒,30 分钟)。 -/// -/// 语义是**退避/存在期**,不是「恢复时刻」——恢复由探测闭环判定(2xx 即 `clear_cool`), -/// 硬顶到期只表示「放弃未验证的持续锁定,账号回队,若仍耗尽会被真实流量再标」(自愈)。 -/// -/// 旧口径是「天锚定重置边界」(`cool_until_until`):它把厂商窗口算错过两次(1.0.14 的 -/// 28800、1.0.24 的 0——后者误以为容器 TZ 会影响纯 epoch 算术,边界错开 8h, -/// 造成「MiniMax 15:00 重置、网关锁到 18:00」的线上事故),1.0.44 起整个删除, -/// `resets` 配置不再参与路由。**不要再往回加边界计算。** -pub const ACCT_COOL_HARD_CAP: i64 = 30 * 60; - -/// 粘性绑定 TTL 兜底(秒):账号 `resets` 全空时绑定的最长保留时长(7 天)。 -pub const AFFINITY_TTL_DEFAULT: i64 = 7 * 86400; - -// =========================================================================== -// 趋势 -// =========================================================================== - -/// 日桶保留时长(秒):90 天。到期由共享内存自然淘汰(`get_live` 视为不存在), -/// 趋势接口默认只取最近 14 天,留足余量供改大区间。 -pub const TREND_KEEP_SECS: i64 = 90 * 86400; -/// 趋势接口默认 / 最大天数(防止 URL 里塞个 99999 把共享内存扫爆) -pub const TREND_DAYS_DEFAULT: i64 = 14; -pub const TREND_DAYS_MAX: i64 = 90; - -// =========================================================================== -// 对话审计(log 阶段旁路落盘,docs/llm/AUDIT_LOG.md) -// =========================================================================== - -/// 审计目录名:默认挂在 data 目录的**父目录**下(即与 data 同级),可被 -/// `ipass_audit_dir` 指令整体覆盖 -pub const AUDIT_DIR_NAME: &str = "audit"; -/// 审计总量上限(字节):超过即按日期目录从旧到新整目录删除,直到回落限内。 -/// **1.0.18 起默认 1 GB**(旧 10 GB)——审计从「全量请求体」改为「meta + 100 字节摘要」, -/// 单条 ~400 B,1 GB 够撑约 250 万条(约 7 天 @ QPS 4);旧 10 GB 在新口径下等于 -/// 「永远不会被 purge」的反向证据,留着只是误导容量判断。 -pub const AUDIT_MAX_BYTES: u64 = 1024 * 1024 * 1024; -/// 容量巡检最小间隔(秒):巡检要扫整个审计树,节流避免高频 IO -pub const AUDIT_PURGE_MIN_INTERVAL: i64 = 60; -/// 请求体摘录字节数(meta 同一行的 `excerpt` 字段): -/// **1.0.27 起改为智能摘录**——短请求体(≤ `AUDIT_EXCERPT_BYTES`)保留原文; -/// 长请求体(> `AUDIT_EXCERPT_BYTES`)解析 JSON 后提取 `model` + -/// 最后一个 user message 的 `content`(按 `AUDIT_CONTENT_CHARS` 截前 N 字符), -/// 输出 `{"model":"…","content":"…"}`。 -/// -/// 设计取舍:大 body(带 tools / 多轮 history / context_management)只看 -/// 前 100 字节 = 几乎全是结构噪声,看不到用户实际问啥。智能摘录**跳过 -/// context_management / tools / temperature** 等结构字段,**优先把 -/// 位置留给 user content**——对 100 KB+ 大请求也能复盘「当时问了啥」。 -/// -/// 想看全文就别存 audit,走 hot path 日志或实时抓包。 -pub const AUDIT_EXCERPT_BYTES: usize = 100; -/// 智能 excerpt 中 user content 字符上限(按 Unicode scalar value 算)。 -/// 200 字 ≈ 100 token,够支撑「用户问了啥」的诊断;超长 prompt 截前 200 字。 -pub const AUDIT_CONTENT_CHARS: usize = 200; -/// 长请求体解析 JSON 时最多读前 N 字节。165 KB 那种大 body 不会全读进内存。 -pub const AUDIT_JSON_PARSE_BYTES: usize = 16 * 1024; -/// `body_hash` 字段截断字节数(hex):完整 SHA256 64 字符太长,复盘只需要够用即可; -/// 16 hex = 64 bit 抗碰撞,远超 audit「识别同一请求」的需求。 -/// ⚠️ 哈希对象是**请求体全文**(流式喂入,不整份读进内存),不是 excerpt —— 只哈希 -/// 前 N 字节会因所有请求开头相同而全量碰撞。 -pub const AUDIT_HASH_HEX_BYTES: usize = 16; - -// =========================================================================== -// 统计落盘 / 恢复(docs/llm/STATS_PERSISTENCE.md) -// =========================================================================== - -/// 运行期统计的快照文件名(落在 `data/` 下,与其他状态文件同级)。 -/// -/// **不含任何凭据**(只有账号名 / slot 名 / 计数 / token 数),但仍在 `data/` 里 -/// ——`data/` 整体 gitignore,别把它单独挪到别处再误提交。 -pub const STATS_FILE_NAME: &str = "llm-stats.json"; - -/// 周期性落盘间隔(秒)。**默认 60**,可被 `ipass_stats_persist <秒>` 覆盖, -/// `0` = 关闭周期落盘(只靠 worker 退出钩子那一次)。 -/// -/// 取 60 的理由:单次落盘要扫 10 个前缀 + 序列化,量级是毫秒;60 秒一次对磁盘与 -/// 热路径都可忽略,而「最多丢 60 秒统计」远好于「丢全部历史」(1.0.18 之前的实际行为)。 -/// 真要保零丢失也不靠调小它 —— 靠 worker 退出钩子(见 `stats_persist` 模块注释)。 -pub const STATS_PERSIST_INTERVAL_DEFAULT: i64 = 60; - -/// 快照格式版本。将来改结构时用它做兼容判断(版本对不上就**不恢复**, -/// 宁可从零开始,也不要把语义不明的旧数据灌进共享内存)。 -pub const STATS_SNAPSHOT_VERSION: i64 = 1; - -/// 键前缀:统计落盘模块的「上次恢复」记录(键 = `spr:last`, -/// value = `<时刻> \x1f <恢复键数> \x1f <快照键数>`)。 -/// -/// **刻意不在 `stats_persist::PREFIXES` 白名单里** —— 它是恢复过程的元信息, -/// 不是统计量,被持久化只会自我引用。 -/// -/// 为什么要落共享内存而不是进程内静态量:`init_process` 是**每 worker 跑一次**, -/// 首个 worker 恢复完、后续 worker 因 put-if-absent 全部跳过,各自计数天然不同 -/// —— 管理接口落到哪个 worker 就报哪个数,运维看到的是随机值。放共享内存后 -/// 所有 worker 报同一份,`/api/admin/stats/persist` 才可信。 -pub const KEY_SPR_PREFIX: &[u8] = b"spr:"; - -/// 键前缀:统计落盘模块的「上次落盘」记录(键 = `spd:last`,value = `<时刻>`)。 -/// -/// 与 `KEY_SPR_PREFIX` **同类问题的同一解法**:落盘是每个 worker **各自发生** -/// 的(`maybe_dump` 由记账路径驱动,每个 worker 独立计数),记录若放进程内 -/// 静态量,`/api/admin/stats/persist` 落到哪个 worker 就报哪个 worker 自己的 -/// 时刻 —— 刚落下盘的 worker 报「刚刚」,另一个没落过的报「还没落过」, -/// 运维看到的是随机值(1.0.18 线上实测就是这个现象)。 -/// -/// 落在共享内存里一次解决两件事: -/// * **上报一致**:所有 worker 读同一个时刻; -/// * **节流收敛**:`maybe_dump` 慢路径查这一条,把「8 个 worker 各写一遍文件」 -/// 收敛成「全局每间隔一次」。见 `stats_persist::maybe_dump`。 -/// -/// 同样**刻意不在 `stats_persist::PREFIXES` 白名单里**(不是统计量,持久化只会 -/// 自我引用)。 -pub const KEY_SPD_PREFIX: &[u8] = b"spd:"; diff --git a/rust/crates/plugin-llm/src/handlers.rs b/rust/crates/plugin-llm/src/handlers.rs deleted file mode 100644 index b47450b..0000000 --- a/rust/crates/plugin-llm/src/handlers.rs +++ /dev/null @@ -1,347 +0,0 @@ -//! LLM 插件的 phase handler:access 转发决策、log 阶段健康标记与对话审计。 -//! -//! 插件化改造点(相对 gateway 旧版行为等价): -//! * location 开关改走内核契约 `loc::enabled(r, Switch::Llm)`——插件不认识 -//! gateway 私有的 `ModuleConfig`; -//! * 下行鉴权(取 Key / 查身份 / 限流配额 / OpenAI 兼容错误体)用内核 -//! `ipass_core::auth`(LLM 与 apix 两条下行链路共用); -//! * `$upstream_status` 变量索引的预取从 gateway postconfiguration 迁入本插件 -//! 的 postconfig(见 [`prefetch_upstream_status_index`])。 - -use core::sync::atomic::{AtomicIsize, Ordering}; - -use ngx::core::{Status, NgxStr}; -use ngx::ffi::{ - ngx_conf_t, ngx_http_get_indexed_variable, ngx_http_get_variable_index, - ngx_http_request_t, ngx_str_t, ngx_uint_t, -}; -use ngx::http::{HTTPStatus, HttpPhase, HttpRequestHandler, Request}; - -use crate::llm; -use crate::llm_audit; -use crate::stats; -use crate::upstreams; -use crate::usage_filter; -use ipass_core::{auth, http, loc, util}; -use ipass_core::loc::Switch; - -// =========================================================================== -// LLM 代理热路径(access) -// =========================================================================== - -/// LLM 代理热路径:鉴权 → 校验上游与白名单 → 放行。 -/// -/// 刻意**只做决策、不搬数据**:真正的转发交给 nginx 的 proxy 模块, -/// 于是 SSE 流式、`proxy_buffering`、上游超时全部是 nginx 原生行为, -/// 不需要自己实现 upstream——那正是「按 body 里的 model 路由」代价高昂之处。 -/// -/// 变量(`$llm_target` / `$llm_host` / `$llm_auth`)不在这里赋值, -/// 而由 `llm::var_get` 在 `proxy_pass` 求值时计算,与 `$proxy_backend_addr` 同构。 -pub struct LlmHandler; - -impl HttpRequestHandler for LlmHandler { - const PHASE: HttpPhase = HttpPhase::Access; - type Output = Status; - - fn handler(request: &mut Request) -> Self::Output { - let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t; - if !loc::enabled(r, Switch::Llm) { - return Status::NGX_DECLINED; - } - - let Some(slot) = llm::slot_of(r) else { - return auth::fail( - r, - HTTPStatus::BAD_REQUEST, - "invalid_request_error", - "路径缺少上游段,正确形式:/<上游名>/v1/chat/completions", - ); - }; - - let Some(key) = auth::downstream_key(r) else { - return auth::fail( - r, - HTTPStatus::UNAUTHORIZED, - "invalid_api_key", - "缺少 API Key,请在 Authorization: Bearer 或 x-api-key 中携带", - ); - }; - - // 只碰共享内存:这里若改成 users::find_by_key 会每请求读一次 users.json - let Some((username, allowed, rpm, daily)) = auth::identity(&key) else { - util::log_warn(&format!( - "[llm] API Key 校验失败 slot={slot} client={}", - http::remote_addr(r) - )); - return auth::fail( - r, - HTTPStatus::UNAUTHORIZED, - "invalid_api_key", - "API Key 无效或已失效", - ); - }; - - // 「列出可用模型」是网关自己的元信息接口(OpenAI 兼容形状), - // 在查上游之前就地应答——否则 `/v1/models` 会被当成 slot=v1 → 404。 - if llm::is_models_probe(r) { - return llm::respond_models(r, &allowed); - } - - let upstream_live = llm::upstream_live(&slot); - if !upstream_live { - return auth::fail( - r, - HTTPStatus::NOT_FOUND, - "model_not_found", - &format!("上游 {slot} 不存在或已停用"), - ); - } - - // 空白名单 = 不限(老账号没有该字段时的语义) - if !allowed.is_empty() && !allowed.iter().any(|s| s == &slot) { - util::log_warn(&format!("[llm] {username} 无权访问上游 {slot}")); - return auth::fail( - r, - HTTPStatus::FORBIDDEN, - "permission_denied", - &format!("当前账号无权访问上游 {slot}"), - ); - } - - // 限流与每日配额(先增后判,纯共享内存)。放在最后: - // 鉴权/白名单都没过的请求不该消耗对方的计数 - // SAFETY: r 由 nginx 传入且有效 - if let Some(resp) = unsafe { auth::check_quota(r, &username, rpm, daily) } { - return resp; - } - - // 用量统计:走到这里说明请求**真的会被转发**,才计入。 - // 被拒的请求(401/403/404/429)不进统计——那是给人看的数字, - // 把拒绝也算进"用量"只会误导(与配额计数 st: 的语义刻意不同)。 - stats::record(&username, &slot, util::now()); - - Status::NGX_DECLINED - } -} - -// =========================================================================== -// log 阶段:上游健康标记 -// =========================================================================== - -/// `$upstream_status` 的变量索引,postconfiguration 时解析一次(log 阶段复用)。 -static UPSTREAM_STATUS_IDX: AtomicIsize = AtomicIsize::new(-1); - -/// `$llm_acct` 的变量索引(本插件注册),log 阶段读它拿到「本次请求打到的上游账号下标」, -/// 用于额度耗尽时精确标记那一个账号进入冷却。postconfiguration 时解析一次。 -static LLM_ACCT_IDX: AtomicIsize = AtomicIsize::new(-1); - -/// postconfiguration:预取 `$upstream_status` 与 `$llm_acct` 变量索引。 -/// 失败返回 false,gateway 会 EMERG 退出。 -pub fn prefetch_upstream_status_index(cf: *mut ngx_conf_t) -> bool { - let mut status_name = ngx_str_t { - len: "upstream_status".len() as usize, - data: b"upstream_status\0".as_ptr() as *mut u8, - }; - // SAFETY: cf 由 nginx 传入且有效;name 指向栈上合法 ngx_str_t - let sidx = unsafe { ngx_http_get_variable_index(cf, &mut status_name) }; - if sidx == -1 { - return false; - } - UPSTREAM_STATUS_IDX.store(sidx as isize, Ordering::Relaxed); - - let mut acct_name = ngx_str_t { - len: "llm_acct".len() as usize, - data: b"llm_acct\0".as_ptr() as *mut u8, - }; - // SAFETY: 同上;本插件已在 preconfiguration 用 add_var 注册过 llm_acct - let aidx = unsafe { ngx_http_get_variable_index(cf, &mut acct_name) }; - if aidx == -1 { - return false; - } - LLM_ACCT_IDX.store(aidx as isize, Ordering::Relaxed); - true -} - -/// LLM 上游健康标记(log 阶段):把上游**真实**状态码写进 `uh:` 缓存。 -/// -/// 为什么在 log 阶段:转发决策在 access、响应在 proxy 模块手里,只有 log -/// 阶段能同时拿到「slot 是谁」和「上游实际回了什么」。这相当于让**真实流量 -/// 自己当探针**——不需要额外的常驻探测进程(项目红线),健康状态也最真实。 -/// 管理台「测试」按钮的探测请求走同一条 `proxy_pass` 链路,天然共用本逻辑。 -/// -/// 写下的缓存**只用于展示**(列表标红 / 模型发现附状态),转发路径不读它, -/// 误标不影响任何请求的结果。 -/// -/// ⚠️ 状态码从 `$upstream_status` 变量读,**不能读 `r->upstream`**—— -/// `ngx_http_upstream_finalize_request` 在日志阶段之前就把 `r->upstream` -/// 置空了(实测踩过:代码编译通过、跑起来状态永远是 unknown)。 -/// access log 模块同款手法:变量在 finalize 时快照,log 阶段仍有效。 -pub struct LlmHealthHandler; - -impl HttpRequestHandler for LlmHealthHandler { - const PHASE: HttpPhase = HttpPhase::Log; - type Output = Status; - - fn handler(request: &mut Request) -> Self::Output { - let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t; - - // 两类请求要标记: - // ① LLM server 的真实转发流量(Switch::Llm 开启); - // ② 管理台探测流量——内部重定向到管理 server 的 /_upstream_test location, - // 那里**没有** ipass_llm 指令(有就会重跑 access 鉴权,探测请求只有 - // 登录会话没有 API Key),所以只能按 URI 前缀认。 - let uri = http::uri(r); - let is_llm = loc::enabled(r, Switch::Llm); - let is_probe = uri.starts_with("/_upstream_test"); - if !is_llm && !is_probe { - return Status::NGX_DECLINED; - } - - // SAFETY: r 有效;索引在 postconfiguration 已解析(见 prefetch_upstream_status_index) - let code = unsafe { - let idx = UPSTREAM_STATUS_IDX.load(Ordering::Relaxed); - if idx < 0 { - 0 - } else { - let v = ngx_http_get_indexed_variable(r, idx as ngx_uint_t); - if (*v).not_found() != 0 || (*v).data.is_null() { - 0 - } else { - let s = core::str::from_utf8(core::slice::from_raw_parts( - (*v).data, - (*v).len() as usize, - )) - .unwrap_or(""); - // 内部重定向会有多段(如 "502, 200"),取**最后一段** = 最近一次上游尝试 - s.rsplit(',').next().unwrap_or("").trim().parse().unwrap_or(0) - } - } - }; - // 没有 upstream(本地应答如 /v1/models)或没拿到状态码(客户端中途 - // 断连等)→ 无从判断,不写缓存,留给下一次真实转发 - if code == 0 { - return Status::NGX_DECLINED; - } - - if let Some(slot) = llm::slot_of(r) { - let now = util::now(); - // 上游回了头 = 它活着:2xx~4xx 都算正常(4xx 是它对我们请求的意见, - // 不是机器死了);5xx / 连接失败(502) / 等头超时(504) 才算失效 - upstreams::mark_health(&slot, (200..500).contains(&code), code); - - // 账号级负载:每次真实转发都记到选中的上游账号(负载分布 / 问题发现)。 - // 只在这里记一次(read_llm_acct 触发一次 pick_account),绝不放在 - // pick_account 里——否则 $llm_target/$llm_host/$llm_auth/$llm_acct - // 四个变量各触发一次,会把同一请求记好几遍。 - if let Some(idx) = read_llm_acct(r) { - upstreams::bump_acct_load(&slot, idx, now); - - // 额度耗尽检测:状态码命中 或 响应体命中标记 → 标记该账号冷却 + 耗尽计数 +1。 - // 状态码来自 `$upstream_status`,标记来自 body filter 的 `take_exhausted` - // (流式也能扫到)。 - // - // 探测闭环(1.0.44)的两半边都在这里合流(log 阶段每请求只跑一次, - // 这是副作用只能放这的原因——pick 会被四个变量求值多次): - // * 耗尽信号(状态码或 body marker)→ mark_exhausted:新账号开冷却; - // 冷却中且窗口已到期(= 探测失败)→ re-arm 翻倍关窗;未到期 - // (best-effort 撞墙)→ 时间线不动; - // * 2xx 且无耗尽信号 → clear_cool:冷却中的账号被探测/转发验证恢复, - // 立即清键回队(健康账号无键,remove 返回 false、静默)。 - // - // is_probe(管理台「测试」按钮的内跳)**不写冷却/耗尽**—— - // 测试账号由 pick 随机选,上游回耗尽形态只说明「这个请求过不了」, - // 不能据此认定账号已耗尽。线上事故:13:50 三把好 key 被面板测试轮 - // 点进冷却,整池不可用,恢复要走完整退避。mark_health 与 - // bump_acct_load 照常(测试也是真转发,有统计意义); - // 2xx clear_cool 也保留——测试成功 = 该账号此刻健康(正向证据)。 - let (status_list, _) = upstreams::read_upe(&slot); - let status_exhausted = status_list.iter().any(|&s| s == code); - let body_exhausted = usage_filter::take_exhausted(r); - if !is_probe && (status_exhausted || body_exhausted) { - upstreams::mark_exhausted(&slot, idx, now); - upstreams::bump_acct_exhaust(&slot, idx, now); - } else if (200..300).contains(&code) { - upstreams::clear_cool(&slot, idx); - } - } - } - - Status::NGX_DECLINED - } -} - -/// 从 `$llm_acct` 索引读本次请求选中的上游账号下标(NOCACHEABLE,读时重算,确定一致)。 -/// -/// 读不到(未求值/解析失败)返回 None——不标记,避免误伤。 -fn read_llm_acct(r: *mut ngx_http_request_t) -> Option { - let idx = LLM_ACCT_IDX.load(Ordering::Relaxed); - if idx < 0 { - return None; - } - // SAFETY: 索引在 postconfiguration 已解析;变量为 NOCACHEABLE,读即触发 get_handler - unsafe { - let v = ngx_http_get_indexed_variable(r, idx as ngx_uint_t); - if v.is_null() || (*v).not_found() != 0 || (*v).data.is_null() { - return None; - } - let s = core::str::from_utf8(core::slice::from_raw_parts( - (*v).data, - (*v).len() as usize, - )) - .ok()?; - s.trim().parse::().ok() - } -} - -// =========================================================================== -// log 阶段:对话审计 + token 入账 -// =========================================================================== - -/// LLM 对话审计(log 阶段):把被转发请求的 body 临时文件复制进审计目录。 -/// -/// 捕获机制与清理策略见 `llm_audit` 模块注释与 docs/llm/AUDIT_LOG.md。 -/// 关键点:只依赖 `client_body_in_file_only clean` 留下的临时文件,本阶段 -/// **不读请求体、不碰内存缓冲**——热路径与 SSE 透传完全无感知。 -pub struct AuditHandler; - -impl HttpRequestHandler for AuditHandler { - const PHASE: HttpPhase = HttpPhase::Log; - type Output = Status; - - fn handler(request: &mut Request) -> Self::Output { - let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t; - if !loc::enabled(r, Switch::Llm) { - return Status::NGX_DECLINED; - } - - // 身份在 log 阶段重新解析:ukey shm 查询是纯内存读,代价可忽略; - // 换取 access→log 之间不挂任何请求级状态(无 ctx、无生命周期问题)。 - // 请求已被转发过(有临时文件)才查得到 Key——被拒请求天然不入档。 - let user = auth::downstream_key(r) - .and_then(|k| auth::identity(&k)) - .map(|(u, _, _, _)| u); - let slot = llm::slot_of(r).unwrap_or_default(); - - // token 计量:body filter 旁路抓到的 usage(见 usage_filter 模块注释) - let (tin, tout) = usage_filter::take(r); - if tin > 0 || tout > 0 { - if let Some(name) = user.as_deref() { - stats::add_tokens(name, &slot, tin, tout, util::now()); - } - // 账号级 token 负载(与账号级请求数同一个日桶维度 uda:) - let now = util::now(); - if let Some(idx) = read_llm_acct(r) { - upstreams::add_acct_tokens(&slot, idx, tin as i64, tout as i64, now); - } - } - - // SAFETY: 同上;capture 内部任何失败只记日志,绝不 panic - unsafe { llm_audit::capture(r, user.as_deref().unwrap_or("-"), &slot, tin, tout) }; - - Status::NGX_DECLINED - } -} - -// 类型占位:NgxStr 在本 crate 被引用但暂未直接使用,保留编译期链接检查 -#[allow(dead_code)] -fn _type_witness(_: &NgxStr) {} diff --git a/rust/crates/plugin-llm/src/lib.rs b/rust/crates/plugin-llm/src/lib.rs deleted file mode 100644 index ada91ce..0000000 --- a/rust/crates/plugin-llm/src/lib.rs +++ /dev/null @@ -1,83 +0,0 @@ -//! plugin-llm —— LLM API 代理插件。 -//! -//! 职责:`//...` → 上游转发决策、上游 CRUD、OpenAI 兼容模型发现、 -//! `rl:`/`st:` 配额执行、token 计量(body filter 旁路)、用量统计与趋势、 -//! 对话审计落盘、运行期统计落盘/恢复。数据文件 `data/llm-upstreams.json` -//! (敏感级别等同 users.json)与 `data/llm-stats.json`(统计快照,无凭据)。 -//! -//! ## 装配点(gateway) -//! -//! * `preconfig` —— 注册 `$llm_target` / `$llm_host` / `$llm_auth` 三件套 -//! * `postconfig` —— 注册 LlmHandler(access)/ LlmHealthHandler、AuditHandler -//! (log)/ body filter 链头插入(token 计量)/ 预取 `$upstream_status` 索引 -//! * `zone_load` —— 全量装载 `up:` 上游表 -//! * `dispatch_api` —— 认领上游 / 用量类端点(趋势端点留 gateway,需三方名册) -//! -//! ## 进程生命周期钩子(由 gateway 调用,不是插件自己的钩子) -//! -//! * `stats_persist::restore()` 在 `init_process` —— 重启后把统计快照灌回共享内存 -//! * `stats_persist::dump_now()` 在 `exit_process` —— 退出前落盘,发版不丢统计 -//! -//! ## 与其他插件的关系 -//! -//! 下行鉴权与配额执行(`ukey:` / `rl:` / `st:`)在内核 `ipass_core::auth`; -//! 记账原语在内核 `ipass_core::usage`——apix 插件复用同一套下行契约, -//! 与本插件共用同一片用量键空间(apix 的 slot 带 `api/` 前缀天然归组)。 - -pub mod api; -pub mod config; -pub mod handlers; -pub mod llm; -pub mod llm_audit; -pub mod models; -pub mod stats; -pub mod stats_persist; -pub mod store; -pub mod upstreams; -pub mod usage_filter; - -use ngx::core::Status; -use ngx::ffi::{ngx_conf_t, ngx_http_request_t}; - -use ipass_core::plugin::Plugin; - -pub struct LlmPlugin; - -impl Plugin for LlmPlugin { - fn name(&self) -> &'static str { - "llm" - } - - fn preconfig(&self, cf: *mut ngx_conf_t) -> bool { - // SAFETY: cf 由 nginx 传入且有效 - unsafe { llm::preconfig(cf) } - } - - fn postconfig(&self, cf: *mut ngx_conf_t) -> bool { - // SAFETY: cf 由 nginx 传入且有效 - let cf = unsafe { &mut *cf }; - - // token 计量:插进 body filter 链头,旁路扫描上游响应的 usage - usage_filter::install(); - - if ngx::http::add_phase_handler::(cf).is_err() { - return false; - } - if ngx::http::add_phase_handler::(cf).is_err() { - return false; - } - if ngx::http::add_phase_handler::(cf).is_err() { - return false; - } - // 预取 `$upstream_status` 索引(upstream 模块 preconfiguration 已注册该内建变量) - handlers::prefetch_upstream_status_index(cf) - } - - fn dispatch_api(&self, r: *mut ngx_http_request_t) -> Option { - api::dispatch(r) - } - - fn zone_load(&self) -> usize { - upstreams::reload_shm() - } -} diff --git a/rust/crates/plugin-llm/src/llm.rs b/rust/crates/plugin-llm/src/llm.rs deleted file mode 100644 index 041bd65..0000000 --- a/rust/crates/plugin-llm/src/llm.rs +++ /dev/null @@ -1,458 +0,0 @@ -//! LLM API 代理热路径。 -//! -//! 客户端把 base_url 指向 `https://llm.yourdomain.com/`,用**与域名接口 -//! 同一套** API Key 鉴权,由网关换成上游真实密钥后转发: -//! -//! ```text -//! POST /openai/v1/chat/completions -//! │ Authorization: Bearer sk_<本网关签发的 Key> -//! ▼ -//! ① 取 URL 第一段 → slot = "openai" -//! ② 查 ukey: → 身份(纯内存,不读 users.json) -//! ③ 查 up: → 上游基址 + 上游真 Key(纯内存) -//! ④ 填 $llm_target/Host/Auth → 交给 proxy_pass 转发 -//! ``` -//! -//! ## 三条硬约束 -//! -//! * **鉴权绝不能走 `users::find_by_key`**。它每次都同步读 `users.json` 并全表 -//! JSON 解码——管理接口低频无所谓,但 LLM 每个推理请求都吃这个代价。按项目 -//! 实测口径(CLAUDE.md「性能基线」),这类同步文件 I/O 足以把吞吐打到 1/25。 -//! 所以身份预置在 `ukey:` 键里,热路径只碰共享内存。取 Key / 查身份 / -//! 限流配额 / OpenAI 兼容错误体已下沉内核 `ipass_core::auth`(LLM 与 apix -//! 两条下行链路共用)。 -//! * **access 阶段读不到请求体**,因此这里不解析 `model` 字段,上游由 URL 前缀 -//! 决定(原因见 `upstreams` 模块头注释)。 -//! * **错误体必须是 OpenAI 兼容格式**(`{"error":{...}}`)。客户端的 SDK 只认这个, -//! 项目自己的 `{"code":400,"msg":...}` 会被 SDK 报成「无法解析的响应」。 - -use core::ptr; - -use ngx::core::{Pool, Status}; -use ngx::ffi::{ - NGX_HTTP_VAR_CHANGEABLE, NGX_HTTP_VAR_NOCACHEABLE, ngx_conf_t, ngx_http_add_variable, - ngx_http_request_t, ngx_http_variable_value_t, ngx_int_t, ngx_str_t, ngx_uint_t, -}; -use ngx::http::HTTPStatus; -use ngx::ngx_string; -use serde_json::json; - -use crate::config; -use crate::upstreams; -use ipass_core::{auth, http, state, util}; - -// =========================================================================== -// 变量分派标记 -// =========================================================================== - -/// `$llm_target`:完整上游 URL(含 path 与 query),直接交给 `proxy_pass` -pub const VAR_TARGET: usize = 1; -/// `$llm_host`:上游 Host 头(`proxy_set_header Host $llm_host`) -/// -/// 变量形式的 `proxy_pass` 不会自动推导 Host,必须显式设, -/// 否则打给 OpenAI / Cloudflare 后端会因 Host 不符被拒。 -pub const VAR_HOST: usize = 2; -/// `$llm_auth`:上游 Authorization 头值 -pub const VAR_AUTH: usize = 3; -/// `$llm_acct`:本次请求选中的上游账号下标(多账号路由用;log 阶段据此标记耗尽)。 -/// 由 `pick_account` 在变量求值时计算(与 `$llm_auth` 同源,保证一致)。 -pub const VAR_ACCT: usize = 4; - -// =========================================================================== -// preconfiguration:注册变量 -// =========================================================================== - -/// 注册 LLM 四件套只读变量(gateway preconfiguration 经插件循环调用)。 -/// -/// 名字里的 `llm_` 前缀不要省:nginx 变量名全局唯一, -/// 将来接入别的模块时才不会撞名。 -/// -/// # Safety -/// `cf` 由 nginx 传入且有效。 -pub unsafe fn preconfig(cf: *mut ngx_conf_t) -> bool { - let mut target = ngx_string!("llm_target"); - let mut host = ngx_string!("llm_host"); - let mut auth_var = ngx_string!("llm_auth"); - let mut acct_var = ngx_string!("llm_acct"); - unsafe { - add_var(cf, &raw mut target, VAR_TARGET) - && add_var(cf, &raw mut host, VAR_HOST) - && add_var(cf, &raw mut auth_var, VAR_AUTH) - && add_var(cf, &raw mut acct_var, VAR_ACCT) - } -} - -/// 注册一个只读变量,值由 [`var_get`] 按 `slot` 分派计算。 -/// -/// 与 `$proxy_backend_addr` 不同,LLM 三件套**不需要** -/// `ngx_http_get_variable_index`——它们是纯只读的,模块从不主动写, -/// 只有 `proxy_pass` / `proxy_set_header` 求值时触发 get_handler。 -unsafe fn add_var(cf: *mut ngx_conf_t, name: *mut ngx_str_t, slot: usize) -> bool { - // SAFETY: cf 由 nginx 传入;name 指向本函数调用方栈上的 ngx_str_t,注册时被拷贝 - let var = unsafe { - ngx_http_add_variable( - cf, - name, - (NGX_HTTP_VAR_CHANGEABLE | NGX_HTTP_VAR_NOCACHEABLE) as ngx_uint_t, - ) - }; - if var.is_null() { - return false; - } - // SAFETY: var 非空且由本函数独占初始化;data 承载分派标记 - unsafe { - (*var).get_handler = Some(var_get); - (*var).set_handler = None; - (*var).data = slot; - } - true -} - -// =========================================================================== -// 请求解析 -// =========================================================================== - -/// 上游在共享内存里是否可用(原 `state::get_upstream` 的语义:`get_live` -/// 精确取 `up:`,过期视为不存在)。**未命中即 404,无回源兜底**。 -pub fn upstream_live(slot: &str) -> bool { - state::get_live(config::KEY_UP_PREFIX, slot.as_bytes(), util::now()).is_some() -} - -/// 剥掉内部测试前缀:`/_upstream_test//...` → `//...` -/// -/// 管理台的「测试」按钮经 `ngx_http_internal_redirect` 落到这个内部 location, -/// 复用本文件的取 slot / 拼 URL / 换密钥逻辑。**slot_of 与 upstream_uri 都必须 -/// 走这一步**——漏一边的表现(实测):slot 被当成路径段留给上游,拼出 -/// `.../v1/deepseeklike/v1/chat/completions` → 404/502;对「任何路径都回 200」 -/// 的 echo 靶机则会掩盖成假绿,必须看靶机实际收到的 path。 -fn strip_internal_prefix(uri: &str) -> &str { - uri.strip_prefix("/_upstream_test").unwrap_or(uri) -} - -/// 取 URL 第一段作为上游 slot:`/openai/v1/chat/completions` → `openai` -/// -/// 兼容一条内部链路:管理台的「测试」按钮走 -/// `POST /api/admin/upstreams/test`,模块校验后 `ngx_http_internal_redirect` -/// 到管理 server 的内部 location `/_upstream_test//v1/chat/completions`, -/// 让 `proxy_pass $llm_target` 原样复用本文件的取 slot / 拼 URL / 换密钥逻辑。 -/// 前缀剥掉后语义与客户端直打 `//...` 完全一致(同一套鉴权变量)。 -pub fn slot_of(r: *mut ngx_http_request_t) -> Option { - let uri = http::uri(r); - let rest = strip_internal_prefix(&uri).strip_prefix('/')?; - let slot = rest.split('/').next().unwrap_or(""); - if slot.is_empty() { - return None; - } - Some(slot.to_ascii_lowercase()) -} - -/// 这条请求是不是走 Anthropic 协议:`//anthropic/...` 是,其余不是。 -/// -/// 判定只看 slot **之后**的第一段路径是否 `anthropic`——模型名不在路径里, -/// 不会误判。内部测试 location `/_upstream_test//anthropic/...` 同样识别。 -pub fn is_anthropic(r: *mut ngx_http_request_t) -> bool { - let raw = http::uri(r); - let uri = strip_internal_prefix(&raw); - let rest = uri.strip_prefix('/').unwrap_or(uri); - // 剥掉 slot 段(第一段),看剩下第一段是否 `anthropic` - let after_slot = match rest.find('/') { - Some(i) => &rest[i..], - None => return false, - }; - after_slot - .trim_start_matches('/') - .split('/') - .next() - .map(|s| s.eq_ignore_ascii_case("anthropic")) - .unwrap_or(false) -} - -/// 剥掉 slot 段、拼回 query:`/openai/v1/chat/completions?a=1` → `/v1/chat/completions?a=1` -/// -/// Anthropic 协议时(`anthropic = true`),额外剥掉路径里紧跟 slot 的 -/// `/anthropic` 段——那段是网关替客户端表达的「协议标记」,不属于上游路径; -/// 不剥会让 `base_anthropic`(已以 `/anthropic` 结尾)变成 `…/anthropic/anthropic/...`。 -/// -/// query 必须在这里拼——变量形式的 `proxy_pass` **不会**自动追加原始 args。 -pub fn upstream_uri(r: *mut ngx_http_request_t, anthropic: bool) -> String { - let uri = http::uri(r); - let uri = strip_internal_prefix(&uri); - let rest = uri.strip_prefix('/').unwrap_or(uri); - // 只有 slot、没有后续路径时(如 `/openai`)打到上游根 - let path = match rest.find('/') { - Some(i) => &rest[i..], - None => "/", - }; - // Anthropic:剥掉紧跟 slot 的 `/anthropic` 段(及其后接的 `/` 由 join_target 处理) - let path = if anthropic { - match path.strip_prefix("/anthropic") { - Some(tail) if tail.is_empty() => "/", - Some(tail) => tail, - None => path, - } - } else { - path - }; - let args = http::args_raw(r); - if args.is_empty() { - path.to_owned() - } else { - format!("{}?{}", path, String::from_utf8_lossy(&args)) - } -} - -/// 从 `https://api.openai.com:443` 取出 `api.openai.com:443`(去掉 scheme 与路径) -fn host_of(base: &str) -> &str { - let rest = match base.find("://") { - Some(i) => &base[i + 3..], - None => base, - }; - match rest.find('/') { - Some(i) => &rest[..i], - None => rest, - } -} - -/// 拆 base:`https://api.deepseek.com/v1` → `("https://api.deepseek.com", "/v1")` -fn split_origin(base: &str) -> (&str, &str) { - let Some(i) = base.find("://") else { - return (base, ""); - }; - let after = &base[i + 3..]; - match after.find('/') { - Some(j) => (&base[..i + 3 + j], &after[j..]), - None => (base, ""), - } -} - -/// 拼上游完整 URL:`base + 客户端路径(已剥掉 slot)`。 -/// -/// **核心规则(仅 OpenAI 协议生效)**:当 base 自带路径段(如智谱的 -/// `…/api/coding/paas/v4`、阿里的 `…/compatible-mode/v1`)而客户端又按 -/// OpenAI 习惯带 `/v1` 时,`/v1` 是冗余的,必须剥掉再拼。否则会拼出 -/// `…/v4/v1/chat/completions`(智谱严格 404)或 `…/compatible-mode/v1/v1/…` -/// (阿里容忍但脏拼)。 -/// -/// Anthropic 协议**不**剥 `/v1`:Anthropic 协议 `messages` 前必须有 `/v1`, -/// 上游 base 已是 `…/api/anthropic`(裸 host 后的路径),客户端发的 `/v1/messages` -/// 必须原样保留,剥掉就漏版本段。 -/// -/// ```text -/// base 裸 host(OpenAI,deepseek/minimax): -/// https://api.deepseek.com + /v1/chat/completions → https://api.deepseek.com/v1/chat/completions (客户端 /v1 保留) -/// base 自带路径段(OpenAI,glm/qwen): -/// …/api/coding/paas/v4 + /v1/chat/completions → …/api/coding/paas/v4/chat/completions (剥客户端 /v1) -/// …/api/coding/paas/v4 + /chat/completions → …/api/coding/paas/v4/chat/completions (客户端没带,照常) -/// Anthropic(base 带 /api/anthropic 段): -/// …/api/anthropic + /v1/messages → …/api/anthropic/v1/messages (/v1 不剥) -/// http://127.0.0.1:18999 + /anthropic/v1/messages → http://127.0.0.1:18999/anthropic/v1/messages (openai 裸 host + Anthropic 路径:不剥) -/// ``` -/// -/// 这条规则是**实测踩坑换来的**:智谱(glm) 的 OpenAI 基址是 `…/api/coding/paas/v4`、 -/// Anthropic 基址是 `…/api/anthropic`;客户端统一带 `/v1` 时,glm 的 OpenAI 路径 -/// 会被拼成 `…/v4/v1/chat/completions` 被智谱 404(响应体为空,网关侧看不出是 -/// 拼接错)。而 Anthropic 的 `/v1` 必须保留——否则会漏版本段被 Anthropic 协议 -/// 上游 404。改后所有厂商只需把文档里的 host 换成网关域名 + 保留厂商原始路径段。 -fn join_target(base: &str, path: &str, anthropic: bool) -> String { - let (origin, base_path) = split_origin(base); - // OpenAI:base 是裸 host → 客户端路径原样拼 - // Anthropic:始终原样拼,/v1/messages 必须保留版本段 - if anthropic || base_path.is_empty() || base_path == "/" { - return format!("{base}{path}"); - } - // OpenAI + base 自带路径段:剥掉客户端冗余的 `/v1` 再拼。 - let tail = match path.strip_prefix("/v1") { - Some(t) if !t.is_empty() => t, // `/v1/chat/completions` → `/chat/completions` - Some(_) => "/", // 客户端只发了 `/v1` → 打到上游根 - None => path, // 客户端没带 `/v1`(如 `/chat/completions`)→ 照常拼 - }; - format!("{origin}{base_path}{tail}") -} - -// =========================================================================== -// 模型发现(本地应答,不去上游) -// =========================================================================== - -/// 是不是"列出模型"请求:`GET /v1/models`(OpenAI SDK 的 `client.models.list()`)。 -/// -/// **必须在查 slot 之前判断**:URL 第一段被当作 slot,`/v1/models` 天然会被 -/// 解析成 `slot=v1` → 「上游不存在」404。这两个路径是本网关自己的元信息接口, -/// 与"某个上游的模型清单"(`GET //v1/models`,照常透传给厂商)互不干扰。 -pub fn is_models_probe(r: *mut ngx_http_request_t) -> bool { - if !http::method_str(r).eq_ignore_ascii_case("GET") { - return false; - } - let uri = http::uri(r); - let uri = uri.trim_end_matches('/'); - uri == "/v1/models" || uri == "/models" -} - -/// 本地应答模型列表,形状与 OpenAI 的 `/v1/models` 一致。 -/// -/// 客户端 SDK 只认这个形状,返回项目自己的 `{code,msg,data}` 会让 SDK -/// 报"无法解析的响应"——与错误体必须 OpenAI 兼容是同一个理由。 -/// -/// 顶层额外挂一个 `x_ipass_upstreams`(OpenAI SDK 忽略未知字段): -/// 每个**该账号可用**的上游一段,给出 OpenAI / Anthropic 两种 base_url、 -/// 登记的模型清单与健康状态——客户端拿一把有效 Key 一次就能取齐 -/// 「能调哪些地址、哪些模型、哪些上游当前失效」。健康来自真实流量的 -/// log 阶段标记(`uh:` 缓存),从没被调用过 = `unknown`。 -pub fn respond_models(r: *mut ngx_http_request_t, allowed: &[String]) -> Status { - let data: Vec = upstreams::models_flat(allowed) - .into_iter() - .map(|(model, slot)| { - json!({ "id": model, "object": "model", "created": 0, "owned_by": slot }) - }) - .collect(); - - // base_url 跟随调用方所用的端口(与账号态 /api/v1/models 同一口径) - let port = http::host_port(r); - let x_upstreams: Vec = upstreams::available(allowed) - .into_iter() - .map(|(slot, up)| { - let (status, code, checked_at) = match upstreams::health(&slot) { - Some((ok, c, ts)) => (if ok { "ok" } else { "failed" }, c, ts), - None => ("unknown", 0, 0), - }; - json!({ - "slot": slot, - "base_url": upstreams::base_url(&slot, &up, &port), - "anthropic_base_url": upstreams::anthropic_base_url(&slot, &up, &port), - "models": up.models, - "status": status, - "status_code": if code > 0 { json!(code) } else { serde_json::Value::Null }, - "checked_at": checked_at, - }) - }) - .collect(); - - let body = - json!({ "object": "list", "data": data, "x_ipass_upstreams": x_upstreams }).to_string(); - // 与错误路径同一手法:自己把响应发出去,再把状态码交回 nginx。 - // 注意**不能**返回 NGX_OK——access 阶段的 OK 语义是"放行、继续走 content", - // 那样 `proxy_pass` 照样会执行,客户端拿到模型列表的同时上游被真调一次。 - let _ = http::send(r, HTTPStatus::OK, &body); - Status(HTTPStatus::OK.0 as ngx_int_t) -} - -// =========================================================================== -// 变量 get_handler -// =========================================================================== - -/// 在请求内存池里分配字符串并写进变量槽。随请求回收,无需手动释放。 -/// -/// # Safety -/// `r` / `v` 必须由 nginx 传入且在本调用期间有效。 -unsafe fn set_value( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - value: &str, -) -> ngx_int_t { - let pool = Pool::from_ngx_pool((*r).pool); - let bytes = value.as_bytes(); - let data = pool.alloc(bytes.len()) as *mut u8; - if data.is_null() { - (*v).set_not_found(1); - return Status::NGX_ERROR.into(); - } - ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len()); - (*v).data = data; - (*v).set_len(bytes.len() as _); - (*v).set_valid(1); - (*v).set_no_cacheable(1); - (*v).set_not_found(0); - Status::NGX_OK.into() -} - -/// 四个 LLM 变量共用一个 get_handler,靠 `var.data` 分派(值由 `preconfiguration` 写入)。 -/// -/// 共用代价:每个变量各调一次 `pick_account`(一次读锁 + 几次 BTreeMap 查找 + 一次 -/// 小分配,微秒级),相对一次 LLM 推理完全可忽略;换来不必维护四份相近代码。 -/// -/// `$llm_acct` 的求值会顺带把「用户 key → 账号」粘性绑定写进共享内存 -/// (仅当下行 key 存在且绑定变化),是幂等副作用。log 阶段读取 `$llm_acct` 即得到 -/// 本次请求实际打到的账号下标(用于额度耗尽时精确标记那一个账号)。 -pub extern "C" fn var_get( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - data: usize, -) -> ngx_int_t { - // SAFETY: r/v 由 nginx 传入且有效;slot_of/upstream_uri 只读请求结构 - unsafe { - let resolved = (|| -> Option<(String, String, String, usize)> { - let slot = slot_of(r)?; - // 协议分流:路径里有没有 `/anthropic` 段决定走 base_anthropic 还是 base - let anthropic = is_anthropic(r); - let key = auth::downstream_key(r); - let now = util::now(); - // 选账号:粘性优先 → 到期探测 → FNV 冷分散 → 全冷却 best-effort(见 pick_account) - let (idx, auth, base) = - upstreams::pick_account(&slot, anthropic, key.as_deref(), now)?; - let target = join_target(&base, &upstream_uri(r, anthropic), anthropic); - let host = host_of(&base).to_owned(); - Some((target, host, auth, idx)) - })(); - - match resolved { - Some((target, host, auth, idx)) => { - // 四个分支各取一个字段(只有命中分支的 String 被移动出去) - let value = match data { - VAR_TARGET => target, - VAR_HOST => host, - VAR_AUTH => auth, - VAR_ACCT => idx.to_string(), - _ => return Status::NGX_ERROR.into(), - }; - set_value(r, v, &value) - } - None => { - (*v).set_not_found(1); - (*v).set_valid(0); - Status::NGX_ERROR.into() - } - } - } -} - -#[cfg(test)] -mod tests { - use super::join_target; - - #[test] - fn join_target_strips_redundant_v1_when_base_has_path() { - // 智谱:base 自带 /api/coding/paas/v4,OpenAI 客户端带 /v1 → 必须剥掉 - assert_eq!( - join_target("https://open.bigmodel.cn/api/coding/paas/v4", "/v1/chat/completions", false), - "https://open.bigmodel.cn/api/coding/paas/v4/chat/completions" - ); - // 阿里:base 自带 /compatible-mode/v1,双 /v1 也要收敛成单 /v1 - assert_eq!( - join_target("https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1", "/v1/chat/completions", false), - "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions" - ); - // 客户端没带 /v1(如 OpenAI SDK 直接拼 /chat/completions)→ 照常 - assert_eq!( - join_target("https://open.bigmodel.cn/api/coding/paas/v4", "/chat/completions", false), - "https://open.bigmodel.cn/api/coding/paas/v4/chat/completions" - ); - // 裸 host(无路径段)→ OpenAI 客户端 /v1 原样保留(deepseek/minimax) - assert_eq!( - join_target("https://api.deepseek.com", "/v1/chat/completions", false), - "https://api.deepseek.com/v1/chat/completions" - ); - assert_eq!( - join_target("https://api.minimaxi.com", "/v1/chat/completions", false), - "https://api.minimaxi.com/v1/chat/completions" - ); - // Anthropic:/v1/messages 必须保留版本段,不能剥 - assert_eq!( - join_target("https://open.bigmodel.cn/api/anthropic", "/v1/messages", true), - "https://open.bigmodel.cn/api/anthropic/v1/messages" - ); - // Anthropic + 裸 host base + 客户端 /anthropic/v1/messages → 原样拼 - assert_eq!( - join_target("http://127.0.0.1:18999", "/anthropic/v1/messages", true), - "http://127.0.0.1:18999/anthropic/v1/messages" - ); - } -} diff --git a/rust/crates/plugin-llm/src/llm_audit.rs b/rust/crates/plugin-llm/src/llm_audit.rs deleted file mode 100644 index d1b29b2..0000000 --- a/rust/crates/plugin-llm/src/llm_audit.rs +++ /dev/null @@ -1,686 +0,0 @@ -//! LLM 对话审计:把每个**被转发**的 LLM 请求**元数据**落盘存档。 -//! -//! 设计要点(详见 docs/llm/AUDIT_LOG.md): -//! * **捕获方式**:LLM location 开 `client_body_in_file_only clean`,nginx 自己把 -//! 请求体刷到临时文件;本模块在 **log 阶段**单遍扫描该临时文件 —— **只保留前 N 字节** -//! 作为 `excerpt`,其余边读边喂 SHA256 后丢弃,产出 `body_hash`(全文指纹)与 -//! `body_bytes`(全文长度),然后追加一行到 `meta.jsonl`。 -//! **1.0.18 起不再复制完整 body 到 audit 目录** —— 旧版每请求 700 KB+(带 tools / -//! 多轮 history),QPS 10 一天 600 GB,10 GB 配额 24 分钟就触发 purge,复盘价值 -//! 也不大(前 100 字符就能秒判「用了哪个模型 + 用户问了啥」)。 -//! * **目录**:`<审计根>/YYYY-MM-DD/meta.jsonl`。不再有 `<毫秒>_.json` 子文件。 -//! 审计根默认 = data 目录的兄弟目录 `audit/`(`ipass_audit_dir` 指令可覆盖)。 -//! * **清理**:按容量触发(`AUDIT_MAX_BYTES`)——写入后节流巡检 -//! (≥ `AUDIT_PURGE_MIN_INTERVAL` 秒一次、AtomicBool 单飞),超限时按日期 -//! 目录**从旧到新整目录删除**;worker 启动时强制巡检一次(不依赖外部 cron)。 -//! * **失败策略**:审计是旁路功能,任何失败只 `log_err`,绝不影响转发结果。 -//! -//! ## 1.0.18 行为变更 -//! -//! - 不再复制完整 body:单请求审计占用从 ~700 KB 降到 ~500 B(一行 meta),**降约 3 个数量级** -//! - meta 行新增三个字段:`excerpt`(body 前 100 字节,JSON 转义交给序列化器,**不双重编码**)、`body_hash`(**全文** SHA256 前 16 hex)、`body_bytes`(全文字节数) -//! - `AUDIT_MAX_BYTES` 默认从 10 GB 改为 1 GB(旧值在新口径下等于「永远不会 purge」) - -use std::fs; -use std::io::{BufReader, Read, Write}; -use std::path::PathBuf; -use std::sync::Mutex; -use std::sync::atomic::{AtomicBool, AtomicI64, Ordering}; - -use ngx::ffi::ngx_http_request_t; -use serde_json::{Value, json}; - -use crate::config; -use ipass_core::state; -use ipass_core::store; -use ipass_core::util; - -/// `ipass_audit_dir` 覆盖值。配置阶段由 nginx 主线程设置一次,之后只读。 -static AUDIT_DIR_OVERRIDE: Mutex> = Mutex::new(None); - -/// 上次容量巡检时刻(秒);0 = 从未巡检。 -static LAST_PURGE_AT: AtomicI64 = AtomicI64::new(0); - -/// 巡检单飞标记:多 worker / 多请求并发时只放一个进去。 -static PURGING: AtomicBool = AtomicBool::new(false); - -/// 指令 `ipass_audit_dir `:显式指定审计根目录(缺省 = data 兄弟目录)。 -pub fn set_dir(dir: &str) { - if let Ok(mut slot) = AUDIT_DIR_OVERRIDE.lock() { - *slot = Some(dir.to_owned()); - } -} - -/// 审计根目录:显式指令优先,否则取 data 目录的兄弟目录。 -/// 每次现算(配置阶段后才可能被调用,store::data_dir() 已就绪)。 -fn root() -> PathBuf { - if let Ok(slot) = AUDIT_DIR_OVERRIDE.lock() { - if let Some(d) = slot.as_deref() { - return PathBuf::from(d); - } - } - PathBuf::from(store::data_dir()) - .parent() - .map(|p| p.join(config::AUDIT_DIR_NAME)) - .unwrap_or_else(|| PathBuf::from(config::AUDIT_DIR_NAME)) -} - -/// worker 启动:确保审计目录存在 + 强制巡检一次容量。 -/// 失败只降级(审计停用 / 跳过本次巡检),绝不阻塞 worker 启动。 -pub fn startup() { - let r = root(); - if let Err(e) = fs::create_dir_all(&r) { - util::log_err(&format!("[audit] 建目录失败 {}: {e},审计停用", r.display())); - return; - } - purge(true); -} - -/// 把「body 前 N 字节」解成字符串,供 meta 行的 `excerpt` 字段使用。 -/// -/// **只做 UTF-8 安全解码,不做 JSON 转义** —— 引号 / 反斜杠 / 控制字符一律交给 -/// `json!()`(serde_json)统一转义。这里若自己再转义一遍就是**双重编码** -/// (1.0.18 第一版踩过:落盘成 `"excerpt":"\"{\\\"model\\\"...\""`,读出来多一层 -/// 引号,任何按前缀匹配的消费方都解析不到)。 -/// -/// 把「body 前 N 字节」解成字符串,供 meta 行的 `excerpt` 字段使用。 -/// -/// **1.0.27 起改为智能摘录**: -/// - body 长度 ≤ `AUDIT_EXCERPT_BYTES` → 保留原文(短 query 整条有诊断价值); -/// - body 长度 > `AUDIT_EXCERPT_BYTES` → 解析 JSON,提取 `model` + 最后一个 user -/// message 的 `content`(按 `AUDIT_CONTENT_CHARS` 截前 N 字),跳过 -/// `context_management` / `tools` / `temperature` 等结构噪声,输出 -/// `{"model":"…","content":"…"}`。 -/// -/// 大 body(165 KB / 238 KB 那种)原 100 字节截到的全是 `context_management` -/// 元数据 —— 智能摘录**优先把位置留给 user content**,让 audit 真有复盘价值。 -fn excerpt_text(body_bytes_total: u64, raw: &[u8]) -> String { - if body_bytes_total <= config::AUDIT_EXCERPT_BYTES as u64 { - // 短请求体:原文保留(含 model + messages 结构)。`from_utf8_lossy` 把 - // 切在多字节字符中间的残缺尾巴换成 U+FFFD,序列化器吃得下。 - return String::from_utf8_lossy(raw).into_owned(); - } - - // 长请求体:仅解析前 N 字节(受 `AUDIT_JSON_PARSE_BYTES` 限制,避免 165 KB - // 大 body 全读进内存)。 - let parse_cap = raw.len().min(config::AUDIT_JSON_PARSE_BYTES); - let v: Value = match serde_json::from_slice(&raw[..parse_cap]) { - Ok(v) => v, - Err(_) => return String::new(), // 非 JSON body(罕见,如多模态二进制)→ 空 excerpt - }; - - let model = v - .get("model") - .and_then(|m| m.as_str()) - .unwrap_or(""); - let user_content = extract_user_content(&v); - if user_content.is_empty() { - // 没找到 messages / content(异常 body)→ 空 excerpt - return String::new(); - } - // 截前 N 字(按 char 而非 byte,避免切断 UTF-8 多字节字符) - let content: String = user_content.chars().take(config::AUDIT_CONTENT_CHARS).collect(); - json!({ "model": model, "content": content }).to_string() -} - -/// 提取最后一个 user message 的 content 文本。 -/// -/// 兼容 OpenAI(`messages[-1].content` 是字符串)与 Anthropic -///(`messages[-1].content` 是 `[{type:"text", text:"..."}, ...]` 数组)。 -fn extract_user_content(v: &Value) -> String { - let messages = match v.get("messages").and_then(|m| m.as_array()) { - Some(arr) => arr, - None => return String::new(), - }; - // 倒序找最后一个 role=user 的消息(避免工具结果 assistant 回复被误算) - for m in messages.iter().rev() { - let role_ok = m.get("role").and_then(|r| r.as_str()) == Some("user"); - if !role_ok { - continue; - } - if let Some(content) = m.get("content") { - // OpenAI: content 是字符串 - if let Some(s) = content.as_str() { - return s.to_owned(); - } - // Anthropic: content 是 [{type:"text", text:"..."}, ...] 数组 - if let Some(arr) = content.as_array() { - for item in arr { - if let Some(text) = item.get("text").and_then(|t| t.as_str()) { - return text.to_owned(); - } - } - } - } - } - String::new() -} - -/// 单遍扫描 body 临时文件:**只把前 N 字节留在内存**,其余边读边喂哈希后丢弃。 -/// -/// 返回 `(前 N 字节, 全量 SHA256 hex, 全量字节数)`。读失败返回空值(审计是旁路,失败不报错)。 -/// -/// 为什么扫全文而不是只读前 N 字节:`body_hash` 要当「同一请求」的指纹用, -/// 而所有 OpenAI/Anthropic 请求的前 100 字节都是同一个开头 -/// (`{"model":"…","messages":[{"role":`),只哈希前缀 = 全部碰撞,这一列就是废数据。 -/// 扫全文只是顺序 IO,不复制、不落盘,成本仍**低于** 1.0.17 的「读全文 + 写全文副本」。 -fn scan_body(path: &str, n: usize) -> (Vec, String, u64) { - let f = match fs::File::open(path) { - Ok(f) => f, - Err(_) => return (Vec::new(), String::new(), 0), - }; - let mut r = BufReader::new(f); - let mut prefix: Vec = Vec::with_capacity(n); - let mut hasher = util::Sha256Stream::new(); - let mut total: u64 = 0; - let mut buf = [0u8; 64 * 1024]; - loop { - match r.read(&mut buf) { - Ok(0) => break, - Ok(k) => { - if prefix.len() < n { - let need = (n - prefix.len()).min(k); - prefix.extend_from_slice(&buf[..need]); - } - hasher.update(&buf[..k]); - total += k as u64; - } - Err(_) => break, - } - } - (prefix, hasher.finish_hex(), total) -} - -/// log 阶段捕获:单遍扫描 body 临时文件,取**前 N 字节**作为 excerpt、全文算 SHA256、 -/// 追加一行元数据。**1.0.18 起不再复制完整 body**。 -/// -/// `tin` / `tout` 是 body filter 旁路抓到的 token 数(`llm_usage` 模块提供), -/// 0 = 上游没给 usage(如 OpenAI 流式未开 include_usage)。 -/// -/// SAFETY: r 必须是 nginx 当前正在 finalize 的请求(log 阶段回调保证)。 -pub unsafe fn capture(r: *mut ngx_http_request_t, username: &str, slot: &str, tin: u64, tout: u64) { - let rb = (*r).request_body; - if rb.is_null() { - return; - } - let tf = (*rb).temp_file; - if tf.is_null() { - // body 没被刷过盘(本地应答 / 被拒请求 / 无 body)→ 不审计 - return; - } - // 临时文件路径:ngx_temp_file_t.file.name(nginx 创建时填好的完整路径) - let src = match core::str::from_utf8((*tf).file.name.as_bytes()) { - Ok(s) if !s.is_empty() => s.to_owned(), - _ => return, - }; - - let ts = util::now(); - let day = util::fmt_time(ts).get(..10).unwrap_or("unknown").to_owned(); - let dir = root().join(&day); - if let Err(e) = fs::create_dir_all(&dir) { - util::log_err(&format!("[audit] 建日期目录失败 {}: {e}", dir.display())); - return; - } - - // 单遍扫描:取前 N 字节作为 excerpt 原文,全文算 SHA256(前 16 hex)。body 可能几 MB, - // 但只有前 N 字节留在内存,且**不再复制完整文件、不再写 `<毫秒>_.json` 子文件**。 - let (prefix, full_hash, body_bytes) = scan_body(&src, config::AUDIT_JSON_PARSE_BYTES); - // 1.0.27 智能摘录:短请求保留原文;长请求解析 JSON → 提取 model + user content - let excerpt = excerpt_text(body_bytes, &prefix); - let hash_hex = &full_hash[..config::AUDIT_HASH_HEX_BYTES.min(full_hash.len())]; - - let status = (*r).headers_out.status as u64; - let user = if username.is_empty() { "-" } else { username }; - let up = if slot.is_empty() { "-" } else { slot }; - - let line = json!({ - "ts": ts, - "time": util::fmt_time(ts), - "user": user, - "slot": up, - "status": status, - "tin": tin, - "tout": tout, - "excerpt": excerpt, - "body_hash": hash_hex, - "body_bytes": body_bytes, - }); - match fs::OpenOptions::new() - .create(true) - .append(true) - .open(dir.join("meta.jsonl")) - { - Ok(mut f) => { - if let Err(e) = writeln!(f, "{line}") { - util::log_err(&format!("[audit] 写 meta 失败: {e}")); - } - } - Err(e) => util::log_err(&format!("[audit] 打开 meta 失败: {e}")), - } - - purge(false); -} - -/// 容量巡检入口。force=true 跳过节流(启动时用);平时每分钟最多一次。 -pub fn purge(force: bool) { - let now = util::now(); - if !force && now - LAST_PURGE_AT.load(Ordering::Relaxed) < config::AUDIT_PURGE_MIN_INTERVAL { - return; - } - LAST_PURGE_AT.store(now, Ordering::Relaxed); - if PURGING.swap(true, Ordering::Acquire) { - return; // 已有巡检在跑 - } - let result = purge_once(); - PURGING.store(false, Ordering::Release); - if let Err(e) = result { - util::log_err(&format!("[audit] 容量巡检失败: {e}")); - } -} - -/// 扫描审计根:目录名即日期(字典序 = 时间序),总量超限就从最旧整目录删。 -/// -/// ⚠️ 本模块无法跑 `cargo test`(dylib 依赖 nginx 主程序的运行时符号, -/// `cargo test --lib` 直接 SIGABRT)——行为由 tests/llm/e2e.sh【9】覆盖。 -fn purge_once() -> Result<(), String> { - let r = root(); - let mut total: u64 = 0; - let mut dirs: Vec<(PathBuf, u64)> = Vec::new(); - for ent in fs::read_dir(&r).map_err(|e| e.to_string())?.flatten() { - let p = ent.path(); - if !p.is_dir() { - continue; - } - let mut size: u64 = 0; - if let Ok(files) = fs::read_dir(&p) { - for f in files.flatten() { - if let Ok(md) = f.metadata() { - if md.is_file() { - size += md.len(); - } - } - } - } - total += size; - dirs.push((p, size)); - } - if total <= config::AUDIT_MAX_BYTES { - return Ok(()); - } - dirs.sort_by(|a, b| a.0.cmp(&b.0)); - for (p, size) in dirs { - if total <= config::AUDIT_MAX_BYTES { - break; - } - match fs::remove_dir_all(&p) { - Ok(()) => { - total = total.saturating_sub(size); - util::log_notice(&format!("[audit] 超容量,已删除 {}", p.display())); - } - Err(e) => util::log_err(&format!("[audit] 删除 {} 失败: {e}", p.display())), - } - } - Ok(()) -} - -// =========================================================================== -// 按日查询 helper(1.0.27):管理台「小时级趋势」直接读 meta.jsonl 不聚合 -// =========================================================================== - -/// 列出审计根下所有有 `meta.jsonl` 的日期(字典序 = 时间序)。 -/// -/// 仅返回目录名符合 `YYYY-MM-DD` 形式(防止「meta 临时文件」等噪声目录干扰)。 -pub fn list_days() -> Vec { - let Ok(rd) = fs::read_dir(root()) else { return Vec::new() }; - let mut out: Vec = rd - .flatten() - .filter_map(|e| { - let name = e.file_name().to_string_lossy().to_string(); - // 8 位 `YYYY-MM-DD` 校验 + 目录里必须有 meta.jsonl - if name.len() == 10 - && name.as_bytes()[4] == b'-' - && name.as_bytes()[7] == b'-' - && e.path().join("meta.jsonl").is_file() - { - Some(name) - } else { - None - } - }) - .collect(); - out.sort_by(|a, b| b.cmp(a)); // 倒序:最近日期在前 - out -} - -/// 读某天的 meta.jsonl,每行解析为 JSON。 -/// -/// **剔除 excerpt / body_hash** —— excerpt 含请求体前 100 字节(可能含敏感 prompt), -/// body_hash 不参与小时聚合;剔除省流量(约 60%)且降低敏感数据外泄面。 -/// 返回每行 `{user, slot, status, tin, tout, ts, time, body_bytes}` 八个字段。 -pub fn read_day(day: &str) -> Result, String> { - if day.len() != 10 || day.as_bytes()[4] != b'-' || day.as_bytes()[7] != b'-' { - return Err(format!("日期格式错(期望 YYYY-MM-DD):{day}")); - } - let path = root().join(day).join("meta.jsonl"); - let file = fs::File::open(&path).map_err(|e| format!("{} 不存在或不可读:{e}", path.display()))?; - let reader = BufReader::new(file); - let mut out = Vec::new(); - for line in std::io::BufRead::lines(reader).flatten() { - if line.is_empty() { - continue; - } - if let Ok(mut v) = serde_json::from_str::(&line) { - if let Some(obj) = v.as_object_mut() { - // 剔除敏感 / 不需要字段 - obj.remove("excerpt"); - obj.remove("body_hash"); - } - out.push(v); - } - } - Ok(out) -} - -// =========================================================================== -// 自服务上线以来累计 token(1.0.32 给 /api/admin/usage 的累计字段用) -// =========================================================================== - -/// 全生命周期 token 累计(按 audit meta.jsonl 聚合,不依赖 shm 日桶)。 -/// -/// audit meta.jsonl 是真源: -/// - `tin` / `tout` 字段来自上游响应体的 usage 块(usage_filter 旁路读), -/// 与 shm `tin_total` 同源 —— 但**不受 stats 落盘/恢复影响**(audit -/// 每条都是 append-only,stats 会被重置,audit 不会)。 -/// - 容量上限 `AUDIT_MAX_BYTES`(1GB),超过会从最旧整目录删。 -/// 在 1GB 内所有历史都保留,**完全覆盖「自服务上线以来累计」语义**。 -/// -/// 读所有 `YYYY-MM-DD/meta.jsonl`,sum 出全网总 `tin` / `tout` + `requests`。 -/// 失败兜底:审计目录无数据 → 返回空(不阻塞 /usage)。 -pub fn all_time_total() -> Value { - let days = list_days(); - if days.is_empty() { - return json!({ - "requests": 0u64, - "tin": 0u64, - "tout": 0u64, - "since": Value::Null, - "days_scanned": 0u32, - }); - } - let mut total_requests: u64 = 0; - let mut total_tin: u64 = 0; - let mut total_tout: u64 = 0; - for d in &days { - let Ok(rows) = read_day(d) else { continue }; - for r in rows.iter() { - total_requests += 1; - total_tin += r.get("tin").and_then(|v| v.as_u64()).unwrap_or(0); - total_tout += r.get("tout").and_then(|v| v.as_u64()).unwrap_or(0); - } - } - // since = 最早一天的目录名(list_days 已按字典序排过,最小在最前) - json!({ - "requests": total_requests, - "tin": total_tin, - "tout": total_tout, - "since": days.last(), // list_days 是倒序(最近在前),所以 last() 是最早的 - "days_scanned": days.len(), - }) -} - -// =========================================================================== -// 按账号聚合(1.0.29 给 /api/v1/me 用) -// =========================================================================== - -/// 单个账号最近 N 天的调用聚合(用于 `/api/v1/me` 的 `recent` 块)。 -/// -/// **1.0.32 改造**:增量 + 持久化。 -/// -/// 维护 shm `usum:` 存「自服务上线以来累计 + 上次扫到的 max ts」 -/// (dump 走 stats_persist 自动持久化到 llm-stats.json,**跨重启不丢**)。 -/// 每次调用只扫 audit delta (`ts > last_ts`),把结果累加进 shm 缓存。 -/// -/// 关键: -/// - `sum.calls / sum.tin / sum.tout / sum.by_slot` 是**全时间累计** -/// (last_ts 之上 + last_ts 之下被持久化的旧 sum)——**不衰减** -/// - **24h hour / 24h totals / 7d by_slot 是滚动数据,每次重算 24h/7d 范围 audit** -/// (持久化会过期,所以不缓存;audit 容量 1GB = 14 年历史, -/// 24h/7d 范围内的数据**永远在 audit 里**,可放心重算) -/// -/// 失败兜底:audit 目录无数据 / 权限问题 → 返回空统计(不让 /me 整个挂掉)。 -/// shm 反序列化失败 → 重新扫全部 audit(等价于 cold start)。 -pub fn user_recent(username: &str, days: u32) -> Value { - if username.is_empty() { - return empty_recent(); - } - let now = util::now(); - let cutoff_24h = now - 86400; - let cutoff_7d = now - (days as i64) * 86400; - - // 1. 读 shm 持久化累计(仅 calls/tin/tout/by_slot/last_ts,**不**含 hour) - let mut sum = load_user_sum(username).unwrap_or_default(); - - let all_days = list_days(); - - // 2. 扫 audit delta(ts > sum.last_ts)→ 累加进 sum - for d in &all_days { - let rows = match read_day(d) { - Ok(r) => r, - Err(_) => continue, - }; - for r in rows.iter() { - let u = r.get("user").and_then(|v| v.as_str()).unwrap_or(""); - if u != username { - continue; - } - let ts = r.get("ts").and_then(|v| v.as_i64()).unwrap_or(0); - if ts <= sum.last_ts || ts > now { - continue; - } - let slot = r.get("slot").and_then(|v| v.as_str()).unwrap_or("-"); - let status = r.get("status").and_then(|v| v.as_u64()).unwrap_or(0); - let tin = r.get("tin").and_then(|v| v.as_u64()).unwrap_or(0); - let tout = r.get("tout").and_then(|v| v.as_u64()).unwrap_or(0); - - sum.calls += 1; - sum.tin += tin; - sum.tout += tout; - let entry = sum.by_slot.entry(slot.to_owned()).or_default(); - entry.calls += 1; - entry.tin += tin; - entry.tout += tout; - if ts >= sum.last_ts { - sum.last_ts = ts; - sum.last_slot = slot.to_owned(); - sum.last_status = status; - } - } - } - - // 3. 24h hour / 24h totals / 7d by_slot 都是滚动数据 → 重算 24h/7d 范围 audit - // (不持久化,**每次现算**——audit 永远在,量小几百 KB,可接受) - let mut hour_24 = [0u64; 24]; - let mut total_24h: u64 = 0; - let mut total_tin_24h: u64 = 0; - let mut total_tout_24h: u64 = 0; - let mut by_slot_7d: std::collections::BTreeMap = - std::collections::BTreeMap::new(); - for d in &all_days { - let rows = match read_day(d) { - Ok(r) => r, - Err(_) => continue, - }; - for r in rows.iter() { - let u = r.get("user").and_then(|v| v.as_str()).unwrap_or(""); - if u != username { - continue; - } - let ts = r.get("ts").and_then(|v| v.as_i64()).unwrap_or(0); - if ts > now { - continue; - } - let slot = r.get("slot").and_then(|v| v.as_str()).unwrap_or("-"); - let status = r.get("status").and_then(|v| v.as_u64()).unwrap_or(0); - let tin = r.get("tin").and_then(|v| v.as_u64()).unwrap_or(0); - let tout = r.get("tout").and_then(|v| v.as_u64()).unwrap_or(0); - - // 24h 滚动窗口(**左老右新**:hour_24[0] = 23h 前,hour_24[23] = 当前小时) - // age = now - ts,age_h = age / 3600 ∈ [0, 24] - // age_h = 0 → 现在 - // age_h = 23 → 23h 前(窗口里最老的整小时桶) - // age_h = 24 → 正好 24h 前(cutoff 边界,clamp 到 0 桶) - if ts >= cutoff_24h { - total_24h += 1; - total_tin_24h += tin; - total_tout_24h += tout; - let age_h = ((now - ts) / 3600) as i64; - let idx = (23 - age_h).max(0) as usize; - hour_24[idx] += 1; - } - // 7d by_slot:last_status 必须用「最大 ts 的那次调用」 - // —— list_days() 是倒序(今天 → 昨天),如果无脑 entry.last_status = status - // 覆盖,昨天最后几行的 status 会**覆盖**今天的最新值 - // (昨天 9-14 末尾 wugui minimax 全是 429,今天 9-15 末尾是 200, - // 无脑覆盖 → 显示 429 而非 200,1.0.34 现场踩坑)。 - if ts >= cutoff_7d { - let entry = by_slot_7d.entry(slot.to_owned()).or_default(); - entry.calls += 1; - entry.tin += tin; - entry.tout += tout; - if ts >= cutoff_24h { - entry.calls_24h += 1; - } - // 按 ts 取 max:ts 更大才覆盖(**7d 窗口内最大 ts** 那次的 status) - if ts > entry.max_ts { - entry.max_ts = ts; - entry.last_status = status; - } - } - } - } - - // 4. 写回 shm(增量缓存,下一次从 last_ts 继续累加) - save_user_sum(username, &sum); - - // 5. by_slot 数组按 calls 降序;7d totals 从 by_slot_7d 求和 - let mut slot_vec: Vec<(String, SlotStat7d)> = by_slot_7d.into_iter().collect(); - slot_vec.sort_by(|a, b| b.1.calls.cmp(&a.1.calls)); - let mut total_7d: u64 = 0; - let mut total_tin_7d: u64 = 0; - let mut total_tout_7d: u64 = 0; - for (_, st) in &slot_vec { - total_7d += st.calls; - total_tin_7d += st.tin; - total_tout_7d += st.tout; - } - - json!({ - "calls_24h": total_24h, - "calls_7d": total_7d, - "tin_24h": total_tin_24h, - "tout_24h": total_tout_24h, - "tin_7d": total_tin_7d, - "tout_7d": total_tout_7d, - "hour_24h": hour_24.to_vec(), - // hour_24h_labels[i] = 「i 小时前」语义:i=0 → "23h前"(最老),i=23 → "现在" - "hour_24h_labels": (0..24).map(|i| { - let age = 23 - i; - if age == 0 { "现在".to_string() } - else if age == 1 { "1h前".to_string() } - else { format!("{}h前", age) } - }).collect::>(), - "by_slot": slot_vec.iter().map(|(s, st)| json!({ - "slot": s, - "calls_24h": st.calls_24h, - "calls_7d": st.calls, - "tin_7d": st.tin, - "tout_7d": st.tout, - "last_status": st.last_status, - })).collect::>(), - "last_at": if sum.last_ts > 0 { json!(util::fmt_time(sum.last_ts)) } else { json!(null) }, - "last_slot": if sum.last_ts > 0 { json!(sum.last_slot) } else { json!(null) }, - // 调试字段:运维可看出缓存命中情况(_cached.last_ts == now 表示无新请求) - "_cached": { - "last_ts": sum.last_ts, - "calls": sum.calls, - "tin": sum.tin, - "tout": sum.tout, - }, - }) -} - -/// 持久化的每用户累计(shm `usum:` 的 value,JSON 序列化) -/// -/// 只存「自上次 stats_persist 恢复以来的全时间累计」,**不**存 24h/7d 滚动数据 -/// (滚动窗口会衰减,持久化反而错位)。 -/// -/// `last_ts` 是关键:下次 user_recent 只扫 `ts > last_ts` 的 delta 行。 -#[derive(Default, serde::Serialize, serde::Deserialize)] -struct UserSum { - calls: u64, - tin: u64, - tout: u64, - /// 按 slot 累计(last_ts 之上的全时间累计) - by_slot: std::collections::BTreeMap, - last_ts: i64, - last_slot: String, - last_status: u64, -} - -/// 7d 范围 by_slot 聚合(user_recent 内每次现算,**不持久化**) -#[derive(Default)] -struct SlotStat7d { - calls: u64, - calls_24h: u64, - tin: u64, - tout: u64, - /// 当前已记下的最大 ts —— last_status 仅在 ts 更大时覆盖(1.0.34 现场) - max_ts: i64, - last_status: u64, -} - -/// 持久化累计的 by_slot 单 slot 值 -#[derive(Default, serde::Serialize, serde::Deserialize)] -struct SlotStat { - calls: u64, - tin: u64, - tout: u64, -} - -fn load_user_sum(username: &str) -> Option { - let (payload, _exp) = state::get(config::KEY_USER_SUM_PREFIX, username.as_bytes())?; - serde_json::from_str(&payload).ok() -} - -fn save_user_sum(username: &str, sum: &UserSum) { - if let Ok(payload) = serde_json::to_string(sum) { - // exp = 0 表示永不过期(持久化累计) - state::put( - config::KEY_USER_SUM_PREFIX, - username.as_bytes(), - payload.as_bytes(), - 0, - ); - } -} - -fn empty_recent() -> Value { - json!({ - "calls_24h": 0, - "calls_7d": 0, - "tin_24h": 0, - "tout_24h": 0, - "tin_7d": 0, - "tout_7d": 0, - "hour_24h": vec![0u64; 24], - "hour_24h_labels": (0..24).map(|i| { - let age = 23 - i; - if age == 0 { "现在".to_string() } - else if age == 1 { "1h前".to_string() } - else { format!("{}h前", age) } - }).collect::>(), - "by_slot": Vec::::new(), - "last_at": null, - "last_slot": null, - }) -} diff --git a/rust/crates/plugin-llm/src/models.rs b/rust/crates/plugin-llm/src/models.rs deleted file mode 100644 index cf932fc..0000000 --- a/rust/crates/plugin-llm/src/models.rs +++ /dev/null @@ -1,123 +0,0 @@ -//! LLM 上游持久化模型(`llm-upstreams.json`:以 slot 名为 key)。 - -use serde::{Deserialize, Serialize}; -use std::collections::BTreeMap; - -use crate::config::EXHAUST_STATUS_DEFAULT; - -/// 上游表:`slot` 就是 LLM 客户端 URL 的第一段路径:`/openai/v1/chat/completions` -/// 里的 `openai`。**刻意不按 body 里的 `model` 字段路由**——请求体要到 -/// content 阶段才读得到,而 access 阶段(转发决策点)读不到, -/// 按 model 分流就得放弃 `proxy_pass` 自己实现 upstream,代价差一个数量级。 -pub type UpstreamTable = BTreeMap; - -/// 单个上游账号:一把上游真 Key + 它的额度重置周期。 -/// -/// 一个 `slot`(一家厂商)可挂多个账号——不同账号通常是同一厂商的多把免费/付费 Key, -/// 各自有独立的周期性额度。网关按「用户 key → 上游账号」做粘性路由,账号额度耗尽时 -/// 自动切到同厂商下一个可用账号(见 `upstreams::pick_account` 与 `handlers` 的耗尽标记)。 -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct UpstreamAccount { - /// 完整的 Authorization 头值,如 `Bearer sk-xxx` - pub auth: String, - /// 备注(区分多账号用,如 `minimax-周额度` / `minimax-4h额度`) - #[serde(default)] - pub note: String, - /// **历史字段(1.0.44 起不参与路由)**:原为「额度重置周期(秒)」,供 - /// `cool_until_until` 天锚定边界计算——该计算两次算错锚点(见 config.rs 旧注释), - /// 已由「指数退避 + 真实请求探测」闭环取代。字段仍随 JSON 读写(存量兼容、 - /// view 回显),但填什么都不再影响冷却与路由;管理台表单已移除该输入。 - #[serde(default)] - pub resets: Vec, -} - -/// 单个 LLM 上游。 -/// -/// 字段名与旧版 `data/llm-upstreams.json` **逐字对应**,以便新版直接接管旧数据文件。 -/// 旧文件只有顶层 `auth`(单账号),新版通过 `effective_accounts()` 自动合成单账号, -/// 因此存量数据零改动即可继续工作。 -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Upstream { - /// OpenAI 兼容协议的真实上游基址(含完整路径段)。 - /// - /// 例:`https://api.deepseek.com/v1` / `https://open.bigmodel.cn/api/coding/paas/v4` - /// / `https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1`。 - /// 各厂商路径段完全不同,**网关只原样把它拼到 SDK 转发的余下路径前**, - /// 不做任何 `/v1` 猜测(见 `upstreams::base_url` 与 `llm::join_target`)。 - pub base: String, - /// Anthropic 协议的真实上游基址(如 `https://api.deepseek.com/anthropic` / - /// `https://open.bigmodel.cn/api/anthropic` / `https://token-plan.../apps/anthropic`)。 - /// - /// 各家与 OpenAI 路径**完全独立、无法推导**(阿里是 `/apps/anthropic`、 - /// 智谱是 `/api/anthropic`、deepseek/minimax 是 `/anthropic`),所以必须显式配。 - /// 旧数据只有 `base` 时由 `upstreams::derive_anthropic` 兜底派生(仅对 - /// deepseek/minimax 这类「`base` 末尾 `/v1` 或根路径」的厂商正确)。 - #[serde(default)] - pub base_anthropic: String, - /// 该上游**挂载的模型名清单**(如 `deepseek-chat` / `deepseek-reasoner`)。 - /// - /// 一个上游(一家厂商)通常挂多个模型,客户端要能先"列出有什么"再选。 - /// 这里只是网关侧的**清单**,用于 `GET /v1/models` 之类的模型发现; - /// 转发本身仍按 URL 里的 slot 走,**不校验 model 字段**(access 阶段读不到 body)。 - /// 留空表示"清单未知"——模型发现接口就不列该上游的模型,但转发照常可用。 - #[serde(default)] - pub models: Vec, - /// 关闭后该 slot 立即停止转发(保留配置便于恢复) - #[serde(default = "default_true")] - pub enabled: bool, - #[serde(default)] - pub note: String, - #[serde(default)] - pub created_at: i64, - /// 多账号列表。空时由 legacy `auth` 合成单账号(向后兼容旧数据)。 - #[serde(default)] - pub accounts: Vec, - /// 上游级「额度耗尽」探测配置(同一厂商所有账号共享错误形态)。 - /// - /// 命中下列任一即判该账号额度耗尽、进入冷却:上游返回这些 HTTP 状态码, - /// **或** 响应体(流式也能扫,见 `usage_filter`)含任一 `exhaust_markers` 子串。 - /// 空 = 用 `config::EXHAUST_STATUS_DEFAULT`(默认 `[402, 429, 403]`)。 - #[serde(default)] - pub exhaust_status: Vec, - /// 响应体「额度耗尽」标记子串(大小写敏感,逐字节匹配)。 - /// 例:MiniMax 余额不足可配 `"status_code":1004` 或 `余额不足`。 - #[serde(default)] - pub exhaust_markers: Vec, - /// 兼容旧版单 key 数据:旧 `auth` 字段。 - /// `accounts` 非空时忽略;`accounts` 空且 `auth` 非空 → 合成单账号(resets 空 = 不自动切换)。 - #[serde(default)] - pub auth: String, -} - -fn default_true() -> bool { - true -} - -impl Upstream { - /// 实际生效的账号列表。 - /// - /// `accounts` 非空 → 用它;否则 `auth` 非空 → 合成单账号(resets 空,等同无限额度, - /// 不会因「耗尽」被冷却,单账号时自然也不存在可切换对象)。两者皆空 → 空表(上游不可用)。 - pub fn effective_accounts(&self) -> Vec { - if !self.accounts.is_empty() { - self.accounts.clone() - } else if !self.auth.is_empty() { - vec![UpstreamAccount { - auth: self.auth.clone(), - note: String::new(), - resets: Vec::new(), - }] - } else { - Vec::new() - } - } - - /// 生效的「额度耗尽」状态码清单(空 → 默认 `[402, 429, 403]`)。 - pub fn effective_exhaust_status(&self) -> Vec { - if self.exhaust_status.is_empty() { - EXHAUST_STATUS_DEFAULT.to_vec() - } else { - self.exhaust_status.clone() - } - } -} diff --git a/rust/crates/plugin-llm/src/stats.rs b/rust/crates/plugin-llm/src/stats.rs deleted file mode 100644 index 1ca03b1..0000000 --- a/rust/crates/plugin-llm/src/stats.rs +++ /dev/null @@ -1,382 +0,0 @@ -//! LLM 用量统计:按**账号**与按**账号 × 上游**两个维度计数。 -//! -//! 数据全部躺在共享内存里(`ust:` / `ums:` 两个键前缀),没有新文件、 -//! 没有新进程——这是项目「单容器零依赖」红线下的必然选择。 -//! -//! ```text -//! ust: → <日窗id>:<今日数>:<累计数>:<最近调用时间戳> -//! ums:: → 同 上 -//! ``` -//! -//! 本模块是**业务解读层**:键前缀(`ust:` / `ums:` / `utt:` / `utms:` / -//! `ud:` / `udu:` / `uds:`)在这里与内核记账原语(`ipass_core::usage`)组合。 -//! 原语本身无任何业务语义——「去掉 LLM 还要(趋势图要给 apix 用)、 -//! 去掉 apix 还要(趋势图要给 LLM 用)」⇒ 原语放内核,口径与聚合放本 crate。 -//! -//! ## 口径:只统计"真的转发出去"的请求 -//! -//! 计数点在 `LlmHandler` 的**鉴权 / 白名单 / 限流全部通过之后**, -//! 所以 401 / 403 / 404 / 429 都不计入。这与配额计数 `st:` 刻意不同—— -//! 后者是"先增后判"(含被拒),要的是"超限后计数继续涨"的执行语义; -//! 而用量统计是给人看的,把被拒的请求算进"用量"只会让人误解。 -//! -//! ## token 级计量(2026-09-12 起) -//! -//! token 数来自上游响应体里的 `usage` 字段:`usage_filter` 模块在 **body filter 链** -//! 旁路扫描响应(SSE 尾部 / 非 stream JSON 尾部),零缓冲、不影响透传; -//! log 阶段统一入账到 `utt:` / `utms:` 两个键前缀,编码与本文件同款格式。 -//! 抓不到 usage(如 OpenAI 流式客户端没带 `include_usage`)= 记 0,不补估。 -//! 计数口径与请求数一致:**只算真的转发出去且上游给了 usage 的**。 - -use std::collections::BTreeMap; - -use serde_json::{Value, json}; - -use crate::config::{ - KEY_UDAY_PREFIX, KEY_UDAY_SLOT_PREFIX, KEY_UDAY_USER_PREFIX, KEY_UMS_PREFIX, - KEY_UTMS_PREFIX, KEY_UST_PREFIX, KEY_UTT_PREFIX, TREND_DAYS_DEFAULT, TREND_DAYS_MAX, - TREND_KEEP_SECS, -}; -use ipass_core::{state, usage, util}; - -// 内核记账类型与读数结构(re-export 方便 gateway / api 组合使用) -pub use ipass_core::usage::{TokenUsage, Usage}; - -/// 一个账号的用量 + 其下每个上游的明细 -pub struct UserUsage { - pub username: String, - pub usage: Usage, - pub models: Vec<(String, Usage)>, -} - -/// 一个上游的用量 + 用它的每个账号的明细 -pub struct ModelUsage { - pub slot: String, - pub usage: Usage, - pub users: Vec<(String, Usage)>, -} - -// =========================================================================== -// 写 -// =========================================================================== - -/// 记录一次已放行的 LLM 请求。**在鉴权/白名单/限流全通过之后调用**。 -pub fn record(username: &str, slot: &str, now: i64) { - if username.is_empty() || slot.is_empty() { - return; - } - let day = util::day_id(now); - usage::bump(KEY_UST_PREFIX, username, day, now); - usage::bump(KEY_UMS_PREFIX, &format!("{username}:{slot}"), day, now); - // 日桶:趋势图数据源(请求维度)。三个维度各写一次,键名带日窗 id - let delta = usage::DayBucket { requests: 1, ..Default::default() }; - usage::bump_day(KEY_UDAY_PREFIX, &day.to_string(), &delta, TREND_KEEP_SECS); - usage::bump_day(KEY_UDAY_USER_PREFIX, &format!("{username}:{day}"), &delta, TREND_KEEP_SECS); - usage::bump_day(KEY_UDAY_SLOT_PREFIX, &format!("{slot}:{day}"), &delta, TREND_KEEP_SECS); - - // 顺手看一眼要不要把统计落盘(默认 60 秒一次,间隔内只是一次原子比较)。 - // 挂在这里而不是各调用点,是因为本函数是 LLM 与 apix **两方唯一的记账入口** - // (apix 复用同一套统计,slot 带 `api/` 前缀)——以后再加一个租户也不会漏。 - crate::stats_persist::maybe_dump(); -} - -/// 累加一次真实 token 消耗(来源:body filter 从上游响应抓到的 usage)。 -/// 口径:只有抓到 usage 才记账;抓不到不补估。 -pub fn add_tokens(username: &str, slot: &str, tin: u64, tout: u64, now: i64) { - if username.is_empty() || slot.is_empty() || (tin == 0 && tout == 0) { - return; - } - let day = util::day_id(now); - usage::bump_t(KEY_UTT_PREFIX, username, tin, tout, day, now); - usage::bump_t(KEY_UTMS_PREFIX, &format!("{username}:{slot}"), tin, tout, day, now); - // 日桶:趋势图数据源(token 维度)。与 record 写的是同一批键,字段互补 - let delta = usage::DayBucket { tin: tin as i64, tout: tout as i64, ..Default::default() }; - usage::bump_day(KEY_UDAY_PREFIX, &day.to_string(), &delta, TREND_KEEP_SECS); - usage::bump_day(KEY_UDAY_USER_PREFIX, &format!("{username}:{day}"), &delta, TREND_KEEP_SECS); - usage::bump_day(KEY_UDAY_SLOT_PREFIX, &format!("{slot}:{day}"), &delta, TREND_KEEP_SECS); -} - -/// 删除账号时清理其全部用量键(`ust:` 与 `ums::*`)。 -/// -/// 用量键是**按前缀扫出来**做报表的(`all_users` / `all_models`),不清就会让 -/// 已删除的账号继续挂在用量榜上。这与「删账号必须摘 `ukey:` 索引」是同类的 -/// 正确性问题(只是影响面在报表而非鉴权,因此更容易被漏掉)。 -pub fn forget(username: &str) { - if username.is_empty() { - return; - } - // 先物化键名再删,避免"遍历中改表"(四个键前缀同一套路) - let prefix = format!("{username}:"); - for p in [KEY_UST_PREFIX, KEY_UTT_PREFIX] { - state::remove(p, username.as_bytes()); - } - // 日桶两个维度也要清:`udu::` 是按前缀扫不到的(键里还带日窗 id), - // 靠 TTL 自然淘汰也能收敛,但删账号后趋势图仍会挂着幽灵账号 90 天 - for p in [KEY_UMS_PREFIX, KEY_UTMS_PREFIX, KEY_UDAY_USER_PREFIX] { - let keys: Vec = state::iter_prefix(p) - .into_iter() - .map(|(name, _)| name) - .filter(|name| name.starts_with(&prefix)) - .collect(); - for name in keys { - state::remove(p, name.as_bytes()); - } - } -} - -// =========================================================================== -// 读 -// =========================================================================== - -/// 单账号读数(不含模型明细) -pub fn user(username: &str, now: i64) -> Usage { - let day = util::day_id(now); - usage::read(KEY_UST_PREFIX, username, day) -} - -/// 单账号 × 上游明细,按 slot 升序 -pub fn user_models(username: &str, now: i64) -> Vec<(String, Usage)> { - let day = util::day_id(now); - let prefix = format!("{username}:"); - let mut out: Vec<(String, Usage)> = state::iter_prefix(KEY_UMS_PREFIX) - .into_iter() - .filter_map(|(name, _)| { - let slot = name.strip_prefix(&prefix)?.to_owned(); - Some((slot, usage::read(KEY_UMS_PREFIX, &name, day))) - }) - .collect(); - out.sort_by(|a, b| a.0.cmp(&b.0)); - out -} - -/// 全部账号的用量(含各自的模型明细),按账号名升序。 -/// -/// 只扫一次 `ums:` 前缀再分组,避免"每个账号遍历一遍全表"。 -pub fn all_users(now: i64) -> Vec { - let day = util::day_id(now); - - let mut by_user: BTreeMap> = BTreeMap::new(); - for (name, _) in state::iter_prefix(KEY_UMS_PREFIX) { - let Some((username, slot)) = name.split_once(':') else { - continue; - }; - by_user - .entry(username.to_owned()) - .or_default() - .push((slot.to_owned(), usage::read(KEY_UMS_PREFIX, &name, day))); - } - for models in by_user.values_mut() { - models.sort_by(|a, b| a.0.cmp(&b.0)); - } - - state::iter_prefix(KEY_UST_PREFIX) - .into_iter() - .map(|(username, _)| { - let usage = usage::read(KEY_UST_PREFIX, &username, day); - let models = by_user.remove(&username).unwrap_or_default(); - UserUsage { username, usage, models } - }) - .collect() -} - -/// 全部上游的用量(含用到它的账号明细),按 slot 升序。 -pub fn all_models(now: i64) -> Vec { - let day = util::day_id(now); - - // slot → (总量, 各账号) - let mut by_slot: BTreeMap)> = BTreeMap::new(); - for (name, _) in state::iter_prefix(KEY_UMS_PREFIX) { - let Some((username, slot)) = name.split_once(':') else { - continue; - }; - let u = usage::read(KEY_UMS_PREFIX, &name, day); - let entry = by_slot.entry(slot.to_owned()).or_default(); - entry.0.merge(&u); - entry.1.push((username.to_owned(), u)); - } - - by_slot - .into_iter() - .map(|(slot, (usage, mut users))| { - users.sort_by(|a, b| a.0.cmp(&b.0)); - ModelUsage { slot, usage, users } - }) - .collect() -} - -/// 全局合计 -pub fn totals(now: i64) -> Usage { - let day = util::day_id(now); - let mut sum = Usage::default(); - for (name, _) in state::iter_prefix(KEY_UST_PREFIX) { - sum.merge(&usage::read(KEY_UST_PREFIX, &name, day)); - } - sum -} - -/// 单账号 token 读数 -pub fn tokens_user(username: &str, now: i64) -> TokenUsage { - let day = util::day_id(now); - usage::read_t(KEY_UTT_PREFIX, username, day) -} - -/// token 全表一次扫齐:`(按账号, 按上游, 按 账号→上游)`。 -/// utt/utms 各扫一遍,避免报表页多次遍历共享内存。 -fn tokens_scan( - now: i64, -) -> ( - BTreeMap, - BTreeMap, - BTreeMap>, -) { - let day = util::day_id(now); - let mut by_user: BTreeMap = BTreeMap::new(); - for (name, _) in state::iter_prefix(KEY_UTT_PREFIX) { - let t = usage::read_t(KEY_UTT_PREFIX, &name, day); - by_user.insert(name, t); - } - let mut by_slot: BTreeMap = BTreeMap::new(); - let mut by_pair: BTreeMap> = BTreeMap::new(); - for (name, _) in state::iter_prefix(KEY_UTMS_PREFIX) { - let Some((username, slot)) = name.split_once(':') else { - continue; - }; - let t = usage::read_t(KEY_UTMS_PREFIX, &name, day); - by_slot.entry(slot.to_owned()).or_default().merge(&t); - by_pair.entry(username.to_owned()).or_default().insert(slot.to_owned(), t); - } - (by_user, by_slot, by_pair) -} - -/// 管理台用量页的完整投影 -pub fn overview(now: i64) -> Value { - let (t_by_user, t_by_slot, t_by_pair) = tokens_scan(now); - - let users: Vec = all_users(now) - .into_iter() - .map(|u| { - let tin = t_by_pair.get(&u.username); - json!({ - "username": u.username, - "today": u.usage.today, - "total": u.usage.total, - "last_ts": u.usage.last_ts, - "last_ts_text": if u.usage.last_ts > 0 { util::fmt_time(u.usage.last_ts) } else { String::new() }, - "tokens": t_by_user.get(&u.username).cloned().unwrap_or_default().json(), - "models": u.models.iter().map(|(slot, usage)| { - json!({ "slot": slot, "today": usage.today, "total": usage.total, - "tokens": tin.and_then(|m| m.get(slot)).cloned().unwrap_or_default().json() }) - }).collect::>(), - }) - }) - .collect(); - - let models: Vec = all_models(now) - .into_iter() - .map(|m| { - json!({ - "slot": m.slot, - "today": m.usage.today, - "total": m.usage.total, - "last_ts": m.usage.last_ts, - "last_ts_text": if m.usage.last_ts > 0 { util::fmt_time(m.usage.last_ts) } else { String::new() }, - "tokens": t_by_slot.get(&m.slot).cloned().unwrap_or_default().json(), - "users": m.users.iter().map(|(name, usage)| { - json!({ "username": name, "today": usage.today, "total": usage.total }) - }).collect::>(), - }) - }) - .collect(); - - let sum = totals(now); - let mut tsum = TokenUsage::default(); - for t in t_by_user.values() { - tsum.merge(t); - } - json!({ - "total": sum.json(), - "tokens": tsum.json(), - "users": users, - "models": models, - }) -} - -// =========================================================================== -// 趋势(图表数据源) -// =========================================================================== - -/// 把一串日桶摊成图表直接能吃的三条等长数组 -fn buckets_json(v: &[usage::DayBucket]) -> Value { - json!({ - "requests": v.iter().map(|b| b.requests).collect::>(), - "in": v.iter().map(|b| b.tin).collect::>(), - "out": v.iter().map(|b| b.tout).collect::>(), - }) -} - -/// 最近 `days_req` 天的趋势序列(全局 / 按账号 / 按上游)。 -/// -/// `days_req` 会被夹到 `[1, TREND_DAYS_MAX]`——URL 里塞个 99999 也不会把 -/// 共享内存扫爆。三个维度都是**按键精确取**(键名带日窗 id), -/// 成本 = 天数 × (1 + 账号数 + 上游数) 次查表,且不随历史增长。 -/// -/// 注意:调用方(gateway 的 `/api/admin/trend`)要先给名册——日桶键里 -/// 带日窗 id,**扫不出来**,账号/上游名单由 users / upstreams / apix 提供。 -pub fn trend(now: i64, days_req: i64, users: &[String], slots: &[String]) -> Value { - let days = days_req.clamp(1, TREND_DAYS_MAX); - // 往前推 N 天:每个采样点一个时间戳,日窗 id 现算(本地自然日,不用 UTC) - let stamps: Vec = (0..days).rev().map(|i| now - i * 86400).collect(); - let labels: Vec = stamps.iter().map(|t| util::fmt_day(*t)).collect(); - let ids: Vec = stamps.iter().map(|t| util::day_id(*t)).collect(); - - let total: Vec = - ids.iter().map(|d| usage::day_bucket(KEY_UDAY_PREFIX, &d.to_string())).collect(); - - let by_user: Vec = users - .iter() - .map(|u| { - let series: Vec = ids - .iter() - .map(|d| usage::day_bucket(KEY_UDAY_USER_PREFIX, &format!("{u}:{d}"))) - .collect(); - let mut o = buckets_json(&series); - // 全 0 的账号不回传:趋势图里一堆积零的线只会把图例挤爆 - let empty = series.iter().all(|b| b.requests == 0 && b.tin == 0 && b.tout == 0); - if let Value::Object(ref mut m) = o { - m.insert("username".to_owned(), json!(u)); - m.insert("empty".to_owned(), json!(empty)); - } - o - }) - .collect(); - - let by_slot: Vec = slots - .iter() - .map(|s| { - let series: Vec = ids - .iter() - .map(|d| usage::day_bucket(KEY_UDAY_SLOT_PREFIX, &format!("{s}:{d}"))) - .collect(); - let empty = series.iter().all(|b| b.requests == 0 && b.tin == 0 && b.tout == 0); - let mut o = buckets_json(&series); - if let Value::Object(ref mut m) = o { - m.insert("slot".to_owned(), json!(s)); - m.insert("empty".to_owned(), json!(empty)); - } - o - }) - .collect(); - - json!({ - "days": days, - "labels": labels, - "total": buckets_json(&total), - "users": by_user, - "slots": by_slot, - }) -} - -/// 趋势接口的默认天数(URL 没给 `days` 时用) -pub fn trend_default_days() -> i64 { - TREND_DAYS_DEFAULT -} diff --git a/rust/crates/plugin-llm/src/stats_persist.rs b/rust/crates/plugin-llm/src/stats_persist.rs deleted file mode 100644 index af4984e..0000000 --- a/rust/crates/plugin-llm/src/stats_persist.rs +++ /dev/null @@ -1,463 +0,0 @@ -//! 运行期统计的**落盘 / 恢复**:让用量统计与趋势图熬过一次重启。 -//! -//! ## 为什么需要它(1.0.18 之前的实际故障) -//! -//! 共享内存是 nginx 每进程启动时 `mmap` 出来的匿名区,**进程退出即消失**。 -//! 而用量统计(`ust:`/`ums:`/`utt:`/`utms:`/`ud:`/`udu:`/`uds:`/`uda:`/`uex:`) -//! 全部只活在共享内存里 —— 于是「每次发版 = 统计与趋势清零」, -//! 用量页与趋势图在发版后从 0 重新长起来。 -//! -//! 不引入数据库(项目红线),做法就是**一个 JSON 快照**,与 -//! `users.json`/`llm-upstreams.json` 同级落在 `data/` 下;`data/` 是宿主挂载卷, -//! 容器重建也不丢。 -//! -//! ## 两个落盘点(互补,不是二选一) -//! -//! | 触发 | 时机 | 覆盖场景 | -//! | --- | --- | --- | -//! | **worker 退出钩子** | `exit_process`(gateway 注册) | 发版 / `docker stop` / `nginx -s quit` ——**零丢失** | -//! | **周期性节流落盘** | log 阶段记账后,默认 60 秒一次 | SIGKILL / OOM / 断电 —— 最多丢一个周期 | -//! -//! 退出钩子是主力:`docker stop` 发的是镜像里声明的 `STOPSIGNAL=SIGQUIT` -//! (优雅停机),`SIGTERM` 也走同一条 worker 退出路径,两者都会调到 `exit_process`。 -//! 只有 `SIGKILL` 绕过它,那由周期落盘兜住。 -//! -//! ## 恢复语义:**只在键不存在时写入**(put-if-absent) -//! -//! 三个性质一次拿到: -//! * 重启后共享内存是空的 → 所有键都「不存在」→ 全量恢复; -//! * 多 worker 各跑一遍 `init_process` → 第二个起键已存在 → 跳过,**天然幂等**, -//! 不会把计数恢复成 N 倍; -//! * 恢复期间若有新请求已经把某个键写活 → **不覆盖**,宁可少恢复一条, -//! 也不打死刚产生的新数据。 -//! -//! 已过期(`exp <= now`)的键不恢复:日桶的 90 天保留期不能因为一次重启被续命。 -//! -//! 恢复结果(恢复了多少 / 快照里有多少)**发布到共享内存**(`spr:last`), -//! 而不是放进程内静态量:`init_process` 每 worker 跑一次,只有第一个 worker -//! 真的在写键,其余全部跳过 —— 用静态量的话 `/api/admin/stats/persist` 落到哪个 -//! worker 就报哪个数,运维看到的是随机值。 -//! -//! **同一类错误在落盘侧也有一份**:`maybe_dump` 由记账路径驱动、每 worker 独立 -//! 计数,所以「上次落盘时刻」同样是每 worker 各一个。1.0.18 首次线上验证就撞上 -//! 了 —— 日志里 10 次落盘、状态页却说「本进程还没落过」。解法一样:时刻存 -//! `spd:last`(见 `publish_dump`);顺带用它把落盘节流从「每 worker 一次」收敛成 -//! 「全局一次」(见 `maybe_dump` 的慢路径)。 -//! -//! ## 刻意**不**持久化的东西 -//! -//! * **配置类** `up:` / `upa:` / `upe:` / `ax:` / `ukey:` / `sub:` —— 它们各自有 -//! 数据文件(`data/*.json`),重启时由 `reload_shm` 装载。再存一份会出现两个真源, -//! 「文件是新的、快照是旧的」时互相覆盖,属于自找的故障。 -//! * **`ukb:`**(用户 key → 上游账号粘性)—— 设计上就是重启即重绑(见常量注释)。 -//! * **`spr:` / `spd:`**(本模块自己的「上次恢复」/「上次落盘」记录)—— 都是过程 -//! 元信息,不是统计量,持久化它们只会自我引用。**只在共享内存里**,为的是让 -//! 多 worker 报同一份结果。 -//! * **内核的** `st:`(配额)/ `rl:`(限流)/ `fail:` / `sess:` —— 语义上重启清零更安全 -//! (多送一点额度,好过把用户锁死),且属内核状态,不由本插件代管。 -//! -//! ## 容量 -//! -//! 一个键一行;键数 = 账号数 × slot 数 + 90 天 × (1 + 账号数 + slot 数 + 账号 × 上游数)。 -//! 十来个账号的规模下是几十 KB,写好一次几毫秒 —— 与「每请求都写」的量级完全不同。 -//! 详细口径与运维见 `docs/llm/STATS_PERSISTENCE.md`。 - -use std::collections::BTreeMap; -use std::path::PathBuf; -use std::sync::atomic::{AtomicBool, AtomicI64, Ordering}; - -use serde::{Deserialize, Serialize}; -use serde_json::{Value, json}; - -use ipass_core::{state, store, util}; - -use crate::config::{ - KEY_SPD_PREFIX, KEY_SPR_PREFIX, KEY_UDAY_ACCT_PREFIX, KEY_UDAY_PREFIX, - KEY_UDAY_SLOT_PREFIX, KEY_UDAY_USER_PREFIX, KEY_UEX_PREFIX, KEY_UH_PREFIX, KEY_UMS_PREFIX, - KEY_UTMS_PREFIX, KEY_UST_PREFIX, KEY_UTT_PREFIX, KEY_USER_SUM_PREFIX, STATS_FILE_NAME, - STATS_PERSIST_INTERVAL_DEFAULT, STATS_SNAPSHOT_VERSION, -}; - -/// 参与持久化的共享内存前缀(一律以 `:` 结尾,且 `:` 不出现在前缀内部)。 -/// -/// 顺序无关,但保持一致(状态接口按此顺序统计 key 数)。 -/// 排除项的理由见模块注释「刻意不持久化的东西」。 -const PREFIXES: &[&[u8]] = &[ - KEY_UST_PREFIX, // 账号维度请求数 - KEY_UMS_PREFIX, // 账号 × 上游 请求数 - KEY_UTT_PREFIX, // 账号维度 token - KEY_UTMS_PREFIX, // 账号 × 上游 token - KEY_UDAY_PREFIX, // 全局日桶(趋势图总览) - KEY_UDAY_USER_PREFIX, // 按账号日桶 - KEY_UDAY_SLOT_PREFIX, // 按上游日桶 - KEY_UDAY_ACCT_PREFIX, // 按上游账号日桶(「各 Key 负载」) - KEY_UEX_PREFIX, // 账号额度耗尽次数日桶 - KEY_UH_PREFIX, // 上游健康展示缓存 - KEY_USER_SUM_PREFIX, // 每用户聚合缓存(1.0.32 /api/v1/me 增量) -]; - -/// 快照值里的分隔符:`\x1f`。 -/// -/// 复用共享内存的 `VAL_SEP`(0x1f)而不是自定义一个:语义完全一样 -/// (payload 里不会出现该字节,因为内核写共享内存时就是这么切的), -/// 少一个常量少一处不一致。JSON 序列化时 serde 会把它转义成 `\u001f`。 -const SNAP_SEP: char = '\u{1f}'; - -/// 落盘间隔(秒);`0` = 关闭周期落盘(只保留退出钩子那一次)。 -/// 配置阶段由 `ipass_stats_persist` 设置一次,之后只读。 -static INTERVAL: AtomicI64 = AtomicI64::new(STATS_PERSIST_INTERVAL_DEFAULT); - -/// 本 worker 上次落盘时刻(秒);0 = 本 worker 还没落过盘 → 第一次调用就落。 -/// -/// 这只是**快路径缓存**(无锁原子比较,热路径几乎免费),**不是真源** —— -/// 真源是共享内存 `spd:last`(跨 worker 一致),见 `publish_dump` / `read_dump_at`。 -/// 两者配合的理由见 `maybe_dump`。 -static LAST_DUMP_AT: AtomicI64 = AtomicI64::new(0); - -/// 单飞标记:并发请求同时到点,只放一个进去落盘。 -/// 注意这是**每 worker** 的静态量(跨 worker 不互斥)——多个 worker 同时落盘 -/// 只是白写几次同样的内容,`rename` 是原子的,不会有半个文件被人读到。 -static DUMPING: AtomicBool = AtomicBool::new(false); - -#[derive(Debug, Serialize, Deserialize)] -struct Snapshot { - version: i64, - saved_at: i64, - #[serde(default)] - saved_at_text: String, - #[serde(default)] - keys: BTreeMap, -} - -// =========================================================================== -// 配置 -// =========================================================================== - -/// `ipass_stats_persist <秒>`:周期落盘间隔。`0` = 只靠 worker 退出钩子。 -pub fn set_interval(secs: i64) { - INTERVAL.store(secs.max(0), Ordering::Relaxed); -} - -pub fn interval() -> i64 { - INTERVAL.load(Ordering::Relaxed) -} - -fn path() -> PathBuf { - store::path_in_data(STATS_FILE_NAME) -} - -// =========================================================================== -// 落盘 -// =========================================================================== - -/// 原子写:先写 `.tmp.` 再 `rename`。 -/// -/// 不复用 `store::write_json` 的原因:它用固定的 `.tmp`,多 worker 同时落盘 -/// 会互相踩临时文件(一个 rename 走了,另一个还在往里写同名文件)。 -/// 带 pid 后缀后各写各的,`rename` 本身是原子的,最后一个赢 —— 而内容本来就 -/// 只是「同一份共享内存的近似快照」,谁赢都对。 -/// -/// 也**不复用** `state::file_lock()`:那道锁是给「读文件→改→写文件」防丢失用的, -/// 本函数是纯覆盖写、不读旧值,拿锁只会在退出路径上增加卡死风险。 -fn write_atomic(path: &PathBuf, text: &str) -> Result<(), String> { - let tmp = PathBuf::from(format!("{}.tmp.{}", path.display(), std::process::id())); - std::fs::write(&tmp, text.as_bytes()).map_err(|e| { - let _ = std::fs::remove_file(&tmp); - format!("写入 {} 失败:{e}", tmp.display()) - })?; - std::fs::rename(&tmp, path).map_err(|e| { - let _ = std::fs::remove_file(&tmp); - format!("重命名到 {} 失败:{e}", path.display()) - }) -} - -/// 立刻落盘(不看间隔)。返回写入的键数。 -/// -/// `reason` 只进日志,用于事后分辨这次是谁触发的(`worker-exit` / `periodic` / `admin`)。 -pub fn dump_now(reason: &str) -> Result { - let now = util::now(); - let mut keys: BTreeMap = BTreeMap::new(); - - for p in PREFIXES { - // 前缀都是 ASCII 字面量,from_utf8 不会失败 - let Some(pfx) = core::str::from_utf8(p).ok() else { - continue; - }; - for (name, payload, exp) in state::iter_prefix_ex(p) { - // 已过期的键不带回:日桶的 90 天保留期不能靠重启续命 - if exp != 0 && exp <= now { - continue; - } - keys.insert(format!("{pfx}{name}"), format!("{payload}{SNAP_SEP}{exp}")); - } - } - - let snap = Snapshot { - version: STATS_SNAPSHOT_VERSION, - saved_at: now, - saved_at_text: util::fmt_time(now), - keys, - }; - let text = serde_json::to_string(&snap).map_err(|e| format!("序列化失败:{e}"))?; - let n = snap.keys.len(); - let p = path(); - write_atomic(&p, &text)?; - // 落盘成功才推进时间戳:失败时下次请求会立刻重试,而不是等满一个周期。 - // 静态量是本 worker 的快路径缓存;共享内存那份是跨 worker 的真源(供上报 + 节流)。 - LAST_DUMP_AT.store(now, Ordering::Relaxed); - publish_dump(now); - util::log_info(&format!( - "[stats] 落盘 {n} 键({} B,{reason})-> {}", - text.len(), - p.display() - )); - Ok(n) -} - -/// 把「上次落盘时刻」发布到共享内存,供**所有** worker 一致地上报与节流。 -/// -/// 与 `publish_restore` 是同一类问题的同一解法(见 `KEY_SPD_PREFIX` 注释)。 -/// 与恢复侧唯一不同:落盘**不需要累加**,只要记住最近一次的时刻, -/// 所以取 `max` 而不是相加。 -/// -/// 这里是「读-改-写」,没有原子性保证:两个 worker 同一瞬间落盘时,理论上可能出现 -/// 后写入的是稍早的时刻(差 ≤1 秒)。对本用途(状态页展示 + 分钟级节流)无影响, -/// 不值得为它引入跨 worker 的 CAS —— `state` 层本来也没有这个原语。 -fn publish_dump(at: i64) { - let prev = read_dump_at(); - let v = at.max(prev).to_string(); - if !state::put(KEY_SPD_PREFIX, b"last", v.as_bytes(), 0) { - util::log_err("[stats] 发布落盘时刻到共享内存失败(仅影响状态展示与节流)"); - } -} - -/// 读回「上次落盘时刻」(跨 worker 共享)。没有记录则 0。 -fn read_dump_at() -> i64 { - state::get(KEY_SPD_PREFIX, b"last") - .and_then(|(payload, _)| payload.trim().parse::().ok()) - .unwrap_or(0) -} - -/// 按间隔节流落盘。**由记账路径每次请求调用一次**(见 `stats::record`)。 -/// -/// 两级判断,热路径代价仍是一次原子比较: -/// * **快路径**(绝大多数请求走这里):本 worker 自己在 `iv` 秒内落过 → 直接返回; -/// * **慢路径**(每 worker 每 `iv` 秒最多进一次):本 worker 没落过,但**别的** worker -/// 可能刚落过 —— 读一次 `spd:last` 确认。读到的时刻回填本地缓存, -/// 于是下一个 `iv` 秒内的请求又全部走快路径。 -/// -/// 慢路径那一下把落盘从「8 个 worker 各写一遍同一个文件」收敛成「全局每 `iv` 秒一次」, -/// 同时也让状态页读到的时刻与节流依据是同一份数据,不会自相矛盾。 -/// -/// 间隔设为 0 = 关闭周期落盘,此时这里退化为纯判断。 -pub fn maybe_dump() { - let iv = interval(); - if iv <= 0 { - return; - } - let now = util::now(); - - let local = LAST_DUMP_AT.load(Ordering::Relaxed); - if local != 0 && now - local < iv { - return; - } - - let shared = read_dump_at(); - if shared != 0 && now - shared < iv { - LAST_DUMP_AT.store(shared, Ordering::Relaxed); // 回填缓存,别每次请求都读共享内存 - return; - } - - if DUMPING.swap(true, Ordering::Acquire) { - return; // 同 worker 内已有一个在落盘 - } - let r = dump_now("periodic"); - DUMPING.store(false, Ordering::Release); - if let Err(e) = r { - util::log_err(&format!("[stats] 周期落盘失败:{e}")); - } -} - -// =========================================================================== -// 恢复 -// =========================================================================== - -/// 拆快照值 `payload \x1f exp`。 -/// -/// 用 `rposition` 与内核 `split_value` 同款理由:`exp` 是 ASCII 数字串、 -/// 不含分隔符,所以**最后一个**分隔符才是真边界。 -fn split_value(v: &str) -> Option<(&str, i64)> { - let i = v.rfind(SNAP_SEP)?; - let exp = v[i + SNAP_SEP.len_utf8()..].parse::().ok()?; - Some((&v[..i], exp)) -} - -/// 把本次恢复结果发布到共享内存,供**所有** worker 一致地报告(见 `KEY_SPR_PREFIX` 注释)。 -/// -/// 两个要点: -/// * `restored == 0` 的 worker **不写** —— 它只是「来晚了、键已存在」的观察者, -/// 用自己的 0 覆盖真实结果就等于把这次恢复从记录里抹掉。 -/// * 已有记录时**累加**而不是覆盖:master 是顺序 fork 的,但两个 worker 的 -/// `init_process` 仍可能重叠,各恢复一半(put-if-absent 决定谁抢到哪批键)。 -/// 累加 + `max(total)` 才能拼出完整结果。 -fn publish_restore(at: i64, restored: usize, total: usize) { - if restored == 0 { - return; - } - let (prev_at, prev_ok, prev_total) = match state::get(KEY_SPR_PREFIX, b"last") { - Some((payload, _)) => parse_restore(&payload).unwrap_or((0, 0, 0)), - None => (0, 0, 0), - }; - let v = format!( - "{}{SNAP_SEP}{}{SNAP_SEP}{}", - at.max(prev_at), - prev_ok + restored, - prev_total.max(total) - ); - if !state::put(KEY_SPR_PREFIX, b"last", v.as_bytes(), 0) { - util::log_err("[stats] 发布恢复记录到共享内存失败(仅影响状态展示)"); - } -} - -/// 解析 `spr:last` 的 payload:`<时刻> \x1f <恢复键数> \x1f <快照键数>`。 -fn parse_restore(payload: &str) -> Option<(i64, usize, usize)> { - let mut it = payload.split(SNAP_SEP); - let at = it.next()?.parse::().ok()?; - let ok = it.next()?.parse::().ok()?; - let total = it.next()?.parse::().ok()?; - Some((at, ok, total)) -} - -/// 读回「上次恢复」记录:`(恢复时刻, 恢复键数, 快照键数)`。没有记录则全 0。 -fn read_restore() -> (i64, usize, usize) { - state::get(KEY_SPR_PREFIX, b"last") - .and_then(|(payload, _)| parse_restore(&payload)) - .unwrap_or((0, 0, 0)) -} - -/// worker 启动时把快照灌回共享内存。每个 worker 都会调一次(幂等,见模块注释)。 -/// -/// 任何异常都只降级:审计/统计都是旁路,绝不能让 worker 起不来。 -pub fn restore() { - let p = path(); - let text = match std::fs::read_to_string(&p) { - Ok(t) => t, - // 首次部署本来就没有快照,不算错误 - Err(e) if e.kind() == std::io::ErrorKind::NotFound => return, - Err(e) => { - util::log_err(&format!("[stats] 读取快照失败 {}:{e}", p.display())); - return; - } - }; - if text.trim().is_empty() { - return; - } - - let snap: Snapshot = match serde_json::from_str(&text) { - Ok(s) => s, - Err(e) => { - // 解析不了就**改名留档**再走:否则第一个请求到点就会用「空共享内存」 - // 覆盖掉这份文件,把唯一的副本抹掉,连现场都没得查。 - let bad = PathBuf::from(format!("{}.bad", p.display())); - let moved = std::fs::rename(&p, &bad); - util::log_err(&format!( - "[stats] 快照解析失败({e}),已{},本次不恢复统计", - match moved { - Ok(()) => format!("改名留档到 {}", bad.display()), - Err(me) => format!("尝试改名留档但失败({me})"), - } - )); - return; - } - }; - - if snap.version != STATS_SNAPSHOT_VERSION { - util::log_err(&format!( - "[stats] 快照版本 {} 与当前 {} 不符,不恢复(宁可从零开始)", - snap.version, STATS_SNAPSHOT_VERSION - )); - return; - } - - let now = util::now(); - let total = snap.keys.len(); - let mut restored = 0usize; - for (k, v) in &snap.keys { - let kb = k.as_bytes(); - let Some(pfx) = PREFIXES.iter().find(|p| kb.starts_with(p)) else { - continue; // 不在白名单(旧版本写的 / 手改的)→ 忽略 - }; - let Some((payload, exp)) = split_value(v) else { - continue; - }; - if exp != 0 && exp <= now { - continue; // 已过期 - } - let name = &kb[pfx.len()..]; - if name.is_empty() { - continue; - } - if state::get(pfx, name).is_some() { - continue; // 已被新请求写活 → 不覆盖 - } - if state::put(pfx, name, payload.as_bytes(), exp) { - restored += 1; - } - } - - publish_restore(now, restored, total); - util::log_notice(&format!( - "[stats] 已从快照恢复 {restored}/{total} 键({})", - p.display() - )); -} - -// =========================================================================== -// 状态(管理接口) -// =========================================================================== - -/// `/api/admin/stats/persist` 的响应体:快照文件状态 + 当前共享内存键数。 -/// -/// 「文件里有多少键」与「共享内存里现在有多少键」分开给:两者长期接近说明 -/// 落盘正常;文件远小于内存 = 周期落盘没跑(或刚重启还没到点)。 -pub fn status() -> Value { - let p = path(); - let text = std::fs::read_to_string(&p).ok(); - let (saved_at, saved_at_text, file_keys) = match text.as_deref().map(serde_json::from_str::) { - Some(Ok(s)) => (s.saved_at, s.saved_at_text, s.keys.len()), - _ => (0, String::new(), 0), - }; - let file_bytes = std::fs::metadata(&p).map(|m| m.len()).unwrap_or(0); - - let live: usize = PREFIXES - .iter() - .map(|pfx| { - let now = util::now(); - state::iter_prefix_ex(pfx) - .into_iter() - .filter(|(_, _, exp)| *exp == 0 || *exp > now) - .count() - }) - .sum(); - - let (restore_at, restore_ok, restore_total) = read_restore(); - // 读共享内存而不是本 worker 静态量:落盘是每 worker 各自发生的, - // 用静态量会出现「日志明明落了 10 次、状态页却说本进程还没落过」(见 KEY_SPD_PREFIX)。 - let last_dump = read_dump_at(); - - json!({ - "file": p.display().to_string(), - "file_exists": text.is_some(), - "file_bytes": file_bytes, - "file_keys": file_keys, - "saved_at": saved_at, - "saved_at_text": saved_at_text, - "live_keys": live, - "interval_secs": interval(), - "last_dump_at": last_dump, - "last_dump_text": if last_dump > 0 { util::fmt_time(last_dump) } else { String::new() }, - "restored_at": restore_at, - "restored_keys": restore_ok, - "restore_total": restore_total, - }) -} diff --git a/rust/crates/plugin-llm/src/store.rs b/rust/crates/plugin-llm/src/store.rs deleted file mode 100644 index 2647170..0000000 --- a/rust/crates/plugin-llm/src/store.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! 上游表的文件读写(复用内核 store 的原子写 + 全局文件写锁)。 -//! -//! `data/llm-upstreams.json` 含上游真实 API Key,**敏感级别等同 `users.json`**。 - -use std::path::PathBuf; - -use ipass_core::{state, store}; - -use crate::models::UpstreamTable; - -fn upstreams_path() -> PathBuf { - store::path_in_data("llm-upstreams.json") -} - -pub fn read_upstreams() -> UpstreamTable { - store::read_json(&upstreams_path(), UpstreamTable::new()) -} - -pub fn write_upstreams(data: &UpstreamTable) -> Result<(), String> { - store::write_json(&upstreams_path(), data) -} - -/// 加锁「读-改-写」上游表。 -/// -/// 闭包返回 `Ok(T)` 表示提交,`Err(msg)` 表示放弃(不写盘)。 -pub fn update_upstreams(f: F) -> Result -where - F: FnOnce(&mut UpstreamTable) -> Result, -{ - let _guard = state::file_lock().ok_or("内部错误:状态锁不可用")?; - let mut data = read_upstreams(); - let out = f(&mut data)?; - write_upstreams(&data)?; - Ok(out) -} diff --git a/rust/crates/plugin-llm/src/upstreams.rs b/rust/crates/plugin-llm/src/upstreams.rs deleted file mode 100644 index fd9c518..0000000 --- a/rust/crates/plugin-llm/src/upstreams.rs +++ /dev/null @@ -1,1159 +0,0 @@ -//! LLM 上游配置与共享内存同步。 -//! -//! 这是「转发映射」机制在 LLM 侧的翻版:`domains.rs` 管 -//! `子域名 → 后端`,本模块管 `路径前缀(slot) → 上游(基址 + 密钥)`。 -//! 两者都**写穿共享内存**,理由相同——热路径未命中即失败,不回源读文件。 -//! -//! ## 为什么按 URL 前缀分上游,而不是按请求体里的 `model` -//! -//! 转发决策点在 access 阶段,而请求体要到 content 阶段才读得到 -//! (见 `http::collect_body` 的调用前提)。按 `model` 字段分流就必须放弃 -//! `proxy_pass`、自己实现 upstream 转发,代码量与出错面差一个数量级。 -//! 代价只是客户端 base_url 需要带上 slot 段,可接受。 -//! -//! `data/llm-upstreams.json` 含上游真实 API Key,**敏感级别等同 `users.json`**。 - -use core::sync::atomic::{AtomicI64, Ordering}; - -use serde_json::{Value, json}; - -use crate::config::{ - ACCT_COOL_HARD_CAP, ACCT_TREND_DAYS, AFFINITY_TTL_DEFAULT, EXHAUST_STATUS_DEFAULT, - KEY_UAC_PREFIX, KEY_UDAY_ACCT_PREFIX, KEY_UEX_PREFIX, KEY_UH_PREFIX, KEY_UKB_PREFIX, - KEY_UPA_PREFIX, KEY_UPE_PREFIX, KEY_UP_PREFIX, MODEL_NAME_MAX_LEN, PROBE_MIN_DEFAULT, - TREND_DAYS_MAX, TREND_KEEP_SECS, UPSTREAM_BASE_MAX_LEN, UPSTREAM_MODELS_MAX, - UPSTREAM_SLOT_MAX_LEN, -}; -use crate::models::{Upstream, UpstreamAccount}; -use crate::store; -use ipass_core::config::LLM_PUBLIC_HOST; -use ipass_core::{state, usage, util}; - -/// 共享内存 payload 的内部分隔符:base 与 auth 之间。 -/// -/// 与 `config::VAL_SEP`(0x1f,负责切 payload / expires_at)是两层不同的 -/// 分隔,不能混用。base 是 URL、auth 是 `Bearer `,都不可能含换行。 -pub const PAYLOAD_SEP: char = '\n'; - -// =========================================================================== -// 校验 -// =========================================================================== - -/// slot:小写字母/数字开头,其后可含短横线、下划线,总长 ≤ 32。 -/// -/// 允许下划线(`azure_openai` 这类厂牌写法很常见),这点比子域名规则宽。 -pub fn validate_slot(raw: &str) -> Result { - let s = util::trim(raw).to_ascii_lowercase(); - if s.is_empty() { - return Err("缺少 slot 参数".to_owned()); - } - if s.len() > UPSTREAM_SLOT_MAX_LEN { - return Err(format!("slot 长度不能超过 {UPSTREAM_SLOT_MAX_LEN} 个字符")); - } - let bytes = s.as_bytes(); - let head_ok = matches!(bytes[0], b'a'..=b'z' | b'0'..=b'9'); - let tail_ok = bytes[1..].iter().all(|&b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_')); - if !head_ok || !tail_ok { - return Err("slot 只能是小写字母、数字、短横线或下划线,且不能以符号开头".to_owned()); - } - Ok(s) -} - -/// 模型名清单:去空白、去空项、去重、限长。 -/// -/// 刻意**允许** `/` `.` `:` 等字符——`Qwen/Qwen2.5-7B`(HF 风格)、 -/// `llama3:8b`(Ollama 标签)都是合法模型名。只挡空白与控制字符, -/// 因为模型名要出现在 URL/JSON/HTML 里,空白只会带来歧义。 -fn check_models(raw: &[String]) -> Result, String> { - let mut out: Vec = Vec::new(); - for item in raw { - let m = util::trim(item); - if m.is_empty() { - continue; - } - if m.chars().count() > MODEL_NAME_MAX_LEN { - return Err(format!("模型名「{m}」超过 {MODEL_NAME_MAX_LEN} 个字符")); - } - if m.chars().any(char::is_whitespace) || m.chars().any(char::is_control) { - return Err(format!("模型名「{m}」不能包含空格或控制字符")); - } - if !out.iter().any(|x| x == m) { - out.push(m.to_owned()); - } - } - if out.len() > UPSTREAM_MODELS_MAX { - return Err(format!("单个上游最多登记 {UPSTREAM_MODELS_MAX} 个模型")); - } - Ok(out) -} - -/// 上游基址:必须显式带 `http://` 或 `https://`。 -/// -/// 比子域名的 `backend`(纯 host:port)宽——LLM 上游常挂在网关路径下 -/// (如 `https://gw.corp.com/llm`),所以要允许尾随路径。 -pub fn validate_base(raw: &str) -> Result { - let s = util::trim(raw).trim_end_matches('/').to_owned(); - if s.is_empty() { - return Err("缺少 base 参数".to_owned()); - } - if s.len() > UPSTREAM_BASE_MAX_LEN { - return Err(format!("base 长度不能超过 {UPSTREAM_BASE_MAX_LEN} 个字符")); - } - let rest = if let Some(r) = s.strip_prefix("https://") { - r - } else if let Some(r) = s.strip_prefix("http://") { - r - } else { - return Err("base 必须以 http:// 或 https:// 开头,例如 https://api.openai.com".to_owned()); - }; - if rest.is_empty() { - return Err("base 缺少主机名".to_owned()); - } - if rest.chars().any(char::is_whitespace) || rest.contains('@') { - return Err("base 不允许包含空格或用户信息(user@host)".to_owned()); - } - if rest.split('/').next().unwrap_or("").is_empty() { - return Err("base 缺少主机名".to_owned()); - } - Ok(s) -} - -/// 从 OpenAI 基址兜底派生 Anthropic 基址。 -/// -/// 仅对「`base` 末尾是 `/v1`(deepseek/openai)或根路径(minimax)」的厂商正确; -/// 阿里(`/compatible-mode/v1` → `/apps/anthropic`)、智谱 -/// (`/api/coding/paas/v4` → `/api/anthropic`)路径完全对不上,**必须显式配 -/// `base_anthropic`**。留这个兜底是为了让「只配了 OpenAI 地址」的旧上游不至于 -/// 在 `//anthropic` 上 404。 -fn derive_anthropic(base: &str) -> String { - let t = base.trim_end_matches('/'); - if let Some(stripped) = t.strip_suffix("/v1") { - format!("{stripped}/anthropic") - } else { - format!("{t}/anthropic") - } -} - -/// 取生效的 Anthropic 基址:显式配了用显式的,否则派生。 -fn effective_anthropic(up: &Upstream) -> String { - if up.base_anthropic.trim().is_empty() { - derive_anthropic(&up.base) - } else { - up.base_anthropic.trim().to_owned() - } -} - -// =========================================================================== -// 共享内存同步(写穿 / 装载) -// =========================================================================== - -/// 一个 slot 最多支持的账号下标(清理残留旧账号下标时遍历的上界)。 -/// 实际部署不会接近这个数;超出部分不被支持(管理台会拦)。 -const ACCOUNT_INDEX_MAX: usize = 64; - -/// `up:` payload:`base \n base_anthropic \n <账号数>`。 -fn up_payload(base: &str, base_anthropic: &str, n: usize) -> String { - format!("{base}\n{base_anthropic}\n{n}") -} - -/// `upa::` payload:`auth \n `(resets 空则第二段为空)。 -fn upa_payload(auth: &str, resets: &[i64]) -> String { - let csv = resets - .iter() - .map(|x| x.to_string()) - .collect::>() - .join(","); - format!("{auth}\n{csv}") -} - -/// 把一条上游配置同步进共享内存。**所有写操作之后必须调用它**。 -/// -/// 关闭(`enabled=false`)时是**删除**整个 slot 的键(up/upa/uac/upe),而不是 -/// 写入空值——热路径「查不到 = 不可用」,停用即刻生效且不留残留。 -/// -/// 多账号布局(见 `config` 的键前缀注释): -/// * `up:` = `base \n <账号数>` -/// * `upa::` = `auth \n `(每个账号一条) -/// * `upe:` = ` \n `(上游级耗尽探测配置) -/// * `uac::`(冷却运行时态)**不在此清除**——保留到自然到期, -/// 这样「正在冷却的账号」不会因为一次普通的 upsert(比如改备注)而莫名复活。 -pub fn sync_shm(slot: &str, up: &Upstream) { - if !up.enabled { - remove_slot_shm(slot); - return; - } - let accts = up.effective_accounts(); - let ba = effective_anthropic(up); - - if !state::put( - KEY_UP_PREFIX, - slot.as_bytes(), - up_payload(&up.base, &ba, accts.len()).as_bytes(), - 0, - ) { - util::log_err(&format!( - "[upstreams] 上游 base 写共享内存失败 slot={slot} —— 该上游会 404(共享内存可能已满)" - )); - } - write_upe(slot, up); - - for (i, a) in accts.iter().enumerate() { - let ak = format!("{slot}:{i}"); - if !state::put( - KEY_UPA_PREFIX, - ak.as_bytes(), - upa_payload(&a.auth, &a.resets).as_bytes(), - 0, - ) { - util::log_err(&format!( - "[upstreams] 上游账号写共享内存失败 slot={slot}#{i} —— 该账号会 404" - )); - } - } - // 清掉被删/被减的账号下标(i >= 现账号数),避免旧 upa/uac 残留被误读 - for i in accts.len()..ACCOUNT_INDEX_MAX { - state::remove(KEY_UPA_PREFIX, format!("{slot}:{i}").as_bytes()); - state::remove(KEY_UAC_PREFIX, format!("{slot}:{i}").as_bytes()); - } -} - -/// 删除一个 slot 在共享内存里的全部键(up / upe / upa* / uac*)。 -pub fn remove_slot_shm(slot: &str) { - state::remove(KEY_UP_PREFIX, slot.as_bytes()); - state::remove(KEY_UPE_PREFIX, slot.as_bytes()); - for i in 0..ACCOUNT_INDEX_MAX { - state::remove(KEY_UPA_PREFIX, format!("{slot}:{i}").as_bytes()); - state::remove(KEY_UAC_PREFIX, format!("{slot}:{i}").as_bytes()); - } -} - -/// 写上游级「额度耗尽」探测配置:`upe:` = ` \n `。 -fn write_upe(slot: &str, up: &Upstream) { - let status = up - .effective_exhaust_status() - .iter() - .map(|x| x.to_string()) - .collect::>() - .join(","); - let markers = serde_json::to_string(&up.exhaust_markers).unwrap_or_else(|_| "[]".to_owned()); - let payload = format!("{status}\n{markers}"); - if !state::put(KEY_UPE_PREFIX, slot.as_bytes(), payload.as_bytes(), 0) { - util::log_err(&format!("[upstreams] 上游耗尽配置写共享内存失败 slot={slot}")); - } -} - -/// 全量重载上游表(启动装载 / 批量变更后)。返回装载条数。 -/// -/// 清 `up:` / `upa:` / `uac:` / `upe:` 四类前缀(**不**清 `ukb:` 粘性绑定—— -/// 重启后重新绑定本就是预期语义)。 -pub fn reload_shm() -> usize { - state::clear_prefix(KEY_UP_PREFIX); - state::clear_prefix(KEY_UPA_PREFIX); - state::clear_prefix(KEY_UAC_PREFIX); - state::clear_prefix(KEY_UPE_PREFIX); - let mut expected = 0; - let mut loaded = 0; - for (slot, up) in store::read_upstreams() { - if !up.enabled { - continue; - } - expected += 1; - let accts = up.effective_accounts(); - let ba = effective_anthropic(&up); - if state::put( - KEY_UP_PREFIX, - slot.as_bytes(), - up_payload(&up.base, &ba, accts.len()).as_bytes(), - 0, - ) { - loaded += 1; - } - write_upe(&slot, &up); - for (i, a) in accts.iter().enumerate() { - let ak = format!("{slot}:{i}"); - state::put( - KEY_UPA_PREFIX, - ak.as_bytes(), - upa_payload(&a.auth, &a.resets).as_bytes(), - 0, - ); - } - } - if loaded != expected { - util::log_err(&format!( - "[upstreams] 上游装载不全:{loaded}/{expected} —— 有上游会 404" - )); - } - loaded -} - -/// 读 `up:`:`(base_openai, base_anthropic, 账号数)`。 -pub fn read_up(slot: &str) -> Option<(String, String, usize)> { - let (payload, _) = state::get(KEY_UP_PREFIX, slot.as_bytes())?; - let mut it = payload.split('\n'); - let base = it.next().unwrap_or("").to_owned(); - let ba = it.next().unwrap_or("").to_owned(); - let n = it.next().unwrap_or("0").trim().parse::().unwrap_or(0); - Some((base, ba, n)) -} - -/// 规范化账号 Authorization 头值:缺 scheme 时自动补 `Bearer `。 -/// -/// 用途:**兜住「只填了裸 key 没填 `Bearer `」这种历史坏数据**。表单 placeholder -/// 写的是 `sk-...`,用户照着填 → 之前保存的就是 `sk-cp-...` 这种裸 key, -/// 上游(MiniMax)只认 `Bearer `,于是回 1004。 -/// -/// 规则: -/// * 空 → 原样返回("未配 Key"是合法状态,不是数据错); -/// * 含空格 → 原样返回(已是完整头值,如 `Bearer xxx` / `Basic xxx`,别动); -/// * 不含空格 → 补 `Bearer ` 前缀。 -/// -/// 同时也在 `upsert` 里应用:下次保存时旧数据自动归正,无需用户手动重填。 -pub fn normalize_auth(auth: &str) -> String { - let t = auth.trim(); - if t.is_empty() { - return String::new(); - } - if t.contains(char::is_whitespace) { - return t.to_owned(); - } - format!("Bearer {}", t) -} - -/// 读 `upa::`:`(auth, resets)`。 -pub fn read_upa(slot: &str, i: usize) -> Option<(String, Vec)> { - let (payload, _) = state::get(KEY_UPA_PREFIX, format!("{slot}:{i}").as_bytes())?; - let (auth, rest) = payload.split_once('\n').unwrap_or((&payload, "")); - let resets = if rest.trim().is_empty() { - Vec::new() - } else { - rest.split(',') - .filter_map(|s| s.trim().parse::().ok()) - .collect() - }; - Some((normalize_auth(auth), resets)) -} - -/// 读 `upe:`:`(status 列表, markers 列表)`。 -pub fn read_upe(slot: &str) -> (Vec, Vec) { - let (payload, _) = match state::get(KEY_UPE_PREFIX, slot.as_bytes()) { - Some(p) => p, - None => return (EXHAUST_STATUS_DEFAULT.to_vec(), Vec::new()), - }; - let (status_str, markers_str) = payload.split_once('\n').unwrap_or((&payload, "[]")); - let status = if status_str.trim().is_empty() { - EXHAUST_STATUS_DEFAULT.to_vec() - } else { - status_str - .split(',') - .filter_map(|s| s.trim().parse::().ok()) - .collect() - }; - let markers = serde_json::from_str::>(markers_str.trim()) - .unwrap_or_else(|_| Vec::new()); - (status, markers) -} - -// =========================================================================== -// 额度耗尽冷却:指数退避 + 真实请求探测(1.0.44 探测闭环) -// -// 旧口径(已删除,勿回加):按账号 `resets` 配置算「下一个天锚定重置边界」再锁到点。 -// 该边界计算 1.0.14 / 1.0.24 两次锚点算错(纯 epoch 算术不看容器 TZ), -// 造成「MiniMax 15:00 重置、网关锁到 18:00」的线上事故。新口径不猜厂商窗口: -// 冷却键带退避时间表,到期后**一个**真实请求被路由过去当探测, -// 2xx 即清键回队、失败则退避翻倍——`resets` 配置自此只存储、不参与路由。 -// =========================================================================== - -/// `ipass_probe_min` 指令的运行期值(秒)。配置阶段由 gateway 写入,worker 只读。 -static PROBE_MIN_SEC: AtomicI64 = AtomicI64::new(PROBE_MIN_DEFAULT); - -/// 供 gateway 模块壳在配置阶段回填首轮退避(非法值忽略,保持默认)。 -pub fn set_probe_min(secs: i64) { - if secs > 0 { - PROBE_MIN_SEC.store(secs, Ordering::Relaxed); - } -} - -fn probe_min() -> i64 { - PROBE_MIN_SEC.load(Ordering::Relaxed).max(1) -} - -/// 冷却键的内存表示:value = `::`。 -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct Cool { - /// 本轮退避长度(秒) - backoff: i64, - /// 到点后允许一个真实请求把它当探测路由过去 - next_probe: i64, - /// 键 TTL;到期 = 放弃未验证的持续锁定,账号无条件回队(自愈) - hard_cap: i64, -} - -/// 解析 `uac` payload。旧格式单段 `` 或损坏值 → 视作「立即可探测」, -/// 升级过渡期最多多牺牲一次探测即被新格式覆盖,不需要迁移脚本。 -fn parse_cool(payload: &str, exp: i64) -> Cool { - let mut it = payload.split(':'); - match ( - it.next().and_then(|s| s.trim().parse::().ok()), - it.next().and_then(|s| s.trim().parse::().ok()), - it.next().and_then(|s| s.trim().parse::().ok()), - ) { - (Some(b), Some(np), Some(hc)) if b > 0 => Cool { - backoff: b, - next_probe: np, - hard_cap: hc, - }, - _ => Cool { - backoff: probe_min(), - next_probe: 0, - hard_cap: exp, - }, - } -} - -/// 退避推进:翻倍封顶硬顶(纯算术,单测钉死)。 -fn next_backoff(backoff: i64) -> i64 { - backoff.saturating_mul(2).min(ACCT_COOL_HARD_CAP) -} - -/// 读某账号冷却状态;不在冷却(无键/过期/损坏到无法定 TTL)返回 None。 -fn read_cool(slot: &str, i: usize, now: i64) -> Option { - let (payload, exp) = state::get(KEY_UAC_PREFIX, format!("{slot}:{i}").as_bytes())?; - if exp <= 0 || exp <= now { - return None; - } - Some(parse_cool(&payload, exp)) -} - -/// 某账号当前是否在「额度耗尽」冷却中(含探测窗口——是否放行由 `pick_account` 细分)。 -pub fn is_cooled(slot: &str, i: usize, now: i64) -> bool { - read_cool(slot, i, now).is_some() -} - -/// 标记某账号额度耗尽,进入冷却(首轮回避 = `probe_min`,硬顶 = `ACCT_COOL_HARD_CAP`)。 -/// -/// 冷却中的账号再被标记 = **探测窗口已到期还失败**,做 re-arm(退避翻倍、关窗); -/// 未到期(best-effort 撞墙)则时间线原样——没消耗探测就不推进窗口。 -/// 判定放在这里(而非 pick)是因为 `$llm_target/$llm_host/$llm_auth/$llm_acct` -/// 会让 pick 每请求被求值多次,任何副作用都必须由只跑一次的 log 阶段承担。 -pub fn mark_exhausted(slot: &str, i: usize, now: i64) { - if let Some(c) = read_cool(slot, i, now) { - // 严格大于:与 `pick_account` 的探测判定同一口径(见那里的时间粒度说明)。 - if now > c.next_probe { - re_arm_cool(slot, i, now, c); - } - return; - } - let backoff = probe_min(); - let cool = Cool { - backoff, - next_probe: now + backoff, - hard_cap: now + ACCT_COOL_HARD_CAP, - }; - if !state::put( - KEY_UAC_PREFIX, - format!("{slot}:{i}").as_bytes(), - format!("{}:{}:{}", cool.backoff, cool.next_probe, cool.hard_cap).as_bytes(), - cool.hard_cap, - ) { - util::log_err(&format!( - "[upstreams] 账号冷却写共享内存失败 slot={slot}#{i} —— 该账号不会被自动跳过" - )); - } else { - util::log_warn(&format!( - "[upstreams] slot={slot} 账号#{i} 额度耗尽,进入冷却:{}s 后由真实请求探测(硬顶 {})", - backoff, - util::fmt_time(cool.hard_cap) - )); - } -} - -/// 探测失败后的 re-arm(log 阶段单点调用):退避翻倍、`next_probe` 推后关窗。 -/// 跨 worker 同时各探测一次最多多牺牲 1 个请求,与 `usage::bump_window` 同级近似, -/// 不为此把写锁放进热路径。 -fn re_arm_cool(slot: &str, i: usize, now: i64, c: Cool) { - let nb = next_backoff(c.backoff); - if !state::put( - KEY_UAC_PREFIX, - format!("{slot}:{i}").as_bytes(), - format!("{}:{}:{}", nb, now + nb, c.hard_cap).as_bytes(), - c.hard_cap, - ) { - util::log_err(&format!( - "[upstreams] 冷却 re-arm 写共享内存失败 slot={slot}#{i} —— 探测窗口可能重复开启" - )); - } -} - -/// 探测/转发成功(log 阶段 2xx 且无耗尽信号)→ 清除冷却,账号立即回队。 -/// 键不存在时 `remove` 返回 false、静默——健康账号的每个 2xx 都会走到这里。 -pub fn clear_cool(slot: &str, i: usize) { - if state::remove(KEY_UAC_PREFIX, format!("{slot}:{i}").as_bytes()) { - util::log_notice(&format!( - "[upstreams] slot={slot} 账号#{i} 探测恢复,冷却解除(自动回队)" - )); - } -} - -/// 冷分配(无粘性绑定时)按下流 key 选一个「可用」账号下标。 -/// -/// 用 **FNV-1a 哈希(key) % n** 做确定性分散——同时满足了两件事: -/// * **均匀铺开**:不同下游 key 哈希值不同 → 落到不同上游账号,避免所有请求都压在 -/// `account#0` 触发 MiniMax 这类厂商的「agent 并发上限」; -/// * **分配后保持**:同一 key 哈希值固定 → 永远落到同一账号(粘性绑定的前提), -/// 于是同一用户的请求始终由同一个上游账号处理,**prompt / token 缓存得以命中** -/// (否则同一会话的请求被轮到不同账号,缓存前缀对不上 → 命中率暴跌、等于白缓存)。 -/// -/// 下流 key 不存在(内部「测试」按钮 location 不带 Bearer)→ 退化为 nginx 自带伪随机 -/// `ngx_random()`,仅用于分散探测/压测流量,不要求粘性。 -fn cold_index(key: Option<&str>, n: usize) -> usize { - if n == 0 { - return 0; - } - let r = match key { - Some(k) => { - // FNV-1a 64:快、无依赖、同 key 同值(偏移 0xcbf29ce484232325 / 素数 0x100000001b3)。 - // 选它是因为确定性可复现——本机回归能用同一段哈希精确预测首次落点, - // 既验证「分散」又验证「分配后保持」而不依赖全局随机源。 - let mut h: u64 = 0xcbf2_9ce4_8423_2325; - for &b in k.as_bytes() { - h ^= b as u64; - h = h.wrapping_mul(0x100_0000_01b3); - } - h - } - None => nginx_sys::ngx_random() as u64, - }; - (r % n as u64) as usize -} - -/// 选上游账号:粘性优先 + 探测闭环(指数退避、真实请求探测)+ 全冷却 best-effort。 -/// -/// 返回 `(选中账号下标, 该账号 auth, 上游 base)`。选不到返回 `None`(上游无账号)。 -/// -/// 探测候选 = 冷却账号中**已完整经过一个退避**(`next_probe < now`,严格)者里最早到期的一个。 -/// -/// 1. **绑定账号可用 → 直接用它**:粘性与 prompt 缓存亲和高于探测,绑定流量 -/// **永不被劫持当探测**(否则长会话隔一会儿就被拉去烧一个请求、缓存前缀也白丢); -/// 2. 有探测候选 → 本请求路由过去——哪怕池里有健康账号。这是唯一的恢复通道, -/// 必须持续验证;代价最坏 = 每个退避周期牺牲 1 个无绑定真实请求(拿到的是上游 -/// 真实错误,非网关发明)。**本函数必须保持纯决策**:四个 `$llm_*` 变量会把 -/// 它求值多次,re-arm 写在 `mark_exhausted`(log 阶段单点)——放这里会三次 -/// 翻倍退避、还会让同一请求的 target/auth 落不同账号(1.0.44 开发中踩过); -/// 3. 无到期探测、有可用账号 → FNV-1a 冷分散(绑定目标已被删/冷却也走这支, -/// 与未绑定一样重新分散,不退化成永远 acct0); -/// 4. 全冷却且无到期 → best-effort 挑 `next_probe` 最近的一个转发 -/// (让上游回真实错误;**不 re-arm**——不是探测就不该推进别人的窗口)。 -/// -/// 真正选了账号且拿到下行 key 时,写 `ukb:` 粘性绑定(变化才写,避免每请求写; -/// TTL = `AFFINITY_TTL_DEFAULT`,探测成功后绑定自然稳定在恢复的账号上)。 -/// -/// ⚠️ 只读 `up:` / `upa:` / `uac:` / `ukb:`,**不回源读文件**;未命中即视为不可用。 -pub fn pick_account( - slot: &str, - anthropic: bool, - key: Option<&str>, - now: i64, -) -> Option<(usize, String, String)> { - let (base, base_anthropic, n) = read_up(slot)?; - if n == 0 { - return None; - } - - let mut avail: Vec<(usize, String)> = Vec::new(); - let mut cooled: Vec<(usize, String, Cool)> = Vec::new(); - for i in 0..n { - // resets 只存不路由(探测闭环接管恢复判定,配置错不再伤路由) - let (auth, _resets) = read_upa(slot, i)?; - match read_cool(slot, i, now) { - Some(c) => cooled.push((i, auth, c)), - None => avail.push((i, auth)), - } - } - - let bound = key.and_then(|k| { - state::get(KEY_UKB_PREFIX, k.as_bytes()) - .and_then(|(v, _)| v.trim().parse::().ok()) - }); - let bound_ok = bound - .and_then(|b| avail.iter().find(|(i, _)| *i == b)) - .map(|(i, a)| (*i, a.clone())); - - let chosen: (usize, String) = if let Some(a) = bound_ok { - a - } else if let Some((i, a, _)) = cooled - .iter() - // ⚠️ 严格小于(不是 `<=`):时间源是**秒**,若 `now == next_probe` 就放行, - // 实际等待可能只有 0.05s(标记于 45.95s → next_probe=46 → 46.05s 即"到期"), - // 短退避下等于「刚冷却完就被立刻探测」,冷却形同虚设(1.0.44 回归暴露)。 - // 严格小于保证**实际等待 ≥ 一个完整退避**(∈ (backoff, backoff+1] 秒)。 - // `mark_exhausted` 的 re-arm 判定必须用同一口径,否则会 re-arm 一个没被探测的窗口。 - .filter(|(_, _, c)| c.next_probe < now) - .min_by_key(|(_, _, c)| c.next_probe) - { - // 无绑定/绑定失效 → 本请求当探测。绑定目标恰好冷却到期时也会被选中: - // 探测成功用户直接回到原账号,是最顺路的恢复。 - (*i, a.clone()) - } else if !avail.is_empty() { - // 未绑定:按哈希分散(均匀铺开 + 粘性前提);绑定失效:同理重新分散 - let a = &avail[cold_index(key, avail.len())]; - (a.0, a.1.clone()) - } else if !cooled.is_empty() { - // 全冷却、无到期:best-effort 挑最快要探测的那个(不 re-arm) - cooled.iter().min_by_key(|(_, _, c)| c.next_probe).map(|(i, a, _)| (*i, a.clone())).unwrap() - } else { - return None; - }; - - // 写粘性绑定(仅当下行 key 存在且绑定变化) - if let Some(k) = key { - let cur = state::get(KEY_UKB_PREFIX, k.as_bytes()).map(|(v, _)| v.trim().to_owned()); - if cur.as_deref() != Some(chosen.0.to_string().as_str()) { - state::put( - KEY_UKB_PREFIX, - k.as_bytes(), - chosen.0.to_string().as_bytes(), - now + AFFINITY_TTL_DEFAULT, - ); - } - } - - Some((chosen.0, chosen.1, if anthropic { base_anthropic } else { base })) -} - -// =========================================================================== -// 账号级负载统计(shm 日桶,无数据库;与 uds:/udu: 同构) -// =========================================================================== - -/// 账号级日桶键 = `::<日窗id>` -fn acct_day_key(slot: &str, idx: usize, day: i64) -> String { - format!("{slot}:{idx}:{day}") -} - -/// 记一次转发落到 `#`(请求数 +1)。log 阶段每真实转发调用一次。 -/// -/// ⚠️ 只在这里记——`pick_account` 会被 `$llm_target/$llm_auth/$llm_acct` 三个变量 -/// 各触发一次,若在里面记会三倍计数;log 阶段经 `read_llm_acct` 取一次下标即可。 -pub fn bump_acct_load(slot: &str, idx: usize, now: i64) { - let d = usage::DayBucket { requests: 1, ..Default::default() }; - usage::bump_day(KEY_UDAY_ACCT_PREFIX, &acct_day_key(slot, idx, util::day_id(now)), &d, TREND_KEEP_SECS); -} - -/// 账号级 token 负载(来源:body filter 从上游响应抓到的 usage)。log 阶段入账。 -pub fn add_acct_tokens(slot: &str, idx: usize, tin: i64, tout: i64, now: i64) { - let d = usage::DayBucket { tin, tout, ..Default::default() }; - usage::bump_day(KEY_UDAY_ACCT_PREFIX, &acct_day_key(slot, idx, util::day_id(now)), &d, TREND_KEEP_SECS); -} - -/// 账号级额度耗尽次数 +1(仅指标,不参与路由)。 -pub fn bump_acct_exhaust(slot: &str, idx: usize, now: i64) { - let d = usage::DayBucket { requests: 1, ..Default::default() }; - usage::bump_day(KEY_UEX_PREFIX, &acct_day_key(slot, idx, util::day_id(now)), &d, TREND_KEEP_SECS); -} - -/// 读某账号今日负载:`(requests, tin, tout, exhausts)`。 -pub fn read_acct_load(slot: &str, idx: usize, now: i64) -> (i64, i64, i64, i64) { - let day = util::day_id(now); - let b = usage::day_bucket(KEY_UDAY_ACCT_PREFIX, &acct_day_key(slot, idx, day)); - let e = usage::day_bucket(KEY_UEX_PREFIX, &acct_day_key(slot, idx, day)); - (b.requests, b.tin, b.tout, e.requests) -} - -/// 最近 `days` 天的日期标签(`MM-DD`),与 [`account_trend`] 的序列**逐位对齐**。 -/// -/// 由后端算而不是前端 `Date` 推导:日窗口径(`util::day_id` 的本地自然日) -/// 只应有一处定义,前端各算一份迟早会在跨时区/跨零点时对不上。 -pub fn trend_labels(now: i64, days_req: i64) -> Vec { - let days = days_req.clamp(1, TREND_DAYS_MAX); - (0..days).rev().map(|i| util::fmt_day(now - i * 86400)).collect() -} - -/// 某 slot 各账号最近 `days` 天的负载序列(图表数据源,默认 14 天)。 -/// -/// 每个账号一条:`requests / in / out / exhausts` 四个等长数组,键精确取、 -/// 不随历史增长。前端用 `requests` 画迷你趋势,一眼看出「哪把 Key 哪天扛了多少」。 -pub fn account_trend(slot: &str, n: usize, now: i64, days_req: i64) -> Vec { - let days = days_req.clamp(1, TREND_DAYS_MAX); - let stamps: Vec = (0..days).rev().map(|i| now - i * 86400).collect(); - let ids: Vec = stamps.iter().map(|t| util::day_id(*t)).collect(); - let mut out = Vec::with_capacity(n); - for idx in 0..n { - let req: Vec = ids - .iter() - .map(|d| usage::day_bucket(KEY_UDAY_ACCT_PREFIX, &acct_day_key(slot, idx, *d)).requests) - .collect(); - let tin: Vec = ids - .iter() - .map(|d| usage::day_bucket(KEY_UDAY_ACCT_PREFIX, &acct_day_key(slot, idx, *d)).tin) - .collect(); - let tout: Vec = ids - .iter() - .map(|d| usage::day_bucket(KEY_UDAY_ACCT_PREFIX, &acct_day_key(slot, idx, *d)).tout) - .collect(); - let ex: Vec = ids - .iter() - .map(|d| usage::day_bucket(KEY_UEX_PREFIX, &acct_day_key(slot, idx, *d)).requests) - .collect(); - out.push(json!({ - "idx": idx, - "requests": req, - "in": tin, - "out": tout, - "exhausts": ex, - })); - } - out -} - -// =========================================================================== -// 查询 -// =========================================================================== - -/// 全部上游,按 slot 升序 -pub fn all() -> Vec<(String, Upstream)> { - store::read_upstreams().into_iter().collect() -} - -pub fn get(slot: &str) -> Option { - store::read_upstreams().remove(slot) -} - -/// 某账号**可用**的上游(已启用,且在白名单内;`allowed` 为空 = 不限)。 -/// -/// 账号态接口与模型发现都用它,避免"能看见但不能用"或反之的两套判定。 -pub fn available(allowed: &[String]) -> Vec<(String, Upstream)> { - all() - .into_iter() - .filter(|(slot, up)| { - up.enabled && (allowed.is_empty() || allowed.iter().any(|s| s == slot)) - }) - .collect() -} - -/// 客户端应当使用的 OpenAI 兼容 `base_url`(网关出口地址,纯 slot,不带厂商路径)。 -/// -/// ```text -/// https://lm.mayishangshu.cn:88/deepseek → 客户端 OpenAI SDK 填这个 -/// https://lm.mayishangshu.cn:88/glm -/// ``` -/// -/// **关键**:出口地址是**纯 `/`**,不拼 `/v1`、也不带上游 base 的路径段 -/// (`/v1` / `/compatible-mode/v1` / `/api/coding/paas/v4` 等)。因为: -/// -/// 1. 各厂商的版本/协议路径段千差万别(见上),网关**不能**在这里猜——它原样 -/// 存在 `base`(`base_openai`)里,转发时由 `join_target` 拼到 SDK 转发的 -/// 余下路径前。出口地址只表达「打到哪个上游」,路径交给上游基址。 -/// 2. OpenAI 兼容 SDK 不会自动在 base_url 后拼 `/v1`,它拼的是 `/chat/completions`; -/// 网关收到 `//chat/completions` 后,把 `/chat/completions` 接到 `base` -/// 后面即得 `https://api.deepseek.com/v1/chat/completions`(对 `/compatible-mode/v1` -/// 等同样适用)。历史客户端若把 base_url 写成 `…/deepseek/v1`,网关靠 -/// `join_target` 的版本段去重依然兼容,不会变 `/v1/v1/...`。 -/// -/// `port` 由调用方从请求 Host 头取(`http::host_port`),**不要写死**: -/// 部署在 88 时客户端必须带 `:88` 才连得到,放在 443 反代后面时又必须不带端口。 -pub fn base_url(slot: &str, _up: &Upstream, port: &str) -> String { - format!("https://{LLM_PUBLIC_HOST}{port}/{slot}") -} - -/// Anthropic 客户端(Claude Code / Claude SDK)应当使用的 `ANTHROPIC_BASE_URL`。 -/// -/// 网关出口地址:`/<slot>/anthropic`。Anthropic SDK **会自动在 base_url 后拼 -/// `/v1/messages`**,所以出口地址**不带 /v1**——让 SDK 自己拼,避免 -/// `/v1/v1/messages` 404。 -/// -/// 网关收到 `//anthropic/v1/messages` 后,剥掉 `/anthropic` 段,把 -/// `/v1/messages` 接到 `base_anthropic`(`https://api.deepseek.com/anthropic`)后 -/// 即得 `https://api.deepseek.com/anthropic/v1/messages`。各厂商 Anthropic 路径 -/// 不同(阿里 `/apps/anthropic`、智谱 `/api/anthropic`),同样原样存在 -/// `base_anthropic` 里,网关不猜。 -pub fn anthropic_base_url(slot: &str, _up: &Upstream, port: &str) -> String { - format!("https://{LLM_PUBLIC_HOST}{port}/{slot}/anthropic") -} - -/// 模型发现:把可用上游挂载的模型摊平成 `(模型名, slot)` 列表。 -/// -/// 没登记 `models` 的上游不产出任何条目(清单未知 ≠ 有 0 个模型), -/// 但它的转发能力不受影响——模型发现只是"告诉你有什么", -/// 不是"允许你用什么"。 -pub fn models_flat(allowed: &[String]) -> Vec<(String, String)> { - let mut out = Vec::new(); - for (slot, up) in available(allowed) { - for model in &up.models { - out.push((model.clone(), slot.clone())); - } - } - out -} - -// =========================================================================== -// 写入 -// =========================================================================== - -/// 新增或覆盖一个上游。 -/// -/// 参数语义(兼容旧调用方,同时支持多账号): -/// * `models = None` / `auth_raw = None` / `note = None` / `base_raw = None` 都表示**保持原值**; -/// `Some("")` 表示显式清空。 -/// * `accounts = Some(..)` 表示用多账号列表**整体替换**(此时顶层 `auth` 不再生效, -/// 写入时被清为 `""`,避免 `auth` 与 `accounts[0]` 重复);`None` 表示沿用旧逻辑 -/// —— 用 `auth_raw` 维护单账号(legacy `auth` 字段)。 -/// * `exhaust_status` / `exhaust_markers` 同理:`Some` 覆盖,`None` 保持原值。 -/// -/// `enabled = false` 会立刻从共享内存摘除。 -pub fn upsert( - slot_raw: &str, - base_raw: Option<&String>, - base_anthropic_raw: Option<&String>, - models: Option>, - auth_raw: Option<&String>, - accounts: Option>, - exhaust_status: Option>, - exhaust_markers: Option>, - enabled: bool, - note: Option<&String>, -) -> Result { - let slot = validate_slot(slot_raw)?; - let models = match models { - Some(list) => Some(check_models(&list)?), - None => None, - }; - - let key = slot.clone(); - let up = store::update_upstreams(move |table| { - // 先克隆出来:下面 insert 需要可变借用,不能与这里的不可变借用并存 - let existing = table.get(&key).cloned(); - - // base:None / `Some("")` = 保留原值;`Some(有效)` = 校验并覆盖。 - // 1.0.24 修:之前 base_raw 是 `&str`,管理台「停用」按钮(前端不传 base) - // 会被 `validate_base("")` 拦截。改成 Option + 保留原值后, - // 1) 新建时 base_raw 必传 → 否则报「缺少 base」; - // 2) 编辑时只传 enabled=false 也能停用(base 沿用原值)。 - let base = match base_raw { - Some(b) => { - let b = util::trim(b); - if b.is_empty() { - existing.as_ref().map(|e| e.base.clone()).unwrap_or_default() - } else { - validate_base(&b)? - } - } - None => existing.as_ref().map(|e| e.base.clone()).unwrap_or_default(), - }; - if base.is_empty() { - // 新建时(existing 为 None)必须 base 非空——上面已经保留了 existing.base, - // 这里只在「None + 无 existing」时报错(`base_raw = None` 是异常调用) - return Err("缺少 base 参数".to_owned()); - } - - // base_anthropic:OpenAI 兼容地址之外的 Anthropic 协议地址。 - // 语义与 base 一致(None / `Some("")` = 保留原值;`Some(有效)` = 校验并覆盖); - // 新建且未传时兜底派生(仅对 deepseek/minimax 这类厂商正确,阿里/智谱必须显式配)。 - let base_anthropic = match base_anthropic_raw { - Some(b) => { - let b = util::trim(b); - if b.is_empty() { - existing - .as_ref() - .map(|e| e.base_anthropic.clone()) - .unwrap_or_else(|| derive_anthropic(&base)) - } else { - validate_base(&b)? - } - } - None => existing - .as_ref() - .map(|e| e.base_anthropic.clone()) - .unwrap_or_else(|| derive_anthropic(&base)), - }; - - // 多账号:整体替换(清掉顶层 auth,避免重复);否则走 legacy 单 auth - let (accounts, legacy_auth) = match accounts { - Some(list) => { - let existing_accounts = existing - .as_ref() - .map(|e| e.effective_accounts()) - .unwrap_or_default(); - // 账号 auth 留空 = 保留该下标原账号的密钥(编辑时前端只发掩码外的空串) - let merged: Vec = list - .into_iter() - .enumerate() - .map(|(i, mut a)| { - if a.auth.trim().is_empty() { - if let Some(old) = existing_accounts.get(i) { - a.auth = old.auth.clone(); - } - } else { - // 缺 scheme 自动补 `Bearer `(兜住「只填裸 key」的历史坏数据) - a.auth = normalize_auth(&a.auth); - } - a - }) - .collect(); - (merged, String::new()) - } - None => ( - // accounts = None = 保留原 accounts 字段(与 base/models/note 语义一致)。 - // 1.0.25 修:之前写空 vec,toggle「停用/启用」会把账号清单清空。 - // 注意:existing.accounts(顶层字段)保存的是账号的 note/resets, - // auth 在 effective_accounts() 时合并 auth 与 accounts[*].auth。 - // 完整保留 = 保留顶层 accounts(保留 note/resets)即可。 - existing - .as_ref() - .map(|e| e.accounts.clone()) - .unwrap_or_default(), - match auth_raw { - Some(a) => normalize_auth(a), - None => existing.as_ref().map(|e| e.auth.clone()).unwrap_or_default(), - }, - ), - }; - - let note = match note { - Some(n) => util::trim(n).to_owned(), - None => existing.as_ref().map(|e| e.note.clone()).unwrap_or_default(), - }; - let models = match models { - Some(m) => m, - None => existing.as_ref().map(|e| e.models.clone()).unwrap_or_default(), - }; - let exhaust_status = match exhaust_status { - Some(s) => s, - None => existing - .as_ref() - .map(|e| e.exhaust_status.clone()) - .unwrap_or_default(), - }; - let exhaust_markers = match exhaust_markers { - Some(m) => m, - None => existing - .as_ref() - .map(|e| e.exhaust_markers.clone()) - .unwrap_or_default(), - }; - let created_at = existing - .as_ref() - .map(|e| e.created_at) - .filter(|&t| t > 0) - .unwrap_or_else(util::now); - - let item = Upstream { - base, - base_anthropic, - models, - enabled, - note, - created_at, - accounts, - exhaust_status, - exhaust_markers, - auth: legacy_auth, - }; - table.insert(key.clone(), item.clone()); - Ok(item) - })?; - - sync_shm(&slot, &up); - Ok(up) -} - -/// 删除上游,并立刻从共享内存摘掉 -pub fn remove(slot_raw: &str) -> Result { - let slot = validate_slot(slot_raw)?; - let key = slot.clone(); - - store::update_upstreams(move |table| { - if table.remove(&key).is_none() { - return Err(format!("上游 {key} 不存在")); - } - Ok(()) - })?; - - state::remove(KEY_UP_PREFIX, slot.as_bytes()); - Ok(slot) -} - -// =========================================================================== -// 对外投影 -// =========================================================================== - -/// 转成管理台可见的字段。 -/// -/// **绝不回显完整 auth**——管理台页面会被截图、被浏览器插件读取, -/// 回显上游真 Key 等于把公司所有厂商密钥摆在一个 HTML 里。 -/// -/// 多账号:列出每个账号的掩码 auth / 备注 / 重置周期 / 当前是否冷却, -/// 便于运维直观看到「哪把 Key 在额度耗尽冷却中」。 -pub fn view(slot: &str, up: &Upstream, port: &str) -> Value { - let (status, code, checked_at) = match health(slot) { - Some((ok, c, ts)) => (if ok { "ok" } else { "failed" }, c, ts), - None => ("unknown", 0, 0), - }; - let now = util::now(); - let accounts: Vec = up - .effective_accounts() - .into_iter() - .enumerate() - .map(|(i, a)| { - let (lr, lin, lout, lex) = read_acct_load(slot, i, now); - // 探测闭环时间线:UI 徽章 tooltip 展示「HH:MM 探测 / HH:MM 硬顶」 - let cool = read_cool(slot, i, now); - json!({ - "idx": i, - "auth_set": !a.auth.is_empty(), - "auth_mask": if a.auth.is_empty() { "-".to_owned() } else { util::mask_key(&a.auth) }, - "note": a.note, - "resets": a.resets, - "resets_text": resets_text(&a.resets), - "cooled": cool.is_some(), - "probe_next": cool.map(|c| c.next_probe), - "cool_hard_cap": cool.map(|c| c.hard_cap), - "load": { - "requests": lr, - "tokens_in": lin, - "tokens_out": lout, - "exhausts": lex, - }, - }) - }) - .collect(); - let has_auth = accounts.iter().any(|a| a["auth_set"] == Value::Bool(true)); - // 各账号最近 14 天负载序列(图表数据源):用于管理台展示「哪把 Key 哪天扛了多少」 - let acct_trend = account_trend(slot, accounts.len(), now, ACCT_TREND_DAYS); - let labels = trend_labels(now, ACCT_TREND_DAYS); - json!({ - "slot": slot, - "base": up.base, - "base_anthropic": effective_anthropic(up), - "base_url": base_url(slot, up, port), - "anthropic_base_url": anthropic_base_url(slot, up, port), - "models": up.models, - "model_count": up.models.len(), - "account_count": accounts.len(), - "auth_set": has_auth, - "enabled": up.enabled, - "note": up.note, - "exhaust_status": up.effective_exhaust_status(), - "exhaust_markers": up.exhaust_markers, - "accounts": accounts, - "account_trend": acct_trend, - "trend_labels": labels, - "created_at": up.created_at, - "created_at_text": util::fmt_time(up.created_at), - "status": status, - "status_code": if code > 0 { json!(code) } else { Value::Null }, - "checked_at": checked_at, - "checked_at_text": if checked_at > 0 { util::fmt_time(checked_at) } else { String::new() }, - }) -} - -/// 把重置周期数组渲染成人类可读文本,如 `[604800, 14400]` → "每周, 每4小时"。 -fn resets_text(resets: &[i64]) -> String { - const H1: i64 = 3600; - const H2: i64 = 2 * 3600; - const H4: i64 = 4 * 3600; - const H6: i64 = 6 * 3600; - const H12: i64 = 12 * 3600; - const D1: i64 = 86400; - const W1: i64 = 7 * 86400; - resets - .iter() - .map(|&s| match s { - H1 => "每1小时".to_owned(), - H2 => "每2小时".to_owned(), - H4 => "每4小时".to_owned(), - H6 => "每6小时".to_owned(), - H12 => "每12小时".to_owned(), - D1 => "每天".to_owned(), - W1 => "每周".to_owned(), - _ => format!("每{}秒", s), - }) - .collect::>() - .join(", ") -} - -pub fn view_all(list: &[(String, Upstream)], port: &str) -> Vec { - list.iter().map(|(slot, up)| view(slot, up, port)).collect() -} - -// =========================================================================== -// 上游健康缓存(uh:) -// =========================================================================== - -/// 记录一次上游真实响应结果。**只读端不会用它做任何决策**——转发路径 -/// 从不查这份缓存,所以误标最多影响展示,不会误杀请求。 -/// -/// 写失败必须出声(热路径无回源兜底是转发侧的事,这里出声是为了 -/// 别让「列表永远显示未探测」这种静默怪象无人解释)。 -pub fn mark_health(slot: &str, ok: bool, code: i64) { - let ts = util::now(); - let payload = format!("{}\n{}\n{}", if ok { 1 } else { 0 }, code, ts); - if !state::put( - KEY_UH_PREFIX, - slot.as_bytes(), - payload.as_bytes(), - ts + 30 * 86400, - ) { - util::log_err(&format!("[upstreams] 健康状态写共享内存失败 slot={slot}")); - } -} - -/// 读健康缓存:`Some((是否正常, 上游状态码, 记录时刻))`,没探测过 = `None`。 -pub fn health(slot: &str) -> Option<(bool, i64, i64)> { - let (payload, _) = state::get(KEY_UH_PREFIX, slot.as_bytes())?; - let mut it = payload.split('\n'); - let ok = it.next().unwrap_or("0").trim() == "1"; - let code = it - .next() - .and_then(|s| s.trim().parse::().ok()) - .unwrap_or(0); - let ts = it - .next() - .and_then(|s| s.trim().parse::().ok()) - .unwrap_or(0); - Some((ok, code, ts)) -} - -// =========================================================================== -// 单元测试(纯算术,不依赖 nginx 运行时) -// =========================================================================== - -#[cfg(test)] -mod tests { - use super::{Cool, next_backoff, parse_cool}; - use crate::config::ACCT_COOL_HARD_CAP; - - #[test] - fn parse_cool_roundtrip() { - let c = parse_cool("60:1000:1800", 1800); - assert_eq!( - c, - Cool { - backoff: 60, - next_probe: 1000, - hard_cap: 1800 - } - ); - } - - #[test] - fn parse_cool_legacy_single_value_is_probe_due_now() { - // 1.0.44 前的旧格式 ``(无冒号)→ 视作「立即可探测」、硬顶沿用键 TTL, - // 升级过渡期由下一次 pick 自动 re-arm 成新格式。 - let c = parse_cool("1789599600", 1789599600); - assert_eq!(c.next_probe, 0); - assert_eq!(c.hard_cap, 1789599600); - // 垃圾值同理:宁可多探一次,不可把账号永久锁死 - let g = parse_cool("not-a-number", 99); - assert_eq!(g.next_probe, 0); - assert_eq!(g.hard_cap, 99); - } - - #[test] - fn backoff_doubles_capped_at_hard_cap() { - assert_eq!(next_backoff(60), 120); - assert_eq!(next_backoff(120), 240); - assert_eq!(next_backoff(ACCT_COOL_HARD_CAP / 2), ACCT_COOL_HARD_CAP); - // 饱和乘:病态大值不许回绕成小退避 - assert_eq!(next_backoff(i64::MAX), ACCT_COOL_HARD_CAP); - } -} diff --git a/rust/crates/plugin-llm/src/usage_filter.rs b/rust/crates/plugin-llm/src/usage_filter.rs deleted file mode 100644 index 2c28e65..0000000 --- a/rust/crates/plugin-llm/src/usage_filter.rs +++ /dev/null @@ -1,358 +0,0 @@ -//! LLM token 计量:注册 body filter,旁路扫描上游响应,提取尾部 `usage` 的 token 数。 -//! -//! 为什么调研结论是「要做 C 模块」、最后只用 Rust 就成了:`ngx_http_top_body_filter` -//! 是 nginx **导出的全局符号**(`src/http/ngx_http.c:76` 定义,`ngx_http.h:197` -//! extern),动态模块可以直接 FFI 引用并把自己插到链头——不需要第二个 .so, -//! 「单容器单动态库」红线无伤。做法与官方 C 模块(copy/postpone filter)同款: -//! `next = top; top = ours;`,next 自持一份。 -//! -//! ## 工作方式 -//! -//! * **只旁观、不改动**:filter 拿到 chunk 后原样交给下一环(SSE 透传零影响), -//! 同时把内存 buf 的字节追加进 32 KiB **尾部窗口**(请求池分配,随请求回收)。 -//! 必须在调下一环**之前**读——下游 filter(write)发送后会前移 `pos`。 -//! * **流量识别**:`loc::enabled(r, Switch::Llm)` 为真才建窗口—— -//! 管理台 / 子域名转发不经过扫描,零开销。(插件化改造点:原来直接读 -//! gateway 私有的 `ModuleConfig.llm`,现改走内核 loc 契约。) -//! * **token 提取**(不做 JSON 解析,按键扫数字,容错优先): -//! - 入 token:`"prompt_tokens"`(OpenAI) / `"input_tokens"`(Anthropic) / -//! `"promptTokenCount"`(Gemini)——**路过就记**(Anthropic 的 message_start -//! 在流头,长流会滑出尾部窗口,不能等收尾再找); -//! - 出 token:`"completion_tokens"` / `"output_tokens"` / -//! `"candidatesTokenCount"`——流结束时取尾部窗口**最后一次**出现 -//! (Anthropic message_delta 是累计值,最后一次 = 终值); -//! - 兜底:入出都为 0 时试 Gemini `"totalTokenCount"`。 -//! 搜索键都带前导引号,天然避开 `cache_read_input_tokens` 这类子串陷阱。 -//! * **记账**:窗口值挂在模块 ctx 上,log 阶段(AuditHandler)统一 -//! `stats::add_tokens`(键 `utt:` / `utms:`)并写进审计 meta 行—— -//! 身份解析只在 log 阶段做一次,body filter 全程无身份语义。 -//! -//! ## 口径 -//! -//! OpenAI 系**流式**需要客户端带 `stream_options: {"include_usage": true}` -//! 才有 usage 块(Anthropic / Gemini 默认就带);非流式 JSON 天然带 usage。 -//! 抓不到 = 记 0,绝不猜测、绝不补估。 - -use core::slice; - -use ngx::core::Status; -use ngx::ffi::{ngx_chain_t, ngx_http_request_t, ngx_int_t}; - -use ipass_core::loc::{self, Switch}; -use ipass_core::plugin; - -use crate::llm; -use crate::upstreams; - -/// 尾部窗口容量:SSE 最终 usage 块 / 非 stream JSON 的尾部都远小于此。 -const TAIL_CAP: usize = 32 * 1024; - -type BodyFilterPt = - Option ngx_int_t>; - -extern "C" { - /// nginx 导出的 body filter 链头(动态模块引用主程序符号,dlopen 时解析) - static mut ngx_http_top_body_filter: BodyFilterPt; -} - -/// 插入前的原链头——与官方 C 模块同款,每个模块自持一份 next。 -static mut NEXT_BODY_FILTER: BodyFilterPt = None; - -/// postconfiguration 阶段调用:把自己插到 body filter 链头。 -pub fn install() { - unsafe { - NEXT_BODY_FILTER = ngx_http_top_body_filter; - ngx_http_top_body_filter = Some(body_filter); - } -} - -// =========================================================================== -// usage 键与数字扫描 -// =========================================================================== - -/// 入 token 候选键(带前导引号防子串误配) -const KEYS_IN: [&[u8]; 3] = [ - b"\"prompt_tokens\":", - b"\"input_tokens\":", - b"\"promptTokenCount\":", -]; -/// 出 token 候选键 -const KEYS_OUT: [&[u8]; 3] = [ - b"\"completion_tokens\":", - b"\"output_tokens\":", - b"\"candidatesTokenCount\":", -]; - -fn find_from(hay: &[u8], needle: &[u8], from: usize) -> Option { - if needle.is_empty() || hay.len() < needle.len() { - return None; - } - let end = hay.len() - needle.len(); - let mut i = from; - while i <= end { - if &hay[i..i + needle.len()] == needle { - return Some(i); - } - i += 1; - } - None -} - -/// 从键名结束处解析 `: ` -fn value_at(hay: &[u8], key_end: usize) -> Option { - let mut i = key_end; - while i < hay.len() && (hay[i] == b' ' || hay[i] == b'\t') { - i += 1; - } - let start = i; - while i < hay.len() && hay[i].is_ascii_digit() { - i += 1; - } - if i == start { - return None; - } - core::str::from_utf8(&hay[start..i]).ok()?.parse::().ok() -} - -/// 一段字节里,各键**第一次**出现的非零值 -fn first_u64(hay: &[u8], keys: &[&[u8]]) -> u64 { - for k in keys { - if let Some(p) = find_from(hay, k, 0) { - if let Some(v) = value_at(hay, p + k.len()) { - if v > 0 { - return v; - } - } - } - } - 0 -} - -/// 一段字节里,各键**最后一次**出现的非零值(Anthropic message_delta 累计语义) -fn last_u64(hay: &[u8], keys: &[&[u8]]) -> Option { - let mut out = None; - for k in keys { - let mut from = 0; - while let Some(p) = find_from(hay, k, from) { - if let Some(v) = value_at(hay, p + k.len()) { - if v > 0 { - out = Some(v); - } - } - from = p + 1; - } - } - out -} - -// =========================================================================== -// per-request 扫描窗口(挂在模块 ctx 上,请求池分配、随请求回收) -// =========================================================================== - -/// 内联缓存「额度耗尽」探测配置的上限(避免每 chunk 查 shm,且不引入堆分配/泄漏)。 -/// marker 最长 128 字节、最多 4 个,超出部分截断/忽略(v1 够用,详见 `take_exhausted`)。 -const MAX_MARKERS: usize = 4; -const MARKER_CAP: usize = 128; - -#[repr(C)] -struct ScanCtx { - tail: [u8; TAIL_CAP], - len: usize, - tin: u64, - tout: u64, - /// 响应体命中「额度耗尽」标记(流式也能扫到,log 阶段据此标记账号冷却) - exhausted: bool, - /// 内联缓存的耗尽探测配置(首次扫描时从 `upe:` 读入,随请求池整体回收) - status: [i64; 8], - status_n: usize, - markers: [[u8; MARKER_CAP]; MAX_MARKERS], - marker_len: [usize; MAX_MARKERS], - marker_n: usize, -} - -impl ScanCtx { - fn append(&mut self, data: &[u8]) { - // 入 token 路过就记(Anthropic message_start 在流头,收尾时早滑出窗口) - if self.tin == 0 { - self.tin = first_u64(data, &KEYS_IN); - } - // 维护「最后 TAIL_CAP 字节」窗口 - let n = data.len(); - if n >= self.tail.len() { - let off = n - self.tail.len(); - self.tail.copy_from_slice(&data[off..]); - self.len = self.tail.len(); - } else if self.len + n > self.tail.len() { - let drop = self.len + n - self.tail.len(); - self.tail.copy_within(drop..self.len, 0); - self.len -= drop; - self.tail[self.len..self.len + n].copy_from_slice(data); - self.len += n; - } else { - self.tail[self.len..self.len + n].copy_from_slice(data); - self.len += n; - } - } - - /// last_buf:从尾部窗口取出/收拢 token 数 - fn finish(&mut self) { - let w = &self.tail[..self.len]; - if let Some(v) = last_u64(w, &KEYS_OUT) { - self.tout = v; - } - if self.tin == 0 { - self.tin = last_u64(w, &KEYS_IN).unwrap_or(0); - } - if self.tin == 0 && self.tout == 0 { - // 兜底:Gemini totalTokenCount - if let Some(v) = last_u64(w, &[b"\"totalTokenCount\":" as &[u8]]) { - self.tout = v; - } - } - } -} - -/// ctx_index 由 gateway 在 postconfiguration 前注入(见 `ipass_core::plugin`)。 -/// 未注入(usize::MAX)说明装配异常,返回空指针让调用方放弃计量—— -/// **绝不**拿 usize::MAX 去做 `r->ctx.add()`,那是越界写。 -unsafe fn ctx_slot(r: *mut ngx_http_request_t) -> *mut *mut ScanCtx { - let idx = plugin::ctx_index(); - if idx == usize::MAX { - return core::ptr::null_mut(); - } - // SAFETY: idx 是 gateway 模块的 ctx_index,r->ctx 数组必然覆盖该下标 - unsafe { (*r).ctx.add(idx) as *mut *mut ScanCtx } -} - -// =========================================================================== -// filter 本体 -// =========================================================================== - -/// body filter:先旁观扫描,再原样交下一环。任何异常都只影响计量,不影响转发。 -unsafe extern "C" fn body_filter(r: *mut ngx_http_request_t, in_: *mut ngx_chain_t) -> ngx_int_t { - if !r.is_null() && !in_.is_null() && (*r).main == r { - scan(r, in_); - } - // 无条件放行给下一环——本 filter 对数据链零改动 - match NEXT_BODY_FILTER { - Some(f) => f(r, in_), - None => Status::NGX_OK.into(), - } -} - -unsafe fn scan(r: *mut ngx_http_request_t, in_: *mut ngx_chain_t) { - // 只有 LLM location 才扫:管理台 / 子域名转发零开销直过 - if !loc::enabled(r, Switch::Llm) { - return; - } - - let slot = ctx_slot(r); - if slot.is_null() { - return; - } - let mut ctx = *slot; - if ctx.is_null() { - let p = nginx_sys::ngx_pcalloc((*r).pool, core::mem::size_of::()) as *mut ScanCtx; - if p.is_null() { - // 分配失败:放弃本请求的 token 计量,不影响转发 - return; - } - *slot = p; - ctx = p; - // 首次扫描:从 `upe:` 读入耗尽探测配置(状态码 + 响应体标记) - let c = &mut *ctx; - if let Some(s) = llm::slot_of(r) { - let (status, markers) = upstreams::read_upe(&s); - let n = status.len().min(c.status.len()); - c.status[..n].copy_from_slice(&status[..n]); - c.status_n = n; - let m = markers.len().min(MAX_MARKERS); - for i in 0..m { - let bytes = markers[i].as_bytes(); - let cap = bytes.len().min(MARKER_CAP); - if cap > 0 { - c.markers[i][..cap].copy_from_slice(&bytes[..cap]); - c.marker_len[i] = cap; - } - } - c.marker_n = m; - } - } - - // ctx 此刻一定非 null 且指向有效内存;用 &mut 引用操作,避免裸指针隐式自动取引用报错 - let c = &mut *ctx; - let mut cl = in_; - while !cl.is_null() { - let buf = (*cl).buf; - cl = (*cl).next; - if buf.is_null() { - continue; - } - let (pos, last) = ((*buf).pos, (*buf).last); - if !pos.is_null() && !last.is_null() && last > pos { - // SAFETY: 内存 buf 的 pos..last 由 nginx 保证有效 - let data = slice::from_raw_parts(pos, last as usize - pos as usize); - c.append(data); - // 扫「额度耗尽」标记(逐字节子串匹配,流式响应也能命中) - if !c.exhausted && c.marker_n > 0 { - for i in 0..c.marker_n { - let ml = c.marker_len[i]; - if ml == 0 { - continue; - } - let needle = &c.markers[i][..ml]; - if data.len() >= needle.len() - && data.windows(needle.len()).any(|w| w == needle) - { - c.exhausted = true; - break; - } - } - } - } - if (*buf).last_buf() != 0 { - c.finish(); - } - } -} - -/// log 阶段读取本请求抓到的 token 数(ctx 随请求池整体回收,无需清理钩子)。 -pub fn take(r: *mut ngx_http_request_t) -> (u64, u64) { - if r.is_null() { - return (0, 0); - } - unsafe { - let slot = ctx_slot(r); - if slot.is_null() { - return (0, 0); - } - let c = *slot; - if c.is_null() { - (0, 0) - } else { - ((*c).tin, (*c).tout) - } - } -} - -/// log 阶段读取本请求响应体是否命中「额度耗尽」标记。 -/// -/// 与 [`take`] 同生命周期:ctx 随请求池回收,无需清理钩子。 -/// 仅 LLM location 的 body filter 会置位;其它请求恒为 false(靠状态码判定兜底)。 -pub fn take_exhausted(r: *mut ngx_http_request_t) -> bool { - if r.is_null() { - return false; - } - unsafe { - let slot = ctx_slot(r); - if slot.is_null() { - return false; - } - let c = *slot; - if c.is_null() { - false - } else { - (*c).exhausted - } - } -} diff --git a/rust/gateway/Cargo.toml b/rust/gateway/Cargo.toml deleted file mode 100644 index 3d27284..0000000 --- a/rust/gateway/Cargo.toml +++ /dev/null @@ -1,31 +0,0 @@ -[package] -name = "ipass-gateway" -version = "2.0.0" -edition.workspace = true -publish = false -description = "尚数网 统一网关(iPass 山海关)—— nginx + Rust 扩展模块实现(模块壳 + 插件装配)" - -[lib] -name = "ipass_gateway" -# cdylib 给 nginx 加载;rlib 便于单元测试 -crate-type = ["cdylib", "rlib"] - -[features] -default = ["export-modules"] -# 脱离 nginx 构建系统、单独产出动态模块时必须开启(生成 ngx_modules 表) -export-modules = [] - -[dependencies] -ngx.workspace = true -nginx-sys.workspace = true -ipass-core.workspace = true -plugin-domain.workspace = true -plugin-llm.workspace = true -plugin-apix.workspace = true -plugin-cache.workspace = true - -serde.workspace = true -serde_json.workspace = true -sha2.workspace = true -getrandom.workspace = true -libc.workspace = true diff --git a/rust/gateway/build.rs b/rust/gateway/build.rs deleted file mode 100644 index bc4930b..0000000 --- a/rust/gateway/build.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! 构建脚本:让 cdylib 允许「未定义符号」。 -//! -//! nginx 动态模块的运行时符号(`ngx_palloc` / `ngx_http_add_variable` / -//! `ngx_shared_memory_add` …)由 nginx 主程序在 `load_module` 时提供, -//! 链接本模块时它们必然是未定义的——这是动态模块的固有属性,不是错误。 -//! -//! * Linux:cdylib 默认允许未定义符号,无需处理。 -//! * macOS:必须显式 `-undefined dynamic_lookup`,否则 ld 直接报 -//! `symbol(s) not found for architecture x86_64`。 -//! -//! 注意:vendored 的 `ngx` crate 的 build.rs 也设了这两项,但 -//! `rustc-link-arg` 只对「设置它的那个 crate」生效,不会传递给依赖它的 -//! crate,所以本 crate 必须自己再设一次。 - -fn main() { - if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("macos") { - println!("cargo::rustc-link-arg=-undefined"); - println!("cargo::rustc-link-arg=dynamic_lookup"); - } -} diff --git a/rust/gateway/src/api.rs b/rust/gateway/src/api.rs deleted file mode 100644 index 690e62a..0000000 --- a/rust/gateway/src/api.rs +++ /dev/null @@ -1,86 +0,0 @@ -//! 插件路由表(最终形态):仅剩用量趋势组合端点。 -//! -//! 身份类端点在 `ipass-core::api`(CorePlugin),域名类在 -//! `plugin-domain::api`,LLM 上游 / 用量端点在 `plugin-llm::api`, -//! API 代理端点在 `plugin-apix::api`。本表只剩 -//! -//! * **`/api/admin/trend`** —— **刻意留在 gateway**(对方案的有意偏离): -//! 日桶键带日窗 id、按前缀扫不出来,趋势接口需要 users + upstreams + -//! apix 三方名册,这是跨插件组合逻辑,属于装配根职责。 -//! -//! 鉴权语义与内核共用一套:`ipass_core::api::{Guard, Actor, run_guarded}`。 - -use ngx::core::Status; -use ngx::ffi::ngx_http_request_t; - -use ipass_core::api::{Actor, Guard, run_guarded}; -use ipass_core::{http, util}; - -use crate::users; - -// =========================================================================== -// 路由 -// =========================================================================== - -/// 路由表。刻意用精确匹配而非正则——接口路径是固定的, -/// 正则只会引入「写错锚点导致前缀误匹配」的风险。 -fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> { - let entry: (Guard, Handler) = match (method, uri) { - ("GET", "/api/admin/trend") => (Guard::Admin, h_admin_trend), - _ => return None, - }; - Some(entry) -} - -type Handler = fn(*mut ngx_http_request_t, Actor) -> Status; - -/// 本表分发:认领则返回响应状态,不认领返回 `None`(交给下一个插件)。 -pub fn dispatch(r: *mut ngx_http_request_t) -> Option { - let method = http::method_str(r); - let uri = http::uri(r); - - let Some((guard, handler)) = route(&method, &uri) else { - return None; - }; - - Some(run_guarded(r, guard, handler)) -} - -// =========================================================================== -// 用量趋势(组合根:需要三方名册) -// =========================================================================== - -/// 管理员:LLM 用量趋势(图表数据源)。 -/// -/// `days` 缺省 14 天,上限由 `plugin_llm::config::TREND_DAYS_MAX` 兜住。 -/// 按账号 / 按上游两个维度需要**先知道有哪些名字**(键里带日窗 id,扫不出来), -/// 所以这里把 users / upstreams / apix 的名册传进去,由 plugin_llm::stats -/// 逐个精确取键。 -fn h_admin_trend(r: *mut ngx_http_request_t, _actor: Actor) -> Status { - let p = http::params(r); - let days = p - .get("days") - .and_then(|v| util::trim(v).parse::().ok()) - .unwrap_or_else(plugin_llm::stats::trend_default_days); - - let usernames: Vec = users::all().into_iter().map(|u| u.username).collect(); - // API 代理与 LLM 上游共用同一片用量键空间,只是 slot 带 `api/` 前缀 - // (见 `plugin_apix::config::APIX_STATS_PREFIX`)——这里一起列出,趋势图才会出现它们 - let slots: Vec = plugin_llm::upstreams::all() - .into_iter() - .map(|(slot, _)| slot) - .chain( - plugin_apix::apix::all() - .into_iter() - .map(|(slug, _)| { - format!("{}{}", plugin_apix::config::APIX_STATS_PREFIX, slug) - }), - ) - .collect(); - - http::ok( - r, - plugin_llm::stats::trend(util::now(), days, &usernames, &slots), - "ok", - ) -} diff --git a/rust/gateway/src/lib.rs b/rust/gateway/src/lib.rs deleted file mode 100644 index 67c3c9c..0000000 --- a/rust/gateway/src/lib.rs +++ /dev/null @@ -1,784 +0,0 @@ -//! iPass 山海关(ipass-gateway)—— nginx + Rust 动态模块 -//! -//! 尚数网 统一网关(动态域名 / LLM API 路由及未来各类统一出口能力)。 -//! -//! 插件化定型后(2026-09-13),本 crate 仅作"装配壳":模块入口、7 条 -//! `ipass_*` 指令、`ModuleConfig` 开关、`ApiHandler` content 阶段两段式读体 -//! 后按 PLUGINS 顺序分发、trend 组合端点、state_zone_init 三表装载、 -//! `init_process` 播种 admin + 审计目录启动巡检。具体业务实现都在 -//! `crates/ipass-core` 与三个 `plugin-*` crate 里。 -//! -//! ## 三条热路径 -//! -//! | 入口 | 阶段 | 开关 | 做什么 | -//! | --- | --- | --- | --- | -//! | `*.yourdomain.com/*` | access | `ipass_proxy` | 查 `sub:` 决定后端 | -//! | `llm.yourdomain.com//*` | access | `ipass_llm` | 查 `ukey:` 鉴权 + `up:` 定上游 | -//! | `llm.yourdomain.com/apix/` | access | `ipass_apix` | 同上鉴权,查 `ax:` 定供应商资产 | -//! | `proxy-api.yourdomain.com/api/*` | content | `ipass_api` | 管理接口,读体后分发 | -//! -//! ## 关键实现约束(均为实测/查证确认,详见 CLAUDE.md) -//! -//! * `ngx_http_add_variable` 必须在 **preconfiguration** 阶段调用, -//! 否则配置里的 `proxy_pass http://$proxy_backend_addr` 无法解析。 -//! * 阶段处理器在 postconfiguration 阶段注册,且**必须按 location 开关** -//! (`ipass_proxy` / `ipass_api` / `ipass_llm`),否则管理台与 API -//! 也会被转发逻辑 404 掉。 -//! * 共享内存只有 `RwLock, _>` 一种安全形态; -//! `SlabPool` 只能在 zone 初始化回调里取出,锁本身也必须用 -//! `ngx::allocator::allocate` 放进共享内存,否则各 worker 拿到的是各自的副本。 -//! * ngx crate **没有**暴露请求体读取,必须走 -//! `ngx_http_read_client_request_body` 的两段式异步接口(见 `ApiHandler`)。 -//! **这也是 LLM 代理不按 body 里的 `model` 字段路由的原因**——access 阶段 -//! 根本拿不到请求体。 - -mod api; - -// 内核模块桥接:业务文件里的 `crate::config::X` / `crate::state::…` 等路径 -// 不用改写,直接经此解析到 ipass-core(插件化 P0 的行为等价手段, -// 各业务模块随 P2–P4 迁入插件 crate 后,这里的 re-export 会逐个撤掉)。 -pub use ipass_core::{config, http, models, session, state, store, users, util}; - -use core::ffi::{c_char, c_void}; -use core::ptr; - -use ngx::core::{NGX_CONF_ERROR, NGX_CONF_OK, NgxStr, NgxString, SlabPool, Status}; -use serde_json::{Value, json}; -use ngx::ffi::{ - NGX_CONF_TAKE1, NGX_CONF_TAKE2, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MAIN_CONF, - NGX_HTTP_MAIN_CONF_OFFSET, NGX_HTTP_MODULE, - NGX_LOG_EMERG, ngx_command_t, ngx_conf_t, ngx_cycle_t, - ngx_http_module_t, - ngx_http_request_t, ngx_int_t, ngx_module_t, ngx_parse_size, - ngx_shared_memory_add, ngx_shm_zone_t, ngx_str_t, ngx_uint_t, -}; -use ngx::http::{ - HTTPStatus, HttpModule, HttpModuleLocationConf, HttpPhase, HttpRequestHandler, Merge, - MergeConfigError, Request, -}; -use ngx::{ngx_conf_log_error, ngx_string}; - -use ipass_core::loc::{self, Switch}; -use ipass_core::plugin::Plugin; - -// =========================================================================== -// 插件装配(PLUGINS 顺序 = 认领顺序;身份类端点将来必须排最前) -// =========================================================================== - -/// 装配根:trend 组合端点由 gateway 自己认领(见 `api::h_admin_trend`)。 -struct GatewayRoutes; - -impl Plugin for GatewayRoutes { - fn name(&self) -> &'static str { - "gateway-routes" - } - - fn dispatch_api(&self, r: *mut ngx_http_request_t) -> Option { - api::dispatch(r) - } -} - -/// 插件注册表。**身份类端点(CorePlugin)排在最前**:dispatch 按顺序认领, -/// 先注册者先得。装配顺序:内核(身份)→ 域名 → LLM → API 代理 → trend。 -static PLUGINS: &[&dyn Plugin] = &[ - &ipass_core::api::CorePlugin, - &plugin_domain::DomainPlugin, - &plugin_llm::LlmPlugin, - &plugin_apix::ApixPlugin, - &plugin_cache::CachePlugin, - &GatewayRoutes, -]; - -/// 装配根的插件扩展:把身份端点需要的插件侧数据回填给内核 -/// (内核不认识任何插件,见 `ipass_core::api::CoreExt`)。 -struct GatewayExt; - -impl ipass_core::api::CoreExt for GatewayExt { - /// 管理台账号列表:域名数 + LLM 用量摘要(一次扫表,避免逐账号查共享内存) - fn decorate_admin_users(&self, views: &mut [Value]) { - let mut counts: std::collections::HashMap = std::collections::HashMap::new(); - for d in plugin_domain::domains::all() { - *counts.entry(d.owner).or_insert(0) += 1; - } - - let now = util::now(); - let usage: std::collections::HashMap = - plugin_llm::stats::all_users(now) - .into_iter() - .map(|u| (u.username.clone(), u)) - .collect(); - - for v in views { - let Some(username) = v.get("username").and_then(Value::as_str).map(str::to_owned) - else { - continue; - }; - if let Value::Object(ref mut m) = v { - let mut usage_json = json!({ "today": 0, "total": 0, "last_ts": 0, "last_ts_text": "" }); - if let Some(uu) = usage.get(&username) { - usage_json = uu.usage.json(); - } - m.insert( - "domain_count".to_owned(), - json!(counts.get(&username).copied().unwrap_or(0)), - ); - // 今日 LLM 用量:与热路径同一数据源(共享内存)——注意口径是 - // **已转发**的请求,不含被限流/越权拒掉的那些 - m.insert( - "llm_today".to_owned(), - json!(usage.get(&username).map(|x| x.usage.today).unwrap_or(0)), - ); - m.insert("llm_usage".to_owned(), usage_json); - } - } - } - - /// `/api/v1/me`:我的域名 + 用量摘要 + 策略 TTL + **能力目录**(upstreams + apix)。 - /// - /// 能力目录让同事拿一把 key 一个 me 拿全,不再需要分别调 - /// `/api/v1/upstreams` / `/api/v1/apix` / `/api/v1/domains`。 - /// 老 3 个端点保留,客户端脚本不破坏。 - /// - /// LLM 上游按 `allowed_models` 白名单过滤(复用 `plugin_llm::upstreams::available`), - /// apix 只列 `enabled = true` 的代理(`plugin_apix::apix::enabled`), - /// 否则列表里出现「调就 404」让人误判网关坏掉。 - /// `port` 从请求 Host 头取,**不要写死**——见 `plugin_apix::call_url` 注释。 - fn decorate_me(&self, user: &ipass_core::models::User, port: &str, view: &mut Value) { - let Value::Object(ref mut m) = view else { - return; - }; - m.insert( - "domains".to_owned(), - json!(plugin_domain::domains::view_all( - &plugin_domain::domains::by_owner(&user.username), - )), - ); - // 用量摘要(明细走 /api/v1/usage)——让客户端一个请求就能决定要不要自我节流 - m.insert( - "llm_usage".to_owned(), - plugin_llm::stats::user(&user.username, util::now()).json(), - ); - m.insert( - "policy".to_owned(), - json!({ - "temp_grant_ttl_hours": config::TEMP_GRANT_TTL / 3600, - "temp_domain_ttl_hours": plugin_domain::config::TEMP_DOMAIN_TTL / 3600, - }), - ); - // 能力目录:upstreams(按白名单过滤)+ apix(仅 enabled)。 - // 这两个 view_all 都返回完整结构(slot/base_url/models 或 slug/url/params/...), - // 同事拿到后能直接配客户端,不再需要二次问「这个 slot 怎么调」。 - m.insert( - "upstreams".to_owned(), - json!(plugin_llm::upstreams::view_all( - &plugin_llm::upstreams::available(&user.allowed_models), - port, - )), - ); - m.insert( - "apix".to_owned(), - json!(plugin_apix::apix::view_all( - &plugin_apix::apix::enabled(), - port, - )), - ); - // **1.0.29 同事反馈**:一个 /me 拿全"我最近调用了多少、消耗多少 token、 - // 主要用了哪些 slot",不再需要二次调 /usage /upstreams。 - // 数据从 audit meta.jsonl 实时聚合(log 阶段落盘即聚合,零延迟), - // 不依赖 stats 落盘周期;只统计真正转发给上游的请求(status 2xx/4xx/5xx 都算)。 - // `recent` 顶层块含 24h/7d 调用数 + token 数 + 24h 小时分布 + 按 slot 拆分。 - m.insert( - "recent".to_owned(), - plugin_llm::llm_audit::user_recent(&user.username, 7), - ); - } - - /// 删除账号:连带回收该账号名下的域名,避免出现无主映射 - fn reclaim_on_delete(&self, username: &str) -> usize { - plugin_domain::domains::remove_by_owner(username) - } - - /// 账号「可用上游」白名单校验:slot 必须真实存在,否则白名单里会存进 - /// 永远匹配不上的死名字,表现为「账号被授权了却一直 403」 - fn validate_slots(&self, slots: &[String]) -> Result<(), String> { - for slot in slots { - if plugin_llm::upstreams::get(slot).is_none() { - return Err(format!("上游 {slot} 不存在")); - } - } - Ok(()) - } -} - -/// gateway 注入给 `loc::enabled` 的查询实现:把 Switch 翻译到 ModuleConfig。 -/// 未实现的变体(Iap / Domain)**保守返回 false**,绝不误开。 -fn loc_switch(request: &mut Request, s: Switch) -> bool { - let conf = Module::location_conf(request); - match s { - Switch::Api => conf.map(|c| c.api).unwrap_or(false), - Switch::Proxy => conf.map(|c| c.proxy).unwrap_or(false), - Switch::Llm => conf.map(|c| c.llm).unwrap_or(false), - Switch::Apix => conf.map(|c| c.apix).unwrap_or(false), - Switch::Cache => conf.map(|c| c.cache).unwrap_or(false), - Switch::Iap | Switch::Domain => false, - } -} - -/// 管理 API 分发:按 PLUGINS 顺序问每个插件,都不认领才统一 404。 -fn dispatch_api(r: *mut ngx_http_request_t) -> Status { - for p in PLUGINS { - if let Some(s) = p.dispatch_api(r) { - return s; - } - } - let method = http::method_str(r); - let uri = http::uri(r); - http::fail( - r, - HTTPStatus::NOT_FOUND, - &format!("接口不存在:{method} {uri}"), - ) -} -/// 管理 API:content 阶段先读请求体,读完后交给 `api::dispatch`。 -/// -/// 这是本项目唯一需要异步的地方。`ngx_http_read_client_request_body` 是 -/// **两段式**接口:请求体已在缓冲区时会**同步**调用 post_handler, -/// 否则挂起等 socket 事件后回调。两条路径都必须正确,所以不能假设 -/// 「回调一定晚于本次返回」。 -struct ApiHandler; - -impl HttpRequestHandler for ApiHandler { - const PHASE: HttpPhase = HttpPhase::Content; - type Output = Status; - - fn handler(request: &mut ngx::http::Request) -> Self::Output { - if !Module::location_conf(request).map(|c| c.api).unwrap_or(false) { - return Status::NGX_DECLINED; - } - - let r: *mut ngx_http_request_t = request.as_mut() as *mut ngx_http_request_t; - - // SAFETY: r 有效;回调只会在请求内存池存活期间被调用 - let rc = unsafe { nginx_sys::ngx_http_read_client_request_body(r, Some(api_body_ready)) }; - - // 读体失败(请求体超限等):把原始状态码交回 nginx,由它生成对应错误页 - if rc >= nginx_sys::NGX_HTTP_SPECIAL_RESPONSE as ngx_int_t { - return Status(rc); - } - - // 响应由回调发出,本阶段到此挂起 - Status::NGX_DONE - } -} - -/// 请求体就绪回调:执行路由分发并终结请求。 -unsafe extern "C" fn api_body_ready(r: *mut ngx_http_request_t) { - let status = dispatch_api(r); - // SAFETY: r 由 nginx 传入且仍然有效;NGX_OK 表示响应已完整发出 - unsafe { nginx_sys::ngx_http_finalize_request(r, status.0) }; -} - -// =========================================================================== -// 模块定义 -// =========================================================================== - -struct Module; - -/// 按 location 生效的开关。 -/// -/// 必须存在:阶段处理器注册在 http 主配置上,若不按 location 收敛, -/// 管理台与 API 也会走转发逻辑并被 404。 -#[derive(Debug, Default)] -struct ModuleConfig { - /// 该 location 走「子域名 → 后端」转发 - proxy: bool, - /// 该 location 暴露管理 API(/api/admin/*、/api/v1/*) - api: bool, - /// 该 location 走 LLM 代理(`//...` → 上游) - llm: bool, - /// 该 location 走 API 代理(`/apix/` → 外部供应商) - apix: bool, - /// 该 location 走统一制品缓存(`/go/` `/cargo/` … → 各生态 registry) - cache: bool, -} - -unsafe impl HttpModuleLocationConf for Module { - type LocationConf = ModuleConfig; -} - -impl Merge for ModuleConfig { - fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> { - if prev.proxy { - self.proxy = true; - } - if prev.api { - self.api = true; - } - if prev.llm { - self.llm = true; - } - if prev.apix { - self.apix = true; - } - if prev.cache { - self.cache = true; - } - Ok(()) - } -} - -impl HttpModule for Module { - fn module() -> &'static ngx_module_t { - // SAFETY: 模块定义是静态的,生命周期贯穿进程 - unsafe { &*ptr::addr_of!(ngx_http_ipass_module) } - } - - /// 必须在配置解析前注册变量,否则 `proxy_pass http://$proxy_backend_addr` 无法解析 - unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - // 契约层:注入 location 开关查询实现(早于任何请求与 phase handler) - loc::set_switch_fn(loc_switch); - // 装配根注册插件扩展:身份端点所需的插件侧数据经它回填内核 - ipass_core::api::set_ext(&GatewayExt); - - // 各插件注册自己的 nginx 变量($proxy_backend_addr 归 plugin-domain、 - // LLM 三件套归 plugin-llm、API 代理六件套归 plugin-apix) - for p in PLUGINS { - if !p.preconfig(cf) { - ngx_conf_log_error!( - NGX_LOG_EMERG, - unsafe { &mut *cf }, - "插件 {} 变量注册失败", - p.name() - ); - return Status::NGX_ERROR.into(); - } - } - - Status::NGX_OK.into() - } - - unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cf = unsafe { &mut *cf }; - - // 把本模块 ctx_index 注入内核契约:插件在 r->ctx 上挂请求级状态 - // (如 plugin-llm 的 usage_filter 扫描窗口)用它定位自己的槽位 - ipass_core::plugin::set_ctx_index(Module::module().ctx_index as usize); - - // 各插件注册自己的 phase handler(ResolveHandler 归 plugin-domain、 - // LlmHandler/LlmHealthHandler/AuditHandler 归 plugin-llm、 - // ApiProxyHandler 归 plugin-apix) - for p in PLUGINS { - if !p.postconfig(cf) { - return Status::NGX_ERROR.into(); - } - } - - if ngx::http::add_phase_handler::(cf).is_err() { - return Status::NGX_ERROR.into(); - } - Status::NGX_OK.into() - } -} - -static mut NGX_HTTP_IPASS_COMMANDS: [ngx_command_t; 11] = [ - ngx_command_t { - name: ngx_string!("ipass_state"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2) as ngx_uint_t, - set: Some(set_state_zone), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_data_dir"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_data_dir), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_audit_dir"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_audit_dir), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_stats_persist"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_stats_persist), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_probe_min"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_probe_min), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_proxy"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_flag_proxy), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_api"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_flag_api), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_llm"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_flag_llm), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_apix"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_flag_apix), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("ipass_cache"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(set_flag_cache), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t::empty(), -]; - -static NGX_HTTP_IPASS_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(Module::preconfiguration), - postconfiguration: Some(Module::postconfiguration), - create_main_conf: None, - init_main_conf: None, - create_srv_conf: None, - merge_srv_conf: None, - create_loc_conf: Some(Module::create_loc_conf), - merge_loc_conf: Some(Module::merge_loc_conf), -}; - -// 脱离 nginx 构建系统、单独产出 cdylib 时必须靠这个宏导出 ngx_modules 表 -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_ipass_module); - -#[used] -#[allow(non_upper_case_globals)] -pub static mut ngx_http_ipass_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_IPASS_MODULE_CTX as _, - commands: unsafe { &raw mut NGX_HTTP_IPASS_COMMANDS[0] }, - type_: NGX_HTTP_MODULE as _, - init_process: Some(init_process), - exit_process: Some(exit_process), - ..ngx_module_t::default() -}; - -/// 每个 worker 启动后执行一次。管理员播种是**幂等**的: -/// 文件里已有任何账号就直接跳过,所以多 worker 同时跑不会重复创建。 -unsafe extern "C" fn init_process(_cycle: *mut ngx_cycle_t) -> ngx_int_t { - match users::seed_admin() { - Ok(true) => util::log_warn( - "[ipass] 已创建初始管理员 admin,请首次登录后立即修改密码", - ), - Ok(false) => {} - Err(e) => util::log_err(&format!("[ipass] 初始化管理员失败: {e}")), - } - // 对话审计:确保目录存在 + 容量巡检一次(此后由请求写入节流触发) - plugin_llm::llm_audit::startup(); - // 运行期统计:把上一次退出时落盘的快照灌回共享内存。 - // **放在每个 worker 里跑是刻意的**:恢复是「键不存在才写」(put-if-absent), - // 第一个 worker 恢复完,其余 worker 全部跳过 —— 天然幂等,不会把计数恢复成 N 倍。 - // 详见 plugin_llm::stats_persist 模块注释 / docs/llm/STATS_PERSISTENCE.md。 - plugin_llm::stats_persist::restore(); - Status::NGX_OK.into() -} - -/// worker 退出前执行一次(nginx 的 `exit_process` 钩子)。 -/// -/// **这是「发版不丢统计」的关键**:共享内存随进程消失,而用量/趋势只活在共享内存里, -/// 所以必须在退出前落一次盘。`docker stop` 发的是镜像声明的 `STOPSIGNAL=SIGQUIT` -/// (优雅停机),`SIGTERM` 也走同一条 worker 退出路径 —— 两者都会到这里。 -/// 只有 `SIGKILL`(`rm -f` 超时兜底 / OOM / 断电)绕过它,那由周期落盘兜住 -/// (见 `stats_persist::maybe_dump`,默认 60 秒一次)。 -/// -/// 多 worker 会各落一次:内容本就只是「同一份共享内存的近似快照」, -/// 写临时文件带 pid、`rename` 原子,谁最后赢都对。 -unsafe extern "C" fn exit_process(_cycle: *mut ngx_cycle_t) { - if let Err(e) = plugin_llm::stats_persist::dump_now("worker-exit") { - util::log_err(&format!("[stats] 退出落盘失败:{e}")); - } -} - -// =========================================================================== -// 指令处理 -// =========================================================================== - -/// 读第 2 个参数并转成 bool(on/off/1/0/yes/no)。 -unsafe fn arg_flag(cf: *mut ngx_conf_t, idx: usize) -> Option { - let cf_ref = unsafe { cf.as_mut() }?; - let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() }; - let raw = args.get(idx)?.as_bytes(); - let s = core::str::from_utf8(raw).ok()?; - Some(matches!(s.to_ascii_lowercase().as_str(), "on" | "1" | "true" | "yes")) -} - -extern "C" fn set_flag_proxy( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - let Some(flag) = (unsafe { arg_flag(cf, 1) }) else { - return NGX_CONF_ERROR; - }; - // SAFETY: nginx 保证 conf 指向本模块的 loc conf 结构 - unsafe { (*(conf as *mut ModuleConfig)).proxy = flag }; - NGX_CONF_OK -} - -extern "C" fn set_flag_api( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - let Some(flag) = (unsafe { arg_flag(cf, 1) }) else { - return NGX_CONF_ERROR; - }; - // SAFETY: 同上 - unsafe { (*(conf as *mut ModuleConfig)).api = flag }; - NGX_CONF_OK -} - -extern "C" fn set_flag_llm( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - let Some(flag) = (unsafe { arg_flag(cf, 1) }) else { - return NGX_CONF_ERROR; - }; - // SAFETY: 同上 - unsafe { (*(conf as *mut ModuleConfig)).llm = flag }; - NGX_CONF_OK -} - -extern "C" fn set_flag_apix( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - let Some(flag) = (unsafe { arg_flag(cf, 1) }) else { - return NGX_CONF_ERROR; - }; - // SAFETY: 同上 - unsafe { (*(conf as *mut ModuleConfig)).apix = flag }; - NGX_CONF_OK -} - -extern "C" fn set_flag_cache( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - let Some(flag) = (unsafe { arg_flag(cf, 1) }) else { - return NGX_CONF_ERROR; - }; - // SAFETY: 同上 - unsafe { (*(conf as *mut ModuleConfig)).cache = flag }; - NGX_CONF_OK -} - -extern "C" fn set_state_zone( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - _conf: *mut c_void, -) -> *mut c_char { - // SAFETY: 配置回调必然拿到有效的 cf - let cf_ref = unsafe { cf.as_mut().unwrap() }; - let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() }; - - let mut name: ngx_str_t = args[1]; - let size = unsafe { ngx_parse_size(&raw mut args[2]) }; - if size == -1 { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_state: 共享内存大小非法"); - return NGX_CONF_ERROR; - } - - let zone = unsafe { - ngx_shared_memory_add( - cf, - &raw mut name, - size as usize, - (&raw mut ngx_http_ipass_module).cast(), - ) - }; - if zone.is_null() { - return NGX_CONF_ERROR; - } - - unsafe { - (*zone).init = Some(state_zone_init); - } - state::set_zone(zone); - NGX_CONF_OK -} - -/// 共享内存初始化:由 nginx 在 **fork 之前**调用一次。 -/// -/// 在这里装载三张表,所有 worker 直接共享结果——比放在 -/// `init_process`(每个 worker 各跑一遍)省掉 N-1 次文件读与 JSON 解析。 -extern "C" fn state_zone_init(zone: *mut ngx_shm_zone_t, _data: *mut c_void) -> ngx_int_t { - let status = state::init_zone(zone); - if status != Status::NGX_OK { - return status.into(); - } - - // 插件注册「删账号要清什么」:gateway 装配了 plugin_llm 的 stats(用量账本), - // 就要在这里挂上清理钩子——内核自己不认识 ust:/utt: 这些键(P0 起钩子化)。 - // on_delete 内部按函数指针去重,nginx reload 重复走到这里也安全。 - users::on_delete(plugin_llm::stats::forget); - - let loaded = plugin_domain::domains::reload_shm(); - util::log_notice(&format!("[ipass] 共享内存已装载 {loaded} 条转发映射")); - - // 账号 Key 与 LLM 上游 / API 代理必须一起进内存:各热路径未命中即 - // 401 / 404,没有任何回源兜底(同 domains 的写穿语义)。 - // 上游表装载归 plugin-llm、代理表装载归 plugin-apix。 - let keys = users::reload_shm(); - let ups = plugin_llm::upstreams::reload_shm(); - let axs = plugin_apix::apix::reload_shm(); - // 制品缓存的上游表(`cx:` → base/key/mode):热路径查不到 = 该生态未配置上游。 - let cxs = plugin_cache::upstreams::reload_shm(); - util::log_notice(&format!( - "[ipass] 已装载 {keys} 个账号 Key、{ups} 个 LLM 上游、{axs} 个 API 代理、{cxs} 个缓存上游" - )); - - Status::NGX_OK.into() -} - -extern "C" fn set_data_dir( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - _conf: *mut c_void, -) -> *mut c_char { - // SAFETY: 配置回调必然拿到有效的 cf - let cf_ref = unsafe { cf.as_mut().unwrap() }; - let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() }; - - match core::str::from_utf8(args[1].as_bytes()) { - Ok(dir) => { - store::set_data_dir(dir); - NGX_CONF_OK - } - Err(_) => { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_data_dir: 路径不是合法 UTF-8"); - NGX_CONF_ERROR - } - } -} - -/// `ipass_audit_dir `:LLM 对话审计根目录。缺省 = data 目录的兄弟目录。 -extern "C" fn set_audit_dir( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - _conf: *mut c_void, -) -> *mut c_char { - // SAFETY: 配置回调必然拿到有效的 cf - let cf_ref = unsafe { cf.as_mut().unwrap() }; - let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() }; - - match core::str::from_utf8(args[1].as_bytes()) { - Ok(dir) => { - plugin_llm::llm_audit::set_dir(dir); - NGX_CONF_OK - } - Err(_) => { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_audit_dir: 路径不是合法 UTF-8"); - NGX_CONF_ERROR - } - } -} - -/// `ipass_stats_persist <秒>`:运行期统计(用量 / 趋势 / 账号负载)的周期落盘间隔。 -/// -/// 缺省 60 秒;`0` = 关闭周期落盘,只保留 worker 退出钩子那一次(见 `exit_process`)。 -/// 调小它只为缩短「被 SIGKILL 时最多丢多久」,正常发版走退出钩子,本来就是零丢失。 -extern "C" fn set_stats_persist( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - _conf: *mut c_void, -) -> *mut c_char { - // SAFETY: 配置回调必然拿到有效的 cf - let cf_ref = unsafe { cf.as_mut().unwrap() }; - let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() }; - - let Ok(raw) = core::str::from_utf8(args[1].as_bytes()) else { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_stats_persist: 不是合法 UTF-8"); - return NGX_CONF_ERROR; - }; - let Ok(secs) = raw.trim().parse::() else { - ngx_conf_log_error!( - NGX_LOG_EMERG, - cf_ref, - "ipass_stats_persist: 需要非负整数秒(0 = 只靠退出钩子)" - ); - return NGX_CONF_ERROR; - }; - if secs < 0 { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_stats_persist: 秒数不能为负"); - return NGX_CONF_ERROR; - } - plugin_llm::stats_persist::set_interval(secs); - NGX_CONF_OK -} - -/// `ipass_probe_min <秒>`:额度耗尽探测闭环的**首轮**退避(默认 60 秒)。 -/// -/// 冷却账号第一次可被真实请求探测的等待时间;之后每次探测失败翻倍、封顶 30 分钟 -/// (见 `upstreams` 模块「冷却」段与 config::ACCT_COOL_HARD_CAP)。 -/// 生产 `conf/nginx.conf` **刻意不写**这条指令——与 `ipass_stats_persist` 同理, -/// 回滚到不含该指令的旧镜像时不会 unknown directive 起不来;测试夹具用 `1` 压秒。 -extern "C" fn set_probe_min( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - _conf: *mut c_void, -) -> *mut c_char { - // SAFETY: 配置回调必然拿到有效的 cf - let cf_ref = unsafe { cf.as_mut().unwrap() }; - let args: &mut [ngx_str_t] = unsafe { (*cf_ref.args).as_slice_mut() }; - - let Ok(raw) = core::str::from_utf8(args[1].as_bytes()) else { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_probe_min: 不是合法 UTF-8"); - return NGX_CONF_ERROR; - }; - let Ok(secs) = raw.trim().parse::() else { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_probe_min: 需要正整数秒"); - return NGX_CONF_ERROR; - }; - if secs <= 0 { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_probe_min: 秒数必须为正"); - return NGX_CONF_ERROR; - } - plugin_llm::upstreams::set_probe_min(secs); - NGX_CONF_OK -} - -/// 供测试与诊断使用:`NgxStr` 类型在模块里被多处引用,确保编译期链接到正确的符号 -#[allow(dead_code)] -fn _type_witness(_: &NgxStr, _: &NgxString) {} diff --git a/rust/vendor/ngx-rust/.cargo/audit.toml b/rust/vendor/ngx-rust/.cargo/audit.toml deleted file mode 100644 index e796859..0000000 --- a/rust/vendor/ngx-rust/.cargo/audit.toml +++ /dev/null @@ -1,35 +0,0 @@ -# audit config file -# -# It may be located in the user home (`~/.cargo/audit.toml`) or in the project -# root (`.cargo/audit.toml`). -# -# All of the options which can be passed via CLI arguments can also be -# permanently specified in this file. -# It may be located in the user home (`~/.cargo/audit.toml`) or in the project -# root (`.cargo/audit.toml`). - -[advisories] -ignore = ["RUSTSEC-2020-0071"] - -# # Advisory Database Configuration -# [database] -# path = "~/.cargo/advisory-db" # Path where advisory git repo will be cloned -# url = "https://github.com/RustSec/advisory-db.git" # URL to git repo -# fetch = true # Perform a `git fetch` before auditing (default: true) -# stale = false # Allow stale advisory DB (i.e. no commits for 90 days, default: false) - -# # Output Configuration -# [output] -# deny = ["unmaintained"] # exit on error if unmaintained dependencies are found -# format = "terminal" # "terminal" (human readable report) or "json" -# quiet = false # Only print information on error -# show_tree = true # Show inverse dependency trees along with advisories (default: true) - -# # Target Configuration -# [target] -# arch = "x86_64" # Ignore advisories for CPU architectures other than this one -# os = "linux" # Ignore advisories for operating systems other than this one - -# [yanked] -# enabled = true # Warn for yanked crates in Cargo.lock (default: true) -# update_index = true # Auto-update the crates.io index (default: true) diff --git a/rust/vendor/ngx-rust/.cargo/config.toml b/rust/vendor/ngx-rust/.cargo/config.toml deleted file mode 100644 index 910c3da..0000000 --- a/rust/vendor/ngx-rust/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[env] -CACHE_DIR = { value = ".cache", relative = true } diff --git a/rust/vendor/ngx-rust/.dockerignore b/rust/vendor/ngx-rust/.dockerignore deleted file mode 100644 index d1c460e..0000000 --- a/rust/vendor/ngx-rust/.dockerignore +++ /dev/null @@ -1,23 +0,0 @@ -**/.idea -.cargo -.dockerignore -.git -.github -CHANGELOG.md -CODE_OF_CONDUCT.md -CONTRIBUTING.md -Containerfile.debian -Dockerfile -GNUmakefile -LICENSE.txt -README.md -SECURITY.md -build - -.vscode/** - -**/target -target - -.cache/ -.nginx/ diff --git a/rust/vendor/ngx-rust/.git-blame-ignore-revs b/rust/vendor/ngx-rust/.git-blame-ignore-revs deleted file mode 100644 index 15ce9d0..0000000 --- a/rust/vendor/ngx-rust/.git-blame-ignore-revs +++ /dev/null @@ -1,17 +0,0 @@ -# This file specifies the revisions hidden from the blame view in GitHub UI. -# -# You can achieve the same effect locally with "--ignore-revs-file": -# -# git blame --ignore-revs-file .git-blame-ignore-revs -# -# or "blame.ignoreRevsFile" configuration option -# -# git config --local blame.ignoreRevsFile .git-blame-ignore-revs -# -# See also: -# https://docs.github.com/en/repositories/working-with-files/using-files/viewing-and-understanding-files - -# Tree-wide formatting change: max-width 120 -> 100 -d866b64a3c83105c20b0c005392f8e8fcaaf2066 -# Tree-wide formatting change: use_small_heuristics -> "Max" -4b30e402b0393dd4b888980d57ce6792adbb9455 diff --git a/rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/bug_report.md b/rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index a85753e..0000000 --- a/rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve - ---- - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behavior: -1. See my project that depends on this project -2. View logs on '....' -3. Run this test I created -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Your environment** -* Version of the repo - a specific commit or tag -* Version of ngx-rust -* Version of Rust -* Version of NGINX (and all dependencies if modified) -* OS and distribution -* Details about containerization, virtualization, or physical environment - -**Additional context** -Add any other context about the problem here. Any log files you want to share. diff --git a/rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/feature_request.md b/rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index 6bcce42..0000000 --- a/rust/vendor/ngx-rust/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project - ---- - -**Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Additional context** -Add any other context or screenshots about the feature request here. \ No newline at end of file diff --git a/rust/vendor/ngx-rust/.github/PULL_REQUEST_TEMPLATE.md b/rust/vendor/ngx-rust/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 0037d3d..0000000 --- a/rust/vendor/ngx-rust/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,15 +0,0 @@ -### Proposed changes -Describe the use case and detail of the change. If this PR addresses an issue -on GitHub, make sure to include a link to that issue here in this description -(not in the title of the PR). - -### Checklist -Before creating a PR, run through this checklist and mark each as complete. - -- [ ] I have written my commit messages in the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format. -- [ ] I have read the [CONTRIBUTING](/CONTRIBUTING.md) doc -- [ ] I have added tests (when possible) that prove my fix is effective or that my feature works -- [ ] I have checked that all unit tests pass after adding my changes -- [ ] I have updated necessary documentation -- [ ] I have rebased my branch onto main -- [ ] I will ensure my PR is targeting the main branch and pulling from my branch from my own fork diff --git a/rust/vendor/ngx-rust/.github/dependabot.yml b/rust/vendor/ngx-rust/.github/dependabot.yml deleted file mode 100644 index 0b574cc..0000000 --- a/rust/vendor/ngx-rust/.github/dependabot.yml +++ /dev/null @@ -1,26 +0,0 @@ -# To get started with Dependabot version updates, you'll need to specify which -# package ecosystems to update and where the package manifests are located. -# Please see the documentation for all configuration options: -# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates - -version: 2 -updates: - - - package-ecosystem: "cargo" - allow: - # Allow both direct and indirect updates for all packages. - - dependency-type: "all" - directory: "/" # Location of package manifests - schedule: - interval: "weekly" - groups: - all-dependencies: - patterns: - - "*" - # semver-incompatible updates will be handled manually - versioning-strategy: "lockfile-only" - - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "weekly" diff --git a/rust/vendor/ngx-rust/.github/workflows/cargo-deny.yaml b/rust/vendor/ngx-rust/.github/workflows/cargo-deny.yaml deleted file mode 100644 index 1e44ade..0000000 --- a/rust/vendor/ngx-rust/.github/workflows/cargo-deny.yaml +++ /dev/null @@ -1,25 +0,0 @@ -name: cargo-deny -on: - push: - branches: - - main - - "[0-9]+.[0-9]+.x" - pull_request: - -jobs: - check: - runs-on: ubuntu-latest - strategy: - matrix: - checks: - - advisories - - bans licenses sources - - # Prevent sudden announcement of a new advisory from failing ci: - continue-on-error: ${{ matrix.checks == 'advisories' }} - - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: EmbarkStudios/cargo-deny-action@3c6349835b2b7b196a839186cb8b78e02f7b5f25 # v2.1.1 - with: - command: check ${{ matrix.checks }} diff --git a/rust/vendor/ngx-rust/.github/workflows/ci.yaml b/rust/vendor/ngx-rust/.github/workflows/ci.yaml deleted file mode 100644 index d02c3c5..0000000 --- a/rust/vendor/ngx-rust/.github/workflows/ci.yaml +++ /dev/null @@ -1,194 +0,0 @@ -name: Rust - -on: [ push, pull_request ] - -env: - CARGO_TERM_COLOR: 'always' - CARGO_TERM_VERBOSE: 'true' - RUSTFLAGS: '--deny warnings' - RUSTDOCFLAGS: '-Dwarnings' - -jobs: - rust-version: - name: Minimal supported Rust version - outputs: - version: ${{ steps.read_version.outputs.msrv }} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - id: read_version - run: | - awk -F '=' \ - '/^rust-version[[:space:]]*=/ { gsub(/([" ]|#.*)/,"",$2); print ("msrv=" $2) }' \ - Cargo.toml \ - | tee -a "$GITHUB_OUTPUT" - - test-linux: - name: Test (Linux) - needs: rust-version - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - rust-version: - - ${{ needs.rust-version.outputs.version }} - - stable - steps: - - name: checkout source - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - submodules: true - - name: set up cargo cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - continue-on-error: false - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ matrix.rust-version}}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo-${{ matrix.rust-version }}- - - name: set up nginx deps cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - continue-on-error: false - with: - path: | - .cache/.gnupg - .cache/nginx - .cache/*.tar.gz - .cache/*.tar.asc - .cache/*.tar.sig - key: ${{ runner.os }}-deps-${{ hashFiles('**/nginx-sys/build.rs') }} - restore-keys: ${{ runner.os }}-deps- - - - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 - with: - components: rustfmt, clippy - toolchain: ${{ matrix.rust-version }} - - - name: build (all targets, all features) - id: build - run: cargo build --workspace --all-targets --all-features - - - name: check ngx, no default features (no_std) - run: cargo check -p ngx --no-default-features --features vendored - - - name: check ngx, alloc - run: cargo check -p ngx --no-default-features --features vendored,alloc - - - name: check ngx, std - run: cargo check -p ngx --no-default-features --features vendored,std - - - name: check ngx, alloc + serde - run: cargo check -p ngx --no-default-features --features vendored,alloc,serde - - - name: clippy - # always run if build succeeds - if: ${{ !cancelled() && steps.build.outcome == 'success' }} - run: cargo clippy --workspace --all-targets --all-features -- -Dwarnings - - - name: test - # always run if build succeeds - if: ${{ !cancelled() && steps.build.outcome == 'success' }} - run: cargo test --workspace --all-features - - - name: rustdoc - # always run if build succeeds - if: ${{ !cancelled() && steps.build.outcome == 'success' }} - run: cargo doc --all-features --no-deps - - examples-linux: - name: Examples (Linux) - runs-on: ubuntu-latest - steps: - - name: checkout source - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - submodules: true - - name: set up cargo cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - continue-on-error: false - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo- - - name: set up nginx deps cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - continue-on-error: false - with: - path: | - .cache/.gnupg - .cache/nginx - .cache/*.tar.gz - .cache/*.tar.asc - .cache/*.tar.sig - key: ${{ runner.os }}-deps-${{ hashFiles('**/nginx-sys/build.rs') }} - restore-keys: ${{ runner.os }}-deps- - - name: compile examples - run: cargo build --release --package examples --examples --all-features - - test-macos: - name: Test (MacOS) - runs-on: macos-latest - steps: - - name: install command line dependencies - run: brew install make gnupg - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - submodules: true - - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 - with: - toolchain: stable - - name: set up cargo cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - continue-on-error: false - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo- - - name: set up nginx deps cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - continue-on-error: false - with: - path: | - .cache/*.tar.gz - .cache/*.tar.asc - .cache/*.tar.sig - key: ${{ runner.os }}-deps-${{ hashFiles('**/nginx-sys/build.rs') }} - restore-keys: ${{ runner.os }}-deps- - - name: current directory - run: pwd - - name: make cache directory - run: mkdir -p .cache/.gnupg - - name: disable ipv6 for gpg - run: echo "disable-ipv6" > .cache/.gnupg/dirmngr.conf - - name: build - run: cargo build --workspace --all-targets --features "async,vendored" - - name: run tests - run: cargo test --workspace --features "async,vendored" - - fmt: - name: Rustfmt - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 - with: - components: rustfmt - toolchain: stable - - name: rustfmt version - run: rustfmt --version - - name: cargo fmt - run: cargo fmt --all --check diff --git a/rust/vendor/ngx-rust/.github/workflows/nginx.yaml b/rust/vendor/ngx-rust/.github/workflows/nginx.yaml deleted file mode 100644 index 2f88fbc..0000000 --- a/rust/vendor/ngx-rust/.github/workflows/nginx.yaml +++ /dev/null @@ -1,238 +0,0 @@ -name: NGINX - -on: - push: - branches: - - main - - "[0-9]+.[0-9]+.x" - pull_request: - -env: - CARGO_TERM_COLOR: 'always' - RUST_BACKTRACE: '1' - NGX_CONFIGURE_CMD: >- - auto/configure - --with-compat - --with-debug - --with-http_realip_module - --with-http_ssl_module - --with-http_v2_module - --with-stream - --with-stream_realip_module - --with-stream_ssl_module - - NGX_CONFIGURE_UNIX: >- - --with-threads - - NGX_CONFIGURE_WINDOWS: >- - --with-cc=cl - --prefix= - --conf-path=conf/nginx.conf - --pid-path=logs/nginx.pid - --http-log-path=logs/access.log - --error-log-path=logs/error.log - --sbin-path=nginx.exe - --http-client-body-temp-path=temp/client_body_temp - --http-proxy-temp-path=temp/proxy_temp - --http-fastcgi-temp-path=temp/fastcgi_temp - --http-scgi-temp-path=temp/scgi_temp - --http-uwsgi-temp-path=temp/uwsgi_temp - --with-cc-opt=-DFD_SETSIZE=1024 - --with-pcre=objs/lib/pcre - --with-zlib=objs/lib/zlib - --with-openssl=objs/lib/openssl - --with-openssl-opt="no-asm no-module no-tests no-makedepend -D_WIN32_WINNT=0x0601" - - NGX_CONFIGURE_DYNAMIC_MODULES: >- - --add-dynamic-module=${{ github.workspace }}/examples - NGX_CONFIGURE_STATIC_MODULES: >- - --add-module=${{ github.workspace }}/examples - - NGX_TEST_FILES: examples/t - NGX_TEST_GLOBALS_DYNAMIC: >- - load_module ${{ github.workspace }}/nginx/objs/ngx_http_async_module.so; - load_module ${{ github.workspace }}/nginx/objs/ngx_http_awssigv4_module.so; - load_module ${{ github.workspace }}/nginx/objs/ngx_http_curl_module.so; - load_module ${{ github.workspace }}/nginx/objs/ngx_http_shared_dict_module.so; - load_module ${{ github.workspace }}/nginx/objs/ngx_http_upstream_custom_module.so; - - OPENSSL_VERSION: '3.0.16' - PCRE2_VERSION: '10.45' - ZLIB_VERSION: '1.3.1' - -jobs: - linux: - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - nginx-ref: - - master - - stable-1.30 - module: - - static - - dynamic - include: - # minimal supported nginx version (Debian bookworm: 1.22.1) - - nginx-ref: stable-1.22 - module: dynamic - - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ matrix.nginx-ref }} - repository: 'nginx/nginx' - path: 'nginx' - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - repository: 'nginx/nginx-tests' - path: 'nginx/tests' - sparse-checkout: | - lib - - - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 - with: - toolchain: stable - - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - nginx/objs/**/CACHEDIR.TAG - nginx/objs/**/ngx-debug - nginx/objs/**/ngx-release - key: ${{ runner.os }}-nginx-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-nginx- - - - name: Update configure arguments - if: matrix.nginx-ref != 'stable-1.22' - run: | - echo NGX_CONFIGURE_UNIX="${NGX_CONFIGURE_UNIX} --with-http_v3_module" \ - >> "$GITHUB_ENV" - - - name: Configure nginx with static modules - if: matrix.module == 'static' - working-directory: nginx - run: | - ${NGX_CONFIGURE_CMD} \ - ${NGX_CONFIGURE_UNIX} \ - ${NGX_CONFIGURE_STATIC_MODULES} - - - name: Configure nginx with dynamic modules - if: matrix.module != 'static' - working-directory: nginx - run: | - ${NGX_CONFIGURE_CMD} \ - ${NGX_CONFIGURE_UNIX} \ - ${NGX_CONFIGURE_DYNAMIC_MODULES} - echo TEST_NGINX_GLOBALS="$NGX_TEST_GLOBALS_DYNAMIC" >> "$GITHUB_ENV" - - - name: Build nginx - working-directory: nginx - run: make -j$(nproc) - - - name: Run tests - env: - PERL5LIB: ${{ github.workspace }}/nginx/tests/lib - TEST_NGINX_BINARY: ${{ github.workspace }}/nginx/objs/nginx - TEST_NGINX_MODULES: ${{ github.workspace }}/nginx/objs - TEST_NGINX_VERBOSE: 1 - run: | - prove -j$(nproc) --state=save ${NGX_TEST_FILES} || prove -v --state=failed - - windows: - runs-on: windows-2022 - env: - TEMP: "C:\\TEMP" - TMP: "C:\\TEMP" - TMPDIR: "C:\\TEMP" - VCARCH: x64 - - strategy: - fail-fast: false - matrix: - nginx-ref: - - master - module: - - static - - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ matrix.nginx-ref }} - repository: 'nginx/nginx' - path: 'nginx' - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - repository: 'nginx/nginx-tests' - path: 'nginx/tests' - sparse-checkout: | - lib - - - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 - with: - toolchain: stable - - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - nginx/objs/**/CACHEDIR.TAG - nginx/objs/**/ngx-debug - nginx/objs/**/ngx-release - key: ${{ runner.os }}-nginx-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-nginx- - - - name: Prepare build environment - shell: bash - working-directory: nginx - run: | - # Disable dynamic lookup of WSAPoll(); it crashes if the symbol is already imported by - # Rust stdlib. - sed 's/\(_WIN32_WINNT\s*\) 0x0501/\1 0x0601/' -i src/os/win32/ngx_win32_config.h - - echo VCVARSALL="$('C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe' -products \* -latest -property installationPath)\\VC\\Auxiliary\\Build\\vcvarsall.bat" \ - >> "$GITHUB_ENV" - - mkdir -p $TEMP - mkdir -p objs/lib - - curl -sLO https://github.com/PCRE2Project/pcre2/releases/download/pcre2-$PCRE2_VERSION/pcre2-$PCRE2_VERSION.tar.gz - tar -C objs/lib --transform "s/pcre2-$PCRE2_VERSION/pcre/" -xzf ./pcre2-$PCRE2_VERSION.tar.gz - echo '#include ' > objs/lib/pcre/src/inttypes.h - - curl -sLO https://github.com/madler/zlib/releases/download/v$ZLIB_VERSION/zlib-$ZLIB_VERSION.tar.gz - tar -C objs/lib --transform "s/zlib-$ZLIB_VERSION/zlib/" -xzf ./zlib-$ZLIB_VERSION.tar.gz - - curl -sLO https://github.com/openssl/openssl/releases/download/openssl-$OPENSSL_VERSION/openssl-$OPENSSL_VERSION.tar.gz - tar -C objs/lib --transform "s/openssl-$OPENSSL_VERSION/openssl/" -xzf ./openssl-$OPENSSL_VERSION.tar.gz - - - name: Configure and build nginx - shell: cmd - working-directory: nginx - run: | - @echo on - call "%VCVARSALL%" %VCARCH% - bash.exe ^ - %NGX_CONFIGURE_CMD% ^ - %NGX_CONFIGURE_WINDOWS% ^ - %NGX_CONFIGURE_STATIC_MODULES% - nmake -f objs/Makefile - - - name: Run tests - shell: cmd - env: - PERL5LIB: "${{ github.workspace }}\\nginx\\tests\\lib" - TEST_NGINX_BINARY: "${{ github.workspace }}\\nginx\\objs\\nginx.exe" - TEST_NGINX_VERBOSE: 1 - run: | - prove --state=save %NGX_TEST_FILES% || prove -v --state=failed diff --git a/rust/vendor/ngx-rust/.github/workflows/sanitizers.yaml b/rust/vendor/ngx-rust/.github/workflows/sanitizers.yaml deleted file mode 100644 index d2bc2d1..0000000 --- a/rust/vendor/ngx-rust/.github/workflows/sanitizers.yaml +++ /dev/null @@ -1,112 +0,0 @@ -name: sanitizers - -on: - push: - branches: - - main - - "[0-9]+.[0-9]+.x" - pull_request: - -env: - CARGO_TERM_COLOR: 'always' - RUST_BACKTRACE: '1' - BUILDREQUIRES: >- - openssl-devel pcre2-devel zlib-devel - cargo rust-src rustfmt - clang compiler-rt llvm - git-core - make patch - perl-FindBin - perl-IO-Socket-SSL - perl-Test-Harness - perl-Test-Simple - perl-lib - -jobs: - test: - runs-on: ubuntu-latest - container: ghcr.io/almalinux/almalinux:10 - - strategy: - fail-fast: false - matrix: - nginx-ref: - # master - - stable-1.30 - - steps: - - name: Install dependencies - run: dnf install -y ${BUILDREQUIRES} - - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ matrix.nginx-ref }} - repository: 'nginx/nginx' - path: 'nginx' - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - repository: 'nginx/nginx-tests' - path: 'nginx/tests' - - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - nginx/objs/ngx_rust_examples - key: ${{ runner.os }}-cargo-asan-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo-asan- - - - name: Configure and build nginx - working-directory: nginx - env: - CFLAGS: >- - -DNGX_DEBUG_PALLOC=1 - -DNGX_SUPPRESS_WARN=1 - -O1 - -fno-omit-frame-pointer - -fsanitize=address,undefined - LDFLAGS: -fsanitize=address,undefined - RUSTFLAGS: -Zsanitizer=address -Zexternal-clangrt - # Pass --target to cargo to ensure that sanitizer flags are not - # applied to the build scripts and proc-macros. - NGX_RUST_TARGET: x86_64-unknown-linux-gnu - # Extra options passed to cargo rustc - NGX_RUSTC_OPT: -Zbuild-std - # Enable unstable features, such as the -Z options above, - # in the stable toolchain. - RUSTC_BOOTSTRAP: 1 - run: | - patch -p1 < $GITHUB_WORKSPACE/misc/nginx-sanitizer-support.patch - auto/configure \ - --with-cc=clang \ - --with-cc-opt="$CFLAGS" \ - --with-ld-opt="$LDFLAGS" \ - --with-compat \ - --with-debug \ - --with-http_ssl_module \ - --with-http_v2_module \ - --with-http_v3_module \ - --with-stream \ - --with-stream_ssl_module \ - --with-threads \ - --add-module=$(realpath ../examples) - make -j$(nproc) - - - name: Run tests - env: - ASAN_OPTIONS: detect_stack_use_after_return=1:detect_odr_violation=0 - # `container` job steps are running as root, and thus all the files - # created by the test scripts are owned by root. - # But the worker processes are spawned as "nobody" by default, - # resulting in permission errors. - TEST_NGINX_GLOBALS: >- - user root; - run: | - TEST_NGINX_BINARY="$PWD/nginx/objs/nginx" \ - LSAN_OPTIONS="suppressions=$PWD/misc/lsan-suppressions.txt" \ - UBSAN_OPTIONS="suppressions=$PWD/misc/ubsan-suppressions.txt" \ - prove -v -Inginx/tests/lib examples/t diff --git a/rust/vendor/ngx-rust/.gitignore b/rust/vendor/ngx-rust/.gitignore deleted file mode 100644 index 0a75edb..0000000 --- a/rust/vendor/ngx-rust/.gitignore +++ /dev/null @@ -1,256 +0,0 @@ -# Created by https://www.toptal.com/developers/gitignore/api/c,rust,intellij+all,visualstudiocode,vim,emacs -# Edit at https://www.toptal.com/developers/gitignore?templates=c,rust,intellij+all,visualstudiocode,vim,emacs - -### C ### -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf - -### Emacs ### -# -*- mode: gitignore; -*- -*~ -\#*\# -/.emacs.desktop -/.emacs.desktop.lock -*.elc -auto-save-list -tramp -.\#* - -# Org-mode -.org-id-locations -*_archive - -# flymake-mode -*_flymake.* - -# eshell files -/eshell/history -/eshell/lastdir - -# elpa packages -/elpa/ - -# reftex files -*.rel - -# AUCTeX auto folder -/auto/ - -# cask packages -.cask/ -dist/ - -# Flycheck -flycheck_*.el - -# server auth directory -/server/ - -# projectiles files -.projectile - -# directory configuration -.dir-locals.el - -# network security -/network-security.data - - -### Intellij+all ### -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/**/usage.statistics.xml -.idea/**/dictionaries -.idea/**/shelf - -# AWS User-specific -.idea/**/aws.xml - -# Generated files -.idea/**/contentModel.xml - -# Sensitive or high-churn files -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml -.idea/**/dbnavigator.xml - -# Gradle -.idea/**/gradle.xml -.idea/**/libraries - -# Gradle and Maven with auto-import -# When using Gradle or Maven with auto-import, you should exclude module files, -# since they will be recreated, and may cause churn. Uncomment if using -# auto-import. -# .idea/artifacts -# .idea/compiler.xml -# .idea/jarRepositories.xml -# .idea/modules.xml -# .idea/*.iml -# .idea/modules -# *.iml -# *.ipr - -# CMake -cmake-build-*/ - -# Mongo Explorer plugin -.idea/**/mongoSettings.xml - -# File-based project format -*.iws - -# IntelliJ -out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Cursive Clojure plugin -.idea/replstate.xml - -# SonarLint plugin -.idea/sonarlint/ - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties - -# Editor-based Rest Client -.idea/httpRequests - -# Android studio 3.1+ serialized cache file -.idea/caches/build_file_checksums.ser - -### Intellij+all Patch ### -# Ignore everything but code style settings and run configurations -# that are supposed to be shared within teams. - -.idea/* - -!.idea/codeStyles -!.idea/runConfigurations - -### Rust ### -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information - -### Vim ### -# Swap -[._]*.s[a-v][a-z] -!*.svg # comment out if you don't need vector files -[._]*.sw[a-p] -[._]s[a-rt-v][a-z] -[._]ss[a-gi-z] -[._]sw[a-p] - -# Session -Session.vim -Sessionx.vim - -# Temporary -.netrwhist -# Auto-generated tag files -tags -# Persistent undo -[._]*.un~ - -### VisualStudioCode ### -.vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json -!.vscode/*.code-snippets - -# Local History for Visual Studio Code -.history/ - -# Built Visual Studio Code Extensions -*.vsix - -### VisualStudioCode Patch ### -# Ignore all local history of files -.history -.ionide - -# End of https://www.toptal.com/developers/gitignore/api/c,rust,intellij+all,visualstudiocode,vim,emacs - -.vscode/c_cpp_properties.json -.cache/ -.nginx/ -.DS_Store diff --git a/rust/vendor/ngx-rust/.gitmodules b/rust/vendor/ngx-rust/.gitmodules deleted file mode 100644 index d4fe729..0000000 --- a/rust/vendor/ngx-rust/.gitmodules +++ /dev/null @@ -1,4 +0,0 @@ -[submodule "nginx-src/nginx"] - path = nginx-src/nginx - url = https://github.com/nginx/nginx.git - branch = stable-1.28 diff --git a/rust/vendor/ngx-rust/CHANGELOG.md b/rust/vendor/ngx-rust/CHANGELOG.md deleted file mode 100644 index 6f23139..0000000 --- a/rust/vendor/ngx-rust/CHANGELOG.md +++ /dev/null @@ -1,170 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -## Release 0.5.0 - -### Breaking changes - -The release contains too many breaking changes to offer a complete migration -guide. We will strive to do better in future releases; meanwhile, we suggest to -look at the changes in example modules: - - git diff v0.4.1..v0.5.0 -- examples/*.rs - -### Build experience - -In this release, we shifted the focus to an integration into the NGINX build -process. - -The default set of features now requires providing a preconfigured NGINX source -tree with `NGINX_SOURCE_DIR` and `NGINX_BUILD_DIR` variables ([#67], [#124]). - -The option to build a copy of NGINX as a part of the module process was -preserved as a feature flag `vendored` and no longer requires downloading -sources from the network ([#160]). -We encourage you to limit use of this feature to testing only. - -The [`examples`](./examples/) directory provides `config`, `config.make` and -`auto/rust` scripts to add a Rust-based module to the nginx build, linked -statically with `--add-module=` or dynamically with `--add-dynamic-module=`. -[nginx-acme] offers another, more complete example ([#67], [#95], [#124], -[#154], [#176]). - -In addition, we now have a proper detection of the features available in the -build of NGINX ([#97]). See [build.rs](./build.rs) for the necessary setup for -your project. - -[nginx-acme]: https://github.com/nginx/nginx-acme -[#67]: https://github.com/nginx/ngx-rust/pull/67 -[#95]: https://github.com/nginx/ngx-rust/pull/95 -[#97]: https://github.com/nginx/ngx-rust/pull/97 -[#124]: https://github.com/nginx/ngx-rust/pull/124 -[#154]: https://github.com/nginx/ngx-rust/pull/154 -[#160]: https://github.com/nginx/ngx-rust/pull/160 -[#176]: https://github.com/nginx/ngx-rust/pull/176 - -### Module declaration - -The traits for declaring a module and accessing module configuration underwent -significant changes in [#142] with the goal of reducing the amount of unsafe -code and enforcing type safety. - -See for a -summary and migration steps. - -[#142]: https://github.com/nginx/ngx-rust/pull/142 - -### Allocators - -`ngx` now offers custom allocator support based on the [allocator-api2] crate. -The `ngx::core::Pool` and `ngx::core::SlabPool` can be used for failible -allocations within the NGINX pools and shared zones correspondingly -([#164], [#171]). - -Most of the crates with [allocator-api2 support] are compatible with this -implementation. We also provide wrappers for common data structures implemented -in NGINX in [`ngx::collections`](./src/collections/) ([#164], [#181]). - -[allocator-api2]: https://crates.io/crates/allocator_api2 -[allocator-api2 support]: https://crates.io/crates/allocator_api2/reverse_dependencies -[#164]: https://github.com/nginx/ngx-rust/pull/164 -[#171]: https://github.com/nginx/ngx-rust/pull/171 -[#181]: https://github.com/nginx/ngx-rust/pull/181 - - -### Other - -* We audited the code and fixed or removed most of the methods that made wrong - assumptions and could panic or crash ([#91], [#152], [#183]). -* `no_std` build support ([#111]). -* Logging API improvements ([#113], [#187]) -* The SDK and the example modules can be built and are tested in CI on Windows - ([#124], [#161]). No further porting or testing work was done. -* Reimplementations for `nginx-sys` methods and macros that cannot be translated - with bindgen ([#131], [#162], [#167]) -* Initial work on the NGINX async runtime ([#170]) -* The default branch was renamed to `main`. - -[#91]: https://github.com/nginx/ngx-rust/pull/91 -[#111]: https://github.com/nginx/ngx-rust/pull/111 -[#113]: https://github.com/nginx/ngx-rust/pull/113 -[#131]: https://github.com/nginx/ngx-rust/pull/131 -[#152]: https://github.com/nginx/ngx-rust/pull/152 -[#161]: https://github.com/nginx/ngx-rust/pull/161 -[#162]: https://github.com/nginx/ngx-rust/pull/162 -[#167]: https://github.com/nginx/ngx-rust/pull/167 -[#170]: https://github.com/nginx/ngx-rust/pull/170 -[#183]: https://github.com/nginx/ngx-rust/pull/183 -[#187]: https://github.com/nginx/ngx-rust/pull/187 - -### Supported versions - -The minimum supported Rust version is 1.81.0. The version was chosen to support -the packaged Rust toolchain in the recent versions of popular Linux and BSD -distributions. - -The minimum supported NGINX version is 1.22. The bindings may compile with an -older version of NGINX, but we do not test that regularly. - -Full changelog: [v0.4.1..v0.5.0](https://github.com/nginx/ngx-rust/compare/v0.4.1...v0.5.0) - -## Release v0.4.1 - * release: ngx 0.4.1 (9d2ce0d) - * release: nginx-sys 0.2.1 (89eb277) - * fix(#50): user_agent method returns 'Option<&NgxStr>' and not '&NgxStr' (8706830) - * fix(#41): zlib version update (89e0fcc) - * fix: check user_agent for null (0ca0bc9) - * feat: (2 of 2) Upstream module example (#37) (840d789) - * feat: (1 of 2) Supporting changes for upstream example module. (#36) (8b3a119) - * Revert "ci: use GH cache for .cache folder on MAC OS" (7972ae7) - * docs: updated changelog (74604e2) - -## Release 0.4.0-beta - * realease: bump nginx-sys to 0.2.0 (ad093d8) - * feat: unsafe updates for raw pointer arguments (1b88323) - * feat: Add debug log mask support (#40) (57c68ff) - * docs: add support badge to README (4aa2e35) - * cargo: make macOS linker flags workaround apply to Apple silicon (f4ea26f) - * docs: added repostatus and crates badges (dd687a4) - - ## Initial release 0.3.0-beta - * docs: prepare ngx to be published to crates.io (a1bff29) - * fix: nginx-sys enable a few modules by default (f23e4b1) - * !misc: project refactor and new module structure (b3e8f45) - * update README (#18) (d2c0b3a) - * Fix usage example in README (#16) (8deaec0) - * use nginxinc namespace (9bb9ef6) - * upgrade to rust 1.26 (#15) (bbfc114) - * 1.39 upgrade (#12) (be9d532) - * add pkg-config (#11) (a33c329) - * upgrade to nginx 1.13.7 (#10) (8c6b968) - * update the README (#9) (7693ea2) - * Rust 1.21 (#8) (4fa395c) - * Rust 1.21 (#7) (9517b56) - * add rust docker tooling (8b4d492) - * revert back tool tag (82bd0d6) - * update the tag for rust tool image (f2418c0) - * bump up tool image to 1.22 (7db2ced) - * add rust tool on top of the nginx (9aa4fa0) - * add developer version of nginx which can do ps (994c703) - * upgrade nginx to 1.13.5 bump up cargo version number respectively separate out nginx make file (7095777) - * consolidate makefile by reducing duplicates simplify docker command (cfe1756) - * set default rust tooling to 1.20 generate linux binding in the cargo build fix the linux configuration (d396803) - * build nginx binary for darwin add target for darwin (d2e04ce) - * add configuration for linux (b3bf4da) - * remove unnecessary reference to libc invoke make for build.rs in the mac (8e1c544) - * add unit test for nginx server (d0ff3df) - * Merge branch 'master' into module (4f6ed43) - * update the README (a8cfe50) - * add license file (805e70b) - * add license file (8c574be) - * add nginx instance module where you can start and stop nginx sample test for instance (acfc545) - * upgrade bindgen to latest version remove redudant c module code update the nginx config (3102b5a) - * update the tools make file to use Dockerhub (36ae1ca) - * update the README (ba563fc) - * add tools directory to generate docker image for rust toolchain check for nginx directory when setting up source (6842329) - * add targets for building on linux (6176272) - * make target for setting up darwin source add os specific build path (fbc5e2f) - * initial check in generate binding (a38b8ab) - diff --git a/rust/vendor/ngx-rust/CODE_OF_CONDUCT.md b/rust/vendor/ngx-rust/CODE_OF_CONDUCT.md deleted file mode 100644 index 4547fd8..0000000 --- a/rust/vendor/ngx-rust/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,129 +0,0 @@ - -# Contributor Covenant Code of Conduct - -## Our Pledge - -We as members, contributors, and leaders pledge to make participation in our -community a harassment-free experience for everyone, regardless of age, body -size, visible or invisible disability, ethnicity, sex characteristics, gender -identity and expression, level of experience, education, socio-economic status, -nationality, personal appearance, race, religion, or sexual identity -and orientation. - -We pledge to act and interact in ways that contribute to an open, welcoming, -diverse, inclusive, and healthy community. - -## Our Standards - -Examples of behavior that contributes to a positive environment for our -community include: - -* Demonstrating empathy and kindness toward other people -* Being respectful of differing opinions, viewpoints, and experiences -* Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, - and learning from the experience -* Focusing on what is best not just for us as individuals, but for the - overall community - -Examples of unacceptable behavior include: - -* The use of sexualized language or imagery, and sexual attention or - advances of any kind -* Trolling, insulting or derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or email - address, without their explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -## Enforcement Responsibilities - -Community leaders are responsible for clarifying and enforcing our standards of -acceptable behavior and will take appropriate and fair corrective action in -response to any behavior that they deem inappropriate, threatening, offensive, -or harmful. - -Community leaders have the right and responsibility to remove, edit, or reject -comments, commits, code, wiki edits, issues, and other contributions that are -not aligned to this Code of Conduct, and will communicate reasons for moderation -decisions when appropriate. - -## Scope - -This Code of Conduct applies within all community spaces, and also applies when -an individual is officially representing the community in public spaces. -Examples of representing our community include using an official e-mail address, -posting via an official social media account, or acting as an appointed -representative at an online or offline event. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported to the community leaders responsible for enforcement as listed on the -github project page. -All complaints will be reviewed and investigated promptly and fairly. - -All community leaders are obligated to respect the privacy and security of the -reporter of any incident. - -## Enforcement Guidelines - -Community leaders will follow these Community Impact Guidelines in determining -the consequences for any action they deem in violation of this Code of Conduct: - -### 1. Correction - -**Community Impact**: Use of inappropriate language or other behavior deemed -unprofessional or unwelcome in the community. - -**Consequence**: A private, written warning from community leaders, providing -clarity around the nature of the violation and an explanation of why the -behavior was inappropriate. A public apology may be requested. - -### 2. Warning - -**Community Impact**: A violation through a single incident or series -of actions. - -**Consequence**: A warning with consequences for continued behavior. No -interaction with the people involved, including unsolicited interaction with -those enforcing the Code of Conduct, for a specified period of time. This -includes avoiding interactions in community spaces as well as external channels -like social media. Violating these terms may lead to a temporary or -permanent ban. - -### 3. Temporary Ban - -**Community Impact**: A serious violation of community standards, including -sustained inappropriate behavior. - -**Consequence**: A temporary ban from any sort of interaction or public -communication with the community for a specified period of time. No public or -private interaction with the people involved, including unsolicited interaction -with those enforcing the Code of Conduct, is allowed during this period. -Violating these terms may lead to a permanent ban. - -### 4. Permanent Ban - -**Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an -individual, or aggression toward or disparagement of classes of individuals. - -**Consequence**: A permanent ban from any sort of public interaction within -the community. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], -version 2.0, available at -https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. - -Community Impact Guidelines were inspired by [Mozilla's code of conduct -enforcement ladder](https://github.com/mozilla/diversity). - -[homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at -https://www.contributor-covenant.org/translations. \ No newline at end of file diff --git a/rust/vendor/ngx-rust/CONTRIBUTING.md b/rust/vendor/ngx-rust/CONTRIBUTING.md deleted file mode 100644 index 5242d62..0000000 --- a/rust/vendor/ngx-rust/CONTRIBUTING.md +++ /dev/null @@ -1,55 +0,0 @@ -# Contributing Guidelines - -The following is a set of guidelines for contributing. We really appreciate that you are considering contributing! - -#### Table Of Contents - -[Ask a Question](#ask-a-question) - -[Contributing](#contributing) - -[Style Guides](#style-guides) - * [Git Style Guide](#git-style-guide) - * [Rust Style Guide](#rust-style-guide) - -[Code of Conduct](CODE_OF_CONDUCT.md) - -## Ask a Question - -Please ask your question on github using discussions. - -## Contributing - -### Report a Bug - -To report a bug, open an issue on GitHub with the label `bug` using the available bug report issue template. Please ensure the issue has not already been reported. - -### Suggest an Enhancement - -To suggest an enhancement, please create an issue on GitHub with the label `enhancement` using the available feature issue template. - -### Open a Pull Request - -* Fork the repo, create a branch, submit a PR when your changes are tested and ready for review. -* Fill in [our pull request template](/.github/PULL_REQUEST_TEMPLATE.md) - -Note: if you’d like to implement a new feature, please consider creating a feature request issue first to start a discussion about the feature. - -## Style Guides - -### Git Style Guide - -* Keep a clean, concise and meaningful git commit history on your branch, rebasing locally and squashing before submitting a PR -* Use the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format when writing a commit message, so that changelogs can be automatically generated -* Follow the guidelines of writing a good commit message as described [here](https://chris.beams.io/posts/git-commit/) and summarised in the next few points - * In the subject line, use the present tense ("Add feature" not "Added feature") - * In the subject line, use the imperative mood ("Move cursor to..." not "Moves cursor to...") - * Limit the subject line to 72 characters or less - * Reference issues and pull requests liberally after the subject line - * Add more detailed description in the body of the git message (`git commit -a` to give you more space and time in your text editor to write a good message instead of `git commit -am`) - -### Rust Style Guide - -* Rust code should be checked in after `cargo fmt` has been run. -* The code style broadly complies with the [official Rust Style Guide](https://doc.rust-lang.org/style-guide/index.html). -* Where feasible, include unit tests. diff --git a/rust/vendor/ngx-rust/Cargo.toml b/rust/vendor/ngx-rust/Cargo.toml deleted file mode 100644 index 1b1bcd2..0000000 --- a/rust/vendor/ngx-rust/Cargo.toml +++ /dev/null @@ -1,84 +0,0 @@ -[workspace] -members = [ - "nginx-src", - "nginx-sys", - "examples", -] - -[workspace.package] -edition = "2024" -license = "Apache-2.0" -homepage = "https://github.com/nginx/ngx-rust" -repository = "https://github.com/nginx/ngx-rust" -rust-version = "1.85.0" - -[workspace.lints.clippy] -alloc_instead_of_core = "warn" -borrow_as_ptr = "warn" -std_instead_of_alloc = "warn" -std_instead_of_core = "warn" - -[workspace.lints.rust] -rust-2024-compatibility = "warn" - -[package] -name = "ngx" -version = "0.5.0" -autoexamples = false -categories = ["api-bindings", "network-programming"] -description = "FFI bindings to NGINX" -include = [ - "/build.rs", - "/src", - "/*.md", - "LICENSE", -] -keywords = ["nginx", "module", "sys"] -edition.workspace = true -license.workspace = true -homepage.workspace = true -repository.workspace = true -rust-version.workspace = true - -[package.metadata.docs.rs] -all-features = true -default-target = "x86_64-unknown-linux-gnu" -targets = [] - -[dependencies] -allocator-api2 = { version = "0.4.0", default-features = false, features = ["fresh-rust"] } -async-task = { version = "4.7.1", optional = true } -lock_api = "0.4.13" -nginx-sys = { path = "nginx-sys", version = "0.5.0"} -pin-project-lite = { version = "0.2.16", optional = true } - -[features] -default = ["std"] -# Enables a minimal async runtime built on top of the NGINX event loop. -async = [ - "alloc", - "dep:async-task", - "dep:pin-project-lite", -] -# Provides APIs that require allocations via the `alloc` crate. -alloc = ["allocator-api2/alloc"] -# Enables serialization support for some of the provided and re-exported types. -serde = [ - "allocator-api2/serde", -] -# Provides APIs that require the standard library. -std = [ - "alloc", - "allocator-api2/std" -] -# Enables the build scripts to build a copy of nginx source and link against it. -vendored = ["nginx-sys/vendored"] - -[badges] -maintenance = { status = "experimental" } - -[dev-dependencies] -tempfile = { version = "3.20.0", default-features = false } - -[lints] -workspace = true diff --git a/rust/vendor/ngx-rust/Containerfile.debian b/rust/vendor/ngx-rust/Containerfile.debian deleted file mode 100644 index 7d8bca4..0000000 --- a/rust/vendor/ngx-rust/Containerfile.debian +++ /dev/null @@ -1,16 +0,0 @@ -FROM rust:slim-bullseye - -RUN set -eux \ - export DEBIAN_FRONTEND=noninteractive; \ - apt-get -qq update; \ - apt-get -qq install --yes --no-install-recommends --no-install-suggests \ - libclang-dev \ - libssl-dev \ - pkg-config \ - git \ - grep \ - gawk \ - gnupg2 \ - sed \ - make; \ - git config --global --add safe.directory /project \ No newline at end of file diff --git a/rust/vendor/ngx-rust/Dockerfile b/rust/vendor/ngx-rust/Dockerfile deleted file mode 100644 index b08598d..0000000 --- a/rust/vendor/ngx-rust/Dockerfile +++ /dev/null @@ -1,51 +0,0 @@ -ARG NGX_VERSION=1.30.4 -# NGINX official images are available for one specific release of Debian, -# e.g `nginx:1.30.4-trixie`. Please see the list of tags on Docker Hub -# if you need to change NGX_VERSION. -ARG DEBIAN_RELEASE=trixie - -# --- builder: build all examples -FROM rust:slim-${DEBIAN_RELEASE} AS build -ARG NGX_VERSION -ARG NGX_CONFIGURE_ARGS= -WORKDIR /project -RUN --mount=type=cache,target=/var/cache/apt < /dev/null || command -v grep 2> /dev/null) -SED ?= $(shell command -v gsed 2> /dev/null || command -v sed 2> /dev/null) -AWK ?= $(shell command -v gawk 2> /dev/null || command -v awk 2> /dev/null) -VERSION ?= $(shell $(GREP) -Po '^version\s+=\s+"\K.*?(?=")' $(CURDIR)/Cargo.toml) -CARGO ?= cargo -DOCKER ?= docker -DOCKER_BUILD_FLAGS ?= --load -COMMITSAR_DOCKER := $(DOCKER) run --tty --rm --workdir /src -v "$(CURDIR):/src" aevea/commitsar -COMMITSAR ?= $(shell command -v commitsar 2> /dev/null) -PROJECT_NAME ?= ngx-rust -GITHUB_REPOSITORY ?= nginx/$(PROJECT_NAME) -SRC_REPO := https://github.com/$(GITHUB_REPOSITORY) - -RELEASE_BUILD_FLAGS ?= --quiet --release - -Q = $(if $(filter 1,$V),,@) -M = $(shell printf "\033[34;1m▶\033[0m") - -UNAME_S := $(shell uname -s) -ifeq ($(UNAME_S),Linux) - FEATURES += --features=linux -endif - -# Use docker based commitsar if it isn't in the path -ifeq ($(COMMITSAR),) - COMMITSAR = $(COMMITSAR_DOCKER) -endif - -.PHONY: help -help: - @$(GREP) --no-filename -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ - $(AWK) 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-28s\033[0m %s\n", $$1, $$2}' | sort - -.PHONY: clean -clean: clean-cache; $(info $(M) cleaning...) @ ## Cleanup everything - $Q rm -rf $(CURDIR)/target - $Q $(CARGO) clean - -.PHONY: clean-cache -clean-cache: ## Remove all cached dependencies and build artifacts - $(Q) rm -rf $(CACHE_DIR) - -.PHONY: commitsar -commitsar: ## Run git commit linter - $Q $(info $(M) running commitsar...) - $(COMMITSAR) - -target: - $Q mkdir -p $@ - -.PHONY: debug -debug: target/debug ## Build current platform target in debug mode - -target/debug: - $Q echo "$(M) building in debug mode for the current platform" - $Q $(CARGO) build --quiet - -.PHONY: release -release: target/release ## Build current platform target in release mode - -target/release: - $Q echo "$(M) building in release mode for the current platform" - $Q $(CARGO) build $(RELEASE_BUILD_FLAGS) - -.PHONY: test -test: ## Run tests - $Q $(CARGO) test - -.PHONY: format -format: ## Run rustfmt - $Q $(CARGO) fmt - -.PHONY: lint -lint: ## Run clippy - $Q $(CARGO) clippy - -.PHONY: examples-debug -examples-debug: - $Q echo "$(M) building all examples as debug" - $Q $(CARGO) build --quiet --package=examples --examples $(FEATURES) - -include $(CURDIR)/build/container.mk -include $(CURDIR)/build/release.mk -include $(CURDIR)/build/github.mk diff --git a/rust/vendor/ngx-rust/LICENSE b/rust/vendor/ngx-rust/LICENSE deleted file mode 100644 index 98f7217..0000000 --- a/rust/vendor/ngx-rust/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ -Apache License -Version 2.0, January 2004 -http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - -"License" shall mean the terms and conditions for use, reproduction, -and distribution as defined by Sections 1 through 9 of this document. - -"Licensor" shall mean the copyright owner or entity authorized by -the copyright owner that is granting the License. - -"Legal Entity" shall mean the union of the acting entity and all -other entities that control, are controlled by, or are under common -control with that entity. For the purposes of this definition, -"control" means (i) the power, direct or indirect, to cause the -direction or management of such entity, whether by contract or -otherwise, or (ii) ownership of fifty percent (50%) or more of the -outstanding shares, or (iii) beneficial ownership of such entity. - -"You" (or "Your") shall mean an individual or Legal Entity -exercising permissions granted by this License. - -"Source" form shall mean the preferred form for making modifications, -including but not limited to software source code, documentation -source, and configuration files. - -"Object" form shall mean any form resulting from mechanical -transformation or translation of a Source form, including but -not limited to compiled object code, generated documentation, -and conversions to other media types. - -"Work" shall mean the work of authorship, whether in Source or -Object form, made available under the License, as indicated by a -copyright notice that is included in or attached to the work -(an example is provided in the Appendix below). - -"Derivative Works" shall mean any work, whether in Source or Object -form, that is based on (or derived from) the Work and for which the -editorial revisions, annotations, elaborations, or other modifications -represent, as a whole, an original work of authorship. For the purposes -of this License, Derivative Works shall not include works that remain -separable from, or merely link (or bind by name) to the interfaces of, -the Work and Derivative Works thereof. - -"Contribution" shall mean any work of authorship, including -the original version of the Work and any modifications or additions -to that Work or Derivative Works thereof, that is intentionally -submitted to Licensor for inclusion in the Work by the copyright owner -or by an individual or Legal Entity authorized to submit on behalf of -the copyright owner. For the purposes of this definition, "submitted" -means any form of electronic, verbal, or written communication sent -to the Licensor or its representatives, including but not limited to -communication on electronic mailing lists, source code control systems, -and issue tracking systems that are managed by, or on behalf of, the -Licensor for the purpose of discussing and improving the Work, but -excluding communication that is conspicuously marked or otherwise -designated in writing by the copyright owner as "Not a Contribution." - -"Contributor" shall mean Licensor and any individual or Legal Entity -on behalf of whom a Contribution has been received by Licensor and -subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of -this License, each Contributor hereby grants to You a perpetual, -worldwide, non-exclusive, no-charge, royalty-free, irrevocable -copyright license to reproduce, prepare Derivative Works of, -publicly display, publicly perform, sublicense, and distribute the -Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of -this License, each Contributor hereby grants to You a perpetual, -worldwide, non-exclusive, no-charge, royalty-free, irrevocable -(except as stated in this section) patent license to make, have made, -use, offer to sell, sell, import, and otherwise transfer the Work, -where such license applies only to those patent claims licensable -by such Contributor that are necessarily infringed by their -Contribution(s) alone or by combination of their Contribution(s) -with the Work to which such Contribution(s) was submitted. If You -institute patent litigation against any entity (including a -cross-claim or counterclaim in a lawsuit) alleging that the Work -or a Contribution incorporated within the Work constitutes direct -or contributory patent infringement, then any patent licenses -granted to You under this License for that Work shall terminate -as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the -Work or Derivative Works thereof in any medium, with or without -modifications, and in Source or Object form, provided that You -meet the following conditions: - -(a) You must give any other recipients of the Work or -Derivative Works a copy of this License; and - -(b) You must cause any modified files to carry prominent notices -stating that You changed the files; and - -(c) You must retain, in the Source form of any Derivative Works -that You distribute, all copyright, patent, trademark, and -attribution notices from the Source form of the Work, -excluding those notices that do not pertain to any part of -the Derivative Works; and - -(d) If the Work includes a "NOTICE" text file as part of its -distribution, then any Derivative Works that You distribute must -include a readable copy of the attribution notices contained -within such NOTICE file, excluding those notices that do not -pertain to any part of the Derivative Works, in at least one -of the following places: within a NOTICE text file distributed -as part of the Derivative Works; within the Source form or -documentation, if provided along with the Derivative Works; or, -within a display generated by the Derivative Works, if and -wherever such third-party notices normally appear. The contents -of the NOTICE file are for informational purposes only and -do not modify the License. You may add Your own attribution -notices within Derivative Works that You distribute, alongside -or as an addendum to the NOTICE text from the Work, provided -that such additional attribution notices cannot be construed -as modifying the License. - -You may add Your own copyright statement to Your modifications and -may provide additional or different license terms and conditions -for use, reproduction, or distribution of Your modifications, or -for any such Derivative Works as a whole, provided Your use, -reproduction, and distribution of the Work otherwise complies with -the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, -any Contribution intentionally submitted for inclusion in the Work -by You to the Licensor shall be under the terms and conditions of -this License, without any additional terms or conditions. -Notwithstanding the above, nothing herein shall supersede or modify -the terms of any separate license agreement you may have executed -with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade -names, trademarks, service marks, or product names of the Licensor, -except as required for reasonable and customary use in describing the -origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or -agreed to in writing, Licensor provides the Work (and each -Contributor provides its Contributions) on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -implied, including, without limitation, any warranties or conditions -of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A -PARTICULAR PURPOSE. You are solely responsible for determining the -appropriateness of using or redistributing the Work and assume any -risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, -whether in tort (including negligence), contract, or otherwise, -unless required by applicable law (such as deliberate and grossly -negligent acts) or agreed to in writing, shall any Contributor be -liable to You for damages, including any direct, indirect, special, -incidental, or consequential damages of any character arising as a -result of this License or out of the use or inability to use the -Work (including but not limited to damages for loss of goodwill, -work stoppage, computer failure or malfunction, or any and all -other commercial damages or losses), even if such Contributor -has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing -the Work or Derivative Works thereof, You may choose to offer, -and charge a fee for, acceptance of support, warranty, indemnity, -or other liability obligations and/or rights consistent with this -License. However, in accepting such obligations, You may act only -on Your own behalf and on Your sole responsibility, not on behalf -of any other Contributor, and only if You agree to indemnify, -defend, and hold each Contributor harmless for any liability -incurred by, or claims asserted against, such Contributor by reason -of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work. - -To apply the Apache License to your work, attach the following -boilerplate notice, with the fields enclosed by brackets "{}" -replaced with your own identifying information. (Don't include -the brackets!) The text should be enclosed in the appropriate -comment syntax for the file format. We also recommend that a -file or class name and description of purpose be included on the -same "printed page" as the copyright notice for easier -identification within third-party archives. - -Copyright 2016 Nginx, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/rust/vendor/ngx-rust/README.md b/rust/vendor/ngx-rust/README.md deleted file mode 100644 index 7175f67..0000000 --- a/rust/vendor/ngx-rust/README.md +++ /dev/null @@ -1,256 +0,0 @@ -[![Rust](https://github.com/nginx/ngx-rust/actions/workflows/ci.yaml/badge.svg)](https://github.com/nginx/ngx-rust/actions/workflows/ci.yaml) -[![crates.io](https://img.shields.io/crates/v/ngx.svg)](https://crates.io/crates/ngx) -[![docs.rs](https://img.shields.io/docsrs/ngx?label=docs(ngx))](https://docs.rs/ngx) -[![docs.rs](https://img.shields.io/docsrs/nginx-sys?label=docs(nginx-sys))](https://docs.rs/nginx-sys) -[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip) -[![Community Support](https://badgen.net/badge/support/community/cyan?icon=awesome)](https://github.com/nginx/ngx-rust/discussions) - - - -# Rust bindings for NGINX - -The `ngx` crate provides a high-level Rust interface for the [NGINX] C APIs, -allowing creation of NGINX dynamic modules completely in Rust. - -[NGINX]: https://nginx.org/ - -## Project status - -This project is in active development. It is stable enough to be used in our -own products, but the APIs are not stabilized and breaking changes are expected. - -## Build - -NGINX modules can be built against a particular version of NGINX. The following -environment variables can be used to specify the NGINX build to use: - -- `NGINX_BUILD_DIR` - absolute path to the NGINX build directory. - Usually it's `objs` under the source directory, but can be changed with the - `--builddir=` argument of the configure script. - -- `NGINX_SOURCE_DIR` - absolute path to the NGINX source directory, configured - with the [`configure`](https://nginx.org/en/docs/configure.html) command. - -Only the `./configure` step of the NGINX build is mandatory because bindings -generator don't depend on any of the artifacts generated by `make`. - -For example, this is how you would compile the [examples](#examples) using a -specific version of NGINX: - -```sh -NGINX_BUILD_DIR=/path/to/nginx-1.30.4/objs cargo build --package=examples --examples -``` - -### Building with the NGINX build script - -You can build your Rust-based module as a part of the NGINX build process by -providing a `config` script implementation and passing the `--add-module`/ -`--add-dynamic-module` options to the configure script. - -See the following example integration scripts: [`examples/config`] and -[`examples/config.make`]. - -[`examples/config`]: https://github.com/bavshin-f5/ngx-rust/blob/main/examples/config -[`examples/config.make`]: https://github.com/bavshin-f5/ngx-rust/blob/main/examples/config.make - -### Building with vendored NGINX sources - -It is possible to build module with a vendored copy of the NGINX sources -provided in the `nginx-src` crate by enabling feature `vendored`: - -By default, this will use the latest stable release of NGINX and require -system-wide installation of build dependencies (OpenSSL, PCRE2, Zlib). - -The behavior of vendored builds can be customized with environment variables, -as documented in the [nginx-src] crate README. - -**NOTE**: We recommend to build the module binaries against the exact source and -configuration of the NGINX build that you are planning to use in production, -and that is unlikely to be possible with the vendored source. - -`configure` arguments alter the APIs and the symbols visible to the Rust code, -and some OS distributions are known to ship nginx packages with API-breaking -patches applied. - -[nginx-src]: https://docs.rs/nginx-src/ - -## Cargo features - -- `alloc` - **Enabled** by default. This provides APIs that require allocations - via the `alloc` crate. -- `async` - Enables a minimal async runtime built on top of the NGINX event loop. -- `serde` - Enables serialization support for some of the provided and - re-exported types. -- `std` - **Enabled** by default. This provides APIs that require the standard - library. -- `vendored`: Enables the build scripts to build a copy of nginx source and link - against it. See the [nginx-src] crate documentation for additional details. - -## Dependencies - -The following dependencies are required to build a Rust NGINX module on Linux -or BSD platform: - -- NGINX build dependencies: C compiler, make, OpenSSL, PCRE2, and Zlib. -- Rust toolchain (1.85.0 or later) -- [libclang] for rust-bindgen - -The installation process and the package names are system-dependent. Please, -consult the documentation for your distribution. - -Note that on some systems you will need `-devel` or `-dev` versions of the -packages. - -For example, [Dockerfile] contains the installation commands for Debian Linux. - -[Dockerfile]: https://github.com/nginx/ngx-rust/blob/main/Dockerfile -[libclang]: https://rust-lang.github.io/rust-bindgen/requirements.html - -### macOS dependencies - -In order to use the optional GNU make build process on macOS, you will need -to install additional tools. This can be done via [homebrew](https://brew.sh/) -with the following command: - -```sh -brew install make openssl grep -``` - -Additionally, you may need to set up LLVM and clang. Typically, this is done -as follows: - -```sh -# make sure xcode tools are installed -xcode-select --install -# instal llvm -brew install --with-toolchain llvm -``` - - - -### Examples - -Example modules are available in [examples] folder. -You can use `cargo build --package=examples --examples` to build these examples. -After compilation, the modules can be found in the path `target/debug/examples/` -(with the `.so` file extension for Linux or `.dylib` for macOS). -Add `--features=linux` to build Linux specific modules. - -**NOTE**: adding the "linux" feature on macOS will cause a build failure. - -For example (all examples plus linux specific): - -```sh -cargo build --package=examples --examples --features=linux -``` - -[examples]: https://github.com/nginx/ngx-rust/tree/main/examples - -### Docker - -We provide a multistage [Dockerfile]: - -```sh -# build all dynamic modules examples and specify NGINX version to use -docker buildx build --build-arg NGX_VERSION=1.29.1 -t ngx-rust . - -# start NGINX using [curl](examples/curl.conf) module example: -docker run --rm -d -p 8000:8000 ngx-rust nginx -c examples/curl.conf - -# test it - you should see 403 Forbidden -curl http://127.0.0.1:8000 -v -H "user-agent: curl" - -# test it - you should see 404 Not Found -curl http://127.0.0.1:8000 -v -H "user-agent: foo" -``` - -## Usage - -A complete module example using the SDK can be found [here](examples/curl.rs). -You can build it with `cargo build --package=examples --example=curl` then set -up NGINX to use it, as shown below: - -```nginx -daemon off; -error_log stderr debug; - -load_module modules/libcurl.so; - -events { -} - -http { - server { - listen 8000; - server_name localhost; - - access_log /dev/stdout; - - location / { - root /srv/http; - # ... Other config stuff ... - - curl on; - } - } -} -``` - -## Support - -This SDK is currently unstable. Right now, our primary goal is collect feedback -and stabilize it before offering support. -Feel free to [contribute](CONTRIBUTING.md) by creating issues, PRs, or github -discussions. - -Currently, the supported platforms are: - -- Darwin (macOS) -- FreeBSD -- Linux - -See the Rust toolchain [platform support] document for supported versions and -architectures. - -[platform support]: https://doc.rust-lang.org/rustc/platform-support.html - -### Tested OS and platforms - -In addition, we verified that the modules based on this SDK can be built and -work on the following platforms. - -- Illumos -- NetBSD 9-10 -- OpenBSD 7 -- Solaris 11.4 -- Windows 10+/Windows Server 2022+ - -## Roadmap - -If you have ideas for releases in the future, please suggest them in the github -discussions. - -## Contributing - -We welcome pull requests and issues! - -Please refer to the [Contributing Guidelines](CONTRIBUTING.md) when doing a PR. - -## Authors and acknowledgment - -This project uses some great work from: - -- [dcoles/nginx-rs](https://github.com/dcoles/nginx-rs), -- [arvancloud/nginx-rs](https://github.com/arvancloud/nginx-rs). - -## Projects using the SDK - -- [ngx-strict-sni](https://github.com/JyJyJcr/ngx-strict-sni) - Strict SNI validator for Nginx -- [nginx-acme](https://github.com/nginx/nginx-acme) - ACMEv2 client implementation - -## License - -All code in this repository is licensed under the -[Apache License, Version 2.0](./LICENSE). - -© [F5, Inc.](https://www.f5.com/) diff --git a/rust/vendor/ngx-rust/SECURITY.md b/rust/vendor/ngx-rust/SECURITY.md deleted file mode 100644 index dd2be6b..0000000 --- a/rust/vendor/ngx-rust/SECURITY.md +++ /dev/null @@ -1,22 +0,0 @@ -# Security Policy - -## General Guidance - -We advise users to run the most recent release of the NGINX Rust Module SDK, and we issue software updates to the -most recent release. - -## Support - -The NGINX Rust Module SDK is not officially supported. - -## Reporting a Vulnerability - -The F5 Security Incident Response Team (F5 SIRT) has an email alias that makes it easy to report potential security -vulnerabilities. - -- If you’re an F5 customer with an active support contract, please contact - [F5 Technical Support](https://www.f5.com/services/support). -- If you aren’t an F5 customer, please report any potential or current instances of security vulnerabilities with any - F5 product to the F5 Security Incident Response Team at F5SIRT@f5.com - -For more information visit https://www.f5.com/services/support/report-a-vulnerability diff --git a/rust/vendor/ngx-rust/build.rs b/rust/vendor/ngx-rust/build.rs deleted file mode 100644 index 3ffcf94..0000000 --- a/rust/vendor/ngx-rust/build.rs +++ /dev/null @@ -1,67 +0,0 @@ -/// Example buildscript for an nginx module. -/// -/// Due to the limitations of cargo[1], this buildscript _requires_ adding `nginx-sys` to the -/// direct dependencies of your crate. -/// -/// [1]: https://github.com/rust-lang/cargo/issues/3544 -fn main() { - // Generate `ngx_os` and `ngx_feature` cfg values - - // Specify acceptable values for `ngx_feature` - println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES_CHECK"); - println!( - "cargo::rustc-check-cfg=cfg(ngx_feature, values({}))", - std::env::var("DEP_NGINX_FEATURES_CHECK").unwrap_or("any()".to_string()) - ); - // Read feature flags detected by nginx-sys and pass to the compiler. - println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES"); - if let Ok(features) = std::env::var("DEP_NGINX_FEATURES") { - for feature in features.split(',').map(str::trim) { - println!("cargo::rustc-cfg=ngx_feature=\"{feature}\""); - } - } - - // Specify acceptable values for `ngx_os` - println!("cargo::rerun-if-env-changed=DEP_NGINX_OS_CHECK"); - println!( - "cargo::rustc-check-cfg=cfg(ngx_os, values({}))", - std::env::var("DEP_NGINX_OS_CHECK").unwrap_or("any()".to_string()) - ); - // Read operating system detected by nginx-sys and pass to the compiler. - println!("cargo::rerun-if-env-changed=DEP_NGINX_OS"); - if let Ok(os) = std::env::var("DEP_NGINX_OS") { - println!("cargo::rustc-cfg=ngx_os=\"{os}\""); - } - - // Generate cfg values for version checks - const VERSION_CHECKS: &[(u64, &str)] = &[ - // - (1_021_001, "nginx1_21_1"), - (1_025_001, "nginx1_25_1"), - ]; - VERSION_CHECKS.iter().for_each(|check| println!("cargo::rustc-check-cfg=cfg({})", check.1)); - - println!("cargo::rerun-if-env-changed=DEP_NGINX_VERSION_NUMBER"); - if let Ok(version) = std::env::var("DEP_NGINX_VERSION_NUMBER") { - let version: u64 = version.parse().unwrap(); - - for check in VERSION_CHECKS { - if version >= check.0 { - println!("cargo::rustc-cfg={}", check.1); - } - } - } - - // Pass build directory to the tests - println!("cargo::rerun-if-env-changed=DEP_NGINX_BUILD_DIR"); - if let Ok(build_dir) = std::env::var("DEP_NGINX_BUILD_DIR") { - println!("cargo::rustc-env=DEP_NGINX_BUILD_DIR={build_dir}"); - } - - // Generate required compiler flags - if cfg!(target_os = "macos") { - // https://stackoverflow.com/questions/28124221/error-linking-with-cc-failed-exit-code-1 - println!("cargo::rustc-link-arg=-undefined"); - println!("cargo::rustc-link-arg=dynamic_lookup"); - } -} diff --git a/rust/vendor/ngx-rust/deny.toml b/rust/vendor/ngx-rust/deny.toml deleted file mode 100644 index a2b7132..0000000 --- a/rust/vendor/ngx-rust/deny.toml +++ /dev/null @@ -1,27 +0,0 @@ -[bans] -multiple-versions = "warn" - -[graph] -all-features = true - -[licenses] -allow = [ - "Apache-2.0", - "BSD-2-Clause", - "BSD-3-Clause", - "ISC", - "MIT", - "Unicode-3.0", -] -confidence-threshold = 0.8 - -[[licenses.exceptions]] -crate = "webpki-roots" -allow = ["CDLA-Permissive-2.0", "MPL-2.0"] - -[[licenses.clarify]] -crate = "ring" -expression = "MIT AND ISC AND OpenSSL" -license-files = [ - { path = "LICENSE", hash = 0xbd0eed23 } -] diff --git a/rust/vendor/ngx-rust/examples/Cargo.toml b/rust/vendor/ngx-rust/examples/Cargo.toml deleted file mode 100644 index 5419075..0000000 --- a/rust/vendor/ngx-rust/examples/Cargo.toml +++ /dev/null @@ -1,70 +0,0 @@ -[package] -name = "examples" -version = "0.0.0" -publish = false -edition.workspace = true -license.workspace = true -homepage.workspace = true -repository.workspace = true -rust-version.workspace = true -# https://github.com/dependabot/dependabot-core/issues/1156 -autobins = false - -build = "../build.rs" - -[dependencies] -nginx-sys = { path = "../nginx-sys/" } -ngx = { path = "../", default-features = false, features = ["std"] } - -[dev-dependencies] -aws-sign-v4 = "0.3.0" -chrono = "0.4.27" -http = "1.1.0" -# use unicode-rs idna backend for lower MSRV and faster builds -idna_adapter = "=1.1.0" -libc = "0.2.168" -tokio = { version = "1.33.0", features = ["full"] } - -[[example]] -name = "curl" -path = "curl.rs" -crate-type = ["cdylib"] - -[[example]] -name = "awssig" -path = "awssig.rs" -crate-type = ["cdylib"] - -[[example]] -name = "httporigdst" -path = "httporigdst.rs" -crate-type = ["cdylib"] -required-features = ["linux"] - -[[example]] -name = "upstream" -path = "upstream.rs" -crate-type = ["cdylib"] - -[[example]] -name = "async" -path = "async.rs" -crate-type = ["cdylib"] - -[[example]] -name = "shared_dict" -path = "shared_dict.rs" -crate-type = ["cdylib"] - -[features] -default = ["export-modules", "ngx/vendored"] -# Generate `ngx_modules` table with module exports -# The exports table is required for building loadable modules with --crate-type cdylib -# outside of the NGINX buildsystem. However, cargo currently does not detect -# this configuration automatically. -# See https://github.com/rust-lang/rust/issues/20267 -export-modules = [] -linux = [] - -[lints] -workspace = true diff --git a/rust/vendor/ngx-rust/examples/README.md b/rust/vendor/ngx-rust/examples/README.md deleted file mode 100644 index e40e4db..0000000 --- a/rust/vendor/ngx-rust/examples/README.md +++ /dev/null @@ -1,226 +0,0 @@ -- [Examples](#examples) - - [CURL](#curl) - - [AWSSIG](#awssig) - - [HTTPORIGDST - NGINX Destination IP recovery module for HTTP](#httporigdst----nginx-destination-ip-recovery-module-for-http) - - [Dependencies](#dependencies) - - [Example Configuration](#example-configuration) - - [HTTP](#http) - - [Embedded Variables](#embedded-variables) - - [Usage](#usage) - - [Caveats](#caveats) - - -# Examples -This crate provides a couple of example using [ngx](https://crates.io/crates/ngx) crate: - -- [awssig.rs](./awssig.rs) - An example of NGINX dynamic module that can sign GET request using AWS Signature v4. -- [curl](./curl.rs) - An example of the Access Phase NGINX dynamic module that blocks HTTP requests if `user-agent` header starts with `curl`. -- [httporigdst](./httporigdst.rs) - A dynamic module recovers the original IP address and port number of the destination packet. -- [upstream](./upstream.rs) - A dynamic module demonstrating the setup code to write an upstream filter or load balancer. - -To build all these examples simply run: - -``` -cargo build --package=examples --examples -``` - - -## CURL - -This module demonstrates how to create a minimal dynamic module with `http_request_handler`, that checks for User-Agent headers and returns status code 403 if UA starts with `curl`, if a module is disabled then uses `core::Status::NGX_DECLINED` to indicate the operation is rejected, for example, because it is disabled in the configuration (`curl off`). Additionally, it demonstrates how to write a defective parser. - -An example of nginx configuration file that uses that module can be found at [curl.conf](./curl.conf). - -How to build and run in a [Docker](../Dockerfile) container curl example: -``` -# build all dynamic modules examples and specify NGINX version to use -docker buildx build --build-arg NGX_VERSION=1.23.3 -t ngx-rust . - -# start NGINX using curl.conf module example: -docker run --rm -d -p 8000:8000 ngx-rust nginx -c examples/curl.conf - -# test it - you should see 403 Forbidden -curl http://127.0.0.1:8000 -v -H "user-agent: curl" - - -# test it - you should see 404 Not Found -curl http://127.0.0.1:8000 -v -H "user-agent: foo" -``` - -## AWSSIG - -This module uses [NGX_HTTP_PRECONTENT_PHASE](https://nginx.org/en/docs/dev/development_guide.html#http_phases) and provides examples, of how to use external dependency and manipulate HTTP headers before sending client requests upstream. - -An example of nginx configuration file that uses that module can be found at [awssig.conf](./awssig.conf). - -## HTTPORIGDST - NGINX Destination IP recovery module for HTTP - -This dynamic module recovers the original IP address and port number of the destination packet. It is useful, for example, with container sidecars where all outgoing traffic is redirected to a separate container with iptables before reaching the target. - -This module can only be built with the "linux" feature enabled, and will only successfully build on a Linux OS. - -### Dependencies -This module uses the Rust crate libc and Linux **getsockopt** socket API. - -### Example Configuration -#### HTTP - -```nginx configuration -load_module "modules/libhttporigdst.so"; - -http { - server { - # use iptables to capture all outgoing traffic and REDIRECT - # to listening port 15501 - listen 15501; - - # binding variables provided by module will lazily activate it - # and store a context - # variables can be used in config - location / { - # Return if no backend is available or proxy_pass - # return 200 "recv'd: $server_addr:$server_port\n\nproxy_pass http://$server_orig_addr:$server_orig_port\n"; - proxy_pass http://$server_orig_addr:$server_orig_port; - } - } -} -``` - -### Embedded Variables - -The following embedded variables are provided: - -* **server_orig_addr** - * Original IP address -* **server_orig_port** - * Original port - -### Usage - -1. Clone the git repository. - ``` - git clone git@github.com:nginx/ngx-rust.git - ``` - -2. Compile the module from the cloned repo. - ``` - cd ${CLONED_DIRECTORY}/ngx-rust - cargo build --package=examples --example=httporigdst --features=linux - ``` - -3. Copy the shared object to the modules directory, /etc/nginx/modules. - ``` - cp ./target/debug/examples/libhttporigdst.so /etc/nginx/modules - ``` - -4. Add the `load_module` directive to your configuration. - ``` - load_module "modules/libhttporigdst.so"; - ``` - -5. Reload NGINX. - ``` - nginx -t && nginx -s reload - ``` - -6. Redirect traffic outbound. - ``` - iptables -t nat -N NGINX_REDIRECT && \ - iptables -t nat -A NGINX_REDIRECT -p tcp -j REDIRECT --to-port 15501 --dport 15001 && \ - iptables -t nat -N NGINX_OUTPUT && \ - iptables -t nat -A OUTPUT -p tcp -j NGINX_OUTPUT && \ - iptables -t nat -A NGINX_OUTPUT -j NGINX_REDIRECT - ``` - -7. Redirect traffic inbound. - ``` - iptables -t nat -N NGINX_IN_REDIRECT && \ - iptables -t nat -A NGINX_IN_REDIRECT -p tcp -j REDIRECT --to-port 15501 --dport 15001 && \ - iptables -t nat -N NGINX_INBOUND && \ - iptables -t nat -A PREROUTING -p tcp -j NGINX_INBOUND && \ - iptables -t nat -A NGINX_INBOUND -p tcp -j NGINX_IN_REDIRECT - ``` - -8. Test with `curl` (this step assumes you've uncommented the return directive). - ``` - curl --output - ${LISTEN_IP}:15001 - recv'd: ${LISTEN_IP}:15501 - - proxy_pass http://${LISTEN_IP}:15001 - ``` -### Caveats - -This module only supports IPv4. - -## UPSTREAM - Example upstream / load balancing module for HTTP - -This module simply proxies requests through a custom load balancer to the previously configured balancer. This is for demonstration purposes only. As a module writer, you can start with this structure and adjust to your needs, then implement the proper algorithm for your usage. - -The module replaces the `peer` callback functions with its own, logs, and then calls through to the originally saved `peer` functions. This may look confusing at first, but rest assured, it's intentionally not implementing an algorithm of its own. - -### Attributions - -This module was converted from https://github.com/gabihodoroaga/nginx-upstream-module and also highly inspired by the same techniques used in NGINX source: `ngx_http_upstream_keepalive_module.c`. - -### Example Configuration -#### HTTP - -```nginx configuration -load_module "modules/upstream.so" - -http { - upstream backend { - server localhost:15501; - custom 32; - } - - server { - listen 15500; - server_name _; - - location / { - proxy_pass http://backend; - } - } - - server { - listen 15501; - - location / { - return 418; - } - } -} -``` - -### Usage - -1. Clone the git repository. - ``` - git clone git@github.com:nginx/ngx-rust.git - ``` - -2. Compile the module from the cloned repo. - ``` - cd ${CLONED_DIRECTORY}/ngx-rust - cargo build --package=examples --example=upstream - ``` - -3. Copy the shared object to the modules directory, /etc/nginx/modules. - ``` - cp ./target/debug/examples/libupstream.so /etc/nginx/modules - ``` - -4. Add the `load_module` directive to your configuration. - ``` - load_module "modules/libupstream.so"; - ``` - -5. Add the example `server` and `upstream` block from the example above. - -6. Reload NGINX. - ``` - nginx -t && nginx -s reload - ``` - -7. Test with `curl`. Traffic should pass to your listener on port 8081 (this could be another NGINX server for example). With debug logging enabled you should notice the upstream log messages (see the source code for log examples, prefixed with "CUSTOM UPSTREAM"). diff --git a/rust/vendor/ngx-rust/examples/async.conf b/rust/vendor/ngx-rust/examples/async.conf deleted file mode 100644 index d96876e..0000000 --- a/rust/vendor/ngx-rust/examples/async.conf +++ /dev/null @@ -1,24 +0,0 @@ -daemon off; -master_process off; -# worker_processes 1; - -load_module modules/libasync.so; -error_log error.log debug; - -events { } - -http { - server { - listen *:8000; - server_name localhost; - location / { - root html; - index index.html index.htm; - async on; - } - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root html; - } - } -} diff --git a/rust/vendor/ngx-rust/examples/async.rs b/rust/vendor/ngx-rust/examples/async.rs deleted file mode 100644 index c1f9620..0000000 --- a/rust/vendor/ngx-rust/examples/async.rs +++ /dev/null @@ -1,237 +0,0 @@ -extern crate alloc; - -use alloc::sync::Arc; -use core::ffi::{c_char, c_void}; -use core::mem; -use core::ptr; -use core::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; -use core::time::Duration; -use std::sync::OnceLock; -use std::time::Instant; - -use ngx::core::Status; -use ngx::ffi::{ - NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_LOG_EMERG, - ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_module_t, ngx_int_t, - ngx_module_t, ngx_post_event, ngx_posted_events, ngx_posted_next_events, ngx_str_t, ngx_uint_t, -}; -use ngx::http::{self, HttpModule, HttpModuleLocationConf, HttpRequestHandler, MergeConfigError}; -use ngx::{ngx_conf_log_error, ngx_log_debug_http, ngx_string}; -use tokio::runtime::Runtime; - -struct Module; - -impl http::HttpModule for Module { - fn module() -> &'static ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_async_module) } - } - - unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - // SAFETY: this function is called with non-NULL cf always - let cf = unsafe { &mut *cf }; - http::add_phase_handler::(cf) - .map_or(Status::NGX_ERROR, |_| Status::NGX_OK) - .into() - } -} - -#[derive(Debug, Default)] -struct ModuleConfig { - enable: bool, -} - -unsafe impl HttpModuleLocationConf for Module { - type LocationConf = ModuleConfig; -} - -static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [ - ngx_command_t { - name: ngx_string!("async"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_async_commands_set_enable), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t::empty(), -]; - -static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(Module::preconfiguration), - postconfiguration: Some(Module::postconfiguration), - create_main_conf: None, - init_main_conf: None, - create_srv_conf: None, - merge_srv_conf: None, - create_loc_conf: Some(Module::create_loc_conf), - merge_loc_conf: Some(Module::merge_loc_conf), -}; - -// Generate the `ngx_modules` table with exported modules. -// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_async_module); - -#[used] -#[allow(non_upper_case_globals)] -#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))] -pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_ASYNC_MODULE_CTX as _, - commands: unsafe { &raw mut NGX_HTTP_ASYNC_COMMANDS[0] }, - type_: NGX_HTTP_MODULE as _, - ..ngx_module_t::default() -}; - -impl http::Merge for ModuleConfig { - fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> { - if prev.enable { - self.enable = true; - }; - Ok(()) - } -} - -unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) { - let ctx = ngx::ngx_container_of!(event, RequestCTX, event); - let c: *mut ngx_connection_t = unsafe { (*event).data.cast() }; - - if unsafe { (*ctx).done.load(Ordering::Relaxed) } { - // Triggering async_access_handler again - unsafe { ngx_post_event((*c).write, &raw mut ngx_posted_events) }; - } else { - // this doesn't have have good performance but works as a simple thread-safe example and - // doesn't causes segfault. The best method that provides both thread-safety and - // performance requires an nginx patch. - unsafe { ngx_post_event(event, &raw mut ngx_posted_next_events) }; - } -} - -struct RequestCTX { - done: Arc, - event: ngx_event_t, - task: Option>, -} - -impl Default for RequestCTX { - fn default() -> Self { - Self { - done: AtomicBool::new(false).into(), - event: unsafe { mem::zeroed() }, - task: Default::default(), - } - } -} - -impl Drop for RequestCTX { - fn drop(&mut self) { - if let Some(handle) = self.task.take() { - handle.abort(); - } - - if self.event.posted() != 0 { - unsafe { ngx::ffi::ngx_delete_posted_event(&raw mut self.event) }; - } - } -} - -struct AsyncAccessHandler; - -impl HttpRequestHandler for AsyncAccessHandler { - const PHASE: ngx::http::HttpPhase = ngx::http::HttpPhase::Access; - type Output = Status; - - fn handler(request: &mut http::Request) -> Self::Output { - let co = Module::location_conf(request).expect("module config is none"); - - ngx_log_debug_http!(request, "async module enabled: {}", co.enable); - - if !co.enable { - return Status::NGX_DECLINED; - } - - if let Some(ctx) = request.get_module_ctx::(Module::module()) { - if !ctx.done.load(Ordering::Relaxed) { - return Status::NGX_AGAIN; - } - - return Status::NGX_OK; - } - - let ctx = request.pool().allocate(RequestCTX::default()); - if ctx.is_null() { - return Status::NGX_ERROR; - } - request.set_module_ctx(ctx.cast(), Module::module()); - - let ctx = unsafe { &mut *ctx }; - ctx.event.handler = Some(check_async_work_done); - ctx.event.data = request.connection().cast(); - ctx.event.log = unsafe { (*request.connection()).log }; - unsafe { ngx_post_event(&raw mut ctx.event, &raw mut ngx_posted_next_events) }; - - // Request is no longer needed and can be converted to something movable to the async block - let req = AtomicPtr::new(request.into()); - let done_flag = ctx.done.clone(); - - let rt = ngx_http_async_runtime(); - ctx.task = Some(rt.spawn(async move { - let start = Instant::now(); - tokio::time::sleep(Duration::from_secs(2)).await; - let req = unsafe { http::Request::from_ngx_http_request(req.load(Ordering::Relaxed)) }; - // not really thread safe, we should apply all these operation in nginx thread - // but this is just an example. proper way would be storing these headers in the request - // ctx and apply them when we get back to the nginx thread. - req.add_header_out("X-Async-Time", start.elapsed().as_millis().to_string().as_str()); - - done_flag.store(true, Ordering::Release); - // there is a small issue here. If traffic is low we may get stuck behind a 300ms timer - // in the nginx event loop. To workaround it we can notify the event loop using - // pthread_kill( nginx_thread, SIGIO ) to wake up the event loop. (or patch nginx - // and use the same trick as the thread pool) - })); - - Status::NGX_AGAIN - } -} - -extern "C" fn ngx_http_async_commands_set_enable( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args: &[ngx_str_t] = (*(*cf).args).as_slice(); - let val = match args[1].to_str() { - Ok(s) => s, - Err(_) => { - ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`async` argument is not utf-8 encoded"); - return ngx::core::NGX_CONF_ERROR; - } - }; - - // set default value optionally - conf.enable = false; - - if val.eq_ignore_ascii_case("on") { - conf.enable = true; - } else if val.eq_ignore_ascii_case("off") { - conf.enable = false; - } - }; - - ngx::core::NGX_CONF_OK -} - -fn ngx_http_async_runtime() -> &'static Runtime { - // Should not be called from the master process - assert_ne!(unsafe { ngx::ffi::ngx_process }, ngx::ffi::NGX_PROCESS_MASTER as _); - - static RUNTIME: OnceLock = OnceLock::new(); - RUNTIME.get_or_init(|| { - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .expect("tokio runtime init") - }) -} diff --git a/rust/vendor/ngx-rust/examples/auto/rust b/rust/vendor/ngx-rust/examples/auto/rust deleted file mode 100644 index f3f1032..0000000 --- a/rust/vendor/ngx-rust/examples/auto/rust +++ /dev/null @@ -1,351 +0,0 @@ -#!/bin/sh -# -# Copyright 2025 Nginx, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# -# Utility library for integration of ngx-rust modules into the NGINX build -# configuration. -# -# Usage: -# -# In "config", -# -# ```sh -# . $ngx_addon_dir/auto/rust -# -# # ngx_addon_name determines the build directory and should be set before -# # any modules are defined -# -# ngx_addon_name="example" -# -# if [ $HTTP = YES ]; then -# # Regular NGINX module options, -# # http://nginx.org/en/docs/dev/development_guide.html#adding_new_modules -# -# ngx_module_type=HTTP -# # Should match the "ngx_module_t" static name(s) exported from the Rust code -# ngx_module_name=ngx_http_example_module -# ngx_module_incs= -# ngx_module_deps= -# ngx_module_libs= -# -# # Options specific to ngx-rust modules -# -# # Target type: LIB or EXAMPLE -# ngx_rust_target_type=LIB -# -# # Target name: crate name, lib.name or example.name -# ngx_rust_target_name=example -# -# # Whitespace-separated list of cargo features. -# # "default" should be specified explicitly if required. -# ngx_rust_target_features= -# -# ngx_rust_module -# fi -# ``` -# -# In "config.make", -# -# ```sh -# ngx_addon_name="example" -# ngx_cargo_manifest=$ngx_addon_dir/Cargo.toml -# -# # generate Makefile section for all the modules configured earlier -# -# ngx_rust_make_modules -# ``` -# -# The following environment variables can affect the generated Makefile: -# -# - NGX_CARGO sets the "cargo" binary to use, e.g. NGX_CARGO=cargo-1.82 -# - NGX_RUST_TARGET passes the --target to the Rust compiler if set -# - NGX_RUSTC_OPT passes additional options to "cargo rustc" - -# Prevent duplicate invocation unless it is a newer library version -if [ "${NGX_RUST_AUTO_VER:-0}" -ge 1 ]; then - return -fi - -NGX_RUST_AUTO_VER=1 - -echo $ngx_n "checking for Rust toolchain ...$ngx_c" - -NGX_CARGO=${NGX_CARGO:-cargo} - -NGX_RUST_VER=$($NGX_CARGO version 2>&1 \ - | grep 'cargo 1\.[0-9][0-9]*\.[0-9]*' 2>&1 \ - | sed -e 's/^.* \(1\.[0-9][0-9]*\.[0-9][0.9]*.*\)/\1/') - -NGX_RUST_VERSION=${NGX_RUST_VER%% *} - -if [ -z "$NGX_RUST_VERSION" ]; then - echo " not found" - echo - echo $0: error: cargo binary $NGX_CARGO is not found - echo - exit 1 -fi - -echo " found" -echo " + Rust version: $NGX_RUST_VER" - -case "$NGX_MACHINE" in - - amd64) - RUST_TARGET_ARCH=x86_64 - ;; - - arm64) - RUST_TARGET_ARCH=aarch64 - ;; - - i?86) - RUST_TARGET_ARCH=i686 - ;; - - *) - RUST_TARGET_ARCH=$NGX_MACHINE - ;; - -esac - -case "$NGX_PLATFORM" in - - OpenBSD:*) - # ld: error: undefined symbol: _Unwind_... - RUST_LIBS="$RUST_LIBS -lutil" - RUST_LIBS="$RUST_LIBS -lexecinfo" - RUST_LIBS="$RUST_LIBS -lc++abi" - ;; - - win32) - case "$NGX_CC_NAME" in - - msvc) - # as suggested by rustc --print native-static-libs, - # excluding entries already present in CORE_LIBS - RUST_LIBS="$RUST_LIBS bcrypt.lib" # ??? - RUST_LIBS="$RUST_LIBS ntdll.lib" # std::io, std::sys::pal::windows - RUST_LIBS="$RUST_LIBS userenv.lib" # std::env::home_dir - RUST_LIBS="$RUST_LIBS dbghelp.lib" # backtrace symbolization - - NGX_RUST_TARGET=${NGX_RUST_TARGET:-$RUST_TARGET_ARCH-pc-windows-msvc} - ;; - - gcc | clang) - RUST_LIBS="$RUST_LIBS -lbcrypt" - RUST_LIBS="$RUST_LIBS -lntdll" - RUST_LIBS="$RUST_LIBS -luserenv" - RUST_LIBS="$RUST_LIBS -ldbghelp" - # gnullvm on arm64? - NGX_RUST_TARGET=${NGX_RUST_TARGET:-$RUST_TARGET_ARCH-pc-windows-gnu} - ;; - - esac - ;; - -esac - - -# Prepare cargo configuration file - -if [ "$NGX_DEBUG" = YES ]; then - ngx_cargo_default_profile=ngx-debug -else - ngx_cargo_default_profile=ngx-release -fi - -ngx_cargo_config=$NGX_OBJS/.cargo/config.toml -ngx_cargo_profile=${ngx_cargo_profile:-$ngx_cargo_default_profile} - -mkdir -p "$NGX_OBJS/.cargo" - -cat << END > "$ngx_cargo_config" - -[profile.ngx-debug] -inherits = "dev" - -[profile.ngx-release] -inherits = "release" -strip = "none" - -# compatibility with LIBC=-MT set in auto/cc/msvc -[target.aarch64-pc-windows-msvc] -rustflags = ["-C", "target-feature=+crt-static"] - -[target.i686-pc-windows-msvc] -rustflags = ["-C", "target-feature=+crt-static"] - -[target.x86_64-pc-windows-msvc] -rustflags = ["-C", "target-feature=+crt-static"] - -[env] -NGINX_BUILD_DIR = { value = ".", force = true, relative = true } -END - -if [ "$NGX_PLATFORM" = win32 ] && command -v cygpath >/dev/null; then - printf >> "$ngx_cargo_config" 'NGINX_SOURCE_DIR = "%s"\n' \ - "$(cygpath -m "$PWD")" -else - printf >> "$ngx_cargo_config" 'NGINX_SOURCE_DIR = "%s"\n' "$PWD" -fi - - -# Reconstructs path to a static lib built with cargo rustc, -# relative to the --target-dir - -ngx_rust_target_path () { - ngx_rust_obj=$(echo "$ngx_rust_target_name" | tr - _) - - case "$NGX_CC_NAME" in - - msvc) - ngx_rust_obj=${ngx_rust_obj}.lib - ;; - - *) - ngx_rust_obj=lib${ngx_rust_obj}.a - ;; - - esac - - if [ "$ngx_rust_target_type" = EXAMPLE ]; then - ngx_rust_obj=examples/$ngx_rust_obj - fi - - echo "${NGX_RUST_TARGET:+$NGX_RUST_TARGET/}$ngx_cargo_profile/$ngx_rust_obj" -} - - -# Registers a module in the buildsystem. -# In addition to the regular auto/module parameters, the following variables -# are expected to be set: -# -# ngx_rust_target_type=LIB|EXAMPLE -# ngx_rust_target_name= -# ngx_rust_target_features= -# -# [^1]: https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field) - -ngx_rust_module () { - ngx_addon_id=$(echo "$ngx_addon_name" | sed -e 's/[^A-Za-z0-9_]/_/g') - ngx_rust_obj=$NGX_OBJS/$ngx_addon_id/$(ngx_rust_target_path) - - ngx_module_deps_saved=$ngx_module_deps - ngx_module_deps="$ngx_rust_obj $ngx_module_deps" - - ngx_module_libs_saved=$ngx_module_libs - ngx_module_libs="$ngx_rust_obj $ngx_module_libs $RUST_LIBS" - - if [ -n "$ngx_rust_target_features" ]; then - eval ${ngx_addon_id}_RUST_FEATURES=\"\$${ngx_addon_id}_RUST_FEATURES \ - $ngx_rust_target_features\" - fi - - . auto/module - - ngx_rust_target=$ngx_rust_target_type:$ngx_rust_target_name - - # module deps are usually added to the object file targets, but we don't have any - - if [ "$ngx_module_link" = DYNAMIC ]; then - # remember the dynamic module name and generate dependency later - ngx_rust_target=$ngx_rust_target:$ngx_module - else - # add dependency to the binary target - LINK_DEPS="$LINK_DEPS $ngx_rust_obj" - fi - - eval ${ngx_addon_id}_RUST_TARGETS=\"\$${ngx_addon_id}_RUST_TARGETS \ - $ngx_rust_target\" - - ngx_module_deps=$ngx_module_deps_saved - ngx_module_libs=$ngx_module_libs_saved -} - - -# Writes a Makefile fragment for all the modules configured for "ngx_addon_name" - -ngx_rust_make_modules () { - ngx_addon_id=$(echo "$ngx_addon_name" | sed -e 's/[^A-Za-z0-9_]/_/g') - ngx_cargo_manifest=${ngx_cargo_manifest:-"$ngx_addon_dir/Cargo.toml"} - - eval ngx_rust_features="\$${ngx_addon_id}_RUST_FEATURES" - eval ngx_rust_targets="\$${ngx_addon_id}_RUST_TARGETS" - - for target in $ngx_rust_targets; do - IFS=':' read -r ngx_rust_target_type ngx_rust_target_name ngx_rust_module_name <> $NGX_MAKEFILE - -.NGX_RUST_PHONY: ; - -END - fi - - cat << END >> $NGX_MAKEFILE - -$ngx_rust_obj: .NGX_RUST_PHONY - $NGX_CARGO rustc \\ - --config $ngx_cargo_config \\ - --crate-type staticlib \\ - --manifest-path "$ngx_cargo_manifest" \\ - --no-default-features \\ - --profile $ngx_cargo_profile \\ - ${NGX_RUST_TARGET:+--target $NGX_RUST_TARGET} \\ - --target-dir $NGX_OBJS/$ngx_addon_id \\ - --features "$ngx_rust_features" \\ - $ngx_rustc_module_opt $NGX_RUSTC_OPT - -END - - # Ensure that the "auto"-generated dynamic module target depends on the - # static library. Normally this is achieved by attaching ADDON_DEPS to - # the module object files, but we don't have any suitable C sources. - - if [ -n "$ngx_rust_module_name" ]; then - cat << END >> $NGX_MAKEFILE -$NGX_OBJS$ngx_dirsep$ngx_rust_module_name$ngx_modext: $ngx_rust_obj -END - fi -} diff --git a/rust/vendor/ngx-rust/examples/awssig.conf b/rust/vendor/ngx-rust/examples/awssig.conf deleted file mode 100644 index 5ff331e..0000000 --- a/rust/vendor/ngx-rust/examples/awssig.conf +++ /dev/null @@ -1,50 +0,0 @@ -daemon off; -master_process off; -# worker_processes 1; - -# on linix load a module: -load_module modules/libawssig.so; - -# error_log /dev/stdout debug; -error_log error.log debug; - -# working_directory /tmp/cores/; -# worker_rlimit_core 500M; - -events { } - -http { - server { - listen *:8000; - server_name localhost; - - awssigv4_access_key my-access-key; - awssigv4_secret_key my-secret-key; - awssigv4_s3_bucket my-bucket; - - location / { - awssigv4 on; - proxy_pass http://localhost:8777; - ## (on | off ) to enable aws sig v4 - location /some { - awssigv4 off; - } - ## awssigv4_s3_endpoint if not set then 's3.amazonaws.com' - # awssigv4_s3_endpoint s3.amazonaws.com; - } - - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root html; - } - } - server { - listen 8777; - server_name localhost; - location / { - add_header x-authorization $http_authorization; - add_header x-Amz-Date $http_x_amz_date; - return 204; - } - } -} diff --git a/rust/vendor/ngx-rust/examples/awssig.rs b/rust/vendor/ngx-rust/examples/awssig.rs deleted file mode 100644 index 0ff8c7c..0000000 --- a/rust/vendor/ngx-rust/examples/awssig.rs +++ /dev/null @@ -1,324 +0,0 @@ -use core::ffi::{c_char, c_void}; -use core::ptr; - -use http::HeaderMap; -use ngx::core::Status; -use ngx::ffi::{ - NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, - NGX_HTTP_SRV_CONF, NGX_LOG_EMERG, ngx_command_t, ngx_conf_t, ngx_http_module_t, ngx_int_t, - ngx_module_t, ngx_str_t, ngx_uint_t, -}; -use ngx::http::*; -use ngx::{ngx_conf_log_error, ngx_log_debug_http, ngx_string}; - -struct Module; - -impl HttpModule for Module { - fn module() -> &'static ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_awssigv4_module) } - } - - unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - // SAFETY: this function is called with non-NULL cf always - let cf = unsafe { &mut *cf }; - ngx::http::add_phase_handler::(cf) - .map_or(Status::NGX_ERROR, |_| Status::NGX_OK) - .into() - } -} - -#[derive(Debug, Default)] -struct ModuleConfig { - enable: bool, - access_key: String, - secret_key: String, - s3_bucket: String, - s3_endpoint: String, -} - -unsafe impl HttpModuleLocationConf for Module { - type LocationConf = ModuleConfig; -} - -static mut NGX_HTTP_AWSSIGV4_COMMANDS: [ngx_command_t; 6] = [ - ngx_command_t { - name: ngx_string!("awssigv4"), - type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_awssigv4_commands_set_enable), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("awssigv4_access_key"), - type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_awssigv4_commands_set_access_key), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("awssigv4_secret_key"), - type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_awssigv4_commands_set_secret_key), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("awssigv4_s3_bucket"), - type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_awssigv4_commands_set_s3_bucket), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("awssigv4_s3_endpoint"), - type_: (NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_awssigv4_commands_set_s3_endpoint), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t::empty(), -]; - -static NGX_HTTP_AWSSIGV4_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(Module::preconfiguration), - postconfiguration: Some(Module::postconfiguration), - create_main_conf: None, - init_main_conf: None, - create_srv_conf: None, - merge_srv_conf: None, - create_loc_conf: Some(Module::create_loc_conf), - merge_loc_conf: Some(Module::merge_loc_conf), -}; - -// Generate the `ngx_modules` table with exported modules. -// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_awssigv4_module); - -#[used] -#[allow(non_upper_case_globals)] -#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))] -pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_AWSSIGV4_MODULE_CTX as _, - commands: unsafe { &raw mut NGX_HTTP_AWSSIGV4_COMMANDS[0] }, - type_: NGX_HTTP_MODULE as _, - ..ngx_module_t::default() -}; - -impl Merge for ModuleConfig { - fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> { - if prev.enable { - self.enable = true; - }; - - if self.access_key.is_empty() { - self.access_key = - String::from(if !prev.access_key.is_empty() { &prev.access_key } else { "" }); - } - if self.enable && self.access_key.is_empty() { - return Err(MergeConfigError::NoValue); - } - - if self.secret_key.is_empty() { - self.secret_key = - String::from(if !prev.secret_key.is_empty() { &prev.secret_key } else { "" }); - } - if self.enable && self.secret_key.is_empty() { - return Err(MergeConfigError::NoValue); - } - - if self.s3_bucket.is_empty() { - self.s3_bucket = - String::from(if !prev.s3_bucket.is_empty() { &prev.s3_bucket } else { "" }); - } - if self.enable && self.s3_bucket.is_empty() { - return Err(MergeConfigError::NoValue); - } - - if self.s3_endpoint.is_empty() { - self.s3_endpoint = String::from(if !prev.s3_endpoint.is_empty() { - &prev.s3_endpoint - } else { - "s3.amazonaws.com" - }); - } - Ok(()) - } -} - -extern "C" fn ngx_http_awssigv4_commands_set_enable( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args: &[ngx_str_t] = (*(*cf).args).as_slice(); - let val = match args[1].to_str() { - Ok(s) => s, - Err(_) => { - ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`awssigv4` argument is not utf-8 encoded"); - return ngx::core::NGX_CONF_ERROR; - } - }; - - // set default value optionally - conf.enable = false; - - if val.len() == 2 && val.eq_ignore_ascii_case("on") { - conf.enable = true; - } else if val.len() == 3 && val.eq_ignore_ascii_case("off") { - conf.enable = false; - } - }; - - ngx::core::NGX_CONF_OK -} - -extern "C" fn ngx_http_awssigv4_commands_set_access_key( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args: &[ngx_str_t] = (*(*cf).args).as_slice(); - conf.access_key = args[1].to_string(); - }; - - ngx::core::NGX_CONF_OK -} - -extern "C" fn ngx_http_awssigv4_commands_set_secret_key( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args: &[ngx_str_t] = (*(*cf).args).as_slice(); - conf.secret_key = args[1].to_string(); - }; - - ngx::core::NGX_CONF_OK -} - -extern "C" fn ngx_http_awssigv4_commands_set_s3_bucket( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args: &[ngx_str_t] = (*(*cf).args).as_slice(); - conf.s3_bucket = args[1].to_string(); - if conf.s3_bucket.len() == 1 { - println!("Validation failed"); - return ngx::core::NGX_CONF_ERROR; - } - }; - - ngx::core::NGX_CONF_OK -} - -extern "C" fn ngx_http_awssigv4_commands_set_s3_endpoint( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args = (*(*cf).args).elts as *mut ngx_str_t; - conf.s3_endpoint = (*args.add(1)).to_string(); - }; - - ngx::core::NGX_CONF_OK -} - -struct AwsSigV4HeaderHandler; - -impl HttpRequestHandler for AwsSigV4HeaderHandler { - const PHASE: HttpPhase = HttpPhase::PreContent; - type Output = Status; - - fn handler(request: &mut Request) -> Self::Output { - // get Module Config from request - let conf = Module::location_conf(request).expect("module conf"); - ngx_log_debug_http!(request, "AWS signature V4 module {}", { - if conf.enable { "enabled" } else { "disabled" } - }); - if !conf.enable { - return Status::NGX_DECLINED; - } - - // TODO: build url properly from the original URL from client - let method = request.method(); - if !matches!(method, ngx::http::Method::HEAD | ngx::http::Method::GET) { - return HTTPStatus::FORBIDDEN.into(); - } - - let datetime = chrono::Utc::now(); - let uri = match request.unparsed_uri().to_str() { - Ok(v) => format!("https://{}.{}{}", conf.s3_bucket, conf.s3_endpoint, v), - Err(_) => return Status::NGX_DECLINED, - }; - - let datetime_now = datetime.format("%Y%m%dT%H%M%SZ"); - let datetime_now = datetime_now.to_string(); - - let signature = { - // NOTE: aws_sign_v4::AwsSign::new() implementation requires a HeaderMap. - // Iterate over requests headers_in and copy into HeaderMap - // Copy only headers that will be used to sign the request - let mut headers = HeaderMap::new(); - for (name, value) in request.headers_in_iterator() { - if let Ok(name) = name.to_str() { - if name.to_lowercase() == "host" { - let Ok(value) = http::HeaderValue::from_bytes(value.as_bytes()) else { - return Status::NGX_DECLINED; - }; - - headers.insert(http::header::HOST, value); - } - } else { - return Status::NGX_DECLINED; - } - } - headers.insert("X-Amz-Date", datetime_now.parse().unwrap()); - ngx_log_debug_http!(request, "headers {:?}", headers); - ngx_log_debug_http!(request, "method {:?}", method); - ngx_log_debug_http!(request, "uri {:?}", uri); - ngx_log_debug_http!(request, "datetime_now {:?}", datetime_now); - - let s = aws_sign_v4::AwsSign::new( - method.as_str(), - &uri, - &datetime, - &headers, - "us-east-1", - conf.access_key.as_str(), - conf.secret_key.as_str(), - "s3", - "", - ); - s.sign() - }; - - request.add_header_in("authorization", signature.as_str()); - request.add_header_in("X-Amz-Date", datetime_now.as_str()); - - for (name, value) in request.headers_out_iterator() { - ngx_log_debug_http!(request, "headers_out {name}: {value}",); - } - for (name, value) in request.headers_in_iterator() { - ngx_log_debug_http!(request, "headers_in {name}: {value}",); - } - - Status::NGX_OK - } -} diff --git a/rust/vendor/ngx-rust/examples/awssig_curl_combined.conf b/rust/vendor/ngx-rust/examples/awssig_curl_combined.conf deleted file mode 100644 index 7956e0e..0000000 --- a/rust/vendor/ngx-rust/examples/awssig_curl_combined.conf +++ /dev/null @@ -1,53 +0,0 @@ -daemon off; -master_process off; -# worker_processes 1; - -# on linix load a module: -load_module modules/libawssig.so; -load_module modules/libcurl.so; - -# error_log /dev/stdout debug; -error_log error.log debug; - -# working_directory /tmp/cores/; -# worker_rlimit_core 500M; - -events { } - -http { - server { - listen *:8000; - server_name localhost; - - awssigv4_access_key my-access-key; - awssigv4_secret_key my-secret-key; - awssigv4_s3_bucket my-bucket; - - location / { - awssigv4 on; - curl on; - - proxy_pass http://localhost:8777; - ## (on | off ) to enable aws sig v4 - location /some { - awssigv4 off; - } - ## awssigv4_s3_endpoint if not set then 's3.amazonaws.com' - # awssigv4_s3_endpoint s3.amazonaws.com; - } - - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root html; - } - } - server { - listen 8777; - server_name localhost; - location / { - add_header x-authorization $http_authorization; - add_header x-Amz-Date $http_x_amz_date; - return 204; - } - } -} diff --git a/rust/vendor/ngx-rust/examples/config b/rust/vendor/ngx-rust/examples/config deleted file mode 100644 index 6b76365..0000000 --- a/rust/vendor/ngx-rust/examples/config +++ /dev/null @@ -1,66 +0,0 @@ -# shellcheck source=auto/rust -. $ngx_addon_dir/auto/rust - -# ngx_addon_name determines the build directory and should be set before -# any modules are defined - -ngx_addon_name=ngx_rust_examples - -if [ $HTTP = YES ]; then - ngx_module_type=HTTP - ngx_module_incs= - ngx_module_deps= - ngx_module_order= - - ngx_rust_target_type=EXAMPLE - ngx_rust_target_features= - - if :; then - ngx_module_name=ngx_http_async_module - ngx_module_libs="-lm" - ngx_rust_target_name=async - - ngx_rust_module - fi - - if :; then - ngx_module_name=ngx_http_awssigv4_module - ngx_module_libs="-lm" - ngx_rust_target_name=awssig - - ngx_rust_module - fi - - if :; then - ngx_module_name=ngx_http_curl_module - ngx_module_libs= - ngx_rust_target_name=curl - - ngx_rust_module - fi - - if :; then - ngx_module_name=ngx_http_shared_dict_module - ngx_module_libs= - ngx_rust_target_name=shared_dict - - ngx_rust_module - fi - - if :; then - ngx_module_name=ngx_http_upstream_custom_module - ngx_module_libs= - ngx_rust_target_name=upstream - - ngx_rust_module - fi - - if [ "$NGX_SYSTEM" = Linux ]; then - ngx_module_name=ngx_http_orig_dst_module - ngx_module_libs= - ngx_rust_target_name=httporigdst - ngx_rust_target_features=linux - - ngx_rust_module - fi -fi diff --git a/rust/vendor/ngx-rust/examples/config.make b/rust/vendor/ngx-rust/examples/config.make deleted file mode 100644 index 5e62127..0000000 --- a/rust/vendor/ngx-rust/examples/config.make +++ /dev/null @@ -1,6 +0,0 @@ -ngx_addon_name=ngx_rust_examples -ngx_cargo_manifest=$ngx_addon_dir/Cargo.toml - -# generate Makefile section for all the modules configured earlier - -ngx_rust_make_modules diff --git a/rust/vendor/ngx-rust/examples/curl.conf b/rust/vendor/ngx-rust/examples/curl.conf deleted file mode 100644 index 603fc15..0000000 --- a/rust/vendor/ngx-rust/examples/curl.conf +++ /dev/null @@ -1,31 +0,0 @@ -daemon off; -master_process off; -# worker_processes 1; - -# on linux load a module: -load_module modules/libcurl.so; - -# on mac os it would be dylib -# load_module modules/libcurl.dylib; - -# error_log /dev/stdout debug; -error_log error.log debug; - -events { } - -http { - server { - listen *:8000; - server_name localhost; - location / { - root html; - index index.html index.htm; - # libcurl module directive: - curl on; - } - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root html; - } - } -} diff --git a/rust/vendor/ngx-rust/examples/curl.rs b/rust/vendor/ngx-rust/examples/curl.rs deleted file mode 100644 index be77920..0000000 --- a/rust/vendor/ngx-rust/examples/curl.rs +++ /dev/null @@ -1,136 +0,0 @@ -use core::ffi::{c_char, c_void}; -use core::ptr; - -use ngx::core::Status; -use ngx::ffi::{ - NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_LOG_EMERG, - ngx_command_t, ngx_conf_t, ngx_http_module_t, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t, -}; -use ngx::http::{self, HttpModule, HttpModuleLocationConf, HttpRequestHandler, MergeConfigError}; -use ngx::{ngx_conf_log_error, ngx_log_debug_http, ngx_string}; - -struct Module; - -impl http::HttpModule for Module { - fn module() -> &'static ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_curl_module) } - } - - unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - // SAFETY: this function is called with non-NULL cf always - let cf = unsafe { &mut *cf }; - http::add_phase_handler::(cf) - .map_or(Status::NGX_ERROR, |_| Status::NGX_OK) - .into() - } -} - -#[derive(Debug, Default)] -struct ModuleConfig { - enable: bool, -} - -unsafe impl HttpModuleLocationConf for Module { - type LocationConf = ModuleConfig; -} - -static mut NGX_HTTP_CURL_COMMANDS: [ngx_command_t; 2] = [ - ngx_command_t { - name: ngx_string!("curl"), - type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_curl_commands_set_enable), - conf: NGX_HTTP_LOC_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t::empty(), -]; - -static NGX_HTTP_CURL_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(Module::preconfiguration), - postconfiguration: Some(Module::postconfiguration), - create_main_conf: None, - init_main_conf: None, - create_srv_conf: None, - merge_srv_conf: None, - create_loc_conf: Some(Module::create_loc_conf), - merge_loc_conf: Some(Module::merge_loc_conf), -}; - -// Generate the `ngx_modules` table with exported modules. -// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_curl_module); - -#[used] -#[allow(non_upper_case_globals)] -#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))] -pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_CURL_MODULE_CTX as _, - commands: unsafe { &raw mut NGX_HTTP_CURL_COMMANDS[0] }, - type_: NGX_HTTP_MODULE as _, - ..ngx_module_t::default() -}; - -impl http::Merge for ModuleConfig { - fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> { - if prev.enable { - self.enable = true; - }; - Ok(()) - } -} - -struct CurlRequestHandler; - -impl HttpRequestHandler for CurlRequestHandler { - const PHASE: ngx::http::HttpPhase = ngx::http::HttpPhase::Access; - type Output = Status; - - fn handler(request: &mut http::Request) -> Self::Output { - let co = Module::location_conf(request).expect("module config is none"); - - ngx_log_debug_http!(request, "curl module enabled: {}", co.enable); - - match co.enable { - true => { - if request.user_agent().is_some_and(|ua| ua.as_bytes().starts_with(b"curl")) { - http::HTTPStatus::FORBIDDEN.into() - } else { - Status::NGX_DECLINED - } - } - false => Status::NGX_DECLINED, - } - } -} - -extern "C" fn ngx_http_curl_commands_set_enable( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - unsafe { - let conf = &mut *(conf as *mut ModuleConfig); - let args: &[ngx_str_t] = (*(*cf).args).as_slice(); - - let val = match args[1].to_str() { - Ok(s) => s, - Err(_) => { - ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`curl` argument is not utf-8 encoded"); - return ngx::core::NGX_CONF_ERROR; - } - }; - - // set default value optionally - conf.enable = false; - - if val.len() == 2 && val.eq_ignore_ascii_case("on") { - conf.enable = true; - } else if val.len() == 3 && val.eq_ignore_ascii_case("off") { - conf.enable = false; - } - }; - - ngx::core::NGX_CONF_OK -} diff --git a/rust/vendor/ngx-rust/examples/httporigdst.conf b/rust/vendor/ngx-rust/examples/httporigdst.conf deleted file mode 100644 index 6fff8c2..0000000 --- a/rust/vendor/ngx-rust/examples/httporigdst.conf +++ /dev/null @@ -1,16 +0,0 @@ -http { - server { - # use iptables to capture all outgoing traffic and REDIRECT - # to listening port 15501 - listen 15501; - - # binding variables provided by module will lazily activate it - # and store a context - # variables can be used in config - location / { - # Return if no backend is available or proxy_pass - # return 200 "recv'd: $server_addr:$server_port\n\nproxy_pass http://$server_orig_addr:$server_orig_port\n"; - proxy_pass http://$server_orig_addr:$server_orig_port; - } - } -} \ No newline at end of file diff --git a/rust/vendor/ngx-rust/examples/httporigdst.rs b/rust/vendor/ngx-rust/examples/httporigdst.rs deleted file mode 100644 index 2eda38d..0000000 --- a/rust/vendor/ngx-rust/examples/httporigdst.rs +++ /dev/null @@ -1,283 +0,0 @@ -use core::ffi::{c_int, c_void}; -use core::mem; -use core::ptr::{self, NonNull}; - -use ngx::core::{Pool, Status}; -use ngx::ffi::{ - INET_ADDRSTRLEN, NGX_HTTP_MODULE, in_port_t, ngx_conf_t, ngx_connection_local_sockaddr, - ngx_http_add_variable, ngx_http_module_t, ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, - ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_variable_value_t, sockaddr, sockaddr_storage, -}; -use ngx::http::{self, HttpModule}; -use ngx::{http_variable_get, ngx_log_debug_http, ngx_string}; - -const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize; - -#[derive(Debug, Default)] -struct NgxHttpOrigDstCtx { - orig_dst_addr: ngx_str_t, - orig_dst_port: ngx_str_t, -} - -impl NgxHttpOrigDstCtx { - pub fn save(&mut self, addr: &str, port: in_port_t, pool: &Pool) -> Status { - let addr_data = pool.alloc_unaligned(addr.len()); - if addr_data.is_null() { - return Status::NGX_ERROR; - } - unsafe { libc::memcpy(addr_data, addr.as_ptr() as *const c_void, addr.len()) }; - self.orig_dst_addr.len = addr.len(); - self.orig_dst_addr.data = addr_data as *mut u8; - - let port_str = port.to_string(); - let port_data = pool.alloc_unaligned(port_str.len()); - if port_data.is_null() { - return Status::NGX_ERROR; - } - unsafe { - libc::memcpy(port_data, port_str.as_bytes().as_ptr() as *const c_void, port_str.len()) - }; - self.orig_dst_port.len = port_str.len(); - self.orig_dst_port.data = port_data as *mut u8; - - Status::NGX_OK - } - - pub unsafe fn bind_addr(&self, v: *mut ngx_variable_value_t) { - let mut v = NonNull::new(v).unwrap(); - let v = unsafe { v.as_mut() }; - if self.orig_dst_addr.len == 0 { - v.set_not_found(1); - return; - } - - v.set_valid(1); - v.set_no_cacheable(0); - v.set_not_found(0); - v.set_len(self.orig_dst_addr.len as u32); - v.data = self.orig_dst_addr.data; - } - - pub unsafe fn bind_port(&self, v: *mut ngx_variable_value_t) { - let mut v = NonNull::new(v).unwrap(); - let v = unsafe { v.as_mut() }; - if self.orig_dst_port.len == 0 { - v.set_not_found(1); - return; - } - - v.set_valid(1); - v.set_no_cacheable(0); - v.set_not_found(0); - v.set_len(self.orig_dst_port.len as u32); - v.data = self.orig_dst_port.data; - } -} - -static NGX_HTTP_ORIG_DST_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(Module::preconfiguration), - postconfiguration: Some(Module::postconfiguration), - create_main_conf: None, - init_main_conf: None, - create_srv_conf: None, - merge_srv_conf: None, - create_loc_conf: None, - merge_loc_conf: None, -}; - -// Generate the `ngx_modules` table with exported modules. -// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_orig_dst_module); - -#[used] -#[allow(non_upper_case_globals)] -#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))] -pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_ORIG_DST_MODULE_CTX as _, - commands: ptr::null_mut(), - type_: NGX_HTTP_MODULE as _, - ..ngx_module_t::default() -}; - -static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 2] = [ - // ngx_str_t name - // ngx_http_set_variable_pt set_handler - // ngx_http_get_variable_pt get_handler - // uintptr_t data - // ngx_uint_t flags - // ngx_uint_t index - ngx_http_variable_t { - name: ngx_string!("server_orig_addr"), - set_handler: None, - get_handler: Some(ngx_http_orig_dst_addr_variable), - data: 0, - flags: 0, - index: 0, - }, - ngx_http_variable_t { - name: ngx_string!("server_orig_port"), - set_handler: None, - get_handler: Some(ngx_http_orig_dst_port_variable), - data: 0, - flags: 0, - index: 0, - }, -]; - -unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_port_t), Status> { - let c = request.connection(); - - if unsafe { (*c).type_ } != libc::SOCK_STREAM { - ngx_log_debug_http!(request, "httporigdst: connection is not type SOCK_STREAM"); - return Err(Status::NGX_DECLINED); - } - - if unsafe { ngx_connection_local_sockaddr(c, ptr::null_mut(), 0) } != Status::NGX_OK.into() { - ngx_log_debug_http!(request, "httporigdst: no local sockaddr from connection"); - return Err(Status::NGX_ERROR); - } - - let level: c_int; - let optname: c_int; - match unsafe { (*(*c).local_sockaddr).sa_family } as i32 { - libc::AF_INET => { - level = libc::SOL_IP; - optname = libc::SO_ORIGINAL_DST; - } - _ => { - ngx_log_debug_http!(request, "httporigdst: only support IPv4"); - return Err(Status::NGX_DECLINED); - } - } - - let mut addr: sockaddr_storage = unsafe { mem::zeroed() }; - let mut addrlen: libc::socklen_t = mem::size_of_val(&addr) as libc::socklen_t; - let rc = unsafe { - libc::getsockopt((*c).fd, level, optname, (&raw mut addr).cast(), &raw mut addrlen) - }; - if rc == -1 { - ngx_log_debug_http!(request, "httporigdst: getsockopt failed"); - return Err(Status::NGX_DECLINED); - } - let mut ip: Vec = vec![0; IPV4_STRLEN]; - let e = unsafe { - ngx_sock_ntop( - (&raw mut addr).cast(), - mem::size_of::() as u32, - ip.as_mut_ptr(), - IPV4_STRLEN, - 0, - ) - }; - if e == 0 { - ngx_log_debug_http!(request, "httporigdst: ngx_sock_ntop failed to convert sockaddr"); - return Err(Status::NGX_ERROR); - } - ip.truncate(e); - - let port = unsafe { ngx_inet_get_port((&raw mut addr).cast()) }; - - Ok((String::from_utf8(ip).unwrap(), port)) -} - -http_variable_get!( - ngx_http_orig_dst_addr_variable, - |request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| { - let ctx = request.get_module_ctx::(Module::module()); - if let Some(obj) = ctx { - ngx_log_debug_http!(request, "httporigdst: found context and binding variable",); - unsafe { obj.bind_addr(v) }; - return Status::NGX_OK; - } - // lazy initialization: - // get original dest information - // create context - // set context - // bind address - ngx_log_debug_http!(request, "httporigdst: context not found, getting address"); - let r = unsafe { ngx_get_origdst(request) }; - match r { - Err(e) => { - return e; - } - Ok((ip, port)) => { - // create context, - // set context - let new_ctx = request.pool().allocate::(Default::default()); - - if new_ctx.is_null() { - return Status::NGX_ERROR; - } - - ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,); - unsafe { (*new_ctx).save(&ip, port, &request.pool()) }; - unsafe { (*new_ctx).bind_addr(v) }; - request.set_module_ctx(new_ctx as *mut c_void, Module::module()); - } - } - Status::NGX_OK - } -); - -http_variable_get!( - ngx_http_orig_dst_port_variable, - |request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| { - let ctx = request.get_module_ctx::(Module::module()); - if let Some(obj) = ctx { - ngx_log_debug_http!(request, "httporigdst: found context and binding variable",); - unsafe { obj.bind_port(v) }; - return Status::NGX_OK; - } - // lazy initialization: - // get original dest information - // create context - // set context - // bind port - ngx_log_debug_http!(request, "httporigdst: context not found, getting address"); - let r = unsafe { ngx_get_origdst(request) }; - match r { - Err(e) => { - return e; - } - Ok((ip, port)) => { - // create context, - // set context - let new_ctx = request.pool().allocate::(Default::default()); - - if new_ctx.is_null() { - return Status::NGX_ERROR; - } - - ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,); - unsafe { (*new_ctx).save(&ip, port, &request.pool()) }; - unsafe { (*new_ctx).bind_port(v) }; - request.set_module_ctx(new_ctx as *mut c_void, Module::module()); - } - } - Status::NGX_OK - } -); - -struct Module; - -impl HttpModule for Module { - fn module() -> &'static ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_orig_dst_module) } - } - - // static ngx_int_t ngx_http_orig_dst_add_variables(ngx_conf_t *cf) - unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - for mut v in unsafe { NGX_HTTP_ORIG_DST_VARS } { - let var = NonNull::new(unsafe { ngx_http_add_variable(cf, &raw mut v.name, v.flags) }); - if var.is_none() { - return Status::NGX_ERROR.into(); - } - let mut var = var.unwrap(); - let var = unsafe { var.as_mut() }; - var.get_handler = v.get_handler; - var.data = v.data; - } - Status::NGX_OK.into() - } -} diff --git a/rust/vendor/ngx-rust/examples/shared_dict.rs b/rust/vendor/ngx-rust/examples/shared_dict.rs deleted file mode 100644 index d88ebdb..0000000 --- a/rust/vendor/ngx-rust/examples/shared_dict.rs +++ /dev/null @@ -1,425 +0,0 @@ -#![no_std] -use core::ffi::{c_char, c_void}; -use core::mem; -use core::ptr::{self, NonNull}; - -use nginx_sys::{ - NGX_CONF_TAKE2, NGX_HTTP_DELETE, NGX_HTTP_MAIN_CONF, NGX_HTTP_MAIN_CONF_OFFSET, - NGX_HTTP_MODULE, NGX_HTTP_VAR_CHANGEABLE, NGX_HTTP_VAR_NOCACHEABLE, NGX_LOG_EMERG, - ngx_command_t, ngx_conf_t, ngx_http_add_variable, ngx_http_compile_complex_value_t, - ngx_http_complex_value, ngx_http_complex_value_t, ngx_http_module_t, ngx_http_request_t, - ngx_http_variable_t, ngx_http_variable_value_t, ngx_int_t, ngx_module_t, ngx_parse_size, - ngx_shared_memory_add, ngx_shm_zone_t, ngx_str_t, ngx_uint_t, -}; -use ngx::collections::RbTreeMap; -use ngx::core::{NGX_CONF_ERROR, NGX_CONF_OK, NgxStr, NgxString, Pool, SlabPool, Status}; -use ngx::http::{HttpModule, HttpModuleMainConf}; -use ngx::{ngx_conf_log_error, ngx_log_debug, ngx_string}; - -struct HttpSharedDictModule; - -impl HttpModule for HttpSharedDictModule { - fn module() -> &'static ngx_module_t { - unsafe { &*ptr::addr_of!(ngx_http_shared_dict_module) } - } - - unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - for mut v in unsafe { NGX_HTTP_SHARED_DICT_VARS } { - let var = NonNull::new(unsafe { ngx_http_add_variable(cf, &raw mut v.name, v.flags) }); - if var.is_none() { - return Status::NGX_ERROR.into(); - } - let var = unsafe { var.unwrap().as_mut() }; - var.get_handler = v.get_handler; - var.set_handler = v.set_handler; - var.data = v.data; - } - Status::NGX_OK.into() - } -} - -unsafe impl HttpModuleMainConf for HttpSharedDictModule { - type MainConf = SharedDictMainConfig; -} - -static mut NGX_HTTP_SHARED_DICT_COMMANDS: [ngx_command_t; 3] = [ - ngx_command_t { - name: ngx_string!("shared_dict_zone"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2) as ngx_uint_t, - set: Some(ngx_http_shared_dict_add_zone), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t { - name: ngx_string!("shared_dict"), - type_: (NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2) as ngx_uint_t, - set: Some(ngx_http_shared_dict_add_variable), - conf: NGX_HTTP_MAIN_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t::empty(), -]; - -static mut NGX_HTTP_SHARED_DICT_VARS: [ngx_http_variable_t; 1] = [ngx_http_variable_t { - name: ngx_string!("shared_dict_entries"), - set_handler: Some(ngx_http_shared_dict_set_entries), - get_handler: Some(ngx_http_shared_dict_get_entries), - data: 0, - flags: (NGX_HTTP_VAR_CHANGEABLE | NGX_HTTP_VAR_NOCACHEABLE) as ngx_uint_t, - index: 0, -}]; - -static NGX_HTTP_SHARED_DICT_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(HttpSharedDictModule::preconfiguration), - postconfiguration: None, - create_main_conf: Some(HttpSharedDictModule::create_main_conf), - init_main_conf: None, - create_srv_conf: None, - merge_srv_conf: None, - create_loc_conf: None, - merge_loc_conf: None, -}; - -// Generate the `ngx_modules` table with exported modules. -// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_shared_dict_module); - -#[used] -#[allow(non_upper_case_globals)] -#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))] -pub static mut ngx_http_shared_dict_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_SHARED_DICT_MODULE_CTX as _, - commands: unsafe { &raw mut NGX_HTTP_SHARED_DICT_COMMANDS[0] }, - type_: NGX_HTTP_MODULE as _, - ..ngx_module_t::default() -}; - -type SharedData = ngx::sync::RwLock, NgxString, SlabPool>>; - -#[derive(Debug)] -struct SharedDictMainConfig { - shm_zone: *mut ngx_shm_zone_t, -} - -impl Default for SharedDictMainConfig { - fn default() -> Self { - Self { shm_zone: ptr::null_mut() } - } -} - -extern "C" fn ngx_http_shared_dict_add_zone( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - // SAFETY: configuration handlers always receive a valid `cf` pointer. - let cf = unsafe { cf.as_mut().unwrap() }; - let smcf = - unsafe { conf.cast::().as_mut().expect("shared dict main config") }; - - // SAFETY: - // - `cf.args` is guaranteed to be a pointer to an array with 3 elements (NGX_CONF_TAKE2). - // - The pointers are well-aligned by construction method (`ngx_palloc`). - debug_assert!(!cf.args.is_null() && unsafe { (*cf.args).nelts >= 3 }); - let args = unsafe { (*cf.args).as_slice_mut() }; - - let mut name: ngx_str_t = args[1]; - let size = unsafe { ngx_parse_size(&raw mut args[2]) }; - if size == -1 { - return NGX_CONF_ERROR; - } - - smcf.shm_zone = unsafe { - ngx_shared_memory_add( - cf, - &raw mut name, - size as usize, - (&raw mut ngx_http_shared_dict_module).cast(), - ) - }; - - let Some(shm_zone) = (unsafe { smcf.shm_zone.as_mut() }) else { - return NGX_CONF_ERROR; - }; - - shm_zone.init = Some(ngx_http_shared_dict_zone_init); - shm_zone.data = ptr::from_mut(smcf).cast(); - - NGX_CONF_OK -} - -fn ngx_http_shared_dict_get_shared(shm_zone: &mut ngx_shm_zone_t) -> Result<&SharedData, Status> { - let mut alloc = unsafe { SlabPool::from_shm_zone(shm_zone) }.ok_or(Status::NGX_ERROR)?; - - if alloc.as_mut().data.is_null() { - let shared: RbTreeMap, NgxString, SlabPool> = - RbTreeMap::try_new_in(alloc.clone()).map_err(|_| Status::NGX_ERROR)?; - - let shared = ngx::sync::RwLock::new(shared); - - alloc.as_mut().data = ngx::allocator::allocate(shared, &alloc) - .map_err(|_| Status::NGX_ERROR)? - .as_ptr() - .cast(); - } - - unsafe { alloc.as_ref().data.cast::().as_ref().ok_or(Status::NGX_ERROR) } -} - -extern "C" fn ngx_http_shared_dict_zone_init( - shm_zone: *mut ngx_shm_zone_t, - _data: *mut c_void, -) -> ngx_int_t { - let shm_zone = unsafe { &mut *shm_zone }; - - match ngx_http_shared_dict_get_shared(shm_zone) { - Err(e) => e.into(), - Ok(_) => Status::NGX_OK.into(), - } -} - -extern "C" fn ngx_http_shared_dict_add_variable( - cf: *mut ngx_conf_t, - _cmd: *mut ngx_command_t, - _conf: *mut c_void, -) -> *mut c_char { - // SAFETY: configuration handlers always receive a valid `cf` pointer. - let cf = unsafe { cf.as_mut().unwrap() }; - let pool = unsafe { Pool::from_ngx_pool(cf.pool) }; - - let key = pool.calloc_type::(); - if key.is_null() { - return NGX_CONF_ERROR; - } - - // SAFETY: - // - `cf.args` is guaranteed to be a pointer to an array with 3 elements (NGX_CONF_TAKE2). - // - The pointers are well-aligned by construction method (`ngx_palloc`). - debug_assert!(!cf.args.is_null() && unsafe { (*cf.args).nelts >= 3 }); - let args = unsafe { (*cf.args).as_slice_mut() }; - - let mut ccv: ngx_http_compile_complex_value_t = unsafe { mem::zeroed() }; - ccv.cf = cf; - ccv.value = &raw mut args[1]; - ccv.complex_value = key; - - if unsafe { nginx_sys::ngx_http_compile_complex_value(&raw mut ccv) } != Status::NGX_OK.into() { - return NGX_CONF_ERROR; - } - - let mut name = args[2]; - - if name.as_bytes()[0] != b'$' { - ngx_conf_log_error!(NGX_LOG_EMERG, cf, "invalid variable name \"{name}\""); - return NGX_CONF_ERROR; - } - - name.data = unsafe { name.data.add(1) }; - name.len -= 1; - - let var = unsafe { - ngx_http_add_variable( - cf, - &raw mut name, - (NGX_HTTP_VAR_CHANGEABLE | NGX_HTTP_VAR_NOCACHEABLE) as ngx_uint_t, - ) - }; - if var.is_null() { - return NGX_CONF_ERROR; - } - - unsafe { - (*var).get_handler = Some(ngx_http_shared_dict_get_variable); - (*var).set_handler = Some(ngx_http_shared_dict_set_variable); - (*var).data = key as usize; - } - - NGX_CONF_OK -} - -extern "C" fn ngx_http_shared_dict_get_variable( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - data: usize, -) -> ngx_int_t { - let r = unsafe { &mut *r }; - let v = unsafe { &mut *v }; - let smcf = HttpSharedDictModule::main_conf_mut(r).expect("shared dict main config"); - - let mut key = ngx_str_t::empty(); - if unsafe { ngx_http_complex_value(r, data as _, &raw mut key) } != Status::NGX_OK.into() { - return Status::NGX_ERROR.into(); - } - - let key = unsafe { NgxStr::from_ngx_str(key) }; - - let Ok(shared) = ngx_http_shared_dict_get_shared(unsafe { &mut *smcf.shm_zone }) else { - return Status::NGX_ERROR.into(); - }; - - let value = - shared.read().get(key).and_then(|x| unsafe { ngx_str_t::from_bytes(r.pool, x.as_bytes()) }); - - ngx_log_debug!( - unsafe { (*r.connection).log }, - "shared dict: get \"{}\" -> {:?} w:{} p:{}", - key, - value.as_ref().map(|x| unsafe { NgxStr::from_ngx_str(*x) }), - unsafe { nginx_sys::ngx_worker }, - unsafe { nginx_sys::ngx_pid }, - ); - - let Some(value) = value else { - v.set_not_found(1); - return Status::NGX_ERROR.into(); - }; - - v.data = value.data; - v.set_len(value.len as _); - - v.set_valid(1); - v.set_no_cacheable(0); - v.set_not_found(0); - - Status::NGX_OK.into() -} - -extern "C" fn ngx_http_shared_dict_set_variable( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - data: usize, -) { - let r = unsafe { &mut *r }; - let v = unsafe { &mut *v }; - let smcf = HttpSharedDictModule::main_conf_mut(r).expect("shared dict main config"); - let mut key = ngx_str_t::empty(); - - if unsafe { ngx_http_complex_value(r, data as _, &raw mut key) } != Status::NGX_OK.into() { - return; - } - - let Ok(shared) = ngx_http_shared_dict_get_shared(unsafe { &mut *smcf.shm_zone }) else { - return; - }; - - if r.method == NGX_HTTP_DELETE as _ { - let key = unsafe { NgxStr::from_ngx_str(key) }; - - ngx_log_debug!( - unsafe { (*r.connection).log }, - "shared dict: delete \"{}\" w:{} p:{}", - key, - unsafe { nginx_sys::ngx_worker }, - unsafe { nginx_sys::ngx_pid }, - ); - - let _ = shared.write().remove(key); - } else { - let alloc = unsafe { SlabPool::from_shm_zone(&*smcf.shm_zone).expect("slab pool") }; - - let Ok(key) = NgxString::try_from_bytes_in(key.as_bytes(), alloc.clone()) else { - return; - }; - - let Ok(value) = NgxString::try_from_bytes_in(v.as_bytes(), alloc.clone()) else { - return; - }; - - ngx_log_debug!( - unsafe { (*r.connection).log }, - "shared dict: set \"{}\" -> \"{}\" w:{} p:{}", - key, - value, - unsafe { nginx_sys::ngx_worker }, - unsafe { nginx_sys::ngx_pid }, - ); - - let _ = shared.write().try_insert(key, value); - } -} - -extern "C" fn ngx_http_shared_dict_get_entries( - r: *mut ngx_http_request_t, - v: *mut ngx_http_variable_value_t, - _data: usize, -) -> ngx_int_t { - use core::fmt::Write; - - let r = unsafe { &mut *r }; - let v = unsafe { &mut *v }; - let pool = unsafe { Pool::from_ngx_pool(r.pool) }; - let smcf = HttpSharedDictModule::main_conf_mut(r).expect("shared dict main config"); - - ngx_log_debug!(unsafe { (*r.connection).log }, "shared dict: get all entries"); - - let Ok(shared) = ngx_http_shared_dict_get_shared(unsafe { &mut *smcf.shm_zone }) else { - return Status::NGX_ERROR.into(); - }; - - let mut str = NgxString::new_in(pool); - { - let dict = shared.read(); - - let mut len: usize = 0; - let mut values: usize = 0; - - for (key, value) in dict.iter() { - len += key.len() + value.len() + b" = ; ".len(); - values += 1; - } - - len += values.checked_ilog10().unwrap_or(0) as usize + b"0; ".len(); - - if str.try_reserve(len).is_err() { - return Status::NGX_ERROR.into(); - } - - if write!(str, "{values}; ").is_err() { - return Status::NGX_ERROR.into(); - } - - for (key, value) in dict.iter() { - if write!(str, "{key} = {value}; ").is_err() { - return Status::NGX_ERROR.into(); - } - } - } - - // The string is allocated on the `ngx_pool_t` and will be freed with the request. - let (data, len, _, _) = str.into_raw_parts(); - - v.data = data; - v.set_len(len as _); - - v.set_valid(1); - v.set_no_cacheable(1); - v.set_not_found(0); - - Status::NGX_OK.into() -} - -extern "C" fn ngx_http_shared_dict_set_entries( - r: *mut ngx_http_request_t, - _v: *mut ngx_http_variable_value_t, - _data: usize, -) { - let r = unsafe { &mut *r }; - let smcf = HttpSharedDictModule::main_conf_mut(r).expect("shared dict main config"); - - ngx_log_debug!(unsafe { (*r.connection).log }, "shared dict: clear"); - - let Ok(shared) = ngx_http_shared_dict_get_shared(unsafe { &mut *smcf.shm_zone }) else { - return; - }; - - let Ok(tree) = RbTreeMap::try_new_in(shared.read().allocator().clone()) else { - return; - }; - - // This would check both .clear() and the drop implementation - *shared.write() = tree; - // shared.write().clear() -} diff --git a/rust/vendor/ngx-rust/examples/t/async.t b/rust/vendor/ngx-rust/examples/t/async.t deleted file mode 100644 index 98505fd..0000000 --- a/rust/vendor/ngx-rust/examples/t/async.t +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/perl - -# (C) Nginx, Inc - -# Tests for ngx-rust example modules. - -############################################################################### - -use warnings; -use strict; - -use Test::More; - -BEGIN { use FindBin; chdir($FindBin::Bin); } - -use lib 'lib'; -use Test::Nginx; - -############################################################################### - -select STDERR; $| = 1; -select STDOUT; $| = 1; - -my $t = Test::Nginx->new()->has(qw/http/)->plan(1) - ->write_file_expand('nginx.conf', <<'EOF'); - -%%TEST_GLOBALS%% - -daemon off; - -events { -} - -http { - %%TEST_GLOBALS_HTTP%% - - server { - listen 127.0.0.1:8080; - server_name localhost; - - location / { - async on; - } - } -} - -EOF - -$t->write_file('index.html', ''); -$t->run(); - -############################################################################### - -like(http_get('/index.html'), qr/X-Async-Time:/, 'async handler'); - -############################################################################### diff --git a/rust/vendor/ngx-rust/examples/t/awssig.t b/rust/vendor/ngx-rust/examples/t/awssig.t deleted file mode 100644 index ba43cd0..0000000 --- a/rust/vendor/ngx-rust/examples/t/awssig.t +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/perl - -# (C) Nginx, Inc - -# Tests for ngx-rust example modules. - -############################################################################### - -use warnings; -use strict; - -use Test::More; - -BEGIN { use FindBin; chdir($FindBin::Bin); } - -use lib 'lib'; -use Test::Nginx; - -############################################################################### - -select STDERR; $| = 1; -select STDOUT; $| = 1; - -my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(1) - ->write_file_expand('nginx.conf', <<"EOF"); - -%%TEST_GLOBALS%% - -daemon off; - -events { -} - -http { - %%TEST_GLOBALS_HTTP%% - - server { - listen 127.0.0.1:8080; - server_name localhost; - - awssigv4_access_key my-access-key; - awssigv4_secret_key my-secret-key; - awssigv4_s3_bucket my-bucket; - awssigv4_s3_endpoint s3.example.com; - - location / { - awssigv4 on; - proxy_pass http://127.0.0.1:8081; - } - } - - server { - listen 127.0.0.1:8081; - server_name localhost; - - add_header x-amz-date \$http_x_amz_date; - add_header x-authorization \$http_authorization; - - location / { } - } -} - -EOF - -$t->write_file('index.html', ''); -$t->run(); - -############################################################################### - -like(http_get('/'), qr/x-authorization: AWS4.*Credential=my-access-key/i, - 'awssig header'); - -############################################################################### diff --git a/rust/vendor/ngx-rust/examples/t/curl.t b/rust/vendor/ngx-rust/examples/t/curl.t deleted file mode 100644 index dffe5a0..0000000 --- a/rust/vendor/ngx-rust/examples/t/curl.t +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/perl - -# (C) Nginx, Inc - -# Tests for ngx-rust example modules. - -############################################################################### - -use warnings; -use strict; - -use Test::More; - -BEGIN { use FindBin; chdir($FindBin::Bin); } - -use lib 'lib'; -use Test::Nginx; - -############################################################################### - -select STDERR; $| = 1; -select STDOUT; $| = 1; - -my $t = Test::Nginx->new()->has(qw/http/)->plan(2) - ->write_file_expand('nginx.conf', <<"EOF"); - -%%TEST_GLOBALS%% - -daemon off; - -events { -} - -http { - %%TEST_GLOBALS_HTTP%% - - server { - listen 127.0.0.1:8080; - server_name localhost; - - location / { - curl on; - } - } -} - -EOF - -$t->write_file('index.html', ''); -$t->run(); - -############################################################################### - -like(get('/', 'curl/1.2.3'), qr/403 Forbidden/, 'curl UA forbidden'); -like(get('/', 'MSIE 6.0'), qr/200 OK/, 'other UA allowed'); - -############################################################################### - -sub get { - my ($url, $ua, $extra) = @_; - return http(<new()->has(qw/http rewrite/)->plan(12) - ->write_file_expand('nginx.conf', <<'EOF'); - -%%TEST_GLOBALS%% - -daemon off; - -worker_processes 2; - -events { -} - -http { - %%TEST_GLOBALS_HTTP%% - - shared_dict_zone z 64k; - shared_dict $arg_key $foo; - - server { - listen 127.0.0.1:8080; - server_name localhost; - - add_header X-Value $foo; - add_header X-Process $pid; - - location /set/ { - add_header X-Process $pid; - set $foo $arg_value; - return 200; - } - - location /entries/ { - add_header X-Process $pid; - return 200 $shared_dict_entries; - } - - location /clear/ { - add_header X-Process $pid; - set $shared_dict_entries ""; - return 200; - } - } -} - -EOF - -$t->write_file('index.html', ''); -$t->run(); - -############################################################################### - -like(http_get('/set/?key=fst&value=hello'), qr/200 OK/, 'set value 1'); -like(http_get('/set/?key=snd&value=world'), qr/200 OK/, 'set value 2'); - -ok(check('/?key=fst', qr/X-Value: hello/i), 'check value 1'); -ok(check('/?key=snd', qr/X-Value: world/i), 'check value 2'); - -like(http_get('/set/?key=fst&value=new_value'), qr/200 OK/, 'update value 1'); -ok(check('/?key=fst', qr/X-Value: new_value/i), 'check updated value'); - -like(http_get('/entries/'), qr/^2; ((?:fst = new_value|snd = world); ){2}$/ms, - 'get entries'); - -like(http_delete('/set/?key=snd'), qr/200 OK/, 'delete value 2'); - -unlike(http_get('/?key=snd'), qr/X-Value:/i, 'check deleted value'); - -like(http_get('/entries/'), qr/^1; fst = new_value; $/ms, - 'get entries - deleted'); - -like(http_get('/clear/'), qr/200 OK/, 'clear'); - -like(http_get('/entries/'), qr/^0; $/ms, 'get entries - clear'); - -############################################################################### - -sub check { - my ($uri, $like) = @_; - - my $r = http_get($uri); - - return unless ($r =~ $like && $r =~ /X-Process: (\d+)/); - - return 1 if $^O eq 'MSWin32'; # only one active worker process - - my $pid = $1; - - for (1 .. 25) { - $r = http_get($uri); - - return unless ($r =~ $like && $r =~ /X-Process: (\d+)/); - return 1 if $pid != $1; - } -} - -sub http_delete { - my ($url, %extra) = @_; - return http(<new()->has(qw/http proxy/)->plan(2) - ->write_file_expand('nginx.conf', <<"EOF"); - -%%TEST_GLOBALS%% - -daemon off; - -events { -} - -http { - %%TEST_GLOBALS_HTTP%% - - upstream u { - server 127.0.0.1:8081; - custom 32; - } - - server { - listen 127.0.0.1:8080; - server_name localhost; - - error_log %%TESTDIR%%/e_debug.log debug; - - location / { - proxy_pass http://u; - } - } - - server { - listen 127.0.0.1:8081; - server_name localhost; - - location / { } - } -} - -EOF - -$t->write_file('index.html', ''); -$t->run(); - -############################################################################### - -like(http_get('/'), qr/200 OK/, 'custom upstream'); - -$t->stop(); - -SKIP: { - skip "no --with-debug", 1 unless $t->has_module('--with-debug'); - - like($t->read_file('e_debug.log'), qr/CUSTOM UPSTREAM request/, - 'log - custom upstream'); -} - -############################################################################### diff --git a/rust/vendor/ngx-rust/examples/upstream.conf b/rust/vendor/ngx-rust/examples/upstream.conf deleted file mode 100644 index 5a56b0d..0000000 --- a/rust/vendor/ngx-rust/examples/upstream.conf +++ /dev/null @@ -1,24 +0,0 @@ -# example configuration block to test upstream.rs -http { - upstream backend { - server localhost:15501; - custom 32; - } - - server { - listen 15500; - server_name _; - - location / { - proxy_pass http://backend; - } - } - - server { - listen 15501; - - location / { - return 418; - } - } -} diff --git a/rust/vendor/ngx-rust/examples/upstream.rs b/rust/vendor/ngx-rust/examples/upstream.rs deleted file mode 100644 index 544ca1b..0000000 --- a/rust/vendor/ngx-rust/examples/upstream.rs +++ /dev/null @@ -1,331 +0,0 @@ -/* - * This example is based on: - * https://github.com/gabihodoroaga/nginx-upstream-module - * as well as the NGINX keepalive module: ngx_http_upstream_keepalive_module.c. - * - * The NGINX authors are grateful to @gabihodoroaga for their contributions - * to the community at large. - */ -use core::ffi::{c_char, c_void}; -use core::mem; -use core::ptr; - -use ngx::core::{Pool, Status}; -use ngx::ffi::{ - NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE, - NGX_HTTP_SRV_CONF_OFFSET, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, ngx_atoi, ngx_command_t, - ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt, ngx_event_get_peer_pt, ngx_http_module_t, - ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin, - ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_int_t, ngx_module_t, - ngx_peer_connection_t, ngx_str_t, ngx_uint_t, -}; -use ngx::http::{HttpModule, Merge, MergeConfigError, Request}; -use ngx::http::{HttpModuleServerConf, NgxHttpUpstreamModule}; -use ngx::{ - http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask, - ngx_string, -}; - -#[derive(Clone, Copy, Debug)] -#[repr(C)] -struct SrvConfig { - max: u32, - - original_init_upstream: ngx_http_upstream_init_pt, - original_init_peer: ngx_http_upstream_init_peer_pt, -} - -impl Default for SrvConfig { - fn default() -> Self { - SrvConfig { max: u32::MAX, original_init_upstream: None, original_init_peer: None } - } -} - -impl Merge for SrvConfig { - fn merge(&mut self, _prev: &SrvConfig) -> Result<(), MergeConfigError> { - Ok(()) - } -} - -#[derive(Clone, Copy, Debug)] -#[repr(C)] -struct UpstreamPeerData { - conf: Option<*const SrvConfig>, - upstream: Option<*mut ngx_http_upstream_t>, - client_connection: Option<*mut ngx_connection_t>, - original_get_peer: ngx_event_get_peer_pt, - original_free_peer: ngx_event_free_peer_pt, - data: *mut c_void, -} - -impl Default for UpstreamPeerData { - fn default() -> Self { - UpstreamPeerData { - conf: None, - upstream: None, - client_connection: None, - original_get_peer: None, - original_free_peer: None, - data: ptr::null_mut(), - } - } -} - -static NGX_HTTP_UPSTREAM_CUSTOM_CTX: ngx_http_module_t = ngx_http_module_t { - preconfiguration: Some(Module::preconfiguration), - postconfiguration: Some(Module::postconfiguration), - create_main_conf: None, - init_main_conf: None, - create_srv_conf: Some(Module::create_srv_conf), - merge_srv_conf: Some(Module::merge_srv_conf), - create_loc_conf: None, - merge_loc_conf: None, -}; - -static mut NGX_HTTP_UPSTREAM_CUSTOM_COMMANDS: [ngx_command_t; 2] = [ - ngx_command_t { - name: ngx_string!("custom"), - type_: (NGX_HTTP_UPS_CONF | NGX_CONF_NOARGS | NGX_CONF_TAKE1) as ngx_uint_t, - set: Some(ngx_http_upstream_commands_set_custom), - conf: NGX_HTTP_SRV_CONF_OFFSET, - offset: 0, - post: ptr::null_mut(), - }, - ngx_command_t::empty(), -]; - -// Generate the `ngx_modules` table with exported modules. -// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. -#[cfg(feature = "export-modules")] -ngx::ngx_modules!(ngx_http_upstream_custom_module); - -#[used] -#[allow(non_upper_case_globals)] -#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))] -pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t { - ctx: &raw const NGX_HTTP_UPSTREAM_CUSTOM_CTX as _, - commands: unsafe { &raw mut NGX_HTTP_UPSTREAM_CUSTOM_COMMANDS[0] }, - type_: NGX_HTTP_MODULE as _, - ..ngx_module_t::default() -}; - -// http_upstream_init_custom_peer -// The module's custom peer.init callback. On HTTP request the peer upstream get and free callbacks -// are saved into peer data and replaced with this module's custom callbacks. -http_upstream_init_peer_pt!( - http_upstream_init_custom_peer, - |request: &mut Request, us: *mut ngx_http_upstream_srv_conf_t| { - ngx_log_debug_http!(request, "CUSTOM UPSTREAM request peer init"); - - let hcpd = request.pool().alloc_type::(); - if hcpd.is_null() { - return Status::NGX_ERROR; - } - - // SAFETY: this function is called with non-NULL uf always - let us = unsafe { &mut *us }; - let hccf = match Module::server_conf(us) { - Some(x) => x, - None => return Status::NGX_ERROR, - }; - - let original_init_peer = hccf.original_init_peer.unwrap(); - if unsafe { original_init_peer(request.into(), us) != Status::NGX_OK.into() } { - return Status::NGX_ERROR; - } - - let maybe_upstream = request.upstream(); - if maybe_upstream.is_none() { - return Status::NGX_ERROR; - } - let upstream_ptr = maybe_upstream.unwrap(); - - unsafe { - (*hcpd).conf = Some(hccf); - (*hcpd).upstream = maybe_upstream; - (*hcpd).data = (*upstream_ptr).peer.data; - (*hcpd).client_connection = Some(request.connection()); - (*hcpd).original_get_peer = (*upstream_ptr).peer.get; - (*hcpd).original_free_peer = (*upstream_ptr).peer.free; - - (*upstream_ptr).peer.data = hcpd as *mut c_void; - (*upstream_ptr).peer.get = Some(ngx_http_upstream_get_custom_peer); - (*upstream_ptr).peer.free = Some(ngx_http_upstream_free_custom_peer); - } - - ngx_log_debug_http!(request, "CUSTOM UPSTREAM end request peer init"); - Status::NGX_OK - } -); - -// ngx_http_usptream_get_custom_peer -// For demonstration purposes, use the original get callback, but log this callback proxies through -// to the original. -unsafe extern "C" fn ngx_http_upstream_get_custom_peer( - pc: *mut ngx_peer_connection_t, - data: *mut c_void, -) -> ngx_int_t { - let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) }; - - ngx_log_debug_mask!( - DebugMask::Http, - unsafe { (*pc).log }, - "CUSTOM UPSTREAM get peer, try: {}, conn: {:p}", - unsafe { (*pc).tries }, - unsafe { (*hcpd).client_connection.unwrap() }, - ); - - let original_get_peer = unsafe { (*hcpd).original_get_peer.unwrap() }; - let rc = unsafe { original_get_peer(pc, (*hcpd).data) }; - - if rc != Status::NGX_OK.into() { - return rc; - } - - ngx_log_debug_mask!(DebugMask::Http, unsafe { (*pc).log }, "CUSTOM UPSTREAM end get peer"); - Status::NGX_OK.into() -} - -// ngx_http_upstream_free_custom_peer -// For demonstration purposes, use the original free callback, but log this callback proxies -// through to the original. -unsafe extern "C" fn ngx_http_upstream_free_custom_peer( - pc: *mut ngx_peer_connection_t, - data: *mut c_void, - state: ngx_uint_t, -) { - ngx_log_debug_mask!(DebugMask::Http, unsafe { (*pc).log }, "CUSTOM UPSTREAM free peer"); - - let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) }; - - let original_free_peer = unsafe { (*hcpd).original_free_peer.unwrap() }; - - unsafe { original_free_peer(pc, (*hcpd).data, state) }; - - ngx_log_debug_mask!(DebugMask::Http, unsafe { (*pc).log }, "CUSTOM UPSTREAM end free peer"); -} - -// ngx_http_upstream_init_custom -// The module's custom `peer.init_upstream` callback. -// The original callback is saved in our SrvConfig data and reset to this module's `peer.init`. -unsafe extern "C" fn ngx_http_upstream_init_custom( - cf: *mut ngx_conf_t, - us: *mut ngx_http_upstream_srv_conf_t, -) -> ngx_int_t { - ngx_log_debug_mask!( - DebugMask::Http, - unsafe { (*cf).log }, - "CUSTOM UPSTREAM peer init_upstream" - ); - - // SAFETY: this function is called with non-NULL uf always - let us = unsafe { &mut *us }; - let hccf = Module::server_conf_mut(us); - if hccf.is_none() { - ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM no upstream srv_conf"); - return isize::from(Status::NGX_ERROR); - } - let hccf = hccf.unwrap(); - // NOTE: ngx_conf_init_uint_value macro is unavailable - if hccf.max == u32::MAX { - hccf.max = 100; - } - - let init_upstream_ptr = hccf.original_init_upstream.unwrap(); - if unsafe { init_upstream_ptr(cf, us) } != Status::NGX_OK.into() { - ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM failed calling init_upstream"); - return isize::from(Status::NGX_ERROR); - } - - hccf.original_init_peer = us.peer.init; - us.peer.init = Some(http_upstream_init_custom_peer); - - ngx_log_debug_mask!( - DebugMask::Http, - unsafe { (*cf).log }, - "CUSTOM UPSTREAM end peer init_upstream" - ); - isize::from(Status::NGX_OK) -} - -// ngx_http_upstream_commands_set_custom -// Entry point for the module, if this command is set our custom upstreams take effect. -// The original upstream initializer function is saved and replaced with this module's initializer. -unsafe extern "C" fn ngx_http_upstream_commands_set_custom( - cf: *mut ngx_conf_t, - cmd: *mut ngx_command_t, - conf: *mut c_void, -) -> *mut c_char { - // SAFETY: this function is called with non-NULL cf always - let cf = unsafe { &mut *cf }; - ngx_log_debug_mask!(DebugMask::Http, cf.log, "CUSTOM UPSTREAM module init"); - let args: &[ngx_str_t] = unsafe { (*cf.args).as_slice() }; - - let ccf = unsafe { &mut (*(conf as *mut SrvConfig)) }; - - if let Some(value) = args.get(1) { - let n = unsafe { ngx_atoi(value.data, value.len) }; - if n == (NGX_ERROR as isize) || n == 0 { - ngx_conf_log_error!( - NGX_LOG_EMERG, - cf, - "invalid value \"{}\" in \"{}\" directive", - value, - unsafe { &(*cmd).name } - ); - return ngx::core::NGX_CONF_ERROR; - } - ccf.max = n as u32; - } - - let uscf = NgxHttpUpstreamModule::server_conf_mut(cf).expect("http upstream srv conf"); - - ccf.original_init_upstream = if uscf.peer.init_upstream.is_some() { - uscf.peer.init_upstream - } else { - Some(ngx_http_upstream_init_round_robin) - }; - - uscf.peer.init_upstream = Some(ngx_http_upstream_init_custom); - - ngx_log_debug_mask!(DebugMask::Http, cf.log, "CUSTOM UPSTREAM end module init"); - - ngx::core::NGX_CONF_OK -} - -// The upstream module. -// Only server blocks are supported to trigger the module command; therefore, the only callback -// implemented is our `create_srv_conf` method. -struct Module; - -impl HttpModule for Module { - fn module() -> &'static ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_upstream_custom_module) } - } - - unsafe extern "C" fn create_srv_conf(cf: *mut ngx_conf_t) -> *mut c_void { - let pool = unsafe { Pool::from_ngx_pool((*cf).pool) }; - let conf = pool.alloc_type::(); - if conf.is_null() { - ngx_conf_log_error!( - NGX_LOG_EMERG, - cf, - "CUSTOM UPSTREAM could not allocate memory for config" - ); - return ptr::null_mut(); - } - - unsafe { (*conf).max = NGX_CONF_UNSET as u32 }; - - ngx_log_debug_mask!( - DebugMask::Http, - unsafe { (*cf).log }, - "CUSTOM UPSTREAM end create_srv_conf" - ); - conf as *mut c_void - } -} - -unsafe impl HttpModuleServerConf for Module { - type ServerConf = SrvConfig; -} diff --git a/rust/vendor/ngx-rust/misc/lsan-suppressions.txt b/rust/vendor/ngx-rust/misc/lsan-suppressions.txt deleted file mode 100644 index 4731e9b..0000000 --- a/rust/vendor/ngx-rust/misc/lsan-suppressions.txt +++ /dev/null @@ -1,17 +0,0 @@ -# LeakSanitizer suppressions list for nginx -# -# To be used with -fsanitize=address and -# LSAN_OPTIONS=suppressions=lsan-suppressions.txt. -# -# https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions - -# cycle->connections, cycle->read_events, cycle->write_events -leak:ngx_event_process_init - -# XXX: can silence leaks from nginx SSL callbacks -leak:SSL_do_handshake -leak:SSL_read - -# rcf->ranges not freed at process exit -leak:ngx_http_upstream_update_random -leak:ngx_stream_upstream_update_random diff --git a/rust/vendor/ngx-rust/misc/nginx-sanitizer-support.patch b/rust/vendor/ngx-rust/misc/nginx-sanitizer-support.patch deleted file mode 100644 index 02ba70d..0000000 --- a/rust/vendor/ngx-rust/misc/nginx-sanitizer-support.patch +++ /dev/null @@ -1,28 +0,0 @@ -diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h -index 713eb42a7..999c6b25f 100644 ---- a/src/core/ngx_string.h -+++ b/src/core/ngx_string.h -@@ -96,6 +96,23 @@ void ngx_explicit_memzero(void *buf, size_t n); - void *ngx_memcpy(void *dst, const void *src, size_t n); - #define ngx_cpymem(dst, src, n) (((u_char *) ngx_memcpy(dst, src, n)) + (n)) - -+#elif (NGX_SUPPRESS_WARN) -+ -+/* -+ * Checked versions for sanitizers. -+ * See https://mailman.nginx.org/pipermail/nginx-devel/2023-December/7VNQZEBNXEKAYTYE4Y65FORF4HNELM6V.html -+ */ -+ -+static ngx_inline void * -+ngx_memcpy(void *dst, const void *src, size_t n) { -+ return (n == 0) ? dst : memcpy(dst, src, n); -+} -+ -+static ngx_inline void * -+ngx_cpymem(void *dst, const void *src, size_t n) { -+ return (n == 0) ? dst : ((u_char *) memcpy(dst, src, n)) + n; -+} -+ - #else - - /* diff --git a/rust/vendor/ngx-rust/misc/ubsan-suppressions.txt b/rust/vendor/ngx-rust/misc/ubsan-suppressions.txt deleted file mode 100644 index 5da588f..0000000 --- a/rust/vendor/ngx-rust/misc/ubsan-suppressions.txt +++ /dev/null @@ -1,28 +0,0 @@ -# UndefinedBehaviorSanitizer suppressions list for nginx -# -# To be used with -fsanitize=undefined and -# UBSAN_OPTIONS=suppressions=ubsan-suppressions.txt. -# -# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#runtime-suppressions - -# src/http/v2/ngx_http_v2.c:2747:17: runtime error: store to misaligned address 0x7b35a1a19885 for type 'uint32_t' (aka 'unsigned int'), which requires 4 byte alignment -alignment:src/http/v2/ngx_http_v2.c -alignment:src/http/v2/ngx_http_v2_filter_module.c -alignment:ngx_http_huff_encode -alignment:ngx_http_parse_request_line - -# ngx_quic_write_uint32 at src/event/quic/ngx_event_quic_transport.c:642 -alignment:ngx_quic_create_long_header -alignment:ngx_quic_read_uint32 - -# src/core/ngx_output_chain.c:70:20: runtime error: call to function ngx_http_trailers_filter through pointer to incorrect function type 'long (*)(void *, struct ngx_chain_s *)' -# src/http/modules/ngx_http_headers_filter_module.c:249: note: ngx_http_trailers_filter defined here -function:ngx_output_chain - -# violates nonnull on memcmp -nonnull-attribute:ngx_http_upstream_zone_preresolve -nonnull-attribute:ngx_stream_upstream_zone_preresolve - -# src/http/ngx_http_script.c:800:16: runtime error: applying non-zero offset 112 to null pointer -pointer-overflow:ngx_http_script_add_code -pointer-overflow:ngx_stream_script_add_code diff --git a/rust/vendor/ngx-rust/misc/update-action-sha.sh b/rust/vendor/ngx-rust/misc/update-action-sha.sh deleted file mode 100755 index 30e8390..0000000 --- a/rust/vendor/ngx-rust/misc/update-action-sha.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -set -ex - -if [ $# -lt 2 ]; then - grep "uses:[[:space:]]*$1" .github/workflows/*.yaml - exit -fi - -sed -e "s|\\(uses:[[:space:]]*$1@\\).*|\\1$2|" -i .github/workflows/*.yaml diff --git a/rust/vendor/ngx-rust/nginx-src/Cargo.toml b/rust/vendor/ngx-rust/nginx-src/Cargo.toml deleted file mode 100644 index 0db238e..0000000 --- a/rust/vendor/ngx-rust/nginx-src/Cargo.toml +++ /dev/null @@ -1,30 +0,0 @@ -[package] -name = "nginx-src" -# Version format: ..+ -version = "1.30.0+1.30.4" -# Crate sources are licensed under Apache-2.0, with exception of the -# NGINX submodule that is redistributed under BSD-2-Clause. -license = "Apache-2.0 AND BSD-2-Clause" -description = "Source of NGINX" -keywords = ["nginx", "module", "sys"] -exclude = [ - "/nginx/.git*", - "/nginx/contrib", - "/nginx/docs", - "/nginx/misc", - "!/nginx/docs/html", - "!/nginx/docs/man", -] -edition.workspace = true -homepage.workspace = true -repository.workspace = true -rust-version.workspace = true - -[dependencies] -duct = "1" -flate2 = "1.0.32" -tar = "0.4.45" -ureq = "3.2" - -[lints] -workspace = true diff --git a/rust/vendor/ngx-rust/nginx-src/LICENSE b/rust/vendor/ngx-rust/nginx-src/LICENSE deleted file mode 120000 index ea5b606..0000000 --- a/rust/vendor/ngx-rust/nginx-src/LICENSE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE \ No newline at end of file diff --git a/rust/vendor/ngx-rust/nginx-src/README.md b/rust/vendor/ngx-rust/nginx-src/README.md deleted file mode 100644 index 334a44d..0000000 --- a/rust/vendor/ngx-rust/nginx-src/README.md +++ /dev/null @@ -1,82 +0,0 @@ -# nginx-src - -The crate contains a vendored copy of the NGINX source and a logic to build it. -It is intended to be consumed by the [nginx-sys] crate for CI builds, tests or -rustdoc generation. - -It is notably not intended for producing binaries suitable for production use. -For such scenarios we recommend building the `ngx-rust` based module against -the packages from or your preferred distribution. -See the [nginx-sys] documentation for building `ngx-rust` modules against an -existing pre-configured NGINX source tree. - -[nginx-sys]: https://docs.rs/nginx-sys/ - -## Versioning - -This crate follows the latest stable branch of NGINX. - - * The major and minor fields are taken from the NGINX version. - * The patch version is incremented on changes to the build logic or crate - metadata. - * The version metadata contains full version of NGINX. - -## Build Requirements - -The crate can be built on common Unix-like operating systems and requires all -the usual NGINX build dependencies (including development headers for the -libraries) installed in system paths: - - * C compiler and toolchain - * SSL library, OpenSSL or LibreSSL - * PCRE or PCRE2 - * Zlib or zlib-ng witn Zlib compatible API enabled - -We don't intend to support Windows at the moment, as NGINX does not support -dynamic modules for this target. - -## Environment variables - -Following variables can be set to customize the build. - - * `NGX_CONFIGURE_ARGS` — additional arguments to pass to the NGINX configure - script. - - Example: `export NGX_CONFIGURE_ARGS='--with-debug'; cargo build` - - * `NGX_CFLAGS`, `NGX_LDFLAGS` — additional C compiler and linker flags to - pass to the NGINX configure script. Internally, this is added to the - `--with-cc-opt=...` and `--with-ld-opt=...` configure arguments. - - Example: - ```sh - export NGX_CFLAGS='-I/opt/boringssl/include' - export NGX_LDFLAGS='-L/opt/boringssl/build -lstdc++' - cargo build - ``` - -## Download NGINX and dependency sources during build - -While we recommend using the system libraries, it is still possible to opt into -downloading the NGINX itself and the dependency sources from the network with -the help of the following variables: - - * `NGX_VERSION` — if specified, the version of NGINX to download and build - instead of the one bundled with the crate. - * `OPENSSL_VERSION` — if specified, the version of OpenSSL to download and use - use instead of the system-provided library. - * `PCRE2_VERSION` — if specified, the version of PCRE2 to download and use - instead of the system-provided library. - * `ZLIB_VERSION` — if specified, the version of Zlib to download and use - instead of the system-provided library. - -If the `gpg` executable is present in the path, the build script will verify -the integrity of the downloaded files using GPG signatures and a known set of -public keys. -This behavior can be disabled by setting `NGX_NO_SIGNATURE_CHECK`. - -## License - -The code in this crate is licensed under the [Apache License 2.0](../LICENSE). -The crate also contains the source code of NGINX, distributed under the -[BSD 2-Clause License](https://nginx.org/LICENSE). diff --git a/rust/vendor/ngx-rust/nginx-src/src/download.rs b/rust/vendor/ngx-rust/nginx-src/src/download.rs deleted file mode 100644 index c64ca6c..0000000 --- a/rust/vendor/ngx-rust/nginx-src/src/download.rs +++ /dev/null @@ -1,252 +0,0 @@ -extern crate duct; - -use core::error::Error as StdError; -use std::fs::File; -use std::io; -use std::path::{Path, PathBuf}; -use std::sync::LazyLock; -use std::{env, fs}; - -use flate2::read::GzDecoder; -use tar::Archive; - -use crate::verifier::SignatureVerifier; - -const NGINX_URL_PREFIX: &str = "https://nginx.org/download"; -const OPENSSL_URL_PREFIX: &str = "https://github.com/openssl/openssl/releases/download"; -const PCRE1_URL_PREFIX: &str = "https://sourceforge.net/projects/pcre/files/pcre"; -const PCRE2_URL_PREFIX: &str = "https://github.com/PCRE2Project/pcre2/releases/download"; -const ZLIB_URL_PREFIX: &str = "https://github.com/madler/zlib/releases/download"; -const UBUNTU_KEYSEVER: &str = "hkps://keyserver.ubuntu.com"; - -struct SourceSpec<'a> { - url: fn(&str) -> String, - variable: &'a str, - signature: &'a str, - keyserver: &'a str, - key_ids: &'a [&'a str], -} - -const NGINX_SOURCE: SourceSpec = SourceSpec { - url: |version| format!("{NGINX_URL_PREFIX}/nginx-{version}.tar.gz"), - variable: "NGX_VERSION", - signature: "asc", - keyserver: UBUNTU_KEYSEVER, - key_ids: &[ - // Key 1: Konstantin Pavlov's public key. For Nginx 1.25.3 and earlier - "13C82A63B603576156E30A4EA0EA981B66B0D967", - // Key 2: Sergey Kandaurov's public key. For Nginx 1.25.4 - "D6786CE303D9A9022998DC6CC8464D549AF75C0A", - // Key 3: Maxim Dounin's public key. At least used for Nginx 1.18.0 - "B0F4253373F8F6F510D42178520A9993A1C052F8", - // Key 4: Roman Arutyunyan's public key. For Nginx 1.25.5 - "43387825DDB1BB97EC36BA5D007C8D7C15D87369", - ], -}; - -const DEPENDENCIES: &[(&str, SourceSpec)] = &[ - ( - "openssl", - SourceSpec { - url: |version| { - if version.starts_with("1.") { - let ver_hyphened = version.replace('.', "_"); - format!("{OPENSSL_URL_PREFIX}/OpenSSL_{ver_hyphened}/openssl-{version}.tar.gz") - } else { - format!("{OPENSSL_URL_PREFIX}/openssl-{version}/openssl-{version}.tar.gz") - } - }, - variable: "OPENSSL_VERSION", - signature: "asc", - keyserver: UBUNTU_KEYSEVER, - key_ids: &[ - "EFC0A467D613CB83C7ED6D30D894E2CE8B3D79F5", - "A21FAB74B0088AA361152586B8EF1A6BA9DA2D5C", - "8657ABB260F056B1E5190839D9C4D26D0E604491", - "B7C1C14360F353A36862E4D5231C84CDDCC69C45", - "95A9908DDFA16830BE9FB9003D30A3A9FF1360DC", - "7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C", - "E5E52560DD91C556DDBDA5D02064C53641C25E5D", - "C1F33DD8CE1D4CC613AF14DA9195C48241FBF7DD", - "BA5473A2B0587B07FB27CF2D216094DFD0CB81EF", - ], - }, - ), - ( - "pcre", - SourceSpec { - url: |version| { - // We can distinguish pcre1/pcre2 by checking whether the second character is '.', - // because the final version of pcre1 is 8.45 and the first one of pcre2 is 10.00. - if version.chars().nth(1).is_some_and(|c| c == '.') { - format!("{PCRE1_URL_PREFIX}/{version}/pcre-{version}.tar.gz") - } else { - format!("{PCRE2_URL_PREFIX}/pcre2-{version}/pcre2-{version}.tar.gz") - } - }, - variable: "PCRE2_VERSION", - signature: "sig", - keyserver: UBUNTU_KEYSEVER, - key_ids: &[ - // Key 1: Phillip Hazel's public key. For PCRE2 10.44 and earlier - "45F68D54BBE23FB3039B46E59766E084FB0F43D8", - // Key 2: Nicholas Wilson's public key. For PCRE2 10.45 - "A95536204A3BB489715231282A98E77EB6F24CA8", - ], - }, - ), - ( - "zlib", - SourceSpec { - url: |version| format!("{ZLIB_URL_PREFIX}/v{version}/zlib-{version}.tar.gz"), - variable: "ZLIB_VERSION", - signature: "asc", - keyserver: UBUNTU_KEYSEVER, - key_ids: &[ - // Key 1: Mark Adler's public key. For zlib 1.3.1 and earlier - "5ED46A6721D365587791E2AA783FCD8E58BCAFBA", - ], - }, - ), -]; - -static VERIFIER: LazyLock> = LazyLock::new(|| { - SignatureVerifier::new().inspect_err(|err| eprintln!("GnuPG verifier: {err}")).ok() -}); - -static CACHE_DIR: LazyLock = LazyLock::new(|| { - let base_dir = env::var("OUT_DIR") - .map(PathBuf::from) - .unwrap_or_else(|_| env::current_dir().expect("Failed to get current directory")); - // Choose `.cache` relative to the OUT_DIR of the caller (nginx-sys) as the default cache - // directory. Environment variable `CACHE_DIR` overrides this. - // Recommendation: set env "CACHE_DIR = { value = ".cache", relative = true }" in - // `.cargo/config.toml` in your project - let cache_dir = env::var("CACHE_DIR").map(PathBuf::from).unwrap_or(base_dir.join(".cache")); - - if !cache_dir.exists() { - fs::create_dir_all(&cache_dir) - .map_err(|err| format!("Failed to create {cache_dir:?}: {err}")) - .unwrap(); - } - - cache_dir -}); - -/// Downloads a tarball from the specified URL into the `.cache` directory. -fn download(cache_dir: &Path, url: &str) -> Result> { - fn proceed_with_download(file_path: &Path) -> bool { - // File does not exist or is zero bytes - !file_path.exists() || file_path.metadata().is_ok_and(|m| m.len() < 1) - } - let filename = url.split('/').next_back().unwrap(); - let file_path = cache_dir.join(filename); - if proceed_with_download(&file_path) { - println!("Downloading: {} -> {}", url, file_path.display()); - let mut response = ureq::get(url).call()?; - let mut reader = response.body_mut().as_reader(); - let mut file = File::create(&file_path)?; - std::io::copy(&mut reader, &mut file)?; - } - - if !file_path.exists() { - return Err( - format!("Downloaded file was not written to the expected location: {url}",).into() - ); - } - Ok(file_path) -} - -/// Gets a given tarball and signature file from a remote URL and copies it to the `.cache` -/// directory. -fn get_archive(cache_dir: &Path, source: &SourceSpec, version: &str) -> io::Result { - let archive_url = (source.url)(version); - let archive = download(cache_dir, &archive_url).map_err(io::Error::other)?; - - if let Some(verifier) = &*VERIFIER { - let signature = format!("{archive_url}.{}", source.signature); - - let verify = || -> io::Result<()> { - let signature = download(cache_dir, &signature).map_err(io::Error::other)?; - verifier.import_keys(source.keyserver, source.key_ids)?; - verifier.verify_signature(&archive, &signature)?; - Ok(()) - }; - - if let Err(err) = verify() { - let _ = fs::remove_file(&archive); - let _ = fs::remove_file(&signature); - return Err(err); - } - } - - Ok(archive) -} - -/// Extracts a tarball into a subdirectory based on the tarball's name under the source base -/// directory. -fn extract_archive(archive_path: &Path, extract_output_base_dir: &Path) -> io::Result { - if !extract_output_base_dir.exists() { - fs::create_dir_all(extract_output_base_dir)?; - } - let archive_file = File::open(archive_path) - .unwrap_or_else(|_| panic!("Unable to open archive file: {}", archive_path.display())); - let stem = archive_path - .file_name() - .and_then(|s| s.to_str()) - .and_then(|s| s.rsplitn(3, '.').last()) - .expect("Unable to determine archive file name stem"); - - let extract_output_dir = extract_output_base_dir.to_owned(); - let archive_output_dir = extract_output_dir.join(stem); - if !archive_output_dir.exists() { - Archive::new(GzDecoder::new(archive_file)).entries()?.filter_map(|e| e.ok()).for_each( - |mut entry| { - let path = entry.path().unwrap(); - let stripped_path = path.components().skip(1).collect::(); - entry.unpack(archive_output_dir.join(stripped_path)).unwrap(); - }, - ); - } else { - println!( - "Archive [{}] already extracted to directory: {}", - stem, - archive_output_dir.display() - ); - } - - Ok(archive_output_dir) -} - -/// Downloads and extracts all requested sources. -pub fn prepare(source_dir: &Path, build_dir: &Path) -> io::Result<(PathBuf, Vec)> { - let extract_output_base_dir = build_dir.join("lib"); - if !extract_output_base_dir.exists() { - fs::create_dir_all(&extract_output_base_dir)?; - } - - let mut options = vec![]; - - // Download NGINX only if NGX_VERSION is set. - let source_dir = if let Ok(version) = env::var(NGINX_SOURCE.variable) { - let archive_path = get_archive(&CACHE_DIR, &NGINX_SOURCE, version.as_str())?; - let output_base_dir: PathBuf = env::var("OUT_DIR").unwrap().into(); - extract_archive(&archive_path, &output_base_dir)? - } else { - source_dir.to_path_buf() - }; - - for (name, source) in DEPENDENCIES { - // Download dependencies if a corresponding DEPENDENCY_VERSION is set. - let Ok(requested) = env::var(source.variable) else { - continue; - }; - - let archive_path = get_archive(&CACHE_DIR, source, &requested)?; - let output_dir = extract_archive(&archive_path, &extract_output_base_dir)?; - let output_dir = output_dir.to_string_lossy(); - options.push(format!("--with-{name}={output_dir}")); - } - - Ok((source_dir, options)) -} diff --git a/rust/vendor/ngx-rust/nginx-src/src/lib.rs b/rust/vendor/ngx-rust/nginx-src/src/lib.rs deleted file mode 100644 index 52a4e60..0000000 --- a/rust/vendor/ngx-rust/nginx-src/src/lib.rs +++ /dev/null @@ -1,214 +0,0 @@ -#![doc = include_str!("../README.md")] -#![warn(missing_docs)] - -use std::ffi::OsString; -use std::path::{Path, PathBuf}; -use std::process::Output; -use std::{env, io, thread}; - -mod download; -mod verifier; - -static NGINX_DEFAULT_SOURCE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/nginx"); - -const NGINX_BUILD_INFO: &str = "last-build-info"; -const NGINX_BINARY: &str = "nginx"; - -static NGINX_CONFIGURE_BASE: &[&str] = &[ - "--with-compat", - "--with-http_realip_module", - "--with-http_ssl_module", - "--with-http_v2_module", - "--with-mail", - "--with-mail_ssl_module", - "--with-stream", - "--with-stream_realip_module", - "--with-stream_ssl_module", - "--with-threads", -]; - -const ENV_VARS_TRIGGERING_RECOMPILE: [&str; 10] = [ - "CACHE_DIR", - "CARGO_MANIFEST_DIR", - "CARGO_TARGET_TMPDIR", - "NGX_CONFIGURE_ARGS", - "NGX_CFLAGS", - "NGX_LDFLAGS", - "NGX_VERSION", - "OPENSSL_VERSION", - "PCRE2_VERSION", - "ZLIB_VERSION", -]; - -/* -########################################################################### -# NGINX Build Functions - Everything below here is for building NGINX # -########################################################################### - -In order to build Rust bindings for NGINX using the bindgen crate, we need -to do the following: - - 1. Obtain a copy of the NGINX source code and the necessary dependencies: - OpenSSL, PCRE2, Zlib. - 3. Run autoconf `configure` for NGINX. - 4. Compile NGINX. - 5. Read the autoconf generated makefile for NGINX and configure bindgen - to generate Rust bindings based on the includes in the makefile. -*/ - -/// Outputs cargo instructions required for using this crate from a buildscript. -pub fn print_cargo_metadata() { - for file in ["lib.rs", "download.rs", "verifier.rs"] { - println!("cargo::rerun-if-changed={path}/src/{file}", path = env!("CARGO_MANIFEST_DIR")) - } - - for var in ENV_VARS_TRIGGERING_RECOMPILE { - println!("cargo::rerun-if-env-changed={var}"); - } -} - -/// Builds a copy of NGINX sources, either bundled with the crate or downloaded from the network. -pub fn build(build_dir: impl AsRef) -> io::Result<(PathBuf, PathBuf)> { - let source_dir = PathBuf::from(NGINX_DEFAULT_SOURCE_DIR); - let build_dir = build_dir.as_ref().to_owned(); - - let (source_dir, vendored_flags) = download::prepare(&source_dir, &build_dir)?; - - let flags = nginx_configure_flags(&vendored_flags); - - configure(&source_dir, &build_dir, &flags)?; - - make(&source_dir, &build_dir, ["build"])?; - - Ok((source_dir, build_dir)) -} - -/// Returns the options NGINX was built with -fn build_info(source_dir: &Path, configure_flags: &[String]) -> String { - // Flags should contain strings pointing to OS/platform as well as dependency versions, - // so if any of that changes, it can trigger a rebuild - format!("{:?}|{}", source_dir, configure_flags.join(" ")) -} - -/// Generate the flags to use with autoconf `configure` for NGINX. -fn nginx_configure_flags(vendored: &[String]) -> Vec { - let mut nginx_opts: Vec = - NGINX_CONFIGURE_BASE.iter().map(|x| String::from(*x)).collect(); - - nginx_opts.extend(vendored.iter().map(Into::into)); - - if let Ok(extra_args) = env::var("NGX_CONFIGURE_ARGS") { - // FIXME: shell style split? - nginx_opts.extend(extra_args.split_whitespace().map(Into::into)); - } - - if let Ok(cflags) = env::var("NGX_CFLAGS") { - nginx_opts.push(format!("--with-cc-opt={cflags}")); - } - - if let Ok(ldflags) = env::var("NGX_LDFLAGS") { - nginx_opts.push(format!("--with-ld-opt={ldflags}")); - } - - nginx_opts -} - -/// Runs external process invoking autoconf `configure` for NGINX. -fn configure(source_dir: &Path, build_dir: &Path, flags: &[String]) -> io::Result<()> { - let build_info = build_info(source_dir, flags); - - if build_dir.join("Makefile").is_file() - && build_dir.join(NGINX_BINARY).is_file() - && matches!( - std::fs::read_to_string(build_dir.join(NGINX_BUILD_INFO)).map(|x| x == build_info), - Ok(true) - ) - { - println!("Build info unchanged, skipping configure"); - return Ok(()); - } - - println!("Using NGINX source at {source_dir:?}"); - - let configure = ["configure", "auto/configure"] - .into_iter() - .map(|x| source_dir.join(x)) - .find(|x| x.is_file()) - .ok_or(io::ErrorKind::NotFound)?; - - println!("Running NGINX configure script with flags: {:?}", flags.join(" ")); - - let mut build_dir_arg: OsString = "--builddir=".into(); - build_dir_arg.push(build_dir); - - let mut flags: Vec = flags.iter().map(|x| x.into()).collect(); - flags.push(build_dir_arg); - - let output = duct::cmd(configure, flags).dir(source_dir).stderr_to_stdout().run()?; - - if !output.status.success() { - println!("configure failed with {:?}", output.status); - return Err(io::ErrorKind::Other.into()); - } - - let _ = std::fs::write(build_dir.join(NGINX_BUILD_INFO), build_info); - - Ok(()) -} - -/// Runs `make` within the NGINX source directory as an external process. -fn make(source_dir: &Path, build_dir: &Path, extra_args: U) -> io::Result -where - U: IntoIterator, - U::Item: Into, -{ - // Level of concurrency to use when building nginx - cargo nicely provides this information - let num_jobs = match env::var("NUM_JOBS") { - Ok(s) => s.parse::().ok(), - Err(_) => thread::available_parallelism().ok().map(|n| n.get()), - } - .unwrap_or(1); - - let mut args = vec![ - OsString::from("-f"), - build_dir.join("Makefile").into(), - OsString::from("-j"), - num_jobs.to_string().into(), - ]; - args.extend(extra_args.into_iter().map(Into::into)); - - // Use MAKE passed from the parent process if set. Otherwise prefer `gmake` as it provides a - // better feature-wise implementation on some systems. - // Notably, we want to avoid SUN make on Solaris (does not support -j) or ancient GNU make 3.81 - // on MacOS. - let inherited = env::var("MAKE"); - let make_commands: &[&str] = match inherited { - Ok(ref x) => &[x.as_str(), "gmake", "make"], - _ => &["gmake", "make"], - }; - - // Give preference to the binary with the name of gmake if it exists because this is typically - // the GNU 4+ on MacOS (if it is installed via homebrew). - for make in make_commands { - /* Use the duct dependency here to merge the output of STDOUT and STDERR into a single stream, - and to provide the combined output as a reader which can be iterated over line-by-line. We - use duct to do this because it is a lot of work to implement this from scratch. */ - let result = duct::cmd(*make, &args).dir(source_dir).stderr_to_stdout().run(); - - match result { - Err(err) if err.kind() == io::ErrorKind::NotFound => { - eprintln!("make: command '{make}' not found"); - continue; - } - Ok(out) if !out.status.success() => { - return Err(io::Error::other(format!( - "make: '{}' failed with {:?}", - make, out.status - ))); - } - _ => return result, - } - } - - Err(io::ErrorKind::NotFound.into()) -} diff --git a/rust/vendor/ngx-rust/nginx-src/src/verifier.rs b/rust/vendor/ngx-rust/nginx-src/src/verifier.rs deleted file mode 100644 index 917c947..0000000 --- a/rust/vendor/ngx-rust/nginx-src/src/verifier.rs +++ /dev/null @@ -1,107 +0,0 @@ -use std::ffi::OsString; -use std::fs::{self, Permissions}; -use std::os::unix::fs::PermissionsExt; -use std::path::{Path, PathBuf}; -use std::{env, io}; - -static GNUPG_COMMAND: &str = "gpg"; - -pub struct SignatureVerifier { - gnupghome: PathBuf, -} - -impl SignatureVerifier { - pub fn new() -> io::Result { - if env::var("NGX_NO_SIGNATURE_CHECK").is_ok() { - return Err(io::Error::other("signature check disabled by user".to_string())); - }; - - if let Err(x) = duct::cmd!(GNUPG_COMMAND, "--version").stdout_null().run() { - return Err(io::Error::other(format!( - "signature check disabled: \"{GNUPG_COMMAND}\" not found ({x})" - ))); - } - - // We do not want to mess with the default gpg data for the running user, - // so we store all gpg data within our build directory. - let gnupghome = - env::var("OUT_DIR").map(PathBuf::from).map_err(io::Error::other)?.join(".gnupg"); - - if !fs::exists(&gnupghome)? { - fs::create_dir_all(&gnupghome)?; - } - - change_permissions_recursively(gnupghome.as_path(), 0o700, 0o600)?; - - Ok(Self { gnupghome }) - } - - /// Imports all the required GPG keys into a temporary directory in order to - /// validate the integrity of the downloaded tarballs. - pub fn import_keys(&self, server: &str, key_ids: &[&str]) -> io::Result<()> { - println!("Importing {} GPG keys for key server: {}", key_ids.len(), server); - - let mut args = vec![ - OsString::from("--homedir"), - self.gnupghome.clone().into(), - OsString::from("--keyserver"), - server.into(), - OsString::from("--recv-keys"), - ]; - args.extend(key_ids.iter().map(OsString::from)); - - let cmd = duct::cmd(GNUPG_COMMAND, &args); - let output = cmd.stderr_to_stdout().stdout_capture().unchecked().run()?; - - if !output.status.success() { - eprintln!("{}", String::from_utf8_lossy(&output.stdout)); - return Err(io::Error::other(format!( - "Command: {:?}\nFailed to import GPG keys: {}", - cmd, - key_ids.join(" ") - ))); - } - - Ok(()) - } - - /// Validates the integrity of a file against the cryptographic signature associated with - /// the file. - pub fn verify_signature(&self, path: &Path, signature: &Path) -> io::Result<()> { - let cmd = - duct::cmd!(GNUPG_COMMAND, "--homedir", &self.gnupghome, "--verify", signature, path); - let output = cmd.stderr_to_stdout().stdout_capture().unchecked().run()?; - if !output.status.success() { - eprintln!("{}", String::from_utf8_lossy(&output.stdout)); - return Err(io::Error::other(format!( - "Command: {:?}\nGPG signature verification of archive failed [{}]", - cmd, - path.display() - ))); - } - Ok(()) - } -} - -fn change_permissions_recursively( - path: &Path, - dir_mode: u32, - file_mode: u32, -) -> std::io::Result<()> { - if path.is_dir() { - // Set directory permissions to 700 - fs::set_permissions(path, Permissions::from_mode(dir_mode))?; - - for entry in fs::read_dir(path)? { - let entry = entry?; - let path = entry.path(); - - change_permissions_recursively(&path, dir_mode, file_mode)?; - } - } else { - // Set file permissions to 600 - fs::set_permissions(path, Permissions::from_mode(file_mode))?; - } - - Ok(()) -} diff --git a/rust/vendor/ngx-rust/nginx-sys/Cargo.toml b/rust/vendor/ngx-rust/nginx-sys/Cargo.toml deleted file mode 100644 index 3faf6c7..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/Cargo.toml +++ /dev/null @@ -1,47 +0,0 @@ -[package] -name = "nginx-sys" -version = "0.5.0" -categories = ["external-ffi-bindings"] -description = "FFI bindings to NGINX" -keywords = ["nginx", "ffi", "sys"] -build = "build/main.rs" -# This field is required to export DEP_NGINX_ vars -# See https://github.com/rust-lang/cargo/issues/3544 -links = "nginx" -edition.workspace = true -license.workspace = true -homepage.workspace = true -repository.workspace = true -rust-version.workspace = true - -[package.metadata.docs.rs] -all-features = true -default-target = "x86_64-unknown-linux-gnu" -targets = [] - -[dependencies] - -[target.'cfg(not(windows))'.dependencies] -errno = { version = "0.3.10", default-features = false } - -[build-dependencies] -bindgen = "0.72.1" -cc = "1.2.0" -dunce = "1.0.5" -regex = "1.11.1" -nginx-src = { version = "~1.30.0", optional = true, path = "../nginx-src" } -shlex = "2" - -[features] -default = ["http"] -# Allows generation of bindings for the HTTP module. -http = [] -# Allows generation of bindings for the Mail module. -mail = [] -# Allows generation of bindings for the Stream module. -stream = [] -# Enables downloading and building a copy of NGINX in the build script. -vendored = ["dep:nginx-src"] - -[lints] -workspace = true diff --git a/rust/vendor/ngx-rust/nginx-sys/LICENSE b/rust/vendor/ngx-rust/nginx-sys/LICENSE deleted file mode 120000 index ea5b606..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/LICENSE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE \ No newline at end of file diff --git a/rust/vendor/ngx-rust/nginx-sys/README.md b/rust/vendor/ngx-rust/nginx-sys/README.md deleted file mode 100644 index 3750a79..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/README.md +++ /dev/null @@ -1,173 +0,0 @@ -# nginx-sys - -The `nginx-sys` crate provides low-level bindings for the nginx C API, allowing -Rust applications to interact with nginx servers and modules. - -## Usage - -Add `nginx-sys` as a dependency in your `Cargo.toml`: - -```toml -[dependencies] -nginx-sys = "0.5.0" -``` - -Set the path to your nginx sources and build directory using the -[environment variables](#input-variables), or enable the `nginx-sys/vendored` -feature to use a downloaded copy of the nginx sources. - -Note that it is recommended to use the exact source and `configure` arguments -of the nginx build you are planning to use in production. -`configure` arguments alter the APIs and the symbols visible to the Rust code, -and some OS distributions are known to ship nginx packages with API-breaking -patches applied. - -## Features - -- `http` - **Enabled** by default. Allows generation of bindings for the HTTP - module, `ngx_http.h`. - The feature will be ignored if NGINX is configured without HTTP support, - thus it may be necessary to check for `http` in - [`DEP_NGINX_FEATURES`](#dep_nginx_features). - -- `mail`: Allows generation of bindings for the Mail module, `ngx_mail.h`. - The feature will be ignored if NGINX is configured without Mail support, - thus it may be necessary to check for `mail` in - [`DEP_NGINX_FEATURES`](#dep_nginx_features). - -- `stream`: Allows generation of bindings for the Stream module, `ngx_stream.h`. - The feature will be ignored if NGINX is configured without Stream support, - thus it may be necessary to check for `stream` in - [`DEP_NGINX_FEATURES`](#dep_nginx_features). - -- `vendored`: Enables the build scripts to download and build a copy of nginx - source and link against it. - See `nginx-src` crate documentation for additional details. - -## Input variables - -`NGINX_SOURCE_DIR`, `NGINX_BUILD_DIR` control paths to an external nginx source -tree and a configured build directory. Normally, specifying either of these is -sufficient, as the build directory defaults to `objs` under the source dir. - -However, it's possible to set the latter to any valid path with -`configure --builddir=<...>`, and we need _both_ paths to support such -configuration. - -The variables above are optional, but take preference when the `vendored` crate -feature is enabled. - -## Output variables - -Following metadata variables are passed to the build scripts of any **direct** -dependents of the package. - -Check the [using another sys crate] and the [links manifest key] sections of the -Cargo Book for more details about passing metadata between packages. - -### `DEP_NGINX_FEATURES` - -nginx has various configuration options that may affect the availability of -functions, constants and structure fields. This is not something that can be -detected at runtime, as an attempt to use a symbol unavailable in the bindings -would result in compilation error. - -`DEP_NGINX_FEATURES_CHECK` contains the full list of feature flags supported -by `nginx-sys`, i.e. everything that can be used for feature checks. -The most common use for this variable is to specify [`cargo::rustc-check-cfg`]. - -`DEP_NGINX_FEATURES` contains the list of features enabled in the version of -nginx being built against. - -An example of a build script with these variables: - -```rust -// Specify acceptable values for `ngx_feature`. -println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES_CHECK"); -println!( - "cargo::rustc-check-cfg=cfg(ngx_feature, values({}))", - std::env::var("DEP_NGINX_FEATURES_CHECK").unwrap_or("any()".to_string()) -); -// Read feature flags detected by nginx-sys and pass to the compiler. -println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES"); -if let Ok(features) = std::env::var("DEP_NGINX_FEATURES") { - for feature in features.split(',').map(str::trim) { - println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature); - } -} -``` - -And an usage example: - -```rust -#[cfg(ngx_feature = "debug")] -println!("this nginx binary was built with debug logging enabled"); -``` - -### `DEP_NGINX_OS` - -Version, as detected by the nginx configuration script. - -`DEP_NGINX_OS_CHECK` contains the full list of supported values, and -`DEP_NGINX_OS` the currently detected one. - -Usage examples: - -```rust -// Specify acceptable values for `ngx_os` -println!("cargo::rerun-if-env-changed=DEP_NGINX_OS_CHECK"); -println!( - "cargo::rustc-check-cfg=cfg(ngx_os, values({}))", - std::env::var("DEP_NGINX_OS_CHECK").unwrap_or("any()".to_string()) -); -// Read operating system detected by nginx-sys and pass to the compiler. -println!("cargo::rerun-if-env-changed=DEP_NGINX_OS"); -if let Ok(os) = std::env::var("DEP_NGINX_OS") { - println!("cargo::rustc-cfg=ngx_os=\"{}\"", os); -} -``` - -```rust -#[cfg(ngx_os = "freebsd")] -println!("this nginx binary was built on FreeBSD"); -``` - -### Version and build information - -- `DEP_NGINX_VERSION_NUMBER`: - a numeric representation with 3 digits for each component: `1026002` -- `DEP_NGINX_VERSION`: - human-readable string in a product/version format: `nginx/1.26.2` -- `DEP_NGINX_BUILD`: - version string with the optional build name (`--build=`) included: - `nginx/1.25.5 (nginx-plus-r32)` - -Usage example: - -```rust -println!("cargo::rustc-check-cfg=cfg(nginx1_27_0)"); -println!("cargo::rerun-if-env-changed=DEP_NGINX_VERSION_NUMBER"); -if let Ok(version) = std::env::var("DEP_NGINX_VERSION_NUMBER") { - let version: u64 = version.parse().unwrap(); - - if version >= 1_027_000 { - println!("cargo::rustc-cfg=nginx1_27_0"); - } -} -``` - -## Examples - -### Get nginx Version - -This example demonstrates how to retrieve the version of the nginx server. - -```rust,no_run -use nginx_sys::nginx_version; - -println!("nginx version: {}", nginx_version); -``` - -[using another sys crate]: https://doc.rust-lang.org/nightly/cargo/reference/build-script-examples.html#using-another-sys-crate -[links manifest key]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#the-links-manifest-key -[`cargo::rustc-check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg diff --git a/rust/vendor/ngx-rust/nginx-sys/src/detail.rs b/rust/vendor/ngx-rust/nginx-sys/src/detail.rs deleted file mode 100644 index c2cbe3f..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/detail.rs +++ /dev/null @@ -1,153 +0,0 @@ -//! Implementation details shared between nginx-sys and ngx. -#![allow(missing_docs)] - -use core::fmt; -use core::ptr::copy_nonoverlapping; - -use crate::bindings::{ngx_pnalloc, ngx_pool_t, u_char}; - -/// Convert a byte slice to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool. -/// -/// # Safety -/// -/// The caller must provide a valid pointer to the memory pool. -pub unsafe fn bytes_to_uchar(pool: *mut ngx_pool_t, data: &[u8]) -> Option<*mut u_char> { - let ptr: *mut u_char = unsafe { ngx_pnalloc(pool, data.len()) as _ }; - if ptr.is_null() { - return None; - } - unsafe { copy_nonoverlapping(data.as_ptr(), ptr, data.len()) }; - Some(ptr) -} - -/// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx -/// memory pool. -/// -/// # Arguments -/// -/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`). -/// * `data` - The string slice to convert to a raw pointer. -/// -/// # Safety -/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory -/// allocation using `ngx_pnalloc`. -/// -/// # Returns -/// A raw pointer (`*mut u_char`) to the allocated memory containing the converted string data. -/// -/// # Example -/// ```rust,ignore -/// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool -/// let data: &str = "example"; // The string to convert -/// let ptr = str_to_uchar(pool, data); -/// ``` -pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char { - let ptr: *mut u_char = unsafe { ngx_pnalloc(pool, data.len()) as _ }; - debug_assert!(!ptr.is_null()); - unsafe { copy_nonoverlapping(data.as_ptr(), ptr, data.len()) }; - ptr -} - -#[inline] -pub fn debug_bytes(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result { - if f.alternate() { - match bytes.len() { - 0 => Ok(()), - 1 => write!(f, "{:02x}", bytes[0]), - x => { - for b in &bytes[..x - 1] { - write!(f, "{b:02x},")?; - } - write!(f, "{:02x}", bytes[x - 1]) - } - } - } else { - f.write_str("\"")?; - display_bytes(f, bytes)?; - f.write_str("\"") - } -} - -#[inline] -pub fn display_bytes(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result { - // The implementation is similar to an inlined `String::from_utf8_lossy`, with two - // important differences: - // - // - it writes directly to the Formatter instead of allocating a temporary String - // - invalid sequences are represented as escaped individual bytes - for chunk in bytes.utf8_chunks() { - f.write_str(chunk.valid())?; - for byte in chunk.invalid() { - write!(f, "\\x{byte:02x}")?; - } - } - - Ok(()) -} - -#[cfg(test)] -mod tests { - extern crate alloc; - use alloc::format; - use alloc::string::ToString; - - use super::*; - - struct TestStr(&'static [u8]); - - impl fmt::Debug for TestStr { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("TestStr(")?; - debug_bytes(f, self.0)?; - f.write_str(")") - } - } - - impl fmt::Display for TestStr { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - display_bytes(f, self.0) - } - } - - #[test] - fn test_display() { - let cases: &[(&[u8], &str)] = &[ - (b"", ""), - (b"Ferris the \xf0\x9f\xa6\x80", "Ferris the 🦀"), - (b"\xF0\x90\x80", "\\xf0\\x90\\x80"), - (b"\xF0\x90\x80Hello World", "\\xf0\\x90\\x80Hello World"), - (b"Hello \xF0\x90\x80World", "Hello \\xf0\\x90\\x80World"), - (b"Hello World\xF0\x90\x80", "Hello World\\xf0\\x90\\x80"), - ]; - - for (bytes, expected) in cases { - let str = TestStr(bytes); - assert_eq!(str.to_string(), *expected); - } - - // Check that the formatter arguments are ignored correctly - for (bytes, expected) in &cases[2..3] { - let str = TestStr(bytes); - assert_eq!(format!("{str:12.12}"), *expected); - } - } - - #[test] - fn test_debug() { - let cases: &[(&[u8], &str, &str)] = &[ - (b"", "TestStr(\"\")", "TestStr()"), - (b"a", "TestStr(\"a\")", "TestStr(61)"), - ( - b"Ferris the \xf0\x9f\xa6\x80", - "TestStr(\"Ferris the 🦀\")", - "TestStr(46,65,72,72,69,73,20,74,68,65,20,f0,9f,a6,80)", - ), - (b"\xF0\x90\x80", "TestStr(\"\\xf0\\x90\\x80\")", "TestStr(f0,90,80)"), - ]; - for (bytes, expected, alternate) in cases { - let str = TestStr(bytes); - assert_eq!(format!("{str:?}"), *expected); - assert_eq!(format!("{str:#?}"), *alternate); - } - } -} diff --git a/rust/vendor/ngx-rust/nginx-sys/src/event.rs b/rust/vendor/ngx-rust/nginx-sys/src/event.rs deleted file mode 100644 index af7c0ff..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/event.rs +++ /dev/null @@ -1,85 +0,0 @@ -use core::ptr; - -use crate::{ - NGX_TIMER_LAZY_DELAY, ngx_current_msec, ngx_event_t, ngx_event_timer_rbtree, ngx_msec_t, - ngx_queue_insert_before, ngx_queue_remove, ngx_queue_t, ngx_rbtree_delete, ngx_rbtree_insert, -}; - -/// Sets a timeout for an event. -/// -/// # Safety -/// -///`ev` must be a valid pointer to an `ngx_event_t`. -#[inline] -pub unsafe fn ngx_add_timer(ev: *mut ngx_event_t, timer: ngx_msec_t) { - unsafe { - let key: ngx_msec_t = ngx_current_msec.wrapping_add(timer); - - if (*ev).timer_set() != 0 { - /* - * Use a previous timer value if difference between it and a new - * value is less than NGX_TIMER_LAZY_DELAY milliseconds: this allows - * to minimize the rbtree operations for fast connections. - */ - if key.abs_diff((*ev).timer.key) < NGX_TIMER_LAZY_DELAY as _ { - return; - } - - ngx_del_timer(ev); - } - - (*ev).timer.key = key; - - ngx_rbtree_insert(&raw mut ngx_event_timer_rbtree, &raw mut (*ev).timer); - - (*ev).set_timer_set(1); - } -} - -/// Deletes a previously set timeout. -/// -/// # Safety -/// -/// `ev` must be a valid pointer to an `ngx_event_t`, previously armed with [ngx_add_timer]. -#[inline] -pub unsafe fn ngx_del_timer(ev: *mut ngx_event_t) { - unsafe { - ngx_rbtree_delete(&raw mut ngx_event_timer_rbtree, &raw mut (*ev).timer); - - (*ev).timer.left = ptr::null_mut(); - (*ev).timer.right = ptr::null_mut(); - (*ev).timer.parent = ptr::null_mut(); - - (*ev).set_timer_set(0); - } -} - -/// Post the event `ev` to the post queue `q`. -/// -/// # Safety -/// -/// `ev` must be a valid pointer to an `ngx_event_t`. -/// `q` is a valid pointer to a queue head. -#[inline] -pub unsafe fn ngx_post_event(ev: *mut ngx_event_t, q: *mut ngx_queue_t) { - unsafe { - if (*ev).posted() == 0 { - (*ev).set_posted(1); - ngx_queue_insert_before(q, &raw mut (*ev).queue); - } - } -} - -/// Deletes the event `ev` from the queue it's currently posted in. -/// -/// # Safety -/// -/// `ev` must be a valid pointer to an `ngx_event_t`. -/// `ev.queue` is initialized with `ngx_queue_init`. -#[inline] -pub unsafe fn ngx_delete_posted_event(ev: *mut ngx_event_t) { - unsafe { - (*ev).set_posted(0); - ngx_queue_remove(&raw mut (*ev).queue); - } -} diff --git a/rust/vendor/ngx-rust/nginx-sys/src/http.rs b/rust/vendor/ngx-rust/nginx-sys/src/http.rs deleted file mode 100644 index 435cc79..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/http.rs +++ /dev/null @@ -1,18 +0,0 @@ -use core::mem::offset_of; - -use crate::bindings::ngx_http_conf_ctx_t; - -/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct. -/// -/// This is used to access the main configuration context for an HTTP module. -pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf); - -/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct. -/// -/// This is used to access the server configuration context for an HTTP module. -pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf); - -/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct. -/// -/// This is used to access the location configuration context for an HTTP module. -pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf); diff --git a/rust/vendor/ngx-rust/nginx-sys/src/lib.rs b/rust/vendor/ngx-rust/nginx-sys/src/lib.rs deleted file mode 100644 index a3e3454..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/lib.rs +++ /dev/null @@ -1,352 +0,0 @@ -#![doc = include_str!("../README.md")] -#![warn(missing_docs)] -#![no_std] - -pub mod detail; -mod event; -#[cfg(all(feature = "http", ngx_feature = "http"))] -mod http; -#[cfg(all(feature = "mail", ngx_feature = "mail"))] -mod mail; -mod queue; -mod rbtree; -#[cfg(all(feature = "stream", ngx_feature = "stream"))] -mod stream; -mod string; - -use core::ptr; - -#[doc(hidden)] -mod bindings { - #![allow(unknown_lints)] // unnecessary_transmutes - #![allow(missing_docs)] - #![allow(non_upper_case_globals)] - #![allow(non_camel_case_types)] - #![allow(non_snake_case)] - #![allow(dead_code)] - #![allow(clippy::all)] - #![allow(improper_ctypes)] - #![allow(rustdoc::broken_intra_doc_links)] - #![allow(unnecessary_transmutes)] - include!(concat!(env!("OUT_DIR"), "/bindings.rs")); -} -#[doc(no_inline)] -pub use bindings::*; -pub use event::*; -#[cfg(all(feature = "http", ngx_feature = "http"))] -pub use http::*; -#[cfg(all(feature = "mail", ngx_feature = "mail"))] -pub use mail::*; -pub use queue::*; -pub use rbtree::*; -#[cfg(all(feature = "stream", ngx_feature = "stream"))] -pub use stream::*; - -/// Default alignment for pool allocations. -pub const NGX_ALIGNMENT: usize = NGX_RS_ALIGNMENT; - -/// Sentinel returned by `ngx_resolve_start()` when no resolver is configured. -/// -/// nginx's `NGX_NO_RESOLVER` macro expands to `(void *) -1`, which bindgen does -/// not emit. It is an address-only sentinel that must never be dereferenced; -/// callers compare the `ngx_resolve_start()` return value against it. -pub const NGX_NO_RESOLVER: *mut ngx_resolver_ctx_t = ptr::without_provenance_mut(usize::MAX); - -// Check if the allocations made with ngx_palloc are properly aligned. -// If the check fails, objects allocated from `ngx_pool` can violate Rust pointer alignment -// requirements. -const _: () = assert!(core::mem::align_of::() <= NGX_ALIGNMENT); - -impl ngx_array_t { - /// Returns the contents of this array as a slice of `T`. - /// - /// # Safety - /// - /// The array must be a valid, initialized array containing elements of type T or compatible in - /// layout with T (e.g. `#[repr(transparent)]` wrappers). - pub unsafe fn as_slice(&self) -> &[T] { - debug_assert_eq!( - core::mem::size_of::(), - self.size, - "ngx_array_t::as_slice(): element size mismatch" - ); - if self.nelts == 0 { - &[] - } else { - // SAFETY: in a valid array, `elts` is a valid well-aligned pointer to at least `nelts` - // elements of size `size` - unsafe { core::slice::from_raw_parts(self.elts.cast(), self.nelts) } - } - } - - /// Returns the contents of this array as a mutable slice of `T`. - /// - /// # Safety - /// - /// The array must be a valid, initialized array containing elements of type T or compatible in - /// layout with T (e.g. `#[repr(transparent)]` wrappers). - pub unsafe fn as_slice_mut(&mut self) -> &mut [T] { - debug_assert_eq!( - core::mem::size_of::(), - self.size, - "ngx_array_t::as_slice_mut(): element size mismatch" - ); - if self.nelts == 0 { - &mut [] - } else { - // SAFETY: in a valid array, `elts` is a valid well-aligned pointer to at least `nelts` - // elements of size `size` - unsafe { core::slice::from_raw_parts_mut(self.elts.cast(), self.nelts) } - } - } -} - -impl ngx_command_t { - /// Creates a new empty [`ngx_command_t`] instance. - /// - /// This method replaces the `ngx_null_command` C macro. This is typically used to terminate an - /// array of configuration directives. - /// - /// [`ngx_command_t`]: https://nginx.org/en/docs/dev/development_guide.html#config_directives - pub const fn empty() -> Self { - Self { - name: ngx_str_t::empty(), - type_: 0, - set: None, - conf: 0, - offset: 0, - post: ptr::null_mut(), - } - } -} - -impl ngx_module_t { - /// Create a new `ngx_module_t` instance with default values. - pub const fn default() -> Self { - Self { - ctx_index: ngx_uint_t::MAX, - index: ngx_uint_t::MAX, - name: ptr::null_mut(), - spare0: 0, - spare1: 0, - version: nginx_version as ngx_uint_t, - signature: NGX_RS_MODULE_SIGNATURE.as_ptr(), - ctx: ptr::null_mut(), - commands: ptr::null_mut(), - type_: 0, - init_master: None, - init_module: None, - init_process: None, - init_thread: None, - exit_thread: None, - exit_process: None, - exit_master: None, - spare_hook0: 0, - spare_hook1: 0, - spare_hook2: 0, - spare_hook3: 0, - spare_hook4: 0, - spare_hook5: 0, - spare_hook6: 0, - spare_hook7: 0, - } - } -} - -impl ngx_variable_value_t { - /// Returns the contents of this variable value as a byte slice. - pub fn as_bytes(&self) -> &[u8] { - match self.len() { - 0 => &[], - // SAFETY: data for non-empty value must be a valid well-aligned pointer. - len => unsafe { core::slice::from_raw_parts(self.data, len as usize) }, - } - } -} - -impl AsRef<[u8]> for ngx_variable_value_t { - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } -} - -/// Returns the error code of the last failed operation (`errno`). -#[inline] -pub fn ngx_errno() -> ngx_err_t { - // SAFETY: GetLastError takes no arguments and reads a thread-local variable - #[cfg(windows)] - let err = unsafe { GetLastError() }; - - #[cfg(not(windows))] - let err = errno::errno().0; - - err as ngx_err_t -} - -/// Sets the error code (`errno`). -#[inline] -pub fn ngx_set_errno(err: ngx_err_t) { - #[cfg(windows)] - // SAFETY: SetLastError takes one argument by value and updates a thread-local variable - unsafe { - SetLastError(err as _) - } - #[cfg(not(windows))] - errno::set_errno(errno::Errno(err as _)) -} - -/// Returns the error code of the last failed sockets operation. -#[inline] -pub fn ngx_socket_errno() -> ngx_err_t { - // SAFETY: WSAGetLastError takes no arguments and reads a thread-local variable - #[cfg(windows)] - let err = unsafe { WSAGetLastError() }; - - #[cfg(not(windows))] - let err = errno::errno().0; - - err as ngx_err_t -} - -/// Sets the error code of the sockets operation. -#[inline] -pub fn ngx_set_socket_errno(err: ngx_err_t) { - #[cfg(windows)] - // SAFETY: WSaSetLastError takes one argument by value and updates a thread-local variable - unsafe { - WSASetLastError(err as _) - } - #[cfg(not(windows))] - errno::set_errno(errno::Errno(err as _)) -} - -/// Returns a non cryptograhpically-secure pseudo-random integer. -#[inline] -pub fn ngx_random() -> core::ffi::c_long { - #[cfg(windows)] - unsafe { - // Emulate random() as Microsoft CRT does not provide it. - // rand() should be thread-safe in the multi-threaded CRT we link to, but will not be seeded - // outside of the main thread. - let x: u32 = ((rand() as u32) << 16) ^ ((rand() as u32) << 8) ^ (rand() as u32); - (0x7fffffff & x) as _ - } - #[cfg(not(windows))] - unsafe { - random() - } -} - -/// Causes the calling thread to relinquish the CPU. -#[inline] -pub fn ngx_sched_yield() { - #[cfg(windows)] - unsafe { - SwitchToThread() - }; - #[cfg(all(not(windows), ngx_feature = "have_sched_yield"))] - unsafe { - sched_yield() - }; - #[cfg(not(any(windows, ngx_feature = "have_sched_yield")))] - unsafe { - usleep(1) - } -} - -/// Returns cached timestamp in seconds, updated at the start of the event loop iteration. -/// -/// Can be stale when accessing from threads, see [ngx_time_update]. -#[inline] -pub fn ngx_time() -> time_t { - // SAFETY: ngx_cached_time is initialized before any module code can run - unsafe { (*ngx_cached_time).sec } -} - -/// Returns cached time, updated at the start of the event loop iteration. -/// -/// Can be stale when accessing from threads, see [ngx_time_update]. -/// A cached reference to the ngx_timeofday() result is guaranteed to remain unmodified for the next -/// NGX_TIME_SLOTS seconds. -#[inline] -pub fn ngx_timeofday() -> &'static ngx_time_t { - // SAFETY: ngx_cached_time is initialized before any module code can run - unsafe { &*ngx_cached_time } -} - -/// Initialize a list, using a pool for the backing memory, with capacity to store the given number -/// of elements and element size. -/// -/// # Safety -/// * `list` must be non-null -/// * `pool` must be a valid pool -#[inline] -pub unsafe fn ngx_list_init( - list: *mut ngx_list_t, - pool: *mut ngx_pool_t, - n: ngx_uint_t, - size: usize, -) -> ngx_int_t { - unsafe { - (*list).part.elts = ngx_palloc(pool, n * size); - if (*list).part.elts.is_null() { - return NGX_ERROR as ngx_int_t; - } - (*list).part.nelts = 0; - (*list).part.next = ptr::null_mut(); - (*list).last = &raw mut (*list).part; - (*list).size = size; - (*list).nalloc = n; - (*list).pool = pool; - NGX_OK as ngx_int_t - } -} - -/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool. -/// -/// # Arguments -/// -/// * `table` - A pointer to the nginx table entry (`ngx_table_elt_t`) to modify. -/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`) for memory allocation. -/// * `key` - The key string to add to the table entry. -/// * `value` - The value string to add to the table entry. -/// -/// # Safety -/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory -/// allocation using `str_to_uchar`. -/// -/// # Returns -/// An `Option<()>` representing the result of the operation. `Some(())` indicates success, while -/// `None` indicates a null table pointer. -/// -/// # Example -/// ```rust -/// # use nginx_sys::*; -/// # unsafe fn example(pool: *mut ngx_pool_t, headers: *mut ngx_list_t) { -/// // Obtain a pointer to the nginx table entry -/// let table: *mut ngx_table_elt_t = ngx_list_push(headers).cast(); -/// assert!(!table.is_null()); -/// let key: &str = "key"; // The key to add -/// let value: &str = "value"; // The value to add -/// let result = add_to_ngx_table(table, pool, key, value); -/// # } -/// ``` -pub unsafe fn add_to_ngx_table( - table: *mut ngx_table_elt_t, - pool: *mut ngx_pool_t, - key: impl AsRef<[u8]>, - value: impl AsRef<[u8]>, -) -> Option<()> { - if let Some(table) = unsafe { table.as_mut() } { - let key = key.as_ref(); - table.key = unsafe { ngx_str_t::from_bytes(pool, key)? }; - table.value = unsafe { ngx_str_t::from_bytes(pool, value.as_ref())? }; - table.lowcase_key = unsafe { ngx_pnalloc(pool, table.key.len).cast() }; - if table.lowcase_key.is_null() { - return None; - } - table.hash = unsafe { ngx_hash_strlow(table.lowcase_key, table.key.data, table.key.len) }; - return Some(()); - } - None -} diff --git a/rust/vendor/ngx-rust/nginx-sys/src/mail.rs b/rust/vendor/ngx-rust/nginx-sys/src/mail.rs deleted file mode 100644 index 7415fbe..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/mail.rs +++ /dev/null @@ -1,13 +0,0 @@ -use core::mem::offset_of; - -use crate::bindings::ngx_mail_conf_ctx_t; - -/// The offset of the `main_conf` field in the `ngx_mail_conf_ctx_t` struct. -/// -/// This is used to access the main configuration context for a MAIL module. -pub const NGX_MAIL_MAIN_CONF_OFFSET: usize = offset_of!(ngx_mail_conf_ctx_t, main_conf); - -/// The offset of the `srv_conf` field in the `ngx_mail_conf_ctx_t` struct. -/// -/// This is used to access the server configuration context for a MAIL module. -pub const NGX_MAIL_SRV_CONF_OFFSET: usize = offset_of!(ngx_mail_conf_ctx_t, srv_conf); diff --git a/rust/vendor/ngx-rust/nginx-sys/src/queue.rs b/rust/vendor/ngx-rust/nginx-sys/src/queue.rs deleted file mode 100644 index 7dd3432..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/queue.rs +++ /dev/null @@ -1,265 +0,0 @@ -use core::ptr; - -use crate::bindings::ngx_queue_t; - -/// Get a reference to the beginning of a queue node data structure, -/// considering the queue field offset in it. -/// -/// # Safety -/// -/// `$q` must be a valid pointer to the field `$link` in the struct `$type` -#[macro_export] -macro_rules! ngx_queue_data { - ($q:expr, $type:path, $link:ident) => { - $q.byte_sub(::core::mem::offset_of!($type, $link)).cast::<$type>() - }; -} - -/// Initializes the queue head before use. -/// -/// # Safety -/// -/// `q` must be a valid pointer to [ngx_queue_t]. -#[inline] -pub unsafe fn ngx_queue_init(q: *mut ngx_queue_t) { - unsafe { - (*q).prev = q; - (*q).next = q; - } -} - -/// Returns `true` if the queue contains no elements. -/// -/// # Safety -/// -/// `q` must be a valid pointer to [ngx_queue_t], initialized with [ngx_queue_init]. -#[inline] -pub unsafe fn ngx_queue_empty(q: *const ngx_queue_t) -> bool { - unsafe { ptr::eq(q, (*q).prev) } -} - -/// Inserts a new node after the current. -/// -/// # Safety -/// -/// Both `q` and `x` must be valid pointers to [ngx_queue_t] -#[inline] -pub unsafe fn ngx_queue_insert_after(q: *mut ngx_queue_t, x: *mut ngx_queue_t) { - unsafe { - (*x).next = (*q).next; - (*(*x).next).prev = x; - (*x).prev = q; - (*q).next = x; - } -} - -/// Inserts a new node before the current. -/// -/// # Safety -/// -/// Both `q` and `x` must be valid pointers to [ngx_queue_t]. -#[inline] -pub unsafe fn ngx_queue_insert_before(q: *mut ngx_queue_t, x: *mut ngx_queue_t) { - unsafe { - (*x).prev = (*q).prev; - (*(*x).prev).next = x; - (*x).next = q; - (*q).prev = x; - } -} - -/// Removes a node from the queue. -/// -/// # Safety -/// -/// `q` must be a valid pointer to an [ngx_queue_t] node. -#[inline] -pub unsafe fn ngx_queue_remove(q: *mut ngx_queue_t) { - unsafe { - (*(*q).next).prev = (*q).prev; - (*(*q).prev).next = (*q).next; - (*q).prev = ptr::null_mut(); - (*q).next = ptr::null_mut(); - } -} - -/// Splits a queue at a node, returning the queue tail in a separate queue. -/// -/// # Safety -/// -/// `h` must be a valid pointer to a head queue node. -/// `q` must be a node in the queue `h`. -/// `n` must be a valid pointer to [ngx_queue_t]. -#[inline] -pub unsafe fn ngx_queue_split(h: *mut ngx_queue_t, q: *mut ngx_queue_t, n: *mut ngx_queue_t) { - unsafe { - (*n).prev = (*h).prev; - (*(*n).prev).next = n; - (*n).next = q; - (*h).prev = (*q).prev; - (*(*h).prev).next = h; - (*q).prev = n; - } -} - -/// Adds a second queue to the first queue. -/// -/// # Safety -/// -/// Both `h` and `n` must be valid pointers to queue heads, initialized with [ngx_queue_init]. -/// `n` will be left in invalid state, pointing to the subrange of `h` without back references. -#[inline] -pub unsafe fn ngx_queue_add(h: *mut ngx_queue_t, n: *mut ngx_queue_t) { - unsafe { - (*(*h).prev).next = (*n).next; - (*(*n).next).prev = (*h).prev; - (*h).prev = (*n).prev; - (*(*h).prev).next = h; - } -} - -impl ngx_queue_t { - /// Returns `true` if the queue contains no elements. - pub fn is_empty(&self) -> bool { - unsafe { ngx_queue_empty(self) } - } -} - -impl Default for ngx_queue_t { - fn default() -> ngx_queue_t { - ngx_queue_t { prev: ptr::null_mut(), next: ptr::null_mut() } - } -} - -#[cfg(test)] -mod tests { - extern crate alloc; - use alloc::boxed::Box; - - use super::*; - - struct TestData { - value: usize, - queue: ngx_queue_t, - } - - impl TestData { - pub fn new(value: usize) -> *mut Self { - // We should be using `ngx_pool_t` here, but that is not possible without linking to - // the nginx - let mut x = Box::new(Self { value, queue: Default::default() }); - unsafe { ngx_queue_init(&raw mut x.queue) }; - Box::into_raw(x) - } - - pub unsafe fn free(x: *mut Self) { - unsafe { - let _ = Box::from_raw(x); - } - } - } - - impl Drop for TestData { - fn drop(&mut self) { - if !self.queue.next.is_null() && !self.queue.is_empty() { - unsafe { ngx_queue_remove(&raw mut self.queue) }; - } - } - } - - struct Iter { - h: *mut ngx_queue_t, - q: *mut ngx_queue_t, - next: fn(*mut ngx_queue_t) -> *mut ngx_queue_t, - } - - impl Iter { - pub fn new(h: *mut ngx_queue_t) -> Self { - let next = |x: *mut ngx_queue_t| unsafe { (*x).next }; - Self { h, q: next(h), next } - } - - pub fn new_reverse(h: *mut ngx_queue_t) -> Self { - let next = |x: *mut ngx_queue_t| unsafe { (*x).prev }; - Self { h, q: next(h), next } - } - } - - impl Iterator for Iter { - type Item = *mut ngx_queue_t; - - fn next(&mut self) -> Option { - if ptr::eq(self.h, self.q) { - return None; - } - - let item = self.q; - self.q = (self.next)(self.q); - Some(item) - } - } - - #[test] - fn test_queue() { - fn value(q: *mut ngx_queue_t) -> usize { - unsafe { (*ngx_queue_data!(q, TestData, queue)).value } - } - - // Check forward and reverse iteration - fn cmp(h: *mut ngx_queue_t, other: &[usize]) -> bool { - Iter::new(h).map(value).eq(other.iter().cloned()) - && Iter::new_reverse(h).map(value).eq(other.iter().rev().cloned()) - } - - // Note how this test does not use references or borrows to avoid triggering UBs - // detectable by Miri. This does not mean that the code is safe or sound. - unsafe { - // Initialize and fill the queue - - let mut h1 = ngx_queue_t::default(); - ngx_queue_init(&raw mut h1); - - let mut h2 = ngx_queue_t::default(); - ngx_queue_init(&raw mut h2); - - for i in 1..=5 { - let elem = TestData::new(i); - ngx_queue_insert_before(&raw mut h1, &raw mut (*elem).queue); - - let elem = TestData::new(i); - ngx_queue_insert_after(&raw mut h2, &raw mut (*elem).queue); - } - - // Iterate and test the values - - assert!(cmp(&raw mut h1, &[1, 2, 3, 4, 5])); - assert!(cmp(&raw mut h2, &[5, 4, 3, 2, 1])); - - // Move nodes from h2 to h1 - - // h2 still points to the subrange of h1 after this operation - ngx_queue_add(&raw mut h1, &raw mut h2); - - assert!(cmp(&raw mut h1, &[1, 2, 3, 4, 5, 5, 4, 3, 2, 1])); - - ngx_queue_split(&raw mut h1, (*h2.next).next, &raw mut h2); - - assert!(cmp(&raw mut h1, &[1, 2, 3, 4, 5, 5])); - assert!(cmp(&raw mut h2, &[4, 3, 2, 1])); - - // Cleanup - - for q in Iter::new(&raw mut h1) { - let td = ngx_queue_data!(q, TestData, queue); - TestData::free(td); - } - assert!(h1.is_empty()); - - for q in Iter::new(&raw mut h2) { - let td = ngx_queue_data!(q, TestData, queue); - TestData::free(td); - } - assert!(h2.is_empty()); - }; - } -} diff --git a/rust/vendor/ngx-rust/nginx-sys/src/rbtree.rs b/rust/vendor/ngx-rust/nginx-sys/src/rbtree.rs deleted file mode 100644 index daa28c4..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/rbtree.rs +++ /dev/null @@ -1,85 +0,0 @@ -use core::ptr; - -use crate::bindings::{ngx_rbtree_insert_pt, ngx_rbtree_node_t, ngx_rbtree_t}; - -/// Get a reference to the beginning of a tree element data structure, -/// considering the link field offset in it. -/// -/// # Safety -/// -/// `$node` must be a valid pointer to the field `$link` in the struct `$type` -#[macro_export] -macro_rules! ngx_rbtree_data { - ($node:expr, $type:path, $link:ident) => { - $node.byte_sub(::core::mem::offset_of!($type, $link)).cast::<$type>() - }; -} - -/// Initializes the RbTree with specified sentinel and insert function. -/// -/// # Safety -/// -/// All of the pointers passed must be valid. -/// `sentinel` is expected to be valid for the whole lifetime of the `tree`. -pub unsafe fn ngx_rbtree_init( - tree: *mut ngx_rbtree_t, - sentinel: *mut ngx_rbtree_node_t, - insert: ngx_rbtree_insert_pt, -) { - unsafe { - ngx_rbtree_sentinel_init(sentinel); - (*tree).root = sentinel; - (*tree).sentinel = sentinel; - (*tree).insert = insert; - } -} - -/// Marks the tree node as red. -/// -/// # Safety -/// -/// `node` must be a valid pointer to a [ngx_rbtree_node_t]. -#[inline] -pub unsafe fn ngx_rbt_red(node: *mut ngx_rbtree_node_t) { - unsafe { (*node).color = 1 } -} - -/// Marks the tree node as black. -/// -/// # Safety -/// -/// `node` must be a valid pointer to a [ngx_rbtree_node_t]. -#[inline] -pub unsafe fn ngx_rbt_black(node: *mut ngx_rbtree_node_t) { - unsafe { (*node).color = 0 } -} - -/// Initializes the sentinel node. -/// -/// # Safety -/// -/// `node` must be a valid pointer to a [ngx_rbtree_node_t]. -#[inline] -pub unsafe fn ngx_rbtree_sentinel_init(node: *mut ngx_rbtree_node_t) { - unsafe { ngx_rbt_black(node) } -} - -/// Returns the least (leftmost) node of the tree. -/// -/// # Safety -/// -/// `node` must be a valid pointer to a [ngx_rbtree_node_t]. -/// `sentinel` must be a valid pointer to the sentinel node in the same Red-Black tree. -#[inline] -pub unsafe fn ngx_rbtree_min( - mut node: *mut ngx_rbtree_node_t, - sentinel: *mut ngx_rbtree_node_t, -) -> *mut ngx_rbtree_node_t { - unsafe { - while !ptr::addr_eq((*node).left, sentinel) { - node = (*node).left; - } - - node - } -} diff --git a/rust/vendor/ngx-rust/nginx-sys/src/stream.rs b/rust/vendor/ngx-rust/nginx-sys/src/stream.rs deleted file mode 100644 index 6288fd4..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/stream.rs +++ /dev/null @@ -1,13 +0,0 @@ -use core::mem::offset_of; - -use crate::bindings::ngx_stream_conf_ctx_t; - -/// The offset of the `main_conf` field in the `ngx_stream_conf_ctx_t` struct. -/// -/// This is used to access the main configuration context for a STREAM module. -pub const NGX_STREAM_MAIN_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, main_conf); - -/// The offset of the `srv_conf` field in the `ngx_stream_conf_ctx_t` struct. -/// -/// This is used to access the server configuration context for a STREAM module. -pub const NGX_STREAM_SRV_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, srv_conf); diff --git a/rust/vendor/ngx-rust/nginx-sys/src/string.rs b/rust/vendor/ngx-rust/nginx-sys/src/string.rs deleted file mode 100644 index a05d285..0000000 --- a/rust/vendor/ngx-rust/nginx-sys/src/string.rs +++ /dev/null @@ -1,233 +0,0 @@ -use core::cmp; -use core::fmt; -use core::hash; -use core::ptr; -use core::slice; -use core::str; - -use crate::bindings::{ngx_pool_t, ngx_str_t}; -use crate::detail; - -impl ngx_str_t { - /// Returns the contents of this `ngx_str_t` as a byte slice. - /// - /// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data` - /// may have. - #[inline] - pub fn as_bytes(&self) -> &[u8] { - if self.is_empty() { - &[] - } else { - // SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer - unsafe { slice::from_raw_parts(self.data, self.len) } - } - } - - /// Returns the contents of this `ngx_str_t` as a mutable byte slice. - #[inline] - pub fn as_bytes_mut(&mut self) -> &mut [u8] { - if self.is_empty() { - &mut [] - } else { - // SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer - unsafe { slice::from_raw_parts_mut(self.data, self.len) } - } - } - - /// Returns `true` if the string has a length of 0. - #[inline] - pub fn is_empty(&self) -> bool { - self.len == 0 - } - - /// Returns the contents of this `ngx_str_t` a string slice (`&str`) if - /// the contents are utf-8 encoded. - /// - /// # Returns - /// A string slice (`&str`) representing the nginx string, or else a - /// Utf8Error. - pub fn to_str(&self) -> Result<&str, str::Utf8Error> { - str::from_utf8(self.as_bytes()) - } - - /// Creates an empty `ngx_str_t` instance. - /// - /// This method replaces the `ngx_null_string` C macro. - pub const fn empty() -> Self { - ngx_str_t { len: 0, data: ptr::null_mut() } - } - - /// Create an `ngx_str_t` instance from a byte slice. - /// - /// # Safety - /// - /// The caller must provide a valid pointer to a memory pool. - pub unsafe fn from_bytes(pool: *mut ngx_pool_t, src: &[u8]) -> Option { - unsafe { detail::bytes_to_uchar(pool, src).map(|data| Self { data, len: src.len() }) } - } - - /// Create an `ngx_str_t` instance from a string slice (`&str`). - /// - /// # Arguments - /// - /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`). - /// * `data` - The string slice from which to create the nginx string. - /// - /// # Safety - /// This function is marked as unsafe because it accepts a raw pointer argument. There is no - /// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to - /// avoid indeterminate behavior. - /// - /// # Returns - /// An `ngx_str_t` instance representing the given string slice. - pub unsafe fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self { - unsafe { ngx_str_t { data: detail::str_to_uchar(pool, data), len: data.len() } } - } - - /// Divides one `ngx_str_t` into two at an index. - /// - /// # Safety - /// - /// The results will reference the original string; be wary of the ownership and lifetime. - pub fn split_at(&self, mid: usize) -> Option<(ngx_str_t, ngx_str_t)> { - if mid > self.len { - return None; - } - - Some(( - ngx_str_t { data: self.data, len: mid }, - ngx_str_t { data: unsafe { self.data.add(mid) }, len: self.len - mid }, - )) - } - - /// Returns an `ngx_str_t` with the prefix removed. - /// - /// If the string starts with the byte sequence `prefix`, returns the substring after the - /// prefix, wrapped in `Some`. The resulting substring can be empty. - /// - /// # Safety - /// - /// The result will reference the original string; be wary of the ownership and lifetime. - /// - /// The method is not marked as `unsafe` as everything it does is possible via safe interfaces. - pub fn strip_prefix(&self, prefix: impl AsRef<[u8]>) -> Option { - let prefix = prefix.as_ref(); - if self.as_bytes().starts_with(prefix) { - self.split_at(prefix.len()).map(|x| x.1) - } else { - None - } - } - - /// Returns an `ngx_str_t` with the suffix removed. - /// - /// If the string ends with the byte sequence `suffix`, returns the substring before the - /// suffix, wrapped in `Some`. The resulting substring can be empty. - /// - /// # Safety - /// - /// The result will reference the original string; be wary of the ownership and lifetime. - /// - /// The method is not marked as `unsafe` as everything it does is possible via safe interfaces. - pub fn strip_suffix(&self, suffix: impl AsRef<[u8]>) -> Option { - let suffix = suffix.as_ref(); - if self.as_bytes().ends_with(suffix) { - self.split_at(self.len - suffix.len()).map(|x| x.0) - } else { - None - } - } -} - -impl AsRef<[u8]> for ngx_str_t { - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } -} - -impl AsMut<[u8]> for ngx_str_t { - fn as_mut(&mut self) -> &mut [u8] { - self.as_bytes_mut() - } -} - -impl Default for ngx_str_t { - fn default() -> Self { - Self::empty() - } -} - -impl fmt::Display for ngx_str_t { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - detail::display_bytes(f, self.as_bytes()) - } -} - -impl From for &[u8] { - fn from(s: ngx_str_t) -> Self { - if s.len == 0 || s.data.is_null() { - return Default::default(); - } - unsafe { slice::from_raw_parts(s.data, s.len) } - } -} - -impl hash::Hash for ngx_str_t { - fn hash(&self, state: &mut H) { - self.as_bytes().hash(state) - } -} - -impl PartialEq for ngx_str_t { - fn eq(&self, other: &Self) -> bool { - PartialEq::eq(self.as_bytes(), other.as_bytes()) - } -} - -impl Eq for ngx_str_t {} - -impl PartialOrd for ngx_str_t { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for ngx_str_t { - fn cmp(&self, other: &Self) -> cmp::Ordering { - Ord::cmp(self.as_bytes(), other.as_bytes()) - } -} - -impl TryFrom for &str { - type Error = str::Utf8Error; - - fn try_from(s: ngx_str_t) -> Result { - str::from_utf8(s.into()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn ngx_str_prefix() { - let s = "key=value"; - let s = ngx_str_t { data: s.as_ptr().cast_mut(), len: s.len() }; - - assert_eq!( - s.strip_prefix("key=").as_ref().map(ngx_str_t::as_bytes), - Some("value".as_bytes()) - ); - - assert_eq!(s.strip_prefix("test"), None); - - assert_eq!( - s.strip_suffix("value").as_ref().map(ngx_str_t::as_bytes), - Some("key=".as_bytes()) - ); - - assert_eq!(s.strip_suffix("test"), None); - } -} diff --git a/rust/vendor/ngx-rust/rustfmt.toml b/rust/vendor/ngx-rust/rustfmt.toml deleted file mode 100644 index f1b45de..0000000 --- a/rust/vendor/ngx-rust/rustfmt.toml +++ /dev/null @@ -1,9 +0,0 @@ -edition = "2024" -max_width = 100 -use_small_heuristics = "Max" - -# unstable options: -# comment_width = 100 -# group_imports = "StdExternalCrate" -# format_strings = true -# wrap_comments = true \ No newline at end of file diff --git a/rust/vendor/ngx-rust/src/allocator.rs b/rust/vendor/ngx-rust/src/allocator.rs deleted file mode 100644 index d5702ff..0000000 --- a/rust/vendor/ngx-rust/src/allocator.rs +++ /dev/null @@ -1,81 +0,0 @@ -//! The allocator module. -//! -//! The module provides custom memory allocator support traits and utilities based on the unstable -//! [feature(allocator_api)]. -//! -//! Currently implemented as a reexport of parts of the [allocator_api2]. -//! -//! [feature(allocator_api)]: https://github.com/rust-lang/rust/issues/32838 - -use ::core::alloc::Layout; -use ::core::mem; -use ::core::ptr::{self, NonNull}; -pub use allocator_api2::alloc::{AllocError, Allocator}; -#[cfg(feature = "alloc")] -pub use allocator_api2::{alloc::Global, boxed::Box, unsize_box}; - -/// Explicitly duplicate an object using the specified Allocator. -pub trait TryCloneIn: Sized { - /// Target type, generic over an allocator. - type Target; - - /// Attempts to copy the value using `alloc` as an underlying Allocator. - fn try_clone_in(&self, alloc: A) -> Result, AllocError>; -} - -/// Moves `value` to the memory backed by `alloc` and returns a pointer. -/// -/// This should be similar to `Box::into_raw(Box::try_new_in(value, alloc)?)`, except without -/// `alloc` requirement and intermediate steps. -/// -/// # Note -/// -/// The resulting pointer has no owner. The caller is responsible for destroying `T` and releasing -/// the memory. -pub fn allocate(value: T, alloc: &A) -> Result, AllocError> -where - A: Allocator, -{ - let layout = Layout::for_value(&value); - let ptr: NonNull = alloc.allocate(layout)?.cast(); - - // SAFETY: the allocator succeeded and gave us a correctly aligned pointer to an uninitialized - // data - unsafe { ptr.cast::>().as_mut().write(value) }; - - Ok(ptr) -} -/// -/// Creates a [NonNull] that is dangling, but well-aligned for this [Layout]. -/// -/// See also [::core::alloc::Layout::dangling()] -#[inline(always)] -pub(crate) const fn dangling_for_layout(layout: &Layout) -> NonNull { - unsafe { - let ptr = ptr::null_mut::().byte_add(layout.align()); - NonNull::new_unchecked(ptr) - } -} - -#[cfg(feature = "alloc")] -mod impls { - use allocator_api2::boxed::Box; - - use super::*; - - impl TryCloneIn for Box - where - T: TryCloneIn, - OA: Allocator, - { - type Target = Box<::Target, A>; - - fn try_clone_in( - &self, - alloc: A, - ) -> Result, AllocError> { - let x = self.as_ref().try_clone_in(alloc.clone())?; - Box::try_new_in(x, alloc) - } - } -} diff --git a/rust/vendor/ngx-rust/src/async_/mod.rs b/rust/vendor/ngx-rust/src/async_/mod.rs deleted file mode 100644 index 3ec7832..0000000 --- a/rust/vendor/ngx-rust/src/async_/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! Async runtime and set of utilities on top of the NGINX event loop. -pub use self::sleep::{Sleep, sleep}; -pub use self::spawn::{Task, spawn}; - -pub mod resolver; - -mod sleep; -mod spawn; diff --git a/rust/vendor/ngx-rust/src/async_/resolver.rs b/rust/vendor/ngx-rust/src/async_/resolver.rs deleted file mode 100644 index 53bf55c..0000000 --- a/rust/vendor/ngx-rust/src/async_/resolver.rs +++ /dev/null @@ -1,328 +0,0 @@ -// Copyright (c) F5, Inc. -// -// This source code is licensed under the Apache License, Version 2.0 license found in the -// LICENSE file in the root directory of this source tree. - -//! Wrapper for the nginx resolver. -//! -//! See . - -use alloc::string::{String, ToString}; -use core::ffi::c_void; -use core::fmt; -use core::num::NonZero; -use core::pin::Pin; -use core::ptr::NonNull; -use core::task::{Context, Poll, Waker}; - -use nginx_sys::{ - NGX_NO_RESOLVER, NGX_RESOLVE_FORMERR, NGX_RESOLVE_NOTIMP, NGX_RESOLVE_NXDOMAIN, - NGX_RESOLVE_REFUSED, NGX_RESOLVE_SERVFAIL, NGX_RESOLVE_TIMEDOUT, -}; - -use crate::{ - allocator::Box, - collections::Vec, - core::{Pool, Status}, - ffi::{ - ngx_addr_t, ngx_msec_t, ngx_resolve_name, ngx_resolve_start, ngx_resolver_ctx_t, - ngx_resolver_t, ngx_str_t, - }, -}; - -/// Error type for all uses of `Resolver`. -#[derive(Debug)] -pub enum Error { - /// No resolver configured - NoResolver, - /// Resolver error, with context of name being resolved - Resolver(ResolverError, String), - /// Allocation failed - AllocationFailed, - /// Unknown internal error while starting name resolution - Internal, -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::NoResolver => write!(f, "No resolver configured"), - Error::Resolver(err, context) => write!(f, "{err}: resolving `{context}`"), - Error::AllocationFailed => write!(f, "Allocation failed"), - Error::Internal => write!(f, "Internal error"), - } - } -} -impl core::error::Error for Error {} - -/// These cases directly reflect the NGX_RESOLVE_ error codes, -/// plus a timeout, and a case for an unknown error where a known -/// NGX_RESOLVE_ should be. -#[derive(Debug)] -pub enum ResolverError { - /// Format error (NGX_RESOLVE_FORMERR) - FormErr, - /// Server failure (NGX_RESOLVE_SERVFAIL) - ServFail, - /// Host not found (NGX_RESOLVE_NXDOMAIN) - NXDomain, - /// Unimplemented (NGX_RESOLVE_NOTIMP) - NotImp, - /// Operation refused (NGX_RESOLVE_REFUSED) - Refused, - /// Timed out (NGX_RESOLVE_TIMEDOUT) - TimedOut, - /// Unknown NGX_RESOLVE error - Unknown(isize), -} -impl fmt::Display for ResolverError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - ResolverError::FormErr => write!(f, "Format error"), - ResolverError::ServFail => write!(f, "Server Failure"), - ResolverError::NXDomain => write!(f, "Host not found"), - ResolverError::NotImp => write!(f, "Unimplemented"), - ResolverError::Refused => write!(f, "Refused"), - ResolverError::TimedOut => write!(f, "Timed out"), - ResolverError::Unknown(code) => write!(f, "Unknown NGX_RESOLVE error {code}"), - } - } -} -impl core::error::Error for ResolverError {} - -/// Convert from the NGX_RESOLVE_ error codes. -impl From> for ResolverError { - fn from(code: NonZero) -> ResolverError { - match code.get() as u32 { - NGX_RESOLVE_FORMERR => ResolverError::FormErr, - NGX_RESOLVE_SERVFAIL => ResolverError::ServFail, - NGX_RESOLVE_NXDOMAIN => ResolverError::NXDomain, - NGX_RESOLVE_NOTIMP => ResolverError::NotImp, - NGX_RESOLVE_REFUSED => ResolverError::Refused, - NGX_RESOLVE_TIMEDOUT => ResolverError::TimedOut, - _ => ResolverError::Unknown(code.get()), - } - } -} - -type Res = Result, Error>; - -/// A wrapper for an ngx_resolver_t which provides an async Rust API -pub struct Resolver { - resolver: NonNull, - timeout: ngx_msec_t, -} - -impl Resolver { - /// Create a new `Resolver` from existing pointer to `ngx_resolver_t` and - /// timeout. - pub fn from_resolver(resolver: NonNull, timeout: ngx_msec_t) -> Self { - Self { resolver, timeout } - } - - /// Resolve a name into a set of addresses. - pub async fn resolve_name(&self, name: &ngx_str_t, pool: &Pool) -> Res { - let mut resolver = Resolution::new(name, &ngx_str_t::empty(), self, pool)?; - resolver.as_mut().await - } - - /// Resolve a service into a set of addresses. - pub async fn resolve_service(&self, name: &ngx_str_t, service: &ngx_str_t, pool: &Pool) -> Res { - let mut resolver = Resolution::new(name, service, self, pool)?; - resolver.as_mut().await - } -} - -struct Resolution<'a> { - // Storage for the result of the resolution `Res`. Populated by the - // callback handler, and taken by the Future::poll impl. - complete: Option, - // Storage for a pending Waker. Populated by the Future::poll impl, - // and taken by the callback handler. - waker: Option, - // Pool used for allocating `Vec` contents in `Res`. Read by - // the callback handler. - pool: &'a Pool, - // Owned pointer to the ngx_resolver_ctx_t. - ctx: Option, -} - -impl<'a> Resolution<'a> { - pub fn new( - name: &ngx_str_t, - service: &ngx_str_t, - resolver: &Resolver, - pool: &'a Pool, - ) -> Result>, Error> { - // Create a pinned Resolution on the Pool, so that we can make - // a stable pointer to the Resolution struct. - let mut this = - Box::pin_in(Resolution { complete: None, waker: None, pool, ctx: None }, pool.clone()); - - // Set up the ctx with everything the resolver needs to resolve a - // name, and the handler callback which is called on completion. - let mut ctx = ResolverCtx::new(resolver.resolver)?; - ctx.name = *name; - ctx.service = *service; - ctx.timeout = resolver.timeout; - ctx.set_cancelable(1); - ctx.handler = Some(Self::handler); - - { - // Safety: Self::handler, Future::poll, and Drop::drop will have - // access to &mut Resolution. Nginx is single-threaded and we are - // assured only one of those is on the stack at a time, except if - // Self::handler wakes a task which polls or drops the Future, - // which it only does after use of &mut Resolution is complete. - let ptr: &mut Resolution = unsafe { Pin::into_inner_unchecked(this.as_mut()) }; - ctx.data = ptr as *mut Resolution as *mut c_void; - } - - // Neither ownership nor borrows are tracked for this pointer, - // and ctxp will not be used after the ctx destruction. - let ctxp = ctx.0; - this.ctx = Some(ctx); - - // Start name resolution using the ctx. If the name is in the dns - // cache, the handler may get called from this stack. Otherwise, it - // will be called later by nginx when it gets a dns response or a - // timeout. - let ret = unsafe { Status(ngx_resolve_name(ctxp.as_ptr())) }; - if !ret.is_ok() { - // ngx_resolve_name will free the context on error. - core::mem::forget(this.ctx.take()); - return Err(Error::Internal); - } - - Ok(this) - } - - // Nginx will call this handler when name resolution completes. If the - // result is in the cache, this could be called from inside ngx_resolve_name. - // Otherwise, it will be called later on the event loop. - unsafe extern "C" fn handler(ctx: *mut ngx_resolver_ctx_t) { - let mut data = unsafe { NonNull::new_unchecked((*ctx).data as *mut Resolution) }; - let this: &mut Resolution = unsafe { data.as_mut() }; - - if let Some(ctx) = this.ctx.take() { - this.complete = Some(ctx.into_result(this.pool)); - } - - // Wake last, after all use of &mut Resolution, because wake may - // poll Resolution future on current stack. - if let Some(waker) = this.waker.take() { - waker.wake(); - } - } -} - -impl core::future::Future for Resolution<'_> { - type Output = Result, Error>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - // Resolution is Unpin, so we can use it as just a &mut Resolution - let this: &mut Resolution = self.get_mut(); - - // The handler populates this.complete, and we consume it here: - match this.complete.take() { - Some(res) => Poll::Ready(res), - None => { - // If the handler has not yet fired, populate the waker field, - // which the handler will consume: - match &mut this.waker { - None => { - this.waker = Some(cx.waker().clone()); - } - Some(w) => w.clone_from(cx.waker()), - } - Poll::Pending - } - } - } -} - -/// An owned ngx_resolver_ctx_t. -struct ResolverCtx(NonNull); - -impl core::ops::Deref for ResolverCtx { - type Target = ngx_resolver_ctx_t; - - fn deref(&self) -> &Self::Target { - // SAFETY: this wrapper is always constructed with a valid non-empty resolve context - unsafe { self.0.as_ref() } - } -} - -impl core::ops::DerefMut for ResolverCtx { - fn deref_mut(&mut self) -> &mut Self::Target { - // SAFETY: this wrapper is always constructed with a valid non-empty resolve context - unsafe { self.0.as_mut() } - } -} - -impl Drop for ResolverCtx { - fn drop(&mut self) { - unsafe { - nginx_sys::ngx_resolve_name_done(self.0.as_mut()); - } - } -} - -impl ResolverCtx { - /// Creates a new resolver context. - /// - /// This implementation currently passes a null for the second argument `temp`. A non-null - /// `temp` provides a fast, non-callback-based path for immediately returning an addr if - /// `temp` contains a name which is textual form of an addr. - pub fn new(resolver: NonNull) -> Result { - let ctx = unsafe { ngx_resolve_start(resolver.as_ptr(), core::ptr::null_mut()) }; - if ctx == NGX_NO_RESOLVER { - return Err(Error::NoResolver); - } - NonNull::new(ctx).map(Self).ok_or(Error::AllocationFailed) - } - - /// Take the results in a ctx and make an owned copy as a - /// Result, Error>, where the Vec and the internals - /// of the ngx_addr_t are allocated on the given Pool - pub fn into_result(self, pool: &Pool) -> Result, Error> { - if let Some(e) = NonZero::new(self.state) { - return Err(Error::Resolver(ResolverError::from(e), self.name.to_string())); - } - if self.addrs.is_null() { - Err(Error::AllocationFailed)?; - } - - let mut out = Vec::new_in(pool.clone()); - - if self.naddrs > 0 { - out.try_reserve_exact(self.naddrs).map_err(|_| Error::AllocationFailed)?; - - for addr in unsafe { core::slice::from_raw_parts(self.addrs, self.naddrs) } { - out.push(copy_resolved_addr(addr, pool)?); - } - } - - Ok(out) - } -} - -/// Take the contents of an ngx_resolver_addr_t and make an owned copy as -/// an ngx_addr_t, using the Pool for allocation of the internals. -fn copy_resolved_addr( - addr: &nginx_sys::ngx_resolver_addr_t, - pool: &Pool, -) -> Result { - let sockaddr = pool.alloc(addr.socklen as usize) as *mut nginx_sys::sockaddr; - if sockaddr.is_null() { - Err(Error::AllocationFailed)?; - } - unsafe { - addr.sockaddr.cast::().copy_to_nonoverlapping(sockaddr.cast(), addr.socklen as usize) - }; - - let name = unsafe { ngx_str_t::from_bytes(pool.as_ptr(), addr.name.as_bytes()) } - .ok_or(Error::AllocationFailed)?; - - Ok(ngx_addr_t { sockaddr, socklen: addr.socklen, name }) -} diff --git a/rust/vendor/ngx-rust/src/async_/sleep.rs b/rust/vendor/ngx-rust/src/async_/sleep.rs deleted file mode 100644 index 39feded..0000000 --- a/rust/vendor/ngx-rust/src/async_/sleep.rs +++ /dev/null @@ -1,135 +0,0 @@ -use core::future::Future; -use core::mem; -use core::pin::Pin; -use core::ptr::NonNull; -use core::task::{self, Poll}; -use core::time::Duration; - -use nginx_sys::{ngx_add_timer, ngx_del_timer, ngx_event_t, ngx_log_t, ngx_msec_int_t, ngx_msec_t}; -use pin_project_lite::pin_project; - -use crate::{ngx_container_of, ngx_log_debug}; - -/// Maximum duration that can be achieved using [ngx_add_timer]. -const NGX_TIMER_DURATION_MAX: Duration = Duration::from_millis(ngx_msec_int_t::MAX as _); - -/// Puts the current task to sleep for at least the specified amount of time. -/// -/// The function is a shorthand for [Sleep::new] using the global logger for debug output. -#[inline] -pub fn sleep(duration: Duration) -> Sleep { - Sleep::new(duration, crate::log::ngx_cycle_log()) -} - -pin_project! { -/// Future returned by [sleep]. -pub struct Sleep { - #[pin] - timer: TimerEvent, - duration: Duration, -} -} - -impl Sleep { - /// Creates a new Sleep with the specified duration and logger for debug messages. - pub fn new(duration: Duration, log: NonNull) -> Self { - let timer = TimerEvent::new(log); - ngx_log_debug!(timer.event.log, "async: sleep for {duration:?}"); - Sleep { timer, duration } - } -} - -impl Future for Sleep { - type Output = (); - - #[cfg(not(target_pointer_width = "32"))] - fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll { - let msec = self.duration.min(NGX_TIMER_DURATION_MAX).as_millis() as ngx_msec_t; - let this = self.project(); - this.timer.poll_sleep(msec, cx) - } - - #[cfg(target_pointer_width = "32")] - fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll { - if self.duration.is_zero() { - return Poll::Ready(()); - } - let step = self.duration.min(NGX_TIMER_DURATION_MAX); - - let mut this = self.project(); - // Handle ngx_msec_t overflow on 32-bit platforms. - match this.timer.as_mut().poll_sleep(step.as_millis() as _, cx) { - // Last step - Poll::Ready(()) if this.duration == &step => Poll::Ready(()), - Poll::Ready(()) => { - *this.duration = this.duration.saturating_sub(step); - this.timer.event.set_timedout(0); // rearm - this.timer.as_mut().poll_sleep(step.as_millis() as _, cx) - } - x => x, - } - } -} - -struct TimerEvent { - event: ngx_event_t, - waker: Option, -} - -// SAFETY: Timer will only be used in a single-threaded environment -unsafe impl Send for TimerEvent {} -unsafe impl Sync for TimerEvent {} - -impl TimerEvent { - pub fn new(log: NonNull) -> Self { - static IDENT: [usize; 4] = [ - 0, 0, 0, 0x4153594e, // ASYN - ]; - - let mut ev: ngx_event_t = unsafe { mem::zeroed() }; - // The data is only used for `ngx_event_ident` and will not be mutated. - ev.data = (&raw const IDENT).cast_mut().cast(); - ev.handler = Some(Self::timer_handler); - ev.log = log.as_ptr(); - ev.set_cancelable(1); - - Self { event: ev, waker: None } - } - - pub fn poll_sleep( - mut self: Pin<&mut Self>, - duration: ngx_msec_t, - context: &mut task::Context<'_>, - ) -> Poll<()> { - if self.event.timedout() != 0 { - Poll::Ready(()) - } else if self.event.timer_set() != 0 { - if let Some(waker) = self.waker.as_mut() { - waker.clone_from(context.waker()); - } else { - self.waker = Some(context.waker().clone()); - } - Poll::Pending - } else { - unsafe { ngx_add_timer(&raw mut self.event, duration) }; - self.waker = Some(context.waker().clone()); - Poll::Pending - } - } - - unsafe extern "C" fn timer_handler(ev: *mut ngx_event_t) { - let timer = ngx_container_of!(ev, Self, event); - - if let Some(waker) = unsafe { (*timer).waker.take() } { - waker.wake(); - } - } -} - -impl Drop for TimerEvent { - fn drop(&mut self) { - if self.event.timer_set() != 0 { - unsafe { ngx_del_timer(&raw mut self.event) }; - } - } -} diff --git a/rust/vendor/ngx-rust/src/async_/spawn.rs b/rust/vendor/ngx-rust/src/async_/spawn.rs deleted file mode 100644 index fa6008f..0000000 --- a/rust/vendor/ngx-rust/src/async_/spawn.rs +++ /dev/null @@ -1,146 +0,0 @@ -use alloc::collections::vec_deque::VecDeque; -use core::cell::UnsafeCell; -use core::future::Future; -use core::mem; -use core::ptr::{self, NonNull}; - -pub use async_task::Task; -use async_task::{Runnable, ScheduleInfo, WithInfo}; -use nginx_sys::{ - ngx_del_timer, ngx_delete_posted_event, ngx_event_t, ngx_post_event, ngx_posted_next_events, -}; - -use crate::log::ngx_cycle_log; -use crate::{ngx_container_of, ngx_log_debug}; - -static SCHEDULER: Scheduler = Scheduler::new(); - -struct Scheduler(UnsafeCell); - -// SAFETY: Scheduler must only be used from the main thread of a worker process. -unsafe impl Send for Scheduler {} -unsafe impl Sync for Scheduler {} - -impl Scheduler { - const fn new() -> Self { - Self(SchedulerInner::new()) - } - - pub fn schedule(&self, runnable: Runnable) { - // SAFETY: the cell is not empty, and we have exclusive access due to being a - // single-threaded application. - let inner = unsafe { &mut *UnsafeCell::raw_get(&raw const self.0) }; - inner.send(runnable) - } -} - -#[repr(C)] -struct SchedulerInner { - _ident: [usize; 4], // `ngx_event_ident` compatibility - event: ngx_event_t, - queue: VecDeque, -} - -impl SchedulerInner { - const fn new() -> UnsafeCell { - let mut event: ngx_event_t = unsafe { mem::zeroed() }; - event.handler = Some(Self::scheduler_event_handler); - - UnsafeCell::new(Self { - _ident: [ - 0, 0, 0, 0x4153594e, // ASYN - ], - event, - queue: VecDeque::new(), - }) - } - - pub fn send(&mut self, runnable: Runnable) { - // Cached `ngx_cycle.log` can be invalidated when reloading configuration in a single - // process mode. Update `log` every time to avoid using stale log pointer. - self.event.log = ngx_cycle_log().as_ptr(); - - // While this event is not used as a timer at the moment, we still want to ensure that it is - // compatible with `ngx_event_ident`. - if self.event.data.is_null() { - self.event.data = ptr::from_mut(self).cast(); - } - - // FIXME: VecDeque::push could panic on an allocation failure, switch to a datastructure - // which will not and propagate the failure. - self.queue.push_back(runnable); - unsafe { ngx_post_event(&raw mut self.event, &raw mut ngx_posted_next_events) } - } - - /// This event handler is called by ngx_event_process_posted at the end of - /// ngx_process_events_and_timers. - extern "C" fn scheduler_event_handler(ev: *mut ngx_event_t) { - let mut runnables = { - // SAFETY: - // This handler always receives a non-null pointer to an event embedded into a - // UnsafeCell instance. We modify the contents of the `UnsafeCell`, - // but we ensured that: - // - we access the cell correctly, as documented in https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html#memory-layout - // - the access is unique due to being single-threaded - // - the reference is dropped before we start processing queued runnables. - let cell: NonNull> = - ngx_container_of!(unsafe { NonNull::new_unchecked(ev) }, Self, event).cast(); - let this = unsafe { &mut *UnsafeCell::raw_get(cell.as_ptr()) }; - - ngx_log_debug!( - this.event.log, - "async: processing {} deferred wakeups", - this.queue.len() - ); - - // Move runnables to a new queue to avoid borrowing from the SchedulerInner and limit - // processing to already queued wakeups. This ensures that we correctly handle tasks - // that keep scheduling themselves (e.g. using yield_now() in a loop). - // We can't use drain() as it borrows from self and breaks aliasing rules. - mem::take(&mut this.queue) - }; - - for runnable in runnables.drain(..) { - runnable.run(); - } - } -} - -impl Drop for SchedulerInner { - fn drop(&mut self) { - if self.event.posted() != 0 { - unsafe { ngx_delete_posted_event(&raw mut self.event) }; - } - - if self.event.timer_set() != 0 { - unsafe { ngx_del_timer(&raw mut self.event) }; - } - } -} - -fn schedule(runnable: Runnable, _info: ScheduleInfo) { - // Always defer the wake via `ngx_post_event`; never re-poll synchronously. - // - // `Waker::wake()` may fire from arbitrary contexts, including a future's - // `Drop` while a lock is held (e.g. h2's `Streams::drop` wakes its parked - // `Connection` task while holding `Arc>`). A synchronous - // re-poll would re-enter the task and deadlock on that lock. Deferring - // costs one event-loop tick: `ngx_event_process_posted` drains the queue - // at the end of each cycle. - SCHEDULER.schedule(runnable); -} - -/// Creates a new task running on the NGINX event loop. -pub fn spawn(future: F) -> Task -where - F: Future + 'static, - T: 'static, -{ - ngx_log_debug!(ngx_cycle_log().as_ptr(), "async: spawning new task"); - let scheduler = WithInfo(schedule); - // Safety: single threaded embedding takes care of send/sync requirements for future and - // scheduler. Future and scheduler are both 'static. - let (runnable, task) = unsafe { async_task::spawn_unchecked(future, scheduler) }; - runnable.schedule(); - task -} diff --git a/rust/vendor/ngx-rust/src/collections/mod.rs b/rust/vendor/ngx-rust/src/collections/mod.rs deleted file mode 100644 index ce36eb3..0000000 --- a/rust/vendor/ngx-rust/src/collections/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -//! Collection types. -//! -//! This module provides common collection types, mostly implemented as wrappers over the -//! corresponding NGINX types. - -#[cfg(feature = "alloc")] -pub use allocator_api2::{ - collections::{TryReserveError, TryReserveErrorKind}, - vec, // reexport both the module and the macro - vec::Vec, -}; -pub use queue::Queue; -pub use rbtree::RbTreeMap; - -pub mod queue; -pub mod rbtree; diff --git a/rust/vendor/ngx-rust/src/collections/queue.rs b/rust/vendor/ngx-rust/src/collections/queue.rs deleted file mode 100644 index 3adfe2a..0000000 --- a/rust/vendor/ngx-rust/src/collections/queue.rs +++ /dev/null @@ -1,419 +0,0 @@ -//! Types and utilities for working with [ngx_queue_t], an intrusive doubly-linked list. -//! -//! This module provides both the tools for interaction with the existing `ngx_queue_t` objects in -//! the NGINX, and useful high-level types built on top of the `ngx_queue_t`. -//! -//! See . - -use core::alloc::Layout; -use core::marker::PhantomData; -use core::mem; -use core::ptr::{self, NonNull}; - -use nginx_sys::{ - ngx_queue_data, ngx_queue_empty, ngx_queue_init, ngx_queue_insert_after, - ngx_queue_insert_before, ngx_queue_remove, ngx_queue_t, -}; - -use crate::allocator::{AllocError, Allocator}; - -/// Trait for pointer conversions between the queue entry and its container. -/// -/// # Safety -/// -/// This trait must only be implemented on types that contain a queue link or wrappers with -/// compatible layout. The type then can be used to access elements of a raw queue type -/// [NgxQueue] linked via specified field. -/// -/// If the struct can belong to several queues through multiple embedded `ngx_queue_t` fields, -/// a separate [NgxQueueEntry] implementation via wrapper type should be used for each queue. -pub unsafe trait NgxQueueEntry { - /// Gets a container pointer from queue node. - fn from_queue(queue: NonNull) -> NonNull; - /// Gets a queue node from a container reference. - fn to_queue(&mut self) -> &mut ngx_queue_t; -} - -unsafe impl NgxQueueEntry for ngx_queue_t { - fn from_queue(queue: NonNull) -> NonNull { - queue - } - - fn to_queue(&mut self) -> &mut ngx_queue_t { - self - } -} - -/// A wrapper over a raw `ngx_queue_t`, an intrusive doubly-linked list. -/// -/// This wrapper is defined in terms of type `T` that embeds and can be converted from or to the -/// list entries. -/// -/// Example: -/// ```rust,no_run -/// # use core::ptr::NonNull; -/// # use nginx_sys::{ngx_event_t, ngx_posted_events, ngx_queue_data, ngx_queue_t}; -/// # use ngx::collections::queue::{NgxQueue, NgxQueueEntry}; -/// // We need a wrapper type to define [NgxQueueEntry] on. -/// #[repr(transparent)] -/// struct PostedEvent(ngx_event_t); -/// -/// unsafe impl NgxQueueEntry for PostedEvent { -/// fn from_queue(queue: NonNull) -> NonNull { -/// // We can safely cast obtained ngx_event_t to a transparent wrapper. -/// unsafe { ngx_queue_data!(queue, ngx_event_t, queue) }.cast() -/// } -/// -/// fn to_queue(&mut self) -> &mut ngx_queue_t { -/// &mut self.0.queue -/// } -/// } -/// -/// // SAFETY: `ngx_posted_events` global static is a list of `ngx_event_t` linked via -/// // `ngx_event_t.queue`. -/// // NGINX is single-threaded, so we get exclusive access to the static. -/// let posted: &mut NgxQueue = -/// unsafe { NgxQueue::from_ptr_mut(&raw mut ngx_posted_events) }; -/// ``` -/// -/// See . -#[derive(Debug)] -#[repr(transparent)] -pub struct NgxQueue { - head: ngx_queue_t, - _type: PhantomData, -} - -impl NgxQueue -where - T: NgxQueueEntry, -{ - /// Creates a queue reference from a pointer to [ngx_queue_t]. - /// - /// # Safety - /// - /// `head` is a valid pointer to a list head, and `T::from_queue` on the list entries results in - /// valid pointers to `T`. - pub unsafe fn from_ptr<'a>(head: *const ngx_queue_t) -> &'a Self { - unsafe { &*head.cast() } - } - - /// Creates a mutable queue reference from a pointer to [ngx_queue_t]. - /// - /// # Safety - /// - /// `head` is a valid pointer to a list head, and `T::from_queue` on the list entries results in - /// valid pointers to `T`. - pub unsafe fn from_ptr_mut<'a>(head: *mut ngx_queue_t) -> &'a mut Self { - unsafe { &mut *head.cast() } - } - - /// Returns `true` if the queue contains no elements. - pub fn is_empty(&self) -> bool { - self.head.prev.is_null() || unsafe { ngx_queue_empty(&raw const self.head) } - } - - /// Appends an element to the end of the queue. - pub fn push_back(&mut self, entry: &mut T) { - if self.head.prev.is_null() { - unsafe { ngx_queue_init(&raw mut self.head) } - } - - unsafe { ngx_queue_insert_before(&raw mut self.head, entry.to_queue()) } - } - - /// Appends an element to the beginning of the queue. - pub fn push_front(&mut self, entry: &mut T) { - if self.head.prev.is_null() { - unsafe { ngx_queue_init(&raw mut self.head) } - } - - unsafe { ngx_queue_insert_after(&raw mut self.head, entry.to_queue()) } - } - - /// Returns an iterator over the entries of the queue. - pub fn iter(&self) -> NgxQueueIter<'_, T> { - NgxQueueIter::new(&self.head) - } - - /// Returns a mutable iterator over the entries of the queue. - pub fn iter_mut(&mut self) -> NgxQueueIterMut<'_, T> { - NgxQueueIterMut::new(&mut self.head) - } -} - -/// An iterator for the queue. -pub struct NgxQueueIter<'a, T> { - head: NonNull, - current: NonNull, - _lifetime: PhantomData<&'a T>, -} - -impl<'a, T> NgxQueueIter<'a, T> -where - T: NgxQueueEntry, -{ - /// Creates a new queue iterator. - pub fn new(head: &'a ngx_queue_t) -> Self { - let head = NonNull::from(head); - NgxQueueIter { head, current: head, _lifetime: PhantomData } - } -} - -impl<'a, T> Iterator for NgxQueueIter<'a, T> -where - T: NgxQueueEntry + 'a, -{ - type Item = &'a T; - - fn next(&mut self) -> Option { - unsafe { - let next = NonNull::new(self.current.as_ref().next)?; - if next == self.head { - return None; - } - - self.current = next; - Some(T::from_queue(self.current).as_ref()) - } - } -} - -/// A mutable iterator for the queue. -pub struct NgxQueueIterMut<'a, T> { - head: NonNull, - current: NonNull, - _lifetime: PhantomData<&'a T>, -} - -impl<'a, T> NgxQueueIterMut<'a, T> -where - T: NgxQueueEntry, -{ - /// Creates a new mutable queue iterator. - pub fn new(head: &'a mut ngx_queue_t) -> Self { - let head = NonNull::from(head); - NgxQueueIterMut { head, current: head, _lifetime: PhantomData } - } -} - -impl<'a, T> Iterator for NgxQueueIterMut<'a, T> -where - T: NgxQueueEntry + 'a, -{ - type Item = &'a mut T; - - fn next(&mut self) -> Option { - unsafe { - let next = NonNull::new(self.current.as_ref().next)?; - if next == self.head { - return None; - } - - self.current = next; - Some(T::from_queue(self.current).as_mut()) - } - } -} - -/// A doubly-linked list that owns elements of type `T` backed by the specified allocator `A`. -#[derive(Debug)] -pub struct Queue -where - A: Allocator, -{ - // The address of the NgxQueue with queue head has to be stable, as the queue elements will - // contain pointers to the head. - raw: NonNull>>, - len: usize, - alloc: A, -} - -impl Drop for Queue -where - A: Allocator, -{ - fn drop(&mut self) { - while self.pop_front().is_some() {} - - let layout = Layout::for_value(unsafe { self.raw.as_ref() }); - unsafe { self.allocator().deallocate(self.raw.cast(), layout) }; - } -} - -unsafe impl Send for Queue -where - A: Send + Allocator, - T: Send, -{ -} - -unsafe impl Sync for Queue -where - A: Sync + Allocator, - T: Sync, -{ -} - -impl Queue { - /// Creates a new list with specified allocator. - pub fn try_new_in(alloc: A) -> Result { - let raw = NgxQueue { head: unsafe { mem::zeroed() }, _type: PhantomData }; - let raw = crate::allocator::allocate(raw, &alloc)?; - Ok(Self { raw, len: 0, alloc }) - } - - /// Returns a reference to the underlying allocator. - pub fn allocator(&self) -> &A { - &self.alloc - } - - /// Returns `true` if the list contains no elements. - pub fn is_empty(&self) -> bool { - self.raw().is_empty() - } - - /// Returns the number of elements in the queue. - pub fn len(&self) -> usize { - self.len - } - - /// Returns an iterator over the entries of the list. - pub fn iter(&self) -> QueueIter<'_, T> { - QueueIter::new(&self.raw().head) - } - - /// Returns a mutable iterator over the entries of the list. - pub fn iter_mut(&mut self) -> QueueIterMut<'_, T> { - QueueIterMut::new(&mut self.raw_mut().head) - } - - /// Removes the last element and returns it or `None` if the list is empty. - pub fn pop_back(&mut self) -> Option { - if self.is_empty() { - return None; - } - let node = NonNull::new(self.raw_mut().head.prev)?; - Some(unsafe { self.remove(node) }) - } - - /// Removes the first element and returns it or `None` if the list is empty. - pub fn pop_front(&mut self) -> Option { - if self.is_empty() { - return None; - } - let node = NonNull::new(self.raw_mut().head.next)?; - Some(unsafe { self.remove(node) }) - } - - /// Appends an element to the end of the list. - pub fn push_back(&mut self, item: T) -> Result<&mut T, AllocError> { - let mut entry = QueueEntry::new_in(item, self.allocator())?; - let entry = unsafe { entry.as_mut() }; - self.raw_mut().push_back(entry); - self.len += 1; - Ok(&mut entry.item) - } - - /// Appends an element to the beginning of the list. - pub fn push_front(&mut self, item: T) -> Result<&mut T, AllocError> { - let mut entry = QueueEntry::new_in(item, self.allocator())?; - let entry = unsafe { entry.as_mut() }; - self.raw_mut().push_front(entry); - self.len += 1; - Ok(&mut entry.item) - } - - fn raw(&self) -> &NgxQueue> { - // SAFETY: we allocated this pointer as well-aligned and convertible to reference. - unsafe { self.raw.as_ref() } - } - - fn raw_mut(&mut self) -> &mut NgxQueue> { - // SAFETY: we allocated this pointer as well-aligned and convertible to reference. - unsafe { self.raw.as_mut() } - } - - /// Removes a node from the queue and returns the contained value. - /// - /// # Safety - /// - /// `node` must be an element of this list. - unsafe fn remove(&mut self, node: NonNull) -> T { - unsafe { ngx_queue_remove(node.as_ptr()) }; - self.len -= 1; - - let entry = QueueEntry::::from_queue(node); - let copy = unsafe { entry.read() }; - // Skip drop as QueueEntry is already copied to `x`. - unsafe { self.allocator().deallocate(entry.cast(), Layout::for_value(entry.as_ref())) }; - copy.item - } -} - -#[derive(Debug)] -struct QueueEntry { - queue: ngx_queue_t, - item: T, -} - -unsafe impl NgxQueueEntry for QueueEntry { - fn from_queue(queue: NonNull) -> NonNull { - unsafe { ngx_queue_data!(queue, Self, queue) } - } - - fn to_queue(&mut self) -> &mut ngx_queue_t { - &mut self.queue - } -} - -impl QueueEntry { - pub fn new_in(item: T, alloc: &impl Allocator) -> Result, AllocError> { - let p: NonNull = alloc.allocate(Layout::new::())?.cast(); - - unsafe { - let u = p.cast::>().as_mut(); - // does not read the uninitialized data - ngx_queue_init(&raw mut u.assume_init_mut().queue); - ptr::write(&raw mut u.assume_init_mut().item, item); - } - - Ok(p) - } -} - -/// An iterator for the linked list [Queue]. -pub struct QueueIter<'a, T>(NgxQueueIter<'a, QueueEntry>); - -impl<'a, T> QueueIter<'a, T> { - /// Creates a new iterator for the linked list. - pub fn new(head: &'a ngx_queue_t) -> Self { - Self(NgxQueueIter::new(head)) - } -} - -impl<'a, T> Iterator for QueueIter<'a, T> { - type Item = &'a T; - - fn next(&mut self) -> Option { - Some(&self.0.next()?.item) - } -} - -/// A mutable iterator for the linked list [Queue]. -pub struct QueueIterMut<'a, T>(NgxQueueIterMut<'a, QueueEntry>); - -impl<'a, T> QueueIterMut<'a, T> { - /// Creates a new mutable iterator for the linked list. - pub fn new(head: &'a mut ngx_queue_t) -> Self { - Self(NgxQueueIterMut::new(head)) - } -} - -impl<'a, T> Iterator for QueueIterMut<'a, T> { - type Item = &'a mut T; - - fn next(&mut self) -> Option { - Some(&mut self.0.next()?.item) - } -} diff --git a/rust/vendor/ngx-rust/src/collections/rbtree.rs b/rust/vendor/ngx-rust/src/collections/rbtree.rs deleted file mode 100644 index 64692d5..0000000 --- a/rust/vendor/ngx-rust/src/collections/rbtree.rs +++ /dev/null @@ -1,472 +0,0 @@ -//! Types and utilities for working with [ngx_rbtree_t]. -//! -//! This module provides both the tools for interaction with the existing `ngx_rbtree_t` objects in -//! the NGINX, and useful high-level types built on top of the `ngx_rbtree_t`. -//! -//! See . - -use core::alloc::Layout; -use core::cmp::Ordering; -use core::hash::{self, BuildHasher, Hash}; -use core::marker::PhantomData; -use core::ptr::{self, NonNull}; -use core::{borrow, mem}; - -use nginx_sys::{ - ngx_rbt_red, ngx_rbtree_data, ngx_rbtree_delete, ngx_rbtree_init, ngx_rbtree_insert, - ngx_rbtree_key_t, ngx_rbtree_min, ngx_rbtree_next, ngx_rbtree_node_t, ngx_rbtree_t, -}; - -use crate::allocator::{self, AllocError, Allocator}; - -/// Trait for pointer conversions between the tree entry and its container. -/// -/// # Safety -/// -/// This trait must only be implemented on types that contain a tree node or wrappers with -/// compatible layout. The type then can be used to access elements of a raw rbtree type -/// [NgxRbTree] linked via specified field. -/// -/// If the struct can belong to several trees through multiple embedded `ngx_rbtree_node_t` fields, -/// a separate [NgxRbTreeEntry] implementation via wrapper type should be used for each tree. -pub unsafe trait NgxRbTreeEntry { - /// Gets a container pointer from tree node. - fn from_rbtree_node(node: NonNull) -> NonNull; - /// Gets an rbtree node from a container reference. - fn to_rbtree_node(&mut self) -> &mut ngx_rbtree_node_t; -} - -unsafe impl NgxRbTreeEntry for ngx_rbtree_node_t { - fn from_rbtree_node(node: NonNull) -> NonNull { - node - } - - fn to_rbtree_node(&mut self) -> &mut ngx_rbtree_node_t { - self - } -} - -/// A wrapper over a raw `ngx_rbtree_t`, a red-black tree implementation. -/// -/// This wrapper is defined in terms of type `T` that embeds and can be converted from or to the -/// tree nodes. -/// -/// See . -#[derive(Debug)] -#[repr(transparent)] -pub struct NgxRbTree { - inner: ngx_rbtree_t, - _type: PhantomData, -} - -impl NgxRbTree -where - T: NgxRbTreeEntry, -{ - /// Creates a tree reference from a pointer to [ngx_rbtree_t]. - /// - /// # Safety - /// - /// `tree` is a valid pointer to [ngx_rbtree_t], and `T::from_rbtree_node` on the tree nodes - /// results in valid pointers to `T`. - pub unsafe fn from_ptr<'a>(tree: *const ngx_rbtree_t) -> &'a Self { - unsafe { &*tree.cast() } - } - - /// Creates a mutable tree reference from a pointer to [ngx_rbtree_t]. - /// - /// # Safety - /// - /// `tree` is a valid pointer to [ngx_rbtree_t], and `T::from_rbtree_node` on the tree nodes - /// results in valid pointers to `T`. - pub unsafe fn from_ptr_mut<'a>(tree: *mut ngx_rbtree_t) -> &'a mut Self { - unsafe { &mut *tree.cast() } - } - - /// Returns `true` if the tree contains no elements. - pub fn is_empty(&self) -> bool { - ptr::addr_eq(self.inner.root, self.inner.sentinel) - } - - /// Appends a node to the tree. - pub fn insert(&mut self, node: &mut T) { - unsafe { ngx_rbtree_insert(&raw mut self.inner, node.to_rbtree_node()) }; - } - - /// Removes the specified node from the tree. - pub fn remove(&mut self, node: &mut T) { - unsafe { ngx_rbtree_delete(&raw mut self.inner, node.to_rbtree_node()) }; - } - - /// Returns an iterator over the nodes of the tree. - pub fn iter(&self) -> NgxRbTreeIter<'_> { - unsafe { NgxRbTreeIter::new(NonNull::from(&self.inner)) } - } - - /// Returns a mutable iterator over the nodes of the tree. - pub fn iter_mut(&mut self) -> NgxRbTreeIter<'_> { - unsafe { NgxRbTreeIter::new(NonNull::from(&mut self.inner)) } - } -} - -/// Raw iterator over the `ngx_rbtree_t` nodes. -/// -/// This iterator type can be used to access elements of any correctly initialized `ngx_rbtree_t` -/// instance, including those already embedded in the nginx structures. The iterator stores pointer -/// to the next node and thus remains valid and usable even if the last returned item is removed -/// from the tree. -pub struct NgxRbTreeIter<'a> { - tree: NonNull, - node: *mut ngx_rbtree_node_t, - _lifetime: PhantomData<&'a ()>, -} - -impl NgxRbTreeIter<'_> { - /// Creates an iterator for the `ngx_rbtree_t`. - /// - /// # Safety - /// - /// The tree must outlive the iterator. - pub unsafe fn new(tree: NonNull) -> Self { - let t = unsafe { tree.as_ref() }; - let node = if ptr::addr_eq(t.root, t.sentinel) { - // empty tree - ptr::null_mut() - } else { - unsafe { ngx_rbtree_min(t.root, t.sentinel) } - }; - - Self { tree, node, _lifetime: PhantomData } - } -} - -impl Iterator for NgxRbTreeIter<'_> { - type Item = NonNull; - - fn next(&mut self) -> Option { - let item = NonNull::new(self.node)?; - // ngx_rbtree_next does not mutate the tree - self.node = unsafe { ngx_rbtree_next(self.tree.as_mut(), self.node) }; - Some(item) - } -} - -#[allow(deprecated)] -type BuildMapHasher = core::hash::BuildHasherDefault; - -/// A map type based on the `ngx_rbtree_t`. -/// -/// This map implementation owns the stored keys and values and ensures that the data is dropped. -/// The order of the elements is an undocumented implementation detail. -/// -/// This is a `ngx`-specific high-level type with no direct counterpart in the NGINX code. -#[derive(Debug)] -pub struct RbTreeMap -where - A: Allocator, -{ - tree: NgxRbTree>, - sentinel: NonNull, - alloc: A, -} - -/// Entry type for the [RbTreeMap]. -/// -/// The struct is used from the Rust code only and thus does not need to be compatible with C. -#[derive(Debug)] -struct MapEntry { - node: ngx_rbtree_node_t, - key: K, - value: V, -} - -impl MapEntry -where - K: Hash, -{ - fn new(key: K, value: V) -> Self { - let mut node: ngx_rbtree_node_t = unsafe { mem::zeroed() }; - node.key = BuildMapHasher::default().hash_one(&key) as ngx_rbtree_key_t; - - Self { node, key, value } - } - - fn into_kv(self) -> (K, V) { - (self.key, self.value) - } -} - -unsafe impl NgxRbTreeEntry for MapEntry { - fn from_rbtree_node(node: NonNull) -> NonNull { - unsafe { ngx_rbtree_data!(node, Self, node) } - } - - fn to_rbtree_node(&mut self) -> &mut ngx_rbtree_node_t { - &mut self.node - } -} - -/// An iterator for the [RbTreeMap]. -pub struct MapIter<'a, K: 'a, V: 'a>(NgxRbTreeIter<'a>, PhantomData<(K, V)>); - -impl<'a, K: 'a, V: 'a> MapIter<'a, K, V> { - /// Creates an iterator for the [RbTreeMap]. - pub fn new(tree: &'a RbTreeMap) -> Self { - // msrv(1.89.0): NonNull::from_ref() - let rbtree = NonNull::from(&tree.tree.inner); - // SAFETY: Iter borrows from the tree, ensuring that the tree would outlive it. - Self(unsafe { NgxRbTreeIter::new(rbtree) }, Default::default()) - } -} - -impl<'a, K: 'a, V: 'a> Iterator for MapIter<'a, K, V> { - type Item = (&'a K, &'a V); - - fn next(&mut self) -> Option { - let item = self.0.next()?; - let item = unsafe { ngx_rbtree_data!(item, MapEntry, node).as_ref() }; - Some((&item.key, &item.value)) - } -} - -/// A mutable iterator for the [RbTreeMap]. -pub struct MapIterMut<'a, K: 'a, V: 'a>(NgxRbTreeIter<'a>, PhantomData<(K, V)>); - -impl<'a, K: 'a, V: 'a> MapIterMut<'a, K, V> { - /// Creates an iterator for the [RbTreeMap]. - pub fn new(tree: &'a mut RbTreeMap) -> Self { - // msrv(1.89.0): NonNull::from_mut() - let rbtree = NonNull::from(&mut tree.tree.inner); - // SAFETY: IterMut borrows from the tree, ensuring that the tree would outlive it. - Self(unsafe { NgxRbTreeIter::new(rbtree) }, Default::default()) - } -} - -impl<'a, K: 'a, V: 'a> Iterator for MapIterMut<'a, K, V> { - type Item = (&'a K, &'a mut V); - - fn next(&mut self) -> Option { - let mut item = MapEntry::::from_rbtree_node(self.0.next()?); - let item = unsafe { item.as_mut() }; - Some((&item.key, &mut item.value)) - } -} - -impl RbTreeMap -where - A: Allocator, -{ - /// Returns a reference to the underlying allocator. - pub fn allocator(&self) -> &A { - &self.alloc - } - - /// Clears the tree, removing all elements. - pub fn clear(&mut self) { - // SAFETY: the iter lives until the end of the scope - let iter = unsafe { NgxRbTreeIter::new(NonNull::from(&self.tree.inner)) }; - let layout = Layout::new::>(); - - for node in iter { - unsafe { - let mut data = MapEntry::::from_rbtree_node(node); - - ngx_rbtree_delete(&raw mut self.tree.inner, &raw mut data.as_mut().node); - ptr::drop_in_place(data.as_mut()); - self.allocator().deallocate(data.cast(), layout) - } - } - } - - /// Returns true if the tree contains no entries. - pub fn is_empty(&self) -> bool { - self.tree.is_empty() - } - - /// Returns an iterator over the entries of the tree. - #[inline] - pub fn iter(&self) -> MapIter<'_, K, V> { - MapIter::new(self) - } - - /// Returns a mutable iterator over the entries of the tree. - #[inline] - pub fn iter_mut(&mut self) -> MapIterMut<'_, K, V> { - MapIterMut::new(self) - } -} - -impl RbTreeMap -where - A: Allocator, - K: Hash + Ord, -{ - /// Attempts to create and initialize a new RbTreeMap with specified allocator. - pub fn try_new_in(alloc: A) -> Result { - let layout = Layout::new::(); - let sentinel: NonNull = alloc.allocate_zeroed(layout)?.cast(); - - let tree = NgxRbTree { inner: unsafe { mem::zeroed() }, _type: PhantomData }; - - let mut this = RbTreeMap { tree, sentinel, alloc }; - - unsafe { - ngx_rbtree_init(&raw mut this.tree.inner, this.sentinel.as_ptr(), Some(Self::insert)) - }; - - Ok(this) - } - - /// Returns a reference to the value corresponding to the key. - pub fn get(&self, key: &Q) -> Option<&V> - where - K: borrow::Borrow, - Q: Hash + Ord + ?Sized, - { - self.lookup(key).map(|x| unsafe { &x.as_ref().value }) - } - - /// Returns a mutable reference to the value corresponding to the key. - pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> - where - K: borrow::Borrow, - Q: Hash + Ord + ?Sized, - { - self.lookup(key).map(|mut x| unsafe { &mut x.as_mut().value }) - } - - /// Removes a key from the tree, returning the value at the key if the key was previously in the - /// tree. - pub fn remove(&mut self, key: &Q) -> Option - where - K: borrow::Borrow, - Q: Hash + Ord + ?Sized, - { - self.remove_entry(key).map(|(_, v)| v) - } - - /// Removes a key from the tree, returning the stored key and value if the key was previously in - /// the tree. - pub fn remove_entry(&mut self, key: &Q) -> Option<(K, V)> - where - K: borrow::Borrow, - Q: Hash + Ord + ?Sized, - { - let mut node = self.lookup(key)?; - unsafe { - self.tree.remove(node.as_mut()); - - let layout = Layout::for_value(node.as_ref()); - // SAFETY: we make a bitwise copy of the node and dispose of the original value without - // dropping it. - let copy = node.as_ptr().read(); - self.allocator().deallocate(node.cast(), layout); - Some(copy.into_kv()) - } - } - - /// Attempts to insert a new element into the tree. - pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, AllocError> { - let mut node = if let Some(mut node) = self.lookup(&key) { - unsafe { node.as_mut().value = value }; - node - } else { - let node = MapEntry::new(key, value); - let mut node = allocator::allocate(node, self.allocator())?; - self.tree.insert(unsafe { node.as_mut() }); - node - }; - - Ok(unsafe { &mut node.as_mut().value }) - } - - extern "C" fn insert( - mut temp: *mut ngx_rbtree_node_t, - node: *mut ngx_rbtree_node_t, - sentinel: *mut ngx_rbtree_node_t, - ) { - let n = unsafe { &mut *ngx_rbtree_data!(node, MapEntry, node) }; - - loop { - let t = unsafe { &mut *ngx_rbtree_data!(temp, MapEntry, node) }; - let p = match Ord::cmp(&n.node.key, &t.node.key) { - Ordering::Less => &mut t.node.left, - Ordering::Greater => &mut t.node.right, - Ordering::Equal => match Ord::cmp(&n.key, &t.key) { - Ordering::Less => &mut t.node.left, - Ordering::Greater => &mut t.node.right, - // should be handled in try_insert - Ordering::Equal => &mut t.node.right, - }, - }; - - if ptr::addr_eq(*p, sentinel) { - *p = node; - break; - } - - temp = *p; - } - - n.node.parent = temp; - n.node.left = sentinel; - n.node.right = sentinel; - unsafe { ngx_rbt_red(node) }; - } - - fn lookup(&self, key: &Q) -> Option>> - where - K: borrow::Borrow, - Q: Hash + Ord + ?Sized, - { - let mut node = self.tree.inner.root; - let hash = BuildMapHasher::default().hash_one(key) as ngx_rbtree_key_t; - - while !ptr::addr_eq(node, self.tree.inner.sentinel) { - let n = unsafe { NonNull::new_unchecked(ngx_rbtree_data!(node, MapEntry, node)) }; - let nr = unsafe { n.as_ref() }; - - node = match Ord::cmp(&hash, &nr.node.key) { - Ordering::Less => nr.node.left, - Ordering::Greater => nr.node.right, - Ordering::Equal => match Ord::cmp(key, nr.key.borrow()) { - Ordering::Less => nr.node.left, - Ordering::Greater => nr.node.right, - Ordering::Equal => return Some(n), - }, - } - } - - None - } -} - -impl Drop for RbTreeMap -where - A: Allocator, -{ - fn drop(&mut self) { - self.clear(); - - unsafe { - self.allocator() - .deallocate(self.sentinel.cast(), Layout::for_value(self.sentinel.as_ref())) - }; - } -} - -unsafe impl Send for RbTreeMap -where - A: Send + Allocator, - K: Send, - V: Send, -{ -} - -unsafe impl Sync for RbTreeMap -where - A: Sync + Allocator, - K: Sync, - V: Sync, -{ -} diff --git a/rust/vendor/ngx-rust/src/core/buffer.rs b/rust/vendor/ngx-rust/src/core/buffer.rs deleted file mode 100644 index cfd1282..0000000 --- a/rust/vendor/ngx-rust/src/core/buffer.rs +++ /dev/null @@ -1,129 +0,0 @@ -use core::slice; - -use crate::ffi::*; - -/// The `Buffer` trait provides methods for working with an nginx buffer (`ngx_buf_t`). -/// -/// See -pub trait Buffer { - /// Returns a raw pointer to the underlying `ngx_buf_t` of the buffer. - fn as_ngx_buf(&self) -> *const ngx_buf_t; - - /// Returns a mutable raw pointer to the underlying `ngx_buf_t` of the buffer. - fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t; - - /// Returns the buffer contents as a byte slice. - fn as_bytes(&self) -> &[u8] { - let buf = self.as_ngx_buf(); - unsafe { slice::from_raw_parts((*buf).pos, self.len()) } - } - - /// Returns the length of the buffer contents. - fn len(&self) -> usize { - let buf = self.as_ngx_buf(); - unsafe { - let pos = (*buf).pos; - let last = (*buf).last; - assert!(last >= pos); - usize::wrapping_sub(last as _, pos as _) - } - } - - /// Returns `true` if the buffer is empty, i.e., it has zero length. - fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Sets the `last_buf` flag of the buffer. - /// - /// # Arguments - /// - /// * `last` - A boolean indicating whether the buffer is the last buffer in a request. - fn set_last_buf(&mut self, last: bool) { - let buf = self.as_ngx_buf_mut(); - unsafe { - (*buf).set_last_buf(if last { 1 } else { 0 }); - } - } - - /// Sets the `last_in_chain` flag of the buffer. - /// - /// # Arguments - /// - /// * `last` - A boolean indicating whether the buffer is the last buffer in a chain of buffers. - fn set_last_in_chain(&mut self, last: bool) { - let buf = self.as_ngx_buf_mut(); - unsafe { - (*buf).set_last_in_chain(if last { 1 } else { 0 }); - } - } -} - -/// The `MutableBuffer` trait extends the `Buffer` trait and provides methods for working with a -/// mutable buffer. -pub trait MutableBuffer: Buffer { - /// Returns a mutable reference to the buffer contents as a byte slice. - fn as_bytes_mut(&mut self) -> &mut [u8] { - let buf = self.as_ngx_buf_mut(); - unsafe { slice::from_raw_parts_mut((*buf).pos, self.len()) } - } -} - -/// Wrapper struct for a temporary buffer, providing methods for working with an `ngx_buf_t`. -pub struct TemporaryBuffer(*mut ngx_buf_t); - -impl TemporaryBuffer { - /// Creates a new `TemporaryBuffer` from an `ngx_buf_t` pointer. - /// - /// # Panics - /// Panics if the given buffer pointer is null. - pub fn from_ngx_buf(buf: *mut ngx_buf_t) -> TemporaryBuffer { - assert!(!buf.is_null()); - TemporaryBuffer(buf) - } -} - -impl Buffer for TemporaryBuffer { - /// Returns the underlying `ngx_buf_t` pointer as a raw pointer. - fn as_ngx_buf(&self) -> *const ngx_buf_t { - self.0 - } - - /// Returns a mutable reference to the underlying `ngx_buf_t` pointer. - fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t { - self.0 - } -} - -impl MutableBuffer for TemporaryBuffer { - /// Returns a mutable reference to the buffer contents as a byte slice. - fn as_bytes_mut(&mut self) -> &mut [u8] { - unsafe { slice::from_raw_parts_mut((*self.0).pos, self.len()) } - } -} - -/// Wrapper struct for a memory buffer, providing methods for working with an `ngx_buf_t`. -pub struct MemoryBuffer(*mut ngx_buf_t); - -impl MemoryBuffer { - /// Creates a new `MemoryBuffer` from an `ngx_buf_t` pointer. - /// - /// # Panics - /// Panics if the given buffer pointer is null. - pub fn from_ngx_buf(buf: *mut ngx_buf_t) -> MemoryBuffer { - assert!(!buf.is_null()); - MemoryBuffer(buf) - } -} - -impl Buffer for MemoryBuffer { - /// Returns the underlying `ngx_buf_t` pointer as a raw pointer. - fn as_ngx_buf(&self) -> *const ngx_buf_t { - self.0 - } - - /// Returns a mutable reference to the underlying `ngx_buf_t` pointer. - fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t { - self.0 - } -} diff --git a/rust/vendor/ngx-rust/src/core/conf.rs b/rust/vendor/ngx-rust/src/core/conf.rs deleted file mode 100644 index 6da4c47..0000000 --- a/rust/vendor/ngx-rust/src/core/conf.rs +++ /dev/null @@ -1,179 +0,0 @@ -use core::ptr::NonNull; - -use crate::ffi::{ngx_core_conf_t, ngx_module_t}; - -/// Trait for core-style modules. -/// -/// This is the foundational trait that identifies a type as representing a -/// concrete NGINX core module. -pub trait CoreModule { - /// Returns the global `ngx_module_t` describing this module. - fn module() -> &'static ngx_module_t; -} - -/// Raw access to core module main configuration slots. -/// -/// This trait is implemented for NGINX-owned types such as `ngx_cycle_t` and `ngx_conf_t` -/// that carry configuration context pointers. It exposes the low-level lookup step that -/// retrieves a module's main configuration as an untyped pointer. -/// -/// Most callers should not use this trait directly. Prefer `CoreModuleMainConf` to obtain a -/// typed reference for a specific core module. -pub trait CoreModuleConfExt { - /// Get a non-null pointer to a core module's main configuration. - /// - /// # Safety - /// Caller must ensure that type `T` matches the configuration type for the specified module. - /// Supplying the wrong type will produce an invalid typed pointer. - #[inline] - unsafe fn core_main_conf_unchecked(&self, _module: &ngx_module_t) -> Option> { - None - } -} - -impl CoreModuleConfExt for crate::ffi::ngx_cycle_t { - #[inline] - unsafe fn core_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - let conf_ctx = NonNull::new(self.conf_ctx)?; - let conf = unsafe { *conf_ctx.as_ptr().add(module.index) }; - NonNull::new(conf.cast()) - } -} - -impl CoreModuleConfExt for crate::ffi::ngx_conf_t { - #[inline] - unsafe fn core_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.cycle.as_ref()?.core_main_conf_unchecked(module) } - } -} - -/// Typed access to a core module's main configuration. -/// -/// Implement this trait for a concrete core-style module to associate it with its main -/// configuration type and global `ngx_module_t`. The provided default methods build on top of -/// `CoreModuleConfExt` to turn the raw configuration slot lookup into typed references. -/// -/// # Safety -/// Caller must ensure that type `CoreModuleMainConf::MainConf` matches the configuration type -/// for the specified module. -pub unsafe trait CoreModuleMainConf: CoreModule { - /// Concrete type of this module's main configuration. - type MainConf; - - /// Get a typed shared reference to this module's main configuration. - fn main_conf(o: &impl CoreModuleConfExt) -> Option<&'static Self::MainConf> { - unsafe { Some(o.core_main_conf_unchecked(Self::module())?.as_ref()) } - } - - /// Get a typed mutable reference to this module's main configuration. - fn main_conf_mut(o: &impl CoreModuleConfExt) -> Option<&'static mut Self::MainConf> { - unsafe { Some(o.core_main_conf_unchecked(Self::module())?.as_mut()) } - } -} - -/// Auxiliary structure to access `ngx_core_module` configuration. -pub struct NgxCoreModule; - -impl CoreModule for NgxCoreModule { - fn module() -> &'static ngx_module_t { - unsafe { &*core::ptr::addr_of!(nginx_sys::ngx_core_module) } - } -} - -unsafe impl CoreModuleMainConf for NgxCoreModule { - type MainConf = ngx_core_conf_t; -} - -#[cfg(test)] -mod tests { - extern crate alloc; - extern crate std; - - use alloc::boxed::Box; - use core::ffi::c_void; - use core::mem::MaybeUninit; - - use super::{CoreModule, CoreModuleConfExt, CoreModuleMainConf}; - use crate::ffi::{ngx_conf_t, ngx_cycle_t, ngx_module_t}; - - type CoreConfSlot = *mut *mut *mut c_void; - - fn module_with_index(index: usize) -> ngx_module_t { - let mut module = ngx_module_t::default(); - module.index = index; - module - } - - #[test] - fn null_conf_ctx_returns_none() { - let cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; - let module = module_with_index(0); - assert!(unsafe { cycle.core_main_conf_unchecked::(&module) }.is_none()); - } - - #[test] - fn missing_module_slot_returns_none() { - let mut slots: [CoreConfSlot; 2] = [core::ptr::null_mut(); 2]; - let mut cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; - cycle.conf_ctx = slots.as_mut_ptr(); - - let module = module_with_index(1); - assert!(unsafe { cycle.core_main_conf_unchecked::(&module) }.is_none()); - } - - #[test] - fn populated_slot_returns_typed_reference() { - let mut value: u32 = 42; - let mut slots: [CoreConfSlot; 1] = [(&raw mut value).cast()]; - - let mut cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; - cycle.conf_ctx = slots.as_mut_ptr(); - - let mut conf: ngx_conf_t = unsafe { MaybeUninit::zeroed().assume_init() }; - conf.cycle = &raw mut cycle; - - let module = module_with_index(0); - - let got = unsafe { conf.core_main_conf_unchecked::(&module).map(|v| *v.as_ref()) }; - assert_eq!(got, Some(42)); - - let got_mut = - unsafe { conf.core_main_conf_unchecked::(&module).map(|mut v| v.as_mut()) }; - assert!(got_mut.is_some()); - if let Some(v) = got_mut { - *v = 99; - } - assert_eq!(value, 99); - } - - struct TestCoreModule; - - impl CoreModule for TestCoreModule { - fn module() -> &'static ngx_module_t { - Box::leak(Box::new(module_with_index(0))) - } - } - - unsafe impl CoreModuleMainConf for TestCoreModule { - type MainConf = u32; - } - - #[test] - fn main_conf_trait_accessors_return_typed_references() { - let mut value: u32 = 42; - let mut slots: [CoreConfSlot; 1] = [(&raw mut value).cast()]; - - let mut cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; - cycle.conf_ctx = slots.as_mut_ptr(); - - let mut conf: ngx_conf_t = unsafe { MaybeUninit::zeroed().assume_init() }; - conf.cycle = &raw mut cycle; - - assert_eq!(TestCoreModule::main_conf(&conf).copied(), Some(42)); - - if let Some(v) = TestCoreModule::main_conf_mut(&conf) { - *v = 99; - } - assert_eq!(value, 99); - } -} diff --git a/rust/vendor/ngx-rust/src/core/mod.rs b/rust/vendor/ngx-rust/src/core/mod.rs deleted file mode 100644 index 9b9a778..0000000 --- a/rust/vendor/ngx-rust/src/core/mod.rs +++ /dev/null @@ -1,27 +0,0 @@ -mod buffer; -mod conf; -mod pool; -pub mod slab; -mod status; -mod string; - -pub use buffer::*; -pub use conf::*; -pub use pool::*; -pub use slab::SlabPool; -pub use status::*; -pub use string::*; - -/// Gets an outer object pointer from a pointer to one of its fields. -/// While there is no corresponding C macro, the pattern is common in the NGINX source. -/// -/// # Safety -/// -/// `$ptr` must be a valid pointer to the field `$field` of `$type`. -#[macro_export] -macro_rules! ngx_container_of { - ($ptr:expr, $type:path, $field:ident) => {{ - let ptr = $ptr; - unsafe { ptr.byte_sub(::core::mem::offset_of!($type, $field)) }.cast::<$type>() - }}; -} diff --git a/rust/vendor/ngx-rust/src/core/pool.rs b/rust/vendor/ngx-rust/src/core/pool.rs deleted file mode 100644 index efb283e..0000000 --- a/rust/vendor/ngx-rust/src/core/pool.rs +++ /dev/null @@ -1,337 +0,0 @@ -use core::alloc::Layout; -use core::ffi::c_void; -use core::mem; -use core::ptr::{self, NonNull}; - -use nginx_sys::{ - NGX_ALIGNMENT, ngx_buf_t, ngx_create_temp_buf, ngx_palloc, ngx_pcalloc, ngx_pfree, - ngx_pmemalign, ngx_pnalloc, ngx_pool_cleanup_add, ngx_pool_t, -}; - -use crate::allocator::{AllocError, Allocator, dangling_for_layout}; -use crate::core::buffer::{Buffer, MemoryBuffer, TemporaryBuffer}; - -/// Non-owning wrapper for an [`ngx_pool_t`] pointer, providing methods for working with memory -/// pools. -/// -/// See -#[derive(Clone, Debug)] -#[repr(transparent)] -pub struct Pool(NonNull); - -unsafe impl Allocator for Pool { - fn allocate(&self, layout: Layout) -> Result, AllocError> { - // SAFETY: - // * This wrapper should be constructed with a valid pointer to ngx_pool_t. - // * The Pool type is !Send, thus we expect exclusive access for this call. - // * Pointers are considered mutable unless obtained from an immutable reference. - let ptr = if layout.size() == 0 { - // We can guarantee alignment <= NGX_ALIGNMENT for allocations of size 0 made with - // ngx_palloc_small. Any other cases are implementation-defined, and we can't tell which - // one will be used internally. - return Ok(NonNull::slice_from_raw_parts(dangling_for_layout(&layout), layout.size())); - } else if layout.align() == 1 { - unsafe { ngx_pnalloc(self.0.as_ptr(), layout.size()) } - } else if layout.align() <= NGX_ALIGNMENT { - unsafe { ngx_palloc(self.0.as_ptr(), layout.size()) } - } else if cfg!(any(ngx_feature = "have_posix_memalign", ngx_feature = "have_memalign")) { - // ngx_pmemalign is always defined, but does not guarantee the requested alignment - // unless memalign/posix_memalign exists. - unsafe { ngx_pmemalign(self.0.as_ptr(), layout.size(), layout.align()) } - } else { - return Err(AllocError); - }; - - // Verify the alignment of the result - debug_assert_eq!(ptr.align_offset(layout.align()), 0); - - let ptr = NonNull::new(ptr.cast()).ok_or(AllocError)?; - Ok(NonNull::slice_from_raw_parts(ptr, layout.size())) - } - - unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - // ngx_pfree is noop for small allocations unless NGX_DEBUG_PALLOC is set. - // - // Note: there should be no cleanup handlers for the allocations made using this API. - // Violating that could result in the following issues: - // - use-after-free on large allocation - // - multiple cleanup handlers attached to a dangling ptr (these are not unique) - if layout.size() > 0 // 0 is dangling ptr - && (layout.size() > self.as_ref().max || layout.align() > NGX_ALIGNMENT) - { - unsafe { ngx_pfree(self.0.as_ptr(), ptr.as_ptr().cast()) }; - } - } - - unsafe fn grow( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - debug_assert!( - new_layout.size() >= old_layout.size(), - "`new_layout.size()` must be greater than or equal to `old_layout.size()`" - ); - unsafe { self.resize(ptr, old_layout, new_layout) } - } - - unsafe fn grow_zeroed( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - debug_assert!( - new_layout.size() >= old_layout.size(), - "`new_layout.size()` must be greater than or equal to `old_layout.size()`" - ); - unsafe { - #[allow(clippy::manual_inspect)] - self.resize(ptr, old_layout, new_layout).map(|new_ptr| { - new_ptr - .cast::() - .byte_add(old_layout.size()) - .write_bytes(0, new_layout.size() - old_layout.size()); - new_ptr - }) - } - } - - unsafe fn shrink( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - debug_assert!( - new_layout.size() <= old_layout.size(), - "`new_layout.size()` must be smaller than or equal to `old_layout.size()`" - ); - unsafe { self.resize(ptr, old_layout, new_layout) } - } -} - -impl AsRef for Pool { - #[inline] - fn as_ref(&self) -> &ngx_pool_t { - // SAFETY: this wrapper should be constructed with a valid pointer to ngx_pool_t - unsafe { self.0.as_ref() } - } -} - -impl AsMut for Pool { - #[inline] - fn as_mut(&mut self) -> &mut ngx_pool_t { - // SAFETY: this wrapper should be constructed with a valid pointer to ngx_pool_t - unsafe { self.0.as_mut() } - } -} - -impl Pool { - /// Creates a new `Pool` from an `ngx_pool_t` pointer. - /// - /// # Safety - /// The caller must ensure that a valid `ngx_pool_t` pointer is provided, pointing to valid - /// memory and non-null. A null argument will cause an assertion failure and panic. - pub unsafe fn from_ngx_pool(pool: *mut ngx_pool_t) -> Pool { - unsafe { - debug_assert!(!pool.is_null()); - debug_assert!(pool.is_aligned()); - Pool(NonNull::new_unchecked(pool)) - } - } - - /// Expose the underlying `ngx_pool_t` pointer, for use with `ngx::ffi` - /// functions. - pub fn as_ptr(&self) -> *mut ngx_pool_t { - self.0.as_ptr() - } - - /// Creates a buffer of the specified size in the memory pool. - /// - /// Returns `Some(TemporaryBuffer)` if the buffer is successfully created, or `None` if - /// allocation fails. - pub fn create_buffer(&self, size: usize) -> Option { - let buf = unsafe { ngx_create_temp_buf(self.0.as_ptr(), size) }; - if buf.is_null() { - return None; - } - - Some(TemporaryBuffer::from_ngx_buf(buf)) - } - - /// Creates a buffer from a string in the memory pool. - /// - /// Returns `Some(TemporaryBuffer)` if the buffer is successfully created, or `None` if - /// allocation fails. - pub fn create_buffer_from_str(&self, str: &str) -> Option { - let mut buffer = self.create_buffer(str.len())?; - unsafe { - let buf = buffer.as_ngx_buf_mut(); - ptr::copy_nonoverlapping(str.as_ptr(), (*buf).pos, str.len()); - (*buf).last = (*buf).pos.add(str.len()); - } - Some(buffer) - } - - /// Creates a buffer from a static string in the memory pool. - /// - /// Returns `Some(MemoryBuffer)` if the buffer is successfully created, or `None` if allocation - /// fails. - pub fn create_buffer_from_static_str(&self, str: &'static str) -> Option { - let buf = self.calloc_type::(); - if buf.is_null() { - return None; - } - - // We cast away const, but buffers with the memory flag are read-only - let start = str.as_ptr() as *mut u8; - let end = unsafe { start.add(str.len()) }; - - unsafe { - (*buf).start = start; - (*buf).pos = start; - (*buf).last = end; - (*buf).end = end; - (*buf).set_memory(1); - } - - Some(MemoryBuffer::from_ngx_buf(buf)) - } - - /// Adds a cleanup handler for a value in the memory pool. - /// - /// Returns `Ok(())` if the cleanup handler is successfully added, or `Err(())` if the cleanup - /// handler cannot be added. - /// - /// # Safety - /// This function is marked as unsafe because it involves raw pointer manipulation. - unsafe fn add_cleanup_for_value(&self, value: *mut T) -> Result<(), ()> { - let cln = unsafe { ngx_pool_cleanup_add(self.0.as_ptr(), 0) }; - if cln.is_null() { - return Err(()); - } - - unsafe { - (*cln).handler = Some(cleanup_type::); - (*cln).data = value as *mut c_void; - } - - Ok(()) - } - - /// Allocates memory from the pool of the specified size. - /// The resulting pointer is aligned to a platform word size. - /// - /// Returns a raw pointer to the allocated memory. - pub fn alloc(&self, size: usize) -> *mut c_void { - unsafe { ngx_palloc(self.0.as_ptr(), size) } - } - - /// Allocates memory for a type from the pool. - /// The resulting pointer is aligned to a platform word size. - /// - /// Returns a typed pointer to the allocated memory. - pub fn alloc_type(&self) -> *mut T { - self.alloc(mem::size_of::()) as *mut T - } - - /// Allocates zeroed memory from the pool of the specified size. - /// The resulting pointer is aligned to a platform word size. - /// - /// Returns a raw pointer to the allocated memory. - pub fn calloc(&self, size: usize) -> *mut c_void { - unsafe { ngx_pcalloc(self.0.as_ptr(), size) } - } - - /// Allocates zeroed memory for a type from the pool. - /// The resulting pointer is aligned to a platform word size. - /// - /// Returns a typed pointer to the allocated memory. - pub fn calloc_type(&self) -> *mut T { - self.calloc(mem::size_of::()) as *mut T - } - - /// Allocates unaligned memory from the pool of the specified size. - /// - /// Returns a raw pointer to the allocated memory. - pub fn alloc_unaligned(&self, size: usize) -> *mut c_void { - unsafe { ngx_pnalloc(self.0.as_ptr(), size) } - } - - /// Allocates unaligned memory for a type from the pool. - /// - /// Returns a typed pointer to the allocated memory. - pub fn alloc_type_unaligned(&self) -> *mut T { - self.alloc_unaligned(mem::size_of::()) as *mut T - } - - /// Allocates memory for a value of a specified type and adds a cleanup handler to the memory - /// pool. - /// - /// Returns a typed pointer to the allocated memory if successful, or a null pointer if - /// allocation or cleanup handler addition fails. - pub fn allocate(&self, value: T) -> *mut T { - unsafe { - let p = self.alloc(mem::size_of::()) as *mut T; - ptr::write(p, value); - if self.add_cleanup_for_value(p).is_err() { - ptr::drop_in_place(p); - return ptr::null_mut(); - }; - p - } - } - - /// Resizes a memory allocation in place if possible. - /// - /// If resizing is requested for the last allocation in the pool, it may be - /// possible to adjust pool data and avoid any real allocations. - /// - /// # Safety - /// `ptr` must point to allocated address and `old_layout` must match the current layout - /// of the allocation. - #[inline(always)] - unsafe fn resize( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - if unsafe { - ptr.byte_add(old_layout.size()).as_ptr() == self.as_ref().d.last - && ptr.byte_add(new_layout.size()).as_ptr() <= self.as_ref().d.end - && ptr.align_offset(new_layout.align()) == 0 - } { - let pool = self.0.as_ptr(); - unsafe { (*pool).d.last = ptr.byte_add(new_layout.size()).as_ptr() }; - Ok(NonNull::slice_from_raw_parts(ptr, new_layout.size())) - } else { - let size = core::cmp::min(old_layout.size(), new_layout.size()); - let new_ptr = ::allocate(self, new_layout)?; - unsafe { - ptr.copy_to_nonoverlapping(new_ptr.cast(), size); - self.deallocate(ptr, old_layout); - } - Ok(new_ptr) - } - } -} - -/// Cleanup handler for a specific type `T`. -/// -/// This function is called when cleaning up a value of type `T` in an FFI context. -/// -/// # Safety -/// This function is marked as unsafe due to the raw pointer manipulation and the assumption that -/// `data` is a valid pointer to `T`. -/// -/// # Arguments -/// -/// * `data` - A raw pointer to the value of type `T` to be cleaned up. -unsafe extern "C" fn cleanup_type(data: *mut c_void) { - unsafe { - ptr::drop_in_place(data as *mut T); - } -} diff --git a/rust/vendor/ngx-rust/src/core/slab.rs b/rust/vendor/ngx-rust/src/core/slab.rs deleted file mode 100644 index 56bfb4e..0000000 --- a/rust/vendor/ngx-rust/src/core/slab.rs +++ /dev/null @@ -1,151 +0,0 @@ -//! Wrapper for the nginx slab pool allocator. -//! -//! See . -use core::alloc::Layout; -use core::cmp; -use core::ptr::NonNull; - -use nginx_sys::{ - ngx_shm_zone_t, ngx_shmtx_lock, ngx_shmtx_unlock, ngx_slab_alloc_locked, ngx_slab_free_locked, - ngx_slab_pool_t, -}; - -use crate::allocator::{AllocError, Allocator, dangling_for_layout}; - -/// Non-owning wrapper for an [`ngx_slab_pool_t`] pointer, providing methods for working with -/// shared memory slab pools. -/// -/// See . -#[derive(Clone, Debug)] -pub struct SlabPool(NonNull); - -unsafe impl Send for SlabPool {} -unsafe impl Sync for SlabPool {} - -unsafe impl Allocator for SlabPool { - #[inline] - fn allocate(&self, layout: Layout) -> Result, AllocError> { - self.lock().allocate(layout) - } - - #[inline] - unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - unsafe { self.lock().deallocate(ptr, layout) } - } - - #[inline] - fn allocate_zeroed(&self, layout: Layout) -> Result, AllocError> { - self.lock().allocate_zeroed(layout) - } - - #[inline] - unsafe fn grow( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - unsafe { self.lock().grow(ptr, old_layout, new_layout) } - } - - #[inline] - unsafe fn grow_zeroed( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - unsafe { self.lock().grow_zeroed(ptr, old_layout, new_layout) } - } - - #[inline] - unsafe fn shrink( - &self, - ptr: NonNull, - old_layout: Layout, - new_layout: Layout, - ) -> Result, AllocError> { - unsafe { self.lock().shrink(ptr, old_layout, new_layout) } - } -} - -impl AsRef for SlabPool { - #[inline] - fn as_ref(&self) -> &ngx_slab_pool_t { - // SAFETY: this wrapper should be constructed with a valid pointer to ngx_slab_pool_t - unsafe { self.0.as_ref() } - } -} - -impl AsMut for SlabPool { - #[inline] - fn as_mut(&mut self) -> &mut ngx_slab_pool_t { - // SAFETY: this wrapper should be constructed with a valid pointer to ngx_slab_pool_t - unsafe { self.0.as_mut() } - } -} - -impl SlabPool { - /// Creates a new `SlabPool` from an initialized shared zone. - /// - /// # Safety - /// - /// Shared zones are initialized and safe to use: - /// * between the zone init callback and configuration reload in the master process - /// * during the whole lifetime of a worker process. - /// - /// After the configuration reload (notably, in the cycle pool cleanup handlers), zone addresses - /// in the old cycle may become unmapped. - pub unsafe fn from_shm_zone(shm_zone: &ngx_shm_zone_t) -> Option { - let ptr = NonNull::new(shm_zone.shm.addr)?.cast(); - Some(Self(ptr)) - } - - /// Locks the slab pool mutex. - #[inline] - pub fn lock(&self) -> LockedSlabPool { - let shpool = self.0.as_ptr(); - unsafe { ngx_shmtx_lock(&raw mut (*shpool).mutex) }; - LockedSlabPool(self.0) - } -} - -/// Wrapper for a locked [`ngx_slab_pool_t`] pointer. -pub struct LockedSlabPool(NonNull); - -unsafe impl Allocator for LockedSlabPool { - fn allocate(&self, layout: Layout) -> Result, AllocError> { - if layout.size() == 0 { - return Ok(NonNull::slice_from_raw_parts(dangling_for_layout(&layout), layout.size())); - } - - // Small slab allocations (size <= ngx_pagesize / 2) are always aligned to the size rounded - // up to the nearest power of 2. - // If the requested alignment exceeds size, we can guarantee the alignment by allocating - // `align()` bytes. - let size = cmp::max(layout.size(), layout.align()); - - let ptr = unsafe { ngx_slab_alloc_locked(self.0.as_ptr(), size) }; - let ptr = NonNull::new(ptr.cast()).ok_or(AllocError)?; - - if ptr.align_offset(layout.align()) != 0 { - unsafe { self.deallocate(ptr, layout) }; - return Err(AllocError); - } - - Ok(NonNull::slice_from_raw_parts(ptr, layout.size())) - } - - unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - if layout.size() != 0 { - unsafe { ngx_slab_free_locked(self.0.as_ptr(), ptr.as_ptr().cast()) } - } - } -} - -impl Drop for LockedSlabPool { - fn drop(&mut self) { - let shpool = unsafe { self.0.as_mut() }; - unsafe { ngx_shmtx_unlock(&raw mut shpool.mutex) } - } -} diff --git a/rust/vendor/ngx-rust/src/core/status.rs b/rust/vendor/ngx-rust/src/core/status.rs deleted file mode 100644 index 4cab884..0000000 --- a/rust/vendor/ngx-rust/src/core/status.rs +++ /dev/null @@ -1,70 +0,0 @@ -use core::ffi::c_char; -use core::fmt; -use core::ptr; - -use crate::ffi::*; - -/// Status -/// -/// Rust native wrapper for NGINX status codes. -#[derive(Ord, PartialOrd, Eq, PartialEq)] -pub struct Status(pub ngx_int_t); - -impl Status { - /// Is this Status equivalent to NGX_OK? - pub fn is_ok(&self) -> bool { - self == &Status::NGX_OK - } -} - -impl fmt::Debug for Status { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - -impl From for ngx_int_t { - fn from(val: Status) -> Self { - val.0 - } -} - -macro_rules! ngx_codes { - ( - $( - $(#[$docs:meta])* - ($konst:ident); - )+ - ) => { - impl Status { - $( - $(#[$docs])* - pub const $konst: Status = Status($konst as ngx_int_t); - )+ - - } - } -} - -ngx_codes! { - /// NGX_OK - Operation succeeded. - (NGX_OK); - /// NGX_ERROR - Operation failed. - (NGX_ERROR); - /// NGX_AGAIN - Operation incomplete; call the function again. - (NGX_AGAIN); - /// NGX_BUSY - Resource is not available. - (NGX_BUSY); - /// NGX_DONE - Operation complete or continued elsewhere. Also used as an alternative success code. - (NGX_DONE); - /// NGX_DECLINED - Operation rejected, for example, because it is disabled in the configuration. - /// This is never an error. - (NGX_DECLINED); - /// NGX_ABORT - Function was aborted. Also used as an alternative error code. - (NGX_ABORT); -} - -/// An error occurred while parsing and validating configuration. -pub const NGX_CONF_ERROR: *mut c_char = ptr::null_mut::().wrapping_offset(-1); -/// Configuration handler succeeded. -pub const NGX_CONF_OK: *mut c_char = ptr::null_mut(); diff --git a/rust/vendor/ngx-rust/src/core/string.rs b/rust/vendor/ngx-rust/src/core/string.rs deleted file mode 100644 index ad245df..0000000 --- a/rust/vendor/ngx-rust/src/core/string.rs +++ /dev/null @@ -1,633 +0,0 @@ -#[cfg(feature = "alloc")] -use alloc::{borrow::Cow, string::String}; -use core::cmp; -use core::fmt; -use core::str::{self, Utf8Error}; - -use crate::ffi::{ngx_str_t, u_char}; - -/// Static string initializer for [`ngx_str_t`]. -/// -/// The resulting byte string is always nul-terminated (just like a C string). -/// -/// [`ngx_str_t`]: https://nginx.org/en/docs/dev/development_guide.html#string_overview -#[macro_export] -macro_rules! ngx_string { - ($s:expr) => {{ $crate::ffi::ngx_str_t { len: $s.len() as _, data: concat!($s, "\0").as_ptr() as *mut u8 } }}; -} - -#[cfg(feature = "alloc")] -pub use self::_alloc::NgxString; - -/// Representation of a borrowed [Nginx string]. -/// -/// [Nginx string]: https://nginx.org/en/docs/dev/development_guide.html#string_overview -#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)] -#[repr(transparent)] -pub struct NgxStr([u_char]); - -impl NgxStr { - /// Create an [`NgxStr`] from an [`ngx_str_t`]. - /// - /// [`ngx_str_t`]: https://nginx.org/en/docs/dev/development_guide.html#string_overview - /// - /// # Safety - /// - /// The caller has provided a valid `ngx_str_t` with a `data` pointer that points - /// to range of bytes of at least `len` bytes, whose content remains valid and doesn't - /// change for the lifetime of the returned `NgxStr`. - pub unsafe fn from_ngx_str<'a>(str: ngx_str_t) -> &'a NgxStr { - unsafe { - let bytes: &[u8] = str.as_bytes(); - &*(bytes as *const [u8] as *const NgxStr) - } - } - - /// Create an [NgxStr] from a borrowed byte slice. - #[inline] - pub fn from_bytes(bytes: &[u8]) -> &Self { - // SAFETY: An `NgxStr` is identical to a `[u8]` slice, given `u_char` is an alias for `u8` - unsafe { &*(bytes as *const [u8] as *const NgxStr) } - } - - /// Create a mutable [NgxStr] from a borrowed byte slice. - #[inline] - pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self { - // SAFETY: An `NgxStr` is identical to a `[u8]` slice, given `u_char` is an alias for `u8` - unsafe { &mut *(bytes as *mut [u8] as *mut NgxStr) } - } - - /// Access the [`NgxStr`] as a byte slice. - pub fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Yields a `&str` slice if the [`NgxStr`] contains valid UTF-8. - pub fn to_str(&self) -> Result<&str, Utf8Error> { - str::from_utf8(self.as_bytes()) - } - - /// Converts an [`NgxStr`] into a [`Cow`], replacing invalid UTF-8 sequences. - /// - /// See [`String::from_utf8_lossy`]. - #[cfg(feature = "alloc")] - pub fn to_string_lossy(&self) -> Cow<'_, str> { - String::from_utf8_lossy(self.as_bytes()) - } - - /// Returns `true` if the [`NgxStr`] is empty, otherwise `false`. - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } -} - -impl AsRef<[u8]> for NgxStr { - #[inline] - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } -} - -impl AsMut<[u8]> for NgxStr { - #[inline] - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl fmt::Debug for NgxStr { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // XXX: Use debug_tuple() and feature(debug_closure_helpers) once it's stabilized - f.write_str("NgxStr(")?; - nginx_sys::detail::debug_bytes(f, &self.0)?; - f.write_str(")") - } -} - -impl Default for &NgxStr { - fn default() -> Self { - NgxStr::from_bytes(&[]) - } -} - -impl fmt::Display for NgxStr { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - nginx_sys::detail::display_bytes(f, &self.0) - } -} - -macro_rules! impl_partial_ord_eq_from { - ($self:ty, $other:ty) => { impl_partial_ord_eq_from!($self, $other;); }; - - ($self:ty, $other:ty; $($args:tt)*) => { - impl<'a, $($args)*> From<$other> for &'a NgxStr { - #[inline] - fn from(other: $other) -> Self { - let other: &[u8] = other.as_ref(); - NgxStr::from_bytes(other) - } - } - - impl_partial_eq!($self, $other; $($args)*); - impl_partial_ord!($self, $other; $($args)*); - }; -} - -macro_rules! impl_partial_eq { - ($self:ty, $other:ty) => { impl_partial_eq!($self, $other;); }; - - ($self:ty, $other:ty; $($args:tt)*) => { - impl<'a, $($args)*> PartialEq<$other> for $self { - #[inline] - fn eq(&self, other: &$other) -> bool { - let other: &[u8] = other.as_ref(); - PartialEq::eq(self.as_bytes(), other) - } - } - - impl<'a, $($args)*> PartialEq<$self> for $other { - #[inline] - fn eq(&self, other: &$self) -> bool { - let this: &[u8] = self.as_ref(); - PartialEq::eq(this, other.as_bytes()) - } - } - }; -} - -macro_rules! impl_partial_ord { - ($self:ty, $other:ty) => { impl_partial_ord!($self, $other;); }; - - ($self:ty, $other:ty; $($args:tt)*) => { - impl<'a, $($args)*> PartialOrd<$other> for $self { - #[inline] - fn partial_cmp(&self, other: &$other) -> Option { - let other: &[u8] = other.as_ref(); - PartialOrd::partial_cmp(self.as_bytes(), other) - } - } - - impl<'a, $($args)*> PartialOrd<$self> for $other { - #[inline] - fn partial_cmp(&self, other: &$self) -> Option { - let this: &[u8] = self.as_ref(); - PartialOrd::partial_cmp(this, other.as_bytes()) - } - } - }; -} - -impl_partial_eq!(NgxStr, [u8]); -impl_partial_eq!(NgxStr, [u8; N]; const N: usize); -impl_partial_eq!(NgxStr, str); -impl_partial_eq!(NgxStr, ngx_str_t); -impl_partial_eq!(&'a NgxStr, ngx_str_t); -impl_partial_ord!(NgxStr, [u8]); -impl_partial_ord!(NgxStr, [u8; N]; const N: usize); -impl_partial_ord!(NgxStr, str); -impl_partial_ord!(NgxStr, ngx_str_t); -impl_partial_ord!(&'a NgxStr, ngx_str_t); -impl_partial_ord_eq_from!(NgxStr, &'a [u8]); -impl_partial_ord_eq_from!(NgxStr, &'a [u8; N]; const N: usize); -impl_partial_ord_eq_from!(NgxStr, &'a str); - -#[cfg(feature = "alloc")] -mod _alloc { - use core::borrow::Borrow; - use core::hash; - use core::ops; - use core::ptr; - - use super::*; - use crate::allocator::{self, Allocator}; - use crate::collections::{TryReserveError, Vec}; - - /// Owned byte string type with Allocator support. - /// - /// Inspired by [bstr] and unstable [feature(bstr)], with two important differences: - /// - Allocator always have to be specified, - /// - any allocating methods are failible and require explicit handling of the result. - /// - /// [bstr]: https://docs.rs/bstr/latest/bstr/ - /// [feature(bstr)]: https://github.com/rust-lang/rust/issues/134915 - #[derive(Clone)] - #[repr(transparent)] - pub struct NgxString(Vec) - where - A: Allocator + Clone; - - impl NgxString - where - A: Allocator + Clone, - { - /// Constructs a new, empty `NgxString`. - /// - /// No allocations will be made until data is added to the string. - pub fn new_in(alloc: A) -> Self { - Self(Vec::new_in(alloc)) - } - - /// Tries to construct a new `NgxString` from a byte slice. - #[inline] - pub fn try_from_bytes_in( - bytes: impl AsRef<[u8]>, - alloc: A, - ) -> Result { - let mut this = Self::new_in(alloc); - this.try_reserve_exact(bytes.as_ref().len())?; - this.0.extend_from_slice(bytes.as_ref()); - Ok(this) - } - - /// Returns a reference to the underlying allocator - #[inline] - pub fn allocator(&self) -> &A { - self.0.allocator() - } - - /// Returns this `NgxString`'s capacity, in bytes. - #[inline] - pub fn capacity(&self) -> usize { - self.0.capacity() - } - - /// Returns `true` if this `NgxString` has a length of zero, and `false` otherwise. - #[inline] - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - /// Return this `NgxString`'s length, in bytes. - #[inline] - pub fn len(&self) -> usize { - self.0.len() - } - - /// Appends bytes if there is sufficient spare capacity. - /// - /// Returns the number of remaining bytes on overflow. - #[inline] - pub fn append_within_capacity(&mut self, other: impl AsRef<[u8]>) -> Result<(), usize> { - let other = other.as_ref(); - if self.0.len() == self.0.capacity() { - return Err(other.len()); - } - - let n = cmp::min(self.0.capacity() - self.0.len(), other.len()); - unsafe { - // SAFETY: - // - self.0 has at least n writable bytes allocated past self.0.len(), - // - other has at least n bytes available for reading. - // - self.0 internal buffer will be initialized until len + n after this operation - // - other is not borrowed from `self` - let p = self.0.as_mut_ptr().add(self.0.len()); - ptr::copy_nonoverlapping(other.as_ptr(), p, n); - self.0.set_len(self.0.len() + n); - } - - match other.len() - n { - 0 => Ok(()), - x => Err(x), - } - } - - /// Tries to append the bytes to the `NgxString`. - #[inline] - pub fn try_append(&mut self, other: impl AsRef<[u8]>) -> Result<(), TryReserveError> { - let other = other.as_ref(); - self.0.try_reserve_exact(other.len())?; - self.0.extend_from_slice(other); - Ok(()) - } - - /// Tries to reserve capacity for at least `additional` more bytes. - #[inline] - pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.0.try_reserve(additional) - } - - /// Tries to reserve the minimum capacity for at least `additional` more bytes. - #[inline] - pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.0.try_reserve_exact(additional) - } - - #[inline] - pub(crate) fn as_bytes(&self) -> &[u8] { - &self.0 - } - - #[inline] - pub(crate) fn as_bytes_mut(&mut self) -> &mut [u8] { - &mut self.0 - } - - #[inline] - pub(crate) fn as_ngx_str(&self) -> &NgxStr { - NgxStr::from_bytes(self.0.as_slice()) - } - - #[inline] - pub(crate) fn as_ngx_str_mut(&mut self) -> &mut NgxStr { - NgxStr::from_bytes_mut(self.0.as_mut_slice()) - } - - /// Creates NgxString directly from a pointer, a capacity, a length and an allocator. - /// - /// # Safety - /// - /// See [Vec::from_raw_parts_in] - #[inline] - pub unsafe fn from_raw_parts( - ptr: *mut u8, - length: usize, - capacity: usize, - alloc: A, - ) -> Self { - unsafe { Self(Vec::from_raw_parts_in(ptr, length, capacity, alloc)) } - } - - /// Splits the NgxString into its raw components. - /// - /// The caller becomes responsible for the memory previously managed by this NgxString. - #[inline] - pub fn into_raw_parts(self) -> (*mut u8, usize, usize, A) { - self.0.into_raw_parts_with_alloc() - } - } - - impl AsRef for NgxString - where - A: Allocator + Clone, - { - fn as_ref(&self) -> &NgxStr { - self.as_ngx_str() - } - } - - impl AsMut for NgxString - where - A: Allocator + Clone, - { - fn as_mut(&mut self) -> &mut NgxStr { - self.as_ngx_str_mut() - } - } - - impl AsRef<[u8]> for NgxString - where - A: Allocator + Clone, - { - #[inline] - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } - } - - impl AsMut<[u8]> for NgxString - where - A: Allocator + Clone, - { - #[inline] - fn as_mut(&mut self) -> &mut [u8] { - self.as_bytes_mut() - } - } - - impl Borrow for NgxString - where - A: Allocator + Clone, - { - fn borrow(&self) -> &NgxStr { - self.as_ngx_str() - } - } - - impl Borrow<[u8]> for NgxString - where - A: Allocator + Clone, - { - fn borrow(&self) -> &[u8] { - self.0.as_slice() - } - } - - impl fmt::Debug for NgxString - where - A: Allocator + Clone, - { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // XXX: Use debug_tuple() and feature(debug_closure_helpers) once it's stabilized - f.write_str("NgxString(")?; - nginx_sys::detail::debug_bytes(f, &self.0)?; - f.write_str(")") - } - } - - impl ops::Deref for NgxString - where - A: Allocator + Clone, - { - type Target = NgxStr; - - fn deref(&self) -> &Self::Target { - self.as_ngx_str() - } - } - - impl ops::DerefMut for NgxString - where - A: Allocator + Clone, - { - fn deref_mut(&mut self) -> &mut Self::Target { - self.as_ngx_str_mut() - } - } - - impl fmt::Display for NgxString - where - A: Allocator + Clone, - { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(self.as_ngx_str(), f) - } - } - - impl hash::Hash for NgxString - where - A: Allocator + Clone, - { - fn hash(&self, state: &mut H) { - self.0.hash(state); - } - } - - // `NgxString`'s with different allocators should be comparable - - impl PartialEq> for NgxString - where - A1: Allocator + Clone, - A2: Allocator + Clone, - { - fn eq(&self, other: &NgxString) -> bool { - PartialEq::eq(self.as_bytes(), other.as_bytes()) - } - } - - impl Eq for NgxString where A: Allocator + Clone {} - - impl PartialOrd> for NgxString - where - A1: Allocator + Clone, - A2: Allocator + Clone, - { - fn partial_cmp(&self, other: &NgxString) -> Option { - Some(Ord::cmp(self.as_bytes(), other.as_bytes())) - } - } - - impl Ord for NgxString - where - A: Allocator + Clone, - { - fn cmp(&self, other: &Self) -> cmp::Ordering { - Ord::cmp(self.as_bytes(), other.as_bytes()) - } - } - - impl allocator::TryCloneIn for NgxString { - type Target = NgxString; - - fn try_clone_in( - &self, - alloc: A, - ) -> Result, allocator::AllocError> { - NgxString::try_from_bytes_in(self.as_bytes(), alloc).map_err(|_| allocator::AllocError) - } - } - - impl fmt::Write for NgxString - where - A: Allocator + Clone, - { - fn write_str(&mut self, s: &str) -> fmt::Result { - self.append_within_capacity(s).map_err(|_| fmt::Error) - } - } - - // Implement byte comparisons directly, leave the rest to Deref. - - impl_partial_eq!(NgxString, &'a [u8]; A: Allocator + Clone); - impl_partial_eq!(NgxString, &'a [u8; N]; A: Allocator + Clone, const N: usize); - impl_partial_eq!(NgxString, &'a NgxStr; A: Allocator + Clone); - impl_partial_eq!(NgxString, ngx_str_t; A: Allocator + Clone); - - impl_partial_ord!(NgxString, &'a [u8]; A: Allocator + Clone); - impl_partial_ord!(NgxString, &'a [u8; N]; A: Allocator + Clone, const N: usize); - impl_partial_ord!(NgxString, &'a NgxStr; A: Allocator + Clone); - impl_partial_ord!(NgxString, ngx_str_t; A: Allocator + Clone); - - impl_partial_eq!(NgxStr, String); - impl_partial_eq!(&'a NgxStr, String); - impl_partial_ord!(NgxStr, String); - impl_partial_ord!(&'a NgxStr, String); - impl_partial_ord_eq_from!(NgxStr, &'a String); -} - -#[cfg(test)] -mod tests { - extern crate alloc; - - use alloc::string::ToString; - - use super::*; - - #[test] - fn test_str_comparisons() { - let string = "test".to_string(); - let ngx_string = ngx_str_t { data: string.as_ptr().cast_mut(), len: string.len() }; - let ns: &NgxStr = string.as_bytes().into(); - - #[cfg(feature = "alloc")] - assert_eq!(string, ns); - assert_eq!(ngx_string, ns); - assert_eq!(string.as_bytes(), ns); - assert_eq!(string.as_str(), ns); - assert_eq!(b"test", ns); - assert_eq!("test", ns); - - #[cfg(feature = "alloc")] - assert_eq!(ns, string); - assert_eq!(ns, ngx_string); - assert_eq!(ns, string.as_bytes()); - assert_eq!(ns, string.as_str()); - assert_eq!(ns, b"test"); - assert_eq!(ns, "test"); - } - - #[test] - #[cfg(feature = "alloc")] - fn test_string_comparisons() { - use crate::allocator::Global; - - let string = "test".to_string(); - let ngx_string = ngx_str_t { data: string.as_ptr().cast_mut(), len: string.len() }; - let borrowed: &NgxStr = string.as_bytes().into(); - let owned = NgxString::try_from_bytes_in(&string, Global).unwrap(); - - assert_eq!(string.as_bytes(), owned); - assert_eq!(ngx_string, owned); - assert_eq!(borrowed, owned); - assert_eq!(b"test", owned); - assert_eq!(owned, string.as_bytes()); - assert_eq!(owned, ngx_string); - assert_eq!(owned, borrowed); - assert_eq!(owned, b"test"); - - // String comparisons via Deref - assert_eq!(string, *owned); - assert_eq!(string.as_str(), *owned); - assert_eq!("test", *owned); - assert_eq!(*owned, string); - assert_eq!(*owned, string.as_str()); - assert_eq!(*owned, "test"); - } - - #[test] - #[cfg(feature = "alloc")] - fn test_string_write() { - use core::fmt::Write; - - use crate::allocator::Global; - - let h = NgxStr::from_bytes(b"Hello"); - let w = NgxStr::from_bytes(b"world"); - - let mut s = NgxString::new_in(Global); - s.try_reserve(16).expect("reserve"); - - // Remember ptr and len of internal buffer - let saved = (s.as_bytes().as_ptr(), s.capacity()); - - write!(s, "{h} {w}!").expect("write"); - - assert_eq!(s, b"Hello world!"); - assert_eq!((s.as_bytes().as_ptr(), s.capacity()), saved); - } - - #[test] - fn test_lifetimes() { - let a: &NgxStr = "Hello World!".into(); - - let s = "Hello World!".to_string(); - let b: &NgxStr = s.as_bytes().into(); - - // The compiler should detect that s is borrowed and fail. - // drop(s); // ☢️ - - assert_eq!(a.0, b.0); - } -} diff --git a/rust/vendor/ngx-rust/src/ffi/mod.rs b/rust/vendor/ngx-rust/src/ffi/mod.rs deleted file mode 100644 index 887a73e..0000000 --- a/rust/vendor/ngx-rust/src/ffi/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub use nginx_sys::*; diff --git a/rust/vendor/ngx-rust/src/http/conf.rs b/rust/vendor/ngx-rust/src/http/conf.rs deleted file mode 100644 index c6e9d10..0000000 --- a/rust/vendor/ngx-rust/src/http/conf.rs +++ /dev/null @@ -1,373 +0,0 @@ -use ::core::ptr::NonNull; - -use crate::ffi::{ - ngx_http_conf_ctx_t, ngx_http_core_srv_conf_t, ngx_http_request_t, - ngx_http_upstream_srv_conf_t, ngx_module_t, -}; -use crate::http::HttpModule; - -/// Utility trait for types containing HTTP module configuration -pub trait HttpModuleConfExt { - /// Get a non-null reference to the main configuration structure for HTTP module - /// - /// # Safety - /// Caller must ensure that type `T` matches the configuration type for the specified module. - #[inline] - unsafe fn http_main_conf_unchecked(&self, _module: &ngx_module_t) -> Option> { - None - } - - /// Get a non-null reference to the server configuration structure for HTTP module - /// - /// # Safety - /// Caller must ensure that type `T` matches the configuration type for the specified module. - #[inline] - unsafe fn http_server_conf_unchecked(&self, _module: &ngx_module_t) -> Option> { - None - } - - /// Get a non-null reference to the location configuration structure for HTTP module - /// - /// Applies to a single `location`, `if` or `limit_except` block - /// - /// # Safety - /// Caller must ensure that type `T` matches the configuration type for the specified module. - #[inline] - unsafe fn http_location_conf_unchecked(&self, _module: &ngx_module_t) -> Option> { - None - } -} - -impl HttpModuleConfExt for crate::ffi::ngx_http_conf_ctx_t { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - NonNull::new(unsafe { *self.main_conf.add(module.ctx_index) }.cast()) - } - - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - NonNull::new(unsafe { *self.srv_conf.add(module.ctx_index) }.cast()) - } - - #[inline] - unsafe fn http_location_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - NonNull::new(unsafe { *self.loc_conf.add(module.ctx_index) }.cast()) - } -} - -impl HttpModuleConfExt for crate::ffi::ngx_cycle_t { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - let http_conf = unsafe { self.conf_ctx.add(nginx_sys::ngx_http_module.index).as_ref()? }; - let conf_ctx = (*http_conf).cast::(); - unsafe { conf_ctx.as_ref()?.http_main_conf_unchecked(module) } - } -} - -impl HttpModuleConfExt for crate::ffi::ngx_conf_t { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - let conf_ctx = self.ctx.cast::(); - unsafe { conf_ctx.as_ref()?.http_main_conf_unchecked(module) } - } - - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - let conf_ctx = self.ctx.cast::(); - unsafe { - let conf_ctx = conf_ctx.as_ref()?; - NonNull::new((*conf_ctx.srv_conf.add(module.ctx_index)).cast()) - } - } - - #[inline] - unsafe fn http_location_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - let conf_ctx = self.ctx.cast::(); - unsafe { conf_ctx.as_ref()?.http_location_conf_unchecked(module) } - } -} - -impl HttpModuleConfExt for crate::ffi::ngx_http_connection_t { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.conf_ctx.as_ref()?.http_main_conf_unchecked(module) } - } - - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.conf_ctx.as_ref()?.http_server_conf_unchecked(module) } - } - - #[inline] - unsafe fn http_location_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.conf_ctx.as_ref()?.http_location_conf_unchecked(module) } - } -} - -impl HttpModuleConfExt for ngx_http_core_srv_conf_t { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.ctx.as_ref()?.http_main_conf_unchecked(module) } - } - - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.ctx.as_ref()?.http_server_conf_unchecked(module) } - } - - #[inline] - unsafe fn http_location_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { self.ctx.as_ref()?.http_location_conf_unchecked(module) } - } -} - -impl HttpModuleConfExt for ngx_http_request_t { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - NonNull::new(unsafe { *self.main_conf.add(module.ctx_index) }.cast()) - } - - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - NonNull::new(unsafe { *self.srv_conf.add(module.ctx_index) }.cast()) - } - - #[inline] - unsafe fn http_location_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - NonNull::new(unsafe { *self.loc_conf.add(module.ctx_index) }.cast()) - } -} - -impl HttpModuleConfExt for ngx_http_upstream_srv_conf_t { - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - let conf = self.srv_conf; - if conf.is_null() { - return None; - } - NonNull::new(unsafe { *conf.add(module.ctx_index) }.cast()) - } -} - -/// Trait to define and access main module configuration -/// -/// # Safety -/// Caller must ensure that type `HttpModuleMainConf::MainConf` matches the configuration type -/// for the specified module. -pub unsafe trait HttpModuleMainConf: HttpModule { - /// Type for main module configuration - type MainConf; - /// Get reference to main module configuration - fn main_conf(o: &impl HttpModuleConfExt) -> Option<&'static Self::MainConf> { - unsafe { Some(o.http_main_conf_unchecked(Self::module())?.as_ref()) } - } - /// Get mutable reference to main module configuration - fn main_conf_mut(o: &impl HttpModuleConfExt) -> Option<&'static mut Self::MainConf> { - unsafe { Some(o.http_main_conf_unchecked(Self::module())?.as_mut()) } - } -} - -/// Trait to define and access server-specific module configuration -/// -/// # Safety -/// Caller must ensure that type `HttpModuleServerConf::ServerConf` matches the configuration type -/// for the specified module. -pub unsafe trait HttpModuleServerConf: HttpModule { - /// Type for server-specific module configuration - type ServerConf; - /// Get reference to server-specific module configuration - fn server_conf(o: &impl HttpModuleConfExt) -> Option<&'static Self::ServerConf> { - unsafe { Some(o.http_server_conf_unchecked(Self::module())?.as_ref()) } - } - /// Get mutable reference to server-specific module configuration - fn server_conf_mut(o: &impl HttpModuleConfExt) -> Option<&'static mut Self::ServerConf> { - unsafe { Some(o.http_server_conf_unchecked(Self::module())?.as_mut()) } - } -} - -/// Trait to define and access location-specific module configuration -/// -/// Applies to a single `location`, `if` or `limit_except` block -/// -/// # Safety -/// Caller must ensure that type `HttpModuleLocationConf::LocationConf` matches the configuration -/// type for the specified module. -pub unsafe trait HttpModuleLocationConf: HttpModule { - /// Type for location-specific module configuration - type LocationConf; - /// Get reference to location-specific module configuration - fn location_conf(o: &impl HttpModuleConfExt) -> Option<&'static Self::LocationConf> { - unsafe { Some(o.http_location_conf_unchecked(Self::module())?.as_ref()) } - } - /// Get mutable reference to location-specific module configuration - fn location_conf_mut(o: &impl HttpModuleConfExt) -> Option<&'static mut Self::LocationConf> { - unsafe { Some(o.http_location_conf_unchecked(Self::module())?.as_mut()) } - } -} - -mod core { - use crate::allocator::AllocError; - use crate::{ - ffi::{ - ngx_http_core_loc_conf_t, ngx_http_core_main_conf_t, ngx_http_core_module, - ngx_http_core_srv_conf_t, - }, - http::{HttpModuleMainConf, HttpRequestHandler}, - ngx_conf_log_error, - }; - - /// Auxiliary structure to access `ngx_http_core_module` configuration. - pub struct NgxHttpCoreModule; - - impl crate::http::HttpModule for NgxHttpCoreModule { - fn module() -> &'static crate::ffi::ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_core_module) } - } - } - unsafe impl crate::http::HttpModuleMainConf for NgxHttpCoreModule { - type MainConf = ngx_http_core_main_conf_t; - } - unsafe impl crate::http::HttpModuleServerConf for NgxHttpCoreModule { - type ServerConf = ngx_http_core_srv_conf_t; - } - unsafe impl crate::http::HttpModuleLocationConf for NgxHttpCoreModule { - type LocationConf = ngx_http_core_loc_conf_t; - } - - /// HTTP phases in which a module can register handlers. - #[repr(usize)] - pub enum HttpPhase { - /// Post-read phase - PostRead = crate::ffi::ngx_http_phases_NGX_HTTP_POST_READ_PHASE as _, - /// Server rewrite phase - ServerRewrite = crate::ffi::ngx_http_phases_NGX_HTTP_SERVER_REWRITE_PHASE as _, - /// Find configuration phase - FindConfig = crate::ffi::ngx_http_phases_NGX_HTTP_FIND_CONFIG_PHASE as _, - /// Rewrite phase - Rewrite = crate::ffi::ngx_http_phases_NGX_HTTP_REWRITE_PHASE as _, - /// Post-rewrite phase - PostRewrite = crate::ffi::ngx_http_phases_NGX_HTTP_POST_REWRITE_PHASE as _, - /// Pre-access phase - Preaccess = crate::ffi::ngx_http_phases_NGX_HTTP_PREACCESS_PHASE as _, - /// Access phase - Access = crate::ffi::ngx_http_phases_NGX_HTTP_ACCESS_PHASE as _, - /// Post-access phase - PostAccess = crate::ffi::ngx_http_phases_NGX_HTTP_POST_ACCESS_PHASE as _, - /// Pre-content phase - PreContent = crate::ffi::ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as _, - /// Content phase - Content = crate::ffi::ngx_http_phases_NGX_HTTP_CONTENT_PHASE as _, - /// Log phase - Log = crate::ffi::ngx_http_phases_NGX_HTTP_LOG_PHASE as _, - } - - /// Register a request handler for a specified phase. - /// This function must be called from the module's `postconfiguration()` function. - pub fn add_phase_handler(cf: &mut nginx_sys::ngx_conf_t) -> Result<(), AllocError> - where - H: HttpRequestHandler, - { - let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf"); - let h: *mut nginx_sys::ngx_http_handler_pt = unsafe { - nginx_sys::ngx_array_push(&raw mut cmcf.phases[H::PHASE as usize].handlers).cast() - }; - if h.is_null() { - ngx_conf_log_error!( - nginx_sys::NGX_LOG_EMERG, - cf, - "failed to register {} handler", - H::name(), - ); - return Err(AllocError); - } - // set an H::PHASE phase handler - unsafe { - *h = Some(crate::http::raw_handler::); - } - Ok(()) - } -} - -pub use core::{HttpPhase, NgxHttpCoreModule, add_phase_handler}; - -#[cfg(ngx_feature = "http_ssl")] -mod ssl { - use crate::ffi::{ngx_http_ssl_module, ngx_http_ssl_srv_conf_t}; - - /// Auxiliary structure to access `ngx_http_ssl_module` configuration. - pub struct NgxHttpSslModule; - - impl crate::http::HttpModule for NgxHttpSslModule { - fn module() -> &'static crate::ffi::ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_ssl_module) } - } - } - unsafe impl crate::http::HttpModuleServerConf for NgxHttpSslModule { - type ServerConf = ngx_http_ssl_srv_conf_t; - } -} -#[cfg(ngx_feature = "http_ssl")] -pub use ssl::NgxHttpSslModule; - -mod upstream { - use crate::ffi::{ - ngx_http_upstream_main_conf_t, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, - }; - - /// Auxiliary structure to access `ngx_http_upstream_module` configuration. - pub struct NgxHttpUpstreamModule; - - impl crate::http::HttpModule for NgxHttpUpstreamModule { - fn module() -> &'static crate::ffi::ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_upstream_module) } - } - } - unsafe impl crate::http::HttpModuleMainConf for NgxHttpUpstreamModule { - type MainConf = ngx_http_upstream_main_conf_t; - } - unsafe impl crate::http::HttpModuleServerConf for NgxHttpUpstreamModule { - type ServerConf = ngx_http_upstream_srv_conf_t; - } -} - -pub use upstream::NgxHttpUpstreamModule; - -#[cfg(all(nginx1_25_1, ngx_feature = "http_v2"))] -mod http_v2 { - use crate::ffi::{ngx_http_v2_module, ngx_http_v2_srv_conf_t}; - - /// Auxiliary structure to access `ngx_http_v2_module` configuration. - pub struct NgxHttpV2Module; - - impl crate::http::HttpModule for NgxHttpV2Module { - fn module() -> &'static crate::ffi::ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_v2_module) } - } - } - unsafe impl crate::http::HttpModuleServerConf for NgxHttpV2Module { - type ServerConf = ngx_http_v2_srv_conf_t; - } -} -// ngx_http_v2_module was not exposed by default until aefd862a -#[cfg(all(nginx1_25_1, ngx_feature = "http_v2"))] -pub use http_v2::NgxHttpV2Module; - -#[cfg(ngx_feature = "http_v3")] -mod http_v3 { - use crate::ffi::{ngx_http_v3_module, ngx_http_v3_srv_conf_t}; - - /// Auxiliary structure to access `ngx_http_v3_module` configuration. - pub struct NgxHttpV3Module; - - impl crate::http::HttpModule for NgxHttpV3Module { - fn module() -> &'static crate::ffi::ngx_module_t { - unsafe { &*::core::ptr::addr_of!(ngx_http_v3_module) } - } - } - unsafe impl crate::http::HttpModuleServerConf for NgxHttpV3Module { - type ServerConf = ngx_http_v3_srv_conf_t; - } -} - -#[cfg(ngx_feature = "http_v3")] -pub use http_v3::NgxHttpV3Module; diff --git a/rust/vendor/ngx-rust/src/http/mod.rs b/rust/vendor/ngx-rust/src/http/mod.rs deleted file mode 100644 index 00c329a..0000000 --- a/rust/vendor/ngx-rust/src/http/mod.rs +++ /dev/null @@ -1,10 +0,0 @@ -mod conf; -mod module; -mod request; -mod status; -mod upstream; - -pub use conf::*; -pub use module::*; -pub use request::*; -pub use status::*; diff --git a/rust/vendor/ngx-rust/src/http/module.rs b/rust/vendor/ngx-rust/src/http/module.rs deleted file mode 100644 index 7a9292c..0000000 --- a/rust/vendor/ngx-rust/src/http/module.rs +++ /dev/null @@ -1,173 +0,0 @@ -use core::error; -use core::ffi::{c_char, c_void}; -use core::fmt; -use core::ptr; - -use crate::core::NGX_CONF_ERROR; -use crate::core::*; -use crate::ffi::*; - -/// MergeConfigError - configuration cannot be merged with levels above. -#[derive(Debug)] -pub enum MergeConfigError { - /// No value provided for configuration argument - NoValue, -} - -impl error::Error for MergeConfigError {} - -impl fmt::Display for MergeConfigError { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self { - MergeConfigError::NoValue => "no value".fmt(fmt), - } - } -} - -/// The `Merge` trait provides a method for merging configuration down through each level. -/// -/// A module configuration should implement this trait for setting its configuration throughout -/// each level. -pub trait Merge { - /// Module merge function. - /// - /// # Returns - /// Result, Ok on success or MergeConfigError on failure. - fn merge(&mut self, prev: &Self) -> Result<(), MergeConfigError>; -} - -impl Merge for () { - fn merge(&mut self, _prev: &Self) -> Result<(), MergeConfigError> { - Ok(()) - } -} - -/// The `HTTPModule` trait provides the NGINX configuration stage interface. -/// -/// These functions allocate structures, initialize them, and merge through the configuration -/// layers. -/// -/// See for details. -pub trait HttpModule { - /// Returns reference to a global variable of type [ngx_module_t] created for this module. - fn module() -> &'static ngx_module_t; - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn preconfiguration(_cf: *mut ngx_conf_t) -> ngx_int_t { - Status::NGX_OK.into() - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn postconfiguration(_cf: *mut ngx_conf_t) -> ngx_int_t { - Status::NGX_OK.into() - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn create_main_conf(cf: *mut ngx_conf_t) -> *mut c_void - where - Self: super::HttpModuleMainConf, - Self::MainConf: Default, - { - unsafe { - let pool = Pool::from_ngx_pool((*cf).pool); - pool.allocate::(Default::default()) as *mut c_void - } - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn init_main_conf(_cf: *mut ngx_conf_t, _conf: *mut c_void) -> *mut c_char - where - Self: super::HttpModuleMainConf, - Self::MainConf: Default, - { - ptr::null_mut() - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn create_srv_conf(cf: *mut ngx_conf_t) -> *mut c_void - where - Self: super::HttpModuleServerConf, - Self::ServerConf: Default, - { - unsafe { - let pool = Pool::from_ngx_pool((*cf).pool); - pool.allocate::(Default::default()) as *mut c_void - } - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn merge_srv_conf( - _cf: *mut ngx_conf_t, - prev: *mut c_void, - conf: *mut c_void, - ) -> *mut c_char - where - Self: super::HttpModuleServerConf, - Self::ServerConf: Merge, - { - unsafe { - let prev = &mut *(prev as *mut Self::ServerConf); - let conf = &mut *(conf as *mut Self::ServerConf); - match conf.merge(prev) { - Ok(_) => ptr::null_mut(), - Err(_) => NGX_CONF_ERROR as _, - } - } - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn create_loc_conf(cf: *mut ngx_conf_t) -> *mut c_void - where - Self: super::HttpModuleLocationConf, - Self::LocationConf: Default, - { - unsafe { - let pool = Pool::from_ngx_pool((*cf).pool); - pool.allocate::(Default::default()) as *mut c_void - } - } - - /// # Safety - /// - /// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must - /// guard against null inputs or risk runtime errors. - unsafe extern "C" fn merge_loc_conf( - _cf: *mut ngx_conf_t, - prev: *mut c_void, - conf: *mut c_void, - ) -> *mut c_char - where - Self: super::HttpModuleLocationConf, - Self::LocationConf: Merge, - { - unsafe { - let prev = &mut *(prev as *mut Self::LocationConf); - let conf = &mut *(conf as *mut Self::LocationConf); - match conf.merge(prev) { - Ok(_) => ptr::null_mut(), - Err(_) => NGX_CONF_ERROR as _, - } - } - } -} diff --git a/rust/vendor/ngx-rust/src/http/request.rs b/rust/vendor/ngx-rust/src/http/request.rs deleted file mode 100644 index 64d7e6e..0000000 --- a/rust/vendor/ngx-rust/src/http/request.rs +++ /dev/null @@ -1,802 +0,0 @@ -use core::error; -use core::ffi::c_void; -use core::fmt; -use core::ptr::NonNull; -use core::slice; -use core::str::FromStr; - -use crate::core::*; -use crate::ffi::*; -use crate::http::HttpPhase; -use crate::http::status::*; - -/// Define a static request handler. -/// -/// Handlers are expected to take a single [`Request`] argument and return a [`Status`]. -#[macro_export] -macro_rules! http_request_handler { - ( $name: ident, $handler: expr ) => { - extern "C" fn $name(r: *mut $crate::ffi::ngx_http_request_t) -> $crate::ffi::ngx_int_t { - let request = unsafe { $crate::http::Request::from_ngx_http_request(r) }; - let status: $crate::core::Status = $handler(request); - status.0 - } - }; -} - -/// Define a static post subrequest handler. -/// -/// Handlers are expected to take a single [`Request`] argument and return a [`Status`]. -#[macro_export] -macro_rules! http_subrequest_handler { - ( $name: ident, $handler: expr ) => { - unsafe extern "C" fn $name( - r: *mut $crate::ffi::ngx_http_request_t, - data: *mut ::core::ffi::c_void, - rc: $crate::ffi::ngx_int_t, - ) -> $crate::ffi::ngx_int_t { - $handler(r, data, rc) - } - }; -} - -/// Define a static variable setter. -/// -/// The set handler allows setting the property referenced by the variable. -/// The set handler expects a [`Request`], [`mut ngx_variable_value_t`], and a [`usize`]. -/// Variables: -#[macro_export] -macro_rules! http_variable_set { - ( $name: ident, $handler: expr ) => { - unsafe extern "C" fn $name( - r: *mut $crate::ffi::ngx_http_request_t, - v: *mut $crate::ffi::ngx_variable_value_t, - data: usize, - ) { - let request = unsafe { $crate::http::Request::from_ngx_http_request(r) }; - $handler(request, v, data); - } - }; -} - -/// Define a static variable evaluator. -/// -/// The get handler is responsible for evaluating a variable in the context of a specific request. -/// Variable evaluators accept a [`Request`] input argument and two output -/// arguments: [`ngx_variable_value_t`] and [`usize`]. -/// Variables: -#[macro_export] -macro_rules! http_variable_get { - ( $name: ident, $handler: expr ) => { - unsafe extern "C" fn $name( - r: *mut $crate::ffi::ngx_http_request_t, - v: *mut $crate::ffi::ngx_variable_value_t, - data: usize, - ) -> $crate::ffi::ngx_int_t { - let request = unsafe { $crate::http::Request::from_ngx_http_request(r) }; - let status: $crate::core::Status = $handler(request, v, data); - status.0 - } - }; -} - -/// Trait for converting handler return types into `ngx_int_t`. -/// Any desired error handling / logging logic can be implemented -/// in the `into_handler_status` method. -/// -/// There are predefined implementations for `ngx_int_t`, [`Status`], [`HTTPStatus`], -/// [`Option`] with value type implementing [`IntoHandlerStatus`]. -pub trait IntoHandlerStatus -where - Self: Sized, -{ - /// Convert the handler return type into an `ngx_int_t`. - fn into_handler_status(self, _r: &Request) -> ngx_int_t; -} - -impl IntoHandlerStatus for Option -where - T: IntoHandlerStatus, -{ - #[inline] - fn into_handler_status(self, r: &Request) -> ngx_int_t { - self.map(|val| val.into_handler_status(r)).unwrap_or(NGX_ERROR as _) - } -} - -impl IntoHandlerStatus for ngx_int_t { - #[inline] - fn into_handler_status(self, _r: &Request) -> ngx_int_t { - self - } -} - -impl IntoHandlerStatus for Status { - #[inline] - fn into_handler_status(self, _r: &Request) -> ngx_int_t { - self.0 - } -} - -impl IntoHandlerStatus for HTTPStatus { - #[inline] - fn into_handler_status(self, _r: &Request) -> ngx_int_t { - self.0 as _ - } -} - -/// Trait for static request handler. -pub trait HttpRequestHandler { - /// The phase in which the handler is invoked. - const PHASE: HttpPhase; - /// The return type of the handler. - type Output: IntoHandlerStatus; - /// The handler function. - fn handler(request: &mut Request) -> Self::Output; - /// Handler name for logging purposes. - /// [`core::any::type_name`] is used by default. - fn name() -> &'static str { - core::any::type_name::() - } -} - -/// The C-compatible handler wrapper function. -/// -/// # Safety -/// -/// The caller has provided a valid non-null pointer to an [`ngx_http_request_t`]. -pub(crate) unsafe extern "C" fn raw_handler(r: *mut ngx_http_request_t) -> ngx_int_t -where - H: HttpRequestHandler, -{ - let r = unsafe { Request::from_ngx_http_request(r) }; - H::handler(r).into_handler_status(r) -} - -/// Wrapper struct for an [`ngx_http_request_t`] pointer, providing methods for working with HTTP -/// requests. -/// -/// See -#[repr(transparent)] -pub struct Request(ngx_http_request_t); - -impl<'a> From<&'a Request> for *const ngx_http_request_t { - fn from(request: &'a Request) -> Self { - &raw const request.0 - } -} - -impl<'a> From<&'a mut Request> for *mut ngx_http_request_t { - fn from(request: &'a mut Request) -> Self { - &raw mut request.0 - } -} - -impl AsRef for Request { - fn as_ref(&self) -> &ngx_http_request_t { - &self.0 - } -} - -impl AsMut for Request { - fn as_mut(&mut self) -> &mut ngx_http_request_t { - &mut self.0 - } -} - -impl Request { - /// Create a [`Request`] from an [`ngx_http_request_t`]. - /// - /// # Safety - /// - /// The caller has provided a valid non-null pointer to a valid `ngx_http_request_t` - /// which shares the same representation as `Request`. - pub unsafe fn from_ngx_http_request<'a>(r: *mut ngx_http_request_t) -> &'a mut Request { - unsafe { &mut *r.cast::() } - } - - /// Is this the main request (as opposed to a subrequest)? - pub fn is_main(&self) -> bool { - let main = self.0.main.cast(); - core::ptr::eq(self, main) - } - - /// Request pool. - pub fn pool(&self) -> Pool { - // SAFETY: This request is allocated from `pool`, thus must be a valid pool. - unsafe { Pool::from_ngx_pool(self.0.pool) } - } - - /// Returns the result as an `Option` if it exists, otherwise `None`. - /// - /// The option wraps an ngx_http_upstream_t instance, it will be none when the underlying NGINX - /// request does not have a pointer to a [`ngx_http_upstream_t`] upstream structure. - /// - /// [`ngx_http_upstream_t`] is best described in - /// - pub fn upstream(&self) -> Option<*mut ngx_http_upstream_t> { - if self.0.upstream.is_null() { - return None; - } - Some(self.0.upstream) - } - - /// Pointer to a [`ngx_connection_t`] client connection object. - /// - /// [`ngx_connection_t`]: https://nginx.org/en/docs/dev/development_guide.html#connection - pub fn connection(&self) -> *mut ngx_connection_t { - self.0.connection - } - - /// Pointer to a [`ngx_log_t`]. - /// - /// [`ngx_log_t`]: https://nginx.org/en/docs/dev/development_guide.html#logging - pub fn log(&self) -> *mut ngx_log_t { - unsafe { (*self.connection()).log } - } - - /// Get Module context pointer - fn get_module_ctx_ptr(&self, module: &ngx_module_t) -> *mut c_void { - unsafe { *self.0.ctx.add(module.ctx_index) } - } - - /// Get Module context - pub fn get_module_ctx(&self, module: &ngx_module_t) -> Option<&T> { - let ctx = self.get_module_ctx_ptr(module).cast::(); - // SAFETY: ctx is either NULL or allocated with ngx_p(c)alloc and - // explicitly initialized by the module - unsafe { ctx.as_ref() } - } - - /// Sets the value as the module's context. - /// - /// See - pub fn set_module_ctx(&self, value: *mut c_void, module: &ngx_module_t) { - unsafe { - *self.0.ctx.add(module.ctx_index) = value; - }; - } - - /// Get the value of a [complex value]. - /// - /// [complex value]: https://nginx.org/en/docs/dev/development_guide.html#http_complex_values - pub fn get_complex_value(&self, cv: &ngx_http_complex_value_t) -> Option<&NgxStr> { - let r = (self as *const Request as *mut Request).cast(); - let val = cv as *const ngx_http_complex_value_t as *mut ngx_http_complex_value_t; - // SAFETY: `ngx_http_complex_value` does not mutate `r` or `val` and guarentees that - // a valid Nginx string is stored in `value` if it successfully returns. - unsafe { - let mut value = ngx_str_t::default(); - if ngx_http_complex_value(r, val, &raw mut value) != NGX_OK as ngx_int_t { - return None; - } - Some(NgxStr::from_ngx_str(value)) - } - } - - /// Discard (read and ignore) the [request body]. - /// - /// [request body]: https://nginx.org/en/docs/dev/development_guide.html#http_request_body - pub fn discard_request_body(&mut self) -> Status { - unsafe { Status(ngx_http_discard_request_body(&raw mut self.0)) } - } - - /// Client HTTP [User-Agent]. - /// - /// [User-Agent]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent - pub fn user_agent(&self) -> Option<&NgxStr> { - if !self.0.headers_in.user_agent.is_null() { - unsafe { Some(NgxStr::from_ngx_str((*self.0.headers_in.user_agent).value)) } - } else { - None - } - } - - /// Set HTTP status of response. - pub fn set_status(&mut self, status: HTTPStatus) { - self.0.headers_out.status = status.into(); - } - - /// Add header to the `headers_in` object. - /// - /// See - pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> { - let table: *mut ngx_table_elt_t = - unsafe { ngx_list_push(&raw mut self.0.headers_in.headers).cast() }; - unsafe { add_to_ngx_table(table, self.0.pool, key, value) } - } - - /// Add header to the `headers_out` object. - /// - /// See - pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> { - let table: *mut ngx_table_elt_t = - unsafe { ngx_list_push(&raw mut self.0.headers_out.headers).cast() }; - unsafe { add_to_ngx_table(table, self.0.pool, key, value) } - } - - /// Set response body [Content-Length]. - /// - /// [Content-Length]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length - pub fn set_content_length_n(&mut self, n: usize) { - self.0.headers_out.content_length_n = n as off_t; - } - - /// Send the output header. - /// - /// Do not call this function until all output headers are set. - pub fn send_header(&mut self) -> Status { - unsafe { Status(ngx_http_send_header(&raw mut self.0)) } - } - - /// Flag indicating that the output does not require a body. - /// - /// For example, this flag is used by `HTTP HEAD` requests. - pub fn header_only(&self) -> bool { - self.0.header_only() != 0 - } - - /// request method - pub fn method(&self) -> Method { - Method::from_ngx(self.0.method) - } - - /// path part of request only - pub fn path(&self) -> &NgxStr { - unsafe { NgxStr::from_ngx_str(self.0.uri) } - } - - /// full uri - containing path and args - pub fn unparsed_uri(&self) -> &NgxStr { - unsafe { NgxStr::from_ngx_str(self.0.unparsed_uri) } - } - - /// Send the [response body]. - /// - /// This function can be called multiple times. - /// Set the `last_buf` flag in the last body buffer. - /// - /// [response body]: https://nginx.org/en/docs/dev/development_guide.html#http_request_body - pub fn output_filter(&mut self, body: &mut ngx_chain_t) -> Status { - unsafe { Status(ngx_http_output_filter(&raw mut self.0, body)) } - } - - /// Perform internal redirect to a location - pub fn internal_redirect(&self, location: &str) -> Status { - assert!(!location.is_empty(), "uri location is empty"); - let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, location) as *mut _ }; - - // FIXME: check status of ngx_http_named_location or ngx_http_internal_redirect - if location.starts_with('@') { - unsafe { - ngx_http_named_location((self as *const Request as *mut Request).cast(), uri_ptr); - } - } else { - unsafe { - ngx_http_internal_redirect( - (self as *const Request as *mut Request).cast(), - uri_ptr, - core::ptr::null_mut(), - ); - } - } - Status::NGX_DONE - } - - /// Send a subrequest - pub fn subrequest( - &self, - uri: &str, - module: &ngx_module_t, - post_callback: unsafe extern "C" fn( - *mut ngx_http_request_t, - *mut c_void, - ngx_int_t, - ) -> ngx_int_t, - ) -> Status { - let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _ }; - // ------------- - // allocate memory and set values for ngx_http_post_subrequest_t - let sub_ptr = self.pool().alloc(core::mem::size_of::()); - - // assert!(sub_ptr.is_null()); - let post_subreq = - sub_ptr as *const ngx_http_post_subrequest_t as *mut ngx_http_post_subrequest_t; - unsafe { - (*post_subreq).handler = Some(post_callback); - // WARN: safety! ensure that ctx is already set - (*post_subreq).data = self.get_module_ctx_ptr(module); - } - // ------------- - - let mut psr: *mut ngx_http_request_t = core::ptr::null_mut(); - let r = unsafe { - ngx_http_subrequest( - (self as *const Request as *mut Request).cast(), - uri_ptr, - core::ptr::null_mut(), - &raw mut psr, - sub_ptr as *mut _, - NGX_HTTP_SUBREQUEST_WAITED as _, - ) - }; - - // previously call of ngx_http_subrequest() would ensure that the pointer is not null - // anymore - let sr = unsafe { &mut *psr }; - - /* - * allocate fake request body to avoid attempts to read it and to make - * sure real body file (if already read) won't be closed by upstream - */ - sr.request_body = - self.pool().alloc(core::mem::size_of::()) as *mut _; - - if sr.request_body.is_null() { - return Status::NGX_ERROR; - } - sr.set_header_only(1 as _); - Status(r) - } - - /// Iterate over headers_in - /// each header item is (&str, &str) (borrowed) - pub fn headers_in_iterator(&self) -> NgxListIterator<'_> { - unsafe { list_iterator(&self.0.headers_in.headers) } - } - - /// Iterate over headers_out - /// each header item is (&str, &str) (borrowed) - pub fn headers_out_iterator(&self) -> NgxListIterator<'_> { - unsafe { list_iterator(&self.0.headers_out.headers) } - } -} - -impl crate::http::HttpModuleConfExt for Request { - #[inline] - unsafe fn http_main_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { - // SAFETY: main_conf[module.ctx_index] is either NULL or allocated with ngx_p(c)alloc - // and explicitly initialized by the module - NonNull::new((*self.0.main_conf.add(module.ctx_index)).cast()) - } - } - - #[inline] - unsafe fn http_server_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { - // SAFETY: srv_conf[module.ctx_index] is either NULL or allocated with ngx_p(c)alloc and - // explicitly initialized by the module - NonNull::new((*self.0.srv_conf.add(module.ctx_index)).cast()) - } - } - - #[inline] - unsafe fn http_location_conf_unchecked(&self, module: &ngx_module_t) -> Option> { - unsafe { - // SAFETY: loc_conf[module.ctx_index] is either NULL or allocated with ngx_p(c)alloc and - // explicitly initialized by the module - NonNull::new((*self.0.loc_conf.add(module.ctx_index)).cast()) - } - } -} - -// trait OnSubRequestDone { - -// } - -impl fmt::Debug for Request { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Request").field("request_", &self.0).finish() - } -} - -/// Iterator for [`ngx_list_t`] types. -/// -/// Implementes the core::iter::Iterator trait. -pub struct NgxListIterator<'a> { - part: Option>, - i: ngx_uint_t, -} -struct ListPart<'a> { - raw: &'a ngx_list_part_t, - arr: &'a [ngx_table_elt_t], -} -impl<'a> From<&'a ngx_list_part_t> for ListPart<'a> { - fn from(raw: &'a ngx_list_part_t) -> Self { - let arr = if raw.nelts != 0 { - unsafe { slice::from_raw_parts(raw.elts.cast(), raw.nelts) } - } else { - &[] - }; - Self { raw, arr } - } -} - -/// Creates new HTTP header iterator -/// -/// # Safety -/// -/// The caller has provided a valid [`ngx_str_t`] which can be dereferenced validly. -pub unsafe fn list_iterator(list: &ngx_list_t) -> NgxListIterator<'_> { - NgxListIterator { part: Some((&list.part).into()), i: 0 } -} - -// iterator for ngx_list_t -impl<'a> Iterator for NgxListIterator<'a> { - // TODO: try to use struct instead of &str pair - // something like pub struct Header(ngx_table_elt_t); - // then header would have key and value - - type Item = (&'a NgxStr, &'a NgxStr); - - fn next(&mut self) -> Option { - let part = self.part.as_mut()?; - if self.i >= part.arr.len() { - if let Some(next_part_raw) = unsafe { part.raw.next.as_ref() } { - // loop back - *part = next_part_raw.into(); - self.i = 0; - } else { - self.part = None; - return None; - } - } - let header = &part.arr[self.i]; - self.i += 1; - unsafe { Some((NgxStr::from_ngx_str(header.key), NgxStr::from_ngx_str(header.value))) } - } -} - -/// A possible error value when converting `Method` -pub struct InvalidMethod { - _priv: (), -} - -/// Request method verb -#[derive(Clone, PartialEq, Eq, Hash)] -pub struct Method(MethodInner); - -impl Method { - /// UNKNOWN - pub const UNKNOWN: Method = Method(MethodInner::Unknown); - - /// GET - pub const GET: Method = Method(MethodInner::Get); - - /// HEAD - pub const HEAD: Method = Method(MethodInner::Head); - - /// POST - pub const POST: Method = Method(MethodInner::Post); - - /// PUT - pub const PUT: Method = Method(MethodInner::Put); - - /// DELETE - pub const DELETE: Method = Method(MethodInner::Delete); - - /// MKCOL - pub const MKCOL: Method = Method(MethodInner::Mkcol); - - /// COPY - pub const COPY: Method = Method(MethodInner::Copy); - - /// MOVE - pub const MOVE: Method = Method(MethodInner::Move); - - /// OPTIONS - pub const OPTIONS: Method = Method(MethodInner::Options); - - /// PROPFIND - pub const PROPFIND: Method = Method(MethodInner::Propfind); - - /// PROPPATCH - pub const PROPPATCH: Method = Method(MethodInner::Proppatch); - - /// LOCK - pub const LOCK: Method = Method(MethodInner::Lock); - - /// UNLOCK - pub const UNLOCK: Method = Method(MethodInner::Unlock); - - /// PATCH - pub const PATCH: Method = Method(MethodInner::Patch); - - /// TRACE - pub const TRACE: Method = Method(MethodInner::Trace); - - /// CONNECT - pub const CONNECT: Method = Method(MethodInner::Connect); - - /// Convert a Method to a &str. - #[inline] - pub fn as_str(&self) -> &str { - match self.0 { - MethodInner::Unknown => "UNKNOWN", - MethodInner::Get => "GET", - MethodInner::Head => "HEAD", - MethodInner::Post => "POST", - MethodInner::Put => "PUT", - MethodInner::Delete => "DELETE", - MethodInner::Mkcol => "MKCOL", - MethodInner::Copy => "COPY", - MethodInner::Move => "MOVE", - MethodInner::Options => "OPTIONS", - MethodInner::Propfind => "PROPFIND", - MethodInner::Proppatch => "PROPPATCH", - MethodInner::Lock => "LOCK", - MethodInner::Unlock => "UNLOCK", - MethodInner::Patch => "PATCH", - MethodInner::Trace => "TRACE", - MethodInner::Connect => "CONNECT", - } - } - - fn from_bytes(_t: &[u8]) -> Result { - todo!() - } - - fn from_ngx(t: ngx_uint_t) -> Method { - let t = t as _; - match t { - crate::ffi::NGX_HTTP_GET => Method(MethodInner::Get), - crate::ffi::NGX_HTTP_HEAD => Method(MethodInner::Head), - crate::ffi::NGX_HTTP_POST => Method(MethodInner::Post), - crate::ffi::NGX_HTTP_PUT => Method(MethodInner::Put), - crate::ffi::NGX_HTTP_DELETE => Method(MethodInner::Delete), - crate::ffi::NGX_HTTP_MKCOL => Method(MethodInner::Mkcol), - crate::ffi::NGX_HTTP_COPY => Method(MethodInner::Copy), - crate::ffi::NGX_HTTP_MOVE => Method(MethodInner::Move), - crate::ffi::NGX_HTTP_OPTIONS => Method(MethodInner::Options), - crate::ffi::NGX_HTTP_PROPFIND => Method(MethodInner::Propfind), - crate::ffi::NGX_HTTP_PROPPATCH => Method(MethodInner::Proppatch), - crate::ffi::NGX_HTTP_LOCK => Method(MethodInner::Lock), - crate::ffi::NGX_HTTP_UNLOCK => Method(MethodInner::Unlock), - crate::ffi::NGX_HTTP_PATCH => Method(MethodInner::Patch), - crate::ffi::NGX_HTTP_TRACE => Method(MethodInner::Trace), - #[cfg(nginx1_21_1)] - crate::ffi::NGX_HTTP_CONNECT => Method(MethodInner::Connect), - _ => Method(MethodInner::Unknown), - } - } -} - -impl AsRef for Method { - #[inline] - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl<'a> PartialEq<&'a Method> for Method { - #[inline] - fn eq(&self, other: &&'a Method) -> bool { - self == *other - } -} - -impl PartialEq for &Method { - #[inline] - fn eq(&self, other: &Method) -> bool { - *self == other - } -} - -impl PartialEq for Method { - #[inline] - fn eq(&self, other: &str) -> bool { - self.as_ref() == other - } -} - -impl PartialEq for str { - #[inline] - fn eq(&self, other: &Method) -> bool { - self == other.as_ref() - } -} - -impl<'a> PartialEq<&'a str> for Method { - #[inline] - fn eq(&self, other: &&'a str) -> bool { - self.as_ref() == *other - } -} - -impl PartialEq for &str { - #[inline] - fn eq(&self, other: &Method) -> bool { - *self == other.as_ref() - } -} - -impl fmt::Debug for Method { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.as_ref()) - } -} - -impl fmt::Display for Method { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.write_str(self.as_ref()) - } -} - -impl<'a> From<&'a Method> for Method { - #[inline] - fn from(t: &'a Method) -> Self { - t.clone() - } -} - -impl<'a> TryFrom<&'a [u8]> for Method { - type Error = InvalidMethod; - - #[inline] - fn try_from(t: &'a [u8]) -> Result { - Method::from_bytes(t) - } -} - -impl<'a> TryFrom<&'a str> for Method { - type Error = InvalidMethod; - - #[inline] - fn try_from(t: &'a str) -> Result { - TryFrom::try_from(t.as_bytes()) - } -} - -impl FromStr for Method { - type Err = InvalidMethod; - - #[inline] - fn from_str(t: &str) -> Result { - TryFrom::try_from(t) - } -} - -impl InvalidMethod { - #[allow(dead_code)] - fn new() -> InvalidMethod { - InvalidMethod { _priv: () } - } -} - -impl fmt::Debug for InvalidMethod { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("InvalidMethod") - // skip _priv noise - .finish() - } -} - -impl fmt::Display for InvalidMethod { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("invalid HTTP method") - } -} - -impl error::Error for InvalidMethod {} - -#[derive(Clone, PartialEq, Eq, Hash)] -enum MethodInner { - Unknown, - Get, - Head, - Post, - Put, - Delete, - Mkcol, - Copy, - Move, - Options, - Propfind, - Proppatch, - Lock, - Unlock, - Patch, - Trace, - Connect, -} diff --git a/rust/vendor/ngx-rust/src/http/status.rs b/rust/vendor/ngx-rust/src/http/status.rs deleted file mode 100644 index a545f65..0000000 --- a/rust/vendor/ngx-rust/src/http/status.rs +++ /dev/null @@ -1,208 +0,0 @@ -use core::{error, fmt}; - -use crate::core::Status; -use crate::ffi::*; - -/// Represents an HTTP status code. -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct HTTPStatus(pub ngx_uint_t); - -/// A possible error value when converting a `HTTPStatus` from a `u16` or `&str` -/// -/// This error indicates that the supplied input was not a valid number, was less -/// than 100, or was greater than 599. -#[derive(Debug)] -pub struct InvalidHTTPStatusCode { - _priv: (), -} - -impl InvalidHTTPStatusCode { - fn new() -> InvalidHTTPStatusCode { - InvalidHTTPStatusCode { _priv: () } - } -} - -impl fmt::Display for InvalidHTTPStatusCode { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str("invalid status code") - } -} - -impl error::Error for InvalidHTTPStatusCode {} - -impl From for Status { - fn from(val: HTTPStatus) -> Self { - Status(val.0 as ngx_int_t) - } -} - -impl From for ngx_int_t { - fn from(val: HTTPStatus) -> Self { - val.0 as _ - } -} - -impl From for ngx_uint_t { - fn from(val: HTTPStatus) -> Self { - val.0 - } -} - -impl fmt::Debug for HTTPStatus { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - -impl HTTPStatus { - /// Convets a u16 to a status code. - #[inline] - pub fn from_u16(src: u16) -> Result { - if !(100..600).contains(&src) { - return Err(InvalidHTTPStatusCode::new()); - } - - Ok(HTTPStatus(src.into())) - } - - /// Converts a &[u8] to a status code. - pub fn from_bytes(src: &[u8]) -> Result { - if src.len() != 3 { - return Err(InvalidHTTPStatusCode::new()); - } - - let a = src[0].wrapping_sub(b'0') as u16; - let b = src[1].wrapping_sub(b'0') as u16; - let c = src[2].wrapping_sub(b'0') as u16; - - if a == 0 || a > 5 || b > 9 || c > 9 { - return Err(InvalidHTTPStatusCode::new()); - } - - let status = (a * 100) + (b * 10) + c; - Ok(HTTPStatus(status.into())) - } -} - -macro_rules! http_status_codes { - ( - $( - $(#[$docs:meta])* - ($num:expr, $konst:ident, $phrase:expr); - )+ - ) => { - impl HTTPStatus { - $( - $(#[$docs])* - pub const $konst: HTTPStatus = HTTPStatus($num); - )+ - - } - } -} - -http_status_codes! { - /// 100 CONTINUE - (100, CONTINUE, "Continue"); - /// 101 SWITCHING_PROTOCOLS - (101, SWITCHING_PROTOCOLS, "Switching Protocols"); - /// 102 PROCESSING - (102, PROCESSING, "Processing"); - /// 200 OK - (200, OK, "OK"); - /// 201 Created - (201, CREATED, "Created"); - /// 202 Accepted - (202, ACCEPTED, "Accepted"); - /// 204 No Content - (204, NO_CONTENT, "No Content"); - /// 206 Partial Content - (206, PARTIAL_CONTENT, "Partial Content"); - - /// 300 SPECIAL_RESPONSE - (300, SPECIAL_RESPONSE, "SPECIAL_RESPONSE"); - /// 301 Moved Permanently - (301, MOVED_PERMANENTLY, "Moved Permanently"); - /// 302 Moved Temporarily - (302, MOVED_TEMPORARILY, "Moved Temporarily"); - /// 303 See Other - (303, SEE_OTHER, "See Other"); - /// 304 Not Modified - (304, NOT_MODIFIED, "Not Modified"); - /// 307 Temporary Redirect - (307, TEMPORARY_REDIRECT, "Temporary Redirect"); - /// 308 Permanent Redirect - (308, PERMANENT_REDIRECT, "Permanent Redirect"); - - /// 400 Bad Request - (400, BAD_REQUEST, "Bad Request"); - /// 401 Unauthorized - (401, UNAUTHORIZED, "Unauthorized"); - /// 403 Forbidden - (403, FORBIDDEN, "Forbidden"); - /// 404 Not Found - (404, NOT_FOUND, "Not Found"); - /// 405 Method Not Allowed - (405, NOT_ALLOWED, "Method Not Allowed"); - /// 408 Request Time Out - (408, REQUEST_TIME_OUT, "Request Time Out"); - /// 409 Conflict - (409, CONFLICT, "Conflict"); - /// 411 Length Required - (411, LENGTH_REQUIRED, "Length Required"); - /// 412 Precondition Failed - (412, PRECONDITION_FAILED, "Precondition Failed"); - /// 413 Payload Too Large - (413, REQUEST_ENTITY_TOO_LARGE, "Payload Too Large"); - /// 414 Request Uri Too Large - (414, REQUEST_URI_TOO_LARGE, "Request Uri Too Large"); - /// 415 Unsupported Media Type - (415, UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type"); - /// 416 Range Not Satisfiable - (416, RANGE_NOT_SATISFIABLE, "Range Not Satisfiable"); - /// 421 Misdirected Request - (421, MISDIRECTED_REQUEST, "Misdirected Request"); - /// 429 Too Many Requests - (429, TOO_MANY_REQUESTS, "Too Many Requests"); - - // /* Our own HTTP codes */ - // /* The special code to close connection without any response */ - /// 444 CLOSE - (444, CLOSE, "CLOSE"); - - /// 494 NGINX_CODES - (494, NGINX_CODES, "NGINX_CODES"); - - /// 494 REQUEST_HEADER_TOO_LARGE - (494, REQUEST_HEADER_TOO_LARGE, "REQUEST_HEADER_TOO_LARGE"); - - /// 495 NGX_HTTPS_CERT_ERROR - (495, HTTPS_CERT_ERROR, "NGX_HTTPS_CERT_ERROR"); - /// 496 NGX_HTTPS_NO_CERT - (496, HTTPS_NO_CERT, "NGX_HTTPS_NO_CERT"); - - // /* - // * We use the special code for the plain HTTP requests that are sent to - // * HTTPS port to distinguish it from 4XX in an error page redirection - // */ - /// 497 TO_HTTPS - (497, TO_HTTPS, "TO_HTTPS"); - - /// 499 CLIENT_CLOSED_REQUEST - (499, CLIENT_CLOSED_REQUEST, "CLIENT_CLOSED_REQUEST"); - - /// 500 INTERNAL_SERVER_ERROR - (500, INTERNAL_SERVER_ERROR, "INTERNAL_SERVER_ERROR"); - /// 501 NOT_IMPLEMENTED - (501, NOT_IMPLEMENTED, "NOT_IMPLEMENTED"); - /// 502 BAD_GATEWAY - (502, BAD_GATEWAY, "BAD_GATEWAY"); - /// 503 SERVICE_UNAVAILABLE - (503, SERVICE_UNAVAILABLE, "SERVICE_UNAVAILABLE"); - /// 504 GATEWAY_TIME_OUT - (504, GATEWAY_TIME_OUT, "GATEWAY_TIME_OUT"); - /// 505 VERSION_NOT_SUPPORTED - (505, VERSION_NOT_SUPPORTED, "VERSION_NOT_SUPPORTED"); - /// 507 INSUFFICIENT_STORAGE - (507, INSUFFICIENT_STORAGE, "INSUFFICIENT_STORAGE"); -} diff --git a/rust/vendor/ngx-rust/src/http/upstream.rs b/rust/vendor/ngx-rust/src/http/upstream.rs deleted file mode 100644 index cfd3986..0000000 --- a/rust/vendor/ngx-rust/src/http/upstream.rs +++ /dev/null @@ -1,24 +0,0 @@ -/// Define a static upstream peer initializer -/// -/// Initializes the upstream 'get', 'free', and 'session' callbacks and gives the module writer an -/// opportunity to set custom data. -/// -/// This macro will define the NGINX callback type: -/// `typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r, -/// ngx_http_upstream_srv_conf_t *us)`, we keep this macro name in-sync with its underlying NGINX -/// type, this callback is required to initialize your peer. -/// -/// Load Balancing: -#[macro_export] -macro_rules! http_upstream_init_peer_pt { - ( $name: ident, $handler: expr ) => { - extern "C" fn $name( - r: *mut $crate::ffi::ngx_http_request_t, - us: *mut $crate::ffi::ngx_http_upstream_srv_conf_t, - ) -> $crate::ffi::ngx_int_t { - let request = unsafe { $crate::http::Request::from_ngx_http_request(r) }; - let status: $crate::core::Status = $handler(request, us); - status.0 - } - }; -} diff --git a/rust/vendor/ngx-rust/src/lib.rs b/rust/vendor/ngx-rust/src/lib.rs deleted file mode 100644 index b0329b4..0000000 --- a/rust/vendor/ngx-rust/src/lib.rs +++ /dev/null @@ -1,189 +0,0 @@ -//! # Rust bindings for NGINX -//! -//! The `ngx` crate provides a high-level Rust interface for the [NGINX] C APIs, -//! allowing creation of NGINX dynamic modules completely in Rust. -//! -//! [NGINX]: https://nginx.org/ -//! -//! ## Project status -//! -//! This project is in active development. It is stable enough to be used in our -//! own products, but the APIs are not stabilized and breaking changes are expected. -//! -//! ## Build -//! -//! NGINX modules can be built against a particular version of NGINX. The following -//! environment variables can be used to specify the NGINX build to use: -//! -//! - `NGINX_BUILD_DIR` - absolute path to the NGINX build directory. -//! Usually it's `objs` under the source directory, but can be changed with the -//! `--builddir=` argument of the configure script. -//! -//! - `NGINX_SOURCE_DIR` - absolute path to the NGINX source directory, configured -//! with the [`configure`](https://nginx.org/en/docs/configure.html) command. -//! -//! Only the `./configure` step of the NGINX build is mandatory because bindings -//! generator don't depend on any of the artifacts generated by `make`. -//! -//! For example, this is how you would compile the [examples](#examples) using a -//! specific version of NGINX: -//! -//! ```sh -//! NGINX_BUILD_DIR=/path/to/nginx-1.30.4/objs cargo build --package=examples --examples -//! ``` -//! -//! ### Building with the NGINX build script -//! -//! You can build your Rust-based module as a part of the NGINX build process by -//! providing a `config` script implementation and passing the `--add-module`/ -//! `--add-dynamic-module` options to the configure script. -//! -//! See the following example integration scripts: [`examples/config`] and -//! [`examples/config.make`]. -//! -//! [`examples/config`]: https://github.com/bavshin-f5/ngx-rust/blob/main/examples/config -//! [`examples/config.make`]: https://github.com/bavshin-f5/ngx-rust/blob/main/examples/config.make -//! -//! ### Building with vendored NGINX sources -//! -//! It is possible to build module with a vendored copy of the NGINX sources -//! provided in the `nginx-src` crate by enabling feature `vendored`: -//! -//! By default, this will use the latest stable release of NGINX and require -//! system-wide installation of build dependencies (OpenSSL, PCRE2, Zlib). -//! -//! The behavior of vendored builds can be customized with environment variables, -//! as documented in the [nginx-src] crate README. -//! -//! **NOTE**: We recommend to build the module binaries against the exact source and -//! configuration of the NGINX build that you are planning to use in production, -//! and that is unlikely to be possible with the vendored source. -//! -//! `configure` arguments alter the APIs and the symbols visible to the Rust code, -//! and some OS distributions are known to ship nginx packages with API-breaking -//! patches applied. -//! -//! [nginx-src]: https://docs.rs/nginx-src/ -//! -//! ## Cargo features -//! -//! - `alloc` - **Enabled** by default. This provides APIs that require allocations -//! via the `alloc` crate. -//! - `async` - Enables a minimal async runtime built on top of the NGINX event loop. -//! - `serde` - Enables serialization support for some of the provided and -//! re-exported types. -//! - `std` - **Enabled** by default. This provides APIs that require the standard -//! library. -//! - `vendored`: Enables the build scripts to build a copy of nginx source and link -//! against it. See the [nginx-src] crate documentation for additional details. -//! -//! ## Dependencies -//! -//! The following dependencies are required to build a Rust NGINX module on Linux -//! or BSD platform: -//! -//! - NGINX build dependencies: C compiler, make, OpenSSL, PCRE2, and Zlib. -//! - Rust toolchain (1.85.0 or later) -//! - [libclang] for rust-bindgen -//! -//! The installation process and the package names are system-dependent. Please, -//! consult the documentation for your distribution. -//! -//! Note that on some systems you will need `-devel` or `-dev` versions of the -//! packages. -//! -//! For example, [Dockerfile] contains the installation commands for Debian Linux. -//! -//! [Dockerfile]: https://github.com/nginx/ngx-rust/blob/main/Dockerfile -//! [libclang]: https://rust-lang.github.io/rust-bindgen/requirements.html -//! -//! ### macOS dependencies -//! -//! In order to use the optional GNU make build process on macOS, you will need -//! to install additional tools. This can be done via [homebrew](https://brew.sh/) -//! with the following command: -//! -//! ```sh -//! brew install make openssl grep -//! ``` -//! -//! Additionally, you may need to set up LLVM and clang. Typically, this is done -//! as follows: -//! -//! ```sh -//! # make sure xcode tools are installed -//! xcode-select --install -//! # instal llvm -//! brew install --with-toolchain llvm -//! ``` -#![warn(missing_docs)] -#![no_std] -#[cfg(feature = "alloc")] -extern crate alloc; - -pub mod allocator; -#[cfg(feature = "async")] -pub mod async_; -pub mod collections; - -/// The core module. -/// -/// This module provides fundamental utilities needed to interface with many NGINX primitives. -/// String conversions, the pool (memory interface) object, and buffer APIs are covered here. These -/// utilities will generally align with the NGINX 'core' files and APIs. -pub mod core; - -/// The ffi module. -/// -/// This module provides scoped FFI bindings for NGINX symbols. -pub mod ffi; - -/// The http module. -/// -/// This modules provides wrappers and utilities to NGINX http APIs, such as requests, -/// configuration access, and statuses. -#[cfg(ngx_feature = "http")] -pub mod http; - -/// The log module. -/// -/// This module provides an interface into the NGINX logger framework. -pub mod log; - -pub mod sync; - -/// Define modules exported by this library. -/// -/// These are normally generated by the Nginx module system, but need to be -/// defined when building modules outside of it. -#[macro_export] -macro_rules! ngx_modules { - ($( $mod:ident ),+) => { - #[unsafe(no_mangle)] - #[allow(non_upper_case_globals)] - pub static mut ngx_modules: [*const $crate::ffi::ngx_module_t; $crate::count!($( $mod, )+) + 1] = [ - $( unsafe { &$mod } as *const $crate::ffi::ngx_module_t, )+ - ::core::ptr::null() - ]; - - #[unsafe(no_mangle)] - #[allow(non_upper_case_globals)] - pub static mut ngx_module_names: [*const ::core::ffi::c_char; $crate::count!($( $mod, )+) + 1] = [ - $( concat!(stringify!($mod), "\0").as_ptr() as *const ::core::ffi::c_char, )+ - ::core::ptr::null() - ]; - - #[unsafe(no_mangle)] - #[allow(non_upper_case_globals)] - pub static mut ngx_module_order: [*const ::core::ffi::c_char; 1] = [ - ::core::ptr::null() - ]; - }; -} - -/// Count number of arguments -#[macro_export] -macro_rules! count { - () => { 0usize }; - ($x:tt, $( $xs:tt ),*) => { 1usize + $crate::count!($( $xs, )*) }; -} diff --git a/rust/vendor/ngx-rust/src/log.rs b/rust/vendor/ngx-rust/src/log.rs deleted file mode 100644 index e461d6b..0000000 --- a/rust/vendor/ngx-rust/src/log.rs +++ /dev/null @@ -1,342 +0,0 @@ -use core::cmp; -use core::fmt::{self, Write}; -use core::mem::MaybeUninit; -use core::ptr::NonNull; - -use crate::ffi::{self, NGX_MAX_ERROR_STR, ngx_err_t, ngx_log_t, ngx_uint_t}; - -/// This constant is set to `true` if NGINX is compiled with debug logging (`--with-debug`). -pub const DEBUG: bool = cfg!(ngx_feature = "debug"); - -/// Size of the static buffer used to format log messages. -/// -/// Approximates the remaining space in `u_char[NGX_MAX_ERROR_STR]` after writing the standard -/// prefix -pub const LOG_BUFFER_SIZE: usize = - NGX_MAX_ERROR_STR as usize - b"1970/01/01 00:00:00 [info] 1#1: ".len(); - -/// Obtains a pointer to the global (cycle) log object. -/// -/// The returned pointer is tied to the current cycle lifetime, and will be invalidated by a -/// configuration reload in the master process or in a single-process mode. If you plan to store it, -/// make sure that your storage is also tied to the cycle lifetime (e.g. module configuration or -/// connection/request data). -/// -/// The function may panic if you call it before the main() in nginx creates an initial cycle. -#[inline(always)] -pub fn ngx_cycle_log() -> NonNull { - NonNull::new(unsafe { (*nginx_sys::ngx_cycle).log }).expect("global logger") -} - -/// Utility function to provide typed checking of the mask's field state. -#[inline(always)] -pub fn check_mask(mask: DebugMask, log_level: usize) -> bool { - let mask_bits: u32 = mask.into(); - if log_level & mask_bits as usize == 0 { - return false; - } - true -} - -/// Format args into a provided buffer -// May produce incomplete UTF-8 sequences. But any writes to `ngx_log_t` already can be truncated, -// so nothing we can do here. -#[inline] -pub fn write_fmt<'a>(buf: &'a mut [MaybeUninit], args: fmt::Arguments<'_>) -> &'a [u8] { - if let Some(str) = args.as_str() { - str.as_bytes() - } else { - let mut buf = LogBuf::from(buf); - // nothing we can or want to do on errors - let _ = buf.write_fmt(args); - buf.filled() - } -} - -/// Writes the provided buffer to the nginx logger at a specified level. -/// -/// # Safety -/// Requires a valid log pointer. -#[inline] -pub unsafe fn log_error(level: ngx_uint_t, log: *mut ngx_log_t, err: ngx_err_t, buf: &[u8]) { - unsafe { - #[cfg(ngx_feature = "have_variadic_macros")] - ffi::ngx_log_error_core(level, log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr()); - #[cfg(not(ngx_feature = "have_variadic_macros"))] - ffi::ngx_log_error(level, log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr()); - } -} - -/// Writes the provided buffer to the nginx logger at the debug level. -/// -/// # Safety -/// Requires a valid log pointer. -#[inline] -pub unsafe fn log_debug(log: *mut ngx_log_t, err: ngx_err_t, buf: &[u8]) { - unsafe { - #[cfg(ngx_feature = "have_variadic_macros")] - ffi::ngx_log_error_core( - ffi::NGX_LOG_DEBUG as _, - log, - err, - c"%*s".as_ptr(), - buf.len(), - buf.as_ptr(), - ); - #[cfg(not(ngx_feature = "have_variadic_macros"))] - ffi::ngx_log_debug_core(log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr()); - } -} - -/// Write to logger at a specified level. -/// -/// See [Logging](https://nginx.org/en/docs/dev/development_guide.html#logging) -/// for available log levels. -#[macro_export] -macro_rules! ngx_log_error { - ( $level:expr, $log:expr, $($arg:tt)+ ) => { - let log = $log; - let level = $level as $crate::ffi::ngx_uint_t; - if level <= unsafe { (*log).log_level } { - let mut buf = - [const { ::core::mem::MaybeUninit::::uninit() }; $crate::log::LOG_BUFFER_SIZE]; - let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+)); - unsafe { $crate::log::log_error(level, log, 0, message) }; - } - } -} - -/// Write to logger with the context of currently processed configuration file. -#[macro_export] -macro_rules! ngx_conf_log_error { - ( $level:expr, $cf:expr, $($arg:tt)+ ) => { - let cf: *mut $crate::ffi::ngx_conf_t = $cf; - let level = $level as $crate::ffi::ngx_uint_t; - if level <= unsafe { (*(*cf).log).log_level } { - let mut buf = - [const { ::core::mem::MaybeUninit::::uninit() }; $crate::log::LOG_BUFFER_SIZE]; - let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+)); - unsafe { - $crate::ffi::ngx_conf_log_error( - level, - cf, - 0, - c"%*s".as_ptr(), - message.len(), - message.as_ptr() - ); - } - } - } -} - -/// Write to logger at debug level. -#[macro_export] -macro_rules! ngx_log_debug { - ( mask: $mask:expr, $log:expr, $($arg:tt)+ ) => { - let log = $log; - if $crate::log::DEBUG && $crate::log::check_mask($mask, unsafe { (*log).log_level }) { - let mut buf = - [const { ::core::mem::MaybeUninit::::uninit() }; $crate::log::LOG_BUFFER_SIZE]; - let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+)); - unsafe { $crate::log::log_debug(log, 0, message) }; - } - }; - ( $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::All, $log, $($arg)+); - } -} - -/// Log to request connection log at level [`NGX_LOG_DEBUG_HTTP`]. -/// -/// [`NGX_LOG_DEBUG_HTTP`]: https://nginx.org/en/docs/dev/development_guide.html#logging -#[macro_export] -macro_rules! ngx_log_debug_http { - ( $request:expr, $($arg:tt)+ ) => { - let log = unsafe { (*$request.connection()).log }; - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Http, log, $($arg)+); - } -} - -/// Log with requested debug mask. -/// -/// **NOTE:** This macro supports [`DebugMask::Http`] (`NGX_LOG_DEBUG_HTTP`), however, if you have -/// access to a Request object via an http handler it can be more convenient and readable to use -/// the [`ngx_log_debug_http`] macro instead. -/// -/// See for details and available -/// masks. -#[macro_export] -macro_rules! ngx_log_debug_mask { - ( DebugMask::Core, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Core, $log, $($arg)+); - }; - ( DebugMask::Alloc, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Alloc, $log, $($arg)+); - }; - ( DebugMask::Mutex, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Mutex, $log, $($arg)+); - }; - ( DebugMask::Event, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Event, $log, $($arg)+); - }; - ( DebugMask::Http, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Http, $log, $($arg)+); - }; - ( DebugMask::Mail, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Mail, $log, $($arg)+); - }; - ( DebugMask::Stream, $log:expr, $($arg:tt)+ ) => { - $crate::ngx_log_debug!(mask: $crate::log::DebugMask::Stream, $log, $($arg)+); - }; -} - -/// Debug masks for use with [`ngx_log_debug_mask`], these represent the only accepted values for -/// the mask. -#[derive(Debug)] -pub enum DebugMask { - /// Aligns to the NGX_LOG_DEBUG_CORE mask. - Core, - /// Aligns to the NGX_LOG_DEBUG_ALLOC mask. - Alloc, - /// Aligns to the NGX_LOG_DEBUG_MUTEX mask. - Mutex, - /// Aligns to the NGX_LOG_DEBUG_EVENT mask. - Event, - /// Aligns to the NGX_LOG_DEBUG_HTTP mask. - Http, - /// Aligns to the NGX_LOG_DEBUG_MAIL mask. - Mail, - /// Aligns to the NGX_LOG_DEBUG_STREAM mask. - Stream, - /// Aligns to the NGX_LOG_DEBUG_ALL mask. - All, -} - -impl TryFrom for DebugMask { - type Error = u32; - - fn try_from(value: u32) -> Result { - match value { - crate::ffi::NGX_LOG_DEBUG_CORE => Ok(DebugMask::Core), - crate::ffi::NGX_LOG_DEBUG_ALLOC => Ok(DebugMask::Alloc), - crate::ffi::NGX_LOG_DEBUG_MUTEX => Ok(DebugMask::Mutex), - crate::ffi::NGX_LOG_DEBUG_EVENT => Ok(DebugMask::Event), - crate::ffi::NGX_LOG_DEBUG_HTTP => Ok(DebugMask::Http), - crate::ffi::NGX_LOG_DEBUG_MAIL => Ok(DebugMask::Mail), - crate::ffi::NGX_LOG_DEBUG_STREAM => Ok(DebugMask::Stream), - crate::ffi::NGX_LOG_DEBUG_ALL => Ok(DebugMask::All), - _ => Err(0), - } - } -} - -impl From for u32 { - fn from(value: DebugMask) -> Self { - match value { - DebugMask::Core => crate::ffi::NGX_LOG_DEBUG_CORE, - DebugMask::Alloc => crate::ffi::NGX_LOG_DEBUG_ALLOC, - DebugMask::Mutex => crate::ffi::NGX_LOG_DEBUG_MUTEX, - DebugMask::Event => crate::ffi::NGX_LOG_DEBUG_EVENT, - DebugMask::Http => crate::ffi::NGX_LOG_DEBUG_HTTP, - DebugMask::Mail => crate::ffi::NGX_LOG_DEBUG_MAIL, - DebugMask::Stream => crate::ffi::NGX_LOG_DEBUG_STREAM, - DebugMask::All => crate::ffi::NGX_LOG_DEBUG_ALL, - } - } -} - -/// Minimal subset of unstable core::io::{BorrowedBuf,BorrowedCursor} -struct LogBuf<'data> { - buf: &'data mut [MaybeUninit], - filled: usize, -} - -impl<'data> LogBuf<'data> { - pub fn filled(&self) -> &'data [u8] { - // SAFETY: valid bytes have been written to self.buf[..self.filled] - unsafe { - let buf = self.buf.get_unchecked(..self.filled); - // inlined MaybeUninit::slice_assume_init_ref - &*(buf as *const [MaybeUninit] as *const [u8]) - } - } - - pub fn append(&mut self, buf: &[u8]) -> &mut Self { - let n = cmp::min(self.buf.len() - self.filled, buf.len()); - unsafe { - // SAFETY: The source buf has at least n bytes - let src = buf.get_unchecked(..n); - // SAFETY: &[u8] and &[MaybeUninit] have the same layout - let src: &[MaybeUninit] = core::mem::transmute(src); - // SAFETY: self.buf has at least n bytes available after self.filled - self.buf.get_unchecked_mut(self.filled..self.filled + n).copy_from_slice(src); - } - self.filled += n; - self - } -} - -impl<'data> From<&'data mut [MaybeUninit]> for LogBuf<'data> { - fn from(buf: &'data mut [MaybeUninit]) -> Self { - Self { buf, filled: 0 } - } -} - -impl fmt::Write for LogBuf<'_> { - fn write_str(&mut self, s: &str) -> fmt::Result { - self.append(s.as_bytes()); - Ok(()) - } -} - -#[cfg(test)] -mod tests { - - use super::*; - - #[test] - fn test_mask_lower_bound() { - assert!(>::into(DebugMask::Core) == crate::ffi::NGX_LOG_DEBUG_FIRST); - } - #[test] - fn test_mask_upper_bound() { - assert!( - >::into(DebugMask::Stream) == crate::ffi::NGX_LOG_DEBUG_LAST - ); - } - #[test] - fn test_check_mask() { - struct MockLog { - log_level: usize, - } - let mock = MockLog { log_level: 16 }; - - let mut r = check_mask(DebugMask::Core, mock.log_level); - assert!(r); - - r = check_mask(DebugMask::Alloc, mock.log_level); - assert!(!r); - } - - #[test] - fn log_buffer() { - use core::str; - - let mut buf = [const { MaybeUninit::::uninit() }; 32]; - let mut buf = LogBuf::from(&mut buf[..]); - let words = ["Hello", "World"]; - - // normal write - write!(&mut buf, "{} {}!", words[0], words[1]).unwrap(); - assert_eq!(str::from_utf8(buf.filled()), Ok("Hello World!")); - - // overflow results in truncated output - write!(&mut buf, " This is a test, {}", u64::MAX).unwrap(); - assert_eq!(str::from_utf8(buf.filled()), Ok("Hello World! This is a test, 184")); - - // and any following writes are still safe - write!(&mut buf, "test").unwrap(); - assert_eq!(str::from_utf8(buf.filled()), Ok("Hello World! This is a test, 184")); - } -} diff --git a/rust/vendor/ngx-rust/src/sync.rs b/rust/vendor/ngx-rust/src/sync.rs deleted file mode 100644 index 258357c..0000000 --- a/rust/vendor/ngx-rust/src/sync.rs +++ /dev/null @@ -1,118 +0,0 @@ -//! Synchronization primitives over shared memory. -//! -//! This module provides an alternative implementation for the `ngx_atomic_t` type, -//! `ngx_atomic_*`/`ngx_rwlock_*` family of functions and related usage patterns from nginx. -//! -//! `` contains a wide variety of implementation variants for different platforms and -//! build configurations. It's not feasible to properly expose all of these to the Rust code, and we -//! are not going to. The implementation here uses similar logic on the foundation of the -//! [core::sync::atomic] types and is intentionally _not interoperable_ with the nginx atomics. -//! Thus, it's only suitable for use for new shared memory structures instead of, for example, -//! interacting with the upstream zones. -//! -//! One potential pitfall here is that atomics in Rust are specified in terms of threads, and we use -//! the types in this module for interprocess synchronization. This should not be an issue though, -//! as Rust refers to the C/C++11 memory model for atomics, and there's a following note in -//! [atomics.lockfree]: -//! -//! > [Note: Operations that are lock-free should also be address-free. That is, atomic operations -//! > on the same memory location via two different addresses will communicate atomically. The -//! > implementation should not depend on any per-process state. This restriction enables -//! > communication via memory that is mapped into a process more than once and by memory that is -//! > shared between two processes. — end note] -//! -//! In practice, this recommendation is applied in all the implementations that matter to us. -use core::sync::atomic::{self, Ordering}; - -use nginx_sys::ngx_sched_yield; - -const NGX_RWLOCK_SPIN: usize = 2048; -const NGX_RWLOCK_WLOCK: usize = usize::MAX; - -type NgxAtomic = atomic::AtomicUsize; - -/// Raw lock type. -pub struct RawSpinlock(NgxAtomic); - -/// Reader-writer lock over an atomic variable, based on the nginx rwlock implementation. -pub type RwLock = lock_api::RwLock; - -/// RAII structure used to release the shared read access of a lock when dropped. -pub type RwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, RawSpinlock, T>; - -/// RAII structure used to release the exclusive write access of a lock when dropped. -pub type RwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, RawSpinlock, T>; - -unsafe impl lock_api::RawRwLock for RawSpinlock { - // Only used for initialization, will not be mutated - #[allow(clippy::declare_interior_mutable_const)] - const INIT: RawSpinlock = RawSpinlock(NgxAtomic::new(0)); - - type GuardMarker = lock_api::GuardNoSend; - - fn lock_shared(&self) { - loop { - if self.try_lock_shared() { - return; - } - - if unsafe { nginx_sys::ngx_ncpu > 1 } { - for n in 0..NGX_RWLOCK_SPIN { - for _ in 0..n { - core::hint::spin_loop() - } - - if self.try_lock_shared() { - return; - } - } - } - - ngx_sched_yield() - } - } - - fn try_lock_shared(&self) -> bool { - let value = self.0.load(Ordering::Acquire); - - if value == NGX_RWLOCK_WLOCK { - return false; - } - - self.0.compare_exchange(value, value + 1, Ordering::Acquire, Ordering::Relaxed).is_ok() - } - - unsafe fn unlock_shared(&self) { - self.0.fetch_sub(1, Ordering::Release); - } - - fn lock_exclusive(&self) { - loop { - if self.try_lock_exclusive() { - return; - } - - if unsafe { nginx_sys::ngx_ncpu > 1 } { - for n in 0..NGX_RWLOCK_SPIN { - for _ in 0..n { - core::hint::spin_loop() - } - - if self.try_lock_exclusive() { - return; - } - } - } - - ngx_sched_yield() - } - } - - fn try_lock_exclusive(&self) -> bool { - self.0.compare_exchange(0, NGX_RWLOCK_WLOCK, Ordering::Acquire, Ordering::Relaxed).is_ok() - } - - unsafe fn unlock_exclusive(&self) { - self.0.store(0, Ordering::Release) - } -} diff --git a/rust/vendor/ngx-rust/tests/conditional-compilation.rs b/rust/vendor/ngx-rust/tests/conditional-compilation.rs deleted file mode 100644 index 156401d..0000000 --- a/rust/vendor/ngx-rust/tests/conditional-compilation.rs +++ /dev/null @@ -1,23 +0,0 @@ -use ngx::ffi; - -#[test] -fn test_os_symbols() { - #[cfg(ngx_os = "freebsd")] - assert_eq!(ffi::NGX_FREEBSD, 1); - - #[cfg(ngx_os = "linux")] - assert_eq!(ffi::NGX_LINUX, 1); - - #[cfg(ngx_os = "darwin")] - assert_eq!(ffi::NGX_DARWIN, 1); -} - -#[test] -fn test_feature_symbols() { - let ev: ffi::ngx_event_t = unsafe { core::mem::zeroed() }; - - assert_eq!(ev.available, 0); - - #[cfg(ngx_feature = "have_kqueue")] - assert_eq!(ev.kq_errno, 0); -} diff --git a/rust/vendor/ngx-rust/tests/config/nginx.conf b/rust/vendor/ngx-rust/tests/config/nginx.conf deleted file mode 100644 index 8889ed7..0000000 --- a/rust/vendor/ngx-rust/tests/config/nginx.conf +++ /dev/null @@ -1,43 +0,0 @@ -#load_module modules/ngx_http_rust_module.so; - -#user nobody; -worker_processes 1; - -error_log logs/error.log; -#error_log logs/error.log notice; -#error_log logs/error.log info; - -#pid logs/nginx.pid; - - -events { - worker_connections 1024; -} - - -http { - include mime.types; - default_type application/octet-stream; - - - # rust test; - - - - server { - listen 8080; - server_name localhost; - - #charset koi8-r; - - #access_log logs/host.access.log main; - - location / { - root html; - index index.html index.htm; - } - - - } - -} diff --git a/rust/vendor/ngx-rust/tests/log_test.rs b/rust/vendor/ngx-rust/tests/log_test.rs deleted file mode 100644 index 0610169..0000000 --- a/rust/vendor/ngx-rust/tests/log_test.rs +++ /dev/null @@ -1,148 +0,0 @@ -use std::env; -use std::fs; -use std::io; -use std::io::Result; -#[cfg(unix)] -use std::os::unix::ffi::OsStrExt; -use std::path::{Path, PathBuf}; -use std::process::Command; -use std::process::Output; - -const NGINX_BINARY_NAME: &str = "nginx"; - -/// Convert a CStr to a PathBuf -pub fn cstr_to_path(val: &core::ffi::CStr) -> Option { - if val.is_empty() { - return None; - } - - #[cfg(unix)] - let str = std::ffi::OsStr::from_bytes(val.to_bytes()); - #[cfg(not(unix))] - let str = std::str::from_utf8(val.to_bytes()).ok()?; - - Some(PathBuf::from(str)) -} - -/// Find nginx binary in the build directory -pub fn find_nginx_binary() -> io::Result { - let path = [ - // TEST_NGINX_BINARY is specified for tests - env::var("TEST_NGINX_BINARY").ok().map(PathBuf::from), - // The module is built against an external NGINX source tree - env::var("NGINX_BUILD_DIR").map(PathBuf::from).map(|x| x.join(NGINX_BINARY_NAME)).ok(), - env::var("NGINX_SOURCE_DIR") - .map(PathBuf::from) - .map(|x| x.join("objs").join(NGINX_BINARY_NAME)) - .ok(), - // Fallback to the build directory exposed by nginx-sys - option_env!("DEP_NGINX_BUILD_DIR").map(PathBuf::from).map(|x| x.join(NGINX_BINARY_NAME)), - ] - .into_iter() - .flatten() - .find(|x| x.is_file()) - .ok_or(io::ErrorKind::NotFound)?; - - Ok(path) -} - -/// harness to test nginx -pub struct Nginx { - pub prefix: tempfile::TempDir, - pub bin_path: PathBuf, - pub config_path: PathBuf, -} - -impl Default for Nginx { - /// create nginx with default - fn default() -> Nginx { - let binary = find_nginx_binary().expect("nginx binary"); - Nginx::new(binary).expect("test harness") - } -} - -impl Nginx { - pub fn new(binary: impl AsRef) -> io::Result { - let prefix = tempfile::tempdir()?; - let config = prefix.path().join("nginx.conf"); - - fs::create_dir(prefix.path().join("logs"))?; - - Ok(Nginx { prefix, bin_path: binary.as_ref().to_owned(), config_path: config }) - } - - /// start nginx process with arguments - pub fn cmd(&self, args: &[&str]) -> Result { - let prefix = self.prefix.path().to_string_lossy(); - let config_path = self.config_path.to_string_lossy(); - let args = [&["-p", &prefix, "-c", &config_path], args].concat(); - let result = Command::new(&self.bin_path).args(args).output(); - - match result { - Err(e) => Err(e), - - Ok(output) => { - println!("status: {}", output.status); - println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); - println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); - Ok(output) - } - } - } - - /// complete stop the nginx binary - pub fn stop(&mut self) -> Result { - self.cmd(&["-s", "stop"]) - } - - /// start the nginx binary - pub fn start(&mut self) -> Result { - self.cmd(&[]) - } - - // make sure we stop existing nginx and start new master process - // intentinally ignore failure in stop - pub fn restart(&mut self) -> Result { - let _ = self.stop(); - self.start() - } - - // replace config with another config - pub fn replace_config>(&mut self, from: P) -> Result { - println!("copying config from: {:?} to: {:?}", from.as_ref(), self.config_path); // replace with logging - fs::copy(from, &self.config_path) - } -} - -#[cfg(test)] -mod tests { - use std::env; - - use super::*; - - const TEST_NGINX_CONFIG: &str = "tests/nginx.conf"; - - #[test] - fn test() { - let mut nginx = Nginx::default(); - - let current_dir = env::current_dir().expect("Unable to get current directory"); - let test_config_path = current_dir.join(TEST_NGINX_CONFIG); - - assert!( - test_config_path.exists(), - "Config file not found: {}\nCurrent directory: {}", - test_config_path.to_string_lossy(), - current_dir.to_string_lossy() - ); - - nginx.replace_config(&test_config_path).unwrap_or_else(|_| { - panic!("Unable to load config file: {}", test_config_path.to_string_lossy()) - }); - let output = nginx.restart().expect("Unable to restart NGINX"); - assert!(output.status.success()); - - let output = nginx.stop().expect("Unable to stop NGINX"); - assert!(output.status.success()); - } -} diff --git a/rust/vendor/ngx-rust/tests/module/config b/rust/vendor/ngx-rust/tests/module/config deleted file mode 100644 index 88b592a..0000000 --- a/rust/vendor/ngx-rust/tests/module/config +++ /dev/null @@ -1,12 +0,0 @@ -ngx_addon_name=ngx_http_rust_module - -ngx_module_type=HTTP_FILTER -ngx_module_name=ngx_http_rust_module -ngx_module_srcs="$ngx_addon_dir/ngx_http_rust_module.c" -ngx_module_deps="cargo" -ngx_module_libs="$ngx_addon_dir/../target/debug/libngx_rust.a" - - -. auto/module - - diff --git a/rust/vendor/ngx-rust/tests/module/config.make b/rust/vendor/ngx-rust/tests/module/config.make deleted file mode 100644 index 23559e8..0000000 --- a/rust/vendor/ngx-rust/tests/module/config.make +++ /dev/null @@ -1,7 +0,0 @@ - -cat << END >> $NGX_MAKEFILE - -cargo: - cargo build --manifest-path $ngx_addon_dir/../Cargo.toml --lib - -END diff --git a/rust/vendor/ngx-rust/tests/module/ngx_http_rust_module.c b/rust/vendor/ngx-rust/tests/module/ngx_http_rust_module.c deleted file mode 100644 index 374a606..0000000 --- a/rust/vendor/ngx-rust/tests/module/ngx_http_rust_module.c +++ /dev/null @@ -1,92 +0,0 @@ -/** - * @file ngx_rust.c - * @author Sehyo Chang - * @date - * - * @brief Dummy module - * - * @section LICENSE - * - * Copyright (C) 2011 by Nginx - * - */ -#include -#include -#include - - -/** - * @brief element mixer configuration - */ -typedef struct { - ngx_str_t name; /**< test name */ -} ngx_http_rust_main_conf_t; - - - -static void *ngx_http_rust_create_main_conf(ngx_conf_t *cf); - - -/* - * dummy rust - */ -static ngx_command_t ngx_http_rust_commands[] = { - { - ngx_string("rust"), /* dummy directive */ - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, // server takes 1 // - ngx_conf_set_str_slot, /* configuration setup function */ - NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_rust_main_conf_t, name), - NULL - }, - ngx_null_command /* command termination */ -}; - - -/* The module context. */ -static ngx_http_module_t ngx_http_rust_module_ctx = { - NULL, /* preconfiguration */ - NULL, /* postconfiguration */ - ngx_http_rust_create_main_conf, /* create main configuration */ - NULL, /* init main configuration */ - - NULL, /* create server configuration */ - NULL, /* merge server configuration */ - - NULL, - NULL -}; - -/* Module definition. */ -ngx_module_t ngx_http_rust_module = { - NGX_MODULE_V1, - &ngx_http_rust_module_ctx, /* module context */ - ngx_http_rust_commands, /* module directives */ - NGX_HTTP_MODULE, /* module type */ - NULL, /* init master */ - NULL, /* init module */ - NULL, /* init process */ - NULL, /* init thread */ - NULL, /* exit thread */ - NULL, /* exit process */ - NULL, /* exit master */ - NGX_MODULE_V1_PADDING -}; - - - -static void *ngx_http_rust_create_main_conf(ngx_conf_t *cf) -{ - ngx_http_rust_main_conf_t *conf; - - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "setting up main config"); - - - conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rust_main_conf_t)); - if (conf == NULL) { - return NULL; - } - - - return conf; -} \ No newline at end of file diff --git a/rust/vendor/ngx-rust/tests/nginx.conf b/rust/vendor/ngx-rust/tests/nginx.conf deleted file mode 100644 index be3e9c9..0000000 --- a/rust/vendor/ngx-rust/tests/nginx.conf +++ /dev/null @@ -1,42 +0,0 @@ -#load_module modules/ngx_http_rust_module.so; - -#user nobody; -worker_processes 1; - -error_log logs/error.log; -#error_log logs/error.log notice; -#error_log logs/error.log info; - -#pid logs/nginx.pid; - - -events { - worker_connections 1024; -} - - -http { - default_type application/octet-stream; - - - # rust test; - - - - server { - listen 30000; - server_name localhost; - - #charset koi8-r; - - #access_log logs/host.access.log main; - - location / { - root html; - index index.html index.htm; - } - - - } - -} diff --git a/tests/cache/.run-auth/cache/cargo/6/c4/56d612c5a01718fa4713b10d04281c46 b/tests/cache/.run-auth/cache/cargo/6/c4/56d612c5a01718fa4713b10d04281c46 deleted file mode 100644 index 6840f4d7009dab1081d2d2c21fb825f779fd8341..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 596 zcmeHET}s115RRfC3%SG4$J!*D4dh`#1Z!xaN?WooKCa7{q_n#+*@(S@2k;u=aXgLP z#)9?^4g=r(eII)n8T z$$WAvEpo^^J#swAv39^xh!aj-rZBk|J{cQrQQP1Sbzw^mEdHH|ZS4ePU9T%8ZRNC$ zvS@?WHYhhf$%HI8?BD7*Qk59&)f%(rP)mcEbi_PIOkJKgs$$I%zJoy%UpduO-*jpLAM{45~T zf@YfDuW4Dd=590O<5>1-pG>4&Q0_Ab7=!uLB?&fbYy>>077t72N3htMTvs&oxkccT zM`chzvZ@A_2MN|T1rhL2FpC=)&!kI6x-wW9?+L5S9RJNNuntX2x1K_RB0izaXE%d5=}EPnHKv8{T4Uk=lBEu zhE88d|G~?F`#c8@_te?O*~RDct~^LTyI6gFZ1(Tp=dX_Qa&&ck(((J9gWLbcamYA+ z6p(4bGRCVakJ!J1kbEapBdv46#0z^z*29^_#L%dKmSj?yqDPkeT#9118m zdO|!X(Nan>h*QDIuVMNieU=;TP`ltBb?q8K=-*6kEeR-VS8b(SOWLM#=z_KDQFT5m sq^boBuOyDD8Vm-t-mC=J%@dI((5bgT)p(O;ocoL@`D`KRDj2T&0kl3-&Hw-a diff --git a/tests/cache/.run-auth/conf/cache-common.conf b/tests/cache/.run-auth/conf/cache-common.conf deleted file mode 100644 index 3852e97..0000000 --- a/tests/cache/.run-auth/conf/cache-common.conf +++ /dev/null @@ -1,131 +0,0 @@ -# ============================================================================ -# cache-common.conf —— 各生态 cache location 共用的缓存/代理指令 -# -# 由 conf/cache-routes.conf 在每个 location 内 `include`。 -# **每个 location 自己指定** `proxy_cache `(各生态分库),本文件只放公共项。 -# -# --------------------------------------------------------------------------- -# 与「静态上游版」相比的三处关键变化(配合 plugin-cache 的变量层) -# --------------------------------------------------------------------------- -# -# 1. `proxy_pass` 由字面量改为变量 `$cache_target`。上游地址与凭据因此来自 -# 共享内存(`cx:`,由管理台写),**改上游不用改 conf、不用 reload**。 -# 代价:变量式 proxy_pass 不推导 Host、不追加 query —— 两者都由模块算好, -# 所以这里必须显式 `proxy_set_header Host $cache_host`。 -# -# 2. `proxy_cache_key` 不再用 `$scheme$proxy_host$request_uri`(变量式下 -# `$proxy_host` 不可靠),改用模块给的 `$cache_key`,并**并入方法 + -# Accept**(下面的注释详述为什么这是正确性问题,不是调优)。 -# -# 3. 出站鉴权头改成三段式(`proxy_pass_request_headers on` + 抹掉三个 -# 网关 Key 携带位 + 用 `$cache_auth` 下发)——见文件下半部分。 -# -# ⚠️ 本文件**不得**使用 rewrite 模块的 `set` / `if`;生态判定由 plugin-cache -# 直读 `r->uri` 首段完成,不需要 `set $cache_ecosystem`。 -# (`return` / `rewrite` 本身可用:本机构建与 ipass 生产都带 --with-pcre, -# rewrite 模块在编。它们只在 cache-routes.conf 里用。) -# ============================================================================ - -# --------------------------------------------------------------------------- -# 缓存键 -# --------------------------------------------------------------------------- -# -# `$cache_key` = `:<完整 URI + query>`,**不含任何下游凭据** —— 缓存因此是 -# 「全站共享」的:同一个 go module / npm 包被 N 个账号拉,只回源一次。 -# 这正是制品缓存的价值所在;鉴权在 access 层(谁能读),数据在共享层复用。 -# 代价:任一有效 Key 都能命中别人拉过的制品 —— 对公开 registry 无碍, -# 若某路由指向**私有**上游,请理解这一点再启用。 -# -# ★ query 必须进键(由模块的 `$cache_key` 负责,本行不要再自己拼 `$args`): -# npm 的 `?write=true`、docker 的 `?ns=`、go 的 `?go-get=1`、cargo 的 -# `?archive=tar.gz` 都靠 query 区分实体。变量式 `proxy_pass` 下 `$uri` -# **不含** query,只按 `$uri` 做键会把 `/x?a` 与 `/x?b` 当成同一个资源, -# 把错的制品发给客户端(且第二次完全不回源,很难察觉)。 -# -# ★ 为什么要并入 `$request_method` 与 `$http_accept`(这是正确性,不是调优): -# -# · `$request_method`:Docker 客户端对同一 URL 先 `HEAD` 看清单存不存在、 -# 再 `GET` 取清单(registry 协议如此)。若两者共用一个键,一次 HEAD 的 -# **空体**响应会被缓存下来并回给随后的 GET,客户端拿到 0 字节清单直接失败。 -# -# · `$http_accept`:nginx 的 proxy_cache **不实现 Vary**(只能靠 -# proxy_ignore_headers 把 Vary 忽略掉,无法参与分档)。而上游同一 URL -# 会因为 Accept 返回**不同实体**: -# - npm:`application/vnd.npm.install-v1+json` → 精简元数据, -# 缺省 → 完整元数据; -# - PyPI simple:`application/vnd.pypi.simple.v1+json` → JSON, -# `text/html` → HTML; -# - Docker:清单 v2 / OCI / v1 的 mediaType 由 Accept 偏好决定。 -# 不并入 Accept 就会出现「发给 A 的实体被回给 B」的串档。代价是每个 -# Accept 变体一个桶(现代客户端取值收敛,实际只多一个桶)。 -# -# 注:刻意**不**再带 `$scheme`/`$proxy_host`:同一份制品无论客户端走 http -# 还是 https、走哪个域名进来,上游都是同一个,共享一份缓存更省盘也更省带宽。 -proxy_cache_key $cache_key$request_method$http_accept; - -# 只缓存完整 GET/HEAD(200/301/308);404 短缓存;206(range)/401 等不缓存,避免错片 -proxy_cache_methods GET HEAD; -proxy_cache_valid 200 301 308 1d; -proxy_cache_valid 404 1m; - -# 上游抖动时用陈旧副本兜底;并发同键只回源一次(防击穿) -proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; -proxy_cache_lock on; -proxy_cache_lock_age 10s; -proxy_cache_lock_timeout 30s; - -# 带 If-None-Match / If-Modified-Since 回源校验,省带宽 -proxy_cache_revalidate on; - -# 加速缓存:不被上游 no-cache/Set-Cookie/Vary 拦下(镜像型缓存的标准取舍) -# ⚠️ Vary 被忽略是**必须**的——见上面缓存键的说明,我们已用 Accept 显式分档; -# 留着 Vary 反而会让 nginx 拒绝缓存这些响应。 -proxy_ignore_headers Cache-Control Expires Set-Cookie Vary; - -proxy_http_version 1.1; - -# --------------------------------------------------------------------------- -# 出站请求头(镜像型:默认透传,但抹掉「本网关自己的 Key」) -# --------------------------------------------------------------------------- -# -# 与 LLM/API 代理的 allowlist 策略**刻意不同**:那些是无状态 API,客户端 -# 头没有语义;而镜像必须原样带上客户端的 `Accept` / `Range` / -# `If-None-Match` / `User-Agent`,否则 docker 的 mediaType 协商、range 取 -# 大 blob、条件请求全部失效。所以这里保持 `on`(nginx 默认),只在三个 -# 「网关 Key 的携带位」上显式抹空。 -# -# 为什么必须抹:`sk_` 是客户端访问**本网关**的身份(auth::downstream_key 认 -# Authorization / x-api-key / api-key 三处)。原样透传给上游 = 把网关钥匙 -# 递给上游厂商,对方可拿它反调本网关的其它路由刷额度。ipass 的 LLM 路径 -# 记过同一个坑(宽松厂商把这缺陷掩盖成 200,换严格厂商才暴露成 401)。 -proxy_pass_request_headers on; -proxy_set_header x-api-key ""; -proxy_set_header api-key ""; - -# 变量形式 proxy_pass 不会推导 Host,必须显式设(上游多为 Cloudflare 前端, -# Host 不符会被直接拒) -proxy_set_header Host $cache_host; - -# Authorization 三段式(由模块里的 `$cache_auth` 实现,语义见其文档): -# 配了上游凭据 → 下发配置值(管理员写全 `Bearer xxx` / `Basic yyy`) -# 没配 → 透传客户端值(Docker Hub 的短期 Bearer token 走这条) -# 客户端那把恰好是下行 sk_ → 丢弃 -proxy_set_header Authorization $cache_auth; - -proxy_set_header X-Real-IP $remote_addr; -proxy_set_header X-Forwarded-For $remote_addr; -proxy_set_header X-Forwarded-Proto $scheme; - -# registry 大 blob / 慢索引:放宽超时 -proxy_read_timeout 300s; -proxy_send_timeout 300s; - -# 变量式/直连上游为 HTTPS 时开 SNI;证书校验此阶段关闭(镜像多为公开源, -# 且目标主机来自管理台配置而非请求。要严格校验需另配 proxy_ssl_trusted_certificate) -proxy_ssl_server_name on; -proxy_ssl_protocols TLSv1.2 TLSv1.3; -proxy_ssl_verify off; - -# 观测:每次响应带回缓存命中状态(ipass plugin-cache 亦从同一变量记账) -add_header X-Cache-Status $upstream_cache_status always; -add_header X-Cache-Eco $cache_eco always; diff --git a/tests/cache/.run-auth/conf/cache-routes.conf b/tests/cache/.run-auth/conf/cache-routes.conf deleted file mode 100644 index 4c50106..0000000 --- a/tests/cache/.run-auth/conf/cache-routes.conf +++ /dev/null @@ -1,177 +0,0 @@ -# ============================================================================ -# cache-routes.conf —— 制品缓存网关的「server 内容」(在 server { } 里 include) -# -# 为什么单独成一个文件:这份路由要**两处复用**—— -# * 明文入口 conf/server.cache.conf(本机 e2e / 内网直连) -# * TLS 入口 conf/tls/*.conf(对外,Docker 镜像加速必须走 https) -# 路由、鉴权开关、探针、管理台只写一份,两个入口不会漂。 -# -# 路由约定:/<前缀>/ => <该 route 配置的上游>/ -# /go/ Go module proxy → 默认 proxy.golang.org -# /cargo/ crates.io 稀疏索引 → 默认 index.crates.io -# /cargo-dl/ .crate 包本体 → 默认 static.crates.io -# /pypi/ PyPI 简单索引/JSON → 默认 pypi.org -# /pypi-files/ wheel/sdist 包文件 → 默认 files.pythonhosted.org -# /npm/ npm registry → 默认 registry.npmjs.org -# /maven/ Maven Central → 默认 repo1.maven.org/maven2 -# /v2/ Docker Registry v2 → 默认 registry-1.docker.io -# -# ⚠️ 上游地址**不写在本文件里**:全部来自 plugin-cache 的上游表(管理台 -# 「制品缓存」页,落盘 data/cache-upstreams.json + 写穿共享内存)。 -# 本文件只出现变量名。改上游 = 改管理台,不需要动 conf、不需要 reload。 -# -# ⚠️ 每个带 `ipass_cache on` 的 location 都会在 access 阶段做闸门: -# 1) 该路由没配上游 → 503(不会让请求走到空 $cache_target 变成 500) -# 2) 统一 sk_ Key 鉴权(与 LLM / API 代理同一把)→ 401 -# 3) 每分钟/每日配额 → 429 -# 然后数据面才交给 nginx 原生 proxy_cache。 -# ============================================================================ - - # ---------------- 管理台(多页静态资源,挂在 /admin/ 下)---------------- - # 每页 head 里有 ,所以资源与 /api/ 都是绝对路径。 - # - # **不发版不重启也能修好页面**:所有 HTML / CSS / JS 都加 - # `Cache-Control: no-store`(expires -1 + add_header)。否则用户浏览器会 - # 一直用发版前的旧 JS —— 典型表现是「后端加了字段,页面上看不到」。 - # - # ⚠️ alias 刻意写**相对**路径 `www/`:nginx 把 root/alias 的相对路径解析到 - # **prefix** 而不是 conf/,所以生产(prefix=/usr/local/nginx)得到 - # /usr/local/nginx/www/,本地回归(prefix=tests/.../.run-prod)得到 - # 测试自己的 www/。写死绝对路径则本机必 404。 - location = / { - return 302 /admin; - } - location = /admin { - default_type text/html; - alias www/cache.html; - expires -1; - add_header Cache-Control "no-store" always; - } - # `/admin/`(末尾斜杠、后面无字符)不能直接 alias 到目录:nginx 会把 - # index 拼成 `cache.htmlindex.html` 而 500。内部重定向到具体文件最干净。 - location = /admin/ { - rewrite ^ /admin/cache.html last; - } - location /admin/ { - alias www/; - expires -1; - add_header Cache-Control "no-store" always; - } - - # ---------------- API:管理态(登录会话)+ 账号态(sk_)---------------- - # 由 plugin-cache 认领 /api/admin/cache* 与 /api/v1/cache*; - # 其余 /api/* 交给同一 gateway 里的其它插件(llm/apix/domain),互不干扰。 - location /api/ { - ipass_api on; - } - - # ---------------- 上游连通性探针(管理台「测试」按钮)---------------- - # /api/admin/cache/test 校验 route 后由模块内部重定向到这里。 - # 刻意**复用** $cache_target / $cache_host / $cache_auth —— 与真实转发 - # 走同一套「拼上游 URL + 换凭据」逻辑,所以「测试通过」才等价于 - # 「真实请求能到上游并带上凭据」;另写一套探针代码必然漂移。 - # - # 为什么这里不写 `ipass_cache on`:内部重定向会重跑 access 阶段,而管理台 - # 带的是**登录 Cookie、不是 sk_**,再鉴一次会把自己 401 掉。鉴权已在 - # /api/admin/cache/test 那一跳用 Admin 守卫做过,这里是 internal,外部进不来。 - # - # proxy_cache off:探针是「验连通」,不该用一次探测污染 1d 的制品缓存。 - location /_cache_test/ { - internal; - proxy_cache off; - proxy_pass $cache_target; - proxy_http_version 1.1; - # 探针只验「拼目标 + 换凭据 + 到达上游」,请求头与请求体都不需要转发: - # * 方法由模块钉成 GET(见 plugin-cache api.rs 的 h_admin_test); - # * body 必须 off —— 管理台是用 POST 触发的,不 off 的话那 2 字节 `{}` - # 会连 Content-Length 一起发出去,上游等一个 GET 的 body、 - # 或 keepalive 上残留半截请求体,表现为「测试时好时坏」。 - proxy_pass_request_headers off; - proxy_pass_request_body off; - proxy_set_header Host $cache_host; - proxy_set_header Authorization $cache_auth; - proxy_set_header User-Agent $http_user_agent; - proxy_ssl_server_name on; - proxy_connect_timeout 5s; - proxy_send_timeout 15s; - proxy_read_timeout 15s; - } - - # ---------------- Go module proxy ---------------- - location /go/ { - ipass_cache on; - proxy_cache go; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- crates.io 稀疏索引 ---------------- - location /cargo/ { - ipass_cache on; - proxy_cache cargo; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- .crate 包本体 ---------------- - # ⚠️ crates.io 的 config.json 里 `dl` 字段给的是**绝对 URL** - # (https://static.crates.io/crates/...),cargo 默认直连上游域名、绕过本网关, - # 所以这个 location 只是**落点就绪**。要真正命中需二选一: - # (a) nginx 开 http_sub_module,用 sub_filter 把索引/`config.json` 里的 - # 绝对 URL 改写成 https://<本网关>/cargo-dl;或 - # (b) 网络层把 static.crates.io 指到本网关(内网 DNS + 证书)。 - # 两者都归到 cargo 生态计数(plugin-cache 的 eco_of_route 已做映射)。 - location /cargo-dl/ { - ipass_cache on; - proxy_cache cargo; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- PyPI 简单索引 / JSON ---------------- - location /pypi/ { - ipass_cache on; - proxy_cache pypi; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- wheel/sdist 包文件 ---------------- - # ⚠️ 与 /cargo-dl/ 同理:PyPI simple 页里的 href 是绝对 URL - # (https://files.pythonhosted.org/...),pip 默认直连,需 sub_filter 重写 - # 或 DNS 指向才能命中本 location。归到 pypi 生态计数。 - location /pypi-files/ { - ipass_cache on; - proxy_cache pypi; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- npm registry ---------------- - location /npm/ { - ipass_cache on; - proxy_cache npm; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- Maven Central ---------------- - location /maven/ { - ipass_cache on; - proxy_cache maven; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- Docker Registry v2 ---------------- - # registry-mirrors 要求 https(见 conf/tls/),且**只适合公开镜像**: - # 缓存键不含用户身份,私有镜像会被串档(见 cache-common.conf 的说明)。 - # 客户端先向 auth.docker.io 换短期 Bearer token 再带 Authorization 来拉, - # 该 token 由 cache-common.conf 的 $cache_auth 透传给上游(但网关自己的 - # sk_ 会被剔除)。上游路径的 `/v2` 前缀由模块的 upstream_path_prefix 补回。 - location /v2/ { - ipass_cache on; - proxy_cache docker; - proxy_pass $cache_target; - include cache-common.conf; - } diff --git a/tests/cache/.run-auth/conf/mime.types b/tests/cache/.run-auth/conf/mime.types deleted file mode 100644 index 1c00d70..0000000 --- a/tests/cache/.run-auth/conf/mime.types +++ /dev/null @@ -1,99 +0,0 @@ - -types { - text/html html htm shtml; - text/css css; - text/xml xml; - image/gif gif; - image/jpeg jpeg jpg; - application/javascript js; - application/atom+xml atom; - application/rss+xml rss; - - text/mathml mml; - text/plain txt; - text/vnd.sun.j2me.app-descriptor jad; - text/vnd.wap.wml wml; - text/x-component htc; - - image/avif avif; - image/png png; - image/svg+xml svg svgz; - image/tiff tif tiff; - image/vnd.wap.wbmp wbmp; - image/webp webp; - image/x-icon ico; - image/x-jng jng; - image/x-ms-bmp bmp; - - font/woff woff; - font/woff2 woff2; - - application/java-archive jar war ear; - application/json json; - application/mac-binhex40 hqx; - application/msword doc; - application/pdf pdf; - application/postscript ps eps ai; - application/rtf rtf; - application/vnd.apple.mpegurl m3u8; - application/vnd.google-earth.kml+xml kml; - application/vnd.google-earth.kmz kmz; - application/vnd.ms-excel xls; - application/vnd.ms-fontobject eot; - application/vnd.ms-powerpoint ppt; - application/vnd.oasis.opendocument.graphics odg; - application/vnd.oasis.opendocument.presentation odp; - application/vnd.oasis.opendocument.spreadsheet ods; - application/vnd.oasis.opendocument.text odt; - application/vnd.openxmlformats-officedocument.presentationml.presentation - pptx; - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - xlsx; - application/vnd.openxmlformats-officedocument.wordprocessingml.document - docx; - application/vnd.wap.wmlc wmlc; - application/wasm wasm; - application/x-7z-compressed 7z; - application/x-cocoa cco; - application/x-java-archive-diff jardiff; - application/x-java-jnlp-file jnlp; - application/x-makeself run; - application/x-perl pl pm; - application/x-pilot prc pdb; - application/x-rar-compressed rar; - application/x-redhat-package-manager rpm; - application/x-sea sea; - application/x-shockwave-flash swf; - application/x-stuffit sit; - application/x-tcl tcl tk; - application/x-x509-ca-cert der pem crt; - application/x-xpinstall xpi; - application/xhtml+xml xhtml; - application/xspf+xml xspf; - application/zip zip; - - application/octet-stream bin exe dll; - application/octet-stream deb; - application/octet-stream dmg; - application/octet-stream iso img; - application/octet-stream msi msp msm; - - audio/midi mid midi kar; - audio/mpeg mp3; - audio/ogg ogg; - audio/x-m4a m4a; - audio/x-realaudio ra; - - video/3gpp 3gpp 3gp; - video/mp2t ts; - video/mp4 mp4; - video/mpeg mpeg mpg; - video/quicktime mov; - video/webm webm; - video/x-flv flv; - video/x-m4v m4v; - video/x-mng mng; - video/x-ms-asf asx asf; - video/x-ms-wmv wmv; - video/x-msvideo avi; -} diff --git a/tests/cache/.run-auth/conf/nginx.cache.conf b/tests/cache/.run-auth/conf/nginx.cache.conf deleted file mode 100644 index d57cf47..0000000 --- a/tests/cache/.run-auth/conf/nginx.cache.conf +++ /dev/null @@ -1,87 +0,0 @@ -# ============================================================================ -# nginx.cache.conf —— 统一制品缓存网关 · 顶层配置 -# -# vanilla nginx + ipass Rust 动态模块(plugin-cache) -# -# 本机回归:bash tests/cache/run.sh -# 生产部署:把本文件放到 nginx prefix 的 conf/ 下,同目录放 -# server.cache.conf / cache-routes.conf / cache-common.conf / -# mime.types,启动即可;要对外 https 再加 conf/tls/*.conf。 -# -# 数据面 = nginx 原生 proxy_cache(六生态各一个 zone,落本地盘); -# 管理面 = ipass plugin-cache(access 鉴权 + log 按生态记账 + 管理 API)。 -# -# ⚠️ 上游地址/凭据**不在 conf 里**:全在 plugin-cache 的上游表 -# (管理台「制品缓存」页 → data/cache-upstreams.json → 写穿共享内存)。 -# conf 只出现 `$cache_*` 变量名,所以改上游不需要动 conf、不需要 reload。 -# -# ⚠️ 因为 proxy_pass 是变量形式,`resolver` 是**必需**的:nginx 无法在配置 -# 解析期把变量里的主机名解析成 IP,只能运行期查 DNS。缺 resolver 的表现是 -# `/go/...` 一律 500(no resolver defined to resolve ...)。 -# -# 本构建(build/build-nginx-ssl.sh)对齐 ipass 生产的 Dockerfile 参数: -# --with-pcre --with-http_ssl_module --with-http_realip_module -# 即 rewrite 模块与 SSL 都在编,所以本文件与 server.cache.conf / -# cache-routes.conf 里可以用 `return` / `rewrite` / `ssl_certificate`。 -# ============================================================================ - -load_module /Users/rong/Desktop/cache/rust/target/debug/libipass_gateway.dylib; - -worker_processes 1; - error_log /Users/rong/Desktop/cache/tests/cache/.run-auth/logs/error.log info; - pid /Users/rong/Desktop/cache/tests/cache/.run-auth/logs/nginx.pid; - -# 初始管理员口令(不声明则会被 nginx 从环境中剥离) -env ADMIN_INIT_PASSWORD; - -events { - worker_connections 4096; -} - -http { - include mime.types; - default_type application/octet-stream; # 缺 Content-Type 时的兜底; - # API 响应由模块显式设 application/json - - sendfile on; - keepalive_timeout 65; - - # ========== 变量式上游的必需项 ========== - # proxy_pass $cache_target 里的主机名只能运行期解析(见文件头注释)。 - # 两个国内解析器,30s 缓存;ipv6=off 避免拿到不可达的 AAAA 卡住连接。 - resolver 223.5.5.5 119.29.29.29 valid=30s ipv6=off; - resolver_timeout 5s; - - # ========== 可选 TLS(HTTPS)========== - # conf/tls/ 下放任意 *.conf(完整的 443/18098 server 块)即启用对应域名的 - # HTTPS;目录为空或不存在时 include 空匹配,不影响启动。 - # 模板见 conf/tls/cache.conf.example,说明见 conf/tls/README.md - include /Users/rong/Desktop/cache/tests/cache/.run-auth/conf/tls/*.conf; - - # ========== ipass 内核:共享内存 + 数据目录 ========== - # 一张共享内存表承载:上游表(cx:)/ 命中计数(cache:)/ 管理会话 / 限流窗口。 - # 上游表条目数 = 路由数(个位数),10m 绰绰有余。 - ipass_state state 10m; - ipass_data_dir /Users/rong/Desktop/cache/tests/cache/.run-auth/data; - ipass_audit_dir /Users/rong/Desktop/cache/tests/cache/.run-auth/audit; - - # ========== 六类生态缓存区(本地盘热层)========== - # 各生态独立 zone,互不串档、独立上限;use_temp_path=off 省一次改名。 - # ⚠️ cargo / pypi 各有两个 route 共用同一个 zone(索引 + 包本体): - # 这是刻意的——同一门语言的流量记在一起才看得懂,而 zone 内靠 - # $cache_key 的 route 前缀天然分档,不会串。 - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-auth/cache/go levels=1:2 keys_zone=go:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-auth/cache/cargo levels=1:2 keys_zone=cargo:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-auth/cache/pypi levels=1:2 keys_zone=pypi:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-auth/cache/npm levels=1:2 keys_zone=npm:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-auth/cache/maven levels=1:2 keys_zone=maven:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-auth/cache/docker levels=1:2 keys_zone=docker:200m max_size=200g inactive=7d use_temp_path=off; - - # 命中率日志(排障用;真实指标由 plugin-cache 的共享内存计数器提供) - log_format cache '$remote_addr [$time_local] "$request" $status ' - '$body_bytes_sent cache=$upstream_cache_status eco=$cache_eco ' - 'uri=$uri rt=$request_time'; - access_log /Users/rong/Desktop/cache/tests/cache/.run-auth/logs/cache.log cache; - - include server.cache.conf; -} diff --git a/tests/cache/.run-auth/conf/server.cache.conf b/tests/cache/.run-auth/conf/server.cache.conf deleted file mode 100644 index 7b37077..0000000 --- a/tests/cache/.run-auth/conf/server.cache.conf +++ /dev/null @@ -1,30 +0,0 @@ -# ============================================================================ -# server.cache.conf —— 统一制品缓存 server(明文入口) -# -# 这是**内网 / 本机 e2e** 用的明文入口。对外的 https 入口在 conf/tls/*.conf -# (模板见 conf/tls/cache.conf.example)——两者 include 同一份 cache-routes.conf, -# 所以路由只此一份、不会漂。 -# -# 为什么要拆两个入口:Docker 的 `registry-mirrors` 只接受 https(明文地址会 -# 被 daemon 当成 insecure registry 拒绝),而本机自测/内网互调又不想折腾证书。 -# -# 为什么本 server 不写 listen ... ssl:证书与域名属于部署环境(见 conf/tls/), -# 写死在这里会让「同一份 conf 换域名」变成改代码。 -# ============================================================================ - -server { - listen 54166; - server_name _; - - # registry 大 blob 直通,不设体积上限(0 = 不限制) - client_max_body_size 0; - # 请求体都是小 JSON / 空体,调大缓冲区让它留在内存里,避免刷临时文件 - client_body_buffer_size 1m; - - # 管理台 `location = /` 会 302 到 /admin。不关掉的话 nginx 默认写 - # 绝对 URL + 实际 listen 端口,反代/端口映射后会把浏览器踢出链路。 - absolute_redirect off; - port_in_redirect off; - - include cache-routes.conf; -} diff --git a/tests/cache/.run-auth/data/cache-upstreams.json b/tests/cache/.run-auth/data/cache-upstreams.json deleted file mode 100644 index 8031caf..0000000 --- a/tests/cache/.run-auth/data/cache-upstreams.json +++ /dev/null @@ -1 +0,0 @@ -{"items":{"cargo":{"base":"http://127.0.0.1:54167","key":"","mode":"proxy","name":"cargo 测试上游(无凭据)","desc":"假上游","enabled":true,"note":"","created_at":1757500000,"updated_at":1757500000},"go":{"base":"http://127.0.0.1:54167","key":"Bearer upstream-secret-token","mode":"proxy","name":"Go 测试上游","desc":"假上游","enabled":true,"note":"","created_at":1757500000,"updated_at":1757500000},"npm":{"base":"http://127.0.0.1:54167","key":"","mode":"proxy","name":"e2e-runtime","desc":"","enabled":true,"note":"","created_at":1789909388,"updated_at":1789909388}}} \ No newline at end of file diff --git a/tests/cache/.run-auth/data/llm-stats.json b/tests/cache/.run-auth/data/llm-stats.json deleted file mode 100644 index f0d1106..0000000 --- a/tests/cache/.run-auth/data/llm-stats.json +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"saved_at":1789909390,"saved_at_text":"2026-09-20 21:03:10","keys":{}} \ No newline at end of file diff --git a/tests/cache/.run-auth/data/users.json b/tests/cache/.run-auth/data/users.json deleted file mode 100644 index 1c50523..0000000 --- a/tests/cache/.run-auth/data/users.json +++ /dev/null @@ -1 +0,0 @@ -{"admin":{"username":"admin","display_name":"","role":"admin","salt":"5a1b2c3d4e5f60718293a4b5c6d7e8f9","pwd_hash":"062b45d8f3cbef3d30464859355401bb37193868dc42eb941de86855416ab5e8","api_key":"sk_admin_0000000000000000000000000000000000000000000000ab","allowed_models":[],"llm_rpm":60,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"alice":{"username":"alice","display_name":"爱丽丝","role":"user","salt":"00112233445566778899aabbccddeeff","pwd_hash":"00","api_key":"sk_alice_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","allowed_models":["openai"],"llm_rpm":60,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"bob":{"username":"bob","display_name":"","role":"user","salt":"ffeeddccbbaa99887766554433221100","pwd_hash":"00","api_key":"sk_bob_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","allowed_models":[],"llm_rpm":60,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"carol":{"username":"carol","display_name":"","role":"user","salt":"112233445566778899aabbccddeeff00","pwd_hash":"00","api_key":"sk_carol_cccccccccccccccccccccccccccccccccccccccccccccc","allowed_models":[],"llm_rpm":2,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"dave":{"username":"dave","display_name":"","role":"user","salt":"aabbccddeeff00112233445566778899","pwd_hash":"00","api_key":"sk_dave_dddddddddddddddddddddddddddddddddddddddddddddddd","allowed_models":[],"llm_rpm":0,"llm_daily":2,"temp_grant_until":0,"created_at":1757500000}} \ No newline at end of file diff --git a/tests/cache/.run-auth/logs/cache.log b/tests/cache/.run-auth/logs/cache.log deleted file mode 100644 index 577dead..0000000 --- a/tests/cache/.run-auth/logs/cache.log +++ /dev/null @@ -1,189 +0,0 @@ -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 78 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 78 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.028 -127.0.0.1 [20/Sep/2026:16:06:35 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.030 -127.0.0.1 [20/Sep/2026:16:06:36 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.021 -127.0.0.1 [20/Sep/2026:16:06:36 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:38 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:38 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.029 -127.0.0.1 [20/Sep/2026:16:06:39 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:39 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:06:39 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:33 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.019 -127.0.0.1 [20/Sep/2026:16:20:36 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:36 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=HIT eco=cargo uri=/cargo/config.json rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:37 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:37 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:20:37 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.022 -127.0.0.1 [20/Sep/2026:16:21:23 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.024 -127.0.0.1 [20/Sep/2026:16:21:24 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.014 -127.0.0.1 [20/Sep/2026:16:21:24 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.015 -127.0.0.1 [20/Sep/2026:16:21:26 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:26 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.016 -127.0.0.1 [20/Sep/2026:16:21:27 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:27 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:21:27 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:10 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.082 -127.0.0.1 [20/Sep/2026:16:24:11 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.011 -127.0.0.1 [20/Sep/2026:16:24:11 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.014 -127.0.0.1 [20/Sep/2026:16:24:11 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.012 -127.0.0.1 [20/Sep/2026:16:24:12 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:13 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.016 -127.0.0.1 [20/Sep/2026:16:24:13 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:13 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:13 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:14 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:14 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:14 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:14 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:14 +0800] "POST /api/admin/cache/save HTTP/1.1" 200 163 cache=- eco=- uri=/api/admin/cache/save rt=0.000 -127.0.0.1 [20/Sep/2026:16:24:15 +0800] "GET /npm/foo HTTP/1.1" 200 13 cache=MISS eco=npm uri=/npm/foo rt=0.016 -127.0.0.1 [20/Sep/2026:16:24:15 +0800] "GET /api/v1/cache HTTP/1.1" 200 1408 cache=- eco=- uri=/api/v1/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.021 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.027 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.016 -127.0.0.1 [20/Sep/2026:16:29:24 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.016 -127.0.0.1 [20/Sep/2026:16:29:27 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:28 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.029 -127.0.0.1 [20/Sep/2026:16:29:29 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:29 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:29 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:29 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:29 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:29 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:30 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:30 +0800] "POST /api/admin/cache/save HTTP/1.1" 200 163 cache=- eco=- uri=/api/admin/cache/save rt=0.000 -127.0.0.1 [20/Sep/2026:16:29:31 +0800] "GET /npm/foo HTTP/1.1" 200 13 cache=MISS eco=npm uri=/npm/foo rt=0.030 -127.0.0.1 [20/Sep/2026:16:29:31 +0800] "GET /api/v1/cache HTTP/1.1" 200 1408 cache=- eco=- uri=/api/v1/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.021 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.024 -127.0.0.1 [20/Sep/2026:16:59:23 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.017 -127.0.0.1 [20/Sep/2026:16:59:24 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.018 -127.0.0.1 [20/Sep/2026:16:59:26 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:26 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.017 -127.0.0.1 [20/Sep/2026:16:59:27 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:27 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:27 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:27 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:27 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:27 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:28 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:28 +0800] "POST /api/admin/cache/save HTTP/1.1" 200 163 cache=- eco=- uri=/api/admin/cache/save rt=0.000 -127.0.0.1 [20/Sep/2026:16:59:28 +0800] "GET /npm/foo HTTP/1.1" 200 13 cache=MISS eco=npm uri=/npm/foo rt=0.016 -127.0.0.1 [20/Sep/2026:16:59:28 +0800] "GET /api/v1/cache HTTP/1.1" 200 1408 cache=- eco=- uri=/api/v1/cache rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:13 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:13 +0800] "GET /admin HTTP/1.1" 200 3256 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.015 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.015 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.012 -127.0.0.1 [20/Sep/2026:17:58:14 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.014 -127.0.0.1 [20/Sep/2026:17:58:15 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.016 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:16 +0800] "GET /api/admin/cache HTTP/1.1" 200 4804 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:17 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:17 +0800] "POST /api/admin/cache/save HTTP/1.1" 200 163 cache=- eco=- uri=/api/admin/cache/save rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:17 +0800] "GET /npm/foo HTTP/1.1" 200 13 cache=MISS eco=npm uri=/npm/foo rt=0.012 -127.0.0.1 [20/Sep/2026:17:58:17 +0800] "GET /api/v1/cache HTTP/1.1" 200 1408 cache=- eco=- uri=/api/v1/cache rt=0.000 -127.0.0.1 [20/Sep/2026:17:58:18 +0800] "POST /api/admin/cache/test?route=go HTTP/1.1" 200 10 cache=- eco=go uri=/_cache_test/go rt=0.012 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET /admin HTTP/1.1" 200 5452 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET /admin HTTP/1.1" 200 5452 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:07 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:08 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.229 -127.0.0.1 [20/Sep/2026:18:29:08 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.521 -127.0.0.1 [20/Sep/2026:18:29:08 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.543 -127.0.0.1 [20/Sep/2026:18:29:08 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.024 -127.0.0.1 [20/Sep/2026:18:29:10 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:10 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.012 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "GET /api/admin/cache HTTP/1.1" 200 7469 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "GET /api/admin/cache HTTP/1.1" 200 7469 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:11 +0800] "POST /api/admin/cache/save HTTP/1.1" 200 163 cache=- eco=- uri=/api/admin/cache/save rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:12 +0800] "GET /npm/foo HTTP/1.1" 200 13 cache=MISS eco=npm uri=/npm/foo rt=0.010 -127.0.0.1 [20/Sep/2026:18:29:12 +0800] "GET /api/v1/cache HTTP/1.1" 200 1408 cache=- eco=- uri=/api/v1/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:12 +0800] "POST /api/admin/cache/test?route=go HTTP/1.1" 200 10 cache=- eco=go uri=/_cache_test/go rt=0.011 -127.0.0.1 [20/Sep/2026:21:03:03 +0800] "GET /admin HTTP/1.1" 200 6437 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:03 +0800] "GET /admin HTTP/1.1" 200 6437 cache=- eco=- uri=/admin rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /api/admin/cache HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /api/v1/cache/stats HTTP/1.1" 401 99 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /api/v1/cache/stats HTTP/1.1" 200 694 cache=- eco=- uri=/api/v1/cache/stats rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /go/@v/list HTTP/1.1" 200 90 cache=MISS eco=go uri=/go/@v/list rt=0.021 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /go/@v/list HTTP/1.1" 200 47 cache=MISS eco=go uri=/go/@v/list rt=0.016 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=MISS eco=go uri=/go/@v/list rt=0.018 -127.0.0.1 [20/Sep/2026:21:03:04 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=MISS eco=go uri=/go/@v/list rt=0.017 -127.0.0.1 [20/Sep/2026:21:03:06 +0800] "GET /go/@v/list?x=1 HTTP/1.1" 200 21 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:06 +0800] "GET /cargo/config.json HTTP/1.1" 200 21 cache=MISS eco=cargo uri=/cargo/config.json rt=0.020 -127.0.0.1 [20/Sep/2026:21:03:07 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:07 +0800] "GET /go/@v/list HTTP/1.1" 200 17 cache=HIT eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:07 +0800] "GET /go/@v/list HTTP/1.1" 429 139 cache=- eco=go uri=/go/@v/list rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:07 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:07 +0800] "GET /api/admin/cache HTTP/1.1" 200 7469 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:07 +0800] "GET /api/admin/cache HTTP/1.1" 200 7469 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:08 +0800] "GET /npm/foo HTTP/1.1" 500 116 cache=- eco=- uri=/npm/foo rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:08 +0800] "POST /api/admin/cache/save HTTP/1.1" 200 163 cache=- eco=- uri=/api/admin/cache/save rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:08 +0800] "GET /npm/foo HTTP/1.1" 200 13 cache=MISS eco=npm uri=/npm/foo rt=0.018 -127.0.0.1 [20/Sep/2026:21:03:08 +0800] "GET /api/v1/cache HTTP/1.1" 200 1408 cache=- eco=- uri=/api/v1/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:03:09 +0800] "POST /api/admin/cache/test?route=go HTTP/1.1" 200 10 cache=- eco=go uri=/_cache_test/go rt=0.016 diff --git a/tests/cache/.run-auth/logs/cookie.jar b/tests/cache/.run-auth/logs/cookie.jar deleted file mode 100644 index bea6e43..0000000 --- a/tests/cache/.run-auth/logs/cookie.jar +++ /dev/null @@ -1,5 +0,0 @@ -# Netscape HTTP Cookie File -# https://curl.se/docs/http-cookies.html -# This file was generated by libcurl! Edit at your own risk. - -#HttpOnly_127.0.0.1 FALSE / FALSE 1789938187 ipass_admin_session a0d282debcb9f9745193c3a7bbe7cc3eb1e00b30893e1442e2cbc35d0064dad2 diff --git a/tests/cache/.run-auth/logs/error.log b/tests/cache/.run-auth/logs/error.log deleted file mode 100644 index dcf5d9d..0000000 --- a/tests/cache/.run-auth/logs/error.log +++ /dev/null @@ -1,684 +0,0 @@ -2026/09/20 16:03:16 [emerg] 28713#0: open() "/usr/local/nginx/logs/cache.log" failed (2: No such file or directory) -2026/09/20 16:03:18 [emerg] 28722#0: open() "/usr/local/nginx/logs/cache.log" failed (2: No such file or directory) -2026/09/20 16:03:18 [emerg] 28723#0: open() "/usr/local/nginx/logs/cache.log" failed (2: No such file or directory) -2026/09/20 16:03:23 [notice] 28825#0: signal process started -2026/09/20 16:03:23 [error] 28825#0: invalid PID number "" in "/Users/rong/Desktop/cache/tests/cache/.run-auth/logs/nginx.pid" -2026/09/20 16:04:19 [emerg] 28984#0: invalid number of arguments in "listen" directive in /Users/rong/Desktop/cache/tests/cache/.run-auth/conf/server.cache.conf:16 -2026/09/20 16:04:20 [emerg] 28992#0: invalid number of arguments in "listen" directive in /Users/rong/Desktop/cache/tests/cache/.run-auth/conf/server.cache.conf:16 -2026/09/20 16:04:20 [emerg] 28993#0: invalid number of arguments in "listen" directive in /Users/rong/Desktop/cache/tests/cache/.run-auth/conf/server.cache.conf:16 -2026/09/20 16:04:25 [emerg] 29096#0: invalid number of arguments in "listen" directive in /Users/rong/Desktop/cache/tests/cache/.run-auth/conf/server.cache.conf:16 -2026/09/20 16:05:00 [emerg] 29252#0: open() "/usr/local/nginx/logs/cache.log" failed (2: No such file or directory) -2026/09/20 16:05:00 [emerg] 29260#0: open() "/usr/local/nginx/logs/cache.log" failed (2: No such file or directory) -2026/09/20 16:05:00 [emerg] 29261#0: open() "/usr/local/nginx/logs/cache.log" failed (2: No such file or directory) -2026/09/20 16:05:06 [notice] 29363#0: signal process started -2026/09/20 16:05:06 [error] 29363#0: invalid PID number "" in "/Users/rong/Desktop/cache/tests/cache/.run-auth/logs/nginx.pid" -2026/09/20 16:06:33 [notice] 29591#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:06:33 [notice] 29591#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:06:34 [notice] 29599#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:06:34 [notice] 29599#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:06:34 [notice] 29600#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:06:34 [notice] 29600#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:06:34 [notice] 29600#0: using the "kqueue" event method -2026/09/20 16:06:34 [notice] 29600#0: nginx/1.30.4 -2026/09/20 16:06:34 [notice] 29600#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 16:06:34 [notice] 29600#0: OS: Darwin 21.6.0 -2026/09/20 16:06:34 [notice] 29600#0: hw.ncpu: 4 -2026/09/20 16:06:34 [notice] 29600#0: net.inet.tcp.sendspace: 131072 -2026/09/20 16:06:34 [notice] 29600#0: kern.ipc.somaxconn: 128 -2026/09/20 16:06:34 [notice] 29600#0: hw.cachelinesize: 64 -2026/09/20 16:06:34 [notice] 29600#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 16:06:34 [notice] 29601#0: start worker processes -2026/09/20 16:06:34 [notice] 29601#0: start worker process 29602 -2026/09/20 16:06:34 [notice] 29601#0: start cache manager process 29604 -2026/09/20 16:06:34 [notice] 29601#0: start cache loader process 29605 -2026/09/20 16:06:34 [notice] 29601#0: signal 23 (SIGIO) received from 29604 -2026/09/20 16:06:34 [notice] 29601#0: signal 23 (SIGIO) received from 29602 -2026/09/20 16:06:35 [info] 29602#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:35 [info] 29602#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:35 [info] 29602#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:35 [info] 29602#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:35 [warn] 29602#0: [api] API Key 校验失败 client=127.0.0.1 -2026/09/20 16:06:35 [info] 29602#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:35 [warn] 29602#0: [api] API Key 校验失败 client=127.0.0.1 -2026/09/20 16:06:35 [info] 29602#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:35 [warn] 29602#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:06:35 [error] 29602#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:61721" -2026/09/20 16:06:35 [alert] 29602#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:61721" -2026/09/20 16:06:35 [info] 29602#0: *8 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:61722/@v/list", host: "127.0.0.1:61721" -2026/09/20 16:06:35 [alert] 29602#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:61722/@v/list", host: "127.0.0.1:61721" -2026/09/20 16:06:35 [warn] 29602#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 16:06:35 [info] 29602#0: *10 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:61722/@v/list", host: "127.0.0.1:61721" -2026/09/20 16:06:35 [alert] 29602#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:61722/@v/list", host: "127.0.0.1:61721" -2026/09/20 16:06:36 [info] 29602#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:36 [info] 29602#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:38 [info] 29602#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:38 [info] 29602#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:39 [info] 29602#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:39 [info] 29602#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:06:39 [warn] 29602#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 16:06:39 [alert] 29602#0: *20 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:61721" -2026/09/20 16:06:39 [notice] 29699#0: signal process started -2026/09/20 16:06:39 [notice] 29601#0: signal 3 (SIGQUIT) received from 29699, shutting down -2026/09/20 16:06:39 [notice] 29602#0: gracefully shutting down -2026/09/20 16:06:39 [notice] 29602#0: exiting -2026/09/20 16:06:39 [notice] 29604#0: exiting -2026/09/20 16:06:39 [notice] 29605#0: exiting -2026/09/20 16:06:39 [info] 29602#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 16:06:39 [notice] 29601#0: signal 23 (SIGIO) received from 29605 -2026/09/20 16:06:39 [notice] 29602#0: exit -2026/09/20 16:06:39 [notice] 29601#0: signal 20 (SIGCHLD) received from 29604 -2026/09/20 16:06:39 [notice] 29601#0: cache loader process 29605 exited with code 0 -2026/09/20 16:06:39 [notice] 29601#0: cache manager process 29604 exited with code 0 -2026/09/20 16:06:39 [notice] 29601#0: signal 23 (SIGIO) received -2026/09/20 16:06:39 [notice] 29601#0: signal 23 (SIGIO) received -2026/09/20 16:06:39 [notice] 29601#0: signal 20 (SIGCHLD) received from 29602 -2026/09/20 16:06:39 [notice] 29601#0: worker process 29602 exited with code 0 -2026/09/20 16:06:39 [notice] 29601#0: exit -2026/09/20 16:20:30 [notice] 31527#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:20:30 [notice] 31527#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:20:32 [notice] 31535#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:20:32 [notice] 31535#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:20:32 [notice] 31536#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:20:32 [notice] 31536#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:20:32 [notice] 31536#0: using the "kqueue" event method -2026/09/20 16:20:32 [notice] 31536#0: nginx/1.30.4 -2026/09/20 16:20:32 [notice] 31536#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 16:20:32 [notice] 31536#0: OS: Darwin 21.6.0 -2026/09/20 16:20:32 [notice] 31536#0: hw.ncpu: 4 -2026/09/20 16:20:32 [notice] 31536#0: net.inet.tcp.sendspace: 131072 -2026/09/20 16:20:32 [notice] 31536#0: kern.ipc.somaxconn: 128 -2026/09/20 16:20:32 [notice] 31536#0: hw.cachelinesize: 64 -2026/09/20 16:20:32 [notice] 31536#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 16:20:32 [notice] 31537#0: start worker processes -2026/09/20 16:20:32 [notice] 31537#0: start worker process 31538 -2026/09/20 16:20:32 [notice] 31537#0: start cache manager process 31539 -2026/09/20 16:20:32 [notice] 31537#0: start cache loader process 31540 -2026/09/20 16:20:32 [notice] 31539#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:20:32 [notice] 31537#0: signal 23 (SIGIO) received from 31539 -2026/09/20 16:20:32 [notice] 31538#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:20:32 [notice] 31537#0: signal 23 (SIGIO) received from 31538 -2026/09/20 16:20:32 [notice] 31540#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:20:33 [info] 31538#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [info] 31538#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [info] 31538#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [info] 31538#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [info] 31538#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [info] 31538#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [warn] 31538#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:20:33 [error] 31538#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62141" -2026/09/20 16:20:33 [alert] 31538#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62141" -2026/09/20 16:20:33 [alert] 31538#0: *8 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:62141" -2026/09/20 16:20:33 [warn] 31538#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 16:20:33 [alert] 31538#0: *9 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:62141" -2026/09/20 16:20:33 [info] 31538#0: *10 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:33 [info] 31538#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:36 [info] 31538#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:36 [info] 31538#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:37 [info] 31538#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:37 [info] 31538#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:20:37 [warn] 31538#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 16:20:37 [alert] 31538#0: *17 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:62141" -2026/09/20 16:20:37 [notice] 31645#0: signal process started -2026/09/20 16:20:37 [notice] 31537#0: signal 3 (SIGQUIT) received from 31645, shutting down -2026/09/20 16:20:37 [notice] 31538#0: gracefully shutting down -2026/09/20 16:20:37 [notice] 31537#0: signal 23 (SIGIO) received -2026/09/20 16:20:37 [notice] 31538#0: exiting -2026/09/20 16:20:37 [notice] 31540#0: exiting -2026/09/20 16:20:37 [notice] 31537#0: signal 23 (SIGIO) received from 31540 -2026/09/20 16:20:37 [notice] 31537#0: signal 23 (SIGIO) received from 31539 -2026/09/20 16:20:37 [notice] 31539#0: exiting -2026/09/20 16:20:37 [notice] 31537#0: signal 20 (SIGCHLD) received from 31540 -2026/09/20 16:20:37 [notice] 31537#0: cache loader process 31540 exited with code 0 -2026/09/20 16:20:37 [info] 31538#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 16:20:37 [notice] 31537#0: signal 23 (SIGIO) received -2026/09/20 16:20:37 [notice] 31537#0: signal 20 (SIGCHLD) received from 31539 -2026/09/20 16:20:37 [notice] 31537#0: cache manager process 31539 exited with code 0 -2026/09/20 16:20:37 [notice] 31537#0: signal 23 (SIGIO) received -2026/09/20 16:20:37 [notice] 31538#0: exit -2026/09/20 16:20:37 [notice] 31537#0: signal 23 (SIGIO) received -2026/09/20 16:20:37 [notice] 31537#0: signal 20 (SIGCHLD) received from 31538 -2026/09/20 16:20:37 [notice] 31537#0: worker process 31538 exited with code 0 -2026/09/20 16:20:37 [notice] 31537#0: exit -2026/09/20 16:21:21 [notice] 31812#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:21:21 [notice] 31812#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:21:22 [notice] 31820#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:21:22 [notice] 31820#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:21:22 [notice] 31821#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:21:22 [notice] 31821#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:21:22 [notice] 31821#0: using the "kqueue" event method -2026/09/20 16:21:22 [notice] 31821#0: nginx/1.30.4 -2026/09/20 16:21:22 [notice] 31821#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 16:21:22 [notice] 31821#0: OS: Darwin 21.6.0 -2026/09/20 16:21:22 [notice] 31821#0: hw.ncpu: 4 -2026/09/20 16:21:22 [notice] 31821#0: net.inet.tcp.sendspace: 131072 -2026/09/20 16:21:22 [notice] 31821#0: kern.ipc.somaxconn: 128 -2026/09/20 16:21:22 [notice] 31821#0: hw.cachelinesize: 64 -2026/09/20 16:21:22 [notice] 31821#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 16:21:22 [notice] 31822#0: start worker processes -2026/09/20 16:21:22 [notice] 31822#0: start worker process 31823 -2026/09/20 16:21:22 [notice] 31822#0: start cache manager process 31824 -2026/09/20 16:21:22 [notice] 31822#0: start cache loader process 31825 -2026/09/20 16:21:22 [notice] 31824#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:21:22 [notice] 31822#0: signal 23 (SIGIO) received from 31824 -2026/09/20 16:21:22 [notice] 31823#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:21:22 [notice] 31822#0: signal 23 (SIGIO) received from 31823 -2026/09/20 16:21:22 [notice] 31822#0: signal 23 (SIGIO) received -2026/09/20 16:21:22 [notice] 31825#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:21:23 [info] 31823#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:23 [info] 31823#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:23 [info] 31823#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:23 [info] 31823#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:23 [info] 31823#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:23 [info] 31823#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:23 [warn] 31823#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:21:23 [error] 31823#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62190" -2026/09/20 16:21:23 [alert] 31823#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62190" -2026/09/20 16:21:23 [info] 31823#0: *8 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62191/@v/list", host: "127.0.0.1:62190" -2026/09/20 16:21:23 [alert] 31823#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62191/@v/list", host: "127.0.0.1:62190" -2026/09/20 16:21:23 [warn] 31823#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 16:21:23 [info] 31823#0: *10 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62191/@v/list", host: "127.0.0.1:62190" -2026/09/20 16:21:23 [alert] 31823#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62191/@v/list", host: "127.0.0.1:62190" -2026/09/20 16:21:24 [info] 31823#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:24 [info] 31823#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:26 [info] 31823#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:26 [info] 31823#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:27 [info] 31823#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:27 [info] 31823#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:21:27 [warn] 31823#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 16:21:27 [alert] 31823#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:62190" -2026/09/20 16:21:27 [notice] 31926#0: signal process started -2026/09/20 16:21:27 [notice] 31822#0: signal 3 (SIGQUIT) received from 31926, shutting down -2026/09/20 16:21:27 [notice] 31822#0: signal 23 (SIGIO) received -2026/09/20 16:21:27 [notice] 31823#0: gracefully shutting down -2026/09/20 16:21:27 [notice] 31823#0: exiting -2026/09/20 16:21:27 [notice] 31824#0: exiting -2026/09/20 16:21:27 [notice] 31825#0: exiting -2026/09/20 16:21:27 [notice] 31822#0: signal 23 (SIGIO) received from 31825 -2026/09/20 16:21:27 [info] 31823#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 16:21:27 [notice] 31822#0: signal 20 (SIGCHLD) received from 31824 -2026/09/20 16:21:27 [notice] 31822#0: cache manager process 31824 exited with code 0 -2026/09/20 16:21:27 [notice] 31822#0: signal 23 (SIGIO) received -2026/09/20 16:21:27 [notice] 31822#0: signal 20 (SIGCHLD) received from 31825 -2026/09/20 16:21:27 [notice] 31822#0: cache loader process 31825 exited with code 0 -2026/09/20 16:21:27 [notice] 31822#0: signal 23 (SIGIO) received -2026/09/20 16:21:27 [notice] 31823#0: exit -2026/09/20 16:21:27 [notice] 31822#0: signal 23 (SIGIO) received -2026/09/20 16:21:27 [notice] 31822#0: signal 20 (SIGCHLD) received from 31823 -2026/09/20 16:21:27 [notice] 31822#0: worker process 31823 exited with code 0 -2026/09/20 16:21:27 [notice] 31822#0: exit -2026/09/20 16:24:09 [notice] 32346#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:24:09 [notice] 32346#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:24:09 [notice] 32354#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:24:09 [notice] 32354#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:24:09 [notice] 32355#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:24:09 [notice] 32355#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:24:09 [notice] 32355#0: using the "kqueue" event method -2026/09/20 16:24:09 [notice] 32355#0: nginx/1.30.4 -2026/09/20 16:24:09 [notice] 32355#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 16:24:09 [notice] 32355#0: OS: Darwin 21.6.0 -2026/09/20 16:24:09 [notice] 32355#0: hw.ncpu: 4 -2026/09/20 16:24:09 [notice] 32355#0: net.inet.tcp.sendspace: 131072 -2026/09/20 16:24:09 [notice] 32355#0: kern.ipc.somaxconn: 128 -2026/09/20 16:24:09 [notice] 32355#0: hw.cachelinesize: 64 -2026/09/20 16:24:09 [notice] 32355#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 16:24:09 [notice] 32356#0: start worker processes -2026/09/20 16:24:09 [notice] 32356#0: start worker process 32357 -2026/09/20 16:24:09 [notice] 32356#0: start cache manager process 32358 -2026/09/20 16:24:09 [notice] 32356#0: start cache loader process 32359 -2026/09/20 16:24:09 [notice] 32358#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:24:09 [notice] 32356#0: signal 23 (SIGIO) received from 32358 -2026/09/20 16:24:09 [notice] 32357#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:24:09 [notice] 32356#0: signal 23 (SIGIO) received from 32357 -2026/09/20 16:24:09 [notice] 32359#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:24:10 [info] 32357#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:10 [info] 32357#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:10 [info] 32357#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:10 [info] 32357#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:10 [info] 32357#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:10 [info] 32357#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:10 [warn] 32357#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:24:10 [error] 32357#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62328" -2026/09/20 16:24:10 [alert] 32357#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62328" -2026/09/20 16:24:10 [info] 32357#0: *8 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62329/@v/list", host: "127.0.0.1:62328" -2026/09/20 16:24:10 [alert] 32357#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62329/@v/list", host: "127.0.0.1:62328" -2026/09/20 16:24:11 [warn] 32357#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 16:24:11 [info] 32357#0: *10 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62329/@v/list", host: "127.0.0.1:62328" -2026/09/20 16:24:11 [alert] 32357#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62329/@v/list", host: "127.0.0.1:62328" -2026/09/20 16:24:11 [info] 32357#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:11 [info] 32357#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:12 [info] 32357#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:13 [info] 32357#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:13 [info] 32357#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:13 [info] 32357#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:13 [warn] 32357#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 16:24:13 [alert] 32357#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:62328" -2026/09/20 16:24:14 [notice] 32357#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 16:24:14 [info] 32357#0: *22 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:14 [info] 32357#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:14 [info] 32357#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:14 [warn] 32357#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:24:14 [error] 32357#0: *25 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62328" -2026/09/20 16:24:14 [alert] 32357#0: *25 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62328" -2026/09/20 16:24:14 [notice] 32357#0: [cache] 保存上游 route=npm base=http://127.0.0.1:62329 enabled=true active=true -2026/09/20 16:24:14 [info] 32357#0: *26 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:15 [info] 32357#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:15 [info] 32357#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:24:15 [notice] 32514#0: signal process started -2026/09/20 16:24:15 [notice] 32356#0: signal 3 (SIGQUIT) received from 32514, shutting down -2026/09/20 16:24:15 [notice] 32357#0: gracefully shutting down -2026/09/20 16:24:15 [notice] 32356#0: signal 23 (SIGIO) received from 32357 -2026/09/20 16:24:15 [notice] 32357#0: exiting -2026/09/20 16:24:15 [notice] 32356#0: signal 23 (SIGIO) received from 32359 -2026/09/20 16:24:15 [notice] 32359#0: exiting -2026/09/20 16:24:15 [notice] 32358#0: exiting -2026/09/20 16:24:15 [notice] 32356#0: signal 23 (SIGIO) received -2026/09/20 16:24:15 [info] 32357#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 16:24:15 [notice] 32356#0: signal 20 (SIGCHLD) received from 32358 -2026/09/20 16:24:15 [notice] 32356#0: cache loader process 32359 exited with code 0 -2026/09/20 16:24:15 [notice] 32356#0: cache manager process 32358 exited with code 0 -2026/09/20 16:24:15 [notice] 32356#0: signal 23 (SIGIO) received -2026/09/20 16:24:15 [notice] 32357#0: exit -2026/09/20 16:24:15 [notice] 32356#0: signal 23 (SIGIO) received -2026/09/20 16:24:15 [notice] 32356#0: signal 20 (SIGCHLD) received from 32357 -2026/09/20 16:24:15 [notice] 32356#0: worker process 32357 exited with code 0 -2026/09/20 16:24:15 [notice] 32356#0: exit -2026/09/20 16:29:22 [notice] 33000#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:29:22 [notice] 33000#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:29:23 [notice] 33008#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:29:23 [notice] 33008#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:29:23 [notice] 33009#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:29:23 [notice] 33009#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:29:23 [notice] 33009#0: using the "kqueue" event method -2026/09/20 16:29:23 [notice] 33009#0: nginx/1.30.4 -2026/09/20 16:29:23 [notice] 33009#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 16:29:23 [notice] 33009#0: OS: Darwin 21.6.0 -2026/09/20 16:29:23 [notice] 33009#0: hw.ncpu: 4 -2026/09/20 16:29:23 [notice] 33009#0: net.inet.tcp.sendspace: 131072 -2026/09/20 16:29:23 [notice] 33009#0: kern.ipc.somaxconn: 128 -2026/09/20 16:29:23 [notice] 33009#0: hw.cachelinesize: 64 -2026/09/20 16:29:23 [notice] 33009#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 16:29:23 [notice] 33010#0: start worker processes -2026/09/20 16:29:23 [notice] 33010#0: start worker process 33011 -2026/09/20 16:29:23 [notice] 33010#0: start cache manager process 33012 -2026/09/20 16:29:23 [notice] 33010#0: start cache loader process 33013 -2026/09/20 16:29:23 [notice] 33012#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:29:23 [notice] 33010#0: signal 23 (SIGIO) received from 33012 -2026/09/20 16:29:23 [notice] 33011#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:29:23 [notice] 33010#0: signal 23 (SIGIO) received from 33011 -2026/09/20 16:29:23 [notice] 33013#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:29:24 [info] 33011#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [info] 33011#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [info] 33011#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [info] 33011#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [info] 33011#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [info] 33011#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [warn] 33011#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:29:24 [error] 33011#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62582" -2026/09/20 16:29:24 [alert] 33011#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62582" -2026/09/20 16:29:24 [info] 33011#0: *8 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62583/@v/list", host: "127.0.0.1:62582" -2026/09/20 16:29:24 [alert] 33011#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62583/@v/list", host: "127.0.0.1:62582" -2026/09/20 16:29:24 [warn] 33011#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 16:29:24 [info] 33011#0: *10 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62583/@v/list", host: "127.0.0.1:62582" -2026/09/20 16:29:24 [alert] 33011#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:62583/@v/list", host: "127.0.0.1:62582" -2026/09/20 16:29:24 [info] 33011#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:24 [info] 33011#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:27 [info] 33011#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:28 [info] 33011#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:29 [info] 33011#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:29 [info] 33011#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:29 [warn] 33011#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 16:29:29 [alert] 33011#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:62582" -2026/09/20 16:29:29 [notice] 33011#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 16:29:29 [info] 33011#0: *22 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:29 [info] 33011#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:29 [info] 33011#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:30 [warn] 33011#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:29:30 [error] 33011#0: *25 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62582" -2026/09/20 16:29:30 [alert] 33011#0: *25 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:62582" -2026/09/20 16:29:30 [notice] 33011#0: [cache] 保存上游 route=npm base=http://127.0.0.1:62583 enabled=true active=true -2026/09/20 16:29:30 [info] 33011#0: *26 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:31 [info] 33011#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:31 [info] 33011#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:29:31 [notice] 33169#0: signal process started -2026/09/20 16:29:31 [notice] 33010#0: signal 3 (SIGQUIT) received from 33169, shutting down -2026/09/20 16:29:31 [notice] 33011#0: gracefully shutting down -2026/09/20 16:29:31 [notice] 33010#0: signal 23 (SIGIO) received -2026/09/20 16:29:31 [notice] 33011#0: exiting -2026/09/20 16:29:31 [notice] 33013#0: exiting -2026/09/20 16:29:31 [notice] 33012#0: exiting -2026/09/20 16:29:31 [notice] 33010#0: signal 23 (SIGIO) received from 33012 -2026/09/20 16:29:31 [info] 33011#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 16:29:31 [notice] 33010#0: signal 20 (SIGCHLD) received from 33012 -2026/09/20 16:29:31 [notice] 33010#0: cache manager process 33012 exited with code 0 -2026/09/20 16:29:31 [notice] 33010#0: signal 23 (SIGIO) received -2026/09/20 16:29:31 [notice] 33010#0: signal 20 (SIGCHLD) received from 33013 -2026/09/20 16:29:31 [notice] 33010#0: cache loader process 33013 exited with code 0 -2026/09/20 16:29:31 [notice] 33010#0: signal 23 (SIGIO) received -2026/09/20 16:29:31 [notice] 33011#0: exit -2026/09/20 16:29:31 [notice] 33010#0: signal 23 (SIGIO) received -2026/09/20 16:29:31 [notice] 33010#0: signal 20 (SIGCHLD) received from 33011 -2026/09/20 16:29:31 [notice] 33010#0: worker process 33011 exited with code 0 -2026/09/20 16:29:31 [notice] 33010#0: exit -2026/09/20 16:59:21 [notice] 39310#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:59:21 [notice] 39310#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:59:22 [notice] 39318#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:59:22 [notice] 39318#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:59:22 [notice] 39319#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 16:59:22 [notice] 39319#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 16:59:22 [notice] 39319#0: using the "kqueue" event method -2026/09/20 16:59:22 [notice] 39319#0: nginx/1.30.4 -2026/09/20 16:59:22 [notice] 39319#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 16:59:22 [notice] 39319#0: OS: Darwin 21.6.0 -2026/09/20 16:59:22 [notice] 39319#0: hw.ncpu: 4 -2026/09/20 16:59:22 [notice] 39319#0: net.inet.tcp.sendspace: 131072 -2026/09/20 16:59:22 [notice] 39319#0: kern.ipc.somaxconn: 128 -2026/09/20 16:59:22 [notice] 39319#0: hw.cachelinesize: 64 -2026/09/20 16:59:22 [notice] 39319#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 16:59:22 [notice] 39320#0: start worker processes -2026/09/20 16:59:22 [notice] 39320#0: start worker process 39321 -2026/09/20 16:59:22 [notice] 39320#0: start cache manager process 39322 -2026/09/20 16:59:22 [notice] 39320#0: start cache loader process 39323 -2026/09/20 16:59:22 [notice] 39322#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:59:22 [notice] 39320#0: signal 23 (SIGIO) received from 39322 -2026/09/20 16:59:22 [notice] 39321#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:59:22 [notice] 39320#0: signal 23 (SIGIO) received from 39321 -2026/09/20 16:59:22 [notice] 39320#0: signal 23 (SIGIO) received -2026/09/20 16:59:22 [notice] 39323#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 16:59:23 [info] 39321#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:23 [info] 39321#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:23 [info] 39321#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:23 [info] 39321#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:23 [info] 39321#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:23 [info] 39321#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:23 [warn] 39321#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:59:23 [error] 39321#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:63819" -2026/09/20 16:59:23 [alert] 39321#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:63819" -2026/09/20 16:59:23 [info] 39321#0: *8 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:63820/@v/list", host: "127.0.0.1:63819" -2026/09/20 16:59:23 [alert] 39321#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:63820/@v/list", host: "127.0.0.1:63819" -2026/09/20 16:59:23 [warn] 39321#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 16:59:23 [info] 39321#0: *10 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:63820/@v/list", host: "127.0.0.1:63819" -2026/09/20 16:59:23 [alert] 39321#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:63820/@v/list", host: "127.0.0.1:63819" -2026/09/20 16:59:23 [info] 39321#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:24 [info] 39321#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:26 [info] 39321#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:26 [info] 39321#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:27 [info] 39321#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:27 [info] 39321#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:27 [warn] 39321#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 16:59:27 [alert] 39321#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:63819" -2026/09/20 16:59:27 [notice] 39321#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 16:59:27 [info] 39321#0: *22 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:27 [info] 39321#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:27 [info] 39321#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:28 [warn] 39321#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 16:59:28 [error] 39321#0: *25 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:63819" -2026/09/20 16:59:28 [alert] 39321#0: *25 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:63819" -2026/09/20 16:59:28 [notice] 39321#0: [cache] 保存上游 route=npm base=http://127.0.0.1:63820 enabled=true active=true -2026/09/20 16:59:28 [info] 39321#0: *26 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:28 [info] 39321#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:28 [info] 39321#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 16:59:28 [notice] 39479#0: signal process started -2026/09/20 16:59:28 [notice] 39320#0: signal 3 (SIGQUIT) received from 39479, shutting down -2026/09/20 16:59:28 [notice] 39321#0: gracefully shutting down -2026/09/20 16:59:28 [notice] 39321#0: exiting -2026/09/20 16:59:28 [notice] 39320#0: signal 23 (SIGIO) received -2026/09/20 16:59:28 [notice] 39323#0: exiting -2026/09/20 16:59:28 [notice] 39322#0: exiting -2026/09/20 16:59:28 [notice] 39320#0: signal 23 (SIGIO) received -2026/09/20 16:59:28 [notice] 39320#0: signal 23 (SIGIO) received -2026/09/20 16:59:28 [info] 39321#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 16:59:28 [notice] 39321#0: exit -2026/09/20 16:59:28 [notice] 39320#0: signal 20 (SIGCHLD) received from 39323 -2026/09/20 16:59:28 [notice] 39320#0: cache loader process 39323 exited with code 0 -2026/09/20 16:59:28 [notice] 39320#0: signal 23 (SIGIO) received -2026/09/20 16:59:28 [notice] 39320#0: signal 20 (SIGCHLD) received from 39322 -2026/09/20 16:59:28 [notice] 39320#0: cache manager process 39322 exited with code 0 -2026/09/20 16:59:28 [notice] 39320#0: signal 23 (SIGIO) received -2026/09/20 16:59:28 [notice] 39320#0: signal 23 (SIGIO) received from 39321 -2026/09/20 16:59:28 [notice] 39320#0: signal 20 (SIGCHLD) received from 39321 -2026/09/20 16:59:28 [notice] 39320#0: worker process 39321 exited with code 0 -2026/09/20 16:59:28 [notice] 39320#0: exit -2026/09/20 17:58:12 [notice] 50884#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 17:58:12 [notice] 50884#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 17:58:12 [notice] 50892#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 17:58:12 [notice] 50892#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 17:58:12 [notice] 50893#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 17:58:12 [notice] 50893#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 17:58:12 [notice] 50893#0: using the "kqueue" event method -2026/09/20 17:58:12 [notice] 50893#0: nginx/1.30.4 -2026/09/20 17:58:12 [notice] 50893#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 17:58:12 [notice] 50893#0: OS: Darwin 21.6.0 -2026/09/20 17:58:12 [notice] 50893#0: hw.ncpu: 4 -2026/09/20 17:58:12 [notice] 50893#0: net.inet.tcp.sendspace: 131072 -2026/09/20 17:58:12 [notice] 50893#0: kern.ipc.somaxconn: 128 -2026/09/20 17:58:12 [notice] 50893#0: hw.cachelinesize: 64 -2026/09/20 17:58:12 [notice] 50893#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 17:58:12 [notice] 50894#0: start worker processes -2026/09/20 17:58:12 [notice] 50894#0: start worker process 50895 -2026/09/20 17:58:12 [notice] 50894#0: start cache manager process 50896 -2026/09/20 17:58:12 [notice] 50894#0: start cache loader process 50897 -2026/09/20 17:58:12 [notice] 50896#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 17:58:12 [notice] 50894#0: signal 23 (SIGIO) received from 50896 -2026/09/20 17:58:12 [notice] 50895#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 17:58:12 [notice] 50894#0: signal 23 (SIGIO) received from 50895 -2026/09/20 17:58:12 [notice] 50894#0: signal 23 (SIGIO) received -2026/09/20 17:58:12 [notice] 50897#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 17:58:13 [info] 50895#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:13 [info] 50895#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:14 [info] 50895#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:14 [info] 50895#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:14 [info] 50895#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:14 [info] 50895#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:14 [warn] 50895#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 17:58:14 [error] 50895#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:49630" -2026/09/20 17:58:14 [alert] 50895#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:49630" -2026/09/20 17:58:14 [info] 50895#0: *8 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:49631/@v/list", host: "127.0.0.1:49630" -2026/09/20 17:58:14 [alert] 50895#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:49631/@v/list", host: "127.0.0.1:49630" -2026/09/20 17:58:14 [warn] 50895#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 17:58:14 [info] 50895#0: *10 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:49631/@v/list", host: "127.0.0.1:49630" -2026/09/20 17:58:14 [alert] 50895#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:49631/@v/list", host: "127.0.0.1:49630" -2026/09/20 17:58:14 [info] 50895#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:14 [info] 50895#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:15 [info] 50895#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:16 [info] 50895#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:16 [info] 50895#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:16 [info] 50895#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:16 [warn] 50895#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 17:58:16 [alert] 50895#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:49630" -2026/09/20 17:58:16 [notice] 50895#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 17:58:16 [info] 50895#0: *22 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:16 [info] 50895#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:16 [info] 50895#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:17 [warn] 50895#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 17:58:17 [error] 50895#0: *25 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:49630" -2026/09/20 17:58:17 [alert] 50895#0: *25 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:49630" -2026/09/20 17:58:17 [notice] 50895#0: [cache] 保存上游 route=npm base=http://127.0.0.1:49631 enabled=true active=true -2026/09/20 17:58:17 [info] 50895#0: *26 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:17 [info] 50895#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:17 [info] 50895#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:18 [notice] 50895#0: [cache] 测试上游 route=go -2026/09/20 17:58:18 [info] 50895#0: *30 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 17:58:19 [notice] 51078#0: signal process started -2026/09/20 17:58:19 [notice] 50894#0: signal 3 (SIGQUIT) received from 51078, shutting down -2026/09/20 17:58:19 [notice] 50895#0: gracefully shutting down -2026/09/20 17:58:19 [notice] 50894#0: signal 23 (SIGIO) received from 50895 -2026/09/20 17:58:19 [notice] 50897#0: exiting -2026/09/20 17:58:19 [notice] 50896#0: exiting -2026/09/20 17:58:19 [notice] 50895#0: exiting -2026/09/20 17:58:19 [notice] 50894#0: signal 23 (SIGIO) received -2026/09/20 17:58:19 [info] 50895#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 17:58:19 [notice] 50894#0: signal 20 (SIGCHLD) received from 50897 -2026/09/20 17:58:19 [notice] 50894#0: cache loader process 50897 exited with code 0 -2026/09/20 17:58:19 [notice] 50894#0: signal 23 (SIGIO) received -2026/09/20 17:58:19 [notice] 50895#0: exit -2026/09/20 17:58:19 [notice] 50894#0: signal 20 (SIGCHLD) received from 50896 -2026/09/20 17:58:19 [notice] 50894#0: cache manager process 50896 exited with code 0 -2026/09/20 17:58:19 [notice] 50894#0: signal 23 (SIGIO) received -2026/09/20 17:58:19 [notice] 50894#0: signal 23 (SIGIO) received from 50895 -2026/09/20 17:58:19 [notice] 50894#0: signal 20 (SIGCHLD) received from 50895 -2026/09/20 17:58:19 [notice] 50894#0: worker process 50895 exited with code 0 -2026/09/20 17:58:19 [notice] 50894#0: exit -2026/09/20 18:29:05 [notice] 56260#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:29:05 [notice] 56260#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:29:05 [notice] 56260#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 18:29:06 [notice] 56268#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:29:06 [notice] 56268#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:29:06 [notice] 56268#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 18:29:06 [notice] 56269#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:29:06 [notice] 56269#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:29:06 [notice] 56269#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 18:29:06 [notice] 56269#0: using the "kqueue" event method -2026/09/20 18:29:06 [notice] 56269#0: nginx/1.30.4 -2026/09/20 18:29:06 [notice] 56269#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 18:29:06 [notice] 56269#0: OS: Darwin 21.6.0 -2026/09/20 18:29:06 [notice] 56269#0: hw.ncpu: 4 -2026/09/20 18:29:06 [notice] 56269#0: net.inet.tcp.sendspace: 131072 -2026/09/20 18:29:06 [notice] 56269#0: kern.ipc.somaxconn: 128 -2026/09/20 18:29:06 [notice] 56269#0: hw.cachelinesize: 64 -2026/09/20 18:29:06 [notice] 56269#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 18:29:06 [notice] 56270#0: start worker processes -2026/09/20 18:29:06 [notice] 56270#0: start worker process 56271 -2026/09/20 18:29:06 [notice] 56270#0: start cache manager process 56272 -2026/09/20 18:29:06 [notice] 56270#0: start cache loader process 56273 -2026/09/20 18:29:06 [notice] 56271#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 18:29:06 [notice] 56270#0: signal 23 (SIGIO) received from 56271 -2026/09/20 18:29:06 [notice] 56272#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 18:29:06 [notice] 56270#0: signal 23 (SIGIO) received from 56272 -2026/09/20 18:29:06 [notice] 56273#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 18:29:07 [info] 56271#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:07 [info] 56271#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:07 [info] 56271#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:07 [info] 56271#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:07 [info] 56271#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:07 [info] 56271#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:07 [warn] 56271#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 18:29:07 [error] 56271#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:50857" -2026/09/20 18:29:07 [alert] 56271#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:50857" -2026/09/20 18:29:07 [info] 56271#0: *8 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:50858/@v/list", host: "127.0.0.1:50857" -2026/09/20 18:29:08 [warn] 56271#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 18:29:08 [alert] 56271#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:50858/@v/list", host: "127.0.0.1:50857" -2026/09/20 18:29:08 [info] 56271#0: *10 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:50858/@v/list", host: "127.0.0.1:50857" -2026/09/20 18:29:08 [alert] 56271#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:50858/@v/list", host: "127.0.0.1:50857" -2026/09/20 18:29:08 [info] 56271#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:08 [info] 56271#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:10 [info] 56271#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:10 [info] 56271#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:11 [info] 56271#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:11 [info] 56271#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:11 [warn] 56271#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 18:29:11 [alert] 56271#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:50857" -2026/09/20 18:29:11 [notice] 56271#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 18:29:11 [info] 56271#0: *22 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:11 [info] 56271#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:11 [info] 56271#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:11 [warn] 56271#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 18:29:11 [error] 56271#0: *25 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:50857" -2026/09/20 18:29:11 [alert] 56271#0: *25 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:50857" -2026/09/20 18:29:11 [notice] 56271#0: [cache] 保存上游 route=npm base=http://127.0.0.1:50858 enabled=true active=true -2026/09/20 18:29:11 [info] 56271#0: *26 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:12 [info] 56271#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:12 [info] 56271#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:12 [notice] 56271#0: [cache] 测试上游 route=go -2026/09/20 18:29:12 [info] 56271#0: *30 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:13 [notice] 56456#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:29:13 [notice] 56456#0: signal process started -2026/09/20 18:29:13 [notice] 56270#0: signal 3 (SIGQUIT) received from 56456, shutting down -2026/09/20 18:29:13 [notice] 56271#0: gracefully shutting down -2026/09/20 18:29:13 [notice] 56271#0: exiting -2026/09/20 18:29:13 [info] 56271#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 18:29:13 [notice] 56271#0: exit -2026/09/20 18:29:13 [notice] 56273#0: exiting -2026/09/20 18:29:13 [notice] 56272#0: exiting -2026/09/20 18:29:13 [notice] 56270#0: signal 23 (SIGIO) received -2026/09/20 18:29:13 [notice] 56270#0: signal 20 (SIGCHLD) received from 56273 -2026/09/20 18:29:13 [notice] 56270#0: cache loader process 56273 exited with code 0 -2026/09/20 18:29:13 [notice] 56270#0: signal 23 (SIGIO) received -2026/09/20 18:29:13 [notice] 56270#0: signal 23 (SIGIO) received -2026/09/20 18:29:13 [notice] 56270#0: signal 20 (SIGCHLD) received from 56271 -2026/09/20 18:29:13 [notice] 56270#0: cache manager process 56272 exited with code 0 -2026/09/20 18:29:13 [notice] 56270#0: worker process 56271 exited with code 0 -2026/09/20 18:29:13 [notice] 56270#0: exit -2026/09/20 21:03:02 [notice] 64920#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:03:02 [notice] 64920#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:03:02 [notice] 64920#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 21:03:02 [notice] 64928#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:03:02 [notice] 64928#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:03:02 [notice] 64928#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 21:03:02 [notice] 64929#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:03:02 [notice] 64929#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:03:02 [notice] 64929#0: [ipass] 已装载 5 个账号 Key、0 个 LLM 上游、0 个 API 代理、2 个缓存上游 -2026/09/20 21:03:02 [notice] 64929#0: using the "kqueue" event method -2026/09/20 21:03:02 [notice] 64929#0: nginx/1.30.4 -2026/09/20 21:03:02 [notice] 64929#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 21:03:02 [notice] 64929#0: OS: Darwin 21.6.0 -2026/09/20 21:03:02 [notice] 64929#0: hw.ncpu: 4 -2026/09/20 21:03:02 [notice] 64929#0: net.inet.tcp.sendspace: 131072 -2026/09/20 21:03:02 [notice] 64929#0: kern.ipc.somaxconn: 128 -2026/09/20 21:03:02 [notice] 64929#0: hw.cachelinesize: 64 -2026/09/20 21:03:02 [notice] 64929#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 21:03:02 [notice] 64930#0: start worker processes -2026/09/20 21:03:02 [notice] 64930#0: start worker process 64931 -2026/09/20 21:03:02 [notice] 64930#0: start cache manager process 64932 -2026/09/20 21:03:02 [notice] 64930#0: start cache loader process 64933 -2026/09/20 21:03:02 [notice] 64932#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 21:03:02 [notice] 64930#0: signal 23 (SIGIO) received from 64932 -2026/09/20 21:03:02 [notice] 64931#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 21:03:02 [notice] 64930#0: signal 23 (SIGIO) received from 64931 -2026/09/20 21:03:02 [notice] 64930#0: signal 23 (SIGIO) received from 64931 -2026/09/20 21:03:02 [notice] 64933#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json) -2026/09/20 21:03:03 [info] 64931#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:03 [info] 64931#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:04 [info] 64931#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:04 [info] 64931#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:04 [info] 64931#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:04 [info] 64931#0: *6 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:04 [warn] 64931#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 21:03:04 [error] 64931#0: *7 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:54166" -2026/09/20 21:03:04 [alert] 64931#0: *7 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:54166" -2026/09/20 21:03:04 [info] 64931#0: *8 kevent() reported that client prematurely closed connection while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:54167/@v/list", host: "127.0.0.1:54166" -2026/09/20 21:03:04 [alert] 64931#0: *8 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:54167/@v/list", host: "127.0.0.1:54166" -2026/09/20 21:03:04 [warn] 64931#0: [cache] API Key 校验失败 route=go client=127.0.0.1 -2026/09/20 21:03:04 [info] 64931#0: *10 kevent() reported that client prematurely closed connection while sending request to upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:54167/@v/list", host: "127.0.0.1:54166" -2026/09/20 21:03:04 [alert] 64931#0: *10 header already sent while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", upstream: "http://127.0.0.1:54167/@v/list", host: "127.0.0.1:54166" -2026/09/20 21:03:04 [info] 64931#0: *12 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:04 [info] 64931#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:06 [info] 64931#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:06 [info] 64931#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:07 [info] 64931#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:07 [info] 64931#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:07 [warn] 64931#0: [llm] carol 触发每分钟限流 3/2,本请求 429 -2026/09/20 21:03:07 [alert] 64931#0: *21 header already sent, client: 127.0.0.1, server: _, request: "GET /go/@v/list HTTP/1.1", host: "127.0.0.1:54166" -2026/09/20 21:03:07 [notice] 64931#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 21:03:07 [info] 64931#0: *22 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:07 [info] 64931#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:07 [info] 64931#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:08 [warn] 64931#0: [cache] 路由 npm 未配置上游,拒绝 /npm/foo(client=127.0.0.1) -2026/09/20 21:03:08 [error] 64931#0: *25 invalid URL prefix in "", client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:54166" -2026/09/20 21:03:08 [alert] 64931#0: *25 header already sent, client: 127.0.0.1, server: _, request: "GET /npm/foo HTTP/1.1", host: "127.0.0.1:54166" -2026/09/20 21:03:08 [notice] 64931#0: [cache] 保存上游 route=npm base=http://127.0.0.1:54167 enabled=true active=true -2026/09/20 21:03:08 [info] 64931#0: *26 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:08 [info] 64931#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:08 [info] 64931#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:09 [notice] 64931#0: [cache] 测试上游 route=go -2026/09/20 21:03:09 [info] 64931#0: *30 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:03:10 [notice] 65115#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:03:10 [notice] 65115#0: signal process started -2026/09/20 21:03:10 [notice] 64930#0: signal 3 (SIGQUIT) received from 65115, shutting down -2026/09/20 21:03:10 [notice] 64931#0: gracefully shutting down -2026/09/20 21:03:10 [notice] 64930#0: signal 23 (SIGIO) received from 64931 -2026/09/20 21:03:10 [notice] 64932#0: exiting -2026/09/20 21:03:10 [notice] 64931#0: exiting -2026/09/20 21:03:10 [notice] 64933#0: exiting -2026/09/20 21:03:10 [notice] 64930#0: signal 23 (SIGIO) received -2026/09/20 21:03:10 [info] 64931#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-auth/data/llm-stats.json -2026/09/20 21:03:10 [notice] 64931#0: exit -2026/09/20 21:03:10 [notice] 64930#0: signal 20 (SIGCHLD) received from 64932 -2026/09/20 21:03:10 [notice] 64930#0: cache manager process 64932 exited with code 0 -2026/09/20 21:03:10 [notice] 64930#0: signal 23 (SIGIO) received -2026/09/20 21:03:10 [notice] 64930#0: signal 20 (SIGCHLD) received from 64933 -2026/09/20 21:03:10 [notice] 64930#0: cache loader process 64933 exited with code 0 -2026/09/20 21:03:10 [notice] 64930#0: signal 23 (SIGIO) received -2026/09/20 21:03:10 [notice] 64930#0: signal 20 (SIGCHLD) received from 64931 -2026/09/20 21:03:10 [notice] 64930#0: worker process 64931 exited with code 0 -2026/09/20 21:03:10 [notice] 64930#0: signal 23 (SIGIO) received from 64931 -2026/09/20 21:03:10 [notice] 64930#0: exit diff --git a/tests/cache/.run-auth/logs/h7.body b/tests/cache/.run-auth/logs/h7.body deleted file mode 100644 index 18dd315..0000000 --- a/tests/cache/.run-auth/logs/h7.body +++ /dev/null @@ -1 +0,0 @@ -ECHO:GET /@v/list?x=1 \ No newline at end of file diff --git a/tests/cache/.run-auth/logs/h7.head b/tests/cache/.run-auth/logs/h7.head deleted file mode 100644 index a5cbcc1..0000000 --- a/tests/cache/.run-auth/logs/h7.head +++ /dev/null @@ -1,11 +0,0 @@ -HTTP/1.1 200 OK -Server: nginx/1.30.4 -Date: Sun, 20 Sep 2026 13:03:04 GMT -Content-Type: application/octet-stream -Content-Length: 21 -Connection: keep-alive -ETag: "21" -Cache-Control: public, max-age=3600 -X-Cache-Status: MISS -X-Cache-Eco: go - diff --git a/tests/cache/.run-auth/logs/h9.head b/tests/cache/.run-auth/logs/h9.head deleted file mode 100644 index 9eda142..0000000 --- a/tests/cache/.run-auth/logs/h9.head +++ /dev/null @@ -1,11 +0,0 @@ -HTTP/1.1 200 OK -Server: nginx/1.30.4 -Date: Sun, 20 Sep 2026 13:03:06 GMT -Content-Type: application/octet-stream -Content-Length: 21 -Connection: keep-alive -ETag: "21" -Cache-Control: public, max-age=3600 -X-Cache-Status: MISS -X-Cache-Eco: cargo - diff --git a/tests/cache/.run-auth/logs/login.body b/tests/cache/.run-auth/logs/login.body deleted file mode 100644 index 84aaba7..0000000 --- a/tests/cache/.run-auth/logs/login.body +++ /dev/null @@ -1 +0,0 @@ -{"code":200,"data":{"allowed_models":[],"api_key":"sk_admin_0000000000000000000000000000000000000000000000ab","api_key_masked":"sk_admin...00ab","created_at":1757500000,"created_at_text":"2025-09-10 18:26:40","display_name":"","llm_daily":0,"llm_rpm":60,"name":"admin","role":"admin","temp_grant":false,"temp_grant_left":0,"temp_grant_left_text":"","temp_grant_until":0,"temp_grant_until_text":"","username":"admin"},"msg":"登录成功"} \ No newline at end of file diff --git a/tests/cache/.run-auth/logs/probe.body b/tests/cache/.run-auth/logs/probe.body deleted file mode 100644 index 5213d5f..0000000 --- a/tests/cache/.run-auth/logs/probe.body +++ /dev/null @@ -1 +0,0 @@ -ECHO:GET / \ No newline at end of file diff --git a/tests/cache/.run-auth/logs/upstream.log b/tests/cache/.run-auth/logs/upstream.log deleted file mode 100644 index 9c0d8d2..0000000 --- a/tests/cache/.run-auth/logs/upstream.log +++ /dev/null @@ -1 +0,0 @@ -GET / auth=Bearer upstream-secret-token diff --git a/tests/cache/.run-auth/logs/upstream.out b/tests/cache/.run-auth/logs/upstream.out deleted file mode 100644 index f7f0759..0000000 --- a/tests/cache/.run-auth/logs/upstream.out +++ /dev/null @@ -1 +0,0 @@ -mock upstream on 54167 diff --git a/tests/cache/.run-auth/www b/tests/cache/.run-auth/www deleted file mode 120000 index 898e1cb..0000000 --- a/tests/cache/.run-auth/www +++ /dev/null @@ -1 +0,0 @@ -/Users/rong/Desktop/cache/www \ No newline at end of file diff --git a/tests/cache/.run-cap/.cookies b/tests/cache/.run-cap/.cookies deleted file mode 100644 index 6ea7968..0000000 --- a/tests/cache/.run-cap/.cookies +++ /dev/null @@ -1,5 +0,0 @@ -# Netscape HTTP Cookie File -# https://curl.se/docs/http-cookies.html -# This file was generated by libcurl! Edit at your own risk. - -#HttpOnly_127.0.0.1 FALSE / FALSE 1789938349 ipass_admin_session 7a6458a49d2182c315cc0b20c812fcecd7587f7ce9b13c6f16031d35cd9d8309 diff --git a/tests/cache/.run-cap/.inv.json b/tests/cache/.run-cap/.inv.json deleted file mode 100644 index 9d29356..0000000 --- a/tests/cache/.run-cap/.inv.json +++ /dev/null @@ -1 +0,0 @@ -{"code":401,"msg":"未登录或会话已过期,请重新登录"} \ No newline at end of file diff --git a/tests/cache/.run-cap/.snap.json b/tests/cache/.run-cap/.snap.json deleted file mode 100644 index 99453d4..0000000 --- a/tests/cache/.run-cap/.snap.json +++ /dev/null @@ -1 +0,0 @@ -{"code":200,"data":{"active":2,"capacity":{"available":true,"reason":"","scanned":true,"totals":{"crit":0,"disk_bytes":0,"entries":4,"limit_bytes":429497778176,"pct":0.0,"used_bytes":630784,"warn":0,"zones":6},"zones":[{"bsize":4096,"cold":true,"dir":"/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/go","dir_exists":true,"disk_bytes":0,"disk_files":0,"disk_truncated":false,"eco":"go","entries":0,"entries_cap_est":873813,"inactive_secs":604800,"index_pressure":false,"keys_zone_bytes":104857600,"level":"ok","limit_bytes":53687091200,"loading":false,"pct":0.0,"routes":["go"],"used_bytes":0,"zone":"go"},{"bsize":4096,"cold":true,"dir":"/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/cargo","dir_exists":true,"disk_bytes":0,"disk_files":0,"disk_truncated":false,"eco":"cargo","entries":0,"entries_cap_est":873813,"inactive_secs":604800,"index_pressure":false,"keys_zone_bytes":104857600,"level":"ok","limit_bytes":53687091200,"loading":false,"pct":0.0,"routes":["cargo","cargo-dl"],"used_bytes":0,"zone":"cargo"},{"bsize":4096,"cold":true,"dir":"/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/pypi","dir_exists":true,"disk_bytes":0,"disk_files":0,"disk_truncated":false,"eco":"pypi","entries":0,"entries_cap_est":873813,"inactive_secs":604800,"index_pressure":false,"keys_zone_bytes":104857600,"level":"ok","limit_bytes":53687091200,"loading":false,"pct":0.0,"routes":["pypi","pypi-files"],"used_bytes":0,"zone":"pypi"},{"bsize":4096,"cold":true,"dir":"/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm","dir_exists":true,"disk_bytes":0,"disk_files":0,"disk_truncated":false,"eco":"npm","entries":4,"entries_cap_est":873813,"inactive_secs":3600,"index_pressure":false,"keys_zone_bytes":104857600,"level":"ok","limit_bytes":1048576,"loading":false,"pct":60.2,"routes":["npm"],"used_bytes":630784,"zone":"npm"},{"bsize":4096,"cold":true,"dir":"/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/maven","dir_exists":true,"disk_bytes":0,"disk_files":0,"disk_truncated":false,"eco":"maven","entries":0,"entries_cap_est":873813,"inactive_secs":604800,"index_pressure":false,"keys_zone_bytes":104857600,"level":"ok","limit_bytes":53687091200,"loading":false,"pct":0.0,"routes":["maven"],"used_bytes":0,"zone":"maven"},{"bsize":4096,"cold":true,"dir":"/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/docker","dir_exists":true,"disk_bytes":0,"disk_files":0,"disk_truncated":false,"eco":"docker","entries":0,"entries_cap_est":1747626,"inactive_secs":604800,"index_pressure":false,"keys_zone_bytes":209715200,"level":"ok","limit_bytes":214748364800,"loading":false,"pct":0.0,"routes":["docker"],"used_bytes":0,"zone":"docker"}]},"configured":2,"count":8,"entry":"http://127.0.0.1:18087","items":[{"active":false,"base":"","client":{"command":"export GOPROXY=http://127.0.0.1:18087/go","label":"Go module proxy"},"configured":false,"default_base":"https://proxy.golang.org","desc":"","eco":"go","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"","note":"","route":"go","updated_at":0,"uri":"/go/"},{"active":false,"base":"","client":{"command":"mkdir -p ~/.cargo && printf '[registries.crates-io]\\nindex = \"sparse+http://127.0.0.1:18087/cargo/\"\\n' >> ~/.cargo/config.toml","label":"crates.io 稀疏索引(包本体需另配 /cargo-dl/,见文档)"},"configured":false,"default_base":"https://index.crates.io","desc":"","eco":"cargo","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"","note":"","route":"cargo","updated_at":0,"uri":"/cargo/"},{"active":false,"base":"","client":{"command":"mkdir -p ~/.cargo && printf '[registries.crates-io]\\nindex = \"sparse+http://127.0.0.1:18087/cargo/\"\\n' >> ~/.cargo/config.toml","label":"crates.io 稀疏索引(包本体需另配 /cargo-dl/,见文档)"},"configured":false,"default_base":"https://static.crates.io","desc":"","eco":"cargo","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"","note":"","route":"cargo-dl","updated_at":0,"uri":"/cargo-dl/"},{"active":false,"base":"","client":{"command":"pip config set global.index-url http://127.0.0.1:18087/pypi/simple/","label":"PyPI 索引(包文件需另配 /pypi-files/,见文档)"},"configured":false,"default_base":"https://pypi.org","desc":"","eco":"pypi","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"","note":"","route":"pypi","updated_at":0,"uri":"/pypi/"},{"active":false,"base":"","client":{"command":"pip config set global.index-url http://127.0.0.1:18087/pypi/simple/","label":"PyPI 索引(包文件需另配 /pypi-files/,见文档)"},"configured":false,"default_base":"https://files.pythonhosted.org","desc":"","eco":"pypi","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"","note":"","route":"pypi-files","updated_at":0,"uri":"/pypi-files/"},{"active":true,"base":"http://127.0.0.1:18996","client":{"command":"npm config set registry http://127.0.0.1:18087/npm/","label":"npm registry"},"configured":true,"default_base":"https://registry.npmjs.org","desc":"假上游","eco":"npm","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"测试上游 npm","note":"","route":"npm","updated_at":1757500000,"uri":"/npm/"},{"active":true,"base":"http://127.0.0.1:18996","client":{"command":"ipasshttp://127.0.0.1:18087/mavencentral","label":"Maven Central 镜像(settings.xml 的 mirror)"},"configured":true,"default_base":"https://repo1.maven.org/maven2","desc":"假上游","eco":"maven","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"测试上游 maven","note":"","route":"maven","updated_at":1757500000,"uri":"/maven/"},{"active":false,"base":"","client":{"command":"sudo mkdir -p /etc/docker && printf '{\"registry-mirrors\":[\"http://127.0.0.1:18087\"]}' | sudo tee /etc/docker/daemon.json","label":"Docker Registry v2(需 TLS;仅 docker.io 官方镜像)"},"configured":false,"default_base":"https://registry-1.docker.io","desc":"","eco":"docker","enabled":true,"key_masked":"","key_set":false,"mode":"proxy","name":"","note":"","route":"docker","updated_at":0,"uri":"/v2/"}],"probe_prefix":"/_cache_test/","stats":{"by_ecosystem":{"cargo":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":0,"REVALIDATED":0,"STALE":0,"UPDATING":0},"docker":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":0,"REVALIDATED":0,"STALE":0,"UPDATING":0},"global":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":4,"REVALIDATED":0,"STALE":0,"UPDATING":0},"go":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":0,"REVALIDATED":0,"STALE":0,"UPDATING":0},"maven":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":0,"REVALIDATED":0,"STALE":0,"UPDATING":0},"npm":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":4,"REVALIDATED":0,"STALE":0,"UPDATING":0},"pypi":{"BYPASS":0,"EXPIRED":0,"HIT":0,"MISS":0,"REVALIDATED":0,"STALE":0,"UPDATING":0}},"now":1789909561},"variables":[{"desc":"上游完整 URL(proxy_pass)","name":"$cache_target"},{"desc":"上游 Host 头","name":"$cache_host"},{"desc":"管理台配的上游凭据原值(未配置为空)","name":"$cache_upstream_key"},{"desc":"实际下发的 Authorization:配了凭据就用它,没配就透传客户端值,但会剔除下游 sk_ 自身","name":"$cache_auth"},{"desc":"生态名","name":"$cache_eco"},{"desc":"缓存键(proxy_cache_key)= :[?args],含 query、不含任何下游凭据(全站共享)","name":"$cache_key"}]},"msg":"ok"} \ No newline at end of file diff --git a/tests/cache/.run-cap/cache/maven/0/23/e0e8d4befc6b8d7e9c4f4744d39a8230 b/tests/cache/.run-cap/cache/maven/0/23/e0e8d4befc6b8d7e9c4f4744d39a8230 deleted file mode 100644 index cb86c52c22d7df88b489231d707adf352b8c6666..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 205393 zcmeI)!D`xY90zbW7!1L82t9Ucq7F9#rLay~=teutWrrP7`J>&c22IO)8tjF}*aPf6 z_A*=4md>rm-S!b7)|f!F_4oaLw0Tt9T)sc~`u^tT*5c>QW&g*Q_4;?;J~wN%PY>Qa ze7w_5o<2{Kovp|J??2R!2XE78nqP%k8m|}ehY*U^H16!5U6*0Gw|UkM2ib1CTR+OO z)40=4qHdBzC&!KYFf6XZB8^_;%dlRVwENNNbvc>O;$FK`UAv9?%e)L}G`yTOtL>s; zD5^!bA9Z?Z(od6T(c$Z?Q9qc^$}lTiufl9xPSWZjt5@sp{_MJ_c9Ivx#rY^N&#V2< zM`b8m%W@I&X`?>K@^Kp7zR1;f`DhYatGzDf7im;no>dz*t3&k$$8f=|}pJexx6*&d;qM>WBKFeyAVnhx(y@ zs2}Qw`k{WPAL@ttp?=H<_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi zKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sMAK(LgfDiBi vKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AcD1-~+z^-_e;J diff --git a/tests/cache/.run-cap/cache/npm/5/f8/3f31dfdcfabc98bc16b76f4911374f85 b/tests/cache/.run-cap/cache/npm/5/f8/3f31dfdcfabc98bc16b76f4911374f85 deleted file mode 100644 index 4655bc71a23422ce09f8535578db4410db633984..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 205389 zcmeI)Pih)r90p)pC=|gvd}NtM9ktjp5CTm@0%=IV*>pLP4^10&&`GKEL|uiRpgl*A z&`Y#qOh|3B?5>YU#$^70n)rL)Cpu4R>&w??U*6r`+*tg)z3hFzzFFV+{Nr1#_UX~d zMF&53d-Pt=|mT|GWe#8zBvhDVE{V2=M z+MQMsx=9jFj~n$toPUV(G`z|e@n&Vx>W8ysIi5}1y;i5Xb{qBAc^T6%xSH-%`-MR) zsztXSI=wXMr^$IYdqO2gd~TU?&YhT`hH+HogL@{i4Y6nA_5)xoRX_K!}} z>fVKi@3-|HAHN^|KD<77eSByxnh)lK`CvYn59WjU@ON)CAI(Se(R?%?%}4XmeEidu z=Y`A|NT59LGo zP(G9oNq!+waf1Mi0rtXKcH^@zTeOEJgMw1-=BZ`aC>ua@$>dF{PFc>{g-F;L8bEf(eUxp zgHH0|Ws)51ZU4W2Q$6j!OT!|cr>*i!>-}n(&tt3o>S9yG_0jGT8}!HZR=s*M9-p_` z%_MY^B%Gbrs-w927*}a{ovq`|%B0x~=bK`-T(r8)c6sg8s&BF)reSowI4t)IqnMYA zPA{~(Y0^tO?JyXQYt`fBqKJ#4@is1|#Vjp1**;VEKOb-M@*r8BUtLbJ;<7yca#F;i zu`X6Io7bxSaW+lE-4k5ymrZ7|u|4W)d6kCz`l8(NFwC=0jcgi^y1nhe%iWGo&eHPU zg@^CA^&TI;AO1ePK6rh6XfB!$=7afQK9~>YgZc1xZ!{mxNAuBqG#|}J^U-|#)0O1| z`9MC9599;+Kt7NU=Y`SAX8OY*UNEFa6q^09m@AIrz`v3x8a%g1-;HP8?A1N}fh&=2$j{Xjp^5A*~5 zKtIqAcIRKykMtw`NI%k#^dtR9Khlr%BmGD}(vS3`?fJR&L;X-c)DQJT{ZK#D5A{R+ zP(RcU^+WwoKh%%;03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM zAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu!+2lxOV-~)Vs5AXp#zz6sM rAK(LgfDiBiKEMa~03YB3e1H$|0Y1P7_y8Z^1AKrF@Bu#X4}9PkK<1yM diff --git a/tests/cache/.run-cap/conf/cache-common.conf b/tests/cache/.run-cap/conf/cache-common.conf deleted file mode 100644 index 3852e97..0000000 --- a/tests/cache/.run-cap/conf/cache-common.conf +++ /dev/null @@ -1,131 +0,0 @@ -# ============================================================================ -# cache-common.conf —— 各生态 cache location 共用的缓存/代理指令 -# -# 由 conf/cache-routes.conf 在每个 location 内 `include`。 -# **每个 location 自己指定** `proxy_cache `(各生态分库),本文件只放公共项。 -# -# --------------------------------------------------------------------------- -# 与「静态上游版」相比的三处关键变化(配合 plugin-cache 的变量层) -# --------------------------------------------------------------------------- -# -# 1. `proxy_pass` 由字面量改为变量 `$cache_target`。上游地址与凭据因此来自 -# 共享内存(`cx:`,由管理台写),**改上游不用改 conf、不用 reload**。 -# 代价:变量式 proxy_pass 不推导 Host、不追加 query —— 两者都由模块算好, -# 所以这里必须显式 `proxy_set_header Host $cache_host`。 -# -# 2. `proxy_cache_key` 不再用 `$scheme$proxy_host$request_uri`(变量式下 -# `$proxy_host` 不可靠),改用模块给的 `$cache_key`,并**并入方法 + -# Accept**(下面的注释详述为什么这是正确性问题,不是调优)。 -# -# 3. 出站鉴权头改成三段式(`proxy_pass_request_headers on` + 抹掉三个 -# 网关 Key 携带位 + 用 `$cache_auth` 下发)——见文件下半部分。 -# -# ⚠️ 本文件**不得**使用 rewrite 模块的 `set` / `if`;生态判定由 plugin-cache -# 直读 `r->uri` 首段完成,不需要 `set $cache_ecosystem`。 -# (`return` / `rewrite` 本身可用:本机构建与 ipass 生产都带 --with-pcre, -# rewrite 模块在编。它们只在 cache-routes.conf 里用。) -# ============================================================================ - -# --------------------------------------------------------------------------- -# 缓存键 -# --------------------------------------------------------------------------- -# -# `$cache_key` = `:<完整 URI + query>`,**不含任何下游凭据** —— 缓存因此是 -# 「全站共享」的:同一个 go module / npm 包被 N 个账号拉,只回源一次。 -# 这正是制品缓存的价值所在;鉴权在 access 层(谁能读),数据在共享层复用。 -# 代价:任一有效 Key 都能命中别人拉过的制品 —— 对公开 registry 无碍, -# 若某路由指向**私有**上游,请理解这一点再启用。 -# -# ★ query 必须进键(由模块的 `$cache_key` 负责,本行不要再自己拼 `$args`): -# npm 的 `?write=true`、docker 的 `?ns=`、go 的 `?go-get=1`、cargo 的 -# `?archive=tar.gz` 都靠 query 区分实体。变量式 `proxy_pass` 下 `$uri` -# **不含** query,只按 `$uri` 做键会把 `/x?a` 与 `/x?b` 当成同一个资源, -# 把错的制品发给客户端(且第二次完全不回源,很难察觉)。 -# -# ★ 为什么要并入 `$request_method` 与 `$http_accept`(这是正确性,不是调优): -# -# · `$request_method`:Docker 客户端对同一 URL 先 `HEAD` 看清单存不存在、 -# 再 `GET` 取清单(registry 协议如此)。若两者共用一个键,一次 HEAD 的 -# **空体**响应会被缓存下来并回给随后的 GET,客户端拿到 0 字节清单直接失败。 -# -# · `$http_accept`:nginx 的 proxy_cache **不实现 Vary**(只能靠 -# proxy_ignore_headers 把 Vary 忽略掉,无法参与分档)。而上游同一 URL -# 会因为 Accept 返回**不同实体**: -# - npm:`application/vnd.npm.install-v1+json` → 精简元数据, -# 缺省 → 完整元数据; -# - PyPI simple:`application/vnd.pypi.simple.v1+json` → JSON, -# `text/html` → HTML; -# - Docker:清单 v2 / OCI / v1 的 mediaType 由 Accept 偏好决定。 -# 不并入 Accept 就会出现「发给 A 的实体被回给 B」的串档。代价是每个 -# Accept 变体一个桶(现代客户端取值收敛,实际只多一个桶)。 -# -# 注:刻意**不**再带 `$scheme`/`$proxy_host`:同一份制品无论客户端走 http -# 还是 https、走哪个域名进来,上游都是同一个,共享一份缓存更省盘也更省带宽。 -proxy_cache_key $cache_key$request_method$http_accept; - -# 只缓存完整 GET/HEAD(200/301/308);404 短缓存;206(range)/401 等不缓存,避免错片 -proxy_cache_methods GET HEAD; -proxy_cache_valid 200 301 308 1d; -proxy_cache_valid 404 1m; - -# 上游抖动时用陈旧副本兜底;并发同键只回源一次(防击穿) -proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; -proxy_cache_lock on; -proxy_cache_lock_age 10s; -proxy_cache_lock_timeout 30s; - -# 带 If-None-Match / If-Modified-Since 回源校验,省带宽 -proxy_cache_revalidate on; - -# 加速缓存:不被上游 no-cache/Set-Cookie/Vary 拦下(镜像型缓存的标准取舍) -# ⚠️ Vary 被忽略是**必须**的——见上面缓存键的说明,我们已用 Accept 显式分档; -# 留着 Vary 反而会让 nginx 拒绝缓存这些响应。 -proxy_ignore_headers Cache-Control Expires Set-Cookie Vary; - -proxy_http_version 1.1; - -# --------------------------------------------------------------------------- -# 出站请求头(镜像型:默认透传,但抹掉「本网关自己的 Key」) -# --------------------------------------------------------------------------- -# -# 与 LLM/API 代理的 allowlist 策略**刻意不同**:那些是无状态 API,客户端 -# 头没有语义;而镜像必须原样带上客户端的 `Accept` / `Range` / -# `If-None-Match` / `User-Agent`,否则 docker 的 mediaType 协商、range 取 -# 大 blob、条件请求全部失效。所以这里保持 `on`(nginx 默认),只在三个 -# 「网关 Key 的携带位」上显式抹空。 -# -# 为什么必须抹:`sk_` 是客户端访问**本网关**的身份(auth::downstream_key 认 -# Authorization / x-api-key / api-key 三处)。原样透传给上游 = 把网关钥匙 -# 递给上游厂商,对方可拿它反调本网关的其它路由刷额度。ipass 的 LLM 路径 -# 记过同一个坑(宽松厂商把这缺陷掩盖成 200,换严格厂商才暴露成 401)。 -proxy_pass_request_headers on; -proxy_set_header x-api-key ""; -proxy_set_header api-key ""; - -# 变量形式 proxy_pass 不会推导 Host,必须显式设(上游多为 Cloudflare 前端, -# Host 不符会被直接拒) -proxy_set_header Host $cache_host; - -# Authorization 三段式(由模块里的 `$cache_auth` 实现,语义见其文档): -# 配了上游凭据 → 下发配置值(管理员写全 `Bearer xxx` / `Basic yyy`) -# 没配 → 透传客户端值(Docker Hub 的短期 Bearer token 走这条) -# 客户端那把恰好是下行 sk_ → 丢弃 -proxy_set_header Authorization $cache_auth; - -proxy_set_header X-Real-IP $remote_addr; -proxy_set_header X-Forwarded-For $remote_addr; -proxy_set_header X-Forwarded-Proto $scheme; - -# registry 大 blob / 慢索引:放宽超时 -proxy_read_timeout 300s; -proxy_send_timeout 300s; - -# 变量式/直连上游为 HTTPS 时开 SNI;证书校验此阶段关闭(镜像多为公开源, -# 且目标主机来自管理台配置而非请求。要严格校验需另配 proxy_ssl_trusted_certificate) -proxy_ssl_server_name on; -proxy_ssl_protocols TLSv1.2 TLSv1.3; -proxy_ssl_verify off; - -# 观测:每次响应带回缓存命中状态(ipass plugin-cache 亦从同一变量记账) -add_header X-Cache-Status $upstream_cache_status always; -add_header X-Cache-Eco $cache_eco always; diff --git a/tests/cache/.run-cap/conf/cache-routes.conf b/tests/cache/.run-cap/conf/cache-routes.conf deleted file mode 100644 index 4c50106..0000000 --- a/tests/cache/.run-cap/conf/cache-routes.conf +++ /dev/null @@ -1,177 +0,0 @@ -# ============================================================================ -# cache-routes.conf —— 制品缓存网关的「server 内容」(在 server { } 里 include) -# -# 为什么单独成一个文件:这份路由要**两处复用**—— -# * 明文入口 conf/server.cache.conf(本机 e2e / 内网直连) -# * TLS 入口 conf/tls/*.conf(对外,Docker 镜像加速必须走 https) -# 路由、鉴权开关、探针、管理台只写一份,两个入口不会漂。 -# -# 路由约定:/<前缀>/ => <该 route 配置的上游>/ -# /go/ Go module proxy → 默认 proxy.golang.org -# /cargo/ crates.io 稀疏索引 → 默认 index.crates.io -# /cargo-dl/ .crate 包本体 → 默认 static.crates.io -# /pypi/ PyPI 简单索引/JSON → 默认 pypi.org -# /pypi-files/ wheel/sdist 包文件 → 默认 files.pythonhosted.org -# /npm/ npm registry → 默认 registry.npmjs.org -# /maven/ Maven Central → 默认 repo1.maven.org/maven2 -# /v2/ Docker Registry v2 → 默认 registry-1.docker.io -# -# ⚠️ 上游地址**不写在本文件里**:全部来自 plugin-cache 的上游表(管理台 -# 「制品缓存」页,落盘 data/cache-upstreams.json + 写穿共享内存)。 -# 本文件只出现变量名。改上游 = 改管理台,不需要动 conf、不需要 reload。 -# -# ⚠️ 每个带 `ipass_cache on` 的 location 都会在 access 阶段做闸门: -# 1) 该路由没配上游 → 503(不会让请求走到空 $cache_target 变成 500) -# 2) 统一 sk_ Key 鉴权(与 LLM / API 代理同一把)→ 401 -# 3) 每分钟/每日配额 → 429 -# 然后数据面才交给 nginx 原生 proxy_cache。 -# ============================================================================ - - # ---------------- 管理台(多页静态资源,挂在 /admin/ 下)---------------- - # 每页 head 里有 ,所以资源与 /api/ 都是绝对路径。 - # - # **不发版不重启也能修好页面**:所有 HTML / CSS / JS 都加 - # `Cache-Control: no-store`(expires -1 + add_header)。否则用户浏览器会 - # 一直用发版前的旧 JS —— 典型表现是「后端加了字段,页面上看不到」。 - # - # ⚠️ alias 刻意写**相对**路径 `www/`:nginx 把 root/alias 的相对路径解析到 - # **prefix** 而不是 conf/,所以生产(prefix=/usr/local/nginx)得到 - # /usr/local/nginx/www/,本地回归(prefix=tests/.../.run-prod)得到 - # 测试自己的 www/。写死绝对路径则本机必 404。 - location = / { - return 302 /admin; - } - location = /admin { - default_type text/html; - alias www/cache.html; - expires -1; - add_header Cache-Control "no-store" always; - } - # `/admin/`(末尾斜杠、后面无字符)不能直接 alias 到目录:nginx 会把 - # index 拼成 `cache.htmlindex.html` 而 500。内部重定向到具体文件最干净。 - location = /admin/ { - rewrite ^ /admin/cache.html last; - } - location /admin/ { - alias www/; - expires -1; - add_header Cache-Control "no-store" always; - } - - # ---------------- API:管理态(登录会话)+ 账号态(sk_)---------------- - # 由 plugin-cache 认领 /api/admin/cache* 与 /api/v1/cache*; - # 其余 /api/* 交给同一 gateway 里的其它插件(llm/apix/domain),互不干扰。 - location /api/ { - ipass_api on; - } - - # ---------------- 上游连通性探针(管理台「测试」按钮)---------------- - # /api/admin/cache/test 校验 route 后由模块内部重定向到这里。 - # 刻意**复用** $cache_target / $cache_host / $cache_auth —— 与真实转发 - # 走同一套「拼上游 URL + 换凭据」逻辑,所以「测试通过」才等价于 - # 「真实请求能到上游并带上凭据」;另写一套探针代码必然漂移。 - # - # 为什么这里不写 `ipass_cache on`:内部重定向会重跑 access 阶段,而管理台 - # 带的是**登录 Cookie、不是 sk_**,再鉴一次会把自己 401 掉。鉴权已在 - # /api/admin/cache/test 那一跳用 Admin 守卫做过,这里是 internal,外部进不来。 - # - # proxy_cache off:探针是「验连通」,不该用一次探测污染 1d 的制品缓存。 - location /_cache_test/ { - internal; - proxy_cache off; - proxy_pass $cache_target; - proxy_http_version 1.1; - # 探针只验「拼目标 + 换凭据 + 到达上游」,请求头与请求体都不需要转发: - # * 方法由模块钉成 GET(见 plugin-cache api.rs 的 h_admin_test); - # * body 必须 off —— 管理台是用 POST 触发的,不 off 的话那 2 字节 `{}` - # 会连 Content-Length 一起发出去,上游等一个 GET 的 body、 - # 或 keepalive 上残留半截请求体,表现为「测试时好时坏」。 - proxy_pass_request_headers off; - proxy_pass_request_body off; - proxy_set_header Host $cache_host; - proxy_set_header Authorization $cache_auth; - proxy_set_header User-Agent $http_user_agent; - proxy_ssl_server_name on; - proxy_connect_timeout 5s; - proxy_send_timeout 15s; - proxy_read_timeout 15s; - } - - # ---------------- Go module proxy ---------------- - location /go/ { - ipass_cache on; - proxy_cache go; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- crates.io 稀疏索引 ---------------- - location /cargo/ { - ipass_cache on; - proxy_cache cargo; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- .crate 包本体 ---------------- - # ⚠️ crates.io 的 config.json 里 `dl` 字段给的是**绝对 URL** - # (https://static.crates.io/crates/...),cargo 默认直连上游域名、绕过本网关, - # 所以这个 location 只是**落点就绪**。要真正命中需二选一: - # (a) nginx 开 http_sub_module,用 sub_filter 把索引/`config.json` 里的 - # 绝对 URL 改写成 https://<本网关>/cargo-dl;或 - # (b) 网络层把 static.crates.io 指到本网关(内网 DNS + 证书)。 - # 两者都归到 cargo 生态计数(plugin-cache 的 eco_of_route 已做映射)。 - location /cargo-dl/ { - ipass_cache on; - proxy_cache cargo; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- PyPI 简单索引 / JSON ---------------- - location /pypi/ { - ipass_cache on; - proxy_cache pypi; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- wheel/sdist 包文件 ---------------- - # ⚠️ 与 /cargo-dl/ 同理:PyPI simple 页里的 href 是绝对 URL - # (https://files.pythonhosted.org/...),pip 默认直连,需 sub_filter 重写 - # 或 DNS 指向才能命中本 location。归到 pypi 生态计数。 - location /pypi-files/ { - ipass_cache on; - proxy_cache pypi; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- npm registry ---------------- - location /npm/ { - ipass_cache on; - proxy_cache npm; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- Maven Central ---------------- - location /maven/ { - ipass_cache on; - proxy_cache maven; - proxy_pass $cache_target; - include cache-common.conf; - } - - # ---------------- Docker Registry v2 ---------------- - # registry-mirrors 要求 https(见 conf/tls/),且**只适合公开镜像**: - # 缓存键不含用户身份,私有镜像会被串档(见 cache-common.conf 的说明)。 - # 客户端先向 auth.docker.io 换短期 Bearer token 再带 Authorization 来拉, - # 该 token 由 cache-common.conf 的 $cache_auth 透传给上游(但网关自己的 - # sk_ 会被剔除)。上游路径的 `/v2` 前缀由模块的 upstream_path_prefix 补回。 - location /v2/ { - ipass_cache on; - proxy_cache docker; - proxy_pass $cache_target; - include cache-common.conf; - } diff --git a/tests/cache/.run-cap/conf/mime.types b/tests/cache/.run-cap/conf/mime.types deleted file mode 100644 index 1c00d70..0000000 --- a/tests/cache/.run-cap/conf/mime.types +++ /dev/null @@ -1,99 +0,0 @@ - -types { - text/html html htm shtml; - text/css css; - text/xml xml; - image/gif gif; - image/jpeg jpeg jpg; - application/javascript js; - application/atom+xml atom; - application/rss+xml rss; - - text/mathml mml; - text/plain txt; - text/vnd.sun.j2me.app-descriptor jad; - text/vnd.wap.wml wml; - text/x-component htc; - - image/avif avif; - image/png png; - image/svg+xml svg svgz; - image/tiff tif tiff; - image/vnd.wap.wbmp wbmp; - image/webp webp; - image/x-icon ico; - image/x-jng jng; - image/x-ms-bmp bmp; - - font/woff woff; - font/woff2 woff2; - - application/java-archive jar war ear; - application/json json; - application/mac-binhex40 hqx; - application/msword doc; - application/pdf pdf; - application/postscript ps eps ai; - application/rtf rtf; - application/vnd.apple.mpegurl m3u8; - application/vnd.google-earth.kml+xml kml; - application/vnd.google-earth.kmz kmz; - application/vnd.ms-excel xls; - application/vnd.ms-fontobject eot; - application/vnd.ms-powerpoint ppt; - application/vnd.oasis.opendocument.graphics odg; - application/vnd.oasis.opendocument.presentation odp; - application/vnd.oasis.opendocument.spreadsheet ods; - application/vnd.oasis.opendocument.text odt; - application/vnd.openxmlformats-officedocument.presentationml.presentation - pptx; - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - xlsx; - application/vnd.openxmlformats-officedocument.wordprocessingml.document - docx; - application/vnd.wap.wmlc wmlc; - application/wasm wasm; - application/x-7z-compressed 7z; - application/x-cocoa cco; - application/x-java-archive-diff jardiff; - application/x-java-jnlp-file jnlp; - application/x-makeself run; - application/x-perl pl pm; - application/x-pilot prc pdb; - application/x-rar-compressed rar; - application/x-redhat-package-manager rpm; - application/x-sea sea; - application/x-shockwave-flash swf; - application/x-stuffit sit; - application/x-tcl tcl tk; - application/x-x509-ca-cert der pem crt; - application/x-xpinstall xpi; - application/xhtml+xml xhtml; - application/xspf+xml xspf; - application/zip zip; - - application/octet-stream bin exe dll; - application/octet-stream deb; - application/octet-stream dmg; - application/octet-stream iso img; - application/octet-stream msi msp msm; - - audio/midi mid midi kar; - audio/mpeg mp3; - audio/ogg ogg; - audio/x-m4a m4a; - audio/x-realaudio ra; - - video/3gpp 3gpp 3gp; - video/mp2t ts; - video/mp4 mp4; - video/mpeg mpeg mpg; - video/quicktime mov; - video/webm webm; - video/x-flv flv; - video/x-m4v m4v; - video/x-mng mng; - video/x-ms-asf asx asf; - video/x-ms-wmv wmv; - video/x-msvideo avi; -} diff --git a/tests/cache/.run-cap/conf/nginx.cache.conf b/tests/cache/.run-cap/conf/nginx.cache.conf deleted file mode 100644 index 3664d9c..0000000 --- a/tests/cache/.run-cap/conf/nginx.cache.conf +++ /dev/null @@ -1,87 +0,0 @@ -# ============================================================================ -# nginx.cache.conf —— 统一制品缓存网关 · 顶层配置 -# -# vanilla nginx + ipass Rust 动态模块(plugin-cache) -# -# 本机回归:bash tests/cache/run.sh -# 生产部署:把本文件放到 nginx prefix 的 conf/ 下,同目录放 -# server.cache.conf / cache-routes.conf / cache-common.conf / -# mime.types,启动即可;要对外 https 再加 conf/tls/*.conf。 -# -# 数据面 = nginx 原生 proxy_cache(六生态各一个 zone,落本地盘); -# 管理面 = ipass plugin-cache(access 鉴权 + log 按生态记账 + 管理 API)。 -# -# ⚠️ 上游地址/凭据**不在 conf 里**:全在 plugin-cache 的上游表 -# (管理台「制品缓存」页 → data/cache-upstreams.json → 写穿共享内存)。 -# conf 只出现 `$cache_*` 变量名,所以改上游不需要动 conf、不需要 reload。 -# -# ⚠️ 因为 proxy_pass 是变量形式,`resolver` 是**必需**的:nginx 无法在配置 -# 解析期把变量里的主机名解析成 IP,只能运行期查 DNS。缺 resolver 的表现是 -# `/go/...` 一律 500(no resolver defined to resolve ...)。 -# -# 本构建(build/build-nginx-ssl.sh)对齐 ipass 生产的 Dockerfile 参数: -# --with-pcre --with-http_ssl_module --with-http_realip_module -# 即 rewrite 模块与 SSL 都在编,所以本文件与 server.cache.conf / -# cache-routes.conf 里可以用 `return` / `rewrite` / `ssl_certificate`。 -# ============================================================================ - -load_module /Users/rong/Desktop/cache/rust/target/debug/libipass_gateway.dylib; - -worker_processes 1; - error_log /Users/rong/Desktop/cache/tests/cache/.run-cap/logs/error.log info; - pid /Users/rong/Desktop/cache/tests/cache/.run-cap/logs/nginx.pid; - -# 初始管理员口令(不声明则会被 nginx 从环境中剥离) -env ADMIN_INIT_PASSWORD; - -events { - worker_connections 4096; -} - -http { - include mime.types; - default_type application/octet-stream; # 缺 Content-Type 时的兜底; - # API 响应由模块显式设 application/json - - sendfile on; - keepalive_timeout 65; - - # ========== 变量式上游的必需项 ========== - # proxy_pass $cache_target 里的主机名只能运行期解析(见文件头注释)。 - # 两个国内解析器,30s 缓存;ipv6=off 避免拿到不可达的 AAAA 卡住连接。 - resolver 223.5.5.5 119.29.29.29 valid=30s ipv6=off; - resolver_timeout 5s; - - # ========== 可选 TLS(HTTPS)========== - # conf/tls/ 下放任意 *.conf(完整的 443/18098 server 块)即启用对应域名的 - # HTTPS;目录为空或不存在时 include 空匹配,不影响启动。 - # 模板见 conf/tls/cache.conf.example,说明见 conf/tls/README.md - include /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/tls/*.conf; - - # ========== ipass 内核:共享内存 + 数据目录 ========== - # 一张共享内存表承载:上游表(cx:)/ 命中计数(cache:)/ 管理会话 / 限流窗口。 - # 上游表条目数 = 路由数(个位数),10m 绰绰有余。 - ipass_state state 10m; - ipass_data_dir /Users/rong/Desktop/cache/tests/cache/.run-cap/data; - ipass_audit_dir /Users/rong/Desktop/cache/tests/cache/.run-cap/audit; - - # ========== 六类生态缓存区(本地盘热层)========== - # 各生态独立 zone,互不串档、独立上限;use_temp_path=off 省一次改名。 - # ⚠️ cargo / pypi 各有两个 route 共用同一个 zone(索引 + 包本体): - # 这是刻意的——同一门语言的流量记在一起才看得懂,而 zone 内靠 - # $cache_key 的 route 前缀天然分档,不会串。 - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-cap/cache/go levels=1:2 keys_zone=go:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-cap/cache/cargo levels=1:2 keys_zone=cargo:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-cap/cache/pypi levels=1:2 keys_zone=pypi:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm levels=1:2 keys_zone=npm:100m max_size=1m inactive=1h use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-cap/cache/maven levels=1:2 keys_zone=maven:100m max_size=50g inactive=7d use_temp_path=off; - proxy_cache_path /Users/rong/Desktop/cache/tests/cache/.run-cap/cache/docker levels=1:2 keys_zone=docker:200m max_size=200g inactive=7d use_temp_path=off; - - # 命中率日志(排障用;真实指标由 plugin-cache 的共享内存计数器提供) - log_format cache '$remote_addr [$time_local] "$request" $status ' - '$body_bytes_sent cache=$upstream_cache_status eco=$cache_eco ' - 'uri=$uri rt=$request_time'; - access_log /Users/rong/Desktop/cache/tests/cache/.run-cap/logs/cache.log cache; - - include server.cache.conf; -} diff --git a/tests/cache/.run-cap/conf/server.cache.conf b/tests/cache/.run-cap/conf/server.cache.conf deleted file mode 100644 index 2dd2340..0000000 --- a/tests/cache/.run-cap/conf/server.cache.conf +++ /dev/null @@ -1,30 +0,0 @@ -# ============================================================================ -# server.cache.conf —— 统一制品缓存 server(明文入口) -# -# 这是**内网 / 本机 e2e** 用的明文入口。对外的 https 入口在 conf/tls/*.conf -# (模板见 conf/tls/cache.conf.example)——两者 include 同一份 cache-routes.conf, -# 所以路由只此一份、不会漂。 -# -# 为什么要拆两个入口:Docker 的 `registry-mirrors` 只接受 https(明文地址会 -# 被 daemon 当成 insecure registry 拒绝),而本机自测/内网互调又不想折腾证书。 -# -# 为什么本 server 不写 listen ... ssl:证书与域名属于部署环境(见 conf/tls/), -# 写死在这里会让「同一份 conf 换域名」变成改代码。 -# ============================================================================ - -server { - listen 18087; - server_name _; - - # registry 大 blob 直通,不设体积上限(0 = 不限制) - client_max_body_size 0; - # 请求体都是小 JSON / 空体,调大缓冲区让它留在内存里,避免刷临时文件 - client_body_buffer_size 1m; - - # 管理台 `location = /` 会 302 到 /admin。不关掉的话 nginx 默认写 - # 绝对 URL + 实际 listen 端口,反代/端口映射后会把浏览器踢出链路。 - absolute_redirect off; - port_in_redirect off; - - include cache-routes.conf; -} diff --git a/tests/cache/.run-cap/data/api-proxies.json b/tests/cache/.run-cap/data/api-proxies.json deleted file mode 100644 index 59c3871..0000000 --- a/tests/cache/.run-cap/data/api-proxies.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "items": { - "search": { - "supplier": "vendor-a", - "asset_id": "searxng-search", - "base": "http://127.0.0.1:18999", - "key": "ak_test_supplier_key", - "name": "SearXNG 免费搜索", - "desc": "服务端聚合 Google / Brave / Bing", - "params": ["q"], - "enabled": true, - "created_at": 1750000000 - }, - "offline": { - "supplier": "vendor-a", - "asset_id": "disabled-asset", - "base": "http://127.0.0.1:18999", - "key": "ak_test_supplier_key", - "name": "已停用的代理", - "desc": "enabled=false,热路径必须查不到", - "params": [], - "enabled": false, - "created_at": 1750000000 - } - } -} diff --git a/tests/cache/.run-cap/data/cache-upstreams.json b/tests/cache/.run-cap/data/cache-upstreams.json deleted file mode 100644 index 6d66e6e..0000000 --- a/tests/cache/.run-cap/data/cache-upstreams.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "items": { - "npm": { "base": "http://127.0.0.1:18996", "key": "", - "mode": "proxy", "name": "测试上游 npm", "desc": "假上游", - "enabled": true, "note": "", "created_at": 1757500000, "updated_at": 1757500000 }, - "maven": { "base": "http://127.0.0.1:18996", "key": "", - "mode": "proxy", "name": "测试上游 maven", "desc": "假上游", - "enabled": true, "note": "", "created_at": 1757500000, "updated_at": 1757500000 } - } -} diff --git a/tests/cache/.run-cap/data/domains.json b/tests/cache/.run-cap/data/domains.json deleted file mode 100644 index fe51488..0000000 --- a/tests/cache/.run-cap/data/domains.json +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/tests/cache/.run-cap/data/llm-stats.json b/tests/cache/.run-cap/data/llm-stats.json deleted file mode 100644 index ac0eb07..0000000 --- a/tests/cache/.run-cap/data/llm-stats.json +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"saved_at":1789909586,"saved_at_text":"2026-09-20 21:06:26","keys":{}} \ No newline at end of file diff --git a/tests/cache/.run-cap/data/llm-upstreams.json b/tests/cache/.run-cap/data/llm-upstreams.json deleted file mode 100644 index 13aa3bd..0000000 --- a/tests/cache/.run-cap/data/llm-upstreams.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "openai": { - "base": "http://127.0.0.1:18999", - "models": ["gpt-4o", "gpt-4o-mini"], - "auth": "Bearer sk-UPSTREAM-REAL-SECRET", - "enabled": true, - "note": "本地靶机;auth 是占位值,不是真密钥", - "created_at": 1757500000 - }, - "other": { - "base": "http://127.0.0.1:18999/llm", - "models": ["claude-3-5-sonnet"], - "auth": "Bearer sk-OTHER-UPSTREAM", - "enabled": true, - "note": "不在 alice 白名单内;base 带路径,用于验证路径拼接与模型发现过滤", - "created_at": 1757500000 - }, - "downstream": { - "base": "http://127.0.0.1:18999", - "models": ["should-not-appear"], - "auth": "", - "enabled": false, - "note": "已停用,用于验证「停用即从共享内存摘除」→ 404,且其模型不应出现在模型发现里", - "created_at": 1757500000 - }, - "deepseeklike": { - "base": "http://127.0.0.1:18999/v1", - "models": ["ds-chat"], - "auth": "Bearer sk-DS-REAL", - "enabled": true, - "note": "base 自带 /v1(厂商文档的常见写法),用于验证版本段不重复拼接", - "created_at": 1757500000 - } -} diff --git a/tests/cache/.run-cap/data/users.json b/tests/cache/.run-cap/data/users.json deleted file mode 100644 index 1c50523..0000000 --- a/tests/cache/.run-cap/data/users.json +++ /dev/null @@ -1 +0,0 @@ -{"admin":{"username":"admin","display_name":"","role":"admin","salt":"5a1b2c3d4e5f60718293a4b5c6d7e8f9","pwd_hash":"062b45d8f3cbef3d30464859355401bb37193868dc42eb941de86855416ab5e8","api_key":"sk_admin_0000000000000000000000000000000000000000000000ab","allowed_models":[],"llm_rpm":60,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"alice":{"username":"alice","display_name":"爱丽丝","role":"user","salt":"00112233445566778899aabbccddeeff","pwd_hash":"00","api_key":"sk_alice_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","allowed_models":["openai"],"llm_rpm":60,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"bob":{"username":"bob","display_name":"","role":"user","salt":"ffeeddccbbaa99887766554433221100","pwd_hash":"00","api_key":"sk_bob_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","allowed_models":[],"llm_rpm":60,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"carol":{"username":"carol","display_name":"","role":"user","salt":"112233445566778899aabbccddeeff00","pwd_hash":"00","api_key":"sk_carol_cccccccccccccccccccccccccccccccccccccccccccccc","allowed_models":[],"llm_rpm":2,"llm_daily":0,"temp_grant_until":0,"created_at":1757500000},"dave":{"username":"dave","display_name":"","role":"user","salt":"aabbccddeeff00112233445566778899","pwd_hash":"00","api_key":"sk_dave_dddddddddddddddddddddddddddddddddddddddddddddddd","allowed_models":[],"llm_rpm":0,"llm_daily":2,"temp_grant_until":0,"created_at":1757500000}} \ No newline at end of file diff --git a/tests/cache/.run-cap/logs/cache.log b/tests/cache/.run-cap/logs/cache.log deleted file mode 100644 index 4225109..0000000 --- a/tests/cache/.run-cap/logs/cache.log +++ /dev/null @@ -1,238 +0,0 @@ -127.0.0.1 [20/Sep/2026:18:25:09 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:09 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:09 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:49 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:49 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:49 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:50 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:52 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.012 -127.0.0.1 [20/Sep/2026:18:25:52 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.012 -127.0.0.1 [20/Sep/2026:18:25:52 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.010 -127.0.0.1 [20/Sep/2026:18:25:52 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.011 -127.0.0.1 [20/Sep/2026:18:25:53 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:54 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:56 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:56 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.009 -127.0.0.1 [20/Sep/2026:18:25:56 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:18:25:57 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:48 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:48 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:48 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:50 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:51 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.019 -127.0.0.1 [20/Sep/2026:18:29:51 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.019 -127.0.0.1 [20/Sep/2026:18:29:51 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.017 -127.0.0.1 [20/Sep/2026:18:29:51 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.010 -127.0.0.1 [20/Sep/2026:18:29:52 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:53 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:54 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:55 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:55 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:55 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:56 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:57 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.019 -127.0.0.1 [20/Sep/2026:18:29:57 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:18:29:58 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:29 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:29 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:29 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:31 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:33 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.016 -127.0.0.1 [20/Sep/2026:18:40:33 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.015 -127.0.0.1 [20/Sep/2026:18:40:33 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.016 -127.0.0.1 [20/Sep/2026:18:40:33 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.017 -127.0.0.1 [20/Sep/2026:18:40:34 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:35 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:36 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:36 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:36 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:36 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:36 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:38 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:39 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.017 -127.0.0.1 [20/Sep/2026:18:40:39 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:18:40:40 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:00 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:00 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:00 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:02 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:03 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.009 -127.0.0.1 [20/Sep/2026:18:43:03 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.010 -127.0.0.1 [20/Sep/2026:18:43:03 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.010 -127.0.0.1 [20/Sep/2026:18:43:03 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.010 -127.0.0.1 [20/Sep/2026:18:43:04 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:05 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:05 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:05 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:05 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:05 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:05 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:07 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:07 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.010 -127.0.0.1 [20/Sep/2026:18:43:07 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:18:43:08 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:07 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:07 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:07 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:09 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:11 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.016 -127.0.0.1 [20/Sep/2026:20:58:11 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.017 -127.0.0.1 [20/Sep/2026:20:58:11 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.015 -127.0.0.1 [20/Sep/2026:20:58:11 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.022 -127.0.0.1 [20/Sep/2026:20:58:12 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:14 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:15 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:15 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:15 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:15 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:16 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:18 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:19 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.013 -127.0.0.1 [20/Sep/2026:20:58:19 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:20 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:58:23 +0800] "GET /npm/keep-a/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/keep-a/1 rt=0.013 -127.0.0.1 [20/Sep/2026:20:58:23 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.021 -127.0.0.1 [20/Sep/2026:20:58:23 +0800] "GET /maven/keep-m/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=maven uri=/maven/keep-m/1 rt=0.018 -127.0.0.1 [20/Sep/2026:20:58:27 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1232 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:23 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:24 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:24 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:26 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:28 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.019 -127.0.0.1 [20/Sep/2026:20:59:28 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.014 -127.0.0.1 [20/Sep/2026:20:59:28 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.014 -127.0.0.1 [20/Sep/2026:20:59:28 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.011 -127.0.0.1 [20/Sep/2026:20:59:29 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:30 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:32 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:32 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:32 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:32 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:32 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:34 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:35 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.013 -127.0.0.1 [20/Sep/2026:20:59:35 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:36 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:40 +0800] "GET /npm/keep-a/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/keep-a/1 rt=0.013 -127.0.0.1 [20/Sep/2026:20:59:40 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.020 -127.0.0.1 [20/Sep/2026:20:59:40 +0800] "GET /maven/keep-m/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=maven uri=/maven/keep-m/1 rt=0.019 -127.0.0.1 [20/Sep/2026:20:59:44 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1232 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:48 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1220 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:53 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.018 -127.0.0.1 [20/Sep/2026:20:59:53 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/dropme/1 rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:53 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1240 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:53 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1279 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:54 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 365 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:54 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1187 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:54 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 121 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:54 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:55 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:20:59:55 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:00:51 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:21:00:51 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:21:00:51 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:00:54 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:00:57 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.021 -127.0.0.1 [20/Sep/2026:21:00:57 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.021 -127.0.0.1 [20/Sep/2026:21:00:57 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.026 -127.0.0.1 [20/Sep/2026:21:00:57 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.017 -127.0.0.1 [20/Sep/2026:21:00:58 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:00:59 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:00 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:00 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:00 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:00 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:00 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:03 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:04 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.020 -127.0.0.1 [20/Sep/2026:21:01:04 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:05 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:08 +0800] "GET /npm/keep-a/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/keep-a/1 rt=0.013 -127.0.0.1 [20/Sep/2026:21:01:08 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.011 -127.0.0.1 [20/Sep/2026:21:01:08 +0800] "GET /maven/keep-m/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=maven uri=/maven/keep-m/1 rt=0.054 -127.0.0.1 [20/Sep/2026:21:01:11 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1232 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:15 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1220 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:18 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.014 -127.0.0.1 [20/Sep/2026:21:01:18 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/dropme/1 rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:18 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1240 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:18 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1279 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:19 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 365 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:20 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1187 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:20 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 121 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:20 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:20 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:01:20 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:13 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:13 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:13 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:16 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:18 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.016 -127.0.0.1 [20/Sep/2026:21:04:18 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.016 -127.0.0.1 [20/Sep/2026:21:04:18 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.023 -127.0.0.1 [20/Sep/2026:21:04:18 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.014 -127.0.0.1 [20/Sep/2026:21:04:19 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:20 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:21 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:21 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:21 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:21 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:21 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:24 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:25 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.015 -127.0.0.1 [20/Sep/2026:21:04:25 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:26 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:30 +0800] "GET /npm/keep-a/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/keep-a/1 rt=0.019 -127.0.0.1 [20/Sep/2026:21:04:30 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.022 -127.0.0.1 [20/Sep/2026:21:04:30 +0800] "GET /maven/keep-m/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=maven uri=/maven/keep-m/1 rt=0.014 -127.0.0.1 [20/Sep/2026:21:04:34 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1232 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:39 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1220 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:43 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.023 -127.0.0.1 [20/Sep/2026:21:04:43 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/dropme/1 rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:43 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1240 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:44 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1279 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:44 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 365 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:45 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1187 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:45 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 121 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:46 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:46 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:04:46 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:48 +0800] "GET / HTTP/1.1" 302 145 cache=- eco=- uri=/ rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:49 +0800] "POST /api/admin/login HTTP/1.1" 200 438 cache=- eco=- uri=/api/admin/login rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:49 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:52 +0800] "GET /api/admin/cache HTTP/1.1" 200 7426 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:54 +0800] "GET /npm/probe?bytes=1024 HTTP/1.1" 200 1024 cache=MISS eco=npm uri=/npm/probe rt=0.020 -127.0.0.1 [20/Sep/2026:21:05:54 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.025 -127.0.0.1 [20/Sep/2026:21:05:54 +0800] "GET /npm/cap/2?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/2 rt=0.018 -127.0.0.1 [20/Sep/2026:21:05:54 +0800] "GET /npm/cap/3?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/3 rt=0.018 -127.0.0.1 [20/Sep/2026:21:05:55 +0800] "GET /api/admin/cache HTTP/1.1" 200 7437 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:56 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7413 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:58 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:58 +0800] "POST /api/admin/cache/purge HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:58 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:58 +0800] "POST /api/admin/cache/purge HTTP/1.1" 400 64 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:05:58 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 242 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:01 +0800] "GET /api/admin/cache?scan=1 HTTP/1.1" 200 7403 cache=- eco=- uri=/api/admin/cache rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:02 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/cap/1 rt=0.031 -127.0.0.1 [20/Sep/2026:21:06:02 +0800] "GET /npm/cap/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/cap/1 rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:03 +0800] "POST /api/admin/cache/purge HTTP/1.1" 200 846 cache=- eco=- uri=/api/admin/cache/purge rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:07 +0800] "GET /npm/keep-a/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/keep-a/1 rt=0.022 -127.0.0.1 [20/Sep/2026:21:06:07 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.018 -127.0.0.1 [20/Sep/2026:21:06:07 +0800] "GET /maven/keep-m/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=maven uri=/maven/keep-m/1 rt=0.021 -127.0.0.1 [20/Sep/2026:21:06:12 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1232 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:16 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1220 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:21 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=MISS eco=npm uri=/npm/dropme/1 rt=0.018 -127.0.0.1 [20/Sep/2026:21:06:21 +0800] "GET /npm/dropme/1?bytes=204800 HTTP/1.1" 200 204800 cache=HIT eco=npm uri=/npm/dropme/1 rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:21 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1240 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:22 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1279 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:22 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 365 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:23 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 200 1187 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:23 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 121 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:23 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 400 133 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:23 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 -127.0.0.1 [20/Sep/2026:21:06:23 +0800] "POST /api/admin/cache/invalidate HTTP/1.1" 401 66 cache=- eco=- uri=/api/admin/cache/invalidate rt=0.000 diff --git a/tests/cache/.run-cap/logs/error.log b/tests/cache/.run-cap/logs/error.log deleted file mode 100644 index 3a43db1..0000000 --- a/tests/cache/.run-cap/logs/error.log +++ /dev/null @@ -1,720 +0,0 @@ -2026/09/20 18:25:08 [notice] 54361#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 18:25:08 [notice] 54361#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:25:08 [notice] 54361#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 18:25:09 [notice] 54362#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:25:09 [notice] 54362#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:25:09 [notice] 54362#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 18:25:09 [notice] 54362#0: using the "kqueue" event method -2026/09/20 18:25:09 [notice] 54362#0: nginx/1.30.4 -2026/09/20 18:25:09 [notice] 54362#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 18:25:09 [notice] 54362#0: OS: Darwin 21.6.0 -2026/09/20 18:25:09 [notice] 54362#0: hw.ncpu: 4 -2026/09/20 18:25:09 [notice] 54362#0: net.inet.tcp.sendspace: 131072 -2026/09/20 18:25:09 [notice] 54362#0: kern.ipc.somaxconn: 128 -2026/09/20 18:25:09 [notice] 54362#0: hw.cachelinesize: 64 -2026/09/20 18:25:09 [notice] 54362#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 18:25:09 [notice] 54363#0: start worker processes -2026/09/20 18:25:09 [notice] 54363#0: start worker process 54364 -2026/09/20 18:25:09 [notice] 54363#0: start cache manager process 54365 -2026/09/20 18:25:09 [notice] 54363#0: start cache loader process 54366 -2026/09/20 18:25:09 [notice] 54363#0: signal 23 (SIGIO) received from 54365 -2026/09/20 18:25:09 [notice] 54363#0: signal 23 (SIGIO) received from 54364 -2026/09/20 18:25:09 [info] 54364#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:09 [notice] 54364#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 18:25:09 [info] 54364#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:09 [info] 54364#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:11 [notice] 54363#0: signal 15 (SIGTERM) received from 54199, exiting -2026/09/20 18:25:11 [notice] 54366#0: exiting -2026/09/20 18:25:11 [notice] 54365#0: exiting -2026/09/20 18:25:11 [notice] 54364#0: exiting -2026/09/20 18:25:11 [notice] 54363#0: signal 23 (SIGIO) received from 54365 -2026/09/20 18:25:11 [notice] 54363#0: signal 23 (SIGIO) received -2026/09/20 18:25:11 [info] 54364#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 18:25:11 [notice] 54364#0: exit -2026/09/20 18:25:11 [notice] 54363#0: signal 20 (SIGCHLD) received from 54366 -2026/09/20 18:25:11 [notice] 54363#0: cache loader process 54366 exited with code 0 -2026/09/20 18:25:11 [notice] 54363#0: signal 23 (SIGIO) received -2026/09/20 18:25:11 [notice] 54363#0: signal 20 (SIGCHLD) received from 54365 -2026/09/20 18:25:11 [notice] 54363#0: cache manager process 54365 exited with code 0 -2026/09/20 18:25:11 [notice] 54363#0: signal 23 (SIGIO) received -2026/09/20 18:25:11 [notice] 54363#0: signal 23 (SIGIO) received from 54364 -2026/09/20 18:25:11 [notice] 54363#0: signal 20 (SIGCHLD) received from 54364 -2026/09/20 18:25:11 [notice] 54363#0: worker process 54364 exited with code 0 -2026/09/20 18:25:11 [notice] 54363#0: exit -2026/09/20 18:25:49 [notice] 54626#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 18:25:49 [notice] 54626#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:25:49 [notice] 54626#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 18:25:49 [notice] 54627#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:25:49 [notice] 54627#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:25:49 [notice] 54627#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 18:25:49 [notice] 54627#0: using the "kqueue" event method -2026/09/20 18:25:49 [notice] 54627#0: nginx/1.30.4 -2026/09/20 18:25:49 [notice] 54627#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 18:25:49 [notice] 54627#0: OS: Darwin 21.6.0 -2026/09/20 18:25:49 [notice] 54627#0: hw.ncpu: 4 -2026/09/20 18:25:49 [notice] 54627#0: net.inet.tcp.sendspace: 131072 -2026/09/20 18:25:49 [notice] 54627#0: kern.ipc.somaxconn: 128 -2026/09/20 18:25:49 [notice] 54627#0: hw.cachelinesize: 64 -2026/09/20 18:25:49 [notice] 54627#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 18:25:49 [notice] 54628#0: start worker processes -2026/09/20 18:25:49 [notice] 54628#0: start worker process 54629 -2026/09/20 18:25:49 [notice] 54629#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:25:49 [notice] 54628#0: start cache manager process 54630 -2026/09/20 18:25:49 [notice] 54628#0: start cache loader process 54632 -2026/09/20 18:25:49 [notice] 54628#0: signal 23 (SIGIO) received -2026/09/20 18:25:49 [notice] 54630#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:25:49 [notice] 54628#0: signal 23 (SIGIO) received from 54630 -2026/09/20 18:25:49 [notice] 54632#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:25:49 [info] 54629#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:49 [notice] 54629#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 18:25:49 [info] 54629#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:49 [info] 54629#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:50 [info] 54629#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:52 [info] 54629#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:52 [info] 54629#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:52 [info] 54629#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:52 [info] 54629#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:53 [info] 54629#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:54 [info] 54629#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:54 [info] 54629#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:54 [info] 54629#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:54 [info] 54629#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:54 [info] 54629#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:54 [notice] 54629#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 18:25:54 [info] 54629#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:56 [info] 54629#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:56 [info] 54629#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:56 [info] 54629#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:57 [notice] 54629#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 18:25:57 [info] 54629#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:25:59 [notice] 54628#0: signal 15 (SIGTERM) received from 54478, exiting -2026/09/20 18:25:59 [notice] 54630#0: exiting -2026/09/20 18:25:59 [notice] 54629#0: exiting -2026/09/20 18:25:59 [notice] 54632#0: exiting -2026/09/20 18:25:59 [notice] 54628#0: signal 23 (SIGIO) received from 54632 -2026/09/20 18:25:59 [info] 54629#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 18:25:59 [notice] 54629#0: exit -2026/09/20 18:25:59 [notice] 54628#0: signal 20 (SIGCHLD) received from 54632 -2026/09/20 18:25:59 [notice] 54628#0: cache loader process 54632 exited with code 0 -2026/09/20 18:25:59 [notice] 54628#0: cache manager process 54630 exited with code 0 -2026/09/20 18:25:59 [notice] 54628#0: signal 23 (SIGIO) received -2026/09/20 18:25:59 [notice] 54628#0: signal 20 (SIGCHLD) received from 54630 -2026/09/20 18:25:59 [notice] 54628#0: signal 23 (SIGIO) received from 54629 -2026/09/20 18:25:59 [notice] 54628#0: signal 20 (SIGCHLD) received from 54629 -2026/09/20 18:25:59 [notice] 54628#0: worker process 54629 exited with code 0 -2026/09/20 18:25:59 [notice] 54628#0: exit -2026/09/20 18:29:48 [notice] 56642#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 18:29:48 [notice] 56642#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:29:48 [notice] 56642#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 18:29:48 [notice] 56643#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:29:48 [notice] 56643#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:29:48 [notice] 56643#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 18:29:48 [notice] 56643#0: using the "kqueue" event method -2026/09/20 18:29:48 [notice] 56643#0: nginx/1.30.4 -2026/09/20 18:29:48 [notice] 56643#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 18:29:48 [notice] 56643#0: OS: Darwin 21.6.0 -2026/09/20 18:29:48 [notice] 56643#0: hw.ncpu: 4 -2026/09/20 18:29:48 [notice] 56643#0: net.inet.tcp.sendspace: 131072 -2026/09/20 18:29:48 [notice] 56643#0: kern.ipc.somaxconn: 128 -2026/09/20 18:29:48 [notice] 56643#0: hw.cachelinesize: 64 -2026/09/20 18:29:48 [notice] 56643#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 18:29:48 [notice] 56644#0: start worker processes -2026/09/20 18:29:48 [notice] 56644#0: start worker process 56646 -2026/09/20 18:29:48 [notice] 56644#0: start cache manager process 56647 -2026/09/20 18:29:48 [notice] 56644#0: start cache loader process 56648 -2026/09/20 18:29:48 [notice] 56646#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:29:48 [notice] 56644#0: signal 23 (SIGIO) received from 56646 -2026/09/20 18:29:48 [notice] 56647#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:29:48 [notice] 56644#0: signal 23 (SIGIO) received from 56647 -2026/09/20 18:29:48 [notice] 56648#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:29:48 [info] 56646#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:48 [notice] 56646#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 18:29:48 [info] 56646#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:48 [info] 56646#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:50 [info] 56646#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:51 [info] 56646#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:51 [info] 56646#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:51 [info] 56646#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:51 [info] 56646#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:52 [info] 56646#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:53 [info] 56646#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:54 [info] 56646#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:54 [info] 56646#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:55 [info] 56646#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:55 [info] 56646#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:55 [notice] 56646#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 18:29:55 [info] 56646#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:56 [info] 56646#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:57 [info] 56646#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:57 [info] 56646#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:29:58 [notice] 56646#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 18:29:58 [info] 56646#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:30:01 [notice] 56644#0: signal 15 (SIGTERM) received from 56490, exiting -2026/09/20 18:30:01 [notice] 56646#0: exiting -2026/09/20 18:30:01 [notice] 56644#0: signal 23 (SIGIO) received from 56646 -2026/09/20 18:30:01 [notice] 56647#0: exiting -2026/09/20 18:30:01 [notice] 56648#0: exiting -2026/09/20 18:30:01 [notice] 56644#0: signal 23 (SIGIO) received -2026/09/20 18:30:01 [info] 56646#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 18:30:01 [notice] 56644#0: signal 20 (SIGCHLD) received from 56648 -2026/09/20 18:30:01 [notice] 56644#0: cache loader process 56648 exited with code 0 -2026/09/20 18:30:01 [notice] 56644#0: cache manager process 56647 exited with code 0 -2026/09/20 18:30:01 [notice] 56644#0: signal 23 (SIGIO) received -2026/09/20 18:30:01 [notice] 56644#0: signal 20 (SIGCHLD) received from 56647 -2026/09/20 18:30:01 [notice] 56646#0: exit -2026/09/20 18:30:01 [notice] 56644#0: signal 23 (SIGIO) received from 56646 -2026/09/20 18:30:01 [notice] 56644#0: signal 20 (SIGCHLD) received from 56646 -2026/09/20 18:30:01 [notice] 56644#0: worker process 56646 exited with code 0 -2026/09/20 18:30:01 [notice] 56644#0: exit -2026/09/20 18:40:29 [notice] 58898#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 18:40:29 [notice] 58898#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:40:29 [notice] 58898#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 18:40:29 [notice] 58899#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:40:29 [notice] 58899#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:40:29 [notice] 58899#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 18:40:29 [notice] 58899#0: using the "kqueue" event method -2026/09/20 18:40:29 [notice] 58899#0: nginx/1.30.4 -2026/09/20 18:40:29 [notice] 58899#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 18:40:29 [notice] 58899#0: OS: Darwin 21.6.0 -2026/09/20 18:40:29 [notice] 58899#0: hw.ncpu: 4 -2026/09/20 18:40:29 [notice] 58899#0: net.inet.tcp.sendspace: 131072 -2026/09/20 18:40:29 [notice] 58899#0: kern.ipc.somaxconn: 128 -2026/09/20 18:40:29 [notice] 58899#0: hw.cachelinesize: 64 -2026/09/20 18:40:29 [notice] 58899#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 18:40:29 [notice] 58900#0: start worker processes -2026/09/20 18:40:29 [notice] 58900#0: start worker process 58901 -2026/09/20 18:40:29 [notice] 58900#0: start cache manager process 58902 -2026/09/20 18:40:29 [notice] 58900#0: start cache loader process 58903 -2026/09/20 18:40:29 [notice] 58902#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:40:29 [notice] 58900#0: signal 23 (SIGIO) received from 58902 -2026/09/20 18:40:29 [notice] 58901#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:40:29 [notice] 58900#0: signal 23 (SIGIO) received from 58901 -2026/09/20 18:40:29 [notice] 58903#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:40:29 [info] 58901#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:29 [notice] 58901#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 18:40:29 [info] 58901#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:29 [info] 58901#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:31 [info] 58901#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:33 [info] 58901#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:33 [info] 58901#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:33 [info] 58901#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:33 [info] 58901#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:34 [info] 58901#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:35 [info] 58901#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:36 [info] 58901#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:36 [info] 58901#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:36 [info] 58901#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:36 [info] 58901#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:36 [notice] 58901#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 18:40:36 [info] 58901#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:38 [info] 58901#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:39 [info] 58901#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:39 [info] 58901#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:40 [notice] 58901#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 18:40:40 [info] 58901#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:40:44 [notice] 58900#0: signal 15 (SIGTERM) received from 58747, exiting -2026/09/20 18:40:44 [notice] 58901#0: exiting -2026/09/20 18:40:44 [notice] 58900#0: signal 23 (SIGIO) received from 58902 -2026/09/20 18:40:44 [notice] 58902#0: exiting -2026/09/20 18:40:44 [notice] 58903#0: exiting -2026/09/20 18:40:44 [notice] 58900#0: signal 23 (SIGIO) received -2026/09/20 18:40:44 [notice] 58900#0: signal 20 (SIGCHLD) received from 58902 -2026/09/20 18:40:44 [notice] 58900#0: cache loader process 58903 exited with code 0 -2026/09/20 18:40:44 [notice] 58900#0: cache manager process 58902 exited with code 0 -2026/09/20 18:40:44 [notice] 58900#0: signal 23 (SIGIO) received -2026/09/20 18:40:44 [notice] 58900#0: signal 20 (SIGCHLD) received -2026/09/20 18:40:44 [info] 58901#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 18:40:44 [notice] 58901#0: exit -2026/09/20 18:40:44 [notice] 58900#0: signal 23 (SIGIO) received from 58901 -2026/09/20 18:40:44 [notice] 58900#0: signal 20 (SIGCHLD) received from 58901 -2026/09/20 18:40:44 [notice] 58900#0: worker process 58901 exited with code 0 -2026/09/20 18:40:44 [notice] 58900#0: exit -2026/09/20 18:43:00 [notice] 59539#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 18:43:00 [notice] 59539#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:43:00 [notice] 59539#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 18:43:00 [notice] 59540#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 18:43:00 [notice] 59540#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 18:43:00 [notice] 59540#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 18:43:00 [notice] 59540#0: using the "kqueue" event method -2026/09/20 18:43:00 [notice] 59540#0: nginx/1.30.4 -2026/09/20 18:43:00 [notice] 59540#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 18:43:00 [notice] 59540#0: OS: Darwin 21.6.0 -2026/09/20 18:43:00 [notice] 59540#0: hw.ncpu: 4 -2026/09/20 18:43:00 [notice] 59540#0: net.inet.tcp.sendspace: 131072 -2026/09/20 18:43:00 [notice] 59540#0: kern.ipc.somaxconn: 128 -2026/09/20 18:43:00 [notice] 59540#0: hw.cachelinesize: 64 -2026/09/20 18:43:00 [notice] 59540#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 18:43:00 [notice] 59541#0: start worker processes -2026/09/20 18:43:00 [notice] 59541#0: start worker process 59542 -2026/09/20 18:43:00 [notice] 59541#0: start cache manager process 59543 -2026/09/20 18:43:00 [notice] 59541#0: start cache loader process 59544 -2026/09/20 18:43:00 [notice] 59543#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:43:00 [notice] 59541#0: signal 23 (SIGIO) received from 59543 -2026/09/20 18:43:00 [notice] 59542#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:43:00 [notice] 59541#0: signal 23 (SIGIO) received from 59542 -2026/09/20 18:43:00 [notice] 59544#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 18:43:00 [info] 59542#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:00 [notice] 59542#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 18:43:00 [info] 59542#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:00 [info] 59542#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:02 [info] 59542#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:03 [info] 59542#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:03 [info] 59542#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:03 [info] 59542#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:03 [info] 59542#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:04 [info] 59542#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:05 [info] 59542#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:05 [info] 59542#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:05 [info] 59542#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:05 [info] 59542#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:05 [info] 59542#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:05 [notice] 59542#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 18:43:05 [info] 59542#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:07 [info] 59542#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:07 [info] 59542#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:07 [info] 59542#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:08 [notice] 59542#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 18:43:08 [info] 59542#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 18:43:10 [notice] 59541#0: signal 15 (SIGTERM) received from 59388, exiting -2026/09/20 18:43:10 [notice] 59543#0: exiting -2026/09/20 18:43:10 [notice] 59542#0: exiting -2026/09/20 18:43:10 [notice] 59544#0: exiting -2026/09/20 18:43:10 [notice] 59541#0: signal 23 (SIGIO) received from 59544 -2026/09/20 18:43:10 [info] 59542#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 18:43:10 [notice] 59542#0: exit -2026/09/20 18:43:10 [notice] 59541#0: signal 20 (SIGCHLD) received from 59544 -2026/09/20 18:43:10 [notice] 59541#0: cache loader process 59544 exited with code 0 -2026/09/20 18:43:10 [notice] 59541#0: cache manager process 59543 exited with code 0 -2026/09/20 18:43:10 [notice] 59541#0: signal 23 (SIGIO) received -2026/09/20 18:43:10 [notice] 59541#0: signal 20 (SIGCHLD) received from 59543 -2026/09/20 18:43:10 [notice] 59541#0: signal 23 (SIGIO) received from 59542 -2026/09/20 18:43:10 [notice] 59541#0: signal 23 (SIGIO) received -2026/09/20 18:43:10 [notice] 59541#0: signal 20 (SIGCHLD) received from 59542 -2026/09/20 18:43:10 [notice] 59541#0: worker process 59542 exited with code 0 -2026/09/20 18:43:10 [notice] 59541#0: exit -2026/09/20 20:58:06 [notice] 62412#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 20:58:06 [notice] 62412#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 20:58:06 [notice] 62412#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 20:58:07 [notice] 62413#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 20:58:07 [notice] 62413#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 20:58:07 [notice] 62413#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 20:58:07 [notice] 62413#0: using the "kqueue" event method -2026/09/20 20:58:07 [notice] 62413#0: nginx/1.30.4 -2026/09/20 20:58:07 [notice] 62413#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 20:58:07 [notice] 62413#0: OS: Darwin 21.6.0 -2026/09/20 20:58:07 [notice] 62413#0: hw.ncpu: 4 -2026/09/20 20:58:07 [notice] 62413#0: net.inet.tcp.sendspace: 131072 -2026/09/20 20:58:07 [notice] 62413#0: kern.ipc.somaxconn: 128 -2026/09/20 20:58:07 [notice] 62413#0: hw.cachelinesize: 64 -2026/09/20 20:58:07 [notice] 62413#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 20:58:07 [notice] 62414#0: start worker processes -2026/09/20 20:58:07 [notice] 62414#0: start worker process 62415 -2026/09/20 20:58:07 [notice] 62414#0: start cache manager process 62416 -2026/09/20 20:58:07 [notice] 62414#0: start cache loader process 62417 -2026/09/20 20:58:07 [notice] 62416#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 20:58:07 [notice] 62417#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 20:58:07 [notice] 62414#0: signal 23 (SIGIO) received from 62416 -2026/09/20 20:58:07 [notice] 62415#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 20:58:07 [notice] 62414#0: signal 23 (SIGIO) received from 62415 -2026/09/20 20:58:07 [info] 62415#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:07 [notice] 62415#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 20:58:07 [info] 62415#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:07 [info] 62415#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:09 [info] 62415#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:11 [info] 62415#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:11 [info] 62415#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:11 [info] 62415#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:11 [info] 62415#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:12 [info] 62415#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:14 [info] 62415#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:15 [info] 62415#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:15 [info] 62415#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:15 [info] 62415#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:15 [info] 62415#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:16 [notice] 62415#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 20:58:16 [info] 62415#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:18 [info] 62415#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:19 [info] 62415#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:19 [info] 62415#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:20 [notice] 62415#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 20:58:20 [info] 62415#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:23 [info] 62415#0: *25 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:23 [info] 62415#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:23 [info] 62415#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:27 [crit] 62416#0: unlink() "/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm/e/bd/808c8ce74bf94df103653a7d95bc5bde" failed (2: No such file or directory) -2026/09/20 20:58:27 [notice] 62415#0: [cache] 预览 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 20:58:27 [info] 62415#0: *31 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:58:31 [notice] 62414#0: signal 15 (SIGTERM) received from 62261, exiting -2026/09/20 20:58:31 [notice] 62415#0: exiting -2026/09/20 20:58:31 [notice] 62417#0: exiting -2026/09/20 20:58:31 [notice] 62414#0: signal 23 (SIGIO) received from 62415 -2026/09/20 20:58:31 [notice] 62416#0: exiting -2026/09/20 20:58:31 [notice] 62414#0: signal 23 (SIGIO) received -2026/09/20 20:58:31 [info] 62415#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 20:58:31 [notice] 62415#0: exit -2026/09/20 20:58:31 [notice] 62414#0: signal 20 (SIGCHLD) received from 62416 -2026/09/20 20:58:31 [notice] 62414#0: cache loader process 62417 exited with code 0 -2026/09/20 20:58:31 [notice] 62414#0: cache manager process 62416 exited with code 0 -2026/09/20 20:58:31 [notice] 62414#0: worker process 62415 exited with code 0 -2026/09/20 20:58:31 [notice] 62414#0: exit -2026/09/20 20:59:23 [notice] 62871#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 20:59:23 [notice] 62871#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 20:59:23 [notice] 62871#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 20:59:23 [notice] 62872#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 20:59:23 [notice] 62872#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 20:59:23 [notice] 62872#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 20:59:23 [notice] 62872#0: using the "kqueue" event method -2026/09/20 20:59:23 [notice] 62872#0: nginx/1.30.4 -2026/09/20 20:59:23 [notice] 62872#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 20:59:23 [notice] 62872#0: OS: Darwin 21.6.0 -2026/09/20 20:59:23 [notice] 62872#0: hw.ncpu: 4 -2026/09/20 20:59:23 [notice] 62872#0: net.inet.tcp.sendspace: 131072 -2026/09/20 20:59:23 [notice] 62872#0: kern.ipc.somaxconn: 128 -2026/09/20 20:59:23 [notice] 62872#0: hw.cachelinesize: 64 -2026/09/20 20:59:23 [notice] 62872#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 20:59:23 [notice] 62873#0: start worker processes -2026/09/20 20:59:23 [notice] 62873#0: start worker process 62874 -2026/09/20 20:59:23 [notice] 62873#0: start cache manager process 62875 -2026/09/20 20:59:23 [notice] 62874#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 20:59:23 [notice] 62873#0: start cache loader process 62877 -2026/09/20 20:59:23 [notice] 62873#0: signal 23 (SIGIO) received -2026/09/20 20:59:23 [notice] 62875#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 20:59:23 [notice] 62873#0: signal 23 (SIGIO) received from 62874 -2026/09/20 20:59:23 [notice] 62873#0: signal 23 (SIGIO) received from 62875 -2026/09/20 20:59:23 [notice] 62877#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 20:59:23 [info] 62874#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:24 [notice] 62874#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 20:59:24 [info] 62874#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:24 [info] 62874#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:26 [info] 62874#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:28 [info] 62874#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:28 [info] 62874#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:28 [info] 62874#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:28 [info] 62874#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:29 [info] 62874#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:30 [info] 62874#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:32 [info] 62874#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:32 [info] 62874#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:32 [info] 62874#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:32 [info] 62874#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:32 [notice] 62874#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 20:59:32 [info] 62874#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:34 [info] 62874#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:35 [info] 62874#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:35 [info] 62874#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:36 [notice] 62874#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 20:59:36 [info] 62874#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:40 [info] 62874#0: *25 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:40 [info] 62874#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:40 [info] 62874#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:43 [crit] 62875#0: unlink() "/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm/e/bd/808c8ce74bf94df103653a7d95bc5bde" failed (2: No such file or directory) -2026/09/20 20:59:44 [notice] 62874#0: [cache] 预览 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 20:59:44 [info] 62874#0: *31 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:48 [notice] 62874#0: [cache] 失效 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 1 个(只删文件,目录保留) -2026/09/20 20:59:48 [info] 62874#0: *32 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:53 [info] 62874#0: *33 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:53 [info] 62874#0: *35 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:53 [notice] 62874#0: [cache] 预览 cache selector=all match="npm:/npm/keep*":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 20:59:53 [info] 62874#0: *36 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:53 [notice] 62874#0: [cache] 预览 cache selector=all match="*keep*":命中 2 个 / 410782 字节,删除 0 个(只删文件,目录保留) -2026/09/20 20:59:53 [info] 62874#0: *37 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:54 [notice] 62874#0: [cache] 预览 cache selector=npm match="keep-m":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 20:59:54 [info] 62874#0: *38 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:54 [notice] 62874#0: [cache] 预览 cache selector=all match="nosuchartifact":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 20:59:54 [info] 62874#0: *39 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:54 [info] 62874#0: *40 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:54 [info] 62874#0: *41 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:55 [info] 62874#0: *42 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:55 [info] 62874#0: *43 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 20:59:57 [notice] 62873#0: signal 15 (SIGTERM) received from 62718, exiting -2026/09/20 20:59:57 [notice] 62873#0: signal 23 (SIGIO) received from 62875 -2026/09/20 20:59:57 [notice] 62875#0: exiting -2026/09/20 20:59:57 [notice] 62877#0: exiting -2026/09/20 20:59:57 [notice] 62873#0: signal 23 (SIGIO) received from 62877 -2026/09/20 20:59:57 [notice] 62874#0: exiting -2026/09/20 20:59:57 [info] 62874#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 20:59:57 [notice] 62874#0: exit -2026/09/20 20:59:57 [notice] 62873#0: signal 20 (SIGCHLD) received from 62877 -2026/09/20 20:59:57 [notice] 62873#0: cache loader process 62877 exited with code 0 -2026/09/20 20:59:57 [notice] 62873#0: cache manager process 62875 exited with code 0 -2026/09/20 20:59:57 [notice] 62873#0: signal 23 (SIGIO) received -2026/09/20 20:59:57 [notice] 62873#0: signal 23 (SIGIO) received from 62874 -2026/09/20 20:59:57 [notice] 62873#0: signal 20 (SIGCHLD) received from 62874 -2026/09/20 20:59:57 [notice] 62873#0: worker process 62874 exited with code 0 -2026/09/20 20:59:57 [notice] 62873#0: exit -2026/09/20 21:00:51 [notice] 63406#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 21:00:51 [notice] 63406#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:00:51 [notice] 63406#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 21:00:51 [notice] 63407#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:00:51 [notice] 63407#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:00:51 [notice] 63407#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 21:00:51 [notice] 63407#0: using the "kqueue" event method -2026/09/20 21:00:51 [notice] 63407#0: nginx/1.30.4 -2026/09/20 21:00:51 [notice] 63407#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 21:00:51 [notice] 63407#0: OS: Darwin 21.6.0 -2026/09/20 21:00:51 [notice] 63407#0: hw.ncpu: 4 -2026/09/20 21:00:51 [notice] 63407#0: net.inet.tcp.sendspace: 131072 -2026/09/20 21:00:51 [notice] 63407#0: kern.ipc.somaxconn: 128 -2026/09/20 21:00:51 [notice] 63407#0: hw.cachelinesize: 64 -2026/09/20 21:00:51 [notice] 63407#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 21:00:51 [notice] 63409#0: start worker processes -2026/09/20 21:00:51 [notice] 63409#0: start worker process 63410 -2026/09/20 21:00:51 [notice] 63409#0: start cache manager process 63411 -2026/09/20 21:00:51 [notice] 63409#0: start cache loader process 63413 -2026/09/20 21:00:51 [notice] 63410#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:00:51 [notice] 63409#0: signal 23 (SIGIO) received from 63410 -2026/09/20 21:00:51 [notice] 63411#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:00:51 [notice] 63409#0: signal 23 (SIGIO) received from 63411 -2026/09/20 21:00:51 [notice] 63413#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:00:51 [info] 63410#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:51 [notice] 63410#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 21:00:51 [info] 63410#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:51 [info] 63410#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:54 [info] 63410#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:57 [info] 63410#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:57 [info] 63410#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:57 [info] 63410#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:57 [info] 63410#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:58 [info] 63410#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:00:59 [info] 63410#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:00 [info] 63410#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:00 [info] 63410#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:00 [info] 63410#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:00 [info] 63410#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:00 [notice] 63410#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 21:01:00 [info] 63410#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:03 [info] 63410#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:04 [info] 63410#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:04 [info] 63410#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:05 [notice] 63410#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 21:01:05 [info] 63410#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:08 [info] 63410#0: *25 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:08 [info] 63410#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:08 [info] 63410#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:11 [crit] 63411#0: unlink() "/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm/e/bd/808c8ce74bf94df103653a7d95bc5bde" failed (2: No such file or directory) -2026/09/20 21:01:11 [notice] 63410#0: [cache] 预览 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:01:11 [info] 63410#0: *31 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:15 [notice] 63410#0: [cache] 失效 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 1 个(只删文件,目录保留) -2026/09/20 21:01:15 [info] 63410#0: *32 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:18 [info] 63410#0: *33 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:18 [info] 63410#0: *35 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:18 [notice] 63410#0: [cache] 预览 cache selector=all match="npm:/npm/keep*":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:01:18 [info] 63410#0: *36 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:18 [notice] 63410#0: [cache] 预览 cache selector=all match="*keep*":命中 2 个 / 410782 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:01:18 [info] 63410#0: *37 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:19 [notice] 63410#0: [cache] 预览 cache selector=npm match="keep-m":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:01:19 [info] 63410#0: *38 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:20 [notice] 63410#0: [cache] 预览 cache selector=all match="nosuchartifact":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:01:20 [info] 63410#0: *39 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:20 [info] 63410#0: *40 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:20 [info] 63410#0: *41 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:20 [info] 63410#0: *42 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:20 [info] 63410#0: *43 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:01:22 [notice] 63409#0: signal 15 (SIGTERM) received from 63239, exiting -2026/09/20 21:01:22 [notice] 63410#0: exiting -2026/09/20 21:01:22 [notice] 63409#0: signal 23 (SIGIO) received from 63410 -2026/09/20 21:01:22 [notice] 63411#0: exiting -2026/09/20 21:01:22 [notice] 63409#0: signal 23 (SIGIO) received -2026/09/20 21:01:22 [notice] 63413#0: exiting -2026/09/20 21:01:22 [notice] 63409#0: signal 23 (SIGIO) received from 63413 -2026/09/20 21:01:23 [notice] 63409#0: signal 20 (SIGCHLD) received from 63413 -2026/09/20 21:01:23 [notice] 63409#0: cache loader process 63413 exited with code 0 -2026/09/20 21:01:23 [notice] 63409#0: signal 23 (SIGIO) received -2026/09/20 21:01:23 [notice] 63409#0: signal 20 (SIGCHLD) received from 63411 -2026/09/20 21:01:23 [notice] 63409#0: cache manager process 63411 exited with code 0 -2026/09/20 21:01:23 [notice] 63409#0: signal 23 (SIGIO) received -2026/09/20 21:01:22 [info] 63410#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 21:01:22 [notice] 63410#0: exit -2026/09/20 21:01:23 [notice] 63409#0: signal 23 (SIGIO) received from 63410 -2026/09/20 21:01:23 [notice] 63409#0: signal 20 (SIGCHLD) received from 63410 -2026/09/20 21:01:23 [notice] 63409#0: worker process 63410 exited with code 0 -2026/09/20 21:01:23 [notice] 63409#0: exit -2026/09/20 21:04:13 [notice] 65308#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 21:04:13 [notice] 65308#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:04:13 [notice] 65308#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 21:04:13 [notice] 65309#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:04:13 [notice] 65309#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:04:13 [notice] 65309#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 21:04:13 [notice] 65309#0: using the "kqueue" event method -2026/09/20 21:04:13 [notice] 65309#0: nginx/1.30.4 -2026/09/20 21:04:13 [notice] 65309#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 21:04:13 [notice] 65309#0: OS: Darwin 21.6.0 -2026/09/20 21:04:13 [notice] 65309#0: hw.ncpu: 4 -2026/09/20 21:04:13 [notice] 65309#0: net.inet.tcp.sendspace: 131072 -2026/09/20 21:04:13 [notice] 65309#0: kern.ipc.somaxconn: 128 -2026/09/20 21:04:13 [notice] 65309#0: hw.cachelinesize: 64 -2026/09/20 21:04:13 [notice] 65309#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 21:04:13 [notice] 65310#0: start worker processes -2026/09/20 21:04:13 [notice] 65310#0: start worker process 65311 -2026/09/20 21:04:13 [notice] 65310#0: start cache manager process 65312 -2026/09/20 21:04:13 [notice] 65310#0: start cache loader process 65313 -2026/09/20 21:04:13 [notice] 65312#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:04:13 [notice] 65310#0: signal 23 (SIGIO) received from 65312 -2026/09/20 21:04:13 [notice] 65313#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:04:13 [notice] 65311#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:04:13 [notice] 65310#0: signal 23 (SIGIO) received from 65311 -2026/09/20 21:04:13 [info] 65311#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:13 [notice] 65311#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 21:04:13 [info] 65311#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:13 [info] 65311#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:16 [info] 65311#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:18 [info] 65311#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:18 [info] 65311#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:18 [info] 65311#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:18 [info] 65311#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:19 [info] 65311#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:20 [info] 65311#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:21 [info] 65311#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:21 [info] 65311#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:21 [info] 65311#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:21 [info] 65311#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:21 [notice] 65311#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 21:04:21 [info] 65311#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:24 [info] 65311#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:25 [info] 65311#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:25 [info] 65311#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:26 [notice] 65311#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 21:04:26 [info] 65311#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:30 [info] 65311#0: *25 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:30 [info] 65311#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:30 [info] 65311#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:33 [crit] 65312#0: unlink() "/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm/e/bd/808c8ce74bf94df103653a7d95bc5bde" failed (2: No such file or directory) -2026/09/20 21:04:34 [notice] 65311#0: [cache] 预览 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:04:34 [info] 65311#0: *31 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:39 [notice] 65311#0: [cache] 失效 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 1 个(只删文件,目录保留) -2026/09/20 21:04:39 [info] 65311#0: *32 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:43 [info] 65311#0: *33 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:43 [info] 65311#0: *35 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:43 [notice] 65311#0: [cache] 预览 cache selector=all match="npm:/npm/keep*":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:04:43 [info] 65311#0: *36 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:44 [notice] 65311#0: [cache] 预览 cache selector=all match="*keep*":命中 2 个 / 410782 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:04:44 [info] 65311#0: *37 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:44 [notice] 65311#0: [cache] 预览 cache selector=npm match="keep-m":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:04:44 [info] 65311#0: *38 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:45 [notice] 65311#0: [cache] 预览 cache selector=all match="nosuchartifact":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:04:45 [info] 65311#0: *39 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:45 [info] 65311#0: *40 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:46 [info] 65311#0: *41 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:46 [info] 65311#0: *42 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:46 [info] 65311#0: *43 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:04:49 [notice] 65310#0: signal 15 (SIGTERM) received from 65155, exiting -2026/09/20 21:04:49 [notice] 65311#0: exiting -2026/09/20 21:04:49 [notice] 65312#0: exiting -2026/09/20 21:04:49 [notice] 65313#0: exiting -2026/09/20 21:04:49 [notice] 65310#0: signal 23 (SIGIO) received from 65313 -2026/09/20 21:04:49 [notice] 65310#0: signal 20 (SIGCHLD) received from 65312 -2026/09/20 21:04:49 [notice] 65310#0: cache manager process 65312 exited with code 0 -2026/09/20 21:04:49 [notice] 65310#0: signal 23 (SIGIO) received -2026/09/20 21:04:49 [notice] 65310#0: signal 20 (SIGCHLD) received from 65313 -2026/09/20 21:04:49 [notice] 65310#0: cache loader process 65313 exited with code 0 -2026/09/20 21:04:49 [notice] 65310#0: signal 23 (SIGIO) received -2026/09/20 21:04:49 [info] 65311#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 21:04:49 [notice] 65311#0: exit -2026/09/20 21:04:49 [notice] 65310#0: signal 20 (SIGCHLD) received from 65311 -2026/09/20 21:04:49 [notice] 65310#0: worker process 65311 exited with code 0 -2026/09/20 21:04:49 [notice] 65310#0: signal 23 (SIGIO) received from 65311 -2026/09/20 21:04:49 [notice] 65310#0: exit -2026/09/20 21:05:48 [notice] 65921#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -nginx: the configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf syntax is ok -2026/09/20 21:05:48 [notice] 65921#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:05:48 [notice] 65921#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -nginx: configuration file /Users/rong/Desktop/cache/tests/cache/.run-cap/conf/nginx.cache.conf test is successful -2026/09/20 21:05:48 [notice] 65922#0: [cache] 容量区已识别 6 个:["go", "cargo", "pypi", "npm", "maven", "docker"] -2026/09/20 21:05:48 [notice] 65922#0: [ipass] 共享内存已装载 0 条转发映射 -2026/09/20 21:05:48 [notice] 65922#0: [ipass] 已装载 5 个账号 Key、3 个 LLM 上游、1 个 API 代理、2 个缓存上游 -2026/09/20 21:05:48 [notice] 65922#0: using the "kqueue" event method -2026/09/20 21:05:48 [notice] 65922#0: nginx/1.30.4 -2026/09/20 21:05:48 [notice] 65922#0: built by clang 14.0.0 (clang-1400.0.29.202) -2026/09/20 21:05:48 [notice] 65922#0: OS: Darwin 21.6.0 -2026/09/20 21:05:48 [notice] 65922#0: hw.ncpu: 4 -2026/09/20 21:05:48 [notice] 65922#0: net.inet.tcp.sendspace: 131072 -2026/09/20 21:05:48 [notice] 65922#0: kern.ipc.somaxconn: 128 -2026/09/20 21:05:48 [notice] 65922#0: hw.cachelinesize: 64 -2026/09/20 21:05:48 [notice] 65922#0: getrlimit(RLIMIT_NOFILE): 1048575:9223372036854775807 -2026/09/20 21:05:48 [notice] 65923#0: start worker processes -2026/09/20 21:05:48 [notice] 65923#0: start worker process 65924 -2026/09/20 21:05:48 [notice] 65923#0: start cache manager process 65925 -2026/09/20 21:05:48 [notice] 65923#0: start cache loader process 65926 -2026/09/20 21:05:48 [notice] 65925#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:05:48 [notice] 65923#0: signal 23 (SIGIO) received from 65925 -2026/09/20 21:05:48 [notice] 65924#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:05:48 [notice] 65923#0: signal 23 (SIGIO) received from 65924 -2026/09/20 21:05:48 [notice] 65926#0: [stats] 已从快照恢复 0/0 键(/Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json) -2026/09/20 21:05:48 [info] 65924#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:49 [notice] 65924#0: [api] 管理员登录 user=admin client=127.0.0.1 -2026/09/20 21:05:49 [info] 65924#0: *2 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:49 [info] 65924#0: *3 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:52 [info] 65924#0: *4 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:54 [info] 65924#0: *5 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:54 [info] 65924#0: *7 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:54 [info] 65924#0: *9 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:54 [info] 65924#0: *11 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:55 [info] 65924#0: *13 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:56 [info] 65924#0: *14 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:58 [info] 65924#0: *15 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:58 [info] 65924#0: *16 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:58 [info] 65924#0: *17 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:58 [info] 65924#0: *18 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:05:58 [notice] 65924#0: [cache] 清理缓存 selector=npm 删除 4 个文件 / 617762 字节(只删文件,目录保留) -2026/09/20 21:05:58 [info] 65924#0: *19 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:01 [info] 65924#0: *20 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:02 [info] 65924#0: *21 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:02 [info] 65924#0: *23 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:03 [notice] 65924#0: [cache] 清理缓存 selector=all 删除 1 个文件 / 205386 字节(只删文件,目录保留) -2026/09/20 21:06:03 [info] 65924#0: *24 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:07 [info] 65924#0: *25 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:07 [info] 65924#0: *27 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:07 [info] 65924#0: *29 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:08 [crit] 65925#0: unlink() "/Users/rong/Desktop/cache/tests/cache/.run-cap/cache/npm/e/bd/808c8ce74bf94df103653a7d95bc5bde" failed (2: No such file or directory) -2026/09/20 21:06:12 [notice] 65924#0: [cache] 预览 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:06:12 [info] 65924#0: *31 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:16 [notice] 65924#0: [cache] 失效 cache selector=all match="dropme":命中 1 个 / 205389 字节,删除 1 个(只删文件,目录保留) -2026/09/20 21:06:16 [info] 65924#0: *32 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:21 [info] 65924#0: *33 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:21 [info] 65924#0: *35 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:21 [notice] 65924#0: [cache] 预览 cache selector=all match="npm:/npm/keep*":命中 1 个 / 205389 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:06:21 [info] 65924#0: *36 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:22 [notice] 65924#0: [cache] 预览 cache selector=all match="*keep*":命中 2 个 / 410782 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:06:22 [info] 65924#0: *37 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:22 [notice] 65924#0: [cache] 预览 cache selector=npm match="keep-m":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:06:22 [info] 65924#0: *38 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:23 [notice] 65924#0: [cache] 预览 cache selector=all match="nosuchartifact":命中 0 个 / 0 字节,删除 0 个(只删文件,目录保留) -2026/09/20 21:06:23 [info] 65924#0: *39 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:23 [info] 65924#0: *40 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:23 [info] 65924#0: *41 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:23 [info] 65924#0: *42 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:23 [info] 65924#0: *43 kevent() reported that client 127.0.0.1 closed keepalive connection -2026/09/20 21:06:26 [notice] 65923#0: signal 15 (SIGTERM) received from 65769, exiting -2026/09/20 21:06:26 [notice] 65923#0: signal 23 (SIGIO) received from 65925 -2026/09/20 21:06:26 [notice] 65924#0: exiting -2026/09/20 21:06:26 [notice] 65926#0: exiting -2026/09/20 21:06:26 [notice] 65925#0: exiting -2026/09/20 21:06:26 [notice] 65923#0: signal 23 (SIGIO) received -2026/09/20 21:06:26 [notice] 65923#0: signal 20 (SIGCHLD) received from 65926 -2026/09/20 21:06:26 [notice] 65923#0: cache loader process 65926 exited with code 0 -2026/09/20 21:06:26 [notice] 65923#0: cache manager process 65925 exited with code 0 -2026/09/20 21:06:26 [notice] 65923#0: signal 23 (SIGIO) received -2026/09/20 21:06:26 [info] 65924#0: [stats] 落盘 0 键(83 B,worker-exit)-> /Users/rong/Desktop/cache/tests/cache/.run-cap/data/llm-stats.json -2026/09/20 21:06:26 [notice] 65924#0: exit -2026/09/20 21:06:26 [notice] 65923#0: signal 20 (SIGCHLD) received from 65924 -2026/09/20 21:06:26 [notice] 65923#0: worker process 65924 exited with code 0 -2026/09/20 21:06:26 [notice] 65923#0: signal 23 (SIGIO) received from 65924 -2026/09/20 21:06:26 [notice] 65923#0: exit diff --git a/tests/cache/.run-cap/logs/error.log.mark b/tests/cache/.run-cap/logs/error.log.mark deleted file mode 100644 index ace9443..0000000 --- a/tests/cache/.run-cap/logs/error.log.mark +++ /dev/null @@ -1 +0,0 @@ -661 diff --git a/tests/cache/.run-cap/logs/upstream.out b/tests/cache/.run-cap/logs/upstream.out deleted file mode 100644 index 14efc36..0000000 --- a/tests/cache/.run-cap/logs/upstream.out +++ /dev/null @@ -1,10 +0,0 @@ -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 -mock upstream on 18996 diff --git a/tests/cache/.run-cap/upstream.log b/tests/cache/.run-cap/upstream.log deleted file mode 100644 index 4b68259..0000000 --- a/tests/cache/.run-cap/upstream.log +++ /dev/null @@ -1,10 +0,0 @@ -GET / auth=- -GET /probe?bytes=1024 auth=- -GET /cap/1?bytes=204800 auth=- -GET /cap/2?bytes=204800 auth=- -GET /cap/3?bytes=204800 auth=- -GET /cap/1?bytes=204800 auth=- -GET /keep-a/1?bytes=204800 auth=- -GET /dropme/1?bytes=204800 auth=- -GET /keep-m/1?bytes=204800 auth=- -GET /dropme/1?bytes=204800 auth=- diff --git a/tests/cache/.run-cap/upstream.pid b/tests/cache/.run-cap/upstream.pid deleted file mode 100644 index 9564ca5..0000000 --- a/tests/cache/.run-cap/upstream.pid +++ /dev/null @@ -1 +0,0 @@ -65914 \ No newline at end of file diff --git a/tests/cache/.run-cap/www b/tests/cache/.run-cap/www deleted file mode 120000 index 898e1cb..0000000 --- a/tests/cache/.run-cap/www +++ /dev/null @@ -1 +0,0 @@ -/Users/rong/Desktop/cache/www \ No newline at end of file diff --git a/tests/run.sh b/tests/run.sh deleted file mode 100755 index 71cd9df..0000000 --- a/tests/run.sh +++ /dev/null @@ -1,221 +0,0 @@ -#!/bin/bash -# ============================================================================ -# 本机回归 · 统一入口 -# -# bash tests/run.sh # 跑全部(域名网关 + LLM 代理) -# bash tests/run.sh gateway # 只跑域名网关(90 条断言) -# bash tests/run.sh llm # 只跑 LLM 代理 -# -# 前置:本机 nginx 已编译好(默认 /Users/xs/Desktop/workspace/iPass/build/ngxbuild/nginx-1.30.4/objs/nginx), -# Rust 模块由本脚本自动 cargo build。 -# 可覆盖:NGINX_BIN / NGINX_BUILD_DIR / PY -# -# ⚠️ 全程使用 tests/.run/ 下的独立 data 目录,**绝不碰生产 data/**。 -# nginx.conf 由 .in 模板渲染(占位符 @MODULE@ / @REPO@ / @RUN@), -# 所以 nginx 跑在哪个路径、模块编到哪都不写死。 -# ============================================================================ -set -uo pipefail - -HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -REPO=$(cd "$HERE/.." && pwd) - -NGINX_BIN=${NGINX_BIN:-/Users/xs/Desktop/workspace/iPass/build/ngxbuild/nginx-1.30.4/objs/nginx} -NGINX_BUILD_DIR=${NGINX_BUILD_DIR:-/Users/xs/Desktop/workspace/iPass/build/ngxbuild/nginx-1.30.4/objs} -MODULE="$REPO/rust/target/debug/libipass_gateway.dylib" - -# python3:优先隔离的托管运行时,其次系统的 -PY=${PY:-} -if [ -z "$PY" ]; then - for c in /Users/xs/.workbuddy/binaries/python/versions/3.13.12/bin/python3 python3; do - if command -v "$c" >/dev/null 2>&1; then PY=$c; break; fi - done -fi -[ -n "$PY" ] || { echo "错误:找不到 python3" >&2; exit 1; } - -# --------------------------------------------------------------------------- -# 工具 -# --------------------------------------------------------------------------- - -# sed 替换串里的 & 和 | 需要转义,否则路径里的这些字符会炸 -esc() { printf '%s' "$1" | sed -e 's/[&|]/\\&/g'; } - -# render <模板> <输出> -render() { - sed -e "s|@MODULE@|$(esc "$MODULE")|g" \ - -e "s|@REPO@|$(esc "$REPO")|g" \ - -e "s|@RUN@|$(esc "$3")|g" "$1" > "$2" -} - -ngx_stop() { - if [ -f "$1/nginx.conf" ]; then - "$NGINX_BIN" -p "$1" -c "$1/nginx.conf" -s stop >/dev/null 2>&1 - fi - sleep 1 - return 0 -} -ngx_start() { "$NGINX_BIN" -p "$1" -c "$1/nginx.conf"; } - -# wait_tcp -wait_tcp() { - local i - for i in $(seq 1 60); do - "$PY" -c 'import socket, sys -s = socket.socket(); s.settimeout(0.2) -try: - s.connect((sys.argv[1], int(sys.argv[2]))); sys.exit(0) -except Exception: - sys.exit(1)' "$1" "$2" && return 0 - sleep 0.1 - done - return 1 -} - -build() { - echo "── 编译 Rust 模块 ──" - ( cd "$REPO/rust" && NGINX_BUILD_DIR="$NGINX_BUILD_DIR" cargo build ) || return 1 -} - -# --------------------------------------------------------------------------- -# 套件:域名网关 -# --------------------------------------------------------------------------- -suite_gateway() { - local RUN="$HERE/.run/gateway" - mkdir -p "$RUN/data" "$RUN/logs" - render "$HERE/gateway/nginx.conf.in" "$RUN/nginx.conf" "$RUN" - - ngx_stop "$RUN" - # 夹具覆盖式播种:保证每次跑起点一致(合成数据,非生产 data)。 - # 用 cp 而非 rm —— 批量删除会被安全钩子拦截,导致脏数据残留、断言失败。 - cp "$HERE/gateway/fixtures/users.json" "$RUN/data/users.json" - cp "$HERE/gateway/fixtures/domains.json" "$RUN/data/domains.json" - : > "$RUN/cookie.jar" - : > "$RUN/logs/error.log" - - echo "── 启动网关(18080 转发 / 18081 靶机 / 18082 管理)──" - if ! ngx_start "$RUN"; then - echo "nginx 启动失败,见 $RUN/logs/error.log" >&2; return 1 - fi - wait_tcp 127.0.0.1 18082 || { echo "管理端口未就绪" >&2; ngx_stop "$RUN"; return 1; } - - echo - ADMIN="http://localhost:18082" GW="http://127.0.0.1:18080" \ - JAR="$RUN/cookie.jar" RUN="$RUN" PY="$PY" \ - bash "$HERE/gateway/e2e.sh" - local rc=$? - - ngx_stop "$RUN" - return $rc -} - -# --------------------------------------------------------------------------- -# 套件:LLM 代理 -# --------------------------------------------------------------------------- -suite_llm() { - local RUN="$HERE/.run/llm" - local up_pid="" - mkdir -p "$RUN/data" "$RUN/logs" - - # 夹具覆盖式播种,保证每次跑的起点一致(这两个文件是合成数据,不是生产 data) - cp "$HERE/llm/fixtures/users.json" "$RUN/data/users.json" - cp "$HERE/llm/fixtures/llm-upstreams.json" "$RUN/data/llm-upstreams.json" - cp "$HERE/llm/fixtures/api-proxies.json" "$RUN/data/api-proxies.json" - rm -f "$RUN/upstream.log" - render "$HERE/llm/nginx.conf.in" "$RUN/nginx.conf" "$RUN" - - # TLS 夹具:本机 nginx 带 http_ssl_module 且有 openssl 时,生成自签证书 + - # 渲染 443 server(@RUN@/tls/llm.conf,nginx.conf.in 已 include 该目录)。 - # 任一条件不满足就留空目录 → include 空匹配,HTTPS 组断言自动跳过。 - HAVE_TLS=0 - mkdir -p "$RUN/tls" - rm -f "$RUN"/tls/*.pem "$RUN"/tls/*.conf - if command -v openssl >/dev/null 2>&1 \ - && "$NGINX_BIN" -V 2>&1 | grep -q http_ssl_module; then - if openssl req -x509 -newkey rsa:2048 -nodes -days 30 \ - -subj "/CN=llm.local" -addext "subjectAltName=DNS:llm.local" \ - -keyout "$RUN/tls/key.pem" -out "$RUN/tls/cert.pem" >/dev/null 2>&1; then - render "$HERE/llm/tls.conf.in" "$RUN/tls/llm.conf" "$RUN" - HAVE_TLS=1 - fi - fi - - ngx_stop "$RUN" - : > "$RUN/logs/error.log" - - echo "── 启动假上游(18999)与 LLM 网关(18090)──" - # 清掉**本项目自己**残留的靶机进程(如上次调试用 run_in_background 起的)。 - # 否则新靶机 bind 失败、旧靶机继续服务但日志写到别的目录—— - # 表现就是「请求全 200 但 UP_LOG 全空」,25 条断言集体假失败(2026-09-12 实测)。 - pkill -f "tests/llm/upstream.py" 2>/dev/null - for i in $(seq 1 20); do - "$PY" -c 'import socket, sys -s = socket.socket(); s.settimeout(0.2) -try: - s.connect(("127.0.0.1", 18999)); sys.exit(0) -except Exception: - sys.exit(1)' && sleep 0.2 || break - done - - UPSTREAM_LOG="$RUN/upstream.log" UPSTREAM_PORT=18999 "$PY" "$HERE/llm/upstream.py" \ - >"$RUN/logs/upstream.out" 2>&1 & - up_pid=$! - - # wait_tcp 可能被残留进程「骗绿」——必须确认**本进程**还活着(bind 失败会立刻退出) - if ! kill -0 "$up_pid" 2>/dev/null; then - echo "假上游启动即退出(多半是 18999 被非本项目的进程占用),见 $RUN/logs/upstream.out" >&2 - return 1 - fi - if ! wait_tcp 127.0.0.1 18999; then - echo "假上游未就绪,见 $RUN/logs/upstream.out" >&2 - kill "$up_pid" 2>/dev/null; return 1 - fi - if ! ngx_start "$RUN"; then - echo "nginx 启动失败,见 $RUN/logs/error.log" >&2 - kill "$up_pid" 2>/dev/null; return 1 - fi - if ! wait_tcp 127.0.0.1 18090; then - echo "LLM 端口未就绪" >&2 - ngx_stop "$RUN"; kill "$up_pid" 2>/dev/null; return 1 - fi - wait_tcp 127.0.0.1 18091 || { echo "管理端口未就绪" >&2; ngx_stop "$RUN"; kill "$up_pid" 2>/dev/null; return 1; } - - echo - GW="http://127.0.0.1:18090" API="http://127.0.0.1:18091" \ - UPSTREAM_LOG="$RUN/upstream.log" RUN="$RUN" PY="$PY" HAVE_TLS="$HAVE_TLS" \ - bash "$HERE/llm/e2e.sh" - local rc=$? - - ngx_stop "$RUN" - kill "$up_pid" 2>/dev/null - wait "$up_pid" 2>/dev/null - return $rc -} - -# --------------------------------------------------------------------------- -# main -# --------------------------------------------------------------------------- -WHICH=${1:-all} -rc=0 -rc2=0 - -build || { echo "编译失败,先修 Rust 再跑回归" >&2; exit 1; } -echo - -case "$WHICH" in - gateway) suite_gateway; rc=$? ;; - llm) suite_llm; rc=$? ;; - all|"") - suite_gateway; rc=$? - echo - suite_llm; rc2=$? - [ "$rc2" -ne 0 ] && rc=$rc2 - ;; - *) echo "用法: bash tests/run.sh [gateway|llm|all]" >&2; exit 2 ;; -esac - -echo -if [ "$rc" -eq 0 ]; then - echo "全部通过" -else - echo "有断言失败(退出码 ${rc:-0})" -fi -exit $rc