1
0

optionsconfiguration.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "encoding/json"
  9. "encoding/xml"
  10. "fmt"
  11. "github.com/syncthing/syncthing/lib/util"
  12. )
  13. type WeakHashSelectionMethod int
  14. const (
  15. WeakHashAuto WeakHashSelectionMethod = iota
  16. WeakHashAlways
  17. WeakHashNever
  18. )
  19. func (m WeakHashSelectionMethod) MarshalString() (string, error) {
  20. switch m {
  21. case WeakHashAuto:
  22. return "auto", nil
  23. case WeakHashAlways:
  24. return "always", nil
  25. case WeakHashNever:
  26. return "never", nil
  27. default:
  28. return "", fmt.Errorf("unrecognized hash selection method")
  29. }
  30. }
  31. func (m WeakHashSelectionMethod) String() string {
  32. s, err := m.MarshalString()
  33. if err != nil {
  34. panic(err)
  35. }
  36. return s
  37. }
  38. func (m *WeakHashSelectionMethod) UnmarshalString(value string) error {
  39. switch value {
  40. case "auto":
  41. *m = WeakHashAuto
  42. return nil
  43. case "always":
  44. *m = WeakHashAlways
  45. return nil
  46. case "never":
  47. *m = WeakHashNever
  48. return nil
  49. }
  50. return fmt.Errorf("unrecognized hash selection method")
  51. }
  52. func (m WeakHashSelectionMethod) MarshalJSON() ([]byte, error) {
  53. val, err := m.MarshalString()
  54. if err != nil {
  55. return nil, err
  56. }
  57. return json.Marshal(val)
  58. }
  59. func (m *WeakHashSelectionMethod) UnmarshalJSON(data []byte) error {
  60. var value string
  61. if err := json.Unmarshal(data, &value); err != nil {
  62. return err
  63. }
  64. return m.UnmarshalString(value)
  65. }
  66. func (m WeakHashSelectionMethod) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  67. val, err := m.MarshalString()
  68. if err != nil {
  69. return err
  70. }
  71. return e.EncodeElement(val, start)
  72. }
  73. func (m *WeakHashSelectionMethod) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  74. var value string
  75. if err := d.DecodeElement(&value, &start); err != nil {
  76. return err
  77. }
  78. return m.UnmarshalString(value)
  79. }
  80. func (WeakHashSelectionMethod) ParseDefault(value string) (interface{}, error) {
  81. var m WeakHashSelectionMethod
  82. err := m.UnmarshalString(value)
  83. return m, err
  84. }
  85. type OptionsConfiguration struct {
  86. ListenAddresses []string `xml:"listenAddress" json:"listenAddresses" default:"default"`
  87. GlobalAnnServers []string `xml:"globalAnnounceServer" json:"globalAnnounceServers" json:"globalAnnounceServer" default:"default" restart:"true"`
  88. GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" json:"globalAnnounceEnabled" default:"true" restart:"true"`
  89. LocalAnnEnabled bool `xml:"localAnnounceEnabled" json:"localAnnounceEnabled" default:"true" restart:"true"`
  90. LocalAnnPort int `xml:"localAnnouncePort" json:"localAnnouncePort" default:"21027" restart:"true"`
  91. LocalAnnMCAddr string `xml:"localAnnounceMCAddr" json:"localAnnounceMCAddr" default:"[ff12::8384]:21027" restart:"true"`
  92. MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
  93. MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
  94. ReconnectIntervalS int `xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60"`
  95. RelaysEnabled bool `xml:"relaysEnabled" json:"relaysEnabled" default:"true"`
  96. RelayReconnectIntervalM int `xml:"relayReconnectIntervalM" json:"relayReconnectIntervalM" default:"10"`
  97. StartBrowser bool `xml:"startBrowser" json:"startBrowser" default:"true"`
  98. NATEnabled bool `xml:"natEnabled" json:"natEnabled" default:"true"`
  99. NATLeaseM int `xml:"natLeaseMinutes" json:"natLeaseMinutes" default:"60"`
  100. NATRenewalM int `xml:"natRenewalMinutes" json:"natRenewalMinutes" default:"30"`
  101. NATTimeoutS int `xml:"natTimeoutSeconds" json:"natTimeoutSeconds" default:"10"`
  102. URAccepted int `xml:"urAccepted" json:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
  103. URSeen int `xml:"urSeen" json:"urSeen"` // Report which the user has been prompted for.
  104. URUniqueID string `xml:"urUniqueID" json:"urUniqueId"` // Unique ID for reporting purposes, regenerated when UR is turned on.
  105. URURL string `xml:"urURL" json:"urURL" default:"https://data.syncthing.net/newdata"`
  106. URPostInsecurely bool `xml:"urPostInsecurely" json:"urPostInsecurely" default:"false"` // For testing
  107. URInitialDelayS int `xml:"urInitialDelayS" json:"urInitialDelayS" default:"1800"`
  108. RestartOnWakeup bool `xml:"restartOnWakeup" json:"restartOnWakeup" default:"true" restart:"true"`
  109. AutoUpgradeIntervalH int `xml:"autoUpgradeIntervalH" json:"autoUpgradeIntervalH" default:"12" restart:"true"` // 0 for off
  110. UpgradeToPreReleases bool `xml:"upgradeToPreReleases" json:"upgradeToPreReleases" restart:"true"` // when auto upgrades are enabled
  111. KeepTemporariesH int `xml:"keepTemporariesH" json:"keepTemporariesH" default:"24"` // 0 for off
  112. CacheIgnoredFiles bool `xml:"cacheIgnoredFiles" json:"cacheIgnoredFiles" default:"false" restart:"true"`
  113. ProgressUpdateIntervalS int `xml:"progressUpdateIntervalS" json:"progressUpdateIntervalS" default:"5"`
  114. LimitBandwidthInLan bool `xml:"limitBandwidthInLan" json:"limitBandwidthInLan" default:"false"`
  115. MinHomeDiskFree Size `xml:"minHomeDiskFree" json:"minHomeDiskFree" default:"1 %"`
  116. ReleasesURL string `xml:"releasesURL" json:"releasesURL" default:"https://upgrades.syncthing.net/meta.json" restart:"true"`
  117. AlwaysLocalNets []string `xml:"alwaysLocalNet" json:"alwaysLocalNets"`
  118. OverwriteRemoteDevNames bool `xml:"overwriteRemoteDeviceNamesOnConnect" json:"overwriteRemoteDeviceNamesOnConnect" default:"false"`
  119. TempIndexMinBlocks int `xml:"tempIndexMinBlocks" json:"tempIndexMinBlocks" default:"10"`
  120. UnackedNotificationIDs []string `xml:"unackedNotificationID" json:"unackedNotificationIDs"`
  121. TrafficClass int `xml:"trafficClass" json:"trafficClass"`
  122. WeakHashSelectionMethod WeakHashSelectionMethod `xml:"weakHashSelectionMethod" json:"weakHashSelectionMethod" restart:"true"`
  123. StunServers []string `xml:"stunServer" json:"stunServers" default:"default"`
  124. StunKeepaliveS int `xml:"stunKeepaliveSeconds" json:"stunKeepaliveSeconds" default:"24"`
  125. KCPNoDelay bool `xml:"kcpNoDelay" json:"kcpNoDelay" default:"false"`
  126. KCPUpdateIntervalMs int `xml:"kcpUpdateIntervalMs" json:"kcpUpdateIntervalMs" default:"25"`
  127. KCPFastResend bool `xml:"kcpFastResend" json:"kcpFastResend" default:"false"`
  128. KCPCongestionControl bool `xml:"kcpCongestionControl" json:"kcpCongestionControl" default:"true"`
  129. KCPSendWindowSize int `xml:"kcpSendWindowSize" json:"kcpSendWindowSize" default:"128"`
  130. KCPReceiveWindowSize int `xml:"kcpReceiveWindowSize" json:"kcpReceiveWindowSize" default:"128"`
  131. DefaultFolderPath string `xml:"defaultFolderPath" json:"defaultFolderPath" default:"~"`
  132. DeprecatedUPnPEnabled bool `xml:"upnpEnabled,omitempty" json:"-"`
  133. DeprecatedUPnPLeaseM int `xml:"upnpLeaseMinutes,omitempty" json:"-"`
  134. DeprecatedUPnPRenewalM int `xml:"upnpRenewalMinutes,omitempty" json:"-"`
  135. DeprecatedUPnPTimeoutS int `xml:"upnpTimeoutSeconds,omitempty" json:"-"`
  136. DeprecatedRelayServers []string `xml:"relayServer,omitempty" json:"-"`
  137. DeprecatedMinHomeDiskFreePct float64 `xml:"minHomeDiskFreePct" json:"-"`
  138. }
  139. func (orig OptionsConfiguration) Copy() OptionsConfiguration {
  140. c := orig
  141. c.ListenAddresses = make([]string, len(orig.ListenAddresses))
  142. copy(c.ListenAddresses, orig.ListenAddresses)
  143. c.GlobalAnnServers = make([]string, len(orig.GlobalAnnServers))
  144. copy(c.GlobalAnnServers, orig.GlobalAnnServers)
  145. c.AlwaysLocalNets = make([]string, len(orig.AlwaysLocalNets))
  146. copy(c.AlwaysLocalNets, orig.AlwaysLocalNets)
  147. c.UnackedNotificationIDs = make([]string, len(orig.UnackedNotificationIDs))
  148. copy(c.UnackedNotificationIDs, orig.UnackedNotificationIDs)
  149. return c
  150. }
  151. // RequiresRestartOnly returns a copy with only the attributes that require
  152. // restart on change.
  153. func (orig OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
  154. copy := orig
  155. blank := OptionsConfiguration{}
  156. util.CopyMatchingTag(&blank, &copy, "restart", func(v string) bool {
  157. if len(v) > 0 && v != "true" {
  158. panic(fmt.Sprintf(`unexpected tag value: %s. expected untagged or "true"`, v))
  159. }
  160. return v != "true"
  161. })
  162. return copy
  163. }