-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
97 lines (86 loc) · 2.35 KB
/
db.go
File metadata and controls
97 lines (86 loc) · 2.35 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
package main
import (
"networkinator/models"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func ConnectToSQLite() *gorm.DB {
db, err := gorm.Open(sqlite.Open("network.db"), &gorm.Config{})
if err != nil {
panic(err)
}
return db
}
func AddConnectionToDB(id string, src, dst string, port int, count float64) error {
connection := models.Connection{ID: id, Src: src, Dst: dst, Port: port, Count: count}
result := db.Create(&connection)
if result.Error != nil {
return result.Error
}
return nil
}
func DeleteConnectionFromDB(id string) error {
var connection models.Connection
result := db.Where("id = ?", id).First(&connection)
if result.Error != nil {
return result.Error
}
result = db.Delete(&connection)
if result.Error != nil {
return result.Error
}
return nil
}
func GetAllConnections(db *gorm.DB) ([]models.Connection, error) {
var connections []models.Connection
result := db.Find(&connections)
if result.Error != nil {
return nil, result.Error
}
return connections, nil
}
func GetConnectionsByIP(host string) ([]models.Connection, error) {
var connections []models.Connection
result := db.Where("src = ? OR dst = ?", host, host).Find(&connections)
if result.Error != nil {
return nil, result.Error
}
return connections, nil
}
func UpdateConnectionCount(id string, count float64) error {
var connection models.Connection
result := db.Where("id = ?", id).First(&connection)
if result.Error != nil {
return result.Error
}
connection.Count = count
result = db.Save(&connection)
if result.Error != nil {
return result.Error
}
return nil
}
func AddAgentToDB(ID string, hostname, hostOS, ip string) error {
agent := models.Agent{ID: ID, Hostname: hostname, HostOS: hostOS, IP: ip, Status: "Alive"}
result := db.Create(&agent)
if result.Error != nil {
return result.Error
}
return nil
}
func GetAllAgents() ([]models.Agent, error) {
var agents []models.Agent
result := db.Find(&agents)
if result.Error != nil {
return nil, result.Error
}
return agents, nil
}
func GetAgentByIP(ip string) (models.Agent, error) {
var agent models.Agent
result := db.Where("IP = ?", ip).First(&agent)
if result.Error != nil {
return agent, result.Error
}
return agent, nil
}