From bba585f063ccc1e483346c9b5625d4dcf84d6586 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Sun, 27 Apr 2008 16:12:24 +0000 Subject: [PATCH] * Added initial version of RPC info API * Fixed client splash --- .../package/ffluci-splash/src/luci-splash.lua | 5 ++- .../src/luci-splash/htdocs/cgi-bin/index.cgi | 15 +++++++- .../ffluci-splash/src/luci_splash.init | 5 +-- contrib/package/ffluci/Makefile | 3 ++ core/src/ffluci/http.lua | 2 ++ core/src/ffluci/model/uci.lua | 17 +++++++-- core/src/ffluci/sys.lua | 29 +++++++++++++-- .../admin-core/src/controller/admin/uci.lua | 2 +- .../src/controller/splash/splash.lua | 10 ++++-- .../src/model/cbi/admin_index/contact.lua | 2 +- .../src/model/cbi/admin_index/freifunk.lua | 13 +++++++ module/admin-core/src/model/menu/00admin.lua | 1 + module/admin-core/src/view/splash/splash.htm | 28 +++++++++------ .../src/view/splash_splash/splash.htm | 5 +-- .../src/view/public_status/index.htm | 11 +----- .../src/view/public_status/routes.htm | 13 ++----- module/rpc-core/Makefile | 35 +++++++++++++++++++ .../rpc-core/src/controller/rpc/luciinfo.lua | 35 +++++++++++++++++++ 18 files changed, 180 insertions(+), 51 deletions(-) create mode 100644 module/admin-core/src/model/cbi/admin_index/freifunk.lua create mode 100644 module/rpc-core/Makefile create mode 100644 module/rpc-core/src/controller/rpc/luciinfo.lua diff --git a/contrib/package/ffluci-splash/src/luci-splash.lua b/contrib/package/ffluci-splash/src/luci-splash.lua index 54c32add6d..0699d2820a 100644 --- a/contrib/package/ffluci-splash/src/luci-splash.lua +++ b/contrib/package/ffluci-splash/src/luci-splash.lua @@ -10,7 +10,6 @@ require("ffluci.model.uci") uci = ffluci.model.uci.Session("/var/state") --- Parse stdin and do something function main(argv) local cmd = argv[1] local arg = argv[2] @@ -156,7 +155,7 @@ function sync() local n = uci:add("luci_splash", "lease") uci:set("luci_splash", n, "mac", v.mac) uci:set("luci_splash", n, "start", v.start) - written[v.mac] = 1 + written[v.mac:lower()] = 1 end end end @@ -164,7 +163,7 @@ function sync() -- Delete rules without state for i, r in ipairs(listrules()) do - if #r > 0 and not written[r] then + if #r > 0 and not written[r:lower()] then remove_rule(r) end end diff --git a/contrib/package/ffluci-splash/src/luci-splash/htdocs/cgi-bin/index.cgi b/contrib/package/ffluci-splash/src/luci-splash/htdocs/cgi-bin/index.cgi index 4cbddc0c5b..91a6b3397f 100644 --- a/contrib/package/ffluci-splash/src/luci-splash/htdocs/cgi-bin/index.cgi +++ b/contrib/package/ffluci-splash/src/luci-splash/htdocs/cgi-bin/index.cgi @@ -7,22 +7,35 @@ require("ffluci.sys") require("ffluci.model.uci") local srv +local net local ip = ffluci.http.remote_addr() for k, v in pairs(ffluci.model.uci.show("network").network) do if v[".type"] == "interface" and v.ipaddr then local p = ffluci.sys.net.mask4prefix(v.netmask) if ffluci.sys.net.belongs(ip, v.ipaddr, p) then + net = k srv = v.ipaddr break end end end +local stat = false +for k, v in pairs(ffluci.model.uci.show("luci_splash").luci_splash) do + if v[".type"] == "iface" and v.network == net then + stat = true + end +end + if not srv then ffluci.http.textheader() return print("Unable to detect network settings!") end +if not stat then + ffluci.http.redirect("http://" .. srv) +end + local action = "splash" local mac = ffluci.sys.net.ip4mac(ip) @@ -30,7 +43,7 @@ if not mac then action = "unknown" end -local status = ffluci.sys.exec("luci-splash status "..mac) +local status = ffluci.sys.execl("luci-splash status "..mac)[1] if status == "whitelisted" or status == "lease" then action = "allowed" diff --git a/contrib/package/ffluci-splash/src/luci_splash.init b/contrib/package/ffluci-splash/src/luci_splash.init index a7bb4abeb4..bce432ed30 100644 --- a/contrib/package/ffluci-splash/src/luci_splash.init +++ b/contrib/package/ffluci-splash/src/luci_splash.init @@ -20,7 +20,7 @@ iface_add() { eval "$(ipcalc.sh $ipaddr $netmask)" iptables -t nat -A luci_splash -i "$iface" -s "$IP/$PREFIX" -j luci_splash_portal - iptables -t nat -A luci_splash_portal -i "$iface" -s "$IP/$PREFIX" -d "$ipaddr" -p tcp --dport 80 -j RETURN + iptables -t nat -A luci_splash_portal -i "$iface" -s "$IP/$PREFIX" -d "$ipaddr" -p tcp -m multiport --dports 22,80,443 -j RETURN } blacklist_add() { @@ -64,9 +64,6 @@ start() { ### Start the splash httpd httpd -c /etc/luci_splash_httpd.conf -p 8082 -h /usr/lib/luci-splash/htdocs - ### Sync leases - /usr/lib/luci-splash/sync.lua - ### Hook in the chain iptables -t nat -A prerouting_rule -j luci_splash } diff --git a/contrib/package/ffluci/Makefile b/contrib/package/ffluci/Makefile index 1c45695bb0..aba1509e99 100644 --- a/contrib/package/ffluci/Makefile +++ b/contrib/package/ffluci/Makefile @@ -37,6 +37,7 @@ define Build/Compile $(MAKE) -C $(PKG_BUILD_DIR)/core $(MAKE_ACTION) $(MAKE) -C $(PKG_BUILD_DIR)/module/admin-core $(MAKE_ACTION) $(MAKE) -C $(PKG_BUILD_DIR)/module/public-core $(MAKE_ACTION) + $(MAKE) -C $(PKG_BUILD_DIR)/module/rpc-core $(MAKE_ACTION) endef define Package/ffluci/install @@ -66,6 +67,8 @@ define Package/ffluci/install $(CP) $(PKG_BUILD_DIR)/module/public-core/dist/* $(1)/usr/lib/lua/ffluci/ -R $(CP) $(PKG_BUILD_DIR)/module/public-core/contrib/media $(1)/www/ffluci/ -R + $(CP) $(PKG_BUILD_DIR)/module/rpc-core/dist/* $(1)/usr/lib/lua/ffluci/ -R + $(CP) -a ./ipkg/ffluci.postinst $(1)/CONTROL/postinst $(CP) -a ./ipkg/conffiles $(1)/CONTROL/conffiles rm $(DL_DIR)/$(PKG_SOURCE) diff --git a/core/src/ffluci/http.lua b/core/src/ffluci/http.lua index 44d1a925c4..62b9da1132 100644 --- a/core/src/ffluci/http.lua +++ b/core/src/ffluci/http.lua @@ -27,6 +27,8 @@ limitations under the License. ]]-- +ENV = ENV or {} +FORM = FORM or {} module("ffluci.http", package.seeall) require("ffluci.util") diff --git a/core/src/ffluci/model/uci.lua b/core/src/ffluci/model/uci.lua index 0e3a79fcbe..75a898acbd 100644 --- a/core/src/ffluci/model/uci.lua +++ b/core/src/ffluci/model/uci.lua @@ -52,6 +52,13 @@ end -- The default Session local default = Session() +local state = Session("/var/state") + +-- The state Session +function StateSession() + return state +end + -- Wrapper for "uci add" function Session.add(self, config, section_type) @@ -114,8 +121,8 @@ end -- Wrapper for "uci show" -function Session.show(self, config) - return self:_uci3("show " .. _path(config)) +function Session.show(self, config, ...) + return self:_uci3("show " .. _path(config), ...) end function show(...) @@ -155,11 +162,15 @@ function Session._uci2(self, cmd) end end -function Session._uci3(self, cmd) +function Session._uci3(self, cmd, raw) local res = ffluci.sys.execl(self.ucicmd .. " 2>&1 " .. cmd) if res[1] and res[1]:sub(1, self.ucicmd:len()+1) == self.ucicmd..":" then return nil, res[1] end + + if raw then + return table.concat(res, "\n") + end tbl = {} diff --git a/core/src/ffluci/sys.lua b/core/src/ffluci/sys.lua index 8aa77cf13b..d71bce71bc 100644 --- a/core/src/ffluci/sys.lua +++ b/core/src/ffluci/sys.lua @@ -29,6 +29,13 @@ require("posix") require("ffluci.bits") require("ffluci.util") +-- Returns whether a system is bigendian +function bigendian() + local fp = io.open("/bin/sh") + fp:seek("set", 5) + return (fp:read(1):byte() ~= 1) +end + -- Runs "command" and returns its output function exec(command) local pp = io.popen(command) @@ -125,6 +132,20 @@ function net.belongs(ip, ipnet, prefix) return (net.ip4bin(ip):sub(1, prefix) == net.ip4bin(ipnet):sub(1, prefix)) end +-- Detect the default route +function net.defaultroute() + local routes = net.routes() + local route = nil + + for i, r in pairs(ffluci.sys.net.routes()) do + if r.Destination == "00000000" and (not route or route.Metric > r.Metric) then + route = r + end + end + + return route +end + -- Returns all available network interfaces function net.devices() local devices = {} @@ -163,16 +184,18 @@ function net.routes() return _parse_delimited_table(io.lines("/proc/net/route")) end --- Returns the numeric IP to a given hexstring (little endian) -function net.hexip4(hex, bigendian) +-- Returns the numeric IP to a given hexstring +function net.hexip4(hex, be) if #hex ~= 8 then return nil end + be = be or bigendian() + local hexdec = ffluci.bits.Hex2Dec local ip = "" - if bigendian then + if be then ip = ip .. tostring(hexdec(hex:sub(1,2))) .. "." ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "." ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "." diff --git a/module/admin-core/src/controller/admin/uci.lua b/module/admin-core/src/controller/admin/uci.lua index 2d19db1b29..1a3ae1279f 100644 --- a/module/admin-core/src/controller/admin/uci.lua +++ b/module/admin-core/src/controller/admin/uci.lua @@ -13,7 +13,7 @@ function action_apply() -- Collect files to be applied for i, line in ipairs(ffluci.util.split(changes)) do local r = line:match("^-?([^.]+)") - if r then + if r and not ffluci.util.contains(apply, ffluci.config.uci_oncommit[r]) then table.insert(apply, ffluci.config.uci_oncommit[r]) end end diff --git a/module/admin-core/src/controller/splash/splash.lua b/module/admin-core/src/controller/splash/splash.lua index 7aec62d33c..4e8a79a22f 100644 --- a/module/admin-core/src/controller/splash/splash.lua +++ b/module/admin-core/src/controller/splash/splash.lua @@ -1,9 +1,13 @@ -module("ffluci.controller.public.splash", package.seeall) +module("ffluci.controller.splash.splash", package.seeall) function action_activate() local mac = ffluci.sys.net.ip4mac(ffluci.http.remote_addr()) - os.execute("luci-splash add "..mac) - ffluci.http.request_redirect() + if mac and ffluci.http.formvalue("accept") then + os.execute("luci-splash add "..mac.." >/dev/null 2>&1") + ffluci.http.redirect(ffluci.model.uci.get("freifunk", "community", "homepage")) + else + ffluci.http.request_redirect() + end end function action_accepted() diff --git a/module/admin-core/src/model/cbi/admin_index/contact.lua b/module/admin-core/src/model/cbi/admin_index/contact.lua index ecb4bedd78..66a1ec4bde 100644 --- a/module/admin-core/src/model/cbi/admin_index/contact.lua +++ b/module/admin-core/src/model/cbi/admin_index/contact.lua @@ -5,7 +5,7 @@ Diese Informationen sollten nach der Picopeering Vereinbarung mindestens deine E Damit dein Knoten durch Topographieprogramme erfasst werden kann, gib bitte deine Geokoordinaten oder zumindest deine Straße und Hausnummer unter Standort an.]])) -c = m:section(NamedSection, "contact") +c = m:section(NamedSection, "contact", "public") c:option(Value, "nickname", translate("nickname", "Pseudonym")) c:option(Value, "name", translate("name", "Name")) diff --git a/module/admin-core/src/model/cbi/admin_index/freifunk.lua b/module/admin-core/src/model/cbi/admin_index/freifunk.lua new file mode 100644 index 0000000000..64e916f32b --- /dev/null +++ b/module/admin-core/src/model/cbi/admin_index/freifunk.lua @@ -0,0 +1,13 @@ +-- Todo: Translate +m = Map("freifunk", "Freifunk", [[Informationen über die lokale Freifunkgemeinschaft.]]) + +c = m:section(NamedSection, "community", "public") + +c:option(Value, "name", "Gemeinschaft") +c:option(Value, "homepage", "Webseite") +c:option(Value, "essid", "ESSID") +c:option(Value, "bssid", "BSSID") +c:option(Value, "realm", "Realm") +c:option(Value, "pool", "Adressbereich") + +return m \ No newline at end of file diff --git a/module/admin-core/src/model/menu/00admin.lua b/module/admin-core/src/model/menu/00admin.lua index 3754a5bc8b..7d56b8b545 100644 --- a/module/admin-core/src/model/menu/00admin.lua +++ b/module/admin-core/src/model/menu/00admin.lua @@ -1,6 +1,7 @@ add("admin", "index", "Übersicht", 10) act("contact", "Kontakt") act("luci", "Oberfläche") +act("freifunk", "Freifunk") add("admin", "system", "System", 30) act("packages", "Paketverwaltung") diff --git a/module/admin-core/src/view/splash/splash.htm b/module/admin-core/src/view/splash/splash.htm index 2cae0cdf97..db4bd0f780 100644 --- a/module/admin-core/src/view/splash/splash.htm +++ b/module/admin-core/src/view/splash/splash.htm @@ -1,23 +1,31 @@ +

