1
1
package service
2
2
3
3
import (
4
- "bufio"
5
4
"fmt"
6
5
"os"
7
6
"os/exec"
@@ -28,14 +27,14 @@ import (
28
27
)
29
28
30
29
const (
31
- clamServiceNameCentOs = "[email protected] "
32
- clamServiceNameUbuntu = "clamav-daemon.service"
33
- freshClamService = "clamav-freshclam.service"
34
- resultDir = "clamav"
30
+ clamServiceKey = "clam"
31
+ freshClamServiceKey = "freshclam"
32
+ resultDir = "clamav"
35
33
)
36
34
37
35
type ClamService struct {
38
- serviceName string
36
+ serviceName string
37
+ freshClamService string
39
38
}
40
39
41
40
type IClamService interface {
@@ -63,23 +62,32 @@ func (c *ClamService) LoadBaseInfo() (dto.ClamBaseInfo, error) {
63
62
var baseInfo dto.ClamBaseInfo
64
63
baseInfo .Version = "-"
65
64
baseInfo .FreshVersion = "-"
66
- exist1 , _ := systemctl .IsExist (clamServiceNameCentOs )
67
- if exist1 {
68
- c .serviceName = clamServiceNameCentOs
69
- baseInfo .IsExist = true
70
- baseInfo .IsActive , _ = systemctl .IsActive (clamServiceNameCentOs )
71
- }
72
- exist2 , _ := systemctl .IsExist (clamServiceNameUbuntu )
73
- if exist2 {
74
- c .serviceName = clamServiceNameUbuntu
75
- baseInfo .IsExist = true
76
- baseInfo .IsActive , _ = systemctl .IsActive (clamServiceNameUbuntu )
77
- }
78
- freshExist , _ := systemctl .IsExist (freshClamService )
79
- if freshExist {
80
- baseInfo .FreshIsExist = true
81
- baseInfo .FreshIsActive , _ = systemctl .IsActive (freshClamService )
65
+ clamSvc , err := systemctl .GetServiceName (clamServiceKey )
66
+ if err != nil {
67
+ baseInfo .IsExist = false
68
+ return baseInfo , nil
69
+ }
70
+ c .serviceName = clamSvc
71
+ isExist , err := systemctl .IsExist (c .serviceName )
72
+ if err != nil {
73
+ baseInfo .IsExist = false
74
+ }
75
+ baseInfo .IsExist = isExist
76
+ baseInfo .IsActive , _ = systemctl .IsActive (clamSvc )
77
+
78
+ freshSvc , err := systemctl .GetServiceName (freshClamServiceKey )
79
+ if err != nil {
80
+ baseInfo .FreshIsExist = false
81
+ return baseInfo , nil
82
+ }
83
+ c .freshClamService = freshSvc
84
+ freshisExist , err := systemctl .IsExist (c .freshClamService )
85
+ if err != nil {
86
+ baseInfo .FreshIsExist = false
82
87
}
88
+ baseInfo .FreshIsExist = freshisExist
89
+ baseInfo .FreshIsActive , _ = systemctl .IsActive (freshSvc )
90
+
83
91
if ! cmd .Which ("clamdscan" ) {
84
92
baseInfo .IsActive = false
85
93
}
@@ -110,22 +118,27 @@ func (c *ClamService) LoadBaseInfo() (dto.ClamBaseInfo, error) {
110
118
}
111
119
112
120
func (c * ClamService ) Operate (operate string ) error {
121
+ var err error
113
122
switch operate {
114
- case "start" , "restart" , "stop" :
115
- stdout , err := cmd . Execf ( "systemctl %s %s" , operate , c .serviceName )
116
- if err != nil {
117
- return fmt . Errorf ( "%s the %s failed, err: %s" , operate , c .serviceName , stdout )
118
- }
119
- return nil
120
- case "fresh-start" , "fresh-restart" , "fresh-stop" :
121
- stdout , err := cmd . Execf ( " systemctl %s %s" , strings . TrimPrefix ( operate , "fresh-" ), freshClamService )
122
- if err != nil {
123
- return fmt . Errorf ( "%s the %s failed, err: %s" , operate , c . serviceName , stdout )
124
- }
125
- return nil
123
+ case "start" :
124
+ err = systemctl . Start ( c .serviceName )
125
+ case "stop" :
126
+ err = systemctl . Stop ( c .serviceName )
127
+ case "restart" :
128
+ err = systemctl . Restart ( c . serviceName )
129
+ case "fresh-start" :
130
+ err = systemctl . Start ( c . freshClamService )
131
+ case "fresh-stop" :
132
+ err = systemctl . Stop ( c . freshClamService )
133
+ case "fresh-restart" :
134
+ err = systemctl . Restart ( c . freshClamService )
126
135
default :
127
- return fmt .Errorf ("not support such operation: %v " , operate )
136
+ return fmt .Errorf ("unsupported operation: %s " , operate )
128
137
}
138
+ if err != nil {
139
+ return fmt .Errorf ("%s %s failed: %v" , operate , c .serviceName , err )
140
+ }
141
+ return nil
129
142
}
130
143
131
144
func (c * ClamService ) SearchWithPage (req dto.SearchClamWithPage ) (int64 , interface {}, error ) {
@@ -432,102 +445,80 @@ func (c *ClamService) LoadFile(req dto.ClamFileReq) (string, error) {
432
445
filePath := ""
433
446
switch req .Name {
434
447
case "clamd" :
435
- if c .serviceName == clamServiceNameUbuntu {
436
- filePath = "/etc/clamav/clamd.conf"
437
- } else {
438
- filePath = "/etc/clamd.d/scan.conf"
439
- }
448
+ filePath = c .getConfigPath ("clamd" )
440
449
case "clamd-log" :
441
450
filePath = c .loadLogPath ("clamd-log" )
442
- if len (filePath ) != 0 {
443
- break
444
- }
445
- if c .serviceName == clamServiceNameUbuntu {
446
- filePath = "/var/log/clamav/clamav.log"
447
- } else {
448
- filePath = "/var/log/clamd.scan"
449
- }
450
451
case "freshclam" :
451
- if c .serviceName == clamServiceNameUbuntu {
452
- filePath = "/etc/clamav/freshclam.conf"
453
- } else {
454
- filePath = "/etc/freshclam.conf"
455
- }
452
+ filePath = c .getConfigPath ("freshclam" )
456
453
case "freshclam-log" :
457
454
filePath = c .loadLogPath ("freshclam-log" )
458
- if len (filePath ) != 0 {
459
- break
460
- }
461
- if c .serviceName == clamServiceNameUbuntu {
462
- filePath = "/var/log/clamav/freshclam.log"
463
- } else {
464
- filePath = "/var/log/freshclam.log"
465
- }
466
455
default :
467
- return "" , fmt .Errorf ("not support such type" )
468
- }
469
- if _ , err := os .Stat (filePath ); err != nil {
470
- return "" , buserr .New ("ErrHttpReqNotFound" )
456
+ return "" , fmt .Errorf ("unsupported file type" )
471
457
}
472
- var tail string
473
- if req .Tail != "0" {
474
- tail = req .Tail
475
- } else {
476
- tail = "+1"
477
- }
478
- cmd := exec .Command ("tail" , "-n" , tail , filePath )
479
- stdout , err := cmd .CombinedOutput ()
458
+
459
+ content , err := systemctl .ViewConfig (filePath , systemctl.ConfigOption {TailLines : req .Tail })
480
460
if err != nil {
481
- return "" , fmt . Errorf ( "tail -n %v failed, err: %v" , req . Tail , err )
461
+ return "" , buserr . New ( "ErrHttpReqNotFound" )
482
462
}
483
- return string ( stdout ) , nil
463
+ return content , nil
484
464
}
485
465
486
466
func (c * ClamService ) UpdateFile (req dto.UpdateByNameAndFile ) error {
487
- filePath := ""
488
- service := ""
467
+ var (
468
+ filePath string
469
+ service string
470
+ )
471
+
489
472
switch req .Name {
490
473
case "clamd" :
491
- if c .serviceName == clamServiceNameUbuntu {
492
- service = clamServiceNameUbuntu
493
- filePath = "/etc/clamav/clamd.conf"
494
- } else {
495
- service = clamServiceNameCentOs
496
- filePath = "/etc/clamd.d/scan.conf"
497
- }
474
+ filePath = c .getConfigPath ("clamd" )
475
+ service = c .serviceName
498
476
case "freshclam" :
499
- if c .serviceName == clamServiceNameUbuntu {
500
- filePath = "/etc/clamav/freshclam.conf"
501
- } else {
502
- filePath = "/etc/freshclam.conf"
503
- }
504
- service = "clamav-freshclam.service"
477
+ filePath = c .getConfigPath ("freshclam" )
478
+ service = c .freshClamService
505
479
default :
506
- return fmt .Errorf ("not support such type" )
480
+ return fmt .Errorf ("unsupported file type" )
507
481
}
482
+
508
483
file , err := os .OpenFile (filePath , os .O_WRONLY | os .O_TRUNC , 0640 )
509
484
if err != nil {
510
485
return err
511
486
}
512
487
defer file .Close ()
513
- write := bufio .NewWriter (file )
514
- _ , _ = write .WriteString (req .File )
515
- write .Flush ()
516
488
517
- _ = systemctl .Restart (service )
489
+ if _ , err := file .WriteString (req .File ); err != nil {
490
+ return err
491
+ }
492
+
493
+ if err := systemctl .Restart (service ); err != nil {
494
+ return fmt .Errorf ("restart %s failed: %v" , service , err )
495
+ }
518
496
return nil
519
497
}
520
498
499
+ func (c * ClamService ) getConfigPath (confType string ) string {
500
+ switch confType {
501
+ case "clamd" :
502
+ if _ , err := os .Stat ("/etc/clamav/clamd.conf" ); err == nil {
503
+ return "/etc/clamav/clamd.conf"
504
+ }
505
+ return "/etc/clamd.d/scan.conf"
506
+ case "freshclam" :
507
+ if _ , err := os .Stat ("/etc/clamav/freshclam.conf" ); err == nil {
508
+ return "/etc/clamav/freshclam.conf"
509
+ }
510
+ return "/etc/freshclam.conf"
511
+ default :
512
+ return ""
513
+ }
514
+ }
515
+
521
516
func StopAllCronJob (withCheck bool ) bool {
522
517
if withCheck {
523
518
isActive := false
524
- exist1 , _ := systemctl .IsExist (clamServiceNameCentOs )
525
- if exist1 {
526
- isActive , _ = systemctl .IsActive (clamServiceNameCentOs )
527
- }
528
- exist2 , _ := systemctl .IsExist (clamServiceNameUbuntu )
529
- if exist2 {
530
- isActive , _ = systemctl .IsActive (clamServiceNameUbuntu )
519
+ isexist , _ := systemctl .IsExist (clamServiceKey )
520
+ if isexist {
521
+ isActive , _ = systemctl .IsActive (clamServiceKey )
531
522
}
532
523
if isActive {
533
524
return false
@@ -590,42 +581,25 @@ func loadResultFromLog(pathItem string) dto.ClamLog {
590
581
return data
591
582
}
592
583
func (c * ClamService ) loadLogPath (name string ) string {
593
- confPath := ""
594
- if name == "clamd-log" {
595
- if c .serviceName == clamServiceNameUbuntu {
596
- confPath = "/etc/clamav/clamd.conf"
597
- } else {
598
- confPath = "/etc/clamd.d/scan.conf"
599
- }
600
- } else {
601
- if c .serviceName == clamServiceNameUbuntu {
602
- confPath = "/etc/clamav/freshclam.conf"
603
- } else {
604
- confPath = "/etc/freshclam.conf"
605
- }
606
- }
607
- if _ , err := os .Stat (confPath ); err != nil {
608
- return ""
584
+ configKey := "clamd"
585
+ searchPrefix := "LogFile "
586
+ if name != "clamd-log" {
587
+ configKey = "freshclam"
588
+ searchPrefix = "UpdateLogFile "
609
589
}
590
+ confPath := c .getConfigPath (configKey )
591
+
610
592
content , err := os .ReadFile (confPath )
611
593
if err != nil {
594
+ global .LOG .Debugf ("Failed to read %s config: %v" , configKey , err )
612
595
return ""
613
596
}
614
- lines := strings .Split (string (content ), "\n " )
615
- if name == "clamd-log" {
616
- for _ , line := range lines {
617
- if strings .HasPrefix (line , "LogFile " ) {
618
- return strings .Trim (strings .ReplaceAll (line , "LogFile " , "" ), " " )
619
- }
620
- }
621
- } else {
622
- for _ , line := range lines {
623
- if strings .HasPrefix (line , "UpdateLogFile " ) {
624
- return strings .Trim (strings .ReplaceAll (line , "UpdateLogFile " , "" ), " " )
625
- }
597
+
598
+ for _ , line := range strings .Split (string (content ), "\n " ) {
599
+ if strings .HasPrefix (line , searchPrefix ) {
600
+ return strings .TrimSpace (strings .TrimPrefix (line , searchPrefix ))
626
601
}
627
602
}
628
-
629
603
return ""
630
604
}
631
605
0 commit comments