From 6902dd90db424b449021d2f14cc99e9a0fbe54de Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sun, 22 May 2022 16:17:55 +0200 Subject: [PATCH] refactor: add input validation to mainsail/fluidd port selection Signed-off-by: Dominik Willner th33xitus@gmail.com --- scripts/fluidd.sh | 13 ++++++------- scripts/mainsail.sh | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/scripts/fluidd.sh b/scripts/fluidd.sh index c5ca116..1be5cc5 100644 --- a/scripts/fluidd.sh +++ b/scripts/fluidd.sh @@ -366,7 +366,6 @@ function fluidd_port_check() { } function select_fluidd_port() { - local new_port if [[ ${PORT_80_BLOCKED} == "true" ]]; then echo top_border @@ -377,18 +376,18 @@ function select_fluidd_port() { [[ ${OCTOPRINT_PORT} == "80" ]] && echo "| ● OctoPrint |" [[ ${MAINSAIL_PORT} == "80" ]] && echo "| ● Mainsail |" blank_line - echo -e "| Make sure you don't choose a port which was already |" - echo -e "| assigned to one of the other webinterfaces and do ${red}NOT${white} |" - echo -e "| use ports in the range of 4750 or above! |" + echo -e "| Make sure you don't choose a port which is already |" + echo -e "| assigned to one of the other webinterfaces |" blank_line echo -e "| Be aware: there is ${red}NO${white} sanity check for the following |" echo -e "| input. So make sure to choose a valid port! |" bottom_border - while true; do - #TODO implement regex input validation for numbers only + + local new_port re="^[0-9]+$" + while [[ ! ${new_port} =~ ${re} ]]; do read -p "${cyan}Please enter a new Port:${white} " new_port if [[ ${new_port} != "${MAINSAIL_PORT}" ]] && [[ ${new_port} != "${OCTOPRINT_PORT}" ]]; then - echo "Setting port ${new_port} for Mainsail!" + echo "Setting port ${new_port} for Fluidd!" SET_LISTEN_PORT=${new_port} break else diff --git a/scripts/mainsail.sh b/scripts/mainsail.sh index e155b40..499969c 100644 --- a/scripts/mainsail.sh +++ b/scripts/mainsail.sh @@ -487,7 +487,6 @@ function mainsail_port_check() { } function select_mainsail_port() { - local new_port if [[ ${PORT_80_BLOCKED} == "true" ]]; then echo top_border @@ -505,8 +504,9 @@ function select_mainsail_port() { echo -e "| Be aware: there is ${red}NO${white} sanity check for the following |" echo -e "| input. So make sure to choose a valid port! |" bottom_border - while true; do - #TODO implement regex input validation for numbers only + + local new_port re="^[0-9]+$" + while [[ ! ${new_port} =~ ${re} ]]; do read -p "${cyan}Please enter a new Port:${white} " new_port if [[ ${new_port} != "${FLUIDD_PORT}" ]] && [[ ${new_port} != "${OCTOPRINT_PORT}" ]]; then echo "Setting port ${new_port} for Mainsail!"