From 633de52aee8b72c252d8fd030df14f9164c2e9f3 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Mon, 1 Feb 2021 20:06:45 +0800 Subject: [PATCH] Fix: wrap net.Conn to avoid using *net.TCPConn.(ReadFrom) (#1209) --- common/net/io.go | 11 +++++++++++ tunnel/connection.go | 7 +++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 common/net/io.go diff --git a/common/net/io.go b/common/net/io.go new file mode 100644 index 00000000..5bb4f002 --- /dev/null +++ b/common/net/io.go @@ -0,0 +1,11 @@ +package net + +import "io" + +type ReadOnlyReader struct { + io.Reader +} + +type WriteOnlyWriter struct { + io.Writer +} diff --git a/tunnel/connection.go b/tunnel/connection.go index 77671a4b..b3554a47 100644 --- a/tunnel/connection.go +++ b/tunnel/connection.go @@ -10,6 +10,7 @@ import ( "time" "github.com/Dreamacro/clash/adapters/inbound" + N "github.com/Dreamacro/clash/common/net" "github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/component/resolver" C "github.com/Dreamacro/clash/constant" @@ -141,14 +142,16 @@ func relay(leftConn, rightConn net.Conn) { go func() { buf := pool.Get(pool.RelayBufferSize) - _, err := io.CopyBuffer(leftConn, rightConn, buf) + // 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(rightConn, leftConn, buf) + io.CopyBuffer(N.WriteOnlyWriter{Writer: rightConn}, N.ReadOnlyReader{Reader: leftConn}, buf) pool.Put(buf) rightConn.SetReadDeadline(time.Now()) <-ch