subscribe.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 cache = {}
  19. local nodeResult = setmetatable({}, {__index = cache}) -- update result
  20. local name = 'shadowsocksr'
  21. local uciType = 'servers'
  22. local ucic = luci.model.uci.cursor()
  23. local proxy = ucic:get_first(name, 'server_subscribe', 'proxy', '0')
  24. local switch = ucic:get_first(name, 'server_subscribe', 'switch', '1')
  25. local subscribe_url = ucic:get_first(name, 'server_subscribe', 'subscribe_url', {})
  26. local filter_words = ucic:get_first(name, 'server_subscribe', 'filter_words', '过期时间/剩余流量')
  27. local save_words = ucic:get_first(name, 'server_subscribe', 'save_words', '')
  28. local packet_encoding = luci.model.ipkg.installed("sagernet-core") and ucic:get_first(name, 'global', 'default_packet_encoding', 'xudp') or nil
  29. local v2_ss = luci.sys.exec('type -t -p ss-redir sslocal') ~= "" and "ss" or "v2ray"
  30. local v2_ssr = luci.sys.exec('type -t -p ssr-redir') ~= "" and "ssr" 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 = v2_ssr
  148. result.v2ray_protocol = (v2_ssr == "v2ray") and "shadowsocksr" or nil
  149. result.server = hostInfo[1]
  150. result.server_port = hostInfo[2]
  151. result.protocol = hostInfo[3]
  152. result.encrypt_method = hostInfo[4]
  153. result.obfs = hostInfo[5]
  154. result.password = base64Decode(hostInfo[6])
  155. local params = {}
  156. for _, v in pairs(split(dat[2], '&')) do
  157. local t = split(v, '=')
  158. params[t[1]] = t[2]
  159. end
  160. result.obfs_param = base64Decode(params.obfsparam)
  161. result.protocol_param = base64Decode(params.protoparam)
  162. local group = base64Decode(params.group)
  163. if group then
  164. result.alias = "[" .. group .. "] "
  165. end
  166. result.alias = result.alias .. base64Decode(params.remarks)
  167. elseif szType == 'vmess' then
  168. local info = jsonParse(content)
  169. result.type = 'v2ray'
  170. result.v2ray_protocol = 'vmess'
  171. result.server = info.add
  172. result.server_port = info.port
  173. result.transport = info.net
  174. result.vmess_id = info.id
  175. result.alias = info.ps
  176. result.packet_encoding = packet_encoding
  177. -- result.mux = 1
  178. -- result.concurrency = 8
  179. if info.net == 'ws' then
  180. result.ws_host = info.host
  181. result.ws_path = info.path
  182. end
  183. if info.net == 'h2' then
  184. result.h2_host = info.host
  185. result.h2_path = info.path
  186. end
  187. if info.net == 'tcp' then
  188. if info.type and info.type ~= "http" then
  189. info.type = "none"
  190. end
  191. result.tcp_guise = info.type
  192. result.http_host = info.host
  193. result.http_path = info.path
  194. end
  195. if info.net == 'kcp' then
  196. result.kcp_guise = info.type
  197. result.mtu = 1350
  198. result.tti = 50
  199. result.uplink_capacity = 5
  200. result.downlink_capacity = 20
  201. result.read_buffer_size = 2
  202. result.write_buffer_size = 2
  203. end
  204. if info.net == 'grpc' then
  205. if info.path then
  206. result.serviceName = info.path
  207. elseif info.serviceName then
  208. result.serviceName = info.serviceName
  209. end
  210. end
  211. if info.net == 'quic' then
  212. result.quic_guise = info.type
  213. result.quic_key = info.key
  214. result.quic_security = info.securty
  215. end
  216. if info.security then
  217. result.security = info.security
  218. end
  219. if info.tls == "tls" or info.tls == "1" then
  220. result.tls = "1"
  221. if info.host then
  222. result.tls_host = info.host
  223. elseif info.sni then
  224. result.tls_host = info.sni
  225. end
  226. result.insecure = 1
  227. else
  228. result.tls = "0"
  229. end
  230. -- https://www.v2fly.org/config/protocols/vmess.html#vmess-md5-认证信息-淘汰机制
  231. if info.aid and (tonumber(info.aid) > 0) then
  232. result.server = nil
  233. end
  234. elseif szType == "ss" then
  235. local idx_sp = 0
  236. local alias = ""
  237. if content:find("#") then
  238. idx_sp = content:find("#")
  239. alias = content:sub(idx_sp + 1, -1)
  240. end
  241. local info = content:sub(1, idx_sp - 1)
  242. local hostInfo = split(base64Decode(info), "@")
  243. local host = split(hostInfo[2], ":")
  244. local userinfo = base64Decode(hostInfo[1])
  245. local method = userinfo:sub(1, userinfo:find(":") - 1)
  246. local password = userinfo:sub(userinfo:find(":") + 1, #userinfo)
  247. result.alias = UrlDecode(alias)
  248. result.type = v2_ss
  249. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  250. result.encrypt_method_ss = method
  251. result.password = password
  252. result.server = host[1]
  253. if host[2]:find("/%?") then
  254. local query = split(host[2], "/%?")
  255. result.server_port = query[1]
  256. local params = {}
  257. for _, v in pairs(split(query[2], '&')) do
  258. local t = split(v, '=')
  259. params[t[1]] = t[2]
  260. end
  261. if params.plugin then
  262. local plugin_info = UrlDecode(params.plugin)
  263. local idx_pn = plugin_info:find(";")
  264. if idx_pn then
  265. result.plugin = plugin_info:sub(1, idx_pn - 1)
  266. result.plugin_opts = plugin_info:sub(idx_pn + 1, #plugin_info)
  267. else
  268. result.plugin = plugin_info
  269. end
  270. -- 部分机场下发的插件名为 simple-obfs,这里应该改为 obfs-local
  271. if result.plugin == "simple-obfs" then
  272. result.plugin = "obfs-local"
  273. end
  274. end
  275. else
  276. result.server_port = host[2]:gsub("/","")
  277. end
  278. if not checkTabValue(encrypt_methods_ss)[method] then
  279. -- 1202 年了还不支持 SS AEAD 的屑机场
  280. result.server = nil
  281. end
  282. elseif szType == "sip008" then
  283. result.type = v2_ss
  284. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  285. result.server = content.server
  286. result.server_port = content.server_port
  287. result.password = content.password
  288. result.encrypt_method_ss = content.method
  289. result.plugin = content.plugin
  290. result.plugin_opts = content.plugin_opts
  291. result.alias = content.remarks
  292. if not checkTabValue(encrypt_methods_ss)[content.method] then
  293. result.server = nil
  294. end
  295. elseif szType == "ssd" then
  296. result.type = v2_ss
  297. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  298. result.server = content.server
  299. result.server_port = content.port
  300. result.password = content.password
  301. result.encrypt_method_ss = content.method
  302. result.plugin_opts = content.plugin_options
  303. result.alias = "[" .. content.airport .. "] " .. content.remarks
  304. if content.plugin == "simple-obfs" then
  305. result.plugin = "obfs-local"
  306. else
  307. result.plugin = content.plugin
  308. end
  309. if not checkTabValue(encrypt_methods_ss)[content.encryption] then
  310. result.server = nil
  311. end
  312. elseif szType == "trojan" then
  313. local idx_sp = 0
  314. local alias = ""
  315. if content:find("#") then
  316. idx_sp = content:find("#")
  317. alias = content:sub(idx_sp + 1, -1)
  318. end
  319. local info = content:sub(1, idx_sp - 1)
  320. local hostInfo = split(info, "@")
  321. local host = split(hostInfo[2], ":")
  322. local userinfo = hostInfo[1]
  323. local password = userinfo
  324. result.alias = UrlDecode(alias)
  325. result.type = v2_tj
  326. result.v2ray_protocol = "trojan"
  327. result.server = host[1]
  328. -- 按照官方的建议 默认验证ssl证书
  329. result.insecure = "0"
  330. result.tls = "1"
  331. if host[2]:find("?") then
  332. local query = split(host[2], "?")
  333. result.server_port = query[1]
  334. local params = {}
  335. for _, v in pairs(split(query[2], '&')) do
  336. local t = split(v, '=')
  337. params[t[1]] = t[2]
  338. end
  339. if params.sni then
  340. -- 未指定peer(sni)默认使用remote addr
  341. result.tls_host = params.sni
  342. end
  343. else
  344. result.server_port = host[2]
  345. end
  346. result.password = password
  347. elseif szType == "vless" then
  348. local idx_sp = 0
  349. local alias = ""
  350. if content:find("#") then
  351. idx_sp = content:find("#")
  352. alias = content:sub(idx_sp + 1, -1)
  353. end
  354. local info = content:sub(1, idx_sp - 1)
  355. local hostInfo = split(info, "@")
  356. local host = split(hostInfo[2], ":")
  357. local uuid = hostInfo[1]
  358. if host[2]:find("?") then
  359. local query = split(host[2], "?")
  360. local params = {}
  361. for _, v in pairs(split(UrlDecode(query[2]), '&')) do
  362. local t = split(v, '=')
  363. params[t[1]] = t[2]
  364. end
  365. result.alias = UrlDecode(alias)
  366. result.type = 'v2ray'
  367. result.v2ray_protocol = 'vless'
  368. result.server = host[1]
  369. result.server_port = query[1]
  370. result.vmess_id = uuid
  371. result.vless_encryption = params.encryption or "none"
  372. result.transport = params.type and (params.type == 'http' and 'h2' or params.type) or "tcp"
  373. result.packet_encoding = packet_encoding
  374. if not params.type or params.type == "tcp" then
  375. if params.security == "xtls" then
  376. result.xtls = "1"
  377. result.tls_host = params.sni
  378. result.vless_flow = params.flow
  379. else
  380. result.xtls = "0"
  381. end
  382. end
  383. if params.type == 'ws' then
  384. result.ws_host = params.host
  385. result.ws_path = params.path or "/"
  386. end
  387. if params.type == 'http' then
  388. result.h2_host = params.host
  389. result.h2_path = params.path or "/"
  390. end
  391. if params.type == 'kcp' then
  392. result.kcp_guise = params.headerType or "none"
  393. result.mtu = 1350
  394. result.tti = 50
  395. result.uplink_capacity = 5
  396. result.downlink_capacity = 20
  397. result.read_buffer_size = 2
  398. result.write_buffer_size = 2
  399. result.seed = params.seed
  400. end
  401. if params.type == 'quic' then
  402. result.quic_guise = params.headerType or "none"
  403. result.quic_key = params.key
  404. result.quic_security = params.quicSecurity or "none"
  405. end
  406. if params.type == 'grpc' then
  407. result.serviceName = params.serviceName
  408. end
  409. if params.security == "tls" then
  410. result.tls = "1"
  411. result.tls_host = params.sni
  412. else
  413. result.tls = "0"
  414. end
  415. else
  416. result.server_port = host[2]
  417. end
  418. end
  419. if not result.alias then
  420. if result.server and result.server_port then
  421. result.alias = result.server .. ':' .. result.server_port
  422. else
  423. result.alias = "NULL"
  424. end
  425. end
  426. -- alias 不参与 hashkey 计算
  427. local alias = result.alias
  428. result.alias = nil
  429. local switch_enable = result.switch_enable
  430. result.switch_enable = nil
  431. result.hashkey = md5(jsonStringify(result))
  432. result.alias = alias
  433. result.switch_enable = switch_enable
  434. return result
  435. end
  436. -- wget
  437. local function wget(url)
  438. local stdout = luci.sys.exec('uclient-fetch -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 .. '"')
  439. return trim(stdout)
  440. end
  441. local function check_filer(result)
  442. do
  443. -- 过滤的关键词列表
  444. local filter_word = split(filter_words, "/")
  445. -- 保留的关键词列表
  446. local check_save = false
  447. if save_words ~= nil and save_words ~= "" and save_words ~= "NULL" then
  448. check_save = true
  449. end
  450. local save_word = split(save_words, "/")
  451. -- 检查结果
  452. local filter_result = false
  453. local save_result = true
  454. -- 检查是否存在过滤关键词
  455. for i, v in pairs(filter_word) do
  456. if tostring(result.alias):find(v, nil, true) then
  457. filter_result = true
  458. end
  459. end
  460. -- 检查是否打开了保留关键词检查,并且进行过滤
  461. if check_save == true then
  462. for i, v in pairs(save_word) do
  463. if tostring(result.alias):find(v, nil, true) then
  464. save_result = false
  465. end
  466. end
  467. else
  468. save_result = false
  469. end
  470. -- 不等时返回
  471. if filter_result == true or save_result == true then
  472. return true
  473. else
  474. return false
  475. end
  476. end
  477. end
  478. local execute = function()
  479. -- exec
  480. do
  481. if proxy == '0' then -- 不使用代理更新的话先暂停
  482. log('服务正在暂停')
  483. luci.sys.init.stop(name)
  484. end
  485. for k, url in ipairs(subscribe_url) do
  486. local raw = wget(url)
  487. if #raw > 0 then
  488. local nodes, szType
  489. local groupHash = md5(url)
  490. cache[groupHash] = {}
  491. tinsert(nodeResult, {})
  492. local index = #nodeResult
  493. -- SSD 似乎是这种格式 ssd:// 开头的
  494. if raw:find('ssd://') then
  495. szType = 'ssd'
  496. local nEnd = select(2, raw:find('ssd://'))
  497. nodes = base64Decode(raw:sub(nEnd + 1, #raw))
  498. nodes = jsonParse(nodes)
  499. local extra = {airport = nodes.airport, port = nodes.port, encryption = nodes.encryption, password = nodes.password}
  500. local servers = {}
  501. -- SS里面包着 干脆直接这样
  502. for _, server in ipairs(nodes.servers) do
  503. tinsert(servers, setmetatable(server, {__index = extra}))
  504. end
  505. nodes = servers
  506. -- SS SIP008 直接使用 Json 格式
  507. elseif jsonParse(raw) then
  508. nodes = jsonParse(raw).servers or jsonParse(raw)
  509. if nodes[1].server and nodes[1].method then
  510. szType = 'sip008'
  511. end
  512. else
  513. -- ssd 外的格式
  514. nodes = split(base64Decode(raw):gsub(" ", "_"), "\n")
  515. end
  516. for _, v in ipairs(nodes) do
  517. if v then
  518. local result
  519. if szType then
  520. result = processData(szType, v)
  521. elseif not szType then
  522. local node = trim(v)
  523. local dat = split(node, "://")
  524. if dat and dat[1] and dat[2] then
  525. local dat3 = ""
  526. if dat[3] then
  527. dat3 = "://" .. dat[3]
  528. end
  529. if dat[1] == 'ss' or dat[1] == 'trojan' then
  530. result = processData(dat[1], dat[2] .. dat3)
  531. else
  532. result = processData(dat[1], base64Decode(dat[2]))
  533. end
  534. end
  535. else
  536. log('跳过未知类型: ' .. szType)
  537. end
  538. -- log(result)
  539. if result then
  540. -- 中文做地址的 也没有人拿中文域名搞,就算中文域也有Puny Code SB 机场
  541. 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
  542. log('丢弃无效节点: ' .. result.type .. ' 节点, ' .. result.alias)
  543. else
  544. -- log('成功解析: ' .. result.type ..' 节点, ' .. result.alias)
  545. result.grouphashkey = groupHash
  546. tinsert(nodeResult[index], result)
  547. cache[groupHash][result.hashkey] = nodeResult[index][#nodeResult[index]]
  548. end
  549. end
  550. end
  551. end
  552. log('成功解析节点数量: ' .. #nodes)
  553. else
  554. log(url .. ': 获取内容为空')
  555. end
  556. end
  557. end
  558. -- diff
  559. do
  560. if next(nodeResult) == nil then
  561. log("更新失败,没有可用的节点信息")
  562. if proxy == '0' then
  563. luci.sys.init.start(name)
  564. log('订阅失败, 恢复服务')
  565. end
  566. return
  567. end
  568. local add, del = 0, 0
  569. ucic:foreach(name, uciType, function(old)
  570. if old.grouphashkey or old.hashkey then -- 没有 hash 的不参与删除
  571. if not nodeResult[old.grouphashkey] or not nodeResult[old.grouphashkey][old.hashkey] then
  572. ucic:delete(name, old['.name'])
  573. del = del + 1
  574. else
  575. local dat = nodeResult[old.grouphashkey][old.hashkey]
  576. ucic:tset(name, old['.name'], dat)
  577. -- 标记一下
  578. setmetatable(nodeResult[old.grouphashkey][old.hashkey], {__index = {_ignore = true}})
  579. end
  580. else
  581. if not old.alias then
  582. if old.server or old.server_port then
  583. old.alias = old.server .. ':' .. old.server_port
  584. log('忽略手动添加的节点: ' .. old.alias)
  585. else
  586. ucic:delete(name, old['.name'])
  587. end
  588. else
  589. log('忽略手动添加的节点: ' .. old.alias)
  590. end
  591. end
  592. end)
  593. for k, v in ipairs(nodeResult) do
  594. for kk, vv in ipairs(v) do
  595. if not vv._ignore then
  596. local section = ucic:add(name, uciType)
  597. ucic:tset(name, section, vv)
  598. ucic:set(name, section, "switch_enable", switch)
  599. add = add + 1
  600. end
  601. end
  602. end
  603. ucic:commit(name)
  604. -- 如果原有服务器节点已经不见了就尝试换为第一个节点
  605. local globalServer = ucic:get_first(name, 'global', 'global_server', '')
  606. if globalServer ~= "nil" then
  607. local firstServer = ucic:get_first(name, uciType)
  608. if firstServer then
  609. if not ucic:get(name, globalServer) then
  610. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &")
  611. ucic:commit(name)
  612. ucic:set(name, ucic:get_first(name, 'global'), 'global_server', ucic:get_first(name, uciType))
  613. ucic:commit(name)
  614. log('当前主服务器节点已被删除,正在自动更换为第一个节点。')
  615. luci.sys.call("/etc/init.d/" .. name .. " start > /dev/null 2>&1 &")
  616. else
  617. log('维持当前主服务器节点。')
  618. luci.sys.call("/etc/init.d/" .. name .. " restart > /dev/null 2>&1 &")
  619. end
  620. else
  621. log('没有服务器节点了,停止服务')
  622. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &")
  623. end
  624. end
  625. log('新增节点数量: ' .. add, '删除节点数量: ' .. del)
  626. log('订阅更新成功')
  627. end
  628. end
  629. if subscribe_url and #subscribe_url > 0 then
  630. xpcall(execute, function(e)
  631. log(e)
  632. log(debug.traceback())
  633. log('发生错误, 正在恢复服务')
  634. local firstServer = ucic:get_first(name, uciType)
  635. if firstServer then
  636. luci.sys.call("/etc/init.d/" .. name .. " restart > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早
  637. log('重启服务成功')
  638. else
  639. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早
  640. log('停止服务成功')
  641. end
  642. end)
  643. end