2015-11-11 09:43:00 +01:00
|
|
|
#!/usr/bin/env bash
|
2017-07-24 13:24:34 +02:00
|
|
|
# shellcheck disable=SC1090
|
|
|
|
|
2015-11-23 11:52:12 +01:00
|
|
|
# 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.
|
|
|
|
#
|
2017-07-24 13:24:34 +02:00
|
|
|
# Usage: "pihole -g"
|
2015-11-07 00:03:55 +01:00
|
|
|
# Compiles a list of ad-serving domains by downloading them from multiple sources
|
2015-12-06 14:55:50 +01:00
|
|
|
#
|
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
|
|
|
coltable="/opt/pihole/COL_TABLE"
|
|
|
|
source ${coltable}
|
2016-04-11 12:29:14 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
basename="pihole"
|
|
|
|
PIHOLE_COMMAND="/usr/local/bin/${basename}"
|
|
|
|
WHITELIST_COMMAND="${PIHOLE_COMMAND} -w"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
piholeDir="/etc/${basename}"
|
|
|
|
piholeRepo="/etc/.${basename}"
|
|
|
|
|
|
|
|
adListFile="${piholeDir}/adlists.list"
|
|
|
|
adListDefault="${piholeDir}/adlists.default"
|
|
|
|
adListRepoDefault="${piholeRepo}/adlists.default"
|
2016-08-17 19:59:00 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
whitelistFile="${piholeDir}/whitelist.txt"
|
|
|
|
blacklistFile="${piholeDir}/blacklist.txt"
|
|
|
|
wildcardFile="/etc/dnsmasq.d/03-pihole-wildcard.conf"
|
2016-01-21 23:14:55 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
adList="${piholeDir}/gravity.list"
|
|
|
|
blackList="${piholeDir}/black.list"
|
|
|
|
localList="${piholeDir}/local.list"
|
2016-01-26 21:26:09 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
domainsExtension="domains"
|
|
|
|
matterAndLight="${basename}.0.matterandlight.txt"
|
|
|
|
supernova="${basename}.1.supernova.txt"
|
|
|
|
preEventHorizon="list.preEventHorizon"
|
|
|
|
eventHorizon="${basename}.2.supernova.txt"
|
|
|
|
accretionDisc="${basename}.3.accretionDisc.txt"
|
|
|
|
|
|
|
|
skipDownload="false"
|
|
|
|
|
|
|
|
# Source setupVars from install script
|
|
|
|
setupVars="${piholeDir}/setupVars.conf"
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -f "${setupVars}" ]];then
|
2017-07-24 13:24:34 +02:00
|
|
|
source "${setupVars}"
|
|
|
|
|
|
|
|
# Remove CIDR mask from IPv4/6 addresses
|
|
|
|
IPV4_ADDRESS="${IPV4_ADDRESS%/*}"
|
|
|
|
IPV6_ADDRESS="${IPV6_ADDRESS%/*}"
|
2016-10-15 21:03:33 +02:00
|
|
|
else
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e " ${COL_LIGHT_RED}Installation Failure: ${setupVars} does not exist! ${COL_NC}
|
|
|
|
Please run 'pihole -r', and choose the 'reconfigure' option to fix."
|
2017-06-21 13:49:05 +02:00
|
|
|
exit 1
|
2016-10-15 21:03:33 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
# Warn users still using pihole.conf that it no longer has any effect
|
2017-07-24 13:24:34 +02:00
|
|
|
if [[ -r "${piholeDir}/pihole.conf" ]]; then
|
|
|
|
echo -e " ${COL_LIGHT_RED}Ignoring overrides specified within pihole.conf! ${COL_NC}"
|
2015-11-07 00:03:55 +01:00
|
|
|
fi
|
2015-11-27 00:48:52 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
helpFunc() {
|
|
|
|
echo "Usage: pihole -g
|
|
|
|
Update domains from blocklists specified in adlists.list
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-f, --force Force the download of all specified blocklists
|
|
|
|
-h, --help Show this help dialog"
|
|
|
|
exit 0
|
|
|
|
}
|
2017-03-31 20:16:09 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Retrieve blocklist URLs from adlists.list
|
|
|
|
gravity_Collapse() {
|
|
|
|
echo -e " ${INFO} Neutrino emissions detected..."
|
2017-03-31 20:16:09 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Handle "adlists.list" and "adlists.default" files
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -f "${adListDefault}" ]] && [[ -f "${adListFile}" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
# Remove superceded $adListDefault file
|
|
|
|
rm "${adListDefault}" 2> /dev/null || \
|
|
|
|
echo -e " ${CROSS} Unable to remove ${adListDefault}"
|
|
|
|
elif [[ ! -f "${adListFile}" ]]; then
|
|
|
|
# Create "adlists.list"
|
|
|
|
cp "${adListRepoDefault}" "${adListFile}" 2> /dev/null || \
|
|
|
|
echo -e " ${CROSS} Unable to copy ${adListFile##*/} from ${piholeRepo}"
|
2017-03-31 20:16:09 +02:00
|
|
|
fi
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
local str="Pulling blocklist source list into range"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -ne " ${INFO} ${str}..."
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Retrieve source URLs from $adListFile
|
|
|
|
# Logic: Remove comments, CR line endings and empty lines
|
|
|
|
mapfile -t sources < <(awk '!/^[#@;!\[]/ {gsub(/\r$/, "", $0); if ($1) { print $1 } }' "${adListFile}" 2> /dev/null)
|
|
|
|
|
|
|
|
# Parse source domains from $sources
|
|
|
|
# Logic: Split by folder/port and remove URL protocol/password
|
|
|
|
mapfile -t sourceDomains < <(
|
|
|
|
awk -F '[/:]' '{
|
|
|
|
gsub(/(.*:\/\/|.*:.*@)/, "", $0)
|
|
|
|
print $1
|
|
|
|
}' <<< "$(printf '%s\n' "${sources[@]}")" 2> /dev/null
|
|
|
|
)
|
|
|
|
|
|
|
|
if [[ -n "${sources[*]}" ]] || [[ -n "${sourceDomains[*]}" ]]; then
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
|
|
|
else
|
|
|
|
echo -e "${OVER} ${CROSS} ${str}"
|
|
|
|
gravity_Cleanup "error"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Parse source file into domains-only format
|
|
|
|
gravity_ParseFileAsDomains() {
|
|
|
|
local source destination hostsFilter firstLine abpFilter
|
|
|
|
source="${1}"
|
|
|
|
destination="${2}"
|
|
|
|
|
|
|
|
# Determine how to parse source file
|
|
|
|
if [[ "${source}" == "${piholeDir}/${matterAndLight}" ]]; then
|
|
|
|
# Symbols used as comments: "#;@![/"
|
|
|
|
commentPattern="[#;@![\\/]"
|
|
|
|
|
|
|
|
# Parse consolidated file by removing comments and hosts IP's
|
|
|
|
# Logic: Process lines which do not begin with comments
|
|
|
|
awk '!/^'"${commentPattern}"'/ {
|
|
|
|
# If there are multiple words seperated by space
|
|
|
|
if (NF>1) {
|
|
|
|
# Remove comments (Inc. prefixed spaces/tabs)
|
|
|
|
if ($0 ~ /'"${commentPattern}"'/) { gsub("( | )'"${commentPattern}"'.*", "", $0) }
|
|
|
|
# Print consecutive domains
|
|
|
|
if ($3) {
|
|
|
|
$1=""
|
|
|
|
gsub("^ ", "", $0)
|
|
|
|
print $0
|
|
|
|
# Print single domain
|
|
|
|
} else if ($2) {
|
|
|
|
print $2
|
|
|
|
}
|
|
|
|
# Print single domain
|
|
|
|
} else if($1) {
|
|
|
|
print $1
|
|
|
|
}
|
|
|
|
}' "${source}" 2> /dev/null > "${destination}"
|
|
|
|
else
|
|
|
|
# Individual file parsing
|
|
|
|
# Logic: comments are kept, and domains are extracted from each line
|
|
|
|
read -r firstLine < "${source}"
|
|
|
|
|
|
|
|
# Determine how to parse individual source file
|
|
|
|
if [[ "${firstLine,,}" =~ "adblock" ]] || [[ "${firstLine,,}" =~ "ublock" ]] || [[ "${firstLine,,}" =~ "! checksum" ]]; then
|
|
|
|
# Parse Adblock domains & comments: https://adblockplus.org/filter-cheatsheet
|
|
|
|
abpFilter="/^(\\[|!)|^(\\|\\|.*\\^)/"
|
|
|
|
awk ''"${abpFilter}"' {
|
|
|
|
# Remove valid adblock type options
|
|
|
|
gsub(/~?(important|third-party|popup|subdocument|websocket),?/, "", $0)
|
|
|
|
# Remove starting domain name anchor "||" and ending seperator "^$" ($ optional)
|
|
|
|
gsub(/(\|\||\^\$?$)/, "", $0)
|
|
|
|
# Remove lines which are only IPv4 addresses or contain "^/*"
|
|
|
|
if ($0 ~ /(^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|[\\^\/\*])/) { $0="" }
|
|
|
|
# Print if not empty
|
|
|
|
if ($0) { print $0 }
|
|
|
|
}' "${source}" 2> /dev/null > "${destination}"
|
|
|
|
echo -e " ${TICK} Format: Adblock"
|
|
|
|
elif grep -q -E "^(https?://|([0-9]{1,3}\.){3}[0-9]{1,3}$)" "${source}" &> /dev/null; then
|
|
|
|
# Parse URLs
|
|
|
|
awk '{
|
|
|
|
# Remove URL protocol, optional "username:password@", and ":?/;"
|
|
|
|
if ($0 ~ /[:?\/;]/) { gsub(/(^.*:\/\/(.*:.*@)?|[:?\/;].*)/, "", $0) }
|
|
|
|
# Remove lines which are only IPv4 addresses
|
|
|
|
if ($0 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { $0="" }
|
|
|
|
if ($0) { print $0 }
|
|
|
|
}' "${source}" 2> /dev/null > "${destination}"
|
|
|
|
echo -e " ${TICK} Format: URL"
|
2017-03-31 20:16:09 +02:00
|
|
|
else
|
2017-07-24 13:24:34 +02:00
|
|
|
# Keep hosts/domains file in same format as it was downloaded
|
|
|
|
output=$( { mv "${source}" "${destination}"; } 2>&1 )
|
|
|
|
status="$?"
|
|
|
|
|
|
|
|
if [[ "${status}" -ne 0 ]]; then
|
|
|
|
echo -e " ${CROSS} Unable to move tmp file to ${piholeDir}
|
|
|
|
${output}"
|
|
|
|
gravity_Cleanup "error"
|
|
|
|
fi
|
2017-03-31 20:16:09 +02:00
|
|
|
fi
|
2017-07-24 13:24:34 +02:00
|
|
|
fi
|
2015-11-23 09:36:01 +01:00
|
|
|
}
|
2015-11-23 08:49:38 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Determine output based on gravity_Transport() status
|
|
|
|
gravity_AdvancedTransport() {
|
|
|
|
local patternBuffer success error output status
|
|
|
|
patternBuffer="${1}"
|
|
|
|
success="${2}"
|
|
|
|
error="${3}"
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ "${success}" = true ]]; then
|
|
|
|
if [[ "${error}" == "304" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
: # Print no output
|
2017-06-21 13:49:05 +02:00
|
|
|
# Check if the patternbuffer is a non-zero length file
|
|
|
|
elif [[ -s "${patternBuffer}" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
# Parse Adblock/URL format blocklists and include comments
|
|
|
|
# HOSTS format is moved as-is
|
|
|
|
gravity_ParseFileAsDomains "${patternBuffer}" "${saveLocation}" "1"
|
2017-06-21 13:49:05 +02:00
|
|
|
else
|
2017-07-24 13:24:34 +02:00
|
|
|
# Fall back to previously cached list if current $patternBuffer is empty
|
|
|
|
echo -e " ${INFO} Received empty file: ${COL_LIGHT_GREEN}using previously cached list${COL_NC}"
|
2017-06-21 13:49:05 +02:00
|
|
|
fi
|
|
|
|
else
|
2017-07-24 13:24:34 +02:00
|
|
|
# Determine if cached list exists
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -r "${saveLocation}" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e " ${CROSS} List download failed: ${COL_LIGHT_GREEN}using previously cached list${COL_NC}"
|
2017-06-21 13:49:05 +02:00
|
|
|
else
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e " ${CROSS} List download failed: ${COL_LIGHT_RED}no cached list available${COL_NC}"
|
2017-06-21 13:49:05 +02:00
|
|
|
fi
|
|
|
|
fi
|
2015-11-23 11:52:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Curl the specified URL with any necessary command extentions
|
|
|
|
gravity_Transport() {
|
|
|
|
local url cmd_ext agent
|
|
|
|
url="${1}"
|
|
|
|
cmd_ext="${2}"
|
|
|
|
agent="${3}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Store downloaded content to temp file instead of RAM
|
2017-06-21 13:49:05 +02:00
|
|
|
patternBuffer=$(mktemp)
|
|
|
|
heisenbergCompensator=""
|
2017-07-24 13:24:34 +02:00
|
|
|
if [[ -r "${saveLocation}" ]]; then
|
2017-06-21 13:49:05 +02:00
|
|
|
# If domain has been saved, add file for date check to only download newer
|
|
|
|
heisenbergCompensator="-z ${saveLocation}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local str="Status:"
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -ne " ${INFO} ${str} Pending..."
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
httpCode=$(curl -s -L ${cmd_ext} ${heisenbergCompensator} -w "%{http_code}" -A "${agent}" "${url}" -o "${patternBuffer}" 2> /dev/null)
|
|
|
|
|
|
|
|
# Determine "Status:" output based on HTTP response
|
|
|
|
case "$httpCode" in
|
|
|
|
"200" ) echo -e "${OVER} ${TICK} ${str} Transport successful"; success=true;;
|
|
|
|
"304" ) echo -e "${OVER} ${TICK} ${str} No changes detected"; success=true;;
|
|
|
|
"403" ) echo -e "${OVER} ${CROSS} ${str} Forbidden"; success=false;;
|
|
|
|
"404" ) echo -e "${OVER} ${CROSS} ${str} Not found"; success=false;;
|
|
|
|
"408" ) echo -e "${OVER} ${CROSS} ${str} Time-out"; success=false;;
|
|
|
|
"451" ) echo -e "${OVER} ${CROSS} ${str} Unavailable For Legal Reasons"; success=false;;
|
|
|
|
"521" ) echo -e "${OVER} ${CROSS} ${str} Web Server Is Down (Cloudflare)"; success=false;;
|
|
|
|
"522" ) echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Cloudflare)"; success=false;;
|
|
|
|
"500" ) echo -e "${OVER} ${CROSS} ${str} Internal Server Error"; success=false;;
|
|
|
|
* ) echo -e "${OVER} ${CROSS} ${str} Status $httpCode"; success=false;;
|
2017-06-21 13:49:05 +02:00
|
|
|
esac
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Output additional info if success=false
|
|
|
|
gravity_AdvancedTransport "${patternBuffer}" "${success}" "${httpCode}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Delete temp file if it has not been moved
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -f "${patternBuffer}" ]]; then
|
|
|
|
rm "${patternBuffer}"
|
|
|
|
fi
|
2015-11-23 11:52:12 +01:00
|
|
|
}
|
2015-11-27 00:48:52 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Define User Agent and options for each blocklist
|
|
|
|
gravity_Pull() {
|
|
|
|
local agent url domain cmd_ext str
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
echo ""
|
2017-07-24 13:24:34 +02:00
|
|
|
|
|
|
|
# Loop through $sources to download each one
|
2017-06-21 13:49:05 +02:00
|
|
|
for ((i = 0; i < "${#sources[@]}"; i++)); do
|
2017-07-24 13:24:34 +02:00
|
|
|
url="${sources[$i]}"
|
|
|
|
domain="${sourceDomains[$i]}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
|
|
|
# Save the file as list.#.domain
|
2017-07-24 13:24:34 +02:00
|
|
|
saveLocation="${piholeDir}/list.${i}.${domain}.${domainsExtension}"
|
|
|
|
activeDomains[$i]="${saveLocation}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
|
|
|
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Use a case statement to download lists that need special commands
|
2017-06-21 13:49:05 +02:00
|
|
|
case "${domain}" in
|
2017-07-24 13:24:34 +02:00
|
|
|
"pgl.yoyo.org") cmd_ext="-d mimetype=plaintext -d hostformat=hosts";;
|
|
|
|
*) cmd_ext="";;
|
2017-06-21 13:49:05 +02:00
|
|
|
esac
|
|
|
|
|
|
|
|
if [[ "${skipDownload}" == false ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
str="Target: $domain (${url##*/})"
|
|
|
|
echo -e " ${INFO} ${str}"
|
|
|
|
|
|
|
|
gravity_Transport "$url" "$cmd_ext" "$agent" "$str"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
|
|
|
echo ""
|
|
|
|
fi
|
|
|
|
done
|
2015-11-23 11:52:12 +01:00
|
|
|
}
|
2014-06-08 17:03:56 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Consolidate domains to one list and add blacklisted domains
|
|
|
|
gravity_Schwarzschild() {
|
|
|
|
local str lastLine
|
|
|
|
|
|
|
|
str="Consolidating blocklists"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -ne " ${INFO} ${str}..."
|
2017-07-24 13:24:34 +02:00
|
|
|
|
|
|
|
# Compile all blacklisted domains into one file and remove CRs
|
|
|
|
truncate -s 0 "${piholeDir}/${matterAndLight}"
|
2017-06-21 13:49:05 +02:00
|
|
|
for i in "${activeDomains[@]}"; do
|
|
|
|
# Only assimilate list if it is available (download might have failed permanently)
|
|
|
|
if [[ -r "${i}" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
tr -d '\r' < "${i}" >> "${piholeDir}/${matterAndLight}"
|
|
|
|
|
|
|
|
# Ensure each source blocklist has a final newline
|
|
|
|
lastLine=$(tail -1 "${piholeDir}/${matterAndLight}")
|
|
|
|
[[ "${#lastLine}" -gt 0 ]] && echo "" >> "${piholeDir}/${matterAndLight}"
|
2017-06-21 13:49:05 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2015-11-27 00:48:52 +01:00
|
|
|
}
|
2015-05-19 20:31:37 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Append blacklist entries to eventHorizon if they exist
|
2016-10-20 04:47:45 +02:00
|
|
|
gravity_Blacklist() {
|
2017-07-24 13:24:34 +02:00
|
|
|
local numBlacklisted plural str
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -f "${blacklistFile}" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
numBlacklisted=$(printf "%'.0f" "$(wc -l < "${blacklistFile}")")
|
|
|
|
plural=; [[ "${numBlacklisted}" != "1" ]] && plural=s
|
|
|
|
str="Exact blocked domain${plural}: $numBlacklisted"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} ${str}"
|
|
|
|
else
|
|
|
|
echo -e " ${INFO} Nothing to blacklist!"
|
|
|
|
fi
|
2016-01-15 15:49:16 +01:00
|
|
|
}
|
2015-11-07 00:03:55 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Return number of wildcards in output
|
2017-01-29 13:46:27 +01:00
|
|
|
gravity_Wildcard() {
|
2017-07-24 13:24:34 +02:00
|
|
|
local numWildcards plural
|
|
|
|
|
|
|
|
if [[ -f "${wildcardFile}" ]]; then
|
|
|
|
numWildcards=$(grep -c ^ "${wildcardFile}")
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -n "${IPV4_ADDRESS}" ]] && [[ -n "${IPV6_ADDRESS}" ]];then
|
|
|
|
let numWildcards/=2
|
|
|
|
fi
|
2017-07-24 13:24:34 +02:00
|
|
|
plural=; [[ "${numWildcards}" != "1" ]] && plural=s
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Wildcard blocked domain${plural}: $numWildcards"
|
|
|
|
else
|
|
|
|
echo -e " ${INFO} No wildcards used!"
|
|
|
|
fi
|
2017-01-29 13:46:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Prevent the domains of adlist sources from being blacklisted by other blocklists
|
2016-10-15 21:02:57 +02:00
|
|
|
gravity_Whitelist() {
|
2017-07-24 13:24:34 +02:00
|
|
|
local plural str
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
echo ""
|
2017-07-24 13:24:34 +02:00
|
|
|
plural=; [[ "${#sources[*]}" != "1" ]] && plural=s
|
|
|
|
str="Adding blocklist source${plural} to the whitelist"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -ne " ${INFO} ${str}..."
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Create array of unique $sourceDomains
|
|
|
|
# shellcheck disable=SC2046
|
|
|
|
read -r -a uniqDomains <<< $(awk '{ if(!a[$1]++) { print $1 } }' <<< "$(printf '%s\n' "${sourceDomains[@]}")")
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
${WHITELIST_COMMAND} -nr -q "${uniqDomains[*]}" > /dev/null
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Test existence of whitelist.txt
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -f "${whitelistFile}" ]]; then
|
|
|
|
# Remove anything in whitelist.txt from the Event Horizon
|
|
|
|
numWhitelisted=$(wc -l < "${whitelistFile}")
|
2017-07-24 13:24:34 +02:00
|
|
|
plural=; [[ "${numWhitelisted}" != "1" ]] && plural=s
|
2017-06-21 13:49:05 +02:00
|
|
|
local str="Whitelisting $numWhitelisted domain${plural}"
|
|
|
|
echo -ne " ${INFO} ${str}..."
|
|
|
|
|
|
|
|
# Print everything from preEventHorizon into eventHorizon EXCEPT domains in whitelist.txt
|
2017-07-24 13:24:34 +02:00
|
|
|
grep -F -x -v -f "${whitelistFile}" "${piholeDir}/${preEventHorizon}" > "${piholeDir}/${eventHorizon}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
|
|
|
else
|
|
|
|
echo -e " ${INFO} Nothing to whitelist!"
|
|
|
|
fi
|
2015-11-23 10:16:00 +01:00
|
|
|
}
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Sort and remove duplicate blacklisted domains
|
|
|
|
gravity_Unique() {
|
|
|
|
local str numberOf
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
str="Removing duplicate domains"
|
|
|
|
echo -ne " ${INFO} ${str}..."
|
|
|
|
sort -u "${piholeDir}/${supernova}" > "${piholeDir}/${preEventHorizon}"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2017-07-24 13:24:34 +02:00
|
|
|
|
|
|
|
numberOf=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${preEventHorizon}")")
|
|
|
|
echo -e " ${INFO} ${COL_LIGHT_BLUE}${numberOf}${COL_NC} unique domains trapped in the Event Horizon"
|
2015-11-23 12:11:16 +01:00
|
|
|
}
|
2015-11-27 00:48:52 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Parse list of domains into hosts format
|
|
|
|
gravity_ParseDomainsIntoHosts() {
|
|
|
|
if [[ -n "${IPV4_ADDRESS}" ]] || [[ -n "${IPV6_ADDRESS}" ]]; then
|
|
|
|
awk -v ipv4addr="$IPV4_ADDRESS" -v ipv6addr="$IPV6_ADDRESS" \
|
|
|
|
'{sub(/\r$/,""); if(ipv4addr) { print ipv4addr" "$0; }; if(ipv6addr) { print ipv6addr" "$0; }}' >> "${2}" < "${1}"
|
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e "${OVER} ${CROSS} ${str}"
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e " ${COL_LIGHT_RED}No IP addresses found! Please run 'pihole -r' to reconfigure${COL_NC}\\n"
|
|
|
|
gravity_Cleanup "error"
|
2017-06-17 13:50:10 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Create "localhost" entries
|
|
|
|
gravity_ParseLocalDomains() {
|
2017-06-21 13:49:05 +02:00
|
|
|
if [[ -f "/etc/hostname" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
hostname=$(< "/etc/hostname")
|
|
|
|
elif command -v hostname &> /dev/null; then
|
2017-06-21 13:49:05 +02:00
|
|
|
hostname=$(hostname -f)
|
|
|
|
else
|
|
|
|
echo -e " ${CROSS} Unable to determine fully qualified domain name of host"
|
|
|
|
fi
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e "${hostname}\\npi.hole" > "${localList}.tmp"
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
# Copy the file over as /etc/pihole/local.list so dnsmasq can use it
|
2017-07-24 13:24:34 +02:00
|
|
|
rm "${localList}" 2> /dev/null || \
|
|
|
|
echo -e " ${CROSS} Unable to remove ${localList}"
|
|
|
|
gravity_ParseDomainsIntoHosts "${localList}.tmp" "${localList}"
|
|
|
|
rm "${localList}.tmp" 2> /dev/null || \
|
|
|
|
echo -e " ${CROSS} Unable to remove ${localList}.tmp"
|
2017-06-17 13:57:27 +02:00
|
|
|
}
|
2016-04-02 02:19:47 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Create primary blacklist entries
|
|
|
|
gravity_ParseBlacklistDomains() {
|
|
|
|
# Create $accretionDisc
|
|
|
|
[[ ! -f "${piholeDir}/${accretionDisc}" ]] && echo "" > "${piholeDir}/${accretionDisc}"
|
|
|
|
|
|
|
|
gravity_ParseDomainsIntoHosts "${piholeDir}/${eventHorizon}" "${piholeDir}/${accretionDisc}"
|
|
|
|
|
2017-06-21 13:49:05 +02:00
|
|
|
# Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it
|
2017-07-24 13:24:34 +02:00
|
|
|
output=$( { mv "${piholeDir}/${accretionDisc}" "${adList}"; } 2>&1 )
|
|
|
|
status="$?"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
if [[ "${status}" -ne 0 ]]; then
|
|
|
|
echo -e " ${CROSS} Unable to move ${accretionDisc} from ${piholeDir}
|
|
|
|
${output}"
|
|
|
|
gravity_Cleanup "error"
|
|
|
|
fi
|
2017-06-17 13:57:27 +02:00
|
|
|
}
|
2017-06-17 13:50:10 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Create user-added blacklist entries
|
|
|
|
gravity_ParseUserDomains() {
|
2017-06-17 14:24:30 +02:00
|
|
|
if [[ -f "${blacklistFile}" ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
numBlacklisted=$(printf "%'.0f" "$(wc -l < "${blacklistFile}")")
|
|
|
|
gravity_ParseDomainsIntoHosts "${blacklistFile}" "${blackList}.tmp"
|
2017-06-17 14:24:30 +02:00
|
|
|
# Copy the file over as /etc/pihole/black.list so dnsmasq can use it
|
2017-07-24 13:24:34 +02:00
|
|
|
mv "${blackList}.tmp" "${blackList}" 2> /dev/null || \
|
|
|
|
echo -e " ${CROSS} Unable to move ${blackList##*/}.tmp to ${piholeDir}"
|
2017-06-17 14:24:30 +02:00
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Nothing to blacklist!"
|
2017-06-17 14:24:30 +02:00
|
|
|
fi
|
2015-11-23 12:11:16 +01:00
|
|
|
}
|
2015-11-27 00:48:52 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Parse consolidated blocklist into domains-only format
|
|
|
|
gravity_Advanced() {
|
|
|
|
local str="Extracting domains from blocklists"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -ne " ${INFO} ${str}..."
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Parse files as Hosts
|
|
|
|
gravity_ParseFileAsDomains "${piholeDir}/${matterAndLight}" "${piholeDir}/${supernova}"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
numberOf=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${supernova}")")
|
|
|
|
echo -e "${OVER} ${TICK} ${str}
|
|
|
|
${INFO} ${COL_LIGHT_BLUE}${numberOf}${COL_NC} domains being pulled in by gravity"
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
gravity_Unique
|
|
|
|
}
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Trap Ctrl-C
|
|
|
|
gravity_Trap() {
|
|
|
|
trap '{ echo -e "\\n\\n ${INFO} ${COL_LIGHT_RED}User-abort detected${COL_NC}"; gravity_Cleanup "error"; }' INT
|
2015-12-26 19:37:51 +01:00
|
|
|
}
|
2015-12-05 04:41:37 +01:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Clean up after Gravity
|
|
|
|
gravity_Cleanup() {
|
|
|
|
local error="${1:-}"
|
|
|
|
|
|
|
|
str="Cleaning up debris"
|
|
|
|
echo -ne " ${INFO} ${str}..."
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
rm ${piholeDir}/pihole.*.txt 2> /dev/null
|
|
|
|
rm ${piholeDir}/*.tmp 2> /dev/null
|
|
|
|
|
|
|
|
# Remove any unused .domains files
|
|
|
|
for file in ${piholeDir}/*.${domainsExtension}; do
|
|
|
|
# If list is in active array then leave it (noop) else rm the list
|
|
|
|
if [[ "${activeDomains[*]}" =~ ${file} ]]; then
|
|
|
|
:
|
|
|
|
else
|
|
|
|
rm -f "${file}" 2> /dev/null || \
|
|
|
|
echo -e " ${CROSS} Failed to remove ${file##*/}"
|
|
|
|
fi
|
|
|
|
done
|
2017-06-21 13:49:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
|
|
|
|
|
|
|
[[ -n "$error" ]] && echo ""
|
|
|
|
|
|
|
|
# Only restart DNS service if offline
|
|
|
|
if ! pidof dnsmasq &> /dev/null; then
|
|
|
|
"${PIHOLE_COMMAND}" restartdns
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "$error" ]]; then
|
|
|
|
"${PIHOLE_COMMAND}" status
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-11-06 03:11:34 +01:00
|
|
|
}
|
2015-08-23 06:44:41 +02:00
|
|
|
|
2016-10-22 08:02:45 +02:00
|
|
|
for var in "$@"; do
|
2017-06-21 13:49:05 +02:00
|
|
|
case "${var}" in
|
2017-07-24 13:24:34 +02:00
|
|
|
"-f" | "--force" ) forceDelete=true;;
|
|
|
|
"-h" | "--help" ) helpFunc;;
|
|
|
|
"-sd" | "--skip-download" ) skipDownload=true;;
|
|
|
|
"-b" | "--blacklist-only" ) blackListOnly=true;;
|
|
|
|
"-w" | "--wildcard" ) dnsRestart="restart";;
|
2017-06-21 13:49:05 +02:00
|
|
|
esac
|
2016-08-17 20:08:55 +02:00
|
|
|
done
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# Main Gravity Execution
|
|
|
|
gravity_Trap
|
|
|
|
|
|
|
|
# Use "force-reload" when restarting dnsmasq for Blacklists and Whitelists
|
|
|
|
[[ -z "${dnsRestart}" ]] && dnsRestart="force-reload"
|
|
|
|
|
|
|
|
if [[ "${forceDelete}" == true ]]; then
|
2017-06-21 13:49:05 +02:00
|
|
|
str="Deleting exising list cache"
|
|
|
|
echo -ne "${INFO} ${str}..."
|
|
|
|
|
|
|
|
if rm /etc/pihole/list.* 2> /dev/null; then
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
|
|
|
else
|
|
|
|
echo -e "${OVER} ${CROSS} ${str}"
|
|
|
|
fi
|
2016-08-17 20:08:55 +02:00
|
|
|
fi
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
# If $blackListOnly is true, only run essential functions
|
2017-06-17 13:57:27 +02:00
|
|
|
if [[ ! "${blackListOnly}" == true ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
gravity_Collapse
|
|
|
|
gravity_Pull
|
|
|
|
|
2017-06-17 13:57:27 +02:00
|
|
|
if [[ "${skipDownload}" == false ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
gravity_Schwarzschild
|
|
|
|
gravity_Advanced
|
2017-06-17 13:57:27 +02:00
|
|
|
else
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -e " ${INFO} Using cached Event Horizon list..."
|
2017-07-24 13:24:34 +02:00
|
|
|
numberOf=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${preEventHorizon}")")
|
|
|
|
echo -e " ${INFO} ${COL_LIGHT_BLUE}${numberOf}${COL_NC} unique domains trapped in the Event Horizon"
|
2017-06-17 13:57:27 +02:00
|
|
|
fi
|
2017-07-24 13:24:34 +02:00
|
|
|
|
2017-06-17 13:57:27 +02:00
|
|
|
gravity_Whitelist
|
2016-10-20 00:52:54 +02:00
|
|
|
fi
|
2017-07-24 13:24:34 +02:00
|
|
|
|
2016-01-15 15:49:16 +01:00
|
|
|
gravity_Blacklist
|
2017-01-29 13:46:27 +01:00
|
|
|
gravity_Wildcard
|
2016-10-20 00:15:05 +02:00
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
str="Parsing domains into hosts format"
|
2017-06-21 13:49:05 +02:00
|
|
|
echo -ne " ${INFO} ${str}..."
|
2017-07-24 13:24:34 +02:00
|
|
|
|
2017-06-17 13:57:27 +02:00
|
|
|
if [[ ! "${blackListOnly}" == true ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
gravity_ParseLocalDomains
|
|
|
|
gravity_ParseBlacklistDomains
|
2017-06-17 13:57:27 +02:00
|
|
|
fi
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
gravity_ParseUserDomains
|
|
|
|
echo -e "${OVER} ${TICK} ${str}"
|
2016-10-20 00:15:05 +02:00
|
|
|
|
2017-06-17 13:57:27 +02:00
|
|
|
if [[ ! "${blackListOnly}" == true ]]; then
|
2017-07-24 13:24:34 +02:00
|
|
|
gravity_Cleanup
|
2017-06-17 13:57:27 +02:00
|
|
|
fi
|
|
|
|
|
2017-07-24 13:24:34 +02:00
|
|
|
echo ""
|
|
|
|
"${PIHOLE_COMMAND}" restartdns "${dnsRestart}"
|
2017-03-12 23:15:23 +01:00
|
|
|
"${PIHOLE_COMMAND}" status
|