hooks.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package feature
  4. import (
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "sync"
  9. "tailscale.com/types/logger"
  10. "tailscale.com/types/persist"
  11. )
  12. // HookCanAutoUpdate is a hook for the clientupdate package
  13. // to conditionally initialize.
  14. var HookCanAutoUpdate Hook[func() bool]
  15. var testAllowAutoUpdate = sync.OnceValue(func() bool {
  16. return os.Getenv("TS_TEST_ALLOW_AUTO_UPDATE") == "1"
  17. })
  18. // CanAutoUpdate reports whether the current binary is built with auto-update
  19. // support and, if so, whether the current platform supports it.
  20. func CanAutoUpdate() bool {
  21. if testAllowAutoUpdate() {
  22. return true
  23. }
  24. if f, ok := HookCanAutoUpdate.GetOk(); ok {
  25. return f()
  26. }
  27. return false
  28. }
  29. // HookProxyFromEnvironment is a hook for feature/useproxy to register
  30. // a function to use as http.ProxyFromEnvironment.
  31. var HookProxyFromEnvironment Hook[func(*http.Request) (*url.URL, error)]
  32. // HookProxyInvalidateCache is a hook for feature/useproxy to register
  33. // [tshttpproxy.InvalidateCache].
  34. var HookProxyInvalidateCache Hook[func()]
  35. // HookProxyGetAuthHeader is a hook for feature/useproxy to register
  36. // [tshttpproxy.GetAuthHeader].
  37. var HookProxyGetAuthHeader Hook[func(*url.URL) (string, error)]
  38. // HookProxySetSelfProxy is a hook for feature/useproxy to register
  39. // [tshttpproxy.SetSelfProxy].
  40. var HookProxySetSelfProxy Hook[func(...string)]
  41. // HookProxySetTransportGetProxyConnectHeader is a hook for feature/useproxy to register
  42. // [tshttpproxy.SetTransportGetProxyConnectHeader].
  43. var HookProxySetTransportGetProxyConnectHeader Hook[func(*http.Transport)]
  44. // HookTPMAvailable is a hook that reports whether a TPM device is supported
  45. // and available.
  46. var HookTPMAvailable Hook[func() bool]
  47. var HookGenerateAttestationKeyIfEmpty Hook[func(p *persist.Persist, logf logger.Logf) (bool, error)]
  48. // TPMAvailable reports whether a TPM device is supported and available.
  49. func TPMAvailable() bool {
  50. if f, ok := HookTPMAvailable.GetOk(); ok {
  51. return f()
  52. }
  53. return false
  54. }
  55. // HookHardwareAttestationAvailable is a hook that reports whether hardware
  56. // attestation is supported and available.
  57. var HookHardwareAttestationAvailable Hook[func() bool]
  58. // HardwareAttestationAvailable reports whether hardware attestation is
  59. // supported and available (TPM on Windows/Linux, Secure Enclave on macOS|iOS,
  60. // KeyStore on Android)
  61. func HardwareAttestationAvailable() bool {
  62. if f, ok := HookHardwareAttestationAvailable.GetOk(); ok {
  63. return f()
  64. }
  65. return false
  66. }