config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. "net"
  14. "net/url"
  15. "os"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "github.com/pkg/errors"
  20. "github.com/syncthing/syncthing/lib/build"
  21. "github.com/syncthing/syncthing/lib/fs"
  22. "github.com/syncthing/syncthing/lib/protocol"
  23. "github.com/syncthing/syncthing/lib/util"
  24. )
  25. const (
  26. OldestHandledVersion = 10
  27. CurrentVersion = 36
  28. MaxRescanIntervalS = 365 * 24 * 60 * 60
  29. )
  30. var (
  31. // DefaultTCPPort defines default TCP port used if the URI does not specify one, for example tcp://0.0.0.0
  32. DefaultTCPPort = 22000
  33. // DefaultQUICPort defines default QUIC port used if the URI does not specify one, for example quic://0.0.0.0
  34. DefaultQUICPort = 22000
  35. // DefaultListenAddresses should be substituted when the configuration
  36. // contains <listenAddress>default</listenAddress>. This is done by the
  37. // "consumer" of the configuration as we don't want these saved to the
  38. // config.
  39. DefaultListenAddresses = []string{
  40. util.Address("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultTCPPort))),
  41. "dynamic+https://relays.syncthing.net/endpoint",
  42. util.Address("quic", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultQUICPort))),
  43. }
  44. DefaultGUIPort = 8384
  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. // Default stun servers should be substituted when the configuration
  63. // contains <stunServer>default</stunServer>.
  64. // DefaultPrimaryStunServers are servers provided by us (to avoid causing the public servers burden)
  65. DefaultPrimaryStunServers = []string{
  66. "stun.syncthing.net:3478",
  67. }
  68. DefaultSecondaryStunServers = []string{
  69. "stun.callwithus.com:3478",
  70. "stun.counterpath.com:3478",
  71. "stun.counterpath.net:3478",
  72. "stun.ekiga.net:3478",
  73. "stun.ideasip.com:3478",
  74. "stun.internetcalls.com:3478",
  75. "stun.schlund.de:3478",
  76. "stun.sipgate.net:10000",
  77. "stun.sipgate.net:3478",
  78. "stun.voip.aebc.com:3478",
  79. "stun.voiparound.com:3478",
  80. "stun.voipbuster.com:3478",
  81. "stun.voipstunt.com:3478",
  82. "stun.xten.com:3478",
  83. }
  84. )
  85. var (
  86. errFolderIDEmpty = errors.New("folder has empty ID")
  87. errFolderIDDuplicate = errors.New("folder has duplicate ID")
  88. errFolderPathEmpty = errors.New("folder has empty path")
  89. )
  90. func New(myID protocol.DeviceID) Configuration {
  91. var cfg Configuration
  92. cfg.Version = CurrentVersion
  93. cfg.Options.UnackedNotificationIDs = []string{"authenticationUserAndPassword"}
  94. util.SetDefaults(&cfg)
  95. // Can't happen.
  96. if err := cfg.prepare(myID); err != nil {
  97. l.Warnln("bug: error in preparing new folder:", err)
  98. panic("error in preparing new folder")
  99. }
  100. return cfg
  101. }
  102. func (cfg *Configuration) ProbeFreePorts() error {
  103. port, err := getFreePort("127.0.0.1", DefaultGUIPort)
  104. if err != nil {
  105. return errors.Wrap(err, "get free port (GUI)")
  106. }
  107. cfg.GUI.RawAddress = fmt.Sprintf("127.0.0.1:%d", port)
  108. port, err = getFreePort("0.0.0.0", DefaultTCPPort)
  109. if err != nil {
  110. return errors.Wrap(err, "get free port (BEP)")
  111. }
  112. if port == DefaultTCPPort {
  113. cfg.Options.RawListenAddresses = []string{"default"}
  114. } else {
  115. cfg.Options.RawListenAddresses = []string{
  116. util.Address("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(port))),
  117. "dynamic+https://relays.syncthing.net/endpoint",
  118. util.Address("quic", net.JoinHostPort("0.0.0.0", strconv.Itoa(port))),
  119. }
  120. }
  121. return nil
  122. }
  123. type xmlConfiguration struct {
  124. Configuration
  125. XMLName xml.Name `xml:"configuration"`
  126. }
  127. func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, int, error) {
  128. var cfg xmlConfiguration
  129. util.SetDefaults(&cfg)
  130. if err := xml.NewDecoder(r).Decode(&cfg); err != nil {
  131. return Configuration{}, 0, err
  132. }
  133. originalVersion := cfg.Version
  134. if err := cfg.prepare(myID); err != nil {
  135. return Configuration{}, originalVersion, err
  136. }
  137. return cfg.Configuration, originalVersion, nil
  138. }
  139. func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  140. bs, err := io.ReadAll(r)
  141. if err != nil {
  142. return Configuration{}, err
  143. }
  144. var cfg Configuration
  145. util.SetDefaults(&cfg)
  146. if err := json.Unmarshal(bs, &cfg); err != nil {
  147. return Configuration{}, err
  148. }
  149. // Unmarshal list of devices and folders separately to set defaults
  150. var rawFoldersDevices struct {
  151. Folders []json.RawMessage
  152. Devices []json.RawMessage
  153. }
  154. if err := json.Unmarshal(bs, &rawFoldersDevices); err != nil {
  155. return Configuration{}, err
  156. }
  157. cfg.Folders = make([]FolderConfiguration, len(rawFoldersDevices.Folders))
  158. for i, bs := range rawFoldersDevices.Folders {
  159. cfg.Folders[i] = cfg.Defaults.Folder.Copy()
  160. if err := json.Unmarshal(bs, &cfg.Folders[i]); err != nil {
  161. return Configuration{}, err
  162. }
  163. }
  164. cfg.Devices = make([]DeviceConfiguration, len(rawFoldersDevices.Devices))
  165. for i, bs := range rawFoldersDevices.Devices {
  166. cfg.Devices[i] = cfg.Defaults.Device.Copy()
  167. if err := json.Unmarshal(bs, &cfg.Devices[i]); err != nil {
  168. return Configuration{}, err
  169. }
  170. }
  171. if err := cfg.prepare(myID); err != nil {
  172. return Configuration{}, err
  173. }
  174. return cfg, nil
  175. }
  176. func (cfg Configuration) Copy() Configuration {
  177. newCfg := cfg
  178. // Deep copy FolderConfigurations
  179. newCfg.Folders = make([]FolderConfiguration, len(cfg.Folders))
  180. for i := range newCfg.Folders {
  181. newCfg.Folders[i] = cfg.Folders[i].Copy()
  182. }
  183. // Deep copy DeviceConfigurations
  184. newCfg.Devices = make([]DeviceConfiguration, len(cfg.Devices))
  185. for i := range newCfg.Devices {
  186. newCfg.Devices[i] = cfg.Devices[i].Copy()
  187. }
  188. newCfg.Options = cfg.Options.Copy()
  189. newCfg.GUI = cfg.GUI.Copy()
  190. // DeviceIDs are values
  191. newCfg.IgnoredDevices = make([]ObservedDevice, len(cfg.IgnoredDevices))
  192. copy(newCfg.IgnoredDevices, cfg.IgnoredDevices)
  193. return newCfg
  194. }
  195. func (cfg *Configuration) WriteXML(w io.Writer) error {
  196. e := xml.NewEncoder(w)
  197. e.Indent("", " ")
  198. xmlCfg := xmlConfiguration{Configuration: *cfg}
  199. err := e.Encode(xmlCfg)
  200. if err != nil {
  201. return err
  202. }
  203. _, err = w.Write([]byte("\n"))
  204. return err
  205. }
  206. func (cfg *Configuration) prepare(myID protocol.DeviceID) error {
  207. cfg.ensureMyDevice(myID)
  208. existingDevices, err := cfg.prepareFoldersAndDevices(myID)
  209. if err != nil {
  210. return err
  211. }
  212. cfg.GUI.prepare()
  213. guiPWIsSet := cfg.GUI.User != "" && cfg.GUI.Password != ""
  214. cfg.Options.prepare(guiPWIsSet)
  215. cfg.prepareIgnoredDevices(existingDevices)
  216. cfg.Defaults.prepare(myID, existingDevices)
  217. cfg.removeDeprecatedProtocols()
  218. util.FillNilExceptDeprecated(cfg)
  219. // TestIssue1750 relies on migrations happening after preparing options.
  220. cfg.applyMigrations()
  221. return nil
  222. }
  223. func (cfg *Configuration) ensureMyDevice(myID protocol.DeviceID) {
  224. for _, device := range cfg.Devices {
  225. if device.DeviceID == myID {
  226. return
  227. }
  228. }
  229. myName, _ := os.Hostname()
  230. cfg.Devices = append(cfg.Devices, DeviceConfiguration{
  231. DeviceID: myID,
  232. Name: myName,
  233. })
  234. }
  235. func (cfg *Configuration) prepareFoldersAndDevices(myID protocol.DeviceID) (map[protocol.DeviceID]bool, error) {
  236. existingDevices := cfg.prepareDeviceList()
  237. sharedFolders, err := cfg.prepareFolders(myID, existingDevices)
  238. if err != nil {
  239. return nil, err
  240. }
  241. cfg.prepareDevices(sharedFolders)
  242. return existingDevices, nil
  243. }
  244. func (cfg *Configuration) prepareDeviceList() map[protocol.DeviceID]bool {
  245. // Ensure that the device list is
  246. // - free from duplicates
  247. // - no devices with empty ID
  248. // - sorted by ID
  249. // Happen before preparting folders as that needs a correct device list.
  250. cfg.Devices = ensureNoDuplicateOrEmptyIDDevices(cfg.Devices)
  251. sort.Slice(cfg.Devices, func(a, b int) bool {
  252. return cfg.Devices[a].DeviceID.Compare(cfg.Devices[b].DeviceID) == -1
  253. })
  254. // Build a list of available devices
  255. existingDevices := make(map[protocol.DeviceID]bool, len(cfg.Devices))
  256. for _, device := range cfg.Devices {
  257. existingDevices[device.DeviceID] = true
  258. }
  259. return existingDevices
  260. }
  261. func (cfg *Configuration) prepareFolders(myID protocol.DeviceID, existingDevices map[protocol.DeviceID]bool) (map[protocol.DeviceID][]string, error) {
  262. // Prepare folders and check for duplicates. Duplicates are bad and
  263. // dangerous, can't currently be resolved in the GUI, and shouldn't
  264. // happen when configured by the GUI. We return with an error in that
  265. // situation.
  266. sharedFolders := make(map[protocol.DeviceID][]string, len(cfg.Devices))
  267. existingFolders := make(map[string]*FolderConfiguration, len(cfg.Folders))
  268. for i := range cfg.Folders {
  269. folder := &cfg.Folders[i]
  270. if folder.ID == "" {
  271. return nil, errFolderIDEmpty
  272. }
  273. if folder.Path == "" {
  274. return nil, fmt.Errorf("folder %q: %w", folder.ID, errFolderPathEmpty)
  275. }
  276. if _, ok := existingFolders[folder.ID]; ok {
  277. return nil, fmt.Errorf("folder %q: %w", folder.ID, errFolderIDDuplicate)
  278. }
  279. folder.prepare(myID, existingDevices)
  280. existingFolders[folder.ID] = folder
  281. for _, dev := range folder.Devices {
  282. sharedFolders[dev.DeviceID] = append(sharedFolders[dev.DeviceID], folder.ID)
  283. }
  284. }
  285. // Ensure that the folder list is sorted by ID
  286. sort.Slice(cfg.Folders, func(a, b int) bool {
  287. return cfg.Folders[a].ID < cfg.Folders[b].ID
  288. })
  289. return sharedFolders, nil
  290. }
  291. func (cfg *Configuration) prepareDevices(sharedFolders map[protocol.DeviceID][]string) {
  292. for i := range cfg.Devices {
  293. cfg.Devices[i].prepare(sharedFolders[cfg.Devices[i].DeviceID])
  294. }
  295. }
  296. func (cfg *Configuration) prepareIgnoredDevices(existingDevices map[protocol.DeviceID]bool) map[protocol.DeviceID]bool {
  297. // The list of ignored devices should not contain any devices that have
  298. // been manually added to the config.
  299. newIgnoredDevices := cfg.IgnoredDevices[:0]
  300. ignoredDevices := make(map[protocol.DeviceID]bool, len(cfg.IgnoredDevices))
  301. for _, dev := range cfg.IgnoredDevices {
  302. if !existingDevices[dev.ID] {
  303. ignoredDevices[dev.ID] = true
  304. newIgnoredDevices = append(newIgnoredDevices, dev)
  305. }
  306. }
  307. cfg.IgnoredDevices = newIgnoredDevices
  308. return ignoredDevices
  309. }
  310. func (cfg *Configuration) removeDeprecatedProtocols() {
  311. // Deprecated protocols are removed from the list of listeners and
  312. // device addresses. So far just kcp*.
  313. for _, prefix := range []string{"kcp"} {
  314. cfg.Options.RawListenAddresses = filterURLSchemePrefix(cfg.Options.RawListenAddresses, prefix)
  315. for i := range cfg.Devices {
  316. dev := &cfg.Devices[i]
  317. dev.Addresses = filterURLSchemePrefix(dev.Addresses, prefix)
  318. }
  319. }
  320. }
  321. func (cfg *Configuration) applyMigrations() {
  322. if cfg.Version > 0 && cfg.Version < OldestHandledVersion {
  323. l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
  324. }
  325. // Upgrade configuration versions as appropriate
  326. migrationsMut.Lock()
  327. migrations.apply(cfg)
  328. migrationsMut.Unlock()
  329. }
  330. func (cfg *Configuration) Device(id protocol.DeviceID) (DeviceConfiguration, int, bool) {
  331. for i, device := range cfg.Devices {
  332. if device.DeviceID == id {
  333. return device, i, true
  334. }
  335. }
  336. return DeviceConfiguration{}, 0, false
  337. }
  338. // DeviceMap returns a map of device ID to device configuration for the given configuration.
  339. func (cfg *Configuration) DeviceMap() map[protocol.DeviceID]DeviceConfiguration {
  340. m := make(map[protocol.DeviceID]DeviceConfiguration, len(cfg.Devices))
  341. for _, dev := range cfg.Devices {
  342. m[dev.DeviceID] = dev
  343. }
  344. return m
  345. }
  346. func (cfg *Configuration) SetDevice(device DeviceConfiguration) {
  347. cfg.SetDevices([]DeviceConfiguration{device})
  348. }
  349. func (cfg *Configuration) SetDevices(devices []DeviceConfiguration) {
  350. inds := make(map[protocol.DeviceID]int, len(cfg.Devices))
  351. for i, device := range cfg.Devices {
  352. inds[device.DeviceID] = i
  353. }
  354. filtered := devices[:0]
  355. for _, device := range devices {
  356. if i, ok := inds[device.DeviceID]; ok {
  357. cfg.Devices[i] = device
  358. } else {
  359. filtered = append(filtered, device)
  360. }
  361. }
  362. cfg.Devices = append(cfg.Devices, filtered...)
  363. }
  364. func (cfg *Configuration) Folder(id string) (FolderConfiguration, int, bool) {
  365. for i, folder := range cfg.Folders {
  366. if folder.ID == id {
  367. return folder, i, true
  368. }
  369. }
  370. return FolderConfiguration{}, 0, false
  371. }
  372. // FolderMap returns a map of folder ID to folder configuration for the given configuration.
  373. func (cfg *Configuration) FolderMap() map[string]FolderConfiguration {
  374. m := make(map[string]FolderConfiguration, len(cfg.Folders))
  375. for _, folder := range cfg.Folders {
  376. m[folder.ID] = folder
  377. }
  378. return m
  379. }
  380. // FolderPasswords returns the folder passwords set for this device, for
  381. // folders that have an encryption password set.
  382. func (cfg Configuration) FolderPasswords(device protocol.DeviceID) map[string]string {
  383. res := make(map[string]string, len(cfg.Folders))
  384. for _, folder := range cfg.Folders {
  385. if dev, ok := folder.Device(device); ok && dev.EncryptionPassword != "" {
  386. res[folder.ID] = dev.EncryptionPassword
  387. }
  388. }
  389. return res
  390. }
  391. func (cfg *Configuration) SetFolder(folder FolderConfiguration) {
  392. cfg.SetFolders([]FolderConfiguration{folder})
  393. }
  394. func (cfg *Configuration) SetFolders(folders []FolderConfiguration) {
  395. inds := make(map[string]int, len(cfg.Folders))
  396. for i, folder := range cfg.Folders {
  397. inds[folder.ID] = i
  398. }
  399. filtered := folders[:0]
  400. for _, folder := range folders {
  401. if i, ok := inds[folder.ID]; ok {
  402. cfg.Folders[i] = folder
  403. } else {
  404. filtered = append(filtered, folder)
  405. }
  406. }
  407. cfg.Folders = append(cfg.Folders, filtered...)
  408. }
  409. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  410. for _, device := range devices {
  411. if device.DeviceID.Equals(myID) {
  412. return devices
  413. }
  414. }
  415. devices = append(devices, FolderDeviceConfiguration{
  416. DeviceID: myID,
  417. })
  418. return devices
  419. }
  420. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  421. count := len(devices)
  422. i := 0
  423. loop:
  424. for i < count {
  425. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  426. devices[i] = devices[count-1]
  427. count--
  428. continue loop
  429. }
  430. i++
  431. }
  432. return devices[0:count]
  433. }
  434. func ensureNoDuplicateFolderDevices(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  435. count := len(devices)
  436. i := 0
  437. seenDevices := make(map[protocol.DeviceID]bool)
  438. loop:
  439. for i < count {
  440. id := devices[i].DeviceID
  441. if _, ok := seenDevices[id]; ok {
  442. devices[i] = devices[count-1]
  443. count--
  444. continue loop
  445. }
  446. seenDevices[id] = true
  447. i++
  448. }
  449. return devices[0:count]
  450. }
  451. func ensureNoDuplicateOrEmptyIDDevices(devices []DeviceConfiguration) []DeviceConfiguration {
  452. count := len(devices)
  453. i := 0
  454. seenDevices := make(map[protocol.DeviceID]bool)
  455. loop:
  456. for i < count {
  457. id := devices[i].DeviceID
  458. if _, ok := seenDevices[id]; ok || id == protocol.EmptyDeviceID {
  459. devices[i] = devices[count-1]
  460. count--
  461. continue loop
  462. }
  463. seenDevices[id] = true
  464. i++
  465. }
  466. return devices[0:count]
  467. }
  468. func cleanSymlinks(filesystem fs.Filesystem, dir string) {
  469. if build.IsWindows {
  470. // We don't do symlinks on Windows. Additionally, there may
  471. // be things that look like symlinks that are not, which we
  472. // should leave alone. Deduplicated files, for example.
  473. return
  474. }
  475. filesystem.Walk(dir, func(path string, info fs.FileInfo, err error) error {
  476. if err != nil {
  477. return err
  478. }
  479. if info.IsSymlink() {
  480. l.Infoln("Removing incorrectly versioned symlink", path)
  481. filesystem.Remove(path)
  482. return fs.SkipDir
  483. }
  484. return nil
  485. })
  486. }
  487. // filterURLSchemePrefix returns the list of addresses after removing all
  488. // entries whose URL scheme matches the given prefix.
  489. func filterURLSchemePrefix(addrs []string, prefix string) []string {
  490. for i := 0; i < len(addrs); i++ {
  491. uri, err := url.Parse(addrs[i])
  492. if err != nil {
  493. continue
  494. }
  495. if strings.HasPrefix(uri.Scheme, prefix) {
  496. // Remove this entry
  497. copy(addrs[i:], addrs[i+1:])
  498. addrs = addrs[:len(addrs)-1]
  499. i--
  500. }
  501. }
  502. return addrs
  503. }
  504. // tried in succession and the first to succeed is returned. If none succeed,
  505. // a random high port is returned.
  506. func getFreePort(host string, ports ...int) (int, error) {
  507. for _, port := range ports {
  508. c, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
  509. if err == nil {
  510. c.Close()
  511. return port, nil
  512. }
  513. }
  514. c, err := net.Listen("tcp", host+":0")
  515. if err != nil {
  516. return 0, err
  517. }
  518. addr := c.Addr().(*net.TCPAddr)
  519. c.Close()
  520. return addr.Port, nil
  521. }
  522. func (defaults *Defaults) prepare(myID protocol.DeviceID, existingDevices map[protocol.DeviceID]bool) {
  523. ensureZeroForNodefault(&FolderConfiguration{}, &defaults.Folder)
  524. ensureZeroForNodefault(&DeviceConfiguration{}, &defaults.Device)
  525. defaults.Folder.prepare(myID, existingDevices)
  526. defaults.Device.prepare(nil)
  527. }
  528. func ensureZeroForNodefault(empty interface{}, target interface{}) {
  529. util.CopyMatchingTag(empty, target, "nodefault", func(v string) bool {
  530. if len(v) > 0 && v != "true" {
  531. panic(fmt.Sprintf(`unexpected tag value: %s. expected untagged or "true"`, v))
  532. }
  533. return len(v) > 0
  534. })
  535. }
  536. func (i Ignores) Copy() Ignores {
  537. out := Ignores{Lines: make([]string, len(i.Lines))}
  538. copy(out.Lines, i.Lines)
  539. return out
  540. }