forked from ivcosla/dmsg-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
317 lines (278 loc) · 7.67 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
package dmsg
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/dmsg/disc"
"github.com/SkycoinProject/dmsg/noise"
)
var log = logging.MustGetLogger("dmsg")
const (
clientReconnectInterval = 3 * time.Second
)
var (
// ErrNoSrv indicate that remote client does not have DelegatedServers in entry.
ErrNoSrv = errors.New("remote has no DelegatedServers")
// ErrClientClosed indicates that client is closed and not accepting new connections.
ErrClientClosed = errors.New("client closed")
// ErrClientAcceptMaxed indicates that the client cannot take in more accepts.
ErrClientAcceptMaxed = errors.New("client accepts buffer maxed")
)
// ClientOption represents an optional argument for Client.
type ClientOption func(c *Client) error
// SetLogger sets the internal logger for Client.
func SetLogger(log *logging.Logger) ClientOption {
return func(c *Client) error {
if log == nil {
return errors.New("nil logger set")
}
c.log = log
return nil
}
}
// Client implements transport.Factory
type Client struct {
log *logging.Logger
pk cipher.PubKey
sk cipher.SecKey
dc disc.APIClient
conns map[cipher.PubKey]*ClientConn // conns with messaging servers. Key: pk of server
mx sync.RWMutex
pm *PortManager
// accept map[uint16]chan *transport
done chan struct{}
once sync.Once
}
// NewClient creates a new Client.
func NewClient(pk cipher.PubKey, sk cipher.SecKey, dc disc.APIClient, opts ...ClientOption) *Client {
c := &Client{
log: logging.MustGetLogger("dmsg_client"),
pk: pk,
sk: sk,
dc: dc,
conns: make(map[cipher.PubKey]*ClientConn),
pm: newPortManager(),
// accept: make(chan *transport, AcceptBufferSize),
// accept: make(map[uint16]chan *transport),
done: make(chan struct{}),
}
for _, opt := range opts {
if err := opt(c); err != nil {
panic(err)
}
}
return c
}
func (c *Client) updateDiscEntry(ctx context.Context) error {
srvPKs := make([]cipher.PubKey, 0, len(c.conns))
for pk := range c.conns {
srvPKs = append(srvPKs, pk)
}
entry, err := c.dc.Entry(ctx, c.pk)
if err != nil {
entry = disc.NewClientEntry(c.pk, 0, srvPKs)
if err := entry.Sign(c.sk); err != nil {
return err
}
return c.dc.SetEntry(ctx, entry)
}
entry.Client.DelegatedServers = srvPKs
c.log.Infoln("updatingEntry:", entry)
return c.dc.UpdateEntry(ctx, c.sk, entry)
}
func (c *Client) setConn(ctx context.Context, conn *ClientConn) {
c.mx.Lock()
c.conns[conn.remoteSrv] = conn
if err := c.updateDiscEntry(ctx); err != nil {
c.log.WithError(err).Warn("updateEntry: failed")
}
c.mx.Unlock()
}
func (c *Client) delConn(ctx context.Context, pk cipher.PubKey) {
c.mx.Lock()
delete(c.conns, pk)
if err := c.updateDiscEntry(ctx); err != nil {
c.log.WithError(err).Warn("updateEntry: failed")
}
c.mx.Unlock()
}
func (c *Client) getConn(pk cipher.PubKey) (*ClientConn, bool) {
c.mx.RLock()
l, ok := c.conns[pk]
c.mx.RUnlock()
return l, ok
}
func (c *Client) connCount() int {
c.mx.RLock()
n := len(c.conns)
c.mx.RUnlock()
return n
}
// InitiateServerConnections initiates connections with dms_servers.
func (c *Client) InitiateServerConnections(ctx context.Context, min int) error {
if min == 0 {
return nil
}
entries, err := c.findServerEntries(ctx)
if err != nil {
return err
}
c.log.Info("found dmsg_server entries:", entries)
if err := c.findOrConnectToServers(ctx, entries, min); err != nil {
return err
}
return nil
}
func (c *Client) findServerEntries(ctx context.Context) ([]*disc.Entry, error) {
for {
entries, err := c.dc.AvailableServers(ctx)
if err != nil || len(entries) == 0 {
select {
case <-ctx.Done():
return nil, fmt.Errorf("dmsg_servers are not available: %s", err)
default:
retry := time.Second
c.log.WithError(err).Warnf("no dmsg_servers found: trying again in %v...", retry)
time.Sleep(retry)
continue
}
}
return entries, nil
}
}
func (c *Client) findOrConnectToServers(ctx context.Context, entries []*disc.Entry, min int) error {
for _, entry := range entries {
_, err := c.findOrConnectToServer(ctx, entry.Static)
if err != nil {
c.log.Warnf("findOrConnectToServers: failed to find/connect to server %s: %s", entry.Static, err)
continue
}
c.log.Infof("findOrConnectToServers: found/connected to server %s", entry.Static)
if c.connCount() >= min {
return nil
}
}
return fmt.Errorf("findOrConnectToServers: all servers failed")
}
func (c *Client) findOrConnectToServer(ctx context.Context, srvPK cipher.PubKey) (*ClientConn, error) {
if conn, ok := c.getConn(srvPK); ok {
return conn, nil
}
entry, err := c.dc.Entry(ctx, srvPK)
if err != nil {
return nil, err
}
if entry.Server == nil {
return nil, errors.New("entry is of client instead of server")
}
tcpConn, err := net.Dial("tcp", entry.Server.Address)
if err != nil {
return nil, err
}
ns, err := noise.New(noise.HandshakeXK, noise.Config{
LocalPK: c.pk,
LocalSK: c.sk,
RemotePK: srvPK,
Initiator: true,
})
if err != nil {
return nil, err
}
nc, err := noise.WrapConn(tcpConn, ns, TransportHandshakeTimeout)
if err != nil {
return nil, err
}
conn := NewClientConn(c.log, nc, c.pk, srvPK, c.pm)
if err := conn.readOK(); err != nil {
return nil, err
}
c.setConn(ctx, conn)
go func() {
err := conn.Serve(ctx)
conn.log.WithField("reason", err).WithField("remoteServer", srvPK).Debug("connection with server closed")
c.delConn(ctx, srvPK)
// reconnect logic.
retryServerConnect:
select {
case <-c.done:
case <-ctx.Done():
case <-time.After(clientReconnectInterval):
conn.log.WithField("remoteServer", srvPK).Warn("Reconnecting")
if _, err := c.findOrConnectToServer(ctx, srvPK); err != nil {
conn.log.WithError(err).WithField("remoteServer", srvPK).Warn("ReconnectionFailed")
goto retryServerConnect
}
conn.log.WithField("remoteServer", srvPK).Warn("ReconnectionSucceeded")
}
}()
return conn, nil
}
// Listen creates a listener on a given port, adds it to port manager and returns the listener.
func (c *Client) Listen(port uint16) (*Listener, error) {
l, ok := c.pm.NewListener(c.pk, port)
if !ok {
return nil, errors.New("port is busy")
}
return l, nil
}
// Dial dials a transport to remote dmsg_client.
func (c *Client) Dial(ctx context.Context, remote cipher.PubKey, port uint16) (*Transport, error) {
entry, err := c.dc.Entry(ctx, remote)
if err != nil {
return nil, fmt.Errorf("get entry failure: %s", err)
}
if entry.Client == nil {
return nil, errors.New("entry is of server instead of client")
}
if len(entry.Client.DelegatedServers) == 0 {
return nil, ErrNoSrv
}
for _, srvPK := range entry.Client.DelegatedServers {
conn, err := c.findOrConnectToServer(ctx, srvPK)
if err != nil {
c.log.WithError(err).Warn("failed to connect to server")
continue
}
return conn.DialTransport(ctx, remote, port)
}
return nil, errors.New("failed to find dmsg_servers for given client pk")
}
// Addr returns the local dmsg_client's public key.
func (c *Client) Addr() net.Addr {
return Addr{
PK: c.pk,
}
}
// Type returns the transport type.
func (c *Client) Type() string {
return Type
}
// Close closes the dmsg_client and associated connections.
// TODO(evaninjin): proper error handling.
func (c *Client) Close() error {
if c == nil {
return nil
}
c.once.Do(func() {
close(c.done)
c.mx.Lock()
for _, conn := range c.conns {
if err := conn.Close(); err != nil {
log.WithField("reason", err).Debug("Connection closed")
}
}
c.conns = make(map[cipher.PubKey]*ClientConn)
c.mx.Unlock()
c.pm.mu.Lock()
defer c.pm.mu.Unlock()
for _, lis := range c.pm.listeners {
lis.close()
}
})
return nil
}