This commit is contained in:
WJQSERVER 2024-10-24 01:17:57 +08:00
parent d7949f625a
commit c0af779642
9 changed files with 37 additions and 20 deletions

View file

@ -1,5 +1,12 @@
# 更新日志
24w19a
---
- PRE-RELEASE: 此版本是v1.6.1的预发布版本,请勿在生产环境中使用
- CHANGE: 根据社区建议,将`sizeLimit`由过去的以`byte`为单位,改为以`MB`为单位,以便于直观理解
- CHANGE: 更新相关依赖
- CHANGE: 对`Proxy`模块的核心函数进行模块化,为后续修改和扩展提供空间
v1.6.0
---
- CHANGE: 优化代码结构,提升性能

View file

@ -1 +1 @@
24w18f
24w19a

View file

@ -1,7 +1,7 @@
[server]
host = "127.0.0.1"
port = 8080
sizeLimit = 131072000 # 125MB
sizeLimit = 125 # MB
[pages]
enabled = false

View file

@ -1,7 +1,7 @@
[server]
host = "127.0.0.1"
port = 8080
sizeLimit = 131072000 # 125MB
sizeLimit = 125 # MB
[pages]
enabled = true

2
go.mod
View file

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

2
go.sum
View file

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

View file

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

10
main.go
View file

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

View file

@ -47,17 +47,8 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
rawPath = "https://" + matches[2]
// 提取用户名和仓库名,格式为 handle/<username>/<repo>/*
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/<username>/<repo>/*
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)