backend.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 + SessionID
  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. // SessionID identifies the unique WatchIPNBus session.
  67. // This field is only set in the first message when requesting
  68. // NotifyInitialState. Clients must store it on their side as
  69. // following notifications will not include this field.
  70. SessionID string `json:",omitempty"`
  71. // ErrMessage, if non-nil, contains a critical error message.
  72. // For State InUseOtherUser, ErrMessage is not critical and just contains the details.
  73. ErrMessage *string
  74. LoginFinished *empty.Message // non-nil when/if the login process succeeded
  75. State *State // if non-nil, the new or current IPN state
  76. Prefs *PrefsView // if non-nil && Valid, the new or current preferences
  77. NetMap *netmap.NetworkMap // if non-nil, the new or current netmap
  78. Engine *EngineStatus // if non-nil, the new or current wireguard stats
  79. BrowseToURL *string // if non-nil, UI should open a browser right now
  80. BackendLogID *string // if non-nil, the public logtail ID used by backend
  81. // FilesWaiting if non-nil means that files are buffered in
  82. // the Tailscale daemon and ready for local transfer to the
  83. // user's preferred storage location.
  84. //
  85. // Deprecated: use LocalClient.AwaitWaitingFiles instead.
  86. FilesWaiting *empty.Message `json:",omitempty"`
  87. // IncomingFiles, if non-nil, specifies which files are in the
  88. // process of being received. A nil IncomingFiles means this
  89. // Notify should not update the state of file transfers. A non-nil
  90. // but empty IncomingFiles means that no files are in the middle
  91. // of being transferred.
  92. //
  93. // Deprecated: use LocalClient.AwaitWaitingFiles instead.
  94. IncomingFiles []PartialFile `json:",omitempty"`
  95. // LocalTCPPort, if non-nil, informs the UI frontend which
  96. // (non-zero) localhost TCP port it's listening on.
  97. // This is currently only used by Tailscale when run in the
  98. // macOS Network Extension.
  99. LocalTCPPort *uint16 `json:",omitempty"`
  100. // ClientVersion, if non-nil, describes whether a client version update
  101. // is available.
  102. ClientVersion *tailcfg.ClientVersion `json:",omitempty"`
  103. // type is mirrored in xcode/Shared/IPN.swift
  104. }
  105. func (n Notify) String() string {
  106. var sb strings.Builder
  107. sb.WriteString("Notify{")
  108. if n.ErrMessage != nil {
  109. fmt.Fprintf(&sb, "err=%q ", *n.ErrMessage)
  110. }
  111. if n.LoginFinished != nil {
  112. sb.WriteString("LoginFinished ")
  113. }
  114. if n.State != nil {
  115. fmt.Fprintf(&sb, "state=%v ", *n.State)
  116. }
  117. if n.Prefs != nil && n.Prefs.Valid() {
  118. fmt.Fprintf(&sb, "%v ", n.Prefs.Pretty())
  119. }
  120. if n.NetMap != nil {
  121. sb.WriteString("NetMap{...} ")
  122. }
  123. if n.Engine != nil {
  124. fmt.Fprintf(&sb, "wg=%v ", *n.Engine)
  125. }
  126. if n.BrowseToURL != nil {
  127. sb.WriteString("URL=<...> ")
  128. }
  129. if n.BackendLogID != nil {
  130. sb.WriteString("BackendLogID ")
  131. }
  132. if n.FilesWaiting != nil {
  133. sb.WriteString("FilesWaiting ")
  134. }
  135. if len(n.IncomingFiles) != 0 {
  136. sb.WriteString("IncomingFiles ")
  137. }
  138. if n.LocalTCPPort != nil {
  139. fmt.Fprintf(&sb, "tcpport=%v ", n.LocalTCPPort)
  140. }
  141. s := sb.String()
  142. return s[0:len(s)-1] + "}"
  143. }
  144. // PartialFile represents an in-progress file transfer.
  145. type PartialFile struct {
  146. Name string // e.g. "foo.jpg"
  147. Started time.Time // time transfer started
  148. DeclaredSize int64 // or -1 if unknown
  149. Received int64 // bytes copied thus far
  150. // PartialPath is set non-empty in "direct" file mode to the
  151. // in-progress '*.partial' file's path when the peerapi isn't
  152. // being used; see LocalBackend.SetDirectFileRoot.
  153. PartialPath string `json:",omitempty"`
  154. // Done is set in "direct" mode when the partial file has been
  155. // closed and is ready for the caller to rename away the
  156. // ".partial" suffix.
  157. Done bool `json:",omitempty"`
  158. }
  159. // StateKey is an opaque identifier for a set of LocalBackend state
  160. // (preferences, private keys, etc.). It is also used as a key for
  161. // the various LoginProfiles that the instance may be signed into.
  162. //
  163. // Additionally, the StateKey can be debug setting name:
  164. //
  165. // - "_debug_magicsock_until" with value being a unix timestamp stringified
  166. // - "_debug_<component>_until" with value being a unix timestamp stringified
  167. type StateKey string
  168. // DebuggableComponents is a list of components whose debugging can be turned on
  169. // and off individually using the tailscale debug command.
  170. var DebuggableComponents = []string{
  171. "magicsock",
  172. "sockstats",
  173. }
  174. type Options struct {
  175. // FrontendLogID is the public logtail id used by the frontend.
  176. FrontendLogID string
  177. // LegacyMigrationPrefs are used to migrate preferences from the
  178. // frontend to the backend.
  179. // If non-nil, they are imported as a new profile.
  180. LegacyMigrationPrefs *Prefs `json:"Prefs"`
  181. // UpdatePrefs, if provided, overrides Options.LegacyMigrationPrefs
  182. // *and* the Prefs already stored in the backend state, *except* for
  183. // the Persist member. If you just want to provide prefs, this is
  184. // probably what you want.
  185. //
  186. // TODO(apenwarr): Rename this to Prefs, and possibly move Prefs.Persist
  187. // elsewhere entirely (as it always should have been). Or, move the
  188. // fancy state migration stuff out of Start().
  189. UpdatePrefs *Prefs
  190. // AuthKey is an optional node auth key used to authorize a
  191. // new node key without user interaction.
  192. AuthKey string
  193. }