From a5a7f2316a21b3b226db139ba681a0f224597a70 Mon Sep 17 00:00:00 2001 From: stablestud Date: Fri, 1 Jun 2018 15:04:34 +0200 Subject: [PATCH 01/43] install is now setup --- README.md | 52 ++++++++++++++++---------------- misc/clean.sh | 2 ++ src/bin/adsorber | 33 ++++++++++---------- src/lib/config.sh | 2 +- src/lib/cron/80adsorber | 2 +- src/lib/cron/cron.sh | 10 +++--- src/lib/remove.sh | 32 ++++++++++---------- src/lib/{install.sh => setup.sh} | 35 ++++++++++----------- src/lib/systemd/systemd.sh | 12 ++++---- src/lib/update.sh | 8 ++--- 10 files changed, 94 insertions(+), 94 deletions(-) rename src/lib/{install.sh => setup.sh} (77%) diff --git a/README.md b/README.md index 1fcd96e..a936054 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Adsorber -[![Latest version](https://img.shields.io/badge/latest-v0.4.0-brightgreen.svg)](https://github.com/stablestud/adsorber/releases) +[![Latest version](https://img.shields.io/badge/latest-v0.5.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) -(Ad)sorber blocks ads by 'absorbing' and dumbing them into void. +(Ad)sorber blocks ads by 'absorbing' and dumbing them into void. Technically speaking, it adds ad-domains to the hosts file `/etc/hosts` with a redirection to a non-existent ip `0.0.0.0`. ## Features @@ -25,12 +25,12 @@ To add your own hosts sources, just add them to the `sources.list` file. ## Usage -### First steps .. installation to system -Adsorber can be installed into the system (recommended) for a more stable and robust usage. -This can be achieved through running `install_to_system.sh`, files will placed to `/usr/local/` if not other specified. +### First steps .. installation to system or portable mode +Adsorber can be installed into the system (recommended) for a more stable and robust usage. +This can be achieved through running `install_to_system.sh`, files will placed to `/usr/local/` if not other specified. If you don't want Adsorber to be installed into your system run `portable_adsorber.sh`. -Installing Adsorber into the system has the advantage that Adsorber can run independently from the user who downloaded it. +Installing Adsorber into the system has the advantage that Adsorber can run independently from the user who downloaded it. Also it prevents broken cronjobs/services as there is no risk that the files/directory of Adsorber will be accidentally deleted or moved. To configure where it should be installed (to fit your system), edit the relevant lines in `install_to_system.sh` and `remove_from_system.sh`. For more help about the specific file run it with `--help`. @@ -43,7 +43,7 @@ Usage: adsorber [] (with the help of the hosts file) Operations: - install - setup necessary things needed for Adsorber + setup - setup necessary things needed for Adsorber e.g., create backup file of hosts file, create scheduler which updates the host file once a week However this should've been done automatically. @@ -59,8 +59,8 @@ Operations: Options: (optional) -s, --systemd - use Systemd ... - -c, --cron - use Cronjob as scheduler (use with 'install') - -ns, --no-scheduler - skip scheduler creation (use with 'install') + -c, --cron - use Cronjob as scheduler (use with 'setup') + -ns, --no-scheduler - skip scheduler creation (use with 'setup') -y, --yes, --assume-yes - answer all prompts with 'yes' -f, --force - force the update if no /etc/hosts backup has been created (dangerous) @@ -72,13 +72,13 @@ If you encounter any issues please report them to the Github repository. ### Operations (required): Note: to get further information about a operation run `adsorber --help` -#### `adsorber install {options}`: +#### `adsorber setup {options}`: You should run this command first. The command will: * backup your `/etc/hosts` file to `/etc/hosts.original` (if not other specified in `adsorber.conf`) -* install a scheduler which updates your hosts file with ad-server domains once a week. (either systemd, cronjob or none) -* install the newest ad-server domains in your hosts file. +* setup a scheduler which updates your hosts file with ad-server domains once a week. (either systemd, cronjob or none) +* fetch the newest ad-server domains in your hosts file. (same as `update`) Possible options are: * `-s, --systemd` @@ -117,8 +117,8 @@ To restore the hosts file temporary, without removing the backup. The command will: * copy `/etc/hosts.original` to `/etc/hosts`, overwriting the modified `/etc/hosts` by Adsorber. -Important: If you have a scheduler installed, it'll re-apply ad-server domains to your hosts file when triggered. -For this reason this command is used to temporary disable Adsorber, e.g. when it's blocking some sites you need access for a short period of time. +Important: If Adsorber's scheduler was set-up, it'll re-apply ad-server domains to your hosts file when triggered. +For this reason this command is used to temporary disable Adsorber, e.g. when it's blocking some sites you need access for a short period of time. To re-apply run `adsorber update` Possible option: @@ -139,36 +139,36 @@ Possible options are: ### Options (optional): #### `-s, --systemd`: -Use with `install`. -* Installs systemd scheduler, skipping the scheduler prompt. Files are placed into `/etc/systemd/system` by default. +Use with `setup`. +* Setup systemd scheduler, skipping the scheduler prompt. Files are placed into `/etc/systemd/system` by default. #### `-c, --cronjob`: -Use with `install`. -* Installs the cron scheduler, skipping the scheduler prompt. File is placed into `/etc/cron.weekly/` by default. +Use with `setup`. +* Setup the cron scheduler, skipping the scheduler prompt. File is placed into `/etc/cron.weekly/` by default. #### `-ns, --no-scheduler`: -Use with `install` -* Skips the installation of a scheduler. You'll need to update Adsorber manually with `adsorber update`. +Use with `setup` +* Skips the setup of a scheduler. You'll need to update Adsorber manually with `adsorber update`. #### `-y, --yes, --assume-yes`: Answers all prompts with `yes` e.g., -* `Do you really want to install Adsorber?` +* `Do you really want to setup Adsorber?` * `Do you really want to remove Adsorber?` It'll not answer prompts which may harm your system. But `--force` will do it. #### `-f, --force`: -This will force the script to continue (dangerous) the update e.g., +This will force the script to continue (dangerous) the update e.g., * Continue if no backup has been created, overwriting the existing hosts file. #### `-h, --help`: If specified in conjunction with an operation, it'll show extended help about the operation. ## Settings: -To add or remove ad-domain sources edit the `soures.list` file which is created after the installation of Adsorber. -For a general configuration of Adsorber e.g., the path of the crontab installation, edit `adsorber.conf` +To add or remove ad-domain sources edit the `soures.list` file which is created after the setup of Adsorber. +For a general configuration of Adsorber e.g., the path of the crontab, edit `adsorber.conf` To add domains to the `whilelist` or `blacklist` edit the relevant files at the default config location. -The configuration's default location is at `/usr/local/etc/adsorber/` if installed to system. +The configuration's default location is at `/usr/local/etc/adsorber/` if installed to system. If not, the config files should be placed at the scripts root directory. ## Todo for future releases -You're free to implement things listed/not listed in [`TODO.md`](https://github.com/stablestud/adsorber/blob/master/TODO.md) to Adsorber. +You're free to implement things listed/not listed in [`TODO.md`](https://github.com/stablestud/adsorber/blob/master/TODO.md) to Adsorber. Any additions are appreciated. :) ## License diff --git a/misc/clean.sh b/misc/clean.sh index 6dffb16..e18fc1d 100755 --- a/misc/clean.sh +++ b/misc/clean.sh @@ -23,3 +23,5 @@ if [ -f "${source_dir_path}/sources.list" ]; then rm "${source_dir_path}/sources.list" -f echo "Removed sources.list" fi + +echo "Done." diff --git a/src/bin/adsorber b/src/bin/adsorber index 0807eaf..81d8983 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -25,7 +25,8 @@ readonly shareable_dir_path="${executable_dir_path}/../share/" readonly config_dir_path="${executable_dir_path}/../../" -readonly version="0.4.0" +readonly version="0.5.0" +readonly command="${0}" readonly operation="${1}" if [ "${#}" -ne 0 ]; then @@ -70,7 +71,7 @@ showUsage() echo "Adsorber: Invalid option: '${_wrong_option}'" 1>&2 fi - echo "Usage: adsorber []" + echo "Usage: adsorber []" echo "Try '--help' for more information." exit 80 @@ -85,7 +86,7 @@ showHelp() echo " (with the help of the hosts file)" echo "" echo "Operations (required):" - echo " install - setup necessary things needed for Adsorber" + echo " setup - setup necessary things needed for Adsorber" echo " e.g., create backup file of hosts file," echo " create scheduler which updates the host file once a week" echo " However this should've been done automatically." @@ -101,8 +102,8 @@ showHelp() echo "" echo "Options (optional):" echo " -s, --systemd - use Systemd ..." - echo " -c, --cron - use Cronjob as scheduler (use with 'install')" - echo " -ns, --no-scheduler - skip scheduler creation (use with 'install')" + echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" + echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" echo " -f, --force - force the update if no /etc/hosts backup" echo " has been created (dangerous)" @@ -118,8 +119,8 @@ showHelp() showSpecificHelp() { case "${operation}" in - install ) - printf "\\033[4;37madsorber install []\\033[0m:\\n" + setup ) + printf "\\033[4;37madsorber setup []\\033[0m:\\n" echo echo "You should run this command first." echo " (e.g. after installation to the system)" @@ -127,9 +128,9 @@ showSpecificHelp() echo "The command will:" echo " - backup your /etc/hosts file to /etc/hosts.original" echo " (if not other specified in adsorber.conf)" - echo " - install a scheduler which updates your hosts file with ad-domains" + echo " - setup a scheduler which updates your hosts file with ad-domains" echo " once a week. (either systemd, cronjob or none)" - echo " - install the newest ad-domains in your hosts file." + echo " - fetch the newest ad-domains in your hosts file." echo " (same as 'adsorber update')" echo echo "Note: this is not the same as the install_to_system.sh script." @@ -165,7 +166,7 @@ showSpecificHelp() echo "The command will:" echo " - copy /etc/hosts.original to /etc/hosts, overwriting the modified /etc/hosts by adsorber." echo - echo "Important: If you have a scheduler installed it'll re-apply ad-domains to your hosts" + echo "Important: If you have a scheduler setup it'll re-apply ad-domains to your hosts" echo "file when triggered." echo "For this reason this command is used to temporary disable Adsorber." echo "(e.g. when it's blocking some sites you need access for a short period of time)" @@ -231,8 +232,8 @@ duplicateOption() echo "Adsorber: Duplicate option for scheduler: '${_option}'" 1>&2 echo "You may only select one:" echo " -s, --systemd - use Systemd ..." - echo " -c, --cron - use Cronjob as scheduler (use with 'install')" - echo " -ns, --no-scheduler - skip scheduler creation (use with 'install')" + echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" + echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" else echo "Adsorber: Duplicate option: '${_option}'" 1>&2 showUsage @@ -276,8 +277,8 @@ checkPaths() sourceFiles() { - # shellcheck source=../src/lib/install.sh - . "${library_dir_path}/install.sh" + # shellcheck source=../src/lib/setup.sh + . "${library_dir_path}/setup.sh" # shellcheck source=../src/lib/remove.sh . "${library_dir_path}/remove.sh" # shellcheck source=../src/lib/update.sh @@ -355,11 +356,11 @@ for _option in "${@}"; do done case "${operation}" in - install ) + setup ) checkForWrongParameters checkRoot config - install + setup update ;; remove ) diff --git a/src/lib/config.sh b/src/lib/config.sh index 322a0b4..526383e 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -295,7 +295,7 @@ config_IsVariableSet() # if not abort, and call error clean-up function if [ -z "${hosts_file_path}" ] || [ -z "${hosts_file_backup_path}" ] || [ -z "${crontab_dir_path}" ] || [ -z "${systemd_dir_path}" ] || [ -z "${hosts_file_previous_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 install' to create a new config file.\\n" "${prefix_warning}" "${config_dir_path}" 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 remove_ErrorCleanUp exit 127 fi diff --git a/src/lib/cron/80adsorber b/src/lib/cron/80adsorber index 444693b..f11bb6c 100755 --- a/src/lib/cron/80adsorber +++ b/src/lib/cron/80adsorber @@ -6,7 +6,7 @@ # For more information: https://github.com/stablestud/adsorber # Or send me an email: # -# adsorber install --cron: +# adsorber setup --cron: # By default this file is going to be copied into /etc/cron.weekly/ if not # other specified. diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index f696689..c7aba5e 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -27,9 +27,9 @@ # shellcheck disable=SC2154 -crontabInstall() +crontabSetup() { - echo "${prefix}Installing cronjob ..." + echo "${prefix}Setting up cronjob ..." # Check if crontabs directory variable is correctly set, if not abort and call the error clean-up function if [ ! -d "${crontab_dir_path}" ]; then @@ -48,9 +48,9 @@ crontabInstall() chmod u=rwx,g=rx,o=rx "${crontab_dir_path}/80adsorber" chown root:root "${crontab_dir_path}/80adsorber" - # Make known that we have installed the crontab in this run, + # Make known that we have setup the crontab in this run, # if we fail now, crontab will be also removed (see remove_ErrorCleanUp) - readonly installed_scheduler="cronjob" + readonly setup_scheduler="cronjob" return 0 } @@ -68,7 +68,7 @@ crontabRemove() echo "${prefix}Removed Adsorber's cronjob." else - echo "${prefix}Cronjob not installed. Skipping ..." + echo "${prefix}Cronjob not setup. Skipping ..." fi return 0 diff --git a/src/lib/remove.sh b/src/lib/remove.sh index f367b87..11d54e5 100755 --- a/src/lib/remove.sh +++ b/src/lib/remove.sh @@ -11,19 +11,19 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:------- ---default value:--- ---defined in:------------------ -# backedup Null (not set) src/lib/install.sh -# hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf -# hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf -# installed_scheduler Null (not set) src/lib/systemd/systemd.sh, src/lib/cron/cron.sh -# prefix ' ' (two spaces) src/lib/colours.sh -# prefix_fatal '\033[0;91mE ' src/lib/colours.sh -# prefix_input ' ' (two spaces) src/lib/colours.sh -# prefix_reset \033[0m src/lib/colours.sh -# prefix_title \033[1;37m src/lib/colours.sh -# prefix_warning '- ' src/lib/colours.sh -# reply_to_prompt Null (not set) src/bin/adsorber -# tmp_dir_path /tmp/adsorber src/bin/adsorber +# ---variable:---------- ---default value:-- ---defined in:------------------ +# backedup Null (not set) src/lib/setup.sh +# hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf +# hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf +# setup_scheduler Null (not set) src/lib/systemd/systemd.sh, src/lib/cron/cron.sh +# prefix ' ' (two spaces) src/lib/colours.sh +# prefix_fatal '\033[0;91mE ' src/lib/colours.sh +# prefix_input ' ' (two spaces) src/lib/colours.sh +# prefix_reset \033[0m src/lib/colours.sh +# prefix_title \033[1;37m src/lib/colours.sh +# prefix_warning '- ' src/lib/colours.sh +# reply_to_prompt Null (not set) src/bin/adsorber +# tmp_dir_path /tmp/adsorber src/bin/adsorber # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: @@ -38,8 +38,8 @@ remove_ErrorCleanUp() { printf "%bCleaning up ...\\n" "${prefix_warning}" - # Remove scheduler if it was installed in the same run - case "${installed_scheduler}" in + # Remove scheduler if it was setup (created) in the same run + case "${setup_scheduler}" in cronjob ) crontabRemove ;; @@ -50,7 +50,7 @@ remove_ErrorCleanUp() # Remove backup if backed-up in the same run if [ "${backedup}" = "true" ]; then - echo "${prefix}Removed backup as the installation failed." + echo "${prefix}Removed backup as the setup failed." rm "${hosts_file_backup_path}" fi diff --git a/src/lib/install.sh b/src/lib/setup.sh similarity index 77% rename from src/lib/install.sh rename to src/lib/setup.sh index a10ff4d..7b41c57 100755 --- a/src/lib/install.sh +++ b/src/lib/setup.sh @@ -1,8 +1,5 @@ #!/bin/sh -# TODO: Maybe rename install to init (initialize) because there's already an -# installation into the system (install-to-system.sh) however with a different goal - # Author: stablestud # Repository: https://github.com/stablestud/adsorber # License: MIT, https://opensource.org/licenses/MIT @@ -24,13 +21,13 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: # ---function:----- ---function defined in:--- -# crontabInstall src/lib/cron/cron.sh +# crontabSetup src/lib/cron/cron.sh # remove_ErrorCleanUp src/lib/remove.sh -# systemdInstall src/lib/systemd/systemd.sh +# systemdSetup src/lib/systemd/systemd.sh # shellcheck disable=SC2154 -install_BackupHostsFile() +setup_BackupHostsFile() { # Create a backup, to be able to restore to it later if neccessary if [ ! -f "${hosts_file_backup_path}" ]; then @@ -45,11 +42,11 @@ install_BackupHostsFile() } -install_Prompt() +setup_Prompt() { - # Ask the user if he/she is sure about to install Adsorber + # Ask the user if he/she is sure about to setup Adsorber if [ -z "${reply_to_prompt}" ]; then - printf "%bDo you really want to install Adsorber? [Y/n]: %b" "${prefix_input}" "${prefix_reset}" + printf "%bDo you really want to setup Adsorber? [Y/n]: %b" "${prefix_input}" "${prefix_reset}" read -r reply_to_prompt fi @@ -58,7 +55,7 @@ install_Prompt() return 0 ;; * ) - printf "%bInstallation cancelled.\\n" "${prefix_warning}" 1>&2 + printf "%bSetup cancelled.\\n" "${prefix_warning}" 1>&2 remove_ErrorCleanUp exit 130 ;; @@ -68,7 +65,7 @@ install_Prompt() } -install_PromptScheduler() +setup_PromptScheduler() { # The user enters interactively what scheduler (Systemd, Cron or none) # should be used to update the hosts file periodically @@ -79,10 +76,10 @@ install_PromptScheduler() case "${reply_to_scheduler_prompt}" in [Ss] | [Ss]ystemd | [Ss][Yy][Ss] ) - systemdInstall + systemdSetup ;; [Cc] | [Cc]ron | [Cc]ron[Jj]ob | [Cc]ron[Tt]ab ) - crontabInstall + crontabSetup ;; * ) echo "${prefix}Skipping scheduler creation ..." @@ -93,13 +90,13 @@ install_PromptScheduler() } -# Main function when calling installation related tasks -install() +# Main function when calling setup related tasks +setup() { - printf "%bInstalling Adsorber ...%b\\n" "${prefix_title}" "${prefix_reset}" - install_Prompt - install_BackupHostsFile - install_PromptScheduler + printf "%bSetting up Adsorber ...%b\\n" "${prefix_title}" "${prefix_reset}" + setup_Prompt + setup_BackupHostsFile + setup_PromptScheduler return 0 } diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index 8846fc7..e8f7c0a 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -27,7 +27,7 @@ # shellcheck disable=SC2154 -systemdInstall() +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 @@ -37,13 +37,13 @@ systemdInstall() exit 126 fi - # Remove systemd service if already installed + # Remove systemd service if already present if [ -f "${systemd_dir_path}/adsorber.service" ] || [ -f "${systemd_dir_path}/adsorber.timer" ]; then - echo "${prefix}Removing previously installed systemd services ..." + echo "${prefix}Removing previous systemd services ..." systemdRemove fi - echo "${prefix}Installing systemd service ..." + echo "${prefix}Setting up systemd service ..." # Replace the @ place holder line with the location of adsorber and copy # the service to the systemd directory ( /etc/sytemd/system/adsorber.service ) @@ -68,9 +68,9 @@ systemdInstall() exit 126 fi - # Make known that we have installed the systemd service in this run, + # Make known that we have setup the systemd service in this run, # if we fail now, systemd will be also removed (see remove_ErrorCleanUp) - readonly installed_scheduler="systemd" + readonly setup_scheduler="systemd" echo "${prefix}Initialized Systemd service ..." diff --git a/src/lib/update.sh b/src/lib/update.sh index a10dd81..ea1bee3 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -11,7 +11,7 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:---------- ---default value:------------ ---declared in:---- +# ---variable:-------------- ---default value:------------ ---declared in:---- # config_dir_path ${executable_dir_path}/../../ src/bin/adsorber # hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf # hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf @@ -28,7 +28,7 @@ # prefix_title \033[1;37m src/lib/colours.sh # prefix_warning '- ' src/lib/colours.sh # primary_list blacklist src/lib/config.sh, adsorber.conf -# reply_to_force_prompt Null (not set) src/lib/install.sh, src/bin/adsorber +# reply_to_force_prompt Null (not set) src/lib/setup.sh, src/bin/adsorber # tmp_dir_path /tmp/adsorber src/bin/adsorber # use_partial_matching true src/lib/config.sh, adsorber.conf # version 0.2.2 or similar src/bin/adsorber @@ -50,7 +50,7 @@ update_CheckBackupExist() # 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 install'.%b\\n" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 + 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}" read -r reply_to_force_prompt fi @@ -94,7 +94,7 @@ update_ReadSourceList() if [ ! -s "${config_dir_path}/sources.list" ]; then if [ ! -s "${config_dir_path}/blacklist" ]; then - printf "%bMissing 'sources.list' and blacklist. To fix run '%s install'.%b\\n" "${prefix_fatal}" "${0}" "${prefix_reset}" 1>&2 + printf "%bMissing 'sources.list' and blacklist. To fix run 'adsorber setup'.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 exit 127 fi From 297d56d3517e6400106c9c87284fb54dfe84749c Mon Sep 17 00:00:00 2001 From: stablestud Date: Fri, 1 Jun 2018 15:17:44 +0200 Subject: [PATCH 02/43] Add notice for config files location to help screen --- src/bin/adsorber | 2 ++ src/lib/cron/cron.sh | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bin/adsorber b/src/bin/adsorber index 81d8983..4a65c58 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -109,6 +109,8 @@ showHelp() echo " has been created (dangerous)" echo " -h, --help - show specific help of specified operations" echo + echo "Config files are located at: $(cd ${config_dir_path} && pwd)/" + echo echo "Documentation: https://github.com/stablestud/adsorber" echo "If you encounter any issues please report them to the Github repository." diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index c7aba5e..43863cd 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -68,7 +68,7 @@ crontabRemove() echo "${prefix}Removed Adsorber's cronjob." else - echo "${prefix}Cronjob not setup. Skipping ..." + echo "${prefix}Cronjob not installed. Skipping ..." fi return 0 From 679b6164e7683de688fdde1737533d19966ea1ce Mon Sep 17 00:00:00 2001 From: stablestud Date: Fri, 1 Jun 2018 17:36:37 +0200 Subject: [PATCH 03/43] Added requirements info to README.md --- README.md | 10 +++++++++- src/bin/adsorber | 2 +- src/lib/systemd/systemd.sh | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a936054..2dc23da 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,14 @@ Currently we are using the following hosts lists: To add your own hosts sources, just add them to the `sources.list` file. +## Requirements +* `*nix` like distribution (`Ubuntu, Debian, Mint, Gentoo, Archlinux, Fedora, etc.`) +* POSIX-compliant shell (`bash, dash, fish, ksh, zsh, sh, etc.`) +* Support for `hosts` file +* `root` rights (e.g. with `sudo`) +* `curl` or `wget` +Note: If you've any of the above listed distributions you don't need to worry about the requirements, as they are fulfilled by default . + ## Usage ### First steps .. installation to system or portable mode @@ -33,7 +41,7 @@ If you don't want Adsorber to be installed into your system run `portable_adsorb Installing Adsorber into the system has the advantage that Adsorber can run independently from the user who downloaded it. Also it prevents broken cronjobs/services as there is no risk that the files/directory of Adsorber will be accidentally deleted or moved. -To configure where it should be installed (to fit your system), edit the relevant lines in `install_to_system.sh` and `remove_from_system.sh`. For more help about the specific file run it with `--help`. +To configure where it should be installed (to fit your system), edit the relevant lines in `install_to_system.sh` and `remove_from_system.sh`. For more help about the specific files run them with `--help`. ### Default help screen of `adsorber help` ``` diff --git a/src/bin/adsorber b/src/bin/adsorber index 4a65c58..b65cc48 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -132,7 +132,7 @@ showSpecificHelp() echo " (if not other specified in adsorber.conf)" echo " - setup a scheduler which updates your hosts file with ad-domains" echo " once a week. (either systemd, cronjob or none)" - echo " - fetch the newest ad-domains in your hosts file." + echo " - fetch the newest ad-domains to your hosts file." echo " (same as 'adsorber update')" echo echo "Note: this is not the same as the install_to_system.sh script." diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index e8f7c0a..3f4a7b6 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -62,7 +62,7 @@ systemdSetup() 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." + echo "${prefix}Is Systemd installed? If not use Cron instead." systemdRemove remove_ErrorCleanUp exit 126 From 63a41d7e3fd0317387f7ba49506f681b650c7e23 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sat, 2 Jun 2018 00:34:14 +0200 Subject: [PATCH 04/43] Added prompt for scheduler frequency --- README.md | 7 ++++--- TODO.md | 1 + src/lib/config.sh | 2 +- src/lib/cron/cron.sh | 40 +++++++++++++++++++++++++++++++++++++- src/lib/remove.sh | 2 +- src/lib/revert.sh | 14 ++++++------- src/lib/setup.sh | 4 +++- src/lib/systemd/systemd.sh | 39 ++++++++++++++++++++++++++++++++++++- 8 files changed, 94 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2dc23da..3f6daf7 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,13 @@ Currently we are using the following hosts lists: To add your own hosts sources, just add them to the `sources.list` file. ## Requirements -* `*nix` like distribution (`Ubuntu, Debian, Mint, Gentoo, Archlinux, Fedora, etc.`) +* `root` rights (e.g. with `sudo`) + +If you have a `*nix` like distribution (`Ubuntu, Debian, Mint, Gentoo, Archlinux, Fedora, etc.`) +the following requirements should have been fulfilled by default: * POSIX-compliant shell (`bash, dash, fish, ksh, zsh, sh, etc.`) * Support for `hosts` file -* `root` rights (e.g. with `sudo`) * `curl` or `wget` -Note: If you've any of the above listed distributions you don't need to worry about the requirements, as they are fulfilled by default . ## Usage diff --git a/TODO.md b/TODO.md index c8d27cd..ecb3b97 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,7 @@ ## Must: * README.md update +* read scheduler frequency by option ## Should: * interactive ability to set the update frequency diff --git a/src/lib/config.sh b/src/lib/config.sh index 526383e..6da7cf5 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -11,7 +11,7 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:-------- ---default value:------------ ----declared in:----- +# ---variable:-------- ---default value:--------------- ----declared in:----- # config_dir_path ${executable_dir_path}/../../ src/bin/adsorber # debug false src/bin/adsorber # options every parameter but first src/bin/adsorber diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index 43863cd..c113c52 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -11,11 +11,12 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:------- ---default value:-- ---declared in:--------- +# ---variable:------- ---default value:------------ ---declared in:--------- # crontab_dir_path /etc/cron.weekly src/lib/config.sh, adsorber.conf # executable_dir_path the root dir of the script src/bin/adsorber # library_dir_path ${executable_dir_path}/../lib src/bin/adsorber # prefix ' ' (two spaces) src/lib/colours.sh +# prefix_input ' ' (two spaces) src/lib/colours.sh # prefix_fatal '\033[0;91mE ' src/lib/colours.sh # prefix_reset \033[0m src/lib/colours.sh # prefix_warning '- ' src/lib/colours.sh @@ -56,6 +57,43 @@ crontabSetup() } +crontabPromptFrequency() +{ + printf "%bHow often should the crontab be triggered? " "${prefix_input}" + read -r frequency + + case "${frequency}" in + "" ) + echo "default" + ;; + [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][YY] ) + echo "hourly" + ;; + [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) + echo "daily" + ;; + [Ww] | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) + echo "weekly" + ;; + [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) + echo "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] ) + echo "yearly - only available with systemd" + ;; + [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] | ) + echo "quarterly - only available wih systemd" + ;; + [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] | ) + echo "semiannually - only available with systemd" + ;; + * ) + echo "abort" + ;; + esac +} + + crontabRemove() { if [ -f "${crontab_dir_path}/80adsorber" ]; then diff --git a/src/lib/remove.sh b/src/lib/remove.sh index 11d54e5..d5df4da 100755 --- a/src/lib/remove.sh +++ b/src/lib/remove.sh @@ -11,7 +11,7 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:---------- ---default value:-- ---defined in:------------------ +# ---variable:---------- ---default value:-- ---defined in:------------------- # backedup Null (not set) src/lib/setup.sh # hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf # hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf diff --git a/src/lib/revert.sh b/src/lib/revert.sh index 1e8fdab..c5ef224 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -11,13 +11,13 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:---------- ---default value:-- ---declared in:---------------- -# hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf -# hosts_file_previous_path /etc/hosts.previous src/lib/config.sh, adsorber.conf -# prefix ' ' (two spaces) src/lib/colours.sh -# prefix_fatal '\033[0;91mE ' src/lib/colours.sh -# prefix_reset \033[0m src/lib/colours.sh -# prefix_title \033[1;37m src/lib/colours.sh +# ---variable:------------ ---default value:-- ---declared in:---------------- +# hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf +# hosts_file_previous_path /etc/hosts.previous src/lib/config.sh, adsorber.conf +# prefix ' ' (two spaces) src/lib/colours.sh +# prefix_fatal '\033[0;91mE ' src/lib/colours.sh +# prefix_reset \033[0m src/lib/colours.sh +# prefix_title \033[1;37m src/lib/colours.sh # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: diff --git a/src/lib/setup.sh b/src/lib/setup.sh index 7b41c57..c933f9b 100755 --- a/src/lib/setup.sh +++ b/src/lib/setup.sh @@ -76,9 +76,11 @@ setup_PromptScheduler() case "${reply_to_scheduler_prompt}" in [Ss] | [Ss]ystemd | [Ss][Yy][Ss] ) + systemdPromptFrequency systemdSetup ;; - [Cc] | [Cc]ron | [Cc]ron[Jj]ob | [Cc]ron[Tt]ab ) + [Cc] | [Cc][Rr][Oo][Nn] | [Cc]ron[Jj]ob | [Cc]ron[Tt]ab ) + crontabPromptFrequency crontabSetup ;; * ) diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index 3f4a7b6..bf856e0 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -11,10 +11,11 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:------- ---default value:-- ---declared in:--------- +# ---variable:------- ---default value:------------ ---declared in:--------- # executable_dir_path the root dir of the script src/bin/adsorber # library_dir_path ${executable_dir_path}/../lib src/bin/adsorber # prefix ' ' (two spaces) src/lib/colours.sh +# prefix_input ' ' (two spaces) src/lib/colours.sh # prefix_fatal '\033[0;91mE ' src/lib/colours.sh # prefix_reset \033[0m src/lib/colours.sh # prefix_warning '- ' src/lib/colours.sh @@ -77,6 +78,42 @@ systemdSetup() return 0 } +systemdPromptFrequency() +{ + printf "%bHow often should the service run? " "${prefix_input}" + read -r frequency + + case "${frequency}" in + "" ) + echo "default" + ;; + [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][YY] ) + echo "hourly" + ;; + [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) + echo "daily" + ;; + [Ww] | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) + echo "weekly" + ;; + [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) + echo "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] ) + echo "yearly" + ;; + [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] | ) + echo "quarterly" + ;; + [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] | ) + echo "semiannually" + ;; + * ) + echo "abort" + ;; + esac +} + systemdRemove() { From 655ad8c9be006bf51c17eeea930a7b653056157e Mon Sep 17 00:00:00 2001 From: stablestud Date: Sun, 3 Jun 2018 00:49:06 +0200 Subject: [PATCH 05/43] Added interactive ability to set scheduler frequency --- TODO.md | 3 +- src/bin/adsorber | 134 +++++++++++++++++++----- src/lib/config.sh | 105 +++++++++++-------- src/lib/cron/cron.sh | 89 +++++++++------- src/lib/remove.sh | 6 +- src/lib/restore.sh | 2 +- src/lib/revert.sh | 2 +- src/lib/setup.sh | 20 ++-- src/lib/systemd/adsorber.service | 2 +- src/lib/systemd/adsorber.timer | 4 +- src/lib/systemd/systemd.sh | 121 +++++++++++---------- src/lib/update.sh | 2 +- src/share/default/default-adsorber.conf | 23 ---- 13 files changed, 302 insertions(+), 211 deletions(-) diff --git a/TODO.md b/TODO.md index ecb3b97..8747b59 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,8 @@ ## Must: * README.md update -* read scheduler frequency by option +* Remove all crontabs from cron.x directories +* Update comments about weekly frequency, in `hosts_header` ## Should: * interactive ability to set the update frequency diff --git a/src/bin/adsorber b/src/bin/adsorber index b65cc48..ba6e2b0 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -6,7 +6,7 @@ # Variable naming: # under_score - used for global variables which are accessible between functions. -# _extra_under_score - used for local function variables. Should be unset afterwards. +# _extra_under_score - used for local function/file variables. Should be unset afterwards. # (Note the underscore in the beginning of _extra_under_score!) # shellcheck disable=SC2154 @@ -26,7 +26,6 @@ readonly config_dir_path="${executable_dir_path}/../../" readonly version="0.5.0" -readonly command="${0}" readonly operation="${1}" if [ "${#}" -ne 0 ]; then @@ -49,14 +48,14 @@ checkRoot() checkForWrongParameters() { - if [ -n "${_wrong_operation}" ] || [ -n "${_wrong_option}" ]; then - showUsage - fi - if [ "${_option_help}" = "true" ]; then showSpecificHelp fi + if [ -n "${_wrong_operation}" ] || [ -n "${_wrong_option}" ]; then + showUsage + fi + return 0 } @@ -86,7 +85,7 @@ showHelp() echo " (with the help of the hosts file)" echo "" echo "Operations (required):" - echo " setup - setup necessary things needed for Adsorber" + echo " setup - setup necessary things needed for Adsorber" echo " e.g., create backup file of hosts file," echo " create scheduler which updates the host file once a week" echo " However this should've been done automatically." @@ -101,13 +100,21 @@ showHelp() echo " help - show this help" echo "" echo "Options (optional):" - echo " -s, --systemd - use Systemd ..." - echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" - echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" 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 "Scheduler specifc options (use with 'setup'):" + echo " -ns, --no-scheduler - skip scheduler creation" + echo " -s, --systemd - use Systemd ..." + echo " -c, --cron - use Cronjob as scheduler" + 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 @@ -131,7 +138,7 @@ showSpecificHelp() echo " - backup your /etc/hosts file to /etc/hosts.original" echo " (if not other specified in adsorber.conf)" echo " - setup a scheduler which updates your hosts file with ad-domains" - echo " once a week. (either systemd, cronjob or none)" + echo " (either systemd, cronjob or none)" echo " - fetch the newest ad-domains to your hosts file." echo " (same as 'adsorber update')" echo @@ -141,11 +148,20 @@ showSpecificHelp() echo "described here. The same goes for 'remove'" echo echo "Possible options:" + echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" echo " -s, --systemd - use Systemd ..." - echo " -c, --cronjob - use Cronjob as scheduler" + echo " -c, --cron - use Cronjob as scheduler" echo " -ns, --no-scheduler - skip scheduler creation" - echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" 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 + echo "Note: Cron supports only hourly, daily, weekly and monthly." ;; update ) printf "\\033[4;37madsorber update []\\033[0m:\\n" @@ -206,6 +222,9 @@ showSpecificHelp() echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" echo " -h, --help - show this help screen" ;; + * ) + showHelp + ;; esac exit 0 @@ -231,13 +250,23 @@ showVersion() duplicateOption() { if [ "${1}" = "scheduler" ]; then - echo "Adsorber: Duplicate option for scheduler: '${_option}'" 1>&2 + echo "Adsorber: Duplicate option for scheduler: '${2}'" 1>&2 echo "You may only select one:" - echo " -s, --systemd - use Systemd ..." - echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" - echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" - else - echo "Adsorber: Duplicate option: '${_option}'" 1>&2 + echo " -s, --systemd - use Systemd ..." + echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" + echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" + elif [ "${1}" = "frequency" ]; then + echo "Adsorber: Duplicate option for schedulers frequency: '${2}'" 1>&2 + echo "You may only select 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 @@ -309,28 +338,28 @@ sourceFiles for _option in "${@}"; do case "${_option}" in - -[Ss] | --systemd ) + -s | --systemd ) if [ -z "${reply_to_scheduler_prompt}" ]; then readonly reply_to_scheduler_prompt="systemd" else - duplicateOption "scheduler" + duplicateOption "scheduler" "${_option}" fi ;; - -[Cc] | --cron ) + -[Cc] | --cron | --crontab | --cronjob ) if [ -z "${reply_to_scheduler_prompt}" ]; then readonly reply_to_scheduler_prompt="cronjob" else - duplicateOption "scheduler" + duplicateOption "scheduler" "${_option}" fi ;; -[Nn][Ss] | --no-scheduler ) if [ -z "${reply_to_scheduler_prompt}" ]; then readonly reply_to_scheduler_prompt="no-scheduler" else - duplicateOption "scheduler" + duplicateOption "scheduler" "${_option}" fi ;; - -[Yy] | --[Yy][Ee][Ss] | --assume-yes ) + -y | --[Yy][Ee][Ss] | --assume-yes ) if [ -z "${reply_to_prompt}" ]; then readonly reply_to_prompt="yes" else @@ -344,10 +373,57 @@ for _option in "${@}"; do duplicateOption fi ;; - "" ) - : # Do nothing - ;; - -[Hh] | --help | help ) + -H | --hourly ) + if [ -z "${frequency}" ]; then + readonly frequency="hourly" + else + duplicateOption "frequency" "${_option}" + fi + + ;; + -D | --daily ) + if [ -z "${frequency}" ]; then + readonly frequency="daily" + else + duplicateOption "frequency" "${_option}" + fi + ;; + -W | --weekly ) + if [ -z "${frequency}" ]; then + readonly frequency="weekly" + else + duplicateOption "frequency" "${_option}" + fi + ;; + -M | --monthly ) + if [ -z "${frequency}" ]; then + readonly frequency="monthly" + else + duplicateOption "frequency" "${_option}" + fi + ;; + -Q | --quarterly ) + if [ -z "${frequency}" ]; then + readonly frequency="quarterly" + else + duplicateOption "frequency" "${_option}" + fi + ;; + -S | --semi | --semiannually ) + if [ -z "${frequency}" ]; then + readonly frequency="semiannually" + else + duplicateOption "frequency" "${_option}" + fi + ;; + -Y | --yearly | --annually ) + if [ -z "${frequency}" ]; then + readonly frequency="yearly" + else + duplicateOption "frequency" "${_option}" + fi + ;; + -h | --help | help ) _option_help="true" ;; * ) diff --git a/src/lib/config.sh b/src/lib/config.sh index 6da7cf5..45c00ea 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -11,22 +11,22 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:-------- ---default value:--------------- ----declared in:----- -# config_dir_path ${executable_dir_path}/../../ src/bin/adsorber -# debug false src/bin/adsorber -# options every parameter but first src/bin/adsorber -# operations the first parameter src/bin/adsorber -# prefix ' ' (two spaces) src/lib/colours.sh -# prefix_reset \033[0m src/lib/colours.sh -# prefix_title \033[1;37m src/lib/colours.sh -# prefix_warning '- ' src/lib/colours.sh -# shareable_dir_path ${executable_dir_path}/../share/ src/bin/adsorber -# tmp_dir_path /tmp/adsorber src/bin/adsorber -# version 0.2.2 or similar src/bin/adsorber +# ---variable:------ ---default value:--------------- ---declared in:------- +# config_dir_path ${executable_dir_path}/../../ src/bin/adsorber +# debug false src/bin/adsorber +# options every parameter but first src/bin/adsorber +# operations the first parameter src/bin/adsorber +# prefix ' ' (two spaces) src/lib/colours.sh +# prefix_reset \033[0m src/lib/colours.sh +# prefix_title \033[1;37m src/lib/colours.sh +# prefix_warning '- ' src/lib/colours.sh +# shareable_dir_path ${executable_dir_path}/../share/ src/bin/adsorber +# tmp_dir_path /tmp/adsorber src/bin/adsorber +# version 0.2.2 or similar src/bin/adsorber # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- +# ---function:------- ---function defined in:--- # remove_ErrorCleanUp src/lib/remove.sh # shellcheck disable=SC2154 @@ -117,7 +117,8 @@ config_CopyConfig() printf "%bNo config file found. Creating default.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 echo "${prefix_warning}Please re-run the command to continue." - sed "s|@.*|# Config file for Adsorber v${version}|g" "${shareable_dir_path}/default/default-adsorber.conf" > "${config_dir_path}/adsorber.conf" + sed "s|@.*|# Config file for Adsorber v${version}|g" "${shareable_dir_path}/default/default-adsorber.conf" \ + > "${config_dir_path}/adsorber.conf" chown root:root -R "${config_dir_path}/adsorber.conf" \ || { @@ -155,7 +156,6 @@ config_FilterConfig() 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 "/^crontab_dir_path=/p" "${tmp_dir_path}/config" sed -n "/^systemd_dir_path=/p" "${tmp_dir_path}/config" } > "${tmp_dir_path}/config-filtered" @@ -235,13 +235,6 @@ config_ReadConfig() echo "${prefix_warning}Duplicate configuration for 'hosts_file_previous_path', keeping the first value: ${hosts_file_previous_path}" fi ;; - crontab_dir_path=* ) - if [ -z "${crontab_dir_path}" ]; then - readonly "${_line}" - else - echo "${prefix_warning}Duplicate configuration for 'crontab_dir_path', keeping the first value: ${crontab_dir_path}" - fi - ;; systemd_dir_path=* ) if [ -z "${systemd_dir_path}" ]; then readonly "${_line}" @@ -253,7 +246,9 @@ config_ReadConfig() # This should never be reached, as the config # 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}" + 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}" + echo "Please report this error with your config file to https://github.com/stablestud/adsorber" remove_ErrorCleanUp exit 1 @@ -272,30 +267,41 @@ config_IsVariableSet() # These configurations are not mandatory needed by Adsorber, thus # if any of them were not defined, Adsorber will use the default value if [ -z "${primary_list}" ]; then - printf "%bprimary_list not set in adsorber.conf. Using default value: blacklist\\n" "${prefix_warning}" 1>&2 + printf "%bprimary_list not set in adsorber.conf. Using default value: blacklist\\n" \ + "${prefix_warning}" 1>&2 + readonly primary_list="blacklist" fi if [ -z "${use_partial_matching}" ]; then - printf "%buse_partial_matching not set in adsorber.conf. Using default value: true\\n" "${prefix_warning}" 1>&2 + printf "%buse_partial_matching not set in adsorber.conf. Using default value: true\\n" \ + "${prefix_warning}" 1>&2 + readonly use_partial_matching="true" fi if [ -z "${ignore_download_error}" ]; then - printf "%bignore_download_error not set in adsorber.conf. Using default value: false\\n" "${prefix_warning}" 1>&2 + printf "%bignore_download_error not set in adsorber.conf. Using default value: false\\n" \ + "${prefix_warning}" 1>&2 + readonly ignore_download_error="true" fi if [ -z "${hosts_file_previous_enable}" ]; then - printf "%bhosts_file_previous_enable not set in adsorber.conf. Using default value: true\\n" "${prefix_warning}" 1>&2 + printf "%bhosts_file_previous_enable not set in adsorber.conf. Using default value: true\\n" \ + "${prefix_warning}" 1>&2 + readonly hosts_file_previous_enable="true" 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 "${crontab_dir_path}" ] || [ -z "${systemd_dir_path}" ] || [ -z "${hosts_file_previous_path}" ]; then + if [ -z "${hosts_file_path}" ] || [ -z "${hosts_file_backup_path}" ] \ + || [ -z "${systemd_dir_path}" ] || [ -z "${hosts_file_previous_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 + 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 + remove_ErrorCleanUp exit 127 fi @@ -311,42 +317,58 @@ config_IsVariableValid() # and abort with the error clean-up function if [ "${primary_list}" != "blacklist" ] && [ "${primary_list}" != "whitelist" ]; then - printf "%bWrong 'primary_list' set in adsorber.conf. Choose either 'blacklist' or 'whitelist'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bWrong 'primary_list' set in adsorber.conf. Choose either 'blacklist' or 'whitelist'%b\\n" \ + "${prefix_fatal}" "${prefix_reset}" 1>&2 + wrongVariable="true" fi if [ "${use_partial_matching}" != "true" ] && [ "${use_partial_matching}" != "false" ]; then - printf "%bWrong 'use_partial_matching' set in adsorber.conf. Possible option: 'true' or 'false'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bWrong 'use_partial_matching' set in adsorber.conf. Possible option: 'true' or 'false'%b\\n" \ + "${prefix_fatal}" "${prefix_reset}" 1>&2 + wrongVariable="true" fi if [ "${ignore_download_error}" != "true" ] && [ "${ignore_download_error}" != "false" ]; then - printf "%bWrong 'ignore_download_error' set in adsorber.conf. Possible option: 'true' or 'false'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bWrong 'ignore_download_error' set in adsorber.conf. Possible option: 'true' or 'false'%b\\n" \ + "${prefix_fatal}" "${prefix_reset}" 1>&2 + wrongVariable="true" fi if [ ! -f "${hosts_file_path}" ]; then - printf "%bWrong 'hosts_file_path' set in adsorber.conf. Can't access: %s%b\\n" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 + printf "%bWrong 'hosts_file_path' set in adsorber.conf. Can't access: %s%b\\n" \ + "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" 1>&2 + wrongVariable="true" fi if [ ! -d "$(dirname "${hosts_file_backup_path}")" ]; then - printf "%bWrong '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 + printf "%bWrong '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 + wrongVariable="true" fi if [ "${hosts_file_previous_enable}" != "true" ] && [ "${hosts_file_previous_enable}" != "false" ]; then - printf "%bWrong 'hosts_file_previous_enable' set in adsorber.conf. Possible options: 'true' or 'false'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bWrong 'hosts_file_previous_enable' set in adsorber.conf. Possible options: 'true' or 'false'%b\\n" \ + "${prefix_fatal}" "${prefix_reset}" 1>&2 + wrongVariable="true" fi if [ ! -d "$(dirname "${hosts_file_previous_path}")" ]; then - printf "%bWrong '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 + printf "%bWrong '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 + wrongVariable="true" fi if [ ! -d "$(dirname "${tmp_dir_path}")" ]; then - printf "%bWrong 'tmp_dir_path' set in adsorber.conf. Can't access: %s%b\\n" "${prefix_fatal}" "$(dirname "${tmp_dir_path}")" "${prefix_reset}" 1>&2 + printf "%bWrong 'tmp_dir_path' set in adsorber.conf. Can't access: %s%b\\n" \ + "${prefix_fatal}" "$(dirname "${tmp_dir_path}")" "${prefix_reset}" 1>&2 + wrongVariable="true" fi @@ -357,8 +379,6 @@ config_IsVariableValid() remove_ErrorCleanUp exit 126 fi - - return 0 } @@ -375,22 +395,19 @@ config_PrintVariables() 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 " - crontab_dir_path: ${crontab_dir_path}" echo " - systemd_dir_path: ${systemd_dir_path}" echo " - tmp_dir_path: ${tmp_dir_path}" echo " - executable_dir_path: ${executable_dir_path}" echo " - library_dir_path: ${library_dir_path}" echo " - shareable_dir_path: ${shareable_dir_path}" echo " - config_dir_path: ${config_dir_path}" - - return 0 } # Main function for calling config related tasks config() { - printf "%b" "${prefix_title}Reading configuration ... ${prefix_reset}\\n" + printf "%bReading configuration ...%b\\n" "${prefix_title}" "${prefix_reset}" config_CreateTmpDir config_CopySourceList config_CopyWhiteList @@ -404,6 +421,4 @@ config() if [ "${debug}" = "true" ]; then config_PrintVariables # used for debugging fi - - return 0 } diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index c113c52..f5cc09c 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -12,8 +12,8 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: # ---variable:------- ---default value:------------ ---declared in:--------- -# crontab_dir_path /etc/cron.weekly src/lib/config.sh, adsorber.conf # executable_dir_path the root dir of the script src/bin/adsorber +# frequency none (null) src/bin/adsorber # library_dir_path ${executable_dir_path}/../lib src/bin/adsorber # prefix ' ' (two spaces) src/lib/colours.sh # prefix_input ' ' (two spaces) src/lib/colours.sh @@ -23,7 +23,7 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- +# ---function:------- ---function defined in:--- # remove_ErrorCleanUp src/lib/remove.sh # shellcheck disable=SC2154 @@ -34,13 +34,15 @@ 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 crontab_dir_path set. Can't access: %s.%b\\n" "${prefix_fatal}" "${crontab_dir_path}" "${prefix_reset}" 1>&2 + printf "%bWrong frequency set. Can't access: %s.%b\\n" \ + "${prefix_fatal}" "${crontab_dir_path}" "${prefix_reset}" 1>&2 + echo "${prefix}Is a cron service installed? If not use systemd if possible." remove_ErrorCleanUp exit 126 fi - # Replace the @ place holder line with the location of adsorber + # Replace the @ place holder line with the location of adsorber in 80adsorber # and copy and manipulate the content to crontabs directory sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/80adsorber" \ | sed "s|^#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" \ @@ -52,45 +54,45 @@ crontabSetup() # Make known that we have setup the crontab in this run, # if we fail now, crontab will be also removed (see remove_ErrorCleanUp) readonly setup_scheduler="cronjob" - - return 0 } crontabPromptFrequency() { - printf "%bHow often should the crontab be triggered? " "${prefix_input}" - read -r frequency - - case "${frequency}" in - "" ) - echo "default" - ;; - [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][YY] ) - echo "hourly" - ;; - [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) - echo "daily" - ;; - [Ww] | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) - echo "weekly" - ;; - [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) - echo "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] ) - echo "yearly - only available with systemd" - ;; - [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] | ) - echo "quarterly - only available wih systemd" - ;; - [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] | ) - echo "semiannually - only available with systemd" - ;; - * ) - echo "abort" - ;; - esac + if [ -z "${frequency}" ]; then + printf "%bHow often should the service 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 crontab_dir_path="/etc/cron.hourly/" + ;; + [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) + readonly crontab_dir_path="/etc/cron.daily/" + ;; + [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) + readonly crontab_dir_path="/etc/cron.weekly/" + ;; + [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) + 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] ) + echo "${prefix_warning}Sorry, this frequency is only available with Systemd." + echo "Exiting ..." + remove_ErrorCleanUp + exit 1 + ;; + * ) + echo "${prefix_warning}Frequency '${frequency}' not understood." + echo "Aborting ..." + remove_ErrorCleanUp + exit 1 + ;; + esac } @@ -100,7 +102,9 @@ crontabRemove() # Remove the crontab from /etc/cron.weekly rm "${crontab_dir_path}/80adsorber" \ || { - printf "%bCouldn't remove crontab %s\\n." "${prefix_warning}" "${crontab_dir_path}" 1>&2 + printf "%bCouldn't remove crontab %s\\n." \ + "${prefix_warning}" "${crontab_dir_path}" 1>&2 + return 1 } @@ -108,6 +112,11 @@ crontabRemove() else echo "${prefix}Cronjob not installed. Skipping ..." fi +} + - return 0 +crontab() +{ + crontabPromptFrequency + crontabSetup } diff --git a/src/lib/remove.sh b/src/lib/remove.sh index d5df4da..edbbb49 100755 --- a/src/lib/remove.sh +++ b/src/lib/remove.sh @@ -27,9 +27,9 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- -# systemdRemove src/lib/systemd/systemd.sh -# crontabRemove src/lib/cron/cron.sh +# ---function:- ---function defined in:--- +# systemdRemove src/lib/systemd/systemd.sh +# crontabRemove src/lib/cron/cron.sh # shellcheck disable=SC2154 diff --git a/src/lib/restore.sh b/src/lib/restore.sh index 82dd9ab..36acade 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -21,7 +21,7 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- +# ---function:------- ---function defined in:--- # remove_CleanUp src/lib/remove.sh # remove_ErrorCleanUp src/lib/remove.sh diff --git a/src/lib/revert.sh b/src/lib/revert.sh index c5ef224..46f8def 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -21,7 +21,7 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- +# ---function:------- ---function defined in:--- # remove_CleanUp src/lib/remove.sh # remove_ErrorCleanUp src/lib/remove.sh diff --git a/src/lib/setup.sh b/src/lib/setup.sh index c933f9b..620da47 100755 --- a/src/lib/setup.sh +++ b/src/lib/setup.sh @@ -20,10 +20,10 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- -# crontabSetup src/lib/cron/cron.sh +# ---function:------- ---function defined in:--- +# crontab src/lib/cron/cron.sh # remove_ErrorCleanUp src/lib/remove.sh -# systemdSetup src/lib/systemd/systemd.sh +# systemd src/lib/systemd/systemd.sh # shellcheck disable=SC2154 @@ -51,7 +51,7 @@ setup_Prompt() fi case "${reply_to_prompt}" in - [Yy] | [Yy][Ee][Ss] ) + [Yy] | [Yy][Ee][Ss] | "" ) return 0 ;; * ) @@ -70,18 +70,16 @@ setup_PromptScheduler() # The user enters interactively what scheduler (Systemd, Cron or none) # should be used to update the hosts file periodically if [ -z "${reply_to_scheduler_prompt}" ]; then - printf "%bWhat scheduler should be used to update the host file automatically? [(S)ystemd/(C)ron/(N)one]: %b" "${prefix_input}" "${prefix_reset}" + printf "%bWhat scheduler should be used to update the host file automatically? [(C)ron/(s)ystemd/(n)one]: %b" "${prefix_input}" "${prefix_reset}" read -r reply_to_scheduler_prompt fi case "${reply_to_scheduler_prompt}" in - [Ss] | [Ss]ystemd | [Ss][Yy][Ss] ) - systemdPromptFrequency - systemdSetup + [Cc] | [Cc][Rr][Oo][Nn] | [Cc]ron[Jj]ob | [Cc]ron[Tt]ab | [Cc]ronie | "" ) + crontab ;; - [Cc] | [Cc][Rr][Oo][Nn] | [Cc]ron[Jj]ob | [Cc]ron[Tt]ab ) - crontabPromptFrequency - crontabSetup + [Ss] | [Ss]ystemd | [Ss][Yy][Ss] ) + systemd ;; * ) echo "${prefix}Skipping scheduler creation ..." diff --git a/src/lib/systemd/adsorber.service b/src/lib/systemd/adsorber.service index c9a6aab..7256b47 100755 --- a/src/lib/systemd/adsorber.service +++ b/src/lib/systemd/adsorber.service @@ -6,4 +6,4 @@ After=network.target [Service] Type=oneshot User=root -#@ExecStart=/some/path/adsorber update@# +ExecStart=#@/some/path/adsorber update@# diff --git a/src/lib/systemd/adsorber.timer b/src/lib/systemd/adsorber.timer index bc6f201..eaa9402 100755 --- a/src/lib/systemd/adsorber.timer +++ b/src/lib/systemd/adsorber.timer @@ -4,8 +4,8 @@ Documentation=https://github.com/stablestud/adsorber Requires=network-online.target [Timer] -OnCalendar=weekly -RandomizedDelaySec=3600 +OnCalendar=#@frequency@# +RandomizedDelaySec=600 Persistent=true Unit=adsorber.service diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index bf856e0..247abb8 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -23,7 +23,7 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- +# ---function:------- ---function defined in:--- # remove_ErrorCleanUp src/lib/remove.sh # shellcheck disable=SC2154 @@ -32,7 +32,9 @@ 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 + printf "%bWrong systemd_dir_path set. Can't access: %s.%b\\n" \ + "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 + echo "${prefix}Is Systemd installed? If not use cron instead." remove_ErrorCleanUp exit 126 @@ -48,10 +50,12 @@ 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|^#@ExecStart=\\/some\\/path\\/adsorber update@#$|ExecStart=${executable_dir_path}\\/adsorber update|g" "${library_dir_path}/systemd/adsorber.service" \ - > "${systemd_dir_path}/adsorber.service" + sed "s|#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" \ + "${library_dir_path}/systemd/adsorber.service" > "${systemd_dir_path}/adsorber.service" + # Copy the systemd timer to /etc/systemd/system/adsorber.timer, timer is the clock that triggers adsorber.service - cp "${library_dir_path}/systemd/adsorber.timer" "${systemd_dir_path}/adsorber.timer" + sed "s/#@frequency@#$/${frequency}/g" "${library_dir_path}/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" chown root:root "${systemd_dir_path}/adsorber.service" "${systemd_dir_path}/adsorber.timer" @@ -60,58 +64,62 @@ 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 - remove_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 + remove_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 remove_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 remove_ErrorCleanUp) + readonly setup_scheduler="systemd" echo "${prefix}Initialized Systemd service ..." - - return 0 } + systemdPromptFrequency() { - printf "%bHow often should the service run? " "${prefix_input}" - read -r frequency - - case "${frequency}" in - "" ) - echo "default" - ;; - [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][YY] ) - echo "hourly" - ;; - [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) - echo "daily" - ;; - [Ww] | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) - echo "weekly" - ;; - [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) - echo "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] ) - echo "yearly" - ;; - [Qq] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr] | [Qq][Uu][Aa][Rr][Tt][Ee][Rr][Ll][Yy] | ) - echo "quarterly" - ;; - [Ss] | [Ss][Ee][Mm][Ii] | [Ss][Ee][Mm][Ii][Aa][Nn][Nn][Uu][Aa][Ll][Ll][Yy] | ) - echo "semiannually" - ;; - * ) - echo "abort" - ;; - esac + 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 _input_frequency + + case "${_input_frequency}" 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" + ;; + * ) + printf "%bFrequency '${frequency}' not understood.%b" "${prefix_warning}" "${prefix_reset}" + echo "${prefix_warning}Aborting ..." + remove_ErrorCleanUp + exit 1 + ;; + esac + + unset _input_frequency + fi } @@ -119,7 +127,7 @@ systemdRemove() { if [ -f "${systemd_dir_path}/adsorber.service" ] || [ -f "${systemd_dir_path}/adsorber.timer" ]; then - # Disable timer and add "${prefix}" to the output stream, to format it so it can fit the Adsorber 'style' + # Disable timer and add "${prefix}" to the output stream, to format it so it can fit the Adsorber 'style' systemctl stop adsorber.timer 2>/dev/null systemctl disable adsorber.timer 2>/dev/null \ @@ -130,7 +138,9 @@ systemdRemove() # Remove leftover service files. rm "${systemd_dir_path}/adsorber.timer" "${systemd_dir_path}/adsorber.service" \ || { - printf "%bCouldn't remove systemd service files at %s%b\\n." "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 + printf "%bCouldn't remove systemd service files at %s%b\\n." \ + "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 + return 1 } @@ -141,6 +151,11 @@ systemdRemove() else echo "${prefix}Systemd service not installed. Skipping ..." fi +} - return 0 + +systemd() +{ + systemdPromptFrequency + systemdSetup } diff --git a/src/lib/update.sh b/src/lib/update.sh index ea1bee3..64988f1 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -35,7 +35,7 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:----- ---function defined in:--- +# ---function:------- ---function defined in:--- # remove_CleanUp src/lib/remove.sh # remove_ErrorCleanUp src/lib/remove.sh diff --git a/src/share/default/default-adsorber.conf b/src/share/default/default-adsorber.conf index 21eaae1..35fe672 100755 --- a/src/share/default/default-adsorber.conf +++ b/src/share/default/default-adsorber.conf @@ -101,34 +101,11 @@ hosts_file_previous_enable=true hosts_file_previous_path=/etc/hosts.previous -## crontab_dir_path -# This defines the directory in which we'll set the crontab. -# Used to create a scheduler which supplies the hosts file regulary with -# new ad-domains. -# -# In the default setting it'll update once a week. You can change this behavior -# with setting another directory, for example /etc/cron/daily to update the -# hosts file daily. -# -# Possible value: /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, -# /etc/cron.monthly/, or any other directory which it's contents will be -# executed as a script regulary. -# -# Default value: /etc/cron.weekly/ - -crontab_dir_path=/etc/cron.weekly/ - - ## systemd_dir_path # This defines the directory in which we'll place the systemd files. # Used to create a scheduler which supplies the hosts file regulary with # new ad-domains. Change if your system has an other path. # -# In the default setting it'll update once a week. To change this behavior you -# need to change adsorber.timer's 'OnCalendar' setting to another period. -# The file can be found at systemd_dir_path, to change it before placement go -# into the scripts root directory and edit bin/systemd/adsorber.timer -# # Default value: /etc/systemd/system/ systemd_dir_path=/etc/systemd/system/ From 05acf2e5606b0bad4d348b1beea70aa8e73cf704 Mon Sep 17 00:00:00 2001 From: stablestud Date: Sun, 3 Jun 2018 09:27:40 +0200 Subject: [PATCH 06/43] Fixed variable not quoted --- src/bin/adsorber | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/adsorber b/src/bin/adsorber index ba6e2b0..74bef7e 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -116,7 +116,7 @@ showHelp() echo " -S, --semiannually ... semiannually (2x a year)" echo " -Y, --yearly ... yearly" echo - echo "Config files are located at: $(cd ${config_dir_path} && pwd)/" + echo "Config files are located at: $(cd "${config_dir_path}" && pwd)/" echo echo "Documentation: https://github.com/stablestud/adsorber" echo "If you encounter any issues please report them to the Github repository." From e235bdd7fc9e896f329df22caf8eac494fc67891 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 5 Jun 2018 10:24:33 +0200 Subject: [PATCH 07/43] Finished functions to set the scheduler frequency --- misc/shellcheck.sh | 2 +- src/bin/adsorber | 14 ++++----- src/lib/cron/80adsorber | 2 +- src/lib/cron/cron.sh | 54 +++++++++++++++++++++----------- src/lib/systemd/adsorber.service | 2 +- src/lib/systemd/adsorber.timer | 2 +- src/lib/systemd/systemd.sh | 30 ++++++++++-------- 7 files changed, 64 insertions(+), 42 deletions(-) diff --git a/misc/shellcheck.sh b/misc/shellcheck.sh index 76f7aa9..4aade9a 100755 --- a/misc/shellcheck.sh +++ b/misc/shellcheck.sh @@ -13,7 +13,7 @@ fi echo "Running shellcheck ..." ( - cd -P -e "${script_dir_path}" || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } + cd -P "${script_dir_path}" -e || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } shellcheck -x \ "${script_dir_path}/../src/bin/adsorber" \ diff --git a/src/bin/adsorber b/src/bin/adsorber index 74bef7e..f909ab3 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -375,7 +375,7 @@ for _option in "${@}"; do ;; -H | --hourly ) if [ -z "${frequency}" ]; then - readonly frequency="hourly" + frequency="hourly" else duplicateOption "frequency" "${_option}" fi @@ -383,42 +383,42 @@ for _option in "${@}"; do ;; -D | --daily ) if [ -z "${frequency}" ]; then - readonly frequency="daily" + frequency="daily" else duplicateOption "frequency" "${_option}" fi ;; -W | --weekly ) if [ -z "${frequency}" ]; then - readonly frequency="weekly" + frequency="weekly" else duplicateOption "frequency" "${_option}" fi ;; -M | --monthly ) if [ -z "${frequency}" ]; then - readonly frequency="monthly" + frequency="monthly" else duplicateOption "frequency" "${_option}" fi ;; -Q | --quarterly ) if [ -z "${frequency}" ]; then - readonly frequency="quarterly" + frequency="quarterly" else duplicateOption "frequency" "${_option}" fi ;; -S | --semi | --semiannually ) if [ -z "${frequency}" ]; then - readonly frequency="semiannually" + frequency="semiannually" else duplicateOption "frequency" "${_option}" fi ;; -Y | --yearly | --annually ) if [ -z "${frequency}" ]; then - readonly frequency="yearly" + frequency="yearly" else duplicateOption "frequency" "${_option}" fi diff --git a/src/lib/cron/80adsorber b/src/lib/cron/80adsorber index f11bb6c..32b4f19 100755 --- a/src/lib/cron/80adsorber +++ b/src/lib/cron/80adsorber @@ -1,7 +1,7 @@ #!/bin/sh # # (Ad)sorber v#@version@# crontab file -# Updates the hosts file once a week. +# Updates the hosts file #@frequency@#. # # For more information: https://github.com/stablestud/adsorber # Or send me an email: diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index f5cc09c..9b61f02 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -13,7 +13,7 @@ # If you run this file independently following variables need to be set: # ---variable:------- ---default value:------------ ---declared in:--------- # executable_dir_path the root dir of the script src/bin/adsorber -# frequency none (null) src/bin/adsorber +# frequency null (not set) src/bin/adsorber # library_dir_path ${executable_dir_path}/../lib src/bin/adsorber # prefix ' ' (two spaces) src/lib/colours.sh # prefix_input ' ' (two spaces) src/lib/colours.sh @@ -30,14 +30,14 @@ crontabSetup() { - echo "${prefix}Setting up cronjob ..." + echo "${prefix}Setting up ${frequency} Cronjob ..." # 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 - echo "${prefix}Is a cron service installed? If not use systemd if possible." + echo "${prefix}Is a Cron service installed? If not use Systemd if possible." remove_ErrorCleanUp exit 126 fi @@ -45,14 +45,15 @@ crontabSetup() # Replace the @ place holder line with the location of adsorber in 80adsorber # and copy and manipulate the content to crontabs directory sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/80adsorber" \ - | sed "s|^#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" \ + | sed "s|#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" \ + | sed "s/#@frequency@#/${frequency}/g" \ > "${crontab_dir_path}/80adsorber" chmod u=rwx,g=rx,o=rx "${crontab_dir_path}/80adsorber" chown root:root "${crontab_dir_path}/80adsorber" - # Make known that we have setup the crontab in this run, - # if we fail now, crontab will be also removed (see remove_ErrorCleanUp) + # Make known that we have setup the Crontab in this run, + # if we fail now, Crontab will be also removed (see remove_ErrorCleanUp) readonly setup_scheduler="cronjob" } @@ -60,39 +61,56 @@ crontabSetup() crontabPromptFrequency() { if [ -z "${frequency}" ]; then - printf "%bHow often should the service run? [(h)ourly/(d)aily/(W)eekly/(m)onthly]: " "${prefix_input}" + _freq_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] ) + frequency="hourly" readonly crontab_dir_path="/etc/cron.hourly/" ;; [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) + frequency="daily" readonly crontab_dir_path="/etc/cron.daily/" ;; [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) + frequency="weekly" readonly crontab_dir_path="/etc/cron.weekly/" ;; [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) + frequency="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] ) - echo "${prefix_warning}Sorry, this frequency is only available with Systemd." - echo "Exiting ..." - remove_ErrorCleanUp - exit 1 + if [ "${_freq_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 + remove_ErrorCleanUp + exit 1 + fi ;; * ) - echo "${prefix_warning}Frequency '${frequency}' not understood." - echo "Aborting ..." - remove_ErrorCleanUp - exit 1 + if [ "${_freq_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 + remove_ErrorCleanUp + exit 1 + fi ;; esac + + unset _freq_input } @@ -102,7 +120,7 @@ crontabRemove() # Remove the crontab from /etc/cron.weekly rm "${crontab_dir_path}/80adsorber" \ || { - printf "%bCouldn't remove crontab %s\\n." \ + printf "%bCouldn't remove Crontab %s\\n." \ "${prefix_warning}" "${crontab_dir_path}" 1>&2 return 1 @@ -117,6 +135,6 @@ crontabRemove() crontab() { - crontabPromptFrequency - crontabSetup + crontabPromptFrequency \ + && crontabSetup } diff --git a/src/lib/systemd/adsorber.service b/src/lib/systemd/adsorber.service index 7256b47..da5691b 100755 --- a/src/lib/systemd/adsorber.service +++ b/src/lib/systemd/adsorber.service @@ -1,5 +1,5 @@ [Unit] -Description=Adsorber service to update the hosts file periodically. +Description=Adsorber service to update the hosts file #@frequency@#. Documentation=https://github.com/stablestud/adsorber After=network.target diff --git a/src/lib/systemd/adsorber.timer b/src/lib/systemd/adsorber.timer index eaa9402..d288538 100755 --- a/src/lib/systemd/adsorber.timer +++ b/src/lib/systemd/adsorber.timer @@ -1,5 +1,5 @@ [Unit] -Description=Timer that runs the Adsorber service weekly. +Description=Timer that runs the Adsorber service #@frequency@#. Documentation=https://github.com/stablestud/adsorber Requires=network-online.target diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index 247abb8..a66c496 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -13,6 +13,7 @@ # If you run this file independently following variables need to be set: # ---variable:------- ---default value:------------ ---declared in:--------- # executable_dir_path the root dir of the script src/bin/adsorber +# frequency null (not set) src/bin/adsorber # library_dir_path ${executable_dir_path}/../lib src/bin/adsorber # prefix ' ' (two spaces) src/lib/colours.sh # prefix_input ' ' (two spaces) src/lib/colours.sh @@ -35,26 +36,31 @@ systemdSetup() printf "%bWrong systemd_dir_path set. Can't access: %s.%b\\n" \ "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 - echo "${prefix}Is Systemd installed? If not use cron instead." + echo "${prefix}Is Systemd installed? If not use Cron instead." remove_ErrorCleanUp exit 126 fi # Remove systemd service if already present if [ -f "${systemd_dir_path}/adsorber.service" ] || [ -f "${systemd_dir_path}/adsorber.timer" ]; then - echo "${prefix}Removing previous systemd services ..." - systemdRemove + 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 + remove_ErrorCleanUp; + exit 1; + fi fi - echo "${prefix}Setting up systemd service ..." + echo "${prefix}Setting up ${frequency} Systemd service ..." # 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|g" \ - "${library_dir_path}/systemd/adsorber.service" > "${systemd_dir_path}/adsorber.service" + sed "s|#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" "${library_dir_path}/systemd/adsorber.service" \ + | sed "s/#@frequency@#/${frequency}/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/adsorber.timer" \ + sed "s/#@frequency@#/${frequency}/g" "${library_dir_path}/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" @@ -111,10 +117,8 @@ systemdPromptFrequency() readonly frequency="semiannually" ;; * ) - printf "%bFrequency '${frequency}' not understood.%b" "${prefix_warning}" "${prefix_reset}" - echo "${prefix_warning}Aborting ..." - remove_ErrorCleanUp - exit 1 + echo "${prefix_warning}Frequency '${_input_frequency}' not understood.${prefix_reset}" 1>&2 + systemdPromptFrequency ;; esac @@ -136,9 +140,9 @@ systemdRemove() systemctl disable adsorber.service 2>/dev/null 1>&2 # This service is not enabled by default # Remove leftover service files. - rm "${systemd_dir_path}/adsorber.timer" "${systemd_dir_path}/adsorber.service" \ + 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." \ + printf "%bCouldn't remove Systemd service files at %s%b\\n." \ "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 return 1 From 50ce4da09b55c3ef11ceca3f63bb719f71e14814 Mon Sep 17 00:00:00 2001 From: stablestud Date: Thu, 14 Jun 2018 01:11:33 +0200 Subject: [PATCH 08/43] Delete all crontabs, not only weekly --- misc/shellcheck.sh | 2 +- src/bin/adsorber | 14 +++++++------- src/lib/cron/cron.sh | 35 ++++++++++++++++++++--------------- src/lib/systemd/systemd.sh | 10 +++++----- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/misc/shellcheck.sh b/misc/shellcheck.sh index 4aade9a..1f4329c 100755 --- a/misc/shellcheck.sh +++ b/misc/shellcheck.sh @@ -13,7 +13,7 @@ fi echo "Running shellcheck ..." ( - cd -P "${script_dir_path}" -e || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } + cd -P -e "${script_dir_path}" || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } shellcheck -x \ "${script_dir_path}/../src/bin/adsorber" \ diff --git a/src/bin/adsorber b/src/bin/adsorber index f909ab3..74bef7e 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -375,7 +375,7 @@ for _option in "${@}"; do ;; -H | --hourly ) if [ -z "${frequency}" ]; then - frequency="hourly" + readonly frequency="hourly" else duplicateOption "frequency" "${_option}" fi @@ -383,42 +383,42 @@ for _option in "${@}"; do ;; -D | --daily ) if [ -z "${frequency}" ]; then - frequency="daily" + readonly frequency="daily" else duplicateOption "frequency" "${_option}" fi ;; -W | --weekly ) if [ -z "${frequency}" ]; then - frequency="weekly" + readonly frequency="weekly" else duplicateOption "frequency" "${_option}" fi ;; -M | --monthly ) if [ -z "${frequency}" ]; then - frequency="monthly" + readonly frequency="monthly" else duplicateOption "frequency" "${_option}" fi ;; -Q | --quarterly ) if [ -z "${frequency}" ]; then - frequency="quarterly" + readonly frequency="quarterly" else duplicateOption "frequency" "${_option}" fi ;; -S | --semi | --semiannually ) if [ -z "${frequency}" ]; then - frequency="semiannually" + readonly frequency="semiannually" else duplicateOption "frequency" "${_option}" fi ;; -Y | --yearly | --annually ) if [ -z "${frequency}" ]; then - frequency="yearly" + readonly frequency="yearly" else duplicateOption "frequency" "${_option}" fi diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index 9b61f02..b75c3a4 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -30,7 +30,7 @@ crontabSetup() { - echo "${prefix}Setting up ${frequency} Cronjob ..." + echo "${prefix}Setting up ${frequency_string} Cronjob ..." # Check if crontabs directory variable is correctly set, if not abort and call the error clean-up function if [ ! -d "${crontab_dir_path}" ]; then @@ -46,7 +46,7 @@ crontabSetup() # and copy and manipulate the content to crontabs directory sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/80adsorber" \ | sed "s|#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" \ - | sed "s/#@frequency@#/${frequency}/g" \ + | sed "s/#@frequency@#/${frequency_string}/g" \ > "${crontab_dir_path}/80adsorber" chmod u=rwx,g=rx,o=rx "${crontab_dir_path}/80adsorber" @@ -61,64 +61,69 @@ crontabSetup() crontabPromptFrequency() { if [ -z "${frequency}" ]; then - _freq_input="true" + _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] ) - frequency="hourly" + readonly frequency_string="hourly" readonly crontab_dir_path="/etc/cron.hourly/" ;; [Dd] | [Dd][Aa][Yy] | [Dd][Aa][Ii][Ll][Yy] ) - frequency="daily" + readonly frequency_string="daily" readonly crontab_dir_path="/etc/cron.daily/" ;; [Ww] | "" | [Ww][Ee][Ee][Kk] | [Ww][Ee][Ee][Kk][Ll][Yy] ) - frequency="weekly" + readonly frequency_string="weekly" readonly crontab_dir_path="/etc/cron.weekly/" ;; [Mm] | [Mm][Oo][Nn][Tt][Hh] | [Mm][Oo][Nn][Tt][Hh][Ll][Yy] ) - frequency="monthly" + 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 [ "${_freq_input}" = "true" ]; then + 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 + printf "%bThis frequency is only available with Systemd.%b\\n" \ + "${prefix_fatal}" "${prefix_reset}" 1>&2 remove_ErrorCleanUp exit 1 fi ;; * ) - if [ "${_freq_input}" = "true" ]; then + 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 + printf "%bFrequency '%s' not understood.%b\\n" \ + "${prefix_fatal}" "${frequency}" "${prefix_reset}" 1>&2 remove_ErrorCleanUp exit 1 fi ;; esac - unset _freq_input + unset _used_input } crontabRemove() { - if [ -f "${crontab_dir_path}/80adsorber" ]; then - # Remove the crontab from /etc/cron.weekly - rm "${crontab_dir_path}/80adsorber" \ + if [ -f "/etc/cron.hourly/80adsorber" ] \ + || [ -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 diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index a66c496..aef0b91 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -91,9 +91,9 @@ 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 _input_frequency + read -r _freq_input - case "${_input_frequency}" in + case "${_freq_input}" in [Hh] | [Hh][Oo][Uu][Rr] | [Hh][Oo][Uu][Rr][Ll][Yy] ) readonly frequency="hourly" ;; @@ -117,12 +117,12 @@ systemdPromptFrequency() readonly frequency="semiannually" ;; * ) - echo "${prefix_warning}Frequency '${_input_frequency}' not understood.${prefix_reset}" 1>&2 + echo "${prefix_warning}Frequency '${_freq_input}' not understood.${prefix_reset}" 1>&2 systemdPromptFrequency ;; esac - unset _input_frequency + unset _freq_input fi } @@ -131,7 +131,7 @@ systemdRemove() { if [ -f "${systemd_dir_path}/adsorber.service" ] || [ -f "${systemd_dir_path}/adsorber.timer" ]; then - # Disable timer and add "${prefix}" to the output stream, to format it so it can fit the Adsorber 'style' + # Disable timer systemctl stop adsorber.timer 2>/dev/null systemctl disable adsorber.timer 2>/dev/null \ From fd930769e8b34ec699db10f31700e55c74ad89fa Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 18:28:21 +0100 Subject: [PATCH 09/43] Fixed a few source code formatting issues Removed trailing whitespaces and tabs on emtpy lines --- misc/shellcheck.sh | 2 +- src/bin/adsorber | 3 +-- src/lib/systemd/systemd.sh | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/misc/shellcheck.sh b/misc/shellcheck.sh index 1f4329c..91230eb 100755 --- a/misc/shellcheck.sh +++ b/misc/shellcheck.sh @@ -13,7 +13,7 @@ fi echo "Running shellcheck ..." ( - cd -P -e "${script_dir_path}" || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } + cd -P -e "${script_dir_path}" || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } shellcheck -x \ "${script_dir_path}/../src/bin/adsorber" \ diff --git a/src/bin/adsorber b/src/bin/adsorber index 74bef7e..c1f58e2 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -256,7 +256,7 @@ duplicateOption() echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" elif [ "${1}" = "frequency" ]; then - echo "Adsorber: Duplicate option for schedulers frequency: '${2}'" 1>&2 + echo "Adsorber: Duplicate option for schedulers frequency: '${2}'" 1>&2 echo "You may only select one:" echo " -H, --hourly - run scheduler once hourly" echo " -D, --daily ... daily" @@ -379,7 +379,6 @@ for _option in "${@}"; do else duplicateOption "frequency" "${_option}" fi - ;; -D | --daily ) if [ -z "${frequency}" ]; then diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index aef0b91..931c20b 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -89,7 +89,7 @@ systemdSetup() systemdPromptFrequency() { - if [ -z "${frequency}" ]; then + 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 @@ -121,7 +121,7 @@ systemdPromptFrequency() systemdPromptFrequency ;; esac - + unset _freq_input fi } From d86e82a44f52fe333b01462b15bd9356adac6ab0 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 18:35:22 +0100 Subject: [PATCH 10/43] Remove execute permission from non-executing files Systemd throws a warning if service files have the execute bit. Also config files are not executed. --- src/lib/systemd/adsorber.service | 0 src/lib/systemd/adsorber.timer | 0 src/share/components/hosts_header | 0 src/share/components/hosts_title | 0 src/share/default/default-adsorber.conf | 0 src/share/default/default-blacklist | 0 src/share/default/default-sources.list | 0 src/share/default/default-whitelist | 0 8 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/lib/systemd/adsorber.service mode change 100755 => 100644 src/lib/systemd/adsorber.timer mode change 100755 => 100644 src/share/components/hosts_header mode change 100755 => 100644 src/share/components/hosts_title mode change 100755 => 100644 src/share/default/default-adsorber.conf mode change 100755 => 100644 src/share/default/default-blacklist mode change 100755 => 100644 src/share/default/default-sources.list mode change 100755 => 100644 src/share/default/default-whitelist diff --git a/src/lib/systemd/adsorber.service b/src/lib/systemd/adsorber.service old mode 100755 new mode 100644 diff --git a/src/lib/systemd/adsorber.timer b/src/lib/systemd/adsorber.timer old mode 100755 new mode 100644 diff --git a/src/share/components/hosts_header b/src/share/components/hosts_header old mode 100755 new mode 100644 diff --git a/src/share/components/hosts_title b/src/share/components/hosts_title old mode 100755 new mode 100644 diff --git a/src/share/default/default-adsorber.conf b/src/share/default/default-adsorber.conf old mode 100755 new mode 100644 diff --git a/src/share/default/default-blacklist b/src/share/default/default-blacklist old mode 100755 new mode 100644 diff --git a/src/share/default/default-sources.list b/src/share/default/default-sources.list old mode 100755 new mode 100644 diff --git a/src/share/default/default-whitelist b/src/share/default/default-whitelist old mode 100755 new mode 100644 From 8e1744e644132250dc8e87ac22a788325a970a0a Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 18:48:13 +0100 Subject: [PATCH 11/43] Renamed Adsorbers initial script file It is confusing with all those script files and adsorber commands to properly understand what is doing what. Thus I renamed the script file to better understand what is does and when you should use it. --- ...to_system.sh => place_files_onto_system.sh | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) rename install_to_system.sh => place_files_onto_system.sh (88%) diff --git a/install_to_system.sh b/place_files_onto_system.sh similarity index 88% rename from install_to_system.sh rename to place_files_onto_system.sh index 9ac0663..d4b98da 100755 --- a/install_to_system.sh +++ b/place_files_onto_system.sh @@ -44,9 +44,9 @@ printHelp() echo " Will place Adsorbers executables and other" echo " files relevant to Adsorber into the system." echo - printf "\\033[4;37mNote\\033[0m: Adsorbers own 'install' command will not do the same action as\\n" + printf "\\033[4;37mNote\\033[0m: Adsorbers own 'setup' command will not do the same action as\\n" echo "this script, as it will only setup the scheduler and backup the original hosts file." - echo "You may want to run 'adsorber install' afterwards" + echo "You may want to run 'adsorber setup' afterwards" echo echo "Usage: ${0} [option]:" echo @@ -70,7 +70,7 @@ printLocation echo if [ -z "${prompt}" ]; then - printf "Are you sure you want to install Adsorber into the system? [(Y)es/(N)o]: " + printf "Are you sure you want to place Adsorbers files onto the system? [(y)es/(N)o]: " read -r prompt fi @@ -79,14 +79,14 @@ case "${prompt}" in : ;; * ) - echo "Installation to the system has been cancelled." + echo "Placing files onto the system has been cancelled." exit 1 ;; esac # Check if user is root, if not exit. if [ "$(id -g)" -ne 0 ]; then - echo "You need to be root to install Adsorber into the system." 1>&2 + echo "You need to be root to place Adsorbers files onto the system." 1>&2 exit 126 fi @@ -164,18 +164,16 @@ chown -R root:root "${config_dir_path}" \ printf "Couldn't set ownership of %s" "${config_dir_path}" } +echo "Adsorbers files have been successfully placed onto the system." +printf "\\033[1;37mTo start going and to setup the scheduler or to backup the hosts file run 'adsorber setup'\\033[0m\\n" echo ## We don't run Adsorber after installation yet -#adsorber install \ +#adsorber setup \ # || { -# printf "\033[0;93mAdsorber was installed on your system, however something went wrong at\\n" +# printf "\033[0;93mAdsorber has been placed onto your system, however something went wrong at\\n" # printf "running it.\\n" # printf "If a proxy server is in use, please change the config file\\n" # printf "(${config_dir_path}/adsorber.conf) to the appropriate proxy server.\\n\033[0m" -# echo "Run 'adsorber install' to try again." +# echo "Run 'adsorber setup' to try again." # } - -echo "Installation completed." -printf "\\033[1;37mTo start going and to setup the scheduler or to backup the hosts file run 'adsorber install'\\033[0m\\n" -echo "You can now delete this folder." From 905db03d3627bc31482b525b6b2b435b4ebc36b6 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 19:11:34 +0100 Subject: [PATCH 12/43] Be more verbose where the files are being placed --- place_files_onto_system.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index d4b98da..972cfa9 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -51,7 +51,7 @@ printHelp() echo "Usage: ${0} [option]:" echo echo "Options:" - echo " -y, --yes automatically reply the prompt with yes" + echo " -y, --yes automatically reply the confirmation prompt with yes" echo " -h, --help show this help screen" echo printLocation @@ -93,7 +93,7 @@ fi echo ##[ Main exectuable ]########################################################### -echo "Placing main executable to ${executable_path}" +echo "Placing main executable (src/bin/adsorber) to ${executable_path}" mkdir -p "$(dirname ${executable_path})" @@ -114,7 +114,7 @@ chown root:root "${executable_path}" \ ##[ Libraries ]################################################################# -echo "Placing other executables to ${library_dir_path}" +echo "Placing other executables (src/lib/*) to ${library_dir_path}" mkdir -p "${library_dir_path}" @@ -130,13 +130,13 @@ chown -R root:root "${library_dir_path}" \ } ##[ Shareables ]################################################################ -echo "Placing miscellaneous to ${shareable_dir_path}" +echo "Placing miscellaneous (src/share/*) to ${shareable_dir_path}" mkdir -p "${shareable_dir_path}" cp -r "${script_dir_path}/src/share/." "${shareable_dir_path}" -chmod -R u=rwx,g=rx,o=rx "${shareable_dir_path}" \ +chmod -R u=rw,g=r,o=r "${shareable_dir_path}" \ || { printf "Couldn't set permissions for %s" "${shareable_dir_path}" } @@ -146,7 +146,7 @@ chown -R root:root "${shareable_dir_path}" \ } ##[ Config files ]############################################################## -echo "Placing config files to ${config_dir_path}" +echo "Copying config files (src/share/default/*) to ${config_dir_path}" mkdir -p "${config_dir_path}" @@ -155,7 +155,7 @@ cp "${script_dir_path}/src/share/default/default-blacklist" "${config_dir_path}/ cp "${script_dir_path}/src/share/default/default-whitelist" "${config_dir_path}/whitelist" cp "${script_dir_path}/src/share/default/default-sources.list" "${config_dir_path}/sources.list" -chmod -R u=rwx,g=rx,o=rx "${config_dir_path}" \ +chmod -R u=rw,g=r,o=r "${config_dir_path}" \ || { printf "Couldn't set permissions for %s" "${config_dir_path}" } @@ -164,7 +164,7 @@ chown -R root:root "${config_dir_path}" \ printf "Couldn't set ownership of %s" "${config_dir_path}" } -echo "Adsorbers files have been successfully placed onto the system." +echo "Adsorber files have been successfully placed onto the system." printf "\\033[1;37mTo start going and to setup the scheduler or to backup the hosts file run 'adsorber setup'\\033[0m\\n" echo From 575718f0363f2dc4844faf1ed2643a71fdcfbfb0 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 19:15:17 +0100 Subject: [PATCH 13/43] Fixed illegal option error for cd Not every 'cd' command accepts the option -e, therefore I removed it. --- misc/shellcheck.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/misc/shellcheck.sh b/misc/shellcheck.sh index 91230eb..ca80ab3 100755 --- a/misc/shellcheck.sh +++ b/misc/shellcheck.sh @@ -8,19 +8,20 @@ readonly script_dir_path="$(cd "$(dirname "${0}")" && pwd)" if ! command -v shellcheck 2>/dev/null 1>&2; then echo "Shellcheck must be installed." + exit 1 fi echo "Running shellcheck ..." ( - cd -P -e "${script_dir_path}" || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } + cd -P "${script_dir_path}" || { echo "Couldn't descend to ${script_dir_path}"; exit 1; } shellcheck -x \ + "${script_dir_path}/shellcheck.sh" \ "${script_dir_path}/../src/bin/adsorber" \ "${script_dir_path}/../portable_adsorber.sh" \ - "${script_dir_path}/../install_to_system.sh" \ - "${script_dir_path}/../remove_from_system.sh" \ - "${script_dir_path}/shellcheck.sh" + "${script_dir_path}/../place_files_onto_system.sh" \ + "${script_dir_path}/../remove_from_system.sh" echo "Done." ) From 012e45304882f855e4c38560f8969576c758b647 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 19:20:23 +0100 Subject: [PATCH 14/43] Add description to misc/clean.sh desc what is does --- misc/clean.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/misc/clean.sh b/misc/clean.sh index e18fc1d..5e3ea59 100755 --- a/misc/clean.sh +++ b/misc/clean.sh @@ -1,4 +1,5 @@ #!/bin/sh +# Cleans the scripts root directory of config files created by portable_adsorber.sh readonly source_dir_path="$(cd "$(dirname "${0}")"/.. && pwd)" From d56bc02a7c4d7af1cb98d4c7d1ffaf7ef54abcc1 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 19:30:56 +0100 Subject: [PATCH 15/43] Renamed Adsorbers removal script file With all those script files and commands its hard to distinguish what is doing what. Thus I renamed it to be understandable. Also the source code description has been updated/extended. --- portable_adsorber.sh | 3 ++- remove_from_system.sh => remove_files_from_system.sh | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) rename remove_from_system.sh => remove_files_from_system.sh (93%) diff --git a/portable_adsorber.sh b/portable_adsorber.sh index 42f844b..6e474a1 100755 --- a/portable_adsorber.sh +++ b/portable_adsorber.sh @@ -25,7 +25,8 @@ runAdsorber() if runAdsorber; then echo "Adsorber in portable-mode exited with code ${exit_code}." else - # I defined exit code 80 as an error code if wrong or no input has been made + # I defined exit code 80 as an error code in the adsorber main executable + # if wrong or no input has been made if [ "${exit_code}" -eq 80 ]; then echo "You've supplied no or wrong parameters." fi diff --git a/remove_from_system.sh b/remove_files_from_system.sh similarity index 93% rename from remove_from_system.sh rename to remove_files_from_system.sh index 87260c4..99d0e5e 100755 --- a/remove_from_system.sh +++ b/remove_files_from_system.sh @@ -6,15 +6,15 @@ # This file can run independently, no need to download the full repository to # remove an existing installation. -# Note: only run this file if Adsorber was installed into the system and not if -# it was used with portable-mode (portable_adsorber.sh) +# Note: only run this file if Adsorber was placed onto the system (via place_files_onto_system.sh) +# and not if it was used with portable-mode (portable_adsorber.sh) ##########[ Edit to fit your system ]########################################### # Define where the executable 'adsorber' is. readonly executable_path="/usr/local/bin/adsorber" -# Define where the other executable are. +# Define where the other executables are. readonly library_dir_path="/usr/local/lib/adsorber/" # Define the location of adsorbers shareable data (e.g. default config files...). @@ -87,7 +87,7 @@ echo # 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 (see above) from the system? [(y)es/(N)o]: " read -r prompt fi @@ -139,7 +139,7 @@ rm -r "${library_dir_path}" 2>/dev/null && echo "Cleaned ${library_dir_path}" rm -r "${shareable_dir_path}" 2>/dev/null && echo "Cleaned ${shareable_dir_path}" rm -r "${config_dir_path}" 2>/dev/null && echo "Cleaned ${config_dir_path}" -# Remove the adsorber command from cache/hashtable +# Remove the adsorber command from cache/hashtable. # Shells must be reloaded / reopened to have an effect echo "Clearing adsorber from shell cache ..." if command -v hash 1>/dev/null; then From e8e656e21c343cec1172db55f52d00750a7857bc Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 22:39:23 +0100 Subject: [PATCH 16/43] Updated default description of default conf files --- src/share/default/default-adsorber.conf | 31 ++++++++++++++----------- src/share/default/default-whitelist | 6 ++--- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/share/default/default-adsorber.conf b/src/share/default/default-adsorber.conf index 35fe672..e2677be 100644 --- a/src/share/default/default-adsorber.conf +++ b/src/share/default/default-adsorber.conf @@ -1,7 +1,7 @@ #@ Config file for Adsorber v?.?.? (DO NOT EDIT, WILL BE OVERWRITTEN BY SCRIPT)@ # -# To reset the config file just delete it and run 'adsorber install', -# a new config file will be created. +# To reset the config file just delete it and run 'adsorber setup', +# a new config file will be created. # # For more information please visit: https://github.com/stablestud/Adsorber # Don't hesitate to create issues and pull-requests. @@ -23,7 +23,7 @@ primary_list=blacklist ## use_partial_matching # Defines whether the WHITELIST includes sub-domains or not. # -# For example: You white listed the domain wholesome-ads.com, if the value +# For example: You whitelisted the domain wholesome-ads.com, if the value # is set to 'true' all it's sub-domains like annoying.wholesome-ads.com will be # also allowed. To disable this set the value to 'false'. # @@ -37,11 +37,11 @@ use_partial_matching=true ## ignore_download_error -# If set to false, we'll only apply the hosts file if all hosts sources +# If set to false, it'll only apply the hosts file if all hosts sources # could be reached. # -# For example: You have a few host sources but one is not responding, to not -# lose the already installed domains from the particular source, the script +# For example: You have two host sources but one is not responding, to not +# lose the already installed domains from the failing source, the script # won't continue. # # Possible values: true, false @@ -55,6 +55,9 @@ ignore_download_error=true # Adsorber will prioritize the root users environment variables of 'http_proxy' # and 'https_proxy' over the ones specified here. # +# Please note that your user 'http(s)_proxy' environment variables won't affect Adsorber +# as it doesn't run as user but as root user. +# # Possible value: address:port (e.g proxy:8080, 127.0.0.1:9050) # Default value: Null (not set) # @@ -71,8 +74,8 @@ https_proxy= ## hosts_file_path -# hosts_file_pat is used to set the path to the systems hosts file. -# Change if your system uses an other file or path. +# hosts_file_path is used to set the path to the systems hosts file. +# Change if your system uses a different file or path. # # Default value: /etc/hosts @@ -80,17 +83,17 @@ hosts_file_path=/etc/hosts ## hosts_file_backup_path -# hosts_file_backup_path is used to set the filename and the path to the -# backup of the hosts file to be created by Adsorber in the install process. -# To simply create a new backup run 'adsorber install' +# Defines the backup location of the original hosts file created by the operating system. +# If no backup was created you can do this by running 'adsorber setup'. # # Default value: /etc/hosts.original hosts_file_backup_path=/etc/hosts.original -## hosts_file_previous_enable, hosts_file_previous_path ## -# Info goes here +## 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'. # # Possible values for 'hosts_file_previous_enable': true, false # Default value for 'hosts_file_previous_enable': true @@ -104,7 +107,7 @@ hosts_file_previous_path=/etc/hosts.previous ## systemd_dir_path # This defines the directory in which we'll place the systemd files. # Used to create a scheduler which supplies the hosts file regulary with -# new ad-domains. Change if your system has an other path. +# new ad-domains. Only change if your system has an other path. # # Default value: /etc/systemd/system/ diff --git a/src/share/default/default-whitelist b/src/share/default/default-whitelist index 117af7a..284a228 100644 --- a/src/share/default/default-whitelist +++ b/src/share/default/default-whitelist @@ -1,10 +1,10 @@ # Whitelist # Use to allow domains, even if they are blocked from some hosts sources. -# By default, the blacklist overwrites the whitelist. This can be changed in adsorber.conf. +# However the blacklist overwrites the whitelist by default. This can be changed in adsorber.conf. # -# Note: the whitelist uses partial matching. Means if you allow adserver.tld +# Note: the whitelist uses partial matching. Means if you allow adserver.tld, # all its subdomains like ad1.adserver.tld also will be allowed. -# (Can be changed in adsorber.conf by editing 'use_partial_matching') +# (Can be changed in adsorber.conf by changing 'use_partial_matching' to false) # # Entries must begin with either 127.0.0.1 or 0.0.0.0 From 690555701980f6c4e8eb8ce5a99fb65d29cb3bec Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 22:41:56 +0100 Subject: [PATCH 17/43] Updated description of insert hosts file message --- src/share/components/hosts_header | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/share/components/hosts_header b/src/share/components/hosts_header index e7acf4f..4a47fc3 100644 --- a/src/share/components/hosts_header +++ b/src/share/components/hosts_header @@ -5,6 +5,6 @@ # and run 'adsorber update' to apply your changes. # Or else your changes will be overwritten by Adsorber once a week. # -# By default, the domains will be updated once a week. +# By default, the domains will be updated once a week if not other specified. # For further information please run 'adsorber --help' # Or visit: https://github.com/stablestud/adsorber From df0f2c0de49a6b55d27f111a749903cc5d57d064 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 22:44:55 +0100 Subject: [PATCH 18/43] Improved removal script to clean all cronjobs --- remove_files_from_system.sh | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 99d0e5e..792e923 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -33,7 +33,6 @@ readonly hosts_file_path="/etc/hosts" readonly hosts_file_backup_path="/etc/hosts.original" readonly hosts_file_previous_path="/etc/hosts.previous" readonly systemd_dir_path="/etc/systemd/system" -readonly crontab_file_path="/etc/cron.weekly/80adsorber" readonly tmp_dir_path="/tmp/adsorber" ##########[ End of configuration ]############################################## @@ -115,11 +114,45 @@ printf "\\nRunning 'adsorber remove -y' ...\\n" printf "\\033[0;93mSomething went wrong at running Adsorber's own removal action.\\nNo worries, I can handle it ...\\n\\033[0m" echo "Maybe Adsorber has been already removed ?" + printf "Trying portable_adsorber.sh ... " + + if ${script_dir_path}/portable_adsorber.sh "remove" "-y" 2>/dev/null 1>&2; then + printf "found" + else + ${script_dir_path}/misc/clean.sh 2>/dev/null 1>&2 + fi + + + printf "\n\n" + # Doing it the hard way .., removing everything manually 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" rm "${crontab_file_path}" 2>/dev/null && echo "Removed ${crontab_file_path}" + + # 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 + + if [ -f "/etc/cron.daily/80adsorber" ]; then + 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 + + if [ -f "/etc/cron.monthly/80adsorber" ]; then + 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}" if [ -f "${hosts_file_backup_path}" ]; then From eb01dedaec801d6d5bf2a2b698745ce99c91f56f Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 22:46:18 +0100 Subject: [PATCH 19/43] Removed accomplished todo's from TODO.md --- TODO.md | 4 ---- place_files_onto_system.sh | 2 +- portable_adsorber.sh | 2 ++ 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 8747b59..2b52970 100644 --- a/TODO.md +++ b/TODO.md @@ -2,15 +2,11 @@ ## Must: * README.md update -* Remove all crontabs from cron.x directories -* Update comments about weekly frequency, in `hosts_header` ## Should: -* interactive ability to set the update frequency * save statistics to var/ and read from Adsorber ## Could: -* OpenRC support * log-file * bash completion * proper unittest diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index 972cfa9..fe772c1 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -165,7 +165,7 @@ chown -R root:root "${config_dir_path}" \ } echo "Adsorber files have been successfully placed onto the system." -printf "\\033[1;37mTo start going and to setup the scheduler or to backup the hosts file run 'adsorber setup'\\033[0m\\n" +printf "\\033[1;37mTo start going (to setup the scheduler and to backup the hosts file) run 'adsorber setup'\\033[0m\\n" echo ## We don't run Adsorber after installation yet diff --git a/portable_adsorber.sh b/portable_adsorber.sh index 6e474a1..3abefda 100755 --- a/portable_adsorber.sh +++ b/portable_adsorber.sh @@ -32,4 +32,6 @@ else fi echo "Adsorber in portable-mode exited with code ${exit_code}. Thats an error." + + exit ${exit_code} fi From ba15308bf116afe1768405d5ec82eaf24fc10361 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 23:01:38 +0100 Subject: [PATCH 20/43] Fix quotes to prevent globbing and word splitting --- misc/shellcheck.sh | 2 +- remove_files_from_system.sh | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/misc/shellcheck.sh b/misc/shellcheck.sh index ca80ab3..902c1f6 100755 --- a/misc/shellcheck.sh +++ b/misc/shellcheck.sh @@ -21,7 +21,7 @@ echo "Running shellcheck ..." "${script_dir_path}/../src/bin/adsorber" \ "${script_dir_path}/../portable_adsorber.sh" \ "${script_dir_path}/../place_files_onto_system.sh" \ - "${script_dir_path}/../remove_from_system.sh" + "${script_dir_path}/../remove_files_from_system.sh" echo "Done." ) diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 792e923..32b0174 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -116,10 +116,10 @@ printf "\\nRunning 'adsorber remove -y' ...\\n" printf "Trying portable_adsorber.sh ... " - if ${script_dir_path}/portable_adsorber.sh "remove" "-y" 2>/dev/null 1>&2; then + if "${script_dir_path}/portable_adsorber.sh" "remove" "-y" 2>/dev/null 1>&2; then printf "found" else - ${script_dir_path}/misc/clean.sh 2>/dev/null 1>&2 + "${script_dir_path}/misc/clean.sh" 2>/dev/null 1>&2 fi @@ -129,7 +129,6 @@ printf "\\nRunning 'adsorber remove -y' ...\\n" 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" - rm "${crontab_file_path}" 2>/dev/null && echo "Removed ${crontab_file_path}" # Remove all crontabs if [ -f "/etc/cron.hourly/80adsorber" ]; then From 76dd7db508bb716bde49c44e17c033ac1d76b807 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 23:02:36 +0100 Subject: [PATCH 21/43] Fix and update help description --- src/bin/adsorber | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/bin/adsorber b/src/bin/adsorber index c1f58e2..dc522ee 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -81,7 +81,7 @@ showHelp() { echo "Usage: adsorber []" echo "" - echo "(Ad)sorber blocks ads by 'absorbing' and dumbing them into void." + echo "(Ad)sorber blocks ads by \"absorbing\" and dumbing them into void." echo " (with the help of the hosts file)" echo "" echo "Operations (required):" @@ -92,7 +92,7 @@ showHelp() echo " update - update hosts file with newest ad servers" 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 host file." + echo " revert - reverts the hosts file to the last applied (previous) host file." echo " remove - completely remove changes made by Adsorber" echo " e.g., remove scheduler (if set)" echo " restore hosts file to its original state" @@ -104,6 +104,8 @@ 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 "" echo "Scheduler specifc options (use with 'setup'):" echo " -ns, --no-scheduler - skip scheduler creation" echo " -s, --systemd - use Systemd ..." @@ -132,18 +134,18 @@ showSpecificHelp() printf "\\033[4;37madsorber setup []\\033[0m:\\n" echo echo "You should run this command first." - echo " (e.g. after installation to the system)" + echo " (e.g. after files have been placed to the system)" echo echo "The command will:" echo " - backup your /etc/hosts file to /etc/hosts.original" echo " (if not other specified in adsorber.conf)" - echo " - setup a scheduler which updates your hosts file with ad-domains" + echo " - setup a scheduler which updates your hosts file with ad-domains periodically" echo " (either systemd, cronjob or none)" echo " - fetch the newest ad-domains to your hosts file." - echo " (same as 'adsorber update')" + echo " (it runs 'adsorber update')" echo - echo "Note: this is not the same as the install_to_system.sh script." - echo "install_to_system.sh will place Adsorbers executable files into the system" + echo "Note: this is not the same as the place_files_onto_system.sh script." + echo "place_files_onto_system.sh will place Adsorbers executable files into the system" echo "so it can run directory independently, but it will not take the actions" echo "described here. The same goes for 'remove'" echo @@ -184,7 +186,7 @@ showSpecificHelp() echo "The command will:" echo " - copy /etc/hosts.original to /etc/hosts, overwriting the modified /etc/hosts by adsorber." echo - echo "Important: If you have a scheduler setup it'll re-apply ad-domains to your hosts" + echo "Note: If you have a scheduler set it'll re-apply ad-domains to your hosts" echo "file when triggered." echo "For this reason this command is used to temporary disable Adsorber." echo "(e.g. when it's blocking some sites you need access for a short period of time)" @@ -198,7 +200,7 @@ showSpecificHelp() printf "\\033[4;37madsorber revert []\\033[0m:\\n" echo echo "To revert to the last applied hosts file." - echo "Good use if the current host file has been corruptea or misses some 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." From ad8dd5a0d1dd9661caa8a003e164cb19e35dfe49 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 23:16:58 +0100 Subject: [PATCH 22/43] Updated and added READMEs to directories The READMEs describe what role the folder/files have in the specific directory. --- misc/README.md | 8 +++++++- src/README.md | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/README.md diff --git a/misc/README.md b/misc/README.md index c923721..12e5bbd 100644 --- a/misc/README.md +++ b/misc/README.md @@ -1 +1,7 @@ -This directory contains a few tools which may aid in the development of Adsorber. +### This directory contains a few tools which may aid in the development of Adsorber. + +#### clean.sh +Clean the script's root directory from junk (e.g whitelist, blacklist). + +#### shellcheck.sh +Checks all script files with the shellcheck application against common script issues. diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..79ed9f5 --- /dev/null +++ b/src/README.md @@ -0,0 +1,3 @@ +This directory/file hierachy shows how the files will be placed into `/usr/local/`. + +`/usr/local/` is the default path for external applications not managed by the distribution. From fd433afd2725a1f6d7666f5799653b7538ae6de0 Mon Sep 17 00:00:00 2001 From: stablestud Date: Mon, 25 Feb 2019 23:45:02 +0100 Subject: [PATCH 23/43] Fixed folder permission for adsorber config files --- place_files_onto_system.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index fe772c1..75e3da6 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -103,7 +103,7 @@ sed "s|^readonly library_dir_path=\"\${executable_dir_path}/\\.\\./lib/\"$|reado | sed "s|^readonly config_dir_path=\"\${executable_dir_path}/\\.\\./\\.\\./\"$|readonly config_dir_path=\"${config_dir_path}\"|g" \ > "${executable_path}" -chmod u=rwx,g=rx,o=rx "${executable_path}" \ +chmod u=rwx,g=rx,o=rx "${executable_path}/*" \ || { printf "Couldn't set permissions for %s" "${executable_path}" } @@ -120,7 +120,7 @@ mkdir -p "${library_dir_path}" cp -r "${script_dir_path}/src/lib/." "${library_dir_path}" -chmod -R u=rwx,g=rx,o=rx "${library_dir_path}" \ +chmod -R u=rwx,g=rx,o=rx "${library_dir_path}/*" \ || { printf "Couldn't set permissions for %s" "${library_dir_path}" } @@ -136,7 +136,7 @@ mkdir -p "${shareable_dir_path}" cp -r "${script_dir_path}/src/share/." "${shareable_dir_path}" -chmod -R u=rw,g=r,o=r "${shareable_dir_path}" \ +chmod -R u=rw,g=r,o=r "${shareable_dir_path}/*" \ || { printf "Couldn't set permissions for %s" "${shareable_dir_path}" } @@ -155,7 +155,7 @@ cp "${script_dir_path}/src/share/default/default-blacklist" "${config_dir_path}/ cp "${script_dir_path}/src/share/default/default-whitelist" "${config_dir_path}/whitelist" cp "${script_dir_path}/src/share/default/default-sources.list" "${config_dir_path}/sources.list" -chmod -R u=rw,g=r,o=r "${config_dir_path}" \ +chmod -R u=rw,g=r,o=r "${config_dir_path}/*" \ || { printf "Couldn't set permissions for %s" "${config_dir_path}" } From 124cc81c38e06956df86cbdea0165600275fd26b Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 00:06:02 +0100 Subject: [PATCH 24/43] Let umask set the default permissions Removed manual permission set after file/folder creation, the umask is in all systems already set as preferred or even stricter. --- place_files_onto_system.sh | 33 ----------------------------- remove_files_from_system.sh | 8 +++++--- src/bin/adsorber | 5 +++-- src/lib/config.sh | 41 +++---------------------------------- 4 files changed, 11 insertions(+), 76 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index 75e3da6..d599ca0 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -103,15 +103,6 @@ sed "s|^readonly library_dir_path=\"\${executable_dir_path}/\\.\\./lib/\"$|reado | sed "s|^readonly config_dir_path=\"\${executable_dir_path}/\\.\\./\\.\\./\"$|readonly config_dir_path=\"${config_dir_path}\"|g" \ > "${executable_path}" -chmod u=rwx,g=rx,o=rx "${executable_path}/*" \ - || { - printf "Couldn't set permissions for %s" "${executable_path}" - } -chown root:root "${executable_path}" \ - || { - printf "Couldn't set ownership of %s" "${executable_path}" - } - ##[ Libraries ]################################################################# echo "Placing other executables (src/lib/*) to ${library_dir_path}" @@ -120,14 +111,6 @@ mkdir -p "${library_dir_path}" cp -r "${script_dir_path}/src/lib/." "${library_dir_path}" -chmod -R u=rwx,g=rx,o=rx "${library_dir_path}/*" \ - || { - printf "Couldn't set permissions for %s" "${library_dir_path}" - } -chown -R root:root "${library_dir_path}" \ - || { - printf "Couldn't set ownership of %s" "${library_dir_path}" - } ##[ Shareables ]################################################################ echo "Placing miscellaneous (src/share/*) to ${shareable_dir_path}" @@ -136,14 +119,6 @@ mkdir -p "${shareable_dir_path}" cp -r "${script_dir_path}/src/share/." "${shareable_dir_path}" -chmod -R u=rw,g=r,o=r "${shareable_dir_path}/*" \ - || { - printf "Couldn't set permissions for %s" "${shareable_dir_path}" - } -chown -R root:root "${shareable_dir_path}" \ - || { - printf "Couldn't set ownership of %s" "${shareable_dir_path}" - } ##[ Config files ]############################################################## echo "Copying config files (src/share/default/*) to ${config_dir_path}" @@ -155,14 +130,6 @@ cp "${script_dir_path}/src/share/default/default-blacklist" "${config_dir_path}/ cp "${script_dir_path}/src/share/default/default-whitelist" "${config_dir_path}/whitelist" cp "${script_dir_path}/src/share/default/default-sources.list" "${config_dir_path}/sources.list" -chmod -R u=rw,g=r,o=r "${config_dir_path}/*" \ - || { - printf "Couldn't set permissions for %s" "${config_dir_path}" - } -chown -R root:root "${config_dir_path}" \ - || { - printf "Couldn't set ownership of %s" "${config_dir_path}" - } echo "Adsorber files have been successfully placed onto the system." printf "\\033[1;37mTo start going (to setup the scheduler and to backup the hosts file) run 'adsorber setup'\\033[0m\\n" diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 32b0174..3bc3928 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -114,16 +114,18 @@ printf "\\nRunning 'adsorber remove -y' ...\\n" printf "\\033[0;93mSomething went wrong at running Adsorber's own removal action.\\nNo worries, I can handle it ...\\n\\033[0m" echo "Maybe Adsorber has been already removed ?" - printf "Trying portable_adsorber.sh ... " + printf "\\nTrying portable_adsorber.sh ... " if "${script_dir_path}/portable_adsorber.sh" "remove" "-y" 2>/dev/null 1>&2; then - printf "found" + printf "found\\n" + printf "Removed successfully Adsorber" else + printf "no luck" "${script_dir_path}/misc/clean.sh" 2>/dev/null 1>&2 fi - printf "\n\n" + printf "\\n\\n" # Doing it the hard way .., removing everything manually rm "${systemd_dir_path}/adsorber.timer" 2>/dev/null && echo "Removed ${systemd_dir_path}/adsorber.timer" diff --git a/src/bin/adsorber b/src/bin/adsorber index dc522ee..afb9eb0 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -42,6 +42,9 @@ checkRoot() exit 126 fi + checkPaths + sourceFiles + return 0 } @@ -334,8 +337,6 @@ sourceFiles() return 0 } -checkPaths -sourceFiles for _option in "${@}"; do diff --git a/src/lib/config.sh b/src/lib/config.sh index 45c00ea..0d047df 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -52,16 +52,7 @@ config_CopySourceList() # Create sources.list if not found. Adsorber need sources.list to know from where to fetch hosts domains from if [ ! -f "${config_dir_path}/sources.list" ] || [ ! -s "${config_dir_path}/sources.list" ]; then cp "${shareable_dir_path}/default/default-sources.list" "${config_dir_path}/sources.list" \ - && echo "${prefix_warning}Created sources.list: to add new host sources edit this file." - - chown root:root -R "${config_dir_path}/sources.list" \ - || { - printf "%bCouldn't set ownership of %s\\n" "${prefix_warning}" "${config_dir_path}" - } - chmod u=rwx,g=rx,o=r -R "${config_dir_path}/sources.list" \ - || { - printf "%bCouldn't set permissions for %s\\n" "${prefix_warning}" "${config_dir_path}" - } + && echo "${prefix_warning}Created sources.list - to add new host sources edit this file." fi return 0 @@ -73,16 +64,7 @@ config_CopyWhiteList() # Create whitelist if not found, used to whitelist domains which should be excluded from blocking if [ ! -f "${config_dir_path}/whitelist" ] || [ ! -s "${config_dir_path}/whitelist" ]; then cp "${shareable_dir_path}/default/default-whitelist" "${config_dir_path}/whitelist" \ - && echo "${prefix_warning}Created whitelist: to allow specific domains edit this file." - - chown root:root -R "${config_dir_path}/whitelist" \ - || { - printf "%bCouldn't set ownership of %s\\n" "${prefix_warning}" "${config_dir_path}" - } - chmod u=rwx,g=rx,o=r -R "${config_dir_path}/whitelist" \ - || { - printf "%bCouldn't set permissions for %s\\n" "${prefix_warning}" "${config_dir_path}" - } + && echo "${prefix_warning}Created whitelist - to allow specific domains edit this file." fi return 0 @@ -94,16 +76,7 @@ config_CopyBlackList() # Create blacklist if not found, used to explicitly block domains if [ ! -f "${config_dir_path}/blacklist" ] || [ ! -s "${config_dir_path}/blacklist" ]; then cp "${shareable_dir_path}/default/default-blacklist" "${config_dir_path}/blacklist" \ - && echo "${prefix_warning}Created blacklist: to block additional domains edit this file." - - chown root:root -R "${config_dir_path}/blacklist" \ - || { - printf "%bCouldn't set ownership of %s\\n" "${prefix_warning}" "${config_dir_path}" - } - chmod u=rwx,g=rx,o=r -R "${config_dir_path}/blacklist" \ - || { - printf "%bCouldn't set permissions for %s\\n" "${prefix_warning}" "${config_dir_path}" - } + && echo "${prefix_warning}Created blacklist - to block additional domains edit this file." fi return 0 @@ -120,14 +93,6 @@ config_CopyConfig() sed "s|@.*|# Config file for Adsorber v${version}|g" "${shareable_dir_path}/default/default-adsorber.conf" \ > "${config_dir_path}/adsorber.conf" - chown root:root -R "${config_dir_path}/adsorber.conf" \ - || { - printf "%bCouldn't set ownership of %s\\n" "${prefix_warning}" "${config_dir_path}" - } - chmod u=rwx,g=rx,o=r -R "${config_dir_path}/adsorber.conf" \ - || { - printf "%bCouldn't set permissions for %s\\n" "${prefix_warning}" "${config_dir_path}" - } exit 126 fi From d07579904f13bd249083d08cac5e4d2eef171bad Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 00:25:04 +0100 Subject: [PATCH 25/43] Fixed restore doesn't create a 'previous' backup --- src/bin/adsorber | 2 +- src/lib/restore.sh | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin/adsorber b/src/bin/adsorber index afb9eb0..243b852 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -194,7 +194,7 @@ showSpecificHelp() echo "For this reason this command is 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 update'" + echo "To re-apply run 'adsorber revert' (for last version) or 'adsorber update' (for updated version)" echo echo "Possible option:" echo " -h, --help - show this help screen" diff --git a/src/lib/restore.sh b/src/lib/restore.sh index 36acade..3b2ba93 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -21,15 +21,17 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# remove_CleanUp src/lib/remove.sh -# remove_ErrorCleanUp src/lib/remove.sh +# ---function:------------ ---function defined in:--- +# remove_CleanUp src/lib/remove.sh +# remove_ErrorCleanUp src/lib/remove.sh +# update_PreviousHostsFile src/lib/update.sh # shellcheck disable=SC2154 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}" \ || { From ada0f9cab466e7168bdaf1f2c938b76155293e81 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 00:25:45 +0100 Subject: [PATCH 26/43] Fix main execution script not runnable --- place_files_onto_system.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index d599ca0..8f418da 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -103,6 +103,8 @@ sed "s|^readonly library_dir_path=\"\${executable_dir_path}/\\.\\./lib/\"$|reado | sed "s|^readonly config_dir_path=\"\${executable_dir_path}/\\.\\./\\.\\./\"$|readonly config_dir_path=\"${config_dir_path}\"|g" \ > "${executable_path}" +chmod a+x "${executable_path}" + ##[ Libraries ]################################################################# echo "Placing other executables (src/lib/*) to ${library_dir_path}" From ff603853224e07a2785a60c73cb54a553b44ec20 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 00:39:25 +0100 Subject: [PATCH 27/43] Update description/manual on README.md --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3f6daf7..7fe536b 100644 --- a/README.md +++ b/README.md @@ -34,15 +34,20 @@ the following requirements should have been fulfilled by default: ## Usage -### First steps .. installation to system or portable mode -Adsorber can be installed into the system (recommended) for a more stable and robust usage. -This can be achieved through running `install_to_system.sh`, files will placed to `/usr/local/` if not other specified. -If you don't want Adsorber to be installed into your system run `portable_adsorber.sh`. +### First steps .. placing files to system or portable mode +Adsorber can be placed into the system (recommended) for a more stable and robust usage. +This can be achieved through running `place_files_onto_system.sh`, the files will placed to `/usr/local/` if not other specified. +If you don't want Adsorber to be placed into your system run `portable_adsorber.sh`. -Installing Adsorber into the system has the advantage that Adsorber can run independently from the user who downloaded it. +Placing Adsorber into the system has the advantage that Adsorber can run independently from the user who downloaded it. Also it prevents broken cronjobs/services as there is no risk that the files/directory of Adsorber will be accidentally deleted or moved. -To configure where it should be installed (to fit your system), edit the relevant lines in `install_to_system.sh` and `remove_from_system.sh`. For more help about the specific files run them with `--help`. +To configure where it should be installed (to fit your system), edit the relevant lines in `place_files_onto_system.sh` and `remove_files_from_system.sh`. +For more help about the specific scripts run them with `--help`. + +1. run `place_files_onto_system.sh` as `root` user +2. run `adsorber setup` as `root` user and answer all prompts by your preference, or just hit all the time `ENTER` (default options will be used). +3. You've made it! ### Default help screen of `adsorber help` ``` From bf16dcbbc55ec125fcfe7757bf524945bf56d4bb Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 09:23:20 +0100 Subject: [PATCH 28/43] Cronjob now writes to syslog --- src/lib/cron/80adsorber | 2 +- src/lib/cron/cron.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/cron/80adsorber b/src/lib/cron/80adsorber index 32b4f19..d41f5a6 100755 --- a/src/lib/cron/80adsorber +++ b/src/lib/cron/80adsorber @@ -10,4 +10,4 @@ # By default this file is going to be copied into /etc/cron.weekly/ if not # other specified. -#@/some/path/adsorber update@# +#@/some/path/adsorber update@# 2>&1 | logger -i -e -t Adsorber diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index b75c3a4..1f559a3 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -45,7 +45,7 @@ crontabSetup() # Replace the @ place holder line with the location of adsorber in 80adsorber # and copy and manipulate the content to crontabs directory sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/80adsorber" \ - | sed "s|#@\\/some\\/path\\/adsorber update@#$|${executable_dir_path}\\/adsorber update|g" \ + | sed "s|#@\\/some\\/path\\/adsorber update@#|${executable_dir_path}\\/adsorber update|g" \ | sed "s/#@frequency@#/${frequency_string}/g" \ > "${crontab_dir_path}/80adsorber" From 3f11ab8535c1e213158a681f82de4b2e528959e8 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 13:09:41 +0100 Subject: [PATCH 29/43] Added option to disable formatted output This is needed for syslog as it wil show ASCII control codes but not colours. --- src/bin/adsorber | 20 ++++-- src/lib/colours.sh | 158 ++++++++++++++++++++++++------------------- src/lib/config.sh | 1 + src/lib/cron/cron.sh | 2 +- 4 files changed, 103 insertions(+), 78 deletions(-) diff --git a/src/bin/adsorber b/src/bin/adsorber index 243b852..dcbd8af 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -25,7 +25,7 @@ readonly shareable_dir_path="${executable_dir_path}/../share/" readonly config_dir_path="${executable_dir_path}/../../" -readonly version="0.5.0" +readonly version="0.5.1" readonly operation="${1}" if [ "${#}" -ne 0 ]; then @@ -108,6 +108,7 @@ showHelp() 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 specifc options (use with 'setup'):" echo " -ns, --no-scheduler - skip scheduler creation" @@ -258,8 +259,8 @@ duplicateOption() echo "Adsorber: Duplicate option for scheduler: '${2}'" 1>&2 echo "You may only select one:" echo " -s, --systemd - use Systemd ..." - echo " -c, --cron - use Cronjob as scheduler (use with 'setup')" - echo " -ns, --no-scheduler - skip scheduler creation (use with 'setup')" + echo " -c, --cron - use Cronjob as scheduler" + echo " -ns, --no-scheduler - skip scheduler creation" elif [ "${1}" = "frequency" ]; then echo "Adsorber: Duplicate option for schedulers frequency: '${2}'" 1>&2 echo "You may only select one:" @@ -313,6 +314,8 @@ checkPaths() sourceFiles() { + # shellcheck source=../src/lib/colours.sh + . "${library_dir_path}/colours.sh" # shellcheck source=../src/lib/setup.sh . "${library_dir_path}/setup.sh" # shellcheck source=../src/lib/remove.sh @@ -325,8 +328,6 @@ sourceFiles() . "${library_dir_path}/revert.sh" # shellcheck source=../src/lib/config.sh . "${library_dir_path}/config.sh" - # shellcheck source=../src/lib/colours.sh - . "${library_dir_path}/colours.sh" # Maybe source them only when needed? # shellcheck source=../src/lib/cron/cron.sh @@ -428,6 +429,13 @@ for _option in "${@}"; do -h | --help | help ) _option_help="true" ;; + --noformatting ) + if [ -z "${use_formatting}" ]; then + readonly use_formatting="false" + else + echo "Can't turn off colour, variable'already set." 1>&2 + fi + ;; * ) _wrong_option="${_option}" 2>/dev/null ;; @@ -482,6 +490,6 @@ case "${operation}" in ;; esac -printf "\\033[1;37mFinished successfully.\\033[0m\\n" +printf "%bFinished successfully.%b\\n" "${prefix_title}" "${prefix_reset}" exit 0 diff --git a/src/lib/colours.sh b/src/lib/colours.sh index ea42cc3..1a1b0ae 100755 --- a/src/lib/colours.sh +++ b/src/lib/colours.sh @@ -9,81 +9,97 @@ # _extra_under_score - used for local function variables. Should be unset afterwards. # (Note the underscore in the beginning of _extra_under_score!) -readonly prefix=" " -readonly prefix_fatal="\\033[0;91mE " # 'E' in intensity red -readonly prefix_info="\\033[0;97m " # Intensity white -readonly prefix_input="> " -readonly prefix_title="\\033[1;37m" # Bold white -readonly prefix_warning="- " -readonly prefix_reset="\\033[0m" # Default colour -readonly prefix_underline="\\033[4;37m" # White Underline +# The following variables are declared globally. +# If you run this file independently following variables need to be set: +# ---variable:- --default value:-- ---declared in:---- +# use_formatted true src/bin/adsorber, src/lib/cron/80adsorber, src/lib/systemd/systemd.service -# 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 +if [ "${use_formatting}" != "false" ]; then + readonly prefix=" " + readonly prefix_fatal="\\033[0;91mE " # 'E' in intensity red + readonly prefix_info="\\033[0;97m " # Intensity white + readonly prefix_input="> " + readonly prefix_title="\\033[1;37m" # Bold white + readonly prefix_warning="- " + readonly prefix_reset="\\033[0m" # Default colour + readonly prefix_underline="\\033[4;37m" # White Underline -# 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 + # 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 -# 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 + # 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 -# 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 + # 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 -# 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 + # 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 -# 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 + #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 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 + # 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 +else + 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 0d047df..fbcd894 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -366,6 +366,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}" } diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index 1f559a3..2a062a3 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -45,7 +45,7 @@ crontabSetup() # Replace the @ place holder line with the location of adsorber in 80adsorber # and copy and manipulate the content to crontabs directory sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/80adsorber" \ - | sed "s|#@\\/some\\/path\\/adsorber update@#|${executable_dir_path}\\/adsorber update|g" \ + | sed "s|#@\\/some\\/path\\/adsorber update@#|${executable_dir_path}\\/adsorber update --noformatting|g" \ | sed "s/#@frequency@#/${frequency_string}/g" \ > "${crontab_dir_path}/80adsorber" From e2f83b30d4197afba5c6f67356dd6096c5dadd12 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 13:12:50 +0100 Subject: [PATCH 30/43] Improved understanding of messages send to user --- src/lib/restore.sh | 2 +- src/lib/revert.sh | 2 +- src/share/components/hosts_title | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/restore.sh b/src/lib/restore.sh index 3b2ba93..a7c9b0d 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -44,7 +44,7 @@ restore_HostsFile() 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 hosts file. Original hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bCan't restore original hosts file. Original hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 remove_ErrorCleanUp exit 1 fi diff --git a/src/lib/revert.sh b/src/lib/revert.sh index 46f8def..4f000df 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -44,7 +44,7 @@ revert_HostsFile() 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 hosts file. Previous hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 + printf "%bCan't revert to previous hosts file. Previous hosts file does not exist.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 remove_ErrorCleanUp exit 1 fi diff --git a/src/share/components/hosts_title b/src/share/components/hosts_title index 92ce02c..8587949 100644 --- a/src/share/components/hosts_title +++ b/src/share/components/hosts_title @@ -1,2 +1,2 @@ -# Following #@blocked@# domain(s) were inserted by Adsorber and are blocking advertisement: +# Following #@blocked@# domain(s) were inserted by Adsorber and are being blocked: From 7796252991cc9c52e07600217cdd54ef81d61e12 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 14:37:49 +0100 Subject: [PATCH 31/43] Added systemd syslogging capability --- src/lib/systemd/adsorber.service | 9 ++++++++- src/lib/systemd/adsorber.timer | 2 +- src/lib/systemd/systemd.sh | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lib/systemd/adsorber.service b/src/lib/systemd/adsorber.service index da5691b..a16bbde 100644 --- a/src/lib/systemd/adsorber.service +++ b/src/lib/systemd/adsorber.service @@ -1,5 +1,5 @@ [Unit] -Description=Adsorber service to update the hosts file #@frequency@#. +Description=Adsorber #@frequency@# service Documentation=https://github.com/stablestud/adsorber After=network.target @@ -7,3 +7,10 @@ After=network.target Type=oneshot User=root ExecStart=#@/some/path/adsorber update@# +StandardInput=null +StandardOutput=syslog+console +StandardError=syslog+console +SyslogIdentifier=Adsorber +ProtectKernelTunables=true +ProtectKernelModules=true +ProtectControlGroups=true diff --git a/src/lib/systemd/adsorber.timer b/src/lib/systemd/adsorber.timer index d288538..f2067af 100644 --- a/src/lib/systemd/adsorber.timer +++ b/src/lib/systemd/adsorber.timer @@ -1,5 +1,5 @@ [Unit] -Description=Timer that runs the Adsorber service #@frequency@#. +Description=Timer that runs the Adsorber service #@frequency@# Documentation=https://github.com/stablestud/adsorber Requires=network-online.target diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index 931c20b..92a2f40 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -55,7 +55,7 @@ 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|g" "${library_dir_path}/systemd/adsorber.service" \ + sed "s|#@\\/some\\/path\\/adsorber update@#|${executable_dir_path}\\/adsorber update --noformatting|g" "${library_dir_path}/systemd/adsorber.service" \ | sed "s/#@frequency@#/${frequency}/g" \ > "${systemd_dir_path}/adsorber.service" From 9c0e4370663c63fec201fd63b7eec6281be20530 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 14:46:55 +0100 Subject: [PATCH 32/43] Fixed abort after setup with no inet connection --- src/lib/systemd/adsorber.service | 2 +- src/lib/update.sh | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib/systemd/adsorber.service b/src/lib/systemd/adsorber.service index a16bbde..a732d8f 100644 --- a/src/lib/systemd/adsorber.service +++ b/src/lib/systemd/adsorber.service @@ -1,5 +1,5 @@ [Unit] -Description=Adsorber #@frequency@# service +Description=#@frequency@# adsorber service Documentation=https://github.com/stablestud/adsorber After=network.target diff --git a/src/lib/update.sh b/src/lib/update.sh index 64988f1..388e924 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -210,7 +210,7 @@ update_FetchSources() elif [ "${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 - remove_ErrorCleanUp + remove_ErrorCleanUp exit 1 else printf "%bSuccessfully fetched %d out of %d hosts sources.%b\\n" "${prefix_info}" "${_successful_count}" "${_total_count}" "${prefix_reset}" @@ -324,7 +324,7 @@ update_IsCacheEmpty() { if [ ! -s "${tmp_dir_path}/cache" ]; then printf "%bNothing to apply.\\n" "${prefix_warning}" 1>&2 - remove_ErrorCleanUp + remove_CleanUp exit 1 fi @@ -423,11 +423,14 @@ update() update_ReadWhiteList if update_ReadSourceList; then - update_FetchSources - update_FilterDomains "fetched" "fetched-filtered" - update_SortDomains "fetched-filtered" "fetched-sorted" - - cp "${tmp_dir_path}/fetched-sorted" "${tmp_dir_path}/cache" + 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 else # Create empty cache file for the ad-domains. touch "${tmp_dir_path}/cache" From ac22ab238d0bb71a7bedcb7ad72808a1b27f2404 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 15:11:54 +0100 Subject: [PATCH 33/43] Renamed default scheduler files to a proper name --- src/lib/cron/cron.sh | 2 +- src/lib/cron/{80adsorber => default-cronjob.sh} | 0 src/lib/systemd/{adsorber.service => default-service} | 0 src/lib/systemd/{adsorber.timer => default-timer} | 0 src/lib/systemd/systemd.sh | 6 +++--- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/lib/cron/{80adsorber => default-cronjob.sh} (100%) rename src/lib/systemd/{adsorber.service => default-service} (100%) rename src/lib/systemd/{adsorber.timer => default-timer} (100%) diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index 2a062a3..f65838f 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -44,7 +44,7 @@ crontabSetup() # Replace the @ place holder line with the location of adsorber in 80adsorber # and copy and manipulate the content to crontabs directory - sed "s|#@version@#|${version}|g" "${library_dir_path}/cron/80adsorber" \ + 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" \ > "${crontab_dir_path}/80adsorber" diff --git a/src/lib/cron/80adsorber b/src/lib/cron/default-cronjob.sh similarity index 100% rename from src/lib/cron/80adsorber rename to src/lib/cron/default-cronjob.sh diff --git a/src/lib/systemd/adsorber.service b/src/lib/systemd/default-service similarity index 100% rename from src/lib/systemd/adsorber.service rename to src/lib/systemd/default-service diff --git a/src/lib/systemd/adsorber.timer b/src/lib/systemd/default-timer similarity index 100% rename from src/lib/systemd/adsorber.timer rename to src/lib/systemd/default-timer diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index 92a2f40..d5220b1 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -55,15 +55,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/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" \ > "${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/adsorber.timer" \ + sed "s/#@frequency@#/${frequency}/g" "${library_dir_path}/systemd/default-timer" \ > "${systemd_dir_path}/adsorber.timer" - chmod u=rwx,g=rx,o=rx "${systemd_dir_path}/adsorber.service" "${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" # Enable the systemd service and enable it to start at boot-up From 62ccfe97d000ea5cd351972cf4df5ab3c24ba486 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 17:34:23 +0100 Subject: [PATCH 34/43] Fixed tiny message formatting issue --- place_files_onto_system.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index 8f418da..81a46d4 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -133,14 +133,14 @@ cp "${script_dir_path}/src/share/default/default-whitelist" "${config_dir_path}/ cp "${script_dir_path}/src/share/default/default-sources.list" "${config_dir_path}/sources.list" +echo echo "Adsorber files have been successfully placed onto the system." printf "\\033[1;37mTo start going (to setup the scheduler and to backup the hosts file) run 'adsorber setup'\\033[0m\\n" -echo ## We don't run Adsorber after installation yet #adsorber setup \ # || { -# printf "\033[0;93mAdsorber has been placed onto your system, however something went wrong at\\n" +# printf "\\n\033[0;93mAdsorber has been placed onto your system, however something went wrong at\\n" # printf "running it.\\n" # printf "If a proxy server is in use, please change the config file\\n" # printf "(${config_dir_path}/adsorber.conf) to the appropriate proxy server.\\n\033[0m" From 1e8888dda843a69aad7d102df22739473b95d82f Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 17:54:02 +0100 Subject: [PATCH 35/43] Removal script now checks for adsorber command --- place_files_onto_system.sh | 2 +- remove_files_from_system.sh | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index 81a46d4..b9893a0 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -138,7 +138,7 @@ echo "Adsorber files have been successfully placed onto the system." printf "\\033[1;37mTo start going (to setup the scheduler and to backup the hosts file) run 'adsorber setup'\\033[0m\\n" ## We don't run Adsorber after installation yet -#adsorber setup \ +#adsorber setup --noformatting \ # || { # printf "\\n\033[0;93mAdsorber has been placed onto your system, however something went wrong at\\n" # printf "running it.\\n" diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 3bc3928..f70d1a5 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -107,27 +107,32 @@ if [ "$(id -g)" -ne 0 ]; then fi # Run Adsorber's own removal, if it fails do it manually -printf "\\nRunning 'adsorber remove -y' ...\\n" -( adsorber remove -y ) \ - || { - echo - printf "\\033[0;93mSomething went wrong at running Adsorber's own removal action.\\nNo worries, I can handle it ...\\n\\033[0m" - echo "Maybe Adsorber has been already removed ?" +if [ command -v adsorber ]; then + printf "\\nRunning 'adsorber remove -y --noformatting' ...\\n" + ( adsorber "remove" "-y" "--noformatting" ) \ + || { + echo + printf "\\033[0;93mSomething went wrong at running Adsorber's own removal action.\\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" +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" "remove" "-y" 2>/dev/null 1>&2; then + if "${script_dir_path}/portable_adsorber.sh" "remove" "-y" "--noformatting" 2>/dev/null 1>&2; then printf "found\\n" - printf "Removed successfully Adsorber" + printf "Removed successfully Adsorber\\n" else - printf "no luck" + printf "no luck\\n" "${script_dir_path}/misc/clean.sh" 2>/dev/null 1>&2 fi - printf "\\n\\n" - - # Doing it the hard way .., removing everything manually 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" @@ -163,7 +168,7 @@ printf "\\nRunning 'adsorber remove -y' ...\\n" fi rm "${hosts_file_previous_path}" 2>/dev/null && echo "Removed ${hosts_file_previous_path}" - } +fi echo From bb2ae719f4aa10ee0113f19f57a648af76313c71 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 18:57:32 +0100 Subject: [PATCH 36/43] Cleanup functions moved to there own file --- remove_files_from_system.sh | 2 +- src/bin/adsorber | 2 ++ src/lib/cleanup.sh | 68 +++++++++++++++++++++++++++++++++++++ src/lib/config.sh | 12 +++---- src/lib/cron/cron.sh | 12 +++---- src/lib/remove.sh | 55 ++++-------------------------- src/lib/restore.sh | 10 +++--- src/lib/revert.sh | 12 +++---- src/lib/setup.sh | 10 +++--- src/lib/systemd/systemd.sh | 12 +++---- src/lib/update.sh | 22 ++++++------ 11 files changed, 122 insertions(+), 95 deletions(-) create mode 100755 src/lib/cleanup.sh diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index f70d1a5..1095f65 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -107,7 +107,7 @@ if [ "$(id -g)" -ne 0 ]; then fi # Run Adsorber's own removal, if it fails do it manually -if [ command -v adsorber ]; then +if command -v adsorber; then printf "\\nRunning 'adsorber remove -y --noformatting' ...\\n" ( adsorber "remove" "-y" "--noformatting" ) \ || { diff --git a/src/bin/adsorber b/src/bin/adsorber index dcbd8af..be41004 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -316,6 +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/setup.sh . "${library_dir_path}/setup.sh" # shellcheck source=../src/lib/remove.sh diff --git a/src/lib/cleanup.sh b/src/lib/cleanup.sh new file mode 100755 index 0000000..68739f0 --- /dev/null +++ b/src/lib/cleanup.sh @@ -0,0 +1,68 @@ +#!/bin/sh + +# Author: stablestud +# Repository: https://github.com/stablestud/adsorber +# License: MIT, https://opensource.org/licenses/MIT + +# Variable naming: +# under_score - used for global variables which are accessible between functions. +# _extra_under_score - used for local function variables. Should be unset afterwards. +# (Note the underscore in the beginning of _extra_under_score!) + +# The following variables are declared globally. +# If you run this file independently following variables need to be set: +# ---variable:---------- ---default value:-- ---defined in:------------------- +# backedup Null (not set) src/lib/setup.sh +# hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf +# setup_scheduler Null (not set) src/lib/systemd/systemd.sh, src/lib/cron/cron.sh +# prefix ' ' (two spaces) src/lib/colours.sh +# tmp_dir_path /tmp/adsorber src/bin/adsorber + +# The following functions are defined in different files. +# If you run this file independently following functions need to be emulated: +# ---function:- ---function defined in:--- +# systemdRemove src/lib/systemd/systemd.sh +# crontabRemove src/lib/cron/cron.sh + +# shellcheck disable=SC2154 + +# This function cleans-up all changed files if Adsorber runs into a problem +errorCleanUp() +{ + printf "%bCleaning up ...\\n" "${prefix_warning}" + + # Remove scheduler if it was setup (created) in the same run + case "${setup_scheduler}" in + cronjob ) + crontabRemove + ;; + systemd ) + systemdRemove + ;; + esac + + # Remove backup if backed-up in the same run + if [ "${backedup}" = "true" ]; then + echo "${prefix}Removed backup as the setup failed." + rm "${hosts_file_backup_path}" + fi + + # Remove /tmp/adsorber directory + if [ -d "${tmp_dir_path}" ]; then + rm -r "${tmp_dir_path}" + fi + + return 0 +} + + +# Normal clean-up function +cleanUp() +{ + echo "${prefix}Cleaning up ..." + + # Remove the /tmp/adsorber directory + rm -r "${tmp_dir_path}" + + return 0 +} diff --git a/src/lib/config.sh b/src/lib/config.sh index fbcd894..708d89b 100755 --- a/src/lib/config.sh +++ b/src/lib/config.sh @@ -26,8 +26,8 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# remove_ErrorCleanUp src/lib/remove.sh +# --function:-- --function defined in:-- +# errorCleanUp src/lib/cleanup.sh # shellcheck disable=SC2154 @@ -106,7 +106,7 @@ config_FilterConfig() cp "${config_dir_path}/adsorber.conf" "${tmp_dir_path}/config" \ || { printf "%bCouldn't process config file.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 126 } @@ -215,7 +215,7 @@ config_ReadConfig() "${prefix_fatal}" "${_line}" "${prefix_reset}" echo "Please report this error with your config file to https://github.com/stablestud/adsorber" - remove_ErrorCleanUp + errorCleanUp exit 1 ;; esac @@ -267,7 +267,7 @@ config_IsVariableSet() 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 - remove_ErrorCleanUp + errorCleanUp exit 127 fi @@ -341,7 +341,7 @@ config_IsVariableValid() # If one or more values were invalid exit with error message if [ "${wrongVariable}" = "true" ]; then - remove_ErrorCleanUp + errorCleanUp exit 126 fi } diff --git a/src/lib/cron/cron.sh b/src/lib/cron/cron.sh index f65838f..c8629cf 100755 --- a/src/lib/cron/cron.sh +++ b/src/lib/cron/cron.sh @@ -23,8 +23,8 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# remove_ErrorCleanUp src/lib/remove.sh +# --function:-- ---function defined in:--- +# errorCleanUp src/lib/cleanup.sh # shellcheck disable=SC2154 @@ -38,7 +38,7 @@ crontabSetup() "${prefix_fatal}" "${crontab_dir_path}" "${prefix_reset}" 1>&2 echo "${prefix}Is a Cron service installed? If not use Systemd if possible." - remove_ErrorCleanUp + errorCleanUp exit 126 fi @@ -53,7 +53,7 @@ crontabSetup() chown root:root "${crontab_dir_path}/80adsorber" # Make known that we have setup the Crontab in this run, - # if we fail now, Crontab will be also removed (see remove_ErrorCleanUp) + # if we fail now, Crontab will be also removed (see errorCleanUp) readonly setup_scheduler="cronjob" } @@ -94,7 +94,7 @@ crontabPromptFrequency() else printf "%bThis frequency is only available with Systemd.%b\\n" \ "${prefix_fatal}" "${prefix_reset}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 1 fi ;; @@ -106,7 +106,7 @@ crontabPromptFrequency() else printf "%bFrequency '%s' not understood.%b\\n" \ "${prefix_fatal}" "${frequency}" "${prefix_reset}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 1 fi ;; diff --git a/src/lib/remove.sh b/src/lib/remove.sh index edbbb49..15c6ce3 100755 --- a/src/lib/remove.sh +++ b/src/lib/remove.sh @@ -12,10 +12,8 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: # ---variable:---------- ---default value:-- ---defined in:------------------- -# backedup Null (not set) src/lib/setup.sh # hosts_file_path /etc/hosts src/lib/config.sh, adsorber.conf # hosts_file_backup_path /etc/hosts.original src/lib/config.sh, adsorber.conf -# setup_scheduler Null (not set) src/lib/systemd/systemd.sh, src/lib/cron/cron.sh # prefix ' ' (two spaces) src/lib/colours.sh # prefix_fatal '\033[0;91mE ' src/lib/colours.sh # prefix_input ' ' (two spaces) src/lib/colours.sh @@ -23,58 +21,17 @@ # prefix_title \033[1;37m src/lib/colours.sh # prefix_warning '- ' src/lib/colours.sh # reply_to_prompt Null (not set) src/bin/adsorber -# tmp_dir_path /tmp/adsorber src/bin/adsorber # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:- ---function defined in:--- +# --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 # shellcheck disable=SC2154 -# This function cleans-up all changed files if Adsorber runs into a problem -remove_ErrorCleanUp() -{ - printf "%bCleaning up ...\\n" "${prefix_warning}" - - # Remove scheduler if it was setup (created) in the same run - case "${setup_scheduler}" in - cronjob ) - crontabRemove - ;; - systemd ) - systemdRemove - ;; - esac - - # Remove backup if backed-up in the same run - if [ "${backedup}" = "true" ]; then - echo "${prefix}Removed backup as the setup failed." - rm "${hosts_file_backup_path}" - fi - - # Remove /tmp/adsorber directory - if [ -d "${tmp_dir_path}" ]; then - rm -r "${tmp_dir_path}" - fi - - return 0 -} - - -# Normal clean-up function -remove_CleanUp() -{ - echo "${prefix}Cleaning up ..." - - # Remove the /tmp/adsorber directory - rm -r "${tmp_dir_path}" - - return 0 -} - - remove_Prompt() { # Ask if the user is sure about to remove Adsorber @@ -90,7 +47,7 @@ remove_Prompt() * ) # If other input then Yes, abort and call error clean-up function printf "%bRemoval cancelled.\\n" "${prefix_warning}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 130 ;; esac @@ -110,7 +67,7 @@ remove_HostsFile() # 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 already removed?" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 1 fi @@ -141,7 +98,7 @@ remove() crontabRemove remove_HostsFile remove_PreviousHostsFile - remove_CleanUp + cleanUp return 0 } diff --git a/src/lib/restore.sh b/src/lib/restore.sh index a7c9b0d..ec9250e 100755 --- a/src/lib/restore.sh +++ b/src/lib/restore.sh @@ -22,8 +22,8 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: # ---function:------------ ---function defined in:--- -# remove_CleanUp src/lib/remove.sh -# remove_ErrorCleanUp src/lib/remove.sh +# cleanUp src/lib/cleanup.sh +# errorCleanUp src/lib/cleanup.sh # update_PreviousHostsFile src/lib/update.sh # shellcheck disable=SC2154 @@ -36,7 +36,7 @@ restore_HostsFile() cp "${hosts_file_backup_path}" "${hosts_file_path}" \ || { printf "%bCouldn't restore %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" - remove_ErrorCleanUp + errorCleanUp exit 1 } @@ -45,7 +45,7 @@ restore_HostsFile() 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 - remove_ErrorCleanUp + errorCleanUp exit 1 fi @@ -58,7 +58,7 @@ restore() { printf "%bRestoring %s ...%b\\n" "${prefix_title}" "${hosts_file_path}" "${prefix_reset}" restore_HostsFile - remove_CleanUp + cleanUp return 0 } diff --git a/src/lib/revert.sh b/src/lib/revert.sh index 4f000df..e73232a 100755 --- a/src/lib/revert.sh +++ b/src/lib/revert.sh @@ -21,9 +21,9 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# remove_CleanUp src/lib/remove.sh -# remove_ErrorCleanUp src/lib/remove.sh +# --function:-- ---function defined in:--- +# cleanUp src/lib/cleanup.sh +# errorCleanUp src/lib/cleanup.sh # shellcheck disable=SC2154 @@ -34,7 +34,7 @@ revert_HostsFile() cp "${hosts_file_previous_path}" "${hosts_file_path}" \ || { printf "%bCouldn't revert %s.%b" "${prefix_fatal}" "${hosts_file_path}" "${prefix_reset}" - remove_ErrorCleanUp + errorCleanUp exit 1 } @@ -45,7 +45,7 @@ revert_HostsFile() 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 - remove_ErrorCleanUp + errorCleanUp exit 1 fi @@ -58,7 +58,7 @@ revert() { printf "%bReverting %s with %s ...%b\\n" "${prefix_title}" "${hosts_file_path}" "${hosts_file_previous_path}" "${prefix_reset}" revert_HostsFile - remove_CleanUp + cleanUp return 0; } diff --git a/src/lib/setup.sh b/src/lib/setup.sh index 620da47..1784061 100755 --- a/src/lib/setup.sh +++ b/src/lib/setup.sh @@ -20,10 +20,10 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# crontab src/lib/cron/cron.sh -# remove_ErrorCleanUp src/lib/remove.sh -# systemd src/lib/systemd/systemd.sh +# --function:-- ---function defined in:--- +# crontab src/lib/cron/cron.sh +# errorCleanUp src/lib/cleanup.sh +# systemd src/lib/systemd/systemd.sh # shellcheck disable=SC2154 @@ -56,7 +56,7 @@ setup_Prompt() ;; * ) printf "%bSetup cancelled.\\n" "${prefix_warning}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 130 ;; esac diff --git a/src/lib/systemd/systemd.sh b/src/lib/systemd/systemd.sh index d5220b1..b6410b9 100755 --- a/src/lib/systemd/systemd.sh +++ b/src/lib/systemd/systemd.sh @@ -24,8 +24,8 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# remove_ErrorCleanUp src/lib/remove.sh +# --function:-- ---function defined in:--- +# errorCleanUp src/lib/remove.sh # shellcheck disable=SC2154 @@ -37,7 +37,7 @@ systemdSetup() "${prefix_fatal}" "${systemd_dir_path}" "${prefix_reset}" 1>&2 echo "${prefix}Is Systemd installed? If not use Cron instead." - remove_ErrorCleanUp + errorCleanUp exit 126 fi @@ -46,7 +46,7 @@ systemdSetup() 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 - remove_ErrorCleanUp; + errorCleanUp; exit 1; fi fi @@ -75,12 +75,12 @@ systemdSetup() 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 - remove_ErrorCleanUp + 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 remove_ErrorCleanUp) + # if we fail now, systemd will be also removed (see errorCleanUp) readonly setup_scheduler="systemd" echo "${prefix}Initialized Systemd service ..." diff --git a/src/lib/update.sh b/src/lib/update.sh index 388e924..3c35714 100755 --- a/src/lib/update.sh +++ b/src/lib/update.sh @@ -35,9 +35,9 @@ # The following functions are defined in different files. # If you run this file independently following functions need to be emulated: -# ---function:------- ---function defined in:--- -# remove_CleanUp src/lib/remove.sh -# remove_ErrorCleanUp src/lib/remove.sh +# --function:-- ---function defined in:--- +# cleanUp src/lib/cleanup.sh +# errorCleanUp src/lib/cleanup.sh # shellcheck disable=SC2154 @@ -61,7 +61,7 @@ update_CheckBackupExist() ;; * ) printf "%bAborted.\\n" "${prefix_warning}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 130 ;; esac @@ -195,7 +195,7 @@ update_FetchSources() else printf "%bNeither curl nor wget installed. Can't continue.%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 2 fi @@ -210,7 +210,7 @@ update_FetchSources() elif [ "${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 - remove_ErrorCleanUp + errorCleanUp exit 1 else printf "%bSuccessfully fetched %d out of %d hosts sources.%b\\n" "${prefix_info}" "${_successful_count}" "${_total_count}" "${prefix_reset}" @@ -324,7 +324,7 @@ update_IsCacheEmpty() { if [ ! -s "${tmp_dir_path}/cache" ]; then printf "%bNothing to apply.\\n" "${prefix_warning}" 1>&2 - remove_CleanUp + cleanUp exit 1 fi @@ -381,7 +381,7 @@ update_PreviousHostsFile() 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}" - remove_ErrorCleanUp + errorCleanUp exit 1 } @@ -401,7 +401,7 @@ update_ApplyHostsFile() cp "${tmp_dir_path}/hosts" "${hosts_file_path}" \ || { printf "%b" "${prefix_fatal}Couldn't apply hosts file. Aborting.${prefix_reset}\\n" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 126 } @@ -447,7 +447,7 @@ update() ;; * ) printf "%bWrong primary_list set in adsorber.conf. Choose either 'whitelist' or 'blacklist'%b\\n" "${prefix_fatal}" "${prefix_reset}" 1>&2 - remove_ErrorCleanUp + errorCleanUp exit 127 ;; esac @@ -456,7 +456,7 @@ update() update_BuildHostsFile update_PreviousHostsFile update_ApplyHostsFile - remove_CleanUp + cleanUp return 0 } From 8ebd030e7b38a228b187737a1ba4b9b4927bd3b7 Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 19:38:08 +0100 Subject: [PATCH 37/43] Operation 'remove' is now 'disable' I changed the names to make it more clear that Adsorber command don't remove but rather disable itself. However the 'remove_files_from_system.sh' script does remove Adsorber. --- README.md | 49 ++++++++++++++++--------- remove_files_from_system.sh | 10 ++--- src/bin/adsorber | 29 ++++++++------- src/lib/colours.sh | 4 +- src/lib/{remove.sh => disable.sh} | 24 ++++++------ src/lib/systemd/default-service | 2 +- src/share/default/default-adsorber.conf | 2 +- 7 files changed, 67 insertions(+), 53 deletions(-) rename src/lib/{remove.sh => disable.sh} (86%) diff --git a/README.md b/README.md index 7fe536b..6bad1bf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Adsorber -[![Latest version](https://img.shields.io/badge/latest-v0.5.0-brightgreen.svg)](https://github.com/stablestud/adsorber/releases) +[![Latest version](https://img.shields.io/badge/latest-v0.5.2-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) @@ -14,9 +14,9 @@ Technically speaking, it adds ad-domains to the hosts file `/etc/hosts` with a r * Revert to the previous hosts-file if the current hosts-file contains broken ad-servers * White- and blacklist. -Also it saves data, speeds up loading time and prevents some tracking of your browsing habits. For extensive privacy, I recommend using the script along browser add-ons like [NoScript](https://addons.mozilla.org/en-US/firefox/addon/noscript/) (for [Firefox 56 and below](https://noscript.net/getit)), [Privacy Badger](https://addons.mozilla.org/en-US/firefox/addon/privacy-badger17/) and [HTTPS Everywhere](https://addons.mozilla.org/en-US/firefox/addon/https-everywhere/). +Also it saves data, speeds up loading time and prevents some tracking of your browsing habits. For extensive privacy, I recommend using the script along browser add-ons like [NoScript](https://addons.mozilla.org/en-US/firefox/addon/noscript/) (for [Firefox 56 and below](https://noscript.net/getit)) or [uMatrix](https://addons.mozilla.org/en-US/firefox/addon/umatrix/), [Privacy Badger](https://addons.mozilla.org/en-US/firefox/addon/privacy-badger17/) and [HTTPS Everywhere](https://addons.mozilla.org/en-US/firefox/addon/https-everywhere/). -Currently we are using the following hosts lists: +By default Adsorber uses the following curated ad-hosts sources: * [adaway.org](https://adaway.org/hosts.txt) * [yoyo.org](https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext) * & more to come. @@ -51,34 +51,47 @@ For more help about the specific scripts run them with `--help`. ### Default help screen of `adsorber help` ``` -Usage: adsorber [] +Usage: adsorber [|--help] -(Ad)sorber blocks ads by 'absorbing' and dumbing them into void. +(Ad)sorber blocks ads by "absorbing" and dumbing them into void. (with the help of the hosts file) -Operations: +Operations (required): setup - setup necessary things needed for Adsorber e.g., create backup file of hosts file, create scheduler which updates the host file once a week However this should've been done automatically. update - update hosts file with newest ad servers restore - restore hosts file to its original state - (it does not remove the schedule, this should be used temporary) - revert - reverts the hosts file to the last applied host file. - remove - completely remove changes made by Adsorber - e.g., remove scheduler (if set) + (it does not remove the scheduler, this should be used temporary) + revert - reverts the hosts file to the last applied (previous) host file. + disable - completely remove changes made by Adsorber + e.g., disable scheduler (if set) restore hosts file to its original state version - show version of this shell script help - show this help -Options: (optional) - -s, --systemd - use Systemd ... - -c, --cron - use Cronjob as scheduler (use with 'setup') - -ns, --no-scheduler - skip scheduler creation (use with 'setup') +Options (optional): -y, --yes, --assume-yes - answer all prompts with 'yes' -f, --force - force the update if no /etc/hosts backup has been created (dangerous) -h, --help - show specific help of specified operations + (e.g 'adsorber update --help) + --noformatting - turn off coloured and formatted output + +Scheduler specifc options (use with 'setup'): + -ns, --no-scheduler - skip scheduler creation + -s, --systemd - use Systemd ... + -c, --cron - use Cronjob as scheduler + -H, --hourly - run scheduler once hourly + -D, --daily ... daily + -W, --weekly ... weekly + -M, --monthly ... monthly + -Q, --quarterly ... quarterly (4x a year) + -S, --semiannually ... semiannually (2x a year) + -Y, --yearly ... yearly + +Config files are located at: /usr/local/etc/adsorber/ Documentation: https://github.com/stablestud/adsorber If you encounter any issues please report them to the Github repository. @@ -138,11 +151,11 @@ To re-apply run `adsorber update` Possible option: * `-h, --help` -#### `adsorber remove {options}`: -To completely remove changes made by Adsorber. +#### `adsorber disable {options}`: +Completely remove changes made by Adsorber and disable all its background tasks (schedulers). The command will: -* remove all schedulers (systemd, cronjob) +* disable all schedulers (systemd, cronjob) * restore the hosts file to it's original state * remove all leftovers (previous hosts-file, etc) @@ -169,7 +182,7 @@ Answers all prompts with `yes` e.g., It'll not answer prompts which may harm your system. But `--force` will do it. #### `-f, --force`: This will force the script to continue (dangerous) the update e.g., -* Continue if no backup has been created, overwriting the existing hosts file. +* Continues if no backup has been created, overwriting the existing hosts file. #### `-h, --help`: If specified in conjunction with an operation, it'll show extended help about the operation. diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 1095f65..3ec90f0 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -86,7 +86,7 @@ echo # Prompt user if sure about to remove Adsorber from the system if [ -z "${prompt}" ]; then - printf "Are you sure you want to remove Adsorber (see above) 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 @@ -107,9 +107,9 @@ if [ "$(id -g)" -ne 0 ]; then fi # Run Adsorber's own removal, if it fails do it manually -if command -v adsorber; then - printf "\\nRunning 'adsorber remove -y --noformatting' ...\\n" - ( adsorber "remove" "-y" "--noformatting" ) \ +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 removal action.\\nNo worries, I can handle it ...\\n\\033[0m" @@ -124,7 +124,7 @@ fi if [ "${_hard_way}" = "true" ]; then printf "\\nTrying portable_adsorber.sh ... " - if "${script_dir_path}/portable_adsorber.sh" "remove" "-y" "--noformatting" 2>/dev/null 1>&2; then + if "${script_dir_path}/portable_adsorber.sh" "disable" "-y" "--noformatting" 2>/dev/null 1>&2; then printf "found\\n" printf "Removed successfully Adsorber\\n" else diff --git a/src/bin/adsorber b/src/bin/adsorber index be41004..e7c1504 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -25,7 +25,7 @@ readonly shareable_dir_path="${executable_dir_path}/../share/" readonly config_dir_path="${executable_dir_path}/../../" -readonly version="0.5.1" +readonly version="0.5.2" readonly operation="${1}" if [ "${#}" -ne 0 ]; then @@ -73,8 +73,8 @@ showUsage() echo "Adsorber: Invalid option: '${_wrong_option}'" 1>&2 fi - echo "Usage: adsorber []" - echo "Try '--help' for more information." + echo "Usage: adsorber [|--help]" + echo "Try 'help' for more information." exit 80 } @@ -82,7 +82,7 @@ showUsage() showHelp() { - echo "Usage: adsorber []" + echo "Usage: adsorber [|--help]" echo "" echo "(Ad)sorber blocks ads by \"absorbing\" and dumbing them into void." echo " (with the help of the hosts file)" @@ -96,8 +96,8 @@ showHelp() 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 " remove - completely remove changes made by Adsorber" - echo " e.g., remove scheduler (if set)" + echo " disable - completely remove changes made by Adsorber" + echo " e.g., disable scheduler (if set)" echo " restore hosts file to its original state" echo " version - show version of this shell script" echo " help - show this help" @@ -214,13 +214,14 @@ showSpecificHelp() echo "Possible option:" echo " -h, --help - show this help screen" ;; - remove ) - printf "\\033[4;37madsorber remove []\\033[0m:\\n" + disable ) + printf "\\033[4;37madsorber disable []\\033[0m:\\n" echo - echo "To completely remove changes made by Adsorber." + echo "To completely remove changes made by Adsorber," + echo "and disabling all background tasks (schedulers)." echo echo "The command will:" - echo " - remove all schedulers (systemd, cronjob)" + 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)" echo @@ -320,8 +321,8 @@ sourceFiles() . "${library_dir_path}/cleanup.sh" # shellcheck source=../src/lib/setup.sh . "${library_dir_path}/setup.sh" - # shellcheck source=../src/lib/remove.sh - . "${library_dir_path}/remove.sh" + # shellcheck source=../src/lib/disable.sh + . "${library_dir_path}/disable.sh" # shellcheck source=../src/lib/update.sh . "${library_dir_path}/update.sh" # shellcheck source=../src/lib/restore.sh @@ -453,11 +454,11 @@ case "${operation}" in setup update ;; - remove ) + disable ) checkForWrongParameters checkRoot config - remove + disable ;; update ) checkForWrongParameters diff --git a/src/lib/colours.sh b/src/lib/colours.sh index 1a1b0ae..bf49d25 100755 --- a/src/lib/colours.sh +++ b/src/lib/colours.sh @@ -11,8 +11,8 @@ # The following variables are declared globally. # If you run this file independently following variables need to be set: -# ---variable:- --default value:-- ---declared in:---- -# use_formatted true src/bin/adsorber, src/lib/cron/80adsorber, src/lib/systemd/systemd.service +# ---variable:--- --default value:-- ----declared in:---- +# use_formatting true src/bin/adsorber, src/lib/cron/default-cronjob.sh, src/lib/systemd/default-service if [ "${use_formatting}" != "false" ]; then readonly prefix=" " diff --git a/src/lib/remove.sh b/src/lib/disable.sh similarity index 86% rename from src/lib/remove.sh rename to src/lib/disable.sh index 15c6ce3..e5319f8 100755 --- a/src/lib/remove.sh +++ b/src/lib/disable.sh @@ -32,11 +32,11 @@ # shellcheck disable=SC2154 -remove_Prompt() +disable_Prompt() { # Ask if the user is sure about to remove Adsorber if [ -z "${reply_to_prompt}" ]; then - printf "%bDo you really want to remove Adsorber? [Y/n] %b" "${prefix_input}" "${prefix_reset}" + printf "%bDo you really want to disable Adsorber? [Y/n] %b" "${prefix_input}" "${prefix_reset}" read -r reply_to_prompt fi @@ -46,7 +46,7 @@ remove_Prompt() ;; * ) # If other input then Yes, abort and call error clean-up function - printf "%bRemoval cancelled.\\n" "${prefix_warning}" 1>&2 + printf "%bDisable cancelled.\\n" "${prefix_warning}" 1>&2 errorCleanUp exit 130 ;; @@ -56,7 +56,7 @@ remove_Prompt() } -remove_HostsFile() +disable_HostsFile() { # Moves the original hosts file (backed-up at /etc/hosts.original) # to /etc/hosts, replacing the current one @@ -66,7 +66,7 @@ remove_HostsFile() 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 already removed?" 1>&2 + echo "${prefix}Maybe you've already disabled Adsorber?" 1>&2 errorCleanUp exit 1 fi @@ -75,7 +75,7 @@ remove_HostsFile() } -remove_PreviousHostsFile() +disable_PreviousHostsFile() { # If found, remove /etc/hosts.previous if [ -f "${hosts_file_previous_path}" ]; then @@ -89,15 +89,15 @@ remove_PreviousHostsFile() } -# Main function of remove.sh -remove() +# Main function of disable.sh +disable() { - printf "%bRemoving Adsorber ...%b\\n" "${prefix_title}" "${prefix_reset}" - remove_Prompt + printf "%bDisabling Adsorber ...%b\\n" "${prefix_title}" "${prefix_reset}" + disable_Prompt systemdRemove crontabRemove - remove_HostsFile - remove_PreviousHostsFile + disable_HostsFile + disable_PreviousHostsFile cleanUp return 0 diff --git a/src/lib/systemd/default-service b/src/lib/systemd/default-service index a732d8f..ecac1cf 100644 --- a/src/lib/systemd/default-service +++ b/src/lib/systemd/default-service @@ -1,5 +1,5 @@ [Unit] -Description=#@frequency@# adsorber service +Description=#@frequency@# Adsorber service Documentation=https://github.com/stablestud/adsorber After=network.target diff --git a/src/share/default/default-adsorber.conf b/src/share/default/default-adsorber.conf index e2677be..a7bc458 100644 --- a/src/share/default/default-adsorber.conf +++ b/src/share/default/default-adsorber.conf @@ -55,7 +55,7 @@ ignore_download_error=true # Adsorber will prioritize the root users environment variables of 'http_proxy' # and 'https_proxy' over the ones specified here. # -# Please note that your user 'http(s)_proxy' environment variables won't affect Adsorber +# Please note that your ordinary user 'http(s)_proxy' environmental variables won't affect Adsorber # as it doesn't run as user but as root user. # # Possible value: address:port (e.g proxy:8080, 127.0.0.1:9050) From 647bb11e78ac663d24b46e9d9f2ded1200370e1c Mon Sep 17 00:00:00 2001 From: stablestud Date: Tue, 26 Feb 2019 20:10:50 +0100 Subject: [PATCH 38/43] Initial script has now amore structured output --- place_files_onto_system.sh | 14 +++++--------- remove_files_from_system.sh | 17 +++++++++++------ src/bin/adsorber | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index b9893a0..f50d46c 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -65,9 +65,7 @@ if [ "${prompt}" = "help" ] || [ "${prompt}" = "h" ] || [ "${prompt}" = "-h" ] | printHelp fi -echo "Current script location: ${script_dir_path}" -printLocation -echo +printf "Adsorber will be placed into /usr/local/*, which is the default path for external scripts.\\n\\n" if [ -z "${prompt}" ]; then printf "Are you sure you want to place Adsorbers files onto the system? [(y)es/(N)o]: " @@ -76,8 +74,8 @@ fi case "${prompt}" in -[Yy] | --[Yy][Ee][Ss] | [Yy] | [Yy][Ee][Ss] ) - : - ;; + echo + ;; * ) echo "Placing files onto the system has been cancelled." exit 1 @@ -90,8 +88,6 @@ if [ "$(id -g)" -ne 0 ]; then exit 126 fi -echo - ##[ Main exectuable ]########################################################### echo "Placing main executable (src/bin/adsorber) to ${executable_path}" @@ -107,7 +103,7 @@ chmod a+x "${executable_path}" ##[ Libraries ]################################################################# -echo "Placing other executables (src/lib/*) to ${library_dir_path}" +echo "Placing other executables (src/lib/*) to ${library_dir_path}" mkdir -p "${library_dir_path}" @@ -115,7 +111,7 @@ cp -r "${script_dir_path}/src/lib/." "${library_dir_path}" ##[ Shareables ]################################################################ -echo "Placing miscellaneous (src/share/*) to ${shareable_dir_path}" +echo "Placing miscellaneous (src/share/*) to ${shareable_dir_path}" mkdir -p "${shareable_dir_path}" diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 3ec90f0..3c59c12 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -23,11 +23,8 @@ readonly shareable_dir_path="/usr/local/share/adsorber/" # Define the location of the config files for adsorber. readonly config_dir_path="/usr/local/etc/adsorber/" -# Define the location of the log file. Not in use (yet). -#readonly log_file_path="/var/log/adsorber.log" - ## Following variables are only used when Adsorber's own removal activity failed -## and are used to remove Adsorber in the 'hard' way. Please change according to +## and are used to remove Adsorber manually. Please change according to ## your system configuration readonly hosts_file_path="/etc/hosts" readonly hosts_file_backup_path="/etc/hosts.original" @@ -42,7 +39,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}" @@ -80,10 +77,12 @@ if [ "${prompt}" = "help" ] || [ "${prompt}" = "h" ] || [ "${prompt}" = "-h" ] | printHelp fi + echo "Current script location: ${script_dir_path}" printLocation echo + # 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]: " @@ -100,12 +99,14 @@ case "${prompt}" in ;; esac + # Check if user is root, if not exit. if [ "$(id -g)" -ne 0 ]; then echo "You need to be root to remove Adsorber from the system." 1>&2 exit 126 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" @@ -120,6 +121,7 @@ else readonly _hard_way="true" fi + # Doing it the hard way .., removing everything manually if [ "${_hard_way}" = "true" ]; then printf "\\nTrying portable_adsorber.sh ... " @@ -172,15 +174,18 @@ fi echo + # Remove placed files from the specified locations rm -r "${executable_path}" 2>/dev/null && echo "Removed ${executable_path}" rm -r "${library_dir_path}" 2>/dev/null && echo "Cleaned ${library_dir_path}" rm -r "${shareable_dir_path}" 2>/dev/null && echo "Cleaned ${shareable_dir_path}" rm -r "${config_dir_path}" 2>/dev/null && echo "Cleaned ${config_dir_path}" + # Remove the adsorber command from cache/hashtable. # Shells must be reloaded / reopened to have an effect echo "Clearing adsorber from shell cache ..." + if command -v hash 1>/dev/null; then # Works in bash hash -d adsorber 2>/dev/null @@ -192,6 +197,6 @@ else export PATH="${PATH}" fi -echo +echo echo "Done. Adsorber has been removed from the system." diff --git a/src/bin/adsorber b/src/bin/adsorber index e7c1504..5ab7ac3 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -25,7 +25,7 @@ readonly shareable_dir_path="${executable_dir_path}/../share/" readonly config_dir_path="${executable_dir_path}/../../" -readonly version="0.5.2" +readonly version="0.5.0" readonly operation="${1}" if [ "${#}" -ne 0 ]; then From b9a2d15ea924e2a22cf9ea820aa52596c5b3452c Mon Sep 17 00:00:00 2001 From: stablestud Date: Wed, 27 Feb 2019 11:57:22 +0100 Subject: [PATCH 39/43] Improved help screen of the operations --- place_files_onto_system.sh | 6 ++-- remove_files_from_system.sh | 8 ++--- src/bin/adsorber | 43 +++++++++++++------------ src/lib/disable.sh | 2 +- src/share/default/default-adsorber.conf | 7 ++-- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/place_files_onto_system.sh b/place_files_onto_system.sh index f50d46c..220718b 100755 --- a/place_files_onto_system.sh +++ b/place_files_onto_system.sh @@ -103,7 +103,7 @@ chmod a+x "${executable_path}" ##[ Libraries ]################################################################# -echo "Placing other executables (src/lib/*) to ${library_dir_path}" +echo "Placing other executables (src/lib/) to ${library_dir_path}" mkdir -p "${library_dir_path}" @@ -111,7 +111,7 @@ cp -r "${script_dir_path}/src/lib/." "${library_dir_path}" ##[ Shareables ]################################################################ -echo "Placing miscellaneous (src/share/*) to ${shareable_dir_path}" +echo "Placing miscellaneous (src/share/) to ${shareable_dir_path}" mkdir -p "${shareable_dir_path}" @@ -119,7 +119,7 @@ cp -r "${script_dir_path}/src/share/." "${shareable_dir_path}" ##[ Config files ]############################################################## -echo "Copying config files (src/share/default/*) to ${config_dir_path}" +echo "Copying config files (src/share/default/) to ${config_dir_path}" mkdir -p "${config_dir_path}" diff --git a/remove_files_from_system.sh b/remove_files_from_system.sh index 3c59c12..c95b6aa 100755 --- a/remove_files_from_system.sh +++ b/remove_files_from_system.sh @@ -78,9 +78,9 @@ if [ "${prompt}" = "help" ] || [ "${prompt}" = "h" ] || [ "${prompt}" = "-h" ] | fi -echo "Current script location: ${script_dir_path}" -printLocation -echo +#echo "Current script location: ${script_dir_path}" +#printLocation +#echo # Prompt user if sure about to remove Adsorber from the system @@ -113,7 +113,7 @@ if command -v adsorber 1>/dev/null; then ( adsorber "disable" "-y" "--noformatting" ) \ || { echo - printf "\\033[0;93mSomething went wrong at running Adsorber's own removal action.\\nNo worries, I can handle it ...\\n\\033[0m" + 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" } diff --git a/src/bin/adsorber b/src/bin/adsorber index 5ab7ac3..c297022 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -87,11 +87,11 @@ showHelp() echo "(Ad)sorber blocks ads by \"absorbing\" and dumbing them into void." echo " (with the help of the hosts file)" echo "" - echo "Operations (required):" + echo "Operation (required):" echo " setup - setup necessary things needed for Adsorber" echo " e.g., create backup file of hosts file," echo " create scheduler which updates the host file once a week" - echo " However this should've been done automatically." +# echo " However this should've been done automatically." echo " update - update hosts file with newest ad servers" echo " restore - restore hosts file to its original state" echo " (it does not remove the scheduler, this should be used temporary)" @@ -138,7 +138,7 @@ showSpecificHelp() printf "\\033[4;37madsorber setup []\\033[0m:\\n" echo echo "You should run this command first." - echo " (e.g. after files have been placed to the system)" + echo " (e.g. after files have been placed onto the system)" echo echo "The command will:" echo " - backup your /etc/hosts file to /etc/hosts.original" @@ -148,11 +148,6 @@ showSpecificHelp() echo " - fetch the newest ad-domains to your hosts file." echo " (it runs 'adsorber update')" echo - echo "Note: this is not the same as the place_files_onto_system.sh script." - echo "place_files_onto_system.sh will place Adsorbers executable files into the system" - echo "so it can run directory independently, but it will not take the actions" - echo "described here. The same goes for 'remove'" - echo echo "Possible options:" echo " -y, --yes, --assume-yes - answer all prompts with 'yes'" echo " -s, --systemd - use Systemd ..." @@ -166,13 +161,14 @@ showSpecificHelp() 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" echo - echo "To keep the hosts file up-to-date." + echo "Updates your hosts files with the newest ad-domains fetched from the sources specified in 'sources.list'." echo echo "The command will:" echo " - fetch the newest ad-domains into your hosts file." @@ -181,53 +177,58 @@ 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" ;; restore ) printf "\\033[4;37madsorber restore []\\033[0m:\\n" echo - echo "To restore the hosts file temporary, without removing the backup." + echo "Restore the hosts file to its operating system default, without removing the backup or scheduler." echo echo "The command will:" echo " - copy /etc/hosts.original to /etc/hosts, overwriting the modified /etc/hosts by adsorber." echo - echo "Note: If you have a scheduler set it'll re-apply ad-domains to your hosts" - echo "file when triggered." - echo "For this reason this command is used to temporary disable Adsorber." + echo "Note: If you have a scheduler set it'll re-apply ad-domains to your hosts file when triggered." + 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 last version) 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 option:" + echo "Possible options:" echo " -h, --help - show this help screen" + echo " --noformatting - turn off coloured and formatted output" ;; revert ) printf "\\033[4;37madsorber revert []\\033[0m:\\n" echo - echo "To revert to the last 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:" echo " - copy /etc/hosts.previous to /etc/hosts, overwriting the current host file." echo - echo "To get the latest ad-domains run 'adsorber update'" + echo "To get the latest ad-domains run 'adsorber update'." echo - echo "Possible option:" + echo "Possible options:" echo " -h, --help - show this help screen" + echo " --noformatting - turn off coloured and formatted output" ;; disable ) printf "\\033[4;37madsorber disable []\\033[0m:\\n" echo - echo "To completely remove changes made by Adsorber," - echo "and disabling all background tasks (schedulers)." + 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)" + 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 "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" ;; * ) showHelp diff --git a/src/lib/disable.sh b/src/lib/disable.sh index e5319f8..fc2fb43 100755 --- a/src/lib/disable.sh +++ b/src/lib/disable.sh @@ -36,7 +36,7 @@ disable_Prompt() { # Ask if the user is sure about to remove Adsorber if [ -z "${reply_to_prompt}" ]; then - printf "%bDo you really want to disable Adsorber? [Y/n] %b" "${prefix_input}" "${prefix_reset}" + printf "%bDo you really want to disable Adsorber? [y/N] %b" "${prefix_input}" "${prefix_reset}" read -r reply_to_prompt fi diff --git a/src/share/default/default-adsorber.conf b/src/share/default/default-adsorber.conf index a7bc458..d3d7792 100644 --- a/src/share/default/default-adsorber.conf +++ b/src/share/default/default-adsorber.conf @@ -11,7 +11,7 @@ # Defines what list (either whitelist or blacklist) has the priority. # # For example: if 'blacklist' is set, the blacklist will overwrite -# the whitelist. It'll block the connection to a particular host from the +# the whitelist. It'll block the connection to a particular host listed in the # blacklist even if it's allowed in the whitelist. # # Possible values: whitelist, blacklist @@ -42,7 +42,7 @@ use_partial_matching=true # # For example: You have two host sources but one is not responding, to not # lose the already installed domains from the failing source, the script -# won't continue. +# won't continue if it was set to 'false'. # # Possible values: true, false # Default value: true @@ -109,6 +109,9 @@ hosts_file_previous_path=/etc/hosts.previous # Used to create a scheduler which supplies the hosts file regulary with # new ad-domains. Only change if your system has an other path. # +# Note: if you don't have systemd you can leave this setting by its default value. +# Use in this case cronjob instead. +# # Default value: /etc/systemd/system/ systemd_dir_path=/etc/systemd/system/ From b781e9a506f4f354de77f74bc18d52f87c45b385 Mon Sep 17 00:00:00 2001 From: stablestud Date: Wed, 27 Feb 2019 16:10:46 +0100 Subject: [PATCH 40/43] Updated README.md to reflect changes made in v0.5.0 --- README.md | 85 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 6bad1bf..9aaa800 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,61 @@ # 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-v0.5.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) (Ad)sorber blocks ads by 'absorbing' and dumbing them into void. Technically speaking, it adds ad-domains to the hosts file `/etc/hosts` with a redirection to a non-existent ip `0.0.0.0`. +You can consider it as [Adaway](https://github.com/AdAway/AdAway) for Linux. ## Features * Block advertisements system-wide, not only in the browser. * Prevents annoying anti-adblockers from triggering. -* Update your blocked ad-domain list from external hosts-servers (like https://adaway.org/hosts.txt). +* Update your ad-domain list with domains from external external sources (like https://adaway.org/hosts.txt). * Automatically update the hosts file per cronjob or systemd service. -* Revert to the previous hosts-file if the current hosts-file contains broken ad-servers +* Revert to the previous hosts file if the current hosts file contains broken ad-domains. * White- and blacklist. -Also it saves data, speeds up loading time and prevents some tracking of your browsing habits. For extensive privacy, I recommend using the script along browser add-ons like [NoScript](https://addons.mozilla.org/en-US/firefox/addon/noscript/) (for [Firefox 56 and below](https://noscript.net/getit)) or [uMatrix](https://addons.mozilla.org/en-US/firefox/addon/umatrix/), [Privacy Badger](https://addons.mozilla.org/en-US/firefox/addon/privacy-badger17/) and [HTTPS Everywhere](https://addons.mozilla.org/en-US/firefox/addon/https-everywhere/). - -By default Adsorber uses the following curated ad-hosts sources: -* [adaway.org](https://adaway.org/hosts.txt) -* [yoyo.org](https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext) -* & more to come. - -To add your own hosts sources, just add them to the `sources.list` file. ## Requirements +* `Linux` based distro * `root` rights (e.g. with `sudo`) - -If you have a `*nix` like distribution (`Ubuntu, Debian, Mint, Gentoo, Archlinux, Fedora, etc.`) -the following requirements should have been fulfilled by default: -* POSIX-compliant shell (`bash, dash, fish, ksh, zsh, sh, etc.`) -* Support for `hosts` file +Should be available with all distros: +* POSIX-compliant shell (`/bin/sh`) +* Support for `/etc/hosts` file * `curl` or `wget` -## Usage +## Installation +Download Adsorber from `[releases](https://github.com/stablestud/adsorber/releases)` or clone it. + +Two possibilites: +1. [Place files onto system (recommended)](#Place-files-onto-system-\(recommended\)) OR +2. [Run it portable](#Run-portable) + +### Place files onto system (recommended) +Adsorber will be placed onto your system (at `/usr/local/`). -### First steps .. placing files to system or portable mode -Adsorber can be placed into the system (recommended) for a more stable and robust usage. -This can be achieved through running `place_files_onto_system.sh`, the files will placed to `/usr/local/` if not other specified. -If you don't want Adsorber to be placed into your system run `portable_adsorber.sh`. +1. Execute the script `./place_files_onto_system.sh` +2. Execute the command `adsorber setup` and answer the prompts to configure Adsorber. +3. You're done! -Placing Adsorber into the system has the advantage that Adsorber can run independently from the user who downloaded it. -Also it prevents broken cronjobs/services as there is no risk that the files/directory of Adsorber will be accidentally deleted or moved. +Placing Adsorber has the advantage to run it independently from the user who downloaded it and +it prevents broken cronjobs/services as there is no risk that the files/directories of Adsorber will be accidentally deleted or moved. -To configure where it should be installed (to fit your system), edit the relevant lines in `place_files_onto_system.sh` and `remove_files_from_system.sh`. -For more help about the specific scripts run them with `--help`. +To configure where it should be placed (to fit your custom system), edit the relevant lines in `place_files_onto_system.sh` and `remove_files_from_system.sh`. However the default path (`/usr/local/`) is the default for external scripts on Linux (see [here](http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#USRLOCALLOCALHIERARCHY)). -1. run `place_files_onto_system.sh` as `root` user -2. run `adsorber setup` as `root` user and answer all prompts by your preference, or just hit all the time `ENTER` (default options will be used). -3. You've made it! +### Run portable + +1. Execute the script `./portable_adsorber.sh` +2. You're done! + +### Some other title + +By default Adsorber uses the following external sources: +* [adaway.org](https://adaway.org/hosts.txt) (also used by AdAway) +* [yoyo.org](https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext) (also used by AdAway) +To add your own ad-domain sources, just add them to the `sources.list` config file. You can find it at `/usr/local/etc/adsorber/`. + +## Usage ### Default help screen of `adsorber help` ``` @@ -56,7 +64,7 @@ Usage: adsorber [|--help] (Ad)sorber blocks ads by "absorbing" and dumbing them into void. (with the help of the hosts file) -Operations (required): +Operation (required): setup - setup necessary things needed for Adsorber e.g., create backup file of hosts file, create scheduler which updates the host file once a week @@ -113,6 +121,13 @@ Possible options are: * `-ns, --no-scheduler` * `-y, --yes, --assume-yes` * `-h, --help` +* `-H, --hourly` +* `-D, --daily` +* `-W, --weekly` +* `-M, --monthly` +* `-Q, --quarterly` +* `-S, --semiannually` +* `-Y, --yearly` #### `adsorber update {options}`: To keep the hosts file up-to-date. @@ -152,7 +167,7 @@ Possible option: * `-h, --help` #### `adsorber disable {options}`: -Completely remove changes made by Adsorber and disable all its background tasks (schedulers). +Completely disable all background tasks (schedulers) and remove changes made by Adsorber. The command will: * disable all schedulers (systemd, cronjob) @@ -177,21 +192,23 @@ Use with `setup` #### `-y, --yes, --assume-yes`: Answers all prompts with `yes` e.g., * `Do you really want to setup Adsorber?` -* `Do you really want to remove Adsorber?` +* `Do you really want to disable Adsorber?` -It'll not answer prompts which may harm your system. But `--force` will do it. +It'll not answer prompts which may harm your system. But `--force` will. #### `-f, --force`: This will force the script to continue (dangerous) the update e.g., * Continues if no backup has been created, overwriting the existing hosts file. #### `-h, --help`: -If specified in conjunction with an operation, it'll show extended help about the operation. +If specified with an operation, it'll show extended help about it. +#### `--noformatting`: +Disables coloured and formatted output by Adsorber. Useful for logging to files and output processing by other scripts. ## Settings: To add or remove ad-domain sources edit the `soures.list` file which is created after the setup of Adsorber. For a general configuration of Adsorber e.g., the path of the crontab, edit `adsorber.conf` To add domains to the `whilelist` or `blacklist` edit the relevant files at the default config location. -The configuration's default location is at `/usr/local/etc/adsorber/` if installed to system. +The configuration's default location is at `/usr/local/etc/adsorber/` if placed onto the system. If not, the config files should be placed at the scripts root directory. ## Todo for future releases From 4c235822734365f79c13e9e5e307b56ba0a90905 Mon Sep 17 00:00:00 2001 From: stablestud Date: Wed, 27 Feb 2019 17:20:35 +0100 Subject: [PATCH 41/43] Disabled scheduler creation in portable-mode This fixes broken services or cronjobs in the case when Adsorbers script directory was moved or deleted. --- portable_adsorber.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/portable_adsorber.sh b/portable_adsorber.sh index 3abefda..6aabbcb 100755 --- a/portable_adsorber.sh +++ b/portable_adsorber.sh @@ -16,7 +16,7 @@ runAdsorber() { echo "" # shellcheck disable=SC2086 - ( "${source_dir_path}/src/bin/adsorber" ${parameters} ) + ( "${source_dir_path}/src/bin/adsorber" "${parameters}" "--no-scheduler" ) exit_code="${?}" echo "" return "${exit_code}" From 30c861494071c857178bad6103ccbc2e74f4680b Mon Sep 17 00:00:00 2001 From: stablestud Date: Wed, 27 Feb 2019 17:38:07 +0100 Subject: [PATCH 42/43] Further updated README.md to reflect changes made in v0.5.0 --- README.md | 166 ++++++++++++++++++++++++++++++----------------- src/bin/adsorber | 6 +- 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 9aaa800..3b0b5ec 100644 --- a/README.md +++ b/README.md @@ -3,60 +3,65 @@ [![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) -(Ad)sorber blocks ads by 'absorbing' and dumbing them into void. -Technically speaking, it adds ad-domains to the hosts file `/etc/hosts` with a redirection to a non-existent ip `0.0.0.0`. -You can consider it as [Adaway](https://github.com/AdAway/AdAway) for Linux. +(Ad)sorber blocks ads by 'absorbing' and dumbing them into void. +Technically speaking, it adds ad-domains to the hosts file `/etc/hosts` with a redirection to a non-existent ip `0.0.0.0`. +You can consider it as [AdAway](https://github.com/AdAway/AdAway) for Linux machines. ## Features -* Block advertisements system-wide, not only in the browser. +* Blocks advertisements system-wide, not only in the browser. * Prevents annoying anti-adblockers from triggering. * Update your ad-domain list with domains from external external sources (like https://adaway.org/hosts.txt). -* Automatically update the hosts file per cronjob or systemd service. +* Automatically update the hosts file with schedulers (cronjob or systemd service). * Revert to the previous hosts file if the current hosts file contains broken ad-domains. * White- and blacklist. ## Requirements -* `Linux` based distro * `root` rights (e.g. with `sudo`) -Should be available with all distros: -* POSIX-compliant shell (`/bin/sh`) -* Support for `/etc/hosts` file -* `curl` or `wget` +* The following should be supported on all distros by default: + * `/bin/sh` + * `/etc/hosts` + * `curl` or `wget` ## Installation -Download Adsorber from `[releases](https://github.com/stablestud/adsorber/releases)` or clone it. +Download Adsorber from [`releases`](https://github.com/stablestud/adsorber/releases) or clone it. -Two possibilites: -1. [Place files onto system (recommended)](#Place-files-onto-system-\(recommended\)) OR -2. [Run it portable](#Run-portable) +__Two possibilites:__ +* [Install](#Install-recommended) to system (recommended) +* [Portable mode](#Portable-mode) without installation -### Place files onto system (recommended) -Adsorber will be placed onto your system (at `/usr/local/`). +### Install (recommended) +Adsorber will be placed onto your system (to `/usr/local/`). -1. Execute the script `./place_files_onto_system.sh` -2. Execute the command `adsorber setup` and answer the prompts to configure Adsorber. -3. You're done! +1. Execute the file `./place_files_onto_system.sh` +2. Run the command `adsorber setup` +3. Answer the prompts to configure Adsorber +4. Finished! -Placing Adsorber has the advantage to run it independently from the user who downloaded it and -it prevents broken cronjobs/services as there is no risk that the files/directories of Adsorber will be accidentally deleted or moved. +If you have a super custom system you can configure where Adsorber should be placed, edit the relevant lines in `place_files_onto_system.sh` and `remove_files_from_system.sh` +However the default path (`/usr/local/`) is the default for external scripts on Linux (see [here](http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#USRLOCALLOCALHIERARCHY)) and should be fine for most systems. -To configure where it should be placed (to fit your custom system), edit the relevant lines in `place_files_onto_system.sh` and `remove_files_from_system.sh`. However the default path (`/usr/local/`) is the default for external scripts on Linux (see [here](http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#USRLOCALLOCALHIERARCHY)). +Placing Adsorber has the advantage to run it independently from the user who downloaded it, +preventing broken cronjobs/services as there is no risk that the files/directories of Adsorber will be accidentally deleted or moved. -### Run portable +To reverse the steps (complete uninstall) run the script `./remove_files_from_system`. -1. Execute the script `./portable_adsorber.sh` -2. You're done! +### Portable mode +This mode will only download the recent ad-domains and merges them into your hosts file. +You won't be able to set a scheduler. -### Some other title +1. Execute `./portable_adsorber.sh setup` to generate the config files +1. Execute `./portable_adsorber.sh setup` again to continue +2. Finished! -By default Adsorber uses the following external sources: -* [adaway.org](https://adaway.org/hosts.txt) (also used by AdAway) -* [yoyo.org](https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext) (also used by AdAway) -To add your own ad-domain sources, just add them to the `sources.list` config file. You can find it at `/usr/local/etc/adsorber/`. +If you want to update your hosts file you need to do it yourself by running `./portable_adsorber.sh update` + +Portable-mode won't touch the system except for `/etc/hosts` which is required to block ad-servers. A backup of it will be created at `/etc/hosts.original`. ## Usage +### `adsorber []` + ### Default help screen of `adsorber help` ``` Usage: adsorber [|--help] @@ -87,10 +92,10 @@ Options (optional): (e.g 'adsorber update --help) --noformatting - turn off coloured and formatted output -Scheduler specifc options (use with 'setup'): +Scheduler options (use with 'setup'): -ns, --no-scheduler - skip scheduler creation - -s, --systemd - use Systemd ... - -c, --cron - use Cronjob as scheduler + -s, --systemd - setup Systemd as scheduler + -c, --cron - setup Cronjob ... -H, --hourly - run scheduler once hourly -D, --daily ... daily -W, --weekly ... weekly @@ -104,10 +109,13 @@ Config files are located at: /usr/local/etc/adsorber/ Documentation: https://github.com/stablestud/adsorber If you encounter any issues please report them to the Github repository. ``` + ### Operations (required): +To run Adsorber one of the following operations must be given as first parameter. + Note: to get further information about a operation run `adsorber --help` -#### `adsorber setup {options}`: +### adsorber `setup {options}`: You should run this command first. The command will: @@ -129,7 +137,7 @@ Possible options are: * `-S, --semiannually` * `-Y, --yearly` -#### `adsorber update {options}`: +### adsorber `update {options}`: To keep the hosts file up-to-date. The command will: @@ -141,38 +149,38 @@ Possible options are: * `-f, --force` * `-h, --help` -#### `adsorber revert {options}`: +### adsorber `revert {options}`: To revert to the last applied hosts-file by Adsorber. - + The command will: * copy `/etc/hosts.previous` to `/etc/hosts`, overwriting the newest `/etc/hosts`. -This is useful if the new hosts file contains less ad-domains, because a server -was unreachable and you don't want to loose the ad-servers supplied from this server. +This is useful if the new 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: * `-h, --help` -#### `adsorber restore {options}`: -To restore the hosts file temporary, without removing the backup. +### adsorber `restore {options}`: +To restore the hosts file to its original state, without removing the backup and scheduler. The command will: * copy `/etc/hosts.original` to `/etc/hosts`, overwriting the modified `/etc/hosts` by Adsorber. -Important: If Adsorber's scheduler was set-up, it'll re-apply ad-server domains to your hosts file when triggered. -For this reason this command is used to temporary disable Adsorber, e.g. when it's blocking some sites you need access for a short period of time. -To re-apply run `adsorber update` +Important: If Adsorber's scheduler was set-up, it'll re-apply ad-server domains to your hosts file when triggered. +For this reason this command is used to temporary disable Adsorber, e.g. when it's blocking some sites you need to access for a short period of time. +To re-apply run `adsorber revert` (for previous host file) or `adsorber update` (for updated version). Possible option: * `-h, --help` -#### `adsorber disable {options}`: -Completely disable all background tasks (schedulers) and remove changes made by Adsorber. +### adsorber `disable {options}`: +Completely disable all background tasks (schedulers) and remove all changes made by Adsorber. 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 (previous hosts file, etc) Possible options are: * `-y, --yes, --assume-yes` @@ -181,35 +189,75 @@ Possible options are: ### Options (optional): #### `-s, --systemd`: -Use with `setup`. -* Setup systemd scheduler, skipping the scheduler prompt. Files are placed into `/etc/systemd/system` by default. + +Option is only available with operation `setup`. +Adsorber uses Systemd as a scheduler to update your hosts file periodically. + +* Setup systemd scheduler, skipping the scheduler prompt. + +Files are placed into `/etc/systemd/system` by default. + #### `-c, --cronjob`: -Use with `setup`. -* Setup the cron scheduler, skipping the scheduler prompt. File is placed into `/etc/cron.weekly/` by default. + +Option is only available with operation `setup`. +Adsorber uses Cronjob as a default scheduler to update your hosts file periodically. + +* Setup the cron scheduler, skipping the scheduler prompt. + +File is placed into `/etc/cron.weekly/` by default. + #### `-ns, --no-scheduler`: -Use with `setup` -* Skips the setup of a scheduler. You'll need to update Adsorber manually with `adsorber update`. +Option is only available with operation `setup`. +Tells Adsorber no to install a scheduler. + +* Skips the setup of a scheduler, therefore skipping the scheduler prompt. + +You'll need to update Adsorber manually with `adsorber update`. + #### `-y, --yes, --assume-yes`: + Answers all prompts with `yes` e.g., * `Do you really want to setup Adsorber?` * `Do you really want to disable Adsorber?` It'll not answer prompts which may harm your system. But `--force` will. + #### `-f, --force`: + +Option is only available with operation `setup`. + This will force the script to continue (dangerous) the update e.g., * Continues if no backup has been created, overwriting the existing hosts file. + #### `-h, --help`: + If specified with an operation, it'll show extended help about it. + #### `--noformatting`: -Disables coloured and formatted output by Adsorber. Useful for logging to files and output processing by other scripts. +Disables coloured and formatted output by Adsorber. +Useful for logging to files and output processing by other scripts. ## Settings: -To add or remove ad-domain sources edit the `soures.list` file which is created after the setup of Adsorber. -For a general configuration of Adsorber e.g., the path of the crontab, edit `adsorber.conf` -To add domains to the `whilelist` or `blacklist` edit the relevant files at the default config location. -The configuration's default location is at `/usr/local/etc/adsorber/` if placed onto the system. -If not, the config files should be placed at the scripts root directory. +By default the config files are located at `/usr/local/etc/adsorber/`. +In portable-mode the config files are being created at the scripts root directory after the first run. + +Config files you may want to edit: +* `adsorber.conf` - general configuration +* `sources.list` - sources to fetch from +* `whitelist` - domains which should not be blocked +* `blacklist` - domains which should be always blocked + +By default Adsorber uses the following external sources: +* [adaway.org](https://adaway.org/hosts.txt) (also used by AdAway) +* [yoyo.org](https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext) (also used by AdAway) + +To add your own ad-domain sources, just add them to the `sources.list` config file. + +## Logging: + +The schedulers (systemd service, cronjob) will pass their output to the Syslog process. +The syslog can be examined at `/var/log/syslog`. ## Todo for future releases You're free to implement things listed/not listed in [`TODO.md`](https://github.com/stablestud/adsorber/blob/master/TODO.md) to Adsorber. diff --git a/src/bin/adsorber b/src/bin/adsorber index c297022..24faa8f 100755 --- a/src/bin/adsorber +++ b/src/bin/adsorber @@ -110,10 +110,10 @@ showHelp() echo " (e.g 'adsorber update --help)" echo " --noformatting - turn off coloured and formatted output" echo "" - echo "Scheduler specifc options (use with 'setup'):" + echo "Scheduler options (use with 'setup'):" echo " -ns, --no-scheduler - skip scheduler creation" - echo " -s, --systemd - use Systemd ..." - echo " -c, --cron - use Cronjob as scheduler" + 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" From 5f4ec9ff7a68f987670637593bd5766d81b0a58f Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 27 Feb 2019 22:00:00 +0000 Subject: [PATCH 43/43] Update TODO.md to reflect changes made in v0.5.0 --- TODO.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 2b52970..0e13251 100644 --- a/TODO.md +++ b/TODO.md @@ -1,13 +1,11 @@ ## Things considered for a future release/next release, ordered by priority ## Must: -* README.md update ## Should: -* save statistics to var/ and read from Adsorber ## Could: -* log-file +* save statistics to var/ and read from Adsorber * bash completion * proper unittest