mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 00:01:10 +08:00
24w19a
This commit is contained in:
parent
d7949f625a
commit
c0af779642
9 changed files with 37 additions and 20 deletions
|
|
@ -1,5 +1,12 @@
|
||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
|
24w19a
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v1.6.1的预发布版本,请勿在生产环境中使用
|
||||||
|
- CHANGE: 根据社区建议,将`sizeLimit`由过去的以`byte`为单位,改为以`MB`为单位,以便于直观理解
|
||||||
|
- CHANGE: 更新相关依赖
|
||||||
|
- CHANGE: 对`Proxy`模块的核心函数进行模块化,为后续修改和扩展提供空间
|
||||||
|
|
||||||
v1.6.0
|
v1.6.0
|
||||||
---
|
---
|
||||||
- CHANGE: 优化代码结构,提升性能
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
24w18f
|
24w19a
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
[server]
|
[server]
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
port = 8080
|
port = 8080
|
||||||
sizeLimit = 131072000 # 125MB
|
sizeLimit = 125 # MB
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
enabled = false
|
enabled = false
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
[server]
|
[server]
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
port = 8080
|
port = 8080
|
||||||
sizeLimit = 131072000 # 125MB
|
sizeLimit = 125 # MB
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -11,7 +11,7 @@ require (
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.1.1 // indirect
|
github.com/andybalholm/brotli v1.1.1 // indirect
|
||||||
github.com/bytedance/sonic v1.12.3 // 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/cloudflare/circl v1.5.0 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
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.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 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
|
||||||
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
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 h1:hxIWksrX6XN5a1L2TI/h53AGPhNHoUBo+TD1ms9+pys=
|
||||||
github.com/cloudflare/circl v1.5.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
|
github.com/cloudflare/circl v1.5.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
|
||||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ func Close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func monitorLogSize(logFilePath string, maxLogsize int) {
|
func monitorLogSize(logFilePath string, maxLogsize int) {
|
||||||
var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024 // 最大日志文件大小,单位为MB
|
var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024
|
||||||
for {
|
for {
|
||||||
time.Sleep(120 * time.Minute) // 每120分钟检查一次日志文件大小
|
time.Sleep(120 * time.Minute) // 每120分钟检查一次日志文件大小
|
||||||
logFileMutex.Lock()
|
logFileMutex.Lock()
|
||||||
|
|
|
||||||
10
main.go
10
main.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"ghproxy/api"
|
"ghproxy/api"
|
||||||
"ghproxy/auth"
|
"ghproxy/auth"
|
||||||
|
|
@ -12,6 +13,7 @@ import (
|
||||||
"ghproxy/logger"
|
"ghproxy/logger"
|
||||||
"ghproxy/proxy"
|
"ghproxy/proxy"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/ratelimit"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -76,19 +78,20 @@ func init() {
|
||||||
|
|
||||||
router = gin.Default()
|
router = gin.Default()
|
||||||
router.UseH2C = true
|
router.UseH2C = true
|
||||||
|
router.Use(ratelimit.New(
|
||||||
|
ratelimit.WithTotolLimit(10),
|
||||||
|
ratelimit.WithDuration(1*time.Minute),
|
||||||
|
))
|
||||||
|
|
||||||
setupApi(cfg, router)
|
setupApi(cfg, router)
|
||||||
|
|
||||||
if cfg.Pages.Enabled {
|
if cfg.Pages.Enabled {
|
||||||
indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir)
|
indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir)
|
||||||
faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir)
|
faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir)
|
||||||
// 静态index页
|
|
||||||
//router.StaticFile("/", indexPagePath)
|
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.GET("/", func(c *gin.Context) {
|
||||||
c.File(indexPagePath)
|
c.File(indexPagePath)
|
||||||
logInfo("IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
|
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)
|
router.StaticFile("/favicon.ico", faviconPath)
|
||||||
} else if !cfg.Pages.Enabled {
|
} else if !cfg.Pages.Enabled {
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.GET("/", func(c *gin.Context) {
|
||||||
|
|
@ -97,7 +100,6 @@ func init() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 未匹配路由处理
|
|
||||||
router.NoRoute(func(c *gin.Context) {
|
router.NoRoute(func(c *gin.Context) {
|
||||||
proxy.NoRouteHandler(cfg)(c)
|
proxy.NoRouteHandler(cfg)(c)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -47,17 +47,8 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||||
|
|
||||||
rawPath = "https://" + matches[2]
|
rawPath = "https://" + matches[2]
|
||||||
|
|
||||||
// 提取用户名和仓库名,格式为 handle/<username>/<repo>/*
|
username, repo := MatchUserRepo(rawPath, cfg, c, matches)
|
||||||
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 := pathParts[2]
|
|
||||||
repo := pathParts[3]
|
|
||||||
logWarning("Blacklist Check > Username: %s, Repo: %s", username, repo)
|
logWarning("Blacklist Check > Username: %s, Repo: %s", username, repo)
|
||||||
fullrepo := fmt.Sprintf("%s/%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) {
|
func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
// 记录日志 IP 地址、请求方法、请求 URL、请求头 User-Agent 、HTTP版本
|
// 记录日志 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 {
|
func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context) error {
|
||||||
contentLength := resp.Header.Get("Content-Length")
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
|
sizelimit := cfg.Server.SizeLimit * 1024 * 1024
|
||||||
if contentLength != "" {
|
if contentLength != "" {
|
||||||
size, err := strconv.Atoi(contentLength)
|
size, err := strconv.Atoi(contentLength)
|
||||||
if err == nil && size > cfg.Server.SizeLimit {
|
if err == nil && size > sizelimit {
|
||||||
finalURL := resp.Request.URL.String()
|
finalURL := resp.Request.URL.String()
|
||||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||||
logWarning("Size limit exceeded: %s, Size: %d", finalURL, size)
|
logWarning("Size limit exceeded: %s, Size: %d", finalURL, size)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue