config.go 18 KB

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