2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local fs = require "nixio.fs"
16 local sys = require "luci.sys"
17 local uci = require "luci.model.uci".cursor()
18 local testfullps = luci.sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
19 local psstring = (string.len(testfullps)>0) and "ps w" or "ps axfw" --set command we use to get pid
21 local m = Map("openvpn", translate("OpenVPN"))
22 local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
23 s.template = "cbi/tblsection"
24 s.template_addremove = "openvpn/cbi-select-input-add"
26 s.add_select_options = { }
27 s.extedit = luci.dispatcher.build_url(
28 "admin", "services", "openvpn", "basic", "%s"
31 uci:load("openvpn_recipes")
32 uci:foreach( "openvpn_recipes", "openvpn_recipe",
34 s.add_select_options[section['.name']] =
35 section['_description'] or section['.name']
39 function s.parse(self, section)
40 local recipe = luci.http.formvalue(
41 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
42 self.sectiontype .. ".select"
45 if recipe and not s.add_select_options[recipe] then
46 self.invalid_cts = true
48 TypedSection.parse( self, section )
52 function s.create(self, name)
53 local recipe = luci.http.formvalue(
54 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
55 self.sectiontype .. ".select"
57 name = luci.http.formvalue(
58 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
59 self.sectiontype .. ".text"
61 if string.len(name)>3 and not name:match("[^a-zA-Z0-9_]") then
63 "openvpn", "openvpn", name,
64 uci:get_all( "openvpn_recipes", recipe )
67 uci:delete("openvpn", name, "_role")
68 uci:delete("openvpn", name, "_description")
71 luci.http.redirect( self.extedit:format(name) )
73 self.invalid_cts = true
78 s:option( Flag, "enabled", translate("Enabled") )
80 local active = s:option( DummyValue, "_active", translate("Started") )
81 function active.cfgvalue(self, section)
82 local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
83 if pid and #pid > 0 and tonumber(pid) ~= nil then
84 return (sys.process.signal(pid, 0))
85 and translatef("yes (%i)", pid)
88 return translate("no")
91 local updown = s:option( Button, "_updown", translate("Start/Stop") )
93 updown.redirect = luci.dispatcher.build_url(
94 "admin", "services", "openvpn"
96 function updown.cbid(self, section)
97 local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
98 self._state = pid and #pid > 0 and sys.process.signal(pid, 0)
99 self.option = self._state and "stop" or "start"
100 return AbstractValue.cbid(self, section)
102 function updown.cfgvalue(self, section)
103 self.title = self._state and "stop" or "start"
104 self.inputstyle = self._state and "reset" or "reload"
106 function updown.write(self, section, value)
107 if self.option == "stop" then
108 local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
109 sys.process.signal(pid,15)
111 luci.sys.call("/etc/init.d/openvpn start %s" % section)
113 luci.http.redirect( self.redirect )
117 local port = s:option( DummyValue, "port", translate("Port") )
118 function port.cfgvalue(self, section)
119 local val = AbstractValue.cfgvalue(self, section)
123 local proto = s:option( DummyValue, "proto", translate("Protocol") )
124 function proto.cfgvalue(self, section)
125 local val = AbstractValue.cfgvalue(self, section)