optionsconfiguration.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. func (opts OptionsConfiguration) Copy() OptionsConfiguration {
  16. optsCopy := opts
  17. optsCopy.RawListenAddresses = make([]string, len(opts.RawListenAddresses))
  18. copy(optsCopy.RawListenAddresses, opts.RawListenAddresses)
  19. optsCopy.RawGlobalAnnServers = make([]string, len(opts.RawGlobalAnnServers))
  20. copy(optsCopy.RawGlobalAnnServers, opts.RawGlobalAnnServers)
  21. optsCopy.AlwaysLocalNets = make([]string, len(opts.AlwaysLocalNets))
  22. copy(optsCopy.AlwaysLocalNets, opts.AlwaysLocalNets)
  23. optsCopy.UnackedNotificationIDs = make([]string, len(opts.UnackedNotificationIDs))
  24. copy(optsCopy.UnackedNotificationIDs, opts.UnackedNotificationIDs)
  25. return optsCopy
  26. }
  27. func (opts *OptionsConfiguration) prepare(guiPWIsSet bool) {
  28. structutil.FillNilSlices(opts)
  29. opts.RawListenAddresses = stringutil.UniqueTrimmedStrings(opts.RawListenAddresses)
  30. opts.RawGlobalAnnServers = stringutil.UniqueTrimmedStrings(opts.RawGlobalAnnServers)
  31. // Very short reconnection intervals are annoying
  32. if opts.ReconnectIntervalS < 5 {
  33. opts.ReconnectIntervalS = 5
  34. }
  35. if guiPWIsSet && len(opts.UnackedNotificationIDs) > 0 {
  36. for i, key := range opts.UnackedNotificationIDs {
  37. if key == "authenticationUserAndPassword" {
  38. opts.UnackedNotificationIDs = append(opts.UnackedNotificationIDs[:i], opts.UnackedNotificationIDs[i+1:]...)
  39. break
  40. }
  41. }
  42. }
  43. // Negative limits are meaningless, zero means unlimited.
  44. if opts.ConnectionLimitEnough < 0 {
  45. opts.ConnectionLimitEnough = 0
  46. }
  47. if opts.ConnectionLimitMax < 0 {
  48. opts.ConnectionLimitMax = 0
  49. }
  50. if opts.ConnectionPriorityQUICWAN <= opts.ConnectionPriorityQUICLAN {
  51. l.Warnln("Connection priority number for QUIC over WAN must be worse (higher) than QUIC over LAN. Correcting.")
  52. opts.ConnectionPriorityQUICWAN = opts.ConnectionPriorityQUICLAN + 1
  53. }
  54. if opts.ConnectionPriorityTCPWAN <= opts.ConnectionPriorityTCPLAN {
  55. l.Warnln("Connection priority number for TCP over WAN must be worse (higher) than TCP over LAN. Correcting.")
  56. opts.ConnectionPriorityTCPWAN = opts.ConnectionPriorityTCPLAN + 1
  57. }
  58. }
  59. // RequiresRestartOnly returns a copy with only the attributes that require
  60. // restart on change.
  61. func (opts OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
  62. optsCopy := opts
  63. blank := OptionsConfiguration{}
  64. copyMatchingTag(&blank, &optsCopy, "restart", func(v string) bool {
  65. if len(v) > 0 && v != "true" {
  66. panic(fmt.Sprintf(`unexpected tag value: %s. Expected untagged or "true"`, v))
  67. }
  68. return v != "true"
  69. })
  70. return optsCopy
  71. }
  72. func (opts OptionsConfiguration) IsStunDisabled() bool {
  73. return opts.StunKeepaliveMinS < 1 || opts.StunKeepaliveStartS < 1 || !opts.NATEnabled
  74. }
  75. func (opts OptionsConfiguration) ListenAddresses() []string {
  76. var addresses []string
  77. for _, addr := range opts.RawListenAddresses {
  78. switch addr {
  79. case "default":
  80. addresses = append(addresses, DefaultListenAddresses...)
  81. default:
  82. addresses = append(addresses, addr)
  83. }
  84. }
  85. return stringutil.UniqueTrimmedStrings(addresses)
  86. }
  87. func (opts OptionsConfiguration) StunServers() []string {
  88. var addresses []string
  89. for _, addr := range opts.RawStunServers {
  90. switch addr {
  91. case "default":
  92. defaultPrimaryAddresses := make([]string, len(DefaultPrimaryStunServers))
  93. copy(defaultPrimaryAddresses, DefaultPrimaryStunServers)
  94. rand.Shuffle(defaultPrimaryAddresses)
  95. addresses = append(addresses, defaultPrimaryAddresses...)
  96. defaultSecondaryAddresses := make([]string, len(DefaultSecondaryStunServers))
  97. copy(defaultSecondaryAddresses, DefaultSecondaryStunServers)
  98. rand.Shuffle(defaultSecondaryAddresses)
  99. addresses = append(addresses, defaultSecondaryAddresses...)
  100. default:
  101. addresses = append(addresses, addr)
  102. }
  103. }
  104. addresses = stringutil.UniqueTrimmedStrings(addresses)
  105. return addresses
  106. }
  107. func (opts OptionsConfiguration) GlobalDiscoveryServers() []string {
  108. var servers []string
  109. for _, srv := range opts.RawGlobalAnnServers {
  110. switch srv {
  111. case "default":
  112. servers = append(servers, DefaultDiscoveryServers...)
  113. case "default-v4":
  114. servers = append(servers, DefaultDiscoveryServersV4...)
  115. case "default-v6":
  116. servers = append(servers, DefaultDiscoveryServersV6...)
  117. default:
  118. servers = append(servers, srv)
  119. }
  120. }
  121. return stringutil.UniqueTrimmedStrings(servers)
  122. }
  123. func (opts OptionsConfiguration) MaxFolderConcurrency() int {
  124. // If a value is set, trust that.
  125. if opts.RawMaxFolderConcurrency > 0 {
  126. return opts.RawMaxFolderConcurrency
  127. }
  128. if opts.RawMaxFolderConcurrency < 0 {
  129. // -1 etc means unlimited, which in the implementation means zero
  130. return 0
  131. }
  132. // Otherwise default to the number of CPU cores in the system as a rough
  133. // approximation of system powerfullness.
  134. if n := runtime.GOMAXPROCS(-1); n > 0 {
  135. return n
  136. }
  137. // We should never get here to begin with, but since we're here let's
  138. // use some sort of reasonable compromise between the old "no limit" and
  139. // getting nothing done... (Median number of folders out there at time
  140. // of writing is two, 95-percentile at 12 folders.)
  141. return 4 // https://xkcd.com/221/
  142. }
  143. func (opts OptionsConfiguration) MaxConcurrentIncomingRequestKiB() int {
  144. // Negative is disabled, which in limiter land is spelled zero
  145. if opts.RawMaxCIRequestKiB < 0 {
  146. return 0
  147. }
  148. if opts.RawMaxCIRequestKiB == 0 {
  149. // The default is 256 MiB
  150. return 256 * 1024 // KiB
  151. }
  152. // We can't really do less than a couple of concurrent blocks or we'll
  153. // pretty much stall completely. Check that an explicit value is large
  154. // enough.
  155. const minAllowed = 2 * protocol.MaxBlockSize / 1024
  156. if opts.RawMaxCIRequestKiB < minAllowed {
  157. return minAllowed
  158. }
  159. // Roll with it.
  160. return opts.RawMaxCIRequestKiB
  161. }
  162. func (opts OptionsConfiguration) AutoUpgradeEnabled() bool {
  163. return opts.AutoUpgradeIntervalH > 0
  164. }
  165. func (opts OptionsConfiguration) FeatureFlag(name string) bool {
  166. for _, flag := range opts.FeatureFlags {
  167. if flag == name {
  168. return true
  169. }
  170. }
  171. return false
  172. }
  173. // LowestConnectionLimit is the lower of ConnectionLimitEnough or
  174. // ConnectionLimitMax, or whichever of them is actually set if only one of
  175. // them is set. It's the point where we should stop dialing.
  176. func (opts OptionsConfiguration) LowestConnectionLimit() int {
  177. limit := opts.ConnectionLimitEnough
  178. if limit == 0 || (opts.ConnectionLimitMax != 0 && opts.ConnectionLimitMax < limit) {
  179. // It doesn't really make sense to set Max lower than Enough but
  180. // someone might do it while experimenting and it's easy for us to
  181. // do the right thing.
  182. limit = opts.ConnectionLimitMax
  183. }
  184. return limit
  185. }