allowanddenyip.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package waf
  2. import (
  3. "github.com/gin-gonic/gin"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/handler"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/service/admin"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/service/api/waf"
  9. "net/http"
  10. )
  11. type AllowAndDenyIpHandler struct {
  12. *handler.Handler
  13. allowAndDenyIpService waf.AllowAndDenyIpService
  14. wafLogService admin.WafLogService
  15. }
  16. func NewAllowAndDenyIpHandler(
  17. handler *handler.Handler,
  18. allowAndDenyIpService waf.AllowAndDenyIpService,
  19. wafLogService admin.WafLogService,
  20. ) *AllowAndDenyIpHandler {
  21. return &AllowAndDenyIpHandler{
  22. Handler: handler,
  23. allowAndDenyIpService: allowAndDenyIpService,
  24. wafLogService : wafLogService,
  25. }
  26. }
  27. func (h *AllowAndDenyIpHandler) GetAllowAndDenyIp(ctx *gin.Context) {
  28. var req v1.AllowAndDenyIpRequest
  29. if err := ctx.ShouldBind(&req); err != nil {
  30. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  31. return
  32. }
  33. res, err := h.allowAndDenyIpService.GetAllowAndDenyIp(ctx, int64(req.Id))
  34. if err != nil {
  35. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  36. return
  37. }
  38. v1.HandleSuccess(ctx, res)
  39. }
  40. func (h *AllowAndDenyIpHandler) GetAllowAndDenyIpList(ctx *gin.Context) {
  41. var req v1.AllowAndDenyIpRequest
  42. if err := ctx.ShouldBind(&req); err != nil {
  43. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  44. return
  45. }
  46. res, err := h.allowAndDenyIpService.GetAllowAndDenyIpsAllByHostId(ctx, int64(req.HostId))
  47. if err != nil {
  48. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  49. return
  50. }
  51. v1.HandleSuccess(ctx, res)
  52. }
  53. func (h *AllowAndDenyIpHandler) AddAllowAndDenyIp(ctx *gin.Context) {
  54. var req v1.AllowAndDenyIpRequest
  55. if err := ctx.ShouldBind(&req); err != nil {
  56. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  57. return
  58. }
  59. id,err := h.allowAndDenyIpService.AddAllowAndDenyIps(ctx,req)
  60. if err != nil {
  61. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  62. return
  63. }
  64. go h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
  65. Uid: req.Uid,
  66. RequestIp: ctx.ClientIP(), // 复制 ClientIP
  67. UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
  68. Api: ctx.Request.URL.Path, // 复制 Path
  69. HostId: req.HostId,
  70. RuleId: id,
  71. ExtraData: req,
  72. })
  73. v1.HandleSuccess(ctx, nil)
  74. }
  75. func (h *AllowAndDenyIpHandler) EditAllowAndDenyIp(ctx *gin.Context) {
  76. var req v1.AllowAndDenyIpRequest
  77. if err := ctx.ShouldBind(&req); err != nil {
  78. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  79. return
  80. }
  81. err := h.allowAndDenyIpService.EditAllowAndDenyIps(ctx,req)
  82. if err != nil {
  83. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  84. return
  85. }
  86. go h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
  87. Uid: req.Uid,
  88. RequestIp: ctx.ClientIP(), // 复制 ClientIP
  89. UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
  90. Api: ctx.Request.URL.Path, // 复制 Path
  91. HostId: req.HostId,
  92. RuleId: req.Id,
  93. ExtraData: req,
  94. })
  95. v1.HandleSuccess(ctx, nil)
  96. }
  97. func (h *AllowAndDenyIpHandler) DeleteAllowAndDenyIp(ctx *gin.Context) {
  98. var req v1.DelAllowAndDenyIpRequest
  99. if err := ctx.ShouldBind(&req); err != nil {
  100. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  101. return
  102. }
  103. err := h.allowAndDenyIpService.DeleteAllowAndDenyIps(ctx,req)
  104. if err != nil {
  105. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  106. return
  107. }
  108. go h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
  109. Uid: req.Uid,
  110. RequestIp: ctx.ClientIP(), // 复制 ClientIP
  111. UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
  112. Api: ctx.Request.URL.Path, // 复制 Path
  113. HostId: req.HostId,
  114. ExtraData: req,
  115. })
  116. v1.HandleSuccess(ctx, nil)
  117. }