-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
84 lines (67 loc) · 2.1 KB
/
dns.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package namesys
import (
"errors"
"net"
"strings"
isd "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-is-domain"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
path "github.com/ipfs/go-ipfs/path"
)
type LookupTXTFunc func(name string) (txt []string, err error)
// DNSResolver implements a Resolver on DNS domains
type DNSResolver struct {
lookupTXT LookupTXTFunc
// TODO: maybe some sort of caching?
// cache would need a timeout
}
// NewDNSResolver constructs a name resolver using DNS TXT records.
func NewDNSResolver() Resolver {
return &DNSResolver{lookupTXT: net.LookupTXT}
}
// newDNSResolver constructs a name resolver using DNS TXT records,
// returning a resolver instead of NewDNSResolver's Resolver.
func newDNSResolver() resolver {
return &DNSResolver{lookupTXT: net.LookupTXT}
}
// Resolve implements Resolver.
func (r *DNSResolver) Resolve(ctx context.Context, name string) (path.Path, error) {
return r.ResolveN(ctx, name, DefaultDepthLimit)
}
// ResolveN implements Resolver.
func (r *DNSResolver) ResolveN(ctx context.Context, name string, depth int) (path.Path, error) {
return resolve(ctx, r, name, depth, "/ipns/")
}
// resolveOnce implements resolver.
// TXT records for a given domain name should contain a b58
// encoded multihash.
func (r *DNSResolver) resolveOnce(ctx context.Context, name string) (path.Path, error) {
if !isd.IsDomain(name) {
return "", errors.New("not a valid domain name")
}
log.Infof("DNSResolver resolving %s", name)
txt, err := r.lookupTXT(name)
if err != nil {
return "", err
}
for _, t := range txt {
p, err := parseEntry(t)
if err == nil {
return p, nil
}
}
return "", ErrResolveFailed
}
func parseEntry(txt string) (path.Path, error) {
p, err := path.ParseKeyToPath(txt) // bare IPFS multihashes
if err == nil {
return p, nil
}
return tryParseDnsLink(txt)
}
func tryParseDnsLink(txt string) (path.Path, error) {
parts := strings.SplitN(txt, "=", 2)
if len(parts) == 2 && parts[0] == "dnslink" {
return path.ParsePath(parts[1])
}
return "", errors.New("not a valid dnslink entry")
}