From b8ebdc34988282ad4b4f2016ed1990ce64aef759 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Tue, 22 May 2012 19:28:02 +0200 Subject: [PATCH] initial configmode --- luci/config-mode/Makefile | 39 ++++++++++++ luci/config-mode/files/etc/config/ffhl | 4 ++ .../lib/lua/luci/controller/ffhl/index.lua | 12 ++++ .../lib/lua/luci/controller/ffhl/wizard.lua | 29 +++++++++ .../luci/model/cbi/ffhl-wizard/hostname.lua | 38 ++++++++++++ .../luci/model/cbi/ffhl-wizard/meshvpn.lua | 39 ++++++++++++ .../luci/model/cbi/ffhl-wizard/password.lua | 36 +++++++++++ .../lib/lua/luci/tools/ffhl-wizard/nav.lua | 43 +++++++++++++ .../lua/luci/view/ffhl-wizard/completed.htm | 14 +++++ .../lib/lua/luci/view/ffhl-wizard/footer.htm | 19 ++++++ .../lib/lua/luci/view/ffhl-wizard/header.htm | 24 ++++++++ .../lua/luci/view/ffhl-wizard/meshvpn-key.htm | 15 +++++ .../usr/lib/lua/luci/view/ffhl-wizard/nav.htm | 16 +++++ .../lua/luci/view/ffhl-wizard/password.htm | 14 +++++ .../lib/lua/luci/view/ffhl-wizard/welcome.htm | 8 +++ .../lua/luci/view/ffhl-wizard/wizardform.htm | 61 +++++++++++++++++++ 16 files changed, 411 insertions(+) create mode 100644 luci/config-mode/Makefile create mode 100644 luci/config-mode/files/etc/config/ffhl create mode 100644 luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/index.lua create mode 100644 luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/wizard.lua create mode 100644 luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/hostname.lua create mode 100644 luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/meshvpn.lua create mode 100644 luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/password.lua create mode 100644 luci/config-mode/files/usr/lib/lua/luci/tools/ffhl-wizard/nav.lua create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/completed.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/footer.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/header.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/meshvpn-key.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/nav.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/password.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/welcome.htm create mode 100644 luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/wizardform.htm diff --git a/luci/config-mode/Makefile b/luci/config-mode/Makefile new file mode 100644 index 0000000..15281f9 --- /dev/null +++ b/luci/config-mode/Makefile @@ -0,0 +1,39 @@ +# Copyright (C) 2012 Nils Schneider +# This is free software, licensed under the Apache 2.0 license. + +include $(TOPDIR)/rules.mk + +PKG_NAME:=configmode +PKG_RELEASE:=0.0.1 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/configmode + SECTION:=luci + CATEGORY:=LuCI + SUBMENU:=9. Freifunk + TITLE:=Luci based configmode for userfriendly setup of new meshnodes + DEPENDS:=+firewall +endef + +define Package/configmode/description + Luci based configmode +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) +endef + +define Build/Configure +endef + +define Build/Compile +endef + +define Package/configmode/install + $(CP) ./files/* $(1)/ +endef + +$(eval $(call BuildPackage,configmode)) diff --git a/luci/config-mode/files/etc/config/ffhl b/luci/config-mode/files/etc/config/ffhl new file mode 100644 index 0000000..5993307 --- /dev/null +++ b/luci/config-mode/files/etc/config/ffhl @@ -0,0 +1,4 @@ + +config wizard + option enabled '1' + diff --git a/luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/index.lua b/luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/index.lua new file mode 100644 index 0000000..c042f9c --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/index.lua @@ -0,0 +1,12 @@ +module("luci.controller.ffhl.index", package.seeall) + +function index() + entry({}, call("maybe_wizard"), "luebeck.freifunk.net", 20).dependent=false +end + +function maybe_wizard() + -- This function should figure out whether the wizard already ran succesfully + -- and redirect to the wizard or the status page. + + luci.http.redirect(luci.dispatcher.build_url("wizard", "welcome")) +end diff --git a/luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/wizard.lua b/luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/wizard.lua new file mode 100644 index 0000000..2a55383 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/controller/ffhl/wizard.lua @@ -0,0 +1,29 @@ +module("luci.controller.ffhl.wizard", package.seeall) + +function index() + local uci = luci.model.uci.cursor() + if uci:get_first("ffhl", "wizard", "enabled") == "1" then + entry({"wizard", "welcome"}, template("ffhl-wizard/welcome"), "Willkommen", 10).dependent=false + entry({"wizard", "password"}, form("ffhl-wizard/password"), "Passwort", 20).dependent=false + entry({"wizard", "hostname"}, form("ffhl-wizard/hostname"), "Hostname", 30).dependent=false + entry({"wizard", "meshvpn"}, form("ffhl-wizard/meshvpn"), "Mesh-VPN", 40).dependent=false + entry({"wizard", "meshvpn", "pubkey"}, template("ffhl-wizard/meshvpn-key"), "Mesh-VPN Key", 1).dependent=false + entry({"wizard", "completed"}, template("ffhl-wizard/completed"), "Fertig", 50).dependent=false + entry({"wizard", "completed", "reboot"}, call("reboot"), "reboot", 1).dependent=false + end +end + +function reboot() + local uci = luci.model.uci.cursor() + + uci:foreach("ffhl", "wizard", function(s) + uci:set("ffhl", s[".name"], "enabled", "0") + end + ) + + uci:save("ffhl") + uci:commit("ffhl") + +-- luci.sys.reboot() +end + diff --git a/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/hostname.lua b/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/hostname.lua new file mode 100644 index 0000000..47e77b1 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/hostname.lua @@ -0,0 +1,38 @@ +local uci = luci.model.uci.cursor() + +local nav = require "luci.tools.ffhl-wizard.nav" + +local f = SimpleForm("hostname", "Name deines Freifunkknotens", "Als nächstes solltest du einem Freifunkknoten einen individuellen Namen geben. Dieser hilft dir und auch uns den Überblick zu behalten.") +f.template = "ffhl-wizard/wizardform" + +hostname = f:field(Value, "hostname", "Hostname") +hostname.value = uci:get_first("system", "system", "hostname") +hostname.rmempty = false + +function hostname.validate(self, value, section) + return value +end + +function f.handle(self, state, data) + if state == FORM_VALID then + local stat = true + uci:foreach("system", "system", function(s) + stat = stat and uci:set("system", s[".name"], "hostname", data.hostname) + end + ) + + stat = stat and uci:save("system") + stat = stat and uci:commit("system") + + if stat then + nav.maybe_redirect_to_successor() + f.message = "Hostname geändert!" + else + f.errmessage = "Fehler!" + end + end + + return true +end + +return f diff --git a/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/meshvpn.lua b/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/meshvpn.lua new file mode 100644 index 0000000..73c4fa9 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/meshvpn.lua @@ -0,0 +1,39 @@ +local uci = luci.model.uci.cursor() + +local nav = require "luci.tools.ffhl-wizard.nav" + +local f = SimpleForm("meshvpn", "Mesh-VPN", "Um deinen Freifunkknoten auch über das Internet mit anderen Knoten zu verbinden, muss das Mesh-VPN aktiviert werden.

