config.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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 https://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. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "net"
  15. "net/url"
  16. "os"
  17. "path"
  18. "path/filepath"
  19. "runtime"
  20. "sort"
  21. "strconv"
  22. "strings"
  23. "github.com/syncthing/syncthing/lib/protocol"
  24. "github.com/syncthing/syncthing/lib/rand"
  25. "github.com/syncthing/syncthing/lib/upgrade"
  26. "github.com/syncthing/syncthing/lib/util"
  27. )
  28. const (
  29. OldestHandledVersion = 10
  30. CurrentVersion = 21
  31. MaxRescanIntervalS = 365 * 24 * 60 * 60
  32. )
  33. var (
  34. // DefaultTCPPort defines default TCP port used if the URI does not specify one, for example tcp://0.0.0.0
  35. DefaultTCPPort = 22000
  36. // DefaultKCPPort defines default KCP (UDP) port used if the URI does not specify one, for example kcp://0.0.0.0
  37. DefaultKCPPort = 22020
  38. // DefaultListenAddresses should be substituted when the configuration
  39. // contains <listenAddress>default</listenAddress>. This is done by the
  40. // "consumer" of the configuration as we don't want these saved to the
  41. // config.
  42. DefaultListenAddresses = []string{
  43. util.Address("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultTCPPort))),
  44. "dynamic+https://relays.syncthing.net/endpoint",
  45. }
  46. // DefaultKCPListenAddress gets added to the default listen address set
  47. // when the appropriate feature flag is set. Feature flag stuff to be
  48. // removed later.
  49. DefaultKCPListenAddress = util.Address("kcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultKCPPort)))
  50. // DefaultDiscoveryServersV4 should be substituted when the configuration
  51. // contains <globalAnnounceServer>default-v4</globalAnnounceServer>.
  52. DefaultDiscoveryServersV4 = []string{
  53. "https://discovery-v4-2.syncthing.net/v2/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 45.55.230.38, USA
  54. "https://discovery-v4-3.syncthing.net/v2/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 128.199.95.124, Singapore
  55. "https://discovery-v4-4.syncthing.net/v2/?id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW", // 95.85.19.244, NL
  56. }
  57. // DefaultDiscoveryServersV6 should be substituted when the configuration
  58. // contains <globalAnnounceServer>default-v6</globalAnnounceServer>.
  59. DefaultDiscoveryServersV6 = []string{
  60. "https://discovery-v6-2.syncthing.net/v2/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 2604:a880:800:10::182:a001, USA
  61. "https://discovery-v6-3.syncthing.net/v2/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 2400:6180:0:d0::d9:d001, Singapore
  62. "https://discovery-v6-4.syncthing.net/v2/?id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW", // 2a03:b0c0:0:1010::4ed:3001, NL
  63. }
  64. // DefaultDiscoveryServers should be substituted when the configuration
  65. // contains <globalAnnounceServer>default</globalAnnounceServer>.
  66. DefaultDiscoveryServers = append(DefaultDiscoveryServersV4, DefaultDiscoveryServersV6...)
  67. // DefaultStunServers should be substituted when the configuration
  68. // contains <stunServer>default</stunServer>.
  69. DefaultStunServers = []string{
  70. "stun.callwithus.com:3478",
  71. "stun.counterpath.com:3478",
  72. "stun.counterpath.net:3478",
  73. "stun.ekiga.net:3478",
  74. "stun.ideasip.com:3478",
  75. "stun.internetcalls.com:3478",
  76. "stun.schlund.de:3478",
  77. "stun.sipgate.net:10000",
  78. "stun.sipgate.net:3478",
  79. "stun.voip.aebc.com:3478",
  80. "stun.voiparound.com:3478",
  81. "stun.voipbuster.com:3478",
  82. "stun.voipstunt.com:3478",
  83. "stun.voxgratia.org:3478",
  84. "stun.xten.com:3478",
  85. }
  86. // DefaultTheme is the default and fallback theme for the web UI.
  87. DefaultTheme = "default"
  88. )
  89. func New(myID protocol.DeviceID) Configuration {
  90. var cfg Configuration
  91. cfg.Version = CurrentVersion
  92. cfg.OriginalVersion = CurrentVersion
  93. util.SetDefaults(&cfg)
  94. util.SetDefaults(&cfg.Options)
  95. util.SetDefaults(&cfg.GUI)
  96. // Can't happen.
  97. if err := cfg.prepare(myID); err != nil {
  98. panic("bug: error in preparing new folder: " + err.Error())
  99. }
  100. return cfg
  101. }
  102. func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  103. var cfg Configuration
  104. util.SetDefaults(&cfg)
  105. util.SetDefaults(&cfg.Options)
  106. util.SetDefaults(&cfg.GUI)
  107. if err := xml.NewDecoder(r).Decode(&cfg); err != nil {
  108. return Configuration{}, err
  109. }
  110. cfg.OriginalVersion = cfg.Version
  111. if err := cfg.prepare(myID); err != nil {
  112. return Configuration{}, err
  113. }
  114. return cfg, nil
  115. }
  116. func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  117. var cfg Configuration
  118. util.SetDefaults(&cfg)
  119. util.SetDefaults(&cfg.Options)
  120. util.SetDefaults(&cfg.GUI)
  121. bs, err := ioutil.ReadAll(r)
  122. if err != nil {
  123. return Configuration{}, err
  124. }
  125. if err := json.Unmarshal(bs, &cfg); err != nil {
  126. return Configuration{}, err
  127. }
  128. cfg.OriginalVersion = cfg.Version
  129. if err := cfg.prepare(myID); err != nil {
  130. return Configuration{}, err
  131. }
  132. return cfg, nil
  133. }
  134. type Configuration struct {
  135. Version int `xml:"version,attr" json:"version"`
  136. Folders []FolderConfiguration `xml:"folder" json:"folders"`
  137. Devices []DeviceConfiguration `xml:"device" json:"devices"`
  138. GUI GUIConfiguration `xml:"gui" json:"gui"`
  139. Options OptionsConfiguration `xml:"options" json:"options"`
  140. IgnoredDevices []protocol.DeviceID `xml:"ignoredDevice" json:"ignoredDevices"`
  141. IgnoredFolders []string `xml:"ignoredFolder" json:"ignoredFolders"`
  142. XMLName xml.Name `xml:"configuration" json:"-"`
  143. MyID protocol.DeviceID `xml:"-" json:"-"` // Provided by the instantiator.
  144. OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion
  145. }
  146. func (cfg Configuration) Copy() Configuration {
  147. newCfg := cfg
  148. // Deep copy FolderConfigurations
  149. newCfg.Folders = make([]FolderConfiguration, len(cfg.Folders))
  150. for i := range newCfg.Folders {
  151. newCfg.Folders[i] = cfg.Folders[i].Copy()
  152. }
  153. // Deep copy DeviceConfigurations
  154. newCfg.Devices = make([]DeviceConfiguration, len(cfg.Devices))
  155. for i := range newCfg.Devices {
  156. newCfg.Devices[i] = cfg.Devices[i].Copy()
  157. }
  158. newCfg.Options = cfg.Options.Copy()
  159. // DeviceIDs are values
  160. newCfg.IgnoredDevices = make([]protocol.DeviceID, len(cfg.IgnoredDevices))
  161. copy(newCfg.IgnoredDevices, cfg.IgnoredDevices)
  162. // FolderConfiguraion.ID is type string
  163. newCfg.IgnoredFolders = make([]string, len(cfg.IgnoredFolders))
  164. copy(newCfg.IgnoredFolders, cfg.IgnoredFolders)
  165. return newCfg
  166. }
  167. func (cfg *Configuration) WriteXML(w io.Writer) error {
  168. e := xml.NewEncoder(w)
  169. e.Indent("", " ")
  170. err := e.Encode(cfg)
  171. if err != nil {
  172. return err
  173. }
  174. _, err = w.Write([]byte("\n"))
  175. return err
  176. }
  177. func (cfg *Configuration) prepare(myID protocol.DeviceID) error {
  178. var myName string
  179. cfg.MyID = myID
  180. // Ensure this device is present in the config
  181. for _, device := range cfg.Devices {
  182. if device.DeviceID == myID {
  183. goto found
  184. }
  185. }
  186. myName, _ = os.Hostname()
  187. cfg.Devices = append(cfg.Devices, DeviceConfiguration{
  188. DeviceID: myID,
  189. Name: myName,
  190. })
  191. found:
  192. if err := cfg.clean(); err != nil {
  193. return err
  194. }
  195. // Ensure that we are part of the devices
  196. for i := range cfg.Folders {
  197. cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
  198. }
  199. return nil
  200. }
  201. func (cfg *Configuration) clean() error {
  202. util.FillNilSlices(&cfg.Options)
  203. // Initialize any empty slices
  204. if cfg.Folders == nil {
  205. cfg.Folders = []FolderConfiguration{}
  206. }
  207. if cfg.IgnoredDevices == nil {
  208. cfg.IgnoredDevices = []protocol.DeviceID{}
  209. }
  210. if cfg.IgnoredFolders == nil {
  211. cfg.IgnoredFolders = []string{}
  212. }
  213. if cfg.Options.AlwaysLocalNets == nil {
  214. cfg.Options.AlwaysLocalNets = []string{}
  215. }
  216. if cfg.Options.UnackedNotificationIDs == nil {
  217. cfg.Options.UnackedNotificationIDs = []string{}
  218. }
  219. // Prepare folders and check for duplicates. Duplicates are bad and
  220. // dangerous, can't currently be resolved in the GUI, and shouldn't
  221. // happen when configured by the GUI. We return with an error in that
  222. // situation.
  223. seenFolders := make(map[string]struct{})
  224. for i := range cfg.Folders {
  225. folder := &cfg.Folders[i]
  226. folder.prepare()
  227. if _, ok := seenFolders[folder.ID]; ok {
  228. return fmt.Errorf("duplicate folder ID %q in configuration", folder.ID)
  229. }
  230. seenFolders[folder.ID] = struct{}{}
  231. }
  232. // Remove ignored folders that are anyway part of the configuration.
  233. for i := 0; i < len(cfg.IgnoredFolders); i++ {
  234. if _, ok := seenFolders[cfg.IgnoredFolders[i]]; ok {
  235. cfg.IgnoredFolders = append(cfg.IgnoredFolders[:i], cfg.IgnoredFolders[i+1:]...)
  236. i-- // IgnoredFolders[i] now points to something else, so needs to be rechecked
  237. }
  238. }
  239. cfg.Options.ListenAddresses = util.UniqueStrings(cfg.Options.ListenAddresses)
  240. cfg.Options.GlobalAnnServers = util.UniqueStrings(cfg.Options.GlobalAnnServers)
  241. if cfg.Version > 0 && cfg.Version < OldestHandledVersion {
  242. l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
  243. }
  244. // Upgrade configuration versions as appropriate
  245. if cfg.Version <= 10 {
  246. convertV10V11(cfg)
  247. }
  248. if cfg.Version == 11 {
  249. convertV11V12(cfg)
  250. }
  251. if cfg.Version == 12 {
  252. convertV12V13(cfg)
  253. }
  254. if cfg.Version == 13 {
  255. convertV13V14(cfg)
  256. }
  257. if cfg.Version == 14 {
  258. convertV14V15(cfg)
  259. }
  260. if cfg.Version == 15 {
  261. convertV15V16(cfg)
  262. }
  263. if cfg.Version == 16 {
  264. convertV16V17(cfg)
  265. }
  266. if cfg.Version == 17 {
  267. convertV17V18(cfg)
  268. }
  269. if cfg.Version == 18 {
  270. convertV18V19(cfg)
  271. }
  272. if cfg.Version == 19 {
  273. convertV19V20(cfg)
  274. }
  275. if cfg.Version == 20 {
  276. convertV20V21(cfg)
  277. }
  278. // Build a list of available devices
  279. existingDevices := make(map[protocol.DeviceID]bool)
  280. for _, device := range cfg.Devices {
  281. existingDevices[device.DeviceID] = true
  282. }
  283. // Ensure that the device list is free from duplicates
  284. cfg.Devices = ensureNoDuplicateDevices(cfg.Devices)
  285. sort.Sort(DeviceConfigurationList(cfg.Devices))
  286. // Ensure that any loose devices are not present in the wrong places
  287. // Ensure that there are no duplicate devices
  288. // Ensure that the versioning configuration parameter map is not nil
  289. for i := range cfg.Folders {
  290. cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
  291. cfg.Folders[i].Devices = ensureNoDuplicateFolderDevices(cfg.Folders[i].Devices)
  292. if cfg.Folders[i].Versioning.Params == nil {
  293. cfg.Folders[i].Versioning.Params = map[string]string{}
  294. }
  295. sort.Sort(FolderDeviceConfigurationList(cfg.Folders[i].Devices))
  296. }
  297. for i := range cfg.Devices {
  298. cfg.Devices[i].prepare()
  299. }
  300. // Very short reconnection intervals are annoying
  301. if cfg.Options.ReconnectIntervalS < 5 {
  302. cfg.Options.ReconnectIntervalS = 5
  303. }
  304. if cfg.GUI.APIKey == "" {
  305. cfg.GUI.APIKey = rand.String(32)
  306. }
  307. // The list of ignored devices should not contain any devices that have
  308. // been manually added to the config.
  309. newIgnoredDevices := []protocol.DeviceID{}
  310. for _, dev := range cfg.IgnoredDevices {
  311. if !existingDevices[dev] {
  312. newIgnoredDevices = append(newIgnoredDevices, dev)
  313. }
  314. }
  315. cfg.IgnoredDevices = newIgnoredDevices
  316. return nil
  317. }
  318. func convertV20V21(cfg *Configuration) {
  319. for _, folder := range cfg.Folders {
  320. switch folder.Versioning.Type {
  321. case "simple", "trashcan":
  322. // Clean out symlinks in the known place
  323. cleanSymlinks(filepath.Join(folder.Path(), ".stversions"))
  324. case "staggered":
  325. versionDir := folder.Versioning.Params["versionsPath"]
  326. if versionDir == "" {
  327. // default place
  328. cleanSymlinks(filepath.Join(folder.Path(), ".stversions"))
  329. } else if filepath.IsAbs(versionDir) {
  330. // absolute
  331. cleanSymlinks(versionDir)
  332. } else {
  333. // relative to folder
  334. cleanSymlinks(filepath.Join(folder.Path(), versionDir))
  335. }
  336. }
  337. }
  338. cfg.Version = 21
  339. }
  340. func convertV19V20(cfg *Configuration) {
  341. cfg.Options.MinHomeDiskFree = Size{Value: cfg.Options.DeprecatedMinHomeDiskFreePct, Unit: "%"}
  342. cfg.Options.DeprecatedMinHomeDiskFreePct = 0
  343. for i := range cfg.Folders {
  344. cfg.Folders[i].MinDiskFree = Size{Value: cfg.Folders[i].DeprecatedMinDiskFreePct, Unit: "%"}
  345. cfg.Folders[i].DeprecatedMinDiskFreePct = 0
  346. }
  347. cfg.Version = 20
  348. }
  349. func convertV18V19(cfg *Configuration) {
  350. // Triggers a database tweak
  351. cfg.Version = 19
  352. }
  353. func convertV17V18(cfg *Configuration) {
  354. // Do channel selection for existing users. Those who have auto upgrades
  355. // and usage reporting on default to the candidate channel. Others get
  356. // stable.
  357. if cfg.Options.URAccepted > 0 && cfg.Options.AutoUpgradeIntervalH > 0 {
  358. cfg.Options.UpgradeToPreReleases = true
  359. }
  360. // Show a notification to explain what's going on, except if upgrades
  361. // are disabled by compilation or environment variable in which case
  362. // it's not relevant.
  363. if !upgrade.DisabledByCompilation && os.Getenv("STNOUPGRADE") == "" {
  364. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "channelNotification")
  365. }
  366. cfg.Version = 18
  367. }
  368. func convertV16V17(cfg *Configuration) {
  369. for i := range cfg.Folders {
  370. cfg.Folders[i].Fsync = true
  371. }
  372. cfg.Version = 17
  373. }
  374. func convertV15V16(cfg *Configuration) {
  375. // Triggers a database tweak
  376. cfg.Version = 16
  377. }
  378. func convertV14V15(cfg *Configuration) {
  379. // Undo v0.13.0 broken migration
  380. for i, addr := range cfg.Options.GlobalAnnServers {
  381. switch addr {
  382. case "default-v4v2/":
  383. cfg.Options.GlobalAnnServers[i] = "default-v4"
  384. case "default-v6v2/":
  385. cfg.Options.GlobalAnnServers[i] = "default-v6"
  386. }
  387. }
  388. cfg.Version = 15
  389. }
  390. func convertV13V14(cfg *Configuration) {
  391. // Not using the ignore cache is the new default. Disable it on existing
  392. // configurations.
  393. cfg.Options.CacheIgnoredFiles = false
  394. // Migrate UPnP -> NAT options
  395. cfg.Options.NATEnabled = cfg.Options.DeprecatedUPnPEnabled
  396. cfg.Options.DeprecatedUPnPEnabled = false
  397. cfg.Options.NATLeaseM = cfg.Options.DeprecatedUPnPLeaseM
  398. cfg.Options.DeprecatedUPnPLeaseM = 0
  399. cfg.Options.NATRenewalM = cfg.Options.DeprecatedUPnPRenewalM
  400. cfg.Options.DeprecatedUPnPRenewalM = 0
  401. cfg.Options.NATTimeoutS = cfg.Options.DeprecatedUPnPTimeoutS
  402. cfg.Options.DeprecatedUPnPTimeoutS = 0
  403. // Replace the default listen address "tcp://0.0.0.0:22000" with the
  404. // string "default", but only if we also have the default relay pool
  405. // among the relay servers as this is implied by the new "default"
  406. // entry.
  407. hasDefault := false
  408. for _, raddr := range cfg.Options.DeprecatedRelayServers {
  409. if raddr == "dynamic+https://relays.syncthing.net/endpoint" {
  410. for i, addr := range cfg.Options.ListenAddresses {
  411. if addr == "tcp://0.0.0.0:22000" {
  412. cfg.Options.ListenAddresses[i] = "default"
  413. hasDefault = true
  414. break
  415. }
  416. }
  417. break
  418. }
  419. }
  420. // Copy relay addresses into listen addresses.
  421. for _, addr := range cfg.Options.DeprecatedRelayServers {
  422. if hasDefault && addr == "dynamic+https://relays.syncthing.net/endpoint" {
  423. // Skip the default relay address if we already have the
  424. // "default" entry in the list.
  425. continue
  426. }
  427. if addr == "" {
  428. continue
  429. }
  430. cfg.Options.ListenAddresses = append(cfg.Options.ListenAddresses, addr)
  431. }
  432. cfg.Options.DeprecatedRelayServers = nil
  433. // For consistency
  434. sort.Strings(cfg.Options.ListenAddresses)
  435. var newAddrs []string
  436. for _, addr := range cfg.Options.GlobalAnnServers {
  437. uri, err := url.Parse(addr)
  438. if err != nil {
  439. // That's odd. Skip the broken address.
  440. continue
  441. }
  442. if uri.Scheme == "https" {
  443. uri.Path = path.Join(uri.Path, "v2") + "/"
  444. addr = uri.String()
  445. }
  446. newAddrs = append(newAddrs, addr)
  447. }
  448. cfg.Options.GlobalAnnServers = newAddrs
  449. for i, fcfg := range cfg.Folders {
  450. if fcfg.DeprecatedReadOnly {
  451. cfg.Folders[i].Type = FolderTypeSendOnly
  452. } else {
  453. cfg.Folders[i].Type = FolderTypeSendReceive
  454. }
  455. cfg.Folders[i].DeprecatedReadOnly = false
  456. }
  457. // v0.13-beta already had config version 13 but did not get the new URL
  458. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  459. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  460. }
  461. cfg.Version = 14
  462. }
  463. func convertV12V13(cfg *Configuration) {
  464. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  465. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  466. }
  467. cfg.Version = 13
  468. }
  469. func convertV11V12(cfg *Configuration) {
  470. // Change listen address schema
  471. for i, addr := range cfg.Options.ListenAddresses {
  472. if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") {
  473. cfg.Options.ListenAddresses[i] = util.Address("tcp", addr)
  474. }
  475. }
  476. for i, device := range cfg.Devices {
  477. for j, addr := range device.Addresses {
  478. if addr != "dynamic" && addr != "" {
  479. cfg.Devices[i].Addresses[j] = util.Address("tcp", addr)
  480. }
  481. }
  482. }
  483. // Use new discovery server
  484. var newDiscoServers []string
  485. var useDefault bool
  486. for _, addr := range cfg.Options.GlobalAnnServers {
  487. if addr == "udp4://announce.syncthing.net:22026" {
  488. useDefault = true
  489. } else if addr == "udp6://announce-v6.syncthing.net:22026" {
  490. useDefault = true
  491. } else {
  492. newDiscoServers = append(newDiscoServers, addr)
  493. }
  494. }
  495. if useDefault {
  496. newDiscoServers = append(newDiscoServers, "default")
  497. }
  498. cfg.Options.GlobalAnnServers = newDiscoServers
  499. // Use new multicast group
  500. if cfg.Options.LocalAnnMCAddr == "[ff32::5222]:21026" {
  501. cfg.Options.LocalAnnMCAddr = "[ff12::8384]:21027"
  502. }
  503. // Use new local discovery port
  504. if cfg.Options.LocalAnnPort == 21025 {
  505. cfg.Options.LocalAnnPort = 21027
  506. }
  507. // Set MaxConflicts to unlimited
  508. for i := range cfg.Folders {
  509. cfg.Folders[i].MaxConflicts = -1
  510. }
  511. cfg.Version = 12
  512. }
  513. func convertV10V11(cfg *Configuration) {
  514. // Set minimum disk free of existing folders to 1%
  515. for i := range cfg.Folders {
  516. cfg.Folders[i].DeprecatedMinDiskFreePct = 1
  517. }
  518. cfg.Version = 11
  519. }
  520. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  521. for _, device := range devices {
  522. if device.DeviceID.Equals(myID) {
  523. return devices
  524. }
  525. }
  526. devices = append(devices, FolderDeviceConfiguration{
  527. DeviceID: myID,
  528. })
  529. return devices
  530. }
  531. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  532. count := len(devices)
  533. i := 0
  534. loop:
  535. for i < count {
  536. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  537. devices[i] = devices[count-1]
  538. count--
  539. continue loop
  540. }
  541. i++
  542. }
  543. return devices[0:count]
  544. }
  545. func ensureNoDuplicateFolderDevices(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  546. count := len(devices)
  547. i := 0
  548. seenDevices := make(map[protocol.DeviceID]bool)
  549. loop:
  550. for i < count {
  551. id := devices[i].DeviceID
  552. if _, ok := seenDevices[id]; ok {
  553. devices[i] = devices[count-1]
  554. count--
  555. continue loop
  556. }
  557. seenDevices[id] = true
  558. i++
  559. }
  560. return devices[0:count]
  561. }
  562. func ensureNoDuplicateDevices(devices []DeviceConfiguration) []DeviceConfiguration {
  563. count := len(devices)
  564. i := 0
  565. seenDevices := make(map[protocol.DeviceID]bool)
  566. loop:
  567. for i < count {
  568. id := devices[i].DeviceID
  569. if _, ok := seenDevices[id]; ok {
  570. devices[i] = devices[count-1]
  571. count--
  572. continue loop
  573. }
  574. seenDevices[id] = true
  575. i++
  576. }
  577. return devices[0:count]
  578. }
  579. func cleanSymlinks(dir string) {
  580. if runtime.GOOS == "windows" {
  581. // We don't do symlinks on Windows. Additionally, there may
  582. // be things that look like symlinks that are not, which we
  583. // should leave alone. Deduplicated files, for example.
  584. return
  585. }
  586. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  587. if err != nil {
  588. return err
  589. }
  590. if info.Mode()&os.ModeSymlink != 0 {
  591. l.Infoln("Removing incorrectly versioned symlink", path)
  592. os.Remove(path)
  593. return filepath.SkipDir
  594. }
  595. return nil
  596. })
  597. }