ocserv.lua 1.8 KB

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