dpcore.go 14 KB

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