-
Notifications
You must be signed in to change notification settings - Fork 664
/
opendns_resolver.go
52 lines (44 loc) · 1.01 KB
/
opendns_resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package dynamicip
import (
"context"
"errors"
"net"
"time"
)
const (
ipResolutionTimeout = 10 * time.Second
openDNSUrl = "resolver1.opendns.com:53"
)
var (
errOpenDNSNoIP = errors.New("openDNS returned no ip")
_ Resolver = &openDNSResolver{}
)
// IFConfigResolves resolves our public IP using openDNS
type openDNSResolver struct {
resolver *net.Resolver
}
func newOpenDNSResolver() Resolver {
return &openDNSResolver{
resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{
Timeout: ipResolutionTimeout,
}
return d.DialContext(ctx, "udp", openDNSUrl)
},
},
}
}
func (r *openDNSResolver) Resolve() (net.IP, error) {
ips, err := r.resolver.LookupIP(context.TODO(), "ip", "myip.opendns.com")
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, errOpenDNSNoIP
}
return ips[0], nil
}