优化客服云权限安全与访客IP解析

This commit is contained in:
yml2213
2026-08-31 13:43:45 +08:00
parent 523a306d5e
commit bdf67395ab
24 changed files with 365 additions and 48 deletions
+2 -2
View File
@@ -197,7 +197,7 @@ func legacyRolePermissions(role string) map[string]bool {
"session.view": true, "session.reply": true, "session.transfer": true,
"session.end": true, "session.note": true, "session.priority": true,
"customer.view": true, "customer.create": true, "customer.edit": true,
"customer.export": true, "customer.tag": true,
"customer.export": true, "customer.tag": true, "customer.delete": true,
"chat_history.view": true, "chat_history.detail": true,
"chat_history.export": true, "chat_history.batch_archive": true,
"knowledge.view": true, "knowledge.create": true, "knowledge.edit": true,
@@ -216,7 +216,7 @@ func legacyRolePermissions(role string) map[string]bool {
"session.view": true, "session.reply": true, "session.transfer": true,
"session.end": true, "session.note": true, "session.priority": true,
"customer.view": true, "customer.create": true, "customer.edit": true,
"customer.export": true, "customer.tag": true,
"customer.export": true, "customer.tag": true, "customer.delete": true,
"chat_history.view": true, "chat_history.detail": true,
"chat_history.export": true, "chat_history.batch_archive": true,
"knowledge.view": true, "knowledge.create": true, "knowledge.edit": true,
+53
View File
@@ -0,0 +1,53 @@
package middleware
import (
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
)
type rateWindow struct {
start time.Time
count int
}
var rateWindows sync.Map
var rateMu sync.Mutex
// RateLimitByIP applies a fixed-window limit to public endpoints.
// A shared gateway limiter is still needed when running multiple replicas.
func RateLimitByIP(max int, window time.Duration) gin.HandlerFunc {
if max < 1 {
max = 1
}
if window <= 0 {
window = time.Minute
}
return func(c *gin.Context) {
key := c.Request.Method + ":" + c.FullPath() + ":" + c.ClientIP()
now := time.Now()
rateMu.Lock()
allowed := false
var current rateWindow
if raw, ok := rateWindows.Load(key); ok {
current, _ = raw.(rateWindow)
}
if current.start.IsZero() || now.Sub(current.start) >= window {
current = rateWindow{start: now, count: 1}
allowed = true
} else if current.count < max {
current.count++
allowed = true
}
rateWindows.Store(key, current)
rateMu.Unlock()
if !allowed {
c.Header("Retry-After", "60")
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{"code": 429, "message": "请求过于频繁,请稍后重试"})
return
}
c.Next()
}
}
@@ -0,0 +1,28 @@
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func TestRateLimitByIPReturns429AfterWindowIsConsumed(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.GET("/rate-test", RateLimitByIP(1, time.Minute), func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
first := httptest.NewRecorder()
r.ServeHTTP(first, httptest.NewRequest(http.MethodGet, "/rate-test", nil))
if first.Code != http.StatusNoContent {
t.Fatalf("首个请求状态码 = %d", first.Code)
}
second := httptest.NewRecorder()
r.ServeHTTP(second, httptest.NewRequest(http.MethodGet, "/rate-test", nil))
if second.Code != http.StatusTooManyRequests {
t.Fatalf("超限请求状态码 = %d,期望 429", second.Code)
}
}