proxyRequestHandler.go 3.6 KB

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