Dadurch ist es möglich das Freifunknetz auch ohne WLAN Verbindung zu anderen Knoten zu nutzen.") +f.template = "ffhl-wizard/wizardform" + +meshvpn = f:field(Flag, "meshvpn", "Mesh-VPN aktivieren?") +meshvpn.default = string.format("%d", uci:get("fastd", "ffhl_mesh_vpn", "enabled")) +meshvpn.rmempty = false + +function f.handle(self, state, data) + if state == FORM_VALID then + local stat = false + uci:set("fastd", "ffhl_mesh_vpn", "enabled", data.meshvpn) + uci:save("fastd") + uci:commit("fastd") + + if data.meshvpn == "1" then + luci.sys.call("/etc/init.d/haveged start") + local f = io.popen("fastd --generate-key --machine-readable", "r") + local secret = f:read("*a") + f:close() + luci.sys.call("/etc/init.d/haveged stop") + + uci:set("fastd", "ffhl_mesh_vpn", "secret", secret) + uci:save("fastd") + uci:commit("fastd") + + luci.http.redirect(luci.dispatcher.build_url("wizard", "meshvpn", "pubkey")) + else + nav.maybe_redirect_to_successor() + end + end + + return true +end + +return f diff --git a/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/password.lua b/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/password.lua new file mode 100644 index 0000000..5f5e15b --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/model/cbi/ffhl-wizard/password.lua @@ -0,0 +1,36 @@ +local nav = require "luci.tools.ffhl-wizard.nav" + +f = SimpleForm("password", "Administrator Passwort setzen", "Damit nur du Zugriff auf deinen Freifunkknoten hast, solltest du jetzt ein Passwort vergeben.

