Closed fabriziosalmi closed 11 months ago
#!/bin/bash
echo "Setup script"
# Detect package manager and set command maps
declare -A CMD_MAP
if command -v apt-get &>/dev/null; then
CMD_MAP[PACKAGE_MANAGER]="apt-get"
CMD_MAP[UPDATE]="sudo apt-get update"
CMD_MAP[INSTALL]="sudo apt-get install -y"
elif command -v apk &>/dev/null; then
CMD_MAP[PACKAGE_MANAGER]="apk"
CMD_MAP[UPDATE]="sudo apk update"
CMD_MAP[INSTALL]="sudo apk add --no-cache"
else
echo "Unsupported package manager. Exiting."
exit 1
fi
install_package() {
local package=$1
if ! ${CMD_MAP[INSTALL]} "$package"; then
echo "Failed to install '$package' using ${CMD_MAP[PACKAGE_MANAGER]}."
exit 1
fi
}
# Update and install prerequisites
${CMD_MAP[UPDATE]}
install_package python3
# Link python3 to python (for Ubuntu, since Alpine doesn't have python2 by default)
if [ "${CMD_MAP[PACKAGE_MANAGER]}" == "apt-get" ] && [ ! -e /usr/bin/python ]; then
sudo ln -s /usr/bin/python3 /usr/bin/python
fi
python3 -m ensurepip --upgrade
pip3 install --no-cache-dir --upgrade pip setuptools tldextract tqdm
# Install pv and ncftp based on the detected package manager
for package in pv ncftp; do
install_package "$package"
done
BLACKLIST_URLS_FILE="blacklists.fqdn.urls"
# Function to download a URL
download_url() {
local url="$1"
echo "Blacklist: $url"
local random_filename=$(uuidgen | tr -dc '[:alnum:]')
if ! wget -q --progress=bar:force -O "${random_filename}.fqdn.list" "$url"; then
echo "Failed to download: $url"
fi
}
echo "Download blacklists"
while IFS= read -r url; do
download_url "$url"
done < "$BLACKLIST_URLS_FILE"
FILES=$(ls -- *.fqdn.list)
echo "Aggregate blacklists"
touch aggregated.fqdn.list
for file in $FILES; do
cat "$file" | sudo tee -a aggregated.fqdn.list > /dev/null
done
sudo cat aggregated.fqdn.list | sort -u | sudo tee all.fqdn.blacklist > /dev/null
sudo rm ./*.fqdn.list
echo "Sanitize blacklists"
mv all.fqdn.blacklist input.txt
python sanitize.py
mv output.txt all.fqdn.blacklist
echo "Remove whitelisted domains"
mv all.fqdn.blacklist blacklist.txt
python whitelist.py
mv filtered_blacklist.txt all.fqdn.blacklist
rm blacklist.txt input.txt
echo "Create compressed file"
if ! tar -czf all.fqdn.blacklist.tar.gz "all.fqdn.blacklist"; then
echo "Error: Failed to create the tar.gz file."
exit 1
fi
total_lines_new=$(wc -l < all.fqdn.blacklist)
echo "Total domains: $total_lines_new."
This should clear security notes and warnings:
Here's the revised version of the script:
Changes Made:
--
before*.fqdn.list
in thels
command to ensure filenames starting with dashes aren't interpreted as options.$?
.echo
andcat
operations into a single group and redirected their combined output.sudo
with redirections tosudo tee
to avoid permission issues.