-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutil_forward.lua
More file actions
355 lines (292 loc) · 11.7 KB
/
util_forward.lua
File metadata and controls
355 lines (292 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
local util_notify = require "util_notify"
local util_http = require "util_http"
local util_smtp = require "util_smtp"
local forward_config = require "forward_config"
local util_forward = {}
-- 转发规则配置
local forward_rules = forward_config
-- 初始化转发规则
local function initForwardRules()
log.info("util_forward", "初始化转发规则", "规则数量", #forward_rules)
-- 打印所有规则用于调试
for i, rule in ipairs(forward_rules) do
local match_desc
if rule.regular then
match_desc = "正则:" .. rule.regular
elseif rule.keyword then
match_desc = "关键词:" .. rule.keyword
else
match_desc = "无匹配条件(匹配所有)"
end
log.info("util_forward", "规则" .. i, "渠道", rule.channel, match_desc, "webhook", rule.webhook and "已配置" or "未配置")
end
return true
end
-- 匹配函数
-- keyword: 关键词部分匹配,不区分大小写
-- regular: Lua正则模式匹配,区分大小写
local function matchRule(content, rule)
if not content then
return false
end
local pattern = rule.regular or rule.keyword
if not pattern then
log.warn("util_forward", "规则未配置keyword或regular,将匹配所有消息")
return true
end
if pattern == "all" then
return true
end
if rule.regular then
-- 正则模式匹配
local ok, result = pcall(string.find, content, pattern)
if not ok then
log.error("util_forward", "正则模式语法错误", pattern, result)
return false
end
return result ~= nil
else
-- 关键词部分匹配,不区分大小写
return string.find(string.lower(content), string.lower(pattern)) ~= nil
end
end
-- 企业微信转发函数
local function sendToWeCom(msg, webhook)
if not webhook or webhook == "" then
log.error("util_forward", "企业微信webhook为空")
return false
end
local header = { ["Content-Type"] = "application/json; charset=utf-8" }
local body = { msgtype = "text", text = { content = msg } }
log.info("util_forward", "发送到企业微信", "webhook", webhook)
local code, headers, response = util_http.fetch(nil, "POST", webhook, header, json.encode(body))
if code and code >= 200 and code < 300 then
log.info("util_forward", "企业微信发送成功", "状态码", code)
return true
else
log.error("util_forward", "企业微信发送失败", "状态码", code, "响应", response)
return false
end
end
-- 飞书转发函数(简化版,无签名验证)
local function sendToFeishu(msg, webhook, secret)
if not webhook or webhook == "" then
log.error("util_forward", "飞书webhook为空")
return false
end
local header = { ["Content-Type"] = "application/json; charset=utf-8" }
local body = { msg_type = "text", content = { text = msg } }
log.info("util_forward", "发送到飞书(无签名模式)", "webhook", webhook)
local code, headers, response = util_http.fetch(nil, "POST", webhook, header, json.encode(body))
if code and code >= 200 and code < 300 then
log.info("util_forward", "飞书发送成功", "状态码", code)
return true
else
log.error("util_forward", "飞书发送失败", "状态码", code, "响应", response)
return false
end
end
-- 钉钉转发函数
local function sendToDingding(msg, webhook, secret)
if not webhook or webhook == "" then
log.error("util_forward", "钉钉webhook为空")
return false
end
local url = webhook
-- 如果配置了密钥,需要签名
if secret and secret ~= "" then
local timestamp = tostring(os.time()) .. "000"
local sign = crypto.hmac_sha256(timestamp .. "\n" .. secret, secret):fromHex():toBase64():urlEncode()
url = url .. "×tamp=" .. timestamp .. "&sign=" .. sign
end
local header = { ["Content-Type"] = "application/json; charset=utf-8" }
local body = { msgtype = "text", text = { content = msg } }
log.info("util_forward", "发送到钉钉", "url", url)
local code, headers, response = util_http.fetch(nil, "POST", url, header, json.encode(body))
if code and code >= 200 and code < 300 then
log.info("util_forward", "钉钉发送成功", "状态码", code)
return true
else
log.error("util_forward", "钉钉发送失败", "状态码", code, "响应", response)
return false
end
end
-- 自定义POST转发函数
local function sendToCustomPost(msg, webhook, content_type, post_body)
if not webhook or webhook == "" then
log.error("util_forward", "自定义POST webhook为空")
return false
end
local header = { ["content-type"] = content_type or "application/json" }
local body = post_body or { title = "消息通知", desp = msg }
-- 替换消息占位符
local function replacePlaceholders(obj)
for k, v in pairs(obj) do
if type(v) == "string" then
obj[k] = string.gsub(v, "{msg}", msg)
elseif type(v) == "table" then
replacePlaceholders(v)
end
end
end
replacePlaceholders(body)
local body_json = json.encode(body)
log.info("util_forward", "发送自定义POST", "url", webhook, "content-type", content_type)
local code, headers, response = util_http.fetch(nil, "POST", webhook, header, body_json)
if code and code >= 200 and code < 300 then
log.info("util_forward", "自定义POST发送成功", "状态码", code)
return true
else
log.error("util_forward", "自定义POST发送失败", "状态码", code, "响应", response)
return false
end
end
-- 根据渠道发送消息
local function sendByChannel(msg, channel, rule)
if not msg or msg == "" then
log.error("util_forward", "消息内容为空")
return false
end
log.info("util_forward", "根据渠道发送消息", "渠道", channel, "消息", msg)
local success = false
if channel == "wecom" then
success = sendToWeCom(msg, rule.webhook)
elseif channel == "feishu" then
success = sendToFeishu(msg, rule.webhook, rule.secret)
elseif channel == "dingding" then
success = sendToDingding(msg, rule.webhook, rule.secret)
elseif channel == "custom_post" then
success = sendToCustomPost(msg, rule.webhook, rule.content_type, rule.post_body)
elseif channel == "email" then
success = util_smtp.send(rule, msg)
else
log.error("util_forward", "不支持的转发渠道", channel)
return false
end
return success
end
--- 通用消息转发函数(异步版本)
-- @param msg 消息内容
-- @param msg_type 消息类型 (可选)
function util_forward.forwardMessage(msg, msg_type)
if not forward_rules or #forward_rules == 0 then
log.warn("util_forward", "没有配置转发规则,跳过转发")
return false
end
log.info("util_forward", "开始转发消息", "类型", msg_type or "未知", "内容", msg)
local matched_rules = {}
-- 循环匹配所有规则
for i, rule in ipairs(forward_rules) do
if matchRule(msg, rule) then
table.insert(matched_rules, rule)
local match_desc
if rule.regular then
match_desc = "正则:" .. rule.regular
elseif rule.keyword then
match_desc = "关键词:" .. rule.keyword
else
match_desc = "匹配所有"
end
log.info("util_forward", "匹配到规则", "索引", i, "渠道", rule.channel, match_desc)
end
end
if #matched_rules == 0 then
log.warn("util_forward", "没有匹配到任何转发规则")
return false
end
log.info("util_forward", "匹配到规则数量", #matched_rules)
-- 启动异步任务执行转发
sys.taskInit(function()
local success_count = 0
for i, rule in ipairs(matched_rules) do
log.info("util_forward", "执行转发规则", i .. "/" .. #matched_rules, "渠道", rule.channel)
local success = sendByChannel(msg, rule.channel, rule)
if success then
success_count = success_count + 1
log.info("util_forward", "转发成功", "渠道", rule.channel)
else
log.error("util_forward", "转发失败", "渠道", rule.channel)
end
-- 避免请求过于频繁
if i < #matched_rules then
sys.wait(1000) -- 等待1秒
end
end
log.info("util_forward", "转发完成", "成功", success_count, "总计", #matched_rules)
end)
return true -- 表示任务已启动
end
--- 主要的转发函数
-- @param msg 消息内容
-- @param sender_number 发件人号码
-- @param time 接收时间
function util_forward.forwardSms(msg, sender_number, time)
if not forward_rules or #forward_rules == 0 then
log.warn("util_forward", "没有配置转发规则,使用默认转发")
-- 使用默认转发方式
util_notify.add({ msg, "", "发件号码: " .. sender_number, "发件时间: " .. time, "#SMS" })
return
end
log.info("util_forward", "开始转发短信", "发件人", sender_number, "内容", msg, "时间", time)
local full_msg = { msg, "", "发件号码: " .. sender_number, "发件时间: " .. time, "#SMS" }
local content = table.concat(full_msg, "\n")
-- 添加设备信息(如果配置启用)
if config.NOTIFY_APPEND_MORE_INFO and not string.find(msg, "开机时长:") then
content = content .. util_mobile.appendDeviceInfo()
end
local matched_rules = {}
-- 循环匹配所有规则
for i, rule in ipairs(forward_rules) do
if matchRule(msg, rule) then
table.insert(matched_rules, rule)
local match_desc
if rule.regular then
match_desc = "正则:" .. rule.regular
elseif rule.keyword then
match_desc = "关键词:" .. rule.keyword
else
match_desc = "匹配所有"
end
log.info("util_forward", "匹配到规则", "索引", i, "渠道", rule.channel, match_desc)
end
end
if #matched_rules == 0 then
log.warn("util_forward", "没有匹配到任何转发规则")
return
end
log.info("util_forward", "匹配到规则数量", #matched_rules)
-- 启动异步任务执行转发
sys.taskInit(function()
local success_count = 0
for i, rule in ipairs(matched_rules) do
log.info("util_forward", "执行转发规则", i .. "/" .. #matched_rules, "渠道", rule.channel)
local success = sendByChannel(content, rule.channel, rule)
if success then
success_count = success_count + 1
log.info("util_forward", "转发成功", "渠道", rule.channel)
else
log.error("util_forward", "转发失败", "渠道", rule.channel)
end
-- 避免请求过于频繁
if i < #matched_rules then
sys.wait(1000) -- 等待1秒
end
end
log.info("util_forward", "转发完成", "成功", success_count, "总计", #matched_rules)
end)
end
--- 重新加载转发规则
function util_forward.reloadRules()
log.info("util_forward", "重新加载转发规则")
return initForwardRules()
end
--- 获取当前转发规则
function util_forward.getRules()
return forward_rules
end
--- 初始化转发模块
function util_forward.init()
log.info("util_forward", "初始化转发模块")
return initForwardRules()
end
return util_forward