Bitte wähle ein sicheres Passwort. Sonst könnten Leute wilden Scheiß machen, den du nicht willst!") +f.template = "ffhl-wizard/wizardform" + +pw1 = f:field(Value, "pw1", "Passwort") +pw1.password = true +pw1.rmempty = false + +pw2 = f:field(Value, "pw2", "Wiederholung") +pw2.password = true +pw2.rmempty = false + +function pw2.validate(self, value, section) + return pw1:formvalue(section) == value and value +end + +function f.handle(self, state, data) + if state == FORM_VALID then + local stat = luci.sys.user.setpasswd("root", data.pw1) == 0 + + if stat then + nav.maybe_redirect_to_successor() + f.message = "Passwort geändert!" + else + f.errmessage = "Fehler!" + end + + data.pw1 = nil + data.pw2 = nil + end + + return true +end + +return f diff --git a/luci/config-mode/files/usr/lib/lua/luci/tools/ffhl-wizard/nav.lua b/luci/config-mode/files/usr/lib/lua/luci/tools/ffhl-wizard/nav.lua new file mode 100644 index 0000000..8ef509a --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/tools/ffhl-wizard/nav.lua @@ -0,0 +1,43 @@ +module("luci.tools.ffhl-wizard.nav", package.seeall) + +function maybe_redirect_to_successor() + local pre, suc = get() + + if suc then + luci.http.redirect(luci.dispatcher.build_url("wizard", suc.href)) + end +end + +function get() + + local disp = require "luci.dispatcher" + + local request = disp.context.path + local category = request[1] + local cattree = category and disp.node(category) + + local childs = disp.node_childs(cattree) + + local predecessor = nil + local successor = nil + + if #childs > 0 then + local found_pre = false + for i, r in ipairs(childs) do + local nnode = cattree.nodes[r] + nnode.href = r + + if r == request[2] then + found_pre = true + elseif found_pre and successor == nil then + successor = nnode + end + + if not found_pre then + predecessor = nnode + end + end + end + + return predecessor, successor +end diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/completed.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/completed.htm new file mode 100644 index 0000000..cbec028 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/completed.htm @@ -0,0 +1,14 @@ +<% + local disp = require "luci.dispatcher" +%> +<%+header%> +

Konfiguration abgeschlossen

+ +

Die Konfiguration deines Freifunkknotens ist nun abgeschlossen.

+ +

Damit sie aktiv wird, muss der Knoten neugestartet werden.

+ +

+ ">Jetzt neustarten! +

