doh.js (1038B)
1 'use strict' 2 3 /* 4 * Sample code to make DNS over HTTPS request using POST 5 * AUTHOR: Tom Pusateri <pusateri@bangj.com> 6 * DATE: March 17, 2018 7 * LICENSE: MIT 8 */ 9 10 const dnsPacket = require('..') 11 const https = require('https') 12 13 function getRandomInt (min, max) { 14 return Math.floor(Math.random() * (max - min + 1)) + min 15 } 16 17 const buf = dnsPacket.encode({ 18 type: 'query', 19 id: getRandomInt(1, 65534), 20 flags: dnsPacket.RECURSION_DESIRED, 21 questions: [{ 22 type: 'A', 23 name: 'google.com' 24 }] 25 }) 26 27 const options = { 28 hostname: 'dns.google.com', 29 port: 443, 30 path: '/experimental', 31 method: 'POST', 32 headers: { 33 'Content-Type': 'application/dns-udpwireformat', 34 'Content-Length': Buffer.byteLength(buf) 35 } 36 } 37 38 const request = https.request(options, (response) => { 39 console.log('statusCode:', response.statusCode) 40 console.log('headers:', response.headers) 41 42 response.on('data', (d) => { 43 console.log(dnsPacket.decode(d)) 44 }) 45 }) 46 47 request.on('error', (e) => { 48 console.error(e) 49 }) 50 request.write(buf) 51 request.end()