optionsconfiguration.go 6.3 KB

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