httpProxy.go 26 KB

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