clientParameters.go 46 KB

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