From 6bff86a5bceac8ae5b41e5dfeba1cfa115aa3418 Mon Sep 17 00:00:00 2001 From: kingecg Date: Tue, 24 Jun 2025 00:26:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(handler):=20=E6=B7=BB=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E5=AE=9A=E5=90=91=E6=8C=87=E4=BB=A4=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=8C=87=E4=BB=A4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 server/directive.go 中添加了新的 Redirect 指令,用于实现请求重定向 - 在 handler/proxy.go 中增加了对非 Proxy_ 指令的过滤,提高了代理处理的灵活性和安全性 --- handler/proxy.go | 3 +++ server/directive.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/handler/proxy.go b/handler/proxy.go index 4a1d94a..dec0650 100644 --- a/handler/proxy.go +++ b/handler/proxy.go @@ -62,6 +62,9 @@ func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.Rever directiveHandlers := []func(r *http.Request){} if len(path.Directives) > 0 { for _, directive := range path.Directives { + if !strings.HasPrefix(directive, "Proxy_") { + continue + } ndirective := strings.TrimPrefix(directive, "Proxy_") d := strings.Replace(string(ndirective), "$target", upstream, 1) dh := GetUpdaterFn(d) diff --git a/server/directive.go b/server/directive.go index 0e6a54a..2a74723 100644 --- a/server/directive.go +++ b/server/directive.go @@ -67,6 +67,19 @@ var BasicAuthDirective Directive = func(args ...string) Middleware { return BasicAuth } +var RedirectDirective Directive = func(args ...string) Middleware { + return func(w http.ResponseWriter, r *http.Request, next http.Handler) { + l := gologger.GetLogger("Directive") + l.Debug("Redirect") + if len(args) < 1 { + http.Error(w, "Redirect directive requires a URL", http.StatusBadRequest) + return + } + targetURL := args[0] + r.URL.Path + http.Redirect(w, r, targetURL, http.StatusFound) + } +} + // 在DirectiveMap中注册新指令 var DirectiveMap = map[string]Directive{ "Set-Header": Set_Header, @@ -75,4 +88,5 @@ var DirectiveMap = map[string]Directive{ "Record-Access": DRecordAccess, "Jwt-Auth": JWTDirective, "Basic-Auth": BasicAuthDirective, + "Redirect": RedirectDirective, }