tshttpproxy.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package tshttpproxy contains Tailscale additions to httpproxy not available
  4. // in golang.org/x/net/http/httpproxy. Notably, it aims to support Windows better.
  5. package tshttpproxy
  6. import (
  7. "context"
  8. "fmt"
  9. "log"
  10. "net"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "runtime"
  15. "strings"
  16. "sync"
  17. "time"
  18. "golang.org/x/net/http/httpproxy"
  19. )
  20. // InvalidateCache invalidates the package-level cache for ProxyFromEnvironment.
  21. //
  22. // It's intended to be called on network link/routing table changes.
  23. func InvalidateCache() {
  24. mu.Lock()
  25. defer mu.Unlock()
  26. noProxyUntil = time.Time{}
  27. }
  28. var (
  29. mu sync.Mutex
  30. noProxyUntil time.Time // if non-zero, time at which ProxyFromEnvironment should check again
  31. config *httpproxy.Config // used to create proxyFunc
  32. proxyFunc func(*url.URL) (*url.URL, error)
  33. )
  34. func getProxyFunc() func(*url.URL) (*url.URL, error) {
  35. // Create config/proxyFunc if it's not created
  36. mu.Lock()
  37. defer mu.Unlock()
  38. if config == nil {
  39. config = httpproxy.FromEnvironment()
  40. }
  41. if proxyFunc == nil {
  42. proxyFunc = config.ProxyFunc()
  43. }
  44. return proxyFunc
  45. }
  46. // setNoProxyUntil stops calls to sysProxyEnv (if any) for the provided duration.
  47. func setNoProxyUntil(d time.Duration) {
  48. mu.Lock()
  49. defer mu.Unlock()
  50. noProxyUntil = time.Now().Add(d)
  51. }
  52. var _ = setNoProxyUntil // quiet staticcheck; Windows uses the above, more might later
  53. // SetSelfProxy configures this package to avoid proxying through any of the
  54. // provided addresses–e.g. if they refer to proxies being run by this process.
  55. func SetSelfProxy(addrs ...string) {
  56. mu.Lock()
  57. defer mu.Unlock()
  58. // Ensure we have a valid config
  59. if config == nil {
  60. config = httpproxy.FromEnvironment()
  61. }
  62. normalizeHostPort := func(s string) string {
  63. host, portStr, err := net.SplitHostPort(s)
  64. if err != nil {
  65. return s
  66. }
  67. // Normalize the localhost IP into "localhost", to avoid IPv4/IPv6 confusion.
  68. if host == "127.0.0.1" || host == "::1" {
  69. return "localhost:" + portStr
  70. }
  71. // On Linux, all 127.0.0.1/8 IPs are also localhost.
  72. if runtime.GOOS == "linux" && strings.HasPrefix(host, "127.0.0.") {
  73. return "localhost:" + portStr
  74. }
  75. return s
  76. }
  77. normHTTP := normalizeHostPort(config.HTTPProxy)
  78. normHTTPS := normalizeHostPort(config.HTTPSProxy)
  79. // If any of our proxy variables point to one of the configured
  80. // addresses, ignore them.
  81. for _, addr := range addrs {
  82. normAddr := normalizeHostPort(addr)
  83. if normHTTP != "" && normHTTP == normAddr {
  84. log.Printf("tshttpproxy: skipping HTTP_PROXY pointing to self: %q", addr)
  85. config.HTTPProxy = ""
  86. normHTTP = ""
  87. }
  88. if normHTTPS != "" && normHTTPS == normAddr {
  89. log.Printf("tshttpproxy: skipping HTTPS_PROXY pointing to self: %q", addr)
  90. config.HTTPSProxy = ""
  91. normHTTPS = ""
  92. }
  93. }
  94. // Invalidate to cause it to get re-created
  95. proxyFunc = nil
  96. }
  97. // sysProxyFromEnv, if non-nil, specifies a platform-specific ProxyFromEnvironment
  98. // func to use if http.ProxyFromEnvironment doesn't return a proxy.
  99. // For example, WPAD PAC files on Windows.
  100. var sysProxyFromEnv func(*http.Request) (*url.URL, error)
  101. // ProxyFromEnvironment is like the standard library's http.ProxyFromEnvironment
  102. // but additionally does OS-specific proxy lookups if the environment variables
  103. // alone don't specify a proxy.
  104. func ProxyFromEnvironment(req *http.Request) (*url.URL, error) {
  105. localProxyFunc := getProxyFunc()
  106. u, err := localProxyFunc(req.URL)
  107. if u != nil && err == nil {
  108. return u, nil
  109. }
  110. mu.Lock()
  111. noProxyTime := noProxyUntil
  112. mu.Unlock()
  113. if time.Now().Before(noProxyTime) {
  114. return nil, nil
  115. }
  116. if sysProxyFromEnv != nil {
  117. u, err := sysProxyFromEnv(req)
  118. if u != nil && err == nil {
  119. return u, nil
  120. }
  121. }
  122. return nil, err
  123. }
  124. var sysAuthHeader func(*url.URL) (string, error)
  125. // GetAuthHeader returns the Authorization header value to send to proxy u.
  126. func GetAuthHeader(u *url.URL) (string, error) {
  127. if fake := os.Getenv("TS_DEBUG_FAKE_PROXY_AUTH"); fake != "" {
  128. return fake, nil
  129. }
  130. if user := u.User.Username(); user != "" {
  131. pass, ok := u.User.Password()
  132. if !ok {
  133. return "", nil
  134. }
  135. req := &http.Request{Header: make(http.Header)}
  136. req.SetBasicAuth(user, pass)
  137. return req.Header.Get("Authorization"), nil
  138. }
  139. if sysAuthHeader != nil {
  140. return sysAuthHeader(u)
  141. }
  142. return "", nil
  143. }
  144. const proxyAuthHeader = "Proxy-Authorization"
  145. // SetTransportGetProxyConnectHeader sets the provided Transport's
  146. // GetProxyConnectHeader field, and adds logging of the received response.
  147. func SetTransportGetProxyConnectHeader(tr *http.Transport) {
  148. tr.GetProxyConnectHeader = func(ctx context.Context, proxyURL *url.URL, target string) (http.Header, error) {
  149. v, err := GetAuthHeader(proxyURL)
  150. if err != nil {
  151. log.Printf("failed to get proxy Auth header for %v; ignoring: %v", proxyURL, err)
  152. return nil, nil
  153. }
  154. if v == "" {
  155. return nil, nil
  156. }
  157. return http.Header{proxyAuthHeader: []string{v}}, nil
  158. }
  159. tr.OnProxyConnectResponse = func(ctx context.Context, proxyURL *url.URL, connectReq *http.Request, res *http.Response) error {
  160. auth := connectReq.Header.Get(proxyAuthHeader)
  161. const truncLen = 20
  162. if len(auth) > truncLen {
  163. auth = fmt.Sprintf("%s...(%d total bytes)", auth[:truncLen], len(auth))
  164. }
  165. log.Printf("tshttpproxy: CONNECT response from %v for target %q (auth %q): %v", proxyURL, connectReq.Host, auth, res.Status)
  166. return nil
  167. }
  168. }