A DNS Server and Client Implementation in Pure JavaScript with no dependencies.
- Server and Client
- Lot of Type Supported
- Extremely lightweight
- DNS over UDP, TCP, HTTPS Supported
$ npm install dns2
Lookup any records available for the domain lsong.org
.
const DNS = require('dns2');
const dns = new DNS();
(async () => {
const result = await dns.resolveA('google.com');
console.log(result.answers);
})();
const dns = require('..');
const { Packet } = dns;
const server = dns.createServer((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.listen(5333);
Then you can test your DNS server:
$ dig @127.0.0.1 -p5333 lsong.org
Note that when implementing your own lookups, the contents of the query
will be found in request.questions[0].name
.
- dns2.createServer()
- dns2.lookup()
- dns2.Packet()
- dns2.Client()
- dns2.Server()
- RFC-1034 - Domain Names - Concepts and Facilities
- RFC-1035 - Domain Names - Implementation and Specification
- RFC-2782 - A DNS RR for specifying the location of services (DNS SRV)
- RFC-7766 - DNS Transport over TCP - Implementation Requirements
- RFC-8484 - DNS Queries over HTTPS (DoH)
- Fork this Repo first
- Clone your Repo
- Install dependencies by
$ npm install
- Checkout a feature branch
- Feel free to add your features
- Make sure your features are fully tested
- Publish your local branch, Open a pull request
- Enjoy hacking <3
Copyright (c) 2016 lsong
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.