2019-02-06 20:20:20 +00:00
/**
* @author h345983745 []
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation" ;
import OperationError from "../errors/OperationError" ;
/**
* HTTPS Over DNS operation
*/
class HTTPSOverDNS extends Operation {
/**
* HTTPSOverDNS constructor
*/
constructor () {
super ();
2019-02-06 22:34:43 +00:00
this . name = "DNS over HTTPS" ;
2019-02-07 21:05:07 +00:00
this . module = "Default" ;
2019-02-06 22:50:46 +00:00
this . description = [ "Takes a single domain name and performs a DNS lookup using DNS over HTTPS." ,
2019-02-07 08:28:23 +00:00
"<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" );
2019-02-06 20:20:20 +00:00
this . infoURL = "https://en.wikipedia.org/wiki/DNS_over_HTTPS" ;
this . inputType = "string" ;
this . outputType = "JSON" ;
2019-02-06 20:54:06 +00:00
this . manualBake = true ;
2019-02-06 20:20:20 +00: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-06 23:27:27 +00:00
"MX" ,
"DNSKEY" ,
"NS"
2019-02-06 20:20:20 +00:00
]
},
{
2019-02-06 22:50:46 +00:00
name : "Answer Data Only" ,
2019-02-06 20:20:20 +00:00
type : "boolean" ,
value : false
},
{
name : "Validate DNSSEC" ,
type : "boolean" ,
value : true
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {JSON}
*/
run ( input , args ) {
const [ resolver , requestType , justAnswer , DNSSEC ] = args ;
2019-02-07 08:28:23 +00:00
let url = URL ;
try {
url = new URL ( resolver );
2019-02-06 20:54:06 +00:00
} catch ( error ) {
2019-02-07 08:28:23 +00:00
throw new OperationError ( error . toString () +
2019-02-06 20:54:06 +00:00
"\n\nThis error could be caused by one of the following:\n" +
2019-02-07 08:28:23 +00:00
" - An invalid Resolver URL\n" );
2019-02-06 20:54:06 +00:00
}
2019-02-07 08:28:23 +00:00
const params = { name : input , type : requestType , cd : DNSSEC };
2019-02-06 23:27:27 +00:00
2019-02-07 08:28:23 +00:00
url . search = new URLSearchParams ( params );
2019-02-06 20:20:20 +00:00
2019-02-07 08:28:23 +00:00
return fetch ( url , { headers : { "accept" : "application/dns-json" }}). then ( response => {
return response . json ();
})
. then ( data => {
if ( justAnswer ) {
2019-02-07 21:05:07 +00:00
return this . extractData ( data . Answer );
2019-02-07 08:28:23 +00:00
}
return data ;
2019-02-06 20:20:20 +00:00
2019-02-07 08:28:23 +00:00
}). catch ( e => {
throw new OperationError ( "Error making request to : " + url + "\n" +
"Error Message: " + e . toString ());
});
2019-02-06 20:20:20 +00:00
}
2019-02-07 21:05:07 +00:00
/**
* Construct an array of just data from a DNS Answer section
* @private
* @param {JSON} data
* @returns {JSON}
*/
extractData ( data ) {
if ( typeof ( data ) == "undefined" ){
return [];
} else {
const dataValues = [];
data . forEach ( element => {
dataValues . push ( element . data );
});
return dataValues ;
}
}
2019-02-06 20:20:20 +00:00
}
export default HTTPSOverDNS ;