1
0

Server.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. Special Routing Rules, bypass most of the limitations
  21. */
  22. //Check if there are external routing rule matches.
  23. //If yes, route them via external rr
  24. matchedRoutingRule := h.Parent.GetMatchingRoutingRule(r)
  25. if matchedRoutingRule != nil {
  26. //Matching routing rule found. Let the sub-router handle it
  27. if matchedRoutingRule.UseSystemAccessControl {
  28. //This matching rule request system access control.
  29. //check access logic
  30. respWritten := h.handleAccessRouting(w, r)
  31. if respWritten {
  32. return
  33. }
  34. }
  35. matchedRoutingRule.Route(w, r)
  36. return
  37. }
  38. /*
  39. General Access Check
  40. */
  41. respWritten := h.handleAccessRouting(w, r)
  42. if respWritten {
  43. return
  44. }
  45. /*
  46. Redirection Routing
  47. */
  48. //Check if this is a redirection url
  49. if h.Parent.Option.RedirectRuleTable.IsRedirectable(r) {
  50. statusCode := h.Parent.Option.RedirectRuleTable.HandleRedirect(w, r)
  51. h.logRequest(r, statusCode != 500, statusCode, "redirect", "")
  52. return
  53. }
  54. //Extract request host to see if it is virtual directory or subdomain
  55. domainOnly := r.Host
  56. if strings.Contains(r.Host, ":") {
  57. hostPath := strings.Split(r.Host, ":")
  58. domainOnly = hostPath[0]
  59. }
  60. /*
  61. Subdomain Routing
  62. */
  63. if strings.Contains(r.Host, ".") {
  64. //This might be a subdomain. See if there are any subdomain proxy router for this
  65. sep := h.Parent.getSubdomainProxyEndpointFromHostname(domainOnly)
  66. if sep != nil {
  67. if sep.RequireBasicAuth {
  68. err := h.handleBasicAuthRouting(w, r, sep)
  69. if err != nil {
  70. return
  71. }
  72. }
  73. h.subdomainRequest(w, r, sep)
  74. return
  75. }
  76. }
  77. /*
  78. Virtual Directory Routing
  79. */
  80. //Clean up the request URI
  81. proxyingPath := strings.TrimSpace(r.RequestURI)
  82. targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath)
  83. if targetProxyEndpoint != nil {
  84. if targetProxyEndpoint.RequireBasicAuth {
  85. err := h.handleBasicAuthRouting(w, r, targetProxyEndpoint)
  86. if err != nil {
  87. return
  88. }
  89. }
  90. h.proxyRequest(w, r, targetProxyEndpoint)
  91. } else if !strings.HasSuffix(proxyingPath, "/") {
  92. potentialProxtEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath + "/")
  93. if potentialProxtEndpoint != nil {
  94. //Missing tailing slash. Redirect to target proxy endpoint
  95. http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
  96. } else {
  97. //Passthrough the request to root
  98. h.proxyRequest(w, r, h.Parent.Root)
  99. }
  100. } else {
  101. //No routing rules found. Route to root.
  102. h.proxyRequest(w, r, h.Parent.Root)
  103. }
  104. }
  105. // Handle access routing logic. Return true if the request is handled or blocked by the access control logic
  106. // if the return value is false, you can continue process the response writer
  107. func (h *ProxyHandler) handleAccessRouting(w http.ResponseWriter, r *http.Request) bool {
  108. //Check if this ip is in blacklist
  109. clientIpAddr := geodb.GetRequesterIP(r)
  110. if h.Parent.Option.GeodbStore.IsBlacklisted(clientIpAddr) {
  111. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  112. w.WriteHeader(http.StatusForbidden)
  113. template, err := os.ReadFile("./web/forbidden.html")
  114. if err != nil {
  115. w.Write([]byte("403 - Forbidden"))
  116. } else {
  117. w.Write(template)
  118. }
  119. h.logRequest(r, false, 403, "blacklist", "")
  120. return true
  121. }
  122. //Check if this ip is in whitelist
  123. if !h.Parent.Option.GeodbStore.IsWhitelisted(clientIpAddr) {
  124. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  125. w.WriteHeader(http.StatusForbidden)
  126. template, err := os.ReadFile("./web/forbidden.html")
  127. if err != nil {
  128. w.Write([]byte("403 - Forbidden"))
  129. } else {
  130. w.Write(template)
  131. }
  132. h.logRequest(r, false, 403, "whitelist", "")
  133. return true
  134. }
  135. return false
  136. }