Advertisement
Drakonas

Customized DD-WRT Adblock Script (Firewall Command)

Jun 8th, 2014
1,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.57 KB | None | 0 0
  1. # --- COPY THE TEXT BELOW TO DD-WRT / ADMINISTRATION / COMMANDS then click SAVE FIREWALL ---
  2. # --- WARNING: This script has enabled extra ad repositories that can be taxing on memory ---
  3. # --- It is recommended to not use this specific script on a router with 32MB ram or less. Recommended 64MB or more.
  4. BH_SCRIPT="/tmp/blocking_hosts.sh"
  5. BH_WHITELIST="/tmp/blocking_hosts.whitelist"
  6. logger "Download blocking hosts file and restart dnsmasq ..."
  7. # Create whitelist. The whitelist entries will be removed from the
  8. # hosts files, i.e. blacklist files.
  9. cat > "$BH_WHITELIST" <<EOF
  10. localhost\\.localdomain
  11. local
  12. invalid
  13. adf\\.ly
  14. .*\\.adf\\.ly
  15. tiny\\.cc
  16. .*\\.tiny\\.cc
  17. bit\\.ly
  18. .*\\.bit\\.ly
  19. bitly\\.com
  20. .*\\.bitly\\.com
  21. q\\.gs
  22. .*\\.q\\.gs
  23. adfoc\\.us
  24. .*\\.adfoc\\.us
  25. shorte\\.st
  26. .*\\.shorte\\.st
  27. hulu\\.com
  28. .*\\.hulu\\.com
  29. EOF
  30. # Create download script.
  31. cat > "$BH_SCRIPT" <<EOF
  32. #!/bin/sh
  33. # Function: clean_hosts_file [file ...]
  34. clean_hosts_file() {
  35.   # The sed script cleans up the file.
  36.   # The awk script groups the hosts by ten items.
  37.   sed -e '/^127.0.0.1/b replace;
  38.           /^0.0.0.0/b replace;
  39.           :drop;
  40.             d; b;
  41.           :replace;
  42.             s/^0.0.0.0[[:space:]]*//;
  43.             s/^127.0.0.1[[:space:]]*//;
  44.             s/[[:space:]]*#.*\$//;
  45.             s/[[:space:]]*\$//;
  46.             s/[[:space:]][[:space:]]*/ /;
  47.             /^localhost\$/b drop;
  48.             /^[[:space:]]*\$/b drop;' \$* | \\
  49.   awk 'BEGIN {
  50.          # Read whitelist file.
  51.          n_whitelist = 0
  52.          while ( getline < "$BH_WHITELIST" ) {
  53.            if ( \$0 == "" ) {
  54.              break
  55.            }
  56.            else {
  57.              a_whitelist[++n_whitelist] = \$0
  58.            }
  59.          }
  60.          close("$BH_WHITELIST")
  61.          # Setup record sparator.
  62.          RS=" +"
  63.          c = 0
  64.        }
  65.        {
  66.          for ( n = 1; \$n != ""; n++ ) {
  67.            # Check whitelist.
  68.            whitelist_flag = 0
  69.            for ( w = 1; w <= n_whitelist; w++ ) {
  70.              if ( \$n ~ ( "^" a_whitelist[w] "\$" ) ) {
  71.                whitelist_flag = 1
  72.                break
  73.              }
  74.            }
  75.            if ( whitelist_flag == 0 ) {
  76.              hosts[++c] = \$n
  77.              if ( c == 10 ) {
  78.                s_hosts = "0.0.0.0"
  79.                for ( i = 1; i <= c; i++ ) {
  80.                  s_hosts = s_hosts " " hosts[i]
  81.                }
  82.                print s_hosts
  83.                c = 0
  84.              }
  85.            }
  86.          }
  87.        }
  88.        END {
  89.         if ( c > 0 ) {
  90.            s_hosts = "0.0.0.0"
  91.            for ( i = 1; i <= c; i++ ) {
  92.              s_hosts = s_hosts = s_hosts " " hosts[i]
  93.            }
  94.            print s_hosts
  95.          }
  96.        }'
  97. }
  98. # Function: wait_for_connection
  99. wait_for_connection() {
  100.   # Wait for an Internet connection.
  101.   # This possibly could take a long time.
  102.   while :; do
  103.     ping -c 1 -w 10 www.freebsd.org > /dev/null 2>&1 && break
  104.     sleep 10
  105.   done
  106. }
  107. # Set lock file.
  108. LOCK_FILE="/tmp/blocking_hosts.lock"
  109. # Check lock file.
  110. if [ ! -f "\$LOCK_FILE" ]; then
  111.   sleep \$((\$\$ % 5 + 5))
  112.   [ -f "\$LOCK_FILE" ] && exit 0
  113.   echo \$\$ > "\$LOCK_FILE"
  114.   # Start downloading files.
  115.   HOSTS_FILE_NUMBER=1
  116.   [ -d "/tmp/blocking_hosts" ] || mkdir "/tmp/blocking_hosts"
  117.   for URL in "http://winhelp2002.mvps.org/hosts.txt" \\
  118.              "http://someonewhocares.org/hosts/zero/hosts" \\
  119.              "http://jansal.googlecode.com/svn/trunk/adblock/hosts" \\
  120.              "http://adblock.gjtech.net/?format=hostfile" \\
  121.              "http://adaway.org/hosts.txt" \\
  122.              "http://www.malwaredomainlist.com/hostslist/hosts.txt" \\
  123.              "http://www.hostsfile.org/Downloads/hosts.txt"; do
  124.     HOSTS_FILE="/tmp/blocking_hosts/hosts\`printf '%02d' \$HOSTS_FILE_NUMBER\`"
  125.     logger "Downloading \$URL ..."
  126.     REPEAT=1
  127.     while :; do
  128.       # Wait for internet connection.
  129.       wait_for_connection
  130.       START_TIME=\`date +%s\`
  131.       # Create process to download a hosts file.
  132.       wget -O - "\$URL" 2> /dev/null > "\${HOSTS_FILE}.tmp" &
  133.       WGET_PID=\$!
  134.       WAIT_TIME=\$((\$REPEAT * 10 + 20))
  135.       # Create timeout process.
  136.       ( sleep \$WAIT_TIME; kill -TERM \$WGET_PID ) &
  137.       TIMEOUT_PID=\$!
  138.       wait \$WGET_PID
  139.       CURRENT_RC=\$?
  140.       kill -KILL \$TIMEOUT_PID
  141.       STOP_TIME=\`date +%s\`
  142.       if [ \$CURRENT_RC = 0 ]; then
  143.         clean_hosts_file "\${HOSTS_FILE}.tmp" > "\$HOSTS_FILE"
  144.         rm "\${HOSTS_FILE}.tmp"
  145.         break
  146.       fi
  147.       # In the case of an error: wait the remaining time.
  148.       TIME_SPAN=\$((\$STOP_TIME - \$START_TIME))
  149.       WAIT_TIME=\$((\$WAIT_TIME - \$TIME_SPAN))
  150.       [ \$WAIT_TIME -gt 0 ] && sleep \$WAIT_TIME
  151.       # Increase the number of repeats.
  152.       REPEAT=\$((\$REPEAT + 1))
  153.       [ \$REPEAT = 4 ] && break
  154.     done
  155.     HOSTS_FILE_NUMBER=\$((\$HOSTS_FILE_NUMBER + 1))
  156.   done
  157.   # Inspect downloaded hosts files.
  158.   ANY_FILE_OK=1
  159.   DNSMASQ_PARAM=""
  160.   for HOSTS_FILE in /tmp/blocking_hosts/hosts[0-9][0-9]; do
  161.     if [ -s "\$HOSTS_FILE" ]; then
  162.       ANY_FILE_OK=0
  163.       DNSMASQ_PARAM=\${DNSMASQ_PARAM:+\$DNSMASQ_PARAM }"--addn-hosts=\$HOSTS_FILE"
  164.     else
  165.       rm "\$HOSTS_FILE"
  166.     fi
  167.   done
  168.   if [ \$ANY_FILE_OK = 0 ]; then
  169.     logger "Restarting dnsmasq with additional hosts file(s) ..."
  170.     killall -TERM dnsmasq
  171.     dnsmasq --conf-file=/tmp/dnsmasq.conf \$DNSMASQ_PARAM &
  172.   fi
  173.   rm "\$LOCK_FILE"
  174. fi
  175. EOF
  176. # Make it executeable.
  177. chmod 755 "$BH_SCRIPT"
  178. # Add crontab entry.
  179. grep -q "$BH_SCRIPT" /tmp/crontab || echo "$(($$ % 60)) 3 * * * root $BH_SCRIPT" >>/tmp/crontab
  180. # Execute script in background.
  181. sh "$BH_SCRIPT" &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement