dpcore.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package dpcore
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. "net/url"
  10. "path/filepath"
  11. "strings"
  12. "sync"
  13. "time"
  14. )
  15. var onExitFlushLoop func()
  16. const (
  17. defaultTimeout = time.Minute * 5
  18. )
  19. // ReverseProxy is an HTTP Handler that takes an incoming request and
  20. // sends it to another server, proxying the response back to the
  21. // client, support http, also support https tunnel using http.hijacker
  22. type ReverseProxy struct {
  23. // Set the timeout of the proxy server, default is 5 minutes
  24. Timeout time.Duration
  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. // default is http.DefaultTransport.
  34. Transport http.RoundTripper
  35. // FlushInterval specifies the flush interval
  36. // to flush to the client while copying the
  37. // response body. If zero, no periodic flushing is done.
  38. FlushInterval time.Duration
  39. // ErrorLog specifies an optional logger for errors
  40. // that occur when attempting to proxy the request.
  41. // If nil, logging goes to os.Stderr via the log package's
  42. // standard logger.
  43. ErrorLog *log.Logger
  44. // ModifyResponse is an optional function that
  45. // modifies the Response from the backend.
  46. // If it returns an error, the proxy returns a StatusBadGateway error.
  47. ModifyResponse func(*http.Response) error
  48. //Prepender is an optional prepend text for URL rewrite
  49. //
  50. Prepender string
  51. Verbal bool
  52. }
  53. type requestCanceler interface {
  54. CancelRequest(req *http.Request)
  55. }
  56. func NewDynamicProxyCore(target *url.URL, prepender string) *ReverseProxy {
  57. targetQuery := target.RawQuery
  58. director := func(req *http.Request) {
  59. req.URL.Scheme = target.Scheme
  60. req.URL.Host = target.Host
  61. req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
  62. // If Host is empty, the Request.Write method uses
  63. // the value of URL.Host.
  64. // force use URL.Host
  65. req.Host = req.URL.Host
  66. if targetQuery == "" || req.URL.RawQuery == "" {
  67. req.URL.RawQuery = targetQuery + req.URL.RawQuery
  68. } else {
  69. req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
  70. }
  71. if _, ok := req.Header["User-Agent"]; !ok {
  72. req.Header.Set("User-Agent", "")
  73. }
  74. }
  75. return &ReverseProxy{
  76. Director: director,
  77. Prepender: prepender,
  78. Verbal: false,
  79. }
  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. //Custom header rewriter functions
  253. if res.Header.Get("Location") != "" {
  254. //Custom redirection to this rproxy relative path
  255. fmt.Println(res.Header.Get("Location"))
  256. res.Header.Set("Location", filepath.ToSlash(filepath.Join(p.Prepender, res.Header.Get("Location"))))
  257. }
  258. // Copy header from response to client.
  259. copyHeader(rw.Header(), res.Header)
  260. // The "Trailer" header isn't included in the Transport's response, Build it up from Trailer.
  261. if len(res.Trailer) > 0 {
  262. trailerKeys := make([]string, 0, len(res.Trailer))
  263. for k := range res.Trailer {
  264. trailerKeys = append(trailerKeys, k)
  265. }
  266. rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
  267. }
  268. rw.WriteHeader(res.StatusCode)
  269. if len(res.Trailer) > 0 {
  270. // Force chunking if we saw a response trailer.
  271. // This prevents net/http from calculating the length for short
  272. // bodies and adding a Content-Length.
  273. if fl, ok := rw.(http.Flusher); ok {
  274. fl.Flush()
  275. }
  276. }
  277. p.copyResponse(rw, res.Body)
  278. // close now, instead of defer, to populate res.Trailer
  279. res.Body.Close()
  280. copyHeader(rw.Header(), res.Trailer)
  281. return nil
  282. }
  283. func (p *ReverseProxy) ProxyHTTPS(rw http.ResponseWriter, req *http.Request) error {
  284. hij, ok := rw.(http.Hijacker)
  285. if !ok {
  286. p.logf("http server does not support hijacker")
  287. return errors.New("http server does not support hijacker")
  288. }
  289. clientConn, _, err := hij.Hijack()
  290. if err != nil {
  291. if p.Verbal {
  292. p.logf("http: proxy error: %v", err)
  293. }
  294. return err
  295. }
  296. proxyConn, err := net.Dial("tcp", req.URL.Host)
  297. if err != nil {
  298. if p.Verbal {
  299. p.logf("http: proxy error: %v", err)
  300. }
  301. return err
  302. }
  303. // The returned net.Conn may have read or write deadlines
  304. // already set, depending on the configuration of the
  305. // Server, to set or clear those deadlines as needed
  306. // we set timeout to 5 minutes
  307. deadline := time.Now()
  308. if p.Timeout == 0 {
  309. deadline = deadline.Add(time.Minute * 5)
  310. } else {
  311. deadline = deadline.Add(p.Timeout)
  312. }
  313. err = clientConn.SetDeadline(deadline)
  314. if err != nil {
  315. if p.Verbal {
  316. p.logf("http: proxy error: %v", err)
  317. }
  318. return err
  319. }
  320. err = proxyConn.SetDeadline(deadline)
  321. if err != nil {
  322. if p.Verbal {
  323. p.logf("http: proxy error: %v", err)
  324. }
  325. return err
  326. }
  327. _, err = clientConn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
  328. if err != nil {
  329. if p.Verbal {
  330. p.logf("http: proxy error: %v", err)
  331. }
  332. return err
  333. }
  334. go func() {
  335. io.Copy(clientConn, proxyConn)
  336. clientConn.Close()
  337. proxyConn.Close()
  338. }()
  339. io.Copy(proxyConn, clientConn)
  340. proxyConn.Close()
  341. clientConn.Close()
  342. return nil
  343. }
  344. func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) error {
  345. if req.Method == "CONNECT" {
  346. err := p.ProxyHTTPS(rw, req)
  347. return err
  348. } else {
  349. err := p.ProxyHTTP(rw, req)
  350. return err
  351. }
  352. }