From 8f0ae46d96f053584737a5f9aaefc252b15b2fac Mon Sep 17 00:00:00 2001 From: gitea-action Date: Sat, 16 Nov 2024 17:30:20 +0800 Subject: [PATCH] luci-app-passwall: sync upstream last commit: https://github.com/xiaorouji/openwrt-passwall/commit/3f6188033793067764bc55bdcaab4ef865d298fa --- .../luasrc/passwall/util_sing-box.lua | 74 ++++++------ .../luasrc/passwall/util_xray.lua | 112 +++++++++--------- 2 files changed, 96 insertions(+), 90 deletions(-) diff --git a/luci-app-passwall/luasrc/passwall/util_sing-box.lua b/luci-app-passwall/luasrc/passwall/util_sing-box.lua index 4ae77e5a3..2367441b3 100644 --- a/luci-app-passwall/luasrc/passwall/util_sing-box.lua +++ b/luci-app-passwall/luasrc/passwall/util_sing-box.lua @@ -22,30 +22,23 @@ function gen_outbound(flag, node, tag, proxy_table) local result = nil if node and node ~= "nil" then local node_id = node[".name"] - if tag == nil then - tag = node_id - end + local node_remarks = node.remarks - local proxy = 0 - local proxy_tag = "nil" + local proxy_tag = nil if proxy_table ~= nil and type(proxy_table) == "table" then - proxy = proxy_table.proxy or 0 - proxy_tag = proxy_table.tag or "nil" - end - - if node.type == "sing-box" then - proxy = 0 - if proxy_tag ~= "nil" then - node.detour = proxy_tag - end + proxy_tag = proxy_table.tag or nil end if node.type ~= "sing-box" then + local config_tag = tag + if config_tag == nil then + config_tag = node_id + end local relay_port = node.port new_port = get_new_port() - local config_file = string.format("%s_%s_%s.json", flag, tag, new_port) - if tag and node_id and tag ~= node_id then - config_file = string.format("%s_%s_%s_%s.json", flag, tag, node_id, new_port) + local config_file = string.format("%s_%s_%s.json", flag, config_tag, new_port) + if config_tag and node_id and config_tag ~= node_id then + config_file = string.format("%s_%s_%s_%s.json", flag, config_tag, node_id, new_port) end sys.call(string.format('/usr/share/%s/app.sh run_socks "%s"> /dev/null', appname, @@ -55,7 +48,7 @@ function gen_outbound(flag, node, tag, proxy_table) "127.0.0.1", --bind new_port, --socks port config_file, --config file - (proxy == 1 and relay_port) and tostring(relay_port) or "" --relay port + (proxy_tag and proxy_tag ~= "nil" and relay_port) and tostring(relay_port) or "" --relay port ) ) ) @@ -64,11 +57,19 @@ function gen_outbound(flag, node, tag, proxy_table) address = "127.0.0.1", port = new_port } + else + if proxy_tag and proxy_tag ~= "nil" then + node.detour = proxy_tag + end + end + + if tag == nil then + tag = node_id .. "_" .. node_remarks end result = { - _flag_tag = node_id, - _flag_proxy = proxy, + _id = node_id, + _flag = flag, _flag_proxy_tag = proxy_tag, tag = tag, type = node.protocol, @@ -778,6 +779,7 @@ function gen_config(var) local dns = nil local inbounds = {} local outbounds = {} + local COMMON = {} local singbox_settings = uci:get_all(appname, "@global_singbox[0]") or {} @@ -797,7 +799,6 @@ function gen_config(var) local experimental = nil - local default_outTag = nil if node_id then local node = uci:get_all(appname, node_id) if node then @@ -1000,7 +1001,7 @@ function gen_config(var) local proxy = preproxy_enabled and node[rule_name .. "_proxy_tag"] == preproxy_tag and _node_id ~= preproxy_node_id local copied_outbound for index, value in ipairs(outbounds) do - if value["_flag_tag"] == _node_id and value["_flag_proxy_tag"] == preproxy_tag then + if value["_id"] == _node_id and value["_flag_proxy_tag"] == preproxy_tag then copied_outbound = api.clone(value) break end @@ -1036,23 +1037,23 @@ function gen_config(var) }) end end - local _outbound = gen_outbound(flag, _node, rule_name, { proxy = proxy and 1 or 0, tag = proxy and preproxy_tag or nil }) + local outbound_tag = rule_name .. ":" .. _node.remarks + local _outbound = gen_outbound(flag, _node, outbound_tag, { tag = proxy and preproxy_tag or nil }) if _outbound then - set_outbound_detour(_node, _outbound, outbounds, rule_name) + rule_outboundTag = set_outbound_detour(_node, _outbound, outbounds, outbound_tag) table.insert(outbounds, _outbound) - rule_outboundTag = rule_name end end elseif _node.protocol == "_iface" then if _node.iface then local _outbound = { type = "direct", - tag = rule_name, + tag = rule_name .. ":" .. _node.remarks, bind_interface = _node.iface, routing_mark = 255, } table.insert(outbounds, _outbound) - rule_outboundTag = rule_name + rule_outboundTag = _outbound.tag sys.call("touch /tmp/etc/passwall/iface/" .. _node.iface) end end @@ -1211,8 +1212,7 @@ function gen_config(var) end) if default_outboundTag then - route.final = default_outboundTag - default_outTag = default_outboundTag + COMMON.default_outbound_tag = default_outboundTag end for index, value in ipairs(rules) do @@ -1222,25 +1222,27 @@ function gen_config(var) if node.iface then local outbound = { type = "direct", - tag = node_id, + tag = node.remarks or node_id, bind_interface = node.iface, routing_mark = 255, } table.insert(outbounds, outbound) - default_outTag = outbound.tag - route.final = default_outTag + COMMON.default_outbound_tag = outbound.tag sys.call("touch /tmp/etc/passwall/iface/" .. node.iface) end else local outbound = gen_outbound(flag, node) if outbound then - default_outTag = set_outbound_detour(node, outbound, outbounds) + COMMON.default_outbound_tag = set_outbound_detour(node, outbound, outbounds) table.insert(outbounds, outbound) - route.final = default_outTag end end end + if COMMON.default_outbound_tag then + route.final = COMMON.default_outbound_tag + end + if dns_listen_port then dns = { servers = {}, @@ -1265,6 +1267,8 @@ function gen_config(var) server = dns_socks_address, server_port = tonumber(dns_socks_port) }) + else + default_outTag = COMMON.default_outbound_tag end local remote_strategy = "prefer_ipv6" @@ -1406,7 +1410,7 @@ function gen_config(var) } if value.outboundTag ~= "block" and value.outboundTag ~= "direct" then dns_rule.server = "remote" - if value.outboundTag ~= "default" and remote_server.address then + if value.outboundTag ~= COMMON.default_outbound_tag and remote_server.address then local remote_dns_server = api.clone(remote_server) remote_dns_server.tag = value.outboundTag remote_dns_server.detour = value.outboundTag diff --git a/luci-app-passwall/luasrc/passwall/util_xray.lua b/luci-app-passwall/luasrc/passwall/util_xray.lua index e14b80a20..3bc787259 100644 --- a/luci-app-passwall/luasrc/passwall/util_xray.lua +++ b/luci-app-passwall/luasrc/passwall/util_xray.lua @@ -50,44 +50,31 @@ function gen_outbound(flag, node, tag, proxy_table) local result = nil if node and node ~= "nil" then local node_id = node[".name"] - if tag == nil then - tag = node_id - end + local node_remarks = node.remarks - local proxy = 0 - local proxy_tag = "nil" + local proxy_tag = nil local fragment = nil local noise = nil if proxy_table ~= nil and type(proxy_table) == "table" then - proxy = proxy_table.proxy or 0 - proxy_tag = proxy_table.tag or "nil" + proxy_tag = proxy_table.tag or nil fragment = proxy_table.fragment or nil noise = proxy_table.noise or nil end - if node.type == "Xray" then - if node.flow == "xtls-rprx-vision" then - else - proxy = 0 - if proxy_tag ~= "nil" then - node.proxySettings = { - tag = proxy_tag, - transportLayer = true - } - end - end - end - if node.type ~= "Xray" then if node.type == "Socks" then node.protocol = "socks" node.transport = "tcp" else + local config_tag = tag + if config_tag == nil then + config_tag = node_id + end local relay_port = node.port new_port = get_new_port() - local config_file = string.format("%s_%s_%s.json", flag, tag, new_port) - if tag and node_id and tag ~= node_id then - config_file = string.format("%s_%s_%s_%s.json", flag, tag, node_id, new_port) + local config_file = string.format("%s_%s_%s.json", flag, config_tag, new_port) + if config_tag and node_id and config_tag ~= node_id then + config_file = string.format("%s_%s_%s_%s.json", flag, config_tag, node_id, new_port) end sys.call(string.format('/usr/share/%s/app.sh run_socks "%s"> /dev/null', appname, @@ -97,7 +84,7 @@ function gen_outbound(flag, node, tag, proxy_table) "127.0.0.1", --bind new_port, --socks port config_file, --config file - (proxy == 1 and relay_port) and tostring(relay_port) or "" --relay port + (proxy_tag and proxy_tag ~= "nil" and relay_port) and tostring(relay_port) or "" --relay port ) )) node = {} @@ -107,6 +94,16 @@ function gen_outbound(flag, node, tag, proxy_table) node.port = new_port end node.stream_security = "none" + else + if node.flow == "xtls-rprx-vision" then + else + if proxy_tag and proxy_tag ~= "nil" then + node.proxySettings = { + tag = proxy_tag, + transportLayer = true + } + end + end end if node.type == "Xray" then @@ -142,9 +139,13 @@ function gen_outbound(flag, node, tag, proxy_table) node.wireguard_reserved = #bytes > 0 and bytes or nil end + if tag == nil then + tag = node_id .. "_" .. node_remarks + end + result = { - _flag_tag = node_id, - _flag_proxy = proxy, + _id = node_id, + _flag = flag, _flag_proxy_tag = proxy_tag, tag = tag, proxySettings = node.proxySettings or nil, @@ -450,7 +451,6 @@ function gen_config_server(node) domainStrategy = "IPOnDemand", rules = { { - type = "field", ip = {"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"}, outboundTag = (node.accept_lan == nil or node.accept_lan == "0") and "blocked" or "direct" } @@ -641,6 +641,7 @@ function gen_config(var) local observatory = nil local inbounds = {} local outbounds = {} + local COMMON = {} local xray_settings = uci:get_all(appname, "@global_xray[0]") or {} @@ -817,7 +818,7 @@ function gen_config(var) end end local inbound_tag = gen_loopback(loopback_tag, loopback_dst) - table.insert(rules, { type = "field", inboundTag = { inbound_tag }, balancerTag = balancer_tag }) + table.insert(rules, { inboundTag = { inbound_tag }, balancerTag = balancer_tag }) return balancer_tag end @@ -897,7 +898,7 @@ function gen_config(var) if use_proxy and proxy_balancer_tag and proxy_nodes[_node_id] then use_proxy = false end local copied_outbound for index, value in ipairs(outbounds) do - if value["_flag_tag"] == _node_id and value["_flag_proxy_tag"] == (use_proxy and proxy_tag or "nil") then + if value["_id"] == _node_id and value["_flag_proxy_tag"] == (use_proxy and proxy_tag or nil) then copied_outbound = api.clone(value) break end @@ -923,23 +924,23 @@ function gen_config(var) _node.address = "127.0.0.1" _node.port = new_port table.insert(rules, 1, { - type = "field", inboundTag = {"proxy_" .. rule_name}, outboundTag = not proxy_balancer_tag and proxy_tag or nil, balancerTag = proxy_balancer_tag }) end local proxy_table = { - proxy = use_proxy and 1 or 0, tag = use_proxy and proxy_tag or nil } - if xray_settings.fragment == "1" and not proxy_table.tag then - proxy_table.fragment = true + if not proxy_table.tag then + if xray_settings.fragment == "1" then + proxy_table.fragment = true + end + if xray_settings.noise == "1" then + proxy_table.noise = true + end end - if xray_settings.noise == "1" and not proxy_table.tag then - proxy_table.noise = true - end - local outbound = gen_outbound(flag, _node, rule_name, proxy_table) + local outbound = gen_outbound(flag, _node, rule_name .. ":" .. _node.remarks, proxy_table) local outbound_tag if outbound then set_outbound_detour(_node, outbound, outbounds, rule_name) @@ -997,6 +998,8 @@ function gen_config(var) --default_node local default_node_id = node.default_node or "_direct" local default_outbound_tag, default_balancer_tag = gen_shunt_node("default", default_node_id) + COMMON.default_outbound_tag = default_outbound_tag + COMMON.default_balancer_tag = default_balancer_tag --shunt rule uci:foreach(appname, "shunt_rules", function(e) local outbound_tag, balancer_tag = gen_shunt_node(e[".name"]) @@ -1055,8 +1058,7 @@ function gen_config(var) end) end local rule = { - _flag = e.remarks, - type = "field", + ruleTag = e.remarks, inboundTag = inbound_tag, outboundTag = outbound_tag, balancerTag = balancer_tag, @@ -1068,13 +1070,13 @@ function gen_config(var) } if domains then local _rule = api.clone(rule) - _rule["_flag"] = _rule["_flag"] .. "_domains" + _rule.ruleTag = _rule.ruleTag .. " Domains" _rule.domains = domains table.insert(rules, _rule) end if ip then local _rule = api.clone(rule) - _rule["_flag"] = _rule["_flag"] .. "_ip" + _rule.ruleTag = _rule.ruleTag .. " IP" _rule.ip = ip table.insert(rules, _rule) end @@ -1087,7 +1089,6 @@ function gen_config(var) --[[ if default_outbound_tag or default_balancer_tag then table.insert(rules, { - type = "field", outboundTag = default_outbound_tag, balancerTag = default_balancer_tag, network = "tcp,udp" @@ -1105,18 +1106,19 @@ function gen_config(var) if node.balancing_node then local balancer_tag = gen_balancer(node) if balancer_tag then - table.insert(rules, { type = "field", network = "tcp,udp", balancerTag = balancer_tag }) + table.insert(rules, { network = "tcp,udp", balancerTag = balancer_tag }) end routing = { balancers = balancers, rules = rules } + COMMON.default_balancer_tag = balancer_tag end elseif node.protocol == "_iface" then if node.iface then local outbound = { protocol = "freedom", - tag = "outbound", + tag = node.remarks or node_id, streamSettings = { sockopt = { mark = 255, @@ -1125,12 +1127,13 @@ function gen_config(var) } } table.insert(outbounds, outbound) + COMMON.default_outbound_tag = outbound.tag sys.call("touch /tmp/etc/passwall/iface/" .. node.iface) end else local outbound = gen_outbound(flag, node, nil, { fragment = xray_settings.fragment == "1" or nil, noise = xray_settings.fragment == "1" or nil }) if outbound then - set_outbound_detour(node, outbound, outbounds) + COMMON.default_outbound_tag = set_outbound_detour(node, outbound, outbounds) table.insert(outbounds, outbound) end routing = { @@ -1231,12 +1234,8 @@ function gen_config(var) } }) else - if node_id and tcp_redir_port then - dns_outbound_tag = node_id - local node = uci:get_all(appname, node_id) - if node.protocol == "_shunt" then - dns_outbound_tag = "default" - end + if COMMON.default_outbound_tag then + dns_outbound_tag = COMMON.default_outbound_tag end end @@ -1268,7 +1267,6 @@ function gen_config(var) }) table.insert(routing.rules, 1, { - type = "field", inboundTag = { "dns-in" }, @@ -1276,7 +1274,6 @@ function gen_config(var) }) end table.insert(rules, { - type = "field", inboundTag = { "dns-in1" }, @@ -1288,7 +1285,6 @@ function gen_config(var) }) if _remote_dns_host then table.insert(rules, { - type = "field", inboundTag = { "dns-in1" }, @@ -1301,7 +1297,6 @@ function gen_config(var) end if remote_dns_doh_ip then table.insert(rules, { - type = "field", inboundTag = { "dns-in1" }, @@ -1409,6 +1404,13 @@ function gen_config(var) protocol = "blackhole", tag = "blackhole" }) + for index, value in ipairs(config.outbounds) do + for k, v in pairs(config.outbounds[index]) do + if k:find("_") == 1 then + config.outbounds[index][k] = nil + end + end + end return jsonc.stringify(config, 1) end end