From a5f428541198f5d3abef522563888ce927316433 Mon Sep 17 00:00:00 2001 From: OniriCorpe Date: Wed, 27 Dec 2023 06:49:32 +0100 Subject: [PATCH] replace two 'for loops' in install & upgrade scripts by process_ips() --- scripts/_common.sh | 21 +++++++++++++++++++++ scripts/install | 31 +++---------------------------- scripts/upgrade | 33 ++++----------------------------- 3 files changed, 28 insertions(+), 57 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 3114400..35e2f71 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -25,6 +25,27 @@ is_public_ip(){ fi } +process_ips(){ +# used to process the IPs to put them in the AGH's config file + + local ips="$1" + + for i in $(seq "$(echo "$ips" | wc -w)" -1 1); do + ip=$(echo "$ips" | awk "{print \$$i}") + if ynh_validate_ip4 --ip_address="$ip"; then + if is_public_ip "$ip" && [ "$open_port_53" == "false" ] ; then + # if the IP is public and the user doesn't want to expose port 53, skip it + break + else + ips="- $ip" + break + fi + fi + done + + return "$ips" +} + #================================================= # EXPERIMENTAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index 57afc3b..0dfa1b8 100644 --- a/scripts/install +++ b/scripts/install @@ -97,35 +97,10 @@ systemctl restart dnsmasq ynh_store_file_checksum --file="/etc/dnsmasq.d/$app" ipv4_route_output=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1 | head -n1) +ipv4_addr=$(process_ips "$ipv4_route_output") + ipv6_route_output=$(echo "$(ip -6 route get ::1.2.3.4 2> /dev/null)" | head -n1) - -ipv4_addr="" -for i in $(seq "$(echo "$ipv4_route_output" | wc -w)" -1 1); do - ip=$(echo "$ipv4_route_output" | awk "{print \$$i}") - if ynh_validate_ip4 --ip_address="$ip"; then - if is_public_ip "$ip" && [ "$open_port_53" == "false" ] ; then - # if the IP is public and the user doesn't want to expose port 53, skip it - break - else - ipv4_addr="- $ip" - break - fi - fi -done - -ipv6_addr="" -for i in $(seq "$(echo "$ipv6_route_output" | wc -w)" -1 1); do - ip=$(echo "$ipv6_route_output" | awk "{print \$$i}") - if ynh_validate_ip6 --ip_address="$ip"; then - if is_public_ip "$ip" && [ "$open_port_53" == "false" ] ; then - # if the IP is public and the user doesn't want to expose port 53, skip it - break - else - ipv6_addr="- $ip" - break - fi - fi -done +ipv6_addr=$(process_ips "$ipv6_route_output") password=$(python3 -c "import bcrypt; print(bcrypt.hashpw(b\"$password\", bcrypt.gensalt(rounds=10)).decode())") ynh_app_setting_set --app="$app" --key=password --value="$password" diff --git a/scripts/upgrade b/scripts/upgrade index 4fd0bed..e35f752 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -147,36 +147,11 @@ systemctl restart dnsmasq ynh_store_file_checksum --file="/etc/dnsmasq.d/$app" -ipv4_route_output=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1) +ipv4_route_output=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1 | head -n1) +ipv4_addr=$(process_ips "$ipv4_route_output") + ipv6_route_output=$(echo "$(ip -6 route get ::1.2.3.4 2> /dev/null)" | head -n1) - -ipv4_addr="" -for i in $(seq "$(echo "$ipv4_route_output" | wc -w)" -1 1); do - ip=$(echo "$ipv4_route_output" | awk "{print \$$i}") - if ynh_validate_ip4 --ip_address="$ip"; then - if is_public_ip "$ip" && [ "$open_port_53" == "false" ] ; then - # if the IP is public and the user doesn't want to expose port 53, skip it - break - else - ipv4_addr="$ip" - break - fi - fi -done - -ipv6_addr="" -for i in $(seq "$(echo "$ipv6_route_output" | wc -w)" -1 1); do - ip=$(echo "$ipv6_route_output" | awk "{print \$$i}") - if ynh_validate_ip6 --ip_address="$ip"; then - if is_public_ip "$ip" && [ "$open_port_53" == "false" ] ; then - # if the IP is public and the user doesn't want to expose port 53, skip it - break - else - ipv6_addr="$ip" - break - fi - fi -done +ipv6_addr=$(process_ips "$ipv6_route_output") # Reset the bind_hosts if the current ip is 0.0.0.0 python3 -c "import yaml