[Script] Block ads, fake news etc

I got inspired by this and this situation.

I wrote it for myself, but decided to share with community.

Here is is
#!/bin/sh
# Ultimate blocker installer for OpenWrt
# Date: 2025-10-30
 
echo "Ultimate blocker"
echo "This script will automatically install ad blocking hosts, block tracking and will attempt to block fake news/propaganda"
echo "NOT DESIGNED FOR ru REGION. Script blocks .ru and .su to avoid propaganda"
printf "Continue? (y/n) "
read ans
case "$ans" in
  y|Y) ;;
  *) echo "Aborted."; exit 0 ;;
esac
 
TMP_HOSTS="/tmp/ub_hosts.txt"
TARGET_HOSTS="/etc/hosts"
DNSMASQ_DIR="/etc/dnsmasq.conf.d"
DNSMASQ_FILE="${DNSMASQ_DIR}/ultimate-blocker.conf"
BACKUP_DIR="/etc/ultimate-blocker-backup-$(date +%s)"
 
# Ensure dnsmasq conf dir exists
mkdir -p "$DNSMASQ_DIR"
 
# Try primary download, fallback to mirror
rm -f "$TMP_HOSTS"
echo "Downloading hosts list..."
if ! curl -fsSL "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews/hosts" -o "$TMP_HOSTS"; then
  echo "Primary failed, trying mirror..."
  if ! curl -fsSL "http://sbc.io/hosts/alternates/fakenews/hosts" -o "$TMP_HOSTS"; then
    echo "Both downloads failed. Proceeding with local additions only."
    rm -f "$TMP_HOSTS"
  fi
fi
 
# Backup current hosts and dnsmasq conf if present
mkdir -p "$BACKUP_DIR"
cp -a "$TARGET_HOSTS" "$BACKUP_DIR/hosts.orig" 2>/dev/null || true
if [ -f "$DNSMASQ_FILE" ]; then
  cp -a "$DNSMASQ_FILE" "$BACKUP_DIR/ultimate-blocker.conf.orig" 2>/dev/null || true
fi
 
TMP_ADD="/tmp/ub_additions.txt"
rm -f "$TMP_ADD"
touch "$TMP_ADD"
 
# If downloaded exist, sanitize it
if [ -s "$TMP_HOSTS" ]; then
  # Syntax tweaks
  awk '
    /^[[:space:]]*#/ { next }
    /^[[:space:]]*$/ { next }
    {
      # remove inline comments
      gsub(/#.*/,"")
      # trim
      sub(/^[ \t]+/, "")
      sub(/[ \t]+$/, "")
      if ($1 ~ /^(0\.0\.0\.0|127\.0\.0\.1)$/ && $2 != "") {
        print $1 " " $2
      }
    }
  ' "$TMP_HOSTS" >> "$TMP_ADD"
fi
 
# Add GL-iNet ad block
grep -q -F "ad-api.gl-inet.com" "$TARGET_HOSTS" 2>/dev/null || echo "0.0.0.0 ad-api.gl-inet.com" >> "$TMP_ADD"
grep -q -F "static.gl-inet.com" "$TARGET_HOSTS" 2>/dev/null || echo "0.0.0.0 static.gl-inet.com" >> "$TMP_ADD"
 
# Remove duplicates
if [ -s "$TMP_ADD" ]; then
  # create a temp file
  awk '
    { print }
  ' "$TARGET_HOSTS" 2>/dev/null > /tmp/ub_hosts_orig || true
 
  # Append only lines not already present
  while read -r line; do
    [ -z "$line" ] && continue
    # escape for grep
    if ! grep -Fxq "$line" /tmp/ub_hosts_orig 2>/dev/null; then
      echo "$line" >> /tmp/ub_hosts_orig
    fi
  done < "$TMP_ADD"
 
  # Write merged hosts back
  cp /tmp/ub_hosts_orig "$TARGET_HOSTS"
  echo "Updated $TARGET_HOSTS"
fi
 
# Block ru propaganda
cat > "$DNSMASQ_FILE" <<'EOF'
address=/.ru/0.0.0.0
address=/.su/0.0.0.0
address=/.ru/:: 
address=/.su/::
EOF
 
# permissions :>
chmod 644 "$DNSMASQ_FILE" 2>/dev/null || true
 
# Restart dnsmasq
if /etc/init.d/dnsmasq status >/dev/null 2>&1; then
  /etc/init.d/dnsmasq restart || {
    echo "Warning: failed to restart dnsmasq. Try: /etc/init.d/dnsmasq restart"
  }
else
  # Try generic restart
  if command -v systemctl >/dev/null 2>&1; then
    systemctl restart dnsmasq || true
  fi
fi
 
echo "Crap successfully blocked! Now you are more safe!"
 
# Self-remove script not to clutter your router
SELF="$(readlink -f "$0" 2>/dev/null || echo "$0")"
if [ -f "$SELF" ]; then
  ( sleep 1; rm -f "$SELF" ) &
fi
 
exit 0

It will be good for those who don't have Adguard home (ex E750v2) or don't use it to save some computing power.

If something, ping me i will try to fix script

1 Like

Great! Thank you for your sharing!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.