Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5685240b41 | ||
|
|
40f0e3ad06 | ||
|
|
ef783f33c2 | ||
|
|
c478409bf8 | ||
|
|
a53e18cb0b | ||
|
|
0e7abf3411 | ||
|
|
b5db6bcccc | ||
|
|
c1ba935ca4 | ||
|
|
3c247665fc | ||
|
|
3e40146281 | ||
|
|
ac7e1e43b5 | ||
|
|
f134d22540 | ||
|
|
79153c0f7d | ||
|
|
4fd47812f7 | ||
|
|
17c49d534b | ||
|
|
284b38bab4 | ||
|
|
d73dfe7db5 | ||
|
|
dc286e002c |
7
.github/workflows/build-dev.yml
vendored
7
.github/workflows/build-dev.yml
vendored
@@ -59,6 +59,13 @@ jobs:
|
|||||||
else
|
else
|
||||||
echo "DEV-VERSION file not found!" && exit 1
|
echo "DEV-VERSION file not found!" && exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
- name: 拉取前端
|
||||||
|
run: |
|
||||||
|
sudo git clone https://github.com/WJQSERVER-STUDIO/GHPrxoy-Frontend.git pages
|
||||||
|
sudo rm -rf pages/.git/
|
||||||
|
|
||||||
|
|
||||||
- name: 安装 Go
|
- name: 安装 Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
|
|||||||
6
.github/workflows/build.yml
vendored
6
.github/workflows/build.yml
vendored
@@ -56,6 +56,12 @@ jobs:
|
|||||||
else
|
else
|
||||||
echo "VERSION file not found!" && exit 1
|
echo "VERSION file not found!" && exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
- name: 拉取前端
|
||||||
|
run: |
|
||||||
|
sudo git clone https://github.com/WJQSERVER-STUDIO/GHPrxoy-Frontend.git pages
|
||||||
|
sudo rm -rf pages/.git/
|
||||||
|
|
||||||
- name: 安装 Go
|
- name: 安装 Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,5 @@ demo.toml
|
|||||||
*.log
|
*.log
|
||||||
*.bak
|
*.bak
|
||||||
list.json
|
list.json
|
||||||
repos
|
repos
|
||||||
|
pages
|
||||||
109
CHANGELOG.md
109
CHANGELOG.md
@@ -1,11 +1,114 @@
|
|||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
2.4.1 - 2025-03-12
|
25w23a - 2025-03-22
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v2.6.0的预发布版本,请勿在生产环境中使用;
|
||||||
|
- BACKPORT: 将v3的功能性改进反向移植
|
||||||
|
|
||||||
|
e3.0.2 - 2025-03-21
|
||||||
|
---
|
||||||
|
- ATTENTION: 此版本是实验性的, 请确保了解这一点
|
||||||
|
- RELEASE: 在此表达对各位的歉意, v3迁移到HertZ带来了许多问题; 此版本完善v3的同时, 修正已知问题; v3会与v2.4.0及以上版本保证兼容关系, 可平顺升级;
|
||||||
|
- FIX: 使用等效`c.Writer()`, 回归v2.5.0 func以修正问题
|
||||||
|
- CHANGE: 更新相关依赖
|
||||||
|
|
||||||
|
25w22a - 2025-03-21
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用;
|
||||||
|
- FIX: 使用等效`c.Writer()`, 回归v2.5.0 func以修正问题
|
||||||
|
|
||||||
|
e3.0.1 - 2025-03-21
|
||||||
|
---
|
||||||
|
- ATTENTION: 此版本是实验性的, 请确保了解这一点
|
||||||
|
- RELEASE: Next Step; 下一步; 完善v3的同时, 修正已知问题; v3会与v2.4.0及以上版本保证兼容关系, 可平顺升级;
|
||||||
|
- CHANGE: 改进cli
|
||||||
|
- CHANGE: 重写`ProcessLinksAndWriteChunked`(脚本嵌套加速处理器), 修正已知问题的同时提高性能与效率
|
||||||
|
- CHANGE: 完善`gitreq`部分
|
||||||
|
- FIX: 修正日志输出格式问题
|
||||||
|
- FIX: 使用更新的`hwriter`以修正相关问题
|
||||||
|
|
||||||
|
25w21e - 2025-03-21
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用;
|
||||||
|
- CHANGE: 重写`ProcessLinksAndWriteChunked`(脚本嵌套加速处理器), 修正已知问题的同时提高性能与效率
|
||||||
|
|
||||||
|
25w21d - 2025-03-21
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用;
|
||||||
|
- FIX: 使用更新的`hwriter`以修正相关问题
|
||||||
|
|
||||||
|
25w21c - 2025-03-20
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用;
|
||||||
|
- TEST: 测试新的`hwriter`
|
||||||
|
|
||||||
|
25w21b - 2025-03-20
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用;
|
||||||
|
- FIX: 修正日志输出格式问题
|
||||||
|
|
||||||
|
25w21a - 2025-03-20
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.1的预发布版本,请勿在生产环境中使用;
|
||||||
|
- CHANGE: 改进cli
|
||||||
|
- CHANGE: 完善`gitreq`部分
|
||||||
|
|
||||||
|
e3.0.0 - 2025-03-19
|
||||||
|
---
|
||||||
|
- ATTENTION: 此版本是实验性的, 请确保了解这一点
|
||||||
|
- RELEASE: Next Gen; 下一个起点; v3会与v2.4.0及以上版本保证兼容关系, 可平顺升级;
|
||||||
|
- CHANGE: 使用HertZ框架重构, 提升性能
|
||||||
|
- CHANGE: 前端在构建时加入, 新增`Design`,`Metro`,`Classic`主题
|
||||||
|
- CHANGE: 加入`Mino`主题对接选项
|
||||||
|
- FIX: 修正部分日志输出问题
|
||||||
|
- CHANGE: 移除gin残留
|
||||||
|
- CHANGE: 移除无用传入参数, 调整代码结构
|
||||||
|
|
||||||
|
25w20b - 2025-03-19
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.0的预发布版本,请勿在生产环境中使用; v3.0.0会与v2.4.0及以上保证兼容关系, 可平顺升级;
|
||||||
|
- CHANGE: 加入`Mino`主题对接选项
|
||||||
|
- FIX: 修正部分日志输出问题
|
||||||
|
- CHANGE: 移除gin残留
|
||||||
|
- CHANGE: 移除无用传入参数, 调整代码结构
|
||||||
|
|
||||||
|
25w20a - 2025-03-18
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v3.0.0的预发布版本,请勿在生产环境中使用; v3.0.0会与v2.4.0及以上保证兼容关系, 可平顺升级;
|
||||||
|
- CHANGE: 使用HertZ重构
|
||||||
|
- CHANGE: 前端在构建时加入, 新增`Design`,`Metro`,`Classic`主题
|
||||||
|
|
||||||
|
2.5.0 - 2025-03-17
|
||||||
|
---
|
||||||
|
- ADD: 加入脚本嵌套加速功能
|
||||||
|
- CHANGE: 改进Auth模块
|
||||||
|
|
||||||
|
25w19a - 2025-03-16
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v2.5.0的预发布版本,请勿在生产环境中使用;
|
||||||
|
- ADD: 加入脚本嵌套加速功能
|
||||||
|
- CHANGE: 改进Auth模块
|
||||||
|
- CHANGE: 将handler模块化改进
|
||||||
|
|
||||||
|
2.4.2 - 2025-03-14
|
||||||
|
---
|
||||||
|
- CHANGE: 在GitClone Cache模式下, 相关请求会使用独立httpc client
|
||||||
|
- CHANGE: 为GitClone Cache的独立httpc client增加ForceH2C选项
|
||||||
|
- FIX: 修正GitClone Cache模式下的Url生成问题
|
||||||
|
|
||||||
|
25w18a - 2025-03-14
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: 此版本是v2.4.2的预发布版本,请勿在生产环境中使用;
|
||||||
|
- CHANGE: 在GitClone Cache模式下, 相关请求会使用独立httpc client
|
||||||
|
- CHANGE: 为GitClone Cache的独立httpc client增加ForceH2C选项
|
||||||
|
- FIX: 修正GitClone Cache模式下的Url生成问题
|
||||||
|
|
||||||
|
2.4.1 - 2025-03-13
|
||||||
---
|
---
|
||||||
- CHANGE: 重构路由匹配
|
- CHANGE: 重构路由匹配
|
||||||
- CHANGE: 更新相关依赖以修复错误
|
- CHANGE: 更新相关依赖以修复错误
|
||||||
|
|
||||||
25w17a - 2025-03-12
|
25w17a - 2025-03-13
|
||||||
---
|
---
|
||||||
- PRE-RELEASE: 此版本是v2.4.1的预发布版本,请勿在生产环境中使用;
|
- PRE-RELEASE: 此版本是v2.4.1的预发布版本,请勿在生产环境中使用;
|
||||||
- CHANGE: 重构路由匹配
|
- CHANGE: 重构路由匹配
|
||||||
@@ -1066,4 +1169,4 @@ v0.1.0
|
|||||||
- ADD: 实现符合[RFC 7234](https://httpwg.org/specs/rfc7234.html)的HTTP缓存机制
|
- ADD: 实现符合[RFC 7234](https://httpwg.org/specs/rfc7234.html)的HTTP缓存机制
|
||||||
- ADD: 实现action编译
|
- ADD: 实现action编译
|
||||||
- ADD: 实现Docker部署
|
- ADD: 实现Docker部署
|
||||||
- INFO: 使用Caddy作为Web服务器,通过Caddy实现了缓存与速率限制
|
- INFO: 使用Caddy作为Web服务器,通过Caddy实现了缓存与速率限制
|
||||||
@@ -1 +1 @@
|
|||||||
25w17a
|
25w23a
|
||||||
36
README.md
36
README.md
@@ -20,10 +20,13 @@
|
|||||||
- 使用[Gin](https://github.com/gin-gonic/gin)作为Web框架
|
- 使用[Gin](https://github.com/gin-gonic/gin)作为Web框架
|
||||||
- 使用[Touka-HTTPC](https://github.com/satomitouka/touka-httpc)作为HTTP客户端
|
- 使用[Touka-HTTPC](https://github.com/satomitouka/touka-httpc)作为HTTP客户端
|
||||||
- 支持Git clone,raw,realeases等文件拉取
|
- 支持Git clone,raw,realeases等文件拉取
|
||||||
|
- 支持多个前端主题
|
||||||
|
- 支持自定义黑名单/白名单
|
||||||
|
- 支持Git Clone缓存(配合组件)
|
||||||
- 支持Docker部署
|
- 支持Docker部署
|
||||||
- 支持速率限制
|
- 支持速率限制
|
||||||
- 支持用户鉴权
|
- 支持用户鉴权
|
||||||
- 支持自定义黑名单/白名单
|
- 支持shell脚本嵌套加速
|
||||||
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
||||||
|
|
||||||
### 项目开发过程
|
### 项目开发过程
|
||||||
@@ -31,8 +34,9 @@
|
|||||||
**本项目是[WJQSERVER-STUDIO/ghproxy-go](https://github.com/WJQSERVER-STUDIO/ghproxy-go)的重构版本,实现了原项目原定功能的同时,进一步优化了性能**
|
**本项目是[WJQSERVER-STUDIO/ghproxy-go](https://github.com/WJQSERVER-STUDIO/ghproxy-go)的重构版本,实现了原项目原定功能的同时,进一步优化了性能**
|
||||||
关于此项目的详细开发过程,请参看Commit记录与[CHANGELOG.md](https://github.com/WJQSERVER-STUDIO/ghproxy/blob/main/CHANGELOG.md)
|
关于此项目的详细开发过程,请参看Commit记录与[CHANGELOG.md](https://github.com/WJQSERVER-STUDIO/ghproxy/blob/main/CHANGELOG.md)
|
||||||
|
|
||||||
- V2.0.0 对`proxy`核心模块进行了重构,大幅优化内存占用
|
- v2.4.1 对路径匹配进行优化
|
||||||
- V1.0.0 迁移至本仓库,并再次重构内容实现
|
- v2.0.0 对`proxy`核心模块进行了重构,大幅优化内存占用
|
||||||
|
- v1.0.0 迁移至本仓库,并再次重构内容实现
|
||||||
- v0.2.0 重构项目实现
|
- v0.2.0 重构项目实现
|
||||||
|
|
||||||
### LICENSE
|
### LICENSE
|
||||||
@@ -48,9 +52,11 @@
|
|||||||
```
|
```
|
||||||
# 下载文件
|
# 下载文件
|
||||||
https://ghproxy.1888866.xyz/raw.githubusercontent.com/WJQSERVER-STUDIO/tools-stable/main/tools-stable-ghproxy.sh
|
https://ghproxy.1888866.xyz/raw.githubusercontent.com/WJQSERVER-STUDIO/tools-stable/main/tools-stable-ghproxy.sh
|
||||||
|
https://ghproxy.1888866.xyz/https://raw.githubusercontent.com/WJQSERVER-STUDIO/tools-stable/main/tools-stable-ghproxy.sh
|
||||||
|
|
||||||
# 克隆仓库
|
# 克隆仓库
|
||||||
git clone https://ghproxy.1888866.xyz/github.com/WJQSERVER-STUDIO/ghproxy.git
|
git clone https://ghproxy.1888866.xyz/github.com/WJQSERVER-STUDIO/ghproxy.git
|
||||||
|
git clone https://ghproxy.1888866.xyz/https://github.com/WJQSERVER-STUDIO/ghproxy.git
|
||||||
```
|
```
|
||||||
|
|
||||||
## 部署说明
|
## 部署说明
|
||||||
@@ -105,6 +111,10 @@ maxConnsPerHost = 0 # only for advanced mode 仅用于高级模式
|
|||||||
[gitclone]
|
[gitclone]
|
||||||
mode = "bypass" # bypass / cache 运行模式, cache模式依赖smart-git
|
mode = "bypass" # bypass / cache 运行模式, cache模式依赖smart-git
|
||||||
smartGitAddr = "http://127.0.0.1:8080" # smart-git组件地址
|
smartGitAddr = "http://127.0.0.1:8080" # smart-git组件地址
|
||||||
|
ForceH2C = false # 强制使用H2C连接
|
||||||
|
|
||||||
|
[shell]
|
||||||
|
editor = false # 脚本嵌套加速
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
mode = "internal" # "internal" or "external" 内部/外部 前端 默认内部
|
mode = "internal" # "internal" or "external" 内部/外部 前端 默认内部
|
||||||
@@ -120,6 +130,7 @@ level = "info" # 日志级别 dump, debug, info, warn, error, none
|
|||||||
authMethod = "parameters" # 鉴权方式,支持parameters,header
|
authMethod = "parameters" # 鉴权方式,支持parameters,header
|
||||||
authToken = "token" # 用户鉴权Token
|
authToken = "token" # 用户鉴权Token
|
||||||
enabled = false # 是否开启用户鉴权
|
enabled = false # 是否开启用户鉴权
|
||||||
|
ForceAllowApi = false # 在不开启Header鉴权的情况下允许api代理
|
||||||
|
|
||||||
[blacklist]
|
[blacklist]
|
||||||
blacklistFile = "/data/ghproxy/config/blacklist.json" # 黑名单文件路径
|
blacklistFile = "/data/ghproxy/config/blacklist.json" # 黑名单文件路径
|
||||||
@@ -170,19 +181,16 @@ url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890" 支持Socks5/HTTP(S)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Caddy反代配置
|
|
||||||
|
|
||||||
```Caddyfile
|
|
||||||
example.com {
|
|
||||||
reverse_proxy * 127.0.0.1:7210
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 前端页面
|
### 前端页面
|
||||||
|
|
||||||
|
#### Bootstrap主题
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
|
#### Nebula主题
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
## 赞助
|
## 赞助
|
||||||
|
|
||||||
如果您觉得本项目对您有帮助,欢迎赞助支持,您的赞助将用于Demo服务器开支及开发者时间成本支出,感谢您的支持!
|
如果您觉得本项目对您有帮助,欢迎赞助支持,您的赞助将用于Demo服务器开支及开发者时间成本支出,感谢您的支持!
|
||||||
@@ -191,6 +199,10 @@ example.com {
|
|||||||
|
|
||||||
爱发电: https://afdian.com/a/wjqserver
|
爱发电: https://afdian.com/a/wjqserver
|
||||||
|
|
||||||
|
USDT(TRC20): `TNfSYG6F2vkiibd6J6mhhHNWDgWgNdF5hN`
|
||||||
|
|
||||||
### 捐赠列表
|
### 捐赠列表
|
||||||
|
|
||||||
虚位以待...
|
| 赞助人 |金额|
|
||||||
|
|--------|------|
|
||||||
|
| starry | 8 USDT (TRC20) |
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
|
|
||||||
| 版本 | 是否支持 |
|
| 版本 | 是否支持 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| v2.x.x | :white_check_mark: 当前最新版本序列, 受支持 |
|
| v3.x.x | :white_check_mark: 接受issue, 实验性 |
|
||||||
|
| v2.x.x | :white_check_mark: 受支持, 正在维护 |
|
||||||
| v1.x.x | :x: 这些版本已结束生命周期,不再受支持 |
|
| v1.x.x | :x: 这些版本已结束生命周期,不再受支持 |
|
||||||
| 25w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 |
|
| 25w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 |
|
||||||
| 24w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 生命周期已完全结束 |
|
| 24w*a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 生命周期已完全结束 |
|
||||||
|
|||||||
13
api/api.go
13
api/api.go
@@ -15,7 +15,7 @@ var (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
logw = logger.Logw
|
logw = logger.Logw
|
||||||
LogDump = logger.LogDump
|
logDump = logger.LogDump
|
||||||
logDebug = logger.LogDebug
|
logDebug = logger.LogDebug
|
||||||
logInfo = logger.LogInfo
|
logInfo = logger.LogInfo
|
||||||
logWarning = logger.LogWarning
|
logWarning = logger.LogWarning
|
||||||
@@ -59,6 +59,9 @@ func InitHandleRouter(cfg *config.Config, router *gin.Engine, version string) {
|
|||||||
apiRouter.GET("/rate_limit/limit", func(c *gin.Context) {
|
apiRouter.GET("/rate_limit/limit", func(c *gin.Context) {
|
||||||
RateLimitLimitHandler(c, cfg)
|
RateLimitLimitHandler(c, cfg)
|
||||||
})
|
})
|
||||||
|
apiRouter.GET("/smartgit/status", func(c *gin.Context) {
|
||||||
|
SmartGitStatusHandler(c, cfg)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
logInfo("API router Init success")
|
logInfo("API router Init success")
|
||||||
}
|
}
|
||||||
@@ -127,3 +130,11 @@ func RateLimitLimitHandler(c *gin.Context, cfg *config.Config) {
|
|||||||
"RatePerMinute": cfg.RateLimit.RatePerMinute,
|
"RatePerMinute": cfg.RateLimit.RatePerMinute,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SmartGitStatusHandler(c *gin.Context, cfg *config.Config) {
|
||||||
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
||||||
|
c.Writer.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
||||||
|
"enabled": cfg.GitClone.Mode == "cache",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,23 +7,21 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AuthHeaderHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
|
func AuthHeaderHandler(c *gin.Context, cfg *config.Config) (isValid bool, err error) {
|
||||||
if !cfg.Auth.Enabled {
|
if !cfg.Auth.Enabled {
|
||||||
return true, ""
|
return true, nil
|
||||||
}
|
}
|
||||||
// 获取"GH-Auth"的值
|
// 获取"GH-Auth"的值
|
||||||
authToken := c.GetHeader("GH-Auth")
|
authToken := c.GetHeader("GH-Auth")
|
||||||
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)
|
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 == "" {
|
if authToken == "" {
|
||||||
err := "Auth Header == nil"
|
return false, fmt.Errorf("Auth token not found")
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid = authToken == cfg.Auth.AuthToken
|
isValid = authToken == cfg.Auth.AuthToken
|
||||||
if !isValid {
|
if !isValid {
|
||||||
err := fmt.Sprintf("Auth token incorrect: %s", authToken)
|
return false, fmt.Errorf("Auth token incorrect")
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return isValid, ""
|
return isValid, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,24 +7,22 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AuthParametersHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
|
func AuthParametersHandler(c *gin.Context, cfg *config.Config) (isValid bool, err error) {
|
||||||
if !cfg.Auth.Enabled {
|
if !cfg.Auth.Enabled {
|
||||||
return true, ""
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
authToken := c.Query("auth_token")
|
authToken := c.Query("auth_token")
|
||||||
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)
|
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 == "" {
|
if authToken == "" {
|
||||||
err := "Auth token == nil"
|
return false, fmt.Errorf("Auth token not found")
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid = authToken == cfg.Auth.AuthToken
|
isValid = authToken == cfg.Auth.AuthToken
|
||||||
if !isValid {
|
if !isValid {
|
||||||
err := fmt.Sprintf("Auth token incorrect: %s", authToken)
|
return false, fmt.Errorf("Auth token invalid")
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return isValid, ""
|
return isValid, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
|
|
||||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||||
@@ -9,7 +10,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
logw = logger.Logw
|
logw = logger.Logw
|
||||||
LogDump = logger.LogDump
|
logDump = logger.LogDump
|
||||||
logDebug = logger.LogDebug
|
logDebug = logger.LogDebug
|
||||||
logInfo = logger.LogInfo
|
logInfo = logger.LogInfo
|
||||||
logWarning = logger.LogWarning
|
logWarning = logger.LogWarning
|
||||||
@@ -34,7 +35,7 @@ func Init(cfg *config.Config) {
|
|||||||
logDebug("Auth Init")
|
logDebug("Auth Init")
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
|
func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err error) {
|
||||||
if cfg.Auth.AuthMethod == "parameters" {
|
if cfg.Auth.AuthMethod == "parameters" {
|
||||||
isValid, err = AuthParametersHandler(c, cfg)
|
isValid, err = AuthParametersHandler(c, cfg)
|
||||||
return isValid, err
|
return isValid, err
|
||||||
@@ -43,9 +44,9 @@ func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string)
|
|||||||
return isValid, err
|
return isValid, err
|
||||||
} else if cfg.Auth.AuthMethod == "" {
|
} else if cfg.Auth.AuthMethod == "" {
|
||||||
logError("Auth method not set")
|
logError("Auth method not set")
|
||||||
return true, ""
|
return true, nil
|
||||||
} else {
|
} else {
|
||||||
logError("Auth method not supported")
|
logError("Auth method not supported")
|
||||||
return false, "Auth method not supported"
|
return false, fmt.Errorf(fmt.Sprintf("Auth method %s not supported", cfg.Auth.AuthMethod))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ type Config struct {
|
|||||||
Server ServerConfig
|
Server ServerConfig
|
||||||
Httpc HttpcConfig
|
Httpc HttpcConfig
|
||||||
GitClone GitCloneConfig
|
GitClone GitCloneConfig
|
||||||
|
Shell ShellConfig
|
||||||
Pages PagesConfig
|
Pages PagesConfig
|
||||||
Log LogConfig
|
Log LogConfig
|
||||||
Auth AuthConfig
|
Auth AuthConfig
|
||||||
@@ -23,7 +24,6 @@ host = "0.0.0.0" # 监听地址
|
|||||||
port = 8080 # 监听端口
|
port = 8080 # 监听端口
|
||||||
sizeLimit = 125 # 125MB
|
sizeLimit = 125 # 125MB
|
||||||
H2C = true # 是否开启H2C传输
|
H2C = true # 是否开启H2C传输
|
||||||
enableH2C = "on" # 是否开启H2C传输(latest和dev版本请开启) on/off (2.4.0弃用)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
@@ -32,7 +32,6 @@ type ServerConfig struct {
|
|||||||
SizeLimit int `toml:"sizeLimit"`
|
SizeLimit int `toml:"sizeLimit"`
|
||||||
H2C bool `toml:"H2C"`
|
H2C bool `toml:"H2C"`
|
||||||
Cors string `toml:"cors"`
|
Cors string `toml:"cors"`
|
||||||
EnableH2C string `toml:"enableH2C"`
|
|
||||||
Debug bool `toml:"debug"`
|
Debug bool `toml:"debug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,23 +52,31 @@ type HttpcConfig struct {
|
|||||||
/*
|
/*
|
||||||
[gitclone]
|
[gitclone]
|
||||||
mode = "bypass" # bypass / cache
|
mode = "bypass" # bypass / cache
|
||||||
smartGitAddr = ":8080"
|
smartGitAddr = "http://127.0.0.1:8080"
|
||||||
|
ForceH2C = true
|
||||||
*/
|
*/
|
||||||
type GitCloneConfig struct {
|
type GitCloneConfig struct {
|
||||||
Mode string `toml:"mode"`
|
Mode string `toml:"mode"`
|
||||||
SmartGitAddr string `toml:"smartGitAddr"`
|
SmartGitAddr string `toml:"smartGitAddr"`
|
||||||
|
ForceH2C bool `toml:"ForceH2C"`
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
[shell]
|
||||||
|
editor = true
|
||||||
|
*/
|
||||||
|
type ShellConfig struct {
|
||||||
|
Editor bool `toml:"editor"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
[pages]
|
[pages]
|
||||||
mode = "internal" # "internal" or "external"
|
mode = "internal" # "internal" or "external"
|
||||||
enabled = false
|
|
||||||
theme = "bootstrap" # "bootstrap" or "nebula"
|
theme = "bootstrap" # "bootstrap" or "nebula"
|
||||||
staticDir = "/data/www"
|
staticDir = "/data/www"
|
||||||
*/
|
*/
|
||||||
type PagesConfig struct {
|
type PagesConfig struct {
|
||||||
Mode string `toml:"mode"`
|
Mode string `toml:"mode"`
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
Theme string `toml:"theme"`
|
Theme string `toml:"theme"`
|
||||||
StaticDir string `toml:"staticDir"`
|
StaticDir string `toml:"staticDir"`
|
||||||
}
|
}
|
||||||
@@ -80,11 +87,20 @@ type LogConfig struct {
|
|||||||
Level string `toml:"level"`
|
Level string `toml:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
[auth]
|
||||||
|
authMethod = "parameters" # "header" or "parameters"
|
||||||
|
authToken = "token"
|
||||||
|
enabled = false
|
||||||
|
passThrough = false
|
||||||
|
ForceAllowApi = true
|
||||||
|
*/
|
||||||
type AuthConfig struct {
|
type AuthConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled"`
|
||||||
AuthMethod string `toml:"authMethod"`
|
AuthMethod string `toml:"authMethod"`
|
||||||
AuthToken string `toml:"authToken"`
|
AuthToken string `toml:"authToken"`
|
||||||
PassThrough bool `toml:"passThrough"`
|
PassThrough bool `toml:"passThrough"`
|
||||||
|
ForceAllowApi bool `toml:"ForceAllowApi"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BlacklistConfig struct {
|
type BlacklistConfig struct {
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ maxConnsPerHost = 0 # only for advanced mode
|
|||||||
[gitclone]
|
[gitclone]
|
||||||
mode = "bypass" # bypass / cache
|
mode = "bypass" # bypass / cache
|
||||||
smartGitAddr = "http://127.0.0.1:8080"
|
smartGitAddr = "http://127.0.0.1:8080"
|
||||||
|
ForceH2C = false
|
||||||
|
|
||||||
|
[shell]
|
||||||
|
editor = false
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
mode = "internal" # "internal" or "external"
|
mode = "internal" # "internal" or "external"
|
||||||
@@ -31,6 +35,7 @@ authMethod = "parameters" # "header" or "parameters"
|
|||||||
authToken = "token"
|
authToken = "token"
|
||||||
enabled = false
|
enabled = false
|
||||||
passThrough = false
|
passThrough = false
|
||||||
|
ForceAllowApi = false
|
||||||
|
|
||||||
[blacklist]
|
[blacklist]
|
||||||
blacklistFile = "/data/ghproxy/config/blacklist.json"
|
blacklistFile = "/data/ghproxy/config/blacklist.json"
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ maxConnsPerHost = 0 # only for advanced mode
|
|||||||
[gitclone]
|
[gitclone]
|
||||||
mode = "bypass" # bypass / cache
|
mode = "bypass" # bypass / cache
|
||||||
smartGitAddr = "http://127.0.0.1:8080"
|
smartGitAddr = "http://127.0.0.1:8080"
|
||||||
|
ForceH2C = false
|
||||||
|
|
||||||
|
[shell]
|
||||||
|
editor = false
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
mode = "internal" # "internal" or "external"
|
mode = "internal" # "internal" or "external"
|
||||||
@@ -31,6 +35,7 @@ authMethod = "parameters" # "header" or "parameters"
|
|||||||
authToken = "token"
|
authToken = "token"
|
||||||
enabled = false
|
enabled = false
|
||||||
passThrough = false
|
passThrough = false
|
||||||
|
ForceAllowApi = false
|
||||||
|
|
||||||
[blacklist]
|
[blacklist]
|
||||||
blacklistFile = "/usr/local/ghproxy/config/blacklist.json"
|
blacklistFile = "/usr/local/ghproxy/config/blacklist.json"
|
||||||
|
|||||||
@@ -1,120 +0,0 @@
|
|||||||
package gitclone
|
|
||||||
|
|
||||||
import (
|
|
||||||
"archive/tar"
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
|
||||||
"github.com/pierrec/lz4"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CloneRepo(dir string, repoName string, repoUrl string) error {
|
|
||||||
repoPath := dir
|
|
||||||
_, err := git.PlainClone(repoPath, true, &git.CloneOptions{
|
|
||||||
URL: repoUrl,
|
|
||||||
Progress: os.Stdout,
|
|
||||||
Mirror: true,
|
|
||||||
})
|
|
||||||
if err != nil && !errors.Is(err, git.ErrRepositoryAlreadyExists) {
|
|
||||||
fmt.Printf("Fail to clone: %v\n", err)
|
|
||||||
} else if err != nil && errors.Is(err, git.ErrRepositoryAlreadyExists) {
|
|
||||||
// 移除文件夹
|
|
||||||
fmt.Printf("Repository already exists\n")
|
|
||||||
err = os.RemoveAll(repoPath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Fail to remove: %v\n", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = git.PlainClone(repoPath, true, &git.CloneOptions{
|
|
||||||
URL: repoUrl,
|
|
||||||
Progress: os.Stdout,
|
|
||||||
Mirror: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Fail to clone: %v\n", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 压缩
|
|
||||||
err = CompressRepo(repoPath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Fail to compress: %v\n", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CompressRepo 将指定的仓库压缩成 LZ4 格式的压缩包
|
|
||||||
func CompressRepo(repoPath string) error {
|
|
||||||
lz4File, err := os.Create(repoPath + ".lz4")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create LZ4 file: %w", err)
|
|
||||||
}
|
|
||||||
defer lz4File.Close()
|
|
||||||
|
|
||||||
// 创建 LZ4 编码器
|
|
||||||
lz4Writer := lz4.NewWriter(lz4File)
|
|
||||||
defer lz4Writer.Close()
|
|
||||||
|
|
||||||
// 创建 tar.Writer
|
|
||||||
tarBuffer := new(bytes.Buffer)
|
|
||||||
tarWriter := tar.NewWriter(tarBuffer)
|
|
||||||
|
|
||||||
// 遍历仓库目录并打包
|
|
||||||
err = filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建 tar 文件头
|
|
||||||
header, err := tar.FileInfoHeader(info, "")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
header.Name, err = filepath.Rel(repoPath, path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入 tar 文件头
|
|
||||||
if err := tarWriter.WriteHeader(header); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是文件,写入文件内容
|
|
||||||
if !info.IsDir() {
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(tarWriter, file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to walk through repo directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭 tar.Writer
|
|
||||||
if err := tarWriter.Close(); err != nil {
|
|
||||||
return fmt.Errorf("failed to close tar writer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将 tar 数据写入 LZ4 压缩包
|
|
||||||
if _, err := lz4Writer.Write(tarBuffer.Bytes()); err != nil {
|
|
||||||
return fmt.Errorf("failed to write to LZ4 file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package gitclone
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
logw = logger.Logw
|
|
||||||
logDump = logger.LogDump
|
|
||||||
logDebug = logger.LogDebug
|
|
||||||
logInfo = logger.LogInfo
|
|
||||||
logWarning = logger.LogWarning
|
|
||||||
logError = logger.LogError
|
|
||||||
)
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
package gitclone
|
|
||||||
|
|
||||||
/*
|
|
||||||
package gitclone
|
|
||||||
|
|
||||||
import (
|
|
||||||
"compress/gzip"
|
|
||||||
"ghproxy/config"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/go-git/go-billy/v5/osfs"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/pktline"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/server"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MIT https://github.com/erred/gitreposerver
|
|
||||||
|
|
||||||
// httpInfoRefs 函数处理 /info/refs 请求,用于 Git 客户端获取仓库的引用信息。
|
|
||||||
// 返回一个 gin.HandlerFunc 类型的处理函数。
|
|
||||||
func HttpInfoRefs(cfg *config.Config) gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
|
|
||||||
repo := c.Param("repo") // 从 Gin 上下文中获取路由参数 "repo",即仓库名
|
|
||||||
username := c.Param("username")
|
|
||||||
repoName := repo
|
|
||||||
dir := cfg.GitClone.Dir + "/" + username + "/" + repo
|
|
||||||
url := "https://github.com/" + username + "/" + repo
|
|
||||||
|
|
||||||
// 输出 repo user dir url
|
|
||||||
logInfo("Repo: %s, User: %s, Dir: %s, Url: %s\n", repoName, username, dir, url)
|
|
||||||
|
|
||||||
_, err := os.Stat(dir) // 检查目录是否存在
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
CloneRepo(dir, repoName, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查请求参数 "service" 是否为 "git-upload-pack"。
|
|
||||||
// 这是为了确保只处理 smart git 的 upload-pack 服务请求。
|
|
||||||
if c.Query("service") != "git-upload-pack" {
|
|
||||||
c.String(http.StatusForbidden, "only smart git") // 如果 service 参数不正确,返回 403 Forbidden 状态码和错误信息
|
|
||||||
log.Printf("Request to /info/refs with invalid service: %s, repo: %s\n", c.Query("service"), repoName) // 记录无效 service 参数的日志
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Header("content-type", "application/x-git-upload-pack-advertisement") // 设置 HTTP 响应头的 Content-Type 为 advertisement 类型。
|
|
||||||
// 这种类型用于告知客户端服务器支持的 Git 服务。
|
|
||||||
|
|
||||||
ep, err := transport.NewEndpoint("/") // 创建一个新的传输端点 (Endpoint)。这里使用根路径 "/" 作为端点,表示本地文件系统。
|
|
||||||
if err != nil { // 检查创建端点是否出错
|
|
||||||
log.Printf("Error creating endpoint: %v, repo: %s\n", err, repoName) // 记录创建端点错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs := osfs.New(dir) // 创建一个基于本地文件系统的 billy Filesystem (bfs)。dir 变量指定了仓库的根目录。
|
|
||||||
ld := server.NewFilesystemLoader(bfs) // 创建一个基于文件系统的仓库加载器 (Loader)。Loader 负责从文件系统中加载仓库。
|
|
||||||
svr := server.NewServer(ld) // 创建一个新的 Git 服务器 (Server)。Server 负责处理 Git 服务请求。
|
|
||||||
sess, err := svr.NewUploadPackSession(ep, nil) // 创建一个新的 upload-pack 会话 (Session)。Session 用于处理客户端的 upload-pack 请求。
|
|
||||||
if err != nil { // 检查创建会话是否出错
|
|
||||||
log.Printf("Error creating upload pack session: %v, repo: %s\n", err, repoName) // 记录创建会话错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
ar, err := sess.AdvertisedReferencesContext(c.Request.Context()) // 获取已通告的引用 (Advertised References)。Advertised References 包含了仓库的分支、标签等信息。
|
|
||||||
if err != nil { // 检查获取 Advertised References 是否出错
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
log.Printf("Error getting advertised references: %v, repo: %s\n", err, repoName) // 记录获取 Advertised References 错误日志
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置 Advertised References 的前缀 (Prefix)。
|
|
||||||
// Prefix 通常包含 # service=git-upload-pack 和 pktline.Flush。
|
|
||||||
// # service=git-upload-pack 用于告知客户端服务器提供的是 upload-pack 服务。
|
|
||||||
// pktline.Flush 用于在 pkt-line 格式中发送 flush-pkt。
|
|
||||||
ar.Prefix = [][]byte{
|
|
||||||
[]byte("# service=git-upload-pack"), // 服务类型声明
|
|
||||||
pktline.Flush, // pkt-line flush 信号
|
|
||||||
}
|
|
||||||
err = ar.Encode(c.Writer) // 将 Advertised References 编码并写入 HTTP 响应。使用 pkt-line 格式进行编码。
|
|
||||||
if err != nil { // 检查编码和写入是否出错
|
|
||||||
log.Printf("Error encoding advertised references: %v, repo: %s\n", err, repoName) // 记录编码错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// httpGitUploadPack 函数处理 /git-upload-pack 请求,用于处理 Git 客户端的推送 (push) 操作。
|
|
||||||
// 返回一个 gin.HandlerFunc 类型的处理函数。
|
|
||||||
func HttpGitUploadPack(cfg *config.Config) gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
|
|
||||||
repo := c.Param("repo") // 从 Gin 上下文中获取路由参数 "repo",即仓库名
|
|
||||||
username := c.Param("username")
|
|
||||||
repoName := repo
|
|
||||||
dir := cfg.GitClone.Dir + "/" + username + "/" + repo
|
|
||||||
|
|
||||||
c.Header("content-type", "application/x-git-upload-pack-result") // 设置 HTTP 响应头的 Content-Type 为 result 类型。
|
|
||||||
// 这种类型用于返回 upload-pack 操作的结果。
|
|
||||||
|
|
||||||
var bodyReader io.Reader = c.Request.Body // 初始化 bodyReader 为 HTTP 请求的 body。用于读取客户端发送的数据。
|
|
||||||
// 检查请求头 "Content-Encoding" 是否为 "gzip"。
|
|
||||||
// 如果是 gzip,则需要使用 gzip 解压缩请求 body。
|
|
||||||
if c.GetHeader("Content-Encoding") == "gzip" {
|
|
||||||
gzipReader, err := gzip.NewReader(c.Request.Body) // 创建一个新的 gzip Reader,用于解压缩请求 body。
|
|
||||||
if err != nil { // 检查创建 gzip Reader 是否出错
|
|
||||||
log.Printf("Error creating gzip reader: %v, repo: %s\n", err, repoName) // 记录创建 gzip Reader 错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
defer gzipReader.Close() // 延迟关闭 gzip Reader,确保资源释放
|
|
||||||
bodyReader = gzipReader // 将 bodyReader 替换为 gzip Reader,后续从 gzip Reader 中读取数据
|
|
||||||
}
|
|
||||||
|
|
||||||
upr := packp.NewUploadPackRequest() // 创建一个新的 UploadPackRequest 对象。UploadPackRequest 用于解码客户端发送的 upload-pack 请求数据。
|
|
||||||
err := upr.Decode(bodyReader) // 解码请求 body 中的数据到 UploadPackRequest 对象中。使用 packp 协议格式进行解码。
|
|
||||||
if err != nil { // 检查解码是否出错
|
|
||||||
log.Printf("Error decoding upload pack request: %v, repo: %s\n", err, repoName) // 记录解码错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
ep, err := transport.NewEndpoint("/") // 创建一个新的传输端点 (Endpoint)。这里使用根路径 "/" 作为端点,表示本地文件系统。
|
|
||||||
if err != nil { // 检查创建端点是否出错
|
|
||||||
log.Printf("Error creating endpoint: %v, repo: %s\n", err, repoName) // 记录创建端点错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs := osfs.New(dir) // 创建一个基于本地文件系统的 billy Filesystem (bfs)。dir 变量指定了仓库的根目录。
|
|
||||||
ld := server.NewFilesystemLoader(bfs) // 创建一个基于文件系统的仓库加载器 (Loader)。Loader 负责从文件系统中加载仓库。
|
|
||||||
svr := server.NewServer(ld) // 创建一个新的 Git 服务器 (Server)。Server 负责处理 Git 服务请求。
|
|
||||||
sess, err := svr.NewUploadPackSession(ep, nil) // 创建一个新的 upload-pack 会话 (Session)。Session 用于处理客户端的 upload-pack 请求。
|
|
||||||
if err != nil { // 检查创建会话是否出错
|
|
||||||
log.Printf("Error creating upload pack session: %v, repo: %s\n", err, repoName) // 记录创建会话错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := sess.UploadPack(c.Request.Context(), upr) // 处理 upload-pack 请求,执行实际的仓库推送操作。
|
|
||||||
// sess.UploadPack 函数接收 context 和 UploadPackRequest 对象作为参数,返回 UploadPackResult 和 error。
|
|
||||||
if err != nil { // 检查 UploadPack 操作是否出错
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
log.Printf("Error during upload pack: %v, repo: %s\n", err, repoName) // 记录 UploadPack 操作错误日志
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
|
|
||||||
err = res.Encode(c.Writer) // 将 UploadPackResult 编码并写入 HTTP 响应。使用 pkt-line 格式进行编码。
|
|
||||||
if err != nil { // 检查编码和写入是否出错
|
|
||||||
log.Printf("Error encoding upload pack result: %v, repo: %s\n", err, repoName) // 记录编码错误日志
|
|
||||||
c.String(http.StatusInternalServerError, err.Error()) // 返回 500 Internal Server Error 状态码和错误信息
|
|
||||||
return // 结束处理
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
89
main.go
89
main.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"ghproxy/api"
|
"ghproxy/api"
|
||||||
@@ -23,22 +24,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
router *gin.Engine
|
router *gin.Engine
|
||||||
configfile = "/data/ghproxy/config/config.toml"
|
configfile = "/data/ghproxy/config/config.toml"
|
||||||
cfgfile string
|
cfgfile string
|
||||||
version string
|
version string
|
||||||
dev string
|
dev string
|
||||||
runMode string
|
runMode string
|
||||||
limiter *rate.RateLimiter
|
limiter *rate.RateLimiter
|
||||||
iplimiter *rate.IPRateLimiter
|
iplimiter *rate.IPRateLimiter
|
||||||
|
showVersion bool
|
||||||
|
showHelp bool
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//go:embed pages/bootstrap/*
|
//go:embed pages/*
|
||||||
pagesFS embed.FS
|
pagesFS embed.FS
|
||||||
//go:embed pages/nebula/*
|
|
||||||
NebulaPagesFS embed.FS
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -52,6 +53,38 @@ var (
|
|||||||
|
|
||||||
func readFlag() {
|
func readFlag() {
|
||||||
flag.StringVar(&cfgfile, "cfg", configfile, "config file path")
|
flag.StringVar(&cfgfile, "cfg", configfile, "config file path")
|
||||||
|
flag.BoolVar(&showVersion, "v", false, "show version and exit") // 添加-v标志
|
||||||
|
flag.BoolVar(&showHelp, "h", false, "show help message and exit") // 添加-h标志
|
||||||
|
|
||||||
|
// 捕获未定义的 flag
|
||||||
|
flag.Usage = func() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||||
|
flag.PrintDefaults()
|
||||||
|
fmt.Fprintln(os.Stderr, "\nInvalid flags:")
|
||||||
|
|
||||||
|
// 检查未定义的flags
|
||||||
|
invalidFlags := []string{}
|
||||||
|
for _, arg := range os.Args[1:] {
|
||||||
|
if arg[0] == '-' && arg != "-h" && arg != "-v" { // 检查是否是flag, 排除 -h 和 -v
|
||||||
|
defined := false
|
||||||
|
flag.VisitAll(func(f *flag.Flag) {
|
||||||
|
if "-"+f.Name == arg {
|
||||||
|
defined = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if !defined {
|
||||||
|
invalidFlags = append(invalidFlags, arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, flag := range invalidFlags {
|
||||||
|
fmt.Fprintf(os.Stderr, " %s\n", flag)
|
||||||
|
}
|
||||||
|
if len(invalidFlags) > 0 {
|
||||||
|
os.Exit(2) // 使用非零状态码退出,表示有错误
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() {
|
func loadConfig() {
|
||||||
@@ -59,8 +92,11 @@ func loadConfig() {
|
|||||||
cfg, err = config.LoadConfig(cfgfile)
|
cfg, err = config.LoadConfig(cfgfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to load config: %v\n", err)
|
fmt.Printf("Failed to load config: %v\n", err)
|
||||||
|
// 如果配置文件加载失败,也显示帮助信息并退出
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if cfg.Server.Debug {
|
if cfg != nil && cfg.Server.Debug { // 确保 cfg 不为 nil
|
||||||
fmt.Println("Config File Path: ", cfgfile)
|
fmt.Println("Config File Path: ", cfgfile)
|
||||||
fmt.Printf("Loaded config: %v\n", cfg)
|
fmt.Printf("Loaded config: %v\n", cfg)
|
||||||
}
|
}
|
||||||
@@ -110,12 +146,19 @@ func InitReq(cfg *config.Config) {
|
|||||||
func loadEmbeddedPages(cfg *config.Config) (fs.FS, error) {
|
func loadEmbeddedPages(cfg *config.Config) (fs.FS, error) {
|
||||||
var pages fs.FS
|
var pages fs.FS
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
switch cfg.Pages.Theme {
|
switch cfg.Pages.Theme {
|
||||||
case "bootstrap":
|
case "bootstrap":
|
||||||
pages, err = fs.Sub(pagesFS, "pages/bootstrap")
|
pages, err = fs.Sub(pagesFS, "pages/bootstrap")
|
||||||
case "nebula":
|
case "nebula":
|
||||||
pages, err = fs.Sub(NebulaPagesFS, "pages/nebula")
|
pages, err = fs.Sub(pagesFS, "pages/nebula")
|
||||||
|
case "design":
|
||||||
|
pages, err = fs.Sub(pagesFS, "pages/design")
|
||||||
|
case "metro":
|
||||||
|
pages, err = fs.Sub(pagesFS, "pages/metro")
|
||||||
|
case "classic":
|
||||||
|
pages, err = fs.Sub(pagesFS, "pages/classic")
|
||||||
|
case "mino":
|
||||||
|
pages, err = fs.Sub(pagesFS, "pages/mino")
|
||||||
default:
|
default:
|
||||||
pages, err = fs.Sub(pagesFS, "pages/bootstrap") // 默认主题
|
pages, err = fs.Sub(pagesFS, "pages/bootstrap") // 默认主题
|
||||||
logWarning("Invalid Pages Theme: %s, using default theme 'bootstrap'", cfg.Pages.Theme)
|
logWarning("Invalid Pages Theme: %s, using default theme 'bootstrap'", cfg.Pages.Theme)
|
||||||
@@ -184,6 +227,19 @@ func setupPages(cfg *config.Config, router *gin.Engine) {
|
|||||||
func init() {
|
func init() {
|
||||||
readFlag()
|
readFlag()
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// 如果设置了 -h,则显示帮助信息并退出
|
||||||
|
if showHelp {
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果设置了 -v,则显示版本号并退出
|
||||||
|
if showVersion {
|
||||||
|
fmt.Printf("GHProxy Version: %s \n", version)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
loadConfig()
|
loadConfig()
|
||||||
setupLogger(cfg)
|
setupLogger(cfg)
|
||||||
InitReq(cfg)
|
InitReq(cfg)
|
||||||
@@ -275,6 +331,9 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if showVersion || showHelp {
|
||||||
|
return
|
||||||
|
}
|
||||||
err := router.Run(fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port))
|
err := router.Run(fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logError("Failed to start server: %v\n", err)
|
logError("Failed to start server: %v\n", err)
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 3.2 KiB |
@@ -1,104 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="zh">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Github文件加速</title>
|
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
|
|
||||||
onerror="this.onerror=null; this.href='https://static.wjqserver.com/bootstrap.min.css';">
|
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
||||||
<link href="style.css" rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div class="container py-4 py-md-5">
|
|
||||||
<main>
|
|
||||||
<div class="card mb-4">
|
|
||||||
<div class="card-body">
|
|
||||||
<h1 class="text-center mb-4">Github文件加速</h1>
|
|
||||||
<p class="lead text-center mb-4">为访问Github文件进行加速</p>
|
|
||||||
<form id="github-form">
|
|
||||||
<div class="mb-3">
|
|
||||||
<input type="text" class="form-control form-control-lg" id="githubLinkInput"
|
|
||||||
placeholder="请键入需要代理的 Github 链接">
|
|
||||||
</div>
|
|
||||||
<button type="submit" class="btn btn-primary btn-lg w-100">获取文件链接</button>
|
|
||||||
</form>
|
|
||||||
<div id="output" class="mt-3 bg-light p-3 rounded position-relative" style="display: none;">
|
|
||||||
<pre id="formattedLinkOutput" class="mb-0"></pre>
|
|
||||||
<button id="copyButton"
|
|
||||||
class="btn btn-outline-secondary btn-sm position-absolute top-0 end-0 m-2" title="复制链接">
|
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
|
|
||||||
class="bi bi-clipboard" viewBox="0 0 16 16">
|
|
||||||
<path
|
|
||||||
d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z" />
|
|
||||||
<path
|
|
||||||
d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z" />
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
<button id="openButton"
|
|
||||||
class="btn btn-outline-secondary btn-sm position-absolute top-0 end-0 m-2 me-5"
|
|
||||||
title="在新标签页中打开">
|
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
|
|
||||||
class="bi bi-box-arrow-up-right" viewBox="0 0 16 16">
|
|
||||||
<path fill-rule="evenodd"
|
|
||||||
d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5z" />
|
|
||||||
<path fill-rule="evenodd"
|
|
||||||
d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0v-5z" />
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<p class="text-muted small mt-3 mb-0">GitHub 链接带不带协议头均可,支持 release、archive 以及文件,转换后链接均可使用。</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-md-4 mb-3">
|
|
||||||
<div class="card h-100">
|
|
||||||
<div class="card-body">
|
|
||||||
<h5 class="card-title">文件大小限制</h5>
|
|
||||||
<p class="card-text" id="sizeLimitDisplay">...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-4 mb-3">
|
|
||||||
<div class="card h-100">
|
|
||||||
<div class="card-body">
|
|
||||||
<h5 class="card-title">白名单状态</h5>
|
|
||||||
<p class="card-text" id="whiteListStatus">...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-4 mb-3">
|
|
||||||
<div class="card h-100">
|
|
||||||
<div class="card-body">
|
|
||||||
<h5 class="card-title">黑名单状态</h5>
|
|
||||||
<p class="card-text" id="blackListStatus">...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
<footer class="text-center mt-4">
|
|
||||||
<p class="text-muted">
|
|
||||||
Copyright © 2024-2025 WJQSERVER-STUDIO<br>
|
|
||||||
<a href="https://github.com/WJQSERVER-STUDIO/ghproxy" class="text-decoration-none">GitHub 仓库</a> |
|
|
||||||
<a href="https://t.me/ghproxy_go" class="text-decoration-none">Telegram 交流群</a>
|
|
||||||
</p>
|
|
||||||
</footer>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="toast-container position-fixed top-0 end-0 p-3">
|
|
||||||
<div id="toast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
||||||
<div class="toast-body"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="versionBadge" class="version-badge"></div>
|
|
||||||
|
|
||||||
<script src="https://static.wjqserver.com/bootstrap.bundle.min.js"></script>
|
|
||||||
<script src="script.js"></script>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
const githubForm = document.getElementById('github-form');
|
|
||||||
const githubLinkInput = document.getElementById('githubLinkInput');
|
|
||||||
const formattedLinkOutput = document.getElementById('formattedLinkOutput');
|
|
||||||
const output = document.getElementById('output');
|
|
||||||
const copyButton = document.getElementById('copyButton');
|
|
||||||
const openButton = document.getElementById('openButton');
|
|
||||||
const toast = new bootstrap.Toast(document.getElementById('toast'));
|
|
||||||
|
|
||||||
function showToast(message) {
|
|
||||||
const toastBody = document.querySelector('.toast-body');
|
|
||||||
toastBody.textContent = message;
|
|
||||||
toast.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatGithubLink(githubLink) {
|
|
||||||
const currentHost = window.location.host;
|
|
||||||
let formattedLink = "";
|
|
||||||
|
|
||||||
if (githubLink.startsWith("https://github.com/") || githubLink.startsWith("http://github.com/")) {
|
|
||||||
formattedLink = window.location.protocol + "//" + currentHost + "/github.com" + githubLink.substring(githubLink.indexOf("/", 8));
|
|
||||||
} else if (githubLink.startsWith("github.com/")) {
|
|
||||||
formattedLink = window.location.protocol + "//" + currentHost + "/" + githubLink;
|
|
||||||
} else if (githubLink.startsWith("https://raw.githubusercontent.com/") || githubLink.startsWith("http://raw.githubusercontent.com/")) {
|
|
||||||
formattedLink = window.location.protocol + "//" + currentHost + githubLink.substring(githubLink.indexOf("/", 7));
|
|
||||||
} else if (githubLink.startsWith("raw.githubusercontent.com/")) {
|
|
||||||
formattedLink = window.location.protocol + "//" + currentHost + "/" + githubLink;
|
|
||||||
} else if (githubLink.startsWith("https://gist.githubusercontent.com/") || githubLink.startsWith("http://gist.githubusercontent.com/")) {
|
|
||||||
formattedLink = window.location.protocol + "//" + currentHost + "/gist.github.com" + githubLink.substring(githubLink.indexOf("/", 18));
|
|
||||||
} else if (githubLink.startsWith("gist.githubusercontent.com/")) {
|
|
||||||
formattedLink = window.location.protocol + "//" + currentHost + "/" + githubLink;
|
|
||||||
} else {
|
|
||||||
showToast('请输入有效的GitHub链接');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return formattedLink;
|
|
||||||
}
|
|
||||||
|
|
||||||
githubForm.addEventListener('submit', function (e) {
|
|
||||||
e.preventDefault();
|
|
||||||
const formattedLink = formatGithubLink(githubLinkInput.value);
|
|
||||||
if (formattedLink) {
|
|
||||||
formattedLinkOutput.textContent = formattedLink;
|
|
||||||
output.style.display = 'block';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
copyButton.addEventListener('click', function () {
|
|
||||||
navigator.clipboard.writeText(formattedLinkOutput.textContent).then(() => {
|
|
||||||
showToast('链接已复制到剪贴板');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
openButton.addEventListener('click', function () {
|
|
||||||
window.open(formattedLinkOutput.textContent, '_blank');
|
|
||||||
});
|
|
||||||
|
|
||||||
function fetchAPI() {
|
|
||||||
fetch('/api/size_limit')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
document.getElementById('sizeLimitDisplay').textContent = `${data.MaxResponseBodySize} MB`;
|
|
||||||
});
|
|
||||||
|
|
||||||
fetch('/api/whitelist/status')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
document.getElementById('whiteListStatus').textContent = data.Whitelist ? '已开启' : '已关闭';
|
|
||||||
});
|
|
||||||
|
|
||||||
fetch('/api/blacklist/status')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
document.getElementById('blackListStatus').textContent = data.Blacklist ? '已开启' : '已关闭';
|
|
||||||
});
|
|
||||||
|
|
||||||
fetch('/api/version')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
document.getElementById('versionBadge').textContent = data.Version;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', fetchAPI);
|
|
||||||
@@ -1,259 +0,0 @@
|
|||||||
/* 通用样式 */
|
|
||||||
:root {
|
|
||||||
--primary-color: #007aff;
|
|
||||||
/* 主要按钮颜色 */
|
|
||||||
--secondary-color: #34c759;
|
|
||||||
/* 次要按钮颜色 */
|
|
||||||
--background-color: #f9f9f9;
|
|
||||||
/* 亮色模式背景 */
|
|
||||||
--card-background: #ffffff;
|
|
||||||
/* 卡片背景 */
|
|
||||||
--text-color: #333333;
|
|
||||||
/* 亮色模式文本颜色 */
|
|
||||||
--border-color: #e0e0e0;
|
|
||||||
/* 边框颜色 */
|
|
||||||
--input-background: #ffffff;
|
|
||||||
/* 输入框背景 */
|
|
||||||
--input-border: #d1d1d6;
|
|
||||||
/* 输入框边框 */
|
|
||||||
--pre-background: #f1f3f4;
|
|
||||||
/* 代码块背景 */
|
|
||||||
--pre-text-color: #333333;
|
|
||||||
/* 代码块文本颜色 */
|
|
||||||
--version-badge-hover: #39c5bb;
|
|
||||||
/* 说明文字颜色 */
|
|
||||||
--muted-text-color: #6c757d;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background-color: var(--background-color);
|
|
||||||
color: var(--text-color);
|
|
||||||
font-family: sans-serif;
|
|
||||||
line-height: 1.8;
|
|
||||||
font-size: 15px;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1,
|
|
||||||
h2,
|
|
||||||
h3,
|
|
||||||
h4,
|
|
||||||
h5,
|
|
||||||
h6 {
|
|
||||||
color: var(--text-color);
|
|
||||||
font-weight: 800;
|
|
||||||
letter-spacing: 0.5px;
|
|
||||||
margin: 1rem 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
p,
|
|
||||||
span,
|
|
||||||
a,
|
|
||||||
li {
|
|
||||||
color: var(--text-color);
|
|
||||||
margin-bottom: 1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
text-decoration: none;
|
|
||||||
color: var(--primary-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
a:hover {
|
|
||||||
color: #0056b3;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card {
|
|
||||||
background-color: var(--card-background);
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
border-radius: 8px;
|
|
||||||
padding: 16px;
|
|
||||||
margin: 16px 0;
|
|
||||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
|
||||||
transition: transform 0.2s, box-shadow 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card:hover {
|
|
||||||
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
|
|
||||||
transform: translateY(-4px);
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-outline-secondary {
|
|
||||||
border-radius: 50%;
|
|
||||||
padding: 6px;
|
|
||||||
transition: #e9e9e9 0.3s ease-in-out, color 0.3s ease-in-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-outline-secondary:hover {
|
|
||||||
background-color: var(--primary-color);
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.form-control {
|
|
||||||
background-color: var(--input-background);
|
|
||||||
border: 1px solid var(--input-border);
|
|
||||||
color: var(--text-color);
|
|
||||||
padding: 10px;
|
|
||||||
border-radius: 4px;
|
|
||||||
font-size: 14px;
|
|
||||||
outline: none;
|
|
||||||
transition: border-color 0.2s, box-shadow 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.form-control:focus {
|
|
||||||
border-color: var(--primary-color);
|
|
||||||
box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.3);
|
|
||||||
}
|
|
||||||
|
|
||||||
.text-muted {
|
|
||||||
color: var(--muted-text-color) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-light {
|
|
||||||
background-color: var(--card-background) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
pre {
|
|
||||||
background-color: var(--pre-background);
|
|
||||||
color: var(--pre-text-color);
|
|
||||||
padding: 16px;
|
|
||||||
border-radius: 8px;
|
|
||||||
overflow-x: auto;
|
|
||||||
font-size: 14px;
|
|
||||||
line-height: 1.6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.version-badge {
|
|
||||||
position: fixed;
|
|
||||||
bottom: 20px;
|
|
||||||
right: 20px;
|
|
||||||
background-color: var(--secondary-color);
|
|
||||||
color: white;
|
|
||||||
padding: 6px 12px;
|
|
||||||
border-radius: 20px;
|
|
||||||
font-size: 0.8rem;
|
|
||||||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
|
||||||
transition: background-color 0.3s ease-in-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
.version-badge:hover {
|
|
||||||
background-color: var(--version-badge-hover);
|
|
||||||
}
|
|
||||||
|
|
||||||
footer {
|
|
||||||
padding: 16px;
|
|
||||||
text-align: center;
|
|
||||||
color: var(--text-color);
|
|
||||||
font-size: 0.9rem;
|
|
||||||
background-color: var(--card-background);
|
|
||||||
}
|
|
||||||
|
|
||||||
footer a {
|
|
||||||
color: var(--primary-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
footer a:hover {
|
|
||||||
color: #0056b3;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 暗色模式 */
|
|
||||||
@media (prefers-color-scheme: dark) {
|
|
||||||
:root {
|
|
||||||
--background-color: #121212;
|
|
||||||
/* 深灰色背景 */
|
|
||||||
--card-background: #1e1e1e;
|
|
||||||
/* 卡片背景稍浅 */
|
|
||||||
--text-color: #ffffff;
|
|
||||||
/* 纯白文本 */
|
|
||||||
--primary-color: #0a84ff;
|
|
||||||
/* 按钮蓝色 */
|
|
||||||
--secondary-color: #30d158;
|
|
||||||
/* 次要按钮绿色 */
|
|
||||||
--border-color: #3a3a3a;
|
|
||||||
/* 边框颜色 */
|
|
||||||
--input-background: #2c2c2c;
|
|
||||||
/* 输入框背景 */
|
|
||||||
--input-border: #4a4a4a;
|
|
||||||
/* 输入框边框 */
|
|
||||||
--pre-background: #3b3636;
|
|
||||||
/* 代码块背景 */
|
|
||||||
--pre-text-color: #ffffff;
|
|
||||||
/* 代码块文本颜色 */
|
|
||||||
--version-badge-hover: #39c5bc9a;
|
|
||||||
/* 说明文字颜色 */
|
|
||||||
--muted-text-color: #a0a0a0;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background-color: var(--background-color);
|
|
||||||
color: var(--text-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
h1,
|
|
||||||
h2,
|
|
||||||
h3,
|
|
||||||
h4,
|
|
||||||
h5,
|
|
||||||
h6,
|
|
||||||
p,
|
|
||||||
span,
|
|
||||||
a,
|
|
||||||
li {
|
|
||||||
color: var(--text-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.card {
|
|
||||||
background-color: var(--card-background);
|
|
||||||
color: var(--text-color);
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-outline-secondary {
|
|
||||||
border-radius: 50%;
|
|
||||||
padding: 6px;
|
|
||||||
transition: background-color 0.3s ease-in-out, color 0.3s ease-in-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-outline-secondary:hover {
|
|
||||||
background-color: var(--primary-color);
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.toast {
|
|
||||||
background-color: var(--card-background);
|
|
||||||
color: var(--text-color);
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
border-radius: 8px;
|
|
||||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
|
||||||
}
|
|
||||||
|
|
||||||
.toast-body {
|
|
||||||
padding: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
.form-control {
|
|
||||||
background-color: var(--input-background);
|
|
||||||
border: 1px solid var(--input-border);
|
|
||||||
color: var(--text-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-light {
|
|
||||||
background-color: var(--card-background) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
pre {
|
|
||||||
background-color: var(--pre-background);
|
|
||||||
color: var(--pre-text-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
footer {
|
|
||||||
background-color: var(--card-background);
|
|
||||||
color: var(--text-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
footer a {
|
|
||||||
color: var(--primary-color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 3.2 KiB |
@@ -1,169 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="zh" data-bs-theme="auto">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>GitHub加速服务</title>
|
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
|
|
||||||
onerror="this.onerror=null; this.href='https://static.wjqserver.com/bootstrap.min.css';">
|
|
||||||
<link href="style.css" rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div class="container py-4 py-lg-5">
|
|
||||||
<header class="text-center mb-5">
|
|
||||||
<h1 class="display-5 fw-bold mb-3">GitHub加速服务</h1>
|
|
||||||
<p class="lead text-muted">高速稳定的 GitHub 资源访问解决方案</p>
|
|
||||||
</header>
|
|
||||||
<div class="main-card p-4 mb-4">
|
|
||||||
<form id="mainForm" class="mb-4">
|
|
||||||
<div class="mb-3">
|
|
||||||
<label for="inputUrl" class="form-label fw-semibold">GitHub 链接</label>
|
|
||||||
<input type="url" class="form-control form-control-lg" id="inputUrl" placeholder="输入 GitHub 文件/仓库链接"
|
|
||||||
required>
|
|
||||||
</div>
|
|
||||||
<button type="submit" class="btn btn-primary btn-lg w-100 py-2">
|
|
||||||
🚀 生成加速链接
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<!-- 结果输出 -->
|
|
||||||
<div id="output" class="mt-4" hidden>
|
|
||||||
<div class="code-block">
|
|
||||||
<code id="outputLink" class="d-block text-break"></code>
|
|
||||||
</div>
|
|
||||||
<div class="d-flex gap-2 mt-3">
|
|
||||||
<button id="copyBtn" class="btn btn-outline-secondary">
|
|
||||||
⎘ 复制链接
|
|
||||||
</button>
|
|
||||||
<button id="openBtn" class="btn btn-primary">
|
|
||||||
↗ 立即打开
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="row g-3 mb-4">
|
|
||||||
<div class="col-md-4">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
|
||||||
<div>
|
|
||||||
<h6 class="mb-0">文件大小限制</h6>
|
|
||||||
<small class="text-muted">最大支持文件尺寸</small>
|
|
||||||
</div>
|
|
||||||
<span id="sizeLimit" class="status-badge bg-primary">...</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-4">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
|
||||||
<div>
|
|
||||||
<h6 class="mb-0">白名单状态</h6>
|
|
||||||
<small class="text-muted">访问控制列表</small>
|
|
||||||
</div>
|
|
||||||
<span id="whitelistStatus" class="status-badge bg-success">...</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-4">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
|
||||||
<div>
|
|
||||||
<h6 class="mb-0">黑名单状态</h6>
|
|
||||||
<small class="text-muted">屏蔽列表状态</small>
|
|
||||||
</div>
|
|
||||||
<span id="blacklistStatus" class="status-badge bg-danger">...</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="main-card p-4">
|
|
||||||
<h2 class="h4 fw-bold mb-4">📚 详细使用指南</h2>
|
|
||||||
|
|
||||||
<div class="guide-section">
|
|
||||||
<h3 class="h5 fw-semibold mb-3">支持的工具</h3>
|
|
||||||
<ul class="list-unstyled">
|
|
||||||
<li class="mb-2">✅ 支持域名:github.com</li>
|
|
||||||
<li class="mb-2">✅ 支持域名:raw.githubusercontent.com</li>
|
|
||||||
<li class="mb-2">✅ 支持域名:gist.githubusercontent.com</li>
|
|
||||||
<li class="mb-2">✅ 支持HTTPS Git Clone</li>
|
|
||||||
<li class="text-muted">❌ 不支持 SSH Git Clone</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
<div class="guide-section">
|
|
||||||
<h3 class="h5 fw-semibold mb-3">基础用法示例</h3>
|
|
||||||
<div class="command-example">
|
|
||||||
<h4 class="h6 fw-medium mb-2">Git 克隆</h4>
|
|
||||||
<code
|
|
||||||
class="d-block mb-3">git clone <span class="protocol">https</span>://<span class="host">example.com</span>/https://github.com/user/project.git</code>
|
|
||||||
<h4 class="h6 fw-medium mb-2">私有仓库克隆</h4>
|
|
||||||
<code
|
|
||||||
class="d-block">git clone <span class="protocol">https</span>://user:your_token@<span class="host">example.com</span>/https://github.com/user/project.git</code>
|
|
||||||
</div>
|
|
||||||
<div class="command-example">
|
|
||||||
<h4 class="h6 fw-medium mb-2">文件下载</h4>
|
|
||||||
<code
|
|
||||||
class="d-block mb-3">wget <span class="protocol">https</span>://<span class="host">example.com</span>/https://raw.githubusercontent.com/user/project/main/README.md</code>
|
|
||||||
<h4 class="h6 fw-medium mb-2">版本发布</h4>
|
|
||||||
<code
|
|
||||||
class="d-block">curl -LO <span class="protocol">https</span>://<span class="host">example.com</span>/https://github.com/user/project/releases/download/v1.0.0/project_1.0.0.amd64.tar.gz</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="guide-section">
|
|
||||||
<h3 class="h5 fw-semibold mb-3">支持的文件类型</h3>
|
|
||||||
<div class="row g-3">
|
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<h4 class="h6 fw-medium mb-2">原始文件</h4>
|
|
||||||
<code
|
|
||||||
class="d-block text-muted fs-sm">https://raw.githubusercontent.com/user/repo/main/file.txt</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<h4 class="h6 fw-medium mb-2">分支源码</h4>
|
|
||||||
<code class="d-block text-muted fs-sm">https://github.com/user/repo/archive/main.zip</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<h4 class="h6 fw-medium mb-2">版本发布</h4>
|
|
||||||
<code
|
|
||||||
class="d-block text-muted fs-sm">https://github.com/user/repo/releases/download/v1.0.0/app.zip</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<h4 class="h6 fw-medium mb-2">Gist 文件</h4>
|
|
||||||
<code
|
|
||||||
class="d-block text-muted fs-sm">https://gist.githubusercontent.com/user/gist_id/raw/file.txt</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="main-card p-3">
|
|
||||||
<h4 class="h6 fw-medium mb-2">HTTPS Git Clone</h4>
|
|
||||||
<code class="d-block text-muted fs-sm">git clone https://github.com/user/repo.git</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<footer class="text-center mt-5 text-muted">
|
|
||||||
<p class="mb-0 small" id="version">v </p>
|
|
||||||
<p class="mb-0 small">Copyright © 2024 - <span id="currentYear"></span> WJQSERVER-STUDIO<br>
|
|
||||||
<a href="https://github.com/WJQSERVER-STUDIO/ghproxy" class="text-decoration-none">GitHub 仓库</a> |
|
|
||||||
<a href="https://t.me/ghproxy_go" class="text-decoration-none">Telegram 交流群</a>
|
|
||||||
</p>
|
|
||||||
</footer>
|
|
||||||
</div>
|
|
||||||
<div class="toast-container position-fixed bottom-0 end-0 p-3">
|
|
||||||
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
||||||
<div class="toast-body"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<script src="https://static.wjqserver.com/bootstrap.bundle.min.js"></script>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
(() => {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// 初始化基础配置
|
|
||||||
const currentYear = new Date().getFullYear();
|
|
||||||
document.getElementById('currentYear').textContent = currentYear;
|
|
||||||
const toast = new bootstrap.Toast('#liveToast');
|
|
||||||
|
|
||||||
// DOM 元素
|
|
||||||
const form = document.getElementById('mainForm');
|
|
||||||
const input = document.getElementById('inputUrl');
|
|
||||||
const output = document.getElementById('output');
|
|
||||||
const outputLink = document.getElementById('outputLink');
|
|
||||||
|
|
||||||
// 获取当前域名
|
|
||||||
const CURRENT_PROTOCOL = window.location.protocol.replace(':', '');
|
|
||||||
const CURRENT_HOST = window.location.host;
|
|
||||||
// 替换协议部分
|
|
||||||
document.querySelectorAll('code .protocol').forEach(span => {
|
|
||||||
span.textContent = CURRENT_PROTOCOL;
|
|
||||||
});
|
|
||||||
// 替换域名部分
|
|
||||||
document.querySelectorAll('code .host').forEach(span => {
|
|
||||||
span.textContent = CURRENT_HOST;
|
|
||||||
});
|
|
||||||
|
|
||||||
// URL 转换规则
|
|
||||||
const URL_RULES = [
|
|
||||||
{
|
|
||||||
regex: /^(?:https?:\/\/)?(?:www\.)?(github\.com\/.*)/i,
|
|
||||||
build: path => `${location.protocol}//${location.host}/${path}`
|
|
||||||
},
|
|
||||||
{
|
|
||||||
regex: /^(?:https?:\/\/)?(raw\.githubusercontent\.com\/.*)/i,
|
|
||||||
build: path => `${location.protocol}//${location.host}/${path}`
|
|
||||||
},
|
|
||||||
{
|
|
||||||
regex: /^(?:https?:\/\/)?(gist\.(?:githubusercontent|github)\.com\/.*)/i,
|
|
||||||
build: path => `${location.protocol}//${location.host}/${path}`
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
// 核心功能:链接转换
|
|
||||||
function transformGitHubURL(url) {
|
|
||||||
const cleanURL = url.trim().replace(/^https?:\/\//i, '');
|
|
||||||
for (const rule of URL_RULES) {
|
|
||||||
const match = cleanURL.match(rule.regex);
|
|
||||||
if (match) return rule.build(match[1]);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 事件处理
|
|
||||||
form.addEventListener('submit', e => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
if (!input.checkValidity()) {
|
|
||||||
input.classList.add('is-invalid');
|
|
||||||
showToast('⚠️ 请输入有效的 GitHub 链接');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = transformGitHubURL(input.value);
|
|
||||||
if (!result) {
|
|
||||||
showToast('❌ 不支持的链接格式');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
outputLink.textContent = result;
|
|
||||||
output.hidden = false;
|
|
||||||
window.scrollTo({ top: output.offsetTop - 100, behavior: 'smooth' });
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('copyBtn').addEventListener('click', async () => {
|
|
||||||
try {
|
|
||||||
await navigator.clipboard.writeText(outputLink.textContent);
|
|
||||||
showToast('✅ 链接已复制');
|
|
||||||
} catch {
|
|
||||||
showToast('❌ 复制失败');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('openBtn').addEventListener('click', () => {
|
|
||||||
window.open(outputLink.textContent, '_blank', 'noopener,noreferrer');
|
|
||||||
});
|
|
||||||
|
|
||||||
// 服务状态监控
|
|
||||||
async function loadServiceStatus() {
|
|
||||||
try {
|
|
||||||
const [size, whitelist, blacklist, version] = await Promise.all([
|
|
||||||
fetchJSON('/api/size_limit'),
|
|
||||||
fetchJSON('/api/whitelist/status'),
|
|
||||||
fetchJSON('/api/blacklist/status'),
|
|
||||||
fetchJSON('/api/version')
|
|
||||||
]);
|
|
||||||
|
|
||||||
updateStatus('sizeLimit', `${size.MaxResponseBodySize}MB`);
|
|
||||||
updateStatus('whitelistStatus', whitelist.Whitelist ? '已开启' : '已关闭');
|
|
||||||
updateStatus('blacklistStatus', blacklist.Blacklist ? '已开启' : '已关闭');
|
|
||||||
updateStatus('version', `Version ${version.Version}`);
|
|
||||||
} catch {
|
|
||||||
showToast('⚠️ 服务状态获取失败');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchJSON(url) {
|
|
||||||
const response = await fetch(url);
|
|
||||||
if (!response.ok) throw new Error('API Error');
|
|
||||||
return response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateStatus(elementId, text) {
|
|
||||||
const element = document.getElementById(elementId);
|
|
||||||
if (element) element.textContent = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 工具函数
|
|
||||||
function showToast(message) {
|
|
||||||
const toastBody = document.querySelector('.toast-body');
|
|
||||||
toastBody.textContent = message;
|
|
||||||
toast.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化
|
|
||||||
input.addEventListener('input', () => {
|
|
||||||
input.classList.remove('is-invalid');
|
|
||||||
if (output.hidden === false) output.hidden = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
loadServiceStatus();
|
|
||||||
})();
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
:root {
|
|
||||||
--primary: #007aff;
|
|
||||||
--secondary: #007aff;
|
|
||||||
--background: #f9f9f9;
|
|
||||||
--card-bg: #ffffff;
|
|
||||||
--text: #333333;
|
|
||||||
--border: #d1d1d6;
|
|
||||||
--muted: #64748b;
|
|
||||||
--success: #00a83ed2;
|
|
||||||
--danger: #ce0000dd;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
|
||||||
:root {
|
|
||||||
--background: #121212;
|
|
||||||
--card-bg: #1e1e1e;
|
|
||||||
--text: #ffffff;
|
|
||||||
--border: #334155;
|
|
||||||
--muted: #94a3b8;
|
|
||||||
}
|
|
||||||
|
|
||||||
.form-control::placeholder {
|
|
||||||
color: var(--muted);
|
|
||||||
}
|
|
||||||
|
|
||||||
.text-muted {
|
|
||||||
color: var(--muted) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-outline-secondary {
|
|
||||||
color: var(--muted);
|
|
||||||
border-color: var(--border);
|
|
||||||
}
|
|
||||||
|
|
||||||
.status-badge {
|
|
||||||
color: var(--text) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.code-block {
|
|
||||||
background: rgba(255, 255, 255, 0.05);
|
|
||||||
}
|
|
||||||
|
|
||||||
.command-example {
|
|
||||||
background: rgba(255, 255, 255, 0.03);
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-primary {
|
|
||||||
--bs-btn-bg: var(--primary);
|
|
||||||
--bs-btn-border-color: var(--primary);
|
|
||||||
--bs-btn-hover-bg: var(--secondary);
|
|
||||||
--bs-btn-hover-border-color: var(--secondary);
|
|
||||||
}
|
|
||||||
|
|
||||||
.form-control {
|
|
||||||
background-color: rgba(255, 255, 255, 0.05);
|
|
||||||
border-color: var(--border);
|
|
||||||
color: var(--text);
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
color: var(--secondary);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background: var(--background);
|
|
||||||
color: var(--text);
|
|
||||||
font-family: 'Inter', system-ui, sans-serif;
|
|
||||||
line-height: 1.6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.main-card {
|
|
||||||
background: var(--card-bg);
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
border-radius: 12px;
|
|
||||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
|
|
||||||
transition: transform 0.2s, box-shadow 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.main-card:hover {
|
|
||||||
transform: translateY(-2px);
|
|
||||||
box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
|
|
||||||
}
|
|
||||||
|
|
||||||
.code-block {
|
|
||||||
background: rgba(37, 99, 235, 0.05);
|
|
||||||
border-left: 3px solid var(--primary);
|
|
||||||
border-radius: 6px;
|
|
||||||
padding: 1rem;
|
|
||||||
position: relative;
|
|
||||||
overflow-x: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status-badge {
|
|
||||||
padding: 6px 12px;
|
|
||||||
border-radius: 20px;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
font-weight: 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
.guide-section {
|
|
||||||
border-left: 3px solid var(--primary);
|
|
||||||
padding-left: 1rem;
|
|
||||||
margin: 2rem 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.command-example {
|
|
||||||
position: relative;
|
|
||||||
padding: 1.25rem;
|
|
||||||
background: rgba(37, 99, 235, 0.03);
|
|
||||||
border-radius: 8px;
|
|
||||||
margin: 1rem 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.command-example::before {
|
|
||||||
content: "➜";
|
|
||||||
position: absolute;
|
|
||||||
left: -1.5rem;
|
|
||||||
color: var(--muted);
|
|
||||||
}
|
|
||||||
|
|
||||||
.toast {
|
|
||||||
position: fixed;
|
|
||||||
top: 1%;
|
|
||||||
right: 1%;
|
|
||||||
background-color: var(--card-background);
|
|
||||||
color: var(--text-color);
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
border-radius: 8px;
|
|
||||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
|
||||||
}
|
|
||||||
|
|
||||||
.toast-body {
|
|
||||||
padding: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#version {
|
|
||||||
text-align: center;
|
|
||||||
font-size: 13px;
|
|
||||||
line-height: 26px;
|
|
||||||
color: #747474;
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-primary {
|
|
||||||
--bs-bg-opacity: 1;
|
|
||||||
background-color: #2c82de !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-success {
|
|
||||||
--bs-bg-opacity: 1;
|
|
||||||
background-color: #2c82de !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-danger {
|
|
||||||
--bs-bg-opacity: 1;
|
|
||||||
background-color: #2c82de !important;
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
|
func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher string) {
|
||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
|
|
||||||
// 发送HEAD请求, 预获取Content-Length
|
// 发送HEAD请求, 预获取Content-Length
|
||||||
@@ -23,6 +23,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
|||||||
}
|
}
|
||||||
setRequestHeaders(c, headReq)
|
setRequestHeaders(c, headReq)
|
||||||
removeWSHeader(headReq) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
removeWSHeader(headReq) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
||||||
|
reWriteEncodeHeader(headReq)
|
||||||
AuthPassThrough(c, cfg, headReq)
|
AuthPassThrough(c, cfg, headReq)
|
||||||
|
|
||||||
headResp, err := client.Do(headReq)
|
headResp, err := client.Do(headReq)
|
||||||
@@ -64,6 +65,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
|||||||
}
|
}
|
||||||
setRequestHeaders(c, req)
|
setRequestHeaders(c, req)
|
||||||
removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
||||||
|
reWriteEncodeHeader(req)
|
||||||
AuthPassThrough(c, cfg, req)
|
AuthPassThrough(c, cfg, req)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
@@ -106,14 +108,6 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
|||||||
resp.Header.Del(header)
|
resp.Header.Del(header)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
if cfg.CORS.Enabled {
|
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
|
||||||
} else {
|
|
||||||
c.Header("Access-Control-Allow-Origin", "")
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
switch cfg.Server.Cors {
|
switch cfg.Server.Cors {
|
||||||
case "*":
|
case "*":
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
c.Header("Access-Control-Allow-Origin", "*")
|
||||||
@@ -127,12 +121,30 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
|||||||
|
|
||||||
c.Status(resp.StatusCode)
|
c.Status(resp.StatusCode)
|
||||||
|
|
||||||
//_, err = io.CopyBuffer(c.Writer, resp.Body, nil)
|
if MatcherShell(u) && matchString(matcher, matchedMatchers) && cfg.Shell.Editor {
|
||||||
_, err = copyb.CopyBuffer(c.Writer, resp.Body, nil)
|
// 判断body是不是gzip
|
||||||
if err != nil {
|
var compress string
|
||||||
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)
|
if resp.Header.Get("Content-Encoding") == "gzip" {
|
||||||
return
|
compress = "gzip"
|
||||||
|
}
|
||||||
|
|
||||||
|
logInfo("Is Shell: %s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||||
|
c.Header("Content-Length", "")
|
||||||
|
_, err = processLinks(resp.Body, c.Writer, compress, c.Request.Host, cfg)
|
||||||
|
if err != nil {
|
||||||
|
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() // 确保刷入
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
c.Writer.Flush() // 确保刷入
|
//_, err = io.CopyBuffer(c.Writer, resp.Body, nil)
|
||||||
|
_, err = copyb.Copy(c.Writer, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
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() // 确保刷入
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
102
proxy/gitreq.go
102
proxy/gitreq.go
@@ -6,9 +6,7 @@ import (
|
|||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/WJQSERVER-STUDIO/go-utils/copyb"
|
"github.com/WJQSERVER-STUDIO/go-utils/copyb"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -18,17 +16,23 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||||
|
|
||||||
logInfo("U:%s", u)
|
logDump("Url Before FMT:%s", u)
|
||||||
if cfg.GitClone.Mode == "cache" {
|
if cfg.GitClone.Mode == "cache" {
|
||||||
userPath, repoPath, remainingPath, err := extractParts(u)
|
userPath, repoPath, remainingPath, queryParams, err := extractParts(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
HandleError(c, fmt.Sprintf("Failed to extract parts from URL: %v", err))
|
HandleError(c, fmt.Sprintf("Failed to extract parts from URL: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 构建新url
|
// 构建新url
|
||||||
u = cfg.GitClone.SmartGitAddr + userPath + repoPath + remainingPath
|
u = cfg.GitClone.SmartGitAddr + userPath + repoPath + remainingPath + "?" + queryParams.Encode()
|
||||||
|
logDump("New Url After FMT:%s", u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
resp *http.Response
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
body, err := readRequestBody(c)
|
body, err := readRequestBody(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
HandleError(c, err.Error())
|
HandleError(c, err.Error())
|
||||||
@@ -37,18 +41,39 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||||||
|
|
||||||
bodyReader := bytes.NewBuffer(body)
|
bodyReader := bytes.NewBuffer(body)
|
||||||
// 创建请求
|
// 创建请求
|
||||||
req, err := client.NewRequest(method, u, bodyReader)
|
|
||||||
if err != nil {
|
|
||||||
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setRequestHeaders(c, req)
|
|
||||||
AuthPassThrough(c, cfg, req)
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
if cfg.GitClone.Mode == "cache" {
|
||||||
if err != nil {
|
req, err := gitclient.NewRequest(method, u, bodyReader)
|
||||||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
if err != nil {
|
||||||
return
|
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setRequestHeaders(c, req)
|
||||||
|
removeWSHeader(req)
|
||||||
|
reWriteEncodeHeader(req)
|
||||||
|
AuthPassThrough(c, cfg, req)
|
||||||
|
|
||||||
|
resp, err = gitclient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
req, err := client.NewRequest(method, u, bodyReader)
|
||||||
|
if err != nil {
|
||||||
|
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setRequestHeaders(c, req)
|
||||||
|
removeWSHeader(req)
|
||||||
|
reWriteEncodeHeader(req)
|
||||||
|
AuthPassThrough(c, cfg, req)
|
||||||
|
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//defer resp.Body.Close()
|
//defer resp.Body.Close()
|
||||||
defer func(Body io.ReadCloser) {
|
defer func(Body io.ReadCloser) {
|
||||||
@@ -97,21 +122,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Status(resp.StatusCode)
|
c.Status(resp.StatusCode)
|
||||||
/*
|
_, err = copyb.Copy(c.Writer, resp.Body)
|
||||||
// 使用固定32KB缓冲池
|
|
||||||
buffer := BufferPool.Get().([]byte)
|
|
||||||
defer BufferPool.Put(buffer)
|
|
||||||
|
|
||||||
_, err = io.CopyBuffer(c.Writer, resp.Body, buffer)
|
|
||||||
if err != nil {
|
|
||||||
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() // 确保刷入
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
_, err = copyb.CopyBuffer(c.Writer, resp.Body, nil)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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)
|
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)
|
||||||
@@ -122,32 +133,3 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractParts 从给定的 URL 中提取所需的部分
|
|
||||||
func extractParts(rawURL string) (string, string, string, error) {
|
|
||||||
// 解析 URL
|
|
||||||
parsedURL, err := url.Parse(rawURL)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取路径部分并分割
|
|
||||||
pathParts := strings.Split(parsedURL.Path, "/")
|
|
||||||
|
|
||||||
// 提取所需的部分
|
|
||||||
if len(pathParts) < 3 {
|
|
||||||
return "", "", "", fmt.Errorf("URL path is too short")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取 /WJQSERVER-STUDIO 和 /go-utils.git
|
|
||||||
repoOwner := "/" + pathParts[1]
|
|
||||||
repoName := "/" + pathParts[2]
|
|
||||||
|
|
||||||
// 剩余部分
|
|
||||||
remainingPath := strings.Join(pathParts[3:], "/")
|
|
||||||
if remainingPath != "" {
|
|
||||||
remainingPath = "/" + remainingPath
|
|
||||||
}
|
|
||||||
|
|
||||||
return repoOwner, repoName, remainingPath, nil
|
|
||||||
}
|
|
||||||
|
|||||||
124
proxy/handler.go
124
proxy/handler.go
@@ -1,6 +1,7 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"ghproxy/auth"
|
"ghproxy/auth"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
@@ -13,16 +14,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var re = regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
|
var re = regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
|
||||||
/*
|
|
||||||
var exps = []*regexp.Regexp{
|
|
||||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`), // 匹配 GitHub Releases 或 Archive 链接
|
|
||||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`), // 匹配 GitHub Blob 或 Raw 链接
|
|
||||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`), // 匹配 GitHub Info 或 Git 相关链接 (例如 .gitattributes, .gitignore)
|
|
||||||
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`), // 匹配 raw.githubusercontent.com 链接
|
|
||||||
regexp.MustCompile(`^(?:https?://)?gist\.github(?:usercontent|)\.com/([^/]+)/.+?/.+`), // 匹配 gist.githubusercontent.com 链接
|
|
||||||
regexp.MustCompile(`^(?:https?://)?api\.github\.com/repos/([^/]+)/([^/]+)/.*`), // 匹配 api.github.com/repos 链接 (GitHub API)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter, runMode string) gin.HandlerFunc {
|
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter, runMode string) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
@@ -65,93 +56,24 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||||||
// 制作url
|
// 制作url
|
||||||
rawPath = "https://" + matches[2]
|
rawPath = "https://" + matches[2]
|
||||||
|
|
||||||
var (
|
user, repo, matcher, err := Matcher(rawPath, cfg)
|
||||||
user string
|
if err != nil {
|
||||||
repo string
|
if errors.Is(err, ErrInvalidURL) {
|
||||||
matcher string
|
|
||||||
)
|
|
||||||
|
|
||||||
// 匹配 "https://github.com"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://github.com") {
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://github.com")
|
|
||||||
if strings.HasPrefix(remainingPath, "/") {
|
|
||||||
remainingPath = strings.TrimPrefix(remainingPath, "/")
|
|
||||||
}
|
|
||||||
// 预期格式/user/repo/more...
|
|
||||||
// 取出user和repo和最后部分
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if len(parts) <= 2 {
|
|
||||||
logWarning("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
|
||||||
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
||||||
|
logWarning(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user = parts[0]
|
if errors.Is(err, ErrAuthHeaderUnavailable) {
|
||||||
repo = parts[1]
|
c.String(http.StatusForbidden, "AuthHeader Unavailable")
|
||||||
// 匹配 "https://github.com"开头的链接
|
logWarning(err.Error())
|
||||||
if len(parts) >= 3 {
|
|
||||||
switch parts[2] {
|
|
||||||
case "releases", "archive":
|
|
||||||
matcher = "releases"
|
|
||||||
case "blob", "raw":
|
|
||||||
matcher = "blob"
|
|
||||||
case "info", "git-upload-pack":
|
|
||||||
matcher = "clone"
|
|
||||||
default:
|
|
||||||
fmt.Println("Invalid URL: Unknown type")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 匹配 "https://raw"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://raw") {
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if len(parts) <= 3 {
|
|
||||||
logWarning("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
|
||||||
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
user = parts[1]
|
|
||||||
repo = parts[2]
|
|
||||||
matcher = "raw"
|
|
||||||
}
|
|
||||||
// 匹配 "https://gist"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://gist") {
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if len(parts) <= 3 {
|
|
||||||
logWarning("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
|
||||||
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
user = parts[1]
|
|
||||||
matcher = "gist"
|
|
||||||
}
|
|
||||||
// 匹配 "https://api.github.com/"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://api.github.com/") {
|
|
||||||
matcher = "api"
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://api.github.com/")
|
|
||||||
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if parts[0] == "repos" {
|
|
||||||
user = parts[1]
|
|
||||||
repo = parts[2]
|
|
||||||
}
|
|
||||||
if parts[0] == "users" {
|
|
||||||
user = parts[1]
|
|
||||||
}
|
|
||||||
if cfg.Auth.AuthMethod != "header" || !cfg.Auth.Enabled {
|
|
||||||
c.JSON(http.StatusForbidden, gin.H{"error": "HeaderAuth is not enabled."})
|
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
username := user
|
username := user
|
||||||
//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)
|
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
|
// 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)
|
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)
|
repouser := fmt.Sprintf("%s/%s", username, repo)
|
||||||
|
|
||||||
// 白名单检查
|
// 白名单检查
|
||||||
@@ -178,15 +100,6 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
matches = CheckURL(rawPath, c)
|
|
||||||
if matches == nil {
|
|
||||||
c.AbortWithStatus(http.StatusNotFound)
|
|
||||||
logWarning("%s %s %s %s %s 404-NOMATCH", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 若匹配api.github.com/repos/用户名/仓库名/路径, 则检查是否开启HeaderAuth
|
// 若匹配api.github.com/repos/用户名/仓库名/路径, 则检查是否开启HeaderAuth
|
||||||
|
|
||||||
// 处理blob/raw路径
|
// 处理blob/raw路径
|
||||||
@@ -195,7 +108,8 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 鉴权
|
// 鉴权
|
||||||
authcheck, err := auth.AuthHandler(c, cfg)
|
var authcheck bool
|
||||||
|
authcheck, err = auth.AuthHandler(c, cfg)
|
||||||
if !authcheck {
|
if !authcheck {
|
||||||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||||
logWarning("%s %s %s %s %s Auth-Error: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
logWarning("%s %s %s %s %s Auth-Error: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
||||||
@@ -207,8 +121,7 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||||||
|
|
||||||
switch matcher {
|
switch matcher {
|
||||||
case "releases", "blob", "raw", "gist", "api":
|
case "releases", "blob", "raw", "gist", "api":
|
||||||
//ProxyRequest(c, rawPath, cfg, "chrome", runMode)
|
ChunkedProxyRequest(c, rawPath, cfg, matcher)
|
||||||
ChunkedProxyRequest(c, rawPath, cfg, "chrome", runMode) // dev test chunk
|
|
||||||
case "clone":
|
case "clone":
|
||||||
//ProxyRequest(c, rawPath, cfg, "git", runMode)
|
//ProxyRequest(c, rawPath, cfg, "git", runMode)
|
||||||
GitReq(c, rawPath, cfg, "git", runMode)
|
GitReq(c, rawPath, cfg, "git", runMode)
|
||||||
@@ -219,16 +132,3 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func CheckURL(u string, c *gin.Context) []string {
|
|
||||||
for _, exp := range exps {
|
|
||||||
if matches := exp.FindStringSubmatch(u); matches != nil {
|
|
||||||
return matches[1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
logError(errMsg)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|||||||
@@ -14,12 +14,17 @@ var BufferSize int = 32 * 1024 // 32KB
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
tr *http.Transport
|
tr *http.Transport
|
||||||
|
gittr *http.Transport
|
||||||
BufferPool *sync.Pool
|
BufferPool *sync.Pool
|
||||||
client *httpc.Client
|
client *httpc.Client
|
||||||
|
gitclient *httpc.Client
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitReq(cfg *config.Config) {
|
func InitReq(cfg *config.Config) {
|
||||||
initHTTPClient(cfg)
|
initHTTPClient(cfg)
|
||||||
|
if cfg.GitClone.Mode == "cache" {
|
||||||
|
initGitHTTPClient(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化固定大小的缓存池
|
// 初始化固定大小的缓存池
|
||||||
BufferPool = &sync.Pool{
|
BufferPool = &sync.Pool{
|
||||||
@@ -30,20 +35,6 @@ func InitReq(cfg *config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initHTTPClient(cfg *config.Config) {
|
func initHTTPClient(cfg *config.Config) {
|
||||||
/*
|
|
||||||
ctr = &http.Transport{
|
|
||||||
MaxIdleConns: 100,
|
|
||||||
MaxConnsPerHost: 60,
|
|
||||||
IdleConnTimeout: 20 * time.Second,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
|
||||||
ResponseHeaderTimeout: 10 * time.Second,
|
|
||||||
DialContext: (&net.Dialer{
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
}).DialContext,
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
var proTolcols = new(http.Protocols)
|
var proTolcols = new(http.Protocols)
|
||||||
proTolcols.SetHTTP1(true)
|
proTolcols.SetHTTP1(true)
|
||||||
proTolcols.SetHTTP2(true)
|
proTolcols.SetHTTP2(true)
|
||||||
@@ -93,3 +84,60 @@ func initHTTPClient(cfg *config.Config) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initGitHTTPClient(cfg *config.Config) {
|
||||||
|
|
||||||
|
var proTolcols = new(http.Protocols)
|
||||||
|
proTolcols.SetHTTP1(true)
|
||||||
|
proTolcols.SetHTTP2(true)
|
||||||
|
proTolcols.SetUnencryptedHTTP2(true)
|
||||||
|
if cfg.GitClone.ForceH2C {
|
||||||
|
proTolcols.SetHTTP1(false)
|
||||||
|
proTolcols.SetHTTP2(false)
|
||||||
|
proTolcols.SetUnencryptedHTTP2(true)
|
||||||
|
}
|
||||||
|
if cfg.Httpc.Mode == "auto" {
|
||||||
|
|
||||||
|
gittr = &http.Transport{
|
||||||
|
//MaxIdleConns: 160,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
WriteBufferSize: 32 * 1024, // 32KB
|
||||||
|
ReadBufferSize: 32 * 1024, // 32KB
|
||||||
|
Protocols: proTolcols,
|
||||||
|
}
|
||||||
|
} else if cfg.Httpc.Mode == "advanced" {
|
||||||
|
gittr = &http.Transport{
|
||||||
|
MaxIdleConns: cfg.Httpc.MaxIdleConns,
|
||||||
|
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
|
||||||
|
MaxIdleConnsPerHost: cfg.Httpc.MaxIdleConnsPerHost,
|
||||||
|
WriteBufferSize: 32 * 1024, // 32KB
|
||||||
|
ReadBufferSize: 32 * 1024, // 32KB
|
||||||
|
Protocols: proTolcols,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 错误的模式
|
||||||
|
logError("unknown httpc mode: %s", cfg.Httpc.Mode)
|
||||||
|
fmt.Println("unknown httpc mode: ", cfg.Httpc.Mode)
|
||||||
|
logWarning("use Auto to Run HTTP Client")
|
||||||
|
fmt.Println("use Auto to Run HTTP Client")
|
||||||
|
gittr = &http.Transport{
|
||||||
|
//MaxIdleConns: 160,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
WriteBufferSize: 32 * 1024, // 32KB
|
||||||
|
ReadBufferSize: 32 * 1024, // 32KB
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cfg.Outbound.Enabled {
|
||||||
|
initTransport(cfg, gittr)
|
||||||
|
}
|
||||||
|
if cfg.Server.Debug {
|
||||||
|
gitclient = httpc.New(
|
||||||
|
httpc.WithTransport(gittr),
|
||||||
|
httpc.WithDumpLog(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
gitclient = httpc.New(
|
||||||
|
httpc.WithTransport(gittr),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
319
proxy/match.go
Normal file
319
proxy/match.go
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"compress/gzip"
|
||||||
|
"fmt"
|
||||||
|
"ghproxy/config"
|
||||||
|
"io"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 定义错误类型, error承载描述, 便于处理
|
||||||
|
type MatcherErrors struct {
|
||||||
|
Code int
|
||||||
|
Msg string
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidURL = &MatcherErrors{
|
||||||
|
Code: 403,
|
||||||
|
Msg: "Invalid URL Format",
|
||||||
|
}
|
||||||
|
ErrAuthHeaderUnavailable = &MatcherErrors{
|
||||||
|
Code: 403,
|
||||||
|
Msg: "AuthHeader Unavailable",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (e *MatcherErrors) Error() string {
|
||||||
|
if e.Err != nil {
|
||||||
|
return fmt.Sprintf("Code: %d, Msg: %s, Err: %s", e.Code, e.Msg, e.Err.Error())
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("Code: %d, Msg: %s", e.Code, e.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *MatcherErrors) Unwrap() error {
|
||||||
|
return e.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Matcher(rawPath string, cfg *config.Config) (string, string, string, error) {
|
||||||
|
var (
|
||||||
|
user string
|
||||||
|
repo string
|
||||||
|
matcher string
|
||||||
|
)
|
||||||
|
// 匹配 "https://github.com"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://github.com") {
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://github.com")
|
||||||
|
if strings.HasPrefix(remainingPath, "/") {
|
||||||
|
remainingPath = strings.TrimPrefix(remainingPath, "/")
|
||||||
|
}
|
||||||
|
// 预期格式/user/repo/more...
|
||||||
|
// 取出user和repo和最后部分
|
||||||
|
parts := strings.Split(remainingPath, "/")
|
||||||
|
if len(parts) <= 2 {
|
||||||
|
return "", "", "", ErrInvalidURL
|
||||||
|
}
|
||||||
|
user = parts[0]
|
||||||
|
repo = parts[1]
|
||||||
|
// 匹配 "https://github.com"开头的链接
|
||||||
|
if len(parts) >= 3 {
|
||||||
|
switch parts[2] {
|
||||||
|
case "releases", "archive":
|
||||||
|
matcher = "releases"
|
||||||
|
case "blob", "raw":
|
||||||
|
matcher = "blob"
|
||||||
|
case "info", "git-upload-pack":
|
||||||
|
matcher = "clone"
|
||||||
|
default:
|
||||||
|
return "", "", "", ErrInvalidURL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://raw"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://raw") {
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
||||||
|
parts := strings.Split(remainingPath, "/")
|
||||||
|
if len(parts) <= 3 {
|
||||||
|
return "", "", "", ErrInvalidURL
|
||||||
|
}
|
||||||
|
user = parts[1]
|
||||||
|
repo = parts[2]
|
||||||
|
matcher = "raw"
|
||||||
|
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://gist"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://gist") {
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
||||||
|
parts := strings.Split(remainingPath, "/")
|
||||||
|
if len(parts) <= 3 {
|
||||||
|
return "", "", "", ErrInvalidURL
|
||||||
|
}
|
||||||
|
user = parts[1]
|
||||||
|
repo = ""
|
||||||
|
matcher = "gist"
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://api.github.com/"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://api.github.com/") {
|
||||||
|
matcher = "api"
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://api.github.com/")
|
||||||
|
|
||||||
|
parts := strings.Split(remainingPath, "/")
|
||||||
|
if parts[0] == "repos" {
|
||||||
|
user = parts[1]
|
||||||
|
repo = parts[2]
|
||||||
|
}
|
||||||
|
if parts[0] == "users" {
|
||||||
|
user = parts[1]
|
||||||
|
}
|
||||||
|
if !cfg.Auth.ForceAllowApi {
|
||||||
|
if cfg.Auth.AuthMethod != "header" || !cfg.Auth.Enabled {
|
||||||
|
return "", "", "", ErrAuthHeaderUnavailable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
return "", "", "", ErrInvalidURL
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditorMatcher(rawPath string, cfg *config.Config) (bool, string, error) {
|
||||||
|
var (
|
||||||
|
matcher string
|
||||||
|
)
|
||||||
|
// 匹配 "https://github.com"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://github.com") {
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://github.com")
|
||||||
|
if strings.HasPrefix(remainingPath, "/") {
|
||||||
|
remainingPath = strings.TrimPrefix(remainingPath, "/")
|
||||||
|
}
|
||||||
|
return true, "", nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://raw.githubusercontent.com"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://raw.githubusercontent.com") {
|
||||||
|
return true, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://raw.github.com"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://raw.github.com") {
|
||||||
|
return true, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://gist.githubusercontent.com"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://gist.githubusercontent.com") {
|
||||||
|
return true, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://gist.github.com"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://gist.github.com") {
|
||||||
|
return true, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://api.github.com/"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://api.github.com") {
|
||||||
|
matcher = "api"
|
||||||
|
return true, matcher, nil
|
||||||
|
}
|
||||||
|
return false, "", ErrInvalidURL
|
||||||
|
}
|
||||||
|
|
||||||
|
// 匹配文件扩展名是sh的rawPath
|
||||||
|
func MatcherShell(rawPath string) bool {
|
||||||
|
if strings.HasSuffix(rawPath, ".sh") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// LinkProcessor 是一个函数类型,用于处理提取到的链接。
|
||||||
|
type LinkProcessor func(string) string
|
||||||
|
|
||||||
|
// 自定义 URL 修改函数
|
||||||
|
func modifyURL(url string, host string, cfg *config.Config) string {
|
||||||
|
// 去除url内的https://或http://
|
||||||
|
matched, _, err := EditorMatcher(url, cfg)
|
||||||
|
if err != nil {
|
||||||
|
logDump("Invalid URL: %s", url)
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
if matched {
|
||||||
|
|
||||||
|
u := strings.TrimPrefix(url, "https://")
|
||||||
|
u = strings.TrimPrefix(url, "http://")
|
||||||
|
logDump("Modified URL: %s", "https://"+host+"/"+u)
|
||||||
|
return "https://" + host + "/" + u
|
||||||
|
}
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
matchedMatchers = []string{
|
||||||
|
"blob",
|
||||||
|
"raw",
|
||||||
|
"gist",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// matchString 检查目标字符串是否在给定的字符串集合中
|
||||||
|
func matchString(target string, stringsToMatch []string) bool {
|
||||||
|
matchMap := make(map[string]struct{}, len(stringsToMatch))
|
||||||
|
for _, str := range stringsToMatch {
|
||||||
|
matchMap[str] = struct{}{}
|
||||||
|
}
|
||||||
|
_, exists := matchMap[target]
|
||||||
|
return exists
|
||||||
|
}
|
||||||
|
|
||||||
|
// processLinks 处理链接并将结果写入输出流
|
||||||
|
func processLinks(input io.Reader, output io.Writer, compress string, host string, cfg *config.Config) (written int64, err error) {
|
||||||
|
var reader *bufio.Reader
|
||||||
|
|
||||||
|
if compress == "gzip" {
|
||||||
|
// 解压gzip
|
||||||
|
gzipReader, err := gzip.NewReader(input)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("gzip解压错误: %v", err)
|
||||||
|
}
|
||||||
|
defer gzipReader.Close()
|
||||||
|
reader = bufio.NewReader(gzipReader)
|
||||||
|
} else {
|
||||||
|
reader = bufio.NewReader(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
var writer *bufio.Writer
|
||||||
|
var gzipWriter *gzip.Writer
|
||||||
|
|
||||||
|
// 根据是否gzip确定 writer 的创建
|
||||||
|
if compress == "gzip" {
|
||||||
|
gzipWriter = gzip.NewWriter(output)
|
||||||
|
writer = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小
|
||||||
|
} else {
|
||||||
|
writer = bufio.NewWriterSize(output, 4096)
|
||||||
|
}
|
||||||
|
|
||||||
|
//确保writer关闭
|
||||||
|
defer func() {
|
||||||
|
var closeErr error // 局部变量,用于保存defer中可能发生的错误
|
||||||
|
|
||||||
|
if gzipWriter != nil {
|
||||||
|
if closeErr = gzipWriter.Close(); closeErr != nil {
|
||||||
|
logError("gzipWriter close failed %v", closeErr)
|
||||||
|
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||||
|
if err == nil {
|
||||||
|
err = closeErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if flushErr := writer.Flush(); flushErr != nil {
|
||||||
|
logError("writer flush failed %v", flushErr)
|
||||||
|
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||||
|
if err == nil {
|
||||||
|
err = flushErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 使用正则表达式匹配 http 和 https 链接
|
||||||
|
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
||||||
|
for {
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
break // 文件结束
|
||||||
|
}
|
||||||
|
return written, fmt.Errorf("读取行错误: %v", err) // 传递错误
|
||||||
|
}
|
||||||
|
|
||||||
|
// 替换所有匹配的 URL
|
||||||
|
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
||||||
|
return modifyURL(originalURL, host, cfg)
|
||||||
|
})
|
||||||
|
|
||||||
|
n, werr := writer.WriteString(modifiedLine)
|
||||||
|
written += int64(n) // 更新写入的字节数
|
||||||
|
if werr != nil {
|
||||||
|
return written, fmt.Errorf("写入文件错误: %v", werr) // 传递错误
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在返回之前,再刷新一次
|
||||||
|
if fErr := writer.Flush(); fErr != nil {
|
||||||
|
return written, fErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return written, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractParts 从给定的 URL 中提取所需的部分
|
||||||
|
func extractParts(rawURL string) (string, string, string, url.Values, error) {
|
||||||
|
// 解析 URL
|
||||||
|
parsedURL, err := url.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", "", nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取路径部分并分割
|
||||||
|
pathParts := strings.Split(parsedURL.Path, "/")
|
||||||
|
|
||||||
|
// 提取所需的部分
|
||||||
|
if len(pathParts) < 3 {
|
||||||
|
return "", "", "", nil, fmt.Errorf("URL path is too short")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取 /WJQSERVER-STUDIO 和 /go-utils.git
|
||||||
|
repoOwner := "/" + pathParts[1]
|
||||||
|
repoName := "/" + pathParts[2]
|
||||||
|
|
||||||
|
// 剩余部分
|
||||||
|
remainingPath := strings.Join(pathParts[3:], "/")
|
||||||
|
if remainingPath != "" {
|
||||||
|
remainingPath = "/" + remainingPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询参数
|
||||||
|
queryParams := parsedURL.Query()
|
||||||
|
|
||||||
|
return repoOwner, repoName, remainingPath, queryParams, nil
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package proxy
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"ghproxy/config"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 预定义regex
|
|
||||||
var (
|
|
||||||
pathRegex = regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`) // 匹配路径
|
|
||||||
gistRegex = regexp.MustCompile(`^(?:https?://)?gist\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.*`) // 匹配gist路径
|
|
||||||
)
|
|
||||||
|
|
||||||
// 提取用户名和仓库名
|
|
||||||
func MatchUserRepo(rawPath string, cfg *config.Config, c *gin.Context, matches []string) (string, string) {
|
|
||||||
if gistMatches := gistRegex.FindStringSubmatch(rawPath); len(gistMatches) == 3 {
|
|
||||||
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], ""
|
|
||||||
}
|
|
||||||
// 定义路径
|
|
||||||
if pathMatches := pathRegex.FindStringSubmatch(matches[2]); len(pathMatches) >= 4 {
|
|
||||||
return pathMatches[2], pathMatches[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回错误信息
|
|
||||||
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
|
||||||
logWarning(errMsg)
|
|
||||||
c.String(http.StatusForbidden, "Invalid path; expected username/repo, Path: %s", rawPath)
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
// 日志模块
|
// 日志模块
|
||||||
var (
|
var (
|
||||||
logw = logger.Logw
|
logw = logger.Logw
|
||||||
LogDump = logger.LogDump
|
logDump = logger.LogDump
|
||||||
logDebug = logger.LogDebug
|
logDebug = logger.LogDebug
|
||||||
logInfo = logger.LogInfo
|
logInfo = logger.LogInfo
|
||||||
logWarning = logger.LogWarning
|
logWarning = logger.LogWarning
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
package proxy
|
|
||||||
|
|
||||||
/*
|
|
||||||
func ProxyRequest(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)
|
|
||||||
|
|
||||||
client := createHTTPClient(mode)
|
|
||||||
if runMode == "dev" {
|
|
||||||
client.DevMode()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发送HEAD请求, 预获取Content-Length
|
|
||||||
headReq := client.R()
|
|
||||||
setRequestHeaders(c, headReq)
|
|
||||||
AuthPassThrough(c, cfg, headReq)
|
|
||||||
|
|
||||||
headResp, err := headReq.Head(u)
|
|
||||||
if err != nil {
|
|
||||||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer headResp.Body.Close()
|
|
||||||
|
|
||||||
if err := HandleResponseSize(headResp, cfg, c); err != nil {
|
|
||||||
logWarning("%s %s %s %s %s Response-Size-Error: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := readRequestBody(c)
|
|
||||||
if err != nil {
|
|
||||||
HandleError(c, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req := client.R().SetBody(body)
|
|
||||||
setRequestHeaders(c, req)
|
|
||||||
AuthPassThrough(c, cfg, req)
|
|
||||||
|
|
||||||
resp, err := SendRequest(c, 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 {
|
|
||||||
logWarning("%s %s %s %s %s Response-Size-Error: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
CopyResponseHeaders(resp, c, cfg)
|
|
||||||
c.Status(resp.StatusCode)
|
|
||||||
if err := copyResponseBody(c, resp.Body); err != nil {
|
|
||||||
logError("%s %s %s %s %s Response-Copy-Error: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 复制响应体
|
|
||||||
func copyResponseBody(c *gin.Context, respBody io.Reader) error {
|
|
||||||
_, err := io.Copy(c.Writer, respBody)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断并选择TLS指纹
|
|
||||||
func createHTTPClient(mode string) *req.Client {
|
|
||||||
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/130.0.0.0 Safari/537.36").
|
|
||||||
SetTLSFingerprintChrome().
|
|
||||||
ImpersonateChrome()
|
|
||||||
case "git":
|
|
||||||
client.SetUserAgent("git/2.33.1")
|
|
||||||
}
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
@@ -2,6 +2,7 @@ package proxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -19,3 +20,31 @@ func removeWSHeader(req *http.Request) {
|
|||||||
req.Header.Del("Upgrade")
|
req.Header.Del("Upgrade")
|
||||||
req.Header.Del("Connection")
|
req.Header.Del("Connection")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reWriteEncodeHeader(req *http.Request) {
|
||||||
|
|
||||||
|
if isGzipAccepted(req.Header) {
|
||||||
|
req.Header.Set("Content-Encoding", "gzip")
|
||||||
|
req.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
} else {
|
||||||
|
req.Header.Del("Content-Encoding")
|
||||||
|
req.Header.Del("Accept-Encoding")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// isGzipAccepted 检查 Accept-Encoding 头部中是否包含 gzip
|
||||||
|
func isGzipAccepted(header http.Header) bool {
|
||||||
|
// 获取 Accept-Encoding 的值
|
||||||
|
encodings := header["Accept-Encoding"]
|
||||||
|
for _, encoding := range encodings {
|
||||||
|
// 将 encoding 字符串拆分为多个编码
|
||||||
|
for _, enc := range strings.Split(encoding, ",") {
|
||||||
|
// 去除空格并检查是否为 gzip
|
||||||
|
if strings.TrimSpace(enc) == "gzip" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user