This commit is contained in:
WJQSERVER 2025-02-14 15:50:42 +08:00
parent 459aa24f89
commit bd666e08d1
7 changed files with 62 additions and 52 deletions

View file

@ -1,5 +1,13 @@
# 更新日志 # 更新日志
25w14t-1
---
- PRE-RELEASE: 此版本是测试验证版本,请勿在生产环境中使用;
- CHANGE: 使用`touka-httpc`封装`HTTP Client`
- CHANGE: 重构前端页面, 见[#49](https://github.com/WJQSERVER-STUDIO/ghproxy/pull/49)
- CHANGE: 重构`blacklist`实现
- CHANGE: 优化404处理
2.2.0 2.2.0
--- ---
- RELEASE: v2.2.0正式版发布; - RELEASE: v2.2.0正式版发布;

View file

@ -1 +1 @@
25w13b 25w14t-1

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/BurntSushi/toml v1.4.0 github.com/BurntSushi/toml v1.4.0
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0 github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/satomitouka/touka-httpc v0.0.3
golang.org/x/net v0.35.0 golang.org/x/net v0.35.0
golang.org/x/time v0.10.0 golang.org/x/time v0.10.0
) )

4
go.sum
View file

@ -51,6 +51,8 @@ github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNH
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satomitouka/touka-httpc v0.0.3 h1:SLb14DWBIDeIaNQ0wMwRwJMjUDakHVR1Jbdct3Qi8fA=
github.com/satomitouka/touka-httpc v0.0.3/go.mod h1:ULB/0Ze0Apm46YKl35Jmj1hW5YLVVeOGqCqn+ijqGPM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@ -71,8 +73,6 @@ golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4=
golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View file

@ -149,6 +149,19 @@
line-height: 26px; line-height: 26px;
color: #747474; color: #747474;
} }
.bg-primary {
--bs-bg-opacity: 1;
background-color: #2c82de !important;
}
.bg-success {
--bs-bg-opacity: 1;
background-color: #2c82de !important;
}
.bg-danger {
--bs-bg-opacity: 1;
background-color: #2c82de !important;
}
</style> </style>
</head> </head>
@ -299,7 +312,7 @@
<div class="col-md-6"> <div class="col-md-6">
<div class="main-card p-3"> <div class="main-card p-3">
<h4 class="h6 fw-medium mb-2">HTTPS Git Clone</h4> <h4 class="h6 fw-medium mb-2">HTTPS Git Clone</h4>
<code class="d-block text-muted fs-sm">https://github.com/user/repo.git</code> <code class="d-block text-muted fs-sm">git clone https://github.com/user/repo.git</code>
</div> </div>
</div> </div>
</div> </div>

View file

