I wanted to create a Pi-hole that I could use outside of home and share with friends and family. I did not, however, want to share it with the entire world. Since Pi-hole comes with an admin interface (if you choose to install), you helpfully have a webserver and php
available.
What I did was to block everything with iptables, create a bash script that is called via php to add the visitng ip address to the port 53 allow udp/tcp in iptables.
The PHP script:
<?php
#/var/www/html/setdnsip.php
$ip = $_SERVER['REMOTE_ADDR'];
if($_GET['secret'] === "supersecret"){
echo shell_exec('whoami');
echo "adding ip " . $ip . " tcp/udp port 53 to iptables";
echo "<br>";
$m=shell_exec("sudo /home/saturn/setdnsipallow.sh " . $ip);
echo $m;
}else{
echo "404 not found";
}
?>
I added a $_GET
check in there just in case. I call this file setdnsip.php
and added it to /var/www/html/
.
In /home/saturn/
I have the aforementioned shell script: setdnsallowip.sh
:
#!/bin/sh
#/home/saturn/setdnsallowip.sh
IPTOADD=$1
TIME=$(date)
if [ $IPTOADD ]; then
printf $IPTOADD; echo "added: " $IPTOADD " on " $TIME " by " $USER"/"$(whoami) >> /home/saturn/ipadd.log;
iptables -A INPUT -s $IPTOADD -i eth0 -p udp --destination-port 53 -j ACCEPT;
iptables -A INPUT -s $IPTOADD -i eth0 -p tcp --destination-port 53 -j ACCEPT;
iptables-save > /etc/iptables/rules.v4
else
echo "no var sent"
fi
The default policy for the INPUT
chain is DROP
iptables --policy INPUT DROP
In order to allow the owner of the lighttpd
process to execute the script, you have to add permission: create a file in /etc/sudoers.d/
like setdns
with the following:
#/etc/sudoers/setdns
www-data ALL=NOPASSWD: /home/saturn/setdnsallowip.sh
and your script will be allowed to run.
This is all it takes for a simple setup that you can use and share among friends/family. Just visit http://yourpiholeip/setdnsip.php?secret=supersecret
and the ip address you are on will be added to the port 53 allow list so any device there can use your Pi-hole.
Any suggestions for improvements/upgrades welcomed.
This makes for a nice simple ad blocker that can be set up for a few bucks a month on something like Digital Ocean or Rackspace or something that you can share with friends or family. It also makes it easy to update when your ISP changes your ip address.