deviceconfiguration.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  7. import (
  8. "fmt"
  9. "sort"
  10. )
  11. const defaultNumConnections = 1 // number of connections to use by default; may change in the future.
  12. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  13. c := cfg
  14. c.Addresses = make([]string, len(cfg.Addresses))
  15. copy(c.Addresses, cfg.Addresses)
  16. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  17. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  18. c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
  19. copy(c.IgnoredFolders, cfg.IgnoredFolders)
  20. return c
  21. }
  22. func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
  23. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  24. cfg.Addresses = []string{"dynamic"}
  25. }
  26. ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
  27. for _, sharedFolder := range sharedFolders {
  28. delete(ignoredFolders, sharedFolder)
  29. }
  30. cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
  31. // A device cannot be simultaneously untrusted and an introducer, nor
  32. // auto accept folders.
  33. if cfg.Untrusted {
  34. if cfg.Introducer {
  35. l.Warnf("Device %s (%s) is both untrusted and an introducer, removing introducer flag", cfg.DeviceID.Short(), cfg.Name)
  36. cfg.Introducer = false
  37. }
  38. if cfg.AutoAcceptFolders {
  39. l.Warnf("Device %s (%s) is both untrusted and auto-accepting folders, removing auto-accept flag", cfg.DeviceID.Short(), cfg.Name)
  40. cfg.AutoAcceptFolders = false
  41. }
  42. }
  43. }
  44. func (cfg *DeviceConfiguration) NumConnections() int {
  45. switch {
  46. case cfg.RawNumConnections == 0:
  47. return defaultNumConnections
  48. case cfg.RawNumConnections < 0:
  49. return 1
  50. default:
  51. return cfg.RawNumConnections
  52. }
  53. }
  54. func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
  55. for _, ignoredFolder := range cfg.IgnoredFolders {
  56. if ignoredFolder.ID == folder {
  57. return true
  58. }
  59. }
  60. return false
  61. }
  62. func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
  63. output := make([]ObservedFolder, 0, len(input))
  64. for _, folder := range input {
  65. output = append(output, folder)
  66. }
  67. sort.Slice(output, func(i, j int) bool {
  68. return output[i].Time.Before(output[j].Time)
  69. })
  70. return output
  71. }
  72. func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
  73. output := make(map[string]ObservedFolder, len(input))
  74. for _, folder := range input {
  75. if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
  76. output[folder.ID] = folder
  77. }
  78. }
  79. return output
  80. }
  81. func (cfg *DeviceConfiguration) Description() string {
  82. if cfg.Name == "" {
  83. return cfg.DeviceID.Short().String()
  84. }
  85. return fmt.Sprintf("%s (%s)", cfg.Name, cfg.DeviceID.Short())
  86. }