server.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package controlhttp
  5. import (
  6. "bufio"
  7. "context"
  8. "encoding/base64"
  9. "errors"
  10. "fmt"
  11. "net"
  12. "net/http"
  13. "tailscale.com/control/controlbase"
  14. "tailscale.com/types/key"
  15. )
  16. // AcceptHTTP upgrades the HTTP request given by w and r into a
  17. // Tailscale control protocol base transport connection.
  18. //
  19. // AcceptHTTP always writes an HTTP response to w. The caller must not
  20. // attempt their own response after calling AcceptHTTP.
  21. func AcceptHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, private key.MachinePrivate) (*controlbase.Conn, error) {
  22. next := r.Header.Get("Upgrade")
  23. if next == "" {
  24. http.Error(w, "missing next protocol", http.StatusBadRequest)
  25. return nil, errors.New("no next protocol in HTTP request")
  26. }
  27. if next != upgradeHeaderValue {
  28. http.Error(w, "unknown next protocol", http.StatusBadRequest)
  29. return nil, fmt.Errorf("client requested unhandled next protocol %q", next)
  30. }
  31. initB64 := r.Header.Get(handshakeHeaderName)
  32. if initB64 == "" {
  33. http.Error(w, "missing Tailscale handshake header", http.StatusBadRequest)
  34. return nil, errors.New("no tailscale handshake header in HTTP request")
  35. }
  36. init, err := base64.StdEncoding.DecodeString(initB64)
  37. if err != nil {
  38. http.Error(w, "invalid tailscale handshake header", http.StatusBadRequest)
  39. return nil, fmt.Errorf("decoding base64 handshake header: %v", err)
  40. }
  41. hijacker, ok := w.(http.Hijacker)
  42. if !ok {
  43. http.Error(w, "make request over HTTP/1", http.StatusBadRequest)
  44. return nil, errors.New("can't hijack client connection")
  45. }
  46. w.Header().Set("Upgrade", upgradeHeaderValue)
  47. w.Header().Set("Connection", "upgrade")
  48. w.WriteHeader(http.StatusSwitchingProtocols)
  49. conn, brw, err := hijacker.Hijack()
  50. if err != nil {
  51. return nil, fmt.Errorf("hijacking client connection: %w", err)
  52. }
  53. if err := brw.Flush(); err != nil {
  54. conn.Close()
  55. return nil, fmt.Errorf("flushing hijacked HTTP buffer: %w", err)
  56. }
  57. if brw.Reader.Buffered() > 0 {
  58. conn = &drainBufConn{conn, brw.Reader}
  59. }
  60. nc, err := controlbase.Server(ctx, conn, private, init)
  61. if err != nil {
  62. conn.Close()
  63. return nil, fmt.Errorf("noise handshake failed: %w", err)
  64. }
  65. return nc, nil
  66. }
  67. // drainBufConn is a net.Conn with an initial bunch of bytes in a
  68. // bufio.Reader. Read drains the bufio.Reader until empty, then passes
  69. // through subsequent reads to the Conn directly.
  70. type drainBufConn struct {
  71. net.Conn
  72. r *bufio.Reader
  73. }
  74. func (b *drainBufConn) Read(bs []byte) (int, error) {
  75. if b.r == nil {
  76. return b.Conn.Read(bs)
  77. }
  78. n, err := b.r.Read(bs)
  79. if b.r.Buffered() == 0 {
  80. b.r = nil
  81. }
  82. return n, err
  83. }