123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #!/bin/bash
- HOSTS_URLS=( "https://someonewhocares.org/hosts/zero/hosts" "https://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext&useip=0.0.0.0" "https://winhelp2002.mvps.org/hosts.txt" )
- HOSTS_PATH="$HOME/etc/hosts"
- INITIAL_HOSTS="$HOME/etc/hosts.initial"
- EXCLUDE_HOSTS="$HOME/etc/hosts.exclude"
- NEW_HOSTS="hosts"
- NB_MAX_DOWNLOAD_RETRY=10
- CHECK_HOSTS=0
- GOOD_HOSTS=( "127."*"."*"."* "10."*"."*"."* "0.0.0.0" "255.255.255.255" "::1" "fe"*"::"* "ff0"*"::"* )
- function check_hosts() {
- echo "Checking hosts data..."
- lineNumber=0
- fileLines=$(cat $HOSTS_PATH | tr '\t' ' ' | tr '\r' ' ' | sed -e 's/^[[:space:]]*//' | cut -d " " -f1)
- while IFS= read -r line; do
- lineNumber=$((lineNumber + 1))
- found=0
- first_word="$(echo "$line" | head -n1)"
- first_letter="$(echo "$first_word" | head -c 1)"
- if [ -n "${first_word// }" ] && [ "$first_letter" != "#" ]; then
- for i in "${GOOD_HOSTS[@]}"; do
- if [[ "$first_word" == $i ]]; then
- found=1
- fi
- done
- if [ "$found" = "0" ]; then
- echo "Found incorrect or malicious entry. Exiting..."
- echo "Entry found: '${line}' at line $lineNumber"
- return 1
- fi
- fi
- done <<< "$fileLines"
-
- echo "No incorrect or malicious entry found."
- return 0
- }
- function check_curl() {
- if ! [ -x "$(command -v curl)" ]; then
- echo "Error: curl is not installed. Please install it to run this script." >&2
- exit 1
- fi
- }
- if [ $# -ge 1 ]; then
- if [ "$1" = "restore" ]; then
- echo "Restoring your hosts file backup..."
- if [ -f "${HOSTS_PATH}.bak" ]; then
- cp -v ${HOSTS_PATH}.bak $HOSTS_PATH
- echo "Done!"
- exit 0
- else
- echo "The backup hosts file doesn't exist: ${HOSTS_PATH}.bak"
- echo "Exiting..."
- exit 1
- fi
- fi
- if [ "$1" = "uninstall" ]; then
- echo "Uninstalling your hosts file and restoring initial hosts file..."
- if [ -f "$INITIAL_HOSTS" ]; then
- cp -v $INITIAL_HOSTS $HOSTS_PATH
- echo "Done!"
- exit 0
- else
- echo "The initial hosts file doesn't exist: $INITIAL_HOSTS"
- echo "Exiting..."
- exit 1
- fi
- fi
- if [ "$1" = "check" ]; then
- if [ -f "${HOSTS_PATH}" ]; then
- check_hosts $HOSTS_PATH || exit 1
- exit 0
- else
- echo "The hosts file doesn't exist: $HOSTS_PATH"
- echo "Exiting..."
- exit 1
- fi
- fi
- fi
- check_curl
- if [ ! -f "${HOSTS_PATH}" ]; then
- echo "The hosts file doesn't exist: $HOSTS_PATH"
- echo "Exiting..."
- exit 1
- fi
- echo "Creating temporary directory..."
- TEMP_DIR=`mktemp -d`
- if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
- echo "The temporary directory could not have been created. Exiting securely..."
- exit 1
- fi
- cd "$TEMP_DIR"
- echo "Created temporary directory at $(pwd)"
- echo "">$NEW_HOSTS
- DATE=`date '+%Y-%m-%d %H:%M:%S'`
- echo "">>$NEW_HOSTS
- echo "# HOSTS last updated: $DATE">>$NEW_HOSTS
- echo "#">>$NEW_HOSTS
- for i in "${HOSTS_URLS[@]}"
- do
- :
- nberror=0
- echo "Downloading hosts list from: $i"
- while true; do
- curl -s --fail "$i">>$NEW_HOSTS && break ||
- nberror=$((nberror + 1))
- echo "Download failed ! Retrying..."
- if [ $nberror -ge $NB_MAX_DOWNLOAD_RETRY ]; then
- echo "Download failed $NB_MAX_DOWNLOAD_RETRY time(s). Check your Internet connection and the hosts source then try again. Exiting..."
- exit 1
- fi
- done
- done
- echo "Backup old hosts file..."
- cp -v $HOSTS_PATH ${HOSTS_PATH}.bak
- if ! [ -f "${HOSTS_PATH}.bak" ]; then
- echo "HOSTS file backup not created. Exiting securely..."
- exit 1
- fi
- if [ -f "$EXCLUDE_HOSTS" ]; then
- echo "Excluding hosts..."
- lineNumber=0
- linesHost=$(cat "$NEW_HOSTS" | tr '\t' ' ' | tr '\r' ' ')
- linesHostExcluded=$(cat "$EXCLUDE_HOSTS" | tr '\t' ' ' | tr '\r' ' ')
-
- while read -r lineHost; do
- lineNumber=$((lineNumber + 1))
- excludedHost=0
- fileHost=$(echo "$lineHost" | tr '\t' ' ' | tr '\r' ' ' | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | head -n2)
-
- while read -r lineExclude; do
- if [[ "$fileHost" == $lineExclude ]]; then
- echo "Excluded '${lineHost}' (line $lineNumber)"
- excludedHost=1
- fi
- done <<< $linesHostExcluded
-
- if [ "$excludedHost" = "0" ]; then
- echo "$lineHost">>$NEW_HOSTS".tmp"
- fi
- done <<< $linesHost
-
- echo "">$NEW_HOSTS
- cat $NEW_HOSTS".tmp">>$NEW_HOSTS
- fi
- if [ "$CHECK_HOSTS" = "1" ]; then
- check_hosts $NEW_HOSTS || ( echo "You can disable hosts checking by changing the value of the variable CHECK_HOSTS to 0 in the script." && exit 1 )
- fi
- echo "Installing hosts list..."
- if [ -f "$INITIAL_HOSTS" ]; then
- cat $INITIAL_HOSTS>$HOSTS_PATH
- else
- echo "The initial hosts file doesn't exist: $INITIAL_HOSTS"
- echo "">$HOSTS_PATH
- fi
- cat $NEW_HOSTS >> $HOSTS_PATH
- echo "Removing cache..."
- rm $NEW_HOSTS*
- echo "Done!"
- exit 0
|