52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package proxy
|
|
|
|
import (
|
|
"net"
|
|
"zproxy/zlog"
|
|
)
|
|
|
|
type UClientProxy struct {
|
|
ServerConn *UPacketConnection
|
|
}
|
|
|
|
func NewClientProxy() *UClientProxy {
|
|
return &UClientProxy{}
|
|
}
|
|
func (client *UClientProxy) ServeTCP(serverAddr string, clientAddr string) error {
|
|
conn, err := net.Dial("tcp", serverAddr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
zlog.Infof("connect server %s", serverAddr)
|
|
client.ServerConn = NewPacketConnection(conn)
|
|
_, err = client.ServerConn.Write([]byte("anki"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for {
|
|
data := make([]byte, 4)
|
|
_, err := client.ServerConn.Read(data, 0, 4)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if string(data) != "anki" {
|
|
zlog.Error("rcv package error:", data)
|
|
client.ServerConn.Clear()
|
|
continue
|
|
}
|
|
conn, err := net.Dial("tcp", clientAddr)
|
|
if err != nil {
|
|
zlog.Error("connect local anki error: ", err.Error(), clientAddr)
|
|
client.ServerConn.ForwardError(err)
|
|
continue
|
|
}
|
|
pc := NewPacketConnection(conn)
|
|
client.ServerConn.Forward(pc)
|
|
pc.Forward(client.ServerConn)
|
|
err = pc.Close()
|
|
if err != nil {
|
|
zlog.Error("close local anki error: ", err.Error())
|
|
}
|
|
}
|
|
}
|