httpProxy.go 26 KB

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