dpcore.go 14 KB

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