| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // Package server provides HTTP server initialization and configuration.
- package server
- import (
- "context"
- "log"
- "log/slog"
- "net/http"
- "time"
- "github.com/Wei-Shaw/sub2api/internal/config"
- "github.com/Wei-Shaw/sub2api/internal/handler"
- "github.com/Wei-Shaw/sub2api/internal/pkg/websearch"
- middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
- "github.com/Wei-Shaw/sub2api/internal/service"
- "github.com/gin-gonic/gin"
- "github.com/google/wire"
- "github.com/redis/go-redis/v9"
- "golang.org/x/net/http2"
- )
- // ProviderSet 提供服务器层的依赖
- var ProviderSet = wire.NewSet(
- ProvideRouter,
- ProvideHTTPServer,
- )
- // ProvideRouter 提供路由器
- func ProvideRouter(
- cfg *config.Config,
- handlers *handler.Handlers,
- jwtAuth middleware2.JWTAuthMiddleware,
- optionalJWTAuth middleware2.OptionalJWTAuthMiddleware,
- adminAuth middleware2.AdminAuthMiddleware,
- apiKeyAuth middleware2.APIKeyAuthMiddleware,
- auditLog middleware2.AuditLogMiddleware,
- stepUpAuth middleware2.StepUpAuthMiddleware,
- apiKeyService *service.APIKeyService,
- subscriptionService *service.SubscriptionService,
- opsService *service.OpsService,
- settingService *service.SettingService,
- userService *service.UserService,
- compositeResolver *service.CompositeRouteResolver,
- redisClient *redis.Client,
- ) *gin.Engine {
- if cfg.Server.Mode == "release" {
- gin.SetMode(gin.ReleaseMode)
- }
- r := gin.New()
- r.Use(middleware2.Recovery())
- configureTrustedProxies(r, cfg.Server)
- // Wire up websearch Manager builder so it initializes on startup and rebuilds on config save.
- settingService.SetWebSearchManagerBuilder(context.Background(), func(cfg *service.WebSearchEmulationConfig, proxyURLs map[int64]string) {
- 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,
- QuotaLimit: derefInt64(p.QuotaLimit),
- ExpiresAt: p.ExpiresAt,
- }
- if p.SubscribedAt != nil {
- pc.SubscribedAt = p.SubscribedAt
- }
- if p.ProxyID != nil {
- pc.ProxyID = *p.ProxyID
- if u, ok := proxyURLs[*p.ProxyID]; ok {
- pc.ProxyURL = u
- } 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
- }
- }
- configs = append(configs, pc)
- }
- service.SetWebSearchManager(websearch.NewManager(configs, redisClient))
- })
- return SetupRouter(r, handlers, jwtAuth, optionalJWTAuth, adminAuth, apiKeyAuth, auditLog, stepUpAuth, apiKeyService, subscriptionService, opsService, settingService, userService, compositeResolver, cfg, redisClient)
- }
- func configureTrustedProxies(r *gin.Engine, cfg config.ServerConfig) {
- if cfg.TrustedProxiesConfigured {
- if err := r.SetTrustedProxies(cfg.TrustedProxies); err != nil {
- log.Printf("Failed to set trusted proxies: %v", err)
- _ = r.SetTrustedProxies(nil)
- }
- if len(cfg.TrustedProxies) == 0 && cfg.Mode == "release" {
- log.Printf("Warning: server.trusted_proxies is explicitly empty; forwarded client IP trust is disabled")
- }
- } else {
- if err := r.SetTrustedProxies(nil); err != nil {
- log.Printf("Failed to disable trusted proxies: %v", err)
- }
- if cfg.Mode == "release" {
- log.Printf("Warning: server.trusted_proxies is not configured; disabling the forwarded-IP compatibility switch will use direct peer addresses only")
- }
- }
- }
- // ProvideHTTPServer 提供 HTTP 服务器
- func ProvideHTTPServer(cfg *config.Config, router *gin.Engine) *http.Server {
- httpHandler := http.Handler(router)
- server := &http.Server{
- Addr: cfg.Server.Address(),
- Handler: httpHandler,
- MaxHeaderBytes: cfg.Server.MaxHeaderBytes,
- // ReadHeaderTimeout: 读取请求头的超时时间,防止慢速请求头攻击
- ReadHeaderTimeout: time.Duration(cfg.Server.ReadHeaderTimeout) * time.Second,
- // IdleTimeout: 空闲连接超时时间,释放不活跃的连接资源
- IdleTimeout: time.Duration(cfg.Server.IdleTimeout) * time.Second,
- // 注意:不设置 WriteTimeout,因为流式响应可能持续十几分钟
- // 不设置 ReadTimeout,因为大请求体可能需要较长时间读取
- }
- 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))
- }
- // 根据配置决定是否启用 H2C
- if cfg.Server.H2C.Enabled {
- h2cConfig := cfg.Server.H2C
- if err := http2.ConfigureServer(server, &http2.Server{
- MaxConcurrentStreams: h2cConfig.MaxConcurrentStreams,
- IdleTimeout: time.Duration(h2cConfig.IdleTimeout) * time.Second,
- MaxReadFrameSize: uint32(h2cConfig.MaxReadFrameSize),
- MaxUploadBufferPerConnection: int32(h2cConfig.MaxUploadBufferPerConnection),
- MaxUploadBufferPerStream: int32(h2cConfig.MaxUploadBufferPerStream),
- }); err != nil {
- log.Printf("Failed to configure HTTP/2 Cleartext (h2c): %v", err)
- } else {
- protocols := new(http.Protocols)
- protocols.SetHTTP1(true)
- protocols.SetUnencryptedHTTP2(true)
- server.Protocols = protocols
- 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,
- )
- }
- }
- server.Handler = httpHandler
- return server
- }
- func derefInt64(p *int64) int64 {
- if p == nil {
- return 0
- }
- return *p
- }
|