Skip to content

Commit

Permalink
add dns server option to DNSResolver
Browse files Browse the repository at this point in the history
This commit is for ipfs#6915.
This enables dns resolver to select server.
  • Loading branch information
matyapiro31 authored May 27, 2020
1 parent f58665c commit bcfbdb6
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions namesys/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,31 @@ type LookupTXTFunc func(name string) (txt []string, err error)

// DNSResolver implements a Resolver on DNS domains
type DNSResolver struct {
lookupTXT LookupTXTFunc
lookupTXT LookupTXTFunc
DNSAddress string
// TODO: maybe some sort of caching?
// cache would need a timeout
}

// NewDNSResolver constructs a name resolver using DNS TXT records.
func NewDNSResolver() *DNSResolver {
return &DNSResolver{lookupTXT: net.LookupTXT}
func NewDNSResolver(dnsaddress string) *DNSResolver {
var lookupTXTDNS func(ctx context.Context, name string) ([]string, error)
if dnsaddress == "" {
lookupTXTDNS = (&net.Resolver{}).LookupTXT
} else {
lookupTXTDNS = (&net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "udp", dnsaddress)
},
}).LookupTXT
}
return &DNSResolver{lookupTXT: func(name string) (txt []string, err error) {
return lookupTXTDNS(context.Background(), name)
},
DNSAddress: dnsaddress,
}
}

// Resolve implements Resolver.
Expand Down

0 comments on commit bcfbdb6

Please sign in to comment.