proxyRequestHandler.go 4.6 KB

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