clientParameters.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /*
  20. Package parameters implements dynamic, concurrency-safe parameters that
  21. determine Psiphon client behavior.
  22. Parameters include network timeouts, probabilities for actions, lists of
  23. protocols, etc. Parameters are initialized with reasonable defaults. New
  24. values may be applied, allowing the client to customized its parameters from
  25. both a config file and tactics data. Sane minimum values are enforced.
  26. Parameters may be read and updated concurrently. The read mechanism offers a
  27. snapshot so that related parameters, such as two Ints representing a range; or
  28. a more complex series of related parameters; may be read in an atomic and
  29. consistent way. For example:
  30. p := clientParameters.Get()
  31. min := p.Int("Min")
  32. max := p.Int("Max")
  33. p = nil
  34. For long-running operations, it is recommended to set any pointer to the
  35. snapshot to nil to allow garbage collection of old snaphots in cases where the
  36. parameters change.
  37. In general, client parameters should be read as close to the point of use as
  38. possible to ensure that dynamic changes to the parameter values take effect.
  39. For duration parameters, time.ParseDuration-compatible string values are
  40. supported when applying new values. This allows specifying durations as, for
  41. example, "100ms" or "24h".
  42. Values read from the parameters are not deep copies and must be treated as
  43. read-only.
  44. */
  45. package parameters
  46. import (
  47. "encoding/json"
  48. "fmt"
  49. "net/http"
  50. "reflect"
  51. "sync/atomic"
  52. "time"
  53. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  54. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/obfuscator"
  55. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  56. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  57. )
  58. const (
  59. NetworkLatencyMultiplier = "NetworkLatencyMultiplier"
  60. TacticsWaitPeriod = "TacticsWaitPeriod"
  61. TacticsRetryPeriod = "TacticsRetryPeriod"
  62. TacticsRetryPeriodJitter = "TacticsRetryPeriodJitter"
  63. TacticsTimeout = "TacticsTimeout"
  64. ConnectionWorkerPoolSize = "ConnectionWorkerPoolSize"
  65. TunnelConnectTimeout = "TunnelConnectTimeout"
  66. EstablishTunnelTimeout = "EstablishTunnelTimeout"
  67. EstablishTunnelWorkTime = "EstablishTunnelWorkTime"
  68. EstablishTunnelPausePeriod = "EstablishTunnelPausePeriod"
  69. EstablishTunnelPausePeriodJitter = "EstablishTunnelPausePeriodJitter"
  70. EstablishTunnelServerAffinityGracePeriod = "EstablishTunnelServerAffinityGracePeriod"
  71. StaggerConnectionWorkersPeriod = "StaggerConnectionWorkersPeriod"
  72. StaggerConnectionWorkersJitter = "StaggerConnectionWorkersJitter"
  73. LimitIntensiveConnectionWorkers = "LimitIntensiveConnectionWorkers"
  74. IgnoreHandshakeStatsRegexps = "IgnoreHandshakeStatsRegexps"
  75. PrioritizeTunnelProtocolsProbability = "PrioritizeTunnelProtocolsProbability"
  76. PrioritizeTunnelProtocols = "PrioritizeTunnelProtocols"
  77. PrioritizeTunnelProtocolsCandidateCount = "PrioritizeTunnelProtocolsCandidateCount"
  78. InitialLimitTunnelProtocolsProbability = "InitialLimitTunnelProtocolsProbability"
  79. InitialLimitTunnelProtocols = "InitialLimitTunnelProtocols"
  80. InitialLimitTunnelProtocolsCandidateCount = "InitialLimitTunnelProtocolsCandidateCount"
  81. LimitTunnelProtocolsProbability = "LimitTunnelProtocolsProbability"
  82. LimitTunnelProtocols = "LimitTunnelProtocols"
  83. LimitTLSProfilesProbability = "LimitTLSProfilesProbability"
  84. LimitTLSProfiles = "LimitTLSProfiles"
  85. SelectRandomizedTLSProfileProbability = "SelectRandomizedTLSProfileProbability"
  86. LimitQUICVersionsProbability = "LimitQUICVersionsProbability"
  87. LimitQUICVersions = "LimitQUICVersions"
  88. FragmentorProbability = "FragmentorProbability"
  89. FragmentorLimitProtocols = "FragmentorLimitProtocols"
  90. FragmentorMinTotalBytes = "FragmentorMinTotalBytes"
  91. FragmentorMaxTotalBytes = "FragmentorMaxTotalBytes"
  92. FragmentorMinWriteBytes = "FragmentorMinWriteBytes"
  93. FragmentorMaxWriteBytes = "FragmentorMaxWriteBytes"
  94. FragmentorMinDelay = "FragmentorMinDelay"
  95. FragmentorMaxDelay = "FragmentorMaxDelay"
  96. FragmentorDownstreamProbability = "FragmentorDownstreamProbability"
  97. FragmentorDownstreamLimitProtocols = "FragmentorDownstreamLimitProtocols"
  98. FragmentorDownstreamMinTotalBytes = "FragmentorDownstreamMinTotalBytes"
  99. FragmentorDownstreamMaxTotalBytes = "FragmentorDownstreamMaxTotalBytes"
  100. FragmentorDownstreamMinWriteBytes = "FragmentorDownstreamMinWriteBytes"
  101. FragmentorDownstreamMaxWriteBytes = "FragmentorDownstreamMaxWriteBytes"
  102. FragmentorDownstreamMinDelay = "FragmentorDownstreamMinDelay"
  103. FragmentorDownstreamMaxDelay = "FragmentorDownstreamMaxDelay"
  104. ObfuscatedSSHMinPadding = "ObfuscatedSSHMinPadding"
  105. ObfuscatedSSHMaxPadding = "ObfuscatedSSHMaxPadding"
  106. TunnelOperateShutdownTimeout = "TunnelOperateShutdownTimeout"
  107. TunnelPortForwardDialTimeout = "TunnelPortForwardDialTimeout"
  108. TunnelRateLimits = "TunnelRateLimits"
  109. AdditionalCustomHeaders = "AdditionalCustomHeaders"
  110. SpeedTestPaddingMinBytes = "SpeedTestPaddingMinBytes"
  111. SpeedTestPaddingMaxBytes = "SpeedTestPaddingMaxBytes"
  112. SpeedTestMaxSampleCount = "SpeedTestMaxSampleCount"
  113. SSHKeepAliveSpeedTestSampleProbability = "SSHKeepAliveSpeedTestSampleProbability"
  114. SSHKeepAlivePaddingMinBytes = "SSHKeepAlivePaddingMinBytes"
  115. SSHKeepAlivePaddingMaxBytes = "SSHKeepAlivePaddingMaxBytes"
  116. SSHKeepAlivePeriodMin = "SSHKeepAlivePeriodMin"
  117. SSHKeepAlivePeriodMax = "SSHKeepAlivePeriodMax"
  118. SSHKeepAlivePeriodicTimeout = "SSHKeepAlivePeriodicTimeout"
  119. SSHKeepAlivePeriodicInactivePeriod = "SSHKeepAlivePeriodicInactivePeriod"
  120. SSHKeepAliveProbeTimeout = "SSHKeepAliveProbeTimeout"
  121. SSHKeepAliveProbeInactivePeriod = "SSHKeepAliveProbeInactivePeriod"
  122. HTTPProxyOriginServerTimeout = "HTTPProxyOriginServerTimeout"
  123. HTTPProxyMaxIdleConnectionsPerHost = "HTTPProxyMaxIdleConnectionsPerHost"
  124. FetchRemoteServerListTimeout = "FetchRemoteServerListTimeout"
  125. FetchRemoteServerListRetryPeriod = "FetchRemoteServerListRetryPeriod"
  126. FetchRemoteServerListStalePeriod = "FetchRemoteServerListStalePeriod"
  127. RemoteServerListSignaturePublicKey = "RemoteServerListSignaturePublicKey"
  128. RemoteServerListURLs = "RemoteServerListURLs"
  129. ObfuscatedServerListRootURLs = "ObfuscatedServerListRootURLs"
  130. PsiphonAPIRequestTimeout = "PsiphonAPIRequestTimeout"
  131. PsiphonAPIStatusRequestPeriodMin = "PsiphonAPIStatusRequestPeriodMin"
  132. PsiphonAPIStatusRequestPeriodMax = "PsiphonAPIStatusRequestPeriodMax"
  133. PsiphonAPIStatusRequestShortPeriodMin = "PsiphonAPIStatusRequestShortPeriodMin"
  134. PsiphonAPIStatusRequestShortPeriodMax = "PsiphonAPIStatusRequestShortPeriodMax"
  135. PsiphonAPIStatusRequestPaddingMinBytes = "PsiphonAPIStatusRequestPaddingMinBytes"
  136. PsiphonAPIStatusRequestPaddingMaxBytes = "PsiphonAPIStatusRequestPaddingMaxBytes"
  137. PsiphonAPIPersistentStatsMaxCount = "PsiphonAPIPersistentStatsMaxCount"
  138. PsiphonAPIConnectedRequestPeriod = "PsiphonAPIConnectedRequestPeriod"
  139. PsiphonAPIConnectedRequestRetryPeriod = "PsiphonAPIConnectedRequestRetryPeriod"
  140. FetchSplitTunnelRoutesTimeout = "FetchSplitTunnelRoutesTimeout"
  141. SplitTunnelRoutesURLFormat = "SplitTunnelRoutesURLFormat"
  142. SplitTunnelRoutesSignaturePublicKey = "SplitTunnelRoutesSignaturePublicKey"
  143. SplitTunnelDNSServer = "SplitTunnelDNSServer"
  144. FetchUpgradeTimeout = "FetchUpgradeTimeout"
  145. FetchUpgradeRetryPeriod = "FetchUpgradeRetryPeriod"
  146. FetchUpgradeStalePeriod = "FetchUpgradeStalePeriod"
  147. UpgradeDownloadURLs = "UpgradeDownloadURLs"
  148. UpgradeDownloadClientVersionHeader = "UpgradeDownloadClientVersionHeader"
  149. TotalBytesTransferredNoticePeriod = "TotalBytesTransferredNoticePeriod"
  150. MeekDialDomainsOnly = "MeekDialDomainsOnly"
  151. MeekLimitBufferSizes = "MeekLimitBufferSizes"
  152. MeekCookieMaxPadding = "MeekCookieMaxPadding"
  153. MeekFullReceiveBufferLength = "MeekFullReceiveBufferLength"
  154. MeekReadPayloadChunkLength = "MeekReadPayloadChunkLength"
  155. MeekLimitedFullReceiveBufferLength = "MeekLimitedFullReceiveBufferLength"
  156. MeekLimitedReadPayloadChunkLength = "MeekLimitedReadPayloadChunkLength"
  157. MeekMinPollInterval = "MeekMinPollInterval"
  158. MeekMinPollIntervalJitter = "MeekMinPollIntervalJitter"
  159. MeekMaxPollInterval = "MeekMaxPollInterval"
  160. MeekMaxPollIntervalJitter = "MeekMaxPollIntervalJitter"
  161. MeekPollIntervalMultiplier = "MeekPollIntervalMultiplier"
  162. MeekPollIntervalJitter = "MeekPollIntervalJitter"
  163. MeekApplyPollIntervalMultiplierProbability = "MeekApplyPollIntervalMultiplierProbability"
  164. MeekRoundTripRetryDeadline = "MeekRoundTripRetryDeadline"
  165. MeekRoundTripRetryMinDelay = "MeekRoundTripRetryMinDelay"
  166. MeekRoundTripRetryMaxDelay = "MeekRoundTripRetryMaxDelay"
  167. MeekRoundTripRetryMultiplier = "MeekRoundTripRetryMultiplier"
  168. MeekRoundTripTimeout = "MeekRoundTripTimeout"
  169. TransformHostNameProbability = "TransformHostNameProbability"
  170. PickUserAgentProbability = "PickUserAgentProbability"
  171. LivenessTestMinUpstreamBytes = "LivenessTestMinUpstreamBytes"
  172. LivenessTestMaxUpstreamBytes = "LivenessTestMaxUpstreamBytes"
  173. LivenessTestMinDownstreamBytes = "LivenessTestMinDownstreamBytes"
  174. LivenessTestMaxDownstreamBytes = "LivenessTestMaxDownstreamBytes"
  175. ReplayCandidateCount = "ReplayCandidateCount"
  176. ReplayDialParametersTTL = "ReplayDialParametersTTL"
  177. ReplayTargetUpstreamBytes = "ReplayTargetUpstreamBytes"
  178. ReplayTargetDownstreamBytes = "ReplayTargetDownstreamBytes"
  179. ReplaySSH = "ReplaySSH"
  180. ReplayObfuscatorPadding = "ReplayObfuscatorPadding"
  181. ReplayFragmentor = "ReplayFragmentor"
  182. ReplayTLSProfile = "ReplayTLSProfile"
  183. ReplayRandomizedTLSProfile = "ReplayRandomizedTLSProfile"
  184. ReplayFronting = "ReplayFronting"
  185. ReplayHostname = "ReplayHostname"
  186. ReplayQUICVersion = "ReplayQUICVersion"
  187. ReplayObfuscatedQUIC = "ReplayObfuscatedQUIC"
  188. ReplayLivenessTest = "ReplayLivenessTest"
  189. ReplayUserAgent = "ReplayUserAgent"
  190. ReplayAPIRequestPadding = "ReplayAPIRequestPadding"
  191. APIRequestUpstreamPaddingMinBytes = "APIRequestUpstreamPaddingMinBytes"
  192. APIRequestUpstreamPaddingMaxBytes = "APIRequestUpstreamPaddingMaxBytes"
  193. APIRequestDownstreamPaddingMinBytes = "APIRequestDownstreamPaddingMinBytes"
  194. APIRequestDownstreamPaddingMaxBytes = "APIRequestDownstreamPaddingMaxBytes"
  195. PersistentStatsMaxStoreRecords = "PersistentStatsMaxStoreRecords"
  196. PersistentStatsMaxSendBytes = "PersistentStatsMaxSendBytes"
  197. RecordRemoteServerListPersistentStatsProbability = "RecordRemoteServerListPersistentStatsProbability"
  198. RecordFailedTunnelPersistentStatsProbability = "RecordFailedTunnelPersistentStatsProbability"
  199. )
  200. const (
  201. useNetworkLatencyMultiplier = 1
  202. serverSideOnly = 2
  203. )
  204. // defaultClientParameters specifies the type, default value, and minimum
  205. // value for all dynamically configurable client parameters.
  206. //
  207. // Do not change the names or types of existing values, as that can break
  208. // client logic or cause parameters to not be applied.
  209. //
  210. // Minimum values are a fail-safe for cases where lower values would break the
  211. // client logic. For example, setting a ConnectionWorkerPoolSize of 0 would
  212. // make the client never connect.
  213. var defaultClientParameters = map[string]struct {
  214. value interface{}
  215. minimum interface{}
  216. flags int32
  217. }{
  218. // NetworkLatencyMultiplier defaults to 0, meaning off. But when set, it
  219. // must be a multiplier >= 1.
  220. NetworkLatencyMultiplier: {value: 0.0, minimum: 1.0},
  221. TacticsWaitPeriod: {value: 10 * time.Second, minimum: 0 * time.Second, flags: useNetworkLatencyMultiplier},
  222. TacticsRetryPeriod: {value: 5 * time.Second, minimum: 1 * time.Millisecond},
  223. TacticsRetryPeriodJitter: {value: 0.3, minimum: 0.0},
  224. TacticsTimeout: {value: 2 * time.Minute, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  225. ConnectionWorkerPoolSize: {value: 10, minimum: 1},
  226. TunnelConnectTimeout: {value: 20 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  227. EstablishTunnelTimeout: {value: 300 * time.Second, minimum: time.Duration(0)},
  228. EstablishTunnelWorkTime: {value: 60 * time.Second, minimum: 1 * time.Second},
  229. EstablishTunnelPausePeriod: {value: 5 * time.Second, minimum: 1 * time.Millisecond},
  230. EstablishTunnelPausePeriodJitter: {value: 0.1, minimum: 0.0},
  231. EstablishTunnelServerAffinityGracePeriod: {value: 1 * time.Second, minimum: time.Duration(0), flags: useNetworkLatencyMultiplier},
  232. StaggerConnectionWorkersPeriod: {value: time.Duration(0), minimum: time.Duration(0)},
  233. StaggerConnectionWorkersJitter: {value: 0.1, minimum: 0.0},
  234. LimitIntensiveConnectionWorkers: {value: 0, minimum: 0},
  235. IgnoreHandshakeStatsRegexps: {value: false},
  236. TunnelOperateShutdownTimeout: {value: 1 * time.Second, minimum: 1 * time.Millisecond, flags: useNetworkLatencyMultiplier},
  237. TunnelPortForwardDialTimeout: {value: 10 * time.Second, minimum: 1 * time.Millisecond, flags: useNetworkLatencyMultiplier},
  238. TunnelRateLimits: {value: common.RateLimits{}},
  239. // PrioritizeTunnelProtocols parameters are obsoleted by InitialLimitTunnelProtocols.
  240. // TODO: remove once no longer required for older clients.
  241. PrioritizeTunnelProtocolsProbability: {value: 1.0, minimum: 0.0},
  242. PrioritizeTunnelProtocols: {value: protocol.TunnelProtocols{}},
  243. PrioritizeTunnelProtocolsCandidateCount: {value: 10, minimum: 0},
  244. InitialLimitTunnelProtocolsProbability: {value: 1.0, minimum: 0.0},
  245. InitialLimitTunnelProtocols: {value: protocol.TunnelProtocols{}},
  246. InitialLimitTunnelProtocolsCandidateCount: {value: 0, minimum: 0},
  247. LimitTunnelProtocolsProbability: {value: 1.0, minimum: 0.0},
  248. LimitTunnelProtocols: {value: protocol.TunnelProtocols{}},
  249. LimitTLSProfilesProbability: {value: 1.0, minimum: 0.0},
  250. LimitTLSProfiles: {value: protocol.TLSProfiles{}},
  251. SelectRandomizedTLSProfileProbability: {value: 0.25, minimum: 0.0},
  252. LimitQUICVersionsProbability: {value: 1.0, minimum: 0.0},
  253. LimitQUICVersions: {value: protocol.QUICVersions{}},
  254. FragmentorProbability: {value: 0.5, minimum: 0.0},
  255. FragmentorLimitProtocols: {value: protocol.TunnelProtocols{}},
  256. FragmentorMinTotalBytes: {value: 0, minimum: 0},
  257. FragmentorMaxTotalBytes: {value: 0, minimum: 0},
  258. FragmentorMinWriteBytes: {value: 1, minimum: 1},
  259. FragmentorMaxWriteBytes: {value: 1500, minimum: 1},
  260. FragmentorMinDelay: {value: time.Duration(0), minimum: time.Duration(0)},
  261. FragmentorMaxDelay: {value: 10 * time.Millisecond, minimum: time.Duration(0)},
  262. FragmentorDownstreamProbability: {value: 0.5, minimum: 0.0, flags: serverSideOnly},
  263. FragmentorDownstreamLimitProtocols: {value: protocol.TunnelProtocols{}, flags: serverSideOnly},
  264. FragmentorDownstreamMinTotalBytes: {value: 0, minimum: 0, flags: serverSideOnly},
  265. FragmentorDownstreamMaxTotalBytes: {value: 0, minimum: 0, flags: serverSideOnly},
  266. FragmentorDownstreamMinWriteBytes: {value: 1, minimum: 1, flags: serverSideOnly},
  267. FragmentorDownstreamMaxWriteBytes: {value: 1500, minimum: 1, flags: serverSideOnly},
  268. FragmentorDownstreamMinDelay: {value: time.Duration(0), minimum: time.Duration(0), flags: serverSideOnly},
  269. FragmentorDownstreamMaxDelay: {value: 10 * time.Millisecond, minimum: time.Duration(0), flags: serverSideOnly},
  270. // The Psiphon server will reject obfuscated SSH seed messages with
  271. // padding greater than OBFUSCATE_MAX_PADDING.
  272. // obfuscator.NewClientObfuscator will ignore invalid min/max padding
  273. // configurations.
  274. ObfuscatedSSHMinPadding: {value: 0, minimum: 0},
  275. ObfuscatedSSHMaxPadding: {value: obfuscator.OBFUSCATE_MAX_PADDING, minimum: 0},
  276. AdditionalCustomHeaders: {value: make(http.Header)},
  277. // Speed test and SSH keep alive padding is intended to frustrate
  278. // fingerprinting and should not exceed ~1 IP packet size.
  279. //
  280. // Currently, each serialized speed test sample, populated with real
  281. // values, is approximately 100 bytes. All SpeedTestMaxSampleCount samples
  282. // are loaded into memory are sent as API inputs.
  283. SpeedTestPaddingMinBytes: {value: 0, minimum: 0},
  284. SpeedTestPaddingMaxBytes: {value: 256, minimum: 0},
  285. SpeedTestMaxSampleCount: {value: 25, minimum: 1},
  286. // The Psiphon server times out inactive tunnels after 5 minutes, so this
  287. // is a soft max for SSHKeepAlivePeriodMax.
  288. SSHKeepAliveSpeedTestSampleProbability: {value: 0.5, minimum: 0.0},
  289. SSHKeepAlivePaddingMinBytes: {value: 0, minimum: 0},
  290. SSHKeepAlivePaddingMaxBytes: {value: 256, minimum: 0},
  291. SSHKeepAlivePeriodMin: {value: 1 * time.Minute, minimum: 1 * time.Second},
  292. SSHKeepAlivePeriodMax: {value: 2 * time.Minute, minimum: 1 * time.Second},
  293. SSHKeepAlivePeriodicTimeout: {value: 30 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  294. SSHKeepAlivePeriodicInactivePeriod: {value: 10 * time.Second, minimum: 1 * time.Second},
  295. SSHKeepAliveProbeTimeout: {value: 5 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  296. SSHKeepAliveProbeInactivePeriod: {value: 10 * time.Second, minimum: 1 * time.Second},
  297. HTTPProxyOriginServerTimeout: {value: 15 * time.Second, minimum: time.Duration(0), flags: useNetworkLatencyMultiplier},
  298. HTTPProxyMaxIdleConnectionsPerHost: {value: 50, minimum: 0},
  299. FetchRemoteServerListTimeout: {value: 30 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  300. FetchRemoteServerListRetryPeriod: {value: 30 * time.Second, minimum: 1 * time.Millisecond},
  301. FetchRemoteServerListStalePeriod: {value: 6 * time.Hour, minimum: 1 * time.Hour},
  302. RemoteServerListSignaturePublicKey: {value: ""},
  303. RemoteServerListURLs: {value: DownloadURLs{}},
  304. ObfuscatedServerListRootURLs: {value: DownloadURLs{}},
  305. PsiphonAPIRequestTimeout: {value: 20 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  306. PsiphonAPIStatusRequestPeriodMin: {value: 5 * time.Minute, minimum: 1 * time.Second},
  307. PsiphonAPIStatusRequestPeriodMax: {value: 10 * time.Minute, minimum: 1 * time.Second},
  308. PsiphonAPIStatusRequestShortPeriodMin: {value: 5 * time.Second, minimum: 1 * time.Second},
  309. PsiphonAPIStatusRequestShortPeriodMax: {value: 10 * time.Second, minimum: 1 * time.Second},
  310. // PsiphonAPIPersistentStatsMaxCount parameter is obsoleted by PersistentStatsMaxSendBytes.
  311. // TODO: remove once no longer required for older clients.
  312. PsiphonAPIPersistentStatsMaxCount: {value: 100, minimum: 1},
  313. // PsiphonAPIStatusRequestPadding parameters are obsoleted by APIRequestUp/DownstreamPadding.
  314. // TODO: remove once no longer required for older clients.
  315. PsiphonAPIStatusRequestPaddingMinBytes: {value: 0, minimum: 0},
  316. PsiphonAPIStatusRequestPaddingMaxBytes: {value: 256, minimum: 0},
  317. PsiphonAPIConnectedRequestRetryPeriod: {value: 5 * time.Second, minimum: 1 * time.Millisecond},
  318. FetchSplitTunnelRoutesTimeout: {value: 60 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  319. SplitTunnelRoutesURLFormat: {value: ""},
  320. SplitTunnelRoutesSignaturePublicKey: {value: ""},
  321. SplitTunnelDNSServer: {value: ""},
  322. FetchUpgradeTimeout: {value: 60 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  323. FetchUpgradeRetryPeriod: {value: 30 * time.Second, minimum: 1 * time.Millisecond},
  324. FetchUpgradeStalePeriod: {value: 6 * time.Hour, minimum: 1 * time.Hour},
  325. UpgradeDownloadURLs: {value: DownloadURLs{}},
  326. UpgradeDownloadClientVersionHeader: {value: ""},
  327. TotalBytesTransferredNoticePeriod: {value: 5 * time.Minute, minimum: 1 * time.Second},
  328. // The meek server times out inactive sessions after 45 seconds, so this
  329. // is a soft max for MeekMaxPollInterval, MeekRoundTripTimeout, and
  330. // MeekRoundTripRetryDeadline. MeekCookieMaxPadding cannot exceed
  331. // common.OBFUSCATE_SEED_LENGTH.
  332. MeekDialDomainsOnly: {value: false},
  333. MeekLimitBufferSizes: {value: false},
  334. MeekCookieMaxPadding: {value: 256, minimum: 0},
  335. MeekFullReceiveBufferLength: {value: 4194304, minimum: 1024},
  336. MeekReadPayloadChunkLength: {value: 65536, minimum: 1024},
  337. MeekLimitedFullReceiveBufferLength: {value: 131072, minimum: 1024},
  338. MeekLimitedReadPayloadChunkLength: {value: 4096, minimum: 1024},
  339. MeekMinPollInterval: {value: 100 * time.Millisecond, minimum: 1 * time.Millisecond},
  340. MeekMinPollIntervalJitter: {value: 0.3, minimum: 0.0},
  341. MeekMaxPollInterval: {value: 5 * time.Second, minimum: 1 * time.Millisecond},
  342. MeekMaxPollIntervalJitter: {value: 0.1, minimum: 0.0},
  343. MeekPollIntervalMultiplier: {value: 1.5, minimum: 0.0},
  344. MeekPollIntervalJitter: {value: 0.1, minimum: 0.0},
  345. MeekApplyPollIntervalMultiplierProbability: {value: 0.5},
  346. MeekRoundTripRetryDeadline: {value: 5 * time.Second, minimum: 1 * time.Millisecond, flags: useNetworkLatencyMultiplier},
  347. MeekRoundTripRetryMinDelay: {value: 50 * time.Millisecond, minimum: time.Duration(0)},
  348. MeekRoundTripRetryMaxDelay: {value: 1 * time.Second, minimum: time.Duration(0)},
  349. MeekRoundTripRetryMultiplier: {value: 2.0, minimum: 0.0},
  350. MeekRoundTripTimeout: {value: 20 * time.Second, minimum: 1 * time.Second, flags: useNetworkLatencyMultiplier},
  351. TransformHostNameProbability: {value: 0.5, minimum: 0.0},
  352. PickUserAgentProbability: {value: 0.5, minimum: 0.0},
  353. LivenessTestMinUpstreamBytes: {value: 0, minimum: 0},
  354. LivenessTestMaxUpstreamBytes: {value: 0, minimum: 0},
  355. LivenessTestMinDownstreamBytes: {value: 0, minimum: 0},
  356. LivenessTestMaxDownstreamBytes: {value: 0, minimum: 0},
  357. ReplayCandidateCount: {value: 10, minimum: 0},
  358. ReplayDialParametersTTL: {value: 24 * time.Hour, minimum: time.Duration(0)},
  359. ReplayTargetUpstreamBytes: {value: 0, minimum: 0},
  360. ReplayTargetDownstreamBytes: {value: 0, minimum: 0},
  361. ReplaySSH: {value: true},
  362. ReplayObfuscatorPadding: {value: true},
  363. ReplayFragmentor: {value: true},
  364. ReplayTLSProfile: {value: true},
  365. ReplayRandomizedTLSProfile: {value: true},
  366. ReplayFronting: {value: true},
  367. ReplayHostname: {value: true},
  368. ReplayQUICVersion: {value: true},
  369. ReplayObfuscatedQUIC: {value: true},
  370. ReplayLivenessTest: {value: true},
  371. ReplayUserAgent: {value: true},
  372. ReplayAPIRequestPadding: {value: true},
  373. APIRequestUpstreamPaddingMinBytes: {value: 0, minimum: 0},
  374. APIRequestUpstreamPaddingMaxBytes: {value: 1024, minimum: 0},
  375. APIRequestDownstreamPaddingMinBytes: {value: 0, minimum: 0},
  376. APIRequestDownstreamPaddingMaxBytes: {value: 1024, minimum: 0},
  377. PersistentStatsMaxStoreRecords: {value: 200, minimum: 1},
  378. PersistentStatsMaxSendBytes: {value: 65536, minimum: 1},
  379. RecordRemoteServerListPersistentStatsProbability: {value: 1.0, minimum: 0.0},
  380. RecordFailedTunnelPersistentStatsProbability: {value: 0.0, minimum: 0.0},
  381. }
  382. // IsServerSideOnly indicates if the parameter specified by name is used
  383. // server-side only.
  384. func IsServerSideOnly(name string) bool {
  385. defaultParameter, ok := defaultClientParameters[name]
  386. return ok && (defaultParameter.flags&serverSideOnly) != 0
  387. }
  388. // ClientParameters is a set of client parameters. To use the parameters, call
  389. // Get. To apply new values to the parameters, call Set.
  390. type ClientParameters struct {
  391. getValueLogger func(error)
  392. snapshot atomic.Value
  393. }
  394. // ClientParametersSnapshot is an atomic snapshot of the client parameter
  395. // values. ClientParameters.Get will return a snapshot which may be used to
  396. // read multiple related values atomically and consistently while the current
  397. // snapshot in ClientParameters may change concurrently.
  398. type ClientParametersSnapshot struct {
  399. getValueLogger func(error)
  400. tag string
  401. parameters map[string]interface{}
  402. }
  403. // NewClientParameters initializes a new ClientParameters with the default
  404. // parameter values.
  405. //
  406. // getValueLogger is optional, and is used to report runtime errors with
  407. // getValue; see comment in getValue.
  408. func NewClientParameters(
  409. getValueLogger func(error)) (*ClientParameters, error) {
  410. clientParameters := &ClientParameters{
  411. getValueLogger: getValueLogger,
  412. }
  413. _, err := clientParameters.Set("", false)
  414. if err != nil {
  415. return nil, common.ContextError(err)
  416. }
  417. return clientParameters, nil
  418. }
  419. func makeDefaultParameters() (map[string]interface{}, error) {
  420. parameters := make(map[string]interface{})
  421. for name, defaults := range defaultClientParameters {
  422. if defaults.value == nil {
  423. return nil, common.ContextError(fmt.Errorf("default parameter missing value: %s", name))
  424. }
  425. if defaults.minimum != nil &&
  426. reflect.TypeOf(defaults.value) != reflect.TypeOf(defaults.minimum) {
  427. return nil, common.ContextError(fmt.Errorf("default parameter value and minimum type mismatch: %s", name))
  428. }
  429. _, isDuration := defaults.value.(time.Duration)
  430. if defaults.flags&useNetworkLatencyMultiplier != 0 && !isDuration {
  431. return nil, common.ContextError(fmt.Errorf("default non-duration parameter uses multipler: %s", name))
  432. }
  433. parameters[name] = defaults.value
  434. }
  435. return parameters, nil
  436. }
  437. // Set replaces the current parameters. First, a set of parameters are
  438. // initialized using the default values. Then, each applyParameters is applied
  439. // in turn, with the later instances having precedence.
  440. //
  441. // When skipOnError is true, unknown or invalid parameters in any
  442. // applyParameters are skipped instead of aborting with an error.
  443. //
  444. // For protocol.TunnelProtocols and protocol.TLSProfiles type values, when
  445. // skipOnError is true the values are filtered instead of validated, so
  446. // only known tunnel protocols and TLS profiles are retained.
  447. //
  448. // When an error is returned, the previous parameters remain completely
  449. // unmodified.
  450. //
  451. // For use in logging, Set returns a count of the number of parameters applied
  452. // from each applyParameters.
  453. func (p *ClientParameters) Set(
  454. tag string, skipOnError bool, applyParameters ...map[string]interface{}) ([]int, error) {
  455. var counts []int
  456. parameters, err := makeDefaultParameters()
  457. if err != nil {
  458. return nil, common.ContextError(err)
  459. }
  460. for i := 0; i < len(applyParameters); i++ {
  461. count := 0
  462. for name, value := range applyParameters[i] {
  463. existingValue, ok := parameters[name]
  464. if !ok {
  465. if skipOnError {
  466. continue
  467. }
  468. return nil, common.ContextError(fmt.Errorf("unknown parameter: %s", name))
  469. }
  470. // Accept strings such as "1h" for duration parameters.
  471. switch existingValue.(type) {
  472. case time.Duration:
  473. if s, ok := value.(string); ok {
  474. if d, err := time.ParseDuration(s); err == nil {
  475. value = d
  476. }
  477. }
  478. }
  479. // A JSON remarshal resolves cases where applyParameters is a
  480. // result of unmarshal-into-interface, in which case non-scalar
  481. // values will not have the expected types; see:
  482. // https://golang.org/pkg/encoding/json/#Unmarshal. This remarshal
  483. // also results in a deep copy.
  484. marshaledValue, err := json.Marshal(value)
  485. if err != nil {
  486. continue
  487. }
  488. newValuePtr := reflect.New(reflect.TypeOf(existingValue))
  489. err = json.Unmarshal(marshaledValue, newValuePtr.Interface())
  490. if err != nil {
  491. if skipOnError {
  492. continue
  493. }
  494. return nil, common.ContextError(fmt.Errorf("unmarshal parameter %s failed: %s", name, err))
  495. }
  496. newValue := newValuePtr.Elem().Interface()
  497. // Perform type-specific validation for some cases.
  498. // TODO: require RemoteServerListSignaturePublicKey when
  499. // RemoteServerListURLs is set?
  500. switch v := newValue.(type) {
  501. case DownloadURLs:
  502. err := v.DecodeAndValidate()
  503. if err != nil {
  504. if skipOnError {
  505. continue
  506. }
  507. return nil, common.ContextError(err)
  508. }
  509. case protocol.TunnelProtocols:
  510. if skipOnError {
  511. newValue = v.PruneInvalid()
  512. } else {
  513. err := v.Validate()
  514. if err != nil {
  515. return nil, common.ContextError(err)
  516. }
  517. }
  518. case protocol.TLSProfiles:
  519. if skipOnError {
  520. newValue = v.PruneInvalid()
  521. } else {
  522. err := v.Validate()
  523. if err != nil {
  524. return nil, common.ContextError(err)
  525. }
  526. }
  527. case protocol.QUICVersions:
  528. if skipOnError {
  529. newValue = v.PruneInvalid()
  530. } else {
  531. err := v.Validate()
  532. if err != nil {
  533. return nil, common.ContextError(err)
  534. }
  535. }
  536. }
  537. // Enforce any minimums. Assumes defaultClientParameters[name]
  538. // exists.
  539. if defaultClientParameters[name].minimum != nil {
  540. valid := true
  541. switch v := newValue.(type) {
  542. case int:
  543. m, ok := defaultClientParameters[name].minimum.(int)
  544. if !ok || v < m {
  545. valid = false
  546. }
  547. case float64:
  548. m, ok := defaultClientParameters[name].minimum.(float64)
  549. if !ok || v < m {
  550. valid = false
  551. }
  552. case time.Duration:
  553. m, ok := defaultClientParameters[name].minimum.(time.Duration)
  554. if !ok || v < m {
  555. valid = false
  556. }
  557. default:
  558. if skipOnError {
  559. continue
  560. }
  561. return nil, common.ContextError(fmt.Errorf("unexpected parameter with minimum: %s", name))
  562. }
  563. if !valid {
  564. if skipOnError {
  565. continue
  566. }
  567. return nil, common.ContextError(fmt.Errorf("parameter below minimum: %s", name))
  568. }
  569. }
  570. parameters[name] = newValue
  571. count++
  572. }
  573. counts = append(counts, count)
  574. }
  575. snapshot := &ClientParametersSnapshot{
  576. getValueLogger: p.getValueLogger,
  577. tag: tag,
  578. parameters: parameters,
  579. }
  580. p.snapshot.Store(snapshot)
  581. return counts, nil
  582. }
  583. // Get returns the current parameters. Values read from the current parameters
  584. // are not deep copies and must be treated read-only.
  585. func (p *ClientParameters) Get() *ClientParametersSnapshot {
  586. return p.snapshot.Load().(*ClientParametersSnapshot)
  587. }
  588. // Tag returns the tag associated with these parameters.
  589. func (p *ClientParametersSnapshot) Tag() string {
  590. return p.tag
  591. }
  592. // getValue sets target to the value of the named parameter.
  593. //
  594. // It is an error if the name is not found, target is not a pointer, or the
  595. // type of target points to does not match the value.
  596. //
  597. // Any of these conditions would be a bug in the caller. getValue does not
  598. // panic in these cases as the client is deployed as a library in various apps
  599. // and the failure of Psiphon may not be a failure for the app process.
  600. //
  601. // Instead, errors are logged to the getValueLogger and getValue leaves the
  602. // target unset, which will result in the caller getting and using a zero
  603. // value of the requested type.
  604. func (p *ClientParametersSnapshot) getValue(name string, target interface{}) {
  605. value, ok := p.parameters[name]
  606. if !ok {
  607. if p.getValueLogger != nil {
  608. p.getValueLogger(common.ContextError(fmt.Errorf(
  609. "value %s not found", name)))
  610. }
  611. return
  612. }
  613. valueType := reflect.TypeOf(value)
  614. if reflect.PtrTo(valueType) != reflect.TypeOf(target) {
  615. if p.getValueLogger != nil {
  616. p.getValueLogger(common.ContextError(fmt.Errorf(
  617. "value %s has unexpected type %s", name, valueType.Name())))
  618. }
  619. return
  620. }
  621. // Note: there is no deep copy of parameter values; the returned value may
  622. // share memory with the original and should not be modified.
  623. targetValue := reflect.ValueOf(target)
  624. if targetValue.Kind() != reflect.Ptr {
  625. p.getValueLogger(common.ContextError(fmt.Errorf(
  626. "target for value %s is not pointer", name)))
  627. return
  628. }
  629. targetValue.Elem().Set(reflect.ValueOf(value))
  630. }
  631. // String returns a string parameter value.
  632. func (p *ClientParametersSnapshot) String(name string) string {
  633. value := ""
  634. p.getValue(name, &value)
  635. return value
  636. }
  637. // Strings returns a []string parameter value.
  638. func (p *ClientParametersSnapshot) Strings(name string) []string {
  639. value := []string{}
  640. p.getValue(name, &value)
  641. return value
  642. }
  643. // Int returns an int parameter value.
  644. func (p *ClientParametersSnapshot) Int(name string) int {
  645. value := int(0)
  646. p.getValue(name, &value)
  647. return value
  648. }
  649. // Bool returns a bool parameter value.
  650. func (p *ClientParametersSnapshot) Bool(name string) bool {
  651. value := false
  652. p.getValue(name, &value)
  653. return value
  654. }
  655. // Float returns a float64 parameter value.
  656. func (p *ClientParametersSnapshot) Float(name string) float64 {
  657. value := float64(0.0)
  658. p.getValue(name, &value)
  659. return value
  660. }
  661. // WeightedCoinFlip returns the result of prng.FlipWeightedCoin using the
  662. // specified float parameter as the probability input.
  663. func (p *ClientParametersSnapshot) WeightedCoinFlip(name string) bool {
  664. var value float64
  665. p.getValue(name, &value)
  666. return prng.FlipWeightedCoin(value)
  667. }
  668. // Duration returns a time.Duration parameter value. When the duration
  669. // parameter has the useNetworkLatencyMultiplier flag, the
  670. // NetworkLatencyMultiplier is applied to the returned value.
  671. func (p *ClientParametersSnapshot) Duration(name string) time.Duration {
  672. value := time.Duration(0)
  673. p.getValue(name, &value)
  674. defaultParameter, ok := defaultClientParameters[name]
  675. if value > 0 && ok && defaultParameter.flags&useNetworkLatencyMultiplier != 0 {
  676. multiplier := float64(0.0)
  677. p.getValue(NetworkLatencyMultiplier, &multiplier)
  678. if multiplier > 0.0 {
  679. value = time.Duration(float64(value) * multiplier)
  680. }
  681. }
  682. return value
  683. }
  684. // TunnelProtocols returns a protocol.TunnelProtocols parameter value.
  685. // If there is a corresponding Probability value, a weighted coin flip
  686. // will be performed and, depending on the result, the value or the
  687. // parameter default will be returned.
  688. func (p *ClientParametersSnapshot) TunnelProtocols(name string) protocol.TunnelProtocols {
  689. probabilityName := name + "Probability"
  690. _, ok := p.parameters[probabilityName]
  691. if ok {
  692. probabilityValue := float64(1.0)
  693. p.getValue(probabilityName, &probabilityValue)
  694. if !prng.FlipWeightedCoin(probabilityValue) {
  695. defaultParameter, ok := defaultClientParameters[name]
  696. if ok {
  697. defaultValue, ok := defaultParameter.value.(protocol.TunnelProtocols)
  698. if ok {
  699. value := make(protocol.TunnelProtocols, len(defaultValue))
  700. copy(value, defaultValue)
  701. return value
  702. }
  703. }
  704. }
  705. }
  706. value := protocol.TunnelProtocols{}
  707. p.getValue(name, &value)
  708. return value
  709. }
  710. // TLSProfiles returns a protocol.TLSProfiles parameter value.
  711. // If there is a corresponding Probability value, a weighted coin flip
  712. // will be performed and, depending on the result, the value or the
  713. // parameter default will be returned.
  714. func (p *ClientParametersSnapshot) TLSProfiles(name string) protocol.TLSProfiles {
  715. probabilityName := name + "Probability"
  716. _, ok := p.parameters[probabilityName]
  717. if ok {
  718. probabilityValue := float64(1.0)
  719. p.getValue(probabilityName, &probabilityValue)
  720. if !prng.FlipWeightedCoin(probabilityValue) {
  721. defaultParameter, ok := defaultClientParameters[name]
  722. if ok {
  723. defaultValue, ok := defaultParameter.value.(protocol.TLSProfiles)
  724. if ok {
  725. value := make(protocol.TLSProfiles, len(defaultValue))
  726. copy(value, defaultValue)
  727. return value
  728. }
  729. }
  730. }
  731. }
  732. value := protocol.TLSProfiles{}
  733. p.getValue(name, &value)
  734. return value
  735. }
  736. // QUICVersions returns a protocol.QUICVersions parameter value.
  737. // If there is a corresponding Probability value, a weighted coin flip
  738. // will be performed and, depending on the result, the value or the
  739. // parameter default will be returned.
  740. func (p *ClientParametersSnapshot) QUICVersions(name string) protocol.QUICVersions {
  741. probabilityName := name + "Probability"
  742. _, ok := p.parameters[probabilityName]
  743. if ok {
  744. probabilityValue := float64(1.0)
  745. p.getValue(probabilityName, &probabilityValue)
  746. if !prng.FlipWeightedCoin(probabilityValue) {
  747. defaultParameter, ok := defaultClientParameters[name]
  748. if ok {
  749. defaultValue, ok := defaultParameter.value.(protocol.QUICVersions)
  750. if ok {
  751. value := make(protocol.QUICVersions, len(defaultValue))
  752. copy(value, defaultValue)
  753. return value
  754. }
  755. }
  756. }
  757. }
  758. value := protocol.QUICVersions{}
  759. p.getValue(name, &value)
  760. return value
  761. }
  762. // DownloadURLs returns a DownloadURLs parameter value.
  763. func (p *ClientParametersSnapshot) DownloadURLs(name string) DownloadURLs {
  764. value := DownloadURLs{}
  765. p.getValue(name, &value)
  766. return value
  767. }
  768. // RateLimits returns a common.RateLimits parameter value.
  769. func (p *ClientParametersSnapshot) RateLimits(name string) common.RateLimits {
  770. value := common.RateLimits{}
  771. p.getValue(name, &value)
  772. return value
  773. }
  774. // HTTPHeaders returns an http.Header parameter value.
  775. func (p *ClientParametersSnapshot) HTTPHeaders(name string) http.Header {
  776. value := make(http.Header)
  777. p.getValue(name, &value)
  778. return value
  779. }