subscribe.lua 19 KB

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