|
@@ -2,7 +2,6 @@ package dynamicproxy
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
- "fmt"
|
|
|
"log"
|
|
|
"net"
|
|
|
"net/http"
|
|
@@ -124,35 +123,33 @@ func (h *ProxyHandler) hostRequest(w http.ResponseWriter, r *http.Request, targe
|
|
|
}
|
|
|
|
|
|
/* 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
|
|
|
+ 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 {
|
|
@@ -165,7 +162,7 @@ func (h *ProxyHandler) hostRequest(w http.ResponseWriter, r *http.Request, targe
|
|
|
//Build downstream and upstream header rules
|
|
|
upstreamHeaders, downstreamHeaders := target.SplitInboundOutboundHeaders()
|
|
|
|
|
|
- err = selectedUpstream.ServeHTTP(w, r, &dpcore.ResponseRewriteRuleSet{
|
|
|
+ statusCode, err := selectedUpstream.ServeHTTP(w, r, &dpcore.ResponseRewriteRuleSet{
|
|
|
ProxyDomain: selectedUpstream.OriginIpOrDomain,
|
|
|
OriginalHost: originalHostHeader,
|
|
|
UseTLS: selectedUpstream.RequireTLS,
|
|
@@ -191,7 +188,7 @@ func (h *ProxyHandler) hostRequest(w http.ResponseWriter, r *http.Request, targe
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- h.Parent.logRequest(r, true, 200, "host-http", r.URL.Hostname())
|
|
|
+ h.Parent.logRequest(r, true, statusCode, "host-http", r.URL.Hostname())
|
|
|
}
|
|
|
|
|
|
// Handle vdir type request
|
|
@@ -233,7 +230,7 @@ func (h *ProxyHandler) vdirRequest(w http.ResponseWriter, r *http.Request, targe
|
|
|
//Build downstream and upstream header rules
|
|
|
upstreamHeaders, downstreamHeaders := target.parent.SplitInboundOutboundHeaders()
|
|
|
|
|
|
- err := target.proxy.ServeHTTP(w, r, &dpcore.ResponseRewriteRuleSet{
|
|
|
+ statusCode, err := target.proxy.ServeHTTP(w, r, &dpcore.ResponseRewriteRuleSet{
|
|
|
ProxyDomain: target.Domain,
|
|
|
OriginalHost: originalHostHeader,
|
|
|
UseTLS: target.RequireTLS,
|
|
@@ -256,7 +253,7 @@ func (h *ProxyHandler) vdirRequest(w http.ResponseWriter, r *http.Request, targe
|
|
|
h.Parent.logRequest(r, false, 521, "vdir-http", target.Domain)
|
|
|
}
|
|
|
}
|
|
|
- h.Parent.logRequest(r, true, 200, "vdir-http", target.Domain)
|
|
|
+ h.Parent.logRequest(r, true, statusCode, "vdir-http", target.Domain)
|
|
|
|
|
|
}
|
|
|
|