-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
connector.go
263 lines (238 loc) · 7.6 KB
/
connector.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
package connector
import (
"context"
"errors"
"fmt"
"net/url"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/network"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
//go:generate mockery --quiet --name GatewayConnector --output ./mocks/ --case=underscore
//go:generate mockery --quiet --name Signer --output ./mocks/ --case=underscore
//go:generate mockery --quiet --name GatewayConnectorHandler --output ./mocks/ --case=underscore
// GatewayConnector is a component run by Nodes to connect to a set of Gateways.
type GatewayConnector interface {
job.ServiceCtx
network.ConnectionInitiator
SendToGateway(ctx context.Context, gatewayId string, msg *api.Message) error
}
// Signer implementation needs to be provided by a GatewayConnector user (node)
// in order to sign handshake messages with node's private key.
type Signer interface {
// Sign keccak256 hash of data.
Sign(data ...[]byte) ([]byte, error)
}
// GatewayConnector user (node) implements application logic in the Handler interface.
type GatewayConnectorHandler interface {
job.ServiceCtx
HandleGatewayMessage(ctx context.Context, gatewayId string, msg *api.Message)
}
type gatewayConnector struct {
services.StateMachine
config *ConnectorConfig
codec api.Codec
clock utils.Clock
nodeAddress []byte
signer Signer
handler GatewayConnectorHandler
gateways map[string]*gatewayState
urlToId map[string]string
closeWait sync.WaitGroup
shutdownCh services.StopChan
lggr logger.Logger
}
func (c *gatewayConnector) HealthReport() map[string]error {
m := map[string]error{c.Name(): c.Healthy()}
for _, g := range c.gateways {
services.CopyHealth(m, g.conn.HealthReport())
}
return m
}
func (c *gatewayConnector) Name() string { return c.lggr.Name() }
type gatewayState struct {
conn network.WSConnectionWrapper
config ConnectorGatewayConfig
url *url.URL
wsClient network.WebSocketClient
}
func NewGatewayConnector(config *ConnectorConfig, signer Signer, handler GatewayConnectorHandler, clock utils.Clock, lggr logger.Logger) (GatewayConnector, error) {
if config == nil || signer == nil || handler == nil || clock == nil || lggr == nil {
return nil, errors.New("nil dependency")
}
if len(config.DonId) == 0 || len(config.DonId) > int(network.HandshakeDonIdLen) {
return nil, errors.New("invalid DON ID")
}
addressBytes, err := utils.TryParseHex(config.NodeAddress)
if err != nil {
return nil, err
}
connector := &gatewayConnector{
config: config,
codec: &api.JsonRPCCodec{},
clock: clock,
nodeAddress: addressBytes,
signer: signer,
handler: handler,
shutdownCh: make(chan struct{}),
lggr: lggr.Named("GatewayConnector"),
}
gateways := make(map[string]*gatewayState)
urlToId := make(map[string]string)
for _, gw := range config.Gateways {
gw := gw
if _, exists := gateways[gw.Id]; exists {
return nil, fmt.Errorf("duplicate Gateway ID %s", gw.Id)
}
if _, exists := urlToId[gw.URL]; exists {
return nil, fmt.Errorf("duplicate Gateway URL %s", gw.URL)
}
parsedURL, err := url.Parse(gw.URL)
if err != nil {
return nil, err
}
gateway := &gatewayState{
conn: network.NewWSConnectionWrapper(lggr),
config: gw,
url: parsedURL,
wsClient: network.NewWebSocketClient(config.WsClientConfig, connector, lggr),
}
gateways[gw.Id] = gateway
urlToId[gw.URL] = gw.Id
}
connector.gateways = gateways
connector.urlToId = urlToId
return connector, nil
}
func (c *gatewayConnector) SendToGateway(ctx context.Context, gatewayId string, msg *api.Message) error {
data, err := c.codec.EncodeResponse(msg)
if err != nil {
return fmt.Errorf("error encoding response for gateway %s: %v", gatewayId, err)
}
gateway, ok := c.gateways[gatewayId]
if !ok {
return fmt.Errorf("invalid Gateway ID %s", gatewayId)
}
if gateway.conn == nil {
return fmt.Errorf("connector not started")
}
return gateway.conn.Write(ctx, websocket.BinaryMessage, data)
}
func (c *gatewayConnector) readLoop(gatewayState *gatewayState) {
ctx, cancel := c.shutdownCh.NewCtx()
defer cancel()
for {
select {
case <-c.shutdownCh:
c.closeWait.Done()
return
case item := <-gatewayState.conn.ReadChannel():
msg, err := c.codec.DecodeRequest(item.Data)
if err != nil {
c.lggr.Errorw("parse error when reading from Gateway", "id", gatewayState.config.Id, "err", err)
break
}
if err = msg.Validate(); err != nil {
c.lggr.Errorw("failed to validate message signature", "id", gatewayState.config.Id, "error", err)
break
}
c.handler.HandleGatewayMessage(ctx, gatewayState.config.Id, msg)
}
}
}
func (c *gatewayConnector) reconnectLoop(gatewayState *gatewayState) {
redialBackoff := utils.NewRedialBackoff()
ctx, cancel := c.shutdownCh.NewCtx()
defer cancel()
for {
conn, err := gatewayState.wsClient.Connect(ctx, gatewayState.url)
if err != nil {
c.lggr.Errorw("connection error", "url", gatewayState.url, "error", err)
} else {
c.lggr.Infow("connected successfully", "url", gatewayState.url)
closeCh := gatewayState.conn.Reset(conn)
<-closeCh
c.lggr.Infow("connection closed", "url", gatewayState.url)
// reset backoff
redialBackoff = utils.NewRedialBackoff()
}
select {
case <-c.shutdownCh:
c.closeWait.Done()
return
case <-time.After(redialBackoff.Duration()):
c.lggr.Info("reconnecting ...")
}
}
}
func (c *gatewayConnector) Start(ctx context.Context) error {
return c.StartOnce("GatewayConnector", func() error {
c.lggr.Info("starting gateway connector")
if err := c.handler.Start(ctx); err != nil {
return err
}
for _, gatewayState := range c.gateways {
gatewayState := gatewayState
if err := gatewayState.conn.Start(ctx); err != nil {
return err
}
c.closeWait.Add(2)
go c.readLoop(gatewayState)
go c.reconnectLoop(gatewayState)
}
return nil
})
}
func (c *gatewayConnector) Close() error {
return c.StopOnce("GatewayConnector", func() (err error) {
c.lggr.Info("closing gateway connector")
close(c.shutdownCh)
for _, gatewayState := range c.gateways {
gatewayState.conn.Close()
}
c.closeWait.Wait()
return c.handler.Close()
})
}
func (c *gatewayConnector) NewAuthHeader(url *url.URL) ([]byte, error) {
gatewayId, found := c.urlToId[url.String()]
if !found {
return nil, network.ErrAuthInvalidGateway
}
authHeaderElems := &network.AuthHeaderElems{
Timestamp: uint32(c.clock.Now().Unix()),
DonId: c.config.DonId,
GatewayId: gatewayId,
}
packedElems := network.PackAuthHeader(authHeaderElems)
signature, err := c.signer.Sign(packedElems)
if err != nil {
return nil, err
}
return append(packedElems, signature...), nil
}
func (c *gatewayConnector) ChallengeResponse(url *url.URL, challenge []byte) ([]byte, error) {
challengeElems, err := network.UnpackChallenge(challenge)
if err != nil {
return nil, err
}
if len(challengeElems.ChallengeBytes) < c.config.AuthMinChallengeLen {
return nil, network.ErrChallengeTooShort
}
gatewayId, found := c.urlToId[url.String()]
if !found || challengeElems.GatewayId != gatewayId {
return nil, network.ErrAuthInvalidGateway
}
nowTs := uint32(c.clock.Now().Unix())
ts := challengeElems.Timestamp
if ts < nowTs-c.config.AuthTimestampToleranceSec || nowTs+c.config.AuthTimestampToleranceSec < ts {
return nil, network.ErrAuthInvalidTimestamp
}
return c.signer.Sign(challenge)
}