From 8d12f406b94a11ea5a5695257d7de1b3cbe3e89a Mon Sep 17 00:00:00 2001 From: xiaorouji <60100640+xiaorouji@users.noreply.github.com> Date: Tue, 7 Feb 2023 17:10:15 +0800 Subject: [PATCH] luci: Optimization code logic --- .../luasrc/model/cbi/passwall/api/api.lua | 49 +++++++++++++++++-- .../luasrc/model/cbi/passwall/api/brook.lua | 2 +- .../model/cbi/passwall/api/hysteria.lua | 2 +- .../model/cbi/passwall/api/trojan_go.lua | 2 +- .../luasrc/model/cbi/passwall/api/v2ray.lua | 2 +- .../luasrc/model/cbi/passwall/api/xray.lua | 2 +- .../cbi/passwall/client/node_subscribe.lua | 5 -- .../root/usr/share/passwall/0_default_config | 1 - .../usr/share/passwall/helper_dnsmasq_add.lua | 31 ++---------- .../root/usr/share/passwall/rule_update.lua | 14 +++--- .../root/usr/share/passwall/subscribe.lua | 39 ++++----------- 11 files changed, 72 insertions(+), 77 deletions(-) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/api/api.lua b/luci-app-passwall/luasrc/model/cbi/passwall/api/api.lua index 402fae0b2..c38c3784e 100755 --- a/luci-app-passwall/luasrc/model/cbi/passwall/api/api.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/api/api.lua @@ -8,8 +8,7 @@ jsonc = require "luci.jsonc" i18n = require "luci.i18n" appname = "passwall" -curl = "/usr/bin/curl" -curl_args = {"-skfL", "--connect-timeout 3", "--retry 3", "-m 60"} +curl_args = { "-skfL", "--connect-timeout 3", "--retry 3", "-m 60" } command_timeout = 300 LEDE_BOARD = nil DISTRIB_TARGET = nil @@ -34,6 +33,39 @@ function base64Decode(text) end end +function curl_base(url, file, args) + if not args then args = {} end + if file then + args[#args + 1] = "-o " .. file + end + local cmd = string.format('curl %s "%s"', table_join(args), url) + if file then + return luci.sys.call(cmd .. " > /dev/null") + else + return trim(luci.sys.exec(cmd)) + end +end + +function curl_proxy(url, file, args) + --使用代理 + local socks_server = luci.sys.exec("[ -f /tmp/etc/passwall/TCP_SOCKS_server ] && echo -n $(cat /tmp/etc/passwall/TCP_SOCKS_server) || echo -n ''") + if socks_server ~= "" then + if not args then args = {} end + local tmp_args = clone(args) + tmp_args[#tmp_args + 1] = "-x socks5h://" .. socks_server + return curl_base(url, file, tmp_args) + end + return nil +end + +function curl_logic(url, file, args) + local result = curl_proxy(url, file, args) + if not result then + result = curl_base(url, file, args) + end + return result +end + function url(...) local url = string.format("admin/services/%s", appname) local args = { ... } @@ -455,6 +487,17 @@ function _unpack(t, i) if t[i] ~= nil then return t[i], _unpack(t, i + 1) end end +function table_join(t, s) + if not s then + s = " " + end + local str = "" + for index, value in ipairs(t) do + str = str .. t[index] .. (index == #t and "" or s) + end + return str +end + function exec(cmd, args, writer, timeout) local os = require "os" local nixio = require "nixio" @@ -585,7 +628,7 @@ end function get_api_json(url) local jsonc = require "luci.jsonc" - local json_content = luci.sys.exec(curl .. " " .. _unpack(curl_args) .. " " .. url) + local json_content = curl_logic(url, nil, curl_args) if json_content == "" then return {} end return jsonc.parse(json_content) or {} end diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/api/brook.lua b/luci-app-passwall/luasrc/model/cbi/passwall/api/brook.lua index 8941b663a..e55bd427a 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/api/brook.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/api/brook.lua @@ -63,7 +63,7 @@ function to_download(url, size) end end - result = api.exec(api.curl, {api._unpack(api.curl_args), "-o", tmp_file, url}, nil, api.command_timeout) == 0 + result = api.curl_logic(url, tmp_file, api.curl_args) == 0 if not result then api.exec("/bin/rm", {"-f", tmp_file}) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/api/hysteria.lua b/luci-app-passwall/luasrc/model/cbi/passwall/api/hysteria.lua index 2d2e36128..0f1664a1a 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/api/hysteria.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/api/hysteria.lua @@ -63,7 +63,7 @@ function to_download(url, size) end end - result = api.exec(api.curl, {api._unpack(api.curl_args), "-o", tmp_file, url}, nil, api.command_timeout) == 0 + result = api.curl_logic(url, tmp_file, api.curl_args) == 0 if not result then api.exec("/bin/rm", {"-f", tmp_file}) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/api/trojan_go.lua b/luci-app-passwall/luasrc/model/cbi/passwall/api/trojan_go.lua index 4f8b48497..49e6158d6 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/api/trojan_go.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/api/trojan_go.lua @@ -71,7 +71,7 @@ function to_download(url, size) end end - result = api.exec(api.curl, {api._unpack(api.curl_args), "-o", tmp_file, url}, nil, api.command_timeout) == 0 + result = api.curl_logic(url, tmp_file, api.curl_args) == 0 if not result then api.exec("/bin/rm", {"-f", tmp_file}) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/api/v2ray.lua b/luci-app-passwall/luasrc/model/cbi/passwall/api/v2ray.lua index f64fc697b..5078e00bc 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/api/v2ray.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/api/v2ray.lua @@ -69,7 +69,7 @@ function to_download(url, size) end end - result = api.exec(api.curl, {api._unpack(api.curl_args), "-o", tmp_file, url}, nil, api.command_timeout) == 0 + result = api.curl_logic(url, tmp_file, api.curl_args) == 0 if not result then api.exec("/bin/rm", {"-f", tmp_file}) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/api/xray.lua b/luci-app-passwall/luasrc/model/cbi/passwall/api/xray.lua index 448da4df7..518576e6a 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/api/xray.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/api/xray.lua @@ -69,7 +69,7 @@ function to_download(url, size) end end - result = api.exec(api.curl, {api._unpack(api.curl_args), "-o", tmp_file, url}, nil, api.command_timeout) == 0 + result = api.curl_logic(url, tmp_file, api.curl_args) == 0 if not result then api.exec("/bin/rm", {"-f", tmp_file}) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe.lua index 71be3cc4b..087916353 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe.lua @@ -35,11 +35,6 @@ m = Map(appname) s = m:section(TypedSection, "global_subscribe", "") s.anonymous = true ----- Subscribe via proxy -o = s:option(Flag, "subscribe_proxy", translate("Subscribe via proxy")) -o.default = 0 -o.rmempty = false - o = s:option(ListValue, "filter_keyword_mode", translate("Filter keyword Mode")) o:value("0", translate("Close")) o:value("1", translate("Discard List")) diff --git a/luci-app-passwall/root/usr/share/passwall/0_default_config b/luci-app-passwall/root/usr/share/passwall/0_default_config index ddb05be03..f1466902b 100644 --- a/luci-app-passwall/root/usr/share/passwall/0_default_config +++ b/luci-app-passwall/root/usr/share/passwall/0_default_config @@ -68,7 +68,6 @@ config global_app option hysteria_file '/usr/bin/hysteria' config global_subscribe - option subscribe_proxy '0' option filter_keyword_mode '1' list filter_discard_list '过期时间' list filter_discard_list '剩余流量' diff --git a/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua b/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua index cfd870071..b0ee37eaf 100644 --- a/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua +++ b/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua @@ -166,9 +166,8 @@ end local dnsmasq_default_dns local cache_text = "" -local subscribe_proxy=uci:get(appname, "@global_subscribe[0]", "subscribe_proxy") or "0" local new_rules = luci.sys.exec("echo -n $(find /usr/share/passwall/rules -type f | xargs md5sum)") -local new_text = TMP_DNSMASQ_PATH .. DNSMASQ_CONF_FILE .. DEFAULT_DNS .. LOCAL_DNS .. TUN_DNS .. REMOTE_FAKEDNS .. CHINADNS_DNS .. PROXY_MODE .. NO_PROXY_IPV6 .. subscribe_proxy .. new_rules .. NFTFLAG +local new_text = TMP_DNSMASQ_PATH .. DNSMASQ_CONF_FILE .. DEFAULT_DNS .. LOCAL_DNS .. TUN_DNS .. REMOTE_FAKEDNS .. CHINADNS_DNS .. PROXY_MODE .. NO_PROXY_IPV6 .. new_rules .. NFTFLAG if fs.access(CACHE_TEXT_FILE) then for line in io.lines(CACHE_TEXT_FILE) do cache_text = line @@ -227,33 +226,9 @@ if not fs.access(CACHE_DNS_PATH) then end log(string.format(" - 域名白名单(whitelist):%s", LOCAL_DNS or "默认")) - local fwd_dns = LOCAL_DNS - local ipset_flag = setflag_4 .. "whitelist," .. setflag_6 .. "whitelist6" + local fwd_dns + local ipset_flag local no_ipv6 - if subscribe_proxy == "1" then - fwd_dns = TUN_DNS - ipset_flag = setflag_4 .. "blacklist," .. setflag_6 .. "blacklist6" - if NO_PROXY_IPV6 == "1" then - ipset_flag = setflag_4 .. "blacklist" - no_ipv6 = true - end - if not only_global then - if REMOTE_FAKEDNS == "1" then - ipset_flag = nil - end - end - end - uci:foreach(appname, "subscribe_list", function(t) - local domain = get_domain_from_url(t.url) - if domain then - if no_ipv6 then - set_domain_address(domain, "::") - end - set_domain_dns(domain, fwd_dns) - set_domain_ipset(domain, ipset_flag) - end - end) - log(string.format(" - 节点订阅域名(blacklist):%s", fwd_dns or "默认")) --始终使用远程DNS解析代理(黑名单)列表 for line in io.lines("/usr/share/passwall/rules/proxy_host") do diff --git a/luci-app-passwall/root/usr/share/passwall/rule_update.lua b/luci-app-passwall/root/usr/share/passwall/rule_update.lua index 698fc9d28..bbc333ebc 100755 --- a/luci-app-passwall/root/usr/share/passwall/rule_update.lua +++ b/luci-app-passwall/root/usr/share/passwall/rule_update.lua @@ -57,19 +57,21 @@ end -- curl local function curl(url, file, valifile) - local cmd = "curl -skL -w %{http_code} --retry 3 --connect-timeout 3 '" .. url .. "'" + local args = { + "-sKL", "-w %{http_code}", "--retry 3", "--connect-timeout 3" + } if file then - cmd = cmd .. " -o " .. file + args[#args + 1] = "-o " .. file end if valifile then - cmd = cmd .. " --dump-header " .. valifile + args[#args + 1] = "--dump-header " .. valifile end - local stdout = luci.sys.exec(cmd) + local result = api.curl_logic(url, nil, args) if file then - return tonumber(trim(stdout)) + return tonumber(trim(result)) else - return trim(stdout) + return trim(result) end end diff --git a/luci-app-passwall/root/usr/share/passwall/subscribe.lua b/luci-app-passwall/root/usr/share/passwall/subscribe.lua index 7ee19d1ca..2f553c279 100755 --- a/luci-app-passwall/root/usr/share/passwall/subscribe.lua +++ b/luci-app-passwall/root/usr/share/passwall/subscribe.lua @@ -830,30 +830,15 @@ local function processData(szType, content, add_mode, add_from) return result end --- curl local function curl(url, file, ua) if not ua or ua == "" then ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36" end - local stdout = "" - local cmd = string.format('curl -skL --user-agent "%s" --retry 3 --connect-timeout 3 "%s"', ua, url) - if file then - cmd = cmd .. " -o " .. file - stdout = luci.sys.call(cmd .. " > /dev/null") - return stdout - else - stdout = luci.sys.exec(cmd) - return trim(stdout) - end - - if not stdout or #stdout <= 0 then - if uci:get(appname, "@global_subscribe[0]", "subscribe_proxy") or "0" == "1" and uci:get(appname, "@global[0]", "enabled") or "0" == "1" then - log('通过代理订阅失败,尝试关闭代理订阅。') - luci.sys.call("/etc/init.d/" .. appname .. " stop > /dev/null") - stdout = luci.sys.exec(string.format('curl -skL --user-agent "%s" -k --retry 3 --connect-timeout 3 "%s"', ua, url)) - end - end - return trim(stdout) + local args = { + "-skL", "--retry 3", "--connect-timeout 3", '--user-agent "' .. ua .. '"' + } + local result = api.curl_logic(url, file, args) + return result end local function truncate_nodes(add_from) @@ -1146,7 +1131,7 @@ end local execute = function() do local subscribe_list = {} - local retry = {} + local fail_list = {} if arg[2] then string.gsub(arg[2], '[^' .. "," .. ']+', function(w) subscribe_list[#subscribe_list + 1] = uci:get_all(appname, w) or {} @@ -1201,7 +1186,7 @@ local execute = function() os.remove("/tmp/" .. cfgid) parse_link(raw, "2", remark) else - retry[#retry + 1] = value + fail_list[#fail_list + 1] = value end allowInsecure_default = nil filter_keyword_mode_default = uci:get(appname, "@global_subscribe[0]", "filter_keyword_mode") or "0" @@ -1211,13 +1196,9 @@ local execute = function() trojan_type_default = uci:get(appname, "@global_subscribe[0]", "trojan_type") or "trojan-plus" end - if #retry > 0 then - for index, value in ipairs(retry) do - if (uci:get(appname, "@global_subscribe[0]", "subscribe_proxy") or "0") == "1" and (uci:get(appname, "@global[0]", "enabled") or "0") == "1" then - log(value.remark .. '订阅失败,请尝试关闭代理后再订阅。') - else - log(value.remark .. '订阅失败,可能是订阅地址失效,或是网络问题,请诊断!') - end + if #fail_list > 0 then + for index, value in ipairs(fail_list) do + log(value.remark .. '订阅失败,可能是订阅地址失效,或是网络问题,请诊断!') end end update_node(0)