From f4f916a8b29d084f9db841aba3eb5ba9d199c3ea Mon Sep 17 00:00:00 2001 From: OniriCorpe Date: Thu, 28 Dec 2023 05:54:28 +0100 Subject: [PATCH] new function: get_network_interface() --- scripts/_common.sh | 18 ++++++++++++------ scripts/install | 6 +++++- scripts/restore | 6 +++++- scripts/upgrade | 3 --- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 001d3f7..e0e3840 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -8,15 +8,21 @@ # PERSONAL HELPERS #================================================= +get_network_interface(){ +# get the network interface name for IPv4 and IPv6 + + local IPvx="$1" + + # note: echo the IP route command to prevent a crash if the server doesn't have any IPv4/6 + # shellcheck disable=SC2005 + echo ipv4_interface="$(echo "$(ip -"$IPvx" route get 1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true)" +} + configure_network_interface_dnsmasq(){ # used to put the network interface in a dedicated dnsmasq config - # get the network interface name for IPv4 and IPv6 - # note: echo the IP route command to prevent a crash if the server doesn't have any IPv4/6 - # shellcheck disable=SC2005 - ipv4_interface=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) - # shellcheck disable=SC2005 - ipv6_interface=$(echo "$(ip -6 route get ::1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) + local ipv4_interface="$1" + local ipv6_interface="$2" if [ -z "$ipv4_interface" ] && [ -z "$ipv6_interface" ]; then ynh_die --message="Impossible to find the main network interface, please report this issue." diff --git a/scripts/install b/scripts/install index 5402385..cd17771 100644 --- a/scripts/install +++ b/scripts/install @@ -77,8 +77,12 @@ usermod -a -G ssl-cert "$app" #================================================= ynh_script_progression --message="Adding a configuration file..." --weight=1 +# get the name of the network interface in IPv4 and IPv6 +ipv4_interface="$(get_network_interface 4)" +ipv6_interface="$(get_network_interface 6)" + # put the network interface in a dedicated dnsmasq config -configure_network_interface_dnsmasq +configure_network_interface_dnsmasq "$ipv4_interface" "$ipv6_interface" # get IPv4 for the AGH config file (with a starting "- ") ipv4_addr=$(echo "- " "$(process_ips "$(ip -4 route get 1.2.3.4 2> /dev/null | head -n1 | head -n1)")") diff --git a/scripts/restore b/scripts/restore index 9773c8e..18e5cba 100644 --- a/scripts/restore +++ b/scripts/restore @@ -44,8 +44,12 @@ ynh_restore_file --origin_path="$install_dir" # we need to refresh IP adresses in case the backup is restored in a different # environment, else AGH will try to bind port 53 on non-existent IPs and crash +# get the name of the network interface in IPv4 and IPv6 +ipv4_interface="$(get_network_interface 4)" +ipv6_interface="$(get_network_interface 6)" + # put the network interface in a dedicated dnsmasq config -configure_network_interface_dnsmasq +configure_network_interface_dnsmasq "$ipv4_interface" "$ipv6_interface" # get IPv4 for the AGH config file ipv4_addr=$(process_ips "$(ip -4 route get 1.2.3.4 2> /dev/null | head -n1 | head -n1)") diff --git a/scripts/upgrade b/scripts/upgrade index 81c756f..3706d5b 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -124,9 +124,6 @@ usermod -a -G ssl-cert "$app" #================================================= ynh_script_progression --message="Updating a configuration file..." --weight=1 -# put the network interface in a dedicated dnsmasq config -configure_network_interface_dnsmasq - # get IPv4 for the AGH config file ipv4_addr=$(process_ips "$(ip -4 route get 1.2.3.4 2> /dev/null | head -n1 | head -n1)")