2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-06-02 23:20:42 +00:00
|
|
|
|
|
|
|
package controlhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2022-09-16 19:06:25 +00:00
|
|
|
"errors"
|
2022-06-02 23:20:42 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
|
2024-08-09 19:32:24 +00:00
|
|
|
"github.com/coder/websocket"
|
2022-06-02 23:20:42 +00:00
|
|
|
"tailscale.com/control/controlbase"
|
2024-11-08 00:49:47 +00:00
|
|
|
"tailscale.com/control/controlhttp/controlhttpcommon"
|
2022-10-18 21:20:43 +00:00
|
|
|
"tailscale.com/net/wsconn"
|
2022-06-02 23:20:42 +00:00
|
|
|
)
|
|
|
|
|
2022-06-08 21:56:52 +00:00
|
|
|
// Variant of Dial that tunnels the request over WebSockets, since we cannot do
|
2022-06-02 23:20:42 +00:00
|
|
|
// bi-directional communication over an HTTP connection when in JS.
|
2022-10-17 21:50:52 +00:00
|
|
|
func (d *Dialer) Dial(ctx context.Context) (*ClientConn, error) {
|
2022-09-16 19:06:25 +00:00
|
|
|
if d.Hostname == "" {
|
|
|
|
return nil, errors.New("required Dialer.Hostname empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
init, cont, err := controlbase.ClientDeferred(d.MachineKey, d.ControlKey, d.ProtocolVersion)
|
2022-06-02 23:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:56:52 +00:00
|
|
|
wsScheme := "wss"
|
2022-09-16 19:06:25 +00:00
|
|
|
host := d.Hostname
|
2022-09-26 20:50:26 +00:00
|
|
|
// If using a custom control server (on a non-standard port), prefer that.
|
|
|
|
// This mirrors the port selection in newNoiseClient from noise.go.
|
|
|
|
if d.HTTPPort != "" && d.HTTPPort != "80" && d.HTTPSPort == "443" {
|
2022-06-08 21:56:52 +00:00
|
|
|
wsScheme = "ws"
|
2022-09-26 20:50:26 +00:00
|
|
|
host = net.JoinHostPort(host, d.HTTPPort)
|
2022-06-08 21:56:52 +00:00
|
|
|
}
|
2022-06-02 23:20:42 +00:00
|
|
|
wsURL := &url.URL{
|
2022-06-08 21:56:52 +00:00
|
|
|
Scheme: wsScheme,
|
2022-08-15 13:47:12 +00:00
|
|
|
Host: host,
|
2022-06-02 23:20:42 +00:00
|
|
|
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{
|
2024-11-08 00:49:47 +00:00
|
|
|
controlhttpcommon.HandshakeHeaderName: []string{base64.StdEncoding.EncodeToString(init)},
|
2022-06-02 23:20:42 +00:00
|
|
|
}.Encode(),
|
|
|
|
}
|
2022-10-28 19:14:58 +00:00
|
|
|
wsConn, _, err := websocket.Dial(ctx, wsURL.String(), &websocket.DialOptions{
|
2024-11-08 00:49:47 +00:00
|
|
|
Subprotocols: []string{controlhttpcommon.UpgradeHeaderValue},
|
2022-06-02 23:20:42 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-29 23:27:30 +00:00
|
|
|
netConn := wsconn.NetConn(context.Background(), wsConn, websocket.MessageBinary, wsURL.String())
|
2022-06-02 23:20:42 +00:00
|
|
|
cbConn, err := cont(ctx, netConn)
|
|
|
|
if err != nil {
|
|
|
|
netConn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-28 19:14:58 +00:00
|
|
|
return &ClientConn{Conn: cbConn}, nil
|
2022-06-02 23:20:42 +00:00
|
|
|
}
|