ocserv.lua 1.8 KB

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