mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
24w09a
This commit is contained in:
parent
999016be2b
commit
815b86c6c6
5 changed files with 11 additions and 33 deletions
|
|
@ -1,5 +1,12 @@
|
||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
|
24w09a
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
- CHANGE: 优化黑名单功能,提升稳定性
|
||||||
|
- CHANGE&ADD: 新增auth子模块blacklist.go
|
||||||
|
- CHANGE: 黑名单转为使用json文件存储,便于程序处理
|
||||||
|
|
||||||
24w08b
|
24w08b
|
||||||
---
|
---
|
||||||
- CHANGE: 优化代码结构,提升性能
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
24w08b
|
24w09a
|
||||||
|
|
@ -33,10 +33,6 @@ type Config struct {
|
||||||
} `yaml:"blacklist"`
|
} `yaml:"blacklist"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Blist struct {
|
|
||||||
Blacklist []string `yaml:"blacklist"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadConfig 从 YAML 配置文件加载配置
|
// LoadConfig 从 YAML 配置文件加载配置
|
||||||
func LoadConfig(filePath string) (*Config, error) {
|
func LoadConfig(filePath string) (*Config, error) {
|
||||||
var config Config
|
var config Config
|
||||||
|
|
@ -46,15 +42,6 @@ func LoadConfig(filePath string) (*Config, error) {
|
||||||
return &config, nil
|
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 配置文件加载配置
|
// LoadyamlConfig 从 YAML 配置文件加载配置
|
||||||
func loadYAML(filePath string, out interface{}) error {
|
func loadYAML(filePath string, out interface{}) error {
|
||||||
data, err := os.ReadFile(filePath)
|
data, err := os.ReadFile(filePath)
|
||||||
|
|
|
||||||
13
main.go
13
main.go
|
|
@ -16,7 +16,6 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
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"
|
||||||
|
|
@ -42,15 +41,6 @@ func loadConfig() {
|
||||||
fmt.Printf("Loaded config: %v\n", cfg)
|
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() {
|
func setupLogger() {
|
||||||
// 初始化日志模块
|
// 初始化日志模块
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -65,7 +55,6 @@ func setupLogger() {
|
||||||
func init() {
|
func init() {
|
||||||
loadConfig()
|
loadConfig()
|
||||||
setupLogger()
|
setupLogger()
|
||||||
loadBlacklistConfig()
|
|
||||||
|
|
||||||
// 设置 Gin 模式
|
// 设置 Gin 模式
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
@ -87,7 +76,7 @@ func init() {
|
||||||
|
|
||||||
// 未匹配路由处理
|
// 未匹配路由处理
|
||||||
router.NoRoute(func(c *gin.Context) {
|
router.NoRoute(func(c *gin.Context) {
|
||||||
proxy.NoRouteHandler(cfg, blist)(c)
|
proxy.NoRouteHandler(cfg)(c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,6 @@ import (
|
||||||
|
|
||||||
var logw = logger.Logw
|
var logw = logger.Logw
|
||||||
|
|
||||||
//var cfg *config.Config
|
|
||||||
//var blacklist *config.Blacklist
|
|
||||||
|
|
||||||
var exps = []*regexp.Regexp{
|
var exps = []*regexp.Regexp{
|
||||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
||||||
|
|
@ -30,7 +27,7 @@ var exps = []*regexp.Regexp{
|
||||||
regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`),
|
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) {
|
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:)?/?/?(.*)`)
|
||||||
|
|
@ -62,11 +59,9 @@ func NoRouteHandler(cfg *config.Config, blist *config.Blist) gin.HandlerFunc {
|
||||||
// 黑名单检查
|
// 黑名单检查
|
||||||
blacklistpass := auth.CheckBlacklist(fullrepo)
|
blacklistpass := auth.CheckBlacklist(fullrepo)
|
||||||
if blacklistpass {
|
if blacklistpass {
|
||||||
c.AbortWithStatusJSON(404, gin.H{"error": "Not found"})
|
c.AbortWithStatus(http.StatusForbidden)
|
||||||
logw("Blacklisted repo: %s", fullrepo)
|
logw("Blacklisted repo: %s", fullrepo)
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
logw("Not blacklisted: %s", fullrepo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
matches = CheckURL(rawPath)
|
matches = CheckURL(rawPath)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue