optionsconfiguration.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "github.com/syncthing/syncthing/lib/rand"
  10. "github.com/syncthing/syncthing/lib/util"
  11. )
  12. type OptionsConfiguration struct {
  13. RawListenAddresses []string `xml:"listenAddress" json:"listenAddresses" default:"default"`
  14. RawGlobalAnnServers []string `xml:"globalAnnounceServer" json:"globalAnnounceServers" default:"default" restart:"true"`
  15. GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" json:"globalAnnounceEnabled" default:"true" restart:"true"`
  16. LocalAnnEnabled bool `xml:"localAnnounceEnabled" json:"localAnnounceEnabled" default:"true" restart:"true"`
  17. LocalAnnPort int `xml:"localAnnouncePort" json:"localAnnouncePort" default:"21027" restart:"true"`
  18. LocalAnnMCAddr string `xml:"localAnnounceMCAddr" json:"localAnnounceMCAddr" default:"[ff12::8384]:21027" restart:"true"`
  19. MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
  20. MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
  21. ReconnectIntervalS int `xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60"`
  22. RelaysEnabled bool `xml:"relaysEnabled" json:"relaysEnabled" default:"true"`
  23. RelayReconnectIntervalM int `xml:"relayReconnectIntervalM" json:"relayReconnectIntervalM" default:"10"`
  24. StartBrowser bool `xml:"startBrowser" json:"startBrowser" default:"true"`
  25. NATEnabled bool `xml:"natEnabled" json:"natEnabled" default:"true"`
  26. NATLeaseM int `xml:"natLeaseMinutes" json:"natLeaseMinutes" default:"60"`
  27. NATRenewalM int `xml:"natRenewalMinutes" json:"natRenewalMinutes" default:"30"`
  28. NATTimeoutS int `xml:"natTimeoutSeconds" json:"natTimeoutSeconds" default:"10"`
  29. URAccepted int `xml:"urAccepted" json:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
  30. URSeen int `xml:"urSeen" json:"urSeen"` // Report which the user has been prompted for.
  31. URUniqueID string `xml:"urUniqueID" json:"urUniqueId"` // Unique ID for reporting purposes, regenerated when UR is turned on.
  32. URURL string `xml:"urURL" json:"urURL" default:"https://data.syncthing.net/newdata"` // usage reporting URL
  33. URPostInsecurely bool `xml:"urPostInsecurely" json:"urPostInsecurely" default:"false"` // For testing
  34. URInitialDelayS int `xml:"urInitialDelayS" json:"urInitialDelayS" default:"1800"`
  35. RestartOnWakeup bool `xml:"restartOnWakeup" json:"restartOnWakeup" default:"true" restart:"true"`
  36. AutoUpgradeIntervalH int `xml:"autoUpgradeIntervalH" json:"autoUpgradeIntervalH" default:"12" restart:"true"` // 0 for off
  37. UpgradeToPreReleases bool `xml:"upgradeToPreReleases" json:"upgradeToPreReleases" restart:"true"` // when auto upgrades are enabled
  38. KeepTemporariesH int `xml:"keepTemporariesH" json:"keepTemporariesH" default:"24"` // 0 for off
  39. CacheIgnoredFiles bool `xml:"cacheIgnoredFiles" json:"cacheIgnoredFiles" default:"false" restart:"true"`
  40. ProgressUpdateIntervalS int `xml:"progressUpdateIntervalS" json:"progressUpdateIntervalS" default:"5"`
  41. LimitBandwidthInLan bool `xml:"limitBandwidthInLan" json:"limitBandwidthInLan" default:"false"`
  42. MinHomeDiskFree Size `xml:"minHomeDiskFree" json:"minHomeDiskFree" default:"1 %"`
  43. ReleasesURL string `xml:"releasesURL" json:"releasesURL" default:"https://upgrades.syncthing.net/meta.json" restart:"true"`
  44. AlwaysLocalNets []string `xml:"alwaysLocalNet" json:"alwaysLocalNets"`
  45. OverwriteRemoteDevNames bool `xml:"overwriteRemoteDeviceNamesOnConnect" json:"overwriteRemoteDeviceNamesOnConnect" default:"false"`
  46. TempIndexMinBlocks int `xml:"tempIndexMinBlocks" json:"tempIndexMinBlocks" default:"10"`
  47. UnackedNotificationIDs []string `xml:"unackedNotificationID" json:"unackedNotificationIDs"`
  48. TrafficClass int `xml:"trafficClass" json:"trafficClass"`
  49. DefaultFolderPath string `xml:"defaultFolderPath" json:"defaultFolderPath" default:"~"`
  50. SetLowPriority bool `xml:"setLowPriority" json:"setLowPriority" default:"true"`
  51. MaxConcurrentScans int `xml:"maxConcurrentScans" json:"maxConcurrentScans"`
  52. CRURL string `xml:"crashReportingURL" json:"crURL" default:"https://crash.syncthing.net/newcrash"` // crash reporting URL
  53. CREnabled bool `xml:"crashReportingEnabled" json:"crashReportingEnabled" default:"true" restart:"true"`
  54. StunKeepaliveStartS int `xml:"stunKeepaliveStartS" json:"stunKeepaliveStartS" default:"180"` // 0 for off
  55. StunKeepaliveMinS int `xml:"stunKeepaliveMinS" json:"stunKeepaliveMinS" default:"20"` // 0 for off
  56. RawStunServers []string `xml:"stunServer" json:"stunServers" default:"default"`
  57. DatabaseTuning Tuning `xml:"databaseTuning" json:"databaseTuning" restart:"true"`
  58. DeprecatedUPnPEnabled bool `xml:"upnpEnabled,omitempty" json:"-"`
  59. DeprecatedUPnPLeaseM int `xml:"upnpLeaseMinutes,omitempty" json:"-"`
  60. DeprecatedUPnPRenewalM int `xml:"upnpRenewalMinutes,omitempty" json:"-"`
  61. DeprecatedUPnPTimeoutS int `xml:"upnpTimeoutSeconds,omitempty" json:"-"`
  62. DeprecatedRelayServers []string `xml:"relayServer,omitempty" json:"-"`
  63. DeprecatedMinHomeDiskFreePct float64 `xml:"minHomeDiskFreePct,omitempty" json:"-"`
  64. }
  65. func (opts OptionsConfiguration) Copy() OptionsConfiguration {
  66. optsCopy := opts
  67. optsCopy.RawListenAddresses = make([]string, len(opts.RawListenAddresses))
  68. copy(optsCopy.RawListenAddresses, opts.RawListenAddresses)
  69. optsCopy.RawGlobalAnnServers = make([]string, len(opts.RawGlobalAnnServers))
  70. copy(optsCopy.RawGlobalAnnServers, opts.RawGlobalAnnServers)
  71. optsCopy.AlwaysLocalNets = make([]string, len(opts.AlwaysLocalNets))
  72. copy(optsCopy.AlwaysLocalNets, opts.AlwaysLocalNets)
  73. optsCopy.UnackedNotificationIDs = make([]string, len(opts.UnackedNotificationIDs))
  74. copy(optsCopy.UnackedNotificationIDs, opts.UnackedNotificationIDs)
  75. return optsCopy
  76. }
  77. // RequiresRestartOnly returns a copy with only the attributes that require
  78. // restart on change.
  79. func (opts OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
  80. optsCopy := opts
  81. blank := OptionsConfiguration{}
  82. util.CopyMatchingTag(&blank, &optsCopy, "restart", func(v string) bool {
  83. if len(v) > 0 && v != "true" {
  84. panic(fmt.Sprintf(`unexpected tag value: %s. Expected untagged or "true"`, v))
  85. }
  86. return v != "true"
  87. })
  88. return optsCopy
  89. }
  90. func (opts OptionsConfiguration) IsStunDisabled() bool {
  91. return opts.StunKeepaliveMinS < 1 || opts.StunKeepaliveStartS < 1 || !opts.NATEnabled
  92. }
  93. func (opts OptionsConfiguration) ListenAddresses() []string {
  94. var addresses []string
  95. for _, addr := range opts.RawListenAddresses {
  96. switch addr {
  97. case "default":
  98. addresses = append(addresses, DefaultListenAddresses...)
  99. default:
  100. addresses = append(addresses, addr)
  101. }
  102. }
  103. return util.UniqueTrimmedStrings(addresses)
  104. }
  105. func (opts OptionsConfiguration) StunServers() []string {
  106. var addresses []string
  107. for _, addr := range opts.RawStunServers {
  108. switch addr {
  109. case "default":
  110. defaultPrimaryAddresses := make([]string, len(DefaultPrimaryStunServers))
  111. copy(defaultPrimaryAddresses, DefaultPrimaryStunServers)
  112. rand.Shuffle(defaultPrimaryAddresses)
  113. addresses = append(addresses, defaultPrimaryAddresses...)
  114. defaultSecondaryAddresses := make([]string, len(DefaultSecondaryStunServers))
  115. copy(defaultSecondaryAddresses, DefaultSecondaryStunServers)
  116. rand.Shuffle(defaultSecondaryAddresses)
  117. addresses = append(addresses, defaultSecondaryAddresses...)
  118. default:
  119. addresses = append(addresses, addr)
  120. }
  121. }
  122. addresses = util.UniqueTrimmedStrings(addresses)
  123. return addresses
  124. }
  125. func (opts OptionsConfiguration) GlobalDiscoveryServers() []string {
  126. var servers []string
  127. for _, srv := range opts.RawGlobalAnnServers {
  128. switch srv {
  129. case "default":
  130. servers = append(servers, DefaultDiscoveryServers...)
  131. case "default-v4":
  132. servers = append(servers, DefaultDiscoveryServersV4...)
  133. case "default-v6":
  134. servers = append(servers, DefaultDiscoveryServersV6...)
  135. default:
  136. servers = append(servers, srv)
  137. }
  138. }
  139. return util.UniqueTrimmedStrings(servers)
  140. }