openvpn-file.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 fs.access(cfg_file) and fs.stat(cfg_file).size >= 102400 then
  17. local f = makeForm("error", nil,
  18. translatef("The size of the OVPN config file (%s) is too large for online editing in LuCI (≥ 100 KB). ", cfg_file)
  19. .. translate("Please edit this file directly in a terminal session."))
  20. f:append(Template("openvpn/ovpn_css"))
  21. f.reset = false
  22. f.submit = false
  23. return f
  24. end
  25. f = makeForm("cfg", nil)
  26. f:append(Template("openvpn/ovpn_css"))
  27. f.submit = translate("Save")
  28. f.reset = false
  29. s = f:section(SimpleSection, nil, translatef("Section to modify the OVPN config file (%s)", cfg_file))
  30. file = s:option(TextValue, "data1")
  31. file.datatype = "string"
  32. file.rows = 20
  33. function file.cfgvalue()
  34. return fs.readfile(cfg_file) or ""
  35. end
  36. function file.write(self, section, data1)
  37. return fs.writefile(cfg_file, util.trim(data1:gsub("\r\n", "\n")) .. "\n")
  38. end
  39. function file.remove(self, section, value)
  40. return fs.writefile(cfg_file, "")
  41. end
  42. function s.handle(self, state, data1)
  43. return true
  44. end
  45. s = f:section(SimpleSection, nil, translatef("Section to add an optional 'auth-user-pass' file with your credentials (%s)", auth_file))
  46. file = s:option(TextValue, "data2")
  47. file.datatype = "string"
  48. file.rows = 5
  49. function file.cfgvalue()
  50. return fs.readfile(auth_file) or ""
  51. end
  52. function file.write(self, section, data2)
  53. return fs.writefile(auth_file, util.trim(data2:gsub("\r\n", "\n")) .. "\n")
  54. end
  55. function file.remove(self, section, value)
  56. return fs.writefile(auth_file, "")
  57. end
  58. function s.handle(self, state, data2)
  59. return true
  60. end
  61. return f