Compare commits

...

10 Commits

Author SHA1 Message Date
WJQSERVER
97ae0044e7 Merge pull request #50 from WJQSERVER-STUDIO/dev
2.2.0
2025-02-12 19:30:46 +08:00
里見 灯花
b95582ae1a fix wrong transport 2025-02-12 19:14:13 +08:00
WJQSERVER
9aaa3e64d3 2.2.0 2025-02-12 18:45:13 +08:00
WJQSERVER
9e0f222125 25w13b 2025-02-12 18:26:58 +08:00
WJQSERVER
21d30dee53 update deploy bash 2025-02-10 23:25:16 +08:00
WJQSERVER
a061b8d369 fix deploy/config.toml 2025-02-10 19:10:58 +08:00
WJQSERVER
68346717a5 25w13a 2025-02-10 18:54:20 +08:00
WJQSERVER
2b7fbd2a0d update outbound 2025-02-10 18:53:16 +08:00
三千
4c5d288f03 Support using proxy dial-up connection to GitHub. (#46) 2025-02-10 00:45:37 +08:00
WJQSERVER
09163ed4df 2.1.0 (#47)
2.1.0
---
- RELEASE: v2.1.0正式版发布;
- CHANGE: 加入`FreeBSD`与`Darwin`系统支持
- CHANGE: 更新安全政策, v1和24w版本序列生命周期正式结束
- ADD: 加入`timing`中间件记录响应时间
- ADD: 加入`loggin`中间件包装日志输出
- CHANGE: 更新logger版本至v1.3.0
- CHANGE: 改进日志相关
- ADD: 加入日志等级配置项
2025-02-09 23:13:57 +08:00
29 changed files with 575 additions and 125 deletions

View File

@@ -9,15 +9,44 @@ on:
- 'DEV-VERSION'
jobs:
prepare:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 加载版本号
run: |
if [ -f DEV-VERSION ]; then
echo "VERSION=$(cat DEV-VERSION)" >> $GITHUB_ENV
else
echo "DEV-VERSION file not found!" && exit 1
fi
- name: 输出版本号
run: |
echo "Version: ${{ env.VERSION }}"
- name: 预先创建Pre-release
id: create_release
uses: ncipollo/release-action@v1
with:
name: ${{ env.VERSION }}
artifacts: ./DEV-VERSION
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ env.VERSION }}
allowUpdates: true
prerelease: true
body: ${{ env.VERSION }}
env:
export PATH: $PATH:/usr/local/go/bin
build:
runs-on: ubuntu-latest
needs: prepare
strategy:
matrix:
goos: [linux]
goos: [linux, darwin, freebsd]
goarch: [amd64, arm64]
env:
OUTPUT_BINARY: ghproxy
GO_VERSION: 1.23.6
GO_VERSION: 1.24
steps:
- uses: actions/checkout@v4
@@ -64,6 +93,7 @@ jobs:
tag: ${{ env.VERSION }}
allowUpdates: true
prerelease: true
body: ${{ env.VERSION }}
env:
export PATH: $PATH:/usr/local/go/bin

View File

@@ -9,15 +9,43 @@ on:
- 'VERSION'
jobs:
prepare:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 加载版本号
run: |
if [ -f VERSION ]; then
echo "VERSION=$(cat VERSION)" >> $GITHUB_ENV
else
echo "VERSION file not found!" && exit 1
fi
- name: 输出版本号
run: |
echo "Version: ${{ env.VERSION }}"
- name: 预先创建release
id: create_release
uses: ncipollo/release-action@v1
with:
name: ${{ env.VERSION }}
artifacts: ./VERSION
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ env.VERSION }}
allowUpdates: true
body: ${{ env.VERSION }}
env:
export PATH: $PATH:/usr/local/go/bin
build:
runs-on: ubuntu-latest
needs: prepare # 确保这个作业在 prepare 作业完成后运行
strategy:
matrix:
goos: [linux]
goos: [linux, darwin, freebsd]
goarch: [amd64, arm64]
env:
OUTPUT_BINARY: ghproxy
GO_VERSION: 1.23.6
GO_VERSION: 1.24
steps:
- uses: actions/checkout@v3
@@ -60,6 +88,7 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ env.VERSION }}
allowUpdates: true
body: ${{ env.VERSION }}
env:
export PATH: $PATH:/usr/local/go/bin

View File

