Cleaning up wildcards, regex duplicates

A lot of us keep using wildcards, e.g. a configuration file in /etc/dnsmasq.d, containing something like address=/ligatus.com/0.0.0.0 (IPv4) or / and address=/ligatus.com/:: (IPv6). This is repeated for every wildcard domain you want to block.
The only limitation is that the file cannot be named 03-pihole-wildcard.conf because pihole -g calls /opt/pihole/wildcard_regex_converter.sh, witch converts all wildcards into regular expressions (regex.list). So I called it 03-wildcard.conf, conversion prevented.

Since pihole, as of version v4.0 doesn't use wildcards anymore (all converted), there is absolutely no logic to detect wildcards and regular expressions that do the same thing.

This script will advise you to remove obsolete wildcards. You need to change the name of your wildcard configuration file!

#!/bin/bash

basename="pihole"
workdir="/etc/${basename}"
regexfile=regex.list
dnsmasqdir=/etc/dnsmasq.d
wildcardfile=03-wildcard.conf

function cleanregex() {
for regex in  `cat $workdir/$regexfile`
	do
	if [[ $1 =~ $regex ]]; then
		echo "you should remove $1"
		break
	fi
done
}

for wildcard in  `cat $dnsmasqdir/$wildcardfile`
	do
	IFS='/' read -a address <<< "${wildcard}"
	cleanregex "${address[1]}"
done

sample result from my system:

The script can easily modified to do the deletions for you (using sed)

Run the script manually, with sudo, whenever you add something to your wildcard configuration.

Enjoy...

1 Like

I've been testing the dev version, prior to version 4.1, expecting it to be in v4.1, unfortunately, it wasn't. Big disappointment....

The new syntax has been added by Simon Kelley on my request.
From the changelog:

Implement --address=/example.com/# as (more efficient) syntactic
sugar for --address=/example.com/0.0.0.0 and --address=/example.com/::
Returning null addresses is a useful technique for ad-blocking.

The suggestion was made because it was already possible in v2.79 for NXDOMAIN.