Skip to content

Commit 5087627

Browse files
committed
fix(endpoint): use custom resolver if specified for [IP] tests
1 parent fe214e9 commit 5087627

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

client/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type Config struct {
6060
// Expected format is {protocol}://{host}:{port}, e.g. tcp://8.8.8.8:53
6161
DNSResolver string `yaml:"dns-resolver,omitempty"`
6262

63+
DNSResolverConfig *DNSResolverConfig `yaml:"-"`
64+
6365
// OAuth2Config is the OAuth2 configuration used for the client.
6466
//
6567
// If non-nil, the http.Client returned by getHTTPClient will automatically retrieve a token if necessary.
@@ -116,8 +118,10 @@ func (c *Config) ValidateAndSetDefaults() error {
116118
}
117119
if c.HasCustomDNSResolver() {
118120
// Validate the DNS resolver now to make sure it will not return an error later.
119-
if _, err := c.parseDNSResolver(); err != nil {
121+
if resolver, err := c.parseDNSResolver(); err != nil {
120122
return err
123+
} else {
124+
c.DNSResolverConfig = resolver
121125
}
122126
}
123127
if c.HasOAuth2Config() && !c.OAuth2Config.isValid() {

config/endpoint/endpoint.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package endpoint
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/x509"
67
"encoding/json"
78
"errors"
@@ -327,11 +328,28 @@ func (e *Endpoint) EvaluateHealth() *Result {
327328
}
328329

329330
func (e *Endpoint) getIP(result *Result) {
330-
if ips, err := net.LookupIP(result.Hostname); err != nil {
331+
332+
resolver := net.DefaultResolver
333+
334+
if e.ClientConfig.HasCustomDNSResolver() {
335+
dnsResolver := e.ClientConfig.DNSResolverConfig
336+
resolver = &net.Resolver{
337+
PreferGo: true,
338+
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
339+
d := net.Dialer{}
340+
return d.DialContext(ctx, dnsResolver.Protocol, dnsResolver.Host+":"+dnsResolver.Port)
341+
},
342+
}
343+
}
344+
345+
addrs, err := resolver.LookupIP(context.Background(), e.ClientConfig.Network, result.Hostname)
346+
if err != nil {
331347
result.AddError(err.Error())
332348
return
333-
} else {
334-
result.IP = ips[0].String()
349+
}
350+
for _, ia := range addrs {
351+
result.IP = ia.String()
352+
return
335353
}
336354
}
337355

0 commit comments

Comments
 (0)