Skip to content

Commit b5d60f9

Browse files
committed
push notifications for enableVPN disableVPN
1 parent 092624b commit b5d60f9

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

api/v1/vpn/vpn.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,47 @@ func enableVPN(c *gin.Context) {
261261
return
262262
}
263263

264+
vpns, err := core.ReadVPN2("netid", vpn.NetId)
265+
if err != nil {
266+
log.WithFields(log.Fields{
267+
"err": err,
268+
}).Error("failed to read vpns")
269+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
270+
return
271+
}
272+
273+
for _, v := range vpns {
274+
// flush the cache for this vpn
275+
core.FlushCache(v.DeviceID)
276+
277+
if core.Push.PushDevices[v.DeviceID] != "" && v.Enable && v.DeviceID == vpn.DeviceID {
278+
err := core.Push.SendPushNotification(core.Push.PushDevices[v.DeviceID], v.NetName+" enabled", "Connection to "+v.NetName+" has been established")
279+
if err != nil {
280+
log.WithFields(log.Fields{
281+
"err": err,
282+
}).Error("failed to send push notification")
283+
}
284+
} else if core.Push.PushDevices[v.DeviceID] != "" && v.Enable {
285+
// send push notification if appropriate
286+
err := core.Push.SendPushNotification(core.Push.PushDevices[v.DeviceID], v.NetName+" updated", "The VPN configuration for "+v.NetName+" has been updated")
287+
if err != nil {
288+
log.WithFields(log.Fields{
289+
"err": err,
290+
}).Error("failed to send push notification")
291+
}
292+
}
293+
294+
// send push notification to the device that just had the VPN disabled
295+
if core.Push.PushDevices[v.DeviceID] != "" && !v.Enable && v.DeviceID == vpn.DeviceID {
296+
err := core.Push.SendPushNotification(core.Push.PushDevices[v.DeviceID], v.NetName+" disabled", "The VPN configuration for "+v.NetName+" has been disabled")
297+
if err != nil {
298+
log.WithFields(log.Fields{
299+
"err": err,
300+
}).Error("failed to send push notification")
301+
}
302+
}
303+
}
304+
264305
c.JSON(http.StatusOK, vpn)
265306
}
266307

@@ -343,6 +384,47 @@ func disableVPN(c *gin.Context) {
343384
return
344385
}
345386

387+
vpns, err := core.ReadVPN2("netid", vpn.NetId)
388+
if err != nil {
389+
log.WithFields(log.Fields{
390+
"err": err,
391+
}).Error("failed to read vpns")
392+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
393+
return
394+
}
395+
396+
for _, v := range vpns {
397+
// flush the cache for this vpn
398+
core.FlushCache(v.DeviceID)
399+
400+
if core.Push.PushDevices[v.DeviceID] != "" && v.Enable && v.DeviceID == vpn.DeviceID {
401+
err := core.Push.SendPushNotification(core.Push.PushDevices[v.DeviceID], v.NetName+" enabled", "Connection to "+v.NetName+" has been established")
402+
if err != nil {
403+
log.WithFields(log.Fields{
404+
"err": err,
405+
}).Error("failed to send push notification")
406+
}
407+
} else if core.Push.PushDevices[v.DeviceID] != "" && v.Enable {
408+
// send push notification if appropriate
409+
err := core.Push.SendPushNotification(core.Push.PushDevices[v.DeviceID], v.NetName+" updated", "The VPN configuration for "+v.NetName+" has been updated")
410+
if err != nil {
411+
log.WithFields(log.Fields{
412+
"err": err,
413+
}).Error("failed to send push notification")
414+
}
415+
}
416+
417+
// send push notification to the device that just had the VPN disabled
418+
if core.Push.PushDevices[v.DeviceID] != "" && !v.Enable && v.DeviceID == vpn.DeviceID {
419+
err := core.Push.SendPushNotification(core.Push.PushDevices[v.DeviceID], v.NetName+" disabled", "The VPN configuration for "+v.NetName+" has been disabled")
420+
if err != nil {
421+
log.WithFields(log.Fields{
422+
"err": err,
423+
}).Error("failed to send push notification")
424+
}
425+
}
426+
}
427+
346428
c.JSON(http.StatusOK, vpn)
347429
}
348430

0 commit comments

Comments
 (0)