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