mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
tcpproxy propagates a backend's close to the client by calling CloseWrite on the source connection. connHalf did not implement it, so the call was a no-op and the client never saw EOF. TestTCPRoundRobinHandler only unblocked the proxy's remaining copy goroutine by writing more data after the first echo. When the backend closed with that data still buffered in the pipe, the write was discarded and the test hung until the 10 minute timeout. Implement both halves of the half-close contract on connHalf. A new test covers buffered data surviving CloseWrite, EOF on the read after it, and writes failing after CloseRead. Fixes #17533 Signed-off-by: Yunus Emre Karaman <[email protected]>
128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package memnet
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"time"
|
|
)
|
|
|
|
// NetworkName is the network name returned by [net.Addr.Network]
|
|
// for [net.Conn.LocalAddr] and [net.Conn.RemoteAddr] from the [Conn] type.
|
|
const NetworkName = "mem"
|
|
|
|
// Conn is a net.Conn that can additionally have its reads and writes blocked and unblocked.
|
|
type Conn interface {
|
|
net.Conn
|
|
|
|
// SetReadBlock blocks or unblocks the Read method of this Conn.
|
|
// It reports an error if the existing value matches the new value,
|
|
// or if the Conn has been Closed.
|
|
SetReadBlock(bool) error
|
|
|
|
// SetWriteBlock blocks or unblocks the Write method of this Conn.
|
|
// It reports an error if the existing value matches the new value,
|
|
// or if the Conn has been Closed.
|
|
SetWriteBlock(bool) error
|
|
}
|
|
|
|
// NewConn creates a pair of Conns that are wired together by pipes.
|
|
func NewConn(name string, maxBuf int) (Conn, Conn) {
|
|
r := NewPipe(name+"|0", maxBuf)
|
|
w := NewPipe(name+"|1", maxBuf)
|
|
|
|
return &connHalf{r: r, w: w}, &connHalf{r: w, w: r}
|
|
}
|
|
|
|
// NewTCPConn creates a pair of Conns that are wired together by pipes.
|
|
func NewTCPConn(src, dst netip.AddrPort, maxBuf int) (local Conn, remote Conn) {
|
|
r := NewPipe(src.String(), maxBuf)
|
|
w := NewPipe(dst.String(), maxBuf)
|
|
|
|
lAddr := net.TCPAddrFromAddrPort(src)
|
|
rAddr := net.TCPAddrFromAddrPort(dst)
|
|
|
|
return &connHalf{r: r, w: w, remote: rAddr, local: lAddr}, &connHalf{r: w, w: r, remote: lAddr, local: rAddr}
|
|
}
|
|
|
|
type connAddr string
|
|
|
|
func (a connAddr) Network() string { return NetworkName }
|
|
func (a connAddr) String() string { return string(a) }
|
|
|
|
type connHalf struct {
|
|
local, remote net.Addr
|
|
r, w *Pipe
|
|
}
|
|
|
|
func (c *connHalf) LocalAddr() net.Addr {
|
|
if c.local != nil {
|
|
return c.local
|
|
}
|
|
return connAddr(c.r.name)
|
|
}
|
|
|
|
func (c *connHalf) RemoteAddr() net.Addr {
|
|
if c.remote != nil {
|
|
return c.remote
|
|
}
|
|
return connAddr(c.w.name)
|
|
}
|
|
|
|
func (c *connHalf) Read(b []byte) (n int, err error) {
|
|
return c.r.Read(b)
|
|
}
|
|
func (c *connHalf) Write(b []byte) (n int, err error) {
|
|
return c.w.Write(b)
|
|
}
|
|
|
|
func (c *connHalf) Close() error {
|
|
if err := c.w.Close(); err != nil {
|
|
return err
|
|
}
|
|
return c.r.Close()
|
|
}
|
|
|
|
// CloseWrite closes the write side of the connection. The peer can read
|
|
// buffered data, then gets io.EOF. It matches net.TCPConn.CloseWrite, which
|
|
// proxies such as tcpproxy rely on to propagate a backend's close.
|
|
func (c *connHalf) CloseWrite() error {
|
|
return c.w.Close()
|
|
}
|
|
|
|
// CloseRead closes the read side of the connection. Subsequent reads return
|
|
// io.EOF and the peer's writes fail.
|
|
func (c *connHalf) CloseRead() error {
|
|
return c.r.Close()
|
|
}
|
|
|
|
func (c *connHalf) SetDeadline(t time.Time) error {
|
|
err1 := c.SetReadDeadline(t)
|
|
err2 := c.SetWriteDeadline(t)
|
|
if err1 != nil {
|
|
return err1
|
|
}
|
|
return err2
|
|
}
|
|
func (c *connHalf) SetReadDeadline(t time.Time) error {
|
|
return c.r.SetReadDeadline(t)
|
|
}
|
|
func (c *connHalf) SetWriteDeadline(t time.Time) error {
|
|
return c.w.SetWriteDeadline(t)
|
|
}
|
|
|
|
func (c *connHalf) SetReadBlock(b bool) error {
|
|
if b {
|
|
return c.r.Block()
|
|
}
|
|
return c.r.Unblock()
|
|
}
|
|
func (c *connHalf) SetWriteBlock(b bool) error {
|
|
if b {
|
|
return c.w.Block()
|
|
}
|
|
return c.w.Unblock()
|
|
}
|