clientParameters.go 39 KB

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