|
9 | 9 | "time" |
10 | 10 |
|
11 | 11 | "github.com/gin-gonic/gin" |
| 12 | + "golang.org/x/net/http2" |
| 13 | + "golang.org/x/net/http2/h2c" |
12 | 14 | "hubproxy/config" |
13 | 15 | "hubproxy/handlers" |
14 | 16 | "hubproxy/utils" |
@@ -117,9 +119,37 @@ func main() { |
117 | 119 | fmt.Printf("🚀 HubProxy 启动成功\n") |
118 | 120 | fmt.Printf("📡 监听地址: %s:%d\n", cfg.Server.Host, cfg.Server.Port) |
119 | 121 | 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 | + |
120 | 128 | fmt.Printf("🔗 项目地址: https://github.com/sky22333/hubproxy\n") |
121 | 129 |
|
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() |
123 | 153 | if err != nil { |
124 | 154 | fmt.Printf("启动服务失败: %v\n", err) |
125 | 155 | } |
@@ -173,4 +203,3 @@ func initHealthRoutes(router *gin.Engine) { |
173 | 203 | }) |
174 | 204 | }) |
175 | 205 | } |
176 | | - |
|
0 commit comments