meekConn.go 25 KB

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