Skip to content

Instantly share code, notes, and snippets.

@ahupowerdns
Last active July 31, 2024 21:11
Show Gist options
  • Save ahupowerdns/bb1a043ce453a9f9eeed to your computer and use it in GitHub Desktop.
Save ahupowerdns/bb1a043ce453a9f9eeed to your computer and use it in GitHub Desktop.
How to do really simple adblocking with the PowerDNS Recursor 4.x

First, clone the Mozilla focus project and make it fetch its list:

$ git clone https://github.com/mozilla/focus.git
$ cd focus
$ ./checkout.sh
$ cd Lists

This delivers several JSON formatted files, of which we are going to use disconnect-advertising.json. We'll filter out the good bits using jq, and create a Lua representation:

(
echo 'return{'
for a in $(jq '.[].trigger["url-filter"]' disconnect-advertising.json  | cut -f3 -d? | sed 's:\\\\.:.:g' | sed s:\"::)
do 
   echo \"$a\", 
done 
echo '}'
) > blocklist.lua    # ends up as return{"dom1.com", "dom2.com", .... "dom3.com"}

Next, we use this small file adblock.lua to tell the PowerDNS Recursor 4.x what to do:

adservers=newDS()

function preresolve(dq)
	if(not adservers:check(dq.qname)) then
		return false
	end
	
	if(dq.qtype == pdns.A) then
		dq:addAnswer(dq.qtype, "127.0.0.1")
	elseif(dq.qtype == pdns.AAAA) then
		dq:addAnswer(dq.qtype, "::1")
	end
	return true
end

adservers:add(dofile("blocklist.lua"))

Next add to recursor.conf:

lua-dns-script=adblock.lua

Now fire up the PowerDNS Recursor and access to the domain names from the Mozilla focus project will be replaced by a link to 127.0.0.1. Note: the actual blocking strategy used by Mozilla is a lot smarter, and includes knowledge of the website containing the ad!

Copy link

ghost commented Jan 19, 2016

You refer to the disconnect-advertising.json above the second code block. Do I open this file, delete the contents and add the contents of the second code block and save it with the same name. Or am I appending the contents of the second code block at the end of the existing contents of the disconnect-advertising.json file?

Also, am I creating the adblock.lua file in the ~/focus/Lists directory and filling the file with the contents of the third code block?

@Habbie
Copy link

Habbie commented Jan 19, 2016

The first two blocks are shell blocks, that you execute. adblock.lua goes in whatever dir you prefer, but if this is not /etc/powerdns, you need to make the 4th block (lua-dns-script=..) point to it.

@mdzidic
Copy link

mdzidic commented Feb 19, 2016

Hello,

I have problems when I try to load adblock.lua:

attempt to call global 'newDS' (a nil value)

@dmgeurts
Copy link

dmgeurts commented Nov 2, 2021

Hello,

I have problems when I try to load adblock.lua:

attempt to call global 'newDS' (a nil value)

Has anyone got a solution for this one?

@notr1ch
Copy link

notr1ch commented Jul 20, 2024

That error can happen if you tried to load the lua file using lua-config-file instead of lua-dns-script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment