169 lines
4.0 KiB
Go
169 lines
4.0 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"time"
|
||
|
||
"equipment-analyzer/internal/capture"
|
||
"equipment-analyzer/internal/config"
|
||
"equipment-analyzer/internal/model"
|
||
"equipment-analyzer/internal/utils"
|
||
)
|
||
|
||
type App struct {
|
||
config *config.Config
|
||
logger *utils.Logger
|
||
captureService *CaptureService
|
||
parserService *ParserService
|
||
}
|
||
|
||
func NewApp(cfg *config.Config, logger *utils.Logger) *App {
|
||
return &App{
|
||
config: cfg,
|
||
logger: logger,
|
||
captureService: NewCaptureService(cfg, logger),
|
||
parserService: NewParserService(cfg, logger),
|
||
}
|
||
}
|
||
|
||
func (a *App) Startup(ctx context.Context) {
|
||
a.logger.Info("应用启动")
|
||
}
|
||
|
||
func (a *App) DomReady(ctx context.Context) {
|
||
a.logger.Info("DOM准备就绪")
|
||
}
|
||
|
||
func (a *App) BeforeClose(ctx context.Context) (prevent bool) {
|
||
a.logger.Info("应用即将关闭")
|
||
return false
|
||
}
|
||
|
||
func (a *App) Shutdown(ctx context.Context) {
|
||
a.logger.Info("应用关闭")
|
||
}
|
||
|
||
// GetNetworkInterfaces 获取网络接口列表
|
||
func (a *App) GetNetworkInterfaces() ([]model.NetworkInterface, error) {
|
||
interfaces, err := capture.GetNetworkInterfaces()
|
||
if err != nil {
|
||
a.logger.Error("获取网络接口失败", "error", err)
|
||
return nil, err
|
||
}
|
||
return interfaces, nil
|
||
}
|
||
|
||
// StartCapture 开始抓包
|
||
func (a *App) StartCapture(interfaceName string) error {
|
||
if a.captureService.IsCapturing() {
|
||
return fmt.Errorf("抓包已在进行中")
|
||
}
|
||
|
||
config := capture.Config{
|
||
InterfaceName: interfaceName,
|
||
Filter: a.config.Capture.DefaultFilter,
|
||
Timeout: time.Duration(a.config.Capture.DefaultTimeout) * time.Millisecond,
|
||
BufferSize: a.config.Capture.BufferSize,
|
||
}
|
||
|
||
err := a.captureService.StartCapture(context.Background(), config)
|
||
if err != nil {
|
||
a.logger.Error("开始抓包失败", "error", err)
|
||
return err
|
||
}
|
||
|
||
a.logger.Info("抓包开始", "interface", interfaceName)
|
||
return nil
|
||
}
|
||
|
||
// StopCapture 停止抓包
|
||
func (a *App) StopCapture() error {
|
||
if !a.captureService.IsCapturing() {
|
||
return fmt.Errorf("没有正在进行的抓包")
|
||
}
|
||
|
||
err := a.captureService.StopCapture()
|
||
if err != nil {
|
||
a.logger.Error("停止抓包失败", "error", err)
|
||
return err
|
||
}
|
||
|
||
// 处理所有收集的数据
|
||
a.captureService.ProcessAllData()
|
||
|
||
a.logger.Info("抓包停止")
|
||
return nil
|
||
}
|
||
|
||
// GetCapturedData 获取抓包数据
|
||
func (a *App) GetCapturedData() ([]string, error) {
|
||
return a.captureService.GetCapturedData(), nil
|
||
}
|
||
|
||
// ParseData 解析数据为JSON
|
||
func (a *App) ParseData(hexDataList []string) (string, error) {
|
||
result, err := a.parserService.ParseHexData(hexDataList)
|
||
if err != nil {
|
||
a.logger.Error("解析数据失败", "error", err)
|
||
return "", err
|
||
}
|
||
|
||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||
if err != nil {
|
||
a.logger.Error("JSON序列化失败", "error", err)
|
||
return "", err
|
||
}
|
||
|
||
a.logger.Info("数据解析完成", "count", len(result.Data))
|
||
return string(jsonData), nil
|
||
}
|
||
|
||
// ExportData 导出数据到文件
|
||
func (a *App) ExportData(hexDataList []string, filename string) error {
|
||
result, err := a.parserService.ParseHexData(hexDataList)
|
||
if err != nil {
|
||
a.logger.Error("解析数据失败", "error", err)
|
||
return err
|
||
}
|
||
|
||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||
if err != nil {
|
||
a.logger.Error("JSON序列化失败", "error", err)
|
||
return err
|
||
}
|
||
|
||
// 这里可以添加文件写入逻辑
|
||
a.logger.Info("导出数据", "filename", filename, "count", len(result.Data))
|
||
|
||
// 简单示例:写入到当前目录
|
||
err = utils.WriteFile(filename, jsonData)
|
||
if err != nil {
|
||
a.logger.Error("写入文件失败", "error", err)
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// GetCaptureStatus 获取抓包状态
|
||
func (a *App) GetCaptureStatus() model.CaptureStatus {
|
||
return model.CaptureStatus{
|
||
IsCapturing: a.captureService.IsCapturing(),
|
||
Status: a.getStatusMessage(),
|
||
}
|
||
}
|
||
|
||
func (a *App) getStatusMessage() string {
|
||
if a.captureService.IsCapturing() {
|
||
return "正在抓包..."
|
||
}
|
||
return "准备就绪"
|
||
}
|
||
|
||
// ReadRawJsonFile 供前端调用,读取output_raw.json内容
|
||
func (a *App) ReadRawJsonFile() (string, error) {
|
||
return a.parserService.ReadRawJsonFile()
|
||
}
|