-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
688 lines (583 loc) · 15.6 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package zwibserve
import (
"encoding/base64"
"errors"
"fmt"
"log"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
"github.com/golang-jwt/jwt/v4"
)
// Default maximum message size
const maxMessageSize = 100 * 1024
// nextClientNumber is just a number identifing the client for logging.
var nextClientNumber int64
type client struct {
id string
ws *websocket.Conn
protocolVersion uint16
docID string
// last document range sent in an append message
lastEnd uint64
// for tokens
userID string
writePermission bool
adminPermission bool
db DocumentDB
hub *hub
// A single thread writes to the socket. When another thread wants to
// send, it appends to the queue and signals the condition variable.
// To close the socket, we set closed = true and signal the condition.
wakeup *sync.Cond
mutex sync.Mutex
closed bool
// queued messages to send, other than key-information
queued [][]byte
// queued keys to update to client
keys []Key
// maximum message size requested by client.
maxSize int
}
func idstr(id int64) string {
return fmt.Sprintf("%x", id)
}
// Takes over the connection and runs the client, Responsible for closing the socket.
func runClient(hub *hub, db DocumentDB, ws *websocket.Conn) {
c := &client{
ws: ws,
db: db,
hub: hub,
id: idstr(atomic.AddInt64(&nextClientNumber, 1)),
maxSize: maxMessageSize,
}
c.wakeup = sync.NewCond(&c.mutex)
go c.writeThread()
// wait up to 30 seconds for init message
message, err := readMessageWithTimeout(c.ws, 30*time.Second)
if err != nil {
ws.Close()
log.Printf("%s: error waiting for init message: %v", c.id, err)
return
}
if len(message) < 2 {
ws.Close()
log.Printf("Received message is too short. Close connection")
return
}
if message[0] == serverIdentificationMessageType {
hub.swarm.HandleIncomingConnection(ws, message)
return
}
defer func() {
c.mutex.Lock()
c.closed = true
c.wakeup.Signal()
c.mutex.Unlock()
}()
if !c.processInitMessage(message) {
return
}
log.Printf("Client %s connected", c.id)
hub.addClient(c.docID, c)
defer hub.RemoveClient(c.docID, c.id)
sessionKeys, _ := c.db.GetDocumentKeys(c.docID)
c.notifyKeysUpdated(c.hub.getClientKeys(c.docID))
c.notifyKeysUpdated(sessionKeys)
sessionKeys = nil
for {
message, err = readMessage(c.ws)
if err != nil {
log.Printf("client for %s disconnected", c.docID)
break
}
if message[0] == 0x02 || message[0] == 0x05 {
if !c.processAppend(message) {
break
}
} else if message[0] == 0x03 {
if !c.processSetKey(message) {
break
}
} else if message[0] == 0x04 {
if !c.processBroadcast(message) {
break
}
} else {
log.Printf("client %v sent unexpected message type %v", c.id, message[0])
}
}
}
func (c *client) writeThread() {
defer func() {
err := recover()
if err != nil {
log.Printf("Handling panic: %v", err)
c.ws.Close()
}
}()
closed := false
for !closed {
c.mutex.Lock()
for len(c.queued) == 0 && len(c.keys) == 0 && !c.closed {
c.wakeup.Wait()
}
messages := c.queued
keys := c.keys
closed = c.closed
c.queued = nil
c.keys = nil
c.mutex.Unlock()
// send all the queued messages.
for _, message := range messages {
c.sendMessage(message)
}
if len(keys) > 0 {
info := keyInformationMessage{
MessageType: keyInformationMessageType,
}
for _, k := range keys {
info.Keys = append(info.Keys, keyInformation{
Version: uint32(k.Version),
NameLength: uint32(len(k.Name)),
Name: k.Name,
ValueLength: uint32(len(k.Value)),
Value: k.Value,
})
}
c.sendMessage(encode(nil, info))
}
}
c.ws.Close()
}
// Reads a complete message, taking into account the MORE byte to
// join continuation messages together.
func readMessage(conn *websocket.Conn) ([]uint8, error) {
// only to be called from runClient
var buffer []byte
for {
_, p, err := conn.ReadMessage()
if err != nil {
return nil, err
}
if len(p) < 2 {
log.Panicf("message too short")
}
if buffer == nil {
// first message
buffer = append(buffer, p...)
} else if p[0] == continuationMessageType {
// continuation message
buffer = append(buffer, p[2:]...)
} else {
log.Panicf("expected continuation message")
}
if p[1] == 0 {
break
}
}
return buffer, nil
}
func readMessageWithTimeout(conn *websocket.Conn, timeout time.Duration) ([]uint8, error) {
// wait up to 'timeout' seconds for init message
conn.SetReadDeadline(time.Now().Add(timeout))
msg, err := readMessage(conn)
conn.SetReadDeadline(time.Time{})
return msg, err
}
func sendMessage(conn *websocket.Conn, data []byte, maxSize int) {
send := len(data)
if send > maxSize {
send = maxSize
data[1] = 1
} else {
data[1] = 0
}
// send first part
err := conn.WriteMessage(websocket.BinaryMessage, data[:send])
if err != nil {
log.Printf("Got ERROR writing to socket: %v", err)
}
data = data[send:]
for len(data) > 0 {
log.Printf("Sent %d/%d bytes", send, len(data))
writer, err := conn.NextWriter(websocket.BinaryMessage)
if err != nil {
log.Panic(err)
}
send := len(data)
more := byte(0)
if send > maxSize-2 {
send = maxSize - 2
more = 1
}
writer.Write([]byte{0xff, more})
writer.Write(data[:send])
writer.Close()
data = data[send:]
}
}
// Writes a complete message, respecting maximum message size and breaking it into chunks
// if necessary. MUST ONLY BE CALLED BY WRITETHREAD
func (c *client) sendMessage(data []byte) {
sendMessage(c.ws, data, c.maxSize)
}
func (c *client) enqueue(message interface{}) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.queued = append(c.queued, encode(nil, message))
c.wakeup.Signal()
}
type errorCode uint16
const (
errorUnspecified errorCode = 0
errorDoesNotExist errorCode = 1
errorInvalidOffset errorCode = 3
errorAccessDenied errorCode = 4
)
var errorStrings = []string{
"unspecified",
"does not exist",
"already exists",
"invalid offset",
"access denied",
}
func (c *client) enqueueError(code errorCode, text string) {
log.Printf("Client %v error: %s", c.id, text)
if text == "" && int(code) < len(errorStrings) {
text = errorStrings[code]
}
c.enqueue(errorMessage{
MessageType: 0x80,
ErrorCode: uint16(code),
Description: text,
})
}
func (c *client) enqueueAppend(data []byte, offset uint64) {
if offset < c.lastEnd {
return
}
c.lastEnd = offset + uint64(len(data))
//log.Printf("Append: %d bytes at offset %d", len(data), offset)
if c.protocolVersion < 3 {
c.enqueue(appendMessageV2{
MessageType: appendV2MessageType,
Offset: offset,
Data: data,
})
return
}
c.enqueue(appendMessage{
MessageType: appendMessageType,
Generation: 0, // Future feature
Offset: offset,
Data: data,
})
}
func (c *client) enqueueBroadcast(data []byte) {
c.enqueue(broadcastMessage{
MessageType: 0x04,
DataLength: uint32(len(data)),
Data: data,
})
}
func (c *client) enqueueAckNack(ack uint16, length uint64) {
c.enqueue(ackNackMessage{
MessageType: 0x81,
Ack: ack,
Offset: length,
})
}
func (c *client) enqueueSetKeyAckNack(ack bool, requestID uint16) {
var Ack uint16
if ack {
log.Printf("Client %v << KEY ACK", c.id)
Ack = 0x01
} else {
log.Printf("Client %v << KEY NACK", c.id)
Ack = 0x00
}
c.enqueue(setKeyAckNackMessage{
MessageType: 0x83,
Ack: Ack,
RequestID: requestID,
})
}
// Decode both the V2 and V3 version into the V3 version.
func decodeInitMessage(m *initMessage, data []uint8) error {
if len(data) > 4 && data[0] == initMessageType &&
data[2] == 0 && data[3] == 2 {
// protocol version 2
var m2 initMessageV2
err := decode(&m2, data)
if err != nil {
return err
}
m.MessageType = m2.MessageType
m.More = m2.More
m.ProtocolVersion = m2.ProtocolVersion
m.MaxMessageSize = m2.MaxMessageSize
m.CreationMode = m2.CreationMode
m.Offset = m2.Offset
m.DocIDLength = uint32(m2.DocIDLength)
m.DocID = m2.DocID
m.Data = m2.Data
return nil
}
return decode(m, data)
}
// decode both append and append_v2 into the append message
func decodeAppendMessage(m *appendMessage, data []uint8) error {
if data[0] == appendV2MessageType {
var m2 appendMessageV2
err := decode(&m2, data)
if err != nil {
return err
}
m.MessageType = m2.MessageType
m.More = m2.More
m.Offset = m2.Offset
m.Data = m2.Data
return nil
}
return decode(m, data)
}
func (c *client) processInitMessage(data []uint8) bool {
var m initMessage
err := decodeInitMessage(&m, data)
if err != nil {
log.Printf("Client %v: %v", c.id, err)
return false
}
if m.MessageType != 0x01 {
log.Printf("Client %v: ERROR Expected INIT message", c.id)
return false
}
if m.ProtocolVersion != 2 && m.ProtocolVersion != 3 {
log.Printf("Client %v: protocol versiom must be 2", c.id)
return false
}
c.protocolVersion = m.ProtocolVersion
if m.MaxMessageSize != 0 {
c.maxSize = int(m.MaxMessageSize)
}
errorCodeOnMissing := errorDoesNotExist
// check if its a token
realDocID, userID, permissions, err := c.db.GetToken(m.DocID)
if err == ErrMissing && c.hub.jwtKey != "" {
// interpret as JWT token
realDocID, userID, permissions, err = decodeJWT(c.hub.jwtKey, c.hub.keyIsBase64, m.DocID)
}
if err == nil {
log.Printf("Token %s maps to doc %s", m.DocID, realDocID)
c.docID = realDocID
c.writePermission = strings.Contains(permissions, "w")
c.adminPermission = strings.Contains(permissions, "a")
c.userID = userID
if !strings.Contains(permissions, "r") ||
m.CreationMode == AlwaysCreate && !c.writePermission {
c.enqueueError(errorAccessDenied, "")
return false
}
if !c.writePermission && m.CreationMode == PossiblyCreate {
m.CreationMode = NeverCreate
errorCodeOnMissing = errorAccessDenied
}
} else if err == ErrMissing && c.hub.jwtKey == "" {
c.docID = m.DocID
c.writePermission = true
} else if (err == ErrMissing || err == errTokenExpired || err == errSignatureInvalid) && c.hub.jwtKey != "" {
c.enqueueError(0x0004, "access denied")
return false
} else {
c.enqueueError(0, err.Error())
return false
}
initialData := m.Data
// look up document id
// if the document exists and create mode is ALWAYS_CREATE, then send error code ALREADY_EXISTS
// if the document does not exist and create mode is NEVER_CREATE then send error code DOES NOT EXIST
log.Printf("client %v looks for document %s", c.id, c.docID)
doc, created, err := c.db.GetDocument(c.docID, CreateMode(m.CreationMode), initialData)
if err != nil {
switch err {
case ErrExists:
c.enqueueError(0x0002, "already exists")
case ErrMissing:
c.enqueueError(errorCodeOnMissing, "")
default:
c.enqueueError(0, err.Error())
}
return false
}
offset := int(m.Offset)
if created {
offset = len(doc)
}
// if the document exists and its size is < bytesSynced, then send error code INVALID_OFFSET
if len(doc) < offset {
c.enqueueError(0x0003, "invalid offset")
return false
}
// note that at least broadcast m is always sent, even if empty.
c.enqueueAppend(doc[offset:], uint64(offset))
return true
}
type claims struct {
jwt.StandardClaims
UserID string `json:"u"`
Permissions string `json:"p"`
}
var errTokenExpired error
var errSignatureInvalid error
func init() {
errTokenExpired = errors.New("token expired")
errSignatureInvalid = errors.New("signature invalid")
}
func decodeJWT(jwtKey string, keyIsBase64 bool, tokenString string) (realDocID string, userID string, permissions string, err error) {
// decode JWT token and verify signature using JSON Web Keyset
// pass your custom claims to the parser function
var token *jwt.Token
token, err = jwt.ParseWithClaims(tokenString, &claims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("unexpected signing method")
}
var key []byte
var err error
if keyIsBase64 {
key, err = base64.StdEncoding.DecodeString(jwtKey)
} else {
key = []byte(jwtKey)
}
return key, err
})
// type-assert `Claims` into a variable of the appropriate type
if err == nil {
myClaims := token.Claims.(*claims)
now := time.Now().Unix()
if myClaims.ExpiresAt <= now {
log.Printf("User's JWT Token has expired. Token: %d Now is: %d", myClaims.ExpiresAt, now)
err = ErrMissing
}
realDocID = myClaims.Subject
userID = myClaims.UserID
permissions = myClaims.Permissions
} else {
bits := err.(*jwt.ValidationError).Errors
if bits&jwt.ValidationErrorExpired != 0 {
err = errTokenExpired
} else if bits&jwt.ValidationErrorSignatureInvalid != 0 {
err = errSignatureInvalid
}
}
return
}
func (c *client) processAppend(data []uint8) bool {
var m appendMessage
err := decodeAppendMessage(&m, data)
if err != nil {
log.Printf("Client %v: %v", c.id, err)
return false
}
if !c.writePermission {
log.Printf("Nack. no permission to write")
m.Data = nil
}
// attempt to append to document
newLength, err := c.db.AppendDocument(c.docID, m.Offset, m.Data)
if err == nil && c.writePermission {
c.enqueueAckNack(0x01, newLength)
c.lastEnd = newLength
c.hub.Append(c.docID, c.id, m.Offset, m.Data)
} else if err == nil && !c.writePermission {
c.enqueueAckNack(0x02, newLength)
} else if err == ErrConflict {
//log.Printf("Nack. offset should be %d not %d", newLength, m.Offset)
c.enqueueAckNack(0x00, newLength)
} else if err == ErrMissing {
log.Printf("ErrMissing during append: %s does not exist", c.docID)
c.enqueueError(0x0001, "does not exist")
} else {
log.Panic(err)
}
return true
}
func (c *client) processSetKey(data []uint8) bool {
var m setKeyMessage
err := decode(&m, data)
if err != nil {
log.Printf("Client %v: %v", c.id, err)
return false
}
var ack bool
if strings.HasPrefix(m.Name, "admin:") && !c.adminPermission {
log.Printf("Client %v: Tried to set admin: key but lacks permissions.", c.id)
} else if m.Lifetime == 0x00 {
ack = c.hub.SetClientKey(c.docID, c.id, int(m.OldVersion), int(m.NewVersion), m.Name, m.Value)
} else {
key := Key{int(m.NewVersion), m.Name, m.Value}
ack = nil == c.db.SetDocumentKey(c.docID, int(m.OldVersion), key)
if ack {
c.hub.SetSessionKey(c.docID, c.id, key)
}
}
if !ack || m.OldVersion != m.NewVersion {
c.enqueueSetKeyAckNack(ack, m.RequestID)
}
return true
}
func (c *client) processBroadcast(data []uint8) bool {
var m broadcastMessage
err := decode(&m, data)
if err != nil {
log.Printf("Client %v: %v", c.id, err)
return false
}
// attempt to append to document
c.hub.Broadcast(c.docID, c.id, m.Data)
return true
}
func (c *client) notifyKeysUpdated(keys []Key) {
// a key has been updated. By the time we get around to sending it, it may have been updated
// again, so just store the fact that it needs to be sent for the writeThread
c.mutex.Lock()
defer c.mutex.Unlock()
added := false
outer:
for _, key := range keys {
for i, k := range c.keys {
if k.Name == key.Name {
if key.Version >= k.Version {
c.keys[i] = key
added = true
}
continue outer
}
}
// key not found, add and wakeup the writeThread
c.keys = append(c.keys, key)
added = true
}
if added {
c.wakeup.Signal()
}
}
// The client has lost access to the document.
func (c *client) notifyLostAccess(code errorCode) {
log.Printf(" Client %v lost access to the document. Closing connection.", c.id)
c.enqueueError(code, "")
c.mutex.Lock()
c.closed = true
c.mutex.Unlock()
}
func (c *client) notifyPermissionChange(permissions string) {
c.mutex.Lock()
c.writePermission = strings.Contains(permissions, "w")
c.adminPermission = strings.Contains(permissions, "a")
c.mutex.Unlock()
if !strings.Contains(permissions, "r") {
c.notifyLostAccess(errorAccessDenied)
}
}