openvpn-file.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. -- Licensed to the public under the Apache License 2.0.
  2. local ip = require("luci.ip")
  3. local fs = require("nixio.fs")
  4. local util = require("luci.util")
  5. local uci = require("luci.model.uci").cursor()
  6. local cfg_file = uci:get("openvpn", arg[1], "config")
  7. local auth_file = cfg_file:match("(.+)%..+").. ".auth"
  8. local function makeForm(id, title, desc)
  9. local t = Template("openvpn/pageswitch")
  10. t.mode = "file"
  11. t.instance = arg[1]
  12. local f = SimpleForm(id, title, desc)
  13. f:append(t)
  14. return f
  15. end
  16. if not cfg_file or not fs.access(cfg_file) then
  17. local f = makeForm("error", nil, translatef("The OVPN config file (%s) could not be found, please check your configuration.", cfg_file or "n/a"))
  18. f:append(Template("openvpn/ovpn_css"))
  19. f.reset = false
  20. f.submit = false
  21. return f
  22. end
  23. if fs.stat(cfg_file).size >= 102400 then
  24. local f = makeForm("error", nil,
  25. translatef("The size of the OVPN config file (%s) is too large for online editing in LuCI (≥ 100 KB). ", cfg_file)
  26. .. translate("Please edit this file directly in a terminal session."))
  27. f:append(Template("openvpn/ovpn_css"))
  28. f.reset = false
  29. f.submit = false
  30. return f
  31. end
  32. f = makeForm("cfg", nil)
  33. f:append(Template("openvpn/ovpn_css"))
  34. f.submit = translate("Save")
  35. f.reset = false
  36. s = f:section(SimpleSection, nil, translatef("Section to modify the OVPN config file (%s)", cfg_file))
  37. file = s:option(TextValue, "data1")
  38. file.datatype = "string"
  39. file.rows = 20
  40. function file.cfgvalue()
  41. return fs.readfile(cfg_file) or ""
  42. end
  43. function file.write(self, section, data1)
  44. return fs.writefile(cfg_file, util.trim(data1:gsub("\r\n", "\n")) .. "\n")
  45. end
  46. function file.remove(self, section, value)
  47. return fs.writefile(cfg_file, "")
  48. end
  49. function s.handle(self, state, data1)
  50. return true
  51. end
  52. s = f:section(SimpleSection, nil, translatef("Section to add an optional 'auth-user-pass' file with your credentials (%s)", auth_file))
  53. file = s:option(TextValue, "data2")
  54. file.datatype = "string"
  55. file.rows = 5
  56. function file.cfgvalue()
  57. return fs.readfile(auth_file) or ""
  58. end
  59. function file.write(self, section, data2)
  60. return fs.writefile(auth_file, util.trim(data2:gsub("\r\n", "\n")) .. "\n")
  61. end
  62. function file.remove(self, section, value)
  63. return fs.writefile(auth_file, "")
  64. end
  65. function s.handle(self, state, data2)
  66. return true
  67. end
  68. return f