dns.go 2.55 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/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
14 15 16
)

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

33
	> dig +short TXT _dnslink.ipfs.io
Lars Gierth's avatar
Lars Gierth committed
34
	dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
35 36 37

The resolver will give:

Lars Gierth's avatar
Lars Gierth committed
38 39
	> ipfs dns ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
40

Lars Gierth's avatar
Lars Gierth committed
41
The resolver can recursively resolve:
42

Lars Gierth's avatar
Lars Gierth committed
43 44 45 46
	> dig +short TXT recursive.ipfs.io
	dnslink=/ipns/ipfs.io
	> ipfs dns -r recursive.ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
47 48 49
`,
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
50 51
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("domain-name", true, false, "The domain-name name to resolve.").EnableStdin(),
52
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
53
	Options: []cmdkit.Option{
54
		cmdkit.BoolOption("recursive", "r", "Resolve until the result is not a DNS link."),
55 56 57 58 59
	},
	Run: func(req cmds.Request, res cmds.Response) {

		recursive, _, _ := req.Option("recursive").Bool()
		name := req.Arguments()[0]
60
		resolver := namesys.NewDNSResolver()
61

Dirk McCormick's avatar
Dirk McCormick committed
62
		var ropts []nsopts.ResolveOpt
63
		if !recursive {
Dirk McCormick's avatar
Dirk McCormick committed
64
			ropts = append(ropts, nsopts.Depth(1))
65
		}
Dirk McCormick's avatar
Dirk McCormick committed
66 67

		output, err := resolver.Resolve(req.Context(), name, ropts...)
68
		if err == namesys.ErrResolveFailed {
Jan Winkelmann's avatar
Jan Winkelmann committed
69
			res.SetError(err, cmdkit.ErrNotFound)
70 71
			return
		}
72
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
73
			res.SetError(err, cmdkit.ErrNormal)
74 75
			return
		}
76
		res.SetOutput(&ncmd.ResolvedPath{Path: output})
77 78 79
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
80 81 82 83 84
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

85
			output, ok := v.(*ncmd.ResolvedPath)
86
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
87
				return nil, e.TypeErr(output, v)
88
			}
89
			return strings.NewReader(output.Path.String() + "\n"), nil
90 91
		},
	},
92
	Type: ncmd.ResolvedPath{},
93
}