+<%+footer%> diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/footer.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/footer.htm new file mode 100644 index 0000000..888ee5f --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/footer.htm @@ -0,0 +1,19 @@ +<% local xtime +if exectime then + xtime = (string.format("%.2fs", os.clock() - exectime)) +end %> + + + + + + + + diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/header.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/header.htm new file mode 100644 index 0000000..a6fd011 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/header.htm @@ -0,0 +1,24 @@ +<% + local fs = require "luci.fs" + local sys = require "luci.sys" + local http = require "luci.http" + local disp = require "luci.dispatcher" + + local hostname = sys.hostname() +%> + + + + + + +<%=striptags( hostname )%> - Wizard + + + + +
+ + diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/meshvpn-key.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/meshvpn-key.htm new file mode 100644 index 0000000..59c37e9 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/meshvpn-key.htm @@ -0,0 +1,15 @@ +<% + local disp = require "luci.dispatcher" + local f = io.popen("/etc/init.d/fastd show-key ffhl_mesh_vpn", "r") + local key = f:read("*a") + f:close() + +%> +<%+header%> +

Schlüsselaustausch

+

Dies ist der öffentliche Schlüssel deines Freifunkknotens. Bitte schicke ihn an freifunk.luebeck@asta.uni-luebeck.de um ihn auf den Freifunkservern eintragen zu lassen. Sobald der Schlüssel eingetragen wurde, kann dein Knoten das Mesh-VPN nutzen.

+
+		<%=key%>
+	
+<%+ffhl-wizard/nav%> +<%+footer%> diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/nav.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/nav.htm new file mode 100644 index 0000000..905df98 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/nav.htm @@ -0,0 +1,16 @@ +<% + local nav = require "luci.tools.ffhl-wizard.nav" + local predecessor, successor = nav.get() +%> + +<% if predecessor then %> + "> + Zurück + +<% end %> + +<% if not have_form and successor then %> + "> + Weiter + +<% end %> diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/password.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/password.htm new file mode 100644 index 0000000..10be1a0 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/password.htm @@ -0,0 +1,14 @@ +<% + local disp = require "luci.dispatcher" +%> +<%+ffhl-wizard/header%> +
+

Passwort setzen

+

Hier steht noch kein Text, der den Benutzer auffordert ein Passwort zu setzen.

+ +

+
+<%+ffhl-wizard/footer%> diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/welcome.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/welcome.htm new file mode 100644 index 0000000..ab9a7e3 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/welcome.htm @@ -0,0 +1,8 @@ +<% + local disp = require "luci.dispatcher" +%> +<%+header%> +

Willkommen auf deinem Freifunkknoten!

+

Dieser Wizard hilft dir deinen Freifunkknoten einzurichten.

+<%+ffhl-wizard/nav%> +<%+footer%> diff --git a/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/wizardform.htm b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/wizardform.htm new file mode 100644 index 0000000..b3f01c4 --- /dev/null +++ b/luci/config-mode/files/usr/lib/lua/luci/view/ffhl-wizard/wizardform.htm @@ -0,0 +1,61 @@ +<% if not self.embedded then %> +
+
+ + +
+<% end %> +
+ <% if self.title and #self.title > 0 then %>

<%=self.title%>

<% end %> + <% if self.description and #self.description > 0 then %>
<%=self.description%>
<% end %> + <% self:render_children() %> +
+
+<%- if self.message then %> +
<%=self.message%>
+<%- end %> +<%- if self.errmessage then %> +
<%=self.errmessage%>
+<%- end %> +<% if not self.embedded then %> +
+<%- + if type(self.hidden) == "table" then + for k, v in pairs(self.hidden) do +-%> + +<%- + end + end +%> +<% if redirect then %> +
+ +
+<% end %> +<%- if self.flow and self.flow.skip then %> + +<% end %> +<%- if self.submit ~= false then %> + +<% end %> +<%- if self.reset ~= false then %> + +<% end %> +<%- if self.cancel ~= false and self.on_cancel then %> + +<% end %> + +
+
+<% end %> + +<% have_form = true %> + +<%+ffhl-wizard/nav%>