2024-06-24 13:52:42 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2024-07-17 16:55:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2024-06-24 13:52:42 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/systemctl"
|
2024-07-17 16:55:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
|
2024-06-24 13:52:42 +08:00
|
|
|
"github.com/jinzhu/copier"
|
2024-07-17 16:55:28 +08:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-06-24 13:52:42 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
clamServiceNameCentOs = "clamd@scan.service"
|
|
|
|
clamServiceNameUbuntu = "clamav-daemon.service"
|
2024-07-08 15:47:23 +08:00
|
|
|
freshClamService = "clamav-freshclam.service"
|
2024-07-02 17:25:25 +08:00
|
|
|
resultDir = "clamav"
|
2024-06-24 13:52:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClamService struct {
|
|
|
|
serviceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type IClamService interface {
|
|
|
|
LoadBaseInfo() (dto.ClamBaseInfo, error)
|
|
|
|
Operate(operate string) error
|
2024-07-17 16:55:28 +08:00
|
|
|
SearchWithPage(search dto.SearchClamWithPage) (int64, interface{}, error)
|
2024-06-24 13:52:42 +08:00
|
|
|
Create(req dto.ClamCreate) error
|
|
|
|
Update(req dto.ClamUpdate) error
|
2024-07-17 16:55:28 +08:00
|
|
|
UpdateStatus(id uint, status string) error
|
2024-07-02 17:25:25 +08:00
|
|
|
Delete(req dto.ClamDelete) error
|
2024-06-24 13:52:42 +08:00
|
|
|
HandleOnce(req dto.OperateByID) error
|
2024-07-08 15:47:23 +08:00
|
|
|
LoadFile(req dto.ClamFileReq) (string, error)
|
2024-06-24 13:52:42 +08:00
|
|
|
UpdateFile(req dto.UpdateByNameAndFile) error
|
|
|
|
LoadRecords(req dto.ClamLogSearch) (int64, interface{}, error)
|
|
|
|
CleanRecord(req dto.OperateByID) error
|
2024-07-04 16:00:32 +08:00
|
|
|
|
|
|
|
LoadRecordLog(req dto.ClamLogReq) (string, error)
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIClamService() IClamService {
|
|
|
|
return &ClamService{}
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) LoadBaseInfo() (dto.ClamBaseInfo, error) {
|
2024-06-24 13:52:42 +08:00
|
|
|
var baseInfo dto.ClamBaseInfo
|
|
|
|
baseInfo.Version = "-"
|
2024-07-08 15:47:23 +08:00
|
|
|
baseInfo.FreshVersion = "-"
|
2024-06-24 13:52:42 +08:00
|
|
|
exist1, _ := systemctl.IsExist(clamServiceNameCentOs)
|
|
|
|
if exist1 {
|
2024-07-12 14:11:25 +08:00
|
|
|
c.serviceName = clamServiceNameCentOs
|
2024-06-24 13:52:42 +08:00
|
|
|
baseInfo.IsExist = true
|
|
|
|
baseInfo.IsActive, _ = systemctl.IsActive(clamServiceNameCentOs)
|
|
|
|
}
|
2024-06-26 22:40:52 +08:00
|
|
|
exist2, _ := systemctl.IsExist(clamServiceNameUbuntu)
|
2024-06-24 13:52:42 +08:00
|
|
|
if exist2 {
|
2024-07-12 14:11:25 +08:00
|
|
|
c.serviceName = clamServiceNameUbuntu
|
2024-06-24 13:52:42 +08:00
|
|
|
baseInfo.IsExist = true
|
2024-06-26 22:40:52 +08:00
|
|
|
baseInfo.IsActive, _ = systemctl.IsActive(clamServiceNameUbuntu)
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
2024-07-08 15:47:23 +08:00
|
|
|
freshExist, _ := systemctl.IsExist(freshClamService)
|
|
|
|
if freshExist {
|
|
|
|
baseInfo.FreshIsExist = true
|
|
|
|
baseInfo.FreshIsActive, _ = systemctl.IsActive(freshClamService)
|
|
|
|
}
|
2024-07-17 16:55:28 +08:00
|
|
|
if !cmd.Which("clamdscan") {
|
2024-07-15 12:05:26 +08:00
|
|
|
baseInfo.IsActive = false
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
|
|
|
|
if baseInfo.IsActive {
|
|
|
|
version, err := cmd.Exec("clamdscan --version")
|
2024-07-12 14:11:25 +08:00
|
|
|
if err == nil {
|
|
|
|
if strings.Contains(version, "/") {
|
|
|
|
baseInfo.Version = strings.TrimPrefix(strings.Split(version, "/")[0], "ClamAV ")
|
|
|
|
} else {
|
|
|
|
baseInfo.Version = strings.TrimPrefix(version, "ClamAV ")
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
2024-08-20 17:38:47 +08:00
|
|
|
} else {
|
|
|
|
_ = StopAllCronJob(false)
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
2024-07-08 15:47:23 +08:00
|
|
|
if baseInfo.FreshIsActive {
|
|
|
|
version, err := cmd.Exec("freshclam --version")
|
2024-07-12 14:11:25 +08:00
|
|
|
if err == nil {
|
|
|
|
if strings.Contains(version, "/") {
|
|
|
|
baseInfo.FreshVersion = strings.TrimPrefix(strings.Split(version, "/")[0], "ClamAV ")
|
|
|
|
} else {
|
|
|
|
baseInfo.FreshVersion = strings.TrimPrefix(version, "ClamAV ")
|
|
|
|
}
|
2024-07-08 15:47:23 +08:00
|
|
|
}
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
return baseInfo, nil
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) Operate(operate string) error {
|
2024-06-24 13:52:42 +08:00
|
|
|
switch operate {
|
|
|
|
case "start", "restart", "stop":
|
2024-07-12 14:11:25 +08:00
|
|
|
stdout, err := cmd.Execf("systemctl %s %s", operate, c.serviceName)
|
2024-06-24 13:52:42 +08:00
|
|
|
if err != nil {
|
2024-07-12 14:11:25 +08:00
|
|
|
return fmt.Errorf("%s the %s failed, err: %s", operate, c.serviceName, stdout)
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
|
|
|
return nil
|
2024-07-08 15:47:23 +08:00
|
|
|
case "fresh-start", "fresh-restart", "fresh-stop":
|
|
|
|
stdout, err := cmd.Execf("systemctl %s %s", strings.TrimPrefix(operate, "fresh-"), freshClamService)
|
|
|
|
if err != nil {
|
2024-07-12 14:11:25 +08:00
|
|
|
return fmt.Errorf("%s the %s failed, err: %s", operate, c.serviceName, stdout)
|
2024-07-08 15:47:23 +08:00
|
|
|
}
|
|
|
|
return nil
|
2024-06-24 13:52:42 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("not support such operation: %v", operate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 16:55:28 +08:00
|
|
|
func (c *ClamService) SearchWithPage(req dto.SearchClamWithPage) (int64, interface{}, error) {
|
|
|
|
total, commands, err := clamRepo.Page(req.Page, req.PageSize, commonRepo.WithLikeName(req.Info), commonRepo.WithOrderRuleBy(req.OrderBy, req.Order))
|
2024-06-24 13:52:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
var datas []dto.ClamInfo
|
|
|
|
for _, command := range commands {
|
|
|
|
var item dto.ClamInfo
|
|
|
|
if err := copier.Copy(&item, &command); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
item.LastHandleDate = "-"
|
|
|
|
datas = append(datas, item)
|
|
|
|
}
|
2024-08-19 18:30:42 +08:00
|
|
|
nyc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
|
2024-06-24 13:52:42 +08:00
|
|
|
for i := 0; i < len(datas); i++ {
|
|
|
|
logPaths := loadFileByName(datas[i].Name)
|
|
|
|
sort.Slice(logPaths, func(i, j int) bool {
|
|
|
|
return logPaths[i] > logPaths[j]
|
|
|
|
})
|
|
|
|
if len(logPaths) != 0 {
|
2024-06-28 14:04:08 +08:00
|
|
|
t1, err := time.ParseInLocation(constant.DateTimeSlimLayout, logPaths[0], nyc)
|
2024-06-24 13:52:42 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-28 14:04:08 +08:00
|
|
|
datas[i].LastHandleDate = t1.Format(constant.DateTimeLayout)
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return total, datas, err
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) Create(req dto.ClamCreate) error {
|
2024-06-24 13:52:42 +08:00
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByName(req.Name))
|
|
|
|
if clam.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&clam, &req); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2024-07-08 10:32:24 +08:00
|
|
|
if clam.InfectedStrategy == "none" || clam.InfectedStrategy == "remove" {
|
|
|
|
clam.InfectedDir = ""
|
|
|
|
}
|
2024-07-17 16:55:28 +08:00
|
|
|
if len(req.Spec) != 0 {
|
|
|
|
entryID, err := xpack.StartClam(clam, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clam.EntryID = entryID
|
|
|
|
clam.Status = constant.StatusEnable
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
if err := clamRepo.Create(&clam); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) Update(req dto.ClamUpdate) error {
|
2024-06-24 13:52:42 +08:00
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByName(req.Name))
|
|
|
|
if clam.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
2024-07-08 10:32:24 +08:00
|
|
|
if req.InfectedStrategy == "none" || req.InfectedStrategy == "remove" {
|
|
|
|
req.InfectedDir = ""
|
|
|
|
}
|
2024-07-17 16:55:28 +08:00
|
|
|
var clamItem model.Clam
|
|
|
|
if err := copier.Copy(&clamItem, &req); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
clamItem.EntryID = clam.EntryID
|
2024-06-24 13:52:42 +08:00
|
|
|
upMap := map[string]interface{}{}
|
2024-07-17 16:55:28 +08:00
|
|
|
if len(clam.Spec) != 0 && clam.EntryID != 0 {
|
|
|
|
global.Cron.Remove(cron.EntryID(clamItem.EntryID))
|
|
|
|
upMap["entry_id"] = 0
|
|
|
|
}
|
|
|
|
if len(req.Spec) == 0 {
|
|
|
|
upMap["status"] = ""
|
|
|
|
upMap["entry_id"] = 0
|
|
|
|
}
|
|
|
|
if len(req.Spec) != 0 && clam.Status != constant.StatusDisable {
|
|
|
|
newEntryID, err := xpack.StartClam(clamItem, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
upMap["entry_id"] = newEntryID
|
|
|
|
}
|
|
|
|
if len(clam.Spec) == 0 && len(req.Spec) != 0 {
|
|
|
|
upMap["status"] = constant.StatusEnable
|
|
|
|
}
|
|
|
|
|
2024-06-24 13:52:42 +08:00
|
|
|
upMap["name"] = req.Name
|
|
|
|
upMap["path"] = req.Path
|
2024-07-08 10:32:24 +08:00
|
|
|
upMap["infected_dir"] = req.InfectedDir
|
|
|
|
upMap["infected_strategy"] = req.InfectedStrategy
|
2024-07-17 16:55:28 +08:00
|
|
|
upMap["spec"] = req.Spec
|
2024-06-24 13:52:42 +08:00
|
|
|
upMap["description"] = req.Description
|
|
|
|
if err := clamRepo.Update(req.ID, upMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 16:55:28 +08:00
|
|
|
func (c *ClamService) UpdateStatus(id uint, status string) error {
|
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if clam.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
entryID int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if status == constant.StatusEnable {
|
|
|
|
entryID, err = xpack.StartClam(clam, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
global.Cron.Remove(cron.EntryID(clam.EntryID))
|
|
|
|
global.LOG.Infof("stop cronjob entryID: %v", clam.EntryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return clamRepo.Update(clam.ID, map[string]interface{}{"status": status, "entry_id": entryID})
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) Delete(req dto.ClamDelete) error {
|
2024-07-02 17:25:25 +08:00
|
|
|
for _, id := range req.Ids {
|
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByID(id))
|
2024-06-24 13:52:42 +08:00
|
|
|
if clam.ID == 0 {
|
2024-07-02 17:25:25 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-07-04 16:00:32 +08:00
|
|
|
if req.RemoveRecord {
|
2024-07-02 17:25:25 +08:00
|
|
|
_ = os.RemoveAll(path.Join(global.CONF.System.DataDir, resultDir, clam.Name))
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
2024-07-02 17:25:25 +08:00
|
|
|
if req.RemoveInfected {
|
|
|
|
_ = os.RemoveAll(path.Join(clam.InfectedDir, "1panel-infected", clam.Name))
|
|
|
|
}
|
2024-07-05 16:52:39 +08:00
|
|
|
if err := clamRepo.Delete(commonRepo.WithByID(id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
2024-07-02 17:25:25 +08:00
|
|
|
return nil
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) HandleOnce(req dto.OperateByID) error {
|
2024-08-20 17:38:47 +08:00
|
|
|
if cleaned := StopAllCronJob(true); cleaned {
|
2024-07-12 14:11:25 +08:00
|
|
|
return buserr.New("ErrClamdscanNotFound")
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if clam.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
if cmd.CheckIllegal(clam.Path) {
|
|
|
|
return buserr.New(constant.ErrCmdIllegal)
|
|
|
|
}
|
2024-07-02 17:25:25 +08:00
|
|
|
timeNow := time.Now().Format(constant.DateTimeSlimLayout)
|
|
|
|
logFile := path.Join(global.CONF.System.DataDir, resultDir, clam.Name, timeNow)
|
2024-06-24 13:52:42 +08:00
|
|
|
if _, err := os.Stat(path.Dir(logFile)); err != nil {
|
|
|
|
_ = os.MkdirAll(path.Dir(logFile), os.ModePerm)
|
|
|
|
}
|
|
|
|
go func() {
|
2024-07-02 17:25:25 +08:00
|
|
|
strategy := ""
|
|
|
|
switch clam.InfectedStrategy {
|
|
|
|
case "remove":
|
|
|
|
strategy = "--remove"
|
|
|
|
case "move":
|
|
|
|
dir := path.Join(clam.InfectedDir, "1panel-infected", clam.Name, timeNow)
|
|
|
|
strategy = "--move=" + dir
|
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
_ = os.MkdirAll(dir, os.ModePerm)
|
|
|
|
}
|
|
|
|
case "copy":
|
|
|
|
dir := path.Join(clam.InfectedDir, "1panel-infected", clam.Name, timeNow)
|
|
|
|
strategy = "--copy=" + dir
|
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
_ = os.MkdirAll(dir, os.ModePerm)
|
|
|
|
}
|
|
|
|
}
|
2024-07-05 16:52:39 +08:00
|
|
|
global.LOG.Debugf("clamdscan --fdpass %s %s -l %s", strategy, clam.Path, logFile)
|
|
|
|
stdout, err := cmd.Execf("clamdscan --fdpass %s %s -l %s", strategy, clam.Path, logFile)
|
|
|
|
if err != nil {
|
2024-07-12 14:11:25 +08:00
|
|
|
global.LOG.Errorf("clamdscan failed, stdout: %v, err: %v", stdout, err)
|
2024-07-05 16:52:39 +08:00
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) LoadRecords(req dto.ClamLogSearch) (int64, interface{}, error) {
|
2024-06-24 13:52:42 +08:00
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByID(req.ClamID))
|
|
|
|
if clam.ID == 0 {
|
|
|
|
return 0, nil, constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
logPaths := loadFileByName(clam.Name)
|
|
|
|
if len(logPaths) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterFiles []string
|
2024-08-19 18:30:42 +08:00
|
|
|
nyc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
|
2024-06-24 13:52:42 +08:00
|
|
|
for _, item := range logPaths {
|
2024-06-28 14:04:08 +08:00
|
|
|
t1, err := time.ParseInLocation(constant.DateTimeSlimLayout, item, nyc)
|
2024-06-24 13:52:42 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t1.After(req.StartTime) && t1.Before(req.EndTime) {
|
|
|
|
filterFiles = append(filterFiles, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(filterFiles) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(filterFiles, func(i, j int) bool {
|
|
|
|
return filterFiles[i] > filterFiles[j]
|
|
|
|
})
|
|
|
|
|
|
|
|
var records []string
|
|
|
|
total, start, end := len(filterFiles), (req.Page-1)*req.PageSize, req.Page*req.PageSize
|
|
|
|
if start > total {
|
|
|
|
records = make([]string, 0)
|
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
|
|
|
}
|
|
|
|
records = filterFiles[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
var datas []dto.ClamLog
|
|
|
|
for i := 0; i < len(records); i++ {
|
2024-07-02 17:25:25 +08:00
|
|
|
item := loadResultFromLog(path.Join(global.CONF.System.DataDir, resultDir, clam.Name, records[i]))
|
2024-06-24 13:52:42 +08:00
|
|
|
datas = append(datas, item)
|
|
|
|
}
|
|
|
|
return int64(total), datas, nil
|
|
|
|
}
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) LoadRecordLog(req dto.ClamLogReq) (string, error) {
|
2024-07-04 16:00:32 +08:00
|
|
|
logPath := path.Join(global.CONF.System.DataDir, resultDir, req.ClamName, req.RecordName)
|
|
|
|
var tail string
|
|
|
|
if req.Tail != "0" {
|
|
|
|
tail = req.Tail
|
|
|
|
} else {
|
|
|
|
tail = "+1"
|
|
|
|
}
|
|
|
|
cmd := exec.Command("tail", "-n", tail, logPath)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("tail -n %v failed, err: %v", req.Tail, err)
|
|
|
|
}
|
|
|
|
return string(stdout), nil
|
|
|
|
}
|
2024-06-24 13:52:42 +08:00
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) CleanRecord(req dto.OperateByID) error {
|
2024-06-24 13:52:42 +08:00
|
|
|
clam, _ := clamRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if clam.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
2024-07-02 17:25:25 +08:00
|
|
|
pathItem := path.Join(global.CONF.System.DataDir, resultDir, clam.Name)
|
2024-06-24 13:52:42 +08:00
|
|
|
_ = os.RemoveAll(pathItem)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) LoadFile(req dto.ClamFileReq) (string, error) {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath := ""
|
|
|
|
switch req.Name {
|
|
|
|
case "clamd":
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath = "/etc/clamav/clamd.conf"
|
|
|
|
} else {
|
|
|
|
filePath = "/etc/clamd.d/scan.conf"
|
|
|
|
}
|
|
|
|
case "clamd-log":
|
2024-07-12 14:11:25 +08:00
|
|
|
filePath = c.loadLogPath("clamd-log")
|
2024-07-08 15:47:23 +08:00
|
|
|
if len(filePath) != 0 {
|
|
|
|
break
|
|
|
|
}
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath = "/var/log/clamav/clamav.log"
|
|
|
|
} else {
|
|
|
|
filePath = "/var/log/clamd.scan"
|
|
|
|
}
|
|
|
|
case "freshclam":
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath = "/etc/clamav/freshclam.conf"
|
|
|
|
} else {
|
|
|
|
filePath = "/etc/freshclam.conf"
|
|
|
|
}
|
|
|
|
case "freshclam-log":
|
2024-07-12 14:11:25 +08:00
|
|
|
filePath = c.loadLogPath("freshclam-log")
|
2024-07-08 15:47:23 +08:00
|
|
|
if len(filePath) != 0 {
|
|
|
|
break
|
|
|
|
}
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath = "/var/log/clamav/freshclam.log"
|
|
|
|
} else {
|
2024-07-05 16:52:39 +08:00
|
|
|
filePath = "/var/log/freshclam.log"
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("not support such type")
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(filePath); err != nil {
|
|
|
|
return "", buserr.New("ErrHttpReqNotFound")
|
|
|
|
}
|
2024-07-08 15:47:23 +08:00
|
|
|
var tail string
|
|
|
|
if req.Tail != "0" {
|
|
|
|
tail = req.Tail
|
|
|
|
} else {
|
|
|
|
tail = "+1"
|
|
|
|
}
|
|
|
|
cmd := exec.Command("tail", "-n", tail, filePath)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
2024-06-24 13:52:42 +08:00
|
|
|
if err != nil {
|
2024-07-08 15:47:23 +08:00
|
|
|
return "", fmt.Errorf("tail -n %v failed, err: %v", req.Tail, err)
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
2024-07-08 15:47:23 +08:00
|
|
|
return string(stdout), nil
|
2024-06-24 13:52:42 +08:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) UpdateFile(req dto.UpdateByNameAndFile) error {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath := ""
|
|
|
|
service := ""
|
|
|
|
switch req.Name {
|
|
|
|
case "clamd":
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-06-26 22:40:52 +08:00
|
|
|
service = clamServiceNameUbuntu
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath = "/etc/clamav/clamd.conf"
|
|
|
|
} else {
|
|
|
|
service = clamServiceNameCentOs
|
|
|
|
filePath = "/etc/clamd.d/scan.conf"
|
|
|
|
}
|
|
|
|
case "freshclam":
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-06-24 13:52:42 +08:00
|
|
|
filePath = "/etc/clamav/freshclam.conf"
|
|
|
|
} else {
|
|
|
|
filePath = "/etc/freshclam.conf"
|
|
|
|
}
|
|
|
|
service = "clamav-freshclam.service"
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("not support such type")
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC, 0640)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
|
|
|
_, _ = write.WriteString(req.File)
|
|
|
|
write.Flush()
|
|
|
|
|
|
|
|
_ = systemctl.Restart(service)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 17:38:47 +08:00
|
|
|
func StopAllCronJob(withCheck bool) bool {
|
|
|
|
if withCheck {
|
|
|
|
isActive := false
|
|
|
|
exist1, _ := systemctl.IsExist(clamServiceNameCentOs)
|
|
|
|
if exist1 {
|
|
|
|
isActive, _ = systemctl.IsActive(clamServiceNameCentOs)
|
|
|
|
}
|
|
|
|
exist2, _ := systemctl.IsExist(clamServiceNameUbuntu)
|
|
|
|
if exist2 {
|
|
|
|
isActive, _ = systemctl.IsActive(clamServiceNameUbuntu)
|
|
|
|
}
|
|
|
|
if isActive {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 23:16:30 +08:00
|
|
|
clams, _ := clamRepo.List(commonRepo.WithByStatus(constant.StatusEnable))
|
|
|
|
for i := 0; i < len(clams); i++ {
|
|
|
|
global.Cron.Remove(cron.EntryID(clams[i].EntryID))
|
|
|
|
_ = clamRepo.Update(clams[i].ID, map[string]interface{}{"status": constant.StatusDisable, "entry_id": 0})
|
|
|
|
}
|
2024-08-20 17:38:47 +08:00
|
|
|
return true
|
2024-08-15 23:16:30 +08:00
|
|
|
}
|
|
|
|
|
2024-06-24 13:52:42 +08:00
|
|
|
func loadFileByName(name string) []string {
|
|
|
|
var logPaths []string
|
2024-07-02 17:25:25 +08:00
|
|
|
pathItem := path.Join(global.CONF.System.DataDir, resultDir, name)
|
2024-06-24 13:52:42 +08:00
|
|
|
_ = filepath.Walk(pathItem, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if info.IsDir() || info.Name() == name {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logPaths = append(logPaths, info.Name())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return logPaths
|
|
|
|
}
|
|
|
|
func loadResultFromLog(pathItem string) dto.ClamLog {
|
|
|
|
var data dto.ClamLog
|
|
|
|
data.Name = path.Base(pathItem)
|
|
|
|
data.Status = constant.StatusWaiting
|
|
|
|
file, err := os.ReadFile(pathItem)
|
|
|
|
if err != nil {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(file), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.Contains(line, "- SCAN SUMMARY -") {
|
|
|
|
data.Status = constant.StatusDone
|
|
|
|
}
|
|
|
|
if data.Status != constant.StatusDone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(line, "Infected files:"):
|
|
|
|
data.InfectedFiles = strings.TrimPrefix(line, "Infected files:")
|
2024-07-04 16:00:32 +08:00
|
|
|
case strings.HasPrefix(line, "Total errors:"):
|
|
|
|
data.TotalError = strings.TrimPrefix(line, "Total errors:")
|
2024-06-24 13:52:42 +08:00
|
|
|
case strings.HasPrefix(line, "Time:"):
|
|
|
|
if strings.Contains(line, "(") {
|
|
|
|
data.ScanTime = strings.ReplaceAll(strings.Split(line, "(")[1], ")", "")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data.ScanTime = strings.TrimPrefix(line, "Time:")
|
|
|
|
case strings.HasPrefix(line, "Start Date:"):
|
|
|
|
data.ScanDate = strings.TrimPrefix(line, "Start Date:")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2024-07-12 14:11:25 +08:00
|
|
|
func (c *ClamService) loadLogPath(name string) string {
|
2024-07-08 15:47:23 +08:00
|
|
|
confPath := ""
|
|
|
|
if name == "clamd-log" {
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-07-08 15:47:23 +08:00
|
|
|
confPath = "/etc/clamav/clamd.conf"
|
|
|
|
} else {
|
|
|
|
confPath = "/etc/clamd.d/scan.conf"
|
|
|
|
}
|
|
|
|
} else {
|
2024-07-12 14:11:25 +08:00
|
|
|
if c.serviceName == clamServiceNameUbuntu {
|
2024-07-08 15:47:23 +08:00
|
|
|
confPath = "/etc/clamav/freshclam.conf"
|
|
|
|
} else {
|
|
|
|
confPath = "/etc/freshclam.conf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(confPath); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
content, err := os.ReadFile(confPath)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(content), "\n")
|
|
|
|
if name == "clamd-log" {
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "LogFile ") {
|
|
|
|
return strings.Trim(strings.ReplaceAll(line, "LogFile ", ""), " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "UpdateLogFile ") {
|
|
|
|
return strings.Trim(strings.ReplaceAll(line, "UpdateLogFile ", ""), " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|