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
``` js
var packet = require('dns-packet')
var dnsPacket = require('dns-packet')
var dgram = require('dgram')
var socket = dgram.createSocket('udp4')
var buf = packet.encode({
var buf = dnsPacket.encode({
type: 'query',
id: 1,
flags: packet.RECURSION_DESIRED,
flags: dnsPacket.RECURSION_DESIRED,
questions: [{
type: 'A',
class: 'IN',
@ -27,7 +27,7 @@ var buf = packet.encode({
})
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')

View File

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

View File

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

View File

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

View File

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

View File

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