From 1f68d1b2138f41f05655606c73b2a0539070daa6 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 23 Sep 2020 14:49:57 -0400 Subject: [PATCH] Fix linter warnings and go fmt --- cmd/meshnamed/main.go | 15 ++++++++------- pkg/meshname/config.go | 2 -- pkg/meshname/domain_test.go | 6 +++--- pkg/meshname/server.go | 24 +++++++++++++----------- pkg/meshname/server_test.go | 3 +-- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/cmd/meshnamed/main.go b/cmd/meshnamed/main.go index d025028..a474588 100644 --- a/cmd/meshnamed/main.go +++ b/cmd/meshnamed/main.go @@ -38,7 +38,7 @@ func loadConfig(s *meshname.MeshnameServer, confPath string) error { var ( genconf, subdomain, useconffile, listenAddr, networksconf string - debug bool + debug bool ) func init() { @@ -47,14 +47,13 @@ func init() { flag.StringVar(&useconffile, "useconffile", "", "run daemon with a config file") flag.StringVar(&listenAddr, "listenaddr", "[::1]:53535", "address to listen on") flag.StringVar(&networksconf, "networks", "ygg=200::/7,cjd=fc00::/8,meshname=::/0", "TLD=subnet list separated by comma") - flag.BoolVar(&debug,"debug", false, "enable debug logging") + flag.BoolVar(&debug, "debug", false, "enable debug logging") } func main() { flag.Parse() - var logger *log.Logger - logger = log.New(os.Stdout, "", log.Flags()) + logger := log.New(os.Stdout, "", log.Flags()) logger.EnableLevel("error") logger.EnableLevel("warn") @@ -86,7 +85,9 @@ func main() { } } - s.Start() + if err := s.Start(); err != nil { + logger.Fatal(err) + } logger.Infoln("Listening on:", listenAddr) c := make(chan os.Signal, 1) @@ -96,9 +97,9 @@ func main() { defer s.Stop() for { select { - case _ = <-c: + case <-c: return - case _ = <-r: + case <-r: if useconffile != "" { if err := loadConfig(s, useconffile); err != nil { logger.Errorln(err) diff --git a/pkg/meshname/config.go b/pkg/meshname/config.go index b021dad..0c9b567 100644 --- a/pkg/meshname/config.go +++ b/pkg/meshname/config.go @@ -10,7 +10,6 @@ import ( "github.com/miekg/dns" ) - func GenConf(target, zone string) (string, error) { ip := net.ParseIP(target) if ip == nil { @@ -50,4 +49,3 @@ func ParseZoneConfigMap(zoneConfigMap map[string][]string) (map[string][]dns.RR, } return zoneConfig, nil } - diff --git a/pkg/meshname/domain_test.go b/pkg/meshname/domain_test.go index 26512ef..3495218 100644 --- a/pkg/meshname/domain_test.go +++ b/pkg/meshname/domain_test.go @@ -1,10 +1,10 @@ package meshname -import ( +import ( "bytes" + "fmt" "net" "testing" - "fmt" "github.com/zhoreeq/meshname/pkg/meshname" ) @@ -16,7 +16,7 @@ func TestIPFromDomain(t *testing.T) { if ip, err := meshname.IPFromDomain(&test_subdomain); err != nil { t.Fatal(err) } else if bytes.Compare(ip, test_ip) != 0 { - t.Fatalf("Decoding IP error %s != %s", ip.String(), test_ip.String()) + t.Fatalf("Decoding IP error %s != %s", ip.String(), test_ip.String()) } } diff --git a/pkg/meshname/server.go b/pkg/meshname/server.go index d427a63..f5cfa82 100644 --- a/pkg/meshname/server.go +++ b/pkg/meshname/server.go @@ -30,24 +30,23 @@ func New(log *log.Logger, listenAddr string) *MeshnameServer { dnsClient.Timeout = 5000000000 // increased 5 seconds timeout return &MeshnameServer{ - log: log, + log: log, listenAddr: listenAddr, zoneConfig: make(map[string][]dns.RR), - networks: make(map[string]*net.IPNet), - dnsClient: dnsClient, + networks: make(map[string]*net.IPNet), + dnsClient: dnsClient, } } -func (s *MeshnameServer) Stop() error { +func (s *MeshnameServer) Stop() { s.startedLock.Lock() defer s.startedLock.Unlock() - if s.started == true { - s.dnsServer.Shutdown() + if s.started { + if err := s.dnsServer.Shutdown(); err != nil { + s.log.Debugln(err) + } s.started = false - return nil - } else { - return errors.New("MeshnameServer is not running") } } @@ -55,7 +54,7 @@ func (s *MeshnameServer) Start() error { s.startedLock.Lock() defer s.startedLock.Unlock() - if s.started == false { + if !s.started { s.dnsServer = &dns.Server{Addr: s.listenAddr, Net: "udp"} for tld, subnet := range s.networks { dns.HandleFunc(tld, s.handleRequest) @@ -129,7 +128,10 @@ func (s *MeshnameServer) handleRequest(w dns.ResponseWriter, r *dns.Msg) { } m.Answer = append(m.Answer, resp.Answer...) } - w.WriteMsg(m) + + if err := w.WriteMsg(m); err != nil { + s.log.Debugln("Error writing response:", err) + } } func (s *MeshnameServer) isRemoteLookupAllowed(addr net.Addr) bool { diff --git a/pkg/meshname/server_test.go b/pkg/meshname/server_test.go index 263972b..de85c55 100644 --- a/pkg/meshname/server_test.go +++ b/pkg/meshname/server_test.go @@ -23,7 +23,7 @@ func TestServerLocalDomain(t *testing.T) { exampleConfig := make(map[string][]string) exampleConfig["aiarnf2wpqjxkp6rhivuxbondy"] = append(exampleConfig["aiarnf2wpqjxkp6rhivuxbondy"], - "test.aiarnf2wpqjxkp6rhivuxbondy.meshname. AAAA 201:1697:567c:1375:3fd1:3a2b:4b85:cd1e") + "test.aiarnf2wpqjxkp6rhivuxbondy.meshname. AAAA 201:1697:567c:1375:3fd1:3a2b:4b85:cd1e") if zoneConfig, err := meshname.ParseZoneConfigMap(exampleConfig); err == nil { ts.SetZoneConfig(zoneConfig) @@ -31,7 +31,6 @@ func TestServerLocalDomain(t *testing.T) { t.Fatalf("meshname: Failed to parse Meshname config: %s", err) } - ts.Start() tc := new(dns.Client)