Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ac08bba56 | ||
|
|
61e741c9b3 | ||
|
|
815b86c6c6 | ||
|
|
999016be2b | ||
|
|
af6c6cfe64 | ||
|
|
88ee8abb5c | ||
|
|
2d9ce57e90 | ||
|
|
d33377087f | ||
|
|
c78d114767 | ||
|
|
b87a8de3c4 | ||
|
|
5dbf137116 | ||
|
|
57f67278a3 | ||
|
|
b9a7f30705 | ||
|
|
c0a9ed11e3 | ||
|
|
eec50879e9 | ||
|
|
862b92a1c5 | ||
|
|
1d7780a890 | ||
|
|
ee215eff36 | ||
|
|
20b9c7827d | ||
|
|
89e6be7709 |
34
CHANGELOG.md
34
CHANGELOG.md
@@ -1,5 +1,39 @@
|
||||
# 更新日志
|
||||
|
||||
v1.3.0
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 优化黑名单功能,提升稳定性
|
||||
- CHANGE: 剃刀计划,减少调试用日志输出
|
||||
- ADD: 新增auth子模块blacklist.go,支持黑名单功能
|
||||
- ADD: 新增blacklist.json文件,用于配置黑名单
|
||||
- CHANGE: config.yaml文件格式修改,使其具备更好的可读性
|
||||
- WARNING: 此版本为大版本更新,配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||
|
||||
24w09b
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 修正配置,提升稳定性
|
||||
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||
|
||||
24w09a
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- CHANGE: 优化黑名单功能,提升稳定性
|
||||
- CHANGE&ADD: 新增auth子模块blacklist.go
|
||||
- CHANGE: 黑名单转为使用json文件存储,便于程序处理
|
||||
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||
|
||||
24w08b
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
- ADD & CHANGE: 新增仓库黑名单功能,改进Auth模块
|
||||
- ADD: 新增blacklist.yaml文件,用于配置仓库黑名单
|
||||
- CHANGE: 大幅度修改Config包,使其更加模块化
|
||||
- CHANGE: 与Config包同步修改config.yaml文件(不向前兼容)
|
||||
- CHANGE: 修改config.yaml文件的格式,使其具备更好的可读性
|
||||
- WARNING: 此版本配置文件重构,此版本不再向前兼容,请注意备份文件并重新部署
|
||||
|
||||
v1.2.0
|
||||
---
|
||||
- CHANGE: 优化代码结构,提升性能
|
||||
|
||||
@@ -1 +1 @@
|
||||
24w08a
|
||||
24w09b
|
||||
50
README.md
50
README.md
@@ -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)模板构建,具有标准化的日志记录与构建流程
|
||||
@@ -62,19 +63,51 @@ 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 # 监听端口(小白请勿修改)
|
||||
host: "127.0.0.1" # 监听地址(小白请勿修改)
|
||||
sizelimit: 131072000 # 125MB
|
||||
|
||||
# 日志配置
|
||||
logger:
|
||||
logfilepath: "/data/ghproxy/log/ghproxy.log" # 日志文件路径(小白请勿修改)
|
||||
maxlogsize: 25 # MB
|
||||
|
||||
# CORS 配置
|
||||
cors:
|
||||
enabled: true # 是否开启CORS
|
||||
|
||||
# 鉴权配置
|
||||
auth:
|
||||
enabled: false # 是否开启鉴权
|
||||
authtoken: "test" # 鉴权Token
|
||||
|
||||
# 黑名单配置
|
||||
blacklist:
|
||||
enabled: true
|
||||
blacklistfile: "/data/ghproxy/config/blacklist.json"
|
||||
|
||||
```
|
||||
port: 8080 # 监听端口
|
||||
host: "127.0.0.1" # 监听地址
|
||||
sizelimit: 131072000 # 125MB
|
||||
logfilepath: "/data/ghproxy/log/ghproxy.log" # 日志文件路径
|
||||
CorsAllowOrigins: true # 是否允许跨域请求
|
||||
auth: true # 是否开启鉴权
|
||||
authtoken: "test" # 鉴权token
|
||||
|
||||
### 黑名单配置
|
||||
|
||||
黑名单配置位于config/blacklist.json,格式如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"blacklist": [
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/repo3"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Caddy反代配置
|
||||
|
||||
```
|
||||
```Caddyfile
|
||||
example.com {
|
||||
reverse_proxy {
|
||||
to 127.0.0.1:7210
|
||||
@@ -94,6 +127,7 @@ example.com {
|
||||
- [x] 允许更多参数通过config结构传入
|
||||
- [x] 改进程序效率
|
||||
- [x] 用户鉴权
|
||||
- [x] 仓库黑名单
|
||||
|
||||
### DEV
|
||||
|
||||
|
||||
@@ -11,8 +11,7 @@ var logw = logger.Logw
|
||||
|
||||
func AuthHandler(c *gin.Context, cfg *config.Config) bool {
|
||||
// 如果身份验证未启用,直接返回 true
|
||||
if !cfg.Auth {
|
||||
logw("auth PASSED")
|
||||
if !cfg.Auth.Enabled {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -26,10 +25,11 @@ func AuthHandler(c *gin.Context, cfg *config.Config) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
isValid := authToken == cfg.AuthToken
|
||||
isValid := authToken == cfg.Auth.AuthToken
|
||||
if !isValid {
|
||||
logw("auth FAILED: invalid auth_token: %s", authToken)
|
||||
}
|
||||
|
||||
logw("auth SUCCESS: %t", isValid)
|
||||
return isValid
|
||||
}
|
||||
|
||||
46
auth/blacklist.go
Normal file
46
auth/blacklist.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"ghproxy/config"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Blacklist []string `json:"blacklist"`
|
||||
}
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
blacklistfile = "/data/ghproxy/config/blacklist.json"
|
||||
blacklist *Config
|
||||
)
|
||||
|
||||
func LoadBlacklist(cfg *config.Config) {
|
||||
blacklistfile = cfg.Blacklist.BlacklistFile
|
||||
blacklist = &Config{}
|
||||
|
||||
data, err := os.ReadFile(blacklistfile)
|
||||
if err != nil {
|
||||
logw("Failed to read blacklist file: %v", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, blacklist)
|
||||
if err != nil {
|
||||
logw("Failed to unmarshal blacklist JSON: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckBlacklist(fullrepo string) bool {
|
||||
return forRangeCheck(blacklist.Blacklist, fullrepo)
|
||||
}
|
||||
|
||||
func forRangeCheck(blist []string, fullrepo string) bool {
|
||||
for _, blocked := range blist {
|
||||
if blocked == fullrepo {
|
||||
return true
|
||||
}
|
||||
}
|
||||
logw("%s not in blacklist", fullrepo)
|
||||
return false
|
||||
}
|
||||
8
config/blacklist.json
Normal file
8
config/blacklist.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"blacklist": [
|
||||
"test/test1",
|
||||
"example/repo2",
|
||||
"another/repo3"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -7,26 +7,46 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
Host string `yaml:"host"`
|
||||
SizeLimit int `yaml:"sizelimit"`
|
||||
LogFilePath string `yaml:"logfilepath"`
|
||||
MaxLogSize int `yaml:"maxlogsize"`
|
||||
CORSOrigin bool `yaml:"CorsAllowOrigins"`
|
||||
Auth bool `yaml:"auth"`
|
||||
AuthToken string `yaml:"authtoken"`
|
||||
Server struct {
|
||||
Port int `yaml:"port"`
|
||||
Host string `yaml:"host"`
|
||||
SizeLimit int `yaml:"sizelimit"`
|
||||
} `yaml:"server"`
|
||||
|
||||
Log struct {
|
||||
LogFilePath string `yaml:"logfilepath"`
|
||||
MaxLogSize int `yaml:"maxlogsize"`
|
||||
} `yaml:"logger"`
|
||||
|
||||
CORS struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
} `yaml:"cors"`
|
||||
|
||||
Auth struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
AuthToken string `yaml:"authtoken"`
|
||||
} `yaml:"auth"`
|
||||
|
||||
Blacklist struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
BlacklistFile string `yaml:"blacklistfile"`
|
||||
} `yaml:"blacklist"`
|
||||
}
|
||||
|
||||
// LoadConfig 从 YAML 配置文件加载配置
|
||||
func LoadConfig(filePath string) (*Config, error) {
|
||||
var config Config
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = yaml.Unmarshal(data, &config)
|
||||
if err != nil {
|
||||
if err := loadYAML(filePath, &config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// LoadyamlConfig 从 YAML 配置文件加载配置
|
||||
func loadYAML(filePath string, out interface{}) error {
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return yaml.Unmarshal(data, out)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,24 @@
|
||||
port: 8080
|
||||
host: "127.0.0.1"
|
||||
sizelimit: 131072000 # 125MB
|
||||
logfilepath: "/data/ghproxy/log/ghproxy.log"
|
||||
maxlogsize: 25 #MB
|
||||
CorsAllowOrigins: true
|
||||
auth: false
|
||||
authtoken: "test"
|
||||
# Server Configuration
|
||||
server:
|
||||
port: 8080
|
||||
host: "127.0.0.1"
|
||||
sizelimit: 131072000 # 125MB
|
||||
|
||||
# Logging Configuration
|
||||
logger:
|
||||
logfilepath: "/data/ghproxy/log/ghproxy.log"
|
||||
maxlogsize: 25 # MB
|
||||
|
||||
# CORS Configuration
|
||||
cors:
|
||||
enabled: true
|
||||
|
||||
# Authentication Configuration
|
||||
auth:
|
||||
enabled: false
|
||||
authtoken: "test"
|
||||
|
||||
# Blacklist Configuration
|
||||
blacklist:
|
||||
enabled: true
|
||||
blacklistfile: "/data/ghproxy/config/blacklist.json"
|
||||
|
||||
@@ -13,6 +13,7 @@ RUN wget -O /data/caddy/Caddyfile https://raw.githubusercontent.com/${USER}/${RE
|
||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/DEV-VERSION) && \
|
||||
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 /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,7 @@ RUN wget -O /data/caddy/Caddyfile https://raw.githubusercontent.com/${USER}/${RE
|
||||
RUN VERSION=$(curl -s https://raw.githubusercontent.com/${USER}/${REPO}/main/VERSION) && \
|
||||
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 /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
@@ -6,6 +6,10 @@ if [ ! -f /data/caddy/config/Caddyfile ]; then
|
||||
cp /data/caddy/Caddyfile /data/caddy/config/Caddyfile
|
||||
fi
|
||||
|
||||
if [ ! -f /data/${APPLICATON}/config/blacklist.json ]; then
|
||||
cp /data/${APPLICATON}/blacklist.json /data/${APPLICATON}/config/blacklist.json
|
||||
fi
|
||||
|
||||
if [ ! -f /data/${APPLICATON}/config/config.yaml ]; then
|
||||
cp /data/${APPLICATON}/config.yaml /data/${APPLICATON}/config/config.yaml
|
||||
fi
|
||||
|
||||
12
main.go
12
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"ghproxy/auth"
|
||||
"ghproxy/config"
|
||||
"ghproxy/logger"
|
||||
"ghproxy/proxy"
|
||||
@@ -44,7 +45,7 @@ func loadConfig() {
|
||||
func setupLogger() {
|
||||
// 初始化日志模块
|
||||
var err error
|
||||
err = logger.Init(cfg.LogFilePath, cfg.MaxLogSize) // 传递日志文件路径
|
||||
err = logger.Init(cfg.Log.LogFilePath, cfg.Log.MaxLogSize) // 传递日志文件路径
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize logger: %v", err)
|
||||
}
|
||||
@@ -52,9 +53,14 @@ func setupLogger() {
|
||||
logw("Init Completed")
|
||||
}
|
||||
|
||||
func LoadBlacklist(cfg *config.Config) {
|
||||
auth.LoadBlacklist(cfg)
|
||||
}
|
||||
|
||||
func init() {
|
||||
loadConfig()
|
||||
setupLogger()
|
||||
LoadBlacklist(cfg)
|
||||
|
||||
// 设置 Gin 模式
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
@@ -82,7 +88,7 @@ func init() {
|
||||
|
||||
func main() {
|
||||
// 启动服务器
|
||||
err := router.Run(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
|
||||
err := router.Run(fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port))
|
||||
if err != nil {
|
||||
log.Fatalf("Error starting server: %v\n", err)
|
||||
}
|
||||
@@ -94,6 +100,6 @@ func api(c *gin.Context) {
|
||||
// 设置响应头
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
||||
"MaxResponseBodySize": cfg.SizeLimit,
|
||||
"MaxResponseBodySize": cfg.Server.SizeLimit,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
)
|
||||
|
||||
var logw = logger.Logw
|
||||
var cfg *config.Config
|
||||
|
||||
var exps = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`),
|
||||
@@ -34,11 +33,40 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||
re := regexp.MustCompile(`^(http:|https:)?/?/?(.*)`)
|
||||
matches := re.FindStringSubmatch(rawPath)
|
||||
|
||||
if len(matches) < 3 {
|
||||
logw("Invalid URL: %s", rawPath)
|
||||
c.String(http.StatusForbidden, "Invalid URL.")
|
||||
return
|
||||
}
|
||||
|
||||
rawPath = "https://" + matches[2]
|
||||
|
||||
// 提取用户名和仓库名,格式为 handle/<username>/<repo>/*
|
||||
pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`)
|
||||
pathParts := pathmatches.FindStringSubmatch(matches[2])
|
||||
if len(pathParts) < 4 {
|
||||
logw("Invalid path: %s", rawPath)
|
||||
c.String(http.StatusForbidden, "Invalid path; expected username/repo.")
|
||||
return
|
||||
}
|
||||
|
||||
username := pathParts[2]
|
||||
repo := pathParts[3]
|
||||
logw("Blacklist Check > Username: %s, Repo: %s", username, repo)
|
||||
fullrepo := fmt.Sprintf("%s/%s", username, repo)
|
||||
|
||||
// 黑名单检查
|
||||
blacklistpass := auth.CheckBlacklist(fullrepo)
|
||||
if blacklistpass {
|
||||
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logw(errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
matches = CheckURL(rawPath)
|
||||
if matches == nil {
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -52,7 +80,6 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
logw("Request: %s %s", c.Request.Method, rawPath)
|
||||
logw("Matches: %v", matches)
|
||||
|
||||
switch {
|
||||
@@ -71,7 +98,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()
|
||||
|
||||
@@ -138,7 +165,7 @@ func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context)
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > cfg.SizeLimit {
|
||||
if err == nil && size > cfg.Server.SizeLimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logw("Redirecting to %s due to size limit (%d bytes)", finalURL, size)
|
||||
@@ -166,7 +193,7 @@ func CopyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config)
|
||||
}
|
||||
|
||||
c.Header("Access-Control-Allow-Origin", "")
|
||||
if cfg.CORSOrigin {
|
||||
if cfg.CORS.Enabled {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user