-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathserverimpl.go
344 lines (293 loc) · 8.54 KB
/
serverimpl.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
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
package server
import (
"bytes"
"compress/gzip"
"context"
"errors"
"io"
"net"
"net/http"
"github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
"github.com/open-telemetry/opamp-go/client/types"
"github.com/open-telemetry/opamp-go/internal"
"github.com/open-telemetry/opamp-go/protobufs"
serverTypes "github.com/open-telemetry/opamp-go/server/types"
)
var (
errAlreadyStarted = errors.New("already started")
)
const defaultOpAMPPath = "/v1/opamp"
const headerContentType = "Content-Type"
const headerContentEncoding = "Content-Encoding"
const headerAcceptEncoding = "Accept-Encoding"
const contentEncodingGzip = "gzip"
const contentTypeProtobuf = "application/x-protobuf"
type server struct {
logger types.Logger
settings Settings
// Upgrader to use to upgrade HTTP to WebSocket.
wsUpgrader websocket.Upgrader
// The listening HTTP Server after successful Start() call. Nil if Start()
// is not called or was not successful.
httpServer *http.Server
}
var _ OpAMPServer = (*server)(nil)
func New(logger types.Logger) *server {
if logger == nil {
logger = &internal.NopLogger{}
}
return &server{logger: logger}
}
func (s *server) Attach(settings Settings) (HTTPHandlerFunc, ConnContext, error) {
s.settings = settings
s.wsUpgrader = websocket.Upgrader{
EnableCompression: settings.EnableCompression,
}
return s.httpHandler, contextWithConn, nil
}
func (s *server) Start(settings StartSettings) error {
if s.httpServer != nil {
return errAlreadyStarted
}
_, _, err := s.Attach(settings.Settings)
if err != nil {
return err
}
// Prepare handling OpAMP incoming HTTP requests on the requests URL path.
mux := http.NewServeMux()
path := settings.ListenPath
if path == "" {
path = defaultOpAMPPath
}
mux.HandleFunc(path, s.httpHandler)
hs := &http.Server{
Handler: mux,
Addr: settings.ListenEndpoint,
TLSConfig: settings.TLSConfig,
ConnContext: contextWithConn,
}
s.httpServer = hs
listenAddr := s.httpServer.Addr
// Start the HTTP Server in background.
if hs.TLSConfig != nil {
if listenAddr == "" {
listenAddr = ":https"
}
err = s.startHttpServer(
listenAddr,
func(l net.Listener) error { return hs.ServeTLS(l, "", "") },
)
} else {
if listenAddr == "" {
listenAddr = ":http"
}
err = s.startHttpServer(
listenAddr,
func(l net.Listener) error { return hs.Serve(l) },
)
}
return err
}
func (s *server) startHttpServer(listenAddr string, serveFunc func(l net.Listener) error) error {
// If the listen address is not specified use the default.
ln, err := net.Listen("tcp", listenAddr)
if err != nil {
return err
}
// Begin serving connections in the background.
go func() {
err = serveFunc(ln)
// ErrServerClosed is expected after successful Stop(), so we won't log that
// particular error.
if err != nil && err != http.ErrServerClosed {
s.logger.Errorf("Error running HTTP Server: %v", err)
}
}()
return nil
}
func (s *server) Stop(ctx context.Context) error {
if s.httpServer != nil {
defer func() { s.httpServer = nil }()
// This stops accepting new connections. TODO: close existing
// connections and wait them to be terminated.
return s.httpServer.Shutdown(ctx)
}
return nil
}
func (s *server) httpHandler(w http.ResponseWriter, req *http.Request) {
var connectionCallbacks serverTypes.ConnectionCallbacks
if s.settings.Callbacks != nil {
resp := s.settings.Callbacks.OnConnecting(req)
if !resp.Accept {
// HTTP connection is not accepted. Set the response headers.
for k, v := range resp.HTTPResponseHeader {
w.Header().Set(k, v)
}
// And write the response status code.
w.WriteHeader(resp.HTTPStatusCode)
return
}
// use connection-specific handler provided by ConnectionResponse
connectionCallbacks = resp.ConnectionCallbacks
}
// HTTP connection is accepted. Check if it is a plain HTTP request.
if req.Header.Get(headerContentType) == contentTypeProtobuf {
// Yes, a plain HTTP request.
s.handlePlainHTTPRequest(req, w, connectionCallbacks)
return
}
// No, it is a WebSocket. Upgrade it.
conn, err := s.wsUpgrader.Upgrade(w, req, nil)
if err != nil {
s.logger.Errorf("Cannot upgrade HTTP connection to WebSocket: %v", err)
return
}
// Return from this func to reduce memory usage.
// Handle the connection on a separate goroutine.
go s.handleWSConnection(conn, connectionCallbacks)
}
func (s *server) handleWSConnection(wsConn *websocket.Conn, connectionCallbacks serverTypes.ConnectionCallbacks) {
agentConn := wsConnection{wsConn: wsConn}
defer func() {
// Close the connection when all is done.
defer func() {
err := wsConn.Close()
if err != nil {
s.logger.Errorf("error closing the WebSocket connection: %v", err)
}
}()
if connectionCallbacks != nil {
connectionCallbacks.OnConnectionClose(agentConn)
}
}()
if connectionCallbacks != nil {
connectionCallbacks.OnConnected(agentConn)
}
// Loop until fail to read from the WebSocket connection.
for {
// Block until the next message can be read.
mt, bytes, err := wsConn.ReadMessage()
if err != nil {
if !websocket.IsUnexpectedCloseError(err) {
s.logger.Errorf("Cannot read a message from WebSocket: %v", err)
break
}
// This is a normal closing of the WebSocket connection.
s.logger.Debugf("Agent disconnected: %v", err)
break
}
if mt != websocket.BinaryMessage {
s.logger.Errorf("Received unexpected message type from WebSocket: %v", mt)
continue
}
// Decode WebSocket message as a Protobuf message.
var request protobufs.AgentToServer
err = internal.DecodeWSMessage(bytes, &request)
if err != nil {
s.logger.Errorf("Cannot decode message from WebSocket: %v", err)
continue
}
if connectionCallbacks != nil {
response := connectionCallbacks.OnMessage(agentConn, &request)
if response.InstanceUid == "" {
response.InstanceUid = request.InstanceUid
}
err = agentConn.Send(context.Background(), response)
if err != nil {
s.logger.Errorf("Cannot send message to WebSocket: %v", err)
}
}
}
}
func decompressGzip(data []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, err
}
defer r.Close()
return io.ReadAll(r)
}
func (s *server) readReqBody(req *http.Request) ([]byte, error) {
data, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
if req.Header.Get(headerContentEncoding) == contentEncodingGzip {
data, err = decompressGzip(data)
if err != nil {
return nil, err
}
}
return data, nil
}
func compressGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
w := gzip.NewWriter(&buf)
_, err := w.Write(data)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s *server) handlePlainHTTPRequest(req *http.Request, w http.ResponseWriter, connectionCallbacks serverTypes.ConnectionCallbacks) {
bytes, err := s.readReqBody(req)
if err != nil {
s.logger.Debugf("Cannot read HTTP body: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
// Decode the message as a Protobuf message.
var request protobufs.AgentToServer
err = proto.Unmarshal(bytes, &request)
if err != nil {
s.logger.Debugf("Cannot decode message from HTTP Body: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
agentConn := httpConnection{
conn: connFromRequest(req),
}
if connectionCallbacks == nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
connectionCallbacks.OnConnected(agentConn)
defer func() {
// Indicate via the callback that the OpAMP Connection is closed. From OpAMP
// perspective the connection represented by this http request
// is closed. It is not possible to send or receive more OpAMP messages
// via this agentConn.
connectionCallbacks.OnConnectionClose(agentConn)
}()
response := connectionCallbacks.OnMessage(agentConn, &request)
// Set the InstanceUid if it is not set by the callback.
if response.InstanceUid == "" {
response.InstanceUid = request.InstanceUid
}
// Marshal the response.
bytes, err = proto.Marshal(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Send the response.
w.Header().Set(headerContentType, contentTypeProtobuf)
if req.Header.Get(headerAcceptEncoding) == contentEncodingGzip {
bytes, err = compressGzip(bytes)
if err != nil {
s.logger.Errorf("Cannot compress response: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set(headerContentEncoding, contentEncodingGzip)
}
_, err = w.Write(bytes)
if err != nil {
s.logger.Debugf("Cannot send HTTP response: %v", err)
}
}