cmd/cli: new flow for LAN hostname query

If there is no explicit rules for LAN hostname queries, using OS
resolver instead of forwarding requests to remote upstreams.
This commit is contained in:
Cuong Manh Le
2024-12-19 21:50:00 +07:00
committed by Cuong Manh Le
parent 17941882a9
commit 09426dcd36
2 changed files with 9 additions and 4 deletions
+6 -4
View File
@@ -456,7 +456,7 @@ func (p *prog) proxy(ctx context.Context, req *proxyRequest) *proxyResponse {
res.clientInfo = true res.clientInfo = true
return res return res
} }
upstreams, upstreamConfigs = p.upstreamsAndUpstreamConfigForLanAndPtr(upstreams, upstreamConfigs) upstreams, upstreamConfigs = p.upstreamsAndUpstreamConfigForPtr(upstreams, upstreamConfigs)
ctrld.Log(ctx, mainLog.Load().Debug(), "private PTR lookup, using upstreams: %v", upstreams) ctrld.Log(ctx, mainLog.Load().Debug(), "private PTR lookup, using upstreams: %v", upstreams)
case isLanHostnameQuery(req.msg): case isLanHostnameQuery(req.msg):
isLanOrPtrQuery = true isLanOrPtrQuery = true
@@ -465,7 +465,8 @@ func (p *prog) proxy(ctx context.Context, req *proxyRequest) *proxyResponse {
res.clientInfo = true res.clientInfo = true
return res return res
} }
upstreams, upstreamConfigs = p.upstreamsAndUpstreamConfigForLanAndPtr(upstreams, upstreamConfigs) upstreams = []string{upstreamOS}
upstreamConfigs = []*ctrld.UpstreamConfig{osUpstreamConfig}
ctrld.Log(ctx, mainLog.Load().Debug(), "lan hostname lookup, using upstreams: %v", upstreams) ctrld.Log(ctx, mainLog.Load().Debug(), "lan hostname lookup, using upstreams: %v", upstreams)
default: default:
ctrld.Log(ctx, mainLog.Load().Debug(), "no explicit policy matched, using default routing -> %v", upstreams) ctrld.Log(ctx, mainLog.Load().Debug(), "no explicit policy matched, using default routing -> %v", upstreams)
@@ -605,7 +606,7 @@ func (p *prog) proxy(ctx context.Context, req *proxyRequest) *proxyResponse {
return res return res
} }
func (p *prog) upstreamsAndUpstreamConfigForLanAndPtr(upstreams []string, upstreamConfigs []*ctrld.UpstreamConfig) ([]string, []*ctrld.UpstreamConfig) { func (p *prog) upstreamsAndUpstreamConfigForPtr(upstreams []string, upstreamConfigs []*ctrld.UpstreamConfig) ([]string, []*ctrld.UpstreamConfig) {
if len(p.localUpstreams) > 0 { if len(p.localUpstreams) > 0 {
tmp := make([]string, 0, len(p.localUpstreams)+len(upstreams)) tmp := make([]string, 0, len(p.localUpstreams)+len(upstreams))
tmp = append(tmp, p.localUpstreams...) tmp = append(tmp, p.localUpstreams...)
@@ -1060,7 +1061,8 @@ func isLanHostnameQuery(m *dns.Msg) bool {
name := strings.TrimSuffix(q.Name, ".") name := strings.TrimSuffix(q.Name, ".")
return !strings.Contains(name, ".") || return !strings.Contains(name, ".") ||
strings.HasSuffix(name, ".domain") || strings.HasSuffix(name, ".domain") ||
strings.HasSuffix(name, ".lan") strings.HasSuffix(name, ".lan") ||
strings.HasSuffix(name, ".local")
} }
// isSrvLookup reports whether DNS message is a SRV query. // isSrvLookup reports whether DNS message is a SRV query.
+3
View File
@@ -365,6 +365,9 @@ func Test_isLanHostnameQuery(t *testing.T) {
{"A not LAN", newDnsMsgWithHostname("example.com", dns.TypeA), false}, {"A not LAN", newDnsMsgWithHostname("example.com", dns.TypeA), false},
{"AAAA not LAN", newDnsMsgWithHostname("example.com", dns.TypeAAAA), false}, {"AAAA not LAN", newDnsMsgWithHostname("example.com", dns.TypeAAAA), false},
{"Not A or AAAA", newDnsMsgWithHostname("foo", dns.TypeTXT), false}, {"Not A or AAAA", newDnsMsgWithHostname("foo", dns.TypeTXT), false},
{".domain", newDnsMsgWithHostname("foo.domain", dns.TypeA), true},
{".lan", newDnsMsgWithHostname("foo.lan", dns.TypeA), true},
{".local", newDnsMsgWithHostname("foo.local", dns.TypeA), true},
} }
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc