From d4d5c5c3e41402a40de5aa4d6589e944ab90d2e3 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Thu, 19 May 2016 12:17:18 +0200 Subject: [PATCH] dnsdist: Add `showResponseRules()`, `{mv,rm,top}ResponseRule()` --- pdns/README-dnsdist.md | 6 ++++- pdns/dnsdist-console.cc | 13 +++++----- pdns/dnsdist-lua2.cc | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/pdns/README-dnsdist.md b/pdns/README-dnsdist.md index 70bb432f4..2b40c3578 100644 --- a/pdns/README-dnsdist.md +++ b/pdns/README-dnsdist.md @@ -1078,9 +1078,13 @@ instantiate a server with additional parameters * Rule management related: * `getAction(num)`: returns the Action associate with rule 'num'. * `showRules()`: show all defined rules (Pool, Block, QPS, addAnyTCRule) - * `rmRule(n)`: remove rule n + * `mvResponseRule(from, to)`: move response rule 'from' to a position where it is in front of 'to'. 'to' can be one larger than the largest rule, + in which case the rule will be moved to the last position. * `mvRule(from, to)`: move rule 'from' to a position where it is in front of 'to'. 'to' can be one larger than the largest rule, in which case the rule will be moved to the last position. + * `rmResponseRule(n)`: remove response rule n + * `rmRule(n)`: remove rule n + * `topResponseRule()`: move the last response rule to the first position * `topRule()`: move the last rule to the first position * Built-in Actions for Rules: * `AllowAction()`: let these packets go through diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index f3ba05124..eb1476dcd 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -234,22 +234,23 @@ char* my_generator(const char* text, int state) "generateDNSCryptCertificate(", "generateDNSCryptProviderKeys(", "getPoolServers(", "getResponseRing(", "getServer(", "getServers()", "grepq(", "leastOutstanding", "LogAction(", - "makeKey()", "MaxQPSIPRule(", "MaxQPSRule(", "mvRule(", + "makeKey()", "MaxQPSIPRule(", "MaxQPSRule(", "mvResponseRule(", + "mvRule(", "newDNSName(", "newQPSLimiter(", "newRemoteLogger(", "newServer(", "newServerPolicy(", "newSuffixMatchNode(", "NoRecurseAction(", "PoolAction(", "printDNSCryptProviderFingerprint(", - "RegexRule(", "RemoteLogAction(", "RemoteLogResponseAction(", "rmRule(", - "rmServer(", "roundrobin", + "RegexRule(", "RemoteLogAction(", "RemoteLogResponseAction(", "rmResponseRule(", + "rmRule(", "rmServer(", "roundrobin", "QTypeRule(", "setACL(", "setDNSSECPool(", "setECSOverride(", "setECSSourcePrefixV4(", "setECSSourcePrefixV6(", "setKey(", "setLocal(", "setMaxTCPClientThreads(", "setMaxTCPQueuedConnections(", "setMaxUDPOutstanding(", "setServerPolicy(", "setServerPolicyLua(", "setTCPRecvTimeout(", "setTCPSendTimeout(", "setVerboseHealthChecks(", "show(", "showACL()", - "showDNSCryptBinds()", "showDynBlocks()", "showResponseLatency()", "showRules()", - "showServerPolicy()", "showServers()", "shutdown()", "SpoofAction(", + "showDNSCryptBinds()", "showDynBlocks()", "showResponseLatency()", "showResponseRules()", + "showRules()", "showServerPolicy()", "showServers()", "shutdown()", "SpoofAction(", "TCAction(", "testCrypto()", "topBandwidth(", "topClients(", - "topQueries(", "topResponses(", "topRule()", "topSlow(", + "topQueries(", "topResponses(", "topResponseRule()", "topRule()", "topSlow(", "truncateTC(", "webserver(", "whashed", "wrandom" }; static int s_counter=0; diff --git a/pdns/dnsdist-lua2.cc b/pdns/dnsdist-lua2.cc index ada1ebbd7..16799a6bf 100644 --- a/pdns/dnsdist-lua2.cc +++ b/pdns/dnsdist-lua2.cc @@ -606,4 +606,57 @@ void moreLua(bool client) g_lua.registerFunction("getStats", &DNSAction::getStats); + g_lua.writeFunction("showResponseRules", []() { + setLuaNoSideEffect(); + boost::format fmt("%-3d %9d %-50s %s\n"); + g_outputBuffer += (fmt % "#" % "Matches" % "Rule" % "Action").str(); + int num=0; + for(const auto& lim : g_resprulactions.getCopy()) { + string name = lim.first->toString(); + g_outputBuffer += (fmt % num % lim.first->d_matches % name % lim.second->toString()).str(); + ++num; + } + }); + + g_lua.writeFunction("rmResponseRule", [](unsigned int num) { + setLuaSideEffect(); + auto rules = g_resprulactions.getCopy(); + if(num >= rules.size()) { + g_outputBuffer = "Error: attempt to delete non-existing rule\n"; + return; + } + rules.erase(rules.begin()+num); + g_resprulactions.setState(rules); + }); + + g_lua.writeFunction("topResponseRule", []() { + setLuaSideEffect(); + auto rules = g_resprulactions.getCopy(); + if(rules.empty()) + return; + auto subject = *rules.rbegin(); + rules.erase(std::prev(rules.end())); + rules.insert(rules.begin(), subject); + g_resprulactions.setState(rules); + }); + + g_lua.writeFunction("mvResponseRule", [](unsigned int from, unsigned int to) { + setLuaSideEffect(); + auto rules = g_resprulactions.getCopy(); + if(from >= rules.size() || to > rules.size()) { + g_outputBuffer = "Error: attempt to move rules from/to invalid index\n"; + return; + } + auto subject = rules[from]; + rules.erase(rules.begin()+from); + if(to == rules.size()) + rules.push_back(subject); + else { + if(from < to) + --to; + rules.insert(rules.begin()+to, subject); + } + g_resprulactions.setState(rules); + }); + } -- 2.40.0