Skip to content

Commit 1881b5b

Browse files
author
user123456
committed
增加HTTP2多路复用的支持
1 parent 75e3715 commit 1881b5b

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ host = "0.0.0.0"
109109
port = 5000
110110
# Github文件大小限制(字节),默认2GB
111111
fileSize = 2147483648
112+
# HTTP/2 多路复用
113+
enableH2C = false
112114
113115
[rateLimit]
114116
# 每个IP每小时允许的请求数(注意Docker镜像会有多个层,会消耗多个次数)

src/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ host = "0.0.0.0"
44
port = 5000
55
# Github文件大小限制(字节),默认2GB
66
fileSize = 2147483648
7+
# HTTP/2 多路复用
8+
enableH2C = false
79

810
[rateLimit]
911
# 每个IP每小时允许的请求数(注意Docker镜像会有多个层,会消耗多个次数)

src/config/config.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ type RegistryMapping struct {
2222
// AppConfig 应用配置结构体
2323
type AppConfig struct {
2424
Server struct {
25-
Host string `toml:"host"`
26-
Port int `toml:"port"`
27-
FileSize int64 `toml:"fileSize"`
25+
Host string `toml:"host"`
26+
Port int `toml:"port"`
27+
FileSize int64 `toml:"fileSize"`
28+
EnableH2C bool `toml:"enableH2C"`
2829
} `toml:"server"`
2930

3031
RateLimit struct {
@@ -69,13 +70,15 @@ var (
6970
func DefaultConfig() *AppConfig {
7071
return &AppConfig{
7172
Server: struct {
72-
Host string `toml:"host"`
73-
Port int `toml:"port"`
74-
FileSize int64 `toml:"fileSize"`
73+
Host string `toml:"host"`
74+
Port int `toml:"port"`
75+
FileSize int64 `toml:"fileSize"`
76+
EnableH2C bool `toml:"enableH2C"`
7577
}{
76-
Host: "0.0.0.0",
77-
Port: 5000,
78-
FileSize: 2 * 1024 * 1024 * 1024, // 2GB
78+
Host: "0.0.0.0",
79+
Port: 5000,
80+
FileSize: 2 * 1024 * 1024 * 1024, // 2GB
81+
EnableH2C: false, // 默认关闭H2C
7982
},
8083
RateLimit: struct {
8184
RequestLimit int `toml:"requestLimit"`
@@ -219,6 +222,11 @@ func overrideFromEnv(cfg *AppConfig) {
219222
cfg.Server.Port = port
220223
}
221224
}
225+
if val := os.Getenv("ENABLE_H2C"); val != "" {
226+
if enable, err := strconv.ParseBool(val); err == nil {
227+
cfg.Server.EnableH2C = enable
228+
}
229+
}
222230
if val := os.Getenv("MAX_FILE_SIZE"); val != "" {
223231
if size, err := strconv.ParseInt(val, 10, 64); err == nil && size > 0 {
224232
cfg.Server.FileSize = size

src/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/gin-gonic/gin v1.10.0
77
github.com/google/go-containerregistry v0.20.5
88
github.com/pelletier/go-toml/v2 v2.2.3
9+
golang.org/x/net v0.33.0
910
golang.org/x/time v0.11.0
1011
)
1112

src/main.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"time"
1010

1111
"github.com/gin-gonic/gin"
12+
"golang.org/x/net/http2"
13+
"golang.org/x/net/http2/h2c"
1214
"hubproxy/config"
1315
"hubproxy/handlers"
1416
"hubproxy/utils"
@@ -117,9 +119,37 @@ func main() {
117119
fmt.Printf("🚀 HubProxy 启动成功\n")
118120
fmt.Printf("📡 监听地址: %s:%d\n", cfg.Server.Host, cfg.Server.Port)
119121
fmt.Printf("⚡ 限流配置: %d请求/%g小时\n", cfg.RateLimit.RequestLimit, cfg.RateLimit.PeriodHours)
122+
123+
// 显示HTTP/2支持状态
124+
if cfg.Server.EnableH2C {
125+
fmt.Printf("H2c: 已启用\n")
126+
}
127+
120128
fmt.Printf("🔗 项目地址: https://github.com/sky22333/hubproxy\n")
121129

122-
err := router.Run(fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port))
130+
// 创建HTTP2服务器
131+
server := &http.Server{
132+
Addr: fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port),
133+
ReadTimeout: 60 * time.Second,
134+
WriteTimeout: 300 * time.Second,
135+
IdleTimeout: 120 * time.Second,
136+
}
137+
138+
// 根据配置决定是否启用H2C
139+
if cfg.Server.EnableH2C {
140+
h2cHandler := h2c.NewHandler(router, &http2.Server{
141+
MaxConcurrentStreams: 250,
142+
IdleTimeout: 300 * time.Second,
143+
MaxReadFrameSize: 4 << 20,
144+
MaxUploadBufferPerConnection: 8 << 20,
145+
MaxUploadBufferPerStream: 2 << 20,
146+
})
147+
server.Handler = h2cHandler
148+
} else {
149+
server.Handler = router
150+
}
151+
152+
err := server.ListenAndServe()
123153
if err != nil {
124154
fmt.Printf("启动服务失败: %v\n", err)
125155
}
@@ -173,4 +203,3 @@ func initHealthRoutes(router *gin.Engine) {
173203
})
174204
})
175205
}
176-

0 commit comments

Comments
 (0)