Add fixes to sql proxy

This commit is contained in:
Kevin Baensch 2022-12-23 11:14:55 +01:00
parent f35b108e17
commit 2e824f92b3
2 changed files with 31 additions and 22 deletions

View file

@ -88,8 +88,6 @@ get_host() {
then
printf 'No such host: "%s"\n' $1
exit 1
else
printf "%s" "${TARGET_HOST_DATA}"
fi
fi
}
@ -102,33 +100,34 @@ get_host() {
# $4 - username (optional)
# $5 - password (optional)
set_host_env() {
if [ $1 = 'mysql' ] || [ $1 = 'psql' ]
if [ "$1" = 'mysql' ] || [ "$1" = 'psql' ]
then
TARGET_HOST_TYPE=$1
TARGET_HOST_TYPE="$1"
else
printf 'Invalid Database type: "%s"\n' $1
printf 'Invalid Database type: "%s"\n' "$1"
exit 1
fi
if [ $2 != '' ]
if [[ "$2" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
TARGET_HOST_IP=$2
TARGET_HOST_IP="$2"
else
printf 'No Host IP given.\n'
printf 'Invalid Host IP "%s" given.\n' "$2"
exit 1
fi
if echo $3 | grep -q '[0-9]\+'
if [[ "$3" =~ ^[0-9]+$ ]]
then
TARGET_HOST_PORT=$3
TARGET_HOST_PORT="$3"
else
printf 'Invalid Host Port "%s" given.\n' "$3"
exit 1
fi
if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n $4 ]
if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n "$4" ]
then
TARGET_HOST_USERNAME=$4
TARGET_HOST_USERNAME="$4"
fi
if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n $5 ]
if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n "$5" ]
then
TARGET_HOST_PASSWORD=$5
TARGET_HOST_PASSWORD="$5"
fi
}
@ -161,18 +160,27 @@ run_client() {
fi
}
MAIN_OPTION=$1
MAIN_OPTION="$1"
if [ -n "$1" ]
then
shift
fi
# ensure connection
connect
case $MAIN_OPTION in
case "${MAIN_OPTION}" in
ls)
ls_hosts;;
connect)
# check if host is valid
TARGET_HOST=$1
TARGET_HOST="$1"
if [ -n "$1" ]
then
shift
else
printf 'No host specified.\n'
exit 1
fi
while getopts "u:p:" o
do
@ -181,7 +189,8 @@ case $MAIN_OPTION in
p) TARGET_HOST_PASSWORD="$OPTARG" ;;
esac
done
set_host_env $(get_host "${TARGET_HOST}")
get_host "${TARGET_HOST}"
set_host_env ${TARGET_HOST_DATA}
port_forward "${TARGET_HOST_IP}" "$TARGET_HOST_PORT"
if [ -n "${TARGET_HOST_USERNAME}" ] && [ -n "${TARGET_HOST_PASSWORD}" ]
then
@ -191,5 +200,5 @@ case $MAIN_OPTION in
disconnect)
disconnect;;
*)
echo -e $HELP;;
echo -e "${HELP}";;
esac

View file

@ -13,7 +13,7 @@ WRAP_END='\n]'
OUT="/config/sqlproxy.json"
label_hook() {
if [ -z "{LOCAL_DB_PORT}" ]
if [ -z "${LOCAL_DB_PORT}" ]
then
if [ "${LOCAL_DB_TYPE}" = "mysql" ]
then