mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-11-03 22:43:56 +08:00
update 2026-10-16 00:09:36
This commit is contained in:
1 parent
eaef2c6d95
commit
9122882ea3
5 files changed
+144
-43
No files matched your search
@@ -2,7 +2,8 @@ local api = require "luci.passwall.api"
|
||||
|
||||
local var = api.get_args(arg)
|
||||
local FLAG = var["-FLAG"]
|
||||
local LOCAL_DNS = var["-LOCAL_DNS"]
|
||||
local DIRECT_DNS = var["-DIRECT_DNS"]
|
||||
local LOCAL_PORT = tonumber(var["-LOCAL_PORT"]) or 0
|
||||
local REMOTE_PROXY_SERVER = var["-REMOTE_PROXY_SERVER"]
|
||||
local USE_DEFAULT_DNS = var["-USE_DEFAULT_DNS"]
|
||||
local REMOTE_DNS = var["-REMOTE_DNS"]
|
||||
@@ -132,12 +133,14 @@ if data_dir ~= SMARTDNS_DATA_PATH then
|
||||
sys.call("mkdir -p " .. api.util.shellquote(data_dir))
|
||||
end
|
||||
|
||||
local LOCAL_SERVERS = {}
|
||||
for endpoint in string.gmatch(LOCAL_DNS or "", "[^,]+") do
|
||||
endpoint = api.trim(endpoint)
|
||||
local protocol, address = endpoint:match("^(%w+)://(.+)$")
|
||||
protocol = protocol and protocol:lower() or "udp"
|
||||
address = address or endpoint
|
||||
-- 统一生成直连、远程上游,协议和默认端口由 SmartDNS 根据 URL 识别。
|
||||
local function make_server_param(endpoint)
|
||||
local dns = api.split(api.trim(endpoint), ",")
|
||||
local address = api.trim(dns[1] or "")
|
||||
if address == "" then return nil end
|
||||
local protocol, host_address = address:match("^(%w+)://(.+)$")
|
||||
protocol = protocol and protocol:lower()
|
||||
address = host_address or address
|
||||
local host, port = address:match("^(.-)#(%d+)$")
|
||||
if host then
|
||||
if host:find(":", 1, true) and not host:find("[", 1, true) then host = "[" .. host .. "]" end
|
||||
@@ -145,18 +148,27 @@ for endpoint in string.gmatch(LOCAL_DNS or "", "[^,]+") do
|
||||
elseif datatypes.ip6addr(address) and not address:find("[", 1, true) then
|
||||
address = "[" .. address .. "]"
|
||||
end
|
||||
if (protocol == "udp" or protocol == "tcp") and address ~= "" then
|
||||
table.insert(LOCAL_SERVERS, "server " .. (protocol == "tcp" and "tcp://" or "") .. address)
|
||||
local server_param = "server " .. (protocol and (protocol .. "://") or "") .. address
|
||||
if dns[2] and dns[2] ~= "" then
|
||||
-- 保留 URL 中的域名,固定连接 IP 交给 SmartDNS 处理,避免手动替换影响 SNI 和 HTTP Host。
|
||||
server_param = server_param .. " -host-ip " .. api.trim(dns[2]):gsub("^%[(.*)%]$", "%1")
|
||||
end
|
||||
return server_param
|
||||
end
|
||||
|
||||
local DIRECT_SERVERS = {}
|
||||
for endpoint in string.gmatch(DIRECT_DNS or "", "[^|]+") do
|
||||
local server_param = make_server_param(endpoint)
|
||||
if server_param then
|
||||
-- 复用 PassWall 的直连标记,使所有协议及动态域名上游绕过本机透明代理。
|
||||
table.insert(DIRECT_SERVERS, server_param .. " -set-mark 255")
|
||||
end
|
||||
end
|
||||
if #LOCAL_SERVERS == 0 then
|
||||
if #DIRECT_SERVERS == 0 then
|
||||
log(" * 注意:SmartDNS 没有可用的直连 DNS 服务器,DNS 将无法正常工作!!!")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if REMOTE_DNS then
|
||||
REMOTE_DNS = REMOTE_DNS:gsub("#", ":")
|
||||
end
|
||||
local AUTO_GROUP = "auto"
|
||||
local auto_config_lines = {}
|
||||
-- 直连规则显式覆盖远程兜底的缓存、IPv6、测速和双栈优选设置。
|
||||
@@ -177,13 +189,15 @@ local local_domain = DNS_REDIRECT and api.uci_get("dhcp", "@dnsmasq[0]", "domain
|
||||
config_lines = {
|
||||
tonumber(LISTEN_PORT) ~= 0 and ("bind " .. listen_address) or "",
|
||||
tonumber(LISTEN_PORT) ~= 0 and ("bind-tcp " .. listen_address) or "",
|
||||
LOCAL_PORT > 0 and ("bind [::]:" .. LOCAL_PORT .. "@lo -group direct -no-rules") or "",
|
||||
LOCAL_PORT > 0 and ("bind-tcp [::]:" .. LOCAL_PORT .. "@lo -group direct -no-rules") or "",
|
||||
"log-level " .. LOG_LEVEL,
|
||||
"log-num " .. (LOG and "1" or "0"),
|
||||
LOG and string.format("log-file %q", FLAG_PATH .. "/smartdns.log") or "",
|
||||
LOG and ("log-size " .. LOG_SIZE .. "M") or "",
|
||||
string.format("cache-file %q", SMARTDNS_DATA_PATH .. "/smartdns.cache"),
|
||||
string.format("data-dir %q", data_dir),
|
||||
"server 223.5.5.5 -bootstrap-dns",
|
||||
"server 223.5.5.5 -bootstrap-dns -set-mark 255",
|
||||
-- Set the suffix before loading hosts/leases so initial records are expanded too.
|
||||
(local_domain and local_domain ~= "") and "local-domain " .. local_domain or "",
|
||||
DNS_REDIRECT and "hosts-file /tmp/hosts/*" or "",
|
||||
@@ -220,7 +234,7 @@ if DNS_REDIRECT then
|
||||
table.insert(config_lines, string.format("odhcpd-lease-file %q", odhcpd_lease))
|
||||
end
|
||||
end
|
||||
for _, server_param in ipairs(LOCAL_SERVERS) do
|
||||
for _, server_param in ipairs(DIRECT_SERVERS) do
|
||||
table.insert(config_lines, server_param .. " -group direct -exclude-default-group")
|
||||
if DEFAULT_DNS_GROUP == AUTO_GROUP then
|
||||
-- 自动组的直连副本仅接受大陆 IP,普通直连组保持默认行为。
|
||||
@@ -241,13 +255,7 @@ end
|
||||
|
||||
if DNS_MODE == "socks" then
|
||||
for w in string.gmatch(REMOTE_DNS, '[^|]+') do
|
||||
-- 协议和默认端口由 SmartDNS 根据原始 URL 识别。
|
||||
local dns = api.split(api.trim(w), ",")
|
||||
local server_param = "server " .. api.trim(dns[1])
|
||||
if dns[2] and dns[2] ~= "" then
|
||||
-- 保留 URL 中的域名,固定连接 IP 交给 SmartDNS 处理,避免手动替换影响 SNI 和 HTTP Host。
|
||||
server_param = server_param .. " -host-ip " .. api.trim(dns[2]):gsub("^%[(.*)%]$", "%1")
|
||||
end
|
||||
local server_param = make_server_param(w)
|
||||
|
||||
if not api.is_local_ip(w) then
|
||||
server_param = server_param .. " -proxy " .. proxy_server_name
|
||||
|
||||
Reference in new issue
Block a user