2016-08-27 00:10:22 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
2017-02-22 18:55:20 +01:00
|
|
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
#
|
2016-08-27 00:10:22 +02:00
|
|
|
# Controller for all pihole scripts and functions.
|
|
|
|
#
|
2017-02-22 18:55:20 +01:00
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
colfile="/opt/pihole/COL_TABLE"
|
|
|
|
source ${colfile}
|
|
|
|
|
2017-03-08 13:16:40 +01:00
|
|
|
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
|
2017-01-02 14:27:13 +01:00
|
|
|
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
|
2017-05-14 03:11:44 +02:00
|
|
|
|
2016-08-27 00:10:22 +02:00
|
|
|
# Must be root to use this tool
|
|
|
|
if [[ ! $EUID -eq 0 ]];then
|
2017-05-14 03:11:44 +02:00
|
|
|
if [[ -x "$(command -v sudo)" ]]; then
|
2016-11-02 17:36:30 +01:00
|
|
|
exec sudo bash "$0" "$@"
|
|
|
|
exit $?
|
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${CROSS} sudo is needed to run pihole commands. Please run this script as root or install sudo."
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
2016-08-27 00:10:22 +02:00
|
|
|
fi
|
|
|
|
|
2016-11-16 21:34:43 +01:00
|
|
|
webpageFunc() {
|
2016-12-28 17:25:14 +01:00
|
|
|
source /opt/pihole/webpage.sh
|
|
|
|
main "$@"
|
2016-11-16 21:34:43 +01:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
whitelistFunc() {
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
blacklistFunc() {
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 13:49:04 +01:00
|
|
|
wildcardFunc() {
|
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
debugFunc() {
|
2017-02-27 00:36:53 +01:00
|
|
|
local automated
|
2017-02-27 20:03:57 +01:00
|
|
|
local web
|
|
|
|
|
|
|
|
# Pull off the `debug` leaving passed call augmentation flags in $1
|
2017-02-27 00:36:53 +01:00
|
|
|
shift
|
2017-02-27 20:40:20 +01:00
|
|
|
if [[ "$@" == *"-a"* ]]; then
|
2017-02-27 00:36:53 +01:00
|
|
|
automated="true"
|
|
|
|
fi
|
2017-02-27 20:40:20 +01:00
|
|
|
if [[ "$@" == *"-w"* ]]; then
|
2017-02-27 20:03:57 +01:00
|
|
|
web="true"
|
|
|
|
fi
|
|
|
|
|
|
|
|
AUTOMATED=${automated:-} WEBCALL=${web:-} "${PI_HOLE_SCRIPT_DIR}"/piholeDebug.sh
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
flushFunc() {
|
2017-05-17 12:44:35 +02:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/piholeLogFlush.sh "$@"
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
updatePiholeFunc() {
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/update.sh
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
reconfigurePiholeFunc() {
|
2016-11-02 17:36:30 +01:00
|
|
|
/etc/.pihole/automated\ install/basic-install.sh --reconfigure
|
|
|
|
exit 0;
|
2016-10-15 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
updateGravityFunc() {
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/gravity.sh "$@"
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 22:42:44 +02:00
|
|
|
scanList(){
|
2016-12-06 13:18:01 +01:00
|
|
|
domain="${1}"
|
|
|
|
list="${2}"
|
|
|
|
method="${3}"
|
2017-07-15 12:56:40 +02:00
|
|
|
|
|
|
|
# Switch folder, preventing grep from printing file path
|
|
|
|
cd "/etc/pihole" || return 1
|
|
|
|
|
|
|
|
if [[ -n "${method}" ]]; then
|
|
|
|
grep -i -E -l "(^|\s|\/)${domain}($|\s|\/)" ${list} /dev/null 2> /dev/null
|
2016-12-06 13:18:01 +01:00
|
|
|
else
|
2017-07-15 12:56:40 +02:00
|
|
|
grep -i "${domain}" ${list} /dev/null 2> /dev/null
|
2016-12-06 13:18:01 +01:00
|
|
|
fi
|
2016-12-06 09:55:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-27 22:42:44 +02:00
|
|
|
processWildcards() {
|
|
|
|
IFS="." read -r -a array <<< "${1}"
|
|
|
|
for (( i=${#array[@]}-1; i>=0; i-- )); do
|
|
|
|
ar=""
|
|
|
|
for (( j=${#array[@]}-1; j>${#array[@]}-i-2; j-- )); do
|
|
|
|
if [[ $j == $((${#array[@]}-1)) ]]; then
|
|
|
|
ar="${array[$j]}"
|
|
|
|
else
|
|
|
|
ar="${array[$j]}.${ar}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "${ar}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
queryFunc() {
|
2017-07-15 12:56:40 +02:00
|
|
|
options="$*"
|
|
|
|
options="${options/-q /}"
|
|
|
|
|
|
|
|
if [[ "${options}" == "-h" ]] || [[ "${options}" == "--help" ]]; then
|
|
|
|
echo "Usage: pihole -q [option] <domain>
|
|
|
|
Example: 'pihole -q -exact domain.com'
|
|
|
|
Query the adlists for a specified domain
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-adlist Print the name of the block list URL
|
|
|
|
-exact Search the block lists for exact domain matches
|
|
|
|
-all Return all query matches within a block list
|
|
|
|
-h, --help Show this help dialog"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${options}" == *"-exact"* ]]; then
|
|
|
|
method="exact"
|
|
|
|
exact=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${options}" == *"-adlist"* ]]; then
|
|
|
|
adlist=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${options}" == *"-bp"* ]]; then
|
|
|
|
method="exact"
|
|
|
|
blockpage=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${options}" == *"-all"* ]]; then
|
|
|
|
all=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Strip valid options, leaving only the domain and invalid options
|
|
|
|
options=$(sed 's/ \?-\(exact\|adlist\|bp\|all\) \?//g' <<< "$options")
|
|
|
|
|
|
|
|
# Handle errors
|
|
|
|
if [[ "${options}" == *" "* ]]; then
|
|
|
|
error=true
|
|
|
|
str="Unknown option specified"
|
|
|
|
elif [[ "${options}" == "-q" ]]; then
|
|
|
|
error=true
|
|
|
|
str="No domain specified"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${error}" ]]; then
|
|
|
|
echo -e " ${COL_LIGHT_RED}${str}${COL_NC}
|
|
|
|
Try 'pihole -q --help' for more information."
|
2017-06-21 13:49:05 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2017-07-15 12:56:40 +02:00
|
|
|
|
|
|
|
# If domain contains non ASCII characters, convert domain to punycode if python is available
|
|
|
|
# Cr: https://serverfault.com/a/335079
|
|
|
|
if [[ "$options" = *[![:ascii:]]* ]]; then
|
|
|
|
if command -v python &> /dev/null; then
|
|
|
|
query=$(python -c 'import sys;print sys.argv[1].decode("utf-8").encode("idna")' "${options}")
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
query="${options}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Scan Whitelist and Blacklist
|
|
|
|
lists="whitelist.txt blacklist.txt"
|
|
|
|
results=($(scanList "${query}" "${lists}" "${method}"))
|
|
|
|
|
|
|
|
if [[ -n "${results[*]}" ]]; then
|
|
|
|
# Loop through each scanList line to print appropriate title
|
|
|
|
for result in "${results[@]}"; do
|
|
|
|
filename="${result/:*/}"
|
|
|
|
if [[ -n "$exact" ]]; then
|
|
|
|
printf " Exact result in %s\n" "${filename}"
|
|
|
|
elif [[ -n "$blockpage" ]]; then
|
|
|
|
printf " [i] %s\n" "${filename}"
|
|
|
|
else
|
|
|
|
domain="${result/*:/}"
|
|
|
|
if [[ ! "${filename}" == "${filename_prev:-}" ]]; then
|
|
|
|
printf " Result from %s\n" "${filename}"
|
|
|
|
fi
|
|
|
|
printf " %s\n" "${domain}"
|
|
|
|
filename_prev="${filename}"
|
2017-05-27 22:42:44 +02:00
|
|
|
fi
|
2017-07-15 12:56:40 +02:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Scan Wildcards
|
|
|
|
if [[ -e "${wildcardlist}" ]]; then
|
|
|
|
wildcards=($(processWildcards "${query}"))
|
|
|
|
|
|
|
|
for match in "${wildcards[@]}"; do
|
|
|
|
results=($(scanList "\/${match}\/" ${wildcardlist}))
|
|
|
|
|
|
|
|
if [[ -n "${results[*]}" ]]; then
|
|
|
|
# Remove empty lines before couting number of results
|
|
|
|
count=$(sed '/^\s*$/d' <<< "${results[@]}" | wc -l)
|
|
|
|
if [[ "${count}" -ge 0 ]]; then
|
|
|
|
blResult=true
|
|
|
|
if [[ -z "${blockpage}" ]]; then
|
|
|
|
printf " Wildcard result in %s\n" "${wildcardlist/*dnsmasq.d\/}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${blockpage}" ]]; then
|
|
|
|
echo " ${INFO} ${match}"
|
|
|
|
else
|
|
|
|
echo " *.${match}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
[[ -n "${blResult}" ]] && [[ -n "${blockpage}" ]] && exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Glob *.domains file names, remove file paths and sort by list number
|
|
|
|
lists_raw=(/etc/pihole/*.domains)
|
|
|
|
IFS_OLD=$IFS
|
|
|
|
IFS=$'\n'
|
|
|
|
lists=$(sort -t . -k 2 -g <<< "${lists_raw[*]//\/etc\/pihole\//}")
|
|
|
|
|
|
|
|
# Scan Domains files
|
|
|
|
results=($(scanList "${query}" "${lists}" "${method}"))
|
|
|
|
|
|
|
|
# Handle notices
|
|
|
|
if [[ -z "${blResult}" ]] && [[ -z "${results[*]}" ]]; then
|
|
|
|
notice=true
|
|
|
|
str="No ${method/t/t }results found for ${query} found within block lists"
|
|
|
|
elif [[ -z "${all}" ]] && [[ "${#results[*]}" -ge 16000 ]]; then
|
|
|
|
# 16000 chars is 15 chars X 1000 lines worth of results
|
|
|
|
notice=true
|
|
|
|
str="Hundreds of ${method/t/t }results found for ${query}
|
|
|
|
This can be overriden using the -all option"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${notice}" ]]; then
|
|
|
|
echo -e " ${INFO} ${str}"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove unwanted content from results
|
|
|
|
if [[ -z "${method}" ]]; then
|
|
|
|
results=($(sed "/:#/d" <<< "${results[*]}")) # Lines starting with comments
|
|
|
|
results=($(sed "s/[ \t]#.*//g" <<< "${results[*]}")) # Comments after domain
|
|
|
|
results=($(sed "s/:.*[ \t]/:/g" <<< "${results[*]}")) # IP address
|
|
|
|
fi
|
|
|
|
IFS=$IFS_OLD
|
|
|
|
|
|
|
|
# Get adlist content as array
|
|
|
|
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
|
|
|
if [[ -f "/etc/pihole/adlists.list" ]]; then
|
|
|
|
for url in $(< /etc/pihole/adlists.list); do
|
|
|
|
if [[ "${url:0:4}" == "http" ]] || [[ "${url:0:3}" == "www" ]]; then
|
|
|
|
adlists+=("$url")
|
|
|
|
fi
|
|
|
|
done
|
2017-05-27 22:42:44 +02:00
|
|
|
else
|
2017-07-15 12:56:40 +02:00
|
|
|
echo -e " ${COL_LIGHT_RED}The file '/etc/pihole/adlists.list' was not found${COL_NC}"
|
|
|
|
exit 1
|
2017-05-27 22:42:44 +02:00
|
|
|
fi
|
2017-07-15 12:56:40 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${results[*]}" ]]; then
|
|
|
|
if [[ -n "${exact}" ]]; then
|
|
|
|
echo " Exact result(s) for ${query} found in:"
|
|
|
|
fi
|
|
|
|
|
|
|
|
for result in "${results[@]}"; do
|
|
|
|
filename="${result/:*/}"
|
|
|
|
|
|
|
|
# Convert file name to URL name for -adlist or -bp options
|
|
|
|
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
|
|
|
filenum=("${filename/list./}")
|
|
|
|
filenum=("${filenum/.*/}")
|
|
|
|
filename="${adlists[$filenum]}"
|
|
|
|
fi
|
2017-05-27 22:42:44 +02:00
|
|
|
|
2017-07-15 12:56:40 +02:00
|
|
|
if [[ -n "${exact}" ]]; then
|
|
|
|
printf " %s\n" "${filename}"
|
|
|
|
elif [[ -n "${blockpage}" ]]; then
|
|
|
|
printf " [%s] %s\n" "${filenum}" "${filename}"
|
|
|
|
else # Standard query output
|
|
|
|
|
|
|
|
# Print filename heading once per file, not for every match
|
|
|
|
if [[ ! "${filename}" == "${filename_prev:-}" ]]; then
|
|
|
|
unset count
|
|
|
|
printf " Result from %s\n" "${filename}"
|
|
|
|
else
|
|
|
|
let count++
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Print matching domain if $max_count has not been reached
|
|
|
|
[[ -z "${all}" ]] && max_count="20"
|
|
|
|
if [[ -z "${all}" ]] && [[ "${count}" -eq "${max_count}" ]]; then
|
|
|
|
echo " Over $count results found, skipping rest of file"
|
|
|
|
elif [[ -z "${all}" ]] && [[ "${count}" -gt "${max_count}" ]]; then
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
domain="${result/*:/}"
|
|
|
|
printf " %s\n" "${domain}"
|
|
|
|
fi
|
|
|
|
filename_prev="${filename}"
|
2017-05-27 22:42:44 +02:00
|
|
|
fi
|
|
|
|
done
|
2017-05-14 03:11:44 +02:00
|
|
|
fi
|
2017-07-15 12:56:40 +02:00
|
|
|
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-26 10:39:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
chronometerFunc() {
|
2016-11-02 17:36:30 +01:00
|
|
|
shift
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/chronometer.sh "$@"
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
uninstallFunc() {
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/uninstall.sh
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
versionFunc() {
|
2016-11-02 17:36:30 +01:00
|
|
|
shift
|
2016-11-02 18:39:27 +01:00
|
|
|
"${PI_HOLE_SCRIPT_DIR}"/version.sh "$@"
|
2016-11-02 17:36:30 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 14:45:20 +02:00
|
|
|
restartDNS() {
|
|
|
|
dnsmasqPid=$(pidof dnsmasq)
|
2017-07-15 12:11:06 +02:00
|
|
|
local str="Restarting DNS service"
|
|
|
|
echo -ne " ${INFO} ${str}"
|
2017-05-14 03:11:44 +02:00
|
|
|
if [[ "${dnsmasqPid}" ]]; then
|
|
|
|
# Service already running - reload config
|
|
|
|
if [[ -x "$(command -v systemctl)" ]]; then
|
2017-07-15 12:11:06 +02:00
|
|
|
output=$( { systemctl restart dnsmasq; } 2>&1 )
|
2016-10-20 14:45:20 +02:00
|
|
|
else
|
2017-07-15 12:11:06 +02:00
|
|
|
output=$( { service dnsmasq restart; } 2>&1 )
|
|
|
|
fi
|
|
|
|
if [[ -z "${output}" ]]; then
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
|
|
|
else
|
|
|
|
echo -e "${OVER} ${CROSS} ${output}"
|
2016-10-20 14:45:20 +02:00
|
|
|
fi
|
|
|
|
else
|
2017-05-14 03:11:44 +02:00
|
|
|
# Service not running, start it up
|
|
|
|
if [[ -x "$(command -v systemctl)" ]]; then
|
2017-07-15 12:11:06 +02:00
|
|
|
output=$( { systemctl start dnsmasq; } 2>&1 )
|
|
|
|
else
|
|
|
|
output=$( { service dnsmasq start; } 2>&1 )
|
|
|
|
fi
|
|
|
|
if [[ -z "${output}" ]]; then
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2016-10-20 14:45:20 +02:00
|
|
|
else
|
2017-07-15 12:11:06 +02:00
|
|
|
echo -e "${OVER} ${CROSS} ${output}"
|
2016-10-20 14:45:20 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
piholeEnable() {
|
2017-05-14 03:53:40 +02:00
|
|
|
if [[ "${2}" == "-h" ]] || [[ "${2}" == "--help" ]]; then
|
2017-05-14 03:11:44 +02:00
|
|
|
echo "Usage: pihole disable [time]
|
|
|
|
Example: 'pihole disable', or 'pihole disable 5m'
|
|
|
|
Disable Pi-hole subsystems
|
|
|
|
|
|
|
|
Time:
|
|
|
|
#s Disable Pi-hole functionality for # second(s)
|
|
|
|
#m Disable Pi-hole functionality for # minute(s)"
|
|
|
|
exit 0
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-05-14 03:11:44 +02:00
|
|
|
elif [[ "${1}" == "0" ]]; then
|
|
|
|
# Disable Pi-hole
|
2016-12-14 20:42:20 +01:00
|
|
|
sed -i 's/^addn-hosts=\/etc\/pihole\/gravity.list/#addn-hosts=\/etc\/pihole\/gravity.list/' /etc/dnsmasq.d/01-pihole.conf
|
2017-06-17 14:49:02 +02:00
|
|
|
sed -i 's/^addn-hosts=\/etc\/pihole\/black.list/#addn-hosts=\/etc\/pihole\/black.list/' /etc/dnsmasq.d/01-pihole.conf
|
2017-05-27 17:51:41 +02:00
|
|
|
if [[ -e "$wildcardlist" ]]; then
|
|
|
|
mv "$wildcardlist" "/etc/pihole/wildcard.list"
|
|
|
|
fi
|
2017-05-14 03:11:44 +02:00
|
|
|
if [[ $# > 1 ]]; then
|
2017-06-21 13:49:05 +02:00
|
|
|
local error=false
|
|
|
|
if [[ "${2}" == *"s" ]]; then
|
2016-11-17 23:31:11 +01:00
|
|
|
tt=${2%"s"}
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ "${tt}" =~ ^-?[0-9]+$ ]];then
|
|
|
|
local str="Disabling blocking for ${tt} seconds"
|
|
|
|
echo -e " ${INFO} ${str}..."
|
|
|
|
local str="Blocking will be re-enabled in ${tt} seconds"
|
|
|
|
nohup bash -c "sleep ${tt}; pihole enable" </dev/null &>/dev/null &
|
|
|
|
else
|
|
|
|
local error=true
|
|
|
|
fi
|
|
|
|
elif [[ "${2}" == *"m" ]]; then
|
2016-11-17 23:31:11 +01:00
|
|
|
tt=${2%"m"}
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ "${tt}" =~ ^-?[0-9]+$ ]];then
|
|
|
|
local str="Disabling blocking for ${tt} minutes"
|
|
|
|
echo -e " ${INFO} ${str}..."
|
|
|
|
local str="Blocking will be re-enabled in ${tt} minutes"
|
|
|
|
tt=$((${tt}*60))
|
|
|
|
nohup bash -c "sleep ${tt}; pihole enable" </dev/null &>/dev/null &
|
|
|
|
else
|
|
|
|
local error=true
|
|
|
|
fi
|
|
|
|
elif [[ -n "${2}" ]]; then
|
|
|
|
local error=true
|
2016-11-17 22:58:00 +01:00
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Disabling blocking"
|
2016-11-17 22:58:00 +01:00
|
|
|
fi
|
2017-06-21 13:49:05 +02:00
|
|
|
|
|
|
|
if [[ ${error} == true ]];then
|
|
|
|
echo -e " ${COL_LIGHT_RED}Unknown format for delayed reactivation of the blocking!${COL_NC}"
|
|
|
|
echo -e " Try 'pihole disable --help' for more information."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local str="Pi-hole Disabled"
|
2016-11-17 22:58:00 +01:00
|
|
|
fi
|
2016-10-20 14:45:20 +02:00
|
|
|
else
|
2017-05-14 03:11:44 +02:00
|
|
|
# Enable Pi-hole
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Enabling blocking"
|
|
|
|
local str="Pi-hole Enabled"
|
|
|
|
|
2016-10-20 14:45:20 +02:00
|
|
|
sed -i 's/^#addn-hosts/addn-hosts/' /etc/dnsmasq.d/01-pihole.conf
|
2017-05-27 17:51:41 +02:00
|
|
|
if [[ -e "/etc/pihole/wildcard.list" ]]; then
|
|
|
|
mv "/etc/pihole/wildcard.list" "$wildcardlist"
|
|
|
|
fi
|
2016-10-20 14:45:20 +02:00
|
|
|
fi
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2016-10-20 14:45:20 +02:00
|
|
|
restartDNS
|
2017-06-21 13:49:05 +02:00
|
|
|
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2016-10-20 14:45:20 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 23:02:20 +01:00
|
|
|
piholeLogging() {
|
2016-11-02 10:35:48 +01:00
|
|
|
shift
|
2017-05-14 03:53:40 +02:00
|
|
|
if [[ "${1}" == "-h" ]] || [[ "${1}" == "--help" ]]; then
|
2017-05-14 03:11:44 +02:00
|
|
|
echo "Usage: pihole logging [options]
|
|
|
|
Example: 'pihole logging on'
|
|
|
|
Specify whether the Pi-hole log should be used
|
|
|
|
|
|
|
|
Options:
|
|
|
|
on Enable the Pi-hole log at /var/log/pihole.log
|
|
|
|
off Disable the Pi-hole log at /var/log/pihole.log"
|
|
|
|
exit 0
|
|
|
|
elif [[ "${1}" == "off" ]]; then
|
|
|
|
# Disable logging
|
2016-11-02 10:35:48 +01:00
|
|
|
sed -i 's/^log-queries/#log-queries/' /etc/dnsmasq.d/01-pihole.conf
|
|
|
|
sed -i 's/^QUERY_LOGGING=true/QUERY_LOGGING=false/' /etc/pihole/setupVars.conf
|
|
|
|
pihole -f
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Disabling logging..."
|
|
|
|
local str="Logging has been disabled!"
|
2017-05-14 03:11:44 +02:00
|
|
|
elif [[ "${1}" == "on" ]]; then
|
|
|
|
# Enable logging
|
2016-11-02 10:35:48 +01:00
|
|
|
sed -i 's/^#log-queries/log-queries/' /etc/dnsmasq.d/01-pihole.conf
|
|
|
|
sed -i 's/^QUERY_LOGGING=false/QUERY_LOGGING=true/' /etc/pihole/setupVars.conf
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Enabling logging..."
|
|
|
|
local str="Logging has been enabled!"
|
2016-11-02 10:35:48 +01:00
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${COL_LIGHT_RED}Invalid option${COL_NC}
|
|
|
|
Try 'pihole logging --help' for more information."
|
2016-11-02 10:35:48 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
restartDNS
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2016-10-31 23:02:20 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 14:45:20 +02:00
|
|
|
piholeStatus() {
|
2017-05-14 03:11:44 +02:00
|
|
|
if [[ "$(netstat -plnt | grep -c ':53 ')" -gt "0" ]]; then
|
|
|
|
if [[ "${1}" != "web" ]]; then
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${TICK} DNS service is running"
|
2016-12-28 16:58:48 +01:00
|
|
|
fi
|
|
|
|
else
|
2017-05-14 03:11:44 +02:00
|
|
|
if [[ "${1}" == "web" ]]; then
|
2016-12-28 16:58:48 +01:00
|
|
|
echo "-1";
|
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${CROSS} DNS service is NOT running"
|
2016-12-28 16:58:48 +01:00
|
|
|
fi
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2017-05-14 03:11:44 +02:00
|
|
|
if [[ "$(grep -i "^#addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf)" ]]; then
|
|
|
|
# List is commented out
|
|
|
|
if [[ "${1}" == "web" ]]; then
|
2016-10-23 20:33:28 +02:00
|
|
|
echo 0;
|
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${CROSS} Pi-hole blocking is Disabled";
|
2016-10-21 00:40:03 +02:00
|
|
|
fi
|
2017-05-14 03:11:44 +02:00
|
|
|
elif [[ "$(grep -i "^addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf)" ]]; then
|
|
|
|
# List set
|
|
|
|
if [[ "${1}" == "web" ]]; then
|
2016-10-23 20:33:28 +02:00
|
|
|
echo 1;
|
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${TICK} Pi-hole blocking is Enabled";
|
2016-10-20 14:45:20 +02:00
|
|
|
fi
|
2016-10-21 00:40:03 +02:00
|
|
|
else
|
2017-05-14 03:11:44 +02:00
|
|
|
# Addn-host not found
|
|
|
|
if [[ "${1}" == "web" ]]; then
|
2016-10-21 00:45:27 +02:00
|
|
|
echo 99
|
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} No hosts file linked to dnsmasq, adding it in enabled state"
|
2016-10-21 00:45:27 +02:00
|
|
|
fi
|
2017-05-14 03:11:44 +02:00
|
|
|
# Add addn-host= to dnsmasq
|
2016-10-21 00:40:03 +02:00
|
|
|
echo "addn-hosts=/etc/pihole/gravity.list" >> /etc/dnsmasq.d/01-pihole.conf
|
|
|
|
restartDNS
|
2016-10-20 14:45:20 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-12-08 21:15:03 +01:00
|
|
|
tailFunc() {
|
2017-07-02 15:21:00 +02:00
|
|
|
date=$(date +'%b %d ')
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Press Ctrl-C to exit"
|
2017-07-02 15:21:00 +02:00
|
|
|
tail -f /var/log/pihole.log | sed \
|
|
|
|
-e "s,\(${date}\| dnsmasq\[.*[0-9]]\),,g" \
|
|
|
|
-e "s,\(.*\(gravity.list\|black.list\| config \).* is \(${IPV4_ADDRESS%/*}\|${IPV6_ADDRESS:-NULL}\).*\),${COL_LIGHT_RED}&${COL_NC}," \
|
|
|
|
-e "s,.*\(query\[A\|DHCP\).*,${COL_NC}&${COL_NC}," \
|
|
|
|
-e "s,.*,${COL_DARK_GRAY}&${COL_NC},"
|
2016-12-08 21:15:03 +01:00
|
|
|
exit 0
|
|
|
|
}
|
2016-10-20 14:45:20 +02:00
|
|
|
|
2017-03-08 13:16:40 +01:00
|
|
|
piholeCheckoutFunc() {
|
2017-05-14 04:22:19 +02:00
|
|
|
if [[ "$2" == "-h" ]] || [[ "$2" == "--help" ]]; then
|
2017-05-14 03:11:44 +02:00
|
|
|
echo "Usage: pihole checkout [repo] [branch]
|
|
|
|
Example: 'pihole checkout master' or 'pihole checkout core dev'
|
|
|
|
Switch Pi-hole subsystems to a different Github branch
|
|
|
|
|
|
|
|
Repositories:
|
|
|
|
core [branch] Change the branch of Pi-hole's core subsystem
|
|
|
|
web [branch] Change the branch of Admin Console subsystem
|
2017-05-14 17:17:04 +02:00
|
|
|
|
2017-05-14 03:11:44 +02:00
|
|
|
Branches:
|
|
|
|
master Update subsystems to the latest stable release
|
|
|
|
dev Update subsystems to the latest development release"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2017-03-08 13:16:40 +01:00
|
|
|
source "${PI_HOLE_SCRIPT_DIR}"/piholeCheckout.sh
|
2017-03-08 22:57:35 +01:00
|
|
|
shift
|
2017-03-08 13:16:40 +01:00
|
|
|
checkout "$@"
|
|
|
|
}
|
|
|
|
|
2017-05-13 21:08:21 +02:00
|
|
|
tricorderFunc() {
|
2017-05-18 04:53:32 +02:00
|
|
|
if [[ ! -p "/dev/stdin" ]]; then
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Please do not call Tricorder directly"
|
2017-05-14 11:27:14 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2017-05-27 17:51:41 +02:00
|
|
|
|
2017-05-18 04:43:17 +02:00
|
|
|
if ! timeout 2 nc -z tricorder.pi-hole.net 9998 &> /dev/null; then
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${CROSS} Unable to connect to Pi-hole's Tricorder server"
|
2017-05-18 04:43:17 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2017-05-14 11:27:14 +02:00
|
|
|
|
2017-05-13 21:08:21 +02:00
|
|
|
if command -v openssl &> /dev/null; then
|
|
|
|
openssl s_client -quiet -connect tricorder.pi-hole.net:9998 2> /dev/null < /dev/stdin
|
2017-05-18 04:43:17 +02:00
|
|
|
exit "$?"
|
2017-05-13 21:08:21 +02:00
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} ${COL_YELLOW}Security Notice${COL_NC}: ${COL_WHITE}openssl${COL_NC} is not installed
|
|
|
|
Your debug log will be transmitted unencrypted via plain-text
|
|
|
|
There is a possibility that this could be intercepted by a third party
|
|
|
|
If you wish to cancel, press Ctrl-C to exit within 10 seconds"
|
2017-05-14 11:27:14 +02:00
|
|
|
secs="10"
|
2017-05-18 04:53:32 +02:00
|
|
|
while [[ "$secs" -gt "0" ]]; do
|
2017-05-14 11:27:14 +02:00
|
|
|
echo -ne "."
|
|
|
|
sleep 1
|
|
|
|
: $((secs--))
|
|
|
|
done
|
|
|
|
echo " "
|
2017-05-16 01:48:46 +02:00
|
|
|
nc tricorder.pi-hole.net 9999 < /dev/stdin
|
2017-05-18 04:43:17 +02:00
|
|
|
exit "$?"
|
2017-05-13 21:08:21 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-10-15 18:25:17 +02:00
|
|
|
helpFunc() {
|
2017-05-14 03:11:44 +02:00
|
|
|
echo "Usage: pihole [options]
|
|
|
|
Example: 'pihole -w -h'
|
|
|
|
Add '-h' after specific commands for more information on usage
|
2017-05-14 17:17:04 +02:00
|
|
|
|
2017-05-14 03:11:44 +02:00
|
|
|
Whitelist/Blacklist Options:
|
|
|
|
-w, whitelist Whitelist domain(s)
|
|
|
|
-b, blacklist Blacklist domain(s)
|
|
|
|
-wild, wildcard Blacklist domain(s), and all its subdomains
|
|
|
|
Add '-h' for more info on whitelist/blacklist usage
|
|
|
|
|
|
|
|
Debugging Options:
|
|
|
|
-d, debug Start a debugging session
|
|
|
|
Add '-a' to enable automated debugging
|
|
|
|
-f, flush Flush the Pi-hole log
|
|
|
|
-r, reconfigure Reconfigure or Repair Pi-hole subsystems
|
|
|
|
-t, tail View the live output of the Pi-hole log
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-a, admin Admin Console options
|
|
|
|
Add '-h' for more info on admin console usage
|
|
|
|
-c, chronometer Calculates stats and displays to an LCD
|
|
|
|
Add '-h' for more info on chronometer usage
|
|
|
|
-g, updateGravity Update the list of ad-serving domains
|
|
|
|
-h, --help, help Show this help dialog
|
|
|
|
-l, logging Specify whether the Pi-hole log should be used
|
|
|
|
Add '-h' for more info on logging usage
|
|
|
|
-q, query Query the adlists for a specified domain
|
2017-07-15 12:56:40 +02:00
|
|
|
Add '-h' for more info on query usage
|
2017-05-14 17:17:04 +02:00
|
|
|
-up, updatePihole Update Pi-hole subsystems
|
2017-05-14 03:11:44 +02:00
|
|
|
-v, version Show installed versions of Pi-hole, Admin Console & FTL
|
2017-05-14 17:17:04 +02:00
|
|
|
Add '-h' for more info on version usage
|
2017-05-14 03:11:44 +02:00
|
|
|
uninstall Uninstall Pi-hole from your system
|
|
|
|
status Display the running status of Pi-hole subsystems
|
|
|
|
enable Enable Pi-hole subsystems
|
|
|
|
disable Disable Pi-hole subsystems
|
|
|
|
Add '-h' for more info on disable usage
|
|
|
|
restartdns Restart Pi-hole subsystems
|
|
|
|
checkout Switch Pi-hole subsystems to a different Github branch
|
|
|
|
Add '-h' for more info on checkout usage";
|
2016-12-01 21:21:08 +01:00
|
|
|
exit 0
|
2016-08-27 00:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
2016-11-02 17:36:30 +01:00
|
|
|
helpFunc
|
2016-08-27 00:10:22 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Handle redirecting to specific functions based on arguments
|
2016-10-22 08:17:14 +02:00
|
|
|
case "${1}" in
|
2016-11-02 17:36:30 +01:00
|
|
|
"-w" | "whitelist" ) whitelistFunc "$@";;
|
|
|
|
"-b" | "blacklist" ) blacklistFunc "$@";;
|
2016-12-31 13:49:04 +01:00
|
|
|
"-wild" | "wildcard" ) wildcardFunc "$@";;
|
2017-02-27 00:36:53 +01:00
|
|
|
"-d" | "debug" ) debugFunc "$@";;
|
2017-05-17 12:44:35 +02:00
|
|
|
"-f" | "flush" ) flushFunc "$@";;
|
2016-11-02 17:36:30 +01:00
|
|
|
"-up" | "updatePihole" ) updatePiholeFunc;;
|
|
|
|
"-r" | "reconfigure" ) reconfigurePiholeFunc;;
|
|
|
|
"-g" | "updateGravity" ) updateGravityFunc "$@";;
|
|
|
|
"-c" | "chronometer" ) chronometerFunc "$@";;
|
|
|
|
"-h" | "help" ) helpFunc;;
|
|
|
|
"-v" | "version" ) versionFunc "$@";;
|
|
|
|
"-q" | "query" ) queryFunc "$@";;
|
|
|
|
"-l" | "logging" ) piholeLogging "$@";;
|
|
|
|
"uninstall" ) uninstallFunc;;
|
|
|
|
"enable" ) piholeEnable 1;;
|
2017-05-14 03:11:44 +02:00
|
|
|
"disable" ) piholeEnable 0 "$2";;
|
2016-11-02 17:36:30 +01:00
|
|
|
"status" ) piholeStatus "$2";;
|
|
|
|
"restartdns" ) restartDNS;;
|
2016-11-20 15:15:27 +01:00
|
|
|
"-a" | "admin" ) webpageFunc "$@";;
|
2016-12-08 21:15:03 +01:00
|
|
|
"-t" | "tail" ) tailFunc;;
|
2017-03-08 13:16:40 +01:00
|
|
|
"checkout" ) piholeCheckoutFunc "$@";;
|
2017-06-03 19:13:40 +02:00
|
|
|
"tricorder" ) tricorderFunc;;
|
2016-11-02 17:36:30 +01:00
|
|
|
* ) helpFunc;;
|
2016-10-15 18:25:17 +02:00
|
|
|
esac
|