Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ba2b94a00 | ||
|
|
5e764a6cd7 | ||
|
|
abeb6ee8eb | ||
|
|
df93a90081 | ||
|
|
05032158d4 | ||
|
|
b5bfc809a2 | ||
|
|
dc8f4a28e9 | ||
|
|
f5d7d0994e | ||
|
|
e3cffe70d9 | ||
|
|
822b6c8cea | ||
|
|
5ac08bba56 |
2
.github/workflows/build-dev.yml
vendored
2
.github/workflows/build-dev.yml
vendored
@@ -31,7 +31,7 @@ jobs:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- name: Build
|
||||
run: |
|
||||
go build -o ${{ env.OUTPUT_BINARY }} ./main.go
|
||||
CGO_ENABLED=0 go build -o ${{ env.OUTPUT_BINARY }} ./main.go
|
||||
- name: Package
|
||||
run: |
|
||||
tar -czvf ${{ env.OUTPUT_ARCHIVE }} ./${{ env.OUTPUT_BINARY }}
|
||||
|
||||
9
.github/workflows/build.yml
vendored
9
.github/workflows/build.yml
vendored
@@ -66,12 +66,11 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
- name: Load VERSION
|
||||
run: |
|
||||
if [ -f DEV-VERSION ]; then
|
||||
echo "VERSION=$(cat DEV-VERSION)" >> $GITHUB_ENV
|
||||
if [ -f VERSION ]; then
|
||||
echo "VERSION=$(cat VERSION)" >> $GITHUB_ENV
|
||||
else
|
||||
echo "DEV-VERSION file not found!" && exit 1
|
||||
fi
|
||||
|
||||
echo "VERSION file not found!" && exit 1
|
||||
fi
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
|
||||
71
CHANGELOG.md
71
CHANGELOG.md
@@ -1,13 +1,61 @@
|
||||
# 更新日志
|
||||
|
||||
v1.4.0
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- ADD: 新增auth子模块whitelist.go,支持白名单功能
|
||||
- ADD: 新增whitelist.json文件,用于配置白名单
|
||||
- CHANGE&ADD: 在config.yaml文件中新增白名单配置块
|
||||
- FIX: 由于临时加入且未在原开发路线上计划的白名单功能,导致函数命名冲突,在此修复blacklist.go的函数命名问题
|
||||
- FIX: 修复黑/白名单是否生效相关问题
|
||||
|
||||
24w11b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.4.0的预发布版本,请勿在生产环境中使用
|
||||
- FIX: 修复黑/白名单是否生效相关问题
|
||||
|
||||
24w11a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.4.0的预发布版本,请勿在生产环境中使用
|
||||
- **ANNOUNCE**: 自此版本起,DEV版本号格式进行修改,小版本号不再仅限于a/b,而是采用字母表顺序进行排列,此修改将带来一个重要改变,正式版前的预发布版本的数字版本号将会统一,以便于版本管理与发布管理
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- ADD: 新增auth子模块whitelist.go,支持白名单功能
|
||||
- ADD: 新增whitelist.json文件,用于配置白名单
|
||||
- FIX: 由于临时加入且未在原开发路线上计划的白名单功能,导致函数命名冲突,在此修复blacklist.go的函数命名问题
|
||||
|
||||
v1.3.1
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 剃刀计划,减少多余日志输出
|
||||
- CHANGE: 调整缓存参数
|
||||
|
||||
24w10a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.3.1的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 剃刀计划,减少多余日志输出
|
||||
- CHANGE: 调整缓存参数
|
||||
|
||||
v1.3.0
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 优化黑名单功能,提升稳定性
|
||||
- CHANGE: 剃刀计划,减少多余日志输出
|
||||
- ADD: 新增auth子模块blacklist.go,支持黑名单功能
|
||||
- ADD: 新增blacklist.json文件,用于配置黑名单
|
||||
- CHANGE: config.yaml文件格式修改,使其具备更好的可读性
|
||||
- WARNING: 此版本为大版本更新,配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||
|
||||
24w09b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.3.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 修正配置,提升稳定性
|
||||
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||
|
||||
24w09a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.3.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 优化黑名单功能,提升稳定性
|
||||
- CHANGE&ADD: 新增auth子模块blacklist.go
|
||||
@@ -16,6 +64,7 @@
|
||||
|
||||
24w08b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.3.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- ADD & CHANGE: 新增仓库黑名单功能,改进Auth模块
|
||||
- ADD: 新增blacklist.yaml文件,用于配置仓库黑名单
|
||||
@@ -32,6 +81,7 @@ v1.2.0
|
||||
|
||||
24w08a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.2.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 同步更新logger模块,与golang-temp项目定义的开发规范保持一致
|
||||
- ADD: 新增日志翻转功能
|
||||
|
||||
@@ -43,12 +93,14 @@ v1.1.1
|
||||
|
||||
24w07b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.1.1的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 修改部分代码,与golang-temp项目定义的开发规范保持一致
|
||||
- CHANGE: 更新Go版本至v1.23.2
|
||||
- CHANGE: 跟随Caddy更新,修改Caddyfile配置
|
||||
|
||||
24w07a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.1.1的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 修改部分代码,与golang-temp项目定义的开发规范保持一致
|
||||
- CHANGE: 更新Go版本至v1.23.2
|
||||
|
||||
@@ -63,6 +115,7 @@ v1.1.0
|
||||
|
||||
24w06b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.1.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 优化代码结构,对main函数进行模块化,提升可读性
|
||||
- CHANGE: Docker代理功能移至DEV版本内,保证稳定性
|
||||
- ADD&CHANGE: 增加Auth(用户鉴权)模块,并改进其的错误处理与日志记录
|
||||
@@ -72,6 +125,7 @@ v1.1.0
|
||||
|
||||
v1.0.0
|
||||
---
|
||||
- **ANNOUNCE**: 项目正式发布, 并迁移至[WJQSERVER-STUDIO/ghproxy](https://github.com/WJQSERVER-STUDIO/ghproxy),由Apache License Version 2.0转为WJQserver Studio License 请注意相关条例变更
|
||||
- CHANGE: 项目正式发布, 并迁移至[WJQSERVER-STUDIO/ghproxy](https://github.com/WJQSERVER-STUDIO/ghproxy)
|
||||
- CHANGE: 再次重构代码,优化性能,提升稳定性
|
||||
- CHANGE: 使用golang-temp项目作为底层构建,标准化日志与配置模块
|
||||
@@ -79,6 +133,7 @@ v1.0.0
|
||||
|
||||
24w06a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v1.0.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 与v1.0.0版本同步
|
||||
|
||||
v0.2.0
|
||||
@@ -90,21 +145,25 @@ v0.2.0
|
||||
|
||||
24w05b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.2.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 重命名proxychrome函数
|
||||
- ADD: 增加多处日志记录,便于审计与排障
|
||||
|
||||
24w05a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.2.0的预发布版本,请勿在生产环境中使用
|
||||
- FIX: 修正上一版本的req请求未继承请求方式的问题
|
||||
- CHANGE: 优化代码结构,进一步模块化,同时提升性能
|
||||
|
||||
24w04b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.2.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 更换Docker基础镜像为daily版本
|
||||
- ADD: 新增使用req库实现代理请求,使用chrome TLS指纹发起请求
|
||||
|
||||
24w04a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.2.0的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 调整程序结构,使用init函数初始化配置,并优化代码结构
|
||||
|
||||
v0.1.7
|
||||
@@ -113,6 +172,7 @@ v0.1.7
|
||||
|
||||
24w03b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.1.7的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 合入上游(wjqserver/caddy:latest)安全更新, 增强镜像安全性
|
||||
|
||||
v0.1.6
|
||||
@@ -122,6 +182,7 @@ v0.1.6
|
||||
|
||||
24w03a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.1.6的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 改进Docker代理相关Caddy配置
|
||||
- ADD: 新增跨域配置选项
|
||||
|
||||
@@ -132,16 +193,17 @@ v0.1.5
|
||||
|
||||
24w02b
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.1.5的预发布版本,请勿在生产环境中使用
|
||||
- ADD: 新增Docker代理 (未并入正式版)
|
||||
|
||||
24w02a
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v0.1.5的预发布版本,请勿在生产环境中使用
|
||||
- CHANGE: 更新Go版本至v1.23.1
|
||||
- CHANGE: 优化代码行为
|
||||
|
||||
v0.1.4
|
||||
---
|
||||
正式版24w01b内容更新
|
||||
- ADD: 新增外部文件配置功能
|
||||
- ADD: 新增日志功能
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
@@ -149,7 +211,7 @@ v0.1.4
|
||||
|
||||
24w01b
|
||||
---
|
||||
标志着项目正式进入自主开发阶段
|
||||
- PRE-RELEASE: 此版本是v0.1.4的预发布版本,请勿在生产环境中使用
|
||||
- ADD: 新增外部文件配置功能
|
||||
- ADD: 新增日志功能
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
@@ -157,12 +219,13 @@ v0.1.4
|
||||
|
||||
v0.1.3
|
||||
---
|
||||
开始自行维护项目,脱离上游更新
|
||||
- **ANNOUNCE**: 开始自行维护项目,脱离上游更新
|
||||
- CHANGE: 改进已有实现,增强程序稳定性
|
||||
|
||||
24w01a
|
||||
---
|
||||
首个DEV版本
|
||||
- PRE-RELEASE: 此版本是v0.1.3的预发布版本,请勿在生产环境中使用
|
||||
- **ANNOUNCE**: 首个DEV版本发布
|
||||
- CHANGE: 同步更新
|
||||
|
||||
v0.1.2
|
||||
|
||||
@@ -1 +1 @@
|
||||
24w09b
|
||||
24w12a
|
||||
52
README.md
52
README.md
@@ -5,7 +5,7 @@
|
||||
|
||||
使用Go实现的GHProxy,用于加速部分地区Github仓库的拉取,支持速率限制,用户鉴权,支持Docker部署
|
||||
|
||||
[DEMO](ghproxy.1888866.xyz)
|
||||
[DEMO](https://ghproxy.1888866.xyz)
|
||||
|
||||
## 项目说明
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
- 支持Docker部署
|
||||
- 支持速率限制
|
||||
- 支持用户鉴权
|
||||
- 支持自定义黑名单
|
||||
- 符合[RFC 7234](https://httpwg.org/specs/rfc7234.html)的HTTP Cache
|
||||
- 使用Caddy作为Web Server
|
||||
- 基于[WJQSERVER-STUDIO/golang-temp](https://github.com/WJQSERVER-STUDIO/golang-temp)模板构建,具有标准化的日志记录与构建流程
|
||||
@@ -50,7 +51,7 @@ git clone https://ghproxy.1888866.xyz/github.com/WJQSERVER-STUDIO/ghproxy.git
|
||||
- Docker-cli
|
||||
|
||||
```
|
||||
docker run -p 7210:80 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/caddy:/data/caddy/log --restart always wjqserver/ghproxy
|
||||
docker run -p 7210:80 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/caddy:/data/caddy/log -v ./ghproxy/config:/data/ghproxy/config --restart always wjqserver/ghproxy
|
||||
```
|
||||
|
||||
- Docker-Compose
|
||||
@@ -62,7 +63,7 @@ docker run -p 7210:80 -v ./ghproxy/log/run:/data/ghproxy/log -v ./ghproxy/log/ca
|
||||
本项目采用config.yaml作为外部配置,默认配置如下
|
||||
使用Docker部署时,慎重修改config.yaml,以免造成不必要的麻烦
|
||||
|
||||
```
|
||||
```yaml
|
||||
# 核心配置
|
||||
server:
|
||||
port: 8080 # 监听端口(小白请勿修改)
|
||||
@@ -72,7 +73,7 @@ server:
|
||||
# 日志配置
|
||||
logger:
|
||||
logfilepath: "/data/ghproxy/log/ghproxy.log" # 日志文件路径(小白请勿修改)
|
||||
maxlogsize: 25 # MB
|
||||
maxlogsize: 5 # MB
|
||||
|
||||
# CORS 配置
|
||||
cors:
|
||||
@@ -85,14 +86,47 @@ auth:
|
||||
|
||||
# 黑名单配置
|
||||
blacklist:
|
||||
enabled: true
|
||||
blacklistfile: "/data/ghproxy/config/blacklist.yaml"
|
||||
enabled: true # 是否开启黑名单
|
||||
blacklistfile: "/data/ghproxy/config/blacklist.json"
|
||||
|
||||
# 白名单配置
|
||||
whitelist:
|
||||
enabled: false # 是否开启白名单
|
||||
whitelistfile: "/data/ghproxy/config/whitelist.json"
|
||||
|
||||
```
|
||||
|
||||
### 黑名单配置
|
||||
|
||||
黑名单配置位于config/blacklist.json,格式如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"blacklist": [
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/repo3"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 白名单配置
|
||||
|
||||
白名单配置位于config/whitelist.json,格式如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"whitelist": [
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/repo3"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Caddy反代配置
|
||||
|
||||
```
|
||||
```Caddyfile
|
||||
example.com {
|
||||
reverse_proxy {
|
||||
to 127.0.0.1:7210
|
||||
@@ -112,9 +146,9 @@ example.com {
|
||||
- [x] 允许更多参数通过config结构传入
|
||||
- [x] 改进程序效率
|
||||
- [x] 用户鉴权
|
||||
- [ ] 仓库黑名单
|
||||
- [x] 仓库黑名单
|
||||
- [x] 仓库白名单
|
||||
|
||||
### DEV
|
||||
|
||||
- [x] Docker Pull 代理
|
||||
- [x] 仓库黑名单
|
||||
|
||||
@@ -7,15 +7,9 @@
|
||||
| 版本 | 是否支持 |
|
||||
| --- | --- |
|
||||
| v1.x.x | :white_check_mark: |
|
||||
| **w**a/b | :warning: 这是测试版本,用于开发测试,可能存在未知的安全隐患 |
|
||||
| **w**a/b/c... | :warning: 此为PRE-RELEASE版本,用于开发与测试,可能存在未知的问题 |
|
||||
| v0.x.x | :x: 这些版本不再受支持 |
|
||||
|
||||
### 版本说明
|
||||
|
||||
- **v1.0.0**: 这是正式发布的版本
|
||||
- **xx W xx A/B***: 这是开发测试版本,可能存在未知的安全隐患。不推荐在生产环境中使用。
|
||||
- **v0.x.x**: 这些早期版本不再受支持,建议尽快升级到 v1.0.0 或更高版本。
|
||||
|
||||
### 用户须知
|
||||
|
||||
本项目为开源项目,开发者不对使用本项目造成的任何损失或问题承担责任。用户需自行评估并承担使用本项目的风险。
|
||||
|
||||
@@ -12,7 +12,6 @@ var logw = logger.Logw
|
||||
func AuthHandler(c *gin.Context, cfg *config.Config) bool {
|
||||
// 如果身份验证未启用,直接返回 true
|
||||
if !cfg.Auth.Enabled {
|
||||
logw("auth PASSED")
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -31,5 +30,6 @@ func AuthHandler(c *gin.Context, cfg *config.Config) bool {
|
||||
logw("auth FAILED: invalid auth_token: %s", authToken)
|
||||
}
|
||||
|
||||
logw("auth SUCCESS: %t", isValid)
|
||||
return isValid
|
||||
}
|
||||
|
||||
@@ -6,19 +6,19 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
type BlacklistConfig struct {
|
||||
Blacklist []string `json:"blacklist"`
|
||||
}
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
blacklistfile = "/data/ghproxy/config/blacklist.json"
|
||||
blacklist *Config
|
||||
blacklist *BlacklistConfig
|
||||
)
|
||||
|
||||
func LoadBlacklist(cfg *config.Config) {
|
||||
blacklistfile = cfg.Blacklist.BlacklistFile
|
||||
blacklist = &Config{}
|
||||
blacklist = &BlacklistConfig{}
|
||||
|
||||
data, err := os.ReadFile(blacklistfile)
|
||||
if err != nil {
|
||||
@@ -32,15 +32,14 @@ func LoadBlacklist(cfg *config.Config) {
|
||||
}
|
||||
|
||||
func CheckBlacklist(fullrepo string) bool {
|
||||
return forRangeCheck(blacklist.Blacklist, fullrepo)
|
||||
return forRangeCheckBlacklist(blacklist.Blacklist, fullrepo)
|
||||
}
|
||||
|
||||
func forRangeCheck(blist []string, fullrepo string) bool {
|
||||
func forRangeCheckBlacklist(blist []string, fullrepo string) bool {
|
||||
for _, blocked := range blist {
|
||||
if blocked == fullrepo {
|
||||
return true
|
||||
}
|
||||
}
|
||||
logw("%s not in blacklist", fullrepo)
|
||||
return false
|
||||
}
|
||||
|
||||
44
auth/whitelist.go
Normal file
44
auth/whitelist.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"ghproxy/config"
|
||||
"os"
|
||||
)
|
||||
|
||||
type WhitelistConfig struct {
|
||||
Whitelist []string `json:"whitelist"`
|
||||
}
|
||||
|
||||
var (
|
||||
whitelistfile = "/data/ghproxy/config/whitelist.json"
|
||||
whitelist *WhitelistConfig
|
||||
)
|
||||
|
||||
func LoadWhitelist(cfg *config.Config) {
|
||||
whitelistfile = cfg.Whitelist.WhitelistFile
|
||||
whitelist = &WhitelistConfig{}
|
||||
|
||||
data, err := os.ReadFile(whitelistfile)
|
||||
if err != nil {
|
||||
logw("Failed to read whitelist file: %v", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, whitelist)
|
||||
if err != nil {
|
||||
logw("Failed to unmarshal whitelist JSON: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckWhitelist(fullrepo string) bool {
|
||||
return forRangeCheckWhitelist(whitelist.Whitelist, fullrepo)
|
||||
}
|
||||
|
||||
func forRangeCheckWhitelist(blist []string, fullrepo string) bool {
|
||||
for _, blocked := range blist {
|
||||
if blocked == fullrepo {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -72,10 +72,12 @@
|
||||
import header_realip
|
||||
}
|
||||
import log ghproxy
|
||||
import cache 0s 600s
|
||||
import cache 0s 300s
|
||||
import error_page
|
||||
import encode
|
||||
import rate_limit 60
|
||||
header Age 10
|
||||
header Cache-Control "max-age=300"
|
||||
route / {
|
||||
root /data/www
|
||||
file_server
|
||||
@@ -84,7 +86,7 @@
|
||||
route /favicon.ico {
|
||||
root /data/www
|
||||
file_server
|
||||
import cache 60s 24h
|
||||
import cache 0s 24h
|
||||
}
|
||||
handle_errors {
|
||||
@redirects `{err.status_code} in [301, 302, 307]`
|
||||
|
||||
@@ -60,12 +60,6 @@
|
||||
header_up X-Forwarded-Proto {http.request.header.CF-Visitor}
|
||||
}
|
||||
|
||||
(buffer) {
|
||||
flush_interval 2000s
|
||||
buffer_responses
|
||||
max_buffer_size 256k
|
||||
}
|
||||
|
||||
(rate_limit) {
|
||||
route /* {
|
||||
rate_limit {remote.ip} {args[0]}r/m 10000 429
|
||||
@@ -78,7 +72,7 @@
|
||||
import header_realip
|
||||
}
|
||||
import log ghproxy
|
||||
import cache 0s 600s
|
||||
import cache 0s 300s
|
||||
import error_page
|
||||
import encode
|
||||
route /* {
|
||||
@@ -92,7 +86,7 @@
|
||||
route /favicon.ico {
|
||||
root /data/www
|
||||
file_server
|
||||
import cache 60s 24h
|
||||
import cache 0s 24h
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"blacklist": [
|
||||
"black/list",
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/repo3"
|
||||
"example/repo2"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -31,6 +31,11 @@ type Config struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
BlacklistFile string `yaml:"blacklistfile"`
|
||||
} `yaml:"blacklist"`
|
||||
|
||||
Whitelist struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
WhitelistFile string `yaml:"whitelistfile"`
|
||||
} `yaml:"whitelist"`
|
||||
}
|
||||
|
||||
// LoadConfig 从 YAML 配置文件加载配置
|
||||
|
||||
@@ -7,7 +7,7 @@ server:
|
||||
# Logging Configuration
|
||||
logger:
|
||||
logfilepath: "/data/ghproxy/log/ghproxy.log"
|
||||
maxlogsize: 25 # MB
|
||||
maxlogsize: 5 # MB
|
||||
|
||||
# CORS Configuration
|
||||
cors:
|
||||
@@ -20,5 +20,10 @@ auth:
|
||||
|
||||
# Blacklist Configuration
|
||||
blacklist:
|
||||
enabled: true
|
||||
enabled: false
|
||||
blacklistfile: "/data/ghproxy/config/blacklist.json"
|
||||
|
||||
# Whitelist Configuration
|
||||
whitelist:
|
||||
enabled: false
|
||||
whitelistfile: "/data/ghproxy/config/whitelist.json"
|
||||
8
config/whitelist.json
Normal file
8
config/whitelist.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"whitelist": [
|
||||
"white/list",
|
||||
"white/test1",
|
||||
"example/white"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/DEV
|
||||
wget -O /data/${APPLICATION}/${APPLICATION} https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}
|
||||
RUN wget -O /data/${APPLICATION}/config.yaml https://raw.githubusercontent.com/${USER}/${REPO}/main/config/config.yaml
|
||||
RUN wget -O /data/${APPLICATION}/blacklist.json https://raw.githubusercontent.com/${USER}/${REPO}/main/config/blacklist.json
|
||||
RUN wget -O /data/${APPLICATION}/whitelist.json https://raw.githubusercontent.com/${USER}/${REPO}/main/config/whitelist.json
|
||||
RUN wget -O /usr/local/bin/init.sh https://raw.githubusercontent.com/${USER}/${REPO}/main/init.sh
|
||||
RUN chmod +x /data/${APPLICATION}/${APPLICATION}
|
||||
RUN chmod +x /usr/local/bin/init.sh
|
||||
|
||||
@@ -13,6 +13,8 @@ RUN wget -O /data/caddy/Caddyfile https://raw.githubusercontent.com/${USER}/${RE
|
||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/VERSION) && \
|
||||
wget -O /data/${APPLICATION}/${APPLICATION} https://github.com/${USER}/${REPO}/releases/download/$VERSION/${APPLICATION}
|
||||
RUN wget -O /data/${APPLICATION}/config.yaml https://raw.githubusercontent.com/${USER}/${REPO}/main/config/config.yaml
|
||||
RUN wget -O /data/${APPLICATION}/blacklist.json https://raw.githubusercontent.com/${USER}/${REPO}/main/config/blacklist.json
|
||||
RUN wget -O /data/${APPLICATION}/whitelist.json https://raw.githubusercontent.com/${USER}/${REPO}/main/config/whitelist.json
|
||||
RUN wget -O /usr/local/bin/init.sh https://raw.githubusercontent.com/${USER}/${REPO}/main/init.sh
|
||||
RUN chmod +x /data/${APPLICATION}/${APPLICATION}
|
||||
RUN chmod +x /usr/local/bin/init.sh
|
||||
|
||||
4
init.sh
4
init.sh
@@ -10,6 +10,10 @@ if [ ! -f /data/${APPLICATON}/config/blacklist.json ]; then
|
||||
cp /data/${APPLICATON}/blacklist.json /data/${APPLICATON}/config/blacklist.json
|
||||
fi
|
||||
|
||||
if [ ! -f /data/${APPLICATON}/config/whitelist.json ]; then
|
||||
cp /data/${APPLICATON}/whitelist.json /data/${APPLICATON}/config/whitelist.json
|
||||
fi
|
||||
|
||||
if [ ! -f /data/${APPLICATON}/config/config.yaml ]; then
|
||||
cp /data/${APPLICATON}/config.yaml /data/${APPLICATON}/config/config.yaml
|
||||
fi
|
||||
|
||||
16
main.go
16
main.go
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"ghproxy/auth"
|
||||
"ghproxy/config"
|
||||
@@ -22,16 +21,6 @@ var (
|
||||
configfile = "/data/ghproxy/config/config.yaml"
|
||||
)
|
||||
|
||||
var (
|
||||
exps = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`),
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`),
|
||||
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`),
|
||||
regexp.MustCompile(`^(?:https?://)?gist\.github\.com/([^/]+)/.+?/.+`),
|
||||
}
|
||||
)
|
||||
|
||||
func loadConfig() {
|
||||
var err error
|
||||
// 初始化配置
|
||||
@@ -53,14 +42,15 @@ func setupLogger() {
|
||||
logw("Init Completed")
|
||||
}
|
||||
|
||||
func LoadBlacklist(cfg *config.Config) {
|
||||
func Loadlist(cfg *config.Config) {
|
||||
auth.LoadBlacklist(cfg)
|
||||
auth.LoadWhitelist(cfg)
|
||||
}
|
||||
|
||||
func init() {
|
||||
loadConfig()
|
||||
setupLogger()
|
||||
LoadBlacklist(cfg)
|
||||
Loadlist(cfg)
|
||||
|
||||
// 设置 Gin 模式
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
@@ -32,7 +32,6 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
|
||||
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
|
||||
matches := re.FindStringSubmatch(rawPath)
|
||||
logw("Matches: %v", matches[2])
|
||||
|
||||
if len(matches) < 3 {
|
||||
logw("Invalid URL: %s", rawPath)
|
||||
@@ -56,12 +55,26 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||
logw("Blacklist Check > Username: %s, Repo: %s", username, repo)
|
||||
fullrepo := fmt.Sprintf("%s/%s", username, repo)
|
||||
|
||||
// 白名单检查
|
||||
if cfg.Whitelist.Enabled {
|
||||
whitelistpass := auth.CheckWhitelist(fullrepo)
|
||||
if !whitelistpass {
|
||||
errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", fullrepo)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logw(errMsg)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 黑名单检查
|
||||
blacklistpass := auth.CheckBlacklist(fullrepo)
|
||||
if blacklistpass {
|
||||
c.AbortWithStatus(http.StatusForbidden)
|
||||
logw("Blacklisted repo: %s", fullrepo)
|
||||
return
|
||||
if cfg.Blacklist.Enabled {
|
||||
blacklistpass := auth.CheckBlacklist(fullrepo)
|
||||
if blacklistpass {
|
||||
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logw(errMsg)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
matches = CheckURL(rawPath)
|
||||
@@ -80,7 +93,6 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
logw("Request: %s %s", c.Request.Method, rawPath)
|
||||
logw("Matches: %v", matches)
|
||||
|
||||
switch {
|
||||
@@ -99,7 +111,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||
|
||||
func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
||||
method := c.Request.Method
|
||||
logw("%s Method: %s", u, method)
|
||||
logw("%s %s", method, u)
|
||||
|
||||
client := req.C()
|
||||
|
||||
@@ -197,6 +209,9 @@ func CopyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config)
|
||||
if cfg.CORS.Enabled {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
|
||||
c.Header("Age", "10")
|
||||
c.Header("Cache-Control", "max-age=300")
|
||||
}
|
||||
|
||||
func HandleError(c *gin.Context, message string) {
|
||||
|
||||
Reference in New Issue
Block a user