dns.go 2.41 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"
Jan Winkelmann's avatar
Jan Winkelmann committed
10

keks's avatar
keks committed
11
	"gx/ipfs/QmNaA1HxkbVtweGfabDMy2DMLvqQ1eg3LNEqDMVA3zCoz1/go-ipfs-cmdkit"
12 13 14
)

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

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

The resolver will give:

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

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

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

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

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

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

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