1
0

Server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package dynamicproxy
  2. import (
  3. "net/http"
  4. "os"
  5. "strings"
  6. "imuslab.com/zoraxy/mod/geodb"
  7. )
  8. /*
  9. Server.go
  10. Main server for dynamic proxy core
  11. Routing Handler Priority (High to Low)
  12. - Blacklist
  13. - Whitelist
  14. - Redirectable
  15. - Subdomain Routing
  16. - Vitrual Directory Routing
  17. */
  18. func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  19. /*
  20. General Access Check
  21. */
  22. //Check if this ip is in blacklist
  23. clientIpAddr := geodb.GetRequesterIP(r)
  24. if h.Parent.Option.GeodbStore.IsBlacklisted(clientIpAddr) {
  25. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  26. w.WriteHeader(http.StatusForbidden)
  27. template, err := os.ReadFile("./web/forbidden.html")
  28. if err != nil {
  29. w.Write([]byte("403 - Forbidden"))
  30. } else {
  31. w.Write(template)
  32. }
  33. h.logRequest(r, false, 403, "blacklist", "")
  34. return
  35. }
  36. /*
  37. Redirection Routing
  38. */
  39. //Check if this is a redirection url
  40. if h.Parent.Option.RedirectRuleTable.IsRedirectable(r) {
  41. statusCode := h.Parent.Option.RedirectRuleTable.HandleRedirect(w, r)
  42. h.logRequest(r, statusCode != 500, statusCode, "redirect", "")
  43. return
  44. }
  45. //Check if there are external routing rule matches.
  46. //If yes, route them via external rr
  47. matchedRoutingRule := h.Parent.GetMatchingRoutingRule(r)
  48. if matchedRoutingRule != nil {
  49. //Matching routing rule found. Let the sub-router handle it
  50. matchedRoutingRule.Route(w, r)
  51. return
  52. }
  53. //Extract request host to see if it is virtual directory or subdomain
  54. domainOnly := r.Host
  55. if strings.Contains(r.Host, ":") {
  56. hostPath := strings.Split(r.Host, ":")
  57. domainOnly = hostPath[0]
  58. }
  59. /*
  60. Subdomain Routing
  61. */
  62. if strings.Contains(r.Host, ".") {
  63. //This might be a subdomain. See if there are any subdomain proxy router for this
  64. sep := h.Parent.getSubdomainProxyEndpointFromHostname(domainOnly)
  65. if sep != nil {
  66. if sep.RequireBasicAuth {
  67. err := h.handleBasicAuthRouting(w, r, sep)
  68. if err != nil {
  69. return
  70. }
  71. }
  72. h.subdomainRequest(w, r, sep)
  73. return
  74. }
  75. }
  76. /*
  77. Virtual Directory Routing
  78. */
  79. //Clean up the request URI
  80. proxyingPath := strings.TrimSpace(r.RequestURI)
  81. targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath)
  82. if targetProxyEndpoint != nil {
  83. if targetProxyEndpoint.RequireBasicAuth {
  84. err := h.handleBasicAuthRouting(w, r, targetProxyEndpoint)
  85. if err != nil {
  86. return
  87. }
  88. }
  89. h.proxyRequest(w, r, targetProxyEndpoint)
  90. } else if !strings.HasSuffix(proxyingPath, "/") {
  91. potentialProxtEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath + "/")
  92. if potentialProxtEndpoint != nil {
  93. //Missing tailing slash. Redirect to target proxy endpoint
  94. http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
  95. } else {
  96. //Passthrough the request to root
  97. h.proxyRequest(w, r, h.Parent.Root)
  98. }
  99. } else {
  100. //No routing rules found. Route to root.
  101. h.proxyRequest(w, r, h.Parent.Root)
  102. }
  103. }