<%:welcome Willkommen%>!

+

Du bist jetzt mit dem freien Funknetz -<%~freifunk.community.name%> -verbunden. Wir sind ein experimentelles Gemeinschaftsnetzwerk, aber kein Internetanbieter. - -
+<%~freifunk.community.name%> verbunden.
+Wir sind ein experimentelles Gemeinschaftsnetzwerk, aber kein Internetanbieter. +

+

Ein Zugang ins Internet ist trotzdem möglich, da einige Freifunker ihre privaten Internetzugänge zur Verfügung stellen. Diese Zugänge müssen sich hier alle teilen. Bitte sei Dir dessen bewusst und verhalte Dich dementsprechend:

+

-Wenn Du unsere Idee gut findest, kannst Du mitmachen bzw. uns unterstützen: +Wenn Du unsere Idee gut findest, kannst Du uns unterstützen:

+

+ +

+Mit einem Klick auf <%:accept Annehmen%> kannst du für <%~luci_splash.general.leasetime%> Stunden +über unser Netz das Internet verwenden. Dann wirst du erneut aufgefordet, diese Bedingungen zu akzeptieren.

\ No newline at end of file diff --git a/module/admin-core/src/view/splash_splash/splash.htm b/module/admin-core/src/view/splash_splash/splash.htm index fee61e0278..9c165802fb 100644 --- a/module/admin-core/src/view/splash_splash/splash.htm +++ b/module/admin-core/src/view/splash_splash/splash.htm @@ -1,6 +1,7 @@ <%+header%> <%+splash/splash%> -
- + + +
<%+footer%> \ No newline at end of file diff --git a/module/public-core/src/view/public_status/index.htm b/module/public-core/src/view/public_status/index.htm index 44c4ea7a76..be0b96888b 100644 --- a/module/public-core/src/view/public_status/index.htm +++ b/module/public-core/src/view/public_status/index.htm @@ -47,19 +47,10 @@ <%:iface Schnittstelle%> <% - --- UGLY hack is UGLY -if routes[1] and routes[1].Gateway:sub(-2) == "00" then - local be = true -else - local be = false -end - - for i, rt in pairs(routes) do %> -<%=ffluci.sys.net.hexip4(rt.Gateway, be)%> +<%=ffluci.sys.net.hexip4(rt.Gateway)%> <%=rt.Metric%> <%=rt.Iface%> diff --git a/module/public-core/src/view/public_status/routes.htm b/module/public-core/src/view/public_status/routes.htm index cd694d3246..85b02212d7 100644 --- a/module/public-core/src/view/public_status/routes.htm +++ b/module/public-core/src/view/public_status/routes.htm @@ -13,19 +13,12 @@ <% local routes = ffluci.sys.net.routes() --- UGLY hack is UGLY -if routes[1] and routes[1].Gateway:sub(-2) == "00" then - local be = true -else - local be = false -end - for i, r in pairs(routes) do %> -<%=ffluci.sys.net.hexip4(r.Destination, be)%> -<%=ffluci.sys.net.hexip4(r.Mask, be)%> -<%=ffluci.sys.net.hexip4(r.Gateway, be)%> +<%=ffluci.sys.net.hexip4(r.Destination)%> +<%=ffluci.sys.net.hexip4(r.Mask)%> +<%=ffluci.sys.net.hexip4(r.Gateway)%> <%=r.Metric%> <%=r.Iface%> diff --git a/module/rpc-core/Makefile b/module/rpc-core/Makefile new file mode 100644 index 0000000000..113799af6c --- /dev/null +++ b/module/rpc-core/Makefile @@ -0,0 +1,35 @@ +LUAC = luac +LUAC_OPTIONS = -s + +FILES = i18n/* view/*/*.htm + +CFILES = controller/*/*.lua model/cbi/*/*.lua model/menu/*.lua + +DIRECTORIES = model/cbi model/menu controller i18n view + + +INFILES = $(CFILES:%=src/%) +OUTDIRS = $(DIRECTORIES:%=dist/%) +CPFILES = $(FILES:%=src/%) + +.PHONY: all compile source clean depends + +all: compile + + +depends: + mkdir -p $(OUTDIRS) + for i in $(CPFILES); do if [ -f "$$i" ]; then i=$$(echo $$i | cut -d/ -f2-); \ + mkdir -p dist/$$(dirname $$i); cp src/$$i dist/$$i; fi; done + +compile: depends + for i in $(INFILES); do if [ -f "$$i" ]; then i=$$(echo $$i | cut -d/ -f2-); \ + mkdir -p dist/$$(dirname $$i); $(LUAC) $(LUAC_OPTIONS) -o dist/$$i src/$$i; fi; done + +source: depends + for i in $(INFILES); do if [ -f "$$i" ]; then i=$$(echo $$i | cut -d/ -f2-); \ + mkdir -p dist/$$(dirname $$i); cp src/$$i dist/$$i; fi; done + + +clean: + rm dist -rf diff --git a/module/rpc-core/src/controller/rpc/luciinfo.lua b/module/rpc-core/src/controller/rpc/luciinfo.lua new file mode 100644 index 0000000000..8d10fa8850 --- /dev/null +++ b/module/rpc-core/src/controller/rpc/luciinfo.lua @@ -0,0 +1,35 @@ +module("ffluci.controller.rpc.luciinfo", package.seeall) + +function action_index() + local uci = ffluci.model.uci.StateSession() + + ffluci.http.textheader() + + -- General + print("luciinfo.api=1") + print("luciinfo.version=" .. tostring(ffluci.__version__)) + + -- Sysinfo + local s, m, r = ffluci.sys.sysinfo() + local dr = ffluci.sys.net.defaultroute() + dr = dr and ffluci.sys.net.hexip4(dr.Gateway) or "" + local l1, l5, l15 = ffluci.sys.loadavg() + + print("sysinfo.system=" .. sanitize(s)) + print("sysinfo.cpu=" .. sanitize(m)) + print("sysinfo.ram=" .. sanitize(r)) + print("sysinfo.hostname=" .. sanitize(ffluci.sys.hostname())) + print("sysinfo.load1=" .. tostring(l1)) + print("sysinfo.load5=" .. tostring(l5)) + print("sysinfo.load15=" .. tostring(l15)) + print("sysinfo.defaultgw=" .. dr) + + + -- Freifunk + local ff = uci:show("freifunk", true) or "" + print(ff) +end + +function sanitize(val) + return val:gsub("\n", "\t") +end \ No newline at end of file -- 2.30.2