summaryrefslogtreecommitdiff
path: root/internal/events/events.go
blob: 5258a21bed04737e067fea766230555e242ced30 (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
package events

import (
	"net/http"
	"time"
)

// RequestEvent carries a fully-read HTTP request and its body, ready to be
// forwarded. The proxy emits this before forwarding. Consumers may inspect
// or modify the request. The result controls whether forwarding proceeds.
type RequestEvent struct {
	Request *http.Request
	Body    []byte
}

// ResponseEvent carries a completed HTTP transaction. The proxy emits this
// after the upstream response is fully received and the body is decompressed.
// All fields are populated; no further store knowledge is required.
type ResponseEvent struct {
	Timestamp   time.Time
	Host        string
	Proto       string // "HTTP/1.1" or "HTTP/2"
	Method      string
	URL         string
	ReqHeaders  http.Header
	ReqBody     []byte
	StatusCode  int
	RespHeaders http.Header
	RespBody    []byte // decompressed; nil if binary
	DurationMs  int64
	TLS         bool
}

// RequestResult is returned by RequestMiddleware. If Drop is true the proxy
// sends a 403 and does not forward. Request and Body are the (possibly
// modified) request to forward when Drop is false.
type RequestResult struct {
	Drop    bool
	Request *http.Request
	Body    []byte
}

// WebSocketDirection indicates which endpoint sent a frame.
type WebSocketDirection int

const (
	WebSocketClient WebSocketDirection = 0 // browser → server
	WebSocketServer WebSocketDirection = 1 // server → browser
)

// WebSocketOpcode mirrors gorilla/websocket message type constants.
type WebSocketOpcode int

const (
	WebSocketText   WebSocketOpcode = 1
	WebSocketBinary WebSocketOpcode = 2
	WebSocketPing   WebSocketOpcode = 9
	WebSocketPong   WebSocketOpcode = 10
	WebSocketClose  WebSocketOpcode = 8
)

// WebSocketConnectionEvent is emitted once when a WebSocket upgrade handshake
// completes successfully. The proxy emits this after the upstream dial and
// browser upgrade both succeed.
type WebSocketConnectionEvent struct {
	Host      string
	URL       string
	TLS       bool
	Timestamp time.Time
}

// WebSocketFrameResult is returned by WebSocketFrameObserver. Payload is the
// (possibly modified) payload to forward. If Payload is nil the original is used.
type WebSocketFrameResult struct {
	Payload []byte
}

// WebSocketFrameEvent is emitted for every proxied WebSocket frame in either
// direction. ConnectionID is the value returned by the
// WebSocketConnectionObserver that handled the corresponding
// WebSocketConnectionEvent.
type WebSocketFrameEvent struct {
	ConnectionID uint64
	Timestamp    time.Time
	Direction    WebSocketDirection
	Opcode       WebSocketOpcode
	Payload      []byte
}

// PluginLogEvent is emitted by the plugin engine when a Lua plugin calls
// log(). The store observes it, buffers the line in memory, and notifies
// the UI via the PluginEntries channel.
type PluginLogEvent struct {
	Name    string // plugin filename (e.g. "myplugin.lua")
	Message string
}

// SetPluginEnabledEvent is emitted by the UI when the user toggles a plugin.
// The engine observes it to skip or include that plugin's hooks.
// The store observes it to persist the new state to the database.
type SetPluginEnabledEvent struct {
	Name    string // plugin filename
	Enabled bool
}

// LoadPluginEvent is emitted by the UI when the user selects a .lua file to
// import. The engine observes it, copies the file into the plugins directory,
// loads it, and registers it for subsequent hook calls.
type LoadPluginEvent struct {
	SourcePath string // absolute path to the selected .lua file
}

// ProxyRestartEvent is emitted by the UI when the user saves proxy settings
// or toggles the proxy on. Addr is the new listen address (host:port).
// The proxy observes this and restarts on the new address.
type ProxyRestartEvent struct {
	Addr string
}

// ProxyStopEvent is emitted by the UI when the user toggles the proxy off.
// The proxy observes this and stops listening.
type ProxyStopEvent struct{}