subscribe.lua 27 KB

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