From c08ab644e74d20ed212f411079d09920391d52fa Mon Sep 17 00:00:00 2001 From: stablestud Date: Sun, 7 Jan 2018 14:52:50 +0100 Subject: [PATCH] Removed unnecessary actions which would slow down the script Made some warnings more user friendly Added check if HOSTS_FILE_PATH is valid --- adsorber.conf | 2 +- adsorber.sh | 2 +- bin/components/hosts_header | 4 ++-- bin/config.sh | 26 +++++++++++++++----------- bin/default/default-adsorber.conf | 2 +- bin/default/default-blacklist | 2 +- bin/install.sh | 15 +++++++-------- bin/update.sh | 26 ++++++++++++++------------ 8 files changed, 42 insertions(+), 37 deletions(-) diff --git a/adsorber.conf b/adsorber.conf index 79bce40..1138138 100644 --- a/adsorber.conf +++ b/adsorber.conf @@ -1,5 +1,5 @@ #!/bin/bash -# Config file for Adsorber v0.2.0 +# Config file for Adsorber v0.2.1 # # To reset the config file just delete it and run 'adsorber.sh install',a new # config file will be created diff --git a/adsorber.sh b/adsorber.sh index 83fdb10..fa1fa88 100755 --- a/adsorber.sh +++ b/adsorber.sh @@ -4,7 +4,7 @@ readonly TMP_DIR_PATH="/tmp/adsorber" readonly SCRIPT_DIR_PATH="$(cd "$(dirname "${0}")" && pwd)" readonly SOURCELIST_FILE_PATH="${SCRIPT_DIR_PATH}/sources.list" -readonly VERSION="0.2.0" +readonly VERSION="0.2.1" readonly OPERATION="${1}" diff --git a/bin/components/hosts_header b/bin/components/hosts_header index c61ba80..10a0956 100644 --- a/bin/components/hosts_header +++ b/bin/components/hosts_header @@ -1,8 +1,8 @@ # This file has been edited by Adsorber. # # To make any changes to this file please edit @${HOSTS_FILE_BACKUP_PATH}@ -# and run './adsorber.sh update'. -# Or else your changes will be overwritten by adsorber every week. +# and run './adsorber.sh update' to apply your changes. +# Or else your changes will be overwritten by adsorber once a week. # # The domains will be updated once a week. # For further information please run './adsorber --help' diff --git a/bin/config.sh b/bin/config.sh index bd716a9..e01b4f3 100755 --- a/bin/config.sh +++ b/bin/config.sh @@ -8,13 +8,14 @@ # SOURCELIST_FILE_PATH SCRIPT_DIR_PATH/sources.list (e.g., /home/user/Downloads/absorber/sources.list) # TMP_DIR_PATH /tmp/adsorber # USE_PARTIAL_MATCHING true +# VERSION 0.2.1 or similar -SETTING_STRING+=("PRIMARY_LIST") -SETTING_STRING+=("USE_PARTIAL_MATCHING") -SETTING_STRING+=("HOSTS_FILE_PATH") -SETTING_STRING+=("HOSTS_FILE_BACKUP_PATH") -SETTING_STRING+=("CRONTAB_DIR_PATH") -SETTING_STRING+=("SYSTEMD_DIR_PATH") +SETTING_STRING[0]="PRIMARY_LIST" +SETTING_STRING[1]="USE_PARTIAL_MATCHING" +SETTING_STRING[2]="HOSTS_FILE_PATH" +SETTING_STRING[3]="HOSTS_FILE_BACKUP_PATH" +SETTING_STRING[4]="CRONTAB_DIR_PATH" +SETTING_STRING[5]="SYSTEMD_DIR_PATH" readonly SETTING_STRING @@ -42,7 +43,7 @@ configCreateTmpDir() { copySourceList() { if [ ! -f "${SCRIPT_DIR_PATH}/sources.list" ] || [ ! -s "${SCRIPT_DIR_PATH}/sources.list" ]; then cp "${SCRIPT_DIR_PATH}/bin/default/default-sources.list" "${SCRIPT_DIR_PATH}/sources.list" \ - && echo "To add new host sources, please edit sources.list" + && echo "Created sources.list: to add new host sources edit this file." fi return 0 @@ -52,7 +53,7 @@ copySourceList() { copyWhiteList() { if [ ! -f "${SCRIPT_DIR_PATH}/whitelist" ] || [ ! -s "${SCRIPT_DIR_PATH}/whitelist" ]; then cp "${SCRIPT_DIR_PATH}/bin/default/default-whitelist" "${SCRIPT_DIR_PATH}/whitelist" \ - && echo "To allow host sources, please edit the whitelist." + && echo "Created whitelist: To allow specific domains edit this file." fi return 0 @@ -62,7 +63,7 @@ copyWhiteList() { copyBlackList() { if [ ! -f "${SCRIPT_DIR_PATH}/blacklist" ] || [ ! -s "${SCRIPT_DIR_PATH}/blacklist" ]; then cp "${SCRIPT_DIR_PATH}/bin/default/default-blacklist" "${SCRIPT_DIR_PATH}/blacklist" \ - && echo "To block additional host sources, please edit the blacklist." + && echo "Created blacklist: to block additional domains edit this file." fi return 0 @@ -75,7 +76,8 @@ copyConfig() { else echo "No config file found. Creating default." echo "Please re-run the command to continue." - cp "${SCRIPT_DIR_PATH}/bin/default/default-adsorber.conf" "${SCRIPT_DIR_PATH}/adsorber.conf" + sed "s|@.*|# Config file for Adsorber v${VERSION}|g" "${SCRIPT_DIR_PATH}/bin/default/default-adsorber.conf" > "${SCRIPT_DIR_PATH}/adsorber.conf" + configCleanUp exit 1 fi @@ -132,7 +134,9 @@ isVariableSet() { isVariableValid() { if [ ! -f "${HOSTS_FILE_PATH}" ]; then - echo "Wrong HOSTS_FILE_PATH set. Can't access ${HOSTS_FILE_PATH}" 1>&2 + echo "Wrong HOSTS_FILE_PATH set in adsorber.conf. Can't access ${HOSTS_FILE_PATH}" 1>&2 + configCleanUp + exit 1 fi return 0 diff --git a/bin/default/default-adsorber.conf b/bin/default/default-adsorber.conf index 79bce40..aac591d 100644 --- a/bin/default/default-adsorber.conf +++ b/bin/default/default-adsorber.conf @@ -1,5 +1,5 @@ #!/bin/bash -# Config file for Adsorber v0.2.0 +@# Config file for Adsorber v?.?.? (DO NOT EDIT, WILL BE OVERWRITTEN BY SCRIPT)@ # # To reset the config file just delete it and run 'adsorber.sh install',a new # config file will be created diff --git a/bin/default/default-blacklist b/bin/default/default-blacklist index 6ecab28..99e88cc 100644 --- a/bin/default/default-blacklist +++ b/bin/default/default-blacklist @@ -1,6 +1,6 @@ # Blacklist # Use to manually block domains. -# By default it overwrites the whitelist. This can be changed in adsorber.conf +# By default the blacklist overwrites the whitelist. This can be changed in adsorber.conf # # Entries must begin with either 127.0.0.1 or 0.0.0.0 diff --git a/bin/install.sh b/bin/install.sh index a7bd996..a010844 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -32,10 +32,9 @@ installCronjob() { exit 1 fi - cp "${SCRIPT_DIR_PATH}/bin/cron/80adsorber" "${CRONTAB_DIR_PATH}" - - # Replace the @ place holder line with SCRIPT_DIR_PATH - sed -i "s|@.*|${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${CRONTAB_DIR_PATH}/80adsorber" + # Replace the @ place holder line with SCRIPT_DIR_PATH and copy the content to crons directory + sed "s|@.*|${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/cron/80adsorber" > "${CRONTAB_DIR_PATH}/80adsorber" + chmod u=rwx,g=rx,o=rx "${CRONTAB_DIR_PATH}/80adsorber" return 0 } @@ -49,12 +48,12 @@ installSystemd() { exit 1 fi - cp "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.service" "${SYSTEMD_DIR_PATH}/adsorber.service" - - # Replace the @ place holder line with SCRIPT_DIR_PATH - sed -i "s|@ExecStart.*|ExecStart=${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SYSTEMD_DIR_PATH}/adsorber.service" + # Replace the @ place holder line with SCRIPT_DIR_PATH and copy to its systemd directory + sed "s|@ExecStart.*|ExecStart=${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.service" > "${SYSTEMD_DIR_PATH}/adsorber.service" cp "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.timer" "${SYSTEMD_DIR_PATH}/adsorber.timer" + chmod u=rwx,g=rx,o=rx "${SYSTEMD_DIR_PATH}/adsorber.service" "${SYSTEMD_DIR_PATH}/adsorber.timer" + systemctl daemon-reload \ && systemctl enable adsorber.timer \ && systemctl start adsorber.timer || echo "Couldn't start systemd service." 1>&2 diff --git a/bin/update.sh b/bin/update.sh index 1e4b52d..8006004 100755 --- a/bin/update.sh +++ b/bin/update.sh @@ -167,6 +167,13 @@ filterDomains() { local input_file="${1}" local output_file="${2}" + # - replace OSX '\r' and MS-DOS '\r\n' with Unix '\n' (linebreak) + # - replace 127.0.0.1 and 127.0.1.1 with 0.0.0.0 + # - only keep lines starting with 0.0.0.0 + # - remove inline '#' comments + # - replace tabs and multiple spaces with one space + # - remove domains without a dot (e.g localhost , loopback , ip6-allnodes , etc...) + # - remove domains that are redirecting to *.local cat "${TMP_DIR_PATH}/${input_file}" \ | sed 's/\r/\n/g' \ | sed 's/^\s*127\.0\.[01]\.1/0\.0\.0\.0/g' \ @@ -176,13 +183,6 @@ filterDomains() { | sed -n '/^0\.0\.0\.0\s.\+\..\+/p' \ | sed -n '/\.local\s*$/!p' \ > "${TMP_DIR_PATH}/${output_file}" - # - replace OSX '\r' and MS-DOS '\r\n' with Unix '\n' (linebreak) - # - replace 127.0.0.1 and 127.0.1.1 with 0.0.0.0 - # - only keep lines starting with 0.0.0.0 - # - remove inline '#' comments - # - replace tabs and multiple spaces with one space - # - remove domains without a dot (e.g localhost , loopback , ip6-allnodes , etc...) - # - remove domains that are redirecting to *.local return 0 } @@ -267,10 +267,8 @@ isCacheEmpty() { preBuildHosts() { - # Add hosts_header # Replace @...@ with the path to the backup hosts - cat "${SCRIPT_DIR_PATH}/bin/components/hosts_header" \ - | sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" > "${TMP_DIR_PATH}/hosts" + sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header" > "${TMP_DIR_PATH}/hosts" echo "" >> "${TMP_DIR_PATH}/hosts" @@ -290,9 +288,13 @@ preBuildHosts() { buildHostsFile() { echo "" >> "${TMP_DIR_PATH}/hosts" - # Glue the final piece of the hosts file to it + # Add ad-domains to the hosts file cat "${TMP_DIR_PATH}/cache" >> "${TMP_DIR_PATH}/hosts" + echo "" >> "${TMP_DIR_PATH}/hosts" + + sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header" >> "${TMP_DIR_PATH}/hosts" + return 0 } @@ -343,7 +345,7 @@ update() { mergeBlackList ;; * ) - echo "Wrong PRIMARY_LIST set. Choose either 'whitelist' or 'blacklist'" 1>&2 + echo "Wrong PRIMARY_LIST set in adsorber.conf. Choose either 'whitelist' or 'blacklist'" 1>&2 updateCleanUp exit 1 ;;