fix: showing meta fields when available in net.show (ref #66)

This commit is contained in:
evilsocket 2018-02-22 19:08:58 +01:00
commit d6fe8fc663
2 changed files with 51 additions and 30 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"sort" "sort"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
@ -47,7 +48,7 @@ func rankByProtoHits(protos map[string]uint64) (ProtoPairList, uint64) {
return pl, max return pl, max
} }
func (d *Discovery) getRow(e *network.Endpoint) []string { func (d *Discovery) getRow(e *network.Endpoint, withMeta bool) []string {
sinceStarted := time.Since(d.Session.StartedAt) sinceStarted := time.Since(d.Session.StartedAt)
sinceFirstSeen := time.Since(e.FirstSeen) sinceFirstSeen := time.Since(e.FirstSeen)
@ -92,7 +93,7 @@ func (d *Discovery) getRow(e *network.Endpoint) []string {
seen = core.Dim(seen) seen = core.Dim(seen)
} }
return []string{ row := []string{
addr, addr,
mac, mac,
name, name,
@ -101,13 +102,24 @@ func (d *Discovery) getRow(e *network.Endpoint) []string {
humanize.Bytes(traffic.Received), humanize.Bytes(traffic.Received),
seen, seen,
} }
if withMeta {
metas := []string{}
e.Meta.Each(func(name string, value interface{}) {
metas = append(metas, fmt.Sprintf("%s: %s", name, value.(string)))
})
row = append(row, strings.Join(metas, "\n"))
}
return row
} }
func (d *Discovery) showTable(header []string, rows [][]string) { func (d *Discovery) showTable(header []string, rows [][]string) {
fmt.Println() fmt.Println()
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header) table.SetHeader(header)
table.SetColWidth(80) table.SetColWidth(180)
table.AppendBulk(rows) table.AppendBulk(rows)
table.Render() table.Render()
} }
@ -132,15 +144,30 @@ func (d *Discovery) Show(by string) error {
targets = append([]*network.Endpoint{d.Session.Interface, d.Session.Gateway}, targets...) targets = append([]*network.Endpoint{d.Session.Interface, d.Session.Gateway}, targets...)
} }
rows := make([][]string, 0) hasMeta := false
for i, t := range targets { for _, t := range targets {
rows = append(rows, d.getRow(t)) if t.Meta.Empty() == false {
if i == pad { hasMeta = true
rows = append(rows, []string{"", "", "", "", "", "", ""}) break
} }
} }
d.showTable([]string{"IP", "MAC", "Name", "Vendor", "Sent", "Recvd", "Last Seen"}, rows) padCols := []string{"", "", "", "", "", "", ""}
colNames := []string{"IP", "MAC", "Name", "Vendor", "Sent", "Recvd", "Last Seen"}
if hasMeta {
padCols = append(padCols, "")
colNames = append(colNames, "Meta")
}
rows := make([][]string, 0)
for i, t := range targets {
rows = append(rows, d.getRow(t, hasMeta))
if i == pad {
rows = append(rows, padCols)
}
}
d.showTable(colNames, rows)
fmt.Printf("\n%s %s / %s %s / %d pkts / %d errs\n\n", fmt.Printf("\n%s %s / %s %s / %d pkts / %d errs\n\n",
core.Red("↑"), core.Red("↑"),
@ -171,27 +198,6 @@ func (d *Discovery) Show(by string) error {
fmt.Println() fmt.Println()
} }
/*
Last events are more useful than this histogram and vertical scroll
isn't infinite :)
rows = make([][]string, 0)
protos, maxPackets := rankByProtoHits(d.Session.Queue.Protos)
maxBarWidth := 70
for _, p := range protos {
width := int(float32(maxBarWidth) * (float32(p.Hits) / float32(maxPackets)))
bar := ""
for i := 0; i < width; i++ {
bar += "▇"
}
rows = append(rows, []string{p.Protocol, fmt.Sprintf("%s %d", bar, p.Hits)})
}
d.showTable([]string{"Proto", "# Packets"}, rows)
*/
d.Session.Refresh() d.Session.Refresh()
return nil return nil

View file

@ -42,3 +42,18 @@ func (m *Meta) Get(name string) interface{} {
} }
return "" return ""
} }
func (m *Meta) Each(cb func(name string, value interface{})) {
m.Lock()
defer m.Unlock()
for k, v := range m.m {
cb(k, v)
}
}
func (m *Meta) Empty() bool {
m.Lock()
defer m.Unlock()
return len(m.m) == 0
}