forked from dreadl0ck/netcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
97 lines (80 loc) · 1.84 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package netcap
import (
"fmt"
"io"
"log"
"os"
"strings"
"github.com/dreadl0ck/netcap/types"
"github.com/evilsocket/islazy/tui"
"github.com/gogo/protobuf/proto"
)
// Dump reads the specified netcap file
// and dumps the output according to the configuration to stdout
func Dump(path string, separator string, tsv bool, structured bool, table bool, selection string, utc bool, fields bool) {
var (
count = 0
r, err = Open(path)
)
if err != nil {
panic(err)
}
defer r.Close()
if separator == "\\t" || tsv {
separator = "\t"
}
var (
header = r.ReadHeader()
record = InitRecord(header.Type)
// rows for table print
rows [][]string
)
types.Select(record, selection)
types.UTC = utc
if !structured && !table {
if p, ok := record.(types.CSV); ok {
fmt.Println(strings.Join(p.CSVHeader(), separator))
} else {
log.Fatal("netcap type does not implement the types.CSV interface!")
}
if fields {
os.Exit(0)
}
}
for {
err := r.Next(record)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
} else if err != nil {
panic(err)
}
count++
if structured {
os.Stdout.WriteString(header.Type.String() + "\n")
os.Stdout.WriteString(proto.MarshalTextString(record) + "\n")
continue
}
if p, ok := record.(types.CSV); ok {
if table {
rows = append(rows, p.CSVRecord())
if count%100 == 0 {
tui.Table(os.Stdout, p.CSVHeader(), rows)
rows = [][]string{}
}
continue
}
os.Stdout.WriteString(strings.Join(p.CSVRecord(), separator) + "\n")
} else {
log.Fatal("netcap type does not implement the types.CSV interface!")
}
}
if table {
if p, ok := record.(types.CSV); ok {
tui.Table(os.Stdout, p.CSVHeader(), rows)
fmt.Println()
} else {
log.Fatal("netcap type does not implement the types.CSV interface!")
}
}
fmt.Println(count, "records.")
}