Merge pull request #31 from itoffshore/alpine

install-ngxblocker: 2 small fixes / setup-ngxblocker: update to new file structure
This commit is contained in:
Mitchell Krog 2017-04-22 09:17:04 +02:00 committed by GitHub
commit e45f733c97
2 changed files with 72 additions and 13 deletions

View file

@ -166,7 +166,7 @@ wget_opts() {
check_online() { check_online() {
local url=$1 options=$(wget_opts) local url=$1 options=$(wget_opts)
if wget $options $include_url &>/dev/null; then if wget $options $url &>/dev/null; then
echo "true" echo "true"
fi fi
} }
@ -190,6 +190,7 @@ main() {
local tmp=$(mktemp) local tmp=$(mktemp)
wget -q $include_url -O $tmp wget -q $include_url -O $tmp
source $tmp 2>/dev/null source $tmp 2>/dev/null
rm -f $tmp
else else
printf "Repo down or missing: $include_url\n" printf "Repo down or missing: $include_url\n"
exit 1 exit 1

View file

@ -22,8 +22,8 @@ INC_DDOS="Y"
####### end user configuration ########################### ####### end user configuration ###########################
usage() { usage() {
local script=$(basename $0) local script=$(basename $0)
cat <<EOF cat <<EOF
$script: add Nginx Bad Bot Blocker configuration [ in $MAIN_CONF ] [ $VHOST_DIR/* ] $script: add Nginx Bad Bot Blocker configuration [ in $MAIN_CONF ] [ $VHOST_DIR/* ]
Usage: $script [OPTIONS] Usage: $script [OPTIONS]
@ -44,7 +44,7 @@ Examples:
$script (Don't change anything: display results on stdout) $script (Don't change anything: display results on stdout)
$script -x (Change / update config files) $script -x (Change / update config files)
EOF EOF
return 0 return 0
} }
check_config() { check_config() {
@ -54,6 +54,11 @@ check_config() {
echo "no vhost files in: $VHOST_DIR/*.$VHOST_EXT => exiting." echo "no vhost files in: $VHOST_DIR/*.$VHOST_EXT => exiting."
exit 1 exit 1
fi fi
if [ ! -f "$MAIN_CONF" ]; then
echo "NGINX main configuration ('$MAIN_CONF') not found => exiting."
exit 1
fi
} }
find_vhosts() { find_vhosts() {
@ -171,7 +176,7 @@ find_includes() {
esac esac
# if inserting beyond the end of the file # if inserting beyond the end of the file
if [ $line -gt $(wc -l < $file) ]; then if [ $line -gt $end ]; then
# insert blank line # insert blank line
sed -i "$end i \ " $file sed -i "$end i \ " $file
fi fi
@ -207,21 +212,70 @@ get_options() {
done done
} }
wget_opts() {
local opts=
# GNU wget / Busybox 1.26.2
if wget --help 2>&1 | grep -q "\--spider"; then
opts="--spider"
else # Busybox wget < 1.26.2
opts="-s"
fi
echo $opts
}
check_online() {
local url=$1 options=$(wget_opts)
if wget $options $url &>/dev/null; then
echo "true"
fi
}
main() { main() {
local file= line= vhost_includes= main_includes= file_list= local include_url= file= line= file_list=
main_includes="botblocker-nginx-settings.conf globalblacklist.conf" local CONF_FILES= BOT_FILES=
vhost_includes="blockbots.conf" local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master
# require root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# parse command line # parse command line
get_options $@ get_options $@
include_url=$REPO/include_filelist.txt
# check repo is online & source includes
printf "Checking url: $include_url\n"
if [ -n "$(check_online $include_url)" ]; then
local tmp=$(mktemp)
wget -q $include_url -O $tmp
source $tmp 2>/dev/null
rm -f $tmp
else
printf "Repo down or missing: $include_url\n"
exit 1
fi
# double check we have some files sourced
if [ -z "$CONF_FILES" ] || [ -z "$BOT_FILES" ]; then
printf "Error sourcing variables from: $include_url"
exit 1
fi
# configure ddos include
case "$INC_DDOS" in case "$INC_DDOS" in
y*|Y*) vhost_includes="$vhost_includes ddos.conf" n*|N*) BOT_FILES=$(echo $BOT_FILES | sed 's|ddos.conf||');;
esac esac
# gather vhosts
file_list=$(find_vhosts) file_list=$(find_vhosts)
check_config $file_list check_config $file_list
# by default do not change any files
if [ -z "$DRY_RUN" ]; then if [ -z "$DRY_RUN" ]; then
printf "\n** Dry Run ** | not updating files | -x or --exec to change files\n\n" printf "\n** Dry Run ** | not updating files | -x or --exec to change files\n\n"
fi fi
@ -229,18 +283,22 @@ main() {
# update vhosts # update vhosts
for file in $file_list; do for file in $file_list; do
line=$(find_includes $file include last server_ last location first ) line=$(find_includes $file include last server_ last location first )
add_includes $line $file $BOTS_DIR $vhost_includes add_includes $line $file $BOTS_DIR $BOT_FILES
done done
# update main config # update main config
line=$(find_includes $MAIN_CONF include last http first '\}' last ) line=$(find_includes $MAIN_CONF include last http first '\}' last )
add_includes $line $MAIN_CONF $CONF_DIR botblocker-nginx-settings.conf globalblacklist.conf add_includes $line $MAIN_CONF $CONF_DIR $CONF_FILES
whitelist_ips whitelist_ips
whitelist_domains
if [ -d $WWW ]; then
whitelist_domains
else
echo "Web directory not found ('$WWW'): not whitelisting domains"
fi
} }
## START ## ## START ##
main $@ main $@
exit $? exit $?