You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.0 KiB

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