From 05843bc6eea89cabd339e0e512275543422c0ba5 Mon Sep 17 00:00:00 2001 From: sbwml Date: Tue, 19 Sep 2023 21:32:03 +0800 Subject: [PATCH] luci-app-passwall2: sync upstream --- luci-app-passwall2/Makefile | 2 +- .../luasrc/passwall2/util_sing-box.lua | 246 +++++++----------- 2 files changed, 88 insertions(+), 160 deletions(-) diff --git a/luci-app-passwall2/Makefile b/luci-app-passwall2/Makefile index b5f2e72dc..efa7d2015 100644 --- a/luci-app-passwall2/Makefile +++ b/luci-app-passwall2/Makefile @@ -5,7 +5,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-passwall2 -PKG_VERSION:=1.20-12 +PKG_VERSION:=1.20-14 PKG_RELEASE:= PKG_CONFIG_DEPENDS:= \ diff --git a/luci-app-passwall2/luasrc/passwall2/util_sing-box.lua b/luci-app-passwall2/luasrc/passwall2/util_sing-box.lua index 986ce2bec..d8e71b1f4 100644 --- a/luci-app-passwall2/luasrc/passwall2/util_sing-box.lua +++ b/luci-app-passwall2/luasrc/passwall2/util_sing-box.lua @@ -692,9 +692,7 @@ function gen_config(var) local dns_cache = var["-dns_cache"] local tags = var["-tags"] - local dns_direct_domains = {} - local dns_remote_domains = {} - local dns_block_domains = {} + local dns_domain_rules = {} local dns = nil local inbounds = {} local outbounds = {} @@ -717,12 +715,9 @@ function gen_config(var) local experimental = nil - local nodes = {} + local node = nil if node_id then - local node = uci:get_all(appname, node_id) - if node then - nodes[node_id] = node - end + node = uci:get_all(appname, node_id) end if local_socks_port then @@ -789,10 +784,9 @@ function gen_config(var) table.insert(inbounds, inbound_tproxy) end - local dns_outTag = nil + local default_outTag = nil - for k, v in pairs(nodes) do - local node = v + if node then if node.protocol == "_shunt" then local rules = {} @@ -997,40 +991,37 @@ function gen_config(var) end if e.domain_list then - local domain = {} - local domain_suffix = {} - local domain_keyword = {} - local domain_regex = {} - local geosite = {} + local domain_table = { + outboundTag = outboundTag, + domain = {}, + domain_suffix = {}, + domain_keyword = {}, + domain_regex = {}, + geosite = {}, + } string.gsub(e.domain_list, '[^' .. "\r\n" .. ']+', function(w) if w:find("geosite:") == 1 then - table.insert(geosite, w:sub(1 + #"geosite:")) + table.insert(domain_table.geosite, w:sub(1 + #"geosite:")) elseif w:find("regexp:") == 1 then - table.insert(domain_regex, w:sub(1 + #"regexp:")) + table.insert(domain_table.domain_regex, w:sub(1 + #"regexp:")) elseif w:find("full:") == 1 then - table.insert(domain, w:sub(1 + #"full:")) + table.insert(domain_table.domain, w:sub(1 + #"full:")) elseif w:find("domain:") == 1 then - table.insert(domain_keyword, w:sub(1 + #"domain:")) + table.insert(domain_table.domain, w:sub(1 + #"domain:")) + table.insert(domain_table.domain_suffix, "." .. w:sub(1 + #"domain:")) else - table.insert(domain, w) - end - - if outboundTag == "direct" then - table.insert(dns_direct_domains, w) - elseif outboundTag == "block" then - table.insert(dns_block_domains, w) - else - if outboundTag ~= "nil" then - table.insert(dns_remote_domains, w) - end + table.insert(domain_table.domain_keyword, w) end end) + rule.domain = #domain_table.domain > 0 and domain_table.domain or nil + rule.domain_suffix = #domain_table.domain_suffix > 0 and domain_table.domain_suffix or nil + rule.domain_keyword = #domain_table.domain_keyword > 0 and domain_table.domain_keyword or nil + rule.domain_regex = #domain_table.domain_regex > 0 and domain_table.domain_regex or nil + rule.geosite = #domain_table.geosite > 0 and domain_table.geosite or nil - rule.domain = #domain > 0 and domain or nil - rule.domain_suffix = #domain_suffix > 0 and domain_suffix or nil - rule.domain_keyword = #domain_keyword > 0 and domain_keyword or nil - rule.domain_regex = #domain_regex > 0 and domain_regex or nil - rule.geosite = #geosite > 0 and geosite or nil + if outboundTag and outboundTag ~= "nil" then + table.insert(dns_domain_rules, domain_table) + end end if e.ip_list then @@ -1054,7 +1045,7 @@ function gen_config(var) if default_outboundTag then route.final = default_outboundTag - dns_outTag = default_outboundTag + default_outTag = default_outboundTag end for index, value in ipairs(rules) do @@ -1076,7 +1067,7 @@ function gen_config(var) outbound = gen_outbound(flag, node) end if outbound then - dns_outTag = outbound.tag + default_outTag = outbound.tag table.insert(outbounds, outbound) end @@ -1095,35 +1086,10 @@ function gen_config(var) fakeip = nil, } - local dns_tag = "remote" - - local domain = {} - local domain_suffix = {} - local domain_keyword = {} - local domain_regex = {} - local geosite = {} - for index, value in ipairs(dns_remote_domains) do - if value:find("geosite:") == 1 then - table.insert(geosite, value:sub(1 + #"geosite:")) - elseif value:find("regexp:") == 1 then - table.insert(domain_regex, value:sub(1 + #"regexp:")) - elseif value:find("full:") == 1 then - table.insert(domain, value:sub(1 + #"full:")) - elseif value:find("domain:") == 1 then - table.insert(domain_keyword, value:sub(1 + #"domain:")) - else - table.insert(domain, value) - end - end - local remote_rule = { - server = dns_tag, - domain = #domain > 0 and domain or nil, - domain_suffix = #domain_suffix > 0 and domain_suffix or nil, - domain_keyword = #domain_keyword > 0 and domain_keyword or nil, - domain_regex = #domain_regex > 0 and domain_regex or nil, - geosite = #geosite > 0 and geosite or nil, - disable_cache = true, - } + table.insert(dns.servers, { + tag = "block", + address = "rcode://success", + }) local remote_strategy = "prefer_ipv6" if remote_dns_query_strategy == "UseIPv4" then @@ -1132,33 +1098,33 @@ function gen_config(var) remote_strategy = "ipv6_only" end - local server = { - tag = dns_tag, + local remote_server = { + tag = "remote", address_strategy = "prefer_ipv4", strategy = remote_strategy, address_resolver = "direct", - detour = dns_outTag, + detour = default_outTag, } if remote_dns_udp_server then local server_port = tonumber(remote_dns_udp_port) or 53 - server.address = "udp://" .. remote_dns_udp_server .. ":" .. server_port + remote_server.address = "udp://" .. remote_dns_udp_server .. ":" .. server_port end if remote_dns_tcp_server then local server_port = tonumber(remote_dns_tcp_port) or 53 - server.address = "tcp://" .. remote_dns_tcp_server .. ":" .. server_port + remote_server.address = "tcp://" .. remote_dns_tcp_server .. ":" .. server_port end if remote_dns_doh_url then - server.address = remote_dns_doh_url + remote_server.address = remote_dns_doh_url end - if server.address then - table.insert(dns.servers, server) + if remote_server.address then + table.insert(dns.servers, remote_server) end - local fakedns_tag = dns_tag .. "_fakeip" + local fakedns_tag = "remote_fakeip" if remote_dns_fake then dns.fakeip = { enabled = true, @@ -1182,56 +1148,18 @@ function gen_config(var) } end end - - if remote_rule.domain or remote_rule.domain_suffix or remote_rule.domain_keyword or remote_rule.domain_regex or remote_rule.geosite then - local rule = api.clone(remote_rule) - rule.server = dns_tag - if remote_dns_fake then - rule.query_type = { - "A", "AAAA" - } - rule.server = fakedns_tag - end - table.insert(dns.rules, rule) - end if direct_dns_udp_server then + local domain = {} local nodes_domain_text = sys.exec('uci show passwall2 | grep ".address=" | cut -d "\'" -f 2 | grep "[a-zA-Z]$" | sort -u') string.gsub(nodes_domain_text, '[^' .. "\r\n" .. ']+', function(w) - table.insert(dns_direct_domains, "full:" .. w) + table.insert(domain, w) end) - - local dns_tag = "direct" - - local domain = {} - local domain_suffix = {} - local domain_keyword = {} - local domain_regex = {} - local geosite = {} - for index, value in ipairs(dns_direct_domains) do - if value:find("geosite:") == 1 then - table.insert(geosite, value:sub(1 + #"geosite:")) - elseif value:find("regexp:") == 1 then - table.insert(domain_regex, value:sub(1 + #"regexp:")) - elseif value:find("full:") == 1 then - table.insert(domain, value:sub(1 + #"full:")) - elseif value:find("domain:") == 1 then - table.insert(domain_keyword, value:sub(1 + #"domain:")) - else - table.insert(domain, value) - end - end - local direct_rule = { - server = dns_tag, - domain = #domain > 0 and domain or nil, - domain_suffix = #domain_suffix > 0 and domain_suffix or nil, - domain_keyword = #domain_keyword > 0 and domain_keyword or nil, - domain_regex = #domain_regex > 0 and domain_regex or nil, - geosite = #geosite > 0 and geosite or nil, - disable_cache = false, - } - if direct_rule.domain or direct_rule.domain_suffix or direct_rule.domain_keyword or direct_rule.domain_regex or direct_rule.geosite then - table.insert(dns.rules, direct_rule) + if #domain > 0 then + table.insert(dns_domain_rules, { + outboundTag = "direct", + domain = domain + }) end local direct_strategy = "prefer_ipv6" @@ -1244,7 +1172,7 @@ function gen_config(var) local port = tonumber(direct_dns_udp_port) or 53 table.insert(dns.servers, { - tag = dns_tag, + tag = "direct", address = "udp://" .. direct_dns_udp_server .. ":" .. port, address_strategy = "prefer_ipv6", strategy = direct_strategy, @@ -1252,43 +1180,6 @@ function gen_config(var) }) end - table.insert(dns.servers, { - tag = "block", - address = "rcode://success", - }) - - local block_domain = {} - local block_domain_suffix = {} - local block_domain_keyword = {} - local block_domain_regex = {} - local block_geosite = {} - for index, value in ipairs(dns_block_domains) do - if value:find("geosite:") == 1 then - table.insert(block_geosite, value:sub(1 + #"geosite:")) - elseif value:find("regexp:") == 1 then - table.insert(block_domain_regex, value:sub(1 + #"regexp:")) - elseif value:find("full:") == 1 then - table.insert(block_domain, value:sub(1 + #"full:")) - elseif value:find("domain:") == 1 then - table.insert(block_domain_keyword, value:sub(1 + #"domain:")) - else - table.insert(block_domain, value) - end - end - local block_rule = { - server = "block", - domain = #block_domain > 0 and block_domain or nil, - domain_suffix = #block_domain_suffix > 0 and block_domain_suffix or nil, - domain_keyword = #block_domain_keyword > 0 and block_domain_keyword or nil, - domain_regex = #block_domain_regex > 0 and block_domain_regex or nil, - geosite = #block_geosite > 0 and block_geosite or nil, - disable_cache = true, - } - - if block_rule.domain or block_rule.domain_suffix or block_rule.domain_keyword or block_rule.domain_regex or block_rule.geosite then - table.insert(dns.rules, block_rule) - end - local default_dns_flag = "remote" if node_id and redir_port then local node = uci:get_all(appname, node_id) @@ -1300,6 +1191,43 @@ function gen_config(var) else default_dns_flag = "direct" end dns.final = default_dns_flag + + --按分流顺序DNS + if dns_domain_rules and #dns_domain_rules > 0 then + for index, value in ipairs(dns_domain_rules) do + if value.outboundTag and (value.domain or value.domain_suffix or value.domain_keyword or value.domain_regex or value.geosite) then + local dns_rule = { + server = value.outboundTag, + domain = (value.domain and #value.domain > 0) and value.domain or nil, + domain_suffix = (value.domain_suffix and #value.domain_suffix > 0) and value.domain_suffix or nil, + domain_keyword = (value.domain_keyword and #value.domain_keyword > 0) and value.domain_keyword or nil, + domain_regex = (value.domain_regex and #value.domain_regex > 0) and value.domain_regex or nil, + geosite = (value.geosite and #value.geosite > 0) and value.geosite or nil, + disable_cache = false, + } + if value.outboundTag ~= "block" and value.outboundTag ~= "direct" then + dns_rule.server = "remote" + if value.outboundTag ~= "default" 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 + table.insert(dns.servers, remote_dns_server) + dns_rule.server = remote_dns_server.tag + end + if remote_dns_fake then + local fakedns_dns_rule = api.clone(dns_rule) + fakedns_dns_rule.query_type = { + "A", "AAAA" + } + fakedns_dns_rule.server = fakedns_tag + fakedns_dns_rule.disable_cache = true + table.insert(dns.rules, fakedns_dns_rule) + end + end + table.insert(dns.rules, dns_rule) + end + end + end table.insert(inbounds, { type = "direct",