forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_example.go
51 lines (43 loc) · 1.51 KB
/
server_example.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
package main
import (
"net"
"github.com/siddontang/go-log/log"
"github.com/cookieY/go-mysql/mysql"
"github.com/cookieY/go-mysql/server"
"github.com/cookieY/go-mysql/test_util/test_keys"
"crypto/tls"
"time"
)
type RemoteThrottleProvider struct {
*server.InMemoryProvider
delay int // in milliseconds
}
func (m *RemoteThrottleProvider) GetCredential(username string) (password string, found bool, err error) {
time.Sleep(time.Millisecond * time.Duration(m.delay))
return m.InMemoryProvider.GetCredential(username)
}
func main() {
l, _ := net.Listen("tcp", "127.0.0.1:3306")
// user either the in-memory credential provider or the remote credential provider (you can implement your own)
//inMemProvider := server.NewInMemoryProvider()
//inMemProvider.AddUser("root", "123")
remoteProvider := &RemoteThrottleProvider{server.NewInMemoryProvider(), 10 + 50}
remoteProvider.AddUser("root", "123")
var tlsConf = server.NewServerTLSConfig(test_keys.CaPem, test_keys.CertPem, test_keys.KeyPem, tls.VerifyClientCertIfGiven)
for {
c, _ := l.Accept()
go func() {
// Create a connection with user root and an empty password.
// You can use your own handler to handle command here.
svr := server.NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, test_keys.PubPem, tlsConf)
conn, err := server.NewCustomizedConn(c, svr, remoteProvider, server.EmptyHandler{})
if err != nil {
log.Errorf("Connection error: %v", err)
return
}
for {
conn.HandleCommand()
}
}()
}
}