feat(handler): 添加重定向指令并优化代理指令处理
- 在 server/directive.go 中添加了新的 Redirect 指令,用于实现请求重定向 - 在 handler/proxy.go 中增加了对非 Proxy_ 指令的过滤,提高了代理处理的灵活性和安全性
This commit is contained in:
parent
65d9037788
commit
6bff86a5bc
|
@ -62,6 +62,9 @@ func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.Rever
|
||||||
directiveHandlers := []func(r *http.Request){}
|
directiveHandlers := []func(r *http.Request){}
|
||||||
if len(path.Directives) > 0 {
|
if len(path.Directives) > 0 {
|
||||||
for _, directive := range path.Directives {
|
for _, directive := range path.Directives {
|
||||||
|
if !strings.HasPrefix(directive, "Proxy_") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
ndirective := strings.TrimPrefix(directive, "Proxy_")
|
ndirective := strings.TrimPrefix(directive, "Proxy_")
|
||||||
d := strings.Replace(string(ndirective), "$target", upstream, 1)
|
d := strings.Replace(string(ndirective), "$target", upstream, 1)
|
||||||
dh := GetUpdaterFn(d)
|
dh := GetUpdaterFn(d)
|
||||||
|
|
|
@ -67,6 +67,19 @@ var BasicAuthDirective Directive = func(args ...string) Middleware {
|
||||||
return BasicAuth
|
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中注册新指令
|
// 在DirectiveMap中注册新指令
|
||||||
var DirectiveMap = map[string]Directive{
|
var DirectiveMap = map[string]Directive{
|
||||||
"Set-Header": Set_Header,
|
"Set-Header": Set_Header,
|
||||||
|
@ -75,4 +88,5 @@ var DirectiveMap = map[string]Directive{
|
||||||
"Record-Access": DRecordAccess,
|
"Record-Access": DRecordAccess,
|
||||||
"Jwt-Auth": JWTDirective,
|
"Jwt-Auth": JWTDirective,
|
||||||
"Basic-Auth": BasicAuthDirective,
|
"Basic-Auth": BasicAuthDirective,
|
||||||
|
"Redirect": RedirectDirective,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue