2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// 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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-05-14 20:40:17 -07:00
|
|
|
// Package config implements reading and writing of the syncthing configuration file.
|
2014-05-14 17:18:09 -07:00
|
|
|
package config
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
import (
|
2015-11-04 13:30:47 -07:00
|
|
|
"encoding/json"
|
2014-03-02 15:58:14 -07:00
|
|
|
"encoding/xml"
|
2014-10-06 00:25:45 -07:00
|
|
|
"io"
|
2014-12-29 05:48:26 -07:00
|
|
|
"math/rand"
|
2015-10-30 13:22:40 -07:00
|
|
|
"net/url"
|
2014-04-22 02:46:08 -07:00
|
|
|
"os"
|
2014-03-02 15:58:14 -07:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2015-03-07 12:48:53 -07:00
|
|
|
"strings"
|
2014-04-19 04:33:51 -07:00
|
|
|
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2014-03-02 15:58:14 -07:00
|
|
|
)
|
|
|
|
|
2015-04-04 12:49:15 -07:00
|
|
|
const (
|
2015-10-27 03:46:33 -07:00
|
|
|
OldestHandledVersion = 10
|
2015-06-23 05:55:30 -07:00
|
|
|
CurrentVersion = 12
|
2015-06-20 11:21:05 -07:00
|
|
|
MaxRescanIntervalS = 365 * 24 * 60 * 60
|
2015-04-04 12:49:15 -07:00
|
|
|
)
|
2014-10-08 04:52:05 -07:00
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
var (
|
2015-11-09 07:35:14 -07:00
|
|
|
// DefaultDiscoveryServersV4 should be substituted when the configuration
|
|
|
|
// contains <globalAnnounceServer>default-v4</globalAnnounceServer>. This is
|
2015-09-20 06:30:25 -07:00
|
|
|
// done by the "consumer" of the configuration, as we don't want these
|
|
|
|
// saved to the config.
|
2015-11-09 07:35:14 -07:00
|
|
|
DefaultDiscoveryServersV4 = []string{
|
2015-09-20 13:30:31 -07:00
|
|
|
"https://discovery-v4-1.syncthing.net/?id=SR7AARM-TCBUZ5O-VFAXY4D-CECGSDE-3Q6IZ4G-XG7AH75-OBIXJQV-QJ6NLQA", // 194.126.249.5, Sweden
|
2015-11-09 05:58:44 -07:00
|
|
|
"https://discovery-v4-2.syncthing.net/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 45.55.230.38, USA
|
2015-12-01 03:14:54 -07:00
|
|
|
"https://discovery-v4-3.syncthing.net/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 128.199.95.124, Singapore
|
2015-11-09 07:35:14 -07:00
|
|
|
}
|
|
|
|
// DefaultDiscoveryServersV6 should be substituted when the configuration
|
|
|
|
// contains <globalAnnounceServer>default-v6</globalAnnounceServer>.
|
|
|
|
DefaultDiscoveryServersV6 = []string{
|
2015-09-20 13:30:31 -07:00
|
|
|
"https://discovery-v6-1.syncthing.net/?id=SR7AARM-TCBUZ5O-VFAXY4D-CECGSDE-3Q6IZ4G-XG7AH75-OBIXJQV-QJ6NLQA", // 2001:470:28:4d6::5, Sweden
|
2015-11-09 05:58:44 -07:00
|
|
|
"https://discovery-v6-2.syncthing.net/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 2604:a880:800:10::182:a001, USA
|
2015-12-01 03:14:54 -07:00
|
|
|
"https://discovery-v6-3.syncthing.net/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 2400:6180:0:d0::d9:d001, Singapore
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
2015-11-09 07:35:14 -07:00
|
|
|
// DefaultDiscoveryServers should be substituted when the configuration
|
|
|
|
// contains <globalAnnounceServer>default</globalAnnounceServer>.
|
|
|
|
DefaultDiscoveryServers = append(DefaultDiscoveryServersV4, DefaultDiscoveryServersV6...)
|
2016-01-10 08:37:31 -07:00
|
|
|
|
|
|
|
// DefaultTheme is the default and fallback theme for the web UI.
|
|
|
|
DefaultTheme = "default"
|
2015-09-20 06:30:25 -07:00
|
|
|
)
|
|
|
|
|
2015-10-27 04:04:51 -07:00
|
|
|
func New(myID protocol.DeviceID) Configuration {
|
|
|
|
var cfg Configuration
|
|
|
|
cfg.Version = CurrentVersion
|
|
|
|
cfg.OriginalVersion = CurrentVersion
|
|
|
|
|
|
|
|
setDefaults(&cfg)
|
|
|
|
setDefaults(&cfg.Options)
|
|
|
|
setDefaults(&cfg.GUI)
|
|
|
|
|
|
|
|
cfg.prepare(myID)
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
|
|
|
|
var cfg Configuration
|
|
|
|
|
|
|
|
setDefaults(&cfg)
|
|
|
|
setDefaults(&cfg.Options)
|
|
|
|
setDefaults(&cfg.GUI)
|
|
|
|
|
|
|
|
err := xml.NewDecoder(r).Decode(&cfg)
|
|
|
|
cfg.OriginalVersion = cfg.Version
|
|
|
|
|
|
|
|
cfg.prepare(myID)
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2015-11-04 13:30:47 -07:00
|
|
|
func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
|
|
|
|
var cfg Configuration
|
|
|
|
|
|
|
|
setDefaults(&cfg)
|
|
|
|
setDefaults(&cfg.Options)
|
|
|
|
setDefaults(&cfg.GUI)
|
|
|
|
|
|
|
|
err := json.NewDecoder(r).Decode(&cfg)
|
|
|
|
cfg.OriginalVersion = cfg.Version
|
|
|
|
|
|
|
|
cfg.prepare(myID)
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
type Configuration struct {
|
2015-03-10 15:45:43 -07:00
|
|
|
Version int `xml:"version,attr" json:"version"`
|
|
|
|
Folders []FolderConfiguration `xml:"folder" json:"folders"`
|
|
|
|
Devices []DeviceConfiguration `xml:"device" json:"devices"`
|
|
|
|
GUI GUIConfiguration `xml:"gui" json:"gui"`
|
|
|
|
Options OptionsConfiguration `xml:"options" json:"options"`
|
|
|
|
IgnoredDevices []protocol.DeviceID `xml:"ignoredDevice" json:"ignoredDevices"`
|
2014-12-27 16:12:12 -07:00
|
|
|
XMLName xml.Name `xml:"configuration" json:"-"`
|
2014-09-28 05:22:39 -07:00
|
|
|
|
2015-04-04 12:49:15 -07:00
|
|
|
OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
func (cfg Configuration) Copy() Configuration {
|
|
|
|
newCfg := cfg
|
2015-04-05 08:36:52 -07:00
|
|
|
|
|
|
|
// Deep copy FolderConfigurations
|
2015-04-28 13:32:10 -07:00
|
|
|
newCfg.Folders = make([]FolderConfiguration, len(cfg.Folders))
|
|
|
|
for i := range newCfg.Folders {
|
|
|
|
newCfg.Folders[i] = cfg.Folders[i].Copy()
|
2015-04-05 08:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deep copy DeviceConfigurations
|
2015-04-28 13:32:10 -07:00
|
|
|
newCfg.Devices = make([]DeviceConfiguration, len(cfg.Devices))
|
|
|
|
for i := range newCfg.Devices {
|
|
|
|
newCfg.Devices[i] = cfg.Devices[i].Copy()
|
2015-04-05 08:36:52 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
newCfg.Options = cfg.Options.Copy()
|
2015-04-05 08:36:52 -07:00
|
|
|
|
|
|
|
// DeviceIDs are values
|
2015-04-28 13:32:10 -07:00
|
|
|
newCfg.IgnoredDevices = make([]protocol.DeviceID, len(cfg.IgnoredDevices))
|
|
|
|
copy(newCfg.IgnoredDevices, cfg.IgnoredDevices)
|
2015-04-05 08:36:52 -07:00
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
return newCfg
|
2015-04-05 08:36:52 -07:00
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
func (cfg *Configuration) WriteXML(w io.Writer) error {
|
|
|
|
e := xml.NewEncoder(w)
|
|
|
|
e.Indent("", " ")
|
|
|
|
err := e.Encode(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte("\n"))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (cfg *Configuration) prepare(myID protocol.DeviceID) {
|
2014-03-04 14:29:43 -07:00
|
|
|
fillNilSlices(&cfg.Options)
|
2014-03-04 03:25:10 -07:00
|
|
|
|
2015-11-04 13:30:47 -07:00
|
|
|
// Initialize any empty slices
|
2014-09-28 04:00:38 -07:00
|
|
|
if cfg.Folders == nil {
|
|
|
|
cfg.Folders = []FolderConfiguration{}
|
2014-05-12 19:57:38 -07:00
|
|
|
}
|
2014-12-27 16:12:12 -07:00
|
|
|
if cfg.IgnoredDevices == nil {
|
|
|
|
cfg.IgnoredDevices = []protocol.DeviceID{}
|
|
|
|
}
|
2015-10-27 04:04:51 -07:00
|
|
|
if cfg.Options.AlwaysLocalNets == nil {
|
|
|
|
cfg.Options.AlwaysLocalNets = []string{}
|
|
|
|
}
|
2014-05-12 19:57:38 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Check for missing, bad or duplicate folder ID:s
|
|
|
|
var seenFolders = map[string]*FolderConfiguration{}
|
|
|
|
for i := range cfg.Folders {
|
|
|
|
folder := &cfg.Folders[i]
|
2015-11-05 01:01:47 -07:00
|
|
|
folder.prepare()
|
2015-06-20 11:21:05 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if seen, ok := seenFolders[folder.ID]; ok {
|
|
|
|
l.Warnf("Multiple folders with ID %q; disabling", folder.ID)
|
|
|
|
seen.Invalid = "duplicate folder ID"
|
|
|
|
folder.Invalid = "duplicate folder ID"
|
2014-04-27 12:53:27 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
seenFolders[folder.ID] = folder
|
2014-03-29 10:53:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
|
|
|
|
cfg.Options.GlobalAnnServers = uniqueStrings(cfg.Options.GlobalAnnServers)
|
|
|
|
|
2015-11-05 01:31:36 -07:00
|
|
|
if cfg.Version > 0 && cfg.Version < OldestHandledVersion {
|
2015-04-04 12:49:15 -07:00
|
|
|
l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
|
2014-06-17 14:22:19 -07:00
|
|
|
}
|
|
|
|
|
2015-03-07 12:48:53 -07:00
|
|
|
// Upgrade configuration versions as appropriate
|
2015-10-27 03:46:33 -07:00
|
|
|
if cfg.Version <= 10 {
|
2015-07-16 03:52:36 -07:00
|
|
|
convertV10V11(cfg)
|
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
if cfg.Version == 11 {
|
|
|
|
convertV11V12(cfg)
|
|
|
|
}
|
2014-11-20 14:24:11 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Build a list of available devices
|
|
|
|
existingDevices := make(map[protocol.DeviceID]bool)
|
|
|
|
for _, device := range cfg.Devices {
|
|
|
|
existingDevices[device.DeviceID] = true
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
|
|
|
|
2014-10-06 12:43:38 -07:00
|
|
|
// Ensure this device is present in the config
|
|
|
|
if !existingDevices[myID] {
|
2014-08-16 15:20:21 -07:00
|
|
|
myName, _ := os.Hostname()
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg.Devices = append(cfg.Devices, DeviceConfiguration{
|
|
|
|
DeviceID: myID,
|
2014-09-28 04:05:25 -07:00
|
|
|
Name: myName,
|
2014-08-16 15:20:21 -07:00
|
|
|
})
|
2014-10-06 12:43:38 -07:00
|
|
|
existingDevices[myID] = true
|
2014-08-16 15:20:21 -07:00
|
|
|
}
|
2014-10-06 12:28:16 -07:00
|
|
|
|
2015-12-31 01:17:17 -07:00
|
|
|
// Ensure that the device list is free from duplicates
|
|
|
|
cfg.Devices = ensureNoDuplicateDevices(cfg.Devices)
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
sort.Sort(DeviceConfigurationList(cfg.Devices))
|
|
|
|
// Ensure that any loose devices are not present in the wrong places
|
|
|
|
// Ensure that there are no duplicate devices
|
2014-11-23 11:43:49 -07:00
|
|
|
// Ensure that puller settings are sane
|
2016-03-16 04:16:38 -07:00
|
|
|
// Ensure that the versioning configuration parameter map is not nil
|
2014-09-28 04:00:38 -07:00
|
|
|
for i := range cfg.Folders {
|
|
|
|
cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
|
|
|
|
cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
|
2015-12-31 01:17:17 -07:00
|
|
|
cfg.Folders[i].Devices = ensureNoDuplicateFolderDevices(cfg.Folders[i].Devices)
|
2016-03-16 04:16:38 -07:00
|
|
|
if cfg.Folders[i].Versioning.Params == nil {
|
|
|
|
cfg.Folders[i].Versioning.Params = map[string]string{}
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
sort.Sort(FolderDeviceConfigurationList(cfg.Folders[i].Devices))
|
2014-04-22 02:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// An empty address list is equivalent to a single "dynamic" entry
|
2014-09-28 04:00:38 -07:00
|
|
|
for i := range cfg.Devices {
|
|
|
|
n := &cfg.Devices[i]
|
2014-04-22 02:46:08 -07:00
|
|
|
if len(n.Addresses) == 0 || len(n.Addresses) == 1 && n.Addresses[0] == "" {
|
|
|
|
n.Addresses = []string{"dynamic"}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 15:57:21 -07:00
|
|
|
|
2015-03-26 05:57:27 -07:00
|
|
|
// Very short reconnection intervals are annoying
|
|
|
|
if cfg.Options.ReconnectIntervalS < 5 {
|
|
|
|
cfg.Options.ReconnectIntervalS = 5
|
|
|
|
}
|
|
|
|
|
2016-02-02 03:12:25 -07:00
|
|
|
if cfg.GUI.APIKey == "" {
|
|
|
|
cfg.GUI.APIKey = randomString(32)
|
2014-12-29 05:48:26 -07:00
|
|
|
}
|
2014-09-06 05:11:18 -07:00
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
func convertV11V12(cfg *Configuration) {
|
|
|
|
// Change listen address schema
|
|
|
|
for i, addr := range cfg.Options.ListenAddress {
|
|
|
|
if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") {
|
2015-10-30 13:22:40 -07:00
|
|
|
cfg.Options.ListenAddress[i] = tcpAddr(addr)
|
2015-06-23 05:55:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, device := range cfg.Devices {
|
|
|
|
for j, addr := range device.Addresses {
|
|
|
|
if addr != "dynamic" && addr != "" {
|
2015-10-30 13:22:40 -07:00
|
|
|
cfg.Devices[i].Addresses[j] = tcpAddr(addr)
|
2015-06-23 05:55:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use new discovery server
|
2015-09-20 06:30:25 -07:00
|
|
|
var newDiscoServers []string
|
|
|
|
var useDefault bool
|
|
|
|
for _, addr := range cfg.Options.GlobalAnnServers {
|
2015-06-23 05:55:30 -07:00
|
|
|
if addr == "udp4://announce.syncthing.net:22026" {
|
2015-09-20 06:30:25 -07:00
|
|
|
useDefault = true
|
2015-06-23 05:55:30 -07:00
|
|
|
} else if addr == "udp6://announce-v6.syncthing.net:22026" {
|
2015-09-20 06:30:25 -07:00
|
|
|
useDefault = true
|
|
|
|
} else {
|
|
|
|
newDiscoServers = append(newDiscoServers, addr)
|
2015-06-23 05:55:30 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
if useDefault {
|
|
|
|
newDiscoServers = append(newDiscoServers, "default")
|
|
|
|
}
|
|
|
|
cfg.Options.GlobalAnnServers = newDiscoServers
|
2015-06-23 05:55:30 -07:00
|
|
|
|
2015-08-23 05:59:38 -07:00
|
|
|
// Use new multicast group
|
|
|
|
if cfg.Options.LocalAnnMCAddr == "[ff32::5222]:21026" {
|
|
|
|
cfg.Options.LocalAnnMCAddr = "[ff12::8384]:21027"
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:04:13 -07:00
|
|
|
// Use new local discovery port
|
|
|
|
if cfg.Options.LocalAnnPort == 21025 {
|
|
|
|
cfg.Options.LocalAnnPort = 21027
|
|
|
|
}
|
|
|
|
|
2015-10-13 12:50:58 -07:00
|
|
|
// Set MaxConflicts to unlimited
|
|
|
|
for i := range cfg.Folders {
|
|
|
|
cfg.Folders[i].MaxConflicts = -1
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
cfg.Version = 12
|
|
|
|
}
|
|
|
|
|
2015-10-13 12:50:58 -07:00
|
|
|
func convertV10V11(cfg *Configuration) {
|
|
|
|
// Set minimum disk free of existing folders to 1%
|
|
|
|
for i := range cfg.Folders {
|
|
|
|
cfg.Folders[i].MinDiskFreePct = 1
|
|
|
|
}
|
|
|
|
cfg.Version = 11
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
func setDefaults(data interface{}) error {
|
|
|
|
s := reflect.ValueOf(data).Elem()
|
|
|
|
t := s.Type()
|
2014-04-08 04:45:18 -07:00
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
f := s.Field(i)
|
|
|
|
tag := t.Field(i).Tag
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
v := tag.Get("default")
|
|
|
|
if len(v) > 0 {
|
|
|
|
switch f.Interface().(type) {
|
|
|
|
case string:
|
|
|
|
f.SetString(v)
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
case int:
|
|
|
|
i, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetInt(i)
|
2014-08-16 15:20:21 -07:00
|
|
|
|
2015-09-05 00:43:07 -07:00
|
|
|
case float64:
|
|
|
|
i, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetFloat(i)
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
case bool:
|
|
|
|
f.SetBool(v == "true")
|
|
|
|
|
|
|
|
case []string:
|
|
|
|
// We don't do anything with string slices here. Any default
|
|
|
|
// we set will be appended to by the XML decoder, so we fill
|
|
|
|
// those after decoding.
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(f.Type())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-08-16 15:20:21 -07:00
|
|
|
}
|
2014-10-06 00:25:45 -07:00
|
|
|
|
|
|
|
// fillNilSlices sets default value on slices that are still nil.
|
|
|
|
func fillNilSlices(data interface{}) error {
|
|
|
|
s := reflect.ValueOf(data).Elem()
|
|
|
|
t := s.Type()
|
|
|
|
|
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
f := s.Field(i)
|
|
|
|
tag := t.Field(i).Tag
|
|
|
|
|
|
|
|
v := tag.Get("default")
|
|
|
|
if len(v) > 0 {
|
|
|
|
switch f.Interface().(type) {
|
|
|
|
case []string:
|
|
|
|
if f.IsNil() {
|
2015-03-07 12:48:53 -07:00
|
|
|
// Treat the default as a comma separated slice
|
|
|
|
vs := strings.Split(v, ",")
|
|
|
|
for i := range vs {
|
|
|
|
vs[i] = strings.TrimSpace(vs[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
|
|
|
|
for i, v := range vs {
|
|
|
|
rv.Index(i).SetString(v)
|
|
|
|
}
|
2014-10-06 00:25:45 -07:00
|
|
|
f.Set(rv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-08-16 15:20:21 -07:00
|
|
|
}
|
2014-10-06 00:25:45 -07:00
|
|
|
|
|
|
|
func uniqueStrings(ss []string) []string {
|
|
|
|
var m = make(map[string]bool, len(ss))
|
|
|
|
for _, s := range ss {
|
2015-06-25 08:48:41 -07:00
|
|
|
m[strings.Trim(s, " ")] = true
|
2014-10-06 00:25:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var us = make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
us = append(us, k)
|
|
|
|
}
|
|
|
|
|
2015-03-07 13:05:30 -07:00
|
|
|
sort.Strings(us)
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
return us
|
2014-08-16 15:20:21 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
|
|
|
|
for _, device := range devices {
|
|
|
|
if device.DeviceID.Equals(myID) {
|
|
|
|
return devices
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
devices = append(devices, FolderDeviceConfiguration{
|
|
|
|
DeviceID: myID,
|
2014-07-27 16:20:36 -07:00
|
|
|
})
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
2014-07-22 14:27:00 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
|
|
|
|
count := len(devices)
|
2014-07-22 14:27:00 -07:00
|
|
|
i := 0
|
2014-07-27 16:08:15 -07:00
|
|
|
loop:
|
|
|
|
for i < count {
|
2014-09-28 04:00:38 -07:00
|
|
|
if _, ok := existingDevices[devices[i].DeviceID]; !ok {
|
|
|
|
devices[i] = devices[count-1]
|
2014-07-27 16:08:15 -07:00
|
|
|
count--
|
|
|
|
continue loop
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
2014-07-27 16:08:15 -07:00
|
|
|
i++
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices[0:count]
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
2014-07-27 16:15:16 -07:00
|
|
|
|
2015-12-31 01:17:17 -07:00
|
|
|
func ensureNoDuplicateFolderDevices(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
|
|
|
|
count := len(devices)
|
|
|
|
i := 0
|
|
|
|
seenDevices := make(map[protocol.DeviceID]bool)
|
|
|
|
loop:
|
|
|
|
for i < count {
|
|
|
|
id := devices[i].DeviceID
|
|
|
|
if _, ok := seenDevices[id]; ok {
|
|
|
|
devices[i] = devices[count-1]
|
|
|
|
count--
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
seenDevices[id] = true
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return devices[0:count]
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureNoDuplicateDevices(devices []DeviceConfiguration) []DeviceConfiguration {
|
2014-09-28 04:00:38 -07:00
|
|
|
count := len(devices)
|
2014-07-27 16:15:16 -07:00
|
|
|
i := 0
|
2014-09-28 04:00:38 -07:00
|
|
|
seenDevices := make(map[protocol.DeviceID]bool)
|
2014-07-27 16:15:16 -07:00
|
|
|
loop:
|
|
|
|
for i < count {
|
2014-09-28 04:00:38 -07:00
|
|
|
id := devices[i].DeviceID
|
|
|
|
if _, ok := seenDevices[id]; ok {
|
|
|
|
devices[i] = devices[count-1]
|
2014-07-27 16:15:16 -07:00
|
|
|
count--
|
|
|
|
continue loop
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
seenDevices[id] = true
|
2014-07-27 16:15:16 -07:00
|
|
|
i++
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices[0:count]
|
2014-07-27 16:15:16 -07:00
|
|
|
}
|
2014-10-06 00:25:45 -07:00
|
|
|
|
2014-12-29 05:48:26 -07:00
|
|
|
// randomCharset contains the characters that can make up a randomString().
|
|
|
|
const randomCharset = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
|
|
|
|
|
|
|
|
// randomString returns a string of random characters (taken from
|
|
|
|
// randomCharset) of the specified length.
|
|
|
|
func randomString(l int) string {
|
|
|
|
bs := make([]byte, l)
|
|
|
|
for i := range bs {
|
|
|
|
bs[i] = randomCharset[rand.Intn(len(randomCharset))]
|
|
|
|
}
|
|
|
|
return string(bs)
|
|
|
|
}
|
2015-10-30 13:22:40 -07:00
|
|
|
|
|
|
|
func tcpAddr(host string) string {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "tcp",
|
|
|
|
Host: host,
|
|
|
|
}
|
|
|
|
return u.String()
|
|
|
|
}
|