Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
460b7514a9 | ||
|
|
c90140a898 | ||
|
|
f7e4fe71d7 | ||
|
|
33973b786d |
31
CHANGELOG.md
31
CHANGELOG.md
@@ -1,5 +1,36 @@
|
||||
# 更新日志
|
||||
|
||||
2.0.3
|
||||
---
|
||||
- RELEASE: v2.0.3正式版发布;
|
||||
- CHANGE: 优化`HTTP Client`参数, 使其更符合本项目使用场景
|
||||
|
||||
25w07b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.0.3的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 改进`HTTP Client`参数
|
||||
|
||||
25w07a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.0.3的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 为`HTTP Client`增加复用, 对性能有所优化
|
||||
- CHANGE: 优化`HTTP Client`参数, 使其更符合本项目使用场景
|
||||
|
||||
2.0.2
|
||||
---
|
||||
- RELEASE: v2.0.2正式版发布; 此版本是v2.0.1改进
|
||||
- CHANGE: 由于用户使用了不符合`RFC 9113`规范的请求头, 导致`ghproxy`无法正常工作, 在此版本为用户的错误行为提供补丁;
|
||||
|
||||
25w06b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是改进验证版本,普通用户请勿使用;
|
||||
- CHANGE: 由于用户使用了不符合`RFC 9113`规范的请求头, 导致`ghproxy`无法正常工作, 在此版本为用户的错误行为提供补丁;
|
||||
|
||||
25w06a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是改进验证版本,普通用户请勿使用;
|
||||
- CHANGE: Remove `Conection: Upgrade` header, which is not currently supported by some web server configurations.
|
||||
|
||||
v2.0.1
|
||||
---
|
||||
- RELEASE: v2.0.1正式版发布; 此版本是v2.0.0的小修复版本, 主要修复了Docker启动脚本存在的一些问题
|
||||
|
||||
@@ -1 +1 @@
|
||||
25w05a
|
||||
25w07b
|
||||
24
README.md
24
README.md
@@ -15,14 +15,12 @@
|
||||
|
||||
### 项目特点
|
||||
|
||||
- 基于Go语言实现,使用[Gin框架](https://github.com/gin-gonic/gin)与[req库](https://github.com/imroc/req)]
|
||||
- 基于Go语言实现,使用[Gin框架](https://github.com/gin-gonic/gin)
|
||||
- 支持Git clone,raw,realeases等文件拉取
|
||||
- 支持Docker部署
|
||||
- 支持速率限制
|
||||
- 支持用户鉴权
|
||||
- 支持自定义黑名单/白名单
|
||||
- 符合[RFC 7234](https://httpwg.org/specs/rfc7234.html)的HTTP Cache
|
||||
- 使用Caddy作为Web Server
|
||||
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
||||
|
||||
### 项目开发过程
|
||||
@@ -30,6 +28,7 @@
|
||||
**本项目是[WJQSERVER-STUDIO/ghproxy-go](https://github.com/WJQSERVER-STUDIO/ghproxy-go)的重构版本,实现了原项目原定功能的同时,进一步优化了性能**
|
||||
关于此项目的详细开发过程,请参看Commit记录与[CHANGELOG.md](https://github.com/WJQSERVER-STUDIO/ghproxy/blob/main/CHANGELOG.md)
|
||||
|
||||
- V2.0.0 对`proxy`核心模块进行了重构,大幅优化内存占用
|
||||
- V1.0.0 迁移至本仓库,并再次重构内容实现
|
||||
- v0.2.0 重构项目实现
|
||||
|
||||
@@ -42,8 +41,10 @@
|
||||
## 使用示例
|
||||
|
||||
```
|
||||
# 下载文件
|
||||
https://ghproxy.1888866.xyz/raw.githubusercontent.com/WJQSERVER-STUDIO/tools-stable/main/tools-stable-ghproxy.sh
|
||||
|
||||
# 克隆仓库
|
||||
git clone https://ghproxy.1888866.xyz/github.com/WJQSERVER-STUDIO/ghproxy.git
|
||||
```
|
||||
|
||||
@@ -54,7 +55,7 @@ git clone https://ghproxy.1888866.xyz/github.com/WJQSERVER-STUDIO/ghproxy.git
|
||||
- Docker-cli
|
||||
|
||||
```
|
||||
docker run -p 7210:80 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/caddy:/data/caddy/log -v ./ghproxy/config:/data/ghproxy/config --restart always wjqserver/ghproxy
|
||||
docker run -p 7210:8080 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/caddy:/data/caddy/log -v ./ghproxy/config:/data/ghproxy/config --restart always wjqserver/ghproxy
|
||||
```
|
||||
|
||||
- Docker-Compose (建议使用)
|
||||
@@ -78,7 +79,7 @@ wget -O install.sh https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/ma
|
||||
|
||||
```toml
|
||||
[server]
|
||||
host = "127.0.0.1" # 监听地址
|
||||
host = "0.0.0.0" # 监听地址
|
||||
port = 8080 # 监听端口
|
||||
sizeLimit = 125 # 125MB
|
||||
bufferSize = 4096 # Bytes 缓冲区大小
|
||||
@@ -110,7 +111,7 @@ whitelistFile = "/data/ghproxy/config/whitelist.json" # 白名单文件路径
|
||||
|
||||
[rateLimit]
|
||||
enabled = false # 是否开启速率限制
|
||||
rateMrthod = "total" # "ip" or "total" 速率限制方式
|
||||
rateMethod = "total" # "ip" or "total" 速率限制方式
|
||||
ratePerMinute = 180 # 每分钟限制请求数量
|
||||
burst = 5 # 突发请求数量
|
||||
```
|
||||
@@ -149,10 +150,6 @@ burst = 5 # 突发请求数量
|
||||
example.com {
|
||||
reverse_proxy {
|
||||
to 127.0.0.1:7210
|
||||
header_up X-Real-IP {remote_host}
|
||||
header_up X-Real-IP {http.request.header.CF-Connecting-IP}
|
||||
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
|
||||
header_up X-Forwarded-Proto {http.request.header.CF-Visitor}
|
||||
}
|
||||
encode zstd gzip
|
||||
}
|
||||
@@ -162,10 +159,3 @@ example.com {
|
||||
|
||||

|
||||

|
||||
|
||||
结语
|
||||
---
|
||||
|
||||
本项目基于Go语言实现,使用Gin框架与req库
|
||||
Docker镜像基于[WJQSERVER-STUDIO/caddy](https://github.com/WJQSERVER-STUDIO/caddy)
|
||||
本项目使用WSL LICENSE Version1.2 (WJQSERVER STUDIO LICENSE Version1.2) 授权协议,请遵守相关条例。
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[server]
|
||||
host = "127.0.0.1"
|
||||
host = "0.0.0.0"
|
||||
port = 8080
|
||||
sizeLimit = 125 # MB
|
||||
enableH2C = "on" # "on" or "off"
|
||||
|
||||
@@ -3,15 +3,9 @@ services:
|
||||
ghproxy:
|
||||
image: 'wjqserver/ghproxy:latest'
|
||||
restart: always
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://127.0.0.1:80/api/healthcheck"]
|
||||
interval: 60s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
volumes:
|
||||
- './ghproxy/log/run:/data/ghproxy/log'
|
||||
- './ghproxy/log/caddy:/data/caddy/log'
|
||||
- './ghproxy/config:/data/ghproxy/config'
|
||||
ports:
|
||||
- '7210:80'
|
||||
- '7210:8080'
|
||||
|
||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.23.5
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.4.0
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.0
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.1
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
golang.org/x/time v0.9.0
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,7 +1,7 @@
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.0 h1:OUrAOWb8xK0kxpWextJYUasmol+5KKqG2az52X2ae64=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.0/go.mod h1:sAqHVYSucoUnycyHMAZc1fMH5dS2bQwgwo8NUiAIcyk=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.1 h1:YS3q54SroxQpEM7c12ZKjLNAaSq++bNpxTujs9cTZ9c=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.1.1/go.mod h1:oW884JCCPDU6c906LI0uKXndWLiRvjb9LkGYC2cqRO8=
|
||||
github.com/bytedance/sonic v1.12.7 h1:CQU8pxOy9HToxhndH0Kx/S1qU/CuS9GnKYrGioDcU1Q=
|
||||
github.com/bytedance/sonic v1.12.7/go.mod h1:tnbal4mxOMju17EGfknm2XyYcpyCnIROYOEYuemj13I=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
|
||||
6
main.go
6
main.go
@@ -91,8 +91,8 @@ func setupRateLimit(cfg *config.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
func initBufferSize() {
|
||||
proxy.InitChunkedBufferSize(cfg.Server.BufferSize)
|
||||
func InitChunkedReq() {
|
||||
proxy.InitChunkedReq(cfg.Server.BufferSize)
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -100,7 +100,7 @@ func init() {
|
||||
flag.Parse()
|
||||
loadConfig()
|
||||
setupLogger(cfg)
|
||||
initBufferSize()
|
||||
InitChunkedReq()
|
||||
loadlist(cfg)
|
||||
setupRateLimit(cfg)
|
||||
|
||||
|
||||
@@ -6,13 +6,24 @@ import (
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var chunkedBufferSize int
|
||||
|
||||
func InitChunkedBufferSize(cfgBufferSize int) {
|
||||
var (
|
||||
client *http.Client
|
||||
tr *http.Transport
|
||||
)
|
||||
|
||||
func InitChunkedReq(cfgBufferSize int) {
|
||||
initChunkedBufferSize(cfgBufferSize)
|
||||
initChunkedHTTPClient()
|
||||
}
|
||||
|
||||
func initChunkedBufferSize(cfgBufferSize int) {
|
||||
if cfgBufferSize == 0 {
|
||||
chunkedBufferSize = 4096 // 默认缓冲区大小
|
||||
} else {
|
||||
@@ -20,12 +31,23 @@ func InitChunkedBufferSize(cfgBufferSize int) {
|
||||
}
|
||||
}
|
||||
|
||||
func initChunkedHTTPClient() {
|
||||
tr = &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxConnsPerHost: 60,
|
||||
IdleConnTimeout: 20 * time.Second,
|
||||
}
|
||||
client = &http.Client{
|
||||
Transport: tr,
|
||||
}
|
||||
}
|
||||
|
||||
func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
|
||||
method := c.Request.Method
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
|
||||
// 创建HTTP客户端
|
||||
client := &http.Client{}
|
||||
//client := &http.Client{}
|
||||
|
||||
// 发送HEAD请求, 预获取Content-Length
|
||||
headReq, err := http.NewRequest("HEAD", u, nil)
|
||||
@@ -34,6 +56,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
||||
return
|
||||
}
|
||||
setRequestHeaders(c, headReq)
|
||||
removeWSHeader(headReq) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
||||
AuthPassThrough(c, cfg, headReq)
|
||||
|
||||
headResp, err := client.Do(headReq)
|
||||
@@ -65,6 +88,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
||||
|
||||
req.Header.Set("Transfer-Encoding", "chunked") // 确保设置分块传输编码
|
||||
setRequestHeaders(c, req)
|
||||
removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
||||
AuthPassThrough(c, cfg, req)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
@@ -82,6 +106,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
||||
CopyResponseHeaders(resp, c, cfg)
|
||||
|
||||
c.Status(resp.StatusCode)
|
||||
|
||||
if err := chunkedCopyResponseBody(c, resp.Body); err != nil {
|
||||
logError("%s %s %s %s %s 响应复制错误: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
||||
}
|
||||
|
||||
@@ -14,3 +14,8 @@ func setRequestHeaders(c *gin.Context, req *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeWSHeader(req *http.Request) {
|
||||
req.Header.Del("Upgrade")
|
||||
req.Header.Del("Connection")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user