Skip to content

Commit 95d05ef

Browse files
authored
Merge pull request #640 from guoyuanchao1202/master
Read auth_data_len from InitialHandshake packet instead of hard code
2 parents 2219281 + 214b9ae commit 95d05ef

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

client/auth.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,25 @@ func (c *Conn) readInitialHandshake() error {
7777
c.capability = uint32(binary.LittleEndian.Uint16(data[pos:pos+2]))<<16 | c.capability
7878
pos += 2
7979

80-
// skip auth data len or [00]
80+
// auth_data is end with 0x00, min data length is 13 + 8 = 21
81+
// ref to https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake
82+
maxAuthDataLen := 21
83+
if c.capability&CLIENT_PLUGIN_AUTH != 0 && int(data[pos]) > maxAuthDataLen {
84+
maxAuthDataLen = int(data[pos])
85+
}
86+
8187
// skip reserved (all [00])
8288
pos += 10 + 1
8389

84-
// The documentation is ambiguous about the length.
85-
// The official Python library uses the fixed length 12
86-
// mysql-proxy also use 12
87-
// which is not documented but seems to work.
88-
c.salt = append(c.salt, data[pos:pos+12]...)
89-
pos += 13
90-
// auth plugin
91-
if end := bytes.IndexByte(data[pos:], 0x00); end != -1 {
92-
c.authPluginName = string(data[pos : pos+end])
93-
} else {
94-
c.authPluginName = string(data[pos:])
90+
// auth_data is end with 0x00, so we need to trim 0x00
91+
resetOfAuthDataEndPos := pos + maxAuthDataLen - 8 - 1
92+
c.salt = append(c.salt, data[pos:resetOfAuthDataEndPos]...)
93+
94+
// skip reset of end pos
95+
pos = resetOfAuthDataEndPos + 1
96+
97+
if c.capability&CLIENT_PLUGIN_AUTH != 0 {
98+
c.authPluginName = string(data[pos : len(data)-1])
9599
}
96100
}
97101

replication/binlogsyncer.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,10 @@ func (b *BinlogSyncer) onStream(s *BinlogStreamer) {
728728
s.closeWithError(err)
729729
return
730730
case EOF_HEADER:
731-
// Refer http://dev.mysql.com/doc/internals/en/packet-EOF_Packet.html
732-
// In the MySQL client/server protocol, EOF and OK packets serve the same purpose.
733-
// Some users told me that they received EOF packet here, but I don't know why.
734-
// So we only log a message and retry ReadPacket.
735-
log.Info("receive EOF packet, retry ReadPacket")
731+
// refer to https://dev.mysql.com/doc/internals/en/com-binlog-dump.html#binlog-dump-non-block
732+
// when COM_BINLOG_DUMP command use BINLOG_DUMP_NON_BLOCK flag,
733+
// if there is no more event to send an EOF_Packet instead of blocking the connection
734+
log.Info("receive EOF packet, no more binlog event now.")
736735
continue
737736
default:
738737
log.Errorf("invalid stream header %c", data[0])

0 commit comments

Comments
 (0)