Last active
August 29, 2015 14:23
-
-
Save shoaibi/d0293ef11948e9e88aa8 to your computer and use it in GitHub Desktop.
Poor man's adblock using hosts file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Most of the code below has been adapted from several different scripts | |
mkdir -p /tmp/adblock &> /dev/null | |
temphosts1=`mktemp` | |
temphosts2=`mktemp` | |
# I prefer to separate adblock entries from my vanilla hosts file. | |
# I also use dnsmasq, which allows me to chain-load an additional | |
# hosts file, which is what i have done here. | |
target_file=/etc/hosts.dnsmasq | |
#function arguments -> filename to compare against current time | |
function compareDate() { | |
if [ ! -f $1 ]; then | |
#echo "$1 does not exist." | |
echo 1 | |
return | |
fi | |
MAXAGE=$(bc <<< '12*60*60') # seconds in 12 hours | |
# file age in seconds = current_time - file_modification_time. | |
FILEAGE=$(($(date +%s) - $(stat -c '%Y' "$1"))) | |
test $FILEAGE -lt $MAXAGE && { | |
#echo "$1 is less than 12 hours old." | |
echo 0 | |
return | |
} | |
#echo "$1 is older than 12 hours seconds." | |
echo 1 | |
} | |
getHash() { | |
/bin/echo "$1" | /usr/bin/md5sum | /bin/cut -f1 -d " " | |
} | |
downloadIfOlder() { | |
url="$1" | |
fileName="$2" | |
download=$(compareDate "$fileName") | |
if [ $download -ne 0 ]; then | |
#echo "Downloading $url" | |
curl -sS "$url" > "$fileName" | |
#else | |
#echo "Not Downloading $url" | |
fi | |
} | |
cd /tmp | |
# set sources | |
servers=( | |
"http://adaway.org/hosts.txt" | |
"http://hosts-file.net/ad_servers.txt" | |
"http://hostsfile.mine.nu/hosts0" | |
"http://mirror2.malwaredomains.com/files/BOOT" | |
"http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext" | |
"http://someonewhocares.org/hosts/hosts" | |
"http://hosts-file.net/download/HOSTS-Optimized.txt" | |
"http://winhelp2002.mvps.org/hosts.txt" | |
"http://www.malwaredomainlist.com/hostslist/hosts.txt" | |
"https://jansal.googlecode.com/svn/trunk/adblock/hosts" | |
"https://raw.githubusercontent.com/StevenBlack/hosts/master/data/StevenBlack/hosts" | |
) | |
for i in "${servers[@]}" | |
do | |
hash=$(getHash "$i") | |
fileName="/tmp/adblock/$hash" | |
echo "Trying $i to $fileName" | |
$(downloadIfOlder "$i" "$fileName") | |
cat "$fileName" >> $temphosts1 | |
#rm "$fileName" 2> /dev/null | |
done | |
# Do some work on the file: | |
# 1. Remove MS-DOS carriage returns | |
# 2. Delete any lines containing the word localhost because we'll obtain that from the original hosts file | |
# 3. Replace 127.0.0.1 with 0.0.0.0 because then we don't have to wait for the resolver to fail | |
# 4. Scrunch extraneous spaces separating address from name into a single tab | |
# 5. Delete any comments on lines | |
# 6. Clean up leftover trailing blanks | |
# Pass all this through sort with the unique flag to remove duplicates and save the result | |
echo "Patching up hosts file" | |
# /etc/hosts.block is a custom file that contains hosts I am not comfortable with | |
cat /etc/hosts.block >> $temphosts1 | |
# Excluding google, kat*, godaddy and few other sites | |
sed -e 's/\r//' -e 's/127.0.0.1/0.0.0.0/' -e 's/0.0.0.0 0.0.0.0/0.0.0.0 /' -e 's/PRIMARY/0.0.0.0/' -e '/localhost/d' -e 's/ \+/\t/' -e 's/#.*$//' -e 's/[ \t]*$//' < $temphosts1 | egrep -v ">|<" | grep -v 'gstatic' | grep -v "//" | grep -v "xn-----6kcvb6bctkco1byg.xn--p1ai" | grep -v 'kickass\.' | grep -v "kat\." | grep -v "google" | grep -v "d-h\.st" | grep -v "secureserver.net" | sort -u > $temphosts2 | |
# Combine system's vhosts file with adblock's | |
echo "Merging hosts file" | |
echo -e "\n# Ad blocking hosts generated "`date` | cat /etc/hosts.vhosts - $temphosts2 > /tmp/adblock/hosts | |
echo "Copy /tmp/hosts as $target_file" | |
sudo cp /tmp/adblock/hosts $target_file | |
echo "Cleaning up temp files" | |
rm $temphosts1 $temphosts2 2> /dev/null | |
echo "Restarting dnsmasq" | |
sudo systemctl restart dnsmasq | |
cd - &>> /dev/null | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/dnsmasq.d/shoaibi.conf | |
port=53 | |
filterwin2k | |
strict-order | |
no-resolv | |
no-poll | |
# server=192.168.20.1 | |
# server=192.168.5.1 | |
server=208.67.220.222 | |
server=8.8.8.8 | |
server=8.8.4.4 | |
server=141.1.27.249 | |
server=216.146.36.36 | |
server=194.6.240.1 | |
server=212.94.32.32 | |
server=156.154.71.1 | |
server=208.67.220.220 | |
server=208.67.222.222 | |
no-dhcp-interface=lo | |
addn-hosts=/etc/hosts.dnsmasq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment