optionsconfiguration.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/util"
  13. )
  14. type OptionsConfiguration struct {
  15. RawListenAddresses []string `xml:"listenAddress" json:"listenAddresses" default:"default"`
  16. RawGlobalAnnServers []string `xml:"globalAnnounceServer" json:"globalAnnounceServers" default:"default"`
  17. GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" json:"globalAnnounceEnabled" default:"true"`
  18. LocalAnnEnabled bool `xml:"localAnnounceEnabled" json:"localAnnounceEnabled" default:"true"`
  19. LocalAnnPort int `xml:"localAnnouncePort" json:"localAnnouncePort" default:"21027"`
  20. LocalAnnMCAddr string `xml:"localAnnounceMCAddr" json:"localAnnounceMCAddr" default:"[ff12::8384]:21027"`
  21. MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
  22. MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
  23. ReconnectIntervalS int `xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60"`
  24. RelaysEnabled bool `xml:"relaysEnabled" json:"relaysEnabled" default:"true"`
  25. RelayReconnectIntervalM int `xml:"relayReconnectIntervalM" json:"relayReconnectIntervalM" default:"10"`
  26. StartBrowser bool `xml:"startBrowser" json:"startBrowser" default:"true"`
  27. NATEnabled bool `xml:"natEnabled" json:"natEnabled" default:"true"`
  28. NATLeaseM int `xml:"natLeaseMinutes" json:"natLeaseMinutes" default:"60"`
  29. NATRenewalM int `xml:"natRenewalMinutes" json:"natRenewalMinutes" default:"30"`
  30. NATTimeoutS int `xml:"natTimeoutSeconds" json:"natTimeoutSeconds" default:"10"`
  31. URAccepted int `xml:"urAccepted" json:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
  32. URSeen int `xml:"urSeen" json:"urSeen"` // Report which the user has been prompted for.
  33. URUniqueID string `xml:"urUniqueID" json:"urUniqueId"` // Unique ID for reporting purposes, regenerated when UR is turned on.
  34. URURL string `xml:"urURL" json:"urURL" default:"https://data.syncthing.net/newdata"` // usage reporting URL
  35. URPostInsecurely bool `xml:"urPostInsecurely" json:"urPostInsecurely" default:"false"` // For testing
  36. URInitialDelayS int `xml:"urInitialDelayS" json:"urInitialDelayS" default:"1800"`
  37. RestartOnWakeup bool `xml:"restartOnWakeup" json:"restartOnWakeup" default:"true"`
  38. AutoUpgradeIntervalH int `xml:"autoUpgradeIntervalH" json:"autoUpgradeIntervalH" default:"12"` // 0 for off
  39. UpgradeToPreReleases bool `xml:"upgradeToPreReleases" json:"upgradeToPreReleases"` // when auto upgrades are enabled
  40. KeepTemporariesH int `xml:"keepTemporariesH" json:"keepTemporariesH" default:"24"` // 0 for off
  41. CacheIgnoredFiles bool `xml:"cacheIgnoredFiles" json:"cacheIgnoredFiles" default:"false"`
  42. ProgressUpdateIntervalS int `xml:"progressUpdateIntervalS" json:"progressUpdateIntervalS" default:"5"`
  43. LimitBandwidthInLan bool `xml:"limitBandwidthInLan" json:"limitBandwidthInLan" default:"false"`
  44. MinHomeDiskFree Size `xml:"minHomeDiskFree" json:"minHomeDiskFree" default:"1 %"`
  45. ReleasesURL string `xml:"releasesURL" json:"releasesURL" default:"https://upgrades.syncthing.net/meta.json"`
  46. AlwaysLocalNets []string `xml:"alwaysLocalNet" json:"alwaysLocalNets"`
  47. OverwriteRemoteDevNames bool `xml:"overwriteRemoteDeviceNamesOnConnect" json:"overwriteRemoteDeviceNamesOnConnect" default:"false"`
  48. TempIndexMinBlocks int `xml:"tempIndexMinBlocks" json:"tempIndexMinBlocks" default:"10"`
  49. UnackedNotificationIDs []string `xml:"unackedNotificationID" json:"unackedNotificationIDs"`
  50. TrafficClass int `xml:"trafficClass" json:"trafficClass"`
  51. DefaultFolderPath string `xml:"defaultFolderPath" json:"defaultFolderPath" default:"~"`
  52. SetLowPriority bool `xml:"setLowPriority" json:"setLowPriority" default:"true"`
  53. RawMaxFolderConcurrency int `xml:"maxFolderConcurrency" json:"maxFolderConcurrency"`
  54. CRURL string `xml:"crashReportingURL" json:"crURL" default:"https://crash.syncthing.net/newcrash"` // crash reporting URL
  55. CREnabled bool `xml:"crashReportingEnabled" json:"crashReportingEnabled" default:"true"` // Read in the monitor, but it's read before every attempt to report stuff, so does not require a restart.
  56. StunKeepaliveStartS int `xml:"stunKeepaliveStartS" json:"stunKeepaliveStartS" default:"180"` // 0 for off
  57. StunKeepaliveMinS int `xml:"stunKeepaliveMinS" json:"stunKeepaliveMinS" default:"20"` // 0 for off
  58. RawStunServers []string `xml:"stunServer" json:"stunServers" default:"default"`
  59. DatabaseTuning Tuning `xml:"databaseTuning" json:"databaseTuning" restart:"true"` // Can't be adjusted once the database has been opened
  60. RawMaxCIRequestKiB int `xml:"maxConcurrentIncomingRequestKiB" json:"maxConcurrentIncomingRequestKiB"`
  61. DeprecatedUPnPEnabled bool `xml:"upnpEnabled,omitempty" json:"-"`
  62. DeprecatedUPnPLeaseM int `xml:"upnpLeaseMinutes,omitempty" json:"-"`
  63. DeprecatedUPnPRenewalM int `xml:"upnpRenewalMinutes,omitempty" json:"-"`
  64. DeprecatedUPnPTimeoutS int `xml:"upnpTimeoutSeconds,omitempty" json:"-"`
  65. DeprecatedRelayServers []string `xml:"relayServer,omitempty" json:"-"`
  66. DeprecatedMinHomeDiskFreePct float64 `xml:"minHomeDiskFreePct,omitempty" json:"-"`
  67. DeprecatedMaxConcurrentScans int `xml:"maxConcurrentScans,omitempty" json:"-"`
  68. }
  69. func (opts OptionsConfiguration) Copy() OptionsConfiguration {
  70. optsCopy := opts
  71. optsCopy.RawListenAddresses = make([]string, len(opts.RawListenAddresses))
  72. copy(optsCopy.RawListenAddresses, opts.RawListenAddresses)
  73. optsCopy.RawGlobalAnnServers = make([]string, len(opts.RawGlobalAnnServers))
  74. copy(optsCopy.RawGlobalAnnServers, opts.RawGlobalAnnServers)
  75. optsCopy.AlwaysLocalNets = make([]string, len(opts.AlwaysLocalNets))
  76. copy(optsCopy.AlwaysLocalNets, opts.AlwaysLocalNets)
  77. optsCopy.UnackedNotificationIDs = make([]string, len(opts.UnackedNotificationIDs))
  78. copy(optsCopy.UnackedNotificationIDs, opts.UnackedNotificationIDs)
  79. return optsCopy
  80. }
  81. // RequiresRestartOnly returns a copy with only the attributes that require
  82. // restart on change.
  83. func (opts OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
  84. optsCopy := opts
  85. blank := OptionsConfiguration{}
  86. util.CopyMatchingTag(&blank, &optsCopy, "restart", func(v string) bool {
  87. if len(v) > 0 && v != "true" {
  88. panic(fmt.Sprintf(`unexpected tag value: %s. Expected untagged or "true"`, v))
  89. }
  90. return v != "true"
  91. })
  92. return optsCopy
  93. }
  94. func (opts OptionsConfiguration) IsStunDisabled() bool {
  95. return opts.StunKeepaliveMinS < 1 || opts.StunKeepaliveStartS < 1 || !opts.NATEnabled
  96. }
  97. func (opts OptionsConfiguration) ListenAddresses() []string {
  98. var addresses []string
  99. for _, addr := range opts.RawListenAddresses {
  100. switch addr {
  101. case "default":
  102. addresses = append(addresses, DefaultListenAddresses...)
  103. default:
  104. addresses = append(addresses, addr)
  105. }
  106. }
  107. return util.UniqueTrimmedStrings(addresses)
  108. }
  109. func (opts OptionsConfiguration) StunServers() []string {
  110. var addresses []string
  111. for _, addr := range opts.RawStunServers {
  112. switch addr {
  113. case "default":
  114. defaultPrimaryAddresses := make([]string, len(DefaultPrimaryStunServers))
  115. copy(defaultPrimaryAddresses, DefaultPrimaryStunServers)
  116. rand.Shuffle(defaultPrimaryAddresses)
  117. addresses = append(addresses, defaultPrimaryAddresses...)
  118. defaultSecondaryAddresses := make([]string, len(DefaultSecondaryStunServers))
  119. copy(defaultSecondaryAddresses, DefaultSecondaryStunServers)
  120. rand.Shuffle(defaultSecondaryAddresses)
  121. addresses = append(addresses, defaultSecondaryAddresses...)
  122. default:
  123. addresses = append(addresses, addr)
  124. }
  125. }
  126. addresses = util.UniqueTrimmedStrings(addresses)
  127. return addresses
  128. }
  129. func (opts OptionsConfiguration) GlobalDiscoveryServers() []string {
  130. var servers []string
  131. for _, srv := range opts.RawGlobalAnnServers {
  132. switch srv {
  133. case "default":
  134. servers = append(servers, DefaultDiscoveryServers...)
  135. case "default-v4":
  136. servers = append(servers, DefaultDiscoveryServersV4...)
  137. case "default-v6":
  138. servers = append(servers, DefaultDiscoveryServersV6...)
  139. default:
  140. servers = append(servers, srv)
  141. }
  142. }
  143. return util.UniqueTrimmedStrings(servers)
  144. }
  145. func (opts OptionsConfiguration) MaxFolderConcurrency() int {
  146. // If a value is set, trust that.
  147. if opts.RawMaxFolderConcurrency > 0 {
  148. return opts.RawMaxFolderConcurrency
  149. }
  150. if opts.RawMaxFolderConcurrency < 0 {
  151. // -1 etc means unlimited, which in the implementation means zero
  152. return 0
  153. }
  154. // Otherwise default to the number of CPU cores in the system as a rough
  155. // approximation of system powerfullness.
  156. if n := runtime.GOMAXPROCS(-1); n > 0 {
  157. return n
  158. }
  159. // We should never get here to begin with, but since we're here let's
  160. // use some sort of reasonable compromise between the old "no limit" and
  161. // getting nothing done... (Median number of folders out there at time
  162. // of writing is two, 95-percentile at 12 folders.)
  163. return 4 // https://xkcd.com/221/
  164. }
  165. func (opts OptionsConfiguration) MaxConcurrentIncomingRequestKiB() int {
  166. // Negative is disabled, which in limiter land is spelled zero
  167. if opts.RawMaxCIRequestKiB < 0 {
  168. return 0
  169. }
  170. if opts.RawMaxFolderConcurrency == 0 {
  171. // The default is 256 MiB
  172. return 256 * 1024 // KiB
  173. }
  174. // We can't really do less than a couple of concurrent blocks or we'll
  175. // pretty much stall completely. Check that an explicit value is large
  176. // enough.
  177. const minAllowed = 2 * protocol.MaxBlockSize / 1024
  178. if opts.RawMaxCIRequestKiB < minAllowed {
  179. return minAllowed
  180. }
  181. // Roll with it.
  182. return opts.RawMaxCIRequestKiB
  183. }
  184. func (opts OptionsConfiguration) AutoUpgradeEnabled() bool {
  185. return opts.AutoUpgradeIntervalH > 0
  186. }