meek.go 26 KB

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