diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh b/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh index 7c98e0a314..c6a76d35b5 100755 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh @@ -592,9 +592,9 @@ run_redir() { } node_switch() { - [ -n "$1" -a -n "$2" -a -n "$3" ] && { - local node=$3 - top -bn1 | grep -E "$TMP_PATH" | grep -i "${1}" | grep -v "grep" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 & + [ -n "$1" -a -n "$2" ] && { + local node=$2 + top -bn1 | grep -E "$TMP_PATH" | grep -i "${1}" | grep -v "grep" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 local config_file=$TMP_PATH/${1}.json local log_file=$TMP_PATH/${1}.log eval current_port=\$${1}_REDIR_PORT diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua b/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua index 3a2a96497e..bb463851a4 100755 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua @@ -48,36 +48,33 @@ do local szType = "@global[0]" local option = protocol .. "_node" - local node = ucic2:get(application, szType, option) - local currentNode - if node then - currentNode = ucic2:get_all(application, node) - end + local node_id = ucic2:get(application, szType, option) CONFIG[#CONFIG + 1] = { log = true, remarks = name .. "节点", - node = node, - currentNode = currentNode, - set = function(server) + currentNodeId = node_id, + currentNode = node_id and ucic2:get_all(application, node_id) or nil, + set = function(o, server) ucic2:set(application, szType, option, server) + o.newNodeId = server end } end import_config("tcp") import_config("udp") + local i = 0 ucic2:foreach(application, "socks", function(t) - local node = t.node - local currentNode - if node then - currentNode = ucic2:get_all(application, node) - end + i = i + 1 + local node_id = t.node CONFIG[#CONFIG + 1] = { log = true, - remarks = "Socks节点" .. t[".name"], - currentNode = currentNode, - set = function(server) + remarks = "Socks节点列表[" .. i .. "]", + currentNodeId = node_id, + currentNode = node_id and ucic2:get_all(application, node_id) or nil, + set = function(o, server) ucic2:set(application, t[".name"], "node", server) + o.newNodeId = server end } end) @@ -86,20 +83,15 @@ do if tcp_node_table then local nodes = {} local new_nodes = {} - for k,v in ipairs(tcp_node_table) do - local node = v - local currentNode - if node then - currentNode = ucic2:get_all(application, node) - end + for k,node in ipairs(tcp_node_table) do nodes[#nodes + 1] = { - log = false, - node = node, - currentNode = currentNode, - remarks = node, - set = function(server) + log = true, + remarks = "TCP备用节点的列表[" .. k .. "]", + currentNodeId = node, + currentNode = node and ucic2:get_all(application, node) or nil, + set = function(o, server) for kk, vv in pairs(CONFIG) do - if (vv.remarks == "自动切换TCP_1节点列表") then + if (vv.remarks == "TCP备用节点的列表") then table.insert(vv.new_nodes, server) end end @@ -107,13 +99,13 @@ do } end CONFIG[#CONFIG + 1] = { - remarks = "自动切换TCP节点列表", + remarks = "TCP备用节点的列表", nodes = nodes, new_nodes = new_nodes, - set = function() + set = function(o) for kk, vv in pairs(CONFIG) do - if (vv.remarks == "自动切换TCP_1节点列表") then - log("刷新自动切换列表") + if (vv.remarks == "TCP备用节点的列表") then + log("刷新自动切换的TCP备用节点的列表") ucic2:set_list(application, "@auto_switch[0]", "tcp_node", vv.new_nodes) end end @@ -126,45 +118,39 @@ do local node_id = node[".name"] ucic2:foreach(application, "shunt_rules", function(e) local _node_id = node[e[".name"]] or nil - local _node - if _node_id then - _node = ucic2:get_all(application, _node_id) - end CONFIG[#CONFIG + 1] = { log = false, - currentNode = _node, + currentNodeId = _node_id, + currentNode = _node_id and ucic2:get_all(application, _node_id) or nil, remarks = "分流" .. e.remarks .. "节点", - set = function(server) + set = function(o, server) ucic2:set(application, node_id, e[".name"], server) + o.newNodeId = server end } end) local default_node_id = node.default_node - local default_node - if default_node_id then - default_node = ucic2:get_all(application, default_node_id) - end CONFIG[#CONFIG + 1] = { log = false, - currentNode = default_node, + currentNodeId = default_node_id, + currentNode = default_node_id and ucic2:get_all(application, default_node_id) or nil, remarks = "分流默认节点", - set = function(server) + set = function(o, server) ucic2:set(application, node_id, "default_node", server) + o.newNodeId = server end } local main_node_id = node.main_node - local main_node - if main_node_id then - main_node = ucic2:get_all(application, main_node_id) - end CONFIG[#CONFIG + 1] = { log = false, - currentNode = main_node, + currentNodeId = main_node_id, + currentNode = main_node_id and ucic2:get_all(application, main_node_id) or nil, remarks = "分流默认前置代理节点", - set = function(server) + set = function(o, server) ucic2:set(application, node_id, "main_node", server) + o.newNodeId = server end } elseif node.protocol and node.protocol == '_balancing' then @@ -172,18 +158,13 @@ do local nodes = {} local new_nodes = {} if node.balancing_node then - for k, v in pairs(node.balancing_node) do - local node = v - local currentNode - if node then - currentNode = ucic2:get_all(application, node) - end + for k, node in pairs(node.balancing_node) do nodes[#nodes + 1] = { log = false, node = node, - currentNode = currentNode, + currentNode = node and ucic2:get_all(application, node) or nil, remarks = node, - set = function(server) + set = function(o, server) for kk, vv in pairs(CONFIG) do if (vv.remarks == "负载均衡节点列表" .. node_id) then table.insert(vv.new_nodes, server) @@ -197,7 +178,7 @@ do remarks = "负载均衡节点列表" .. node_id, nodes = nodes, new_nodes = new_nodes, - set = function() + set = function(o) for kk, vv in pairs(CONFIG) do if (vv.remarks == "负载均衡节点列表" .. node_id) then log("刷新负载均衡节点列表") @@ -642,90 +623,86 @@ end local function select_node(nodes, config) local server if config.currentNode then - -- 特别优先级 分流 + 备注 - if config.currentNode.protocol and config.currentNode.protocol == '_shunt' then - for id, node in pairs(nodes) do + for id, node in pairs(nodes) do + -- 特别优先级 分流 + 备注 + if config.currentNode.protocol and config.currentNode.protocol == '_shunt' then if node.remarks == config.currentNode.remarks then - log('选择【' .. config.remarks .. '】分流匹配节点:' .. node.remarks) + log('更新【' .. config.remarks .. '】分流匹配节点:' .. node.remarks) server = id break end end - end - -- 特别优先级 负载均衡 + 备注 - if config.currentNode.protocol and config.currentNode.protocol == '_balancing' then - for id, node in pairs(nodes) do + -- 特别优先级 负载均衡 + 备注 + if config.currentNode.protocol and config.currentNode.protocol == '_balancing' then if node.remarks == config.currentNode.remarks then - log('选择【' .. config.remarks .. '】负载均衡匹配节点:' .. node.remarks) + log('更新【' .. config.remarks .. '】负载均衡匹配节点:' .. node.remarks) server = id break end end - end - -- 第一优先级 cfgid - if not server then - for id, node in pairs(nodes) do + -- 第一优先级 cfgid + if not server then if id == config.currentNode['.name'] then if config.log == nil or config.log == true then - log('选择【' .. config.remarks .. '】第一匹配节点:' .. node.remarks) + log('更新【' .. config.remarks .. '】第一匹配节点:' .. node.remarks) end server = id break end end - end - -- 第二优先级 类型 + IP + 端口 - if not server then - for id, node in pairs(nodes) do - if node.type and node.address and node.port then - if node.type == config.currentNode.type and (node.address .. ':' .. node.port == config.currentNode.address .. ':' .. config.currentNode.port) then - if config.log == nil or config.log == true then - log('选择【' .. config.remarks .. '】第二匹配节点:' .. node.remarks) + -- 第二优先级 类型 + IP + 端口 + if not server then + if config.currentNode.type and config.currentNode.address and config.currentNode.port then + if node.type and node.address and node.port then + if node.type == config.currentNode.type and (node.address .. ':' .. node.port == config.currentNode.address .. ':' .. config.currentNode.port) then + if config.log == nil or config.log == true then + log('更新【' .. config.remarks .. '】第二匹配节点:' .. node.remarks) + end + server = id + break end - server = id - break end end end - end - -- 第三优先级 IP + 端口 - if not server then - for id, node in pairs(nodes) do - if node.address and node.port then - if node.address .. ':' .. node.port == config.currentNode.address .. ':' .. config.currentNode.port then - if config.log == nil or config.log == true then - log('选择【' .. config.remarks .. '】第三匹配节点:' .. node.remarks) + -- 第三优先级 IP + 端口 + if not server then + if config.currentNode.address and config.currentNode.port then + if node.address and node.port then + if node.address .. ':' .. node.port == config.currentNode.address .. ':' .. config.currentNode.port then + if config.log == nil or config.log == true then + log('更新【' .. config.remarks .. '】第三匹配节点:' .. node.remarks) + end + server = id + break end - server = id - break end end end - end - -- 第四优先级 IP - if not server then - for id, node in pairs(nodes) do - if node.address then - if node.address == config.currentNode.address then - if config.log == nil or config.log == true then - log('选择【' .. config.remarks .. '】第四匹配节点:' .. node.remarks) + -- 第四优先级 IP + if not server then + if config.currentNode.address then + if node.address then + if node.address == config.currentNode.address then + if config.log == nil or config.log == true then + log('更新【' .. config.remarks .. '】第四匹配节点:' .. node.remarks) + end + server = id + break end - server = id - break end end end - end - -- 第五优先级备注 - if not server then - for id, node in pairs(nodes) do - if node.remarks then - if node.remarks == config.currentNode.remarks then - if config.log == nil or config.log == true then - log('选择【' .. config.remarks .. '】第五匹配节点:' .. node.remarks) + -- 第五优先级备注 + if not server then + if config.currentNode.remarks then + if node.remarks then + if node.remarks == config.currentNode.remarks then + if config.log == nil or config.log == true then + log('更新【' .. config.remarks .. '】第五匹配节点:' .. node.remarks) + end + server = id + break end - server = id - break end end end @@ -742,7 +719,7 @@ local function select_node(nodes, config) end end if server then - config.set(server) + config.set(config, server) end end @@ -776,12 +753,13 @@ local function update_node(manual) ucic3:foreach(application, uciType, function(node) nodes[node['.name']] = node end) + for _, config in pairs(CONFIG) do if config.nodes and type(config.nodes) == "table" then for kk, vv in pairs(config.nodes) do select_node(nodes, vv) end - config.set() + config.set(config) else select_node(nodes, config) end @@ -790,11 +768,15 @@ local function update_node(manual) --[[ 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 - print(vv) + new_node_list = new_node_list .. vv .. " " + end + if new_node_list ~= "" then + print(v.remarks, new_node_list) end else - print(v.new_nodes) + print(v.remarks, v.newNodeId) end end ]]-- diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/test.sh b/package/lienol/luci-app-passwall/root/usr/share/passwall/test.sh index d211307766..a16395d16e 100755 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/test.sh +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/test.sh @@ -56,11 +56,11 @@ test_proxy() { } test_auto_switch() { - local type=$1 - local b_tcp_nodes=$3 + local TYPE=$1 + local b_tcp_nodes=$2 local now_node - if [ -f "/var/etc/$CONFIG/id/${type}" ]; then - now_node=$(cat /var/etc/$CONFIG/id/${type}) + if [ -f "/var/etc/$CONFIG/id/${TYPE}" ]; then + now_node=$(cat /var/etc/$CONFIG/id/${TYPE}) else return 1 fi @@ -96,18 +96,18 @@ test_auto_switch() { top -bn1 | grep -v "grep" | grep "/var/etc/${CONFIG}/auto_switch.json" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 if [ "$proxy_status" -eq 200 ]; then #主节点正常,切换到主节点 - echolog "自动切换检测:${type}主节点正常,切换到主节点!" - /usr/share/${CONFIG}/app.sh node_switch $type $2 $main_node + echolog "自动切换检测:${TYPE}主节点正常,切换到主节点!" + /usr/share/${CONFIG}/app.sh node_switch ${TYPE} ${main_node} return 0 fi fi fi if [ "$status" == 0 ]; then - #echolog "自动切换检测:${type}节点【$(config_n_get $now_node type) $(config_n_get $now_node remarks)】正常。" + #echolog "自动切换检测:${TYPE}节点【$(config_n_get $now_node type) $(config_n_get $now_node remarks)】正常。" return 0 elif [ "$status" == 1 ]; then - echolog "自动切换检测:${type}节点异常,开始切换节点!" + echolog "自动切换检测:${TYPE}节点异常,开始切换节点!" local new_node in_backup_nodes=$(echo $b_tcp_nodes | grep $now_node) # 判断当前节点是否存在于备用节点列表里 @@ -124,15 +124,15 @@ test_auto_switch() { new_node=$next_node fi fi - /usr/share/${CONFIG}/app.sh node_switch $type $2 $new_node + /usr/share/${CONFIG}/app.sh node_switch ${TYPE} ${new_node} sleep 10s # 切换节点后等待10秒后再检测一次,如果还是不通继续切,直到可用为止 status2=$(test_proxy) if [ "$status2" -eq 0 ]; then - echolog "自动切换检测:${type}节点切换完毕!" + echolog "自动切换检测:${TYPE}节点切换完毕!" return 0 elif [ "$status2" -eq 1 ]; then - test_auto_switch $1 $2 "$3" + test_auto_switch ${TYPE} "${b_tcp_nodes}" elif [ "$status2" -eq 2 ]; then return 2 fi @@ -150,7 +150,7 @@ start() { do TCP_NODE=$(config_t_get auto_switch tcp_node nil) [ -n "$TCP_NODE" -a "$TCP_NODE" != "nil" ] && { - test_auto_switch TCP tcp "$TCP_NODE" + test_auto_switch TCP "$TCP_NODE" } delay=$(config_t_get auto_switch testing_time 1) sleep ${delay}m