What's new

Adblocking Script No Longer Works on 378.56_2 - Please Assist

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Llama

Occasional Visitor
Hi All,

I had successfully "ported" an older DD-WRT-based adblocking script, http://www.dd-wrt.com/wiki/index.php/Ad_blocking, by revising the line:
Code:
dnsmasq --conf-file=/tmp/dnsmasq.conf \$DNSMASQ_PARAM &
to:
Code:
dnsmasq --conf-file=/tmp/etc/dnsmasq.conf \$DNSMASQ_PARAM &
The firewall-start script in /jffs/scripts was working swimmingly.

However, a few days after the latest FW upgrade to 378.56_2, the script no longer worked. Now, it is stumping me completely.

Can someone offer assistance to fix up this fine script?

The script in its entirety is:
Code:
#!/bin/sh
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" <<EOF
localhost\\.localdomain
local
invalid
whitelist-example\\.com
.*\\.whitelist-example\\.com
EOF
# Create download script.
cat > "$BH_SCRIPT" <<EOF
#!/bin/sh
# Function: clean_hosts_file [file ...]
clean_hosts_file() {
  # The sed script cleans up the file.
  # The awk script groups the hosts by ten items.
  sed -e '/^127.0.0.1/b replace;
          /^0.0.0.0/b replace;
          :drop;
            d; b;
          :replace;
            s/^0.0.0.0[[:space:]]*//;
            s/^127.0.0.1[[:space:]]*//;
            s/[[:space:]]*#.*\$//;
            s/[[:space:]]*\$//;
            s/[[:space:]][[:space:]]*/ /;
            /^localhost\$/b drop;
            /^[[:space:]]*\$/b drop;' \$* | \\
  awk 'BEGIN {
         # Read whitelist file.
         n_whitelist = 0
         while ( getline < "$BH_WHITELIST" ) {
           if ( \$0 == "" ) {
             break
           }
           else {
             a_whitelist[++n_whitelist] = \$0
           }
         }
         close("$BH_WHITELIST")
         # Setup record sparator.
         RS=" +"
         c = 0
       }
       {
         for ( n = 1; \$n != ""; n++ ) {
           # Check whitelist.
           whitelist_flag = 0
           for ( w = 1; w <= n_whitelist; w++ ) {
             if ( \$n ~ ( "^" a_whitelist[w] "\$" ) ) {
               whitelist_flag = 1
               break
             }
           }
           if ( whitelist_flag == 0 ) {
             hosts[++c] = \$n
             if ( c == 10 ) {
               s_hosts = "0.0.0.0"
               for ( i = 1; i <= c; i++ ) {
                 s_hosts = s_hosts " " hosts[i]
               }
               print s_hosts
               c = 0
             }
           }
         }
       }
       END {
        if ( c > 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://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/etc/dnsmasq.conf \$DNSMASQ_PARAM &
  fi
  rm "\$LOCK_FILE"
fi
EOF
# Make it executable.
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" &
 
You don't give too many clues as to what is wrong! I wouldn't put this in the firewall script - that gets called quite early and possibly many times. I also wouldn't kill the dnsmasq which is started by the firmware - you can use the custom scripts to add to the system dnsmasq conf which would ensure it gets used every time the system restarts dnsmasq. You have to be careful not to break dnsmasq by invalid commands in the conf file, your users get upset by that!
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top