config.go 36 KB

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