-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
generic.go
executable file
·65 lines (53 loc) · 2 KB
/
generic.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
package generic
import (
"context"
"net"
"go.keploy.io/server/v2/pkg/core/proxy/integrations"
"go.keploy.io/server/v2/pkg/core/proxy/util"
"go.keploy.io/server/v2/pkg/models"
"go.keploy.io/server/v2/utils"
"go.uber.org/zap"
)
func init() {
integrations.Register("generic", NewGeneric)
}
type Generic struct {
logger *zap.Logger
}
func NewGeneric(logger *zap.Logger) integrations.Integrations {
return &Generic{
logger: logger,
}
}
func (g *Generic) MatchType(_ context.Context, _ []byte) bool {
// generic is checked explicitly in the proxy
return false
}
func (g *Generic) RecordOutgoing(ctx context.Context, src net.Conn, dst net.Conn, mocks chan<- *models.Mock, opts models.OutgoingOptions) error {
logger := g.logger.With(zap.Any("Client IP Address", src.RemoteAddr().String()), zap.Any("Client ConnectionID", ctx.Value(models.ClientConnectionIDKey).(string)), zap.Any("Destination ConnectionID", ctx.Value(models.DestConnectionIDKey).(string)))
reqBuf, err := util.ReadInitialBuf(ctx, logger, src)
if err != nil {
utils.LogError(logger, err, "failed to read the initial generic message")
return err
}
err = encodeGeneric(ctx, logger, reqBuf, src, dst, mocks, opts)
if err != nil {
utils.LogError(logger, err, "failed to encode the generic message into the yaml")
return err
}
return nil
}
func (g *Generic) MockOutgoing(ctx context.Context, src net.Conn, dstCfg *integrations.ConditionalDstCfg, mockDb integrations.MockMemDb, opts models.OutgoingOptions) error {
logger := g.logger.With(zap.Any("Client IP Address", src.RemoteAddr().String()), zap.Any("Client ConnectionID", util.GetNextID()), zap.Any("Destination ConnectionID", util.GetNextID()))
reqBuf, err := util.ReadInitialBuf(ctx, logger, src)
if err != nil {
utils.LogError(logger, err, "failed to read the initial generic message")
return err
}
err = decodeGeneric(ctx, logger, reqBuf, src, dstCfg, mockDb, opts)
if err != nil {
utils.LogError(logger, err, "failed to decode the generic message")
return err
}
return nil
}