deviceconfiguration.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "slices"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. const defaultNumConnections = 1 // number of connections to use by default; may change in the future.
  13. type DeviceConfiguration struct {
  14. DeviceID protocol.DeviceID `json:"deviceID" xml:"id,attr" nodefault:"true"`
  15. Name string `json:"name" xml:"name,attr,omitempty"`
  16. Addresses []string `json:"addresses" xml:"address,omitempty"`
  17. Compression Compression `json:"compression" xml:"compression,attr"`
  18. CertName string `json:"certName" xml:"certName,attr,omitempty"`
  19. Introducer bool `json:"introducer" xml:"introducer,attr"`
  20. SkipIntroductionRemovals bool `json:"skipIntroductionRemovals" xml:"skipIntroductionRemovals,attr"`
  21. IntroducedBy protocol.DeviceID `json:"introducedBy" xml:"introducedBy,attr" nodefault:"true"`
  22. Paused bool `json:"paused" xml:"paused"`
  23. AllowedNetworks []string `json:"allowedNetworks" xml:"allowedNetwork,omitempty"`
  24. AutoAcceptFolders bool `json:"autoAcceptFolders" xml:"autoAcceptFolders"`
  25. MaxSendKbps int `json:"maxSendKbps" xml:"maxSendKbps"`
  26. MaxRecvKbps int `json:"maxRecvKbps" xml:"maxRecvKbps"`
  27. IgnoredFolders []ObservedFolder `json:"ignoredFolders" xml:"ignoredFolder"`
  28. DeprecatedPendingFolders []ObservedFolder `json:"-" xml:"pendingFolder,omitempty"` // Deprecated: Do not use.
  29. MaxRequestKiB int `json:"maxRequestKiB" xml:"maxRequestKiB"`
  30. Untrusted bool `json:"untrusted" xml:"untrusted"`
  31. RemoteGUIPort int `json:"remoteGUIPort" xml:"remoteGUIPort"`
  32. RawNumConnections int `json:"numConnections" xml:"numConnections"`
  33. }
  34. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  35. c := cfg
  36. c.Addresses = make([]string, len(cfg.Addresses))
  37. copy(c.Addresses, cfg.Addresses)
  38. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  39. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  40. c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
  41. copy(c.IgnoredFolders, cfg.IgnoredFolders)
  42. return c
  43. }
  44. func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
  45. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  46. cfg.Addresses = []string{"dynamic"}
  47. }
  48. ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
  49. for _, sharedFolder := range sharedFolders {
  50. delete(ignoredFolders, sharedFolder)
  51. }
  52. cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
  53. // A device cannot be simultaneously untrusted and an introducer, nor
  54. // auto accept folders.
  55. if cfg.Untrusted {
  56. if cfg.Introducer {
  57. l.Warnf("Device %s (%s) is both untrusted and an introducer, removing introducer flag", cfg.DeviceID.Short(), cfg.Name)
  58. cfg.Introducer = false
  59. }
  60. if cfg.AutoAcceptFolders {
  61. l.Warnf("Device %s (%s) is both untrusted and auto-accepting folders, removing auto-accept flag", cfg.DeviceID.Short(), cfg.Name)
  62. cfg.AutoAcceptFolders = false
  63. }
  64. }
  65. }
  66. func (cfg *DeviceConfiguration) NumConnections() int {
  67. switch {
  68. case cfg.RawNumConnections == 0:
  69. return defaultNumConnections
  70. case cfg.RawNumConnections < 0:
  71. return 1
  72. default:
  73. return cfg.RawNumConnections
  74. }
  75. }
  76. func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
  77. for _, ignoredFolder := range cfg.IgnoredFolders {
  78. if ignoredFolder.ID == folder {
  79. return true
  80. }
  81. }
  82. return false
  83. }
  84. func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
  85. output := make([]ObservedFolder, 0, len(input))
  86. for _, folder := range input {
  87. output = append(output, folder)
  88. }
  89. slices.SortFunc(output, func(a, b ObservedFolder) int {
  90. return a.Time.Compare(b.Time)
  91. })
  92. return output
  93. }
  94. func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
  95. output := make(map[string]ObservedFolder, len(input))
  96. for _, folder := range input {
  97. if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
  98. output[folder.ID] = folder
  99. }
  100. }
  101. return output
  102. }
  103. func (cfg *DeviceConfiguration) Description() string {
  104. if cfg.Name == "" {
  105. return cfg.DeviceID.Short().String()
  106. }
  107. return fmt.Sprintf("%s (%s)", cfg.Name, cfg.DeviceID.Short())
  108. }