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