From efaf58c880e2e3b47948a357cb9b105ca00cd227 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Sat, 27 Jul 2013 00:25:43 +0200 Subject: [PATCH] Fix outdated configuration key names; add API function to get all configuration keys at once; remove logging of request arguments to prevent printing large blocks of gcode. --- src/main.lua | 4 +++- src/network/netconfig.lua | 12 ++++++------ src/rest/api/api_config.lua | 7 ++++++- src/rest/api/api_network.lua | 27 ++++++++++++--------------- src/util/settings.lua | 21 ++++++++++++++++++++- 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/main.lua b/src/main.lua index 6dc4147..348602c 100644 --- a/src/main.lua +++ b/src/main.lua @@ -47,8 +47,10 @@ end local function main() local rq = RequestClass.new(postData, confDefaults.DEBUG_API) +-- log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") +-- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") - .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) + .. "/" .. (rq:getRealApiFunctionName() or "")) if rq:getRequestMethod() ~= 'CMDLINE' then log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort()) log:debug("user agent: " .. rq:getUserAgent()) diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index 985be57..3e3a906 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -96,7 +96,7 @@ end --[[ Add/remove access point network ]] function reconf.apnet_add_noreload(dirtyList) reconf.apnet_add(dirtyList, true) end function reconf.apnet_add(dirtyList, noReload) - local ourSsid = wifi.getSubstitutedSsid(settings.get('apSsid')) + local ourSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) local sname = nil uci:foreach('wireless', 'wifi-iface', function(s) if s.ssid == ourSsid then sname = s['.name']; return false end @@ -117,7 +117,7 @@ end function reconf.apnet_rm(dirtyList) local sname = nil uci:foreach('wireless', 'wifi-iface', function(s) - if s.ssid == wifi.getSubstitutedSsid(settings.get('apSsid')) then sname = s['.name']; return false end + if s.ssid == wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) then sname = s['.name']; return false end end) if sname == nil then return log:info("AP network configuration does not exist, nothing to remove") end uci:delete('wireless', sname) @@ -132,8 +132,8 @@ function reconf.staticaddr_add(dirtyList) --NOTE: 'type = "bridge"' should -not- be added as this prevents defining a separate dhcp pool (http://wiki.openwrt.org/doc/recipes/routedap) M.uciTableSet('network', wifi.NET, { proto = 'static', - ipaddr = settings.get('apAddress'), - netmask = settings.get('apNetmask') + ipaddr = settings.get('network.ap.address'), + netmask = settings.get('network.ap.netmask') }) bothBits(dirtyList, 'network') end @@ -180,7 +180,7 @@ end --[[ Add/remove redirecton of all DNS requests to self ]] function reconf.dnsredir_add(dirtyList) - local redirText = '/#/' .. settings.get('apAddress') + local redirText = '/#/' .. settings.get('network.ap.address') local sname = utils.getUciSectionName('dhcp', 'dnsmasq') if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end if uci:get('dhcp', sname, 'address') ~= nil then return log:debug("DNS address redirection already in place, not re-adding", false) end @@ -228,7 +228,7 @@ function reconf.natreflect_add(dirtyList) proto = 'tcp', src_dport = '80', dest_port = '80', - dest_ip = settings.get('apAddress'), + dest_ip = settings.get('network.ap.address'), target = 'DNAT' }) bothBits(dirtyList, 'firewall') diff --git a/src/rest/api/api_config.lua b/src/rest/api/api_config.lua index c9f0229..4e58a39 100644 --- a/src/rest/api/api_config.lua +++ b/src/rest/api/api_config.lua @@ -8,7 +8,6 @@ local M = { function M._global_GET(request, response) response:setSuccess() - --TODO: we need a function to list all configuration keys for k,v in pairs(request:getAll()) do local r,m = settings.get(k) @@ -30,4 +29,10 @@ function M._global_POST(request, response) end end +function M.all_GET(request, response) + for k,v in pairs(settings.getAll()) do + response:addData(k,v) + end +end + return M diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index 6ee8814..a72313f 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -1,6 +1,5 @@ -local s = require("util.settings") -local u = require("util.utils") -local l = require("util.logger") +local settings = require("util.settings") +local utils = require("util.utils") local netconf = require("network.netconfig") local wifi = require("network.wlanconfig") local ResponseClass = require("rest.response") @@ -17,8 +16,8 @@ end --accepts API argument 'nofilter'(bool) to disable filtering of APs and 'self' --accepts with_raw(bool) to include raw table dump function M.scan(request, response) - local noFilter = u.toboolean(request:get("nofilter")) - local withRaw = u.toboolean(request:get("with_raw")) + local noFilter = utils.toboolean(request:get("nofilter")) + local withRaw = utils.toboolean(request:get("with_raw")) local sr = wifi.getScanInfo() local si, se @@ -26,7 +25,7 @@ function M.scan(request, response) response:setSuccess("") local netInfoList = {} for _, se in ipairs(sr) do - if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.getSubstitutedSsid(s.get('apSsid')) then + if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) then local netInfo = {} netInfo["ssid"] = se.ssid @@ -37,7 +36,7 @@ function M.scan(request, response) netInfo["signal"] = se.signal netInfo["quality"] = se.quality netInfo["quality_max"] = se.quality_max - if withRaw then netInfo["_raw"] = u.dump(se) end + if withRaw then netInfo["_raw"] = utils.dump(se) end table.insert(netInfoList, netInfo) end @@ -52,8 +51,8 @@ end --accepts API argument 'nofilter'(bool) to disable filtering of APs and 'self' --accepts with_raw(bool) to include raw table dump function M.known(request, response) - local noFilter = u.toboolean(request:get("nofilter")) - local withRaw = u.toboolean(request:get("with_raw")) + local noFilter = utils.toboolean(request:get("nofilter")) + local withRaw = utils.toboolean(request:get("with_raw")) response:setSuccess() local netInfoList = {} @@ -64,7 +63,7 @@ function M.known(request, response) netInfo["bssid"] = net.bssid or "" netInfo["channel"] = net.channel or "" netInfo["encryption"] = net.encryption - if withRaw then netInfo["_raw"] = u.dump(net) end + if withRaw then netInfo["_raw"] = utils.dump(net) end table.insert(netInfoList, netInfo) end end @@ -74,7 +73,7 @@ end --accepts with_raw(bool) to include raw table dump function M.status(request, response) - local withRaw = u.toboolean(request:get("with_raw")) + local withRaw = utils.toboolean(request:get("with_raw")) local ds = wifi.getDeviceState() response:setSuccess() @@ -88,10 +87,9 @@ function M.status(request, response) response:addData("txpower", ds.txpower) response:addData("signal", ds.signal) response:addData("noise", ds.noise) - if withRaw then response:addData("_raw", u.dump(ds)) end + if withRaw then response:addData("_raw", utils.dump(ds)) end end ---UNTESTED --requires ssid(string), accepts phrase(string), recreate(bool) function M.associate_POST(request, response) local argSsid = request:get("ssid") @@ -138,9 +136,8 @@ function M.disassociate_POST(request, response) response:addData("wifi_restart_result", rv) end ---UNTESTED function M.openap_POST(request, response) - local ssid = wifi.getSubstitutedSsid(s.get('apSsid')) + local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) netconf.switchConfiguration{apnet="add_noreload"} wifi.activateConfig(ssid) netconf.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add", wwwredir="add", dnsredir="add" } diff --git a/src/util/settings.lua b/src/util/settings.lua index 4991abc..311b14a 100644 --- a/src/util/settings.lua +++ b/src/util/settings.lua @@ -27,7 +27,15 @@ local ERR_NO_SUCH_KEY = "key does not exist" -- @return The substituted key, or the key parameter itself if it is not of type 'string'. local function replaceDots(key) if type(key) ~= 'string' then return key end - return key:gsub('%.', '_') + local r = key:gsub('%.', '_') + return r +end + +-- The inverse of replaceDots() +local function replaceUnderscores(key) + if type(key) ~= 'string' then return key end + local r = key:gsub('_', '%.') + return r end local function toUciValue(v, vType) @@ -97,6 +105,17 @@ function M.get(key) return actualV end +function M.getAll() + local result = {} + for k,_ in pairs(baseconfig) do + if not k:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway + local key = replaceUnderscores(k) + result[key] = M.get(key) + end + end + return result +end + function M.exists(key) key = replaceDots(key) return getBaseKeyTable(key) ~= nil