From 0344d9e40204bbd2cd9765074d91e8786b840933 Mon Sep 17 00:00:00 2001 From: stablestud Date: Fri, 30 Oct 2020 00:33:26 +0100 Subject: [PATCH 1/9] Fix prompt wrong suggested default value --- src/lib/update.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/update.sh b/src/lib/update.sh index fe8084e..15a4948 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -44,19 +44,18 @@ update_CheckBackupExist() { if [ ! -f "${hosts_file_backup_path}" ]; then - # The user may proceed without having a backup of the original # hosts file, however it's not recommended to proceed as the # hostname association with 127.0.0.1 and localhost will be lost. # The user may interactively decide here wheter to proceed or not. if [ -z "${reply_to_force_prompt}" ]; then printf "%bBackup of %s does not exist. To backup run 'adsorber setup'.%b\\n" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 - printf "%bIgnore issue and continue? (May break your hostfile, not recommended) [YES/n]: %b" "${prefix_input}" "${prefix_reset}" + printf "%bIgnore issue and continue? (May break your hostfile, not recommended) [y/N]: %b" "${prefix_input}" "${prefix_reset}" read -r reply_to_force_prompt fi case "${reply_to_force_prompt}" in - [Yy][Ee][Ss] ) + [Yy] | [Yy][Ee][Ss] ) return 0 ;; * ) From 6ad5994b6f7b5e6a8c40712ec1570391c275dfd8 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 13:57:29 +0100 Subject: [PATCH 2/9] Add ad-domains inline --- src/bin/adsorber | 136 ++++++++++++++-------------- src/lib/colours.sh | 142 +++++++++++++++--------------- src/lib/config.sh | 106 +++++++++++----------- src/lib/cron/cron.sh | 126 +++++++++++++------------- src/lib/setup.sh | 2 +- src/lib/systemd/systemd.sh | 122 ++++++++++++------------- src/lib/update.sh | 69 +++++++++------ src/share/components/hosts_header | 7 +- src/share/components/hosts_title | 2 - 9 files changed, 362 insertions(+), 350 deletions(-) delete mode 100644 src/share/components/hosts_title diff --git a/src/bin/adsorber b/src/bin/adsorber index 4a7f49b..136e24f 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -42,12 +42,12 @@ checkRoot() { # Changing the hosts file requires root rights! if [ "$(id -g)" -ne 0 ]; then - echo "$(id -un), I require way more more power then this! How about .. root ..? ;)" 1>&2 + echo "$(id -un), I require way more more power then this! How about .. root ..? ;)" 1>&2 exit 126 fi checkPaths - sourceFiles + sourceFiles } @@ -95,7 +95,7 @@ showHelp() echo " update - update hosts file with newest ad-domains" echo " restore - restore hosts file to its original state" echo " (it does not remove the scheduler, this should be used temporary)" - echo " revert - reverts the hosts file to the last applied (previous) host file." + echo " revert - reverts the hosts file to the last applied (previous) host file." echo " disable - completely remove changes made by Adsorber" echo " e.g., disable scheduler (if set)" echo " restore hosts file to its original state" @@ -107,20 +107,20 @@ showHelp() echo " -f, --force - force the update if no /etc/hosts backup" echo " has been created (dangerous!)" echo " -h, --help - show specific help of specified operations" - echo " (e.g 'adsorber update --help)" - echo " --noformatting - turn off coloured and formatted output" - echo - echo "Scheduler options (use with 'setup'):" + echo " (e.g 'adsorber update --help)" + echo " --noformatting - turn off coloured and formatted output" + echo + echo "Scheduler options (use with 'setup'):" echo " -ns, --no-scheduler - skip scheduler creation" echo " -s, --systemd - setup Systemd as scheduler" echo " -c, --cron - setup Cronjob ..." - echo " -H, --hourly - run scheduler once hourly" - echo " -D, --daily ... daily" - echo " -W, --weekly ... weekly" - echo " -M, --monthly ... monthly" - echo " -Q, --quarterly ... quarterly (4x a year)" - echo " -S, --semiannually ... semiannually (2x a year)" - echo " -Y, --yearly ... yearly" + echo " -H, --hourly - run scheduler once hourly" + echo " -D, --daily ... daily" + echo " -W, --weekly ... weekly" + echo " -M, --monthly ... monthly" + echo " -Q, --quarterly ... quarterly (4x a year)" + echo " -S, --semiannually ... semiannually (2x a year)" + echo " -Y, --yearly ... yearly" echo echo "Config files are located at: $(cd "${config_dir_path}" && pwd)/" echo @@ -154,16 +154,16 @@ showSpecificHelp() echo " -c, --cron - use Cronjob as scheduler" echo " -ns, --no-scheduler - skip scheduler creation" echo " -h, --help - show this help screen" - echo " -H, --hourly - run scheduler once hourly" - echo " -D, --daily ... daily" - echo " -W, --weekly ... weekly" - echo " -M, --monthly ... monthly" - echo " -Q, --quarterly ... quarterly (4x a year)" - echo " -S, --semiannually ... semiannually (2x a year)" - echo " -Y, --yearly ... yearly" - echo " --noformatting - turn off coloured and formatted output" - echo - echo "Note: Cron supports only hourly, daily, weekly and monthly." + echo " -H, --hourly - run scheduler once hourly" + echo " -D, --daily ... daily" + echo " -W, --weekly ... weekly" + echo " -M, --monthly ... monthly" + echo " -Q, --quarterly ... quarterly (4x a year)" + echo " -S, --semiannually ... semiannually (2x a year)" + echo " -Y, --yearly ... yearly" + echo " --noformatting - turn off coloured and formatted output" + echo + echo "Note: Cron supports only hourly, daily, weekly and monthly." ;; update ) printf "\\033[4;37madsorber update []\\033[0m:\\n" @@ -177,7 +177,7 @@ showSpecificHelp() echo " -f, --force - force the update if no /etc/hosts backup" echo " has been created (dangerous)" echo " -h, --help - show this help screen" - echo " --noformatting - turn off coloured and formatted output" + echo " --noformatting - turn off coloured and formatted output" ;; restore ) printf "\\033[4;37madsorber restore []\\033[0m:\\n" @@ -191,16 +191,16 @@ showSpecificHelp() echo "For this reason this command should be used to temporary disable Adsorber." echo "(e.g. when it's blocking some sites you need access for a short period of time)" echo - echo "To re-apply run 'adsorber revert' (for previous host file) or 'adsorber update' (for updated version)." + echo "To re-apply run 'adsorber revert' (for previous host file) or 'adsorber update' (for updated version)." echo echo "Possible options:" echo " -h, --help - show this help screen" - echo " --noformatting - turn off coloured and formatted output" + echo " --noformatting - turn off coloured and formatted output" ;; revert ) printf "\\033[4;37madsorber revert []\\033[0m:\\n" echo - echo "Revert the current hosts file to the previously applied hosts file." + echo "Revert the current hosts file to the previously applied hosts file." echo "Use it if the current host file has been corrupted or misses previously applied ad-domains." echo echo "The command will:" @@ -210,29 +210,29 @@ showSpecificHelp() echo echo "Possible options:" echo " -h, --help - show this help screen" - echo " --noformatting - turn off coloured and formatted output" + echo " --noformatting - turn off coloured and formatted output" ;; disable ) printf "\\033[4;37madsorber disable []\\033[0m:\\n" echo - echo "Completely disable all background tasks (schedulers) and remove all changes made by Adsorber." + echo "Completely disable all background tasks (schedulers) and remove all changes made by Adsorber." echo echo "The command will:" echo " - disable/remove all schedulers (systemd, cronjob)" echo " - restore the hosts file to it's original state" echo " - remove all leftovers (e.g. /tmp/adsorber, previous hosts files, etc)" - echo - echo "It'll not remove/uninstall the 'adsorber' command from the system." - echo "To do this you need to run the 'remove_files_from_system.sh' script." + echo + echo "It'll not remove/uninstall the 'adsorber' command from the system." + echo "To do this you need to run the 'remove_files_from_system.sh' script." echo echo "Possible options:" echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" echo " -h, --help - show this help screen" - echo " --noformatting - turn off coloured and formatted output" + echo " --noformatting - turn off coloured and formatted output" + ;; + * ) + showHelp ;; - * ) - showHelp - ;; esac exit 0 @@ -264,16 +264,16 @@ duplicateOption() echo " -c, --cron - use Cronjob ..." echo " -ns, --no-scheduler - skip scheduler creation" elif [ "${1}" = "frequency" ]; then - echo "Adsorber: Duplicate option for schedulers frequency: '${2}'" 1>&2 - echo "You may select only one:" - echo " -H, --hourly - run scheduler once hourly" - echo " -D, --daily ... daily" - echo " -W, --weekly ... weekly" - echo " -M, --monthly ... monthly" - echo " -Q, --quarterly ... quarterly (4x a year)" - echo " -S, --semiannually ... semiannually (2x a year)" - echo " -Y, --yearly ... yearly" - else + echo "Adsorber: Duplicate option for schedulers frequency: '${2}'" 1>&2 + echo "You may select only one:" + echo " -H, --hourly - run scheduler once hourly" + echo " -D, --daily ... daily" + echo " -W, --weekly ... weekly" + echo " -M, --monthly ... monthly" + echo " -Q, --quarterly ... quarterly (4x a year)" + echo " -S, --semiannually ... semiannually (2x a year)" + echo " -Y, --yearly ... yearly" + else echo "Adsorber: Duplicate option: '${2}'" 1>&2 showUsage fi @@ -316,8 +316,8 @@ sourceFiles() { # shellcheck source=../src/lib/colours.sh . "${library_dir_path}/colours.sh" - # shellcheck source=../src/lib/cleanup.sh - . "${library_dir_path}/cleanup.sh" + # shellcheck source=../src/lib/cleanup.sh + . "${library_dir_path}/cleanup.sh" # shellcheck source=../src/lib/setup.sh . "${library_dir_path}/setup.sh" # shellcheck source=../src/lib/disable.sh @@ -377,63 +377,63 @@ for _option in "${@}"; do duplicateOption fi ;; - -H | --hourly ) + -H | --hourly ) if [ -z "${frequency}" ]; then readonly frequency="hourly" else duplicateOption "frequency" "${_option}" fi - ;; - -D | --daily ) + ;; + -D | --daily ) if [ -z "${frequency}" ]; then readonly frequency="daily" else duplicateOption "frequency" "${_option}" fi - ;; - -W | --weekly ) + ;; + -W | --weekly ) if [ -z "${frequency}" ]; then readonly frequency="weekly" else duplicateOption "frequency" "${_option}" fi - ;; - -M | --monthly ) + ;; + -M | --monthly ) if [ -z "${frequency}" ]; then readonly frequency="monthly" else duplicateOption "frequency" "${_option}" fi - ;; - -Q | --quarterly ) + ;; + -Q | --quarterly ) if [ -z "${frequency}" ]; then readonly frequency="quarterly" else duplicateOption "frequency" "${_option}" fi - ;; - -S | --semi | --semiannually ) + ;; + -S | --semi | --semiannually ) if [ -z "${frequency}" ]; then readonly frequency="semiannually" else duplicateOption "frequency" "${_option}" fi - ;; - -Y | --yearly | --annually ) + ;; + -Y | --yearly | --annually ) if [ -z "${frequency}" ]; then readonly frequency="yearly" else duplicateOption "frequency" "${_option}" fi - ;; + ;; -h | --help | help ) _option_help="true" ;; - --noformatting ) - if [ -z "${use_formatting}" ]; then - readonly use_formatting="false" - fi - ;; + --noformatting ) + if [ -z "${use_formatting}" ]; then + readonly use_formatting="false" + fi + ;; * ) _wrong_option="${_option}" 2>/dev/null ;; diff --git a/src/lib/colours.sh b/src/lib/colours.sh index 7e814bc..8a8afc1 100755 --- a/src/lib/colours.sh +++ b/src/lib/colours.sh @@ -24,82 +24,82 @@ if [ "${use_formatting}" != "false" ]; then readonly prefix_reset="\\033[0m" # Default colour readonly prefix_underline="\\033[4;37m" # White Underline - # Regular Colors - #readonly BLACK='\033[0;30m' # Black - #readonly RED='\033[0;31m' # Red - #readonly GREEN='\033[0;32m' # Green - #readonly YELLOW='\033[0;33m' # Yellow - #readonly BLUE='\033[0;34m' # Blue - #readonly PURPLE='\033[0;35m' # Purple - #readonly CYAN='\033[0;36m' # Cyan - #readonly WHITE='\033[0;37m' # White + # Regular Colors + #readonly BLACK='\033[0;30m' # Black + #readonly RED='\033[0;31m' # Red + #readonly GREEN='\033[0;32m' # Green + #readonly YELLOW='\033[0;33m' # Yellow + #readonly BLUE='\033[0;34m' # Blue + #readonly PURPLE='\033[0;35m' # Purple + #readonly CYAN='\033[0;36m' # Cyan + #readonly WHITE='\033[0;37m' # White - # Bold - #readonly BBLACk='\033[1;30m' # Black - #readonly BRED='\033[1;31m' # Red - #readonly BGREEN='\033[1;32m' # Green - #readonly BYELLOW='\033[1;33m' # Yellow - #readonly BBLUE='\033[1;34m' # Blue - #readonly BPURPLE='\033[1;35m' # Purple - #readonly BCYAN='\033[1;36m' # Cyan - #readonly BWHITE='\033[1;37m' # White + # Bold + #readonly BBLACk='\033[1;30m' # Black + #readonly BRED='\033[1;31m' # Red + #readonly BGREEN='\033[1;32m' # Green + #readonly BYELLOW='\033[1;33m' # Yellow + #readonly BBLUE='\033[1;34m' # Blue + #readonly BPURPLE='\033[1;35m' # Purple + #readonly BCYAN='\033[1;36m' # Cyan + #readonly BWHITE='\033[1;37m' # White - # Underline - #readonly UBLACK='\033[4;30m' # Black - #readonly URRED='\033[4;31m' # Red - #readonly UGREEn='\033[4;32m' # Green - #readonly UYELLOW='\033[4;33m' # Yellow - #readonly UBLUE='\033[4;34m' # Blue - #readonly UPURPLE='\033[4;35m' # Purple - #readonly UCYAN='\033[4;36m' # Cyan - #readonly uwhite='\033[4;37m' # White + # Underline + #readonly UBLACK='\033[4;30m' # Black + #readonly URRED='\033[4;31m' # Red + #readonly UGREEn='\033[4;32m' # Green + #readonly UYELLOW='\033[4;33m' # Yellow + #readonly UBLUE='\033[4;34m' # Blue + #readonly UPURPLE='\033[4;35m' # Purple + #readonly UCYAN='\033[4;36m' # Cyan + #readonly uwhite='\033[4;37m' # White - # Background - #readonly BG_BLACK='\033[40m' # Black - #readonly BG_RED='\033[41m' # Red - #readonly BG_GREEN='\033[42m' # Green - #readonly BG_YELLOW='\033[43m' # Yellow - #readonly BG_BLUE='\033[44m' # Blue - #readonly BG_PURPLE='\033[45m' # Purple - #readonly BG_CYAN='\033[46m' # Cyan - #readonly BG_WHITE='\033[47m' # White + # Background + #readonly BG_BLACK='\033[40m' # Black + #readonly BG_RED='\033[41m' # Red + #readonly BG_GREEN='\033[42m' # Green + #readonly BG_YELLOW='\033[43m' # Yellow + #readonly BG_BLUE='\033[44m' # Blue + #readonly BG_PURPLE='\033[45m' # Purple + #readonly BG_CYAN='\033[46m' # Cyan + #readonly BG_WHITE='\033[47m' # White - # High Intensity - #readonly IBLACK='\033[0;90m' # Black - #readonly IRED='\033[0;91m' # Red - #readonly IGREEN='\033[0;92m' # Green - #readonly IYELLOW='\033[0;93m' # Yellow - #readonly IBLUE='\033[0;94m' # Blue - #readonly IPURPLE='\033[0;95m' # Purple - #readonly ICYAN='\033[0;96m' # Cyan - #readonly IWHITE='\033[0;97m' # White + # High Intensity + #readonly IBLACK='\033[0;90m' # Black + #readonly IRED='\033[0;91m' # Red + #readonly IGREEN='\033[0;92m' # Green + #readonly IYELLOW='\033[0;93m' # Yellow + #readonly IBLUE='\033[0;94m' # Blue + #readonly IPURPLE='\033[0;95m' # Purple + #readonly ICYAN='\033[0;96m' # Cyan + #readonly IWHITE='\033[0;97m' # White - # Bold High Intensity - #readonly BIBLACK='\033[1;90m' # Black - #readonly BIRED='\033[1;91m' # Red - #readonly BIGREEN='\033[1;92m' # Green - #readonly BIYELLOW='\033[1;93m' # Yellow - #readonly BIBLUE='\033[1;94m' # Blue - #readonly BIPURPLE='\033[1;95m' # Purple - #readonly BICYAB='\033[1;96m' # Cyan - #readonly BIWHITE='\033[1;97m' # White + # Bold High Intensity + #readonly BIBLACK='\033[1;90m' # Black + #readonly BIRED='\033[1;91m' # Red + #readonly BIGREEN='\033[1;92m' # Green + #readonly BIYELLOW='\033[1;93m' # Yellow + #readonly BIBLUE='\033[1;94m' # Blue + #readonly BIPURPLE='\033[1;95m' # Purple + #readonly BICYAB='\033[1;96m' # Cyan + #readonly BIWHITE='\033[1;97m' # White - # High Intensity backgrounds - #readonly BG_IBLACK='\033[0;100m' # Black - #readonly BG_IRED='\033[0;101m' # Red - #readonly BG_IGREEN='\033[0;102m' # Green - #readonly BG_IYELLOW='\033[0;103m' # Yellow - #readonly BG_IBLUE='\033[0;104m' # Blue - #readonly BG_IPURPLE='\033[0;105m' # Purple - #readonly BG_ICYAN='\033[0;106m' # Cyan - #readonly BG_IWHITE='\033[0;107m' # White + # High Intensity backgrounds + #readonly BG_IBLACK='\033[0;100m' # Black + #readonly BG_IRED='\033[0;101m' # Red + #readonly BG_IGREEN='\033[0;102m' # Green + #readonly BG_IYELLOW='\033[0;103m' # Yellow + #readonly BG_IBLUE='\033[0;104m' # Blue + #readonly BG_IPURPLE='\033[0;105m' # Purple + #readonly BG_ICYAN='\033[0;106m' # Cyan + #readonly BG_IWHITE='\033[0;107m' # White else - unset prefix - unset prefix_fatal - unset prefix_info - unset prefix_input - unset prefix_title - unset prefix_warning - unset prefix_reset - unset prefix_underline + unset prefix + unset prefix_fatal + unset prefix_info + unset prefix_input + unset prefix_title + unset prefix_warning + unset prefix_reset + unset prefix_underline fi diff --git a/src/lib/config.sh b/src/lib/config.sh index 590785a..5a6459a 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -83,7 +83,7 @@ config_CopyConfig() echo "${prefix_warning}Please re-run the command to continue." sed "s|^#@ Config file for Adsorber v.*@#$|## Config file for Adsorber v${version}|g" "${shareable_dir_path}/default/default-adsorber.conf" \ - > "${config_dir_path}/adsorber.conf" + > "${config_dir_path}/adsorber.conf" exit 126 fi @@ -148,32 +148,32 @@ config_ReadConfig() if [ -z "${http_proxy_env}" ]; then # shellcheck disable=SC2163 export "${_line}" - export HTTP_PROXY="${http_proxy}" - elif [ -n "${http_proxy_env}" ]; then + export HTTP_PROXY="${http_proxy}" + elif [ -n "${http_proxy_env}" ]; then export "${_line}" - if [ -z "${http_proxy}" ]; then - export http_proxy="${http_proxy_env}" - export HTTP_PROXY="${http_proxy_env}" - else - export HTTP_PROXY="${http_proxy}" - echo "${prefix}Config HTTP proxy '${http_proxy}' overwrites environmental HTTP proxy '${http_proxy_env}'" - fi + if [ -z "${http_proxy}" ]; then + export http_proxy="${http_proxy_env}" + export HTTP_PROXY="${http_proxy_env}" + else + export HTTP_PROXY="${http_proxy}" + echo "${prefix}Config HTTP proxy '${http_proxy}' overwrites environmental HTTP proxy '${http_proxy_env}'" + fi fi ;; https_proxy=* ) if [ -z "${https_proxy_env}" ]; then # shellcheck disable=SC2163 export "${_line}" - export HTTPS_PROXY="${https_proxy}" - elif [ -n "${https_proxy_env}" ]; then + export HTTPS_PROXY="${https_proxy}" + elif [ -n "${https_proxy_env}" ]; then export "${_line}" - if [ -z "${https_proxy}" ]; then - export https_proxy="${https_proxy_env}" - export HTTPS_PROXY="${https_proxy_env}" - else - export HTTPS_PROXY="${https_proxy}" - echo "${prefix}Config HTTPS proxy '${https_proxy}' overwrites environmental HTTPS proxy '${https_proxy_env}'" - fi + if [ -z "${https_proxy}" ]; then + export https_proxy="${https_proxy_env}" + export HTTPS_PROXY="${https_proxy_env}" + else + export HTTPS_PROXY="${https_proxy}" + echo "${prefix}Config HTTPS proxy '${https_proxy}' overwrites environmental HTTPS proxy '${https_proxy_env}'" + fi fi ;; hosts_file_path=* ) @@ -216,7 +216,7 @@ config_ReadConfig() # file was filtered by config_FilterConfig and # should not contain any unknown lines printf "%bThis is scary: I extracted %s from the config file, however I shouldn't be able to.%b\\n" \ - "${prefix_fatal}" "${_line}" "${prefix_reset}" + "${prefix_fatal}" "${_line}" "${prefix_reset}" echo "Please report this error with your config file to https://github.com/stablestud/adsorber" errorCleanUp @@ -231,17 +231,17 @@ config_ReadConfig() config_CheckEnvVariables() { - if [ -n "${HTTP_PROXY}" ]; then - http_proxy_env="${HTTP_PROXY}" - elif [ -n "${http_proxy}" ]; then - http_proxy_env="${http_proxy}" - fi - - if [ -n "${HTTPS_PROXY}" ]; then - https_proxy_env="${HTTPS_PROXY}" - elif [ -n "${https_proxy}" ]; then - https_proxy_env="${https_proxy}" - fi + if [ -n "${HTTP_PROXY}" ]; then + http_proxy_env="${HTTP_PROXY}" + elif [ -n "${http_proxy}" ]; then + http_proxy_env="${http_proxy}" + fi + + if [ -n "${HTTPS_PROXY}" ]; then + https_proxy_env="${HTTPS_PROXY}" + elif [ -n "${https_proxy}" ]; then + https_proxy_env="${https_proxy}" + fi } @@ -252,35 +252,35 @@ config_IsVariableSet() if [ -z "${primary_list}" ]; then readonly primary_list="blacklist" printf "%bprimary_list not set in adsorber.conf. Using default value: ${primary_list}\\n" \ - "${prefix_warning}" 1>&2 + "${prefix_warning}" 1>&2 fi if [ -z "${use_partial_matching}" ]; then readonly use_partial_matching="true" printf "%buse_partial_matching not set in adsorber.conf. Using default value: ${use_partial_matching}\\n" \ - "${prefix_warning}" 1>&2 + "${prefix_warning}" 1>&2 fi if [ -z "${ignore_download_error}" ]; then readonly ignore_download_error="false" printf "%bignore_download_error not set in adsorber.conf. Using default value: ${ignore_download_error}\\n" \ - "${prefix_warning}" 1>&2 + "${prefix_warning}" 1>&2 fi if [ -z "${hosts_file_previous_enable}" ]; then readonly hosts_file_previous_enable="true" printf "%bhosts_file_previous_enable not set in adsorber.conf. Using default value: ${hosts_file_previous_enable}\\n" \ - "${prefix_warning}" 1>&2 + "${prefix_warning}" 1>&2 fi # Check if essential configurations were set in the config file # if not abort, and call error clean-up function if [ -z "${hosts_file_path}" ] || [ -z "${hosts_file_backup_path}" ] \ - || [ -z "${systemd_dir_path}" ]; then + || [ -z "${systemd_dir_path}" ]; then printf "%bMissing setting(s) in adsorber.conf%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 printf "%bPlease delete adsorber.conf in %s and run 'adsorber setup' to create a new config file.\\n" \ - "${prefix_warning}" "${config_dir_path}" 1>&2 + "${prefix_warning}" "${config_dir_path}" 1>&2 errorCleanUp exit 127 @@ -296,58 +296,58 @@ config_IsVariableValid() if [ "${primary_list}" != "blacklist" ] && [ "${primary_list}" != "whitelist" ]; then printf "%bInvalid 'primary_list' set in adsorber.conf. Choose either 'blacklist' or 'whitelist'%b\\n" \ - "${prefix_fatal}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${prefix_reset}" 1>&2 invalidVariable="true" fi if [ "${use_partial_matching}" != "true" ] && [ "${use_partial_matching}" != "false" ]; then printf "%bInvalid 'use_partial_matching' set in adsorber.conf. Possible values: 'true' or 'false'%b\\n" \ - "${prefix_fatal}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${prefix_reset}" 1>&2 invalidVariable="true" fi if [ "${ignore_download_error}" != "true" ] && [ "${ignore_download_error}" != "false" ]; then printf "%bInvalid 'ignore_download_error' set in adsorber.conf. Possible values: 'true' or 'false'%b\\n" \ - "${prefix_fatal}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${prefix_reset}" 1>&2 invalidVariable="true" fi if [ ! -f "${hosts_file_path}" ] || [ ! -w "${hosts_file_path}" ]; then printf "%bInvalid 'hosts_file_path' set in adsorber.conf. Can't write/access: %s%b\\n" \ - "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 invalidVariable="true" fi if [ ! -d "$(dirname "${hosts_file_backup_path}")" ] || [ ! -w "$(dirname "${hosts_file_backup_path}")" ]; then printf "%bInvalid 'hosts_file_backup_path' set in adsorber.conf. Can't access: %s%b\\n" \ - "${prefix_fatal}" "$(dirname "${hosts_file_backup_path}")" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "$(dirname "${hosts_file_backup_path}")" "${prefix_reset}" 1>&2 invalidVariable="true" fi if [ "${hosts_file_previous_enable}" != "true" ] && [ "${hosts_file_previous_enable}" != "false" ]; then printf "%bInvalid 'hosts_file_previous_enable' set in adsorber.conf. Possible values: 'true' or 'false'%b\\n" \ - "${prefix_fatal}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${prefix_reset}" 1>&2 invalidVariable="true" fi - if [ "${hosts_file_previous_enable}" = "true" ]; then - if [ ! -d "$(dirname "${hosts_file_previous_path}")" ] || [ ! -w "$(dirname "${hosts_file_previous_path}")" ]; then - printf "%bInvalid 'hosts_file_previous_path' set in adsorber.conf. Can't access: %s%b\\n" \ - "${prefix_fatal}" "$(dirname "${hosts_file_previous_path}")" "${prefix_reset}" 1>&2 + if [ "${hosts_file_previous_enable}" = "true" ]; then + if [ ! -d "$(dirname "${hosts_file_previous_path}")" ] || [ ! -w "$(dirname "${hosts_file_previous_path}")" ]; then + printf "%bInvalid 'hosts_file_previous_path' set in adsorber.conf. Can't access: %s%b\\n" \ + "${prefix_fatal}" "$(dirname "${hosts_file_previous_path}")" "${prefix_reset}" 1>&2 - invalidVariable="true" - fi - fi + invalidVariable="true" + fi + fi if [ ! -d "$(dirname "${tmp_dir_path}")" ] || [ ! -w "$(dirname "${tmp_dir_path}")" ]; then printf "%bInvalid 'tmp_dir_path' set in adsorber.conf. Can't access: %s%b\\n" \ - "${prefix_fatal}" "$(dirname "${tmp_dir_path}")" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "$(dirname "${tmp_dir_path}")" "${prefix_reset}" 1>&2 invalidVariable="true" fi @@ -379,7 +379,7 @@ config_PrintVariables() echo " - library_dir_path: ${library_dir_path}" echo " - shareable_dir_path: ${shareable_dir_path}" echo " - config_dir_path: ${config_dir_path}" - echo " - use_formatting: ${use_formatting}" + echo " - use_formatting: ${use_formatting}" } @@ -393,7 +393,7 @@ config() config_CopyBlackList config_CopyConfig config_FilterConfig - config_CheckEnvVariables + config_CheckEnvVariables config_ReadConfig config_IsVariableSet config_IsVariableValid diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index 7f6dbd3..23f077c 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -21,7 +21,7 @@ # prefix_fatal '\033[0;91mE ' src/lib/colours.sh # prefix_reset \033[0m src/lib/colours.sh # prefix_warning '- ' src/lib/colours.sh -# version 0.5.0 or similiar src/bin/adsorber +# version 0.5.0 or similiar src/bin/adsorber # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: @@ -37,7 +37,7 @@ crontabSetup() # Check if crontabs directory variable is correctly set, if not abort and call the error clean-up function if [ ! -d "${crontab_dir_path}" ]; then printf "%bWrong frequency set. Can't access: %s.%b\\n" \ - "${prefix_fatal}" "${crontab_dir_path}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${crontab_dir_path}" "${prefix_reset}" 1>&2 echo "${prefix}Is a Cron service installed? If not use Systemd if possible." errorCleanUp @@ -48,8 +48,8 @@ crontabSetup() # and copy and manipulate the content to crontabs directory sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/default-cronjob.sh" \ | sed "s|#@/some/path/adsorber update@#|\"${executable_dir_path}/adsorber\" update --noformatting|g" \ - | sed "s|#@frequency@#|${frequency_string}|g" \ - | sed "s|#@/some/path/to/logfile@#|${log_file_path}|g" \ + | sed "s|#@frequency@#|${frequency_string}|g" \ + | sed "s|#@/some/path/to/logfile@#|${log_file_path}|g" \ > "${crontab_dir_path}/80adsorber" chmod u=rwx,g=rx,o=rx "${crontab_dir_path}/80adsorber" @@ -63,73 +63,73 @@ crontabSetup() crontabPromptFrequency() { - if [ -z "${frequency}" ]; then - _used_input="true" - printf "%bHow often should the scheduler run? [(h)ourly/(d)aily/(W)eekly/(m)onthly]: " "${prefix_input}" - read -r frequency - fi - - case "${frequency}" in - [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][Yy] ) - readonly frequency_string="hourly" - readonly crontab_dir_path="/etc/cron.hourly/" - ;; - [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) - readonly frequency_string="daily" - readonly crontab_dir_path="/etc/cron.daily/" - ;; - [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) - readonly frequency_string="weekly" - readonly crontab_dir_path="/etc/cron.weekly/" - ;; - [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) - readonly frequency_string="monthly" - readonly crontab_dir_path="/etc/cron.monthly/" - ;; - [Yy] | [Yy][Ee][Aa][Rr] | [Yy][Ee][Aa][Rr][Ll][Yy] | \ - [Aa] | [Aa][Nn][Nn][Uu][Aa][Ll] | [Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] | \ - [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] | \ - [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] ) - if [ "${_used_input}" = "true" ]; then - echo "${prefix_warning}This frequency is only available with Systemd." 1>&2 - unset frequency - crontabPromptFrequency - else - printf "%bThis frequency is only available with Systemd.%b\\n" \ - "${prefix_fatal}" "${prefix_reset}" 1>&2 - errorCleanUp - exit 1 - fi - ;; - * ) - if [ "${_used_input}" = "true" ]; then - echo "${prefix_warning}Frequency '${frequency}' not understood." 1>&2 - unset frequency - crontabPromptFrequency - else - printf "%bFrequency '%s' not understood.%b\\n" \ - "${prefix_fatal}" "${frequency}" "${prefix_reset}" 1>&2 - errorCleanUp - exit 1 - fi - ;; - esac - - unset _used_input + if [ -z "${frequency}" ]; then + _used_input="true" + printf "%bHow often should the scheduler run? [(h)ourly/(d)aily/(W)eekly/(m)onthly]: " "${prefix_input}" + read -r frequency + fi + + case "${frequency}" in + [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][Yy] ) + readonly frequency_string="hourly" + readonly crontab_dir_path="/etc/cron.hourly/" + ;; + [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) + readonly frequency_string="daily" + readonly crontab_dir_path="/etc/cron.daily/" + ;; + [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) + readonly frequency_string="weekly" + readonly crontab_dir_path="/etc/cron.weekly/" + ;; + [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) + readonly frequency_string="monthly" + readonly crontab_dir_path="/etc/cron.monthly/" + ;; + [Yy] | [Yy][Ee][Aa][Rr] | [Yy][Ee][Aa][Rr][Ll][Yy] | \ + [Aa] | [Aa][Nn][Nn][Uu][Aa][Ll] | [Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] | \ + [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] | \ + [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] ) + if [ "${_used_input}" = "true" ]; then + echo "${prefix_warning}This frequency is only available with Systemd." 1>&2 + unset frequency + crontabPromptFrequency + else + printf "%bThis frequency is only available with Systemd.%b\\n" \ + "${prefix_fatal}" "${prefix_reset}" 1>&2 + errorCleanUp + exit 1 + fi + ;; + * ) + if [ "${_used_input}" = "true" ]; then + echo "${prefix_warning}Frequency '${frequency}' not understood." 1>&2 + unset frequency + crontabPromptFrequency + else + printf "%bFrequency '%s' not understood.%b\\n" \ + "${prefix_fatal}" "${frequency}" "${prefix_reset}" 1>&2 + errorCleanUp + exit 1 + fi + ;; + esac + + unset _used_input } crontabRemove() { if [ -f "/etc/cron.hourly/80adsorber" ] \ - || [ -f "/etc/cron.daily/80adsorber" ] \ - || [ -f "/etc/cron.weekly/80adsorber" ] \ - || [ -f "/etc/cron.monthly/80adsorber" ]; then + || [ -f "/etc/cron.daily/80adsorber" ] \ + || [ -f "/etc/cron.weekly/80adsorber" ] \ + || [ -f "/etc/cron.monthly/80adsorber" ]; then # Remove the crontab from /etc/cron.* or other if specified rm /etc/cron.?????*/80adsorber \ || { printf "%bCouldn't remove Crontab %s\\n." \ - "${prefix_warning}" "${crontab_dir_path}" 1>&2 + "${prefix_warning}" "${crontab_dir_path}" 1>&2 return 1 } @@ -143,6 +143,6 @@ crontabRemove() crontab() { - crontabPromptFrequency \ - && crontabSetup + crontabPromptFrequency \ + && crontabSetup } diff --git a/src/lib/setup.sh b/src/lib/setup.sh index cb42338..effb3b2 100755 --- a/src/lib/setup.sh +++ b/src/lib/setup.sh @@ -23,7 +23,7 @@ # --function:-- ---function defined in:--- # crontab src/lib/cron/cron.sh # errorCleanUp src/lib/cleanup.sh -# systemd src/lib/systemd/systemd.sh +# systemd src/lib/systemd/systemd.sh # shellcheck disable=SC2154 diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index 2d88a46..389db78 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -22,7 +22,7 @@ # prefix_reset \033[0m src/lib/colours.sh # prefix_warning '- ' src/lib/colours.sh # systemd_dir_path /etc/systemd/system src/lib/config.sh, adsorber.conf -# version 0.5.0 or similiar src/bin/adsorber +# version 0.5.0 or similiar src/bin/adsorber # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: @@ -36,7 +36,7 @@ systemdSetup() # Check if the variable systemd_dir_path is valid, if not abort and call error clean-up function if [ ! -d "${systemd_dir_path}" ]; then printf "%bWrong systemd_dir_path set. Can't access: %s.%b\\n" \ - "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 echo "${prefix}Is Systemd installed? If not use Cron instead." errorCleanUp @@ -47,10 +47,10 @@ systemdSetup() if [ -f "${systemd_dir_path}/adsorber.service" ] || [ -f "${systemd_dir_path}/adsorber.timer" ]; then echo "${prefix}Removing previous Systemd service ..." if ! systemdRemove; then - printf "%bSomething failed at updating the Systemd service, aborting ...%b" "${prefix_fatal}" "${prefix_reset}" 1>&2 - errorCleanUp; - exit 1; - fi + printf "%bSomething failed at updating the Systemd service, aborting ...%b" "${prefix_fatal}" "${prefix_reset}" 1>&2 + errorCleanUp; + exit 1; + fi fi echo "${prefix}Setting up ${frequency} Systemd service ..." @@ -58,15 +58,15 @@ systemdSetup() # Replace the @ place holder line with the location of adsorber and copy # the service to the systemd directory ( /etc/sytemd/system/adsorber.service ) sed "s|#@/some/path/adsorber update@#|\"${executable_dir_path}/adsorber\" update --noformatting|g" "${library_dir_path}/systemd/default-service" \ - | sed "s|#@frequency@#|${frequency}|g" \ - | sed "s|#@/some/path/to/logfile@#|${log_file_path}|g" \ - | sed "s|#@version@#|${version}|g" \ - > "${systemd_dir_path}/adsorber.service" + | sed "s|#@frequency@#|${frequency}|g" \ + | sed "s|#@/some/path/to/logfile@#|${log_file_path}|g" \ + | sed "s|#@version@#|${version}|g" \ + > "${systemd_dir_path}/adsorber.service" # Copy the systemd timer to /etc/systemd/system/adsorber.timer, timer is the clock that triggers adsorber.service - sed "s/#@frequency@#/${frequency}/g" "${library_dir_path}/systemd/default-timer" \ - > "${systemd_dir_path}/adsorber.timer" + sed "s/#@frequency@#/${frequency}/g" "${library_dir_path}/systemd/default-timer" \ + > "${systemd_dir_path}/adsorber.timer" chmod u=rw,g=r,o=r "${systemd_dir_path}/adsorber.service" "${systemd_dir_path}/adsorber.timer" chown root:root "${systemd_dir_path}/adsorber.service" "${systemd_dir_path}/adsorber.timer" @@ -75,18 +75,18 @@ systemdSetup() systemctl daemon-reload 2>/dev/null \ && systemctl enable adsorber.timer 2>/dev/null - if ! systemctl start adsorber.timer 2>/dev/null; then - # Systemd couldn't be run, probably it's a systemd-less system like Gentoo - printf "%bCouldn't start systemd service.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 - echo "${prefix}Is Systemd installed? If not use Cron instead." - systemdRemove - errorCleanUp - exit 126 - fi + if ! systemctl start adsorber.timer 2>/dev/null; then + # Systemd couldn't be run, probably it's a systemd-less system like Gentoo + printf "%bCouldn't start systemd service.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + echo "${prefix}Is Systemd installed? If not use Cron instead." + systemdRemove + errorCleanUp + exit 126 + fi - # Make known that we have setup the systemd service in this run, - # if we fail now, systemd will be also removed (see errorCleanUp) - readonly setup_scheduler="systemd" + # Make known that we have setup the systemd service in this run, + # if we fail now, systemd will be also removed (see errorCleanUp) + readonly setup_scheduler="systemd" echo "${prefix}Initialized Systemd service ..." } @@ -94,41 +94,41 @@ systemdSetup() systemdPromptFrequency() { - if [ -z "${frequency}" ]; then - printf "%bHow often should the service run? [(h)ourly/(d)aily/(W)eekly/(m)onthly/(q)uarterly]: " "${prefix_input}" - read -r _freq_input - - case "${_freq_input}" in - [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][Yy] ) - readonly frequency="hourly" - ;; - [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) - readonly frequency="daily" - ;; - [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) - readonly frequency="weekly" - ;; - [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) - readonly frequency="monthly" - ;; - [Yy] | [Yy][Ee][Aa][Rr] | [Yy][Ee][Aa][Rr][Ll][Yy] | \ - [Aa] | [Aa][Nn][Nn][Uu][Aa][Ll] | [Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] ) - readonly frequency="yearly" - ;; - [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] ) - readonly frequency="quarterly" - ;; - [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] ) - readonly frequency="semiannually" - ;; - * ) - echo "${prefix_warning}Frequency '${_freq_input}' not understood.${prefix_reset}" 1>&2 - systemdPromptFrequency - ;; - esac - - unset _freq_input - fi + if [ -z "${frequency}" ]; then + printf "%bHow often should the service run? [(h)ourly/(d)aily/(W)eekly/(m)onthly/(q)uarterly]: " "${prefix_input}" + read -r _freq_input + + case "${_freq_input}" in + [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][Yy] ) + readonly frequency="hourly" + ;; + [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) + readonly frequency="daily" + ;; + [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) + readonly frequency="weekly" + ;; + [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) + readonly frequency="monthly" + ;; + [Yy] | [Yy][Ee][Aa][Rr] | [Yy][Ee][Aa][Rr][Ll][Yy] | \ + [Aa] | [Aa][Nn][Nn][Uu][Aa][Ll] | [Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] ) + readonly frequency="yearly" + ;; + [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] ) + readonly frequency="quarterly" + ;; + [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] ) + readonly frequency="semiannually" + ;; + * ) + echo "${prefix_warning}Frequency '${_freq_input}' not understood.${prefix_reset}" 1>&2 + systemdPromptFrequency + ;; + esac + + unset _freq_input + fi } @@ -148,7 +148,7 @@ systemdRemove() rm "${systemd_dir_path}/adsorber.timer" "${systemd_dir_path}/adsorber.service" 2>/dev/null 1>&2 \ || { printf "%bCouldn't remove Systemd service files at %s%b\\n." \ - "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 + "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 return 1 } @@ -165,6 +165,6 @@ systemdRemove() systemd() { - systemdPromptFrequency - systemdSetup + systemdPromptFrequency + systemdSetup } diff --git a/src/lib/update.sh b/src/lib/update.sh index 15a4948..af8257c 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -41,6 +41,9 @@ # shellcheck disable=SC2154 +begin_section="# BEGIN OF ADSORBER SECTION" +end_section="# END OF ADSORBER SECTION" + update_CheckBackupExist() { if [ ! -f "${hosts_file_backup_path}" ]; then @@ -190,7 +193,7 @@ update_FetchSources() done < "${tmp_dir_path}/sourceslist-filtered" - unset _domain + unset _domain if [ "${ignore_download_error}" = "false" ] && [ "${_successful_count}" -ne "${_total_count}" ]; then printf "%bCouldn't fetch all hosts sources [%d/%d]. Aborting ...\\n" "${prefix_warning}" "${_successful_count}" "${_total_count}" 1>&2 @@ -273,7 +276,7 @@ update_ApplyWhiteList() done < "${tmp_dir_path}/whitelist-sorted" - unset _domain + unset _domain cp "${tmp_dir_path}/applied-whitelist" "${tmp_dir_path}/cache" fi @@ -309,7 +312,7 @@ update_IsCacheEmpty() } -update_BuildHostsFile() +update_CreateAdsorberLines() { { # Replace #@...@# with variables @@ -320,17 +323,6 @@ update_BuildHostsFile() echo - # Add hosts.original - cat "${hosts_file_backup_path}" \ - || echo "${prefix}You may want to add your hostname to ${hosts_file_path}" 1>&2 - - echo - - # Add hosts_title - sed "s|#@blocked@#|$(wc -l < "${tmp_dir_path}/cache")|g" "${shareable_dir_path}/components/hosts_title" - - echo - # Add the fetched ad-domains to the hosts file cat "${tmp_dir_path}/cache" @@ -342,7 +334,7 @@ update_BuildHostsFile() | sed "s|#@blocked@#|$(wc -l < "${tmp_dir_path}/cache")|g" \ | sed "s|#@hosts_file_backup_path@#|${hosts_file_backup_path}|g" - } > "${tmp_dir_path}/hosts" + } > "${tmp_dir_path}/adsorber.lines" } @@ -352,10 +344,10 @@ update_PreviousHostsFile() if [ "${hosts_file_previous_enable}" = "true" ]; then echo "${prefix}Creating previous hosts file ..." - # Copy current hosts file to /etc/hosts.previous before the new hosts file will be applied + # Copy current hosts file to /etc/hosts.previous before the new hosts file will be applied cp "${hosts_file_path}" "${hosts_file_previous_path}" \ || { - printf "%bCouldn't create previous hosts file at %s%b\\n" "${prefix_fatal}" "${hosts_file_previous_path}" "${prefix_reset}" + printf "%bCouldn't create previous hosts file to %s%b\\n" "${prefix_fatal}" "${hosts_file_previous_path}" "${prefix_reset}" errorCleanUp exit 1 } @@ -366,12 +358,35 @@ update_PreviousHostsFile() } +update_RemoveAdsorberLines() +{ + sed "/${begin_section}/,/${end_section}/d" "${tmp_dir_path}/hosts.old" > "${tmp_dir_path}/hosts.clean" +} + + +update_AddAdsorberLines() +{ + update_RemoveAdsorberLines + + { + cat "${tmp_dir_path}/hosts.clean" + echo "${begin_section}" + cat "${tmp_dir_path}/adsorber.lines" + echo "${end_section}" + } >> "${tmp_dir_path}/hosts.new" +} + + update_ApplyHostsFile() { echo "${prefix}Applying new hosts file ..." + + cp "${hosts_file_path}" "${tmp_dir_path}/hosts.old" + + update_AddAdsorberLines # Replace systems hosts file with the modified version from /tmp/adsorber - cp "${tmp_dir_path}/hosts" "${hosts_file_path}" \ + cp "${tmp_dir_path}/hosts.new" "${hosts_file_path}" \ || { printf "%b" "${prefix_fatal}Couldn't apply hosts file. Aborting.${prefix_reset}\\n" 1>&2 errorCleanUp @@ -395,13 +410,13 @@ update() if update_ReadSourceList; then if update_FetchSources; then - update_FilterDomains "fetched" "fetched-filtered" - update_SortDomains "fetched-filtered" "fetched-sorted" - cp "${tmp_dir_path}/fetched-sorted" "${tmp_dir_path}/cache" - else - # Create empty cache file for the ad-domains. - touch "${tmp_dir_path}/cache" - fi + update_FilterDomains "fetched" "fetched-filtered" + update_SortDomains "fetched-filtered" "fetched-sorted" + cp "${tmp_dir_path}/fetched-sorted" "${tmp_dir_path}/cache" + else + # Create empty cache file for the ad-domains. + touch "${tmp_dir_path}/cache" + fi else # Create empty cache file for the ad-domains. touch "${tmp_dir_path}/cache" @@ -424,8 +439,8 @@ update() esac update_IsCacheEmpty - update_BuildHostsFile + update_CreateAdsorberLines update_PreviousHostsFile update_ApplyHostsFile - cleanUp + #cleanUp } diff --git a/src/share/components/hosts_header b/src/share/components/hosts_header index 4a47fc3..2ed667e 100644 --- a/src/share/components/hosts_header +++ b/src/share/components/hosts_header @@ -1,10 +1,9 @@ -# This file has been edited by Adsorber v#@version@# at #@date@# +# This section has been edited by Adsorber v#@version@# at #@date@# # and is blocking #@blocked@# domain(s). # -# To make any changes to this file please edit #@hosts_file_backup_path@# -# and run 'adsorber update' to apply your changes. -# Or else your changes will be overwritten by Adsorber once a week. +# Do not make changes in this section as they will be overwritten by Adsorber. # # By default, the domains will be updated once a week if not other specified. +# Or you can manually trigger a run with 'adsorber update' # For further information please run 'adsorber --help' # Or visit: https://github.com/stablestud/adsorber diff --git a/src/share/components/hosts_title b/src/share/components/hosts_title deleted file mode 100644 index 8587949..0000000 --- a/src/share/components/hosts_title +++ /dev/null @@ -1,2 +0,0 @@ - -# Following #@blocked@# domain(s) were inserted by Adsorber and are being blocked: From baf5471596771bd6ad65451f310f89ea33f6ab89 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 15:33:19 +0100 Subject: [PATCH 3/9] Make disable and revert inline aware --- src/bin/adsorber | 1 + src/lib/config.sh | 19 +----------- src/lib/disable.sh | 37 ++++++++++------------ src/lib/restore.sh | 41 ++++++++++++------------- src/lib/revert.sh | 24 +++++++++------ src/lib/update.sh | 39 ++++++++++++++++------- src/share/default/default-adsorber.conf | 10 +++--- 7 files changed, 85 insertions(+), 86 deletions(-) diff --git a/src/bin/adsorber b/src/bin/adsorber index 136e24f..a1913c4 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -12,6 +12,7 @@ # shellcheck disable=SC2154 readonly tmp_dir_path="/tmp/adsorber" +readonly cache_dir_path="/var/cache/adsorber" readonly debug="false" diff --git a/src/lib/config.sh b/src/lib/config.sh index 5a6459a..87dadc2 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -110,7 +110,6 @@ config_FilterConfig() sed -n "/^hosts_file_path=/p" "${tmp_dir_path}/config" sed -n "/^hosts_file_backup_path=/p" "${tmp_dir_path}/config" sed -n "/^hosts_file_previous_enable=/p" "${tmp_dir_path}/config" - sed -n "/^hosts_file_previous_path=/p" "${tmp_dir_path}/config" sed -n "/^systemd_dir_path=/p" "${tmp_dir_path}/config" } > "${tmp_dir_path}/config-filtered" } @@ -197,13 +196,6 @@ config_ReadConfig() echo "${prefix_warning}Duplicate configuration for 'hosts_file_previous_enable', using the first value: ${hosts_file_previous_enable}" fi ;; - hosts_file_previous_path=* ) - if [ -z "${hosts_file_previous_path}" ]; then - readonly "${_line}" - else - echo "${prefix_warning}Duplicate configuration for 'hosts_file_previous_path', using the first value: ${hosts_file_previous_path}" - fi - ;; systemd_dir_path=* ) if [ -z "${systemd_dir_path}" ]; then readonly "${_line}" @@ -336,15 +328,6 @@ config_IsVariableValid() invalidVariable="true" fi - if [ "${hosts_file_previous_enable}" = "true" ]; then - if [ ! -d "$(dirname "${hosts_file_previous_path}")" ] || [ ! -w "$(dirname "${hosts_file_previous_path}")" ]; then - printf "%bInvalid 'hosts_file_previous_path' set in adsorber.conf. Can't access: %s%b\\n" \ - "${prefix_fatal}" "$(dirname "${hosts_file_previous_path}")" "${prefix_reset}" 1>&2 - - invalidVariable="true" - fi - fi - if [ ! -d "$(dirname "${tmp_dir_path}")" ] || [ ! -w "$(dirname "${tmp_dir_path}")" ]; then printf "%bInvalid 'tmp_dir_path' set in adsorber.conf. Can't access: %s%b\\n" \ "${prefix_fatal}" "$(dirname "${tmp_dir_path}")" "${prefix_reset}" 1>&2 @@ -372,9 +355,9 @@ config_PrintVariables() echo " - hosts_file_path: ${hosts_file_path}" echo " - hosts_file_backup_path: ${hosts_file_backup_path}" echo " - hosts_file_previous_enable: ${hosts_file_previous_enable}" - echo " - hosts_file_previous_path: ${hosts_file_previous_path}" echo " - systemd_dir_path: ${systemd_dir_path}" echo " - tmp_dir_path: ${tmp_dir_path}" + echo " - cache_dir_path: ${cache_dir_path}" echo " - executable_dir_path: ${executable_dir_path}" echo " - library_dir_path: ${library_dir_path}" echo " - shareable_dir_path: ${shareable_dir_path}" diff --git a/src/lib/disable.sh b/src/lib/disable.sh index 317944b..1cd51fe 100755 --- a/src/lib/disable.sh +++ b/src/lib/disable.sh @@ -12,6 +12,7 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: # ---variable:---------- ---default value:-- ---defined in:------------------- +# cache_dir_path /var/cache7adsorber src/bin/adsorber # hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf # hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf # prefix ' ' (two spaces) src/lib/colours.sh @@ -24,11 +25,12 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# --function:-- ---function defined in:--- -# cleanUp src/lib/cleanup.sh -# errorCleanUp src/lib/cleanup.sh -# systemdRemove src/lib/systemd/systemd.sh -# crontabRemove src/lib/cron/cron.sh +# --function:-- ---function defined in:--- +# cleanUp src/lib/cleanup.sh +# errorCleanUp src/lib/cleanup.sh +# systemdRemove src/lib/systemd/systemd.sh +# crontabRemove src/lib/cron/cron.sh +# restore_HostsFile src/lib/restore.sh # shellcheck disable=SC2154 @@ -56,29 +58,22 @@ disable_Prompt() disable_HostsFile() { - # Moves the original hosts file (backed-up at /etc/hosts.original) - # to /etc/hosts, replacing the current one - if [ -f "${hosts_file_backup_path}" ]; then - mv "${hosts_file_backup_path}" "${hosts_file_path}" \ - && echo "${prefix}Successfully restored ${hosts_file_path}" + restore_HostsFile + if restore_HostsFile + then + echo "${prefix}Successfully restored ${hosts_file_path}" else - # If /etc/hosts.original was not found, abort and call the error clean-up function - printf "%bCan not restore hosts file. Original hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 - echo "${prefix}Maybe you've already disabled Adsorber?" 1>&2 errorCleanUp exit 1 fi } -disable_PreviousHostsFile() +disable_RemoveCache() { - # If found, remove /etc/hosts.previous - if [ -f "${hosts_file_previous_path}" ]; then - rm "${hosts_file_previous_path}" \ - && echo "${prefix}Removed previous hosts file." - else - echo "${prefix}Previous hosts file does not exist. Ignoring ..." + if [ -d "${cache_dir_path}" ]; then + echo "${prefix}Removing cache ${cache_dir_path}" + rm -rf "${cache_dir_path}" fi } @@ -90,7 +85,7 @@ disable() disable_Prompt systemdRemove crontabRemove + disable_RemoveCache disable_HostsFile - disable_PreviousHostsFile cleanUp } diff --git a/src/lib/restore.sh b/src/lib/restore.sh index 0fca36e..a1b2e78 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -21,33 +21,28 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------------ ---function defined in:--- -# cleanUp src/lib/cleanup.sh -# errorCleanUp src/lib/cleanup.sh -# update_PreviousHostsFile src/lib/update.sh +# ---function:-------------- --function defined in:-- +# cleanUp src/lib/cleanup.sh +# errorCleanUp src/lib/cleanup.sh +# update_RemoveAdsorberLines src/lib/update.sh # shellcheck disable=SC2154 +restore_Error() +{ + printf "%bCouldn't restore %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 + errorCleanUp + exit 1 +} + restore_HostsFile() { - if [ -f "${hosts_file_backup_path}" ]; then - update_PreviousHostsFile - # Copy /etc/hosts.original to /etc/hosts, replacing the current one - cp "${hosts_file_backup_path}" "${hosts_file_path}" \ - || { - printf "%bCouldn't restore %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" - errorCleanUp - exit 1 - } - - printf "%bSuccessfully restored %s.\\n" "${prefix}" "${hosts_file_path}" - printf "%bTo reapply please run 'adsorber update'.\\n" "${prefix}" - else - # If /etc/hosts.previous was not found, abort and call error clean-up function - printf "%bCan't restore original hosts file. Original hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 - errorCleanUp - exit 1 - fi + update_CreateTmpDir + cp "${hosts_file_path}" "${tmp_dir_path}/hosts.old" \ + || restore_Error + update_RemoveAdsorberLines + cp "${tmp_dir_path}/hosts.clean" "${hosts_file_path}" \ + || restore_Error } @@ -56,5 +51,7 @@ restore() { printf "%bRestoring %s ...%b\\n" "${prefix_title}" "${hosts_file_path}" "${prefix_reset}" restore_HostsFile + printf "%bRestored %s.\\n" "${prefix}" "${hosts_file_path}" + printf "%bTo reapply please run 'adsorber update'.\\n" "${prefix}" cleanUp } diff --git a/src/lib/revert.sh b/src/lib/revert.sh index 6382103..2d0aae7 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -21,30 +21,36 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# --function:-- ---function defined in:--- -# cleanUp src/lib/cleanup.sh -# errorCleanUp src/lib/cleanup.sh +# --function:-------------- ---function defined in:--- +# cleanUp src/lib/cleanup.sh +# errorCleanUp src/lib/cleanup.sh +# update_CreateTmpDir src/lib/update.sh +# update_RemoveAdsorberLines src/lib/update.sh # shellcheck disable=SC2154 revert_HostsFile() { - if [ -f "${hosts_file_previous_path}" ]; then - # Copy /etc/hosts.previous to /etc/hosts, replacing the current one - cp "${hosts_file_previous_path}" "${hosts_file_path}" \ + if [ -f "${cache_dir_path}/adsorber.hosts.old" ]; then + update_CreateTmpDir \ + && cp "${hosts_file_path}" "${tmp_dir_path}/hosts.old" \ + && update_RemoveAdsorberLines \ + && cp "${cache_dir_path}/adsorber.hosts.old" "${tmp_dir_path}/adsorber.hosts" \ + && update_AddAdsorberLines \ + && cp "${tmp_dir_path}/hosts.new" "${hosts_file_path}" \ || { printf "%bCouldn't revert %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" errorCleanUp exit 1 } - # Remove previous host file notice from /etc/hosts.previous in /etc/hosts and also the line before - sed -n -i '/^## This was the hosts file/{n; $p; x; d}; x; 1!p; ${x;p;}' "${hosts_file_path}" + cp "${cache_dir_path}/adsorber.hosts" "${cache_dir_path}/adsorber.hosts.old" + cp "${tmp_dir_path}/adsorber.hosts" "${cache_dir_path}/adsorber.hosts" printf "%bSuccessfully reverted %s.\\n" "${prefix}" "${hosts_file_path}" else # If /etc/hosts.previous was not found, abort and call error clean-up function - printf "%bCan't revert to previous hosts file. Previous hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bCan't revert to previous hosts domains.. Previous hosts '${cache_dir_path}/adsorber.hosts.old' does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 errorCleanUp exit 1 fi diff --git a/src/lib/update.sh b/src/lib/update.sh index af8257c..64b08ec 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -76,7 +76,11 @@ update_CreateTmpDir() # Create a temporary folder in which Adsorber can manipulate files # without distracting the environment if [ ! -d "${tmp_dir_path}" ]; then - mkdir "${tmp_dir_path}" + mkdir "${tmp_dir_path}" \ + || { + printf "%bCannot create tmp dir '${tmp_dir_path}'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + exit 1 + } elif [ ! -s "${tmp_dir_path}/config-filtered" ]; then echo "${prefix}Removing previous tmp folder ..." rm -rf "${tmp_dir_path}" @@ -85,6 +89,19 @@ update_CreateTmpDir() } +update_CreateCacheDir() +{ + # Cache dir is used to save the previous host domains + if [ ! -d "${cache_dir_path}" ]; then + mkdir "${cache_dir_path}" \ + || { + printf "%bCannot create cache dir '${cache_dir_path}'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + errorCleanUp + exit 130 + } + fi +} + update_ReadSourceList() { # Read the sources.list (by default located at /usr/local/etc/adsorber) @@ -334,7 +351,7 @@ update_CreateAdsorberLines() | sed "s|#@blocked@#|$(wc -l < "${tmp_dir_path}/cache")|g" \ | sed "s|#@hosts_file_backup_path@#|${hosts_file_backup_path}|g" - } > "${tmp_dir_path}/adsorber.lines" + } > "${tmp_dir_path}/adsorber.hosts" } @@ -342,18 +359,17 @@ update_PreviousHostsFile() { # Check if we should backup the previous hosts file if [ "${hosts_file_previous_enable}" = "true" ]; then - echo "${prefix}Creating previous hosts file ..." + echo "${prefix}Creating backup of current ad-domains" - # Copy current hosts file to /etc/hosts.previous before the new hosts file will be applied - cp "${hosts_file_path}" "${hosts_file_previous_path}" \ + if [ -f "${cache_dir_path}/adsorber.hosts" ]; then + cp "${cache_dir_path}/adsorber.hosts" "${cache_dir_path}/adsorber.hosts.old" + fi + cp "${tmp_dir_path}/adsorber.hosts" "${cache_dir_path}" \ || { - printf "%bCouldn't create previous hosts file to %s%b\\n" "${prefix_fatal}" "${hosts_file_previous_path}" "${prefix_reset}" + printf "%bCouldn't create previous hosts file to %s%b\\n" "${prefix_fatal}" "${cache_path}/adsorber.lines" "${prefix_reset}" 1>&2 errorCleanUp exit 1 } - - echo >> "${hosts_file_previous_path}" - echo "## This was the hosts file before $(date +'%b %e %X')" >> "${hosts_file_previous_path}" fi } @@ -371,7 +387,7 @@ update_AddAdsorberLines() { cat "${tmp_dir_path}/hosts.clean" echo "${begin_section}" - cat "${tmp_dir_path}/adsorber.lines" + cat "${tmp_dir_path}/adsorber.hosts" echo "${end_section}" } >> "${tmp_dir_path}/hosts.new" } @@ -405,6 +421,7 @@ update() update_CheckBackupExist update_CreateTmpDir + update_CreateCacheDir update_ReadBlackList update_ReadWhiteList @@ -442,5 +459,5 @@ update() update_CreateAdsorberLines update_PreviousHostsFile update_ApplyHostsFile - #cleanUp + cleanUp } diff --git a/src/share/default/default-adsorber.conf b/src/share/default/default-adsorber.conf index dbb4549..efa8bdd 100644 --- a/src/share/default/default-adsorber.conf +++ b/src/share/default/default-adsorber.conf @@ -92,16 +92,16 @@ hosts_file_backup_path=/etc/hosts.original ## hosts_file_previous_enable, hosts_file_previous_path -# Set where and if the previous hosts file should be backed-up before creating -# a new one. To return back to the previous host file run 'adsorber revert'. +# Set if the previous applied ad-domains should be backed-up before applying +# a new version. To return back to the previous hosts run 'adsorber revert'. +# +# This option is useful if you want to rollback to the last applied version of ad-domains. +# If for example the current list has less or no ad-domains. # # Possible values for 'hosts_file_previous_enable': true, false # Default value for 'hosts_file_previous_enable': true -# -# Default value for 'hosts_file_previous_path': /etc/hosts.previous hosts_file_previous_enable=true -hosts_file_previous_path=/etc/hosts.previous ## systemd_dir_path From c8eb823736e41c1f50e44958e1212f703c17b6ba Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 15:42:13 +0100 Subject: [PATCH 4/9] Bump version to 1.0.0 --- README.md | 18 ++++++++++-------- src/bin/adsorber | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6ceeefc..0fab950 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Adsorber -[![Latest version](https://img.shields.io/badge/latest-v0.5.2-brightgreen.svg)](https://github.com/stablestud/adsorber/releases) +[![Latest version](https://img.shields.io/badge/latest-v1.0.0-brightgreen.svg)](https://github.com/stablestud/adsorber/releases) [![License](https://img.shields.io/github/license/stablestud/adsorber.svg)](https://github.com/stablestud/adsorber/blob/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/stablestud/adsorber.svg)](https://github.com/stablestud/adsorber/stargazers) @@ -84,11 +84,12 @@ The script also works on [portable mode](#portable-mode) setups. However running #### Manual removal If you prefer manual removal, here you go: -1. copy: -* `/etc/hosts.original` to `/etc/hosts`, overwriting it +1. delete inline: +* `/etc/hosts` + Delete everything between the lines: + `# BEGIN OF ADSORBER SECTION` and `# END OF ADSORBER SECTION` 2. remove: * `/etc/hosts.original` -* `/etc/hosts.previous` * `/usr/local/bin/adsorber` * `/usr/local/etc/adsorber/` * `/usr/local/lib/adsorber/` @@ -99,7 +100,8 @@ If you prefer manual removal, here you go: * `/etc/cron.daily/80adsorber` * `/etc/cron.weekly/80adsorber` * `/etc/cron.monthly/80adsorber` -* **`/var/log/adsorber.log`** +* `/var/cache/adsorber` +* **`/var/log/adsorber.log`** <- won't be removed automatically * `/tmp/adsorber/` Not all files of the above will exist, so dont worry if they are not found. @@ -207,9 +209,9 @@ Possible options are: Reverts the last applied hosts-file by Adsorber. The command will: -* copy `/etc/hosts.previous` to `/etc/hosts`, overwriting the newest `/etc/hosts`. +* downgrade the `/etc/hosts` with ad-domains applied previously -This is useful if the newest hosts file contains less or no ad-domains, because a source +This is useful if the newest hosts file contains less or no ad-domains, because a source was unreachable and you don't want to loose the ad-servers supplied previously from this source. Possible option: @@ -238,7 +240,7 @@ To completely uninstall see [removal](#removal). The command will: * disable all schedulers (systemd, cronjob) * restore the hosts file to it's original state -* remove all leftovers (previous hosts file, etc) +* remove all leftovers (cache, etc) Possible options are: * [`-y`, `--yes`, `--assume-yes`](#-y---yes---assume-yes) diff --git a/src/bin/adsorber b/src/bin/adsorber index a1913c4..8885102 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -30,7 +30,7 @@ readonly log_file_path="/var/log/adsorber.log" -readonly version="0.5.2" +readonly version="1.0.0" readonly operation="${1}" if [ "${#}" -ne 0 ]; then From 65617d748b680248110e6a6f63da1fd46c9e565c Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 16:03:28 +0100 Subject: [PATCH 5/9] Update description for disable and revert operations --- README.md | 2 +- src/bin/adsorber | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0fab950..4ae986d 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ Possible options are: Reverts the last applied hosts-file by Adsorber. The command will: -* downgrade the `/etc/hosts` with ad-domains applied previously +* downgrade `/etc/hosts` with ad-domains applied previously This is useful if the newest hosts file contains less or no ad-domains, because a source was unreachable and you don't want to loose the ad-servers supplied previously from this source. diff --git a/src/bin/adsorber b/src/bin/adsorber index 8885102..4f1df21 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -96,7 +96,7 @@ showHelp() echo " update - update hosts file with newest ad-domains" echo " restore - restore hosts file to its original state" echo " (it does not remove the scheduler, this should be used temporary)" - echo " revert - reverts the hosts file to the last applied (previous) host file." + echo " revert - reverts the hosts file to the last applied (previous) ad-domains" echo " disable - completely remove changes made by Adsorber" echo " e.g., disable scheduler (if set)" echo " restore hosts file to its original state" @@ -201,11 +201,11 @@ showSpecificHelp() revert ) printf "\\033[4;37madsorber revert []\\033[0m:\\n" echo - echo "Revert the current hosts file to the previously applied hosts file." + echo "Revert the current hosts file to the previously applied ad-domains" echo "Use it if the current host file has been corrupted or misses previously applied ad-domains." echo echo "The command will:" - echo " - copy /etc/hosts.previous to /etc/hosts, overwriting the current host file." + echo " - downgrade /etc/hosts with ad-domains applied previously" echo echo "To get the latest ad-domains run 'adsorber update'." echo @@ -221,7 +221,7 @@ showSpecificHelp() echo "The command will:" echo " - disable/remove all schedulers (systemd, cronjob)" echo " - restore the hosts file to it's original state" - echo " - remove all leftovers (e.g. /tmp/adsorber, previous hosts files, etc)" + echo " - remove all leftovers (e.g. /tmp/adsorber, cache, etc)" echo echo "It'll not remove/uninstall the 'adsorber' command from the system." echo "To do this you need to run the 'remove_files_from_system.sh' script." From b45bff88cc830e88364aec19079d7fce146ac377 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 16:06:54 +0100 Subject: [PATCH 6/9] Add cache removal instructions to removal script --- remove_files_from_system.sh | 75 +++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 34596d8..699ab65 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -31,7 +31,8 @@ readonly hosts_file_backup_path="/etc/hosts.original" readonly hosts_file_previous_path="/etc/hosts.previous" readonly systemd_dir_path="/etc/systemd/system" readonly tmp_dir_path="/tmp/adsorber" -readonly log_file_path="/var/log/adsorber.log" # Log file won't be removed +readonly cache_dir_path="/var/cache/adsorber" +readonly log_file_path="/var/log/adsorber.log" # Log file won't be removed ##########[ End of configuration ]############################################## @@ -40,7 +41,7 @@ readonly script_dir_path="$(cd "$(dirname "${0}")" && pwd)" printLocation() { - echo "Going to remove files from:" + echo "Going to remove files from:" echo " - main exectuable: ${executable_path}" echo " - other executables: ${library_dir_path}" echo " - configuration: ${config_dir_path}" @@ -66,20 +67,20 @@ printHelp() echo printLocation - exit 0 + exit 0 } prompt="${1}" if [ "${prompt}" = "help" ] || [ "${prompt}" = "h" ] || [ "${prompt}" = "-h" ] || [ "${prompt}" = "--help" ]; then - printHelp + printHelp fi # Prompt user if sure about to remove Adsorber from the system if [ -z "${prompt}" ]; then - printf "Are you sure you want to remove Adsorber from the system? [(y)es/(N)o]: " + printf "Are you sure you want to remove Adsorber from the system? [(y)es/(N)o]: " read -r prompt fi @@ -103,59 +104,59 @@ fi # Run Adsorber's own removal, if it fails do it manually if command -v adsorber 1>/dev/null; then - printf "\\nRunning 'adsorber disable -y --noformatting' ...\\n\\n" - ( adsorber "disable" "-y" "--noformatting" ) \ - || { - echo - printf "\\033[0;93mSomething went wrong at running Adsorber's own disable operation.\\nNo worries, I can handle it ...\\n\\033[0m" - echo "Maybe Adsorber has been already removed ?" - readonly _hard_way="true" - } + printf "\\nRunning 'adsorber disable -y --noformatting' ...\\n\\n" + ( adsorber "disable" "-y" "--noformatting" ) \ + || { + echo + printf "\\033[0;93mSomething went wrong at running Adsorber's own disable operation.\\nNo worries, I can handle it ...\\n\\033[0m" + echo "Maybe Adsorber has been already removed ?" + readonly _hard_way="true" + } else - readonly _hard_way="true" + readonly _hard_way="true" fi # Doing it the hard way .., removing everything manually if [ "${_hard_way}" = "true" ]; then - printf "\\nTrying portable_adsorber.sh ... " - - if "${script_dir_path}/portable_adsorber.sh" "disable" "-y" "--noformatting" 2>/dev/null 1>&2; then - printf "found\\n" - printf "Removed Adsorber successfully\\n" - else - printf "no luck\\n" - "${script_dir_path}/misc/clean.sh" 2>/dev/null 1>&2 - fi - + printf "\\nTrying portable_adsorber.sh ... " + + if "${script_dir_path}/portable_adsorber.sh" "disable" "-y" "--noformatting" 2>/dev/null 1>&2; then + printf "found\\n" + printf "Removed Adsorber successfully\\n" + else + printf "no luck\\n" + "${script_dir_path}/misc/clean.sh" 2>/dev/null 1>&2 + fi rm "${systemd_dir_path}/adsorber.timer" 2>/dev/null && echo "Removed ${systemd_dir_path}/adsorber.timer" rm "${systemd_dir_path}/adsorber.service" 2>/dev/null && echo "Removed ${systemd_dir_path}/adsorber.service" systemctl daemon-reload 2>/dev/null && echo "Reloaded systemctl daemon" - # Remove all crontabs + # Remove all crontabs if [ -f "/etc/cron.hourly/80adsorber" ]; then - rm "/etc/cron.hourly/80adsorber" 2>/dev/null \ - && echo "Removed cronjob from /etc/cron.hourly/" - fi + rm "/etc/cron.hourly/80adsorber" 2>/dev/null \ + && echo "Removed cronjob from /etc/cron.hourly/" + fi if [ -f "/etc/cron.daily/80adsorber" ]; then - rm "/etc/cron.daily/80adsorber" 2>/dev/null \ - && echo "Removed cronjob from /etc/cron.daily/" - fi + rm "/etc/cron.daily/80adsorber" 2>/dev/null \ + && echo "Removed cronjob from /etc/cron.daily/" + fi if [ -f "/etc/cron.weekly/80adsorber" ]; then - rm "/etc/cron.weekly/80adsorber" 2>/dev/null \ - && echo "Removed cronjob from /etc/cron.weekly/" - fi + rm "/etc/cron.weekly/80adsorber" 2>/dev/null \ + && echo "Removed cronjob from /etc/cron.weekly/" + fi if [ -f "/etc/cron.monthly/80adsorber" ]; then - rm "/etc/cron.monthly/80adsorber" 2>/dev/null \ - && echo "Removed cronjob from /etc/cron.monthly/" - fi + rm "/etc/cron.monthly/80adsorber" 2>/dev/null \ + && echo "Removed cronjob from /etc/cron.monthly/" + fi rm -r "${tmp_dir_path}" 2>/dev/null && echo "Removed ${tmp_dir_path}" + rm -r "${cache_dir_path}" 2>/dev/null && echo "Removed ${cache_dir_path}" if [ -f "${hosts_file_backup_path}" ]; then echo "Backup of hosts file found at ${hosts_file_backup_path}" From 865dcd0fc51ba25ed2ef055c49ceca3582664ac4 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 16:16:12 +0100 Subject: [PATCH 7/9] Add fallback revert option --- src/lib/restore.sh | 2 +- src/lib/revert.sh | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lib/restore.sh b/src/lib/restore.sh index a1b2e78..3e90fd1 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -30,7 +30,7 @@ restore_Error() { - printf "%bCouldn't restore %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 + printf "%bCannot restore %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 errorCleanUp exit 1 } diff --git a/src/lib/revert.sh b/src/lib/revert.sh index 2d0aae7..6bd545b 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -48,9 +48,22 @@ revert_HostsFile() cp "${tmp_dir_path}/adsorber.hosts" "${cache_dir_path}/adsorber.hosts" printf "%bSuccessfully reverted %s.\\n" "${prefix}" "${hosts_file_path}" - else + elif [ -f "${cache_dir_path}/adsorber.hosts" ]; then + # Fallback if actual previous ad-domains save does not exist + update_CreateTmpDir \ + && cp "${hosts_file_path}" "${tmp_dir_path}/hosts.old" \ + && update_RemoveAdsorberLines \ + && cp "${cache_dir_path}/adsorber.hosts" "${tmp_dir_path}/adsorber.hosts" \ + && update_AddAdsorberLines \ + && cp "${tmp_dir_path}/hosts.new" "${hosts_file_path}" \ + || { + printf "%bCouldn't revert %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" + errorCleanUp + exit 1 + } + else # If /etc/hosts.previous was not found, abort and call error clean-up function - printf "%bCan't revert to previous hosts domains.. Previous hosts '${cache_dir_path}/adsorber.hosts.old' does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bCannot revert to previous ad-domains.. Previous save '${cache_dir_path}/adsorber.hosts.old' does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 errorCleanUp exit 1 fi From bd23096036c8e4b7bc0ae982a2ae81c86175e2a4 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 31 Oct 2020 16:39:48 +0100 Subject: [PATCH 8/9] Improve output to stdout when restoring --- src/lib/restore.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/restore.sh b/src/lib/restore.sh index 3e90fd1..b0bda77 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -51,7 +51,7 @@ restore() { printf "%bRestoring %s ...%b\\n" "${prefix_title}" "${hosts_file_path}" "${prefix_reset}" restore_HostsFile - printf "%bRestored %s.\\n" "${prefix}" "${hosts_file_path}" + printf "%bSuccessfully purged adsorber section from %s.\\n" "${prefix}" "${hosts_file_path}" printf "%bTo reapply please run 'adsorber update'.\\n" "${prefix}" cleanUp } From 0b4b58f4bd3cc67e0479ba71f1b428432090b6ed Mon Sep 17 00:00:00 2001 From: stablestud Date: Sun, 1 Nov 2020 03:21:31 +0100 Subject: [PATCH 9/9] Fix missing message in revert --- src/lib/revert.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/revert.sh b/src/lib/revert.sh index 6bd545b..772ae58 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -47,7 +47,7 @@ revert_HostsFile() cp "${cache_dir_path}/adsorber.hosts" "${cache_dir_path}/adsorber.hosts.old" cp "${tmp_dir_path}/adsorber.hosts" "${cache_dir_path}/adsorber.hosts" - printf "%bSuccessfully reverted %s.\\n" "${prefix}" "${hosts_file_path}" + printf "%bSuccessfully reverted %s with %s\\n" "${prefix}" "${hosts_file_path}" "${cache_dir_path}/adsorber.hosts.old" elif [ -f "${cache_dir_path}/adsorber.hosts" ]; then # Fallback if actual previous ad-domains save does not exist update_CreateTmpDir \ @@ -61,6 +61,7 @@ revert_HostsFile() errorCleanUp exit 1 } + printf "%bSuccessfully reverted %s with %s\\n" "${prefix}" "${hosts_file_path}" "${cache_dir_path}/adsorber.hosts" else # If /etc/hosts.previous was not found, abort and call error clean-up function printf "%bCannot revert to previous ad-domains.. Previous save '${cache_dir_path}/adsorber.hosts.old' does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 @@ -73,7 +74,7 @@ revert_HostsFile() # Main function of revert.sh revert() { - printf "%bReverting %s with %s ...%b\\n" "${prefix_title}" "${hosts_file_path}" "${hosts_file_previous_path}" "${prefix_reset}" + printf "%bReverting %s with previous ad-domains ...%b\\n" "${prefix_title}" "${hosts_file_path}" "${prefix_reset}" revert_HostsFile cleanUp }