2014-11-16 21:13:20 +01:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-06 09:25:45 +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-10-06 09:25:45 +02:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-07-04 22:32:34 +02:00
|
|
|
"sync/atomic"
|
2018-08-25 12:36:10 +02:00
|
|
|
"time"
|
2014-10-06 09:25:45 +02:00
|
|
|
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 19:38:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2019-05-29 09:56:40 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2016-03-25 21:22:29 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2014-10-06 09:25:45 +02:00
|
|
|
)
|
|
|
|
|
2015-06-03 09:47:39 +02:00
|
|
|
// The Committer interface is implemented by objects that need to know about
|
|
|
|
// or have a say in configuration changes.
|
|
|
|
//
|
|
|
|
// When the configuration is about to be changed, VerifyConfiguration() is
|
|
|
|
// called for each subscribing object, with the old and new configuration. A
|
|
|
|
// nil error is returned if the new configuration is acceptable (i.e. does not
|
|
|
|
// contain any errors that would prevent it from being a valid config).
|
|
|
|
// Otherwise an error describing the problem is returned.
|
|
|
|
//
|
|
|
|
// If any subscriber returns an error from VerifyConfiguration(), the
|
|
|
|
// configuration change is not committed and an error is returned to whoever
|
|
|
|
// tried to commit the broken config.
|
|
|
|
//
|
|
|
|
// If all verification calls returns nil, CommitConfiguration() is called for
|
|
|
|
// each subscribing object. The callee returns true if the new configuration
|
|
|
|
// has been successfully applied, otherwise false. Any Commit() call returning
|
2015-11-12 03:20:34 +01:00
|
|
|
// false will result in a "restart needed" response to the API/user. Note that
|
2015-06-03 09:47:39 +02:00
|
|
|
// the new configuration will still have been applied by those who were
|
|
|
|
// capable of doing so.
|
|
|
|
type Committer interface {
|
|
|
|
VerifyConfiguration(from, to Configuration) error
|
|
|
|
CommitConfiguration(from, to Configuration) (handled bool)
|
|
|
|
String() string
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 08:08:24 +01:00
|
|
|
// Waiter allows to wait for the given config operation to complete.
|
|
|
|
type Waiter interface {
|
|
|
|
Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopWaiter struct{}
|
|
|
|
|
|
|
|
func (noopWaiter) Wait() {}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
// A Wrapper around a Configuration that manages loads, saves and published
|
2014-10-06 09:25:45 +02:00
|
|
|
// notifications of changes to registered Handlers
|
2019-02-26 09:09:25 +01:00
|
|
|
type Wrapper interface {
|
|
|
|
MyName() string
|
|
|
|
ConfigPath() string
|
2014-10-06 09:25:45 +02:00
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
RawCopy() Configuration
|
|
|
|
Replace(cfg Configuration) (Waiter, error)
|
|
|
|
RequiresRestart() bool
|
|
|
|
Save() error
|
|
|
|
|
|
|
|
GUI() GUIConfiguration
|
|
|
|
SetGUI(gui GUIConfiguration) (Waiter, error)
|
|
|
|
LDAP() LDAPConfiguration
|
|
|
|
|
|
|
|
Options() OptionsConfiguration
|
|
|
|
SetOptions(opts OptionsConfiguration) (Waiter, error)
|
|
|
|
|
|
|
|
Folder(id string) (FolderConfiguration, bool)
|
|
|
|
Folders() map[string]FolderConfiguration
|
|
|
|
FolderList() []FolderConfiguration
|
|
|
|
SetFolder(fld FolderConfiguration) (Waiter, error)
|
|
|
|
|
|
|
|
Device(id protocol.DeviceID) (DeviceConfiguration, bool)
|
|
|
|
Devices() map[protocol.DeviceID]DeviceConfiguration
|
|
|
|
RemoveDevice(id protocol.DeviceID) (Waiter, error)
|
|
|
|
SetDevice(DeviceConfiguration) (Waiter, error)
|
|
|
|
SetDevices([]DeviceConfiguration) (Waiter, error)
|
|
|
|
|
|
|
|
AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string)
|
|
|
|
AddOrUpdatePendingFolder(id, label string, device protocol.DeviceID)
|
|
|
|
IgnoredDevice(id protocol.DeviceID) bool
|
|
|
|
IgnoredFolder(device protocol.DeviceID, folder string) bool
|
|
|
|
|
|
|
|
ListenAddresses() []string
|
|
|
|
GlobalDiscoveryServers() []string
|
2019-05-29 09:56:40 +02:00
|
|
|
StunServers() []string
|
2019-02-26 09:09:25 +01:00
|
|
|
|
|
|
|
Subscribe(c Committer)
|
|
|
|
Unsubscribe(c Committer)
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapper struct {
|
2019-08-15 16:29:37 +02:00
|
|
|
cfg Configuration
|
|
|
|
path string
|
|
|
|
evLogger events.Logger
|
2014-10-06 09:25:45 +02:00
|
|
|
|
|
|
|
deviceMap map[protocol.DeviceID]DeviceConfiguration
|
|
|
|
folderMap map[string]FolderConfiguration
|
2015-09-21 10:37:45 +02:00
|
|
|
subs []Committer
|
2014-10-06 09:25:45 +02:00
|
|
|
mut sync.Mutex
|
2016-07-04 22:32:34 +02:00
|
|
|
|
|
|
|
requiresRestart uint32 // an atomic bool
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 09:56:40 +02:00
|
|
|
func (w *wrapper) StunServers() []string {
|
|
|
|
var addresses []string
|
|
|
|
for _, addr := range w.cfg.Options.StunServers {
|
|
|
|
switch addr {
|
|
|
|
case "default":
|
|
|
|
defaultPrimaryAddresses := make([]string, len(DefaultPrimaryStunServers))
|
|
|
|
copy(defaultPrimaryAddresses, DefaultPrimaryStunServers)
|
|
|
|
rand.Shuffle(defaultPrimaryAddresses)
|
|
|
|
addresses = append(addresses, defaultPrimaryAddresses...)
|
|
|
|
|
|
|
|
defaultSecondaryAddresses := make([]string, len(DefaultSecondaryStunServers))
|
|
|
|
copy(defaultSecondaryAddresses, DefaultSecondaryStunServers)
|
|
|
|
rand.Shuffle(defaultSecondaryAddresses)
|
|
|
|
addresses = append(addresses, defaultSecondaryAddresses...)
|
|
|
|
default:
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses = util.UniqueTrimmedStrings(addresses)
|
|
|
|
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
2014-10-06 09:25:45 +02:00
|
|
|
// Wrap wraps an existing Configuration structure and ties it to a file on
|
|
|
|
// disk.
|
2019-08-15 16:29:37 +02:00
|
|
|
func Wrap(path string, cfg Configuration, evLogger events.Logger) Wrapper {
|
2019-02-26 09:09:25 +01:00
|
|
|
w := &wrapper{
|
2019-08-15 16:29:37 +02:00
|
|
|
cfg: cfg,
|
|
|
|
path: path,
|
|
|
|
evLogger: evLogger,
|
|
|
|
mut: sync.NewMutex(),
|
2015-04-23 00:54:31 +02:00
|
|
|
}
|
2014-10-06 09:25:45 +02:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads an existing file on disk and returns a new configuration
|
|
|
|
// wrapper.
|
2019-08-15 16:29:37 +02:00
|
|
|
func Load(path string, myID protocol.DeviceID, evLogger events.Logger) (Wrapper, error) {
|
2014-10-06 09:25:45 +02:00
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
cfg, err := ReadXML(fd, myID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:29:37 +02:00
|
|
|
return Wrap(path, cfg, evLogger), nil
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) ConfigPath() string {
|
2015-07-16 12:52:36 +02:00
|
|
|
return w.path
|
|
|
|
}
|
|
|
|
|
2014-10-07 10:34:53 +02:00
|
|
|
// Subscribe registers the given handler to be called on any future
|
2014-10-06 09:25:45 +02:00
|
|
|
// configuration changes.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Subscribe(c Committer) {
|
2015-09-21 10:37:45 +02:00
|
|
|
w.mut.Lock()
|
2015-06-03 09:47:39 +02:00
|
|
|
w.subs = append(w.subs, c)
|
2015-09-21 10:37:45 +02:00
|
|
|
w.mut.Unlock()
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2015-06-28 21:09:10 +02:00
|
|
|
// Unsubscribe de-registers the given handler from any future calls to
|
|
|
|
// configuration changes
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Unsubscribe(c Committer) {
|
2015-09-21 10:37:45 +02:00
|
|
|
w.mut.Lock()
|
2015-06-28 21:09:10 +02:00
|
|
|
for i := range w.subs {
|
|
|
|
if w.subs[i] == c {
|
|
|
|
copy(w.subs[i:], w.subs[i+1:])
|
|
|
|
w.subs[len(w.subs)-1] = nil
|
|
|
|
w.subs = w.subs[:len(w.subs)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 10:37:45 +02:00
|
|
|
w.mut.Unlock()
|
2015-06-28 21:09:10 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 10:34:18 +01:00
|
|
|
// RawCopy returns a copy of the currently wrapped Configuration object.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) RawCopy() Configuration {
|
2016-11-12 10:34:18 +01:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.Copy()
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace swaps the current configuration object for the given one.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Replace(cfg Configuration) (Waiter, error) {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.replaceLocked(cfg.Copy())
|
2015-06-03 09:47:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) replaceLocked(to Configuration) (Waiter, error) {
|
2015-06-03 09:47:39 +02:00
|
|
|
from := w.cfg
|
2014-10-06 09:25:45 +02:00
|
|
|
|
2016-08-07 18:21:59 +02:00
|
|
|
if err := to.clean(); err != nil {
|
2017-12-07 08:08:24 +01:00
|
|
|
return noopWaiter{}, err
|
2016-08-07 18:21:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 09:47:39 +02:00
|
|
|
for _, sub := range w.subs {
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln(sub, "verifying configuration")
|
2018-07-26 23:14:12 +02:00
|
|
|
if err := sub.VerifyConfiguration(from.Copy(), to.Copy()); err != nil {
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln(sub, "rejected config:", err)
|
2017-12-07 08:08:24 +01:00
|
|
|
return noopWaiter{}, err
|
2015-06-03 09:47:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.cfg = to
|
2014-10-06 09:25:45 +02:00
|
|
|
w.deviceMap = nil
|
|
|
|
w.folderMap = nil
|
2015-06-03 09:47:39 +02:00
|
|
|
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.notifyListeners(from.Copy(), to.Copy()), nil
|
2016-07-04 22:32:34 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) notifyListeners(from, to Configuration) Waiter {
|
2017-12-07 08:08:24 +01:00
|
|
|
wg := sync.NewWaitGroup()
|
|
|
|
wg.Add(len(w.subs))
|
2016-07-04 22:32:34 +02:00
|
|
|
for _, sub := range w.subs {
|
2017-10-04 00:53:02 +02:00
|
|
|
go func(commiter Committer) {
|
2018-07-26 23:14:12 +02:00
|
|
|
w.notifyListener(commiter, from, to)
|
2017-12-07 08:08:24 +01:00
|
|
|
wg.Done()
|
2017-10-04 00:53:02 +02:00
|
|
|
}(sub)
|
2016-07-04 22:32:34 +02:00
|
|
|
}
|
2017-12-07 08:08:24 +01:00
|
|
|
return wg
|
2016-07-04 22:32:34 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) notifyListener(sub Committer, from, to Configuration) {
|
2016-07-04 22:32:34 +02:00
|
|
|
l.Debugln(sub, "committing configuration")
|
|
|
|
if !sub.CommitConfiguration(from, to) {
|
|
|
|
l.Debugln(sub, "requires restart")
|
|
|
|
w.setRequiresRestart()
|
2015-06-03 09:47:39 +02:00
|
|
|
}
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:14:12 +02:00
|
|
|
// Devices returns a map of devices.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
if w.deviceMap == nil {
|
|
|
|
w.deviceMap = make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
|
|
|
|
for _, dev := range w.cfg.Devices {
|
2018-07-26 23:14:12 +02:00
|
|
|
w.deviceMap[dev.DeviceID] = dev.Copy()
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.deviceMap
|
|
|
|
}
|
|
|
|
|
2017-03-04 08:54:13 +01:00
|
|
|
// SetDevices adds new devices to the configuration, or overwrites existing
|
|
|
|
// devices with the same ID.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) SetDevices(devs []DeviceConfiguration) (Waiter, error) {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
2015-06-03 09:47:39 +02:00
|
|
|
newCfg := w.cfg.Copy()
|
2017-03-04 08:54:13 +01:00
|
|
|
var replaced bool
|
|
|
|
for oldIndex := range devs {
|
|
|
|
replaced = false
|
|
|
|
for newIndex := range newCfg.Devices {
|
|
|
|
if newCfg.Devices[newIndex].DeviceID == devs[oldIndex].DeviceID {
|
2018-07-26 23:14:12 +02:00
|
|
|
newCfg.Devices[newIndex] = devs[oldIndex].Copy()
|
2017-03-04 08:54:13 +01:00
|
|
|
replaced = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !replaced {
|
2018-07-26 23:14:12 +02:00
|
|
|
newCfg.Devices = append(newCfg.Devices, devs[oldIndex].Copy())
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
2015-06-03 09:47:39 +02:00
|
|
|
}
|
2014-10-06 09:25:45 +02:00
|
|
|
|
2017-12-07 08:08:24 +01:00
|
|
|
return w.replaceLocked(newCfg)
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-04 08:54:13 +01:00
|
|
|
// SetDevice adds a new device to the configuration, or overwrites an existing
|
|
|
|
// device with the same ID.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) SetDevice(dev DeviceConfiguration) (Waiter, error) {
|
2017-03-04 08:54:13 +01:00
|
|
|
return w.SetDevices([]DeviceConfiguration{dev})
|
|
|
|
}
|
|
|
|
|
2016-11-17 09:56:55 +01:00
|
|
|
// RemoveDevice removes the device from the configuration
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) RemoveDevice(id protocol.DeviceID) (Waiter, error) {
|
2016-11-17 09:56:55 +01:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
|
|
|
newCfg := w.cfg.Copy()
|
|
|
|
for i := range newCfg.Devices {
|
|
|
|
if newCfg.Devices[i].DeviceID == id {
|
|
|
|
newCfg.Devices = append(newCfg.Devices[:i], newCfg.Devices[i+1:]...)
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.replaceLocked(newCfg)
|
2016-11-17 09:56:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:14:12 +02:00
|
|
|
return noopWaiter{}, nil
|
2016-11-17 09:56:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:32:10 +02:00
|
|
|
// Folders returns a map of folders. Folder structures should not be changed,
|
2014-10-06 09:25:45 +02:00
|
|
|
// other than for the purpose of updating via SetFolder().
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Folders() map[string]FolderConfiguration {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
if w.folderMap == nil {
|
|
|
|
w.folderMap = make(map[string]FolderConfiguration, len(w.cfg.Folders))
|
|
|
|
for _, fld := range w.cfg.Folders {
|
2018-07-26 23:14:12 +02:00
|
|
|
w.folderMap[fld.ID] = fld.Copy()
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.folderMap
|
|
|
|
}
|
|
|
|
|
2018-04-22 18:01:52 +02:00
|
|
|
// FolderList returns a slice of folders.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) FolderList() []FolderConfiguration {
|
2018-04-22 18:01:52 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.cfg.Copy().Folders
|
2018-04-22 18:01:52 +02:00
|
|
|
}
|
|
|
|
|
2014-10-06 09:25:45 +02:00
|
|
|
// SetFolder adds a new folder to the configuration, or overwrites an existing
|
|
|
|
// folder with the same ID.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) SetFolder(fld FolderConfiguration) (Waiter, error) {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
2015-06-03 09:47:39 +02:00
|
|
|
newCfg := w.cfg.Copy()
|
2018-07-26 23:14:12 +02:00
|
|
|
|
2015-06-03 09:47:39 +02:00
|
|
|
for i := range newCfg.Folders {
|
|
|
|
if newCfg.Folders[i].ID == fld.ID {
|
|
|
|
newCfg.Folders[i] = fld
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.replaceLocked(newCfg)
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-26 23:14:12 +02:00
|
|
|
|
|
|
|
newCfg.Folders = append(newCfg.Folders, fld)
|
2014-10-06 09:25:45 +02:00
|
|
|
|
2017-12-07 08:08:24 +01:00
|
|
|
return w.replaceLocked(newCfg)
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns the current options configuration object.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Options() OptionsConfiguration {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.cfg.Options.Copy()
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions replaces the current options configuration object.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) SetOptions(opts OptionsConfiguration) (Waiter, error) {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2015-06-03 09:47:39 +02:00
|
|
|
newCfg := w.cfg.Copy()
|
2018-07-26 23:14:12 +02:00
|
|
|
newCfg.Options = opts.Copy()
|
2017-12-07 08:08:24 +01:00
|
|
|
return w.replaceLocked(newCfg)
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) LDAP() LDAPConfiguration {
|
2018-09-11 23:25:24 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.LDAP.Copy()
|
|
|
|
}
|
|
|
|
|
2014-10-06 09:25:45 +02:00
|
|
|
// GUI returns the current GUI configuration object.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) GUI() GUIConfiguration {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2018-07-26 23:14:12 +02:00
|
|
|
return w.cfg.GUI.Copy()
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGUI replaces the current GUI configuration object.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) SetGUI(gui GUIConfiguration) (Waiter, error) {
|
2014-10-06 09:25:45 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2015-06-03 09:47:39 +02:00
|
|
|
newCfg := w.cfg.Copy()
|
2018-07-26 23:14:12 +02:00
|
|
|
newCfg.GUI = gui.Copy()
|
2017-12-07 08:08:24 +01:00
|
|
|
return w.replaceLocked(newCfg)
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:32:10 +02:00
|
|
|
// IgnoredDevice returns whether or not connection attempts from the given
|
|
|
|
// device should be silently ignored.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) IgnoredDevice(id protocol.DeviceID) bool {
|
2014-12-28 00:12:12 +01:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, device := range w.cfg.IgnoredDevices {
|
2018-08-25 12:36:10 +02:00
|
|
|
if device.ID == id {
|
2014-12-28 00:12:12 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-31 20:04:00 +02:00
|
|
|
// IgnoredFolder returns whether or not share attempts for the given
|
|
|
|
// folder should be silently ignored.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) IgnoredFolder(device protocol.DeviceID, folder string) bool {
|
2018-08-25 12:36:10 +02:00
|
|
|
dev, ok := w.Device(device)
|
|
|
|
if !ok {
|
|
|
|
return false
|
2017-05-31 20:04:00 +02:00
|
|
|
}
|
2018-08-25 12:36:10 +02:00
|
|
|
return dev.IgnoredFolder(folder)
|
2017-05-31 20:04:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 11:29:49 +02:00
|
|
|
// Device returns the configuration for the given device and an "ok" bool.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Device(id protocol.DeviceID) (DeviceConfiguration, bool) {
|
2016-08-05 11:29:49 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, device := range w.cfg.Devices {
|
|
|
|
if device.DeviceID == id {
|
2018-07-26 23:14:12 +02:00
|
|
|
return device.Copy(), true
|
2016-08-05 11:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return DeviceConfiguration{}, false
|
|
|
|
}
|
|
|
|
|
2016-12-21 19:41:25 +01:00
|
|
|
// Folder returns the configuration for the given folder and an "ok" bool.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Folder(id string) (FolderConfiguration, bool) {
|
2016-12-21 19:41:25 +01:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, folder := range w.cfg.Folders {
|
|
|
|
if folder.ID == id {
|
2018-07-26 23:14:12 +02:00
|
|
|
return folder.Copy(), true
|
2016-12-21 19:41:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return FolderConfiguration{}, false
|
|
|
|
}
|
|
|
|
|
2014-10-06 09:25:45 +02:00
|
|
|
// Save writes the configuration to disk, and generates a ConfigSaved event.
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) Save() error {
|
2018-07-26 23:14:12 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
2016-11-23 15:06:08 +01:00
|
|
|
fd, err := osutil.CreateAtomic(w.path)
|
2014-10-06 09:25:45 +02:00
|
|
|
if err != nil {
|
2016-09-24 09:58:17 +02:00
|
|
|
l.Debugln("CreateAtomic:", err)
|
2014-10-06 09:25:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-11 17:03:40 +02:00
|
|
|
if err := w.cfg.WriteXML(fd); err != nil {
|
2016-09-24 09:58:17 +02:00
|
|
|
l.Debugln("WriteXML:", err)
|
2014-10-06 09:25:45 +02:00
|
|
|
fd.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-11 17:03:40 +02:00
|
|
|
if err := fd.Close(); err != nil {
|
2016-09-24 09:58:17 +02:00
|
|
|
l.Debugln("Close:", err)
|
2014-10-06 09:25:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:29:37 +02:00
|
|
|
w.evLogger.Log(events.ConfigSaved, w.cfg)
|
2015-07-11 17:03:40 +02:00
|
|
|
return nil
|
2014-10-06 09:25:45 +02:00
|
|
|
}
|
2015-09-20 15:30:25 +02:00
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) GlobalDiscoveryServers() []string {
|
2015-09-20 15:30:25 +02:00
|
|
|
var servers []string
|
2018-07-26 23:14:12 +02:00
|
|
|
for _, srv := range w.Options().GlobalAnnServers {
|
2015-11-09 15:35:14 +01:00
|
|
|
switch srv {
|
|
|
|
case "default":
|
2015-09-20 15:30:25 +02:00
|
|
|
servers = append(servers, DefaultDiscoveryServers...)
|
2015-11-09 15:35:14 +01:00
|
|
|
case "default-v4":
|
|
|
|
servers = append(servers, DefaultDiscoveryServersV4...)
|
|
|
|
case "default-v6":
|
|
|
|
servers = append(servers, DefaultDiscoveryServersV6...)
|
|
|
|
default:
|
2015-09-20 15:30:25 +02:00
|
|
|
servers = append(servers, srv)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 09:56:40 +02:00
|
|
|
return util.UniqueTrimmedStrings(servers)
|
2015-09-20 15:30:25 +02:00
|
|
|
}
|
2016-05-04 21:38:12 +02:00
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) ListenAddresses() []string {
|
2016-05-04 21:38:12 +02:00
|
|
|
var addresses []string
|
2018-07-26 23:14:12 +02:00
|
|
|
for _, addr := range w.Options().ListenAddresses {
|
2016-05-04 21:38:12 +02:00
|
|
|
switch addr {
|
|
|
|
case "default":
|
|
|
|
addresses = append(addresses, DefaultListenAddresses...)
|
|
|
|
default:
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 09:56:40 +02:00
|
|
|
return util.UniqueTrimmedStrings(addresses)
|
2016-05-04 21:38:12 +02:00
|
|
|
}
|
2016-07-04 22:32:34 +02:00
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) RequiresRestart() bool {
|
2016-07-04 22:32:34 +02:00
|
|
|
return atomic.LoadUint32(&w.requiresRestart) != 0
|
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) setRequiresRestart() {
|
2016-07-04 22:32:34 +02:00
|
|
|
atomic.StoreUint32(&w.requiresRestart, 1)
|
|
|
|
}
|
2017-03-07 13:44:16 +01:00
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) MyName() string {
|
2017-05-22 21:58:33 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
myID := w.cfg.MyID
|
|
|
|
w.mut.Unlock()
|
|
|
|
cfg, _ := w.Device(myID)
|
|
|
|
return cfg.Name
|
|
|
|
}
|
2017-10-24 09:58:55 +02:00
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string) {
|
2018-08-25 12:36:10 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
|
|
|
for i := range w.cfg.PendingDevices {
|
|
|
|
if w.cfg.PendingDevices[i].ID == device {
|
2019-03-27 20:35:42 +01:00
|
|
|
w.cfg.PendingDevices[i].Time = time.Now().Round(time.Second)
|
2018-08-25 12:36:10 +02:00
|
|
|
w.cfg.PendingDevices[i].Name = name
|
|
|
|
w.cfg.PendingDevices[i].Address = address
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.cfg.PendingDevices = append(w.cfg.PendingDevices, ObservedDevice{
|
2019-03-27 20:35:42 +01:00
|
|
|
Time: time.Now().Round(time.Second),
|
2018-08-25 12:36:10 +02:00
|
|
|
ID: device,
|
|
|
|
Name: name,
|
|
|
|
Address: address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-26 09:09:25 +01:00
|
|
|
func (w *wrapper) AddOrUpdatePendingFolder(id, label string, device protocol.DeviceID) {
|
2018-08-25 12:36:10 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
2018-09-12 13:16:52 +02:00
|
|
|
for i := range w.cfg.Devices {
|
|
|
|
if w.cfg.Devices[i].DeviceID == device {
|
|
|
|
for j := range w.cfg.Devices[i].PendingFolders {
|
|
|
|
if w.cfg.Devices[i].PendingFolders[j].ID == id {
|
|
|
|
w.cfg.Devices[i].PendingFolders[j].Label = label
|
2019-03-27 20:35:42 +01:00
|
|
|
w.cfg.Devices[i].PendingFolders[j].Time = time.Now().Round(time.Second)
|
2018-08-25 12:36:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:16:52 +02:00
|
|
|
w.cfg.Devices[i].PendingFolders = append(w.cfg.Devices[i].PendingFolders, ObservedFolder{
|
2019-03-27 20:35:42 +01:00
|
|
|
Time: time.Now().Round(time.Second),
|
2018-08-25 12:36:10 +02:00
|
|
|
ID: id,
|
|
|
|
Label: label,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("bug: adding pending folder for non-existing device")
|
|
|
|
}
|