/
console.go
83 lines (69 loc) · 1.58 KB
/
console.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
package tcp
import (
"fmt"
"strconv"
"strings"
"github.com/m-mizutani/lurker/pkg/domain/interfaces"
)
func outputConsole(out interfaces.ConsoleFunc, flow *tcpFlow) {
state := "SYNed"
if flow.recvAck {
state = "ACKed"
}
hdr := fmt.Sprintf("%s:%d -> %s:%d (%s)",
flow.srcHost.String(),
flow.srcPort,
flow.dstHost.String(),
flow.dstPort,
state,
)
lines := []string{hdr, toHex(flow.recvData)}
if len(flow.recvData) > 0 {
graph := toGraph(flow.recvData)
lines = append(lines, []string{
"----------------------------------[dump]----------------------------------",
string(graph),
"--------------------------------------------------------------------------",
"",
}...)
}
out(strings.Join(lines, "\n"))
}
func toHex(data []byte) string {
lines := []string{}
for i := 0; i < len(data); i += 16 {
hex := make([]string, 16)
asc := make([]string, 16)
for n := 0; n < 16; n++ {
d := i + n
if i+n < len(data) {
p := data[d]
hex[n] = fmt.Sprintf("%02X", p)
if strconv.IsPrint(rune(p)) {
asc[n] = string(p)
} else {
asc[n] = "."
}
} else {
hex[n] = " "
asc[n] = " "
}
}
lines = append(lines, fmt.Sprintf(
"%04X %s | %s |", i, strings.Join(hex, " "), strings.Join(asc, ""),
))
}
return strings.Join(lines, "\n")
}
func toGraph(data []byte) string {
graph := make([]rune, len(data))
for d := 0; d < len(data); d++ {
p := data[d]
if strconv.IsGraphic(rune(p)) || rune(p) == '\n' || rune(p) == '\r' || rune(p) == '\t' {
graph[d] = rune(p)
} else {
graph[d] = '.'
}
}
return string(graph)
}