subscribe.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 sslocal ss-redir') ~= "" and "ss" or "v2ray"
  31. local ss_variant = luci.sys.exec('type -t -p sslocal') ~= "" and "isSSRust" or luci.sys.exec('type -t -p ss-redir') ~= "" and "isSSLibev"
  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 = 'ssr'
  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. if info.net == "tcp" then
  174. info.net = "raw"
  175. end
  176. result.transport = info.net
  177. result.alter_id = info.aid
  178. result.vmess_id = info.id
  179. result.alias = info.ps
  180. -- result.mux = 1
  181. -- result.concurrency = 8
  182. if info.net == 'ws' then
  183. result.ws_host = info.host
  184. result.ws_path = info.path
  185. end
  186. if info.net == 'httpupgrade' then
  187. result.httpupgrade_host = info.host
  188. result.httpupgrade_path = info.path
  189. end
  190. if info.net == 'splithttp' then
  191. result.splithttp_host = info.host
  192. result.splithttp_path = info.path
  193. end
  194. if info.net == 'xhttp' then
  195. result.xhttp_mode = info.mode
  196. result.xhttp_host = info.host
  197. result.xhttp_path = info.path
  198. -- 检查 extra 参数是否存在且非空
  199. result.enable_xhttp_extra = (info.extra and info.extra ~= "") and "1" or nil
  200. result.xhttp_extra = (info.extra and info.extra ~= "") and info.extra or nil
  201. -- 尝试解析 JSON 数据
  202. local success, Data = pcall(jsonParse, info.extra)
  203. if success and Data then
  204. local address = (Data.extra and Data.extra.downloadSettings and Data.extra.downloadSettings.address)
  205. or (Data.downloadSettings and Data.downloadSettings.address)
  206. result.download_address = address and address ~= "" and address or nil
  207. else
  208. -- 如果解析失败,清空下载地址
  209. result.download_address = nil
  210. end
  211. end
  212. if info.net == 'h2' then
  213. result.h2_host = info.host
  214. result.h2_path = info.path
  215. end
  216. if info.net == 'raw' or info.net == 'tcp' then
  217. if info.type and info.type ~= "http" then
  218. info.type = "none"
  219. end
  220. result.tcp_guise = info.type
  221. result.http_host = info.host
  222. result.http_path = info.path
  223. end
  224. if info.net == 'kcp' then
  225. result.kcp_guise = info.type
  226. result.mtu = 1350
  227. result.tti = 50
  228. result.uplink_capacity = 5
  229. result.downlink_capacity = 20
  230. result.read_buffer_size = 2
  231. result.write_buffer_size = 2
  232. end
  233. if info.net == 'grpc' then
  234. if info.path then
  235. result.serviceName = info.path
  236. elseif info.serviceName then
  237. result.serviceName = info.serviceName
  238. end
  239. end
  240. if info.net == 'quic' then
  241. result.quic_guise = info.type
  242. result.quic_key = info.key
  243. result.quic_security = info.securty
  244. end
  245. if info.security then
  246. result.security = info.security
  247. end
  248. if info.tls == "tls" or info.tls == "1" then
  249. result.tls = "1"
  250. if info.alpn and info.alpn ~= "" then
  251. result.xhttp_alpn = info.alpn
  252. end
  253. if info.sni and info.sni ~= "" then
  254. result.tls_host = info.sni
  255. elseif info.host then
  256. result.tls_host = info.host
  257. end
  258. result.insecure = allow_insecure
  259. else
  260. result.tls = "0"
  261. end
  262. elseif szType == "ss" then
  263. local idx_sp = 0
  264. local alias = ""
  265. if content:find("#") then
  266. idx_sp = content:find("#")
  267. alias = content:sub(idx_sp + 1, -1)
  268. end
  269. local info = content:sub(1, idx_sp - 1)
  270. local hostInfo = split(base64Decode(info), "@")
  271. local host = split(hostInfo[2], ":")
  272. local userinfo = base64Decode(hostInfo[1])
  273. local method = userinfo:sub(1, userinfo:find(":") - 1)
  274. local password = userinfo:sub(userinfo:find(":") + 1, #userinfo)
  275. result.alias = UrlDecode(alias)
  276. result.type = v2_ss
  277. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  278. result.ss_variant = ss_variant
  279. result.encrypt_method_ss = method
  280. result.password = password
  281. result.server = host[1]
  282. if host[2]:find("/%?") then
  283. local query = split(host[2], "/%?")
  284. result.server_port = query[1]
  285. local params = {}
  286. for _, v in pairs(split(query[2], '&')) do
  287. local t = split(v, '=')
  288. params[t[1]] = t[2]
  289. end
  290. if params.plugin then
  291. local plugin_info = UrlDecode(params.plugin)
  292. local idx_pn = plugin_info:find(";")
  293. if idx_pn then
  294. result.plugin = plugin_info:sub(1, idx_pn - 1)
  295. result.plugin_opts = plugin_info:sub(idx_pn + 1, #plugin_info)
  296. else
  297. result.plugin = plugin_info
  298. end
  299. -- 部分机场下发的插件名为 simple-obfs,这里应该改为 obfs-local
  300. if result.plugin == "simple-obfs" then
  301. result.plugin = "obfs-local"
  302. end
  303. -- 如果插件不為 none,確保 enable_plugin 為 1
  304. if result.plugin ~= "none" and result.plugin ~= "" then
  305. result.enable_plugin = 1
  306. end
  307. end
  308. else
  309. result.server_port = host[2]:gsub("/","")
  310. end
  311. if not checkTabValue(encrypt_methods_ss)[method] then
  312. -- 1202 年了还不支持 SS AEAD 的屑机场
  313. result.server = nil
  314. end
  315. elseif szType == "sip008" then
  316. result.type = v2_ss
  317. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  318. result.server = content.server
  319. result.server_port = content.server_port
  320. result.password = content.password
  321. result.encrypt_method_ss = content.method
  322. result.plugin = content.plugin
  323. result.plugin_opts = content.plugin_opts
  324. result.alias = content.remarks
  325. if not checkTabValue(encrypt_methods_ss)[content.method] then
  326. result.server = nil
  327. end
  328. elseif szType == "ssd" then
  329. result.type = v2_ss
  330. result.v2ray_protocol = (v2_ss == "v2ray") and "shadowsocks" or nil
  331. result.server = content.server
  332. result.server_port = content.port
  333. result.password = content.password
  334. result.encrypt_method_ss = content.method
  335. result.plugin_opts = content.plugin_options
  336. result.alias = "[" .. content.airport .. "] " .. content.remarks
  337. if content.plugin == "simple-obfs" then
  338. result.plugin = "obfs-local"
  339. else
  340. result.plugin = content.plugin
  341. end
  342. if not checkTabValue(encrypt_methods_ss)[content.encryption] then
  343. result.server = nil
  344. end
  345. elseif szType == "trojan" then
  346. local params = {}
  347. local idx_sp = 0
  348. local alias = ""
  349. if content:find("#") then
  350. idx_sp = content:find("#")
  351. alias = content:sub(idx_sp + 1, -1)
  352. end
  353. local info = content:sub(1, idx_sp - 1)
  354. local hostInfo = split(info, "@")
  355. local userinfo = hostInfo[1]
  356. local password = userinfo
  357. -- 分离服务器地址和端口
  358. local host = split(hostInfo[2], ":")
  359. local server = host[1]
  360. local port = host[2]
  361. result.alias = UrlDecode(alias)
  362. result.type = v2_tj
  363. result.v2ray_protocol = "trojan"
  364. result.server = server
  365. result.password = password
  366. -- 按照官方的建议 默认验证ssl证书
  367. result.insecure = "0"
  368. result.tls = "1"
  369. if port:find("?") then
  370. local query = split(port, "?")
  371. result.server_port = query[1]
  372. for _, v in pairs(split(query[2], '&')) do
  373. local t = split(v, '=')
  374. params[t[1]] = t[2]
  375. end
  376. if params.alpn then
  377. -- 处理 alpn 参数
  378. result.xhttp_alpn = params.alpn
  379. end
  380. if params.sni then
  381. -- 未指定peer(sni)默认使用remote addr
  382. result.tls_host = params.sni
  383. end
  384. if params.allowInsecure then
  385. -- 处理 insecure 参数
  386. result.insecure = params.allowInsecure
  387. end
  388. else
  389. result.server_port = port
  390. end
  391. if v2_tj ~= "trojan" then
  392. if params.fp then
  393. -- 处理 fingerprint 参数
  394. result.fingerprint = params.fp
  395. end
  396. -- 处理传输协议
  397. result.transport = params.type or "tcp" -- 默认传输协议为 tcp
  398. if result.transport == "tcp" then
  399. result.transport = "raw"
  400. end
  401. if result.transport == "ws" then
  402. result.ws_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  403. result.ws_path = params.path and UrlDecode(params.path) or "/"
  404. elseif result.transport == "httpupgrade" then
  405. result.httpupgrade_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  406. result.httpupgrade_path = params.path and UrlDecode(params.path) or "/"
  407. elseif result.transport == "splithttp" then
  408. result.splithttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  409. result.splithttp_path = params.path and UrlDecode(params.path) or "/"
  410. elseif result.transport == "xhttp" then
  411. result.xhttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  412. result.xhttp_mode = params.mode or "auto"
  413. result.xhttp_path = params.path and UrlDecode(params.path) or "/"
  414. -- 检查 extra 参数是否存在且非空
  415. result.enable_xhttp_extra = (params.extra and params.extra ~= "") and "1" or nil
  416. result.xhttp_extra = (params.extra and params.extra ~= "") and params.extra or nil
  417. -- 尝试解析 JSON 数据
  418. local success, Data = pcall(jsonParse, params.extra)
  419. if success and Data then
  420. local address = (Data.extra and Data.extra.downloadSettings and Data.extra.downloadSettings.address)
  421. or (Data.downloadSettings and Data.downloadSettings.address)
  422. result.download_address = address and address ~= "" and address or nil
  423. else
  424. -- 如果解析失败,清空下载地址
  425. result.download_address = nil
  426. end
  427. elseif result.transport == "http" or result.transport == "h2" then
  428. result.transport = "h2"
  429. result.h2_host = params.host and UrlDecode(params.host) or nil
  430. result.h2_path = params.path and UrlDecode(params.path) or nil
  431. elseif result.transport == "kcp" then
  432. result.kcp_guise = params.headerType or "none"
  433. result.seed = params.seed
  434. result.mtu = 1350
  435. result.tti = 50
  436. result.uplink_capacity = 5
  437. result.downlink_capacity = 20
  438. result.read_buffer_size = 2
  439. result.write_buffer_size = 2
  440. elseif result.transport == "quic" then
  441. result.quic_guise = params.headerType or "none"
  442. result.quic_security = params.quicSecurity or "none"
  443. result.quic_key = params.key
  444. elseif result.transport == "grpc" then
  445. result.serviceName = params.serviceName
  446. result.grpc_mode = params.mode or "gun"
  447. elseif result.transport == "tcp" or result.transport == "raw" then
  448. result.tcp_guise = params.headerType and params.headerType ~= "" and params.headerType or "none"
  449. if result.tcp_guise == "http" then
  450. result.tcp_host = params.host and UrlDecode(params.host) or nil
  451. result.tcp_path = params.path and UrlDecode(params.path) or nil
  452. end
  453. end
  454. end
  455. elseif szType == "vless" then
  456. local url = URL.parse("http://" .. content)
  457. local params = url.query
  458. result.alias = url.fragment and UrlDecode(url.fragment) or nil
  459. result.type = "v2ray"
  460. result.v2ray_protocol = "vless"
  461. result.server = url.host
  462. result.server_port = url.port
  463. result.vmess_id = url.user
  464. result.vless_encryption = params.encryption or "none"
  465. result.transport = params.type or "tcp"
  466. result.tls = (params.security == "tls" or params.security == "xtls") and "1" or "0"
  467. result.xhttp_alpn = params.alpn or ""
  468. result.tls_host = params.sni
  469. result.tls_flow = (params.security == "tls" or params.security == "reality") and params.flow or nil
  470. result.fingerprint = params.fp
  471. result.reality = (params.security == "reality") and "1" or "0"
  472. result.reality_publickey = params.pbk and UrlDecode(params.pbk) or nil
  473. result.reality_shortid = params.sid
  474. result.reality_spiderx = params.spx and UrlDecode(params.spx) or nil
  475. if result.transport == "ws" then
  476. result.ws_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  477. result.ws_path = params.path and UrlDecode(params.path) or "/"
  478. elseif result.transport == "httpupgrade" then
  479. result.httpupgrade_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  480. result.httpupgrade_path = params.path and UrlDecode(params.path) or "/"
  481. elseif result.transport == "splithttp" then
  482. result.splithttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  483. result.splithttp_path = params.path and UrlDecode(params.path) or "/"
  484. elseif result.transport == "xhttp" then
  485. result.xhttp_host = (result.tls ~= "1") and (params.host and UrlDecode(params.host)) or nil
  486. result.xhttp_mode = params.mode or "auto"
  487. result.xhttp_path = params.path and UrlDecode(params.path) or "/"
  488. -- 检查 extra 参数是否存在且非空
  489. result.enable_xhttp_extra = (params.extra and params.extra ~= "") and "1" or nil
  490. result.xhttp_extra = (params.extra and params.extra ~= "") and params.extra or nil
  491. -- 尝试解析 JSON 数据
  492. local success, Data = pcall(jsonParse, params.extra)
  493. if success and Data then
  494. local address = (Data.extra and Data.extra.downloadSettings and Data.extra.downloadSettings.address)
  495. or (Data.downloadSettings and Data.downloadSettings.address)
  496. result.download_address = address and address ~= "" and address or nil
  497. else
  498. -- 如果解析失败,清空下载地址
  499. result.download_address = nil
  500. end
  501. -- make it compatible with bullshit, "h2" transport is non-existent at all
  502. elseif result.transport == "http" or result.transport == "h2" then
  503. result.transport = "h2"
  504. result.h2_host = params.host and UrlDecode(params.host) or nil
  505. result.h2_path = params.path and UrlDecode(params.path) or nil
  506. elseif result.transport == "kcp" then
  507. result.kcp_guise = params.headerType or "none"
  508. result.seed = params.seed
  509. result.mtu = 1350
  510. result.tti = 50
  511. result.uplink_capacity = 5
  512. result.downlink_capacity = 20
  513. result.read_buffer_size = 2
  514. result.write_buffer_size = 2
  515. elseif result.transport == "quic" then
  516. result.quic_guise = params.headerType or "none"
  517. result.quic_security = params.quicSecurity or "none"
  518. result.quic_key = params.key
  519. elseif result.transport == "grpc" then
  520. result.serviceName = params.serviceName
  521. result.grpc_mode = params.mode or "gun"
  522. elseif result.transport == "tcp" or result.transport == "raw" then
  523. result.tcp_guise = params.headerType or "none"
  524. if result.tcp_guise == "http" then
  525. result.tcp_host = params.host and UrlDecode(params.host) or nil
  526. result.tcp_path = params.path and UrlDecode(params.path) or nil
  527. end
  528. end
  529. end
  530. if not result.alias then
  531. if result.server and result.server_port then
  532. result.alias = result.server .. ':' .. result.server_port
  533. else
  534. result.alias = "NULL"
  535. end
  536. end
  537. -- alias 不参与 hashkey 计算
  538. local alias = result.alias
  539. result.alias = nil
  540. local switch_enable = result.switch_enable
  541. result.switch_enable = nil
  542. result.hashkey = md5(jsonStringify(result))
  543. result.alias = alias
  544. result.switch_enable = switch_enable
  545. return result
  546. end
  547. -- wget
  548. local function wget(url)
  549. 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 .. '"')
  550. return trim(stdout)
  551. end
  552. local function check_filer(result)
  553. do
  554. -- 过滤的关键词列表
  555. local filter_word = split(filter_words, "/")
  556. -- 保留的关键词列表
  557. local check_save = false
  558. if save_words ~= nil and save_words ~= "" and save_words ~= "NULL" then
  559. check_save = true
  560. end
  561. local save_word = split(save_words, "/")
  562. -- 检查结果
  563. local filter_result = false
  564. local save_result = true
  565. -- 检查是否存在过滤关键词
  566. for i, v in pairs(filter_word) do
  567. if tostring(result.alias):find(v, nil, true) then
  568. filter_result = true
  569. end
  570. end
  571. -- 检查是否打开了保留关键词检查,并且进行过滤
  572. if check_save == true then
  573. for i, v in pairs(save_word) do
  574. if tostring(result.alias):find(v, nil, true) then
  575. save_result = false
  576. end
  577. end
  578. else
  579. save_result = false
  580. end
  581. -- 不等时返回
  582. if filter_result == true or save_result == true then
  583. return true
  584. else
  585. return false
  586. end
  587. end
  588. end
  589. local execute = function()
  590. -- exec
  591. do
  592. if proxy == '0' then -- 不使用代理更新的话先暂停
  593. log('服务正在暂停')
  594. luci.sys.init.stop(name)
  595. end
  596. for k, url in ipairs(subscribe_url) do
  597. local raw = wget(url)
  598. if #raw > 0 then
  599. local nodes, szType
  600. local groupHash = md5(url)
  601. cache[groupHash] = {}
  602. tinsert(nodeResult, {})
  603. local index = #nodeResult
  604. -- SSD 似乎是这种格式 ssd:// 开头的
  605. if raw:find('ssd://') then
  606. szType = 'ssd'
  607. local nEnd = select(2, raw:find('ssd://'))
  608. nodes = base64Decode(raw:sub(nEnd + 1, #raw))
  609. nodes = jsonParse(nodes)
  610. local extra = {airport = nodes.airport, port = nodes.port, encryption = nodes.encryption, password = nodes.password}
  611. local servers = {}
  612. -- SS里面包着 干脆直接这样
  613. for _, server in ipairs(nodes.servers) do
  614. tinsert(servers, setmetatable(server, {__index = extra}))
  615. end
  616. nodes = servers
  617. -- SS SIP008 直接使用 Json 格式
  618. elseif jsonParse(raw) then
  619. nodes = jsonParse(raw).servers or jsonParse(raw)
  620. if nodes[1].server and nodes[1].method then
  621. szType = 'sip008'
  622. end
  623. else
  624. -- ssd 外的格式
  625. nodes = split(base64Decode(raw):gsub(" ", "_"), "\n")
  626. end
  627. for _, v in ipairs(nodes) do
  628. if v then
  629. local result
  630. if szType then
  631. result = processData(szType, v)
  632. elseif not szType then
  633. local node = trim(v)
  634. local dat = split(node, "://")
  635. if dat and dat[1] and dat[2] then
  636. local dat3 = ""
  637. if dat[3] then
  638. dat3 = "://" .. dat[3]
  639. end
  640. if dat[1] == 'ss' or dat[1] == 'trojan' then
  641. result = processData(dat[1], dat[2] .. dat3)
  642. else
  643. result = processData(dat[1], base64Decode(dat[2]))
  644. end
  645. end
  646. else
  647. log('跳过未知类型: ' .. szType)
  648. end
  649. -- log(result)
  650. if result then
  651. -- 中文做地址的 也没有人拿中文域名搞,就算中文域也有Puny Code SB 机场
  652. 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
  653. log('丢弃无效节点: ' .. result.type .. ' 节点, ' .. result.alias)
  654. else
  655. -- log('成功解析: ' .. result.type ..' 节点, ' .. result.alias)
  656. result.grouphashkey = groupHash
  657. tinsert(nodeResult[index], result)
  658. cache[groupHash][result.hashkey] = nodeResult[index][#nodeResult[index]]
  659. end
  660. end
  661. end
  662. end
  663. log('成功解析节点数量: ' .. #nodes)
  664. else
  665. log(url .. ': 获取内容为空')
  666. end
  667. end
  668. end
  669. -- diff
  670. do
  671. if next(nodeResult) == nil then
  672. log("更新失败,没有可用的节点信息")
  673. if proxy == '0' then
  674. luci.sys.init.start(name)
  675. log('订阅失败, 恢复服务')
  676. end
  677. return
  678. end
  679. local add, del = 0, 0
  680. ucic:foreach(name, uciType, function(old)
  681. if old.grouphashkey or old.hashkey then -- 没有 hash 的不参与删除
  682. if not nodeResult[old.grouphashkey] or not nodeResult[old.grouphashkey][old.hashkey] then
  683. ucic:delete(name, old['.name'])
  684. del = del + 1
  685. else
  686. local dat = nodeResult[old.grouphashkey][old.hashkey]
  687. ucic:tset(name, old['.name'], dat)
  688. -- 标记一下
  689. setmetatable(nodeResult[old.grouphashkey][old.hashkey], {__index = {_ignore = true}})
  690. end
  691. else
  692. if not old.alias then
  693. if old.server or old.server_port then
  694. old.alias = old.server .. ':' .. old.server_port
  695. log('忽略手动添加的节点: ' .. old.alias)
  696. else
  697. ucic:delete(name, old['.name'])
  698. end
  699. else
  700. log('忽略手动添加的节点: ' .. old.alias)
  701. end
  702. end
  703. end)
  704. for k, v in ipairs(nodeResult) do
  705. for kk, vv in ipairs(v) do
  706. if not vv._ignore then
  707. local section = ucic:add(name, uciType)
  708. ucic:tset(name, section, vv)
  709. ucic:set(name, section, "switch_enable", switch)
  710. add = add + 1
  711. end
  712. end
  713. end
  714. ucic:commit(name)
  715. -- 如果原有服务器节点已经不见了就尝试换为第一个节点
  716. local globalServer = ucic:get_first(name, 'global', 'global_server', '')
  717. if globalServer ~= "nil" then
  718. local firstServer = ucic:get_first(name, uciType)
  719. if firstServer then
  720. if not ucic:get(name, globalServer) then
  721. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &")
  722. ucic:commit(name)
  723. ucic:set(name, ucic:get_first(name, 'global'), 'global_server', ucic:get_first(name, uciType))
  724. ucic:commit(name)
  725. log('当前主服务器节点已被删除,正在自动更换为第一个节点。')
  726. luci.sys.call("/etc/init.d/" .. name .. " start > /dev/null 2>&1 &")
  727. else
  728. log('维持当前主服务器节点。')
  729. luci.sys.call("/etc/init.d/" .. name .. " restart > /dev/null 2>&1 &")
  730. end
  731. else
  732. log('没有服务器节点了,停止服务')
  733. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &")
  734. end
  735. end
  736. log('新增节点数量: ' .. add, '删除节点数量: ' .. del)
  737. log('订阅更新成功')
  738. end
  739. end
  740. if subscribe_url and #subscribe_url > 0 then
  741. xpcall(execute, function(e)
  742. log(e)
  743. log(debug.traceback())
  744. log('发生错误, 正在恢复服务')
  745. local firstServer = ucic:get_first(name, uciType)
  746. if firstServer then
  747. luci.sys.call("/etc/init.d/" .. name .. " restart > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早
  748. log('重启服务成功')
  749. else
  750. luci.sys.call("/etc/init.d/" .. name .. " stop > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早
  751. log('停止服务成功')
  752. end
  753. end)
  754. end