3

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?

3

If 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 did not want to use it, because I was afraid it would mess up my configuration. But maybe I will give it a try. Thanks! – jake Oct 5 '18 at 10:21
2

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"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.