2
0

openvpn.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 file = "/etc/openvpn/" ..name.. ".ovpn"
  20. if name and upload then
  21. local fp
  22. http.setfilehandler(
  23. function(meta, chunk, eof)
  24. local data = util.trim(chunk:gsub("\r\n", "\n")) .. "\n"
  25. data = util.trim(data:gsub("[\128-\255]", ""))
  26. if not fp and meta and meta.name == "ovpn_file" then
  27. fp = io.open(file, "w")
  28. end
  29. if fp and data then
  30. fp:write(data)
  31. end
  32. if fp and eof then
  33. fp:close()
  34. end
  35. end
  36. )
  37. if fs.access(file) then
  38. if not uci:get_first("openvpn", name) then
  39. uci:set("openvpn", name, "openvpn")
  40. uci:set("openvpn", name, "config", file)
  41. uci:save("openvpn")
  42. uci:commit("openvpn")
  43. end
  44. end
  45. end
  46. http.redirect(luci.dispatcher.build_url('admin/vpn/openvpn'))
  47. end