Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
d482524912
2 changed files with 35 additions and 2 deletions
  1. 8 2
      mod/dynamicproxy/dpcore/dpcore.go
  2. 27 0
      mod/dynamicproxy/dpcore/utils.go

+ 8 - 2
mod/dynamicproxy/dpcore/dpcore.go

@@ -353,8 +353,14 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request, rrr
 			//Full path
 			//Replace the forwarded target with expected Host
 			fmt.Println(rrr.ProxyDomain, domainWithoutPort, rrr.OriginalHost)
-			locationRewrite = strings.ReplaceAll(locationRewrite, rrr.ProxyDomain, rrr.OriginalHost)
-			locationRewrite = strings.ReplaceAll(locationRewrite, domainWithoutPort, rrr.OriginalHost)
+			lr, err := replaceLocationHost(locationRewrite, rrr.OriginalHost)
+			if err == nil {
+				locationRewrite = lr
+			}
+			//locationRewrite = strings.ReplaceAll(locationRewrite, rrr.ProxyDomain, rrr.OriginalHost)
+			//locationRewrite = strings.ReplaceAll(locationRewrite, domainWithoutPort, rrr.OriginalHost)
+		} else {
+			//Relative path. Do not modifiy location header
 		}
 
 		//Custom redirection to this rproxy relative path

+ 27 - 0
mod/dynamicproxy/dpcore/utils.go

@@ -0,0 +1,27 @@
+package dpcore
+
+import (
+	"net/url"
+	"strings"
+)
+
+func replaceLocationHost(urlString, newHost string) (string, error) {
+	parsedURL, err := url.Parse(urlString)
+	if err != nil {
+		return "", err
+	}
+
+	// Extract the port number from the host string (if it exists)
+	hostParts := strings.Split(parsedURL.Host, ":")
+	var newHostString string
+	if len(hostParts) > 1 {
+		newHostString = newHost + ":" + hostParts[1]
+	} else {
+		newHostString = newHost
+	}
+
+	// Set the new host string
+	parsedURL.Host = newHostString
+
+	return parsedURL.String(), nil
+}