From 393eb13e81d2f0f88acb4b83e132124c3d376401 Mon Sep 17 00:00:00 2001 From: onefang Date: Sun, 29 Dec 2019 10:16:06 +1000 Subject: Refactor nested pairs() calls. --- apt-panopticommon.lua | 14 +++++++ apt-panopticon-report-email-web.lua | 82 ++++++++++--------------------------- apt-panopticon.lua | 65 ++++++++--------------------- 3 files changed, 54 insertions(+), 107 deletions(-) diff --git a/apt-panopticommon.lua b/apt-panopticommon.lua index b1f39d7..8a18c4b 100644 --- a/apt-panopticommon.lua +++ b/apt-panopticommon.lua @@ -334,6 +334,20 @@ dumpTableSub = function (table, space) return r end + +APT.allpairs = function(tbl, func) + for k, v in pairs(tbl) do + if 'table' == type(v) then + for i, w in pairs(v) do + func(i, w, k, v) + end + else + func(k, v) + end + end +end + + APT.search = function(t, s) for i, v in pairs(t) do if v == s then return true end diff --git a/apt-panopticon-report-email-web.lua b/apt-panopticon-report-email-web.lua index 7d5ce7f..5598515 100755 --- a/apt-panopticon-report-email-web.lua +++ b/apt-panopticon-report-email-web.lua @@ -205,33 +205,20 @@ if nil == email then C("opening mirrors file - " .. e) else -- DNS-RR test. if ("deb.devuan.org" ~= k) and (nil ~= APT.mirrors["deb.devuan.org"]) then - for l, w in pairs(APT.mirrors[k].IPs) do - if type(w) == "table" then - for i, u in pairs(w) do - if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then - local log = logCount("deb.devuan.org", i) - if "" ~= log then - if "" == dns then dns = " " else dns = dns .. " " end - dns = dns .. logCount("deb.devuan.org", i) - else - if "" == dns then dns = " " else dns = dns .. " " end - dns = dns .. i - end - end - end - else - if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then - local log = logCount("deb.devuan.org", l) + APT.allpairs(APT.mirrors[k].IPs, + function(i, w, k, v) + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then + local log = logCount("deb.devuan.org", i) if "" ~= log then if "" == dns then dns = " " else dns = dns .. " " end - dns = dns .. log + dns = dns .. logCount("deb.devuan.org", i) else if "" == dns then dns = " " else dns = dns .. " " end - dns = dns .. l + dns = dns .. i end end end - end + ) if "" == dns then dns = "[no]" end dns = " DNS-RR: " .. dns end @@ -396,33 +383,20 @@ if nil == web then C("opening mirrors file - " .. e) else -- DNS-RR test. if ("deb.devuan.org" ~= k) and (nil ~= APT.mirrors["deb.devuan.org"]) then - for l, w in pairs(APT.mirrors[k].IPs) do - if type(w) == "table" then - for i, u in pairs(w) do - if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then - local log = logCount("deb.devuan.org", i) - if "" ~= log then - if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. logCount("deb.devuan.org", i) - else - if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. "" .. i .. "" - end - end - end - else - if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then - local log = logCount("deb.devuan.org", l) + APT.allpairs(APT.mirrors[k].IPs, + function(i, w, k, v) + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then + local log = logCount("deb.devuan.org", i) if "" ~= log then if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. log + dns = dns .. logCount("deb.devuan.org", i) else if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. "" .. l .. "" + dns = dns .. "" .. i .. "" end end end - end + ) if "" == dns then dns = "no" end if 0 == max then @@ -495,6 +469,7 @@ if nil == web then C("opening mirrors file - " .. e) else APT.mirrors[k].BaseURL = nil APT.mirrors[k].Country = nil APT.mirrors[k].Bandwidth = nil + for l, w in pairs(APT.mirrors[k].IPs) do if type(w) == "table" then n[l] = {} @@ -564,33 +539,20 @@ if nil == web then C("opening mirrors file - " .. e) else -- DNS-RR test. if ("deb.devuan.org" ~= k) and (nil ~= APT.debians["deb.devuan.org"]) then - for l, w in pairs(APT.debians[k].IPs) do - if type(w) == "table" then - for i, u in pairs(w) do - if nil ~= APT.debians["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then - local log = logCount("deb.devuan.org", i) - if "" ~= log then - if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. logCount("deb.devuan.org", i) - else - if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. "" .. i .. "" - end - end - end - else - if nil ~= APT.debians["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then - local log = logCount("deb.devuan.org", l) + APT.allpairs(APT.debians[k].IPs, + function(i, w, k, v) + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then + local log = logCount("deb.devuan.org", i) if "" ~= log then if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. log + dns = dns .. logCount("deb.devuan.org", i) else if "" == dns then dns = " " else dns = dns .. "   " end - dns = dns .. "" .. l .. "" + dns = dns .. "" .. i .. "" end end end - end + ) if "" == dns then dns = "no" end if 0 == max then diff --git a/apt-panopticon.lua b/apt-panopticon.lua index 25c2b49..675754d 100755 --- a/apt-panopticon.lua +++ b/apt-panopticon.lua @@ -1011,17 +1011,8 @@ if 0 < #arg then if nil == path then path = '' end if APT.origin then local ips = APT.results["IPs"] - for k, v in pairs(ips) do - if "table" == type(v) then - for k1, v1 in pairs(v) do - if v1 == "A" then - if APT.testing("IPv4") then APT.exe("./apt-panopticon.lua " .. sendArgs .. " " .. pu.host .. path .. " " .. k1 .. " " .. file):Nice():log():fork() end - elseif v1 == "AAAA" then - if APT.testing("IPv6") then APT.exe("./apt-panopticon.lua " .. sendArgs .. " " .. pu.host .. path .. " " .. k1 .. " " .. file):Nice():log():fork() end - end - D('logging to ' .. APT.logName(pu.host, k1, file)[2]) - end - else + APT.allpairs(ips, + function(k, v) if v == "A" then if APT.testing("IPv4") then APT.exe("./apt-panopticon.lua " .. sendArgs .. " " .. pu.host .. path .. " " .. k .. " " .. file):Nice():log():fork() end elseif v == "AAAA" then @@ -1029,7 +1020,7 @@ if 0 < #arg then end D('logging to ' .. APT.logName(pu.host, k, file)[2]) end - end + ) end if not APT.redir then @@ -1156,48 +1147,28 @@ os.execute('sleep 1') -- Wait for things to start up before checking for them. if not dn then table.insert(baseFiles, ll) end end - APT.logOpen(hst) - APT.logFile:write('

Note log lines will be out of order, this is a bunch of other log files combined.

\n') - for i, f in pairs(baseFiles) do - f = f:sub(9, -1) - APT.logFile:write('
\n
\n

' .. f .. '

\n') - for l in io.lines('results/' .. f) do - if l:match('^' .. os.date('%Y%-%m%-%d ') .. '.*$') then APT.logFile:write(l .. '\n') end + local combine = function(ip, a) + if not APT.logOpen(hst, ip) then print('PROBLEM OPENING LOG FILE ' .. hst .. ' ' .. ip) end + APT.logFile:write('

Note log lines will be out of order, this is a bunch of other log files combined.

\n') + for i, f in pairs(a) do + f = f:sub(9, -1) + APT.logFile:write('
\n
\n

' .. f .. '

\n') + for ln in io.lines('results/' .. f) do + if ln:match('^' .. os.date('!%Y%-%m%-%d ') .. '.*$') then APT.logFile:write(ln .. '\n') end -- %F isn't good enough, coz we have to escape the '-'. + end end + APT.logPost() + APT.args = APT_args + APT.logFile = APT_logFile end - APT.logPost() - APT.args = APT_args - APT.logFile = APT_logFile - + combine(nil, baseFiles) for ip, a in pairs(IPfiles) do if nil == a[1] then for i, f in pairs(a) do - if not APT.logOpen(hst, i) then print('PROBLEM OPENING LOG FILE ' .. hst .. ' ' .. i) end - APT.logFile:write('

Note log lines will be out of order, this is a bunch of other log files combined.

\n') - for j, g in pairs(f) do - g = g:sub(9, -1) - APT.logFile:write('
\n
\n

' .. g .. '

\n') - for l in io.lines('results/' .. g) do - if l:match('^' .. os.date('%Y%-%m%-%d ') .. '.*$') then APT.logFile:write(l .. '\n') end - end - end - APT.logPost() - APT.args = APT_args - APT.logFile = APT_logFile + combine(i, f) end else - if not APT.logOpen(hst, ip) then print('PROBLEM OPENING LOG FILE ' .. hst .. ' ' .. ip) end - APT.logFile:write('

Note log lines will be out of order, this is a bunch of other log files combined.

\n') - for i, f in pairs(a) do - f = f:sub(9, -1) - APT.logFile:write('
\n
\n

' .. f .. '

\n') - for l in io.lines('results/' .. f) do - if l:match('^' .. os.date('%Y%-%m%-%d ') .. '.*$') then APT.logFile:write(l .. '\n') end - end - end - APT.logPost() - APT.args = APT_args - APT.logFile = APT_logFile + combine(ip, a) end end -- cgit v1.1