diff --git a/scripts/_common.sh b/scripts/_common.sh index 901985c..687315f 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -8,6 +8,31 @@ # PERSONAL HELPERS #================================================= +configure_network_interface_dnsmasq(){ +# used to put the network interface in a dedicated dnsmasq config + + ipv4_interface=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) + ipv6_interface=$(echo "$(ip -6 route get ::1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) + + if [ -z "$ipv4_interface" ] && [ -z "$ipv6_interface" ]; then + ynh_die --message="Impossible to find the main network interface, please report this issue." + elif [ "$ipv4_interface" != "$ipv6_interface" ]; then + if [ -z "$ipv4_interface" ]; then + echo -e "bind-interfaces\nexcept-interface=$ipv6_interface" > "/etc/dnsmasq.d/$app" + elif [ -z "$ipv6_interface" ]; then + echo -e "bind-interfaces\nexcept-interface=$ipv4_interface" > "/etc/dnsmasq.d/$app" + else + echo -e "bind-interfaces\nexcept-interface=$ipv4_interface, $ipv6_interface" > "/etc/dnsmasq.d/$app" + fi + else + echo -e "bind-interfaces\nexcept-interface=$ipv4_interface" > "/etc/dnsmasq.d/$app" + fi + + systemctl restart dnsmasq + + ynh_store_file_checksum --file="/etc/dnsmasq.d/$app" +} + is_public_ip(){ # used to discriminate publics IPs over privates IPs # private IPv4 start with: 10.; 169.; 172. or 192.168. diff --git a/scripts/install b/scripts/install index faf9a6a..ff98f43 100644 --- a/scripts/install +++ b/scripts/install @@ -77,27 +77,8 @@ usermod -a -G ssl-cert "$app" #================================================= ynh_script_progression --message="Adding a configuration file..." --weight=1 -# echo the ip route command to prevent a crash if the server doesn't have any ipv4/6 -ipv4_interface=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) -ipv6_interface=$(echo "$(ip -6 route get ::1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) - -if [ -z "$ipv4_interface" ] && [ -z "$ipv6_interface" ]; then - ynh_die --message="Impossible to find the main network interface, please report this issue." -elif [ "$ipv4_interface" != "$ipv6_interface" ]; then - if [ -z "$ipv4_interface" ]; then - echo -e "bind-interface\nexcept-interface=$ipv6_interface" > "/etc/dnsmasq.d/$app" - elif [ -z "$ipv6_interface" ]; then - echo -e "bind-interfaces\nexcept-interface=$ipv4_interface" > "/etc/dnsmasq.d/$app" - else - echo -e "bind-interfaces\nexcept-interface=$ipv4_interface, $ipv6_interface" > "/etc/dnsmasq.d/$app" - fi -else - echo -e "bind-interfaces\nexcept-interface=$ipv4_interface" > "/etc/dnsmasq.d/$app" -fi - -systemctl restart dnsmasq - -ynh_store_file_checksum --file="/etc/dnsmasq.d/$app" +# 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)") diff --git a/scripts/restore b/scripts/restore index d028225..2909ab7 100644 --- a/scripts/restore +++ b/scripts/restore @@ -41,9 +41,12 @@ ynh_script_progression --message="Restoring the app main directory..." --weight= ynh_restore_file --origin_path="$install_dir" -# we need to update IP adresses in case the backup is restored in a different +# 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 +# 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)") diff --git a/scripts/upgrade b/scripts/upgrade index 915c4ab..4532929 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -126,26 +126,8 @@ usermod -a -G ssl-cert "$app" #================================================= ynh_script_progression --message="Updating a configuration file..." --weight=1 -ipv4_interface=$(echo "$(ip -4 route get 1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) -ipv6_interface=$(echo "$(ip -6 route get ::1.2.3.4 2> /dev/null)" | head -n1 | grep -oP '(?<=dev )\w+' || true) - -if [ -z "$ipv4_interface" ] && [ -z "$ipv6_interface" ]; then - ynh_die --message="Impossible to find the main network interface, please report this issue." -elif [ "$ipv4_interface" != "$ipv6_interface" ]; then - if [ -z "$ipv4_interface" ]; then - echo -e "bind-interfaces\nexcept-interface=$ipv6_interface" > "/etc/dnsmasq.d/$app" - elif [ -z "$ipv6_interface" ]; then - echo -e "bind-interfaces\nexcept-interface=$ipv4_interface" > "/etc/dnsmasq.d/$app" - else - echo -e "bind-interfaces\nexcept-interface=$ipv4_interface, $ipv6_interface" > "/etc/dnsmasq.d/$app" - fi -else - echo -e "bind-interfaces\nexcept-interface=$ipv4_interface" > "/etc/dnsmasq.d/$app" -fi - -systemctl restart dnsmasq - -ynh_store_file_checksum --file="/etc/dnsmasq.d/$app" +# 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)")