-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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=/dnslink/ipfs.io ..." | ||
The resolver will give: | ||
> ipfs dns ipfs.io | ||
/dnslink/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 = 0 | ||
} | ||
output, err := resolver.Resolve(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{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters