config.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/json"
  22. "errors"
  23. "fmt"
  24. "net/http"
  25. "os"
  26. "strconv"
  27. "time"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  30. )
  31. // TODO: allow all params to be configured
  32. const (
  33. LEGACY_DATA_STORE_FILENAME = "psiphon.db"
  34. DATA_STORE_FILENAME = "psiphon.boltdb"
  35. CONNECTION_WORKER_POOL_SIZE = 10
  36. TUNNEL_POOL_SIZE = 1
  37. TUNNEL_CONNECT_TIMEOUT_SECONDS = 20
  38. TUNNEL_OPERATE_SHUTDOWN_TIMEOUT = 1 * time.Second
  39. TUNNEL_PORT_FORWARD_DIAL_TIMEOUT_SECONDS = 10
  40. TUNNEL_SSH_KEEP_ALIVE_PAYLOAD_MAX_BYTES = 256
  41. TUNNEL_SSH_KEEP_ALIVE_PERIOD_MIN = 60 * time.Second
  42. TUNNEL_SSH_KEEP_ALIVE_PERIOD_MAX = 120 * time.Second
  43. TUNNEL_SSH_KEEP_ALIVE_PERIODIC_TIMEOUT_SECONDS = 30
  44. TUNNEL_SSH_KEEP_ALIVE_PERIODIC_INACTIVE_PERIOD = 10 * time.Second
  45. TUNNEL_SSH_KEEP_ALIVE_PROBE_TIMEOUT_SECONDS = 5
  46. TUNNEL_SSH_KEEP_ALIVE_PROBE_INACTIVE_PERIOD = 10 * time.Second
  47. ESTABLISH_TUNNEL_TIMEOUT_SECONDS = 300
  48. ESTABLISH_TUNNEL_WORK_TIME = 60 * time.Second
  49. ESTABLISH_TUNNEL_PAUSE_PERIOD_SECONDS = 5
  50. ESTABLISH_TUNNEL_SERVER_AFFINITY_GRACE_PERIOD = 1 * time.Second
  51. HTTP_PROXY_ORIGIN_SERVER_TIMEOUT_SECONDS = 15
  52. HTTP_PROXY_MAX_IDLE_CONNECTIONS_PER_HOST = 50
  53. FETCH_REMOTE_SERVER_LIST_TIMEOUT_SECONDS = 30
  54. FETCH_REMOTE_SERVER_LIST_RETRY_PERIOD_SECONDS = 30
  55. FETCH_REMOTE_SERVER_LIST_STALE_PERIOD = 6 * time.Hour
  56. PSIPHON_API_SERVER_TIMEOUT_SECONDS = 20
  57. PSIPHON_API_SHUTDOWN_SERVER_TIMEOUT = 1 * time.Second
  58. PSIPHON_API_STATUS_REQUEST_PERIOD_MIN = 5 * time.Minute
  59. PSIPHON_API_STATUS_REQUEST_PERIOD_MAX = 10 * time.Minute
  60. PSIPHON_API_STATUS_REQUEST_SHORT_PERIOD_MIN = 5 * time.Second
  61. PSIPHON_API_STATUS_REQUEST_SHORT_PERIOD_MAX = 10 * time.Second
  62. PSIPHON_API_STATUS_REQUEST_PADDING_MAX_BYTES = 256
  63. PSIPHON_API_CONNECTED_REQUEST_PERIOD = 24 * time.Hour
  64. PSIPHON_API_CONNECTED_REQUEST_RETRY_PERIOD = 5 * time.Second
  65. PSIPHON_API_PERSISTENT_STATS_MAX_COUNT = 100
  66. PSIPHON_API_CLIENT_VERIFICATION_REQUEST_RETRY_PERIOD = 5 * time.Second
  67. PSIPHON_API_CLIENT_VERIFICATION_REQUEST_MAX_RETRIES = 10
  68. FETCH_ROUTES_TIMEOUT_SECONDS = 60
  69. DOWNLOAD_UPGRADE_TIMEOUT = 15 * time.Minute
  70. DOWNLOAD_UPGRADE_RETRY_PERIOD_SECONDS = 30
  71. DOWNLOAD_UPGRADE_STALE_PERIOD = 6 * time.Hour
  72. IMPAIRED_PROTOCOL_CLASSIFICATION_DURATION = 2 * time.Minute
  73. IMPAIRED_PROTOCOL_CLASSIFICATION_THRESHOLD = 3
  74. TOTAL_BYTES_TRANSFERRED_NOTICE_PERIOD = 5 * time.Minute
  75. TRANSFORM_HOST_NAMES_ALWAYS = "always"
  76. TRANSFORM_HOST_NAMES_NEVER = "never"
  77. )
  78. // To distinguish omitted timeout params from explicit 0 value timeout
  79. // params, these params are int pointers. nil means no param was supplied
  80. // so use the default; a non-nil pointer to 0 means no timeout.
  81. // Config is the Psiphon configuration specified by the application. This
  82. // configuration controls the behavior of the core tunnel functionality.
  83. type Config struct {
  84. // LogFilename specifies a file to receive event notices (JSON format)
  85. // By default, notices are emitted to stdout.
  86. LogFilename string
  87. // DataStoreDirectory is the directory in which to store the persistent
  88. // database, which contains information such as server entries.
  89. // By default, current working directory.
  90. //
  91. // Warning: If the datastore file, DataStoreDirectory/DATA_STORE_FILENAME,
  92. // exists but fails to open for any reason (checksum error, unexpected file
  93. // format, etc.) it will be deleted in order to pave a new datastore and
  94. // continue running.
  95. DataStoreDirectory string
  96. // PropagationChannelId is a string identifier which indicates how the
  97. // Psiphon client was distributed. This parameter is required.
  98. // This value is supplied by and depends on the Psiphon Network, and is
  99. // typically embedded in the client binary.
  100. PropagationChannelId string
  101. // PropagationChannelId is a string identifier which indicates who
  102. // is sponsoring this Psiphon client. One purpose of this value is to
  103. // determine the home pages for display. This parameter is required.
  104. // This value is supplied by and depends on the Psiphon Network, and is
  105. // typically embedded in the client binary.
  106. SponsorId string
  107. // RemoteServerListUrl is a URL which specifies a location to fetch
  108. // out-of-band server entries. This facility is used when a tunnel cannot
  109. // be established to known servers.
  110. // This value is supplied by and depends on the Psiphon Network, and is
  111. // typically embedded in the client binary.
  112. RemoteServerListUrl string
  113. // RemoteServerListDownloadFilename specifies a target filename for
  114. // storing the remote server list download. Data is stored in co-located
  115. // files (RemoteServerListDownloadFilename.part*) to allow for resumable
  116. // downloading.
  117. RemoteServerListDownloadFilename string
  118. // RemoteServerListSignaturePublicKey specifies a public key that's
  119. // used to authenticate the remote server list payload.
  120. // This value is supplied by and depends on the Psiphon Network, and is
  121. // typically embedded in the client binary.
  122. RemoteServerListSignaturePublicKey string
  123. // ObfuscatedServerListRootURL is a URL which specifies the root location
  124. // from which to fetch obfuscated server list files.
  125. // This value is supplied by and depends on the Psiphon Network, and is
  126. // typically embedded in the client binary.
  127. ObfuscatedServerListRootURL string
  128. // ObfuscatedServerListDownloadDirectory specifies a target directory for
  129. // storing the obfuscated remote server list downloads. Data is stored in
  130. // co-located files (<OSL filename>.part*) to allow for resumable
  131. // downloading.
  132. ObfuscatedServerListDownloadDirectory string
  133. // ClientVersion is the client version number that the client reports
  134. // to the server. The version number refers to the host client application,
  135. // not the core tunnel library. One purpose of this value is to enable
  136. // automatic updates.
  137. // This value is supplied by and depends on the Psiphon Network, and is
  138. // typically embedded in the client binary.
  139. // Note that sending a ClientPlatform string which includes "windows"
  140. // (case insensitive) and a ClientVersion of <= 44 will cause an
  141. // error in processing the response to DoConnectedRequest calls.
  142. ClientVersion string
  143. // ClientPlatform is the client platform ("Windows", "Android", etc.) that
  144. // the client reports to the server.
  145. ClientPlatform string
  146. // TunnelWholeDevice is a flag that is passed through to the handshake
  147. // request for stats purposes. Set to 1 when the host application is tunneling
  148. // the whole device, 0 otherwise.
  149. TunnelWholeDevice int
  150. // EgressRegion is a ISO 3166-1 alpha-2 country code which indicates which
  151. // country to egress from. For the default, "", the best performing server
  152. // in any country is selected.
  153. EgressRegion string
  154. // TunnelProtocol indicates which protocol to use. Valid values include:
  155. // "SSH", "OSSH", "UNFRONTED-MEEK-OSSH", "UNFRONTED-MEEK-HTTPS-OSSH",
  156. // "FRONTED-MEEK-OSSH", "FRONTED-MEEK-HTTP-OSSH". For the default, "",
  157. // the best performing protocol is used.
  158. TunnelProtocol string
  159. // EstablishTunnelTimeoutSeconds specifies a time limit after which to halt
  160. // the core tunnel controller if no tunnel has been established. The default
  161. // is ESTABLISH_TUNNEL_TIMEOUT_SECONDS.
  162. EstablishTunnelTimeoutSeconds *int
  163. // ListenInterface specifies which interface to listen on. If no interface
  164. // is provided then listen on 127.0.0.1.
  165. // If 'any' is provided then use 0.0.0.0.
  166. // If there are multiple IP addresses on an interface use the first IPv4 address.
  167. ListenInterface string
  168. // LocalSocksProxyPort specifies a port number for the local SOCKS proxy
  169. // running at 127.0.0.1. For the default value, 0, the system selects a free
  170. // port (a notice reporting the selected port is emitted).
  171. LocalSocksProxyPort int
  172. // LocalHttpProxyPort specifies a port number for the local HTTP proxy
  173. // running at 127.0.0.1. For the default value, 0, the system selects a free
  174. // port (a notice reporting the selected port is emitted).
  175. LocalHttpProxyPort int
  176. // ConnectionWorkerPoolSize specifies how many connection attempts to attempt
  177. // in parallel. The default, 0, uses CONNECTION_WORKER_POOL_SIZE which is
  178. // recommended.
  179. ConnectionWorkerPoolSize int
  180. // TunnelPoolSize specifies how many tunnels to run in parallel. Port forwards
  181. // are multiplexed over multiple tunnels. The default, 0, uses TUNNEL_POOL_SIZE
  182. // which is recommended.
  183. TunnelPoolSize int
  184. // UpstreamProxyUrl is a URL specifying an upstream proxy to use for all
  185. // outbound connections. The URL should include proxy type and authentication
  186. // information, as required. See example URLs here:
  187. // https://github.com/Psiphon-Labs/psiphon-tunnel-core/tree/master/psiphon/upstreamproxy
  188. UpstreamProxyUrl string
  189. // UpstreamProxyCustomHeaders is a set of additional arbitrary HTTP headers that are
  190. // added to all requests made through the upstream proxy specified by UpstreamProxyUrl
  191. // NOTE: Only HTTP(s) proxies use this if specified
  192. UpstreamProxyCustomHeaders http.Header
  193. // NetworkConnectivityChecker is an interface that enables the core tunnel to call
  194. // into the host application to check for network connectivity. This parameter is
  195. // only applicable to library deployments.
  196. NetworkConnectivityChecker NetworkConnectivityChecker
  197. // DeviceBinder is an interface that enables the core tunnel to call
  198. // into the host application to bind sockets to specific devices. This is used
  199. // for VPN routing exclusion. This parameter is only applicable to library
  200. // deployments.
  201. DeviceBinder DeviceBinder
  202. // DnsServerGetter is an interface that enables the core tunnel to call
  203. // into the host application to discover the native network DNS server settings.
  204. // This parameter is only applicable to library deployments.
  205. DnsServerGetter DnsServerGetter
  206. // TransformHostNames specifies whether to use hostname transformation circumvention
  207. // strategies. Set to "always" to always transform, "never" to never transform, and
  208. // "", the default, for the default transformation strategy.
  209. TransformHostNames string
  210. // TargetServerEntry is an encoded server entry. When specified, this server entry
  211. // is used exclusively and all other known servers are ignored.
  212. TargetServerEntry string
  213. // DisableApi disables Psiphon server API calls including handshake, connected,
  214. // status, etc. This is used for special case temporary tunnels (Windows VPN mode).
  215. DisableApi bool
  216. // TargetApiProtocol specifies whether to force use of "ssh" or "web" API protocol.
  217. // When blank, the default, the optimal API protocol is used. Note that this
  218. // capability check is not applied before the "CandidateServers" count is emitted.
  219. // This parameter is intended for testing and debugging only.
  220. TargetApiProtocol string
  221. // DisableRemoteServerListFetcher disables fetching remote server lists. This is
  222. // used for special case temporary tunnels.
  223. DisableRemoteServerListFetcher bool
  224. // SplitTunnelRoutesUrlFormat is an URL which specifies the location of a routes
  225. // file to use for split tunnel mode. The URL must include a placeholder for the
  226. // client region to be supplied. Split tunnel mode uses the routes file to classify
  227. // port forward destinations as foreign or domestic and does not tunnel domestic
  228. // destinations. Split tunnel mode is on when all the SplitTunnel parameters are
  229. // supplied.
  230. // This value is supplied by and depends on the Psiphon Network, and is
  231. // typically embedded in the client binary.
  232. SplitTunnelRoutesUrlFormat string
  233. // SplitTunnelRoutesSignaturePublicKey specifies a public key that's
  234. // used to authenticate the split tunnel routes payload.
  235. // This value is supplied by and depends on the Psiphon Network, and is
  236. // typically embedded in the client binary.
  237. SplitTunnelRoutesSignaturePublicKey string
  238. // SplitTunnelDnsServer specifies a DNS server to use when resolving port
  239. // forward target domain names to IP addresses for classification. The DNS
  240. // server must support TCP requests.
  241. SplitTunnelDnsServer string
  242. // UpgradeDownloadUrl specifies a URL from which to download a host client upgrade
  243. // file, when one is available. The core tunnel controller provides a resumable
  244. // download facility which downloads this resource and emits a notice when complete.
  245. // This value is supplied by and depends on the Psiphon Network, and is
  246. // typically embedded in the client binary.
  247. UpgradeDownloadUrl string
  248. // UpgradeDownloadClientVersionHeader specifies the HTTP header name for the
  249. // entity at UpgradeDownloadUrl which specifies the client version (an integer
  250. // value). A HEAD request may be made to check the version number available at
  251. // UpgradeDownloadUrl. UpgradeDownloadClientVersionHeader is required when
  252. // UpgradeDownloadUrl is specified.
  253. UpgradeDownloadClientVersionHeader string
  254. // UpgradeDownloadFilename is the local target filename for an upgrade download.
  255. // This parameter is required when UpgradeDownloadUrl is specified.
  256. // Data is stored in co-located files (UpgradeDownloadFilename.part*) to allow
  257. // for resumable downloading.
  258. UpgradeDownloadFilename string
  259. // EmitBytesTransferred indicates whether to emit periodic notices showing
  260. // bytes sent and received.
  261. EmitBytesTransferred bool
  262. // UseIndistinguishableTLS enables use of an alternative TLS stack with a less
  263. // distinct fingerprint (ClientHello content) than the stock Go TLS.
  264. // UseIndistinguishableTLS only applies to untunneled TLS connections. This
  265. // parameter is only supported on platforms built with OpenSSL.
  266. // Requires TrustedCACertificatesFilename to be set.
  267. UseIndistinguishableTLS bool
  268. // UseTrustedCACertificates toggles use of the trusted CA certs, specified
  269. // in TrustedCACertificatesFilename, for tunneled TLS connections that expect
  270. // server certificates signed with public certificate authorities (currently,
  271. // only upgrade downloads). This option is used with stock Go TLS in cases where
  272. // Go may fail to obtain a list of root CAs from the operating system.
  273. // Requires TrustedCACertificatesFilename to be set.
  274. UseTrustedCACertificatesForStockTLS bool
  275. // TrustedCACertificatesFilename specifies a file containing trusted CA certs.
  276. // The file contents should be compatible with OpenSSL's SSL_CTX_load_verify_locations.
  277. // When specified, this enables use of indistinguishable TLS for HTTPS requests
  278. // that require typical (system CA) server authentication.
  279. TrustedCACertificatesFilename string
  280. // DisablePeriodicSshKeepAlive indicates whether to send an SSH keepalive every
  281. // 1-2 minutes, when the tunnel is idle. If the SSH keepalive times out, the tunnel
  282. // is considered to have failed.
  283. DisablePeriodicSshKeepAlive bool
  284. // DeviceRegion is the optional, reported region the host device is running in.
  285. // This input value should be a ISO 3166-1 alpha-2 country code. The device region
  286. // is reported to the server in the connected request and recorded for Psiphon
  287. // stats.
  288. // When provided, this value may be used, pre-connection, to select performance
  289. // or circumvention optimization strategies for the given region.
  290. DeviceRegion string
  291. // EmitDiagnosticNotices indicates whether to output notices containing detailed
  292. // information about the Psiphon session. As these notices may contain sensitive
  293. // network information, they should not be insecurely distributed or displayed
  294. // to users. Default is off.
  295. EmitDiagnosticNotices bool
  296. // TunnelConnectTimeoutSeconds specifies a single tunnel connection sequence timeout.
  297. // Zero value means that connection process will not time out.
  298. // If omitted, the default value is TUNNEL_CONNECT_TIMEOUT_SECONDS.
  299. TunnelConnectTimeoutSeconds *int
  300. // TunnelPortForwardDialTimeoutSeconds specifies a dial timeout per SSH port forward.
  301. // Zero value means a port forward dial will not time out.
  302. // If omitted, the default value is TUNNEL_PORT_FORWARD_DIAL_TIMEOUT_SECONDS.
  303. TunnelPortForwardDialTimeoutSeconds *int
  304. // TunnelSshKeepAliveProbeTimeoutSeconds specifies a timeout value for "probe"
  305. // SSH keep-alive that is sent upon port forward failure.
  306. // Zero value means keep-alive request will not time out.
  307. // If omitted, the default value is TUNNEL_SSH_KEEP_ALIVE_PROBE_TIMEOUT_SECONDS.
  308. TunnelSshKeepAliveProbeTimeoutSeconds *int
  309. // TunnelSshKeepAlivePeriodicTimeoutSeconds specifies a timeout value for regular
  310. // SSH keep-alives that are sent periodically.
  311. // Zero value means keep-alive request will not time out.
  312. // If omitted, the default value is TUNNEL_SSH_KEEP_ALIVE_PERIODIC_TIMEOUT_SECONDS.
  313. TunnelSshKeepAlivePeriodicTimeoutSeconds *int
  314. // FetchRemoteServerListTimeoutSeconds specifies a timeout value for remote server list
  315. // HTTP request. Zero value means that request will not time out.
  316. // If omitted, the default value is FETCH_REMOTE_SERVER_LIST_TIMEOUT_SECONDS.
  317. FetchRemoteServerListTimeoutSeconds *int
  318. // PsiphonApiServerTimeoutSeconds specifies a timeout for periodic API HTTP
  319. // requests to Psiphon server such as stats, home pages, etc.
  320. // Zero value means that request will not time out.
  321. // If omitted, the default value is PSIPHON_API_SERVER_TIMEOUT_SECONDS.
  322. // Note that this value is overridden for final stats requests during shutdown
  323. // process in order to prevent hangs.
  324. PsiphonApiServerTimeoutSeconds *int
  325. // FetchRoutesTimeoutSeconds specifies a timeout value for split tunnel routes
  326. // HTTP request. Zero value means that request will not time out.
  327. // If omitted, the default value is FETCH_ROUTES_TIMEOUT_SECONDS.
  328. FetchRoutesTimeoutSeconds *int
  329. // HttpProxyOriginServerTimeoutSeconds specifies an HTTP response header timeout
  330. // value in various HTTP relays found in httpProxy.
  331. // Zero value means that request will not time out.
  332. // If omitted, the default value is HTTP_PROXY_ORIGIN_SERVER_TIMEOUT_SECONDS.
  333. HttpProxyOriginServerTimeoutSeconds *int
  334. // FetchRemoteServerListRetryPeriodSeconds specifies the delay before
  335. // resuming a remote server list download after a failure.
  336. // If omitted, the default value FETCH_REMOTE_SERVER_LIST_RETRY_PERIOD_SECONDS.
  337. FetchRemoteServerListRetryPeriodSeconds *int
  338. // DownloadUpgradestRetryPeriodSeconds specifies the delay before
  339. // resuming a client upgrade download after a failure.
  340. // If omitted, the default value DOWNLOAD_UPGRADE_RETRY_PERIOD_SECONDS.
  341. DownloadUpgradeRetryPeriodSeconds *int
  342. // EstablishTunnelPausePeriodSeconds specifies the delay between attempts
  343. // to establish tunnels. Briefly pausing allows for network conditions to improve
  344. // and for asynchronous operations such as fetch remote server list to complete.
  345. // If omitted, the default value is ESTABLISH_TUNNEL_PAUSE_PERIOD_SECONDS.
  346. EstablishTunnelPausePeriodSeconds *int
  347. // RateLimits specify throttling configuration for the tunnel.
  348. RateLimits common.RateLimits
  349. // EmitSLOKs indicates whether to emit notices for each seeded SLOK. As this
  350. // could reveal user browsing activity, it's intended for debugging and testing
  351. // only.
  352. EmitSLOKs bool
  353. }
  354. // LoadConfig parses and validates a JSON format Psiphon config JSON
  355. // string and returns a Config struct populated with config values.
  356. func LoadConfig(configJson []byte) (*Config, error) {
  357. var config Config
  358. err := json.Unmarshal(configJson, &config)
  359. if err != nil {
  360. return nil, common.ContextError(err)
  361. }
  362. // Do SetEmitDiagnosticNotices first, to ensure config file errors are emitted.
  363. if config.EmitDiagnosticNotices {
  364. SetEmitDiagnosticNotices(true)
  365. }
  366. // These fields are required; the rest are optional
  367. if config.PropagationChannelId == "" {
  368. return nil, common.ContextError(
  369. errors.New("propagation channel ID is missing from the configuration file"))
  370. }
  371. if config.SponsorId == "" {
  372. return nil, common.ContextError(
  373. errors.New("sponsor ID is missing from the configuration file"))
  374. }
  375. if config.DataStoreDirectory == "" {
  376. config.DataStoreDirectory, err = os.Getwd()
  377. if err != nil {
  378. return nil, common.ContextError(err)
  379. }
  380. }
  381. if config.ClientVersion == "" {
  382. config.ClientVersion = "0"
  383. }
  384. _, err = strconv.Atoi(config.ClientVersion)
  385. if err != nil {
  386. return nil, common.ContextError(
  387. fmt.Errorf("invalid client version: %s", err))
  388. }
  389. if config.TunnelProtocol != "" {
  390. if !common.Contains(protocol.SupportedTunnelProtocols, config.TunnelProtocol) {
  391. return nil, common.ContextError(
  392. errors.New("invalid tunnel protocol"))
  393. }
  394. }
  395. if config.EstablishTunnelTimeoutSeconds == nil {
  396. defaultEstablishTunnelTimeoutSeconds := ESTABLISH_TUNNEL_TIMEOUT_SECONDS
  397. config.EstablishTunnelTimeoutSeconds = &defaultEstablishTunnelTimeoutSeconds
  398. }
  399. if config.ConnectionWorkerPoolSize == 0 {
  400. config.ConnectionWorkerPoolSize = CONNECTION_WORKER_POOL_SIZE
  401. }
  402. if config.TunnelPoolSize == 0 {
  403. config.TunnelPoolSize = TUNNEL_POOL_SIZE
  404. }
  405. if config.NetworkConnectivityChecker != nil {
  406. return nil, common.ContextError(
  407. errors.New("NetworkConnectivityChecker interface must be set at runtime"))
  408. }
  409. if config.DeviceBinder != nil {
  410. return nil, common.ContextError(
  411. errors.New("DeviceBinder interface must be set at runtime"))
  412. }
  413. if config.DnsServerGetter != nil {
  414. return nil, common.ContextError(
  415. errors.New("DnsServerGetter interface must be set at runtime"))
  416. }
  417. if !common.Contains(
  418. []string{"", TRANSFORM_HOST_NAMES_ALWAYS, TRANSFORM_HOST_NAMES_NEVER},
  419. config.TransformHostNames) {
  420. return nil, common.ContextError(
  421. errors.New("invalid TransformHostNames"))
  422. }
  423. if !common.Contains(
  424. []string{"", protocol.PSIPHON_SSH_API_PROTOCOL, protocol.PSIPHON_WEB_API_PROTOCOL},
  425. config.TargetApiProtocol) {
  426. return nil, common.ContextError(
  427. errors.New("invalid TargetApiProtocol"))
  428. }
  429. if config.UpgradeDownloadUrl != "" &&
  430. (config.UpgradeDownloadClientVersionHeader == "" || config.UpgradeDownloadFilename == "") {
  431. return nil, common.ContextError(errors.New(
  432. "UpgradeDownloadUrl requires UpgradeDownloadClientVersionHeader and UpgradeDownloadFilename"))
  433. }
  434. if !config.DisableRemoteServerListFetcher {
  435. if config.RemoteServerListUrl != "" {
  436. if config.RemoteServerListSignaturePublicKey == "" {
  437. return nil, common.ContextError(errors.New("missing RemoteServerListSignaturePublicKey"))
  438. }
  439. if config.RemoteServerListDownloadFilename == "" {
  440. return nil, common.ContextError(errors.New("missing RemoteServerListDownloadFilename"))
  441. }
  442. }
  443. if config.ObfuscatedServerListRootURL != "" {
  444. if config.RemoteServerListSignaturePublicKey == "" {
  445. return nil, common.ContextError(errors.New("missing RemoteServerListSignaturePublicKey"))
  446. }
  447. if config.ObfuscatedServerListDownloadDirectory == "" {
  448. return nil, common.ContextError(errors.New("missing ObfuscatedServerListDownloadDirectory"))
  449. }
  450. }
  451. }
  452. if config.TunnelConnectTimeoutSeconds == nil {
  453. defaultTunnelConnectTimeoutSeconds := TUNNEL_CONNECT_TIMEOUT_SECONDS
  454. config.TunnelConnectTimeoutSeconds = &defaultTunnelConnectTimeoutSeconds
  455. }
  456. if config.TunnelPortForwardDialTimeoutSeconds == nil {
  457. TunnelPortForwardDialTimeoutSeconds := TUNNEL_PORT_FORWARD_DIAL_TIMEOUT_SECONDS
  458. config.TunnelPortForwardDialTimeoutSeconds = &TunnelPortForwardDialTimeoutSeconds
  459. }
  460. if config.TunnelSshKeepAliveProbeTimeoutSeconds == nil {
  461. defaultTunnelSshKeepAliveProbeTimeoutSeconds := TUNNEL_SSH_KEEP_ALIVE_PROBE_TIMEOUT_SECONDS
  462. config.TunnelSshKeepAliveProbeTimeoutSeconds = &defaultTunnelSshKeepAliveProbeTimeoutSeconds
  463. }
  464. if config.TunnelSshKeepAlivePeriodicTimeoutSeconds == nil {
  465. defaultTunnelSshKeepAlivePeriodicTimeoutSeconds := TUNNEL_SSH_KEEP_ALIVE_PERIODIC_TIMEOUT_SECONDS
  466. config.TunnelSshKeepAlivePeriodicTimeoutSeconds = &defaultTunnelSshKeepAlivePeriodicTimeoutSeconds
  467. }
  468. if config.FetchRemoteServerListTimeoutSeconds == nil {
  469. defaultFetchRemoteServerListTimeoutSeconds := FETCH_REMOTE_SERVER_LIST_TIMEOUT_SECONDS
  470. config.FetchRemoteServerListTimeoutSeconds = &defaultFetchRemoteServerListTimeoutSeconds
  471. }
  472. if config.PsiphonApiServerTimeoutSeconds == nil {
  473. defaultPsiphonApiServerTimeoutSeconds := PSIPHON_API_SERVER_TIMEOUT_SECONDS
  474. config.PsiphonApiServerTimeoutSeconds = &defaultPsiphonApiServerTimeoutSeconds
  475. }
  476. if config.FetchRoutesTimeoutSeconds == nil {
  477. defaultFetchRoutesTimeoutSeconds := FETCH_ROUTES_TIMEOUT_SECONDS
  478. config.FetchRoutesTimeoutSeconds = &defaultFetchRoutesTimeoutSeconds
  479. }
  480. if config.HttpProxyOriginServerTimeoutSeconds == nil {
  481. defaultHttpProxyOriginServerTimeoutSeconds := HTTP_PROXY_ORIGIN_SERVER_TIMEOUT_SECONDS
  482. config.HttpProxyOriginServerTimeoutSeconds = &defaultHttpProxyOriginServerTimeoutSeconds
  483. }
  484. if config.FetchRemoteServerListRetryPeriodSeconds == nil {
  485. defaultFetchRemoteServerListRetryPeriodSeconds := FETCH_REMOTE_SERVER_LIST_RETRY_PERIOD_SECONDS
  486. config.FetchRemoteServerListRetryPeriodSeconds = &defaultFetchRemoteServerListRetryPeriodSeconds
  487. }
  488. if config.DownloadUpgradeRetryPeriodSeconds == nil {
  489. defaultDownloadUpgradeRetryPeriodSeconds := DOWNLOAD_UPGRADE_RETRY_PERIOD_SECONDS
  490. config.DownloadUpgradeRetryPeriodSeconds = &defaultDownloadUpgradeRetryPeriodSeconds
  491. }
  492. if config.EstablishTunnelPausePeriodSeconds == nil {
  493. defaultEstablishTunnelPausePeriodSeconds := ESTABLISH_TUNNEL_PAUSE_PERIOD_SECONDS
  494. config.EstablishTunnelPausePeriodSeconds = &defaultEstablishTunnelPausePeriodSeconds
  495. }
  496. return &config, nil
  497. }