config.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 = 25
  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. // Build a list of available devices
  289. existingDevices := make(map[protocol.DeviceID]bool)
  290. for _, device := range cfg.Devices {
  291. existingDevices[device.DeviceID] = true
  292. }
  293. // Ensure that the device list is free from duplicates
  294. cfg.Devices = ensureNoDuplicateDevices(cfg.Devices)
  295. sort.Sort(DeviceConfigurationList(cfg.Devices))
  296. // Ensure that any loose devices are not present in the wrong places
  297. // Ensure that there are no duplicate devices
  298. // Ensure that the versioning configuration parameter map is not nil
  299. for i := range cfg.Folders {
  300. cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
  301. cfg.Folders[i].Devices = ensureNoDuplicateFolderDevices(cfg.Folders[i].Devices)
  302. if cfg.Folders[i].Versioning.Params == nil {
  303. cfg.Folders[i].Versioning.Params = map[string]string{}
  304. }
  305. sort.Sort(FolderDeviceConfigurationList(cfg.Folders[i].Devices))
  306. }
  307. for i := range cfg.Devices {
  308. cfg.Devices[i].prepare()
  309. }
  310. // Very short reconnection intervals are annoying
  311. if cfg.Options.ReconnectIntervalS < 5 {
  312. cfg.Options.ReconnectIntervalS = 5
  313. }
  314. if cfg.GUI.APIKey == "" {
  315. cfg.GUI.APIKey = rand.String(32)
  316. }
  317. // The list of ignored devices should not contain any devices that have
  318. // been manually added to the config.
  319. newIgnoredDevices := []protocol.DeviceID{}
  320. for _, dev := range cfg.IgnoredDevices {
  321. if !existingDevices[dev] {
  322. newIgnoredDevices = append(newIgnoredDevices, dev)
  323. }
  324. }
  325. cfg.IgnoredDevices = newIgnoredDevices
  326. return nil
  327. }
  328. func convertV24V25(cfg *Configuration) {
  329. for i := range cfg.Folders {
  330. cfg.Folders[i].FSWatcherDelayS = 10
  331. }
  332. cfg.Version = 25
  333. }
  334. func convertV23V24(cfg *Configuration) {
  335. cfg.Options.URSeen = 2
  336. cfg.Version = 24
  337. }
  338. func convertV22V23(cfg *Configuration) {
  339. permBits := fs.FileMode(0777)
  340. if runtime.GOOS == "windows" {
  341. // Windows has no umask so we must chose a safer set of bits to
  342. // begin with.
  343. permBits = 0700
  344. }
  345. for i := range cfg.Folders {
  346. fs := cfg.Folders[i].Filesystem()
  347. // Invalid config posted, or tests.
  348. if fs == nil {
  349. continue
  350. }
  351. if stat, err := fs.Stat(".stfolder"); err == nil && !stat.IsDir() {
  352. err = fs.Remove(".stfolder")
  353. if err == nil {
  354. err = fs.Mkdir(".stfolder", permBits)
  355. fs.Hide(".stfolder") // ignore error
  356. }
  357. if err != nil {
  358. l.Infoln("Failed to upgrade folder marker:", err)
  359. }
  360. }
  361. }
  362. cfg.Version = 23
  363. }
  364. func convertV21V22(cfg *Configuration) {
  365. for i := range cfg.Folders {
  366. cfg.Folders[i].FilesystemType = fs.FilesystemTypeBasic
  367. // Migrate to templated external versioner commands
  368. if cfg.Folders[i].Versioning.Type == "external" {
  369. cfg.Folders[i].Versioning.Params["command"] += " %FOLDER_PATH% %FILE_PATH%"
  370. }
  371. }
  372. cfg.Version = 22
  373. }
  374. func convertV20V21(cfg *Configuration) {
  375. for _, folder := range cfg.Folders {
  376. if folder.FilesystemType != fs.FilesystemTypeBasic {
  377. continue
  378. }
  379. switch folder.Versioning.Type {
  380. case "simple", "trashcan":
  381. // Clean out symlinks in the known place
  382. cleanSymlinks(folder.Filesystem(), ".stversions")
  383. case "staggered":
  384. versionDir := folder.Versioning.Params["versionsPath"]
  385. if versionDir == "" {
  386. // default place
  387. cleanSymlinks(folder.Filesystem(), ".stversions")
  388. } else if filepath.IsAbs(versionDir) {
  389. // absolute
  390. cleanSymlinks(fs.NewFilesystem(fs.FilesystemTypeBasic, versionDir), ".")
  391. } else {
  392. // relative to folder
  393. cleanSymlinks(folder.Filesystem(), versionDir)
  394. }
  395. }
  396. }
  397. cfg.Version = 21
  398. }
  399. func convertV19V20(cfg *Configuration) {
  400. cfg.Options.MinHomeDiskFree = Size{Value: cfg.Options.DeprecatedMinHomeDiskFreePct, Unit: "%"}
  401. cfg.Options.DeprecatedMinHomeDiskFreePct = 0
  402. for i := range cfg.Folders {
  403. cfg.Folders[i].MinDiskFree = Size{Value: cfg.Folders[i].DeprecatedMinDiskFreePct, Unit: "%"}
  404. cfg.Folders[i].DeprecatedMinDiskFreePct = 0
  405. }
  406. cfg.Version = 20
  407. }
  408. func convertV18V19(cfg *Configuration) {
  409. // Triggers a database tweak
  410. cfg.Version = 19
  411. }
  412. func convertV17V18(cfg *Configuration) {
  413. // Do channel selection for existing users. Those who have auto upgrades
  414. // and usage reporting on default to the candidate channel. Others get
  415. // stable.
  416. if cfg.Options.URAccepted > 0 && cfg.Options.AutoUpgradeIntervalH > 0 {
  417. cfg.Options.UpgradeToPreReleases = true
  418. }
  419. // Show a notification to explain what's going on, except if upgrades
  420. // are disabled by compilation or environment variable in which case
  421. // it's not relevant.
  422. if !upgrade.DisabledByCompilation && os.Getenv("STNOUPGRADE") == "" {
  423. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "channelNotification")
  424. }
  425. cfg.Version = 18
  426. }
  427. func convertV16V17(cfg *Configuration) {
  428. // Fsync = true removed
  429. cfg.Version = 17
  430. }
  431. func convertV15V16(cfg *Configuration) {
  432. // Triggers a database tweak
  433. cfg.Version = 16
  434. }
  435. func convertV14V15(cfg *Configuration) {
  436. // Undo v0.13.0 broken migration
  437. for i, addr := range cfg.Options.GlobalAnnServers {
  438. switch addr {
  439. case "default-v4v2/":
  440. cfg.Options.GlobalAnnServers[i] = "default-v4"
  441. case "default-v6v2/":
  442. cfg.Options.GlobalAnnServers[i] = "default-v6"
  443. }
  444. }
  445. cfg.Version = 15
  446. }
  447. func convertV13V14(cfg *Configuration) {
  448. // Not using the ignore cache is the new default. Disable it on existing
  449. // configurations.
  450. cfg.Options.CacheIgnoredFiles = false
  451. // Migrate UPnP -> NAT options
  452. cfg.Options.NATEnabled = cfg.Options.DeprecatedUPnPEnabled
  453. cfg.Options.DeprecatedUPnPEnabled = false
  454. cfg.Options.NATLeaseM = cfg.Options.DeprecatedUPnPLeaseM
  455. cfg.Options.DeprecatedUPnPLeaseM = 0
  456. cfg.Options.NATRenewalM = cfg.Options.DeprecatedUPnPRenewalM
  457. cfg.Options.DeprecatedUPnPRenewalM = 0
  458. cfg.Options.NATTimeoutS = cfg.Options.DeprecatedUPnPTimeoutS
  459. cfg.Options.DeprecatedUPnPTimeoutS = 0
  460. // Replace the default listen address "tcp://0.0.0.0:22000" with the
  461. // string "default", but only if we also have the default relay pool
  462. // among the relay servers as this is implied by the new "default"
  463. // entry.
  464. hasDefault := false
  465. for _, raddr := range cfg.Options.DeprecatedRelayServers {
  466. if raddr == "dynamic+https://relays.syncthing.net/endpoint" {
  467. for i, addr := range cfg.Options.ListenAddresses {
  468. if addr == "tcp://0.0.0.0:22000" {
  469. cfg.Options.ListenAddresses[i] = "default"
  470. hasDefault = true
  471. break
  472. }
  473. }
  474. break
  475. }
  476. }
  477. // Copy relay addresses into listen addresses.
  478. for _, addr := range cfg.Options.DeprecatedRelayServers {
  479. if hasDefault && addr == "dynamic+https://relays.syncthing.net/endpoint" {
  480. // Skip the default relay address if we already have the
  481. // "default" entry in the list.
  482. continue
  483. }
  484. if addr == "" {
  485. continue
  486. }
  487. cfg.Options.ListenAddresses = append(cfg.Options.ListenAddresses, addr)
  488. }
  489. cfg.Options.DeprecatedRelayServers = nil
  490. // For consistency
  491. sort.Strings(cfg.Options.ListenAddresses)
  492. var newAddrs []string
  493. for _, addr := range cfg.Options.GlobalAnnServers {
  494. uri, err := url.Parse(addr)
  495. if err != nil {
  496. // That's odd. Skip the broken address.
  497. continue
  498. }
  499. if uri.Scheme == "https" {
  500. uri.Path = path.Join(uri.Path, "v2") + "/"
  501. addr = uri.String()
  502. }
  503. newAddrs = append(newAddrs, addr)
  504. }
  505. cfg.Options.GlobalAnnServers = newAddrs
  506. for i, fcfg := range cfg.Folders {
  507. if fcfg.DeprecatedReadOnly {
  508. cfg.Folders[i].Type = FolderTypeSendOnly
  509. } else {
  510. cfg.Folders[i].Type = FolderTypeSendReceive
  511. }
  512. cfg.Folders[i].DeprecatedReadOnly = false
  513. }
  514. // v0.13-beta already had config version 13 but did not get the new URL
  515. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  516. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  517. }
  518. cfg.Version = 14
  519. }
  520. func convertV12V13(cfg *Configuration) {
  521. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  522. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  523. }
  524. cfg.Version = 13
  525. }
  526. func convertV11V12(cfg *Configuration) {
  527. // Change listen address schema
  528. for i, addr := range cfg.Options.ListenAddresses {
  529. if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") {
  530. cfg.Options.ListenAddresses[i] = util.Address("tcp", addr)
  531. }
  532. }
  533. for i, device := range cfg.Devices {
  534. for j, addr := range device.Addresses {
  535. if addr != "dynamic" && addr != "" {
  536. cfg.Devices[i].Addresses[j] = util.Address("tcp", addr)
  537. }
  538. }
  539. }
  540. // Use new discovery server
  541. var newDiscoServers []string
  542. var useDefault bool
  543. for _, addr := range cfg.Options.GlobalAnnServers {
  544. if addr == "udp4://announce.syncthing.net:22026" {
  545. useDefault = true
  546. } else if addr == "udp6://announce-v6.syncthing.net:22026" {
  547. useDefault = true
  548. } else {
  549. newDiscoServers = append(newDiscoServers, addr)
  550. }
  551. }
  552. if useDefault {
  553. newDiscoServers = append(newDiscoServers, "default")
  554. }
  555. cfg.Options.GlobalAnnServers = newDiscoServers
  556. // Use new multicast group
  557. if cfg.Options.LocalAnnMCAddr == "[ff32::5222]:21026" {
  558. cfg.Options.LocalAnnMCAddr = "[ff12::8384]:21027"
  559. }
  560. // Use new local discovery port
  561. if cfg.Options.LocalAnnPort == 21025 {
  562. cfg.Options.LocalAnnPort = 21027
  563. }
  564. // Set MaxConflicts to unlimited
  565. for i := range cfg.Folders {
  566. cfg.Folders[i].MaxConflicts = -1
  567. }
  568. cfg.Version = 12
  569. }
  570. func convertV10V11(cfg *Configuration) {
  571. // Set minimum disk free of existing folders to 1%
  572. for i := range cfg.Folders {
  573. cfg.Folders[i].DeprecatedMinDiskFreePct = 1
  574. }
  575. cfg.Version = 11
  576. }
  577. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  578. for _, device := range devices {
  579. if device.DeviceID.Equals(myID) {
  580. return devices
  581. }
  582. }
  583. devices = append(devices, FolderDeviceConfiguration{
  584. DeviceID: myID,
  585. })
  586. return devices
  587. }
  588. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  589. count := len(devices)
  590. i := 0
  591. loop:
  592. for i < count {
  593. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  594. devices[i] = devices[count-1]
  595. count--
  596. continue loop
  597. }
  598. i++
  599. }
  600. return devices[0:count]
  601. }
  602. func ensureNoDuplicateFolderDevices(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  603. count := len(devices)
  604. i := 0
  605. seenDevices := make(map[protocol.DeviceID]bool)
  606. loop:
  607. for i < count {
  608. id := devices[i].DeviceID
  609. if _, ok := seenDevices[id]; ok {
  610. devices[i] = devices[count-1]
  611. count--
  612. continue loop
  613. }
  614. seenDevices[id] = true
  615. i++
  616. }
  617. return devices[0:count]
  618. }
  619. func ensureNoDuplicateDevices(devices []DeviceConfiguration) []DeviceConfiguration {
  620. count := len(devices)
  621. i := 0
  622. seenDevices := make(map[protocol.DeviceID]bool)
  623. loop:
  624. for i < count {
  625. id := devices[i].DeviceID
  626. if _, ok := seenDevices[id]; ok {
  627. devices[i] = devices[count-1]
  628. count--
  629. continue loop
  630. }
  631. seenDevices[id] = true
  632. i++
  633. }
  634. return devices[0:count]
  635. }
  636. func cleanSymlinks(filesystem fs.Filesystem, dir string) {
  637. if runtime.GOOS == "windows" {
  638. // We don't do symlinks on Windows. Additionally, there may
  639. // be things that look like symlinks that are not, which we
  640. // should leave alone. Deduplicated files, for example.
  641. return
  642. }
  643. filesystem.Walk(dir, func(path string, info fs.FileInfo, err error) error {
  644. if err != nil {
  645. return err
  646. }
  647. if info.IsSymlink() {
  648. l.Infoln("Removing incorrectly versioned symlink", path)
  649. filesystem.Remove(path)
  650. return fs.SkipDir
  651. }
  652. return nil
  653. })
  654. }