Improve: better relay copies

This commit is contained in:
gVisor bot 2018-09-17 00:15:58 +08:00
parent 40982266de
commit e8206f0f2b

View file

@ -3,6 +3,7 @@ package tunnel
import ( import (
"bufio" "bufio"
"io" "io"
"net"
"net/http" "net/http"
"github.com/Dreamacro/clash/adapters/local" "github.com/Dreamacro/clash/adapters/local"
@ -56,6 +57,18 @@ func (t *Tunnel) handleHTTP(request *adapters.HTTPAdapter, proxy C.ProxyAdapter)
func (t *Tunnel) handleSOCKS(request *adapters.SocketAdapter, proxy C.ProxyAdapter) { func (t *Tunnel) handleSOCKS(request *adapters.SocketAdapter, proxy C.ProxyAdapter) {
conn := newTrafficTrack(proxy.Conn(), t.traffic) conn := newTrafficTrack(proxy.Conn(), t.traffic)
go io.Copy(request.Conn(), conn) relay(request.Conn(), conn)
io.Copy(conn, request.Conn()) }
// relay copies between left and right bidirectionally.
func relay(leftConn, rightConn net.Conn) {
ch := make(chan error)
go func() {
_, err := io.Copy(leftConn, rightConn)
ch <- err
}()
io.Copy(rightConn, leftConn)
<-ch
} }