maza-ad-blocking/maza

218 lines
5.4 KiB
Plaintext
Raw Normal View History

2020-10-16 11:49:43 +02:00
#!/usr/bin/env bash
2020-01-03 08:16:42 +01:00
# START
set -e
2020-01-03 10:17:00 +01:00
# VARIABLES
2023-08-21 18:58:58 +02:00
URL_DNS_LIST="https://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext"
2024-05-05 15:04:06 +02:00
URL_DNS_LIST_CUSTOM=""
2020-03-21 20:26:10 +01:00
NAME_OSX="Darwin"
THIS_OS=$(uname -mrs)
PROGNAME=$(basename "$0")
2023-05-29 10:05:46 +02:00
[[ -z "${XDG_CONFIG_HOME}" ]] && CONFIG=$HOME/.config/maza/ || CONFIG=$XDG_CONFIG_HOME/maza/
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"
2023-08-21 18:57:40 +02:00
CUSTOM_DOMAINS="custom-domains"
IGNORE_LIST_DEFAULT="localhost \
localhost.localdomain \
local \
broadcasthost \
ip6-localhost \
ip6-loopback \
ip6-localnet \
ip6-mcastprefix \
ip6-allnodes \
ip6-allrouters \
ip6-allhosts \
0.0.0.0"
2023-03-13 18:41:15 +01:00
IGNORE_LIST_FILE="ignore"
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"
2023-02-28 08:55:38 +01:00
AUTHOR="### Created by Andros Fenollosa (https://andros.dev/)"
2020-01-03 09:50:01 +01:00
END_TAG="## END MAZA"
2020-01-03 08:16:42 +01:00
2020-03-21 20:26:10 +01:00
# Create sed cross system
2020-03-21 21:02:04 +01:00
custom-sed() {
2020-03-21 20:26:10 +01:00
if [[ $THIS_OS = *$NAME_OSX* ]]; then
# Check if OSX and install GSED
if [ -x "$(command -v gsed)" ]; then
gsed "$@"
else
echo "${COLOR_RED}ERROR. You must install gsed if you are using OSX${COLOR_RESET}"
exit 1
fi
else
# Linux
sed "$@"
fi
}
2020-03-21 21:02:04 +01:00
export -f custom-sed
2020-03-21 20:26:10 +01:00
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() {
2023-08-21 18:57:40 +02:00
2024-05-05 15:04:06 +02:00
local local_url_dns_list="$URL_DNS_LIST" # Default
if [ -n "$URL_DNS_LIST_CUSTOM" ]; then
echo "si"
local_url_dns_list="$URL_DNS_LIST_CUSTOM" # Custom
fi
2020-01-03 08:22:28 +01:00
# Make conf folder
rm -f "$CONFIG$LIST"
rm -f "$CONFIG$LIST_DNSMASQ"
mkdir -p "$CONFIG"
2023-08-21 18:57:40 +02:00
2020-01-03 08:22:28 +01:00
# Download DNS list
2024-05-05 15:04:06 +02:00
curl -L -s "$local_url_dns_list" -o "$CONFIG$LIST"
2020-01-03 17:17:54 +01:00
## Remove comments
2023-08-21 11:47:53 +02:00
### Start with #
2020-03-21 21:02:04 +01:00
custom-sed -i.bak '/^#/ d' "$CONFIG$LIST"
2023-08-21 11:47:53 +02:00
### Remove comments in middle of line
custom-sed -i.bak 's/#.*$//g' "$CONFIG$LIST"
### Remove spaces in end of line
custom-sed -i.bak 's/ *$//g' "$CONFIG$LIST"
## Remove "0.0.0.0" or "127.0.0.1"
custom-sed -i.bak 's/0.0.0.0 //g' "$CONFIG$LIST"
custom-sed -i.bak 's/127.0.0.1 //g' "$CONFIG$LIST"
2023-08-21 18:57:40 +02:00
# Make ignore list
if [ ! -f "$CONFIG$IGNORE_LIST_FILE" ]; then
echo "$IGNORE_LIST_DEFAULT" | tr " " "\n" > "$CONFIG$IGNORE_LIST_FILE"
fi
2023-08-21 18:57:40 +02:00
# Remove ignore list
while read -r line; do
custom-sed -i.bak "/$line/d" "$CONFIG$LIST"
done < "$CONFIG$IGNORE_LIST_FILE"
2023-08-21 18:57:40 +02:00
# Make custom domains
## Check if file exists
if [ ! -f "$CONFIG$CUSTOM_DOMAINS" ]; then
## Create file
touch "$CONFIG$CUSTOM_DOMAINS"
fi
## Include custom domains
cat "$CONFIG$CUSTOM_DOMAINS" >> "$CONFIG$LIST"
echo "" >> "$CONFIG$LIST"
# Remove empty lines
custom-sed -i.bak '/^$/d' "$CONFIG$LIST"
2023-08-21 18:57:40 +02:00
2020-01-04 08:32:22 +01:00
# Make dnsmasq format
2020-03-21 20:26:10 +01:00
cp "$CONFIG$LIST" "$CONFIG$LIST_DNSMASQ"
2023-08-21 12:16:05 +02:00
## Remove triple "-". Example: "my---domain.com" to "my-domain.com". Only in dnsmasq.
custom-sed -i.bak "s/---/-/g" "$CONFIG$LIST_DNSMASQ"
## Remove double "-". Example: "my--domain.com" to "my-domain.com". Only in dnsmasq.
custom-sed -i.bak "s/--/-/g" "$CONFIG$LIST_DNSMASQ"
## mydomain.com to address=/mydomain.com/127.0.0.1
custom-sed -i.bak "s|\(.*\)|address=/\1/127.0.0.1|" "$CONFIG$LIST_DNSMASQ"
2020-01-03 09:50:01 +01:00
## Add start tag DNS list in first line
2020-03-21 21:02:04 +01:00
custom-sed -i.bak "1i\\$AUTHOR" "$CONFIG$LIST"
custom-sed -i.bak "1i\\$PROJECT" "$CONFIG$LIST"
custom-sed -i.bak "1i\\$START_TAG" "$CONFIG$LIST"
2020-01-03 09:50:01 +01:00
## 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
2020-03-21 21:02:04 +01:00
custom-sed -i.bak "1i\\$AUTHOR" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak "1i\\$PROJECT" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak "1i\\$START_TAG" "$CONFIG$LIST_DNSMASQ"
2020-01-04 08:32:22 +01:00
## Add end tag DNS DNSMASQ in first line
echo "$END_TAG" >> "$CONFIG$LIST_DNSMASQ"
2023-08-21 18:57:40 +02:00
2020-01-04 08:32:22 +01:00
# Remove temp file
rm "$CONFIG$LIST.bak"
2020-01-04 08:38:33 +01:00
rm "$CONFIG$LIST_DNSMASQ.bak"
2023-08-21 18:57:40 +02:00
2020-01-03 09:50:01 +01:00
# Notify user
2020-01-04 08:38:33 +01:00
echo "${COLOR_GREEN}List updated!${COLOR_RESET}"
2020-01-03 10:17:00 +01:00
}
start() {
2020-01-04 08:38:33 +01:00
update
2023-08-21 11:13:51 +02:00
# Add List to host file.
# Transform "mydomain.com" to "127.0.0.1 mydomain.com" except comments
custom-sed "/^#/!s/^/127.0.0.1 /g" "$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-03-21 21:33:42 +01:00
custom-sed -i "/$START_TAG/,/$END_TAG/d" "$HOST_FILE"
2020-01-03 17:17:54 +01:00
2020-01-04 08:38:33 +01:00
# Remove DNSMASQ
cat /dev/null > "$CONFIG$LIST_DNSMASQ"
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