mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
24w06b
This commit is contained in:
parent
bb4e2d8ae7
commit
178d5f14c2
3 changed files with 27 additions and 13 deletions
18
auth/auth.go
18
auth/auth.go
|
|
@ -7,26 +7,28 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var logw = logger.Logw
|
||||||
cfg *config.Config
|
|
||||||
log = logger.Logw
|
|
||||||
)
|
|
||||||
|
|
||||||
func AuthHandler(c *gin.Context) bool {
|
func AuthHandler(c *gin.Context, cfg *config.Config) bool {
|
||||||
// 如果身份验证未启用,直接返回 true
|
// 如果身份验证未启用,直接返回 true
|
||||||
if !cfg.Auth {
|
if !cfg.Auth {
|
||||||
log("auth PASS")
|
logw("auth PASS")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取 auth_token 参数
|
// 获取 auth_token 参数
|
||||||
authToken := c.Query("auth_token")
|
authToken := c.Query("auth_token")
|
||||||
log("auth_token: ", authToken)
|
log("auth_token received: %s", authToken)
|
||||||
|
|
||||||
// 验证 token
|
// 验证 token
|
||||||
|
if authToken == "" {
|
||||||
|
logw("auth FAIL: no auth_token provided")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
isValid := authToken == cfg.AuthToken
|
isValid := authToken == cfg.AuthToken
|
||||||
if !isValid {
|
if !isValid {
|
||||||
log("auth FAIL")
|
logw("auth FAIL: invalid auth_token")
|
||||||
}
|
}
|
||||||
|
|
||||||
return isValid
|
return isValid
|
||||||
|
|
|
||||||
20
main.go
20
main.go
|
|
@ -97,11 +97,23 @@ func api(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthHandler(c *gin.Context) bool {
|
func AuthHandler(c *gin.Context) bool {
|
||||||
if cfg.Auth {
|
// 如果身份验证未启用,直接返回 true
|
||||||
authToken := c.Query("auth_token")
|
if !cfg.Auth {
|
||||||
return authToken == cfg.AuthToken
|
logw("auth PASS")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func noRouteHandler(config *config.Config) gin.HandlerFunc {
|
func noRouteHandler(config *config.Config) gin.HandlerFunc {
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||||
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !auth.AuthHandler(c) {
|
if !auth.AuthHandler(c, cfg) {
|
||||||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||||
logw("Unauthorized request: %s", rawPath)
|
logw("Unauthorized request: %s", rawPath)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue