meekConn.go 61 KB

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