-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
90 lines (76 loc) · 2.41 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
85
86
87
88
89
90
package commands
import (
"io"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
namesys "github.com/ipfs/go-ipfs/namesys"
"gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
)
var DNSCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Resolve DNS links.",
ShortDescription: `
Multihashes are hard to remember, but domain names are usually easy to
remember. To create memorable aliases for multihashes, DNS TXT
records can point to other DNS links, IPFS objects, IPNS keys, etc.
This command resolves those links to the referenced object.
`,
LongDescription: `
Multihashes are hard to remember, but domain names are usually easy to
remember. To create memorable aliases for multihashes, DNS TXT
records can point to other DNS links, IPFS objects, IPNS keys, etc.
This command resolves those links to the referenced object.
For example, with this DNS TXT record:
> dig +short TXT _dnslink.ipfs.io
dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
The resolver will give:
> ipfs dns ipfs.io
/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
The resolver can recursively resolve:
> dig +short TXT recursive.ipfs.io
dnslink=/ipns/ipfs.io
> ipfs dns -r recursive.ipfs.io
/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("domain-name", true, false, "The domain-name name to resolve.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("recursive", "r", "Resolve until the result is not a DNS link."),
},
Run: func(req cmds.Request, res cmds.Response) {
recursive, _, _ := req.Option("recursive").Bool()
name := req.Arguments()[0]
resolver := namesys.NewDNSResolver()
depth := 1
if recursive {
depth = namesys.DefaultDepthLimit
}
output, err := resolver.ResolveN(req.Context(), name, depth)
if err == namesys.ErrResolveFailed {
res.SetError(err, cmdkit.ErrNotFound)
return
}
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
res.SetOutput(&ResolvedPath{output})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}
output, ok := v.(*ResolvedPath)
if !ok {
return nil, e.TypeErr(output, v)
}
return strings.NewReader(output.Path.String() + "\n"), nil
},
},
Type: ResolvedPath{},
}