fix: middleware bypass

This commit is contained in:
yusing
2025-05-11 06:33:22 +08:00
parent f1eefde964
commit 71ca8c738e
7 changed files with 294 additions and 273 deletions

View File

@@ -7,7 +7,7 @@ import (
"strconv"
"strings"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/route/routes"
)
type (
@@ -91,12 +91,12 @@ var staticReqVarSubsMap = map[string]reqVarGetter{
return ""
},
VarRemoteAddr: func(req *http.Request) string { return req.RemoteAddr },
VarUpstreamName: func(req *http.Request) string { return reverseproxy.TryGetUpstreamName(req) },
VarUpstreamScheme: func(req *http.Request) string { return reverseproxy.TryGetUpstreamScheme(req) },
VarUpstreamHost: func(req *http.Request) string { return reverseproxy.TryGetUpstreamHost(req) },
VarUpstreamPort: func(req *http.Request) string { return reverseproxy.TryGetUpstreamPort(req) },
VarUpstreamAddr: func(req *http.Request) string { return reverseproxy.TryGetUpstreamAddr(req) },
VarUpstreamURL: func(req *http.Request) string { return reverseproxy.TryGetUpstreamURL(req) },
VarUpstreamName: routes.TryGetUpstreamName,
VarUpstreamScheme: routes.TryGetUpstreamScheme,
VarUpstreamHost: routes.TryGetUpstreamHost,
VarUpstreamPort: routes.TryGetUpstreamPort,
VarUpstreamAddr: routes.TryGetUpstreamAddr,
VarUpstreamURL: routes.TryGetUpstreamURL,
}
var staticRespVarSubsMap = map[string]respVarGetter{

View File

@@ -1,61 +0,0 @@
package reverseproxy
import (
"context"
"net/http"
)
var reverseProxyContextKey = struct{}{}
func (rp *ReverseProxy) WithContextValue(r *http.Request) *http.Request {
return r.WithContext(context.WithValue(r.Context(), reverseProxyContextKey, rp))
}
func TryGetReverseProxy(r *http.Request) *ReverseProxy {
if rp, ok := r.Context().Value(reverseProxyContextKey).(*ReverseProxy); ok {
return rp
}
return nil
}
func TryGetUpstreamName(r *http.Request) string {
if rp := TryGetReverseProxy(r); rp != nil {
return rp.TargetName
}
return ""
}
func TryGetUpstreamScheme(r *http.Request) string {
if rp := TryGetReverseProxy(r); rp != nil {
return rp.TargetURL.Scheme
}
return ""
}
func TryGetUpstreamHost(r *http.Request) string {
if rp := TryGetReverseProxy(r); rp != nil {
return rp.TargetURL.Hostname()
}
return ""
}
func TryGetUpstreamPort(r *http.Request) string {
if rp := TryGetReverseProxy(r); rp != nil {
return rp.TargetURL.Port()
}
return ""
}
func TryGetUpstreamAddr(r *http.Request) string {
if rp := TryGetReverseProxy(r); rp != nil {
return rp.TargetURL.Host
}
return ""
}
func TryGetUpstreamURL(r *http.Request) string {
if rp := TryGetReverseProxy(r); rp != nil {
return rp.TargetURL.String()
}
return ""
}