Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions host/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,15 @@ func (h *Host) handleConnect(w http.ResponseWriter, r *http.Request) {
}()
go func() {
io.Copy(client, upstream)
// The hijacked conn comes from proxymux, whose wrapper hides the
// underlying TCP CloseWrite. Without a half-close the browser would
// never see the tailnet side go away and would keep a dead pooled
// connection open (#152), so close the client outright instead. That
// also unblocks the client-to-upstream copy.
if cw, ok := client.(interface{ CloseWrite() error }); ok {
cw.CloseWrite()
} else {
client.Close()
}
done <- struct{}{}
}()
Expand Down
48 changes: 48 additions & 0 deletions host/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,54 @@ func TestHandleConnectForwardsBufferedClientBytes(t *testing.T) {
}
}

// TestHandleConnectClosesClientWhenUpstreamCloses covers a hijacked conn that
// cannot half-close, like proxymux's wrapper (#152): the browser must still see
// EOF once the tailnet side closes, without closing its own side first.
func TestHandleConnectClosesClientWhenUpstreamCloses(t *testing.T) {
h := newHost(nil, nil)
upstream, target := net.Pipe()
h.proxyDial = func(context.Context, string, string) (net.Conn, error) {
return upstream, nil
}
serverConn, browserConn := net.Pipe()
defer browserConn.Close()
if _, ok := serverConn.(interface{ CloseWrite() error }); ok {
t.Fatal("test client conn must not support CloseWrite")
}
w := &hijackTestWriter{
header: make(http.Header),
conn: serverConn,
buffered: bufio.NewReadWriter(bufio.NewReader(serverConn), bufio.NewWriter(serverConn)),
}
req := httptest.NewRequest(http.MethodConnect, "http://peer.example", nil)
req.Host = "peer.example:443"
done := make(chan struct{})
go func() {
h.handleConnect(w, req)
close(done)
}()

const reply = "goodbye"
go func() {
target.Write([]byte(reply))
target.Close()
}()

browserConn.SetReadDeadline(time.Now().Add(2 * time.Second))
got, err := io.ReadAll(browserConn)
if err != nil {
t.Fatalf("browser did not see EOF after upstream closed: %v", err)
}
if want := "HTTP/1.1 200 Connection Established\r\n\r\n" + reply; string(got) != want {
t.Fatalf("browser read %q, want %q", got, want)
}
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("CONNECT handler did not exit after upstream closed")
}
}

type hijackTestWriter struct {
header http.Header
conn net.Conn
Expand Down
Loading