Skip to content

Commit

Permalink
feat: add response header to pretty output
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Aug 16, 2023
1 parent 4d8ed19 commit 3a54dff
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var colors = map[string]string{

// color returns a color formatted string
func color(color string, args ...interface{}) string {
if _, ok := colors[color]; !ok {
panic("invalid color: " + color)
}

if opts.Color {
return fmt.Sprintf(colors[color], fmt.Sprint(args...))
} else {
Expand Down Expand Up @@ -165,6 +169,43 @@ func display(replies []*dns.Msg, server string, queryTime time.Duration, out io.
color("teal", queryTime.Round(100*time.Microsecond)),
color("magenta", time.Now().Format("15:04:05 01-02-2006 MST")),
)

flags := ""
if reply.MsgHdr.Response {
flags = "qr"
}
if reply.MsgHdr.Authoritative {
flags = "aa"
}
if reply.MsgHdr.Truncated {
flags = "tc"
}
if reply.MsgHdr.RecursionDesired {
flags = "rd"
}
if reply.MsgHdr.RecursionAvailable {
flags = "ra"
}
if reply.MsgHdr.Zero {
flags = "z"
}
if reply.MsgHdr.AuthenticatedData {
flags = "ad"
}
if reply.MsgHdr.CheckingDisabled {
flags = "cd"
}

mustWritef(out, "Opcode: %s Status: %s ID %s: Flags: %s (%s Query %s Ans %s Auth %s Add)\n",
color("magenta", dns.OpcodeToString[reply.MsgHdr.Opcode]),
color("teal", fmt.Sprintf("%s", dns.RcodeToString[reply.MsgHdr.Rcode])),
color("green", fmt.Sprintf("%d", reply.MsgHdr.Id)),
color("purple", flags),
color("purple", fmt.Sprintf("%d", len(reply.Question))),
color("green", fmt.Sprintf("%d", len(reply.Answer))),
color("teal", fmt.Sprintf("%d", len(reply.Ns))),
color("magenta", fmt.Sprintf("%d", len(reply.Extra))),
)
}
case "raw":
s := reply.MsgHdr.String() + " "
Expand Down

0 comments on commit 3a54dff

Please sign in to comment.