applications, modules: remove i18n handling from controller modules as it moved to...
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.controller.admin.network", package.seeall)
17
18 function index()
19 local uci = require("luci.model.uci").cursor()
20 local net = require "luci.model.network".init(uci)
21 local has_wifi = nixio.fs.stat("/etc/config/wireless")
22 local has_switch = false
23
24 uci:foreach("network", "switch",
25 function(s)
26 has_switch = true
27 return false
28 end
29 )
30
31 local page
32
33 page = node("admin", "network")
34 page.target = alias("admin", "network", "network")
35 page.title = _("Network")
36 page.order = 50
37 page.index = true
38
39 if has_switch then
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = _("Switch")
43 page.order = 20
44 end
45
46 if has_wifi and has_wifi.size > 0 then
47 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wifi"), 15)
48 page.leaf = true
49 page.subindex = true
50
51 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
52 page.leaf = true
53
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
55 page.leaf = true
56
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
58 page.leaf = true
59
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
61 page.leaf = true
62
63 local wdev
64 for _, wdev in ipairs(net:get_wifidevs()) do
65 local wnet
66 for _, wnet in ipairs(wdev:get_wifinets()) do
67 entry(
68 {"admin", "network", "wireless", wnet:id()},
69 alias("admin", "network", "wireless"),
70 wdev:name() .. ": " .. wnet:shortname()
71 )
72 end
73 end
74 end
75
76 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)
77 page.leaf = true
78 page.subindex = true
79
80 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
81 page.leaf = true
82
83 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
84 page.leaf = true
85
86 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
87 page.leaf = true
88
89 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
90 page.leaf = true
91
92 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
93 page.leaf = true
94
95 uci:foreach("network", "interface",
96 function (section)
97 local ifc = section[".name"]
98 if ifc ~= "loopback" then
99 entry({"admin", "network", "network", ifc},
100 true,
101 ifc:upper())
102 end
103 end
104 )
105
106 if nixio.fs.access("/etc/config/dhcp") then
107 page = node("admin", "network", "dhcp")
108 page.target = cbi("admin_network/dhcp")
109 page.title = _("DHCP and DNS")
110 page.order = 30
111
112 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
113 page.leaf = true
114
115 page = node("admin", "network", "hosts")
116 page.target = cbi("admin_network/hosts")
117 page.title = _("Hostnames")
118 page.order = 40
119 end
120
121 page = node("admin", "network", "routes")
122 page.target = cbi("admin_network/routes")
123 page.title = _("Static Routes")
124 page.order = 50
125
126 page = node("admin", "network", "diagnostics")
127 page.target = template("admin_network/diagnostics")
128 page.title = _("Diagnostics")
129 page.order = 60
130
131 page = entry({"admin", "network", "diag_ping"}, call("diag_ping"), nil)
132 page.leaf = true
133
134 page = entry({"admin", "network", "diag_nslookup"}, call("diag_nslookup"), nil)
135 page.leaf = true
136
137 page = entry({"admin", "network", "diag_traceroute"}, call("diag_traceroute"), nil)
138 page.leaf = true
139 end
140
141 function wifi_join()
142 local function param(x)
143 return luci.http.formvalue(x)
144 end
145
146 local function ptable(x)
147 x = param(x)
148 return x and (type(x) ~= "table" and { x } or x) or {}
149 end
150
151 local dev = param("device")
152 local ssid = param("join")
153
154 if dev and ssid then
155 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
156
157 if cancel then
158 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
159 else
160 local cbi = require "luci.cbi"
161 local tpl = require "luci.template"
162 local map = luci.cbi.load("admin_network/wifi_add")[1]
163
164 if map:parse() ~= cbi.FORM_DONE then
165 tpl.render("header")
166 map:render()
167 tpl.render("footer")
168 end
169 end
170 else
171 luci.template.render("admin_network/wifi_join")
172 end
173 end
174
175 function wifi_add()
176 local dev = luci.http.formvalue("device")
177 local ntm = require "luci.model.network".init()
178
179 dev = dev and ntm:get_wifidev(dev)
180
181 if dev then
182 local net = dev:add_wifinet({
183 mode = "ap",
184 ssid = "OpenWrt",
185 encryption = "none"
186 })
187
188 ntm:save("wireless")
189 luci.http.redirect(net:adminlink())
190 end
191 end
192
193 function wifi_delete(network)
194 local ntm = require "luci.model.network".init()
195
196 ntm:del_wifinet(network)
197 ntm:save("wireless")
198
199 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
200 end
201
202 function iface_status()
203 local path = luci.dispatcher.context.requestpath
204 local netm = require "luci.model.network".init()
205 local rv = { }
206
207 local iface
208 for iface in path[#path]:gmatch("[%w%.%-_]+") do
209 local net = netm:get_network(iface)
210 if net then
211 local info
212 local dev = net:ifname()
213 local data = {
214 id = iface,
215 proto = net:proto(),
216 uptime = net:uptime(),
217 gwaddr = net:gwaddr(),
218 dnsaddrs = net:dnsaddrs()
219 }
220 for _, info in ipairs(nixio.getifaddrs()) do
221 local name = info.name:match("[^:]+")
222 if name == dev then
223 if info.family == "packet" then
224 data.flags = info.flags
225 data.stats = info.data
226 data.macaddr = info.addr
227 data.ifname = name
228 elseif info.family == "inet" then
229 data.ipaddrs = data.ipaddrs or { }
230 data.ipaddrs[#data.ipaddrs+1] = {
231 addr = info.addr,
232 broadaddr = info.broadaddr,
233 dstaddr = info.dstaddr,
234 netmask = info.netmask,
235 prefix = info.prefix
236 }
237 elseif info.family == "inet6" then
238 data.ip6addrs = data.ip6addrs or { }
239 data.ip6addrs[#data.ip6addrs+1] = {
240 addr = info.addr,
241 netmask = info.netmask,
242 prefix = info.prefix
243 }
244 end
245 end
246 end
247
248 if next(data) then
249 rv[#rv+1] = data
250 end
251 end
252 end
253
254 if #rv > 0 then
255 luci.http.prepare_content("application/json")
256 luci.http.write_json(rv)
257 return
258 end
259
260 luci.http.status(404, "No such device")
261 end
262
263 function iface_reconnect()
264 local path = luci.dispatcher.context.requestpath
265 local iface = path[#path]
266 local netmd = require "luci.model.network".init()
267
268 local net = netmd:get_network(iface)
269 if net then
270 local ifn
271 for _, ifn in ipairs(net:get_interfaces()) do
272 local wnet = ifn:get_wifinet()
273 if wnet then
274 local wdev = wnet:get_device()
275 if wdev then
276 luci.sys.call(
277 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
278 % wdev:name()
279 )
280
281 luci.http.status(200, "Reconnected")
282 return
283 end
284 end
285 end
286
287 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
288 luci.http.status(200, "Reconnected")
289 return
290 end
291
292 luci.http.status(404, "No such interface")
293 end
294
295 function iface_shutdown()
296 local path = luci.dispatcher.context.requestpath
297 local iface = path[#path]
298 local netmd = require "luci.model.network".init()
299
300 local net = netmd:get_network(iface)
301 if net then
302 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
303 luci.http.status(200, "Shutdown")
304 return
305 end
306
307 luci.http.status(404, "No such interface")
308 end
309
310 function iface_delete()
311 local path = luci.dispatcher.context.requestpath
312 local iface = path[#path]
313 local netmd = require "luci.model.network".init()
314
315 local net = netmd:del_network(iface)
316 if net then
317 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
318 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
319 netmd:commit("network")
320 netmd:commit("wireless")
321 return
322 end
323
324 luci.http.status(404, "No such interface")
325 end
326
327 function wifi_status()
328 local path = luci.dispatcher.context.requestpath
329 local s = require "luci.tools.status"
330 local rv = { }
331
332 local dev
333 for dev in path[#path]:gmatch("[%w%.%-]+") do
334 rv[#rv+1] = s.wifi_network(dev)
335 end
336
337 if #rv > 0 then
338 luci.http.prepare_content("application/json")
339 luci.http.write_json(rv)
340 return
341 end
342
343 luci.http.status(404, "No such device")
344 end
345
346 function lease_status()
347 local s = require "luci.tools.status"
348
349 luci.http.prepare_content("application/json")
350 luci.http.write_json(s.dhcp_leases())
351 end
352
353 function diag_command(cmd)
354 local path = luci.dispatcher.context.requestpath
355 local addr = path[#path]
356
357 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
358 luci.http.prepare_content("text/plain")
359
360 local util = io.popen(cmd % addr)
361 if util then
362 while true do
363 local ln = util:read("*l")
364 if not ln then break end
365 luci.http.write(ln)
366 luci.http.write("\n")
367 end
368
369 util:close()
370 end
371
372 return
373 end
374
375 luci.http.status(500, "Bad address")
376 end
377
378 function diag_ping()
379 diag_command("ping -c 5 -W 1 %q 2>&1")
380 end
381
382 function diag_traceroute()
383 diag_command("traceroute -q 1 -w 1 -n %q 2>&1")
384 end
385
386 function diag_nslookup()
387 diag_command("nslookup %q 2>&1")
388 end