-
Notifications
You must be signed in to change notification settings - Fork 5
/
agent.go
186 lines (158 loc) · 4.66 KB
/
agent.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package db
import (
"time"
"github.com/google/uuid"
"github.com/lachlan2k/phatcrack/api/internal/util"
"github.com/lachlan2k/phatcrack/common/pkg/apitypes"
"github.com/lachlan2k/phatcrack/common/pkg/hashcattypes"
"gorm.io/datatypes"
)
const (
AgentStatusHealthy = "AgentStatusHealthy"
AgentStatusUnhealthyButConnected = "AgentStatusUnhealthyButConnected"
AgentStatusUnhealthyAndDisconnected = "AgentStatusUnhealthyAndDisconnected"
AgentStatusDead = "AgentStatusDead"
)
type Agent struct {
UUIDBaseModel
Name string
KeyHash string
IsMaintenanceMode bool `gorm:"default:false; not null"`
AgentInfo datatypes.JSONType[AgentInfo]
AgentDevices datatypes.JSONType[AgentDeviceInfo]
}
type AgentFile struct {
Name string `json:"name"`
Size int64 `json:"size"`
}
type AgentDeviceInfo struct {
Devices []hashcattypes.HashcatStatusDevice
}
type AgentInfo struct {
Status string `json:"status"`
Version string `json:"version"`
TimeOfLastHeartbeat time.Time `json:"time_of_last_heartbeat,omitempty"`
TimeOfLastDisconnect time.Time `json:"time_of_last_disconnect,omitempty"`
TimeOfLastConnect time.Time `json:"time_of_last_connect,omitempty"`
AvailableListfiles []AgentFile `json:"available_listfiles,omitempty"`
ActiveJobIDs []string `json:"active_job_ids,omitempty"`
}
func (a AgentFile) ToDTO() apitypes.AgentFileDTO {
return apitypes.AgentFileDTO{
Name: a.Name,
Size: a.Size,
}
}
func (a AgentInfo) ToDTO() apitypes.AgentInfoDTO {
listfileDTOs := make([]apitypes.AgentFileDTO, len(a.AvailableListfiles))
for i, f := range a.AvailableListfiles {
listfileDTOs[i] = f.ToDTO()
}
return apitypes.AgentInfoDTO{
Status: a.Status,
Version: a.Version,
LastCheckInTime: a.TimeOfLastHeartbeat.Unix(),
AvailableListfiles: listfileDTOs,
ActiveJobIDs: a.ActiveJobIDs,
}
}
func (a Agent) ToDTO() apitypes.AgentDTO {
return apitypes.AgentDTO{
ID: a.ID.String(),
Name: a.Name,
IsMaintenanceMode: a.IsMaintenanceMode,
AgentInfo: a.AgentInfo.Data().ToDTO(),
AgentDevices: a.AgentDevices.Data().Devices,
}
}
func CreateAgent(name string) (newAgent *Agent, plaintextKey string, err error) {
plaintextKey, keyHash, err := util.GenAgentKeyAndHash()
if err != nil {
return
}
agent := &Agent{
Name: name,
KeyHash: keyHash,
}
err = GetInstance().Create(agent).Error
if err != nil {
return
}
newAgent = agent
return
}
func GetAllAgents() ([]Agent, error) {
agents := []Agent{}
err := GetInstance().Find(&agents).Error
if err != nil {
return nil, err
}
return agents, nil
}
func GetAllHealthyAgents() ([]Agent, error) {
agents := []Agent{}
err := GetInstance().Find(&agents, "agent_info->>'status' = ?", AgentStatusHealthy).Error
if err != nil {
return nil, err
}
return agents, nil
}
func GetAllSchedulableAgents() ([]Agent, error) {
agents := []Agent{}
err := GetInstance().Find(&agents, "agent_info->>'status' = ? and is_maintenance_mode = false", AgentStatusHealthy).Error
if err != nil {
return nil, err
}
return agents, nil
}
func GetAgent(id string) (*Agent, error) {
var agent Agent
err := GetInstance().First(&agent, "id = ?", id).Error
if err != nil {
return nil, err
}
return &agent, nil
}
func FindAgentByAuthKey(authKey string) (*Agent, error) {
keyHash := util.HashAgentKey(authKey)
agent := &Agent{}
err := GetInstance().Where(&Agent{KeyHash: keyHash}).First(agent).Error
if err != nil {
return nil, err
}
return agent, nil
}
func FindAgentIDByAuthKey(authKey string) (string, error) {
var result struct {
ID uuid.UUID
}
keyHash := util.HashAgentKey(authKey)
err := GetInstance().Model(&Agent{}).Where(&Agent{KeyHash: keyHash}).First(&result).Error
if err != nil {
return "", err
}
return result.ID.String(), nil
}
func UpdateAgentDevices(agentId string, devices []hashcattypes.HashcatStatusDevice) error {
return GetInstance().
Table("agents").
Where("id", agentId).
Update("agent_devices", AgentDeviceInfo{
Devices: devices,
}).Error
}
func UpdateAgentStatus(agentId string, status string) error {
return GetInstance().
Table("agents").
Where("id", agentId).
UpdateColumn("agent_info",
datatypes.JSONSet("agent_info").Set("{status}", status),
).
Error
}
func UpdateAgentInfo(agentId string, info AgentInfo) error {
return GetInstance().Table("agents").Where("id", agentId).Update("agent_info", info).Error
}
func UpdateAgentMaintenanceMode(agentId string, isMaintenance bool) error {
return GetInstance().Table("agents").Where("id", agentId).Update("is_maintenance_mode", isMaintenance).Error
}