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().
This commit is contained in:
Tom Pusateri 2018-03-27 16:06:14 -04:00 committed by silverwind
parent cd7caa6619
commit 0cf5761579
3 changed files with 97 additions and 9 deletions

View File

@ -1,11 +1,18 @@
'use strict' 'use strict'
const packet = require('./') const packet = require('..')
const net = require('net') 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', type: 'query',
id: 0xdead, id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED, flags: packet.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
@ -16,13 +23,28 @@ const buf = packet.streamEncode({
const client = new net.Socket() const client = new net.Socket()
client.connect(53, '8.8.8.8', function () { client.connect(53, '8.8.8.8', function () {
console.log('Connected') console.log('Connected')
client.write(buf) client.write(encodedPacket)
}) })
client.on('data', function (data) { client.on('data', function (data) {
console.log('Received response') console.log('Received response: %d bytes', data.byteLength)
console.log(packet.streamDecode(data)) if (response == null) {
client.destroy() // kill client after server's response 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 () { client.on('close', function () {

61
examples/tls.js Normal file
View File

@ -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')
})

View File

@ -1,13 +1,17 @@
'use strict' 'use strict'
const packet = require('./') const packet = require('..')
const dgram = require('dgram') const dgram = require('dgram')
const socket = dgram.createSocket('udp4') const socket = dgram.createSocket('udp4')
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const buf = packet.encode({ const buf = packet.encode({
type: 'query', type: 'query',
id: 1, id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED, flags: packet.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
@ -18,6 +22,7 @@ const buf = packet.encode({
socket.on('message', function (message, rinfo) { socket.on('message', function (message, rinfo) {
console.log(rinfo) console.log(rinfo)
console.log(packet.decode(message)) // prints out a response from google dns 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') socket.send(buf, 0, buf.length, 53, '8.8.8.8')