tshttpproxy_future.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) 2020 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. // +build tailscale_go
  5. // We want to use https://github.com/golang/go/issues/41048 but it's only in the
  6. // Tailscale Go tree for now. Hence the build tag above.
  7. package tshttpproxy
  8. import (
  9. "context"
  10. "fmt"
  11. "log"
  12. "net/http"
  13. "net/url"
  14. )
  15. const proxyAuthHeader = "Proxy-Authorization"
  16. func init() {
  17. condSetTransportGetProxyConnectHeader = func(tr *http.Transport) {
  18. tr.GetProxyConnectHeader = func(ctx context.Context, proxyURL *url.URL, target string) (http.Header, error) {
  19. v, err := GetAuthHeader(proxyURL)
  20. if err != nil {
  21. log.Printf("failed to get proxy Auth header for %v; ignoring: %v", proxyURL, err)
  22. return nil, nil
  23. }
  24. if v == "" {
  25. return nil, nil
  26. }
  27. return http.Header{proxyAuthHeader: []string{v}}, nil
  28. }
  29. tr.OnProxyConnectResponse = func(ctx context.Context, proxyURL *url.URL, connectReq *http.Request, res *http.Response) error {
  30. auth := connectReq.Header.Get(proxyAuthHeader)
  31. const truncLen = 20
  32. if len(auth) > truncLen {
  33. auth = fmt.Sprintf("%s...(%d total bytes)", auth[:truncLen], len(auth))
  34. }
  35. log.Printf("tshttpproxy: CONNECT response from %v for target %q (auth %q): %v", proxyURL, connectReq.Host, auth, res.Status)
  36. return nil
  37. }
  38. }
  39. }