[break] change auth config & add auth key

This commit is contained in:
wjqserver 2025-04-01 18:32:45 +08:00
parent 978ece6fa0
commit 395f641468
8 changed files with 50 additions and 27 deletions

View file

@ -3,6 +3,7 @@ package api
import (
"context"
"ghproxy/config"
"ghproxy/middleware/nocache"
"github.com/WJQSERVER-STUDIO/go-utils/logger"
"github.com/cloudwego/hertz/pkg/app"
@ -18,18 +19,8 @@ var (
logError = logger.LogError
)
func NoCacheMiddleware() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
// 设置禁止缓存的响应头
c.Response.Header.Set("Cache-Control", "no-store, no-cache, must-revalidate")
c.Response.Header.Set("Pragma", "no-cache")
c.Response.Header.Set("Expires", "0")
c.Next(ctx) // 继续处理请求
}
}
func InitHandleRouter(cfg *config.Config, r *server.Hertz, version string) {
apiRouter := r.Group("/api", NoCacheMiddleware())
apiRouter := r.Group("/api", nocache.NoCacheMiddleware())
{
apiRouter.GET("/size_limit", func(ctx context.Context, c *app.RequestContext) {
SizeLimitHandler(cfg, c, ctx)

View file

@ -12,13 +12,19 @@ func AuthHeaderHandler(c *app.RequestContext, cfg *config.Config) (isValid bool,
return true, nil
}
// 获取"GH-Auth"的值
authToken := string(c.GetHeader("GH-Auth"))
var authToken string
if cfg.Auth.Key != "" {
authToken = string(c.GetHeader(cfg.Auth.Key))
} else {
authToken = string(c.GetHeader("GH-Auth"))
}
logDebug("%s %s %s %s %s AUTH_TOKEN: %s", c.Method(), string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), authToken)
if authToken == "" {
return false, fmt.Errorf("Auth token not found")
}
isValid = authToken == cfg.Auth.AuthToken
isValid = authToken == cfg.Auth.Token
if !isValid {
return false, fmt.Errorf("Auth token incorrect")
}

View file

@ -12,14 +12,20 @@ func AuthParametersHandler(c *app.RequestContext, cfg *config.Config) (isValid b
return true, nil
}
authToken := c.Query("auth_token")
var authToken string
if cfg.Auth.Key != "" {
authToken = c.Query(cfg.Auth.Key)
} else {
authToken = c.Query("auth_token")
}
logDebug("%s %s %s %s %s AUTH_TOKEN: %s", c.ClientIP(), c.Method(), string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), authToken)
if authToken == "" {
return false, fmt.Errorf("Auth token not found")
}
isValid = authToken == cfg.Auth.AuthToken
isValid = authToken == cfg.Auth.Token
if !isValid {
return false, fmt.Errorf("Auth token invalid")
}

View file

@ -37,17 +37,17 @@ func Init(cfg *config.Config) {
}
func AuthHandler(ctx context.Context, c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
if cfg.Auth.AuthMethod == "parameters" {
if cfg.Auth.Method == "parameters" {
isValid, err = AuthParametersHandler(c, cfg)
return isValid, err
} else if cfg.Auth.AuthMethod == "header" {
} else if cfg.Auth.Method == "header" {
isValid, err = AuthHeaderHandler(c, cfg)
return isValid, err
} else if cfg.Auth.AuthMethod == "" {
} else if cfg.Auth.Method == "" {
logError("Auth method not set")
return true, nil
} else {
logError("Auth method not supported")
return false, fmt.Errorf(fmt.Sprintf("Auth method %s not supported", cfg.Auth.AuthMethod))
return false, fmt.Errorf(fmt.Sprintf("Auth method %s not supported", cfg.Auth.Method))
}
}

View file

@ -93,16 +93,18 @@ type LogConfig struct {
/*
[auth]
authMethod = "parameters" # "header" or "parameters"
authToken = "token"
Method = "parameters" # "header" or "parameters"
Key = ""
Token = "token"
enabled = false
passThrough = false
ForceAllowApi = true
*/
type AuthConfig struct {
Enabled bool `toml:"enabled"`
AuthMethod string `toml:"authMethod"`
AuthToken string `toml:"authToken"`
Method string `toml:"method"`
Key string `toml:"key"`
Token string `toml:"token"`
PassThrough bool `toml:"passThrough"`
ForceAllowApi bool `toml:"ForceAllowApi"`
}
@ -208,8 +210,9 @@ func DefaultConfig() *Config {
},
Auth: AuthConfig{
Enabled: false,
AuthMethod: "parameters",
AuthToken: "token",
Method: "parameters",
Key: "",
Token: "token",
PassThrough: false,
ForceAllowApi: true,
},

View file

@ -0,0 +1,17 @@
package nocache
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
func NoCacheMiddleware() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
// 设置禁止缓存的响应头
c.Response.Header.Set("Cache-Control", "no-store, no-cache, must-revalidate")
c.Response.Header.Set("Pragma", "no-cache")
c.Response.Header.Set("Expires", "0")
c.Next(ctx) // 继续处理请求
}
}

View file

@ -12,7 +12,7 @@ func AuthPassThrough(c *app.RequestContext, cfg *config.Config, req *http.Reques
token := c.Query("token")
if token != "" {
logDebug("%s %s %s %s %s Auth-PassThrough: token %s", c.ClientIP(), c.Method(), string(c.Path()), c.UserAgent(), c.Request.Header.GetProtocol(), token)
switch cfg.Auth.AuthMethod {
switch cfg.Auth.Method {
case "parameters":
if !cfg.Auth.Enabled {
req.Header.Set("Authorization", "token "+token)

View file

@ -114,7 +114,7 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, error)
user = parts[1]
}
if !cfg.Auth.ForceAllowApi {
if cfg.Auth.AuthMethod != "header" || !cfg.Auth.Enabled {
if cfg.Auth.Method != "header" || !cfg.Auth.Enabled {
return "", "", "", ErrAuthHeaderUnavailable
}
}