forked from moodrain/mysql-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (55 loc) · 1.4 KB
/
main.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
package main
import (
"mysql-proxy/lib"
"net"
"runtime"
"sync"
)
func main() {
mysqlUrl := ":3306" // 数据库的端口
listen := ":8899" // 对外暴露的端口
connCount := runtime.NumCPU()
//connCount := 1 // local 只是开启一个 , navicat 是开启多个连接的,开启一个有问题
server, _ := net.Listen("tcp", listen)
connList := make([]lib.ProxyConn, connCount)
for i := 0; i < connCount; i++ {
proxy := lib.ProxyConn{}
connList[i] = proxy
}
wg := sync.WaitGroup{}
wg.Add(1)
for i := 0; i < connCount; i++ {
go func(proxy lib.ProxyConn) {
// 一: client , mysql的连接信息
// 初始化mysql client
proxy.NewClientConn(server)
// 初始化mysql server
proxy.NewMysqlConn(mysqlUrl)
// 类似于登录过程,换token存储于:FinishHandshakePacket , auth操作
err := proxy.Handshake()
if err != nil {
proxy.Close()
}
// 读取mysql server->client
go proxy.PipeMysql2Client()
// 读取mysql client->server
go proxy.PipeClient2Mysql()
for {
// 二: 请求客户端的连接
// todo 待优化
//if !proxy.IsClientClose() {
// fmt.Println("xxx", time.Now())
// continue
//}
proxy.NewClientConn(server)
// TODO 这个也需要再次auth吗
//err := proxy.FakeHandshake()
//if err != nil {
// proxy.CloseClient()
//}
go proxy.PipeClient2Mysql()
}
}(connList[i])
}
wg.Wait()
}