Files
useful-scripts/haproxy-util
2025-07-18 11:50:57 -06:00

90 lines
1.4 KiB
Bash
Executable File

#! /bin/bash
SUBDOMAIN=""
DOMAIN=""
PORT=""
FILENAME="/etc/haproxy/haproxy.cfg"
usage(){
cat << EOF
usage: update-ipv6 [OPTIONS] -s subdomain [-d domain_name] -p port
Description
haproxy-util allows to add and modify redirection rules on haproxy
Options:
TBD
EOF
exit 1
}
if [[ $# -lt 4 ]] ;
then
usage
fi
while [[ ! -z "$1" ]]; do
case $1 in
-s)
SUBDOMAIN=$2
shift 2;;
-d)
DOMAIN=$2
shift 2;;
-p)
PORT=$2
shift 2;;
*)
echo $option
usage;;
esac
done
if [[ -z "$SUBDOMAIN" ]] ;
then
usage
fi
#if [[ -z "$DOMAIN" ]] ;
#then
#usage
#fi
if [[ -z "$PORT" ]] ;
then
usage
fi
if [[ -n $(grep " $SUBDOMAIN" $FILENAME) ]] ;
then
echo "ERROR: Subdomain already on use"
exit 1
fi
if [[ -n $(grep "$PORT" $FILENAME) ]] ;
then
echo "Error: Port already on use"
exit 1
fi
servername=$SUBDOMAIN"_server"
sudo sed -i "s/#ACL Definition/#ACL Definition\n\tacl host_$SUBDOMAIN hdr_beg(host) -i $SUBDOMAIN/" $FILENAME
sudo sed -i "s/#USE_BACKEND Definition/#USE_BACKEND Definition\n\tuse_backend $SUBDOMAIN if host_$SUBDOMAIN/" $FILENAME
sudo sed -i "s/#BACKEND Definition/#BACKEND Definition\nbackend $SUBDOMAIN\n\toption forwardfor\n\tserver $servername localhost:$PORT/" $FILENAME
sudo systemctl restart haproxy
#ACL Definition
#USE_BACKEND Definition
#BACKED Definition