proxyRequestHandler.go 3.3 KB

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