config.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. "encoding/base64"
  22. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "net/http"
  26. "os"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "unicode"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  34. )
  35. const (
  36. TUNNEL_POOL_SIZE = 1
  37. )
  38. // Config is the Psiphon configuration specified by the application. This
  39. // configuration controls the behavior of the core tunnel functionality.
  40. //
  41. // To distinguish omitted timeout params from explicit 0 value timeout params,
  42. // corresponding fieldss are int pointers. nil means no value was supplied and
  43. // to use the default; a non-nil pointer to 0 means no timeout.
  44. type Config struct {
  45. // DataStoreDirectory is the directory in which to store the persistent
  46. // database, which contains information such as server entries. By
  47. // default, current working directory.
  48. //
  49. // Warning: If the datastore file, DataStoreDirectory/DATA_STORE_FILENAME,
  50. // exists but fails to open for any reason (checksum error, unexpected
  51. // file format, etc.) it will be deleted in order to pave a new datastore
  52. // and continue running.
  53. DataStoreDirectory string
  54. // PropagationChannelId is a string identifier which indicates how the
  55. // Psiphon client was distributed. This parameter is required. This value
  56. // is supplied by and depends on the Psiphon Network, and is typically
  57. // embedded in the client binary.
  58. PropagationChannelId string
  59. // SponsorId is a string identifier which indicates who is sponsoring this
  60. // Psiphon client. One purpose of this value is to determine the home
  61. // pages for display. This parameter is required. This value is supplied
  62. // by and depends on the Psiphon Network, and is typically embedded in the
  63. // client binary.
  64. SponsorId string
  65. // ClientVersion is the client version number that the client reports to
  66. // the server. The version number refers to the host client application,
  67. // not the core tunnel library. One purpose of this value is to enable
  68. // automatic updates. This value is supplied by and depends on the Psiphon
  69. // Network, and is typically embedded in the client binary.
  70. //
  71. // Note that sending a ClientPlatform string which includes "windows"
  72. // (case insensitive) and a ClientVersion of <= 44 will cause an error in
  73. // processing the response to DoConnectedRequest calls.
  74. ClientVersion string
  75. // ClientPlatform is the client platform ("Windows", "Android", etc.) that
  76. // the client reports to the server.
  77. ClientPlatform string
  78. // TunnelWholeDevice is a flag that is passed through to the handshake
  79. // request for stats purposes. Set to 1 when the host application is
  80. // tunneling the whole device, 0 otherwise.
  81. TunnelWholeDevice int
  82. // EgressRegion is a ISO 3166-1 alpha-2 country code which indicates which
  83. // country to egress from. For the default, "", the best performing server
  84. // in any country is selected.
  85. EgressRegion string
  86. // ListenInterface specifies which interface to listen on. If no
  87. // interface is provided then listen on 127.0.0.1. If 'any' is provided
  88. // then use 0.0.0.0. If there are multiple IP addresses on an interface
  89. // use the first IPv4 address.
  90. ListenInterface string
  91. // DisableLocalSocksProxy disables running the local SOCKS proxy.
  92. DisableLocalSocksProxy bool
  93. // LocalSocksProxyPort specifies a port number for the local SOCKS proxy
  94. // running at 127.0.0.1. For the default value, 0, the system selects a
  95. // free port (a notice reporting the selected port is emitted).
  96. LocalSocksProxyPort int
  97. // LocalHttpProxyPort specifies a port number for the local HTTP proxy
  98. // running at 127.0.0.1. For the default value, 0, the system selects a
  99. // free port (a notice reporting the selected port is emitted).
  100. LocalHttpProxyPort int
  101. // DisableLocalHTTPProxy disables running the local HTTP proxy.
  102. DisableLocalHTTPProxy bool
  103. // NetworkLatencyMultiplier is a multiplier that is to be applied to
  104. // default network event timeouts. Set this to tune performance for
  105. // slow networks.
  106. // When set, must be >= 1.0.
  107. NetworkLatencyMultiplier float64
  108. // TunnelProtocol indicates which protocol to use. For the default, "",
  109. // all protocols are used.
  110. //
  111. // Deprecated: Use LimitTunnelProtocols. When LimitTunnelProtocols is not
  112. // nil, this parameter is ignored.
  113. TunnelProtocol string
  114. // LimitTunnelProtocols indicates which protocols to use. Valid values
  115. // include:
  116. // "SSH", "OSSH", "UNFRONTED-MEEK-OSSH", "UNFRONTED-MEEK-HTTPS-OSSH",
  117. // "UNFRONTED-MEEK-SESSION-TICKET-OSSH", "FRONTED-MEEK-OSSH",
  118. // "FRONTED-MEEK-HTTP-OSSH", "QUIC-OSSH", "MARIONETTE-OSSH", and
  119. // "TAPDANCE-OSSH".
  120. // For the default, an empty list, all protocols are used.
  121. LimitTunnelProtocols []string
  122. // InitialLimitTunnelProtocols is an optional initial phase of limited
  123. // protocols for the first InitialLimitTunnelProtocolsCandidateCount
  124. // candidates; after these candidates, LimitTunnelProtocols applies.
  125. //
  126. // For the default, an empty list, InitialLimitTunnelProtocols is off.
  127. InitialLimitTunnelProtocols []string
  128. // InitialLimitTunnelProtocolsCandidateCount is the number of candidates
  129. // to which InitialLimitTunnelProtocols is applied instead of
  130. // LimitTunnelProtocols.
  131. //
  132. // For the default, 0, InitialLimitTunnelProtocols is off.
  133. InitialLimitTunnelProtocolsCandidateCount int
  134. // LimitTLSProfiles indicates which TLS profiles to select from. Valid
  135. // values are listed in protocols.SupportedTLSProfiles.
  136. // For the default, an empty list, all profiles are candidates for
  137. // selection.
  138. LimitTLSProfiles []string
  139. // LimitQUICVersions indicates which QUIC versions to select from. Valid
  140. // values are listed in protocols.SupportedQUICVersions.
  141. // For the default, an empty list, all versions are candidates for
  142. // selection.
  143. LimitQUICVersions []string
  144. // EstablishTunnelTimeoutSeconds specifies a time limit after which to
  145. // halt the core tunnel controller if no tunnel has been established. The
  146. // default is parameters.EstablishTunnelTimeoutSeconds.
  147. EstablishTunnelTimeoutSeconds *int
  148. // EstablishTunnelPausePeriodSeconds specifies the delay between attempts
  149. // to establish tunnels. Briefly pausing allows for network conditions to
  150. // improve and for asynchronous operations such as fetch remote server
  151. // list to complete. If omitted, a default value is used. This value is
  152. // typical overridden for testing.
  153. EstablishTunnelPausePeriodSeconds *int
  154. // ConnectionWorkerPoolSize specifies how many connection attempts to
  155. // attempt in parallel. If omitted of when 0, a default is used; this is
  156. // recommended.
  157. ConnectionWorkerPoolSize int
  158. // TunnelPoolSize specifies how many tunnels to run in parallel. Port
  159. // forwards are multiplexed over multiple tunnels. If omitted or when 0,
  160. // the default is TUNNEL_POOL_SIZE, which is recommended.
  161. TunnelPoolSize int
  162. // StaggerConnectionWorkersMilliseconds adds a specified delay before
  163. // making each server candidate available to connection workers. This
  164. // option is enabled when StaggerConnectionWorkersMilliseconds > 0.
  165. StaggerConnectionWorkersMilliseconds int
  166. // LimitIntensiveConnectionWorkers limits the number of concurrent
  167. // connection workers attempting connections with resource intensive
  168. // protocols. This option is enabled when LimitIntensiveConnectionWorkers
  169. // > 0.
  170. LimitIntensiveConnectionWorkers int
  171. // LimitMeekBufferSizes selects smaller buffers for meek protocols.
  172. LimitMeekBufferSizes bool
  173. // IgnoreHandshakeStatsRegexps skips compiling and using stats regexes.
  174. IgnoreHandshakeStatsRegexps bool
  175. // UpstreamProxyURL is a URL specifying an upstream proxy to use for all
  176. // outbound connections. The URL should include proxy type and
  177. // authentication information, as required. See example URLs here:
  178. // https://github.com/Psiphon-Labs/psiphon-tunnel-core/tree/master/psiphon/upstreamproxy
  179. UpstreamProxyURL string
  180. // CustomHeaders is a set of additional arbitrary HTTP headers that are
  181. // added to all plaintext HTTP requests and requests made through an HTTP
  182. // upstream proxy when specified by UpstreamProxyURL.
  183. CustomHeaders http.Header
  184. // Deprecated: Use CustomHeaders. When CustomHeaders is not nil, this
  185. // parameter is ignored.
  186. UpstreamProxyCustomHeaders http.Header
  187. // NetworkConnectivityChecker is an interface that enables tunnel-core to
  188. // call into the host application to check for network connectivity. See:
  189. // NetworkConnectivityChecker doc.
  190. //
  191. // This parameter is only applicable to library deployments.
  192. NetworkConnectivityChecker NetworkConnectivityChecker
  193. // DeviceBinder is an interface that enables tunnel-core to call into the
  194. // host application to bind sockets to specific devices. See: DeviceBinder
  195. // doc.
  196. //
  197. // This parameter is only applicable to library deployments.
  198. DeviceBinder DeviceBinder
  199. // IPv6Synthesizer is an interface that allows tunnel-core to call into
  200. // the host application to synthesize IPv6 addresses. See: IPv6Synthesizer
  201. // doc.
  202. //
  203. // This parameter is only applicable to library deployments.
  204. IPv6Synthesizer IPv6Synthesizer
  205. // DnsServerGetter is an interface that enables tunnel-core to call into
  206. // the host application to discover the native network DNS server
  207. // settings. See: DnsServerGetter doc.
  208. //
  209. // This parameter is only applicable to library deployments.
  210. DnsServerGetter DnsServerGetter
  211. // NetworkIDGetter in an interface that enables tunnel-core to call into
  212. // the host application to get an identifier for the host's current active
  213. // network. See: NetworkIDGetter doc.
  214. //
  215. // This parameter is only applicable to library deployments.
  216. NetworkIDGetter NetworkIDGetter
  217. // NetworkID, when not blank, is used as the identifier for the host's
  218. // current active network.
  219. // NetworkID is ignored when NetworkIDGetter is set.
  220. NetworkID string
  221. // DisableTactics disables tactics operations including requests, payload
  222. // handling, and application of parameters.
  223. DisableTactics bool
  224. // TransformHostNames specifies whether to use hostname transformation
  225. // circumvention strategies. Set to "always" to always transform, "never"
  226. // to never transform, and "", the default, for the default transformation
  227. // strategy.
  228. TransformHostNames string
  229. // TargetServerEntry is an encoded server entry. When specified, this
  230. // server entry is used exclusively and all other known servers are
  231. // ignored.
  232. TargetServerEntry string
  233. // DisableApi disables Psiphon server API calls including handshake,
  234. // connected, status, etc. This is used for special case temporary tunnels
  235. // (Windows VPN mode).
  236. DisableApi bool
  237. // TargetApiProtocol specifies whether to force use of "ssh" or "web" API
  238. // protocol. When blank, the default, the optimal API protocol is used.
  239. // Note that this capability check is not applied before the
  240. // "CandidateServers" count is emitted.
  241. //
  242. // This parameter is intended for testing and debugging only. Not all
  243. // parameters are supported in the legacy "web" API protocol, including
  244. // speed test samples.
  245. TargetApiProtocol string
  246. // RemoteServerListUrl is a URL which specifies a location to fetch out-
  247. // of-band server entries. This facility is used when a tunnel cannot be
  248. // established to known servers. This value is supplied by and depends on
  249. // the Psiphon Network, and is typically embedded in the client binary.
  250. //
  251. // Deprecated: Use RemoteServerListURLs. When RemoteServerListURLs is not
  252. // nil, this parameter is ignored.
  253. RemoteServerListUrl string
  254. // RemoteServerListURLs is list of URLs which specify locations to fetch
  255. // out-of-band server entries. This facility is used when a tunnel cannot
  256. // be established to known servers. This value is supplied by and depends
  257. // on the Psiphon Network, and is typically embedded in the client binary.
  258. // All URLs must point to the same entity with the same ETag. At least one
  259. // DownloadURL must have OnlyAfterAttempts = 0.
  260. RemoteServerListURLs parameters.DownloadURLs
  261. // RemoteServerListDownloadFilename specifies a target filename for
  262. // storing the remote server list download. Data is stored in co-located
  263. // files (RemoteServerListDownloadFilename.part*) to allow for resumable
  264. // downloading.
  265. RemoteServerListDownloadFilename string
  266. // RemoteServerListSignaturePublicKey specifies a public key that's used
  267. // to authenticate the remote server list payload. This value is supplied
  268. // by and depends on the Psiphon Network, and is typically embedded in the
  269. // client binary.
  270. RemoteServerListSignaturePublicKey string
  271. // DisableRemoteServerListFetcher disables fetching remote server lists.
  272. // This is used for special case temporary tunnels.
  273. DisableRemoteServerListFetcher bool
  274. // FetchRemoteServerListRetryPeriodMilliseconds specifies the delay before
  275. // resuming a remote server list download after a failure. If omitted, a
  276. // default value is used. This value is typical overridden for testing.
  277. FetchRemoteServerListRetryPeriodMilliseconds *int
  278. // ObfuscatedServerListRootURL is a URL which specifies the root location
  279. // from which to fetch obfuscated server list files. This value is
  280. // supplied by and depends on the Psiphon Network, and is typically
  281. // embedded in the client binary.
  282. //
  283. // Deprecated: Use ObfuscatedServerListRootURLs. When
  284. // ObfuscatedServerListRootURLs is not nil, this parameter is ignored.
  285. ObfuscatedServerListRootURL string
  286. // ObfuscatedServerListRootURLs is a list of URLs which specify root
  287. // locations from which to fetch obfuscated server list files. This value
  288. // is supplied by and depends on the Psiphon Network, and is typically
  289. // embedded in the client binary. All URLs must point to the same entity
  290. // with the same ETag. At least one DownloadURL must have
  291. // OnlyAfterAttempts = 0.
  292. ObfuscatedServerListRootURLs parameters.DownloadURLs
  293. // ObfuscatedServerListDownloadDirectory specifies a target directory for
  294. // storing the obfuscated remote server list downloads. Data is stored in
  295. // co-located files (<OSL filename>.part*) to allow for resumable
  296. // downloading.
  297. ObfuscatedServerListDownloadDirectory string
  298. // SplitTunnelRoutesURLFormat is a URL which specifies the location of a
  299. // routes file to use for split tunnel mode. The URL must include a
  300. // placeholder for the client region to be supplied. Split tunnel mode
  301. // uses the routes file to classify port forward destinations as foreign
  302. // or domestic and does not tunnel domestic destinations. Split tunnel
  303. // mode is on when all the SplitTunnel parameters are supplied. This value
  304. // is supplied by and depends on the Psiphon Network, and is typically
  305. // embedded in the client binary.
  306. SplitTunnelRoutesURLFormat string
  307. // SplitTunnelRoutesSignaturePublicKey specifies a public key that's used
  308. // to authenticate the split tunnel routes payload. This value is supplied
  309. // by and depends on the Psiphon Network, and is typically embedded in the
  310. // client binary.
  311. SplitTunnelRoutesSignaturePublicKey string
  312. // SplitTunnelDNSServer specifies a DNS server to use when resolving port
  313. // forward target domain names to IP addresses for classification. The DNS
  314. // server must support TCP requests.
  315. SplitTunnelDNSServer string
  316. // UpgradeDownloadUrl specifies a URL from which to download a host client
  317. // upgrade file, when one is available. The core tunnel controller
  318. // provides a resumable download facility which downloads this resource
  319. // and emits a notice when complete. This value is supplied by and depends
  320. // on the Psiphon Network, and is typically embedded in the client binary.
  321. //
  322. // Deprecated: Use UpgradeDownloadURLs. When UpgradeDownloadURLs is not
  323. // nil, this parameter is ignored.
  324. UpgradeDownloadUrl string
  325. // UpgradeDownloadURLs is list of URLs which specify locations from which
  326. // to download a host client upgrade file, when one is available. The core
  327. // tunnel controller provides a resumable download facility which
  328. // downloads this resource and emits a notice when complete. This value is
  329. // supplied by and depends on the Psiphon Network, and is typically
  330. // embedded in the client binary. All URLs must point to the same entity
  331. // with the same ETag. At least one DownloadURL must have
  332. // OnlyAfterAttempts = 0.
  333. UpgradeDownloadURLs parameters.DownloadURLs
  334. // UpgradeDownloadClientVersionHeader specifies the HTTP header name for
  335. // the entity at UpgradeDownloadURLs which specifies the client version
  336. // (an integer value). A HEAD request may be made to check the version
  337. // number available at UpgradeDownloadURLs.
  338. // UpgradeDownloadClientVersionHeader is required when UpgradeDownloadURLs
  339. // is specified.
  340. UpgradeDownloadClientVersionHeader string
  341. // UpgradeDownloadFilename is the local target filename for an upgrade
  342. // download. This parameter is required when UpgradeDownloadURLs (or
  343. // UpgradeDownloadUrl) is specified. Data is stored in co-located files
  344. // (UpgradeDownloadFilename.part*) to allow for resumable downloading.
  345. UpgradeDownloadFilename string
  346. // FetchUpgradeRetryPeriodMilliseconds specifies the delay before resuming
  347. // a client upgrade download after a failure. If omitted, a default value
  348. // is used. This value is typical overridden for testing.
  349. FetchUpgradeRetryPeriodMilliseconds *int
  350. // EmitBytesTransferred indicates whether to emit periodic notices showing
  351. // bytes sent and received.
  352. EmitBytesTransferred bool
  353. // TrustedCACertificatesFilename specifies a file containing trusted CA
  354. // certs. When set, this toggles use of the trusted CA certs, specified in
  355. // TrustedCACertificatesFilename, for tunneled TLS connections that expect
  356. // server certificates signed with public certificate authorities
  357. // (currently, only upgrade downloads). This option is used with stock Go
  358. // TLS in cases where Go may fail to obtain a list of root CAs from the
  359. // operating system.
  360. TrustedCACertificatesFilename string
  361. // DisablePeriodicSshKeepAlive indicates whether to send an SSH keepalive
  362. // every 1-2 minutes, when the tunnel is idle. If the SSH keepalive times
  363. // out, the tunnel is considered to have failed.
  364. DisablePeriodicSshKeepAlive bool
  365. // DeviceRegion is the optional, reported region the host device is
  366. // running in. This input value should be a ISO 3166-1 alpha-2 country
  367. // code. The device region is reported to the server in the connected
  368. // request and recorded for Psiphon stats.
  369. //
  370. // When provided, this value may be used, pre-connection, to select
  371. // performance or circumvention optimization strategies for the given
  372. // region.
  373. DeviceRegion string
  374. // EmitDiagnosticNotices indicates whether to output notices containing
  375. // detailed information about the Psiphon session. As these notices may
  376. // contain sensitive network information, they should not be insecurely
  377. // distributed or displayed to users. Default is off.
  378. EmitDiagnosticNotices bool
  379. // RateLimits specify throttling configuration for the tunnel.
  380. RateLimits common.RateLimits
  381. // EmitSLOKs indicates whether to emit notices for each seeded SLOK. As
  382. // this could reveal user browsing activity, it's intended for debugging
  383. // and testing only.
  384. EmitSLOKs bool
  385. // PacketTunnelTunDeviceFileDescriptor specifies a tun device file
  386. // descriptor to use for running a packet tunnel. When this value is > 0,
  387. // a packet tunnel is established through the server and packets are
  388. // relayed via the tun device file descriptor. The file descriptor is
  389. // duped in NewController. When PacketTunnelTunDeviceFileDescriptor is
  390. // set, TunnelPoolSize must be 1.
  391. PacketTunnelTunFileDescriptor int
  392. // SessionID specifies a client session ID to use in the Psiphon API. The
  393. // session ID should be a randomly generated value that is used only for a
  394. // single session, which is defined as the period between a user starting
  395. // a Psiphon client and stopping the client.
  396. //
  397. // A session ID must be 32 hex digits (lower case). When blank, a random
  398. // session ID is automatically generated. Supply a session ID when a
  399. // single client session will cross multiple Controller instances.
  400. SessionID string
  401. // Authorizations is a list of encoded, signed access control
  402. // authorizations that the client has obtained and will present to the
  403. // server.
  404. Authorizations []string
  405. // UseFragmentor and associated Fragmentor fields are for testing
  406. // purposes.
  407. UseFragmentor string
  408. FragmentorMinTotalBytes *int
  409. FragmentorMaxTotalBytes *int
  410. FragmentorMinWriteBytes *int
  411. FragmentorMaxWriteBytes *int
  412. FragmentorMinDelayMicroseconds *int
  413. FragmentorMaxDelayMicroseconds *int
  414. // ObfuscatedSSHAlgorithms and associated ObfuscatedSSH fields are for
  415. // testing purposes. If specified, ObfuscatedSSHAlgorithms must have 4 SSH
  416. // KEX elements in order: the kex algorithm, cipher, MAC, and server host
  417. // key algorithm.
  418. ObfuscatedSSHAlgorithms []string
  419. ObfuscatedSSHMinPadding *int
  420. ObfuscatedSSHMaxPadding *int
  421. // clientParameters is the active ClientParameters with defaults, config
  422. // values, and, optionally, tactics applied.
  423. //
  424. // New tactics must be applied by calling Config.SetClientParameters;
  425. // calling clientParameters.Set directly will fail to add config values.
  426. clientParameters *parameters.ClientParameters
  427. dynamicConfigMutex sync.Mutex
  428. sponsorID string
  429. authorizations []string
  430. deviceBinder DeviceBinder
  431. networkIDGetter NetworkIDGetter
  432. committed bool
  433. }
  434. // LoadConfig parses a JSON format Psiphon config JSON string and returns a
  435. // Config struct populated with config values.
  436. //
  437. // The Config struct may then be programmatically populated with additional
  438. // values, including callbacks such as DeviceBinder.
  439. //
  440. // Before using the Config, Commit must be called, which will perform further
  441. // validation and initialize internal data structures.
  442. func LoadConfig(configJson []byte) (*Config, error) {
  443. var config Config
  444. err := json.Unmarshal(configJson, &config)
  445. if err != nil {
  446. return nil, common.ContextError(err)
  447. }
  448. return &config, nil
  449. }
  450. // IsCommitted checks if Commit was called.
  451. func (config *Config) IsCommitted() bool {
  452. return config.committed
  453. }
  454. // Commit validates Config fields finalizes initialization.
  455. //
  456. // Config fields should not be set after calling Config, as any changes may
  457. // not be reflected in internal data structures.
  458. func (config *Config) Commit() error {
  459. // Do SetEmitDiagnosticNotices first, to ensure config file errors are emitted.
  460. if config.EmitDiagnosticNotices {
  461. SetEmitDiagnosticNotices(true)
  462. }
  463. // Promote legacy fields.
  464. if config.CustomHeaders == nil {
  465. config.CustomHeaders = config.UpstreamProxyCustomHeaders
  466. config.UpstreamProxyCustomHeaders = nil
  467. }
  468. if config.RemoteServerListUrl != "" && config.RemoteServerListURLs == nil {
  469. config.RemoteServerListURLs = promoteLegacyDownloadURL(config.RemoteServerListUrl)
  470. }
  471. if config.ObfuscatedServerListRootURL != "" && config.ObfuscatedServerListRootURLs == nil {
  472. config.ObfuscatedServerListRootURLs = promoteLegacyDownloadURL(config.ObfuscatedServerListRootURL)
  473. }
  474. if config.UpgradeDownloadUrl != "" && config.UpgradeDownloadURLs == nil {
  475. config.UpgradeDownloadURLs = promoteLegacyDownloadURL(config.UpgradeDownloadUrl)
  476. }
  477. // Supply default values.
  478. if config.DataStoreDirectory == "" {
  479. wd, err := os.Getwd()
  480. if err != nil {
  481. return common.ContextError(err)
  482. }
  483. config.DataStoreDirectory = wd
  484. }
  485. if config.ClientVersion == "" {
  486. config.ClientVersion = "0"
  487. }
  488. if config.TunnelPoolSize == 0 {
  489. config.TunnelPoolSize = TUNNEL_POOL_SIZE
  490. }
  491. // Validate config fields.
  492. if config.PropagationChannelId == "" {
  493. return common.ContextError(
  494. errors.New("propagation channel ID is missing from the configuration file"))
  495. }
  496. if config.SponsorId == "" {
  497. return common.ContextError(
  498. errors.New("sponsor ID is missing from the configuration file"))
  499. }
  500. _, err := strconv.Atoi(config.ClientVersion)
  501. if err != nil {
  502. return common.ContextError(
  503. fmt.Errorf("invalid client version: %s", err))
  504. }
  505. if !common.Contains(
  506. []string{"", protocol.PSIPHON_SSH_API_PROTOCOL, protocol.PSIPHON_WEB_API_PROTOCOL},
  507. config.TargetApiProtocol) {
  508. return common.ContextError(
  509. errors.New("invalid TargetApiProtocol"))
  510. }
  511. if !config.DisableRemoteServerListFetcher {
  512. if config.RemoteServerListURLs != nil {
  513. if config.RemoteServerListSignaturePublicKey == "" {
  514. return common.ContextError(errors.New("missing RemoteServerListSignaturePublicKey"))
  515. }
  516. if config.RemoteServerListDownloadFilename == "" {
  517. return common.ContextError(errors.New("missing RemoteServerListDownloadFilename"))
  518. }
  519. }
  520. if config.ObfuscatedServerListRootURLs != nil {
  521. if config.RemoteServerListSignaturePublicKey == "" {
  522. return common.ContextError(errors.New("missing RemoteServerListSignaturePublicKey"))
  523. }
  524. if config.ObfuscatedServerListDownloadDirectory == "" {
  525. return common.ContextError(errors.New("missing ObfuscatedServerListDownloadDirectory"))
  526. }
  527. }
  528. }
  529. if config.SplitTunnelRoutesURLFormat != "" {
  530. if config.SplitTunnelRoutesSignaturePublicKey == "" {
  531. return common.ContextError(errors.New("missing SplitTunnelRoutesSignaturePublicKey"))
  532. }
  533. if config.SplitTunnelDNSServer == "" {
  534. return common.ContextError(errors.New("missing SplitTunnelDNSServer"))
  535. }
  536. }
  537. if config.UpgradeDownloadURLs != nil {
  538. if config.UpgradeDownloadClientVersionHeader == "" {
  539. return common.ContextError(errors.New("missing UpgradeDownloadClientVersionHeader"))
  540. }
  541. if config.UpgradeDownloadFilename == "" {
  542. return common.ContextError(errors.New("missing UpgradeDownloadFilename"))
  543. }
  544. }
  545. // This constraint is expected by logic in Controller.runTunnels().
  546. if config.PacketTunnelTunFileDescriptor > 0 && config.TunnelPoolSize != 1 {
  547. return common.ContextError(errors.New("packet tunnel mode requires TunnelPoolSize to be 1"))
  548. }
  549. // SessionID must be PSIPHON_API_CLIENT_SESSION_ID_LENGTH lowercase hex-encoded bytes.
  550. if config.SessionID == "" {
  551. sessionID, err := MakeSessionId()
  552. if err != nil {
  553. return common.ContextError(err)
  554. }
  555. config.SessionID = sessionID
  556. }
  557. if len(config.SessionID) != 2*protocol.PSIPHON_API_CLIENT_SESSION_ID_LENGTH ||
  558. -1 != strings.IndexFunc(config.SessionID, func(c rune) bool {
  559. return !unicode.Is(unicode.ASCII_Hex_Digit, c) || unicode.IsUpper(c)
  560. }) {
  561. return common.ContextError(errors.New("invalid SessionID"))
  562. }
  563. config.clientParameters, err = parameters.NewClientParameters(
  564. func(err error) {
  565. NoticeAlert("ClientParameters getValue failed: %s", err)
  566. })
  567. if err != nil {
  568. return common.ContextError(err)
  569. }
  570. if config.ObfuscatedSSHAlgorithms != nil &&
  571. len(config.ObfuscatedSSHAlgorithms) != 4 {
  572. // TODO: validate each algorithm?
  573. return common.ContextError(errors.New("invalid ObfuscatedSSHAlgorithms"))
  574. }
  575. // clientParameters.Set will validate the config fields applied to parameters.
  576. err = config.SetClientParameters("", false, nil)
  577. if err != nil {
  578. return common.ContextError(err)
  579. }
  580. // Set defaults for dynamic config fields.
  581. config.SetDynamicConfig(config.SponsorId, config.Authorizations)
  582. // Initialize config.deviceBinder and config.config.networkIDGetter. These
  583. // wrap config.DeviceBinder and config.NetworkIDGetter/NetworkID with
  584. // loggers.
  585. //
  586. // New variables are set to avoid mutating input config fields.
  587. // Internally, code must use config.deviceBinder and
  588. // config.networkIDGetter and not the input/exported fields.
  589. if config.DeviceBinder != nil {
  590. config.deviceBinder = &loggingDeviceBinder{config.DeviceBinder}
  591. }
  592. networkIDGetter := config.NetworkIDGetter
  593. if networkIDGetter == nil && config.NetworkID != "" {
  594. // Enable tactics when a NetworkID is specified
  595. //
  596. // Limitation: unlike NetworkIDGetter, which calls back to platform APIs
  597. // this method of network identification is not dynamic and will not reflect
  598. // network changes that occur while running.
  599. networkIDGetter = newStaticNetworkGetter(config.NetworkID)
  600. }
  601. if networkIDGetter != nil {
  602. config.networkIDGetter = &loggingNetworkIDGetter{networkIDGetter}
  603. }
  604. config.committed = true
  605. return nil
  606. }
  607. // GetClientParameters returns a snapshot of the current client parameters.
  608. func (config *Config) GetClientParameters() *parameters.ClientParametersSnapshot {
  609. return config.clientParameters.Get()
  610. }
  611. // SetClientParameters resets Config.clientParameters to the default values,
  612. // applies any config file values, and then applies the input parameters (from
  613. // tactics, etc.)
  614. //
  615. // Set skipOnError to false when initially applying only config values, as
  616. // this will validate the values and should fail. Set skipOnError to true when
  617. // applying tactics to ignore invalid or unknown parameter values from tactics.
  618. //
  619. // In the case of applying tactics, do not call Config.clientParameters.Set
  620. // directly as this will not first apply config values.
  621. //
  622. // If there is an error, the existing Config.clientParameters are left
  623. // entirely unmodified.
  624. func (config *Config) SetClientParameters(tag string, skipOnError bool, applyParameters map[string]interface{}) error {
  625. setParameters := []map[string]interface{}{config.makeConfigParameters()}
  626. if applyParameters != nil {
  627. setParameters = append(setParameters, applyParameters)
  628. }
  629. counts, err := config.clientParameters.Set(tag, skipOnError, setParameters...)
  630. if err != nil {
  631. return common.ContextError(err)
  632. }
  633. NoticeInfo("applied %v parameters with tag '%s'", counts, tag)
  634. // Emit certain individual parameter values for quick reference in diagnostics.
  635. networkLatencyMultiplier := config.clientParameters.Get().Float(parameters.NetworkLatencyMultiplier)
  636. if networkLatencyMultiplier != 0.0 {
  637. NoticeInfo(
  638. "NetworkLatencyMultiplier: %f",
  639. config.clientParameters.Get().Float(parameters.NetworkLatencyMultiplier))
  640. }
  641. return nil
  642. }
  643. // SetDynamicConfig sets the current client sponsor ID and authorizations.
  644. // Invalid values for sponsor ID are ignored. The caller must not modify the
  645. // input authorizations slice.
  646. func (config *Config) SetDynamicConfig(sponsorID string, authorizations []string) {
  647. config.dynamicConfigMutex.Lock()
  648. defer config.dynamicConfigMutex.Unlock()
  649. if sponsorID != "" {
  650. config.sponsorID = sponsorID
  651. }
  652. config.authorizations = authorizations
  653. }
  654. // GetSponsorID returns the current client sponsor ID.
  655. func (config *Config) GetSponsorID() string {
  656. config.dynamicConfigMutex.Lock()
  657. defer config.dynamicConfigMutex.Unlock()
  658. return config.sponsorID
  659. }
  660. // GetAuthorizations returns the current client authorizations.
  661. // The caller must not modify the returned slice.
  662. func (config *Config) GetAuthorizations() []string {
  663. config.dynamicConfigMutex.Lock()
  664. defer config.dynamicConfigMutex.Unlock()
  665. return config.authorizations
  666. }
  667. func (config *Config) UseUpstreamProxy() bool {
  668. return config.UpstreamProxyURL != ""
  669. }
  670. func (config *Config) makeConfigParameters() map[string]interface{} {
  671. // Build set of config values to apply to parameters.
  672. //
  673. // Note: names of some config fields such as
  674. // StaggerConnectionWorkersMilliseconds and LimitMeekBufferSizes have
  675. // changed in the parameters. The existing config fields are retained for
  676. // backwards compatibility.
  677. applyParameters := make(map[string]interface{})
  678. if config.NetworkLatencyMultiplier > 0.0 {
  679. applyParameters[parameters.NetworkLatencyMultiplier] = config.NetworkLatencyMultiplier
  680. }
  681. if len(config.LimitTunnelProtocols) > 0 {
  682. applyParameters[parameters.LimitTunnelProtocols] = protocol.TunnelProtocols(config.LimitTunnelProtocols)
  683. } else if config.TunnelProtocol != "" {
  684. applyParameters[parameters.LimitTunnelProtocols] = protocol.TunnelProtocols{config.TunnelProtocol}
  685. }
  686. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  687. applyParameters[parameters.InitialLimitTunnelProtocols] = protocol.TunnelProtocols(config.InitialLimitTunnelProtocols)
  688. applyParameters[parameters.InitialLimitTunnelProtocolsCandidateCount] = config.InitialLimitTunnelProtocolsCandidateCount
  689. }
  690. if len(config.LimitTLSProfiles) > 0 {
  691. applyParameters[parameters.LimitTLSProfiles] = protocol.TunnelProtocols(config.LimitTLSProfiles)
  692. }
  693. if len(config.LimitQUICVersions) > 0 {
  694. applyParameters[parameters.LimitQUICVersions] = protocol.QUICVersions(config.LimitQUICVersions)
  695. }
  696. if config.EstablishTunnelTimeoutSeconds != nil {
  697. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  698. }
  699. if config.EstablishTunnelPausePeriodSeconds != nil {
  700. applyParameters[parameters.EstablishTunnelPausePeriod] = fmt.Sprintf("%ds", *config.EstablishTunnelPausePeriodSeconds)
  701. }
  702. if config.ConnectionWorkerPoolSize != 0 {
  703. applyParameters[parameters.ConnectionWorkerPoolSize] = config.ConnectionWorkerPoolSize
  704. }
  705. if config.StaggerConnectionWorkersMilliseconds > 0 {
  706. applyParameters[parameters.StaggerConnectionWorkersPeriod] = fmt.Sprintf("%dms", config.StaggerConnectionWorkersMilliseconds)
  707. }
  708. if config.LimitIntensiveConnectionWorkers > 0 {
  709. applyParameters[parameters.LimitIntensiveConnectionWorkers] = config.LimitIntensiveConnectionWorkers
  710. }
  711. applyParameters[parameters.MeekLimitBufferSizes] = config.LimitMeekBufferSizes
  712. applyParameters[parameters.IgnoreHandshakeStatsRegexps] = config.IgnoreHandshakeStatsRegexps
  713. if config.EstablishTunnelTimeoutSeconds != nil {
  714. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  715. }
  716. if config.FetchRemoteServerListRetryPeriodMilliseconds != nil {
  717. applyParameters[parameters.FetchRemoteServerListRetryPeriod] = fmt.Sprintf("%dms", *config.FetchRemoteServerListRetryPeriodMilliseconds)
  718. }
  719. if config.FetchUpgradeRetryPeriodMilliseconds != nil {
  720. applyParameters[parameters.FetchUpgradeRetryPeriod] = fmt.Sprintf("%dms", *config.FetchUpgradeRetryPeriodMilliseconds)
  721. }
  722. switch config.TransformHostNames {
  723. case "always":
  724. applyParameters[parameters.TransformHostNameProbability] = 1.0
  725. case "never":
  726. applyParameters[parameters.TransformHostNameProbability] = 0.0
  727. }
  728. if !config.DisableRemoteServerListFetcher {
  729. if config.RemoteServerListURLs != nil {
  730. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  731. applyParameters[parameters.RemoteServerListURLs] = config.RemoteServerListURLs
  732. }
  733. if config.ObfuscatedServerListRootURLs != nil {
  734. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  735. applyParameters[parameters.ObfuscatedServerListRootURLs] = config.ObfuscatedServerListRootURLs
  736. }
  737. }
  738. applyParameters[parameters.SplitTunnelRoutesURLFormat] = config.SplitTunnelRoutesURLFormat
  739. applyParameters[parameters.SplitTunnelRoutesSignaturePublicKey] = config.SplitTunnelRoutesSignaturePublicKey
  740. applyParameters[parameters.SplitTunnelDNSServer] = config.SplitTunnelDNSServer
  741. if config.UpgradeDownloadURLs != nil {
  742. applyParameters[parameters.UpgradeDownloadClientVersionHeader] = config.UpgradeDownloadClientVersionHeader
  743. applyParameters[parameters.UpgradeDownloadURLs] = config.UpgradeDownloadURLs
  744. }
  745. applyParameters[parameters.TunnelRateLimits] = config.RateLimits
  746. switch config.UseFragmentor {
  747. case "always":
  748. applyParameters[parameters.FragmentorProbability] = 1.0
  749. case "never":
  750. applyParameters[parameters.FragmentorProbability] = 0.0
  751. }
  752. if config.FragmentorMinTotalBytes != nil {
  753. applyParameters[parameters.FragmentorMinTotalBytes] = *config.FragmentorMinTotalBytes
  754. }
  755. if config.FragmentorMaxTotalBytes != nil {
  756. applyParameters[parameters.FragmentorMaxTotalBytes] = *config.FragmentorMaxTotalBytes
  757. }
  758. if config.FragmentorMinWriteBytes != nil {
  759. applyParameters[parameters.FragmentorMinWriteBytes] = *config.FragmentorMinWriteBytes
  760. }
  761. if config.FragmentorMaxWriteBytes != nil {
  762. applyParameters[parameters.FragmentorMaxWriteBytes] = *config.FragmentorMaxWriteBytes
  763. }
  764. if config.FragmentorMinDelayMicroseconds != nil {
  765. applyParameters[parameters.FragmentorMinDelay] = fmt.Sprintf("%dus", *config.FragmentorMinDelayMicroseconds)
  766. }
  767. if config.FragmentorMaxDelayMicroseconds != nil {
  768. applyParameters[parameters.FragmentorMaxDelay] = fmt.Sprintf("%dus", *config.FragmentorMaxDelayMicroseconds)
  769. }
  770. if config.ObfuscatedSSHMinPadding != nil {
  771. applyParameters[parameters.ObfuscatedSSHMinPadding] = *config.ObfuscatedSSHMinPadding
  772. }
  773. if config.ObfuscatedSSHMaxPadding != nil {
  774. applyParameters[parameters.ObfuscatedSSHMaxPadding] = *config.ObfuscatedSSHMaxPadding
  775. }
  776. return applyParameters
  777. }
  778. func promoteLegacyDownloadURL(URL string) parameters.DownloadURLs {
  779. downloadURLs := make(parameters.DownloadURLs, 1)
  780. downloadURLs[0] = &parameters.DownloadURL{
  781. URL: base64.StdEncoding.EncodeToString([]byte(URL)),
  782. SkipVerify: false,
  783. OnlyAfterAttempts: 0,
  784. }
  785. return downloadURLs
  786. }
  787. type loggingDeviceBinder struct {
  788. d DeviceBinder
  789. }
  790. func newLoggingDeviceBinder(d DeviceBinder) *loggingDeviceBinder {
  791. return &loggingDeviceBinder{d: d}
  792. }
  793. func (d *loggingDeviceBinder) BindToDevice(fileDescriptor int) (string, error) {
  794. deviceInfo, err := d.d.BindToDevice(fileDescriptor)
  795. if err == nil && deviceInfo != "" {
  796. NoticeBindToDevice(deviceInfo)
  797. }
  798. return deviceInfo, err
  799. }
  800. type staticNetworkGetter struct {
  801. networkID string
  802. }
  803. func newStaticNetworkGetter(networkID string) *staticNetworkGetter {
  804. return &staticNetworkGetter{networkID: networkID}
  805. }
  806. func (n *staticNetworkGetter) GetNetworkID() string {
  807. return n.networkID
  808. }
  809. type loggingNetworkIDGetter struct {
  810. n NetworkIDGetter
  811. }
  812. func newLoggingNetworkIDGetter(n NetworkIDGetter) *loggingNetworkIDGetter {
  813. return &loggingNetworkIDGetter{n: n}
  814. }
  815. func (n *loggingNetworkIDGetter) GetNetworkID() string {
  816. networkID := n.n.GetNetworkID()
  817. // All PII must appear after the initial "-"
  818. // See: https://godoc.org/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon#NetworkIDGetter
  819. logNetworkID := networkID
  820. index := strings.Index(logNetworkID, "-")
  821. if index != -1 {
  822. logNetworkID = logNetworkID[:index]
  823. }
  824. if len(logNetworkID)+1 < len(networkID) {
  825. // Indicate when additional network info was present after the first "-".
  826. logNetworkID += "+<network info>"
  827. }
  828. NoticeNetworkID(logNetworkID)
  829. return networkID
  830. }