allowanddenyip.go 3.8 KB

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