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