reverseproxy.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. "io"
  9. "log"
  10. "net"
  11. "net/http"
  12. "net/url"
  13. "strings"
  14. "sync"
  15. "time"
  16. frpIo "github.com/fatedier/frp/utils/io"
  17. )
  18. // onExitFlushLoop is a callback set by tests to detect the state of the
  19. // flushLoop() goroutine.
  20. var onExitFlushLoop func()
  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. type ReverseProxy struct {
  25. // Director must be a function which modifies
  26. // the request into a new request to be sent
  27. // using Transport. Its response is then copied
  28. // back to the original client unmodified.
  29. // Director must not access the provided Request
  30. // after returning.
  31. Director func(*http.Request)
  32. // The transport used to perform proxy requests.
  33. // If nil, http.DefaultTransport is used.
  34. Transport http.RoundTripper
  35. // FlushInterval specifies the flush interval
  36. // to flush to the client while copying the
  37. // response body.
  38. // If zero, no periodic flushing is done.
  39. FlushInterval time.Duration
  40. // ErrorLog specifies an optional logger for errors
  41. // that occur when attempting to proxy the request.
  42. // If nil, logging goes to os.Stderr via the log package's
  43. // standard logger.
  44. ErrorLog *log.Logger
  45. // BufferPool optionally specifies a buffer pool to
  46. // get byte slices for use by io.CopyBuffer when
  47. // copying HTTP response bodies.
  48. BufferPool BufferPool
  49. // ModifyResponse is an optional function that
  50. // modifies the Response from the backend.
  51. // If it returns an error, the proxy returns a StatusBadGateway error.
  52. ModifyResponse func(*http.Response) error
  53. WebSocketDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
  54. }
  55. // A BufferPool is an interface for getting and returning temporary
  56. // byte slices for use by io.CopyBuffer.
  57. type BufferPool interface {
  58. Get() []byte
  59. Put([]byte)
  60. }
  61. func singleJoiningSlash(a, b string) string {
  62. aslash := strings.HasSuffix(a, "/")
  63. bslash := strings.HasPrefix(b, "/")
  64. switch {
  65. case aslash && bslash:
  66. return a + b[1:]
  67. case !aslash && !bslash:
  68. return a + "/" + b
  69. }
  70. return a + b
  71. }
  72. // NewSingleHostReverseProxy returns a new ReverseProxy that routes
  73. // URLs to the scheme, host, and base path provided in target. If the
  74. // target's path is "/base" and the incoming request was for "/dir",
  75. // the target request will be for /base/dir.
  76. // NewSingleHostReverseProxy does not rewrite the Host header.
  77. // To rewrite Host headers, use ReverseProxy directly with a custom
  78. // Director policy.
  79. func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
  80. targetQuery := target.RawQuery
  81. director := func(req *http.Request) {
  82. req.URL.Scheme = target.Scheme
  83. req.URL.Host = target.Host
  84. req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
  85. if targetQuery == "" || req.URL.RawQuery == "" {
  86. req.URL.RawQuery = targetQuery + req.URL.RawQuery
  87. } else {
  88. req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
  89. }
  90. if _, ok := req.Header["User-Agent"]; !ok {
  91. // explicitly disable User-Agent so it's not set to default value
  92. req.Header.Set("User-Agent", "")
  93. }
  94. }
  95. return &ReverseProxy{Director: director}
  96. }
  97. func copyHeader(dst, src http.Header) {
  98. for k, vv := range src {
  99. for _, v := range vv {
  100. dst.Add(k, v)
  101. }
  102. }
  103. }
  104. func cloneHeader(h http.Header) http.Header {
  105. h2 := make(http.Header, len(h))
  106. for k, vv := range h {
  107. vv2 := make([]string, len(vv))
  108. copy(vv2, vv)
  109. h2[k] = vv2
  110. }
  111. return h2
  112. }
  113. // Hop-by-hop headers. These are removed when sent to the backend.
  114. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
  115. var hopHeaders = []string{
  116. "Connection",
  117. "Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
  118. "Keep-Alive",
  119. "Proxy-Authenticate",
  120. "Proxy-Authorization",
  121. "Te", // canonicalized version of "TE"
  122. "Trailer", // not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
  123. "Transfer-Encoding",
  124. "Upgrade",
  125. }
  126. func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  127. if IsWebsocketRequest(req) {
  128. p.serveWebSocket(rw, req)
  129. } else {
  130. p.serveHTTP(rw, req)
  131. }
  132. }
  133. func (p *ReverseProxy) serveWebSocket(rw http.ResponseWriter, req *http.Request) {
  134. if p.WebSocketDialContext == nil {
  135. rw.WriteHeader(500)
  136. return
  137. }
  138. req = req.WithContext(context.WithValue(req.Context(), "url", req.URL.Path))
  139. req = req.WithContext(context.WithValue(req.Context(), "host", req.Host))
  140. targetConn, err := p.WebSocketDialContext(req.Context(), "tcp", "")
  141. if err != nil {
  142. rw.WriteHeader(501)
  143. return
  144. }
  145. defer targetConn.Close()
  146. p.Director(req)
  147. hijacker, ok := rw.(http.Hijacker)
  148. if !ok {
  149. rw.WriteHeader(500)
  150. return
  151. }
  152. conn, _, errHijack := hijacker.Hijack()
  153. if errHijack != nil {
  154. rw.WriteHeader(500)
  155. return
  156. }
  157. defer conn.Close()
  158. req.Write(targetConn)
  159. frpIo.Join(conn, targetConn)
  160. }
  161. func (p *ReverseProxy) serveHTTP(rw http.ResponseWriter, req *http.Request) {
  162. transport := p.Transport
  163. if transport == nil {
  164. transport = http.DefaultTransport
  165. }
  166. ctx := req.Context()
  167. if cn, ok := rw.(http.CloseNotifier); ok {
  168. var cancel context.CancelFunc
  169. ctx, cancel = context.WithCancel(ctx)
  170. defer cancel()
  171. notifyChan := cn.CloseNotify()
  172. go func() {
  173. select {
  174. case <-notifyChan:
  175. cancel()
  176. case <-ctx.Done():
  177. }
  178. }()
  179. }
  180. outreq := req.WithContext(ctx) // includes shallow copies of maps, but okay
  181. if req.ContentLength == 0 {
  182. outreq.Body = nil // Issue 16036: nil Body for http.Transport retries
  183. }
  184. outreq.Header = cloneHeader(req.Header)
  185. // Modify for frp
  186. outreq = outreq.WithContext(context.WithValue(outreq.Context(), "url", req.URL.Path))
  187. outreq = outreq.WithContext(context.WithValue(outreq.Context(), "host", req.Host))
  188. p.Director(outreq)
  189. outreq.Close = false
  190. // Remove hop-by-hop headers listed in the "Connection" header.
  191. // See RFC 2616, section 14.10.
  192. if c := outreq.Header.Get("Connection"); c != "" {
  193. for _, f := range strings.Split(c, ",") {
  194. if f = strings.TrimSpace(f); f != "" {
  195. outreq.Header.Del(f)
  196. }
  197. }
  198. }
  199. // Remove hop-by-hop headers to the backend. Especially
  200. // important is "Connection" because we want a persistent
  201. // connection, regardless of what the client sent to us.
  202. for _, h := range hopHeaders {
  203. if outreq.Header.Get(h) != "" {
  204. outreq.Header.Del(h)
  205. }
  206. }
  207. if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
  208. // If we aren't the first proxy retain prior
  209. // X-Forwarded-For information as a comma+space
  210. // separated list and fold multiple headers into one.
  211. if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
  212. clientIP = strings.Join(prior, ", ") + ", " + clientIP
  213. }
  214. outreq.Header.Set("X-Forwarded-For", clientIP)
  215. }
  216. res, err := transport.RoundTrip(outreq)
  217. if err != nil {
  218. p.logf("http: proxy error: %v", err)
  219. rw.WriteHeader(http.StatusNotFound)
  220. rw.Write([]byte(NotFound))
  221. return
  222. }
  223. // Remove hop-by-hop headers listed in the
  224. // "Connection" header of the response.
  225. if c := res.Header.Get("Connection"); c != "" {
  226. for _, f := range strings.Split(c, ",") {
  227. if f = strings.TrimSpace(f); f != "" {
  228. res.Header.Del(f)
  229. }
  230. }
  231. }
  232. for _, h := range hopHeaders {
  233. res.Header.Del(h)
  234. }
  235. if p.ModifyResponse != nil {
  236. if err := p.ModifyResponse(res); err != nil {
  237. p.logf("http: proxy error: %v", err)
  238. rw.WriteHeader(http.StatusBadGateway)
  239. return
  240. }
  241. }
  242. copyHeader(rw.Header(), res.Header)
  243. // The "Trailer" header isn't included in the Transport's response,
  244. // at least for *http.Transport. Build it up from Trailer.
  245. announcedTrailers := len(res.Trailer)
  246. if announcedTrailers > 0 {
  247. trailerKeys := make([]string, 0, len(res.Trailer))
  248. for k := range res.Trailer {
  249. trailerKeys = append(trailerKeys, k)
  250. }
  251. rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
  252. }
  253. rw.WriteHeader(res.StatusCode)
  254. if len(res.Trailer) > 0 {
  255. // Force chunking if we saw a response trailer.
  256. // This prevents net/http from calculating the length for short
  257. // bodies and adding a Content-Length.
  258. if fl, ok := rw.(http.Flusher); ok {
  259. fl.Flush()
  260. }
  261. }
  262. p.copyResponse(rw, res.Body)
  263. res.Body.Close() // close now, instead of defer, to populate res.Trailer
  264. if len(res.Trailer) == announcedTrailers {
  265. copyHeader(rw.Header(), res.Trailer)
  266. return
  267. }
  268. for k, vv := range res.Trailer {
  269. k = http.TrailerPrefix + k
  270. for _, v := range vv {
  271. rw.Header().Add(k, v)
  272. }
  273. }
  274. }
  275. func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
  276. if p.FlushInterval != 0 {
  277. if wf, ok := dst.(writeFlusher); ok {
  278. mlw := &maxLatencyWriter{
  279. dst: wf,
  280. latency: p.FlushInterval,
  281. done: make(chan bool),
  282. }
  283. go mlw.flushLoop()
  284. defer mlw.stop()
  285. dst = mlw
  286. }
  287. }
  288. var buf []byte
  289. if p.BufferPool != nil {
  290. buf = p.BufferPool.Get()
  291. }
  292. p.copyBuffer(dst, src, buf)
  293. if p.BufferPool != nil {
  294. p.BufferPool.Put(buf)
  295. }
  296. }
  297. func (p *ReverseProxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
  298. if len(buf) == 0 {
  299. buf = make([]byte, 32*1024)
  300. }
  301. var written int64
  302. for {
  303. nr, rerr := src.Read(buf)
  304. if rerr != nil && rerr != io.EOF && rerr != context.Canceled {
  305. p.logf("httputil: ReverseProxy read error during body copy: %v", rerr)
  306. }
  307. if nr > 0 {
  308. nw, werr := dst.Write(buf[:nr])
  309. if nw > 0 {
  310. written += int64(nw)
  311. }
  312. if werr != nil {
  313. return written, werr
  314. }
  315. if nr != nw {
  316. return written, io.ErrShortWrite
  317. }
  318. }
  319. if rerr != nil {
  320. return written, rerr
  321. }
  322. }
  323. }
  324. func (p *ReverseProxy) logf(format string, args ...interface{}) {
  325. if p.ErrorLog != nil {
  326. p.ErrorLog.Printf(format, args...)
  327. } else {
  328. log.Printf(format, args...)
  329. }
  330. }
  331. type writeFlusher interface {
  332. io.Writer
  333. http.Flusher
  334. }
  335. type maxLatencyWriter struct {
  336. dst writeFlusher
  337. latency time.Duration
  338. mu sync.Mutex // protects Write + Flush
  339. done chan bool
  340. }
  341. func (m *maxLatencyWriter) Write(p []byte) (int, error) {
  342. m.mu.Lock()
  343. defer m.mu.Unlock()
  344. return m.dst.Write(p)
  345. }
  346. func (m *maxLatencyWriter) flushLoop() {
  347. t := time.NewTicker(m.latency)
  348. defer t.Stop()
  349. for {
  350. select {
  351. case <-m.done:
  352. if onExitFlushLoop != nil {
  353. onExitFlushLoop()
  354. }
  355. return
  356. case <-t.C:
  357. m.mu.Lock()
  358. m.dst.Flush()
  359. m.mu.Unlock()
  360. }
  361. }
  362. }
  363. func (m *maxLatencyWriter) stop() { m.done <- true }
  364. func IsWebsocketRequest(req *http.Request) bool {
  365. containsHeader := func(name, value string) bool {
  366. items := strings.Split(req.Header.Get(name), ",")
  367. for _, item := range items {
  368. if value == strings.ToLower(strings.TrimSpace(item)) {
  369. return true
  370. }
  371. }
  372. return false
  373. }
  374. return containsHeader("Connection", "upgrade") && containsHeader("Upgrade", "websocket")
  375. }