mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 00:01:10 +08:00
Compare commits
16 commits
4.3.0-rc.0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32baca85db | ||
|
|
0135fd2ce0 | ||
|
|
ba33d5743f | ||
|
|
bd9f590b0a | ||
|
|
93cabc900a | ||
|
|
e3f84f4c17 | ||
|
|
4a7ad2ec75 | ||
|
|
efb63927e9 | ||
|
|
a285777217 | ||
|
|
44cc5d5677 | ||
|
|
7972931280 | ||
|
|
74a22be16c | ||
|
|
905a88a86d | ||
|
|
972a37b497 | ||
|
|
5fc6f7ab6f | ||
|
|
d389a61f09 |
16 changed files with 356 additions and 216 deletions
2
.github/workflows/build-dev.yml
vendored
2
.github/workflows/build-dev.yml
vendored
|
|
@ -46,7 +46,7 @@ jobs:
|
||||||
goarch: [amd64, arm64]
|
goarch: [amd64, arm64]
|
||||||
env:
|
env:
|
||||||
OUTPUT_BINARY: ghproxy
|
OUTPUT_BINARY: ghproxy
|
||||||
GO_VERSION: 1.24
|
GO_VERSION: 1.25
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
|
||||||
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
|
|
@ -47,7 +47,7 @@ jobs:
|
||||||
goarch: [amd64, arm64]
|
goarch: [amd64, arm64]
|
||||||
env:
|
env:
|
||||||
OUTPUT_BINARY: ghproxy
|
OUTPUT_BINARY: ghproxy
|
||||||
GO_VERSION: 1.24
|
GO_VERSION: 1.25
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,5 +1,6 @@
|
||||||
demo
|
demo
|
||||||
demo.toml
|
demo.toml
|
||||||
|
demo.wanf
|
||||||
*.log
|
*.log
|
||||||
*.bak
|
*.bak
|
||||||
list.json
|
list.json
|
||||||
|
|
|
||||||
30
CHANGELOG.md
30
CHANGELOG.md
|
|
@ -1,5 +1,35 @@
|
||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
|
4.3.4 - 2025-09-14
|
||||||
|
---
|
||||||
|
- CHANGE: 改进嵌套加速实现, 增强稳定性
|
||||||
|
|
||||||
|
4.3.3 - 2025-09-10
|
||||||
|
---
|
||||||
|
- CHANGE: 增强对[wanf](https://github.com/WJQSERVER/wanf)的支持
|
||||||
|
- CHANGE: 更新包括Touka框架在内的各个依赖版本
|
||||||
|
|
||||||
|
4.3.2 - 2025-08-20
|
||||||
|
---
|
||||||
|
- FIX: 修正`cfg.Pages.StaticDir`为空时的处置
|
||||||
|
|
||||||
|
4.3.1 - 2025-08-13
|
||||||
|
---
|
||||||
|
- CHANGE: 更新至[Go 1.25](https://tip.golang.org/doc/go1.25)
|
||||||
|
|
||||||
|
4.3.0 - 2025-08-11
|
||||||
|
---
|
||||||
|
- CHANGE: 为OCI镜像(Docker)代理带来自动library附加功能
|
||||||
|
- CHANGE(refactor): 改进OCI镜像(Docker)代理路径组成流程
|
||||||
|
- ADD: 新增[WANF](https://github.com/WJQSERVER/wanf)配置文件格式支持
|
||||||
|
|
||||||
|
4.3.0-rc.0 - 2025-08-11
|
||||||
|
---
|
||||||
|
- PRE-RELEASE: v4.3.0-rc.0是v4.3.0发布版本,请勿在生产环境中使用;
|
||||||
|
- CHANGE: 为OCI镜像(Docker)代理带来自动library附加功能
|
||||||
|
- CHANGE(refactor): 改进OCI镜像(Docker)代理路径组成流程
|
||||||
|
- ADD: 新增[WANF](https://github.com/WJQSERVER/wanf)配置文件格式支持
|
||||||
|
|
||||||
4.2.7 - 2025-08-04
|
4.2.7 - 2025-08-04
|
||||||
---
|
---
|
||||||
- CHANGE: 在OCI镜像(docker)代理部分增加特殊处理, 保证可用性 参看[#159](https://github.com/WJQSERVER-STUDIO/ghproxy/issues/159)
|
- CHANGE: 在OCI镜像(docker)代理部分增加特殊处理, 保证可用性 参看[#159](https://github.com/WJQSERVER-STUDIO/ghproxy/issues/159)
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
4.2.4-rc.0
|
4.3.0-rc.0
|
||||||
|
|
@ -36,8 +36,6 @@
|
||||||
|
|
||||||
[相关文章](https://blog.wjqserver.com/categories/my-program/)
|
[相关文章](https://blog.wjqserver.com/categories/my-program/)
|
||||||
|
|
||||||
代理相关推广: [Thordata](https://www.thordata.com/?ls=github&lk=WJQserver),市面上最具性价比的代理服务商,便宜好用,来自全球195个国家城市的6000万IP,轮换住宅/原生ISP/无限量仅从$0.65/GB 起,新用户$1=5GB .联系客户可获得免费测试.
|
|
||||||
|
|
||||||
### 使用示例
|
### 使用示例
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|
|
||||||
2
VERSION
2
VERSION
|
|
@ -1 +1 @@
|
||||||
4.2.7
|
4.3.4
|
||||||
215
config/config.go
215
config/config.go
|
|
@ -1,25 +1,31 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
|
||||||
|
"github.com/WJQSERVER/wanf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config 结构体定义了整个应用程序的配置
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server ServerConfig `toml:"server"`
|
Server ServerConfig `toml:"server" wanf:"server"`
|
||||||
Httpc HttpcConfig `toml:"httpc"`
|
Httpc HttpcConfig `toml:"httpc" wanf:"httpc"`
|
||||||
GitClone GitCloneConfig `toml:"gitclone"`
|
GitClone GitCloneConfig `toml:"gitclone" wanf:"gitclone"`
|
||||||
Shell ShellConfig `toml:"shell"`
|
Shell ShellConfig `toml:"shell" wanf:"shell"`
|
||||||
Pages PagesConfig `toml:"pages"`
|
Pages PagesConfig `toml:"pages" wanf:"pages"`
|
||||||
Log LogConfig `toml:"log"`
|
Log LogConfig `toml:"log" wanf:"log"`
|
||||||
Auth AuthConfig `toml:"auth"`
|
Auth AuthConfig `toml:"auth" wanf:"auth"`
|
||||||
Blacklist BlacklistConfig `toml:"blacklist"`
|
Blacklist BlacklistConfig `toml:"blacklist" wanf:"blacklist"`
|
||||||
Whitelist WhitelistConfig `toml:"whitelist"`
|
Whitelist WhitelistConfig `toml:"whitelist" wanf:"whitelist"`
|
||||||
IPFilter IPFilterConfig `toml:"ipFilter"`
|
IPFilter IPFilterConfig `toml:"ipFilter" wanf:"ipFilter"`
|
||||||
RateLimit RateLimitConfig `toml:"rateLimit"`
|
RateLimit RateLimitConfig `toml:"rateLimit" wanf:"rateLimit"`
|
||||||
Outbound OutboundConfig `toml:"outbound"`
|
Outbound OutboundConfig `toml:"outbound" wanf:"outbound"`
|
||||||
Docker DockerConfig `toml:"docker"`
|
Docker DockerConfig `toml:"docker" wanf:"docker"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -32,13 +38,14 @@ cors = "*" # "*"/"" -> "*" ; "nil" -> "" ;
|
||||||
debug = false
|
debug = false
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// ServerConfig 定义服务器相关的配置
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Port int `toml:"port"`
|
Port int `toml:"port" wanf:"port"`
|
||||||
Host string `toml:"host"`
|
Host string `toml:"host" wanf:"host"`
|
||||||
SizeLimit int `toml:"sizeLimit"`
|
SizeLimit int `toml:"sizeLimit" wanf:"sizeLimit"`
|
||||||
MemLimit int64 `toml:"memLimit"`
|
MemLimit int64 `toml:"memLimit" wanf:"memLimit"`
|
||||||
Cors string `toml:"cors"`
|
Cors string `toml:"cors" wanf:"cors"`
|
||||||
Debug bool `toml:"debug"`
|
Debug bool `toml:"debug" wanf:"debug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -49,12 +56,13 @@ maxIdleConnsPerHost = 60 # only for advanced mode
|
||||||
maxConnsPerHost = 0 # only for advanced mode
|
maxConnsPerHost = 0 # only for advanced mode
|
||||||
useCustomRawHeaders = false
|
useCustomRawHeaders = false
|
||||||
*/
|
*/
|
||||||
|
// HttpcConfig 定义 HTTP 客户端相关的配置
|
||||||
type HttpcConfig struct {
|
type HttpcConfig struct {
|
||||||
Mode string `toml:"mode"`
|
Mode string `toml:"mode" wanf:"mode"`
|
||||||
MaxIdleConns int `toml:"maxIdleConns"`
|
MaxIdleConns int `toml:"maxIdleConns" wanf:"maxIdleConns"`
|
||||||
MaxIdleConnsPerHost int `toml:"maxIdleConnsPerHost"`
|
MaxIdleConnsPerHost int `toml:"maxIdleConnsPerHost" wanf:"maxIdleConnsPerHost"`
|
||||||
MaxConnsPerHost int `toml:"maxConnsPerHost"`
|
MaxConnsPerHost int `toml:"maxConnsPerHost" wanf:"maxConnsPerHost"`
|
||||||
UseCustomRawHeaders bool `toml:"useCustomRawHeaders"`
|
UseCustomRawHeaders bool `toml:"useCustomRawHeaders" wanf:"useCustomRawHeaders"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -64,11 +72,12 @@ smartGitAddr = "http://127.0.0.1:8080"
|
||||||
//cacheTimeout = 10
|
//cacheTimeout = 10
|
||||||
ForceH2C = true
|
ForceH2C = true
|
||||||
*/
|
*/
|
||||||
|
// GitCloneConfig 定义 Git 克隆相关的配置
|
||||||
type GitCloneConfig struct {
|
type GitCloneConfig struct {
|
||||||
Mode string `toml:"mode"`
|
Mode string `toml:"mode" wanf:"mode"`
|
||||||
SmartGitAddr string `toml:"smartGitAddr"`
|
SmartGitAddr string `toml:"smartGitAddr" wanf:"smartGitAddr"`
|
||||||
//CacheTimeout int `toml:"cacheTimeout"`
|
//CacheTimeout int `toml:"cacheTimeout"`
|
||||||
ForceH2C bool `toml:"ForceH2C"`
|
ForceH2C bool `toml:"ForceH2C" wanf:"ForceH2C"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -76,9 +85,10 @@ type GitCloneConfig struct {
|
||||||
editor = true
|
editor = true
|
||||||
rewriteAPI = false
|
rewriteAPI = false
|
||||||
*/
|
*/
|
||||||
|
// ShellConfig 定义 Shell 相关的配置
|
||||||
type ShellConfig struct {
|
type ShellConfig struct {
|
||||||
Editor bool `toml:"editor"`
|
Editor bool `toml:"editor" wanf:"editor"`
|
||||||
RewriteAPI bool `toml:"rewriteAPI"`
|
RewriteAPI bool `toml:"rewriteAPI" wanf:"rewriteAPI"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -87,16 +97,18 @@ mode = "internal" # "internal" or "external"
|
||||||
theme = "bootstrap" # "bootstrap" or "nebula"
|
theme = "bootstrap" # "bootstrap" or "nebula"
|
||||||
staticDir = "/data/www"
|
staticDir = "/data/www"
|
||||||
*/
|
*/
|
||||||
|
// PagesConfig 定义静态页面相关的配置
|
||||||
type PagesConfig struct {
|
type PagesConfig struct {
|
||||||
Mode string `toml:"mode"`
|
Mode string `toml:"mode" wanf:"mode"`
|
||||||
Theme string `toml:"theme"`
|
Theme string `toml:"theme" wanf:"theme"`
|
||||||
StaticDir string `toml:"staticDir"`
|
StaticDir string `toml:"staticDir" wanf:"staticDir"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LogConfig 定义日志相关的配置
|
||||||
type LogConfig struct {
|
type LogConfig struct {
|
||||||
LogFilePath string `toml:"logFilePath"`
|
LogFilePath string `toml:"logFilePath" wanf:"logFilePath"`
|
||||||
MaxLogSize int64 `toml:"maxLogSize"`
|
MaxLogSize int64 `toml:"maxLogSize" wanf:"maxLogSize"`
|
||||||
Level string `toml:"level"`
|
Level string `toml:"level" wanf:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -109,31 +121,35 @@ passThrough = false
|
||||||
ForceAllowApi = false
|
ForceAllowApi = false
|
||||||
ForceAllowApiPassList = false
|
ForceAllowApiPassList = false
|
||||||
*/
|
*/
|
||||||
|
// AuthConfig 定义认证相关的配置
|
||||||
type AuthConfig struct {
|
type AuthConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
Method string `toml:"method"`
|
Method string `toml:"method" wanf:"method"`
|
||||||
Key string `toml:"key"`
|
Key string `toml:"key" wanf:"key"`
|
||||||
Token string `toml:"token"`
|
Token string `toml:"token" wanf:"token"`
|
||||||
PassThrough bool `toml:"passThrough"`
|
PassThrough bool `toml:"passThrough" wanf:"passThrough"`
|
||||||
ForceAllowApi bool `toml:"ForceAllowApi"`
|
ForceAllowApi bool `toml:"ForceAllowApi" wanf:"ForceAllowApi"`
|
||||||
ForceAllowApiPassList bool `toml:"ForceAllowApiPassList"`
|
ForceAllowApiPassList bool `toml:"ForceAllowApiPassList" wanf:"ForceAllowApiPassList"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BlacklistConfig 定义黑名单相关的配置
|
||||||
type BlacklistConfig struct {
|
type BlacklistConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
BlacklistFile string `toml:"blacklistFile"`
|
BlacklistFile string `toml:"blacklistFile" wanf:"blacklistFile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WhitelistConfig 定义白名单相关的配置
|
||||||
type WhitelistConfig struct {
|
type WhitelistConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
WhitelistFile string `toml:"whitelistFile"`
|
WhitelistFile string `toml:"whitelistFile" wanf:"whitelistFile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPFilterConfig 定义 IP 过滤相关的配置
|
||||||
type IPFilterConfig struct {
|
type IPFilterConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
EnableAllowList bool `toml:"enableAllowList"`
|
EnableAllowList bool `toml:"enableAllowList" wanf:"enableAllowList"`
|
||||||
EnableBlockList bool `toml:"enableBlockList"`
|
EnableBlockList bool `toml:"enableBlockList" wanf:"enableBlockList"`
|
||||||
IPFilterFile string `toml:"ipFilterFile"`
|
IPFilterFile string `toml:"ipFilterFile" wanf:"ipFilterFile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -150,19 +166,21 @@ burst = 10
|
||||||
singleBurst = "10mbps"
|
singleBurst = "10mbps"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// RateLimitConfig 定义限速相关的配置
|
||||||
type RateLimitConfig struct {
|
type RateLimitConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
RatePerMinute int `toml:"ratePerMinute"`
|
RatePerMinute int `toml:"ratePerMinute" wanf:"ratePerMinute"`
|
||||||
Burst int `toml:"burst"`
|
Burst int `toml:"burst" wanf:"burst"`
|
||||||
BandwidthLimit BandwidthLimitConfig
|
BandwidthLimit BandwidthLimitConfig `toml:"bandwidthLimit" wanf:"bandwidthLimit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BandwidthLimitConfig 定义带宽限制相关的配置
|
||||||
type BandwidthLimitConfig struct {
|
type BandwidthLimitConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
TotalLimit string `toml:"totalLimit"`
|
TotalLimit string `toml:"totalLimit" wanf:"totalLimit"`
|
||||||
TotalBurst string `toml:"totalBurst"`
|
TotalBurst string `toml:"totalBurst" wanf:"totalBurst"`
|
||||||
SingleLimit string `toml:"singleLimit"`
|
SingleLimit string `toml:"singleLimit" wanf:"singleLimit"`
|
||||||
SingleBurst string `toml:"singleBurst"`
|
SingleBurst string `toml:"singleBurst" wanf:"singleBurst"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -170,9 +188,10 @@ type BandwidthLimitConfig struct {
|
||||||
enabled = false
|
enabled = false
|
||||||
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"
|
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"
|
||||||
*/
|
*/
|
||||||
|
// OutboundConfig 定义出站代理相关的配置
|
||||||
type OutboundConfig struct {
|
type OutboundConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
Url string `toml:"url"`
|
Url string `toml:"url" wanf:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -184,17 +203,19 @@ auth = false
|
||||||
user1 = "testpass"
|
user1 = "testpass"
|
||||||
test = "test123"
|
test = "test123"
|
||||||
*/
|
*/
|
||||||
|
// DockerConfig 定义 Docker 相关的配置
|
||||||
type DockerConfig struct {
|
type DockerConfig struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" wanf:"enabled"`
|
||||||
Target string `toml:"target"`
|
Target string `toml:"target" wanf:"target"`
|
||||||
Auth bool `toml:"auth"`
|
Auth bool `toml:"auth" wanf:"auth"`
|
||||||
Credentials map[string]string `toml:"credentials"`
|
Credentials map[string]string `toml:"credentials" wanf:"credentials"`
|
||||||
AuthPassThrough bool `toml:"authPassThrough"`
|
AuthPassThrough bool `toml:"authPassThrough" wanf:"authPassThrough"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig 从 TOML 配置文件加载配置
|
// LoadConfig 从配置文件加载配置
|
||||||
func LoadConfig(filePath string) (*Config, error) {
|
func LoadConfig(filePath string) (*Config, error) {
|
||||||
if !FileExists(filePath) {
|
exist, filePath2read := FileExists(filePath)
|
||||||
|
if !exist {
|
||||||
// 楔入配置文件
|
// 楔入配置文件
|
||||||
err := DefaultConfig().WriteConfig(filePath)
|
err := DefaultConfig().WriteConfig(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -202,15 +223,22 @@ func LoadConfig(filePath string) (*Config, error) {
|
||||||
}
|
}
|
||||||
return DefaultConfig(), nil
|
return DefaultConfig(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
if _, err := toml.DecodeFile(filePath, &config); err != nil {
|
ext := filepath.Ext(filePath2read)
|
||||||
|
if ext == ".wanf" {
|
||||||
|
if err := wanf.DecodeFile(filePath2read, &config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入配置文件
|
if _, err := toml.DecodeFile(filePath2read, &config); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteConfig 写入配置文件
|
||||||
func (c *Config) WriteConfig(filePath string) error {
|
func (c *Config) WriteConfig(filePath string) error {
|
||||||
file, err := os.Create(filePath)
|
file, err := os.Create(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -218,17 +246,54 @@ func (c *Config) WriteConfig(filePath string) error {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
ext := filepath.Ext(filePath)
|
||||||
|
if ext == ".wanf" {
|
||||||
|
err := wanf.NewStreamEncoder(file).Encode(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
encoder := toml.NewEncoder(file)
|
encoder := toml.NewEncoder(file)
|
||||||
return encoder.Encode(c)
|
return encoder.Encode(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检测文件是否存在
|
// FileExists 检测文件是否存在
|
||||||
func FileExists(filename string) bool {
|
func FileExists(filename string) (bool, string) {
|
||||||
_, err := os.Stat(filename)
|
_, err := os.Stat(filename)
|
||||||
return !os.IsNotExist(err)
|
if err == nil {
|
||||||
|
return true, filename
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// 获取文件名(不包含路径)
|
||||||
|
base := filepath.Base(filename)
|
||||||
|
dir := filepath.Dir(filename)
|
||||||
|
|
||||||
|
// 获取扩展名
|
||||||
|
fileNameBody := strings.TrimSuffix(base, filepath.Ext(base))
|
||||||
|
|
||||||
|
// 重新组合路径, 扩展名改为.wanf, 确认是否存在
|
||||||
|
wanfFilename := filepath.Join(dir, fileNameBody+".wanf")
|
||||||
|
|
||||||
|
_, err = os.Stat(wanfFilename)
|
||||||
|
if err == nil {
|
||||||
|
// .wanf 文件存在
|
||||||
|
fmt.Printf("\n Found .wanf file: %s\n", wanfFilename)
|
||||||
|
return true, wanfFilename
|
||||||
|
} else if os.IsNotExist(err) {
|
||||||
|
// .wanf 文件不存在
|
||||||
|
return false, ""
|
||||||
|
} else {
|
||||||
|
// 其他错误
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false, filename
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 默认配置结构体
|
// DefaultConfig 返回默认配置结构体
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Server: ServerConfig{
|
Server: ServerConfig{
|
||||||
|
|
|
||||||
11
go.mod
11
go.mod
|
|
@ -1,25 +1,26 @@
|
||||||
module ghproxy
|
module ghproxy
|
||||||
|
|
||||||
go 1.24.5
|
go 1.25.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.5.0
|
github.com/BurntSushi/toml v1.5.0
|
||||||
github.com/WJQSERVER-STUDIO/httpc v0.8.2
|
github.com/WJQSERVER-STUDIO/httpc v0.8.2
|
||||||
golang.org/x/net v0.42.0
|
golang.org/x/net v0.44.0
|
||||||
golang.org/x/time v0.12.0
|
golang.org/x/time v0.13.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2
|
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2
|
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2
|
||||||
|
github.com/WJQSERVER/wanf v0.0.0-20250810023226-e51d9d0737ee
|
||||||
github.com/fenthope/bauth v0.0.1
|
github.com/fenthope/bauth v0.0.1
|
||||||
github.com/fenthope/ikumi v0.0.2
|
github.com/fenthope/ikumi v0.0.2
|
||||||
github.com/fenthope/ipfilter v0.0.1
|
github.com/fenthope/ipfilter v0.0.1
|
||||||
github.com/fenthope/reco v0.0.4
|
github.com/fenthope/reco v0.0.4
|
||||||
github.com/fenthope/record v0.0.4
|
github.com/fenthope/record v0.0.4
|
||||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2
|
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7
|
github.com/hashicorp/golang-lru/v2 v2.0.7
|
||||||
github.com/infinite-iroha/touka v0.3.6
|
github.com/infinite-iroha/touka v0.3.7
|
||||||
github.com/wjqserver/modembed v0.0.1
|
github.com/wjqserver/modembed v0.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
18
go.sum
18
go.sum
|
|
@ -6,6 +6,8 @@ github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2 h1:8bBkKk6E2Zr+I5szL7gyc
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2/go.mod h1:yPX8xuZH+py7eLJwOYj3VVI/4/Yuy5+x8Mhq8qezcPg=
|
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2/go.mod h1:yPX8xuZH+py7eLJwOYj3VVI/4/Yuy5+x8Mhq8qezcPg=
|
||||||
github.com/WJQSERVER-STUDIO/httpc v0.8.2 h1:PFPLodV0QAfGEP6915J57vIqoKu9cGuuiXG/7C9TNUk=
|
github.com/WJQSERVER-STUDIO/httpc v0.8.2 h1:PFPLodV0QAfGEP6915J57vIqoKu9cGuuiXG/7C9TNUk=
|
||||||
github.com/WJQSERVER-STUDIO/httpc v0.8.2/go.mod h1:8WhHVRO+olDFBSvL5PC/bdMkb6U3vRdPJ4p4pnguV5Y=
|
github.com/WJQSERVER-STUDIO/httpc v0.8.2/go.mod h1:8WhHVRO+olDFBSvL5PC/bdMkb6U3vRdPJ4p4pnguV5Y=
|
||||||
|
github.com/WJQSERVER/wanf v0.0.0-20250810023226-e51d9d0737ee h1:tJ31DNBn6UhWkk8fiikAQWqULODM+yBcGAEar1tzdZc=
|
||||||
|
github.com/WJQSERVER/wanf v0.0.0-20250810023226-e51d9d0737ee/go.mod h1:q2Pyg+G+s1acMWxrbI4CwS/Yk76/BzLREEdZ8iFwUNE=
|
||||||
github.com/fenthope/bauth v0.0.1 h1:+4UIQshGx3mYD4L3f2S4MLZOi5PWU7fU5GK3wsZvwzE=
|
github.com/fenthope/bauth v0.0.1 h1:+4UIQshGx3mYD4L3f2S4MLZOi5PWU7fU5GK3wsZvwzE=
|
||||||
github.com/fenthope/bauth v0.0.1/go.mod h1:1fveTpgfR1p+WXQ8MXm9BfBCeNYi55j23jxCOGOvBSA=
|
github.com/fenthope/bauth v0.0.1/go.mod h1:1fveTpgfR1p+WXQ8MXm9BfBCeNYi55j23jxCOGOvBSA=
|
||||||
github.com/fenthope/ikumi v0.0.2 h1:5oaSTf/Msp7M2O3o/X20omKWEQbFhX4KV0CVF21oCdk=
|
github.com/fenthope/ikumi v0.0.2 h1:5oaSTf/Msp7M2O3o/X20omKWEQbFhX4KV0CVF21oCdk=
|
||||||
|
|
@ -16,17 +18,17 @@ github.com/fenthope/reco v0.0.4 h1:yo2g3aWwdoMpaZWZX4SdZOW7mCK82RQIU/YI8ZUQThM=
|
||||||
github.com/fenthope/reco v0.0.4/go.mod h1:eMyS8HpdMVdJ/2WJt6Cvt8P1EH9Igzj5lSJrgc+0jeg=
|
github.com/fenthope/reco v0.0.4/go.mod h1:eMyS8HpdMVdJ/2WJt6Cvt8P1EH9Igzj5lSJrgc+0jeg=
|
||||||
github.com/fenthope/record v0.0.4 h1:/1JHNCxiXGLL/qCh4LEGaAvhj4CcKsb6siTxjLmjdO4=
|
github.com/fenthope/record v0.0.4 h1:/1JHNCxiXGLL/qCh4LEGaAvhj4CcKsb6siTxjLmjdO4=
|
||||||
github.com/fenthope/record v0.0.4/go.mod h1:G0a6KCiCDyX2SsC3nfzSN651fJKxH482AyJvzlnvAJU=
|
github.com/fenthope/record v0.0.4/go.mod h1:G0a6KCiCDyX2SsC3nfzSN651fJKxH482AyJvzlnvAJU=
|
||||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 h1:iizUGZ9pEquQS5jTGkh4AqeeHCMbfbjeb0zMt0aEFzs=
|
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b h1:6Q4zRHXS/YLOl9Ng1b1OOOBWMidAQZR3Gel0UKPC/KU=
|
||||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
|
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
github.com/infinite-iroha/touka v0.3.6 h1:SkpM/VFGCWOFQP3RRuoWdX/Q4zafPngG1VMwkrLwtkw=
|
github.com/infinite-iroha/touka v0.3.7 h1:bIIZW5Weh7lVpyOWh4FmyR9UOfb5FOt+cR9yQ30FJLA=
|
||||||
github.com/infinite-iroha/touka v0.3.6/go.mod h1:XW7a3fpLAjJfylSmdNuDQ8wGKkKmLVi9V/89sT1d7uw=
|
github.com/infinite-iroha/touka v0.3.7/go.mod h1:uwkF1gTrNEgQ4P/Gwtk6WLbERehq3lzB8x1FMedyrfE=
|
||||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs=
|
github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs=
|
||||||
github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ=
|
github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ=
|
||||||
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
|
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
|
||||||
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
|
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||||
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
|
golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI=
|
||||||
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||||
|
|
|
||||||
31
main.go
31
main.go
|
|
@ -234,8 +234,18 @@ func setupPages(cfg *config.Config, r *touka.Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "external":
|
case "external":
|
||||||
r.SetUnMatchFS(http.Dir(cfg.Pages.StaticDir))
|
if cfg.Pages.StaticDir == "" {
|
||||||
|
logger.Errorf("Pages Mode is 'external' but StaticDir is empty. Using embedded pages instead.")
|
||||||
|
err := setInternalRoute(cfg, r)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Failed to load embedded pages: %s", err)
|
||||||
|
fmt.Printf("Failed to load embedded pages: %s", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
extPageFS := os.DirFS(cfg.Pages.StaticDir)
|
||||||
|
r.SetUnMatchFS(http.FS(extPageFS))
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// 处理无效的Pages Mode
|
// 处理无效的Pages Mode
|
||||||
logger.Warnf("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode)
|
logger.Warnf("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode)
|
||||||
|
|
@ -474,20 +484,11 @@ func main() {
|
||||||
proxy.RoutingHandler(cfg)(c)
|
proxy.RoutingHandler(cfg)(c)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.GET("/v2/",
|
r.ANY("/v2/*path",
|
||||||
r.UseIf(cfg.Docker.Auth, func() touka.HandlerFunc {
|
r.UseIf(cfg.Docker.Auth, func() touka.HandlerFunc {
|
||||||
return bauth.BasicAuthForStatic(cfg.Docker.Credentials, "GHProxy Docker Proxy")
|
return bauth.BasicAuthForStatic(cfg.Docker.Credentials, "GHProxy Docker Proxy")
|
||||||
}),
|
}),
|
||||||
func(c *touka.Context) {
|
proxy.OciWithImageRouting(cfg),
|
||||||
emptyJSON := "{}"
|
|
||||||
c.Header("Content-Type", "application/json")
|
|
||||||
c.Header("Content-Length", fmt.Sprint(len(emptyJSON)))
|
|
||||||
|
|
||||||
c.Header("Docker-Distribution-API-Version", "registry/2.0")
|
|
||||||
|
|
||||||
c.Status(200)
|
|
||||||
c.Writer.Write([]byte(emptyJSON))
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
r.GET("/v2", func(c *touka.Context) {
|
r.GET("/v2", func(c *touka.Context) {
|
||||||
|
|
@ -495,10 +496,6 @@ func main() {
|
||||||
c.Redirect(http.StatusMovedPermanently, "/v2/")
|
c.Redirect(http.StatusMovedPermanently, "/v2/")
|
||||||
})
|
})
|
||||||
|
|
||||||
r.ANY("/v2/:target/:user/:repo/*filepath", func(c *touka.Context) {
|
|
||||||
proxy.GhcrWithImageRouting(cfg)(c)
|
|
||||||
})
|
|
||||||
|
|
||||||
r.NoRoute(func(c *touka.Context) {
|
r.NoRoute(func(c *touka.Context) {
|
||||||
proxy.NoRouteHandler(cfg)(c)
|
proxy.NoRouteHandler(cfg)(c)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -127,18 +127,14 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
|
||||||
defer bodyReader.Close()
|
defer bodyReader.Close()
|
||||||
|
|
||||||
if MatcherShell(u) && matchString(matcher) && cfg.Shell.Editor {
|
if MatcherShell(u) && matchString(matcher) && cfg.Shell.Editor {
|
||||||
// 判断body是不是gzip
|
|
||||||
var compress string
|
|
||||||
if resp.Header.Get("Content-Encoding") == "gzip" {
|
|
||||||
compress = "gzip"
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Debugf("Use Shell Editor: %s %s %s %s %s", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto)
|
c.Debugf("Use Shell Editor: %s %s %s %s %s", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto)
|
||||||
c.Header("Content-Length", "")
|
c.DelHeader("Content-Length")
|
||||||
|
c.DelHeader("Content-Encoding")
|
||||||
|
|
||||||
var reader io.Reader
|
var reader io.Reader
|
||||||
|
|
||||||
reader, _, err = processLinks(bodyReader, compress, c.Request.Host, cfg, c)
|
reader, _, err = processLinks(bodyReader, c.Request.Host, cfg, c)
|
||||||
c.WriteStream(reader)
|
c.WriteStream(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Errorf("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto, err)
|
c.Errorf("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto, err)
|
||||||
|
|
@ -146,7 +142,6 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if contentLength != "" {
|
if contentLength != "" {
|
||||||
c.SetHeader("Content-Length", contentLength)
|
c.SetHeader("Content-Length", contentLength)
|
||||||
c.WriteStream(bodyReader)
|
c.WriteStream(bodyReader)
|
||||||
|
|
|
||||||
207
proxy/docker.go
207
proxy/docker.go
|
|
@ -40,90 +40,167 @@ func InitWeakCache() *weakcache.Cache[string] {
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// GhcrWithImageRouting 处理带有镜像路由的请求, 根据目标路由到不同的Docker注册表
|
var (
|
||||||
func GhcrWithImageRouting(cfg *config.Config) touka.HandlerFunc {
|
authEndpoint = "/"
|
||||||
return func(c *touka.Context) {
|
passTypeMap = map[string]struct{}{
|
||||||
// 从 main.go 中固定的路由 "/v2/:target/:user/:repo/*filepath" 获取参数
|
"manifests": {},
|
||||||
reqTarget := c.Param("target")
|
"blobs": {},
|
||||||
reqImageUser := c.Param("user")
|
"tags": {},
|
||||||
reqImageName := c.Param("repo")
|
"index": {},
|
||||||
reqFilePath := c.Param("filepath")
|
}
|
||||||
|
)
|
||||||
var upstreamTarget string
|
|
||||||
var requestPath string
|
// 处理路径各种情况
|
||||||
var imageNameForAuth string
|
func OciWithImageRouting(cfg *config.Config) touka.HandlerFunc {
|
||||||
|
return func(c *touka.Context) {
|
||||||
// 关键逻辑: 判断 reqTarget 是真实主机名还是镜像名的一部分
|
if !cfg.Docker.Enabled {
|
||||||
// 依据: 真实主机名/IP通常包含'.'或':'
|
ErrorPage(c, NewErrorWithStatusLookup(403, "Docker proxy is not enabled"))
|
||||||
if strings.Contains(reqTarget, ".") || strings.Contains(reqTarget, ":") {
|
return
|
||||||
// 情况 A: reqTarget 是一个显式指定的主机名 (例如 "ghcr.io", "my-registry.com", "127.0.0.1:5000")
|
}
|
||||||
c.Debugf("Request target '%s' identified as an explicit hostname.", reqTarget)
|
var (
|
||||||
|
p1 string
|
||||||
// https://github.com/WJQSERVER-STUDIO/ghproxy/issues/159
|
p2 string
|
||||||
if reqTarget == "docker.io" {
|
p3 string
|
||||||
upstreamTarget = dockerhubTarget
|
p4 string
|
||||||
} else {
|
target string
|
||||||
upstreamTarget = reqTarget
|
user string
|
||||||
|
repo string
|
||||||
|
extpath string
|
||||||
|
p1IsTarget bool
|
||||||
|
ignorep3 bool
|
||||||
|
imageNameForAuth string
|
||||||
|
finalreqUrl string
|
||||||
|
iInfo *imageInfo
|
||||||
|
)
|
||||||
|
ociPath := c.Param("path")
|
||||||
|
if ociPath == authEndpoint {
|
||||||
|
emptyJSON := "{}"
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
|
c.Header("Content-Length", fmt.Sprint(len(emptyJSON)))
|
||||||
|
|
||||||
|
c.Header("Docker-Distribution-API-Version", "registry/2.0")
|
||||||
|
|
||||||
|
c.Status(200)
|
||||||
|
c.Writer.Write([]byte(emptyJSON))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据/分割 /:target/:user/:repo/*ext
|
||||||
|
ociPath = ociPath[1:]
|
||||||
|
i := strings.IndexByte(ociPath, '/')
|
||||||
|
if i <= 0 {
|
||||||
|
ErrorPage(c, NewErrorWithStatusLookup(404, "Not Found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p1 = ociPath[:i]
|
||||||
|
|
||||||
|
// 开始判断p1是否为target
|
||||||
|
if strings.Contains(p1, ".") || strings.Contains(p1, ":") {
|
||||||
|
p1IsTarget = true
|
||||||
|
if p1 == "docker.io" {
|
||||||
|
target = dockerhubTarget
|
||||||
|
} else {
|
||||||
|
target = p1
|
||||||
}
|
}
|
||||||
// 上游请求的路径是主机名之后的部分
|
|
||||||
requestPath = fmt.Sprintf("%s/%s%s", reqImageUser, reqImageName, reqFilePath)
|
|
||||||
// 用于认证的镜像名是 user/repo
|
|
||||||
imageNameForAuth = fmt.Sprintf("%s/%s", reqImageUser, reqImageName)
|
|
||||||
} else {
|
} else {
|
||||||
// 情况 B: reqTarget 是镜像名的一部分 (例如 "wjqserver", "library")
|
|
||||||
c.Debugf("Request target '%s' identified as part of an image name. Using default registry.", reqTarget)
|
|
||||||
// 使用配置文件中的默认目标
|
|
||||||
switch cfg.Docker.Target {
|
switch cfg.Docker.Target {
|
||||||
case "ghcr":
|
case "ghcr":
|
||||||
upstreamTarget = ghcrTarget
|
target = ghcrTarget
|
||||||
case "dockerhub":
|
case "dockerhub":
|
||||||
upstreamTarget = dockerhubTarget
|
target = dockerhubTarget
|
||||||
case "":
|
case "":
|
||||||
ErrorPage(c, NewErrorWithStatusLookup(500, "Default Docker Target is not configured in config file"))
|
ErrorPage(c, NewErrorWithStatusLookup(500, "Default Docker Target is not configured in config file"))
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
upstreamTarget = cfg.Docker.Target
|
target = cfg.Docker.Target
|
||||||
}
|
}
|
||||||
// 必须将路由错误分割的所有部分重新组合成完整的镜像路径
|
|
||||||
requestPath = fmt.Sprintf("%s/%s/%s%s", reqTarget, reqImageUser, reqImageName, reqFilePath)
|
|
||||||
// 用于认证的镜像名是 target/user (例如 "wjqserver/ghproxy", "library/ubuntu")
|
|
||||||
imageNameForAuth = fmt.Sprintf("%s/%s", reqTarget, reqImageUser)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理路径, 防止出现 "//"
|
ociPath = ociPath[i+1:]
|
||||||
requestPath = strings.TrimPrefix(requestPath, "/")
|
i = strings.IndexByte(ociPath, '/')
|
||||||
|
if i <= 0 {
|
||||||
|
ErrorPage(c, NewErrorWithStatusLookup(404, "Not Found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p2 = ociPath[:i]
|
||||||
|
ociPath = ociPath[i+1:]
|
||||||
|
|
||||||
// 为认证和缓存准备镜像信息
|
// 若p2和passTypeMap匹配
|
||||||
image := &imageInfo{
|
if !p1IsTarget {
|
||||||
|
if _, ok := passTypeMap[p2]; ok {
|
||||||
|
ignorep3 = true
|
||||||
|
switch cfg.Docker.Target {
|
||||||
|
case "ghcr":
|
||||||
|
target = ghcrTarget
|
||||||
|
case "dockerhub":
|
||||||
|
target = dockerhubTarget
|
||||||
|
case "":
|
||||||
|
ErrorPage(c, NewErrorWithStatusLookup(500, "Default Docker Target is not configured in config file"))
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
target = cfg.Docker.Target
|
||||||
|
}
|
||||||
|
user = "library"
|
||||||
|
repo = p1
|
||||||
|
extpath = "/" + p2 + "/" + ociPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ignorep3 {
|
||||||
|
i = strings.IndexByte(ociPath, '/')
|
||||||
|
if i <= 0 {
|
||||||
|
ErrorPage(c, NewErrorWithStatusLookup(404, "Not Found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p3 = ociPath[:i]
|
||||||
|
|
||||||
|
ociPath = ociPath[i+1:]
|
||||||
|
p4 = ociPath
|
||||||
|
|
||||||
|
if p1IsTarget {
|
||||||
|
if _, ok := passTypeMap[p3]; ok {
|
||||||
|
user = "library"
|
||||||
|
repo = p2
|
||||||
|
extpath = "/" + p3 + "/" + p4
|
||||||
|
} else {
|
||||||
|
user = p2
|
||||||
|
repo = p3
|
||||||
|
extpath = "/" + p4
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch cfg.Docker.Target {
|
||||||
|
case "ghcr":
|
||||||
|
target = ghcrTarget
|
||||||
|
case "dockerhub":
|
||||||
|
target = dockerhubTarget
|
||||||
|
case "":
|
||||||
|
ErrorPage(c, NewErrorWithStatusLookup(500, "Default Docker Target is not configured in config file"))
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
target = cfg.Docker.Target
|
||||||
|
}
|
||||||
|
user = p1
|
||||||
|
repo = p2
|
||||||
|
extpath = "/" + p3 + "/" + p4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
imageNameForAuth = user + "/" + repo
|
||||||
|
finalreqUrl = "https://" + target + "/v2/" + imageNameForAuth + extpath
|
||||||
|
if query := c.GetReqQueryString(); query != "" {
|
||||||
|
finalreqUrl += "?" + query
|
||||||
|
}
|
||||||
|
|
||||||
|
iInfo = &imageInfo{
|
||||||
|
User: user,
|
||||||
|
Repo: repo,
|
||||||
Image: imageNameForAuth,
|
Image: imageNameForAuth,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用 GhcrToTarget 处理实际的代理请求
|
GhcrRequest(c.Request.Context(), c, finalreqUrl, iInfo, cfg, target)
|
||||||
GhcrToTarget(c, cfg, upstreamTarget, requestPath, image)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GhcrToTarget 根据配置和目标信息将请求代理到上游Docker注册表
|
|
||||||
func GhcrToTarget(c *touka.Context, cfg *config.Config, target string, path string, image *imageInfo) {
|
|
||||||
// 检查Docker代理是否启用
|
|
||||||
if !cfg.Docker.Enabled {
|
|
||||||
ErrorPage(c, NewErrorWithStatusLookup(403, "Docker is not Allowed"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var ctx = c.Request.Context()
|
|
||||||
|
|
||||||
// 构造目标URL. 这里的target和path都是由GhcrWithImageRouting正确解析得来的.
|
|
||||||
destUrl := "https://" + target + "/v2/" + path
|
|
||||||
if query := c.GetReqQueryString(); query != "" {
|
|
||||||
destUrl += "?" + query
|
|
||||||
}
|
|
||||||
c.Debugf("Proxying to target '%s' with path '%s'. Final URL: %s", target, path, destUrl)
|
|
||||||
|
|
||||||
// 执行实际的代理请求
|
|
||||||
GhcrRequest(ctx, c, destUrl, image, cfg, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GhcrRequest 执行对Docker注册表的HTTP请求, 处理认证和重定向
|
// GhcrRequest 执行对Docker注册表的HTTP请求, 处理认证和重定向
|
||||||
func GhcrRequest(ctx context.Context, c *touka.Context, u string, image *imageInfo, cfg *config.Config, target string) {
|
func GhcrRequest(ctx context.Context, c *touka.Context, u string, image *imageInfo, cfg *config.Config, target string) {
|
||||||
var (
|
var (
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ const (
|
||||||
gistPrefix = "https://gist.github.com/"
|
gistPrefix = "https://gist.github.com/"
|
||||||
gistContentPrefix = "https://gist.githubusercontent.com/"
|
gistContentPrefix = "https://gist.githubusercontent.com/"
|
||||||
apiPrefix = "https://api.github.com/"
|
apiPrefix = "https://api.github.com/"
|
||||||
|
ociv2Prefix = "https://v2/"
|
||||||
releasesDownloadSnippet = "releases/download/"
|
releasesDownloadSnippet = "releases/download/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -36,9 +37,11 @@ func init() {
|
||||||
|
|
||||||
// Matcher 从原始URL路径中高效地解析并匹配代理规则.
|
// Matcher 从原始URL路径中高效地解析并匹配代理规则.
|
||||||
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
||||||
|
/*
|
||||||
if len(rawPath) < 18 {
|
if len(rawPath) < 18 {
|
||||||
return "", "", "", NewErrorWithStatusLookup(404, "path too short")
|
return "", "", "", NewErrorWithStatusLookup(404, "path too short")
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// 匹配 "https://github.com/"
|
// 匹配 "https://github.com/"
|
||||||
if strings.HasPrefix(rawPath, githubPrefix) {
|
if strings.HasPrefix(rawPath, githubPrefix) {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"compress/gzip"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -66,7 +65,7 @@ func modifyURL(url string, host string, cfg *config.Config) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// processLinks 处理链接,返回包含处理后数据的 io.Reader
|
// processLinks 处理链接,返回包含处理后数据的 io.Reader
|
||||||
func processLinks(input io.ReadCloser, compress string, host string, cfg *config.Config, c *touka.Context) (readerOut io.Reader, written int64, err error) {
|
func processLinks(input io.ReadCloser, host string, cfg *config.Config, c *touka.Context) (readerOut io.Reader, written int64, err error) {
|
||||||
pipeReader, pipeWriter := io.Pipe() // 创建 io.Pipe
|
pipeReader, pipeWriter := io.Pipe() // 创建 io.Pipe
|
||||||
readerOut = pipeReader
|
readerOut = pipeReader
|
||||||
|
|
||||||
|
|
@ -97,43 +96,14 @@ func processLinks(input io.ReadCloser, compress string, host string, cfg *config
|
||||||
|
|
||||||
var bufReader *bufio.Reader
|
var bufReader *bufio.Reader
|
||||||
|
|
||||||
if compress == "gzip" {
|
|
||||||
// 解压gzip
|
|
||||||
gzipReader, gzipErr := gzip.NewReader(input)
|
|
||||||
if gzipErr != nil {
|
|
||||||
err = fmt.Errorf("gzip解压错误: %v", gzipErr)
|
|
||||||
return // Goroutine 中使用 return 返回错误
|
|
||||||
}
|
|
||||||
defer gzipReader.Close()
|
|
||||||
bufReader = bufio.NewReader(gzipReader)
|
|
||||||
} else {
|
|
||||||
bufReader = bufio.NewReader(input)
|
bufReader = bufio.NewReader(input)
|
||||||
}
|
|
||||||
|
|
||||||
var bufWriter *bufio.Writer
|
var bufWriter *bufio.Writer
|
||||||
var gzipWriter *gzip.Writer
|
|
||||||
|
|
||||||
// 根据是否gzip确定 writer 的创建
|
|
||||||
if compress == "gzip" {
|
|
||||||
gzipWriter = gzip.NewWriter(pipeWriter) // 使用 pipeWriter
|
|
||||||
bufWriter = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小
|
|
||||||
} else {
|
|
||||||
bufWriter = bufio.NewWriterSize(pipeWriter, 4096) // 使用 pipeWriter
|
bufWriter = bufio.NewWriterSize(pipeWriter, 4096) // 使用 pipeWriter
|
||||||
}
|
|
||||||
|
|
||||||
//确保writer关闭
|
//确保writer关闭
|
||||||
defer func() {
|
defer func() {
|
||||||
var closeErr error // 局部变量,用于保存defer中可能发生的错误
|
|
||||||
|
|
||||||
if gzipWriter != nil {
|
|
||||||
if closeErr = gzipWriter.Close(); closeErr != nil {
|
|
||||||
c.Errorf("gzipWriter close failed %v", closeErr)
|
|
||||||
// 如果已经存在错误,则保留。否则,记录此错误。
|
|
||||||
if err == nil {
|
|
||||||
err = closeErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if flushErr := bufWriter.Flush(); flushErr != nil {
|
if flushErr := bufWriter.Flush(); flushErr != nil {
|
||||||
c.Errorf("writer flush failed %v", flushErr)
|
c.Errorf("writer flush failed %v", flushErr)
|
||||||
// 如果已经存在错误,则保留。否则,记录此错误。
|
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ var (
|
||||||
"CDN-Loop": {},
|
"CDN-Loop": {},
|
||||||
"Upgrade": {},
|
"Upgrade": {},
|
||||||
"Connection": {},
|
"Connection": {},
|
||||||
|
"Accept-Encoding": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
cloneHeadersToRemove = map[string]struct{}{
|
cloneHeadersToRemove = map[string]struct{}{
|
||||||
|
|
@ -43,7 +44,7 @@ var (
|
||||||
var (
|
var (
|
||||||
defaultHeaders = map[string]string{
|
defaultHeaders = map[string]string{
|
||||||
"Accept": "*/*",
|
"Accept": "*/*",
|
||||||
"Accept-Encoding": "gzip",
|
"Accept-Encoding": "",
|
||||||
"Transfer-Encoding": "chunked",
|
"Transfer-Encoding": "chunked",
|
||||||
"User-Agent": "GHProxy/1.0",
|
"User-Agent": "GHProxy/1.0",
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue