From: Remi Gacogne Date: Mon, 13 Nov 2023 10:07:49 +0000 (+0100) Subject: dnsdist: Add a Lua interface to get the list of dynamic blocks X-Git-Tag: rec-5.0.0-rc1~31^2~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=34c9e69ce1da3265900d831fdb49df1a29c4cc8b;p=thirdparty%2Fpdns.git dnsdist: Add a Lua interface to get the list of dynamic blocks --- diff --git a/pdns/dnsdist-lua-inspection.cc b/pdns/dnsdist-lua-inspection.cc index 3ae4a4e8ec..f1bb31fb16 100644 --- a/pdns/dnsdist-lua-inspection.cc +++ b/pdns/dnsdist-lua-inspection.cc @@ -946,5 +946,14 @@ void setupLuaInspection(LuaContext& luaCtx) }); luaCtx.registerFunction("setQuiet", &DynBlockRulesGroup::setQuiet); luaCtx.registerFunction("toString", &DynBlockRulesGroup::toString); + + /* DynBlock object accessors */ + luaCtx.registerMember("reason", &DynBlock::reason); + luaCtx.registerMember("domain", &DynBlock::domain); + luaCtx.registerMember("until", &DynBlock::until); + luaCtx.registerMember("blocks", [](const DynBlock& block) { return block.blocks.load(); }, [](DynBlock& block, [[maybe_unused]] unsigned int blocks) { }); + luaCtx.registerMember("action", &DynBlock::action); + luaCtx.registerMember("warning", &DynBlock::warning); + luaCtx.registerMember("bpf", &DynBlock::bpf); #endif /* DISABLE_DYNBLOCKS */ } diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index c9fdeb72e5..6c2d972184 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -1418,6 +1418,54 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) }); }); + luaCtx.writeFunction("getDynamicBlocks", []() { + setLuaNoSideEffect(); + struct timespec now; + gettime(&now); + + LuaAssociativeTable entries; + auto fullCopy = g_dynblockNMG.getCopy(); + for (const auto& blockPair : fullCopy) { + const auto& requestor = blockPair.first; + if (!(now < blockPair.second.until)) { + continue; + } + auto entry = blockPair.second; + if (g_defaultBPFFilter && entry.bpf) { + entry.blocks += g_defaultBPFFilter->getHits(requestor.getNetwork()); + } + if (entry.action == DNSAction::Action::None) { + entry.action = g_dynBlockAction; + } + entries.emplace(requestor.toString(), std::move(entry)); + } + return entries; + }); + + luaCtx.writeFunction("getSMTDynamicBlocks", []() { + setLuaNoSideEffect(); + struct timespec now; + gettime(&now); + + LuaAssociativeTable entries; + auto fullCopy = g_dynblockSMT.getCopy(); + fullCopy.visit([&now, &entries](const SuffixMatchTree& node) { + if (!(now < node.d_value.until)) { + return; + } + auto entry = node.d_value; + string key("empty"); + if (!entry.domain.empty()) { + key = entry.domain.toString(); + } + if (entry.action == DNSAction::Action::None) { + entry.action = g_dynBlockAction; + } + entries.emplace(std::move(key), std::move(entry)); + }); + return entries; + }); + luaCtx.writeFunction("clearDynBlocks", []() { setLuaSideEffect(); nmts_t nmg;