|
@@ -3,6 +3,7 @@ package dynamicproxy
|
|
import (
|
|
import (
|
|
_ "embed"
|
|
_ "embed"
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
"os"
|
|
"os"
|
|
@@ -116,37 +117,55 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
|
|
http.Redirect(w, r, r.RequestURI+"/", http.StatusTemporaryRedirect)
|
|
} else {
|
|
} else {
|
|
//Passthrough the request to root
|
|
//Passthrough the request to root
|
|
-
|
|
|
|
- h.proxyRequest(w, r, h.Parent.Root)
|
|
|
|
|
|
+ h.handleRootRouting(w, r)
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
//No routing rules found.
|
|
//No routing rules found.
|
|
- if h.Parent.RootRoutingOptions.EnableRedirectForUnsetRules {
|
|
|
|
- //Route to custom domain
|
|
|
|
- if h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget == "" {
|
|
|
|
- //Not set. Redirect to first level of domain redirectable
|
|
|
|
- fld, err := h.getTopLevelRedirectableDomain(domainOnly)
|
|
|
|
- if err != nil {
|
|
|
|
- //Redirect to proxy root
|
|
|
|
- log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
|
|
|
|
- h.proxyRequest(w, r, h.Parent.Root)
|
|
|
|
- } else {
|
|
|
|
- log.Println("[Router] Redirecting request from " + domainOnly + " to " + fld)
|
|
|
|
- h.logRequest(r, false, 307, "root-redirect", domainOnly)
|
|
|
|
- http.Redirect(w, r, fld, http.StatusTemporaryRedirect)
|
|
|
|
- }
|
|
|
|
- return
|
|
|
|
|
|
+ h.handleRootRouting(w, r)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+handleRootRouting
|
|
|
|
+
|
|
|
|
+This function handle root routing situations where there are no subdomain
|
|
|
|
+, vdir or special routing rule matches the requested URI.
|
|
|
|
+
|
|
|
|
+Once entered this routing segment, the root routing options will take over
|
|
|
|
+for the routing logic.
|
|
|
|
+*/
|
|
|
|
+func (h *ProxyHandler) handleRootRouting(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ domainOnly := r.Host
|
|
|
|
+ if strings.Contains(r.Host, ":") {
|
|
|
|
+ hostPath := strings.Split(r.Host, ":")
|
|
|
|
+ domainOnly = hostPath[0]
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if h.Parent.RootRoutingOptions.EnableRedirectForUnsetRules {
|
|
|
|
+ //Route to custom domain
|
|
|
|
+ fmt.Println("test1", h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget)
|
|
|
|
+ if h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget == "" {
|
|
|
|
+ //Not set. Redirect to first level of domain redirectable
|
|
|
|
+ fld, err := h.getTopLevelRedirectableDomain(domainOnly)
|
|
|
|
+ if err != nil {
|
|
|
|
+ //Redirect to proxy root
|
|
|
|
+ log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
|
|
|
|
+ h.proxyRequest(w, r, h.Parent.Root)
|
|
} else {
|
|
} else {
|
|
- //Redirect to target
|
|
|
|
|
|
+ log.Println("[Router] Redirecting request from " + domainOnly + " to " + fld)
|
|
h.logRequest(r, false, 307, "root-redirect", domainOnly)
|
|
h.logRequest(r, false, 307, "root-redirect", domainOnly)
|
|
- http.Redirect(w, r, h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
|
|
|
|
- return
|
|
|
|
|
|
+ http.Redirect(w, r, fld, http.StatusTemporaryRedirect)
|
|
}
|
|
}
|
|
|
|
+ return
|
|
} else {
|
|
} else {
|
|
- //Route to root
|
|
|
|
- h.proxyRequest(w, r, h.Parent.Root)
|
|
|
|
|
|
+ //Redirect to target
|
|
|
|
+ h.logRequest(r, false, 307, "root-redirect", domainOnly)
|
|
|
|
+ http.Redirect(w, r, h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ } else {
|
|
|
|
+ //Route to root
|
|
|
|
+ h.proxyRequest(w, r, h.Parent.Root)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|