dnsmasq_regenconf_hook 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. source /usr/share/yunohost/helpers
  3. app="__APP__"
  4. action=$1
  5. pending_conf=$4/../dnsmasq
  6. [[ "$action" == "pre" ]] || exit 0
  7. [[ -d "$pending_conf" ]] || exit 0
  8. #
  9. # Regen /etc/dnsmasq.d/01-pihole.conf
  10. #
  11. dnsmasq_dir="${pending_conf}/etc/dnsmasq.d"
  12. mkdir -p "$dnsmasq_dir"
  13. cp -a "/etc/.pihole/advanced/01-pihole.conf" "$dnsmasq_dir/"
  14. ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf"
  15. ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf"
  16. ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file="$dnsmasq_dir/01-pihole.conf"
  17. ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file="$dnsmasq_dir/01-pihole.conf"
  18. query_logging=$(ynh_app_setting_get --app=$app --key=query_logging)
  19. if [ "$query_logging" = "true" ]; then
  20. ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  21. else
  22. ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  23. fi
  24. #
  25. # Tweak dnsmsasq's general conf cache-size
  26. #
  27. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  28. #
  29. # Regen /etc/dnsmasq.d/02-pihole-dhcp.conf
  30. #
  31. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  32. if [ $enable_dhcp -eq 1 ]
  33. then
  34. # Get the default network interface
  35. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  36. # Find the IP associated to the network interface
  37. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  38. max_dhcp_range=250
  39. dhcp_range=100
  40. # Define the dhcp range from the current ip
  41. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  42. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  43. b_range=$(( $ip_fourth_part + $dhcp_range ))
  44. if [ $b_range -gt $max_dhcp_range ]; then
  45. b_range=$max_dhcp_range
  46. fi
  47. a_range=$(( $b_range - $dhcp_range ))
  48. # Get the gateway
  49. gateway=$(ip route | grep default | awk '{print $3;}')
  50. # And the mac adress
  51. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  52. # Copy the config file
  53. cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/"
  54. # And set the config
  55. 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"
  56. 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"
  57. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf"
  58. # Set a static ip for the server.
  59. echo "dhcp-host=$hw_adress,$localipv4" > "${dnsmasq_dir}/04-pihole-static-dhcp.conf"
  60. fi
  61. exit 0