perspector / Pihole-404

A nice 404 page for pihole with an added automatic script that can be used to whitelist the domain with the click of a button.
MIT License
11 stars 3 forks source link

The default blocking mode is NULL. #1

Closed dschaper closed 3 years ago

dschaper commented 3 years ago

https://github.com/BennyThePythonCoder/Pihole-404/blob/c04f53f8c183fcbd7908f27eae1728cc71606e24/install.sh#L53

That sed line won't do anything for a default install of Pi-hole. The pihole-FTL.conf file has no blocking mode config line so there's nothing for sed to modify. You'll never actually enable IP blocking mode with this script on a default Pi-hole install.

perspector commented 3 years ago

Thanks! I changed it from sed -i 's/NULL/IP/gi' /etc/pihole/pihole-FTL.conf to sed -i 's/BLOCKINGMODE=/BLOCKINGMODE=IP/gi' /etc/pihole/pihole-FTL.conf I did not realize that NULL was not default, I read a post on Reddit that said it was default. Now I know better.

perspector commented 3 years ago

I will close this issue since it has been resolved.

dschaper commented 3 years ago

It's not resolved.

What I am saying is that the /etc/pihole/pihole-FTL.conf file has no contents by default. It's empty if it even exists. There's a good chance that it doesn't. https://docs.pi-hole.net/ftldns/configfile/

pihole-FTL runs with it's default settings when there is no config file and the default mode is NULL.

https://docs.pi-hole.net/ftldns/blockingmode/

Your sed lines will not enable IP blocking mode for the default installation. Running your install script will not make the block page work.

dan@raspberrypi:~ $ cat /etc/pihole/pihole-FTL.conf
PRIVACYLEVEL=0
dan@raspberrypi:~ $ sed -i 's/BLOCKINGMODE=/BLOCKINGMODE=IP/gi' /etc/pihole/pihole-FTL.conf
[sudo] password for dan:
dan@raspberrypi:~ $ cat /etc/pihole/pihole-FTL.conf
PRIVACYLEVEL=0
perspector commented 3 years ago

Oh sorry. I updated the file. Thank you!

dschaper commented 3 years ago

https://github.com/BennyThePythonCoder/Pihole-404/commit/0d39413120ffa398af58e8e3e7d1146170f4c812#r50267766

perspector commented 3 years ago

Thank you for all your support. I think I (finally) figured it out! I just used echo and piped to tee: echo "BLOCKINGMODE=IP" | sudo tee -a /etc/pihole/pihole-FTL.conf

dschaper commented 3 years ago

Almost!

That change will duplicate BLOCKINGMODE if it already exists and that will break things.

What you need are three conditions:

  1. File doesn't exit.
  2. File exists without a BLOCKINGMODE line
  3. FIle exists with a BLOCKINGMODE line that isn't IP.
perspector commented 3 years ago

Thanks for your patience! This should Actually work:

FILE=/etc/pihole/pihole-FTL.conf

if [ -f "$FILE" ]; then
    # File exists
    if grep -q "BLOCKINGMODE" $FILE
    then
      # If BLOCKINGMODE line is present
      if grep -q "=IP" $FILE
      then
        # If BLOCKINGMODE is set to IP
        continue
      else
        # IF BLOCKINGMODE is present but not set to IP
        sed -i 's/.*BLOCKINGMODE.*/BLOCKINGMODE=IP/' $FILE
      fi
    else
      # If BLOCKINGMODE line is not present
      echo "BLOCKINGMODE=IP" | sudo tee -a $FILE
    fi
else 
    # File does not exist
    touch $FILE
    echo "BLOCKINGMODE=IP" | sudo tee -a $FILE
fi

There is probably a simpler way, but I think this should work. A bunch of nested if then else statements.

perspector commented 3 years ago

I tested the code, and it works. I'm closing this issue since it has (now actually) been resolved. Thank you so much for your help!