config.go 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. * Copyright (c) 2016, 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 server
  20. import (
  21. "crypto/rand"
  22. "crypto/rsa"
  23. "crypto/x509"
  24. "encoding/base64"
  25. "encoding/hex"
  26. "encoding/json"
  27. "encoding/pem"
  28. "net"
  29. "os"
  30. "strconv"
  31. "strings"
  32. "sync/atomic"
  33. "time"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/accesscontrol"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/inproxy"
  39. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/osl"
  40. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  41. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  42. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
  43. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/values"
  44. "golang.org/x/crypto/nacl/box"
  45. )
  46. const (
  47. SERVER_CONFIG_FILENAME = "psiphond.config"
  48. SERVER_TRAFFIC_RULES_CONFIG_FILENAME = "psiphond-traffic-rules.config"
  49. SERVER_OSL_CONFIG_FILENAME = "psiphond-osl.config"
  50. SERVER_TACTICS_CONFIG_FILENAME = "psiphond-tactics.config"
  51. SERVER_ENTRY_FILENAME = "server-entry.dat"
  52. DEFAULT_SERVER_IP_ADDRESS = "127.0.0.1"
  53. WEB_SERVER_SECRET_BYTE_LENGTH = 32
  54. DISCOVERY_VALUE_KEY_BYTE_LENGTH = 32
  55. SSH_USERNAME_SUFFIX_BYTE_LENGTH = 8
  56. SSH_PASSWORD_BYTE_LENGTH = 32
  57. SSH_RSA_HOST_KEY_BITS = 2048
  58. SSH_OBFUSCATED_KEY_BYTE_LENGTH = 32
  59. PEAK_UPSTREAM_FAILURE_RATE_MINIMUM_SAMPLE_SIZE = 10
  60. PERIODIC_GARBAGE_COLLECTION = 120 * time.Second
  61. STOP_ESTABLISH_TUNNELS_ESTABLISHED_CLIENT_THRESHOLD = 20
  62. DEFAULT_LOG_FILE_REOPEN_RETRIES = 25
  63. )
  64. // Config specifies the configuration and behavior of a Psiphon
  65. // server.
  66. type Config struct {
  67. // LogLevel specifies the log level. Valid values are:
  68. // panic, fatal, error, warn, info, debug
  69. LogLevel string
  70. // LogFilename specifies the path of the file to log
  71. // to. When blank, logs are written to stderr.
  72. LogFilename string
  73. // LogFileReopenRetries specifies how many retries, each with a 1ms delay,
  74. // will be attempted after reopening a rotated log file fails. Retries
  75. // mitigate any race conditions between writes/reopens and file operations
  76. // performed by external log managers, such as logrotate.
  77. //
  78. // When omitted, DEFAULT_LOG_FILE_REOPEN_RETRIES is used.
  79. LogFileReopenRetries *int
  80. // LogFileCreateMode specifies that the Psiphon server should create a new
  81. // log file when one is not found, such as after rotation with logrotate
  82. // configured with nocreate. The value is the os.FileMode value to use when
  83. // creating the file.
  84. //
  85. // When omitted, the Psiphon server does not create log files.
  86. LogFileCreateMode *int
  87. // SkipPanickingLogWriter disables panicking when
  88. // unable to write any logs.
  89. SkipPanickingLogWriter bool
  90. // DiscoveryValueHMACKey is the network-wide secret value
  91. // used to determine a unique discovery strategy.
  92. DiscoveryValueHMACKey string
  93. // GeoIPDatabaseFilenames are paths of GeoIP2/GeoLite2
  94. // MaxMind database files. When empty, no GeoIP lookups are
  95. // performed. Each file is queried, in order, for the
  96. // logged fields: country code, city, and ISP. Multiple
  97. // file support accommodates the MaxMind distribution where
  98. // ISP data in a separate file.
  99. GeoIPDatabaseFilenames []string
  100. // PsinetDatabaseFilename is the path of the file containing
  101. // psinet.Database data.
  102. PsinetDatabaseFilename string
  103. // HostID identifies the server host; this value is included with all logs.
  104. HostID string
  105. // HostProvider identifies the server host provider; this value is
  106. // included with all logs and logged only when not blank.
  107. HostProvider string
  108. // ServerIPAddress is the public IP address of the server.
  109. ServerIPAddress string
  110. // TunnelProtocolPorts specifies which tunnel protocols to run
  111. // and which ports to listen on for each protocol. Valid tunnel
  112. // protocols include:
  113. // "SSH", "OSSH", "TLS-OSSH", "UNFRONTED-MEEK-OSSH", "UNFRONTED-MEEK-HTTPS-OSSH",
  114. // "UNFRONTED-MEEK-SESSION-TICKET-OSSH", "FRONTED-MEEK-OSSH",
  115. // "FRONTED-MEEK-QUIC-OSSH", "FRONTED-MEEK-HTTP-OSSH", "QUIC-OSSH",
  116. // "TAPDANCE-OSSH", abd "CONJURE-OSSH".
  117. TunnelProtocolPorts map[string]int
  118. // TunnelProtocolPassthroughAddresses specifies passthrough addresses to be
  119. // used for tunnel protocols configured in TunnelProtocolPorts. Passthrough
  120. // is a probing defense which relays all network traffic between a client and
  121. // the passthrough target when the client fails anti-probing tests.
  122. //
  123. // TunnelProtocolPassthroughAddresses is supported for:
  124. // "TLS-OSSH", "UNFRONTED-MEEK-HTTPS-OSSH",
  125. // "UNFRONTED-MEEK-SESSION-TICKET-OSSH", "UNFRONTED-MEEK-OSSH".
  126. TunnelProtocolPassthroughAddresses map[string]string
  127. // LegacyPassthrough indicates whether to expect legacy passthrough messages
  128. // from clients attempting to connect. This should be set for existing/legacy
  129. // passthrough servers only.
  130. LegacyPassthrough bool
  131. // EnableGQUIC indicates whether to enable legacy gQUIC QUIC-OSSH
  132. // versions, for backwards compatibility with all versions used by older
  133. // clients. Enabling gQUIC degrades the anti-probing stance of QUIC-OSSH,
  134. // as the legacy gQUIC stack will respond to probing packets.
  135. EnableGQUIC bool
  136. // SSHPrivateKey is the SSH host key. The same key is used for
  137. // all protocols, run by this server instance, which use SSH.
  138. SSHPrivateKey string
  139. // SSHServerVersion is the server version presented in the
  140. // identification string. The same value is used for all
  141. // protocols, run by this server instance, which use SSH.
  142. SSHServerVersion string
  143. // SSHUserName is the SSH user name to be presented by the
  144. // the tunnel-core client. The same value is used for all
  145. // protocols, run by this server instance, which use SSH.
  146. SSHUserName string
  147. // SSHPassword is the SSH password to be presented by the
  148. // the tunnel-core client. The same value is used for all
  149. // protocols, run by this server instance, which use SSH.
  150. SSHPassword string
  151. // SSHBeginHandshakeTimeoutMilliseconds specifies the timeout
  152. // for clients queueing to begin an SSH handshake. The default
  153. // is SSH_BEGIN_HANDSHAKE_TIMEOUT.
  154. SSHBeginHandshakeTimeoutMilliseconds *int
  155. // SSHHandshakeTimeoutMilliseconds specifies the timeout
  156. // before which a client must complete its handshake. The default
  157. // is SSH_HANDSHAKE_TIMEOUT.
  158. SSHHandshakeTimeoutMilliseconds *int
  159. // ObfuscatedSSHKey is the secret key for use in the Obfuscated
  160. // SSH protocol. The same secret key is used for all protocols,
  161. // run by this server instance, which use Obfuscated SSH.
  162. ObfuscatedSSHKey string
  163. // MeekCookieEncryptionPrivateKey is the NaCl private key used
  164. // to decrypt meek cookie payload sent from clients. The same
  165. // key is used for all meek protocols run by this server instance.
  166. MeekCookieEncryptionPrivateKey string
  167. // MeekObfuscatedKey is the secret key used for obfuscating
  168. // meek cookies sent from clients. The same key is used for all
  169. // meek protocols run by this server instance.
  170. //
  171. // NOTE: this key is also used by the TLS-OSSH protocol, which allows
  172. // clients with legacy unfronted meek-https server entries, that have the
  173. // passthrough capability, to connect with TLS-OSSH to the servers
  174. // corresponding to those server entries, which now support TLS-OSSH by
  175. // demultiplexing meek-https and TLS-OSSH over the meek-https port.
  176. MeekObfuscatedKey string
  177. // MeekProhibitedHeaders is a list of HTTP headers to check for
  178. // in client requests. If one of these headers is found, the
  179. // request fails. This is used to defend against abuse.
  180. MeekProhibitedHeaders []string
  181. // MeekRequiredHeaders is a list of HTTP header names and values that must
  182. // appear in requests. This is used to defend against abuse.
  183. MeekRequiredHeaders map[string]string
  184. // MeekServerCertificate specifies an optional certificate to use for meek
  185. // servers, in place of the default, randomly generate certificate. When
  186. // specified, the corresponding private key must be supplied in
  187. // MeekServerPrivateKey. Any specified certificate is used for all meek
  188. // listeners.
  189. MeekServerCertificate string
  190. // MeekServerPrivateKey is the private key corresponding to the optional
  191. // MeekServerCertificate parameter.
  192. MeekServerPrivateKey string
  193. // MeekProxyForwardedForHeaders is a list of HTTP headers which
  194. // may be added by downstream HTTP proxies or CDNs in front
  195. // of clients. These headers supply the original client IP
  196. // address, which is geolocated for stats purposes. Headers
  197. // include, for example, X-Forwarded-For. The header's value
  198. // is assumed to be a comma delimted list of IP addresses where
  199. // the client IP is the first IP address in the list. Meek protocols
  200. // look for these headers and use the client IP address from
  201. // the header if any one is present and the value is a valid
  202. // IP address; otherwise the direct connection remote address is
  203. // used as the client IP.
  204. MeekProxyForwardedForHeaders []string
  205. // MeekTurnAroundTimeoutMilliseconds specifies the amount of time meek will
  206. // wait for downstream bytes before responding to a request. The default is
  207. // MEEK_DEFAULT_TURN_AROUND_TIMEOUT.
  208. MeekTurnAroundTimeoutMilliseconds *int
  209. // MeekExtendedTurnAroundTimeoutMilliseconds specifies the extended amount of
  210. // time meek will wait for downstream bytes, as long as bytes arrive every
  211. // MeekTurnAroundTimeoutMilliseconds, before responding to a request. The
  212. // default is MEEK_DEFAULT_EXTENDED_TURN_AROUND_TIMEOUT.
  213. MeekExtendedTurnAroundTimeoutMilliseconds *int
  214. // MeekSkipExtendedTurnAroundThresholdBytes specifies when to skip the
  215. // extended turn around. When the number of bytes received in the client
  216. // request meets the threshold, optimize for upstream flows with quicker
  217. // round trip turn arounds.
  218. MeekSkipExtendedTurnAroundThresholdBytes *int
  219. // MeekMaxSessionStalenessMilliseconds specifies the TTL for meek sessions.
  220. // The default is MEEK_DEFAULT_MAX_SESSION_STALENESS.
  221. MeekMaxSessionStalenessMilliseconds *int
  222. // MeekHTTPClientIOTimeoutMilliseconds specifies meek HTTP server I/O
  223. // timeouts. The default is MEEK_DEFAULT_HTTP_CLIENT_IO_TIMEOUT.
  224. MeekHTTPClientIOTimeoutMilliseconds *int
  225. // MeekFrontedHTTPClientIOTimeoutMilliseconds specifies meek HTTP server
  226. // I/O timeouts for fronted protocols. The default is
  227. // MEEK_DEFAULT_FRONTED_HTTP_CLIENT_IO_TIMEOUT.
  228. MeekFrontedHTTPClientIOTimeoutMilliseconds *int
  229. // MeekCachedResponseBufferSize is the size of a private,
  230. // fixed-size buffer allocated for every meek client. The buffer
  231. // is used to cache response payload, allowing the client to retry
  232. // fetching when a network connection is interrupted. This retry
  233. // makes the OSSH tunnel within meek resilient to interruptions
  234. // at the HTTP TCP layer.
  235. // Larger buffers increase resiliency to interruption, but consume
  236. // more memory as buffers as never freed. The maximum size of a
  237. // response payload is a function of client activity, network
  238. // throughput and throttling.
  239. // A default of 64K is used when MeekCachedResponseBufferSize is 0.
  240. MeekCachedResponseBufferSize int
  241. // MeekCachedResponsePoolBufferSize is the size of a fixed-size,
  242. // shared buffer used to temporarily extend a private buffer when
  243. // MeekCachedResponseBufferSize is insufficient. Shared buffers
  244. // allow some clients to successfully retry longer response payloads
  245. // without allocating large buffers for all clients.
  246. // A default of 64K is used when MeekCachedResponsePoolBufferSize
  247. // is 0.
  248. MeekCachedResponsePoolBufferSize int
  249. // MeekCachedResponsePoolBufferCount is the number of shared
  250. // buffers. Shared buffers are allocated on first use and remain
  251. // allocated, so shared buffer count * size is roughly the memory
  252. // overhead of this facility.
  253. // A default of 2048 is used when MeekCachedResponsePoolBufferCount
  254. // is 0.
  255. MeekCachedResponsePoolBufferCount int
  256. // UDPInterceptUdpgwServerAddress specifies the network address of
  257. // a udpgw server which clients may be port forwarding to. When
  258. // specified, these TCP port forwards are intercepted and handled
  259. // directly by this server, which parses the SSH channel using the
  260. // udpgw protocol. Handling includes udpgw transparent DNS: tunneled
  261. // UDP DNS packets are rerouted to the host's DNS server.
  262. //
  263. // The intercept is applied before the port forward destination is
  264. // validated against SSH_DISALLOWED_PORT_FORWARD_HOSTS and
  265. // AllowTCPPorts. So the intercept address may be any otherwise
  266. // prohibited destination.
  267. UDPInterceptUdpgwServerAddress string
  268. // DNSResolverIPAddress specifies the IP address of a DNS server
  269. // to be used when "/etc/resolv.conf" doesn't exist or fails to
  270. // parse. When blank, "/etc/resolv.conf" must contain a usable
  271. // "nameserver" entry.
  272. DNSResolverIPAddress string
  273. // LoadMonitorPeriodSeconds indicates how frequently to log server
  274. // load information (number of connected clients per tunnel protocol,
  275. // number of running goroutines, amount of memory allocated, etc.)
  276. // The default, 0, disables load logging.
  277. LoadMonitorPeriodSeconds int
  278. // PeakUpstreamFailureRateMinimumSampleSize specifies the minimum number
  279. // of samples (e.g., upstream port forward attempts) that are required
  280. // before taking a failure rate snapshot which may be recorded as
  281. // peak_dns_failure_rate/peak_tcp_port_forward_failure_rate. The default
  282. // is PEAK_UPSTREAM_FAILURE_RATE_SAMPLE_SIZE.
  283. PeakUpstreamFailureRateMinimumSampleSize *int
  284. // ProcessProfileOutputDirectory is the path of a directory to which
  285. // process profiles will be written when signaled with SIGUSR2. The
  286. // files are overwritten on each invocation. When set to the default
  287. // value, blank, no profiles are written on SIGUSR2. Profiles include
  288. // the default profiles here: https://golang.org/pkg/runtime/pprof/#Profile.
  289. ProcessProfileOutputDirectory string
  290. // ProcessBlockProfileDurationSeconds specifies the sample duration for
  291. // "block" profiling. For the default, 0, no "block" profile is taken.
  292. ProcessBlockProfileDurationSeconds int
  293. // ProcessCPUProfileDurationSeconds specifies the sample duration for
  294. // CPU profiling. For the default, 0, no CPU profile is taken.
  295. ProcessCPUProfileDurationSeconds int
  296. // TrafficRulesFilename is the path of a file containing a JSON-encoded
  297. // TrafficRulesSet, the traffic rules to apply to Psiphon client tunnels.
  298. TrafficRulesFilename string
  299. // OSLConfigFilename is the path of a file containing a JSON-encoded
  300. // OSL Config, the OSL schemes to apply to Psiphon client tunnels.
  301. OSLConfigFilename string
  302. // RunPacketTunnel specifies whether to run a packet tunnel.
  303. RunPacketTunnel bool
  304. // PacketTunnelEgressInterface specifies tun.ServerConfig.EgressInterface.
  305. PacketTunnelEgressInterface string
  306. // PacketTunnelEnableDNSFlowTracking sets
  307. // tun.ServerConfig.EnableDNSFlowTracking.
  308. PacketTunnelEnableDNSFlowTracking bool
  309. // PacketTunnelDownstreamPacketQueueSize specifies
  310. // tun.ServerConfig.DownStreamPacketQueueSize.
  311. PacketTunnelDownstreamPacketQueueSize int
  312. // PacketTunnelSessionIdleExpirySeconds specifies
  313. // tun.ServerConfig.SessionIdleExpirySeconds.
  314. PacketTunnelSessionIdleExpirySeconds int
  315. // PacketTunnelSudoNetworkConfigCommands sets
  316. // tun.ServerConfig.SudoNetworkConfigCommands.
  317. PacketTunnelSudoNetworkConfigCommands bool
  318. // RunPacketManipulator specifies whether to run a packet manipulator.
  319. RunPacketManipulator bool
  320. // MaxConcurrentSSHHandshakes specifies a limit on the number of concurrent
  321. // SSH handshake negotiations. This is set to mitigate spikes in memory
  322. // allocations and CPU usage associated with SSH handshakes when many clients
  323. // attempt to connect concurrently. When a maximum limit is specified and
  324. // reached, additional clients that establish TCP or meek connections will
  325. // be disconnected after a short wait for the number of concurrent handshakes
  326. // to drop below the limit.
  327. // The default, 0 is no limit.
  328. MaxConcurrentSSHHandshakes int
  329. // PeriodicGarbageCollectionSeconds turns on periodic calls to
  330. // debug.FreeOSMemory, every specified number of seconds, to force garbage
  331. // collection and memory scavenging. Specify 0 to disable. The default is
  332. // PERIODIC_GARBAGE_COLLECTION.
  333. PeriodicGarbageCollectionSeconds *int
  334. // StopEstablishTunnelsEstablishedClientThreshold sets the established client
  335. // threshold for dumping profiles when SIGTSTP is signaled. When there are
  336. // less than or equal to the threshold number of established clients,
  337. // profiles are dumped to aid investigating unusual load limited states that
  338. // occur when few clients are connected and load should be relatively low. A
  339. // profile dump is attempted at most once per process lifetime, the first
  340. // time the threshold is met. Disabled when < 0.
  341. StopEstablishTunnelsEstablishedClientThreshold *int
  342. // AccessControlVerificationKeyRing is the access control authorization
  343. // verification key ring used to verify signed authorizations presented
  344. // by clients. Verified, active (unexpired) access control types will be
  345. // available for matching in the TrafficRulesFilter for the client via
  346. // AuthorizedAccessTypes. All other authorizations are ignored.
  347. AccessControlVerificationKeyRing accesscontrol.VerificationKeyRing
  348. // TacticsConfigFilename is the path of a file containing a JSON-encoded
  349. // tactics server configuration.
  350. TacticsConfigFilename string
  351. // BlocklistFilename is the path of a file containing a CSV-encoded
  352. // blocklist configuration. See NewBlocklist for more file format
  353. // documentation.
  354. BlocklistFilename string
  355. // BlocklistActive indicates whether to actively prevent blocklist hits in
  356. // addition to logging events.
  357. BlocklistActive bool
  358. // AllowBogons disables port forward bogon checks. This should be used only
  359. // for testing.
  360. AllowBogons bool
  361. // EnableSteeringIPs enables meek server steering IP support.
  362. EnableSteeringIPs bool
  363. // OwnEncodedServerEntries is a list of the server's own encoded server
  364. // entries, idenfified by server entry tag. These values are used in the
  365. // handshake API to update clients that don't yet have a signed copy of these
  366. // server entries.
  367. //
  368. // For purposes of compartmentalization, each server receives only its own
  369. // server entries here; and, besides the discovery server entries, in
  370. // psinet.Database, necessary for the discovery feature, no other server
  371. // entries are stored on a Psiphon server.
  372. OwnEncodedServerEntries map[string]string
  373. // MeekServerRunInproxyBroker indicates whether to run an in-proxy broker
  374. // endpoint and service under the meek server.
  375. MeekServerRunInproxyBroker bool
  376. // MeekServerInproxyBrokerOnly indicates whether to run only an in-proxy
  377. // broker under the meek server, and not run any meek tunnel protocol. To
  378. // run the meek listener, a meek server protocol and port must still be
  379. // specified in TunnelProtocolPorts, but no other tunnel protocol
  380. // parameters are required.
  381. MeekServerInproxyBrokerOnly bool
  382. // InproxyBrokerSessionPrivateKey specifies the broker's in-proxy session
  383. // private key and derived public key used by in-proxy clients and
  384. // proxies. This value is required when running an in-proxy broker.
  385. InproxyBrokerSessionPrivateKey string
  386. // InproxyBrokerObfuscationRootSecret specifies the broker's in-proxy
  387. // session root obfuscation secret used by in-proxy clients and proxies.
  388. // This value is required when running an in-proxy broker.
  389. InproxyBrokerObfuscationRootSecret string
  390. // InproxyBrokerServerEntrySignaturePublicKey specifies the public key
  391. // used to verify Psiphon server entry signature. This value is required
  392. // when running an in-proxy broker.
  393. InproxyBrokerServerEntrySignaturePublicKey string
  394. // InproxyBrokerAllowCommonASNMatching overrides the default broker
  395. // matching behavior which doesn't match non-personal in-proxy clients
  396. // and proxies from the same ASN. This parameter is for testing only.
  397. InproxyBrokerAllowCommonASNMatching bool
  398. // InproxyServerSessionPrivateKey specifies the server's in-proxy session
  399. // private key and derived public key used by brokers. This value is
  400. // required when running in-proxy tunnel protocols.
  401. InproxyServerSessionPrivateKey string
  402. // InproxyServerObfuscationRootSecret specifies the server's in-proxy
  403. // session root obfuscation secret used by brokers. This value is
  404. // required when running in-proxy tunnel protocols.
  405. InproxyServerObfuscationRootSecret string
  406. sshBeginHandshakeTimeout time.Duration
  407. sshHandshakeTimeout time.Duration
  408. peakUpstreamFailureRateMinimumSampleSize int
  409. periodicGarbageCollection time.Duration
  410. stopEstablishTunnelsEstablishedClientThreshold int
  411. dumpProfilesOnStopEstablishTunnelsDone int32
  412. providerID string
  413. frontingProviderID string
  414. region string
  415. runningProtocols []string
  416. }
  417. // GetLogFileReopenConfig gets the reopen retries, and create/mode inputs for
  418. // rotate.NewRotatableFileWriter, which is used when writing to log files.
  419. //
  420. // By default, we expect the log files to be managed by logrotate, with
  421. // logrotate configured to re-create the next log file after rotation. As
  422. // described in the documentation for rotate.NewRotatableFileWriter, and as
  423. // observed in production, we occasionally need retries when attempting to
  424. // reopen the log file post-rotation; and we avoid conflicts, and spurious
  425. // re-rotations, by disabling file create in rotate.NewRotatableFileWriter. In
  426. // large scale production, incidents requiring retry are very rare, so the
  427. // retry delay is not expected to have a significant impact on performance.
  428. //
  429. // The defaults may be overriden in the Config.
  430. func (config *Config) GetLogFileReopenConfig() (int, bool, os.FileMode) {
  431. retries := DEFAULT_LOG_FILE_REOPEN_RETRIES
  432. if config.LogFileReopenRetries != nil {
  433. retries = *config.LogFileReopenRetries
  434. }
  435. create := false
  436. mode := os.FileMode(0)
  437. if config.LogFileCreateMode != nil {
  438. create = true
  439. mode = os.FileMode(*config.LogFileCreateMode)
  440. }
  441. return retries, create, mode
  442. }
  443. // RunLoadMonitor indicates whether to monitor and log server load.
  444. func (config *Config) RunLoadMonitor() bool {
  445. return config.LoadMonitorPeriodSeconds > 0
  446. }
  447. // RunPeriodicGarbageCollection indicates whether to run periodic garbage collection.
  448. func (config *Config) RunPeriodicGarbageCollection() bool {
  449. return config.periodicGarbageCollection > 0
  450. }
  451. // DumpProfilesOnStopEstablishTunnels indicates whether dump profiles due to
  452. // an unexpectedly low number of established clients during high load.
  453. func (config *Config) DumpProfilesOnStopEstablishTunnels(establishedClientsCount int) bool {
  454. if config.stopEstablishTunnelsEstablishedClientThreshold < 0 {
  455. return false
  456. }
  457. if atomic.LoadInt32(&config.dumpProfilesOnStopEstablishTunnelsDone) != 0 {
  458. return false
  459. }
  460. dump := (establishedClientsCount <= config.stopEstablishTunnelsEstablishedClientThreshold)
  461. atomic.StoreInt32(&config.dumpProfilesOnStopEstablishTunnelsDone, 1)
  462. return dump
  463. }
  464. // GetOwnEncodedServerEntry returns one of the server's own server entries, as
  465. // identified by the server entry tag.
  466. func (config *Config) GetOwnEncodedServerEntry(serverEntryTag string) (string, bool) {
  467. serverEntry, ok := config.OwnEncodedServerEntries[serverEntryTag]
  468. return serverEntry, ok
  469. }
  470. // GetProviderID returns the provider ID associated with the server.
  471. func (config *Config) GetProviderID() string {
  472. return config.providerID
  473. }
  474. // GetFrontingProviderID returns the fronting provider ID associated with the
  475. // server's fronted protocol(s).
  476. func (config *Config) GetFrontingProviderID() string {
  477. return config.frontingProviderID
  478. }
  479. // GetRegion returns the region associated with the server.
  480. func (config *Config) GetRegion() string {
  481. return config.region
  482. }
  483. // GetRunningProtocols returns the list of protcols this server is running.
  484. // The caller must not mutate the return value.
  485. func (config *Config) GetRunningProtocols() []string {
  486. return config.runningProtocols
  487. }
  488. // LoadConfig loads and validates a JSON encoded server config.
  489. func LoadConfig(configJSON []byte) (*Config, error) {
  490. var config Config
  491. err := json.Unmarshal(configJSON, &config)
  492. if err != nil {
  493. return nil, errors.Trace(err)
  494. }
  495. if config.ServerIPAddress == "" {
  496. return nil, errors.TraceNew("ServerIPAddress is required")
  497. }
  498. if config.MeekServerRunInproxyBroker {
  499. if config.InproxyBrokerSessionPrivateKey == "" {
  500. return nil, errors.TraceNew("Inproxy Broker requires InproxyBrokerSessionPrivateKey")
  501. }
  502. if config.InproxyBrokerObfuscationRootSecret == "" {
  503. return nil, errors.TraceNew("Inproxy Broker requires InproxyBrokerObfuscationRootSecret")
  504. }
  505. // There must be at least one meek tunnel protocol configured for
  506. // MeekServer to run and host an in-proxy broker. Since each
  507. // MeekServer instance runs its own in-proxy Broker instance, allow
  508. // at most one meek tunnel protocol to be configured so all
  509. // connections to the broker use the same, unambiguous instance.
  510. meekServerCount := 0
  511. for tunnelProtocol, _ := range config.TunnelProtocolPorts {
  512. if protocol.TunnelProtocolUsesMeek(tunnelProtocol) {
  513. meekServerCount += 1
  514. }
  515. }
  516. if meekServerCount != 1 {
  517. return nil, errors.TraceNew("Inproxy Broker requires one MeekServer instance")
  518. }
  519. }
  520. if config.MeekServerInproxyBrokerOnly {
  521. if !config.MeekServerRunInproxyBroker {
  522. return nil, errors.TraceNew("Inproxy Broker-only mode requires MeekServerRunInproxyBroker")
  523. }
  524. }
  525. for tunnelProtocol, _ := range config.TunnelProtocolPorts {
  526. if !common.Contains(protocol.SupportedTunnelProtocols, tunnelProtocol) {
  527. return nil, errors.Tracef("Unsupported tunnel protocol: %s", tunnelProtocol)
  528. }
  529. if config.MeekServerInproxyBrokerOnly && protocol.TunnelProtocolUsesMeek(tunnelProtocol) {
  530. // In in-proxy broker-only mode, the TunnelProtocolPorts must be
  531. // specified in order to run the MeekServer, but none of the
  532. // following meek tunnel parameters are required.
  533. continue
  534. }
  535. if protocol.TunnelProtocolUsesSSH(tunnelProtocol) ||
  536. protocol.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  537. if config.SSHPrivateKey == "" || config.SSHServerVersion == "" ||
  538. config.SSHUserName == "" || config.SSHPassword == "" {
  539. return nil, errors.Tracef(
  540. "Tunnel protocol %s requires SSHPrivateKey, SSHServerVersion, SSHUserName, SSHPassword",
  541. tunnelProtocol)
  542. }
  543. }
  544. if protocol.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  545. if config.ObfuscatedSSHKey == "" {
  546. return nil, errors.Tracef(
  547. "Tunnel protocol %s requires ObfuscatedSSHKey",
  548. tunnelProtocol)
  549. }
  550. }
  551. if protocol.TunnelProtocolUsesTLSOSSH(tunnelProtocol) {
  552. // Meek obfuscated key used for legacy reasons. See comment for
  553. // MeekObfuscatedKey.
  554. if config.MeekObfuscatedKey == "" {
  555. return nil, errors.Tracef(
  556. "Tunnel protocol %s requires MeekObfuscatedKey",
  557. tunnelProtocol)
  558. }
  559. }
  560. if protocol.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  561. protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  562. if config.MeekCookieEncryptionPrivateKey == "" || config.MeekObfuscatedKey == "" {
  563. return nil, errors.Tracef(
  564. "Tunnel protocol %s requires MeekCookieEncryptionPrivateKey, MeekObfuscatedKey",
  565. tunnelProtocol)
  566. }
  567. }
  568. // For FRONTED QUIC and HTTP, HTTPS is always used on the
  569. // edge-to-server hop, so it must be enabled or else this
  570. // configuration will not work. There is no FRONTED QUIC listener at
  571. // all; see TunnelServer.Run.
  572. if protocol.TunnelProtocolUsesFrontedMeek(tunnelProtocol) {
  573. _, ok := config.TunnelProtocolPorts[protocol.TUNNEL_PROTOCOL_FRONTED_MEEK]
  574. if !ok {
  575. return nil, errors.Tracef(
  576. "Tunnel protocol %s requires %s to be enabled",
  577. tunnelProtocol,
  578. protocol.TUNNEL_PROTOCOL_FRONTED_MEEK)
  579. }
  580. }
  581. }
  582. for tunnelProtocol, address := range config.TunnelProtocolPassthroughAddresses {
  583. if !protocol.TunnelProtocolSupportsPassthrough(tunnelProtocol) {
  584. return nil, errors.Tracef("Passthrough unsupported tunnel protocol: %s", tunnelProtocol)
  585. }
  586. if _, _, err := net.SplitHostPort(address); err != nil {
  587. if err != nil {
  588. return nil, errors.Tracef(
  589. "Tunnel protocol %s passthrough address %s invalid: %s",
  590. tunnelProtocol, address, err)
  591. }
  592. }
  593. }
  594. config.sshBeginHandshakeTimeout = SSH_BEGIN_HANDSHAKE_TIMEOUT
  595. if config.SSHBeginHandshakeTimeoutMilliseconds != nil {
  596. config.sshBeginHandshakeTimeout = time.Duration(*config.SSHBeginHandshakeTimeoutMilliseconds) * time.Millisecond
  597. }
  598. config.sshHandshakeTimeout = SSH_HANDSHAKE_TIMEOUT
  599. if config.SSHHandshakeTimeoutMilliseconds != nil {
  600. config.sshHandshakeTimeout = time.Duration(*config.SSHHandshakeTimeoutMilliseconds) * time.Millisecond
  601. }
  602. if config.ObfuscatedSSHKey != "" {
  603. seed, err := protocol.DeriveSSHServerVersionPRNGSeed(config.ObfuscatedSSHKey)
  604. if err != nil {
  605. return nil, errors.Tracef(
  606. "DeriveSSHServerVersionPRNGSeed failed: %s", err)
  607. }
  608. serverVersion := values.GetSSHServerVersion(seed)
  609. if serverVersion != "" {
  610. config.SSHServerVersion = serverVersion
  611. }
  612. }
  613. if config.UDPInterceptUdpgwServerAddress != "" {
  614. if err := validateNetworkAddress(config.UDPInterceptUdpgwServerAddress, true); err != nil {
  615. return nil, errors.Tracef("UDPInterceptUdpgwServerAddress is invalid: %s", err)
  616. }
  617. }
  618. if config.DNSResolverIPAddress != "" {
  619. if net.ParseIP(config.DNSResolverIPAddress) == nil {
  620. return nil, errors.Tracef("DNSResolverIPAddress is invalid")
  621. }
  622. }
  623. config.peakUpstreamFailureRateMinimumSampleSize = PEAK_UPSTREAM_FAILURE_RATE_MINIMUM_SAMPLE_SIZE
  624. if config.PeakUpstreamFailureRateMinimumSampleSize != nil {
  625. config.peakUpstreamFailureRateMinimumSampleSize = *config.PeakUpstreamFailureRateMinimumSampleSize
  626. }
  627. config.periodicGarbageCollection = PERIODIC_GARBAGE_COLLECTION
  628. if config.PeriodicGarbageCollectionSeconds != nil {
  629. config.periodicGarbageCollection = time.Duration(*config.PeriodicGarbageCollectionSeconds) * time.Second
  630. }
  631. config.stopEstablishTunnelsEstablishedClientThreshold = STOP_ESTABLISH_TUNNELS_ESTABLISHED_CLIENT_THRESHOLD
  632. if config.StopEstablishTunnelsEstablishedClientThreshold != nil {
  633. config.stopEstablishTunnelsEstablishedClientThreshold = *config.StopEstablishTunnelsEstablishedClientThreshold
  634. }
  635. err = accesscontrol.ValidateVerificationKeyRing(&config.AccessControlVerificationKeyRing)
  636. if err != nil {
  637. return nil, errors.Tracef(
  638. "AccessControlVerificationKeyRing is invalid: %s", err)
  639. }
  640. // Limitation: the following is a shortcut which extracts the server's
  641. // fronting provider ID from the server's OwnEncodedServerEntries. This logic
  642. // assumes a server has only one fronting provider. In principle, it's
  643. // possible for server with multiple server entries to run multiple fronted
  644. // protocols, each with a different fronting provider ID.
  645. //
  646. // TODO: add an explicit parameter mapping tunnel protocol ports to fronting
  647. // provider IDs.
  648. for _, encodedServerEntry := range config.OwnEncodedServerEntries {
  649. serverEntry, err := protocol.DecodeServerEntry(encodedServerEntry, "", "")
  650. if err != nil {
  651. return nil, errors.Tracef(
  652. "protocol.DecodeServerEntry failed: %s", err)
  653. }
  654. if config.providerID == "" {
  655. config.providerID = serverEntry.ProviderID
  656. } else if config.providerID != serverEntry.ProviderID {
  657. return nil, errors.Tracef("unsupported multiple ProviderID values")
  658. }
  659. if config.frontingProviderID == "" {
  660. config.frontingProviderID = serverEntry.FrontingProviderID
  661. } else if config.frontingProviderID != serverEntry.FrontingProviderID {
  662. return nil, errors.Tracef("unsupported multiple FrontingProviderID values")
  663. }
  664. if config.region == "" {
  665. config.region = serverEntry.Region
  666. } else if config.region != serverEntry.Region {
  667. return nil, errors.Tracef("unsupported multiple Region values")
  668. }
  669. }
  670. config.runningProtocols = []string{}
  671. for tunnelProtocol := range config.TunnelProtocolPorts {
  672. config.runningProtocols = append(config.runningProtocols, tunnelProtocol)
  673. }
  674. return &config, nil
  675. }
  676. func validateNetworkAddress(address string, requireIPaddress bool) error {
  677. host, portStr, err := net.SplitHostPort(address)
  678. if err != nil {
  679. return err
  680. }
  681. if requireIPaddress && net.ParseIP(host) == nil {
  682. return errors.TraceNew("host must be an IP address")
  683. }
  684. port, err := strconv.Atoi(portStr)
  685. if err != nil {
  686. return err
  687. }
  688. if port < 0 || port > 65535 {
  689. return errors.TraceNew("invalid port")
  690. }
  691. return nil
  692. }
  693. // GenerateConfigParams specifies customizations to be applied to
  694. // a generated server config.
  695. type GenerateConfigParams struct {
  696. LogFilename string
  697. SkipPanickingLogWriter bool
  698. LogLevel string
  699. ServerEntrySignaturePublicKey string
  700. ServerEntrySignaturePrivateKey string
  701. ServerIPAddress string
  702. TunnelProtocolPorts map[string]int
  703. TunnelProtocolPassthroughAddresses map[string]string
  704. TrafficRulesConfigFilename string
  705. OSLConfigFilename string
  706. TacticsConfigFilename string
  707. TacticsRequestPublicKey string
  708. TacticsRequestObfuscatedKey string
  709. Passthrough bool
  710. LegacyPassthrough bool
  711. LimitQUICVersions protocol.QUICVersions
  712. EnableGQUIC bool
  713. FrontingProviderID string
  714. }
  715. // GenerateConfig creates a new Psiphon server config. It returns JSON encoded
  716. // configs and a client-compatible "server entry" for the server. It generates
  717. // all necessary secrets and key material, which are emitted in the config
  718. // file and server entry as necessary.
  719. //
  720. // GenerateConfig uses sample values for many fields. The intention is for
  721. // generated configs to be used for testing or as examples for production
  722. // setup, not to generate production-ready configurations.
  723. //
  724. // When tactics key material is provided in GenerateConfigParams, tactics
  725. // capabilities are added for all meek protocols in TunnelProtocolPorts.
  726. func GenerateConfig(params *GenerateConfigParams) ([]byte, []byte, []byte, []byte, []byte, error) {
  727. // Input validation
  728. if net.ParseIP(params.ServerIPAddress) == nil {
  729. return nil, nil, nil, nil, nil, errors.TraceNew("invalid IP address")
  730. }
  731. if len(params.TunnelProtocolPorts) == 0 {
  732. return nil, nil, nil, nil, nil, errors.TraceNew("no tunnel protocols")
  733. }
  734. usedPort := make(map[int]bool)
  735. usingMeek := false
  736. usingTLSOSSH := false
  737. usingInproxy := false
  738. for tunnelProtocol, port := range params.TunnelProtocolPorts {
  739. if !common.Contains(protocol.SupportedTunnelProtocols, tunnelProtocol) {
  740. return nil, nil, nil, nil, nil, errors.TraceNew("invalid tunnel protocol")
  741. }
  742. if usedPort[port] {
  743. return nil, nil, nil, nil, nil, errors.TraceNew("duplicate listening port")
  744. }
  745. usedPort[port] = true
  746. if protocol.TunnelProtocolUsesTLSOSSH(tunnelProtocol) {
  747. usingTLSOSSH = true
  748. }
  749. if protocol.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  750. protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  751. usingMeek = true
  752. }
  753. if protocol.TunnelProtocolUsesInproxy(tunnelProtocol) {
  754. usingInproxy = true
  755. }
  756. }
  757. // One test mode populates the tactics config file; this will generate
  758. // keys. Another test mode passes in existing keys to be used in the
  759. // server entry. Both the filename and existing keys cannot be passed in.
  760. if (params.TacticsConfigFilename != "") &&
  761. (params.TacticsRequestPublicKey != "" || params.TacticsRequestObfuscatedKey != "") {
  762. return nil, nil, nil, nil, nil, errors.TraceNew("invalid tactics parameters")
  763. }
  764. // SSH config
  765. rsaKey, err := rsa.GenerateKey(rand.Reader, SSH_RSA_HOST_KEY_BITS)
  766. if err != nil {
  767. return nil, nil, nil, nil, nil, errors.Trace(err)
  768. }
  769. sshPrivateKey := pem.EncodeToMemory(
  770. &pem.Block{
  771. Type: "RSA PRIVATE KEY",
  772. Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
  773. },
  774. )
  775. signer, err := ssh.NewSignerFromKey(rsaKey)
  776. if err != nil {
  777. return nil, nil, nil, nil, nil, errors.Trace(err)
  778. }
  779. sshPublicKey := signer.PublicKey()
  780. sshUserNameSuffixBytes, err := common.MakeSecureRandomBytes(SSH_USERNAME_SUFFIX_BYTE_LENGTH)
  781. if err != nil {
  782. return nil, nil, nil, nil, nil, errors.Trace(err)
  783. }
  784. sshUserNameSuffix := hex.EncodeToString(sshUserNameSuffixBytes)
  785. sshUserName := "psiphon_" + sshUserNameSuffix
  786. sshPasswordBytes, err := common.MakeSecureRandomBytes(SSH_PASSWORD_BYTE_LENGTH)
  787. if err != nil {
  788. return nil, nil, nil, nil, nil, errors.Trace(err)
  789. }
  790. sshPassword := hex.EncodeToString(sshPasswordBytes)
  791. sshServerVersion := "SSH-2.0-Psiphon"
  792. // Obfuscated SSH config
  793. obfuscatedSSHKeyBytes, err := common.MakeSecureRandomBytes(SSH_OBFUSCATED_KEY_BYTE_LENGTH)
  794. if err != nil {
  795. return nil, nil, nil, nil, nil, errors.Trace(err)
  796. }
  797. obfuscatedSSHKey := hex.EncodeToString(obfuscatedSSHKeyBytes)
  798. // Meek config
  799. var meekCookieEncryptionPublicKey, meekCookieEncryptionPrivateKey, meekObfuscatedKey string
  800. if usingMeek {
  801. rawMeekCookieEncryptionPublicKey, rawMeekCookieEncryptionPrivateKey, err :=
  802. box.GenerateKey(rand.Reader)
  803. if err != nil {
  804. return nil, nil, nil, nil, nil, errors.Trace(err)
  805. }
  806. meekCookieEncryptionPublicKey = base64.StdEncoding.EncodeToString(rawMeekCookieEncryptionPublicKey[:])
  807. meekCookieEncryptionPrivateKey = base64.StdEncoding.EncodeToString(rawMeekCookieEncryptionPrivateKey[:])
  808. }
  809. if usingMeek || usingTLSOSSH {
  810. meekObfuscatedKeyBytes, err := common.MakeSecureRandomBytes(SSH_OBFUSCATED_KEY_BYTE_LENGTH)
  811. if err != nil {
  812. return nil, nil, nil, nil, nil, errors.Trace(err)
  813. }
  814. meekObfuscatedKey = hex.EncodeToString(meekObfuscatedKeyBytes)
  815. }
  816. // Inproxy config
  817. var inproxyServerSessionPublicKey,
  818. inproxyServerSessionPrivateKey,
  819. inproxyServerObfuscationRootSecret string
  820. if usingInproxy {
  821. privateKey, err := inproxy.GenerateSessionPrivateKey()
  822. if err != nil {
  823. return nil, nil, nil, nil, nil, errors.Trace(err)
  824. }
  825. inproxyServerSessionPrivateKey = privateKey.String()
  826. publicKey, err := privateKey.GetPublicKey()
  827. if err != nil {
  828. return nil, nil, nil, nil, nil, errors.Trace(err)
  829. }
  830. inproxyServerSessionPublicKey = publicKey.String()
  831. obfuscationRootSecret, err := inproxy.GenerateRootObfuscationSecret()
  832. if err != nil {
  833. return nil, nil, nil, nil, nil, errors.Trace(err)
  834. }
  835. inproxyServerObfuscationRootSecret = obfuscationRootSecret.String()
  836. }
  837. // Other config
  838. discoveryValueHMACKeyBytes, err := common.MakeSecureRandomBytes(DISCOVERY_VALUE_KEY_BYTE_LENGTH)
  839. if err != nil {
  840. return nil, nil, nil, nil, nil, errors.Trace(err)
  841. }
  842. discoveryValueHMACKey := base64.StdEncoding.EncodeToString(discoveryValueHMACKeyBytes)
  843. // Generate a legacy web server secret, to accomodate test cases, such as deriving
  844. // a server entry tag when no tag is present.
  845. webServerSecretBytes, err := common.MakeSecureRandomBytes(WEB_SERVER_SECRET_BYTE_LENGTH)
  846. if err != nil {
  847. return nil, nil, nil, nil, nil, errors.Trace(err)
  848. }
  849. webServerSecret := hex.EncodeToString(webServerSecretBytes)
  850. // Assemble configs and server entry
  851. // Note: this config is intended for either testing or as an illustrative
  852. // example or template and is not intended for production deployment.
  853. logLevel := params.LogLevel
  854. if logLevel == "" {
  855. logLevel = "info"
  856. }
  857. // For testing, set the Psiphon server to create its log files; we do not
  858. // expect tests to necessarily run under log managers, such as logrotate.
  859. createMode := 0666
  860. config := &Config{
  861. LogLevel: logLevel,
  862. LogFilename: params.LogFilename,
  863. LogFileCreateMode: &createMode,
  864. SkipPanickingLogWriter: params.SkipPanickingLogWriter,
  865. GeoIPDatabaseFilenames: nil,
  866. HostID: "example-host-id",
  867. ServerIPAddress: params.ServerIPAddress,
  868. DiscoveryValueHMACKey: discoveryValueHMACKey,
  869. SSHPrivateKey: string(sshPrivateKey),
  870. SSHServerVersion: sshServerVersion,
  871. SSHUserName: sshUserName,
  872. SSHPassword: sshPassword,
  873. ObfuscatedSSHKey: obfuscatedSSHKey,
  874. TunnelProtocolPorts: params.TunnelProtocolPorts,
  875. TunnelProtocolPassthroughAddresses: params.TunnelProtocolPassthroughAddresses,
  876. DNSResolverIPAddress: "8.8.8.8",
  877. UDPInterceptUdpgwServerAddress: "127.0.0.1:7300",
  878. MeekCookieEncryptionPrivateKey: meekCookieEncryptionPrivateKey,
  879. MeekObfuscatedKey: meekObfuscatedKey,
  880. MeekProhibitedHeaders: nil,
  881. MeekProxyForwardedForHeaders: []string{"X-Forwarded-For"},
  882. LoadMonitorPeriodSeconds: 300,
  883. TrafficRulesFilename: params.TrafficRulesConfigFilename,
  884. OSLConfigFilename: params.OSLConfigFilename,
  885. TacticsConfigFilename: params.TacticsConfigFilename,
  886. LegacyPassthrough: params.LegacyPassthrough,
  887. EnableGQUIC: params.EnableGQUIC,
  888. InproxyServerSessionPrivateKey: inproxyServerSessionPrivateKey,
  889. InproxyServerObfuscationRootSecret: inproxyServerObfuscationRootSecret,
  890. }
  891. encodedConfig, err := json.MarshalIndent(config, "\n", " ")
  892. if err != nil {
  893. return nil, nil, nil, nil, nil, errors.Trace(err)
  894. }
  895. intPtr := func(i int) *int {
  896. return &i
  897. }
  898. trafficRulesSet := &TrafficRulesSet{
  899. DefaultRules: TrafficRules{
  900. RateLimits: RateLimits{
  901. ReadUnthrottledBytes: new(int64),
  902. ReadBytesPerSecond: new(int64),
  903. WriteUnthrottledBytes: new(int64),
  904. WriteBytesPerSecond: new(int64),
  905. },
  906. IdleTCPPortForwardTimeoutMilliseconds: intPtr(DEFAULT_IDLE_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS),
  907. IdleUDPPortForwardTimeoutMilliseconds: intPtr(DEFAULT_IDLE_UDP_PORT_FORWARD_TIMEOUT_MILLISECONDS),
  908. MaxTCPPortForwardCount: intPtr(DEFAULT_MAX_TCP_PORT_FORWARD_COUNT),
  909. MaxUDPPortForwardCount: intPtr(DEFAULT_MAX_UDP_PORT_FORWARD_COUNT),
  910. AllowTCPPorts: nil,
  911. AllowUDPPorts: nil,
  912. },
  913. }
  914. encodedTrafficRulesSet, err := json.MarshalIndent(trafficRulesSet, "\n", " ")
  915. if err != nil {
  916. return nil, nil, nil, nil, nil, errors.Trace(err)
  917. }
  918. encodedOSLConfig, err := json.MarshalIndent(&osl.Config{}, "\n", " ")
  919. if err != nil {
  920. return nil, nil, nil, nil, nil, errors.Trace(err)
  921. }
  922. tacticsRequestPublicKey := params.TacticsRequestPublicKey
  923. tacticsRequestObfuscatedKey := params.TacticsRequestObfuscatedKey
  924. var tacticsRequestPrivateKey string
  925. var encodedTacticsConfig []byte
  926. if params.TacticsConfigFilename != "" {
  927. tacticsRequestPublicKey, tacticsRequestPrivateKey, tacticsRequestObfuscatedKey, err =
  928. tactics.GenerateKeys()
  929. if err != nil {
  930. return nil, nil, nil, nil, nil, errors.Trace(err)
  931. }
  932. decodedTacticsRequestPublicKey, err := base64.StdEncoding.DecodeString(tacticsRequestPublicKey)
  933. if err != nil {
  934. return nil, nil, nil, nil, nil, errors.Trace(err)
  935. }
  936. decodedTacticsRequestPrivateKey, err := base64.StdEncoding.DecodeString(tacticsRequestPrivateKey)
  937. if err != nil {
  938. return nil, nil, nil, nil, nil, errors.Trace(err)
  939. }
  940. decodedTacticsRequestObfuscatedKey, err := base64.StdEncoding.DecodeString(tacticsRequestObfuscatedKey)
  941. if err != nil {
  942. return nil, nil, nil, nil, nil, errors.Trace(err)
  943. }
  944. tacticsConfig := &tactics.Server{
  945. RequestPublicKey: decodedTacticsRequestPublicKey,
  946. RequestPrivateKey: decodedTacticsRequestPrivateKey,
  947. RequestObfuscatedKey: decodedTacticsRequestObfuscatedKey,
  948. DefaultTactics: tactics.Tactics{
  949. TTL: "1m",
  950. Probability: 1.0,
  951. },
  952. }
  953. encodedTacticsConfig, err = json.MarshalIndent(tacticsConfig, "\n", " ")
  954. if err != nil {
  955. return nil, nil, nil, nil, nil, errors.Trace(err)
  956. }
  957. }
  958. // Capabilities
  959. capabilities := []string{protocol.CAPABILITY_SSH_API_REQUESTS}
  960. var frontingProviderID string
  961. for tunnelProtocol := range params.TunnelProtocolPorts {
  962. capability := protocol.GetCapability(tunnelProtocol)
  963. // In-proxy tunnel protocol capabilities don't include
  964. // v1/-PASSTHROUGHv2 suffixes; see comments in ServerEntry.hasCapability.
  965. if !protocol.TunnelProtocolUsesInproxy(tunnelProtocol) {
  966. // Note: do not add passthrough annotation if HTTP unfronted meek
  967. // because it would result in an invalid capability.
  968. if params.Passthrough &&
  969. protocol.TunnelProtocolSupportsPassthrough(tunnelProtocol) &&
  970. tunnelProtocol != protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK {
  971. if !params.LegacyPassthrough {
  972. capability += "-PASSTHROUGH-v2"
  973. } else {
  974. capability += "-PASSTHROUGH"
  975. }
  976. }
  977. if tunnelProtocol == protocol.TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH &&
  978. !params.EnableGQUIC {
  979. capability += "v1"
  980. }
  981. }
  982. capabilities = append(capabilities, capability)
  983. if params.TacticsRequestPublicKey != "" && params.TacticsRequestObfuscatedKey != "" &&
  984. protocol.TunnelProtocolSupportsTactics(tunnelProtocol) {
  985. capabilities = append(capabilities, protocol.GetTacticsCapability(tunnelProtocol))
  986. }
  987. if protocol.TunnelProtocolUsesFrontedMeek(tunnelProtocol) {
  988. frontingProviderID = params.FrontingProviderID
  989. }
  990. }
  991. // Tunnel protocol ports
  992. // Limitations:
  993. // - Only one meek port may be specified per server entry.
  994. // - Neither fronted meek nor Conjuure protocols are supported here.
  995. var sshPort, obfuscatedSSHPort, meekPort, obfuscatedSSHQUICPort, tlsOSSHPort int
  996. var inproxySSHPort, inproxyOSSHPort, inproxyQUICPort, inproxyMeekPort, inproxyTlsOSSHPort int
  997. for tunnelProtocol, port := range params.TunnelProtocolPorts {
  998. if !protocol.TunnelProtocolUsesInproxy(tunnelProtocol) {
  999. switch tunnelProtocol {
  1000. case protocol.TUNNEL_PROTOCOL_TLS_OBFUSCATED_SSH:
  1001. tlsOSSHPort = port
  1002. case protocol.TUNNEL_PROTOCOL_SSH:
  1003. sshPort = port
  1004. case protocol.TUNNEL_PROTOCOL_OBFUSCATED_SSH:
  1005. obfuscatedSSHPort = port
  1006. case protocol.TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH:
  1007. obfuscatedSSHQUICPort = port
  1008. case protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
  1009. protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET,
  1010. protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK:
  1011. meekPort = port
  1012. }
  1013. } else {
  1014. switch protocol.TunnelProtocolMinusInproxy(tunnelProtocol) {
  1015. case protocol.TUNNEL_PROTOCOL_TLS_OBFUSCATED_SSH:
  1016. inproxyTlsOSSHPort = port
  1017. case protocol.TUNNEL_PROTOCOL_SSH:
  1018. inproxySSHPort = port
  1019. case protocol.TUNNEL_PROTOCOL_OBFUSCATED_SSH:
  1020. inproxyOSSHPort = port
  1021. case protocol.TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH:
  1022. inproxyQUICPort = port
  1023. case protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
  1024. protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET,
  1025. protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK:
  1026. inproxyMeekPort = port
  1027. }
  1028. }
  1029. }
  1030. // Note: fronting params are a stub; this server entry will exercise
  1031. // client and server fronting code paths, but not actually traverse
  1032. // a fronting hop.
  1033. serverEntry := &protocol.ServerEntry{
  1034. Tag: prng.Base64String(32),
  1035. IpAddress: params.ServerIPAddress,
  1036. WebServerSecret: webServerSecret,
  1037. TlsOSSHPort: tlsOSSHPort,
  1038. SshPort: sshPort,
  1039. SshUsername: sshUserName,
  1040. SshPassword: sshPassword,
  1041. SshHostKey: base64.RawStdEncoding.EncodeToString(sshPublicKey.Marshal()),
  1042. SshObfuscatedPort: obfuscatedSSHPort,
  1043. SshObfuscatedQUICPort: obfuscatedSSHQUICPort,
  1044. LimitQUICVersions: params.LimitQUICVersions,
  1045. SshObfuscatedKey: obfuscatedSSHKey,
  1046. Capabilities: capabilities,
  1047. Region: "US",
  1048. ProviderID: strings.ToUpper(prng.HexString(8)),
  1049. FrontingProviderID: frontingProviderID,
  1050. MeekServerPort: meekPort,
  1051. MeekCookieEncryptionPublicKey: meekCookieEncryptionPublicKey,
  1052. MeekObfuscatedKey: meekObfuscatedKey,
  1053. MeekFrontingHosts: []string{params.ServerIPAddress},
  1054. MeekFrontingAddresses: []string{params.ServerIPAddress},
  1055. MeekFrontingDisableSNI: false,
  1056. TacticsRequestPublicKey: tacticsRequestPublicKey,
  1057. TacticsRequestObfuscatedKey: tacticsRequestObfuscatedKey,
  1058. ConfigurationVersion: 1,
  1059. InproxySessionPublicKey: inproxyServerSessionPublicKey,
  1060. InproxySessionRootObfuscationSecret: inproxyServerObfuscationRootSecret,
  1061. InproxySSHPort: inproxySSHPort,
  1062. InproxyOSSHPort: inproxyOSSHPort,
  1063. InproxyQUICPort: inproxyQUICPort,
  1064. InproxyMeekPort: inproxyMeekPort,
  1065. InproxyTlsOSSHPort: inproxyTlsOSSHPort,
  1066. }
  1067. if params.ServerEntrySignaturePublicKey != "" {
  1068. serverEntryJSON, err := json.Marshal(serverEntry)
  1069. if err != nil {
  1070. return nil, nil, nil, nil, nil, errors.Trace(err)
  1071. }
  1072. var serverEntryFields protocol.ServerEntryFields
  1073. err = json.Unmarshal(serverEntryJSON, &serverEntryFields)
  1074. if err != nil {
  1075. return nil, nil, nil, nil, nil, errors.Trace(err)
  1076. }
  1077. err = serverEntryFields.AddSignature(
  1078. params.ServerEntrySignaturePublicKey, params.ServerEntrySignaturePrivateKey)
  1079. if err != nil {
  1080. return nil, nil, nil, nil, nil, errors.Trace(err)
  1081. }
  1082. serverEntry, err = serverEntryFields.GetServerEntry()
  1083. if err != nil {
  1084. return nil, nil, nil, nil, nil, errors.Trace(err)
  1085. }
  1086. }
  1087. encodedServerEntry, err := protocol.EncodeServerEntry(serverEntry)
  1088. if err != nil {
  1089. return nil, nil, nil, nil, nil, errors.Trace(err)
  1090. }
  1091. return encodedConfig, encodedTrafficRulesSet, encodedOSLConfig, encodedTacticsConfig, []byte(encodedServerEntry), nil
  1092. }