0
0
mirror of https://github.com/XTLS/REALITY.git synced 2025-08-22 14:38:35 +00:00

Call underlying.CloseWrite() once io.Copy() returned (#7)

Co-authored-by: Fangliding <Fangliding.fshxy@outlook.com>
This commit is contained in:
RPRX 2024-07-12 05:55:06 +00:00 committed by GitHub
parent ecc4401070
commit 48f0b2d5ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

16
tls.go
View File

@ -40,6 +40,11 @@ import (
"golang.org/x/crypto/hkdf"
)
type CloseWriteConn interface {
net.Conn
CloseWrite() error
}
type MirrorConn struct {
*sync.Mutex
net.Conn
@ -125,10 +130,11 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
}
}
underlying := conn
if pc, ok := underlying.(*proxyproto.Conn); ok {
underlying = pc.Raw() // for TCP splicing in io.Copy()
raw := conn
if pc, ok := conn.(*proxyproto.Conn); ok {
raw = pc.Raw() // for TCP splicing in io.Copy()
}
underlying := raw.(CloseWriteConn) // *net.TCPConn or *net.UnixConn
mutex := new(sync.Mutex)
@ -334,6 +340,10 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
}
conn.Write(s2cSaved)
io.Copy(underlying, target)
// Here is bidirectional direct forwarding:
// client ---underlying--- server ---target--- dest
// Call `underlying.CloseWrite()` once `io.Copy()` returned
underlying.CloseWrite()
}
waitGroup.Done()
}()