-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconn.go
More file actions
526 lines (412 loc) · 10.7 KB
/
Copy pathconn.go
File metadata and controls
526 lines (412 loc) · 10.7 KB
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
// Package binlog handles establishment of a follower connection to a MySQL server,
// streaming of binary log data, and parsing into memory.
package binlog
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"time"
)
const (
timeout time.Duration = 10 * time.Second
maxPayloadLength int = 1<<24 - 1
initialPacketBufferSize int = 4 * 1024 // 4 kb
)
// Conn speaks the MySQL client/server protocol documented here:
// https://dev.mysql.com/doc/internals/en/client-server-protocol.html.
type Conn struct {
conn net.Conn
br *bufio.Reader
seq uint8 // packet sequence number
user string
password string
db string
// Handshake initialization packet from server
capability uint32
status uint16
charset string
salt []byte
connectionID uint32
}
// NewConn opens a new connection to a MySQL server and returns it.
func NewConn(host string, port uint16, user string, password string, dbName string) (*Conn, error) {
c := new(Conn)
c.user = user
c.password = password
c.db = dbName
c.charset = DEFAULT_CHARSET
var err error
address := fmt.Sprintf("%s:%d", host, port)
c.conn, err = net.DialTimeout("tcp", address, timeout)
if err != nil {
return nil, err
}
c.br = bufio.NewReaderSize(c.conn, initialPacketBufferSize)
if err = c.handshake(); err != nil {
return nil, err
}
return c, nil
}
// handshake performs the handshake to establish the connection.
func (c *Conn) handshake() error {
var err error
if err = c.readInitialHandshake(); err != nil {
c.close()
return err
}
if err := c.writeAuthHandshake(); err != nil {
c.close()
return err
}
if _, err := c.readOKPacket(); err != nil {
c.close()
return err
}
return nil
}
// readInitialHandshake reads the handshake initialization packet.
func (c *Conn) readInitialHandshake() error {
b, err := c.readPacket()
if err != nil {
return err
}
if b[0] == ERR_HEADER {
return errors.New("initial handshake error")
}
if b[0] < MinProtocolVersion {
return fmt.Errorf("invalid protocol version %d, must >= 10", b[0])
}
i := 0
// Skip protocol version (1 byte)
i = i + 1
// Skip (null-terminated) MySQL version
i = i + bytes.IndexByte(b[1:], 0x00) + 1
// Connection ID (4 bytes)
c.connectionID = uint32(binary.LittleEndian.Uint32(b[i : i+4]))
i = i + 4
// Salt (8 bytes)
c.salt = []byte{}
c.salt = append(c.salt, b[i:i+8]...)
i = i + 8
// Skip filler null byte
i = i + 1
// Capability flags (lower 2 bytes)
c.capability = uint32(binary.LittleEndian.Uint16(b[i : i+2]))
i = i + 2
if len(b) > i {
// Skip server charset
i = i + 1
// Status flags
c.status = binary.LittleEndian.Uint16(b[i : i+2])
i = i + 2
// Capability flags (upper 2 bytes)
c.capability = uint32(binary.LittleEndian.Uint16(b[i:i+2]))<<16 | c.capability
i = i + 2
// Skip auth data len or [00]
i = i + 1
// Skip reserved (all [00])
i = i + 10
// Rest of the salt
c.salt = append(c.salt, b[i:i+12]...)
}
return nil
}
func (c *Conn) execute(query string) (*Result, error) {
err := c.writeCommandWithArg(COM_QUERY, query)
if err != nil {
return nil, err
}
return c.readResult(false)
}
func (c *Conn) handleErrorPacket(data []byte) error {
return errors.New("error packet")
}
func (c *Conn) readOKPacket() (*Result, error) {
data, err := c.readPacket()
if err != nil {
return nil, err
}
if data[0] == OK_HEADER {
return c.handleOKPacket(data)
} else if data[0] == ERR_HEADER {
return nil, c.handleErrorPacket(data)
} else {
return nil, errors.New("invalid ok packet")
}
}
func (c *Conn) writeCommandWithArg(command byte, arg string) error {
c.resetSequence()
data := makeCommandWithArg(command, arg)
return c.writePacket(data)
}
func makeCommandWithArg(command byte, s string) []byte {
b := make([]byte, len(s)+5)
b[4] = command
copy(b[5:], s)
return b
}
func (c *Conn) readUntilEOF() (err error) {
var b []byte
for {
b, err = c.readPacket()
if err != nil {
return
}
if isEOFPacket(b) {
return
}
}
}
func isEOFPacket(b []byte) bool {
return b[0] == EOF_HEADER && len(b) <= 5
}
func (c *Conn) handleOKPacket(b []byte) (*Result, error) {
var n int
i := 1
r := new(Result)
r.AffectedRows, _, n = getLengthEncodedInt(b[i:])
i = i + n
r.InsertId, _, n = getLengthEncodedInt(b[i:])
i = i + n
if c.capability&CLIENT_PROTOCOL_41 > 0 {
r.Status = binary.LittleEndian.Uint16(b[i:])
c.status = r.Status
i = i + 2
} else if c.capability&CLIENT_TRANSACTIONS > 0 {
r.Status = binary.LittleEndian.Uint16(b[i:])
c.status = r.Status
i = i + 2
}
// Skip info
return r, nil
}
func (c *Conn) readResult(binary bool) (*Result, error) {
b, err := c.readPacket()
if err != nil {
return nil, err
}
if b[0] == OK_HEADER {
return c.handleOKPacket(b)
} else if b[0] == ERR_HEADER {
return nil, c.handleErrorPacket(b)
} else if b[0] == LocalInFile_HEADER {
return nil, errors.New("malformed packet error")
}
return c.readResultset(b, binary)
}
func (c *Conn) readResultset(b []byte, binary bool) (*Result, error) {
result := &Result{
Status: 0,
InsertId: 0,
AffectedRows: 0,
Resultset: &Resultset{},
}
columnCount, _, n := getLengthEncodedInt(b)
if n-len(b) != 0 {
return nil, errors.New("malformed packet error")
}
result.Fields = make([]*Field, columnCount)
result.FieldNames = make(map[string]int, columnCount)
if err := c.readResultColumns(result); err != nil {
return nil, err
}
if err := c.readResultRows(result, binary); err != nil {
return nil, err
}
return result, nil
}
func (c *Conn) readResultColumns(result *Result) (err error) {
var i int = 0
var b []byte
for {
b, err = c.readPacket()
if err != nil {
return
}
if isEOFPacket(b) {
if c.capability&CLIENT_PROTOCOL_41 > 0 {
result.Status = binary.LittleEndian.Uint16(b[3:])
c.status = result.Status
}
if i != len(result.Fields) {
err = errors.New("malformed packet error")
}
return
}
result.Fields[i], err = FieldData(b).parse()
if err != nil {
return
}
result.FieldNames[superUnsafeGetString(result.Fields[i].Name)] = i
i = i + 1
}
}
func (c *Conn) readResultRows(result *Result, isBinary bool) (err error) {
var b []byte
for {
b, err = c.readPacket()
if err != nil {
return
}
if isEOFPacket(b) {
if c.capability&CLIENT_PROTOCOL_41 > 0 {
result.Status = binary.LittleEndian.Uint16(b[3:])
c.status = result.Status
}
break
}
result.RowDatas = append(result.RowDatas, b)
}
result.Values = make([][]interface{}, len(result.RowDatas))
for i := range result.Values {
result.Values[i], err = result.RowDatas[i].parse(result.Fields, isBinary)
if err != nil {
return err
}
}
return nil
}
// writeAuthHandshake generates the handshake response packet.
func (c *Conn) writeAuthHandshake() error {
// Adjust client capability flags based on server support
capability := CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION |
CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_LONG_FLAG
capability &= c.capability
// Length: capability (4) + max-packet size (4) + charset (1) + reserved all[0]
// (23) + username
packetLength := 4 + 4 + 1 + 23 + (len(c.user) + 1)
// Calculate hash
auth := scramble41(c.salt, []byte(c.password))
packetLength = packetLength + 1 + len(auth)
if len(c.db) > 0 {
capability |= CLIENT_CONNECT_WITH_DB
packetLength = packetLength + len(c.db) + 1
}
c.capability = capability
i := 0
b := make([]byte, packetLength+4)
i = i + 4
// Client capability flags [32 bit]
binary.LittleEndian.PutUint32(b[i:i+4], capability)
i = i + 4
// Max packet size [32 bit] (none)
binary.LittleEndian.PutUint32(b[i:i+4], 0)
i = i + 4
// Client charset [1 byte]; use default collation ID 33 here (utf-8)
b[i] = byte(DEFAULT_COLLATION_ID)
i = i + 1
// Filler [23 bytes] (all 0x00)
i = i + 23
// User [null terminated string]
if len(c.user) > 0 {
i = i + copy(b[i:], c.user)
}
b[i] = 0x00
i = i + 1
// Auth [length encoded integer]
b[i] = byte(len(auth))
i = i + 1 + copy(b[i+1:], auth)
// DB [null terminated string]
if len(c.db) > 0 {
i = i + copy(b[i:], c.db)
b[i] = 0x00
}
return c.writePacket(b)
}
func (c *Conn) write(b []byte) (int, error) {
return c.conn.Write(b)
}
func (c *Conn) setReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
// readPacket() reads a raw packet from the MySQL connection. A raw packet is
// (1) a binary log event sent from a master to a follower <- what we care about
// (2) a single statement sent to the MySQL server, or
// (3) a single row sent to the client.
func (c *Conn) readPacket() ([]byte, error) {
var buf bytes.Buffer
if err := c.readPacketTo(&buf); err != nil {
return nil, err
} else {
return buf.Bytes(), nil
}
}
// readPacketTo reads the next available network packet into the writer provided
// and increments the packet sequence number.
func (c *Conn) readPacketTo(w io.Writer) error {
header := []byte{0, 0, 0, 0}
if _, err := io.ReadFull(c.br, header); err != nil {
return err
}
payloadLength := int(getBinaryUint24(header[0:3]))
if payloadLength < 1 {
return fmt.Errorf("invalid payload length %d", payloadLength)
}
// Check for out-of-order packets
sequence := uint8(header[3])
if sequence != c.seq {
return fmt.Errorf("invalid sequence %d != %d", sequence, c.seq)
}
c.seq = c.seq + 1
// Now read the payload
if n, err := io.CopyN(w, c.br, int64(payloadLength)); err != nil {
return err
} else if n != int64(payloadLength) {
return errors.New("connection is faulty")
} else {
if payloadLength < maxPayloadLength {
return nil
}
if err := c.readPacketTo(w); err != nil {
return err
}
}
return nil
}
// writePacket populates the passed packet buffer with a proper header in place
// and writes it to the network.
func (c *Conn) writePacket(data []byte) error {
payloadLength := len(data) - 4
for payloadLength >= maxPayloadLength {
data[0] = 0xff
data[1] = 0xff
data[2] = 0xff
data[3] = c.seq
if n, err := c.write(data[:4+maxPayloadLength]); err != nil {
return errors.New("connection was bad")
} else if n != (4 + maxPayloadLength) {
return errors.New("connection was bad")
} else {
c.seq = c.seq + 1
payloadLength = payloadLength - maxPayloadLength
data = data[maxPayloadLength:]
}
}
data[0] = byte(payloadLength)
data[1] = byte(payloadLength >> 8)
data[2] = byte(payloadLength >> 16)
data[3] = c.seq
if n, err := c.write(data); err != nil {
return errors.New("connection was bad")
} else if n != len(data) {
return errors.New("connection was bad")
} else {
c.seq = c.seq + 1
return nil
}
}
// resetSequence resets the packet sequence number.
func (c *Conn) resetSequence() {
c.seq = 0
}
func (c *Conn) close() error {
c.seq = 0
if c.conn != nil {
return c.conn.Close()
}
return nil
}