summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-openvpn/luasrc/model/cbi
diff options
context:
space:
mode:
authorDirk Brenken <dev@brenken.org>2018-10-20 21:22:49 +0200
committerDirk Brenken <dev@brenken.org>2018-10-23 21:17:22 +0200
commit0b04912f8d495ea836d565f3536b59d030225cfa (patch)
tree143498433847d1e42e59dcc66652fbef2a3a4bbb /applications/luci-app-openvpn/luasrc/model/cbi
parenta0cc0769d8faf38172312a376d33aec241c19126 (diff)
luci-app-openvpn: add ovpn upload support & more
* add the ability to upload ovpn files directly, incl. appropriate uci entry in openvpn config * add the ability to edit ovpn files directly ('file' mode), beside the 'basic' and 'advanced' modes for normal setups * client side checks to validate instance name & template selection, incl. online error reporting * automatically remove non-ascii characters & windows line endings from transfered ovpn file * change from after_commit to after_apply hook * remove misleading default values for Port & Protocol in Overview Signed-off-by: Dirk Brenken <dev@brenken.org>
Diffstat (limited to 'applications/luci-app-openvpn/luasrc/model/cbi')
-rw-r--r--applications/luci-app-openvpn/luasrc/model/cbi/openvpn-basic.lua1
-rw-r--r--applications/luci-app-openvpn/luasrc/model/cbi/openvpn-file.lua61
-rw-r--r--applications/luci-app-openvpn/luasrc/model/cbi/openvpn.lua35
3 files changed, 81 insertions, 16 deletions
diff --git a/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-basic.lua b/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-basic.lua
index 483860c8e9..6b6323e078 100644
--- a/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-basic.lua
+++ b/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-basic.lua
@@ -85,4 +85,3 @@ for _, option in ipairs(basicParams) do
end
return m
-
diff --git a/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-file.lua b/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-file.lua
new file mode 100644
index 0000000000..6878275d78
--- /dev/null
+++ b/applications/luci-app-openvpn/luasrc/model/cbi/openvpn-file.lua
@@ -0,0 +1,61 @@
+-- Licensed to the public under the Apache License 2.0.
+
+local ip = require("luci.ip")
+local fs = require("nixio.fs")
+local util = require("luci.util")
+local uci = require("luci.model.uci").cursor()
+local cfg_file = uci:get("openvpn", arg[1], "config")
+
+local m = Map("openvpn")
+
+local p = m:section( SimpleSection )
+p.template = "openvpn/pageswitch"
+p.mode = "file"
+p.instance = arg[1]
+
+if not cfg_file or not fs.access(cfg_file) then
+ local f = SimpleForm("error", nil, translatef("The OVPN config file (%s) could not be found, please check your configuration.", cfg_file or "n/a"))
+ f:append(Template("openvpn/ovpn_css"))
+ f.reset = false
+ f.submit = false
+ return m, f
+end
+
+if fs.stat(cfg_file).size >= 102400 then
+ f = SimpleForm("error", nil,
+ translatef("The size of the OVPN config file (%s) is too large for online editing in LuCI (&ge; 100 KB). ", cfg_file)
+ .. translate("Please edit this file directly in a terminal session."))
+ f:append(Template("openvpn/ovpn_css"))
+ f.reset = false
+ f.submit = false
+ return m, f
+end
+
+f = SimpleForm("cfg", nil)
+f:append(Template("openvpn/ovpn_css"))
+f.submit = translate("Save")
+f.reset = false
+
+s = f:section(SimpleSection, nil, translatef("This form allows you to modify the content of the OVPN config file (%s). ", cfg_file))
+file = s:option(TextValue, "data")
+file.datatype = "string"
+file.rows = 20
+file.rmempty = true
+
+function file.cfgvalue()
+ return fs.readfile(cfg_file) or ""
+end
+
+function file.write(self, section, data)
+ return fs.writefile(cfg_file, "\n" .. util.trim(data:gsub("\r\n", "\n")) .. "\n")
+end
+
+function file.remove(self, section, value)
+ return fs.writefile(cfg_file, "")
+end
+
+function s.handle(self, state, data)
+ return true
+end
+
+return m, f
diff --git a/applications/luci-app-openvpn/luasrc/model/cbi/openvpn.lua b/applications/luci-app-openvpn/luasrc/model/cbi/openvpn.lua
index e17aa4085b..8f4859c0e5 100644
--- a/applications/luci-app-openvpn/luasrc/model/cbi/openvpn.lua
+++ b/applications/luci-app-openvpn/luasrc/model/cbi/openvpn.lua
@@ -4,7 +4,7 @@
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 testfullps = 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"))
@@ -13,9 +13,16 @@ 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"
-)
+
+file_cfg = s:option(DummyValue, "config")
+function file_cfg.cfgvalue(self, section)
+ local file_cfg = self.map:get(section, "config")
+ if file_cfg then
+ s.extedit = luci.dispatcher.build_url("admin", "services", "openvpn", "file", "%s")
+ else
+ s.extedit = luci.dispatcher.build_url("admin", "services", "openvpn", "basic", "%s")
+ end
+end
uci:load("openvpn_recipes")
uci:foreach( "openvpn_recipes", "openvpn_recipe",
@@ -61,10 +68,10 @@ function s.create(self, name)
if s then
local options = uci:get_all("openvpn_recipes", recipe)
for k, v in pairs(options) do
- uci:set("openvpn", name, k, v)
+ if k ~= "_role" and k ~= "_description" then
+ uci:set("openvpn", name, k, v)
+ end
end
- uci:delete("openvpn", name, "_role")
- uci:delete("openvpn", name, "_description")
uci:save("openvpn")
luci.http.redirect( self.extedit:format(name) )
end
@@ -75,7 +82,6 @@ function s.create(self, name)
return 0
end
-
s:option( Flag, "enabled", translate("Enabled") )
local active = s:option( DummyValue, "_active", translate("Started") )
@@ -106,28 +112,27 @@ function updown.cfgvalue(self, section)
end
function updown.write(self, section, value)
if self.option == "stop" then
- luci.sys.call("/etc/init.d/openvpn stop %s" % section)
+ sys.call("/etc/init.d/openvpn stop %s" % section)
else
- luci.sys.call("/etc/init.d/openvpn start %s" % section)
+ 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"
+ return val or "-"
end
local proto = s:option( DummyValue, "proto", translate("Protocol") )
function proto.cfgvalue(self, section)
local val = AbstractValue.cfgvalue(self, section)
- return val or "udp"
+ return val or "-"
end
-function m.on_after_commit(self,map)
- require("luci.sys").call('/etc/init.d/openvpn reload')
+function m.on_after_apply(self,map)
+ sys.call('/etc/init.d/openvpn reload')
end
return m