### Install dns2 Package Source: https://github.com/lsongdev/node-dns/blob/master/README.md Install the dns2 package using npm. This is the first step before using the library in your project. ```bash $ npm install dns2 ``` -------------------------------- ### Create and Listen with a DNS Server Source: https://github.com/lsongdev/node-dns/blob/master/README.md Sets up a DNS server that listens on UDP and TCP ports. It handles incoming requests by providing a static A record for any queried domain. The server logs listening events and potential request errors. ```javascript const dns2 = require('dns2'); const { Packet } = dns2; const server = dns2.createServer({ udp: true, handle: (request, send, rinfo) => { const response = Packet.createResponseFromRequest(request); const [question] = request.questions; const { name } = question; response.answers.push({ name, type: Packet.TYPE.A, class: Packet.CLASS.IN, ttl: 300, address: '8.8.8.8', }); send(response); }, }); server.on('request', (request, response, rinfo) => { console.log(request.header.id, request.questions[0]); }); server.on('requestError', error => { console.log('Client sent an invalid request', error); }); server.on('listening', () => { console.log(server.addresses()); }); server.on('close', () => { console.log('server closed'); }); server.listen({ // Optionally specify port, address and/or the family of socket() for udp server: udp: { port: 5333, address: '127.0.0.1', }, // Optionally specify port and/or address for tcp server: tcp: { port: 5333, address: '127.0.0.1', }, }); // eventually server.close(); ``` -------------------------------- ### Run UDP Benchmark Source: https://github.com/lsongdev/node-dns/blob/master/benchmark/README.md Execute the UDP throughput benchmark. This can be tuned via environment variables for total queries and concurrency, or directed at an external DNS server. ```bash node benchmark/udp.js ``` ```bash # Tune via env vars TOTAL=50000 CONCURRENCY=200 node benchmark/udp.js ``` ```bash # Target an external server DNS=127.0.0.1:5353 node benchmark/udp.js ``` -------------------------------- ### UDP Client Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate and use the UDP client for DNS lookups. This is the default client behavior. ```APIDOC ## UDP Client ### Description The `dns2` library provides a `UDPClient` for performing DNS lookups over UDP. This is the default protocol used for DNS queries. ### Instantiation ```javascript const { UDPClient } = require('dns2'); const resolve = UDPClient(); ``` ### Usage ```javascript (async () => { const response = await resolve('google.com'); console.log(response.answers); })(); ``` ### Options The `UDPClient` can be instantiated with options: ```javascript const resolve = UDPClient({ nameServers: ['8.8.8.8'], // Array of DNS server IPs port: 53, // DNS server port (number) recursive: true // Recursion Desired flag (boolean, default true) }); ``` ``` -------------------------------- ### DNS Client UDP Lookup (Direct) Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate a UDP client directly and perform an asynchronous lookup for records of a domain. This provides a more direct way to use the UDP client. ```javascript const { UDPClient } = require('dns2'); const resolve = UDPClient(); (async () => { const response = await resolve('google.com'); console.log(response.answers); })(); ``` -------------------------------- ### Test DNS Server with dig Source: https://github.com/lsongdev/node-dns/blob/master/README.md Command-line utility to test the custom DNS server by sending a query to a specific IP address and port. ```bash $ dig @127.0.0.1 -p5333 lsong.org ``` -------------------------------- ### DNS Client UDP Lookup (A Record) Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate a DNS client and perform an asynchronous lookup for A records of a domain using UDP. The client can be configured with custom name servers and port. ```javascript const dns2 = require('dns2'); const options = { // nameServers: ['8.8.8.8'] — array of DNS server IPs (default: Google + 114dns) // port: 53 — DNS server port (number) // recursive: true — Recursion Desired flag (boolean, default true) }; const dns = new dns2(options); (async () => { const result = await dns.resolveA('google.com'); console.log(result.answers); })(); ``` -------------------------------- ### DNS Client TCP with System DNS Server Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate a TCP client and use the first DNS server available from the operating system's native node dns configuration. This leverages the system's default DNS settings. ```javascript const dns = require('dns'); const { TCPClient } = require('dns2'); const resolve = TCPClient({ dns: dns.getServers()[0], }); (async () => { try { const result = await resolve('google.com'); console.log(result.answers); } catch (error) { console.log(error); } })(); ``` -------------------------------- ### DNS Client TCP Lookup Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate a TCP client and perform an asynchronous lookup for records of a domain. Handles potential empty responses from some DNS servers when using TCP. ```javascript const { TCPClient } = require('dns2'); const resolve = TCPClient(); (async () => { try { const response = await resolve('lsong.org'); console.log(response.answers); } catch (error) { // some DNS servers (i.e cloudflare 1.1.1.1, 1.0.0.1) // may send an empty response when using TCP console.log(error); } })(); ``` -------------------------------- ### DNS Client TCP with Custom DNS Server Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate a TCP client and specify a custom DNS server IP address. This allows directing queries to a specific DNS server. ```javascript const { TCPClient } = require('dns2'); const resolve = TCPClient({ dns: '1.1.1.1', }); (async () => { try { const result = await resolve('google.com'); console.log(result.answers); } catch (error) { console.log(error); } })(); ``` -------------------------------- ### TCP Client Source: https://github.com/lsongdev/node-dns/blob/master/README.md Instantiate and use the TCP client for DNS lookups. Useful when UDP is blocked or for specific server behaviors. ```APIDOC ## TCP Client ### Description The `dns2` library provides a `TCPClient` for performing DNS lookups over TCP. This is useful when UDP is not preferred or when dealing with DNS servers that behave differently over TCP. ### Instantiation ```javascript const { TCPClient } = require('dns2'); const resolve = TCPClient(); ``` ### Usage ```javascript (async () => { try { const response = await resolve('lsong.org'); console.log(response.answers); } catch (error) { console.log(error); } })(); ``` ### Options The `TCPClient` can be instantiated with options, including specifying custom DNS servers: ```javascript // Using a specific DNS server IP const resolve = TCPClient({ dns: '1.1.1.1' }); // Using the system's default DNS server const dns = require('dns'); const resolveSystem = TCPClient({ dns: dns.getServers()[0] }); ``` ### Error Handling Some DNS servers might send empty responses over TCP, which can lead to errors. It's recommended to wrap TCP client calls in a try-catch block. ``` -------------------------------- ### Respond with DNS Error Codes Source: https://github.com/lsongdev/node-dns/blob/master/README.md Demonstrates how to send standard DNS error responses from a custom DNS server handler. It shows how to refuse queries for specific domains or indicate non-existent domains using `Packet.RCODE`. ```javascript const dns2 = require('dns2'); const { Packet } = dns2; const server = dns2.createServer({ udp: true, handle: (request, send) => { const response = Packet.createResponseFromRequest(request); const [question] = request.questions; if (question.name.endsWith('.internal')) { // Refuse queries for internal names response.header.rcode = Packet.RCODE.REFUSED; return send(response); } if (!isKnownDomain(question.name)) { // Domain does not exist response.header.rcode = Packet.RCODE.NXDOMAIN; return send(response); } // Normal answer ... response.answers.push({ name: question.name, type: Packet.TYPE.A, class: Packet.CLASS.IN, ttl: 300, address: '1.2.3.4', }); send(response); }, }); ``` -------------------------------- ### DNS Client Convenience Methods Source: https://github.com/lsongdev/node-dns/blob/master/README.md The DNS class provides high-level methods for resolving common DNS record types. These methods abstract away the complexities of direct record type specification. ```APIDOC ## DNS Client Convenience Methods ### Description The `dns2` library offers a `DNS` class with convenience methods for resolving various common DNS record types. These methods simplify the process of querying specific record types like A, AAAA, MX, CNAME, SOA, PTR, DNSKEY, and RRSIG. ### Methods - `resolveA(domain)`: Resolves A records (IPv4 addresses). - `resolveAAAA(domain)`: Resolves AAAA records (IPv6 addresses). - `resolveMX(domain)`: Resolves MX records (Mail Exchanger). - `resolveCNAME(domain)`: Resolves CNAME records (Canonical Name). - `resolveSOA(domain)`: Resolves SOA records (Start of Authority). - `resolvePTR(domain)`: Resolves PTR records (Pointer). - `resolveDNSKEY(domain)`: Resolves DNSKEY records (DNS Public Key). - `resolveRRSIG(domain)`: Resolves RRSIG records (Resource Record Signature). ### General Resolve Method - `resolve(domain, 'TYPE')`: For any record type not listed above, use this general method by specifying the desired record type as a string. ### Key Answer Fields - **A/AAAA**: `address` - **MX**: `exchange`, `priority` - **CNAME/PTR**: `domain` - **SOA**: `primary`, `admin`, `serial`, `refresh`, `retry`, `expiration`, `minimum` - **DNSKEY**: `publicKey`, `algorithm` - **RRSIG**: Varies depending on the record type. ``` -------------------------------- ### Limit Concurrent Handlers Source: https://github.com/lsongdev/node-dns/blob/master/benchmark/README.md Use the `maxConcurrent` option when creating a server to limit the number of simultaneous handler invocations. Requests exceeding this limit will receive an immediate SERVFAIL response. ```javascript const server = dns2.createServer({ udp: true, maxConcurrent: 500, // at most 500 handler calls in flight at once handle(request, send) { // async work here... }, }); ``` -------------------------------- ### DNS Client SOA Record Lookup Source: https://github.com/lsongdev/node-dns/blob/master/README.md Perform an asynchronous lookup for SOA records of a domain. The result may contain the SOA record in either the 'answers' or 'authorities' section of the response. ```javascript const dns2 = require('dns2'); const dns = new dns2({ nameServers: ['8.8.8.8'] }); (async () => { const result = await dns.resolveSOA('google.com'); const soa = result.answers[0] || result.authorities[0]; if (soa) { console.log(soa.primary); // ns1.google.com console.log(soa.admin); // dns-admin.google.com console.log(soa.serial); // zone serial number console.log(soa.refresh); // refresh interval (seconds) console.log(soa.retry); // retry interval (seconds) console.log(soa.expiration); // expiry (seconds) console.log(soa.minimum); // minimum TTL (seconds) } })(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.