@@ -1,5 +1,58 @@
# 更新日志
2.2.0
---
- RELEASE: v2.2.0正式版发布;
- CHANGE: 更新Go版本至1.24.0
- ADD: 加入`Socks5``HTTP(S)`出站支持
- CHANGE: 配置新增`Outbound`配置块
25w13b
---
- PRE-RELEASE: 此版本是v2.2.0的预发布版本,请勿在生产环境中使用;
- CHANGE: 更新Go版本至1.24.0
25w13a
---
- PRE-RELEASE: 此版本是v2.2.0的预发布版本,请勿在生产环境中使用;
- ADD: 加入`Socks5``HTTP(S)`出站支持
- CHANGE: 配置新增`Outbound`配置块
2.1.0
---
- RELEASE: v2.1.0正式版发布;
- CHANGE: 加入`FreeBSD``Darwin`系统支持
- CHANGE: 更新安全政策, v1和24w版本序列生命周期正式结束
- ADD: 加入`timing`中间件记录响应时间
- ADD: 加入`loggin`中间件包装日志输出
- CHANGE: 更新logger版本至v1.3.0
- CHANGE: 改进日志相关
- ADD: 加入日志等级配置项
25w12d
---
- PRE-RELEASE: 此版本是v2.1.0的预发布版本,请勿在生产环境中使用;
- CHANGE: 处理类型断言相关问题
25w12c
---
- PRE-RELEASE: 此版本是v2.1.0的预发布版本,请勿在生产环境中使用;
- CHANGE: 加入`FreeBSD``Darwin`系统支持
25w12b
---
- PRE-RELEASE: 此版本是v2.0.8/v2.1.0的预发布版本,请勿在生产环境中使用;
- ADD: 加入`timing`中间件记录响应时间
- ADD: 加入`loggin`中间件包装日志输出
- CHANGE: 更新安全政策, v1和24w版本序列生命周期正式结束
25w12a
---
- PRE-RELEASE: 此版本是v2.0.8/v2.1.0的预发布版本,请勿在生产环境中使用;
- CHANGE: 更新logger版本至v1.3.0
- CHANGE: 改进日志相关
- ADD: 加入日志等级配置项
2.0.7
---
- RELEASE: v2.0.7正式版发布;

View File

@@ -1 +1 @@
25w11a
25w13b

View File

