Files
agent-safe-middleware/middleware/lua/agent_protocol.lua
T
zhaiyn d0bb045134 feat(中间层): 实现 Lua 插件驱动的安全代理
新增固定上游的 HTTP/SSE 转发、Lua 插件生命周期和请求级进程隔离。
实现逐插件域名与内网白名单、DNS 地址固定、超时取消和受控错误收尾。
提供三个 Lua 示例插件、配置与开发文档、Docker 和 APISIX 部署示例。
通过 65 项测试及静态检查,验证真实 TCP 流式转发、取消、资源限制和 Windows 命令行启动。
2026-10-08 15:47:16 +08:00

41 lines
1.3 KiB
Lua

-- Agent-specific error serialization is Lua, not a rule in the Python transport.
local plugin = {}
local completed = nil
function plugin.on_event(ctx, event)
if event.event == 'run_completed' then
if completed then error('duplicate terminal event') end
completed = event
return {action='hold'}
end
return {action='pass'}
end
function plugin.on_end(ctx)
if completed then
local event = completed
completed = nil
return {action='replace', events={event}}
end
end
function plugin.on_error(ctx, failure)
local payload = nil
if failure.last_event and failure.last_event.data then
local ok, decoded = pcall(host.json.decode, failure.last_event.data)
if ok then payload = decoded end
end
local sequence = 1
local request_id = ctx.request_id
if type(payload) == 'table' then
if type(payload.sequence) == 'number' then sequence = payload.sequence + 1 end
if type(payload.request_id) == 'string' then request_id = payload.request_id end
end
return {
event = 'run_error', id = tostring(sequence), has_data = true,
data = host.json.encode({
request_id = request_id, sequence = sequence,
timestamp = failure.timestamp,
data = {error_type = failure.error_type},
}),
}
end
return plugin