Initial support for --donotredirect

This commit is contained in:
Lutchy Horace 2021-03-17 17:47:19 -04:00
parent 185d813c0e
commit 2a08de6115
2 changed files with 63 additions and 17 deletions

View File

@ -26,13 +26,18 @@ function warn
function validate_host
{
local _ret=0
echo "$1" | grep -P '^(http|https):\/\/(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])(:[0-9]+)?$' > /dev/null 2>&1
if [ $? -eq 0 ]; then
debug "Checking host is resolvable: $1"
# The remote servers may sometimes have self-signed certs
curl --insecure $1 > /dev/null 2>&1
# Add --insecure becase remote servers may sometimes have self-signed certs
if ! curl --insecure $1 > /dev/null 2>&1; then
_ret=1
debug "Host '$1' is not resolvable!"
fi
fi
return $?
return $_ret
}
function validate_ip

View File

@ -2,7 +2,6 @@
#
# Create Vhosts on VPS3
#
DEBUG=0
#set -e
@ -13,8 +12,9 @@ function usage
echo " Domain to use when creating vhost"
echo " --root /var/www/html"
echo " Root directory of this vhost"
echo " --backend http://127.0.0.1:80"
echo " --backend http://127.0.0.1"
echo " Hostname of the backend server to pass traffic to"
echo " Note: Do not specify a port"
echo " --listenip x.x.x.x"
echo " IP to bind to when listening"
echo " --desc x.x.x.x"
@ -48,10 +48,7 @@ else
exit 1
fi
# gain priviledges
become "$@"
OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc: -n 'createVhosts' -- "$@")
OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc:,donotredirect -n 'createVhosts' -- "$@")
if [ "$?" -gt '0' ]; then
echo 'Failed to set command line arguments'
exit 1;
@ -60,6 +57,7 @@ fi
eval set -- "$OPTS"
_domain=false
_donotredirect=false
_root=""
_backend=""
_listenip=""
@ -80,6 +78,9 @@ while true; do
--desc )
_desc=$2
shift ;;
--donotredirect )
_donotredirect=true
shift ;;
-h | --help ) usage; shift ;;
-- ) shift; break ;;
* ) shift;;
@ -101,16 +102,48 @@ if test -n "$_root"; then
_rootpath="root $_root;"
fi
_check_host=success
_locationblock_http=""
_locationblock_https=""
if test -n "$_backend"; then
echo "Verifying backend..."
if ! validate_host $_backend; then
err "Invalid hostname: $_backend. Not resolvable!"
fi
_locationblock=$(cat <<- EOF
proxy_pass $_backend;
echo "Verifying backend(s)..."
_https_backend=$(echo $_backend | sed 's/http/https/')
if validate_host $_https_backend:443; then
#<<<<<<HEREDOC
_locationblock_https=$(cat <<- EOF
proxy_pass $_https_backend:443;
include proxy_params;
EOF
)
#<<<<<<HEREDOC
else
_check_host=failed
fi
# Include backend for HTTP traffic if donotredirect is enabled
#
if [ "$_donotredirect" = "true" ]; then
_http_backend=$(echo $_backend | sed 's/https/http/')
if validate_host $_http_backend:80; then
#<<<<<<HEREDOC
_locationblock_http=$(cat <<- EOF
proxy_pass $_http_backend:80;
include proxy_params;
EOF
)
#<<<<<<HEREDOC
else
_check_host=failed
fi
fi
if [ "$_check_host" = "failed" ]; then
err "Invalid hostname: $_backend. Not resolvable!"
fi
fi
if test -n "$_listenip"; then
@ -134,6 +167,14 @@ else
echo " Yes!"
fi
echo -n "Checking if we should redirect?"
if [ "$_donotredirect" = "false" ]; then
echo " Yes, enabling redirect!"
_locationblock_http=" return 302 https://${_domain}\$request_uri;"
else
echo " No!"
fi
_vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf
echo -n "Checking if $_vhost_conf_file exists? "
@ -160,7 +201,7 @@ server {
}
location / {
return 302 https://${_domain}\$request_uri;
$_locationblock_http
}
}
EOF
@ -188,7 +229,7 @@ server {
ssl_certificate_key ssl/${_domain}.key;
location / {
${_locationblock}
${_locationblock_https}
}
}
EOF