Skip to content

dnsdist examples

Evaggelos Balaskas edited this page Dec 25, 2019 · 2 revisions

wiki page with dnsdist examples

Filter DNS queries based on DoH endpoint

In the below example, we support two DoH local Endpoints and we want to send their traffic to two different PowerDNS instances. Using HTTPPathRegexRule we can match them we a specific pool.

New in version 1.4.0

-- DNS over HTTPS
addDOHLocal("", nil, nil, "/dns-query", { reusePort=true })
addDOHLocal("", nil, nil, "/ads", { reusePort=true })

-- Filter DNS queries to backend DNS based on endpoint
newServer({address="", pool="ads"})
addAction(HTTPPathRegexRule("ads"), PoolAction("ads"))

-- fwd queries to:

Create a dynamic wildcard DNS

a full working example is here.

This lua code will resolve any domain name with an IP in it's hostname to it's IP. If no IP match, then dnsdist will response with client's IP address (whatismyip).

-- Import  LuaJIT - FFI Library
local ffi = require("ffi")

-- Declare inet_pton
  int inet_pton(int af, const char *src, void *dst);

-- Family IPv4
local AF_INET = 2
local inet ="unsigned int [1]")

function luarule(dq)

  -- replace dash with dot
  ip = string.gsub(dq.qname:toString(), "-", ".")
  -- get only digits
  ip = string.match(ip, "%d+%.%d+%.%d+%.%d+")

  -- if not matched, print whatismyip
  if ip == nil then
    return DNSAction.Spoof, dq.remoteaddr:tostring()

  -- Validate IPv4
  if ffi.C.inet_pton(AF_INET, ip , inet) == 1 then
    return DNSAction.Spoof, ip
    return DNSAction.Spoof, dq.remoteaddr:tostring()


addAction(AllRule(), LuaAction(luarule))
You can’t perform that action at this time.