- 新增 StopAndParseCapture 函数,整合停止抓包和解析数据流程 -重构 ReadRawJsonFile 函数,返回解析后的 ParsedResult 对象 - 优化数据解析流程,提高代码可读性和性能 - 调整前端 CapturePage组件,使用新的解析接口
134 lines
3.0 KiB
Go
134 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"equipment-analyzer/internal/capture"
|
|
"equipment-analyzer/internal/config"
|
|
"equipment-analyzer/internal/model"
|
|
"equipment-analyzer/internal/utils"
|
|
)
|
|
|
|
type CaptureService struct {
|
|
config *config.Config
|
|
logger *utils.Logger
|
|
packetCapture *capture.PacketCapture
|
|
processor *capture.TCPProcessor
|
|
mutex sync.RWMutex
|
|
isCapturing bool
|
|
dataChan chan *model.CaptureResult
|
|
errorChan chan error
|
|
}
|
|
|
|
func NewCaptureService(cfg *config.Config, logger *utils.Logger) *CaptureService {
|
|
return &CaptureService{
|
|
config: cfg,
|
|
logger: logger,
|
|
packetCapture: capture.NewPacketCapture(),
|
|
processor: capture.NewTCPProcessor(),
|
|
dataChan: make(chan *model.CaptureResult, 100),
|
|
errorChan: make(chan error, 100),
|
|
}
|
|
}
|
|
|
|
// StartCapture 开始抓包
|
|
func (cs *CaptureService) StartCapture(ctx context.Context, config capture.Config) error {
|
|
cs.mutex.Lock()
|
|
defer cs.mutex.Unlock()
|
|
|
|
if cs.isCapturing {
|
|
return fmt.Errorf("capture already running")
|
|
}
|
|
|
|
if err := cs.packetCapture.Start(config); err != nil {
|
|
return fmt.Errorf("failed to start capture: %w", err)
|
|
}
|
|
|
|
cs.isCapturing = true
|
|
cs.logger.Info("Packet capture started", "interface", config.InterfaceName)
|
|
|
|
// 启动数据处理协程
|
|
go cs.processData(ctx)
|
|
|
|
return nil
|
|
}
|
|
|
|
// StopCapture 停止抓包
|
|
func (cs *CaptureService) StopCapture() error {
|
|
cs.mutex.Lock()
|
|
defer cs.mutex.Unlock()
|
|
|
|
if !cs.isCapturing {
|
|
return fmt.Errorf("capture not running")
|
|
}
|
|
|
|
cs.packetCapture.Stop()
|
|
cs.isCapturing = false
|
|
cs.logger.Info("Packet capture stopped")
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetCapturedData 获取抓包数据
|
|
func (cs *CaptureService) GetCapturedData() []string {
|
|
return cs.packetCapture.GetCapturedData()
|
|
}
|
|
|
|
// ProcessAllData 处理所有数据
|
|
func (cs *CaptureService) ProcessAllData() {
|
|
cs.packetCapture.ProcessAllData()
|
|
}
|
|
|
|
// IsCapturing 检查是否正在抓包
|
|
func (cs *CaptureService) IsCapturing() bool {
|
|
cs.mutex.RLock()
|
|
defer cs.mutex.RUnlock()
|
|
return cs.isCapturing
|
|
}
|
|
|
|
// processData 处理抓包数据
|
|
func (cs *CaptureService) processData(ctx context.Context) {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
// 这里可以添加实时数据处理逻辑
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}
|
|
}
|
|
|
|
// StopAndParseCapture 停止抓包并解析数据
|
|
func (cs *CaptureService) StopAndParseCapture(parser *ParserService) (*model.ParsedResult, error) {
|
|
cs.mutex.Lock()
|
|
defer cs.mutex.Unlock()
|
|
|
|
if !cs.isCapturing {
|
|
return nil, fmt.Errorf("capture not running")
|
|
}
|
|
|
|
cs.packetCapture.Stop()
|
|
cs.isCapturing = false
|
|
cs.logger.Info("Packet capture stopped (StopAndParseCapture)")
|
|
|
|
// 处理所有收集的数据
|
|
cs.packetCapture.ProcessAllData()
|
|
|
|
// 获取抓包数据
|
|
rawData := cs.packetCapture.GetCapturedData()
|
|
if len(rawData) == 0 {
|
|
return nil, fmt.Errorf("no captured data")
|
|
}
|
|
|
|
// 解析数据
|
|
result, _, err := parser.ParseHexData(rawData)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("解析数据失败: %v", err)
|
|
}
|
|
return result, nil
|
|
}
|