diff --git a/CHANGELOG.md b/CHANGELOG.md index f3cbcba..87007a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # 更新日志 +24w09a +--- +- CHANGE: 优化代码结构,提升性能 +- CHANGE: 优化黑名单功能,提升稳定性 +- CHANGE&ADD: 新增auth子模块blacklist.go +- CHANGE: 黑名单转为使用json文件存储,便于程序处理 + 24w08b --- - CHANGE: 优化代码结构,提升性能 diff --git a/DEV-VERSION b/DEV-VERSION index 1a1e17d..e8461ba 100644 --- a/DEV-VERSION +++ b/DEV-VERSION @@ -1 +1 @@ -24w08b \ No newline at end of file +24w09a \ No newline at end of file diff --git a/config/config.go b/config/config.go index 50ba09d..1a98a98 100644 --- a/config/config.go +++ b/config/config.go @@ -33,10 +33,6 @@ type Config struct { } `yaml:"blacklist"` } -type Blist struct { - Blacklist []string `yaml:"blacklist"` -} - // LoadConfig 从 YAML 配置文件加载配置 func LoadConfig(filePath string) (*Config, error) { var config Config @@ -46,15 +42,6 @@ func LoadConfig(filePath string) (*Config, error) { return &config, nil } -// LoadBlacklistConfig 从 YAML 配置文件加载黑名单配置 -func LoadBlacklistConfig(filePath string) (*Blist, error) { - var blacklist Blist - if err := loadYAML(filePath, &blacklist); err != nil { - return nil, err - } - return &blacklist, nil -} - // LoadyamlConfig 从 YAML 配置文件加载配置 func loadYAML(filePath string, out interface{}) error { data, err := os.ReadFile(filePath) diff --git a/main.go b/main.go index ddd4d91..7ccb844 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ import ( var ( cfg *config.Config - blist *config.Blist logw = logger.Logw router *gin.Engine configfile = "/data/ghproxy/config/config.yaml" @@ -42,15 +41,6 @@ func loadConfig() { fmt.Printf("Loaded config: %v\n", cfg) } -func loadBlacklistConfig() { - // 初始化黑名单配置 - blacklist, err := config.LoadBlacklistConfig(cfg.Blacklist.BlacklistFile) - if err != nil { - log.Fatalf("Failed to load blacklist: %v", err) - } - logw("Loaded blacklist: %v", blacklist) -} - func setupLogger() { // 初始化日志模块 var err error @@ -65,7 +55,6 @@ func setupLogger() { func init() { loadConfig() setupLogger() - loadBlacklistConfig() // 设置 Gin 模式 gin.SetMode(gin.ReleaseMode) @@ -87,7 +76,7 @@ func init() { // 未匹配路由处理 router.NoRoute(func(c *gin.Context) { - proxy.NoRouteHandler(cfg, blist)(c) + proxy.NoRouteHandler(cfg)(c) }) } diff --git a/proxy/proxy.go b/proxy/proxy.go index 5ca126b..2fb0930 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -19,9 +19,6 @@ import ( var logw = logger.Logw -//var cfg *config.Config -//var blacklist *config.Blacklist - var exps = []*regexp.Regexp{ regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`), regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`), @@ -30,7 +27,7 @@ var exps = []*regexp.Regexp{ regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`), } -func NoRouteHandler(cfg *config.Config, blist *config.Blist) gin.HandlerFunc { +func NoRouteHandler(cfg *config.Config) gin.HandlerFunc { return func(c *gin.Context) { rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/") re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) @@ -62,11 +59,9 @@ func NoRouteHandler(cfg *config.Config, blist *config.Blist) gin.HandlerFunc { // 黑名单检查 blacklistpass := auth.CheckBlacklist(fullrepo) if blacklistpass { - c.AbortWithStatusJSON(404, gin.H{"error": "Not found"}) + c.AbortWithStatus(http.StatusForbidden) logw("Blacklisted repo: %s", fullrepo) return - } else { - logw("Not blacklisted: %s", fullrepo) } matches = CheckURL(rawPath)