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 (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2019-02-12 06:58:24 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2018-08-25 10:36:10 +00:00
|
|
|
)
|
2015-10-27 10:37:03 +00:00
|
|
|
|
|
|
|
type DeviceConfiguration struct {
|
2016-11-07 16:40:48 +00:00
|
|
|
DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
|
|
|
|
Name string `xml:"name,attr,omitempty" json:"name"`
|
2019-02-12 06:58:24 +00:00
|
|
|
Addresses []string `xml:"address,omitempty" json:"addresses" default:"dynamic"`
|
2016-11-07 16:40:48 +00:00
|
|
|
Compression protocol.Compression `xml:"compression,attr" json:"compression"`
|
|
|
|
CertName string `xml:"certName,attr,omitempty" json:"certName"`
|
|
|
|
Introducer bool `xml:"introducer,attr" json:"introducer"`
|
|
|
|
SkipIntroductionRemovals bool `xml:"skipIntroductionRemovals,attr" json:"skipIntroductionRemovals"`
|
|
|
|
IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
|
2016-12-21 18:41:25 +00:00
|
|
|
Paused bool `xml:"paused" json:"paused"`
|
2017-04-01 09:52:31 +00:00
|
|
|
AllowedNetworks []string `xml:"allowedNetwork,omitempty" json:"allowedNetworks"`
|
2017-12-07 07:08:24 +00:00
|
|
|
AutoAcceptFolders bool `xml:"autoAcceptFolders" json:"autoAcceptFolders"`
|
2018-03-26 10:01:59 +00:00
|
|
|
MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
|
|
|
|
MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
|
2018-08-25 10:36:10 +00:00
|
|
|
IgnoredFolders []ObservedFolder `xml:"ignoredFolder" json:"ignoredFolders"`
|
|
|
|
PendingFolders []ObservedFolder `xml:"pendingFolder" json:"pendingFolders"`
|
2018-11-13 07:53:55 +00:00
|
|
|
MaxRequestKiB int `xml:"maxRequestKiB" json:"maxRequestKiB"`
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 08:47:31 +00:00
|
|
|
func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration {
|
2017-04-01 09:52:31 +00:00
|
|
|
d := DeviceConfiguration{
|
2015-11-07 08:47:31 +00:00
|
|
|
DeviceID: id,
|
|
|
|
Name: name,
|
|
|
|
}
|
2019-02-12 06:58:24 +00:00
|
|
|
|
|
|
|
util.SetDefaults(&d)
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
d.prepare(nil)
|
2017-04-01 09:52:31 +00:00
|
|
|
return d
|
2015-11-07 08:47:31 +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)
|
|
|
|
c.PendingFolders = make([]ObservedFolder, len(cfg.PendingFolders))
|
|
|
|
copy(c.PendingFolders, cfg.PendingFolders)
|
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"}
|
|
|
|
}
|
|
|
|
if len(cfg.AllowedNetworks) == 0 {
|
|
|
|
cfg.AllowedNetworks = []string{}
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:36:10 +00:00
|
|
|
ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
|
|
|
|
pendingFolders := deduplicateObservedFoldersToMap(cfg.PendingFolders)
|
|
|
|
|
|
|
|
for id := range ignoredFolders {
|
|
|
|
delete(pendingFolders, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sharedFolder := range sharedFolders {
|
|
|
|
delete(ignoredFolders, sharedFolder)
|
|
|
|
delete(pendingFolders, sharedFolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
|
|
|
|
cfg.PendingFolders = sortedObservedFolderSlice(pendingFolders)
|
|
|
|
}
|
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
|
|
|
}
|