deviceconfiguration.go 4.6 KB

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