From c0af779642274cbd177a2e53162a7c9c0940fdb1 Mon Sep 17 00:00:00 2001 From: WJQSERVER Date: Thu, 24 Oct 2024 01:17:57 +0800 Subject: [PATCH] 24w19a --- CHANGELOG.md | 7 +++++++ DEV-VERSION | 2 +- config/config.toml | 2 +- deploy/config.toml | 2 +- go.mod | 2 +- go.sum | 2 ++ logger/logger.go | 2 +- main.go | 10 ++++++---- proxy/proxy.go | 28 +++++++++++++++++----------- 9 files changed, 37 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce6a9dd..aa70dbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # 更新日志 +24w19a +--- +- PRE-RELEASE: 此版本是v1.6.1的预发布版本,请勿在生产环境中使用 +- CHANGE: 根据社区建议,将`sizeLimit`由过去的以`byte`为单位,改为以`MB`为单位,以便于直观理解 +- CHANGE: 更新相关依赖 +- CHANGE: 对`Proxy`模块的核心函数进行模块化,为后续修改和扩展提供空间 + v1.6.0 --- - CHANGE: 优化代码结构,提升性能 diff --git a/DEV-VERSION b/DEV-VERSION index 430b82d..2e85c10 100644 --- a/DEV-VERSION +++ b/DEV-VERSION @@ -1 +1 @@ -24w18f \ No newline at end of file +24w19a \ No newline at end of file diff --git a/config/config.toml b/config/config.toml index ea5c9cf..7dc645b 100644 --- a/config/config.toml +++ b/config/config.toml @@ -1,7 +1,7 @@ [server] host = "127.0.0.1" port = 8080 -sizeLimit = 131072000 # 125MB +sizeLimit = 125 # MB [pages] enabled = false diff --git a/deploy/config.toml b/deploy/config.toml index 4ffbd54..65b2fca 100644 --- a/deploy/config.toml +++ b/deploy/config.toml @@ -1,7 +1,7 @@ [server] host = "127.0.0.1" port = 8080 -sizeLimit = 131072000 # 125MB +sizeLimit = 125 # MB [pages] enabled = true diff --git a/go.mod b/go.mod index 1e85761..a69e010 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( require ( github.com/andybalholm/brotli v1.1.1 // indirect github.com/bytedance/sonic v1.12.3 // indirect - github.com/bytedance/sonic/loader v0.2.0 // indirect + github.com/bytedance/sonic/loader v0.2.1 // indirect github.com/cloudflare/circl v1.5.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect diff --git a/go.sum b/go.sum index 27c3095..38f6b03 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKz github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E= +github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/cloudflare/circl v1.5.0 h1:hxIWksrX6XN5a1L2TI/h53AGPhNHoUBo+TD1ms9+pys= github.com/cloudflare/circl v1.5.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= diff --git a/logger/logger.go b/logger/logger.go index e5e5b75..81aa5a3 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -96,7 +96,7 @@ func Close() { } func monitorLogSize(logFilePath string, maxLogsize int) { - var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024 // 最大日志文件大小,单位为MB + var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024 for { time.Sleep(120 * time.Minute) // 每120分钟检查一次日志文件大小 logFileMutex.Lock() diff --git a/main.go b/main.go index 47f63c1..20e081d 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "time" "ghproxy/api" "ghproxy/auth" @@ -12,6 +13,7 @@ import ( "ghproxy/logger" "ghproxy/proxy" + "github.com/gin-contrib/ratelimit" "github.com/gin-gonic/gin" ) @@ -76,19 +78,20 @@ func init() { router = gin.Default() router.UseH2C = true + router.Use(ratelimit.New( + ratelimit.WithTotolLimit(10), + ratelimit.WithDuration(1*time.Minute), + )) setupApi(cfg, router) if cfg.Pages.Enabled { indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir) faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir) - // 静态index页 - //router.StaticFile("/", indexPagePath) router.GET("/", func(c *gin.Context) { c.File(indexPagePath) logInfo("IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto) }) - // 静态favicon.ico router.StaticFile("/favicon.ico", faviconPath) } else if !cfg.Pages.Enabled { router.GET("/", func(c *gin.Context) { @@ -97,7 +100,6 @@ func init() { }) } - // 未匹配路由处理 router.NoRoute(func(c *gin.Context) { proxy.NoRouteHandler(cfg)(c) }) diff --git a/proxy/proxy.go b/proxy/proxy.go index 4479af3..3f467c0 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -47,17 +47,8 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc { rawPath = "https://" + matches[2] - // 提取用户名和仓库名,格式为 handle///* - pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`) - pathParts := pathmatches.FindStringSubmatch(matches[2]) - if len(pathParts) < 4 { - logWarning("Invalid path: %s", rawPath) - c.String(http.StatusForbidden, "Invalid path; expected username/repo.") - return - } + username, repo := MatchUserRepo(rawPath, cfg, c, matches) - username := pathParts[2] - repo := pathParts[3] logWarning("Blacklist Check > Username: %s, Repo: %s", username, repo) fullrepo := fmt.Sprintf("%s/%s", username, repo) @@ -115,6 +106,20 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc { } } +func MatchUserRepo(rawPath string, cfg *config.Config, c *gin.Context, matches []string) (string, string) { + // 提取用户名和仓库名,格式为 handle///* + pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`) + pathParts := pathmatches.FindStringSubmatch(matches[2]) + + if len(pathParts) < 4 { + logWarning("Invalid path: %s", rawPath) + c.String(http.StatusForbidden, "Invalid path; expected username/repo.") + return "", "" + } else { + return pathParts[2], pathParts[3] + } +} + func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) { method := c.Request.Method // 记录日志 IP 地址、请求方法、请求 URL、请求头 User-Agent 、HTTP版本 @@ -206,9 +211,10 @@ func SendRequest(req *req.Request, method, url string) (*req.Response, error) { func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context) error { contentLength := resp.Header.Get("Content-Length") + sizelimit := cfg.Server.SizeLimit * 1024 * 1024 if contentLength != "" { size, err := strconv.Atoi(contentLength) - if err == nil && size > cfg.Server.SizeLimit { + if err == nil && size > sizelimit { finalURL := resp.Request.URL.String() c.Redirect(http.StatusMovedPermanently, finalURL) logWarning("Size limit exceeded: %s, Size: %d", finalURL, size)