123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package dynamicproxy
- import (
- _ "embed"
- "errors"
- "fmt"
- "log"
- "net/http"
- "net/url"
- "os"
- "strings"
- "imuslab.com/zoraxy/mod/geodb"
- )
- var (
-
- rawTldMap []byte
- )
- 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 {
-
- if h.Parent.RootOptions.EnableRedirectForUnsetRules {
-
- if h.Parent.RootOptions.UnsetRuleRedirectTarget == "" {
-
- fld, err := h.getTopLevelRedirectableDomain(r.RequestURI)
- if err != nil {
-
- log.Println("[Router] Unable to resolve top level redirectable domain: " + err.Error())
- h.proxyRequest(w, r, h.Parent.Root)
- } else {
- http.Redirect(w, r, fld, http.StatusTemporaryRedirect)
- }
- return
- } else {
-
- http.Redirect(w, r, h.Parent.RootOptions.UnsetRuleRedirectTarget, http.StatusTemporaryRedirect)
- return
- }
- } 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
- }
- func (h *ProxyHandler) getTopLevelRedirectableDomain(unsetSubdomainUri string) (string, error) {
-
- parsedURL, err := url.Parse(unsetSubdomainUri)
- if err != nil {
- return "", err
- }
- hostname := parsedURL.Hostname()
-
-
- chunks := strings.Split(hostname, ".")
- for i := len(chunks); i > 0; i-- {
- thisChunk := chunks[i]
- fmt.Println(thisChunk)
- }
- return "", errors.New("wip")
- }
|