socialmedia-blocker-linux/extend-domains

68 lines
1.9 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Must run as root
if [[ $EUID -ne 0 ]]; then
echo "🚨 This script must be run as root."
echo "👉 Please run it using: sudo $0"
exit 1
fi
# Validate arguments
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <FileName> <Type>"
echo "Example: $0 Hosts.txt adultsites"
exit 1
fi
HOSTS_FILE="$1"
CATEGORY="$2"
DOMAINS_FILE="domains.txt"
TMP_DOMAINS=$(mktemp)
# Validate input file
if [[ ! -f "$HOSTS_FILE" ]]; then
echo "🚨 The file '$HOSTS_FILE' does not exist."
exit 1
fi
# Backup domains.txt
cp "$DOMAINS_FILE" "${DOMAINS_FILE}.bak"
# Prepare category comment
CATEGORY_TAG="#$CATEGORY"
# Extract domains
while IFS= read -r line || [[ -n "$line" ]]; do
domain=$(echo "$line" | sed -E 's/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+//; s/\s+//; s/^#.*//')
domain=$(echo "$domain" | sed 's/^[ \t]*//;s/[ \t]*$//') # trim
[[ "$domain" =~ ^[a-zA-Z0-9.-]+$ && "$domain" != "localhost" ]] || continue
# Avoid duplicates
if ! grep -qwF "$domain" "$DOMAINS_FILE"; then
echo "$domain" >> "$TMP_DOMAINS"
fi
done < "$HOSTS_FILE"
# Insert domains under category
if [[ -s "$TMP_DOMAINS" ]]; then
if grep -Fxq "$CATEGORY_TAG" "$DOMAINS_FILE"; then
# Find line number of category tag
LINE_NUM=$(grep -Fnx "$CATEGORY_TAG" "$DOMAINS_FILE" | cut -d: -f1)
# Split file and insert domains
head -n "$LINE_NUM" "$DOMAINS_FILE" > "${DOMAINS_FILE}.new"
cat "$TMP_DOMAINS" >> "${DOMAINS_FILE}.new"
tail -n +$((LINE_NUM + 1)) "$DOMAINS_FILE" >> "${DOMAINS_FILE}.new"
mv "${DOMAINS_FILE}.new" "$DOMAINS_FILE"
else
echo -e "\n$CATEGORY_TAG" >> "$DOMAINS_FILE"
cat "$TMP_DOMAINS" >> "$DOMAINS_FILE"
fi
echo "✅ New domains added under $CATEGORY_TAG in '$DOMAINS_FILE'."
else
echo " No new domains to add."
fi
rm "$TMP_DOMAINS"
echo " Run sudo ./blocker to add entries to /etc/hosts"