2008-10-05 19:49:21 +00:00
--[[
LuCI - Lua Configuration Interface
Copyright 2008 Steven Barth < steven @ midlink.org >
Licensed under the Apache License , Version 2.0 ( the " License " ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : // www.apache . org / licenses / LICENSE - 2.0
2008-10-09 22:24:21 +00:00
$ Id $
2008-10-05 19:49:21 +00:00
] ] --
2009-07-19 00:24:58 +00:00
local fs = require " nixio.fs "
local sys = require " luci.sys "
local uci = require " luci.model.uci " . cursor ( )
2014-07-14 02:03:32 +00:00
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
2008-10-05 19:49:21 +00:00
2009-10-31 15:54:11 +00:00
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 " ) )
2008-10-09 22:24:21 +00:00
s.template = " cbi/tblsection "
s.template_addremove = " openvpn/cbi-select-input-add "
s.addremove = true
s.add_select_options = { }
s.extedit = luci.dispatcher . build_url (
" admin " , " services " , " openvpn " , " basic " , " %s "
)
2008-10-05 19:49:21 +00:00
2008-10-09 22:24:21 +00:00
uci : load ( " openvpn_recipes " )
uci : foreach ( " openvpn_recipes " , " openvpn_recipe " ,
function ( section )
s.add_select_options [ section [ ' .name ' ] ] =
section [ ' _description ' ] or section [ ' .name ' ]
end
)
2008-10-05 19:49:21 +00:00
2008-10-09 22:24:21 +00:00
function s . parse ( self , section )
local recipe = luci.http . formvalue (
luci.cbi . CREATE_PREFIX .. self.config .. " . " ..
self.sectiontype .. " .select "
)
if recipe and not s.add_select_options [ recipe ] then
self.invalid_cts = true
else
TypedSection.parse ( self , section )
2008-10-05 19:49:21 +00:00
end
end
2008-10-09 22:24:21 +00:00
function s . create ( self , name )
local recipe = luci.http . formvalue (
luci.cbi . CREATE_PREFIX .. self.config .. " . " ..
self.sectiontype .. " .select "
)
2014-07-14 02:03:32 +00:00
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
2009-08-11 16:00:26 +00:00
uci : section (
" openvpn " , " openvpn " , name ,
uci : get_all ( " openvpn_recipes " , recipe )
)
2008-10-05 19:49:21 +00:00
2009-08-11 16:00:26 +00:00
uci : delete ( " openvpn " , name , " _role " )
uci : delete ( " openvpn " , name , " _description " )
uci : save ( " openvpn " )
2008-10-05 19:49:21 +00:00
2009-08-11 16:00:26 +00:00
luci.http . redirect ( self.extedit : format ( name ) )
else
self.invalid_cts = true
end
2008-10-09 22:24:21 +00:00
end
2008-10-05 19:49:21 +00:00
2012-07-09 06:25:04 +00:00
s : option ( Flag , " enabled " , translate ( " Enabled " ) )
2008-10-05 19:49:21 +00:00
2009-10-31 15:54:11 +00:00
local active = s : option ( DummyValue , " _active " , translate ( " Started " ) )
2008-10-09 22:24:21 +00:00
function active . cfgvalue ( self , section )
2014-07-14 02:03:32 +00:00
local pid = sys.exec ( " %s | grep %s | grep openvpn | grep -v grep | awk '{print $1}' " % { psstring , section } )
2009-07-19 00:24:58 +00:00
if pid and # pid > 0 and tonumber ( pid ) ~= nil then
return ( sys.process . signal ( pid , 0 ) )
2009-10-31 15:54:11 +00:00
and translatef ( " yes (%i) " , pid )
or translate ( " no " )
2008-10-05 19:49:21 +00:00
end
2009-10-31 15:54:11 +00:00
return translate ( " no " )
2008-10-09 22:24:21 +00:00
end
2008-10-05 19:49:21 +00:00
2009-10-31 15:54:11 +00:00
local updown = s : option ( Button , " _updown " , translate ( " Start/Stop " ) )
2009-08-12 14:05:31 +00:00
updown._state = false
2014-07-14 02:03:32 +00:00
updown.redirect = luci.dispatcher . build_url (
" admin " , " services " , " openvpn "
)
2009-08-12 14:05:31 +00:00
function updown . cbid ( self , section )
2014-07-14 02:03:32 +00:00
local pid = sys.exec ( " %s | grep %s | grep openvpn | grep -v grep | awk '{print $1}' " % { psstring , section } )
2009-08-12 14:05:31 +00:00
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
2014-07-14 02:03:32 +00:00
local pid = sys.exec ( " %s | grep %s | grep openvpn | grep -v grep | awk '{print $1}' " % { psstring , section } )
sys.process . signal ( pid , 15 )
2009-08-12 14:05:31 +00:00
else
2014-07-14 02:03:32 +00:00
luci.sys . call ( " /etc/init.d/openvpn start %s " % section )
2009-08-12 14:05:31 +00:00
end
2014-07-14 02:03:32 +00:00
luci.http . redirect ( self.redirect )
2009-08-12 14:05:31 +00:00
end
2014-07-14 02:03:32 +00:00
2009-10-31 15:54:11 +00:00
local port = s : option ( DummyValue , " port " , translate ( " Port " ) )
2008-10-09 22:24:21 +00:00
function port . cfgvalue ( self , section )
local val = AbstractValue.cfgvalue ( self , section )
return val or " 1194 "
end
2009-10-31 15:54:11 +00:00
local proto = s : option ( DummyValue , " proto " , translate ( " Protocol " ) )
2008-10-09 22:24:21 +00:00
function proto . cfgvalue ( self , section )
local val = AbstractValue.cfgvalue ( self , section )
return val or " udp "
2008-10-05 19:49:21 +00:00
end
2008-10-09 22:24:21 +00:00
2008-10-05 19:49:21 +00:00
return m