applications/splash: Fix blacklisting mechanism, expose status on public freifunk...
[project/luci.git] / modules / freifunk / luasrc / controller / freifunk / freifunk.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.freifunk.freifunk", package.seeall)
16
17 function index()
18 local uci = require "luci.model.uci".cursor()
19 local page
20
21 -- Frontend
22 page = node()
23 page.lock = true
24 page.target = alias("freifunk")
25 page.subindex = true
26 page.index = false
27
28 page = node("freifunk")
29 page.title = _("Freifunk")
30 page.target = alias("freifunk", "index")
31 page.order = 5
32 page.setuser = "nobody"
33 page.setgroup = "nogroup"
34 page.i18n = "freifunk"
35 page.index = true
36
37 page = node("freifunk", "index")
38 page.target = template("freifunk/index")
39 page.title = _("Overview")
40 page.order = 10
41 page.indexignore = true
42
43 page = node("freifunk", "contact")
44 page.target = template("freifunk/contact")
45 page.title = _("Contact")
46 page.order = 15
47
48 page = node("freifunk", "status")
49 page.target = template("freifunk/public_status")
50 page.title = _("Status")
51 page.order = 20
52 page.i18n = "base"
53 page.setuser = false
54 page.setgroup = false
55
56 entry({"freifunk", "status.json"}, call("jsonstatus"))
57 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
58 entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true
59
60 if nixio.fs.access("/usr/sbin/luci-splash") then
61 assign({"freifunk", "status", "splash"}, {"splash", "publicstatus"}, _("Splash"), 40)
62 end
63
64 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, _("OLSR"), 30)
65
66 if nixio.fs.access("/etc/config/luci_statistics") then
67 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, _("Statistics"), 40)
68 end
69
70 -- backend
71 assign({"mini", "freifunk"}, {"admin", "freifunk"}, _("Freifunk"), 5)
72 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), _("Freifunk"), 5)
73
74 page = node("admin", "freifunk")
75 page.target = template("freifunk/adminindex")
76 page.title = _("Freifunk")
77 page.order = 5
78
79 page = node("admin", "freifunk", "basics")
80 page.target = cbi("freifunk/basics")
81 page.title = _("Basic Settings")
82 page.order = 5
83
84 page = node("admin", "freifunk", "basics", "profile")
85 page.target = cbi("freifunk/profile")
86 page.title = _("Profile")
87 page.order = 10
88
89 page = node("admin", "freifunk", "basics", "profile_expert")
90 page.target = cbi("freifunk/profile_expert")
91 page.title = _("Profile (Expert)")
92 page.order = 20
93
94 page = node("admin", "freifunk", "Index-Page")
95 page.target = cbi("freifunk/user_index")
96 page.title = _("Index Page")
97 page.order = 50
98
99 page = node("admin", "freifunk", "contact")
100 page.target = cbi("freifunk/contact")
101 page.title = _("Contact")
102 page.order = 15
103
104 entry({"freifunk", "map"}, template("freifunk-map/frame"), _("Map"), 50)
105 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
106 entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
107 end
108
109 local function fetch_olsrd()
110 local sys = require "luci.sys"
111 local util = require "luci.util"
112 local table = require "table"
113 local rawdata = sys.httpget("http://127.0.0.1:2006/")
114
115 if #rawdata == 0 then
116 if nixio.fs.access("/proc/net/ipv6_route", "r") then
117 rawdata = sys.httpget("http://[::1]:2006/")
118 if #rawdata == 0 then
119 return nil
120 end
121 else
122 return nil
123 end
124 end
125
126 local data = {}
127
128 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
129
130
131 for i, tbl in ipairs(tables) do
132 local lines = util.split(tbl, "\r?\n", nil, true)
133 local name = table.remove(lines, 1):sub(8)
134 local keys = util.split(table.remove(lines, 1), "\t")
135 local split = #keys - 1
136
137 data[name] = {}
138
139 for j, line in ipairs(lines) do
140 local fields = util.split(line, "\t", split)
141 data[name][j] = {}
142 for k, key in pairs(keys) do
143 data[name][j][key] = fields[k]
144 end
145
146 if data[name][j].Linkcost then
147 data[name][j].LinkQuality,
148 data[name][j].NLQ,
149 data[name][j].ETX =
150 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
151 end
152 end
153 end
154
155 return data
156 end
157
158 function zeroes()
159 local string = require "string"
160 local http = require "luci.http"
161 local zeroes = string.rep(string.char(0), 8192)
162 local cnt = 0
163 local lim = 1024 * 1024 * 1024
164
165 http.prepare_content("application/x-many-zeroes")
166
167 while cnt < lim do
168 http.write(zeroes)
169 cnt = cnt + #zeroes
170 end
171 end
172
173 function jsonstatus()
174 local root = {}
175 local sys = require "luci.sys"
176 local uci = require "luci.model.uci"
177 local util = require "luci.util"
178 local http = require "luci.http"
179 local json = require "luci.json"
180 local ltn12 = require "luci.ltn12"
181 local version = require "luci.version"
182 local webadmin = require "luci.tools.webadmin"
183
184 local cursor = uci.cursor_state()
185
186 local ffzone = webadmin.firewall_find_zone("freifunk")
187 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
188 local ffwifs = ffznet and util.split(ffznet, " ") or {}
189
190
191 root.protocol = 1
192
193 root.system = {
194 uptime = {sys.uptime()},
195 loadavg = {sys.loadavg()},
196 sysinfo = {sys.sysinfo()},
197 hostname = sys.hostname()
198 }
199
200 root.firmware = {
201 luciname=version.luciname,
202 luciversion=version.luciversion,
203 distname=version.distname,
204 distversion=version.distversion
205 }
206
207 root.freifunk = {}
208 cursor:foreach("freifunk", "public", function(s)
209 root.freifunk[s[".name"]] = s
210 end)
211
212 cursor:foreach("system", "system", function(s)
213 root.geo = {
214 latitude = s.latitude,
215 longitude = s.longitude
216 }
217 end)
218
219 root.network = {}
220 root.wireless = {devices = {}, interfaces = {}, status = {}}
221 local wifs = root.wireless.interfaces
222 local wifidata = luci.sys.wifi.getiwconfig() or {}
223 local netdata = luci.sys.net.deviceinfo() or {}
224
225 for _, vif in ipairs(ffwifs) do
226 root.network[vif] = cursor:get_all("network", vif)
227 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
228 cursor:foreach("wireless", "wifi-iface", function(s)
229 if s.device == vif and s.network == vif then
230 wifs[#wifs+1] = s
231 if s.ifname then
232 root.wireless.status[s.ifname] = wifidata[s.ifname]
233 end
234 end
235 end)
236 end
237
238 root.olsrd = fetch_olsrd()
239
240 http.prepare_content("application/json")
241 ltn12.pump.all(json.Encoder(root):source(), http.write)
242 end
243
244 function public_status_json()
245 local twa = require "luci.tools.webadmin"
246 local sys = require "luci.sys"
247 local i18n = require "luci.i18n"
248 local path = luci.dispatcher.context.requestpath
249 local rv = { }
250
251 local dev
252 for dev in path[#path]:gmatch("[%w%.%-]+") do
253 local j = { id = dev }
254 local iw = luci.sys.wifi.getiwinfo(dev)
255 if iw then
256 local f
257 for _, f in ipairs({
258 "channel", "txpower", "bitrate", "signal", "noise",
259 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
260 }) do
261 j[f] = iw[f]
262 end
263 end
264 rv[#rv+1] = j
265 end
266
267 local load1, load5, load15 = sys.loadavg()
268
269 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
270 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
271 tonumber(memtotal) / 1024,
272 tonumber(memtotal - memfree) / 1024,
273 tostring(i18n.translate("used")),
274 memfree / 1024,
275 tostring(i18n.translate("free")),
276 memcached / 1024,
277 tostring(i18n.translate("cached")),
278 membuffers / 1024,
279 tostring(i18n.translate("buffered"))
280 )
281
282 local dr4 = sys.net.defaultroute()
283 local dr6 = sys.net.defaultroute6()
284
285 if dr6 then
286 def6 = {
287 gateway = dr6.nexthop:string(),
288 dest = dr6.dest:string(),
289 dev = dr6.device,
290 metr = dr6.metric }
291 end
292
293 if dr4 then
294 def4 = {
295 gateway = dr4.gateway:string(),
296 dest = dr4.dest:string(),
297 dev = dr4.device,
298 metr = dr4.metric }
299 else
300 local dr = sys.exec("ip r s t olsr-default")
301 if dr then
302 local dest, gateway, dev, metr = dr:match("^(%w+) via (%d+.%d+.%d+.%d+) dev (%w+) +metric (%d+)")
303 def4 = {
304 dest = dest,
305 gateway = gateway,
306 dev = dev,
307 metr = metr
308 }
309 end
310 end
311
312 rv[#rv+1] = {
313 time = os.date("%a, %d %b %Y, %H:%M:%S"),
314 uptime = twa.date_format(tonumber(sys.uptime())),
315 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
316 mem = mem,
317 defroutev4 = def4,
318 defroutev6 = def6
319 }
320
321 luci.http.prepare_content("application/json")
322 luci.http.write_json(rv)
323 return
324 end