client_js.go 1.8 KB

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