外置模块 ABI v2
Rust 版 wxserver 使用 manifest + hook 的原生插件模型。内置模块与外置 cdylib
使用同一套声明结构;v1(WxModuleV1 / on_request(phase))已删除,不保留兼容。
配置
# 外置插件注册(全局)
external_modules = [{
path = "plugins/example_echo/target/release/libexample_echo.so",
enabled = true,
}]
# global 作用域
modules = {
example_echo = { greeting = "hello" },
}
http = {
# http 作用域
modules = { example_echo = {} },
servers = [{
# server 作用域(站点文件里就是 `servers = [{ ... }]` 片段)
modules = { example_echo = {} },
locations = [{
path = "/echo",
type = "example_echo",
# location 作用域
modules = { example_echo = { greeting = "hi" } },
}],
}],
}
# waf 作用域
waf = {
modules = { example_echo = {} },
}
path 相对主配置目录。enabled=true 必须显式设置。配置值由宿主按 scope
合并、按 manifest schema 校验后,随每次 hook 调用传给插件(config 字段)。
导出符号
#[unsafe(no_mangle)]
pub extern "C" fn wxserver_module_v2() -> *const wxserver_plugin_api::WxModuleV2;
#[repr(C)]
pub struct WxModuleV2 {
pub abi_version: u32, // 必须为 2
pub manifest_json: *const u8,
pub manifest_len: usize,
pub init: Option<InitFn>, // 接收 global [modules.<name>] JSON
pub shutdown: Option<ShutdownFn>,
pub on_hook: Option<OnHookFn>,
pub free: Option<FreeFn>,
}
manifest_json 必须指向静态内存,宿主在模块生命周期内读取。
Manifest
{
"schema_version": 1,
"name": "example_echo",
"version": "0.2.0",
"description": {"en": "...", "zh-CN": "..."},
"content_types": ["example_echo"],
"depends": ["http_headers"],
"scopes": [
{
"scope": "global",
"title": {"en": "Example Echo"},
"fields": [
{"key": "greeting", "kind": "string", "default": "hello",
"label": {"en": "Greeting", "zh-CN": "问候语"}},
{"key": "enabled", "kind": "bool", "default": true}
]
}
],
"depends": ["http_headers"],
"hooks": [
{
"id": "example_echo.access_block",
"phase": "access",
"kind": "filter",
"scopes": ["global", "location"],
"priority": 500,
"terminal": true,
"conditions": {"path_prefix": "/plugin-blocked"}
},
{
"id": "example_echo.content",
"phase": "content",
"kind": "content",
"scopes": ["global", "location"]
},
{
"id": "example_echo.header",
"phase": "header_filter",
"kind": "header",
"scopes": ["global", "location"]
}
],
"ui": {
"areas": [
{"id": "example-global", "scope": "global", "page": "settings",
"title": {"en": "Example Echo"}, "fields": ["greeting", "enabled"]}
],
"actions": []
}
}
depends
"depends": [
"http_headers",
{"name": "http_waf", "optional": true, "min_version": "1.0.0"}
]
必选依赖缺失会导致启动失败;可选依赖缺失只告警。依赖模块先 init、同 priority 时先执行; 环依赖启动失败。
scope / phase / 条件
- scope:
global/http/server/location/waf - phase:
rewrite/preaccess/access/content/header_filter/body_filter/log/waf_request/waf_response - 条件:
methods、path_prefix/exact/regex、hosts、headers、cookies、request_content_types、response_status、response_content_types、tls、client_cidrs priority越小越先执行;内置与外置在同一队列terminal仅对已处理/拦截结果生效content_types与location.type匹配,首个 Content hook 生效
on_hook
uint8_t *on_hook(uint32_t hook_index,
const uint8_t *req_json, size_t req_len,
size_t *out_len);
hook_index 是宿主按 manifest hooks 数组顺序分配的稳定序号。
请求 JSON 常用字段:
{
"hook": "example_echo.content",
"phase": "content",
"kind": "content",
"scope": "location",
"config": {"greeting": "hi", "enabled": true},
"method": "GET", "path": "/echo", "query": "",
"host": "a.test", "client_ip": "127.0.0.1",
"is_tls": false, "proto": "HTTP/1.1",
"headers": {"accept": ["*/*"]},
"body_base64": "",
"vars": {},
"response": {"status": 200, "headers": {"content-type": ["text/plain"]}}
}
回复 JSON:
{
"action": "continue|handled",
"status": 403,
"headers": {"x-plugin": "1"},
"body_base64": "...",
"vars": {"k": "v"},
"path": "/new-path",
"blocked": false,
"rule_id": "",
"tags": []
}
handled:PreAccess/Access/Content 生效;Header/Body Filter 中仅body_base64存在时替换响应体。blocked=true:宿主按 WAF 拦截处理,写X-Waf-Blocked(rule_id)。path:修改后续请求路径(不自动重新匹配 location)。- 插件拥有与服务相同权限;只应加载可信库。
Action 回调(可选)
manifest ui.actions 声明的动作由 Manager POST /api/modules/{name}/action/{id}
触发。外置插件可导出:
typedef struct {
uint32_t abi_version;
OnActionFn on_action; // (action, action_len, req_json, out_len) -> reply_json
FreeFn free;
} WxActionV1;
请求 JSON: {"module","action","body"};回复 JSON 由插件定义。
BodyFilter 流式回调(可选)
manifest 的 body hook 设置 "streaming": true 后,宿主会尝试加载
wxserver_body_filter_v1 导出符号:
typedef struct {
uint32_t abi_version; // 2
BodyStartFn start; // (hook_index, req_json, out_id) -> 0
BodyChunkFn chunk; // (id, data,len, &out,&out_len) -> 0
BodyEndFn end; // (id, &out,&out_len) -> 0
FreeFn free; // 释放输出缓冲
} WxBodyFilterV1;
start返回 0 且 id 非 0 表示接管;否则宿主原样透传chunk返回 0 且 out_len>0 时发送转换后的数据;out_len=0 表示丢弃该块end可返回最后一段数据- 宿主按 hook 顺序链式包装流式响应;仅对
RespBody::Stream生效
Manager
GET /api/modules:内置+外置摘要(hooks/scopes/areas)GET /api/modules/{name}/manifestGET /api/modules/{name}/hooksGET /api/modules/{name}/schema?scope=&lang=GET/PUT /api/modules/{name}/config?scope=&server=&location=GET /api/modules/i18n?lang=- Web 端
PluginModuleForm.vue按 manifest/schema 渲染表单,词条来自内联 i18n