meekConn.go 59 KB

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