2025-02-06 14:42:06 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-03 15:07:01 +02:00
|
|
|
"log"
|
2025-02-06 14:42:06 +01:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AgentInfo struct {
|
2025-04-07 11:09:28 +02:00
|
|
|
Type string
|
2025-04-22 23:54:40 +02:00
|
|
|
ID string
|
2025-04-21 15:45:19 +02:00
|
|
|
Listener string
|
2025-02-06 14:42:06 +01:00
|
|
|
OSVersion string
|
|
|
|
|
Architecture string
|
|
|
|
|
Hostname string
|
|
|
|
|
Username string
|
|
|
|
|
LocalIP string
|
2025-04-22 23:54:40 +02:00
|
|
|
Procname string
|
|
|
|
|
PID string
|
2025-02-06 14:42:06 +01:00
|
|
|
RemoteIP string
|
|
|
|
|
LastConnection time.Time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var agents sync.Map
|
|
|
|
|
|
|
|
|
|
// Update or create agent information with new data
|
2025-04-01 20:28:14 +02:00
|
|
|
func UpdateAgent(agentID string, newInfo *AgentInfo) {
|
2025-04-06 09:36:31 +02:00
|
|
|
if existingInfo, exists := GetAgent(agentID); exists {
|
2025-02-06 14:42:06 +01:00
|
|
|
// Update existing agent's fields with new data if provided
|
|
|
|
|
if newInfo.OSVersion != "" {
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.OSVersion = newInfo.OSVersion
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
if newInfo.Architecture != "" {
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.Architecture = newInfo.Architecture
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
if newInfo.Hostname != "" {
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.Hostname = newInfo.Hostname
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
if newInfo.Username != "" {
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.Username = newInfo.Username
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
if newInfo.LocalIP != "" {
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.LocalIP = newInfo.LocalIP
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
2025-04-22 23:54:40 +02:00
|
|
|
if newInfo.Procname != "" {
|
|
|
|
|
existingInfo.Procname = newInfo.Procname
|
|
|
|
|
}
|
|
|
|
|
if newInfo.PID != "" {
|
|
|
|
|
existingInfo.PID = newInfo.PID
|
|
|
|
|
}
|
2025-02-06 14:42:06 +01:00
|
|
|
if newInfo.RemoteIP != "" {
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.RemoteIP = newInfo.RemoteIP
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
// Always update the last connection time
|
2025-04-06 09:36:31 +02:00
|
|
|
existingInfo.LastConnection = time.Now()
|
|
|
|
|
|
|
|
|
|
agents.Store(agentID, existingInfo)
|
2025-08-03 15:07:01 +02:00
|
|
|
log.Printf("Updated agent entry for %s", agentID)
|
2025-02-06 14:42:06 +01:00
|
|
|
} else {
|
|
|
|
|
// If agent doesn't exist, use the newInfo struct to create a new agent
|
|
|
|
|
agents.Store(agentID, newInfo)
|
2025-08-03 15:07:01 +02:00
|
|
|
log.Printf("Created new agent entry for %s", agentID)
|
2025-04-06 09:36:31 +02:00
|
|
|
// Send new list of agents to operators to update tab completion
|
|
|
|
|
UpdateAgentsTabCompletion()
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get agent info by name
|
2025-04-06 09:36:31 +02:00
|
|
|
func GetAgent(agentID string) (*AgentInfo, bool) {
|
2025-02-06 14:42:06 +01:00
|
|
|
value, exists := agents.Load(agentID)
|
|
|
|
|
if !exists {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
return value.(*AgentInfo), true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get agents list of agents' names
|
|
|
|
|
func GetAgentNames() []string {
|
|
|
|
|
var names []string
|
|
|
|
|
agents.Range(func(key, value any) bool {
|
|
|
|
|
names = append(names, key.(string))
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return names
|
|
|
|
|
}
|