summaryrefslogtreecommitdiff
path: root/internal/ui/websocket.go
blob: 9251cac21f0b07fa94e9f19af2ff783c5cd8147b (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package ui

import (
	"fmt"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"

	"github.com/shiv/internal/events"
	"github.com/shiv/internal/logger"
	"github.com/shiv/internal/store"
	"github.com/shiv/internal/ui/widgets"
)

var wsWin fyne.Window

func showWebSocketWindow(fyneApp fyne.App, projectStore *store.Store, parentWin fyne.Window, repeater *repeaterTab) {
	if wsWin != nil {
		wsWin.RequestFocus()
		return
	}
	wsWin = fyneApp.NewWindow("WebSockets")
	wsWin.Resize(fyne.NewSize(1100, 650))
	wsWin.SetOnClosed(func() { wsWin = nil })
	closeOnEscape(wsWin, wsWin.Close)
	wsWin.SetContent(buildWebSocketContent(projectStore, wsWin, repeater))
	wsWin.Show()
}

func filterConnections(all []store.WebSocketConnection, query string) []store.WebSocketConnection {
	q := strings.ToLower(strings.TrimSpace(query))
	if q == "" {
		out := make([]store.WebSocketConnection, len(all))
		copy(out, all)
		return out
	}
	terms := strings.Fields(q)
	var out []store.WebSocketConnection
	for _, c := range all {
		s := strings.ToLower(c.Host + " " + c.URL)
		ok := true
		for _, t := range terms {
			if !strings.Contains(s, t) {
				ok = false
				break
			}
		}
		if ok {
			out = append(out, c)
		}
	}
	return out
}

func filterFrames(all []store.WebSocketFrame, query string) []store.WebSocketFrame {
	q := strings.ToLower(strings.TrimSpace(query))
	if q == "" {
		out := make([]store.WebSocketFrame, len(all))
		copy(out, all)
		return out
	}
	terms := strings.Fields(q)
	var out []store.WebSocketFrame
	for _, f := range all {
		s := strings.ToLower(string(f.Payload) + " " + opcodeLabel(f.Opcode))
		ok := true
		for _, t := range terms {
			if !strings.Contains(s, t) {
				ok = false
				break
			}
		}
		if ok {
			out = append(out, f)
		}
	}
	return out
}

func buildConnTable(win fyne.Window, filteredConns *[]store.WebSocketConnection) (*widgets.DataTable, fyne.CanvasObject) {
	connColumns := []widgets.DataTableColumn{
		{Header: "ID", Width: 50},
		{Header: "Host", Width: 220},
		{Header: "URL", Width: 380},
		{Header: "TLS", Width: 45},
		{Header: "Frames", Width: 70},
		{Header: "Time", Width: 180},
	}
	connTable := widgets.NewDataTable()
	connTable.SetWindow(win)
	connTable.Columns = connColumns
	connTable.RowCount = func() int { return len(*filteredConns) }
	connTable.RowID = func(row int) int64 {
		if row >= len(*filteredConns) {
			return 0
		}
		return int64((*filteredConns)[row].ID)
	}
	connTable.CellValue = func(row, col int) string {
		if row >= len(*filteredConns) {
			return ""
		}
		c := (*filteredConns)[row]
		switch col {
		case 0:
			return fmt.Sprintf("%d", c.ID)
		case 1:
			return c.Host
		case 2:
			return c.URL
		case 3:
			if c.TLS {
				return "✓"
			}
			return ""
		case 4:
			return fmt.Sprintf("%d", c.FrameCount)
		case 5:
			return c.Timestamp.Local().Format("2006-01-02 15:04:05")
		}
		return ""
	}
	return connTable, connTable.Build()
}

func buildFrameTable(win fyne.Window, filteredFrames *[]store.WebSocketFrame) (*widgets.DataTable, fyne.CanvasObject) {
	frameColumns := []widgets.DataTableColumn{
		{Header: "Dir", Width: 90},
		{Header: "Type", Width: 70},
		{Header: "Length", Width: 80},
		{Header: "Time", Width: 150},
	}
	frameTable := widgets.NewDataTable()
	frameTable.SetWindow(win)
	frameTable.Columns = frameColumns
	frameTable.RowCount = func() int { return len(*filteredFrames) }
	frameTable.RowID = func(row int) int64 {
		if row >= len(*filteredFrames) {
			return 0
		}
		return int64((*filteredFrames)[row].ID)
	}
	frameTable.CellValue = func(row, col int) string {
		if row >= len(*filteredFrames) {
			return ""
		}
		f := (*filteredFrames)[row]
		switch col {
		case 0:
			if f.Direction == events.WebSocketClient {
				return "→ Client"
			}
			return "← Server"
		case 1:
			return opcodeLabel(f.Opcode)
		case 2:
			return fmt.Sprintf("%db", len(f.Payload))
		case 3:
			return f.Timestamp.Local().Format("15:04:05.000")
		}
		return ""
	}
	frameTable.CellStyle = func(row, col int) widget.Importance {
		if col != 0 || row >= len(*filteredFrames) {
			return widget.MediumImportance
		}
		if (*filteredFrames)[row].Direction == events.WebSocketClient {
			return widget.HighImportance
		}
		return widget.SuccessImportance
	}
	return frameTable, frameTable.Build()
}

func buildFrameDetail(win fyne.Window, filteredFrames *[]store.WebSocketFrame, frameTable *widgets.DataTable) (*widgets.TextView, fyne.CanvasObject) {
	payloadView := widgets.NewTextView()
	payloadView.SetWindow(win)
	payloadView.SetPlaceHolder("Select a frame to view payload...")

	frameTable.OnSelect = func(row int) {
		if row >= len(*filteredFrames) {
			return
		}
		payload := (*filteredFrames)[row].Payload
		if len(payload) == 0 {
			payloadView.SetText("(empty)")
		} else {
			payloadView.SetText(string(payload))
		}
	}

	payloadPane := container.NewBorder(
		newBoldLabel("Payload"),
		nil, nil, nil,
		payloadView.Build(),
	)
	return payloadView, payloadPane
}

func buildWebSocketContent(projectStore *store.Store, win fyne.Window, repeater *repeaterTab) fyne.CanvasObject {
	var allConns []store.WebSocketConnection
	var filteredConns []store.WebSocketConnection
	var allFrames []store.WebSocketFrame
	var filteredFrames []store.WebSocketFrame
	var selectedConnID uint64

	connFilterEntry := widget.NewEntry()
	connFilterEntry.SetPlaceHolder("Filter — host, url...")
	frameFilterEntry := widget.NewEntry()
	frameFilterEntry.SetPlaceHolder("Filter frames — payload, type...")

	refilterConns := func() { filteredConns = filterConnections(allConns, connFilterEntry.Text) }
	refilterFrames := func() { filteredFrames = filterFrames(allFrames, frameFilterEntry.Text) }

	connTable, connTableObj := buildConnTable(win, &filteredConns)
	frameTable, frameTableObj := buildFrameTable(win, &filteredFrames)
	payloadView, payloadPane := buildFrameDetail(win, &filteredFrames, frameTable)

	loadFrames := func(connID uint64) {
		go func() {
			loaded, err := projectStore.FramesForConnection(connID)
			if err != nil {
				logger.Error("ws ui: load frames %d: %v", connID, err)
				return
			}
			fyne.Do(func() {
				allFrames = loaded
				refilterFrames()
				frameTable.Refresh()
				payloadView.SetText("")
			})
		}()
	}

	connTable.OnSelect = func(row int) {
		if row >= len(filteredConns) {
			return
		}
		selectedConnID = filteredConns[row].ID
		loadFrames(selectedConnID)
	}

	connTable.MenuItems = func(row int) []widgets.ContextMenuItem {
		if row >= len(filteredConns) {
			return nil
		}
		c := filteredConns[row]
		return []widgets.ContextMenuItem{
			{Label: "Send to Repeater", Action: func() {
				if repeater != nil {
					repeater.AddWSTab(c.Host, c.URL, c.TLS)
				}
			}},
			{Label: "Copy URL", Action: func() {
				fyne.CurrentApp().Clipboard().SetContent(c.URL)
			}},
		}
	}

	connFilterEntry.OnChanged = func(q string) {
		filteredConns = filterConnections(allConns, q)
		connTable.Refresh()
	}
	frameFilterEntry.OnChanged = func(q string) {
		filteredFrames = filterFrames(allFrames, q)
		frameTable.Refresh()
	}

	go func() {
		for conn := range projectStore.WebSocketConnections {
			c := conn
			fyne.Do(func() {
				for _, existing := range allConns {
					if existing.ID == c.ID {
						return
					}
				}
				allConns = append([]store.WebSocketConnection{c}, allConns...)
				refilterConns()
				connTable.Refresh()
			})
		}
	}()

	go func() {
		for frame := range projectStore.WebSocketFrames {
			f := frame
			fyne.Do(func() {
				for i := range allConns {
					if allConns[i].ID == f.ConnectionID {
						allConns[i].FrameCount++
						break
					}
				}
				refilterConns()
				connTable.Refresh()
				if f.ConnectionID != selectedConnID {
					return
				}
				go func() {
					loaded, err := projectStore.FramesForConnection(f.ConnectionID)
					if err != nil {
						logger.Error("ws ui: reload frames %d: %v", f.ConnectionID, err)
						return
					}
					fyne.Do(func() {
						allFrames = loaded
						refilterFrames()
						frameTable.Refresh()
					})
				}()
			})
		}
	}()

	refreshBtn := widget.NewButtonWithIcon("Refresh", AppIcon("history"), func() {
		go func() {
			loaded, err := projectStore.AllWebSocketConnections()
			if err != nil {
				logger.Error("ws ui: load connections: %v", err)
				return
			}
			fyne.Do(func() {
				allConns = loaded
				refilterConns()
				connTable.Refresh()
				if selectedConnID > 0 {
					loadFrames(selectedConnID)
				}
			})
		}()
	})

	go func() {
		loaded, err := projectStore.AllWebSocketConnections()
		if err != nil {
			logger.Error("ws ui: initial load: %v", err)
			return
		}
		fyne.Do(func() {
			allConns = loaded
			filteredConns = filterConnections(allConns, "")
			connTable.Refresh()
		})
	}()

	connFilterBar := container.NewBorder(nil, nil, nil, refreshBtn, connFilterEntry)
	frameFilterBar := container.NewBorder(nil, nil, newBoldLabel("Frames"), nil, frameFilterEntry)

	connPane := container.NewBorder(
		container.NewVBox(newBoldLabel("Connections"), connFilterBar),
		nil, nil, nil, connTableObj,
	)
	framePane := container.NewBorder(frameFilterBar, nil, nil, nil, frameTableObj)

	bottomSplit := container.NewHSplit(framePane, payloadPane)
	bottomSplit.SetOffset(0.4)
	mainSplit := container.NewVSplit(connPane, bottomSplit)
	mainSplit.SetOffset(0.4)
	return mainSplit
}

func opcodeLabel(op events.WebSocketOpcode) string {
	switch op {
	case events.WebSocketText:
		return "Text"
	case events.WebSocketBinary:
		return "Binary"
	case events.WebSocketPing:
		return "Ping"
	case events.WebSocketPong:
		return "Pong"
	case events.WebSocketClose:
		return "Close"
	}
	return fmt.Sprintf("0x%x", int(op))
}