Compare commits

...

52 Commits

Author SHA1 Message Date
里見 灯花
4e469a4896 Merge pull request #138 from WJQSERVER-STUDIO/dev
4.1.5
2025-07-03 11:42:00 +08:00
wjqserver
00513f689d fix changelog 2025-07-03 11:41:40 +08:00
wjqserver
ad4d55bc39 4.1.5 2025-07-03 11:37:21 +08:00
WJQSERVER
7adab36c68 Merge pull request #136 from WJQSERVER-STUDIO/dev
4.1.4
2025-06-30 15:37:46 +08:00
wjqserver
4ea5a875fe 4.1.4 2025-06-30 15:27:17 +08:00
wjqserver
904a800eea use touka uni httpc 2025-06-30 15:26:48 +08:00
WJQSERVER
f0902c1da3 Merge pull request #135 from WJQSERVER-STUDIO/dev
4.1.3
2025-06-25 17:59:16 +08:00
wjqserver
781e175721 4.1.3 2025-06-25 17:53:35 +08:00
wjqserver
79692965a6 refix changelog 2025-06-18 17:22:58 +08:00
wjqserver
ff5f77edc9 fix changelog 2025-06-18 17:22:41 +08:00
WJQSERVER
402308d620 Merge pull request #134 from WJQSERVER-STUDIO/dev 2025-06-18 17:21:17 +08:00
wjqserver
d4237f0463 4.1.2-rc.0 2025-06-18 10:35:08 +08:00
wjqserver
eb3bf16e06 update design theme 2025-06-18 10:35:01 +08:00
WJQSERVER
b701a89b98 Merge pull request #133 from WJQSERVER-STUDIO/dev
4.1.1
2025-06-18 09:06:11 +08:00
wjqserver
933aeee518 4.1.1 2025-06-18 09:05:45 +08:00
WJQSERVER
3812b029cf Merge pull request #131 from WJQSERVER-STUDIO/dev
4.1.0
2025-06-17 17:05:42 +08:00
wjqserver
e629b5db47 4.1.0 2025-06-17 17:04:34 +08:00
wjqserver
e5bc171f25 4.1.0-rc.0 2025-06-17 16:43:51 +08:00
wjqserver
6eae638256 remove dev codes 2025-06-17 15:07:31 +08:00
wjqserver
3e03f47ef7 update deps rebuild 4.1.0-beta.0 2025-06-17 14:47:23 +08:00
wjqserver
eb113b4191 add docker login basic auth support 2025-06-17 14:45:14 +08:00
里見 灯花
8ea741aec8 Merge pull request #130 from WJQSERVER-STUDIO/dev
remove HertZ, Touka ready (v4)
2025-06-16 09:44:31 +08:00
WJQSERVER
37488db087 Merge branch 'main' into dev 2025-06-16 09:27:12 +08:00
wjqserver
5b253998ce update changelog 2025-06-16 09:26:05 +08:00
wjqserver
92432121e5 4.0.0-rc.0 & 4.0.0 ready 2025-06-16 09:19:34 +08:00
WJQSERVER
37fd1383ee Merge pull request #129 from WJQSERVER-STUDIO/v4
4.0.0-beta.0
2025-06-16 08:59:33 +08:00
WJQSERVER
35b70fadca Merge pull request #127 from WJQSERVER-STUDIO/dev
3.5.6
2025-06-15 16:42:58 +08:00
WJQSERVER
c13dd9082b Merge pull request #124 from WJQSERVER-STUDIO/dev
3.5.5
2025-06-14 22:17:46 +08:00
WJQSERVER
96c30889f4 Merge pull request #123 from WJQSERVER-STUDIO/dev
3.5.4
2025-06-14 12:50:36 +08:00
WJQSERVER
bf75e62eb2 Merge pull request #121 from WJQSERVER-STUDIO/dev
3.5.3
2025-06-13 15:30:54 +08:00
WJQSERVER
a1991367c3 Merge pull request #120 from WJQSERVER-STUDIO/dev
3.5.2
2025-06-11 11:55:13 +08:00
WJQSERVER
65769975b6 Merge pull request #119 from WJQSERVER-STUDIO/dev
optimize matcher performance
2025-06-09 23:32:08 +08:00
WJQSERVER
b29940df21 Merge pull request #118 from WJQSERVER-STUDIO/dev
3.5.0
2025-06-05 22:55:42 +08:00
WJQSERVER
1ba100c28d Merge pull request #116 from WJQSERVER-STUDIO/dev
3.4.3
2025-06-05 17:30:01 +08:00
WJQSERVER
75d909ef16 Merge pull request #113 from WJQSERVER-STUDIO/dev
revert github.com/nyaruka/phonenumbers to v1.6.1
2025-06-03 16:48:03 +08:00
WJQSERVER
362ad96fbe Merge pull request #111 from WJQSERVER-STUDIO/dev
update docs link
2025-05-30 17:32:00 +08:00
WJQSERVER
e40e1aadee Merge 3.4.1 #110 2025-05-29 15:26:44 +08:00
WJQSERVER
d2b2d823b8 3.4.0
Add support for multi-target docker image(oci) proxy (3.4.0)
Add Hub theme
2025-05-21 16:24:08 +08:00
WJQSERVER
f3eb92ea51 Merge pull request #107 from WJQSERVER-STUDIO/dev
3.3.3
2025-05-20 10:10:19 +08:00
WJQSERVER
0f437dc891 Merge pull request #106 from WJQSERVER-STUDIO/dev
3.3.2
2025-05-18 07:05:19 +08:00
WJQSERVER
5c25bc012f Merge pull request #105 from WJQSERVER-STUDIO/dev
3.3.1
2025-05-16 19:54:20 +08:00
WJQSERVER
aa95daf8c0 Merge pull request #103 from WJQSERVER-STUDIO/dev
3.3.0
2025-05-15 18:46:29 +08:00
WJQSERVER
e32479b287 Merge pull request #102 from WJQSERVER-STUDIO/dev
3.2.4
2025-05-13 14:24:32 +08:00
WJQSERVER
92c4c62b46 Merge pull request #100 from WJQSERVER-STUDIO/dev
3.2.3
2025-05-07 19:09:18 +08:00
WJQSERVER
ef3b1bf1f0 Merge pull request #99 from WJQSERVER-STUDIO/dev
3.2.2
2025-04-29 22:29:13 +08:00
WJQSERVER
7eb312243c Merge pull request #97 from WJQSERVER-STUDIO/dev
3.2.1
2025-04-29 07:44:45 +08:00
里見 灯花
1498aaed14 Merge pull request #95 from WJQSERVER-STUDIO/dev
3.2.0
2025-04-27 17:41:32 +08:00
WJQSERVER
9977eb1437 3.1.0
- CHANGE: 对标准url使用`HertZ`路由匹配器, 而不是自制匹配器, 以提升效率
- CHANGE: 使用`bodystream`进行req方向的body复制, 而不是使用额外的`buffer reader`
- CHANGE: 使用`HertZ`的`requestContext`传递matcher参数, 而不是`25w30a`中的ctx
- CHANGE: 改进`rate`模块, 避免并发竞争问题
- CHANGE: 将大部分状态码返回改为新的`html/tmpl`方式处理
- CHANGE: 修改部分log等级
- FIX:    修正默认配置的填充错误
- CHANGE: 使用go `html/tmpl`处理状态码页面, 同时实现错误信息显示
- CHANGE: 改进handle, 复用共同部分
- CHANGE: 细化url匹配的返回码处理
2025-04-24 18:46:28 +08:00
WJQSERVER
c656aa41ca Merge pull request #93 from WJQSERVER-STUDIO/dev
3.0.3
2025-04-19 21:26:39 +08:00
WJQSERVER
060453f070 Merge pull request #88 from WJQSERVER-STUDIO/dev
3.0.2
2025-04-15 13:26:46 +08:00
里見 灯花
152fb8aa71 Merge pull request #84 from WJQSERVER-STUDIO/dev
3.0.1
2025-04-08 20:49:27 +08:00
wjqserver
c522eba7ae update 2025-04-04 16:40:34 +08:00
15 changed files with 167 additions and 157 deletions

