From 43cbe97bbf70bde6c2d5cb7a9f2fec896a03d2f1 Mon Sep 17 00:00:00 2001 From: zxlhhyccc Date: Wed, 8 Apr 2026 21:45:36 +0800 Subject: [PATCH] luci-app-ssr-plus: Smart core select and import/subscribe stability enhance. --- .../model/cbi/shadowsocksr/client-config.lua | 126 ++- .../luasrc/model/cbi/shadowsocksr/client.lua | 1 - .../luasrc/model/cbi/shadowsocksr/servers.lua | 102 ++- .../luasrc/view/shadowsocksr/ssrurl.htm | 249 ++++-- luci-app-ssr-plus/po/templates/ssr-plus.pot | 775 ++++++++--------- luci-app-ssr-plus/po/zh_Hans/ssr-plus.po | 777 +++++++++--------- .../root/etc/init.d/shadowsocksr | 61 +- .../usr/share/shadowsocksr/gen_config.lua | 4 + .../share/shadowsocksr/shadowsocksr.config | 2 - .../root/usr/share/shadowsocksr/subscribe.lua | 762 +++++++++-------- 10 files changed, 1660 insertions(+), 1199 deletions(-) diff --git a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client-config.lua b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client-config.lua index 2a885cc4..090ae144 100644 --- a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client-config.lua +++ b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client-config.lua @@ -5,6 +5,7 @@ require "nixio.fs" require "luci.sys" require "luci.http" require "luci.jsonc" +local nixio = require "nixio" require "luci.model.uci" local uci = require "luci.model.uci".cursor() @@ -146,11 +147,11 @@ local function set_apply_on_parse(map) end end -local has_xray = is_finded("xray") -local has_hysteria2 = is_finded("hysteria") - local has_ss_rust = is_finded("sslocal") or is_finded("ssserver") local has_ss_libev = is_finded("ss-redir") or is_finded("ss-local") +local has_trojan = is_finded("trojan") +local has_xray = is_finded("xray") +local has_hysteria2 = is_finded("hysteria") local server_table = {} local encrypt_methods = { @@ -277,25 +278,63 @@ o.value = sid -- 新增一个选择框,用于选择 Xray 或 Hysteria2 核心 o = s:option(ListValue, "_xray_hy2_type", string.format("%s", translatef("%s Node Use Type", "Hysteria2"))) o.description = translate("The configured type also applies to the core specified when manually importing nodes.") --- 设置默认 Xray 或 Hysteria2 核心 --- 动态添加选项 -if has_xray then - o:value("v2ray", translate("Xray (Hysteria2)")) -end +-- 注意:Auto 选项使用特殊字符串 "__auto__" 而不是空字符串 +o:value("__auto__", translate("Auto")) if has_hysteria2 then - o:value("hysteria2", translate("Hysteria2")) + o:value("hysteria2", translate("Hysteria2")) +end +if has_xray then + o:value("v2ray", translate("Xray (Hysteria2)")) end -- 读取全局 xray_hy2_type o.cfgvalue = function(self, section) - return self.map.uci:get("shadowsocksr", "@server_subscribe[0]", "xray_hy2_type") or "hysteria2" + local val = uci:get("shadowsocksr", "@server_subscribe[0]", "xray_hy2_type") + if val == nil or val == "" then + return "__auto__" -- 对应 Auto 选项 + end + return val end o.rmempty = true +-- 保存时更新全局配置 o.write = function(self, section, value) - -- 更新 server_subscribe 的 xray_hy2_type - local old_value = self.map.uci:get("shadowsocksr", "@server_subscribe[0]", "xray_hy2_type") - if old_value ~= value then - self.map.uci:set("shadowsocksr", "@server_subscribe[0]", "xray_hy2_type", value) - end + if value == "__auto__" then + -- 删除全局配置 + uci:delete("shadowsocksr", "@server_subscribe[0]", "xray_hy2_type") + else + -- 设置具体值 + uci:set("shadowsocksr", "@server_subscribe[0]", "xray_hy2_type", value) + end +end + +-- 新增一个选择框,用于选择 Xray 或 Trojan 核心 +o = s:option(ListValue, "_xray_tj_type", string.format("%s", translatef("%s Node Use Type", "Trojan"))) +o.description = translate("The configured type also applies to the core specified when manually importing nodes.") +-- 注意:Auto 选项使用特殊字符串 "__auto__" 而不是空字符串 +o:value("__auto__", translate("Auto")) +if has_hysteria2 then + o:value("trojan", translate("Trojan")) +end +if has_xray then + o:value("v2ray", translate("Xray (Trojan)")) +end +-- 读取全局 xray_tj_type +o.cfgvalue = function(self, section) + local val = uci:get("shadowsocksr", "@server_subscribe[0]", "xray_tj_type") + if val == nil or val == "" then + return "__auto__" -- 对应 Auto 选项 + end + return val +end +o.rmempty = true +-- 保存时更新全局配置 +o.write = function(self, section, value) + if value == "__auto__" then + -- 删除全局配置 + uci:delete("shadowsocksr", "@server_subscribe[0]", "xray_tj_type") + else + -- 设置具体值 + uci:set("shadowsocksr", "@server_subscribe[0]", "xray_tj_type", value) + end end o = s:option(ListValue, "type", translate("Server Node Type")) @@ -329,6 +368,29 @@ end if is_finded("redsocks2") then o:value("tun", translate("Network Tunnel")) end +local old_cfgvalue = o.cfgvalue +o.cfgvalue = function(self, section) + local val = self.map.uci:get("shadowsocksr", section, "type") + if val == "ss-rust" or val == "ss-libev" then + return "ss" + end + if old_cfgvalue then + return old_cfgvalue(self, section) + end + return val +end +-- 重写 write,当用户选择 "ss" 时不写入(由 _ss_core 负责写入具体核心) +local old_write = o.write +o.write = function(self, section, value) + if value == "ss" then + return -- 不做任何写入,等待 _ss_core 写入 + end + if old_write then + old_write(self, section, value) + else + self.map.uci:set("shadowsocksr", section, "type", value) + end +end o.description = translate("Using incorrect encryption mothod may causes service fail to start") @@ -343,29 +405,37 @@ end o:depends("type", "tun") o.description = translate("Redirect traffic to this network interface") --- 新增一个选择框,用于选择 Shadowsocks 版本 -o = s:option(ListValue, "_has_ss_type", string.format("%s", translatef("%s Node Use Version", "ShadowSocks"))) +-- 新增一个选择框,用于选择 Shadowsocks 具体版本(仅当节点类型为 ss 或其具体子类型时显示) +o = s:option(ListValue, "_ss_core", string.format("%s", translatef("%s Node Use Version", "ShadowSocks"))) o.description = translate("Selection ShadowSocks Node Use Version.") --- 设置默认 Shadowsocks 版本 --- 动态添加选项 if has_ss_rust then - o:value("ss-rust", translate("ShadowSocks-rust Version")) + o:value("ss-rust", translate("ShadowSocks-rust Version")) end if has_ss_libev then - o:value("ss-libev", translate("ShadowSocks-libev Version")) + o:value("ss-libev", translate("ShadowSocks-libev Version")) end --- 读取全局 ss_type o.cfgvalue = function(self, section) - return self.map.uci:get("shadowsocksr", "@server_subscribe[0]", "ss_type") or "ss-rust" + -- 读取当前节点的 type 值,如果已经是具体核心则显示对应的选项 + local node_type = self.map.uci:get("shadowsocksr", section, "type") + if node_type == "ss-rust" or node_type == "ss-libev" then + return node_type + end + -- 如果全局 ss_type 有值且为具体核心则返回该值 + local ss_type = self.map.uci:get("shadowsocksr", "@server_subscribe[0]", "ss_type") + if ss_type == "ss-rust" or ss_type == "ss-libev" then + return ss_type + end + -- 如果节点 type 是旧的 "ss",则返回空,手动选择 + return nil end +-- 显示条件:当节点类型为 "ss" 或其具体核心时显示 o:depends("type", "ss") o.rmempty = true +-- 保存时,将选择的值直接写入当前节点的 type 字段 o.write = function(self, section, value) - -- 更新 server_subscribe 的 ss_type - local old_value = self.map.uci:get("shadowsocksr", "@server_subscribe[0]", "ss_type") - if old_value ~= value then - self.map.uci:set("shadowsocksr", "@server_subscribe[0]", "ss_type", value) - end + if value and value ~= "" then + self.map.uci:set("shadowsocksr", section, "type", value) + end end o = s:option(ListValue, "v2ray_protocol", translate("V2Ray/XRay protocol")) diff --git a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client.lua b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client.lua index bb19eb7d..1404134c 100644 --- a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client.lua +++ b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/client.lua @@ -294,4 +294,3 @@ if is_finded("chinadns-ng") then end return m - diff --git a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua index 65d1a7f1..07a74257 100644 --- a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua +++ b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua @@ -70,9 +70,14 @@ local function set_apply_on_parse(map) end end +local has_ss_rust = is_finded("sslocal") or is_finded("ssserver") +local has_ss_libev = is_finded("ss-redir") or is_finded("ss-local") +local has_trojan = is_finded("trojan") local has_xray = is_finded("xray") local has_hysteria2 = is_finded("hysteria") +local ss_type_list = {} +local tj_type_list = {} local hy2_type_list = {} if has_hysteria2 then @@ -82,19 +87,12 @@ if has_xray then table.insert(hy2_type_list, { id = "v2ray", name = translate("Xray (Hysteria2)") }) end --- 如果用户没有手动设置,则自动选择 -if not xray_hy2_type or xray_hy2_type == "" then - if has_hysteria2 then - xray_hy2_type = "hysteria2" - elseif has_xray then - xray_hy2_type = "v2ray" - end +if has_trojan then + table.insert(tj_type_list, { id = "trojan", name = translate("Trojan") }) +end +if has_xray then + table.insert(tj_type_list, { id = "v2ray", name = translate("Xray (Trojan)") }) end - -local has_ss_rust = is_finded("sslocal") or is_finded("ssserver") -local has_ss_libev = is_finded("ss-redir") or is_finded("ss-local") - -local ss_type_list = {} if has_ss_rust then table.insert(ss_type_list, { id = "ss-rust", name = translate("ShadowSocks-rust Version") }) @@ -106,17 +104,6 @@ if has_xray then table.insert(ss_type_list, { id = "v2ray", name = translate("Xray (ShadowSocks)") }) end --- 如果用户没有手动设置,则自动选择 -if not ss_type or ss_type == "" then - if has_ss_rust then - ss_type = "ss-rust" - elseif has_ss_libev then - ss_type = "ss-libev" - elseif has_xray then - ss_type = "v2ray" - end -end - uci:foreach("shadowsocksr", "servers", function(s) server_count = server_count + 1 end) @@ -162,22 +149,87 @@ o:depends("auto_update", "1") -- 确保 hy2_type_list 不为空 if #hy2_type_list > 0 then + local sid = uci:get_first("shadowsocksr", "server_subscribe") + if not sid then + uci:foreach("shadowsocksr", "server_subscribe", function(section) + sid = section[".name"] + return false + end) + end + if sid then + local old_val = uci:get("shadowsocksr", sid, "xray_hy2_type") + if old_val and old_val ~= "" then + if (old_val == "hysteria2" and not has_hysteria2) or + (old_val == "v2ray" and not has_xray) then + -- 核心不可用,设置为空(删除配置) + uci:set("shadowsocksr", sid, "xray_hy2_type", "") + uci:commit("shadowsocksr") + end + end + end o = s:option(ListValue, "xray_hy2_type", string.format("%s", translatef("%s Node Use Type", "Hysteria2"))) o.description = translate("The configured type also applies to the core specified when manually importing nodes.") + o:value("", translate("Auto")) for _, v in ipairs(hy2_type_list) do o:value(v.id, v.name) -- 存储 "Xray" / "Hysteria2",但 UI 显示完整名称 end - o.default = xray_hy2_type -- 设置默认值 +end + +-- 确保 tj_type_list 不为空 +if #tj_type_list > 0 then + local sid = uci:get_first("shadowsocksr", "server_subscribe") + if not sid then + uci:foreach("shadowsocksr", "server_subscribe", function(section) + sid = section[".name"] + return false + end) + end + if sid then + local old_val = uci:get("shadowsocksr", sid, "xray_tj_type") + if old_val and old_val ~= "" then + if (old_val == "trojan" and not has_trojan) or + (old_val == "v2ray" and not has_xray) then + -- 核心不可用,设置为空(删除配置) + uci:set("shadowsocksr", sid, "xray_tj_type", "") + uci:commit("shadowsocksr") + end + end + end + o = s:option(ListValue, "xray_tj_type", string.format("%s", translatef("%s Node Use Type", "Trojan"))) + o.description = translate("The configured type also applies to the core specified when manually importing nodes.") + o:value("", translate("Auto")) + for _, v in ipairs(tj_type_list) do + o:value(v.id, v.name) -- 存储 "Xray" / "Trojan",但 UI 显示完整名称 + end end -- 确保 ss_type_list 不为空 if #ss_type_list > 0 then + local sid = uci:get_first("shadowsocksr", "server_subscribe") + if not sid then + uci:foreach("shadowsocksr", "server_subscribe", function(section) + sid = section[".name"] + return false + end) + end + if sid then + local old_val = uci:get("shadowsocksr", sid, "ss_type") + if old_val and old_val ~= "" then + if (old_val == "ss-rust" and not has_ss_rust) or + (old_val == "ss-libev" and not has_ss_libev) or + (old_val == "v2ray" and not has_xray) then + -- 核心不可用,设置为空(删除配置) + uci:set("shadowsocksr", sid, "ss_type", "") + uci:commit("shadowsocksr") + end + end + end o = s:option(ListValue, "ss_type", string.format("%s", translatef("%s Node Use Version", "ShadowSocks"))) o.description = translate("Selection ShadowSocks Node Use Version.") + o:value("", translate("Auto")) for _, v in ipairs(ss_type_list) do o:value(v.id, v.name) -- 存储 "ss-libev" / "ss-rust",但 UI 显示完整名称 end - o.default = ss_type -- 设置默认值 end o = s:option(DynamicList, "subscribe_url", translate("Subscribe URL")) diff --git a/luci-app-ssr-plus/luasrc/view/shadowsocksr/ssrurl.htm b/luci-app-ssr-plus/luasrc/view/shadowsocksr/ssrurl.htm index 0f3d6e36..b5610c2e 100644 --- a/luci-app-ssr-plus/luasrc/view/shadowsocksr/ssrurl.htm +++ b/luci-app-ssr-plus/luasrc/view/shadowsocksr/ssrurl.htm @@ -3,11 +3,24 @@ local map = self.map local ss_type = map:get("@server_subscribe[0]", "ss_type") local xray_hy2_type = map:get("@server_subscribe[0]", "xray_hy2_type") +local xray_tj_type = map:get("@server_subscribe[0]", "xray_tj_type") + +local has_ss_rust = luci.sys.exec('type -t -p sslocal 2>/dev/null || type -t -p ssserver 2>/dev/null') ~= "" +local has_ss_libev = luci.sys.exec('type -t -p ss-redir 2>/dev/null || type -t -p ss-local 2>/dev/null') ~= "" +local has_hysteria = luci.sys.exec('type -t -p hysteria 2>/dev/null') ~= "" +local has_trojan = luci.sys.exec('type -t -p trojan 2>/dev/null') ~= "" +local has_xray = luci.sys.exec('type -t -p xray 2>/dev/null') ~= "" -%>