config.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*
  2. * Copyright (c) 2015, 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. package psiphon
  20. import (
  21. "crypto/md5"
  22. "encoding/base64"
  23. "encoding/binary"
  24. "encoding/json"
  25. "fmt"
  26. "net/http"
  27. "os"
  28. "strconv"
  29. "strings"
  30. "sync"
  31. "unicode"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  36. )
  37. const (
  38. TUNNEL_POOL_SIZE = 1
  39. )
  40. // Config is the Psiphon configuration specified by the application. This
  41. // configuration controls the behavior of the core tunnel functionality.
  42. //
  43. // To distinguish omitted timeout params from explicit 0 value timeout params,
  44. // corresponding fieldss are int pointers. nil means no value was supplied and
  45. // to use the default; a non-nil pointer to 0 means no timeout.
  46. type Config struct {
  47. // DataStoreDirectory is the directory in which to store the persistent
  48. // database, which contains information such as server entries. By
  49. // default, current working directory.
  50. //
  51. // Warning: If the datastore file, DataStoreDirectory/DATA_STORE_FILENAME,
  52. // exists but fails to open for any reason (checksum error, unexpected
  53. // file format, etc.) it will be deleted in order to pave a new datastore
  54. // and continue running.
  55. DataStoreDirectory string
  56. // PropagationChannelId is a string identifier which indicates how the
  57. // Psiphon client was distributed. This parameter is required. This value
  58. // is supplied by and depends on the Psiphon Network, and is typically
  59. // embedded in the client binary.
  60. PropagationChannelId string
  61. // SponsorId is a string identifier which indicates who is sponsoring this
  62. // Psiphon client. One purpose of this value is to determine the home
  63. // pages for display. This parameter is required. This value is supplied
  64. // by and depends on the Psiphon Network, and is typically embedded in the
  65. // client binary.
  66. SponsorId string
  67. // ClientVersion is the client version number that the client reports to
  68. // the server. The version number refers to the host client application,
  69. // not the core tunnel library. One purpose of this value is to enable
  70. // automatic updates. This value is supplied by and depends on the Psiphon
  71. // Network, and is typically embedded in the client binary.
  72. //
  73. // Note that sending a ClientPlatform string which includes "windows"
  74. // (case insensitive) and a ClientVersion of <= 44 will cause an error in
  75. // processing the response to DoConnectedRequest calls.
  76. ClientVersion string
  77. // ClientPlatform is the client platform ("Windows", "Android", etc.) that
  78. // the client reports to the server.
  79. ClientPlatform string
  80. // TunnelWholeDevice is a flag that is passed through to the handshake
  81. // request for stats purposes. Set to 1 when the host application is
  82. // tunneling the whole device, 0 otherwise.
  83. TunnelWholeDevice int
  84. // EgressRegion is a ISO 3166-1 alpha-2 country code which indicates which
  85. // country to egress from. For the default, "", the best performing server
  86. // in any country is selected.
  87. EgressRegion string
  88. // ListenInterface specifies which interface to listen on. If no
  89. // interface is provided then listen on 127.0.0.1. If 'any' is provided
  90. // then use 0.0.0.0. If there are multiple IP addresses on an interface
  91. // use the first IPv4 address.
  92. ListenInterface string
  93. // DisableLocalSocksProxy disables running the local SOCKS proxy.
  94. DisableLocalSocksProxy bool
  95. // LocalSocksProxyPort specifies a port number for the local SOCKS proxy
  96. // running at 127.0.0.1. For the default value, 0, the system selects a
  97. // free port (a notice reporting the selected port is emitted).
  98. LocalSocksProxyPort int
  99. // LocalHttpProxyPort specifies a port number for the local HTTP proxy
  100. // running at 127.0.0.1. For the default value, 0, the system selects a
  101. // free port (a notice reporting the selected port is emitted).
  102. LocalHttpProxyPort int
  103. // DisableLocalHTTPProxy disables running the local HTTP proxy.
  104. DisableLocalHTTPProxy bool
  105. // NetworkLatencyMultiplier is a multiplier that is to be applied to
  106. // default network event timeouts. Set this to tune performance for
  107. // slow networks.
  108. // When set, must be >= 1.0.
  109. NetworkLatencyMultiplier float64
  110. // TunnelProtocol indicates which protocol to use. For the default, "",
  111. // all protocols are used.
  112. //
  113. // Deprecated: Use LimitTunnelProtocols. When LimitTunnelProtocols is not
  114. // nil, this parameter is ignored.
  115. TunnelProtocol string
  116. // LimitTunnelProtocols indicates which protocols to use. Valid values
  117. // include:
  118. // "SSH", "OSSH", "UNFRONTED-MEEK-OSSH", "UNFRONTED-MEEK-HTTPS-OSSH",
  119. // "UNFRONTED-MEEK-SESSION-TICKET-OSSH", "FRONTED-MEEK-OSSH",
  120. // "FRONTED-MEEK-HTTP-OSSH", "QUIC-OSSH", "MARIONETTE-OSSH", and
  121. // "TAPDANCE-OSSH".
  122. // For the default, an empty list, all protocols are used.
  123. LimitTunnelProtocols []string
  124. // InitialLimitTunnelProtocols is an optional initial phase of limited
  125. // protocols for the first InitialLimitTunnelProtocolsCandidateCount
  126. // candidates; after these candidates, LimitTunnelProtocols applies.
  127. //
  128. // For the default, an empty list, InitialLimitTunnelProtocols is off.
  129. InitialLimitTunnelProtocols []string
  130. // InitialLimitTunnelProtocolsCandidateCount is the number of candidates
  131. // to which InitialLimitTunnelProtocols is applied instead of
  132. // LimitTunnelProtocols.
  133. //
  134. // For the default, 0, InitialLimitTunnelProtocols is off.
  135. InitialLimitTunnelProtocolsCandidateCount int
  136. // LimitTLSProfiles indicates which TLS profiles to select from. Valid
  137. // values are listed in protocols.SupportedTLSProfiles.
  138. // For the default, an empty list, all profiles are candidates for
  139. // selection.
  140. LimitTLSProfiles []string
  141. // LimitQUICVersions indicates which QUIC versions to select from. Valid
  142. // values are listed in protocols.SupportedQUICVersions.
  143. // For the default, an empty list, all versions are candidates for
  144. // selection.
  145. LimitQUICVersions []string
  146. // EstablishTunnelTimeoutSeconds specifies a time limit after which to
  147. // halt the core tunnel controller if no tunnel has been established. The
  148. // default is parameters.EstablishTunnelTimeout.
  149. EstablishTunnelTimeoutSeconds *int
  150. // EstablishTunnelPausePeriodSeconds specifies the delay between attempts
  151. // to establish tunnels. Briefly pausing allows for network conditions to
  152. // improve and for asynchronous operations such as fetch remote server
  153. // list to complete. If omitted, a default value is used. This value is
  154. // typical overridden for testing.
  155. EstablishTunnelPausePeriodSeconds *int
  156. // EstablishTunnelPausePeriodSeconds specifies the grace period, or head
  157. // start, provided to the affinity server candidate when establishing. The
  158. // affinity server is the server used for the last established tunnel.
  159. EstablishTunnelServerAffinityGracePeriodMilliseconds *int
  160. // ConnectionWorkerPoolSize specifies how many connection attempts to
  161. // attempt in parallel. If omitted of when 0, a default is used; this is
  162. // recommended.
  163. ConnectionWorkerPoolSize int
  164. // TunnelPoolSize specifies how many tunnels to run in parallel. Port
  165. // forwards are multiplexed over multiple tunnels. If omitted or when 0,
  166. // the default is TUNNEL_POOL_SIZE, which is recommended.
  167. TunnelPoolSize int
  168. // StaggerConnectionWorkersMilliseconds adds a specified delay before
  169. // making each server candidate available to connection workers. This
  170. // option is enabled when StaggerConnectionWorkersMilliseconds > 0.
  171. StaggerConnectionWorkersMilliseconds int
  172. // LimitIntensiveConnectionWorkers limits the number of concurrent
  173. // connection workers attempting connections with resource intensive
  174. // protocols. This option is enabled when LimitIntensiveConnectionWorkers
  175. // > 0.
  176. LimitIntensiveConnectionWorkers int
  177. // LimitMeekBufferSizes selects smaller buffers for meek protocols.
  178. LimitMeekBufferSizes bool
  179. // IgnoreHandshakeStatsRegexps skips compiling and using stats regexes.
  180. IgnoreHandshakeStatsRegexps bool
  181. // UpstreamProxyURL is a URL specifying an upstream proxy to use for all
  182. // outbound connections. The URL should include proxy type and
  183. // authentication information, as required. See example URLs here:
  184. // https://github.com/Psiphon-Labs/psiphon-tunnel-core/tree/master/psiphon/upstreamproxy
  185. UpstreamProxyURL string
  186. // CustomHeaders is a set of additional arbitrary HTTP headers that are
  187. // added to all plaintext HTTP requests and requests made through an HTTP
  188. // upstream proxy when specified by UpstreamProxyURL.
  189. CustomHeaders http.Header
  190. // Deprecated: Use CustomHeaders. When CustomHeaders is not nil, this
  191. // parameter is ignored.
  192. UpstreamProxyCustomHeaders http.Header
  193. // NetworkConnectivityChecker is an interface that enables tunnel-core to
  194. // call into the host application to check for network connectivity. See:
  195. // NetworkConnectivityChecker doc.
  196. //
  197. // This parameter is only applicable to library deployments.
  198. NetworkConnectivityChecker NetworkConnectivityChecker
  199. // DeviceBinder is an interface that enables tunnel-core to call into the
  200. // host application to bind sockets to specific devices. See: DeviceBinder
  201. // doc.
  202. //
  203. // This parameter is only applicable to library deployments.
  204. DeviceBinder DeviceBinder
  205. // IPv6Synthesizer is an interface that allows tunnel-core to call into
  206. // the host application to synthesize IPv6 addresses. See: IPv6Synthesizer
  207. // doc.
  208. //
  209. // This parameter is only applicable to library deployments.
  210. IPv6Synthesizer IPv6Synthesizer
  211. // DnsServerGetter is an interface that enables tunnel-core to call into
  212. // the host application to discover the native network DNS server
  213. // settings. See: DnsServerGetter doc.
  214. //
  215. // This parameter is only applicable to library deployments.
  216. DnsServerGetter DnsServerGetter
  217. // NetworkIDGetter in an interface that enables tunnel-core to call into
  218. // the host application to get an identifier for the host's current active
  219. // network. See: NetworkIDGetter doc.
  220. //
  221. // This parameter is only applicable to library deployments.
  222. NetworkIDGetter NetworkIDGetter
  223. // NetworkID, when not blank, is used as the identifier for the host's
  224. // current active network.
  225. // NetworkID is ignored when NetworkIDGetter is set.
  226. NetworkID string
  227. // DisableTactics disables tactics operations including requests, payload
  228. // handling, and application of parameters.
  229. DisableTactics bool
  230. // DisableReplay causes any persisted dial parameters to be ignored when
  231. // they would otherwise be used for replay.
  232. DisableReplay bool
  233. // TargetServerEntry is an encoded server entry. When specified, this
  234. // server entry is used exclusively and all other known servers are
  235. // ignored; also, when set, ConnectionWorkerPoolSize is ignored and
  236. // the pool size is 1.
  237. TargetServerEntry string
  238. // DisableApi disables Psiphon server API calls including handshake,
  239. // connected, status, etc. This is used for special case temporary tunnels
  240. // (Windows VPN mode).
  241. DisableApi bool
  242. // TargetApiProtocol specifies whether to force use of "ssh" or "web" API
  243. // protocol. When blank, the default, the optimal API protocol is used.
  244. // Note that this capability check is not applied before the
  245. // "CandidateServers" count is emitted.
  246. //
  247. // This parameter is intended for testing and debugging only. Not all
  248. // parameters are supported in the legacy "web" API protocol, including
  249. // speed test samples.
  250. TargetApiProtocol string
  251. // RemoteServerListUrl is a URL which specifies a location to fetch out-
  252. // of-band server entries. This facility is used when a tunnel cannot be
  253. // established to known servers. This value is supplied by and depends on
  254. // the Psiphon Network, and is typically embedded in the client binary.
  255. //
  256. // Deprecated: Use RemoteServerListURLs. When RemoteServerListURLs is not
  257. // nil, this parameter is ignored.
  258. RemoteServerListUrl string
  259. // RemoteServerListURLs is list of URLs which specify locations to fetch
  260. // out-of-band server entries. This facility is used when a tunnel cannot
  261. // be established to known servers. This value is supplied by and depends
  262. // on the Psiphon Network, and is typically embedded in the client binary.
  263. // All URLs must point to the same entity with the same ETag. At least one
  264. // DownloadURL must have OnlyAfterAttempts = 0.
  265. RemoteServerListURLs parameters.DownloadURLs
  266. // RemoteServerListDownloadFilename specifies a target filename for
  267. // storing the remote server list download. Data is stored in co-located
  268. // files (RemoteServerListDownloadFilename.part*) to allow for resumable
  269. // downloading.
  270. RemoteServerListDownloadFilename string
  271. // RemoteServerListSignaturePublicKey specifies a public key that's used
  272. // to authenticate the remote server list payload. This value is supplied
  273. // by and depends on the Psiphon Network, and is typically embedded in the
  274. // client binary.
  275. RemoteServerListSignaturePublicKey string
  276. // DisableRemoteServerListFetcher disables fetching remote server lists.
  277. // This is used for special case temporary tunnels.
  278. DisableRemoteServerListFetcher bool
  279. // FetchRemoteServerListRetryPeriodMilliseconds specifies the delay before
  280. // resuming a remote server list download after a failure. If omitted, a
  281. // default value is used. This value is typical overridden for testing.
  282. FetchRemoteServerListRetryPeriodMilliseconds *int
  283. // ObfuscatedServerListRootURL is a URL which specifies the root location
  284. // from which to fetch obfuscated server list files. This value is
  285. // supplied by and depends on the Psiphon Network, and is typically
  286. // embedded in the client binary.
  287. //
  288. // Deprecated: Use ObfuscatedServerListRootURLs. When
  289. // ObfuscatedServerListRootURLs is not nil, this parameter is ignored.
  290. ObfuscatedServerListRootURL string
  291. // ObfuscatedServerListRootURLs is a list of URLs which specify root
  292. // locations from which to fetch obfuscated server list files. This value
  293. // is supplied by and depends on the Psiphon Network, and is typically
  294. // embedded in the client binary. All URLs must point to the same entity
  295. // with the same ETag. At least one DownloadURL must have
  296. // OnlyAfterAttempts = 0.
  297. ObfuscatedServerListRootURLs parameters.DownloadURLs
  298. // ObfuscatedServerListDownloadDirectory specifies a target directory for
  299. // storing the obfuscated remote server list downloads. Data is stored in
  300. // co-located files (<OSL filename>.part*) to allow for resumable
  301. // downloading.
  302. ObfuscatedServerListDownloadDirectory string
  303. // SplitTunnelRoutesURLFormat is a URL which specifies the location of a
  304. // routes file to use for split tunnel mode. The URL must include a
  305. // placeholder for the client region to be supplied. Split tunnel mode
  306. // uses the routes file to classify port forward destinations as foreign
  307. // or domestic and does not tunnel domestic destinations. Split tunnel
  308. // mode is on when all the SplitTunnel parameters are supplied. This value
  309. // is supplied by and depends on the Psiphon Network, and is typically
  310. // embedded in the client binary.
  311. SplitTunnelRoutesURLFormat string
  312. // SplitTunnelRoutesSignaturePublicKey specifies a public key that's used
  313. // to authenticate the split tunnel routes payload. This value is supplied
  314. // by and depends on the Psiphon Network, and is typically embedded in the
  315. // client binary.
  316. SplitTunnelRoutesSignaturePublicKey string
  317. // SplitTunnelDNSServer specifies a DNS server to use when resolving port
  318. // forward target domain names to IP addresses for classification. The DNS
  319. // server must support TCP requests.
  320. SplitTunnelDNSServer string
  321. // UpgradeDownloadUrl specifies a URL from which to download a host client
  322. // upgrade file, when one is available. The core tunnel controller
  323. // provides a resumable download facility which downloads this resource
  324. // and emits a notice when complete. This value is supplied by and depends
  325. // on the Psiphon Network, and is typically embedded in the client binary.
  326. //
  327. // Deprecated: Use UpgradeDownloadURLs. When UpgradeDownloadURLs is not
  328. // nil, this parameter is ignored.
  329. UpgradeDownloadUrl string
  330. // UpgradeDownloadURLs is list of URLs which specify locations from which
  331. // to download a host client upgrade file, when one is available. The core
  332. // tunnel controller provides a resumable download facility which
  333. // downloads this resource and emits a notice when complete. This value is
  334. // supplied by and depends on the Psiphon Network, and is typically
  335. // embedded in the client binary. All URLs must point to the same entity
  336. // with the same ETag. At least one DownloadURL must have
  337. // OnlyAfterAttempts = 0.
  338. UpgradeDownloadURLs parameters.DownloadURLs
  339. // UpgradeDownloadClientVersionHeader specifies the HTTP header name for
  340. // the entity at UpgradeDownloadURLs which specifies the client version
  341. // (an integer value). A HEAD request may be made to check the version
  342. // number available at UpgradeDownloadURLs.
  343. // UpgradeDownloadClientVersionHeader is required when UpgradeDownloadURLs
  344. // is specified.
  345. UpgradeDownloadClientVersionHeader string
  346. // UpgradeDownloadFilename is the local target filename for an upgrade
  347. // download. This parameter is required when UpgradeDownloadURLs (or
  348. // UpgradeDownloadUrl) is specified. Data is stored in co-located files
  349. // (UpgradeDownloadFilename.part*) to allow for resumable downloading.
  350. UpgradeDownloadFilename string
  351. // FetchUpgradeRetryPeriodMilliseconds specifies the delay before resuming
  352. // a client upgrade download after a failure. If omitted, a default value
  353. // is used. This value is typical overridden for testing.
  354. FetchUpgradeRetryPeriodMilliseconds *int
  355. // EmitBytesTransferred indicates whether to emit periodic notices showing
  356. // bytes sent and received.
  357. EmitBytesTransferred bool
  358. // TrustedCACertificatesFilename specifies a file containing trusted CA
  359. // certs. When set, this toggles use of the trusted CA certs, specified in
  360. // TrustedCACertificatesFilename, for tunneled TLS connections that expect
  361. // server certificates signed with public certificate authorities
  362. // (currently, only upgrade downloads). This option is used with stock Go
  363. // TLS in cases where Go may fail to obtain a list of root CAs from the
  364. // operating system.
  365. TrustedCACertificatesFilename string
  366. // DisablePeriodicSshKeepAlive indicates whether to send an SSH keepalive
  367. // every 1-2 minutes, when the tunnel is idle. If the SSH keepalive times
  368. // out, the tunnel is considered to have failed.
  369. DisablePeriodicSshKeepAlive bool
  370. // DeviceRegion is the optional, reported region the host device is
  371. // running in. This input value should be a ISO 3166-1 alpha-2 country
  372. // code. The device region is reported to the server in the connected
  373. // request and recorded for Psiphon stats.
  374. //
  375. // When provided, this value may be used, pre-connection, to select
  376. // performance or circumvention optimization strategies for the given
  377. // region.
  378. DeviceRegion string
  379. // EmitDiagnosticNotices indicates whether to output notices containing
  380. // detailed information about the Psiphon session. As these notices may
  381. // contain sensitive information, they should not be insecurely distributed
  382. // or displayed to users. Default is off.
  383. EmitDiagnosticNotices bool
  384. // EmitDiagnosticNetworkParameters indicates whether to include network
  385. // parameters in diagnostic notices. As these parameters are sensitive
  386. // circumvention network information, they should not be insecurely
  387. // distributed or displayed to users. Default is off.
  388. EmitDiagnosticNetworkParameters bool
  389. // RateLimits specify throttling configuration for the tunnel.
  390. RateLimits common.RateLimits
  391. // EmitSLOKs indicates whether to emit notices for each seeded SLOK. As
  392. // this could reveal user browsing activity, it's intended for debugging
  393. // and testing only.
  394. EmitSLOKs bool
  395. // PacketTunnelTunDeviceFileDescriptor specifies a tun device file
  396. // descriptor to use for running a packet tunnel. When this value is > 0,
  397. // a packet tunnel is established through the server and packets are
  398. // relayed via the tun device file descriptor. The file descriptor is
  399. // duped in NewController. When PacketTunnelTunDeviceFileDescriptor is
  400. // set, TunnelPoolSize must be 1.
  401. PacketTunnelTunFileDescriptor int
  402. // SessionID specifies a client session ID to use in the Psiphon API. The
  403. // session ID should be a randomly generated value that is used only for a
  404. // single session, which is defined as the period between a user starting
  405. // a Psiphon client and stopping the client.
  406. //
  407. // A session ID must be 32 hex digits (lower case). When blank, a random
  408. // session ID is automatically generated. Supply a session ID when a
  409. // single client session will cross multiple Controller instances.
  410. SessionID string
  411. // Authorizations is a list of encoded, signed access control
  412. // authorizations that the client has obtained and will present to the
  413. // server.
  414. Authorizations []string
  415. // ServerEntrySignaturePublicKey is a base64-encoded, ed25519 public
  416. // key value used to verify individual server entry signatures. This value
  417. // is supplied by and depends on the Psiphon Network, and is typically
  418. // embedded in the client binary.
  419. ServerEntrySignaturePublicKey string
  420. // ExchangeObfuscationKey is a base64-encoded, NaCl secretbox key used to
  421. // obfuscate server info exchanges between clients.
  422. // Required for the exchange functionality.
  423. ExchangeObfuscationKey string
  424. // EmitTapdanceLogs indicates whether to emit gotapdance log messages
  425. // to stdout. Note that gotapdance log messages do not conform to the
  426. // Notice format standard. Default is off.
  427. EmitTapdanceLogs bool
  428. // TransformHostNameProbability is for testing purposes.
  429. TransformHostNameProbability *float64
  430. // FragmentorProbability and associated Fragmentor fields are for testing
  431. // purposes.
  432. FragmentorProbability *float64
  433. FragmentorLimitProtocols []string
  434. FragmentorMinTotalBytes *int
  435. FragmentorMaxTotalBytes *int
  436. FragmentorMinWriteBytes *int
  437. FragmentorMaxWriteBytes *int
  438. FragmentorMinDelayMicroseconds *int
  439. FragmentorMaxDelayMicroseconds *int
  440. // MeekTrafficShapingProbability and associated fields are for testing
  441. // purposes.
  442. MeekTrafficShapingProbability *float64
  443. MeekTrafficShapingLimitProtocols []string
  444. MeekMinTLSPadding *int
  445. MeekMaxTLSPadding *int
  446. MeekMinLimitRequestPayloadLength *int
  447. MeekMaxLimitRequestPayloadLength *int
  448. MeekRedialTLSProbability *float64
  449. // ObfuscatedSSHAlgorithms and associated ObfuscatedSSH fields are for
  450. // testing purposes. If specified, ObfuscatedSSHAlgorithms must have 4 SSH
  451. // KEX elements in order: the kex algorithm, cipher, MAC, and server host
  452. // key algorithm.
  453. ObfuscatedSSHAlgorithms []string
  454. ObfuscatedSSHMinPadding *int
  455. ObfuscatedSSHMaxPadding *int
  456. // LivenessTestMinUpstreamBytes and other LivenessTest fields are for
  457. // testing purposes.
  458. LivenessTestMinUpstreamBytes *int
  459. LivenessTestMaxUpstreamBytes *int
  460. LivenessTestMinDownstreamBytes *int
  461. LivenessTestMaxDownstreamBytes *int
  462. // ReplayCandidateCount and other Replay fields are for testing purposes.
  463. ReplayCandidateCount *int
  464. ReplayDialParametersTTLSeconds *int
  465. ReplayTargetUpstreamBytes *int
  466. ReplayTargetDownstreamBytes *int
  467. ReplayTargetTunnelDurationSeconds *int
  468. ReplayLaterRoundMoveToFrontProbability *float64
  469. ReplayRetainFailedProbability *float64
  470. // NetworkLatencyMultiplierMin and other NetworkLatencyMultiplier fields are
  471. // for testing purposes.
  472. NetworkLatencyMultiplierMin float64
  473. NetworkLatencyMultiplierMax float64
  474. NetworkLatencyMultiplierLambda float64
  475. // UseOnlyCustomTLSProfiles and other TLS configuration fields are for
  476. // testing purposes.
  477. UseOnlyCustomTLSProfiles *bool
  478. CustomTLSProfiles protocol.CustomTLSProfiles
  479. SelectRandomizedTLSProfileProbability *float64
  480. NoDefaultTLSSessionIDProbability *float64
  481. // ApplicationParameters is for testing purposes.
  482. ApplicationParameters parameters.KeyValues
  483. // clientParameters is the active ClientParameters with defaults, config
  484. // values, and, optionally, tactics applied.
  485. //
  486. // New tactics must be applied by calling Config.SetClientParameters;
  487. // calling clientParameters.Set directly will fail to add config values.
  488. clientParameters *parameters.ClientParameters
  489. dialParametersHash []byte
  490. dynamicConfigMutex sync.Mutex
  491. sponsorID string
  492. authorizations []string
  493. deviceBinder DeviceBinder
  494. networkIDGetter NetworkIDGetter
  495. committed bool
  496. loadTimestamp string
  497. }
  498. // LoadConfig parses a JSON format Psiphon config JSON string and returns a
  499. // Config struct populated with config values.
  500. //
  501. // The Config struct may then be programmatically populated with additional
  502. // values, including callbacks such as DeviceBinder.
  503. //
  504. // Before using the Config, Commit must be called, which will perform further
  505. // validation and initialize internal data structures.
  506. func LoadConfig(configJson []byte) (*Config, error) {
  507. var config Config
  508. err := json.Unmarshal(configJson, &config)
  509. if err != nil {
  510. return nil, errors.Trace(err)
  511. }
  512. config.loadTimestamp = common.TruncateTimestampToHour(
  513. common.GetCurrentTimestamp())
  514. return &config, nil
  515. }
  516. // IsCommitted checks if Commit was called.
  517. func (config *Config) IsCommitted() bool {
  518. return config.committed
  519. }
  520. // Commit validates Config fields finalizes initialization.
  521. //
  522. // Config fields should not be set after calling Config, as any changes may
  523. // not be reflected in internal data structures.
  524. func (config *Config) Commit() error {
  525. // Do SetEmitDiagnosticNotices first, to ensure config file errors are
  526. // emitted.
  527. if config.EmitDiagnosticNotices {
  528. SetEmitDiagnosticNotices(
  529. true, config.EmitDiagnosticNetworkParameters)
  530. }
  531. // Promote legacy fields.
  532. if config.CustomHeaders == nil {
  533. config.CustomHeaders = config.UpstreamProxyCustomHeaders
  534. config.UpstreamProxyCustomHeaders = nil
  535. }
  536. if config.RemoteServerListUrl != "" && config.RemoteServerListURLs == nil {
  537. config.RemoteServerListURLs = promoteLegacyDownloadURL(config.RemoteServerListUrl)
  538. }
  539. if config.ObfuscatedServerListRootURL != "" && config.ObfuscatedServerListRootURLs == nil {
  540. config.ObfuscatedServerListRootURLs = promoteLegacyDownloadURL(config.ObfuscatedServerListRootURL)
  541. }
  542. if config.UpgradeDownloadUrl != "" && config.UpgradeDownloadURLs == nil {
  543. config.UpgradeDownloadURLs = promoteLegacyDownloadURL(config.UpgradeDownloadUrl)
  544. }
  545. if config.TunnelProtocol != "" && len(config.LimitTunnelProtocols) == 0 {
  546. config.LimitTunnelProtocols = []string{config.TunnelProtocol}
  547. }
  548. // Supply default values.
  549. if config.DataStoreDirectory == "" {
  550. wd, err := os.Getwd()
  551. if err != nil {
  552. return errors.Trace(err)
  553. }
  554. config.DataStoreDirectory = wd
  555. }
  556. if config.ClientVersion == "" {
  557. config.ClientVersion = "0"
  558. }
  559. if config.TunnelPoolSize == 0 {
  560. config.TunnelPoolSize = TUNNEL_POOL_SIZE
  561. }
  562. // Validate config fields.
  563. if config.PropagationChannelId == "" {
  564. return errors.TraceNew("propagation channel ID is missing from the configuration file")
  565. }
  566. if config.SponsorId == "" {
  567. return errors.TraceNew("sponsor ID is missing from the configuration file")
  568. }
  569. _, err := strconv.Atoi(config.ClientVersion)
  570. if err != nil {
  571. return errors.Tracef("invalid client version: %s", err)
  572. }
  573. if !common.Contains(
  574. []string{"", protocol.PSIPHON_SSH_API_PROTOCOL, protocol.PSIPHON_WEB_API_PROTOCOL},
  575. config.TargetApiProtocol) {
  576. return errors.TraceNew("invalid TargetApiProtocol")
  577. }
  578. if !config.DisableRemoteServerListFetcher {
  579. if config.RemoteServerListURLs != nil {
  580. if config.RemoteServerListSignaturePublicKey == "" {
  581. return errors.TraceNew("missing RemoteServerListSignaturePublicKey")
  582. }
  583. if config.RemoteServerListDownloadFilename == "" {
  584. return errors.TraceNew("missing RemoteServerListDownloadFilename")
  585. }
  586. }
  587. if config.ObfuscatedServerListRootURLs != nil {
  588. if config.RemoteServerListSignaturePublicKey == "" {
  589. return errors.TraceNew("missing RemoteServerListSignaturePublicKey")
  590. }
  591. if config.ObfuscatedServerListDownloadDirectory == "" {
  592. return errors.TraceNew("missing ObfuscatedServerListDownloadDirectory")
  593. }
  594. }
  595. }
  596. if config.SplitTunnelRoutesURLFormat != "" {
  597. if config.SplitTunnelRoutesSignaturePublicKey == "" {
  598. return errors.TraceNew("missing SplitTunnelRoutesSignaturePublicKey")
  599. }
  600. if config.SplitTunnelDNSServer == "" {
  601. return errors.TraceNew("missing SplitTunnelDNSServer")
  602. }
  603. }
  604. if config.UpgradeDownloadURLs != nil {
  605. if config.UpgradeDownloadClientVersionHeader == "" {
  606. return errors.TraceNew("missing UpgradeDownloadClientVersionHeader")
  607. }
  608. if config.UpgradeDownloadFilename == "" {
  609. return errors.TraceNew("missing UpgradeDownloadFilename")
  610. }
  611. }
  612. // This constraint is expected by logic in Controller.runTunnels().
  613. if config.PacketTunnelTunFileDescriptor > 0 && config.TunnelPoolSize != 1 {
  614. return errors.TraceNew("packet tunnel mode requires TunnelPoolSize to be 1")
  615. }
  616. // SessionID must be PSIPHON_API_CLIENT_SESSION_ID_LENGTH lowercase hex-encoded bytes.
  617. if config.SessionID == "" {
  618. sessionID, err := MakeSessionId()
  619. if err != nil {
  620. return errors.Trace(err)
  621. }
  622. config.SessionID = sessionID
  623. }
  624. if len(config.SessionID) != 2*protocol.PSIPHON_API_CLIENT_SESSION_ID_LENGTH ||
  625. -1 != strings.IndexFunc(config.SessionID, func(c rune) bool {
  626. return !unicode.Is(unicode.ASCII_Hex_Digit, c) || unicode.IsUpper(c)
  627. }) {
  628. return errors.TraceNew("invalid SessionID")
  629. }
  630. config.clientParameters, err = parameters.NewClientParameters(
  631. func(err error) {
  632. NoticeAlert("ClientParameters getValue failed: %s", err)
  633. })
  634. if err != nil {
  635. return errors.Trace(err)
  636. }
  637. if config.ObfuscatedSSHAlgorithms != nil &&
  638. len(config.ObfuscatedSSHAlgorithms) != 4 {
  639. // TODO: validate each algorithm?
  640. return errors.TraceNew("invalid ObfuscatedSSHAlgorithms")
  641. }
  642. // clientParameters.Set will validate the config fields applied to parameters.
  643. err = config.SetClientParameters("", false, nil)
  644. if err != nil {
  645. return errors.Trace(err)
  646. }
  647. // Calculate and set the dial parameters hash. After this point, related
  648. // config fields must not change.
  649. config.setDialParametersHash()
  650. // Set defaults for dynamic config fields.
  651. config.SetDynamicConfig(config.SponsorId, config.Authorizations)
  652. // Initialize config.deviceBinder and config.config.networkIDGetter. These
  653. // wrap config.DeviceBinder and config.NetworkIDGetter/NetworkID with
  654. // loggers.
  655. //
  656. // New variables are set to avoid mutating input config fields.
  657. // Internally, code must use config.deviceBinder and
  658. // config.networkIDGetter and not the input/exported fields.
  659. if config.DeviceBinder != nil {
  660. config.deviceBinder = newLoggingDeviceBinder(config.DeviceBinder)
  661. }
  662. networkIDGetter := config.NetworkIDGetter
  663. if networkIDGetter == nil {
  664. // Limitation: unlike NetworkIDGetter, which calls back to platform APIs
  665. // this method of network identification is not dynamic and will not reflect
  666. // network changes that occur while running.
  667. if config.NetworkID != "" {
  668. networkIDGetter = newStaticNetworkGetter(config.NetworkID)
  669. } else {
  670. networkIDGetter = newStaticNetworkGetter("UNKNOWN")
  671. }
  672. }
  673. config.networkIDGetter = newLoggingNetworkIDGetter(networkIDGetter)
  674. config.committed = true
  675. return nil
  676. }
  677. // GetClientParameters returns a the current client parameters.
  678. func (config *Config) GetClientParameters() *parameters.ClientParameters {
  679. return config.clientParameters
  680. }
  681. // SetClientParameters resets Config.clientParameters to the default values,
  682. // applies any config file values, and then applies the input parameters (from
  683. // tactics, etc.)
  684. //
  685. // Set skipOnError to false when initially applying only config values, as
  686. // this will validate the values and should fail. Set skipOnError to true when
  687. // applying tactics to ignore invalid or unknown parameter values from tactics.
  688. //
  689. // In the case of applying tactics, do not call Config.clientParameters.Set
  690. // directly as this will not first apply config values.
  691. //
  692. // If there is an error, the existing Config.clientParameters are left
  693. // entirely unmodified.
  694. func (config *Config) SetClientParameters(tag string, skipOnError bool, applyParameters map[string]interface{}) error {
  695. setParameters := []map[string]interface{}{config.makeConfigParameters()}
  696. if applyParameters != nil {
  697. setParameters = append(setParameters, applyParameters)
  698. }
  699. counts, err := config.clientParameters.Set(tag, skipOnError, setParameters...)
  700. if err != nil {
  701. return errors.Trace(err)
  702. }
  703. NoticeInfo("applied %v parameters with tag '%s'", counts, tag)
  704. // Emit certain individual parameter values for quick reference in diagnostics.
  705. p := config.clientParameters.Get()
  706. NoticeInfo(
  707. "NetworkLatencyMultiplier Min/Max/Lambda: %f/%f/%f",
  708. p.Float(parameters.NetworkLatencyMultiplierMin),
  709. p.Float(parameters.NetworkLatencyMultiplierMax),
  710. p.Float(parameters.NetworkLatencyMultiplierLambda))
  711. // Application Parameters are feature flags/config info, delivered as Client
  712. // Parameters via tactics/etc., to be communicated to the outer application.
  713. // Emit these now, as notices.
  714. if p.WeightedCoinFlip(parameters.ApplicationParametersProbability) {
  715. NoticeApplicationParameters(p.KeyValues(parameters.ApplicationParameters))
  716. }
  717. return nil
  718. }
  719. // SetDynamicConfig sets the current client sponsor ID and authorizations.
  720. // Invalid values for sponsor ID are ignored. The caller must not modify the
  721. // input authorizations slice.
  722. func (config *Config) SetDynamicConfig(sponsorID string, authorizations []string) {
  723. config.dynamicConfigMutex.Lock()
  724. defer config.dynamicConfigMutex.Unlock()
  725. if sponsorID != "" {
  726. config.sponsorID = sponsorID
  727. }
  728. config.authorizations = authorizations
  729. }
  730. // GetSponsorID returns the current client sponsor ID.
  731. func (config *Config) GetSponsorID() string {
  732. config.dynamicConfigMutex.Lock()
  733. defer config.dynamicConfigMutex.Unlock()
  734. return config.sponsorID
  735. }
  736. // GetAuthorizations returns the current client authorizations.
  737. // The caller must not modify the returned slice.
  738. func (config *Config) GetAuthorizations() []string {
  739. config.dynamicConfigMutex.Lock()
  740. defer config.dynamicConfigMutex.Unlock()
  741. return config.authorizations
  742. }
  743. // UseUpstreamProxy indicates if an upstream proxy has been
  744. // configured.
  745. func (config *Config) UseUpstreamProxy() bool {
  746. return config.UpstreamProxyURL != ""
  747. }
  748. // GetNetworkID returns the current network ID. When NetworkIDGetter
  749. // is set, this calls into the host application; otherwise, a default
  750. // value is returned.
  751. func (config *Config) GetNetworkID() string {
  752. return config.networkIDGetter.GetNetworkID()
  753. }
  754. func (config *Config) makeConfigParameters() map[string]interface{} {
  755. // Build set of config values to apply to parameters.
  756. //
  757. // Note: names of some config fields such as
  758. // StaggerConnectionWorkersMilliseconds and LimitMeekBufferSizes have
  759. // changed in the parameters. The existing config fields are retained for
  760. // backwards compatibility.
  761. applyParameters := make(map[string]interface{})
  762. // To support platform clients that configure NetworkLatencyMultiplier, set
  763. // the NetworkLatencyMultiplierMin/NetworkLatencyMultiplierMax range to the
  764. // specified value. Also set the older NetworkLatencyMultiplier tactic, since
  765. // that will be used in the case of replaying with dial parameters persisted
  766. // by an older client version.
  767. if config.NetworkLatencyMultiplier > 0.0 {
  768. applyParameters[parameters.NetworkLatencyMultiplier] = config.NetworkLatencyMultiplier
  769. applyParameters[parameters.NetworkLatencyMultiplierMin] = config.NetworkLatencyMultiplier
  770. applyParameters[parameters.NetworkLatencyMultiplierMax] = config.NetworkLatencyMultiplier
  771. }
  772. if config.NetworkLatencyMultiplierMin > 0.0 {
  773. applyParameters[parameters.NetworkLatencyMultiplierMin] = config.NetworkLatencyMultiplierMin
  774. }
  775. if config.NetworkLatencyMultiplierMax > 0.0 {
  776. applyParameters[parameters.NetworkLatencyMultiplierMax] = config.NetworkLatencyMultiplierMax
  777. }
  778. if config.NetworkLatencyMultiplierLambda > 0.0 {
  779. applyParameters[parameters.NetworkLatencyMultiplierLambda] = config.NetworkLatencyMultiplierLambda
  780. }
  781. if len(config.LimitTunnelProtocols) > 0 {
  782. applyParameters[parameters.LimitTunnelProtocols] = protocol.TunnelProtocols(config.LimitTunnelProtocols)
  783. }
  784. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  785. applyParameters[parameters.InitialLimitTunnelProtocols] = protocol.TunnelProtocols(config.InitialLimitTunnelProtocols)
  786. applyParameters[parameters.InitialLimitTunnelProtocolsCandidateCount] = config.InitialLimitTunnelProtocolsCandidateCount
  787. }
  788. if len(config.LimitTLSProfiles) > 0 {
  789. applyParameters[parameters.LimitTLSProfiles] = protocol.TunnelProtocols(config.LimitTLSProfiles)
  790. }
  791. if len(config.LimitQUICVersions) > 0 {
  792. applyParameters[parameters.LimitQUICVersions] = protocol.QUICVersions(config.LimitQUICVersions)
  793. }
  794. if config.EstablishTunnelTimeoutSeconds != nil {
  795. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  796. }
  797. if config.EstablishTunnelServerAffinityGracePeriodMilliseconds != nil {
  798. applyParameters[parameters.EstablishTunnelServerAffinityGracePeriod] = fmt.Sprintf("%dms", *config.EstablishTunnelServerAffinityGracePeriodMilliseconds)
  799. }
  800. if config.EstablishTunnelPausePeriodSeconds != nil {
  801. applyParameters[parameters.EstablishTunnelPausePeriod] = fmt.Sprintf("%ds", *config.EstablishTunnelPausePeriodSeconds)
  802. }
  803. if config.ConnectionWorkerPoolSize != 0 {
  804. applyParameters[parameters.ConnectionWorkerPoolSize] = config.ConnectionWorkerPoolSize
  805. }
  806. if config.StaggerConnectionWorkersMilliseconds > 0 {
  807. applyParameters[parameters.StaggerConnectionWorkersPeriod] = fmt.Sprintf("%dms", config.StaggerConnectionWorkersMilliseconds)
  808. }
  809. if config.LimitIntensiveConnectionWorkers > 0 {
  810. applyParameters[parameters.LimitIntensiveConnectionWorkers] = config.LimitIntensiveConnectionWorkers
  811. }
  812. applyParameters[parameters.MeekLimitBufferSizes] = config.LimitMeekBufferSizes
  813. applyParameters[parameters.IgnoreHandshakeStatsRegexps] = config.IgnoreHandshakeStatsRegexps
  814. if config.EstablishTunnelTimeoutSeconds != nil {
  815. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  816. }
  817. if config.FetchRemoteServerListRetryPeriodMilliseconds != nil {
  818. applyParameters[parameters.FetchRemoteServerListRetryPeriod] = fmt.Sprintf("%dms", *config.FetchRemoteServerListRetryPeriodMilliseconds)
  819. }
  820. if config.FetchUpgradeRetryPeriodMilliseconds != nil {
  821. applyParameters[parameters.FetchUpgradeRetryPeriod] = fmt.Sprintf("%dms", *config.FetchUpgradeRetryPeriodMilliseconds)
  822. }
  823. if !config.DisableRemoteServerListFetcher {
  824. if config.RemoteServerListURLs != nil {
  825. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  826. applyParameters[parameters.RemoteServerListURLs] = config.RemoteServerListURLs
  827. }
  828. if config.ObfuscatedServerListRootURLs != nil {
  829. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  830. applyParameters[parameters.ObfuscatedServerListRootURLs] = config.ObfuscatedServerListRootURLs
  831. }
  832. }
  833. applyParameters[parameters.SplitTunnelRoutesURLFormat] = config.SplitTunnelRoutesURLFormat
  834. applyParameters[parameters.SplitTunnelRoutesSignaturePublicKey] = config.SplitTunnelRoutesSignaturePublicKey
  835. applyParameters[parameters.SplitTunnelDNSServer] = config.SplitTunnelDNSServer
  836. if config.UpgradeDownloadURLs != nil {
  837. applyParameters[parameters.UpgradeDownloadClientVersionHeader] = config.UpgradeDownloadClientVersionHeader
  838. applyParameters[parameters.UpgradeDownloadURLs] = config.UpgradeDownloadURLs
  839. }
  840. applyParameters[parameters.TunnelRateLimits] = config.RateLimits
  841. if config.TransformHostNameProbability != nil {
  842. applyParameters[parameters.TransformHostNameProbability] = *config.TransformHostNameProbability
  843. }
  844. if config.FragmentorProbability != nil {
  845. applyParameters[parameters.FragmentorProbability] = *config.FragmentorProbability
  846. }
  847. if len(config.FragmentorLimitProtocols) > 0 {
  848. applyParameters[parameters.FragmentorLimitProtocols] = protocol.TunnelProtocols(config.FragmentorLimitProtocols)
  849. }
  850. if config.FragmentorMinTotalBytes != nil {
  851. applyParameters[parameters.FragmentorMinTotalBytes] = *config.FragmentorMinTotalBytes
  852. }
  853. if config.FragmentorMaxTotalBytes != nil {
  854. applyParameters[parameters.FragmentorMaxTotalBytes] = *config.FragmentorMaxTotalBytes
  855. }
  856. if config.FragmentorMinWriteBytes != nil {
  857. applyParameters[parameters.FragmentorMinWriteBytes] = *config.FragmentorMinWriteBytes
  858. }
  859. if config.FragmentorMaxWriteBytes != nil {
  860. applyParameters[parameters.FragmentorMaxWriteBytes] = *config.FragmentorMaxWriteBytes
  861. }
  862. if config.FragmentorMinDelayMicroseconds != nil {
  863. applyParameters[parameters.FragmentorMinDelay] = fmt.Sprintf("%dus", *config.FragmentorMinDelayMicroseconds)
  864. }
  865. if config.FragmentorMaxDelayMicroseconds != nil {
  866. applyParameters[parameters.FragmentorMaxDelay] = fmt.Sprintf("%dus", *config.FragmentorMaxDelayMicroseconds)
  867. }
  868. if config.MeekTrafficShapingProbability != nil {
  869. applyParameters[parameters.MeekTrafficShapingProbability] = *config.MeekTrafficShapingProbability
  870. }
  871. if len(config.MeekTrafficShapingLimitProtocols) > 0 {
  872. applyParameters[parameters.MeekTrafficShapingLimitProtocols] = protocol.TunnelProtocols(config.MeekTrafficShapingLimitProtocols)
  873. }
  874. if config.MeekMinTLSPadding != nil {
  875. applyParameters[parameters.MeekMinTLSPadding] = *config.MeekMinTLSPadding
  876. }
  877. if config.MeekMaxTLSPadding != nil {
  878. applyParameters[parameters.MeekMaxTLSPadding] = *config.MeekMaxTLSPadding
  879. }
  880. if config.MeekMinLimitRequestPayloadLength != nil {
  881. applyParameters[parameters.MeekMinLimitRequestPayloadLength] = *config.MeekMinLimitRequestPayloadLength
  882. }
  883. if config.MeekMaxLimitRequestPayloadLength != nil {
  884. applyParameters[parameters.MeekMaxLimitRequestPayloadLength] = *config.MeekMaxLimitRequestPayloadLength
  885. }
  886. if config.MeekRedialTLSProbability != nil {
  887. applyParameters[parameters.MeekRedialTLSProbability] = *config.MeekRedialTLSProbability
  888. }
  889. if config.ObfuscatedSSHMinPadding != nil {
  890. applyParameters[parameters.ObfuscatedSSHMinPadding] = *config.ObfuscatedSSHMinPadding
  891. }
  892. if config.ObfuscatedSSHMaxPadding != nil {
  893. applyParameters[parameters.ObfuscatedSSHMaxPadding] = *config.ObfuscatedSSHMaxPadding
  894. }
  895. if config.LivenessTestMinUpstreamBytes != nil {
  896. applyParameters[parameters.LivenessTestMinUpstreamBytes] = *config.LivenessTestMinUpstreamBytes
  897. }
  898. if config.LivenessTestMaxUpstreamBytes != nil {
  899. applyParameters[parameters.LivenessTestMaxUpstreamBytes] = *config.LivenessTestMaxUpstreamBytes
  900. }
  901. if config.LivenessTestMinDownstreamBytes != nil {
  902. applyParameters[parameters.LivenessTestMinDownstreamBytes] = *config.LivenessTestMinDownstreamBytes
  903. }
  904. if config.LivenessTestMaxDownstreamBytes != nil {
  905. applyParameters[parameters.LivenessTestMaxDownstreamBytes] = *config.LivenessTestMaxDownstreamBytes
  906. }
  907. if config.ReplayCandidateCount != nil {
  908. applyParameters[parameters.ReplayCandidateCount] = *config.ReplayCandidateCount
  909. }
  910. if config.ReplayDialParametersTTLSeconds != nil {
  911. applyParameters[parameters.ReplayDialParametersTTL] = fmt.Sprintf("%ds", *config.ReplayDialParametersTTLSeconds)
  912. }
  913. if config.ReplayTargetUpstreamBytes != nil {
  914. applyParameters[parameters.ReplayTargetUpstreamBytes] = *config.ReplayTargetUpstreamBytes
  915. }
  916. if config.ReplayTargetDownstreamBytes != nil {
  917. applyParameters[parameters.ReplayTargetDownstreamBytes] = *config.ReplayTargetDownstreamBytes
  918. }
  919. if config.ReplayTargetTunnelDurationSeconds != nil {
  920. applyParameters[parameters.ReplayTargetTunnelDuration] = fmt.Sprintf("%ds", *config.ReplayTargetTunnelDurationSeconds)
  921. }
  922. if config.ReplayLaterRoundMoveToFrontProbability != nil {
  923. applyParameters[parameters.ReplayLaterRoundMoveToFrontProbability] = *config.ReplayLaterRoundMoveToFrontProbability
  924. }
  925. if config.ReplayRetainFailedProbability != nil {
  926. applyParameters[parameters.ReplayRetainFailedProbability] = *config.ReplayRetainFailedProbability
  927. }
  928. if config.UseOnlyCustomTLSProfiles != nil {
  929. applyParameters[parameters.UseOnlyCustomTLSProfiles] = *config.UseOnlyCustomTLSProfiles
  930. }
  931. if config.CustomTLSProfiles != nil {
  932. applyParameters[parameters.CustomTLSProfiles] = config.CustomTLSProfiles
  933. }
  934. if config.SelectRandomizedTLSProfileProbability != nil {
  935. applyParameters[parameters.SelectRandomizedTLSProfileProbability] = *config.SelectRandomizedTLSProfileProbability
  936. }
  937. if config.NoDefaultTLSSessionIDProbability != nil {
  938. applyParameters[parameters.NoDefaultTLSSessionIDProbability] = *config.NoDefaultTLSSessionIDProbability
  939. }
  940. if config.ApplicationParameters != nil {
  941. applyParameters[parameters.ApplicationParameters] = config.ApplicationParameters
  942. }
  943. return applyParameters
  944. }
  945. func (config *Config) setDialParametersHash() {
  946. // Calculate and store a hash of the config values that may impact
  947. // dial parameters. This hash is used as part of the dial parameters
  948. // replay mechanism to detect when persisted dial parameters should
  949. // be discarded due to conflicting config changes.
  950. //
  951. // MD5 hash is used solely as a data checksum and not for any security
  952. // purpose; serialization is not strictly unambiguous.
  953. hash := md5.New()
  954. if len(config.LimitTunnelProtocols) > 0 {
  955. for _, protocol := range config.LimitTunnelProtocols {
  956. hash.Write([]byte(protocol))
  957. }
  958. }
  959. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  960. for _, protocol := range config.InitialLimitTunnelProtocols {
  961. hash.Write([]byte(protocol))
  962. }
  963. binary.Write(hash, binary.LittleEndian, int64(config.InitialLimitTunnelProtocolsCandidateCount))
  964. }
  965. if len(config.LimitTLSProfiles) > 0 {
  966. for _, profile := range config.LimitTLSProfiles {
  967. hash.Write([]byte(profile))
  968. }
  969. }
  970. if len(config.LimitQUICVersions) > 0 {
  971. for _, version := range config.LimitQUICVersions {
  972. hash.Write([]byte(version))
  973. }
  974. }
  975. // Whether a custom User-Agent is specified is a binary flag: when not set,
  976. // the replay dial parameters value applies. When set, external
  977. // considerations apply.
  978. if _, ok := config.CustomHeaders["User-Agent"]; ok {
  979. hash.Write([]byte{1})
  980. }
  981. if config.UpstreamProxyURL != "" {
  982. hash.Write([]byte(config.UpstreamProxyURL))
  983. }
  984. if config.TransformHostNameProbability != nil {
  985. binary.Write(hash, binary.LittleEndian, *config.TransformHostNameProbability)
  986. }
  987. if config.FragmentorProbability != nil {
  988. binary.Write(hash, binary.LittleEndian, *config.FragmentorProbability)
  989. }
  990. if len(config.FragmentorLimitProtocols) > 0 {
  991. for _, protocol := range config.FragmentorLimitProtocols {
  992. hash.Write([]byte(protocol))
  993. }
  994. }
  995. if config.FragmentorMinTotalBytes != nil {
  996. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinTotalBytes))
  997. }
  998. if config.FragmentorMaxTotalBytes != nil {
  999. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxTotalBytes))
  1000. }
  1001. if config.FragmentorMinWriteBytes != nil {
  1002. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinWriteBytes))
  1003. }
  1004. if config.FragmentorMaxWriteBytes != nil {
  1005. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxWriteBytes))
  1006. }
  1007. if config.FragmentorMinDelayMicroseconds != nil {
  1008. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinDelayMicroseconds))
  1009. }
  1010. if config.FragmentorMaxDelayMicroseconds != nil {
  1011. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxDelayMicroseconds))
  1012. }
  1013. if config.MeekTrafficShapingProbability != nil {
  1014. binary.Write(hash, binary.LittleEndian, int64(*config.MeekTrafficShapingProbability))
  1015. }
  1016. if len(config.MeekTrafficShapingLimitProtocols) > 0 {
  1017. for _, protocol := range config.MeekTrafficShapingLimitProtocols {
  1018. hash.Write([]byte(protocol))
  1019. }
  1020. }
  1021. if config.MeekMinLimitRequestPayloadLength != nil {
  1022. binary.Write(hash, binary.LittleEndian, int64(*config.MeekMinLimitRequestPayloadLength))
  1023. }
  1024. if config.MeekMaxLimitRequestPayloadLength != nil {
  1025. binary.Write(hash, binary.LittleEndian, int64(*config.MeekMaxLimitRequestPayloadLength))
  1026. }
  1027. if config.MeekRedialTLSProbability != nil {
  1028. binary.Write(hash, binary.LittleEndian, *config.MeekRedialTLSProbability)
  1029. }
  1030. if config.ObfuscatedSSHMinPadding != nil {
  1031. binary.Write(hash, binary.LittleEndian, int64(*config.ObfuscatedSSHMinPadding))
  1032. }
  1033. if config.ObfuscatedSSHMaxPadding != nil {
  1034. binary.Write(hash, binary.LittleEndian, int64(*config.ObfuscatedSSHMaxPadding))
  1035. }
  1036. if config.LivenessTestMinUpstreamBytes != nil {
  1037. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMinUpstreamBytes))
  1038. }
  1039. if config.LivenessTestMaxUpstreamBytes != nil {
  1040. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMaxUpstreamBytes))
  1041. }
  1042. if config.LivenessTestMinDownstreamBytes != nil {
  1043. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMinDownstreamBytes))
  1044. }
  1045. if config.LivenessTestMaxDownstreamBytes != nil {
  1046. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMaxDownstreamBytes))
  1047. }
  1048. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierMin)
  1049. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierMax)
  1050. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierLambda)
  1051. if config.UseOnlyCustomTLSProfiles != nil {
  1052. binary.Write(hash, binary.LittleEndian, *config.UseOnlyCustomTLSProfiles)
  1053. }
  1054. for _, customTLSProfile := range config.CustomTLSProfiles {
  1055. // Assumes consistent definition for a given profile name
  1056. hash.Write([]byte(customTLSProfile.Name))
  1057. }
  1058. if config.SelectRandomizedTLSProfileProbability != nil {
  1059. binary.Write(hash, binary.LittleEndian, *config.SelectRandomizedTLSProfileProbability)
  1060. }
  1061. if config.NoDefaultTLSSessionIDProbability != nil {
  1062. binary.Write(hash, binary.LittleEndian, *config.NoDefaultTLSSessionIDProbability)
  1063. }
  1064. config.dialParametersHash = hash.Sum(nil)
  1065. }
  1066. func promoteLegacyDownloadURL(URL string) parameters.DownloadURLs {
  1067. downloadURLs := make(parameters.DownloadURLs, 1)
  1068. downloadURLs[0] = &parameters.DownloadURL{
  1069. URL: base64.StdEncoding.EncodeToString([]byte(URL)),
  1070. SkipVerify: false,
  1071. OnlyAfterAttempts: 0,
  1072. }
  1073. return downloadURLs
  1074. }
  1075. type loggingDeviceBinder struct {
  1076. d DeviceBinder
  1077. }
  1078. func newLoggingDeviceBinder(d DeviceBinder) *loggingDeviceBinder {
  1079. return &loggingDeviceBinder{d: d}
  1080. }
  1081. func (d *loggingDeviceBinder) BindToDevice(fileDescriptor int) (string, error) {
  1082. deviceInfo, err := d.d.BindToDevice(fileDescriptor)
  1083. if err == nil && deviceInfo != "" {
  1084. NoticeBindToDevice(deviceInfo)
  1085. }
  1086. return deviceInfo, err
  1087. }
  1088. type staticNetworkGetter struct {
  1089. networkID string
  1090. }
  1091. func newStaticNetworkGetter(networkID string) *staticNetworkGetter {
  1092. return &staticNetworkGetter{networkID: networkID}
  1093. }
  1094. func (n *staticNetworkGetter) GetNetworkID() string {
  1095. return n.networkID
  1096. }
  1097. type loggingNetworkIDGetter struct {
  1098. n NetworkIDGetter
  1099. }
  1100. func newLoggingNetworkIDGetter(n NetworkIDGetter) *loggingNetworkIDGetter {
  1101. return &loggingNetworkIDGetter{n: n}
  1102. }
  1103. func (n *loggingNetworkIDGetter) GetNetworkID() string {
  1104. networkID := n.n.GetNetworkID()
  1105. // All PII must appear after the initial "-"
  1106. // See: https://godoc.org/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon#NetworkIDGetter
  1107. logNetworkID := networkID
  1108. index := strings.Index(logNetworkID, "-")
  1109. if index != -1 {
  1110. logNetworkID = logNetworkID[:index]
  1111. }
  1112. if len(logNetworkID)+1 < len(networkID) {
  1113. // Indicate when additional network info was present after the first "-".
  1114. logNetworkID += "+[redacted]"
  1115. }
  1116. NoticeNetworkID(logNetworkID)
  1117. return networkID
  1118. }