This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
extclient_handlers.go
166 lines (152 loc) · 4.16 KB
/
extclient_handlers.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
controller "github.com/gravitl/netmaker/controllers"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
"github.com/skip2/go-qrcode"
)
func CreateIngressClient(c *gin.Context) {
var client models.ExtClient
client.Network = c.Param("net")
client.IngressGatewayID = c.Param("mac")
node, err := functions.GetNodeByMacAddress(client.Network, client.IngressGatewayID)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
client.IngressGatewayEndpoint = node.Endpoint + ":" + strconv.FormatInt(int64(node.ListenPort), 10)
err = controller.CreateExtClient(client)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
ReturnSuccess(c, "ExtClients", "external client has been created")
}
func DeleteIngressClient(c *gin.Context) {
net := c.Param("net")
id := c.Param("id")
err := controller.DeleteExtClient(net, id)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
ReturnSuccess(c, "ExtClients", "external client "+id+" @ "+net+" has been deleted")
}
//EditIngressClient displays a form to update name of external client
func EditIngressClient(c *gin.Context) {
net := c.Param("net")
id := c.Param("id")
client, err := controller.GetExtClient(id, net)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
c.HTML(http.StatusOK, "EditExtClient", client)
}
func GetQR(c *gin.Context) {
net := c.Param("net")
id := c.Param("id")
config, err := GetConf(net, id)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "ExtClient")
return
}
b, err := qrcode.Encode(config, qrcode.Medium, 220)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "ExtClient")
return
}
c.Header("Content-Type", "image/png")
c.Data(http.StatusOK, "application/octet-strean", b)
}
func GetConf(net, id string) (string, error) {
client, err := controller.GetExtClient(id, net)
if err != nil {
return "", err
}
gwnode, err := functions.GetNodeByMacAddress(client.Network, client.IngressGatewayID)
if err != nil {
return "", err
}
network, err := functions.GetParentNetwork(client.Network)
if err != nil {
return "", err
}
keepalive := ""
if network.DefaultKeepalive != 0 {
keepalive = "PersistentKeepalive = " + strconv.Itoa(int(network.DefaultKeepalive))
}
gwendpoint := gwnode.Endpoint + ":" + strconv.Itoa(int(gwnode.ListenPort))
newAllowedIPs := network.AddressRange
if egressGatewayRanges, err := client.GetEgressRangesOnNetwork(); err == nil {
for _, egressGatewayRange := range egressGatewayRanges {
newAllowedIPs += "," + egressGatewayRange
}
}
defaultDNS := ""
if network.DefaultExtClientDNS != "" {
defaultDNS = "DNS = " + network.DefaultExtClientDNS
}
config := fmt.Sprintf(`[Interface]
Address = %s
PrivateKey = %s
%s
[Peer]
PublicKey = %s
AllowedIPs = %s
Endpoint = %s
%s
`, client.Address+"/32",
client.PrivateKey,
defaultDNS,
gwnode.PublicKey,
newAllowedIPs,
gwendpoint,
keepalive)
return config, nil
}
func GetClientConfig(c *gin.Context) {
net := c.Param("net")
id := c.Param("id")
config, err := GetConf(net, id)
b := []byte(config)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "ExtClient")
return
}
filename := id + ".conf"
//c.FileAttachment(filepath, filename)
c.Header("Content-Description", "File Transfer")
c.Header("Content-Disposition", "attachment: filename="+filename)
c.Data(http.StatusOK, "application/octet-stream", b)
}
//UpdateClient updates name of external Client
func UpdateClient(c *gin.Context) {
net := c.Param("net")
id := c.Param("id")
newid := c.PostForm("newid")
client, err := controller.GetExtClient(id, net)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "ExtClient")
return
}
_, err = controller.UpdateExtClient(newid, net, client)
if err != nil {
fmt.Println(err)
ReturnError(c, http.StatusBadRequest, err, "ExtClient")
return
}
ReturnSuccess(c, "ExtClients", "external client has been updated")
}