openvpn.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("luci.controller.openvpn", package.seeall)
  5. function index()
  6. entry( {"admin", "vpn", "openvpn"}, cbi("openvpn"), _("OpenVPN") ).acl_depends = { "luci-app-openvpn" }
  7. entry( {"admin", "vpn", "openvpn", "basic"}, cbi("openvpn-basic"), nil ).leaf = true
  8. entry( {"admin", "vpn", "openvpn", "advanced"}, cbi("openvpn-advanced"), nil ).leaf = true
  9. entry( {"admin", "vpn", "openvpn", "file"}, form("openvpn-file"), nil ).leaf = true
  10. entry( {"admin", "vpn", "openvpn", "upload"}, call("ovpn_upload"))
  11. end
  12. function ovpn_upload()
  13. local fs = require("nixio.fs")
  14. local http = require("luci.http")
  15. local util = require("luci.util")
  16. local uci = require("luci.model.uci").cursor()
  17. local upload = http.formvalue("ovpn_file")
  18. local name = http.formvalue("instance_name2")
  19. local basedir = "/etc/openvpn"
  20. local file = basedir.. "/" ..name.. ".ovpn"
  21. if not fs.stat(basedir) then
  22. fs.mkdir(basedir)
  23. end
  24. if name and upload then
  25. local fp
  26. http.setfilehandler(
  27. function(meta, chunk, eof)
  28. local data = chunk:gsub("\r\n", "\n")
  29. if not fp and meta and meta.name == "ovpn_file" then
  30. fp = io.open(file, "w")
  31. end
  32. if fp and data then
  33. fp:write(data)
  34. end
  35. if fp and eof then
  36. fp:close()
  37. end
  38. end
  39. )
  40. if fs.access(file) then
  41. if not uci:get_first("openvpn", name) then
  42. uci:set("openvpn", name, "openvpn")
  43. uci:set("openvpn", name, "config", file)
  44. uci:save("openvpn")
  45. uci:commit("openvpn")
  46. end
  47. end
  48. end
  49. http.redirect(luci.dispatcher.build_url('admin/vpn/openvpn'))
  50. end