How to use dns-packet - 10 common examples

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 stackpath / serverless-scripting-examples / dns-over-https / src / index.ts View on Github external
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 stackpath / serverless-scripting-examples / dns-over-https / src / index.ts View on Github external
const newRequest = new Request(conf.dnsHttpUrlFallback, {
    method: request.method,
    headers: { 'Content-Type': 'application/dns-message' },
    body: requestBody,
  })
  const response = await fetch(newRequest)
  const responseContentType = response.headers.get('content-type')
  if (responseContentType !== 'application/dns-message') {
    const respText = await response.clone().text()
    throw new Error('Got other content type, resp text: ' + respText)
  }

  // Log the DNS packet before returning
  if (conf.log) try {
    const responseBody = await response.clone().arrayBuffer()
    logDnsPacket('Response DNS (deferred): ', dnsPacket.decode(Buffer.from(new Uint8Array(responseBody))))
  } catch (e) { }
  return response
}
github commonshost / dohnut / source / worker.js View on Github external
function dnsErrorServFail (id, query) {
  const { questions, flags } = dnsPacket.decode(query)

  // http://www.faqs.org/rfcs/rfc2929.html
  //                                 1  1  1  1  1  1
  //   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
  // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  // |QR|   Opcode  |AA|TC|RD|RA| Z|AD|CD|   RCODE   |

  return dnsPacket.encode({
    id,
    type: 'response',
    flags:
      (0b0111100000000000 & flags) | // opcode copied from query
      (0b0000000100000000 & flags) | // rd copied from query
      (0b0000000010000000) | // ra always true
      (0b0000000000000010), // rcode always ServFail
    questions
github stackpath / serverless-scripting-examples / dns-over-https / src / index.ts View on Github external
async function handleDnsQuery(request: Request) {
  // We need to take out the body and parse the DNS packet
  const requestBody = await request.arrayBuffer()
  const requestPacket = dnsPacket.decode(Buffer.from(new Uint8Array(requestBody)))
  logDnsPacket('Request DNS: ', requestPacket)

  // We only handle single questions for A and AAAA of the new TLD
  const question = (requestPacket.questions && requestPacket.questions.length === 1) ? requestPacket.questions[0] : null
  if (question === null || 
    (question.type !== 'A' && question.type !== 'AAAA') ||
    !question.name.endsWith('.' + conf.newTld)) return deferToDnsFallback(request, requestBody)
  log('Responding to query for new TLD')

  // If we ask for our TLD on localhost, we'll just send back the fixed local
  // IPs. However, if our host is something else, we defer to the fallback and
  // just change the answer names so we get SOAs, NSs, As, CNAMEs, etc.
  let responsePacket: dnsPacket.Packet
  if (conf.proxyDnsTo === 'localhost') {
    log('Sending back localhost IPs')
    responsePacket = {
github android-js / androidjs-builder / example / helloworld / node_modules / dns-socket / index.js View on Github external
const cnameresults = result.answers.filter(e => e.type === 'CNAME')
  if (cnameresults.length === 0) {
    return false
  }

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

  // replace current query with a new one
  q.query = {
    id: id + 1,
    flags: packet.RECURSION_DESIRED,
    questions: [{
      type: 'A',
      name: cnameresults[0].data
    }]
  }
  q.redirects++
  q.firstTry = Date.now()
  q.tries = 0
  q.buffer = packet.encode(q.query)
  this._queries[id] = q
  this.socket.send(q.buffer, 0, q.buffer.length, q.port, Array.isArray(q.host) ? q.host[Math.floor(q.host.length * Math.random())] : q.host || '127.0.0.1')
  return true
}
github stackpath / serverless-scripting-examples / dns-over-https / src / index.ts View on Github external
const question = (requestPacket.questions && requestPacket.questions.length === 1) ? requestPacket.questions[0] : null
  if (question === null || 
    (question.type !== 'A' && question.type !== 'AAAA') ||
    !question.name.endsWith('.' + conf.newTld)) return deferToDnsFallback(request, requestBody)
  log('Responding to query for new TLD')

  // If we ask for our TLD on localhost, we'll just send back the fixed local
  // IPs. However, if our host is something else, we defer to the fallback and
  // just change the answer names so we get SOAs, NSs, As, CNAMEs, etc.
  let responsePacket: dnsPacket.Packet
  if (conf.proxyDnsTo === 'localhost') {
    log('Sending back localhost IPs')
    responsePacket = {
      id: requestPacket.id,
      type: 'response',
      flags: dnsPacket.RECURSION_DESIRED | dnsPacket.RECURSION_AVAILABLE,
      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)
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')
      },
      headers: {
        accept: 'application/dns-message'
      }