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
|
|
|
|
|
2017-02-06 10:27:11 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-27 16:31:17 +00:00
|
|
|
"runtime"
|
2017-12-07 08:33:32 +00:00
|
|
|
|
2020-02-01 07:02:18 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2019-11-26 16:07:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2017-12-07 08:33:32 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2017-02-06 10:27:11 +00:00
|
|
|
)
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
func (opts OptionsConfiguration) Copy() OptionsConfiguration {
|
|
|
|
optsCopy := opts
|
2019-11-26 16:07:25 +00:00
|
|
|
optsCopy.RawListenAddresses = make([]string, len(opts.RawListenAddresses))
|
|
|
|
copy(optsCopy.RawListenAddresses, opts.RawListenAddresses)
|
|
|
|
optsCopy.RawGlobalAnnServers = make([]string, len(opts.RawGlobalAnnServers))
|
|
|
|
copy(optsCopy.RawGlobalAnnServers, opts.RawGlobalAnnServers)
|
2019-05-29 07:56:40 +00:00
|
|
|
optsCopy.AlwaysLocalNets = make([]string, len(opts.AlwaysLocalNets))
|
|
|
|
copy(optsCopy.AlwaysLocalNets, opts.AlwaysLocalNets)
|
|
|
|
optsCopy.UnackedNotificationIDs = make([]string, len(opts.UnackedNotificationIDs))
|
|
|
|
copy(optsCopy.UnackedNotificationIDs, opts.UnackedNotificationIDs)
|
|
|
|
return optsCopy
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
2017-12-07 08:33:32 +00:00
|
|
|
|
2020-11-20 13:21:54 +00:00
|
|
|
func (opts *OptionsConfiguration) prepare(guiPWIsSet bool) {
|
|
|
|
util.FillNilSlices(opts)
|
|
|
|
|
|
|
|
opts.RawListenAddresses = util.UniqueTrimmedStrings(opts.RawListenAddresses)
|
|
|
|
opts.RawGlobalAnnServers = util.UniqueTrimmedStrings(opts.RawGlobalAnnServers)
|
|
|
|
|
|
|
|
// Very short reconnection intervals are annoying
|
|
|
|
if opts.ReconnectIntervalS < 5 {
|
|
|
|
opts.ReconnectIntervalS = 5
|
|
|
|
}
|
|
|
|
|
|
|
|
if guiPWIsSet && len(opts.UnackedNotificationIDs) > 0 {
|
|
|
|
for i, key := range opts.UnackedNotificationIDs {
|
|
|
|
if key == "authenticationUserAndPassword" {
|
|
|
|
opts.UnackedNotificationIDs = append(opts.UnackedNotificationIDs[:i], opts.UnackedNotificationIDs[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 14:14:44 +00:00
|
|
|
|
|
|
|
// Negative limits are meaningless, zero means unlimited.
|
|
|
|
if opts.ConnectionLimitEnough < 0 {
|
|
|
|
opts.ConnectionLimitEnough = 0
|
|
|
|
}
|
|
|
|
if opts.ConnectionLimitMax < 0 {
|
|
|
|
opts.ConnectionLimitMax = 0
|
|
|
|
}
|
2023-04-16 12:54:28 +00:00
|
|
|
|
|
|
|
if opts.ConnectionPriorityQUICWAN <= opts.ConnectionPriorityQUICLAN {
|
|
|
|
l.Warnln("Connection priority number for QUIC over WAN must be worse (higher) than QUIC over LAN. Correcting.")
|
|
|
|
opts.ConnectionPriorityQUICWAN = opts.ConnectionPriorityQUICLAN + 1
|
|
|
|
}
|
|
|
|
if opts.ConnectionPriorityTCPWAN <= opts.ConnectionPriorityTCPLAN {
|
|
|
|
l.Warnln("Connection priority number for TCP over WAN must be worse (higher) than TCP over LAN. Correcting.")
|
|
|
|
opts.ConnectionPriorityTCPWAN = opts.ConnectionPriorityTCPLAN + 1
|
|
|
|
}
|
2020-11-20 13:21:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 08:33:32 +00:00
|
|
|
// RequiresRestartOnly returns a copy with only the attributes that require
|
|
|
|
// restart on change.
|
2019-05-29 07:56:40 +00:00
|
|
|
func (opts OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
|
|
|
|
optsCopy := opts
|
2017-12-07 08:33:32 +00:00
|
|
|
blank := OptionsConfiguration{}
|
2019-05-29 07:56:40 +00:00
|
|
|
util.CopyMatchingTag(&blank, &optsCopy, "restart", func(v string) bool {
|
2017-12-07 08:33:32 +00:00
|
|
|
if len(v) > 0 && v != "true" {
|
2019-05-29 07:56:40 +00:00
|
|
|
panic(fmt.Sprintf(`unexpected tag value: %s. Expected untagged or "true"`, v))
|
2017-12-07 08:33:32 +00:00
|
|
|
}
|
|
|
|
return v != "true"
|
|
|
|
})
|
2019-05-29 07:56:40 +00:00
|
|
|
return optsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts OptionsConfiguration) IsStunDisabled() bool {
|
|
|
|
return opts.StunKeepaliveMinS < 1 || opts.StunKeepaliveStartS < 1 || !opts.NATEnabled
|
2017-12-07 08:33:32 +00:00
|
|
|
}
|
2019-11-26 16:07:25 +00:00
|
|
|
|
|
|
|
func (opts OptionsConfiguration) ListenAddresses() []string {
|
|
|
|
var addresses []string
|
|
|
|
for _, addr := range opts.RawListenAddresses {
|
|
|
|
switch addr {
|
|
|
|
case "default":
|
|
|
|
addresses = append(addresses, DefaultListenAddresses...)
|
|
|
|
default:
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.UniqueTrimmedStrings(addresses)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts OptionsConfiguration) StunServers() []string {
|
|
|
|
var addresses []string
|
|
|
|
for _, addr := range opts.RawStunServers {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts OptionsConfiguration) GlobalDiscoveryServers() []string {
|
|
|
|
var servers []string
|
|
|
|
for _, srv := range opts.RawGlobalAnnServers {
|
|
|
|
switch srv {
|
|
|
|
case "default":
|
|
|
|
servers = append(servers, DefaultDiscoveryServers...)
|
|
|
|
case "default-v4":
|
|
|
|
servers = append(servers, DefaultDiscoveryServersV4...)
|
|
|
|
case "default-v6":
|
|
|
|
servers = append(servers, DefaultDiscoveryServersV6...)
|
|
|
|
default:
|
|
|
|
servers = append(servers, srv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.UniqueTrimmedStrings(servers)
|
|
|
|
}
|
2020-01-27 16:31:17 +00:00
|
|
|
|
|
|
|
func (opts OptionsConfiguration) MaxFolderConcurrency() int {
|
|
|
|
// If a value is set, trust that.
|
|
|
|
if opts.RawMaxFolderConcurrency > 0 {
|
|
|
|
return opts.RawMaxFolderConcurrency
|
|
|
|
}
|
|
|
|
if opts.RawMaxFolderConcurrency < 0 {
|
|
|
|
// -1 etc means unlimited, which in the implementation means zero
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// Otherwise default to the number of CPU cores in the system as a rough
|
|
|
|
// approximation of system powerfullness.
|
|
|
|
if n := runtime.GOMAXPROCS(-1); n > 0 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
// We should never get here to begin with, but since we're here let's
|
|
|
|
// use some sort of reasonable compromise between the old "no limit" and
|
|
|
|
// getting nothing done... (Median number of folders out there at time
|
|
|
|
// of writing is two, 95-percentile at 12 folders.)
|
|
|
|
return 4 // https://xkcd.com/221/
|
|
|
|
}
|
2020-02-01 07:02:18 +00:00
|
|
|
|
|
|
|
func (opts OptionsConfiguration) MaxConcurrentIncomingRequestKiB() int {
|
|
|
|
// Negative is disabled, which in limiter land is spelled zero
|
|
|
|
if opts.RawMaxCIRequestKiB < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:51:51 +00:00
|
|
|
if opts.RawMaxCIRequestKiB == 0 {
|
2020-02-01 07:02:18 +00:00
|
|
|
// The default is 256 MiB
|
|
|
|
return 256 * 1024 // KiB
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't really do less than a couple of concurrent blocks or we'll
|
|
|
|
// pretty much stall completely. Check that an explicit value is large
|
|
|
|
// enough.
|
|
|
|
const minAllowed = 2 * protocol.MaxBlockSize / 1024
|
|
|
|
if opts.RawMaxCIRequestKiB < minAllowed {
|
|
|
|
return minAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roll with it.
|
|
|
|
return opts.RawMaxCIRequestKiB
|
|
|
|
}
|
2020-06-02 09:38:39 +00:00
|
|
|
|
2020-08-18 07:26:33 +00:00
|
|
|
func (opts OptionsConfiguration) AutoUpgradeEnabled() bool {
|
2020-06-02 09:38:39 +00:00
|
|
|
return opts.AutoUpgradeIntervalH > 0
|
|
|
|
}
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
func (opts OptionsConfiguration) FeatureFlag(name string) bool {
|
|
|
|
for _, flag := range opts.FeatureFlags {
|
|
|
|
if flag == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2021-01-11 14:14:44 +00:00
|
|
|
|
|
|
|
// LowestConnectionLimit is the lower of ConnectionLimitEnough or
|
|
|
|
// ConnectionLimitMax, or whichever of them is actually set if only one of
|
2022-08-23 13:44:11 +00:00
|
|
|
// them is set. It's the point where we should stop dialing.
|
2021-01-11 14:14:44 +00:00
|
|
|
func (opts OptionsConfiguration) LowestConnectionLimit() int {
|
|
|
|
limit := opts.ConnectionLimitEnough
|
|
|
|
if limit == 0 || (opts.ConnectionLimitMax != 0 && opts.ConnectionLimitMax < limit) {
|
|
|
|
// It doesn't really make sense to set Max lower than Enough but
|
|
|
|
// someone might do it while experimenting and it's easy for us to
|
|
|
|
// do the right thing.
|
|
|
|
limit = opts.ConnectionLimitMax
|
|
|
|
}
|
|
|
|
return limit
|
|
|
|
}
|