mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
[break] change auth config & add auth key
This commit is contained in:
parent
978ece6fa0
commit
395f641468
8 changed files with 50 additions and 27 deletions
13
api/api.go
13
api/api.go
|
|
@ -3,6 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
|
"ghproxy/middleware/nocache"
|
||||||
|
|
||||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||||
"github.com/cloudwego/hertz/pkg/app"
|
"github.com/cloudwego/hertz/pkg/app"
|
||||||
|
|
@ -18,18 +19,8 @@ var (
|
||||||
logError = logger.LogError
|
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) {
|
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) {
|
apiRouter.GET("/size_limit", func(ctx context.Context, c *app.RequestContext) {
|
||||||
SizeLimitHandler(cfg, c, ctx)
|
SizeLimitHandler(cfg, c, ctx)
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,19 @@ func AuthHeaderHandler(c *app.RequestContext, cfg *config.Config) (isValid bool,
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
// 获取"GH-Auth"的值
|
// 获取"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)
|
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 == "" {
|
if authToken == "" {
|
||||||
return false, fmt.Errorf("Auth token not found")
|
return false, fmt.Errorf("Auth token not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid = authToken == cfg.Auth.AuthToken
|
isValid = authToken == cfg.Auth.Token
|
||||||
if !isValid {
|
if !isValid {
|
||||||
return false, fmt.Errorf("Auth token incorrect")
|
return false, fmt.Errorf("Auth token incorrect")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,20 @@ func AuthParametersHandler(c *app.RequestContext, cfg *config.Config) (isValid b
|
||||||
return true, nil
|
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)
|
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 == "" {
|
if authToken == "" {
|
||||||
return false, fmt.Errorf("Auth token not found")
|
return false, fmt.Errorf("Auth token not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid = authToken == cfg.Auth.AuthToken
|
isValid = authToken == cfg.Auth.Token
|
||||||
if !isValid {
|
if !isValid {
|
||||||
return false, fmt.Errorf("Auth token invalid")
|
return false, fmt.Errorf("Auth token invalid")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,17 +37,17 @@ func Init(cfg *config.Config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthHandler(ctx context.Context, c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
|
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)
|
isValid, err = AuthParametersHandler(c, cfg)
|
||||||
return isValid, err
|
return isValid, err
|
||||||
} else if cfg.Auth.AuthMethod == "header" {
|
} else if cfg.Auth.Method == "header" {
|
||||||
isValid, err = AuthHeaderHandler(c, cfg)
|
isValid, err = AuthHeaderHandler(c, cfg)
|
||||||
return isValid, err
|
return isValid, err
|
||||||
} else if cfg.Auth.AuthMethod == "" {
|
} else if cfg.Auth.Method == "" {
|
||||||
logError("Auth method not set")
|
logError("Auth method not set")
|
||||||
return true, nil
|
return true, nil
|
||||||
} else {
|
} else {
|
||||||
logError("Auth method not supported")
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,16 +93,18 @@ type LogConfig struct {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
[auth]
|
[auth]
|
||||||
authMethod = "parameters" # "header" or "parameters"
|
Method = "parameters" # "header" or "parameters"
|
||||||
authToken = "token"
|
Key = ""
|
||||||
|
Token = "token"
|
||||||
enabled = false
|
enabled = false
|
||||||
passThrough = false
|
passThrough = false
|
||||||
ForceAllowApi = true
|
ForceAllowApi = true
|
||||||
*/
|
*/
|
||||||
type AuthConfig struct {
|
type AuthConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled"`
|
||||||
AuthMethod string `toml:"authMethod"`
|
Method string `toml:"method"`
|
||||||
AuthToken string `toml:"authToken"`
|
Key string `toml:"key"`
|
||||||
|
Token string `toml:"token"`
|
||||||
PassThrough bool `toml:"passThrough"`
|
PassThrough bool `toml:"passThrough"`
|
||||||
ForceAllowApi bool `toml:"ForceAllowApi"`
|
ForceAllowApi bool `toml:"ForceAllowApi"`
|
||||||
}
|
}
|
||||||
|
|
@ -208,8 +210,9 @@ func DefaultConfig() *Config {
|
||||||
},
|
},
|
||||||
Auth: AuthConfig{
|
Auth: AuthConfig{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
AuthMethod: "parameters",
|
Method: "parameters",
|
||||||
AuthToken: "token",
|
Key: "",
|
||||||
|
Token: "token",
|
||||||
PassThrough: false,
|
PassThrough: false,
|
||||||
ForceAllowApi: true,
|
ForceAllowApi: true,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
17
middleware/nocache/nocache.go
Normal file
17
middleware/nocache/nocache.go
Normal 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) // 继续处理请求
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,7 +12,7 @@ func AuthPassThrough(c *app.RequestContext, cfg *config.Config, req *http.Reques
|
||||||
token := c.Query("token")
|
token := c.Query("token")
|
||||||
if 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)
|
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":
|
case "parameters":
|
||||||
if !cfg.Auth.Enabled {
|
if !cfg.Auth.Enabled {
|
||||||
req.Header.Set("Authorization", "token "+token)
|
req.Header.Set("Authorization", "token "+token)
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, error)
|
||||||
user = parts[1]
|
user = parts[1]
|
||||||
}
|
}
|
||||||
if !cfg.Auth.ForceAllowApi {
|
if !cfg.Auth.ForceAllowApi {
|
||||||
if cfg.Auth.AuthMethod != "header" || !cfg.Auth.Enabled {
|
if cfg.Auth.Method != "header" || !cfg.Auth.Enabled {
|
||||||
return "", "", "", ErrAuthHeaderUnavailable
|
return "", "", "", ErrAuthHeaderUnavailable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue