Merge pull request #3545 from michyprima/add-app-ser2net-js
[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 { ListValue,
43 "comp_lzo",
44 {"yes","no","adaptive"},
45 translate("Use fast LZO compression") },
46 { Value,
47 "keepalive",
48 "10 60",
49 translate("Helper directive to simplify the expression of --ping and --ping-restart in server mode configurations") },
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 }
91
92 local has_ipv6 = fs.access("/proc/net/ipv6_route")
93 if has_ipv6 then
94 table.insert( basicParams, { ListValue,
95 "proto",
96 { "udp", "tcp-client", "tcp-server", "udp6", "tcp6-client", "tcp6-server" },
97 translate("Use protocol")
98 })
99 else
100 table.insert( basicParams, { ListValue,
101 "proto",
102 { "udp", "tcp-client", "tcp-server" },
103 translate("Use protocol")
104 })
105 end
106
107 local m = Map("openvpn")
108 m.redirect = luci.dispatcher.build_url("admin", "vpn", "openvpn")
109 m.apply_on_parse = true
110
111 local p = m:section( SimpleSection )
112 p.template = "openvpn/pageswitch"
113 p.mode = "basic"
114 p.instance = arg[1]
115
116
117 local s = m:section( NamedSection, arg[1], "openvpn" )
118
119 for _, option in ipairs(basicParams) do
120 local o = s:option(
121 option[1], option[2],
122 option[2], option[4]
123 )
124
125 o.optional = true
126
127 if option[1] == DummyValue then
128 o.value = option[3]
129 elseif option[1] == FileUpload then
130
131 o.initial_directory = "/etc/openvpn"
132
133 function o.cfgvalue(self, section)
134 local cfg_val = AbstractValue.cfgvalue(self, section)
135
136 if cfg_val then
137 return cfg_val
138 end
139 end
140
141 function o.formvalue(self, section)
142 local sel_val = AbstractValue.formvalue(self, section)
143 local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
144
145 if sel_val and sel_val ~= "" then
146 return sel_val
147 end
148
149 if txt_val and txt_val ~= "" then
150 return txt_val
151 end
152 end
153
154 function o.remove(self, section)
155 local cfg_val = AbstractValue.cfgvalue(self, section)
156 local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
157
158 if cfg_val and fs.access(cfg_val) and txt_val == "" then
159 fs.unlink(cfg_val)
160 end
161 return AbstractValue.remove(self, section)
162 end
163 elseif option[1] == Flag then
164 o.default = nil
165 else
166 if option[1] == DynamicList then
167 function o.cfgvalue(...)
168 local val = AbstractValue.cfgvalue(...)
169 return ( val and type(val) ~= "table" ) and { val } or val
170 end
171 end
172
173 if type(option[3]) == "table" then
174 if o.optional then o:value("", "-- remove --") end
175 for _, v in ipairs(option[3]) do
176 v = tostring(v)
177 o:value(v)
178 end
179 o.default = tostring(option[3][1])
180 else
181 o.default = tostring(option[3])
182 end
183 end
184
185 for i=5,#option do
186 if type(option[i]) == "table" then
187 o:depends(option[i])
188 end
189 end
190 end
191
192 return m