http.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package server
  2. import (
  3. "github.com/casbin/casbin/v2"
  4. "github.com/gin-gonic/gin"
  5. apiV1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/docs"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/handler"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/middleware"
  9. "github.com/go-nunu/nunu-layout-advanced/pkg/jwt"
  10. "github.com/go-nunu/nunu-layout-advanced/pkg/limiter"
  11. "github.com/go-nunu/nunu-layout-advanced/pkg/log"
  12. "github.com/go-nunu/nunu-layout-advanced/pkg/server/http"
  13. "github.com/spf13/viper"
  14. swaggerfiles "github.com/swaggo/files"
  15. ginSwagger "github.com/swaggo/gin-swagger"
  16. )
  17. func NewHTTPServer(
  18. logger *log.Logger,
  19. conf *viper.Viper,
  20. jwt *jwt.JWT,
  21. e *casbin.SyncedEnforcer,
  22. limiterInstance *limiter.Limiter,
  23. rateLimitMiddleware gin.HandlerFunc,
  24. userHandler *handler.UserHandler,
  25. gameShieldHandler *handler.GameShieldHandler,
  26. gameShieldBackendHandler *handler.GameShieldBackendHandler,
  27. webForwardingHandler *handler.WebForwardingHandler,
  28. weblimitHandler *handler.WebLimitHandler,
  29. tcpForwardingHandler *handler.TcpforwardingHandler,
  30. udpForwardingHandler *handler.UdpForWardingHandler,
  31. tcpLimitHandler *handler.TcpLimitHandler,
  32. udpLimitHandler *handler.UdpLimitHandler,
  33. globalLimitHandler *handler.GlobalLimitHandler,
  34. ) *http.Server {
  35. gin.SetMode(gin.DebugMode)
  36. s := http.NewServer(
  37. gin.Default(),
  38. logger,
  39. http.WithServerHost(conf.GetString("http.host")),
  40. http.WithServerPort(conf.GetInt("http.port")),
  41. )
  42. // swagger doc
  43. docs.SwaggerInfo.BasePath = "/v1"
  44. s.GET("/swagger/*any", ginSwagger.WrapHandler(
  45. swaggerfiles.Handler,
  46. //ginSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", conf.GetInt("app.http.port"))),
  47. ginSwagger.DefaultModelsExpandDepth(-1),
  48. ginSwagger.PersistAuthorization(true),
  49. ))
  50. s.Use(
  51. middleware.CORSMiddleware(),
  52. middleware.ResponseLogMiddleware(logger),
  53. middleware.RequestLogMiddleware(logger),
  54. //middleware.SignMiddleware(log),
  55. rateLimitMiddleware,
  56. )
  57. s.GET("/", func(ctx *gin.Context) {
  58. logger.WithContext(ctx).Info("hello")
  59. apiV1.HandleSuccess(ctx, map[string]interface{}{
  60. ":)": "Thank you for using nunu!",
  61. })
  62. })
  63. v1 := s.Group("/v1")
  64. {
  65. // No route group has permission
  66. noAuthRouter := v1.Group("/")
  67. {
  68. // 使用增强的Limiter.GetAPIConfig方法获取特定API的限流配置
  69. // 登录API限流
  70. loginConfig := limiterInstance.GetAPIConfig("login")
  71. noAuthRouter.POST("/login", middleware.IPRateLimitMiddleware(loginConfig), userHandler.Login)
  72. // 注册API限流
  73. registerConfig := limiterInstance.GetAPIConfig("register")
  74. noAuthRouter.POST("/register", middleware.IPRateLimitMiddleware(registerConfig), userHandler.Register)
  75. // 创建IP白名单实例
  76. ipAllowlist := middleware.NewIPAllowlist(conf, logger)
  77. ipAllowlistMiddleware := ipAllowlist.IPAllowlistMiddleware()
  78. // 为GameShield相关接口添加IP白名单保护
  79. noAuthRouter.POST("/gameShield/add", ipAllowlistMiddleware, gameShieldHandler.SubmitGameShield)
  80. noAuthRouter.POST("/gameShield/getField", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldField)
  81. noAuthRouter.POST("/gameShield/getKey", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldKey)
  82. noAuthRouter.POST("/gameShield/edit", ipAllowlistMiddleware, gameShieldHandler.EditGameShield)
  83. noAuthRouter.POST("/gameShield/delete", ipAllowlistMiddleware, gameShieldHandler.DeleteGameShield)
  84. noAuthRouter.POST("/gameShield/getOnline", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldOnlineList)
  85. noAuthRouter.POST("/gameShield/IsExistKey", gameShieldHandler.IsExistGameShieldKey)
  86. noAuthRouter.POST("/webForward/get", ipAllowlistMiddleware, webForwardingHandler.GetWebForwarding)
  87. noAuthRouter.POST("/webForward/getList", ipAllowlistMiddleware, webForwardingHandler.GetWebForwardingList)
  88. noAuthRouter.POST("/webForward/add", ipAllowlistMiddleware, webForwardingHandler.AddWebForwarding)
  89. noAuthRouter.POST("/webForward/edit", ipAllowlistMiddleware, webForwardingHandler.EditWebForwarding)
  90. noAuthRouter.POST("/webForward/delete", ipAllowlistMiddleware, webForwardingHandler.DeleteWebForwarding)
  91. noAuthRouter.POST("/webLimit/add", ipAllowlistMiddleware, weblimitHandler.AddWebLimit)
  92. noAuthRouter.POST("/webLimit/edit", ipAllowlistMiddleware, weblimitHandler.EditWebLimit)
  93. noAuthRouter.POST("/webLimit/delete", ipAllowlistMiddleware, weblimitHandler.DeleteWebLimit)
  94. noAuthRouter.POST("/tcpForward/add", ipAllowlistMiddleware, tcpForwardingHandler.AddTcpForwarding)
  95. noAuthRouter.POST("/tcpForward/get", ipAllowlistMiddleware, tcpForwardingHandler.GetTcpforwarding)
  96. noAuthRouter.POST("/tcpForward/getList", ipAllowlistMiddleware, tcpForwardingHandler.GetTcpForwardingList)
  97. noAuthRouter.POST("/tcpForward/edit", ipAllowlistMiddleware, tcpForwardingHandler.EditTcpForwarding)
  98. noAuthRouter.POST("/tcpForward/delete", ipAllowlistMiddleware, tcpForwardingHandler.DeleteTcpForwarding)
  99. noAuthRouter.POST("/udpForward/get", ipAllowlistMiddleware, udpForwardingHandler.GetUdpForWarding)
  100. noAuthRouter.POST("/udpForward/getList", ipAllowlistMiddleware, udpForwardingHandler.GetUdpForWardingList)
  101. noAuthRouter.POST("/udpForward/add", ipAllowlistMiddleware, udpForwardingHandler.AddUdpForWarding)
  102. noAuthRouter.POST("/udpForward/edit", ipAllowlistMiddleware, udpForwardingHandler.EditUdpForWarding)
  103. noAuthRouter.POST("/udpForward/delete", ipAllowlistMiddleware, udpForwardingHandler.DeleteUdpForWarding)
  104. noAuthRouter.POST("/tcpLimit/add", ipAllowlistMiddleware, tcpLimitHandler.AddTcpLimit)
  105. noAuthRouter.POST("/tcpLimit/edit", ipAllowlistMiddleware, tcpLimitHandler.EditTcpLimit)
  106. noAuthRouter.POST("/tcpLimit/delete", ipAllowlistMiddleware, tcpLimitHandler.DeleteTcpLimit)
  107. noAuthRouter.POST("/udpLimit/add", ipAllowlistMiddleware, udpLimitHandler.AddUdpLimit)
  108. noAuthRouter.POST("/udpLimit/edit", ipAllowlistMiddleware, udpLimitHandler.EditUdpLimit)
  109. noAuthRouter.POST("/udpLimit/delete", ipAllowlistMiddleware, udpLimitHandler.DeleteUdpLimit)
  110. noAuthRouter.POST("/gameShieldBackend/add", ipAllowlistMiddleware, gameShieldBackendHandler.AddGameShieldBackend)
  111. noAuthRouter.POST("/gameShieldBackend/edit", ipAllowlistMiddleware, gameShieldBackendHandler.EditGameShieldBackend)
  112. noAuthRouter.POST("/gameShieldBackend/delete", ipAllowlistMiddleware, gameShieldBackendHandler.DeleteGameShieldBackend)
  113. noAuthRouter.POST("/gameShieldBackend/replacementSourceMachineIp", ipAllowlistMiddleware, gameShieldBackendHandler.ReplacementSourceMachineIp)
  114. noAuthRouter.POST("/globalLimit/add", ipAllowlistMiddleware, globalLimitHandler.AddGlobalLimit)
  115. noAuthRouter.POST("/globalLimit/edit", ipAllowlistMiddleware, globalLimitHandler.EditGlobalLimit)
  116. noAuthRouter.POST("/globalLimit/delete", ipAllowlistMiddleware, globalLimitHandler.DeleteGlobalLimit)
  117. }
  118. // Non-strict permission routing group
  119. noStrictAuthRouter := v1.Group("/").Use(middleware.NoStrictAuth(jwt, logger))
  120. {
  121. noStrictAuthRouter.GET("/user", userHandler.GetProfile)
  122. }
  123. // Strict permission routing group
  124. strictAuthRouter := v1.Group("/").Use(middleware.StrictAuth(jwt, logger), middleware.AuthMiddleware(e))
  125. {
  126. strictAuthRouter.PUT("/user", userHandler.UpdateProfile)
  127. }
  128. }
  129. return s
  130. }