localconfig.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // LocalConfigSettings contains node settings
  34. type LocalConfigSettings struct {
  35. // PrimaryPort is the main UDP port and must be set (defaults to 9993)
  36. PrimaryPort int `json:"primaryPort"`
  37. // SecondaryPort is the secondary UDP port, set to 0 to disbale (picked at random by default)
  38. SecondaryPort int `json:"secondaryPort"`
  39. // TertiaryPort is a third UDP port, set to 0 to disable (picked at random by default)
  40. TertiaryPort int `json:"tertiaryPort"`
  41. // PortSearch causes ZeroTier to try other ports automatically if it can't bind to configured ports
  42. PortSearch bool `json:"portSearch"`
  43. // PortMapping enables uPnP and NAT-PMP support
  44. PortMapping bool `json:"portMapping"`
  45. // LogSizeMax is the maximum size of the log in kilobytes or 0 for no limit and -1 to disable logging
  46. LogSizeMax int `json:"logSizeMax"`
  47. // MultipathMode sets the multipath link aggregation mode
  48. MuiltipathMode int `json:"multipathMode"`
  49. // IP/port to bind for TCP access to control API (disabled if null)
  50. APITCPBindAddress *InetAddress `json:"apiTCPBindAddress,omitempty"`
  51. // InterfacePrefixBlacklist are prefixes of physical network interface names that won't be used by ZeroTier (e.g. "lo" or "utun")
  52. InterfacePrefixBlacklist []string `json:"interfacePrefixBlacklist,omitempty"`
  53. // ExplicitAddresses are explicit IP/port addresses to advertise to other nodes, such as externally mapped ports on a router
  54. ExplicitAddresses []*InetAddress `json:"explicitAddresses,omitempty"`
  55. }
  56. // LocalConfig is the local.conf file and stores local settings for the node.
  57. type LocalConfig struct {
  58. // Physical path configurations by CIDR IP/bits
  59. Physical map[string]*LocalConfigPhysicalPathConfiguration `json:"physical,omitempty"`
  60. // Virtual node specific configurations by 10-digit hex ZeroTier address
  61. Virtual map[Address]*LocalConfigVirtualAddressConfiguration `json:"virtual,omitempty"`
  62. // Network local configurations by 16-digit hex ZeroTier network ID
  63. Network map[NetworkID]*NetworkLocalSettings `json:"network,omitempty"`
  64. // LocalConfigSettings contains other local settings for this node
  65. Settings LocalConfigSettings `json:"settings,omitempty"`
  66. }
  67. // Read this local config from a file, initializing to defaults if the file does not exist
  68. func (lc *LocalConfig) Read(p string, saveDefaultsIfNotExist bool) error {
  69. if lc.Physical == nil {
  70. lc.Physical = make(map[string]*LocalConfigPhysicalPathConfiguration)
  71. lc.Virtual = make(map[Address]*LocalConfigVirtualAddressConfiguration)
  72. lc.Network = make(map[NetworkID]*NetworkLocalSettings)
  73. lc.Settings.PrimaryPort = 9993
  74. lc.Settings.SecondaryPort = 16384 + (rand.Int() % 16384)
  75. lc.Settings.TertiaryPort = 32768 + (rand.Int() % 16384)
  76. lc.Settings.PortSearch = true
  77. lc.Settings.PortMapping = true
  78. lc.Settings.LogSizeMax = 128
  79. lc.Settings.MuiltipathMode = 0
  80. switch runtime.GOOS {
  81. case "windows":
  82. lc.Settings.InterfacePrefixBlacklist = []string{"loopback"}
  83. default:
  84. lc.Settings.InterfacePrefixBlacklist = []string{"lo"}
  85. }
  86. }
  87. data, err := ioutil.ReadFile(p)
  88. if err != nil {
  89. if !os.IsNotExist(err) {
  90. return err
  91. }
  92. if saveDefaultsIfNotExist {
  93. err = lc.Write(p)
  94. if err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }
  100. return json.Unmarshal(data, lc)
  101. }
  102. // Write this local config to a file
  103. func (lc *LocalConfig) Write(p string) error {
  104. data, err := json.MarshalIndent(lc, "", "\t")
  105. if err != nil {
  106. return err
  107. }
  108. return ioutil.WriteFile(p, data, 0644)
  109. }