Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type Config struct {
// Expected format is {protocol}://{host}:{port}, e.g. tcp://8.8.8.8:53
DNSResolver string `yaml:"dns-resolver,omitempty"`

// DNSResolverConfig is parsed DNSResolver
// We do this to avoid parsing DNSResolver every time it is needed
DNSResolverConfig *DNSResolverConfig `yaml:"-"`
Comment on lines +63 to +65
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this isn't a configuration, it shouldn't be exported and thus it should be dnsResolver.
Also, move it next to httpClient (if you have the time, please move both (httpClient and dnsResolver at the bottom of the struct)


// OAuth2Config is the OAuth2 configuration used for the client.
//
// If non-nil, the http.Client returned by getHTTPClient will automatically retrieve a token if necessary.
Expand Down Expand Up @@ -116,8 +120,10 @@ func (c *Config) ValidateAndSetDefaults() error {
}
if c.HasCustomDNSResolver() {
// Validate the DNS resolver now to make sure it will not return an error later.
if _, err := c.parseDNSResolver(); err != nil {
if resolver, err := c.parseDNSResolver(); err != nil {
return err
} else {
c.DNSResolverConfig = resolver
}
}
if c.HasOAuth2Config() && !c.OAuth2Config.isValid() {
Expand Down
24 changes: 21 additions & 3 deletions config/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package endpoint

import (
"bytes"
"context"
"crypto/x509"
"encoding/json"
"errors"
Expand Down Expand Up @@ -432,11 +433,28 @@ func (e *Endpoint) getParsedBody() string {
}

func (e *Endpoint) getIP(result *Result) {
if ips, err := net.LookupIP(result.Hostname); err != nil {
resolver := net.DefaultResolver
// Create a custom DNS resolver for use in looking up the IP address
// if the configuration specifies a custom DNS resolver address
if e.ClientConfig.HasCustomDNSResolver() {
dnsResolver := e.ClientConfig.DNSResolverConfig
resolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, dnsResolver.Protocol, dnsResolver.Host+":"+dnsResolver.Port)
},
}
}

addrs, err := resolver.LookupIP(context.Background(), e.ClientConfig.Network, result.Hostname)
if err != nil {
result.AddError(err.Error())
return
} else {
result.IP = ips[0].String()
}
for _, ia := range addrs {
result.IP = ia.String()
return
}
}

Expand Down