openvpn.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local fs = require "nixio.fs"
  4. local sys = require "luci.sys"
  5. local uci = require "luci.model.uci".cursor()
  6. local testfullps = luci.sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
  7. local psstring = (string.len(testfullps)>0) and "ps w" or "ps axfw" --set command we use to get pid
  8. local m = Map("openvpn", translate("OpenVPN"))
  9. local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
  10. s.template = "cbi/tblsection"
  11. s.template_addremove = "openvpn/cbi-select-input-add"
  12. s.addremove = true
  13. s.add_select_options = { }
  14. s.extedit = luci.dispatcher.build_url(
  15. "admin", "services", "openvpn", "basic", "%s"
  16. )
  17. uci:load("openvpn_recipes")
  18. uci:foreach( "openvpn_recipes", "openvpn_recipe",
  19. function(section)
  20. s.add_select_options[section['.name']] =
  21. section['_description'] or section['.name']
  22. end
  23. )
  24. function s.getPID(section) -- Universal function which returns valid pid # or nil
  25. local pid = sys.exec("%s | grep -w %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
  26. if pid and #pid > 0 and tonumber(pid) ~= nil then
  27. return tonumber(pid)
  28. else
  29. return nil
  30. end
  31. end
  32. function s.parse(self, section)
  33. local recipe = luci.http.formvalue(
  34. luci.cbi.CREATE_PREFIX .. self.config .. "." ..
  35. self.sectiontype .. ".select"
  36. )
  37. if recipe and not s.add_select_options[recipe] then
  38. self.invalid_cts = true
  39. else
  40. TypedSection.parse( self, section )
  41. end
  42. end
  43. function s.create(self, name)
  44. local recipe = luci.http.formvalue(
  45. luci.cbi.CREATE_PREFIX .. self.config .. "." ..
  46. self.sectiontype .. ".select"
  47. )
  48. name = luci.http.formvalue(
  49. luci.cbi.CREATE_PREFIX .. self.config .. "." ..
  50. self.sectiontype .. ".text"
  51. )
  52. if string.len(name)>3 and not name:match("[^a-zA-Z0-9_]") then
  53. uci:section(
  54. "openvpn", "openvpn", name,
  55. uci:get_all( "openvpn_recipes", recipe )
  56. )
  57. uci:delete("openvpn", name, "_role")
  58. uci:delete("openvpn", name, "_description")
  59. uci:save("openvpn")
  60. luci.http.redirect( self.extedit:format(name) )
  61. else
  62. self.invalid_cts = true
  63. end
  64. end
  65. s:option( Flag, "enabled", translate("Enabled") )
  66. local active = s:option( DummyValue, "_active", translate("Started") )
  67. function active.cfgvalue(self, section)
  68. local pid = s.getPID(section)
  69. if pid ~= nil then
  70. return (sys.process.signal(pid, 0))
  71. and translatef("yes (%i)", pid)
  72. or translate("no")
  73. end
  74. return translate("no")
  75. end
  76. local updown = s:option( Button, "_updown", translate("Start/Stop") )
  77. updown._state = false
  78. updown.redirect = luci.dispatcher.build_url(
  79. "admin", "services", "openvpn"
  80. )
  81. function updown.cbid(self, section)
  82. local pid = s.getPID(section)
  83. self._state = pid ~= nil and sys.process.signal(pid, 0)
  84. self.option = self._state and "stop" or "start"
  85. return AbstractValue.cbid(self, section)
  86. end
  87. function updown.cfgvalue(self, section)
  88. self.title = self._state and "stop" or "start"
  89. self.inputstyle = self._state and "reset" or "reload"
  90. end
  91. function updown.write(self, section, value)
  92. if self.option == "stop" then
  93. local pid = s.getPID(section)
  94. if pid ~= nil then
  95. sys.process.signal(pid,15)
  96. end
  97. else
  98. luci.sys.call("/etc/init.d/openvpn start %s" % section)
  99. end
  100. luci.http.redirect( self.redirect )
  101. end
  102. local port = s:option( DummyValue, "port", translate("Port") )
  103. function port.cfgvalue(self, section)
  104. local val = AbstractValue.cfgvalue(self, section)
  105. return val or "1194"
  106. end
  107. local proto = s:option( DummyValue, "proto", translate("Protocol") )
  108. function proto.cfgvalue(self, section)
  109. local val = AbstractValue.cfgvalue(self, section)
  110. return val or "udp"
  111. end
  112. return m