dns.go 2.48 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
	namesys "github.com/ipfs/go-ipfs/namesys"
Dirk McCormick's avatar
Dirk McCormick committed
10
	nsopts "github.com/ipfs/go-ipfs/namesys/opts"
Jan Winkelmann's avatar
Jan Winkelmann committed
11

12
	"gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
13 14 15
)

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

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

The resolver will give:

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

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

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

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

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

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

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

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