reverseproxy.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // HTTP reverse proxy handler
  5. package vhost
  6. import (
  7. "context"
  8. "encoding/base64"
  9. "fmt"
  10. "io"
  11. "log"
  12. "net"
  13. "net/http"
  14. "net/textproto"
  15. "net/url"
  16. "strings"
  17. "sync"
  18. "time"
  19. "golang.org/x/net/http/httpguts"
  20. )
  21. // ReverseProxy is an HTTP Handler that takes an incoming request and
  22. // sends it to another server, proxying the response back to the
  23. // client.
  24. //
  25. // ReverseProxy by default sets the client IP as the value of the
  26. // X-Forwarded-For header.
  27. //
  28. // If an X-Forwarded-For header already exists, the client IP is
  29. // appended to the existing values. As a special case, if the header
  30. // exists in the Request.Header map but has a nil value (such as when
  31. // set by the Director func), the X-Forwarded-For header is
  32. // not modified.
  33. //
  34. // To prevent IP spoofing, be sure to delete any pre-existing
  35. // X-Forwarded-For header coming from the client or
  36. // an untrusted proxy.
  37. type ReverseProxy struct {
  38. // Director must be a function which modifies
  39. // the request into a new request to be sent
  40. // using Transport. Its response is then copied
  41. // back to the original client unmodified.
  42. // Director must not access the provided Request
  43. // after returning.
  44. Director func(*http.Request)
  45. // The transport used to perform proxy requests.
  46. // If nil, http.DefaultTransport is used.
  47. Transport http.RoundTripper
  48. // FlushInterval specifies the flush interval
  49. // to flush to the client while copying the
  50. // response body.
  51. // If zero, no periodic flushing is done.
  52. // A negative value means to flush immediately
  53. // after each write to the client.
  54. // The FlushInterval is ignored when ReverseProxy
  55. // recognizes a response as a streaming response, or
  56. // if its ContentLength is -1; for such responses, writes
  57. // are flushed to the client immediately.
  58. FlushInterval time.Duration
  59. // ErrorLog specifies an optional logger for errors
  60. // that occur when attempting to proxy the request.
  61. // If nil, logging is done via the log package's standard logger.
  62. ErrorLog *log.Logger
  63. // BufferPool optionally specifies a buffer pool to
  64. // get byte slices for use by io.CopyBuffer when
  65. // copying HTTP response bodies.
  66. BufferPool BufferPool
  67. // ModifyResponse is an optional function that modifies the
  68. // Response from the backend. It is called if the backend
  69. // returns a response at all, with any HTTP status code.
  70. // If the backend is unreachable, the optional ErrorHandler is
  71. // called without any call to ModifyResponse.
  72. //
  73. // If ModifyResponse returns an error, ErrorHandler is called
  74. // with its error value. If ErrorHandler is nil, its default
  75. // implementation is used.
  76. ModifyResponse func(*http.Response) error
  77. // ErrorHandler is an optional function that handles errors
  78. // reaching the backend or errors from ModifyResponse.
  79. //
  80. // If nil, the default is to log the provided error and return
  81. // a 502 Status Bad Gateway response.
  82. ErrorHandler func(http.ResponseWriter, *http.Request, error)
  83. }
  84. // A BufferPool is an interface for getting and returning temporary
  85. // byte slices for use by io.CopyBuffer.
  86. type BufferPool interface {
  87. Get() []byte
  88. Put([]byte)
  89. }
  90. func singleJoiningSlash(a, b string) string {
  91. aslash := strings.HasSuffix(a, "/")
  92. bslash := strings.HasPrefix(b, "/")
  93. switch {
  94. case aslash && bslash:
  95. return a + b[1:]
  96. case !aslash && !bslash:
  97. return a + "/" + b
  98. }
  99. return a + b
  100. }
  101. func joinURLPath(a, b *url.URL) (path, rawpath string) {
  102. if a.RawPath == "" && b.RawPath == "" {
  103. return singleJoiningSlash(a.Path, b.Path), ""
  104. }
  105. // Same as singleJoiningSlash, but uses EscapedPath to determine
  106. // whether a slash should be added
  107. apath := a.EscapedPath()
  108. bpath := b.EscapedPath()
  109. aslash := strings.HasSuffix(apath, "/")
  110. bslash := strings.HasPrefix(bpath, "/")
  111. switch {
  112. case aslash && bslash:
  113. return a.Path + b.Path[1:], apath + bpath[1:]
  114. case !aslash && !bslash:
  115. return a.Path + "/" + b.Path, apath + "/" + bpath
  116. }
  117. return a.Path + b.Path, apath + bpath
  118. }
  119. // NewSingleHostReverseProxy returns a new ReverseProxy that routes
  120. // URLs to the scheme, host, and base path provided in target. If the
  121. // target's path is "/base" and the incoming request was for "/dir",
  122. // the target request will be for /base/dir.
  123. // NewSingleHostReverseProxy does not rewrite the Host header.
  124. // To rewrite Host headers, use ReverseProxy directly with a custom
  125. // Director policy.
  126. func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
  127. targetQuery := target.RawQuery
  128. director := func(req *http.Request) {
  129. req.URL.Scheme = target.Scheme
  130. req.URL.Host = target.Host
  131. req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
  132. if targetQuery == "" || req.URL.RawQuery == "" {
  133. req.URL.RawQuery = targetQuery + req.URL.RawQuery
  134. } else {
  135. req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
  136. }
  137. if _, ok := req.Header["User-Agent"]; !ok {
  138. // explicitly disable User-Agent so it's not set to default value
  139. req.Header.Set("User-Agent", "")
  140. }
  141. }
  142. return &ReverseProxy{Director: director}
  143. }
  144. func copyHeader(dst, src http.Header) {
  145. for k, vv := range src {
  146. for _, v := range vv {
  147. dst.Add(k, v)
  148. }
  149. }
  150. }
  151. // Hop-by-hop headers. These are removed when sent to the backend.
  152. // As of RFC 7230, hop-by-hop headers are required to appear in the
  153. // Connection header field. These are the headers defined by the
  154. // obsoleted RFC 2616 (section 13.5.1) and are used for backward
  155. // compatibility.
  156. var hopHeaders = []string{
  157. "Connection",
  158. "Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
  159. "Keep-Alive",
  160. "Proxy-Authenticate",
  161. "Proxy-Authorization",
  162. "Te", // canonicalized version of "TE"
  163. "Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
  164. "Transfer-Encoding",
  165. "Upgrade",
  166. }
  167. func (p *ReverseProxy) defaultErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
  168. p.logf("http: proxy error: %v", err)
  169. rw.WriteHeader(http.StatusBadGateway)
  170. }
  171. func (p *ReverseProxy) getErrorHandler() func(http.ResponseWriter, *http.Request, error) {
  172. if p.ErrorHandler != nil {
  173. return p.ErrorHandler
  174. }
  175. return p.defaultErrorHandler
  176. }
  177. // modifyResponse conditionally runs the optional ModifyResponse hook
  178. // and reports whether the request should proceed.
  179. func (p *ReverseProxy) modifyResponse(rw http.ResponseWriter, res *http.Response, req *http.Request) bool {
  180. if p.ModifyResponse == nil {
  181. return true
  182. }
  183. if err := p.ModifyResponse(res); err != nil {
  184. res.Body.Close()
  185. p.getErrorHandler()(rw, req, err)
  186. return false
  187. }
  188. return true
  189. }
  190. func parseBasicAuth(auth string) (username, password string, ok bool) {
  191. const prefix = "Basic "
  192. // Case insensitive prefix match. See Issue 22736.
  193. if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
  194. return
  195. }
  196. c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
  197. if err != nil {
  198. return
  199. }
  200. cs := string(c)
  201. s := strings.IndexByte(cs, ':')
  202. if s < 0 {
  203. return
  204. }
  205. return cs[:s], cs[s+1:], true
  206. }
  207. func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  208. transport := p.Transport
  209. if transport == nil {
  210. transport = http.DefaultTransport
  211. }
  212. ctx := req.Context()
  213. if cn, ok := rw.(http.CloseNotifier); ok {
  214. var cancel context.CancelFunc
  215. ctx, cancel = context.WithCancel(ctx)
  216. defer cancel()
  217. notifyChan := cn.CloseNotify()
  218. go func() {
  219. select {
  220. case <-notifyChan:
  221. cancel()
  222. case <-ctx.Done():
  223. }
  224. }()
  225. }
  226. outreq := req.Clone(ctx)
  227. if req.ContentLength == 0 {
  228. outreq.Body = nil // Issue 16036: nil Body for http.Transport retries
  229. }
  230. if outreq.Header == nil {
  231. outreq.Header = make(http.Header) // Issue 33142: historical behavior was to always allocate
  232. }
  233. p.Director(outreq)
  234. outreq.Close = false
  235. reqUpType := upgradeType(outreq.Header)
  236. removeConnectionHeaders(outreq.Header)
  237. // Remove hop-by-hop headers to the backend. Especially
  238. // important is "Connection" because we want a persistent
  239. // connection, regardless of what the client sent to us.
  240. for _, h := range hopHeaders {
  241. hv := outreq.Header.Get(h)
  242. if hv == "" {
  243. continue
  244. }
  245. if h == "Te" && hv == "trailers" {
  246. // Issue 21096: tell backend applications that
  247. // care about trailer support that we support
  248. // trailers. (We do, but we don't go out of
  249. // our way to advertise that unless the
  250. // incoming client request thought it was
  251. // worth mentioning)
  252. continue
  253. }
  254. outreq.Header.Del(h)
  255. }
  256. // After stripping all the hop-by-hop connection headers above, add back any
  257. // necessary for protocol upgrades, such as for websockets.
  258. if reqUpType != "" {
  259. outreq.Header.Set("Connection", "Upgrade")
  260. outreq.Header.Set("Upgrade", reqUpType)
  261. }
  262. if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
  263. // If we aren't the first proxy retain prior
  264. // X-Forwarded-For information as a comma+space
  265. // separated list and fold multiple headers into one.
  266. prior, ok := outreq.Header["X-Forwarded-For"]
  267. omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
  268. if len(prior) > 0 {
  269. clientIP = strings.Join(prior, ", ") + ", " + clientIP
  270. }
  271. if !omit {
  272. outreq.Header.Set("X-Forwarded-For", clientIP)
  273. }
  274. }
  275. res, err := transport.RoundTrip(outreq)
  276. if err != nil {
  277. p.getErrorHandler()(rw, outreq, err)
  278. return
  279. }
  280. // Deal with 101 Switching Protocols responses: (WebSocket, h2c, etc)
  281. if res.StatusCode == http.StatusSwitchingProtocols {
  282. if !p.modifyResponse(rw, res, outreq) {
  283. return
  284. }
  285. p.handleUpgradeResponse(rw, outreq, res)
  286. return
  287. }
  288. removeConnectionHeaders(res.Header)
  289. for _, h := range hopHeaders {
  290. res.Header.Del(h)
  291. }
  292. if !p.modifyResponse(rw, res, outreq) {
  293. return
  294. }
  295. copyHeader(rw.Header(), res.Header)
  296. // The "Trailer" header isn't included in the Transport's response,
  297. // at least for *http.Transport. Build it up from Trailer.
  298. announcedTrailers := len(res.Trailer)
  299. if announcedTrailers > 0 {
  300. trailerKeys := make([]string, 0, len(res.Trailer))
  301. for k := range res.Trailer {
  302. trailerKeys = append(trailerKeys, k)
  303. }
  304. rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
  305. }
  306. rw.WriteHeader(res.StatusCode)
  307. err = p.copyResponse(rw, res.Body, p.flushInterval(res))
  308. if err != nil {
  309. defer res.Body.Close()
  310. // Since we're streaming the response, if we run into an error all we can do
  311. // is abort the request. Issue 23643: ReverseProxy should use ErrAbortHandler
  312. // on read error while copying body.
  313. if !shouldPanicOnCopyError(req) {
  314. p.logf("suppressing panic for copyResponse error in test; copy error: %v", err)
  315. return
  316. }
  317. panic(http.ErrAbortHandler)
  318. }
  319. res.Body.Close() // close now, instead of defer, to populate res.Trailer
  320. if len(res.Trailer) > 0 {
  321. // Force chunking if we saw a response trailer.
  322. // This prevents net/http from calculating the length for short
  323. // bodies and adding a Content-Length.
  324. if fl, ok := rw.(http.Flusher); ok {
  325. fl.Flush()
  326. }
  327. }
  328. if len(res.Trailer) == announcedTrailers {
  329. copyHeader(rw.Header(), res.Trailer)
  330. return
  331. }
  332. for k, vv := range res.Trailer {
  333. k = http.TrailerPrefix + k
  334. for _, v := range vv {
  335. rw.Header().Add(k, v)
  336. }
  337. }
  338. }
  339. var inOurTests bool // whether we're in our own tests
  340. // shouldPanicOnCopyError reports whether the reverse proxy should
  341. // panic with http.ErrAbortHandler. This is the right thing to do by
  342. // default, but Go 1.10 and earlier did not, so existing unit tests
  343. // weren't expecting panics. Only panic in our own tests, or when
  344. // running under the HTTP server.
  345. func shouldPanicOnCopyError(req *http.Request) bool {
  346. if inOurTests {
  347. // Our tests know to handle this panic.
  348. return true
  349. }
  350. if req.Context().Value(http.ServerContextKey) != nil {
  351. // We seem to be running under an HTTP server, so
  352. // it'll recover the panic.
  353. return true
  354. }
  355. // Otherwise act like Go 1.10 and earlier to not break
  356. // existing tests.
  357. return false
  358. }
  359. // removeConnectionHeaders removes hop-by-hop headers listed in the "Connection" header of h.
  360. // See RFC 7230, section 6.1
  361. func removeConnectionHeaders(h http.Header) {
  362. for _, f := range h["Connection"] {
  363. for _, sf := range strings.Split(f, ",") {
  364. if sf = textproto.TrimString(sf); sf != "" {
  365. h.Del(sf)
  366. }
  367. }
  368. }
  369. }
  370. // flushInterval returns the p.FlushInterval value, conditionally
  371. // overriding its value for a specific request/response.
  372. func (p *ReverseProxy) flushInterval(res *http.Response) time.Duration {
  373. resCT := res.Header.Get("Content-Type")
  374. // For Server-Sent Events responses, flush immediately.
  375. // The MIME type is defined in https://www.w3.org/TR/eventsource/#text-event-stream
  376. if resCT == "text/event-stream" {
  377. return -1 // negative means immediately
  378. }
  379. // We might have the case of streaming for which Content-Length might be unset.
  380. if res.ContentLength == -1 {
  381. return -1
  382. }
  383. return p.FlushInterval
  384. }
  385. func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader, flushInterval time.Duration) error {
  386. if flushInterval != 0 {
  387. if wf, ok := dst.(writeFlusher); ok {
  388. mlw := &maxLatencyWriter{
  389. dst: wf,
  390. latency: flushInterval,
  391. }
  392. defer mlw.stop()
  393. // set up initial timer so headers get flushed even if body writes are delayed
  394. mlw.flushPending = true
  395. mlw.t = time.AfterFunc(flushInterval, mlw.delayedFlush)
  396. dst = mlw
  397. }
  398. }
  399. var buf []byte
  400. if p.BufferPool != nil {
  401. buf = p.BufferPool.Get()
  402. defer p.BufferPool.Put(buf)
  403. }
  404. _, err := p.copyBuffer(dst, src, buf)
  405. return err
  406. }
  407. // copyBuffer returns any write errors or non-EOF read errors, and the amount
  408. // of bytes written.
  409. func (p *ReverseProxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
  410. if len(buf) == 0 {
  411. buf = make([]byte, 32*1024)
  412. }
  413. var written int64
  414. for {
  415. nr, rerr := src.Read(buf)
  416. if rerr != nil && rerr != io.EOF && rerr != context.Canceled {
  417. p.logf("httputil: ReverseProxy read error during body copy: %v", rerr)
  418. }
  419. if nr > 0 {
  420. nw, werr := dst.Write(buf[:nr])
  421. if nw > 0 {
  422. written += int64(nw)
  423. }
  424. if werr != nil {
  425. return written, werr
  426. }
  427. if nr != nw {
  428. return written, io.ErrShortWrite
  429. }
  430. }
  431. if rerr != nil {
  432. if rerr == io.EOF {
  433. rerr = nil
  434. }
  435. return written, rerr
  436. }
  437. }
  438. }
  439. func (p *ReverseProxy) logf(format string, args ...interface{}) {
  440. if p.ErrorLog != nil {
  441. p.ErrorLog.Printf(format, args...)
  442. } else {
  443. log.Printf(format, args...)
  444. }
  445. }
  446. type writeFlusher interface {
  447. io.Writer
  448. http.Flusher
  449. }
  450. type maxLatencyWriter struct {
  451. dst writeFlusher
  452. latency time.Duration // non-zero; negative means to flush immediately
  453. mu sync.Mutex // protects t, flushPending, and dst.Flush
  454. t *time.Timer
  455. flushPending bool
  456. }
  457. func (m *maxLatencyWriter) Write(p []byte) (n int, err error) {
  458. m.mu.Lock()
  459. defer m.mu.Unlock()
  460. n, err = m.dst.Write(p)
  461. if m.latency < 0 {
  462. m.dst.Flush()
  463. return
  464. }
  465. if m.flushPending {
  466. return
  467. }
  468. if m.t == nil {
  469. m.t = time.AfterFunc(m.latency, m.delayedFlush)
  470. } else {
  471. m.t.Reset(m.latency)
  472. }
  473. m.flushPending = true
  474. return
  475. }
  476. func (m *maxLatencyWriter) delayedFlush() {
  477. m.mu.Lock()
  478. defer m.mu.Unlock()
  479. if !m.flushPending { // if stop was called but AfterFunc already started this goroutine
  480. return
  481. }
  482. m.dst.Flush()
  483. m.flushPending = false
  484. }
  485. func (m *maxLatencyWriter) stop() {
  486. m.mu.Lock()
  487. defer m.mu.Unlock()
  488. m.flushPending = false
  489. if m.t != nil {
  490. m.t.Stop()
  491. }
  492. }
  493. func upgradeType(h http.Header) string {
  494. if !httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade") {
  495. return ""
  496. }
  497. return strings.ToLower(h.Get("Upgrade"))
  498. }
  499. func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
  500. reqUpType := upgradeType(req.Header)
  501. resUpType := upgradeType(res.Header)
  502. if reqUpType != resUpType {
  503. p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch protocol %q when %q was requested", resUpType, reqUpType))
  504. return
  505. }
  506. hj, ok := rw.(http.Hijacker)
  507. if !ok {
  508. p.getErrorHandler()(rw, req, fmt.Errorf("can't switch protocols using non-Hijacker ResponseWriter type %T", rw))
  509. return
  510. }
  511. backConn, ok := res.Body.(io.ReadWriteCloser)
  512. if !ok {
  513. p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body"))
  514. return
  515. }
  516. backConnCloseCh := make(chan bool)
  517. go func() {
  518. // Ensure that the cancelation of a request closes the backend.
  519. // See issue https://golang.org/issue/35559.
  520. select {
  521. case <-req.Context().Done():
  522. case <-backConnCloseCh:
  523. }
  524. backConn.Close()
  525. }()
  526. defer close(backConnCloseCh)
  527. conn, brw, err := hj.Hijack()
  528. if err != nil {
  529. p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", err))
  530. return
  531. }
  532. defer conn.Close()
  533. copyHeader(rw.Header(), res.Header)
  534. res.Header = rw.Header()
  535. res.Body = nil // so res.Write only writes the headers; we have res.Body in backConn above
  536. if err := res.Write(brw); err != nil {
  537. p.getErrorHandler()(rw, req, fmt.Errorf("response write: %v", err))
  538. return
  539. }
  540. if err := brw.Flush(); err != nil {
  541. p.getErrorHandler()(rw, req, fmt.Errorf("response flush: %v", err))
  542. return
  543. }
  544. errc := make(chan error, 1)
  545. spc := switchProtocolCopier{user: conn, backend: backConn}
  546. go spc.copyToBackend(errc)
  547. go spc.copyFromBackend(errc)
  548. <-errc
  549. return
  550. }
  551. // switchProtocolCopier exists so goroutines proxying data back and
  552. // forth have nice names in stacks.
  553. type switchProtocolCopier struct {
  554. user, backend io.ReadWriter
  555. }
  556. func (c switchProtocolCopier) copyFromBackend(errc chan<- error) {
  557. _, err := io.Copy(c.user, c.backend)
  558. errc <- err
  559. }
  560. func (c switchProtocolCopier) copyToBackend(errc chan<- error) {
  561. _, err := io.Copy(c.backend, c.user)
  562. errc <- err
  563. }