-
Notifications
You must be signed in to change notification settings - Fork 26
/
proxy.go
118 lines (97 loc) · 3.02 KB
/
proxy.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
package main
import (
"fmt"
"github.com/orderbynull/lottip/chat"
"github.com/orderbynull/lottip/protocol"
"io"
"log"
"net"
"time"
)
type RequestPacketParser struct {
connId string
queryId *int
queryChan chan chat.Cmd
connStateChan chan chat.ConnState
timer *time.Time
}
func (pp *RequestPacketParser) Write(p []byte) (n int, err error) {
*pp.queryId++
*pp.timer = time.Now()
switch protocol.GetPacketType(p) {
case protocol.ComStmtPrepare:
case protocol.ComQuery:
decoded, err := protocol.DecodeQueryRequest(p)
if err == nil {
pp.queryChan <- chat.Cmd{pp.connId, *pp.queryId, "", decoded.Query, nil, false}
}
case protocol.ComQuit:
pp.connStateChan <- chat.ConnState{pp.connId, protocol.ConnStateFinished}
}
return len(p), nil
}
type ResponsePacketParser struct {
connId string
queryId *int
queryResultChan chan chat.CmdResult
timer *time.Time
}
func (pp *ResponsePacketParser) Write(p []byte) (n int, err error) {
duration := fmt.Sprintf("%.3f", time.Since(*pp.timer).Seconds())
switch protocol.GetPacketType(p) {
case protocol.ResponseErr:
decoded, _ := protocol.DecodeErrResponse(p)
pp.queryResultChan <- chat.CmdResult{pp.connId, *pp.queryId, protocol.ResponseErr, decoded, duration}
default:
pp.queryResultChan <- chat.CmdResult{pp.connId, *pp.queryId, protocol.ResponseOk, "", duration}
}
return len(p), nil
}
// MySQLProxyServer implements server for capturing and forwarding MySQL traffic.
type MySQLProxyServer struct {
cmdChan chan chat.Cmd
cmdResultChan chan chat.CmdResult
connStateChan chan chat.ConnState
appReadyChan chan bool
mysqlHost string
proxyHost string
}
// run starts accepting TCP connection and forwarding it to MySQL server.
// Each incoming TCP connection is handled in own goroutine.
func (p *MySQLProxyServer) run() {
listener, err := net.Listen("tcp", p.proxyHost)
if err != nil {
log.Fatal(err.Error())
}
defer listener.Close()
go func() {
p.appReadyChan <- true
close(p.appReadyChan)
}()
for {
client, err := listener.Accept()
if err != nil {
log.Print(err.Error())
}
go p.handleConnection(client)
}
}
// handleConnection ...
func (p *MySQLProxyServer) handleConnection(client net.Conn) {
defer client.Close()
// New connection to MySQL is made per each incoming TCP request to MySQLProxyServer server.
server, err := net.Dial("tcp", p.mysqlHost)
if err != nil {
log.Print(err.Error())
return
}
defer server.Close()
connId := fmt.Sprintf("%s => %s", client.RemoteAddr().String(), server.RemoteAddr().String())
defer func() { p.connStateChan <- chat.ConnState{connId, protocol.ConnStateFinished} }()
var queryId int
var timer time.Time
// Copy bytes from client to server and requestParser
go io.Copy(io.MultiWriter(server, &RequestPacketParser{connId, &queryId, p.cmdChan, p.connStateChan, &timer}), client)
// Copy bytes from server to client and responseParser
io.Copy(io.MultiWriter(client, &ResponsePacketParser{connId, &queryId, p.cmdResultChan, &timer}), server)
}