mirror of
https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker.git
synced 2025-09-02 18:50:13 +00:00
install-ngxblocker: use getopts not getopt
long options for the command line are removed. getopt is replaced with the newer getopts (which is compatible with Debian's DASH Shell) * fixes https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/issues/35 * adds sanity checks for user command line switches * adds -v command line option to print blacklist version
This commit is contained in:
parent
6147522b7e
commit
f67776d8a9
1 changed files with 72 additions and 27 deletions
|
@ -39,22 +39,36 @@ REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blo
|
|||
####### end user configuration ##########################
|
||||
|
||||
usage() {
|
||||
local script=$(basename $0)
|
||||
cat <<EOF
|
||||
$script: download Nginx Bad Bot Blocker configuration to: [ $CONF_DIR ] [ $BOTS_DIR ]
|
||||
local script=$(basename $0)
|
||||
cat <<EOF
|
||||
$script: INSTALL Nginx Bad Bot Blocker configuration to: [ $CONF_DIR ] [ $BOTS_DIR ]
|
||||
|
||||
Usage: $script [OPTIONS]
|
||||
[ -b | --bots ] : Bot rules directory (default: $BOTS_DIR)
|
||||
[ -c | --conf ] : NGINX conf directory (default: $CONF_DIR)
|
||||
[ -r | --repo ] : Change repo url (default: $REPO)
|
||||
[ -x | --exec ] : Actually change the files (default: don't change anything)
|
||||
[ -h | --help ] : this help message
|
||||
[ -b ] : Bot rules directory (default: $BOTS_DIR)
|
||||
[ -c ] : NGINX conf directory (default: $CONF_DIR)
|
||||
[ -r ] : Change repo url (default: $REPO)
|
||||
[ -x ] : Actually change the files (default: don't change anything)
|
||||
[ -v ] : Print blacklist version
|
||||
[ -h ] : this help message
|
||||
|
||||
Examples:
|
||||
$script (Don't change anything: display results on stdout)
|
||||
$script -x (Download / update config files)
|
||||
EOF
|
||||
return 0
|
||||
exit 0
|
||||
}
|
||||
|
||||
check_version() {
|
||||
local file=$CONF_DIR/globalblacklist.conf
|
||||
|
||||
if [ -f $file ]; then
|
||||
grep Version $file
|
||||
grep 'Updated:' $file
|
||||
else
|
||||
printf "Missing '$file' (pass -c \$path before -v)\n"
|
||||
fi
|
||||
|
||||
exit 0
|
||||
}
|
||||
|
||||
longest_str() {
|
||||
|
@ -127,27 +141,58 @@ check_config() {
|
|||
done
|
||||
}
|
||||
|
||||
sanitize_path() {
|
||||
echo $1 |tr -cd '[:alnum:] [=@=] [=.=] [=-=] [=/=] [=_=]' \
|
||||
|tr -s '@.-/_' |awk '{print tolower($0)}'
|
||||
}
|
||||
|
||||
sanitize_url() {
|
||||
echo $1 |tr -cd '[:alnum:] [=:=] [=.=] [=-=] [=/=]' \
|
||||
|tr -s ':.-' |awk '{print tolower($0)}'
|
||||
}
|
||||
|
||||
check_args() {
|
||||
local option=$1 type=$2 arg=$3
|
||||
local msg="ERROR: option '-$option' argument '$arg' requires:"
|
||||
|
||||
case "$type" in
|
||||
path) if ! echo $arg | grep ^/ 1>/dev/null; then
|
||||
printf "$msg absolute path.\n"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
url) if ! echo $arg | grep -E ^http[s]?://[0-9a-zA-Z-]+[.]+[/0-9a-zA-Z.]+ 1>/dev/null; then
|
||||
printf "$msg url => http[s]://the.url\n"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
none) printf "$msg argument.\n"; exit 1;;
|
||||
esac
|
||||
}
|
||||
|
||||
get_options() {
|
||||
local options=$(getopt -o b:c:r:hx --long \
|
||||
bots:,conf:,repo:,help,exec -- "$@" 2>/dev/null)
|
||||
local arg= opts=
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
while getopts :b:c:r:xvh opts "$@"
|
||||
do
|
||||
if [ -n "${OPTARG}" ]; then
|
||||
case "$opts" in
|
||||
r) arg=$(sanitize_url ${OPTARG});;
|
||||
*) arg=$(sanitize_path ${OPTARG});;
|
||||
esac
|
||||
fi
|
||||
|
||||
eval set -- "$options"
|
||||
|
||||
while :; do
|
||||
case "$1" in
|
||||
-h | --help) usage && exit 1;;
|
||||
-x | --exec) DRY_RUN=N; shift;;
|
||||
-b | --bots) BOTS_DIR=$2; shift 2;;
|
||||
-c | --conf) CONF_DIR=$2; shift 2;;
|
||||
-r | --repo) REPO=$2; shift 2;;
|
||||
*) break;;
|
||||
esac
|
||||
done
|
||||
case "$opts" in
|
||||
b) BOTS_DIR=$arg; check_args $opts path $arg ;;
|
||||
c) CONF_DIR=$arg; check_args $opts path $arg ;;
|
||||
r) REPO=$arg; check_args $opts url $arg ;;
|
||||
x) DRY_RUN=N ;;
|
||||
v) check_version ;;
|
||||
h) usage ;;
|
||||
\?) usage ;;
|
||||
:) check_args $OPTARG none none ;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
wget_opts() {
|
||||
|
|
Loading…
Add table
Reference in a new issue