-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_funcs.go
157 lines (141 loc) · 4.07 KB
/
db_funcs.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
package main
import (
"errors"
"strings"
"github.com/google/uuid"
"gorm.io/gorm"
)
func GetPeer(peerID string) (*Peer, error) {
var peer Peer
err := GetDB().First(&peer, "id = ?", peerID).Error
return &peer, err
}
func GetPeerIP(peerID string) (string, error) {
var peer Peer
err := GetDB().First(&peer, "id = ?", peerID).Select("ip").Error
return peer.IP, err
}
func GetPeerStatus(peerID string) (PeerStatus, error) {
var peer Peer
err := GetDB().First(&peer, "id = ?", peerID).Select("status").Error
return peer.Status, err
}
func GetPeers() ([]Peer, error) {
var peers []Peer
err := GetDB().Find(&peers).Error
return peers, err
}
func CreatePeer() (*Peer, error) {
ip := getUniqueIPInSubnet()
privateKey, err := generateWireguardPrivateKey()
if err != nil {
return nil, err
}
publicKey, err := generateWireguardPublicKey(privateKey)
if err != nil {
return nil, err
}
peer := &Peer{
ID: uuid.New().String(),
IP: ip,
PrivateKey: privateKey,
PublicKey: publicKey,
Status: PeerStatusPending,
}
err = GetDB().Create(peer).Error
if err == nil {
workerQueueChannel <- QueueJob{Type: "peer", ID: peer.ID}
}
return peer, err
}
func UpdatePeerStatus(peerID string, status PeerStatus) error {
err := GetDB().Model(&Peer{}).Where("id = ?", peerID).Update("status", status).Error
if err == nil && status == PeerStatusDeleting {
workerQueueChannel <- QueueJob{Type: "peer", ID: peerID}
}
return err
}
func DeletePeer(peerID string) error {
return GetDB().Delete(&Peer{}, "id = ?", peerID).Error
}
func GetAccessRuleByID(id string) (*AccessRule, error) {
var accessRule AccessRule
err := GetDB().First(&accessRule, "id = ?", id).Error
return &accessRule, err
}
func GetAccessRule(peerAID, peerBID string) (*AccessRule, error) {
var accessRule AccessRule
err := GetDB().First(&accessRule, "peer_a_id = ? AND peer_b_id = ?", peerAID, peerBID).Error
if err == nil {
return &accessRule, nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
// try out peerBID -> peerAID
err = GetDB().First(&accessRule, "peer_b_id = ? AND peer_a_id = ?", peerAID, peerBID).Error
if err != nil {
return nil, err
}
return &accessRule, nil
}
func GetAccessRulesByPeerID(peerID string) ([]AccessRule, error) {
var accessRules []AccessRule
err := GetDB().Find(&accessRules, "peer_a_id = ? OR peer_b_id = ?", peerID, peerID).Error
return accessRules, err
}
func IsAccessRuleExist(peerAID, peerBID string) (bool, error) {
accessRule, err := GetAccessRule(peerAID, peerBID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
return accessRule != nil, nil
}
func CreateAccessRule(peerAID, peerBID string) (*AccessRule, error) {
peerAID = strings.TrimSpace(peerAID)
peerBID = strings.TrimSpace(peerBID)
isExist, err := IsAccessRuleExist(peerAID, peerBID)
if err != nil {
return nil, err
}
if isExist {
record, err := GetAccessRule(peerAID, peerBID)
if err != nil {
return nil, errors.New("failed to get access rule " + peerAID + " -> " + peerBID + " : " + err.Error())
}
return record, nil
}
// Validate peerAID and peerBID
_, err = GetPeer(peerAID)
if err != nil {
return nil, errors.New("failed to get peer " + peerAID + " : " + err.Error())
}
_, err = GetPeer(peerBID)
if err != nil {
return nil, errors.New("failed to get peer " + peerBID + " : " + err.Error())
}
// check if peerAID and peerBID are the same
if peerAID == peerBID {
return nil, errors.New("peerAID and peerBID cannot be the same")
}
accessRule := &AccessRule{
ID: uuid.New().String(),
PeerAID: peerAID,
PeerBID: peerBID,
Status: AccessRuleStatusPending,
}
err = GetDB().Create(accessRule).Error
if err == nil {
workerQueueChannel <- QueueJob{Type: "access_rule", ID: accessRule.ID}
}
return accessRule, err
}
func UpdateAccessRuleStatus(ruleID string, status AccessRuleStatus) error {
return GetDB().Model(&AccessRule{}).Where("id = ?", ruleID).Update("status", status).Error
}
func DeleteAccessRule(ruleID string) error {
return GetDB().Delete(&AccessRule{}, "id = ?", ruleID).Error
}