feat: 代理认证中间件

This commit is contained in:
tbphp
2025-07-24 17:17:18 +08:00
parent 746c9f3108
commit abb8fa1d19
3 changed files with 66 additions and 7 deletions

View File

@@ -3,12 +3,15 @@ package middleware
import (
"fmt"
"slices"
"strings"
"time"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/response"
"gpt-load/internal/services"
"gpt-load/internal/types"
"gpt-load/internal/utils"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
@@ -135,6 +138,43 @@ func Auth(authConfig types.AuthConfig) gin.HandlerFunc {
}
}
// ProxyAuth
func ProxyAuth(gm *services.GroupManager) gin.HandlerFunc {
return func(c *gin.Context) {
// Check key
key := extractAuthKey(c)
if key == "" {
response.Error(c, app_errors.ErrUnauthorized)
c.Abort()
return
}
group, err := gm.GetGroupByName(c.Param("group_name"))
if err != nil {
response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, "Failed to retrieve proxy group"))
c.Abort()
return
}
// Check Group keys first
groupKeys := utils.SplitAndTrim(group.ProxyKeys, ",")
if slices.Contains(groupKeys, key) {
c.Next()
return
}
// Then check System-wide keys
systemKeys := utils.SplitAndTrim(group.EffectiveConfig.ProxyKeys, ",")
if slices.Contains(systemKeys, key) {
c.Next()
return
}
response.Error(c, app_errors.ErrUnauthorized)
c.Abort()
}
}
// Recovery creates a recovery middleware with custom error handling
func Recovery() gin.HandlerFunc {
return gin.CustomRecovery(func(c *gin.Context, recovered any) {