@@ -22,8 +22,8 @@ INC_DDOS="Y"
2222# ###### end user configuration ###########################
2323
2424usage () {
25- local script=$( basename $0 )
26- cat << EOF
25+ local script=$( basename $0 )
26+ cat << EOF
2727$script : add Nginx Bad Bot Blocker configuration [ in $MAIN_CONF ] [ $VHOST_DIR /* ]
2828
2929Usage: $script [OPTIONS]
@@ -44,7 +44,7 @@ Examples:
4444 $script (Don't change anything: display results on stdout)
4545 $script -x (Change / update config files)
4646EOF
47- return 0
47+ return 0
4848}
4949
5050check_config () {
@@ -54,6 +54,11 @@ check_config() {
5454echo " no vhost files in: $VHOST_DIR /*.$VHOST_EXT => exiting."
5555exit 1
5656fi
57+
58+ if [ ! -f " $MAIN_CONF " ]; then
59+ echo " NGINX main configuration ('$MAIN_CONF ') not found => exiting."
60+ exit 1
61+ fi
5762}
5863
5964find_vhosts () {
@@ -171,7 +176,7 @@ find_includes() {
171176esac
172177
173178# if inserting beyond the end of the file
174- if [ $line -gt $( wc -l < $file ) ]; then
179+ if [ $line -gt $end ]; then
175180# insert blank line
176181sed -i " $end i \ " $file
177182fi
@@ -207,40 +212,93 @@ get_options() {
207212 done
208213}
209214
215+ wget_opts () {
216+ local opts=
217+
218+ # GNU wget / Busybox 1.26.2
219+ if wget --help 2>&1 | grep -q " \--spider" ; then
220+ opts=" --spider"
221+ else # Busybox wget < 1.26.2
222+ opts=" -s"
223+ fi
224+
225+ echo $opts
226+ }
227+
228+ check_online () {
229+ local url=$1 options=$( wget_opts)
230+
231+ if wget $options $url & > /dev/null; then
232+ echo " true"
233+ fi
234+ }
235+
210236main () {
211- local file= line= vhost_includes= main_includes= file_list=
212- main_includes=" botblocker-nginx-settings.conf globalblacklist.conf"
213- vhost_includes=" blockbots.conf"
237+ local include_url= file= line= file_list=
238+ local CONF_FILES= BOT_FILES=
239+ local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master
240+
241+ # require root
242+ if [ " $( id -u) " != " 0" ]; then
243+ echo " This script must be run as root" 1>&2
244+ exit 1
245+ fi
214246
215247# parse command line
216248get_options $@
249+ include_url=$REPO /include_filelist.txt
250+
251+ # check repo is online & source includes
252+ printf " Checking url: $include_url \n"
253+ if [ -n " $( check_online $include_url ) " ]; then
254+ local tmp=$( mktemp)
255+ wget -q $include_url -O $tmp
256+ source $tmp 2> /dev/null
257+ rm -f $tmp
258+ else
259+ printf " Repo down or missing: $include_url \n"
260+ exit 1
261+ fi
262+
263+ # double check we have some files sourced
264+ if [ -z " $CONF_FILES " ] || [ -z " $BOT_FILES " ]; then
265+ printf " Error sourcing variables from: $include_url "
266+ exit 1
267+ fi
217268
269+ # configure ddos include
218270case " $INC_DDOS " in
219- y * |Y * ) vhost_includes= " $vhost_includes ddos.conf"
271+ n * |N * ) BOT_FILES= $( echo $BOT_FILES | sed ' s| ddos.conf|| ' ) ;;
220272esac
221273
274+ # gather vhosts
222275file_list=$( find_vhosts)
223276check_config $file_list
224277
278+ # by default do not change any files
225279if [ -z " $DRY_RUN " ]; then
226280printf " \n** Dry Run ** | not updating files | -x or --exec to change files\n\n"
227281fi
228282
229283# update vhosts
230284for file in $file_list ; do
231285line=$( find_includes $file include last server_ last location first )
232- add_includes $line $file $BOTS_DIR $vhost_includes
286+ add_includes $line $file $BOTS_DIR $BOT_FILES
233287done
234288
235289# update main config
236290line=$( find_includes $MAIN_CONF include last http first ' \}' last )
237- add_includes $line $MAIN_CONF $CONF_DIR botblocker-nginx-settings.conf globalblacklist.conf
291+ add_includes $line $MAIN_CONF $CONF_DIR $CONF_FILES
238292
239293whitelist_ips
240- whitelist_domains
294+
295+ if [ -d $WWW ]; then
296+ whitelist_domains
297+ else
298+ echo " Web directory not found ('$WWW '): not whitelisting domains"
299+ fi
241300}
242301
243302# # START ##
244303main $@
245304exit $?
246-
0 commit comments