From 05f350547e275e23062892227e20d536709ef99a Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Fri, 5 Sep 2008 01:01:53 +0000 Subject: FFWizard rewrite --- .../luasrc/controller/ffwizard.lua | 21 ++ .../controller/luci_ffwizard_leipzig/wizard.lua | 229 ------------------ .../luasrc/i18n/ffwizard-leipzig.de.lua | 9 - .../luasrc/i18n/ffwizard-leipzig.de.xml | 14 -- .../luasrc/model/cbi/ffwizard.lua | 269 +++++++++++++++++++++ .../luasrc/tools/ffwizard.lua | 144 +++++++++++ .../luasrc/view/freifunk/wizard.htm | 72 ------ 7 files changed, 434 insertions(+), 324 deletions(-) create mode 100644 applications/luci-ffwizard-leipzig/luasrc/controller/ffwizard.lua delete mode 100644 applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua delete mode 100644 applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.lua delete mode 100644 applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.xml create mode 100644 applications/luci-ffwizard-leipzig/luasrc/model/cbi/ffwizard.lua create mode 100644 applications/luci-ffwizard-leipzig/luasrc/tools/ffwizard.lua delete mode 100644 applications/luci-ffwizard-leipzig/luasrc/view/freifunk/wizard.htm diff --git a/applications/luci-ffwizard-leipzig/luasrc/controller/ffwizard.lua b/applications/luci-ffwizard-leipzig/luasrc/controller/ffwizard.lua new file mode 100644 index 000000000..9e7c0deca --- /dev/null +++ b/applications/luci-ffwizard-leipzig/luasrc/controller/ffwizard.lua @@ -0,0 +1,21 @@ +--[[ +LuCI - Lua Configuration Interface + +Copyright 2008 Steven Barth +Copyright 2008 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ + +]]-- + +module "luci.controller.ffwizard" + +function index() + entry({"admin", "index", "ffwizard"}, form("ffwizard"), "Freifunkassistent", 50) +end \ No newline at end of file diff --git a/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua b/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua deleted file mode 100644 index bd9503ba4..000000000 --- a/applications/luci-ffwizard-leipzig/luasrc/controller/luci_ffwizard_leipzig/wizard.lua +++ /dev/null @@ -1,229 +0,0 @@ -module("luci.controller.luci_ffwizard_leipzig.wizard", package.seeall) - -function index() - entry({"admin", "index", "wizard"}, call("action_wizard"), "Freifunkassistent", 20).i18n="ffwizard-leipzig" -end - - -function action_wizard() - if luci.http.formvalue("ip") then - return configure_freifunk() - end - - local ifaces = {} - luci.model.uci.foreach("wireless", "wifi-device", - function(section) - table.insert(ifaces, section[".name"]) - end) - - luci.template.render("freifunk/wizard", {ifaces=ifaces}) -end - -function configure_freifunk() - local ip = luci.http.formvalue("ip") - local uci = luci.model.uci - local cfg = { - wireless = uci.load("wireless"), - luci_fw = uci.load("luci_fw"), - luci_splash = uci.load("luci_splash"), - olsr = uci.load("olsr") - } - - -- Configure FF-Interface - uci.delete("network", "ff") - uci.delete("network", "ffdhcp") - - uci.section("network", "interface", "ff", { - type = "bridge", - proto = "static", - ipaddr = ip, - netmask = uci.get("freifunk", "community", "mask"), - dns = uci.get("freifunk", "community", "dns") - }) - - -- Reset Routing - uci.delete_all("luci_fw", "routing", - function (section) - return (section.iface == "ff" or section.oface == "ff") - end) - - if cfg.luci_fw then - uci.section("luci_fw", "routing", nil, { - iface = "ff", - oface = "ff", - fwd = "1" - }) - end - - -- Routing from Internal - local iface = luci.http.formvalue("frominternal") - if iface and iface ~= "" then - uci.delete_all("luci_fw", "routing", - function (section) - return (section.iface == iface and section.oface == "ff") - end) - - if cfg.luci_fw then - uci.section("luci_fw", "routing", nil, { - iface = iface, - oface = "ff", - fwd = "1", - nat = "1" - }) - end - end - - -- Routing to External - local iface = luci.http.formvalue("toexternal") - if iface and iface ~= "" then - uci.delete_all("luci_fw", "routing", - function (section) - return (section.oface == iface and section.iface == "ff") - end) - - if cfg.luci_fw then - uci.section("luci_fw", "routing", nil, { - oface = iface, - iface = "ff", - fwd = "1", - nat = "1" - }) - end - end - - -- Configure DHCP - if luci.http.formvalue("dhcp") then - local dhcpnet = uci.get("freifunk", "community", "dhcp"):match("^([0-9]+)") - local dhcpip = ip:gsub("^[0-9]+", dhcpnet) - - uci.section("network", "interface", "ffdhcp", { - proto = "static", - ifname = "br-ff:dhcp", - ipaddr = dhcpip, - netmask = uci.get("freifunk", "community", "dhcpmask") - }) - - uci.delete_all("dhcp", "dhcp", - function (section) - return (section.interface == "ffdhcp") - end) - - local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4 - uci.section("dhcp", "dhcp", nil, { - interface = "ffdhcp", - start = dhcpbeg, - limit = ((dhcpbeg < 252) and 3 or 2), - leasetime = "30m" - }) - - - uci.delete_all("luci_splash", "iface", - function (section) - return (section.network == "ffdhcp") - end) - - if cfg.luci_splash then - uci.section("luci_splash", "iface", nil, { - network = "ffdhcp" - }) - end - - - uci.delete_all("luci_fw", "routing", - function (section) - return (section.iface == "ffdhcp" or section.oface == "ffdhcp") - end) - - if cfg.luci_fw then - uci.section("luci_fw", "routing", nil, { - iface = "ffdhcp", - oface = "ff", - nat = "1" - }) - - local iface = luci.http.formvalue("toexternal") - if iface and iface ~= "" then - uci.section("luci_fw", "routing", nil, { - iface = "ffdhcp", - oface = iface, - nat = "1" - }) - end - end - end - - -- Configure OLSR - if luci.http.formvalue("olsr") and cfg.olsr then - uci.delete_all("olsr", "Interface") - uci.delete_all("olsr", "LoadPlugin") - - if luci.http.formvalue("shareinet") then - uci.section("olsr", "LoadPlugin", "dyn_gw", { - Library = "olsrd_dyn_gw.so.0.4" - }) - end - - uci.section("olsr", "LoadPlugin", "nameservice", { - Library = "olsrd_nameservice.so.0.3", - name = ip:gsub("%.", "-"), - hosts_file = "/var/etc/hosts", - suffix = ".olsr", - latlon_infile = "/tmp/latlon.txt" - }) - - uci.section("olsr", "LoadPlugin", "txtinfo", { - Library = "olsrd_txtinfo.so.0.1", - Accept = "127.0.0.1" - }) - - uci.section("olsr", "Interface", nil, { - Interface = "ff", - HelloInterval = "6.0", - HelloValidityTime = "108.0", - TcInterval = "4.0", - TcValidityTime = "324.0", - MidInterval = "18.0", - MidValidityTime = "324.0", - HnaInterval = "18.0", - HnaValidityTime = "108.0" - }) - end - - -- Configure Wifi - if cfg.wireless then - uci.foreach("wireless", "wifi-device", - function (section) - local device = section[".name"] - - if luci.http.formvalue("wifi."..device) then - uci.delete_all("wireless", "wifi-iface", - function (section) - return (section.device == device) - end) - - uci.tset("wireless", device, { - disabled = "0", - mode = "11g", - txantenna = "1", - rxantenna = "1", - channel = uci.get("freifunk", "community", "channel") - }) - - uci.section("wireless", "wifi-iface", nil, { - device = device, - network = "ff", - mode = "adhoc", - ssid = uci.get("freifunk", "community", "essid"), - bssid = uci.get("freifunk", "community", "bssid"), - txpower = 13 - }) - end - end) - end - - -- Save UCI - uci.save() - - - luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes")) -end \ No newline at end of file diff --git a/applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.lua b/applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.lua deleted file mode 100644 index 3ca7b75ce..000000000 --- a/applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.lua +++ /dev/null @@ -1,9 +0,0 @@ -ffwizard = 'Freifunkassistent' -ffwizard1 = 'Dieser Assistent konfiguriert den Router für die Benutzung im Freifunknetz' -ip = 'IP-Adresse' -cfgdhcp = 'Drahtlos DHCP konfigurieren' -cfgexternal = 'Erlaube Zugriff auf externes Netzwerk' -cfginternal = 'Erlaube Zugriff von internem Netzwerk' -cfgolsr = 'OLSR konfigurieren' -wificfg = 'Drahtlosgerät einrichten' -shareinet = 'Internetzugang ankündigen' diff --git a/applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.xml b/applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.xml deleted file mode 100644 index b69dec3f4..000000000 --- a/applications/luci-ffwizard-leipzig/luasrc/i18n/ffwizard-leipzig.de.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - -Freifunkassistent -Dieser Assistent konfiguriert den Router für die Benutzung im Freifunknetz -IP-Adresse -Drahtlos DHCP konfigurieren -Erlaube Zugriff auf externes Netzwerk -Erlaube Zugriff von internem Netzwerk -OLSR konfigurieren -Drahtlosgerät einrichten -Internetzugang ankündigen - diff --git a/applications/luci-ffwizard-leipzig/luasrc/model/cbi/ffwizard.lua b/applications/luci-ffwizard-leipzig/luasrc/model/cbi/ffwizard.lua new file mode 100644 index 000000000..cd737c985 --- /dev/null +++ b/applications/luci-ffwizard-leipzig/luasrc/model/cbi/ffwizard.lua @@ -0,0 +1,269 @@ +--[[ +LuCI - Lua Configuration Interface + +Copyright 2008 Steven Barth +Copyright 2008 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ + +]]-- + + +local uci = require "luci.model.uci".cursor() +local tools = require "luci.tools.ffwizard" + + +-------------------- View -------------------- +f = SimpleForm("ffwizward", "Freifunkassistent", + "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.") + + +dev = f:field(ListValue, "device", "WLAN-Gerät") +uci:foreach("wireless", "wifi-device", + function(section) + dev:value(section[".name"]) + end) + + +main = f:field(Flag, "wifi", "Freifunkzugang einrichten") + +net = f:field(Value, "net", "Freifunknetz") +net.rmempty = true +net:depends("wifi", "1") +net:value("104.61", "Leipzig (104.61)") +net:value("104.62", "Halle (104.62)") +function net.cfgvalue(self, section) + return uci:get("freifunk", "wizard", "net") +end +function net.write(self, section, value) + uci:set("freifunk", "wizard", "net", value) + uci:save("freifunk") +end + + +subnet = f:field(ListValue, "subnet", "Subnetz (Projekt)") +subnet.rmempty = true +subnet:depends("wifi", "1") +for i=0, 255 do + subnet:value(i) +end +function subnet.cfgvalue(self, section) + return uci:get("freifunk", "wizard", "subnet") +end +function subnet.write(self, section, value) + uci:set("freifunk", "wizard", "subnet", value) + uci:save("freifunk") +end + +node = f:field(Value, "node", "Knoten") +node.rmempty = true +node:depends("wifi", "1") +for i=1, 51 do + node:value(i) +end +function node.cfgvalue(self, section) + return uci:get("freifunk", "wizard", "node") +end +function node.write(self, section, value) + uci:set("freifunk", "wizard", "node", value) + uci:save("freifunk") +end + +client = f:field(Flag, "client", "WLAN-DHCP anbieten") +client:depends("wifi", "1") + + +olsr = f:field(Flag, "olsr", "OLSR einrichten") + +share = f:field(ListValue, "sharenet", "Eigenen Internetzugang freigeben") +share:value("maybe", "-- keine Aktion --") +share:value("yes", "einschalten") +share:value("no", "ausschalten") + + + +-------------------- Control -------------------- +function f.handle(self, state, data) + if state == FORM_VALID then + luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes")) + return false + elseif state == FORM_INVALID then + self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen." + end + return true +end + +local function _strip_internals(tbl) + tbl = tbl or {} + for k, v in pairs(tbl) do + if k:sub(1, 1) == "." then + tbl[k] = nil + end + end + return tbl +end + +-- Configure Freifunk checked +function main.write(self, section, value) + if value == "0" then + return + end + + local device = dev:formvalue(section) + + -- Collect IP-Address + local inet = net:formvalue(section) + local isubnet = subnet:formvalue(section) + local inode = node:formvalue(section) + + -- Invalidate fields + if not inet then + net.tag_missing[section] = true + end + if not isubnet then + subnet.tag_missing[section] = true + end + if not inode then + node.tag_missing[section] = true + end + + if not inet or not isubnet or not inode then + return + end + + local ip = "%s.%s.%s" % {inet, isubnet, inode} + + + -- Cleanup + tools.wifi_delete_ifaces(device) + tools.network_remove_interface(device) + tools.firewall_zone_remove_interface("freifunk", device) + + + -- Tune wifi device + local devconfig = _strip_internals(uci:get_all("freifunk", "wifi_device")) + uci:tset("wireless", device, devconfig) + + -- Create wifi iface + local ifconfig = _strip_internals(uci:get_all("freifunk", "wifi_iface")) + ifconfig.device = device + uci:section("wireless", "wifi-iface", nil, ifconfig) + + -- Save wifi + uci:save("wireless") + + -- Create firewall zone and add default rules (first time) + local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true) + if newzone then + uci:foreach("freifunk", "fw_forwarding", function(section) + uci:section("firewall", "forwarding", nil, _strip_internals(section)) + end) + + uci:foreach("freifunk", "fw_rule", function(section) + uci:section("firewall", "rule", nil, _strip_internals(section)) + end) + + uci:save("firewall") + end + + + -- Crate network interface + local netconfig = _strip_internals(uci:get_all("freifunk", "interface")) + netconfig.ipaddr = ip + uci:section("network", "interface", device, netconfig) + + uci:save("network") + + tools.firewall_zone_add_interface("freifunk", device) +end + + +function olsr.write(self, section, value) + if value == "0" then + return + end + + local device = dev:formvalue(section) + + -- Delete old interface + uci:delete_all("freifunk", "Interface", {Interface=device}) + + -- Write new interface + local olsrbase = _strip_internals(uci:get_all("freifunk", "olsr_interface")) + olsrbase.Interface = device + uci:section("olsr", "Interface", nil, olsrbase) + uci:save("olsr") +end + + +function share.write(self, section, value) + if value == "maybe" then + return + end + + uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"}) + + if value == "yes" then + uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"}) + end + uci:save("firewall") +end + + +function client.write(self, section, value) + if value == "0" then + return + end + + local device = dev:formvalue(section) + + -- Collect IP-Address + local inet = net:formvalue(section) + local isubnet = subnet:formvalue(section) + local inode = node:formvalue(section) + + if not inet or not isubnet or not inode then + return + end + + local dhcpbeg = 48 + tonumber(inode) * 4 + local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg} + local limit = dhcpbeg < 252 and 3 or 2 + + -- Delete old alias + uci:delete("network", device .. "dhcp") + + -- Create alias + local aliasbase = _strip_internals(uci:get_all("freifunk", "alias")) + aliasbase.interface = device + aliasbase.ipaddr = dclient + aliasbase.proto = "static" + uci:section("network", "alias", device .. "dhcp", aliasbase) + uci:save("network") + + + -- Create dhcp + local dhcpbase = _strip_internals(uci:get_all("freifunk", "dhcp")) + dhcpbase.interface = device .. "dhcp" + dhcpbase.start = dhcpbeg + dhcpbase.limit = limit + + uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase) + uci:save("dhcp") + + + -- Delete old splash + uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"}) + + -- Register splash + uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"}) + uci:save("luci_splash") +end + +return f \ No newline at end of file diff --git a/applications/luci-ffwizard-leipzig/luasrc/tools/ffwizard.lua b/applications/luci-ffwizard-leipzig/luasrc/tools/ffwizard.lua new file mode 100644 index 000000000..d679cc93e --- /dev/null +++ b/applications/luci-ffwizard-leipzig/luasrc/tools/ffwizard.lua @@ -0,0 +1,144 @@ +--[[ +LuCI - Lua Configuration Interface + +Copyright 2008 Steven Barth +Copyright 2008 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ + +]]-- + +local uci = require "luci.model.uci" +local util = require "luci.util" +local table = require "table" + + +module "luci.tools.ffwizard" + +-- Deletes all references of a wifi device +function wifi_delete_ifaces(device) + local cursor = uci.cursor() + cursor:delete_all("wireless", "wifi-iface", {device=device}) + cursor:save("wireless") +end + +-- Deletes a network interface and all occurences of it in firewall zones and dhcp +function network_remove_interface(iface) + local cursor = uci.cursor() + + if not cursor:delete("network", iface) then + return false + end + + local aliases = {iface} + cursor:foreach("network", "alias", + function(section) + table.insert(aliases, section[".name"]) + end) + + -- Delete Aliases and Routes + cursor:delete_all("network", nil, {interface=iface}) + + -- Delete DHCP sections + cursor:delete_all("dhcp", "dhcp", + function(section) + return util.contains(aliases, section.interface) + end) + + -- Remove OLSR sections + cursor:delete_all("olsr", "Interface", {Interface=iface}) + + -- Remove Splash sections + cursor:delete_all("luci-splash", "iface", {network=iface}) + + cursor:save("network") + cursor:save("olsr") + cursor:save("dhcp") + cursor:save("luci-splash") +end + +-- Creates a firewall zone +function firewall_create_zone(zone, input, output, forward, masq) + local cursor = uci.cursor() + if not firewall_find_zone(zone) then + local stat = cursor:section("firewall", "zone", nil, { + input = input, + output = output, + forward = forward, + masq = masq and "1", + name = zone + }) + cursor:save("firewall") + return stat + end +end + +-- Adds interface to zone, creates zone on-demand +function firewall_zone_add_interface(name, interface) + local cursor = uci.cursor() + local zone = firewall_find_zone(name) + local net = cursor:get("firewall", zone, "network") + cursor:set("firewall", zone, "network", (net or name .. " ") .. interface) + cursor:save("firewall") +end + +-- Removes interface from zone +function firewall_zone_remove_interface(name, interface) + local cursor = uci.cursor() + local zone = firewall_find_zone(name) + if zone then + local net = cursor:get("firewall", zone, "network") + local new = remove_list_entry(net, interface) + if new then + if #new > 0 then + cursor:set("firewall", zone, "network", new) + else + cursor:delete("firewall", zone, "network") + end + cursor:save("firewall") + end + end +end + + +-- Finds the firewall zone with given name +function firewall_find_zone(name) + local find + + uci.cursor():foreach("firewall", "zone", + function (section) + if section.name == name then + find = section[".name"] + end + end) + + return find +end + + + +-- Helpers -- + +-- Removes a listentry, handles real and pseduo lists transparently +function remove_list_entry(value, entry) + if type(value) == "nil" then + return nil + end + + local result = type(value) == "table" and value or util.split(value, " ") + local key = util.contains(result, entry) + + while key do + table.remove(result, key) + key = util.contains(result, entry) + end + + result = type(value) == "table" and result or table.concat(result, " ") + return result ~= value and result +end \ No newline at end of file diff --git a/applications/luci-ffwizard-leipzig/luasrc/view/freifunk/wizard.htm b/applications/luci-ffwizard-leipzig/luasrc/view/freifunk/wizard.htm deleted file mode 100644 index 26a575e9b..000000000 --- a/applications/luci-ffwizard-leipzig/luasrc/view/freifunk/wizard.htm +++ /dev/null @@ -1,72 +0,0 @@ -<%# -LuCI - Lua Configuration Interface -Copyright 2008 Steven Barth -Copyright 2008 Jo-Philipp Wich - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - --%> -<%+header%> -

<%:ffwizard%>

-

<%:ffwizard1%>

-
-
-
-
-
<%:ip%>
-
-
- <% for i, k in ipairs(ifaces) do %> -
-
<%:wificfg%>: <%=k%>
-
-
- <% end %> -
-
<%:cfgolsr%>
-
-
-
-
<%:cfgdhcp%>
-
-
-
-
<%:cfginternal%>:
-
-
-
-
<%:cfgexternal%>:
-
-
-
-
<%:shareinet%>
-
-
-
-
-
- - -
-
-<%+footer%> -- cgit v1.2.3