subscribe.lua 27 KB

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