diff --git a/README.md b/README.md index 9da58540..561e4f09 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ _If you wish to read over the script before running it, run `nano basic-install. ``` git clone --depth 1 https://github.com/pi-hole/pi-hole.git Pi-hole -cd Pi-hole/automated_installer/ +cd Pi-hole/automated\ install/ bash basic-install.sh ``` @@ -55,17 +55,11 @@ bash basic-install.sh Once installed, [configure your router to have **DHCP clients use the Pi as their DNS server**](https://discourse.pi-hole.net/t/how-do-i-configure-my-devices-to-use-pi-hole-as-their-dns-server/245) and then any device that connects to your network will have ads blocked without any further configuration. Alternatively, you can manually set each device to [use the Raspberry Pi as its DNS server](http://pi-hole.net/faq/how-do-i-use-the-pi-hole-as-my-dns-server/). -## Installing the Pi-hole (Click to Watch!) +## What is Pi-hole and how do I install it?

- +

-## Would you like to know more? - -**Watch the 60-second video below to get a quick overview** -

- -

## Get Help Or Connect With Us On The Web diff --git a/advanced/Scripts/chronometer.sh b/advanced/Scripts/chronometer.sh index a28bb868..93b0cbb1 100755 --- a/advanced/Scripts/chronometer.sh +++ b/advanced/Scripts/chronometer.sh @@ -17,56 +17,21 @@ gravity="/etc/pihole/gravity.list" . /etc/pihole/setupVars.conf -CalcBlockedDomains() { - if [ -e "${gravity}" ]; then - # if BOTH IPV4 and IPV6 are in use, then we need to divide total domains by 2. - if [[ -n "${IPV4_ADDRESS}" && -n "${IPV6_ADDRESS}" ]]; then - blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}') - else - # only one is set. - blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1}') - fi - else - blockedDomainsTotal="Err." - fi -} - -CalcQueriesToday() { - if [ -e "${piLog}" ]; then - queriesToday=$(awk '/query\[/ {print $6}' < "${piLog}" | wc -l) - else - queriesToday="Err." - fi -} - -CalcblockedToday() { - if [ -e "${piLog}" ] && [ -e "${gravity}" ];then - blockedToday=$(awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' < "${piLog}" | wc -l) - else - blockedToday="Err." - fi -} - -CalcPercentBlockedToday() { - if [ "${queriesToday}" != "Err." ] && [ "${blockedToday}" != "Err." ]; then - if [ "${queriesToday}" != 0 ]; then #Fixes divide by zero error :) - #scale 2 rounds the number down, so we'll do scale 4 and then trim the last 2 zeros - percentBlockedToday=$(echo "scale=4; ${blockedToday}/${queriesToday}*100" | bc) - percentBlockedToday=$(sed 's/.\{2\}$//' <<< "${percentBlockedToday}") - else - percentBlockedToday=0 - fi - fi +# Borrowed/modified from https://gist.github.com/cjus/1047794 +function GetJSONValue { + retVal=$(echo $1 | sed 's/\\\\\//\//g' | \ + sed 's/[{}]//g' | \ + awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | \ + sed 's/\"\:\"/\|/g' | \ + sed 's/[\,]/ /g' | \ + sed 's/\"//g' | \ + grep -w $2) + echo ${retVal##*|} } outputJSON() { - CalcQueriesToday - CalcblockedToday - CalcPercentBlockedToday - - CalcBlockedDomains - - printf '{"domains_being_blocked":"%s","dns_queries_today":"%s","ads_blocked_today":"%s","ads_percentage_today":"%s"}\n' "$blockedDomainsTotal" "$queriesToday" "$blockedToday" "$percentBlockedToday" + json=$(curl -s -X GET http://127.0.0.1/admin/api.php?summaryRaw) + echo ${json} } normalChrono() { @@ -87,22 +52,17 @@ normalChrono() { # Uncomment to continually read the log file and display the current domain being blocked #tail -f /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ {if ($7 != "address" && $7 != "name" && $7 != "/etc/pihole/gravity.list") print $7; else;}' - #uncomment next 4 lines to use original query count calculation - #today=$(date "+%b %e") - #todaysQueryCount=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ {print $7}' | wc -l) - #todaysQueryCountV4=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ && /\[A\]/ {print $7}' | wc -l) - #todaysQueryCountV6=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ && /\[AAAA\]/ {print $7}' | wc -l) + json=$(curl -s -X GET http://127.0.0.1/admin/api.php?summaryRaw) + domains=$(printf "%'.f" $(GetJSONValue ${json} "domains_being_blocked")) #add commas in + queries=$(printf "%'.f" $(GetJSONValue ${json} "dns_queries_today")) + blocked=$(printf "%'.f" $(GetJSONValue ${json} "ads_blocked_today")) + LC_NUMERIC=C percentage=$(printf "%0.2f\n" $(GetJSONValue ${json} "ads_percentage_today")) #2 decimal places - CalcQueriesToday - CalcblockedToday - CalcPercentBlockedToday + echo "Blocking: ${domains}" + echo "Queries: ${queries}" - CalcBlockedDomains - - echo "Blocking: ${blockedDomainsTotal}" - echo "Queries: ${queriesToday}" #same total calculation as dashboard - echo "Pi-holed: ${blockedToday} (${percentBlockedToday}%)" + echo "Pi-holed: ${blocked} (${percentage}%)" sleep 5 done diff --git a/advanced/Scripts/list.sh b/advanced/Scripts/list.sh index bb3a8a43..c916e021 100755 --- a/advanced/Scripts/list.sh +++ b/advanced/Scripts/list.sh @@ -15,6 +15,7 @@ basename=pihole piholeDir=/etc/${basename} whitelist=${piholeDir}/whitelist.txt blacklist=${piholeDir}/blacklist.txt +readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf" reload=false addmode=true verbose=true @@ -47,13 +48,17 @@ helpFunc() { ::: -h, --help Show this help dialog ::: -l, --list Display your ${word}listed domains EOM +if [[ "${letter}" == "b" ]]; then + echo "::: -wild, --wildcard Add whitecard entry (only blacklist)" +fi exit 0 } EscapeRegexp() { # This way we may safely insert an arbitrary # string in our regular expressions - echo $* | sed "s/[]\\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g" + # Also remove leading "." if present + echo $* | sed 's/^\.*//' | sed "s/[]\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g" } HandleOther(){ @@ -61,7 +66,7 @@ HandleOther(){ domain=$(sed -e "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" <<< "$1") #check validity of domain - validDomain=$(perl -ne "print if /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/" <<< "$domain") + validDomain=$(echo "${domain}" | perl -lne 'print if /^(?!.*[^a-z0-9-\.].*)\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)*[a-z]{2,63}\b/') if [ -z "${validDomain}" ]; then echo "::: $1 is not a valid argument or domain name" else @@ -89,22 +94,51 @@ AddDomain() { list="$2" domain=$(EscapeRegexp "$1") - bool=true - #Is the domain in the list we want to add it to? - grep -Ex -q "${domain}" ${list} > /dev/null 2>&1 || bool=false + if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then - if [[ "${bool}" == false ]]; then - #domain not found in the whitelist file, add it! - if [[ "${verbose}" == true ]]; then - echo "::: Adding $1 to $list..." - fi - reload=true - # Add it to the list we want to add it to - echo "$1" >> ${list} - else - if [[ "${verbose}" == true ]]; then - echo "::: ${1} already exists in ${list}, no need to add!" - fi + bool=true + #Is the domain in the list we want to add it to? + grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false + + if [[ "${bool}" == false ]]; then + #domain not found in the whitelist file, add it! + if [[ "${verbose}" == true ]]; then + echo "::: Adding $1 to $list..." + fi + reload=true + # Add it to the list we want to add it to + echo "$1" >> "${list}" + else + if [[ "${verbose}" == true ]]; then + echo "::: ${1} already exists in ${list}, no need to add!" + fi + fi + + elif [[ "${list}" == "${wildcardlist}" ]]; then + + source "${piholeDir}/setupVars.conf" + #Remove the /* from the end of the IPv4addr. + IPV4_ADDRESS=${IPV4_ADDRESS%/*} + IPV6_ADDRESS=${IPV6_ADDRESS} + + bool=true + #Is the domain in the list? + grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false + + if [[ "${bool}" == false ]]; then + if [[ "${verbose}" == true ]]; then + echo "::: Adding $1 to wildcard blacklist..." + fi + reload=true + echo "address=/$1/${IPV4_ADDRESS}" >> "${wildcardlist}" + if [[ ${#IPV6_ADDRESS} > 0 ]] ; then + echo "address=/$1/${IPV6_ADDRESS}" >> "${wildcardlist}" + fi + else + if [[ "${verbose}" == true ]]; then + echo "::: ${1} already exists in wildcard blacklist, no need to add!" + fi + fi fi } @@ -112,18 +146,38 @@ RemoveDomain() { list="$2" domain=$(EscapeRegexp "$1") - bool=true - #Is it in the list? Logic follows that if its whitelisted it should not be blacklisted and vice versa - grep -Ex -q "${domain}" ${list} > /dev/null 2>&1 || bool=false - if [[ "${bool}" == true ]]; then - # Remove it from the other one - echo "::: Removing $1 from $list..." - # /I flag: search case-insensitive - sed -i "/${domain}/Id" ${list} - reload=true - else - if [[ "${verbose}" == true ]]; then - echo "::: ${1} does not exist in ${list}, no need to remove!" + if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then + + bool=true + #Is it in the list? Logic follows that if its whitelisted it should not be blacklisted and vice versa + grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false + if [[ "${bool}" == true ]]; then + # Remove it from the other one + echo "::: Removing $1 from $list..." + # /I flag: search case-insensitive + sed -i "/${domain}/Id" "${list}" + reload=true + else + if [[ "${verbose}" == true ]]; then + echo "::: ${1} does not exist in ${list}, no need to remove!" + fi + fi + + elif [[ "${list}" == "${wildcardlist}" ]]; then + + bool=true + #Is it in the list? + grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false + if [[ "${bool}" == true ]]; then + # Remove it from the other one + echo "::: Removing $1 from $list..." + # /I flag: search case-insensitive + sed -i "/address=\/${domain}/Id" "${list}" + reload=true + else + if [[ "${verbose}" == true ]]; then + echo "::: ${1} does not exist in ${list}, no need to remove!" + fi fi fi } @@ -153,6 +207,7 @@ for var in "$@"; do case "${var}" in "-w" | "whitelist" ) listMain="${whitelist}"; listAlt="${blacklist}";; "-b" | "blacklist" ) listMain="${blacklist}"; listAlt="${whitelist}";; + "-wild" | "wildcard" ) listMain="${wildcardlist}";; "-nr"| "--noreload" ) reload=false;; "-d" | "--delmode" ) addmode=false;; "-f" | "--force" ) force=true;; diff --git a/advanced/Scripts/piholeDebug.sh b/advanced/Scripts/piholeDebug.sh index d0e60177..f763d2b8 100755 --- a/advanced/Scripts/piholeDebug.sh +++ b/advanced/Scripts/piholeDebug.sh @@ -122,6 +122,13 @@ version_check() { && log_echo -r "${light_ver}" || (log_echo "lighttpd not installed." && error_found=1) local php_ver="$(php -v |& head -n1)" \ && log_echo -r "${php_ver}" || (log_echo "PHP not installed." && error_found=1) + + (local pi_hole_branch="$(cd /etc/.pihole/ && git rev-parse --abbrev-ref HEAD)" && log_echo -r "Pi-hole branch: ${pi_hole_branch}") || log_echo "Unable to obtain Pi-hole branch" + (local pi_hole_rev="$(cd /etc/.pihole/ && git describe --long --dirty --tags)" && log_echo -r "Pi-hole rev: ${pi_hole_rev}") || log_echo "Unable to obtain Pi-hole revision" + + (local admin_branch="$(cd /var/www/html/admin && git rev-parse --abbrev-ref HEAD)" && log_echo -r "AdminLTE branch: ${admin_branch}") || log_echo "Unable to obtain AdminLTE branch" + (local admin_rev="$(cd /var/www/html/admin && git describe --long --dirty --tags)" && log_echo -r "AdminLTE rev: ${admin_rev}") || log_echo "Unable to obtain AdminLTE revision" + return "${error_found}" } @@ -354,10 +361,21 @@ files_check "${ADLISTFILE}" header_write "Analyzing gravity.list" - gravity_length=$(wc -l "${GRAVITYFILE}") \ + gravity_length=$(grep -c ^ "${GRAVITYFILE}") \ && log_write "${GRAVITYFILE} is ${gravity_length} lines long." \ || log_echo "Warning: No gravity.list file found!" +header_write "Analyzing pihole.log" + + pihole_length=$(grep -c ^ "${PIHOLELOG}") \ + && log_write "${PIHOLELOG} is ${pihole_length} lines long." \ + || log_echo "Warning: No pihole.log file found!" + + pihole_size=$(du -h "${PIHOLELOG}" | awk '{ print $1 }') \ + && log_write "${PIHOLELOG} is ${pihole_size}." \ + || log_echo "Warning: No pihole.log file found!" + + # Continuously append the pihole.log file to the pihole_debug.log file dumpPiHoleLog() { trap '{ echo -e "\n::: Finishing debug write from interrupt... Quitting!" ; exit 1; }' INT diff --git a/advanced/Scripts/piholeLogFlush.sh b/advanced/Scripts/piholeLogFlush.sh index 8b16e3d5..d8f4fd79 100755 --- a/advanced/Scripts/piholeLogFlush.sh +++ b/advanced/Scripts/piholeLogFlush.sh @@ -11,5 +11,10 @@ # (at your option) any later version. echo -n "::: Flushing /var/log/pihole.log ..." -echo " " > /var/log/pihole.log +# Test if logrotate is available on this system +if command -v /usr/sbin/logrotate &> /dev/null; then + /usr/sbin/logrotate --force /etc/pihole/logrotate +else + echo " " > /var/log/pihole.log +fi echo "... done!" diff --git a/advanced/Scripts/webpage.sh b/advanced/Scripts/webpage.sh index acf8eca9..02610d85 100755 --- a/advanced/Scripts/webpage.sh +++ b/advanced/Scripts/webpage.sh @@ -92,12 +92,17 @@ SetWebPassword(){ ProcessDNSSettings() { source "${setupVars}" - delete_dnsmasq_setting "server=" - add_dnsmasq_setting "server" "${PIHOLE_DNS_1}" + delete_dnsmasq_setting "server" - if [[ "${PIHOLE_DNS_2}" != "" ]]; then - add_dnsmasq_setting "server" "${PIHOLE_DNS_2}" - fi + COUNTER=1 + while [[ 1 ]]; do + var=PIHOLE_DNS_${COUNTER} + if [ -z "${!var}" ]; then + break; + fi + add_dnsmasq_setting "server" "${!var}" + let COUNTER=COUNTER+1 + done delete_dnsmasq_setting "domain-needed" @@ -111,31 +116,45 @@ ProcessDNSSettings() { add_dnsmasq_setting "bogus-priv" fi + delete_dnsmasq_setting "dnssec" + delete_dnsmasq_setting "trust-anchor=" + + if [[ "${DNSSEC}" == true ]]; then + echo "dnssec +trust-anchor=.,19036,8,2,49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5 +" >> "${dnsmasqconfig}" + fi + } SetDNSServers(){ # Save setting to file - change_setting "PIHOLE_DNS_1" "${args[2]}" + delete_setting "PIHOLE_DNS" + IFS=',' read -r -a array <<< "${args[2]}" + for index in "${!array[@]}" + do + add_setting "PIHOLE_DNS_$((index+1))" "${array[index]}" + done - if [[ "${args[3]}" != "none" ]]; then - change_setting "PIHOLE_DNS_2" "${args[3]}" - else - change_setting "PIHOLE_DNS_2" "" - fi - - if [[ "${args[4]}" == "domain-needed" ]]; then + if [[ "${args[3]}" == "domain-needed" ]]; then change_setting "DNS_FQDN_REQUIRED" "true" else change_setting "DNS_FQDN_REQUIRED" "false" fi - if [[ "${args[4]}" == "bogus-priv" || "${args[5]}" == "bogus-priv" ]]; then + if [[ "${args[4]}" == "bogus-priv" ]]; then change_setting "DNS_BOGUS_PRIV" "true" else change_setting "DNS_BOGUS_PRIV" "false" fi + if [[ "${args[5]}" == "dnssec" ]]; then + change_setting "DNSSEC" "true" + else + change_setting "DNSSEC" "false" + fi + ProcessDNSSettings # Restart dnsmasq to load new configuration @@ -213,10 +232,13 @@ dhcp-authoritative dhcp-range=${DHCP_START},${DHCP_END},${leasetime} dhcp-option=option:router,${DHCP_ROUTER} dhcp-leasefile=/etc/pihole/dhcp.leases -domain=${PIHOLE_DOMAIN} #quiet-dhcp " > "${dhcpconfig}" +if [[ "${PIHOLE_DOMAIN}" != "none" ]]; then + echo "domain=${PIHOLE_DOMAIN}" >> "${dhcpconfig}" +fi + if [[ "${DHCP_IPv6}" == "true" ]]; then echo "#quiet-dhcp6 #enable-ra @@ -227,7 +249,7 @@ ra-param=*,0,0 fi else - rm "${dhcpconfig}" + rm "${dhcpconfig}" &> /dev/null fi } diff --git a/advanced/index.php b/advanced/index.php index 1434025a..c076f92d 100644 --- a/advanced/index.php +++ b/advanced/index.php @@ -74,7 +74,9 @@ if($uri == "/") Whitelist this page Close window -