From 710cb8b36c8c3107dd182460bcff52772f98a36a Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Sun, 26 Oct 2014 15:51:35 +0100 Subject: [PATCH 1/2] status-page: lua rewrite, use gluon-neighbour-info --- package/gluon-status-page/Makefile | 2 +- .../lib/gluon/status-page/www/cgi-bin/status | 129 +++++++++++------- .../files/lib/gluon/status-page/www/status.js | 9 ++ 3 files changed, 88 insertions(+), 52 deletions(-) create mode 100644 package/gluon-status-page/files/lib/gluon/status-page/www/status.js diff --git a/package/gluon-status-page/Makefile b/package/gluon-status-page/Makefile index d0e9d488..4e44e2b2 100644 --- a/package/gluon-status-page/Makefile +++ b/package/gluon-status-page/Makefile @@ -12,7 +12,7 @@ define Package/gluon-status-page SECTION:=gluon CATEGORY:=Gluon TITLE:=Adds a status page showing information about the node. - DEPENDS:=+gluon-core +uhttpd + DEPENDS:=+gluon-core +gluon-neighbour-info +uhttpd endef define Package/gluon-status-page/description diff --git a/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status b/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status index e25d460f..59c2e00f 100755 --- a/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status +++ b/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status @@ -1,67 +1,94 @@ -#!/bin/sh +#!/usr/bin/lua -model="$(lua -e 'print(require("platform_info").get_model())')" +local util = require("luci.util") +local fs = require("luci.fs") +local sys = require("luci.sys") +local json = require("luci.json") +local platform_info = require("platform_info") -escape_html() { - sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g' -} +local hostname = sys.hostname() +local model = platform_info.get_model() +local release = util.trim(fs.readfile("/lib/gluon/release") or "") -linknodes() { - PREFIX=$(uci get network.local_node_route6.target | cut -d: -f 1-4) - sed 's#\([0-9a-f]\{2\}\):\([0-9a-f]\{2\}\):\([0-9a-f]\{2\}\):\([0-9a-f]\{2\}\):\([0-9a-f]\{2\}\):\([0-9a-f]\{2\}\)#&#g' -} +function neighbours(ifname) + local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 3 -i " .. ifname) + local macs = {} + for _, line in ipairs(util.split(info)) do + local data = json.decode(line) + if data then + if data["network"] and data["network"]["mesh_interfaces"] then + for _, mac in ipairs(data["network"]["mesh_interfaces"]) do + macs[mac] = data + end + end + end + end -echo Content-type: text/html -echo "" + return macs +end -cat < - - - $(cat /proc/sys/kernel/hostname) - - -EOF +io.write("Content-type: text/html\n\n") +io.write("\n") +io.write("") +io.write("") +io.write("") +io.write("" .. hostname .. "") +io.write("") +io.write("") -echo "

$(cat /proc/sys/kernel/hostname)

" +io.write("

" .. hostname .. "

") +io.write("
")
 
-echo "
"
+io.write("Model: " .. model .. "\n")
+io.write("Firmware release: " .. release .. "\n\n")
 
