Browse Source

Fixed hopbyhop remover bug

Toby Chui 7 months ago
parent
commit
2254e5f722
3 changed files with 38 additions and 27 deletions
  1. 34 25
      mod/dynamicproxy/proxyRequestHandler.go
  2. 2 1
      mod/dynamicproxy/typedef.go
  3. 2 1
      reverseproxy.go

+ 34 - 25
mod/dynamicproxy/proxyRequestHandler.go

@@ -2,6 +2,7 @@ package dynamicproxy
 
 import (
 	"errors"
+	"fmt"
 	"log"
 	"net"
 	"net/http"
@@ -112,6 +113,8 @@ func (router *Router) rewriteURL(rooturl string, requestURL string) string {
 func (h *ProxyHandler) hostRequest(w http.ResponseWriter, r *http.Request, target *ProxyEndpoint) {
 	r.Header.Set("X-Forwarded-Host", r.Host)
 	r.Header.Set("X-Forwarded-Server", "zoraxy-"+h.Parent.Option.HostUUID)
+
+	/* Load balancing */
 	selectedUpstream, err := h.Parent.loadBalancer.GetRequestUpstreamTarget(w, r, target.ActiveOrigins, target.UseStickySession)
 	if err != nil {
 		http.ServeFile(w, r, "./web/rperror.html")
@@ -119,31 +122,37 @@ func (h *ProxyHandler) hostRequest(w http.ResponseWriter, r *http.Request, targe
 		h.Parent.logRequest(r, false, 521, "subdomain-http", r.URL.Hostname())
 		return
 	}
-	requestURL := r.URL.String()
-	if r.Header["Upgrade"] != nil && strings.ToLower(r.Header["Upgrade"][0]) == "websocket" {
-		//Handle WebSocket request. Forward the custom Upgrade header and rewrite origin
-		r.Header.Set("Zr-Origin-Upgrade", "websocket")
-		wsRedirectionEndpoint := selectedUpstream.OriginIpOrDomain
-		if wsRedirectionEndpoint[len(wsRedirectionEndpoint)-1:] != "/" {
-			//Append / to the end of the redirection endpoint if not exists
-			wsRedirectionEndpoint = wsRedirectionEndpoint + "/"
-		}
-		if len(requestURL) > 0 && requestURL[:1] == "/" {
-			//Remove starting / from request URL if exists
-			requestURL = requestURL[1:]
-		}
-		u, _ := url.Parse("ws://" + wsRedirectionEndpoint + requestURL)
-		if selectedUpstream.RequireTLS {
-			u, _ = url.Parse("wss://" + wsRedirectionEndpoint + requestURL)
-		}
-		h.Parent.logRequest(r, true, 101, "host-websocket", selectedUpstream.OriginIpOrDomain)
-		wspHandler := websocketproxy.NewProxy(u, websocketproxy.Options{
-			SkipTLSValidation: selectedUpstream.SkipCertValidations,
-			SkipOriginCheck:   selectedUpstream.SkipWebSocketOriginCheck,
-		})
-		wspHandler.ServeHTTP(w, r)
-		return
-	}
+
+	/* WebSocket automatic proxy */
+	fmt.Println("Disable Auto Websocket Proxy", target.DisableAutoWebSockeyProxy)
+	/*
+		if !target.DisableAutoWebSockeyProxy {
+			requestURL := r.URL.String()
+			if r.Header["Upgrade"] != nil && strings.ToLower(r.Header["Upgrade"][0]) == "websocket" {
+				//Handle WebSocket request. Forward the custom Upgrade header and rewrite origin
+				r.Header.Set("Zr-Origin-Upgrade", "websocket")
+				wsRedirectionEndpoint := selectedUpstream.OriginIpOrDomain
+				if wsRedirectionEndpoint[len(wsRedirectionEndpoint)-1:] != "/" {
+					//Append / to the end of the redirection endpoint if not exists
+					wsRedirectionEndpoint = wsRedirectionEndpoint + "/"
+				}
+				if len(requestURL) > 0 && requestURL[:1] == "/" {
+					//Remove starting / from request URL if exists
+					requestURL = requestURL[1:]
+				}
+				u, _ := url.Parse("ws://" + wsRedirectionEndpoint + requestURL)
+				if selectedUpstream.RequireTLS {
+					u, _ = url.Parse("wss://" + wsRedirectionEndpoint + requestURL)
+				}
+				h.Parent.logRequest(r, true, 101, "host-websocket", selectedUpstream.OriginIpOrDomain)
+				wspHandler := websocketproxy.NewProxy(u, websocketproxy.Options{
+					SkipTLSValidation: selectedUpstream.SkipCertValidations,
+					SkipOriginCheck:   selectedUpstream.SkipWebSocketOriginCheck,
+				})
+				wspHandler.ServeHTTP(w, r)
+				return
+			}
+		}*/
 
 	originalHostHeader := r.Host
 	if r.URL != nil {

+ 2 - 1
mod/dynamicproxy/typedef.go

@@ -136,7 +136,8 @@ type ProxyEndpoint struct {
 	HSTSMaxAge                   int64                               //HSTS max age, set to 0 for disable HSTS headers
 	EnablePermissionPolicyHeader bool                                //Enable injection of permission policy header
 	PermissionPolicy             *permissionpolicy.PermissionsPolicy //Permission policy header
-	DisableHopByHopHeaderRemoval bool                                //TODO: Do not remove hop-by-hop headers
+	DisableHopByHopHeaderRemoval bool                                //Do not remove hop-by-hop headers
+	DisableAutoWebSockeyProxy    bool                                //Disable auto sniffing logic for websocket upgrade
 
 	//Authentication
 	RequireBasicAuth        bool                      //Set to true to request basic auth before proxy

+ 2 - 1
reverseproxy.go

@@ -1349,7 +1349,8 @@ func HandleHopByHop(w http.ResponseWriter, r *http.Request) {
 		//we need to clone and respawn this proxy endpoint
 		newProxyEndpoint := targetProxyEndpoint.Clone()
 		//Storage file use false as default, so disable removal = not enable remover
-		targetProxyEndpoint.DisableHopByHopHeaderRemoval = !enableHopByHopRemover
+		newProxyEndpoint.DisableHopByHopHeaderRemoval = !enableHopByHopRemover
+
 		//Save proxy endpoint
 		err = SaveReverseProxyConfig(newProxyEndpoint)
 		if err != nil {