httpProxy.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. responseHeaderTimeout := config.clientParameters.Get().Duration(
  112. parameters.HTTPProxyOriginServerTimeout)
  113. maxIdleConnsPerHost := config.clientParameters.Get().Int(
  114. 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: new(common.Conns),
  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. NoticeAlert("%s", common.ContextError(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 common.ContextError(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 common.ContextError(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 = errors.New("missing origin URL")
  281. }
  282. if err != nil {
  283. NoticeAlert("%s", common.ContextError(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 := url.ParseRequestURI(originURLString)
  289. if err != nil {
  290. NoticeAlert("%s", common.ContextError(FilterUrlError(err)))
  291. forceClose(responseWriter)
  292. return
  293. }
  294. if !originURL.IsAbs() || (originURL.Scheme != "http" && originURL.Scheme != "https") {
  295. NoticeAlert("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.Compare(b[:3], []byte("ICY")) == 0 {
  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, common.ContextError(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, common.ContextError(err)
  371. }
  372. serverName, _, err := net.SplitHostPort(address)
  373. if err != nil {
  374. conn.Close()
  375. return nil, common.ContextError(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.New("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, common.ContextError(err)
  391. }
  392. err = tlsConn.VerifyHostname(serverName)
  393. if err != nil {
  394. conn.Close()
  395. return nil, common.ContextError(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. NoticeAlert("%s", common.ContextError(FilterUrlError(err)))
  436. forceClose(responseWriter)
  437. return
  438. }
  439. defer response.Body.Close()
  440. if rewrites != nil {
  441. // NOTE: Rewrite functions are responsible for leaving response.Body in
  442. // a valid, readable state if there's no error.
  443. for key := range rewrites {
  444. var err error
  445. switch key {
  446. case "m3u8":
  447. err = rewriteM3U8(proxy.listenIP, proxy.listenPort, response)
  448. }
  449. if err != nil {
  450. NoticeAlert("URL proxy rewrite failed for %s: %s", key, common.ContextError(err))
  451. forceClose(responseWriter)
  452. response.Body.Close()
  453. return
  454. }
  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. NoticeAlert("write status line failed: %s", common.ContextError(err))
  488. conn.Close()
  489. return
  490. }
  491. err = responseWriter.Header().Write(conn)
  492. if err != nil {
  493. NoticeAlert("write headers failed: %s", common.ContextError(err))
  494. conn.Close()
  495. return
  496. }
  497. _, err = io.Copy(conn, response.Body)
  498. if err != nil {
  499. NoticeAlert("write body failed: %s", common.ContextError(err))
  500. conn.Close()
  501. return
  502. }
  503. } else {
  504. // Standard HTTP response.
  505. responseWriter.WriteHeader(response.StatusCode)
  506. _, err = io.Copy(responseWriter, response.Body)
  507. if err != nil {
  508. NoticeAlert("%s", common.ContextError(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. NoticeAlert("%s", common.ContextError(errors.New("responseWriter is not an http.Hijacker")))
  527. return nil
  528. }
  529. conn, _, err := hijacker.Hijack()
  530. if err != nil {
  531. NoticeAlert("%s", common.ContextError(fmt.Errorf("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, common.ContextError(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 := url.Parse(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 common.ContextError(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 common.ContextError(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. }