dns.go 2.81 KB
Newer Older
1 2 3 4 5 6 7
package commands

import (
	"io"
	"strings"

	cmds "github.com/ipfs/go-ipfs/commands"
Jan Winkelmann's avatar
Jan Winkelmann committed
8
	e "github.com/ipfs/go-ipfs/core/commands/e"
9
	ncmd "github.com/ipfs/go-ipfs/core/commands/name"
10
	namesys "github.com/ipfs/go-ipfs/namesys"
Dirk McCormick's avatar
Dirk McCormick committed
11
	nsopts "github.com/ipfs/go-ipfs/namesys/opts"
Jan Winkelmann's avatar
Jan Winkelmann committed
12

13
	"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
14 15
)

Kejie Zhang's avatar
Kejie Zhang committed
16 17 18 19
const (
	dnsRecursiveOptionName = "recursive"
)

20
var DNSCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
21
	Helptext: cmdkit.HelpText{
22
		Tagline: "Resolve DNS links.",
23 24 25 26 27 28 29 30 31 32 33 34
		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.

Overbool's avatar
Overbool committed
35 36 37 38
Note: This command can only recursively resolve DNS links,
it will fail to recursively resolve through IPNS keys etc.
For general-purpose recursive resolution, use ipfs name resolve -r.

39 40
For example, with this DNS TXT record:

41
	> dig +short TXT _dnslink.ipfs.io
Lars Gierth's avatar
Lars Gierth committed
42
	dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
43 44 45

The resolver will give:

Lars Gierth's avatar
Lars Gierth committed
46 47
	> ipfs dns ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
48

Lars Gierth's avatar
Lars Gierth committed
49
The resolver can recursively resolve:
50

Lars Gierth's avatar
Lars Gierth committed
51 52 53 54
	> dig +short TXT recursive.ipfs.io
	dnslink=/ipns/ipfs.io
	> ipfs dns -r recursive.ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
55 56 57
`,
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
58 59
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("domain-name", true, false, "The domain-name name to resolve.").EnableStdin(),
60
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
61
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
62
		cmdkit.BoolOption(dnsRecursiveOptionName, "r", "Resolve until the result is not a DNS link."),
63 64 65
	},
	Run: func(req cmds.Request, res cmds.Response) {

Kejie Zhang's avatar
Kejie Zhang committed
66
		recursive, _, _ := req.Option(dnsRecursiveOptionName).Bool()
67
		name := req.Arguments()[0]
68
		resolver := namesys.NewDNSResolver()
69

Dirk McCormick's avatar
Dirk McCormick committed
70
		var ropts []nsopts.ResolveOpt
71
		if !recursive {
Dirk McCormick's avatar
Dirk McCormick committed
72
			ropts = append(ropts, nsopts.Depth(1))
73
		}
Dirk McCormick's avatar
Dirk McCormick committed
74 75

		output, err := resolver.Resolve(req.Context(), name, ropts...)
76
		if err == namesys.ErrResolveFailed {
Jan Winkelmann's avatar
Jan Winkelmann committed
77
			res.SetError(err, cmdkit.ErrNotFound)
78 79
			return
		}
80
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
81
			res.SetError(err, cmdkit.ErrNormal)
82 83
			return
		}
84
		res.SetOutput(&ncmd.ResolvedPath{Path: output})
85 86 87
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
88 89 90 91 92
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

93
			output, ok := v.(*ncmd.ResolvedPath)
94
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
95
				return nil, e.TypeErr(output, v)
96
			}
97
			return strings.NewReader(output.Path.String() + "\n"), nil
98 99
		},
	},
100
	Type: ncmd.ResolvedPath{},
101
}