-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
434 lines (367 loc) · 9.68 KB
/
server.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package dmsg
import (
"context"
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/disc"
"github.com/skycoin/dmsg/noise"
)
// ErrListenerAlreadyWrappedToNoise occurs when the provided net.Listener is already wrapped with noise.Listener
var ErrListenerAlreadyWrappedToNoise = errors.New("listener is already wrapped to *noise.Listener")
// NextConn provides information on the next connection.
type NextConn struct {
conn *ServerConn
id uint16
}
func (r *NextConn) writeFrame(ft FrameType, p []byte) error {
if err := writeFrame(r.conn.Conn, MakeFrame(ft, r.id, p)); err != nil {
go func() {
if err := r.conn.Close(); err != nil {
log.WithError(err).Warn("Failed to close connection")
}
}()
return err
}
return nil
}
// ServerConn is a connection between a dmsg.Server and a dmsg.Client from a server's perspective.
type ServerConn struct {
log *logging.Logger
net.Conn
remoteClient cipher.PubKey
nextRespID uint16
nextConns map[uint16]*NextConn
mx sync.RWMutex
}
// NewServerConn creates a new connection from the perspective of a dms_server.
func NewServerConn(log *logging.Logger, conn net.Conn, remoteClient cipher.PubKey) *ServerConn {
return &ServerConn{
log: log,
Conn: conn,
remoteClient: remoteClient,
nextRespID: randID(false),
nextConns: make(map[uint16]*NextConn),
}
}
func (c *ServerConn) delNext(id uint16) {
c.mx.Lock()
delete(c.nextConns, id)
c.mx.Unlock()
}
func (c *ServerConn) setNext(id uint16, r *NextConn) {
c.mx.Lock()
c.nextConns[id] = r
c.mx.Unlock()
}
func (c *ServerConn) getNext(id uint16) (*NextConn, bool) {
c.mx.RLock()
r := c.nextConns[id]
c.mx.RUnlock()
return r, r != nil
}
func (c *ServerConn) addNext(ctx context.Context, r *NextConn) (uint16, error) {
c.mx.Lock()
defer c.mx.Unlock()
for {
if r := c.nextConns[c.nextRespID]; r == nil {
break
}
c.nextRespID += 2
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
}
}
id := c.nextRespID
c.nextRespID = id + 2
c.nextConns[id] = r
return id, nil
}
// PK returns the remote dms_client's public key.
func (c *ServerConn) PK() cipher.PubKey {
return c.remoteClient
}
type getConnFunc func(pk cipher.PubKey) (*ServerConn, bool)
// Serve handles (and forwards when necessary) incoming frames.
func (c *ServerConn) Serve(ctx context.Context, getConn getConnFunc) (err error) {
log := c.log.WithField("srcClient", c.remoteClient)
// Only manually close the underlying net.Conn when the done signal is context-initiated.
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-done:
case <-ctx.Done():
if err := c.Conn.Close(); err != nil {
log.WithError(err).Warn("failed to close underlying connection")
}
}
}()
defer func() {
// Send CLOSE frames to all transports which are established with this dmsg.Client
// This ensures that all parties are informed about the transport closing.
c.mx.Lock()
for _, conn := range c.nextConns {
why := byte(0)
if err := conn.writeFrame(CloseType, []byte{why}); err != nil {
log.WithError(err).Warnf("failed to write frame: %s", err)
}
}
c.mx.Unlock()
log.WithError(err).WithField("connCount", decrementServeCount()).Infoln("ClosingConn")
if err := c.Conn.Close(); err != nil {
log.WithError(err).Warn("Failed to close connection")
}
}()
log.WithField("connCount", incrementServeCount()).Infoln("ServingConn")
err = c.writeOK()
if err != nil {
return fmt.Errorf("sending OK failed: %s", err)
}
for {
f, err := readFrame(c.Conn)
if err != nil {
return fmt.Errorf("read failed: %s", err)
}
log = log.WithField("received", f)
ft, id, p := f.Disassemble()
switch ft {
case RequestType:
ctx, cancel := context.WithTimeout(ctx, TransportHandshakeTimeout)
_, why, ok := c.handleRequest(ctx, getConn, id, p)
cancel()
if !ok {
log.Debugln("FrameRejected: Erroneous request or unresponsive dstClient.")
if err := c.delChan(id, why); err != nil {
return err
}
}
log.Debugln("FrameForwarded")
case AcceptType, FwdType, AckType, CloseType:
next, why, ok := c.forwardFrame(ft, id, p)
if !ok {
log.Debugln("FrameRejected: Failed to forward to dstClient.")
// Delete channel (and associations) on failure.
if err := c.delChan(id, why); err != nil {
return err
}
continue
}
log.Debugln("FrameForwarded")
// On success, if Close frame, delete the associations.
if ft == CloseType {
c.delNext(id)
next.conn.delNext(next.id)
}
default:
log.Debugln("FrameRejected: Unknown frame type.")
// Unknown frame type.
return errors.New("unknown frame of type received")
}
}
}
func (c *ServerConn) delChan(id uint16, why byte) error {
c.delNext(id)
if err := writeFrame(c.Conn, MakeFrame(CloseType, id, []byte{why})); err != nil {
return fmt.Errorf("failed to write frame: %s", err)
}
return nil
}
func (c *ServerConn) writeOK() error {
if err := writeFrame(c.Conn, MakeFrame(OkType, 0, nil)); err != nil {
return err
}
return nil
}
// nolint:unparam
func (c *ServerConn) forwardFrame(ft FrameType, id uint16, p []byte) (*NextConn, byte, bool) {
next, ok := c.getNext(id)
if !ok {
return next, 0, false
}
if err := next.writeFrame(ft, p); err != nil {
return next, 0, false
}
return next, 0, true
}
// nolint:unparam
func (c *ServerConn) handleRequest(ctx context.Context, getLink getConnFunc, id uint16, p []byte) (*NextConn, byte, bool) {
initPK, respPK, ok := splitPKs(p)
if !ok || initPK != c.PK() {
return nil, 0, false
}
respL, ok := getLink(respPK)
if !ok {
return nil, 0, false
}
// set next relations.
respID, err := respL.addNext(ctx, &NextConn{conn: c, id: id})
if err != nil {
return nil, 0, false
}
next := &NextConn{conn: respL, id: respID}
c.setNext(id, next)
// forward to responding client.
if err := next.writeFrame(RequestType, p); err != nil {
return next, 0, false
}
return next, 0, true
}
// Server represents a dms_server.
type Server struct {
log *logging.Logger
pk cipher.PubKey
sk cipher.SecKey
dc disc.APIClient
addr string
lis net.Listener
conns map[cipher.PubKey]*ServerConn
mx sync.RWMutex
wg sync.WaitGroup
lisDone int32
doneOnce sync.Once
}
// NewServer creates a new dms_server.
func NewServer(pk cipher.PubKey, sk cipher.SecKey, addr string, l net.Listener, dc disc.APIClient) (*Server, error) {
if addr == "" {
addr = l.Addr().String()
}
if _, ok := l.(*noise.Listener); ok {
return nil, ErrListenerAlreadyWrappedToNoise
}
return &Server{
log: logging.MustGetLogger("dms_server"),
pk: pk,
sk: sk,
addr: addr,
lis: noise.WrapListener(l, pk, sk, false, noise.HandshakeXK),
dc: dc,
conns: make(map[cipher.PubKey]*ServerConn),
}, nil
}
// SetLogger set's the logger.
func (s *Server) SetLogger(log *logging.Logger) {
s.log = log
}
// Addr returns the server's listening address.
func (s *Server) Addr() string {
return s.addr
}
func (s *Server) setConn(l *ServerConn) {
s.mx.Lock()
s.conns[l.remoteClient] = l
s.mx.Unlock()
}
func (s *Server) delConn(pk cipher.PubKey) {
s.mx.Lock()
delete(s.conns, pk)
s.mx.Unlock()
}
func (s *Server) getConn(pk cipher.PubKey) (*ServerConn, bool) {
s.mx.RLock()
l, ok := s.conns[pk]
s.mx.RUnlock()
return l, ok
}
func (s *Server) connCount() int {
s.mx.RLock()
n := len(s.conns)
s.mx.RUnlock()
return n
}
func (s *Server) close() (closed bool, err error) {
s.doneOnce.Do(func() {
closed = true
atomic.StoreInt32(&s.lisDone, 1)
if err = s.lis.Close(); err != nil {
return
}
s.mx.Lock()
s.conns = make(map[cipher.PubKey]*ServerConn)
s.mx.Unlock()
})
return closed, err
}
// Close closes the dms_server.
func (s *Server) Close() error {
closed, err := s.close()
if !closed {
return errors.New("server is already closed")
}
if err != nil {
return err
}
s.wg.Wait()
return nil
}
func (s *Server) isLisClosed() bool {
return atomic.LoadInt32(&s.lisDone) == 1
}
// Serve serves the dmsg_server.
func (s *Server) Serve() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := s.retryUpdateEntry(ctx, TransportHandshakeTimeout); err != nil {
return fmt.Errorf("updating server's client entry failed with: %s", err)
}
s.log.Infof("serving: pk(%s) addr(%s)", s.pk, s.addr)
for {
rawConn, err := s.lis.Accept()
if err != nil {
// if the listener is closed, it means that this error is not interesting
// for the outer client
if s.isLisClosed() {
return nil
}
return err
}
s.log.Infof("newConn: %v", rawConn.RemoteAddr())
conn := NewServerConn(s.log, rawConn, rawConn.RemoteAddr().(*noise.Addr).PK)
s.setConn(conn)
s.wg.Add(1)
go func() {
defer s.wg.Done()
err := conn.Serve(ctx, s.getConn)
s.log.Infof("connection with client %s closed: error(%v)", conn.PK(), err)
s.delConn(conn.PK())
}()
}
}
func (s *Server) updateDiscEntry(ctx context.Context) error {
entry, err := s.dc.Entry(ctx, s.pk)
if err != nil {
entry = disc.NewServerEntry(s.pk, 0, s.addr, 10)
if err := entry.Sign(s.sk); err != nil {
return err
}
return s.dc.SetEntry(ctx, entry)
}
entry.Server.Address = s.Addr()
s.log.Infoln("updatingEntry:", entry)
return s.dc.UpdateEntry(ctx, s.sk, entry)
}
func (s *Server) retryUpdateEntry(ctx context.Context, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
for {
if err := s.updateDiscEntry(ctx); err != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
retry := time.Second
s.log.WithError(err).Warnf("updateEntry failed: trying again in %d second...", retry)
time.Sleep(retry)
continue
}
}
return nil
}
}