feat(server): 私有服务域名分流(PANGOLIN_PRIVATE_SPLIT_DOMAINS)
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Failing after 11s
ci-pangolin / Cleartext Scan — Android 禁明文 (push) Failing after 9s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Failing after 9s
ci-pangolin / Lint — shellcheck (push) Has been cancelled
ci-pangolin / OpenAPI Sync Check (push) Has been cancelled
ci-pangolin / Flutter — analyze + test (push) Has been cancelled
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Has been cancelled
ci-pangolin / DS-flow — 原型/跨端同源/代码色单源闸 (push) Has been cancelled
ci-pangolin / Go — build + test (push) Has been cancelled
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Has been cancelled
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Has been cancelled
ci-pangolin / Golden — 视觉回归 (全量:components/auth/desktop/tablet) (push) Has been cancelled

家庭内网穿透域名(nas/git/win.yanmeiai.com)的客户端配置渲染:
- DNS 面: 系统解析器(type=local,底层网络)+私有域名规则置顶+reverse_mapping
- 路由面: 私有域名→强制走隧道,钉在 LAN 直连之后、国内分流(geoip-cn)之前
在家: 局域网 DNS 覆盖→私网IP→LAN直连零绕行; 在外: 锚点(frps@ali,国内IP)
不再被 smartRoute 分流成直连、避开安全组限源。env 不配置=行为零变化。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-16 07:22:16 +08:00
parent 43c76fb978
commit 74d8c857be
5 changed files with 143 additions and 16 deletions
+45 -10
View File
@@ -15,6 +15,13 @@ type ClientConfigOpts struct {
// RulesBaseURL 是控制面对外公网基址(如 "http://node:8080"),rule_set 的 .srs
// 从 <base>/v1/rules/*.srs 下载。SplitCN 生效需此项非空(否则分流静默跳过)。
RulesBaseURL string
// PrivateSplitDomains 私有服务域名(家庭内网穿透,如 nas/git/win.yanmeiai.com,
// 服务端 env PANGOLIN_PRIVATE_SPLIT_DOMAINS 配置;空=行为完全不变):
// - DNS 改用系统解析器(在家吃到局域网 DNS 覆盖→私网 IP;在外解析出公网锚点)
// - 路由上私网结果命中 LAN 直连(在家零绕行),公网结果强制走隧道——该规则
// 必须排在国内分流(geoip-cn)之前:锚点(frps@ali)是国内 IP,否则 smartRoute
// 会把它分流成直连,被 frps 侧安全组限源(仅节点出口)拦截。
PrivateSplitDomains []string
}
// BuildClientConfig renders a complete sing-box CLIENT configuration JSON that
@@ -126,7 +133,8 @@ func BuildClientConfig(node *nodes.NodeRow, dpUUID, deriveKey string, opts Clien
"tolerance": 50,
}
// Route: DNS 劫持 → LAN direct →(可选)国内直连 → 其余 via auto。
// Route: DNS 劫持 → LAN direct →(可选)私有域名走隧道 →(可选)国内直连 → 其余 via auto。
privateSplit := len(opts.PrivateSplitDomains) > 0
routeRules := []any{
// DNS 劫持(sing-box 1.13 action=hijack-dns,按目的端口 53 匹配,不依赖 sniff):
// 把发往隧道 DNS(172.19.0.2:53)的查询交给 sing-box DNS 模块解析。必须排在 LAN
@@ -138,6 +146,15 @@ func BuildClientConfig(node *nodes.NodeRow, dpUUID, deriveKey string, opts Clien
"outbound": "direct",
},
}
if privateSplit {
// 私有域名强制走隧道。在家不受此规则影响:系统 DNS(局域网覆盖)解析出私网 IP,
// 上面的 LAN 直连规则先命中。域名元数据靠 dns.reverse_mapping 补回(应用自行
// 解析后按 IP 连接,无回映射则此规则永不匹配、在外会掉进国内分流被限源拦截)。
routeRules = append(routeRules, map[string]any{
"domain": opts.PrivateSplitDomains,
"outbound": "auto",
})
}
route := map[string]any{
"final": "auto",
"auto_detect_interface": true,
@@ -166,24 +183,42 @@ func BuildClientConfig(node *nodes.NodeRow, dpUUID, deriveKey string, opts Clien
// DNS: remote over tunnel, local for domestic.
// sing-box 1.12+ DNS server format(type+server);旧的 address 串格式在
// 1.13 已 FATAL 拒绝(legacy DNS servers deprecated)。
dnsServers := []any{
map[string]any{"tag": "remote", "type": "tls", "server": "8.8.8.8", "detour": "auto"},
// local 不带 detour:sing-box 1.12 拒绝 DNS detour 到空 direct 出站
// (FATAL: detour to an empty direct outbound makes no sense)。
map[string]any{"tag": "local", "type": "udp", "server": "223.5.5.5"},
}
if privateSplit {
// 系统解析器(type=local,经底层物理网络):在家=路由器 DHCP 下发的局域网 DNS
// (含私有域名覆盖→私网 IP),在外=所在网络 DNS(公网记录→锚点 IP)。
// 不能用 223.5.5.5/8.8.8.8——公共 DNS 不知道家里的覆盖记录。
dnsServers = append(dnsServers, map[string]any{"tag": "dns-system", "type": "local"})
}
dns := map[string]any{
"servers": []any{
map[string]any{"tag": "remote", "type": "tls", "server": "8.8.8.8", "detour": "auto"},
// local 不带 detour:sing-box 1.12 拒绝 DNS detour 到空 direct 出站
// (FATAL: detour to an empty direct outbound makes no sense)。
map[string]any{"tag": "local", "type": "udp", "server": "223.5.5.5"},
},
"servers": dnsServers,
"final": "remote",
"strategy": "ipv4_only",
}
dnsRules := []any{}
if privateSplit {
// 私有域名规则须排在 geosite-cn 之前(优先级最高)。
dnsRules = append(dnsRules, map[string]any{
"domain": opts.PrivateSplitDomains, "server": "dns-system",
})
// 回映射:记住"哪个 IP 是哪个域名解析出来的",给后续按 IP 发起的连接补回
// 域名元数据——路由层的 domain 规则(私有域名→隧道)靠它才会命中。
dns["reverse_mapping"] = true
}
// 国内分流的 DNS 面(补 #5 数据面之外的 DNS 面):开分流时,命中 geosite-cn 的
// 国内域名用 local(223.5.5.5)直连解析,不走 remote(8.8.8.8 经隧道)。否则即便数据
// 直连,域名解析仍绕道出海(实测国内 DNS 段 200-600ms),首连凭空多一个出海 RTT。
// 复用 route.rule_set 里已定义的 geosite-cn 标签。
if splitActive {
dns["rules"] = []any{
map[string]any{"rule_set": []string{"geosite-cn"}, "server": "local"},
}
dnsRules = append(dnsRules, map[string]any{"rule_set": []string{"geosite-cn"}, "server": "local"})
}
if len(dnsRules) > 0 {
dns["rules"] = dnsRules
}
cfg := map[string]any{