2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-06 07:25:45 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00: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 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-10-06 07:25:45 +00:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-10-24 07:58:55 +00:00
|
|
|
"path/filepath"
|
2016-07-04 20:32:34 +00:00
|
|
|
"sync/atomic"
|
2014-10-06 07:25:45 +00:00
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2017-10-24 07:58:55 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2017-03-07 12:44:16 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2016-03-25 20:22:29 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2014-10-06 07:25:45 +00:00
|
|
|
)
|
|
|
|
|
2015-06-03 07:47:39 +00: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 02:20:34 +00:00
|
|
|
// false will result in a "restart needed" response to the API/user. Note that
|
2015-06-03 07:47:39 +00: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 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A wrapper around a Configuration that manages loads, saves and published
|
|
|
|
// notifications of changes to registered Handlers
|
|
|
|
|
2014-12-08 15:39:11 +00:00
|
|
|
type Wrapper struct {
|
2014-10-06 07:25:45 +00:00
|
|
|
cfg Configuration
|
|
|
|
path string
|
|
|
|
|
|
|
|
deviceMap map[protocol.DeviceID]DeviceConfiguration
|
|
|
|
folderMap map[string]FolderConfiguration
|
|
|
|
replaces chan Configuration
|
2015-09-21 08:37:45 +00:00
|
|
|
subs []Committer
|
2014-10-06 07:25:45 +00:00
|
|
|
mut sync.Mutex
|
2016-07-04 20:32:34 +00:00
|
|
|
|
|
|
|
requiresRestart uint32 // an atomic bool
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap wraps an existing Configuration structure and ties it to a file on
|
|
|
|
// disk.
|
2014-12-08 15:39:11 +00:00
|
|
|
func Wrap(path string, cfg Configuration) *Wrapper {
|
2015-04-22 22:54:31 +00:00
|
|
|
w := &Wrapper{
|
|
|
|
cfg: cfg,
|
|
|
|
path: path,
|
|
|
|
mut: sync.NewMutex(),
|
|
|
|
}
|
2014-10-06 07:25:45 +00:00
|
|
|
w.replaces = make(chan Configuration)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads an existing file on disk and returns a new configuration
|
|
|
|
// wrapper.
|
2014-12-08 15:39:11 +00:00
|
|
|
func Load(path string, myID protocol.DeviceID) (*Wrapper, error) {
|
2014-10-06 07:25:45 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
return Wrap(path, cfg), nil
|
|
|
|
}
|
|
|
|
|
2015-07-16 10:52:36 +00:00
|
|
|
func (w *Wrapper) ConfigPath() string {
|
|
|
|
return w.path
|
|
|
|
}
|
|
|
|
|
2014-10-07 08:34:53 +00:00
|
|
|
// Stop stops the Serve() loop. Set and Replace operations will panic after a
|
|
|
|
// Stop.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Stop() {
|
2014-10-06 07:25:45 +00:00
|
|
|
close(w.replaces)
|
|
|
|
}
|
|
|
|
|
2014-10-07 08:34:53 +00:00
|
|
|
// Subscribe registers the given handler to be called on any future
|
2014-10-06 07:25:45 +00:00
|
|
|
// configuration changes.
|
2015-06-03 07:47:39 +00:00
|
|
|
func (w *Wrapper) Subscribe(c Committer) {
|
2015-09-21 08:37:45 +00:00
|
|
|
w.mut.Lock()
|
2015-06-03 07:47:39 +00:00
|
|
|
w.subs = append(w.subs, c)
|
2015-09-21 08:37:45 +00:00
|
|
|
w.mut.Unlock()
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 19:09:10 +00:00
|
|
|
// Unsubscribe de-registers the given handler from any future calls to
|
|
|
|
// configuration changes
|
|
|
|
func (w *Wrapper) Unsubscribe(c Committer) {
|
2015-09-21 08:37:45 +00:00
|
|
|
w.mut.Lock()
|
2015-06-28 19:09:10 +00: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 08:37:45 +00:00
|
|
|
w.mut.Unlock()
|
2015-06-28 19:09:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 09:34:18 +00:00
|
|
|
// RawCopy returns a copy of the currently wrapped Configuration object.
|
|
|
|
func (w *Wrapper) RawCopy() Configuration {
|
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.Copy()
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
// ReplaceBlocking swaps the current configuration object for the given one,
|
|
|
|
// and waits for subscribers to be notified.
|
|
|
|
func (w *Wrapper) ReplaceBlocking(cfg Configuration) error {
|
|
|
|
w.mut.Lock()
|
|
|
|
wg := sync.NewWaitGroup()
|
|
|
|
err := w.replaceLocked(cfg, wg)
|
|
|
|
w.mut.Unlock()
|
|
|
|
wg.Wait()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
// Replace swaps the current configuration object for the given one.
|
2016-07-04 20:32:34 +00:00
|
|
|
func (w *Wrapper) Replace(cfg Configuration) error {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2016-07-04 20:32:34 +00:00
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
return w.replaceLocked(cfg, nil)
|
2015-06-03 07:47:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
func (w *Wrapper) replaceLocked(to Configuration, wg sync.WaitGroup) error {
|
2015-06-03 07:47:39 +00:00
|
|
|
from := w.cfg
|
2014-10-06 07:25:45 +00:00
|
|
|
|
2016-08-07 16:21:59 +00:00
|
|
|
if err := to.clean(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
for _, sub := range w.subs {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(sub, "verifying configuration")
|
2015-06-03 07:47:39 +00:00
|
|
|
if err := sub.VerifyConfiguration(from, to); err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(sub, "rejected config:", err)
|
2016-07-04 20:32:34 +00:00
|
|
|
return err
|
2015-06-03 07:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.cfg = to
|
2014-10-06 07:25:45 +00:00
|
|
|
w.deviceMap = nil
|
|
|
|
w.folderMap = nil
|
2015-06-03 07:47:39 +00:00
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
w.notifyListeners(from, to, wg)
|
2016-07-04 20:32:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
func (w *Wrapper) notifyListeners(from, to Configuration, wg sync.WaitGroup) {
|
|
|
|
if wg != nil {
|
|
|
|
wg.Add(len(w.subs))
|
|
|
|
}
|
2016-07-04 20:32:34 +00:00
|
|
|
for _, sub := range w.subs {
|
2017-10-03 22:53:02 +00:00
|
|
|
go func(commiter Committer) {
|
|
|
|
w.notifyListener(commiter, from.Copy(), to.Copy())
|
|
|
|
if wg != nil {
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}(sub)
|
2016-07-04 20:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wrapper) notifyListener(sub Committer, from, to Configuration) {
|
|
|
|
l.Debugln(sub, "committing configuration")
|
|
|
|
if !sub.CommitConfiguration(from, to) {
|
|
|
|
l.Debugln(sub, "requires restart")
|
|
|
|
w.setRequiresRestart()
|
2015-06-03 07:47:39 +00:00
|
|
|
}
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Devices returns a map of devices. Device structures should not be changed,
|
|
|
|
// other than for the purpose of updating via SetDevice().
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
|
2014-10-06 07:25:45 +00: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 {
|
|
|
|
w.deviceMap[dev.DeviceID] = dev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.deviceMap
|
|
|
|
}
|
|
|
|
|
2017-03-04 07:54:13 +00:00
|
|
|
// SetDevices adds new devices to the configuration, or overwrites existing
|
|
|
|
// devices with the same ID.
|
|
|
|
func (w *Wrapper) SetDevices(devs []DeviceConfiguration) error {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
newCfg := w.cfg.Copy()
|
2017-03-04 07:54:13 +00:00
|
|
|
var replaced bool
|
|
|
|
for oldIndex := range devs {
|
|
|
|
replaced = false
|
|
|
|
for newIndex := range newCfg.Devices {
|
|
|
|
if newCfg.Devices[newIndex].DeviceID == devs[oldIndex].DeviceID {
|
|
|
|
newCfg.Devices[newIndex] = devs[oldIndex]
|
|
|
|
replaced = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !replaced {
|
|
|
|
newCfg.Devices = append(newCfg.Devices, devs[oldIndex])
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
2015-06-03 07:47:39 +00:00
|
|
|
}
|
2014-10-06 07:25:45 +00:00
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
return w.replaceLocked(newCfg, nil)
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 07:54:13 +00:00
|
|
|
// SetDevice adds a new device to the configuration, or overwrites an existing
|
|
|
|
// device with the same ID.
|
|
|
|
func (w *Wrapper) SetDevice(dev DeviceConfiguration) error {
|
|
|
|
return w.SetDevices([]DeviceConfiguration{dev})
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:56:55 +00:00
|
|
|
// RemoveDevice removes the device from the configuration
|
|
|
|
func (w *Wrapper) RemoveDevice(id protocol.DeviceID) error {
|
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
|
|
|
newCfg := w.cfg.Copy()
|
|
|
|
removed := false
|
|
|
|
for i := range newCfg.Devices {
|
|
|
|
if newCfg.Devices[i].DeviceID == id {
|
|
|
|
newCfg.Devices = append(newCfg.Devices[:i], newCfg.Devices[i+1:]...)
|
|
|
|
removed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !removed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
return w.replaceLocked(newCfg, nil)
|
2016-11-17 08:56:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 20:32:10 +00:00
|
|
|
// Folders returns a map of folders. Folder structures should not be changed,
|
2014-10-06 07:25:45 +00:00
|
|
|
// other than for the purpose of updating via SetFolder().
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Folders() map[string]FolderConfiguration {
|
2014-10-06 07:25:45 +00: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 {
|
|
|
|
w.folderMap[fld.ID] = fld
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.folderMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFolder adds a new folder to the configuration, or overwrites an existing
|
|
|
|
// folder with the same ID.
|
2016-07-04 20:32:34 +00:00
|
|
|
func (w *Wrapper) SetFolder(fld FolderConfiguration) error {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
newCfg := w.cfg.Copy()
|
|
|
|
replaced := false
|
|
|
|
for i := range newCfg.Folders {
|
|
|
|
if newCfg.Folders[i].ID == fld.ID {
|
|
|
|
newCfg.Folders[i] = fld
|
|
|
|
replaced = true
|
|
|
|
break
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 07:47:39 +00:00
|
|
|
if !replaced {
|
|
|
|
newCfg.Folders = append(w.cfg.Folders, fld)
|
|
|
|
}
|
2014-10-06 07:25:45 +00:00
|
|
|
|
2017-10-03 22:53:02 +00:00
|
|
|
return w.replaceLocked(newCfg, nil)
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns the current options configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Options() OptionsConfiguration {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions replaces the current options configuration object.
|
2016-07-04 20:32:34 +00:00
|
|
|
func (w *Wrapper) SetOptions(opts OptionsConfiguration) error {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2015-06-03 07:47:39 +00:00
|
|
|
newCfg := w.cfg.Copy()
|
|
|
|
newCfg.Options = opts
|
2017-10-03 22:53:02 +00:00
|
|
|
return w.replaceLocked(newCfg, nil)
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GUI returns the current GUI configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) GUI() GUIConfiguration {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.GUI
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGUI replaces the current GUI configuration object.
|
2016-07-04 20:32:34 +00:00
|
|
|
func (w *Wrapper) SetGUI(gui GUIConfiguration) error {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2015-06-03 07:47:39 +00:00
|
|
|
newCfg := w.cfg.Copy()
|
|
|
|
newCfg.GUI = gui
|
2017-10-03 22:53:02 +00:00
|
|
|
return w.replaceLocked(newCfg, nil)
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 20:32:10 +00:00
|
|
|
// IgnoredDevice returns whether or not connection attempts from the given
|
|
|
|
// device should be silently ignored.
|
2014-12-27 23:12:12 +00:00
|
|
|
func (w *Wrapper) IgnoredDevice(id protocol.DeviceID) bool {
|
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, device := range w.cfg.IgnoredDevices {
|
|
|
|
if device == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-31 18:04:00 +00:00
|
|
|
// IgnoredFolder returns whether or not share attempts for the given
|
|
|
|
// folder should be silently ignored.
|
|
|
|
func (w *Wrapper) IgnoredFolder(folder string) bool {
|
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, nfolder := range w.cfg.IgnoredFolders {
|
|
|
|
if folder == nfolder {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:29:49 +00:00
|
|
|
// Device returns the configuration for the given device and an "ok" bool.
|
|
|
|
func (w *Wrapper) Device(id protocol.DeviceID) (DeviceConfiguration, bool) {
|
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, device := range w.cfg.Devices {
|
|
|
|
if device.DeviceID == id {
|
|
|
|
return device, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DeviceConfiguration{}, false
|
|
|
|
}
|
|
|
|
|
2016-12-21 18:41:25 +00:00
|
|
|
// Folder returns the configuration for the given folder and an "ok" bool.
|
|
|
|
func (w *Wrapper) Folder(id string) (FolderConfiguration, bool) {
|
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
for _, folder := range w.cfg.Folders {
|
|
|
|
if folder.ID == id {
|
|
|
|
return folder, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FolderConfiguration{}, false
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
// Save writes the configuration to disk, and generates a ConfigSaved event.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Save() error {
|
2016-11-23 14:06:08 +00:00
|
|
|
fd, err := osutil.CreateAtomic(w.path)
|
2014-10-06 07:25:45 +00:00
|
|
|
if err != nil {
|
2016-09-24 07:58:17 +00:00
|
|
|
l.Debugln("CreateAtomic:", err)
|
2014-10-06 07:25:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-11 15:03:40 +00:00
|
|
|
if err := w.cfg.WriteXML(fd); err != nil {
|
2016-09-24 07:58:17 +00:00
|
|
|
l.Debugln("WriteXML:", err)
|
2014-10-06 07:25:45 +00:00
|
|
|
fd.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-11 15:03:40 +00:00
|
|
|
if err := fd.Close(); err != nil {
|
2016-09-24 07:58:17 +00:00
|
|
|
l.Debugln("Close:", err)
|
2014-10-06 07:25:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
events.Default.Log(events.ConfigSaved, w.cfg)
|
2015-07-11 15:03:40 +00:00
|
|
|
return nil
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
func (w *Wrapper) GlobalDiscoveryServers() []string {
|
|
|
|
var servers []string
|
|
|
|
for _, srv := range w.cfg.Options.GlobalAnnServers {
|
2015-11-09 14:35:14 +00:00
|
|
|
switch srv {
|
|
|
|
case "default":
|
2015-09-20 13:30:25 +00:00
|
|
|
servers = append(servers, DefaultDiscoveryServers...)
|
2015-11-09 14:35:14 +00:00
|
|
|
case "default-v4":
|
|
|
|
servers = append(servers, DefaultDiscoveryServersV4...)
|
|
|
|
case "default-v6":
|
|
|
|
servers = append(servers, DefaultDiscoveryServersV6...)
|
|
|
|
default:
|
2015-09-20 13:30:25 +00:00
|
|
|
servers = append(servers, srv)
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 20:22:29 +00:00
|
|
|
return util.UniqueStrings(servers)
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
2016-05-04 19:38:12 +00:00
|
|
|
|
|
|
|
func (w *Wrapper) ListenAddresses() []string {
|
|
|
|
var addresses []string
|
|
|
|
for _, addr := range w.cfg.Options.ListenAddresses {
|
|
|
|
switch addr {
|
|
|
|
case "default":
|
|
|
|
addresses = append(addresses, DefaultListenAddresses...)
|
|
|
|
default:
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.UniqueStrings(addresses)
|
|
|
|
}
|
2016-07-04 20:32:34 +00:00
|
|
|
|
|
|
|
func (w *Wrapper) RequiresRestart() bool {
|
|
|
|
return atomic.LoadUint32(&w.requiresRestart) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wrapper) setRequiresRestart() {
|
|
|
|
atomic.StoreUint32(&w.requiresRestart, 1)
|
|
|
|
}
|
2017-03-07 12:44:16 +00:00
|
|
|
|
|
|
|
func (w *Wrapper) StunServers() []string {
|
|
|
|
var addresses []string
|
|
|
|
for _, addr := range w.cfg.Options.StunServers {
|
|
|
|
switch addr {
|
|
|
|
case "default":
|
|
|
|
addresses = append(addresses, DefaultStunServers...)
|
|
|
|
default:
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses = util.UniqueStrings(addresses)
|
|
|
|
|
|
|
|
// Shuffle
|
|
|
|
l := len(addresses)
|
|
|
|
for i := range addresses {
|
|
|
|
r := rand.Intn(l)
|
|
|
|
addresses[i], addresses[r] = addresses[r], addresses[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses
|
|
|
|
}
|
2017-05-22 19:58:33 +00:00
|
|
|
|
|
|
|
func (w *Wrapper) MyName() string {
|
|
|
|
w.mut.Lock()
|
|
|
|
myID := w.cfg.MyID
|
|
|
|
w.mut.Unlock()
|
|
|
|
cfg, _ := w.Device(myID)
|
|
|
|
return cfg.Name
|
|
|
|
}
|
2017-10-24 07:58:55 +00:00
|
|
|
|
|
|
|
// CheckHomeFreeSpace returns nil if the home disk has the required amount of
|
|
|
|
// free space, or if home disk free space checking is disabled.
|
|
|
|
func (w *Wrapper) CheckHomeFreeSpace() error {
|
|
|
|
return checkFreeSpace(w.Options().MinHomeDiskFree, fs.NewFilesystem(fs.FilesystemTypeBasic, filepath.Dir(w.ConfigPath())))
|
|
|
|
}
|