mirror of
https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker.git
synced 2025-09-02 18:50:13 +00:00
Merge pull request #352 from itoffshore/alpine
Revert FreeBSD compatibility
This commit is contained in:
commit
6d437dc972
3 changed files with 32 additions and 77 deletions
|
@ -36,10 +36,8 @@ CONF_DIR=/etc/nginx/conf.d
|
|||
BOTS_DIR=/etc/nginx/bots.d
|
||||
SCRIPT_DIR=/usr/local/sbin
|
||||
REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master
|
||||
SUPPRESS_SCRIPTS=false
|
||||
|
||||
####### end user configuration ##########################
|
||||
OS=$(uname -s)
|
||||
|
||||
usage() {
|
||||
local script=$(basename $0)
|
||||
|
@ -50,7 +48,6 @@ Usage: $script [OPTIONS]
|
|||
[ -b ] : Bot rules directory (default: $BOTS_DIR)
|
||||
[ -c ] : NGINX conf directory (default: $CONF_DIR)
|
||||
[ -s ] : Script directory (default: $SCRIPT_DIR)
|
||||
[ -S ] : Suppress script download (default: $SUPPRESS_SCRIPTS)
|
||||
[ -r ] : Change repo url (default: $REPO)
|
||||
[ -x ] : Actually change the files (default: don't change anything)
|
||||
[ -q ] : Suppress non error messages
|
||||
|
@ -209,7 +206,7 @@ print_message() {
|
|||
get_options() {
|
||||
local arg= opts=
|
||||
|
||||
while getopts :b:c:s:Sr:xvqh opts "$@"
|
||||
while getopts :b:c:s:r:xvqh opts "$@"
|
||||
do
|
||||
if [ -n "${OPTARG}" ]; then
|
||||
case "$opts" in
|
||||
|
@ -222,7 +219,6 @@ get_options() {
|
|||
b) BOTS_DIR=$arg; check_args $opts path $arg ;;
|
||||
c) CONF_DIR=$arg; check_args $opts path $arg ;;
|
||||
s) SCRIPT_DIR=$arg; check_args $opts path $arg ;;
|
||||
S) SUPPRESS_SCRIPTS=true
|
||||
r) REPO=$arg; check_args $opts url $arg ;;
|
||||
x) DRY_RUN=N ;;
|
||||
v) check_version ;;
|
||||
|
@ -271,8 +267,6 @@ check_depends() {
|
|||
fi
|
||||
done
|
||||
|
||||
case $OS in
|
||||
Linux)
|
||||
# give a helpful message for missing pidof
|
||||
if [ -z $(find_binary pidof) ]; then
|
||||
printf "$0 requires 'pidof' \n\n"
|
||||
|
@ -280,16 +274,6 @@ check_depends() {
|
|||
printf "In Centos: yum install sysvinit-tools\n"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
*BSD)
|
||||
# give a helpful message for missing gsed
|
||||
if [ -z $(find_binary gsed) ]; then
|
||||
printf "$0 requires 'gsed' \n\n"
|
||||
printf "In FreeBSD: 'pkg install textproc/gsed' or 'portmaster textproc/gsed'\n"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
check_online() {
|
||||
|
@ -344,14 +328,12 @@ main() {
|
|||
check_config $CONF_DIR $BOTS_DIR $SCRIPT_DIR
|
||||
download_files conf.d $CONF_DIR $CONF_FILES
|
||||
download_files bots.d $BOTS_DIR $BOT_FILES
|
||||
if [ $SUPPRESS_SCRIPTS = false ]; then
|
||||
download_files / $SCRIPT_DIR $SCRIPT_FILES
|
||||
|
||||
# ensures scripts are executable
|
||||
if [ "$DRY_RUN" = "N" ]; then
|
||||
set_mode 700 $SCRIPT_DIR $SCRIPT_FILES
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
## START ##
|
||||
|
|
|
@ -28,7 +28,6 @@ BOLDRED="\033[1m\033[31m"
|
|||
BOLDYELLOW="\033[1m\033[33m"
|
||||
BOLDWHITE="\033[1m\033[37m"
|
||||
RESET="\033[0m"
|
||||
OS=$(uname -s)
|
||||
|
||||
usage() {
|
||||
local script=$(basename $0)
|
||||
|
@ -72,7 +71,7 @@ update_paths() {
|
|||
|
||||
for x in $include_paths; do
|
||||
dir=$(dirname $x)
|
||||
$SED_CMD -i "s|$dir|$BOTS_DIR|" $blacklist
|
||||
sed -i "s|$dir|$BOTS_DIR|" $blacklist
|
||||
done
|
||||
else
|
||||
printf "${BOLDRED}ERROR${RESET}: '$BOTS_DIR' does not exist => ${BOLDWHITE}running $INSTALLER${RESET}.\n"
|
||||
|
@ -197,7 +196,7 @@ add_includes() {
|
|||
printf "%-10s %-${col_size}s %s\n" "inserting:" "$text" "=> $file"
|
||||
if [ "$DRY_RUN" = "N" ]; then
|
||||
# $ph is just a placeholder so sed inserts a \t (tab)
|
||||
$SED_CMD -i "$line i $ph \t$text $ph" $file
|
||||
sed -i "$line i $ph \t$text $ph" $file
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
@ -206,17 +205,17 @@ add_includes() {
|
|||
if [ -n "$update" ]; then
|
||||
#add blank line below inserts
|
||||
line=$(( $line + $(echo $include_list | wc -w) ))
|
||||
if ! $SED_CMD -n "${line}p" $file | grep ^'}' 1>/dev/null; then
|
||||
if ! sed -n "${line}p" $file | grep ^'}' 1>/dev/null; then
|
||||
text="include $conf_dir/$(echo $include_list | awk '{print $1}');"
|
||||
$SED_CMD -i "s|$text|$text\n|" $file
|
||||
sed -i "s|$text|$text\n|" $file
|
||||
fi
|
||||
|
||||
#add comment above inserts
|
||||
text="include $conf_dir/$(echo $include_list | awk '{print $NF}');"
|
||||
$SED_CMD -i "s|$text|\n\n ##\n # Nginx Bad Bot Blocker Includes\n # REPO: https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker\n ##\n\t$text|" $file
|
||||
sed -i "s|$text|\n\n ##\n # Nginx Bad Bot Blocker Includes\n # REPO: https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker\n ##\n\t$text|" $file
|
||||
|
||||
# remove placeholders
|
||||
$SED_CMD -i "s|$ph||g" $file
|
||||
sed -i "s|$ph||g" $file
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
@ -239,7 +238,7 @@ find_includes() {
|
|||
local end=$(find_line $file $end_range $end_first_last)
|
||||
|
||||
if [ -n "$start" ] && [ -n "$end" ]; then
|
||||
$SED_CMD -n "$start,$end"p $file > $tmp
|
||||
sed -n "$start,$end"p $file > $tmp
|
||||
line=$(find_line $tmp $search $search_first_last)
|
||||
rm -f $tmp
|
||||
fi
|
||||
|
@ -255,9 +254,9 @@ find_includes() {
|
|||
esac
|
||||
|
||||
# if inserting beyond the end of the stanza
|
||||
if [ "$($SED_CMD -n $(( $line - 1))p $file | grep ^})" = "}" ]; then
|
||||
if [ "$(sed -n $(( $line - 1))p $file | grep ^})" = "}" ]; then
|
||||
# insert blank line
|
||||
$SED_CMD -i "$(( line - 1)) i \ " $file
|
||||
sed -i "$(( line - 1)) i \ " $file
|
||||
fi
|
||||
|
||||
echo $line
|
||||
|
@ -311,21 +310,6 @@ check_depends() {
|
|||
exit 1
|
||||
fi
|
||||
|
||||
case $OS in
|
||||
Linux)
|
||||
SED_CMD=$(find_binary sed)
|
||||
printf "${BOLDRED}ERROR${RESET}: $0 requires: 'sed' => ${BOLDWHITE}please install sed.${RESET}\n"
|
||||
exit 1
|
||||
;;
|
||||
*BSD)
|
||||
SED_CMD=$(find_binary gsed)
|
||||
if [ -z $SED_CMD ]; then
|
||||
printf "${BOLDRED}ERROR${RESET}: $0 requires: 'gsed' => ${BOLDWHITE}please install textproc/gsed.${RESET}\n"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
# required for whitelisting public ip
|
||||
if [ -z $(find_binary dig) ]; then
|
||||
printf "${BOLDYELLOW}WARN${RESET}: $0 optionally requires: 'dig' => ${BOLDWHITE}cannot whitelist public ip address.${RESET}\n"
|
||||
|
@ -348,7 +332,7 @@ check_nginx_directives() {
|
|||
printf "${BOLDYELLOW}setup will fix conflict from: '$x' in $bot_config${RESET}\n"
|
||||
if [ "$DRY_RUN" = "N" ]; then
|
||||
printf "${BOLDRED}disabling '$x' in: $bot_config${RESET}\n"
|
||||
$SED_CMD -i "s/$x/#$x/g" $bot_config | grep $x
|
||||
sed -i "s/$x/#$x/g" $bot_config | grep $x
|
||||
printf " ${BOLDGREEN}disabled OK${RESET}\n\n"
|
||||
fi
|
||||
fi
|
||||
|
@ -440,7 +424,7 @@ main() {
|
|||
|
||||
# configure ddos include
|
||||
case "$INC_DDOS" in
|
||||
n*|N*) VHOST_INCLUDES=$(echo $VHOST_INCLUDES | $SED_CMD 's|ddos.conf||');;
|
||||
n*|N*) VHOST_INCLUDES=$(echo $VHOST_INCLUDES | sed 's|ddos.conf||');;
|
||||
esac
|
||||
|
||||
# by default do not change any files
|
||||
|
|
|
@ -91,14 +91,14 @@ check_version() {
|
|||
|
||||
if [ -f $file ]; then
|
||||
# local version
|
||||
version=$(grep "Version:" $file | $SED_CMD 's|^.*: V||g')
|
||||
date=$(grep "Updated:" $file | $SED_CMD 's|^.*: ||g')
|
||||
version=$(grep "Version:" $file | sed 's|^.*: V||g')
|
||||
date=$(grep "Updated:" $file | sed 's|^.*: ||g')
|
||||
print_message "\nLOCAL Version: $BOLDWHITE$version$RESET\n"
|
||||
print_message "Updated: $date\n\n"
|
||||
# remote version
|
||||
curl -s --limit-rate 5k -r $range --location $url -o $tmp
|
||||
remote_ver=$(grep "Version:" $tmp | $SED_CMD 's|^.*: V||g')
|
||||
remote_date=$(grep "Updated:" $tmp | $SED_CMD 's|^.*: ||g')
|
||||
remote_ver=$(grep "Version:" $tmp | sed 's|^.*: V||g')
|
||||
remote_date=$(grep "Updated:" $tmp | sed 's|^.*: ||g')
|
||||
print_message "REMOTE Version: $BOLDWHITE$remote_ver$RESET\n"
|
||||
print_message "Updated: $remote_date\n"
|
||||
rm -f $tmp
|
||||
|
@ -154,7 +154,7 @@ update_paths() {
|
|||
|
||||
for x in $include_paths; do
|
||||
dir=$(dirname $x)
|
||||
$SED_CMD -i "s|$dir|$BOTS_DIR|" $blacklist
|
||||
sed -i "s|$dir|$BOTS_DIR|" $blacklist
|
||||
done
|
||||
else
|
||||
printf "${BOLDRED}ERROR${RESET}: '$BOTS_DIR' does not exist => ${BOLDWHITE}running $INSTALLER${RESET}.\n"
|
||||
|
@ -209,17 +209,6 @@ check_args() {
|
|||
}
|
||||
|
||||
check_depends() {
|
||||
local OS=$(uname -s)
|
||||
|
||||
case $OS in
|
||||
Linux)
|
||||
SED_CMD=$(find_binary sed)
|
||||
;;
|
||||
*BSD)
|
||||
SED_CMD=$(find_binary gsed)
|
||||
;;
|
||||
esac
|
||||
|
||||
# centos does not have which by default
|
||||
if [ -z $(find_binary curl) ]; then
|
||||
printf "${BOLDRED}ERROR${RESET}: $0 requires: 'curl' => ${BOLDWHITE}cannot check remote version.${RESET}\n"
|
||||
|
@ -247,9 +236,9 @@ log_output() {
|
|||
|
||||
if [ -n "$logger" ]; then
|
||||
# remove ansi color codes
|
||||
$SED_CMD -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
|
||||
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
|
||||
# remove blank lines
|
||||
$SED_CMD -i '/^\s*$/d' $EMAIL_REPORT
|
||||
sed -i '/^\s*$/d' $EMAIL_REPORT
|
||||
# log output
|
||||
$logger -t $script -f $EMAIL_REPORT 2>&1
|
||||
print_message "Output logged to syslog\n";
|
||||
|
@ -263,7 +252,7 @@ send_email() {
|
|||
if [ -n $(find_binary mail) ]; then
|
||||
print_message "Emailing report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
|
||||
# remove ansi colour codes
|
||||
$SED_CMD -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
|
||||
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
|
||||
cat $EMAIL_REPORT | mail -s "Nginx Bad Bot Blocker Updated" $EMAIL
|
||||
else
|
||||
print_message "${BOLDYELLOW}WARN${RESET}: missing mail command => ${BOLDWHITE}disabling emails${RESET}.\n\n"
|
||||
|
@ -274,7 +263,7 @@ send_email_via_mailgun() {
|
|||
local report= subject= endpoint="https://api.mailgun.net/v3/$MG_DOMAIN/messages"
|
||||
|
||||
echo "Mailgunning report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
|
||||
$SED_CMD -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
|
||||
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
|
||||
report="$(cat $EMAIL_REPORT)"
|
||||
subject='Nginx Bad Bot Blocker Updated'
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue