mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 03:25:35 +00:00
9b71008ef2
This turns 'dialParams' into something more like net.Dialer, where configuration fields are public on the struct. Split out of #5648 Change-Id: I0c56fd151dc5489c3c94fb40d18fd639e06473bc Signed-off-by: Andrew Dunham <andrew@tailscale.com>
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
// Copyright (c) 2022 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 controlhttp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"net"
|
|
"net/url"
|
|
|
|
"nhooyr.io/websocket"
|
|
"tailscale.com/control/controlbase"
|
|
)
|
|
|
|
// Variant of Dial that tunnels the request over WebSockets, since we cannot do
|
|
// bi-directional communication over an HTTP connection when in JS.
|
|
func (d *Dialer) Dial(ctx context.Context) (*controlbase.Conn, error) {
|
|
if d.Hostname == "" {
|
|
return nil, errors.New("required Dialer.Hostname empty")
|
|
}
|
|
|
|
init, cont, err := controlbase.ClientDeferred(d.MachineKey, d.ControlKey, d.ProtocolVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
wsScheme := "wss"
|
|
host := d.Hostname
|
|
if host == "localhost" {
|
|
wsScheme = "ws"
|
|
host = net.JoinHostPort(host, strDef(d.HTTPPort, "80"))
|
|
}
|
|
wsURL := &url.URL{
|
|
Scheme: wsScheme,
|
|
Host: host,
|
|
Path: serverUpgradePath,
|
|
// Can't set HTTP headers on the websocket request, so we have to to send
|
|
// the handshake via an HTTP header.
|
|
RawQuery: url.Values{
|
|
handshakeHeaderName: []string{base64.StdEncoding.EncodeToString(init)},
|
|
}.Encode(),
|
|
}
|
|
wsConn, _, err := websocket.Dial(ctx, wsURL.String(), &websocket.DialOptions{
|
|
Subprotocols: []string{upgradeHeaderValue},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
netConn := websocket.NetConn(context.Background(), wsConn, websocket.MessageBinary)
|
|
cbConn, err := cont(ctx, netConn)
|
|
if err != nil {
|
|
netConn.Close()
|
|
return nil, err
|
|
}
|
|
return cbConn, nil
|
|
}
|