Chore: merge branch 'ogn-dev' into tun-dev

This commit is contained in:
yaling888
2022-04-26 18:46:42 +08:00
5 changed files with 110 additions and 35 deletions

View File

@@ -2,7 +2,6 @@ package tunnel
import (
"errors"
"io"
"net"
"time"
@@ -63,31 +62,9 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr n
}
func handleSocket(ctx C.ConnContext, outbound net.Conn) {
relay(ctx.Conn(), outbound)
}
// relay copies between left and right bidirectionally.
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)
// See also https://github.com/Dreamacro/clash/pull/1209
_, err := io.CopyBuffer(N.WriteOnlyWriter{Writer: leftConn}, N.ReadOnlyReader{Reader: rightConn}, buf)
pool.Put(buf)
leftConn.SetReadDeadline(time.Now())
ch <- err
}()
buf := pool.Get(pool.RelayBufferSize)
io.CopyBuffer(N.WriteOnlyWriter{Writer: rightConn}, N.ReadOnlyReader{Reader: leftConn}, buf)
pool.Put(buf)
rightConn.SetReadDeadline(time.Now())
<-ch
tcpKeepAlive(ctx.Conn())
tcpKeepAlive(outbound)
N.Relay(ctx.Conn(), outbound)
}
func tcpKeepAlive(c net.Conn) {