summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-openvpn/luasrc/model/cbi/openvpn.lua
blob: 22cdcb4d6ed65c66f5d549e4253dfa9b14afea9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
--[[
LuCI - Lua Configuration Interface

Copyright 2008 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--

require("luci.fs")
require("luci.ip")
require("luci.sys")
require("luci.model.uci")


local uci = luci.model.uci.cursor()

local m = Map("openvpn")
local s = m:section( TypedSection, "openvpn" )
s.template = "cbi/tblsection"
s.template_addremove = "openvpn/cbi-select-input-add"
s.addremove = true
s.add_select_options = { }
s.extedit = luci.dispatcher.build_url(
	"admin", "services", "openvpn", "basic", "%s"
)

uci:load("openvpn_recipes")
uci:foreach( "openvpn_recipes", "openvpn_recipe",
	function(section)
		s.add_select_options[section['.name']] =
			section['_description'] or section['.name']
	end
)

function s.parse(self, section)
	local recipe = luci.http.formvalue(
		luci.cbi.CREATE_PREFIX .. self.config .. "." ..
		self.sectiontype .. ".select"
	)

	if recipe and not s.add_select_options[recipe] then
		self.invalid_cts = true
	else
		TypedSection.parse( self, section )
	end
end

function s.create(self, name)
	local recipe = luci.http.formvalue(
		luci.cbi.CREATE_PREFIX .. self.config .. "." ..
		self.sectiontype .. ".select"
	)

	uci:section(
		"openvpn", "openvpn", name,
		uci:get_all( "openvpn_recipes", recipe )
	)

	uci:delete("openvpn", name, "_role")
	uci:delete("openvpn", name, "_description")
	uci:save("openvpn")

	luci.http.redirect( self.extedit:format(name) )
end


s:option( Flag, "enable" )

local active = s:option( DummyValue, "_active" )
function active.cfgvalue(self, section)
	if luci.fs.isfile("/var/run/openvpn_%s.pid" % section) then
		local pid = io.lines("/var/run/openvpn_%s.pid" % section)()
		if pid and #pid > 0 and tonumber(pid) ~= nil then
			return (luci.sys.process.signal(pid, 0)) and "yes (" .. pid .. ")" or "no"
		end
	end
	return "no"
end

local port = s:option( DummyValue, "port" )
function port.cfgvalue(self, section)
	local val = AbstractValue.cfgvalue(self, section)
	return val or "1194"
end

local proto = s:option( DummyValue, "proto" )
function proto.cfgvalue(self, section)
	local val = AbstractValue.cfgvalue(self, section)
	return val or "udp"
end


return m