Skip to content

Commit f4fd8b8

Browse files
authored
DNS: Implement queryStrategy for "localhost" (#4303)
Fixes #4302
1 parent 14a6636 commit f4fd8b8

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

app/dns/nameserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dis
4343
}
4444
switch {
4545
case strings.EqualFold(u.String(), "localhost"):
46-
return NewLocalNameServer(), nil
46+
return NewLocalNameServer(queryStrategy), nil
4747
case strings.EqualFold(u.Scheme, "https"): // DOH Remote mode
4848
return NewDoHNameServer(u, dispatcher, queryStrategy)
4949
case strings.EqualFold(u.Scheme, "https+local"): // DOH Local mode

app/dns/nameserver_local.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ import (
1414

1515
// LocalNameServer is an wrapper over local DNS feature.
1616
type LocalNameServer struct {
17-
client *localdns.Client
17+
client *localdns.Client
18+
queryStrategy QueryStrategy
1819
}
1920

2021
const errEmptyResponse = "No address associated with hostname"
2122

2223
// QueryIP implements Server.
2324
func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) (ips []net.IP, err error) {
25+
option = ResolveIpOptionOverride(s.queryStrategy, option)
26+
if !option.IPv4Enable && !option.IPv6Enable {
27+
return nil, dns.ErrEmptyResponse
28+
}
29+
2430
start := time.Now()
2531
ips, err = s.client.LookupIP(domain, option)
2632

@@ -42,14 +48,15 @@ func (s *LocalNameServer) Name() string {
4248
}
4349

4450
// NewLocalNameServer creates localdns server object for directly lookup in system DNS.
45-
func NewLocalNameServer() *LocalNameServer {
51+
func NewLocalNameServer(queryStrategy QueryStrategy) *LocalNameServer {
4652
errors.LogInfo(context.Background(), "DNS: created localhost client")
4753
return &LocalNameServer{
48-
client: localdns.New(),
54+
queryStrategy: queryStrategy,
55+
client: localdns.New(),
4956
}
5057
}
5158

5259
// NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
5360
func NewLocalDNSClient() *Client {
54-
return &Client{server: NewLocalNameServer()}
61+
return &Client{server: NewLocalNameServer(QueryStrategy_USE_IP)}
5562
}

app/dns/nameserver_local_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestLocalNameServer(t *testing.T) {
15-
s := NewLocalNameServer()
15+
s := NewLocalNameServer(QueryStrategy_USE_IP)
1616
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
1717
ips, err := s.QueryIP(ctx, "google.com", net.IP{}, dns.IPOption{
1818
IPv4Enable: true,

0 commit comments

Comments
 (0)