feat(handler): 添加重定向指令并优化代理指令处理

- 在 server/directive.go 中添加了新的 Redirect 指令,用于实现请求重定向
- 在 handler/proxy.go 中增加了对非 Proxy_ 指令的过滤,提高了代理处理的灵活性和安全性
This commit is contained in:
kingecg 2025-06-24 00:26:59 +08:00
parent 65d9037788
commit 6bff86a5bc
2 changed files with 17 additions and 0 deletions

View File

@ -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)

View File

@ -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,
}