http.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/handler/api/waf"
  9. "github.com/go-nunu/nunu-layout-advanced/internal/middleware"
  10. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  11. "github.com/go-nunu/nunu-layout-advanced/pkg/jwt"
  12. "github.com/go-nunu/nunu-layout-advanced/pkg/limiter"
  13. "github.com/go-nunu/nunu-layout-advanced/pkg/log"
  14. "github.com/go-nunu/nunu-layout-advanced/pkg/server/http"
  15. "github.com/spf13/viper"
  16. swaggerfiles "github.com/swaggo/files"
  17. ginSwagger "github.com/swaggo/gin-swagger"
  18. )
  19. func NewHTTPServer(
  20. logger *log.Logger,
  21. conf *viper.Viper,
  22. jwt *jwt.JWT,
  23. e *casbin.SyncedEnforcer,
  24. limiterInstance *limiter.Limiter,
  25. rateLimitMiddleware gin.HandlerFunc,
  26. userHandler *handler.UserHandler,
  27. gameShieldHandler *handler.GameShieldHandler,
  28. gameShieldBackendHandler *handler.GameShieldBackendHandler,
  29. webForwardingHandler *waf.WebForwardingHandler,
  30. tcpForwardingHandler *waf.TcpforwardingHandler,
  31. udpForwardingHandler *waf.UdpForWardingHandler,
  32. globalLimitHandler *waf.GlobalLimitHandler,
  33. adminHandler *handler.AdminHandler,
  34. gatewayHandler *handler.GatewayGroupHandler,
  35. gatewayIpHandler *handler.GateWayGroupIpHandler,
  36. allowAnddenyHandler *waf.AllowAndDenyIpHandler,
  37. ccHandler *handler.CcHandler,
  38. logService service.LogService,
  39. ) *http.Server {
  40. gin.SetMode(gin.DebugMode)
  41. s := http.NewServer(
  42. gin.Default(),
  43. logger,
  44. http.WithServerHost(conf.GetString("http.host")),
  45. http.WithServerPort(conf.GetInt("http.port")),
  46. )
  47. // swagger doc
  48. docs.SwaggerInfo.BasePath = "/v1"
  49. s.GET("/swagger/*any", ginSwagger.WrapHandler(
  50. swaggerfiles.Handler,
  51. //ginSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", conf.GetInt("app.http.port"))),
  52. ginSwagger.DefaultModelsExpandDepth(-1),
  53. ginSwagger.PersistAuthorization(true),
  54. ))
  55. s.Use(
  56. middleware.CORSMiddleware(),
  57. middleware.ResponseLogMiddleware(logger),
  58. middleware.RequestLogMiddleware(logger),
  59. middleware.OperationLogMiddleware(logService),
  60. //middleware.SignMiddleware(log),
  61. rateLimitMiddleware,
  62. )
  63. s.GET("/", func(ctx *gin.Context) {
  64. logger.WithContext(ctx).Info("hello")
  65. apiV1.HandleSuccess(ctx, map[string]interface{}{
  66. ":)": "Thank you for using nunu!",
  67. })
  68. })
  69. v1 := s.Group("/v1")
  70. {
  71. // No route group has permission
  72. noAuthRouter := v1.Group("/")
  73. {
  74. // 使用增强的Limiter.GetAPIConfig方法获取特定API的限流配置
  75. // 登录API限流
  76. loginConfig := limiterInstance.GetAPIConfig("login")
  77. noAuthRouter.POST("/login", middleware.IPRateLimitMiddleware(loginConfig), adminHandler.Login)
  78. //// 注册API限流
  79. //registerConfig := limiterInstance.GetAPIConfig("register")
  80. //noAuthRouter.POST("/register", middleware.IPRateLimitMiddleware(registerConfig), userHandler.Register)
  81. // 创建IP白名单实例
  82. ipAllowlist := middleware.NewIPAllowlist(conf, logger)
  83. ipAllowlistMiddleware := ipAllowlist.IPAllowlistMiddleware()
  84. // 为GameShield相关接口添加IP白名单保护
  85. noAuthRouter.POST("/gameShield/add", ipAllowlistMiddleware, gameShieldHandler.SubmitGameShield)
  86. noAuthRouter.POST("/gameShield/getField", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldField)
  87. noAuthRouter.POST("/gameShield/getKey", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldKey)
  88. noAuthRouter.POST("/gameShield/edit", ipAllowlistMiddleware, gameShieldHandler.EditGameShield)
  89. noAuthRouter.POST("/gameShield/delete", ipAllowlistMiddleware, gameShieldHandler.DeleteGameShield)
  90. noAuthRouter.POST("/gameShield/getOnline", ipAllowlistMiddleware, gameShieldHandler.GetGameShieldOnlineList)
  91. noAuthRouter.POST("/gameShield/IsExistKey", gameShieldHandler.IsExistGameShieldKey)
  92. noAuthRouter.POST("/webForward/get", ipAllowlistMiddleware, webForwardingHandler.GetWebForwarding)
  93. noAuthRouter.POST("/webForward/getList", ipAllowlistMiddleware, webForwardingHandler.GetWebForwardingList)
  94. noAuthRouter.POST("/webForward/add", ipAllowlistMiddleware, webForwardingHandler.AddWebForwarding)
  95. noAuthRouter.POST("/webForward/edit", ipAllowlistMiddleware, webForwardingHandler.EditWebForwarding)
  96. noAuthRouter.POST("/webForward/delete", ipAllowlistMiddleware, webForwardingHandler.DeleteWebForwarding)
  97. noAuthRouter.POST("/tcpForward/add", ipAllowlistMiddleware, tcpForwardingHandler.AddTcpForwarding)
  98. noAuthRouter.POST("/tcpForward/get", ipAllowlistMiddleware, tcpForwardingHandler.GetTcpforwarding)
  99. noAuthRouter.POST("/tcpForward/getList", ipAllowlistMiddleware, tcpForwardingHandler.GetTcpForwardingList)
  100. noAuthRouter.POST("/tcpForward/edit", ipAllowlistMiddleware, tcpForwardingHandler.EditTcpForwarding)
  101. noAuthRouter.POST("/tcpForward/delete", ipAllowlistMiddleware, tcpForwardingHandler.DeleteTcpForwarding)
  102. noAuthRouter.POST("/udpForward/get", ipAllowlistMiddleware, udpForwardingHandler.GetUdpForWarding)
  103. noAuthRouter.POST("/udpForward/getList", ipAllowlistMiddleware, udpForwardingHandler.GetUdpForWardingList)
  104. noAuthRouter.POST("/udpForward/add", ipAllowlistMiddleware, udpForwardingHandler.AddUdpForWarding)
  105. noAuthRouter.POST("/udpForward/edit", ipAllowlistMiddleware, udpForwardingHandler.EditUdpForWarding)
  106. noAuthRouter.POST("/udpForward/delete", ipAllowlistMiddleware, udpForwardingHandler.DeleteUdpForWarding)
  107. noAuthRouter.POST("/gameShieldBackend/add", ipAllowlistMiddleware, gameShieldBackendHandler.AddGameShieldBackend)
  108. noAuthRouter.POST("/gameShieldBackend/edit", ipAllowlistMiddleware, gameShieldBackendHandler.EditGameShieldBackend)
  109. noAuthRouter.POST("/gameShieldBackend/delete", ipAllowlistMiddleware, gameShieldBackendHandler.DeleteGameShieldBackend)
  110. noAuthRouter.POST("/gameShieldBackend/replacementSourceMachineIp", ipAllowlistMiddleware, gameShieldBackendHandler.ReplacementSourceMachineIp)
  111. noAuthRouter.POST("/globalLimit/add", ipAllowlistMiddleware, globalLimitHandler.AddGlobalLimit)
  112. noAuthRouter.POST("/globalLimit/edit", ipAllowlistMiddleware, globalLimitHandler.EditGlobalLimit)
  113. noAuthRouter.POST("/globalLimit/delete", ipAllowlistMiddleware, globalLimitHandler.DeleteGlobalLimit)
  114. noAuthRouter.POST("/allowAndDeny/get", ipAllowlistMiddleware, allowAnddenyHandler.GetAllowAndDenyIp)
  115. noAuthRouter.POST("/allowAndDeny/getList", ipAllowlistMiddleware, allowAnddenyHandler.GetAllowAndDenyIpList)
  116. noAuthRouter.POST("/allowAndDeny/add", ipAllowlistMiddleware, allowAnddenyHandler.AddAllowAndDenyIp)
  117. noAuthRouter.POST("/allowAndDeny/edit", ipAllowlistMiddleware, allowAnddenyHandler.EditAllowAndDenyIp)
  118. noAuthRouter.POST("/allowAndDeny/delete", ipAllowlistMiddleware, allowAnddenyHandler.DeleteAllowAndDenyIp)
  119. noAuthRouter.POST("/cc/getList", ipAllowlistMiddleware, ccHandler.GetCcList)
  120. noAuthRouter.POST("/cc/editState", ipAllowlistMiddleware, ccHandler.EditCcState)
  121. }
  122. // Non-strict permission routing group
  123. //noStrictAuthRouter := v1.Group("/").Use(middleware.NoStrictAuth(jwt, logger))
  124. //{
  125. // noStrictAuthRouter.GET("/user", userHandler.GetProfile)
  126. //}
  127. // Strict permission routing group
  128. strictAuthRouter := v1.Group("/").Use(middleware.StrictAuth(jwt, logger), middleware.AuthMiddleware(e))
  129. {
  130. strictAuthRouter.GET("/user", userHandler.GetUsers)
  131. strictAuthRouter.GET("/menus", adminHandler.GetMenus)
  132. strictAuthRouter.GET("/admin/menus", adminHandler.GetAdminMenus)
  133. strictAuthRouter.POST("/admin/menu", adminHandler.MenuCreate)
  134. strictAuthRouter.PUT("/admin/menu", adminHandler.MenuUpdate)
  135. strictAuthRouter.DELETE("/admin/menu", adminHandler.MenuDelete)
  136. strictAuthRouter.GET("/admin/users", adminHandler.GetAdminUsers)
  137. strictAuthRouter.GET("/admin/user", adminHandler.GetAdminUser)
  138. strictAuthRouter.PUT("/admin/user", adminHandler.AdminUserUpdate)
  139. strictAuthRouter.POST("/admin/user", adminHandler.AdminUserCreate)
  140. strictAuthRouter.DELETE("/admin/user", adminHandler.AdminUserDelete)
  141. strictAuthRouter.GET("/admin/user/permissions", adminHandler.GetUserPermissions)
  142. strictAuthRouter.GET("/admin/role/permissions", adminHandler.GetRolePermissions)
  143. strictAuthRouter.PUT("/admin/role/permission", adminHandler.UpdateRolePermission)
  144. strictAuthRouter.GET("/admin/roles", adminHandler.GetRoles)
  145. strictAuthRouter.POST("/admin/role", adminHandler.RoleCreate)
  146. strictAuthRouter.PUT("/admin/role", adminHandler.RoleUpdate)
  147. strictAuthRouter.DELETE("/admin/role", adminHandler.RoleDelete)
  148. strictAuthRouter.GET("/admin/apis", adminHandler.GetApis)
  149. strictAuthRouter.POST("/admin/api", adminHandler.ApiCreate)
  150. strictAuthRouter.PUT("/admin/api", adminHandler.ApiUpdate)
  151. strictAuthRouter.DELETE("/admin/api", adminHandler.ApiDelete)
  152. strictAuthRouter.GET("/gateway/list", gatewayHandler.GetGatewayGroupList)
  153. strictAuthRouter.POST("/gateway/add", gatewayHandler.AddGatewayGroup)
  154. strictAuthRouter.PUT("/gateway/edit", gatewayHandler.EditGatewayGroup)
  155. strictAuthRouter.DELETE("/gateway/del", gatewayHandler.DeleteGatewayGroup)
  156. strictAuthRouter.GET("/gatewayIp/get", gatewayIpHandler.GetGateWayGroupIpList)
  157. strictAuthRouter.POST("/gatewayIp/add", gatewayIpHandler.AddGateWayGroupIp)
  158. strictAuthRouter.PUT("/gatewayIp/edit", gatewayIpHandler.EditGateWayGroupIp)
  159. strictAuthRouter.DELETE("/gatewayIp/del", gatewayIpHandler.DeleteGateWayGroupIp)
  160. }
  161. }
  162. return s
  163. }