Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'dns-packet' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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;
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' }
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')
},
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' }
})
}
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
}
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
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 = {
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
}
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)
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'
}