I'm working on MySQL proxy with browser based GUI. Core of an app looks like:
//Trying to connect to server
rightConn, err := net.Dial("tcp", l.rightAddr)
if err != nil {
return
}
defer rightConn.Close()
wg.Add(2)
//Start passing packets from client to server
go func() {
defer wg.Done()
l.Pipe(leftConn, rightConn)
}()
//Start passing packets from server to client
go func() {
defer wg.Done()
l.Pipe(rightConn, leftConn)
}()
wg.Wait()
And here's definition of Pipe:
func (l *Lottip) Pipe(right, left net.Conn) {
for {
pkt, err := mysql.ReadPacket(right)
if err != nil {
break
}
if _, err = mysql.WritePacket(pkt, left); err != nil {
break
}
}
}
The reason i'm using my custom proxy function instead of
go io.Copy(left, right)
go io.Copy(right, left)
is i have to parse each MySQL's Request/Response packet and prepare it for further processing.
As we all know MySQL client can send a lot of queries and get responses in random order.
The problem is i cannot get how to match request to it's response. While i'm piping using 2 goroutines(1 per direction) i can easily read server response but i don't know to what request it responding.
I've examined request/response between MySQL client and server via Wireshark and found no mention for packet_id or query_id or similar