http.go 11 KB

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