Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 101 lines (89 sloc) 2.8 KB
#!/bin/sh
#
cd $(dirname $0); top=$(pwd); cd config
os=$(uname)
tmp=/tmp/network$$
cleanup() {
rm -f $tmp
}
trap cleanup EXIT
case $os in
Linux)
dev="$(ip route get 8.8.8.8 | head -1 | awk '{print $5}')"
IP_LOOKUP="$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')" # May not work for VPN / tun0
IPv6_LOOKUP="$(ip -6 route get 2001:4860:4860::8888 | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')" # May not work for VPN / tun0
nmcli dev show $dev | grep -i dns > $tmp
DNS1=$(grep 'IP4.DNS.1.' $tmp | awk '{print $2}')
DNS2=$(grep 'IP4.DNS.2.' $tmp | awk '{print $2}')
;;
Darwin)
EXTRA_RUN_ARGS="-h $(scutil --get LocalHostName)"
interface=$(route get 8.8.8.8 | grep interface | awk '{print $2}')
IP_LOOKUP=$(ifconfig $interface | grep 'inet ' | awk '{print $2}')
IPv6_LOOKUP=$(ifconfig $interface | grep 'inet6 ' |
awk '{print $2}' | sed 's/%.*//')
scutil --dns > $tmp
DNS1=$(grep 'nameserver.0.' $tmp | awk '{print $3}' | head -1)
DNS2=$(grep 'nameserver.1.' $tmp | awk '{print $3}' | head -1)
;;
esac
IP="${IP:-$IP_LOOKUP}" # use $IP, if set, otherwise IP_LOOKUP
IPv6="${IPv6:-$IPv6_LOOKUP}" # use $IPv6, if set, otherwise IP_LOOKUP
DOCKER_CONFIGS="$(pwd)" # Default of directory you run this from, update to where ever.
DNS1="${DNS1:-9.9.9.9}" # Fall back to Quad9
echo "### Make sure your IPs are correct, hard code ServerIP ENV VARs if necessary"
echo "IP: ${IP}"
echo "IPv6: ${IPv6}"
echo "DNS servers: ${DNS1} ${DNS2}"
# If PIHOLE_DNS environment is not set, try using existing DNS config.
#
if [ $# -gt 0 ]; then
PIHOLE_DNS="$@"
else
PIHOLE_DNS="${DNS1} ${DNS2}"
fi
D_DNS="--dns 127.0.0.1"
PIHOLE_DNS_ARGS=""
n=1
for i in $PIHOLE_DNS
do
PIHOLE_DNS_ARGS="$PIHOLE_DNS_ARGS -e DNS${n}=$i"
D_DNS="$D_DNS --dns $i"
(( n=n+1 ))
done
# Uncomment this to get DHCP in pihole
# DHCP="-p 67:67/udp"
docker run -d \
--name pihole \
${EXTRA_RUN_ARGS} \
-p 53:53/tcp -p 53:53/udp \
$DHCP \
-p 80:80 \
-p 443:443 \
-v "${DOCKER_CONFIGS}/pihole/:/etc/pihole/" \
-v "${DOCKER_CONFIGS}/dnsmasq.d/:/etc/dnsmasq.d/" \
-v $top/lists:/tmp/run_config \
-e ServerIP="${IP}" \
-e ServerIPv6="${IPv6}" \
$PIHOLE_DNS_ARGS \
--restart=unless-stopped \
--cap-add=NET_ADMIN \
$D_DNS \
pihole/pihole:latest
# echo -n "Your password for https://${IP}/admin/ is "
# docker logs pihole 2> /dev/null | grep 'password'
echo ""
echo "Pihole is coming up..."
echo ""
status=$(docker exec pihole pihole status 2>&1 | grep blocking)
while [ -z "$status" ]
do
status=$(docker exec pihole pihole status 2>&1 | grep blocking)
echo "Waiting for pihole..."
sleep 1
done
echo "$status"
echo ""
echo "pihole is ready. Running our configuration script."
echo ""
docker exec pihole /tmp/run_config