dpcore.go 12 KB

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