From 4713071cd0ba0779919c708982cab35bd4aeb026 Mon Sep 17 00:00:00 2001 From: ssw Date: Sun, 13 Sep 2026 18:52:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gateway):=20=E8=A3=85=E9=85=8D?= =?UTF-8?q?=E9=87=8D=E6=8E=92=20=E2=80=94=E2=80=94=20[Sso,=20Core,=20Domai?= =?UTF-8?q?n]=20+=20JSON=20=E4=BF=A1=E5=B0=81=E5=85=9C=E5=BA=95=20+=20Core?= =?UTF-8?q?Ext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PLUGINS = [SsoPlugin, CorePlugin, DomainPlugin]:重叠身份端点由 sso 优先 接管(CAPTCHA/锁定/审计/身份字段),Core 补 LLM 三端点(grant/models/limits) - 实现 CoreExt::reclaim_on_delete:删账号连带回收名下域名 - dispatch 未命中改 JSON 信封 404(对齐上游,原建议项落地) - loc_switch 补 Proxy/Apix 变体(Proxy 同指 domain 开关;Llm/Apix 恒 false) --- source/rust/Cargo.lock | 3 +- source/rust/gateway/src/lib.rs | 74 ++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/source/rust/Cargo.lock b/source/rust/Cargo.lock index cb00de0..b31b739 100644 --- a/source/rust/Cargo.lock +++ b/source/rust/Cargo.lock @@ -276,10 +276,9 @@ dependencies = [ [[package]] name = "plugin-domain" -version = "1.0.0" +version = "2.0.0" dependencies = [ "ipass-core", - "nginx-sys", "ngx", "serde", "serde_json", diff --git a/source/rust/gateway/src/lib.rs b/source/rust/gateway/src/lib.rs index cd4d83e..c7bc35e 100644 --- a/source/rust/gateway/src/lib.rs +++ b/source/rust/gateway/src/lib.rs @@ -6,8 +6,8 @@ //! load_module modules/libsso_gateway.so; //! │ //! ┌───────────────┼────────────────┐ -//! ipass-core plugin-sso plugin-domain -//! 状态/账号/Key 身份插件 域名插件 +//! ipass-core(内核) plugin-sso plugin-domain +//! 状态/账号/Key/身份 身份插件(优先认领) 域名插件 //! ``` //! //! ## 为什么不拆成多个 .so @@ -45,8 +45,26 @@ use ngx::{ngx_conf_log_error, ngx_string}; use plugin_domain::DomainPlugin; use plugin_sso::SsoPlugin; -/// 插件清单。**顺序即装配顺序**:preconfig → postconfig → dispatch_api。 -static PLUGINS: &[&dyn Plugin] = &[&SsoPlugin, &DomainPlugin]; +/// 插件清单。**顺序即装配顺序**:preconfig → postconfig → dispatch_api, +/// dispatch 按顺序认领、先注册者先得。 +/// +/// SsoPlugin 排在 CorePlugin 之前:两侧重叠的身份端点(login/logout/session/ +/// users CRUD/me)由 sso 优先接管——它带 CAPTCHA、账号锁定、审计与身份字段 +/// (上游 A6-2 决策所述「sso 自实现 login 端点替换 CorePlugin 对应路由」的落点); +/// CorePlugin 只补 sso 没有的端点(users/grant|models|limits 三个 LLM 端点)。 +static PLUGINS: &[&dyn Plugin] = &[&SsoPlugin, &ipass_core::api::CorePlugin, &DomainPlugin]; + +/// 装配根的插件扩展:身份端点所需的插件侧数据经它回填内核 +/// (内核不认识任何插件,见 `ipass_core::api::CoreExt`)。 +/// 只实现正确性必需的 `reclaim_on_delete`;装饰类回调走默认空实现。 +struct GatewayExt; + +impl ipass_core::api::CoreExt for GatewayExt { + /// 删除账号:连带回收该账号名下的域名,避免出现无主映射 + fn reclaim_on_delete(&self, username: &str) -> usize { + plugin_domain::domains::remove_by_owner(username) + } +} // =========================================================================== // 阶段处理器 @@ -84,15 +102,25 @@ impl HttpRequestHandler for ApiHandler { } } -/// 请求体就绪回调:按插件顺序分发,无人认领则 404。 -unsafe extern "C" fn api_body_ready(r: *mut ngx_http_request_t) { - let mut status = HTTPStatus::NOT_FOUND.into(); +/// 管理 API 分发:按 PLUGINS 顺序问每个插件,都不认领才统一 JSON 信封 404。 +fn dispatch_api(r: *mut ngx_http_request_t) -> Status { for p in PLUGINS { - if let Some(st) = p.dispatch_api(r) { - status = st; - break; + if let Some(s) = p.dispatch_api(r) { + return s; } } + let method = ipass_core::http::method_str(r); + let uri = ipass_core::http::uri(r); + ipass_core::http::fail( + r, + HTTPStatus::NOT_FOUND, + &format!("接口不存在:{method} {uri}"), + ) +} + +/// 请求体就绪回调:执行路由分发并终结请求。 +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) }; } @@ -147,9 +175,11 @@ fn loc_switch(request: &mut ngx::http::Request, s: ipass_core::loc::Switch) -> b match s { ipass_core::loc::Switch::Api => c.api, ipass_core::loc::Switch::Iap => c.iap, - ipass_core::loc::Switch::Domain => c.domain, - // LLM 代理插件尚未迁入:保守返回 false,绝不误开 - ipass_core::loc::Switch::Llm => false, + // 上游域名插件查 Proxy、开关指令也是 ipass_proxy;二者同指本模块的 + // domain 开关(子域名 → 后端转发) + ipass_core::loc::Switch::Proxy | ipass_core::loc::Switch::Domain => c.domain, + // LLM / API 代理插件明确不迁入(流量层与身份层分界):保守返回 false + ipass_core::loc::Switch::Llm | ipass_core::loc::Switch::Apix => false, } } @@ -163,6 +193,8 @@ impl HttpModule for Module { /// `proxy_set_header X-Auth-User $auth_user` 无法解析。 unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { ipass_core::loc::set_switch_fn(loc_switch); + // 装配根注册插件扩展:删账号回收域名等经它回填内核 + ipass_core::api::set_ext(&GatewayExt); for p in PLUGINS { if !p.preconfig(cf) { @@ -200,7 +232,7 @@ impl HttpModule for Module { // 只能由装配层统一列出。插件通过 `loc::Switch` 查询开关,不直接持有指令。 static mut NGX_HTTP_SSO_COMMANDS: [ngx_command_t; 6] = [ ngx_command_t { - name: ngx_string!("sso_state"), + 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, @@ -208,7 +240,7 @@ static mut NGX_HTTP_SSO_COMMANDS: [ngx_command_t; 6] = [ post: ptr::null_mut(), }, ngx_command_t { - name: ngx_string!("sso_data_dir"), + 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, @@ -216,7 +248,7 @@ static mut NGX_HTTP_SSO_COMMANDS: [ngx_command_t; 6] = [ post: ptr::null_mut(), }, ngx_command_t { - name: ngx_string!("sso_api"), + 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, @@ -232,7 +264,7 @@ static mut NGX_HTTP_SSO_COMMANDS: [ngx_command_t; 6] = [ post: ptr::null_mut(), }, ngx_command_t { - name: ngx_string!("sso_domain"), + name: ngx_string!("ipass_proxy"), type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, set: Some(set_flag_domain), conf: NGX_HTTP_LOC_CONF_OFFSET, @@ -272,10 +304,10 @@ pub static mut ngx_http_sso_module: ngx_module_t = ngx_module_t { unsafe extern "C" fn init_process(_cycle: *mut ngx_cycle_t) -> ngx_int_t { match users::seed_admin() { Ok(true) => util::log_warn( - "[sso] 已创建初始管理员 admin,请首次登录后立即修改密码", + "[ipass] 已创建初始管理员 admin,请首次登录后立即修改密码", ), Ok(false) => {} - Err(e) => util::log_err(&format!("[sso] 初始化管理员失败: {e}")), + Err(e) => util::log_err(&format!("[ipass] 初始化管理员失败: {e}")), } Status::NGX_OK.into() } @@ -344,7 +376,7 @@ extern "C" fn set_state_zone( 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, "sso_state: 共享内存大小非法"); + ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_state: 共享内存大小非法"); return NGX_CONF_ERROR; } @@ -400,7 +432,7 @@ extern "C" fn set_data_dir( NGX_CONF_OK } Err(_) => { - ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "sso_data_dir: 路径不是合法 UTF-8"); + ngx_conf_log_error!(NGX_LOG_EMERG, cf_ref, "ipass_data_dir: 路径不是合法 UTF-8"); NGX_CONF_ERROR } }