From e68ad3f8ba15fc072f3021c5d24bcb5ea786ca79 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 14 Sep 2021 14:31:20 +0200 Subject: [PATCH] Yolorework the regenconf script --- conf/dnsmasq_regenconf_hook | 133 +++++++++++++++++------------------- 1 file changed, 62 insertions(+), 71 deletions(-) diff --git a/conf/dnsmasq_regenconf_hook b/conf/dnsmasq_regenconf_hook index f9341bf..7394955 100755 --- a/conf/dnsmasq_regenconf_hook +++ b/conf/dnsmasq_regenconf_hook @@ -2,91 +2,82 @@ source /usr/share/yunohost/helpers -force=${2:-0} # 0/1 --force argument -dryrun=${3:-0} # 0/1 --dry-run argument -pending_conf=$4 # Path of the pending conf file app="__APP__" -do_pre_regen() { - if [ $dryrun -eq 0 ] - then - dnsmasq_dir="${pending_conf}/etc/dnsmasq.d" - mkdir -p "$dnsmasq_dir" - cp -a "/etc/dnsmasq.conf" "${pending_conf}/etc/dnsmasq.conf" - # Créer une sauvegarde des config dnsmasq de pi-hole. Que la regen-conf va sauvagement supprimer - cp -a "/etc/.pihole/advanced/01-pihole.conf" "$dnsmasq_dir/" - ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf" - ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf" - ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file="$dnsmasq_dir/01-pihole.conf" +action=$1 +pending_conf=$4/../dnsmasq - ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file="$dnsmasq_dir/01-pihole.conf" - if [ "$query_logging" = "true" ]; then - ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" - else - ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" - fi +[[ "$action" == "pre" ]] || exit 0 +[[ -d "$pending_conf" ]] || exit 0 - # Fix a too recent option for our dnsmasq version. - ynh_replace_string --match_string="log-queries=extra" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" +# +# Regen /etc/dnsmasq.d/01-pihole.conf +# +dnsmasq_dir="${pending_conf}/etc/dnsmasq.d" +mkdir -p "$dnsmasq_dir" - ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf" +cp -a "/etc/.pihole/advanced/01-pihole.conf" "$dnsmasq_dir/" - enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp) - if [ $enable_dhcp -eq 1 ] - then +ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf" +ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf" +ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file="$dnsmasq_dir/01-pihole.conf" - # Get the default network interface - main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}') - # Find the IP associated to the network interface - localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1) +ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file="$dnsmasq_dir/01-pihole.conf" +# FIXME : query_logging ain't defined +if [ "$query_logging" = "true" ]; then + ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" +else + ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" +fi - max_dhcp_range=250 - dhcp_range=100 - - # Define the dhcp range from the current ip - ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3) - ip_fourth_part=$(echo "$localipv4" | cut -d. -f4) - b_range=$(( $ip_fourth_part + $dhcp_range )) - if [ $b_range -gt $max_dhcp_range ]; then - b_range=$max_dhcp_range - fi - a_range=$(( $b_range - $dhcp_range )) +# Fix a too recent option for our dnsmasq version. +ynh_replace_string --match_string="log-queries=extra" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" - # Get the gateway - gateway=$(ip route | grep default | awk '{print $3;}') - # And the mac adress - hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}') +# +# Tweak dnsmsasq's general conf cache-size +# - # Copy the config file - cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/" +ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf" - # And set the config - ynh_replace_string --match_string="__A_RANGE__" --replace_string="$ip_beginning_part.$a_range" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" - ynh_replace_string --match_string="__B_RANGE__" --replace_string="$ip_beginning_part.$b_range" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" - ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" +# +# Regen /etc/dnsmasq.d/02-pihole-dhcp.conf +# - # Set a static ip for the server. - echo "dhcp-host=$hw_adress,$localipv4" > "${pending_conf}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" - fi +enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp) +if [ $enable_dhcp -eq 1 ] +then + + # Get the default network interface + main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}') + # Find the IP associated to the network interface + localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1) + + max_dhcp_range=250 + dhcp_range=100 + + # Define the dhcp range from the current ip + ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3) + ip_fourth_part=$(echo "$localipv4" | cut -d. -f4) + b_range=$(( $ip_fourth_part + $dhcp_range )) + if [ $b_range -gt $max_dhcp_range ]; then + b_range=$max_dhcp_range fi -} + a_range=$(( $b_range - $dhcp_range )) -do_post_regen() { - # Reload dnsmasq - systemctl reload dnsmasq -} + # Get the gateway + gateway=$(ip route | grep default | awk '{print $3;}') + # And the mac adress + hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}') -case "$1" in - pre) - do_pre_regen - ;; - post) - do_post_regen - ;; - *) - echo "Hook called with unknown argument \`$1'" >&2 - exit 1 - ;; -esac + # Copy the config file + cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/" + # And set the config + ynh_replace_string --match_string="__A_RANGE__" --replace_string="$ip_beginning_part.$a_range" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" + ynh_replace_string --match_string="__B_RANGE__" --replace_string="$ip_beginning_part.$b_range" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" + ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" + + # Set a static ip for the server. + echo "dhcp-host=$hw_adress,$localipv4" > "${dnsmasq_dir}/04-pihole-static-dhcp.conf" +fi exit 0