0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 02:53: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.channel = line[2];
r.mode = line[3];
r.encryption = line[4];
return r;
}
@ -76,7 +77,7 @@ function fetchAvailableNetworks() {
if (value != "") {
var netinf = parseNetLine(value);
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);
@ -113,7 +114,7 @@ function connectBtnHandler() {
return;
}
$.get(cgiBase + "?op=assoc&ssid=" + ssid + "&passphrase=" + phrase, function(data) {
$.get(cgiBase + "?op=assoc&ssid=" + ssid + "&phrase=" + phrase, function(data) {
data = parseResponse(data);
if (data.status == "ERR") {
setResult(data.msg, true);

View File

@ -66,13 +66,15 @@ function main()
--TODO:
-- - 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
u.printWithSuccess(#sr .. " network(s) found");
for _, se in ipairs(sr) do
print("[[ " .. u.dump(se) .. " ]]") --TEMP
if se.mode ~= "ap" then
print(se.ssid .. "," .. se.bssid .. "," .. se.channel .. "," .. wifi.mapDeviceMode(se.mode))
--print("[[ " .. u.dump(se) .. " ]]") --TEMP
if se.mode ~= "ap" and se.ssid ~= wifi.AP_SSID then
print(se.ssid .. "," .. se.bssid .. "," .. se.channel .. "," .. wifi.mapDeviceMode(se.mode) .. "," .. wifi.mapEncryptionType(se.encryption))
end
end
else
@ -110,15 +112,15 @@ function main()
if cfg == nil or argRecreate ~= nil then
local scanResult = wifi.getScanInfo(argSsid)
if scanResult ~= nil then
wifi.createConfigFromScanInfo(scanResult)
wifi.createConfigFromScanInfo(scanResult, argPhrase)
else
--check for error
u.exitWithError("No wireless network with SSID '" .. argSsid .. "' is available")
end
end
wifi.activateConfig(argSsid)
local rv = wifi.restart()
u.exitWithSuccess("Wlan associated with network "..argSsid.."! [$?=" .. rv .. "]")
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 .. "!")
elseif argOperation == "disassoc" then
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
reconf.switchConfiguration{apnet="add_noreload"}
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 .. "')")
elseif argOperation == "rm" then
@ -141,7 +143,8 @@ function main()
end
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"}
u.exitWithSuccess("nop")

View File

@ -1,12 +1,17 @@
#!/bin/sh /etc/rc.common
START=19
LOGGER="logger -s -t autowifi -p 6"
start() {
echo "dummy auto wifi init"
boot() {
$LOGGER "dummy boot"
#/www/cgi-bin/wfcf op=auto
}
stop() {
echo "dummy auto wifi deinit"
start() {
$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_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)
wifi = wifiInstance
silent = reloadSilent or false
@ -60,12 +74,16 @@ end
--[[ 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 ]]
function reconf.wifiiface_add(dirtyList)
uci:set("network", wifi.NET, "interface")
dirtyList.network = "c";
-- uci:set("network", wifi.NET, "proto", "dhcp")
commitBit(dirtyList, "network")
end
--[[ Add/remove access point network ]]
@ -85,118 +103,97 @@ function reconf.apnet_add(dirtyList, noReload)
mode = "ap",
})
dirtyList.wireless = "c"
if noReload == nil or noReload == false then dirtyList.network = "r" end
commitBit(dirtyList, "wireless")
if noReload == nil or noReload == false then reloadBit(dirtyList, "network") end
end
function reconf.apnet_rm(dirtyList)
local sname = nil
uci:foreach("wireless", "wifi-iface", function(s)
if s.ssid == wifi.AP_SSID then sname = s[".name"]; return false end
end)
if sname == nil then
u:loginfo("AP network configuration does not exist, nothing to remove")
return true
end
if sname == nil then return u:loginfo("AP network configuration does not exist, nothing to remove") end
uci:delete("wireless", sname)
dirtyList.network = "r"; dirtyList.wireless = "c"
reloadBit(dirtyList, "network"); commitBit(dirtyList, "wireless")
end
--[[ Switch between wireless static IP and DHCP ]]
function reconf.staticaddr_add(dirtyList)
-- M.uciTableSet("network", wifi.NET, {
-- proto = "static",
-- ipaddr = wifi.AP_ADDRESS,
-- netmask = wifi.AP_NETMASK,
-- type = "bridge",
-- })
uci:set("network", wifi.NET, "proto", "static")
uci:set("network", wifi.NET, "ipaddr", wifi.AP_ADDRESS)
uci:set("network", wifi.NET, "netmask", wifi.AP_NETMASK)
uci:set("network", wifi.NET, "type", "bridge")
dirtyList.network = "b"
uci:set("network", wifi.NET, "interface")
M.uciTableSet("network", wifi.NET, {
proto = "static",
ipaddr = wifi.AP_ADDRESS,
netmask = wifi.AP_NETMASK,
type = "bridge",
})
bothBits(dirtyList, "network")
end
--TODO: replace repeated deletes by M.uciTableDelete
function reconf.staticaddr_rm(dirtyList)
uci:set("network", wifi.NET, "interface")
uci:set("network", wifi.NET, "proto", "dhcp")
uci:delete("network", wifi.NET, "ipaddr")
uci:delete("network", wifi.NET, "netmask")
uci:delete("network", wifi.NET, "type")
dirtyList.network = "b"
bothBits(dirtyList, "network")
end
--[[ Add/remove DHCP pool for wireless net ]]
function reconf.dhcppool_add(dirtyList)
-- M.uciTableSet("dhcp", wifi.NET, {
-- interface = wifi.NET,
-- start = "100",
-- 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)
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"
uci:set("dhcp", wifi.NET, "dhcp") --create section
M.uciTableSet("dhcp", wifi.NET, {
interface = wifi.NET,
start = "100",
limit = "150",
leasetime = "12h",
})
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end
function reconf.dhcppool_rm(dirtyList)
uci:delete("dhcp", wifi.NET)
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r"
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end
--[[ Add/remove webserver 404 redirection and denial of dirlisting ]]
function reconf.wwwredir_add(dirtyList)
uci:set("uhttpd", "main", "error_page", "/admin/autowifi.html")
uci:set("uhttpd", "main", "no_dirlist", "1")
dirtyList.uhttpd = "b"
bothBits(dirtyList, "uhttpd")
end
function reconf.wwwredir_rm(dirtyList)
uci:delete("uhttpd", "main", "error_page")
uci:delete("uhttpd", "main", "no_dirlist")
dirtyList.uhttpd = "b"
bothBits(dirtyList, "uhttpd")
end
--[[ Add/remove redirecton of all DNS requests to self ]]
function reconf.dnsredir_add(dirtyList)
local redirText = "/#/" .. wifi.AP_ADDRESS
local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then
u:logerror("dhcp config does not contain a dnsmasq section")
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
if sname == nil then return u:logerror("dhcp config does not contain a dnsmasq section") end
if uci:get("dhcp", sname, "address") ~= nil then return u:logdebug("DNS address redirection already in place, not re-adding", false) end
uci:set("dhcp", sname, "address", {redirText})
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r"
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end
function reconf.dnsredir_rm(dirtyList)
local sname = u.getUciSectionName("dhcp", "dnsmasq")
if sname == nil then
u:logerror("dhcp config does not contain a dnsmasq section")
return false
end
if sname == nil then return u:logerror("dhcp config does not contain a dnsmasq section") end
uci:delete("dhcp", sname, "address")
dirtyList.dhcp = "c"; dirtyList.dnsmasq = "r"
commitBit(dirtyList, "dhcp"); reloadBit(dirtyList, "dnsmasq")
end
--TODO: handle os.rename() return values (nil+msg on error)
function reconf.wwwcaptive_add(dirtyList)
if u.exists(M.WWW_CAPTIVE_INDICATOR) then
u:logdebug("WWW captive directory already in place, not redoing")
return true
return u:logdebug("WWW captive directory already in place, not redoing", false)
end
os.rename("/www", M.WWW_RENAME_NAME)
u.symlink(M.WWW_CAPTIVE_PATH, "/www")
return true
end
function reconf.wwwcaptive_rm(dirtyList)
if not u.exists(M.WWW_CAPTIVE_INDICATOR) then
u:logdebug("WWW captive directory not in place, not undoing")
return true
end
if not u.exists(M.WWW_CAPTIVE_INDICATOR) then return u:logdebug("WWW captive directory not in place, not undoing", false) end
os.remove("/www")
return os.rename(M.WWW_RENAME_NAME, "/www")
end
@ -212,11 +209,11 @@ function reconf.natreflect_add(dirtyList)
dest_ip = wifi.AP_ADDRESS,
target = "DNAT"
})
dirtyList.firewall = "b"
bothBits(dirtyList, "firewall")
end
function reconf.natreflect_rm(dirtyList)
uci:delete("firewall", "portalreflect")
dirtyList.firewall = "b"
bothBits(dirtyList, "firewall")
end
return M

View File

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

View File

@ -1,4 +1,5 @@
local reconf = require("reconf")
local util = require("util")
local uci = require("uci").cursor()
local iwinfo = require("iwinfo")
@ -26,6 +27,19 @@ function M.mapDeviceMode(mode, masterIsAp)
return modeMap[mode] or mode
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
-- @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
-- 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 passphrase passphrase to use (optional)
-- @param disabled immediately disable the network (optional)
@ -123,7 +137,7 @@ function M.createConfigFromScanInfo(info, passphrase, disabled)
device = "radio0",
ssid = info.ssid,
bssid = info.bssid,
--encryption = "none",
encryption = M.mapEncryptionType(info.encryption),
mode = mode,
}
if passphrase ~= nil then apconfig.key = passphrase end