From b87a8de3c42721d13b2ddd596cc3235ae0aaa78e Mon Sep 17 00:00:00 2001 From: WJQSERVER Date: Sun, 6 Oct 2024 01:03:38 +0800 Subject: [PATCH] fix --- auth/blacklist.go | 9 +++++---- config/blacklist.yaml | 11 +++-------- config/config.go | 8 ++++---- main.go | 4 ++-- proxy/proxy.go | 4 ++-- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/auth/blacklist.go b/auth/blacklist.go index 2d4b634..a008a42 100644 --- a/auth/blacklist.go +++ b/auth/blacklist.go @@ -1,9 +1,10 @@ package auth -func CheckBlacklist(fullrepo string) bool { - if fullrepo == "test/test1" { - logw("%s in blacklist", fullrepo) - return true +func CheckBlacklist(fullrepo string, blist []string) bool { + for _, blocked := range blist { + if blocked == fullrepo { + return true + } } logw("%s not in blacklist", fullrepo) return false diff --git a/config/blacklist.yaml b/config/blacklist.yaml index db926d5..2809c01 100644 --- a/config/blacklist.yaml +++ b/config/blacklist.yaml @@ -1,9 +1,4 @@ blacklist: - username1: - - repo1 - - repo2 - username2: - - repo3 - - repo4 - username3: - - repo5 \ No newline at end of file + - test/test1 + - example/repo2 + - another/repo3 diff --git a/config/config.go b/config/config.go index e4b36e6..50ba09d 100644 --- a/config/config.go +++ b/config/config.go @@ -33,8 +33,8 @@ type Config struct { } `yaml:"blacklist"` } -type BlacklistMap struct { - Blist map[string][]string `yaml:"blacklist"` +type Blist struct { + Blacklist []string `yaml:"blacklist"` } // LoadConfig 从 YAML 配置文件加载配置 @@ -47,8 +47,8 @@ func LoadConfig(filePath string) (*Config, error) { } // LoadBlacklistConfig 从 YAML 配置文件加载黑名单配置 -func LoadBlacklistConfig(filePath string) (*BlacklistMap, error) { - var blacklist BlacklistMap +func LoadBlacklistConfig(filePath string) (*Blist, error) { + var blacklist Blist if err := loadYAML(filePath, &blacklist); err != nil { return nil, err } diff --git a/main.go b/main.go index 9a58fae..ddd4d91 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( var ( cfg *config.Config - blacklist *config.BlacklistMap + blist *config.Blist logw = logger.Logw router *gin.Engine configfile = "/data/ghproxy/config/config.yaml" @@ -87,7 +87,7 @@ func init() { // 未匹配路由处理 router.NoRoute(func(c *gin.Context) { - proxy.NoRouteHandler(cfg, config.BlacklistMap{})(c) + proxy.NoRouteHandler(cfg, blist)(c) }) } diff --git a/proxy/proxy.go b/proxy/proxy.go index fc1cfe9..fdd06ec 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -30,7 +30,7 @@ var exps = []*regexp.Regexp{ 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) { rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/") 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) // 黑名单检查 - blacklistpass := auth.CheckBlacklist(fullrepo) + blacklistpass := auth.CheckBlacklist(fullrepo, blist.Blacklist) if blacklistpass { c.AbortWithStatusJSON(404, gin.H{"error": "Not found"}) logw("Blacklisted repo: %s", fullrepo)