-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
201 lines (159 loc) · 4.01 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package server
import (
"errors"
"net"
"sync/atomic"
"github.com/siddontang/go/sync2"
. "github.com/892294101/go-mysql/mysql"
"github.com/892294101/go-mysql/packet"
)
/*
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
attributes map[string]string
connectionID uint32
status uint16
warnings 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)
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: RandomBuf(20),
}
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)
}
c := &Conn{
Conn: packetConn,
serverConf: serverConf,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
stmts: make(map[uint32]*Stmt),
salt: RandomBuf(20),
}
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) {
var usingPasswd uint16 = ER_YES
if errors.Is(err, ErrAccessDeniedNoPassword) {
usingPasswd = ER_NO
}
err = NewDefaultError(ER_ACCESS_DENIED_ERROR, c.user, c.RemoteAddr().String(), MySQLErrName[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) SetCapability(cap uint32) {
c.capability |= cap
}
func (c *Conn) UnsetCapability(cap uint32) {
c.capability &= ^cap
}
func (c *Conn) HasCapability(cap uint32) bool {
return c.capability&cap > 0
}
func (c *Conn) Charset() uint8 {
return c.charset
}
func (c *Conn) Attributes() map[string]string {
return c.attributes
}
func (c *Conn) ConnectionID() uint32 {
return c.connectionID
}
func (c *Conn) IsAutoCommit() bool {
return c.HasStatus(SERVER_STATUS_AUTOCOMMIT)
}
func (c *Conn) IsInTransaction() bool {
return c.HasStatus(SERVER_STATUS_IN_TRANS)
}
func (c *Conn) SetInTransaction() {
c.SetStatus(SERVER_STATUS_IN_TRANS)
}
func (c *Conn) ClearInTransaction() {
c.UnsetStatus(SERVER_STATUS_IN_TRANS)
}
func (c *Conn) SetStatus(status uint16) {
c.status |= status
}
func (c *Conn) UnsetStatus(status uint16) {
c.status &= ^status
}
func (c *Conn) HasStatus(status uint16) bool {
return c.status&status > 0
}
func (c *Conn) SetWarnings(warnings uint16) {
c.warnings = warnings
}