localconfig.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package zerotier
  14. import (
  15. "encoding/json"
  16. "io/ioutil"
  17. rand "math/rand"
  18. "os"
  19. "runtime"
  20. )
  21. // LocalConfigPhysicalPathConfiguration contains settings for physical paths
  22. type LocalConfigPhysicalPathConfiguration struct {
  23. // Blacklist flags this path as unusable for ZeroTier traffic
  24. Blacklist bool
  25. // TrustedPathID identifies a path for unencrypted/unauthenticated traffic
  26. TrustedPathID uint64
  27. }
  28. // LocalConfigVirtualAddressConfiguration contains settings for virtual addresses
  29. type LocalConfigVirtualAddressConfiguration struct {
  30. // Try is a list of IPs/ports to try for this peer in addition to anything learned from roots or direct path push
  31. Try []InetAddress `json:",omitempty"`
  32. }
  33. // ExternalAddress is an externally visible address
  34. type ExternalAddress struct {
  35. InetAddress
  36. // Permanent indicates that this address should be incorporated into this node's Locator
  37. Permanent bool `json:"permanent"`
  38. }
  39. // LocalConfigSettings contains node settings
  40. type LocalConfigSettings struct {
  41. // PrimaryPort is the main UDP port and must be set (defaults to 9993)
  42. PrimaryPort int `json:"primaryPort"`
  43. // SecondaryPort is the secondary UDP port, set to 0 to disbale (picked at random by default)
  44. SecondaryPort int `json:"secondaryPort"`
  45. // TertiaryPort is a third UDP port, set to 0 to disable (picked at random by default)
  46. TertiaryPort int `json:"tertiaryPort"`
  47. // PortSearch causes ZeroTier to try other ports automatically if it can't bind to configured ports
  48. PortSearch bool `json:"portSearch"`
  49. // PortMapping enables uPnP and NAT-PMP support
  50. PortMapping bool `json:"portMapping"`
  51. // LogSizeMax is the maximum size of the log in kilobytes or 0 for no limit and -1 to disable logging
  52. LogSizeMax int `json:"logSizeMax"`
  53. // MultipathMode sets the multipath link aggregation mode
  54. MuiltipathMode int `json:"multipathMode"`
  55. // IP/port to bind for TCP access to control API (disabled if null)
  56. APITCPBindAddress *InetAddress `json:"apiTCPBindAddress,omitempty"`
  57. // InterfacePrefixBlacklist are prefixes of physical network interface names that won't be used by ZeroTier (e.g. "lo" or "utun")
  58. InterfacePrefixBlacklist []string `json:"interfacePrefixBlacklist,omitempty"`
  59. // ExplicitAddresses are explicit IP/port addresses to advertise to other nodes, such as externally mapped ports on a router
  60. ExplicitAddresses []ExternalAddress `json:"explicitAddresses,omitempty"`
  61. }
  62. // LocalConfig is the local.conf file and stores local settings for the node.
  63. type LocalConfig struct {
  64. // Physical path configurations by CIDR IP/bits
  65. Physical map[string]LocalConfigPhysicalPathConfiguration `json:"physical,omitempty"`
  66. // Virtual node specific configurations by 10-digit hex ZeroTier address
  67. Virtual map[Address]LocalConfigVirtualAddressConfiguration `json:"virtual,omitempty"`
  68. // Network local configurations by 16-digit hex ZeroTier network ID
  69. Network map[NetworkID]NetworkLocalSettings `json:"network,omitempty"`
  70. // LocalConfigSettings contains other local settings for this node
  71. Settings LocalConfigSettings `json:"settings,omitempty"`
  72. }
  73. // Read this local config from a file, initializing to defaults if the file does not exist
  74. func (lc *LocalConfig) Read(p string, saveDefaultsIfNotExist bool) error {
  75. if lc.Physical == nil {
  76. lc.Physical = make(map[string]LocalConfigPhysicalPathConfiguration)
  77. lc.Virtual = make(map[Address]LocalConfigVirtualAddressConfiguration)
  78. lc.Network = make(map[NetworkID]NetworkLocalSettings)
  79. lc.Settings.PrimaryPort = 9993
  80. lc.Settings.SecondaryPort = 16384 + (rand.Int() % 16384)
  81. lc.Settings.TertiaryPort = 32768 + (rand.Int() % 16384)
  82. lc.Settings.PortSearch = true
  83. lc.Settings.PortMapping = true
  84. lc.Settings.LogSizeMax = 128
  85. lc.Settings.MuiltipathMode = 0
  86. switch runtime.GOOS {
  87. case "windows":
  88. lc.Settings.InterfacePrefixBlacklist = []string{"loopback"}
  89. default:
  90. lc.Settings.InterfacePrefixBlacklist = []string{"lo"}
  91. }
  92. }
  93. data, err := ioutil.ReadFile(p)
  94. if err != nil {
  95. if !os.IsNotExist(err) {
  96. return err
  97. }
  98. if saveDefaultsIfNotExist {
  99. err = lc.Write(p)
  100. if err != nil {
  101. return err
  102. }
  103. }
  104. return nil
  105. }
  106. return json.Unmarshal(data, lc)
  107. }
  108. // Write this local config to a file
  109. func (lc *LocalConfig) Write(p string) error {
  110. data, err := json.MarshalIndent(lc, "", "\t")
  111. if err != nil {
  112. return err
  113. }
  114. return ioutil.WriteFile(p, data, 0644)
  115. }