Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions agent/app/api/v2/clam.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (b *BaseApi) CleanClamRecord(c *gin.Context) {
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := clamService.CleanRecord(req); err != nil {
if err := clamService.CleanRecord(req.ID); err != nil {
helper.InternalServer(c, err)
return
}
Expand All @@ -174,7 +174,7 @@ func (b *BaseApi) SearchClamRecord(c *gin.Context) {
return
}

total, list, err := clamService.LoadRecords(req)
total, list, err := clamService.SearchRecords(req)
if err != nil {
helper.InternalServer(c, err)
return
Expand All @@ -186,29 +186,6 @@ func (b *BaseApi) SearchClamRecord(c *gin.Context) {
})
}

// @Tags Clam
// @Summary Load clam record detail
// @Accept json
// @Param request body dto.ClamLogReq true "request"
// @Success 200 {string} content
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /toolbox/clam/record/log [post]
func (b *BaseApi) LoadClamRecordLog(c *gin.Context) {
var req dto.ClamLogReq
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}

content, err := clamService.LoadRecordLog(req)
if err != nil {
helper.InternalServer(c, err)
return
}

helper.SuccessWithData(c, content)
}

// @Tags Clam
// @Summary Load clam file
// @Accept json
Expand Down Expand Up @@ -289,7 +266,7 @@ func (b *BaseApi) HandleClamScan(c *gin.Context) {
return
}

if err := clamService.HandleOnce(req); err != nil {
if err := clamService.HandleOnce(req.ID); err != nil {
helper.InternalServer(c, err)
return
}
Expand Down
22 changes: 13 additions & 9 deletions agent/app/dto/clam.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type ClamInfo struct {
Path string `json:"path"`
InfectedStrategy string `json:"infectedStrategy"`
InfectedDir string `json:"infectedDir"`
LastHandleDate string `json:"lastHandleDate"`
LastRecordStatus string `json:"lastRecordStatus"`
LastRecordTime string `json:"lastRecordTime"`
Spec string `json:"spec"`
Description string `json:"description"`
AlertCount uint `json:"alertCount"`
Expand All @@ -41,6 +42,7 @@ type ClamLogSearch struct {
PageInfo

ClamID uint `json:"clamID"`
Status string `json:"status"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
}
Expand All @@ -56,13 +58,16 @@ type ClamFileReq struct {
Name string `json:"name" validate:"required"`
}

type ClamLog struct {
Name string `json:"name"`
ScanDate string `json:"scanDate"`
ScanTime string `json:"scanTime"`
InfectedFiles string `json:"infectedFiles"`
TotalError string `json:"totalError"`
Status string `json:"status"`
type ClamRecord struct {
ID uint `json:"id"`
TaskID string `json:"taskID"`
StartTime time.Time `json:"startTime"`
ScanTime string `json:"scanTime"`
InfectedFiles string `json:"infectedFiles"`
TotalError string `json:"totalError"`

Status string `json:"status"`
Message string `json:"message"`
}

type ClamCreate struct {
Expand Down Expand Up @@ -98,7 +103,6 @@ type ClamUpdateStatus struct {
}

type ClamDelete struct {
RemoveRecord bool `json:"removeRecord"`
RemoveInfected bool `json:"removeInfected"`
Ids []uint `json:"ids" validate:"required"`
}
19 changes: 18 additions & 1 deletion agent/app/model/clam.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package model

import "time"

type Clam struct {
BaseModel

Name string `gorm:"not null" json:"name"`
Status string `json:"status"`
Path string `gorm:"not null" json:"path"`
InfectedStrategy string `json:"infectedStrategy"`
InfectedDir string `json:"infectedDir"`
Spec string `json:"spec"`
EntryID int `json:"entryID"`
Description string `json:"description"`

Status string `json:"status"`
IsExecuting bool `json:"isExecuting"`
}

type ClamRecord struct {
BaseModel

ClamID uint `json:"clamID"`
TaskID string `json:"taskID"`
StartTime time.Time `json:"startTime"`
ScanTime string `json:"scanTime"`
InfectedFiles string `json:"infectedFiles"`
TotalError string `json:"totalError"`
Status string `json:"status"`
Message string `json:"message"`
}
79 changes: 79 additions & 0 deletions agent/app/repo/clam.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package repo

import (
"time"

"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/google/uuid"
"gorm.io/gorm"
)

type ClamRepo struct{}
Expand All @@ -14,6 +19,14 @@ type IClamRepo interface {
Delete(opts ...DBOption) error
Get(opts ...DBOption) (model.Clam, error)
List(opts ...DBOption) ([]model.Clam, error)

WithByClamID(id uint) DBOption
ListRecord(opts ...DBOption) ([]model.ClamRecord, error)
RecordFirst(id uint) (model.ClamRecord, error)
DeleteRecord(opts ...DBOption) error
StartRecords(clamID uint) model.ClamRecord
EndRecords(record model.ClamRecord, status, message string)
PageRecords(page, size int, opts ...DBOption) (int64, []model.ClamRecord, error)
}

func NewIClamRepo() IClamRepo {
Expand Down Expand Up @@ -67,3 +80,69 @@ func (u *ClamRepo) Delete(opts ...DBOption) error {
}
return db.Delete(&model.Clam{}).Error
}

func (c *ClamRepo) WithByClamID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("clam_id = ?", id)
}
}

func (u *ClamRepo) ListRecord(opts ...DBOption) ([]model.ClamRecord, error) {
var record []model.ClamRecord
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&record).Error
return record, err
}

func (u *ClamRepo) RecordFirst(id uint) (model.ClamRecord, error) {
var record model.ClamRecord
err := global.DB.Where("clam_id = ?", id).Order("created_at desc").First(&record).Error
return record, err
}

func (u *ClamRepo) PageRecords(page, size int, opts ...DBOption) (int64, []model.ClamRecord, error) {
var records []model.ClamRecord
db := global.DB.Model(&model.ClamRecord{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Order("created_at desc").Limit(size).Offset(size * (page - 1)).Find(&records).Error
return count, records, err
}
func (u *ClamRepo) StartRecords(clamID uint) model.ClamRecord {
var record model.ClamRecord
record.StartTime = time.Now()
record.ClamID = clamID
record.TaskID = uuid.New().String()
record.Status = constant.StatusWaiting
if err := global.DB.Create(&record).Error; err != nil {
global.LOG.Errorf("create record status failed, err: %v", err)
}
_ = u.Update(clamID, map[string]interface{}{"is_executing": true})
return record
}
func (u *ClamRepo) EndRecords(record model.ClamRecord, status, message string) {
upMap := make(map[string]interface{})
upMap["status"] = status
upMap["message"] = message
upMap["task_id"] = record.TaskID
upMap["scan_time"] = record.ScanTime
upMap["infected_files"] = record.InfectedFiles
upMap["total_error"] = record.TotalError
if err := global.DB.Model(&model.ClamRecord{}).Where("id = ?", record.ID).Updates(upMap).Error; err != nil {
global.LOG.Errorf("update record status failed, err: %v", err)
}
_ = u.Update(record.ClamID, map[string]interface{}{"is_executing": false})
}
func (u *ClamRepo) DeleteRecord(opts ...DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
}
return db.Delete(&model.ClamRecord{}).Error
}
Loading
Loading