Compare commits
49 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
448e06d350 | ||
|
|
27cc30ab8b | ||
|
|
a65e44ac02 | ||
|
|
a0cfe826ea | ||
|
|
2e974ad7ae | ||
|
|
b7b9cd5db5 | ||
|
|
bcb73c18de | ||
|
|
ed839b828d | ||
|
|
801b8c6cda | ||
|
|
a92bbb7fb6 | ||
|
|
3e40146281 | ||
|
|
ac7e1e43b5 | ||
|
|
f134d22540 | ||
|
|
79153c0f7d | ||
|
|
4fd47812f7 | ||
|
|
17c49d534b | ||
|
|
284b38bab4 | ||
|
|
d73dfe7db5 | ||
|
|
dc286e002c | ||
|
|
5c54ae788c | ||
|
|
bfcb1c9901 | ||
|
|
9bfe8517cb | ||
|
|
50ba185aab | ||
|
|
6ee928b0c7 | ||
|
|
979f59545b | ||
|
|
da89b3f45e | ||
|
|
498266e08e | ||
|
|
e2faa497ab | ||
|
|
8def955151 | ||
|
|
a18660121a | ||
|
|
d26f6d1e1b | ||
|
|
60a1f6073d | ||
|
|
2cc5409dd0 | ||
|
|
ad9cffe9e2 | ||
|
|
9af5010b79 | ||
|
|
19cd77afd2 | ||
|
|
91cd76e541 | ||
|
|
0faddce474 | ||
|
|
f7ba0c28b4 | ||
|
|
146dedea21 | ||
|
|
8336896979 | ||
|
|
785a74dfeb | ||
|
|
bd666e08d1 | ||
|
|
459aa24f89 | ||
|
|
a1e8e3e373 | ||
|
|
40c9ca5f38 | ||
|
|
55afe7676c | ||
|
|
0d6c1d7e35 | ||
|
|
5c14aeb48d |
5
.github/workflows/build-dev.yml
vendored
5
.github/workflows/build-dev.yml
vendored
@@ -59,6 +59,11 @@ jobs:
|
||||
else
|
||||
echo "DEV-VERSION file not found!" && exit 1
|
||||
fi
|
||||
- name: 拉取前端
|
||||
run: |
|
||||
sudo git clone https://github.com/WJQSERVER-STUDIO/GHPrxoy-Frontend.git pages
|
||||
sudo rm -rf pages/.git/
|
||||
|
||||
- name: 安装 Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
|
||||
5
.github/workflows/build.yml
vendored
5
.github/workflows/build.yml
vendored
@@ -56,6 +56,11 @@ jobs:
|
||||
else
|
||||
echo "VERSION file not found!" && exit 1
|
||||
fi
|
||||
- name: 拉取前端
|
||||
run: |
|
||||
sudo git clone https://github.com/WJQSERVER-STUDIO/GHPrxoy-Frontend.git pages
|
||||
sudo rm -rf pages/.git/
|
||||
|
||||
- name: 安装 Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,3 +2,6 @@ demo
|
||||
demo.toml
|
||||
*.log
|
||||
*.bak
|
||||
list.json
|
||||
repos
|
||||
pages
|
||||
153
CHANGELOG.md
153
CHANGELOG.md
@@ -1,5 +1,158 @@
|
||||
# 更新日志
|
||||
|
||||
3.0.0 - 2025-03-19
|
||||
---
|
||||
- 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: 更新相关依赖以修复错误
|
||||
|
||||
25w17a - 2025-03-13
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.4.1的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 重构路由匹配
|
||||
- CHANGE: 更新相关依赖以修复错误
|
||||
|
||||
2.4.0 - 2025-03-12
|
||||
---
|
||||
- ADD: 支持通过[Smart-Git](https://github.com/WJQSERVER-STUDIO/smart-git)实现Git Clone缓存
|
||||
- CHANGE: 使用更高性能的Buffer Pool 实现, 调用 github.com/WJQSERVER-STUDIO/go-utils/copyb
|
||||
- CHANGE: 改进路由匹配
|
||||
- CHANGE: 更新依赖
|
||||
- CHANGE: 改进前端
|
||||
|
||||
25w16d - 2025-03-12
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.4.0的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 使用更高性能的Buffer Pool 实现
|
||||
|
||||
25w16c
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.4.0的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 使用更高性能的Buffer Pool 实现
|
||||
- CHANGE: 改进路由匹配
|
||||
|
||||
25w16b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.4.0的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 修改路由
|
||||
- CHANGE: 改进前端
|
||||
|
||||
25w16a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.4.0的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 变更CORS配置
|
||||
- ADD: 使用GO-GIT实现git smart http服务端和客户端
|
||||
- CHANGE: 更新依赖
|
||||
|
||||
2.3.1
|
||||
---
|
||||
- CHANGE: 改进`Pages`在`External`模式下的路由
|
||||
- CHANGE: 使用`H2C` bool 代替 `enableH2C` string (2.4.0 弃用 `enableH2C`)
|
||||
- CHANGE: 使用`Mode` string 代替`Pages`内的 `enable` bool (2.4.0 弃用 `enable`)
|
||||
|
||||
25w15a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.3.1的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 改进`Pages`在`External`模式下的路由
|
||||
- CHANGE: 使用`H2C` bool 代替 `enableH2C` string (2.4.0 弃用 `enableH2C`)
|
||||
- CHANGE: 使用`Mode` string 代替`Pages`内的 `enable` bool (2.4.0 弃用 `enable`)
|
||||
|
||||
2.3.0
|
||||
---
|
||||
- CHANGE: 使用`touka-httpc`封装`HTTP Client`, 更新到`v0.2.0`版本, 参看`touka-httpc`
|
||||
- CHANGE: 重构前端页面, 见[#49](https://github.com/WJQSERVER-STUDIO/ghproxy/pull/49)
|
||||
- CHANGE: 重构`blacklist`实现
|
||||
- CHANGE: 优化404处理
|
||||
- CHANGE: 重构`whitelist`实现
|
||||
- CHANGE: 对`proxy`进行结构性调整
|
||||
- CHANGE: `chunckedreq`与`gitreq`共用`BufferPool`和`HTTP Client`
|
||||
- CHANGE: 新增`HTTP Client`配置块
|
||||
- CHANGE: 加入内置主题配置, 支持通过配置切换主题
|
||||
- CHANGE: 将许可证转为WJQserver Studio License 2.0
|
||||
|
||||
25w14b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.3.0的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 将许可证转为WJQserver Studio License 2.0
|
||||
|
||||
25w14a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v2.3.0的预发布版本,请勿在生产环境中使用;
|
||||
- CHANGE: 使用`touka-httpc`封装`HTTP Client`, 更新到`v0.2.0`版本, 参看`touka-httpc`
|
||||
- CHANGE: 重构前端页面, 见[#49](https://github.com/WJQSERVER-STUDIO/ghproxy/pull/49)
|
||||
- CHANGE: 重构`blacklist`实现
|
||||
- CHANGE: 优化404处理
|
||||
- CHANGE: 重构`whitelist`实现
|
||||
- CHANGE: 对`proxy`进行结构性调整
|
||||
- CHANGE: `chunckedreq`与`gitreq`共用`BufferPool`和`HTTP Client`
|
||||
- CHANGE: 新增`HTTP Client`配置块
|
||||
- CHANGE: 加入内置主题配置, 支持通过配置切换主题
|
||||
|
||||
25w14t-2
|
||||
---
|
||||
- PRE-RELEASE: 此版本是测试验证版本,请勿在生产环境中使用;
|
||||
- CHANGE: 使用`touka-httpc`封装`HTTP Client`,更新到`v0.1.0`版本, 参看`touka-httpc`
|
||||
- CHANGE: 重构`whitelist`实现
|
||||
- CHANGE: 对`proxy`进行结构性调整
|
||||
- CHANGE: `chunckedreq`与`gitreq`共用`BufferPool`和`HTTP Client`
|
||||
- CHANGE: 新增`HTTP Client`配置块
|
||||
|
||||
25w14t-1
|
||||
---
|
||||
- PRE-RELEASE: 此版本是测试验证版本,请勿在生产环境中使用;
|
||||
- CHANGE: 使用`touka-httpc`封装`HTTP Client`
|
||||
- CHANGE: 重构前端页面, 见[#49](https://github.com/WJQSERVER-STUDIO/ghproxy/pull/49)
|
||||
- CHANGE: 重构`blacklist`实现
|
||||
- CHANGE: 优化404处理
|
||||
|
||||
2.2.0
|
||||
---
|
||||
- RELEASE: v2.2.0正式版发布;
|
||||
|
||||
@@ -1 +1 @@
|
||||
25w13b
|
||||
25w20b
|
||||
214
LICENSE
214
LICENSE
@@ -1,107 +1,197 @@
|
||||
WJQserver Studio 开源许可证
|
||||
版本 1.2
|
||||
版本 v2.0
|
||||
|
||||
版权所有 © WJQserver Studio 2024
|
||||
|
||||
定义
|
||||
许可:指在本许可证内定义的使用、复制、分发与修改的条款与要求。
|
||||
授权方:指拥有版权的个人或组织,亦或是拥有版权的个人或组织所指派的实体。
|
||||
您:指行使本许可授予的权限的个人或法律实体。
|
||||
开源与自由软件
|
||||
本项目为开源软件,允许用户在遵循本许可证的前提下访问和使用源代码。
|
||||
本项目不等同于自由软件,使用权限受到本许可证条款的限制。
|
||||
强调版权所有,所有权利均由 WJQserver Studio 保留。
|
||||
许可证条款
|
||||
1. 使用权限
|
||||
1.1 您被授予在私人环境中自由使用本软件的权限。
|
||||
|
||||
1.2 您可以在不修改关键声明的前提下进行商用。
|
||||
* 许可 (License): 指的是在本许可证内定义的使用、复制、分发与修改软件的条款与要求。
|
||||
* 授权方 (Licensor): 指的是拥有版权的个人或组织,亦或是拥有版权的个人或组织所指派的实体,在本许可证中特指 WJQserver Studio。
|
||||
* 贡献者 (Contributor): 指的是授权方以及根据本许可证授予贡献代码或软件的个人或实体。
|
||||
* 您 (You): 指的是行使本许可授予的权限的个人或法律实体。
|
||||
* 衍生作品 (Derivative Works): 指的是基于本软件或本软件任何部分的修改作品,无论修改程度如何。这包括但不限于基于本软件或其任何部分的修改、修订、改编、翻译或其他形式的创作,以及包含本软件或其部分的集合作品。
|
||||
* 非营利性使用 (Non-profit Use): 指的是不以直接商业盈利为主要目的的使用方式,包括但不限于:
|
||||
* 个人用途: 由个人为了个人学习、研究、实验、非商业项目、个人网站搭建、毕业设计、家庭内部娱乐等非直接商业目的使用软件。
|
||||
* 教育用途: 在教育机构(如学校、大学、培训机构)内部用于教学、研究、学术交流等活动。
|
||||
* 科研用途: 在科研院所、实验室等机构内部用于科学研究、实验开发等活动。
|
||||
* 慈善与公益用途: 由慈善机构、公益组织等非营利性组织为了其公益使命或慈善事业内部运营使用,或对外提供不直接产生商业利润的公益服务。
|
||||
* 内部运营用途 (非营利组织): 非营利性组织在其内部运营中使用软件,例如用于行政管理、会员管理、内部沟通、项目管理等非直接营利性活动。
|
||||
|
||||
开源与自由软件
|
||||
|
||||
本项目为开源软件,允许用户在遵循本许可证的前提下访问和使用源代码。
|
||||
本项目旨在向用户提供尽可能广泛的非商业使用自由,同时保障社区的共同发展和良性生态,并为商业创新提供清晰的路径。
|
||||
强调版权所有,所有权利由 WJQserver Studio 及贡献者共同保留。
|
||||
|
||||
许可证条款
|
||||
|
||||
1. 使用权限
|
||||
|
||||
* 1.1 非营利性使用: 您被授予在非营利性使用场景下,为了任何目的,自由使用本软件的权限。 非营利性使用的具体场景包括但不限于定义部分所列举的各种情况。
|
||||
|
||||
* 1.2 商业使用: 您可以在商业环境中使用本软件,无需获得额外授权,但您的商业使用行为必须遵守以下条款:
|
||||
|
||||
* 1.2.1 保持声明: 您在进行商业使用时,不得移除或修改软件中包含的原始版权声明、许可证声明以及来源声明。
|
||||
* 1.2.2 开源继承 (Copyleft) 与互惠共享: 如果您或您的组织希望将本软件或其衍生作品用于任何商业用途,包括但不限于:
|
||||
|
||||
* 盈利性分发: 销售、出租、许可分发本软件或其衍生作品。
|
||||
* 盈利性服务: 基于本软件或其衍生作品提供商业服务,例如 SaaS 服务、咨询服务、定制开发服务、收费技术支持服务等。
|
||||
* 嵌入式商业应用: 将本软件或其衍生作品嵌入到商业产品或解决方案中进行销售。
|
||||
* 组织内部商业运营: 在营利性组织的内部运营中使用修改后的版本以直接支持其商业活动,例如定制化内部系统,通过例如但不限于在软件或相关服务中投放广告 (例如 Google Ads 等),应用内购买 (内购), 会员订阅, 增值功能收费等方式直接或间接产生商业收入。
|
||||
|
||||
您必须选择以下两种方式之一:
|
||||
|
||||
* i) 继承本许可证并开源: 您必须以本许可证或兼容的开源许可证分发您的衍生作品,并公开您的衍生作品的全部源代码,使得您的衍生作品的接收者也享有与您相同的权利,包括进一步修改和商业使用的权利。 本选项旨在促进社区的共同发展和知识共享,确保基于本软件的商业创新成果也能回馈社区。
|
||||
* ii) 获得授权方明确授权: 如果您不希望以开源方式发布您的衍生作品,或者希望使用其他许可证进行分发,或者您希望在商业运营中使用修改后的版本但不开源,您必须事先获得 WJQserver Studio 的明确书面授权。 授权的具体条款和条件将由 WJQserver Studio 另行协商确定。
|
||||
|
||||
2. 复制与分发
|
||||
2.1 您可以复制和分发本软件的原始版本,前提是必须保留所有版权声明和本许可证。
|
||||
|
||||
* 2.1 原始版本复制与分发: 您可以复制和分发本软件的原始版本,前提是必须满足以下条件:
|
||||
|
||||
* 保留所有声明: 完整保留所有原始版权声明、许可证声明、来源声明以及其他所有权声明。
|
||||
* 附带许可证: 在分发软件时,必须同时附带本许可证的完整文本,确保接收者知悉并理解本许可证的全部条款。
|
||||
|
||||
* 2.2 衍生作品复制与分发: 您可以复制和分发基于本软件的衍生作品,您对衍生作品的分发行为将受到本许可证第 1.2.2 条(开源继承与互惠共享)的约束。
|
||||
|
||||
3. 修改权限
|
||||
3.1 您可以在非商业用途下修改本软件,前提是继承本许可证并保留原版权声明。
|
||||
|
||||
3.2 禁止在修改后进行商业用途。
|
||||
* 3.1 自由修改: 您被授予自由修改本软件的权限,无论修改目的是非营利性使用还是商业用途。
|
||||
|
||||
4. 专利引用
|
||||
4.1 若项目被专利相关引用,必须保留来源声明。
|
||||
* 3.2 修改后使用与分发约束: 当您将修改后的版本用于商业用途或分发修改后的版本时,您需要遵守本许可证第 1.2.2 条(开源继承与互惠共享)以及第 2 条(复制与分发)的规定。 即使您不分发修改后的版本,只要您将其用于商业目的,也需要遵守开源继承条款或获得授权。
|
||||
|
||||
4.2 若为商业场景,需按照商用处理。
|
||||
* 3.3 贡献接受: WJQserver Studio 鼓励社区贡献代码。如果您向本项目贡献代码,您需要同意您的贡献代码按照本许可证条款进行许可。
|
||||
|
||||
4. 专利权
|
||||
|
||||
* 4.1 无专利担保,风险自担: 本软件以“现状”提供,授权方及贡献者明确声明,不对本软件的专利侵权问题做任何形式的担保,亦不承担任何因专利侵权可能产生的责任与后果。 用户理解并同意,使用本软件的专利风险完全由用户自行承担。
|
||||
|
||||
* 4.2 专利纠纷应对: 如因用户使用本软件而引发任何专利侵权指控、诉讼或索赔,用户应自行负责处理并承担全部法律责任。 授权方及贡献者无义务参与任何相关法律程序,亦不承担任何由此产生的费用或赔偿。
|
||||
|
||||
5. 免责声明
|
||||
5.1 本软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性及非侵权性。
|
||||
|
||||
5.2 在任何情况下,授权方均不对因使用或无法使用本软件而产生的任何直接、间接、偶然、特殊、惩罚性或后果性损害负责,即使已被告知可能发生此类损害。
|
||||
* 5.1 “现状”提供,无任何保证: 本软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性及非侵权性。
|
||||
|
||||
5.3 用户需根据当地法律对待本项目,确保遵守所有适用法规。
|
||||
* 5.2 责任限制: 在适用法律允许的最大范围内,在任何情况下,授权方或任何贡献者均不对因使用或无法使用本软件而产生的任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于采购替代商品或服务;损失使用、数据或利润;或业务中断)负责,无论其是如何造成的,也无论依据何种责任理论,即使已被告知可能发生此类损害。
|
||||
|
||||
6. 许可证期限
|
||||
6.1 本许可证自2024年开始生效,有效期暂为无限。
|
||||
* 5.3 用户法律责任: 用户需根据当地法律对待本项目,确保遵守所有适用法规。
|
||||
|
||||
6.2 项目所有方有权修改许可证相关条例而不另行通知。
|
||||
6. 许可证期限与终止
|
||||
|
||||
条款修订
|
||||
7.1 授权方保留随时修改本许可证条款的权利,以便更好地适应法律和技术的发展。
|
||||
* 6.1 许可证期限: 除版权所有人主动宣布放弃本软件版权外,本许可证无限期生效。
|
||||
|
||||
7.2 修订后的条款将在发布时生效,继续使用本软件即表示接受修订后的条款。
|
||||
* 6.2 许可证终止: 如果您未能遵守本许可证的任何条款或条件,授权方有权终止本许可证。 您的许可证将在您违反本许可证条款时自动终止。
|
||||
|
||||
其他
|
||||
8.1 本许可证不影响您作为最终用户的法定权利。
|
||||
* 6.3 终止后的效力: 许可证终止后,您根据本许可证所享有的所有权利将立即终止,但您在许可证终止前已合法分发的软件副本,其接收者所获得的许可及权利将不受影响,继续有效。 免责声明(第 5 条)和责任限制(第 5.2 条)在本许可证终止后仍然有效。
|
||||
|
||||
8.2 若本许可证的某些条款被认定为不可执行,其余条款仍然有效。
|
||||
7. 条款修订
|
||||
|
||||
* 7.1 修订权利保留: 授权方保留随时修改本许可证条款的权利,以便更好地适应法律、技术发展以及社区需求。
|
||||
|
||||
* 7.2 修订生效与接受: 修订后的条款将在发布时生效,除非另行声明,否则继续使用、复制、分发或修改本软件即表示您接受修订后的条款。授权方鼓励用户定期查阅本许可证的最新版本。
|
||||
|
||||
8. 其他
|
||||
|
||||
* 8.1 法定权利: 本许可证不影响您作为最终用户在适用法律下的法定权利。
|
||||
|
||||
* 8.2 条款可分割性: 若本许可证的某些条款被认定为不可执行,其余条款仍然完全有效。
|
||||
|
||||
* 8.3 版本更新: 授权方可能会发布本许可证的修订版本或新版本。您可以选择是继续使用本许可证的旧版本还是选择适用新版本。
|
||||
|
||||
WJQserver Studio Open Source License
|
||||
Version 1.2
|
||||
Version v2.0
|
||||
|
||||
Copyright © WJQserver Studio 2024
|
||||
|
||||
Definitions
|
||||
License: The terms and conditions defined within this license for use, copying, distribution, and modification.
|
||||
Licensor: The individual or organization holding the copyright, or the entity designated by them.
|
||||
You: The individual or legal entity exercising the permissions granted by this license.
|
||||
Open Source vs. Free Software
|
||||
This project is open source, allowing users to access and use the source code under the terms of this license.
|
||||
This project is not equivalent to free software; usage rights are restricted by this license.
|
||||
Copyright is emphasized, with all rights reserved by WJQserver Studio.
|
||||
|
||||
* License: Refers to the terms and requirements for use, reproduction, distribution, and modification defined within this license.
|
||||
* Licensor: Refers to the individual or organization that holds the copyright, or the entity designated by the copyright holder, specifically WJQserver Studio in this license.
|
||||
* Contributor: Refers to the Licensor and individuals or entities who contribute code or software under this License.
|
||||
* You: Refers to the individual or legal entity exercising permissions granted by this License.
|
||||
* Derivative Works: Refers to works modified based on the Software or any part thereof, regardless of the extent of modification. This includes but is not limited to modifications, revisions, adaptations, translations, or other forms of creation based on the Software or any part thereof, as well as collective works containing the Software or parts thereof.
|
||||
* Non-profit Use: Refers to uses not primarily intended for direct commercial profit, including but not limited to:
|
||||
* Personal Use: Use by an individual for personal learning, research, experimentation, non-commercial projects, personal website development, graduation projects, home entertainment, and other non-directly commercial purposes.
|
||||
* Educational Use: Use within educational institutions (such as schools, universities, training organizations) for activities such as teaching, research, and academic exchange.
|
||||
* Scientific Research Use: Use within scientific research institutions, laboratories, and similar organizations for activities such as scientific research and experimental development.
|
||||
* Charitable and Public Welfare Use: Use by charitable organizations, public welfare organizations, and similar non-profit entities for their public missions or internal operation of charitable activities, or to provide public services that do not directly generate commercial profit.
|
||||
* Internal Operational Use (Non-profit Organizations): Use within the internal operations of non-profit organizations, such as for administrative management, membership management, internal communication, project management, and other non-directly profit-generating activities.
|
||||
|
||||
Open Source and Free Software
|
||||
|
||||
This project is open-source software, allowing users to access and use the source code under the premise of complying with this License.
|
||||
This project aims to provide users with the broadest possible freedom for non-commercial use while ensuring the common development and healthy ecosystem of the community, and providing a clear path for commercial innovation.
|
||||
Copyright is emphasized; all rights are jointly reserved by WJQserver Studio and Contributors.
|
||||
|
||||
License Terms
|
||||
1. Usage Rights
|
||||
1.1 You are granted the right to use this software freely in a private environment.
|
||||
|
||||
1.2 You may use it commercially without modifying key statements.
|
||||
1. Permissions for Use
|
||||
|
||||
2. Copying and Distribution
|
||||
2.1 You may copy and distribute the original version of this software, provided all copyright notices and this license are retained.
|
||||
* 1.1 Non-profit Use: You are granted permission to freely use the Software for any purpose in non-profit use scenarios. Specific non-profit use scenarios include but are not limited to the various situations listed in the Definition section.
|
||||
|
||||
3. Modification Rights
|
||||
3.1 You may modify this software for non-commercial purposes, provided you inherit this license and retain the original copyright notice.
|
||||
* 1.2 Commercial Use: You may use the Software in a commercial environment without additional authorization, but your commercial use must comply with the following terms:
|
||||
|
||||
3.2 Modifications cannot be used commercially.
|
||||
* 1.2.1 Maintain Statements: When conducting commercial use, you must not remove or modify the original copyright notices, license notices, and source statements contained in the Software.
|
||||
* 1.2.2 Open Source Inheritance (Copyleft) and Reciprocal Sharing: If you or your organization wish to use the Software or its Derivative Works for any commercial purpose, including but not limited to:
|
||||
|
||||
4. Patent References
|
||||
4.1 If the project is cited in patent-related contexts, the source statement must be retained.
|
||||
* Profit-generating Distribution: Selling, renting, licensing, or distributing the Software or its Derivative Works.
|
||||
* Profit-generating Services: Providing commercial services based on the Software or its Derivative Works, such as SaaS services, consulting services, custom development services, and paid technical support services.
|
||||
* Embedded Commercial Applications: Embedding the Software or its Derivative Works into commercial products or solutions for sale.
|
||||
* Internal Commercial Operations: Using modified versions within the internal operations of for-profit organizations to directly support their commercial activities, such as customized internal systems, generating commercial revenue directly or indirectly through means including but not limited to placing advertisements in the software or related services (e.g., Google Ads), in-app purchases, membership subscriptions, and charging for value-added features.
|
||||
|
||||
4.2 For commercial scenarios, it must be treated as a commercial use.
|
||||
You must choose one of the following two options:
|
||||
|
||||
5. Disclaimer
|
||||
5.1 This software is provided "as is", without any express or implied warranties, including but not limited to merchantability, fitness for a particular purpose, and non-infringement.
|
||||
* i) Inherit this License and Open Source: You must distribute your Derivative Works under this License or a compatible open-source license and publicly disclose the entire source code of your Derivative Works, so that recipients of your Derivative Works also enjoy the same rights as you, including the right to further modify and use commercially. This option aims to promote the common development and knowledge sharing of the community, ensuring that commercial innovation achievements based on this Software can also contribute back to the community.
|
||||
* ii) Obtain Explicit Authorization from the Licensor: If you do not wish to release your Derivative Works in an open-source manner, or wish to distribute them under another license, or you wish to use a modified version in commercial operations without open-sourcing it, you must obtain explicit written authorization from WJQserver Studio in advance. The specific terms and conditions of authorization will be determined separately by WJQserver Studio through negotiation.
|
||||
|
||||
5.2 In no event shall the licensor be liable for any direct, indirect, incidental, special, punitive, or consequential damages arising out of the use or inability to use this software, even if advised of the possibility of such damages.
|
||||
2. Reproduction and Distribution
|
||||
|
||||
5.3 Users must comply with all applicable laws regarding this project.
|
||||
* 2.1 Reproduction and Distribution of Original Version: You may reproduce and distribute the original version of the Software, provided that the following conditions are met:
|
||||
|
||||
6. License Duration
|
||||
6.1 This license is effective from 2024, with an indefinite duration.
|
||||
* Retain All Statements: Completely retain all original copyright notices, license notices, source statements, and other proprietary notices.
|
||||
* Accompany with License: When distributing the Software, you must also include the full text of this License to ensure that recipients are aware of and understand all terms of this License.
|
||||
|
||||
6.2 The project owner reserves the right to modify the license terms without prior notice.
|
||||
* 2.2 Reproduction and Distribution of Derivative Works: You may reproduce and distribute Derivative Works based on the Software. Your distribution of Derivative Works will be subject to the constraints of Clause 1.2.2 of this License (Open Source Inheritance and Reciprocal Sharing).
|
||||
|
||||
Amendments
|
||||
7.1 The licensor reserves the right to amend this license at any time to better adapt to legal and technological developments.
|
||||
3. Modification Permissions
|
||||
|
||||
7.2 Revised terms become effective upon publication, and continued use of the software indicates acceptance of the revised terms.
|
||||
* 3.1 Free Modification: You are granted permission to freely modify the Software, regardless of whether the purpose of modification is for non-profit use or commercial use.
|
||||
|
||||
Miscellaneous
|
||||
8.1 This license does not affect your statutory rights as an end user.
|
||||
* 3.2 Constraints on Use and Distribution after Modification: When you use a modified version for commercial purposes or distribute a modified version, you need to comply with the provisions of Clause 1.2.2 of this License (Open Source Inheritance and Reciprocal Sharing) and Clause 2 (Reproduction and Distribution). Even if you do not distribute the modified version, as long as you use it for commercial purposes, you also need to comply with the open-source inheritance clause or obtain authorization.
|
||||
|
||||
8.2 If any provision of this license is held to be unenforceable, the remaining provisions shall remain in effect.
|
||||
* 3.3 Contribution Acceptance: WJQserver Studio encourages community contribution of code. If you contribute code to this project, you need to agree that your contributed code is licensed under the terms of this License.
|
||||
|
||||
4. Patent Rights
|
||||
|
||||
* 4.1 No Patent Warranty, Risk Self-Bearing: The software is provided “AS IS”, and the Licensor and Contributors explicitly declare that they do not provide any form of warranty regarding patent infringement issues of this software, nor do they assume any responsibility and consequences arising from patent infringement. Users understand and agree that the patent risk of using this software is entirely borne by the users themselves.
|
||||
|
||||
* 4.2 Handling of Patent Disputes: If any patent infringement allegations, lawsuits, or claims arise due to the user's use of this Software, the user shall be solely responsible for handling and bear all legal liabilities. The Licensor and Contributors are under no obligation to participate in any related legal proceedings, nor do they bear any costs or compensation arising therefrom.
|
||||
|
||||
5. Disclaimer of Warranty
|
||||
|
||||
* 5.1 “AS IS” Provision, No Warranty: The software is provided “AS IS” without any express or implied warranties, including but not limited to warranties of merchantability, fitness for a particular purpose, and non-infringement.
|
||||
|
||||
* 5.2 Limitation of Liability: To the maximum extent permitted by applicable law, in no event shall the Licensor or any Contributor be liable for any direct, indirect, incidental, special, punitive, or consequential damages (including but not limited to procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
|
||||
|
||||
* 5.3 User Legal Responsibility: Users shall treat this project in accordance with local laws and regulations to ensure compliance with all applicable laws and regulations.
|
||||
|
||||
6. License Term and Termination
|
||||
|
||||
* 6.1 License Term: Unless the copyright holder proactively announces the abandonment of the copyright of this software, this License shall be effective indefinitely from the date of your acceptance.
|
||||
|
||||
* 6.2 License Termination: If you fail to comply with any terms or conditions of this License, the Licensor has the right to terminate this License. Your License will automatically terminate upon your violation of the terms of this License.
|
||||
|
||||
* 6.3 Effect after Termination: Upon termination of the License, all rights granted to you under this License will terminate immediately, but the licenses and rights obtained by recipients of software copies you have legally distributed before the termination of the License will not be affected and will remain valid. The Disclaimer of Warranty (Clause 5) and Limitation of Liability (Clause 5.2) shall remain in effect after the termination of this License.
|
||||
|
||||
7. Revision of Terms
|
||||
|
||||
* 7.1 Reservation of Revision Rights: The Licensor reserves the right to modify the terms of this License at any time to better adapt to legal, technological developments, and community needs.
|
||||
|
||||
* 7.2 Effectiveness and Acceptance of Revisions: Revised terms will take effect upon publication, and unless otherwise stated, continued use, reproduction, distribution, or modification of the Software indicates your acceptance of the revised terms. The Licensor encourages users to periodically review the latest version of this License.
|
||||
|
||||
8. Other
|
||||
|
||||
* 8.1 Statutory Rights: This License does not affect your statutory rights as an end-user under applicable laws.
|
||||
|
||||
* 8.2 Severability of Terms: If certain terms of this License are deemed unenforceable, the remaining terms shall remain in full force and effect.
|
||||
|
||||
* 8.3 Version Updates: The Licensor may publish revised versions or new versions of this License. You may choose to continue using the old version of this License or choose to apply the new version.
|
||||
|
||||
69
README.md
69
README.md
@@ -16,12 +16,17 @@
|
||||
|
||||
### 项目特点
|
||||
|
||||
- 基于Go语言实现,使用[Gin框架](https://github.com/gin-gonic/gin)
|
||||
- 基于Go语言实现,支持多平台
|
||||
- 使用字节旗下的[HertZ](https://github.com/cloudwego/hertz)作为Web框架
|
||||
- 使用[Touka-HTTPC](https://github.com/satomitouka/touka-httpc)作为HTTP客户端
|
||||
- 支持Git clone,raw,realeases等文件拉取
|
||||
- 支持多个前端主题
|
||||
- 支持自定义黑名单/白名单
|
||||
- 支持Git Clone缓存(配合组件)
|
||||
- 支持Docker部署
|
||||
- 支持速率限制
|
||||
- 支持用户鉴权
|
||||
- 支持自定义黑名单/白名单
|
||||
- 支持shell脚本嵌套加速
|
||||
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
||||
|
||||
### 项目开发过程
|
||||
@@ -29,13 +34,17 @@
|
||||
**本项目是[WJQSERVER-STUDIO/ghproxy-go](https://github.com/WJQSERVER-STUDIO/ghproxy-go)的重构版本,实现了原项目原定功能的同时,进一步优化了性能**
|
||||
关于此项目的详细开发过程,请参看Commit记录与[CHANGELOG.md](https://github.com/WJQSERVER-STUDIO/ghproxy/blob/main/CHANGELOG.md)
|
||||
|
||||
- V2.0.0 对`proxy`核心模块进行了重构,大幅优化内存占用
|
||||
- V1.0.0 迁移至本仓库,并再次重构内容实现
|
||||
- v3.0.0 迁移到HertZ框架, 进一步提升效率, 同时v3.0.0与v2.4.0及以上版本兼容, 可直接平顺升级
|
||||
- v2.4.1 对路径匹配进行优化
|
||||
- v2.0.0 对`proxy`核心模块进行了重构,大幅优化内存占用
|
||||
- v1.0.0 迁移至本仓库,并再次重构内容实现
|
||||
- v0.2.0 重构项目实现
|
||||
|
||||
### LICENSE
|
||||
|
||||
本项目使用WSL LICENSE Version1.2 (WJQSERVER STUDIO LICENSE Version1.2)
|
||||
本项目使用WJQserver Studio License 2.0 [WJQserver Studio License 2.0](https://wjqserver-studio.github.io/LICENSE/LICENSE.html)
|
||||
|
||||
在v2.3.0之前, 本项目使用WJQserver Studio License 1.2
|
||||
|
||||
在v1.0.0版本之前,本项目继承于[WJQSERVER-STUDIO/ghproxy-go](https://github.com/WJQSERVER-STUDIO/ghproxy-go)的APACHE2.0 LICENSE VERSION
|
||||
|
||||
@@ -44,9 +53,11 @@
|
||||
```
|
||||
# 下载文件
|
||||
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/https://github.com/WJQSERVER-STUDIO/ghproxy.git
|
||||
```
|
||||
|
||||
## 部署说明
|
||||
@@ -89,10 +100,26 @@ wget -O install-dev.sh https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghprox
|
||||
host = "0.0.0.0" # 监听地址
|
||||
port = 8080 # 监听端口
|
||||
sizeLimit = 125 # 125MB
|
||||
enableH2C = "on" # 是否开启H2C传输(latest和dev版本请开启) on/off
|
||||
H2C = true # 是否开启H2C传输
|
||||
cors = "*" # "*"/"" -> "*" ; "nil" -> "" ; 除以上特殊情况, 会将值直接传入
|
||||
|
||||
[httpc]
|
||||
mode = "auto" # "auto" or "advanced" HTTP客户端模式 自动/高级模式
|
||||
maxIdleConns = 100 # only for advanced mode 仅用于高级模式
|
||||
maxIdleConnsPerHost = 60 # only for advanced mode 仅用于高级模式
|
||||
maxConnsPerHost = 0 # only for advanced mode 仅用于高级模式
|
||||
|
||||
[gitclone]
|
||||
mode = "bypass" # bypass / cache 运行模式, cache模式依赖smart-git
|
||||
smartGitAddr = "http://127.0.0.1:8080" # smart-git组件地址
|
||||
ForceH2C = false # 强制使用H2C连接
|
||||
|
||||
[shell]
|
||||
editor = false # 脚本嵌套加速
|
||||
|
||||
[pages]
|
||||
enabled = false # 是否开启内置静态页面(Docker版本请关闭此项)
|
||||
mode = "internal" # "internal" or "external" 内部/外部 前端 默认内部
|
||||
theme = "bootstrap" # "bootstrap" or "nebula" 内置主题
|
||||
staticPath = "/data/www" # 静态页面文件路径
|
||||
|
||||
[log]
|
||||
@@ -100,13 +127,11 @@ logFilePath = "/data/ghproxy/log/ghproxy.log" # 日志文件路径
|
||||
maxLogSize = 5 # MB 日志文件最大大小
|
||||
level = "info" # 日志级别 dump, debug, info, warn, error, none
|
||||
|
||||
[cors]
|
||||
enabled = true # 是否开启跨域
|
||||
|
||||
[auth]
|
||||
authMethod = "parameters" # 鉴权方式,支持parameters,header
|
||||
authToken = "token" # 用户鉴权Token
|
||||
enabled = false # 是否开启用户鉴权
|
||||
ForceAllowApi = false # 在不开启Header鉴权的情况下允许api代理
|
||||
|
||||
[blacklist]
|
||||
blacklistFile = "/data/ghproxy/config/blacklist.json" # 黑名单文件路径
|
||||
@@ -137,6 +162,7 @@ url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890" 支持Socks5/HTTP(S)
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/*"
|
||||
"another"
|
||||
]
|
||||
}
|
||||
```
|
||||
@@ -151,26 +177,21 @@ url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890" 支持Socks5/HTTP(S)
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/*"
|
||||
"another"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Caddy反代配置
|
||||
|
||||
```Caddyfile
|
||||
example.com {
|
||||
reverse_proxy {
|
||||
to 127.0.0.1:7210
|
||||
}
|
||||
encode zstd gzip
|
||||
}
|
||||
```
|
||||
|
||||
### 前端页面
|
||||
|
||||
#### Bootstrap主题
|
||||

|
||||

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

|
||||

|
||||
|
||||
## 赞助
|
||||
|
||||
如果您觉得本项目对您有帮助,欢迎赞助支持,您的赞助将用于Demo服务器开支及开发者时间成本支出,感谢您的支持!
|
||||
@@ -179,6 +200,10 @@ example.com {
|
||||
|
||||
爱发电: https://afdian.com/a/wjqserver
|
||||
|
||||
USDT(TRC20): `TNfSYG6F2vkiibd6J6mhhHNWDgWgNdF5hN`
|
||||
|
||||
### 捐赠列表
|
||||
|
||||
虚位以待...
|
||||
| 赞助人 |金额|
|
||||
|--------|------|
|
||||
| starry | 8 USDT (TRC20) |
|
||||
|
||||
154
api/api.go
154
api/api.go
@@ -1,129 +1,137 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"ghproxy/config"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
router *gin.Engine
|
||||
cfg *config.Config
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
)
|
||||
|
||||
var (
|
||||
logw = logger.Logw
|
||||
LogDump = logger.LogDump
|
||||
logDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
logError = logger.LogError
|
||||
)
|
||||
|
||||
func NoCacheMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
func NoCacheMiddleware() app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
// 设置禁止缓存的响应头
|
||||
c.Header("Cache-Control", "no-store, no-cache, must-revalidate")
|
||||
c.Header("Pragma", "no-cache")
|
||||
c.Header("Expires", "0")
|
||||
c.Next() // 继续处理请求
|
||||
c.Response.Header.Set("Cache-Control", "no-store, no-cache, must-revalidate")
|
||||
c.Response.Header.Set("Pragma", "no-cache")
|
||||
c.Response.Header.Set("Expires", "0")
|
||||
c.Next(ctx) // 继续处理请求
|
||||
}
|
||||
}
|
||||
|
||||
func InitHandleRouter(cfg *config.Config, router *gin.Engine, version string) {
|
||||
apiRouter := router.Group("api", NoCacheMiddleware())
|
||||
func InitHandleRouter(cfg *config.Config, r *server.Hertz, version string) {
|
||||
apiRouter := r.Group("/api", NoCacheMiddleware())
|
||||
{
|
||||
apiRouter.GET("/size_limit", func(c *gin.Context) {
|
||||
SizeLimitHandler(cfg, c)
|
||||
apiRouter.GET("/size_limit", func(ctx context.Context, c *app.RequestContext) {
|
||||
SizeLimitHandler(cfg, c, ctx)
|
||||
})
|
||||
apiRouter.GET("/whitelist/status", func(c *gin.Context) {
|
||||
WhiteListStatusHandler(c, cfg)
|
||||
apiRouter.GET("/whitelist/status", func(ctx context.Context, c *app.RequestContext) {
|
||||
WhiteListStatusHandler(cfg, c, ctx)
|
||||
})
|
||||
apiRouter.GET("/blacklist/status", func(c *gin.Context) {
|
||||
BlackListStatusHandler(c, cfg)
|
||||
apiRouter.GET("/blacklist/status", func(ctx context.Context, c *app.RequestContext) {
|
||||
BlackListStatusHandler(cfg, c, ctx)
|
||||
})
|
||||
apiRouter.GET("/cors/status", func(c *gin.Context) {
|
||||
CorsStatusHandler(c, cfg)
|
||||
apiRouter.GET("/cors/status", func(ctx context.Context, c *app.RequestContext) {
|
||||
CorsStatusHandler(cfg, c, ctx)
|
||||
})
|
||||
apiRouter.GET("/healthcheck", func(c *gin.Context) {
|
||||
HealthcheckHandler(c)
|
||||
apiRouter.GET("/healthcheck", func(ctx context.Context, c *app.RequestContext) {
|
||||
HealthcheckHandler(c, ctx)
|
||||
})
|
||||
apiRouter.GET("/version", func(c *gin.Context) {
|
||||
VersionHandler(c, version)
|
||||
apiRouter.GET("/version", func(ctx context.Context, c *app.RequestContext) {
|
||||
VersionHandler(c, ctx, version)
|
||||
})
|
||||
apiRouter.GET("/rate_limit/status", func(c *gin.Context) {
|
||||
RateLimitStatusHandler(c, cfg)
|
||||
apiRouter.GET("/rate_limit/status", func(ctx context.Context, c *app.RequestContext) {
|
||||
RateLimitStatusHandler(cfg, c, ctx)
|
||||
})
|
||||
apiRouter.GET("/rate_limit/limit", func(c *gin.Context) {
|
||||
RateLimitLimitHandler(c, cfg)
|
||||
apiRouter.GET("/rate_limit/limit", func(ctx context.Context, c *app.RequestContext) {
|
||||
RateLimitLimitHandler(cfg, c, ctx)
|
||||
})
|
||||
apiRouter.GET("/smartgit/status", func(ctx context.Context, c *app.RequestContext) {
|
||||
SmartGitStatusHandler(cfg, c, ctx)
|
||||
})
|
||||
|
||||
}
|
||||
logInfo("API router Init success")
|
||||
}
|
||||
|
||||
func SizeLimitHandler(cfg *config.Config, c *gin.Context) {
|
||||
func SizeLimitHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
sizeLimit := cfg.Server.SizeLimit
|
||||
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{}{
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"MaxResponseBodySize": sizeLimit,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func WhiteListStatusHandler(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{}{
|
||||
func WhiteListStatusHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"Whitelist": cfg.Whitelist.Enabled,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func BlackListStatusHandler(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{}{
|
||||
func BlackListStatusHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"Blacklist": cfg.Blacklist.Enabled,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func CorsStatusHandler(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{}{
|
||||
"Cors": cfg.CORS.Enabled,
|
||||
})
|
||||
func CorsStatusHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"Cors": cfg.Server.Cors,
|
||||
}))
|
||||
}
|
||||
|
||||
func HealthcheckHandler(c *gin.Context) {
|
||||
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{}{
|
||||
func HealthcheckHandler(c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"Status": "OK",
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func VersionHandler(c *gin.Context, version string) {
|
||||
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{}{
|
||||
func VersionHandler(c *app.RequestContext, ctx context.Context, version string) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"Version": version,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func RateLimitStatusHandler(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{}{
|
||||
func RateLimitStatusHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"RateLimit": cfg.RateLimit.Enabled,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func RateLimitLimitHandler(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{}{
|
||||
func RateLimitLimitHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"RatePerMinute": cfg.RateLimit.RatePerMinute,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func SmartGitStatusHandler(cfg *config.Config, c *app.RequestContext, ctx context.Context) {
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
c.Response.Header.Set("Content-Type", "application/json")
|
||||
c.JSON(200, (map[string]interface{}{
|
||||
"enabled": cfg.GitClone.Mode == "cache",
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -4,26 +4,24 @@ import (
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
func AuthHeaderHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
|
||||
func AuthHeaderHandler(c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
|
||||
if !cfg.Auth.Enabled {
|
||||
return true, ""
|
||||
return true, nil
|
||||
}
|
||||
// 获取"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)
|
||||
authToken := string(c.GetHeader("GH-Auth"))
|
||||
logDebug("%s %s %s %s %s AUTH_TOKEN: %s", c.Request.Method, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), authToken)
|
||||
if authToken == "" {
|
||||
err := "Auth Header == nil"
|
||||
return false, err
|
||||
return false, fmt.Errorf("Auth token not found")
|
||||
}
|
||||
|
||||
isValid = authToken == cfg.Auth.AuthToken
|
||||
if !isValid {
|
||||
err := fmt.Sprintf("Auth token incorrect: %s", authToken)
|
||||
return false, err
|
||||
return false, fmt.Errorf("Auth token incorrect")
|
||||
}
|
||||
|
||||
return isValid, ""
|
||||
return isValid, nil
|
||||
}
|
||||
|
||||
@@ -4,27 +4,25 @@ import (
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
func AuthParametersHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
|
||||
func AuthParametersHandler(c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
|
||||
if !cfg.Auth.Enabled {
|
||||
return true, ""
|
||||
return true, nil
|
||||
}
|
||||
|
||||
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, string(c.Path()), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), authToken)
|
||||
|
||||
if authToken == "" {
|
||||
err := "Auth token == nil"
|
||||
return false, err
|
||||
return false, fmt.Errorf("Auth token not found")
|
||||
}
|
||||
|
||||
isValid = authToken == cfg.Auth.AuthToken
|
||||
if !isValid {
|
||||
err := fmt.Sprintf("Auth token incorrect: %s", authToken)
|
||||
return false, err
|
||||
return false, fmt.Errorf("Auth token invalid")
|
||||
}
|
||||
|
||||
return isValid, ""
|
||||
return isValid, nil
|
||||
}
|
||||
|
||||
24
auth/auth.go
24
auth/auth.go
@@ -1,15 +1,17 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
var (
|
||||
logw = logger.Logw
|
||||
LogDump = logger.LogDump
|
||||
logDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
@@ -18,15 +20,23 @@ var (
|
||||
|
||||
func Init(cfg *config.Config) {
|
||||
if cfg.Blacklist.Enabled {
|
||||
LoadBlacklist(cfg)
|
||||
err := InitBlacklist(cfg)
|
||||
if err != nil {
|
||||
logError(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if cfg.Whitelist.Enabled {
|
||||
LoadWhitelist(cfg)
|
||||
err := InitWhitelist(cfg)
|
||||
if err != nil {
|
||||
logError(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
logDebug("Auth Init")
|
||||
}
|
||||
|
||||
func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string) {
|
||||
func AuthHandler(ctx context.Context, c *app.RequestContext, cfg *config.Config) (isValid bool, err error) {
|
||||
if cfg.Auth.AuthMethod == "parameters" {
|
||||
isValid, err = AuthParametersHandler(c, cfg)
|
||||
return isValid, err
|
||||
@@ -35,9 +45,9 @@ func AuthHandler(c *gin.Context, cfg *config.Config) (isValid bool, err string)
|
||||
return isValid, err
|
||||
} else if cfg.Auth.AuthMethod == "" {
|
||||
logError("Auth method not set")
|
||||
return true, ""
|
||||
return true, nil
|
||||
} else {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,59 +2,89 @@ package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type BlacklistConfig struct {
|
||||
Blacklist []string `json:"blacklist"`
|
||||
type Blacklist struct {
|
||||
userSet map[string]struct{} // 用户级黑名单
|
||||
repoSet map[string]map[string]struct{} // 仓库级黑名单
|
||||
initOnce sync.Once // 确保初始化只执行一次
|
||||
initialized bool // 初始化状态标识
|
||||
}
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
blacklistfile = "/data/ghproxy/config/blacklist.json"
|
||||
blacklist *BlacklistConfig
|
||||
instance *Blacklist
|
||||
initErr error
|
||||
)
|
||||
|
||||
func LoadBlacklist(cfg *config.Config) {
|
||||
blacklistfile = cfg.Blacklist.BlacklistFile
|
||||
blacklist = &BlacklistConfig{}
|
||||
// InitBlacklist 初始化黑名单(线程安全,仅执行一次)
|
||||
func InitBlacklist(cfg *config.Config) error {
|
||||
instance = &Blacklist{
|
||||
userSet: make(map[string]struct{}),
|
||||
repoSet: make(map[string]map[string]struct{}),
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(blacklistfile)
|
||||
data, err := os.ReadFile(cfg.Blacklist.BlacklistFile)
|
||||
if err != nil {
|
||||
logError("Failed to read blacklist file: %v", err)
|
||||
return fmt.Errorf("failed to read blacklist: %w", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, blacklist)
|
||||
if err != nil {
|
||||
logError("Failed to unmarshal blacklist JSON: %v", err)
|
||||
var list struct {
|
||||
Entries []string `json:"blacklist"`
|
||||
}
|
||||
}
|
||||
|
||||
func CheckBlacklist(repouser string, user string, repo string) bool {
|
||||
return forRangeCheckBlacklist(blacklist.Blacklist, repouser, user)
|
||||
}
|
||||
|
||||
func sliceRepoName_Blacklist(fullrepo string) (string, string) {
|
||||
s := strings.Split(fullrepo, "/")
|
||||
if len(s) != 2 {
|
||||
return "", ""
|
||||
if err := json.Unmarshal(data, &list); err != nil {
|
||||
return fmt.Errorf("invalid blacklist format: %w", err)
|
||||
}
|
||||
return s[0], s[1]
|
||||
}
|
||||
|
||||
func forRangeCheckBlacklist(blist []string, fullrepo string, user string) bool {
|
||||
for _, blocked := range blist {
|
||||
users, _ := sliceRepoName_Blacklist(blocked)
|
||||
if user == users {
|
||||
if strings.HasSuffix(blocked, "/*") {
|
||||
return true
|
||||
}
|
||||
if fullrepo == blocked {
|
||||
return true
|
||||
for _, entry := range list.Entries {
|
||||
user, repo := splitUserRepo(entry)
|
||||
switch {
|
||||
case repo == "" || repo == "*":
|
||||
instance.userSet[user] = struct{}{}
|
||||
default:
|
||||
if _, exists := instance.repoSet[user]; !exists {
|
||||
instance.repoSet[user] = make(map[string]struct{})
|
||||
}
|
||||
instance.repoSet[user][repo] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
instance.initialized = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckBlacklist 检查用户和仓库是否在黑名单中(无锁设计)
|
||||
func CheckBlacklist(username, repo string) bool {
|
||||
if instance == nil || !instance.initialized {
|
||||
return false
|
||||
}
|
||||
|
||||
// 先检查用户级黑名单
|
||||
if _, exists := instance.userSet[username]; exists {
|
||||
return true
|
||||
}
|
||||
|
||||
// 再检查仓库级黑名单
|
||||
if repos, userExists := instance.repoSet[username]; userExists {
|
||||
// 允许仓库名为空时的全用户仓库匹配
|
||||
if repo == "" {
|
||||
return true
|
||||
}
|
||||
_, repoExists := repos[repo]
|
||||
return repoExists
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// splitUserRepo 优化分割逻辑(仅初始化时使用)
|
||||
func splitUserRepo(fullRepo string) (user, repo string) {
|
||||
if idx := strings.Index(fullRepo, "/"); idx > 0 {
|
||||
return fullRepo[:idx], fullRepo[idx+1:]
|
||||
}
|
||||
return fullRepo, ""
|
||||
}
|
||||
|
||||
@@ -2,58 +2,90 @@ package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WhitelistConfig struct {
|
||||
Whitelist []string `json:"whitelist"`
|
||||
// Whitelist 用于存储白名单信息
|
||||
type Whitelist struct {
|
||||
userSet map[string]struct{} // 用户级白名单
|
||||
repoSet map[string]map[string]struct{} // 仓库级白名单
|
||||
initOnce sync.Once // 确保初始化只执行一次
|
||||
initialized bool // 初始化状态标识
|
||||
}
|
||||
|
||||
var (
|
||||
whitelistfile = "/data/ghproxy/config/whitelist.json"
|
||||
whitelist *WhitelistConfig
|
||||
whitelistInstance *Whitelist
|
||||
whitelistInitErr error
|
||||
)
|
||||
|
||||
func LoadWhitelist(cfg *config.Config) {
|
||||
whitelistfile = cfg.Whitelist.WhitelistFile
|
||||
whitelist = &WhitelistConfig{}
|
||||
// InitWhitelist 初始化白名单(线程安全,仅执行一次)
|
||||
func InitWhitelist(cfg *config.Config) error {
|
||||
whitelistInstance = &Whitelist{
|
||||
userSet: make(map[string]struct{}),
|
||||
repoSet: make(map[string]map[string]struct{}),
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(whitelistfile)
|
||||
data, err := os.ReadFile(cfg.Whitelist.WhitelistFile)
|
||||
if err != nil {
|
||||
logError("Failed to read whitelist file: %v", err)
|
||||
return fmt.Errorf("failed to read whitelist: %w", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, whitelist)
|
||||
if err != nil {
|
||||
logError("Failed to unmarshal whitelist JSON: %v", err)
|
||||
var list struct {
|
||||
Entries []string `json:"whitelist"`
|
||||
}
|
||||
}
|
||||
|
||||
func CheckWhitelist(fullrepo string, user string, repo string) bool {
|
||||
return forRangeCheckWhitelist(whitelist.Whitelist, fullrepo, user)
|
||||
}
|
||||
|
||||
func sliceRepoName_Whitelist(fullrepo string) (string, string) {
|
||||
s := strings.Split(fullrepo, "/")
|
||||
if len(s) != 2 {
|
||||
return "", ""
|
||||
if err := json.Unmarshal(data, &list); err != nil {
|
||||
return fmt.Errorf("invalid whitelist format: %w", err)
|
||||
}
|
||||
return s[0], s[1]
|
||||
}
|
||||
|
||||
func forRangeCheckWhitelist(wlist []string, fullrepo string, user string) bool {
|
||||
for _, passd := range wlist {
|
||||
users, _ := sliceRepoName_Whitelist(passd)
|
||||
if users == user {
|
||||
if strings.HasSuffix(passd, "/*") {
|
||||
return true
|
||||
}
|
||||
if fullrepo == passd {
|
||||
return true
|
||||
for _, entry := range list.Entries {
|
||||
user, repo := splitUserRepoWhitelist(entry)
|
||||
switch {
|
||||
case repo == "" || repo == "*":
|
||||
whitelistInstance.userSet[user] = struct{}{}
|
||||
default:
|
||||
if _, exists := whitelistInstance.repoSet[user]; !exists {
|
||||
whitelistInstance.repoSet[user] = make(map[string]struct{})
|
||||
}
|
||||
whitelistInstance.repoSet[user][repo] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
whitelistInstance.initialized = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckWhitelist 检查用户和仓库是否在白名单中(无锁设计)
|
||||
func CheckWhitelist(username, repo string) bool {
|
||||
if whitelistInstance == nil || !whitelistInstance.initialized {
|
||||
return false
|
||||
}
|
||||
|
||||
// 先检查用户级白名单
|
||||
if _, exists := whitelistInstance.userSet[username]; exists {
|
||||
return true
|
||||
}
|
||||
|
||||
// 再检查仓库级白名单
|
||||
if repos, userExists := whitelistInstance.repoSet[username]; userExists {
|
||||
// 允许仓库名为空时的全用户仓库匹配
|
||||
if repo == "" {
|
||||
return true
|
||||
}
|
||||
_, repoExists := repos[repo]
|
||||
return repoExists
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// splitUserRepoWhitelist 分割用户和仓库信息(仅初始化时使用)
|
||||
func splitUserRepoWhitelist(fullRepo string) (user, repo string) {
|
||||
if idx := strings.Index(fullRepo, "/"); idx > 0 {
|
||||
return fullRepo[:idx], fullRepo[idx+1:]
|
||||
}
|
||||
return fullRepo, ""
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"blacklist": [
|
||||
"black/list",
|
||||
"test/test1",
|
||||
"example/*"
|
||||
"eviluser",
|
||||
"spamuser/bad-repo",
|
||||
"malwareuser/*"
|
||||
]
|
||||
}
|
||||
@@ -6,9 +6,11 @@ import (
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig
|
||||
Httpc HttpcConfig
|
||||
GitClone GitCloneConfig
|
||||
Shell ShellConfig
|
||||
Pages PagesConfig
|
||||
Log LogConfig
|
||||
CORS CORSConfig
|
||||
Auth AuthConfig
|
||||
Blacklist BlacklistConfig
|
||||
Whitelist WhitelistConfig
|
||||
@@ -16,16 +18,66 @@ type Config struct {
|
||||
Outbound OutboundConfig
|
||||
}
|
||||
|
||||
/*
|
||||
[server]
|
||||
host = "0.0.0.0" # 监听地址
|
||||
port = 8080 # 监听端口
|
||||
sizeLimit = 125 # 125MB
|
||||
H2C = true # 是否开启H2C传输
|
||||
*/
|
||||
|
||||
type ServerConfig struct {
|
||||
Port int `toml:"port"`
|
||||
Host string `toml:"host"`
|
||||
SizeLimit int `toml:"sizeLimit"`
|
||||
EnableH2C string `toml:"enableH2C"`
|
||||
H2C bool `toml:"H2C"`
|
||||
Cors string `toml:"cors"`
|
||||
Debug bool `toml:"debug"`
|
||||
}
|
||||
|
||||
/*
|
||||
[httpc]
|
||||
mode = "auto" # "auto" or "advanced"
|
||||
maxIdleConns = 100 # only for advanced mode
|
||||
maxIdleConnsPerHost = 60 # only for advanced mode
|
||||
maxConnsPerHost = 0 # only for advanced mode
|
||||
*/
|
||||
type HttpcConfig struct {
|
||||
Mode string `toml:"mode"`
|
||||
MaxIdleConns int `toml:"maxIdleConns"`
|
||||
MaxIdleConnsPerHost int `toml:"maxIdleConnsPerHost"`
|
||||
MaxConnsPerHost int `toml:"maxConnsPerHost"`
|
||||
}
|
||||
|
||||
/*
|
||||
[gitclone]
|
||||
mode = "bypass" # bypass / cache
|
||||
smartGitAddr = "http://127.0.0.1:8080"
|
||||
ForceH2C = true
|
||||
*/
|
||||
type GitCloneConfig struct {
|
||||
Mode string `toml:"mode"`
|
||||
SmartGitAddr string `toml:"smartGitAddr"`
|
||||
ForceH2C bool `toml:"ForceH2C"`
|
||||
}
|
||||
|
||||
/*
|
||||
[shell]
|
||||
editor = true
|
||||
*/
|
||||
type ShellConfig struct {
|
||||
Editor bool `toml:"editor"`
|
||||
}
|
||||
|
||||
/*
|
||||
[pages]
|
||||
mode = "internal" # "internal" or "external"
|
||||
theme = "bootstrap" # "bootstrap" or "nebula" or "design" or "classic"
|
||||
staticDir = "/data/www"
|
||||
*/
|
||||
type PagesConfig struct {
|
||||
Enabled bool `toml:"enabled"`
|
||||
Mode string `toml:"mode"`
|
||||
Theme string `toml:"theme"`
|
||||
StaticDir string `toml:"staticDir"`
|
||||
}
|
||||
|
||||
@@ -35,15 +87,20 @@ type LogConfig struct {
|
||||
Level string `toml:"level"`
|
||||
}
|
||||
|
||||
type CORSConfig struct {
|
||||
Enabled bool `toml:"enabled"`
|
||||
}
|
||||
|
||||
/*
|
||||
[auth]
|
||||
authMethod = "parameters" # "header" or "parameters"
|
||||
authToken = "token"
|
||||
enabled = false
|
||||
passThrough = false
|
||||
ForceAllowApi = true
|
||||
*/
|
||||
type AuthConfig struct {
|
||||
Enabled bool `toml:"enabled"`
|
||||
AuthMethod string `toml:"authMethod"`
|
||||
AuthToken string `toml:"authToken"`
|
||||
PassThrough bool `toml:"passThrough"`
|
||||
Enabled bool `toml:"enabled"`
|
||||
AuthMethod string `toml:"authMethod"`
|
||||
AuthToken string `toml:"authToken"`
|
||||
PassThrough bool `toml:"passThrough"`
|
||||
ForceAllowApi bool `toml:"ForceAllowApi"`
|
||||
}
|
||||
|
||||
type BlacklistConfig struct {
|
||||
|
||||
@@ -2,11 +2,27 @@
|
||||
host = "0.0.0.0"
|
||||
port = 8080
|
||||
sizeLimit = 125 # MB
|
||||
enableH2C = "on" # "on" or "off"
|
||||
H2C = true
|
||||
cors = "*" # "*"/"" -> "*" ; "nil" -> "" ;
|
||||
debug = false
|
||||
|
||||
[httpc]
|
||||
mode = "auto" # "auto" or "advanced"
|
||||
maxIdleConns = 100 # only for advanced mode
|
||||
maxIdleConnsPerHost = 60 # only for advanced mode
|
||||
maxConnsPerHost = 0 # only for advanced mode
|
||||
|
||||
[gitclone]
|
||||
mode = "bypass" # bypass / cache
|
||||
smartGitAddr = "http://127.0.0.1:8080"
|
||||
ForceH2C = false
|
||||
|
||||
[shell]
|
||||
editor = false
|
||||
|
||||
[pages]
|
||||
enabled = false
|
||||
mode = "internal" # "internal" or "external"
|
||||
theme = "bootstrap" # "bootstrap" or "nebula"
|
||||
staticDir = "/data/www"
|
||||
|
||||
[log]
|
||||
@@ -14,14 +30,12 @@ logFilePath = "/data/ghproxy/log/ghproxy.log"
|
||||
maxLogSize = 5 # MB
|
||||
level = "info" # dump, debug, info, warn, error, none
|
||||
|
||||
[cors]
|
||||
enabled = true
|
||||
|
||||
[auth]
|
||||
authMethod = "parameters" # "header" or "parameters"
|
||||
authToken = "token"
|
||||
enabled = false
|
||||
passThrough = false
|
||||
ForceAllowApi = false
|
||||
|
||||
[blacklist]
|
||||
blacklistFile = "/data/ghproxy/config/blacklist.json"
|
||||
|
||||
@@ -2,11 +2,27 @@
|
||||
host = "127.0.0.1"
|
||||
port = 8080
|
||||
sizeLimit = 125 # MB
|
||||
enableH2C = "on"
|
||||
H2C = true
|
||||
cors = "*" # "*"/"" -> "*" ; "nil" -> "" ;
|
||||
debug = false
|
||||
|
||||
[httpc]
|
||||
mode = "auto" # "auto" or "advanced"
|
||||
maxIdleConns = 100 # only for advanced mode
|
||||
maxIdleConnsPerHost = 60 # only for advanced mode
|
||||
maxConnsPerHost = 0 # only for advanced mode
|
||||
|
||||
[gitclone]
|
||||
mode = "bypass" # bypass / cache
|
||||
smartGitAddr = "http://127.0.0.1:8080"
|
||||
ForceH2C = false
|
||||
|
||||
[shell]
|
||||
editor = false
|
||||
|
||||
[pages]
|
||||
enabled = false
|
||||
mode = "internal" # "internal" or "external"
|
||||
theme = "bootstrap" # "bootstrap" or "nebula"
|
||||
staticDir = "/usr/local/ghproxy/pages"
|
||||
|
||||
[log]
|
||||
@@ -14,14 +30,12 @@ logFilePath = "/usr/local/ghproxy/log/ghproxy.log"
|
||||
maxLogSize = 5 # MB
|
||||
level = "info" # dump, debug, info, warn, error, none
|
||||
|
||||
[cors]
|
||||
enabled = true
|
||||
|
||||
[auth]
|
||||
authMethod = "parameters" # "header" or "parameters"
|
||||
authToken = "token"
|
||||
enabled = false
|
||||
passThrough = false
|
||||
ForceAllowApi = false
|
||||
|
||||
[blacklist]
|
||||
blacklistFile = "/usr/local/ghproxy/config/blacklist.json"
|
||||
|
||||
@@ -90,8 +90,8 @@ tar -zxvf ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz -C ${ghproxy_dir}
|
||||
chmod +x ${ghproxy_dir}/ghproxy
|
||||
|
||||
# 下载pages
|
||||
wget -q -O ${ghproxy_dir}/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/pages/index.html
|
||||
wget -q -O ${ghproxy_dir}/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/pages/favicon.ico
|
||||
wget -q -O ${ghproxy_dir}/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/pages/bootstrap/index.html
|
||||
wget -q -O ${ghproxy_dir}/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/dev/pages/bootstrap/favicon.ico
|
||||
|
||||
|
||||
# 下载配置文件
|
||||
|
||||
@@ -90,8 +90,8 @@ tar -zxvf ${ghproxy_dir}/ghproxy-linux-$ARCH.tar.gz -C ${ghproxy_dir}
|
||||
chmod +x ${ghproxy_dir}/ghproxy
|
||||
|
||||
# 下载pages
|
||||
wget -q -O ${ghproxy_dir}/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/index.html
|
||||
wget -q -O ${ghproxy_dir}/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/favicon.ico
|
||||
wget -q -O ${ghproxy_dir}/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/bootstrap/index.html
|
||||
wget -q -O ${ghproxy_dir}/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/bootstrap/favicon.ico
|
||||
|
||||
|
||||
# 下载配置文件
|
||||
|
||||
@@ -15,10 +15,6 @@ RUN mkdir -p /data/${APPLICATION}/log
|
||||
# 安装依赖
|
||||
RUN apk add --no-cache curl wget tar
|
||||
|
||||
# 前端
|
||||
RUN wget -O /data/www/index.html https://raw.githubusercontent.com/${USER}/${REPO}/dev/pages/index.html
|
||||
RUN wget -O /data/www/favicon.ico https://raw.githubusercontent.com/${USER}/${REPO}/dev/pages/favicon.ico
|
||||
|
||||
# 后端
|
||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/dev/DEV-VERSION) && \
|
||||
wget -O /data/${APPLICATION}/${APPLICATION}-${TARGETOS}-${TARGETARCH}.tar.gz https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}-${TARGETOS}-${TARGETARCH}.tar.gz && \
|
||||
|
||||
@@ -15,10 +15,6 @@ RUN mkdir -p /data/${APPLICATION}/log
|
||||
# 安装依赖
|
||||
RUN apk add --no-cache curl wget tar
|
||||
|
||||
# 前端
|
||||
RUN wget -O /data/www/index.html https://raw.githubusercontent.com/${USER}/${REPO}/main/pages/index.html
|
||||
RUN wget -O /data/www/favicon.ico https://raw.githubusercontent.com/${USER}/${REPO}/main/pages/favicon.ico
|
||||
|
||||
# 后端
|
||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/VERSION) && \
|
||||
wget -O /data/${APPLICATION}/${APPLICATION}-${TARGETOS}-${TARGETARCH}.tar.gz https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}-${TARGETOS}-${TARGETARCH}.tar.gz && \
|
||||
|
||||
56
go.mod
56
go.mod
@@ -1,39 +1,39 @@
|
||||
module ghproxy
|
||||
|
||||
go 1.24.0
|
||||
go 1.24.1
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.4.0
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
golang.org/x/net v0.35.0
|
||||
golang.org/x/time v0.10.0
|
||||
github.com/BurntSushi/toml v1.5.0
|
||||
github.com/WJQSERVER-STUDIO/go-utils/hwriter v0.0.2
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.5.0
|
||||
github.com/cloudwego/hertz v0.9.6
|
||||
github.com/hertz-contrib/http2 v0.1.8
|
||||
github.com/satomitouka/touka-httpc v0.3.3
|
||||
github.com/valyala/bytebufferpool v1.0.0
|
||||
golang.org/x/net v0.37.0
|
||||
golang.org/x/time v0.11.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.12.8 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.3 // indirect
|
||||
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 // indirect
|
||||
github.com/WJQSERVER-STUDIO/go-utils/log v0.0.1 // indirect
|
||||
github.com/bytedance/gopkg v0.1.1 // indirect
|
||||
github.com/bytedance/sonic v1.13.1 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.0.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.24.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||
github.com/cloudwego/netpoll v0.6.5 // indirect
|
||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||
github.com/google/go-cmp v0.7.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/nyaruka/phonenumbers v1.5.0 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
github.com/tidwall/gjson v1.18.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
golang.org/x/arch v0.14.0 // indirect
|
||||
golang.org/x/crypto v0.33.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/text v0.22.0 // indirect
|
||||
golang.org/x/arch v0.15.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
|
||||
golang.org/x/sys v0.31.0 // indirect
|
||||
golang.org/x/text v0.23.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
134
go.sum
134
go.sum
@@ -1,90 +1,100 @@
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0 h1:rOvutC4zYfvtSGN2CNZrycjtq8dLpfu7ypy7tTEErPY=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.3.0/go.mod h1:oW884JCCPDU6c906LI0uKXndWLiRvjb9LkGYC2cqRO8=
|
||||
github.com/bytedance/sonic v1.12.8 h1:4xYRVRlXIgvSZ4e8iVTlMF5szgpXd4AfvuWgA8I8lgs=
|
||||
github.com/bytedance/sonic v1.12.8/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
|
||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 h1:JLtFd00AdFg/TP+dtvIzLkdHwKUGPOAijN1sMtEYoFg=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/hwriter v0.0.2 h1:z9xSC3qkt8Qjjb+KRV0Az5klUBJ/gE3berBbjVSFVzY=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/hwriter v0.0.2/go.mod h1:U3dVP2MzKJfK6dPiobxmSdynibqCOn1mxQEVLylESWA=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/log v0.0.1 h1:gJEQspQPB527Vp2FPcdOrynQEj3YYtrg1ixVSB/JvZM=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/log v0.0.1/go.mod h1:j9Q+xnwpOfve7/uJnZ2izRQw6NNoXjvJHz7vUQAaLZE=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.5.0 h1:Uk4N7Sh4OPth3am3xVv17JlAm7tsna97ZLQRpQj7r5c=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.5.0/go.mod h1:mtxlnDdwsHcqDDpAQLa94nxbPFwNHSAHbBbIXQAA3po=
|
||||
github.com/bytedance/gopkg v0.1.0/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ=
|
||||
github.com/bytedance/gopkg v0.1.1 h1:3azzgSkiaw79u24a+w9arfH8OfnQQ4MHUt9lJFREEaE=
|
||||
github.com/bytedance/gopkg v0.1.1/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/mockey v1.2.12 h1:aeszOmGw8CPX8CRx1DZ/Glzb1yXvhjDh6jdFBNZjsU4=
|
||||
github.com/bytedance/mockey v1.2.12/go.mod h1:3ZA4MQasmqC87Tw0w7Ygdy7eHIc2xgpZ8Pona5rsYIk=
|
||||
github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g=
|
||||
github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0=
|
||||
github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
|
||||
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/hertz v0.9.6 h1:Kj5SSPlKBC32NIN7+B/tt8O1pdDz8brMai00rqqjULQ=
|
||||
github.com/cloudwego/hertz v0.9.6/go.mod h1:X5Ez52XhtszU4t+CTBGIJI4PqmcI1oSf8ULBz0SWfLo=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/cloudwego/netpoll v0.6.5 h1:6E/BWhSzQoyLg9Kx/4xiMdIIpovzwBtXvuqSqaTUzDQ=
|
||||
github.com/cloudwego/netpoll v0.6.5/go.mod h1:BtM+GjKTdwKoC8IOzD08/+8eEn2gYoiNLipFca6BVXQ=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
|
||||
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
|
||||
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
|
||||
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/hertz-contrib/http2 v0.1.8 h1:kjfCGkUxJZHgfPsnRjx1FLJBG55KvtvSQD214guBQLw=
|
||||
github.com/hertz-contrib/http2 v0.1.8/go.mod h1:m42hrl8fiTwE4p8c7JdRUZpkePEthvV89q3elL2GeD0=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/nyaruka/phonenumbers v1.5.0 h1:0M+Gd9zl53QC4Nl5z1Yj1O/zPk2XXBUwR/vlzdXSJv4=
|
||||
github.com/nyaruka/phonenumbers v1.5.0/go.mod h1:gv+CtldaFz+G3vHHnasBSirAi3O2XLqZzVWz4V1pl2E=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/satomitouka/touka-httpc v0.3.3 h1:Th0uJ5do3oqqZgdUDtqD1SH11x8TcJmrwHMJQlEIKCg=
|
||||
github.com/satomitouka/touka-httpc v0.3.3/go.mod h1:sNXyW5XBufkwB9ZJ+PIlgN/6xiJ7aZV1fWGrXR0u3bA=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
|
||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4=
|
||||
golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||
golang.org/x/time v0.10.0 h1:3usCWA8tQn0L8+hFJQNgzpWbd89begxN66o1Ojdn5L4=
|
||||
golang.org/x/time v0.10.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw=
|
||||
golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
|
||||
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
|
||||
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package loggin
|
||||
|
||||
import (
|
||||
"ghproxy/timing"
|
||||
"time"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
logw = logger.Logw
|
||||
LogDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
logError = logger.LogError
|
||||
)
|
||||
|
||||
// 日志中间件
|
||||
func Middleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
var timingResults time.Duration
|
||||
|
||||
// 获取计时结果
|
||||
timingResults, _ = timing.Get(c)
|
||||
|
||||
// 记录日志 IP METHOD URL USERAGENT PROTOCOL STATUS TIMING
|
||||
logInfo("%s %s %s %s %d %s ", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Writer.Status(), timingResults)
|
||||
}
|
||||
}
|
||||
277
main.go
277
main.go
@@ -1,10 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -12,23 +12,26 @@ import (
|
||||
"ghproxy/api"
|
||||
"ghproxy/auth"
|
||||
"ghproxy/config"
|
||||
"ghproxy/loggin"
|
||||
"ghproxy/middleware/loggin"
|
||||
"ghproxy/proxy"
|
||||
"ghproxy/rate"
|
||||
"ghproxy/timing"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
"github.com/cloudwego/hertz/pkg/common/adaptor"
|
||||
|
||||
"github.com/hertz-contrib/http2/factory"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
router *gin.Engine
|
||||
r *server.Hertz
|
||||
configfile = "/data/ghproxy/config/config.toml"
|
||||
cfgfile string
|
||||
version string
|
||||
dev string
|
||||
runMode string
|
||||
limiter *rate.RateLimiter
|
||||
iplimiter *rate.IPRateLimiter
|
||||
@@ -37,11 +40,19 @@ var (
|
||||
var (
|
||||
//go:embed pages/*
|
||||
pagesFS embed.FS
|
||||
/*
|
||||
//go:embed pages/bootstrap/*
|
||||
BootstrapPagesFS embed.FS
|
||||
//go:embed pages/nebula/*
|
||||
NebulaPagesFS embed.FS
|
||||
//go:embed pages/design/*
|
||||
DesignPagesFS embed.FS
|
||||
*/
|
||||
)
|
||||
|
||||
var (
|
||||
logw = logger.Logw
|
||||
LogDump = logger.LogDump
|
||||
logDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
@@ -84,8 +95,8 @@ func loadlist(cfg *config.Config) {
|
||||
auth.Init(cfg)
|
||||
}
|
||||
|
||||
func setupApi(cfg *config.Config, router *gin.Engine, version string) {
|
||||
api.InitHandleRouter(cfg, router, version)
|
||||
func setupApi(cfg *config.Config, r *server.Hertz, version string) {
|
||||
api.InitHandleRouter(cfg, r, version)
|
||||
}
|
||||
|
||||
func setupRateLimit(cfg *config.Config) {
|
||||
@@ -104,6 +115,148 @@ func InitReq(cfg *config.Config) {
|
||||
proxy.InitReq(cfg)
|
||||
}
|
||||
|
||||
// loadEmbeddedPages 加载嵌入式页面资源
|
||||
func loadEmbeddedPages(cfg *config.Config) (fs.FS, error) {
|
||||
var pages fs.FS
|
||||
var err error
|
||||
switch cfg.Pages.Theme {
|
||||
case "bootstrap":
|
||||
pages, err = fs.Sub(pagesFS, "pages/bootstrap")
|
||||
case "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:
|
||||
pages, err = fs.Sub(pagesFS, "pages/bootstrap") // 默认主题
|
||||
logWarning("Invalid Pages Theme: %s, using default theme 'bootstrap'", cfg.Pages.Theme)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load embedded pages: %w", err)
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
// setupPages 设置页面路由
|
||||
func setupPages(cfg *config.Config, r *server.Hertz) {
|
||||
switch cfg.Pages.Mode {
|
||||
case "internal":
|
||||
// 加载嵌入式资源
|
||||
pages, err := loadEmbeddedPages(cfg)
|
||||
if err != nil {
|
||||
logError("Failed when processing internal pages: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置嵌入式资源路由
|
||||
r.GET("/", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
r.GET("/favicon.ico", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
r.GET("/script.js", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
r.GET("/style.css", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
|
||||
case "external":
|
||||
// 设置外部资源路径
|
||||
indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir)
|
||||
faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir)
|
||||
javascriptsPath := fmt.Sprintf("%s/script.js", cfg.Pages.StaticDir)
|
||||
stylesheetsPath := fmt.Sprintf("%s/style.css", cfg.Pages.StaticDir)
|
||||
//bootstrapPath := fmt.Sprintf("%s/bootstrap.min.css", cfg.Pages.StaticDir)
|
||||
|
||||
// 设置外部资源路由
|
||||
r.StaticFile("/", indexPagePath)
|
||||
r.StaticFile("/favicon.ico", faviconPath)
|
||||
r.StaticFile("/script.js", javascriptsPath)
|
||||
r.StaticFile("/style.css", stylesheetsPath)
|
||||
//router.StaticFile("/bootstrap.min.css", bootstrapPath)
|
||||
|
||||
default:
|
||||
// 处理无效的Pages Mode
|
||||
logWarning("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode)
|
||||
|
||||
// 加载嵌入式资源
|
||||
pages, err := loadEmbeddedPages(cfg)
|
||||
if err != nil {
|
||||
logError("Failed when processing pages: %s", err)
|
||||
return
|
||||
}
|
||||
// 设置嵌入式资源路由
|
||||
r.GET("/", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
r.GET("/favicon.ico", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
r.GET("/script.js", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
r.GET("/style.css", func(ctx context.Context, c *app.RequestContext) {
|
||||
staticServer := http.FileServer(http.FS(pages))
|
||||
req, err := adaptor.GetCompatRequest(&c.Request)
|
||||
if err != nil {
|
||||
logError("%s", err)
|
||||
return
|
||||
}
|
||||
staticServer.ServeHTTP(adaptor.GetCompatResponseWriter(&c.Response), req)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
readFlag()
|
||||
flag.Parse()
|
||||
@@ -114,73 +267,87 @@ func init() {
|
||||
setupRateLimit(cfg)
|
||||
|
||||
if cfg.Server.Debug {
|
||||
dev = "true"
|
||||
version = "dev"
|
||||
}
|
||||
if dev == "true" {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
runMode = "dev"
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
runMode = "release"
|
||||
}
|
||||
|
||||
if cfg.Server.Debug {
|
||||
version = "Dev"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
logDebug("Run Mode: %s", runMode)
|
||||
|
||||
gin.LoggerWithWriter(io.Discard)
|
||||
router = gin.New()
|
||||
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||
|
||||
// 添加recovery中间件
|
||||
router.Use(gin.Recovery())
|
||||
r := server.New(
|
||||
server.WithHostPorts(addr),
|
||||
server.WithH2C(true),
|
||||
)
|
||||
|
||||
r.AddProtocol("h2", factory.NewServerFactory())
|
||||
|
||||
// 添加Recovery中间件
|
||||
r.Use(recovery.Recovery())
|
||||
// 添加log中间件
|
||||
router.Use(loggin.Middleware())
|
||||
r.Use(loggin.Middleware())
|
||||
|
||||
// 添加计时中间件
|
||||
router.Use(timing.Middleware())
|
||||
setupApi(cfg, r, version)
|
||||
|
||||
//H2C默认值为true,而后遵循cfg.Server.EnableH2C的设置
|
||||
if cfg.Server.EnableH2C == "on" {
|
||||
router.UseH2C = true
|
||||
} else if cfg.Server.EnableH2C == "" {
|
||||
router.UseH2C = true
|
||||
} else {
|
||||
router.UseH2C = false
|
||||
}
|
||||
setupPages(cfg, r)
|
||||
|
||||
setupApi(cfg, router, version)
|
||||
// 1. GitHub Releases/Archive - Use distinct path segments for type
|
||||
r.GET("/github.com/:username/:repo/releases/*filepath", func(ctx context.Context, c *app.RequestContext) { // Distinct path for releases
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
if cfg.Pages.Enabled {
|
||||
indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir)
|
||||
faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir)
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.File(indexPagePath)
|
||||
logInfo("IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
|
||||
})
|
||||
router.StaticFile("/favicon.ico", faviconPath)
|
||||
} else if !cfg.Pages.Enabled {
|
||||
pages, err := fs.Sub(pagesFS, "pages")
|
||||
if err != nil {
|
||||
logError("Failed when processing pages: %s", err)
|
||||
}
|
||||
router.GET("/", gin.WrapH(http.FileServer(http.FS(pages))))
|
||||
router.GET("/favicon.ico", gin.WrapH(http.FileServer(http.FS(pages))))
|
||||
}
|
||||
r.GET("/github.com/:username/:repo/archive/*filepath", func(ctx context.Context, c *app.RequestContext) { // Distinct path for archive
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter, runMode)(c)
|
||||
// 2. GitHub Blob/Raw - Use distinct path segments for type
|
||||
r.GET("/github.com/:username/:repo/blob/*filepath", func(ctx context.Context, c *app.RequestContext) { // Distinct path for blob
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
r.GET("/github.com/:username/:repo/raw/*filepath", func(ctx context.Context, c *app.RequestContext) { // Distinct path for raw
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
r.GET("/github.com/:username/:repo/info/*filepath", func(ctx context.Context, c *app.RequestContext) { // Distinct path for info
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
r.GET("/github.com/:username/:repo/git-upload-pack", func(ctx context.Context, c *app.RequestContext) {
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
// 4. Raw GitHubusercontent - Keep as is (assuming it's distinct enough)
|
||||
r.GET("/raw.githubusercontent.com/:username/:repo/*filepath", func(ctx context.Context, c *app.RequestContext) {
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
// 5. Gist GitHubusercontent - Keep as is (assuming it's distinct enough)
|
||||
r.GET("/gist.githubusercontent.com/:username/*filepath", func(ctx context.Context, c *app.RequestContext) {
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
// 6. GitHub API Repos - Keep as is (assuming it's distinct enough)
|
||||
r.GET("/api.github.com/repos/:username/:repo/*filepath", func(ctx context.Context, c *app.RequestContext) {
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
r.NoRoute(func(ctx context.Context, c *app.RequestContext) {
|
||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(ctx, c)
|
||||
})
|
||||
|
||||
fmt.Printf("GHProxy Version: %s\n", version)
|
||||
fmt.Printf("A Go Based High-Performance Github Proxy \n")
|
||||
fmt.Printf("Made by WJQSERVER-STUDIO\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := router.Run(fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port))
|
||||
if err != nil {
|
||||
logError("Failed to start server: %v\n", err)
|
||||
}
|
||||
r.Spin()
|
||||
defer logger.Close()
|
||||
fmt.Println("Program Exit")
|
||||
}
|
||||
|
||||
37
middleware/loggin/loggin.go
Normal file
37
middleware/loggin/loggin.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package loggin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
var (
|
||||
logw = logger.Logw
|
||||
logDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
logError = logger.LogError
|
||||
)
|
||||
|
||||
// 日志中间件
|
||||
func Middleware() app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
startTime := time.Now() // 请求开始处理前记录当前时间作为开始时间
|
||||
|
||||
c.Next(ctx) // 调用 Next() 执行后续的 Handler
|
||||
|
||||
endTime := time.Now() // 请求处理完成后记录当前时间作为结束时间
|
||||
timingResults := endTime.Sub(startTime) // 计算时间差,得到请求处理耗时 (Duration 类型)
|
||||
|
||||
// 记录日志 IP METHOD URL USERAGENT PROTOCOL STATUS TIMING
|
||||
// %s %s %s %s %s %d %s 分别对应: ClientIP, Method, Protolcol, Path, UserAgent, StatusCode, timingResults (需要格式化)
|
||||
// %v 可以通用地格式化 time.Duration 类型
|
||||
logInfo("%s %s %s %s %s %d %v ", c.ClientIP(), c.Method(), c.Request.Header.GetProtocol(), string(c.Path()), c.Request.Header.UserAgent(), c.Response.StatusCode(), timingResults)
|
||||
|
||||
//logInfo("%s %s %s %s %d %v ", c.ClientIP(), c.Method(), c.Path(), c.Request.Header.UserAgent(), c.Response.StatusCode(), timingResults)
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 3.2 KiB |
440
pages/index.html
440
pages/index.html
@@ -1,440 +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 href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<style>
|
||||
/* 通用样式 */
|
||||
: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);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</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://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
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);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -4,22 +4,22 @@ import (
|
||||
"ghproxy/config"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
func AuthPassThrough(c *gin.Context, cfg *config.Config, req *http.Request) {
|
||||
func AuthPassThrough(c *app.RequestContext, cfg *config.Config, req *http.Request) {
|
||||
if cfg.Auth.PassThrough {
|
||||
token := c.Query("token")
|
||||
if token != "" {
|
||||
logDebug("%s %s %s %s %s Auth-PassThrough: token %s", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, token)
|
||||
logDebug("%s %s %s %s %s Auth-PassThrough: token %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.UserAgent(), c.Request.Header.GetProtocol(), token)
|
||||
switch cfg.Auth.AuthMethod {
|
||||
case "parameters":
|
||||
if !cfg.Auth.Enabled {
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
} else {
|
||||
logWarning("%s %s %s %s %s Auth-Error: Conflict Auth Method", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning("%s %s %s %s %s Auth-Error: Conflict Auth Method", c.ClientIP(), c.Request.Method, string(c.Path()), c.UserAgent(), c.Request.Header.GetProtocol())
|
||||
// 500 Internal Server Error
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Conflict Auth Method"})
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": "Conflict Auth Method"})
|
||||
return
|
||||
}
|
||||
case "header":
|
||||
@@ -27,9 +27,9 @@ func AuthPassThrough(c *gin.Context, cfg *config.Config, req *http.Request) {
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
}
|
||||
default:
|
||||
logWarning("%s %s %s %s %s Invalid Auth Method / Auth Method is not be set", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning("%s %s %s %s %s Invalid Auth Method / Auth Method is not be set", c.ClientIP(), c.Request.Method, string(c.Path()), c.UserAgent(), c.Request.Header.GetProtocol())
|
||||
// 500 Internal Server Error
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid Auth Method / Auth Method is not be set"})
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": "Invalid Auth Method / Auth Method is not be set"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,73 +2,33 @@ package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/hwriter"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
||||
)
|
||||
|
||||
var BufferSize int = 32 * 1024 // 32KB
|
||||
|
||||
var (
|
||||
cclient *http.Client
|
||||
ctr *http.Transport
|
||||
BufferPool *sync.Pool
|
||||
)
|
||||
|
||||
func InitReq(cfg *config.Config) {
|
||||
initChunkedHTTPClient(cfg)
|
||||
initGitHTTPClient(cfg)
|
||||
|
||||
// 初始化固定大小的缓存池
|
||||
BufferPool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, BufferSize)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func initChunkedHTTPClient(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,
|
||||
}
|
||||
if cfg.Outbound.Enabled {
|
||||
initTransport(cfg, ctr)
|
||||
}
|
||||
cclient = &http.Client{
|
||||
Transport: ctr,
|
||||
}
|
||||
}
|
||||
|
||||
func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
|
||||
func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, matcher string) {
|
||||
method := c.Request.Method
|
||||
|
||||
// 发送HEAD请求, 预获取Content-Length
|
||||
headReq, err := http.NewRequest("HEAD", u, nil)
|
||||
headReq, err := client.NewRequest("HEAD", u, nil)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
||||
return
|
||||
}
|
||||
setRequestHeaders(c, headReq)
|
||||
removeWSHeader(headReq) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
||||
reWriteEncodeHeader(headReq)
|
||||
AuthPassThrough(c, cfg, headReq)
|
||||
|
||||
headResp, err := cclient.Do(headReq)
|
||||
headResp, err := client.Do(headReq)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
@@ -86,59 +46,46 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := headResp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
c.Redirect(http.StatusMovedPermanently, []byte(finalURL))
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Path(), c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), finalURL, size)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
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
|
||||
}
|
||||
body := c.Request.Body()
|
||||
|
||||
bodyReader := bytes.NewBuffer(body)
|
||||
|
||||
// 创建请求
|
||||
req, err := http.NewRequest(method, u, bodyReader)
|
||||
req, err := client.NewRequest(string(method()), u, bodyReader)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
setRequestHeaders(c, req)
|
||||
removeWSHeader(req) // 删除Conection Upgrade头, 避免与HTTP/2冲突(检查是否存在Upgrade头)
|
||||
reWriteEncodeHeader(req)
|
||||
AuthPassThrough(c, cfg, req)
|
||||
|
||||
resp, err := cclient.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
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
|
||||
}
|
||||
*/
|
||||
// 错误处理(404)
|
||||
if resp.StatusCode == 404 {
|
||||
c.String(http.StatusNotFound, "File Not Found")
|
||||
return
|
||||
}
|
||||
|
||||
contentLength = resp.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
c.Redirect(http.StatusMovedPermanently, []byte(finalURL))
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Path(), c.UserAgent(), c.Request.Header.GetProtocol(), finalURL, size)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -159,23 +106,45 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, mode stri
|
||||
resp.Header.Del(header)
|
||||
}
|
||||
|
||||
if cfg.CORS.Enabled {
|
||||
switch cfg.Server.Cors {
|
||||
case "*":
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
} else {
|
||||
case "":
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
case "nil":
|
||||
c.Header("Access-Control-Allow-Origin", "")
|
||||
default:
|
||||
c.Header("Access-Control-Allow-Origin", cfg.Server.Cors)
|
||||
}
|
||||
|
||||
c.Status(resp.StatusCode)
|
||||
c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
||||
|
||||
// 使用固定32KB缓冲池
|
||||
buffer := BufferPool.Get().([]byte)
|
||||
defer BufferPool.Put(buffer)
|
||||
if MatcherShell(u) && matchString(matcher, matchedMatchers) && cfg.Shell.Editor {
|
||||
// 判断body是不是gzip
|
||||
var compress string
|
||||
if resp.Header.Get("Content-Encoding") == "gzip" {
|
||||
compress = "gzip"
|
||||
}
|
||||
|
||||
_, 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
|
||||
logInfo("Is Shell: %s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol())
|
||||
c.Header("Content-Length", "")
|
||||
|
||||
err := ProcessLinksAndWriteChunked(resp.Body, compress, string(c.Request.Host()), cfg, c)
|
||||
|
||||
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.Header.GetProtocol(), err)
|
||||
return
|
||||
} else {
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
} else {
|
||||
c.Writer.Flush() // 确保刷入
|
||||
err = hwriter.Writer(resp.Body, c)
|
||||
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.Header.GetProtocol(), err)
|
||||
return
|
||||
} else {
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
proxy/error.go
Normal file
24
proxy/error.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
// 日志模块
|
||||
var (
|
||||
logw = logger.Logw
|
||||
logDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
logError = logger.LogError
|
||||
)
|
||||
|
||||
func HandleError(c *app.RequestContext, message string) {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||
logError(message)
|
||||
}
|
||||
153
proxy/gitreq.go
153
proxy/gitreq.go
@@ -2,97 +2,70 @@ package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/hwriter"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
var (
|
||||
gclient *http.Client
|
||||
gtr *http.Transport
|
||||
)
|
||||
func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, mode string) {
|
||||
method := string(c.Request.Method())
|
||||
|
||||
func initGitHTTPClient(cfg *config.Config) {
|
||||
gtr = &http.Transport{
|
||||
MaxIdleConns: 30,
|
||||
MaxConnsPerHost: 30,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
}
|
||||
if cfg.Outbound.Enabled {
|
||||
initTransport(cfg, gtr)
|
||||
}
|
||||
gclient = &http.Client{
|
||||
Transport: gtr,
|
||||
}
|
||||
}
|
||||
|
||||
func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
|
||||
method := c.Request.Method
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
|
||||
// 创建HTTP客户端
|
||||
//client := &http.Client{}
|
||||
|
||||
// 发送HEAD请求, 预获取Content-Length
|
||||
headReq, err := http.NewRequest("HEAD", u, nil)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
||||
return
|
||||
}
|
||||
setRequestHeaders(c, headReq)
|
||||
AuthPassThrough(c, cfg, headReq)
|
||||
|
||||
headResp, err := gclient.Do(headReq)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// defer headResp.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
if err := Body.Close(); err != nil {
|
||||
logError("Failed to close response body: %v", err)
|
||||
}
|
||||
}(headResp.Body)
|
||||
|
||||
contentLength := headResp.Header.Get("Content-Length")
|
||||
sizelimit := cfg.Server.SizeLimit * 1024 * 1024
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := headResp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
logDump("Url Before FMT:%s", u)
|
||||
if cfg.GitClone.Mode == "cache" {
|
||||
userPath, repoPath, remainingPath, queryParams, err := extractParts(u)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to extract parts from URL: %v", err))
|
||||
return
|
||||
}
|
||||
// 构建新url
|
||||
u = cfg.GitClone.SmartGitAddr + userPath + repoPath + remainingPath + "?" + queryParams.Encode()
|
||||
logDump("New Url After FMT:%s", u)
|
||||
}
|
||||
|
||||
body, err := readRequestBody(c)
|
||||
if err != nil {
|
||||
HandleError(c, err.Error())
|
||||
return
|
||||
}
|
||||
var (
|
||||
resp *http.Response
|
||||
err error
|
||||
)
|
||||
|
||||
body := c.Request.Body()
|
||||
|
||||
bodyReader := bytes.NewBuffer(body)
|
||||
|
||||
// 创建请求
|
||||
req, err := http.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 := gclient.Do(req)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
if cfg.GitClone.Mode == "cache" {
|
||||
req, err := gitclient.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 = 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)
|
||||
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 func(Body io.ReadCloser) {
|
||||
@@ -101,19 +74,14 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
/*
|
||||
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
|
||||
}
|
||||
*/
|
||||
contentLength = resp.Header.Get("Content-Length")
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
sizelimit := cfg.Server.SizeLimit * 1024 * 1024
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
finalURL := []byte(resp.Request.URL.String())
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Path(), c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), finalURL, size)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -134,15 +102,26 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
||||
resp.Header.Del(header)
|
||||
}
|
||||
|
||||
if cfg.CORS.Enabled {
|
||||
switch cfg.Server.Cors {
|
||||
case "*":
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
} else {
|
||||
case "":
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
case "nil":
|
||||
c.Header("Access-Control-Allow-Origin", "")
|
||||
default:
|
||||
c.Header("Access-Control-Allow-Origin", cfg.Server.Cors)
|
||||
}
|
||||
|
||||
c.Status(resp.StatusCode)
|
||||
err = hwriter.Writer(resp.Body, c)
|
||||
|
||||
if _, err := io.Copy(c.Writer, 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)
|
||||
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.Header.GetProtocol(), err)
|
||||
return
|
||||
} else {
|
||||
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"ghproxy/auth"
|
||||
"ghproxy/config"
|
||||
@@ -9,11 +11,13 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter, runMode string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var re = regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
|
||||
|
||||
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter) app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
|
||||
// 限制访问频率
|
||||
if cfg.RateLimit.Enabled {
|
||||
@@ -31,19 +35,19 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
||||
}
|
||||
|
||||
if !allowed {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{"error": "Too Many Requests"})
|
||||
logWarning("%s %s %s %s %s 429-TooManyRequests", c.ClientIP(), c.Request.Method, c.Request.URL.RequestURI(), c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
c.JSON(http.StatusTooManyRequests, map[string]string{"error": "Too Many Requests"})
|
||||
logWarning("%s %s %s %s %s 429-TooManyRequests", c.ClientIP(), c.Request.Method, c.Request.RequestURI(), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/") // 去掉前缀/
|
||||
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
|
||||
matches := re.FindStringSubmatch(rawPath) // 匹配路径
|
||||
rawPath := strings.TrimPrefix(string(c.Request.RequestURI()), "/") // 去掉前缀/
|
||||
matches := re.FindStringSubmatch(rawPath) // 匹配路径
|
||||
logInfo("Matches: %v", matches)
|
||||
|
||||
// 匹配路径错误处理
|
||||
if len(matches) < 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)
|
||||
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
logWarning(errMsg)
|
||||
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
||||
return
|
||||
@@ -52,76 +56,73 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
||||
// 制作url
|
||||
rawPath = "https://" + matches[2]
|
||||
|
||||
username, repo := MatchUserRepo(rawPath, cfg, c, matches) // 匹配用户名和仓库名
|
||||
user, repo, matcher, err := Matcher(rawPath, cfg)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrInvalidURL) {
|
||||
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
||||
logWarning(err.Error())
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ErrAuthHeaderUnavailable) {
|
||||
c.String(http.StatusForbidden, "AuthHeader Unavailable")
|
||||
logWarning(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
username := user
|
||||
|
||||
logInfo("%s %s %s %s %s Matched-Username: %s, Matched-Repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, username, repo)
|
||||
// dump log 记录详细信息 c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, full Header
|
||||
LogDump("%s %s %s %s %s %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, c.Request.Header)
|
||||
logInfo("%s %s %s %s %s Matched-Username: %s, Matched-Repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), username, repo)
|
||||
// dump log 记录详细信息 c.ClientIP(), c.Request.Method, rawPath,c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), full Header
|
||||
logDump("%s %s %s %s %s %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), c.Request.Header.Header())
|
||||
repouser := fmt.Sprintf("%s/%s", username, repo)
|
||||
|
||||
// 白名单检查
|
||||
if cfg.Whitelist.Enabled {
|
||||
whitelist := auth.CheckWhitelist(repouser, username, repo)
|
||||
whitelist := auth.CheckWhitelist(username, repo)
|
||||
if !whitelist {
|
||||
logErrMsg := fmt.Sprintf("%s %s %s %s %s Whitelist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, repouser)
|
||||
errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", repouser)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logWarning(logErrMsg)
|
||||
c.JSON(http.StatusForbidden, map[string]string{"error": errMsg})
|
||||
logWarning("%s %s %s %s %s Whitelist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), repouser)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 黑名单检查
|
||||
if cfg.Blacklist.Enabled {
|
||||
blacklist := auth.CheckBlacklist(repouser, username, repo)
|
||||
blacklist := auth.CheckBlacklist(username, repo)
|
||||
if blacklist {
|
||||
logErrMsg := fmt.Sprintf("%s %s %s %s %s Blacklist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, repouser)
|
||||
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", repouser)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logWarning(logErrMsg)
|
||||
c.JSON(http.StatusForbidden, map[string]string{"error": errMsg})
|
||||
logWarning("%s %s %s %s %s Blacklist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), repouser)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
if exps[5].MatchString(rawPath) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// 处理blob/raw路径
|
||||
if exps[1].MatchString(rawPath) {
|
||||
if matcher == "blob" {
|
||||
rawPath = strings.Replace(rawPath, "/blob/", "/raw/", 1)
|
||||
}
|
||||
|
||||
// 鉴权
|
||||
authcheck, err := auth.AuthHandler(c, cfg)
|
||||
var authcheck bool
|
||||
authcheck, err = auth.AuthHandler(ctx, c, cfg)
|
||||
if !authcheck {
|
||||
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)
|
||||
//c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||
c.AbortWithStatusJSON(401, map[string]string{"error": "Unauthorized"})
|
||||
logWarning("%s %s %s %s %s Auth-Error: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), err)
|
||||
return
|
||||
}
|
||||
|
||||
// IP METHOD URL USERAGENT PROTO MATCHES
|
||||
logDebug("%s %s %s %s %s Matches: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, matches)
|
||||
logDebug("%s %s %s %s %s Matches: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), matches)
|
||||
|
||||
switch {
|
||||
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
||||
//ProxyRequest(c, rawPath, cfg, "chrome", runMode)
|
||||
ChunkedProxyRequest(c, rawPath, cfg, "chrome", runMode) // dev test chunk
|
||||
case exps[2].MatchString(rawPath):
|
||||
//ProxyRequest(c, rawPath, cfg, "git", runMode)
|
||||
GitReq(c, rawPath, cfg, "git", runMode)
|
||||
switch matcher {
|
||||
case "releases", "blob", "raw", "gist", "api":
|
||||
ChunkedProxyRequest(ctx, c, rawPath, cfg, matcher)
|
||||
case "clone":
|
||||
GitReq(ctx, c, rawPath, cfg, "git")
|
||||
default:
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
fmt.Println("Invalid input.")
|
||||
|
||||
143
proxy/httpc.go
Normal file
143
proxy/httpc.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
httpc "github.com/satomitouka/touka-httpc"
|
||||
)
|
||||
|
||||
var BufferSize int = 32 * 1024 // 32KB
|
||||
|
||||
var (
|
||||
tr *http.Transport
|
||||
gittr *http.Transport
|
||||
BufferPool *sync.Pool
|
||||
client *httpc.Client
|
||||
gitclient *httpc.Client
|
||||
)
|
||||
|
||||
func InitReq(cfg *config.Config) {
|
||||
initHTTPClient(cfg)
|
||||
if cfg.GitClone.Mode == "cache" {
|
||||
initGitHTTPClient(cfg)
|
||||
}
|
||||
|
||||
// 初始化固定大小的缓存池
|
||||
BufferPool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, BufferSize)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func initHTTPClient(cfg *config.Config) {
|
||||
var proTolcols = new(http.Protocols)
|
||||
proTolcols.SetHTTP1(true)
|
||||
proTolcols.SetHTTP2(true)
|
||||
proTolcols.SetUnencryptedHTTP2(true)
|
||||
if cfg.Httpc.Mode == "auto" {
|
||||
|
||||
tr = &http.Transport{
|
||||
//MaxIdleConns: 160,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
Protocols: proTolcols,
|
||||
}
|
||||
} else if cfg.Httpc.Mode == "advanced" {
|
||||
tr = &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")
|
||||
tr = &http.Transport{
|
||||
//MaxIdleConns: 160,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
}
|
||||
}
|
||||
if cfg.Outbound.Enabled {
|
||||
initTransport(cfg, tr)
|
||||
}
|
||||
if cfg.Server.Debug {
|
||||
client = httpc.New(
|
||||
httpc.WithTransport(tr),
|
||||
httpc.WithDumpLog(),
|
||||
)
|
||||
} else {
|
||||
client = httpc.New(
|
||||
httpc.WithTransport(tr),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
)
|
||||
}
|
||||
}
|
||||
327
proxy/match.go
Normal file
327
proxy/match.go
Normal file
@@ -0,0 +1,327 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
||||
"github.com/valyala/bytebufferpool"
|
||||
)
|
||||
|
||||
// 定义错误类型, 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
|
||||
}
|
||||
|
||||
// processLinksAndWriteChunked 处理链接并将结果以 chunked 方式写入响应
|
||||
func ProcessLinksAndWriteChunked(input io.Reader, compress string, host string, cfg *config.Config, c *app.RequestContext) error {
|
||||
var reader *bufio.Reader
|
||||
|
||||
if compress == "gzip" {
|
||||
// 解压 gzip
|
||||
gzipReader, err := gzip.NewReader(input)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("gzip 解压错误: %v", err))
|
||||
return fmt.Errorf("gzip 解压错误: %w", err)
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
reader = bufio.NewReader(gzipReader)
|
||||
} else {
|
||||
reader = bufio.NewReader(input)
|
||||
}
|
||||
|
||||
// 获取 chunked body writer
|
||||
chunkedWriter := hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter())
|
||||
|
||||
var writer io.Writer = chunkedWriter
|
||||
var gzipWriter *gzip.Writer
|
||||
|
||||
if compress == "gzip" {
|
||||
gzipWriter = gzip.NewWriter(writer)
|
||||
writer = gzipWriter
|
||||
defer func() {
|
||||
if err := gzipWriter.Close(); err != nil {
|
||||
logError("gzipWriter close failed: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
bufWrapper := bytebufferpool.Get()
|
||||
buf := bufWrapper.B
|
||||
size := 32768 // 32KB
|
||||
buf = buf[:cap(buf)]
|
||||
if len(buf) < size {
|
||||
buf = append(buf, make([]byte, size-len(buf))...)
|
||||
}
|
||||
buf = buf[:size] // 将缓冲区限制为 'size'
|
||||
defer bytebufferpool.Put(bufWrapper)
|
||||
|
||||
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
||||
return modifyURL(originalURL, host, cfg)
|
||||
})
|
||||
modifiedLineWithNewline := modifiedLine + "\n"
|
||||
|
||||
_, err := writer.Write([]byte(modifiedLineWithNewline))
|
||||
if err != nil {
|
||||
logError("写入 chunk 错误: %v", err)
|
||||
return fmt.Errorf("写入 chunk 错误: %w", err)
|
||||
}
|
||||
|
||||
if compress != "gzip" {
|
||||
if fErr := chunkedWriter.Flush(); fErr != nil {
|
||||
logError("chunkedWriter flush failed: %v", fErr)
|
||||
return fmt.Errorf("chunkedWriter flush failed: %w", fErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
logError("读取输入错误: %v", err)
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("读取输入错误: %v", err))
|
||||
return fmt.Errorf("读取输入错误: %w", err)
|
||||
}
|
||||
|
||||
// 对于 gzip,chunkedWriter 的关闭会触发最后的 chunk
|
||||
if compress != "gzip" {
|
||||
if fErr := chunkedWriter.Flush(); fErr != nil {
|
||||
logError("final chunkedWriter flush failed: %v", fErr)
|
||||
return fmt.Errorf("final chunkedWriter flush failed: %w", fErr)
|
||||
}
|
||||
}
|
||||
|
||||
return 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 "", ""
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 日志模块
|
||||
var (
|
||||
logw = logger.Logw
|
||||
LogDump = logger.LogDump
|
||||
logDebug = logger.LogDebug
|
||||
logInfo = logger.LogInfo
|
||||
logWarning = logger.LogWarning
|
||||
logError = logger.LogError
|
||||
)
|
||||
|
||||
var exps = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`),
|
||||
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`),
|
||||
regexp.MustCompile(`^(?:https?://)?gist\.github(?:usercontent|)\.com/([^/]+)/.+?/.+`),
|
||||
regexp.MustCompile(`^(?:https?://)?api\.github\.com/repos/([^/]+)/([^/]+)/.*`),
|
||||
}
|
||||
|
||||
// 读取请求体
|
||||
func readRequestBody(c *gin.Context) ([]byte, error) {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
logError("failed to read request body: %v", err)
|
||||
return nil, fmt.Errorf("failed to read request body: %v", err)
|
||||
}
|
||||
defer c.Request.Body.Close()
|
||||
return body, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func SendRequest(c *gin.Context, req *req.Request, method, url string) (*req.Response, error) {
|
||||
switch method {
|
||||
case "GET":
|
||||
return req.Get(url)
|
||||
case "POST":
|
||||
return req.Post(url)
|
||||
case "PUT":
|
||||
return req.Put(url)
|
||||
case "DELETE":
|
||||
return req.Delete(url)
|
||||
default:
|
||||
// IP METHOD URL USERAGENT PROTO UNSUPPORTED-METHOD
|
||||
errmsg := fmt.Sprintf("%s %s %s %s %s Unsupported method", c.ClientIP(), method, url, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning(errmsg)
|
||||
return nil, fmt.Errorf(errmsg)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func HandleError(c *gin.Context, message string) {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||
logError(message)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
// 处理响应大小
|
||||
func HandleResponseSize(resp *http.Response, cfg *config.Config, c *gin.Context) error {
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
sizelimit := cfg.Server.SizeLimit * 1024 * 1024
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
return fmt.Errorf("Path: %s size limit exceeded: %d", finalURL, size)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
@@ -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,20 +2,47 @@ package proxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
// 设置请求头
|
||||
func setRequestHeaders(c *gin.Context, req *http.Request) {
|
||||
for key, values := range c.Request.Header {
|
||||
for _, value := range values {
|
||||
req.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
func setRequestHeaders(c *app.RequestContext, req *http.Request) {
|
||||
c.Request.Header.VisitAll(func(key, value []byte) {
|
||||
req.Header.Set(string(key), string(value))
|
||||
})
|
||||
}
|
||||
|
||||
func removeWSHeader(req *http.Request) {
|
||||
req.Header.Del("Upgrade")
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package timing
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 阶段计时结构(固定数组优化)
|
||||
type timingData struct {
|
||||
phases [8]struct { // 预分配8个阶段存储
|
||||
name string
|
||||
dur time.Duration
|
||||
}
|
||||
count int
|
||||
start time.Time
|
||||
}
|
||||
|
||||
// 对象池(内存重用优化)
|
||||
var pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(timingData)
|
||||
},
|
||||
}
|
||||
|
||||
// 中间件入口
|
||||
func Middleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 从池中获取计时器
|
||||
td := pool.Get().(*timingData)
|
||||
td.start = time.Now()
|
||||
td.count = 0
|
||||
|
||||
// 存储到上下文
|
||||
c.Set("timing", td)
|
||||
|
||||
// 请求完成后回收对象
|
||||
defer func() {
|
||||
pool.Put(td)
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// 记录阶段耗时
|
||||
func Record(c *gin.Context, name string) {
|
||||
if val, exists := c.Get("timing"); exists {
|
||||
//td := val.(*timingData)
|
||||
td, ok := val.(*timingData)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if td.count < len(td.phases) {
|
||||
td.phases[td.count].name = name
|
||||
td.phases[td.count].dur = time.Since(td.start) // 直接记录当前时间
|
||||
td.count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取计时结果(日志输出用)
|
||||
func Get(c *gin.Context) (total time.Duration, phases []struct {
|
||||
Name string
|
||||
Dur time.Duration
|
||||
}) {
|
||||
if val, exists := c.Get("timing"); exists {
|
||||
//td := val.(*timingData)
|
||||
td, ok := val.(*timingData)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
for i := 0; i < td.count; i++ {
|
||||
phases = append(phases, struct {
|
||||
Name string
|
||||
Dur time.Duration
|
||||
}{
|
||||
Name: td.phases[i].name,
|
||||
Dur: td.phases[i].dur,
|
||||
})
|
||||
}
|
||||
total = time.Since(td.start)
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user