conf.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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"`
  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. NetfilterMode *string `json:",omitempty"` // "on", "off", "nodivert"
  26. PostureChecking opt.Bool `json:",omitempty"`
  27. RunSSHServer opt.Bool `json:",omitempty"` // Tailscale SSH
  28. RunWebClient opt.Bool `json:",omitempty"`
  29. ShieldsUp opt.Bool `json:",omitempty"`
  30. AutoUpdate *AutoUpdatePrefs `json:",omitempty"`
  31. ServeConfigTemp *ServeConfig `json:",omitempty"` // TODO(bradfitz,maisem): make separate stable type for this
  32. // TODO(bradfitz,maisem): future something like:
  33. // Profile map[string]*Config // keyed by [email protected], corp.com (TailnetSID)
  34. }
  35. func (c *ConfigVAlpha) ToPrefs() (MaskedPrefs, error) {
  36. var mp MaskedPrefs
  37. if c == nil {
  38. return mp, nil
  39. }
  40. mp.WantRunning = !c.Enabled.EqualBool(false)
  41. mp.WantRunningSet = mp.WantRunning || c.Enabled != ""
  42. if c.ServerURL != nil {
  43. mp.ControlURL = *c.ServerURL
  44. mp.ControlURLSet = true
  45. }
  46. if c.AuthKey != nil && *c.AuthKey != "" {
  47. mp.LoggedOut = false
  48. mp.LoggedOutSet = true
  49. }
  50. if c.OperatorUser != nil {
  51. mp.OperatorUser = *c.OperatorUser
  52. mp.OperatorUserSet = true
  53. }
  54. if c.Hostname != nil {
  55. mp.Hostname = *c.Hostname
  56. mp.HostnameSet = true
  57. }
  58. if c.AcceptDNS != "" {
  59. mp.CorpDNS = c.AcceptDNS.EqualBool(true)
  60. mp.CorpDNSSet = true
  61. }
  62. if c.AcceptRoutes != "" {
  63. mp.RouteAll = c.AcceptRoutes.EqualBool(true)
  64. mp.RouteAllSet = true
  65. }
  66. if c.ExitNode != nil {
  67. ip, err := netip.ParseAddr(*c.ExitNode)
  68. if err == nil {
  69. mp.ExitNodeIP = ip
  70. mp.ExitNodeIPSet = true
  71. } else {
  72. mp.ExitNodeID = tailcfg.StableNodeID(*c.ExitNode)
  73. mp.ExitNodeIDSet = true
  74. }
  75. }
  76. if c.AllowLANWhileUsingExitNode != "" {
  77. mp.ExitNodeAllowLANAccess = c.AllowLANWhileUsingExitNode.EqualBool(true)
  78. mp.ExitNodeAllowLANAccessSet = true
  79. }
  80. if c.AdvertiseRoutes != nil {
  81. mp.AdvertiseRoutes = c.AdvertiseRoutes
  82. mp.AdvertiseRoutesSet = true
  83. }
  84. if c.DisableSNAT != "" {
  85. mp.NoSNAT = c.DisableSNAT.EqualBool(true)
  86. mp.NoSNAT = true
  87. }
  88. if c.NetfilterMode != nil {
  89. m, err := preftype.ParseNetfilterMode(*c.NetfilterMode)
  90. if err != nil {
  91. return mp, err
  92. }
  93. mp.NetfilterMode = m
  94. mp.NetfilterModeSet = true
  95. }
  96. if c.PostureChecking != "" {
  97. mp.PostureChecking = c.PostureChecking.EqualBool(true)
  98. mp.PostureCheckingSet = true
  99. }
  100. if c.RunSSHServer != "" {
  101. mp.RunSSH = c.RunSSHServer.EqualBool(true)
  102. mp.RunSSHSet = true
  103. }
  104. if c.RunWebClient != "" {
  105. mp.RunWebClient = c.RunWebClient.EqualBool(true)
  106. mp.RunWebClientSet = true
  107. }
  108. if c.ShieldsUp != "" {
  109. mp.ShieldsUp = c.ShieldsUp.EqualBool(true)
  110. mp.ShieldsUpSet = true
  111. }
  112. if c.AutoUpdate != nil {
  113. mp.AutoUpdate = *c.AutoUpdate
  114. mp.AutoUpdateSet = AutoUpdatePrefsMask{ApplySet: true, CheckSet: true}
  115. }
  116. return mp, nil
  117. }