Skip to content

Commit

Permalink
Removed unnecessary actions which would slow down the script
Browse files Browse the repository at this point in the history
Made some warnings more user friendly
Added check if HOSTS_FILE_PATH is  valid
  • Loading branch information
stablestud committed Jan 7, 2018
1 parent e481b96 commit c08ab64
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion adsorber.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Config file for Adsorber v0.2.0
# Config file for Adsorber v0.2.1
#
# To reset the config file just delete it and run 'adsorber.sh install',a new
# config file will be created
Expand Down
2 changes: 1 addition & 1 deletion adsorber.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ readonly TMP_DIR_PATH="/tmp/adsorber"
readonly SCRIPT_DIR_PATH="$(cd "$(dirname "${0}")" && pwd)"
readonly SOURCELIST_FILE_PATH="${SCRIPT_DIR_PATH}/sources.list"

readonly VERSION="0.2.0"
readonly VERSION="0.2.1"

readonly OPERATION="${1}"

Expand Down
4 changes: 2 additions & 2 deletions bin/components/hosts_header
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file has been edited by Adsorber.
#
# To make any changes to this file please edit @${HOSTS_FILE_BACKUP_PATH}@
# and run './adsorber.sh update'.
# Or else your changes will be overwritten by adsorber every week.
# and run './adsorber.sh update' to apply your changes.
# Or else your changes will be overwritten by adsorber once a week.
#
# The domains will be updated once a week.
# For further information please run './adsorber --help'
Expand Down
26 changes: 15 additions & 11 deletions bin/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
# SOURCELIST_FILE_PATH SCRIPT_DIR_PATH/sources.list (e.g., /home/user/Downloads/absorber/sources.list)
# TMP_DIR_PATH /tmp/adsorber
# USE_PARTIAL_MATCHING true
# VERSION 0.2.1 or similar

SETTING_STRING+=("PRIMARY_LIST")
SETTING_STRING+=("USE_PARTIAL_MATCHING")
SETTING_STRING+=("HOSTS_FILE_PATH")
SETTING_STRING+=("HOSTS_FILE_BACKUP_PATH")
SETTING_STRING+=("CRONTAB_DIR_PATH")
SETTING_STRING+=("SYSTEMD_DIR_PATH")
SETTING_STRING[0]="PRIMARY_LIST"
SETTING_STRING[1]="USE_PARTIAL_MATCHING"
SETTING_STRING[2]="HOSTS_FILE_PATH"
SETTING_STRING[3]="HOSTS_FILE_BACKUP_PATH"
SETTING_STRING[4]="CRONTAB_DIR_PATH"
SETTING_STRING[5]="SYSTEMD_DIR_PATH"

readonly SETTING_STRING

