config.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // Package config implements reading and writing of the syncthing configuration file.
  7. package config
  8. import (
  9. "encoding/json"
  10. "encoding/xml"
  11. "io"
  12. "os"
  13. "sort"
  14. "strings"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. "github.com/syncthing/syncthing/lib/util"
  17. )
  18. const (
  19. OldestHandledVersion = 10
  20. CurrentVersion = 12
  21. MaxRescanIntervalS = 365 * 24 * 60 * 60
  22. )
  23. var (
  24. // DefaultDiscoveryServersV4 should be substituted when the configuration
  25. // contains <globalAnnounceServer>default-v4</globalAnnounceServer>. This is
  26. // done by the "consumer" of the configuration, as we don't want these
  27. // saved to the config.
  28. DefaultDiscoveryServersV4 = []string{
  29. "https://discovery-v4-1.syncthing.net/?id=SR7AARM-TCBUZ5O-VFAXY4D-CECGSDE-3Q6IZ4G-XG7AH75-OBIXJQV-QJ6NLQA", // 194.126.249.5, Sweden
  30. "https://discovery-v4-2.syncthing.net/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 45.55.230.38, USA
  31. "https://discovery-v4-3.syncthing.net/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 128.199.95.124, Singapore
  32. }
  33. // DefaultDiscoveryServersV6 should be substituted when the configuration
  34. // contains <globalAnnounceServer>default-v6</globalAnnounceServer>.
  35. DefaultDiscoveryServersV6 = []string{
  36. "https://discovery-v6-1.syncthing.net/?id=SR7AARM-TCBUZ5O-VFAXY4D-CECGSDE-3Q6IZ4G-XG7AH75-OBIXJQV-QJ6NLQA", // 2001:470:28:4d6::5, Sweden
  37. "https://discovery-v6-2.syncthing.net/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 2604:a880:800:10::182:a001, USA
  38. "https://discovery-v6-3.syncthing.net/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 2400:6180:0:d0::d9:d001, Singapore
  39. }
  40. // DefaultDiscoveryServers should be substituted when the configuration
  41. // contains <globalAnnounceServer>default</globalAnnounceServer>.
  42. DefaultDiscoveryServers = append(DefaultDiscoveryServersV4, DefaultDiscoveryServersV6...)
  43. // DefaultTheme is the default and fallback theme for the web UI.
  44. DefaultTheme = "default"
  45. )
  46. func New(myID protocol.DeviceID) Configuration {
  47. var cfg Configuration
  48. cfg.Version = CurrentVersion
  49. cfg.OriginalVersion = CurrentVersion
  50. util.SetDefaults(&cfg)
  51. util.SetDefaults(&cfg.Options)
  52. util.SetDefaults(&cfg.GUI)
  53. cfg.prepare(myID)
  54. return cfg
  55. }
  56. func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  57. var cfg Configuration
  58. util.SetDefaults(&cfg)
  59. util.SetDefaults(&cfg.Options)
  60. util.SetDefaults(&cfg.GUI)
  61. err := xml.NewDecoder(r).Decode(&cfg)
  62. cfg.OriginalVersion = cfg.Version
  63. cfg.prepare(myID)
  64. return cfg, err
  65. }
  66. func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  67. var cfg Configuration
  68. util.SetDefaults(&cfg)
  69. util.SetDefaults(&cfg.Options)
  70. util.SetDefaults(&cfg.GUI)
  71. err := json.NewDecoder(r).Decode(&cfg)
  72. cfg.OriginalVersion = cfg.Version
  73. cfg.prepare(myID)
  74. return cfg, err
  75. }
  76. type Configuration struct {
  77. Version int `xml:"version,attr" json:"version"`
  78. Folders []FolderConfiguration `xml:"folder" json:"folders"`
  79. Devices []DeviceConfiguration `xml:"device" json:"devices"`
  80. GUI GUIConfiguration `xml:"gui" json:"gui"`
  81. Options OptionsConfiguration `xml:"options" json:"options"`
  82. IgnoredDevices []protocol.DeviceID `xml:"ignoredDevice" json:"ignoredDevices"`
  83. XMLName xml.Name `xml:"configuration" json:"-"`
  84. OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion
  85. }
  86. func (cfg Configuration) Copy() Configuration {
  87. newCfg := cfg
  88. // Deep copy FolderConfigurations
  89. newCfg.Folders = make([]FolderConfiguration, len(cfg.Folders))
  90. for i := range newCfg.Folders {
  91. newCfg.Folders[i] = cfg.Folders[i].Copy()
  92. }
  93. // Deep copy DeviceConfigurations
  94. newCfg.Devices = make([]DeviceConfiguration, len(cfg.Devices))
  95. for i := range newCfg.Devices {
  96. newCfg.Devices[i] = cfg.Devices[i].Copy()
  97. }
  98. newCfg.Options = cfg.Options.Copy()
  99. // DeviceIDs are values
  100. newCfg.IgnoredDevices = make([]protocol.DeviceID, len(cfg.IgnoredDevices))
  101. copy(newCfg.IgnoredDevices, cfg.IgnoredDevices)
  102. return newCfg
  103. }
  104. func (cfg *Configuration) WriteXML(w io.Writer) error {
  105. e := xml.NewEncoder(w)
  106. e.Indent("", " ")
  107. err := e.Encode(cfg)
  108. if err != nil {
  109. return err
  110. }
  111. _, err = w.Write([]byte("\n"))
  112. return err
  113. }
  114. func (cfg *Configuration) prepare(myID protocol.DeviceID) {
  115. util.FillNilSlices(&cfg.Options)
  116. // Initialize any empty slices
  117. if cfg.Folders == nil {
  118. cfg.Folders = []FolderConfiguration{}
  119. }
  120. if cfg.IgnoredDevices == nil {
  121. cfg.IgnoredDevices = []protocol.DeviceID{}
  122. }
  123. if cfg.Options.AlwaysLocalNets == nil {
  124. cfg.Options.AlwaysLocalNets = []string{}
  125. }
  126. // Check for missing, bad or duplicate folder ID:s
  127. var seenFolders = map[string]*FolderConfiguration{}
  128. for i := range cfg.Folders {
  129. folder := &cfg.Folders[i]
  130. folder.prepare()
  131. if seen, ok := seenFolders[folder.ID]; ok {
  132. l.Warnf("Multiple folders with ID %q; disabling", folder.ID)
  133. seen.Invalid = "duplicate folder ID"
  134. folder.Invalid = "duplicate folder ID"
  135. } else {
  136. seenFolders[folder.ID] = folder
  137. }
  138. }
  139. cfg.Options.ListenAddress = util.UniqueStrings(cfg.Options.ListenAddress)
  140. cfg.Options.GlobalAnnServers = util.UniqueStrings(cfg.Options.GlobalAnnServers)
  141. if cfg.Version > 0 && cfg.Version < OldestHandledVersion {
  142. l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
  143. }
  144. // Upgrade configuration versions as appropriate
  145. if cfg.Version <= 10 {
  146. convertV10V11(cfg)
  147. }
  148. if cfg.Version == 11 {
  149. convertV11V12(cfg)
  150. }
  151. // Build a list of available devices
  152. existingDevices := make(map[protocol.DeviceID]bool)
  153. for _, device := range cfg.Devices {
  154. existingDevices[device.DeviceID] = true
  155. }
  156. // Ensure this device is present in the config
  157. if !existingDevices[myID] {
  158. myName, _ := os.Hostname()
  159. cfg.Devices = append(cfg.Devices, DeviceConfiguration{
  160. DeviceID: myID,
  161. Name: myName,
  162. })
  163. existingDevices[myID] = true
  164. }
  165. // Ensure that the device list is free from duplicates
  166. cfg.Devices = ensureNoDuplicateDevices(cfg.Devices)
  167. sort.Sort(DeviceConfigurationList(cfg.Devices))
  168. // Ensure that any loose devices are not present in the wrong places
  169. // Ensure that there are no duplicate devices
  170. // Ensure that puller settings are sane
  171. // Ensure that the versioning configuration parameter map is not nil
  172. for i := range cfg.Folders {
  173. cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
  174. cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
  175. cfg.Folders[i].Devices = ensureNoDuplicateFolderDevices(cfg.Folders[i].Devices)
  176. if cfg.Folders[i].Versioning.Params == nil {
  177. cfg.Folders[i].Versioning.Params = map[string]string{}
  178. }
  179. sort.Sort(FolderDeviceConfigurationList(cfg.Folders[i].Devices))
  180. }
  181. // An empty address list is equivalent to a single "dynamic" entry
  182. for i := range cfg.Devices {
  183. n := &cfg.Devices[i]
  184. if len(n.Addresses) == 0 || len(n.Addresses) == 1 && n.Addresses[0] == "" {
  185. n.Addresses = []string{"dynamic"}
  186. }
  187. }
  188. // Very short reconnection intervals are annoying
  189. if cfg.Options.ReconnectIntervalS < 5 {
  190. cfg.Options.ReconnectIntervalS = 5
  191. }
  192. if cfg.GUI.APIKey == "" {
  193. cfg.GUI.APIKey = util.RandomString(32)
  194. }
  195. }
  196. func convertV11V12(cfg *Configuration) {
  197. // Change listen address schema
  198. for i, addr := range cfg.Options.ListenAddress {
  199. if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") {
  200. cfg.Options.ListenAddress[i] = util.Address("tcp", addr)
  201. }
  202. }
  203. for i, device := range cfg.Devices {
  204. for j, addr := range device.Addresses {
  205. if addr != "dynamic" && addr != "" {
  206. cfg.Devices[i].Addresses[j] = util.Address("tcp", addr)
  207. }
  208. }
  209. }
  210. // Use new discovery server
  211. var newDiscoServers []string
  212. var useDefault bool
  213. for _, addr := range cfg.Options.GlobalAnnServers {
  214. if addr == "udp4://announce.syncthing.net:22026" {
  215. useDefault = true
  216. } else if addr == "udp6://announce-v6.syncthing.net:22026" {
  217. useDefault = true
  218. } else {
  219. newDiscoServers = append(newDiscoServers, addr)
  220. }
  221. }
  222. if useDefault {
  223. newDiscoServers = append(newDiscoServers, "default")
  224. }
  225. cfg.Options.GlobalAnnServers = newDiscoServers
  226. // Use new multicast group
  227. if cfg.Options.LocalAnnMCAddr == "[ff32::5222]:21026" {
  228. cfg.Options.LocalAnnMCAddr = "[ff12::8384]:21027"
  229. }
  230. // Use new local discovery port
  231. if cfg.Options.LocalAnnPort == 21025 {
  232. cfg.Options.LocalAnnPort = 21027
  233. }
  234. // Set MaxConflicts to unlimited
  235. for i := range cfg.Folders {
  236. cfg.Folders[i].MaxConflicts = -1
  237. }
  238. cfg.Version = 12
  239. }
  240. func convertV10V11(cfg *Configuration) {
  241. // Set minimum disk free of existing folders to 1%
  242. for i := range cfg.Folders {
  243. cfg.Folders[i].MinDiskFreePct = 1
  244. }
  245. cfg.Version = 11
  246. }
  247. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  248. for _, device := range devices {
  249. if device.DeviceID.Equals(myID) {
  250. return devices
  251. }
  252. }
  253. devices = append(devices, FolderDeviceConfiguration{
  254. DeviceID: myID,
  255. })
  256. return devices
  257. }
  258. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  259. count := len(devices)
  260. i := 0
  261. loop:
  262. for i < count {
  263. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  264. devices[i] = devices[count-1]
  265. count--
  266. continue loop
  267. }
  268. i++
  269. }
  270. return devices[0:count]
  271. }
  272. func ensureNoDuplicateFolderDevices(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  273. count := len(devices)
  274. i := 0
  275. seenDevices := make(map[protocol.DeviceID]bool)
  276. loop:
  277. for i < count {
  278. id := devices[i].DeviceID
  279. if _, ok := seenDevices[id]; ok {
  280. devices[i] = devices[count-1]
  281. count--
  282. continue loop
  283. }
  284. seenDevices[id] = true
  285. i++
  286. }
  287. return devices[0:count]
  288. }
  289. func ensureNoDuplicateDevices(devices []DeviceConfiguration) []DeviceConfiguration {
  290. count := len(devices)
  291. i := 0
  292. seenDevices := make(map[protocol.DeviceID]bool)
  293. loop:
  294. for i < count {
  295. id := devices[i].DeviceID
  296. if _, ok := seenDevices[id]; ok {
  297. devices[i] = devices[count-1]
  298. count--
  299. continue loop
  300. }
  301. seenDevices[id] = true
  302. i++
  303. }
  304. return devices[0:count]
  305. }