[scanner] 规范统一接口

This commit is contained in:
wuko233 2026-04-01 20:58:20 +08:00
parent 880cbb6db9
commit 8f16df10dd
4 changed files with 30 additions and 29 deletions

View File

@ -5,7 +5,6 @@ import (
"os" "os"
"sysmonitord/internal/config" "sysmonitord/internal/config"
"sysmonitord/internal/scanner/file" "sysmonitord/internal/scanner/file"
"sysmonitord/internal/scanner/hash"
"sysmonitord/internal/scanner/process" "sysmonitord/internal/scanner/process"
"sysmonitord/internal/storage" "sysmonitord/internal/storage"
"sysmonitord/pkg/logger" "sysmonitord/pkg/logger"
@ -32,12 +31,6 @@ var StartCmd = &cobra.Command{
zap.String("审计服务器地址", fmt.Sprintf("%s:%d", cfg.Audit.Server, cfg.Audit.Port)), zap.String("审计服务器地址", fmt.Sprintf("%s:%d", cfg.Audit.Server, cfg.Audit.Port)),
) )
hashCfg := &hash.Config{
UseFastHash: cfg.Scanner.File.FastHash,
Threshold: cfg.Scanner.File.FastHashSize,
ChunkSize: cfg.Scanner.File.FastHashChunk,
}
storageCfg := &storage.Storage{ storageCfg := &storage.Storage{
DataDir: cfg.Storage.DataDir, DataDir: cfg.Storage.DataDir,
ProcessSystemFile: cfg.Storage.ProcessSystemFile, ProcessSystemFile: cfg.Storage.ProcessSystemFile,
@ -46,7 +39,8 @@ var StartCmd = &cobra.Command{
// ====== 进程扫描和存储 ====== // ====== 进程扫描和存储 ======
procs, err := process.ScanAllProcesses(hashCfg) startTime := time.Now()
procs, err := process.ScanAllProcesses(cfg)
if err != nil { if err != nil {
logger.Log.Error("扫描进程失败", zap.Error(err)) logger.Log.Error("扫描进程失败", zap.Error(err))
os.Exit(1) os.Exit(1)
@ -75,7 +69,7 @@ var StartCmd = &cobra.Command{
// ====== 文件扫描和存储 ====== // ====== 文件扫描和存储 ======
logger.Log.Info("正在扫描文件系统...") logger.Log.Info("正在扫描文件系统...")
startTime := time.Now() startTime = time.Now()
fileScanner := file.NewScanner(cfg) fileScanner := file.NewScanner(cfg)
files, err := fileScanner.Scan() files, err := fileScanner.Scan()

View File

@ -85,7 +85,7 @@ func (s *Scanner) WalkFunc(result *[]FileInfo) fs.WalkDirFunc {
return nil return nil
} }
hash, err := hash.CalculateHash(path, hashCfg) hash, err := hash.Calculate(path, info.Size(), hashCfg)
if err != nil { if err != nil {
logger.Log.Debug("[scan]无法计算文件哈希", zap.String("path", path), zap.Error(err)) logger.Log.Debug("[scan]无法计算文件哈希", zap.String("path", path), zap.Error(err))
return nil return nil

View File

@ -56,34 +56,33 @@ type Config struct {
} }
// ==== 计算文件哈希 ==== // ==== 计算文件哈希 ====
func Calculate(filePath string, fileSize int64, cfg *Config) (string, error) {
func CalculateHash(filePath string, cfg *Config) (string, error) { if cfg == nil {
info, err := os.Stat(filePath) cfg = &Config{
if err != nil { Algorithm: &SHA256Algorithm{},
logger.Log.Warn("[hash]获取文件信息失败", zap.String("path", filePath), zap.Error(err)) }
return "", err
} }
fileSize := info.Size() if fileSize == 0 {
info, err := os.Stat(filePath)
if err != nil {
logger.Log.Warn("[scanner]获取文件信息失败", zap.String("path", filePath), zap.Error(err))
return "", err
}
fileSize = info.Size()
}
if cfg.Algorithm == nil { if cfg.Algorithm == nil {
cfg.Algorithm = &SHA256Algorithm{} cfg.Algorithm = &SHA256Algorithm{}
} }
logger.Log.Debug("[hash]计算文件哈希", logger.Log.Debug("[scanner]计算文件哈希", zap.String("path", filePath), zap.Int64("size", fileSize), zap.String("algorithm", cfg.Algorithm.Name()))
zap.String("path", filePath),
zap.Int64("fileSize", fileSize),
zap.String("Algorithm", cfg.Algorithm.Name()))
if cfg.UseFastHash && fileSize > cfg.Threshold { if cfg.UseFastHash && fileSize > cfg.Threshold {
logger.Log.Debug("[hash] 分层哈希...",
zap.String("path", filePath),
zap.Int64("fileSize", fileSize),
)
return calculateFast(filePath, fileSize, cfg) return calculateFast(filePath, fileSize, cfg)
} else {
return calculateFull(filePath, cfg)
} }
return calculateFull(filePath, cfg)
} }
func calculateFull(filePath string, cfg *Config) (string, error) { func calculateFull(filePath string, cfg *Config) (string, error) {

View File

@ -3,6 +3,7 @@ package process
import ( import (
"fmt" "fmt"
"os" "os"
"sysmonitord/internal/config"
"sysmonitord/internal/scanner/hash" "sysmonitord/internal/scanner/hash"
"sysmonitord/pkg/logger" "sysmonitord/pkg/logger"
@ -18,7 +19,7 @@ type ProcessInfo struct {
FileHash string `json:"file_hash"` FileHash string `json:"file_hash"`
} }
func ScanAllProcesses(hashCfg *hash.Config) ([]ProcessInfo, error) { func ScanAllProcesses(cfg *config.Config) ([]ProcessInfo, error) {
logger.Log.Info("[scan]正在扫描系统中的所有进程...") logger.Log.Info("[scan]正在扫描系统中的所有进程...")
pids, err := process.Pids() pids, err := process.Pids()
@ -28,6 +29,13 @@ func ScanAllProcesses(hashCfg *hash.Config) ([]ProcessInfo, error) {
} }
var processList []ProcessInfo var processList []ProcessInfo
hashCfg, err := cfg.GetHashConfig()
if err != nil {
logger.Log.Error("[scan]获取哈希配置失败", zap.Error(err))
return nil, err
}
for _, pid := range pids { for _, pid := range pids {
p, err := process.NewProcess(pid) p, err := process.NewProcess(pid)
if err != nil { if err != nil {
@ -58,7 +66,7 @@ func ScanAllProcesses(hashCfg *hash.Config) ([]ProcessInfo, error) {
if exePath != "" { if exePath != "" {
if _, err := os.Stat(exePath); err == nil { if _, err := os.Stat(exePath); err == nil {
fileHash, err := hash.CalculateHash(exePath, hashCfg) fileHash, err := hash.Calculate(exePath, 0, hashCfg)
if err == nil { if err == nil {
info.FileHash = fileHash info.FileHash = fileHash
} else { } else {