外置插件 C ABI(v2)
Rust 版外置模块是 cdylib,导出 C ABI 符号。宿主只加载受信任路径,并可选
SHA-256 固定 / Ed25519 签名校验(见 plugin-architecture.md)。
必须导出的符号
#include <stdint.h>
#include <stddef.h>
typedef int (*InitFn)(const uint8_t *cfg_json, size_t cfg_len);
typedef void (*ShutdownFn)(void);
typedef const uint8_t *(*OnHookFn)(
uint32_t hook_index,
const uint8_t *req_json, size_t req_len,
size_t *out_len);
typedef void (*FreeFn)(const uint8_t *ptr, size_t len);
typedef struct {
uint32_t abi_version; /* 当前为 2 */
const uint8_t *manifest_json;
size_t manifest_len;
InitFn init; /* 可空 */
ShutdownFn shutdown; /* 可空 */
OnHookFn on_hook; /* 必须 */
FreeFn free; /* 必须 */
} WxModuleV2;
导出:
__attribute__((visibility("default")))
const WxModuleV2 *wxserver_module_v2(void);
on_hook 返回 JSON(UTF-8),由宿主调用 free 释放。Hook 返回结构:
{
"action": "continue|handled|redirect",
"status": 200,
"headers": {"X-Test": "1"},
"body_base64": "...",
"redirect": "/path",
"blocked": false,
"rule_id": "plugin-rule",
"vars": {"name": "value"}
}
action=handled: 直接返回响应;status默认 200。body_base64: 覆盖/生成响应体。- HeaderFilter/BodyFilter 中使用 headers/body。
blocked=true会设置X-Waf-Blocked。
请求 JSON(on_hook)
{
"hook": "example.header",
"phase": "header_filter",
"kind": "header",
"scope": "location",
"config": {"greeting": "hi"},
"method": "GET",
"path": "/a",
"query": "x=1",
"host": "a.test",
"client_ip": "127.0.0.1",
"is_tls": false,
"proto": "HTTP/1.1",
"headers": {"user-agent": ["curl"]},
"body_base64": "...",
"vars": {"uri": "/a"},
"response": {
"status": 200,
"headers": {},
"body_base64": "..."
}
}
可选:流式 BodyFilter
导出 wxserver_body_filter_v1:
typedef int (*BodyStartFn)(uint32_t hook_index,
const uint8_t *req_json, size_t req_len,
uint64_t *out_id);
typedef int (*BodyChunkFn)(uint64_t stream_id,
const uint8_t *data, size_t len,
uint8_t **out_data, size_t *out_len);
typedef int (*BodyEndFn)(uint64_t stream_id,
uint8_t **out_data, size_t *out_len);
typedef struct {
uint32_t abi_version; /* 2 */
BodyStartFn start;
BodyChunkFn chunk;
BodyEndFn end;
FreeFn free;
} WxBodyFilterV1;
const WxBodyFilterV1 *wxserver_body_filter_v1(void);
chunk 返回的缓冲区由宿主调用 free。
最小示例
static const char *MANIFEST =
"{\"schema_version\":1,\"name\":\"c_echo\",\"version\":\"1.0.0\","
"\"hooks\":[{\"id\":\"c_echo.header\",\"phase\":\"header_filter\","
"\"kind\":\"header\",\"scopes\":[\"global\"],\"priority\":900}]}";
static const uint8_t *on_hook(uint32_t idx, const uint8_t *req,
size_t len, size_t *out_len) {
(void)idx; (void)req; (void)len;
static const char reply[] =
"{\"action\":\"continue\",\"headers\":{\"X-C-Plugin\":\"1\"}}";
*out_len = sizeof(reply) - 1;
return (const uint8_t *)reply;
}
static void free_reply(const uint8_t *p, size_t n) { (void)p; (void)n; }
static const WxModuleV2 MODULE = {
2, (const uint8_t *)MANIFEST, sizeof(MANIFEST) - 1,
NULL, NULL, on_hook, free_reply
};
const WxModuleV2 *wxserver_module_v2(void) { return &MODULE; }
编译:
cc -shared -fPIC -O2 -o libc_echo.so c_echo.c
配置:
[[external_modules]]
path = "plugins/libc_echo.so"
enabled = true
sha256 = "...."
← Previous Article
权限分离与安全模型
Next Article →
No next article