-echo "Model: $model" | escape_html
-echo "Firmware release: $(cat /lib/gluon/release | escape_html)"
-echo
+io.write(util.trim(sys.exec("uptime | sed 's/^ \+//'")) .. "\n\n")
+io.write(sys.exec("ip address show dev br-client") .. "\n")
+io.write(sys.exec("free -m") .. "\n")
+io.write(sys.exec("df /rom /overlay"))
+io.write("
") -uptime | sed 's/^ \+//' | escape_html -echo +io.write("

Neighbours

") -ip address show dev br-client | escape_html -echo +local interfaces = util.split(util.trim(util.exec("iw dev | grep IBSS -B 5 | grep Interface | cut -d' ' -f2"))) -free -m | escape_html -echo +for _, ifname in ipairs(interfaces) do + io.write("

" .. ifname .. "

") + io.write("
")
 
-df /rom /overlay | escape_html
+  io.write(sys.exec("iw dev " .. ifname .. " link") .. "\n")
 
-echo "
" + for _, line in ipairs(util.split(util.exec("iw dev " .. ifname .. " station dump"))) do + local mac = line:match("^Station (.*) %(on ") + if mac then + io.write("Station " .. mac .. " (on " .. ifname .. ")\n") + else + io.write(line .. "\n") + end + end -echo "

Neighbours

" + io.write("
") +end +io.write("") +io.write("") +io.write("") diff --git a/package/gluon-status-page/files/lib/gluon/status-page/www/status.js b/package/gluon-status-page/files/lib/gluon/status-page/www/status.js new file mode 100644 index 00000000..e17102d0 --- /dev/null +++ b/package/gluon-status-page/files/lib/gluon/status-page/www/status.js @@ -0,0 +1,9 @@ +function update_node(id, ip, hostname) { + var el = document.getElementById(id); + + if (!el) + return; + + el.href = "http://[" + ip + "]/"; + el.textContent += " (" + hostname + ")"; +} From 5266a073342924fb6a07e9ae2b21ec0004492742 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Thu, 13 Nov 2014 01:24:46 +0100 Subject: [PATCH 2/2] status-page: escape html, move script to body --- .../lib/gluon/status-page/www/cgi-bin/status | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status b/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status index 59c2e00f..aa74b853 100755 --- a/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status +++ b/package/gluon-status-page/files/lib/gluon/status-page/www/cgi-bin/status @@ -10,6 +10,10 @@ local hostname = sys.hostname() local model = platform_info.get_model() local release = util.trim(fs.readfile("/lib/gluon/release") or "") +function escape_html(s) + return (s:gsub('&', '&'):gsub('<', '<'):gsub('>', '>'):gsub('"', '"')) +end + function neighbours(ifname) local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 3 -i " .. ifname) local macs = {} @@ -32,20 +36,20 @@ io.write("\n") io.write("") io.write("") io.write("") -io.write("" .. hostname .. "") +io.write("" .. escape_html(hostname) .. "") io.write("") io.write("") -io.write("

" .. hostname .. "

") +io.write("

" .. escape_html(hostname) .. "

") io.write("
")
 
-io.write("Model: " .. model .. "\n")
-io.write("Firmware release: " .. release .. "\n\n")
+io.write("Model: " .. escape_html(model) .. "\n")
+io.write("Firmware release: " .. escape_html(release) .. "\n\n")
 
-io.write(util.trim(sys.exec("uptime | sed 's/^ \+//'")) .. "\n\n")
-io.write(sys.exec("ip address show dev br-client") .. "\n")
-io.write(sys.exec("free -m") .. "\n")
-io.write(sys.exec("df /rom /overlay"))
+io.write(escape_html(util.trim(sys.exec("uptime | sed 's/^ \+//'"))) .. "\n\n")
+io.write(escape_html(sys.exec("ip address show dev br-client")) .. "\n")
+io.write(escape_html(sys.exec("free -m")) .. "\n")
+io.write(escape_html(sys.exec("df /rom /overlay")))
 io.write("
") io.write("

Neighbours

") @@ -53,24 +57,23 @@ io.write("

Neighbours

") local interfaces = util.split(util.trim(util.exec("iw dev | grep IBSS -B 5 | grep Interface | cut -d' ' -f2"))) for _, ifname in ipairs(interfaces) do - io.write("

" .. ifname .. "

") + io.write("

" .. escape_html(ifname) .. "

") io.write("
")
 
-  io.write(sys.exec("iw dev " .. ifname .. " link") .. "\n")
+  io.write(escape_html(sys.exec("iw dev " .. ifname .. " link")) .. "\n")
 
   for _, line in ipairs(util.split(util.exec("iw dev " .. ifname .. " station dump"))) do
     local mac = line:match("^Station (.*) %(on ")
     if mac then
-      io.write("Station " .. mac .. " (on " .. ifname .. ")\n")
+      io.write("Station " .. mac .. " (on " .. escape_html(ifname) .. ")\n")
     else
-      io.write(line .. "\n")
+      io.write(escape_html(line) .. "\n")
     end
   end
 
   io.write("
") end -io.write("") io.write("") +io.write("") io.write("")