refactor and lint examples

This commit is contained in:
silverwind 2018-07-02 21:19:20 +02:00
parent 0f87ff09f5
commit d478ebe4ab
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
6 changed files with 37 additions and 37 deletions

View File

@ -10,15 +10,15 @@ npm install dns-packet
## UDP Usage ## UDP Usage
``` js ``` js
var packet = require('dns-packet') var dnsPacket = require('dns-packet')
var dgram = require('dgram') var dgram = require('dgram')
var socket = dgram.createSocket('udp4') var socket = dgram.createSocket('udp4')
var buf = packet.encode({ var buf = dnsPacket.encode({
type: 'query', type: 'query',
id: 1, id: 1,
flags: packet.RECURSION_DESIRED, flags: dnsPacket.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
class: 'IN', class: 'IN',
@ -27,7 +27,7 @@ var buf = packet.encode({
}) })
socket.on('message', function (message) { socket.on('message', function (message) {
console.log(packet.decode(message)) // prints out a response from google dns console.log(dnsPacket.decode(message)) // prints out a response from google dns
}) })
socket.send(buf, 0, buf.length, 53, '8.8.8.8') socket.send(buf, 0, buf.length, 53, '8.8.8.8')

View File

@ -8,17 +8,17 @@
* LICENSE: MIT * LICENSE: MIT
*/ */
const packet = require('..') const dnsPacket = require('..')
const https = require('https') const https = require('https')
function getRandomInt (min, max) { function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min
} }
const encodedPacket = packet.encode({ const encodeddnsPacket = dnsPacket.encode({
type: 'query', type: 'query',
id: getRandomInt(1, 65534), id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED, flags: dnsPacket.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
name: 'google.com' name: 'google.com'
@ -32,7 +32,7 @@ const options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/dns-udpwireformat', 'Content-Type': 'application/dns-udpwireformat',
'Content-Length': Buffer.byteLength(encodedPacket) 'Content-Length': Buffer.byteLength(encodeddnsPacket)
} }
} }
@ -41,12 +41,12 @@ const request = https.request(options, (response) => {
console.log('headers:', response.headers) console.log('headers:', response.headers)
response.on('data', (d) => { response.on('data', (d) => {
console.log(packet.decode(d)) console.log(dnsPacket.decode(d))
}) })
}) })
request.on('error', (e) => { request.on('error', (e) => {
console.error(e) console.error(e)
}) })
request.write(encodedPacket) request.write(encodeddnsPacket)
request.end() request.end()

View File

@ -1,19 +1,19 @@
'use strict' 'use strict'
const packet = require('..') const dnsPacket = require('..')
const net = require('net') const net = require('net')
var response = null var response = null
var expected_length = 0 var expectedLength = 0
function getRandomInt (min, max) { function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min
} }
const encodedPacket = packet.streamEncode({ const encodedPacket = dnsPacket.streamEncode({
type: 'query', type: 'query',
id: getRandomInt(1, 65534), id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED, flags: dnsPacket.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
name: 'google.com' name: 'google.com'
@ -31,7 +31,7 @@ client.on('data', function (data) {
if (response == null) { if (response == null) {
if (data.byteLength > 1) { if (data.byteLength > 1) {
const plen = data.readUInt16BE(0) const plen = data.readUInt16BE(0)
expected_length = plen expectedLength = plen
if (plen < 12) { if (plen < 12) {
throw new Error('below DNS minimum packet length') throw new Error('below DNS minimum packet length')
} }
@ -41,8 +41,8 @@ client.on('data', function (data) {
response = Buffer.concat([response, data]) response = Buffer.concat([response, data])
} }
if (response.byteLength >= expected_length) { if (response.byteLength >= expectedLength) {
console.log(packet.streamDecode(response)) console.log(dnsPacket.streamDecode(response))
client.destroy() client.destroy()
} }
}) })

View File

@ -1,19 +1,19 @@
'use strict' 'use strict'
const tls = require('tls') const tls = require('tls')
const packet = require('..') const dnsPacket = require('..')
var response = null var response = null
var expected_length = 0 var expectedLength = 0
function getRandomInt (min, max) { function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min
} }
const encodedPacket = packet.streamEncode({ const encodedPacket = dnsPacket.streamEncode({
type: 'query', type: 'query',
id: getRandomInt(1, 65534), id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED, flags: dnsPacket.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
name: 'google.com' name: 'google.com'
@ -40,7 +40,7 @@ client.on('data', function (data) {
if (response == null) { if (response == null) {
if (data.byteLength > 1) { if (data.byteLength > 1) {
const plen = data.readUInt16BE(0) const plen = data.readUInt16BE(0)
expected_length = plen expectedLength = plen
if (plen < 12) { if (plen < 12) {
throw new Error('below DNS minimum packet length') throw new Error('below DNS minimum packet length')
} }
@ -50,8 +50,8 @@ client.on('data', function (data) {
response = Buffer.concat([response, data]) response = Buffer.concat([response, data])
} }
if (response.byteLength >= expected_length) { if (response.byteLength >= expectedLength) {
console.log(packet.streamDecode(response)) console.log(dnsPacket.streamDecode(response))
client.destroy() client.destroy()
} }
}) })

View File

@ -1,18 +1,18 @@
'use strict' 'use strict'
const packet = require('..') const dnsPacket = require('..')
const dgram = require('dgram') const dgram = require('dgram')
const socket = dgram.createSocket('udp4') const socket = dgram.createSocket('udp4')
function getRandomInt (min, max) { function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min
} }
const buf = packet.encode({ const buf = dnsPacket.encode({
type: 'query', type: 'query',
id: getRandomInt(1, 65534), id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED, flags: dnsPacket.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
name: 'google.com' name: 'google.com'
@ -21,7 +21,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(dnsPacket.decode(message)) // prints out a response from google dns
socket.close() socket.close()
}) })

View File

@ -10,7 +10,7 @@
"node": ">=6" "node": ">=6"
}, },
"scripts": { "scripts": {
"test": "eslint --color *.js && tape test.js" "test": "eslint --color *.js examples/*.js && tape test.js"
}, },
"dependencies": { "dependencies": {
"ip": "^1.1.5" "ip": "^1.1.5"