Fix: should keep alive in tcp relay

This commit is contained in:
yaling888
2022-03-16 18:17:28 +08:00
parent 9ff32d9e29
commit 3a9bbf6c73
7 changed files with 57 additions and 35 deletions

View File

@ -70,6 +70,9 @@ func handleSocket(ctx C.ConnContext, outbound net.Conn) {
func relay(leftConn, rightConn net.Conn) {
ch := make(chan error)
tcpKeepAlive(leftConn)
tcpKeepAlive(rightConn)
go func() {
buf := pool.Get(pool.RelayBufferSize)
// Wrapping to avoid using *net.TCPConn.(ReadFrom)
@ -86,3 +89,9 @@ func relay(leftConn, rightConn net.Conn) {
rightConn.SetReadDeadline(time.Now())
<-ch
}
func tcpKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
}
}