cc.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 CcHandler struct {
  11. *handler.Handler
  12. ccService waf.CcService
  13. wafLogService waf.WaflogService
  14. }
  15. func NewCcHandler(
  16. handler *handler.Handler,
  17. ccService waf.CcService,
  18. wafLogService waf.WaflogService,
  19. ) *CcHandler {
  20. return &CcHandler{
  21. Handler: handler,
  22. ccService: ccService,
  23. wafLogService : wafLogService,
  24. }
  25. }
  26. func (h *CcHandler) GetCcList(ctx *gin.Context) {
  27. var req v1.CCListRequest
  28. if err := ctx.ShouldBind(&req); err != nil {
  29. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  30. return
  31. }
  32. resp, err := h.ccService.GetCcList(ctx, req)
  33. if err != nil {
  34. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  35. return
  36. }
  37. v1.HandleSuccess(ctx, resp)
  38. }
  39. func (h *CcHandler) EditCcState(ctx *gin.Context) {
  40. var req v1.CCStateRequest
  41. if err := ctx.ShouldBind(&req); err != nil {
  42. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  43. return
  44. }
  45. err := h.ccService.EditCcState(ctx, req)
  46. if err != nil {
  47. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  48. return
  49. }
  50. go h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
  51. Uid: int(req.Uid),
  52. RequestIp: ctx.ClientIP(), // 复制 ClientIP
  53. UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
  54. Api: ctx.Request.URL.Path, // 复制 Path
  55. HostId: int(req.HostId),
  56. ExtraData: req,
  57. })
  58. v1.HandleSuccess(ctx, nil)
  59. }
  60. func (h *CcHandler) AddWhiteOneClick (ctx *gin.Context) {
  61. var req v1.CCStateRequest
  62. if err := ctx.ShouldBind(&req); err != nil {
  63. v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
  64. return
  65. }
  66. err := h.ccService.AddWhiteOneClick(ctx, req)
  67. if err != nil {
  68. v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
  69. return
  70. }
  71. go h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
  72. Uid: int(req.Uid),
  73. RequestIp: ctx.ClientIP(), // 复制 ClientIP
  74. UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
  75. Api: ctx.Request.URL.Path, // 复制 Path
  76. HostId: int(req.HostId),
  77. ExtraData: req,
  78. })
  79. v1.HandleSuccess(ctx, nil)
  80. }