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
|
package store
import (
"fmt"
"time"
"github.com/shiv/internal/events"
"github.com/shiv/internal/logger"
)
const wsFrameSizeLimit = 1 << 20 // 1 MB per frame
// WebSocketFrame is a single frame captured during a WebSocket session.
type WebSocketFrame struct {
ID uint64
ConnectionID uint64 // foreign key → websocket_connections.id
Timestamp time.Time
Direction events.WebSocketDirection
Opcode events.WebSocketOpcode
Payload []byte
}
// WebSocketConnection represents the initial HTTP upgrade that started a
// WebSocket session. It is tied to the history transaction of the upgrade
// request so the UI can link from history to the frame list.
type WebSocketConnection struct {
ID uint64
HistoryID uint64
Host string
URL string
TLS bool
InScope bool
Timestamp time.Time
FrameCount int // populated on read, not stored
}
// LogWebSocketConnection inserts a new WebSocket connection record and returns
// its ID. Called once when the upgrade handshake completes.
func (s *Store) LogWebSocketConnection(conn WebSocketConnection) (uint64, error) {
var id uint64
err := s.write(func() error {
res, err := s.db.Exec(`
INSERT INTO websocket_connections (history_id, host, url, tls, in_scope, timestamp)
VALUES (?, ?, ?, ?, ?, ?)`,
conn.HistoryID,
conn.Host,
conn.URL,
boolToInt(conn.TLS),
boolToInt(conn.InScope),
conn.Timestamp.UTC().Format(time.RFC3339),
)
if err != nil {
return fmt.Errorf("store: log websocket connection: %w", err)
}
lastID, err := res.LastInsertId()
if err != nil {
return fmt.Errorf("store: websocket connection last insert id: %w", err)
}
id = uint64(lastID)
return nil
})
if err != nil {
return 0, err
}
// Notify UI listeners — non-blocking, same pattern as Updates.
conn.ID = id
select {
case s.WebSocketConnections <- conn:
default:
}
return id, nil
}
// LogWebSocketFrame appends a single frame to an existing connection.
func (s *Store) LogWebSocketFrame(frame WebSocketFrame) error {
err := s.write(func() error {
_, err := s.db.Exec(`
INSERT INTO websocket_frames (connection_id, timestamp, direction, opcode, payload)
VALUES (?, ?, ?, ?, ?)`,
frame.ConnectionID,
frame.Timestamp.UTC().Format(time.RFC3339),
int(frame.Direction),
int(frame.Opcode),
frame.Payload,
)
if err != nil {
return fmt.Errorf("store: log websocket frame: %w", err)
}
return nil
})
if err != nil {
return err
}
// Notify UI listeners — non-blocking.
select {
case s.WebSocketFrames <- frame:
default:
}
return nil
}
// AllWebSocketConnections returns all WebSocket connections ordered by id
// descending, with frame counts.
func (s *Store) AllWebSocketConnections() ([]WebSocketConnection, error) {
rows, err := s.db.Query(`
SELECT c.id, c.history_id, c.host, c.url, c.tls, c.in_scope, c.timestamp,
COUNT(f.id) as frame_count
FROM websocket_connections c
LEFT JOIN websocket_frames f ON f.connection_id = c.id
GROUP BY c.id
ORDER BY c.id DESC`)
if err != nil {
return nil, fmt.Errorf("store: query websocket connections: %w", err)
}
defer rows.Close()
var conns []WebSocketConnection
for rows.Next() {
var c WebSocketConnection
var ts string
var tlsInt, scopeInt int
if err := rows.Scan(&c.ID, &c.HistoryID, &c.Host, &c.URL, &tlsInt, &scopeInt, &ts, &c.FrameCount); err != nil {
return nil, fmt.Errorf("store: scan websocket connection: %w", err)
}
c.Timestamp, _ = time.Parse(time.RFC3339, ts)
c.TLS = tlsInt == 1
c.InScope = scopeInt == 1
conns = append(conns, c)
}
return conns, rows.Err()
}
// FramesForConnection returns all frames for a given connection ordered by id
// ascending (chronological).
func (s *Store) FramesForConnection(connectionID uint64) ([]WebSocketFrame, error) {
rows, err := s.db.Query(`
SELECT id, connection_id, timestamp, direction, opcode, payload
FROM websocket_frames
WHERE connection_id = ?
ORDER BY id ASC`, connectionID)
if err != nil {
return nil, fmt.Errorf("store: query websocket frames: %w", err)
}
defer rows.Close()
var frames []WebSocketFrame
for rows.Next() {
var f WebSocketFrame
var ts string
if err := rows.Scan(&f.ID, &f.ConnectionID, &ts, &f.Direction, &f.Opcode, &f.Payload); err != nil {
return nil, fmt.Errorf("store: scan websocket frame: %w", err)
}
f.Timestamp, _ = time.Parse(time.RFC3339, ts)
frames = append(frames, f)
}
return frames, rows.Err()
}
// ObserveWebSocketConnection implements events.WebSocketConnectionObserver.
func (s *Store) ObserveWebSocketConnection(e events.WebSocketConnectionEvent) uint64 {
id, err := s.LogWebSocketConnection(WebSocketConnection{
Host: e.Host,
URL: e.URL,
TLS: e.TLS,
InScope: s.InScope(e.Host),
Timestamp: e.Timestamp,
})
if err != nil {
logger.Error("store: observe ws connection: %v", err)
return 0
}
return id
}
// ObserveWebSocketFrame implements events.WebSocketFrameObserver.
func (s *Store) ObserveWebSocketFrame(e events.WebSocketFrameEvent) events.WebSocketFrameResult {
if e.ConnectionID == 0 {
return events.WebSocketFrameResult{Payload: e.Payload}
}
payload := e.Payload
if len(payload) > wsFrameSizeLimit {
payload = payload[:wsFrameSizeLimit]
}
if err := s.LogWebSocketFrame(WebSocketFrame{
ConnectionID: e.ConnectionID,
Timestamp: e.Timestamp,
Direction: e.Direction,
Opcode: e.Opcode,
Payload: payload,
}); err != nil {
logger.Error("store: observe ws frame: %v", err)
}
return events.WebSocketFrameResult{Payload: e.Payload}
}
|