2015-10-27 10:37:03 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-27 10:37:03 +00:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
import (
|
2022-06-20 17:36:45 +00:00
|
|
|
"fmt"
|
2018-08-25 10:36:10 +00:00
|
|
|
"sort"
|
|
|
|
)
|
2015-10-27 10:37:03 +00:00
|
|
|
|
2017-04-01 09:52:31 +00:00
|
|
|
func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
|
|
|
|
c := cfg
|
|
|
|
c.Addresses = make([]string, len(cfg.Addresses))
|
|
|
|
copy(c.Addresses, cfg.Addresses)
|
|
|
|
c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
|
|
|
|
copy(c.AllowedNetworks, cfg.AllowedNetworks)
|
2018-08-25 10:36:10 +00:00
|
|
|
c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
|
|
|
|
copy(c.IgnoredFolders, cfg.IgnoredFolders)
|
2015-10-27 10:37:03 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
|
2017-04-01 09:52:31 +00:00
|
|
|
if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
|
|
|
|
cfg.Addresses = []string{"dynamic"}
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
|
|
|
|
|
|
|
|
for _, sharedFolder := range sharedFolders {
|
|
|
|
delete(ignoredFolders, sharedFolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
|
2023-06-14 07:24:31 +00:00
|
|
|
|
|
|
|
// A device cannot be simultaneously untrusted and an introducer, nor
|
|
|
|
// auto accept folders.
|
|
|
|
if cfg.Untrusted {
|
|
|
|
if cfg.Introducer {
|
|
|
|
l.Warnf("Device %s (%s) is both untrusted and an introducer, removing introducer flag", cfg.DeviceID.Short(), cfg.Name)
|
|
|
|
cfg.Introducer = false
|
|
|
|
}
|
|
|
|
if cfg.AutoAcceptFolders {
|
|
|
|
l.Warnf("Device %s (%s) is both untrusted and auto-accepting folders, removing auto-accept flag", cfg.DeviceID.Short(), cfg.Name)
|
|
|
|
cfg.AutoAcceptFolders = false
|
|
|
|
}
|
|
|
|
}
|
2018-08-25 10:36:10 +00:00
|
|
|
}
|
2015-10-27 10:37:03 +00:00
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
|
|
|
|
for _, ignoredFolder := range cfg.IgnoredFolders {
|
|
|
|
if ignoredFolder.ID == folder {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
|
|
|
|
output := make([]ObservedFolder, 0, len(input))
|
|
|
|
for _, folder := range input {
|
|
|
|
output = append(output, folder)
|
|
|
|
}
|
|
|
|
sort.Slice(output, func(i, j int) bool {
|
|
|
|
return output[i].Time.Before(output[j].Time)
|
|
|
|
})
|
|
|
|
return output
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
|
|
|
|
output := make(map[string]ObservedFolder, len(input))
|
|
|
|
for _, folder := range input {
|
|
|
|
if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
|
|
|
|
output[folder.ID] = folder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
2022-06-20 17:36:45 +00:00
|
|
|
|
|
|
|
func (cfg *DeviceConfiguration) Description() string {
|
|
|
|
if cfg.Name == "" {
|
2022-07-28 15:00:41 +00:00
|
|
|
return cfg.DeviceID.Short().String()
|
2022-06-20 17:36:45 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s (%s)", cfg.Name, cfg.DeviceID.Short())
|
|
|
|
}
|