allow dns lookup output to be sent to systemctl

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-10 03:22:44 +00:00
parent 71f1aa0cfc
commit d6f15e9e22
2 changed files with 25 additions and 14 deletions

View File

@ -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()
}

View File

@ -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)
}