dpcore.go 13 KB

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