refactor: rename services to checks

pull/7/head
sundowndev 2022-11-17 15:26:15 +04:00
parent d4f3a89f62
commit 44b801f14a
10 changed files with 86 additions and 85 deletions

View File

@ -2,6 +2,7 @@ package analysis
import (
"fmt"
"github.com/sundowndev/covermyass/v2/lib/check"
"io"
"os"
"time"
@ -13,7 +14,7 @@ type Summary struct {
}
type Result struct {
Service string
Check check.Check
Path string
Size int64
Mode os.FileMode

View File

@ -3,10 +3,10 @@ package analysis
import (
"context"
"github.com/sirupsen/logrus"
"github.com/sundowndev/covermyass/v2/lib/check"
"github.com/sundowndev/covermyass/v2/lib/filter"
"github.com/sundowndev/covermyass/v2/lib/find"
"github.com/sundowndev/covermyass/v2/lib/output"
"github.com/sundowndev/covermyass/v2/lib/services"
"os"
"runtime"
"sync"
@ -24,15 +24,15 @@ func (a *Analyzer) Analyze() (*Analysis, error) {
analysis := NewAnalysis()
output.Printf("Loading known log files for %s\n", runtime.GOOS)
services.Init()
check.Init()
output.Printf("Scanning file system...\n\n")
wg := &sync.WaitGroup{}
m := &sync.Mutex{}
for _, service := range services.Services() {
for _, c := range check.GetAllChecks() {
wg.Add(1)
go func(svc services.Service) {
finder := find.New(os.DirFS(""), a.filter, svc.Paths())
go func(c check.Check) {
finder := find.New(os.DirFS(""), a.filter, c.Paths())
if err := finder.Run(context.TODO()); err != nil {
logrus.Error(err)
return
@ -42,7 +42,7 @@ func (a *Analyzer) Analyze() (*Analysis, error) {
defer m.Unlock()
for _, info := range finder.Results() {
analysis.AddResult(Result{
Service: svc.Name(),
Check: c,
Path: info.Path(),
Size: info.Size(),
Mode: info.Mode(),
@ -51,7 +51,7 @@ func (a *Analyzer) Analyze() (*Analysis, error) {
}
wg.Done()
}(service)
}(c)
}
wg.Wait()

View File

@ -1,4 +1,4 @@
package services
package check
import (
"fmt"
@ -7,17 +7,17 @@ import (
"os"
)
type ShellHistoryService struct{}
type shellHistoryCheck struct{}
func NewShellHistoryService() Service {
return &ShellHistoryService{}
func NewShellHistoryCheck() Check {
return &shellHistoryCheck{}
}
func (s *ShellHistoryService) Name() string {
func (s *shellHistoryCheck) Name() string {
return "shell_history"
}
func (s *ShellHistoryService) Paths() []string {
func (s *shellHistoryCheck) Paths() []string {
homeDir, err := os.UserHomeDir()
if err != nil {
logrus.Error(err)
@ -31,6 +31,6 @@ func (s *ShellHistoryService) Paths() []string {
}
}
func (s *ShellHistoryService) HandleFile(file find.FileInfo) error {
func (s *shellHistoryCheck) HandleFile(file find.FileInfo) error {
return os.Truncate(file.Path(), 0)
}

7
lib/check/init.go Normal file
View File

@ -0,0 +1,7 @@
package check
func Init() {
AddCheck(NewSSHdCheck())
AddCheck(NewLastLogCheck())
AddCheck(NewShellHistoryCheck())
}

View File

@ -0,0 +1,28 @@
//go:build !windows
package check
import (
"github.com/sundowndev/covermyass/v2/lib/find"
"os"
)
type lastLogCheck struct{}
func NewLastLogCheck() Check {
return &lastLogCheck{}
}
func (s *lastLogCheck) Name() string {
return "lastlog"
}
func (s *lastLogCheck) Paths() []string {
return []string{
"/var/log/lastlog",
}
}
func (s *lastLogCheck) HandleFile(file find.FileInfo) error {
return os.Truncate(file.Path(), 0)
}

View File

@ -1,4 +1,4 @@
package services
package check
import (
"github.com/sundowndev/covermyass/v2/lib/find"
@ -10,18 +10,18 @@ const (
Windows = "windows"
)
var services []Service
var checks []Check
type Service interface {
type Check interface {
Name() string
Paths() []string
HandleFile(find.FileInfo) error
}
func Services() []Service {
return services
func GetAllChecks() []Check {
return checks
}
func AddService(s Service) {
services = append(services, s)
func AddCheck(s Check) {
checks = append(checks, s)
}

28
lib/check/sshd_check.go Normal file
View File

@ -0,0 +1,28 @@
//go:build !windows
package check
import (
"github.com/sundowndev/covermyass/v2/lib/find"
"os"
)
type sshdCheck struct{}
func NewSSHdCheck() Check {
return &sshdCheck{}
}
func (s *sshdCheck) Name() string {
return "sshd"
}
func (s *sshdCheck) Paths() []string {
return []string{
"/var/log/sshd.log",
}
}
func (s *sshdCheck) HandleFile(file find.FileInfo) error {
return os.Truncate(file.Path(), 0)
}

View File

@ -1,7 +0,0 @@
package services
func Init() {
AddService(NewSSHdService())
AddService(NewLastLogService())
AddService(NewShellHistoryService())
}

View File

@ -1,28 +0,0 @@
//go:build !windows
package services
import (
"github.com/sundowndev/covermyass/v2/lib/find"
"os"
)
type LastLogService struct{}
func NewLastLogService() Service {
return &LastLogService{}
}
func (s *LastLogService) Name() string {
return "lastlog"
}
func (s *LastLogService) Paths() []string {
return []string{
"/var/log/lastlog",
}
}
func (s *LastLogService) HandleFile(file find.FileInfo) error {
return os.Truncate(file.Path(), 0)
}

View File

@ -1,28 +0,0 @@
//go:build !windows
package services
import (
"github.com/sundowndev/covermyass/v2/lib/find"
"os"
)
type SSHdService struct{}
func NewSSHdService() Service {
return &SSHdService{}
}
func (s *SSHdService) Name() string {
return "sshd"
}
func (s *SSHdService) Paths() []string {
return []string{
"/var/log/sshd.log",
}
}
func (s *SSHdService) HandleFile(file find.FileInfo) error {
return os.Truncate(file.Path(), 0)
}