Compare commits
13 Commits
4.3.0
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5dee96358 | ||
|
|
32baca85db | ||
|
|
0135fd2ce0 | ||
|
|
ba33d5743f | ||
|
|
bd9f590b0a | ||
|
|
93cabc900a | ||
|
|
e3f84f4c17 | ||
|
|
4a7ad2ec75 | ||
|
|
efb63927e9 | ||
|
|
a285777217 | ||
|
|
44cc5d5677 | ||
|
|
7972931280 | ||
|
|
74a22be16c |
2
.github/workflows/build-dev.yml
vendored
2
.github/workflows/build-dev.yml
vendored
@@ -46,7 +46,7 @@ jobs:
|
||||
goarch: [amd64, arm64]
|
||||
env:
|
||||
OUTPUT_BINARY: ghproxy
|
||||
GO_VERSION: 1.24
|
||||
GO_VERSION: 1.25
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -47,7 +47,7 @@ jobs:
|
||||
goarch: [amd64, arm64]
|
||||
env:
|
||||
OUTPUT_BINARY: ghproxy
|
||||
GO_VERSION: 1.24
|
||||
GO_VERSION: 1.25
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
17
CHANGELOG.md
17
CHANGELOG.md
@@ -1,5 +1,22 @@
|
||||
# 更新日志
|
||||
|
||||
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)
|
||||
|
||||
4.3.0 - 2025-08-11
|
||||
---
|
||||
- CHANGE: 为OCI镜像(Docker)代理带来自动library附加功能
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 返回默认配置结构体
|
||||
|
||||
10
go.mod
10
go.mod
@@ -1,12 +1,12 @@
|
||||
module ghproxy
|
||||
|
||||
go 1.24.6
|
||||
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.47.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-20250725192818-e39067aee2d2
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
16
go.sum
16
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-20250725192818-e39067aee2d2 h1:iizUGZ9pEquQS5jTGkh4AqeeHCMbfbjeb0zMt0aEFzs=
|
||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2/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.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
||||
golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI=
|
||||
golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||
|
||||
14
main.go
14
main.go
@@ -234,8 +234,18 @@ func setupPages(cfg *config.Config, r *touka.Engine) {
|
||||
}
|
||||
|
||||
case "external":
|
||||
r.SetUnMatchFS(http.Dir(cfg.Pages.StaticDir))
|
||||
|
||||
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))
|
||||
}
|
||||
default:
|
||||
// 处理无效的Pages Mode
|
||||
logger.Warnf("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||
|
||||
@@ -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",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user