client_js.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package controlhttp
  4. import (
  5. "context"
  6. "encoding/base64"
  7. "errors"
  8. "net"
  9. "net/url"
  10. "github.com/coder/websocket"
  11. "tailscale.com/control/controlbase"
  12. "tailscale.com/net/wsconn"
  13. )
  14. // Variant of Dial that tunnels the request over WebSockets, since we cannot do
  15. // bi-directional communication over an HTTP connection when in JS.
  16. func (d *Dialer) Dial(ctx context.Context) (*ClientConn, error) {
  17. if d.Hostname == "" {
  18. return nil, errors.New("required Dialer.Hostname empty")
  19. }
  20. init, cont, err := controlbase.ClientDeferred(d.MachineKey, d.ControlKey, d.ProtocolVersion)
  21. if err != nil {
  22. return nil, err
  23. }
  24. wsScheme := "wss"
  25. host := d.Hostname
  26. // If using a custom control server (on a non-standard port), prefer that.
  27. // This mirrors the port selection in newNoiseClient from noise.go.
  28. if d.HTTPPort != "" && d.HTTPPort != "80" && d.HTTPSPort == "443" {
  29. wsScheme = "ws"
  30. host = net.JoinHostPort(host, d.HTTPPort)
  31. }
  32. wsURL := &url.URL{
  33. Scheme: wsScheme,
  34. Host: host,
  35. Path: serverUpgradePath,
  36. // Can't set HTTP headers on the websocket request, so we have to to send
  37. // the handshake via an HTTP header.
  38. RawQuery: url.Values{
  39. handshakeHeaderName: []string{base64.StdEncoding.EncodeToString(init)},
  40. }.Encode(),
  41. }
  42. wsConn, _, err := websocket.Dial(ctx, wsURL.String(), &websocket.DialOptions{
  43. Subprotocols: []string{upgradeHeaderValue},
  44. })
  45. if err != nil {
  46. return nil, err
  47. }
  48. netConn := wsconn.NetConn(context.Background(), wsConn, websocket.MessageBinary, wsURL.String())
  49. cbConn, err := cont(ctx, netConn)
  50. if err != nil {
  51. netConn.Close()
  52. return nil, err
  53. }
  54. return &ClientConn{Conn: cbConn}, nil
  55. }