-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
72 lines (65 loc) · 1.77 KB
/
client.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
package main
import (
"encoding/binary"
"errors"
"fmt"
"github.com/go-sql-driver/mysql"
"net"
)
func NewClient(dsn string) (net.Conn, error) {
conf, err := mysql.ParseDSN(dsn)
if err != nil {
fmt.Errorf("invalid dsn format:%s\n", dsn)
return nil, errors.New("invalid dsn format")
}
conn, err := net.Dial("tcp", conf.Addr)
if err != nil {
return nil, err
}
greeting := readOnePacket(conn)
part1 := greeting[16:24]
part2 := greeting[43:55]
salt := append(part1, part2...)
hash := GetNativePwdHash(conf.Passwd, salt)
packet := make([]byte, 0)
// client capabilities
packet = binary.LittleEndian.AppendUint16(packet, 0xa685)
// extended client capabilities
packet = binary.LittleEndian.AppendUint16(packet, 0x20ff)
// max packet
packet = binary.LittleEndian.AppendUint32(packet, 16777216)
// charset
packet = append(packet, 0x21)
// unused
packet = append(packet, make([]byte, 23, 23)...)
// username
packet = append(packet, []byte(conf.User+"\u0000")...)
// password length
packet = append(packet, uint8(len(hash)))
// password
packet = append(packet, hash...)
// auth plugin
packet = append(packet, []byte("mysql_native_password\u0000")...)
// attribute
attr := make(map[string]string)
attr["_os"] = "linux"
attr["_client_name"] = "libmariadb"
attr["_pid"] = "177733"
attr["_client_version"] = "3.3.2"
attr["_platform"] = "x86_64"
attr["program_name"] = "mysql"
attr["_server_host"] = "127.0.0.1"
tmp := make([]byte, 0)
for k, v := range attr {
tmp = append(tmp, uint8(len(k)))
tmp = append(tmp, []byte(k)...)
tmp = append(tmp, uint8(len(v)))
tmp = append(tmp, []byte(v)...)
}
packet = append(packet, uint8(len(tmp)))
packet = append(packet, tmp...)
buf := addMysqlHeader(packet, 1)
writeBody(conn, buf)
readOnePacket(conn)
return conn, nil
}