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] [general]
# dns interface # dns interface
listen = ":53" listen = ":53"
# protocol, "udp", "udp4", "udp6" or "tcp", "tcp4", "tcp6"
protocol = "udp"
# domain name to serve th requests off of # domain name to serve th requests off of
domain = "auth.example.org" domain = "auth.example.org"
# zone name server # zone name server

View File

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

View File

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

View File

@ -42,8 +42,7 @@ func TestMain(m *testing.M) {
_ = newDb.Init("sqlite3", ":memory:") _ = newDb.Init("sqlite3", ":memory:")
} }
DB = newDb DB = newDb
server := startDNS("0.0.0.0:15353", "udp")
server := startDNS("0.0.0.0:15353")
exitval := m.Run() exitval := m.Run()
server.Shutdown() server.Shutdown()
DB.Close() DB.Close()
@ -84,3 +83,12 @@ func setupTestLogger() {
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
log.AddHook(loghook) 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 // Config file general section
type general struct { type general struct {
Listen string Listen string
Proto string `toml:"protocol"`
Domain string Domain string
Nsname string Nsname string
Nsadmin string Nsadmin string

13
util.go
View File

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