@ -5,21 +5,21 @@ import (
"fmt" "fmt"
"ghproxy/config" "ghproxy/config"
"io" "io"
"net"
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
httpc "github.com/satomitouka/touka-httpc"
) )
var BufferSize int = 32 * 1024 // 32KB var BufferSize int = 32 * 1024 // 32KB
var ( var (
cclient *http.Client
ctr *http.Transport ctr *http.Transport
BufferPool *sync.Pool BufferPool *sync.Pool
cclient *httpc.Client
) )
func InitReq(cfg *config.Config) { func InitReq(cfg *config.Config) {
@ -35,6 +35,7 @@ func InitReq(cfg *config.Config) {
} }
func initChunkedHTTPClient(cfg *config.Config) { func initChunkedHTTPClient(cfg *config.Config) {
/*
ctr = &http.Transport{ ctr = &http.Transport{
MaxIdleConns: 100, MaxIdleConns: 100,
MaxConnsPerHost: 60, MaxConnsPerHost: 60,
@ -47,19 +48,25 @@ func initChunkedHTTPClient(cfg *config.Config) {
KeepAlive: 30 * time.Second, KeepAlive: 30 * time.Second,
}).DialContext, }).DialContext,
} }
*/
ctr = &http.Transport{
MaxIdleConns: 100,
MaxConnsPerHost: 60,
IdleConnTimeout: 20 * time.Second,
}
if cfg.Outbound.Enabled { if cfg.Outbound.Enabled {
initTransport(cfg, ctr) initTransport(cfg, ctr)
} }
cclient = &http.Client{ cclient = httpc.New(
Transport: ctr, httpc.WithTransport(ctr),
} )
} }
func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) { func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
method := c.Request.Method method := c.Request.Method
// 发送HEAD请求, 预获取Content-Length // 发送HEAD请求, 预获取Content-Length
headReq, err := http.NewRequest("HEAD", u, nil) headReq, err := cclient.NewRequest("HEAD", u, nil)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return return
@ -92,13 +99,6 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
} }
} }
/*
if err := HandleResponseSize(headResp, cfg, c); err != nil {
logWarning("%s %s %s %s %s Response-Size-Error: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
return
}
*/
body, err := readRequestBody(c) body, err := readRequestBody(c)
if err != nil { if err != nil {
HandleError(c, err.Error()) HandleError(c, err.Error())
@ -107,13 +107,11 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
bodyReader := bytes.NewBuffer(body) bodyReader := bytes.NewBuffer(body)
// 创建请求 req, err := cclient.NewRequest(method, u, bodyReader)
req, err := http.NewRequest(method, u, bodyReader)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return return
} }
setRequestHeaders(c, req) setRequestHeaders(c, req)
removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头) removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
AuthPassThrough(c, cfg, req) AuthPassThrough(c, cfg, req)
@ -125,13 +123,6 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
} }
defer resp.Body.Close() defer resp.Body.Close()
/*
if err := HandleResponseSize(resp, cfg, c); err != nil {
logWarning("%s %s %s %s %s Response-Size-Error: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
return
}
*/
// 错误处理(404) // 错误处理(404)
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
c.String(http.StatusNotFound, "File Not Found") c.String(http.StatusNotFound, "File Not Found")

View file

@ -10,10 +10,11 @@ import (
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
httpc "github.com/satomitouka/touka-httpc"
) )
var ( var (
gclient *http.Client gclient *httpc.Client
gtr *http.Transport gtr *http.Transport
) )
@ -26,20 +27,22 @@ func initGitHTTPClient(cfg *config.Config) {
if cfg.Outbound.Enabled { if cfg.Outbound.Enabled {
initTransport(cfg, gtr) initTransport(cfg, gtr)
} }
/*
gclient = &http.Client{ gclient = &http.Client{
Transport: gtr, Transport: gtr,
} }
*/
gclient = httpc.New(
httpc.WithTransport(gtr),
)
} }
func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) { func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
method := c.Request.Method method := c.Request.Method
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto) logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
// 创建HTTP客户端
//client := &http.Client{}
// 发送HEAD请求, 预获取Content-Length // 发送HEAD请求, 预获取Content-Length
headReq, err := http.NewRequest("HEAD", u, nil) headReq, err := gclient.NewRequest("HEAD", u, nil)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return return
@ -81,7 +84,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
bodyReader := bytes.NewBuffer(body) bodyReader := bytes.NewBuffer(body)
// 创建请求 // 创建请求
req, err := http.NewRequest(method, u, bodyReader) req, err := gclient.NewRequest(method, u, bodyReader)
if err != nil { if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return return
@ -101,12 +104,6 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
} }
}(resp.Body) }(resp.Body)
/*
if err := HandleResponseSize(resp, cfg, c); err != nil {
logWarning("%s %s %s %s %s Response-Size-Error: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
return
}
*/
contentLength = resp.Header.Get("Content-Length") contentLength = resp.Header.Get("Content-Length")
if contentLength != "" { if contentLength != "" {
size, err := strconv.Atoi(contentLength) size, err := strconv.Atoi(contentLength)