提交 e643f72c 作者: W. Trevor King

core/commands/dns: Add 'ipfs dns ...' for resolving DNS references

This lets users resolve (recursively or not) DNS links without pulling
in the other protocols.  That makes an easier, more isolated target
for alternative implemenations, since they don't need to understand
IPNS, proquint, etc. to handle these resolutions.
上级 c2ff0285
package commands
import (
"io"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
namesys "github.com/ipfs/go-ipfs/namesys"
util "github.com/ipfs/go-ipfs/util"
)
var DNSCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "DNS link resolver",
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:
ipfs.io. TXT "dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy ..."
The resolver will give:
> ipfs dns ipfs.io
/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
And with this DNS TXT record:
ipfs.ipfs.io. TXT "dnslink=/dns/ipfs.io ..."
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{
cmds.BoolOption("recursive", "r", "Resolve until the result is not a DNS link"),
},
Run: func(req cmds.Request, res cmds.Response) {
recursive, _, _ := req.Option("recursive").Bool()
name := req.Arguments()[0]
var resolver namesys.DNSResolver
depth := 1
if recursive {
depth = namesys.DefaultDepthLimit
}
output, err := resolver.ResolveN(req.Context().Context, name, depth)
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()
}
return strings.NewReader(output.Path.String()), nil
},
},
Type: ResolvedPath{},
}
...@@ -41,6 +41,7 @@ ADVANCED COMMANDS ...@@ -41,6 +41,7 @@ ADVANCED COMMANDS
daemon Start a long-running daemon process daemon Start a long-running daemon process
mount Mount an ipfs read-only mountpoint mount Mount an ipfs read-only mountpoint
name Publish or resolve IPNS names name Publish or resolve IPNS names
dns Resolve DNS links
pin Pin objects to local storage pin Pin objects to local storage
repo gc Garbage collect unpinned objects repo gc Garbage collect unpinned objects
...@@ -84,6 +85,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -84,6 +85,7 @@ var rootSubcommands = map[string]*cmds.Command{
"config": ConfigCmd, "config": ConfigCmd,
"dht": DhtCmd, "dht": DhtCmd,
"diag": DiagCmd, "diag": DiagCmd,
"dns": DNSCmd,
"get": GetCmd, "get": GetCmd,
"id": IDCmd, "id": IDCmd,
"log": LogCmd, "log": LogCmd,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论