Server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. //Check if this ip is in whitelist
  37. if !h.Parent.Option.GeodbStore.IsWhitelisted(clientIpAddr) {
  38. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  39. w.WriteHeader(http.StatusForbidden)
  40. template, err := os.ReadFile("./web/forbidden.html")
  41. if err != nil {
  42. w.Write([]byte("403 - Forbidden"))
  43. } else {
  44. w.Write(template)
  45. }
  46. h.logRequest(r, false, 403, "whitelist", "")
  47. return
  48. }
  49. /*
  50. Redirection Routing
  51. */
  52. //Check if this is a redirection url
  53. if h.Parent.Option.RedirectRuleTable.IsRedirectable(r) {
  54. statusCode := h.Parent.Option.RedirectRuleTable.HandleRedirect(w, r)
  55. h.logRequest(r, statusCode != 500, statusCode, "redirect", "")
  56. return
  57. }
  58. //Check if there are external routing rule matches.
  59. //If yes, route them via external rr
  60. matchedRoutingRule := h.Parent.GetMatchingRoutingRule(r)
  61. if matchedRoutingRule != nil {
  62. //Matching routing rule found. Let the sub-router handle it
  63. matchedRoutingRule.Route(w, r)
  64. return
  65. }
  66. //Extract request host to see if it is virtual directory or subdomain
  67. domainOnly := r.Host
  68. if strings.Contains(r.Host, ":") {
  69. hostPath := strings.Split(r.Host, ":")
  70. domainOnly = hostPath[0]
  71. }
  72. /*
  73. Subdomain Routing
  74. */
  75. if strings.Contains(r.Host, ".") {
  76. //This might be a subdomain. See if there are any subdomain proxy router for this
  77. sep := h.Parent.getSubdomainProxyEndpointFromHostname(domainOnly)
  78. if sep != nil {
  79. if sep.RequireBasicAuth {
  80. err := h.handleBasicAuthRouting(w, r, sep)
  81. if err != nil {
  82. return
  83. }
  84. }
  85. h.subdomainRequest(w, r, sep)
  86. return
  87. }
  88. }
  89. /*
  90. Virtual Directory Routing
  91. */
  92. //Clean up the request URI
  93. proxyingPath := strings.TrimSpace(r.RequestURI)
  94. targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath)
  95. if targetProxyEndpoint != nil {
  96. if targetProxyEndpoint.RequireBasicAuth {
  97. err := h.handleBasicAuthRouting(w, r, targetProxyEndpoint)
  98. if err != nil {
  99. return
  100. }
  101. }
  102. h.proxyRequest(w, r, targetProxyEndpoint)
  103. } else if !strings.HasSuffix(proxyingPath, "/") {
  104. potentialProxtEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath + "/")
  105. if potentialProxtEndpoint != nil {
  106. //Missing tailing slash. Redirect to target proxy endpoint
  107. http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
  108. } else {
  109. //Passthrough the request to root
  110. h.proxyRequest(w, r, h.Parent.Root)
  111. }
  112. } else {
  113. //No routing rules found. Route to root.
  114. h.proxyRequest(w, r, h.Parent.Root)
  115. }
  116. }