This commit is contained in:
WJQSERVER 2024-09-27 14:15:41 +08:00
parent 0899397d2e
commit 45246e124e

View file

@ -8,7 +8,6 @@ import (
"strconv"
"strings"
"ghproxy/auth"
"ghproxy/config"
"ghproxy/logger"
@ -47,7 +46,7 @@ func NoRouteHandler(config *config.Config) gin.HandlerFunc {
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
}
if !auth.AuthHandler(c) {
if !AuthHandler(c) {
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
logw("Unauthorized request: %s", rawPath)
return
@ -190,3 +189,23 @@ func checkURL(u string) []string {
logw("Invalid URL: %s", u)
return nil
}
func AuthHandler(c *gin.Context) bool {
// 如果身份验证未启用,直接返回 true
if !cfg.Auth {
logw("auth PASS")
return true
}
// 获取 auth_token 参数
authToken := c.Query("auth_token")
logw("auth_token: ", authToken)
// 验证 token
isValid := authToken == cfg.AuthToken
if !isValid {
logw("auth FAIL")
}
return isValid
}