backend.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package ipn
  4. import (
  5. "fmt"
  6. "strings"
  7. "time"
  8. "tailscale.com/ipn/ipnstate"
  9. "tailscale.com/tailcfg"
  10. "tailscale.com/types/empty"
  11. "tailscale.com/types/key"
  12. "tailscale.com/types/netmap"
  13. "tailscale.com/types/structs"
  14. )
  15. type State int
  16. const (
  17. NoState State = 0
  18. InUseOtherUser State = 1
  19. NeedsLogin State = 2
  20. NeedsMachineAuth State = 3
  21. Stopped State = 4
  22. Starting State = 5
  23. Running State = 6
  24. )
  25. // GoogleIDToken Type is the tailcfg.Oauth2Token.TokenType for the Google
  26. // ID tokens used by the Android client.
  27. const GoogleIDTokenType = "ts_android_google_login"
  28. func (s State) String() string {
  29. return [...]string{
  30. "NoState",
  31. "InUseOtherUser",
  32. "NeedsLogin",
  33. "NeedsMachineAuth",
  34. "Stopped",
  35. "Starting",
  36. "Running"}[s]
  37. }
  38. // EngineStatus contains WireGuard engine stats.
  39. type EngineStatus struct {
  40. RBytes, WBytes int64
  41. NumLive int
  42. LiveDERPs int // number of active DERP connections
  43. LivePeers map[key.NodePublic]ipnstate.PeerStatusLite
  44. }
  45. // NotifyWatchOpt is a bitmask of options about what type of Notify messages
  46. // to subscribe to.
  47. type NotifyWatchOpt uint64
  48. const (
  49. // NotifyWatchEngineUpdates, if set, causes Engine updates to be sent to the
  50. // client either regularly or when they change, without having to ask for
  51. // each one via RequestEngineStatus.
  52. NotifyWatchEngineUpdates NotifyWatchOpt = 1 << iota
  53. NotifyInitialState // if set, the first Notify message (sent immediately) will contain the current State + BrowseToURL
  54. NotifyInitialPrefs // if set, the first Notify message (sent immediately) will contain the current Prefs
  55. NotifyInitialNetMap // if set, the first Notify message (sent immediately) will contain the current NetMap
  56. NotifyNoPrivateKeys // if set, private keys that would normally be sent in updates are zeroed out
  57. )
  58. // Notify is a communication from a backend (e.g. tailscaled) to a frontend
  59. // (cmd/tailscale, iOS, macOS, Win Tasktray).
  60. // In any given notification, any or all of these may be nil, meaning
  61. // that they have not changed.
  62. // They are JSON-encoded on the wire, despite the lack of struct tags.
  63. type Notify struct {
  64. _ structs.Incomparable
  65. Version string // version number of IPN backend
  66. // ErrMessage, if non-nil, contains a critical error message.
  67. // For State InUseOtherUser, ErrMessage is not critical and just contains the details.
  68. ErrMessage *string
  69. LoginFinished *empty.Message // non-nil when/if the login process succeeded
  70. State *State // if non-nil, the new or current IPN state
  71. Prefs *PrefsView // if non-nil && Valid, the new or current preferences
  72. NetMap *netmap.NetworkMap // if non-nil, the new or current netmap
  73. Engine *EngineStatus // if non-nil, the new or current wireguard stats
  74. BrowseToURL *string // if non-nil, UI should open a browser right now
  75. BackendLogID *string // if non-nil, the public logtail ID used by backend
  76. // FilesWaiting if non-nil means that files are buffered in
  77. // the Tailscale daemon and ready for local transfer to the
  78. // user's preferred storage location.
  79. //
  80. // Deprecated: use LocalClient.AwaitWaitingFiles instead.
  81. FilesWaiting *empty.Message `json:",omitempty"`
  82. // IncomingFiles, if non-nil, specifies which files are in the
  83. // process of being received. A nil IncomingFiles means this
  84. // Notify should not update the state of file transfers. A non-nil
  85. // but empty IncomingFiles means that no files are in the middle
  86. // of being transferred.
  87. //
  88. // Deprecated: use LocalClient.AwaitWaitingFiles instead.
  89. IncomingFiles []PartialFile `json:",omitempty"`
  90. // LocalTCPPort, if non-nil, informs the UI frontend which
  91. // (non-zero) localhost TCP port it's listening on.
  92. // This is currently only used by Tailscale when run in the
  93. // macOS Network Extension.
  94. LocalTCPPort *uint16 `json:",omitempty"`
  95. // ClientVersion, if non-nil, describes whether a client version update
  96. // is available.
  97. ClientVersion *tailcfg.ClientVersion `json:",omitempty"`
  98. // type is mirrored in xcode/Shared/IPN.swift
  99. }
  100. func (n Notify) String() string {
  101. var sb strings.Builder
  102. sb.WriteString("Notify{")
  103. if n.ErrMessage != nil {
  104. fmt.Fprintf(&sb, "err=%q ", *n.ErrMessage)
  105. }
  106. if n.LoginFinished != nil {
  107. sb.WriteString("LoginFinished ")
  108. }
  109. if n.State != nil {
  110. fmt.Fprintf(&sb, "state=%v ", *n.State)
  111. }
  112. if n.Prefs != nil && n.Prefs.Valid() {
  113. fmt.Fprintf(&sb, "%v ", n.Prefs.Pretty())
  114. }
  115. if n.NetMap != nil {
  116. sb.WriteString("NetMap{...} ")
  117. }
  118. if n.Engine != nil {
  119. fmt.Fprintf(&sb, "wg=%v ", *n.Engine)
  120. }
  121. if n.BrowseToURL != nil {
  122. sb.WriteString("URL=<...> ")
  123. }
  124. if n.BackendLogID != nil {
  125. sb.WriteString("BackendLogID ")
  126. }
  127. if n.FilesWaiting != nil {
  128. sb.WriteString("FilesWaiting ")
  129. }
  130. if len(n.IncomingFiles) != 0 {
  131. sb.WriteString("IncomingFiles ")
  132. }
  133. if n.LocalTCPPort != nil {
  134. fmt.Fprintf(&sb, "tcpport=%v ", n.LocalTCPPort)
  135. }
  136. s := sb.String()
  137. return s[0:len(s)-1] + "}"
  138. }
  139. // PartialFile represents an in-progress file transfer.
  140. type PartialFile struct {
  141. Name string // e.g. "foo.jpg"
  142. Started time.Time // time transfer started
  143. DeclaredSize int64 // or -1 if unknown
  144. Received int64 // bytes copied thus far
  145. // PartialPath is set non-empty in "direct" file mode to the
  146. // in-progress '*.partial' file's path when the peerapi isn't
  147. // being used; see LocalBackend.SetDirectFileRoot.
  148. PartialPath string `json:",omitempty"`
  149. // Done is set in "direct" mode when the partial file has been
  150. // closed and is ready for the caller to rename away the
  151. // ".partial" suffix.
  152. Done bool `json:",omitempty"`
  153. }
  154. // StateKey is an opaque identifier for a set of LocalBackend state
  155. // (preferences, private keys, etc.). It is also used as a key for
  156. // the various LoginProfiles that the instance may be signed into.
  157. //
  158. // Additionally, the StateKey can be debug setting name:
  159. //
  160. // - "_debug_magicsock_until" with value being a unix timestamp stringified
  161. // - "_debug_<component>_until" with value being a unix timestamp stringified
  162. type StateKey string
  163. type Options struct {
  164. // FrontendLogID is the public logtail id used by the frontend.
  165. FrontendLogID string
  166. // LegacyMigrationPrefs are used to migrate preferences from the
  167. // frontend to the backend.
  168. // If non-nil, they are imported as a new profile.
  169. LegacyMigrationPrefs *Prefs `json:"Prefs"`
  170. // UpdatePrefs, if provided, overrides Options.LegacyMigrationPrefs
  171. // *and* the Prefs already stored in the backend state, *except* for
  172. // the Persist member. If you just want to provide prefs, this is
  173. // probably what you want.
  174. //
  175. // TODO(apenwarr): Rename this to Prefs, and possibly move Prefs.Persist
  176. // elsewhere entirely (as it always should have been). Or, move the
  177. // fancy state migration stuff out of Start().
  178. UpdatePrefs *Prefs
  179. // AuthKey is an optional node auth key used to authorize a
  180. // new node key without user interaction.
  181. AuthKey string
  182. }