/
vpn.go
495 lines (422 loc) · 12.2 KB
/
vpn.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package client
import (
"archive/zip"
"net/http"
"strings"
"github.com/gin-gonic/gin"
core "github.com/nettica-com/nettica-admin/core"
model "github.com/nettica-com/nettica-admin/model"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
)
// ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/vpn")
{
g.POST("", createVPN)
g.GET("/:id", readVPN)
g.PATCH("/:id", updateVPN)
g.DELETE("/:id", deleteVPN)
g.GET("", readVPNs)
g.GET("/:id/config", configVPN)
}
}
// CreateVPN creates a new VPN for a device
// @Summary Create a new VPN for a device
// @Description Create a new VPN for a device
// @Tags vpn
// @Security apiKey
// @Accept json
// @Produce json
// @Param vpn body model.VPN true "VPN"
// @Success 200 {object} model.VPN
// @Router /vpn [post]
func createVPN(c *gin.Context) {
var data model.VPN
if err := c.ShouldBindJSON(&data); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to bind")
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
network, err := core.ReadNet(data.NetId)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read network")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
account, _, err := core.AuthFromContext(c, network.Id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to get account from context")
return
}
data.CreatedBy = account.Email
data.UpdatedBy = account.Email
if data.AccountID == "" {
data.AccountID = network.AccountID
}
if data.Current.Endpoint != "" && account.Role != "Admin" && account.Role != "Owner" {
// check the policy of the network to see if the endpoint is allowed
if !network.Policies.UserEndpoints {
log.Infof("User %s tried to set endpoint %s for network %s", account.Email, data.Current.Endpoint, data.NetId)
c.JSON(http.StatusForbidden, gin.H{"error": "Users cannot create endpoints for this network"})
return
}
}
vpn, err := core.CreateVPN(&data)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to create client")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
vpns, err := core.ReadVPN2("netid", vpn.NetId)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read vpns")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
for _, v := range vpns {
// flush the cache for this vpn
core.FlushCache(v.DeviceID)
}
c.JSON(http.StatusOK, vpn)
}
// ReadVPN reads a VPN
// @Summary Read a VPN
// @Description Read a VPN
// @Tags vpn
// @Produce json
// @Param id path string true "VPN ID"
// @Security apiKey
// @Success 200 {object} model.VPN
// @Router /vpn/{id} [get]
func readVPN(c *gin.Context) {
id := c.Param("id")
account, v, err := core.AuthFromContext(c, id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to get account from context")
return
}
vpn := v.(*model.VPN)
if account.Status == "Suspended" {
log.Errorf("readVPN: account %s is suspended", account.Email)
c.JSON(http.StatusForbidden, gin.H{"error": "Account is suspended"})
return
}
c.JSON(http.StatusOK, vpn)
}
// UpdateVPN updates a VPN
// @Summary Update a VPN
// @Description Update a VPN
// @Tags vpn
// @Security apiKey
// @Accept json
// @Produce json
// @Param id path string true "VPN ID"
// @Param vpn body model.VPN true "VPN"
// @Success 200 {object} model.VPN
// @Router /vpn/{id} [patch]
func updateVPN(c *gin.Context) {
var data model.VPN
id := c.Param("id")
if id == "" {
log.Error("vpnid cannot be empty")
c.JSON(http.StatusBadRequest, gin.H{"error": "vpnid cannot be empty"})
return
}
if err := c.ShouldBindJSON(&data); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to bind")
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
account, v, err := core.AuthFromContext(c, id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to get account from context")
return
}
vpn := v.(*model.VPN)
authorized := false
apikey := c.Request.Header.Get("X-API-KEY")
if apikey != "" && strings.HasPrefix(apikey, "device-api-") {
device, err := core.ReadDevice(vpn.DeviceID)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read client config")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if device.ApiKey == apikey {
authorized = true
}
if !authorized {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
data.UpdatedBy = device.Name
} else {
if vpn.CreatedBy == account.Email || account.Role == "Admin" || account.Role == "Owner" {
authorized = true
}
if !authorized {
c.JSON(http.StatusForbidden, gin.H{"error": "You cannot update this VPN"})
return
}
data.UpdatedBy = account.Email
}
// this is to allow the logic below to work from device updates without crashing.
// the device has effective rights of a user so it will not be allowed to change
// from a client to endpoint, or change the allowedIPs
if account == nil {
account = &model.Account{}
}
if data.Current.Endpoint != "" && vpn.Current.Endpoint == "" && account.Role != "Admin" && account.Role != "Owner" {
// check the policy of the network to see if the endpoint is allowed
network, err := core.ReadNet(vpn.NetId)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read network")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if !network.Policies.UserEndpoints {
log.Infof("User %s tried to set endpoint %s for network %s", account.Email, data.Current.Endpoint, data.NetId)
c.JSON(http.StatusForbidden, gin.H{"error": "User Endpoints are disabled for this network"})
return
}
}
// do not allow changes to the AllowedIPs unless it's an endpoint
// admins are allowed to do this, for example, extending an AWS subnet through a relay
// this code is in place to prevent users from breaking the VPN accidentally (or on purpose)
if data.Current.Endpoint == "" && !CompareArrays(vpn.Current.AllowedIPs, data.Current.AllowedIPs) && account.Role != "Admin" && account.Role != "Owner" {
log.Infof("User %s tried to change AllowedIPs for vpn %s (%v)", account.Email, id, data.Current.AllowedIPs)
c.JSON(http.StatusForbidden, gin.H{"error": "You cannot change AllowedIPs"})
return
}
result, err := core.UpdateVPN(id, &data, false)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to update vpn")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
vpns, err := core.ReadVPN2("netid", result.NetId)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read vpns")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
for _, v := range vpns {
// flush the cache for this vpn
core.FlushCache(v.DeviceID)
}
c.JSON(http.StatusOK, result)
}
func CompareArrays(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for _, x := range a {
found := false
for _, y := range b {
if x == y {
found = true
}
}
if !found {
return false
}
}
return true
}
// DeleteVPN deletes a VPN
// @Summary Delete a VPN
// @Description Delete a VPN
// @Tags vpn
// @Security apiKey
// @Success 200 {object} string "OK"
// @Param id path string true "VPN ID"
// @Router /vpn/{id} [delete]
func deleteVPN(c *gin.Context) {
id := c.Param("id")
account, v, err := core.AuthFromContext(c, id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to get account from context")
return
}
vpn := v.(*model.VPN)
apikey := c.Request.Header.Get("X-API-KEY")
if apikey != "" && strings.HasPrefix(apikey, "device-api-") {
device, err := core.ReadDeviceByApiKey(apikey)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read client config")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
authorized := false
if device.ApiKey == apikey {
authorized = true
}
if !authorized {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
log.Infof("Device %s deleted VPN %s", device.Name, id)
} else {
if vpn.CreatedBy != account.Email && account.Role != "Admin" && account.Role != "Owner" {
log.Infof("User %s is not an admin of %s", account.Email, account.Id)
c.JSON(http.StatusForbidden, gin.H{"error": "You cannot delete this VPN"})
return
}
log.Infof("User %s deleted vpn %s", account.Email, id)
}
vpns, err := core.ReadVPN2("netid", vpn.NetId)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read vpns")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
err = core.DeleteVPN(id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to remove client")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
for _, v := range vpns {
// flush the cache for this vpn
core.FlushCache(v.DeviceID)
}
c.JSON(http.StatusOK, gin.H{})
}
// ReadVPNs reads all VPNs
// @Summary Read all VPNs
// @Description Read all VPNs
// @Tags vpn
// @Security apiKey
// @Produce json
// @Success 200 {object} []model.VPN
// @Router /vpn [get]
func readVPNs(c *gin.Context) {
account, _, err := core.AuthFromContext(c, "")
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to get account from context")
return
}
clients, err := core.ReadVPNsForUser(account.Email)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to list clients")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, clients)
}
// ConfigVPN returns the wireguard configuration file for a VPN in a .zip file
// @Summary Get VPN config
// @Description Get VPN config
// @Tags vpn
// @Security apiKey
// @Produce application/zip
// @Param id path string true "VPN ID"
// @Router /vpn/{id}/config [get]
// @Success 200 {array} byte
func configVPN(c *gin.Context) {
id := c.Param("id")
if id == "" {
log.Error("vpnid cannot be empty")
c.JSON(http.StatusBadRequest, gin.H{"error": "vpnid cannot be empty"})
}
account, _, err := core.AuthFromContext(c, id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to get account from context")
return
}
if account.Status == "Suspended" {
log.Errorf("configVPN: account %s is suspended", account.Email)
c.JSON(http.StatusForbidden, gin.H{"error": "Account is suspended"})
return
}
formatQr := c.DefaultQuery("qrcode", "false")
zipcode := c.DefaultQuery("zip", "false")
data, net, err := core.ReadVPNConfig(id)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read vpn config")
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
sdata := string(data)
if zipcode == "false" {
c.Writer.Header().Set("Content-Type", "application/zip")
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+*net+".zip")
w := zip.NewWriter(c.Writer)
defer w.Close()
// Make a zip file with the config file
f, err := w.Create(*net + ".conf")
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to create zip file")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
_, err = f.Write(data)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to write zip file")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
return
}
if formatQr == "false" {
// return config as txt file
c.Header("Content-Disposition", "attachment; filename=nettica.conf")
c.Data(http.StatusOK, "application/config", data)
return
}
// return config as png qrcode
png, err := qrcode.Encode(sdata, qrcode.Medium, 250)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to create qrcode")
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.Data(http.StatusOK, "image/png", png)
}