-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtcpterm.go
230 lines (185 loc) · 4.96 KB
/
tcpterm.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"time"
"io/ioutil"
"github.com/gdamore/tcell"
"github.com/google/gopacket"
"github.com/sachaos/tview"
)
const (
TailMode = iota
SelectMode
)
type Tcpterm struct {
src *gopacket.PacketSource
view *tview.Application
primitives []tview.Primitive
table *tview.Table
detail *tview.TextView
dump *tview.TextView
frame *tview.Frame
packets []gopacket.Packet
mode int
logger *log.Logger
}
const (
timestampFormt = "2006-01-02 15:04:05.000000"
)
func NewTcpterm(src *gopacket.PacketSource, debug bool) *Tcpterm {
view := tview.NewApplication()
packetList := preparePacketList()
packetDetail := preparePacketDetail()
packetDump := preparePacketDump()
layout := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(packetList, 0, 1, true).
AddItem(packetDetail, 0, 1, false).
AddItem(packetDump, 0, 1, false)
frame := prepareFrame(layout)
view.SetRoot(frame, true).SetFocus(packetList)
var w io.Writer
if debug {
w = os.Stderr
} else {
w = ioutil.Discard
}
app := &Tcpterm{
src: src,
view: view,
primitives: []tview.Primitive{packetList, packetDetail, packetDump},
table: packetList,
detail: packetDetail,
dump: packetDump,
frame: frame,
logger: log.New(w, "[tcpterm]", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile),
}
app.SwitchToTailMode()
view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlC {
app.Stop()
}
if event.Key() == tcell.KeyTAB {
app.rotateView()
}
return event
})
packetList.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEsc {
app.SwitchToTailMode()
}
if key == tcell.KeyEnter {
app.SwitchToSelectMode()
}
})
packetList.SetSelectionChangedFunc(func(row int, column int) {
app.displayDetailOf(row)
})
return app
}
func (app *Tcpterm) PacketListGenerator(refreshTrigger chan bool) {
cnt := 0
for {
packet, err := app.src.NextPacket()
if err == io.EOF {
return
} else if err == nil {
cnt++
rowCount := app.table.GetRowCount()
app.logger.Printf("count: %v start\n", cnt)
app.table.SetCell(rowCount, 0, tview.NewTableCell(strconv.Itoa(cnt)))
app.table.SetCell(rowCount, 1, tview.NewTableCell(packet.Metadata().Timestamp.Format(timestampFormt)))
app.table.SetCell(rowCount, 2, tview.NewTableCell(flowOf(packet)))
app.table.SetCell(rowCount, 3, tview.NewTableCell(strconv.Itoa(packet.Metadata().Length)))
app.table.SetCell(rowCount, 4, tview.NewTableCell(packet.Layers()[1].LayerType().String()))
if len(packet.Layers()) > 2 {
app.table.SetCell(rowCount, 5, tview.NewTableCell(packet.Layers()[2].LayerType().String()))
}
app.packets = append(app.packets, packet)
app.logger.Printf("count: %v end\n", cnt)
}
}
}
func (app *Tcpterm) Ticker(refreshTrigger chan bool) {
for {
time.Sleep(100 * time.Millisecond)
refreshTrigger <- true
}
}
func (app *Tcpterm) Refresh(refreshTrigger chan bool) {
for {
_, ok := <-refreshTrigger
if ok {
app.view.Draw()
}
}
}
func (app *Tcpterm) Run() {
refreshTrigger := make(chan bool)
go app.PacketListGenerator(refreshTrigger)
go app.Ticker(refreshTrigger)
go app.Refresh(refreshTrigger)
if app.view.Run(); err != nil {
panic(err)
}
}
func (app *Tcpterm) Stop() {
app.view.Stop()
}
func (app *Tcpterm) SwitchToTailMode() {
app.mode = TailMode
app.table.SetSelectable(false, false)
app.table.ScrollToEnd()
app.frame.Clear().AddText("**Tail**", true, tview.AlignLeft, tcell.ColorGreen)
app.frame.AddText("g: page top, G: page end, TAB: rotate panel, Enter: Detail mode", true, tview.AlignRight, tcell.ColorDefault)
}
func (app *Tcpterm) SwitchToSelectMode() {
app.mode = SelectMode
app.table.SetSelectable(true, false)
row, _ := app.table.GetOffset()
app.table.Select(row+1, 0)
app.displayDetailOf(row + 1)
app.frame.Clear().AddText("*Detail*", true, tview.AlignLeft, tcell.ColorBlue)
app.frame.AddText("g: page top, G: page end, TAB: rotate panel, ESC: Tail mode", true, tview.AlignRight, tcell.ColorDefault)
}
func (app *Tcpterm) displayDetailOf(row int) {
if row < 1 || row > len(app.packets) {
return
}
app.detail.Clear().ScrollToBeginning()
app.dump.Clear().ScrollToBeginning()
packet := app.packets[row-1]
fmt.Fprint(app.detail, packet.String())
fmt.Fprint(app.dump, packet.Dump())
}
func (app *Tcpterm) rotateView() {
idx, err := app.findPrimitiveIdx(app.view.GetFocus())
if err != nil {
panic(err)
}
nextIdx := idx + 1
if nextIdx >= len(app.primitives) {
nextIdx = 0
}
app.view.SetFocus(app.primitives[nextIdx])
}
func (app *Tcpterm) findPrimitiveIdx(p tview.Primitive) (int, error) {
for i, primitive := range app.primitives {
if p == primitive {
return i, nil
}
}
return 0, errors.New("Primitive not found")
}
func flowOf(packet gopacket.Packet) string {
if packet.NetworkLayer() == nil {
return "-"
} else {
return packet.NetworkLayer().NetworkFlow().String()
}
}