@@ -29,8 +29,6 @@ func Entrance(t *gotext.Locale, conf *koanf.Koanf, session *sessions.Manager) fu
2929 entrance = "/" + entrance
3030 }
3131
32- routePath := chi .RouteContext (r .Context ()).RoutePath
33-
3432 // 情况一:设置了绑定域名、IP、UA,且请求不符合要求,返回错误
3533 host , _ , err := net .SplitHostPort (r .Host )
3634 if err != nil {
@@ -80,7 +78,7 @@ func Entrance(t *gotext.Locale, conf *koanf.Koanf, session *sessions.Manager) fu
8078 }
8179
8280 // 情况二:请求路径与入口路径相同或者未设置访问入口,标记通过验证并重定向到登录页面
83- if (strings .TrimSuffix (routePath , "/" ) == entrance || entrance == "/" ) &&
81+ if (strings .TrimSuffix (r . URL . Path , "/" ) == entrance || entrance == "/" ) &&
8482 r .Header .Get ("Authorization" ) == "" {
8583 sess .Put ("verify_entrance" , true )
8684 render := chix .NewRender (w , r )
@@ -90,12 +88,12 @@ func Entrance(t *gotext.Locale, conf *koanf.Koanf, session *sessions.Manager) fu
9088 }
9189
9290 // 情况三:通过APIKey+入口路径访问,重写请求路径并跳过验证
93- if strings .HasPrefix (routePath , entrance ) && r .Header .Get ("Authorization" ) != "" {
91+ if strings .HasPrefix (r . URL . Path , entrance ) && r .Header .Get ("Authorization" ) != "" {
9492 // 只在设置了入口路径的情况下,才进行重写
9593 if entrance != "/" {
9694 if rctx := chi .RouteContext (r .Context ()); rctx != nil {
97- rctx .RoutePath = strings .TrimPrefix (routePath , entrance )
98- r .URL .Path = strings .TrimPrefix (routePath , entrance )
95+ rctx .RoutePath = strings .TrimPrefix (rctx . RoutePath , entrance )
96+ r .URL .Path = strings .TrimPrefix (r . URL . Path , entrance )
9997 }
10098 }
10199 next .ServeHTTP (w , r )
@@ -105,7 +103,7 @@ func Entrance(t *gotext.Locale, conf *koanf.Koanf, session *sessions.Manager) fu
105103 // 情况四:非调试模式且未通过验证的请求,返回错误
106104 if ! conf .Bool ("app.debug" ) &&
107105 sess .Missing ("verify_entrance" ) &&
108- routePath != "/robots.txt" {
106+ r . URL . Path != "/robots.txt" {
109107 Abort (w , http .StatusTeapot , t .Get ("invalid access entrance" ))
110108 return
111109 }
0 commit comments