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