Skip to content

Instantly share code, notes, and snippets.

@AliKhadivi
Created March 9, 2023 14:01
Show Gist options
  • Save AliKhadivi/31f91331431fe55bee65303e4b2040b6 to your computer and use it in GitHub Desktop.
Save AliKhadivi/31f91331431fe55bee65303e4b2040b6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# color codes
green='\033[0;32m'
red='\033[0;31m'
yellow='\033[0;33m'
cyan='\033[0;36m'
reset='\033[0m'
# Show error message and exit
abort() {
echo -e "${red}--X $1 ${reset}"
#exit 1
}
# Show information message
info() {
echo -e "${cyan}--> $1 ${reset}"
}
# Show warning message
warn() {
echo -e "${yellow}--! $1 ${reset}"
}
# Show success message
success() {
echo -e "${green}--:) $1 ${reset}"
}
# Check root access
if [[ $EUID -ne 0 ]]; then
abort "This script needs to be run with superuser privileges."
exit 1
fi
date="$(date | openssl md5 -r)"
cp /etc/nginx/arvan.conf "/etc/nginx/arvan.${data}.back" -f
#curl -L https://www.arvancloud.ir/fa/ips.txt | while read line; do echo "allow ${line};"; done > /etc/nginx/arvan.conf
IPsLink="https://www.arvancloud.ir/fa/ips.txt"
IPsFile=$(mktemp "/tmp/ar-ips.XXXXX")
if [[ -x "$(command -v curl)" ]]; then
downloadStatus=$(curl "${IPsLink}" -o "${IPsFile}" -L -s -w "%{http_code}\n")
elif [[ -x "$(command -v wget)" ]]; then
downloadStatus=$(wget "${IPsLink}" -O "${IPsFile}" --server-response 2>&1 | awk '/^ HTTP/{print $2}' | tail -n1)
else
abort "curl or wget is required to run this script."
exit 1
fi
if [[ "$downloadStatus" -ne 200 ]]; then
abort "Downloading the IP list wasn't successful. status code: ${downloadStatus}"
exit 1
else
success "Get iplist successfuly!"
fi
info "Applying ips..."
cat "${IPsFile}" | while read line; do echo "allow ${line};"; done > /etc/nginx/arvan.conf
rm -rf ${IPsFile}
if nginx_out=$(nginx -t 2>&1); then
info "Reloading Nginx..."
nginx -s reload
success "Nginx reloaded!"
else
echo "Nginx configuration failed!"
warn "Detail:"
echo "$nginx_out"
rm -f /etc/nginx/arvan.conf
mv "/etc/nginx/arvan.${data}.back" /etc/nginx/arvan.conf
warn "Applying role back!"
fi
rm -f "/etc/nginx/arvan.${data}.back"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment