Merge pull request #299 from nmav/ocserv-match
[project/luci.git] / applications / luci-app-ocserv / luasrc / controller / ocserv.lua
1 -- Copyright 2014 Nikos Mavrogiannopoulos <n.mavrogiannopoulos@gmail.com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.ocserv", package.seeall)
5
6 function index()
7         if not nixio.fs.access("/etc/config/ocserv") then
8                 return
9         end
10
11         local page
12
13         page = entry({"admin", "services", "ocserv"}, alias("admin", "services", "ocserv", "main"),
14                 _("OpenConnect VPN"))
15         page.dependent = true
16         
17         page = entry({"admin", "services", "ocserv", "main"},
18                 cbi("ocserv/main"),
19                 _("Server Settings"), 200)
20         page.dependent = true
21
22         page = entry({"admin", "services", "ocserv", "users"},
23                 cbi("ocserv/users"),
24                 _("User Settings"), 300)
25         page.dependent = true
26
27         entry({"admin", "services", "ocserv", "status"},
28                 call("ocserv_status")).leaf = true
29
30         entry({"admin", "services", "ocserv", "disconnect"},
31                 call("ocserv_disconnect")).leaf = true
32
33 end
34
35 function ocserv_status()
36         local ipt = io.popen("/usr/bin/occtl show users");
37
38         if ipt then
39
40                 local fwd = { }
41                 while true do
42
43                         local ln = ipt:read("*l")
44                         if not ln then break end
45                 
46                         local id, user, group, vpn_ip, ip, device, time, cipher, status = 
47                                 ln:match("^%s*(%d+)%s+([-_%w]+)%s+([%.%*-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%(%)%:%.-_%w]+)%s+([%:%.-_%w]+).*")
48                         if id then
49                                 fwd[#fwd+1] = {
50                                         id = id,
51                                         user = user,
52                                         group = group,
53                                         vpn_ip = vpn_ip,
54                                         ip = ip,
55                                         device = device,
56                                         time = time,
57                                         cipher = cipher,
58                                         status = status
59                                 }
60                         end
61                 end
62                 ipt:close()
63                 luci.http.prepare_content("application/json")
64                 luci.http.write_json(fwd)
65         end
66 end
67
68 function ocserv_disconnect(num)
69         local idx = tonumber(num)
70
71         if idx and idx > 0 then
72                 luci.sys.call("/usr/bin/occtl disconnect id %d" % idx)
73                 luci.http.status(200, "OK")
74
75                 return
76         end
77         luci.http.status(400, "Bad request")
78 end