Expand Down Expand Up @@ -42,7 +43,7 @@ configCreateTmpDir() {
copySourceList() {
if [ ! -f "${SCRIPT_DIR_PATH}/sources.list" ] || [ ! -s "${SCRIPT_DIR_PATH}/sources.list" ]; then
cp "${SCRIPT_DIR_PATH}/bin/default/default-sources.list" "${SCRIPT_DIR_PATH}/sources.list" \
&& echo "To add new host sources, please edit sources.list"
&& echo "Created sources.list: to add new host sources edit this file."
fi

return 0
Expand All @@ -52,7 +53,7 @@ copySourceList() {
copyWhiteList() {
if [ ! -f "${SCRIPT_DIR_PATH}/whitelist" ] || [ ! -s "${SCRIPT_DIR_PATH}/whitelist" ]; then
cp "${SCRIPT_DIR_PATH}/bin/default/default-whitelist" "${SCRIPT_DIR_PATH}/whitelist" \
&& echo "To allow host sources, please edit the whitelist."
&& echo "Created whitelist: To allow specific domains edit this file."
fi

return 0
Expand All @@ -62,7 +63,7 @@ copyWhiteList() {
copyBlackList() {
if [ ! -f "${SCRIPT_DIR_PATH}/blacklist" ] || [ ! -s "${SCRIPT_DIR_PATH}/blacklist" ]; then
cp "${SCRIPT_DIR_PATH}/bin/default/default-blacklist" "${SCRIPT_DIR_PATH}/blacklist" \
&& echo "To block additional host sources, please edit the blacklist."
&& echo "Created blacklist: to block additional domains edit this file."
fi

return 0
Expand All @@ -75,7 +76,8 @@ copyConfig() {
else
echo "No config file found. Creating default."
echo "Please re-run the command to continue."
cp "${SCRIPT_DIR_PATH}/bin/default/default-adsorber.conf" "${SCRIPT_DIR_PATH}/adsorber.conf"
sed "s|@.*|# Config file for Adsorber v${VERSION}|g" "${SCRIPT_DIR_PATH}/bin/default/default-adsorber.conf" > "${SCRIPT_DIR_PATH}/adsorber.conf"

configCleanUp
exit 1
fi
Expand Down Expand Up @@ -132,7 +134,9 @@ isVariableSet() {

isVariableValid() {
if [ ! -f "${HOSTS_FILE_PATH}" ]; then
echo "Wrong HOSTS_FILE_PATH set. Can't access ${HOSTS_FILE_PATH}" 1>&2
echo "Wrong HOSTS_FILE_PATH set in adsorber.conf. Can't access ${HOSTS_FILE_PATH}" 1>&2
configCleanUp
exit 1
fi

return 0
Expand Down
2 changes: 1 addition & 1 deletion bin/default/default-adsorber.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Config file for Adsorber v0.2.0
@# Config file for Adsorber v?.?.? (DO NOT EDIT, WILL BE OVERWRITTEN BY SCRIPT)@
#
# To reset the config file just delete it and run 'adsorber.sh install',a new
# config file will be created
Expand Down
2 changes: 1 addition & 1 deletion bin/default/default-blacklist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Blacklist
# Use to manually block domains.
# By default it overwrites the whitelist. This can be changed in adsorber.conf
# By default the blacklist overwrites the whitelist. This can be changed in adsorber.conf
#
# Entries must begin with either 127.0.0.1 or 0.0.0.0

Expand Down
15 changes: 7 additions & 8 deletions bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ installCronjob() {
exit 1
fi

cp "${SCRIPT_DIR_PATH}/bin/cron/80adsorber" "${CRONTAB_DIR_PATH}"

# Replace the @ place holder line with SCRIPT_DIR_PATH
sed -i "s|@.*|${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${CRONTAB_DIR_PATH}/80adsorber"
# Replace the @ place holder line with SCRIPT_DIR_PATH and copy the content to crons directory
sed "s|@.*|${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/cron/80adsorber" > "${CRONTAB_DIR_PATH}/80adsorber"
chmod u=rwx,g=rx,o=rx "${CRONTAB_DIR_PATH}/80adsorber"

return 0
}
Expand All @@ -49,12 +48,12 @@ installSystemd() {
exit 1
fi

cp "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.service" "${SYSTEMD_DIR_PATH}/adsorber.service"

# Replace the @ place holder line with SCRIPT_DIR_PATH
sed -i "s|@ExecStart.*|ExecStart=${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SYSTEMD_DIR_PATH}/adsorber.service"
# Replace the @ place holder line with SCRIPT_DIR_PATH and copy to its systemd directory
sed "s|@ExecStart.*|ExecStart=${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.service" > "${SYSTEMD_DIR_PATH}/adsorber.service"
cp "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.timer" "${SYSTEMD_DIR_PATH}/adsorber.timer"

chmod u=rwx,g=rx,o=rx "${SYSTEMD_DIR_PATH}/adsorber.service" "${SYSTEMD_DIR_PATH}/adsorber.timer"

systemctl daemon-reload \
&& systemctl enable adsorber.timer \
&& systemctl start adsorber.timer || echo "Couldn't start systemd service." 1>&2
Expand Down
26 changes: 14 additions & 12 deletions bin/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ filterDomains() {
local input_file="${1}"
local output_file="${2}"

# - replace OSX '\r' and MS-DOS '\r\n' with Unix '\n' (linebreak)
# - replace 127.0.0.1 and 127.0.1.1 with 0.0.0.0
# - only keep lines starting with 0.0.0.0
# - remove inline '#' comments
# - replace tabs and multiple spaces with one space
# - remove domains without a dot (e.g localhost , loopback , ip6-allnodes , etc...)
# - remove domains that are redirecting to *.local
cat "${TMP_DIR_PATH}/${input_file}" \
| sed 's/\r/\n/g' \
| sed 's/^\s*127\.0\.[01]\.1/0\.0\.0\.0/g' \
Expand All @@ -176,13 +183,6 @@ filterDomains() {
| sed -n '/^0\.0\.0\.0\s.\+\..\+/p' \
| sed -n '/\.local\s*$/!p' \
> "${TMP_DIR_PATH}/${output_file}"
# - replace OSX '\r' and MS-DOS '\r\n' with Unix '\n' (linebreak)
# - replace 127.0.0.1 and 127.0.1.1 with 0.0.0.0
# - only keep lines starting with 0.0.0.0
# - remove inline '#' comments
# - replace tabs and multiple spaces with one space
# - remove domains without a dot (e.g localhost , loopback , ip6-allnodes , etc...)
# - remove domains that are redirecting to *.local

return 0
}
Expand Down Expand Up @@ -267,10 +267,8 @@ isCacheEmpty() {


preBuildHosts() {
# Add hosts_header
# Replace @...@ with the path to the backup hosts
cat "${SCRIPT_DIR_PATH}/bin/components/hosts_header" \
| sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" > "${TMP_DIR_PATH}/hosts"
sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header" > "${TMP_DIR_PATH}/hosts"

echo "" >> "${TMP_DIR_PATH}/hosts"

Expand All @@ -290,9 +288,13 @@ preBuildHosts() {
buildHostsFile() {
echo "" >> "${TMP_DIR_PATH}/hosts"

# Glue the final piece of the hosts file to it
# Add ad-domains to the hosts file
cat "${TMP_DIR_PATH}/cache" >> "${TMP_DIR_PATH}/hosts"

echo "" >> "${TMP_DIR_PATH}/hosts"

sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header" >> "${TMP_DIR_PATH}/hosts"

return 0
}

Expand Down Expand Up @@ -343,7 +345,7 @@ update() {
mergeBlackList
;;
* )
echo "Wrong PRIMARY_LIST set. Choose either 'whitelist' or 'blacklist'" 1>&2
echo "Wrong PRIMARY_LIST set in adsorber.conf. Choose either 'whitelist' or 'blacklist'" 1>&2
updateCleanUp
exit 1
;;
Expand Down

0 comments on commit c08ab64

Please sign in to comment.