From 0cf5761579b0217c0f0a4c3f2edfff8a96b0e56a Mon Sep 17 00:00:00 2001 From: Tom Pusateri Date: Tue, 27 Mar 2018 16:06:14 -0400 Subject: [PATCH] Add TLS example and fix TCP example. (#31) * Add TLS example and fix TCP buffer management. Allow all examples to run from examples directory. * Inspect response data decoding length, not request. Simplify package require(). --- examples/tcp.js | 36 +++++++++++++++++++++++------ examples/tls.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ examples/udp.js | 9 ++++++-- 3 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 examples/tls.js diff --git a/examples/tcp.js b/examples/tcp.js index 25e69c8..27d1fcc 100644 --- a/examples/tcp.js +++ b/examples/tcp.js @@ -1,11 +1,18 @@ 'use strict' -const packet = require('./') +const packet = require('..') const net = require('net') -const buf = packet.streamEncode({ +var response = null +var expected_length = 0 + +function getRandomInt(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +const encodedPacket = packet.streamEncode({ type: 'query', - id: 0xdead, + id: getRandomInt(1, 65534), flags: packet.RECURSION_DESIRED, questions: [{ type: 'A', @@ -16,13 +23,28 @@ const buf = packet.streamEncode({ const client = new net.Socket() client.connect(53, '8.8.8.8', function () { console.log('Connected') - client.write(buf) + client.write(encodedPacket) }) client.on('data', function (data) { - console.log('Received response') - console.log(packet.streamDecode(data)) - client.destroy() // kill client after server's response + console.log('Received response: %d bytes', data.byteLength) + if (response == null) { + if (data.byteLength > 1) { + const plen = data.readUInt16BE(0) + expected_length = plen + if (plen < 12) { + throw new Error('below DNS minimum packet length') + } + response = Buffer.from(data) + } + } else { + response = Buffer.concat([response, data]) + } + + if (response.byteLength >= expected_length) { + console.log(packet.streamDecode(response)) + client.destroy() + } }) client.on('close', function () { diff --git a/examples/tls.js b/examples/tls.js new file mode 100644 index 0000000..58fc9d3 --- /dev/null +++ b/examples/tls.js @@ -0,0 +1,61 @@ +'use strict' + +const tls = require('tls') +const packet = require('..') + +var response = null +var expected_length = 0 + +function getRandomInt(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +const encodedPacket = packet.streamEncode({ + type: 'query', + id: getRandomInt(1, 65534), + flags: packet.RECURSION_DESIRED, + questions: [{ + type: 'A', + name: 'google.com' + }] +}) + +const context = tls.createSecureContext({ + secureProtocol: 'TLSv1_2_method' +}) + +const options = { + port: 853, + host: 'getdnsapi.net', + secureContext: context +} + +const client = tls.connect(options, () => { + console.log('client connected') + client.write(encodedPacket) +}) + +client.on('data', function (data) { + console.log('Received response: %d bytes', data.byteLength) + if (response == null) { + if (data.byteLength > 1) { + const plen = data.readUInt16BE(0) + expected_length = plen + if (plen < 12) { + throw new Error('below DNS minimum packet length') + } + response = Buffer.from(data) + } + } else { + response = Buffer.concat([response, data]) + } + + if (response.byteLength >= expected_length) { + console.log(packet.streamDecode(response)) + client.destroy() + } +}) + +client.on('end', () => { + console.log('Connection ended') +}) diff --git a/examples/udp.js b/examples/udp.js index 0b96397..dcf50f9 100644 --- a/examples/udp.js +++ b/examples/udp.js @@ -1,13 +1,17 @@ 'use strict' -const packet = require('./') +const packet = require('..') const dgram = require('dgram') const socket = dgram.createSocket('udp4') +function getRandomInt(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + const buf = packet.encode({ type: 'query', - id: 1, + id: getRandomInt(1, 65534), flags: packet.RECURSION_DESIRED, questions: [{ type: 'A', @@ -18,6 +22,7 @@ const buf = packet.encode({ socket.on('message', function (message, rinfo) { console.log(rinfo) console.log(packet.decode(message)) // prints out a response from google dns + socket.close() }) socket.send(buf, 0, buf.length, 53, '8.8.8.8')