luci-app-openvpn: On apply/save redirect to OpenVPN overview page
[project/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn-basic.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
6 local basicParams = {
7 --
8 -- Widget, Name, Default(s), Description
9 --
10 { ListValue,
11 "verb",
12 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
13 translate("Set output verbosity") },
14 { Value,
15 "nice",
16 0,
17 translate("Change process priority") },
18 { Value,
19 "port",
20 1194,
21 translate("TCP/UDP port # for both local and remote") },
22 { ListValue,
23 "dev_type",
24 { "tun", "tap" },
25 translate("Type of used device") },
26 { Value,
27 "ifconfig",
28 "10.200.200.3 10.200.200.1",
29 translate("Set tun/tap adapter parameters") },
30 { Value,
31 "server",
32 "10.200.200.0 255.255.255.0",
33 translate("Configure server mode") },
34 { Value,
35 "server_bridge",
36 "192.168.1.1 255.255.255.0 192.168.1.128 192.168.1.254",
37 translate("Configure server bridge") },
38 { Flag,
39 "nobind",
40 0,
41 translate("Do not bind to local address and port") },
42 { Value,
43 "keepalive",
44 "10 60",
45 translate("Helper directive to simplify the expression of --ping and --ping-restart in server mode configurations") },
46 { ListValue,
47 "proto",
48 { "udp", "tcp-client", "tcp-server" },
49 translate("Use protocol") },
50 { Flag,
51 "client",
52 0,
53 translate("Configure client mode") },
54 { Flag,
55 "client_to_client",
56 0,
57 translate("Allow client-to-client traffic") },
58 { DynamicList,
59 "remote",
60 "vpnserver.example.org",
61 translate("Remote host name or ip address") },
62 { FileUpload,
63 "secret",
64 "/etc/openvpn/secret.key",
65 translate("Enable Static Key encryption mode (non-TLS)") },
66 { ListValue,
67 "key_direction",
68 { 0, 1 },
69 translate("The key direction for 'tls-auth' and 'secret' options") },
70 { FileUpload,
71 "pkcs12",
72 "/etc/easy-rsa/keys/some-client.pk12",
73 translate("PKCS#12 file containing keys") },
74 { FileUpload,
75 "ca",
76 "/etc/easy-rsa/keys/ca.crt",
77 translate("Certificate authority") },
78 { FileUpload,
79 "dh",
80 "/etc/easy-rsa/keys/dh1024.pem",
81 translate("Diffie Hellman parameters") },
82 { FileUpload,
83 "cert",
84 "/etc/easy-rsa/keys/some-client.crt",
85 translate("Local certificate") },
86 { FileUpload,
87 "key",
88 "/etc/easy-rsa/keys/some-client.key",
89 translate("Local private key") },
90 { Value,
91 "config",
92 "/etc/openvpn/ovpn-file.ovpn",
93 translate("Local OVPN configuration file") },
94 }
95
96
97 local m = Map("openvpn")
98 m.redirect = luci.dispatcher.build_url("admin", "services", "openvpn")
99 m.apply_on_parse = true
100
101 local p = m:section( SimpleSection )
102 p.template = "openvpn/pageswitch"
103 p.mode = "basic"
104 p.instance = arg[1]
105
106
107 local s = m:section( NamedSection, arg[1], "openvpn" )
108
109 for _, option in ipairs(basicParams) do
110 local o = s:option(
111 option[1], option[2],
112 option[2], option[4]
113 )
114
115 o.optional = true
116
117 if option[1] == DummyValue then
118 o.value = option[3]
119 elseif option[1] == FileUpload then
120
121 function o.cfgvalue(self, section)
122 local cfg_val = AbstractValue.cfgvalue(self, section)
123
124 if cfg_val then
125 return cfg_val
126 end
127 end
128
129 function o.formvalue(self, section)
130 local sel_val = AbstractValue.formvalue(self, section)
131 local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
132
133 if sel_val and sel_val ~= "" then
134 return sel_val
135 end
136
137 if txt_val and txt_val ~= "" then
138 return txt_val
139 end
140 end
141
142 function o.remove(self, section)
143 local cfg_val = AbstractValue.cfgvalue(self, section)
144 local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
145
146 if cfg_val and fs.access(cfg_val) and txt_val == "" then
147 fs.unlink(cfg_val)
148 end
149 return AbstractValue.remove(self, section)
150 end
151 else
152 if option[1] == DynamicList then
153 function o.cfgvalue(...)
154 local val = AbstractValue.cfgvalue(...)
155 return ( val and type(val) ~= "table" ) and { val } or val
156 end
157 end
158
159 if type(option[3]) == "table" then
160 if o.optional then o:value("", "-- remove --") end
161 for _, v in ipairs(option[3]) do
162 v = tostring(v)
163 o:value(v)
164 end
165 o.default = tostring(option[3][1])
166 else
167 o.default = tostring(option[3])
168 end
169 end
170
171 for i=5,#option do
172 if type(option[i]) == "table" then
173 o:depends(option[i])
174 end
175 end
176 end
177
178 return m