config.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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/json"
  26. "encoding/pem"
  27. "errors"
  28. "fmt"
  29. "net"
  30. "strconv"
  31. "strings"
  32. "github.com/Psiphon-Inc/crypto/nacl/box"
  33. "github.com/Psiphon-Inc/crypto/ssh"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  36. )
  37. const (
  38. SERVER_CONFIG_FILENAME = "psiphond.config"
  39. SERVER_TRAFFIC_RULES_FILENAME = "psiphond-traffic-rules.config"
  40. SERVER_ENTRY_FILENAME = "server-entry.dat"
  41. DEFAULT_SERVER_IP_ADDRESS = "127.0.0.1"
  42. WEB_SERVER_SECRET_BYTE_LENGTH = 32
  43. DISCOVERY_VALUE_KEY_BYTE_LENGTH = 32
  44. SSH_USERNAME_SUFFIX_BYTE_LENGTH = 8
  45. SSH_PASSWORD_BYTE_LENGTH = 32
  46. SSH_RSA_HOST_KEY_BITS = 2048
  47. SSH_OBFUSCATED_KEY_BYTE_LENGTH = 32
  48. )
  49. // Config specifies the configuration and behavior of a Psiphon
  50. // server.
  51. type Config struct {
  52. // LogLevel specifies the log level. Valid values are:
  53. // panic, fatal, error, warn, info, debug
  54. LogLevel string
  55. // LogFilename specifies the path of the file to log
  56. // to. When blank, logs are written to stderr.
  57. LogFilename string
  58. // DiscoveryValueHMACKey is the network-wide secret value
  59. // used to determine a unique discovery strategy.
  60. DiscoveryValueHMACKey string
  61. // GeoIPDatabaseFilenames are paths of GeoIP2/GeoLite2
  62. // MaxMind database files. When empty, no GeoIP lookups are
  63. // performed. Each file is queried, in order, for the
  64. // logged fields: country code, city, and ISP. Multiple
  65. // file support accomodates the MaxMind distribution where
  66. // ISP data in a separate file.
  67. GeoIPDatabaseFilenames []string
  68. // PsinetDatabaseFilename is the path of the Psiphon automation
  69. // jsonpickle format Psiphon API data file.
  70. PsinetDatabaseFilename string
  71. // HostID is the ID of the server host; this is used for API
  72. // event logging.
  73. HostID string
  74. // ServerIPAddress is the public IP address of the server.
  75. ServerIPAddress string
  76. // WebServerPort is the listening port of the web server.
  77. // When <= 0, no web server component is run.
  78. WebServerPort int
  79. // WebServerSecret is the unique secret value that the client
  80. // must supply to make requests to the web server.
  81. WebServerSecret string
  82. // WebServerCertificate is the certificate the client uses to
  83. // authenticate the web server.
  84. WebServerCertificate string
  85. // WebServerPrivateKey is the private key the web server uses to
  86. // authenticate itself to clients.
  87. WebServerPrivateKey string
  88. // WebServerPortForwardAddress specifies the expected network
  89. // address ("<host>:<port>") specified in a client's port forward
  90. // HostToConnect and PortToConnect when the client is making a
  91. // tunneled connection to the web server. This address is always
  92. // exempted from validation against SSH_DISALLOWED_PORT_FORWARD_HOSTS
  93. // and AllowTCPPorts.
  94. WebServerPortForwardAddress string
  95. // WebServerPortForwardRedirectAddress specifies an alternate
  96. // destination address to be substituted and dialed instead of
  97. // the original destination when the port forward destination is
  98. // WebServerPortForwardAddress.
  99. WebServerPortForwardRedirectAddress string
  100. // TunnelProtocolPorts specifies which tunnel protocols to run
  101. // and which ports to listen on for each protocol. Valid tunnel
  102. // protocols include: "SSH", "OSSH", "UNFRONTED-MEEK-OSSH",
  103. // "UNFRONTED-MEEK-HTTPS-OSSH", "FRONTED-MEEK-OSSH",
  104. // "FRONTED-MEEK-HTTP-OSSH".
  105. TunnelProtocolPorts map[string]int
  106. // SSHPrivateKey is the SSH host key. The same key is used for
  107. // all protocols, run by this server instance, which use SSH.
  108. SSHPrivateKey string
  109. // SSHServerVersion is the server version presented in the
  110. // identification string. The same value is used for all
  111. // protocols, run by this server instance, which use SSH.
  112. SSHServerVersion string
  113. // SSHUserName is the SSH user name to be presented by the
  114. // the tunnel-core client. The same value is used for all
  115. // protocols, run by this server instance, which use SSH.
  116. SSHUserName string
  117. // SSHPassword is the SSH password to be presented by the
  118. // the tunnel-core client. The same value is used for all
  119. // protocols, run by this server instance, which use SSH.
  120. SSHPassword string
  121. // ObfuscatedSSHKey is the secret key for use in the Obfuscated
  122. // SSH protocol. The same secret key is used for all protocols,
  123. // run by this server instance, which use Obfuscated SSH.
  124. ObfuscatedSSHKey string
  125. // MeekCookieEncryptionPrivateKey is the NaCl private key used
  126. // to decrypt meek cookie payload sent from clients. The same
  127. // key is used for all meek protocols run by this server instance.
  128. MeekCookieEncryptionPrivateKey string
  129. // MeekObfuscatedKey is the secret key used for obfuscating
  130. // meek cookies sent from clients. The same key is used for all
  131. // meek protocols run by this server instance.
  132. MeekObfuscatedKey string
  133. // MeekProhibitedHeaders is a list of HTTP headers to check for
  134. // in client requests. If one of these headers is found, the
  135. // request fails. This is used to defend against abuse.
  136. MeekProhibitedHeaders []string
  137. // MeekProxyForwardedForHeaders is a list of HTTP headers which
  138. // may be added by downstream HTTP proxies or CDNs in front
  139. // of clients. These headers supply the original client IP
  140. // address, which is geolocated for stats purposes. Headers
  141. // include, for example, X-Forwarded-For. The header's value
  142. // is assumed to be a comma delimted list of IP addresses where
  143. // the client IP is the first IP address in the list. Meek protocols
  144. // look for these headers and use the client IP address from
  145. // the header if any one is present and the value is a valid
  146. // IP address; otherwise the direct connection remote address is
  147. // used as the client IP.
  148. MeekProxyForwardedForHeaders []string
  149. // UDPInterceptUdpgwServerAddress specifies the network address of
  150. // a udpgw server which clients may be port forwarding to. When
  151. // specified, these TCP port forwards are intercepted and handled
  152. // directly by this server, which parses the SSH channel using the
  153. // udpgw protocol. Handling includes udpgw transparent DNS: tunneled
  154. // UDP DNS packets are rerouted to the host's DNS server.
  155. //
  156. // The intercept is applied before the port forward destination is
  157. // validated against SSH_DISALLOWED_PORT_FORWARD_HOSTS and
  158. // AllowTCPPorts. So the intercept address may be any otherwise
  159. // prohibited destination.
  160. UDPInterceptUdpgwServerAddress string
  161. // DNSResolverIPAddress specifies the IP address of a DNS server
  162. // to be used when "/etc/resolv.conf" doesn't exist or fails to
  163. // parse. When blank, "/etc/resolv.conf" must contain a usable
  164. // "nameserver" entry.
  165. DNSResolverIPAddress string
  166. // LoadMonitorPeriodSeconds indicates how frequently to log server
  167. // load information (number of connected clients per tunnel protocol,
  168. // number of running goroutines, amount of memory allocated, etc.)
  169. // The default, 0, disables load logging.
  170. LoadMonitorPeriodSeconds int
  171. // ProcessProfileOutputDirectory is the path of a directory to which
  172. // process profiles will be written when signaled with SIGUSR2. The
  173. // files are overwritten on each invocation. When set to the default
  174. // value, blank, no profiles are written on SIGUSR2. Profiles include
  175. // the default profiles here: https://golang.org/pkg/runtime/pprof/#Profile.
  176. ProcessProfileOutputDirectory string
  177. // ProcessBlockProfileDurationSeconds specifies the sample duration for
  178. // "block" profiling. For the default, 0, no "block" profile is taken.
  179. ProcessBlockProfileDurationSeconds int
  180. // ProcessCPUProfileDurationSeconds specifies the sample duration for
  181. // CPU profiling. For the default, 0, no CPU profile is taken.
  182. ProcessCPUProfileDurationSeconds int
  183. // TrafficRulesFilename is the path of a file containing a JSON-encoded
  184. // TrafficRulesSet, the traffic rules to apply to Psiphon client tunnels.
  185. TrafficRulesFilename string
  186. // OSLConfigFilename is the path of a file containing a JSON-encoded
  187. // OSL Config, the OSL schemes to apply to Psiphon client tunnels.
  188. OSLConfigFilename string
  189. }
  190. // RunWebServer indicates whether to run a web server component.
  191. func (config *Config) RunWebServer() bool {
  192. return config.WebServerPort > 0
  193. }
  194. // RunLoadMonitor indicates whether to monitor and log server load.
  195. func (config *Config) RunLoadMonitor() bool {
  196. return config.LoadMonitorPeriodSeconds > 0
  197. }
  198. // LoadConfig loads and validates a JSON encoded server config.
  199. func LoadConfig(configJSON []byte) (*Config, error) {
  200. var config Config
  201. err := json.Unmarshal(configJSON, &config)
  202. if err != nil {
  203. return nil, common.ContextError(err)
  204. }
  205. if config.ServerIPAddress == "" {
  206. return nil, errors.New("ServerIPAddress is required")
  207. }
  208. if config.WebServerPort > 0 && (config.WebServerSecret == "" || config.WebServerCertificate == "" ||
  209. config.WebServerPrivateKey == "") {
  210. return nil, errors.New(
  211. "Web server requires WebServerSecret, WebServerCertificate, WebServerPrivateKey")
  212. }
  213. if config.WebServerPortForwardAddress != "" {
  214. if err := validateNetworkAddress(config.WebServerPortForwardAddress, false); err != nil {
  215. return nil, errors.New("WebServerPortForwardAddress is invalid")
  216. }
  217. }
  218. if config.WebServerPortForwardRedirectAddress != "" {
  219. if config.WebServerPortForwardAddress == "" {
  220. return nil, errors.New(
  221. "WebServerPortForwardRedirectAddress requires WebServerPortForwardAddress")
  222. }
  223. if err := validateNetworkAddress(config.WebServerPortForwardRedirectAddress, false); err != nil {
  224. return nil, errors.New("WebServerPortForwardRedirectAddress is invalid")
  225. }
  226. }
  227. for tunnelProtocol, _ := range config.TunnelProtocolPorts {
  228. if !common.Contains(protocol.SupportedTunnelProtocols, tunnelProtocol) {
  229. return nil, fmt.Errorf("Unsupported tunnel protocol: %s", tunnelProtocol)
  230. }
  231. if protocol.TunnelProtocolUsesSSH(tunnelProtocol) ||
  232. protocol.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  233. if config.SSHPrivateKey == "" || config.SSHServerVersion == "" ||
  234. config.SSHUserName == "" || config.SSHPassword == "" {
  235. return nil, fmt.Errorf(
  236. "Tunnel protocol %s requires SSHPrivateKey, SSHServerVersion, SSHUserName, SSHPassword",
  237. tunnelProtocol)
  238. }
  239. }
  240. if protocol.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  241. if config.ObfuscatedSSHKey == "" {
  242. return nil, fmt.Errorf(
  243. "Tunnel protocol %s requires ObfuscatedSSHKey",
  244. tunnelProtocol)
  245. }
  246. }
  247. if protocol.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  248. protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  249. if config.MeekCookieEncryptionPrivateKey == "" || config.MeekObfuscatedKey == "" {
  250. return nil, fmt.Errorf(
  251. "Tunnel protocol %s requires MeekCookieEncryptionPrivateKey, MeekObfuscatedKey",
  252. tunnelProtocol)
  253. }
  254. }
  255. }
  256. if config.UDPInterceptUdpgwServerAddress != "" {
  257. if err := validateNetworkAddress(config.UDPInterceptUdpgwServerAddress, true); err != nil {
  258. return nil, fmt.Errorf("UDPInterceptUdpgwServerAddress is invalid: %s", err)
  259. }
  260. }
  261. if config.DNSResolverIPAddress != "" {
  262. if net.ParseIP(config.DNSResolverIPAddress) == nil {
  263. return nil, fmt.Errorf("DNSResolverIPAddress is invalid")
  264. }
  265. }
  266. return &config, nil
  267. }
  268. func validateNetworkAddress(address string, requireIPaddress bool) error {
  269. host, portStr, err := net.SplitHostPort(address)
  270. if err != nil {
  271. return err
  272. }
  273. if requireIPaddress && net.ParseIP(host) == nil {
  274. return errors.New("host must be an IP address")
  275. }
  276. port, err := strconv.Atoi(portStr)
  277. if err != nil {
  278. return err
  279. }
  280. if port < 0 || port > 65535 {
  281. return errors.New("invalid port")
  282. }
  283. return nil
  284. }
  285. // GenerateConfigParams specifies customizations to be applied to
  286. // a generated server config.
  287. type GenerateConfigParams struct {
  288. LogFilename string
  289. LogLevel string
  290. ServerIPAddress string
  291. WebServerPort int
  292. EnableSSHAPIRequests bool
  293. TunnelProtocolPorts map[string]int
  294. TrafficRulesFilename string
  295. }
  296. // GenerateConfig creates a new Psiphon server config. It returns JSON
  297. // encoded configs and a client-compatible "server entry" for the server. It
  298. // generates all necessary secrets and key material, which are emitted in
  299. // the config file and server entry as necessary.
  300. // GenerateConfig uses sample values for many fields. The intention is for
  301. // generated configs to be used for testing or as a template for production
  302. // setup, not to generate production-ready configurations.
  303. func GenerateConfig(params *GenerateConfigParams) ([]byte, []byte, []byte, error) {
  304. // Input validation
  305. if net.ParseIP(params.ServerIPAddress) == nil {
  306. return nil, nil, nil, common.ContextError(errors.New("invalid IP address"))
  307. }
  308. if len(params.TunnelProtocolPorts) == 0 {
  309. return nil, nil, nil, common.ContextError(errors.New("no tunnel protocols"))
  310. }
  311. usedPort := make(map[int]bool)
  312. if params.WebServerPort != 0 {
  313. usedPort[params.WebServerPort] = true
  314. }
  315. usingMeek := false
  316. for tunnelProtocol, port := range params.TunnelProtocolPorts {
  317. if !common.Contains(protocol.SupportedTunnelProtocols, tunnelProtocol) {
  318. return nil, nil, nil, common.ContextError(errors.New("invalid tunnel protocol"))
  319. }
  320. if usedPort[port] {
  321. return nil, nil, nil, common.ContextError(errors.New("duplicate listening port"))
  322. }
  323. usedPort[port] = true
  324. if protocol.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  325. protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  326. usingMeek = true
  327. }
  328. }
  329. // Web server config
  330. var webServerSecret, webServerCertificate,
  331. webServerPrivateKey, webServerPortForwardAddress string
  332. if params.WebServerPort != 0 {
  333. var err error
  334. webServerSecret, err = common.MakeRandomStringHex(WEB_SERVER_SECRET_BYTE_LENGTH)
  335. if err != nil {
  336. return nil, nil, nil, common.ContextError(err)
  337. }
  338. webServerCertificate, webServerPrivateKey, err = GenerateWebServerCertificate("")
  339. if err != nil {
  340. return nil, nil, nil, common.ContextError(err)
  341. }
  342. webServerPortForwardAddress = net.JoinHostPort(
  343. params.ServerIPAddress, strconv.Itoa(params.WebServerPort))
  344. }
  345. // SSH config
  346. // TODO: use other key types: anti-fingerprint by varying params
  347. rsaKey, err := rsa.GenerateKey(rand.Reader, SSH_RSA_HOST_KEY_BITS)
  348. if err != nil {
  349. return nil, nil, nil, common.ContextError(err)
  350. }
  351. sshPrivateKey := pem.EncodeToMemory(
  352. &pem.Block{
  353. Type: "RSA PRIVATE KEY",
  354. Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
  355. },
  356. )
  357. signer, err := ssh.NewSignerFromKey(rsaKey)
  358. if err != nil {
  359. return nil, nil, nil, common.ContextError(err)
  360. }
  361. sshPublicKey := signer.PublicKey()
  362. sshUserNameSuffix, err := common.MakeRandomStringHex(SSH_USERNAME_SUFFIX_BYTE_LENGTH)
  363. if err != nil {
  364. return nil, nil, nil, common.ContextError(err)
  365. }
  366. sshUserName := "psiphon_" + sshUserNameSuffix
  367. sshPassword, err := common.MakeRandomStringHex(SSH_PASSWORD_BYTE_LENGTH)
  368. if err != nil {
  369. return nil, nil, nil, common.ContextError(err)
  370. }
  371. // TODO: vary version string for anti-fingerprint
  372. sshServerVersion := "SSH-2.0-Psiphon"
  373. // Obfuscated SSH config
  374. obfuscatedSSHKey, err := common.MakeRandomStringHex(SSH_OBFUSCATED_KEY_BYTE_LENGTH)
  375. if err != nil {
  376. return nil, nil, nil, common.ContextError(err)
  377. }
  378. // Meek config
  379. var meekCookieEncryptionPublicKey, meekCookieEncryptionPrivateKey, meekObfuscatedKey string
  380. if usingMeek {
  381. rawMeekCookieEncryptionPublicKey, rawMeekCookieEncryptionPrivateKey, err :=
  382. box.GenerateKey(rand.Reader)
  383. if err != nil {
  384. return nil, nil, nil, common.ContextError(err)
  385. }
  386. meekCookieEncryptionPublicKey = base64.StdEncoding.EncodeToString(rawMeekCookieEncryptionPublicKey[:])
  387. meekCookieEncryptionPrivateKey = base64.StdEncoding.EncodeToString(rawMeekCookieEncryptionPrivateKey[:])
  388. meekObfuscatedKey, err = common.MakeRandomStringHex(SSH_OBFUSCATED_KEY_BYTE_LENGTH)
  389. if err != nil {
  390. return nil, nil, nil, common.ContextError(err)
  391. }
  392. }
  393. // Other config
  394. discoveryValueHMACKey, err := common.MakeRandomStringBase64(DISCOVERY_VALUE_KEY_BYTE_LENGTH)
  395. if err != nil {
  396. return nil, nil, nil, common.ContextError(err)
  397. }
  398. // Assemble configs and server entry
  399. // Note: this config is intended for either testing or as an illustrative
  400. // example or template and is not intended for production deployment.
  401. logLevel := params.LogLevel
  402. if logLevel == "" {
  403. logLevel = "info"
  404. }
  405. config := &Config{
  406. LogLevel: logLevel,
  407. LogFilename: params.LogFilename,
  408. GeoIPDatabaseFilenames: nil,
  409. HostID: "example-host-id",
  410. ServerIPAddress: params.ServerIPAddress,
  411. DiscoveryValueHMACKey: discoveryValueHMACKey,
  412. WebServerPort: params.WebServerPort,
  413. WebServerSecret: webServerSecret,
  414. WebServerCertificate: webServerCertificate,
  415. WebServerPrivateKey: webServerPrivateKey,
  416. WebServerPortForwardAddress: webServerPortForwardAddress,
  417. SSHPrivateKey: string(sshPrivateKey),
  418. SSHServerVersion: sshServerVersion,
  419. SSHUserName: sshUserName,
  420. SSHPassword: sshPassword,
  421. ObfuscatedSSHKey: obfuscatedSSHKey,
  422. TunnelProtocolPorts: params.TunnelProtocolPorts,
  423. DNSResolverIPAddress: "8.8.8.8",
  424. UDPInterceptUdpgwServerAddress: "127.0.0.1:7300",
  425. MeekCookieEncryptionPrivateKey: meekCookieEncryptionPrivateKey,
  426. MeekObfuscatedKey: meekObfuscatedKey,
  427. MeekProhibitedHeaders: nil,
  428. MeekProxyForwardedForHeaders: []string{"X-Forwarded-For"},
  429. LoadMonitorPeriodSeconds: 300,
  430. TrafficRulesFilename: params.TrafficRulesFilename,
  431. }
  432. encodedConfig, err := json.MarshalIndent(config, "\n", " ")
  433. if err != nil {
  434. return nil, nil, nil, common.ContextError(err)
  435. }
  436. intPtr := func(i int) *int {
  437. return &i
  438. }
  439. trafficRulesSet := &TrafficRulesSet{
  440. DefaultRules: TrafficRules{
  441. RateLimits: RateLimits{
  442. ReadUnthrottledBytes: new(int64),
  443. ReadBytesPerSecond: new(int64),
  444. WriteUnthrottledBytes: new(int64),
  445. WriteBytesPerSecond: new(int64),
  446. },
  447. IdleTCPPortForwardTimeoutMilliseconds: intPtr(DEFAULT_IDLE_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS),
  448. IdleUDPPortForwardTimeoutMilliseconds: intPtr(DEFAULT_IDLE_UDP_PORT_FORWARD_TIMEOUT_MILLISECONDS),
  449. MaxTCPPortForwardCount: intPtr(DEFAULT_MAX_TCP_PORT_FORWARD_COUNT),
  450. MaxUDPPortForwardCount: intPtr(DEFAULT_MAX_UDP_PORT_FORWARD_COUNT),
  451. AllowTCPPorts: nil,
  452. AllowUDPPorts: nil,
  453. },
  454. }
  455. encodedTrafficRulesSet, err := json.MarshalIndent(trafficRulesSet, "\n", " ")
  456. if err != nil {
  457. return nil, nil, nil, common.ContextError(err)
  458. }
  459. capabilities := []string{}
  460. if params.EnableSSHAPIRequests {
  461. capabilities = append(capabilities, protocol.CAPABILITY_SSH_API_REQUESTS)
  462. }
  463. if params.WebServerPort != 0 {
  464. capabilities = append(capabilities, protocol.CAPABILITY_UNTUNNELED_WEB_API_REQUESTS)
  465. }
  466. for tunnelProtocol, _ := range params.TunnelProtocolPorts {
  467. capabilities = append(capabilities, protocol.GetCapability(tunnelProtocol))
  468. }
  469. sshPort := params.TunnelProtocolPorts["SSH"]
  470. obfuscatedSSHPort := params.TunnelProtocolPorts["OSSH"]
  471. // Meek port limitations
  472. // - fronted meek protocols are hard-wired in the client to be port 443 or 80.
  473. // - only one other meek port may be specified.
  474. meekPort := params.TunnelProtocolPorts["UNFRONTED-MEEK-OSSH"]
  475. if meekPort == 0 {
  476. meekPort = params.TunnelProtocolPorts["UNFRONTED-MEEK-HTTPS-OSSH"]
  477. }
  478. if meekPort == 0 {
  479. meekPort = params.TunnelProtocolPorts["UNFRONTED-MEEK-SESSION-TICKET-OSSH"]
  480. }
  481. // Note: fronting params are a stub; this server entry will exercise
  482. // client and server fronting code paths, but not actually traverse
  483. // a fronting hop.
  484. serverEntryWebServerPort := ""
  485. strippedWebServerCertificate := ""
  486. if params.WebServerPort != 0 {
  487. serverEntryWebServerPort = fmt.Sprintf("%d", params.WebServerPort)
  488. // Server entry format omits the BEGIN/END lines and newlines
  489. lines := strings.Split(webServerCertificate, "\n")
  490. strippedWebServerCertificate = strings.Join(lines[1:len(lines)-2], "")
  491. }
  492. serverEntry := &protocol.ServerEntry{
  493. IpAddress: params.ServerIPAddress,
  494. WebServerPort: serverEntryWebServerPort,
  495. WebServerSecret: webServerSecret,
  496. WebServerCertificate: strippedWebServerCertificate,
  497. SshPort: sshPort,
  498. SshUsername: sshUserName,
  499. SshPassword: sshPassword,
  500. SshHostKey: base64.RawStdEncoding.EncodeToString(sshPublicKey.Marshal()),
  501. SshObfuscatedPort: obfuscatedSSHPort,
  502. SshObfuscatedKey: obfuscatedSSHKey,
  503. Capabilities: capabilities,
  504. Region: "US",
  505. MeekServerPort: meekPort,
  506. MeekCookieEncryptionPublicKey: meekCookieEncryptionPublicKey,
  507. MeekObfuscatedKey: meekObfuscatedKey,
  508. MeekFrontingHosts: []string{params.ServerIPAddress},
  509. MeekFrontingAddresses: []string{params.ServerIPAddress},
  510. MeekFrontingDisableSNI: false,
  511. }
  512. encodedServerEntry, err := protocol.EncodeServerEntry(serverEntry)
  513. if err != nil {
  514. return nil, nil, nil, common.ContextError(err)
  515. }
  516. return encodedConfig, encodedTrafficRulesSet, []byte(encodedServerEntry), nil
  517. }