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

import (
	"io"
	"strings"

	cmds "github.com/ipfs/go-ipfs/commands"
	namesys "github.com/ipfs/go-ipfs/namesys"
9
	util "gx/ipfs/QmZuY8aV7zbNXVy6DyN9SmnuH3o9nG852F4aTiSBpts8d1/go-ipfs-util"
10 11 12 13
)

var DNSCmd = &cmds.Command{
	Helptext: cmds.HelpText{
14
		Tagline: "Resolve DNS links.",
15 16 17 18 19 20 21 22 23 24 25 26 27 28
		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:

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

The resolver will give:

Lars Gierth's avatar
Lars Gierth committed
34 35
	> ipfs dns ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
36

Lars Gierth's avatar
Lars Gierth committed
37
The resolver can recursively resolve:
38

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

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

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

		depth := 1
		if recursive {
			depth = namesys.DefaultDepthLimit
		}
Jeromy's avatar
Jeromy committed
62
		output, err := resolver.ResolveN(req.Context(), name, depth)
63 64 65 66 67 68 69 70 71 72 73 74
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		res.SetOutput(&ResolvedPath{output})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			output, ok := res.Output().(*ResolvedPath)
			if !ok {
				return nil, util.ErrCast()
			}
75
			return strings.NewReader(output.Path.String() + "\n"), nil
76 77 78 79
		},
	},
	Type: ResolvedPath{},
}