Resolves merge request 6

This commit is contained in:
Lutchy Horace 2021-02-17 11:24:46 -05:00
parent c0a0c8c1ff
commit cb97128fb0
2 changed files with 98 additions and 38 deletions

View File

@ -19,6 +19,37 @@ function err
exit 1
}
function warn
{
echo -e "WARNING: $1"
}
function validate_host
{
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]+)?$'
if [ $? -eq 0 ]; then
curl $1 > /dev/null 2>&1
fi
return $?
}
function validate_ip
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
function send_notification
{
opt=""

View File

@ -11,8 +11,10 @@ function usage
echo "Usage: ${0}"
echo " --domain domain.tld"
echo " Domain to use when creating vhost"
echo " --proxypass x.x.x.x"
echo " IP of the backend server to pass traffic to"
echo " --root /var/www/html"
echo " Root directory of this vhost"
echo " --backend http://127.0.0.1:80"
echo " Hostname of the backend server to pass traffic to"
echo " --listenip x.x.x.x"
echo " IP to bind to when listening"
echo " --desc x.x.x.x"
@ -49,7 +51,7 @@ else
exit 1
fi
OPTS=$(getopt -o h -l domain:,proxypass:,listenip:,desc: -n 'createVhosts' -- "$@")
OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc: -n 'createVhosts' -- "$@")
if [ "$?" -gt '0' ]; then
echo 'Failed to set command line arguments'
exit 1;
@ -58,15 +60,19 @@ fi
eval set -- "$OPTS"
_domain=false
_proxyip=false
_listenip=false
_root=""
_backend=""
_listenip=""
while true; do
case "$1" in
--domain )
_domain=$2
shift ;;
--proxypass )
_proxyip=$2
--root )
_root=$2
shift ;;
--backend )
_backend=$2
shift ;;
--listenip )
_listenip=$2
@ -84,15 +90,38 @@ if [[ $_domain = false ]]; then
err "You must set domain"
fi
if [[ $_proxyip = false ]]; then
err "You must set the proxy pass IP"
if test -n "$_root"; then
if ! test -d $_root; then
err "Path doesn't exists! $_root"
fi
_rootpath="root $_root;"
fi
if [[ $_listenip = false ]]; then
err "You must set listen ip"
if test -n "$_backend"; then
if ! validate_host $_backend; then
err "Invalid hostname: $_backend. Not resolvable!"
fi
_locationblock=$(cat <<- EOF
proxy_pass $_backend;
include proxy_params;
EOF
)
fi
echo "Creating Nginx Vhosts..."
if test -n "$_listenip"; then
if ! validate_ip $_listenip; then
err "Invalid IP: $_listenip"
fi
_listenip="$_listenip:"
else
warn "No listen ip specified, listing on all interfaces."
fi
if test -z "$_root" -a -z "$_backend"; then
err "You must specify either --root or --backend!"
fi
echo "Creating Nginx configuration..."
_vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf
cat << EOF > $_vhost_conf_file
@ -101,7 +130,7 @@ cat << EOF > $_vhost_conf_file
## VHost: $_domain
## $_desc
server {
listen ${_listenip}:80;
listen ${_listenip}80;
server_name $_domain;
location /.well-known {
@ -114,8 +143,9 @@ server {
}
server {
listen ${_listenip}:443 http2 ssl;
listen ${_listenip}443 http2 ssl;
server_name $_domain;
$_rootpath
error_log /var/log/nginx/${_domain}.error.log;
access_log /var/log/nginx/${_domain}.access.log main;
@ -124,20 +154,19 @@ server {
ssl_certificate_key ssl/${_domain}.key;
location / {
proxy_pass https://${_proxyip}:443;
include proxy_params;
${_locationblock}
}
}
EOF
echo "Setting permissions on conf file..."
setfacl -m user:sysadmin:rw $_vhost_conf_file
#setfacl -m user:sysadmin:rw $_vhost_conf_file
echo "Stopping Nginx..."
stop_nginx
#stop_nginx
echo "Retrieving Let's Encrypt Certificate..."
get_cert
#get_cert
echo "Starting Nginx..."
start_nginx
#start_nginx