Skip to content

Commit 4985eb2

Browse files
committed
feat: remove CleanPath middleware
1 parent 2181653 commit 4985eb2

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

internal/http/middleware/entrance.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

internal/http/middleware/must_install.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/http"
55
"strings"
66

7-
"github.com/go-chi/chi/v5"
87
"github.com/leonelquinteros/gotext"
98

109
"github.com/tnb-labs/panel/internal/biz"
@@ -14,15 +13,13 @@ import (
1413
func MustInstall(t *gotext.Locale, app biz.AppRepo) func(next http.Handler) http.Handler {
1514
return func(next http.Handler) http.Handler {
1615
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17-
routePath := chi.RouteContext(r.Context()).RoutePath
18-
1916
var slugs []string
20-
if strings.HasPrefix(routePath, "/api/website") {
17+
if strings.HasPrefix(r.URL.Path, "/api/website") {
2118
slugs = append(slugs, "nginx")
22-
} else if strings.HasPrefix(routePath, "/api/container") {
19+
} else if strings.HasPrefix(r.URL.Path, "/api/container") {
2320
slugs = append(slugs, "podman", "docker")
24-
} else if strings.HasPrefix(routePath, "/api/apps/") {
25-
pathArr := strings.Split(routePath, "/")
21+
} else if strings.HasPrefix(r.URL.Path, "/api/apps/") {
22+
pathArr := strings.Split(r.URL.Path, "/")
2623
if len(pathArr) < 4 {
2724
Abort(w, http.StatusForbidden, t.Get("app not found"))
2825
return

internal/http/middleware/must_login.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"slices"
1010
"strings"
1111

12-
"github.com/go-chi/chi/v5"
1312
"github.com/go-rat/sessions"
1413
"github.com/leonelquinteros/gotext"
1514
"github.com/spf13/cast"
@@ -36,18 +35,16 @@ func MustLogin(t *gotext.Locale, session *sessions.Manager, userToken biz.UserTo
3635
return
3736
}
3837

39-
routePath := chi.RouteContext(r.Context()).RoutePath
40-
4138
// 对白名单和非 API 请求放行
42-
if slices.Contains(whiteList, routePath) || !strings.HasPrefix(routePath, "/api") {
39+
if slices.Contains(whiteList, r.URL.Path) || !strings.HasPrefix(r.URL.Path, "/api") {
4340
next.ServeHTTP(w, r)
4441
return
4542
}
4643

4744
userID := uint(0)
4845
if r.Header.Get("Authorization") != "" {
4946
// 禁止访问 ws 相关的接口
50-
if strings.HasPrefix(routePath, "/api/ws") {
47+
if strings.HasPrefix(r.URL.Path, "/api/ws") {
5148
Abort(w, http.StatusForbidden, t.Get("ws not allowed"))
5249
return
5350
}

0 commit comments

Comments
 (0)