Skip to content

Commit 34a15bc

Browse files
committed
feat(middleware): 为IP封禁中间件添加管理后台路径豁免功能
添加isAdminPath函数判断请求路径是否为管理后台路径,如果是则跳过IP封禁检查
1 parent 15197ee commit 34a15bc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

internal/middleware/security.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"proxy-go/internal/security"
7+
"strings"
78
"time"
89

910
"github.com/woodchen-ink/go-web-utils/iputil"
@@ -21,11 +22,35 @@ func NewSecurityMiddleware(banManager *security.IPBanManager) *SecurityMiddlewar
2122
}
2223
}
2324

25+
// isAdminPath 判断是否是管理后台路径
26+
func isAdminPath(path string) bool {
27+
// 管理后台路径前缀
28+
adminPrefixes := []string{
29+
"/admin/",
30+
"/admin",
31+
}
32+
33+
for _, prefix := range adminPrefixes {
34+
if strings.HasPrefix(path, prefix) {
35+
return true
36+
}
37+
}
38+
39+
return false
40+
}
41+
2442
// IPBanMiddleware IP封禁中间件
2543
func (sm *SecurityMiddleware) IPBanMiddleware(next http.Handler) http.Handler {
2644
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2745
clientIP := iputil.GetClientIP(r)
2846

47+
// 管理后台路径不受IP封禁限制
48+
if isAdminPath(r.URL.Path) {
49+
// 直接放行管理后台请求
50+
next.ServeHTTP(w, r)
51+
return
52+
}
53+
2954
// 检查IP是否被封禁
3055
if sm.banManager.IsIPBanned(clientIP) {
3156
banned, banEndTime := sm.banManager.GetBanInfo(clientIP)

0 commit comments

Comments
 (0)