From 48c7e53d7ac496fcc5c4cb44fdfffb00a696ef53 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Tue, 21 Jan 2025 10:32:18 -0800 Subject: tcpproxy: support half-close with gvisor conns (#46) gonet.TCPConn implements CloseRead and CloseWrite, but it is not a net.TCPConn. Use an interface to look for CloseRead and CloseWrite so that they are called on gonet.TCPConn. Updates tailscale/corp#25169 Signed-off-by: James Tucker --- tcpproxy.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tcpproxy.go b/tcpproxy.go index 1f03e32..745e2ba 100644 --- a/tcpproxy.go +++ b/tcpproxy.go @@ -355,16 +355,21 @@ func tcpConn(c net.Conn) (t *net.TCPConn, ok bool) { return nil, false } +type closeReader interface{ CloseRead() error } +type closeWriter interface{ CloseWrite() error } + func goCloseConn(c net.Conn) { go c.Close() } func closeRead(c net.Conn) { - if c, ok := tcpConn(c); ok { + // prefer the interfaces, for compatibility with e.g. gvisor/netstack. + if c, ok := UnderlyingConn(c).(closeReader); ok { c.CloseRead() } } func closeWrite(c net.Conn) { - if c, ok := tcpConn(c); ok { + // prefer the interfaces, for compatibility with e.g. gvisor/netstack. + if c, ok := UnderlyingConn(c).(closeWriter); ok { c.CloseWrite() } } -- cgit v1.2.3