From 0336002980da0426eec66158fc4a60b08dbd532e Mon Sep 17 00:00:00 2001 From: Niklas Date: Sun, 31 Jan 2021 22:31:08 +0100 Subject: [PATCH] moved files --- cmd/cooldns.go | 24 +++++++ blacklist.go => internal/blacklist.go | 2 +- coolDns.go => internal/cooldns.go | 22 +------ lego.go => internal/lego.go | 2 +- rr_test.go | 91 --------------------------- test/rrConfig.yaml | 5 -- test/zonefile.txt | 20 ------ 7 files changed, 29 insertions(+), 137 deletions(-) create mode 100644 cmd/cooldns.go rename blacklist.go => internal/blacklist.go (99%) rename coolDns.go => internal/cooldns.go (96%) rename lego.go => internal/lego.go (99%) delete mode 100644 rr_test.go delete mode 100644 test/rrConfig.yaml delete mode 100644 test/zonefile.txt diff --git a/cmd/cooldns.go b/cmd/cooldns.go new file mode 100644 index 0000000..9f3a0c9 --- /dev/null +++ b/cmd/cooldns.go @@ -0,0 +1,24 @@ +package main + +import ( + "flag" + "log" + "os" + "os/signal" + "syscall" + + cooldns "git.kapelle.org/niklas/cool-dns/internal" +) + +func main() { + configPath := flag.String("c", "/etc/cool-dns/config.yaml", "path to the config file") + flag.Parse() + + cooldns.Start(*configPath) + + 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) +} diff --git a/blacklist.go b/internal/blacklist.go similarity index 99% rename from blacklist.go rename to internal/blacklist.go index 4a3c49d..49ae789 100644 --- a/blacklist.go +++ b/internal/blacklist.go @@ -1,4 +1,4 @@ -package main +package cooldns import ( "errors" diff --git a/coolDns.go b/internal/cooldns.go similarity index 96% rename from coolDns.go rename to internal/cooldns.go index b891d69..1c8ae35 100644 --- a/coolDns.go +++ b/internal/cooldns.go @@ -1,15 +1,12 @@ -package main +package cooldns import ( - "flag" "io/ioutil" "log" "net" "os" - "os/signal" "path/filepath" "strings" - "syscall" "github.com/miekg/dns" "gopkg.in/yaml.v3" @@ -76,7 +73,8 @@ var anyRecordTypes = []uint16{ dns.TypeCAA, } -func start(configPath string) { +// Start starts cooldns +func Start(configPath string) { config, err := loadConfig(configPath) if err != nil { log.Fatalf("Failed to load config: %s\n", err.Error()) @@ -419,17 +417,3 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) { w.WriteMsg(m) } - -func main() { - - configPath := flag.String("c", "/etc/cool-dns/config.yaml", "path to the config file") - flag.Parse() - - start(*configPath) - - 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) -} diff --git a/lego.go b/internal/lego.go similarity index 99% rename from lego.go rename to internal/lego.go index aa0525e..6f42530 100644 --- a/lego.go +++ b/internal/lego.go @@ -1,4 +1,4 @@ -package main +package cooldns import ( "encoding/json" diff --git a/rr_test.go b/rr_test.go deleted file mode 100644 index 7c105f7..0000000 --- a/rr_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "net" - "testing" - - "github.com/miekg/dns" -) - -func init() { - start("test/rrConfig.yaml") -} - -// Helper - -func request(name string, rrType uint16) (*dns.Msg, error) { - m := new(dns.Msg) - m.SetQuestion(dns.Fqdn(name), rrType) - return dns.Exchange(m, "127.0.0.1:8053") -} - -func containsA(haystack []dns.RR, name, ip string) bool { - searchIP := net.ParseIP(ip) - for _, v := range haystack { - if v.Header().Name == dns.Fqdn(name) && v.Header().Rrtype == dns.TypeA { - if t, ok := v.(*dns.A); ok { - if t.A.Equal(searchIP) { - return true - } - } - } - } - - return false -} - -func containsAAAA(haystack []dns.RR, name, ip string) bool { - searchIP := net.ParseIP(ip) - for _, v := range haystack { - if v.Header().Name == dns.Fqdn(name) && v.Header().Rrtype == dns.TypeAAAA { - if t, ok := v.(*dns.AAAA); ok { - if t.AAAA.Equal(searchIP) { - return true - } - } - } - } - - return false -} - -func TestNormalA(t *testing.T) { - res, err := request("example.com", dns.TypeA) - if err != nil { - t.Error(err) - } - - if !containsA(res.Answer, "example.com", "1.2.3.1") || !containsA(res.Answer, "example.com", "1.2.3.2") { - t.FailNow() - } -} - -func TestNormalAAAA(t *testing.T) { - res, err := request("example.com", dns.TypeAAAA) - if err != nil { - t.Error(err) - } - - if !containsAAAA(res.Answer, "example.com", "2001:db8:10::1") { - t.FailNow() - } -} - -func TestNormalSOA(t *testing.T) { - res, err := request("example.com", dns.TypeSOA) - if err != nil { - t.Error(err) - } - - if len(res.Answer) != 1 { - t.Fatalf("Should only be 1 SOA got %d", len(res.Answer)) - } - - if soa, ok := res.Answer[0].(*dns.SOA); ok { - if soa.Ns != "ns.example.com." { - t.Fatal("Wrong SOA rr") - } - } else { - t.Fatal("Answer is not a SOA rr") - } -} diff --git a/test/rrConfig.yaml b/test/rrConfig.yaml deleted file mode 100644 index a463153..0000000 --- a/test/rrConfig.yaml +++ /dev/null @@ -1,5 +0,0 @@ -zones: -- zone: example.com. - file: zonefile.txt - -address: 0.0.0.0:8053 diff --git a/test/zonefile.txt b/test/zonefile.txt deleted file mode 100644 index 1f1a3ab..0000000 --- a/test/zonefile.txt +++ /dev/null @@ -1,20 +0,0 @@ -$ORIGIN example.com. -$TTL 3600 -example.com. IN SOA ns.example.com. username.example.com. ( 2020091025 7200 3600 1209600 3600 ) -example.com. IN NS ns -example.com. IN NS ns.somewhere.example. -example.com. IN MX 10 mail.example.com. -@ IN MX 20 mail2.example.com. -@ IN MX 50 mail3 -example.com. IN A 1.2.3.1 -example.com. IN A 1.2.3.2 - IN AAAA 2001:db8:10::1 -ns IN A 1.2.3.3 - IN AAAA 2001:db8:10::2 -www IN CNAME example.com. -wwwtest IN CNAME www -mail IN A 1.2.3.4 -mail2 IN A 1.2.3.5 -mail3 IN A 1.2.3.6 -*.www IN A 1.2.3.7 -a.www IN A 1.2.3.8 \ No newline at end of file