diff --git a/CHANGELOG.md b/CHANGELOG.md index c46007d..77c5a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # 更新日志 +4.3.4 - 2025-09-14 +--- +- CHANGE: 改进嵌套加速实现, 增强稳定性 + +4.3.3 - 2025-09-10 +--- +- CHANGE: 增强对[wanf](https://github.com/WJQSERVER/wanf)的支持 +- CHANGE: 更新包括Touka框架在内的各个依赖版本 + 4.3.2 - 2025-08-20 --- - FIX: 修正`cfg.Pages.StaticDir`为空时的处置 diff --git a/README.md b/README.md index cc0847e..c977c51 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,6 @@ [相关文章](https://blog.wjqserver.com/categories/my-program/) -代理相关推广: [Thordata](https://www.thordata.com/?ls=github&lk=WJQserver),市面上最具性价比的代理服务商,便宜好用,来自全球195个国家城市的6000万IP,轮换住宅/原生ISP/无限量仅从$0.65/GB 起,新用户$1=5GB .联系客户可获得免费测试. - ### 使用示例 ```bash diff --git a/VERSION b/VERSION index 7e961f9..a6695ff 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.3.2 \ No newline at end of file +4.3.4 \ No newline at end of file diff --git a/config/config.go b/config/config.go index 6a6b039..e098e72 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,10 @@ package config import ( + "fmt" "os" "path/filepath" + "strings" "github.com/BurntSushi/toml" @@ -212,7 +214,8 @@ type DockerConfig struct { // LoadConfig 从配置文件加载配置 func LoadConfig(filePath string) (*Config, error) { - if !FileExists(filePath) { + exist, filePath2read := FileExists(filePath) + if !exist { // 楔入配置文件 err := DefaultConfig().WriteConfig(filePath) if err != nil { @@ -221,15 +224,15 @@ func LoadConfig(filePath string) (*Config, error) { return DefaultConfig(), nil } var config Config - ext := filepath.Ext(filePath) + ext := filepath.Ext(filePath2read) if ext == ".wanf" { - if err := wanf.DecodeFile(filePath, &config); err != nil { + if err := wanf.DecodeFile(filePath2read, &config); err != nil { return nil, err } return &config, nil } - if _, err := toml.DecodeFile(filePath, &config); err != nil { + if _, err := toml.DecodeFile(filePath2read, &config); err != nil { return nil, err } return &config, nil @@ -257,9 +260,37 @@ func (c *Config) WriteConfig(filePath string) error { } // FileExists 检测文件是否存在 -func FileExists(filename string) bool { +func FileExists(filename string) (bool, string) { _, err := os.Stat(filename) - return !os.IsNotExist(err) + if err == nil { + return true, filename + } + if os.IsNotExist(err) { + // 获取文件名(不包含路径) + base := filepath.Base(filename) + dir := filepath.Dir(filename) + + // 获取扩展名 + fileNameBody := strings.TrimSuffix(base, filepath.Ext(base)) + + // 重新组合路径, 扩展名改为.wanf, 确认是否存在 + wanfFilename := filepath.Join(dir, fileNameBody+".wanf") + + _, err = os.Stat(wanfFilename) + if err == nil { + // .wanf 文件存在 + fmt.Printf("\n Found .wanf file: %s\n", wanfFilename) + return true, wanfFilename + } else if os.IsNotExist(err) { + // .wanf 文件不存在 + return false, "" + } else { + // 其他错误 + return false, "" + } + } else { + return false, filename + } } // DefaultConfig 返回默认配置结构体 diff --git a/go.mod b/go.mod index b1b88a6..d43cb00 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ module ghproxy -go 1.25 +go 1.25.1 require ( github.com/BurntSushi/toml v1.5.0 github.com/WJQSERVER-STUDIO/httpc v0.8.2 - golang.org/x/net v0.43.0 - golang.org/x/time v0.12.0 + golang.org/x/net v0.44.0 + golang.org/x/time v0.13.0 ) require ( @@ -18,9 +18,9 @@ require ( github.com/fenthope/ipfilter v0.0.1 github.com/fenthope/reco v0.0.4 github.com/fenthope/record v0.0.4 - github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced + github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b github.com/hashicorp/golang-lru/v2 v2.0.7 - github.com/infinite-iroha/touka v0.3.6 + github.com/infinite-iroha/touka v0.3.7 github.com/wjqserver/modembed v0.0.1 ) diff --git a/go.sum b/go.sum index a9c3c71..97337f6 100644 --- a/go.sum +++ b/go.sum @@ -18,17 +18,17 @@ github.com/fenthope/reco v0.0.4 h1:yo2g3aWwdoMpaZWZX4SdZOW7mCK82RQIU/YI8ZUQThM= github.com/fenthope/reco v0.0.4/go.mod h1:eMyS8HpdMVdJ/2WJt6Cvt8P1EH9Igzj5lSJrgc+0jeg= github.com/fenthope/record v0.0.4 h1:/1JHNCxiXGLL/qCh4LEGaAvhj4CcKsb6siTxjLmjdO4= github.com/fenthope/record v0.0.4/go.mod h1:G0a6KCiCDyX2SsC3nfzSN651fJKxH482AyJvzlnvAJU= -github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced h1:Q311OHjMh/u5E2TITc++WlTP5We0xNseRMkHDyvhW7I= -github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= +github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b h1:6Q4zRHXS/YLOl9Ng1b1OOOBWMidAQZR3Gel0UKPC/KU= +github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= -github.com/infinite-iroha/touka v0.3.6 h1:SkpM/VFGCWOFQP3RRuoWdX/Q4zafPngG1VMwkrLwtkw= -github.com/infinite-iroha/touka v0.3.6/go.mod h1:XW7a3fpLAjJfylSmdNuDQ8wGKkKmLVi9V/89sT1d7uw= +github.com/infinite-iroha/touka v0.3.7 h1:bIIZW5Weh7lVpyOWh4FmyR9UOfb5FOt+cR9yQ30FJLA= +github.com/infinite-iroha/touka v0.3.7/go.mod h1:uwkF1gTrNEgQ4P/Gwtk6WLbERehq3lzB8x1FMedyrfE= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs= github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ= -golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE= -golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg= -golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= -golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= +golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I= +golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= +golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI= +golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= diff --git a/proxy/chunkreq.go b/proxy/chunkreq.go index 9227b78..7e3725e 100644 --- a/proxy/chunkreq.go +++ b/proxy/chunkreq.go @@ -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) diff --git a/proxy/nest.go b/proxy/nest.go index 4f93f20..675e4a3 100644 --- a/proxy/nest.go +++ b/proxy/nest.go @@ -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) // 如果已经存在错误,则保留。否则,记录此错误。 diff --git a/proxy/reqheader.go b/proxy/reqheader.go index c89dc76..57d8542 100644 --- a/proxy/reqheader.go +++ b/proxy/reqheader.go @@ -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", }