meekConn.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. "crypto/rand"
  23. "encoding/base64"
  24. "encoding/json"
  25. "errors"
  26. "fmt"
  27. "io"
  28. "net"
  29. "net/http"
  30. "net/url"
  31. "strings"
  32. "sync"
  33. "time"
  34. "github.com/Psiphon-Inc/crypto/nacl/box"
  35. "github.com/Psiphon-Inc/goarista/monotime"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/upstreamproxy"
  38. )
  39. // MeekConn is based on meek-client.go from Tor and Psiphon:
  40. //
  41. // https://gitweb.torproject.org/pluggable-transports/meek.git/blob/HEAD:/meek-client/meek-client.go
  42. // CC0 1.0 Universal
  43. //
  44. // https://bitbucket.org/psiphon/psiphon-circumvention-system/src/default/go/meek-client/meek-client.go
  45. const (
  46. MEEK_PROTOCOL_VERSION = 2
  47. MEEK_COOKIE_MAX_PADDING = 32
  48. MAX_SEND_PAYLOAD_LENGTH = 65536
  49. FULL_RECEIVE_BUFFER_LENGTH = 4194304
  50. READ_PAYLOAD_CHUNK_LENGTH = 65536
  51. MIN_POLL_INTERVAL = 100 * time.Millisecond
  52. MAX_POLL_INTERVAL = 5 * time.Second
  53. POLL_INTERNAL_MULTIPLIER = 1.5
  54. MEEK_ROUND_TRIP_RETRY_DEADLINE = 1 * time.Second
  55. MEEK_ROUND_TRIP_RETRY_DELAY = 50 * time.Millisecond
  56. MEEK_ROUND_TRIP_TIMEOUT = 20 * time.Second
  57. )
  58. // MeekConfig specifies the behavior of a MeekConn
  59. type MeekConfig struct {
  60. // DialAddress is the actual network address to dial to establish a
  61. // connection to the meek server. This may be either a fronted or
  62. // direct address. The address must be in the form "host:port",
  63. // where host may be a domain name or IP address.
  64. DialAddress string
  65. // UseHTTPS indicates whether to use HTTPS (true) or HTTP (false).
  66. UseHTTPS bool
  67. // SNIServerName is the value to place in the TLS SNI server_name
  68. // field when HTTPS is used.
  69. SNIServerName string
  70. // HostHeader is the value to place in the HTTP request Host header.
  71. HostHeader string
  72. // TransformedHostName records whether a HostNameTransformer
  73. // transformation is in effect. This value is used for stats reporting.
  74. TransformedHostName bool
  75. // The following values are used to create the obfuscated meek cookie.
  76. PsiphonServerAddress string
  77. SessionID string
  78. MeekCookieEncryptionPublicKey string
  79. MeekObfuscatedKey string
  80. }
  81. // MeekConn is a network connection that tunnels TCP over HTTP and supports "fronting". Meek sends
  82. // client->server flow in HTTP request bodies and receives server->client flow in HTTP response bodies.
  83. // Polling is used to achieve full duplex TCP.
  84. //
  85. // Fronting is an obfuscation technique in which the connection
  86. // to a web server, typically a CDN, is indistinguishable from any other HTTPS connection to the generic
  87. // "fronting domain" -- the HTTP Host header is used to route the requests to the actual destination.
  88. // See https://trac.torproject.org/projects/tor/wiki/doc/meek for more details.
  89. //
  90. // MeekConn also operates in unfronted mode, in which plain HTTP connections are made without routing
  91. // through a CDN.
  92. type MeekConn struct {
  93. url *url.URL
  94. additionalHeaders map[string]string
  95. cookie *http.Cookie
  96. pendingConns *common.Conns
  97. transport transporter
  98. mutex sync.Mutex
  99. isClosed bool
  100. broadcastClosed chan struct{}
  101. relayWaitGroup *sync.WaitGroup
  102. emptyReceiveBuffer chan *bytes.Buffer
  103. partialReceiveBuffer chan *bytes.Buffer
  104. fullReceiveBuffer chan *bytes.Buffer
  105. emptySendBuffer chan *bytes.Buffer
  106. partialSendBuffer chan *bytes.Buffer
  107. fullSendBuffer chan *bytes.Buffer
  108. }
  109. // transporter is implemented by both http.Transport and upstreamproxy.ProxyAuthTransport.
  110. type transporter interface {
  111. CancelRequest(req *http.Request)
  112. CloseIdleConnections()
  113. RegisterProtocol(scheme string, rt http.RoundTripper)
  114. RoundTrip(req *http.Request) (resp *http.Response, err error)
  115. }
  116. // DialMeek returns an initialized meek connection. A meek connection is
  117. // an HTTP session which does not depend on an underlying socket connection (although
  118. // persistent HTTP connections are used for performance). This function does not
  119. // wait for the connection to be "established" before returning. A goroutine
  120. // is spawned which will eventually start HTTP polling.
  121. // When frontingAddress is not "", fronting is used. This option assumes caller has
  122. // already checked server entry capabilities.
  123. func DialMeek(
  124. meekConfig *MeekConfig,
  125. dialConfig *DialConfig) (meek *MeekConn, err error) {
  126. // Configure transport
  127. // Note: MeekConn has its own PendingConns to manage the underlying HTTP transport connections,
  128. // which may be interrupted on MeekConn.Close(). This code previously used the establishTunnel
  129. // pendingConns here, but that was a lifecycle mismatch: we don't want to abort HTTP transport
  130. // connections while MeekConn is still in use
  131. pendingConns := new(common.Conns)
  132. // Use a copy of DialConfig with the meek pendingConns
  133. meekDialConfig := new(DialConfig)
  134. *meekDialConfig = *dialConfig
  135. meekDialConfig.PendingConns = pendingConns
  136. var transport transporter
  137. if meekConfig.UseHTTPS {
  138. // Custom TLS dialer:
  139. //
  140. // 1. ignores the HTTP request address and uses the fronting domain
  141. // 2. optionally disables SNI -- SNI breaks fronting when used with certain CDNs.
  142. // 3. skips verifying the server cert.
  143. //
  144. // Reasoning for #3:
  145. //
  146. // With a TLS MiM attack in place, and server certs verified, we'll fail to connect because the client
  147. // will refuse to connect. That's not a successful outcome.
  148. //
  149. // With a MiM attack in place, and server certs not verified, we'll fail to connect if the MiM is actively
  150. // targeting Psiphon and classifying the HTTP traffic by Host header or payload signature.
  151. //
  152. // However, in the case of a passive MiM that's just recording traffic or an active MiM that's targeting
  153. // something other than Psiphon, the client will connect. This is a successful outcome.
  154. //
  155. // What is exposed to the MiM? The Host header does not contain a Psiphon server IP address, just an
  156. // unrelated, randomly generated domain name which cannot be used to block direct connections. The
  157. // Psiphon server IP is sent over meek, but it's in the encrypted cookie.
  158. //
  159. // The payload (user traffic) gets its confidentiality and integrity from the underlying SSH protocol.
  160. // So, nothing is leaked to the MiM apart from signatures which could be used to classify the traffic
  161. // as Psiphon to possibly block it; but note that not revealing that the client is Psiphon is outside
  162. // our threat model; we merely seek to evade mass blocking by taking steps that require progressively
  163. // more effort to block.
  164. //
  165. // There is a subtle attack remaining: an adversary that can MiM some CDNs but not others (and so can
  166. // classify Psiphon traffic on some CDNs but not others) may throttle non-MiM CDNs so that our server
  167. // selection always chooses tunnels to the MiM CDN (without any server cert verification, we won't
  168. // exclusively connect to non-MiM CDNs); then the adversary kills the underlying TCP connection after
  169. // some short period. This is mitigated by the "impaired" protocol classification mechanism.
  170. dialer := NewCustomTLSDialer(&CustomTLSConfig{
  171. DialAddr: meekConfig.DialAddress,
  172. Dial: NewTCPDialer(meekDialConfig),
  173. Timeout: meekDialConfig.ConnectTimeout,
  174. SNIServerName: meekConfig.SNIServerName,
  175. SkipVerify: true,
  176. UseIndistinguishableTLS: meekDialConfig.UseIndistinguishableTLS,
  177. TrustedCACertificatesFilename: meekDialConfig.TrustedCACertificatesFilename,
  178. })
  179. // TODO: wrap in an http.Client and use http.Client.Timeout which actually covers round trip
  180. transport = &http.Transport{
  181. Dial: dialer,
  182. ResponseHeaderTimeout: MEEK_ROUND_TRIP_TIMEOUT,
  183. }
  184. } else {
  185. // The dialer ignores address that http.Transport will pass in (derived
  186. // from the HTTP request URL) and always dials meekConfig.DialAddress.
  187. dialer := func(string, string) (net.Conn, error) {
  188. return NewTCPDialer(meekDialConfig)("tcp", meekConfig.DialAddress)
  189. }
  190. // For HTTP, and when the meekConfig.DialAddress matches the
  191. // meekConfig.HostHeader, we let http.Transport handle proxying.
  192. // http.Transport will put the the HTTP server address in the HTTP
  193. // request line. In this one case, we can use an HTTP proxy that does
  194. // not offer CONNECT support.
  195. var proxyUrl func(*http.Request) (*url.URL, error)
  196. if strings.HasPrefix(meekDialConfig.UpstreamProxyUrl, "http://") &&
  197. (meekConfig.DialAddress == meekConfig.HostHeader ||
  198. meekConfig.DialAddress == meekConfig.HostHeader+":80") {
  199. url, err := url.Parse(meekDialConfig.UpstreamProxyUrl)
  200. if err != nil {
  201. return nil, common.ContextError(err)
  202. }
  203. proxyUrl = http.ProxyURL(url)
  204. meekDialConfig.UpstreamProxyUrl = ""
  205. // Here, the dialer must use the address that http.Transport
  206. // passes in (which will be proxy address).
  207. dialer = NewTCPDialer(meekDialConfig)
  208. }
  209. // TODO: wrap in an http.Client and use http.Client.Timeout which actually covers round trip
  210. httpTransport := &http.Transport{
  211. Proxy: proxyUrl,
  212. Dial: dialer,
  213. ResponseHeaderTimeout: MEEK_ROUND_TRIP_TIMEOUT,
  214. }
  215. if proxyUrl != nil {
  216. // Wrap transport with a transport that can perform HTTP proxy auth negotiation
  217. transport, err = upstreamproxy.NewProxyAuthTransport(httpTransport, meekDialConfig.UpstreamProxyCustomHeaders)
  218. if err != nil {
  219. return nil, common.ContextError(err)
  220. }
  221. } else {
  222. transport = httpTransport
  223. }
  224. }
  225. // Scheme is always "http". Otherwise http.Transport will try to do another TLS
  226. // handshake inside the explicit TLS session (in fronting mode).
  227. url := &url.URL{
  228. Scheme: "http",
  229. Host: meekConfig.HostHeader,
  230. Path: "/",
  231. }
  232. var additionalHeaders map[string]string
  233. if meekConfig.UseHTTPS {
  234. host, _, err := net.SplitHostPort(meekConfig.DialAddress)
  235. if err != nil {
  236. return nil, common.ContextError(err)
  237. }
  238. additionalHeaders = map[string]string{
  239. "X-Psiphon-Fronting-Address": host,
  240. }
  241. }
  242. cookie, err := makeMeekCookie(meekConfig)
  243. if err != nil {
  244. return nil, common.ContextError(err)
  245. }
  246. // The main loop of a MeekConn is run in the relay() goroutine.
  247. // A MeekConn implements net.Conn concurrency semantics:
  248. // "Multiple goroutines may invoke methods on a Conn simultaneously."
  249. //
  250. // Read() calls and relay() are synchronized by exchanging control of a single
  251. // receiveBuffer (bytes.Buffer). This single buffer may be:
  252. // - in the emptyReceiveBuffer channel when it is available and empty;
  253. // - in the partialReadBuffer channel when it is available and contains data;
  254. // - in the fullReadBuffer channel when it is available and full of data;
  255. // - "checked out" by relay or Read when they are are writing to or reading from the
  256. // buffer, respectively.
  257. // relay() will obtain the buffer from either the empty or partial channel but block when
  258. // the buffer is full. Read will obtain the buffer from the partial or full channel when
  259. // there is data to read but block when the buffer is empty.
  260. // Write() calls and relay() are synchronized in a similar way, using a single
  261. // sendBuffer.
  262. meek = &MeekConn{
  263. url: url,
  264. additionalHeaders: additionalHeaders,
  265. cookie: cookie,
  266. pendingConns: pendingConns,
  267. transport: transport,
  268. isClosed: false,
  269. broadcastClosed: make(chan struct{}),
  270. relayWaitGroup: new(sync.WaitGroup),
  271. emptyReceiveBuffer: make(chan *bytes.Buffer, 1),
  272. partialReceiveBuffer: make(chan *bytes.Buffer, 1),
  273. fullReceiveBuffer: make(chan *bytes.Buffer, 1),
  274. emptySendBuffer: make(chan *bytes.Buffer, 1),
  275. partialSendBuffer: make(chan *bytes.Buffer, 1),
  276. fullSendBuffer: make(chan *bytes.Buffer, 1),
  277. }
  278. // TODO: benchmark bytes.Buffer vs. built-in append with slices?
  279. meek.emptyReceiveBuffer <- new(bytes.Buffer)
  280. meek.emptySendBuffer <- new(bytes.Buffer)
  281. meek.relayWaitGroup.Add(1)
  282. go meek.relay()
  283. // Enable interruption
  284. if !dialConfig.PendingConns.Add(meek) {
  285. meek.Close()
  286. return nil, common.ContextError(errors.New("pending connections already closed"))
  287. }
  288. return meek, nil
  289. }
  290. // Close terminates the meek connection. Close waits for the relay processing goroutine
  291. // to stop and releases HTTP transport resources.
  292. // A mutex is required to support net.Conn concurrency semantics.
  293. func (meek *MeekConn) Close() (err error) {
  294. meek.mutex.Lock()
  295. isClosed := meek.isClosed
  296. meek.isClosed = true
  297. meek.mutex.Unlock()
  298. if !isClosed {
  299. close(meek.broadcastClosed)
  300. meek.pendingConns.CloseAll()
  301. meek.relayWaitGroup.Wait()
  302. meek.transport.CloseIdleConnections()
  303. }
  304. return nil
  305. }
  306. func (meek *MeekConn) closed() bool {
  307. meek.mutex.Lock()
  308. isClosed := meek.isClosed
  309. meek.mutex.Unlock()
  310. return isClosed
  311. }
  312. // Read reads data from the connection.
  313. // net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported.
  314. func (meek *MeekConn) Read(buffer []byte) (n int, err error) {
  315. if meek.closed() {
  316. return 0, common.ContextError(errors.New("meek connection is closed"))
  317. }
  318. // Block until there is received data to consume
  319. var receiveBuffer *bytes.Buffer
  320. select {
  321. case receiveBuffer = <-meek.partialReceiveBuffer:
  322. case receiveBuffer = <-meek.fullReceiveBuffer:
  323. case <-meek.broadcastClosed:
  324. return 0, common.ContextError(errors.New("meek connection has closed"))
  325. }
  326. n, err = receiveBuffer.Read(buffer)
  327. meek.replaceReceiveBuffer(receiveBuffer)
  328. return n, err
  329. }
  330. // Write writes data to the connection.
  331. // net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported.
  332. func (meek *MeekConn) Write(buffer []byte) (n int, err error) {
  333. if meek.closed() {
  334. return 0, common.ContextError(errors.New("meek connection is closed"))
  335. }
  336. // Repeats until all n bytes are written
  337. n = len(buffer)
  338. for len(buffer) > 0 {
  339. // Block until there is capacity in the send buffer
  340. var sendBuffer *bytes.Buffer
  341. select {
  342. case sendBuffer = <-meek.emptySendBuffer:
  343. case sendBuffer = <-meek.partialSendBuffer:
  344. case <-meek.broadcastClosed:
  345. return 0, common.ContextError(errors.New("meek connection has closed"))
  346. }
  347. writeLen := MAX_SEND_PAYLOAD_LENGTH - sendBuffer.Len()
  348. if writeLen > 0 {
  349. if writeLen > len(buffer) {
  350. writeLen = len(buffer)
  351. }
  352. _, err = sendBuffer.Write(buffer[:writeLen])
  353. buffer = buffer[writeLen:]
  354. }
  355. meek.replaceSendBuffer(sendBuffer)
  356. }
  357. return n, err
  358. }
  359. // Stub implementation of net.Conn.LocalAddr
  360. func (meek *MeekConn) LocalAddr() net.Addr {
  361. return nil
  362. }
  363. // Stub implementation of net.Conn.RemoteAddr
  364. func (meek *MeekConn) RemoteAddr() net.Addr {
  365. return nil
  366. }
  367. // Stub implementation of net.Conn.SetDeadline
  368. func (meek *MeekConn) SetDeadline(t time.Time) error {
  369. return common.ContextError(errors.New("not supported"))
  370. }
  371. // Stub implementation of net.Conn.SetReadDeadline
  372. func (meek *MeekConn) SetReadDeadline(t time.Time) error {
  373. return common.ContextError(errors.New("not supported"))
  374. }
  375. // Stub implementation of net.Conn.SetWriteDeadline
  376. func (meek *MeekConn) SetWriteDeadline(t time.Time) error {
  377. return common.ContextError(errors.New("not supported"))
  378. }
  379. func (meek *MeekConn) replaceReceiveBuffer(receiveBuffer *bytes.Buffer) {
  380. switch {
  381. case receiveBuffer.Len() == 0:
  382. meek.emptyReceiveBuffer <- receiveBuffer
  383. case receiveBuffer.Len() >= FULL_RECEIVE_BUFFER_LENGTH:
  384. meek.fullReceiveBuffer <- receiveBuffer
  385. default:
  386. meek.partialReceiveBuffer <- receiveBuffer
  387. }
  388. }
  389. func (meek *MeekConn) replaceSendBuffer(sendBuffer *bytes.Buffer) {
  390. switch {
  391. case sendBuffer.Len() == 0:
  392. meek.emptySendBuffer <- sendBuffer
  393. case sendBuffer.Len() >= MAX_SEND_PAYLOAD_LENGTH:
  394. meek.fullSendBuffer <- sendBuffer
  395. default:
  396. meek.partialSendBuffer <- sendBuffer
  397. }
  398. }
  399. // relay sends and receives tunneled traffic (payload). An HTTP request is
  400. // triggered when data is in the write queue or at a polling interval.
  401. // There's a geometric increase, up to a maximum, in the polling interval when
  402. // no data is exchanged. Only one HTTP request is in flight at a time.
  403. func (meek *MeekConn) relay() {
  404. // Note: meek.Close() calls here in relay() are made asynchronously
  405. // (using goroutines) since Close() will wait on this WaitGroup.
  406. defer meek.relayWaitGroup.Done()
  407. interval := MIN_POLL_INTERVAL
  408. timeout := time.NewTimer(interval)
  409. sendPayload := make([]byte, MAX_SEND_PAYLOAD_LENGTH)
  410. for {
  411. timeout.Reset(interval)
  412. // Block until there is payload to send or it is time to poll
  413. var sendBuffer *bytes.Buffer
  414. select {
  415. case sendBuffer = <-meek.partialSendBuffer:
  416. case sendBuffer = <-meek.fullSendBuffer:
  417. case <-timeout.C:
  418. // In the polling case, send an empty payload
  419. case <-meek.broadcastClosed:
  420. // TODO: timeout case may be selected when broadcastClosed is set?
  421. return
  422. }
  423. sendPayloadSize := 0
  424. if sendBuffer != nil {
  425. var err error
  426. sendPayloadSize, err = sendBuffer.Read(sendPayload)
  427. meek.replaceSendBuffer(sendBuffer)
  428. if err != nil {
  429. NoticeAlert("%s", common.ContextError(err))
  430. go meek.Close()
  431. return
  432. }
  433. }
  434. receivedPayload, err := meek.roundTrip(sendPayload[:sendPayloadSize])
  435. if err != nil {
  436. NoticeAlert("%s", common.ContextError(err))
  437. go meek.Close()
  438. return
  439. }
  440. if receivedPayload == nil {
  441. // In this case, meek.roundTrip encountered broadcastClosed. Exit without error.
  442. return
  443. }
  444. receivedPayloadSize, err := meek.readPayload(receivedPayload)
  445. if err != nil {
  446. NoticeAlert("%s", common.ContextError(err))
  447. go meek.Close()
  448. return
  449. }
  450. if receivedPayloadSize > 0 || sendPayloadSize > 0 {
  451. interval = 0
  452. } else if interval == 0 {
  453. interval = MIN_POLL_INTERVAL
  454. } else {
  455. interval = time.Duration(float64(interval) * POLL_INTERNAL_MULTIPLIER)
  456. if interval >= MAX_POLL_INTERVAL {
  457. interval = MAX_POLL_INTERVAL
  458. }
  459. }
  460. }
  461. }
  462. // readPayload reads the HTTP response in chunks, making the read buffer available
  463. // to MeekConn.Read() calls after each chunk; the intention is to allow bytes to
  464. // flow back to the reader as soon as possible instead of buffering the entire payload.
  465. func (meek *MeekConn) readPayload(receivedPayload io.ReadCloser) (totalSize int64, err error) {
  466. defer receivedPayload.Close()
  467. totalSize = 0
  468. for {
  469. reader := io.LimitReader(receivedPayload, READ_PAYLOAD_CHUNK_LENGTH)
  470. // Block until there is capacity in the receive buffer
  471. var receiveBuffer *bytes.Buffer
  472. select {
  473. case receiveBuffer = <-meek.emptyReceiveBuffer:
  474. case receiveBuffer = <-meek.partialReceiveBuffer:
  475. case <-meek.broadcastClosed:
  476. return 0, nil
  477. }
  478. // Note: receiveBuffer size may exceed FULL_RECEIVE_BUFFER_LENGTH by up to the size
  479. // of one received payload. The FULL_RECEIVE_BUFFER_LENGTH value is just a threshold.
  480. n, err := receiveBuffer.ReadFrom(reader)
  481. meek.replaceReceiveBuffer(receiveBuffer)
  482. if err != nil {
  483. return 0, common.ContextError(err)
  484. }
  485. totalSize += n
  486. if n == 0 {
  487. break
  488. }
  489. }
  490. return totalSize, nil
  491. }
  492. // roundTrip configures and makes the actual HTTP POST request
  493. func (meek *MeekConn) roundTrip(sendPayload []byte) (io.ReadCloser, error) {
  494. // The retry mitigates intermittent failures between the client and front/server.
  495. //
  496. // Note: Retry will only be effective if entire request failed (underlying transport protocol
  497. // such as SSH will fail if extra bytes are replayed in either direction due to partial relay
  498. // success followed by retry).
  499. // At least one retry is always attempted. We retry when still within a brief deadline and wait
  500. // for a short time before re-dialing.
  501. //
  502. // TODO: in principle, we could retry for min(TUNNEL_WRITE_TIMEOUT, meek-server.MAX_SESSION_STALENESS),
  503. // i.e., as long as the underlying tunnel has not timed out and as long as the server has not
  504. // expired the current meek session. Presently not doing this to avoid excessive connection attempts
  505. // through the first hop. In addition, this will require additional support for timely shutdown.
  506. retries := uint(0)
  507. retryDeadline := monotime.Now().Add(MEEK_ROUND_TRIP_RETRY_DEADLINE)
  508. var err error
  509. var response *http.Response
  510. for {
  511. var request *http.Request
  512. request, err = http.NewRequest("POST", meek.url.String(), bytes.NewReader(sendPayload))
  513. if err != nil {
  514. // Don't retry when can't initialize a Request
  515. break
  516. }
  517. // Don't use the default user agent ("Go 1.1 package http").
  518. // For now, just omit the header (net/http/request.go: "may be blank to not send the header").
  519. request.Header.Set("User-Agent", "")
  520. request.Header.Set("Content-Type", "application/octet-stream")
  521. for name, value := range meek.additionalHeaders {
  522. request.Header.Set(name, value)
  523. }
  524. request.AddCookie(meek.cookie)
  525. // The http.Transport.RoundTrip is run in a goroutine to enable cancelling a request in-flight.
  526. type roundTripResponse struct {
  527. response *http.Response
  528. err error
  529. }
  530. roundTripResponseChannel := make(chan *roundTripResponse, 1)
  531. roundTripWaitGroup := new(sync.WaitGroup)
  532. roundTripWaitGroup.Add(1)
  533. go func() {
  534. defer roundTripWaitGroup.Done()
  535. r, err := meek.transport.RoundTrip(request)
  536. roundTripResponseChannel <- &roundTripResponse{r, err}
  537. }()
  538. select {
  539. case roundTripResponse := <-roundTripResponseChannel:
  540. response = roundTripResponse.response
  541. err = roundTripResponse.err
  542. case <-meek.broadcastClosed:
  543. meek.transport.CancelRequest(request)
  544. return nil, nil
  545. }
  546. roundTripWaitGroup.Wait()
  547. if err == nil {
  548. break
  549. }
  550. if retries >= 1 && monotime.Now().After(retryDeadline) {
  551. break
  552. }
  553. retries += 1
  554. time.Sleep(MEEK_ROUND_TRIP_RETRY_DELAY)
  555. }
  556. if err != nil {
  557. return nil, common.ContextError(err)
  558. }
  559. if response.StatusCode != http.StatusOK {
  560. return nil, common.ContextError(fmt.Errorf("http request failed %d", response.StatusCode))
  561. }
  562. // observe response cookies for meek session key token.
  563. // Once found it must be used for all consecutive requests made to the server
  564. for _, c := range response.Cookies() {
  565. if meek.cookie.Name == c.Name {
  566. meek.cookie.Value = c.Value
  567. break
  568. }
  569. }
  570. return response.Body, nil
  571. }
  572. type meekCookieData struct {
  573. ServerAddress string `json:"p"`
  574. SessionID string `json:"s"`
  575. MeekProtocolVersion int `json:"v"`
  576. }
  577. // makeCookie creates the cookie to be sent with initial meek HTTP request.
  578. // The purpose of the cookie is to send the following to the server:
  579. // ServerAddress -- the Psiphon Server address the meek server should relay to
  580. // SessionID -- the Psiphon session ID (used by meek server to relay geolocation
  581. // information obtained from the CDN through to the Psiphon Server)
  582. // MeekProtocolVersion -- tells the meek server that this client understands
  583. // the latest protocol.
  584. // The server will create a session using these values and send the session ID
  585. // back to the client via Set-Cookie header. Client must use that value with
  586. // all consequent HTTP requests
  587. // In unfronted meek mode, the cookie is visible over the adversary network, so the
  588. // cookie is encrypted and obfuscated.
  589. func makeMeekCookie(meekConfig *MeekConfig) (cookie *http.Cookie, err error) {
  590. // Make the JSON data
  591. serverAddress := meekConfig.PsiphonServerAddress
  592. cookieData := &meekCookieData{
  593. ServerAddress: serverAddress,
  594. SessionID: meekConfig.SessionID,
  595. MeekProtocolVersion: MEEK_PROTOCOL_VERSION,
  596. }
  597. serializedCookie, err := json.Marshal(cookieData)
  598. if err != nil {
  599. return nil, common.ContextError(err)
  600. }
  601. // Encrypt the JSON data
  602. // NaCl box is used for encryption. The peer public key comes from the server entry.
  603. // Nonce is always all zeros, and is not sent in the cookie (the server also uses an all-zero nonce).
  604. // http://nacl.cace-project.eu/box.html:
  605. // "There is no harm in having the same nonce for different messages if the {sender, receiver} sets are
  606. // different. This is true even if the sets overlap. For example, a sender can use the same nonce for two
  607. // different messages if the messages are sent to two different public keys."
  608. var nonce [24]byte
  609. var publicKey [32]byte
  610. decodedPublicKey, err := base64.StdEncoding.DecodeString(meekConfig.MeekCookieEncryptionPublicKey)
  611. if err != nil {
  612. return nil, common.ContextError(err)
  613. }
  614. copy(publicKey[:], decodedPublicKey)
  615. ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
  616. if err != nil {
  617. return nil, common.ContextError(err)
  618. }
  619. box := box.Seal(nil, serializedCookie, &nonce, &publicKey, ephemeralPrivateKey)
  620. encryptedCookie := make([]byte, 32+len(box))
  621. copy(encryptedCookie[0:32], ephemeralPublicKey[0:32])
  622. copy(encryptedCookie[32:], box)
  623. // Obfuscate the encrypted data
  624. obfuscator, err := NewClientObfuscator(
  625. &ObfuscatorConfig{Keyword: meekConfig.MeekObfuscatedKey, MaxPadding: MEEK_COOKIE_MAX_PADDING})
  626. if err != nil {
  627. return nil, common.ContextError(err)
  628. }
  629. obfuscatedCookie := obfuscator.SendSeedMessage()
  630. seedLen := len(obfuscatedCookie)
  631. obfuscatedCookie = append(obfuscatedCookie, encryptedCookie...)
  632. obfuscator.ObfuscateClientToServer(obfuscatedCookie[seedLen:])
  633. // Format the HTTP cookie
  634. // The format is <random letter 'A'-'Z'>=<base64 data>, which is intended to match common cookie formats.
  635. A := int('A')
  636. Z := int('Z')
  637. // letterIndex is integer in range [int('A'), int('Z')]
  638. letterIndex, err := common.MakeSecureRandomInt(Z - A + 1)
  639. if err != nil {
  640. return nil, common.ContextError(err)
  641. }
  642. return &http.Cookie{
  643. Name: string(byte(A + letterIndex)),
  644. Value: base64.StdEncoding.EncodeToString(obfuscatedCookie)},
  645. nil
  646. }