# --- COPY THE TEXT BELOW TO DD-WRT / ADMINISTRATION / COMMANDS then click SAVE FIREWALL --- # --- WARNING: This script has enabled extra ad repositories that can be taxing on memory --- # --- It is recommended to not use this specific script on a router with 32MB ram or less. Recommended 64MB or more. BH_SCRIPT="/tmp/blocking_hosts.sh" BH_WHITELIST="/tmp/blocking_hosts.whitelist" logger "Download blocking hosts file and restart dnsmasq ..." # Create whitelist. The whitelist entries will be removed from the # hosts files, i.e. blacklist files. cat > "$BH_WHITELIST" < "$BH_SCRIPT" < 0 ) { s_hosts = "0.0.0.0" for ( i = 1; i <= c; i++ ) { s_hosts = s_hosts = s_hosts " " hosts[i] } print s_hosts } }' } # Function: wait_for_connection wait_for_connection() { # Wait for an Internet connection. # This possibly could take a long time. while :; do ping -c 1 -w 10 www.freebsd.org > /dev/null 2>&1 && break sleep 10 done } # Set lock file. LOCK_FILE="/tmp/blocking_hosts.lock" # Check lock file. if [ ! -f "\$LOCK_FILE" ]; then sleep \$((\$\$ % 5 + 5)) [ -f "\$LOCK_FILE" ] && exit 0 echo \$\$ > "\$LOCK_FILE" # Start downloading files. HOSTS_FILE_NUMBER=1 [ -d "/tmp/blocking_hosts" ] || mkdir "/tmp/blocking_hosts" for URL in "http://winhelp2002.mvps.org/hosts.txt" \\ "http://someonewhocares.org/hosts/zero/hosts" \\ "http://jansal.googlecode.com/svn/trunk/adblock/hosts" \\ "http://adblock.gjtech.net/?format=hostfile" \\ "http://adaway.org/hosts.txt" \\ "http://www.malwaredomainlist.com/hostslist/hosts.txt" \\ "http://www.hostsfile.org/Downloads/hosts.txt"; do HOSTS_FILE="/tmp/blocking_hosts/hosts\`printf '%02d' \$HOSTS_FILE_NUMBER\`" logger "Downloading \$URL ..." REPEAT=1 while :; do # Wait for internet connection. wait_for_connection START_TIME=\`date +%s\` # Create process to download a hosts file. wget -O - "\$URL" 2> /dev/null > "\${HOSTS_FILE}.tmp" & WGET_PID=\$! WAIT_TIME=\$((\$REPEAT * 10 + 20)) # Create timeout process. ( sleep \$WAIT_TIME; kill -TERM \$WGET_PID ) & TIMEOUT_PID=\$! wait \$WGET_PID CURRENT_RC=\$? kill -KILL \$TIMEOUT_PID STOP_TIME=\`date +%s\` if [ \$CURRENT_RC = 0 ]; then clean_hosts_file "\${HOSTS_FILE}.tmp" > "\$HOSTS_FILE" rm "\${HOSTS_FILE}.tmp" break fi # In the case of an error: wait the remaining time. TIME_SPAN=\$((\$STOP_TIME - \$START_TIME)) WAIT_TIME=\$((\$WAIT_TIME - \$TIME_SPAN)) [ \$WAIT_TIME -gt 0 ] && sleep \$WAIT_TIME # Increase the number of repeats. REPEAT=\$((\$REPEAT + 1)) [ \$REPEAT = 4 ] && break done HOSTS_FILE_NUMBER=\$((\$HOSTS_FILE_NUMBER + 1)) done # Inspect downloaded hosts files. ANY_FILE_OK=1 DNSMASQ_PARAM="" for HOSTS_FILE in /tmp/blocking_hosts/hosts[0-9][0-9]; do if [ -s "\$HOSTS_FILE" ]; then ANY_FILE_OK=0 DNSMASQ_PARAM=\${DNSMASQ_PARAM:+\$DNSMASQ_PARAM }"--addn-hosts=\$HOSTS_FILE" else rm "\$HOSTS_FILE" fi done if [ \$ANY_FILE_OK = 0 ]; then logger "Restarting dnsmasq with additional hosts file(s) ..." killall -TERM dnsmasq dnsmasq --conf-file=/tmp/dnsmasq.conf \$DNSMASQ_PARAM & fi rm "\$LOCK_FILE" fi EOF # Make it executeable. chmod 755 "$BH_SCRIPT" # Add crontab entry. grep -q "$BH_SCRIPT" /tmp/crontab || echo "$(($$ % 60)) 3 * * * root $BH_SCRIPT" >>/tmp/crontab # Execute script in background. sh "$BH_SCRIPT" &