subscribe.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #!/usr/bin/lua
  2. ------------------------------------------------
  3. -- This file is part of the luci-app-ssr-plus subscribe.lua
  4. -- @author William Chan <[email protected]>
  5. ------------------------------------------------
  6. require "luci.model.uci"
  7. require "nixio"
  8. require "luci.util"
  9. require "luci.sys"
  10. require "luci.jsonc"
  11. require "luci.model.ipkg"
  12. -- these global functions are accessed all the time by the event handler
  13. -- so caching them is worth the effort
  14. local tinsert = table.insert
  15. local ssub, slen, schar, sbyte, sformat, sgsub = string.sub, string.len, string.char, string.byte, string.format, string.gsub
  16. local jsonParse, jsonStringify = luci.jsonc.parse, luci.jsonc.stringify
  17. local b64decode = nixio.bin.b64decode
  18. local URL = require "url"
  19. local cache = {}
  20. local nodeResult = setmetatable({}, {__index = cache}) -- update result
  21. local name = 'shadowsocksr'
  22. local uciType = 'servers'
  23. local ucic = luci.model.uci.cursor()
  24. local proxy = ucic:get_first(name, 'server_subscribe', 'proxy', '0')
  25. local switch = ucic:get_first(name, 'server_subscribe', 'switch', '1')
  26. local allow_insecure = ucic:get_first(name, 'server_subscribe', 'allow_insecure', '0')
  27. local subscribe_url = ucic:get_first(name, 'server_subscribe', 'subscribe_url', {})
  28. local filter_words = ucic:get_first(name, 'server_subscribe', 'filter_words', '过期时间/剩余流量')
  29. local save_words = ucic:get_first(name, 'server_subscribe', 'save_words', '')
  30. local v2_ss = luci.sys.exec('type -t -p ss-redir sslocal') ~= "" and "ss" or "v2ray"
  31. local v2_tj = luci.sys.exec('type -t -p trojan') ~= "" and "trojan" or "v2ray"
  32. local log = function(...)
  33. print(os.date("%Y-%m-%d %H:%M:%S ") .. table.concat({...}, " "))
  34. end
  35. local encrypt_methods_ss = {
  36. -- plain
  37. "none",
  38. "plain",
  39. -- aead
  40. "aes-128-gcm",
  41. "aes-192-gcm",
  42. "aes-256-gcm",
  43. "chacha20-ietf-poly1305",
  44. "xchacha20-ietf-poly1305",
  45. -- aead 2022
  46. "2022-blake3-aes-128-gcm",
  47. "2022-blake3-aes-256-gcm",
  48. "2022-blake3-chacha20-poly1305"
  49. --[[ stream
  50. "table",
  51. "rc4",
  52. "rc4-md5",
  53. "aes-128-cfb",
  54. "aes-192-cfb",
  55. "aes-256-cfb",
  56. "aes-128-ctr",
  57. "aes-192-ctr",
  58. "aes-256-ctr",
  59. "bf-cfb",
  60. "camellia-128-cfb",
  61. "camellia-192-cfb",
  62. "camellia-256-cfb",
  63. "salsa20",
  64. "chacha20",
  65. "chacha20-ietf" ]]
  66. }
  67. -- 分割字符串
  68. local function split(full, sep)
  69. full = full:gsub("%z", "") -- 这里不是很清楚 有时候结尾带个\0
  70. local off, result = 1, {}
  71. while true do
  72. local nStart, nEnd = full:find(sep, off)
  73. if not nEnd then
  74. local res = ssub(full, off, slen(full))
  75. if #res > 0 then -- 过滤掉 \0
  76. tinsert(result, res)
  77. end
  78. break
  79. else
  80. tinsert(result, ssub(full, off, nStart - 1))
  81. off = nEnd + 1
  82. end
  83. end
  84. return result
  85. end
  86. -- urlencode
  87. local function get_urlencode(c)
  88. return sformat("%%%02X", sbyte(c))
  89. end
  90. local function urlEncode(szText)
  91. local str = szText:gsub("([^0-9a-zA-Z ])", get_urlencode)
  92. str = str:gsub(" ", "+")
  93. return str
  94. end
  95. local function get_urldecode(h)
  96. return schar(tonumber(h, 16))
  97. end
  98. local function UrlDecode(szText)
  99. return szText:gsub("+", " "):gsub("%%(%x%x)", get_urldecode)
  100. end
  101. -- trim
  102. local function trim(text)
  103. if not text or text == "" then
  104. return ""
  105. end
  106. return (sgsub(text, "^%s*(.-)%s*$", "%1"))
  107. end
  108. -- md5
  109. local function md5(content)
  110. local stdout = luci.sys.exec('echo \"' .. urlEncode(content) .. '\" | md5sum | cut -d \" \" -f1')
  111. -- assert(nixio.errno() == 0)
  112. return trim(stdout)
  113. end
  114. -- base64
  115. local function base64Decode(text)
  116. local raw = text
  117. if not text then
  118. return ''
  119. end
  120. text = text:gsub("%z", "")
  121. text = text:gsub("_", "/")
  122. text = text:gsub("-", "+")
  123. local mod4 = #text % 4
  124. text = text .. string.sub('====', mod4 + 1)
  125. local result = b64decode(text)
  126. if result then
  127. return result:gsub("%z", "")
  128. else
  129. return raw
  130. end
  131. end
  132. -- 检查数组(table)中是否存在某个字符值
  133. -- https://www.04007.cn/article/135.html
  134. local function checkTabValue(tab)
  135. local revtab = {}
  136. for k,v in pairs(tab) do
  137. revtab[v] = true
  138. end
  139. return revtab
  140. end
  141. -- 处理数据
  142. local function processData(szType, content)
  143. local result = {type = szType, local_port = 1234, kcp_param = '--nocomp'}
  144. if szType == 'ssr' then
  145. local dat = split(content, "/%?")
  146. local hostInfo = split(dat[1], ':')
  147. result.type = 'ssr'
  148. result.server = hostInfo[1]
  149. result.server_port = hostInfo[2]
  150. result.protocol = hostInfo[3]
  151. result.encrypt_method = hostInfo[4]
  152. result.obfs = hostInfo[5]
  153. result.password = base64Decode(hostInfo[6])
  154. local params = {}
  155. for _, v in pairs(split(dat[2], '&')) do
  156. local t = split(v, '=')
  157. params[t[1]] = t[2]
  158. end
  159. result.obfs_param = base64Decode(params.obfsparam)
  160. result.protocol_param = base64Decode(params.protoparam)
  161. local group = base64Decode(params.group)
  162. if group then
  163. result.alias = "[" .. group .. "] "
  164. end
  165. result.alias = result.alias .. base64Decode(params.remarks)
  166. elseif szType == 'vmess' then
  167. local info = jsonParse(content)
  168. result.type = 'v2ray'
  169. result.v2ray_protocol = 'vmess'
  170. result.server = info.add
  171. result.server_port = info.port
  172. if info.net == "tcp" then
  173. info.net = "raw"
  174. end
  175. result.transport = info.net
  176. result.alter_id = info.aid
  177. result.vmess_id = info.id
  178. result.alias = info.ps
  179. -- result.mux = 1
  180. -- result.concurrency = 8
  181. if info.net == 'ws' then
  182. result.ws_host = info.host
  183. result.ws_path = info.path
  184. end
  185. if info.net == 'httpupgrade' then
  186. result.httpupgrade_host = info.host
  187. result.httpupgrade_path = info.path
  188. end
  189. if info.net == 'splithttp' then
  190. result.splithttp_host = info.host
  191. result.splithttp_path = info.path
  192. end
  193. if info.net == 'xhttp' then
  194. result.xhttp_mode = info.mode
  195. result.xhttp_host = info.host
  196. result.xhttp_path = info.path
  197. -- 检查 extra 参数是否存在且非空
  198. result.enable_xhttp_extra = (info.extra and info.extra ~= "") and "1" or nil
  199. result.xhttp_extra = (info.extra and info.extra ~= "") and info.extra or nil
  200. -- 尝试解析 JSON 数据
  201. local success, Data = pcall(jsonParse, info.extra)
  202. if success and Data then
  203. local address = (Data.extra and Data.extra.downloadSettings and Data.extra.downloadSettings.address)
  204. or (Data.downloadSettings and Data.downloadSettings.address)
  205. result.download_address = address and address ~= "" and address or nil
  206. else
  207. -- 如果解析失败,清空下载地址
  208. result.download_address = nil
  209. end
  210. end
  211. if info.net == 'h2' then
  212. result.h2_host = info.host
  213. result.h2_path = info.path
  214. end
  215. if info.net == 'raw' or info.net == 'tcp' then
  216. if info.type and info.type ~= "http" then
  217. info.type = "none"
  218. end
  219. result.tcp_guise = info.type
  220. result.http_host = info.host
  221. result.http_path = info.path
  222. end
  223. if info.net == 'kcp' then
  224. result.kcp_guise = info.type
  225. result.mtu = 1350
  226. result.tti = 50
  227. result.uplink_capacity = 5
  228. result.downlink_capacity = 20
  229. result.read_buffer_size = 2
  230. result.write_buffer_size = 2
  231. end
  232. if info.net == 'grpc' then
  233. if info.path then
  234. result.serviceName = info.path
  235. elseif info.serviceName then
  236. result.serviceName = info.serviceName
  237. end
  238. end
  239. if info.net == 'quic' then
  240. result.quic_guise = info.type
  241. result.quic_key = info.key
  242. result.quic_security = info.securty
  243. end
  244. if info.security then
  245. result.security = info.security
  246. end
  247. if info.tls == "tls" or info.tls == "1" then
  248. result.tls = "1"
  249. if info.alpn and info.alpn ~= "" then
  250. result.xhttp_alpn = info.alpn
  251. end
  252. if info.sni and info.sni ~= "" then
  253. result.tls_host = info.sni
  254. elseif info.host then
  255. result.tls_host = info.host
  256. end
  257. result.insecure = allow_insecure
  258. else
  259. result.tls = "0"
  260. end
  261. elseif szType == "ss" then
  262. local idx_sp = 0
  263. local alias = ""
  264. if content:find("#") then
  265. idx_sp = content:find("#")
  266. alias = content:sub(idx_sp + 1, -1)
  267. end
  268. local info = content:sub(1, idx_sp - 1)
  269. local hostInfo = split(base64Decode(info), "@")
  270. local host = split(hostInfo[2], ":")
  271. local userinfo = base64Decode(hostInfo[1])
  272. local method = userinfo:sub(1, userinfo:find(":") - 1)
  273. local password = userinfo:sub(userinfo:find(":") + 1, #userinfo)
  274. result.alias = UrlDecode(alias)
  275. result.type = v2_ss
  276. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  277. result.encrypt_method_ss = method
  278. result.password = password
  279. result.server = host[1]
  280. if host[2]:find("/%?") then
  281. local query = split(host[2], "/%?")
  282. result.server_port = query[1]
  283. local params = {}
  284. for _, v in pairs(split(query[2], '&')) do
  285. local t = split(v, '=')
  286. params[t[1]] = t[2]
  287. end
  288. if params.plugin then
  289. local plugin_info = UrlDecode(params.plugin)
  290. local idx_pn = plugin_info:find(";")
  291. if idx_pn then
  292. result.plugin = plugin_info:sub(1, idx_pn - 1)
  293. result.plugin_opts = plugin_info:sub(idx_pn + 1, #plugin_info)
  294. else
  295. result.plugin = plugin_info
  296. end
  297. -- 部分机场下发的插件名为 simple-obfs,这里应该改为 obfs-local
  298. if result.plugin == "simple-obfs" then
  299. result.plugin = "obfs-local"
  300. end
  301. -- 如果插件不為 none,確保 enable_plugin 為 1
  302. if result.plugin ~= "none" and result.plugin ~= "" then
  303. result.enable_plugin = 1
  304. end
  305. end
  306. else
  307. result.server_port = host[2]:gsub("/","")
  308. end
  309. if not checkTabValue(encrypt_methods_ss)[method] then
  310. -- 1202 年了还不支持 SS AEAD 的屑机场
  311. result.server = nil
  312. end
  313. elseif szType == "sip008" then
  314. result.type = v2_ss
  315. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  316. result.server = content.server
  317. result.server_port = content.server_port
  318. result.password = content.password
  319. result.encrypt_method_ss = content.method
  320. result.plugin = content.plugin
  321. result.plugin_opts = content.plugin_opts
  322. result.alias = content.remarks
  323. if not checkTabValue(encrypt_methods_ss)[content.method] then
  324. result.server = nil
  325. end
  326. elseif szType == "ssd" then
  327. result.type = v2_ss
  328. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  329. result.server = content.server
  330. result.server_port = content.port
  331. result.password = content.password
  332. result.encrypt_method_ss = content.method
  333. result.plugin_opts = content.plugin_options
  334. result.alias = "[" .. content.airport .. "] " .. content.remarks
  335. if content.plugin == "simple-obfs" then
  336. result.plugin = "obfs-local"
  337. else
  338. result.plugin = content.plugin
  339. end
  340. if not checkTabValue(encrypt_methods_ss)[content.encryption] then
  341. result.server = nil
  342. end
  343. elseif szType == "trojan" then
  344. local params = {}
  345. local idx_sp = 0
  346. local alias = ""
  347. if content:find("#") then
  348. idx_sp = content:find("#")
  349. alias = content:sub(idx_sp + 1, -1)
  350. end
  351. local info = content:sub(1, idx_sp - 1)
  352. local hostInfo = split(info, "@")
  353. local userinfo = hostInfo[1]
  354. local password = userinfo
  355. -- 分离服务器地址和端口
  356. local host = split(hostInfo[2], ":")
  357. local server = host[1]
  358. local port = host[2]
  359. result.alias = UrlDecode(alias)
  360. result.type = v2_tj
  361. result.v2ray_protocol = "trojan"
  362. result.server = server
  363. result.password = password
  364. -- 按照官方的建议 默认验证ssl证书
  365. result.insecure = "0"
  366. result.tls = "1"
  367. if port:find("?") then
  368. local query = split(port, "?")
  369. result.server_port = query[1]
  370. for _, v in pairs(split(query[2], '&')) do
  371. local t = split(v, '=')
  372. params[t[1]] = t[2]
  373. end
  374. if params.alpn then
  375. -- 处理 alpn 参数
  376. result.xhttp_alpn = params.alpn
  377. end
  378. if params.sni then
  379. -- 未指定peer(sni)默认使用remote addr
  380. result.tls_host = params.sni
  381. end
  382. if params.allowInsecure then
  383. -- 处理 insecure 参数
  384. result.insecure = params.allowInsecure
  385. end
  386. else
  387. result.server_port = port
  388. end
  389. if v2_tj ~= "trojan" then
  390. if params.fp then
  391. -- 处理 fingerprint 参数
  392. result.fingerprint = params.fp
  393. end
  394. -- 处理传输协议
  395. result.transport = params.type or "tcp" -- 默认传输协议为 tcp
  396. if result.transport == "tcp" then
  397. result.transport = "raw"
  398. end
  399. if result.transport == "ws" then
  400. result.ws_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  401. result.ws_path = params.path and UrlDecode(params.path) or "/"
  402. elseif result.transport == "httpupgrade" then
  403. result.httpupgrade_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  404. result.httpupgrade_path = params.path and UrlDecode(params.path) or "/"
  405. elseif result.transport == "splithttp" then
  406. result.splithttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  407. result.splithttp_path = params.path and UrlDecode(params.path) or "/"
  408. elseif result.transport == "xhttp" then
  409. result.xhttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  410. result.xhttp_mode = params.mode or "auto"
  411. result.xhttp_path = params.path and UrlDecode(params.path) or "/"
  412. -- 检查 extra 参数是否存在且非空
  413. result.enable_xhttp_extra = (params.extra and params.extra ~= "") and "1" or nil
  414. result.xhttp_extra = (params.extra and params.extra ~= "") and params.extra or nil
  415. -- 尝试解析 JSON 数据
  416. local success, Data = pcall(jsonParse, params.extra)
  417. if success and Data then
  418. local address = (Data.extra and Data.extra.downloadSettings and Data.extra.downloadSettings.address)
  419. or (Data.downloadSettings and Data.downloadSettings.address)
  420. result.download_address = address and address ~= "" and address or nil
  421. else
  422. -- 如果解析失败,清空下载地址
  423. result.download_address = nil
  424. end
  425. elseif result.transport == "http" or result.transport == "h2" then
  426. result.transport = "h2"
  427. result.h2_host = params.host and UrlDecode(params.host) or nil
  428. result.h2_path = params.path and UrlDecode(params.path) or nil
  429. elseif result.transport == "kcp" then
  430. result.kcp_guise = params.headerType or "none"
  431. result.seed = params.seed
  432. result.mtu = 1350
  433. result.tti = 50
  434. result.uplink_capacity = 5
  435. result.downlink_capacity = 20
  436. result.read_buffer_size = 2
  437. result.write_buffer_size = 2
  438. elseif result.transport == "quic" then
  439. result.quic_guise = params.headerType or "none"
  440. result.quic_security = params.quicSecurity or "none"
  441. result.quic_key = params.key
  442. elseif result.transport == "grpc" then
  443. result.serviceName = params.serviceName
  444. result.grpc_mode = params.mode or "gun"
  445. elseif result.transport == "tcp" or result.transport == "raw" then
  446. result.tcp_guise = params.headerType and params.headerType ~= "" and params.headerType or "none"
  447. if result.tcp_guise == "http" then
  448. result.tcp_host = params.host and UrlDecode(params.host) or nil
  449. result.tcp_path = params.path and UrlDecode(params.path) or nil
  450. end
  451. end
  452. end
  453. elseif szType == "vless" then
  454. local url = URL.parse("http://" .. content)
  455. local params = url.query
  456. result.alias = url.fragment and UrlDecode(url.fragment) or nil
  457. result.type = "v2ray"
  458. result.v2ray_protocol = "vless"
  459. result.server = url.host
  460. result.server_port = url.port
  461. result.vmess_id = url.user
  462. result.vless_encryption = params.encryption or "none"
  463. result.transport = params.type or "tcp"
  464. result.tls = (params.security == "tls" or params.security == "xtls") and "1" or "0"
  465. result.xhttp_alpn = params.alpn or ""
  466. result.tls_host = params.sni
  467. result.tls_flow = (params.security == "tls" or params.security == "reality") and params.flow or nil
  468. result.fingerprint = params.fp
  469. result.reality = (params.security == "reality") and "1" or "0"
  470. result.reality_publickey = params.pbk and UrlDecode(params.pbk) or nil
  471. result.reality_shortid = params.sid
  472. result.reality_spiderx = params.spx and UrlDecode(params.spx) or nil
  473. if result.transport == "ws" then
  474. result.ws_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  475. result.ws_path = params.path and UrlDecode(params.path) or "/"
  476. elseif result.transport == "httpupgrade" then
  477. result.httpupgrade_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  478. result.httpupgrade_path = params.path and UrlDecode(params.path) or "/"
  479. elseif result.transport == "splithttp" then
  480. result.splithttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  481. result.splithttp_path = params.path and UrlDecode(params.path) or "/"
  482. elseif result.transport == "xhttp" then
  483. result.xhttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  484. result.xhttp_mode = params.mode or "auto"
  485. result.xhttp_path = params.path and UrlDecode(params.path) or "/"
  486. -- 检查 extra 参数是否存在且非空
  487. result.enable_xhttp_extra = (params.extra and params.extra ~= "") and "1" or nil
  488. result.xhttp_extra = (params.extra and params.extra ~= "") and params.extra or nil
  489. -- 尝试解析 JSON 数据
  490. local success, Data = pcall(jsonParse, params.extra)
  491. if success and Data then
  492. local address = (Data.extra and Data.extra.downloadSettings and Data.extra.downloadSettings.address)
  493. or (Data.downloadSettings and Data.downloadSettings.address)
  494. result.download_address = address and address ~= "" and address or nil
  495. else
  496. -- 如果解析失败,清空下载地址
  497. result.download_address = nil
  498. end
  499. -- make it compatible with bullshit, "h2" transport is non-existent at all
  500. elseif result.transport == "http" or result.transport == "h2" then
  501. result.transport = "h2"
  502. result.h2_host = params.host and UrlDecode(params.host) or nil
  503. result.h2_path = params.path and UrlDecode(params.path) or nil
  504. elseif result.transport == "kcp" then
  505. result.kcp_guise = params.headerType or "none"
  506. result.seed = params.seed
  507. result.mtu = 1350
  508. result.tti = 50
  509. result.uplink_capacity = 5
  510. result.downlink_capacity = 20
  511. result.read_buffer_size = 2
  512. result.write_buffer_size = 2
  513. elseif result.transport == "quic" then
  514. result.quic_guise = params.headerType or "none"
  515. result.quic_security = params.quicSecurity or "none"
  516. result.quic_key = params.key
  517. elseif result.transport == "grpc" then
  518. result.serviceName = params.serviceName
  519. result.grpc_mode = params.mode or "gun"
  520. elseif result.transport == "tcp" or result.transport == "raw" then
  521. result.tcp_guise = params.headerType or "none"
  522. if result.tcp_guise == "http" then
  523. result.tcp_host = params.host and UrlDecode(params.host) or nil
  524. result.tcp_path = params.path and UrlDecode(params.path) or nil
  525. end
  526. end
  527. end
  528. if not result.alias then
  529. if result.server and result.server_port then
  530. result.alias = result.server .. ':' .. result.server_port
  531. else
  532. result.alias = "NULL"
  533. end
  534. end
  535. -- alias 不参与 hashkey 计算
  536. local alias = result.alias
  537. result.alias = nil
  538. local switch_enable = result.switch_enable
  539. result.switch_enable = nil
  540. result.hashkey = md5(jsonStringify(result))
  541. result.alias = alias
  542. result.switch_enable = switch_enable
  543. return result
  544. end
  545. -- wget
  546. local function wget(url)
  547. local stdout = luci.sys.exec('wget -q --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" --no-check-certificate -O- "' .. url .. '"')
  548. return trim(stdout)
  549. end
  550. local function check_filer(result)
  551. do
  552. -- 过滤的关键词列表
  553. local filter_word = split(filter_words, "/")
  554. -- 保留的关键词列表
  555. local check_save = false
  556. if save_words ~= nil and save_words ~= "" and save_words ~= "NULL" then
  557. check_save = true
  558. end
  559. local save_word = split(save_words, "/")
  560. -- 检查结果
  561. local filter_result = false
  562. local save_result = true
  563. -- 检查是否存在过滤关键词
  564. for i, v in pairs(filter_word) do
  565. if tostring(result.alias):find(v, nil, true) then
  566. filter_result = true
  567. end
  568. end
  569. -- 检查是否打开了保留关键词检查,并且进行过滤
  570. if check_save == true then
  571. for i, v in pairs(save_word) do
  572. if tostring(result.alias):find(v, nil, true) then
  573. save_result = false
  574. end
  575. end
  576. else
  577. save_result = false
  578. end
  579. -- 不等时返回
  580. if filter_result == true or save_result == true then
  581. return true
  582. else
  583. return false
  584. end
  585. end
  586. end
  587. local execute = function()
  588. -- exec
  589. do
  590. if proxy == '0' then -- 不使用代理更新的话先暂停
  591. log('服务正在暂停')
  592. luci.sys.init.stop(name)
  593. end
  594. for k, url in ipairs(subscribe_url) do
  595. local raw = wget(url)
  596. if #raw > 0 then
  597. local nodes, szType
  598. local groupHash = md5(url)
  599. cache[groupHash] = {}
  600. tinsert(nodeResult, {})
  601. local index = #nodeResult
  602. -- SSD 似乎是这种格式 ssd:// 开头的
  603. if raw:find('ssd://') then
  604. szType = 'ssd'
  605. local nEnd = select(2, raw:find('ssd://'))
  606. nodes = base64Decode(raw:sub(nEnd + 1, #raw))
  607. nodes = jsonParse(nodes)
  608. local extra = {airport = nodes.airport, port = nodes.port, encryption = nodes.encryption, password = nodes.password}
  609. local servers = {}
  610. -- SS里面包着 干脆直接这样
  611. for _, server in ipairs(nodes.servers) do
  612. tinsert(servers, setmetatable(server, {__index = extra}))
  613. end
  614. nodes = servers
  615. -- SS SIP008 直接使用 Json 格式
  616. elseif jsonParse(raw) then
  617. nodes = jsonParse(raw).servers or jsonParse(raw)
  618. if nodes[1].server and nodes[1].method then
  619. szType = 'sip008'
  620. end
  621. else
  622. -- ssd 外的格式
  623. nodes = split(base64Decode(raw):gsub(" ", "_"), "\n")
  624. end
  625. for _, v in ipairs(nodes) do
  626. if v then
  627. local result
  628. if szType then
  629. result = processData(szType, v)
  630. elseif not szType then
  631. local node = trim(v)
  632. local dat = split(node, "://")
  633. if dat and dat[1] and dat[2] then
  634. local dat3 = ""
  635. if dat[3] then
  636. dat3 = "://" .. dat[3]
  637. end
  638. if dat[1] == 'ss' or dat[1] == 'trojan' then
  639. result = processData(dat[1], dat[2] .. dat3)
  640. else
  641. result = processData(dat[1], base64Decode(dat[2]))
  642. end
  643. end
  644. else
  645. log('跳过未知类型: ' .. szType)
  646. end
  647. -- log(result)
  648. if result then
  649. -- 中文做地址的 也没有人拿中文域名搞,就算中文域也有Puny Code SB 机场
  650. if not result.server or not result.server_port or result.alias == "NULL" or check_filer(result) or result.server:match("[^0-9a-zA-Z%-_%.%s]") or cache[groupHash][result.hashkey] then
  651. log('丢弃无效节点: ' .. result.type .. ' 节点, ' .. result.alias)
  652. else
  653. -- log('成功解析: ' .. result.type ..' 节点, ' .. result.alias)
  654. result.grouphashkey = groupHash
  655. tinsert(nodeResult[index], result)
  656. cache[groupHash][result.hashkey] = nodeResult[index][#nodeResult[index]]
  657. end
  658. end
  659. end
  660. end
  661. log('成功解析节点数量: ' .. #nodes)
  662. else
  663. log(url .. ': 获取内容为空')
  664. end
  665. end
  666. end
  667. -- diff
  668. do
  669. if next(nodeResult) == nil then
  670. log("更新失败,没有可用的节点信息")
  671. if proxy == '0' then
  672. luci.sys.init.start(name)
  673. log('订阅失败, 恢复服务')
  674. end
  675. return
  676. end
  677. local add, del = 0, 0
  678. ucic:foreach(name, uciType, function(old)
  679. if old.grouphashkey or old.hashkey then -- 没有 hash 的不参与删除
  680. if not nodeResult[old.grouphashkey] or not nodeResult[old.grouphashkey][old.hashkey] then
  681. ucic:delete(name, old['.name'])
  682. del = del + 1
  683. else
  684. local dat = nodeResult[old.grouphashkey][old.hashkey]
  685. ucic:tset(name, old['.name'], dat)
  686. -- 标记一下
  687. setmetatable(nodeResult[old.grouphashkey][old.hashkey], {__index = {_ignore = true}})
  688. end
  689. else
  690. if not old.alias then
  691. if old.server or old.server_port then
  692. old.alias = old.server .. ':' .. old.server_port
  693. log('忽略手动添加的节点: ' .. old.alias)
  694. else
  695. ucic:delete(name, old['.name'])
  696. end
  697. else
  698. log('忽略手动添加的节点: ' .. old.alias)
  699. end
  700. end
  701. end)
  702. for k, v in ipairs(nodeResult) do
  703. for kk, vv in ipairs(v) do
  704. if not vv._ignore then
  705. local section = ucic:add(name, uciType)
  706. ucic:tset(name, section, vv)
  707. ucic:set(name, section, "switch_enable", switch)
  708. add = add + 1
  709. end
  710. end
  711. end
  712. ucic:commit(name)
  713. -- 如果原有服务器节点已经不见了就尝试换为第一个节点
  714. local globalServer = ucic:get_first(name, 'global', 'global_server', '')
  715. if globalServer ~= "nil" then
  716. local firstServer = ucic:get_first(name, uciType)
  717. if firstServer then
  718. if not ucic:get(name, globalServer) then
  719. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &")
  720. ucic:commit(name)
  721. ucic:set(name, ucic:get_first(name, 'global'), 'global_server', ucic:get_first(name, uciType))
  722. ucic:commit(name)
  723. log('当前主服务器节点已被删除,正在自动更换为第一个节点。')
  724. luci.sys.call("/etc/init.d/" .. name .. " start > /dev/null 2>&1 &")
  725. else
  726. log('维持当前主服务器节点。')
  727. luci.sys.call("/etc/init.d/" .. name .. " restart > /dev/null 2>&1 &")
  728. end
  729. else
  730. log('没有服务器节点了,停止服务')
  731. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &")
  732. end
  733. end
  734. log('新增节点数量: ' .. add, '删除节点数量: ' .. del)
  735. log('订阅更新成功')
  736. end
  737. end
  738. if subscribe_url and #subscribe_url > 0 then
  739. xpcall(execute, function(e)
  740. log(e)
  741. log(debug.traceback())
  742. log('发生错误, 正在恢复服务')
  743. local firstServer = ucic:get_first(name, uciType)
  744. if firstServer then
  745. luci.sys.call("/etc/init.d/" .. name .. " restart > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早
  746. log('重启服务成功')
  747. else
  748. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早
  749. log('停止服务成功')
  750. end
  751. end)
  752. end