From d6f15e9e22dbd065e7a5882ea80558f132b27d3e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 May 2019 03:22:44 +0000 Subject: [PATCH] allow dns lookup output to be sent to systemctl Signed-off-by: Jeff Carr --- dnstrace.go | 34 ++++++++++++++++++++-------------- server/server.go | 5 +++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/dnstrace.go b/dnstrace.go index 7117fd0..9c09392 100644 --- a/dnstrace.go +++ b/dnstrace.go @@ -3,7 +3,6 @@ package dnssecsocket // inspired from github.com/rs/dnstrace/main.go import "fmt" -import "log" import "net" import "os" import "strings" @@ -12,6 +11,9 @@ import "time" import "github.com/miekg/dns" import "github.com/rs/dnstrace/client" +import log "github.com/sirupsen/logrus" +import "github.com/wercker/journalhook" + // import "github.com/davecgh/go-spew/spew" const ( @@ -71,13 +73,13 @@ func Dnstrace(hostname string, qtypestr string) dns.RR { qname := m.Question[0].Name qtype := dns.TypeToString[m.Question[0].Qtype] if i > 1 { - fmt.Println() + log.Println() } - fmt.Printf("%d - query %s %s", i, qtype, qname) + log.Printf("%d - query %s %s", i, qtype, qname) if r != nil { - fmt.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1)) + log.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1)) } - fmt.Println() + log.Println() for _, pr := range rs { ln := 0 if pr.Msg != nil { @@ -90,15 +92,15 @@ func Dnstrace(hostname string, qtypestr string) dns.RR { } else if pr.Server.LookupRTT > 0 { lrtt = fmt.Sprintf("%.2fms", float64(pr.Server.LookupRTT)/float64(time.Millisecond)) } - fmt.Printf(col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr) + log.Printf(col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr) if pr.Err != nil { err := pr.Err if oerr, ok := err.(*net.OpError); ok { err = oerr.Err } - fmt.Printf(": %v", col(err, cRed)) + log.Printf(": %v", col(err, cRed)) } - fmt.Print("\n") + log.Print("\n") } switch rtype { @@ -118,26 +120,26 @@ func Dnstrace(hostname string, qtypestr string) dns.RR { } else { glue = col("no glue", cYellow) } - fmt.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue) + log.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue) } case client.ResponseTypeCNAME: for _, rr := range r.Answer { - fmt.Println(rr) + log.Println(rr) } } }, FollowingCNAME: func(domain, target string) { - fmt.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target) + log.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target) }, } r, rtt, err := c.RecursiveQuery(m, t) if err != nil { - fmt.Printf(col("*** error: %v\n", cRed), err) + log.Printf(col("*** error: %v\n", cRed), err) os.Exit(1) } - fmt.Println() - fmt.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt) + log.Println() + log.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt) for _, rr := range r.Answer { log.Println(rr) } @@ -153,3 +155,7 @@ func ResolveIPv6hostname(hostname string) *net.TCPAddr { localTCPAddr, _ := net.ResolveTCPAddr("tcp", aaaa) return localTCPAddr } + +func UseJournalctl() { + journalhook.Enable() +} diff --git a/server/server.go b/server/server.go index 10e2eae..9340157 100644 --- a/server/server.go +++ b/server/server.go @@ -39,6 +39,9 @@ go func(c net.Conn) { */ func main() { + // redirect all this output to systemd + dnssecsocket.UseJournalctl() + hostname := fqdn.Get() log.Println("FQDN hostname is", hostname) @@ -75,6 +78,8 @@ func main() { return } + log.Println("Accepted new connection from: " + conn.RemoteAddr().String()) + // Handle connections in a new goroutine. go dnssecsocket.HandleConnection(conn) }