meek.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. "bytes"
  22. "crypto/rand"
  23. golangtls "crypto/tls"
  24. "encoding/base64"
  25. "encoding/hex"
  26. "encoding/json"
  27. "errors"
  28. "io"
  29. "net"
  30. "net/http"
  31. "strings"
  32. "sync"
  33. "sync/atomic"
  34. "time"
  35. "github.com/Psiphon-Inc/crypto/nacl/box"
  36. "github.com/Psiphon-Inc/goarista/monotime"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tls"
  39. )
  40. // MeekServer is based on meek-server.go from Tor and Psiphon:
  41. //
  42. // https://gitweb.torproject.org/pluggable-transports/meek.git/blob/HEAD:/meek-client/meek-client.go
  43. // CC0 1.0 Universal
  44. //
  45. // https://bitbucket.org/psiphon/psiphon-circumvention-system/src/default/go/meek-client/meek-client.go
  46. const (
  47. // Protocol version 1 clients can handle arbitrary length response bodies. Older clients
  48. // report no version number and expect at most 64K response bodies.
  49. MEEK_PROTOCOL_VERSION_1 = 1
  50. // Protocol version 2 clients initiate a session by sending a encrypted and obfuscated meek
  51. // cookie with their initial HTTP request. Connection information is contained within the
  52. // encrypted cookie payload. The server inspects the cookie and establishes a new session and
  53. // returns a new random session ID back to client via Set-Cookie header. The client uses this
  54. // session ID on all subsequent requests for the remainder of the session.
  55. MEEK_PROTOCOL_VERSION_2 = 2
  56. MEEK_MAX_PAYLOAD_LENGTH = 0x10000
  57. MEEK_TURN_AROUND_TIMEOUT = 20 * time.Millisecond
  58. MEEK_EXTENDED_TURN_AROUND_TIMEOUT = 100 * time.Millisecond
  59. MEEK_MAX_SESSION_STALENESS = 45 * time.Second
  60. MEEK_HTTP_CLIENT_IO_TIMEOUT = 45 * time.Second
  61. MEEK_MIN_SESSION_ID_LENGTH = 8
  62. MEEK_MAX_SESSION_ID_LENGTH = 20
  63. )
  64. // MeekServer implements the meek protocol, which tunnels TCP traffic (in the case of Psiphon,
  65. // Obfusated SSH traffic) over HTTP. Meek may be fronted (through a CDN) or direct and may be
  66. // HTTP or HTTPS.
  67. //
  68. // Upstream traffic arrives in HTTP request bodies and downstream traffic is sent in response
  69. // bodies. The sequence of traffic for a given flow is associated using a session ID that's
  70. // set as a HTTP cookie for the client to submit with each request.
  71. //
  72. // MeekServer hooks into TunnelServer via the net.Conn interface by transforming the
  73. // HTTP payload traffic for a given session into net.Conn conforming Read()s and Write()s via
  74. // the meekConn struct.
  75. type MeekServer struct {
  76. support *SupportServices
  77. listener net.Listener
  78. tlsConfig *tls.Config
  79. clientHandler func(clientConn net.Conn)
  80. openConns *common.Conns
  81. stopBroadcast <-chan struct{}
  82. sessionsLock sync.RWMutex
  83. sessions map[string]*meekSession
  84. }
  85. // NewMeekServer initializes a new meek server.
  86. func NewMeekServer(
  87. support *SupportServices,
  88. listener net.Listener,
  89. useTLS, useObfuscatedSessionTickets bool,
  90. clientHandler func(clientConn net.Conn),
  91. stopBroadcast <-chan struct{}) (*MeekServer, error) {
  92. meekServer := &MeekServer{
  93. support: support,
  94. listener: listener,
  95. clientHandler: clientHandler,
  96. openConns: new(common.Conns),
  97. stopBroadcast: stopBroadcast,
  98. sessions: make(map[string]*meekSession),
  99. }
  100. if useTLS {
  101. tlsConfig, err := makeMeekTLSConfig(
  102. support, useObfuscatedSessionTickets)
  103. if err != nil {
  104. return nil, common.ContextError(err)
  105. }
  106. meekServer.tlsConfig = tlsConfig
  107. }
  108. return meekServer, nil
  109. }
  110. // Run runs the meek server; this function blocks while serving HTTP or
  111. // HTTPS connections on the specified listener. This function also runs
  112. // a goroutine which cleans up expired meek client sessions.
  113. //
  114. // To stop the meek server, both Close() the listener and set the stopBroadcast
  115. // signal specified in NewMeekServer.
  116. func (server *MeekServer) Run() error {
  117. defer server.listener.Close()
  118. defer server.openConns.CloseAll()
  119. // Expire sessions
  120. reaperWaitGroup := new(sync.WaitGroup)
  121. reaperWaitGroup.Add(1)
  122. go func() {
  123. defer reaperWaitGroup.Done()
  124. ticker := time.NewTicker(MEEK_MAX_SESSION_STALENESS / 2)
  125. defer ticker.Stop()
  126. for {
  127. select {
  128. case <-ticker.C:
  129. server.closeExpireSessions()
  130. case <-server.stopBroadcast:
  131. return
  132. }
  133. }
  134. }()
  135. // Serve HTTP or HTTPS
  136. // Notes:
  137. // - WriteTimeout may include time awaiting request, as per:
  138. // https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts
  139. // - Legacy meek-server wrapped each client HTTP connection with an explict idle
  140. // timeout net.Conn and didn't use http.Server timeouts. We could do the same
  141. // here (use ActivityMonitoredConn) but the stock http.Server timeouts should
  142. // now be sufficient.
  143. httpServer := &http.Server{
  144. ReadTimeout: MEEK_HTTP_CLIENT_IO_TIMEOUT,
  145. WriteTimeout: MEEK_HTTP_CLIENT_IO_TIMEOUT,
  146. Handler: server,
  147. ConnState: server.httpConnStateCallback,
  148. // Disable auto HTTP/2 (https://golang.org/doc/go1.6)
  149. TLSNextProto: make(map[string]func(*http.Server, *golangtls.Conn, http.Handler)),
  150. }
  151. // Note: Serve() will be interrupted by listener.Close() call
  152. var err error
  153. if server.tlsConfig != nil {
  154. httpsServer := HTTPSServer{Server: *httpServer}
  155. err = httpsServer.ServeTLS(server.listener, server.tlsConfig)
  156. } else {
  157. err = httpServer.Serve(server.listener)
  158. }
  159. // Can't check for the exact error that Close() will cause in Accept(),
  160. // (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
  161. // explicit stop signal to stop gracefully.
  162. select {
  163. case <-server.stopBroadcast:
  164. err = nil
  165. default:
  166. }
  167. reaperWaitGroup.Wait()
  168. return err
  169. }
  170. // ServeHTTP handles meek client HTTP requests, where the request body
  171. // contains upstream traffic and the response will contain downstream
  172. // traffic.
  173. func (server *MeekServer) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
  174. // Note: no longer requiring that the request method is POST
  175. // Check for the expected meek/session ID cookie.
  176. // Also check for prohibited HTTP headers.
  177. var meekCookie *http.Cookie
  178. for _, c := range request.Cookies() {
  179. meekCookie = c
  180. break
  181. }
  182. if meekCookie == nil || len(meekCookie.Value) == 0 {
  183. log.WithContext().Warning("missing meek cookie")
  184. server.terminateConnection(responseWriter, request)
  185. return
  186. }
  187. if len(server.support.Config.MeekProhibitedHeaders) > 0 {
  188. for _, header := range server.support.Config.MeekProhibitedHeaders {
  189. value := request.Header.Get(header)
  190. if header != "" {
  191. log.WithContextFields(LogFields{
  192. "header": header,
  193. "value": value,
  194. }).Warning("prohibited meek header")
  195. server.terminateConnection(responseWriter, request)
  196. return
  197. }
  198. }
  199. }
  200. // Lookup or create a new session for given meek cookie/session ID.
  201. sessionID, session, err := server.getSession(request, meekCookie)
  202. if err != nil {
  203. log.WithContextFields(LogFields{"error": err}).Warning("session lookup failed")
  204. server.terminateConnection(responseWriter, request)
  205. return
  206. }
  207. // pumpReads causes a TunnelServer/SSH goroutine blocking on a Read to
  208. // read the request body as upstream traffic.
  209. // TODO: run pumpReads and pumpWrites concurrently?
  210. err = session.clientConn.pumpReads(request.Body)
  211. if err != nil {
  212. if err != io.EOF {
  213. log.WithContextFields(LogFields{"error": err}).Warning("pump reads failed")
  214. }
  215. server.terminateConnection(responseWriter, request)
  216. server.closeSession(sessionID)
  217. return
  218. }
  219. // Set cookie before writing the response.
  220. if session.meekProtocolVersion >= MEEK_PROTOCOL_VERSION_2 && session.sessionIDSent == false {
  221. // Replace the meek cookie with the session ID.
  222. // SetCookie for the the session ID cookie is only set once, to reduce overhead. This
  223. // session ID value replaces the original meek cookie value.
  224. http.SetCookie(responseWriter, &http.Cookie{Name: meekCookie.Name, Value: sessionID})
  225. session.sessionIDSent = true
  226. }
  227. // pumpWrites causes a TunnelServer/SSH goroutine blocking on a Write to
  228. // write its downstream traffic through to the response body.
  229. err = session.clientConn.pumpWrites(responseWriter)
  230. if err != nil {
  231. if err != io.EOF {
  232. log.WithContextFields(LogFields{"error": err}).Warning("pump writes failed")
  233. }
  234. server.terminateConnection(responseWriter, request)
  235. server.closeSession(sessionID)
  236. return
  237. }
  238. }
  239. // getSession returns the meek client session corresponding the
  240. // meek cookie/session ID. If no session is found, the cookie is
  241. // treated as a meek cookie for a new session and its payload is
  242. // extracted and used to establish a new session.
  243. func (server *MeekServer) getSession(
  244. request *http.Request, meekCookie *http.Cookie) (string, *meekSession, error) {
  245. // Check for an existing session
  246. server.sessionsLock.RLock()
  247. existingSessionID := meekCookie.Value
  248. session, ok := server.sessions[existingSessionID]
  249. server.sessionsLock.RUnlock()
  250. if ok {
  251. session.touch()
  252. return existingSessionID, session, nil
  253. }
  254. // TODO: can multiple http client connections using same session cookie
  255. // cause race conditions on session struct?
  256. // The session is new (or expired). Treat the cookie value as a new meek
  257. // cookie, extract the payload, and create a new session.
  258. payloadJSON, err := getMeekCookiePayload(server.support, meekCookie.Value)
  259. if err != nil {
  260. return "", nil, common.ContextError(err)
  261. }
  262. // Note: this meek server ignores all but Version MeekProtocolVersion;
  263. // the other values are legacy or currently unused.
  264. var clientSessionData struct {
  265. MeekProtocolVersion int `json:"v"`
  266. PsiphonClientSessionId string `json:"s"`
  267. PsiphonServerAddress string `json:"p"`
  268. }
  269. err = json.Unmarshal(payloadJSON, &clientSessionData)
  270. if err != nil {
  271. return "", nil, common.ContextError(err)
  272. }
  273. // Determine the client remote address, which is used for geolocation
  274. // and stats. When an intermediate proxy or CDN is in use, we may be
  275. // able to determine the original client address by inspecting HTTP
  276. // headers such as X-Forwarded-For.
  277. clientIP := strings.Split(request.RemoteAddr, ":")[0]
  278. if len(server.support.Config.MeekProxyForwardedForHeaders) > 0 {
  279. for _, header := range server.support.Config.MeekProxyForwardedForHeaders {
  280. value := request.Header.Get(header)
  281. if len(value) > 0 {
  282. // Some headers, such as X-Forwarded-For, are a comma-separated
  283. // list of IPs (each proxy in a chain). The first IP should be
  284. // the client IP.
  285. proxyClientIP := strings.Split(value, ",")[0]
  286. if net.ParseIP(proxyClientIP) != nil &&
  287. server.support.GeoIPService.Lookup(proxyClientIP).Country != GEOIP_UNKNOWN_VALUE {
  288. clientIP = proxyClientIP
  289. break
  290. }
  291. }
  292. }
  293. }
  294. // Create a new meek conn that will relay the payload
  295. // between meek request/responses and the tunnel server client
  296. // handler. The client IP is also used to initialize the
  297. // meek conn with a useful value to return when the tunnel
  298. // server calls conn.RemoteAddr() to get the client's IP address.
  299. // Assumes clientIP is a valid IP address; the port value is a stub
  300. // and is expected to be ignored.
  301. clientConn := newMeekConn(
  302. &net.TCPAddr{
  303. IP: net.ParseIP(clientIP),
  304. Port: 0,
  305. },
  306. clientSessionData.MeekProtocolVersion)
  307. session = &meekSession{
  308. clientConn: clientConn,
  309. meekProtocolVersion: clientSessionData.MeekProtocolVersion,
  310. sessionIDSent: false,
  311. }
  312. session.touch()
  313. // Note: MEEK_PROTOCOL_VERSION_1 doesn't support changing the
  314. // meek cookie to a session ID; v1 clients always send the
  315. // original meek cookie value with each request. The issue with
  316. // v1 is that clients which wake after a device sleep will attempt
  317. // to resume a meek session and the server can't differentiate
  318. // between resuming a session and creating a new session. This
  319. // causes the v1 client connection to hang/timeout.
  320. sessionID := meekCookie.Value
  321. if clientSessionData.MeekProtocolVersion >= MEEK_PROTOCOL_VERSION_2 {
  322. sessionID, err = makeMeekSessionID()
  323. if err != nil {
  324. return "", nil, common.ContextError(err)
  325. }
  326. }
  327. server.sessionsLock.Lock()
  328. server.sessions[sessionID] = session
  329. server.sessionsLock.Unlock()
  330. // Note: from the tunnel server's perspective, this client connection
  331. // will close when closeSessionHelper calls Close() on the meekConn.
  332. server.clientHandler(session.clientConn)
  333. return sessionID, session, nil
  334. }
  335. func (server *MeekServer) closeSessionHelper(
  336. sessionID string, session *meekSession) {
  337. // TODO: close the persistent HTTP client connection, if one exists
  338. session.clientConn.Close()
  339. // Note: assumes caller holds lock on sessionsLock
  340. delete(server.sessions, sessionID)
  341. }
  342. func (server *MeekServer) closeSession(sessionID string) {
  343. server.sessionsLock.Lock()
  344. session, ok := server.sessions[sessionID]
  345. if ok {
  346. server.closeSessionHelper(sessionID, session)
  347. }
  348. server.sessionsLock.Unlock()
  349. }
  350. func (server *MeekServer) closeExpireSessions() {
  351. server.sessionsLock.Lock()
  352. for sessionID, session := range server.sessions {
  353. if session.expired() {
  354. server.closeSessionHelper(sessionID, session)
  355. }
  356. }
  357. server.sessionsLock.Unlock()
  358. }
  359. // httpConnStateCallback tracks open persistent HTTP/HTTPS connections to the
  360. // meek server.
  361. func (server *MeekServer) httpConnStateCallback(conn net.Conn, connState http.ConnState) {
  362. switch connState {
  363. case http.StateNew:
  364. server.openConns.Add(conn)
  365. case http.StateHijacked, http.StateClosed:
  366. server.openConns.Remove(conn)
  367. }
  368. }
  369. // terminateConnection sends a 404 response to a client and also closes
  370. // a persisitent connection.
  371. func (server *MeekServer) terminateConnection(
  372. responseWriter http.ResponseWriter, request *http.Request) {
  373. http.NotFound(responseWriter, request)
  374. hijack, ok := responseWriter.(http.Hijacker)
  375. if !ok {
  376. return
  377. }
  378. conn, buffer, err := hijack.Hijack()
  379. if err != nil {
  380. return
  381. }
  382. buffer.Flush()
  383. conn.Close()
  384. }
  385. type meekSession struct {
  386. // Note: 64-bit ints used with atomic operations are at placed
  387. // at the start of struct to ensure 64-bit alignment.
  388. // (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
  389. lastActivity int64
  390. clientConn *meekConn
  391. meekProtocolVersion int
  392. sessionIDSent bool
  393. }
  394. func (session *meekSession) touch() {
  395. atomic.StoreInt64(&session.lastActivity, int64(monotime.Now()))
  396. }
  397. func (session *meekSession) expired() bool {
  398. lastActivity := monotime.Time(atomic.LoadInt64(&session.lastActivity))
  399. return monotime.Since(lastActivity) > MEEK_MAX_SESSION_STALENESS
  400. }
  401. // makeMeekTLSConfig creates a TLS config for a meek HTTPS listener.
  402. // Currently, this config is optimized for fronted meek where the nature
  403. // of the connection is non-circumvention; it's optimized for performance
  404. // assuming the peer is an uncensored CDN.
  405. func makeMeekTLSConfig(
  406. support *SupportServices,
  407. useObfuscatedSessionTickets bool) (*tls.Config, error) {
  408. certificate, privateKey, err := GenerateWebServerCertificate(common.GenerateHostName())
  409. if err != nil {
  410. return nil, common.ContextError(err)
  411. }
  412. tlsCertificate, err := tls.X509KeyPair(
  413. []byte(certificate), []byte(privateKey))
  414. if err != nil {
  415. return nil, common.ContextError(err)
  416. }
  417. config := &tls.Config{
  418. Certificates: []tls.Certificate{tlsCertificate},
  419. NextProtos: []string{"http/1.1"},
  420. MinVersion: tls.VersionTLS10,
  421. // This is a reordering of the supported CipherSuites in golang 1.6. Non-ephemeral key
  422. // CipherSuites greatly reduce server load, and we try to select these since the meek
  423. // protocol is providing obfuscation, not privacy/integrity (this is provided by the
  424. // tunneled SSH), so we don't benefit from the perfect forward secrecy property provided
  425. // by ephemeral key CipherSuites.
  426. // https://github.com/golang/go/blob/1cb3044c9fcd88e1557eca1bf35845a4108bc1db/src/crypto/tls/cipher_suites.go#L75
  427. CipherSuites: []uint16{
  428. tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
  429. tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
  430. tls.TLS_RSA_WITH_RC4_128_SHA,
  431. tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  432. tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  433. tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  434. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  435. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  436. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  437. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  438. tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  439. tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
  440. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  441. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  442. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  443. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  444. tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  445. },
  446. PreferServerCipherSuites: true,
  447. }
  448. if useObfuscatedSessionTickets {
  449. // See obfuscated session ticket overview
  450. // in tls.NewObfuscatedClientSessionCache
  451. var obfuscatedSessionTicketKey [32]byte
  452. key, err := hex.DecodeString(support.Config.MeekObfuscatedKey)
  453. if err == nil && len(key) != 32 {
  454. err = errors.New("invalid obfuscated session key length")
  455. }
  456. if err != nil {
  457. return nil, common.ContextError(err)
  458. }
  459. copy(obfuscatedSessionTicketKey[:], key)
  460. var standardSessionTicketKey [32]byte
  461. _, err = rand.Read(standardSessionTicketKey[:])
  462. if err != nil {
  463. return nil, common.ContextError(err)
  464. }
  465. // Note: SessionTicketKey needs to be set, or else, it appears,
  466. // tls.Config.serverInit() will clobber the value set by
  467. // SetSessionTicketKeys.
  468. config.SessionTicketKey = obfuscatedSessionTicketKey
  469. config.SetSessionTicketKeys([][32]byte{
  470. standardSessionTicketKey,
  471. obfuscatedSessionTicketKey})
  472. }
  473. return config, nil
  474. }
  475. // getMeekCookiePayload extracts the payload from a meek cookie. The cookie
  476. // paylod is base64 encoded, obfuscated, and NaCl encrypted.
  477. func getMeekCookiePayload(support *SupportServices, cookieValue string) ([]byte, error) {
  478. decodedValue, err := base64.StdEncoding.DecodeString(cookieValue)
  479. if err != nil {
  480. return nil, common.ContextError(err)
  481. }
  482. // The data consists of an obfuscated seed message prepended
  483. // to the obfuscated, encrypted payload. The server obfuscator
  484. // will read the seed message, leaving the remaining encrypted
  485. // data in the reader.
  486. reader := bytes.NewReader(decodedValue[:])
  487. obfuscator, err := common.NewServerObfuscator(
  488. reader,
  489. &common.ObfuscatorConfig{Keyword: support.Config.MeekObfuscatedKey})
  490. if err != nil {
  491. return nil, common.ContextError(err)
  492. }
  493. offset, err := reader.Seek(0, 1)
  494. if err != nil {
  495. return nil, common.ContextError(err)
  496. }
  497. encryptedPayload := decodedValue[offset:]
  498. obfuscator.ObfuscateClientToServer(encryptedPayload)
  499. var nonce [24]byte
  500. var privateKey, ephemeralPublicKey [32]byte
  501. decodedPrivateKey, err := base64.StdEncoding.DecodeString(
  502. support.Config.MeekCookieEncryptionPrivateKey)
  503. if err != nil {
  504. return nil, common.ContextError(err)
  505. }
  506. copy(privateKey[:], decodedPrivateKey)
  507. if len(encryptedPayload) < 32 {
  508. return nil, common.ContextError(errors.New("unexpected encrypted payload size"))
  509. }
  510. copy(ephemeralPublicKey[0:32], encryptedPayload[0:32])
  511. payload, ok := box.Open(nil, encryptedPayload[32:], &nonce, &ephemeralPublicKey, &privateKey)
  512. if !ok {
  513. return nil, common.ContextError(errors.New("open box failed"))
  514. }
  515. return payload, nil
  516. }
  517. // makeMeekSessionID creates a new session ID. The variable size is intended to
  518. // frustrate traffic analysis of both plaintext and TLS meek traffic.
  519. func makeMeekSessionID() (string, error) {
  520. size := MEEK_MIN_SESSION_ID_LENGTH
  521. n, err := common.MakeSecureRandomInt(MEEK_MAX_SESSION_ID_LENGTH - MEEK_MIN_SESSION_ID_LENGTH)
  522. if err != nil {
  523. return "", common.ContextError(err)
  524. }
  525. size += n
  526. sessionID, err := common.MakeRandomStringBase64(size)
  527. if err != nil {
  528. return "", common.ContextError(err)
  529. }
  530. return sessionID, nil
  531. }
  532. // meekConn implements the net.Conn interface and is to be used as a client
  533. // connection by the tunnel server (being passed to sshServer.handleClient).
  534. // meekConn bridges net/http request/response payload readers and writers
  535. // and goroutines calling Read()s and Write()s.
  536. type meekConn struct {
  537. remoteAddr net.Addr
  538. protocolVersion int
  539. closeBroadcast chan struct{}
  540. closed int32
  541. readLock sync.Mutex
  542. emptyReadBuffer chan *bytes.Buffer
  543. partialReadBuffer chan *bytes.Buffer
  544. fullReadBuffer chan *bytes.Buffer
  545. writeLock sync.Mutex
  546. nextWriteBuffer chan []byte
  547. writeResult chan error
  548. }
  549. func newMeekConn(remoteAddr net.Addr, protocolVersion int) *meekConn {
  550. conn := &meekConn{
  551. remoteAddr: remoteAddr,
  552. protocolVersion: protocolVersion,
  553. closeBroadcast: make(chan struct{}),
  554. closed: 0,
  555. emptyReadBuffer: make(chan *bytes.Buffer, 1),
  556. partialReadBuffer: make(chan *bytes.Buffer, 1),
  557. fullReadBuffer: make(chan *bytes.Buffer, 1),
  558. nextWriteBuffer: make(chan []byte, 1),
  559. writeResult: make(chan error, 1),
  560. }
  561. // Read() calls and pumpReads() are synchronized by exchanging control
  562. // of a single readBuffer. This is the same scheme used in and described
  563. // in psiphon.MeekConn.
  564. conn.emptyReadBuffer <- new(bytes.Buffer)
  565. return conn
  566. }
  567. // pumpReads causes goroutines blocking on meekConn.Read() to read
  568. // from the specified reader. This function blocks until the reader
  569. // is fully consumed or the meekConn is closed. A read buffer allows
  570. // up to MEEK_MAX_PAYLOAD_LENGTH bytes to be read and buffered without
  571. // a Read() immediately consuming the bytes, but there's still a
  572. // possibility of a stall if no Read() calls are made after this
  573. // read buffer is full.
  574. // Note: assumes only one concurrent call to pumpReads
  575. func (conn *meekConn) pumpReads(reader io.Reader) error {
  576. for {
  577. var readBuffer *bytes.Buffer
  578. select {
  579. case readBuffer = <-conn.emptyReadBuffer:
  580. case readBuffer = <-conn.partialReadBuffer:
  581. case <-conn.closeBroadcast:
  582. return io.EOF
  583. }
  584. limitReader := io.LimitReader(reader, int64(MEEK_MAX_PAYLOAD_LENGTH-readBuffer.Len()))
  585. n, err := readBuffer.ReadFrom(limitReader)
  586. conn.replaceReadBuffer(readBuffer)
  587. if n == 0 || err != nil {
  588. return err
  589. }
  590. }
  591. }
  592. // Read reads from the meekConn into buffer. Read blocks until
  593. // some data is read or the meekConn closes. Under the hood, it
  594. // waits for pumpReads to submit a reader to read from.
  595. // Note: lock is to conform with net.Conn concurrency semantics
  596. func (conn *meekConn) Read(buffer []byte) (int, error) {
  597. conn.readLock.Lock()
  598. defer conn.readLock.Unlock()
  599. var readBuffer *bytes.Buffer
  600. select {
  601. case readBuffer = <-conn.partialReadBuffer:
  602. case readBuffer = <-conn.fullReadBuffer:
  603. case <-conn.closeBroadcast:
  604. return 0, io.EOF
  605. }
  606. n, err := readBuffer.Read(buffer)
  607. conn.replaceReadBuffer(readBuffer)
  608. return n, err
  609. }
  610. func (conn *meekConn) replaceReadBuffer(readBuffer *bytes.Buffer) {
  611. switch readBuffer.Len() {
  612. case MEEK_MAX_PAYLOAD_LENGTH:
  613. conn.fullReadBuffer <- readBuffer
  614. case 0:
  615. conn.emptyReadBuffer <- readBuffer
  616. default:
  617. conn.partialReadBuffer <- readBuffer
  618. }
  619. }
  620. // pumpWrites causes goroutines blocking on meekConn.Write() to write
  621. // to the specified writer. This function blocks until the meek response
  622. // body limits (size for protocol v1, turn around time for protocol v2+)
  623. // are met, or the meekConn is closed.
  624. // Note: channel scheme assumes only one concurrent call to pumpWrites
  625. func (conn *meekConn) pumpWrites(writer io.Writer) error {
  626. startTime := monotime.Now()
  627. timeout := time.NewTimer(MEEK_TURN_AROUND_TIMEOUT)
  628. defer timeout.Stop()
  629. for {
  630. select {
  631. case buffer := <-conn.nextWriteBuffer:
  632. _, err := writer.Write(buffer)
  633. // Assumes that writeResult won't block.
  634. // Note: always send the err to writeResult,
  635. // as the Write() caller is blocking on this.
  636. conn.writeResult <- err
  637. if err != nil {
  638. return err
  639. }
  640. if conn.protocolVersion < MEEK_PROTOCOL_VERSION_1 {
  641. // Pre-protocol version 1 clients expect at most
  642. // MEEK_MAX_PAYLOAD_LENGTH response bodies
  643. return nil
  644. }
  645. totalElapsedTime := monotime.Since(startTime) / time.Millisecond
  646. if totalElapsedTime >= MEEK_EXTENDED_TURN_AROUND_TIMEOUT {
  647. return nil
  648. }
  649. timeout.Reset(MEEK_TURN_AROUND_TIMEOUT)
  650. case <-timeout.C:
  651. return nil
  652. case <-conn.closeBroadcast:
  653. return io.EOF
  654. }
  655. }
  656. }
  657. // Write writes the buffer to the meekConn. It blocks until the
  658. // entire buffer is written to or the meekConn closes. Under the
  659. // hood, it waits for sufficient pumpWrites calls to consume the
  660. // write buffer.
  661. // Note: lock is to conform with net.Conn concurrency semantics
  662. func (conn *meekConn) Write(buffer []byte) (int, error) {
  663. conn.writeLock.Lock()
  664. defer conn.writeLock.Unlock()
  665. // TODO: may be more efficient to send whole buffer
  666. // and have pumpWrites stash partial buffer when can't
  667. // send it all.
  668. n := 0
  669. for n < len(buffer) {
  670. end := n + MEEK_MAX_PAYLOAD_LENGTH
  671. if end > len(buffer) {
  672. end = len(buffer)
  673. }
  674. // Only write MEEK_MAX_PAYLOAD_LENGTH at a time,
  675. // to ensure compatibility with v1 protocol.
  676. chunk := buffer[n:end]
  677. select {
  678. case conn.nextWriteBuffer <- chunk:
  679. case <-conn.closeBroadcast:
  680. return n, io.EOF
  681. }
  682. // Wait for the buffer to be processed.
  683. select {
  684. case err := <-conn.writeResult:
  685. if err != nil {
  686. return n, err
  687. }
  688. case <-conn.closeBroadcast:
  689. return n, io.EOF
  690. }
  691. n += len(chunk)
  692. }
  693. return n, nil
  694. }
  695. // Close closes the meekConn. This will interrupt any blocked
  696. // Read, Write, pumpReads, and pumpWrites.
  697. func (conn *meekConn) Close() error {
  698. if atomic.CompareAndSwapInt32(&conn.closed, 0, 1) {
  699. close(conn.closeBroadcast)
  700. }
  701. return nil
  702. }
  703. // Stub implementation of net.Conn.LocalAddr
  704. func (conn *meekConn) LocalAddr() net.Addr {
  705. return nil
  706. }
  707. // RemoteAddr returns the remoteAddr specified in newMeekConn. This
  708. // acts as a proxy for the actual remote address, which is either a
  709. // direct HTTP/HTTPS connection remote address, or in the case of
  710. // downstream proxy of CDN fronts, some other value determined via
  711. // HTTP headers.
  712. func (conn *meekConn) RemoteAddr() net.Addr {
  713. return conn.remoteAddr
  714. }
  715. // SetDeadline is not a true implementation of net.Conn.SetDeadline. It
  716. // merely checks that the requested timeout exceeds the MEEK_MAX_SESSION_STALENESS
  717. // period. When it does, and the session is idle, the meekConn Read/Write will
  718. // be interrupted and return io.EOF (not a timeout error) before the deadline.
  719. // In other words, this conn will approximate the desired functionality of
  720. // timing out on idle on or before the requested deadline.
  721. func (conn *meekConn) SetDeadline(t time.Time) error {
  722. // Overhead: nanoseconds (https://blog.cloudflare.com/its-go-time-on-linux/)
  723. if time.Now().Add(MEEK_MAX_SESSION_STALENESS).Before(t) {
  724. return nil
  725. }
  726. return common.ContextError(errors.New("not supported"))
  727. }
  728. // Stub implementation of net.Conn.SetReadDeadline
  729. func (conn *meekConn) SetReadDeadline(t time.Time) error {
  730. return common.ContextError(errors.New("not supported"))
  731. }
  732. // Stub implementation of net.Conn.SetWriteDeadline
  733. func (conn *meekConn) SetWriteDeadline(t time.Time) error {
  734. return common.ContextError(errors.New("not supported"))
  735. }