subscribe.lua 29 KB

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