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

View file

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

View file

@ -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
}

View file

@ -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)
})
}

View file

@ -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)