config.go 24 KB

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