reverse.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package reverseproxy
  2. import (
  3. "errors"
  4. "io"
  5. "log"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "sync"
  11. "time"
  12. )
  13. var onExitFlushLoop func()
  14. const (
  15. defaultTimeout = time.Minute * 5
  16. )
  17. // ReverseProxy is an HTTP Handler that takes an incoming request and
  18. // sends it to another server, proxying the response back to the
  19. // client, support http, also support https tunnel using http.hijacker
  20. type ReverseProxy struct {
  21. // Set the timeout of the proxy server, default is 5 minutes
  22. Timeout time.Duration
  23. // Director must be a function which modifies
  24. // the request into a new request to be sent
  25. // using Transport. Its response is then copied
  26. // back to the original client unmodified.
  27. // Director must not access the provided Request
  28. // after returning.
  29. Director func(*http.Request)
  30. // The transport used to perform proxy requests.
  31. // default is http.DefaultTransport.
  32. Transport http.RoundTripper
  33. // FlushInterval specifies the flush interval
  34. // to flush to the client while copying the
  35. // response body. If zero, no periodic flushing is done.
  36. FlushInterval time.Duration
  37. // ErrorLog specifies an optional logger for errors
  38. // that occur when attempting to proxy the request.
  39. // If nil, logging goes to os.Stderr via the log package's
  40. // standard logger.
  41. ErrorLog *log.Logger
  42. // ModifyResponse is an optional function that
  43. // modifies the Response from the backend.
  44. // If it returns an error, the proxy returns a StatusBadGateway error.
  45. ModifyResponse func(*http.Response) error
  46. Verbal bool
  47. }
  48. type requestCanceler interface {
  49. CancelRequest(req *http.Request)
  50. }
  51. // NewReverseProxy returns a new ReverseProxy that routes
  52. // URLs to the scheme, host, and base path provided in target. If the
  53. // target's path is "/base" and the incoming request was for "/dir",
  54. // the target request will be for /base/dir. if the target's query is a=10
  55. // and the incoming request's query is b=100, the target's request's query
  56. // will be a=10&b=100.
  57. // NewReverseProxy does not rewrite the Host header.
  58. // To rewrite Host headers, use ReverseProxy directly with a custom
  59. // Director policy.
  60. func NewReverseProxy(target *url.URL) *ReverseProxy {
  61. targetQuery := target.RawQuery
  62. director := func(req *http.Request) {
  63. req.URL.Scheme = target.Scheme
  64. req.URL.Host = target.Host
  65. req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
  66. // If Host is empty, the Request.Write method uses
  67. // the value of URL.Host.
  68. // force use URL.Host
  69. req.Host = req.URL.Host
  70. if targetQuery == "" || req.URL.RawQuery == "" {
  71. req.URL.RawQuery = targetQuery + req.URL.RawQuery
  72. } else {
  73. req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
  74. }
  75. if _, ok := req.Header["User-Agent"]; !ok {
  76. req.Header.Set("User-Agent", "")
  77. }
  78. }
  79. return &ReverseProxy{Director: director, Verbal: false}
  80. }
  81. func singleJoiningSlash(a, b string) string {
  82. aslash := strings.HasSuffix(a, "/")
  83. bslash := strings.HasPrefix(b, "/")
  84. switch {
  85. case aslash && bslash:
  86. return a + b[1:]
  87. case !aslash && !bslash:
  88. return a + "/" + b
  89. }
  90. return a + b
  91. }
  92. func copyHeader(dst, src http.Header) {
  93. for k, vv := range src {
  94. for _, v := range vv {
  95. dst.Add(k, v)
  96. }
  97. }
  98. }
  99. // Hop-by-hop headers. These are removed when sent to the backend.
  100. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
  101. var hopHeaders = []string{
  102. //"Connection",
  103. "Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
  104. "Keep-Alive",
  105. "Proxy-Authenticate",
  106. "Proxy-Authorization",
  107. "Te", // canonicalized version of "TE"
  108. "Trailer", // not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
  109. "Transfer-Encoding",
  110. //"Upgrade",
  111. }
  112. func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
  113. if p.FlushInterval != 0 {
  114. if wf, ok := dst.(writeFlusher); ok {
  115. mlw := &maxLatencyWriter{
  116. dst: wf,
  117. latency: p.FlushInterval,
  118. done: make(chan bool),
  119. }
  120. go mlw.flushLoop()
  121. defer mlw.stop()
  122. dst = mlw
  123. }
  124. }
  125. io.Copy(dst, src)
  126. }
  127. type writeFlusher interface {
  128. io.Writer
  129. http.Flusher
  130. }
  131. type maxLatencyWriter struct {
  132. dst writeFlusher
  133. latency time.Duration
  134. mu sync.Mutex
  135. done chan bool
  136. }
  137. func (m *maxLatencyWriter) Write(b []byte) (int, error) {
  138. m.mu.Lock()
  139. defer m.mu.Unlock()
  140. return m.dst.Write(b)
  141. }
  142. func (m *maxLatencyWriter) flushLoop() {
  143. t := time.NewTicker(m.latency)
  144. defer t.Stop()
  145. for {
  146. select {
  147. case <-m.done:
  148. if onExitFlushLoop != nil {
  149. onExitFlushLoop()
  150. }
  151. return
  152. case <-t.C:
  153. m.mu.Lock()
  154. m.dst.Flush()
  155. m.mu.Unlock()
  156. }
  157. }
  158. }
  159. func (m *maxLatencyWriter) stop() {
  160. m.done <- true
  161. }
  162. func (p *ReverseProxy) logf(format string, args ...interface{}) {
  163. if p.ErrorLog != nil {
  164. p.ErrorLog.Printf(format, args...)
  165. } else {
  166. log.Printf(format, args...)
  167. }
  168. }
  169. func removeHeaders(header http.Header) {
  170. // Remove hop-by-hop headers listed in the "Connection" header.
  171. if c := header.Get("Connection"); c != "" {
  172. for _, f := range strings.Split(c, ",") {
  173. if f = strings.TrimSpace(f); f != "" {
  174. header.Del(f)
  175. }
  176. }
  177. }
  178. // Remove hop-by-hop headers
  179. for _, h := range hopHeaders {
  180. if header.Get(h) != "" {
  181. header.Del(h)
  182. }
  183. }
  184. if header.Get("A-Upgrade") != "" {
  185. header.Set("Upgrade", header.Get("A-Upgrade"))
  186. header.Del("A-Upgrade")
  187. }
  188. }
  189. func addXForwardedForHeader(req *http.Request) {
  190. if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
  191. // If we aren't the first proxy retain prior
  192. // X-Forwarded-For information as a comma+space
  193. // separated list and fold multiple headers into one.
  194. if prior, ok := req.Header["X-Forwarded-For"]; ok {
  195. clientIP = strings.Join(prior, ", ") + ", " + clientIP
  196. }
  197. req.Header.Set("X-Forwarded-For", clientIP)
  198. }
  199. }
  200. func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) error {
  201. transport := p.Transport
  202. if transport == nil {
  203. transport = http.DefaultTransport
  204. }
  205. outreq := new(http.Request)
  206. // Shallow copies of maps, like header
  207. *outreq = *req
  208. if cn, ok := rw.(http.CloseNotifier); ok {
  209. if requestCanceler, ok := transport.(requestCanceler); ok {
  210. // After the Handler has returned, there is no guarantee
  211. // that the channel receives a value, so to make sure
  212. reqDone := make(chan struct{})
  213. defer close(reqDone)
  214. clientGone := cn.CloseNotify()
  215. go func() {
  216. select {
  217. case <-clientGone:
  218. requestCanceler.CancelRequest(outreq)
  219. case <-reqDone:
  220. }
  221. }()
  222. }
  223. }
  224. p.Director(outreq)
  225. outreq.Close = false
  226. // We may modify the header (shallow copied above), so we only copy it.
  227. outreq.Header = make(http.Header)
  228. copyHeader(outreq.Header, req.Header)
  229. // Remove hop-by-hop headers listed in the "Connection" header, Remove hop-by-hop headers.
  230. removeHeaders(outreq.Header)
  231. // Add X-Forwarded-For Header.
  232. addXForwardedForHeader(outreq)
  233. res, err := transport.RoundTrip(outreq)
  234. if err != nil {
  235. if p.Verbal {
  236. p.logf("http: proxy error: %v", err)
  237. }
  238. //rw.WriteHeader(http.StatusBadGateway)
  239. return err
  240. }
  241. // Remove hop-by-hop headers listed in the "Connection" header of the response, Remove hop-by-hop headers.
  242. removeHeaders(res.Header)
  243. if p.ModifyResponse != nil {
  244. if err := p.ModifyResponse(res); err != nil {
  245. if p.Verbal {
  246. p.logf("http: proxy error: %v", err)
  247. }
  248. //rw.WriteHeader(http.StatusBadGateway)
  249. return err
  250. }
  251. }
  252. // Copy header from response to client.
  253. copyHeader(rw.Header(), res.Header)
  254. // The "Trailer" header isn't included in the Transport's response, Build it up from Trailer.
  255. if len(res.Trailer) > 0 {
  256. trailerKeys := make([]string, 0, len(res.Trailer))
  257. for k := range res.Trailer {
  258. trailerKeys = append(trailerKeys, k)
  259. }
  260. rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
  261. }
  262. rw.WriteHeader(res.StatusCode)
  263. if len(res.Trailer) > 0 {
  264. // Force chunking if we saw a response trailer.
  265. // This prevents net/http from calculating the length for short
  266. // bodies and adding a Content-Length.
  267. if fl, ok := rw.(http.Flusher); ok {
  268. fl.Flush()
  269. }
  270. }
  271. p.copyResponse(rw, res.Body)
  272. // close now, instead of defer, to populate res.Trailer
  273. res.Body.Close()
  274. copyHeader(rw.Header(), res.Trailer)
  275. return nil
  276. }
  277. func (p *ReverseProxy) ProxyHTTPS(rw http.ResponseWriter, req *http.Request) error {
  278. hij, ok := rw.(http.Hijacker)
  279. if !ok {
  280. p.logf("http server does not support hijacker")
  281. return errors.New("http server does not support hijacker")
  282. }
  283. clientConn, _, err := hij.Hijack()
  284. if err != nil {
  285. if p.Verbal {
  286. p.logf("http: proxy error: %v", err)
  287. }
  288. return err
  289. }
  290. proxyConn, err := net.Dial("tcp", req.URL.Host)
  291. if err != nil {
  292. if p.Verbal {
  293. p.logf("http: proxy error: %v", err)
  294. }
  295. return err
  296. }
  297. // The returned net.Conn may have read or write deadlines
  298. // already set, depending on the configuration of the
  299. // Server, to set or clear those deadlines as needed
  300. // we set timeout to 5 minutes
  301. deadline := time.Now()
  302. if p.Timeout == 0 {
  303. deadline = deadline.Add(time.Minute * 5)
  304. } else {
  305. deadline = deadline.Add(p.Timeout)
  306. }
  307. err = clientConn.SetDeadline(deadline)
  308. if err != nil {
  309. if p.Verbal {
  310. p.logf("http: proxy error: %v", err)
  311. }
  312. return err
  313. }
  314. err = proxyConn.SetDeadline(deadline)
  315. if err != nil {
  316. if p.Verbal {
  317. p.logf("http: proxy error: %v", err)
  318. }
  319. return err
  320. }
  321. _, err = clientConn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
  322. if err != nil {
  323. if p.Verbal {
  324. p.logf("http: proxy error: %v", err)
  325. }
  326. return err
  327. }
  328. go func() {
  329. io.Copy(clientConn, proxyConn)
  330. clientConn.Close()
  331. proxyConn.Close()
  332. }()
  333. io.Copy(proxyConn, clientConn)
  334. proxyConn.Close()
  335. clientConn.Close()
  336. return nil
  337. }
  338. func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) error {
  339. if req.Method == "CONNECT" {
  340. err := p.ProxyHTTPS(rw, req)
  341. return err
  342. } else {
  343. err := p.ProxyHTTP(rw, req)
  344. return err
  345. }
  346. }