4.0.0-beta.0

This commit is contained in:
wjqserver 2025-06-16 08:28:02 +08:00
parent 91c3ad7fd8
commit a4d324a361
38 changed files with 497 additions and 1428 deletions

View file

@ -4,22 +4,21 @@ import (
"fmt"
"ghproxy/config"
"github.com/cloudwego/hertz/pkg/app"
"github.com/infinite-iroha/touka"
)
func AuthHeaderHandler(c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
func AuthHeaderHandler(c *touka.Context, cfg *config.Config) (isValid bool, err error) {
if !cfg.Auth.Enabled {
return true, nil
}
// 获取"GH-Auth"的值
var authToken string
if cfg.Auth.Key != "" {
authToken = string(c.GetHeader(cfg.Auth.Key))
authToken = string(c.Request.Header.Get(cfg.Auth.Key))
} else {
authToken = string(c.GetHeader("GH-Auth"))
authToken = string(c.Request.Header.Get("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")
}

View file

@ -4,10 +4,10 @@ import (
"fmt"
"ghproxy/config"
"github.com/cloudwego/hertz/pkg/app"
"github.com/infinite-iroha/touka"
)
func AuthParametersHandler(c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
func AuthParametersHandler(c *touka.Context, cfg *config.Config) (isValid bool, err error) {
if !cfg.Auth.Enabled {
return true, nil
}
@ -19,8 +19,6 @@ func AuthParametersHandler(c *app.RequestContext, cfg *config.Config) (isValid b
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")
}

View file

@ -4,38 +4,25 @@ import (
"fmt"
"ghproxy/config"
"github.com/WJQSERVER-STUDIO/logger"
"github.com/cloudwego/hertz/pkg/app"
)
var (
logw = logger.Logw
logDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError
"github.com/infinite-iroha/touka"
)
func Init(cfg *config.Config) {
if cfg.Blacklist.Enabled {
err := InitBlacklist(cfg)
if err != nil {
logError(err.Error())
return
panic(err.Error())
}
}
if cfg.Whitelist.Enabled {
err := InitWhitelist(cfg)
if err != nil {
logError(err.Error())
return
panic(err.Error())
}
}
logDebug("Auth Init")
}
func AuthHandler(c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
func AuthHandler(c *touka.Context, cfg *config.Config) (isValid bool, err error) {
if cfg.Auth.Method == "parameters" {
isValid, err = AuthParametersHandler(c, cfg)
return isValid, err
@ -43,10 +30,10 @@ func AuthHandler(c *app.RequestContext, cfg *config.Config) (isValid bool, err e
isValid, err = AuthHeaderHandler(c, cfg)
return isValid, err
} else if cfg.Auth.Method == "" {
logError("Auth method not set")
c.Errorf("Auth method not set")
return true, nil
} else {
logError("Auth method not supported %s", cfg.Auth.Method)
c.Errorf("Auth method not supported %s", cfg.Auth.Method)
return false, fmt.Errorf("%s", fmt.Sprintf("Auth method %s not supported", cfg.Auth.Method))
}
}

View file

@ -7,7 +7,7 @@ import (
"strings"
"sync"
json "github.com/bytedance/sonic"
"encoding/json"
)
type Blacklist struct {

View file

@ -1,13 +1,12 @@
package auth
import (
"encoding/json"
"fmt"
"ghproxy/config"
"os"
"strings"
"sync"
json "github.com/bytedance/sonic"
)
// Whitelist 用于存储白名单信息