mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 02:42:58 +00:00
Update resolver test & added more comments
* Add/update code comments * Change resolver check to test for pi.hole * Make resolver check timeout after 10 seconds * Use > instead of &> where appropriate * Make resolver check sleep for 30 seconds (effectively waiting up to 50s for dnsmasq to be resolvable) * Provide confirmation upon success of resolver check availability * Quotes and Braced remaining variables as appropriate * Removed duplicate local
This commit is contained in:
parent
7cac207c00
commit
0a00936e99
1 changed files with 34 additions and 24 deletions
58
gravity.sh
58
gravity.sh
|
@ -64,26 +64,27 @@ if [[ -r "${piholeDir}/pihole.conf" ]]; then
|
||||||
echo -e " ${COL_LIGHT_RED}Ignoring overrides specified within pihole.conf! ${COL_NC}"
|
echo -e " ${COL_LIGHT_RED}Ignoring overrides specified within pihole.conf! ${COL_NC}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Attempt to resolve DNS before proceeding with blocklist generation
|
# Determine if DNS resolution is available before proceeding with retrieving blocklists
|
||||||
gravity_DNSLookup() {
|
gravity_DNSLookup() {
|
||||||
local plu
|
local plu
|
||||||
|
|
||||||
# Determine if github.com can be resolved
|
# Determine if pi.hole can be resolved
|
||||||
if ! timeout 2 nslookup github.com &> /dev/null; then
|
if ! timeout 10 nslookup pi.hole > /dev/null; then
|
||||||
if [[ -n "${secs}" ]]; then
|
if [[ -n "${secs}" ]]; then
|
||||||
echo -e "${OVER} ${CROSS} DNS resolution is still unavailable, cancelling"
|
echo -e "${OVER} ${CROSS} DNS resolution is still unavailable, cancelling"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Determine error output message
|
# Determine error output message
|
||||||
if pidof dnsmasq &> /dev/null; then
|
if pidof dnsmasq > /dev/null; then
|
||||||
echo -e " ${CROSS} DNS resolution is temporarily unavailable"
|
echo -e " ${CROSS} DNS resolution is temporarily unavailable"
|
||||||
else
|
else
|
||||||
echo -e " ${CROSS} DNS service is not running"
|
echo -e " ${CROSS} DNS service is not running"
|
||||||
"${PIHOLE_COMMAND}" restartdns
|
"${PIHOLE_COMMAND}" restartdns
|
||||||
fi
|
fi
|
||||||
|
|
||||||
secs="15"
|
# Give time for dnsmasq to be resolvable
|
||||||
|
secs="30"
|
||||||
while [[ "${secs}" -ne 0 ]]; do
|
while [[ "${secs}" -ne 0 ]]; do
|
||||||
[[ "${secs}" -ne 1 ]] && plu="s" || plu=""
|
[[ "${secs}" -ne 1 ]] && plu="s" || plu=""
|
||||||
echo -ne "${OVER} ${INFO} Waiting $secs second${plu} before continuing..."
|
echo -ne "${OVER} ${INFO} Waiting $secs second${plu} before continuing..."
|
||||||
|
@ -93,6 +94,9 @@ gravity_DNSLookup() {
|
||||||
|
|
||||||
# Try again
|
# Try again
|
||||||
gravity_DNSLookup
|
gravity_DNSLookup
|
||||||
|
elif [[ -n "${secs}" ]]; then
|
||||||
|
# Print confirmation of resolvability if it had previously failed
|
||||||
|
echo -e "${OVER} ${TICK} DNS resolution is now available\\n"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +110,7 @@ gravity_Collapse() {
|
||||||
rm "${adListDefault}" 2> /dev/null || \
|
rm "${adListDefault}" 2> /dev/null || \
|
||||||
echo -e " ${CROSS} Unable to remove ${adListDefault}"
|
echo -e " ${CROSS} Unable to remove ${adListDefault}"
|
||||||
elif [[ ! -f "${adListFile}" ]]; then
|
elif [[ ! -f "${adListFile}" ]]; then
|
||||||
# Create "adlists.list"
|
# Create "adlists.list" by copying "adlists.default" from internal Pi-hole repo
|
||||||
cp "${adListRepoDefault}" "${adListFile}" 2> /dev/null || \
|
cp "${adListRepoDefault}" "${adListFile}" 2> /dev/null || \
|
||||||
echo -e " ${CROSS} Unable to copy ${adListFile##*/} from ${piholeRepo}"
|
echo -e " ${CROSS} Unable to copy ${adListFile##*/} from ${piholeRepo}"
|
||||||
fi
|
fi
|
||||||
|
@ -115,11 +119,11 @@ gravity_Collapse() {
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
# Retrieve source URLs from $adListFile
|
# Retrieve source URLs from $adListFile
|
||||||
# Logic: Remove comments, CR line endings and empty lines
|
# Awk Logic: Remove comments, CR line endings and empty lines
|
||||||
mapfile -t sources < <(awk '!/^[#@;!\[]/ {gsub(/\r$/, "", $0); if ($1) { print $1 } }' "${adListFile}" 2> /dev/null)
|
mapfile -t sources < <(awk '!/^[#@;!\[]/ {gsub(/\r$/, "", $0); if ($1) { print $1 } }' "${adListFile}" 2> /dev/null)
|
||||||
|
|
||||||
# Parse source domains from $sources
|
# Parse source domains from $sources
|
||||||
# Logic: Split by folder/port, remove URL protocol & optional username:password@
|
# Awk Logic: Split by folder/port, remove URL protocol & optional username:password@
|
||||||
mapfile -t sourceDomains < <(
|
mapfile -t sourceDomains < <(
|
||||||
awk -F '[/:]' '{
|
awk -F '[/:]' '{
|
||||||
gsub(/(.*:\/\/|.*:.*@)/, "", $0)
|
gsub(/(.*:\/\/|.*:.*@)/, "", $0)
|
||||||
|
@ -150,9 +154,10 @@ gravity_Supernova() {
|
||||||
saveLocation="${piholeDir}/list.${i}.${domain}.${domainsExtension}"
|
saveLocation="${piholeDir}/list.${i}.${domain}.${domainsExtension}"
|
||||||
activeDomains[$i]="${saveLocation}"
|
activeDomains[$i]="${saveLocation}"
|
||||||
|
|
||||||
|
# Default user-agent (for Cloudflare's Browser Integrity Check: https://support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do-)
|
||||||
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"
|
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"
|
||||||
|
|
||||||
# Use a case statement to download lists that need special commands
|
# Provide special commands for blocklists which may need them
|
||||||
case "${domain}" in
|
case "${domain}" in
|
||||||
"pgl.yoyo.org") cmd_ext="-d mimetype=plaintext -d hostformat=hosts";;
|
"pgl.yoyo.org") cmd_ext="-d mimetype=plaintext -d hostformat=hosts";;
|
||||||
*) cmd_ext="";;
|
*) cmd_ext="";;
|
||||||
|
@ -179,9 +184,10 @@ gravity_Pull() {
|
||||||
|
|
||||||
# Store downloaded content to temp file instead of RAM
|
# Store downloaded content to temp file instead of RAM
|
||||||
patternBuffer=$(mktemp)
|
patternBuffer=$(mktemp)
|
||||||
|
|
||||||
heisenbergCompensator=""
|
heisenbergCompensator=""
|
||||||
if [[ -r "${saveLocation}" ]]; then
|
if [[ -r "${saveLocation}" ]]; then
|
||||||
# If domain has been saved, add file for date check to only download newer
|
# Allow curl to determine if a remote file has been modified since last retrieval
|
||||||
heisenbergCompensator="-z ${saveLocation}"
|
heisenbergCompensator="-z ${saveLocation}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -191,7 +197,7 @@ gravity_Pull() {
|
||||||
httpCode=$(curl -s -L ${cmd_ext} ${heisenbergCompensator} -w "%{http_code}" -A "${agent}" "${url}" -o "${patternBuffer}" 2> /dev/null)
|
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
|
# Determine "Status:" output based on HTTP response
|
||||||
case "$httpCode" in
|
case "${httpCode}" in
|
||||||
"200" ) echo -e "${OVER} ${TICK} ${str} Retrieval successful"; success="true";;
|
"200" ) echo -e "${OVER} ${TICK} ${str} Retrieval successful"; success="true";;
|
||||||
"304" ) echo -e "${OVER} ${TICK} ${str} No changes detected"; success="true";;
|
"304" ) echo -e "${OVER} ${TICK} ${str} No changes detected"; success="true";;
|
||||||
"403" ) echo -e "${OVER} ${CROSS} ${str} Forbidden"; success="false";;
|
"403" ) echo -e "${OVER} ${CROSS} ${str} Forbidden"; success="false";;
|
||||||
|
@ -201,7 +207,7 @@ gravity_Pull() {
|
||||||
"521" ) echo -e "${OVER} ${CROSS} ${str} Web Server Is Down (Cloudflare)"; 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";;
|
"522" ) echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Cloudflare)"; success="false";;
|
||||||
"500" ) echo -e "${OVER} ${CROSS} ${str} Internal Server Error"; success="false";;
|
"500" ) echo -e "${OVER} ${CROSS} ${str} Internal Server Error"; success="false";;
|
||||||
* ) echo -e "${OVER} ${CROSS} ${str} Status $httpCode"; success="false";;
|
* ) echo -e "${OVER} ${CROSS} ${str} Status ${httpCode}"; success="false";;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Determine if the blocklist was downloaded and saved correctly
|
# Determine if the blocklist was downloaded and saved correctly
|
||||||
|
@ -242,14 +248,14 @@ gravity_ParseFileIntoDomains() {
|
||||||
if [[ "${source}" == "${piholeDir}/${matterAndLight}" ]]; then
|
if [[ "${source}" == "${piholeDir}/${matterAndLight}" ]]; then
|
||||||
# Consolidated list parsing: Remove comments and hosts IP's
|
# Consolidated list parsing: Remove comments and hosts IP's
|
||||||
|
|
||||||
# Symbols used as comments: #;@![/
|
# Define symbols used as comments: #;@![/
|
||||||
commentPattern="[#;@![\\/]"
|
commentPattern="[#;@![\\/]"
|
||||||
|
|
||||||
# Logic: Process lines which do not begin with comments
|
# Awk Logic: Process lines which do not begin with comments
|
||||||
awk '!/^'"${commentPattern}"'/ {
|
awk '!/^'"${commentPattern}"'/ {
|
||||||
# If there are multiple words seperated by space
|
# If there are multiple words seperated by space
|
||||||
if (NF>1) {
|
if (NF>1) {
|
||||||
# Remove comments (Inc. prefixed spaces/tabs)
|
# Remove comments (including prefixed spaces/tabs)
|
||||||
if ($0 ~ /'"${commentPattern}"'/) { gsub("( |\t)'"${commentPattern}"'.*", "", $0) }
|
if ($0 ~ /'"${commentPattern}"'/) { gsub("( |\t)'"${commentPattern}"'.*", "", $0) }
|
||||||
# Print consecutive domains
|
# Print consecutive domains
|
||||||
if ($3) {
|
if ($3) {
|
||||||
|
@ -271,8 +277,9 @@ gravity_ParseFileIntoDomains() {
|
||||||
read -r firstLine < "${source}"
|
read -r firstLine < "${source}"
|
||||||
|
|
||||||
# Determine how to parse individual source file formats
|
# Determine how to parse individual source file formats
|
||||||
|
# Lists may not capitalise the first line correctly, so compare strings against lower case
|
||||||
if [[ "${firstLine,,}" =~ "adblock" ]] || [[ "${firstLine,,}" =~ "ublock" ]] || [[ "${firstLine,,}" =~ "! checksum" ]]; then
|
if [[ "${firstLine,,}" =~ "adblock" ]] || [[ "${firstLine,,}" =~ "ublock" ]] || [[ "${firstLine,,}" =~ "! checksum" ]]; then
|
||||||
# Logic: Parse Adblock domains & comments: https://adblockplus.org/filter-cheatsheet
|
# Awk Logic: Parse Adblock domains & comments: https://adblockplus.org/filter-cheatsheet
|
||||||
abpFilter="/^(\\[|!)|^(\\|\\|.*\\^)/"
|
abpFilter="/^(\\[|!)|^(\\|\\|.*\\^)/"
|
||||||
awk ''"${abpFilter}"' {
|
awk ''"${abpFilter}"' {
|
||||||
# Remove valid adblock type options
|
# Remove valid adblock type options
|
||||||
|
@ -338,9 +345,10 @@ gravity_Filter() {
|
||||||
str="Extracting domains from blocklists"
|
str="Extracting domains from blocklists"
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
# Parse files as hosts
|
# Parse into hosts file
|
||||||
gravity_ParseFileIntoDomains "${piholeDir}/${matterAndLight}" "${piholeDir}/${parsedMatter}"
|
gravity_ParseFileIntoDomains "${piholeDir}/${matterAndLight}" "${piholeDir}/${parsedMatter}"
|
||||||
|
|
||||||
|
# Format file line count as currency
|
||||||
num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${parsedMatter}")")
|
num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${parsedMatter}")")
|
||||||
echo -e "${OVER} ${TICK} ${str}
|
echo -e "${OVER} ${TICK} ${str}
|
||||||
${INFO} ${COL_LIGHT_BLUE}${num}${COL_NC} domains being pulled in by gravity"
|
${INFO} ${COL_LIGHT_BLUE}${num}${COL_NC} domains being pulled in by gravity"
|
||||||
|
@ -357,6 +365,7 @@ gravity_Unique() {
|
||||||
sort -u "${piholeDir}/${parsedMatter}" > "${piholeDir}/${preEventHorizon}"
|
sort -u "${piholeDir}/${parsedMatter}" > "${piholeDir}/${preEventHorizon}"
|
||||||
echo -e "${OVER} ${TICK} ${str}"
|
echo -e "${OVER} ${TICK} ${str}"
|
||||||
|
|
||||||
|
# Format file line count as currency
|
||||||
num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${preEventHorizon}")")
|
num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${preEventHorizon}")")
|
||||||
echo -e " ${INFO} ${COL_LIGHT_BLUE}${num}${COL_NC} unique domains trapped in the Event Horizon"
|
echo -e " ${INFO} ${COL_LIGHT_BLUE}${num}${COL_NC} unique domains trapped in the Event Horizon"
|
||||||
}
|
}
|
||||||
|
@ -371,6 +380,7 @@ gravity_WhitelistBLD() {
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
# Create array of unique $sourceDomains
|
# Create array of unique $sourceDomains
|
||||||
|
# Disable SC2046 as quoting will only return first domain
|
||||||
# shellcheck disable=SC2046
|
# shellcheck disable=SC2046
|
||||||
read -r -a uniqDomains <<< $(awk '{ if(!a[$1]++) { print $1 } }' <<< "$(printf '%s\n' "${sourceDomains[@]}")")
|
read -r -a uniqDomains <<< $(awk '{ if(!a[$1]++) { print $1 } }' <<< "$(printf '%s\n' "${sourceDomains[@]}")")
|
||||||
|
|
||||||
|
@ -388,7 +398,7 @@ gravity_Whitelist() {
|
||||||
# Remove anything in whitelist.txt from the Event Horizon
|
# Remove anything in whitelist.txt from the Event Horizon
|
||||||
num=$(wc -l < "${whitelistFile}")
|
num=$(wc -l < "${whitelistFile}")
|
||||||
plural=; [[ "${num}" != "1" ]] && plural=s
|
plural=; [[ "${num}" != "1" ]] && plural=s
|
||||||
local str="Whitelisting ${num} domain${plural}"
|
str="Whitelisting ${num} domain${plural}"
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
# Print everything from preEventHorizon into whitelistMatter EXCEPT domains in whitelist.txt
|
# Print everything from preEventHorizon into whitelistMatter EXCEPT domains in whitelist.txt
|
||||||
|
@ -429,6 +439,7 @@ gravity_ShowBlockCount() {
|
||||||
# Parse list of domains into hosts format
|
# Parse list of domains into hosts format
|
||||||
gravity_ParseDomainsIntoHosts() {
|
gravity_ParseDomainsIntoHosts() {
|
||||||
if [[ -n "${IPV4_ADDRESS}" ]] || [[ -n "${IPV6_ADDRESS}" ]]; then
|
if [[ -n "${IPV4_ADDRESS}" ]] || [[ -n "${IPV6_ADDRESS}" ]]; then
|
||||||
|
# Awk Logic: Remove CR line endings and print IP before domain if IPv4/6 is used
|
||||||
awk -v ipv4addr="$IPV4_ADDRESS" -v ipv6addr="$IPV6_ADDRESS" '{
|
awk -v ipv4addr="$IPV4_ADDRESS" -v ipv6addr="$IPV6_ADDRESS" '{
|
||||||
sub(/\r$/, "")
|
sub(/\r$/, "")
|
||||||
if(ipv4addr) { print ipv4addr" "$0; }
|
if(ipv4addr) { print ipv4addr" "$0; }
|
||||||
|
@ -447,7 +458,7 @@ gravity_ParseLocalDomains() {
|
||||||
|
|
||||||
if [[ -f "/etc/hostname" ]]; then
|
if [[ -f "/etc/hostname" ]]; then
|
||||||
hostname=$(< "/etc/hostname")
|
hostname=$(< "/etc/hostname")
|
||||||
elif command -v hostname &> /dev/null; then
|
elif command -v hostname > /dev/null; then
|
||||||
hostname=$(hostname -f)
|
hostname=$(hostname -f)
|
||||||
else
|
else
|
||||||
echo -e " ${CROSS} Unable to determine fully qualified domain name of host"
|
echo -e " ${CROSS} Unable to determine fully qualified domain name of host"
|
||||||
|
@ -471,8 +482,7 @@ gravity_ParseBlacklistDomains() {
|
||||||
status="$?"
|
status="$?"
|
||||||
|
|
||||||
if [[ "${status}" -ne 0 ]]; then
|
if [[ "${status}" -ne 0 ]]; then
|
||||||
echo -e " ${CROSS} Unable to move ${accretionDisc} from ${piholeDir}
|
echo -e " ${CROSS} Unable to move ${accretionDisc} from ${piholeDir}\\n ${output}"
|
||||||
${output}"
|
|
||||||
gravity_Cleanup "error"
|
gravity_Cleanup "error"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
@ -507,7 +517,7 @@ gravity_Cleanup() {
|
||||||
# Remove any unused .domains files
|
# Remove any unused .domains files
|
||||||
for file in ${piholeDir}/*.${domainsExtension}; do
|
for file in ${piholeDir}/*.${domainsExtension}; do
|
||||||
# If list is not in active array, then remove it
|
# If list is not in active array, then remove it
|
||||||
if [[ ! "${activeDomains[*]}" =~ ${file} ]]; then
|
if [[ ! "${activeDomains[*]}" =~ "${file}" ]]; then
|
||||||
rm -f "${file}" 2> /dev/null || \
|
rm -f "${file}" 2> /dev/null || \
|
||||||
echo -e " ${CROSS} Failed to remove ${file##*/}"
|
echo -e " ${CROSS} Failed to remove ${file##*/}"
|
||||||
fi
|
fi
|
||||||
|
@ -515,7 +525,7 @@ gravity_Cleanup() {
|
||||||
|
|
||||||
echo -e "${OVER} ${TICK} ${str}"
|
echo -e "${OVER} ${TICK} ${str}"
|
||||||
|
|
||||||
[[ -n "$error" ]] && echo ""
|
[[ -n "${error}" ]] && echo ""
|
||||||
|
|
||||||
# Only restart DNS service if offline
|
# Only restart DNS service if offline
|
||||||
if ! pidof dnsmasq &> /dev/null; then
|
if ! pidof dnsmasq &> /dev/null; then
|
||||||
|
@ -523,7 +533,7 @@ gravity_Cleanup() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Print Pi-hole status if an error occured
|
# Print Pi-hole status if an error occured
|
||||||
if [[ -n "$error" ]]; then
|
if [[ -n "${error}" ]]; then
|
||||||
"${PIHOLE_COMMAND}" status
|
"${PIHOLE_COMMAND}" status
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue