Skip to content

Commit

Permalink
get-easylist.sh Version 0.2
Browse files Browse the repository at this point in the history
Automatically detect various squid configuration values to be more compatible with different setups
Ability to bypass check when running via cron
Additional message output
  • Loading branch information
jamesmacwhite committed Jun 20, 2015
1 parent 86f16d1 commit c9137d7
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions get-easylist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## get-easylist.sh
## Author: James White ([email protected])
## Description: Gets Adblock lists and converts them for use with SquidGuard
## Version: 0.1
## Version: 0.2
##
## Notes:
## A specific sed pattern file is required for the conversion
Expand All @@ -12,7 +12,7 @@
##

SCRIPT_NAME=${0##*/}
SCRIPT_VERSION="0.1"
SCRIPT_VERSION="0.2"

echo "Starting ${SCRIPT_NAME} Version: ${SCRIPT_VERSION}"

Expand All @@ -21,17 +21,26 @@ if ! [ "$(id -u)" = 0 ]; then
exit 1
fi

get_squid_build_flag() {
# $1: Build flag used during compile
squid -v | tr " " "\n" | grep -- "$1" | tail -n1 | cut -f2 -d '=' | sed "s/'//g"
}

get_squid_conf_value() {
# $1: Squid config value
# $2: Squid config filename
grep -i "$1" "$2" | awk '{ print $2 }'
}

# Squid/SquidGuard Configuration.
SQUID_USER="squid"
SQUID_CONF_DIR="/etc/squid"
SQUID_CONF_FILE=$(find ${SQUID_CONF_DIR} -name squid.conf)
echo "Scanning Squid and SquidGuard setup please wait..."

# Squid/SquidGuard Configuration
SQUID_BIN=$(command -v squid squid2 squid3)
SQUID_USER=$(get_squid_build_flag "--with-default-user")
SQUID_CONF_DIR=$(get_squid_build_flag "--sysconfdir")
SQUID_CONF_FILE=$(find "${SQUID_CONF_DIR}" -name squid.conf)
SQUID_LOG_FILE=$(get_squid_conf_value "access_log" "${SQUID_CONF_FILE}")
SQUIDGUARD_BIN=$(command -v squidguard squidGuard)
SQUIDGUARD_CONF_FILE=$(find / -iname squidGuard.conf)
SQUIDGUARD_DB_DIR=$(get_squid_conf_value "dbhome" "${SQUIDGUARD_CONF_FILE}")
SQUIDGUARD_ADBLOCK_DIR="${SQUIDGUARD_DB_DIR}/adblock"
Expand All @@ -45,23 +54,28 @@ show_message() {
}

show_message "The following paths have been detected"
echo "Squid Bin Path: ${SQUID_BIN}"
echo "Squid User: ${SQUID_USER}"
echo "Squid Config Folder: ${SQUID_CONF_DIR}"
echo "Squid Config File: ${SQUID_CONF_FILE}"
echo "SquidGuard Config File: ${SQUIDGUARD_CONF_FILE}"
echo "SquidGuard Database Folder: ${SQUIDGUARD_DB_DIR}"
echo "SquidGuard Adblock Folder ${SQUIDGUARD_ADBLOCK_DIR}"
echo ""

read -r -p "Does everything look OK? [Y/N] " SQUID_CONF_OK
case ${SQUID_CONF_OK} in
[yY][eE][sS]|[yY])
echo "Great, will continue executing script"
;;
*)
echo "Exiting..."
exit 1

if [ ! "$*" == "bypass_check" ] ; then
read -r -p "Does everything look OK? [Y/N] " SQUID_CONF_OK
case ${SQUID_CONF_OK} in
[yY][eE][sS]|[yY])
echo "Great, will continue executing script"
;;
*)
echo "Exiting..."
exit 1
;;
esac
esac
fi

# Pattern and URL files
SED_PATTERN_FILE="patterns.sed"
Expand Down Expand Up @@ -99,31 +113,33 @@ do
grep -q -E '^\[Adblock.*\]$' "${LIST_FILE_PATH}"

if [ ! $? -eq 0 ] ; then
echo "An non-adblock list was detected"
echo "An non-adblock list was detected."
exit 1
fi

echo "Converting ${LIST_FILE_NAME} to compatible format for SquidGuard"
echo "Converting ${LIST_FILE_NAME} to an expressions list for SquidGuard"
sed -e "${ADBLOCK_PATTERNS}" "${LIST_FILE_PATH}" > "${SQUIDGUARD_ADBLOCK_DIR}/${LIST_FILE_NAME}"

done

show_message "Rebuilding SquidGuard Database"
squidGuard -b -d -C all
${SQUIDGUARD_BIN} -b -d -C all

# Make sure permissions are good, otherwise SquidGuard will go into emergency mode
# https://help.ubuntu.com/community/SquidGuard

chmod 644 "${SQUIDGUARD_CONF_FILE}"
chmod -R 640 "${SQUIDGUARD_DB_DIR}"
chmod -R 644 "$(dirname "${SQUID_LOG_FILE}")"
chown ${SQUID_USER}:${SQUID_USER} "${SQUIDGUARD_CONF_FILE}"
chown -R ${SQUID_USER}:${SQUID_USER} "${SQUIDGUARD_DB_DIR}"
chown "${SQUID_USER}":"${SQUID_USER}" "${SQUIDGUARD_CONF_FILE}"
chown -R "${SQUID_USER}":"${SQUID_USER}" "${SQUIDGUARD_DB_DIR}"
find "${SQUIDGUARD_DB_DIR}" -type d -exec chmod 755 \{\} \; -print > /dev/null 2>&1
chmod 755 "$(dirname "${SQUID_LOG_FILE}")"

show_message "Reloading squid"
squid -k reconfigure
${SQUID_BIN} -k reconfigure

# Remove adblock folder in tmp
rm -rf ${EASYLIST_TMP_DIR} > /dev/null 2>&1

echo "Adblock expressions lists are now installed!"

0 comments on commit c9137d7

Please sign in to comment.