prop.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 runtime.GOOS == "ios" {
  27. return "iOS"
  28. }
  29. if runtime.GOOS == "darwin" {
  30. return "macOS"
  31. }
  32. return runtime.GOOS
  33. }
  34. var isSandboxedMacOS lazy.SyncValue[bool]
  35. // IsSandboxedMacOS reports whether this process is a sandboxed macOS
  36. // process (either the app or the extension). It is true for the Mac App Store
  37. // and macsys (System Extension) version on macOS, and false for
  38. // tailscaled-on-macOS.
  39. func IsSandboxedMacOS() bool {
  40. if runtime.GOOS != "darwin" {
  41. return false
  42. }
  43. return isSandboxedMacOS.Get(func() bool {
  44. if IsMacSysExt() {
  45. return true
  46. }
  47. exe, err := os.Executable()
  48. if err != nil {
  49. return false
  50. }
  51. return filepath.Base(exe) == "io.tailscale.ipn.macsys.network-extension" || strings.HasSuffix(exe, "/Contents/MacOS/Tailscale") || strings.HasSuffix(exe, "/Contents/MacOS/IPNExtension")
  52. })
  53. }
  54. var isMacSysExt lazy.SyncValue[bool]
  55. // IsMacSysExt whether this binary is from the standalone "System
  56. // Extension" (a.k.a. "macsys") version of Tailscale for macOS.
  57. func IsMacSysExt() bool {
  58. if runtime.GOOS != "darwin" {
  59. return false
  60. }
  61. return isMacSysExt.Get(func() bool {
  62. exe, err := os.Executable()
  63. if err != nil {
  64. return false
  65. }
  66. return filepath.Base(exe) == "io.tailscale.ipn.macsys.network-extension"
  67. })
  68. }
  69. var isWindowsGUI lazy.SyncValue[bool]
  70. // IsWindowsGUI reports whether the current process is the Windows GUI.
  71. func IsWindowsGUI() bool {
  72. if runtime.GOOS != "windows" {
  73. return false
  74. }
  75. return isWindowsGUI.Get(func() bool {
  76. exe, err := os.Executable()
  77. if err != nil {
  78. return false
  79. }
  80. return strings.EqualFold(exe, "tailscale-ipn.exe") || strings.EqualFold(exe, "tailscale-ipn")
  81. })
  82. }
  83. var isUnstableBuild lazy.SyncValue[bool]
  84. // IsUnstableBuild reports whether this is an unstable build.
  85. // That is, whether its minor version number is odd.
  86. func IsUnstableBuild() bool {
  87. return isUnstableBuild.Get(func() bool {
  88. _, rest, ok := strings.Cut(Short(), ".")
  89. if !ok {
  90. return false
  91. }
  92. minorStr, _, ok := strings.Cut(rest, ".")
  93. if !ok {
  94. return false
  95. }
  96. minor, err := strconv.Atoi(minorStr)
  97. if err != nil {
  98. return false
  99. }
  100. return minor%2 == 1
  101. })
  102. }
  103. var isDev = lazy.SyncFunc(func() bool {
  104. return strings.Contains(Short(), "-dev")
  105. })
  106. // Meta is a JSON-serializable type that contains all the version
  107. // information.
  108. type Meta struct {
  109. // MajorMinorPatch is the "major.minor.patch" version string, without
  110. // any hyphenated suffix.
  111. MajorMinorPatch string `json:"majorMinorPatch"`
  112. // IsDev is whether Short contains a -dev suffix. This is whether the build
  113. // is a development build (as opposed to an official stable or unstable
  114. // build stamped in the usual ways). If you just run "go install" or "go
  115. // build" on a dev branch, this will be true.
  116. IsDev bool `json:"isDev,omitempty"`
  117. // Short is MajorMinorPatch but optionally adding "-dev" or "-devYYYYMMDD"
  118. // for dev builds, depending on how it was build.
  119. Short string `json:"short"`
  120. // Long is the full version string, including git commit hash(es) as the
  121. // suffix.
  122. Long string `json:"long"`
  123. // UnstableBranch is whether the build is from an unstable (development)
  124. // branch. That is, it reports whether the minor version is odd.
  125. UnstableBranch bool `json:"unstableBranch,omitempty"`
  126. // GitCommit, if non-empty, is the git commit of the
  127. // github.com/tailscale/tailscale repository at which Tailscale was
  128. // built. Its format is the one returned by `git describe --always
  129. // --exclude "*" --dirty --abbrev=200`.
  130. GitCommit string `json:"gitCommit,omitempty"`
  131. // GitDirty is whether Go stamped the binary as having dirty version
  132. // control changes in the working directory (debug.ReadBuildInfo
  133. // setting "vcs.modified" was true).
  134. GitDirty bool `json:"gitDirty,omitempty"`
  135. // ExtraGitCommit, if non-empty, is the git commit of a "supplemental"
  136. // repository at which Tailscale was built. Its format is the same as
  137. // gitCommit.
  138. //
  139. // ExtraGitCommit is used to track the source revision when the main
  140. // Tailscale repository is integrated into and built from another
  141. // repository (for example, Tailscale's proprietary code, or the
  142. // Android OSS repository). Together, GitCommit and ExtraGitCommit
  143. // exactly describe what repositories and commits were used in a
  144. // build.
  145. ExtraGitCommit string `json:"extraGitCommit,omitempty"`
  146. // DaemonLong is the version number from the tailscaled
  147. // daemon, if requested.
  148. DaemonLong string `json:"daemonLong,omitempty"`
  149. // Cap is the current Tailscale capability version. It's a monotonically
  150. // incrementing integer that's incremented whenever a new capability is
  151. // added.
  152. Cap int `json:"cap"`
  153. }
  154. var getMeta lazy.SyncValue[Meta]
  155. // GetMeta returns version metadata about the current build.
  156. func GetMeta() Meta {
  157. return Meta{
  158. MajorMinorPatch: majorMinorPatch(),
  159. Short: Short(),
  160. Long: Long(),
  161. GitCommit: gitCommit(),
  162. GitDirty: gitDirty(),
  163. ExtraGitCommit: extraGitCommitStamp,
  164. IsDev: isDev(),
  165. UnstableBranch: IsUnstableBuild(),
  166. Cap: int(tailcfg.CurrentCapabilityVersion),
  167. }
  168. }