Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 40fc330

Browse files
committed
feat: enable recursive lookups on ipfs.dns
1 parent 1516328 commit 40fc330

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/cli/commands/dns.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ module.exports = {
77
describe: 'Resolve DNS links',
88

99
builder: {
10+
recursive: {
11+
type: 'boolean',
12+
default: false,
13+
alias: 'r',
14+
desc: 'Resolve until the result is not a DNS link'
15+
},
1016
format: {
1117
type: 'string'
1218
}
1319
},
1420

15-
handler ({ getIpfs, domain, resolve }) {
21+
handler ({ getIpfs, domain, recursive, format, resolve }) {
22+
const opts = { recursive, format }
1623
resolve((async () => {
1724
const ipfs = await getIpfs()
18-
const path = await ipfs.dns(domain)
25+
const path = await ipfs.dns(domain, opts)
1926
print(path)
2027
})())
2128
}

src/core/runtime/dns-nodejs.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22

33
const dns = require('dns')
44
const _ = require('lodash')
5+
const isIPFS = require('is-ipfs')
56
const errcode = require('err-code')
67

8+
const maxRecursiveDepth = 32
9+
710
module.exports = (domain, opts, callback) => {
8-
resolveDnslink(domain)
11+
const recursive = opts.recursive && opts.recursive.toString() === 'true'
12+
let depth
13+
if (recursive) {
14+
depth = maxRecursiveDepth
15+
}
16+
17+
return recursiveResolveDnslink(domain, depth, callback)
18+
}
19+
20+
function recursiveResolveDnslink (domain, depth, callback) {
21+
if (depth === 0) {
22+
const errMsg = `recursion limit of ${maxRecursiveDepth} exceeded`
23+
return callback(errcode(errMsg, 'ERR_DNSLINK_RECURSION_LIMIT'))
24+
}
25+
26+
return resolveDnslink(domain)
927
.catch(err => {
1028
// If the code is not ENOTFOUND or ERR_DNSLINK_NOT_FOUND or ENODATA then throw the error
1129
if (err.code !== 'ENOTFOUND' && err.code !== 'ERR_DNSLINK_NOT_FOUND' && err.code !== 'ENODATA') {
@@ -24,7 +42,14 @@ module.exports = (domain, opts, callback) => {
2442
return resolveDnslink(_dnslinkDomain)
2543
})
2644
.then(dnslinkRecord => {
27-
callback(null, dnslinkRecord.replace('dnslink=', ''))
45+
const result = dnslinkRecord.replace('dnslink=', '')
46+
const domainOrCID = result.split('/')[2]
47+
const isIPFSCID = isIPFS.cid(domainOrCID)
48+
49+
if (isIPFSCID || !depth) {
50+
return callback(null, result)
51+
}
52+
return recursiveResolveDnslink(domainOrCID, depth - 1, callback)
2853
})
2954
.catch(callback)
3055
}

src/http/api/resources/dns.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
const Boom = require('boom')
44

55
module.exports = async (request, h) => {
6-
if (!request.query.arg) {
6+
const { arg: domain, ...opts } = request.query
7+
8+
if (!domain) {
79
throw Boom.badRequest("Argument 'domain' is required")
810
}
911

10-
const path = await request.server.app.ipfs.dns(request.query.arg)
12+
const path = await request.server.app.ipfs.dns(domain, opts)
1113
return h.response({
1214
Path: path
1315
})

test/cli/dns.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
6+
const isIPFS = require('is-ipfs')
67

78
describe('dns', () => runOnAndOff((thing) => {
89
let ipfs
@@ -28,6 +29,15 @@ describe('dns', () => runOnAndOff((thing) => {
2829
})
2930
})
3031

32+
it('recursively resolve ipfs.io', function () {
33+
this.timeout(60 * 1000)
34+
35+
return ipfs('dns --recursive ipfs.io').then((res) => {
36+
const resultingDomainOrCid = res.split('/')[2].trim()
37+
expect(isIPFS.cid(resultingDomainOrCid)).to.eql(true)
38+
})
39+
})
40+
3141
it('resolve subdomain docs.ipfs.io dns', function () {
3242
this.timeout(60 * 1000)
3343

0 commit comments

Comments
 (0)