Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f77d951500 | ||
|
|
685388fff9 | ||
|
|
c6d95e683f | ||
|
|
f8828ccb74 | ||
|
|
fdc156adad | ||
|
|
80b0173d7c |
13
.github/workflows/docker-ghcr.yml
vendored
13
.github/workflows/docker-ghcr.yml
vendored
@@ -3,9 +3,9 @@ on:
|
|||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
version:
|
version:
|
||||||
description: 'Version number'
|
description: '版本号 (例如: v1.0.0)'
|
||||||
required: true
|
required: true
|
||||||
default: 'latest'
|
default: 'v1.0.0'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
@@ -36,7 +36,12 @@ jobs:
|
|||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Set version from input
|
- name: Set version from input
|
||||||
run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
|
run: |
|
||||||
|
VERSION=${{ github.event.inputs.version }}
|
||||||
|
if [[ $VERSION == v* ]]; then
|
||||||
|
VERSION=${VERSION:1}
|
||||||
|
fi
|
||||||
|
echo "VERSION=$VERSION" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Convert repository name to lowercase
|
- name: Convert repository name to lowercase
|
||||||
run: |
|
run: |
|
||||||
@@ -53,4 +58,4 @@ jobs:
|
|||||||
--build-arg VERSION=${{ env.VERSION }} \
|
--build-arg VERSION=${{ env.VERSION }} \
|
||||||
-f Dockerfile .
|
-f Dockerfile .
|
||||||
env:
|
env:
|
||||||
GHCR_PUBLIC: true # 将镜像设置为公开
|
GHCR_PUBLIC: true
|
||||||
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@@ -1,7 +1,7 @@
|
|||||||
name: 发布二进制文件
|
name: 发布二进制文件
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch: # 手动触发
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
version:
|
version:
|
||||||
description: '版本号 (例如: v1.0.0)'
|
description: '版本号 (例如: v1.0.0)'
|
||||||
@@ -18,7 +18,7 @@ jobs:
|
|||||||
- name: 检出代码
|
- name: 检出代码
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # 获取完整历史,用于生成变更日志
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: 设置Go环境
|
- name: 设置Go环境
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v5
|
||||||
|
|||||||
@@ -28,9 +28,16 @@ var dockerProxy *DockerProxy
|
|||||||
type RegistryDetector struct{}
|
type RegistryDetector struct{}
|
||||||
|
|
||||||
// detectRegistryDomain 检测Registry域名并返回域名和剩余路径
|
// detectRegistryDomain 检测Registry域名并返回域名和剩余路径
|
||||||
func (rd *RegistryDetector) detectRegistryDomain(path string) (string, string) {
|
func (rd *RegistryDetector) detectRegistryDomain(c *gin.Context, path string) (string, string) {
|
||||||
cfg := config.GetConfig()
|
cfg := config.GetConfig()
|
||||||
|
|
||||||
|
// 兼容Containerd的ns参数
|
||||||
|
if ns := c.Query("ns"); ns != "" {
|
||||||
|
if mapping, exists := cfg.Registries[ns]; exists && mapping.Enabled {
|
||||||
|
return ns, path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for domain := range cfg.Registries {
|
for domain := range cfg.Registries {
|
||||||
if strings.HasPrefix(path, domain+"/") {
|
if strings.HasPrefix(path, domain+"/") {
|
||||||
remainingPath := strings.TrimPrefix(path, domain+"/")
|
remainingPath := strings.TrimPrefix(path, domain+"/")
|
||||||
@@ -99,7 +106,7 @@ func ProxyDockerRegistryGin(c *gin.Context) {
|
|||||||
func handleRegistryRequest(c *gin.Context, path string) {
|
func handleRegistryRequest(c *gin.Context, path string) {
|
||||||
pathWithoutV2 := strings.TrimPrefix(path, "/v2/")
|
pathWithoutV2 := strings.TrimPrefix(path, "/v2/")
|
||||||
|
|
||||||
if registryDomain, remainingPath := registryDetector.detectRegistryDomain(pathWithoutV2); registryDomain != "" {
|
if registryDomain, remainingPath := registryDetector.detectRegistryDomain(c, pathWithoutV2); registryDomain != "" {
|
||||||
if registryDetector.isRegistryEnabled(registryDomain) {
|
if registryDetector.isRegistryEnabled(registryDomain) {
|
||||||
c.Set("target_registry_domain", registryDomain)
|
c.Set("target_registry_domain", registryDomain)
|
||||||
c.Set("target_path", remainingPath)
|
c.Set("target_path", remainingPath)
|
||||||
|
|||||||
@@ -171,9 +171,9 @@ func proxyGitHubWithRedirect(c *gin.Context, u string, redirectCount int) {
|
|||||||
|
|
||||||
processedBody, processedSize, err := utils.ProcessSmart(resp.Body, isGzipCompressed, realHost)
|
processedBody, processedSize, err := utils.ProcessSmart(resp.Body, isGzipCompressed, realHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("智能处理失败,回退到直接代理: %v\n", err)
|
fmt.Printf("脚本处理失败: %v\n", err)
|
||||||
processedBody = resp.Body
|
c.String(http.StatusBadGateway, "Script processing failed: %v", err)
|
||||||
processedSize = 0
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 智能设置响应头
|
// 智能设置响应头
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ func main() {
|
|||||||
fmt.Printf("H2c: 已启用\n")
|
fmt.Printf("H2c: 已启用\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("版本号: v1.2.0\n")
|
fmt.Printf("版本号: v1.2.1\n")
|
||||||
fmt.Printf("项目地址: https://github.com/sky22333/hubproxy\n")
|
fmt.Printf("项目地址: https://github.com/sky22333/hubproxy\n")
|
||||||
|
|
||||||
// 创建HTTP2服务器
|
// 创建HTTP2服务器
|
||||||
|
|||||||
@@ -200,6 +200,13 @@ func (ac *AccessController) checkList(matches, list []string) bool {
|
|||||||
if strings.HasPrefix(fullRepo, item+"/") {
|
if strings.HasPrefix(fullRepo, item+"/") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(item, "*/") {
|
||||||
|
p := item[2:]
|
||||||
|
if p == repoName || (strings.HasSuffix(p, "*") && strings.HasPrefix(repoName, p[:len(p)-1])) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,47 +12,44 @@ import (
|
|||||||
// GitHub URL正则表达式
|
// GitHub URL正则表达式
|
||||||
var githubRegex = regexp.MustCompile(`(?:^|[\s'"(=,\[{;|&<>])https?://(?:github\.com|raw\.githubusercontent\.com|raw\.github\.com|gist\.githubusercontent\.com|gist\.github\.com|api\.github\.com)[^\s'")]*`)
|
var githubRegex = regexp.MustCompile(`(?:^|[\s'"(=,\[{;|&<>])https?://(?:github\.com|raw\.githubusercontent\.com|raw\.github\.com|gist\.githubusercontent\.com|gist\.github\.com|api\.github\.com)[^\s'")]*`)
|
||||||
|
|
||||||
// ProcessSmart Shell脚本智能处理函数
|
// MaxShellSize 限制最大处理大小为 10MB
|
||||||
func ProcessSmart(input io.ReadCloser, isCompressed bool, host string) (io.Reader, int64, error) {
|
const MaxShellSize = 10 * 1024 * 1024
|
||||||
defer input.Close()
|
|
||||||
|
|
||||||
|
// ProcessSmart Shell脚本智能处理函数
|
||||||
|
func ProcessSmart(input io.Reader, isCompressed bool, host string) (io.Reader, int64, error) {
|
||||||
content, err := readShellContent(input, isCompressed)
|
content, err := readShellContent(input, isCompressed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("内容读取失败: %v", err)
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(content) == 0 {
|
if len(content) == 0 {
|
||||||
return strings.NewReader(""), 0, nil
|
return strings.NewReader(""), 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(content) > 10*1024*1024 {
|
if !bytes.Contains(content, []byte("github.com")) && !bytes.Contains(content, []byte("githubusercontent.com")) {
|
||||||
return strings.NewReader(content), int64(len(content)), nil
|
return bytes.NewReader(content), int64(len(content)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(content, "github.com") && !strings.Contains(content, "githubusercontent.com") {
|
processed := processGitHubURLs(string(content), host)
|
||||||
return strings.NewReader(content), int64(len(content)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
processed := processGitHubURLs(content, host)
|
|
||||||
|
|
||||||
return strings.NewReader(processed), int64(len(processed)), nil
|
return strings.NewReader(processed), int64(len(processed)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readShellContent(input io.ReadCloser, isCompressed bool) (string, error) {
|
func readShellContent(input io.Reader, isCompressed bool) ([]byte, error) {
|
||||||
var reader io.Reader = input
|
var reader io.Reader = input
|
||||||
|
|
||||||
if isCompressed {
|
if isCompressed {
|
||||||
peek := make([]byte, 2)
|
peek := make([]byte, 2)
|
||||||
n, err := input.Read(peek)
|
n, err := input.Read(peek)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
return "", fmt.Errorf("读取数据失败: %v", err)
|
return nil, fmt.Errorf("读取数据失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n >= 2 && peek[0] == 0x1f && peek[1] == 0x8b {
|
if n >= 2 && peek[0] == 0x1f && peek[1] == 0x8b {
|
||||||
combinedReader := io.MultiReader(bytes.NewReader(peek[:n]), input)
|
combinedReader := io.MultiReader(bytes.NewReader(peek[:n]), input)
|
||||||
gzReader, err := gzip.NewReader(combinedReader)
|
gzReader, err := gzip.NewReader(combinedReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("gzip解压失败: %v", err)
|
return nil, fmt.Errorf("gzip解压失败: %v", err)
|
||||||
}
|
}
|
||||||
defer gzReader.Close()
|
defer gzReader.Close()
|
||||||
reader = gzReader
|
reader = gzReader
|
||||||
@@ -61,12 +58,19 @@ func readShellContent(input io.ReadCloser, isCompressed bool) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := io.ReadAll(reader)
|
limit := int64(MaxShellSize + 1)
|
||||||
|
limitedReader := io.LimitReader(reader, limit)
|
||||||
|
|
||||||
|
data, err := io.ReadAll(limitedReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("读取内容失败: %v", err)
|
return nil, fmt.Errorf("读取内容失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(data), nil
|
if int64(len(data)) > MaxShellSize {
|
||||||
|
return nil, fmt.Errorf("脚本文件过大,超过 %d MB 限制", MaxShellSize/1024/1024)
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processGitHubURLs(content, host string) string {
|
func processGitHubURLs(content, host string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user