#!/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:
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.
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.
Scheduling:
Schedule the script to run at the desired frequency using cron or another task scheduler. The provided crontab example runs it hourly.
Securing:
Secure the log file by setting appropriate file permissions to prevent unauthorized access, as it might contain sensitive information.
Important Notes:
Permissions and Paths:
/var/log/privoxy_blacklist_update.log
) should be writable by the user running the script.Testing:
Scheduling:
Securing: