This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support _dnslink subdomain specified dnslinks (#1843)
- Loading branch information
1 parent
2df45d7
commit a17253e
Showing
2 changed files
with
49 additions
and
11 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 |
---|---|---|
@@ -1,21 +1,51 @@ | ||
'use strict' | ||
|
||
const dns = require('dns') | ||
const _ = require('lodash') | ||
const errcode = require('err-code') | ||
|
||
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 or ERR_DNSLINK_NOT_FOUND then throw the error | ||
if (err.code !== 'ENOTFOUND' && err.code !== 'ERR_DNSLINK_NOT_FOUND') throw err | ||
|
||
// 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.startsWith('_dnslink.')) { | ||
// The supplied domain contains a _dnslink component | ||
// Check the non-_dnslink domain | ||
const rootDomain = domain.replace('_dnslink.', '') | ||
return resolveDnslink(rootDomain) | ||
} | ||
} | ||
// Check the _dnslink subdomain | ||
const _dnslinkDomain = `_dnslink.${domain}` | ||
// If this throws then we propagate the error | ||
return resolveDnslink(_dnslinkDomain) | ||
}) | ||
.then(dnslinkRecord => { | ||
callback(null, dnslinkRecord.replace('dnslink=', '')) | ||
}) | ||
.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 => { | ||
return _.chain(records).flatten().filter(record => { | ||
return DNSLINK_REGEX.test(record) | ||
}).value() | ||
}) | ||
.then(dnslinkRecords => { | ||
// we now have dns text entries as an array of strings | ||
// only records passing the DNSLINK_REGEX text are included | ||
if (dnslinkRecords.length === 0) { | ||
throw errcode(`No dnslink records found for domain: ${domain}`, 'ERR_DNSLINK_NOT_FOUND') | ||
} | ||
return dnslinkRecords[0] | ||
}) | ||
} |
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