forked from adamwalach/openvpn-web-ui
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapi-session.go
56 lines (49 loc) · 1.37 KB
/
api-session.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
package controllers
import (
"encoding/json"
mi "github.com/adamwalach/go-openvpn/server/mi"
"github.com/adamwalach/openvpn-web-ui/models"
)
//APISessionController manages vpn sessions
type APISessionController struct {
APIBaseController
}
//KillParams contains CommonName of session to kill
type KillParams struct {
Cname string `json:"cname"`
}
// Get lists vpn sessions
// @Title list
// @Description List vpn sessions
// @Success 200 request success
// @Failure 400 request failure
// @router / [get]
func (c *APISessionController) Get() {
client := mi.NewClient(models.GlobalCfg.MINetwork, models.GlobalCfg.MIAddress)
status, err := client.GetStatus()
if err != nil {
c.ServeJSONError(err.Error())
} else {
c.ServeJSONData(status)
}
}
// Kill deletes vpn session
// @Title Kill
// @Description Delete (kill) session
// @Param body body controllers.KillParams true "CommonName of client to kill"
// @Success 200 request success
// @Failure 400 request failure
// @router / [delete]
func (c *APISessionController) Kill() {
client := mi.NewClient(models.GlobalCfg.MINetwork, models.GlobalCfg.MIAddress)
p := KillParams{}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &p); err != nil {
c.ServeJSONError(err.Error())
return
}
if r, err := client.KillSession(p.Cname); err != nil {
c.ServeJSONError(err.Error())
} else {
c.ServeJSONMessage(r)
}
}