diff --git a/CHANGELOG.md b/CHANGELOG.md index 77c5a26..fb222e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,18 +1,5 @@ # 更新日志 -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`为空时的处置 - 4.3.1 - 2025-08-13 --- - CHANGE: 更新至[Go 1.25](https://tip.golang.org/doc/go1.25) diff --git a/README.md b/README.md index c977c51..cc0847e 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ [相关文章](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 a6695ff..ecedc98 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.3.4 \ No newline at end of file +4.3.1 \ No newline at end of file diff --git a/config/config.go b/config/config.go index e098e72..6a6b039 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,8 @@ package config import ( - "fmt" "os" "path/filepath" - "strings" "github.com/BurntSushi/toml" @@ -214,8 +212,7 @@ type DockerConfig struct { // LoadConfig 从配置文件加载配置 func LoadConfig(filePath string) (*Config, error) { - exist, filePath2read := FileExists(filePath) - if !exist { + if !FileExists(filePath) { // 楔入配置文件 err := DefaultConfig().WriteConfig(filePath) if err != nil { @@ -224,15 +221,15 @@ func LoadConfig(filePath string) (*Config, error) { return DefaultConfig(), nil } var config Config - ext := filepath.Ext(filePath2read) + ext := filepath.Ext(filePath) if ext == ".wanf" { - if err := wanf.DecodeFile(filePath2read, &config); err != nil { + if err := wanf.DecodeFile(filePath, &config); err != nil { return nil, err } return &config, nil } - if _, err := toml.DecodeFile(filePath2read, &config); err != nil { + if _, err := toml.DecodeFile(filePath, &config); err != nil { return nil, err } return &config, nil @@ -260,37 +257,9 @@ func (c *Config) WriteConfig(filePath string) error { } // FileExists 检测文件是否存在 -func FileExists(filename string) (bool, string) { +func FileExists(filename string) bool { _, err := os.Stat(filename) - 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 - } + return !os.IsNotExist(err) } // DefaultConfig 返回默认配置结构体 diff --git a/go.mod b/go.mod index d43cb00..b1b88a6 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ module ghproxy -go 1.25.1 +go 1.25 require ( github.com/BurntSushi/toml v1.5.0 github.com/WJQSERVER-STUDIO/httpc v0.8.2 - golang.org/x/net v0.44.0 - golang.org/x/time v0.13.0 + golang.org/x/net v0.43.0 + golang.org/x/time v0.12.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-20250813233538-9b1f9ea2e11b + github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced github.com/hashicorp/golang-lru/v2 v2.0.7 - github.com/infinite-iroha/touka v0.3.7 + github.com/infinite-iroha/touka v0.3.6 github.com/wjqserver/modembed v0.0.1 ) diff --git a/go.sum b/go.sum index 97337f6..a9c3c71 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-20250813233538-9b1f9ea2e11b h1:6Q4zRHXS/YLOl9Ng1b1OOOBWMidAQZR3Gel0UKPC/KU= -github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= +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/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.7 h1:bIIZW5Weh7lVpyOWh4FmyR9UOfb5FOt+cR9yQ30FJLA= -github.com/infinite-iroha/touka v0.3.7/go.mod h1:uwkF1gTrNEgQ4P/Gwtk6WLbERehq3lzB8x1FMedyrfE= +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/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.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= +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= diff --git a/main.go b/main.go index 5f5b3ed..48c10fe 100644 --- a/main.go +++ b/main.go @@ -234,18 +234,8 @@ func setupPages(cfg *config.Config, r *touka.Engine) { } case "external": - if cfg.Pages.StaticDir == "" { - logger.Errorf("Pages Mode is 'external' but StaticDir is empty. Using embedded pages instead.") - err := setInternalRoute(cfg, r) - if err != nil { - logger.Errorf("Failed to load embedded pages: %s", err) - fmt.Printf("Failed to load embedded pages: %s", err) - os.Exit(1) - } - } else { - extPageFS := os.DirFS(cfg.Pages.StaticDir) - r.SetUnMatchFS(http.FS(extPageFS)) - } + r.SetUnMatchFS(http.Dir(cfg.Pages.StaticDir)) + default: // 处理无效的Pages Mode logger.Warnf("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode) diff --git a/proxy/chunkreq.go b/proxy/chunkreq.go index 7e3725e..9227b78 100644 --- a/proxy/chunkreq.go +++ b/proxy/chunkreq.go @@ -127,14 +127,18 @@ 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.DelHeader("Content-Length") - c.DelHeader("Content-Encoding") + c.Header("Content-Length", "") var reader io.Reader - reader, _, err = processLinks(bodyReader, c.Request.Host, cfg, c) + reader, _, err = processLinks(bodyReader, compress, 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) @@ -142,6 +146,7 @@ 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 675e4a3..4f93f20 100644 --- a/proxy/nest.go +++ b/proxy/nest.go @@ -2,6 +2,7 @@ package proxy import ( "bufio" + "compress/gzip" "fmt" "ghproxy/config" "io" @@ -65,7 +66,7 @@ func modifyURL(url string, host string, cfg *config.Config) string { } // processLinks 处理链接,返回包含处理后数据的 io.Reader -func processLinks(input io.ReadCloser, host string, cfg *config.Config, c *touka.Context) (readerOut io.Reader, written int64, err error) { +func processLinks(input io.ReadCloser, compress string, host string, cfg *config.Config, c *touka.Context) (readerOut io.Reader, written int64, err error) { pipeReader, pipeWriter := io.Pipe() // 创建 io.Pipe readerOut = pipeReader @@ -96,14 +97,43 @@ func processLinks(input io.ReadCloser, host string, cfg *config.Config, c *touka var bufReader *bufio.Reader - bufReader = bufio.NewReader(input) + 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) + } var bufWriter *bufio.Writer + var gzipWriter *gzip.Writer - bufWriter = bufio.NewWriterSize(pipeWriter, 4096) // 使用 pipeWriter + // 根据是否gzip确定 writer 的创建 + if compress == "gzip" { + gzipWriter = gzip.NewWriter(pipeWriter) // 使用 pipeWriter + bufWriter = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小 + } else { + 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 57d8542..c89dc76 100644 --- a/proxy/reqheader.go +++ b/proxy/reqheader.go @@ -27,7 +27,6 @@ var ( "CDN-Loop": {}, "Upgrade": {}, "Connection": {}, - "Accept-Encoding": {}, } cloneHeadersToRemove = map[string]struct{}{ @@ -44,7 +43,7 @@ var ( var ( defaultHeaders = map[string]string{ "Accept": "*/*", - "Accept-Encoding": "", + "Accept-Encoding": "gzip", "Transfer-Encoding": "chunked", "User-Agent": "GHProxy/1.0", }