config.go 22 KB

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