http.go 11 KB

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