tidy up DNSServer fields
- make Zone, Iface and Upstream public - this is safe and makes sense since all are immutable, and the former two are passed to the constructor and are thus known anyway, and the latter is derived from external information - remove IfaceName - we can just use Iface instead - log Upstream on startup
This commit is contained in:
1 parent
de557a3e03
commit
10f37db9da
2 files changed
+20
-25
No files matched your search
+19
-25
@@ -18,8 +18,7 @@ const (
|
|||||||
DefaultCacheLen = 8192 // default cache capacity
|
DefaultCacheLen = 8192 // default cache capacity
|
||||||
DefaultResolvWorkers = 8 // default number of resolution workers
|
DefaultResolvWorkers = 8 // default number of resolution workers
|
||||||
DefaultTimeout = 5 // default timeout for DNS resolutions
|
DefaultTimeout = 5 // default timeout for DNS resolutions
|
||||||
DefaultIfaceName = "default interface"
|
DefaultResolvTries = 3 // max # of times a worker tries to resolve a query
|
||||||
DefaultResolvTries = 3 // max # of times a worker tries to resolve a query
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type DNSServerConfig struct {
|
type DNSServerConfig struct {
|
||||||
@@ -68,32 +67,30 @@ func (proto dnsProtocol) GetNewClient(bufsize int) *dns.Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DNSServer struct {
|
type DNSServer struct {
|
||||||
|
Zone Zone
|
||||||
|
Iface *net.Interface
|
||||||
|
Upstream *dns.ClientConfig
|
||||||
udpSrv *dns.Server
|
udpSrv *dns.Server
|
||||||
tcpSrv *dns.Server
|
tcpSrv *dns.Server
|
||||||
mdnsCli *MDNSClient
|
mdnsCli *MDNSClient
|
||||||
mdnsSrv *MDNSServer
|
mdnsSrv *MDNSServer
|
||||||
cache *Cache
|
cache *Cache
|
||||||
zone Zone
|
|
||||||
iface *net.Interface
|
|
||||||
upstream *dns.ClientConfig
|
|
||||||
timeout int
|
timeout int
|
||||||
udpBuf int
|
udpBuf int
|
||||||
listenersWg *sync.WaitGroup
|
listenersWg *sync.WaitGroup
|
||||||
|
|
||||||
Domain string // the local domain
|
Domain string // the local domain
|
||||||
IfaceName string // the interface where mDNS is working on
|
|
||||||
ListenAddr string // the address the server is listening at
|
ListenAddr string // the address the server is listening at
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new DNS server
|
// Creates a new DNS server
|
||||||
func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *DNSServer, err error) {
|
func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *DNSServer, err error) {
|
||||||
s = &DNSServer{
|
s = &DNSServer{
|
||||||
zone: zone,
|
Zone: zone,
|
||||||
iface: iface,
|
Iface: iface,
|
||||||
listenersWg: new(sync.WaitGroup),
|
listenersWg: new(sync.WaitGroup),
|
||||||
|
|
||||||
Domain: DefaultLocalDomain,
|
Domain: DefaultLocalDomain,
|
||||||
IfaceName: DefaultIfaceName,
|
|
||||||
ListenAddr: fmt.Sprintf(":%d", DefaultServerPort),
|
ListenAddr: fmt.Sprintf(":%d", DefaultServerPort),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,13 +102,13 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
|
|||||||
s.Domain = config.LocalDomain
|
s.Domain = config.LocalDomain
|
||||||
}
|
}
|
||||||
if config.UpstreamCfg != nil {
|
if config.UpstreamCfg != nil {
|
||||||
s.upstream = config.UpstreamCfg
|
s.Upstream = config.UpstreamCfg
|
||||||
} else {
|
} else {
|
||||||
cfgFile := DefaultCLICfgFile
|
cfgFile := DefaultCLICfgFile
|
||||||
if len(config.UpstreamCfgFile) > 0 {
|
if len(config.UpstreamCfgFile) > 0 {
|
||||||
cfgFile = config.UpstreamCfgFile
|
cfgFile = config.UpstreamCfgFile
|
||||||
}
|
}
|
||||||
if s.upstream, err = dns.ClientConfigFromFile(cfgFile); err != nil {
|
if s.Upstream, err = dns.ClientConfigFromFile(cfgFile); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,13 +122,10 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.mdnsSrv, err = NewMDNSServer(s.zone)
|
s.mdnsSrv, err = NewMDNSServer(s.Zone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if iface != nil {
|
|
||||||
s.IfaceName = iface.Name
|
|
||||||
}
|
|
||||||
cacheLen := DefaultCacheLen
|
cacheLen := DefaultCacheLen
|
||||||
if config.CacheLen > 0 {
|
if config.CacheLen > 0 {
|
||||||
cacheLen = config.CacheLen
|
cacheLen = config.CacheLen
|
||||||
@@ -146,8 +140,8 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
|
|||||||
// (we use the same protocol for asking upstream servers)
|
// (we use the same protocol for asking upstream servers)
|
||||||
mux := func(proto dnsProtocol) *dns.ServeMux {
|
mux := func(proto dnsProtocol) *dns.ServeMux {
|
||||||
m := dns.NewServeMux()
|
m := dns.NewServeMux()
|
||||||
m.HandleFunc(s.Domain, s.queryHandler([]Lookup{s.zone, s.mdnsCli}, proto))
|
m.HandleFunc(s.Domain, s.queryHandler([]Lookup{s.Zone, s.mdnsCli}, proto))
|
||||||
m.HandleFunc(RDNSDomain, s.rdnsHandler([]Lookup{s.zone, s.mdnsCli}, proto))
|
m.HandleFunc(RDNSDomain, s.rdnsHandler([]Lookup{s.Zone, s.mdnsCli}, proto))
|
||||||
m.HandleFunc(".", s.notUsHandler(proto))
|
m.HandleFunc(".", s.notUsHandler(proto))
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
@@ -160,10 +154,10 @@ func NewDNSServer(config DNSServerConfig, zone Zone, iface *net.Interface) (s *D
|
|||||||
|
|
||||||
// Start the DNS server
|
// Start the DNS server
|
||||||
func (s *DNSServer) Start() error {
|
func (s *DNSServer) Start() error {
|
||||||
Info.Printf("Using mDNS on %s", s.IfaceName)
|
Info.Printf("Using mDNS on %v", s.Iface)
|
||||||
err := s.mdnsCli.Start(s.iface)
|
err := s.mdnsCli.Start(s.Iface)
|
||||||
CheckFatal(err)
|
CheckFatal(err)
|
||||||
err = s.mdnsSrv.Start(s.iface, s.Domain)
|
err = s.mdnsSrv.Start(s.Iface, s.Domain)
|
||||||
CheckFatal(err)
|
CheckFatal(err)
|
||||||
|
|
||||||
s.listenersWg.Add(2)
|
s.listenersWg.Add(2)
|
||||||
@@ -198,9 +192,9 @@ func (s *DNSServer) Status() string {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
fmt.Fprintln(&buf, "Local domain", s.Domain)
|
fmt.Fprintln(&buf, "Local domain", s.Domain)
|
||||||
fmt.Fprintln(&buf, "Listen address", s.ListenAddr)
|
fmt.Fprintln(&buf, "Listen address", s.ListenAddr)
|
||||||
fmt.Fprintln(&buf, "mDNS interface", s.iface)
|
fmt.Fprintln(&buf, "mDNS interface", s.Iface)
|
||||||
fmt.Fprintln(&buf, "Fallback DNS config", s.upstream)
|
fmt.Fprintln(&buf, "Fallback DNS config", s.Upstream)
|
||||||
fmt.Fprintf(&buf, "Zone database:\n%s", s.zone)
|
fmt.Fprintf(&buf, "Zone database:\n%s", s.Zone)
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,8 +328,8 @@ func (s *DNSServer) notUsHandler(proto dnsProtocol) dns.HandlerFunc {
|
|||||||
rcopy.SetEdns0(uint16(maxLen), false)
|
rcopy.SetEdns0(uint16(maxLen), false)
|
||||||
|
|
||||||
Debug.Printf("[dns msgid %d] Fallback query: %+v [%s, max:%d bytes]", rcopy.MsgHdr.Id, q, proto, maxLen)
|
Debug.Printf("[dns msgid %d] Fallback query: %+v [%s, max:%d bytes]", rcopy.MsgHdr.Id, q, proto, maxLen)
|
||||||
for _, server := range s.upstream.Servers {
|
for _, server := range s.Upstream.Servers {
|
||||||
reply, _, err := dnsClient.Exchange(rcopy, fmt.Sprintf("%s:%s", server, s.upstream.Port))
|
reply, _, err := dnsClient.Exchange(rcopy, fmt.Sprintf("%s:%s", server, s.Upstream.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Debug.Printf("[dns msgid %d] Network error trying %s (%s)",
|
Debug.Printf("[dns msgid %d] Network error trying %s (%s)",
|
||||||
r.MsgHdr.Id, server, err)
|
r.MsgHdr.Id, server, err)
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Fatal("Failed to initialize the WeaveDNS server", err)
|
Error.Fatal("Failed to initialize the WeaveDNS server", err)
|
||||||
}
|
}
|
||||||
|
Info.Println("Upstream", srv.Upstream)
|
||||||
|
|
||||||
Debug.Printf("Starting the signals handler")
|
Debug.Printf("Starting the signals handler")
|
||||||
go handleSignals(srv)
|
go handleSignals(srv)
|
||||||
|
|||||||
Reference in new issue
Block a user