luci: Optimization code logic

This commit is contained in:
xiaorouji 2023-02-07 17:10:15 +08:00 committed by sbwml
parent 78a33066ed
commit 8d12f406b9
11 changed files with 72 additions and 77 deletions

View File

@ -8,8 +8,7 @@ jsonc = require "luci.jsonc"
i18n = require "luci.i18n" i18n = require "luci.i18n"
appname = "passwall" 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 command_timeout = 300
LEDE_BOARD = nil LEDE_BOARD = nil
DISTRIB_TARGET = nil DISTRIB_TARGET = nil
@ -34,6 +33,39 @@ function base64Decode(text)
end end
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(...) function url(...)
local url = string.format("admin/services/%s", appname) local url = string.format("admin/services/%s", appname)
local args = { ... } local args = { ... }
@ -455,6 +487,17 @@ function _unpack(t, i)
if t[i] ~= nil then return t[i], _unpack(t, i + 1) end if t[i] ~= nil then return t[i], _unpack(t, i + 1) end
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) function exec(cmd, args, writer, timeout)
local os = require "os" local os = require "os"
local nixio = require "nixio" local nixio = require "nixio"
@ -585,7 +628,7 @@ end
function get_api_json(url) function get_api_json(url)
local jsonc = require "luci.jsonc" 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 if json_content == "" then return {} end
return jsonc.parse(json_content) or {} return jsonc.parse(json_content) or {}
end end

View File

@ -63,7 +63,7 @@ function to_download(url, size)
end end
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 if not result then
api.exec("/bin/rm", {"-f", tmp_file}) api.exec("/bin/rm", {"-f", tmp_file})

View File

@ -63,7 +63,7 @@ function to_download(url, size)
end end
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 if not result then
api.exec("/bin/rm", {"-f", tmp_file}) api.exec("/bin/rm", {"-f", tmp_file})

View File

@ -71,7 +71,7 @@ function to_download(url, size)
end end
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 if not result then
api.exec("/bin/rm", {"-f", tmp_file}) api.exec("/bin/rm", {"-f", tmp_file})

View File

@ -69,7 +69,7 @@ function to_download(url, size)
end end
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 if not result then
api.exec("/bin/rm", {"-f", tmp_file}) api.exec("/bin/rm", {"-f", tmp_file})

View File

@ -69,7 +69,7 @@ function to_download(url, size)
end end
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 if not result then
api.exec("/bin/rm", {"-f", tmp_file}) api.exec("/bin/rm", {"-f", tmp_file})

View File

@ -35,11 +35,6 @@ m = Map(appname)
s = m:section(TypedSection, "global_subscribe", "") s = m:section(TypedSection, "global_subscribe", "")
s.anonymous = true 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 = s:option(ListValue, "filter_keyword_mode", translate("Filter keyword Mode"))
o:value("0", translate("Close")) o:value("0", translate("Close"))
o:value("1", translate("Discard List")) o:value("1", translate("Discard List"))

View File

@ -68,7 +68,6 @@ config global_app
option hysteria_file '/usr/bin/hysteria' option hysteria_file '/usr/bin/hysteria'
config global_subscribe config global_subscribe
option subscribe_proxy '0'
option filter_keyword_mode '1' option filter_keyword_mode '1'
list filter_discard_list '过期时间' list filter_discard_list '过期时间'
list filter_discard_list '剩余流量' list filter_discard_list '剩余流量'

View File

@ -166,9 +166,8 @@ end
local dnsmasq_default_dns local dnsmasq_default_dns
local cache_text = "" 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_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 if fs.access(CACHE_TEXT_FILE) then
for line in io.lines(CACHE_TEXT_FILE) do for line in io.lines(CACHE_TEXT_FILE) do
cache_text = line cache_text = line
@ -227,33 +226,9 @@ if not fs.access(CACHE_DNS_PATH) then
end end
log(string.format(" - 域名白名单(whitelist)%s", LOCAL_DNS or "默认")) log(string.format(" - 域名白名单(whitelist)%s", LOCAL_DNS or "默认"))
local fwd_dns = LOCAL_DNS local fwd_dns
local ipset_flag = setflag_4 .. "whitelist," .. setflag_6 .. "whitelist6" local ipset_flag
local no_ipv6 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解析代理黑名单列表 --始终使用远程DNS解析代理黑名单列表
for line in io.lines("/usr/share/passwall/rules/proxy_host") do for line in io.lines("/usr/share/passwall/rules/proxy_host") do

View File

@ -57,19 +57,21 @@ end
-- curl -- curl
local function curl(url, file, valifile) 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 if file then
cmd = cmd .. " -o " .. file args[#args + 1] = "-o " .. file
end end
if valifile then if valifile then
cmd = cmd .. " --dump-header " .. valifile args[#args + 1] = "--dump-header " .. valifile
end end
local stdout = luci.sys.exec(cmd) local result = api.curl_logic(url, nil, args)
if file then if file then
return tonumber(trim(stdout)) return tonumber(trim(result))
else else
return trim(stdout) return trim(result)
end end
end end

View File

@ -830,30 +830,15 @@ local function processData(szType, content, add_mode, add_from)
return result return result
end end
-- curl
local function curl(url, file, ua) local function curl(url, file, ua)
if not ua or ua == "" then 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" ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
end end
local stdout = "" local args = {
local cmd = string.format('curl -skL --user-agent "%s" --retry 3 --connect-timeout 3 "%s"', ua, url) "-skL", "--retry 3", "--connect-timeout 3", '--user-agent "' .. ua .. '"'
if file then }
cmd = cmd .. " -o " .. file local result = api.curl_logic(url, file, args)
stdout = luci.sys.call(cmd .. " > /dev/null") return result
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)
end end
local function truncate_nodes(add_from) local function truncate_nodes(add_from)
@ -1146,7 +1131,7 @@ end
local execute = function() local execute = function()
do do
local subscribe_list = {} local subscribe_list = {}
local retry = {} local fail_list = {}
if arg[2] then if arg[2] then
string.gsub(arg[2], '[^' .. "," .. ']+', function(w) string.gsub(arg[2], '[^' .. "," .. ']+', function(w)
subscribe_list[#subscribe_list + 1] = uci:get_all(appname, w) or {} subscribe_list[#subscribe_list + 1] = uci:get_all(appname, w) or {}
@ -1201,7 +1186,7 @@ local execute = function()
os.remove("/tmp/" .. cfgid) os.remove("/tmp/" .. cfgid)
parse_link(raw, "2", remark) parse_link(raw, "2", remark)
else else
retry[#retry + 1] = value fail_list[#fail_list + 1] = value
end end
allowInsecure_default = nil allowInsecure_default = nil
filter_keyword_mode_default = uci:get(appname, "@global_subscribe[0]", "filter_keyword_mode") or "0" 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" trojan_type_default = uci:get(appname, "@global_subscribe[0]", "trojan_type") or "trojan-plus"
end end
if #retry > 0 then if #fail_list > 0 then
for index, value in ipairs(retry) do for index, value in ipairs(fail_list) 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 .. '订阅失败,可能是订阅地址失效,或是网络问题,请诊断!')
log(value.remark .. '订阅失败,请尝试关闭代理后再订阅。')
else
log(value.remark .. '订阅失败,可能是订阅地址失效,或是网络问题,请诊断!')
end
end end
end end
update_node(0) update_node(0)