udp.js (617B)
1 'use strict' 2 3 const dnsPacket = require('..') 4 const dgram = require('dgram') 5 6 const socket = dgram.createSocket('udp4') 7 8 function getRandomInt (min, max) { 9 return Math.floor(Math.random() * (max - min + 1)) + min 10 } 11 12 const buf = dnsPacket.encode({ 13 type: 'query', 14 id: getRandomInt(1, 65534), 15 flags: dnsPacket.RECURSION_DESIRED, 16 questions: [{ 17 type: 'A', 18 name: 'google.com' 19 }] 20 }) 21 22 socket.on('message', function (message, rinfo) { 23 console.log(rinfo) 24 console.log(dnsPacket.decode(message)) // prints out a response from google dns 25 socket.close() 26 }) 27 28 socket.send(buf, 0, buf.length, 53, '8.8.8.8')