2019-02-06 21:20:20 +01:00
/ * *
2019-02-08 19:02:13 +01:00
* @ author h345983745
2019-02-06 21:20:20 +01:00
* @ copyright Crown Copyright 2019
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
2019-02-06 21:20:20 +01:00
/ * *
2019-02-08 19:02:13 +01:00
* DNS over HTTPS operation
2019-02-06 21:20:20 +01:00
* /
2019-02-08 19:02:13 +01:00
class DNSOverHTTPS extends Operation {
2019-02-06 21:20:20 +01:00
/ * *
2019-02-08 19:02:13 +01:00
* DNSOverHTTPS constructor
2019-02-06 21:20:20 +01:00
* /
constructor ( ) {
super ( ) ;
2019-02-06 23:34:43 +01:00
this . name = "DNS over HTTPS" ;
2019-02-07 22:05:07 +01:00
this . module = "Default" ;
2019-02-08 19:02:13 +01:00
this . description = [
"Takes a single domain name and performs a DNS lookup using DNS over HTTPS." ,
"<br><br>" ,
"By default, <a href='https://developers.cloudflare.com/1.1.1.1/dns-over-https/'>Cloudflare</a> and <a href='https://developers.google.com/speed/public-dns/docs/dns-over-https'>Google</a> DNS over HTTPS services are supported." ,
"<br><br>" ,
"Can be used with any service that supports the GET parameters <code>name</code> and <code>type</code>."
] . join ( "\n" ) ;
this . infoURL = "https://wikipedia.org/wiki/DNS_over_HTTPS" ;
2019-02-06 21:20:20 +01:00
this . inputType = "string" ;
this . outputType = "JSON" ;
2019-02-06 21:54:06 +01:00
this . manualBake = true ;
2019-02-06 21:20:20 +01:00
this . args = [
{
name : "Resolver" ,
type : "editableOption" ,
value : [
{
name : "Google" ,
value : "https://dns.google.com/resolve"
} ,
{
name : "Cloudflare" ,
value : "https://cloudflare-dns.com/dns-query"
}
]
} ,
{
name : "Request Type" ,
type : "option" ,
value : [
"A" ,
"AAAA" ,
"TXT" ,
2019-02-07 00:27:27 +01:00
"MX" ,
"DNSKEY" ,
2022-09-21 17:37:06 +02:00
"NS" ,
"PTR"
2019-02-06 21:20:20 +01:00
]
} ,
{
2019-02-06 23:50:46 +01:00
name : "Answer Data Only" ,
2019-02-06 21:20:20 +01:00
type : "boolean" ,
value : false
} ,
{
2019-11-25 20:08:30 +01:00
name : "Disable DNSSEC validation" ,
2019-02-06 21:20:20 +01:00
type : "boolean" ,
2019-11-25 20:08:30 +01:00
value : false
2019-02-06 21:20:20 +01:00
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { JSON }
* /
run ( input , args ) {
const [ resolver , requestType , justAnswer , DNSSEC ] = args ;
2019-02-07 09:28:23 +01:00
let url = URL ;
try {
url = new URL ( resolver ) ;
2019-02-06 21:54:06 +01:00
} catch ( error ) {
2019-02-07 09:28:23 +01:00
throw new OperationError ( error . toString ( ) +
2019-02-06 21:54:06 +01:00
"\n\nThis error could be caused by one of the following:\n" +
2019-02-07 09:28:23 +01:00
" - An invalid Resolver URL\n" ) ;
2019-02-06 21:54:06 +01:00
}
2019-02-07 09:28:23 +01:00
const params = { name : input , type : requestType , cd : DNSSEC } ;
2019-02-07 00:27:27 +01:00
2019-02-07 09:28:23 +01:00
url . search = new URLSearchParams ( params ) ;
2019-02-06 21:20:20 +01:00
2019-02-07 09:28:23 +01:00
return fetch ( url , { headers : { "accept" : "application/dns-json" } } ) . then ( response => {
return response . json ( ) ;
2019-02-08 19:02:13 +01:00
} ) . then ( data => {
if ( justAnswer ) {
return extractData ( data . Answer ) ;
}
return data ;
} ) . catch ( e => {
throw new OperationError ( ` Error making request to ${ url } \n ${ e . toString ( ) } ` ) ;
} ) ;
2019-02-06 21:20:20 +01:00
}
2019-02-08 19:02:13 +01:00
}
2019-02-06 21:20:20 +01:00
2019-02-08 19:02:13 +01:00
/ * *
* Construct an array of just data from a DNS Answer section
*
* @ private
* @ param { JSON } data
* @ returns { JSON }
* /
function extractData ( data ) {
2019-08-27 19:13:33 +02:00
if ( typeof ( data ) == "undefined" ) {
2019-02-08 19:02:13 +01:00
return [ ] ;
} else {
const dataValues = [ ] ;
data . forEach ( element => {
dataValues . push ( element . data ) ;
} ) ;
return dataValues ;
2019-02-07 22:05:07 +01:00
}
2019-02-06 21:20:20 +01:00
}
2019-02-08 19:02:13 +01:00
export default DNSOverHTTPS ;