@@ -6,16 +6,16 @@ import (
6
6
"encoding/binary"
7
7
"fmt"
8
8
9
- . "github.com/go-mysql-org/go-mysql/mysql"
9
+ "github.com/go-mysql-org/go-mysql/mysql"
10
10
"github.com/go-mysql-org/go-mysql/packet"
11
11
"github.com/pingcap/errors"
12
12
"github.com/pingcap/tidb/pkg/parser/charset"
13
13
)
14
14
15
- const defaultAuthPluginName = AUTH_NATIVE_PASSWORD
15
+ const defaultAuthPluginName = mysql . AUTH_NATIVE_PASSWORD
16
16
17
17
// defines the supported auth plugins
18
- var supportedAuthPlugins = []string {AUTH_NATIVE_PASSWORD , AUTH_SHA256_PASSWORD , AUTH_CACHING_SHA2_PASSWORD }
18
+ var supportedAuthPlugins = []string {mysql . AUTH_NATIVE_PASSWORD , mysql . AUTH_SHA256_PASSWORD , mysql . AUTH_CACHING_SHA2_PASSWORD }
19
19
20
20
// helper function to determine what auth methods are allowed by this client
21
21
func authPluginAllowed (pluginName string ) bool {
@@ -38,12 +38,12 @@ func (c *Conn) readInitialHandshake() error {
38
38
return errors .Trace (err )
39
39
}
40
40
41
- if data [0 ] == ERR_HEADER {
41
+ if data [0 ] == mysql . ERR_HEADER {
42
42
return errors .Annotate (c .handleErrorPacket (data ), "read initial handshake error" )
43
43
}
44
44
45
- if data [0 ] != ClassicProtocolVersion {
46
- if data [0 ] == XProtocolVersion {
45
+ if data [0 ] != mysql . ClassicProtocolVersion {
46
+ if data [0 ] == mysql . XProtocolVersion {
47
47
return errors .Errorf (
48
48
"invalid protocol version %d, expected 10. " +
49
49
"This might be X Protocol, make sure to connect to the right port" ,
@@ -75,10 +75,10 @@ func (c *Conn) readInitialHandshake() error {
75
75
// The lower 2 bytes of the Capabilities Flags
76
76
c .capability = uint32 (binary .LittleEndian .Uint16 (data [pos : pos + 2 ]))
77
77
// check protocol
78
- if c .capability & CLIENT_PROTOCOL_41 == 0 {
78
+ if c .capability & mysql . CLIENT_PROTOCOL_41 == 0 {
79
79
return errors .New ("the MySQL server can not support protocol 41 and above required by the client" )
80
80
}
81
- if c .capability & CLIENT_SSL == 0 && c .tlsConfig != nil {
81
+ if c .capability & mysql . CLIENT_SSL == 0 && c .tlsConfig != nil {
82
82
return errors .New ("the MySQL Server does not support TLS required by the client" )
83
83
}
84
84
pos += 2
@@ -97,15 +97,15 @@ func (c *Conn) readInitialHandshake() error {
97
97
98
98
// length of the combined auth_plugin_data (scramble), if auth_plugin_data_len is > 0
99
99
authPluginDataLen := data [pos ]
100
- if (c .capability & CLIENT_PLUGIN_AUTH == 0 ) && (authPluginDataLen > 0 ) {
100
+ if (c .capability & mysql . CLIENT_PLUGIN_AUTH == 0 ) && (authPluginDataLen > 0 ) {
101
101
return errors .Errorf ("invalid auth plugin data filler %d" , authPluginDataLen )
102
102
}
103
103
pos ++
104
104
105
105
// skip reserved (all [00] ?)
106
106
pos += 10
107
107
108
- if c .capability & CLIENT_SECURE_CONNECTION != 0 {
108
+ if c .capability & mysql . CLIENT_SECURE_CONNECTION != 0 {
109
109
// Rest of the plugin provided data (scramble)
110
110
111
111
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_handshake_v10.html
@@ -125,7 +125,7 @@ func (c *Conn) readInitialHandshake() error {
125
125
c .salt = append (c .salt , authPluginDataPart2 ... )
126
126
}
127
127
128
- if c .capability & CLIENT_PLUGIN_AUTH != 0 {
128
+ if c .capability & mysql . CLIENT_PLUGIN_AUTH != 0 {
129
129
c .authPluginName = string (data [pos : pos + bytes .IndexByte (data [pos :], 0x00 )])
130
130
pos += len (c .authPluginName )
131
131
@@ -153,13 +153,13 @@ func (c *Conn) readInitialHandshake() error {
153
153
func (c * Conn ) genAuthResponse (authData []byte ) ([]byte , bool , error ) {
154
154
// password hashing
155
155
switch c .authPluginName {
156
- case AUTH_NATIVE_PASSWORD :
157
- return CalcPassword (authData [:20 ], []byte (c .password )), false , nil
158
- case AUTH_CACHING_SHA2_PASSWORD :
159
- return CalcCachingSha2Password (authData , c .password ), false , nil
160
- case AUTH_CLEAR_PASSWORD :
156
+ case mysql . AUTH_NATIVE_PASSWORD :
157
+ return mysql . CalcPassword (authData [:20 ], []byte (c .password )), false , nil
158
+ case mysql . AUTH_CACHING_SHA2_PASSWORD :
159
+ return mysql . CalcCachingSha2Password (authData , c .password ), false , nil
160
+ case mysql . AUTH_CLEAR_PASSWORD :
161
161
return []byte (c .password ), true , nil
162
- case AUTH_SHA256_PASSWORD :
162
+ case mysql . AUTH_SHA256_PASSWORD :
163
163
if len (c .password ) == 0 {
164
164
return nil , true , nil
165
165
}
@@ -186,10 +186,10 @@ func (c *Conn) genAttributes() []byte {
186
186
187
187
attrData := make ([]byte , 0 )
188
188
for k , v := range c .attributes {
189
- attrData = append (attrData , PutLengthEncodedString ([]byte (k ))... )
190
- attrData = append (attrData , PutLengthEncodedString ([]byte (v ))... )
189
+ attrData = append (attrData , mysql . PutLengthEncodedString ([]byte (k ))... )
190
+ attrData = append (attrData , mysql . PutLengthEncodedString ([]byte (v ))... )
191
191
}
192
- return append (PutLengthEncodedInt (uint64 (len (attrData ))), attrData ... )
192
+ return append (mysql . PutLengthEncodedInt (uint64 (len (attrData ))), attrData ... )
193
193
}
194
194
195
195
// See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
@@ -199,23 +199,23 @@ func (c *Conn) writeAuthHandshake() error {
199
199
}
200
200
201
201
// Set default client capabilities that reflect the abilities of this library
202
- capability := CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION |
203
- CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH
202
+ capability := mysql . CLIENT_PROTOCOL_41 | mysql . CLIENT_SECURE_CONNECTION |
203
+ mysql . CLIENT_LONG_PASSWORD | mysql . CLIENT_TRANSACTIONS | mysql . CLIENT_PLUGIN_AUTH
204
204
// Adjust client capability flags based on server support
205
- capability |= c .capability & CLIENT_LONG_FLAG
206
- capability |= c .capability & CLIENT_QUERY_ATTRIBUTES
205
+ capability |= c .capability & mysql . CLIENT_LONG_FLAG
206
+ capability |= c .capability & mysql . CLIENT_QUERY_ATTRIBUTES
207
207
// Adjust client capability flags on specific client requests
208
208
// Only flags that would make any sense setting and aren't handled elsewhere
209
209
// in the library are supported here
210
- capability |= c .ccaps & CLIENT_FOUND_ROWS | c .ccaps & CLIENT_IGNORE_SPACE |
211
- c .ccaps & CLIENT_MULTI_STATEMENTS | c .ccaps & CLIENT_MULTI_RESULTS |
212
- c .ccaps & CLIENT_PS_MULTI_RESULTS | c .ccaps & CLIENT_CONNECT_ATTRS |
213
- c .ccaps & CLIENT_COMPRESS | c .ccaps & CLIENT_ZSTD_COMPRESSION_ALGORITHM |
214
- c .ccaps & CLIENT_LOCAL_FILES
210
+ capability |= c .ccaps & mysql . CLIENT_FOUND_ROWS | c .ccaps & mysql . CLIENT_IGNORE_SPACE |
211
+ c .ccaps & mysql . CLIENT_MULTI_STATEMENTS | c .ccaps & mysql . CLIENT_MULTI_RESULTS |
212
+ c .ccaps & mysql . CLIENT_PS_MULTI_RESULTS | c .ccaps & mysql . CLIENT_CONNECT_ATTRS |
213
+ c .ccaps & mysql . CLIENT_COMPRESS | c .ccaps & mysql . CLIENT_ZSTD_COMPRESSION_ALGORITHM |
214
+ c .ccaps & mysql . CLIENT_LOCAL_FILES
215
215
216
216
// To enable TLS / SSL
217
217
if c .tlsConfig != nil {
218
- capability |= CLIENT_SSL
218
+ capability |= mysql . CLIENT_SSL
219
219
}
220
220
221
221
auth , addNull , err := c .genAuthResponse (c .salt )
@@ -227,11 +227,11 @@ func (c *Conn) writeAuthHandshake() error {
227
227
// here we use the Length-Encoded-Integer(LEI) as the data length may not fit into one byte
228
228
// see: https://dev.mysql.com/doc/internals/en/integer.html#length-encoded-integer
229
229
var authRespLEIBuf [9 ]byte
230
- authRespLEI := AppendLengthEncodedInteger (authRespLEIBuf [:0 ], uint64 (len (auth )))
230
+ authRespLEI := mysql . AppendLengthEncodedInteger (authRespLEIBuf [:0 ], uint64 (len (auth )))
231
231
if len (authRespLEI ) > 1 {
232
232
// if the length can not be written in 1 byte, it must be written as a
233
233
// length encoded integer
234
- capability |= CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
234
+ capability |= mysql . CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
235
235
}
236
236
237
237
// packet length
@@ -248,16 +248,16 @@ func (c *Conn) writeAuthHandshake() error {
248
248
}
249
249
// db name
250
250
if len (c .db ) > 0 {
251
- capability |= CLIENT_CONNECT_WITH_DB
251
+ capability |= mysql . CLIENT_CONNECT_WITH_DB
252
252
length += len (c .db ) + 1
253
253
}
254
254
// connection attributes
255
255
attrData := c .genAttributes ()
256
256
if len (attrData ) > 0 {
257
- capability |= CLIENT_CONNECT_ATTRS
257
+ capability |= mysql . CLIENT_CONNECT_ATTRS
258
258
length += len (attrData )
259
259
}
260
- if c .ccaps & CLIENT_ZSTD_COMPRESSION_ALGORITHM > 0 {
260
+ if c .ccaps & mysql . CLIENT_ZSTD_COMPRESSION_ALGORITHM > 0 {
261
261
length ++
262
262
}
263
263
@@ -279,7 +279,7 @@ func (c *Conn) writeAuthHandshake() error {
279
279
// use default collation id 255 here, is `utf8mb4_0900_ai_ci`
280
280
collationName := c .collation
281
281
if len (collationName ) == 0 {
282
- collationName = DEFAULT_COLLATION_NAME
282
+ collationName = mysql . DEFAULT_COLLATION_NAME
283
283
}
284
284
collation , err := charset .GetCollationByName (collationName )
285
285
if err != nil {
@@ -347,7 +347,7 @@ func (c *Conn) writeAuthHandshake() error {
347
347
pos += copy (data [pos :], attrData )
348
348
}
349
349
350
- if c .ccaps & CLIENT_ZSTD_COMPRESSION_ALGORITHM > 0 {
350
+ if c .ccaps & mysql . CLIENT_ZSTD_COMPRESSION_ALGORITHM > 0 {
351
351
// zstd_compression_level
352
352
data [pos ] = 0x03
353
353
}
0 commit comments