Added protocol selection to DNS server

This commit is contained in:
Joona Hoikkala 2016-11-28 22:46:24 +02:00
parent d33bda96fa
commit 4c437c0506
No known key found for this signature in database
GPG Key ID: C14AAE0F5ADCB854
6 changed files with 19 additions and 20 deletions

View File

@ -1,6 +1,8 @@
[general]
# dns interface
listen = ":53"
# protocol, "udp", "udp4", "udp6" or "tcp", "tcp4", "tcp6"
protocol = "udp"
# domain name to serve th requests off of
domain = "auth.example.org"
# zone name server

View File

@ -5,7 +5,6 @@ import (
"database/sql/driver"
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/erikstmartin/go-testdb"
"github.com/miekg/dns"
"strings"
@ -107,13 +106,9 @@ func TestParse(t *testing.T) {
Debug: false,
}
var testRR Records
loghook.Reset()
testRR.Parse(testcfg)
if len(loghook.Entries) != 1 {
t.Errorf("Expected exactly one logged line, instead there was %d line(s)", len(loghook.Entries))
}
if loghook.LastEntry().Level != log.ErrorLevel {
t.Error("Expected error level of ERROR from last message")
if !loggerHasEntryWithMessage("Error while adding SOA record") {
t.Errorf("Expected SOA parsing to return error, but did not find one")
}
}

View File

@ -30,7 +30,7 @@ func main() {
defer DB.Close()
// DNS server
startDNS(DNSConf.General.Listen)
startDNS(DNSConf.General.Listen, DNSConf.General.Proto)
// HTTP API
startHTTPAPI()

View File

@ -42,8 +42,7 @@ func TestMain(m *testing.M) {
_ = newDb.Init("sqlite3", ":memory:")
}
DB = newDb
server := startDNS("0.0.0.0:15353")
server := startDNS("0.0.0.0:15353", "udp")
exitval := m.Run()
server.Shutdown()
DB.Close()
@ -84,3 +83,12 @@ func setupTestLogger() {
log.SetOutput(ioutil.Discard)
log.AddHook(loghook)
}
func loggerHasEntryWithMessage(message string) bool {
for _, v := range loghook.Entries {
if v.Message == message {
return true
}
}
return false
}

View File

@ -35,6 +35,7 @@ type authMiddleware struct{}
// Config file general section
type general struct {
Listen string
Proto string `toml:"protocol"`
Domain string
Nsname string
Nsadmin string

13
util.go
View File

@ -7,7 +7,6 @@ import (
"github.com/miekg/dns"
"github.com/satori/go.uuid"
"math/big"
"os"
"regexp"
"strings"
)
@ -72,16 +71,10 @@ func setupLogging(format string, level string) {
// TODO: file logging
}
func startDNS(listen string) *dns.Server {
func startDNS(listen string, proto string) *dns.Server {
// DNS server part
dns.HandleFunc(".", handleRequest)
server := &dns.Server{Addr: listen, Net: "udp"}
go func() {
err := server.ListenAndServe()
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
}()
server := &dns.Server{Addr: listen, Net: proto}
go server.ListenAndServe()
return server
}