2014-11-16 21:13:20 +01:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 21:43:32 +02:00
|
|
|
//
|
2015-03-07 21:36:35 +01: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,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 22:50:14 +02:00
|
|
|
|
2014-05-15 05:40:17 +02:00
|
|
|
// Package config implements reading and writing of the syncthing configuration file.
|
2014-05-15 02:18:09 +02:00
|
|
|
package config
|
2014-03-02 23:58:14 +01:00
|
|
|
|
|
|
|
import (
|
2015-11-04 21:30:47 +01:00
|
|
|
"encoding/json"
|
2014-03-02 23:58:14 +01:00
|
|
|
"encoding/xml"
|
2016-07-02 21:38:39 +02:00
|
|
|
"fmt"
|
2014-10-06 09:25:45 +02:00
|
|
|
"io"
|
2016-05-07 00:01:56 +02:00
|
|
|
"io/ioutil"
|
2017-03-07 13:44:16 +01:00
|
|
|
"net"
|
2016-05-04 21:38:12 +02:00
|
|
|
"net/url"
|
2014-04-22 11:46:08 +02:00
|
|
|
"os"
|
2017-08-08 15:13:08 +02:00
|
|
|
"runtime"
|
2014-03-02 23:58:14 +01:00
|
|
|
"sort"
|
2017-03-07 13:44:16 +01:00
|
|
|
"strconv"
|
2015-03-07 20:48:53 +01:00
|
|
|
"strings"
|
2014-04-19 13:33:51 +02:00
|
|
|
|
2019-11-23 16:20:54 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-09-22 19:38:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2016-05-26 09:02:56 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2016-03-25 21:22:29 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2014-03-02 23:58:14 +01:00
|
|
|
)
|
|
|
|
|
2015-04-04 21:49:15 +02:00
|
|
|
const (
|
2015-10-27 11:46:33 +01:00
|
|
|
OldestHandledVersion = 10
|
2020-08-19 19:58:51 +02:00
|
|
|
CurrentVersion = 32
|
2015-06-20 20:21:05 +02:00
|
|
|
MaxRescanIntervalS = 365 * 24 * 60 * 60
|
2015-04-04 21:49:15 +02:00
|
|
|
)
|
2014-10-08 13:52:05 +02:00
|
|
|
|
2015-09-20 15:30:25 +02:00
|
|
|
var (
|
2017-03-07 13:44:16 +01:00
|
|
|
// DefaultTCPPort defines default TCP port used if the URI does not specify one, for example tcp://0.0.0.0
|
|
|
|
DefaultTCPPort = 22000
|
2019-05-29 09:56:40 +02:00
|
|
|
// DefaultQUICPort defines default QUIC port used if the URI does not specify one, for example quic://0.0.0.0
|
|
|
|
DefaultQUICPort = 22000
|
2016-05-04 21:38:12 +02:00
|
|
|
// DefaultListenAddresses should be substituted when the configuration
|
2016-05-17 02:05:38 +02:00
|
|
|
// contains <listenAddress>default</listenAddress>. This is done by the
|
|
|
|
// "consumer" of the configuration as we don't want these saved to the
|
|
|
|
// config.
|
2016-05-04 21:38:12 +02:00
|
|
|
DefaultListenAddresses = []string{
|
2017-03-07 13:44:16 +01:00
|
|
|
util.Address("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultTCPPort))),
|
2016-05-04 21:38:12 +02:00
|
|
|
"dynamic+https://relays.syncthing.net/endpoint",
|
2019-05-29 09:56:40 +02:00
|
|
|
util.Address("quic", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultQUICPort))),
|
2016-05-04 21:38:12 +02:00
|
|
|
}
|
2019-02-02 12:43:57 +01:00
|
|
|
DefaultGUIPort = 8384
|
2016-05-04 21:38:12 +02:00
|
|
|
// DefaultDiscoveryServersV4 should be substituted when the configuration
|
|
|
|
// contains <globalAnnounceServer>default-v4</globalAnnounceServer>.
|
2015-11-09 15:35:14 +01:00
|
|
|
DefaultDiscoveryServersV4 = []string{
|
2018-01-05 15:18:32 +01:00
|
|
|
"https://discovery.syncthing.net/v2/?noannounce&id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW",
|
|
|
|
"https://discovery-v4.syncthing.net/v2/?nolookup&id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW",
|
2015-11-09 15:35:14 +01:00
|
|
|
}
|
|
|
|
// DefaultDiscoveryServersV6 should be substituted when the configuration
|
|
|
|
// contains <globalAnnounceServer>default-v6</globalAnnounceServer>.
|
|
|
|
DefaultDiscoveryServersV6 = []string{
|
2018-01-05 15:18:32 +01:00
|
|
|
"https://discovery.syncthing.net/v2/?noannounce&id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW",
|
|
|
|
"https://discovery-v6.syncthing.net/v2/?nolookup&id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW",
|
2015-09-20 15:30:25 +02:00
|
|
|
}
|
2015-11-09 15:35:14 +01:00
|
|
|
// DefaultDiscoveryServers should be substituted when the configuration
|
|
|
|
// contains <globalAnnounceServer>default</globalAnnounceServer>.
|
|
|
|
DefaultDiscoveryServers = append(DefaultDiscoveryServersV4, DefaultDiscoveryServersV6...)
|
2016-01-10 16:37:31 +01:00
|
|
|
// DefaultTheme is the default and fallback theme for the web UI.
|
|
|
|
DefaultTheme = "default"
|
2019-05-29 09:56:40 +02:00
|
|
|
// Default stun servers should be substituted when the configuration
|
|
|
|
// contains <stunServer>default</stunServer>.
|
|
|
|
|
|
|
|
// DefaultPrimaryStunServers are servers provided by us (to avoid causing the public servers burden)
|
|
|
|
DefaultPrimaryStunServers = []string{
|
|
|
|
"stun.syncthing.net:3478",
|
|
|
|
}
|
|
|
|
DefaultSecondaryStunServers = []string{
|
|
|
|
"stun.callwithus.com:3478",
|
|
|
|
"stun.counterpath.com:3478",
|
|
|
|
"stun.counterpath.net:3478",
|
|
|
|
"stun.ekiga.net:3478",
|
|
|
|
"stun.ideasip.com:3478",
|
|
|
|
"stun.internetcalls.com:3478",
|
|
|
|
"stun.schlund.de:3478",
|
|
|
|
"stun.sipgate.net:10000",
|
|
|
|
"stun.sipgate.net:3478",
|
|
|
|
"stun.voip.aebc.com:3478",
|
|
|
|
"stun.voiparound.com:3478",
|
|
|
|
"stun.voipbuster.com:3478",
|
|
|
|
"stun.voipstunt.com:3478",
|
|
|
|
"stun.xten.com:3478",
|
|
|
|
}
|
2015-09-20 15:30:25 +02:00
|
|
|
)
|
|
|
|
|
2019-07-14 11:03:14 +02:00
|
|
|
var (
|
|
|
|
errFolderIDEmpty = errors.New("folder has empty ID")
|
|
|
|
errFolderIDDuplicate = errors.New("folder has duplicate ID")
|
|
|
|
errFolderPathEmpty = errors.New("folder has empty path")
|
|
|
|
)
|
|
|
|
|
2015-10-27 12:04:51 +01:00
|
|
|
func New(myID protocol.DeviceID) Configuration {
|
|
|
|
var cfg Configuration
|
|
|
|
cfg.Version = CurrentVersion
|
|
|
|
|
2020-06-02 12:08:22 +02:00
|
|
|
cfg.Options.UnackedNotificationIDs = []string{"authenticationUserAndPassword"}
|
|
|
|
|
2019-02-02 12:16:27 +01:00
|
|
|
util.SetDefaults(&cfg)
|
|
|
|
util.SetDefaults(&cfg.Options)
|
|
|
|
util.SetDefaults(&cfg.GUI)
|
2015-10-27 12:04:51 +01:00
|
|
|
|
2016-08-07 18:21:59 +02:00
|
|
|
// Can't happen.
|
|
|
|
if err := cfg.prepare(myID); err != nil {
|
2019-07-31 10:53:35 +02:00
|
|
|
l.Warnln("bug: error in preparing new folder:", err)
|
|
|
|
panic("error in preparing new folder")
|
2016-08-07 18:21:59 +02:00
|
|
|
}
|
2015-10-27 12:04:51 +01:00
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:29:14 +01:00
|
|
|
func NewWithFreePorts(myID protocol.DeviceID) (Configuration, error) {
|
2019-02-02 12:43:57 +01:00
|
|
|
cfg := New(myID)
|
|
|
|
|
|
|
|
port, err := getFreePort("127.0.0.1", DefaultGUIPort)
|
|
|
|
if err != nil {
|
2019-11-23 16:20:54 +01:00
|
|
|
return Configuration{}, errors.Wrap(err, "get free port (GUI)")
|
2019-02-02 12:43:57 +01:00
|
|
|
}
|
|
|
|
cfg.GUI.RawAddress = fmt.Sprintf("127.0.0.1:%d", port)
|
|
|
|
|
|
|
|
port, err = getFreePort("0.0.0.0", DefaultTCPPort)
|
|
|
|
if err != nil {
|
2019-11-23 16:20:54 +01:00
|
|
|
return Configuration{}, errors.Wrap(err, "get free port (BEP)")
|
2019-02-02 12:43:57 +01:00
|
|
|
}
|
|
|
|
if port == DefaultTCPPort {
|
2019-11-26 17:07:25 +01:00
|
|
|
cfg.Options.RawListenAddresses = []string{"default"}
|
2019-02-02 12:43:57 +01:00
|
|
|
} else {
|
2019-11-26 17:07:25 +01:00
|
|
|
cfg.Options.RawListenAddresses = []string{
|
2020-06-02 16:06:02 +02:00
|
|
|
util.Address("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(port))),
|
2019-02-02 12:43:57 +01:00
|
|
|
"dynamic+https://relays.syncthing.net/endpoint",
|
2020-06-02 16:06:02 +02:00
|
|
|
util.Address("quic", net.JoinHostPort("0.0.0.0", strconv.Itoa(port))),
|
2019-02-02 12:43:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:29:14 +01:00
|
|
|
return cfg, nil
|
2019-02-02 12:43:57 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 08:11:14 +02:00
|
|
|
type xmlConfiguration struct {
|
|
|
|
Configuration
|
|
|
|
XMLName xml.Name `xml:"configuration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, int, error) {
|
|
|
|
var cfg xmlConfiguration
|
2015-10-27 12:04:51 +01:00
|
|
|
|
2019-02-02 12:16:27 +01:00
|
|
|
util.SetDefaults(&cfg)
|
|
|
|
util.SetDefaults(&cfg.Options)
|
|
|
|
util.SetDefaults(&cfg.GUI)
|
2015-10-27 12:04:51 +01:00
|
|
|
|
2016-07-02 21:38:39 +02:00
|
|
|
if err := xml.NewDecoder(r).Decode(&cfg); err != nil {
|
2020-08-25 08:11:14 +02:00
|
|
|
return Configuration{}, 0, err
|
2016-07-02 21:38:39 +02:00
|
|
|
}
|
2020-08-25 08:11:14 +02:00
|
|
|
|
|
|
|
originalVersion := cfg.Version
|
2015-10-27 12:04:51 +01:00
|
|
|
|
2016-07-02 21:38:39 +02:00
|
|
|
if err := cfg.prepare(myID); err != nil {
|
2020-08-25 08:11:14 +02:00
|
|
|
return Configuration{}, originalVersion, err
|
2016-07-02 21:38:39 +02:00
|
|
|
}
|
2020-08-25 08:11:14 +02:00
|
|
|
return cfg.Configuration, originalVersion, nil
|
2015-10-27 12:04:51 +01:00
|
|
|
}
|
|
|
|
|
2015-11-04 21:30:47 +01:00
|
|
|
func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
|
|
|
|
var cfg Configuration
|
|
|
|
|
2019-02-02 12:16:27 +01:00
|
|
|
util.SetDefaults(&cfg)
|
|
|
|
util.SetDefaults(&cfg.Options)
|
|
|
|
util.SetDefaults(&cfg.GUI)
|
2015-11-04 21:30:47 +01:00
|
|
|
|
2016-05-07 00:01:56 +02:00
|
|
|
bs, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
2016-07-02 21:38:39 +02:00
|
|
|
return Configuration{}, err
|
2016-05-07 00:01:56 +02:00
|
|
|
}
|
|
|
|
|
2016-07-15 16:23:20 +02:00
|
|
|
if err := json.Unmarshal(bs, &cfg); err != nil {
|
|
|
|
return Configuration{}, err
|
|
|
|
}
|
2015-11-04 21:30:47 +01:00
|
|
|
|
2016-07-02 21:38:39 +02:00
|
|
|
if err := cfg.prepare(myID); err != nil {
|
|
|
|
return Configuration{}, err
|
|
|
|
}
|
|
|
|
return cfg, nil
|
2015-11-04 21:30:47 +01:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:32:10 +02:00
|
|
|
func (cfg Configuration) Copy() Configuration {
|
|
|
|
newCfg := cfg
|
2015-04-05 17:36:52 +02:00
|
|
|
|
|
|
|
// Deep copy FolderConfigurations
|
2015-04-28 22:32:10 +02:00
|
|
|
newCfg.Folders = make([]FolderConfiguration, len(cfg.Folders))
|
|
|
|
for i := range newCfg.Folders {
|
|
|
|
newCfg.Folders[i] = cfg.Folders[i].Copy()
|
2015-04-05 17:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deep copy DeviceConfigurations
|
2015-04-28 22:32:10 +02:00
|
|
|
newCfg.Devices = make([]DeviceConfiguration, len(cfg.Devices))
|
|
|
|
for i := range newCfg.Devices {
|
|
|
|
newCfg.Devices[i] = cfg.Devices[i].Copy()
|
2015-04-05 17:36:52 +02:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:32:10 +02:00
|
|
|
newCfg.Options = cfg.Options.Copy()
|
2018-07-26 23:14:12 +02:00
|
|
|
newCfg.GUI = cfg.GUI.Copy()
|
2015-04-05 17:36:52 +02:00
|
|
|
|
|
|
|
// DeviceIDs are values
|
2018-08-25 12:36:10 +02:00
|
|
|
newCfg.IgnoredDevices = make([]ObservedDevice, len(cfg.IgnoredDevices))
|
2015-04-28 22:32:10 +02:00
|
|
|
copy(newCfg.IgnoredDevices, cfg.IgnoredDevices)
|
2015-04-05 17:36:52 +02:00
|
|
|
|
2018-08-25 12:36:10 +02:00
|
|
|
newCfg.PendingDevices = make([]ObservedDevice, len(cfg.PendingDevices))
|
|
|
|
copy(newCfg.PendingDevices, cfg.PendingDevices)
|
2017-05-31 20:04:00 +02:00
|
|
|
|
2015-04-28 22:32:10 +02:00
|
|
|
return newCfg
|
2015-04-05 17:36:52 +02:00
|
|
|
}
|
|
|
|
|
2014-10-06 09:25:45 +02:00
|
|
|
func (cfg *Configuration) WriteXML(w io.Writer) error {
|
|
|
|
e := xml.NewEncoder(w)
|
|
|
|
e.Indent("", " ")
|
2020-08-25 08:11:14 +02:00
|
|
|
xmlCfg := xmlConfiguration{Configuration: *cfg}
|
|
|
|
err := e.Encode(xmlCfg)
|
2014-10-06 09:25:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte("\n"))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-02 21:38:39 +02:00
|
|
|
func (cfg *Configuration) prepare(myID protocol.DeviceID) error {
|
2016-08-07 18:21:59 +02:00
|
|
|
var myName string
|
|
|
|
|
|
|
|
// Ensure this device is present in the config
|
|
|
|
for _, device := range cfg.Devices {
|
|
|
|
if device.DeviceID == myID {
|
|
|
|
goto found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myName, _ = os.Hostname()
|
|
|
|
cfg.Devices = append(cfg.Devices, DeviceConfiguration{
|
|
|
|
DeviceID: myID,
|
|
|
|
Name: myName,
|
|
|
|
})
|
|
|
|
|
|
|
|
found:
|
|
|
|
|
|
|
|
if err := cfg.clean(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we are part of the devices
|
|
|
|
for i := range cfg.Folders {
|
|
|
|
cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Configuration) clean() error {
|
2019-02-02 12:16:27 +01:00
|
|
|
util.FillNilSlices(&cfg.Options)
|
2014-03-04 11:25:10 +01:00
|
|
|
|
2019-07-17 07:19:14 +02:00
|
|
|
// Ensure that the device list is
|
|
|
|
// - free from duplicates
|
|
|
|
// - no devices with empty ID
|
|
|
|
// - sorted by ID
|
|
|
|
// Happen before preparting folders as that needs a correct device list.
|
|
|
|
cfg.Devices = ensureNoDuplicateOrEmptyIDDevices(cfg.Devices)
|
|
|
|
sort.Slice(cfg.Devices, func(a, b int) bool {
|
|
|
|
return cfg.Devices[a].DeviceID.Compare(cfg.Devices[b].DeviceID) == -1
|
|
|
|
})
|
|
|
|
|
2016-07-02 21:38:39 +02:00
|
|
|
// Prepare folders and check for duplicates. Duplicates are bad and
|
|
|
|
// dangerous, can't currently be resolved in the GUI, and shouldn't
|
|
|
|
// happen when configured by the GUI. We return with an error in that
|
|
|
|
// situation.
|
2018-08-25 12:36:10 +02:00
|
|
|
existingFolders := make(map[string]*FolderConfiguration)
|
2014-09-28 13:00:38 +02:00
|
|
|
for i := range cfg.Folders {
|
|
|
|
folder := &cfg.Folders[i]
|
2015-11-05 09:01:47 +01:00
|
|
|
folder.prepare()
|
2015-06-20 20:21:05 +02:00
|
|
|
|
2018-01-27 10:06:37 +01:00
|
|
|
if folder.ID == "" {
|
2019-07-14 11:03:14 +02:00
|
|
|
return errFolderIDEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
if folder.Path == "" {
|
2020-03-03 22:40:00 +01:00
|
|
|
return fmt.Errorf("folder %q: %w", folder.ID, errFolderPathEmpty)
|
2018-01-27 10:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-08-25 12:36:10 +02:00
|
|
|
if _, ok := existingFolders[folder.ID]; ok {
|
2020-03-03 22:40:00 +01:00
|
|
|
return fmt.Errorf("folder %q: %w", folder.ID, errFolderIDDuplicate)
|
2014-03-29 18:53:48 +01:00
|
|
|
}
|
2019-07-14 11:03:14 +02:00
|
|
|
|
2018-08-25 12:36:10 +02:00
|
|
|
existingFolders[folder.ID] = folder
|
2017-06-17 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:07:25 +01:00
|
|
|
cfg.Options.RawListenAddresses = util.UniqueTrimmedStrings(cfg.Options.RawListenAddresses)
|
|
|
|
cfg.Options.RawGlobalAnnServers = util.UniqueTrimmedStrings(cfg.Options.RawGlobalAnnServers)
|
2015-06-23 14:55:30 +02:00
|
|
|
|
2015-11-05 09:31:36 +01:00
|
|
|
if cfg.Version > 0 && cfg.Version < OldestHandledVersion {
|
2015-04-04 21:49:15 +02:00
|
|
|
l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
|
2014-06-17 23:22:19 +02:00
|
|
|
}
|
|
|
|
|
2015-03-07 20:48:53 +01:00
|
|
|
// Upgrade configuration versions as appropriate
|
2020-10-22 19:54:35 +02:00
|
|
|
migrationsMut.Lock()
|
2019-05-29 11:37:44 +02:00
|
|
|
migrations.apply(cfg)
|
2020-10-22 19:54:35 +02:00
|
|
|
migrationsMut.Unlock()
|
2014-11-20 22:24:11 +01:00
|
|
|
|
2014-09-28 13:00:38 +02: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 23:27:00 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 18:01:52 +02:00
|
|
|
// Ensure that the folder list is sorted by ID
|
2018-08-25 12:36:10 +02:00
|
|
|
sort.Slice(cfg.Folders, func(a, b int) bool {
|
|
|
|
return cfg.Folders[a].ID < cfg.Folders[b].ID
|
|
|
|
})
|
|
|
|
|
2018-04-22 18:01:52 +02:00
|
|
|
// Ensure that in all folder configs
|
|
|
|
// - any loose devices are not present in the wrong places
|
|
|
|
// - there are no duplicate devices
|
|
|
|
// - the versioning configuration parameter map is not nil
|
2018-08-25 12:36:10 +02:00
|
|
|
sharedFolders := make(map[protocol.DeviceID][]string, len(cfg.Devices))
|
2014-09-28 13:00:38 +02:00
|
|
|
for i := range cfg.Folders {
|
|
|
|
cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
|
2015-12-31 09:17:17 +01:00
|
|
|
cfg.Folders[i].Devices = ensureNoDuplicateFolderDevices(cfg.Folders[i].Devices)
|
2016-03-16 12:16:38 +01:00
|
|
|
if cfg.Folders[i].Versioning.Params == nil {
|
|
|
|
cfg.Folders[i].Versioning.Params = map[string]string{}
|
|
|
|
}
|
2018-08-25 12:36:10 +02:00
|
|
|
sort.Slice(cfg.Folders[i].Devices, func(a, b int) bool {
|
|
|
|
return cfg.Folders[i].Devices[a].DeviceID.Compare(cfg.Folders[i].Devices[b].DeviceID) == -1
|
|
|
|
})
|
|
|
|
for _, dev := range cfg.Folders[i].Devices {
|
|
|
|
sharedFolders[dev.DeviceID] = append(sharedFolders[dev.DeviceID], cfg.Folders[i].ID)
|
|
|
|
}
|
2014-04-22 11:46:08 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
for i := range cfg.Devices {
|
2018-08-25 12:36:10 +02:00
|
|
|
cfg.Devices[i].prepare(sharedFolders[cfg.Devices[i].DeviceID])
|
2014-04-22 11:46:08 +02:00
|
|
|
}
|
2014-11-18 23:57:21 +01:00
|
|
|
|
2015-03-26 13:57:27 +01:00
|
|
|
// Very short reconnection intervals are annoying
|
|
|
|
if cfg.Options.ReconnectIntervalS < 5 {
|
|
|
|
cfg.Options.ReconnectIntervalS = 5
|
|
|
|
}
|
|
|
|
|
2016-02-02 11:12:25 +01:00
|
|
|
if cfg.GUI.APIKey == "" {
|
2016-05-26 09:02:56 +02:00
|
|
|
cfg.GUI.APIKey = rand.String(32)
|
2014-12-29 13:48:26 +01:00
|
|
|
}
|
2016-07-02 21:38:39 +02:00
|
|
|
|
2016-08-05 11:29:49 +02:00
|
|
|
// The list of ignored devices should not contain any devices that have
|
|
|
|
// been manually added to the config.
|
2018-08-25 12:36:10 +02:00
|
|
|
var newIgnoredDevices []ObservedDevice
|
|
|
|
ignoredDevices := make(map[protocol.DeviceID]bool)
|
2016-08-05 11:29:49 +02:00
|
|
|
for _, dev := range cfg.IgnoredDevices {
|
2018-08-25 12:36:10 +02:00
|
|
|
if !existingDevices[dev.ID] {
|
|
|
|
ignoredDevices[dev.ID] = true
|
2016-08-05 11:29:49 +02:00
|
|
|
newIgnoredDevices = append(newIgnoredDevices, dev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.IgnoredDevices = newIgnoredDevices
|
|
|
|
|
2018-08-25 12:36:10 +02:00
|
|
|
// The list of pending devices should not contain devices that were added manually, nor should it contain
|
|
|
|
// ignored devices.
|
|
|
|
|
|
|
|
// Sort by time, so that in case of duplicates latest "time" is used.
|
|
|
|
sort.Slice(cfg.PendingDevices, func(i, j int) bool {
|
|
|
|
return cfg.PendingDevices[i].Time.Before(cfg.PendingDevices[j].Time)
|
|
|
|
})
|
|
|
|
|
|
|
|
var newPendingDevices []ObservedDevice
|
|
|
|
nextPendingDevice:
|
|
|
|
for _, pendingDevice := range cfg.PendingDevices {
|
|
|
|
if !existingDevices[pendingDevice.ID] && !ignoredDevices[pendingDevice.ID] {
|
|
|
|
// Deduplicate
|
|
|
|
for _, existingPendingDevice := range newPendingDevices {
|
|
|
|
if existingPendingDevice.ID == pendingDevice.ID {
|
|
|
|
continue nextPendingDevice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newPendingDevices = append(newPendingDevices, pendingDevice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.PendingDevices = newPendingDevices
|
|
|
|
|
2018-02-09 11:40:57 +01:00
|
|
|
// Deprecated protocols are removed from the list of listeners and
|
|
|
|
// device addresses. So far just kcp*.
|
|
|
|
for _, prefix := range []string{"kcp"} {
|
2019-11-26 17:07:25 +01:00
|
|
|
cfg.Options.RawListenAddresses = filterURLSchemePrefix(cfg.Options.RawListenAddresses, prefix)
|
2018-02-09 11:40:57 +01:00
|
|
|
for i := range cfg.Devices {
|
|
|
|
dev := &cfg.Devices[i]
|
|
|
|
dev.Addresses = filterURLSchemePrefix(dev.Addresses, prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 12:36:10 +02:00
|
|
|
// Initialize any empty slices
|
|
|
|
if cfg.Folders == nil {
|
|
|
|
cfg.Folders = []FolderConfiguration{}
|
|
|
|
}
|
|
|
|
if cfg.IgnoredDevices == nil {
|
|
|
|
cfg.IgnoredDevices = []ObservedDevice{}
|
|
|
|
}
|
|
|
|
if cfg.PendingDevices == nil {
|
|
|
|
cfg.PendingDevices = []ObservedDevice{}
|
|
|
|
}
|
|
|
|
if cfg.Options.AlwaysLocalNets == nil {
|
|
|
|
cfg.Options.AlwaysLocalNets = []string{}
|
|
|
|
}
|
|
|
|
if cfg.Options.UnackedNotificationIDs == nil {
|
|
|
|
cfg.Options.UnackedNotificationIDs = []string{}
|
2020-06-02 12:08:22 +02:00
|
|
|
} else if cfg.GUI.User != "" && cfg.GUI.Password != "" {
|
|
|
|
for i, key := range cfg.Options.UnackedNotificationIDs {
|
|
|
|
if key == "authenticationUserAndPassword" {
|
|
|
|
cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs[:i], cfg.Options.UnackedNotificationIDs[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-08-25 12:36:10 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 21:38:39 +02:00
|
|
|
return nil
|
2014-09-06 14:11:18 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 12:56:50 +02:00
|
|
|
// DeviceMap returns a map of device ID to device configuration for the given configuration.
|
|
|
|
func (cfg *Configuration) DeviceMap() map[protocol.DeviceID]DeviceConfiguration {
|
|
|
|
m := make(map[protocol.DeviceID]DeviceConfiguration, len(cfg.Devices))
|
|
|
|
for _, dev := range cfg.Devices {
|
|
|
|
m[dev.DeviceID] = dev
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
|
|
|
|
for _, device := range devices {
|
|
|
|
if device.DeviceID.Equals(myID) {
|
|
|
|
return devices
|
2014-03-02 23:58:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
devices = append(devices, FolderDeviceConfiguration{
|
|
|
|
DeviceID: myID,
|
2014-07-28 01:20:36 +02:00
|
|
|
})
|
2014-03-02 23:58:14 +01:00
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
return devices
|
2014-03-02 23:58:14 +01:00
|
|
|
}
|
2014-07-22 23:27:00 +02:00
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
|
|
|
|
count := len(devices)
|
2014-07-22 23:27:00 +02:00
|
|
|
i := 0
|
2014-07-28 01:08:15 +02:00
|
|
|
loop:
|
|
|
|
for i < count {
|
2014-09-28 13:00:38 +02:00
|
|
|
if _, ok := existingDevices[devices[i].DeviceID]; !ok {
|
|
|
|
devices[i] = devices[count-1]
|
2014-07-28 01:08:15 +02:00
|
|
|
count--
|
|
|
|
continue loop
|
2014-07-22 23:27:00 +02:00
|
|
|
}
|
2014-07-28 01:08:15 +02:00
|
|
|
i++
|
2014-07-22 23:27:00 +02:00
|
|
|
}
|
2014-09-28 13:00:38 +02:00
|
|
|
return devices[0:count]
|
2014-07-22 23:27:00 +02:00
|
|
|
}
|
2014-07-28 01:15:16 +02:00
|
|
|
|
2015-12-31 09:17:17 +01: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]
|
|
|
|
}
|
|
|
|
|
2019-07-17 07:19:14 +02:00
|
|
|
func ensureNoDuplicateOrEmptyIDDevices(devices []DeviceConfiguration) []DeviceConfiguration {
|
2014-09-28 13:00:38 +02:00
|
|
|
count := len(devices)
|
2014-07-28 01:15:16 +02:00
|
|
|
i := 0
|
2014-09-28 13:00:38 +02:00
|
|
|
seenDevices := make(map[protocol.DeviceID]bool)
|
2014-07-28 01:15:16 +02:00
|
|
|
loop:
|
|
|
|
for i < count {
|
2014-09-28 13:00:38 +02:00
|
|
|
id := devices[i].DeviceID
|
2019-07-17 07:19:14 +02:00
|
|
|
if _, ok := seenDevices[id]; ok || id == protocol.EmptyDeviceID {
|
2014-09-28 13:00:38 +02:00
|
|
|
devices[i] = devices[count-1]
|
2014-07-28 01:15:16 +02:00
|
|
|
count--
|
|
|
|
continue loop
|
|
|
|
}
|
2014-09-28 13:00:38 +02:00
|
|
|
seenDevices[id] = true
|
2014-07-28 01:15:16 +02:00
|
|
|
i++
|
|
|
|
}
|
2014-09-28 13:00:38 +02:00
|
|
|
return devices[0:count]
|
2014-07-28 01:15:16 +02:00
|
|
|
}
|
2017-08-08 15:13:08 +02:00
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
func cleanSymlinks(filesystem fs.Filesystem, dir string) {
|
2017-08-08 15:13:08 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// We don't do symlinks on Windows. Additionally, there may
|
|
|
|
// be things that look like symlinks that are not, which we
|
|
|
|
// should leave alone. Deduplicated files, for example.
|
|
|
|
return
|
|
|
|
}
|
2019-02-02 12:16:27 +01:00
|
|
|
filesystem.Walk(dir, func(path string, info fs.FileInfo, err error) error {
|
2017-08-08 15:13:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-19 16:36:56 +02:00
|
|
|
if info.IsSymlink() {
|
2017-08-08 15:13:08 +02:00
|
|
|
l.Infoln("Removing incorrectly versioned symlink", path)
|
2019-02-02 12:16:27 +01:00
|
|
|
filesystem.Remove(path)
|
2017-08-19 16:36:56 +02:00
|
|
|
return fs.SkipDir
|
2017-08-08 15:13:08 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-02-09 11:40:57 +01:00
|
|
|
|
|
|
|
// filterURLSchemePrefix returns the list of addresses after removing all
|
|
|
|
// entries whose URL scheme matches the given prefix.
|
|
|
|
func filterURLSchemePrefix(addrs []string, prefix string) []string {
|
|
|
|
for i := 0; i < len(addrs); i++ {
|
|
|
|
uri, err := url.Parse(addrs[i])
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(uri.Scheme, prefix) {
|
|
|
|
// Remove this entry
|
|
|
|
copy(addrs[i:], addrs[i+1:])
|
|
|
|
addrs = addrs[:len(addrs)-1]
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
2019-02-02 12:43:57 +01:00
|
|
|
|
|
|
|
// tried in succession and the first to succeed is returned. If none succeed,
|
|
|
|
// a random high port is returned.
|
|
|
|
func getFreePort(host string, ports ...int) (int, error) {
|
|
|
|
for _, port := range ports {
|
|
|
|
c, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := net.Listen("tcp", host+":0")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
addr := c.Addr().(*net.TCPAddr)
|
|
|
|
c.Close()
|
|
|
|
return addr.Port, nil
|
|
|
|
}
|