subscribe.lua 22 KB

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