Compare commits
50 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ac08bba56 | ||
|
|
61e741c9b3 | ||
|
|
815b86c6c6 | ||
|
|
999016be2b | ||
|
|
af6c6cfe64 | ||
|
|
88ee8abb5c | ||
|
|
2d9ce57e90 | ||
|
|
d33377087f | ||
|
|
c78d114767 | ||
|
|
b87a8de3c4 | ||
|
|
5dbf137116 | ||
|
|
57f67278a3 | ||
|
|
b9a7f30705 | ||
|
|
c0a9ed11e3 | ||
|
|
eec50879e9 | ||
|
|
862b92a1c5 | ||
|
|
1d7780a890 | ||
|
|
ee215eff36 | ||
|
|
20b9c7827d | ||
|
|
89e6be7709 | ||
|
|
822c08d4c0 | ||
|
|
8588d66a6c | ||
|
|
e4713d0f50 | ||
|
|
51d84f0fc3 | ||
|
|
912b6c0279 | ||
|
|
c4ecf74d71 | ||
|
|
9f801bbac8 | ||
|
|
a1fd8e59f0 | ||
|
|
df05c6a275 | ||
|
|
8ae6a9e090 | ||
|
|
00283fa4d2 | ||
|
|
b68593cbd0 | ||
|
|
c11fd11647 | ||
|
|
058610536b | ||
|
|
7f321bd5f4 | ||
|
|
3665bc9cfd | ||
|
|
fea70c01d8 | ||
|
|
8412231b72 | ||
|
|
178d5f14c2 | ||
|
|
bb4e2d8ae7 | ||
|
|
7d6d9d0da5 | ||
|
|
a8d524739b | ||
|
|
93c3175206 | ||
|
|
58b9fcd1c9 | ||
|
|
af0c364662 | ||
|
|
45246e124e | ||
|
|
0899397d2e | ||
|
|
efeb940676 | ||
|
|
19e2909699 | ||
|
|
48c2ea8f97 |
11
.github/dependabot.yml
vendored
Normal file
11
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# To get started with Dependabot version updates, you'll need to specify which
|
||||||
|
# package ecosystems to update and where the package manifests are located.
|
||||||
|
# Please see the documentation for all configuration options:
|
||||||
|
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "gomod" # See documentation for possible values
|
||||||
|
directory: "/" # Location of package manifests
|
||||||
|
schedule:
|
||||||
|
interval: "weekly"
|
||||||
19
.github/workflows/auto-assign.yml
vendored
Normal file
19
.github/workflows/auto-assign.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
name: Auto Assign
|
||||||
|
on:
|
||||||
|
issues:
|
||||||
|
types: [opened]
|
||||||
|
pull_request:
|
||||||
|
types: [opened]
|
||||||
|
jobs:
|
||||||
|
run:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
issues: write
|
||||||
|
pull-requests: write
|
||||||
|
steps:
|
||||||
|
- name: 'Auto-assign issue'
|
||||||
|
uses: pozil/auto-assign-issue@v1
|
||||||
|
with:
|
||||||
|
repo-token: ${{ secrets.AUTO_ASSIGN }}
|
||||||
|
assignees: WJQSERVER, satomitoka
|
||||||
|
numOfAssignee: 2
|
||||||
2
.github/workflows/build-dev.yml
vendored
2
.github/workflows/build-dev.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
OUTPUT_BINARY: ghproxy
|
OUTPUT_BINARY: ghproxy
|
||||||
OUTPUT_ARCHIVE: ghproxy.tar.gz
|
OUTPUT_ARCHIVE: ghproxy.tar.gz
|
||||||
GO_VERSION: 1.23.1
|
GO_VERSION: 1.23.2
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|||||||
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
OUTPUT_BINARY: ghproxy
|
OUTPUT_BINARY: ghproxy
|
||||||
OUTPUT_ARCHIVE: ghproxy.tar.gz
|
OUTPUT_ARCHIVE: ghproxy.tar.gz
|
||||||
GO_VERSION: 1.23.1
|
GO_VERSION: 1.23.2
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|||||||
80
CHANGELOG.md
80
CHANGELOG.md
@@ -1,5 +1,85 @@
|
|||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
|
v1.3.0
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
- CHANGE: 优化黑名单功能,提升稳定性
|
||||||
|
- CHANGE: 剃刀计划,减少调试用日志输出
|
||||||
|
- ADD: 新增auth子模块blacklist.go,支持黑名单功能
|
||||||
|
- ADD: 新增blacklist.json文件,用于配置黑名单
|
||||||
|
- CHANGE: config.yaml文件格式修改,使其具备更好的可读性
|
||||||
|
- WARNING: 此版本为大版本更新,配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||||
|
|
||||||
|
24w09b
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
- CHANGE: 修正配置,提升稳定性
|
||||||
|
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||||
|
|
||||||
|
24w09a
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
- CHANGE: 优化黑名单功能,提升稳定性
|
||||||
|
- CHANGE&ADD: 新增auth子模块blacklist.go
|
||||||
|
- CHANGE: 黑名单转为使用json文件存储,便于程序处理
|
||||||
|
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||||
|
|
||||||
|
24w08b
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
- ADD & CHANGE: 新增仓库黑名单功能,改进Auth模块
|
||||||
|
- ADD: 新增blacklist.yaml文件,用于配置仓库黑名单
|
||||||
|
- CHANGE: 大幅度修改Config包,使其更加模块化
|
||||||
|
- CHANGE: 与Config包同步修改config.yaml文件(不向前兼容)
|
||||||
|
- CHANGE: 修改config.yaml文件的格式,使其具备更好的可读性
|
||||||
|
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||||
|
|
||||||
|
v1.2.0
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,提升性能
|
||||||
|
- CHANGE: 同步更新logger模块,与golang-temp项目定义的开发规范保持一致
|
||||||
|
- ADD: 新增日志翻转功能
|
||||||
|
|
||||||
|
24w08a
|
||||||
|
---
|
||||||
|
- CHANGE: 同步更新logger模块,与golang-temp项目定义的开发规范保持一致
|
||||||
|
- ADD: 新增日志翻转功能
|
||||||
|
|
||||||
|
v1.1.1
|
||||||
|
---
|
||||||
|
- CHANGE: 修改部分代码,与golang-temp项目定义的开发规范保持一致
|
||||||
|
- CHANGE: 更新Go版本至v1.23.2
|
||||||
|
- CHANGE: 跟随Caddy更新,修改Caddyfile配置
|
||||||
|
|
||||||
|
24w07b
|
||||||
|
---
|
||||||
|
- CHANGE: 修改部分代码,与golang-temp项目定义的开发规范保持一致
|
||||||
|
- CHANGE: 更新Go版本至v1.23.2
|
||||||
|
- CHANGE: 跟随Caddy更新,修改Caddyfile配置
|
||||||
|
|
||||||
|
24w07a
|
||||||
|
---
|
||||||
|
- CHANGE: 修改部分代码,与golang-temp项目定义的开发规范保持一致
|
||||||
|
- CHANGE: 更新Go版本至v1.23.2
|
||||||
|
|
||||||
|
v1.1.0
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,对main函数进行模块化,提升可读性
|
||||||
|
- CHANGE: Docker代理功能移至DEV版本内,保证稳定性
|
||||||
|
- ADD&CHANGE: 增加Auth(用户鉴权)模块,并改进其的错误处理与日志记录
|
||||||
|
- CHANGE: 日志模块引入goroutine协程,提升性能
|
||||||
|
- ADD: 将主要实现分离,作为Proxy模块,并优化代码结构
|
||||||
|
- ADD: 新增安全政策
|
||||||
|
|
||||||
|
24w06b
|
||||||
|
---
|
||||||
|
- CHANGE: 优化代码结构,对main函数进行模块化,提升可读性
|
||||||
|
- CHANGE: Docker代理功能移至DEV版本内,保证稳定性
|
||||||
|
- ADD&CHANGE: 增加Auth(用户鉴权)模块,并改进其的错误处理与日志记录
|
||||||
|
- CHANGE: 日志模块引入goroutine协程,提升性能 (实验性功能)
|
||||||
|
- ADD: 将主要实现分离,作为Proxy模块,并优化代码结构
|
||||||
|
- ADD: 新增安全政策
|
||||||
|
|
||||||
v1.0.0
|
v1.0.0
|
||||||
---
|
---
|
||||||
- CHANGE: 项目正式发布, 并迁移至[WJQSERVER-STUDIO/ghproxy](https://github.com/WJQSERVER-STUDIO/ghproxy)
|
- CHANGE: 项目正式发布, 并迁移至[WJQSERVER-STUDIO/ghproxy](https://github.com/WJQSERVER-STUDIO/ghproxy)
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
24w06a
|
24w09b
|
||||||
59
README.md
59
README.md
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
使用Go实现的GHProxy,用于加速部分地区Github仓库的拉取,支持速率限制,用户鉴权,支持Docker部署
|
使用Go实现的GHProxy,用于加速部分地区Github仓库的拉取,支持速率限制,用户鉴权,支持Docker部署
|
||||||
|
|
||||||
|
[DEMO](ghproxy.1888866.xyz)
|
||||||
|
|
||||||
## 项目说明
|
## 项目说明
|
||||||
|
|
||||||
### 项目特点
|
### 项目特点
|
||||||
@@ -14,6 +16,8 @@
|
|||||||
- 支持Docker部署
|
- 支持Docker部署
|
||||||
- 支持速率限制
|
- 支持速率限制
|
||||||
- 支持用户鉴权
|
- 支持用户鉴权
|
||||||
|
- 支持自定义黑名单
|
||||||
|
- 符合[RFC 7234](https://httpwg.org/specs/rfc7234.html)的HTTP Cache
|
||||||
- 使用Caddy作为Web Server
|
- 使用Caddy作为Web Server
|
||||||
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
||||||
|
|
||||||
@@ -59,19 +63,51 @@ docker run -p 7210:80 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/ca
|
|||||||
本项目采用config.yaml作为外部配置,默认配置如下
|
本项目采用config.yaml作为外部配置,默认配置如下
|
||||||
使用Docker部署时,慎重修改config.yaml,以免造成不必要的麻烦
|
使用Docker部署时,慎重修改config.yaml,以免造成不必要的麻烦
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# 核心配置
|
||||||
|
server:
|
||||||
|
port: 8080 # 监听端口(小白请勿修改)
|
||||||
|
host: "127.0.0.1" # 监听地址(小白请勿修改)
|
||||||
|
sizelimit: 131072000 # 125MB
|
||||||
|
|
||||||
|
# 日志配置
|
||||||
|
logger:
|
||||||
|
logfilepath: "/data/ghproxy/log/ghproxy.log" # 日志文件路径(小白请勿修改)
|
||||||
|
maxlogsize: 25 # MB
|
||||||
|
|
||||||
|
# CORS 配置
|
||||||
|
cors:
|
||||||
|
enabled: true # 是否开启CORS
|
||||||
|
|
||||||
|
# 鉴权配置
|
||||||
|
auth:
|
||||||
|
enabled: false # 是否开启鉴权
|
||||||
|
authtoken: "test" # 鉴权Token
|
||||||
|
|
||||||
|
# 黑名单配置
|
||||||
|
blacklist:
|
||||||
|
enabled: true
|
||||||
|
blacklistfile: "/data/ghproxy/config/blacklist.json"
|
||||||
|
|
||||||
```
|
```
|
||||||
port: 8080 # 监听端口
|
|
||||||
host: "127.0.0.1" # 监听地址
|
### 黑名单配置
|
||||||
sizelimit: 131072000 # 125MB
|
|
||||||
logfilepath: "/data/ghproxy/log/ghproxy-0rtt.log" # 日志文件路径
|
黑名单配置位于config/blacklist.json,格式如下:
|
||||||
CorsAllowOrigins: true # 是否允许跨域请求
|
|
||||||
auth: true # 是否开启鉴权
|
```json
|
||||||
authtoken: "test" # 鉴权token
|
{
|
||||||
```
|
"blacklist": [
|
||||||
|
"test/test1",
|
||||||
|
"example/repo2",
|
||||||
|
"another/repo3"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Caddy反代配置
|
### Caddy反代配置
|
||||||
|
|
||||||
```
|
```Caddyfile
|
||||||
example.com {
|
example.com {
|
||||||
reverse_proxy {
|
reverse_proxy {
|
||||||
to 127.0.0.1:7210
|
to 127.0.0.1:7210
|
||||||
@@ -84,14 +120,15 @@ example.com {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO & BETA
|
## TODO & DEV
|
||||||
|
|
||||||
### TODO
|
### TODO
|
||||||
|
|
||||||
- [x] 允许更多参数通过config结构传入
|
- [x] 允许更多参数通过config结构传入
|
||||||
- [x] 改进程序效率
|
- [x] 改进程序效率
|
||||||
- [x] 用户鉴权
|
- [x] 用户鉴权
|
||||||
|
- [x] 仓库黑名单
|
||||||
|
|
||||||
### BETA
|
### DEV
|
||||||
|
|
||||||
- [x] Docker Pull 代理
|
- [x] Docker Pull 代理
|
||||||
|
|||||||
35
SECURITY.MD
Normal file
35
SECURITY.MD
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# 安全政策
|
||||||
|
|
||||||
|
## 支持的版本
|
||||||
|
|
||||||
|
以下版本的项目目前正在接受安全更新:
|
||||||
|
|
||||||
|
| 版本 | 是否支持 |
|
||||||
|
| --- | --- |
|
||||||
|
| v1.x.x | :white_check_mark: |
|
||||||
|
| **w**a/b | :warning: 这是测试版本,用于开发测试,可能存在未知的安全隐患 |
|
||||||
|
| v0.x.x | :x: 这些版本不再受支持 |
|
||||||
|
|
||||||
|
### 版本说明
|
||||||
|
|
||||||
|
- **v1.0.0**: 这是正式发布的版本
|
||||||
|
- **xx W xx A/B***: 这是开发测试版本,可能存在未知的安全隐患。不推荐在生产环境中使用。
|
||||||
|
- **v0.x.x**: 这些早期版本不再受支持,建议尽快升级到 v1.0.0 或更高版本。
|
||||||
|
|
||||||
|
### 用户须知
|
||||||
|
|
||||||
|
本项目为开源项目,开发者不对使用本项目造成的任何损失或问题承担责任。用户需自行评估并承担使用本项目的风险。
|
||||||
|
|
||||||
|
使用本项目,请遵循 **[WSL (WJQSERVER-STUDIO LICENSE)](https://wjqserver-studio.github.io/LICENSE/LICENSE.html)** 协议。
|
||||||
|
|
||||||
|
## 报告漏洞
|
||||||
|
|
||||||
|
如果您发现本项目存在安全漏洞,请通过发送ISSUES或尝试联系项目维护者来报告。请在您的报告中包含以下信息:
|
||||||
|
|
||||||
|
1. 漏洞的描述,包括重现步骤。
|
||||||
|
2. 受到漏洞影响的项目版本。
|
||||||
|
3. 任何缓解因素,如利用难度或漏洞影响。
|
||||||
|
|
||||||
|
项目维护者将审查您的报告,并尽快解决该漏洞。请注意,根据漏洞的复杂程度,可能需要一些时间来调查和修复问题。
|
||||||
|
|
||||||
|
感谢您帮助保护本项目的安全!
|
||||||
35
auth/auth.go
Normal file
35
auth/auth.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"ghproxy/config"
|
||||||
|
"ghproxy/logger"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logw = logger.Logw
|
||||||
|
|
||||||
|
func AuthHandler(c *gin.Context, cfg *config.Config) bool {
|
||||||
|
// 如果身份验证未启用,直接返回 true
|
||||||
|
if !cfg.Auth.Enabled {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 auth_token 参数
|
||||||
|
authToken := c.Query("auth_token")
|
||||||
|
logw("auth_token received: %s", authToken)
|
||||||
|
|
||||||
|
// 验证 token
|
||||||
|
if authToken == "" {
|
||||||
|
logw("auth FAILED: no auth_token provided")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
isValid := authToken == cfg.Auth.AuthToken
|
||||||
|
if !isValid {
|
||||||
|
logw("auth FAILED: invalid auth_token: %s", authToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
logw("auth SUCCESS: %t", isValid)
|
||||||
|
return isValid
|
||||||
|
}
|
||||||
46
auth/blacklist.go
Normal file
46
auth/blacklist.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"ghproxy/config"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Blacklist []string `json:"blacklist"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
cfg *config.Config
|
||||||
|
blacklistfile = "/data/ghproxy/config/blacklist.json"
|
||||||
|
blacklist *Config
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadBlacklist(cfg *config.Config) {
|
||||||
|
blacklistfile = cfg.Blacklist.BlacklistFile
|
||||||
|
blacklist = &Config{}
|
||||||
|
|
||||||
|
data, err := os.ReadFile(blacklistfile)
|
||||||
|
if err != nil {
|
||||||
|
logw("Failed to read blacklist file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, blacklist)
|
||||||
|
if err != nil {
|
||||||
|
logw("Failed to unmarshal blacklist JSON: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckBlacklist(fullrepo string) bool {
|
||||||
|
return forRangeCheck(blacklist.Blacklist, fullrepo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func forRangeCheck(blist []string, fullrepo string) bool {
|
||||||
|
for _, blocked := range blist {
|
||||||
|
if blocked == fullrepo {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logw("%s not in blacklist", fullrepo)
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
https_port 443
|
https_port 443
|
||||||
order cache before rewrite
|
order cache before rewrite
|
||||||
cache {
|
cache {
|
||||||
cache_name W-Cache
|
cache_name GhProxyCache
|
||||||
}
|
}
|
||||||
log {
|
log {
|
||||||
level INFO
|
level INFO
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
format transform `{request>headers>X-Forwarded-For>[0]:request>remote_ip} - {user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
|
format transform `{request>headers>X-Forwarded-For>[0]:request>remote_ip} - {user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
|
||||||
time_format "02/Jan/2006:15:04:05 -0700"
|
time_format "02/Jan/2006:15:04:05 -0700"
|
||||||
}
|
}
|
||||||
output file /data/caddy/log/{args.0}/access.log {
|
output file /data/caddy/log/{args[0]}/access.log {
|
||||||
roll_size 5MB
|
roll_size 5MB
|
||||||
roll_keep 10
|
roll_keep 10
|
||||||
roll_keep_for 24h
|
roll_keep_for 24h
|
||||||
@@ -48,8 +48,8 @@
|
|||||||
(cache) {
|
(cache) {
|
||||||
cache {
|
cache {
|
||||||
allowed_http_verbs GET
|
allowed_http_verbs GET
|
||||||
stale {args.0}
|
stale {args[0]}
|
||||||
ttl {args.1}
|
ttl {args[1]}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,15 +60,9 @@
|
|||||||
header_up X-Forwarded-Proto {http.request.header.CF-Visitor}
|
header_up X-Forwarded-Proto {http.request.header.CF-Visitor}
|
||||||
}
|
}
|
||||||
|
|
||||||
(buffer) {
|
|
||||||
flush_interval 2000s
|
|
||||||
buffer_responses
|
|
||||||
max_buffer_size 256k
|
|
||||||
}
|
|
||||||
|
|
||||||
(rate_limit) {
|
(rate_limit) {
|
||||||
route /* {
|
route /* {
|
||||||
rate_limit {remote.ip} {args.0}r/m 10000 429
|
rate_limit {remote.ip} {args[0]}r/m 10000 429
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,13 +71,11 @@
|
|||||||
to 127.0.0.1:8080
|
to 127.0.0.1:8080
|
||||||
import header_realip
|
import header_realip
|
||||||
}
|
}
|
||||||
import log go
|
import log ghproxy
|
||||||
import cache 0s 600s
|
import cache 0s 600s
|
||||||
import error_page
|
import error_page
|
||||||
import encode
|
import encode
|
||||||
route /* {
|
import rate_limit 60
|
||||||
rate_limit {remote.ip} 60r/m 10000 429
|
|
||||||
}
|
|
||||||
route / {
|
route / {
|
||||||
root /data/www
|
root /data/www
|
||||||
file_server
|
file_server
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
https_port 443
|
https_port 443
|
||||||
order cache before rewrite
|
order cache before rewrite
|
||||||
cache {
|
cache {
|
||||||
cache_name W-Cache
|
cache_name GhProxyCache
|
||||||
}
|
}
|
||||||
log {
|
log {
|
||||||
level INFO
|
level INFO
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
format transform `{request>headers>X-Forwarded-For>[0]:request>remote_ip} - {user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
|
format transform `{request>headers>X-Forwarded-For>[0]:request>remote_ip} - {user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
|
||||||
time_format "02/Jan/2006:15:04:05 -0700"
|
time_format "02/Jan/2006:15:04:05 -0700"
|
||||||
}
|
}
|
||||||
output file /data/caddy/log/{args.0}/access.log {
|
output file /data/caddy/log/{args[0]}/access.log {
|
||||||
roll_size 5MB
|
roll_size 5MB
|
||||||
roll_keep 10
|
roll_keep 10
|
||||||
roll_keep_for 24h
|
roll_keep_for 24h
|
||||||
@@ -48,8 +48,8 @@
|
|||||||
(cache) {
|
(cache) {
|
||||||
cache {
|
cache {
|
||||||
allowed_http_verbs GET
|
allowed_http_verbs GET
|
||||||
stale {args.0}
|
stale {args[0]}
|
||||||
ttl {args.1}
|
ttl {args[1]}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@
|
|||||||
|
|
||||||
(rate_limit) {
|
(rate_limit) {
|
||||||
route /* {
|
route /* {
|
||||||
rate_limit {remote.ip} {args.0}r/m 10000 429
|
rate_limit {remote.ip} {args[0]}r/m 10000 429
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
to 127.0.0.1:8080
|
to 127.0.0.1:8080
|
||||||
import header_realip
|
import header_realip
|
||||||
}
|
}
|
||||||
import log go
|
import log ghproxy
|
||||||
import cache 0s 600s
|
import cache 0s 600s
|
||||||
import error_page
|
import error_page
|
||||||
import encode
|
import encode
|
||||||
@@ -94,23 +94,6 @@
|
|||||||
file_server
|
file_server
|
||||||
import cache 60s 24h
|
import cache 60s 24h
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_errors {
|
|
||||||
@redirects `{err.status_code} in [301, 302, 307]`
|
|
||||||
reverse_proxy @redirects {
|
|
||||||
header_up Location {http.response.header.Location}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
route /v2* {
|
|
||||||
reverse_proxy https://registry-1.docker.io {
|
|
||||||
header_up Host registry-1.docker.io
|
|
||||||
header_up X-Real-IP {remote}
|
|
||||||
header_up X-Forwarded-For {http.request.header.X-Forwarded-For}
|
|
||||||
header_up X-Forwarded-Proto {scheme}
|
|
||||||
header_up Authorization {http.request.header.Authorization}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
import /data/caddy/config.d/*
|
import /data/caddy/config.d/*
|
||||||
|
|||||||
8
config/blacklist.json
Normal file
8
config/blacklist.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"blacklist": [
|
||||||
|
"test/test1",
|
||||||
|
"example/repo2",
|
||||||
|
"another/repo3"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
@@ -7,25 +7,46 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Port int `yaml:"port"`
|
Server struct {
|
||||||
Host string `yaml:"host"`
|
Port int `yaml:"port"`
|
||||||
SizeLimit int `yaml:"sizelimit"`
|
Host string `yaml:"host"`
|
||||||
LogFilePath string `yaml:"logfilepath"`
|
SizeLimit int `yaml:"sizelimit"`
|
||||||
CORSOrigin bool `yaml:"CorsAllowOrigins"`
|
} `yaml:"server"`
|
||||||
Auth bool `yaml:"auth"`
|
|
||||||
AuthToken string `yaml:"authtoken"`
|
Log struct {
|
||||||
|
LogFilePath string `yaml:"logfilepath"`
|
||||||
|
MaxLogSize int `yaml:"maxlogsize"`
|
||||||
|
} `yaml:"logger"`
|
||||||
|
|
||||||
|
CORS struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
} `yaml:"cors"`
|
||||||
|
|
||||||
|
Auth struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
AuthToken string `yaml:"authtoken"`
|
||||||
|
} `yaml:"auth"`
|
||||||
|
|
||||||
|
Blacklist struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
BlacklistFile string `yaml:"blacklistfile"`
|
||||||
|
} `yaml:"blacklist"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig 从 YAML 配置文件加载配置
|
// LoadConfig 从 YAML 配置文件加载配置
|
||||||
func LoadConfig(filePath string) (*Config, error) {
|
func LoadConfig(filePath string) (*Config, error) {
|
||||||
var config Config
|
var config Config
|
||||||
data, err := os.ReadFile(filePath)
|
if err := loadYAML(filePath, &config); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = yaml.Unmarshal(data, &config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadyamlConfig 从 YAML 配置文件加载配置
|
||||||
|
func loadYAML(filePath string, out interface{}) error {
|
||||||
|
data, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return yaml.Unmarshal(data, out)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,24 @@
|
|||||||
port: 8080
|
# Server Configuration
|
||||||
host: "127.0.0.1"
|
server:
|
||||||
sizelimit: 131072000 # 125MB
|
port: 8080
|
||||||
logfilepath: "/data/ghproxy/log/ghproxy-0rtt.log"
|
host: "127.0.0.1"
|
||||||
CorsAllowOrigins: true
|
sizelimit: 131072000 # 125MB
|
||||||
auth: true
|
|
||||||
authtoken: "test"
|
# Logging Configuration
|
||||||
|
logger:
|
||||||
|
logfilepath: "/data/ghproxy/log/ghproxy.log"
|
||||||
|
maxlogsize: 25 # MB
|
||||||
|
|
||||||
|
# CORS Configuration
|
||||||
|
cors:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
# Authentication Configuration
|
||||||
|
auth:
|
||||||
|
enabled: false
|
||||||
|
authtoken: "test"
|
||||||
|
|
||||||
|
# Blacklist Configuration
|
||||||
|
blacklist:
|
||||||
|
enabled: true
|
||||||
|
blacklistfile: "/data/ghproxy/config/blacklist.json"
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ RUN wget -O /data/caddy/Caddyfile https://raw.githubusercontent.com/${USER}/${RE
|
|||||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/DEV-VERSION) && \
|
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/DEV-VERSION) && \
|
||||||
wget -O /data/${APPLICATION}/${APPLICATION} https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}
|
wget -O /data/${APPLICATION}/${APPLICATION} https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}
|
||||||
RUN wget -O /data/${APPLICATION}/config.yaml https://raw.githubusercontent.com/${USER}/${REPO}/main/config/config.yaml
|
RUN wget -O /data/${APPLICATION}/config.yaml https://raw.githubusercontent.com/${USER}/${REPO}/main/config/config.yaml
|
||||||
|
RUN wget -O /data/${APPLICATION}/blacklist.json https://raw.githubusercontent.com/${USER}/${REPO}/main/config/blacklist.json
|
||||||
RUN wget -O /usr/local/bin/init.sh https://raw.githubusercontent.com/${USER}/${REPO}/main/init.sh
|
RUN wget -O /usr/local/bin/init.sh https://raw.githubusercontent.com/${USER}/${REPO}/main/init.sh
|
||||||
RUN chmod +x /data/${APPLICATION}/${APPLICATION}
|
RUN chmod +x /data/${APPLICATION}/${APPLICATION}
|
||||||
RUN chmod +x /usr/local/bin/init.sh
|
RUN chmod +x /usr/local/bin/init.sh
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ RUN wget -O /data/caddy/Caddyfile https://raw.githubusercontent.com/${USER}/${RE
|
|||||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/VERSION) && \
|
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/VERSION) && \
|
||||||
wget -O /data/${APPLICATION}/${APPLICATION} https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}
|
wget -O /data/${APPLICATION}/${APPLICATION} https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}
|
||||||
RUN wget -O /data/${APPLICATION}/config.yaml https://raw.githubusercontent.com/${USER}/${REPO}/main/config/config.yaml
|
RUN wget -O /data/${APPLICATION}/config.yaml https://raw.githubusercontent.com/${USER}/${REPO}/main/config/config.yaml
|
||||||
|
RUN wget -O /data/${APPLICATION}/blacklist.json https://raw.githubusercontent.com/${USER}/${REPO}/main/config/blacklist.json
|
||||||
RUN wget -O /usr/local/bin/init.sh https://raw.githubusercontent.com/${USER}/${REPO}/main/init.sh
|
RUN wget -O /usr/local/bin/init.sh https://raw.githubusercontent.com/${USER}/${REPO}/main/init.sh
|
||||||
RUN chmod +x /data/${APPLICATION}/${APPLICATION}
|
RUN chmod +x /data/${APPLICATION}/${APPLICATION}
|
||||||
RUN chmod +x /usr/local/bin/init.sh
|
RUN chmod +x /usr/local/bin/init.sh
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module ghproxy
|
module ghproxy
|
||||||
|
|
||||||
go 1.23.1
|
go 1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||||
|
|||||||
4
init.sh
4
init.sh
@@ -6,6 +6,10 @@ if [ ! -f /data/caddy/config/Caddyfile ]; then
|
|||||||
cp /data/caddy/Caddyfile /data/caddy/config/Caddyfile
|
cp /data/caddy/Caddyfile /data/caddy/config/Caddyfile
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ! -f /data/${APPLICATON}/config/blacklist.json ]; then
|
||||||
|
cp /data/${APPLICATON}/blacklist.json /data/${APPLICATON}/config/blacklist.json
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! -f /data/${APPLICATON}/config/config.yaml ]; then
|
if [ ! -f /data/${APPLICATON}/config/config.yaml ]; then
|
||||||
cp /data/${APPLICATON}/config.yaml /data/${APPLICATON}/config/config.yaml
|
cp /data/${APPLICATON}/config.yaml /data/${APPLICATON}/config/config.yaml
|
||||||
fi
|
fi
|
||||||
|
|||||||
141
logger/logger.go
141
logger/logger.go
@@ -1,44 +1,159 @@
|
|||||||
// logger/logger.go
|
|
||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"compress/gzip"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logFile *os.File
|
var (
|
||||||
var logger *log.Logger
|
logw = Logw
|
||||||
|
logFile *os.File
|
||||||
|
logger *log.Logger
|
||||||
|
logChannel = make(chan string, 100)
|
||||||
|
quitChannel = make(chan struct{})
|
||||||
|
logFileMutex sync.Mutex // 保护 logFile 的互斥锁
|
||||||
|
)
|
||||||
|
|
||||||
// Init 初始化日志记录器,接受日志文件路径作为参数
|
// Init 初始化日志记录器,接受日志文件路径作为参数
|
||||||
func Init(logFilePath string) error {
|
func Init(logFilePath string, maxLogsize int) error {
|
||||||
|
logFileMutex.Lock()
|
||||||
|
defer logFileMutex.Unlock()
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
logFile, err = os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
logFile, err = os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger = log.New(logFile, "", 0) // 不使用默认前缀
|
logger = log.New(logFile, "", 0)
|
||||||
|
|
||||||
|
go logWorker()
|
||||||
|
go monitorLogSize(logFilePath, maxLogsize)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log 直接记录日志的函数,带有时间戳
|
// logWorker 处理日志记录
|
||||||
func Log(customMessage string) {
|
func logWorker() {
|
||||||
if logger != nil {
|
for {
|
||||||
timestamp := time.Now().Format("02/Jan/2006:15:04:05 -0700") // 使用自定义时间格式
|
select {
|
||||||
logger.Println(timestamp + " - " + customMessage)
|
case msg := <-logChannel:
|
||||||
|
timestamp := time.Now().Format("02/Jan/2006:15:04:05 -0700")
|
||||||
|
logger.Println(timestamp + " - " + msg)
|
||||||
|
case <-quitChannel:
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log 直接记录日志的函数
|
||||||
|
func Log(customMessage string) {
|
||||||
|
logChannel <- customMessage
|
||||||
|
}
|
||||||
|
|
||||||
// Logw 用于格式化日志记录
|
// Logw 用于格式化日志记录
|
||||||
func Logw(format string, args ...interface{}) {
|
func Logw(format string, args ...interface{}) {
|
||||||
message := fmt.Sprintf(format, args...) // 格式化消息
|
message := fmt.Sprintf(format, args...)
|
||||||
Log(message) // 记录日志
|
Log(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close 关闭日志文件
|
// Close 关闭日志文件
|
||||||
func Close() {
|
func Close() {
|
||||||
|
logFileMutex.Lock()
|
||||||
|
defer logFileMutex.Unlock()
|
||||||
|
|
||||||
if logFile != nil {
|
if logFile != nil {
|
||||||
logFile.Close()
|
quitChannel <- struct{}{}
|
||||||
|
if err := logFile.Close(); err != nil {
|
||||||
|
fmt.Printf("Error closing log file: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func monitorLogSize(logFilePath string, maxLogsize int) {
|
||||||
|
var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024 // 最大日志文件大小,单位为MB
|
||||||
|
for {
|
||||||
|
time.Sleep(600 * time.Second) // 每10分钟检查一次
|
||||||
|
logFileMutex.Lock()
|
||||||
|
info, err := logFile.Stat()
|
||||||
|
logFileMutex.Unlock()
|
||||||
|
|
||||||
|
if err == nil && info.Size() > maxLogsizeBytes {
|
||||||
|
if err := rotateLogFile(logFilePath); err != nil {
|
||||||
|
logw("Log Rotation Failed: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rotateLogFile(logFilePath string) error {
|
||||||
|
logFileMutex.Lock()
|
||||||
|
defer logFileMutex.Unlock()
|
||||||
|
|
||||||
|
if logFile != nil {
|
||||||
|
if err := logFile.Close(); err != nil {
|
||||||
|
logw("Error closing log file for rotation: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打开当前日志文件
|
||||||
|
logFile, err := os.Open(logFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open log file: %s, error: %w", logFilePath, err)
|
||||||
|
}
|
||||||
|
defer logFile.Close()
|
||||||
|
|
||||||
|
newLogFilePath := logFilePath + "-" + time.Now().Format("20060102-150405") + ".tar.gz"
|
||||||
|
outFile, err := os.Create(newLogFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create gz file: %s, error: %w", newLogFilePath, err)
|
||||||
|
}
|
||||||
|
defer outFile.Close()
|
||||||
|
|
||||||
|
gzWriter, err := gzip.NewWriterLevel(outFile, gzip.BestCompression)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create gz writer: %w", err)
|
||||||
|
}
|
||||||
|
defer gzWriter.Close()
|
||||||
|
|
||||||
|
tarWriter := tar.NewWriter(gzWriter)
|
||||||
|
defer tarWriter.Close()
|
||||||
|
|
||||||
|
logFileStat, err := logFile.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to stat log file: %s, error: %w", logFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logFileHeader := &tar.Header{
|
||||||
|
Name: filepath.Base(logFilePath),
|
||||||
|
Size: logFileStat.Size(),
|
||||||
|
Mode: 0644,
|
||||||
|
ModTime: logFileStat.ModTime(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tarWriter.WriteHeader(logFileHeader); err != nil {
|
||||||
|
return fmt.Errorf("failed to write log file header: %s, error: %w", logFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(tarWriter, logFile); err != nil {
|
||||||
|
return fmt.Errorf("failed to copy log file: %s, error: %w", logFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Truncate(logFilePath, 0); err != nil {
|
||||||
|
return fmt.Errorf("failed to truncate log file: %s, error: %w", logFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新打开日志文件
|
||||||
|
logFile, err = os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to reopen log file: %s, error: %w", logFilePath, err)
|
||||||
|
}
|
||||||
|
logger.SetOutput(logFile)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
202
main.go
202
main.go
@@ -3,23 +3,24 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"ghproxy/auth"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
"ghproxy/logger"
|
"ghproxy/logger"
|
||||||
|
"ghproxy/proxy"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/imroc/req/v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var cfg *config.Config
|
var (
|
||||||
var logw = logger.Logw
|
cfg *config.Config
|
||||||
var router *gin.Engine
|
logw = logger.Logw
|
||||||
|
router *gin.Engine
|
||||||
|
configfile = "/data/ghproxy/config/config.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
exps = []*regexp.Regexp{
|
exps = []*regexp.Regexp{
|
||||||
@@ -34,7 +35,7 @@ var (
|
|||||||
func loadConfig() {
|
func loadConfig() {
|
||||||
var err error
|
var err error
|
||||||
// 初始化配置
|
// 初始化配置
|
||||||
cfg, err = config.LoadConfig("/data/ghproxy/config/config.yaml")
|
cfg, err = config.LoadConfig(configfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to load config: %v", err)
|
log.Fatalf("Failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
@@ -44,7 +45,7 @@ func loadConfig() {
|
|||||||
func setupLogger() {
|
func setupLogger() {
|
||||||
// 初始化日志模块
|
// 初始化日志模块
|
||||||
var err error
|
var err error
|
||||||
err = logger.Init(cfg.LogFilePath) // 传递日志文件路径
|
err = logger.Init(cfg.Log.LogFilePath, cfg.Log.MaxLogSize) // 传递日志文件路径
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to initialize logger: %v", err)
|
log.Fatalf("Failed to initialize logger: %v", err)
|
||||||
}
|
}
|
||||||
@@ -52,9 +53,14 @@ func setupLogger() {
|
|||||||
logw("Init Completed")
|
logw("Init Completed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadBlacklist(cfg *config.Config) {
|
||||||
|
auth.LoadBlacklist(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
loadConfig()
|
loadConfig()
|
||||||
setupLogger()
|
setupLogger()
|
||||||
|
LoadBlacklist(cfg)
|
||||||
|
|
||||||
// 设置 Gin 模式
|
// 设置 Gin 模式
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
@@ -75,12 +81,14 @@ func init() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 未匹配路由处理
|
// 未匹配路由处理
|
||||||
router.NoRoute(noRouteHandler(cfg))
|
router.NoRoute(func(c *gin.Context) {
|
||||||
|
proxy.NoRouteHandler(cfg)(c)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 启动服务器
|
// 启动服务器
|
||||||
err := router.Run(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
|
err := router.Run(fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error starting server: %v\n", err)
|
log.Fatalf("Error starting server: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -92,176 +100,6 @@ func api(c *gin.Context) {
|
|||||||
// 设置响应头
|
// 设置响应头
|
||||||
c.Writer.Header().Set("Content-Type", "application/json")
|
c.Writer.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
||||||
"MaxResponseBodySize": cfg.SizeLimit,
|
"MaxResponseBodySize": cfg.Server.SizeLimit,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func authHandler(c *gin.Context) bool {
|
|
||||||
if cfg.Auth {
|
|
||||||
authToken := c.Query("auth_token")
|
|
||||||
return authToken == cfg.AuthToken
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func noRouteHandler(config *config.Config) gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
|
|
||||||
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
|
|
||||||
matches := re.FindStringSubmatch(rawPath)
|
|
||||||
|
|
||||||
rawPath = "https://" + matches[2]
|
|
||||||
|
|
||||||
matches = checkURL(rawPath)
|
|
||||||
if matches == nil {
|
|
||||||
c.String(http.StatusForbidden, "Invalid input.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if exps[1].MatchString(rawPath) {
|
|
||||||
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !authHandler(c) {
|
|
||||||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
|
||||||
logw("Unauthorized request: %s", rawPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 日志记录
|
|
||||||
logw("Request: %s %s", c.Request.Method, rawPath)
|
|
||||||
logw("Matches: %v", matches)
|
|
||||||
|
|
||||||
// 代理请求
|
|
||||||
switch {
|
|
||||||
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
|
||||||
logw("%s Matched - USE proxy-chrome", rawPath)
|
|
||||||
proxyRequest(c, rawPath, config, "chrome")
|
|
||||||
case exps[2].MatchString(rawPath):
|
|
||||||
logw("%s Matched - USE proxy-git", rawPath)
|
|
||||||
proxyRequest(c, rawPath, config, "git")
|
|
||||||
default:
|
|
||||||
c.String(http.StatusForbidden, "Invalid input.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func proxyRequest(c *gin.Context, u string, config *config.Config, mode string) {
|
|
||||||
method := c.Request.Method
|
|
||||||
logw("%s Method: %s", u, method)
|
|
||||||
|
|
||||||
client := req.C()
|
|
||||||
|
|
||||||
switch mode {
|
|
||||||
case "chrome":
|
|
||||||
client.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36").
|
|
||||||
SetTLSFingerprintChrome().
|
|
||||||
ImpersonateChrome()
|
|
||||||
case "git":
|
|
||||||
client.SetUserAgent("git/2.33.1")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取请求体
|
|
||||||
body, err := io.ReadAll(c.Request.Body)
|
|
||||||
if err != nil {
|
|
||||||
handleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer c.Request.Body.Close()
|
|
||||||
|
|
||||||
// 创建新的请求
|
|
||||||
req := client.R().SetBody(body)
|
|
||||||
|
|
||||||
// 复制请求头
|
|
||||||
for key, values := range c.Request.Header {
|
|
||||||
for _, value := range values {
|
|
||||||
req.SetHeader(key, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发送请求并处理响应
|
|
||||||
resp, err := sendRequest(req, method, u)
|
|
||||||
if err != nil {
|
|
||||||
handleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// 检查响应内容长度并处理重定向
|
|
||||||
if err := handleResponseSize(resp, config, c); err != nil {
|
|
||||||
logw("Error handling response size: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
copyResponseHeaders(resp, c, config)
|
|
||||||
c.Status(resp.StatusCode)
|
|
||||||
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
|
||||||
logw("Failed to copy response body: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
|
||||||
switch method {
|
|
||||||
case "GET":
|
|
||||||
return req.Get(url)
|
|
||||||
case "POST":
|
|
||||||
return req.Post(url)
|
|
||||||
case "PUT":
|
|
||||||
return req.Put(url)
|
|
||||||
case "DELETE":
|
|
||||||
return req.Delete(url)
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unsupported method: %s", method)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleResponseSize(resp *req.Response, config *config.Config, c *gin.Context) error {
|
|
||||||
contentLength := resp.Header.Get("Content-Length")
|
|
||||||
if contentLength != "" {
|
|
||||||
size, err := strconv.Atoi(contentLength)
|
|
||||||
if err == nil && size > config.SizeLimit {
|
|
||||||
finalURL := resp.Request.URL.String()
|
|
||||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
|
||||||
logw("Redirecting to %s due to size limit (%d bytes)", finalURL, size)
|
|
||||||
return fmt.Errorf("response size exceeds limit")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func copyResponseHeaders(resp *req.Response, c *gin.Context, config *config.Config) {
|
|
||||||
headersToRemove := []string{"Content-Security-Policy", "Referrer-Policy", "Strict-Transport-Security"}
|
|
||||||
|
|
||||||
for _, header := range headersToRemove {
|
|
||||||
resp.Header.Del(header)
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, values := range resp.Header {
|
|
||||||
for _, value := range values {
|
|
||||||
c.Header(key, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.CORSOrigin {
|
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
|
||||||
} else {
|
|
||||||
c.Header("Access-Control-Allow-Origin", "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleError(c *gin.Context, message string) {
|
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
|
||||||
logw(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkURL(u string) []string {
|
|
||||||
for _, exp := range exps {
|
|
||||||
if matches := exp.FindStringSubmatch(u); matches != nil {
|
|
||||||
logw("URL matched: %s, Matches: %v", u, matches[1:])
|
|
||||||
return matches[1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
logw("Invalid URL: %s", u)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
216
proxy/proxy.go
Normal file
216
proxy/proxy.go
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
// proxy/proxy.go 实验性
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"ghproxy/auth"
|
||||||
|
"ghproxy/config"
|
||||||
|
"ghproxy/logger"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/imroc/req/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logw = logger.Logw
|
||||||
|
|
||||||
|
var exps = []*regexp.Regexp{
|
||||||
|
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||||
|
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
||||||
|
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`),
|
||||||
|
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`),
|
||||||
|
regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`),
|
||||||
|
}
|
||||||
|
|
||||||
|
func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
|
||||||
|
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
|
||||||
|
matches := re.FindStringSubmatch(rawPath)
|
||||||
|
|
||||||
|
if len(matches) < 3 {
|
||||||
|
logw("Invalid URL: %s", rawPath)
|
||||||
|
c.String(http.StatusForbidden, "Invalid URL.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rawPath = "https://" + matches[2]
|
||||||
|
|
||||||
|
// 提取用户名和仓库名,格式为 handle/<username>/<repo>/*
|
||||||
|
pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`)
|
||||||
|
pathParts := pathmatches.FindStringSubmatch(matches[2])
|
||||||
|
if len(pathParts) < 4 {
|
||||||
|
logw("Invalid path: %s", rawPath)
|
||||||
|
c.String(http.StatusForbidden, "Invalid path; expected username/repo.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username := pathParts[2]
|
||||||
|
repo := pathParts[3]
|
||||||
|
logw("Blacklist Check > Username: %s, Repo: %s", username, repo)
|
||||||
|
fullrepo := fmt.Sprintf("%s/%s", username, repo)
|
||||||
|
|
||||||
|
// 黑名单检查
|
||||||
|
blacklistpass := auth.CheckBlacklist(fullrepo)
|
||||||
|
if blacklistpass {
|
||||||
|
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo)
|
||||||
|
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||||
|
logw(errMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
matches = CheckURL(rawPath)
|
||||||
|
if matches == nil {
|
||||||
|
c.AbortWithStatus(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if exps[1].MatchString(rawPath) {
|
||||||
|
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !auth.AuthHandler(c, cfg) {
|
||||||
|
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||||
|
logw("Unauthorized request: %s", rawPath)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logw("Matches: %v", matches)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
||||||
|
logw("%s Matched - USE proxy-chrome", rawPath)
|
||||||
|
ProxyRequest(c, rawPath, cfg, "chrome")
|
||||||
|
case exps[2].MatchString(rawPath):
|
||||||
|
logw("%s Matched - USE proxy-git", rawPath)
|
||||||
|
ProxyRequest(c, rawPath, cfg, "git")
|
||||||
|
default:
|
||||||
|
c.String(http.StatusForbidden, "Invalid input.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
||||||
|
method := c.Request.Method
|
||||||
|
logw("%s %s", method, u)
|
||||||
|
|
||||||
|
client := req.C()
|
||||||
|
|
||||||
|
switch mode {
|
||||||
|
case "chrome":
|
||||||
|
client.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36").
|
||||||
|
SetTLSFingerprintChrome().
|
||||||
|
ImpersonateChrome()
|
||||||
|
case "git":
|
||||||
|
client.SetUserAgent("git/2.33.1")
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(c.Request.Body)
|
||||||
|
if err != nil {
|
||||||
|
HandleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Request.Body.Close()
|
||||||
|
|
||||||
|
req := client.R().SetBody(body)
|
||||||
|
|
||||||
|
for key, values := range c.Request.Header {
|
||||||
|
for _, value := range values {
|
||||||
|
req.SetHeader(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := SendRequest(req, method, u)
|
||||||
|
if err != nil {
|
||||||
|
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err := HandleResponseSize(resp, cfg, c); err != nil {
|
||||||
|
logw("Error handling response size: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
CopyResponseHeaders(resp, c, cfg)
|
||||||
|
c.Status(resp.StatusCode)
|
||||||
|
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
||||||
|
logw("Failed to copy response body: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
||||||
|
switch method {
|
||||||
|
case "GET":
|
||||||
|
return req.Get(url)
|
||||||
|
case "POST":
|
||||||
|
return req.Post(url)
|
||||||
|
case "PUT":
|
||||||
|
return req.Put(url)
|
||||||
|
case "DELETE":
|
||||||
|
return req.Delete(url)
|
||||||
|
default:
|
||||||
|
logw("Unsupported method: %s", method)
|
||||||
|
return nil, fmt.Errorf("unsupported method: %s", method)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context) error {
|
||||||
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
|
if contentLength != "" {
|
||||||
|
size, err := strconv.Atoi(contentLength)
|
||||||
|
if err == nil && size > cfg.Server.SizeLimit {
|
||||||
|
finalURL := resp.Request.URL.String()
|
||||||
|
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||||
|
logw("Redirecting to %s due to size limit (%d bytes)", finalURL, size)
|
||||||
|
return fmt.Errorf("response size exceeds limit")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CopyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config) {
|
||||||
|
headersToRemove := map[string]struct{}{
|
||||||
|
"Content-Security-Policy": {},
|
||||||
|
"Referrer-Policy": {},
|
||||||
|
"Strict-Transport-Security": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
for header := range headersToRemove {
|
||||||
|
resp.Header.Del(header)
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, values := range resp.Header {
|
||||||
|
for _, value := range values {
|
||||||
|
c.Header(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Header("Access-Control-Allow-Origin", "")
|
||||||
|
if cfg.CORS.Enabled {
|
||||||
|
c.Header("Access-Control-Allow-Origin", "*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleError(c *gin.Context, message string) {
|
||||||
|
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||||
|
logw(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckURL(u string) []string {
|
||||||
|
for _, exp := range exps {
|
||||||
|
if matches := exp.FindStringSubmatch(u); matches != nil {
|
||||||
|
logw("URL matched: %s, Matches: %v", u, matches[1:])
|
||||||
|
return matches[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errMsg := fmt.Sprintf("Invalid URL: %s", u)
|
||||||
|
logw(errMsg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user