conf.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package ipn
  4. import (
  5. "net/netip"
  6. "tailscale.com/tailcfg"
  7. "tailscale.com/types/opt"
  8. "tailscale.com/types/preftype"
  9. )
  10. // ConfigVAlpha is the config file format for the "alpha0" version.
  11. type ConfigVAlpha struct {
  12. Version string // "alpha0" for now
  13. Locked opt.Bool `json:",omitempty"` // whether the config is locked from being changed by 'tailscale set'; it defaults to true
  14. ServerURL *string `json:",omitempty"` // defaults to https://controlplane.tailscale.com
  15. AuthKey *string `json:",omitempty"` // as needed if NeedsLogin. either key or path to a file (if prefixed with "file:")
  16. Enabled opt.Bool `json:",omitempty"` // wantRunning; empty string defaults to true
  17. OperatorUser *string `json:",omitempty"` // local user name who is allowed to operate tailscaled without being root or using sudo
  18. Hostname *string `json:",omitempty"`
  19. AcceptDNS opt.Bool `json:"acceptDNS,omitempty"` // --accept-dns
  20. AcceptRoutes opt.Bool `json:"acceptRoutes,omitempty"` // --accept-routes defaults to true
  21. ExitNode *string `json:"exitNode,omitempty"` // IP, StableID, or MagicDNS base name
  22. AllowLANWhileUsingExitNode opt.Bool `json:"allowLANWhileUsingExitNode,omitempty"`
  23. AdvertiseRoutes []netip.Prefix `json:",omitempty"`
  24. DisableSNAT opt.Bool `json:",omitempty"`
  25. AdvertiseServices []string `json:",omitempty"`
  26. AppConnector *AppConnectorPrefs `json:",omitempty"` // advertise app connector; defaults to false (if nil or explicitly set to false)
  27. NetfilterMode *string `json:",omitempty"` // "on", "off", "nodivert"
  28. NoStatefulFiltering opt.Bool `json:",omitempty"`
  29. PostureChecking opt.Bool `json:",omitempty"`
  30. RunSSHServer opt.Bool `json:",omitempty"` // Tailscale SSH
  31. RunWebClient opt.Bool `json:",omitempty"`
  32. ShieldsUp opt.Bool `json:",omitempty"`
  33. AutoUpdate *AutoUpdatePrefs `json:",omitempty"`
  34. ServeConfigTemp *ServeConfig `json:",omitempty"` // TODO(bradfitz,maisem): make separate stable type for this
  35. // StaticEndpoints are additional, user-defined endpoints that this node
  36. // should advertise amongst its wireguard endpoints.
  37. StaticEndpoints []netip.AddrPort `json:",omitempty"`
  38. // TODO(bradfitz,maisem): future something like:
  39. // Profile map[string]*Config // keyed by [email protected], corp.com (TailnetSID)
  40. }
  41. func (c *ConfigVAlpha) ToPrefs() (MaskedPrefs, error) {
  42. var mp MaskedPrefs
  43. if c == nil {
  44. return mp, nil
  45. }
  46. mp.WantRunning = !c.Enabled.EqualBool(false)
  47. mp.WantRunningSet = mp.WantRunning || c.Enabled != ""
  48. if c.ServerURL != nil {
  49. mp.ControlURL = *c.ServerURL
  50. mp.ControlURLSet = true
  51. }
  52. if c.AuthKey != nil && *c.AuthKey != "" {
  53. mp.LoggedOut = false
  54. mp.LoggedOutSet = true
  55. }
  56. if c.OperatorUser != nil {
  57. mp.OperatorUser = *c.OperatorUser
  58. mp.OperatorUserSet = true
  59. }
  60. if c.Hostname != nil {
  61. mp.Hostname = *c.Hostname
  62. mp.HostnameSet = true
  63. }
  64. if c.AcceptDNS != "" {
  65. mp.CorpDNS = c.AcceptDNS.EqualBool(true)
  66. mp.CorpDNSSet = true
  67. }
  68. if c.AcceptRoutes != "" {
  69. mp.RouteAll = c.AcceptRoutes.EqualBool(true)
  70. mp.RouteAllSet = true
  71. }
  72. if c.ExitNode != nil {
  73. ip, err := netip.ParseAddr(*c.ExitNode)
  74. if err == nil {
  75. mp.ExitNodeIP = ip
  76. mp.ExitNodeIPSet = true
  77. } else {
  78. mp.ExitNodeID = tailcfg.StableNodeID(*c.ExitNode)
  79. mp.ExitNodeIDSet = true
  80. }
  81. }
  82. if c.AllowLANWhileUsingExitNode != "" {
  83. mp.ExitNodeAllowLANAccess = c.AllowLANWhileUsingExitNode.EqualBool(true)
  84. mp.ExitNodeAllowLANAccessSet = true
  85. }
  86. if c.AdvertiseRoutes != nil {
  87. mp.AdvertiseRoutes = c.AdvertiseRoutes
  88. mp.AdvertiseRoutesSet = true
  89. }
  90. if c.DisableSNAT != "" {
  91. mp.NoSNAT = c.DisableSNAT.EqualBool(true)
  92. mp.NoSNAT = true
  93. }
  94. if c.NoStatefulFiltering != "" {
  95. mp.NoStatefulFiltering = c.NoStatefulFiltering
  96. mp.NoStatefulFilteringSet = true
  97. }
  98. if c.NetfilterMode != nil {
  99. m, err := preftype.ParseNetfilterMode(*c.NetfilterMode)
  100. if err != nil {
  101. return mp, err
  102. }
  103. mp.NetfilterMode = m
  104. mp.NetfilterModeSet = true
  105. }
  106. if c.PostureChecking != "" {
  107. mp.PostureChecking = c.PostureChecking.EqualBool(true)
  108. mp.PostureCheckingSet = true
  109. }
  110. if c.RunSSHServer != "" {
  111. mp.RunSSH = c.RunSSHServer.EqualBool(true)
  112. mp.RunSSHSet = true
  113. }
  114. if c.RunWebClient != "" {
  115. mp.RunWebClient = c.RunWebClient.EqualBool(true)
  116. mp.RunWebClientSet = true
  117. }
  118. if c.ShieldsUp != "" {
  119. mp.ShieldsUp = c.ShieldsUp.EqualBool(true)
  120. mp.ShieldsUpSet = true
  121. }
  122. if c.AutoUpdate != nil {
  123. mp.AutoUpdate = *c.AutoUpdate
  124. mp.AutoUpdateSet = AutoUpdatePrefsMask{ApplySet: true, CheckSet: true}
  125. }
  126. if c.AppConnector != nil {
  127. mp.AppConnector = *c.AppConnector
  128. mp.AppConnectorSet = true
  129. }
  130. // Configfile should be the source of truth for whether this node
  131. // advertises any services. We need to ensure that each reload updates
  132. // currently advertised services as else the transition from 'some
  133. // services are advertised' to 'advertised services are empty/unset in
  134. // conffile' would have no effect (especially given that an empty
  135. // service slice would be omitted from the JSON config).
  136. mp.AdvertiseServicesSet = true
  137. if c.AdvertiseServices != nil {
  138. mp.AdvertiseServices = c.AdvertiseServices
  139. }
  140. return mp, nil
  141. }