From a343408ccba57a719b27390ec19c55e8bd058355 Mon Sep 17 00:00:00 2001 From: raffertyyu Date: Mon, 11 Nov 2024 11:40:27 +0800 Subject: [PATCH 1/2] send client connect attrs according to whether mysql server supports CLIENT_CONNECT_ATTRS --- packets.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packets.go b/packets.go index eb4e0cef..0552bd82 100644 --- a/packets.go +++ b/packets.go @@ -210,10 +210,12 @@ func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err erro if len(data) > pos { // character set [1 byte] // status flags [2 bytes] + pos += 1 + 2 // capability flags (upper 2 bytes) [2 bytes] + mc.flags |= clientFlag(binary.LittleEndian.Uint16(data[pos:pos+2])) << 16 // length of auth-plugin-data [1 byte] // reserved (all [00]) [10 bytes] - pos += 1 + 2 + 2 + 1 + 10 + pos += 2 + 1 + 10 // second part of the password cipher [minimum 13 bytes], // where len=MAX(13, length of auth-plugin-data - 8) @@ -261,9 +263,11 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string clientLocalFiles | clientPluginAuth | clientMultiResults | - clientConnectAttrs | + mc.flags&clientConnectAttrs | mc.flags&clientLongFlag + serverSupportClientConnectAttrs := mc.flags&clientConnectAttrs != 0 + if mc.cfg.ClientFoundRows { clientFlags |= clientFoundRows } @@ -295,11 +299,14 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string pktLen += n + 1 } + var connAttrsLEI []byte // encode length of the connection attributes - var connAttrsLEIBuf [9]byte - connAttrsLen := len(mc.connector.encodedAttributes) - connAttrsLEI := appendLengthEncodedInteger(connAttrsLEIBuf[:0], uint64(connAttrsLen)) - pktLen += len(connAttrsLEI) + len(mc.connector.encodedAttributes) + if serverSupportClientConnectAttrs { + var connAttrsLEIBuf [9]byte + connAttrsLen := len(mc.connector.encodedAttributes) + connAttrsLEI = appendLengthEncodedInteger(connAttrsLEIBuf[:0], uint64(connAttrsLen)) + pktLen += len(connAttrsLEI) + len(mc.connector.encodedAttributes) + } // Calculate packet length and get buffer with that size data, err := mc.buf.takeBuffer(pktLen + 4) @@ -382,8 +389,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string pos++ // Connection Attributes - pos += copy(data[pos:], connAttrsLEI) - pos += copy(data[pos:], []byte(mc.connector.encodedAttributes)) + if serverSupportClientConnectAttrs { + pos += copy(data[pos:], connAttrsLEI) + pos += copy(data[pos:], []byte(mc.connector.encodedAttributes)) + } // Send Auth packet return mc.writePacket(data[:pos]) From 782166f670b9031c629d002c9b96203d5e695015 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 11 Nov 2024 15:32:42 +0900 Subject: [PATCH 2/2] code styling --- packets.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packets.go b/packets.go index 0552bd82..a2e7ef95 100644 --- a/packets.go +++ b/packets.go @@ -210,12 +210,13 @@ func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err erro if len(data) > pos { // character set [1 byte] // status flags [2 bytes] - pos += 1 + 2 + pos += 3 // capability flags (upper 2 bytes) [2 bytes] mc.flags |= clientFlag(binary.LittleEndian.Uint16(data[pos:pos+2])) << 16 + pos += 2 // length of auth-plugin-data [1 byte] // reserved (all [00]) [10 bytes] - pos += 2 + 1 + 10 + pos += 11 // second part of the password cipher [minimum 13 bytes], // where len=MAX(13, length of auth-plugin-data - 8) @@ -266,7 +267,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string mc.flags&clientConnectAttrs | mc.flags&clientLongFlag - serverSupportClientConnectAttrs := mc.flags&clientConnectAttrs != 0 + sendConnectAttrs := mc.flags&clientConnectAttrs != 0 if mc.cfg.ClientFoundRows { clientFlags |= clientFoundRows @@ -299,9 +300,9 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string pktLen += n + 1 } - var connAttrsLEI []byte // encode length of the connection attributes - if serverSupportClientConnectAttrs { + var connAttrsLEI []byte + if sendConnectAttrs { var connAttrsLEIBuf [9]byte connAttrsLen := len(mc.connector.encodedAttributes) connAttrsLEI = appendLengthEncodedInteger(connAttrsLEIBuf[:0], uint64(connAttrsLen)) @@ -389,7 +390,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string pos++ // Connection Attributes - if serverSupportClientConnectAttrs { + if sendConnectAttrs { pos += copy(data[pos:], connAttrsLEI) pos += copy(data[pos:], []byte(mc.connector.encodedAttributes)) }