From edc90ebc612e9e68316c1c9c1b2d1ddf86079c88 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Mon, 6 Jun 2022 16:13:15 -0700 Subject: [PATCH] net/wsconn: remove homegrown wrapper for turning a websocket.Conn into a net.Conn The one from the nhooyr/websocket package seems to work equally well. Signed-off-by: Mihai Parparita --- cmd/derper/websocket.go | 4 +- cmd/tailscale/depaware.txt | 3 +- cmd/tailscaled/depaware.txt | 1 - control/controlhttp/client_js.go | 3 +- control/controlhttp/server.go | 3 +- derp/derphttp/websocket.go | 4 +- net/wsconn/wsconn.go | 104 ------------------------------- 7 files changed, 7 insertions(+), 115 deletions(-) delete mode 100644 net/wsconn/wsconn.go diff --git a/cmd/derper/websocket.go b/cmd/derper/websocket.go index 141f38078..b3c7eb7c9 100644 --- a/cmd/derper/websocket.go +++ b/cmd/derper/websocket.go @@ -6,6 +6,7 @@ package main import ( "bufio" + "context" "expvar" "log" "net/http" @@ -13,7 +14,6 @@ import ( "nhooyr.io/websocket" "tailscale.com/derp" - "tailscale.com/net/wsconn" ) var counterWebSocketAccepts = expvar.NewInt("derp_websocket_accepts") @@ -45,7 +45,7 @@ func addWebSocketSupport(s *derp.Server, base http.Handler) http.Handler { return } counterWebSocketAccepts.Add(1) - wc := wsconn.New(c) + wc := websocket.NetConn(context.Background(), c, websocket.MessageBinary) brw := bufio.NewReadWriter(bufio.NewReader(wc), bufio.NewWriter(wc)) s.Accept(wc, brw, r.RemoteAddr) }) diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 56d4e0b44..8912ff566 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -31,7 +31,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep go4.org/unsafe/assume-no-moving-gc from go4.org/intern W 💣 golang.zx2c4.com/wireguard/windows/tunnel/winipcfg from tailscale.com/net/interfaces+ inet.af/netaddr from tailscale.com/cmd/tailscale/cli+ - L nhooyr.io/websocket from tailscale.com/derp/derphttp+ + L nhooyr.io/websocket from tailscale.com/derp/derphttp L nhooyr.io/websocket/internal/errd from nhooyr.io/websocket L nhooyr.io/websocket/internal/xsync from nhooyr.io/websocket tailscale.com from tailscale.com/version @@ -62,7 +62,6 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/net/tlsdial from tailscale.com/derp/derphttp tailscale.com/net/tsaddr from tailscale.com/net/interfaces+ 💣 tailscale.com/net/tshttpproxy from tailscale.com/derp/derphttp+ - L tailscale.com/net/wsconn from tailscale.com/derp/derphttp tailscale.com/paths from tailscale.com/cmd/tailscale/cli+ tailscale.com/safesocket from tailscale.com/cmd/tailscale/cli+ tailscale.com/syncs from tailscale.com/net/interfaces+ diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index 9b8afbc25..5901580cc 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -231,7 +231,6 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/net/tsdial from tailscale.com/control/controlclient+ 💣 tailscale.com/net/tshttpproxy from tailscale.com/control/controlclient+ tailscale.com/net/tstun from tailscale.com/net/dns+ - tailscale.com/net/wsconn from tailscale.com/control/controlhttp+ tailscale.com/paths from tailscale.com/ipn/ipnlocal+ tailscale.com/portlist from tailscale.com/ipn/ipnlocal tailscale.com/safesocket from tailscale.com/client/tailscale+ diff --git a/control/controlhttp/client_js.go b/control/controlhttp/client_js.go index 17b4998ca..b59980d83 100644 --- a/control/controlhttp/client_js.go +++ b/control/controlhttp/client_js.go @@ -13,7 +13,6 @@ import ( "nhooyr.io/websocket" "tailscale.com/control/controlbase" "tailscale.com/net/dnscache" - "tailscale.com/net/wsconn" "tailscale.com/types/key" ) @@ -45,7 +44,7 @@ func Dial(ctx context.Context, addr string, machineKey key.MachinePrivate, contr if err != nil { return nil, err } - netConn := wsconn.New(wsConn) + netConn := websocket.NetConn(context.Background(), wsConn, websocket.MessageBinary) cbConn, err := cont(ctx, netConn) if err != nil { netConn.Close() diff --git a/control/controlhttp/server.go b/control/controlhttp/server.go index 54af7433a..816d17ca3 100644 --- a/control/controlhttp/server.go +++ b/control/controlhttp/server.go @@ -14,7 +14,6 @@ import ( "nhooyr.io/websocket" "tailscale.com/control/controlbase" "tailscale.com/net/netutil" - "tailscale.com/net/wsconn" "tailscale.com/types/key" ) @@ -106,7 +105,7 @@ func acceptWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request return nil, fmt.Errorf("decoding base64 handshake parameter: %v", err) } - conn := wsconn.New(c) + conn := websocket.NetConn(ctx, c, websocket.MessageBinary) nc, err := controlbase.Server(ctx, conn, private, init) if err != nil { conn.Close() diff --git a/derp/derphttp/websocket.go b/derp/derphttp/websocket.go index dcc41feb1..110c5fec5 100644 --- a/derp/derphttp/websocket.go +++ b/derp/derphttp/websocket.go @@ -13,7 +13,6 @@ import ( "net" "nhooyr.io/websocket" - "tailscale.com/net/wsconn" ) func init() { @@ -29,5 +28,6 @@ func dialWebsocket(ctx context.Context, urlStr string) (net.Conn, error) { return nil, err } log.Printf("websocket: connected to %v", urlStr) - return wsconn.New(c), nil + netConn := websocket.NetConn(context.Background(), c, websocket.MessageBinary) + return netConn, nil } diff --git a/net/wsconn/wsconn.go b/net/wsconn/wsconn.go deleted file mode 100644 index dbb07ba4a..000000000 --- a/net/wsconn/wsconn.go +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package wsconn contains an adapter type that turns -// a websocket connection into a net.Conn. -package wsconn - -import ( - "context" - "net" - "sync" - "time" - - "nhooyr.io/websocket" -) - -// New returns a net.Conn wrapper around c, -// using c to send and receive binary messages with -// chunks of bytes with no defined framing, effectively -// discarding all WebSocket-level message framing. -func New(c *websocket.Conn) net.Conn { - return &websocketConn{c: c} -} - -// websocketConn implements net.Conn around a *websocket.Conn, -// treating a websocket.Conn as a byte stream, ignoring the WebSocket -// frame/message boundaries. -type websocketConn struct { - c *websocket.Conn - - // rextra are extra bytes owned by the reader. - rextra []byte - - mu sync.Mutex - rdeadline time.Time - cancelRead context.CancelFunc -} - -func (wc *websocketConn) LocalAddr() net.Addr { return addr{} } -func (wc *websocketConn) RemoteAddr() net.Addr { return addr{} } - -type addr struct{} - -func (addr) Network() string { return "websocket" } -func (addr) String() string { return "websocket" } - -func (wc *websocketConn) Read(p []byte) (n int, err error) { - // Drain any leftover from previously. - n = copy(p, wc.rextra) - if n > 0 { - wc.rextra = wc.rextra[n:] - return n, nil - } - - var ctx context.Context - var cancel context.CancelFunc - - wc.mu.Lock() - if dl := wc.rdeadline; !dl.IsZero() { - ctx, cancel = context.WithDeadline(context.Background(), wc.rdeadline) - } else { - ctx, cancel = context.WithDeadline(context.Background(), time.Now().Add(30*24*time.Hour)) - wc.rdeadline = time.Time{} - } - wc.cancelRead = cancel - wc.mu.Unlock() - defer cancel() - - _, buf, err := wc.c.Read(ctx) - n = copy(p, buf) - wc.rextra = buf[n:] - return n, err -} - -func (wc *websocketConn) Write(p []byte) (n int, err error) { - err = wc.c.Write(context.Background(), websocket.MessageBinary, p) - if err != nil { - return 0, err - } - return len(p), nil -} - -func (wc *websocketConn) Close() error { return wc.c.Close(websocket.StatusNormalClosure, "close") } - -func (wc *websocketConn) SetDeadline(t time.Time) error { - wc.SetReadDeadline(t) - wc.SetWriteDeadline(t) - return nil -} - -func (wc *websocketConn) SetReadDeadline(t time.Time) error { - wc.mu.Lock() - defer wc.mu.Unlock() - if !t.IsZero() && (wc.rdeadline.IsZero() || t.Before(wc.rdeadline)) && wc.cancelRead != nil { - wc.cancelRead() - } - wc.rdeadline = t - return nil -} - -func (wc *websocketConn) SetWriteDeadline(t time.Time) error { - return nil -}