Skip to content

Commit

Permalink
feat: improve the output format
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed May 30, 2024
1 parent 4739707 commit dec7032
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
58 changes: 56 additions & 2 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package formatter

import (
"bytes"
"fmt"
"strconv"
"strings"
"time"

"github.com/chenjiandongx/dnstrack/codec"
Expand All @@ -19,11 +23,11 @@ type Formatter interface {
Format(msg MessageWrap) (string, bool)
}

func New(format, server, typ string) Formatter {
func New(format, server, typ string, n int) Formatter {
f := NewFilter(server, typ)
switch format {
case "question", "q":
return questionFormatter{f}
return questionFormatter{f, n}
case "json", "j":
return jsonFormatter{f}
case "yaml", "y":
Expand All @@ -32,3 +36,53 @@ func New(format, server, typ string) Formatter {
return verboseFormatter{f}
}
}

func formatDuration(d time.Duration) string {
s := d.String()
units := []string{"ms", "µs", "ns", "h", "m", "s"}
for _, unit := range units {
if strings.HasSuffix(s, unit) {
v := s[:len(s)-len(unit)]
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return s
}
fs := fmt.Sprintf("%.3f", f)
switch len(fs) {
case 6:
fs = pad(1) + fs
case 5:
fs = pad(2) + fs
case 4:
fs = pad(3) + fs
}
return pad(2-len(unit)) + fs + unit
}
}
return s
}

func formatIface(s string, maxIfaceLen int) string {
n := maxIfaceLen - len(s)
return pad(n) + s
}

func formatServer(s string) string {
const maxServerLen = 18 // 172.172.172.172:53
n := maxServerLen - len(s)
return pad(n) + s
}

func formatType(s string) string {
const maxTypeLen = 5 // CNAME
n := maxTypeLen - len(s)
return pad(n) + s
}

func pad(n int) string {
buf := &bytes.Buffer{}
for i := 0; i < n; i++ {
buf.WriteString(" ")
}
return buf.String()
}
10 changes: 9 additions & 1 deletion formatter/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type questionFormatter struct {
f *Filter
n int
}

var _ Formatter = (*questionFormatter)(nil)
Expand All @@ -17,6 +18,13 @@ func (qf questionFormatter) Format(msg MessageWrap) (string, bool) {
}

q := msg.Msg.QuestionSec
s := fmt.Sprintf("%s\t<%s>@%s\t%s\t%s\t%s", msg.When.Format(time.RFC3339), msg.Device, msg.Server, q.Type, msg.Duration, q.Name)
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s",
msg.When.Format(time.RFC3339),
formatIface(msg.Device, qf.n),
formatServer(msg.Server),
formatType(q.Type),
formatDuration(msg.Duration),
q.Name,
)
return s, true
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.1.0"
const version = "v0.2.0"

func NewApp() *cobra.Command {
defaultOpts := DefaultOptions()
Expand Down
22 changes: 12 additions & 10 deletions pacp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@ type pcapHandler struct {
}

type PcapClient struct {
ctx context.Context
cancel context.CancelFunc
opt Options
handlers []*pcapHandler
common *CommonClient
ctx context.Context
cancel context.CancelFunc
opt Options
handlers []*pcapHandler
common *CommonClient
maxIfaceLen int
}

func NewPcapClient(opt Options) (*PcapClient, error) {
client := &PcapClient{
opt: opt,
common: NewCommonClient(formatter.New(opt.Format, opt.Server, opt.Type)),
}

client := &PcapClient{opt: opt}
client.ctx, client.cancel = context.WithCancel(context.Background())
if err := client.getAvailableDevices(); err != nil {
return nil, err
}

client.common = NewCommonClient(formatter.New(opt.Format, opt.Server, opt.Type, client.maxIfaceLen))
for _, handler := range client.handlers {
go client.listen(handler)
}
Expand All @@ -63,6 +61,10 @@ func (c *PcapClient) getAvailableDevices() error {
if err = c.setBPFFilter(handler, bpfFilter); err != nil {
return errors.Wrapf(err, "set bpf-filter on device(%s) failed", device.Name)
}

if c.maxIfaceLen < len(device.Name) {
c.maxIfaceLen = len(device.Name)
}
c.handlers = append(c.handlers, &pcapHandler{device: device.Name, handle: handler})
}

Expand Down
17 changes: 9 additions & 8 deletions pcap_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ type pcapHandler struct {
}

type PcapClient struct {
opt Options
handlers []*pcapHandler
common *CommonClient
opt Options
handlers []*pcapHandler
common *CommonClient
maxIfaceLen int
}

func NewPcapClient(opt Options) (*PcapClient, error) {
client := &PcapClient{
opt: opt,
common: NewCommonClient(formatter.New(opt.Format, opt.Server, opt.Type)),
}

client := &PcapClient{opt: opt}
if err := client.getAvailableDevices(); err != nil {
return nil, err
}

client.common = NewCommonClient(formatter.New(opt.Format, opt.Server, opt.Type, client.maxIfaceLen))
for _, handler := range client.handlers {
go client.listen(handler)
}
Expand All @@ -53,6 +51,9 @@ func (c *PcapClient) getAvailableDevices() error {
return errors.Wrapf(err, "get device(%s) name failed", device.Name)
}

if c.maxIfaceLen < len(device.Name) {
c.maxIfaceLen = len(device.Name)
}
c.handlers = append(c.handlers, &pcapHandler{
device: device.Name,
handle: handler,
Expand Down

0 comments on commit dec7032

Please sign in to comment.