openmohan / lightdns

A basic DNS Server (Work in progres)
https://medium.com/@openmohan/dns-basics-and-building-simple-dns-server-in-go-6cb8e1cfe461
MIT License
44 stars 8 forks source link
dns dns-server godns

lightdns

This is a light weight DNS Server which serves A type record for now.

Installation

go get github.com/openmohan/lightdns

Requirements

Usage

  1. Import the package lightdns "github.com/openmohan/lightdns"
  2. Create a DNS Server dns := lightdns.NewDNSServer(port_int)
  3. Add Zone data using the function AddZoneData

dns.AddZoneData(zone, staticRecords, lookupFunction, ZoneType)

Example

If the records data available we can code it and Add the zone data

    var googleRecords = map[string]string{
        "mail.google.com":  "192.168.0.2",
        "paste.google.com": "192.168.0.3",
    }
    dns.AddZoneData("google.com", googleRecords, nil, lightdns.DNSForwardLookupZone)

or

If the data is not static and has to be taken from a DB or from any other sources or calculated dynamically then use lookup function. Define a lookup function in the format

func lookupFunc(domain string) (ip string, err error) ie: given a domain name string as parameter, IP string and error value has to be returned.

    func lookupFunc(string) (string, error) {
        //Do some action
        //Get data from DB
        //Process it further more
        return "192.2.2.1", nil
    }
    dns.AddZoneData("amazon.com", nil, lookupFunc, lightdns.DNSForwardLookupZone)
  1. Start the DNS Server by dns.StartAndServe()

Complete Example

package main

import (
    lightdns "github.com/openmohan/lightdns"
)

var records = map[string]string{
    "mail.amazon.com":  "192.162.1.2",
    "paste.amazon.com": "191.165.0.3",
}

func lookupFunc(string) (string, error) {
    //Do some action
    //Get data from DB
    //Process it further more
    return "192.2.2.1", nil
}

func main() {
    var googleRecords = map[string]string{
        "mail.google.com":  "192.168.0.2",
        "paste.google.com": "192.168.0.3",
    }
    var microsoftRecords = map[string]string{
        "mail.microsoft.com":  "192.168.0.78",
        "paste.microsoft.com": "192.168.0.25",
    }
    dns := lightdns.NewDNSServer(1234)
    dns.AddZoneData("google.com", googleRecords, nil, lightdns.DNSForwardLookupZone)
    dns.AddZoneData("microsoft.com", microsoftRecords, nil, lightdns.DNSForwardLookupZone)

    /* Incase if the records are not static or to be taken from DB or from any other sources
    lookupFunc method can be used.append*/
    dns.AddZoneData("amazon.com", nil, lookupFunc, lightdns.DNSForwardLookupZone)
    dns.StartAndServe()
}

Work in progress

Support for

  1. MX, AAAA, SOA and other records
  2. Reverse lookup zone

Code

  1. Use goroutines for optimizing the speed
  2. Unit Test Cases
  3. Logging mechanism for monitoring and debugging
  4. Error handling

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT