httpProxy.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "bytes"
  22. "compress/gzip"
  23. "crypto/tls"
  24. std_errors "errors"
  25. "fmt"
  26. "io"
  27. "io/ioutil"
  28. "net"
  29. "net/http"
  30. "net/url"
  31. "path/filepath"
  32. "strconv"
  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/parameters"
  40. "github.com/grafov/m3u8"
  41. )
  42. // HttpProxy is a HTTP server that relays HTTP requests through the Psiphon tunnel.
  43. // It includes support for HTTP CONNECT.
  44. //
  45. // This proxy also offers a "URL proxy" mode that relays requests for HTTP or HTTPS
  46. // or URLs specified in the proxy request path. This mode relays either through the
  47. // Psiphon tunnel, or directly.
  48. //
  49. // An example use case for tunneled URL proxy relays is to craft proxied URLs to pass to
  50. // components that don't support HTTP or SOCKS proxy settings. For example, the
  51. // Android Media Player (http://developer.android.com/reference/android/media/MediaPlayer.html).
  52. // To make the Media Player use the Psiphon tunnel, construct a URL such as:
  53. // "http://127.0.0.1:<proxy-port>/tunneled/<origin media URL>"; and pass this to the player.
  54. // The <origin media URL> must be escaped in such a way that it can be used inside a URL query.
  55. //
  56. // An example use case for direct, untunneled, relaying is to make use of Go's TLS
  57. // stack for HTTPS requests in cases where the native TLS stack is lacking (e.g.,
  58. // WinHTTP on Windows XP). The URL for direct relaying is:
  59. // "http://127.0.0.1:<proxy-port>/direct/<origin URL>".
  60. // Again, the <origin URL> must be escaped in such a way that it can be used inside a URL query.
  61. //
  62. // An example use case for tunneled relaying with rewriting (/tunneled-rewrite/) is when the
  63. // content of retrieved files contains URLs that also need to be modified to be tunneled.
  64. // For example, in iOS 10 the UIWebView media player does not put requests through the
  65. // NSURLProtocol, so they are not tunneled. Instead, we rewrite those URLs to use the URL
  66. // proxy, and rewrite retrieved playlist files so they also contain proxied URLs.
  67. //
  68. // The URL proxy offers /tunneled-icy/ which is compatible with both HTTP and ICY protocol
  69. // resources.
  70. //
  71. // Origin URLs must include the scheme prefix ("http://" or "https://") and must be
  72. // URL encoded.
  73. //
  74. type HttpProxy struct {
  75. config *Config
  76. tunneler Tunneler
  77. listener net.Listener
  78. serveWaitGroup *sync.WaitGroup
  79. httpProxyTunneledRelay *http.Transport
  80. urlProxyTunneledRelay *http.Transport
  81. urlProxyTunneledClient *http.Client
  82. urlProxyDirectRelay *http.Transport
  83. urlProxyDirectClient *http.Client
  84. responseHeaderTimeout time.Duration
  85. openConns *common.Conns
  86. stopListeningBroadcast chan struct{}
  87. listenIP string
  88. listenPort int
  89. }
  90. var _HTTP_PROXY_TYPE = "HTTP"
  91. // NewHttpProxy initializes and runs a new HTTP proxy server.
  92. func NewHttpProxy(
  93. config *Config,
  94. tunneler Tunneler,
  95. listenIP string) (proxy *HttpProxy, err error) {
  96. listener, portInUse, err := makeLocalProxyListener(
  97. listenIP, config.LocalHttpProxyPort)
  98. if err != nil {
  99. if portInUse {
  100. NoticeHttpProxyPortInUse(config.LocalHttpProxyPort)
  101. }
  102. return nil, errors.Trace(err)
  103. }
  104. tunneledDialer := func(_, addr string) (conn net.Conn, err error) {
  105. // downstreamConn is not set in this case, as there is not a fixed
  106. // association between a downstream client connection and a particular
  107. // tunnel.
  108. return tunneler.Dial(addr, nil)
  109. }
  110. directDialer := func(_, addr string) (conn net.Conn, err error) {
  111. return tunneler.DirectDial(addr)
  112. }
  113. p := config.GetParameters().Get()
  114. responseHeaderTimeout := p.Duration(parameters.HTTPProxyOriginServerTimeout)
  115. maxIdleConnsPerHost := p.Int(parameters.HTTPProxyMaxIdleConnectionsPerHost)
  116. // TODO: could HTTP proxy share a tunneled transport with URL proxy?
  117. // For now, keeping them distinct just to be conservative.
  118. httpProxyTunneledRelay := &http.Transport{
  119. Dial: tunneledDialer,
  120. MaxIdleConnsPerHost: maxIdleConnsPerHost,
  121. ResponseHeaderTimeout: responseHeaderTimeout,
  122. }
  123. // Note: URL proxy relays use http.Client for upstream requests, so
  124. // redirects will be followed. HTTP proxy should not follow redirects
  125. // and simply uses http.Transport directly.
  126. urlProxyTunneledRelay := &http.Transport{
  127. Dial: tunneledDialer,
  128. MaxIdleConnsPerHost: maxIdleConnsPerHost,
  129. ResponseHeaderTimeout: responseHeaderTimeout,
  130. }
  131. urlProxyTunneledClient := &http.Client{
  132. Transport: urlProxyTunneledRelay,
  133. Jar: nil, // TODO: cookie support for URL proxy?
  134. // Leaving original value in the note below:
  135. // Note: don't use this timeout -- it interrupts downloads of large response bodies
  136. //Timeout: HTTP_PROXY_ORIGIN_SERVER_TIMEOUT,
  137. }
  138. urlProxyDirectRelay := &http.Transport{
  139. Dial: directDialer,
  140. MaxIdleConnsPerHost: maxIdleConnsPerHost,
  141. ResponseHeaderTimeout: responseHeaderTimeout,
  142. }
  143. urlProxyDirectClient := &http.Client{
  144. Transport: urlProxyDirectRelay,
  145. Jar: nil,
  146. }
  147. proxyIP, proxyPortString, _ := net.SplitHostPort(listener.Addr().String())
  148. proxyPort, _ := strconv.Atoi(proxyPortString)
  149. proxy = &HttpProxy{
  150. config: config,
  151. tunneler: tunneler,
  152. listener: listener,
  153. serveWaitGroup: new(sync.WaitGroup),
  154. httpProxyTunneledRelay: httpProxyTunneledRelay,
  155. urlProxyTunneledRelay: urlProxyTunneledRelay,
  156. urlProxyTunneledClient: urlProxyTunneledClient,
  157. urlProxyDirectRelay: urlProxyDirectRelay,
  158. urlProxyDirectClient: urlProxyDirectClient,
  159. responseHeaderTimeout: responseHeaderTimeout,
  160. openConns: common.NewConns(),
  161. stopListeningBroadcast: make(chan struct{}),
  162. listenIP: proxyIP,
  163. listenPort: proxyPort,
  164. }
  165. proxy.serveWaitGroup.Add(1)
  166. go proxy.serve()
  167. // TODO: NoticeListeningHttpProxyPort is emitted after net.Listen
  168. // but before go proxy.server() and httpServer.Serve(), and this
  169. // appears to cause client connections to the HTTP proxy to fail
  170. // (in controller_test.go, only when a tunnel is established very quickly
  171. // and NoticeTunnels is emitted and the client makes a request -- all
  172. // before the proxy.server() goroutine runs).
  173. // This condition doesn't arise in Go 1.4, just in Go tip (pre-1.5).
  174. // Note that httpServer.Serve() blocks so the fix can't be to emit
  175. // NoticeListeningHttpProxyPort after that call.
  176. // Also, check the listen backlog queue length -- shouldn't it be possible
  177. // to enqueue pending connections between net.Listen() and httpServer.Serve()?
  178. NoticeListeningHttpProxyPort(proxy.listenPort)
  179. return proxy, nil
  180. }
  181. // Close terminates the HTTP server.
  182. func (proxy *HttpProxy) Close() {
  183. close(proxy.stopListeningBroadcast)
  184. proxy.listener.Close()
  185. proxy.serveWaitGroup.Wait()
  186. // Close local->proxy persistent connections
  187. proxy.openConns.CloseAll()
  188. // Close idle proxy->origin persistent connections
  189. // TODO: also close active connections
  190. proxy.httpProxyTunneledRelay.CloseIdleConnections()
  191. proxy.urlProxyTunneledRelay.CloseIdleConnections()
  192. proxy.urlProxyDirectRelay.CloseIdleConnections()
  193. }
  194. // ServeHTTP receives HTTP requests and proxies them. CONNECT requests
  195. // are hijacked and all data is relayed. Other HTTP requests are proxied
  196. // with explicit round trips. In both cases, the tunnel is used for proxied
  197. // traffic.
  198. //
  199. // Implementation is based on:
  200. //
  201. // https://github.com/justmao945/mallory
  202. // Copyright (c) 2014 JianjunMao
  203. // The MIT License (MIT)
  204. //
  205. // https://golang.org/src/pkg/net/http/httputil/reverseproxy.go
  206. // Copyright 2011 The Go Authors. All rights reserved.
  207. // Use of this source code is governed by a BSD-style
  208. // license that can be found in the LICENSE file.
  209. //
  210. func (proxy *HttpProxy) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
  211. if request.Method == "CONNECT" {
  212. conn := hijack(responseWriter)
  213. if conn == nil {
  214. // hijack emits an alert notice
  215. http.Error(responseWriter, "", http.StatusInternalServerError)
  216. return
  217. }
  218. go func() {
  219. err := proxy.httpConnectHandler(conn, request.URL.Host)
  220. if err != nil {
  221. NoticeWarning("%s", errors.Trace(err))
  222. }
  223. }()
  224. } else if request.URL.IsAbs() {
  225. proxy.httpProxyHandler(responseWriter, request)
  226. } else {
  227. proxy.urlProxyHandler(responseWriter, request)
  228. }
  229. }
  230. func (proxy *HttpProxy) httpConnectHandler(localConn net.Conn, target string) (err error) {
  231. defer localConn.Close()
  232. defer proxy.openConns.Remove(localConn)
  233. proxy.openConns.Add(localConn)
  234. // Setting downstreamConn so localConn.Close() will be called when remoteConn.Close() is called.
  235. // This ensures that the downstream client (e.g., web browser) doesn't keep waiting on the
  236. // open connection for data which will never arrive.
  237. remoteConn, err := proxy.tunneler.Dial(target, localConn)
  238. if err != nil {
  239. return errors.Trace(err)
  240. }
  241. defer remoteConn.Close()
  242. _, err = localConn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
  243. if err != nil {
  244. return errors.Trace(err)
  245. }
  246. LocalProxyRelay(proxy.config, _HTTP_PROXY_TYPE, localConn, remoteConn)
  247. return nil
  248. }
  249. func (proxy *HttpProxy) httpProxyHandler(responseWriter http.ResponseWriter, request *http.Request) {
  250. proxy.relayHTTPRequest(nil, proxy.httpProxyTunneledRelay, request, responseWriter, nil, nil)
  251. }
  252. const (
  253. URL_PROXY_TUNNELED_REQUEST_PATH = "/tunneled/"
  254. URL_PROXY_TUNNELED_REWRITE_REQUEST_PATH = "/tunneled-rewrite/"
  255. URL_PROXY_TUNNELED_ICY_REQUEST_PATH = "/tunneled-icy/"
  256. URL_PROXY_DIRECT_REQUEST_PATH = "/direct/"
  257. )
  258. func (proxy *HttpProxy) urlProxyHandler(responseWriter http.ResponseWriter, request *http.Request) {
  259. var client *http.Client
  260. var rewriteICYStatus *rewriteICYStatus
  261. var originURLString string
  262. var err error
  263. var rewrites url.Values
  264. // Request URL should be "/tunneled/<origin URL>" or "/direct/<origin URL>" and the
  265. // origin URL must be URL encoded.
  266. switch {
  267. case strings.HasPrefix(request.URL.RawPath, URL_PROXY_TUNNELED_REQUEST_PATH):
  268. originURLString, err = url.QueryUnescape(request.URL.RawPath[len(URL_PROXY_TUNNELED_REQUEST_PATH):])
  269. client = proxy.urlProxyTunneledClient
  270. case strings.HasPrefix(request.URL.RawPath, URL_PROXY_TUNNELED_REWRITE_REQUEST_PATH):
  271. originURLString, err = url.QueryUnescape(request.URL.RawPath[len(URL_PROXY_TUNNELED_REWRITE_REQUEST_PATH):])
  272. client = proxy.urlProxyTunneledClient
  273. rewrites = request.URL.Query()
  274. case strings.HasPrefix(request.URL.RawPath, URL_PROXY_TUNNELED_ICY_REQUEST_PATH):
  275. originURLString, err = url.QueryUnescape(request.URL.RawPath[len(URL_PROXY_TUNNELED_ICY_REQUEST_PATH):])
  276. client, rewriteICYStatus = proxy.makeRewriteICYClient()
  277. rewrites = request.URL.Query()
  278. case strings.HasPrefix(request.URL.RawPath, URL_PROXY_DIRECT_REQUEST_PATH):
  279. originURLString, err = url.QueryUnescape(request.URL.RawPath[len(URL_PROXY_DIRECT_REQUEST_PATH):])
  280. client = proxy.urlProxyDirectClient
  281. default:
  282. err = std_errors.New("missing origin URL")
  283. }
  284. if err != nil {
  285. NoticeWarning("%s", errors.Trace(common.RedactURLError(err)))
  286. forceClose(responseWriter)
  287. return
  288. }
  289. // Origin URL must be well-formed, absolute, and have a scheme of "http" or "https"
  290. originURL, err := common.SafeParseRequestURI(originURLString)
  291. if err != nil {
  292. NoticeWarning("%s", errors.Trace(common.RedactURLError(err)))
  293. forceClose(responseWriter)
  294. return
  295. }
  296. if !originURL.IsAbs() || (originURL.Scheme != "http" && originURL.Scheme != "https") {
  297. NoticeWarning("invalid origin URL")
  298. forceClose(responseWriter)
  299. return
  300. }
  301. // Transform received request to directly reference the origin URL
  302. request.Host = originURL.Host
  303. request.URL = originURL
  304. proxy.relayHTTPRequest(client, nil, request, responseWriter, rewrites, rewriteICYStatus)
  305. }
  306. // rewriteICYConn rewrites an ICY procotol responses to that it may be
  307. // consumed by Go's http package. rewriteICYConn expects the ICY response to
  308. // be equivalent to HTTP/1.1 with the exception of the protocol name in the
  309. // status line, which is the one part that is rewritten. Responses that are
  310. // already HTTP are passed through unmodified.
  311. type rewriteICYConn struct {
  312. net.Conn
  313. doneRewriting int32
  314. isICY *int32
  315. }
  316. func (conn *rewriteICYConn) Read(b []byte) (int, error) {
  317. if !atomic.CompareAndSwapInt32(&conn.doneRewriting, 0, 1) {
  318. return conn.Conn.Read(b)
  319. }
  320. if len(b) < 3 {
  321. // Don't attempt to rewrite the protocol when insufficient
  322. // buffer space. This is not expected to happen in practise
  323. // when Go's http reads the response, so for now we just
  324. // skip the rewrite instead of tracking state accross Reads.
  325. return conn.Conn.Read(b)
  326. }
  327. // Expect to read either "ICY" or "HTT".
  328. n, err := conn.Conn.Read(b[:3])
  329. if err != nil {
  330. return n, err
  331. }
  332. if bytes.Equal(b[:3], []byte("ICY")) {
  333. atomic.StoreInt32(conn.isICY, 1)
  334. protocol := "HTTP/1.0"
  335. copy(b, []byte(protocol))
  336. return len(protocol), nil
  337. }
  338. return n, nil
  339. }
  340. type rewriteICYStatus struct {
  341. isFirstConnICY int32
  342. }
  343. func (status *rewriteICYStatus) isICY() bool {
  344. return atomic.LoadInt32(&status.isFirstConnICY) == 1
  345. }
  346. // makeRewriteICYClient creates an http.Client with a Transport configured to
  347. // use rewriteICYConn. Both HTTP and HTTPS are handled. The http.Client is
  348. // intended to be used for one single request. The client disables keep alives
  349. // as rewriteICYConn can only rewrite the first response in a connection. The
  350. // returned rewriteICYStatus indicates whether the first response for the first
  351. // request was ICY, allowing the downstream relayed response to replicate the
  352. // ICY protocol.
  353. func (proxy *HttpProxy) makeRewriteICYClient() (*http.Client, *rewriteICYStatus) {
  354. rewriteICYStatus := &rewriteICYStatus{}
  355. tunneledDialer := func(_, addr string) (conn net.Conn, err error) {
  356. // See comment in NewHttpProxy regarding downstreamConn
  357. return proxy.tunneler.Dial(addr, nil)
  358. }
  359. dial := func(network, address string) (net.Conn, error) {
  360. conn, err := tunneledDialer(network, address)
  361. if err != nil {
  362. return nil, errors.Trace(err)
  363. }
  364. return &rewriteICYConn{
  365. Conn: conn,
  366. isICY: &rewriteICYStatus.isFirstConnICY,
  367. }, nil
  368. }
  369. dialTLS := func(network, address string) (net.Conn, error) {
  370. conn, err := tunneledDialer(network, address)
  371. if err != nil {
  372. return nil, errors.Trace(err)
  373. }
  374. serverName, _, err := net.SplitHostPort(address)
  375. if err != nil {
  376. conn.Close()
  377. return nil, errors.Trace(err)
  378. }
  379. tlsConn := tls.Client(conn, &tls.Config{ServerName: serverName})
  380. resultChannel := make(chan error, 1)
  381. timeout := proxy.responseHeaderTimeout
  382. afterFunc := time.AfterFunc(timeout, func() {
  383. resultChannel <- errors.TraceNew("TLS handshake timeout")
  384. })
  385. defer afterFunc.Stop()
  386. go func() {
  387. resultChannel <- tlsConn.Handshake()
  388. }()
  389. err = <-resultChannel
  390. if err != nil {
  391. conn.Close()
  392. return nil, errors.Trace(err)
  393. }
  394. err = tlsConn.VerifyHostname(serverName)
  395. if err != nil {
  396. conn.Close()
  397. return nil, errors.Trace(err)
  398. }
  399. return &rewriteICYConn{
  400. Conn: tlsConn,
  401. isICY: &rewriteICYStatus.isFirstConnICY,
  402. }, nil
  403. }
  404. return &http.Client{
  405. Transport: &http.Transport{
  406. Dial: dial,
  407. DialTLS: dialTLS,
  408. DisableKeepAlives: true,
  409. ResponseHeaderTimeout: proxy.responseHeaderTimeout,
  410. },
  411. }, rewriteICYStatus
  412. }
  413. func (proxy *HttpProxy) relayHTTPRequest(
  414. client *http.Client,
  415. transport *http.Transport,
  416. request *http.Request,
  417. responseWriter http.ResponseWriter,
  418. rewrites url.Values,
  419. rewriteICYStatus *rewriteICYStatus) {
  420. // Transform received request struct before using as input to relayed request
  421. request.Close = false
  422. request.RequestURI = ""
  423. for _, key := range hopHeaders {
  424. request.Header.Del(key)
  425. }
  426. // Relay the HTTP request and get the response. Use a client when supplied,
  427. // otherwise a transport. A client handles cookies and redirects, and a
  428. // transport does not.
  429. var response *http.Response
  430. var err error
  431. if client != nil {
  432. response, err = client.Do(request)
  433. } else {
  434. response, err = transport.RoundTrip(request)
  435. }
  436. if err != nil {
  437. NoticeWarning("%s", errors.Trace(common.RedactURLError(err)))
  438. forceClose(responseWriter)
  439. return
  440. }
  441. defer response.Body.Close()
  442. // Note: Rewrite functions are responsible for leaving response.Body in
  443. // a valid, readable state if there's no error.
  444. for key := range rewrites {
  445. var err error
  446. switch key {
  447. case "m3u8":
  448. err = rewriteM3U8(proxy.listenIP, proxy.listenPort, response)
  449. }
  450. if err != nil {
  451. NoticeWarning("URL proxy rewrite failed for %s: %s", key, errors.Trace(err))
  452. forceClose(responseWriter)
  453. response.Body.Close()
  454. return
  455. }
  456. }
  457. // Relay the remote response headers
  458. for _, key := range hopHeaders {
  459. response.Header.Del(key)
  460. }
  461. for key := range responseWriter.Header() {
  462. responseWriter.Header().Del(key)
  463. }
  464. for key, values := range response.Header {
  465. for _, value := range values {
  466. responseWriter.Header().Add(key, value)
  467. }
  468. }
  469. // Send the response downstream
  470. if rewriteICYStatus != nil && rewriteICYStatus.isICY() {
  471. // Custom ICY response, using "ICY" as the protocol name
  472. // but otherwise equivalent to the HTTP response.
  473. // As the ICY http.Transport has disabled keep-alives,
  474. // hijacking here does not disrupt an otherwise persistent
  475. // connection.
  476. conn := hijack(responseWriter)
  477. if conn == nil {
  478. // hijack emits an alert notice
  479. return
  480. }
  481. _, err := fmt.Fprintf(
  482. conn,
  483. "ICY %d %s\r\n",
  484. response.StatusCode,
  485. http.StatusText(response.StatusCode))
  486. if err != nil {
  487. NoticeWarning("write status line failed: %s", errors.Trace(err))
  488. conn.Close()
  489. return
  490. }
  491. err = responseWriter.Header().Write(conn)
  492. if err != nil {
  493. NoticeWarning("write headers failed: %s", errors.Trace(err))
  494. conn.Close()
  495. return
  496. }
  497. _, err = RelayCopyBuffer(proxy.config, conn, response.Body)
  498. if err != nil {
  499. NoticeWarning("write body failed: %s", errors.Trace(err))
  500. conn.Close()
  501. return
  502. }
  503. } else {
  504. // Standard HTTP response.
  505. responseWriter.WriteHeader(response.StatusCode)
  506. _, err = RelayCopyBuffer(proxy.config, responseWriter, response.Body)
  507. if err != nil {
  508. NoticeWarning("%s", errors.Trace(err))
  509. forceClose(responseWriter)
  510. return
  511. }
  512. }
  513. }
  514. // forceClose hijacks and closes persistent connections. This is used
  515. // to ensure local persistent connections into the HTTP proxy are closed
  516. // when ServeHTTP encounters an error.
  517. func forceClose(responseWriter http.ResponseWriter) {
  518. conn := hijack(responseWriter)
  519. if conn != nil {
  520. conn.Close()
  521. }
  522. }
  523. func hijack(responseWriter http.ResponseWriter) net.Conn {
  524. hijacker, ok := responseWriter.(http.Hijacker)
  525. if !ok {
  526. NoticeWarning("%s", errors.TraceNew("responseWriter is not an http.Hijacker"))
  527. return nil
  528. }
  529. conn, _, err := hijacker.Hijack()
  530. if err != nil {
  531. NoticeWarning("%s", errors.Tracef("responseWriter hijack failed: %s", err))
  532. return nil
  533. }
  534. return conn
  535. }
  536. // From https://golang.org/src/pkg/net/http/httputil/reverseproxy.go:
  537. // Hop-by-hop headers. These are removed when sent to the backend.
  538. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
  539. var hopHeaders = []string{
  540. "Connection",
  541. "Keep-Alive",
  542. "Proxy-Authenticate",
  543. "Proxy-Authorization",
  544. "Proxy-Connection", // see: http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/web-proxy-connection-header.html
  545. "Te", // canonicalized version of "TE"
  546. "Trailers",
  547. "Transfer-Encoding",
  548. "Upgrade",
  549. }
  550. // httpConnStateCallback is called by http.Server when the state of a local->proxy
  551. // connection changes. Open connections are tracked so that all local->proxy persistent
  552. // connections can be closed by HttpProxy.Close()
  553. // TODO: if the HttpProxy is decoupled from a single Tunnel instance and
  554. // instead uses the "current" Tunnel, it may not be necessary to close
  555. // local persistent connections when the tunnel reconnects.
  556. func (proxy *HttpProxy) httpConnStateCallback(conn net.Conn, connState http.ConnState) {
  557. switch connState {
  558. case http.StateNew:
  559. proxy.openConns.Add(conn)
  560. case http.StateActive, http.StateIdle:
  561. // No action
  562. case http.StateHijacked, http.StateClosed:
  563. proxy.openConns.Remove(conn)
  564. }
  565. }
  566. func (proxy *HttpProxy) serve() {
  567. defer proxy.listener.Close()
  568. defer proxy.serveWaitGroup.Done()
  569. httpServer := &http.Server{
  570. Handler: proxy,
  571. ConnState: proxy.httpConnStateCallback,
  572. }
  573. // Note: will be interrupted by listener.Close() call made by proxy.Close()
  574. err := httpServer.Serve(proxy.listener)
  575. // Can't check for the exact error that Close() will cause in Accept(),
  576. // (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
  577. // explicit stop signal to stop gracefully.
  578. select {
  579. case <-proxy.stopListeningBroadcast:
  580. default:
  581. if err != nil {
  582. proxy.tunneler.SignalComponentFailure()
  583. NoticeLocalProxyError(_HTTP_PROXY_TYPE, errors.Trace(err))
  584. }
  585. }
  586. NoticeInfo("HTTP proxy stopped")
  587. }
  588. //
  589. // Rewrite functions
  590. //
  591. // toAbsoluteURL takes a base URL and a relative URL and constructs an appropriate absolute URL.
  592. func toAbsoluteURL(baseURL *url.URL, relativeURLString string) string {
  593. relativeURL, err := common.SafeParseURL(relativeURLString)
  594. if err != nil {
  595. return ""
  596. }
  597. if relativeURL.IsAbs() {
  598. return relativeURL.String()
  599. }
  600. return baseURL.ResolveReference(relativeURL).String()
  601. }
  602. // proxifyURL takes an absolute URL and rewrites it to go through the local URL proxy.
  603. // urlProxy port is the local HTTP proxy port.
  604. //
  605. // If rewriteParams is nil, then no rewriting will be done. Otherwise, it should contain
  606. // supported rewriting flags (like "m3u8").
  607. func proxifyURL(localHTTPProxyIP string, localHTTPProxyPort int, urlString string, rewriteParams []string) string {
  608. // Note that we need to use the "opaque" form of URL so that it doesn't double-escape the path. See: https://github.com/golang/go/issues/10887
  609. // TODO: IPv6 support
  610. if localHTTPProxyIP == "0.0.0.0" {
  611. localHTTPProxyIP = "127.0.0.1"
  612. }
  613. proxyPath := URL_PROXY_TUNNELED_REQUEST_PATH
  614. if rewriteParams != nil {
  615. proxyPath = URL_PROXY_TUNNELED_REWRITE_REQUEST_PATH
  616. }
  617. opaqueFormat := fmt.Sprintf("//%%s:%%d%s%%s", proxyPath)
  618. var proxifiedURL url.URL
  619. proxifiedURL.Scheme = "http"
  620. proxifiedURL.Opaque = fmt.Sprintf(opaqueFormat, localHTTPProxyIP, localHTTPProxyPort, url.QueryEscape(urlString))
  621. qp := proxifiedURL.Query()
  622. for _, rewrite := range rewriteParams {
  623. qp.Set(rewrite, "")
  624. }
  625. proxifiedURL.RawQuery = qp.Encode()
  626. return proxifiedURL.String()
  627. }
  628. // Rewrite the contents of the M3U8 file in body to be compatible with URL proxying.
  629. // If error is returned, response body may not be valid for reading.
  630. func rewriteM3U8(localHTTPProxyIP string, localHTTPProxyPort int, response *http.Response) error {
  631. // Check URL path extension
  632. extension := filepath.Ext(response.Request.URL.Path)
  633. var shouldHandle = (extension == ".m3u8")
  634. // If not .m3u8 then check content type
  635. if !shouldHandle {
  636. contentType := strings.ToLower(response.Header.Get("Content-Type"))
  637. shouldHandle = (contentType == "application/x-mpegurl" || contentType == "vnd.apple.mpegurl")
  638. }
  639. if !shouldHandle {
  640. return nil
  641. }
  642. var reader io.ReadCloser
  643. switch response.Header.Get("Content-Encoding") {
  644. case "gzip":
  645. var err error
  646. reader, err = gzip.NewReader(response.Body)
  647. if err != nil {
  648. return errors.Trace(err)
  649. }
  650. // Unset Content-Encoding.
  651. // There's is no point in deflating the decoded/rewritten content
  652. response.Header.Del("Content-Encoding")
  653. defer reader.Close()
  654. default:
  655. reader = response.Body
  656. }
  657. contentBodyBytes, err := ioutil.ReadAll(reader)
  658. response.Body.Close()
  659. if err != nil {
  660. return errors.Trace(err)
  661. }
  662. p, listType, err := m3u8.Decode(*bytes.NewBuffer(contentBodyBytes), true)
  663. if err != nil {
  664. // Don't pass this error up. Just don't change anything.
  665. response.Body = ioutil.NopCloser(bytes.NewReader(contentBodyBytes))
  666. response.Header.Set("Content-Length", strconv.FormatInt(int64(len(contentBodyBytes)), 10))
  667. return nil
  668. }
  669. var rewrittenBodyBytes []byte
  670. switch listType {
  671. case m3u8.MEDIA:
  672. mediapl := p.(*m3u8.MediaPlaylist)
  673. for _, segment := range mediapl.Segments {
  674. if segment == nil {
  675. break
  676. }
  677. if segment.URI != "" {
  678. segment.URI = proxifyURL(localHTTPProxyIP, localHTTPProxyPort, toAbsoluteURL(response.Request.URL, segment.URI), nil)
  679. }
  680. if segment.Key != nil && segment.Key.URI != "" {
  681. segment.Key.URI = proxifyURL(localHTTPProxyIP, localHTTPProxyPort, toAbsoluteURL(response.Request.URL, segment.Key.URI), nil)
  682. }
  683. if segment.Map != nil && segment.Map.URI != "" {
  684. segment.Map.URI = proxifyURL(localHTTPProxyIP, localHTTPProxyPort, toAbsoluteURL(response.Request.URL, segment.Map.URI), nil)
  685. }
  686. }
  687. rewrittenBodyBytes = []byte(mediapl.String())
  688. case m3u8.MASTER:
  689. masterpl := p.(*m3u8.MasterPlaylist)
  690. for _, variant := range masterpl.Variants {
  691. if variant == nil {
  692. break
  693. }
  694. if variant.URI != "" {
  695. variant.URI = proxifyURL(localHTTPProxyIP, localHTTPProxyPort, toAbsoluteURL(response.Request.URL, variant.URI), []string{"m3u8"})
  696. }
  697. for _, alternative := range variant.Alternatives {
  698. if alternative == nil {
  699. break
  700. }
  701. if alternative.URI != "" {
  702. alternative.URI = proxifyURL(localHTTPProxyIP, localHTTPProxyPort, toAbsoluteURL(response.Request.URL, alternative.URI), []string{"m3u8"})
  703. }
  704. }
  705. }
  706. rewrittenBodyBytes = []byte(masterpl.String())
  707. }
  708. var responseBodyBytes []byte
  709. if len(rewrittenBodyBytes) == 0 {
  710. responseBodyBytes = contentBodyBytes[:]
  711. } else {
  712. responseBodyBytes = rewrittenBodyBytes[:]
  713. // When rewriting the original URL so that it was URL-proxied, we lost the
  714. // file extension of it. That means we'd better make sure the Content-Type is set.
  715. response.Header.Set("Content-Type", "application/x-mpegurl")
  716. }
  717. response.Header.Set("Content-Length", strconv.FormatInt(int64(len(responseBodyBytes)), 10))
  718. response.Body = ioutil.NopCloser(bytes.NewReader(responseBodyBytes))
  719. return nil
  720. }