[Script] Adblock primitive

Asked my friend to write really simple, setup and forget script. No settings, no nothing.

(Yes she allowed me to publish it here)

Script for geeks
#!/bin/sh

hosts_url="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
hosts_file="/tmp/adblock-hosts"
update_script_path="/usr/bin/update_hosts.sh"
dnsmasq_conf="/etc/dnsmasq.conf"

if ! which wget >/dev/null 2>&1; then
  echo "wget is not installed. Installing..."
  opkg update
  opkg install wget
  if [ $? -ne 0 ]; then
    echo "Error installing wget. Please check your internet connection and try again."
    exit 1
  fi
  echo "wget installed successfully."
else
  echo "wget is already installed."
fi

cat <<EOF > "$update_script_path"
#!/bin/sh
wget -q -O "$hosts_file" "$hosts_url"
EOF
chmod +x "$update_script_path"

if ! grep -q "addn-hosts=$hosts_file" "$dnsmasq_conf"; then
  echo "addn-hosts=$hosts_file" >> "$dnsmasq_conf"
  /etc/init.d/dnsmasq restart
fi

(crontab -l 2>/dev/null; echo "@reboot $update_script_path"; echo "0 0 * * * $update_script_path") | crontab -

rm "/tmp/setup_hosts.sh"

echo "Hosts update setup complete and self-removal successful."
echo "The update script is located at: $update_script_path (persistent)"
echo "It will run on reboot and at midnight."
echo "Additional hosts file is saved in RAM at: $hosts_file"
echo "Dnsmasq configured to use it."

To run it just use this command:

sh <(wget -qO- https://pastebin.com/raw/jrT2JmpN)
2 Likes

Well, that's debatable. Have you actually tested how this behaves when you restart the router?

Busybox crond does not support the @reboot shortcut. The adblock-hosts file is located in RAM and will be missing when dnsmasq starts, so it will be ignored.

Also, dnsmasq reads the additional host files during service startup. The midnight update is useless without a service reload.

In saying that, I don't mean to belittle your friend's hard work. Impressive scripting skills.

3 Likes

Is this for real ? package wget does not support https, additionally lobitomizing opkg. Thats in first 10 lines of the script.