Updated openvpn app to work with current openvpn package
[project/luci.git] / applications / luci-openvpn / luasrc / model / cbi / openvpn.lua
index c220b10..2f865e0 100644 (file)
@@ -15,9 +15,11 @@ $Id$
 local fs  = require "nixio.fs"
 local sys = require "luci.sys"
 local uci = require "luci.model.uci".cursor()
+local testfullps = luci.sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
+local psstring = (string.len(testfullps)>0) and  "ps w" or  "ps axfw" --set command we use to get pid
 
-local m = Map("openvpn", translate("openvpn"))
-local s = m:section( TypedSection, "openvpn", translate("openvpn_overview"), translate("openvpn_overview_desc") )
+local m = Map("openvpn", translate("OpenVPN"))
+local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
 s.template = "cbi/tblsection"
 s.template_addremove = "openvpn/cbi-select-input-add"
 s.addremove = true
@@ -52,40 +54,73 @@ function s.create(self, name)
                luci.cbi.CREATE_PREFIX .. self.config .. "." ..
                self.sectiontype .. ".select"
        )
-
-       uci:section(
-               "openvpn", "openvpn", name,
-               uci:get_all( "openvpn_recipes", recipe )
+       name = luci.http.formvalue(
+               luci.cbi.CREATE_PREFIX .. self.config .. "." ..
+               self.sectiontype .. ".text"
        )
+       if string.len(name)>3 and not name:match("[^a-zA-Z0-9_]") then
+               uci:section(
+                       "openvpn", "openvpn", name,
+                       uci:get_all( "openvpn_recipes", recipe )
+               )
 
-       uci:delete("openvpn", name, "_role")
-       uci:delete("openvpn", name, "_description")
-       uci:save("openvpn")
+               uci:delete("openvpn", name, "_role")
+               uci:delete("openvpn", name, "_description")
+               uci:save("openvpn")
 
-       luci.http.redirect( self.extedit:format(name) )
+               luci.http.redirect( self.extedit:format(name) )
+       else
+               self.invalid_cts = true
+       end
 end
 
 
-s:option( Flag, "enable", translate("openvpn_enable") )
+s:option( Flag, "enabled", translate("Enabled") )
 
-local active = s:option( DummyValue, "_active", translate("openvpn_active") )
+local active = s:option( DummyValue, "_active", translate("Started") )
 function active.cfgvalue(self, section)
-       local pid = fs.readfile("/var/run/openvpn-%s.pid" % section)
+       local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
        if pid and #pid > 0 and tonumber(pid) ~= nil then
                return (sys.process.signal(pid, 0))
-                       and translatef("openvpn_active_yes", "yes (%i)", pid)
-                       or  translate("openvpn_active_no")
+                       and translatef("yes (%i)", pid)
+                       or  translate("no")
        end
-       return translate("openvpn_active_no")
+       return translate("no")
 end
 
-local port = s:option( DummyValue, "port", translate("openvpn_port") )
+local updown = s:option( Button, "_updown", translate("Start/Stop") )
+updown._state = false
+updown.redirect = luci.dispatcher.build_url(
+       "admin", "services", "openvpn"
+)
+function updown.cbid(self, section)
+       local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
+       self._state = pid and #pid > 0 and sys.process.signal(pid, 0)
+       self.option = self._state and "stop" or "start"
+       return AbstractValue.cbid(self, section)
+end
+function updown.cfgvalue(self, section)
+       self.title = self._state and "stop" or "start"
+       self.inputstyle = self._state and "reset" or "reload"
+end
+function updown.write(self, section, value)
+       if self.option == "stop" then
+               local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
+               sys.process.signal(pid,15)
+       else
+               luci.sys.call("/etc/init.d/openvpn start %s" % section)
+       end
+       luci.http.redirect( self.redirect )
+end
+
+
+local port = s:option( DummyValue, "port", translate("Port") )
 function port.cfgvalue(self, section)
        local val = AbstractValue.cfgvalue(self, section)
        return val or "1194"
 end
 
-local proto = s:option( DummyValue, "proto", translate("openvpn_proto") )
+local proto = s:option( DummyValue, "proto", translate("Protocol") )
 function proto.cfgvalue(self, section)
        local val = AbstractValue.cfgvalue(self, section)
        return val or "udp"