View File

@@ -144,4 +144,4 @@ jobs:
${{ env.IMAGE_NAME }}:v4
${{ env.IMAGE_NAME }}:latest
wjqserver/ghproxy-touka:latest
wjqserver/ghproxy-touka:${{ env.VERSION }}
wjqserver/ghproxy-touka:${{ env.VERSION }}

View File

@@ -1,5 +1,75 @@
# 更新日志
4.1.5 - 2025-07-03
---
- CHANGE: 更新`httpc`依赖以修正一些问题
4.1.5-rc.0 - 2025-07-03
---
- PRE-RELEASE: v4.1.5-rc.0是v4.1.5预发布版本,请勿在生产环境中使用;
- CHANGE: 更新`httpc`依赖以修正一些问题
4.1.4 - 2025-06-30
---
- CHANGE: 使用`touka`框架的内建httpc统一管理, 同时对httpc相关初始化进行改进
- CHANGE: 更新`json/v2`版本
4.1.4-rc.0 - 2025-06-30
---
- PRE-RELEASE: v4.1.4-rc.0是v4.1.4预发布版本,请勿在生产环境中使用;
- CHANGE: 使用`touka`框架的内建httpc统一管理, 同时对httpc相关初始化进行改进
- CHANGE: 更新`json/v2`版本
4.1.3 - 2025-06-25
---
- CHANGE: 更新`touka`版本, 使用新的方式配置slash重定向功能
4.1.3-rc.0 - 2025-06-25
---
- PRE-RELEASE: 此版本是v4.1.3预发布版本,请勿在生产环境中使用;
- CHANGE: 更新`touka`版本, 使用新的方式配置slash重定向功能
4.1.2 - 2025-06-18
---
- CHANGE: 更新`design`主题, 更新默认配置生成
4.1.2-rc.0 - 2025-06-18
---
- PRE-RELEASE: 此版本是v4.1.2预发布版本,请勿在生产环境中使用;
- CHANGE: 更新`design`主题, 更新默认配置生成
4.1.1 - 2025-06-18
---
- CHANGE: 更新touka框架到v0.2.6, 解决MidwareX的一些状态问题
4.1.0 - 2025-06-17
---
- ADD: 加入基于`basic auth`的docker鉴权支持
4.1.0-rc.0 - 2025-06-17
---
- PRE-RELEASE: 此版本是v4.1.0预发布版本,请勿在生产环境中使用;
- ADD: 加入基于`basic auth`的docker鉴权支持
4.1.0-beta.0 - 2025-06-17
---
- BETA-TEST: 此版本是v4.1.0的测试版本,请勿在生产环境中使用;
- ADD: 加入基于`basic auth`的docker鉴权支持
4.0.0 - 2025-06-16
---
- CHANGE: 移交到Touka框架
- REMOVE: 移除req rate limit的total方式
- CHANGE: 使用[reco](https://github.com/fenthope/reco)日志库, 异步使能
- FIX: 更换HTTP框架以解决v3可能存在的内存分配与回收问题
4.0.0-rc.0 - 2025-06-16
---
- PRE-RELEASE: 此版本是v4.0.0预发布版本,请勿在生产环境中使用;
- CHANGE: 移交到Touka框架
- REMOVE: 移除req rate limit的total方式
- CHANGE: 使用[reco](https://github.com/fenthope/reco)日志库, 异步使能
4.0.0-beta.0 - 2025-06-15
---
- BETA-TEST: 此版本是v4.0.0的测试版本,请勿在生产环境中使用;

View File

@@ -1 +1 @@
4.0.0-beta.0
4.1.5-rc.0

View File

@@ -6,7 +6,7 @@
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/WJQSERVER-STUDIO/ghproxy)
[![Go Report Card](https://goreportcard.com/badge/github.com/WJQSERVER-STUDIO/ghproxy)](https://goreportcard.com/report/github.com/WJQSERVER-STUDIO/ghproxy)
GHProxy是一个基于Go的支持代理Github仓库资源和API的项目, 同时支持Docker镜像代理与脚本嵌套加速等多种功能
一个基于Go的高性能Github资源代理程序, 同时支持Docker镜像代理与脚本嵌套加速等多种功能
## 项目说明

View File

@@ -1 +1 @@
4.0.0
4.1.5

View File

@@ -169,10 +169,16 @@ type OutboundConfig struct {
[docker]
enabled = false
target = "ghcr" # ghcr/dockerhub
auth = false
[docker.credentials]
user1 = "testpass"
test = "test123"
*/
type DockerConfig struct {
Enabled bool `toml:"enabled"`
Target string `toml:"target"`
Enabled bool `toml:"enabled"`
Target string `toml:"target"`
Auth bool `toml:"auth"`
Credentials map[string]string `toml:"credentials"`
}
// LoadConfig 从 TOML 配置文件加载配置
@@ -239,7 +245,7 @@ func DefaultConfig() *Config {
},
Pages: PagesConfig{
Mode: "internal",
Theme: "bootstrap",
Theme: "hub",
StaticDir: "/data/www",
},
Log: LogConfig{
@@ -265,8 +271,7 @@ func DefaultConfig() *Config {
WhitelistFile: "/data/ghproxy/config/whitelist.json",
},
RateLimit: RateLimitConfig{
Enabled: false,
//RateMethod: "total",
Enabled: false,
RatePerMinute: 100,
Burst: 10,
BandwidthLimit: BandwidthLimitConfig{
@@ -283,7 +288,11 @@ func DefaultConfig() *Config {
},
Docker: DockerConfig{
Enabled: false,
Target: "ghcr",
Target: "dockerhub",
Auth: false,
Credentials: map[string]string{
"testpass": "test123",
},
},
}
}

View File

@@ -67,4 +67,8 @@ url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"
[docker]
enabled = false
target = "dockerhub" # ghcr/dockerhub/ custom
target = "dockerhub" # ghcr/dockerhub/ custom
auth = false
[docker.credentials]
user1 = "testpass"
test = "test123"

7
go.mod
View File

@@ -4,23 +4,24 @@ go 1.24.4
require (
github.com/BurntSushi/toml v1.5.0
github.com/WJQSERVER-STUDIO/httpc v0.7.0
github.com/WJQSERVER-STUDIO/httpc v0.7.2
golang.org/x/net v0.41.0
golang.org/x/time v0.12.0
)
require (
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2
github.com/fenthope/bauth v0.0.1
github.com/fenthope/ikumi v0.0.2
github.com/fenthope/reco v0.0.3
github.com/fenthope/record v0.0.3
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/infinite-iroha/touka v0.2.4
github.com/infinite-iroha/touka v0.2.8
github.com/wjqserver/modembed v0.0.1
)
require (
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 // indirect
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 // indirect
github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
)

14
go.sum
View File

@@ -4,20 +4,22 @@ github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 h1:JLtFd00AdFg/TP+dtvIzLkdHwKU
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc=
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2 h1:8bBkKk6E2Zr+I5szL7gyc5f0DK8N9agIJCpM1Cqw2NE=
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2/go.mod h1:yPX8xuZH+py7eLJwOYj3VVI/4/Yuy5+x8Mhq8qezcPg=
github.com/WJQSERVER-STUDIO/httpc v0.7.0 h1:iHhqlxppJBjlmvsIjvLZKRbWXqSdbeSGGofjHGmqGJc=
github.com/WJQSERVER-STUDIO/httpc v0.7.0/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE=
github.com/WJQSERVER-STUDIO/httpc v0.7.2 h1:ObEw1zCWBOVwhFTd2bE4BweOnEhSRJX/1qgCgt4hpf0=
github.com/WJQSERVER-STUDIO/httpc v0.7.2/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE=
github.com/fenthope/bauth v0.0.1 h1:+4UIQshGx3mYD4L3f2S4MLZOi5PWU7fU5GK3wsZvwzE=
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/go.mod h1:IYbxzOGndZv/yRrbVMyV6dxh06X2wXCbfxrTRM1IruU=
github.com/fenthope/reco v0.0.3 h1:RmnQ0D9a8PWtwOODawitTe4BztTnS9wYwrDbipISNq4=
github.com/fenthope/reco v0.0.3/go.mod h1:mDkGLHte5udWTIcjQTxrABRcf56SSdxBOCLgrRDwI/Y=
github.com/fenthope/record v0.0.3 h1:v5urgs5LAkLMlljAT/MjW8fWuRHXPnAraTem5ui7rm4=
github.com/fenthope/record v0.0.3/go.mod h1:KFEkSc4TDZ3QIhP/wglD32uYVA6X1OUcripiao1DEE4=
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 h1:o8UqXPI6SVwQt04RGsqKp3qqmbOfTNMqDrWsc4O47kk=
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b h1:ooF9/NzXkXL3OOLRwtPuQT/D7Kx2S5w/Kl1GnMF9h2s=
github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b/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/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/infinite-iroha/touka v0.2.4 h1:P1nmQYv4VEiTIahCw356VcFvpTFB9i11c31LeLh6WbM=
github.com/infinite-iroha/touka v0.2.4/go.mod h1:2MBPtsM+5ClIZ/E1mPEKx1Rb+KIndTwZlIa2CwRPV7A=
github.com/infinite-iroha/touka v0.2.8 h1:PH4oR0fUjNr6t+Q3xkpqK+Q+kOFk7LN3xvy81xydu7Y=
github.com/infinite-iroha/touka v0.2.8/go.mod h1:e2LRc8FoSU8qjxSlyh3J8gGsBGKQ2VN9bQMU4sIrqnE=
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/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs=

35
main.go
View File

@@ -15,6 +15,9 @@ import (
"ghproxy/config"
"ghproxy/proxy"
"github.com/WJQSERVER-STUDIO/httpc"
"github.com/fenthope/bauth"
"ghproxy/weakcache"
"github.com/fenthope/ikumi"
@@ -31,7 +34,7 @@ var (
cfg *config.Config
r *touka.Engine
configfile = "/data/ghproxy/config/config.toml"
hertZfile *os.File
httpClient *httpc.Client
cfgfile string
version string
runMode string
@@ -163,7 +166,8 @@ func setupApi(cfg *config.Config, r *touka.Engine, version string) {
}
func InitReq(cfg *config.Config) {
err := proxy.InitReq(cfg)
var err error
httpClient, err = proxy.InitReq(cfg)
if err != nil {
fmt.Printf("Failed to initialize request: %v\n", err)
os.Exit(1)
@@ -333,6 +337,7 @@ func main() {
r.Use(touka.Recovery()) // Recovery中间件
r.SetLogger(logger)
r.SetHTTPClient(httpClient)
r.Use(record.Middleware()) // log中间件
r.Use(viaHeader())
/*
@@ -362,6 +367,8 @@ func main() {
}
setupApi(cfg, r, version)
setupPages(cfg, r)
//r.RedirectTrailingSlash = false
r.SetRedirectTrailingSlash(false)
r.GET("/github.com/:user/:repo/releases/*filepath", func(c *touka.Context) {
c.Set("matcher", "releases")
@@ -411,15 +418,25 @@ func main() {
proxy.RoutingHandler(cfg)(c)
})
r.GET("/v2/", func(c *touka.Context) {
emptyJSON := "{}"
c.Header("Content-Type", "application/json")
c.Header("Content-Length", fmt.Sprint(len(emptyJSON)))
r.GET("/v2/",
r.UseIf(cfg.Docker.Auth, func() touka.HandlerFunc {
return bauth.BasicAuthForStatic(cfg.Docker.Credentials, "GHProxy Docker Proxy")
}),
func(c *touka.Context) {
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.Header("Docker-Distribution-API-Version", "registry/2.0")
c.Status(200)
c.Writer.Write([]byte(emptyJSON))
c.Status(200)
c.Writer.Write([]byte(emptyJSON))
},
)
r.GET("/v2", func(c *touka.Context) {
// 重定向到 /v2/
c.Redirect(http.StatusMovedPermanently, "/v2/")
})
r.ANY("/v2/:target/:user/:repo/*filepath", func(c *touka.Context) {

View File

@@ -119,6 +119,7 @@ func GhcrRequest(ctx context.Context, c *touka.Context, u string, image *imageIn
}()
method = c.Request.Method
ghcrclient := c.GetHTTPC()
rb := ghcrclient.NewRequestBuilder(method, u)
rb.NoDefaultHeaders()
@@ -267,6 +268,7 @@ func ChallengeReq(target string, image *imageInfo, ctx context.Context, c *touka
var resp401 *http.Response
var req401 *http.Request
var err error
ghcrclient := c.GetHTTPC()
rb401 := ghcrclient.NewRequestBuilder("GET", "https://"+target+"/v2/")
rb401.NoDefaultHeaders()

View File

@@ -17,30 +17,12 @@ func GitReq(ctx context.Context, c *touka.Context, u string, cfg *config.Config,
resp *http.Response
)
go func() {
<-ctx.Done()
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
/*
fullBody, err := c.GetReqBodyFull()
if err != nil {
HandleError(c, fmt.Sprintf("Failed to read request body: %v", err))
return
}
reqBodyReader := bytes.NewBuffer(fullBody)
*/
reqBodyReader, err := c.GetReqBodyBuffer()
if err != nil {
HandleError(c, fmt.Sprintf("Failed to read request body: %v", err))
return
}
//bodyReader := c.Request.BodyStream() // 不可替换为此实现
if cfg.GitClone.Mode == "cache" {
userPath, repoPath, remainingPath, queryParams, err := extractParts(u)
if err != nil {
@@ -110,14 +92,6 @@ func GitReq(ctx context.Context, c *touka.Context, u string, cfg *config.Config,
}
}
/*
for key, values := range resp.Header {
for _, value := range values {
c.Response.Header.Add(key, value)
}
}
*/
//copyHeader( resp.Header)
c.SetHeaders(resp.Header)
headersToRemove := map[string]struct{}{
@@ -150,10 +124,6 @@ func GitReq(ctx context.Context, c *touka.Context, u string, cfg *config.Config,
bodyReader := resp.Body
// 读取body内容
//bodyContent, _ := io.ReadAll(bodyReader)
// c.Infof("%s", bodyContent)
if cfg.RateLimit.BandwidthLimit.Enabled {
bodyReader = limitreader.NewRateLimitedReader(bodyReader, bandwidthLimit, int(bandwidthBurst), ctx)
}

View File

@@ -15,10 +15,6 @@ func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
return func(c *touka.Context) {
var ctx = c.Request.Context()
var shoudBreak bool
// shoudBreak = rateCheck(cfg, c, limiter, iplimiter)
// if shoudBreak {
// return
// }
var (
rawPath string

View File

@@ -1,7 +1,6 @@
package proxy
import (
"fmt"
"ghproxy/config"
"net/http"
"time"
@@ -12,42 +11,40 @@ import (
var BufferSize int = 32 * 1024 // 32KB
var (
tr *http.Transport
gittr *http.Transport
client *httpc.Client
gitclient *httpc.Client
ghcrtr *http.Transport
ghcrclient *httpc.Client
tr *http.Transport
gittr *http.Transport
client *httpc.Client
gitclient *httpc.Client
)
func InitReq(cfg *config.Config) error {
initHTTPClient(cfg)
func InitReq(cfg *config.Config) (*httpc.Client, error) {
client := initHTTPClient(cfg)
if cfg.GitClone.Mode == "cache" {
initGitHTTPClient(cfg)
}
initGhcrHTTPClient(cfg)
err := SetGlobalRateLimit(cfg)
if err != nil {
return err
return nil, err
}
return nil
return client, nil
}
func initHTTPClient(cfg *config.Config) {
func initHTTPClient(cfg *config.Config) *httpc.Client {
var proTolcols = new(http.Protocols)
proTolcols.SetHTTP1(true)
proTolcols.SetHTTP2(true)
proTolcols.SetUnencryptedHTTP2(true)
if cfg.Httpc.Mode == "auto" || cfg.Httpc.Mode == "" {
switch cfg.Httpc.Mode {
case "auto", "":
tr = &http.Transport{
IdleConnTimeout: 30 * time.Second,
WriteBufferSize: 32 * 1024, // 32KB
ReadBufferSize: 32 * 1024, // 32KB
Protocols: proTolcols,
}
} else if cfg.Httpc.Mode == "advanced" {
case "advanced":
tr = &http.Transport{
MaxIdleConns: cfg.Httpc.MaxIdleConns,
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
@@ -56,9 +53,10 @@ func initHTTPClient(cfg *config.Config) {
ReadBufferSize: 32 * 1024, // 32KB
Protocols: proTolcols,
}
} else {
default:
panic("unknown httpc mode: " + cfg.Httpc.Mode)
}
if cfg.Outbound.Enabled {
initTransport(cfg, tr)
}
@@ -72,18 +70,18 @@ func initHTTPClient(cfg *config.Config) {
httpc.WithTransport(tr),
)
}
return client
}
func initGitHTTPClient(cfg *config.Config) {
if cfg.Httpc.Mode == "auto" || cfg.Httpc.Mode == "" {
switch cfg.Httpc.Mode {
case "auto", "":
gittr = &http.Transport{
IdleConnTimeout: 30 * time.Second,
WriteBufferSize: 32 * 1024, // 32KB
ReadBufferSize: 32 * 1024, // 32KB
}
} else if cfg.Httpc.Mode == "advanced" {
case "advanced":
gittr = &http.Transport{
MaxIdleConns: cfg.Httpc.MaxIdleConns,
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
@@ -91,84 +89,30 @@ func initGitHTTPClient(cfg *config.Config) {
WriteBufferSize: 32 * 1024, // 32KB
ReadBufferSize: 32 * 1024, // 32KB
}
} else {
default:
panic("unknown httpc mode: " + cfg.Httpc.Mode)
}
if cfg.Outbound.Enabled {
initTransport(cfg, gittr)
}
if cfg.Server.Debug && cfg.GitClone.ForceH2C {
gitclient = httpc.New(
httpc.WithTransport(gittr),
httpc.WithDumpLog(),
httpc.WithProtocols(httpc.ProtocolsConfig{
ForceH2C: true,
}),
)
} else if !cfg.Server.Debug && cfg.GitClone.ForceH2C {
gitclient = httpc.New(
httpc.WithTransport(gittr),
httpc.WithProtocols(httpc.ProtocolsConfig{
ForceH2C: true,
}),
)
} else if cfg.Server.Debug && !cfg.GitClone.ForceH2C {
gitclient = httpc.New(
httpc.WithTransport(gittr),
httpc.WithDumpLog(),
httpc.WithProtocols(httpc.ProtocolsConfig{
Http1: true,
Http2: true,
Http2_Cleartext: true,
}),
)
} else {
gitclient = httpc.New(
httpc.WithTransport(gittr),
httpc.WithProtocols(httpc.ProtocolsConfig{
Http1: true,
Http2: true,
Http2_Cleartext: true,
}),
)
}
}
func initGhcrHTTPClient(cfg *config.Config) {
var proTolcols = new(http.Protocols)
proTolcols.SetHTTP1(true)
proTolcols.SetHTTP2(true)
if cfg.Httpc.Mode == "auto" || cfg.Httpc.Mode == "" {
var opts []httpc.Option // 使用切片来收集选项
opts = append(opts, httpc.WithTransport(gittr))
var protocolsConfig httpc.ProtocolsConfig
ghcrtr = &http.Transport{
IdleConnTimeout: 30 * time.Second,
WriteBufferSize: 32 * 1024, // 32KB
ReadBufferSize: 32 * 1024, // 32KB
Protocols: proTolcols,
}
} else if cfg.Httpc.Mode == "advanced" {
ghcrtr = &http.Transport{
MaxIdleConns: cfg.Httpc.MaxIdleConns,
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
MaxIdleConnsPerHost: cfg.Httpc.MaxIdleConnsPerHost,
WriteBufferSize: 32 * 1024, // 32KB
ReadBufferSize: 32 * 1024, // 32KB
Protocols: proTolcols,
}
if cfg.GitClone.ForceH2C {
protocolsConfig.ForceH2C = true
} else {
panic(fmt.Sprintf("unknown httpc mode: %s", cfg.Httpc.Mode))
}
if cfg.Outbound.Enabled {
initTransport(cfg, ghcrtr)
protocolsConfig.Http1 = true
protocolsConfig.Http2 = true
protocolsConfig.Http2_Cleartext = true
}
opts = append(opts, httpc.WithProtocols(protocolsConfig))
if cfg.Server.Debug {
ghcrclient = httpc.New(
httpc.WithTransport(ghcrtr),
httpc.WithDumpLog(),
)
} else {
ghcrclient = httpc.New(
httpc.WithTransport(ghcrtr),
)
opts = append(opts, httpc.WithDumpLog())
}
gitclient = httpc.New(opts...)
}

View File

@@ -12,11 +12,6 @@ func RoutingHandler(cfg *config.Config) touka.HandlerFunc {
var shoudBreak bool
// shoudBreak = rateCheck(cfg, c, limiter, iplimiter)
// if shoudBreak {
// return
//}
var (
rawPath string
)