proxyRequestHandler.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package dynamicproxy
  2. import (
  3. "log"
  4. "net/http"
  5. "net/url"
  6. "imuslab.com/arozos/ReverseProxy/mod/websocketproxy"
  7. )
  8. func (router *Router) getTargetProxyEndpointFromRequestURI(requestURI string) *ProxyEndpoint {
  9. var targetProxyEndpoint *ProxyEndpoint = nil
  10. router.ProxyEndpoints.Range(func(key, value interface{}) bool {
  11. rootname := key.(string)
  12. if len(requestURI) >= len(rootname) && requestURI[:len(rootname)] == rootname {
  13. thisProxyEndpoint := value.(*ProxyEndpoint)
  14. targetProxyEndpoint = thisProxyEndpoint
  15. }
  16. return true
  17. })
  18. return targetProxyEndpoint
  19. }
  20. func (router *Router) getSubdomainProxyEndpointFromHostname(hostname string) *SubdomainEndpoint {
  21. var targetSubdomainEndpoint *SubdomainEndpoint = nil
  22. ep, ok := router.SubdomainEndpoint.Load(hostname)
  23. if ok {
  24. targetSubdomainEndpoint = ep.(*SubdomainEndpoint)
  25. }
  26. return targetSubdomainEndpoint
  27. }
  28. func (router *Router) rewriteURL(rooturl string, requestURL string) string {
  29. if len(requestURL) > len(rooturl) {
  30. return requestURL[len(rooturl):]
  31. }
  32. return ""
  33. }
  34. func (h *ProxyHandler) subdomainRequest(w http.ResponseWriter, r *http.Request, target *SubdomainEndpoint) {
  35. r.Header.Set("X-Forwarded-Host", r.Host)
  36. requestURL := r.URL.String()
  37. if r.Header["Upgrade"] != nil && r.Header["Upgrade"][0] == "websocket" {
  38. //Handle WebSocket request. Forward the custom Upgrade header and rewrite origin
  39. r.Header.Set("A-Upgrade", "websocket")
  40. wsRedirectionEndpoint := target.Domain
  41. if wsRedirectionEndpoint[len(wsRedirectionEndpoint)-1:] != "/" {
  42. //Append / to the end of the redirection endpoint if not exists
  43. wsRedirectionEndpoint = wsRedirectionEndpoint + "/"
  44. }
  45. if len(requestURL) > 0 && requestURL[:1] == "/" {
  46. //Remove starting / from request URL if exists
  47. requestURL = requestURL[1:]
  48. }
  49. u, _ := url.Parse("ws://" + wsRedirectionEndpoint + requestURL)
  50. if target.RequireTLS {
  51. u, _ = url.Parse("wss://" + wsRedirectionEndpoint + requestURL)
  52. }
  53. wspHandler := websocketproxy.NewProxy(u)
  54. wspHandler.ServeHTTP(w, r)
  55. return
  56. }
  57. r.Host = r.URL.Host
  58. err := target.Proxy.ServeHTTP(w, r)
  59. if err != nil {
  60. log.Println(err.Error())
  61. }
  62. }
  63. func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, target *ProxyEndpoint) {
  64. rewriteURL := h.Parent.rewriteURL(target.Root, r.RequestURI)
  65. r.URL, _ = url.Parse(rewriteURL)
  66. r.Header.Set("X-Forwarded-Host", r.Host)
  67. if r.Header["Upgrade"] != nil && r.Header["Upgrade"][0] == "websocket" {
  68. //Handle WebSocket request. Forward the custom Upgrade header and rewrite origin
  69. r.Header.Set("A-Upgrade", "websocket")
  70. wsRedirectionEndpoint := target.Domain
  71. if wsRedirectionEndpoint[len(wsRedirectionEndpoint)-1:] != "/" {
  72. wsRedirectionEndpoint = wsRedirectionEndpoint + "/"
  73. }
  74. u, _ := url.Parse("ws://" + wsRedirectionEndpoint + r.URL.String())
  75. if target.RequireTLS {
  76. u, _ = url.Parse("wss://" + wsRedirectionEndpoint + r.URL.String())
  77. }
  78. wspHandler := websocketproxy.NewProxy(u)
  79. wspHandler.ServeHTTP(w, r)
  80. return
  81. }
  82. r.Host = r.URL.Host
  83. err := target.Proxy.ServeHTTP(w, r)
  84. if err != nil {
  85. log.Println(err.Error())
  86. }
  87. }