optionsconfiguration.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "fmt"
  9. "runtime"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. "github.com/syncthing/syncthing/lib/rand"
  12. "github.com/syncthing/syncthing/lib/stringutil"
  13. "github.com/syncthing/syncthing/lib/structutil"
  14. )
  15. type OptionsConfiguration struct {
  16. RawListenAddresses []string `json:"listenAddresses" xml:"listenAddress" default:"default"`
  17. RawGlobalAnnServers []string `json:"globalAnnounceServers" xml:"globalAnnounceServer" default:"default"`
  18. GlobalAnnEnabled bool `json:"globalAnnounceEnabled" xml:"globalAnnounceEnabled" default:"true"`
  19. LocalAnnEnabled bool `json:"localAnnounceEnabled" xml:"localAnnounceEnabled" default:"true"`
  20. LocalAnnPort int `json:"localAnnouncePort" xml:"localAnnouncePort" default:"21027"`
  21. LocalAnnMCAddr string `json:"localAnnounceMCAddr" xml:"localAnnounceMCAddr" default:"[ff12::8384]:21027"`
  22. MaxSendKbps int `json:"maxSendKbps" xml:"maxSendKbps"`
  23. MaxRecvKbps int `json:"maxRecvKbps" xml:"maxRecvKbps"`
  24. ReconnectIntervalS int `json:"reconnectionIntervalS" xml:"reconnectionIntervalS" default:"60"`
  25. RelaysEnabled bool `json:"relaysEnabled" xml:"relaysEnabled" default:"true"`
  26. RelayReconnectIntervalM int `json:"relayReconnectIntervalM" xml:"relayReconnectIntervalM" default:"10"`
  27. StartBrowser bool `json:"startBrowser" xml:"startBrowser" default:"true"`
  28. NATEnabled bool `json:"natEnabled" xml:"natEnabled" default:"true"`
  29. NATLeaseM int `json:"natLeaseMinutes" xml:"natLeaseMinutes" default:"60"`
  30. NATRenewalM int `json:"natRenewalMinutes" xml:"natRenewalMinutes" default:"30"`
  31. NATTimeoutS int `json:"natTimeoutSeconds" xml:"natTimeoutSeconds" default:"10"`
  32. URAccepted int `json:"urAccepted" xml:"urAccepted"`
  33. URSeen int `json:"urSeen" xml:"urSeen"`
  34. URUniqueID string `json:"urUniqueId" xml:"urUniqueID"`
  35. URURL string `json:"urURL" xml:"urURL" default:"https://data.syncthing.net/newdata"`
  36. URPostInsecurely bool `json:"urPostInsecurely" xml:"urPostInsecurely" default:"false"`
  37. URInitialDelayS int `json:"urInitialDelayS" xml:"urInitialDelayS" default:"1800"`
  38. AutoUpgradeIntervalH int `json:"autoUpgradeIntervalH" xml:"autoUpgradeIntervalH" default:"12"`
  39. UpgradeToPreReleases bool `json:"upgradeToPreReleases" xml:"upgradeToPreReleases"`
  40. KeepTemporariesH int `json:"keepTemporariesH" xml:"keepTemporariesH" default:"24"`
  41. CacheIgnoredFiles bool `json:"cacheIgnoredFiles" xml:"cacheIgnoredFiles" default:"false"`
  42. ProgressUpdateIntervalS int `json:"progressUpdateIntervalS" xml:"progressUpdateIntervalS" default:"5"`
  43. LimitBandwidthInLan bool `json:"limitBandwidthInLan" xml:"limitBandwidthInLan" default:"false"`
  44. MinHomeDiskFree Size `json:"minHomeDiskFree" xml:"minHomeDiskFree" default:"1 %"`
  45. ReleasesURL string `json:"releasesURL" xml:"releasesURL" default:"https://upgrades.syncthing.net/meta.json"`
  46. AlwaysLocalNets []string `json:"alwaysLocalNets" xml:"alwaysLocalNet"`
  47. OverwriteRemoteDevNames bool `json:"overwriteRemoteDeviceNamesOnConnect" xml:"overwriteRemoteDeviceNamesOnConnect" default:"false"`
  48. TempIndexMinBlocks int `json:"tempIndexMinBlocks" xml:"tempIndexMinBlocks" default:"10"`
  49. UnackedNotificationIDs []string `json:"unackedNotificationIDs" xml:"unackedNotificationID"`
  50. TrafficClass int `json:"trafficClass" xml:"trafficClass"`
  51. DeprecatedDefaultFolderPath string `json:"-" xml:"defaultFolderPath,omitempty"` // Deprecated: Do not use.
  52. SetLowPriority bool `json:"setLowPriority" xml:"setLowPriority" default:"true"`
  53. RawMaxFolderConcurrency int `json:"maxFolderConcurrency" xml:"maxFolderConcurrency"`
  54. CRURL string `json:"crURL" xml:"crashReportingURL" default:"https://crash.syncthing.net/newcrash"`
  55. CREnabled bool `json:"crashReportingEnabled" xml:"crashReportingEnabled" default:"true"`
  56. StunKeepaliveStartS int `json:"stunKeepaliveStartS" xml:"stunKeepaliveStartS" default:"180"`
  57. StunKeepaliveMinS int `json:"stunKeepaliveMinS" xml:"stunKeepaliveMinS" default:"20"`
  58. RawStunServers []string `json:"stunServers" xml:"stunServer" default:"default"`
  59. DatabaseTuning Tuning `json:"databaseTuning" xml:"databaseTuning" restart:"true"`
  60. RawMaxCIRequestKiB int `json:"maxConcurrentIncomingRequestKiB" xml:"maxConcurrentIncomingRequestKiB"`
  61. AnnounceLANAddresses bool `json:"announceLANAddresses" xml:"announceLANAddresses" default:"true"`
  62. SendFullIndexOnUpgrade bool `json:"sendFullIndexOnUpgrade" xml:"sendFullIndexOnUpgrade"`
  63. FeatureFlags []string `json:"featureFlags" xml:"featureFlag"`
  64. // The number of connections at which we stop trying to connect to more
  65. // devices, zero meaning no limit. Does not affect incoming connections.
  66. ConnectionLimitEnough int `json:"connectionLimitEnough" xml:"connectionLimitEnough"`
  67. // The maximum number of connections which we will allow in total, zero
  68. // meaning no limit. Affects incoming connections and prevents
  69. // attempting outgoing connections.
  70. ConnectionLimitMax int `json:"connectionLimitMax" xml:"connectionLimitMax"`
  71. // When set, this allows TLS 1.2 on sync connections, where we otherwise
  72. // default to TLS 1.3+ only.
  73. InsecureAllowOldTLSVersions bool `json:"insecureAllowOldTLSVersions" xml:"insecureAllowOldTLSVersions"`
  74. ConnectionPriorityTCPLAN int `json:"connectionPriorityTcpLan" xml:"connectionPriorityTcpLan" default:"10"`
  75. ConnectionPriorityQUICLAN int `json:"connectionPriorityQuicLan" xml:"connectionPriorityQuicLan" default:"20"`
  76. ConnectionPriorityTCPWAN int `json:"connectionPriorityTcpWan" xml:"connectionPriorityTcpWan" default:"30"`
  77. ConnectionPriorityQUICWAN int `json:"connectionPriorityQuicWan" xml:"connectionPriorityQuicWan" default:"40"`
  78. ConnectionPriorityRelay int `json:"connectionPriorityRelay" xml:"connectionPriorityRelay" default:"50"`
  79. ConnectionPriorityUpgradeThreshold int `json:"connectionPriorityUpgradeThreshold" xml:"connectionPriorityUpgradeThreshold" default:"0"`
  80. // Legacy deprecated
  81. DeprecatedUPnPEnabled bool `json:"-" xml:"upnpEnabled,omitempty"` // Deprecated: Do not use.
  82. DeprecatedUPnPLeaseM int `json:"-" xml:"upnpLeaseMinutes,omitempty"` // Deprecated: Do not use.
  83. DeprecatedUPnPRenewalM int `json:"-" xml:"upnpRenewalMinutes,omitempty"` // Deprecated: Do not use.
  84. DeprecatedUPnPTimeoutS int `json:"-" xml:"upnpTimeoutSeconds,omitempty"` // Deprecated: Do not use.
  85. DeprecatedRelayServers []string `json:"-" xml:"relayServer,omitempty"` // Deprecated: Do not use.
  86. DeprecatedMinHomeDiskFreePct float64 `json:"-" xml:"minHomeDiskFreePct,omitempty"` // Deprecated: Do not use.
  87. DeprecatedMaxConcurrentScans int `json:"-" xml:"maxConcurrentScans,omitempty"` // Deprecated: Do not use.
  88. }
  89. func (opts OptionsConfiguration) Copy() OptionsConfiguration {
  90. optsCopy := opts
  91. optsCopy.RawListenAddresses = make([]string, len(opts.RawListenAddresses))
  92. copy(optsCopy.RawListenAddresses, opts.RawListenAddresses)
  93. optsCopy.RawGlobalAnnServers = make([]string, len(opts.RawGlobalAnnServers))
  94. copy(optsCopy.RawGlobalAnnServers, opts.RawGlobalAnnServers)
  95. optsCopy.AlwaysLocalNets = make([]string, len(opts.AlwaysLocalNets))
  96. copy(optsCopy.AlwaysLocalNets, opts.AlwaysLocalNets)
  97. optsCopy.UnackedNotificationIDs = make([]string, len(opts.UnackedNotificationIDs))
  98. copy(optsCopy.UnackedNotificationIDs, opts.UnackedNotificationIDs)
  99. return optsCopy
  100. }
  101. func (opts *OptionsConfiguration) prepare(guiPWIsSet bool) {
  102. structutil.FillNilSlices(opts)
  103. opts.RawListenAddresses = stringutil.UniqueTrimmedStrings(opts.RawListenAddresses)
  104. opts.RawGlobalAnnServers = stringutil.UniqueTrimmedStrings(opts.RawGlobalAnnServers)
  105. // Very short reconnection intervals are annoying
  106. if opts.ReconnectIntervalS < 5 {
  107. opts.ReconnectIntervalS = 5
  108. }
  109. if guiPWIsSet && len(opts.UnackedNotificationIDs) > 0 {
  110. for i, key := range opts.UnackedNotificationIDs {
  111. if key == "authenticationUserAndPassword" {
  112. opts.UnackedNotificationIDs = append(opts.UnackedNotificationIDs[:i], opts.UnackedNotificationIDs[i+1:]...)
  113. break
  114. }
  115. }
  116. }
  117. // Negative limits are meaningless, zero means unlimited.
  118. if opts.ConnectionLimitEnough < 0 {
  119. opts.ConnectionLimitEnough = 0
  120. }
  121. if opts.ConnectionLimitMax < 0 {
  122. opts.ConnectionLimitMax = 0
  123. }
  124. if opts.ConnectionPriorityQUICWAN <= opts.ConnectionPriorityQUICLAN {
  125. l.Warnln("Connection priority number for QUIC over WAN must be worse (higher) than QUIC over LAN. Correcting.")
  126. opts.ConnectionPriorityQUICWAN = opts.ConnectionPriorityQUICLAN + 1
  127. }
  128. if opts.ConnectionPriorityTCPWAN <= opts.ConnectionPriorityTCPLAN {
  129. l.Warnln("Connection priority number for TCP over WAN must be worse (higher) than TCP over LAN. Correcting.")
  130. opts.ConnectionPriorityTCPWAN = opts.ConnectionPriorityTCPLAN + 1
  131. }
  132. // If usage reporting is enabled we must have a unique ID.
  133. if opts.URAccepted > 0 && opts.URUniqueID == "" {
  134. opts.URUniqueID = rand.String(8)
  135. }
  136. }
  137. // RequiresRestartOnly returns a copy with only the attributes that require
  138. // restart on change.
  139. func (opts OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
  140. optsCopy := opts
  141. blank := OptionsConfiguration{}
  142. copyMatchingTag(&blank, &optsCopy, "restart", func(v string) bool {
  143. if len(v) > 0 && v != "true" {
  144. panic(fmt.Sprintf(`unexpected tag value: %s. Expected untagged or "true"`, v))
  145. }
  146. return v != "true"
  147. })
  148. return optsCopy
  149. }
  150. func (opts OptionsConfiguration) IsStunDisabled() bool {
  151. return opts.StunKeepaliveMinS < 1 || opts.StunKeepaliveStartS < 1 || !opts.NATEnabled
  152. }
  153. func (opts OptionsConfiguration) ListenAddresses() []string {
  154. var addresses []string
  155. for _, addr := range opts.RawListenAddresses {
  156. switch addr {
  157. case "default":
  158. addresses = append(addresses, DefaultListenAddresses...)
  159. default:
  160. addresses = append(addresses, addr)
  161. }
  162. }
  163. return stringutil.UniqueTrimmedStrings(addresses)
  164. }
  165. func (opts OptionsConfiguration) StunServers() []string {
  166. var addresses []string
  167. for _, addr := range opts.RawStunServers {
  168. switch addr {
  169. case "default":
  170. defaultPrimaryAddresses := make([]string, len(DefaultPrimaryStunServers))
  171. copy(defaultPrimaryAddresses, DefaultPrimaryStunServers)
  172. rand.Shuffle(defaultPrimaryAddresses)
  173. addresses = append(addresses, defaultPrimaryAddresses...)
  174. defaultSecondaryAddresses := make([]string, len(DefaultSecondaryStunServers))
  175. copy(defaultSecondaryAddresses, DefaultSecondaryStunServers)
  176. rand.Shuffle(defaultSecondaryAddresses)
  177. addresses = append(addresses, defaultSecondaryAddresses...)
  178. default:
  179. addresses = append(addresses, addr)
  180. }
  181. }
  182. addresses = stringutil.UniqueTrimmedStrings(addresses)
  183. return addresses
  184. }
  185. func (opts OptionsConfiguration) GlobalDiscoveryServers() []string {
  186. var servers []string
  187. for _, srv := range opts.RawGlobalAnnServers {
  188. switch srv {
  189. case "default":
  190. servers = append(servers, DefaultDiscoveryServers...)
  191. case "default-v4":
  192. servers = append(servers, DefaultDiscoveryServersV4...)
  193. case "default-v6":
  194. servers = append(servers, DefaultDiscoveryServersV6...)
  195. default:
  196. servers = append(servers, srv)
  197. }
  198. }
  199. return stringutil.UniqueTrimmedStrings(servers)
  200. }
  201. func (opts OptionsConfiguration) MaxFolderConcurrency() int {
  202. // If a value is set, trust that.
  203. if opts.RawMaxFolderConcurrency > 0 {
  204. return opts.RawMaxFolderConcurrency
  205. }
  206. if opts.RawMaxFolderConcurrency < 0 {
  207. // -1 etc means unlimited, which in the implementation means zero
  208. return 0
  209. }
  210. // Otherwise default to the number of CPU cores in the system as a rough
  211. // approximation of system powerfullness.
  212. if n := runtime.GOMAXPROCS(-1); n > 0 {
  213. return n
  214. }
  215. // We should never get here to begin with, but since we're here let's
  216. // use some sort of reasonable compromise between the old "no limit" and
  217. // getting nothing done... (Median number of folders out there at time
  218. // of writing is two, 95-percentile at 12 folders.)
  219. return 4 // https://xkcd.com/221/
  220. }
  221. func (opts OptionsConfiguration) MaxConcurrentIncomingRequestKiB() int {
  222. // Negative is disabled, which in limiter land is spelled zero
  223. if opts.RawMaxCIRequestKiB < 0 {
  224. return 0
  225. }
  226. if opts.RawMaxCIRequestKiB == 0 {
  227. // The default is 256 MiB
  228. return 256 * 1024 // KiB
  229. }
  230. // We can't really do less than a couple of concurrent blocks or we'll
  231. // pretty much stall completely. Check that an explicit value is large
  232. // enough.
  233. const minAllowed = 2 * protocol.MaxBlockSize / 1024
  234. if opts.RawMaxCIRequestKiB < minAllowed {
  235. return minAllowed
  236. }
  237. // Roll with it.
  238. return opts.RawMaxCIRequestKiB
  239. }
  240. func (opts OptionsConfiguration) AutoUpgradeEnabled() bool {
  241. return opts.AutoUpgradeIntervalH > 0
  242. }
  243. func (opts OptionsConfiguration) FeatureFlag(name string) bool {
  244. for _, flag := range opts.FeatureFlags {
  245. if flag == name {
  246. return true
  247. }
  248. }
  249. return false
  250. }
  251. // LowestConnectionLimit is the lower of ConnectionLimitEnough or
  252. // ConnectionLimitMax, or whichever of them is actually set if only one of
  253. // them is set. It's the point where we should stop dialing.
  254. func (opts OptionsConfiguration) LowestConnectionLimit() int {
  255. limit := opts.ConnectionLimitEnough
  256. if limit == 0 || (opts.ConnectionLimitMax != 0 && opts.ConnectionLimitMax < limit) {
  257. // It doesn't really make sense to set Max lower than Enough but
  258. // someone might do it while experimenting and it's easy for us to
  259. // do the right thing.
  260. limit = opts.ConnectionLimitMax
  261. }
  262. return limit
  263. }