net/wsconn: accept a remote addr string and plumb it through

This makes wsconn.Conns somewhat present reasonably when they are
the client of an http.Request, rather than just put a placeholder
in that field.

Updates tailscale/corp#13777

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2023-08-29 16:27:30 -07:00
committed by Dave Anderson
parent e952564b59
commit 8b492b4121
5 changed files with 24 additions and 11 deletions

View File

@@ -48,10 +48,18 @@ import (
//
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
// io.EOF when reading.
func NetConn(ctx context.Context, c *websocket.Conn, msgType websocket.MessageType) net.Conn {
//
// The given remoteAddr will be the value of the returned conn's
// RemoteAddr().String(). For best compatibility with consumers of
// conns, the string should be an ip:port if available, but in the
// absence of that it can be any string that describes the remote
// endpoint, or the empty string to makes RemoteAddr() return a place
// holder value.
func NetConn(ctx context.Context, c *websocket.Conn, msgType websocket.MessageType, remoteAddr string) net.Conn {
nc := &netConn{
c: c,
msgType: msgType,
c: c,
msgType: msgType,
remoteAddr: remoteAddr,
}
var writeCancel context.CancelFunc
@@ -82,8 +90,9 @@ func NetConn(ctx context.Context, c *websocket.Conn, msgType websocket.MessageTy
}
type netConn struct {
c *websocket.Conn
msgType websocket.MessageType
c *websocket.Conn
msgType websocket.MessageType
remoteAddr string
writeTimer *time.Timer
writeContext context.Context
@@ -167,6 +176,7 @@ func (c *netConn) Read(p []byte) (int, error) {
}
type websocketAddr struct {
addr string
}
func (a websocketAddr) Network() string {
@@ -174,15 +184,18 @@ func (a websocketAddr) Network() string {
}
func (a websocketAddr) String() string {
if a.addr != "" {
return a.addr
}
return "websocket/unknown-addr"
}
func (c *netConn) RemoteAddr() net.Addr {
return websocketAddr{}
return websocketAddr{c.remoteAddr}
}
func (c *netConn) LocalAddr() net.Addr {
return websocketAddr{}
return websocketAddr{""}
}
func (c *netConn) SetDeadline(t time.Time) error {