This commit is contained in:
wjqserver
2025-09-14 07:31:41 +08:00
parent e3f84f4c17
commit bd9f590b0a
4 changed files with 12 additions and 42 deletions

View File

@@ -1,5 +1,9 @@
# 更新日志
4.3.4 - 2025-09-10
---
- CHANGE: 改进嵌套加速实现, 增强稳定性
4.3.3 - 2025-09-10
---
- CHANGE: 增强对[wanf](https://github.com/WJQSERVER/wanf)的支持

View File

@@ -127,18 +127,14 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
defer bodyReader.Close()
if MatcherShell(u) && matchString(matcher) && cfg.Shell.Editor {
// 判断body是不是gzip
var compress string
if resp.Header.Get("Content-Encoding") == "gzip" {
compress = "gzip"
}
c.Debugf("Use Shell Editor: %s %s %s %s %s", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto)
c.Header("Content-Length", "")
c.DelHeader("Content-Length")
c.DelHeader("Content-Encoding")
var reader io.Reader
reader, _, err = processLinks(bodyReader, compress, c.Request.Host, cfg, c)
reader, _, err = processLinks(bodyReader, c.Request.Host, cfg, c)
c.WriteStream(reader)
if err != nil {
c.Errorf("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto, err)
@@ -146,7 +142,6 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
return
}
} else {
if contentLength != "" {
c.SetHeader("Content-Length", contentLength)
c.WriteStream(bodyReader)

View File

@@ -2,7 +2,6 @@ package proxy
import (
"bufio"
"compress/gzip"
"fmt"
"ghproxy/config"
"io"
@@ -66,7 +65,7 @@ func modifyURL(url string, host string, cfg *config.Config) string {
}
// processLinks 处理链接,返回包含处理后数据的 io.Reader
func processLinks(input io.ReadCloser, compress string, host string, cfg *config.Config, c *touka.Context) (readerOut io.Reader, written int64, err error) {
func processLinks(input io.ReadCloser, host string, cfg *config.Config, c *touka.Context) (readerOut io.Reader, written int64, err error) {
pipeReader, pipeWriter := io.Pipe() // 创建 io.Pipe
readerOut = pipeReader
@@ -97,43 +96,14 @@ func processLinks(input io.ReadCloser, compress string, host string, cfg *config
var bufReader *bufio.Reader
if compress == "gzip" {
// 解压gzip
gzipReader, gzipErr := gzip.NewReader(input)
if gzipErr != nil {
err = fmt.Errorf("gzip解压错误: %v", gzipErr)
return // Goroutine 中使用 return 返回错误
}
defer gzipReader.Close()
bufReader = bufio.NewReader(gzipReader)
} else {
bufReader = bufio.NewReader(input)
}
bufReader = bufio.NewReader(input)
var bufWriter *bufio.Writer
var gzipWriter *gzip.Writer
// 根据是否gzip确定 writer 的创建
if compress == "gzip" {
gzipWriter = gzip.NewWriter(pipeWriter) // 使用 pipeWriter
bufWriter = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小
} else {
bufWriter = bufio.NewWriterSize(pipeWriter, 4096) // 使用 pipeWriter
}
bufWriter = bufio.NewWriterSize(pipeWriter, 4096) // 使用 pipeWriter
//确保writer关闭
defer func() {
var closeErr error // 局部变量用于保存defer中可能发生的错误
if gzipWriter != nil {
if closeErr = gzipWriter.Close(); closeErr != nil {
c.Errorf("gzipWriter close failed %v", closeErr)
// 如果已经存在错误,则保留。否则,记录此错误。
if err == nil {
err = closeErr
}
}
}
if flushErr := bufWriter.Flush(); flushErr != nil {
c.Errorf("writer flush failed %v", flushErr)
// 如果已经存在错误,则保留。否则,记录此错误。

View File

@@ -27,6 +27,7 @@ var (
"CDN-Loop": {},
"Upgrade": {},
"Connection": {},
"Accept-Encoding": {},
}
cloneHeadersToRemove = map[string]struct{}{
@@ -43,7 +44,7 @@ var (
var (
defaultHeaders = map[string]string{
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Accept-Encoding": "",
"Transfer-Encoding": "chunked",
"User-Agent": "GHProxy/1.0",
}