Translated using Weblate (Japanese)
[oweals/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", "vpn", "ocserv"}, alias("admin", "vpn", "ocserv", "main"),
14                 _("OpenConnect VPN"))
15         page.dependent = true
16         page.acl_depends = { "luci-app-ocserv" }
17         
18         page = entry({"admin", "vpn", "ocserv", "main"},
19                 cbi("ocserv/main"),
20                 _("Server Settings"), 200)
21         page.dependent = true
22
23         page = entry({"admin", "vpn", "ocserv", "users"},
24                 cbi("ocserv/users"),
25                 _("User Settings"), 300)
26         page.dependent = true
27
28         entry({"admin", "vpn", "ocserv", "status"},
29                 call("ocserv_status")).leaf = true
30
31         entry({"admin", "vpn", "ocserv", "disconnect"},
32                 post("ocserv_disconnect")).leaf = true
33
34 end
35
36 function ocserv_status()
37         local ipt = io.popen("/usr/bin/occtl show users");
38
39         if ipt then
40
41                 local fwd = { }
42                 while true do
43
44                         local ln = ipt:read("*l")
45                         if not ln then break end
46                 
47                         local id, user, group, vpn_ip, ip, device, time, cipher, status = 
48                                 ln:match("^%s*(%d+)%s+([-_%w]+)%s+([%(%)%.%*-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%(%)%:%.-_%w]+)%s+([%:%.-_%w]+).*")
49                         if id then
50                                 fwd[#fwd+1] = {
51                                         id = id,
52                                         user = user,
53                                         group = group,
54                                         vpn_ip = vpn_ip,
55                                         ip = ip,
56                                         device = device,
57                                         time = time,
58                                         cipher = cipher,
59                                         status = status
60                                 }
61                         end
62                 end
63                 ipt:close()
64                 luci.http.prepare_content("application/json")
65                 luci.http.write_json(fwd)
66         end
67 end
68
69 function ocserv_disconnect(num)
70         local idx = tonumber(num)
71
72         if idx and idx > 0 then
73                 luci.sys.call("/usr/bin/occtl disconnect id %d" % idx)
74                 luci.http.status(200, "OK")
75
76                 return
77         end
78         luci.http.status(400, "Bad request")
79 end