-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
122 lines (102 loc) · 2.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"embed"
"fmt"
"io/fs"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
//go:embed frontend/dist/*
var frontendFS embed.FS
// Process 进程信息结构
type Process struct {
PID uint32 `json:"pid"`
Name string `json:"name"`
}
// MemorySearchRequest 内存搜索请求
type MemorySearchRequest struct {
PID uint32 `json:"pid"`
Value interface{} `json:"value"`
DataType string `json:"dataType"` // int32, int64, float32, float64
Operation string `json:"operation"` // equal, greater, less, increased, decreased
PreviousResults []MemoryResult `json:"previousResults"`
}
// MemoryModifyRequest 内存修改请求
type MemoryModifyRequest struct {
PID uint32 `json:"pid"`
Address string `json:"address"`
Value interface{} `json:"value"`
DataType string `json:"dataType"`
}
func main() {
r := gin.Default()
// API路由
api := r.Group("/api")
{
api.GET("/processes", getProcessList)
api.POST("/search", searchMemory)
api.POST("/modify", modifyMemory)
}
// 静态文件服务
// 获取前端文件系统
frontend, err := fs.Sub(frontendFS, "frontend/dist")
if err != nil {
panic(err)
}
// 设置静态文件服务
r.StaticFS("/static", http.FS(frontend))
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/static/index.html")
})
fmt.Println("Starting server at http://localhost:8080")
r.Run(":8080")
}
// getProcessList 获取进程列表
func getProcessList(c *gin.Context) {
processes, err := GetProcessList()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, processes)
}
// searchMemory 搜索内存
func searchMemory(c *gin.Context) {
var req MemorySearchRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 将字符串值转换为数值
if strValue, ok := req.Value.(string); ok {
if floatValue, err := strconv.ParseFloat(strValue, 64); err == nil {
req.Value = floatValue
}
}
results, err := ScanMemory(req.PID, req.Value, req.DataType, req.Operation, req.PreviousResults)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, results)
}
// modifyMemory 修改内存
func modifyMemory(c *gin.Context) {
var req MemoryModifyRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 将字符串值转换为数值
if strValue, ok := req.Value.(string); ok {
if floatValue, err := strconv.ParseFloat(strValue, 64); err == nil {
req.Value = floatValue
}
}
if err := ModifyMemory(req.PID, req.Address, req.Value, req.DataType); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"success": true})
}