Skip to content

Commit

Permalink
Add the ability to enable all adlists from within the script. After a…
Browse files Browse the repository at this point in the history
…nalysis previous configuration can be restored or only adlists with unique domains can be enabled.
  • Loading branch information
yubiuser committed Jun 14, 2020
1 parent 5b43418 commit 4b5559d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ As domains usually appear on more then one adlist I introduce the concept of ***
---
**Limits**

- Disabled blocklist won't be analyzed as gravity is not including domains from deactivated adlists. If you want to check them also you have to enable them and run `pihole -g`
- Disabled blocklist won't be analyzed as gravity is not including domains from deactivated adlists. You can enable all adlists from within the script.

- Black/Whitelisted domains (including regex) are not considered when calculating the number of covered domains (and hits)
- Whitelisted domains reduce the number of blocked domains as reported by pihole compared to the calculated numbers
Expand Down
111 changes: 105 additions & 6 deletions pihole_adlist_tool
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# define path to pihole's databases and temporary database
TEMP_DB="/tmp/temp.db"
PIHOLE_FTL="file:/etc/pihole/pihole-FTL.db?mode=ro"
GRAVITY="file:/etc/pihole/gravity.db?mode=ro"
GRAVITY="file:/etc/pihole/gravity.db"

#define and initialize variables
declare -i DAYS_REQUESTED=30
Expand All @@ -29,6 +29,9 @@ HITS_TOTAL_CURRENT=
BLACKLIST_GRAVITY=
UNIQUE=
NUM_TOTAL_UNIQUE_DOMAINS=
declare -a adlist_conf_old_enabled
declare -a adlist_conf_unique_enabled
declare -i menu_selection

#for text formating
bold=$(tput bold)
Expand Down Expand Up @@ -137,6 +140,41 @@ echo
echo "${bold}*** Pihole Adlist Tool ***${normal}"
echo



# save old adlist_configuration
adlist_conf_old_enabled=(`sqlite3 $GRAVITY "select id from adlist where enabled=1;"`)

echo "Would you like to analyze your current adlist configuration or first enable all adlists (current can be restored later)?"
echo
echo "1) Current adlist configuration"
echo "2) Enable all adlists (runs pihole -g)"
echo

while [[ $menu_selection != [12] ]]; do
read -p "Please select: " menu_selection
done
echo
echo


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

echo "Enabling all adlists...."
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=1;"
echo
echo
echo "Starting gravity"
echo
pihole -g
echo
echo "Gravity update finished"
echo
echo
echo
fi


# calculate various timestamps, converts them to dates
set_timestamps
set_dates
Expand All @@ -160,11 +198,12 @@ if [ "$TIMESTAMP_REQUESTED" -lt "$TIMESTAMP_FIRST_QUERY" ];
echo
echo "${bold}Warning:${normal} You requested to analyze "${DAYS_REQUESTED}" days (starting from "$DATE_REQUESTED"), but oldest query is from "$DATE_FIRST_QUERY". Using this instead."
echo
echo
echo
fi


echo
echo
echo
echo "Calculating....."
echo
echo "This might take some time - please be patient."
Expand Down Expand Up @@ -197,7 +236,7 @@ sqlite3 -cmd ".timeout 5000" $TEMP_DB << EOF
create table unique_domains(domain TEXT, adlist_id INTEGER);
ATTACH DATABASE "${PIHOLE_FTL}" AS pihole_ftl_db;
ATTACH DATABASE "${GRAVITY}" AS gravity_db;
ATTACH DATABASE "${GRAVITY}?mode=ro" AS gravity_db;
INSERT INTO blocked_domains(domain, hits) SELECT domain, COUNT(domain) FROM pihole_ftl_db.queries WHERE EXISTS (select 1 from gravity_db.gravity where gravity.domain=queries.domain) AND id>=${FTL_ID} GROUP BY domain ORDER BY COUNT(domain) DESC;
Expand Down Expand Up @@ -240,6 +279,7 @@ BLACKLIST_GRAVITY=$(sqlite3 $TEMP_DB "SELECT COUNT(*) FROM blacklist_gravity;")
NUM_TOTAL_UNIQUE_DOMAINS=$(sqlite3 $TEMP_DB "SELECT COUNT(*) FROM unique_domains;")



echo
echo "You have ${bold}"$NUM_ADLISTS" adlists${normal} configured ("$NUM_ADLISTS_ENABLED" enabled). Your gravity.db contains ${bold}"$NUM_GRAVITY_UNIQUE_DOMAINS" unique domains${normal}."
echo
Expand Down Expand Up @@ -303,19 +343,78 @@ echo
sqlite3 -column -header $TEMP_DB "SELECT id, enabled, total_domains, domains_covered, hits_covered, unique_domains_covered, address FROM adlist ORDER BY ${SORT_ORDER};"

echo
echo "Domains from disabled adlists are not stored in gravity's database. If you want to include them, enable them and run 'pihole -g'"
echo
echo
echo "Domains from disabled adlists are not stored in gravity's database. If you want to include them in the analysis, run this script again and select 'Enable all adlists'"
echo "As the same domains usually appears on more than one adlist the sum of covered domains from this table is greater "
echo "than the number of calculated blocked domains shown above"
echo
echo
echo "In total your adlists contain ${bold}"$NUM_TOTAL_UNIQUE_DOMAINS" visited (covered) unique domains${normal} - meaning those domains are contained only in a single adlist. "
echo
echo


# save adlist unique_configuration
adlist_conf_unique_enabled=(`sqlite3 $TEMP_DB "select id from adlist where unique_domains_covered IS NOT NULL;"`)

if [ "$menu_selection" -eq 1 ];
then
menu_selection=
echo "Would you like to keept your adlist configurartion, 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"
echo
while [[ $menu_selection != [12] ]]; do
read -p "Please select: " menu_selection
done

else
menu_selection=
echo "Would you like to keep all adlists enabled, keep only adlists enabled with at least one unique covered domain, or revert to your previous adlist configurartion?"
echo
echo "1) Keep all adlists enabled"
echo "2) Enable only adlists with covered unique domains"
echo "3) Restore previous adlist configuration"
echo
while [[ $menu_selection != [123] ]]; do
read -p "Please select: " menu_selection
done
fi

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

echo "Enabling adlists with covered unique domains...."
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=0;"
for adlist_id in "${adlist_conf_unique_enabled[@]}"; do
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=1 where id=$adlist_id;"
done
pihole restartdns reload-lists
echo
echo "Adlists with covered unique domains enabled"
echo
fi

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

echo "Restoring previous adlist configuration...."
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=0;"
for adlist_id in "${adlist_conf_old_enabled[@]}"; do
sudo sqlite3 $GRAVITY "UPDATE adlist SET enabled=1 where id=$adlist_id;"
done
pihole restartdns reload-lists
echo
echo "Previous adlist configuration restored"
echo
fi


if [ "$UNIQUE" = 1 ];
then
echo
echo
read -p "Press enter to continue..."
read -p "Press enter to continue (show covered unique domains)..."
echo
echo
echo "${bold}***Covered unique domains ***${normal}"
Expand Down

0 comments on commit 4b5559d

Please sign in to comment.