openvpn.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local fs = require "nixio.fs"
  4. local sys = require "luci.sys"
  5. local uci = require "luci.model.uci".cursor()
  6. local testfullps = luci.sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
  7. local psstring = (string.len(testfullps)>0) and "ps w" or "ps axfw" --set command we use to get pid
  8. local m = Map("openvpn", translate("OpenVPN"))
  9. local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
  10. s.template = "cbi/tblsection"
  11. s.template_addremove = "openvpn/cbi-select-input-add"
  12. s.addremove = true
  13. s.add_select_options = { }
  14. s.extedit = luci.dispatcher.build_url(
  15. "admin", "services", "openvpn", "basic", "%s"
  16. )
  17. uci:load("openvpn_recipes")
  18. uci:foreach( "openvpn_recipes", "openvpn_recipe",
  19. function(section)
  20. s.add_select_options[section['.name']] =
  21. section['_description'] or section['.name']
  22. end
  23. )
  24. function s.parse(self, section)
  25. local recipe = luci.http.formvalue(
  26. luci.cbi.CREATE_PREFIX .. self.config .. "." ..
  27. self.sectiontype .. ".select"
  28. )
  29. if recipe and not s.add_select_options[recipe] then
  30. self.invalid_cts = true
  31. else
  32. TypedSection.parse( self, section )
  33. end
  34. end
  35. function s.create(self, name)
  36. local recipe = luci.http.formvalue(
  37. luci.cbi.CREATE_PREFIX .. self.config .. "." ..
  38. self.sectiontype .. ".select"
  39. )
  40. name = luci.http.formvalue(
  41. luci.cbi.CREATE_PREFIX .. self.config .. "." ..
  42. self.sectiontype .. ".text"
  43. )
  44. if string.len(name)>3 and not name:match("[^a-zA-Z0-9_]") then
  45. uci:section(
  46. "openvpn", "openvpn", name,
  47. uci:get_all( "openvpn_recipes", recipe )
  48. )
  49. uci:delete("openvpn", name, "_role")
  50. uci:delete("openvpn", name, "_description")
  51. uci:save("openvpn")
  52. luci.http.redirect( self.extedit:format(name) )
  53. else
  54. self.invalid_cts = true
  55. end
  56. end
  57. s:option( Flag, "enabled", translate("Enabled") )
  58. local active = s:option( DummyValue, "_active", translate("Started") )
  59. function active.cfgvalue(self, section)
  60. local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
  61. if pid and #pid > 0 and tonumber(pid) ~= nil then
  62. return (sys.process.signal(pid, 0))
  63. and translatef("yes (%i)", pid)
  64. or translate("no")
  65. end
  66. return translate("no")
  67. end
  68. local updown = s:option( Button, "_updown", translate("Start/Stop") )
  69. updown._state = false
  70. updown.redirect = luci.dispatcher.build_url(
  71. "admin", "services", "openvpn"
  72. )
  73. function updown.cbid(self, section)
  74. local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
  75. self._state = pid and #pid > 0 and sys.process.signal(pid, 0)
  76. self.option = self._state and "stop" or "start"
  77. return AbstractValue.cbid(self, section)
  78. end
  79. function updown.cfgvalue(self, section)
  80. self.title = self._state and "stop" or "start"
  81. self.inputstyle = self._state and "reset" or "reload"
  82. end
  83. function updown.write(self, section, value)
  84. if self.option == "stop" then
  85. local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
  86. sys.process.signal(pid,15)
  87. else
  88. luci.sys.call("/etc/init.d/openvpn start %s" % section)
  89. end
  90. luci.http.redirect( self.redirect )
  91. end
  92. local port = s:option( DummyValue, "port", translate("Port") )
  93. function port.cfgvalue(self, section)
  94. local val = AbstractValue.cfgvalue(self, section)
  95. return val or "1194"
  96. end
  97. local proto = s:option( DummyValue, "proto", translate("Protocol") )
  98. function proto.cfgvalue(self, section)
  99. local val = AbstractValue.cfgvalue(self, section)
  100. return val or "udp"
  101. end
  102. return m