forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
168 lines (135 loc) · 3.41 KB
/
conn.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
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
package server
import (
"errors"
"net"
"sync/atomic"
. "github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/packet"
"github.com/siddontang/go/sync2"
)
/*
Conn acts like a MySQL server connection, you can use MySQL client to communicate with it.
*/
type Conn struct {
*packet.Conn
serverConf *Server
capability uint32
charset uint8
authPluginName string
connectionID uint32
status uint16
salt []byte // should be 8 + 12 for auth-plugin-data-part-1 and auth-plugin-data-part-2
credentialProvider CredentialProvider
user string
password string
cachingSha2FullAuth bool
h Handler
stmts map[uint32]*Stmt
stmtID uint32
closed sync2.AtomicBool
}
var baseConnID uint32 = 10000
// NewConn: create connection with default server settings
func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) {
p := NewInMemoryProvider()
p.AddUser(user, password)
salt, _ := RandomBuf(20)
var packetConn *packet.Conn
if defaultServer.tlsConfig != nil {
packetConn = packet.NewTLSConn(conn)
} else {
packetConn = packet.NewConn(conn)
}
c := &Conn{
Conn: packetConn,
serverConf: defaultServer,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
stmts: make(map[uint32]*Stmt),
salt: salt,
}
c.closed.Set(false)
if err := c.handshake(); err != nil {
c.Close()
return nil, err
}
return c, nil
}
// NewCustomizedConn: create connection with customized server settings
func NewCustomizedConn(conn net.Conn, serverConf *Server, p CredentialProvider, h Handler) (*Conn, error) {
var packetConn *packet.Conn
if serverConf.tlsConfig != nil {
packetConn = packet.NewTLSConn(conn)
} else {
packetConn = packet.NewConn(conn)
}
salt, _ := RandomBuf(20)
c := &Conn{
Conn: packetConn,
serverConf: serverConf,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
stmts: make(map[uint32]*Stmt),
salt: salt,
}
c.closed.Set(false)
if err := c.handshake(); err != nil {
c.Close()
return nil, err
}
return c, nil
}
func (c *Conn) handshake() error {
if err := c.writeInitialHandshake(); err != nil {
return err
}
if err := c.readHandshakeResponse(); err != nil {
if errors.Is(err, ErrAccessDenied) {
usingPasswd := ER_YES
if errors.Is(err, ErrAccessDeniedNoPassword) {
usingPasswd = ER_NO
}
err = NewDefaultError(ER_ACCESS_DENIED_ERROR, c.user, c.RemoteAddr().String(), usingPasswd)
}
_ = c.writeError(err)
return err
}
if err := c.writeOK(nil); err != nil {
return err
}
c.ResetSequence()
return nil
}
func (c *Conn) Close() {
c.closed.Set(true)
c.Conn.Close()
}
func (c *Conn) Closed() bool {
return c.closed.Get()
}
func (c *Conn) GetUser() string {
return c.user
}
func (c *Conn) Capability() uint32 {
return c.capability
}
func (c *Conn) Charset() uint8 {
return c.charset
}
func (c *Conn) ConnectionID() uint32 {
return c.connectionID
}
func (c *Conn) IsAutoCommit() bool {
return c.status&SERVER_STATUS_AUTOCOMMIT > 0
}
func (c *Conn) IsInTransaction() bool {
return c.status&SERVER_STATUS_IN_TRANS > 0
}
func (c *Conn) SetInTransaction() {
c.status |= SERVER_STATUS_IN_TRANS
}
func (c *Conn) ClearInTransaction() {
c.status &= ^SERVER_STATUS_IN_TRANS
}