clientParameters.go 45 KB

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