Files
ipass/rust/crates/ipass-core/src/plugin.rs
T
admin 74aeefee58 refactor(plugin): P0-P2 插件化落地 —— workspace 化 + ipass-core + plugin-domain
- P0: rust/ 改 workspace(ipass-core 内核 + gateway 唯一 cdylib),
  [profile.release] 上移根、依赖收敛 workspace.dependencies;
  users::delete 的 stats::forget 依赖改为 on_delete 钩子注册表
- P1: 契约层 core/plugin.rs(Plugin trait)+ core/loc.rs(Switch 并集 +
  set_switch_fn,未实现变体保守 false);gateway PLUGINS 分发循环,
  不认领统一 404(插件绝不自行 404)
- P2: 身份端点下沉 core/api.rs(CorePlugin + CoreExt 扩展注册表,
  gateway 作装配根回填域名数/用量/连带回收/白名单校验);
  plugin-domain crate(domains + DomainEntry + sub: 前缀 + ResolveHandler
  + $proxy_backend_addr + 域名类端点含 purge)
- 行为等价:conf/nginx.conf、Dockerfile、tests 零改动,262 回归全绿
- 方案文档 docs/plugin/PLUGINIZATION_PLAN.md
2026-09-13 11:43:22 +08:00

52 lines
1.9 KiB
Rust

//! 插件装配契约 —— 内核与插件之间唯一的接口。
//!
//! 沿用 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 ngx::core::Status;
use ngx::ffi::{ngx_conf_t, ngx_http_request_t};
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<Status> {
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
}
}