Files
ssw__ipass/rust/crates/ipass-core/src/loc.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

57 lines
2.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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,
}
/// 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)
}