]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Add a Lua interface to get the list of dynamic blocks
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 13 Nov 2023 10:07:49 +0000 (11:07 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 14 Nov 2023 14:43:38 +0000 (15:43 +0100)
pdns/dnsdist-lua-inspection.cc
pdns/dnsdist-lua.cc

index 3ae4a4e8ecb539a64575ad6d41a3feaca692126e..f1bb31fb160b79a371d20e331cc718caa76a7969 100644 (file)
@@ -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<DynBlock, unsigned int>("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 */
 }
index c9fdeb72e54b9549ec55b07d5671f3fabe4c8233..6c2d972184a1cd7b0a4682f0e1d438a19b040a0d 100644 (file)
@@ -1418,6 +1418,54 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
     });
   });
 
+  luaCtx.writeFunction("getDynamicBlocks", []() {
+    setLuaNoSideEffect();
+    struct timespec now;
+    gettime(&now);
+
+    LuaAssociativeTable<DynBlock> 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<DynBlock> entries;
+    auto fullCopy = g_dynblockSMT.getCopy();
+    fullCopy.visit([&now, &entries](const SuffixMatchTree<DynBlock>& 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;