|
@@ -3,10 +3,8 @@ package dynamicproxy
|
|
import (
|
|
import (
|
|
_ "embed"
|
|
_ "embed"
|
|
"errors"
|
|
"errors"
|
|
- "fmt"
|
|
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
- "net/url"
|
|
|
|
"os"
|
|
"os"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
@@ -123,22 +121,25 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
//No routing rules found.
|
|
//No routing rules found.
|
|
- if h.Parent.RootOptions.EnableRedirectForUnsetRules {
|
|
|
|
|
|
+ if h.Parent.RootRoutingOptions.EnableRedirectForUnsetRules {
|
|
//Route to custom domain
|
|
//Route to custom domain
|
|
- if h.Parent.RootOptions.UnsetRuleRedirectTarget == "" {
|
|
|
|
|
|
+ if h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget == "" {
|
|
//Not set. Redirect to first level of domain redirectable
|
|
//Not set. Redirect to first level of domain redirectable
|
|
- fld, err := h.getTopLevelRedirectableDomain(r.RequestURI)
|
|
|
|
|
|
+ fld, err := h.getTopLevelRedirectableDomain(domainOnly)
|
|
if err != nil {
|
|
if err != nil {
|
|
//Redirect to proxy root
|
|
//Redirect to proxy root
|
|
log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
|
|
log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
|
|
h.proxyRequest(w, r, h.Parent.Root)
|
|
h.proxyRequest(w, r, h.Parent.Root)
|
|
} else {
|
|
} 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)
|
|
http.Redirect(w, r, fld, http.StatusTemporaryRedirect)
|
|
}
|
|
}
|
|
return
|
|
return
|
|
} else {
|
|
} else {
|
|
//Redirect to target
|
|
//Redirect to target
|
|
- http.Redirect(w, r, h.Parent.RootOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
|
|
|
|
|
|
+ h.logRequest(r, false, 307, "root-redirect", domainOnly)
|
|
|
|
+ http.Redirect(w, r, h.Parent.RootRoutingOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -186,21 +187,12 @@ func (h *ProxyHandler) handleAccessRouting(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
|
// GetTopLevelRedirectableDomain returns the toppest level of domain
|
|
// GetTopLevelRedirectableDomain returns the toppest level of domain
|
|
// that is redirectable. E.g. a.b.c.example.co.uk will return example.co.uk
|
|
// that is redirectable. E.g. a.b.c.example.co.uk will return example.co.uk
|
|
-func (h *ProxyHandler) getTopLevelRedirectableDomain(unsetSubdomainUri string) (string, error) {
|
|
|
|
- //Extract hostname from URI
|
|
|
|
- parsedURL, err := url.Parse(unsetSubdomainUri)
|
|
|
|
- if err != nil {
|
|
|
|
- return "", err
|
|
|
|
- }
|
|
|
|
- hostname := parsedURL.Hostname()
|
|
|
|
-
|
|
|
|
- //Generate all levels of domains
|
|
|
|
- //leveledDomains := []string{}
|
|
|
|
- chunks := strings.Split(hostname, ".")
|
|
|
|
- for i := len(chunks); i > 0; i-- {
|
|
|
|
- thisChunk := chunks[i]
|
|
|
|
- fmt.Println(thisChunk)
|
|
|
|
|
|
+func (h *ProxyHandler) getTopLevelRedirectableDomain(unsetSubdomainHost string) (string, error) {
|
|
|
|
+ parts := strings.Split(unsetSubdomainHost, ".")
|
|
|
|
+ if len(parts) > 1 {
|
|
|
|
+ newDomain := strings.Join(parts[1:], ".")
|
|
|
|
+ return "//" + newDomain, nil
|
|
}
|
|
}
|
|
|
|
|
|
- return "", errors.New("wip")
|
|
|
|
|
|
+ return "", errors.New("unsupported top level domain given")
|
|
}
|
|
}
|