```
func (client *ForwardInfo) StartOneForward(tcpConn net.Conn) {
defer tcpConn.Close()
//连接目标地址1
conn1, err := net.DialTimeout("tcp", client.Dest1, 30*time.Second)
//连接目标地址2
conn2, err := net.DialTimeout("tcp", client.Dest2, 30*time.Second)
if err != nil {
public.ForwardLog.Error("转发出现异常 Forward to Dest Addr err:", err.Error())
return
}
defer func() {
conn1.Close()
conn2.Close()
}()
public.ForwardLog.Info("新用户 ", tcpConn.RemoteAddr().String(), "开始TCP转发 --> ", client.Dest1, ",", client.Dest2)
conns := ClientConn{
Uuid: uuid.New(),
SrcConn: tcpConn,
Dest1Conn: conn1,
Dest2Conn: conn2,
}
client.ClientInfoMap.Set(conns.Uuid, &conns)
go func() {
multiReader := io.MultiReader(conns.Dest1Conn, conns.Dest2Conn)
_, err := io.Copy(conns.SrcConn, multiReader)
if err != nil {
conns.StopForward(client) //关闭连接
}
}()
writer := io.MultiWriter(conns.Dest1Conn, conns.Dest2Conn)
_, err = io.Copy(writer, conns.SrcConn)
if err != nil {
conns.StopForward(client) //关闭连接
}
}
```
我正在用io.cpoy对进入的tcp连接复制到2条新连接上,发现有些tcp连接被关闭了,可是无法调用StopForward方法进行tcp关闭
有疑问加站长微信联系(非本文作者)