meekConn.go 62 KB

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