This commit is contained in:
WJQSERVER 2024-10-06 03:36:59 +08:00
parent 999016be2b
commit 815b86c6c6
5 changed files with 11 additions and 33 deletions

View file

@ -1,5 +1,12 @@
# 更新日志
24w09a
---
- CHANGE: 优化代码结构,提升性能
- CHANGE: 优化黑名单功能,提升稳定性
- CHANGE&ADD: 新增auth子模块blacklist.go
- CHANGE: 黑名单转为使用json文件存储,便于程序处理
24w08b
---
- CHANGE: 优化代码结构,提升性能

View file

@ -1 +1 @@
24w08b
24w09a

View file

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

13
main.go
View file

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

View file

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