dns.go 2.22 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/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
10 11 12 13
)

var DNSCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
14
		Tagline: "DNS link resolver.",
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
  ipfs.io TXT "dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy ..."
30 31 32 33 34 35 36 37

The resolver will give:

  > ipfs dns ipfs.io
  /ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy

And with this DNS TXT record:

38
  ipfs.ipfs.io TXT "dnslink=/dns/ipfs.io ..."
39 40 41 42 43 44 45 46 47 48 49 50 51 52

The resolver will give:

  > ipfs dns ipfs.io
  /dns/ipfs.io
  > ipfs dns --recursive
  /ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
`,
	},

	Arguments: []cmds.Argument{
		cmds.StringArg("domain-name", true, false, "The domain-name name to resolve.").EnableStdin(),
	},
	Options: []cmds.Option{
Richard Littauer's avatar
Richard Littauer committed
53
		cmds.BoolOption("recursive", "r", "Resolve until the result is not a DNS link. Default: false."),
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 61 62 63 64

		depth := 1
		if recursive {
			depth = namesys.DefaultDepthLimit
		}
Jeromy's avatar
Jeromy committed
65
		output, err := resolver.ResolveN(req.Context(), name, depth)
66 67 68 69 70 71 72 73 74 75 76 77
		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()
			}
78
			return strings.NewReader(output.Path.String() + "\n"), nil
79 80 81 82
		},
	},
	Type: ResolvedPath{},
}