tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

tcp.js (1141B)


      1 'use strict'
      2 
      3 const dnsPacket = require('..')
      4 const net = require('net')
      5 
      6 var response = null
      7 var expectedLength = 0
      8 
      9 function getRandomInt (min, max) {
     10  return Math.floor(Math.random() * (max - min + 1)) + min
     11 }
     12 
     13 const buf = dnsPacket.streamEncode({
     14  type: 'query',
     15  id: getRandomInt(1, 65534),
     16  flags: dnsPacket.RECURSION_DESIRED,
     17  questions: [{
     18    type: 'A',
     19    name: 'google.com'
     20  }]
     21 })
     22 
     23 const client = new net.Socket()
     24 client.connect(53, '8.8.8.8', function () {
     25  console.log('Connected')
     26  client.write(buf)
     27 })
     28 
     29 client.on('data', function (data) {
     30  console.log('Received response: %d bytes', data.byteLength)
     31  if (response == null) {
     32    if (data.byteLength > 1) {
     33      const plen = data.readUInt16BE(0)
     34      expectedLength = plen
     35      if (plen < 12) {
     36        throw new Error('below DNS minimum packet length')
     37      }
     38      response = Buffer.from(data)
     39    }
     40  } else {
     41    response = Buffer.concat([response, data])
     42  }
     43 
     44  if (response.byteLength >= expectedLength) {
     45    console.log(dnsPacket.streamDecode(response))
     46    client.destroy()
     47  }
     48 })
     49 
     50 client.on('close', function () {
     51  console.log('Connection closed')
     52 })