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

feat: support _dnslink subdomain specified dnslinks #1843

Merged
merged 5 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 50 additions & 11 deletions src/core/runtime/dns-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,58 @@
const dns = require('dns')

module.exports = (domain, opts, callback) => {
dns.resolveTxt(domain, (err, records) => {
if (err) {
return callback(err, null)
}
resolveDnslink(domain)
.catch(err => {
// If the code is not ENOTFOUND then throw the error
if (err.code !== 'ENOTFOUND') throw err
chancehudson marked this conversation as resolved.
Show resolved Hide resolved

// TODO: implement recursive option

for (const record of records) {
if (record[0].startsWith('dnslink=')) {
return callback(null, record[0].substr(8, record[0].length - 1))
if (domain.indexOf('_dnslink') === -1) {
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
// Check the _dnslink subdomain
const _dnslinkDomain = ['_dnslink', ...domain.split('.')].join('.')
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
// If this throws then we propagate the error
return resolveDnslink(_dnslinkDomain)
} else if (domain.split('.').indexOf('_dnslink') === 0) {
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
// The supplied domain contains a _dnslink component
// Check the non-_dnslink domain
const rootDomain = domain.split('.').slice(1).join('.')
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
return resolveDnslink(rootDomain)
}
}
throw err
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
})
.then(dnslinkRecord => {
callback(null, dnslinkRecord.substr(8, dnslinkRecord.length - 1))
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
})
.catch(callback)
}

callback(new Error('domain does not have a txt dnslink entry'))
function resolveDnslink(domain) {
const DNSLINK_REGEX = /^dnslink=.+$/
return new Promise((resolve, reject) => {
dns.resolveTxt(domain, (err, records) => {
if (err) return reject(err)
resolve(records)
})
})
.then(records => {
// records is an array of arrays of strings
// the below expression flattens it into an array of strings
const flatRecords = [].concat(...records)
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
return flatRecords.filter(record => {
return DNSLINK_REGEX.test(record)
})
})
.then(dnslinkRecords => {
// we now have dns text entries as an array of strings
// only records passing the DNSLINK_REGEX text are included
if (dnslinkRecords > 1) {
const err = new Error(`Multiple dnslink records found for domain: ${domain}`)
err.code = 'EMULTFOUND'
throw err
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
} else if (dnslinkRecords.length === 0) {
const err = new Error(`No dnslink records found for domain: ${domain}`)
err.code = 'ENOTFOUND'
throw err
}
return dnslinkRecords[0]
})
}
8 changes: 8 additions & 0 deletions test/cli/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ describe('dns', () => runOnAndOff((thing) => {
expect(res.substr(0, 6)).to.eql('/ipns/')
})
})

it('resolve _dnslink.ipfs.io dns', function () {
this.timeout(60 * 1000)

return ipfs('dns _dnslink.ipfs.io').then((res) => {
expect(res.substr(0, 6)).to.eql('/ipns/')
})
})
}))