123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package dynamicproxy
- import (
- "net/http"
- "os"
- "strings"
- "imuslab.com/zoraxy/mod/geodb"
- )
- func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-
-
-
- matchedRoutingRule := h.Parent.GetMatchingRoutingRule(r)
- if matchedRoutingRule != nil {
-
- if matchedRoutingRule.UseSystemAccessControl {
-
-
- respWritten := h.handleAccessRouting(w, r)
- if respWritten {
- return
- }
- }
- matchedRoutingRule.Route(w, r)
- return
- }
-
- respWritten := h.handleAccessRouting(w, r)
- if respWritten {
- return
- }
-
-
- if h.Parent.Option.RedirectRuleTable.IsRedirectable(r) {
- statusCode := h.Parent.Option.RedirectRuleTable.HandleRedirect(w, r)
- h.logRequest(r, statusCode != 500, statusCode, "redirect", "")
- return
- }
-
- domainOnly := r.Host
- if strings.Contains(r.Host, ":") {
- hostPath := strings.Split(r.Host, ":")
- domainOnly = hostPath[0]
- }
-
- if strings.Contains(r.Host, ".") {
-
- sep := h.Parent.getSubdomainProxyEndpointFromHostname(domainOnly)
- if sep != nil {
- if sep.RequireBasicAuth {
- err := h.handleBasicAuthRouting(w, r, sep)
- if err != nil {
- return
- }
- }
- h.subdomainRequest(w, r, sep)
- return
- }
- }
-
-
- proxyingPath := strings.TrimSpace(r.RequestURI)
- targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath)
- if targetProxyEndpoint != nil {
- if targetProxyEndpoint.RequireBasicAuth {
- err := h.handleBasicAuthRouting(w, r, targetProxyEndpoint)
- if err != nil {
- return
- }
- }
- h.proxyRequest(w, r, targetProxyEndpoint)
- } else if !strings.HasSuffix(proxyingPath, "/") {
- potentialProxtEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath + "/")
- if potentialProxtEndpoint != nil {
-
- http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
- } else {
-
- h.proxyRequest(w, r, h.Parent.Root)
- }
- } else {
-
- h.proxyRequest(w, r, h.Parent.Root)
- }
- }
- func (h *ProxyHandler) handleAccessRouting(w http.ResponseWriter, r *http.Request) bool {
-
- clientIpAddr := geodb.GetRequesterIP(r)
- if h.Parent.Option.GeodbStore.IsBlacklisted(clientIpAddr) {
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- w.WriteHeader(http.StatusForbidden)
- template, err := os.ReadFile("./web/forbidden.html")
- if err != nil {
- w.Write([]byte("403 - Forbidden"))
- } else {
- w.Write(template)
- }
- h.logRequest(r, false, 403, "blacklist", "")
- return true
- }
-
- if !h.Parent.Option.GeodbStore.IsWhitelisted(clientIpAddr) {
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- w.WriteHeader(http.StatusForbidden)
- template, err := os.ReadFile("./web/forbidden.html")
- if err != nil {
- w.Write([]byte("403 - Forbidden"))
- } else {
- w.Write(template)
- }
- h.logRequest(r, false, 403, "whitelist", "")
- return true
- }
- return false
- }
|