feat(中间层): 实现 Lua 插件驱动的安全代理
新增固定上游的 HTTP/SSE 转发、Lua 插件生命周期和请求级进程隔离。 实现逐插件域名与内网白名单、DNS 地址固定、超时取消和受控错误收尾。 提供三个 Lua 示例插件、配置与开发文档、Docker 和 APISIX 部署示例。 通过 65 项测试及静态检查,验证真实 TCP 流式转发、取消、资源限制和 Windows 命令行启动。
This commit is contained in:
commit
d0bb045134
28 files changed
+2764
No files matched your search
@@ -0,0 +1,15 @@
|
||||
local plugin = {}
|
||||
local config = {}
|
||||
function plugin.init(ctx, options) config = options end
|
||||
function plugin.on_request(ctx, request)
|
||||
local response = host.http.request({
|
||||
method='POST', url=config.url,
|
||||
headers={['content-type']='application/json'},
|
||||
body=request.body,
|
||||
})
|
||||
if response.status ~= 200 then return {action='block'} end
|
||||
local verdict = host.json.decode(response.body)
|
||||
if verdict.allow ~= true then return {action='block'} end
|
||||
return {action='pass'}
|
||||
end
|
||||
return plugin
|
||||
@@ -0,0 +1,11 @@
|
||||
local plugin = {}
|
||||
local config = {}
|
||||
function plugin.init(ctx, options) config = options end
|
||||
function plugin.on_request(ctx, request)
|
||||
local body = host.json.decode(request.body)
|
||||
if type(body.message) ~= 'string' then return {action='block'} end
|
||||
body.message = (config.prefix or '') .. body.message
|
||||
request.body = host.json.encode(body)
|
||||
return {action='replace', value=request}
|
||||
end
|
||||
return plugin
|
||||
@@ -0,0 +1,43 @@
|
||||
-- Demonstrates cross-event inspection. Intentionally buffers the entire SSE response.
|
||||
-- Masking keeps byte lengths, so redacted text can be placed back in original events.
|
||||
local plugin = {}
|
||||
local frames = {}
|
||||
function plugin.on_event(ctx, event)
|
||||
frames[#frames+1] = event
|
||||
return {action='hold'}
|
||||
end
|
||||
function plugin.on_end(ctx)
|
||||
local text = {}
|
||||
local payloads = {}
|
||||
for i, event in ipairs(frames) do
|
||||
if event.event == 'text_delta' then
|
||||
local payload = host.json.decode(event.data)
|
||||
local content = payload.data and payload.data.content
|
||||
if type(content) ~= 'string' then return {action='block'} end
|
||||
text[#text+1] = content
|
||||
payloads[i] = payload
|
||||
end
|
||||
end
|
||||
local combined = table.concat(text)
|
||||
local masked = string.gsub(combined, string.rep('%d',17)..'[%dXx]', function(v)
|
||||
return string.rep('*', #v)
|
||||
end)
|
||||
masked = string.gsub(masked, '1[3-9]'..string.rep('%d',9), function(v)
|
||||
return string.rep('*', #v)
|
||||
end)
|
||||
local offset = 1
|
||||
for i, event in ipairs(frames) do
|
||||
local payload = payloads[i]
|
||||
if payload then
|
||||
local length = #payload.data.content
|
||||
payload.data.content = string.sub(masked, offset, offset+length-1)
|
||||
offset = offset + length
|
||||
event.data = host.json.encode(payload)
|
||||
end
|
||||
end
|
||||
local released = host.json.array(frames)
|
||||
frames = {}
|
||||
return {action='replace', events=released}
|
||||
end
|
||||
function plugin.close(ctx) frames = {} end
|
||||
return plugin
|
||||
Reference in new issue
Block a user