serverApi.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. "bytes"
  22. "encoding/base64"
  23. "encoding/hex"
  24. "encoding/json"
  25. "errors"
  26. "fmt"
  27. "io"
  28. "io/ioutil"
  29. "net"
  30. "net/http"
  31. "strconv"
  32. "sync/atomic"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/transferstats"
  34. )
  35. // ServerContext is a utility struct which holds all of the data associated
  36. // with a Psiphon server connection. In addition to the established tunnel, this
  37. // includes data associated with Psiphon API requests and a persistent http
  38. // client configured to make tunneled Psiphon API requests.
  39. type ServerContext struct {
  40. sessionId string
  41. tunnelNumber int64
  42. baseRequestUrl string
  43. psiphonHttpsClient *http.Client
  44. statsRegexps *transferstats.Regexps
  45. clientRegion string
  46. clientUpgradeVersion string
  47. serverHandshakeTimestamp string
  48. }
  49. // FrontedMeekStats holds extra stats that are only gathered for
  50. // FRONTED-MEEK-OSSH. Specifically, which fronting address was selected, what
  51. // IP address a fronting domain resolved to, and whether SNI was enabled.
  52. type FrontedMeekStats struct {
  53. frontingAddress string
  54. resolvedIPAddress string
  55. enabledSNI bool
  56. }
  57. // nextTunnelNumber is a monotonically increasing number assigned to each
  58. // successive tunnel connection. The sessionId and tunnelNumber together
  59. // form a globally unique identifier for tunnels, which is used for
  60. // stats. Note that the number is increasing but not necessarily
  61. // consecutive for each active tunnel in session.
  62. var nextTunnelNumber int64
  63. // MakeSessionId creates a new session ID. The same session ID is used across
  64. // multi-tunnel controller runs, where each tunnel has its own ServerContext
  65. // instance.
  66. // In server-side stats, we now consider a "session" to be the lifetime of the
  67. // Controller (e.g., the user's commanded start and stop) and we measure this
  68. // duration as well as the duration of each tunnel within the session.
  69. func MakeSessionId() (sessionId string, err error) {
  70. randomId, err := MakeSecureRandomBytes(PSIPHON_API_CLIENT_SESSION_ID_LENGTH)
  71. if err != nil {
  72. return "", ContextError(err)
  73. }
  74. return hex.EncodeToString(randomId), nil
  75. }
  76. // NewServerContext makes the tunnelled handshake request to the Psiphon server
  77. // and returns a ServerContext struct for use with subsequent Psiphon server API
  78. // requests (e.g., periodic connected and status requests).
  79. func NewServerContext(tunnel *Tunnel, sessionId string) (*ServerContext, error) {
  80. psiphonHttpsClient, err := makePsiphonHttpsClient(tunnel)
  81. if err != nil {
  82. return nil, ContextError(err)
  83. }
  84. serverContext := &ServerContext{
  85. sessionId: sessionId,
  86. tunnelNumber: atomic.AddInt64(&nextTunnelNumber, 1),
  87. baseRequestUrl: makeBaseRequestUrl(tunnel, "", sessionId),
  88. psiphonHttpsClient: psiphonHttpsClient,
  89. }
  90. err = serverContext.doHandshakeRequest()
  91. if err != nil {
  92. return nil, ContextError(err)
  93. }
  94. return serverContext, nil
  95. }
  96. // doHandshakeRequest performs the handshake API request. The handshake
  97. // returns upgrade info, newly discovered server entries -- which are
  98. // stored -- and sponsor info (home pages, stat regexes).
  99. func (serverContext *ServerContext) doHandshakeRequest() error {
  100. extraParams := make([]*ExtraParam, 0)
  101. serverEntryIpAddresses, err := GetServerEntryIpAddresses()
  102. if err != nil {
  103. return ContextError(err)
  104. }
  105. // Submit a list of known servers -- this will be used for
  106. // discovery statistics.
  107. for _, ipAddress := range serverEntryIpAddresses {
  108. extraParams = append(extraParams, &ExtraParam{"known_server", ipAddress})
  109. }
  110. url := buildRequestUrl(serverContext.baseRequestUrl, "handshake", extraParams...)
  111. responseBody, err := serverContext.doGetRequest(url)
  112. if err != nil {
  113. return ContextError(err)
  114. }
  115. // Skip legacy format lines and just parse the JSON config line
  116. configLinePrefix := []byte("Config: ")
  117. var configLine []byte
  118. for _, line := range bytes.Split(responseBody, []byte("\n")) {
  119. if bytes.HasPrefix(line, configLinePrefix) {
  120. configLine = line[len(configLinePrefix):]
  121. break
  122. }
  123. }
  124. if len(configLine) == 0 {
  125. return ContextError(errors.New("no config line found"))
  126. }
  127. // Note:
  128. // - 'preemptive_reconnect_lifetime_milliseconds' is currently unused
  129. // - 'ssh_session_id' is ignored; client session ID is used instead
  130. var handshakeConfig struct {
  131. Homepages []string `json:"homepages"`
  132. UpgradeClientVersion string `json:"upgrade_client_version"`
  133. PageViewRegexes []map[string]string `json:"page_view_regexes"`
  134. HttpsRequestRegexes []map[string]string `json:"https_request_regexes"`
  135. EncodedServerList []string `json:"encoded_server_list"`
  136. ClientRegion string `json:"client_region"`
  137. ServerTimestamp string `json:"server_timestamp"`
  138. }
  139. err = json.Unmarshal(configLine, &handshakeConfig)
  140. if err != nil {
  141. return ContextError(err)
  142. }
  143. serverContext.clientRegion = handshakeConfig.ClientRegion
  144. NoticeClientRegion(serverContext.clientRegion)
  145. var decodedServerEntries []*ServerEntry
  146. // Store discovered server entries
  147. // We use the server's time, as it's available here, for the server entry
  148. // timestamp since this is more reliable than the client time.
  149. for _, encodedServerEntry := range handshakeConfig.EncodedServerList {
  150. serverEntry, err := DecodeServerEntry(
  151. encodedServerEntry,
  152. TruncateTimestampToHour(handshakeConfig.ServerTimestamp),
  153. SERVER_ENTRY_SOURCE_DISCOVERY)
  154. if err != nil {
  155. return ContextError(err)
  156. }
  157. err = ValidateServerEntry(serverEntry)
  158. if err != nil {
  159. // Skip this entry and continue with the next one
  160. continue
  161. }
  162. decodedServerEntries = append(decodedServerEntries, serverEntry)
  163. }
  164. // The reason we are storing the entire array of server entries at once rather
  165. // than one at a time is that some desirable side-effects get triggered by
  166. // StoreServerEntries that don't get triggered by StoreServerEntry.
  167. err = StoreServerEntries(decodedServerEntries, true)
  168. if err != nil {
  169. return ContextError(err)
  170. }
  171. // TODO: formally communicate the sponsor and upgrade info to an
  172. // outer client via some control interface.
  173. for _, homepage := range handshakeConfig.Homepages {
  174. NoticeHomepage(homepage)
  175. }
  176. serverContext.clientUpgradeVersion = handshakeConfig.UpgradeClientVersion
  177. if handshakeConfig.UpgradeClientVersion != "" {
  178. NoticeClientUpgradeAvailable(handshakeConfig.UpgradeClientVersion)
  179. }
  180. var regexpsNotices []string
  181. serverContext.statsRegexps, regexpsNotices = transferstats.MakeRegexps(
  182. handshakeConfig.PageViewRegexes,
  183. handshakeConfig.HttpsRequestRegexes)
  184. for _, notice := range regexpsNotices {
  185. NoticeAlert(notice)
  186. }
  187. serverContext.serverHandshakeTimestamp = handshakeConfig.ServerTimestamp
  188. return nil
  189. }
  190. // DoConnectedRequest performs the connected API request. This request is
  191. // used for statistics. The server returns a last_connected token for
  192. // the client to store and send next time it connects. This token is
  193. // a timestamp (using the server clock, and should be rounded to the
  194. // nearest hour) which is used to determine when a connection represents
  195. // a unique user for a time period.
  196. func (serverContext *ServerContext) DoConnectedRequest() error {
  197. const DATA_STORE_LAST_CONNECTED_KEY = "lastConnected"
  198. lastConnected, err := GetKeyValue(DATA_STORE_LAST_CONNECTED_KEY)
  199. if err != nil {
  200. return ContextError(err)
  201. }
  202. if lastConnected == "" {
  203. lastConnected = "None"
  204. }
  205. url := buildRequestUrl(
  206. serverContext.baseRequestUrl,
  207. "connected",
  208. &ExtraParam{"session_id", serverContext.sessionId},
  209. &ExtraParam{"last_connected", lastConnected})
  210. responseBody, err := serverContext.doGetRequest(url)
  211. if err != nil {
  212. return ContextError(err)
  213. }
  214. var response struct {
  215. ConnectedTimestamp string `json:"connected_timestamp"`
  216. }
  217. err = json.Unmarshal(responseBody, &response)
  218. if err != nil {
  219. return ContextError(err)
  220. }
  221. err = SetKeyValue(DATA_STORE_LAST_CONNECTED_KEY, response.ConnectedTimestamp)
  222. if err != nil {
  223. return ContextError(err)
  224. }
  225. return nil
  226. }
  227. // StatsRegexps gets the Regexps used for the statistics for this tunnel.
  228. func (serverContext *ServerContext) StatsRegexps() *transferstats.Regexps {
  229. return serverContext.statsRegexps
  230. }
  231. // DoStatusRequest makes a /status request to the server, sending session stats.
  232. func (serverContext *ServerContext) DoStatusRequest(tunnel *Tunnel) error {
  233. url := makeStatusRequestUrl(serverContext.sessionId, serverContext.baseRequestUrl, true)
  234. payload, payloadInfo, err := makeStatusRequestPayload(tunnel.serverEntry.IpAddress)
  235. if err != nil {
  236. return ContextError(err)
  237. }
  238. err = serverContext.doPostRequest(url, "application/json", bytes.NewReader(payload))
  239. if err != nil {
  240. // Resend the transfer stats and tunnel stats later
  241. // Note: potential duplicate reports if the server received and processed
  242. // the request but the client failed to receive the response.
  243. putBackStatusRequestPayload(payloadInfo)
  244. return ContextError(err)
  245. }
  246. confirmStatusRequestPayload(payloadInfo)
  247. return nil
  248. }
  249. func makeStatusRequestUrl(sessionId, baseRequestUrl string, isTunneled bool) string {
  250. // Add a random amount of padding to help prevent stats updates from being
  251. // a predictable size (which often happens when the connection is quiet).
  252. padding := MakeSecureRandomPadding(0, PSIPHON_API_STATUS_REQUEST_PADDING_MAX_BYTES)
  253. // Legacy clients set "connected" to "0" when disconnecting, and this value
  254. // is used to calculate session duration estimates. This is now superseded
  255. // by explicit tunnel stats duration reporting.
  256. // The legacy method of reconstructing session durations is not compatible
  257. // with this client's connected request retries and asynchronous final
  258. // status request attempts. So we simply set this "connected" flag to reflect
  259. // whether the request is sent tunneled or not.
  260. connected := "1"
  261. if !isTunneled {
  262. connected = "0"
  263. }
  264. return buildRequestUrl(
  265. baseRequestUrl,
  266. "status",
  267. &ExtraParam{"session_id", sessionId},
  268. &ExtraParam{"connected", connected},
  269. // TODO: base64 encoding of padding means the padding
  270. // size is not exactly [0, PADDING_MAX_BYTES]
  271. &ExtraParam{"padding", base64.StdEncoding.EncodeToString(padding)})
  272. }
  273. // statusRequestPayloadInfo is a temporary structure for data used to
  274. // either "clear" or "put back" status request payload data depending
  275. // on whether or not the request succeeded.
  276. type statusRequestPayloadInfo struct {
  277. serverId string
  278. transferStats *transferstats.AccumulatedStats
  279. tunnelStats [][]byte
  280. }
  281. func makeStatusRequestPayload(
  282. serverId string) ([]byte, *statusRequestPayloadInfo, error) {
  283. transferStats := transferstats.TakeOutStatsForServer(serverId)
  284. tunnelStats, err := TakeOutUnreportedTunnelStats(
  285. PSIPHON_API_TUNNEL_STATS_MAX_COUNT)
  286. if err != nil {
  287. NoticeAlert(
  288. "TakeOutUnreportedTunnelStats failed: %s", ContextError(err))
  289. tunnelStats = nil
  290. // Proceed with transferStats only
  291. }
  292. payloadInfo := &statusRequestPayloadInfo{
  293. serverId, transferStats, tunnelStats}
  294. payload := make(map[string]interface{})
  295. hostBytes, bytesTransferred := transferStats.GetStatsForStatusRequest()
  296. payload["host_bytes"] = hostBytes
  297. payload["bytes_transferred"] = bytesTransferred
  298. // We're not recording these fields, but the server requires them.
  299. payload["page_views"] = make([]string, 0)
  300. payload["https_requests"] = make([]string, 0)
  301. // Tunnel stats records are already in JSON format
  302. jsonTunnelStats := make([]json.RawMessage, len(tunnelStats))
  303. for i, tunnelStatsRecord := range tunnelStats {
  304. jsonTunnelStats[i] = json.RawMessage(tunnelStatsRecord)
  305. }
  306. payload["tunnel_stats"] = jsonTunnelStats
  307. jsonPayload, err := json.Marshal(payload)
  308. if err != nil {
  309. // Send the transfer stats and tunnel stats later
  310. putBackStatusRequestPayload(payloadInfo)
  311. return nil, nil, ContextError(err)
  312. }
  313. return jsonPayload, payloadInfo, nil
  314. }
  315. func putBackStatusRequestPayload(payloadInfo *statusRequestPayloadInfo) {
  316. transferstats.PutBackStatsForServer(
  317. payloadInfo.serverId, payloadInfo.transferStats)
  318. err := PutBackUnreportedTunnelStats(payloadInfo.tunnelStats)
  319. if err != nil {
  320. // These tunnel stats records won't be resent under after a
  321. // datastore re-initialization.
  322. NoticeAlert(
  323. "PutBackUnreportedTunnelStats failed: %s", ContextError(err))
  324. }
  325. }
  326. func confirmStatusRequestPayload(payloadInfo *statusRequestPayloadInfo) {
  327. err := ClearReportedTunnelStats(payloadInfo.tunnelStats)
  328. if err != nil {
  329. // These tunnel stats records may be resent.
  330. NoticeAlert(
  331. "ClearReportedTunnelStats failed: %s", ContextError(err))
  332. }
  333. }
  334. // TryUntunneledStatusRequest makes direct connections to the specified
  335. // server (if supported) in an attempt to send useful bytes transferred
  336. // and tunnel duration stats after a tunnel has alreay failed.
  337. // The tunnel is assumed to be closed, but its config, protocol, and
  338. // context values must still be valid.
  339. // TryUntunneledStatusRequest emits notices detailing failed attempts.
  340. func TryUntunneledStatusRequest(tunnel *Tunnel, isShutdown bool) error {
  341. for _, port := range tunnel.serverEntry.GetDirectWebRequestPorts() {
  342. err := doUntunneledStatusRequest(tunnel, port, isShutdown)
  343. if err == nil {
  344. return nil
  345. }
  346. NoticeAlert("doUntunneledStatusRequest failed for %s:%s: %s",
  347. tunnel.serverEntry.IpAddress, port, err)
  348. }
  349. return errors.New("all attempts failed")
  350. }
  351. // doUntunneledStatusRequest attempts an untunneled stratus request.
  352. func doUntunneledStatusRequest(
  353. tunnel *Tunnel, port string, isShutdown bool) error {
  354. url := makeStatusRequestUrl(
  355. tunnel.serverContext.sessionId,
  356. makeBaseRequestUrl(tunnel, port, tunnel.serverContext.sessionId),
  357. false)
  358. certificate, err := DecodeCertificate(tunnel.serverEntry.WebServerCertificate)
  359. if err != nil {
  360. return ContextError(err)
  361. }
  362. timeout := PSIPHON_API_SERVER_TIMEOUT
  363. if isShutdown {
  364. timeout = PSIPHON_API_SHUTDOWN_SERVER_TIMEOUT
  365. }
  366. httpClient, requestUrl, err := MakeUntunneledHttpsClient(
  367. tunnel.untunneledDialConfig,
  368. certificate,
  369. url,
  370. timeout)
  371. if err != nil {
  372. return ContextError(err)
  373. }
  374. payload, payloadInfo, err := makeStatusRequestPayload(tunnel.serverEntry.IpAddress)
  375. if err != nil {
  376. return ContextError(err)
  377. }
  378. bodyType := "application/json"
  379. body := bytes.NewReader(payload)
  380. response, err := httpClient.Post(requestUrl, bodyType, body)
  381. if err == nil && response.StatusCode != http.StatusOK {
  382. response.Body.Close()
  383. err = fmt.Errorf("HTTP POST request failed with response code: %d", response.StatusCode)
  384. }
  385. if err != nil {
  386. // Resend the transfer stats and tunnel stats later
  387. // Note: potential duplicate reports if the server received and processed
  388. // the request but the client failed to receive the response.
  389. putBackStatusRequestPayload(payloadInfo)
  390. // Trim this error since it may include long URLs
  391. return ContextError(TrimError(err))
  392. }
  393. confirmStatusRequestPayload(payloadInfo)
  394. response.Body.Close()
  395. return nil
  396. }
  397. // RecordTunnelStats records a tunnel duration and bytes
  398. // sent and received for subsequent reporting and quality
  399. // analysis.
  400. //
  401. // Tunnel durations are precisely measured client-side
  402. // and reported in status requests. As the duration is
  403. // not determined until the tunnel is closed, tunnel
  404. // stats records are stored in the persistent datastore
  405. // and reported via subsequent status requests sent to any
  406. // Psiphon server.
  407. //
  408. // Since the status request that reports a tunnel stats
  409. // record is not necessarily handled by the same server, the
  410. // tunnel stats records include the original server ID.
  411. //
  412. // Other fields that may change between tunnel stats recording
  413. // and reporting include client geo data, propagation channel,
  414. // sponsor ID, client version. These are not stored in the
  415. // datastore (client region, in particular, since that would
  416. // create an on-disk record of user location).
  417. // TODO: the server could encrypt, with a nonce and key unknown to
  418. // the client, a blob containing this data; return it in the
  419. // handshake response; and the client could store and later report
  420. // this blob with its tunnel stats records.
  421. //
  422. // Multiple "status" requests may be in flight at once (due
  423. // to multi-tunnel, asynchronous final status retry, and
  424. // aggressive status requests for pre-registered tunnels),
  425. // To avoid duplicate reporting, tunnel stats records are
  426. // "taken-out" by a status request and then "put back" in
  427. // case the request fails.
  428. //
  429. // Note: since tunnel stats records have a globally unique
  430. // identifier (sessionId + tunnelNumber), we could tolerate
  431. // duplicate reporting and filter our duplicates on the
  432. // server-side. Permitting duplicate reporting could increase
  433. // the velocity of reporting (for example, both the asynchronous
  434. // untunneled final status requests and the post-connected
  435. // immediate startus requests could try to report the same tunnel
  436. // stats).
  437. // Duplicate reporting may also occur when a server receives and
  438. // processes a status request but the client fails to receive
  439. // the response.
  440. func RecordTunnelStats(
  441. sessionId string,
  442. tunnelNumber int64,
  443. tunnelServerIpAddress string,
  444. serverHandshakeTimestamp, duration string,
  445. totalBytesSent, totalBytesReceived int64) error {
  446. tunnelStats := struct {
  447. SessionId string `json:"session_id"`
  448. TunnelNumber int64 `json:"tunnel_number"`
  449. TunnelServerIpAddress string `json:"tunnel_server_ip_address"`
  450. ServerHandshakeTimestamp string `json:"server_handshake_timestamp"`
  451. Duration string `json:"duration"`
  452. TotalBytesSent int64 `json:"total_bytes_sent"`
  453. TotalBytesReceived int64 `json:"total_bytes_received"`
  454. }{
  455. sessionId,
  456. tunnelNumber,
  457. tunnelServerIpAddress,
  458. serverHandshakeTimestamp,
  459. duration,
  460. totalBytesSent,
  461. totalBytesReceived,
  462. }
  463. tunnelStatsJson, err := json.Marshal(tunnelStats)
  464. if err != nil {
  465. return ContextError(err)
  466. }
  467. return StoreTunnelStats(tunnelStatsJson)
  468. }
  469. // doGetRequest makes a tunneled HTTPS request and returns the response body.
  470. func (serverContext *ServerContext) doGetRequest(
  471. requestUrl string) (responseBody []byte, err error) {
  472. response, err := serverContext.psiphonHttpsClient.Get(requestUrl)
  473. if err == nil && response.StatusCode != http.StatusOK {
  474. response.Body.Close()
  475. err = fmt.Errorf("HTTP GET request failed with response code: %d", response.StatusCode)
  476. }
  477. if err != nil {
  478. // Trim this error since it may include long URLs
  479. return nil, ContextError(TrimError(err))
  480. }
  481. defer response.Body.Close()
  482. body, err := ioutil.ReadAll(response.Body)
  483. if err != nil {
  484. return nil, ContextError(err)
  485. }
  486. return body, nil
  487. }
  488. // doPostRequest makes a tunneled HTTPS POST request.
  489. func (serverContext *ServerContext) doPostRequest(
  490. requestUrl string, bodyType string, body io.Reader) (err error) {
  491. response, err := serverContext.psiphonHttpsClient.Post(requestUrl, bodyType, body)
  492. if err == nil && response.StatusCode != http.StatusOK {
  493. response.Body.Close()
  494. err = fmt.Errorf("HTTP POST request failed with response code: %d", response.StatusCode)
  495. }
  496. if err != nil {
  497. // Trim this error since it may include long URLs
  498. return ContextError(TrimError(err))
  499. }
  500. response.Body.Close()
  501. return nil
  502. }
  503. // makeBaseRequestUrl makes a URL containing all the common parameters
  504. // that are included with Psiphon API requests. These common parameters
  505. // are used for statistics.
  506. func makeBaseRequestUrl(tunnel *Tunnel, port, sessionId string) string {
  507. var requestUrl bytes.Buffer
  508. if port == "" {
  509. port = tunnel.serverEntry.WebServerPort
  510. }
  511. // Note: don't prefix with HTTPS scheme, see comment in doGetRequest.
  512. // e.g., don't do this: requestUrl.WriteString("https://")
  513. requestUrl.WriteString("http://")
  514. requestUrl.WriteString(tunnel.serverEntry.IpAddress)
  515. requestUrl.WriteString(":")
  516. requestUrl.WriteString(port)
  517. requestUrl.WriteString("/")
  518. // Placeholder for the path component of a request
  519. requestUrl.WriteString("%s")
  520. requestUrl.WriteString("?client_session_id=")
  521. requestUrl.WriteString(sessionId)
  522. requestUrl.WriteString("&server_secret=")
  523. requestUrl.WriteString(tunnel.serverEntry.WebServerSecret)
  524. requestUrl.WriteString("&propagation_channel_id=")
  525. requestUrl.WriteString(tunnel.config.PropagationChannelId)
  526. requestUrl.WriteString("&sponsor_id=")
  527. requestUrl.WriteString(tunnel.config.SponsorId)
  528. requestUrl.WriteString("&client_version=")
  529. requestUrl.WriteString(tunnel.config.ClientVersion)
  530. // TODO: client_tunnel_core_version
  531. requestUrl.WriteString("&relay_protocol=")
  532. requestUrl.WriteString(tunnel.protocol)
  533. requestUrl.WriteString("&client_platform=")
  534. requestUrl.WriteString(tunnel.config.ClientPlatform)
  535. requestUrl.WriteString("&tunnel_whole_device=")
  536. requestUrl.WriteString(strconv.Itoa(tunnel.config.TunnelWholeDevice))
  537. // The following parameters may be blank and must
  538. // not be sent to the server if blank.
  539. if tunnel.config.DeviceRegion != "" {
  540. requestUrl.WriteString("&device_region=")
  541. requestUrl.WriteString(tunnel.config.DeviceRegion)
  542. }
  543. if tunnel.frontedMeekStats != nil {
  544. requestUrl.WriteString("&fronting_address=")
  545. requestUrl.WriteString(tunnel.frontedMeekStats.frontingAddress)
  546. requestUrl.WriteString("&fronting_resolved_ip_address=")
  547. requestUrl.WriteString(tunnel.frontedMeekStats.resolvedIPAddress)
  548. requestUrl.WriteString("&fronting_enabled_sni=")
  549. if tunnel.frontedMeekStats.enabledSNI {
  550. requestUrl.WriteString("1")
  551. } else {
  552. requestUrl.WriteString("0")
  553. }
  554. }
  555. if tunnel.serverEntry.Region != "" {
  556. requestUrl.WriteString("&server_entry_region=")
  557. requestUrl.WriteString(tunnel.serverEntry.Region)
  558. }
  559. if tunnel.serverEntry.LocalSource != "" {
  560. requestUrl.WriteString("&server_entry_source=")
  561. requestUrl.WriteString(tunnel.serverEntry.LocalSource)
  562. }
  563. // As with last_connected, this timestamp stat, which may be
  564. // a precise handshake request server timestamp, is truncated
  565. // to hour granularity to avoid introducing a reconstructable
  566. // cross-session user trace into server logs.
  567. localServerEntryTimestamp := TruncateTimestampToHour(tunnel.serverEntry.LocalTimestamp)
  568. if localServerEntryTimestamp != "" {
  569. requestUrl.WriteString("&server_entry_timestamp=")
  570. requestUrl.WriteString(localServerEntryTimestamp)
  571. }
  572. return requestUrl.String()
  573. }
  574. type ExtraParam struct{ name, value string }
  575. // buildRequestUrl makes a URL for an API request. The URL includes the
  576. // base request URL and any extra parameters for the specific request.
  577. func buildRequestUrl(baseRequestUrl, path string, extraParams ...*ExtraParam) string {
  578. var requestUrl bytes.Buffer
  579. requestUrl.WriteString(fmt.Sprintf(baseRequestUrl, path))
  580. for _, extraParam := range extraParams {
  581. requestUrl.WriteString("&")
  582. requestUrl.WriteString(extraParam.name)
  583. requestUrl.WriteString("=")
  584. requestUrl.WriteString(extraParam.value)
  585. }
  586. return requestUrl.String()
  587. }
  588. // makePsiphonHttpsClient creates a Psiphon HTTPS client that tunnels requests and which validates
  589. // the web server using the Psiphon server entry web server certificate.
  590. // This is not a general purpose HTTPS client.
  591. // As the custom dialer makes an explicit TLS connection, URLs submitted to the returned
  592. // http.Client should use the "http://" scheme. Otherwise http.Transport will try to do another TLS
  593. // handshake inside the explicit TLS session.
  594. func makePsiphonHttpsClient(tunnel *Tunnel) (httpsClient *http.Client, err error) {
  595. certificate, err := DecodeCertificate(tunnel.serverEntry.WebServerCertificate)
  596. if err != nil {
  597. return nil, ContextError(err)
  598. }
  599. tunneledDialer := func(_, addr string) (conn net.Conn, err error) {
  600. // TODO: check tunnel.isClosed, and apply TUNNEL_PORT_FORWARD_DIAL_TIMEOUT as in Tunnel.Dial?
  601. return tunnel.sshClient.Dial("tcp", addr)
  602. }
  603. dialer := NewCustomTLSDialer(
  604. &CustomTLSConfig{
  605. Dial: tunneledDialer,
  606. Timeout: PSIPHON_API_SERVER_TIMEOUT,
  607. VerifyLegacyCertificate: certificate,
  608. })
  609. transport := &http.Transport{
  610. Dial: dialer,
  611. ResponseHeaderTimeout: PSIPHON_API_SERVER_TIMEOUT,
  612. }
  613. return &http.Client{
  614. Transport: transport,
  615. Timeout: PSIPHON_API_SERVER_TIMEOUT,
  616. }, nil
  617. }