proxyconnect.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) Tailscale Inc & contributors
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !js
  4. package ipnserver
  5. import (
  6. "io"
  7. "net"
  8. "net/http"
  9. "tailscale.com/feature"
  10. "tailscale.com/feature/buildfeatures"
  11. "tailscale.com/logpolicy"
  12. )
  13. // handleProxyConnectConn handles a CONNECT request to
  14. // log.tailscale.com (or whatever the configured log server is). This
  15. // is intended for use by the Windows GUI client to log via when an
  16. // exit node is in use, so the logs don't go out via the exit node and
  17. // instead go directly, like tailscaled's. The dialer tried to do that
  18. // in the unprivileged GUI by binding to a specific interface, but the
  19. // "Internet Kill Switch" installed by tailscaled for exit nodes
  20. // precludes that from working and instead the GUI fails to dial out.
  21. // So, go through tailscaled (with a CONNECT request) instead.
  22. func (s *Server) handleProxyConnectConn(w http.ResponseWriter, r *http.Request) {
  23. if !buildfeatures.HasOutboundProxy {
  24. http.Error(w, feature.ErrUnavailable.Error(), http.StatusNotImplemented)
  25. return
  26. }
  27. ctx := r.Context()
  28. if r.Method != "CONNECT" {
  29. panic("[unexpected] miswired")
  30. }
  31. hostPort := r.RequestURI
  32. logHost := logpolicy.LogHost()
  33. allowed := net.JoinHostPort(logHost, "443")
  34. if hostPort != allowed {
  35. s.logf("invalid CONNECT target %q; want %q", hostPort, allowed)
  36. http.Error(w, "Bad CONNECT target.", http.StatusForbidden)
  37. return
  38. }
  39. dialContext := logpolicy.MakeDialFunc(s.netMon, s.logf)
  40. back, err := dialContext(ctx, "tcp", hostPort)
  41. if err != nil {
  42. s.logf("error CONNECT dialing %v: %v", hostPort, err)
  43. http.Error(w, "Connect failure", http.StatusBadGateway)
  44. return
  45. }
  46. defer back.Close()
  47. hj, ok := w.(http.Hijacker)
  48. if !ok {
  49. http.Error(w, "CONNECT hijack unavailable", http.StatusInternalServerError)
  50. return
  51. }
  52. c, br, err := hj.Hijack()
  53. if err != nil {
  54. s.logf("CONNECT hijack: %v", err)
  55. return
  56. }
  57. defer c.Close()
  58. io.WriteString(c, "HTTP/1.1 200 OK\r\n\r\n")
  59. errc := make(chan error, 2)
  60. go func() {
  61. _, err := io.Copy(c, back)
  62. errc <- err
  63. }()
  64. go func() {
  65. _, err := io.Copy(back, br)
  66. errc <- err
  67. }()
  68. <-errc
  69. }