-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
347 lines (298 loc) · 9.31 KB
/
client.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
345
346
347
package pcap
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/gopacket/gopacket/pcapgo"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
const logProgressWait = 5 * time.Second
type MessageWriter interface {
WriteMessage(message *Message)
}
type LogMessageWriter struct {
Log *zap.Logger
}
// WriteMessage accepts a Message and writes a log-line using a log-level corresponding to the severity of the message.
func (c LogMessageWriter) WriteMessage(message *Message) {
formattedMessage := fmt.Sprintf("%s(%s): %s", message.Origin, message.Type, message.Message)
c.Log.Log(MessageLogLevel(message), formattedMessage)
}
// MessageLogLevel translates the message types to appropropriate default log levels.
func MessageLogLevel(message *Message) zapcore.Level {
switch message.Type {
case MessageType_UNKNOWN:
return zapcore.WarnLevel
case MessageType_INSTANCE_UNAVAILABLE:
return zapcore.WarnLevel
case MessageType_START_CAPTURE_FAILED:
return zapcore.WarnLevel
case MessageType_INVALID_REQUEST:
return zapcore.ErrorLevel
case MessageType_CONGESTED:
return zapcore.WarnLevel
case MessageType_LIMIT_REACHED:
return zapcore.WarnLevel
case MessageType_CAPTURE_STOPPED:
return zapcore.InfoLevel
case MessageType_CONNECTION_ERROR:
return zapcore.ErrorLevel
}
return zapcore.ErrorLevel
}
// Client provides a reusable client for issuing capture requests against the pcap-api.
type Client struct {
packetFile *os.File
log *zap.Logger
stream API_CaptureClient
messageWriter MessageWriter
stopped bool
aPIClient
}
// NewClient sets up logging for the client and creates the outputFile.
// It assumes that the outputFile does not pre-exist and that the path is writeable (should be checked by CLI).
//
// NewClient returns a new Client if there are no issues with outputFile creation.
func NewClient(outputFile string, logger *zap.Logger, writer MessageWriter) (*Client, error) {
var err error
client := &Client{log: logger, messageWriter: writer}
if len(outputFile) == 0 {
if logsToStdout(zapConfig) {
return nil, fmt.Errorf("writing and logging to stdout is not supported")
}
client.packetFile = os.Stdout
} else {
client.packetFile, err = os.Create(outputFile)
if err != nil {
return nil, err
}
}
return client, nil
}
func (c *Client) Stop() {
c.StopRequest()
}
// ConnectToAPI sets up the grpc-connection between client and pcap-api.
//
// Depending on the http scheme in apiURL, it uses plain HTTP or TLS.
func (c *Client) ConnectToAPI(apiURL *url.URL, skipVerify bool) error {
var (
err error
creds credentials.TransportCredentials
)
if apiURL.Scheme == "https" {
tlsConfig := newTLSConfig()
tlsConfig.InsecureSkipVerify = skipVerify
creds = credentials.NewTLS(tlsConfig)
} else { // plain http
creds = insecure.NewCredentials()
}
c.cc, err = grpc.Dial(apiURL.Host, grpc.WithTransportCredentials(creds))
if err != nil {
return fmt.Errorf("could not connect to pcap-api %q", apiURL)
}
return nil
}
func (c *Client) CaptureRequest(ctx context.Context, cancel context.CancelCauseFunc, endpointRequest *EndpointRequest, options *CaptureOptions) error {
// perform capture request
err := c.ProcessCapture(ctx, endpointRequest, options, cancel)
if err != nil {
return fmt.Errorf("encountered error during request handling: %w", err)
}
// handle results of capture request
cause := context.Cause(ctx)
if cause != nil && !errors.Is(cause, context.Canceled) {
return fmt.Errorf("finished with error: %w", cause)
}
return nil
}
// ProcessCapture takes care of the complete lifecycle for a capture request.
// It writes the pcap-header to the outputFile, sends the CaptureRequest to the pcap-api
// and handles the cleanup after the capture is done.
//
// It then delegates writing individual packets and logging messages from the api to ReadCaptureResponse.
// logProgress is called in another goroutine to asynchronously announce on stderr how many bytes were
// already written to the outputFile.
func (c *Client) ProcessCapture(ctx context.Context, endpointRequest *EndpointRequest, options *CaptureOptions, cancel context.CancelCauseFunc) error {
logger := c.log.With(zap.String(LogKeyHandler, "ProcessCapture"))
if endpointRequest == nil {
return fmt.Errorf("endpoint request must not be nil: %w", errInvalidPayload)
}
if options == nil {
return fmt.Errorf("capture options request must not be nil: %w", errInvalidPayload)
}
// setup output/pcap-file
packetWriter := pcapgo.NewWriter(c.packetFile)
err := packetWriter.WriteFileHeader(options.SnapLen, layers.LinkTypeEthernet)
if err != nil {
return err
}
captureRequest := &CaptureRequest{
Operation: &CaptureRequest_Start{
Start: &StartCapture{
Request: endpointRequest,
Options: options,
},
},
}
// use a new context for the API stream to keep the context in client and the context in API decoupled.
c.stream, err = c.Capture(context.Background())
if err != nil {
return err
}
err = c.stream.Send(captureRequest)
if err != nil {
return err
}
done := c.ReadCaptureResponse(c.stream, packetWriter, cancel)
go c.logProgress(ctx, logger)
logger.Info("waiting for capture to finish")
select {
case <-ctx.Done():
// nothing to do, stream was terminated
case <-done:
cancel(nil)
// just to be sure that the error was already propagated
<-ctx.Done()
}
logger.Debug("syncing file to disk")
err = c.packetFile.Sync()
if err != nil {
return err
}
logger.Debug("closing file")
err = c.packetFile.Close()
if err != nil {
return err
}
return nil
}
// logsToStdout determines if the config logs data to stdout.
func logsToStdout(config zap.Config) bool {
for _, path := range config.OutputPaths {
// "stdout" has a special meaning in zap and refers to os.Stdout.
if path == "stdout" {
return true
}
}
return false
}
func (c *Client) StopRequest() {
if c.stream == nil {
c.log.Error("client not connected, could not stop")
return
}
if c.stopped {
return
}
err := c.stream.SendMsg(MakeStopRequest())
if err != nil {
c.log.Error("could not stop")
}
err = c.stream.CloseSend()
if err != nil {
c.log.Error("could not close send direction of client stream")
}
c.stopped = true
}
// ReadCaptureResponse reads CaptureResponse's from the api in a loop and delegates writing/logging messages & packets to WriteMessage / writePacket.
//
// It terminates if an error or clean stop-message is received.
func (c *Client) ReadCaptureResponse(stream API_CaptureClient, packetWriter *pcapgo.Writer, cancel context.CancelCauseFunc) chan struct{} {
logger := c.log.With(zap.String(LogKeyHandler, "ReadCaptureResponse"))
done := make(chan struct{})
go func() {
defer close(done)
for {
res, err := stream.Recv()
if errors.Is(err, io.EOF) {
logger.Info("clean stop, done")
cancel(nil)
return
}
code := status.Code(err)
if code != codes.OK {
err = fmt.Errorf("receive non-OK code: %v: %w", code, err)
cancel(err)
return
}
switch p := res.Payload.(type) {
case *CaptureResponse_Message:
c.messageWriter.WriteMessage(p.Message)
case *CaptureResponse_Packet:
writePacket(p.Packet, packetWriter)
}
}
}()
return done
}
// writePacket writes a Packet to the outputFile (in packetWriter).
func writePacket(packet *Packet, packetWriter *pcapgo.Writer) {
log := zap.L()
if log.Level().Enabled(zap.DebugLevel) {
log.Debug("received packet", zap.Int("bytes", len(packet.Data)), zap.Time("capture-timestamp", packet.Timestamp.AsTime()))
}
captureInfo := gopacket.CaptureInfo{
Timestamp: packet.Timestamp.AsTime(),
CaptureLength: len(packet.Data),
Length: int(packet.Length),
InterfaceIndex: 0,
AncillaryData: nil,
}
err := packetWriter.WritePacket(captureInfo, packet.Data)
if err != nil {
log.Error("writing packet to file failed", zap.Error(err))
}
}
// logProgress logs out the size of the outputFile every 5 seconds (see logProgressWait).
func (c *Client) logProgress(ctx context.Context, logger *zap.Logger) {
ticker := time.NewTicker(logProgressWait)
defer ticker.Stop()
for {
select {
case <-ticker.C:
info, err := c.packetFile.Stat()
if err != nil {
logger.Debug("could not inspect output file", zap.Error(err))
return
}
logger.Debug(fmt.Sprintf("%s bytes written to disk (total).", bytefmt.ByteSize(uint64(info.Size()))))
case <-ctx.Done():
return
}
}
}
// CheckAPIHandler checks if API is healthy and the given handler is available, if that's the case, the returned error will be nil.
func (c *Client) CheckAPIHandler(handler string) error {
ctx, cancel := context.WithTimeout(context.Background(), DefaultStatusTimeout)
defer cancel()
if c.cc == nil {
return ErrNotConnected
}
statusResponse, err := c.Status(ctx, &StatusRequest{})
if err != nil {
return fmt.Errorf("could not fetch api status: %w", err)
}
if !statusResponse.GetHealthy() {
return fmt.Errorf("pcap-api reported unhealthy status")
}
for _, resolverName := range statusResponse.Resolvers {
if resolverName == handler {
return nil
}
}
return fmt.Errorf("pcap-api does not support handler %v", handler)
}