1 -- Licensed to the public under the Apache License 2.0.
3 local ip = require("luci.ip")
4 local fs = require("nixio.fs")
5 local util = require("luci.util")
6 local uci = require("luci.model.uci").cursor()
7 local cfg_file = uci:get("openvpn", arg[1], "config")
8 local auth_file = cfg_file:match("(.+)%..+").. ".auth"
10 local function makeForm(id, title, desc)
11 local t = Template("openvpn/pageswitch")
15 local f = SimpleForm(id, title, desc)
21 if not cfg_file or not fs.access(cfg_file) then
22 local f = makeForm("error", nil, translatef("The OVPN config file (%s) could not be found, please check your configuration.", cfg_file or "n/a"))
23 f:append(Template("openvpn/ovpn_css"))
29 if fs.stat(cfg_file).size >= 102400 then
30 local f = makeForm("error", nil,
31 translatef("The size of the OVPN config file (%s) is too large for online editing in LuCI (≥ 100 KB). ", cfg_file)
32 .. translate("Please edit this file directly in a terminal session."))
33 f:append(Template("openvpn/ovpn_css"))
39 f = makeForm("cfg", nil)
40 f:append(Template("openvpn/ovpn_css"))
41 f.submit = translate("Save")
44 s = f:section(SimpleSection, nil, translatef("Section to modify the OVPN config file (%s)", cfg_file))
45 file = s:option(TextValue, "data1")
46 file.datatype = "string"
49 function file.cfgvalue()
50 return fs.readfile(cfg_file) or ""
53 function file.write(self, section, data1)
54 return fs.writefile(cfg_file, "\n" .. util.trim(data1:gsub("\r\n", "\n")) .. "\n")
57 function file.remove(self, section, value)
58 return fs.writefile(cfg_file, "")
61 function s.handle(self, state, data1)
65 s = f:section(SimpleSection, nil, translatef("Section to add an optional 'auth-user-pass' file with your credentials (%s)", auth_file))
66 file = s:option(TextValue, "data2")
67 file.datatype = "string"
70 function file.cfgvalue()
71 return fs.readfile(auth_file) or ""
74 function file.write(self, section, data2)
75 return fs.writefile(auth_file, util.trim(data2:gsub("\r\n", "\n")) .. "\n")
78 function file.remove(self, section, value)
79 return fs.writefile(auth_file, "")
82 function s.handle(self, state, data2)