meekConn.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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. "context"
  23. "crypto/rand"
  24. "crypto/tls"
  25. "encoding/base64"
  26. "encoding/json"
  27. "fmt"
  28. "io"
  29. "io/ioutil"
  30. "net"
  31. "net/http"
  32. "net/url"
  33. "strings"
  34. "sync"
  35. "time"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/obfuscator"
  39. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  40. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  41. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  42. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic"
  43. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms"
  44. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/values"
  45. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/upstreamproxy"
  46. "golang.org/x/crypto/nacl/box"
  47. "golang.org/x/net/http2"
  48. )
  49. // MeekConn is based on meek-client.go from Tor:
  50. //
  51. // https://gitweb.torproject.org/pluggable-transports/meek.git/blob/HEAD:/meek-client/meek-client.go
  52. // CC0 1.0 Universal
  53. const (
  54. MEEK_PROTOCOL_VERSION = 3
  55. MEEK_MAX_REQUEST_PAYLOAD_LENGTH = 65536
  56. )
  57. type MeekMode int
  58. const (
  59. MeekModeRelay = iota
  60. MeekModeObfuscatedRoundTrip
  61. MeekModePlaintextRoundTrip
  62. MeekModeWrappedPlaintextRoundTrip
  63. )
  64. // MeekConfig specifies the behavior of a MeekConn.
  65. type MeekConfig struct {
  66. // DiagnosticID is the server ID to record in any diagnostics notices.
  67. DiagnosticID string
  68. // Parameters is the active set of parameters.Parameters to use
  69. // for the meek dial.
  70. Parameters *parameters.Parameters
  71. // Mode selects the mode of operation:
  72. //
  73. // MeekModeRelay: encapsulates net.Conn flows in HTTP requests and responses;
  74. // secures and obfuscates metadata in an encrypted HTTP cookie, making it
  75. // suitable for non-TLS HTTP and HTTPS with unverifed server certificates;
  76. // the caller is responsible for securing and obfuscating the net.Conn flows;
  77. // the origin server should be a meek server; used for the meek tunnel
  78. // protocols.
  79. //
  80. // MeekModeObfuscatedRoundTrip: enables ObfuscatedRoundTrip, which performs
  81. // HTTP round trips; secures and obfuscates metadata, including the end point
  82. // (or path), in an encrypted HTTP cookie, making it suitable for non-TLS
  83. // HTTP and HTTPS with unverifed server certificates; the caller is
  84. // responsible for securing and obfuscating request/response payloads; the
  85. // origin server should be a meek server; used for tactics requests.
  86. //
  87. // MeekModePlaintextRoundTrip: enables RoundTrip; the MeekConn is an
  88. // http.RoundTripper; there are no security or obfuscation measures at the
  89. // HTTP level; TLS and server certificate verification is required; the
  90. // origin server may be any HTTP(S) server.
  91. //
  92. // MeekModeWrappedPlaintextRoundTrip: is equivalent to
  93. // MeekModePlaintextRoundTrip, except skipping of server certificate
  94. // verification is permitted. In this mode, the caller is asserting that
  95. // the HTTP plaintext payload is wrapped in its own transport security
  96. // layer.
  97. //
  98. // As with the other modes, MeekMode[Wrapped]PlaintextRoundTrip supports
  99. // HTTP/2 with utls, and integration with DialParameters for replay --
  100. // which are not otherwise implemented if using just CustomTLSDialer and
  101. // net.http.
  102. Mode MeekMode
  103. // DialAddress is the actual network address to dial to establish a
  104. // connection to the meek server. This may be either a fronted or
  105. // direct address. The address must be in the form "host:port",
  106. // where host may be a domain name or IP address.
  107. DialAddress string
  108. // UseQUIC indicates whether to use HTTP/2 over QUIC.
  109. UseQUIC bool
  110. // QUICVersion indicates which QUIC version to use.
  111. QUICVersion string
  112. // QUICClientHelloSeed is used for randomized QUIC Client Hellos.
  113. QUICClientHelloSeed *prng.Seed
  114. // QUICDialEarly indicates whether the client should attempt 0-RTT.
  115. QUICDialEarly bool
  116. // QUICDisablePathMTUDiscovery indicates whether to disable path MTU
  117. // discovery in the QUIC client.
  118. QUICDisablePathMTUDiscovery bool
  119. // UseHTTPS indicates whether to use HTTPS (true) or HTTP (false).
  120. UseHTTPS bool
  121. // TLSProfile specifies the value for CustomTLSConfig.TLSProfile for all
  122. // underlying TLS connections created by this meek connection.
  123. TLSProfile string
  124. // QUICTLSClientSessionCache specifies the TLS session cache to use
  125. // for Meek connections that use HTTP/2 over QUIC.
  126. QUICTLSClientSessionCache *common.TLSClientSessionCacheWrapper
  127. // TLSClientSessionCache specifies the TLS session cache to use for
  128. // HTTPS (non-QUIC) Meek connections.
  129. TLSClientSessionCache *common.UtlsClientSessionCacheWrapper
  130. // TLSFragmentClientHello specifies whether to fragment the TLS Client Hello.
  131. TLSFragmentClientHello bool
  132. // LegacyPassthrough indicates that the server expects a legacy passthrough
  133. // message.
  134. LegacyPassthrough bool
  135. // NoDefaultTLSSessionID specifies the value for
  136. // CustomTLSConfig.NoDefaultTLSSessionID for all underlying TLS connections
  137. // created by this meek connection.
  138. NoDefaultTLSSessionID bool
  139. // RandomizedTLSProfileSeed specifies the value for
  140. // CustomTLSConfig.RandomizedTLSProfileSeed for all underlying TLS
  141. // connections created by this meek connection.
  142. RandomizedTLSProfileSeed *prng.Seed
  143. // UseObfuscatedSessionTickets indicates whether to use obfuscated session
  144. // tickets. Assumes UseHTTPS is true.
  145. // Ignored for MeekMode[Wrapped]PlaintextRoundTrip.
  146. UseObfuscatedSessionTickets bool
  147. // SNIServerName is the value to place in the TLS/QUIC SNI server_name field
  148. // when HTTPS or QUIC is used.
  149. SNIServerName string
  150. // HostHeader is the value to place in the HTTP request Host header.
  151. HostHeader string
  152. // TransformedHostName records whether a hostname transformation is
  153. // in effect. This value is used for stats reporting.
  154. TransformedHostName bool
  155. // AddPsiphonFrontingHeader specifies whether to add the
  156. // X-Psiphon-Fronting-Address custom header.
  157. AddPsiphonFrontingHeader bool
  158. // VerifyServerName specifies a domain name that must appear in the server
  159. // certificate. When blank, server certificate verification is disabled.
  160. VerifyServerName string
  161. // VerifyPins specifies one or more certificate pin values, one of which must
  162. // appear in the verified server certificate chain. A pin value is the
  163. // base64-encoded SHA2 digest of a certificate's public key. When specified,
  164. // at least one pin must match at least one certificate in the chain, at any
  165. // position; e.g., the root CA may be pinned, or the server certificate,
  166. // etc.
  167. VerifyPins []string
  168. // DisableSystemRootCAs, when true, disables loading system root CAs when
  169. // verifying the server certificate chain. Set DisableSystemRootCAs only in
  170. // cases where system root CAs cannot be loaded and there is additional
  171. // security at the payload level; for example, if unsupported (iOS < 12) or
  172. // insufficient memory (VPN extension on iOS < 15).
  173. //
  174. // When DisableSystemRootCAs is set, both VerifyServerName and VerifyPins
  175. // must not be set.
  176. DisableSystemRootCAs bool
  177. // ClientTunnelProtocol is the protocol the client is using. It's included in
  178. // the meek cookie for optional use by the server, in cases where the server
  179. // cannot unambiguously determine the tunnel protocol. ClientTunnelProtocol
  180. // is used when selecting tactics targeted at specific protocols.
  181. // Ignored for MeekMode[Wrapped]PlaintextRoundTrip.
  182. ClientTunnelProtocol string
  183. // NetworkLatencyMultiplier specifies a custom network latency multiplier to
  184. // apply to client parameters used by this meek connection.
  185. NetworkLatencyMultiplier float64
  186. // The following values are used to create the obfuscated meek cookie.
  187. // Ignored for MeekMode[Wrapped]PlaintextRoundTrip.
  188. MeekCookieEncryptionPublicKey string
  189. MeekObfuscatedKey string
  190. MeekObfuscatorPaddingSeed *prng.Seed
  191. // HTTPTransformerParameters specifies an HTTP transformer to apply to the
  192. // meek connection if it uses HTTP.
  193. HTTPTransformerParameters *transforms.HTTPTransformerParameters
  194. // AdditionalHeaders is a set of additional arbitrary HTTP headers that
  195. // are added to all meek HTTP requests. An additional header is ignored
  196. // when the header name is already present in a meek request.
  197. AdditionalHeaders http.Header
  198. }
  199. // MeekConn is a network connection that tunnels net.Conn flows over HTTP and supports
  200. // "domain fronting". Meek sends client->server flow in HTTP request bodies and
  201. // receives server->client flow in HTTP response bodies. Polling is used to
  202. // approximate full duplex TCP. MeekConn also offers HTTP round trip modes.
  203. //
  204. // Domain fronting is a network obfuscation technique in which the connection to a web
  205. // server, typically a CDN, is indistinguishable from any other HTTPS
  206. // connection to the generic "fronting domain" -- the HTTP Host header is used
  207. // to route the requests to the actual destination. See
  208. // https://trac.torproject.org/projects/tor/wiki/doc/meek for more details.
  209. //
  210. // MeekConn also support unfronted operation, in which connections are made
  211. // without routing through a CDN; and plain HTTP operation, without TLS or
  212. // QUIC, with connection metadata obfuscated in HTTP cookies.
  213. type MeekConn struct {
  214. params *parameters.Parameters
  215. mode MeekMode
  216. networkLatencyMultiplier float64
  217. isQUIC bool
  218. url *url.URL
  219. additionalHeaders http.Header
  220. cookie *http.Cookie
  221. contentType string
  222. cookieSize int
  223. tlsPadding int
  224. limitRequestPayloadLength int
  225. redialTLSProbability float64
  226. transport transporter
  227. connManager *meekUnderlyingConnManager
  228. mutex sync.Mutex
  229. isClosed bool
  230. runCtx context.Context
  231. stopRunning context.CancelFunc
  232. relayWaitGroup *sync.WaitGroup
  233. // For MeekModeObfuscatedRoundTrip
  234. meekCookieEncryptionPublicKey string
  235. meekObfuscatedKey string
  236. meekObfuscatorPaddingSeed *prng.Seed
  237. clientTunnelProtocol string
  238. // For MeekModeRelay
  239. fullReceiveBufferLength int
  240. readPayloadChunkLength int
  241. emptyReceiveBuffer chan *bytes.Buffer
  242. partialReceiveBuffer chan *bytes.Buffer
  243. fullReceiveBuffer chan *bytes.Buffer
  244. emptySendBuffer chan *bytes.Buffer
  245. partialSendBuffer chan *bytes.Buffer
  246. fullSendBuffer chan *bytes.Buffer
  247. }
  248. func (conn *MeekConn) getCustomParameters() parameters.ParametersAccessor {
  249. return conn.params.GetCustom(conn.networkLatencyMultiplier)
  250. }
  251. // transporter is implemented by both http.Transport and upstreamproxy.ProxyAuthTransport.
  252. type transporter interface {
  253. CloseIdleConnections()
  254. RoundTrip(req *http.Request) (resp *http.Response, err error)
  255. }
  256. // DialMeek returns an initialized meek connection. A meek connection is
  257. // an HTTP session which does not depend on an underlying socket connection (although
  258. // persistent HTTP connections are used for performance). This function may not
  259. // wait for the connection to be established before returning.
  260. func DialMeek(
  261. ctx context.Context,
  262. meekConfig *MeekConfig,
  263. dialConfig *DialConfig) (*MeekConn, error) {
  264. if meekConfig.UseQUIC && meekConfig.UseHTTPS {
  265. return nil, errors.TraceNew(
  266. "invalid config: only one of UseQUIC or UseHTTPS may be set")
  267. }
  268. if meekConfig.UseQUIC && meekConfig.QUICTLSClientSessionCache == nil {
  269. return nil, errors.TraceNew(
  270. "invalid config: QUICTLSClientSessionCache must be set when UseQUIC is set")
  271. }
  272. if meekConfig.UseHTTPS && meekConfig.TLSClientSessionCache == nil {
  273. return nil, errors.TraceNew(
  274. "invalid config: TLSClientSessionCache must be set when UseHTTPS is set")
  275. }
  276. if meekConfig.UseQUIC &&
  277. (meekConfig.VerifyServerName != "" || len(meekConfig.VerifyPins) > 0) {
  278. // TODO: UseQUIC VerifyServerName and VerifyPins support (required for MeekModePlaintextRoundTrip).
  279. return nil, errors.TraceNew(
  280. "invalid config: VerifyServerName and VerifyPins not supported for UseQUIC")
  281. }
  282. skipVerify := meekConfig.VerifyServerName == ""
  283. if len(meekConfig.VerifyPins) > 0 && skipVerify {
  284. return nil, errors.TraceNew(
  285. "invalid config: VerifyServerName must be set when VerifyPins is set")
  286. }
  287. if meekConfig.DisableSystemRootCAs &&
  288. (len(meekConfig.VerifyServerName) > 0 || len(meekConfig.VerifyPins) > 0) {
  289. return nil, errors.TraceNew(
  290. "invalid config: VerifyServerName and VerifyPins must not be set when DisableSystemRootCAs is set")
  291. }
  292. if meekConfig.Mode == MeekModePlaintextRoundTrip &&
  293. (!meekConfig.UseHTTPS || (skipVerify && !meekConfig.DisableSystemRootCAs)) {
  294. return nil, errors.TraceNew(
  295. "invalid config: MeekModePlaintextRoundTrip requires UseHTTPS and VerifyServerName when system root CAs can be loaded")
  296. }
  297. runCtx, stopRunning := context.WithCancel(context.Background())
  298. meek := &MeekConn{
  299. params: meekConfig.Parameters,
  300. mode: meekConfig.Mode,
  301. networkLatencyMultiplier: meekConfig.NetworkLatencyMultiplier,
  302. isClosed: false,
  303. runCtx: runCtx,
  304. stopRunning: stopRunning,
  305. relayWaitGroup: new(sync.WaitGroup),
  306. }
  307. cleanupStopRunning := true
  308. cleanupConns := true
  309. // Cleanup in error cases
  310. defer func() {
  311. if cleanupStopRunning {
  312. meek.stopRunning()
  313. }
  314. if cleanupConns && meek.connManager != nil {
  315. meek.connManager.closeAll()
  316. }
  317. }()
  318. if meek.mode == MeekModeRelay {
  319. var err error
  320. meek.cookie,
  321. meek.contentType,
  322. meek.tlsPadding,
  323. meek.limitRequestPayloadLength,
  324. meek.redialTLSProbability,
  325. err =
  326. makeMeekObfuscationValues(
  327. meek.getCustomParameters(),
  328. meekConfig.MeekCookieEncryptionPublicKey,
  329. meekConfig.MeekObfuscatedKey,
  330. meekConfig.MeekObfuscatorPaddingSeed,
  331. meekConfig.ClientTunnelProtocol,
  332. "")
  333. if err != nil {
  334. return nil, errors.Trace(err)
  335. }
  336. // For stats, record the size of the initial obfuscated cookie.
  337. meek.cookieSize = len(meek.cookie.Name) + len(meek.cookie.Value)
  338. }
  339. // Configure transport: QUIC or HTTPS or HTTP
  340. var (
  341. scheme string
  342. opaqueURL string
  343. transport transporter
  344. additionalHeaders http.Header
  345. proxyUrl func(*http.Request) (*url.URL, error)
  346. )
  347. if meekConfig.UseQUIC {
  348. meek.isQUIC = true
  349. scheme = "https"
  350. udpDialer := func(ctx context.Context) (net.PacketConn, *net.UDPAddr, error) {
  351. packetConn, remoteAddr, err := NewUDPConn(
  352. ctx, "udp", false, "", meekConfig.DialAddress, dialConfig)
  353. if err != nil {
  354. return nil, nil, errors.Trace(err)
  355. }
  356. return packetConn, remoteAddr, nil
  357. }
  358. meek.connManager = newMeekUnderlyingConnManager(nil, nil, udpDialer)
  359. // Limitation: currently, the meekUnderlyingPacketConn wrapping done by
  360. // dialPacketConn masks the quic-go.OOBCapablePacketConn capabilities
  361. // of the underlying *net.UDPConn. With these capabilities unavailable,
  362. // path MTU discovery and UDP socket buffer optimizations will be disabled.
  363. var err error
  364. transport, err = quic.NewQUICTransporter(
  365. ctx,
  366. func(message string) {
  367. NoticeInfo(message)
  368. },
  369. meek.connManager.dialPacketConn,
  370. meekConfig.SNIServerName,
  371. meekConfig.QUICVersion,
  372. meekConfig.QUICClientHelloSeed,
  373. meekConfig.QUICDisablePathMTUDiscovery,
  374. meekConfig.QUICDialEarly,
  375. meekConfig.QUICTLSClientSessionCache)
  376. if err != nil {
  377. return nil, errors.Trace(err)
  378. }
  379. } else if meekConfig.UseHTTPS {
  380. // Custom TLS dialer:
  381. //
  382. // 1. ignores the HTTP request address and uses the fronting domain
  383. // 2. optionally disables SNI -- SNI breaks fronting when used with certain CDNs.
  384. // 3. may skip verifying the server cert.
  385. //
  386. // Reasoning for #3:
  387. //
  388. // With a TLS MiM attack in place, and server certs verified, we'll fail to connect because the client
  389. // will refuse to connect. That's not a successful outcome.
  390. //
  391. // With a MiM attack in place, and server certs not verified, we'll fail to connect if the MiM is actively
  392. // targeting Psiphon and classifying the HTTP traffic by Host header or payload signature.
  393. //
  394. // However, in the case of a passive MiM that's just recording traffic or an active MiM that's targeting
  395. // something other than Psiphon, the client will connect. This is a successful outcome.
  396. //
  397. // What is exposed to the MiM? The Host header does not contain a Psiphon server IP address, just an
  398. // unrelated, randomly generated domain name which cannot be used to block direct connections. The
  399. // Psiphon server IP is sent over meek, but it's in the encrypted cookie.
  400. //
  401. // The payload (user traffic) gets its confidentiality and integrity from the underlying SSH protocol.
  402. // So, nothing is leaked to the MiM apart from signatures which could be used to classify the traffic
  403. // as Psiphon to possibly block it; but note that not revealing that the client is Psiphon is outside
  404. // our threat model; we merely seek to evade mass blocking by taking steps that require progressively
  405. // more effort to block.
  406. //
  407. // There is a subtle attack remaining: an adversary that can MiM some CDNs but not others (and so can
  408. // classify Psiphon traffic on some CDNs but not others) may throttle non-MiM CDNs so that our server
  409. // selection always chooses tunnels to the MiM CDN (without any server cert verification, we won't
  410. // exclusively connect to non-MiM CDNs); then the adversary kills the underlying TCP connection after
  411. // some short period. This is partially mitigated by tactics mechanisms.
  412. scheme = "https"
  413. tlsConfig := &CustomTLSConfig{
  414. Parameters: meekConfig.Parameters,
  415. DialAddr: meekConfig.DialAddress,
  416. Dial: NewTCPDialer(dialConfig),
  417. SNIServerName: meekConfig.SNIServerName,
  418. SkipVerify: skipVerify,
  419. VerifyServerName: meekConfig.VerifyServerName,
  420. VerifyPins: meekConfig.VerifyPins,
  421. DisableSystemRootCAs: meekConfig.DisableSystemRootCAs,
  422. TLSProfile: meekConfig.TLSProfile,
  423. NoDefaultTLSSessionID: &meekConfig.NoDefaultTLSSessionID,
  424. RandomizedTLSProfileSeed: meekConfig.RandomizedTLSProfileSeed,
  425. TLSPadding: meek.tlsPadding,
  426. TrustedCACertificatesFilename: dialConfig.TrustedCACertificatesFilename,
  427. FragmentClientHello: meekConfig.TLSFragmentClientHello,
  428. ClientSessionCache: meekConfig.TLSClientSessionCache,
  429. }
  430. if meekConfig.UseObfuscatedSessionTickets {
  431. tlsConfig.ObfuscatedSessionTicketKey = meekConfig.MeekObfuscatedKey
  432. }
  433. if meekConfig.Mode != MeekModePlaintextRoundTrip &&
  434. meekConfig.Mode != MeekModeWrappedPlaintextRoundTrip &&
  435. meekConfig.MeekObfuscatedKey != "" {
  436. // As the passthrough message is unique and indistinguishable from a normal
  437. // TLS client random value, we set it unconditionally and not just for
  438. // protocols which may support passthrough (even for those protocols,
  439. // clients don't know which servers are configured to use it).
  440. passthroughMessage, err := obfuscator.MakeTLSPassthroughMessage(
  441. !meekConfig.LegacyPassthrough,
  442. meekConfig.MeekObfuscatedKey)
  443. if err != nil {
  444. return nil, errors.Trace(err)
  445. }
  446. tlsConfig.PassthroughMessage = passthroughMessage
  447. }
  448. tlsDialer := NewCustomTLSDialer(tlsConfig)
  449. // Pre-dial one TLS connection in order to inspect the negotiated
  450. // application protocol. Then we create an HTTP/2 or HTTP/1.1 transport
  451. // depending on which protocol was negotiated. The TLS dialer
  452. // is assumed to negotiate only "h2" or "http/1.1"; or not negotiate
  453. // an application protocol.
  454. //
  455. // We cannot rely on net/http's HTTP/2 support since it's only
  456. // activated when http.Transport.DialTLS returns a golang crypto/tls.Conn;
  457. // e.g., https://github.com/golang/go/blob/c8aec4095e089ff6ac50d18e97c3f46561f14f48/src/net/http/transport.go#L1040
  458. //
  459. // The pre-dialed connection is stored in a cachedTLSDialer, which will
  460. // return the cached pre-dialed connection to its first Dial caller, and
  461. // use the tlsDialer for all other Dials.
  462. //
  463. // cachedTLSDialer.close() must be called on all exits paths from this
  464. // function and in meek.Close() to ensure the cached conn is closed in
  465. // any case where no Dial call is made.
  466. //
  467. // The pre-dial must be interruptible so that DialMeek doesn't block and
  468. // hang/delay a shutdown or end of establishment. So the pre-dial uses
  469. // the Controller's PendingConns, not the MeekConn PendingConns. For this
  470. // purpose, a special preDialer is configured.
  471. //
  472. // Only one pre-dial attempt is made; there are no retries. This differs
  473. // from relayRoundTrip, which retries and may redial for each retry.
  474. // Retries at the pre-dial phase are less useful since there's no active
  475. // session to preserve, and establishment will simply try another server.
  476. // Note that the underlying TCPDial may still try multiple IP addreses when
  477. // the destination is a domain and it resolves to multiple IP adresses.
  478. //
  479. // The pre-dial is made within the parent dial context, so that DialMeek
  480. // may be interrupted. Subsequent dials are made within the meek round trip
  481. // request context.
  482. // As DialAddr is set in the CustomTLSConfig, no address is required here.
  483. preConn, err := tlsDialer(ctx, "tcp", "")
  484. if err != nil {
  485. return nil, errors.Trace(err)
  486. }
  487. meek.connManager = newMeekUnderlyingConnManager(preConn, tlsDialer, nil)
  488. if IsTLSConnUsingHTTP2(preConn) {
  489. NoticeInfo("negotiated HTTP/2 for %s", meekConfig.DiagnosticID)
  490. transport = &http2.Transport{
  491. DialTLSContext: func(
  492. ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
  493. return meek.connManager.dial(ctx, network, addr)
  494. },
  495. }
  496. } else {
  497. transport = &http.Transport{
  498. DialTLSContext: meek.connManager.dial,
  499. }
  500. }
  501. } else {
  502. scheme = "http"
  503. var dialer common.Dialer
  504. // For HTTP, and when the meekConfig.DialAddress matches the
  505. // meekConfig.HostHeader, we let http.Transport handle proxying.
  506. // http.Transport will put the the HTTP server address in the HTTP
  507. // request line. In this one case, we can use an HTTP proxy that does
  508. // not offer CONNECT support.
  509. if strings.HasPrefix(dialConfig.UpstreamProxyURL, "http://") &&
  510. (meekConfig.DialAddress == meekConfig.HostHeader ||
  511. meekConfig.DialAddress == meekConfig.HostHeader+":80") {
  512. url, err := common.SafeParseURL(dialConfig.UpstreamProxyURL)
  513. if err != nil {
  514. return nil, errors.Trace(err)
  515. }
  516. proxyUrl = http.ProxyURL(url)
  517. // Here, the dialer must use the address that http.Transport
  518. // passes in (which will be proxy address).
  519. copyDialConfig := new(DialConfig)
  520. *copyDialConfig = *dialConfig
  521. copyDialConfig.UpstreamProxyURL = ""
  522. dialer = NewTCPDialer(copyDialConfig)
  523. // In this proxy case, the destination server address is in the
  524. // request line URL. net/http will render the request line using
  525. // the URL but preferring the Host header for the host value,
  526. // which means any custom host header will clobber the true
  527. // destination address. The URL.Opaque logic is applied in this
  528. // case, to force the request line URL value.
  529. //
  530. // This URL.Opaque setting assumes MeekModeRelay, with no path; at
  531. // this time plain HTTP is used only with MeekModeRelay.
  532. // x/net/http2 will reject requests where the URL.Opaque contains
  533. // more than the path; but HTTP/2 is not used in this case.
  534. values := dialConfig.CustomHeaders["Host"]
  535. if len(values) > 0 {
  536. opaqueURL = "http://" + meekConfig.DialAddress + "/"
  537. }
  538. } else {
  539. // If dialConfig.UpstreamProxyURL is set, HTTP proxying via
  540. // CONNECT will be used by the dialer.
  541. baseDialer := NewTCPDialer(dialConfig)
  542. // The dialer ignores any address that http.Transport will pass in
  543. // (derived from the HTTP request URL) and always dials
  544. // meekConfig.DialAddress.
  545. dialer = func(ctx context.Context, network, _ string) (net.Conn, error) {
  546. return baseDialer(ctx, network, meekConfig.DialAddress)
  547. }
  548. }
  549. if protocol.TunnelProtocolUsesMeekHTTP(meekConfig.ClientTunnelProtocol) {
  550. // Only apply transformer if it will perform a transform; otherwise
  551. // applying a no-op transform will incur an unnecessary performance
  552. // cost.
  553. if meekConfig.HTTPTransformerParameters != nil &&
  554. meekConfig.HTTPTransformerParameters.ProtocolTransformSpec != nil {
  555. dialer = transforms.WrapDialerWithHTTPTransformer(
  556. dialer, meekConfig.HTTPTransformerParameters)
  557. }
  558. }
  559. meek.connManager = newMeekUnderlyingConnManager(nil, dialer, nil)
  560. httpTransport := &http.Transport{
  561. Proxy: proxyUrl,
  562. DialContext: meek.connManager.dial,
  563. }
  564. if proxyUrl != nil {
  565. // When http.Transport is handling proxying, wrap transport with a
  566. // transport that (a) adds custom headers; (b) can perform HTTP
  567. // proxy auth negotiation.
  568. var err error
  569. transport, err = upstreamproxy.NewProxyAuthTransport(
  570. httpTransport, dialConfig.CustomHeaders)
  571. if err != nil {
  572. return nil, errors.Trace(err)
  573. }
  574. } else {
  575. transport = httpTransport
  576. }
  577. }
  578. url := &url.URL{
  579. Scheme: scheme,
  580. Host: meekConfig.HostHeader,
  581. Path: "/",
  582. Opaque: opaqueURL,
  583. }
  584. if scheme == "http" && proxyUrl == nil {
  585. // Add custom headers to HTTP. This may be unproxied HTTP, or CONNECT
  586. // method proxied HTTP, which is handled implicitly by DialTCP (in the
  587. // latter case, the CONNECT request itself will also have custom
  588. // headers via upstreamproxy applied by the dialer).
  589. //
  590. // When proxyUrl != nil, proxying is handled by http.Transport and
  591. // custom headers are set in upstreamproxy.NewProxyAuthTransport, above.
  592. additionalHeaders = dialConfig.CustomHeaders
  593. } else {
  594. additionalHeaders = make(http.Header)
  595. // User-Agent is passed in via dialConfig.CustomHeaders. Always use
  596. // any User-Agent header, even when not using all custom headers.
  597. userAgent := dialConfig.CustomHeaders.Get("User-Agent")
  598. if userAgent != "" {
  599. additionalHeaders.Set("User-Agent", userAgent)
  600. }
  601. }
  602. if meekConfig.AddPsiphonFrontingHeader {
  603. host, _, err := net.SplitHostPort(meekConfig.DialAddress)
  604. if err != nil {
  605. return nil, errors.Trace(err)
  606. }
  607. additionalHeaders.Set("X-Psiphon-Fronting-Address", host)
  608. }
  609. if meekConfig.AdditionalHeaders != nil {
  610. for name, value := range meekConfig.AdditionalHeaders {
  611. if _, ok := additionalHeaders[name]; !ok {
  612. additionalHeaders[name] = value
  613. }
  614. }
  615. }
  616. meek.url = url
  617. meek.additionalHeaders = additionalHeaders
  618. meek.transport = transport
  619. // stopRunning and cachedTLSDialer will now be closed in meek.Close()
  620. cleanupStopRunning = false
  621. cleanupConns = false
  622. // Allocate relay resources, including buffers and running the relay
  623. // go routine, only when running in relay mode.
  624. if meek.mode == MeekModeRelay {
  625. // The main loop of a MeekConn is run in the relay() goroutine.
  626. // A MeekConn implements net.Conn concurrency semantics:
  627. // "Multiple goroutines may invoke methods on a Conn simultaneously."
  628. //
  629. // Read() calls and relay() are synchronized by exchanging control of a single
  630. // receiveBuffer (bytes.Buffer). This single buffer may be:
  631. // - in the emptyReceiveBuffer channel when it is available and empty;
  632. // - in the partialReadBuffer channel when it is available and contains data;
  633. // - in the fullReadBuffer channel when it is available and full of data;
  634. // - "checked out" by relay or Read when they are are writing to or reading from the
  635. // buffer, respectively.
  636. // relay() will obtain the buffer from either the empty or partial channel but block when
  637. // the buffer is full. Read will obtain the buffer from the partial or full channel when
  638. // there is data to read but block when the buffer is empty.
  639. // Write() calls and relay() are synchronized in a similar way, using a single
  640. // sendBuffer.
  641. p := meek.getCustomParameters()
  642. if p.Bool(parameters.MeekLimitBufferSizes) {
  643. meek.fullReceiveBufferLength = p.Int(parameters.MeekLimitedFullReceiveBufferLength)
  644. meek.readPayloadChunkLength = p.Int(parameters.MeekLimitedReadPayloadChunkLength)
  645. } else {
  646. meek.fullReceiveBufferLength = p.Int(parameters.MeekFullReceiveBufferLength)
  647. meek.readPayloadChunkLength = p.Int(parameters.MeekReadPayloadChunkLength)
  648. }
  649. meek.emptyReceiveBuffer = make(chan *bytes.Buffer, 1)
  650. meek.partialReceiveBuffer = make(chan *bytes.Buffer, 1)
  651. meek.fullReceiveBuffer = make(chan *bytes.Buffer, 1)
  652. meek.emptySendBuffer = make(chan *bytes.Buffer, 1)
  653. meek.partialSendBuffer = make(chan *bytes.Buffer, 1)
  654. meek.fullSendBuffer = make(chan *bytes.Buffer, 1)
  655. meek.emptyReceiveBuffer <- new(bytes.Buffer)
  656. meek.emptySendBuffer <- new(bytes.Buffer)
  657. meek.relayWaitGroup.Add(1)
  658. go meek.relay()
  659. } else if meek.mode == MeekModeObfuscatedRoundTrip {
  660. meek.meekCookieEncryptionPublicKey = meekConfig.MeekCookieEncryptionPublicKey
  661. meek.meekObfuscatedKey = meekConfig.MeekObfuscatedKey
  662. meek.meekObfuscatorPaddingSeed = meekConfig.MeekObfuscatorPaddingSeed
  663. meek.clientTunnelProtocol = meekConfig.ClientTunnelProtocol
  664. } else if meek.mode == MeekModePlaintextRoundTrip ||
  665. meek.mode == MeekModeWrappedPlaintextRoundTrip {
  666. // MeekModeRelay and MeekModeObfuscatedRoundTrip set the Host header
  667. // implicitly via meek.url; MeekMode[Wrapped]PlaintextRoundTrip does
  668. // not use meek.url; it uses the RoundTrip input request.URL instead.
  669. // So the Host header is set to meekConfig.HostHeader explicitly here.
  670. meek.additionalHeaders.Add("Host", meekConfig.HostHeader)
  671. }
  672. return meek, nil
  673. }
  674. type meekPacketConnDialer func(ctx context.Context) (net.PacketConn, *net.UDPAddr, error)
  675. // meekUnderlyingConnManager tracks the TCP/TLS and UDP connections underlying
  676. // the meek HTTP/HTTPS/QUIC transports. This tracking is used to:
  677. //
  678. // - Use the cached predial TLS conn created in DialMeek.
  679. // - Gather metrics from mechanisms enabled in the underlying conns, such as
  680. // the fragmentor, or inproxy.
  681. // - Fully close all underlying connections with the MeekConn is closed.
  682. type meekUnderlyingConnManager struct {
  683. mutex sync.Mutex
  684. cachedConn net.Conn
  685. firstConn net.Conn
  686. firstPacketConn net.PacketConn
  687. dialer common.Dialer
  688. managedConns *common.Conns[net.Conn]
  689. packetConnDialer meekPacketConnDialer
  690. managedPacketConns *common.Conns[net.PacketConn]
  691. }
  692. type meekUnderlyingConn struct {
  693. net.Conn
  694. connManager *meekUnderlyingConnManager
  695. }
  696. func (conn *meekUnderlyingConn) Close() error {
  697. conn.connManager.managedConns.Remove(conn)
  698. // Note: no trace error to preserve error type
  699. return conn.Conn.Close()
  700. }
  701. type meekUnderlyingPacketConn struct {
  702. net.PacketConn
  703. connManager *meekUnderlyingConnManager
  704. }
  705. func (packetConn *meekUnderlyingPacketConn) Close() error {
  706. packetConn.connManager.managedPacketConns.Remove(packetConn)
  707. return packetConn.PacketConn.Close()
  708. }
  709. func newMeekUnderlyingConnManager(
  710. cachedConn net.Conn,
  711. dialer common.Dialer,
  712. packetConnDialer meekPacketConnDialer) *meekUnderlyingConnManager {
  713. m := &meekUnderlyingConnManager{
  714. dialer: dialer,
  715. managedConns: common.NewConns[net.Conn](),
  716. packetConnDialer: packetConnDialer,
  717. managedPacketConns: common.NewConns[net.PacketConn](),
  718. }
  719. if cachedConn != nil {
  720. m.cachedConn = &meekUnderlyingConn{Conn: cachedConn, connManager: m}
  721. m.firstConn = cachedConn
  722. }
  723. return m
  724. }
  725. func (m *meekUnderlyingConnManager) GetMetrics() common.LogFields {
  726. logFields := common.LogFields{}
  727. m.mutex.Lock()
  728. underlyingMetrics, ok := m.firstConn.(common.MetricsSource)
  729. if ok {
  730. logFields.Add(underlyingMetrics.GetMetrics())
  731. }
  732. underlyingMetrics, ok = m.firstPacketConn.(common.MetricsSource)
  733. if ok {
  734. logFields.Add(underlyingMetrics.GetMetrics())
  735. }
  736. m.mutex.Unlock()
  737. return logFields
  738. }
  739. func (m *meekUnderlyingConnManager) dial(
  740. ctx context.Context, network, addr string) (net.Conn, error) {
  741. if m.managedConns.IsClosed() {
  742. return nil, errors.TraceNew("closed")
  743. }
  744. // Consume the cached conn when present.
  745. m.mutex.Lock()
  746. var conn net.Conn
  747. if m.cachedConn != nil {
  748. conn = m.cachedConn
  749. m.cachedConn = nil
  750. }
  751. m.mutex.Unlock()
  752. if conn != nil {
  753. return conn, nil
  754. }
  755. // The mutex lock is not held for the duration of dial, allowing for
  756. // concurrent dials.
  757. conn, err := m.dialer(ctx, network, addr)
  758. if err != nil {
  759. // Note: no trace error to preserve error type
  760. return nil, err
  761. }
  762. // Keep a reference to the first underlying conn to be used as a
  763. // common.MetricsSource in GetMetrics. This enables capturing metrics
  764. // such as fragmentor configuration.
  765. m.mutex.Lock()
  766. if m.firstConn == nil {
  767. m.firstConn = conn
  768. }
  769. m.mutex.Unlock()
  770. // Wrap the dialed conn with meekUnderlyingConn, which will remove the
  771. // conn from the set of tracked conns when the conn is closed.
  772. conn = &meekUnderlyingConn{Conn: conn, connManager: m}
  773. if !m.managedConns.Add(conn) {
  774. _ = conn.Close()
  775. return nil, errors.TraceNew("closed")
  776. }
  777. return conn, nil
  778. }
  779. func (m *meekUnderlyingConnManager) dialPacketConn(
  780. ctx context.Context) (net.PacketConn, *net.UDPAddr, error) {
  781. if m.managedPacketConns.IsClosed() {
  782. return nil, nil, errors.TraceNew("closed")
  783. }
  784. packetConn, addr, err := m.packetConnDialer(ctx)
  785. if err != nil {
  786. // Note: no trace error to preserve error type
  787. return nil, nil, err
  788. }
  789. m.mutex.Lock()
  790. if m.firstPacketConn != nil {
  791. m.firstPacketConn = packetConn
  792. }
  793. m.mutex.Unlock()
  794. packetConn = &meekUnderlyingPacketConn{PacketConn: packetConn, connManager: m}
  795. if !m.managedPacketConns.Add(packetConn) {
  796. _ = packetConn.Close()
  797. return nil, nil, errors.TraceNew("closed")
  798. }
  799. return packetConn, addr, nil
  800. }
  801. func (m *meekUnderlyingConnManager) closeAll() {
  802. m.managedConns.CloseAll()
  803. m.managedPacketConns.CloseAll()
  804. }
  805. // Close terminates the meek connection and releases its resources. In in
  806. // MeekModeRelay, Close waits for the relay goroutine to stop.
  807. func (meek *MeekConn) Close() (err error) {
  808. // A mutex is required to support net.Conn concurrency semantics.
  809. meek.mutex.Lock()
  810. isClosed := meek.isClosed
  811. meek.isClosed = true
  812. meek.mutex.Unlock()
  813. if !isClosed {
  814. meek.stopRunning()
  815. meek.connManager.closeAll()
  816. meek.relayWaitGroup.Wait()
  817. // meek.transport.CloseIdleConnections is no longed called here since
  818. // meekUnderlyingConnManager.closeAll will terminate all underlying
  819. // connections and prevent opening any new connections.
  820. }
  821. return nil
  822. }
  823. // IsClosed implements the Closer interface. The return value
  824. // indicates whether the MeekConn has been closed.
  825. func (meek *MeekConn) IsClosed() bool {
  826. meek.mutex.Lock()
  827. isClosed := meek.isClosed
  828. meek.mutex.Unlock()
  829. return isClosed
  830. }
  831. // GetMetrics implements the common.MetricsSource interface.
  832. func (meek *MeekConn) GetMetrics() common.LogFields {
  833. logFields := make(common.LogFields)
  834. if meek.mode == MeekModeRelay {
  835. logFields["meek_cookie_size"] = meek.cookieSize
  836. logFields["meek_tls_padding"] = meek.tlsPadding
  837. logFields["meek_limit_request"] = meek.limitRequestPayloadLength
  838. logFields["meek_redial_probability"] = meek.redialTLSProbability
  839. }
  840. // Include metrics, such as fragmentor metrics, from the _first_ underlying
  841. // dial conn. Properties of subsequent underlying dial conns are not reflected
  842. // in these metrics; we assume that the first dial conn, which most likely
  843. // transits the various protocol handshakes, is most significant.
  844. logFields.Add(meek.connManager.GetMetrics())
  845. return logFields
  846. }
  847. // GetNoticeMetrics implements the common.NoticeMetricsSource interface.
  848. func (meek *MeekConn) GetNoticeMetrics() common.LogFields {
  849. // These fields are logged only in notices, for diagnostics. The server
  850. // will log the same values, but derives them from HTTP headers, so they
  851. // don't need to be sent in the API request.
  852. logFields := make(common.LogFields)
  853. logFields["meek_cookie_name"] = meek.cookie.Name
  854. logFields["meek_content_type"] = meek.contentType
  855. return logFields
  856. }
  857. // ObfuscatedRoundTrip makes a request to the meek server and returns the
  858. // response. A new, obfuscated meek cookie is created for every request. The
  859. // specified end point is recorded in the cookie and is not exposed as
  860. // plaintext in the meek traffic. The caller is responsible for securing and
  861. // obfuscating the request body.
  862. //
  863. // If Close is called before or concurrent with ObfuscatedRoundTrip, or before
  864. // the response body is read, idle connections may be left open.
  865. func (meek *MeekConn) ObfuscatedRoundTrip(
  866. requestCtx context.Context, endPoint string, requestBody []byte) ([]byte, error) {
  867. if meek.mode != MeekModeObfuscatedRoundTrip {
  868. return nil, errors.TraceNew("operation unsupported")
  869. }
  870. cookie, contentType, _, _, _, err := makeMeekObfuscationValues(
  871. meek.getCustomParameters(),
  872. meek.meekCookieEncryptionPublicKey,
  873. meek.meekObfuscatedKey,
  874. meek.meekObfuscatorPaddingSeed,
  875. meek.clientTunnelProtocol,
  876. endPoint)
  877. if err != nil {
  878. return nil, errors.Trace(err)
  879. }
  880. request, err := meek.newRequest(
  881. requestCtx, cookie, contentType, bytes.NewReader(requestBody), 0)
  882. if err != nil {
  883. return nil, errors.Trace(err)
  884. }
  885. meek.scheduleQUICCloseIdle(request)
  886. response, err := meek.transport.RoundTrip(request)
  887. if err == nil {
  888. defer response.Body.Close()
  889. if response.StatusCode != http.StatusOK {
  890. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  891. }
  892. }
  893. if err != nil {
  894. return nil, errors.Trace(err)
  895. }
  896. responseBody, err := ioutil.ReadAll(response.Body)
  897. if err != nil {
  898. return nil, errors.Trace(err)
  899. }
  900. return responseBody, nil
  901. }
  902. // RoundTrip implements the http.RoundTripper interface. RoundTrip may only be
  903. // used when TLS and server certificate verification are configured. RoundTrip
  904. // does not implement any security or obfuscation at the HTTP layer.
  905. //
  906. // If Close is called before or concurrent with RoundTrip, or before the
  907. // response body is read, idle connections may be left open.
  908. func (meek *MeekConn) RoundTrip(request *http.Request) (*http.Response, error) {
  909. if meek.mode != MeekModePlaintextRoundTrip &&
  910. meek.mode != MeekModeWrappedPlaintextRoundTrip {
  911. return nil, errors.TraceNew("operation unsupported")
  912. }
  913. requestCtx := request.Context()
  914. // Clone the request to apply addtional headers without modifying the input.
  915. request = request.Clone(requestCtx)
  916. meek.addAdditionalHeaders(request)
  917. meek.scheduleQUICCloseIdle(request)
  918. response, err := meek.transport.RoundTrip(request)
  919. if err != nil {
  920. return nil, errors.Trace(err)
  921. }
  922. return response, nil
  923. }
  924. // Read reads data from the connection.
  925. // net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported.
  926. func (meek *MeekConn) Read(buffer []byte) (n int, err error) {
  927. if meek.mode != MeekModeRelay {
  928. return 0, errors.TraceNew("operation unsupported")
  929. }
  930. if meek.IsClosed() {
  931. return 0, errors.TraceNew("meek connection is closed")
  932. }
  933. // Block until there is received data to consume
  934. var receiveBuffer *bytes.Buffer
  935. select {
  936. case receiveBuffer = <-meek.partialReceiveBuffer:
  937. case receiveBuffer = <-meek.fullReceiveBuffer:
  938. case <-meek.runCtx.Done():
  939. return 0, errors.TraceNew("meek connection has closed")
  940. }
  941. n, err = receiveBuffer.Read(buffer)
  942. meek.replaceReceiveBuffer(receiveBuffer)
  943. return n, err
  944. }
  945. // Write writes data to the connection.
  946. // net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported.
  947. func (meek *MeekConn) Write(buffer []byte) (n int, err error) {
  948. if meek.mode != MeekModeRelay {
  949. return 0, errors.TraceNew("operation unsupported")
  950. }
  951. if meek.IsClosed() {
  952. return 0, errors.TraceNew("meek connection is closed")
  953. }
  954. // Repeats until all n bytes are written
  955. n = len(buffer)
  956. for len(buffer) > 0 {
  957. // Block until there is capacity in the send buffer
  958. var sendBuffer *bytes.Buffer
  959. select {
  960. case sendBuffer = <-meek.emptySendBuffer:
  961. case sendBuffer = <-meek.partialSendBuffer:
  962. case <-meek.runCtx.Done():
  963. return 0, errors.TraceNew("meek connection has closed")
  964. }
  965. writeLen := meek.limitRequestPayloadLength - sendBuffer.Len()
  966. if writeLen > 0 {
  967. if writeLen > len(buffer) {
  968. writeLen = len(buffer)
  969. }
  970. _, err = sendBuffer.Write(buffer[:writeLen])
  971. buffer = buffer[writeLen:]
  972. }
  973. meek.replaceSendBuffer(sendBuffer)
  974. }
  975. return n, err
  976. }
  977. // LocalAddr is a stub implementation of net.Conn.LocalAddr
  978. func (meek *MeekConn) LocalAddr() net.Addr {
  979. return nil
  980. }
  981. // RemoteAddr is a stub implementation of net.Conn.RemoteAddr
  982. func (meek *MeekConn) RemoteAddr() net.Addr {
  983. return nil
  984. }
  985. // SetDeadline is a stub implementation of net.Conn.SetDeadline
  986. func (meek *MeekConn) SetDeadline(t time.Time) error {
  987. return errors.TraceNew("not supported")
  988. }
  989. // SetReadDeadline is a stub implementation of net.Conn.SetReadDeadline
  990. func (meek *MeekConn) SetReadDeadline(t time.Time) error {
  991. return errors.TraceNew("not supported")
  992. }
  993. // SetWriteDeadline is a stub implementation of net.Conn.SetWriteDeadline
  994. func (meek *MeekConn) SetWriteDeadline(t time.Time) error {
  995. return errors.TraceNew("not supported")
  996. }
  997. func (meek *MeekConn) replaceReceiveBuffer(receiveBuffer *bytes.Buffer) {
  998. switch {
  999. case receiveBuffer.Len() == 0:
  1000. meek.emptyReceiveBuffer <- receiveBuffer
  1001. case receiveBuffer.Len() >= meek.fullReceiveBufferLength:
  1002. meek.fullReceiveBuffer <- receiveBuffer
  1003. default:
  1004. meek.partialReceiveBuffer <- receiveBuffer
  1005. }
  1006. }
  1007. func (meek *MeekConn) replaceSendBuffer(sendBuffer *bytes.Buffer) {
  1008. switch {
  1009. case sendBuffer.Len() == 0:
  1010. meek.emptySendBuffer <- sendBuffer
  1011. case sendBuffer.Len() >= meek.limitRequestPayloadLength:
  1012. meek.fullSendBuffer <- sendBuffer
  1013. default:
  1014. meek.partialSendBuffer <- sendBuffer
  1015. }
  1016. }
  1017. // relay sends and receives tunneled traffic (payload). An HTTP request is
  1018. // triggered when data is in the write queue or at a polling interval.
  1019. // There's a geometric increase, up to a maximum, in the polling interval when
  1020. // no data is exchanged. Only one HTTP request is in flight at a time.
  1021. func (meek *MeekConn) relay() {
  1022. // Note: meek.Close() calls here in relay() are made asynchronously
  1023. // (using goroutines) since Close() will wait on this WaitGroup.
  1024. defer meek.relayWaitGroup.Done()
  1025. p := meek.getCustomParameters()
  1026. interval := prng.JitterDuration(
  1027. p.Duration(parameters.MeekMinPollInterval),
  1028. p.Float(parameters.MeekMinPollIntervalJitter))
  1029. p.Close()
  1030. timeout := time.NewTimer(interval)
  1031. defer timeout.Stop()
  1032. for {
  1033. timeout.Reset(interval)
  1034. // Block until there is payload to send or it is time to poll
  1035. var sendBuffer *bytes.Buffer
  1036. select {
  1037. case sendBuffer = <-meek.partialSendBuffer:
  1038. case sendBuffer = <-meek.fullSendBuffer:
  1039. case <-timeout.C:
  1040. // In the polling case, send an empty payload
  1041. case <-meek.runCtx.Done():
  1042. // Drop through to second Done() check
  1043. }
  1044. // Check Done() again, to ensure it takes precedence
  1045. select {
  1046. case <-meek.runCtx.Done():
  1047. return
  1048. default:
  1049. }
  1050. sendPayloadSize := 0
  1051. if sendBuffer != nil {
  1052. sendPayloadSize = sendBuffer.Len()
  1053. }
  1054. // relayRoundTrip will replace sendBuffer (by calling replaceSendBuffer). This
  1055. // is a compromise to conserve memory. Using a second buffer here, we could
  1056. // copy sendBuffer and immediately replace it, unblocking meekConn.Write() and
  1057. // allowing more upstream payload to immediately enqueue. Instead, the request
  1058. // payload is read directly from sendBuffer, including retries. Only once the
  1059. // server has acknowledged the request payload is sendBuffer replaced. This
  1060. // still allows meekConn.Write() to unblock before the round trip response is
  1061. // read.
  1062. receivedPayloadSize, err := meek.relayRoundTrip(sendBuffer)
  1063. if err != nil {
  1064. select {
  1065. case <-meek.runCtx.Done():
  1066. // In this case, meek.relayRoundTrip encountered Done(). Exit without
  1067. // logging error.
  1068. return
  1069. default:
  1070. }
  1071. NoticeWarning("%s", errors.Trace(err))
  1072. go meek.Close()
  1073. return
  1074. }
  1075. // Periodically re-dial the underlying TLS/TCP connection
  1076. // (notwithstanding the parameter name, this also applies to TCP
  1077. // connections for HTTP protocols).
  1078. if prng.FlipWeightedCoin(meek.redialTLSProbability) {
  1079. meek.transport.CloseIdleConnections()
  1080. }
  1081. // Calculate polling interval. When data is received,
  1082. // immediately request more. Otherwise, schedule next
  1083. // poll with exponential back off. Jitter and coin
  1084. // flips are used to avoid trivial, static traffic
  1085. // timing patterns.
  1086. p := meek.getCustomParameters()
  1087. if receivedPayloadSize > 0 || sendPayloadSize > 0 {
  1088. interval = 0
  1089. } else if interval == 0 {
  1090. interval = prng.JitterDuration(
  1091. p.Duration(parameters.MeekMinPollInterval),
  1092. p.Float(parameters.MeekMinPollIntervalJitter))
  1093. } else {
  1094. if p.WeightedCoinFlip(parameters.MeekApplyPollIntervalMultiplierProbability) {
  1095. interval =
  1096. time.Duration(float64(interval) *
  1097. p.Float(parameters.MeekPollIntervalMultiplier))
  1098. }
  1099. interval = prng.JitterDuration(
  1100. interval,
  1101. p.Float(parameters.MeekPollIntervalJitter))
  1102. if interval >= p.Duration(parameters.MeekMaxPollInterval) {
  1103. interval = prng.JitterDuration(
  1104. p.Duration(parameters.MeekMaxPollInterval),
  1105. p.Float(parameters.MeekMaxPollIntervalJitter))
  1106. }
  1107. }
  1108. p.Close()
  1109. }
  1110. }
  1111. // readCloseSignaller is an io.ReadCloser wrapper for an io.Reader
  1112. // that is passed, as the request body, to http.Transport.RoundTrip.
  1113. // readCloseSignaller adds the AwaitClosed call, which is used
  1114. // to schedule recycling the buffer underlying the reader only after
  1115. // RoundTrip has called Close and will no longer use the buffer.
  1116. // See: https://golang.org/pkg/net/http/#RoundTripper
  1117. type readCloseSignaller struct {
  1118. context context.Context
  1119. reader io.Reader
  1120. closed chan struct{}
  1121. }
  1122. func NewReadCloseSignaller(
  1123. context context.Context,
  1124. reader io.Reader) *readCloseSignaller {
  1125. return &readCloseSignaller{
  1126. context: context,
  1127. reader: reader,
  1128. closed: make(chan struct{}, 1),
  1129. }
  1130. }
  1131. func (r *readCloseSignaller) Read(p []byte) (int, error) {
  1132. return r.reader.Read(p)
  1133. }
  1134. func (r *readCloseSignaller) Close() error {
  1135. select {
  1136. case r.closed <- struct{}{}:
  1137. default:
  1138. }
  1139. return nil
  1140. }
  1141. func (r *readCloseSignaller) AwaitClosed() bool {
  1142. select {
  1143. case <-r.context.Done():
  1144. case <-r.closed:
  1145. return true
  1146. }
  1147. return false
  1148. }
  1149. // newRequest performs common request setup for both MeekModeRelay and
  1150. // MeekModeObfuscatedRoundTrip.
  1151. //
  1152. // newRequest is not safe for concurrent calls due to its use of
  1153. // setRequestContext.
  1154. //
  1155. // The caller must call the returned cancelFunc.
  1156. func (meek *MeekConn) newRequest(
  1157. requestCtx context.Context,
  1158. cookie *http.Cookie,
  1159. contentType string,
  1160. body io.Reader,
  1161. contentLength int) (*http.Request, error) {
  1162. request, err := http.NewRequest("POST", meek.url.String(), body)
  1163. if err != nil {
  1164. return nil, errors.Trace(err)
  1165. }
  1166. request = request.WithContext(requestCtx)
  1167. // Content-Length may not be be set automatically due to the
  1168. // underlying type of requestBody.
  1169. if contentLength > 0 {
  1170. request.ContentLength = int64(contentLength)
  1171. }
  1172. meek.addAdditionalHeaders(request)
  1173. request.Header.Set("Content-Type", contentType)
  1174. if cookie == nil {
  1175. cookie = meek.cookie
  1176. }
  1177. request.AddCookie(cookie)
  1178. return request, nil
  1179. }
  1180. // Workaround for h2quic.RoundTripper context issue. See comment in
  1181. // MeekConn.Close.
  1182. func (meek *MeekConn) scheduleQUICCloseIdle(request *http.Request) {
  1183. requestCtx := request.Context()
  1184. if meek.isQUIC && requestCtx != context.Background() {
  1185. go func() {
  1186. <-requestCtx.Done()
  1187. meek.transport.CloseIdleConnections()
  1188. }()
  1189. }
  1190. }
  1191. // relayRoundTrip configures and makes the actual HTTP POST request
  1192. func (meek *MeekConn) relayRoundTrip(sendBuffer *bytes.Buffer) (int64, error) {
  1193. // Retries are made when the round trip fails. This adds resiliency
  1194. // to connection interruption and intermittent failures.
  1195. //
  1196. // At least one retry is always attempted, and retries continue
  1197. // while still within a brief deadline -- 5 seconds, currently the
  1198. // deadline for an actively probed SSH connection to timeout. There
  1199. // is a brief delay between retries, allowing for intermittent
  1200. // failure states to resolve.
  1201. //
  1202. // Failure may occur at various stages of the HTTP request:
  1203. //
  1204. // 1. Before the request begins. In this case, the entire request
  1205. // may be rerun.
  1206. //
  1207. // 2. While sending the request payload. In this case, the client
  1208. // must resend its request payload. The server will not have
  1209. // relayed its partially received request payload.
  1210. //
  1211. // 3. After sending the request payload but before receiving
  1212. // a response. The client cannot distinguish between case 2 and
  1213. // this case, case 3. The client resends its payload and the
  1214. // server detects this and skips relaying the request payload.
  1215. //
  1216. // 4. While reading the response payload. The client will omit its
  1217. // request payload when retrying, as the server has already
  1218. // acknowledged it. The client will also indicate to the server
  1219. // the amount of response payload already received, and the
  1220. // server will skip resending the indicated amount of response
  1221. // payload.
  1222. //
  1223. // Retries are indicated to the server by adding a Range header,
  1224. // which includes the response payload resend position.
  1225. defer func() {
  1226. // Ensure sendBuffer is replaced, even in error code paths.
  1227. if sendBuffer != nil {
  1228. sendBuffer.Truncate(0)
  1229. meek.replaceSendBuffer(sendBuffer)
  1230. }
  1231. }()
  1232. retries := uint(0)
  1233. p := meek.getCustomParameters()
  1234. retryDeadline := time.Now().Add(p.Duration(parameters.MeekRoundTripRetryDeadline))
  1235. retryDelay := p.Duration(parameters.MeekRoundTripRetryMinDelay)
  1236. retryMaxDelay := p.Duration(parameters.MeekRoundTripRetryMaxDelay)
  1237. retryMultiplier := p.Float(parameters.MeekRoundTripRetryMultiplier)
  1238. p.Close()
  1239. serverAcknowledgedRequestPayload := false
  1240. receivedPayloadSize := int64(0)
  1241. for try := 0; ; try++ {
  1242. // Omit the request payload when retrying after receiving a
  1243. // partial server response.
  1244. var signaller *readCloseSignaller
  1245. var requestBody io.ReadCloser
  1246. contentLength := 0
  1247. if !serverAcknowledgedRequestPayload && sendBuffer != nil {
  1248. // sendBuffer will be replaced once the data is no longer needed,
  1249. // when RoundTrip calls Close on the Body; this allows meekConn.Write()
  1250. // to unblock and start buffering data for the next roung trip while
  1251. // still reading the current round trip response. signaller provides
  1252. // the hook for awaiting RoundTrip's call to Close.
  1253. signaller = NewReadCloseSignaller(meek.runCtx, bytes.NewReader(sendBuffer.Bytes()))
  1254. requestBody = signaller
  1255. contentLength = sendBuffer.Len()
  1256. }
  1257. // - meek.stopRunning() will abort a round trip in flight
  1258. // - round trip will abort if it exceeds timeout
  1259. requestCtx, cancelFunc := context.WithTimeout(
  1260. meek.runCtx,
  1261. meek.getCustomParameters().Duration(parameters.MeekRoundTripTimeout))
  1262. defer cancelFunc()
  1263. request, err := meek.newRequest(
  1264. requestCtx,
  1265. nil,
  1266. meek.contentType,
  1267. requestBody,
  1268. contentLength)
  1269. if err != nil {
  1270. // Don't retry when can't initialize a Request
  1271. return 0, errors.Trace(err)
  1272. }
  1273. expectedStatusCode := http.StatusOK
  1274. // When retrying, add a Range header to indicate how much
  1275. // of the response was already received.
  1276. if try > 0 {
  1277. expectedStatusCode = http.StatusPartialContent
  1278. request.Header.Set("Range", fmt.Sprintf("bytes=%d-", receivedPayloadSize))
  1279. }
  1280. response, err := meek.transport.RoundTrip(request)
  1281. // Wait for RoundTrip to call Close on the request body, when
  1282. // there is one. This is necessary to ensure it's safe to
  1283. // subsequently replace sendBuffer in both the success and
  1284. // error cases.
  1285. if signaller != nil {
  1286. if !signaller.AwaitClosed() {
  1287. // AwaitClosed encountered Done(). Abort immediately. Do not
  1288. // replace sendBuffer, as we cannot be certain RoundTrip is
  1289. // done with it. MeekConn.Write will exit on Done and not hang
  1290. // awaiting sendBuffer.
  1291. sendBuffer = nil
  1292. return 0, errors.TraceNew("meek connection has closed")
  1293. }
  1294. }
  1295. if err != nil {
  1296. select {
  1297. case <-meek.runCtx.Done():
  1298. // Exit without retrying and without logging error.
  1299. return 0, errors.Trace(err)
  1300. default:
  1301. }
  1302. NoticeWarning("meek round trip failed: %s", err)
  1303. // ...continue to retry
  1304. }
  1305. if err == nil {
  1306. if response.StatusCode != expectedStatusCode &&
  1307. // Certain http servers return 200 OK where we expect 206, so accept that.
  1308. !(expectedStatusCode == http.StatusPartialContent && response.StatusCode == http.StatusOK) {
  1309. // Don't retry when the status code is incorrect
  1310. response.Body.Close()
  1311. return 0, errors.Tracef(
  1312. "unexpected status code: %d instead of %d",
  1313. response.StatusCode, expectedStatusCode)
  1314. }
  1315. // Update meek session cookie
  1316. for _, c := range response.Cookies() {
  1317. if meek.cookie.Name == c.Name {
  1318. meek.cookie.Value = c.Value
  1319. break
  1320. }
  1321. }
  1322. // Received the response status code, so the server
  1323. // must have received the request payload.
  1324. serverAcknowledgedRequestPayload = true
  1325. // sendBuffer is now no longer required for retries, and the
  1326. // buffer may be replaced; this allows meekConn.Write() to unblock
  1327. // and start buffering data for the next round trip while still
  1328. // reading the current round trip response.
  1329. if sendBuffer != nil {
  1330. // Assumes signaller.AwaitClosed is called above, so
  1331. // sendBuffer will no longer be accessed by RoundTrip.
  1332. sendBuffer.Truncate(0)
  1333. meek.replaceSendBuffer(sendBuffer)
  1334. sendBuffer = nil
  1335. }
  1336. readPayloadSize, err := meek.readPayload(response.Body)
  1337. response.Body.Close()
  1338. // receivedPayloadSize is the number of response
  1339. // payload bytes received and relayed. A retry can
  1340. // resume after this position.
  1341. receivedPayloadSize += readPayloadSize
  1342. if err != nil {
  1343. NoticeWarning("meek read payload failed: %s", err)
  1344. // ...continue to retry
  1345. } else {
  1346. // Round trip completed successfully
  1347. break
  1348. }
  1349. }
  1350. // Release context resources immediately.
  1351. cancelFunc()
  1352. // Either the request failed entirely, or there was a failure
  1353. // streaming the response payload. Always retry once. Then
  1354. // retry if time remains; when the next delay exceeds the time
  1355. // remaining until the deadline, do not retry.
  1356. now := time.Now()
  1357. if retries >= 1 &&
  1358. (now.After(retryDeadline) || retryDeadline.Sub(now) <= retryDelay) {
  1359. return 0, errors.Trace(err)
  1360. }
  1361. retries += 1
  1362. delayTimer := time.NewTimer(retryDelay)
  1363. select {
  1364. case <-delayTimer.C:
  1365. case <-meek.runCtx.Done():
  1366. delayTimer.Stop()
  1367. return 0, errors.Trace(err)
  1368. }
  1369. // Increase the next delay, to back off and avoid excessive
  1370. // activity in conditions such as no network connectivity.
  1371. retryDelay = time.Duration(
  1372. float64(retryDelay) * retryMultiplier)
  1373. if retryDelay >= retryMaxDelay {
  1374. retryDelay = retryMaxDelay
  1375. }
  1376. }
  1377. return receivedPayloadSize, nil
  1378. }
  1379. // Add additional headers to the HTTP request using the same method we use for adding
  1380. // custom headers to HTTP proxy requests.
  1381. func (meek *MeekConn) addAdditionalHeaders(request *http.Request) {
  1382. for name, value := range meek.additionalHeaders {
  1383. if name == "Host" {
  1384. if len(value) > 0 {
  1385. request.Host = value[0]
  1386. }
  1387. } else {
  1388. request.Header[name] = value
  1389. }
  1390. }
  1391. }
  1392. // readPayload reads the HTTP response in chunks, making the read buffer available
  1393. // to MeekConn.Read() calls after each chunk; the intention is to allow bytes to
  1394. // flow back to the reader as soon as possible instead of buffering the entire payload.
  1395. //
  1396. // When readPayload returns an error, the totalSize output is remains valid -- it's the
  1397. // number of payload bytes successfully read and relayed.
  1398. func (meek *MeekConn) readPayload(
  1399. receivedPayload io.ReadCloser) (totalSize int64, err error) {
  1400. defer receivedPayload.Close()
  1401. totalSize = 0
  1402. for {
  1403. reader := io.LimitReader(receivedPayload, int64(meek.readPayloadChunkLength))
  1404. // Block until there is capacity in the receive buffer
  1405. var receiveBuffer *bytes.Buffer
  1406. select {
  1407. case receiveBuffer = <-meek.emptyReceiveBuffer:
  1408. case receiveBuffer = <-meek.partialReceiveBuffer:
  1409. case <-meek.runCtx.Done():
  1410. return 0, nil
  1411. }
  1412. // Note: receiveBuffer size may exceed meek.fullReceiveBufferLength by up to the size
  1413. // of one received payload. The meek.fullReceiveBufferLength value is just a guideline.
  1414. n, err := receiveBuffer.ReadFrom(reader)
  1415. meek.replaceReceiveBuffer(receiveBuffer)
  1416. totalSize += n
  1417. if err != nil {
  1418. return totalSize, errors.Trace(err)
  1419. }
  1420. if n == 0 {
  1421. break
  1422. }
  1423. }
  1424. return totalSize, nil
  1425. }
  1426. // makeMeekObfuscationValues creates the meek cookie, to be sent with initial
  1427. // meek HTTP request, and other meek obfuscation values. The cookies contains
  1428. // obfuscated metadata, including meek version and other protocol information.
  1429. //
  1430. // In round tripper mode, the cookie contains the destination endpoint for the
  1431. // round trip request.
  1432. //
  1433. // In relay mode, the server will create a session using the cookie values and
  1434. // send the session ID back to the client via Set-Cookie header. The client
  1435. // must use that value with all consequent HTTP requests.
  1436. //
  1437. // In plain HTTP meek protocols, the cookie is visible over the adversary
  1438. // network, so the cookie is encrypted and obfuscated.
  1439. //
  1440. // Obsolete meek cookie fields used by the legacy server stack are no longer
  1441. // sent. These include ServerAddress and SessionID.
  1442. //
  1443. // The request payload limit and TLS redial probability apply only to relay
  1444. // mode and are selected once and used for the duration of a meek connction.
  1445. func makeMeekObfuscationValues(
  1446. p parameters.ParametersAccessor,
  1447. meekCookieEncryptionPublicKey string,
  1448. meekObfuscatedKey string,
  1449. meekObfuscatorPaddingPRNGSeed *prng.Seed,
  1450. clientTunnelProtocol string,
  1451. endPoint string,
  1452. ) (cookie *http.Cookie,
  1453. contentType string,
  1454. tlsPadding int,
  1455. limitRequestPayloadLength int,
  1456. redialTLSProbability float64,
  1457. err error) {
  1458. if meekCookieEncryptionPublicKey == "" {
  1459. return nil, "", 0, 0, 0.0, errors.TraceNew("missing public key")
  1460. }
  1461. cookieData := &protocol.MeekCookieData{
  1462. MeekProtocolVersion: MEEK_PROTOCOL_VERSION,
  1463. ClientTunnelProtocol: clientTunnelProtocol,
  1464. EndPoint: endPoint,
  1465. }
  1466. serializedCookie, err := json.Marshal(cookieData)
  1467. if err != nil {
  1468. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1469. }
  1470. // Encrypt the JSON data
  1471. // NaCl box is used for encryption. The peer public key comes from the server entry.
  1472. // Nonce is always all zeros, and is not sent in the cookie (the server also uses an all-zero nonce).
  1473. // http://nacl.cace-project.eu/box.html:
  1474. // "There is no harm in having the same nonce for different messages if the {sender, receiver} sets are
  1475. // different. This is true even if the sets overlap. For example, a sender can use the same nonce for two
  1476. // different messages if the messages are sent to two different public keys."
  1477. var nonce [24]byte
  1478. var publicKey [32]byte
  1479. decodedPublicKey, err := base64.StdEncoding.DecodeString(meekCookieEncryptionPublicKey)
  1480. if err != nil {
  1481. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1482. }
  1483. copy(publicKey[:], decodedPublicKey)
  1484. ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
  1485. if err != nil {
  1486. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1487. }
  1488. box := box.Seal(nil, serializedCookie, &nonce, &publicKey, ephemeralPrivateKey)
  1489. encryptedCookie := make([]byte, 32+len(box))
  1490. copy(encryptedCookie[0:32], ephemeralPublicKey[0:32])
  1491. copy(encryptedCookie[32:], box)
  1492. maxPadding := p.Int(parameters.MeekCookieMaxPadding)
  1493. // Obfuscate the encrypted data. NewClientObfuscator checks that
  1494. // meekObfuscatedKey isn't missing.
  1495. obfuscator, err := obfuscator.NewClientObfuscator(
  1496. &obfuscator.ObfuscatorConfig{
  1497. Keyword: meekObfuscatedKey,
  1498. PaddingPRNGSeed: meekObfuscatorPaddingPRNGSeed,
  1499. MaxPadding: &maxPadding})
  1500. if err != nil {
  1501. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1502. }
  1503. obfuscatedCookie, _ := obfuscator.SendPreamble()
  1504. seedLen := len(obfuscatedCookie)
  1505. obfuscatedCookie = append(obfuscatedCookie, encryptedCookie...)
  1506. obfuscator.ObfuscateClientToServer(obfuscatedCookie[seedLen:])
  1507. cookieNamePRNG, err := obfuscator.GetDerivedPRNG("meek-cookie-name")
  1508. if err != nil {
  1509. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1510. }
  1511. var cookieName string
  1512. if cookieNamePRNG.FlipWeightedCoin(p.Float(parameters.MeekAlternateCookieNameProbability)) {
  1513. cookieName = values.GetCookieName(cookieNamePRNG)
  1514. } else {
  1515. // Format the HTTP cookie
  1516. // The format is <random letter 'A'-'Z'>=<base64 data>, which is intended to match common cookie formats.
  1517. A := int('A')
  1518. Z := int('Z')
  1519. // letterIndex is integer in range [int('A'), int('Z')]
  1520. letterIndex := cookieNamePRNG.Intn(Z - A + 1)
  1521. cookieName = string(byte(A + letterIndex))
  1522. }
  1523. cookie = &http.Cookie{
  1524. Name: cookieName,
  1525. Value: base64.StdEncoding.EncodeToString(obfuscatedCookie)}
  1526. contentTypePRNG, err := obfuscator.GetDerivedPRNG("meek-content-type")
  1527. if err != nil {
  1528. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1529. }
  1530. if contentTypePRNG.FlipWeightedCoin(p.Float(parameters.MeekAlternateContentTypeProbability)) {
  1531. contentType = values.GetContentType(contentTypePRNG)
  1532. } else {
  1533. contentType = "application/octet-stream"
  1534. }
  1535. tlsPadding = 0
  1536. limitRequestPayloadLength = MEEK_MAX_REQUEST_PAYLOAD_LENGTH
  1537. redialTLSProbability = 0.0
  1538. tunnelProtocols := p.TunnelProtocols(parameters.MeekTrafficShapingLimitProtocols)
  1539. if (len(tunnelProtocols) == 0 ||
  1540. common.Contains(tunnelProtocols, clientTunnelProtocol)) &&
  1541. p.WeightedCoinFlip(parameters.MeekTrafficShapingProbability) {
  1542. limitRequestPayloadLengthPRNG, err := obfuscator.GetDerivedPRNG(
  1543. "meek-limit-request-payload-length")
  1544. if err != nil {
  1545. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1546. }
  1547. minLength := p.Int(parameters.MeekMinLimitRequestPayloadLength)
  1548. if minLength > MEEK_MAX_REQUEST_PAYLOAD_LENGTH {
  1549. minLength = MEEK_MAX_REQUEST_PAYLOAD_LENGTH
  1550. }
  1551. maxLength := p.Int(parameters.MeekMaxLimitRequestPayloadLength)
  1552. if maxLength > MEEK_MAX_REQUEST_PAYLOAD_LENGTH {
  1553. maxLength = MEEK_MAX_REQUEST_PAYLOAD_LENGTH
  1554. }
  1555. limitRequestPayloadLength = limitRequestPayloadLengthPRNG.Range(
  1556. minLength, maxLength)
  1557. minPadding := p.Int(parameters.MeekMinTLSPadding)
  1558. maxPadding := p.Int(parameters.MeekMaxTLSPadding)
  1559. // Maximum padding size per RFC 7685
  1560. if maxPadding > 65535 {
  1561. maxPadding = 65535
  1562. }
  1563. if maxPadding > 0 {
  1564. tlsPaddingPRNG, err := obfuscator.GetDerivedPRNG(
  1565. "meek-tls-padding")
  1566. if err != nil {
  1567. return nil, "", 0, 0, 0.0, errors.Trace(err)
  1568. }
  1569. tlsPadding = tlsPaddingPRNG.Range(minPadding, maxPadding)
  1570. }
  1571. redialTLSProbability = p.Float(parameters.MeekRedialTLSProbability)
  1572. }
  1573. return cookie, contentType, tlsPadding, limitRequestPayloadLength, redialTLSProbability, nil
  1574. }