http.go 9.6 KB

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