Skip to content

Commit

Permalink
Display a warning and option to run gravity if a mismatch between ena…
Browse files Browse the repository at this point in the history
…bles adlists and data in gravity is detected
  • Loading branch information
yubiuser committed Aug 27, 2020
1 parent 409be37 commit 8dab718
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion pihole_adlist_tool
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ CNAME_AVAILABLE=
BLACKLIST_CNAME=
SQLITE_VERSION=
NUM_DOMAINS_BLOCKED_FUTURE=
declare -a adlist_enabled_in_gravity
declare -i run_gravity_now


#for text formating
bold=$(tput bold)
Expand Down Expand Up @@ -298,6 +301,46 @@ echo

if [ "$menu_selection" -eq 1 ]; then
echo " [i] Keeping current adlist configuration"


# check if a mismatch between enabled adlists and data in gravity exists, offer to run gravity
# https://stackoverflow.com/a/28161520
adlist_enabled_in_gravity=(`sqlite3 $GRAVITY "select distinct adlist_id from gravity;"`)
if [ -n "$(echo ${adlist_conf_old_enabled[@]} ${adlist_enabled_in_gravity[@]} | tr ' ' '\n' |sort |uniq -u)" ]; then

echo
echo " [i] There is a mismatch between your enabled adlists and the data found in the gravity database."
echo " You have ${bold}"${#adlist_conf_old_enabled[@]}"${normal} adlists enabled, but data from ${bold}"${#adlist_enabled_in_gravity[@]}"${normal} (patially different) adlists in your gravity database."
echo " You're likely disabled/enabled adlist without running gravity afterwards."
echo " It's highly recommended to run gravity now to solve the differences, otherwise this tool will analyze the available data."
echo
echo
echo " Would you like to run gravity now?"
echo
echo " 1) Yes"
echo " 2) No"
echo

while [[ $run_gravity_now != [12] ]]; do
read -p " Please select: " run_gravity_now
done
if [ "$run_gravity_now" -eq 1 ]; then
echo
echo
echo " [i] Starting gravity"
echo
pihole -g
echo
echo " [✓] Gravity update finished"
echo
echo
echo
else
echo
echo
echo " [i] Not running gravity, keeping mismatch between enabled adlists and the data found in the gravity database. "
fi
fi
fi


Expand Down Expand Up @@ -369,6 +412,10 @@ EOF
# 4.) select all domains that are on the blacklist and also found in gravity_strip
# 5.) update blacklist_gravity with the number of hits for each domain (must be done before CNAME handling, as this adds hits to domains found during CNAME instection)





sqlite3 -cmd ".timeout 5000" $TEMP_DB << EOF
ATTACH DATABASE "${PIHOLE_FTL}" AS pihole_ftl_db;
ATTACH DATABASE "${GRAVITY}?mode=ro" AS gravity_db;
Expand Down Expand Up @@ -592,7 +639,7 @@ adlist_conf_unique_enabled=(`sqlite3 $TEMP_DB "select id from adlist where uniqu
if [ "$menu_selection" -eq 1 ];
then
menu_selection=
echo " Would you like to keep your adlist configurartion, or keep only adlists enabled with at least one unique covered domain?"
echo " Would you like to keep your adlist configuration, or keep only adlists enabled with at least one unique covered domain?"
echo
echo " 1) Keep adlist configuration"
echo " 2) Enable only adlists with covered unique domains"
Expand Down Expand Up @@ -624,6 +671,7 @@ fi

if [ "$menu_selection" -eq 2 ]; then

echo
echo " [i] Enabling adlists with covered unique domains...."
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=0;"
for adlist_id in "${adlist_conf_unique_enabled[@]}"; do
Expand All @@ -638,6 +686,7 @@ fi

if [ "$menu_selection" -eq 3 ]; then

echo
echo " [i] Restoring previous adlist configuration...."
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=0;"
for adlist_id in "${adlist_conf_old_enabled[@]}"; do
Expand All @@ -651,6 +700,7 @@ if [ "$menu_selection" -eq 3 ]; then
fi



if [ "$UNIQUE" = 1 ];
then
echo
Expand Down

0 comments on commit 8dab718

Please sign in to comment.