@@ -71,6 +71,12 @@ docker run -p 7210:8080 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/
wget -O install.sh https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/install.sh && chmod +x install.sh &&./install.sh
```
Dev一键部署脚本:
```bash
wget -O install-dev.sh https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/deploy/install-dev.sh && chmod +x install-dev.sh && ./install-dev.sh
```
## 配置说明
### 外部配置文件
@@ -92,6 +98,7 @@ staticPath = "/data/www" # 静态页面文件路径
[log]
logFilePath = "/data/ghproxy/log/ghproxy.log" # 日志文件路径
maxLogSize = 5 # MB 日志文件最大大小
level = "info" # 日志级别 dump, debug, info, warn, error, none
[cors]
enabled = true # 是否开启跨域
@@ -114,6 +121,10 @@ enabled = false # 是否开启速率限制
rateMethod = "total" # "ip" or "total" 速率限制方式
ratePerMinute = 180 # 每分钟限制请求数量
burst = 5 # 突发请求数量
[outbound]
enabled = false # 是否使用自定义代理出站
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890" 支持Socks5/HTTP(S)出站传输
```
### 黑名单配置
@@ -159,3 +170,15 @@ example.com {
![ghproxy-demo.png](https://webp.wjqserver.com/ghproxy/1.8.1-light.png)
![ghproxy-demo-dark.png](https://webp.wjqserver.com/ghproxy/1.8.1-dark.png)
## 赞助
如果您觉得本项目对您有帮助,欢迎赞助支持,您的赞助将用于Demo服务器开支及开发者时间成本支出,感谢您的支持!
为爱发电,开源不易
爱发电: https://afdian.com/a/wjqserver
### 捐赠列表
虚位以待...

View File

@@ -6,8 +6,10 @@
| 版本 | 是否支持 |
| --- | --- |
| v1.x.x | :white_check_mark: |
| 24w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 |
| v2.x.x | :white_check_mark: 当前最新版本序列, 受支持 |
| v1.x.x | :x: 这些版本已结束生命周期,不再受支持 |
| 25w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 |
| 24w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 生命周期已完全结束 |
| v0.x.x | :x: 这些版本不再受支持 |
### 用户须知

View File

@@ -1 +1 @@
2.0.7
2.2.0

View File

@@ -15,6 +15,8 @@ var (
var (
logw = logger.Logw
LogDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError

View File

@@ -13,7 +13,7 @@ func AuthHeaderHandler(c *gin.Context, cfg *config.Config) (isValid bool, err st
}
// 获取"GH-Auth"的值
authToken := c.GetHeader("GH-Auth")
logInfo("%s %s %s %s %s AUTH_TOKEN: %s", c.Request.Method, c.Request.Host, c.Request.URL.Path, c.Request.Proto, c.Request.RemoteAddr, authToken)
logDebug("%s %s %s %s %s AUTH_TOKEN: %s", c.Request.Method, c.Request.Host, c.Request.URL.Path, c.Request.Proto, c.Request.RemoteAddr, authToken)
if authToken == "" {
err := "Auth Header == nil"
return false, err
@@ -25,6 +25,5 @@ func AuthHeaderHandler(c *gin.Context, cfg *config.Config) (isValid bool, err st
return false, err
}
logInfo("auth SUCCESS: %t", isValid)
return isValid, ""
}

View File

@@ -13,7 +13,7 @@ func AuthParametersHandler(c *gin.Context, cfg *config.Config) (isValid bool, er
}
authToken := c.Query("auth_token")
logInfo("%s %s %s %s %s AUTH_TOKEN: %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto, authToken)
logDebug("%s %s %s %s %s AUTH_TOKEN: %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto, authToken)
if authToken == "" {
err := "Auth token == nil"
@@ -26,6 +26,5 @@ func AuthParametersHandler(c *gin.Context, cfg *config.Config) (isValid bool, er
return false, err
}
logInfo("auth SUCCESS: %t", isValid)
return isValid, ""
}

View File

@@ -9,6 +9,8 @@ import (
var (
logw = logger.Logw
LogDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError
@@ -21,7 +23,7 @@ func Init(cfg *config.Config) {
if cfg.Whitelist.Enabled {
LoadWhitelist(cfg)
}
logInfo("Auth Init")
logDebug("Auth Init")
}
func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
@@ -32,10 +34,10 @@ func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string)
isValid, err = AuthHeaderHandler(c, cfg)
return isValid, err
} else if cfg.Auth.AuthMethod == "" {
logWarning("Auth method not set")
logError("Auth method not set")
return true, ""
} else {
logWarning("Auth method not supported")
logError("Auth method not supported")
return false, "Auth method not supported"
}
}

View File

@@ -13,6 +13,7 @@ type Config struct {
Blacklist BlacklistConfig
Whitelist WhitelistConfig
RateLimit RateLimitConfig
Outbound OutboundConfig
}
type ServerConfig struct {
@@ -31,6 +32,7 @@ type PagesConfig struct {
type LogConfig struct {
LogFilePath string `toml:"logFilePath"`
MaxLogSize int `toml:"maxLogSize"`
Level string `toml:"level"`
}
type CORSConfig struct {
@@ -61,6 +63,16 @@ type RateLimitConfig struct {
Burst int `toml:"burst"`
}
/*
[outbound]
enabled = false
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"
*/
type OutboundConfig struct {
Enabled bool `toml:"enabled"`
Url string `toml:"url"`
}
// LoadConfig 从 TOML 配置文件加载配置
func LoadConfig(filePath string) (*Config, error) {
var config Config

View File

@@ -12,6 +12,7 @@ staticDir = "/data/www"
[log]
logFilePath = "/data/ghproxy/log/ghproxy.log"
maxLogSize = 5 # MB
level = "info" # dump, debug, info, warn, error, none
[cors]
enabled = true
@@ -35,3 +36,7 @@ enabled = false
rateMethod = "total" # "ip" or "total"
ratePerMinute = 180
burst = 5
[outbound]
enabled = false
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"

View File

@@ -2,7 +2,7 @@
host = "127.0.0.1"
port = 8080
sizeLimit = 125 # MB
enableH2C = false
enableH2C = "on"
debug = false
[pages]
@@ -12,6 +12,7 @@ staticDir = "/usr/local/ghproxy/pages"
[log]
logFilePath = "/usr/local/ghproxy/log/ghproxy.log"
maxLogSize = 5 # MB
level = "info" # dump, debug, info, warn, error, none
[cors]
enabled = true
@@ -35,3 +36,7 @@ enabled = false
rateMethod = "total" # "ip" or "total"
ratePerMinute = 180
burst = 5
[outbound]
enabled = false
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"

View File

@@ -73,8 +73,51 @@ if [ -z "$ghproxy_dir" ]; then
ghproxy_dir="/usr/local/ghproxy"
fi
make_systemd_service() {
# 创建目录
mkdir -p ${ghproxy_dir}
mkdir -p ${ghproxy_dir}/config
mkdir -p ${ghproxy_dir}/log
mkdir -p ${ghproxy_dir}/pages
# 获取最新版本号
VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/DEV-VERSION)
wget -q -O ${ghproxy_dir}/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/DEV-VERSION
# 下载ghproxy
wget -q -O ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/$VERSION/ghproxy-linux-$ARCH.tar.gz
install tar
tar -zxvf ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz -C ${ghproxy_dir}
chmod +x ${ghproxy_dir}/ghproxy
# 下载pages
wget -q -O ${ghproxy_dir}/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/pages/index.html
wget -q -O ${ghproxy_dir}/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/pages/favicon.ico
# 下载配置文件
if [ -f ${ghproxy_dir}/config/config.toml ]; then
echo "配置文件已存在, 跳过下载"
echo "[WARNING] 请检查配置文件是否正确DEV版本升级时请注意配置文件兼容性"
sleep 2
else
wget -q -O ${ghproxy_dir}/config/config.toml https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/deploy/config.toml
fi
# 替换 port = 8080
sed -i "s/port = 8080/port = $PORT/g" ${ghproxy_dir}/config/config.toml
sed -i 's/host = "127.0.0.1"/host = "'"$IP"'"/g' ${ghproxy_dir}/config/config.toml
sed -i "s|staticDir = \"/usr/local/ghproxy/pages\"|staticDir = \"${ghproxy_dir}/pages\"|g" ${ghproxy_dir}/config/config.toml
sed -i "s|logFilePath = \"/usr/local/ghproxy/log/ghproxy.log\"|logFilePath = \"${ghproxy_dir}/log/ghproxy.log\"|g" ${ghproxy_dir}/config/config.toml
sed -i "s|blacklistFile = \"/usr/local/ghproxy/config/blacklist.json\"|blacklistFile = \"${ghproxy_dir}/config/blacklist.json\"|g" ${ghproxy_dir}/config/config.toml
sed -i "s|whitelistFile = \"/usr/local/ghproxy/config/whitelist.json\"|whitelistFile = \"${ghproxy_dir}/config/whitelist.json\"|g" ${ghproxy_dir}/config/config.toml
# 下载systemd服务文件
if [ "$ghproxy_dir" = "/usr/local/ghproxy" ]; then
wget -q -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/deploy/ghproxy.service
else
cat <<EOF > /etc/systemd/system/ghproxy.service
[Unit]
Description=Github Proxy Service
After=network.target
@@ -91,51 +134,6 @@ WantedBy=multi-user.target
EOF
}
# 创建目录
mkdir -p ${ghproxy_dir}
mkdir -p ${ghproxy_dir}/config
mkdir -p ${ghproxy_dir}/log
mkdir -p ${ghproxy_dir}/pages
# 获取最新版本号
VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/DEV-VERSION)
wget -q -O ${ghproxy_dir}/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/DEV-VERSION
# 下载ghproxy
wget -q -O ${ghproxy_dir}/ghproxy https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/$VERSION/ghproxy-linux-$ARCH.tar.gz
install tar
tar -zxvf ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz -C ${ghproxy_dir}
chmod +x ${ghproxy_dir}/ghproxy
# 下载pages
wget -q -O ${ghproxy_dir}/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/index.html
wget -q -O ${ghproxy_dir}/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/favicon.ico
# 下载配置文件
if [ -f ${ghproxy_dir}/config/config.toml ]; then
echo "配置文件已存在, 跳过下载"
echo "[WARNING] 请检查配置文件是否正确DEV版本升级时请注意配置文件兼容性"
sleep 2
else
wget -q -O ${ghproxy_dir}/config/config.toml https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/config.toml
fi
# 替换 port = 8080
sed -i "s/port = 8080/port = $PORT/g" ${ghproxy_dir}/config/config.toml
sed -i 's/host = "127.0.0.1"/host = "'"$IP"'"/g' ${ghproxy_dir}/config/config.toml
sed -i "s|staticDir = \"/usr/local/ghproxy/pages\"|staticDir = \"${ghproxy_dir}/pages\"|g" ${ghproxy_dir}/config/config.toml
sed -i "s|logFilePath = \"/usr/local/ghproxy/log/ghproxy.log\"|logFilePath = \"${ghproxy_dir}/log/ghproxy.log\"|g" ${ghproxy_dir}/config/config.toml
sed -i "s|blacklistFile = \"/usr/local/ghproxy/config/blacklist.json\"|blacklistFile = \"${ghproxy_dir}/config/blacklist.json\"|g" ${ghproxy_dir}/config/config.toml
sed -i "s|whitelistFile = \"/usr/local/ghproxy/config/whitelist.json\"|whitelistFile = \"${ghproxy_dir}/config/whitelist.json\"|g" ${ghproxy_dir}/config/config.toml
# 下载systemd服务文件
if [ "$ghproxy_dir" = "/usr/local/ghproxy" ]; then
wget -q -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/ghproxy.service
else
make_systemd_service()
fi
# 启动ghproxy

View File

@@ -73,26 +73,6 @@ if [ -z "$ghproxy_dir" ]; then
ghproxy_dir="/usr/local/ghproxy"
fi
make_systemd_service() {
cat <<EOF > /etc/systemd/system/ghproxy.service
[Unit]
Description=Github Proxy Service
After=network.target
[Service]
ExecStart=/bin/bash -c '$ghproxy_dir/ghproxy -cfg $ghproxy_dir/config/config.toml > $ghproxy_dir/log/run.log 2>&1'
WorkingDirectory=$ghproxy_dir
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
EOF
}
# 创建目录
mkdir -p ${ghproxy_dir}
mkdir -p ${ghproxy_dir}/config
@@ -104,7 +84,7 @@ VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/mai
wget -q -O ${ghproxy_dir}/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/VERSION
# 下载ghproxy
wget -q -O ${ghproxy_dir}/ghproxy https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/${VERSION}/ghproxy-linux-${ARCH}.tar.gz
wget -q -O ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/${VERSION}/ghproxy-linux-${ARCH}.tar.gz
install tar
tar -zxvf ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz -C ${ghproxy_dir}
chmod +x ${ghproxy_dir}/ghproxy
@@ -135,7 +115,25 @@ sed -i "s|whitelistFile = \"/usr/local/ghproxy/config/whitelist.json\"|whitelist
if [ "$ghproxy_dir" = "/usr/local/ghproxy" ]; then
wget -q -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/ghproxy.service
else
make_systemd_service()
cat <<EOF > /etc/systemd/system/ghproxy.service
[Unit]
Description=Github Proxy Service
After=network.target
[Service]
ExecStart=/bin/bash -c '$ghproxy_dir/ghproxy -cfg $ghproxy_dir/config/config.toml > $ghproxy_dir/log/run.log 2>&1'
WorkingDirectory=$ghproxy_dir
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
EOF
fi
# 启动ghproxy

10
go.mod
View File

@@ -1,11 +1,12 @@
module ghproxy
go 1.23.6
go 1.24.0
require (
github.com/BurntSushi/toml v1.4.0
github.com/WJQSERVER-STUDIO/go-utils/logger v1.2.0
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0
github.com/gin-gonic/gin v1.10.0
golang.org/x/net v0.35.0
golang.org/x/time v0.10.0
)
@@ -30,10 +31,9 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.14.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

14
go.sum
View File

@@ -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.2.0 h1:6JkiVGRasfdBxEV8Qnv5x00Bq4qX1Tg6f58Ar/D2YX0=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.2.0/go.mod h1:oW884JCCPDU6c906LI0uKXndWLiRvjb9LkGYC2cqRO8=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0 h1:rOvutC4zYfvtSGN2CNZrycjtq8dLpfu7ypy7tTEErPY=
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0/go.mod h1:oW884JCCPDU6c906LI0uKXndWLiRvjb9LkGYC2cqRO8=
github.com/bytedance/sonic v1.12.8 h1:4xYRVRlXIgvSZ4e8iVTlMF5szgpXd4AfvuWgA8I8lgs=
github.com/bytedance/sonic v1.12.8/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
@@ -69,10 +69,12 @@ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65E
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4=
golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
@@ -80,8 +82,8 @@ golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/time v0.10.0 h1:3usCWA8tQn0L8+hFJQNgzpWbd89begxN66o1Ojdn5L4=
golang.org/x/time v0.10.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

34
loggin/loggin.go Normal file
View File

@@ -0,0 +1,34 @@
package loggin
import (
"ghproxy/timing"
"time"
"github.com/WJQSERVER-STUDIO/go-utils/logger"
"github.com/gin-gonic/gin"
)
var (
logw = logger.Logw
LogDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError
)
// 日志中间件
func Middleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 处理请求
c.Next()
var timingResults time.Duration
// 获取计时结果
timingResults, _ = timing.Get(c)
// 记录日志 IP METHOD URL USERAGENT PROTOCOL STATUS TIMING
logInfo("%s %s %s %s %d %s ", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Writer.Status(), timingResults)
}
}

42
main.go
View File

@@ -6,15 +6,16 @@ import (
"fmt"
"io"
"io/fs"
"log"
"net/http"
"time"
"ghproxy/api"
"ghproxy/auth"
"ghproxy/config"
"ghproxy/loggin"
"ghproxy/proxy"
"ghproxy/rate"
"ghproxy/timing"
"github.com/WJQSERVER-STUDIO/go-utils/logger"
@@ -40,6 +41,8 @@ var (
var (
logw = logger.Logw
LogDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError
@@ -53,20 +56,27 @@ func loadConfig() {
var err error
cfg, err = config.LoadConfig(cfgfile)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
fmt.Printf("Failed to load config: %v\n", err)
}
if cfg.Server.Debug {
fmt.Println("Config File Path: ", cfgfile)
fmt.Printf("Loaded config: %v\n", cfg)
}
}
func setupLogger(cfg *config.Config) {
var err error
err = logger.Init(cfg.Log.LogFilePath, cfg.Log.MaxLogSize)
if err != nil {
log.Fatalf("Failed to initialize logger: %v", err)
fmt.Printf("Failed to initialize logger: %v\n", err)
}
logInfo("Config File Path: ", cfgfile)
logInfo("Loaded config: %v\n", cfg)
err = logger.SetLogLevel(cfg.Log.Level)
if err != nil {
fmt.Printf("Logger Level Error: %v\n", err)
}
fmt.Printf("Log Level: %s\n", cfg.Log.Level)
logDebug("Config File Path: ", cfgfile)
logDebug("Loaded config: %v\n", cfg)
logInfo("Init Completed")
}
@@ -87,12 +97,11 @@ func setupRateLimit(cfg *config.Config) {
} else {
logError("Invalid RateLimit Method: %s", cfg.RateLimit.RateMethod)
}
logInfo("Rate Limit Loaded")
}
}
func InitReq() {
proxy.InitReq()
func InitReq(cfg *config.Config) {
proxy.InitReq(cfg)
}
func init() {
@@ -100,7 +109,7 @@ func init() {
flag.Parse()
loadConfig()
setupLogger(cfg)
InitReq()
InitReq(cfg)
loadlist(cfg)
setupRateLimit(cfg)
@@ -116,9 +125,20 @@ func init() {
runMode = "release"
}
logDebug("Run Mode: %s", runMode)
gin.LoggerWithWriter(io.Discard)
router = gin.New()
// 添加recovery中间件
router.Use(gin.Recovery())
// 添加log中间件
router.Use(loggin.Middleware())
// 添加计时中间件
router.Use(timing.Middleware())
//H2C默认值为true而后遵循cfg.Server.EnableH2C的设置
if cfg.Server.EnableH2C == "on" {
router.UseH2C = true
@@ -141,7 +161,7 @@ func init() {
} else if !cfg.Pages.Enabled {
pages, err := fs.Sub(pagesFS, "pages")
if err != nil {
log.Fatalf("Failed when processing pages: %s", err)
logError("Failed when processing pages: %s", err)
}
router.GET("/", gin.WrapH(http.FileServer(http.FS(pages))))
router.GET("/favicon.ico", gin.WrapH(http.FileServer(http.FS(pages))))
@@ -152,6 +172,8 @@ func init() {
})
fmt.Printf("GHProxy Version: %s\n", version)
fmt.Printf("A Go Based High-Performance Github Proxy \n")
fmt.Printf("Made by WJQSERVER-STUDIO\n")
}
func main() {

View File

@@ -11,6 +11,7 @@ func AuthPassThrough(c *gin.Context, cfg *config.Config, req *http.Request) {
if cfg.Auth.PassThrough {
token := c.Query("token")
if token != "" {
logDebug("%s %s %s %s %s Auth-PassThrough: token %s", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, token)
switch cfg.Auth.AuthMethod {
case "parameters":
if !cfg.Auth.Enabled {

View File

@@ -22,9 +22,9 @@ var (
BufferPool *sync.Pool
)
func InitReq() {
initChunkedHTTPClient()
initGitHTTPClient()
func InitReq(cfg *config.Config) {
initChunkedHTTPClient(cfg)
initGitHTTPClient(cfg)
// 初始化固定大小的缓存池
BufferPool = &sync.Pool{
@@ -34,7 +34,7 @@ func InitReq() {
}
}
func initChunkedHTTPClient() {
func initChunkedHTTPClient(cfg *config.Config) {
ctr = &http.Transport{
MaxIdleConns: 100,
MaxConnsPerHost: 60,
@@ -47,6 +47,9 @@ func initChunkedHTTPClient() {
KeepAlive: 30 * time.Second,
}).DialContext,
}
if cfg.Outbound.Enabled {
initTransport(cfg, ctr)
}
cclient = &http.Client{
Transport: ctr,
}
@@ -54,12 +57,11 @@ func initChunkedHTTPClient() {
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)
// 发送HEAD请求, 预获取Content-Length
headReq, err := http.NewRequest("HEAD", u, nil)
if err != nil {
HandleError(c, fmt.Sprintf("创建HEAD请求失败: %v", err))
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return
}
setRequestHeaders(c, headReq)
@@ -108,7 +110,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
// 创建请求
req, err := http.NewRequest(method, u, bodyReader)
if err != nil {
HandleError(c, fmt.Sprintf("创建请求失败: %v", err))
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return
}
@@ -118,7 +120,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
resp, err := cclient.Do(req)
if err != nil {
HandleError(c, fmt.Sprintf("发送请求失败: %v", err))
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
return
}
defer resp.Body.Close()
@@ -171,7 +173,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
_, err = io.CopyBuffer(c.Writer, resp.Body, buffer)
if err != nil {
logError("%s %s %s %s %s 响应复制错误: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
return
} else {
c.Writer.Flush() // 确保刷入

135
proxy/dial.go Normal file
View File

@@ -0,0 +1,135 @@
/*
made&PR by @lfhy
https://github.com/WJQSERVER-STUDIO/ghproxy/pull/46
*/
package proxy
import (
"ghproxy/config"
"net/http"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
// initTransport 初始化 HTTP 传输层的代理设置
func initTransport(cfg *config.Config, transport *http.Transport) {
// 如果代理功能未启用,直接返回
if !cfg.Outbound.Enabled {
return
}
// 如果代理 URL 未设置,使用环境变量中的代理配置
if cfg.Outbound.Url == "" {
transport.Proxy = http.ProxyFromEnvironment
logWarning("Outbound proxy is not set, using environment variables")
return
}
// 尝试解析代理 URL
proxyInfo, err := url.Parse(cfg.Outbound.Url)
if err != nil {
// 如果解析失败,记录错误日志并使用环境变量中的代理配置
logError("Failed to parse outbound proxy URL %v", err)
transport.Proxy = http.ProxyFromEnvironment
return
}
// 根据代理 URL 的 scheme协议类型选择代理类型
switch strings.ToLower(proxyInfo.Scheme) {
case "http", "https": // 如果是 HTTP/HTTPS 代理
transport.Proxy = http.ProxyURL(proxyInfo) // 设置 HTTP(S) 代理
logInfo("Using HTTP(S) proxy: %s", proxyInfo.Redacted())
case "socks5": // 如果是 SOCKS5 代理
// 调用 newProxyDial 创建 SOCKS5 代理拨号器
proxyDialer := newProxyDial(cfg.Outbound.Url)
transport.Proxy = nil // 禁用 HTTP Proxy 设置,因为 SOCKS5 不需要 HTTP Proxy
// 尝试将 Dialer 转换为支持上下文的 ContextDialer
if contextDialer, ok := proxyDialer.(proxy.ContextDialer); ok {
transport.DialContext = contextDialer.DialContext
} else {
// 如果不支持 ContextDialer则回退到传统的 Dial 方法
transport.Dial = proxyDialer.Dial
logWarning("SOCKS5 dialer does not support ContextDialer, using legacy Dial")
}
logInfo("Using SOCKS5 proxy chain: %s", cfg.Outbound.Url)
default: // 如果代理协议不支持
logError("Unsupported proxy scheme: %s", proxyInfo.Scheme)
transport.Proxy = http.ProxyFromEnvironment // 回退到环境变量代理
}
}
// newProxyDial 创建一个 SOCKS5 代理拨号器
func newProxyDial(proxyUrls string) proxy.Dialer {
var proxyDialer proxy.Dialer = proxy.Direct // 初始为直接连接,不使用代理
// 支持多个代理 URL以逗号分隔
for _, proxyUrl := range strings.Split(proxyUrls, ",") {
proxyUrl = strings.TrimSpace(proxyUrl) // 去除首尾空格
if proxyUrl == "" { // 跳过空的代理 URL
continue
}
// 解析代理 URL
urlInfo, err := url.Parse(proxyUrl)
if err != nil {
// 如果 URL 解析失败,记录错误日志并跳过
logError("Failed to parse proxy URL %q: %v", proxyUrl, err)
continue
}
// 检查代理协议是否为 SOCKS5
if urlInfo.Scheme != "socks5" {
logWarning("Skipping non-SOCKS5 proxy: %s", urlInfo.Scheme)
continue
}
// 解析代理认证信息(用户名和密码)
auth := parseAuth(urlInfo)
// 创建 SOCKS5 代理拨号器
dialer, err := createSocksDialer(urlInfo.Host, auth, proxyDialer)
if err != nil {
// 如果创建失败,记录错误日志并跳过
logError("Failed to create SOCKS5 dialer for %q: %v", proxyUrl, err)
continue
}
// 更新代理拨号器,支持代理链
proxyDialer = dialer
}
return proxyDialer
}
// parseAuth 解析代理 URL 中的认证信息(用户名和密码)
func parseAuth(urlInfo *url.URL) *proxy.Auth {
// 如果 URL 中没有用户信息,返回 nil
if urlInfo.User == nil {
return nil
}
// 获取用户名
username := urlInfo.User.Username()
// 获取密码注意Password() 返回两个值,需要显式处理第二个值)
password, passwordSet := urlInfo.User.Password()
if !passwordSet {
password = "" // 如果密码未设置,使用空字符串
}
// 返回包含用户名和密码的认证信息
return &proxy.Auth{
User: username,
Password: password, // 允许空密码
}
}
// createSocksDialer 创建 SOCKS5 拨号器
func createSocksDialer(host string, auth *proxy.Auth, previous proxy.Dialer) (proxy.Dialer, error) {
// 调用 golang.org/x/net/proxy 提供的 SOCKS5 方法创建拨号器
return proxy.SOCKS5("tcp", host, auth, previous)
}

View File

@@ -17,12 +17,15 @@ var (
gtr *http.Transport
)
func initGitHTTPClient() {
func initGitHTTPClient(cfg *config.Config) {
gtr = &http.Transport{
MaxIdleConns: 30,
MaxConnsPerHost: 30,
IdleConnTimeout: 30 * time.Second,
}
if cfg.Outbound.Enabled {
initTransport(cfg, gtr)
}
gclient = &http.Client{
Transport: gtr,
}
@@ -38,7 +41,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
// 发送HEAD请求, 预获取Content-Length
headReq, err := http.NewRequest("HEAD", u, nil)
if err != nil {
HandleError(c, fmt.Sprintf("创建HEAD请求失败: %v", err))
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return
}
setRequestHeaders(c, headReq)
@@ -80,7 +83,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
// 创建请求
req, err := http.NewRequest(method, u, bodyReader)
if err != nil {
HandleError(c, fmt.Sprintf("创建请求失败: %v", err))
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return
}
setRequestHeaders(c, req)

View File

@@ -14,6 +14,7 @@ import (
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter, runMode string) gin.HandlerFunc {
return func(c *gin.Context) {
// 限制访问频率
if cfg.RateLimit.Enabled {
@@ -54,6 +55,8 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
username, repo := MatchUserRepo(rawPath, cfg, c, matches) // 匹配用户名和仓库名
logInfo("%s %s %s %s %s Matched-Username: %s, Matched-Repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, username, repo)
// dump log 记录详细信息 c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, full Header
LogDump("%s %s %s %s %s %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, c.Request.Header)
repouser := fmt.Sprintf("%s/%s", username, repo)
// 白名单检查
@@ -83,7 +86,7 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
matches = CheckURL(rawPath, c)
if matches == nil {
c.AbortWithStatus(http.StatusNotFound)
logError("%s %s %s %s %s 404-NOMATCH", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
logWarning("%s %s %s %s %s 404-NOMATCH", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
return
}
@@ -91,7 +94,7 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
if exps[5].MatchString(rawPath) {
if cfg.Auth.AuthMethod != "header" || !cfg.Auth.Enabled {
c.JSON(http.StatusForbidden, gin.H{"error": "HeaderAuth is not enabled."})
logWarning("%s %s %s %s %s HeaderAuth-Error: HeaderAuth is not enabled.", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
logError("%s %s %s %s %s HeaderAuth-Error: HeaderAuth is not enabled.", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
return
}
}
@@ -110,7 +113,7 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
}
// IP METHOD URL USERAGENT PROTO MATCHES
logInfo("%s %s %s %s %s Matches: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, matches)
logDebug("%s %s %s %s %s Matches: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, matches)
switch {
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):

View File

@@ -18,7 +18,7 @@ var (
// 提取用户名和仓库名
func MatchUserRepo(rawPath string, cfg *config.Config, c *gin.Context, matches []string) (string, string) {
if gistMatches := gistRegex.FindStringSubmatch(rawPath); len(gistMatches) == 3 {
logInfo("%s %s %s %s %s Matched-Username: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, gistMatches[1])
LogDump("%s %s %s %s %s Matched-Username: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, gistMatches[1])
return gistMatches[1], ""
}
// 定义路径

View File

@@ -13,6 +13,8 @@ import (
// 日志模块
var (
logw = logger.Logw
LogDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError
@@ -31,6 +33,7 @@ var exps = []*regexp.Regexp{
func readRequestBody(c *gin.Context) ([]byte, error) {
body, err := io.ReadAll(c.Request.Body)
if err != nil {
logError("failed to read request body: %v", err)
return nil, fmt.Errorf("failed to read request body: %v", err)
}
defer c.Request.Body.Close()
@@ -59,7 +62,7 @@ func SendRequest(c *gin.Context, req *req.Request, method, url string) (*req.Res
func HandleError(c *gin.Context, message string) {
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
logWarning(message)
logError(message)
}
func CheckURL(u string, c *gin.Context) []string {
@@ -69,7 +72,7 @@ func CheckURL(u string, c *gin.Context) []string {
}
}
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
logWarning(errMsg)
logError(errMsg)
return nil
}

View File

@@ -10,6 +10,8 @@ import (
// 日志输出
var (
logw = logger.Logw
LogDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError

86
timing/timing.go Normal file
View File

@@ -0,0 +1,86 @@
package timing
import (
"sync"
"time"
"github.com/gin-gonic/gin"
)
// 阶段计时结构(固定数组优化)
type timingData struct {
phases [8]struct { // 预分配8个阶段存储
name string
dur time.Duration
}
count int
start time.Time
}
// 对象池(内存重用优化)
var pool = sync.Pool{
New: func() interface{} {
return new(timingData)
},
}
// 中间件入口
func Middleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 从池中获取计时器
td := pool.Get().(*timingData)
td.start = time.Now()
td.count = 0
// 存储到上下文
c.Set("timing", td)
// 请求完成后回收对象
defer func() {
pool.Put(td)
}()
c.Next()
}
}
// 记录阶段耗时
func Record(c *gin.Context, name string) {
if val, exists := c.Get("timing"); exists {
//td := val.(*timingData)
td, ok := val.(*timingData)
if !ok {
return
}
if td.count < len(td.phases) {
td.phases[td.count].name = name
td.phases[td.count].dur = time.Since(td.start) // 直接记录当前时间
td.count++
}
}
}
// 获取计时结果(日志输出用)
func Get(c *gin.Context) (total time.Duration, phases []struct {
Name string
Dur time.Duration
}) {
if val, exists := c.Get("timing"); exists {
//td := val.(*timingData)
td, ok := val.(*timingData)
if !ok {
return
}
for i := 0; i < td.count; i++ {
phases = append(phases, struct {
Name string
Dur time.Duration
}{
Name: td.phases[i].name,
Dur: td.phases[i].dur,
})
}
total = time.Since(td.start)
}
return
}