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