luci-app-passwall: sync upstream

last commit: 10238e5f69
This commit is contained in:
gitea-action 2025-01-10 17:30:19 +08:00
parent b29c5ffcff
commit 5d0dbdc4a2

View File

@ -96,6 +96,13 @@ local log = function(...)
end end
end end
local nodes_table = {}
for k, e in ipairs(api.get_valid_nodes()) do
if e.node_type == "normal" then
nodes_table[#nodes_table + 1] = e
end
end
-- 获取各项动态配置的当前服务器,可以用 get 和 set get必须要获取到节点表 -- 获取各项动态配置的当前服务器,可以用 get 和 set get必须要获取到节点表
local CONFIG = {} local CONFIG = {}
do do
@ -123,20 +130,56 @@ do
local option = "node" local option = "node"
uci:foreach(appname, "socks", function(t) uci:foreach(appname, "socks", function(t)
i = i + 1 i = i + 1
local id = t[".name"]
local node_id = t[option] local node_id = t[option]
CONFIG[#CONFIG + 1] = { CONFIG[#CONFIG + 1] = {
log = true, log = true,
id = t[".name"], id = id,
remarks = "Socks节点列表[" .. i .. "]", remarks = "Socks节点列表[" .. i .. "]",
currentNode = node_id and uci:get_all(appname, node_id) or nil, currentNode = node_id and uci:get_all(appname, node_id) or nil,
set = function(o, server) set = function(o, server)
if not server or server == "" then
if #nodes_table > 0 then
server = nodes_table[1][".name"]
end
end
uci:set(appname, t[".name"], option, server) uci:set(appname, t[".name"], option, server)
o.newNodeId = server o.newNodeId = server
end,
delete = function(o)
uci:delete(appname, t[".name"])
end end
} }
if t.autoswitch_backup_node and #t.autoswitch_backup_node > 0 then
local flag = "Socks节点列表[" .. i .. "]备用节点的列表"
local currentNodes = {}
local newNodes = {}
for k, node_id in ipairs(t.autoswitch_backup_node) do
if node_id then
local currentNode = uci:get_all(appname, node_id) or nil
if currentNode then
currentNodes[#currentNodes + 1] = {
log = true,
remarks = flag .. "[" .. k .. "]",
currentNode = currentNode,
set = function(o, server)
if server and server ~= "nil" then
table.insert(o.newNodes, server)
end
end
}
end
end
end
CONFIG[#CONFIG + 1] = {
remarks = flag,
currentNodes = currentNodes,
newNodes = newNodes,
set = function(o, newNodes)
if o then
if not newNodes then newNodes = o.newNodes end
uci:set_list(appname, id, "autoswitch_backup_node", newNodes or {})
end
end
}
end
end) end)
end end
@ -151,6 +194,7 @@ do
local node_id = t[option] local node_id = t[option]
CONFIG[#CONFIG + 1] = { CONFIG[#CONFIG + 1] = {
log = true, log = true,
rmempty = true,
id = t[".name"], id = t[".name"],
remarks = "HAProxy负载均衡节点列表[" .. i .. "]", remarks = "HAProxy负载均衡节点列表[" .. i .. "]",
currentNode = node_id and uci:get_all(appname, node_id) or nil, currentNode = node_id and uci:get_all(appname, node_id) or nil,
@ -193,49 +237,6 @@ do
end) end)
end end
uci:foreach(appname, "socks", function(o)
local id = o[".name"]
local node_table = uci:get(appname, id, "autoswitch_backup_node")
if node_table then
local nodes = {}
local new_nodes = {}
for k,node_id in ipairs(node_table) do
if node_id then
local currentNode = uci:get_all(appname, node_id) or nil
if currentNode then
if currentNode.protocol and (currentNode.protocol == "_balancing" or currentNode.protocol == "_shunt") then
currentNode = nil
end
nodes[#nodes + 1] = {
log = true,
remarks = "Socks[" .. id .. "]备用节点的列表[" .. k .. "]",
currentNode = currentNode,
set = function(o, server)
for kk, vv in pairs(CONFIG) do
if (vv.remarks == id .. "备用节点的列表") then
table.insert(vv.new_nodes, server)
end
end
end
}
end
end
end
CONFIG[#CONFIG + 1] = {
remarks = id .. "备用节点的列表",
nodes = nodes,
new_nodes = new_nodes,
set = function(o)
for kk, vv in pairs(CONFIG) do
if (vv.remarks == id .. "备用节点的列表") then
uci:set_list(appname, id, "autoswitch_backup_node", vv.new_nodes)
end
end
end
}
end
end)
uci:foreach(appname, "nodes", function(node) uci:foreach(appname, "nodes", function(node)
local node_id = node[".name"] local node_id = node[".name"]
if node.protocol and node.protocol == '_shunt' then if node.protocol and node.protocol == '_shunt' then
@ -271,39 +272,32 @@ do
end end
end end
elseif node.protocol and node.protocol == '_balancing' then elseif node.protocol and node.protocol == '_balancing' then
local nodes = {} local flag = "Xray负载均衡节点[" .. node_id .. "]列表"
local new_nodes = {} local currentNodes = {}
local newNodes = {}
if node.balancing_node then if node.balancing_node then
for k, node in pairs(node.balancing_node) do for k, node in pairs(node.balancing_node) do
nodes[#nodes + 1] = { currentNodes[#currentNodes + 1] = {
log = false, log = false,
node = node, node = node,
currentNode = node and uci:get_all(appname, node) or nil, currentNode = node and uci:get_all(appname, node) or nil,
remarks = node, remarks = node,
set = function(o, server) set = function(o, server)
for kk, vv in pairs(CONFIG) do if o and server and server ~= "nil" then
if (vv.remarks == "Xray负载均衡节点[" .. node_id .. "]列表") then table.insert(o.newNodes, server)
table.insert(vv.new_nodes, server)
end
end end
end end
} }
end end
end end
CONFIG[#CONFIG + 1] = { CONFIG[#CONFIG + 1] = {
remarks = "Xray负载均衡节点[" .. node_id .. "]列表", remarks = flag,
nodes = nodes, currentNodes = currentNodes,
new_nodes = new_nodes, newNodes = newNodes,
set = function(o) set = function(o, newNodes)
for kk, vv in pairs(CONFIG) do if o then
if (vv.remarks == "Xray负载均衡节点[" .. node_id .. "]列表") then if not newNodes then newNodes = o.newNodes end
uci:foreach(appname, "nodes", function(node2) uci:set_list(appname, node_id, "balancing_node", newNodes or {})
if node2[".name"] == node[".name"] then
local section = uci:section(appname, "nodes", node_id)
uci:set_list(appname, section, "balancing_node", vv.new_nodes)
end
end)
end
end end
end end
} }
@ -364,10 +358,10 @@ do
end) end)
for k, v in pairs(CONFIG) do for k, v in pairs(CONFIG) do
if v.nodes and type(v.nodes) == "table" then if v.currentNodes and type(v.currentNodes) == "table" then
for kk, vv in pairs(v.nodes) do for kk, vv in pairs(v.currentNodes) do
if vv.currentNode == nil then if vv.currentNode == nil then
CONFIG[k].nodes[kk] = nil CONFIG[k].currentNodes[kk] = nil
end end
end end
else else
@ -1278,44 +1272,54 @@ end
local function truncate_nodes(add_from) local function truncate_nodes(add_from)
for _, config in pairs(CONFIG) do for _, config in pairs(CONFIG) do
if config.nodes and type(config.nodes) == "table" then if config.currentNodes and #config.currentNodes > 0 then
for kk, vv in pairs(config.nodes) do local newNodes = {}
if vv.currentNode.add_mode == "2" then local removeNodesSet = {}
else for k, v in pairs(config.currentNodes) do
vv.set(vv, vv.currentNode[".name"]) if v.currentNode and v.currentNode.add_mode == "2" then
if (not add_from) or (add_from and add_from == v.currentNode.add_from) then
removeNodesSet[v.currentNode[".name"]] = true
end
end end
end end
config.set(config) for _, value in ipairs(config.currentNodes) do
if not removeNodesSet[value.currentNode[".name"]] then
newNodes[#newNodes + 1] = value.currentNode[".name"]
end
end
if config.set then
config.set(config, newNodes)
end
else else
if config.currentNode and config.currentNode.add_mode == "2" then if config.currentNode and config.currentNode.add_mode == "2" then
if add_from then if (not add_from) or (add_from and add_from == config.currentNode.add_from) then
if config.currentNode.add_from and config.currentNode.add_from == add_from then if config.delete then
config.delete(config)
elseif config.set then
config.set(config, "") config.set(config, "")
end end
else
config.set(config, "")
end
if config.id then
uci:delete(appname, config.id)
end end
end end
end end
end end
uci:foreach(appname, "nodes", function(node) uci:foreach(appname, "nodes", function(node)
if node.add_mode == "2" then if node.add_mode == "2" then
if add_from then if (not add_from) or (add_from and add_from == node.add_from) then
if node.add_from and node.add_from == add_from then
uci:delete(appname, node['.name'])
end
else
uci:delete(appname, node['.name']) uci:delete(appname, node['.name'])
end end
end end
end) end)
if add_from then
uci:foreach(appname, "subscribe_list", function(o)
if o.remark == add_from then
uci:delete(appname, o['.name'], "md5")
end
end)
end
api.uci_save(uci, appname, true) api.uci_save(uci, appname, true)
end end
local function select_node(nodes, config) local function select_node(nodes, config, parentConfig)
if config.currentNode then if config.currentNode then
local server local server
-- 特别优先级 cfgid -- 特别优先级 cfgid
@ -1408,26 +1412,28 @@ local function select_node(nodes, config)
end end
end end
end end
-- 还不行 随便找一个 if not parentConfig then
if not server then -- 还不行 随便找一个
local nodes_table = {} if not server then
for k, e in ipairs(api.get_valid_nodes()) do if #nodes_table > 0 then
if e.node_type == "normal" then if config.log == nil or config.log == true then
nodes_table[#nodes_table + 1] = e log('' .. config.remarks .. '' .. '无法找到最匹配的节点,当前已更换为:' .. nodes_table[1].remarks)
end
server = nodes_table[1][".name"]
end end
end end
if #nodes_table > 0 then
if config.log == nil or config.log == true then
log('' .. config.remarks .. '' .. '无法找到最匹配的节点,当前已更换为:' .. nodes_table[1].remarks)
end
server = nodes_table[1][".name"]
end
end end
if server then if server then
config.set(config, server) if parentConfig then
config.set(parentConfig, server)
else
config.set(config, server)
end
end end
else else
config.set(config, "") if not parentConfig then
config.set(config, "")
end
end end
end end
@ -1473,9 +1479,9 @@ local function update_node(manual)
end) end)
for _, config in pairs(CONFIG) do for _, config in pairs(CONFIG) do
if config.nodes and type(config.nodes) == "table" then if config.currentNodes and #config.currentNodes > 0 then
for kk, vv in pairs(config.nodes) do for kk, vv in pairs(config.currentNodes) do
select_node(nodes, vv) select_node(nodes, vv, config)
end end
config.set(config) config.set(config)
else else
@ -1483,22 +1489,6 @@ local function update_node(manual)
end end
end end
--[[
for k, v in pairs(CONFIG) do
if type(v.new_nodes) == "table" and #v.new_nodes > 0 then
local new_node_list = ""
for kk, vv in pairs(v.new_nodes) do
new_node_list = new_node_list .. vv .. " "
end
if new_node_list ~= "" then
print(v.remarks, new_node_list)
end
else
print(v.remarks, v.newNodeId)
end
end
]]--
api.uci_save(uci, appname, true) api.uci_save(uci, appname, true)
end end