-
Notifications
You must be signed in to change notification settings - Fork 39
/
messages.go
638 lines (536 loc) · 15.3 KB
/
messages.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
package vnc2video
import (
"encoding/binary"
"fmt"
"github.com/amitbet/vnc2video/logger"
)
var (
// DefaultClientMessages slice of default client messages sent to server
DefaultClientMessages = []ClientMessage{
&SetPixelFormat{},
&SetEncodings{},
&FramebufferUpdateRequest{},
&KeyEvent{},
&PointerEvent{},
&ClientCutText{},
}
// DefaultServerMessages slice of default server messages sent to client
DefaultServerMessages = []ServerMessage{
&FramebufferUpdate{},
&SetColorMapEntries{},
&Bell{},
&ServerCutText{},
}
)
// ClientMessageType represents RFB message type
type ClientMessageType uint8
//go:generate stringer -type=ClientMessageType
// Client-to-Server message types.
const (
SetPixelFormatMsgType ClientMessageType = iota
_
SetEncodingsMsgType
FramebufferUpdateRequestMsgType
KeyEventMsgType
PointerEventMsgType
ClientCutTextMsgType
)
// ServerMessageType represents RFB message type
type ServerMessageType uint8
// Server-to-Client message types
const (
FramebufferUpdateMsgType ServerMessageType = iota
SetColorMapEntriesMsgType
BellMsgType
ServerCutTextMsgType
)
// ServerInit struct used in server init handshake
type ServerInit struct {
FBWidth, FBHeight uint16
PixelFormat PixelFormat
NameLength uint32
NameText []byte
}
// String provide stringer
func (srvInit ServerInit) String() string {
return fmt.Sprintf("Width: %d, Height: %d, PixelFormat: %s, NameLength: %d, MameText: %s", srvInit.FBWidth, srvInit.FBHeight, srvInit.PixelFormat, srvInit.NameLength, srvInit.NameText)
}
type ClientMessage interface {
String() string
Type() ClientMessageType
Read(Conn) (ClientMessage, error)
Write(Conn) error
Supported(Conn) bool
}
type ServerMessage interface {
String() string
Type() ServerMessageType
Read(Conn) (ServerMessage, error)
Write(Conn) error
Supported(Conn) bool
}
// FramebufferUpdate holds a FramebufferUpdate wire format message.
type FramebufferUpdate struct {
_ [1]byte // pad
NumRect uint16 // number-of-rectangles
Rects []*Rectangle // rectangles
}
// String provide stringer
func (msg *FramebufferUpdate) String() string {
return fmt.Sprintf("rects %d rectangle[]: { %v }", msg.NumRect, msg.Rects)
}
func (msg *FramebufferUpdate) Supported(c Conn) bool {
return true
}
// Type return MessageType
func (*FramebufferUpdate) Type() ServerMessageType {
return FramebufferUpdateMsgType
}
// Read unmarshal message from conn
func (*FramebufferUpdate) Read(c Conn) (ServerMessage, error) {
msg := FramebufferUpdate{}
var pad [1]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
}
if err := binary.Read(c, binary.BigEndian, &msg.NumRect); err != nil {
return nil, err
}
logger.Debugf("-------Reading FrameBuffer update with %d rects-------", msg.NumRect)
for i := uint16(0); i < msg.NumRect; i++ {
rect := NewRectangle()
logger.DebugfNoCR("----------RECT %d----------", i)
if err := rect.Read(c); err != nil {
return nil, err
}
if rect.EncType == EncDesktopSizePseudo {
c.(*ClientConn).ResetAllEncodings()
}
logger.Tracef("----End RECT #%d Info (%dx%d) encType:%s", i, rect.Width, rect.Height, rect.EncType)
msg.Rects = append(msg.Rects, rect)
}
return &msg, nil
}
// Write marshals message to conn
func (msg *FramebufferUpdate) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
var pad [1]byte
if err := binary.Write(c, binary.BigEndian, pad); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg.NumRect); err != nil {
return err
}
for _, rect := range msg.Rects {
if err := rect.Write(c); err != nil {
return err
}
}
return c.Flush()
}
// ServerCutText represents server message
type ServerCutText struct {
_ [1]byte
Length uint32
Text []byte
}
func (msg *ServerCutText) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *ServerCutText) String() string {
return fmt.Sprintf("lenght: %d text: %s", msg.Length, msg.Text)
}
// Type returns MessageType
func (*ServerCutText) Type() ServerMessageType {
return ServerCutTextMsgType
}
// Read unmarshal message from conn
func (*ServerCutText) Read(c Conn) (ServerMessage, error) {
msg := ServerCutText{}
var pad [1]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
}
if err := binary.Read(c, binary.BigEndian, &msg.Length); err != nil {
return nil, err
}
msg.Text = make([]byte, msg.Length)
if err := binary.Read(c, binary.BigEndian, &msg.Text); err != nil {
return nil, err
}
return &msg, nil
}
// Write marshal message to conn
func (msg *ServerCutText) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
var pad [1]byte
if err := binary.Write(c, binary.BigEndian, pad); err != nil {
return err
}
if msg.Length < uint32(len(msg.Text)) {
msg.Length = uint32(len(msg.Text))
}
if err := binary.Write(c, binary.BigEndian, msg.Length); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg.Text); err != nil {
return err
}
return c.Flush()
}
// Bell server message
type Bell struct{}
func (*Bell) Supported(c Conn) bool {
return true
}
// String return string
func (*Bell) String() string {
return fmt.Sprintf("bell")
}
// Type returns MessageType
func (*Bell) Type() ServerMessageType {
return BellMsgType
}
// Read unmarshal message from conn
func (*Bell) Read(c Conn) (ServerMessage, error) {
return &Bell{}, nil
}
// Write marshal message to conn
func (msg *Bell) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
return c.Flush()
}
// SetColorMapEntries server message
type SetColorMapEntries struct {
_ [1]byte
FirstColor uint16
ColorsNum uint16
Colors []Color
}
func (msg *SetColorMapEntries) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *SetColorMapEntries) String() string {
return fmt.Sprintf("first color: %d, numcolors: %d, colors[]: { %v }", msg.FirstColor, msg.ColorsNum, msg.Colors)
}
// Type returns MessageType
func (*SetColorMapEntries) Type() ServerMessageType {
return SetColorMapEntriesMsgType
}
// Read unmrashal message from conn
func (*SetColorMapEntries) Read(c Conn) (ServerMessage, error) {
logger.Info("Reading SetColorMapEntries message")
msg := SetColorMapEntries{}
var pad [1]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
}
if err := binary.Read(c, binary.BigEndian, &msg.FirstColor); err != nil {
return nil, err
}
if err := binary.Read(c, binary.BigEndian, &msg.ColorsNum); err != nil {
return nil, err
}
msg.Colors = make([]Color, msg.ColorsNum)
colorMap := c.ColorMap()
for i := uint16(0); i < msg.ColorsNum; i++ {
color := &msg.Colors[i]
err := color.Read(c)
if err != nil {
//if err := binary.Read(c, binary.BigEndian, &color); err != nil {
return nil, err
}
colorMap[msg.FirstColor+i] = *color
}
c.SetColorMap(colorMap)
return &msg, nil
}
// Write marshal message to conn
func (msg *SetColorMapEntries) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
var pad [1]byte
if err := binary.Write(c, binary.BigEndian, &pad); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg.FirstColor); err != nil {
return err
}
if msg.ColorsNum < uint16(len(msg.Colors)) {
msg.ColorsNum = uint16(len(msg.Colors))
}
if err := binary.Write(c, binary.BigEndian, msg.ColorsNum); err != nil {
return err
}
for i := 0; i < len(msg.Colors); i++ {
color := msg.Colors[i]
if err := binary.Write(c, binary.BigEndian, color); err != nil {
return err
}
}
return c.Flush()
}
// SetPixelFormat holds the wire format message.
type SetPixelFormat struct {
_ [3]byte // padding
PF PixelFormat // pixel-format
}
func (msg *SetPixelFormat) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *SetPixelFormat) String() string {
return fmt.Sprintf("%s", msg.PF)
}
// Type returns MessageType
func (*SetPixelFormat) Type() ClientMessageType {
return SetPixelFormatMsgType
}
// Write marshal message to conn
func (msg *SetPixelFormat) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
return err
}
pf := c.PixelFormat()
// Invalidate the color map.
if pf.TrueColor != 0 {
c.SetColorMap(ColorMap{})
}
return c.Flush()
}
// Read unmarshal message from conn
func (*SetPixelFormat) Read(c Conn) (ClientMessage, error) {
msg := SetPixelFormat{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
// SetEncodings holds the wire format message, sans encoding-type field.
type SetEncodings struct {
_ [1]byte // padding
EncNum uint16 // number-of-encodings
Encodings []EncodingType
}
func (msg *SetEncodings) Supported(c Conn) bool {
return true
}
// String return string
func (msg *SetEncodings) String() string {
return fmt.Sprintf("encnum: %d, encodings[]: { %v }", msg.EncNum, msg.Encodings)
}
// Type returns MessageType
func (*SetEncodings) Type() ClientMessageType {
return SetEncodingsMsgType
}
// Read unmarshal message from conn
func (*SetEncodings) Read(c Conn) (ClientMessage, error) {
msg := SetEncodings{}
var pad [1]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
}
if err := binary.Read(c, binary.BigEndian, &msg.EncNum); err != nil {
return nil, err
}
var enc EncodingType
for i := uint16(0); i < msg.EncNum; i++ {
if err := binary.Read(c, binary.BigEndian, &enc); err != nil {
return nil, err
}
msg.Encodings = append(msg.Encodings, enc)
}
c.SetEncodings(msg.Encodings)
return &msg, nil
}
// Write marshal message to conn
func (msg *SetEncodings) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
var pad [1]byte
if err := binary.Write(c, binary.BigEndian, pad); err != nil {
return err
}
if uint16(len(msg.Encodings)) > msg.EncNum {
msg.EncNum = uint16(len(msg.Encodings))
}
if err := binary.Write(c, binary.BigEndian, msg.EncNum); err != nil {
return err
}
for _, enc := range msg.Encodings {
if err := binary.Write(c, binary.BigEndian, enc); err != nil {
return err
}
}
return c.Flush()
}
// FramebufferUpdateRequest holds the wire format message.
type FramebufferUpdateRequest struct {
Inc uint8 // incremental
X, Y uint16 // x-, y-position
Width, Height uint16 // width, height
}
func (msg *FramebufferUpdateRequest) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *FramebufferUpdateRequest) String() string {
return fmt.Sprintf("incremental: %d, x: %d, y: %d, width: %d, height: %d", msg.Inc, msg.X, msg.Y, msg.Width, msg.Height)
}
// Type returns MessageType
func (*FramebufferUpdateRequest) Type() ClientMessageType {
return FramebufferUpdateRequestMsgType
}
// Read unmarshal message from conn
func (*FramebufferUpdateRequest) Read(c Conn) (ClientMessage, error) {
msg := FramebufferUpdateRequest{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
// Write marshal message to conn
func (msg *FramebufferUpdateRequest) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
return err
}
return c.Flush()
}
// KeyEvent holds the wire format message.
type KeyEvent struct {
Down uint8 // down-flag
_ [2]byte // padding
Key Key // key
}
func (msg *KeyEvent) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *KeyEvent) String() string {
return fmt.Sprintf("down: %d, key: %v", msg.Down, msg.Key)
}
// Type returns MessageType
func (*KeyEvent) Type() ClientMessageType {
return KeyEventMsgType
}
// Read unmarshal message from conn
func (*KeyEvent) Read(c Conn) (ClientMessage, error) {
msg := KeyEvent{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
// Write marshal message to conn
func (msg *KeyEvent) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
return err
}
return c.Flush()
}
// PointerEvent message holds the wire format message
type PointerEvent struct {
Mask uint8 // button-mask
X, Y uint16 // x-, y-position
}
func (msg *PointerEvent) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *PointerEvent) String() string {
return fmt.Sprintf("mask %d, x: %d, y: %d", msg.Mask, msg.X, msg.Y)
}
// Type returns MessageType
func (*PointerEvent) Type() ClientMessageType {
return PointerEventMsgType
}
// Read unmarshal message from conn
func (*PointerEvent) Read(c Conn) (ClientMessage, error) {
msg := PointerEvent{}
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
return nil, err
}
return &msg, nil
}
// Write marshal message to conn
func (msg *PointerEvent) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
return err
}
return c.Flush()
}
// ClientCutText holds the wire format message, sans the text field.
type ClientCutText struct {
_ [3]byte // padding
Length uint32 // length
Text []byte
}
func (msg *ClientCutText) Supported(c Conn) bool {
return true
}
// String returns string
func (msg *ClientCutText) String() string {
return fmt.Sprintf("length: %d, text: %s", msg.Length, msg.Text)
}
// Type returns MessageType
func (*ClientCutText) Type() ClientMessageType {
return ClientCutTextMsgType
}
// Read unmarshal message from conn
func (*ClientCutText) Read(c Conn) (ClientMessage, error) {
msg := ClientCutText{}
var pad [3]byte
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
return nil, err
}
if err := binary.Read(c, binary.BigEndian, &msg.Length); err != nil {
return nil, err
}
msg.Text = make([]byte, msg.Length)
if err := binary.Read(c, binary.BigEndian, &msg.Text); err != nil {
return nil, err
}
return &msg, nil
}
// Write marshal message to conn
func (msg *ClientCutText) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
return err
}
var pad [3]byte
if err := binary.Write(c, binary.BigEndian, &pad); err != nil {
return err
}
if uint32(len(msg.Text)) > msg.Length {
msg.Length = uint32(len(msg.Text))
}
if err := binary.Write(c, binary.BigEndian, msg.Length); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, msg.Text); err != nil {
return err
}
return c.Flush()
}