0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 19:13:49 +01:00

Improve logger; set encryption config when creating network from scan info; fix reconfiguration marking; add logging to init script; show encryption in web interface; fixes toward client mode setup; other minor fixes/improvements.

This commit is contained in:
Wouter R 2013-04-09 14:37:31 +02:00
parent 1f39bf6434
commit 8b4d81a7e7
6 changed files with 105 additions and 82 deletions

View File

@ -46,6 +46,7 @@ function parseNetLine(line) {
r.bssid = line[1]; r.bssid = line[1];
r.channel = line[2]; r.channel = line[2];
r.mode = line[3]; r.mode = line[3];
r.encryption = line[4];
return r; return r;
} }
@ -76,7 +77,7 @@ function fetchAvailableNetworks() {
if (value != "") { if (value != "") {
var netinf = parseNetLine(value); var netinf = parseNetLine(value);
var ssid = netinf.ssid; var ssid = netinf.ssid;
options.append($("<option />").val(ssid).text(ssid + " - " + netinf.mode + " mode")); options.append($("<option />").val(ssid).text(ssid + " - " + netinf.mode + " mode" + "(encryption: " + netinf.encryption + ")"));
} }
}); });
$("#wlan_btn_connect").prop('disabled', false); $("#wlan_btn_connect").prop('disabled', false);
@ -113,7 +114,7 @@ function connectBtnHandler() {
return; return;
} }
$.get(cgiBase + "?op=assoc&ssid=" + ssid + "&passphrase=" + phrase, function(data) { $.get(cgiBase + "?op=assoc&ssid=" + ssid + "&phrase=" + phrase, function(data) {
data = parseResponse(data); data = parseResponse(data);
if (data.status == "ERR") { if (data.status == "ERR") {
setResult(data.msg, true); setResult(data.msg, true);

View File

@ -66,13 +66,15 @@ function main()
--TODO: --TODO:
-- - extend reconf interface to support function arguments (as tables) so wifihelper functionality can be integrated -- - extend reconf interface to support function arguments (as tables) so wifihelper functionality can be integrated
-- but how? idea: pass x_args={arg1="a",arg2="2342"} with component 'x' -- but how? idea: pass x_args={arg1="a",arg2="2342"} for component 'x'
-- or: allow alternative for x="y" --> x={action="y", arg1="a", arg2="2342"}
-- in any case, arguments should be put in a new table to pass to the function (since order is undefined it must be an assoc array)
if sr and #sr > 0 then if sr and #sr > 0 then
u.printWithSuccess(#sr .. " network(s) found"); u.printWithSuccess(#sr .. " network(s) found");
for _, se in ipairs(sr) do for _, se in ipairs(sr) do
print("[[ " .. u.dump(se) .. " ]]") --TEMP --print("[[ " .. u.dump(se) .. " ]]") --TEMP
if se.mode ~= "ap" then if se.mode ~= "ap" and se.ssid ~= wifi.AP_SSID then
print(se.ssid .. "," .. se.bssid .. "," .. se.channel .. "," .. wifi.mapDeviceMode(se.mode)) print(se.ssid .. "," .. se.bssid .. "," .. se.channel .. "," .. wifi.mapDeviceMode(se.mode) .. "," .. wifi.mapEncryptionType(se.encryption))
end end
end end
else else
@ -110,15 +112,15 @@ function main()
if cfg == nil or argRecreate ~= nil then if cfg == nil or argRecreate ~= nil then
local scanResult = wifi.getScanInfo(argSsid) local scanResult = wifi.getScanInfo(argSsid)
if scanResult ~= nil then if scanResult ~= nil then
wifi.createConfigFromScanInfo(scanResult) wifi.createConfigFromScanInfo(scanResult, argPhrase)
else else
--check for error --check for error
u.exitWithError("No wireless network with SSID '" .. argSsid .. "' is available") u.exitWithError("No wireless network with SSID '" .. argSsid .. "' is available")
end end
end end
wifi.activateConfig(argSsid) wifi.activateConfig(argSsid)
local rv = wifi.restart() reconf.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", wireless="reload" }
u.exitWithSuccess("Wlan associated with network "..argSsid.."! [$?=" .. rv .. "]") u.exitWithSuccess("Wlan associated with network " .. argSsid .. "!")
elseif argOperation == "disassoc" then elseif argOperation == "disassoc" then
wifi.activateConfig() wifi.activateConfig()
@ -129,7 +131,7 @@ function main()
--add AP net, activate it, deactivate all others, reload network/wireless config, add all dhcp and captive settings and reload as needed --add AP net, activate it, deactivate all others, reload network/wireless config, add all dhcp and captive settings and reload as needed
reconf.switchConfiguration{apnet="add_noreload"} reconf.switchConfiguration{apnet="add_noreload"}
wifi.activateConfig(wifi.AP_SSID) wifi.activateConfig(wifi.AP_SSID)
reconf.switchConfiguration{ network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add", wwwcaptive="add", natreflect="add" } reconf.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add", wwwcaptive="add" }
u.exitWithSuccess("Switched to AP mode (SSID: '" .. wifi.AP_SSID .. "')") u.exitWithSuccess("Switched to AP mode (SSID: '" .. wifi.AP_SSID .. "')")
elseif argOperation == "rm" then elseif argOperation == "rm" then
@ -141,7 +143,8 @@ function main()
end end
elseif argOperation == "test" then elseif argOperation == "test" then
reconf.switchConfiguration{ apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", natreflect="rm" } --invert actions performed by openap operation
reconf.switchConfiguration{ apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm" }
-- reconf.switchConfiguration{dnsredir="add"} -- reconf.switchConfiguration{dnsredir="add"}
u.exitWithSuccess("nop") u.exitWithSuccess("nop")

View File

@ -1,12 +1,17 @@
#!/bin/sh /etc/rc.common #!/bin/sh /etc/rc.common
START=19 START=19
LOGGER="logger -s -t autowifi -p 6"
start() { boot() {
echo "dummy auto wifi init" $LOGGER "dummy boot"
#/www/cgi-bin/wfcf op=auto #/www/cgi-bin/wfcf op=auto
} }
stop() { start() {
echo "dummy auto wifi deinit" $LOGGER "dummy start"
}
stop() {
$LOGGER "dummy stop"
} }

View File

@ -10,6 +10,20 @@ M.WWW_CAPTIVE_PATH = "/usr/share/lua/autowifi/ext/www"
M.WWW_CAPTIVE_INDICATOR = "/www/.autowifi-inplace" M.WWW_CAPTIVE_INDICATOR = "/www/.autowifi-inplace"
M.WWW_RENAME_NAME = "/www-regular" M.WWW_RENAME_NAME = "/www-regular"
local function reloadBit(dlist, itemname)
if dlist[itemname] == nil then dlist[itemname] = "" end
if dlist[itemname] == "" then dlist[itemname] = "r"
elseif dlist[itemname] == "c" then dlist[itemname] = "b"
end
end
local function commitBit(dlist, itemname)
if dlist[itemname] == nil then dlist[itemname] = "" end
if dlist[itemname] == "" then dlist[itemname] = "c"
elseif dlist[itemname] == "r" then dlist[itemname] = "b"
end
end
local function bothBits(dlist, itemname) dlist[itemname] = "b" end
function M.init(wifiInstance, reloadSilent) function M.init(wifiInstance, reloadSilent)
wifi = wifiInstance wifi = wifiInstance
silent = reloadSilent or false silent = reloadSilent or false
@ -60,12 +74,16 @@ end
--[[ Issue '/etc/init.d/network reload' command ]] --[[ Issue '/etc/init.d/network reload' command ]]
function reconf.network_reload(dirtyList) dirtyList.network = "r" end function reconf.network_reload(dirtyList) reloadBit(dirtyList, "network") end
--[[ Issue '/etc/init.d/wireless reload' command ]]
function reconf.wireless_reload(dirtyList) reloadBit(dirtyList, "wireless") end
--[[ Add wlan interface declaration to /etc/config/network ]] --[[ Add wlan interface declaration to /etc/config/network ]]
function reconf.wifiiface_add(dirtyList) function reconf.wifiiface_add(dirtyList)
uci:set("network", wifi.NET, "interface") uci:set("network", wifi.NET, "interface")
dirtyList.network = "c"; -- uci:set("network", wifi.NET, "proto", "dhcp")
commitBit(dirtyList, "network")
end end
--[[ Add/remove access point network ]] --[[ Add/remove access point network ]]
@ -85,118 +103,97 @@ function reconf.apnet_add(dirtyList, noReload)
mode = "ap", mode = "ap",
}) })
dirtyList.wireless = "c" commitBit(dirtyList, "wireless")
if noReload == nil or noReload == false then dirtyList.network = "r" end if noReload == nil or noReload == false then reloadBit(dirtyList, "network") end
end end
function reconf.apnet_rm(dirtyList) function reconf.apnet_rm(dirtyList)
local sname = nil local sname = nil
uci:foreach("wireless", "wifi-iface", function(s) uci:foreach("wireless", "wifi-iface", function(s)
if s.ssid == wifi.AP_SSID then sname = s[".name"]; return false end if s.ssid == wifi.AP_SSID then sname = s[".name"]; return false end
end) end)
if sname == nil then if sname == nil then return u:loginfo("AP network configuration does not exist, nothing to remove") end
u:loginfo("AP network configuration does not exist, nothing to remove")
return true
end
uci:delete("wireless", sname) uci:delete("wireless", sname)
dirtyList.network = "r"; dirtyList.wireless = "c" reloadBit(dirtyList, "network"); commitBit(dirtyList, "wireless")
end end
--[[ Switch between wireless static IP and DHCP ]] --[[ Switch between wireless static IP and DHCP ]]
function reconf.staticaddr_add(dirtyList) function reconf.staticaddr_add(dirtyList)
-- M.uciTableSet("network", wifi.NET, { uci:set("network", wifi.NET, "interface")
-- proto = "static", M.uciTableSet("network", wifi.NET, {
-- ipaddr = wifi.AP_ADDRESS, proto = "static",
-- netmask = wifi.AP_NETMASK, ipaddr = wifi.AP_ADDRESS,
-- type = "bridge", netmask = wifi.AP_NETMASK,
-- }) type = "bridge",
uci:set("network", wifi.NET, "proto", "static") })
uci:set("network", wifi.NET, "ipaddr", wifi.AP_ADDRESS) bothBits(dirtyList, "network")
uci:set("network", wifi.NET, "netmask", wifi.AP_NETMASK)
uci:set("network", wifi.NET, "type", "bridge")
dirtyList.network = "b"
end end
--TODO: replace repeated deletes by M.uciTableDelete --TODO: replace repeated deletes by M.uciTableDelete
function reconf.staticaddr_rm(dirtyList) function reconf.staticaddr_rm(dirtyList)
uci:set("network", wifi.NET, "interface")
uci:set("network", wifi.NET, "proto", "dhcp") uci:set("network", wifi.NET, "proto", "dhcp")
uci:delete("network", wifi.NET, "ipaddr") uci:delete("network", wifi.NET, "ipaddr")
uci:delete("network", wifi.NET, "netmask") uci:delete("network", wifi.NET, "netmask")
uci:delete("network", wifi.NET, "type") uci:delete("network", wifi.NET, "type")
dirtyList.network = "b" bothBits(dirtyList, "network")
end end
--[[ Add/remove DHCP pool for wireless net ]] --[[ Add/remove DHCP pool for wireless net ]]
function reconf.dhcppool_add(dirtyList) function reconf.dhcppool_add(dirtyList)
-- M.uciTableSet("dhcp", wifi.NET, { uci:set("dhcp", wifi.NET, "dhcp") --create section
-- interface = wifi.NET, M.uciTableSet("dhcp", wifi.NET, {
-- start = "100", interface = wifi.NET,
-- limit = "150", start = "100",
-- leasetime = "12h", limit = "150",
-- }) leasetime = "12h",
uci:set("dhcp", wifi.NET, "dhcp") --create section??? TODO: what was this again })
uci:set("dhcp", wifi.NET, "interface", wifi.NET) commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
uci:set("dhcp", wifi.NET, "start", "100")
uci:set("dhcp", wifi.NET, "limit", "150")
uci:set("dhcp", wifi.NET, "leasetime", "12h")
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r"
end end
function reconf.dhcppool_rm(dirtyList) function reconf.dhcppool_rm(dirtyList)
uci:delete("dhcp", wifi.NET) uci:delete("dhcp", wifi.NET)
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r" commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end end
--[[ Add/remove webserver 404 redirection and denial of dirlisting ]] --[[ Add/remove webserver 404 redirection and denial of dirlisting ]]
function reconf.wwwredir_add(dirtyList) function reconf.wwwredir_add(dirtyList)
uci:set("uhttpd", "main", "error_page", "/admin/autowifi.html") uci:set("uhttpd", "main", "error_page", "/admin/autowifi.html")
uci:set("uhttpd", "main", "no_dirlist", "1") uci:set("uhttpd", "main", "no_dirlist", "1")
dirtyList.uhttpd = "b" bothBits(dirtyList, "uhttpd")
end end
function reconf.wwwredir_rm(dirtyList) function reconf.wwwredir_rm(dirtyList)
uci:delete("uhttpd", "main", "error_page") uci:delete("uhttpd", "main", "error_page")
uci:delete("uhttpd", "main", "no_dirlist") uci:delete("uhttpd", "main", "no_dirlist")
dirtyList.uhttpd = "b" bothBits(dirtyList, "uhttpd")
end end
--[[ Add/remove redirecton of all DNS requests to self ]] --[[ Add/remove redirecton of all DNS requests to self ]]
function reconf.dnsredir_add(dirtyList) function reconf.dnsredir_add(dirtyList)
local redirText = "/#/" .. wifi.AP_ADDRESS local redirText = "/#/" .. wifi.AP_ADDRESS
local sname = u.getUciSectionName("dhcp", "dnsmasq") local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then if sname == nil then return u:logerror("dhcp config does not contain a dnsmasq section") end
u:logerror("dhcp config does not contain a dnsmasq section") if uci:get("dhcp", sname, "address") ~= nil then return u:logdebug("DNS address redirection already in place, not re-adding", false) end
return false
end
if uci:get("dhcp", sname, "address") ~= nil then
u:logdebug("DNS address redirection already in place, not re-adding")
return true
end
uci:set("dhcp", sname, "address", {redirText}) uci:set("dhcp", sname, "address", {redirText})
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r" commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end end
function reconf.dnsredir_rm(dirtyList) function reconf.dnsredir_rm(dirtyList)
local sname = u.getUciSectionName("dhcp", "dnsmasq") local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then if sname == nil then return u:logerror("dhcp config does not contain a dnsmasq section") end
u:logerror("dhcp config does not contain a dnsmasq section")
return false
end
uci:delete("dhcp", sname, "address") uci:delete("dhcp", sname, "address")
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r" commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end end
--TODO: handle os.rename() return values (nil+msg on error) --TODO: handle os.rename() return values (nil+msg on error)
function reconf.wwwcaptive_add(dirtyList) function reconf.wwwcaptive_add(dirtyList)
if u.exists(M.WWW_CAPTIVE_INDICATOR) then if u.exists(M.WWW_CAPTIVE_INDICATOR) then
u:logdebug("WWW captive directory already in place, not redoing") return u:logdebug("WWW captive directory already in place, not redoing", false)
return true
end end
os.rename("/www", M.WWW_RENAME_NAME) os.rename("/www", M.WWW_RENAME_NAME)
u.symlink(M.WWW_CAPTIVE_PATH, "/www") u.symlink(M.WWW_CAPTIVE_PATH, "/www")
return true return true
end end
function reconf.wwwcaptive_rm(dirtyList) function reconf.wwwcaptive_rm(dirtyList)
if not u.exists(M.WWW_CAPTIVE_INDICATOR) then if not u.exists(M.WWW_CAPTIVE_INDICATOR) then return u:logdebug("WWW captive directory not in place, not undoing", false) end
u:logdebug("WWW captive directory not in place, not undoing")
return true
end
os.remove("/www") os.remove("/www")
return os.rename(M.WWW_RENAME_NAME, "/www") return os.rename(M.WWW_RENAME_NAME, "/www")
end end
@ -212,11 +209,11 @@ function reconf.natreflect_add(dirtyList)
dest_ip = wifi.AP_ADDRESS, dest_ip = wifi.AP_ADDRESS,
target = "DNAT" target = "DNAT"
}) })
dirtyList.firewall = "b" bothBits(dirtyList, "firewall")
end end
function reconf.natreflect_rm(dirtyList) function reconf.natreflect_rm(dirtyList)
uci:delete("firewall", "portalreflect") uci:delete("firewall", "portalreflect")
dirtyList.firewall = "b" bothBits(dirtyList, "firewall")
end end
return M return M

View File

@ -68,16 +68,19 @@ local function log(level, msg, verbose)
if level >= logLevel then if level >= logLevel then
local now = os.date("%m-%d %H:%M:%S") local now = os.date("%m-%d %H:%M:%S")
local i = debug.getinfo(3) local i = debug.getinfo(3)
local v = verbose ~= nil and verbose or logVerbose local v = verbose
if v then logStream:write(now .. " (" .. level .. ") \t" .. msg .. " [" .. i.name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n") if v == nil then v = logVerbose end
local name = i.name or "(nil)"
local vVal = "nil"
if v then logStream:write(now .. " (" .. level .. ") \t" .. msg .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n")
else logStream:write(now .. " (" .. level .. ") \t" .. msg .. "\n") end else logStream:write(now .. " (" .. level .. ") \t" .. msg .. "\n") end
end end
end end
function M:logdebug(msg, verbose) log(M.LOG_LEVEL.debug, msg, verbose) end function M:logdebug(msg, verbose) log(M.LOG_LEVEL.debug, msg, verbose); return true end
function M:loginfo(msg, verbose) log(M.LOG_LEVEL.info, msg, verbose) end function M:loginfo(msg, verbose) log(M.LOG_LEVEL.info, msg, verbose); return true end
function M:logwarn(msg, verbose) log(M.LOG_LEVEL.warn, msg, verbose) end function M:logwarn(msg, verbose) log(M.LOG_LEVEL.warn, msg, verbose); return true end
function M:logerror(msg, verbose) log(M.LOG_LEVEL.error, msg, verbose) end function M:logerror(msg, verbose) log(M.LOG_LEVEL.error, msg, verbose); return false end
function M:logfatal(msg, verbose) log(M.LOG_LEVEL.fatal, msg, verbose) end function M:logfatal(msg, verbose) log(M.LOG_LEVEL.fatal, msg, verbose); return false end
return M return M

View File

@ -1,4 +1,5 @@
local reconf = require("reconf") local reconf = require("reconf")
local util = require("util")
local uci = require("uci").cursor() local uci = require("uci").cursor()
local iwinfo = require("iwinfo") local iwinfo = require("iwinfo")
@ -26,6 +27,19 @@ function M.mapDeviceMode(mode, masterIsAp)
return modeMap[mode] or mode return modeMap[mode] or mode
end end
--[[
- TODO: several modes need to be tested (wep, psk2, mixed-psk)
- See: http://wiki.openwrt.org/doc/uci/wireless#wpa.modes
]]
function M.mapEncryptionType(scanEncrTbl)
local wpaModeMap = { [1] = "psk", [2] = "psk2", [3] = "mixed-psk" }
if scanEncrTbl.enabled == false then return "none" end
if scanEncrTbl.wep == true then return "wep" end
return wpaModeMap[scanEncrTbl.wpa] or scanEncrTbl.description
end
--- Initialize WiFi helper library --- Initialize WiFi helper library
-- @param device wireless device to operate on (optional, defaults to DFL_DEVICE) -- @param device wireless device to operate on (optional, defaults to DFL_DEVICE)
@ -111,7 +125,7 @@ end
--- Create a new UCI network from the given iwinfo data --- Create a new UCI network from the given iwinfo data
-- http://luci.subsignal.org/trac/browser/luci/trunk/libs/iwinfo/src/iwinfo_wext.c?rev=5645 (outdated?) -- http://luci.subsignal.org/trac/browser/luci/trunk/libs/iwinfo/src/iwinfo_wext.c?rev=5645 (outdated?)
-- TODO: configure encryption correctly (how?) -- TODO: delete previous network if exists (match on MAC-address)
-- @param info iwinfo data to create a network from -- @param info iwinfo data to create a network from
-- @param passphrase passphrase to use (optional) -- @param passphrase passphrase to use (optional)
-- @param disabled immediately disable the network (optional) -- @param disabled immediately disable the network (optional)
@ -123,7 +137,7 @@ function M.createConfigFromScanInfo(info, passphrase, disabled)
device = "radio0", device = "radio0",
ssid = info.ssid, ssid = info.ssid,
bssid = info.bssid, bssid = info.bssid,
--encryption = "none", encryption = M.mapEncryptionType(info.encryption),
mode = mode, mode = mode,
} }
if passphrase ~= nil then apconfig.key = passphrase end if passphrase ~= nil then apconfig.key = passphrase end