Skip to content

Commit

Permalink
Call underlying.CloseWrite() once io.Copy() returned (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Fangliding <[email protected]>
  • Loading branch information
RPRX and Fangliding committed Jul 12, 2024
1 parent ecc4401 commit 48f0b2d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ import (
"golang.org/x/crypto/hkdf"
)

type CloseWriteConn interface {
net.Conn
CloseWrite() error
}

type MirrorConn struct {
*sync.Mutex
net.Conn
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}()
Expand Down

0 comments on commit 48f0b2d

Please sign in to comment.