c220b10a56ee60e03368527dbe294da6dca8d28b
[project/luci.git] / applications / luci-openvpn / luasrc / model / cbi / openvpn.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local fs  = require "nixio.fs"
16 local sys = require "luci.sys"
17 local uci = require "luci.model.uci".cursor()
18
19 local m = Map("openvpn", translate("openvpn"))
20 local s = m:section( TypedSection, "openvpn", translate("openvpn_overview"), translate("openvpn_overview_desc") )
21 s.template = "cbi/tblsection"
22 s.template_addremove = "openvpn/cbi-select-input-add"
23 s.addremove = true
24 s.add_select_options = { }
25 s.extedit = luci.dispatcher.build_url(
26         "admin", "services", "openvpn", "basic", "%s"
27 )
28
29 uci:load("openvpn_recipes")
30 uci:foreach( "openvpn_recipes", "openvpn_recipe",
31         function(section)
32                 s.add_select_options[section['.name']] =
33                         section['_description'] or section['.name']
34         end
35 )
36
37 function s.parse(self, section)
38         local recipe = luci.http.formvalue(
39                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
40                 self.sectiontype .. ".select"
41         )
42
43         if recipe and not s.add_select_options[recipe] then
44                 self.invalid_cts = true
45         else
46                 TypedSection.parse( self, section )
47         end
48 end
49
50 function s.create(self, name)
51         local recipe = luci.http.formvalue(
52                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
53                 self.sectiontype .. ".select"
54         )
55
56         uci:section(
57                 "openvpn", "openvpn", name,
58                 uci:get_all( "openvpn_recipes", recipe )
59         )
60
61         uci:delete("openvpn", name, "_role")
62         uci:delete("openvpn", name, "_description")
63         uci:save("openvpn")
64
65         luci.http.redirect( self.extedit:format(name) )
66 end
67
68
69 s:option( Flag, "enable", translate("openvpn_enable") )
70
71 local active = s:option( DummyValue, "_active", translate("openvpn_active") )
72 function active.cfgvalue(self, section)
73         local pid = fs.readfile("/var/run/openvpn-%s.pid" % section)
74         if pid and #pid > 0 and tonumber(pid) ~= nil then
75                 return (sys.process.signal(pid, 0))
76                         and translatef("openvpn_active_yes", "yes (%i)", pid)
77                         or  translate("openvpn_active_no")
78         end
79         return translate("openvpn_active_no")
80 end
81
82 local port = s:option( DummyValue, "port", translate("openvpn_port") )
83 function port.cfgvalue(self, section)
84         local val = AbstractValue.cfgvalue(self, section)
85         return val or "1194"
86 end
87
88 local proto = s:option( DummyValue, "proto", translate("openvpn_proto") )
89 function proto.cfgvalue(self, section)
90         local val = AbstractValue.cfgvalue(self, section)
91         return val or "udp"
92 end
93
94
95 return m