From 267dfafcb978934d40b0c43ceee333a6e952fa84 Mon Sep 17 00:00:00 2001 From: WJQSERVER Date: Mon, 4 Nov 2024 05:53:53 +0800 Subject: [PATCH] 24w21d --- CHANGELOG.md | 10 +++++ DEV-VERSION | 2 +- api/api.go | 11 +++++ auth/auth.go | 6 --- auth/blacklist.go | 27 ++++++++--- go.mod | 4 +- go.sum | 2 + logger/logger.go | 2 - pages/index.html | 111 +++++++++++++++++++++++++++++++++++++++++----- proxy/proxy.go | 14 +++--- 10 files changed, 154 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cbae18..7cae8bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # 更新日志 +24w21d +--- +- PRE-RELEASE: 此版本是v1.7.0的预发布版本,请勿在生产环境中使用 +- ADD: 新增`ratePerMinute` API可供查询 +- ADD: 前端新增 version 标识 +- ADD: 前端新增 `重定向` 按钮,用于重定向到代理后的链接 +- CHANGE: 优化输出代码块,使样式更加美观 +- CHANGE: 更新相关依赖库 +- CHANGE: 对黑名单模块进行实验性功能优化,提升性能 + 24w21c --- - PRE-RELEASE: 此版本是v1.7.0的预发布版本,请勿在生产环境中使用 diff --git a/DEV-VERSION b/DEV-VERSION index e08590f..9aa2b7c 100644 --- a/DEV-VERSION +++ b/DEV-VERSION @@ -1 +1 @@ -24w21c \ No newline at end of file +24w21d \ No newline at end of file diff --git a/api/api.go b/api/api.go index 47d2992..27f5bac 100644 --- a/api/api.go +++ b/api/api.go @@ -44,6 +44,9 @@ func InitHandleRouter(cfg *config.Config, router *gin.Engine, version string) { apiRouter.GET("/rate_limit/status", func(c *gin.Context) { RateLimitStatusHandler(c, cfg) }) + apiRouter.GET("/rate_limit/limit", func(c *gin.Context) { + RateLimitLimitHandler(c, cfg) + }) } logInfo("API router Init success") } @@ -104,3 +107,11 @@ func RateLimitStatusHandler(c *gin.Context, cfg *config.Config) { "RateLimit": cfg.RateLimit.Enabled, }) } + +func RateLimitLimitHandler(c *gin.Context, cfg *config.Config) { + logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto) + c.Writer.Header().Set("Content-Type", "application/json") + json.NewEncoder(c.Writer).Encode(map[string]interface{}{ + "RatePerMinute": cfg.RateLimit.RatePerMinute, + }) +} diff --git a/auth/auth.go b/auth/auth.go index 35cd8d0..339486f 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -8,7 +8,6 @@ import ( "github.com/gin-gonic/gin" ) -// 日志模块 var ( logw = logger.Logw logInfo = logger.LogInfo @@ -16,7 +15,6 @@ var ( logError = logger.LogError ) -// Auth Init func Init(cfg *config.Config) { if cfg.Blacklist.Enabled { LoadBlacklist(cfg) @@ -28,17 +26,13 @@ func Init(cfg *config.Config) { } func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) { - // 如果身份验证未启用,直接返回 true if !cfg.Auth.Enabled { return true, "" } - // 获取 auth_token 参数 authToken := c.Query("auth_token") - // IP METHOD URL USERAGENT PROTO TOKEN logInfo("%s %s %s %s %s AUTH_TOKEN: %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto, authToken) - // 验证 token if authToken == "" { err := "Auth token == nil" return false, err diff --git a/auth/blacklist.go b/auth/blacklist.go index 4b8f17d..f293ffc 100644 --- a/auth/blacklist.go +++ b/auth/blacklist.go @@ -33,8 +33,8 @@ func LoadBlacklist(cfg *config.Config) { } // fullrepo: "owner/repo" or "owner/*" -func CheckBlacklist(fullrepo string) bool { - return forRangeCheckBlacklist(blacklist.Blacklist, fullrepo) +func CheckBlacklist(repouser string, user string, repo string) bool { + return forRangeCheckBlacklist(blacklist.Blacklist, repouser, user) } func sliceRepoName_Blacklist(fullrepo string) (string, string) { @@ -45,12 +45,29 @@ func sliceRepoName_Blacklist(fullrepo string) (string, string) { return s[0], s[1] } -func forRangeCheckBlacklist(blist []string, fullrepo string) bool { - repoUser, _ := sliceRepoName_Blacklist(fullrepo) +func forRangeCheckBlacklist(blist []string, fullrepo string, user string) bool { + // 先匹配user,再匹配user/*,最后匹配完整repo for _, blocked := range blist { + // 切片 + users, _ := sliceRepoName_Blacklist(blocked) + logw("users:%s, blocked:%s", users, blocked) + // 匹配 user + if user == users { + // 匹配 user/* + if strings.HasSuffix(blocked, "/*") { + return true + } + // 匹配完整repo + if fullrepo == blocked { + return true + } + } + } + + /* for _, blocked := range blist { if blocked == fullrepo || (strings.HasSuffix(blocked, "/*") && strings.HasPrefix(repoUser, blocked[:len(blocked)-2])) { return true } - } + } */ return false } diff --git a/go.mod b/go.mod index b57e697..bc42874 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/BurntSushi/toml v1.4.0 github.com/gin-gonic/gin v1.10.0 github.com/imroc/req/v3 v3.48.0 + golang.org/x/time v0.7.0 ) require ( @@ -22,7 +23,7 @@ require ( github.com/go-playground/validator/v10 v10.22.1 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/goccy/go-json v0.10.3 // indirect - github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db // indirect + github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -48,7 +49,6 @@ require ( golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect - golang.org/x/time v0.7.0 // indirect golang.org/x/tools v0.26.0 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 49b0619..9fab411 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ github.com/google/pprof v0.0.0-20241023014458-598669927662 h1:SKMkD83p7FwUqKmBsP github.com/google/pprof v0.0.0-20241023014458-598669927662/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db h1:097atOisP2aRj7vFgYQBbFN4U4JNXUNYpxael3UzMyo= github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= +github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 h1:sAGdeJj0bnMgUNVeUpp6AYlVdCt3/GdI3pGRqsNSQLs= +github.com/google/pprof v0.0.0-20241101162523-b92577c0c142/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= diff --git a/logger/logger.go b/logger/logger.go index 5eac8e8..5e2f2b6 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -56,7 +56,6 @@ func Log(customMessage string) { logChannel <- customMessage } -// 格式化日志记录 func Logw(format string, args ...interface{}) { message := fmt.Sprintf(format, args...) Log(message) @@ -82,7 +81,6 @@ func LogError(format string, args ...interface{}) { Log(message) } -// 关闭日志文件 func Close() { logFileMutex.Lock() defer logFileMutex.Unlock() diff --git a/pages/index.html b/pages/index.html index 82a072d..15d9590 100644 --- a/pages/index.html +++ b/pages/index.html @@ -6,12 +6,11 @@ - + Github文件加速