mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 02:42:58 +00:00
Add 'Connection Refused' for 000 status
* Shift default dnsRestart value into unset default parameter expansion value * Change nslookup timeout to 5 seconds * Use &> instead of > * Standardise plural code * Update some comments * Add "000" when connection is refused * Condense adblock detection logic * Add Dnsmasq format detection and parsing logic * Removed unnecessary echo * Add dnsWasOffline variable to ensure that if DNS service has been stopped, that it doesn't start and also get reloaded
This commit is contained in:
parent
d3073e5e23
commit
ff5411a93a
1 changed files with 51 additions and 38 deletions
89
gravity.sh
89
gravity.sh
|
@ -43,9 +43,6 @@ preEventHorizon="list.preEventHorizon"
|
||||||
|
|
||||||
skipDownload="false"
|
skipDownload="false"
|
||||||
|
|
||||||
# Use "force-reload" when restarting dnsmasq for everything but Wildcards
|
|
||||||
dnsRestart="force-reload"
|
|
||||||
|
|
||||||
# Source setupVars from install script
|
# Source setupVars from install script
|
||||||
setupVars="${piholeDir}/setupVars.conf"
|
setupVars="${piholeDir}/setupVars.conf"
|
||||||
if [[ -f "${setupVars}" ]];then
|
if [[ -f "${setupVars}" ]];then
|
||||||
|
@ -67,9 +64,10 @@ fi
|
||||||
|
|
||||||
# Determine if DNS resolution is available before proceeding with retrieving blocklists
|
# Determine if DNS resolution is available before proceeding with retrieving blocklists
|
||||||
gravity_DNSLookup() {
|
gravity_DNSLookup() {
|
||||||
local lookupDomain plu
|
local lookupDomain plural
|
||||||
|
|
||||||
# Determine which domain to resolve depending on existence of $localList
|
# Determine which domain should be resolved
|
||||||
|
# "pi.hole" is not always available (e.g: new install), but FTL will not log it
|
||||||
if [[ -e "${localList}" ]]; then
|
if [[ -e "${localList}" ]]; then
|
||||||
lookupDomain="pi.hole"
|
lookupDomain="pi.hole"
|
||||||
else
|
else
|
||||||
|
@ -77,25 +75,25 @@ gravity_DNSLookup() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Determine if domain can be resolved
|
# Determine if domain can be resolved
|
||||||
if ! timeout 10 nslookup "${lookupDomain}" > /dev/null; then
|
if ! timeout 5 nslookup "${lookupDomain}" &> /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
|
||||||
|
|
||||||
# Give time for dnsmasq to be resolvable
|
# Give time for DNS server to be resolvable
|
||||||
secs="30"
|
secs="30"
|
||||||
while [[ "${secs}" -ne 0 ]]; do
|
while [[ "${secs}" -ne 0 ]]; do
|
||||||
[[ "${secs}" -ne 1 ]] && plu="s" || plu=""
|
plural=; [[ "${secs}" -ne 1 ]] && plural="s"
|
||||||
echo -ne "${OVER} ${INFO} Waiting $secs second${plu} before continuing..."
|
echo -ne "${OVER} ${INFO} Waiting $secs second${plural} before continuing..."
|
||||||
sleep 1
|
sleep 1
|
||||||
: $((secs--))
|
: $((secs--))
|
||||||
done
|
done
|
||||||
|
@ -127,7 +125,7 @@ gravity_Collapse() {
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
# Retrieve source URLs from $adListFile
|
# Retrieve source URLs from $adListFile
|
||||||
# Awk Logic: Remove comments, CR line endings and empty lines
|
# Awk Logic: Remove comments (#@;![), CR (windows) 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
|
||||||
|
@ -182,7 +180,7 @@ gravity_Supernova() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Download specified URL and perform QA
|
# Download specified URL and perform checks on HTTP status and file content
|
||||||
gravity_Pull() {
|
gravity_Pull() {
|
||||||
local url cmd_ext agent heisenbergCompensator patternBuffer str httpCode success
|
local url cmd_ext agent heisenbergCompensator patternBuffer str httpCode success
|
||||||
|
|
||||||
|
@ -195,7 +193,8 @@ gravity_Pull() {
|
||||||
|
|
||||||
heisenbergCompensator=""
|
heisenbergCompensator=""
|
||||||
if [[ -r "${saveLocation}" ]]; then
|
if [[ -r "${saveLocation}" ]]; then
|
||||||
# Allow curl to determine if a remote file has been modified since last retrieval
|
# Make curl determine if a remote file has been modified since last retrieval
|
||||||
|
# Uses "Last-Modified" header, which certain web servers do not provide (e.g: raw github links)
|
||||||
heisenbergCompensator="-z ${saveLocation}"
|
heisenbergCompensator="-z ${saveLocation}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -206,28 +205,29 @@ gravity_Pull() {
|
||||||
|
|
||||||
# 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";;
|
"000") echo -e "${OVER} ${CROSS} ${str} Connection Refused"; success="false";;
|
||||||
"404" ) echo -e "${OVER} ${CROSS} ${str} Not found"; success="false";;
|
"403") echo -e "${OVER} ${CROSS} ${str} Forbidden"; success="false";;
|
||||||
"408" ) echo -e "${OVER} ${CROSS} ${str} Time-out"; success="false";;
|
"404") echo -e "${OVER} ${CROSS} ${str} Not found"; success="false";;
|
||||||
"451" ) echo -e "${OVER} ${CROSS} ${str} Unavailable For Legal Reasons"; success="false";;
|
"408") echo -e "${OVER} ${CROSS} ${str} Time-out"; success="false";;
|
||||||
"521" ) echo -e "${OVER} ${CROSS} ${str} Web Server Is Down (Cloudflare)"; success="false";;
|
"451") echo -e "${OVER} ${CROSS} ${str} Unavailable For Legal Reasons"; success="false";;
|
||||||
"522" ) echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Cloudflare)"; success="false";;
|
"521") echo -e "${OVER} ${CROSS} ${str} Web Server Is Down (Cloudflare)"; success="false";;
|
||||||
"500" ) echo -e "${OVER} ${CROSS} ${str} Internal Server Error"; success="false";;
|
"522") echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Cloudflare)"; success="false";;
|
||||||
* ) echo -e "${OVER} ${CROSS} ${str} Status ${httpCode}"; success="false";;
|
"500") echo -e "${OVER} ${CROSS} ${str} Internal Server Error"; success="false";;
|
||||||
|
* ) echo -e "${OVER} ${CROSS} ${str} ${httpCode}"; success="false";;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Determine if the blocklist was downloaded and saved correctly
|
# Determine if the blocklist was downloaded and saved correctly
|
||||||
if [[ "${success}" == "true" ]]; then
|
if [[ "${success}" == "true" ]]; then
|
||||||
if [[ "${httpCode}" == "304" ]]; then
|
if [[ "${httpCode}" == "304" ]]; then
|
||||||
: # Do nothing
|
: # Do not attempt to re-parse file
|
||||||
# Check if patternbuffer is a non-zero length file
|
# Check if $patternbuffer is a non-zero length file
|
||||||
elif [[ -s "${patternBuffer}" ]]; then
|
elif [[ -s "${patternBuffer}" ]]; then
|
||||||
# Determine if blocklist is non-standard and parse as appropriate
|
# Determine if blocklist is non-standard and parse as appropriate
|
||||||
gravity_ParseFileIntoDomains "${patternBuffer}" "${saveLocation}"
|
gravity_ParseFileIntoDomains "${patternBuffer}" "${saveLocation}"
|
||||||
else
|
else
|
||||||
# Fall back to previously cached list if patternBuffer is empty
|
# Fall back to previously cached list if $patternBuffer is empty
|
||||||
echo -e " ${INFO} Received empty file: ${COL_LIGHT_GREEN}using previously cached list${COL_NC}"
|
echo -e " ${INFO} Received empty file: ${COL_LIGHT_GREEN}using previously cached list${COL_NC}"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
|
@ -248,9 +248,7 @@ gravity_Pull() {
|
||||||
|
|
||||||
# Parse non-standard source files into domains-only format
|
# Parse non-standard source files into domains-only format
|
||||||
gravity_ParseFileIntoDomains() {
|
gravity_ParseFileIntoDomains() {
|
||||||
local source destination commentPattern firstLine abpFilter
|
local source="${1}" destination="${2}" commentPattern firstLine abpFilter
|
||||||
source="${1}"
|
|
||||||
destination="${2}"
|
|
||||||
|
|
||||||
# Determine how to parse source file
|
# Determine how to parse source file
|
||||||
if [[ "${source}" == "${piholeDir}/${matterAndLight}" ]]; then
|
if [[ "${source}" == "${piholeDir}/${matterAndLight}" ]]; then
|
||||||
|
@ -286,7 +284,7 @@ gravity_ParseFileIntoDomains() {
|
||||||
|
|
||||||
# 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
|
# 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|ublock|!checksum) ]]; then
|
||||||
# Awk 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}"' {
|
||||||
|
@ -298,8 +296,8 @@ gravity_ParseFileIntoDomains() {
|
||||||
if ($0 ~ /(^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|[\\^\/\*])/) { $0="" }
|
if ($0 ~ /(^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|[\\^\/\*])/) { $0="" }
|
||||||
if ($0) { print $0 }
|
if ($0) { print $0 }
|
||||||
}' "${source}" 2> /dev/null > "${destination}"
|
}' "${source}" 2> /dev/null > "${destination}"
|
||||||
|
# Parse URL list if source file contains http:// or IPv4
|
||||||
elif grep -q -E "^(https?://|([0-9]{1,3}\\.){3}[0-9]{1,3}$)" "${source}" &> /dev/null; then
|
elif grep -q -E "^(https?://|([0-9]{1,3}\\.){3}[0-9]{1,3}$)" "${source}" &> /dev/null; then
|
||||||
# Parse URLs if source file contains http:// or IPv4
|
|
||||||
awk '{
|
awk '{
|
||||||
# Remove URL protocol, optional "username:password@", and ":?/;"
|
# Remove URL protocol, optional "username:password@", and ":?/;"
|
||||||
if ($0 ~ /[:?\/;]/) { gsub(/(^.*:\/\/(.*:.*@)?|[:?\/;].*)/, "", $0) }
|
if ($0 ~ /[:?\/;]/) { gsub(/(^.*:\/\/(.*:.*@)?|[:?\/;].*)/, "", $0) }
|
||||||
|
@ -307,6 +305,17 @@ gravity_ParseFileIntoDomains() {
|
||||||
if ($0 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { $0="" }
|
if ($0 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { $0="" }
|
||||||
if ($0) { print $0 }
|
if ($0) { print $0 }
|
||||||
}' "${source}" 2> /dev/null > "${destination}"
|
}' "${source}" 2> /dev/null > "${destination}"
|
||||||
|
# Parse Dnsmasq format lists
|
||||||
|
elif grep -q "^address=/" "${source}" &> /dev/null; then
|
||||||
|
awk -F/ '{
|
||||||
|
# Print comments
|
||||||
|
if ($0 ~ "#") {
|
||||||
|
print $0
|
||||||
|
# Print domains
|
||||||
|
} else if ($2) {
|
||||||
|
print $2
|
||||||
|
}
|
||||||
|
}' "${source}" 2> /dev/null > "${destination}"
|
||||||
else
|
else
|
||||||
# Keep hosts/domains file in same format as it was downloaded
|
# Keep hosts/domains file in same format as it was downloaded
|
||||||
output=$( { mv "${source}" "${destination}"; } 2>&1 )
|
output=$( { mv "${source}" "${destination}"; } 2>&1 )
|
||||||
|
@ -383,7 +392,7 @@ gravity_WhitelistBLD() {
|
||||||
local plural str uniqDomains
|
local plural str uniqDomains
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
plural=; [[ "${#sources[*]}" != "1" ]] && plural=s
|
plural=; [[ "${#sources[*]}" != "1" ]] && plural="s"
|
||||||
str="Adding blocklist source${plural} to the whitelist"
|
str="Adding blocklist source${plural} to the whitelist"
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
|
@ -403,7 +412,7 @@ gravity_Whitelist() {
|
||||||
if [[ -f "${whitelistFile}" ]]; then
|
if [[ -f "${whitelistFile}" ]]; then
|
||||||
# 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}" -ne 1 ]] && plural="s"
|
||||||
str="Whitelisting ${num} domain${plural}"
|
str="Whitelisting ${num} domain${plural}"
|
||||||
echo -ne " ${INFO} ${str}..."
|
echo -ne " ${INFO} ${str}..."
|
||||||
|
|
||||||
|
@ -422,7 +431,7 @@ gravity_ShowBlockCount() {
|
||||||
|
|
||||||
if [[ -f "${blacklistFile}" ]]; then
|
if [[ -f "${blacklistFile}" ]]; then
|
||||||
num=$(printf "%'.0f" "$(wc -l < "${blacklistFile}")")
|
num=$(printf "%'.0f" "$(wc -l < "${blacklistFile}")")
|
||||||
plural=; [[ "${num}" != "1" ]] && plural=s
|
plural=; [[ "${num}" -ne 1 ]] && plural="s"
|
||||||
str="Exact blocked domain${plural}: ${num}"
|
str="Exact blocked domain${plural}: ${num}"
|
||||||
echo -e " ${INFO} ${str}"
|
echo -e " ${INFO} ${str}"
|
||||||
else
|
else
|
||||||
|
@ -435,7 +444,7 @@ gravity_ShowBlockCount() {
|
||||||
if [[ -n "${IPV4_ADDRESS}" ]] && [[ -n "${IPV6_ADDRESS}" ]];then
|
if [[ -n "${IPV4_ADDRESS}" ]] && [[ -n "${IPV6_ADDRESS}" ]];then
|
||||||
num=$(( num/2 ))
|
num=$(( num/2 ))
|
||||||
fi
|
fi
|
||||||
plural=; [[ "${num}" != "1" ]] && plural=s
|
plural=; [[ "${num}" -ne 1 ]] && plural="s"
|
||||||
echo -e " ${INFO} Wildcard blocked domain${plural}: ${num}"
|
echo -e " ${INFO} Wildcard blocked domain${plural}: ${num}"
|
||||||
else
|
else
|
||||||
echo -e " ${INFO} No wildcards used!"
|
echo -e " ${INFO} No wildcards used!"
|
||||||
|
@ -537,11 +546,10 @@ gravity_Cleanup() {
|
||||||
|
|
||||||
echo -e "${OVER} ${TICK} ${str}"
|
echo -e "${OVER} ${TICK} ${str}"
|
||||||
|
|
||||||
[[ -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
|
||||||
"${PIHOLE_COMMAND}" restartdns
|
"${PIHOLE_COMMAND}" restartdns
|
||||||
|
dnsWasOffline=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Print Pi-hole status if an error occured
|
# Print Pi-hole status if an error occured
|
||||||
|
@ -634,5 +642,10 @@ if [[ "${skipDownload}" == false ]]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
"${PIHOLE_COMMAND}" restartdns "${dnsRestart}"
|
|
||||||
|
# Determine if DNS has been restarted by this instance of gravity
|
||||||
|
if [[ -z "${dnsWasOffline:-}" ]]; then
|
||||||
|
# Use "force-reload" when restarting dnsmasq for everything but Wildcards
|
||||||
|
"${PIHOLE_COMMAND}" restartdns "${dnsRestart:-force-reload}"
|
||||||
|
fi
|
||||||
"${PIHOLE_COMMAND}" status
|
"${PIHOLE_COMMAND}" status
|
||||||
|
|
Loading…
Reference in a new issue