This commit is contained in:
WJQSERVER 2024-10-06 01:03:38 +08:00
parent 5dbf137116
commit b87a8de3c4
5 changed files with 16 additions and 20 deletions

View file

@ -1,9 +1,10 @@
package auth package auth
func CheckBlacklist(fullrepo string) bool { func CheckBlacklist(fullrepo string, blist []string) bool {
if fullrepo == "test/test1" { for _, blocked := range blist {
logw("%s in blacklist", fullrepo) if blocked == fullrepo {
return true return true
}
} }
logw("%s not in blacklist", fullrepo) logw("%s not in blacklist", fullrepo)
return false return false

View file

@ -1,9 +1,4 @@
blacklist: blacklist:
username1: - test/test1
- repo1 - example/repo2
- repo2 - another/repo3
username2:
- repo3
- repo4
username3:
- repo5

View file

@ -33,8 +33,8 @@ type Config struct {
} `yaml:"blacklist"` } `yaml:"blacklist"`
} }
type BlacklistMap struct { type Blist struct {
Blist map[string][]string `yaml:"blacklist"` Blacklist []string `yaml:"blacklist"`
} }
// LoadConfig 从 YAML 配置文件加载配置 // LoadConfig 从 YAML 配置文件加载配置
@ -47,8 +47,8 @@ func LoadConfig(filePath string) (*Config, error) {
} }
// LoadBlacklistConfig 从 YAML 配置文件加载黑名单配置 // LoadBlacklistConfig 从 YAML 配置文件加载黑名单配置
func LoadBlacklistConfig(filePath string) (*BlacklistMap, error) { func LoadBlacklistConfig(filePath string) (*Blist, error) {
var blacklist BlacklistMap var blacklist Blist
if err := loadYAML(filePath, &blacklist); err != nil { if err := loadYAML(filePath, &blacklist); err != nil {
return nil, err return nil, err
} }

View file

@ -16,7 +16,7 @@ import (
var ( var (
cfg *config.Config cfg *config.Config
blacklist *config.BlacklistMap blist *config.Blist
logw = logger.Logw logw = logger.Logw
router *gin.Engine router *gin.Engine
configfile = "/data/ghproxy/config/config.yaml" configfile = "/data/ghproxy/config/config.yaml"
@ -87,7 +87,7 @@ func init() {
// 未匹配路由处理 // 未匹配路由处理
router.NoRoute(func(c *gin.Context) { router.NoRoute(func(c *gin.Context) {
proxy.NoRouteHandler(cfg, config.BlacklistMap{})(c) proxy.NoRouteHandler(cfg, blist)(c)
}) })
} }

View file

@ -30,7 +30,7 @@ var exps = []*regexp.Regexp{
regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`), regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`),
} }
func NoRouteHandler(cfg *config.Config, bmap config.BlacklistMap) gin.HandlerFunc { func NoRouteHandler(cfg *config.Config, blist *config.Blist) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/") rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
@ -60,7 +60,7 @@ func NoRouteHandler(cfg *config.Config, bmap config.BlacklistMap) gin.HandlerFun
fullrepo := fmt.Sprintf("%s/%s", username, repo) fullrepo := fmt.Sprintf("%s/%s", username, repo)
// 黑名单检查 // 黑名单检查
blacklistpass := auth.CheckBlacklist(fullrepo) blacklistpass := auth.CheckBlacklist(fullrepo, blist.Blacklist)
if blacklistpass { if blacklistpass {
c.AbortWithStatusJSON(404, gin.H{"error": "Not found"}) c.AbortWithStatusJSON(404, gin.H{"error": "Not found"})
logw("Blacklisted repo: %s", fullrepo) logw("Blacklisted repo: %s", fullrepo)