A nice custom 404 page for pihole with an added automatic script that can be used to whitelist the domain with the click of a button.
Above: This is what your 404 page will look like! Of course, feel free to customize it.
Setting up a Pihole is awesome and pretty easy. But what happens when a page you want to go to is blocked? You get a regular 404 error. And what if you want to go to the webpage? First, you have to go to your pihole's website. Then you have to login. Next, you need to go to whitelist and type in the domain. This works if the user is the administrator, but what if you don't know the password or a client has problems?
Pihole-404 to the rescue! This simple script will add a button that lets you email the admin. This email will be read by an automatic python script and the domain will be whitelisted! And on top of all that, it makes the 404 page look very nice!
I used Email so it would be more secure, not just a command being executed by the website. Having a button to run any command on the pihole would be a vulnerability, so I decided email would be better.
To get your pihole up and running (haha), head over to https://github.com/pi-hole/pi-hole
If you are using an email provider other than Gmail, skip to step 4.
If you are using Gmail, you need to enable less secure app access to your google account here. You also need to enable IMAP, if it is not already enabled here. Scroll down to IMAP Access and make sure Status is set to Enable IMAP. I am not sure about other providers as I have not tried them.
Step 3: (Gmail users who use 2FA only) Add an App Password to Use Instead of your Google Account Password
If using Gmail and you have 2 Facter Authentication setup, do these steps. Otherwise, skip to step 4
- Go to https://myaccount.google.com/
- On the left side, select 'Security'
- Scroll down to the 'Signing in to Google' section and select 'App Passwords'
- You will be asked to sign in. Sign in.
- Once you have signed in again, press the menu 'Select an App' and then press 'Other'. Make the name something memorable such as 'Pihole'.
- Click 'GENERATE'. Copy the 16 character password for later.
On the Raspberry Pi, Clone this repository:
git clone https://github.com/perspector/Pihole-404.git
Navigate to the project folder:
cd Pihole-404
or wherever the repo folder was installed if you installed it in a different location.
Run the installer script (as root):
sudo python3 install.py
When you are asked for a password, enter your email password. If you use Google/Gmail and have 2 Facter Authentication set up, paste the app password from earlier (Step 3) here using Ctrl + Shift + V
Test the script here: http://doubleclick.net
You should get a 404 page like the screenshot above.
The script only works for http websites, not https/tls 😢.
When using SSH to access the Raspberry Pi, the email checker program may be stopped when exitting SSH.
If this happens, just log on to the Raspberry Pi through HDMI and a keyboard.
Then go to the folder you cloned this repo in using cd
Type:
watch -n 10 python3 EmailChecker.py &
This will start the program in the background.
If you did not setup run on boot in the installer script, when you shutdown or restart, you also must run this command.
If the Raspberry Pi updates, you may need to run the installer script again.
The installer script should fix this.
Note: This script sets blocking mode to IP blocking. If you want to change this, just edit /etc/pihole/pihole-FTL.conf and set blocking mode to something else (anything other than IP will disable the 404 page). This is explained in depth here
If you want to uninstall the programs and restore settings before the script was used, just use the following command:
sudo sh uninstall.sh
Please email me at bennypythoncoder@gmail.com to tell me why you chose to uninstall the programs.
You can always replace background.jpg with another file (must have same filename).
You can also replace foreground.png with another file (must also have the same filename).
The CustomBlockPage.php file can be changed to whatever you want (must also have the same filename).
- Everyone at pi-hole (Github)
- This Reddit post which was incredibly useful
- imap-tools python package, found here at pypi, used for processing emails
- Python3, a phonomenal programming language everyone should know
- The Raspberry Pi Foundation for making awesome computers
- Wallpaperflare for amazing images (Astronaut, Moon)
- Pixlr E for great online photo editing