git » dnss » commit 67cabb9

Better logging

author Alberto Bertogli
2015-09-06 23:51:31 UTC
committer Alberto Bertogli
2015-09-06 23:51:31 UTC
parent 9c963cb3433c982ef6290eeba02df8df0d132ee4

Better logging

dnss.go +29 -14
dnstogrpc/dnstogrpc.go +12 -9
grpctodns/grpctodns.go +10 -11

diff --git a/dnss.go b/dnss.go
index 311508a..ba9427d 100644
--- a/dnss.go
+++ b/dnss.go
@@ -6,15 +6,20 @@ import (
 
 	"blitiri.com.ar/go/dnss/dnstogrpc"
 	"blitiri.com.ar/go/dnss/grpctodns"
+	"blitiri.com.ar/go/l"
 	"blitiri.com.ar/go/profile"
 )
 
 var (
+	enableDNStoGRPC = flag.Bool("enable_dns_to_grpc", false,
+		"enable DNS-to-GRPC server")
 	dnsaddr = flag.String("dnsaddr", ":53",
 		"address to listen on for DNS")
 	grpcupstream = flag.String("grpcupstream", "localhost:9953",
 		"address of the upstream GRPC server")
 
+	enableGRPCtoDNS = flag.Bool("enable_grpc_to_dns", false,
+		"enable GRPC-to-DNS server")
 	grpcaddr = flag.String("grpcaddr", ":9953",
 		"address to listen on for GRPC")
 	dnsupstream = flag.String("dnsupstream", "8.8.8.8:53",
@@ -25,27 +30,37 @@ func main() {
 	flag.Parse()
 
 	profile.Init()
+	l.Init("dnss")
+
+	if !*enableDNStoGRPC && !*enableGRPCtoDNS {
+		l.Fatalf(
+			"ERROR: pass --enable_dns_to_grpc or --enable_grpc_to_dns\n")
+	}
 
 	var wg sync.WaitGroup
 
 	// DNS to GRPC.
-	dtg := dnstogrpc.New(*dnsaddr, *grpcupstream)
-	wg.Add(1)
-	go func() {
-		defer wg.Done()
-		dtg.ListenAndServe()
-	}()
+	if *enableDNStoGRPC {
+		dtg := dnstogrpc.New(*dnsaddr, *grpcupstream)
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			dtg.ListenAndServe()
+		}()
+	}
 
 	// GRPC to DNS.
-	gtd := &grpctodns.Server{
-		Addr:     *grpcaddr,
-		Upstream: *dnsupstream,
+	if *enableGRPCtoDNS {
+		gtd := &grpctodns.Server{
+			Addr:     *grpcaddr,
+			Upstream: *dnsupstream,
+		}
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			gtd.ListenAndServe()
+		}()
 	}
-	wg.Add(1)
-	go func() {
-		defer wg.Done()
-		gtd.ListenAndServe()
-	}()
 
 	wg.Wait()
 }
diff --git a/dnstogrpc/dnstogrpc.go b/dnstogrpc/dnstogrpc.go
index 39c6a8f..3f06e18 100644
--- a/dnstogrpc/dnstogrpc.go
+++ b/dnstogrpc/dnstogrpc.go
@@ -8,6 +8,7 @@ import (
 
 	pb "blitiri.com.ar/go/dnss/proto"
 	"blitiri.com.ar/go/dnss/util"
+	"blitiri.com.ar/go/l"
 	"github.com/miekg/dns"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
@@ -60,29 +61,29 @@ func New(addr, upstream string) *Server {
 	}
 }
 
-func l(w dns.ResponseWriter, r *dns.Msg) string {
+func p(w dns.ResponseWriter, r *dns.Msg) string {
 	return fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
 }
 
 func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
-	fmt.Printf("DNS  %v %v\n", l(w, r), util.QuestionsToString(r.Question))
+	l.Printf("DNS  %v %v\n", p(w, r), util.QuestionsToString(r.Question))
 
 	// TODO: we should create our own IDs, in case different users pick the
 	// same id and we pass that upstream.
 
 	from_up, err := s.client.Query(r)
 	if err != nil {
-		fmt.Printf("DNS  %v  ERR: %v\n", l(w, r), err)
-		fmt.Printf("DNS  %v  UP: %v\n", l(w, r), from_up)
+		l.Printf("DNS  %v  ERR: %v\n", p(w, r), err)
+		l.Printf("DNS  %v  UP: %v\n", p(w, r), from_up)
 	}
 
 	if from_up != nil {
 		if from_up.Rcode != dns.RcodeSuccess {
 			rcode := dns.RcodeToString[from_up.Rcode]
-			fmt.Printf("DNS  %v  !->  %v\n", l(w, r), rcode)
+			l.Printf("DNS  %v  !->  %v\n", p(w, r), rcode)
 		}
 		for _, rr := range from_up.Answer {
-			fmt.Printf("DNS  %v  ->  %v\n", l(w, r), rr)
+			l.Printf("DNS  %v  ->  %v\n", p(w, r), rr)
 		}
 		w.WriteMsg(from_up)
 	}
@@ -92,23 +93,25 @@ func (s *Server) ListenAndServe() {
 	err := s.client.Connect()
 	if err != nil {
 		// TODO: handle errors and reconnect.
-		fmt.Printf("Error creating GRPC client: %v\n", err)
+		l.Printf("Error creating GRPC client: %v\n", err)
 		return
 	}
 
+	l.Printf("DNS listening on %s\n", s.Addr)
+
 	var wg sync.WaitGroup
 	wg.Add(1)
 	go func() {
 		defer wg.Done()
 		err := dns.ListenAndServe(s.Addr, "udp", dns.HandlerFunc(s.Handler))
-		fmt.Printf("Exiting UDP: %v\n", err)
+		l.Printf("Exiting UDP: %v\n", err)
 	}()
 
 	wg.Add(1)
 	go func() {
 		defer wg.Done()
 		err := dns.ListenAndServe(s.Addr, "tcp", dns.HandlerFunc(s.Handler))
-		fmt.Printf("Exiting TCP: %v\n", err)
+		l.Printf("Exiting TCP: %v\n", err)
 	}()
 
 	wg.Wait()
diff --git a/grpctodns/grpctodns.go b/grpctodns/grpctodns.go
index cc05023..5178089 100644
--- a/grpctodns/grpctodns.go
+++ b/grpctodns/grpctodns.go
@@ -9,6 +9,7 @@ import (
 
 	pb "blitiri.com.ar/go/dnss/proto"
 	"blitiri.com.ar/go/dnss/util"
+	"blitiri.com.ar/go/l"
 	"github.com/miekg/dns"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
@@ -32,10 +33,6 @@ func rrsToString(rrs []dns.RR) string {
 
 }
 
-func l(w dns.ResponseWriter, r *dns.Msg) string {
-	return fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
-}
-
 type Server struct {
 	Addr     string
 	Upstream string
@@ -48,14 +45,14 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 		return nil, err
 	}
 
-	fmt.Printf("GRPC %v\n", util.QuestionsToString(r.Question))
+	l.Printf("GRPC %v\n", util.QuestionsToString(r.Question))
 
 	// TODO: we should create our own IDs, in case different users pick the
 	// same id and we pass that upstream.
 	from_up, err := dns.Exchange(r, s.Upstream)
 	if err != nil {
-		fmt.Printf("GRPC   ERR: %v\n", err)
-		fmt.Printf("GRPC   UP: %v\n", from_up)
+		l.Printf("GRPC   ERR: %v\n", err)
+		l.Printf("GRPC   UP: %v\n", from_up)
 		return nil, err
 	}
 
@@ -65,15 +62,15 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 
 	if from_up.Rcode != dns.RcodeSuccess {
 		rcode := dns.RcodeToString[from_up.Rcode]
-		fmt.Printf("GPRC   !->  %v\n", rcode)
+		l.Printf("GPRC   !->  %v\n", rcode)
 	}
 	for _, rr := range from_up.Answer {
-		fmt.Printf("GRPC   ->  %v\n", rr)
+		l.Printf("GRPC   ->  %v\n", rr)
 	}
 
 	buf, err := from_up.Pack()
 	if err != nil {
-		fmt.Printf("GRPC   ERR: %v\n", err)
+		l.Printf("GRPC   ERR: %v\n", err)
 		return nil, err
 	}
 
@@ -83,7 +80,7 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 func (s *Server) ListenAndServe() {
 	lis, err := net.Listen("tcp", s.Addr)
 	if err != nil {
-		fmt.Printf("failed to listen: %v", err)
+		l.Printf("failed to listen: %v", err)
 		return
 	}
 
@@ -91,5 +88,7 @@ func (s *Server) ListenAndServe() {
 
 	grpcServer := grpc.NewServer()
 	pb.RegisterDNSServiceServer(grpcServer, s)
+
+	l.Printf("GRPC listening on %s\n", s.Addr)
 	grpcServer.Serve(lis)
 }