maza-ad-blocking/maza

146 lines
3.1 KiB
Plaintext
Raw Normal View History

2020-01-03 08:16:42 +01:00
#!/bin/sh
# START
set -e
2020-01-03 10:17:00 +01:00
# VARIABLES
2020-01-03 08:16:42 +01:00
PROGNAME=$(basename $0)
2020-01-03 08:18:40 +01:00
CONFIG=($HOME/.maza/)
2020-01-03 10:17:00 +01:00
HOST_FILE=(/etc/hosts)
COLOR_RED=`tput setaf 1`
COLOR_GREEN=`tput setaf 2`
COLOR_RESET=`tput sgr0`
2020-01-03 08:16:42 +01:00
LIST="list"
2020-01-04 08:32:22 +01:00
LIST_DNSMASQ="dnsmasq.conf"
2020-01-03 09:50:01 +01:00
START_TAG="## MAZA - List ad blocking"
2020-01-03 17:17:54 +01:00
PROJECT="### https://github.com/tanrax/maza-ad-blocking"
AUTHOR="### Created by Andros Fenollosa (https://programadorwebvalencia.com/)"
2020-01-03 09:50:01 +01:00
END_TAG="## END MAZA"
2020-01-03 08:16:42 +01:00
# FUNCTIONS
## HELP
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION]
Simple and efficient local ad blocking throughout the network.
Options:
2020-01-03 10:17:00 +01:00
status Check if it's active or not
2020-01-03 08:16:42 +01:00
update Update the list of DNS to be blocked
start Activate blocking DNS.
stop Stop blocking DNS.
--help Display this usage message and exit
EOF
exit 1
}
2020-01-03 10:17:00 +01:00
status() {
if grep -qF "$START_TAG" "$HOST_FILE";then
echo "${COLOR_GREEN}ENABLED${COLOR_RESET}"
else
echo "${COLOR_RED}DISABLED${COLOR_RESET}"
fi
}
2020-01-03 08:16:42 +01:00
update() {
2020-01-03 08:22:28 +01:00
# Make conf folder
2020-01-03 08:16:42 +01:00
rm -f $CONFIG/$LIST
2020-01-04 08:32:22 +01:00
rm -f $CONFIG/$LIST_DNSMASQ
2020-01-03 08:16:42 +01:00
mkdir -p $CONFIG
2020-01-03 08:22:28 +01:00
# Download DNS list
2020-01-04 08:17:41 +01:00
curl -L -s "https://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext" -o "$CONFIG/$LIST"
2020-01-03 08:22:28 +01:00
# Clear list
2020-01-03 17:17:54 +01:00
## Remove comments
sed -i .bak '/^#/ d' "$CONFIG/$LIST"
2020-01-04 08:32:22 +01:00
# Make dnsmasq format
## 127.0.0.1 doubleclick.net to address=/doubleclick.net/127.0.0.1
cp "$CONFIG/$LIST" "$CONFIG/$LIST_DNSMASQ"
sed -i .bak 's/127.0.0.1 /address=\//g' "$CONFIG/$LIST_DNSMASQ"
sed -i .bak 's/$/\/127.0.0.1/g' "$CONFIG/$LIST_DNSMASQ"
2020-01-03 09:50:01 +01:00
## Add start tag DNS list in first line
2020-01-03 17:17:54 +01:00
sed -i .bak "1i\\
$AUTHOR
" "$CONFIG/$LIST"
sed -i .bak "1i\\
$PROJECT
" "$CONFIG/$LIST"
2020-01-03 09:50:01 +01:00
sed -i .bak "1i\\
$START_TAG
" "$CONFIG/$LIST"
## Add end tag DNS list in first line
echo $END_TAG >> "$CONFIG/$LIST"
2020-01-04 08:32:22 +01:00
## Add start tag DNS dnsmasq in first line
sed -i .bak "1i\\
$AUTHOR
" "$CONFIG/$LIST_DNSMASQ"
sed -i .bak "1i\\
$PROJECT
" "$CONFIG/$LIST_DNSMASQ"
sed -i .bak "1i\\
$START_TAG
" "$CONFIG/$LIST_DNSMASQ"
## Add end tag DNS DNSMASQ in first line
echo $END_TAG >> "$CONFIG/$LIST_DNSMASQ"
# Remove temp file
rm "$CONFIG$LIST.bak"
2020-01-03 09:50:01 +01:00
# Notify user
2020-01-03 10:17:00 +01:00
echo "${COLOR_GREEN}Done!${COLOR_RESET}"
}
start() {
2020-01-03 11:21:06 +01:00
# Add List to host file
2020-01-03 10:27:08 +01:00
cat "$CONFIG/$LIST" >> "$HOST_FILE"
2020-01-03 10:17:00 +01:00
# Notify user
echo "${COLOR_GREEN}ENABLED!${COLOR_RESET}"
}
stop() {
2020-01-03 11:21:06 +01:00
# Remove list to host file
2020-01-03 17:24:27 +01:00
sed -i -n "/$START_TAG/,/$END_TAG/d" "$HOST_FILE"
2020-01-03 17:17:54 +01:00
# Remove temp file
2020-01-03 17:24:27 +01:00
#rm "$HOST_FILE.bak"
2020-01-03 11:21:06 +01:00
# Notify user
echo "${COLOR_GREEN}DISABLED!${COLOR_RESET}"
2020-01-03 08:16:42 +01:00
}
# CONTROLE ARGUMENTS
isArg=""
while [ $# -gt 0 ] ; do
case "$1" in
--help)
usage
;;
2020-01-03 10:17:00 +01:00
status)
isArg="1"
status
;;
2020-01-03 08:16:42 +01:00
update)
isArg="1"
update
;;
start)
isArg="1"
2020-01-03 10:17:00 +01:00
start
2020-01-03 08:16:42 +01:00
;;
stop)
isArg="1"
2020-01-03 10:17:00 +01:00
stop
2020-01-03 08:16:42 +01:00
;;
*)
esac
shift
done
if [ -z $isArg ] ; then
usage "Not enough arguments"
fi