initial commit
This commit is contained in:
commit
99568644d1
17
config.yml
Normal file
17
config.yml
Normal file
@ -0,0 +1,17 @@
|
||||
zones:
|
||||
- zone: example.com.
|
||||
file: zonefile.txt
|
||||
- zone: example.com.
|
||||
file: zonefile.txt
|
||||
acl:
|
||||
- vpn
|
||||
|
||||
acl:
|
||||
- name: vpn
|
||||
range: 10.0.0.0/24
|
||||
|
||||
forward:
|
||||
alc:
|
||||
- vpn
|
||||
|
||||
|
205
coolDns.go
Normal file
205
coolDns.go
Normal file
@ -0,0 +1,205 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type zone struct {
|
||||
zone string
|
||||
rr rrMap
|
||||
acl []string
|
||||
}
|
||||
|
||||
type rrMap map[uint16]map[string][]dns.RR
|
||||
|
||||
type config struct {
|
||||
Zones []configZone `yaml:"zones"`
|
||||
ACL []configACL `yaml:"acl"`
|
||||
Forward configForward `yaml:"forward"`
|
||||
}
|
||||
|
||||
type configForward struct {
|
||||
ACL []string `yaml:"acl"`
|
||||
}
|
||||
|
||||
type configACL struct {
|
||||
Name string `yaml:"name"`
|
||||
IPRange string `yaml:"range"`
|
||||
}
|
||||
|
||||
type configZone struct {
|
||||
Zone string `yaml:"zone"`
|
||||
File string `yaml:"file"`
|
||||
ACL []string `yaml:"acl"`
|
||||
}
|
||||
|
||||
func loadConfig() (*config, error) {
|
||||
file, err := ioutil.ReadFile("config.yml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var loadedConfig config
|
||||
|
||||
err = yaml.Unmarshal(file, &loadedConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &loadedConfig, nil
|
||||
}
|
||||
|
||||
func loadZones(configZones []configZone) ([]zone, error) {
|
||||
zones := make([]zone, 0)
|
||||
for _, z := range configZones {
|
||||
rrs, err := loadZonefile(z.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
zones = append(zones, zone{
|
||||
zone: z.Zone,
|
||||
rr: createRRMap(rrs),
|
||||
})
|
||||
log.Printf("Loaded zone %s\n", z.Zone)
|
||||
}
|
||||
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
func createRRMap(rrs []dns.RR) rrMap {
|
||||
rrMap := make(rrMap)
|
||||
for _, rr := range rrs {
|
||||
if rrMap[rr.Header().Rrtype] == nil {
|
||||
rrMap[rr.Header().Rrtype] = make(map[string][]dns.RR)
|
||||
}
|
||||
|
||||
if rrMap[rr.Header().Rrtype][rr.Header().Name] == nil {
|
||||
rrMap[rr.Header().Rrtype][rr.Header().Name] = make([]dns.RR, 0)
|
||||
}
|
||||
rrMap[rr.Header().Rrtype][rr.Header().Name] = append(rrMap[rr.Header().Rrtype][rr.Header().Name], rr)
|
||||
}
|
||||
|
||||
return rrMap
|
||||
}
|
||||
|
||||
func loadZonefile(filepath string) ([]dns.RR, error) {
|
||||
file, err := os.Open(filepath)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parser := dns.NewZoneParser(file, "", "")
|
||||
|
||||
var rrs = make([]dns.RR, 0)
|
||||
|
||||
for rr, ok := parser.Next(); ok; rr, ok = parser.Next() {
|
||||
rrs = append(rrs, rr)
|
||||
}
|
||||
|
||||
if err := parser.Err(); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
return rrs, nil
|
||||
}
|
||||
|
||||
func createServer(zones []zone, config config) *dns.ServeMux {
|
||||
srv := dns.NewServeMux()
|
||||
|
||||
for _, z := range zones {
|
||||
srv.HandleFunc(z.zone, func(w dns.ResponseWriter, r *dns.Msg) {
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
m.Authoritative = true
|
||||
|
||||
// maybe only support one question per query like most servers do it ???
|
||||
for _, q := range r.Question {
|
||||
rr := z.rr[q.Qtype]
|
||||
|
||||
m.Answer = append(m.Answer, rr[q.Name]...)
|
||||
|
||||
// Handle extras
|
||||
switch q.Qtype {
|
||||
case dns.TypeMX:
|
||||
// Resolve MX domains
|
||||
for _, mxRR := range rr[q.Name] {
|
||||
if t, ok := mxRR.(*dns.MX); ok {
|
||||
m.Extra = append(m.Extra, z.rr[dns.TypeA][t.Mx]...)
|
||||
m.Extra = append(m.Extra, z.rr[dns.TypeAAAA][t.Mx]...)
|
||||
}
|
||||
}
|
||||
case dns.TypeA, dns.TypeAAAA:
|
||||
if len(m.Answer) == 0 {
|
||||
// no A or AAAA found. Look for CNAME
|
||||
m.Answer = append(m.Answer, z.rr[dns.TypeCNAME][q.Name]...)
|
||||
if len(m.Answer) != 0 {
|
||||
// Resolve CNAME
|
||||
for _, nameRR := range m.Answer {
|
||||
if t, ok := nameRR.(*dns.CNAME); ok {
|
||||
m.Answer = append(m.Answer, z.rr[q.Qtype][t.Target]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case dns.TypeNS:
|
||||
// Resove NS records
|
||||
for _, nsRR := range rr[q.Name] {
|
||||
if t, ok := nsRR.(*dns.NS); ok {
|
||||
m.Extra = append(m.Extra, z.rr[dns.TypeA][t.Ns]...)
|
||||
m.Extra = append(m.Extra, z.rr[dns.TypeAAAA][t.Ns]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteMsg(m)
|
||||
})
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
func listenAndServer(server *dns.ServeMux) {
|
||||
go func() {
|
||||
if err := dns.ListenAndServe(":"+strconv.Itoa(8053), "udp", server); err != nil {
|
||||
log.Fatalf("Failed to set udp listener %s\n", err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if err := dns.ListenAndServe(":"+strconv.Itoa(8053), "tcp", server); err != nil {
|
||||
log.Fatalf("Failed to set tcp listener %s\n", err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
sig := make(chan os.Signal)
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
||||
s := <-sig
|
||||
log.Printf("Signal (%v) received, stopping\n", s)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func main() {
|
||||
config, err := loadConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load config: %s", err.Error())
|
||||
}
|
||||
|
||||
zones, err := loadZones(config.Zones)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load zones: %s", err.Error())
|
||||
}
|
||||
|
||||
server := createServer(zones, *config)
|
||||
|
||||
listenAndServer(server)
|
||||
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@ -0,0 +1,8 @@
|
||||
module git.kapelle.org/niklas/cool-dns
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/miekg/dns v1.1.35
|
||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
|
||||
)
|
23
go.sum
Normal file
23
go.sum
Normal file
@ -0,0 +1,23 @@
|
||||
github.com/miekg/dns v1.1.35 h1:oTfOaDH+mZkdcgdIjH6yBajRGtIwcwcaR+rt23ZSrJs=
|
||||
github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
|
||||
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M=
|
||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
18
zonefile.txt
Normal file
18
zonefile.txt
Normal file
@ -0,0 +1,18 @@
|
||||
$ORIGIN example.com. ; designates the start of this zone file in the namespace
|
||||
$TTL 3600 ; default expiration time (in seconds) of all RRs without their own TTL value
|
||||
example.com. IN SOA ns.example.com. username.example.com. ( 2020091025 7200 3600 1209600 3600 )
|
||||
example.com. IN NS ns ; ns.example.com is a nameserver for example.com
|
||||
example.com. IN NS ns.somewhere.example. ; ns.somewhere.example is a backup nameserver for example.com
|
||||
example.com. IN MX 10 mail.example.com. ; mail.example.com is the mailserver for example.com
|
||||
@ IN MX 20 mail2.example.com. ; equivalent to above line, "@" represents zone origin
|
||||
@ IN MX 50 mail3 ; equivalent to above line, but using a relative host name
|
||||
example.com. IN A 192.0.2.1 ; IPv4 address for example.com
|
||||
example.com. IN A 192.0.3.1 ; IPv4 address for example.com
|
||||
IN AAAA 2001:db8:10::1 ; IPv6 address for example.com
|
||||
ns IN A 192.0.2.2 ; IPv4 address for ns.example.com
|
||||
IN AAAA 2001:db8:10::2 ; IPv6 address for ns.example.com
|
||||
www IN CNAME example.com. ; www.example.com is an alias for example.com
|
||||
wwwtest IN CNAME www ; wwwtest.example.com is another alias for www.example.com
|
||||
mail IN A 192.0.2.3 ; IPv4 address for mail.example.com
|
||||
mail2 IN A 192.0.2.4 ; IPv4 address for mail2.example.com
|
||||
mail3 IN A 192.0.2.5 ; IPv4 address for mail3.example.com
|
Loading…
Reference in New Issue
Block a user