tshttpproxy_future.go 1.4 KB

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