Browse Source

First commit of adblock.

root 5 years ago
commit
1e23a3f216
4 changed files with 130 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 107 0
      adblock
  3. 22 0
      adblock.conf
  4. 0 0
      dbs/.gitkeep

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+dbs

+ 107 - 0
adblock

@@ -0,0 +1,107 @@
+#!/bin/sh
+
+
+#
+# Load config
+#
+config=/opt/adblock/adblock.conf
+if [ ! -f $config ]; then
+	echo "config file not found [ $config ]"
+	exit 1;
+fi
+source $config;
+
+
+#
+# Check local db version
+#
+localVersion=0
+if [ -f "$dbFile".version ]; then
+
+	localVersion=$( cat "$dbFile".version )
+	echo "Local db version: $localVersion"
+fi
+
+
+#
+# Check remote db version
+#
+remoteVersion=$( wget -O - "$dbURL".version 2> /dev/null )
+echo "Remote db version: $remoteVersion"
+
+
+#
+# Check if db should be updated
+#
+if [ $localVersion -eq $remoteVersion ]; then
+
+	echo "Database is up-to-date."
+else
+
+	# Update stale db
+	echo "Updating local database..."
+	wget -O - $dbURL.version 2> /dev/null > $dbFile.version
+	wget -O - $dbURL 2> /dev/null | grep [0-9].* |
+		sed 's/[[:space:]]*#.*$//g;' |
+		grep -v localhost | tr ' ' '\t' |
+		tr -s '\t' | tr -d '\015' | sort -u > $dbFile
+fi
+
+
+#
+# Update dnsmasq if needed
+#
+grep addn-hosts $dnsmasq &> /dev/null
+if [ $? -eq 0 ]; then
+
+	echo "dnsmasq up-to-date."
+else
+
+	echo "Updating dnsmasq..."
+	echo "" >> $dnsmasq
+	echo "" >> $dnsmasq
+	echo "#Adblock" >> $dnsmasq
+	echo "addn-hosts=$dbFile" >> $dnsmasq
+fi
+
+
+#
+# Restart dnsmasq
+#
+echo "Restarting dnsmasq..."
+killall dnsmasq
+dnsmasq --conf-file=$dnsmasq
+
+
+#
+# Update firewall if needed
+#
+iptables -vnL FORWARD | grep 99.99.99.99 &> /dev/null
+if [ $? -eq 0 ]; then
+
+	echo "Firewall block up-to-date."
+else
+
+	echo "Applying firewall block rule..."
+	iptables -I FORWARD -d 99.99.99.99 -j REJECT
+fi
+
+#
+# Update firewall if needed
+#
+iptables -t nat -vnL OUTPUT | grep 53 &> /dev/null
+if [ $? -eq 0 ]; then
+
+	echo "Firewall dns rule up-to-date."
+else
+
+	echo "Applying firewall dns rule..."
+	iptables -t nat -I OUTPUT -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:53
+fi
+
+
+#
+# Done
+#
+echo "done"
+exit 0

+ 22 - 0
adblock.conf

@@ -0,0 +1,22 @@
+#!/bin/bash
+
+#
+# Blacklist download  url
+#
+dbURL="http://mirror.nclarkdev.com/adblock/blacklist"
+
+#
+# Storage location
+#
+dbFile=/opt/adblock/dbs/blacklist
+
+#
+# Dnsmasq file
+#
+dnsmasq=/tmp/dnsmasq.conf
+
+#
+# Log file
+#
+logFile=/opt/adblock/adblock.log
+

+ 0 - 0
dbs/.gitkeep