Add custom request handler for DNS

This commit is contained in:
Yarmo Mackenbach 2020-11-05 12:57:37 +01:00
parent 98d7e680ef
commit bea7679255

View file

@ -13,9 +13,35 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const dns = require('dns')
const bent = require('bent')
const req = bent('GET')
const utils = require('../utils')
const reURI = /^dns:([a-zA-Z0-9\.\-\_]*)(?:\?(.*))?/
const customRequestHandler = async (spData, opts) => {
if (('resolveTxt' in dns)) {
const prom = async () => {
return new Promise((resolve, reject) => {
dns.resolveTxt(spData.profile.display, (err, records) => {
if (err) reject(err)
resolve(records)
})
})
}
return {
hostname: spData.profile.display,
records: {
txt: await prom()
}
}
} else {
const res = await req(spData.proof.uri, null, { Accept: 'application/json' })
const json = await res.json()
return json
}
}
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
const match = uri.match(reURI)
@ -41,7 +67,8 @@ const processURI = (uri, opts) => {
path: ['records', 'txt'],
relation: 'contains'
},
qr: null
qr: null,
customRequestHandler: customRequestHandler
}
}