prop.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package version
  4. import (
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "tailscale.com/tailcfg"
  11. "tailscale.com/types/lazy"
  12. )
  13. // IsMobile reports whether this is a mobile client build.
  14. func IsMobile() bool {
  15. return runtime.GOOS == "android" || runtime.GOOS == "ios"
  16. }
  17. // OS returns runtime.GOOS, except instead of returning "darwin" it returns
  18. // "iOS" or "macOS".
  19. func OS() string {
  20. // If you're wondering why we have this function that just returns
  21. // runtime.GOOS written differently: in the old days, Go reported
  22. // GOOS=darwin for both iOS and macOS, so we needed this function to
  23. // differentiate them. Then a later Go release added GOOS=ios as a separate
  24. // platform, but by then the "iOS" and "macOS" values we'd picked, with that
  25. // exact capitalization, were already baked into databases.
  26. if IsAppleTV() {
  27. return "tvOS"
  28. }
  29. if runtime.GOOS == "ios" {
  30. return "iOS"
  31. }
  32. if runtime.GOOS == "darwin" {
  33. return "macOS"
  34. }
  35. return runtime.GOOS
  36. }
  37. var isSandboxedMacOS lazy.SyncValue[bool]
  38. // IsSandboxedMacOS reports whether this process is a sandboxed macOS
  39. // process (either the app or the extension). It is true for the Mac App Store
  40. // and macsys (System Extension) version on macOS, and false for
  41. // tailscaled-on-macOS.
  42. func IsSandboxedMacOS() bool {
  43. if runtime.GOOS != "darwin" {
  44. return false
  45. }
  46. return isSandboxedMacOS.Get(func() bool {
  47. if IsMacSysExt() {
  48. return true
  49. }
  50. exe, err := os.Executable()
  51. if err != nil {
  52. return false
  53. }
  54. return filepath.Base(exe) == "io.tailscale.ipn.macsys.network-extension" || strings.HasSuffix(exe, "/Contents/MacOS/Tailscale") || strings.HasSuffix(exe, "/Contents/MacOS/IPNExtension")
  55. })
  56. }
  57. var isMacSysExt lazy.SyncValue[bool]
  58. // IsMacSysExt whether this binary is from the standalone "System
  59. // Extension" (a.k.a. "macsys") version of Tailscale for macOS.
  60. func IsMacSysExt() bool {
  61. if runtime.GOOS != "darwin" {
  62. return false
  63. }
  64. return isMacSysExt.Get(func() bool {
  65. exe, err := os.Executable()
  66. if err != nil {
  67. return false
  68. }
  69. return filepath.Base(exe) == "io.tailscale.ipn.macsys.network-extension"
  70. })
  71. }
  72. var isAppleTV lazy.SyncValue[bool]
  73. // IsAppleTV reports whether this binary is part of the Tailscale network extension for tvOS.
  74. // Needed because runtime.GOOS returns "ios" otherwise.
  75. func IsAppleTV() bool {
  76. if runtime.GOOS != "ios" {
  77. return false
  78. }
  79. return isAppleTV.Get(func() bool {
  80. return strings.EqualFold(os.Getenv("XPC_SERVICE_NAME"), "io.tailscale.ipn.tvos.network-extension")
  81. })
  82. }
  83. var isWindowsGUI lazy.SyncValue[bool]
  84. // IsWindowsGUI reports whether the current process is the Windows GUI.
  85. func IsWindowsGUI() bool {
  86. if runtime.GOOS != "windows" {
  87. return false
  88. }
  89. return isWindowsGUI.Get(func() bool {
  90. exe, err := os.Executable()
  91. if err != nil {
  92. return false
  93. }
  94. return strings.EqualFold(exe, "tailscale-ipn.exe") || strings.EqualFold(exe, "tailscale-ipn")
  95. })
  96. }
  97. var isUnstableBuild lazy.SyncValue[bool]
  98. // IsUnstableBuild reports whether this is an unstable build.
  99. // That is, whether its minor version number is odd.
  100. func IsUnstableBuild() bool {
  101. return isUnstableBuild.Get(func() bool {
  102. _, rest, ok := strings.Cut(Short(), ".")
  103. if !ok {
  104. return false
  105. }
  106. minorStr, _, ok := strings.Cut(rest, ".")
  107. if !ok {
  108. return false
  109. }
  110. minor, err := strconv.Atoi(minorStr)
  111. if err != nil {
  112. return false
  113. }
  114. return minor%2 == 1
  115. })
  116. }
  117. var isDev = lazy.SyncFunc(func() bool {
  118. return strings.Contains(Short(), "-dev")
  119. })
  120. // Meta is a JSON-serializable type that contains all the version
  121. // information.
  122. type Meta struct {
  123. // MajorMinorPatch is the "major.minor.patch" version string, without
  124. // any hyphenated suffix.
  125. MajorMinorPatch string `json:"majorMinorPatch"`
  126. // IsDev is whether Short contains a -dev suffix. This is whether the build
  127. // is a development build (as opposed to an official stable or unstable
  128. // build stamped in the usual ways). If you just run "go install" or "go
  129. // build" on a dev branch, this will be true.
  130. IsDev bool `json:"isDev,omitempty"`
  131. // Short is MajorMinorPatch but optionally adding "-dev" or "-devYYYYMMDD"
  132. // for dev builds, depending on how it was build.
  133. Short string `json:"short"`
  134. // Long is the full version string, including git commit hash(es) as the
  135. // suffix.
  136. Long string `json:"long"`
  137. // UnstableBranch is whether the build is from an unstable (development)
  138. // branch. That is, it reports whether the minor version is odd.
  139. UnstableBranch bool `json:"unstableBranch,omitempty"`
  140. // GitCommit, if non-empty, is the git commit of the
  141. // github.com/tailscale/tailscale repository at which Tailscale was
  142. // built. Its format is the one returned by `git describe --always
  143. // --exclude "*" --dirty --abbrev=200`.
  144. GitCommit string `json:"gitCommit,omitempty"`
  145. // GitDirty is whether Go stamped the binary as having dirty version
  146. // control changes in the working directory (debug.ReadBuildInfo
  147. // setting "vcs.modified" was true).
  148. GitDirty bool `json:"gitDirty,omitempty"`
  149. // ExtraGitCommit, if non-empty, is the git commit of a "supplemental"
  150. // repository at which Tailscale was built. Its format is the same as
  151. // gitCommit.
  152. //
  153. // ExtraGitCommit is used to track the source revision when the main
  154. // Tailscale repository is integrated into and built from another
  155. // repository (for example, Tailscale's proprietary code, or the
  156. // Android OSS repository). Together, GitCommit and ExtraGitCommit
  157. // exactly describe what repositories and commits were used in a
  158. // build.
  159. ExtraGitCommit string `json:"extraGitCommit,omitempty"`
  160. // DaemonLong is the version number from the tailscaled
  161. // daemon, if requested.
  162. DaemonLong string `json:"daemonLong,omitempty"`
  163. // Cap is the current Tailscale capability version. It's a monotonically
  164. // incrementing integer that's incremented whenever a new capability is
  165. // added.
  166. Cap int `json:"cap"`
  167. }
  168. var getMeta lazy.SyncValue[Meta]
  169. // GetMeta returns version metadata about the current build.
  170. func GetMeta() Meta {
  171. return Meta{
  172. MajorMinorPatch: majorMinorPatch(),
  173. Short: Short(),
  174. Long: Long(),
  175. GitCommit: gitCommit(),
  176. GitDirty: gitDirty(),
  177. ExtraGitCommit: extraGitCommitStamp,
  178. IsDev: isDev(),
  179. UnstableBranch: IsUnstableBuild(),
  180. Cap: int(tailcfg.CurrentCapabilityVersion),
  181. }
  182. }