From 5f2217a0791a1232c7af911e6b2b98ebd341d135 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Mon, 30 Apr 2018 21:45:03 +0100 Subject: [PATCH] Fix for gravity.list not being emptied with an empty/missing adlist file. Variable output depending on content of /existence of adlist Signed-off-by: Adam Warner --- gravity.sh | 57 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/gravity.sh b/gravity.sh index 05f4d972..58419029 100755 --- a/gravity.sh +++ b/gravity.sh @@ -44,6 +44,8 @@ skipDownload="false" resolver="pihole-FTL" +haveSourceUrls=true + # Source setupVars from install script setupVars="${piholeDir}/setupVars.conf" if [[ -f "${setupVars}" ]];then @@ -129,18 +131,12 @@ gravity_CheckDNSResolutionAvailable() { gravity_GetBlocklistUrls() { echo -e " ${INFO} ${COL_BOLD}Neutrino emissions detected${COL_NC}..." - # Determine if adlists file needs handling - if [[ ! -f "${adListFile}" ]]; then - echo -e " ${CROSS} No adlists file found. Nothing to do!" - elif [[ -f "${adListDefault}" ]] && [[ -f "${adListFile}" ]]; then + if [[ -f "${adListDefault}" ]] && [[ -f "${adListFile}" ]]; then # Remove superceded $adListDefault file rm "${adListDefault}" 2> /dev/null || \ echo -e " ${CROSS} Unable to remove ${adListDefault}" fi - local str="Pulling blocklist source list into range" - echo -ne " ${INFO} ${str}..." - # Retrieve source URLs from $adListFile # Logic: Remove comments and empty lines mapfile -t sources <<< "$(grep -v -E "^(#|$)" "${adListFile}" 2> /dev/null)" @@ -156,11 +152,15 @@ gravity_GetBlocklistUrls() { }' <<< "$(printf '%s\n' "${sources[@]}")" 2> /dev/null )" + local str="Pulling blocklist source list into range" + if [[ -n "${sources[*]}" ]] && [[ -n "${sourceDomains[*]}" ]]; then echo -e "${OVER} ${TICK} ${str}" else echo -e "${OVER} ${CROSS} ${str}" - gravity_Cleanup "error" + echo -e " ${INFO} No source list found, or it is empty" + echo "" + haveSourceUrls=false fi } @@ -374,7 +374,9 @@ gravity_ConsolidateDownloadedBlocklists() { local str lastLine str="Consolidating blocklists" - echo -ne " ${INFO} ${str}..." + if [[ "${haveSourceUrls}" == true ]]; then + echo -ne " ${INFO} ${str}..." + fi # Empty $matterAndLight if it already exists, otherwise, create it : > "${piholeDir}/${matterAndLight}" @@ -393,8 +395,9 @@ gravity_ConsolidateDownloadedBlocklists() { fi fi done - - echo -e "${OVER} ${TICK} ${str}" + if [[ "${haveSourceUrls}" == true ]]; then + echo -e "${OVER} ${TICK} ${str}" + fi } # Parse consolidated list into (filtered, unique) domains-only format @@ -402,24 +405,33 @@ gravity_SortAndFilterConsolidatedList() { local str num str="Extracting domains from blocklists" - echo -ne " ${INFO} ${str}..." + if [[ "${haveSourceUrls}" == true ]]; then + echo -ne " ${INFO} ${str}..." + fi # Parse into hosts file gravity_ParseFileIntoDomains "${piholeDir}/${matterAndLight}" "${piholeDir}/${parsedMatter}" # Format $parsedMatter line total as currency num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${parsedMatter}")") - echo -e "${OVER} ${TICK} ${str} - ${INFO} Number of domains being pulled in by gravity: ${COL_BLUE}${num}${COL_NC}" + if [[ "${haveSourceUrls}" == true ]]; then + echo -e "${OVER} ${TICK} ${str}" + fi + echo -e " ${INFO} Number of domains being pulled in by gravity: ${COL_BLUE}${num}${COL_NC}" str="Removing duplicate domains" - echo -ne " ${INFO} ${str}..." - sort -u "${piholeDir}/${parsedMatter}" > "${piholeDir}/${preEventHorizon}" - echo -e "${OVER} ${TICK} ${str}" + if [[ "${haveSourceUrls}" == true ]]; then + echo -ne " ${INFO} ${str}..." + fi - # Format $preEventHorizon line total as currency - num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${preEventHorizon}")") - echo -e " ${INFO} Number of unique domains trapped in the Event Horizon: ${COL_BLUE}${num}${COL_NC}" + sort -u "${piholeDir}/${parsedMatter}" > "${piholeDir}/${preEventHorizon}" + + if [[ "${haveSourceUrls}" == true ]]; then + echo -e "${OVER} ${TICK} ${str}" + # Format $preEventHorizon line total as currency + num=$(printf "%'.0f" "$(wc -l < "${piholeDir}/${preEventHorizon}")") + echo -e " ${INFO} Number of unique domains trapped in the Event Horizon: ${COL_BLUE}${num}${COL_NC}" + fi } # Whitelist user-defined domains @@ -531,7 +543,6 @@ gravity_ParseUserDomains() { if [[ ! -f "${blacklistFile}" ]]; then return 0 fi - # Copy the file over as /etc/pihole/black.list so dnsmasq can use it cp "${blacklistFile}" "${blackList}" 2> /dev/null || \ echo -e "\\n ${CROSS} Unable to move ${blacklistFile##*/} to ${piholeDir}" @@ -618,7 +629,9 @@ if [[ "${skipDownload}" == false ]]; then # Gravity needs to download blocklists gravity_CheckDNSResolutionAvailable gravity_GetBlocklistUrls - gravity_SetDownloadOptions + if [[ "${haveSourceUrls}" == true ]]; then + gravity_SetDownloadOptions + fi gravity_ConsolidateDownloadedBlocklists gravity_SortAndFilterConsolidatedList else