session.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package desktop
  4. import (
  5. "fmt"
  6. "tailscale.com/ipn/ipnauth"
  7. )
  8. // SessionID is a unique identifier of a desktop session.
  9. type SessionID uint
  10. // SessionStatus is the status of a desktop session.
  11. type SessionStatus int
  12. const (
  13. // ClosedSession is a session that does not exist, is not yet initialized by the OS,
  14. // or has been terminated.
  15. ClosedSession SessionStatus = iota
  16. // ForegroundSession is a session that a user can interact with,
  17. // such as when attached to a physical console or an active,
  18. // unlocked RDP connection.
  19. ForegroundSession
  20. // BackgroundSession indicates that the session is locked, disconnected,
  21. // or otherwise running without user presence or interaction.
  22. BackgroundSession
  23. )
  24. // String implements [fmt.Stringer].
  25. func (s SessionStatus) String() string {
  26. switch s {
  27. case ClosedSession:
  28. return "Closed"
  29. case ForegroundSession:
  30. return "Foreground"
  31. case BackgroundSession:
  32. return "Background"
  33. default:
  34. panic("unreachable")
  35. }
  36. }
  37. // Session is a state of a desktop session at a given point in time.
  38. type Session struct {
  39. ID SessionID // Identifier of the session; can be reused after the session is closed.
  40. Status SessionStatus // The status of the session, such as foreground or background.
  41. User ipnauth.Actor // User logged into the session.
  42. }
  43. // Description returns a human-readable description of the session.
  44. func (s *Session) Description() string {
  45. if maybeUsername, _ := s.User.Username(); maybeUsername != "" { // best effort
  46. return fmt.Sprintf("Session %d - %q (%s)", s.ID, maybeUsername, s.Status)
  47. }
  48. return fmt.Sprintf("Session %d (%s)", s.ID, s.Status)
  49. }