proxyconnect.go 1.9 KB

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