]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Add HTTPPathRegexRule
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 1 Aug 2019 14:36:54 +0000 (16:36 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 5 Aug 2019 15:22:48 +0000 (17:22 +0200)
pdns/dnsdist-console.cc
pdns/dnsdist-lua-rules.cc
pdns/dnsdistdist/dnsdist-rules.hh
pdns/dnsdistdist/docs/rules-actions.rst
pdns/dnsdistdist/doh.cc

index e0f0db3693cf8fc40dc4ac060aaa7dde9d1a05d3..f33292229657dd1c0a24b39e8bb17590b708580a 100644 (file)
@@ -408,6 +408,7 @@ const std::vector<ConsoleKeyword> g_consoleKeywords{
   { "getTLSContext", true, "n", "returns the TLS context with index n" },
   { "getTLSFrontend", true, "n", "returns the TLS frontend with index n" },
   { "HTTPHeaderRule", true, "name, regex", "matches DoH queries with a HTTP header 'name' whose content matches the regular expression 'regex'"},
+  { "HTTPPathRegexRule", true, "regex", "matches DoH queries whose HTTP path matches 'regex'"},
   { "HTTPPathRule", true, "path", "matches DoH queries whose HTTP path is an exact match to 'path'"},
   { "inClientStartup", true, "", "returns true during console client parsing of configuration" },
   { "includeDirectory", true, "path", "nclude configuration files from `path`" },
index 98aae9ddb2acd1575e026532dc9efaadfb7105a2..3ed911748d6456931a34ffc5958f5a6eee179ff4 100644 (file)
@@ -287,6 +287,9 @@ void setupLuaRules()
   g_lua.writeFunction("HTTPPathRule", [](const std::string& path) {
       return std::shared_ptr<DNSRule>(new HTTPPathRule(path));
     });
+  g_lua.writeFunction("HTTPPathRegexRule", [](const std::string& regex) {
+      return std::shared_ptr<DNSRule>(new HTTPPathRegexRule(regex));
+    });
 #endif
 
 #ifdef HAVE_RE2
index 1d9944e5270b82859eed0b2592545fbc412b035e..ebe80318edabceb807637bc7123e9f5b5d765437 100644 (file)
@@ -529,6 +529,17 @@ public:
 private:
   string d_path;
 };
+
+class HTTPPathRegexRule : public DNSRule
+{
+public:
+  HTTPPathRegexRule(const std::string& regex);
+  bool matches(const DNSQuestion* dq) const override;
+  string toString() const override;
+private:
+  Regex d_regex;
+  std::string d_visual;
+};
 #endif
 
 class SNIRule : public DNSRule
index a5879eb9651f967a4c46008cf91e9127089d343d..d3194e9c5f69eb6f6db094ab1f39b72531910489 100644 (file)
@@ -583,6 +583,13 @@ These ``DNSRule``\ s be one of the following items:
   :param str name: The case-insensitive name of the HTTP header to match on
   :param str regex: A regular expression to match the content of the specified header
 
+.. function:: HTTPPathRegexRule(regex)
+  .. versionadded:: 1.4.0
+
+  Matches DNS over HTTPS queries with a HTTP path matching the regular expression supplied in ``regex``. For example, if the query has been sent to the https://192.0.2.1:443/PowerDNS?dns=... URL, the path would be '/PowerDNS'.
+
+  :param str regex: The regex to match on
+
 .. function:: HTTPPathRule(path)
   .. versionadded:: 1.4.0
 
index f932c49f373f855e59f63d8c5afc68497196df06..618333322ab6d0d8e86a63c600a4ea410a522e53 100644 (file)
@@ -542,6 +542,7 @@ HTTPHeaderRule::HTTPHeaderRule(const std::string& header, const std::string& reg
   d_visual = "http[" + header+ "] ~ " + regex;
 
 }
+
 bool HTTPHeaderRule::matches(const DNSQuestion* dq) const
 {
   if(!dq->du) {
@@ -587,6 +588,30 @@ string HTTPPathRule::toString() const
   return "url path == " + d_path;
 }
 
+HTTPPathRegexRule::HTTPPathRegexRule(const std::string& regex): d_regex(regex), d_visual("http path ~ " + regex)
+{
+}
+
+bool HTTPPathRegexRule::matches(const DNSQuestion* dq) const
+{
+  if(!dq->du) {
+    return false;
+  }
+
+  if(dq->du->req->query_at == SIZE_MAX) {
+    return d_regex.match(std::string(dq->du->req->path.base, dq->du->req->path.len));
+  }
+  else {
+    cerr<<std::string(dq->du->req->path.base, dq->du->req->path.len - dq->du->req->query_at)<<endl;
+    return d_regex.match(std::string(dq->du->req->path.base, dq->du->req->path.len - dq->du->req->query_at));
+  }
+}
+
+string HTTPPathRegexRule::toString() const
+{
+  return d_visual;
+}
+
 void dnsdistclient(int qsock, int rsock)
 {
   setThreadName("dnsdist/doh-cli");