.. _lua-functions:
20.2. Lua函数¶
20.2.1. `output`与`detect`的区别¶
当前``needs``键的初始化方式会根据脚本用途(输出或检测)而有所不同。用于``luaxform``转换的Lua脚本**不使用``needs``**。
若为检测脚本,``needs``应按如下示例初始化(完整检测脚本示例参见:ref:lua-detection):
function init (args)
local needs = {}
needs["packet"] = tostring(true)
return needs
end
若为输出日志,则遵循以下模式(完整脚本结构参见:ref:lua-output):
function init (args)
local needs = {}
needs["protocol"] = "tls"
return needs
end
需注意``log``和``match``可用的函数及协议也可能不同。例如DNP3协议就不支持日志记录。
20.2.2. packet¶
初始化方式:
function init (args)
local needs = {}
needs["type"] = "packet"
return needs
end
20.2.3. flow¶
function init (args)
local needs = {}
needs["type"] = "flow"
return needs
end
20.2.4. http¶
输出日志时初始化:
function init (args)
local needs = {}
needs["protocol"] = "http"
return needs
end
检测时需指定具体缓冲区(完整列表参见:ref:lua-detection),例如:
function init (args)
local needs = {}
needs["http.uri"] = tostring(true)
return needs
end
20.2.5. 流式数据¶
当前支持记录重组后的TCP数据及标准化HTTP数据。脚本会对每个连续数据块触发调用。
对于TCP重组数据,会根据主机操作系统设置去除所有可能的重复段:
function init (args)
return {streaming = "tcp"}
end
对于HTTP正文数据,会自动解压及分块处理(如适用):
function init (args)
return {streaming = "http"}
end
流式数据会通过``args``中的``stream``子表传递给日志函数:
function log(args)
-- 数据缓冲区
local data = args["stream"]["data"]
-- 缓冲区是否开启
local open = args["stream"]["open"]
-- 缓冲区是否关闭
local close = args["stream"]["close"]
-- 发往服务端
local ts = args["stream"]["toserver"]
-- 发往客户端
local tc = args["stream"]["toclient"]
end