-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
135 lines (114 loc) · 3.43 KB
/
api.go
File metadata and controls
135 lines (114 loc) · 3.43 KB
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
package main
import (
"log"
"net/http"
"networkinator/models"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
func GetConnections(c *gin.Context) {
connections, err := GetAllConnections(db)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
connectionMap := make(map[string][]string)
for _, connection := range connections {
connectionMap[connection.ID] = []string{connection.Src, connection.Dst, strconv.Itoa(connection.Port), strconv.FormatFloat(connection.Count, 'f', -1, 64)}
}
c.JSON(http.StatusOK, connectionMap)
}
func AddConnection(jsonData map[string]interface{}) {
id := jsonData["ID"].(string)
src := jsonData["Src"].(string)
dst := jsonData["Dst"].(string)
port := jsonData["Port"].(string)
count := jsonData["Count"].(float64)
portInt, err := strconv.Atoi(port)
if err != nil || portInt < 0 || portInt > 65535 {
log.Println(err)
return
}
connection := models.Connection{}
tx := db.First(&connection, "ID = ?", id)
if tx.Error == nil {
log.Println("Connection already exists")
return
}
err = AddConnectionToDB(id, src, dst, portInt, count)
if err != nil {
log.Println(err)
return
}
for client := range webClients {
err := client.WriteJSON(jsonData)
if err != nil {
log.Println(err)
client.Close()
delete(webClients, client)
}
}
}
func GetAgents(c *gin.Context) {
agents, err := GetAllAgents()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
agentArr := make([][]string, len(agents))
for i := 0; i < len(agents); i++ {
agentArr[i] = []string{agents[i].Hostname, agents[i].HostOS, agents[i].IP, agents[i].ID}
}
c.JSON(http.StatusOK, agentArr)
}
func AddAgent(c *gin.Context) {
jsonData := make(map[string]interface{})
err := c.ShouldBindJSON(&jsonData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
hostname := jsonData["Hostname"].(string)
hostOS := jsonData["HostOS"].(string)
id := jsonData["ID"].(string)
key := jsonData["Key"].(string)
ip := strings.Split(c.ClientIP(), ":")[0]
if strings.Compare(key, tomlConf.AgentKey) != 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid key"})
return
}
agent := models.Agent{}
tx := db.First(&agent, "Hostname = ?", hostname)
if tx.Error == nil {
c.JSON(http.StatusOK, gin.H{"message": "Agent already exists"})
return
}
err = AddAgentToDB(id, hostname, hostOS, ip)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Agent added"})
}
func AgentStatus(jsonData []byte) {
for client := range webClients {
err := client.WriteMessage(websocket.TextMessage, jsonData)
if err != nil {
log.Println(err)
client.Close()
delete(webClients, client)
}
}
}
func sendToAgents(jsonData []byte) {
for client := range agentClients {
err := client.WriteMessage(websocket.TextMessage, jsonData)
if err != nil {
log.Println(err)
client.Close()
delete(agentClients, client)
}
}
}