2026-01-04 19:27:53 +08:00
|
|
|
|
// Package server provides HTTP server initialization and configuration.
|
2025-12-19 00:01:43 +08:00
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-12 13:11:46 +08:00
|
|
|
|
"context"
|
2026-01-02 17:40:57 +08:00
|
|
|
|
"log"
|
2026-04-14 01:10:46 +08:00
|
|
|
|
"log/slog"
|
2025-12-19 00:01:43 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-12-26 10:42:08 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
2026-04-12 13:11:46 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/websearch"
|
2025-12-26 10:42:08 +08:00
|
|
|
|
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
2025-12-26 00:17:55 -08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-26 10:42:08 +08:00
|
|
|
|
|
2025-12-19 00:01:43 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/google/wire"
|
2026-01-10 13:14:35 +08:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
2026-02-04 21:40:25 +08:00
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2025-12-19 00:01:43 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ProviderSet 提供服务器层的依赖
|
|
|
|
|
|
var ProviderSet = wire.NewSet(
|
|
|
|
|
|
ProvideRouter,
|
|
|
|
|
|
ProvideHTTPServer,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ProvideRouter 提供路由器
|
2025-12-26 10:42:08 +08:00
|
|
|
|
func ProvideRouter(
|
|
|
|
|
|
cfg *config.Config,
|
|
|
|
|
|
handlers *handler.Handlers,
|
|
|
|
|
|
jwtAuth middleware2.JWTAuthMiddleware,
|
|
|
|
|
|
adminAuth middleware2.AdminAuthMiddleware,
|
2026-01-04 19:27:53 +08:00
|
|
|
|
apiKeyAuth middleware2.APIKeyAuthMiddleware,
|
|
|
|
|
|
apiKeyService *service.APIKeyService,
|
2025-12-26 00:17:55 -08:00
|
|
|
|
subscriptionService *service.SubscriptionService,
|
2026-01-09 20:55:12 +08:00
|
|
|
|
opsService *service.OpsService,
|
2026-01-10 18:37:44 +08:00
|
|
|
|
settingService *service.SettingService,
|
2026-01-10 13:14:35 +08:00
|
|
|
|
redisClient *redis.Client,
|
2025-12-26 10:42:08 +08:00
|
|
|
|
) *gin.Engine {
|
2025-12-19 00:01:43 +08:00
|
|
|
|
if cfg.Server.Mode == "release" {
|
|
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
r := gin.New()
|
2025-12-26 10:42:08 +08:00
|
|
|
|
r.Use(middleware2.Recovery())
|
2026-01-02 17:40:57 +08:00
|
|
|
|
if len(cfg.Server.TrustedProxies) > 0 {
|
|
|
|
|
|
if err := r.SetTrustedProxies(cfg.Server.TrustedProxies); err != nil {
|
|
|
|
|
|
log.Printf("Failed to set trusted proxies: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if err := r.SetTrustedProxies(nil); err != nil {
|
|
|
|
|
|
log.Printf("Failed to disable trusted proxies: %v", err)
|
|
|
|
|
|
}
|
2026-02-14 11:23:10 +08:00
|
|
|
|
if cfg.Server.Mode == "release" {
|
|
|
|
|
|
log.Printf("Warning: server.trusted_proxies is empty in release mode; client IP trust chain is disabled")
|
|
|
|
|
|
}
|
2026-01-02 17:40:57 +08:00
|
|
|
|
}
|
2025-12-19 00:01:43 +08:00
|
|
|
|
|
2026-04-12 13:11:46 +08:00
|
|
|
|
// Wire up websearch Manager builder so it initializes on startup and rebuilds on config save.
|
2026-04-12 14:43:12 +08:00
|
|
|
|
settingService.SetWebSearchManagerBuilder(context.Background(), func(cfg *service.WebSearchEmulationConfig, proxyURLs map[int64]string) {
|
2026-04-12 13:11:46 +08:00
|
|
|
|
if cfg == nil || !cfg.Enabled || len(cfg.Providers) == 0 {
|
|
|
|
|
|
service.SetWebSearchManager(nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
configs := make([]websearch.ProviderConfig, 0, len(cfg.Providers))
|
|
|
|
|
|
for _, p := range cfg.Providers {
|
|
|
|
|
|
if p.APIKey == "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
pc := websearch.ProviderConfig{
|
|
|
|
|
|
Type: p.Type,
|
|
|
|
|
|
APIKey: p.APIKey,
|
2026-04-14 08:03:27 +08:00
|
|
|
|
QuotaLimit: derefInt64(p.QuotaLimit),
|
2026-04-12 13:11:46 +08:00
|
|
|
|
ExpiresAt: p.ExpiresAt,
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.SubscribedAt != nil {
|
|
|
|
|
|
pc.SubscribedAt = p.SubscribedAt
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.ProxyID != nil {
|
|
|
|
|
|
pc.ProxyID = *p.ProxyID
|
2026-04-12 14:43:12 +08:00
|
|
|
|
if u, ok := proxyURLs[*p.ProxyID]; ok {
|
|
|
|
|
|
pc.ProxyURL = u
|
2026-04-14 01:10:46 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// Proxy configured but not found — skip this provider to prevent direct connection.
|
|
|
|
|
|
slog.Warn("websearch: proxy not found for provider, skipping",
|
|
|
|
|
|
"provider", p.Type, "proxy_id", *p.ProxyID)
|
|
|
|
|
|
continue
|
2026-04-12 14:43:12 +08:00
|
|
|
|
}
|
2026-04-12 13:11:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
configs = append(configs, pc)
|
|
|
|
|
|
}
|
|
|
|
|
|
service.SetWebSearchManager(websearch.NewManager(configs, redisClient))
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-01-11 11:11:37 +08:00
|
|
|
|
return SetupRouter(r, handlers, jwtAuth, adminAuth, apiKeyAuth, apiKeyService, subscriptionService, opsService, settingService, cfg, redisClient)
|
2025-12-19 00:01:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ProvideHTTPServer 提供 HTTP 服务器
|
|
|
|
|
|
func ProvideHTTPServer(cfg *config.Config, router *gin.Engine) *http.Server {
|
2026-02-04 21:40:25 +08:00
|
|
|
|
httpHandler := http.Handler(router)
|
|
|
|
|
|
|
2026-02-05 12:48:05 +08:00
|
|
|
|
globalMaxSize := cfg.Server.MaxRequestBodySize
|
|
|
|
|
|
if globalMaxSize <= 0 {
|
|
|
|
|
|
globalMaxSize = cfg.Gateway.MaxBodySize
|
|
|
|
|
|
}
|
|
|
|
|
|
if globalMaxSize > 0 {
|
|
|
|
|
|
httpHandler = http.MaxBytesHandler(httpHandler, globalMaxSize)
|
|
|
|
|
|
log.Printf("Global max request body size: %d bytes (%.2f MB)", globalMaxSize, float64(globalMaxSize)/(1<<20))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-04 21:40:25 +08:00
|
|
|
|
// 根据配置决定是否启用 H2C
|
2026-02-05 12:48:05 +08:00
|
|
|
|
if cfg.Server.H2C.Enabled {
|
|
|
|
|
|
h2cConfig := cfg.Server.H2C
|
2026-02-04 21:40:25 +08:00
|
|
|
|
httpHandler = h2c.NewHandler(router, &http2.Server{
|
2026-02-05 12:48:05 +08:00
|
|
|
|
MaxConcurrentStreams: h2cConfig.MaxConcurrentStreams,
|
|
|
|
|
|
IdleTimeout: time.Duration(h2cConfig.IdleTimeout) * time.Second,
|
|
|
|
|
|
MaxReadFrameSize: uint32(h2cConfig.MaxReadFrameSize),
|
|
|
|
|
|
MaxUploadBufferPerConnection: int32(h2cConfig.MaxUploadBufferPerConnection),
|
|
|
|
|
|
MaxUploadBufferPerStream: int32(h2cConfig.MaxUploadBufferPerStream),
|
2026-02-04 21:40:25 +08:00
|
|
|
|
})
|
2026-02-05 12:48:05 +08:00
|
|
|
|
log.Printf("HTTP/2 Cleartext (h2c) enabled: max_concurrent_streams=%d, idle_timeout=%ds, max_read_frame_size=%d, max_upload_buffer_per_connection=%d, max_upload_buffer_per_stream=%d",
|
|
|
|
|
|
h2cConfig.MaxConcurrentStreams,
|
|
|
|
|
|
h2cConfig.IdleTimeout,
|
|
|
|
|
|
h2cConfig.MaxReadFrameSize,
|
|
|
|
|
|
h2cConfig.MaxUploadBufferPerConnection,
|
|
|
|
|
|
h2cConfig.MaxUploadBufferPerStream,
|
|
|
|
|
|
)
|
2026-02-04 21:40:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-19 00:01:43 +08:00
|
|
|
|
return &http.Server{
|
|
|
|
|
|
Addr: cfg.Server.Address(),
|
2026-02-04 21:40:25 +08:00
|
|
|
|
Handler: httpHandler,
|
2025-12-19 00:01:43 +08:00
|
|
|
|
// ReadHeaderTimeout: 读取请求头的超时时间,防止慢速请求头攻击
|
|
|
|
|
|
ReadHeaderTimeout: time.Duration(cfg.Server.ReadHeaderTimeout) * time.Second,
|
|
|
|
|
|
// IdleTimeout: 空闲连接超时时间,释放不活跃的连接资源
|
|
|
|
|
|
IdleTimeout: time.Duration(cfg.Server.IdleTimeout) * time.Second,
|
|
|
|
|
|
// 注意:不设置 WriteTimeout,因为流式响应可能持续十几分钟
|
|
|
|
|
|
// 不设置 ReadTimeout,因为大请求体可能需要较长时间读取
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-04-14 08:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
func derefInt64(p *int64) int64 {
|
|
|
|
|
|
if p == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return *p
|
|
|
|
|
|
}
|