dns.go 2.58 KB
Newer Older
1 2 3
package commands

import (
4
	"fmt"
5 6
	"io"

7
	ncmd "github.com/ipfs/go-ipfs/core/commands/name"
8
	namesys "github.com/ipfs/go-ipfs/namesys"
Jakub Sztandera's avatar
Jakub Sztandera committed
9
	nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
Jan Winkelmann's avatar
Jan Winkelmann committed
10

Jakub Sztandera's avatar
Jakub Sztandera committed
11 12
	cmdkit "github.com/ipfs/go-ipfs-cmdkit"
	cmds "github.com/ipfs/go-ipfs-cmds"
13 14
)

Kejie Zhang's avatar
Kejie Zhang committed
15 16 17 18
const (
	dnsRecursiveOptionName = "recursive"
)

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

Overbool's avatar
Overbool committed
34 35 36 37
Note: This command can only recursively resolve DNS links,
it will fail to recursively resolve through IPNS keys etc.
For general-purpose recursive resolution, use ipfs name resolve -r.

38 39
For example, with this DNS TXT record:

40
	> dig +short TXT _dnslink.ipfs.io
Lars Gierth's avatar
Lars Gierth committed
41
	dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
42 43 44

The resolver will give:

Lars Gierth's avatar
Lars Gierth committed
45 46
	> ipfs dns ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
47

Lars Gierth's avatar
Lars Gierth committed
48
The resolver can recursively resolve:
49

Lars Gierth's avatar
Lars Gierth committed
50 51 52 53
	> dig +short TXT recursive.ipfs.io
	dnslink=/ipns/ipfs.io
	> ipfs dns -r recursive.ipfs.io
	/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
54 55 56
`,
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
57 58
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("domain-name", true, false, "The domain-name name to resolve.").EnableStdin(),
59
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
60
	Options: []cmdkit.Option{
Steven Allen's avatar
Steven Allen committed
61
		cmdkit.BoolOption(dnsRecursiveOptionName, "r", "Resolve until the result is not a DNS link.").WithDefault(true),
62
	},
63 64 65
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		recursive, _ := req.Options[dnsRecursiveOptionName].(bool)
		name := req.Arguments[0]
66
		resolver := namesys.NewDNSResolver()
67

Dirk McCormick's avatar
Dirk McCormick committed
68
		var ropts []nsopts.ResolveOpt
69
		if !recursive {
Dirk McCormick's avatar
Dirk McCormick committed
70
			ropts = append(ropts, nsopts.Depth(1))
71
		}
Dirk McCormick's avatar
Dirk McCormick committed
72

73
		output, err := resolver.Resolve(req.Context, name, ropts...)
74
		if err == namesys.ErrResolveFailed {
75
			return err
76
		}
77
		if err != nil {
78
			return err
79
		}
80
		return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: output})
81
	},
82 83 84 85 86
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ncmd.ResolvedPath) error {
			fmt.Fprintln(w, out.Path.String())
			return nil
		}),
87
	},
88
	Type: ncmd.ResolvedPath{},
89
}