/
main.go
367 lines (322 loc) · 8.15 KB
/
main.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
// Command transcode converts an FBS file to an equivalent version that only uses raw encoding.
package main
import (
"bufio"
"bytes"
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/cheggaaa/pb"
logging "github.com/op/go-logging"
"github.com/openai/go-vncdriver/vncclient"
)
var (
in = flag.String("in", "", "path to server.fbs")
out = flag.String("out", "", "path to output file")
)
type cursorEncoding struct {
b []byte
}
func (e *cursorEncoding) Type() int32 {
return -239
}
func (e *cursorEncoding) Size() int {
return len(e.b)
}
func (e *cursorEncoding) Read(c *vncclient.ClientConn, rect *vncclient.Rectangle, r io.Reader) (vncclient.Encoding, error) {
sz := rect.Area()*int(c.PixelFormat.BPP)/8 + (int(rect.Width+7)/8)*int(rect.Height)
b := make([]byte, sz)
_, err := io.ReadFull(r, b)
return &cursorEncoding{b}, err
}
type fbsReader struct {
buf bytes.Buffer
r io.Reader
timestamp [4]byte
err error
}
func (r *fbsReader) read4() ([]byte, error) {
b := make([]byte, 4)
_, err := io.ReadFull(r.r, b)
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return b, err
}
func (r *fbsReader) Read(p []byte) (n int, err error) {
// remember any previous error
if r.err != nil {
return 0, r.err
}
defer func() {
if err != nil {
r.err = err
}
}()
if r.buf.Len() == 0 {
// read length
b, err := r.read4()
if err != nil {
return 0, err
}
l := int64(bytes2Uint32(b))
if l == 0 {
return 0, io.EOF
}
// read data
_, err = io.Copy(&r.buf, io.LimitReader(r.r, l))
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
if err != nil {
return 0, err
}
// read timestamp
b, err = r.read4()
if err != nil {
return 0, err
}
copy(r.timestamp[:], b)
}
return r.buf.Read(p)
}
func (r *fbsReader) Timestamp() []byte {
return r.timestamp[:]
}
func main() {
logging.SetLevel(logging.INFO, "")
flag.Parse()
if *in == "" || *out == "" {
flag.Usage()
fmt.Println("\n--in and --out are both required")
os.Exit(1)
}
// Open input file and start streaming progress to stdout.
var br *bufio.Reader
{
fi, err := os.Stat(*in)
check(err)
bar := pb.New64(fi.Size())
bar.SetMaxWidth(100)
bar.SetUnits(pb.U_BYTES)
bar.Start()
defer bar.FinishPrint("")
f, err := os.Open(*in)
check(err)
defer f.Close()
br = bufio.NewReader(bar.NewProxyReader(f))
}
// Open output file and copy header information to it.
var w *bufio.Writer
{
f, err := os.Create(*out)
check(err)
defer f.Close()
w = bufio.NewWriter(f)
defer w.Flush()
}
{
version := make([]byte, 12)
check(io.ReadFull(br, version))
if s := string(version); s != "FBS 001.002\n" {
log.Fatal("Unrecognized FBS version: " + s)
}
check(w.Write(version))
}
{
b, err := br.ReadBytes('\n')
check(err)
check(w.Write(b))
}
r := &fbsReader{r: br}
// Utilities.
var (
// next returns the next n bytes of data from the server, and updates
// timestamp to the FBS timestamp of the last of those bytes.
// It exits non-zero if there are fewer than n bytes left in the file.
next = func(n int) []byte {
b := make([]byte, n)
check(io.ReadFull(r, b))
return b
}
// nextSafe is like next, but exits zero if there are fewer than n bytes
// left in the file.
nextSafe = func(n int) []byte {
b := make([]byte, n)
_, err := io.ReadFull(r, b)
if err == io.EOF || err == io.ErrUnexpectedEOF {
os.Exit(0)
}
check(err)
return b
}
// emit writes data in FBS format to w.
emit = func(data []byte) {
check(binary.Write(w, binary.BigEndian, uint32(len(data))))
check(w.Write(data))
check(w.Write(r.timestamp[:]))
}
)
// Read handshake and init
{
// ProtocolVersion message
// 12 bytes
version := next(12)
major, minor, err := vncclient.ParseProtocolVersion(version)
check(err)
if major != 3 || minor != 3 {
log.Fatalf("Unsupported RFB protocol version: %v.%v", major, minor)
}
emit(version)
vncAuth := func() {
// VNC Authentication challenge (we assume this is the security type chosen)
// 16 bytes
emit(next(16))
// SecurityResult message
// 4 bytes
b := next(4)
if result := bytes2Uint32(b); result != 0 {
log.Fatalf("Unexpected SecurityResult handshake: %v", result)
}
emit(b)
}
if minor == 3 {
b := next(4)
if sec := bytes2Uint32(b); sec != 2 {
log.Fatalf("Unsupported security-type: %v", sec)
}
emit(b)
vncAuth()
}
if minor == 8 {
// security-types message
// n+1 bytes, where n is the value of the first byte
n := next(1)
emit(append(n, next(int(n[0]))...))
vncAuth()
}
}
// ServerInit message
// 2+2+16+4+N bytes
b := next(24)
var pf vncclient.PixelFormat
check(vncclient.ReadPixelFormat(bytes.NewReader(b[4:20]), &pf))
if !(pf.BPP == 32 &&
pf.Depth == 24 &&
!pf.BigEndian &&
pf.TrueColor &&
pf.RedMax == 255 &&
pf.GreenMax == 255 &&
pf.BlueMax == 255 &&
pf.RedShift == 16 &&
pf.GreenShift == 8 &&
pf.BlueShift == 0) {
log.Fatalf("Unsupported pixel format: %#v\n", pf)
}
emit(append(b, next(int(bytes2Uint32(b[20:24])))...))
var fbu vncclient.FramebufferUpdateMessage
conn := &vncclient.ClientConn{
Encs: []vncclient.Encoding{
new(vncclient.TightEncoding),
new(vncclient.RawEncoding),
new(cursorEncoding),
},
PixelFormat: pf,
}
//
// Most of the processing time is spent here, transcoding FramebufferUpdate messages
//
for {
switch b := nextSafe(1); b[0] {
// FramebufferUpdate
case 0:
msg, err := fbu.Read(conn, r)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return
}
check(err)
rects := msg.(*vncclient.FramebufferUpdateMessage).Rectangles
// Start counting how many bytes we're going to write
size := 0
size += 4 // account for header (see below)
for _, r := range rects {
size += 12 // every Rectangle starts out with four 2-byte fields (X, Y, W, H)
// and a 4-byte encoding type, and then also contains pixels
if ce, ok := r.Enc.(*cursorEncoding); ok {
size += len(ce.b) // if it's a cursor encoding, it already got parsed
} else {
size += r.Area() * 4 // if it's pixels, then it's four bytes per pixel
}
}
// Write FramebufferUpdate header
check(binary.Write(w, binary.BigEndian, uint32(size)))
check(w.Write(b)) // message type
check(w.Write([]byte{0})) // padding
check(binary.Write(w, binary.BigEndian, uint16(len(rects)))) // number of rectangles
// Write FramebufferUpdate rectangles
for _, r := range rects {
var colors []vncclient.Color
isCursor := false
newEncType := int32(0) // regardless of input type, we're outputting raw (unless it's cursor)
switch e := r.Enc.(type) {
case *vncclient.TightEncoding:
colors = e.Colors
case *vncclient.RawEncoding:
colors = e.Colors
case *cursorEncoding:
isCursor = true
newEncType = r.Enc.Type() // keep it as cursor type
}
for _, x := range []interface{}{
r.X, r.Y, r.Width, r.Height, newEncType,
} {
check(binary.Write(w, binary.BigEndian, x))
}
if isCursor {
check(w.Write(r.Enc.(*cursorEncoding).b))
}
for _, c := range colors {
colorOrder := (uint32(c.R) << conn.PixelFormat.RedShift) +
(uint32(c.G) << conn.PixelFormat.GreenShift) +
(uint32(c.B) << conn.PixelFormat.BlueShift)
// Write the RGB bytes out in little-endian order,
// having verified "!pf.BigEndian" above
check(w.Write([]byte{uint8(colorOrder),
uint8(colorOrder >> 8),
uint8(colorOrder >> 16),
0}))
}
}
check(w.Write(r.timestamp[:]))
// SetColorMapEntries
case 1:
log.Fatal("SetColorMapEntries not supported")
// Bell
case 2:
emit(b)
// ServerCutText
case 3:
b = append(b, nextSafe(7)...)
b = append(b, nextSafe(int(bytes2Uint32(b[4:8])))...)
emit(b)
default:
log.Fatalf("Unrecognized Server-to-Client message type: %v", b[0])
}
}
}
func bytes2Uint32(b []byte) (u uint32) {
if len(b) != 4 {
panic(fmt.Sprintf("wrong size for []byte: %v", len(b)))
}
check(binary.Read(bytes.NewReader(b), binary.BigEndian, &u))
return
}
func check(e ...interface{}) {
err := e[len(e)-1]
if err != nil {
log.Fatal(err)
}
}