config.go 21 KB

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