fabriziosalmi / blacklists

Hourly updated domains blacklist 🚫
https://github.com/fabriziosalmi/blacklists/releases/download/latest/blacklist.txt
GNU General Public License v3.0
113 stars 5 forks source link

Privoxy #83

Closed fabriziosalmi closed 5 months ago

fabriziosalmi commented 10 months ago
#!/bin/bash

# URL of your remote blacklist.txt
BLACKLIST_URL="http://example.com/path/to/blacklist.txt"

# Path to Privoxy's user.action file
USER_ACTION_FILE="/etc/privoxy/user.action"

# Path to the log file
LOG_FILE="/var/log/privoxy_blacklist_update.log"

# Fetch the latest blacklist and format it for inclusion in user.action
echo "$(date): Updating Privoxy blacklist from $BLACKLIST_URL..." >> "$LOG_FILE"

# Fetching blacklist
formatted_blacklist=$(curl -s "$BLACKLIST_URL" | sed 's/^/.&/' | sed 's/$/& /')

# Check if curl command failed, or if the fetched blacklist is empty
if [[ $? -ne 0 || -z "$formatted_blacklist" ]]; then
  echo "$(date): Failed to update blacklist. Either fetch failed or the fetched list is empty." >> "$LOG_FILE"
  exit 1
fi

# Create a backup of the current user.action file
if ! cp "$USER_ACTION_FILE" "$USER_ACTION_FILE.bak"; then
  echo "$(date): Failed to create a backup of user.action file." >> "$LOG_FILE"
  exit 1
fi

# Generate a new user.action file
awk -v bl="$formatted_blacklist" '/{ +block{Blacklisted domain} }/{print;print bl;next}1' "$USER_ACTION_FILE.bak" > "$USER_ACTION_FILE.tmp"

# Check if awk command failed
if [[ $? -ne 0 ]]; then
  echo "$(date): Failed to generate new user.action file." >> "$LOG_FILE"
  exit 1
fi

# Replace old user.action file with the new one
if ! mv "$USER_ACTION_FILE.tmp" "$USER_ACTION_FILE"; then
  echo "$(date): Failed to replace old user.action file with the new one." >> "$LOG_FILE"
  exit 1
fi

# Restart Privoxy to apply changes
if ! service privoxy restart; then
  echo "$(date): Failed to restart Privoxy service." >> "$LOG_FILE"
  exit 1
fi

echo "$(date): Privoxy blacklist update completed successfully." >> "$LOG_FILE"

Important Notes:

  1. Permissions and Paths:

    • Make sure the script has the proper permissions and is executable.
    • Ensure the script is run by a user with sufficient privileges to read and write the Privoxy configuration and restart the service.
    • The log file path (/var/log/privoxy_blacklist_update.log) should be writable by the user running the script.
  2. Testing:

    • Test this script in a safe environment before deploying it in production to avoid any service disruption.
    • Check the log file for any errors during the execution of the script.
  3. Scheduling:

    • Schedule the script to run at the desired frequency using cron or another task scheduler. The provided crontab example runs it hourly.
  4. Securing:

    • Secure the log file by setting appropriate file permissions to prevent unauthorized access, as it might contain sensitive information.
fabriziosalmi commented 5 months ago

moved to docs: https://github.com/fabriziosalmi/blacklists/wiki/Documentation#privoxy