I configured my raspberry pi as a wireless repeater (using hostapd and dnsmasq).
Now I wonder how I could use it to block known ad-servers?
What would be the best way? Using iptables? Or setting up an dns-server, which blocks the evil adresses?
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It only takes a minute to sign up.
Sign up to join this communityIf you want a more robust setup, you can try pi-hole. It is an open source software package made to block ads network wide, specifically for a Raspberry Pi. Although it can also run on other platforms. It comes with a management interface and easy updating of the used lists.
I've found a solution and created a script to share it. This script will download a list of known domains and block them by using dnsmasq
. Those lists will be updated daily.
Run ./adblocker.sh --install|--start|--stop
to install, start or stop it.
The script:
#!/bin/bash
installAB () {
if [[ "$(crontab -l | grep adblocker.sh)" =~ adblocker.sh ]]; then
echo "adblocker is already installed"
echo exit
exit 1
fi
crontab -l > mycron
echo '@daily SHELL=/bin/bash ~/adblocker.sh --update' >> mycron
crontab mycron
rm mycron
backupFile="/etc/dnsmasq.conf.bak-$(date +%d%m%y%H%M)"
sudo mv /etc/dnsmasq.conf "$backupFile"
echo "conf-file=/etc/dnsmasq.domains.conf
addn-hosts=/etc/dnsmasq.hostnames.conf
$(egrep -v '^#|^s*$' $backupFile)" | sudo tee /etc/dnsmasq.conf > /dev/null
}
updateAB () {
wget https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt
wgetreturn=$?
sudo mv domains.txt /etc/dnsmasq.domains.conf
sudo mv hostnames.txt /etc/dnsmasq.hostnames.conf
if [ $wgetreturn -eq 0 ]; then
echo "successfully updated: $(date)" >> /home/pi/update_adblocker.log
else
echo "downloading domains and hostnames failed: $(date)" >> /home/pi/update_adblocker.log
fi
}
activateAB () {
sudo sed -i '/dnsmasq.domains.conf\|dnsmasq.hostnames.conf/s/^#*//g' /etc/dnsmasq.conf
echo "adblocker activated"
}
deactivateAB () {
sudo sed -i '/dnsmasq.domains.conf\|dnsmasq.hostnames.conf/s/^#*/#/g' /etc/dnsmasq.conf
echo "adblocker deactivated"
}
case "$1" in
install|--install)
updateAB
installAB
;;
start|--start)
activateAB
;;
stop|--stop)
deactivateAB
;;
update|--update)
updateAB
;;
*)
echo "run the script with »--install«, »--start«, »--stop« or »--update«"
echo "exit"
exit 1
;;
esac
sudo service dnsmasq restart && echo "service restarted"