How to use the dns-packet.encode function in dns-packet

To help you get started, we’ve selected a few dns-packet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github desec-io / desec-stack / test / e2e / setup.js View on Github external
chakram.resolve = function (name, type) {
    var deferred = Q.defer();

    var buf = packet.encode({
        type: 'query',
        id: nextId,
        questions: [{
            type: type,
            class: 'IN',
            name: name
        }]
    });

    // FIXME contacting nslord for DNS responses. This can changed to nsmaster as soon as changes to the DNS are applied
    // immediately, i.e. before pdns HTTP responses return to the API.
    socket.send(buf, 0, buf.length, 53, process.env.DESECSTACK_IPV4_REAR_PREFIX16 + '.0.129');
    inflight[nextId] = deferred;
    nextId = (nextId + 1) % 65536;  // We don't care if id's are predictable in our test setting

    return deferred.promise;
github stackpath / serverless-scripting-examples / dns-over-https / src / index.ts View on Github external
questions: requestPacket.questions,
      answers: [{
        type: question.type,
        class: 'IN',
        name: question.name,
        data: question.type === 'A' ? '127.0.0.1' : '::1',
        ttl: 600
      }]
    }
  } else {
    // Update the request packet to do a different host
    log('Proxying requested DNS name to: ', conf.proxyDnsTo)
    const origName = question.name
    requestPacket.questions.forEach(q => q.name = conf.proxyDnsTo)
    // Send it off
    const proxiedResponse = await deferToDnsFallback(request, dnsPacket.encode(requestPacket).buffer)
    // Change any response names back
    responsePacket = dnsPacket.decode(Buffer.from(new Uint8Array(await proxiedResponse.arrayBuffer())))
    responsePacket.questions.forEach(q => {
      if (q.name === conf.proxyDnsTo) q.name = origName
    })
    responsePacket.answers.forEach(a => {
      if (a.name === conf.proxyDnsTo) a.name = origName
    })
  }
  
  // Send the response back
  const responseBody = dnsPacket.encode(responsePacket).buffer
  if (conf.log) logDnsPacket('Response DNS (custom): ', dnsPacket.decode(Buffer.from(new Uint8Array(responseBody))))
  return new Response(dnsPacket.encode(responsePacket).buffer, {
    status: 200,
    headers: { 'Content-Type': 'application/dns-message' }
github ipfs / js-ipfs / src / core / ipns / routing / dns-datastore.js View on Github external
function dohBinary (key, callback) {
  const cid = new Cid(key.slice(ipns.namespaceLength))
  const buf = dnsPacket.encode({
    type: 'query',
    id: getRandomInt(1, 65534),
    flags: dnsPacket.RECURSION_DESIRED,
    questions: [{
      type: 'TXT',
      name: `${cid.toV1().toString()}.dns.ipns.dev`
    }]
  })
  // https://dns.google.com/experimental
  // https://cloudflare-dns.com/dns-query
  // https://mozilla.cloudflare-dns.com/dns-query
  ky
    .get('https://cloudflare-dns.com/dns-query', {
      searchParams: {
        dns: buf.toString('base64')
      },
github commonshost / dohnut / source / worker.js View on Github external
function spoof () {
    const query = dnsPacket.encode({
      type: 'query',
      questions: [{
        type: 'A',
        name: domain
      }]
    })
    const stream = sendQuery(query)
    stream.on('error', (error) => {
      console.error(`Worker ${threadId}: spoofed query failed - ${error.message}`)
      stream.close()
    })
    stream.resume()
  }
  const random = Math.random()
github ipfs / js-ipfs / src / core / ipns / routing / experimental / utils.js View on Github external
async function dohBinary (url, domain, key) {
  const start = Date.now()
  const keyStr = keyToBase32(key)
  const buf = dnsPacket.encode({
    type: 'query',
    questions: [{
      type: 'TXT',
      name: `${keyStr}.${domain}`
    }]
  })

  const result = await ky
    .get(url, {
      searchParams: {
        dns: buf.toString('base64')
      },
      headers: {
        accept: 'application/dns-message'
      }
    })
github android-js / androidjs-builder / example / helloworld / node_modules / dns-socket / index.js View on Github external
cb(new Error('Socket destroyed'))
    return 0
  }

  this.inflight++
  query.type = 'query'
  query.flags = typeof query.flags === 'number' ? query.flags : DNS.RECURSION_DESIRED

  const id = this._getNextEmptyId()
  if (id === -1) {
    cb(new Error('Query array is full!'))
    return 0
  }

  query.id = id + 1
  const buffer = packet.encode(query)

  this._queries[id] = {
    callback: cb || noop,
    redirects: 0,
    firstTry: Date.now(),
    query: query,
    tries: 0,
    buffer: buffer,
    port: port,
    host: host
  }
  this.socket.send(buffer, 0, buffer.length, port, Array.isArray(host) ? host[Math.floor(host.length * Math.random())] : host || '127.0.0.1')
  return id
}
github mafintosh / multicast-dns / index.js View on Github external
function onbind (err) {
      if (destroyed) return cb()
      if (err) return cb(err)
      var message = packet.encode(value)
      socket.send(message, 0, message.length, rinfo.port, rinfo.address || rinfo.host, cb)
    }
  }
github mafintosh / dns-socket / index.js View on Github external
cb(new Error('Socket destroyed'))
    return 0
  }

  this.inflight++
  query.type = 'query'
  query.flags = typeof query.flags === 'number' ? query.flags : DNS.RECURSION_DESIRED

  const id = this._getNextEmptyId()
  if (id === -1) {
    cb(new Error('Query array is full!'))
    return 0
  }

  query.id = id + 1
  const buffer = packet.encode(query)

  this._queries[id] = {
    callback: cb || noop,
    redirects: 0,
    firstTry: Date.now(),
    query: query,
    tries: 0,
    buffer: buffer,
    port: port,
    host: host
  }
  this.socket.send(buffer, 0, buffer.length, port, Array.isArray(host) ? host[Math.floor(host.length * Math.random())] : host || '127.0.0.1')
  return id
}