_common.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. pihole_adminlte_version=5.18
  6. pihole_flt_version=5.20
  7. # This is hard-coded upstream...
  8. PI_HOLE_LOCAL_REPO="/etc/.pihole"
  9. PI_HOLE_INSTALL_DIR="/opt/pihole"
  10. PI_HOLE_CONFIG_DIR="/etc/pihole"
  11. PI_HOLE_BIN_DIR="/usr/local/bin"
  12. # Get the default network interface
  13. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  14. # Get the dnsmasq user to set log files permissions
  15. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  16. # Find the IP associated to the network interface
  17. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  18. if [ "$query_logging" -eq 1 ]; then
  19. query_logging_str=true
  20. else
  21. query_logging_str=false
  22. fi
  23. #=================================================
  24. # PERSONAL HELPERS
  25. #=================================================
  26. _configure_ports() {
  27. if [ "$port" -gt 4720 ]; then
  28. ynh_die --message="The ports 4711 to 4720 are already in use. Pi-hole can't work on another port. Please try to free one of these ports."
  29. fi
  30. # Disable the port 53 for upnp
  31. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  32. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  33. # Open the UDP port 67 for dhcp
  34. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  35. }
  36. _add_cron_jobs() {
  37. install -D -m 644 -T -o root -g root "$PI_HOLE_LOCAL_REPO/advanced/Templates/pihole.cron" /etc/cron.d/pihole
  38. # Randomize gravity update time
  39. ynh_replace_string --target_file="/etc/cron.d/pihole" \
  40. --match_string="59 1 " \
  41. --replace_string="$((1 + RANDOM % 58)) $((3 + RANDOM % 2)) "
  42. # Randomize update checker time
  43. ynh_replace_string --target_file="/etc/cron.d/pihole" \
  44. --match_string="59 17" \
  45. --replace_string="$((1 + RANDOM % 58)) $((12 + RANDOM % 8))"
  46. # Remove git usage for version. Which fails because we use here a release instead of master.
  47. ynh_replace_string --target_file="/etc/cron.d/pihole" \
  48. --match_string=".*updatechecker.*" \
  49. --replace_string="#&"
  50. }
  51. _add_sudoers_config() {
  52. install -m 0640 "$PI_HOLE_LOCAL_REPO/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  53. echo "$app ALL=NOPASSWD: ${PI_HOLE_BIN_DIR}/pihole" >> /etc/sudoers.d/pihole
  54. }
  55. _add_logrotate_config() {
  56. install -D -m 644 -T "${PI_HOLE_LOCAL_REPO}"/advanced/Templates/logrotate "$PI_HOLE_CONFIG_DIR/logrotate"
  57. sed -i "/# su #/d;" "$PI_HOLE_CONFIG_DIR/logrotate"
  58. }
  59. #=================================================
  60. # EXPERIMENTAL HELPERS
  61. #=================================================
  62. ynh_maintenance_mode_ON () {
  63. mkdir -p /var/www/html/
  64. # Create an html to serve as maintenance notice
  65. echo "<!DOCTYPE html>
  66. <html>
  67. <head>
  68. <meta http-equiv="refresh" content="3">
  69. <title>Your app $app is currently under maintenance!</title>
  70. <style>
  71. body {
  72. width: 70em;
  73. margin: 0 auto;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <h1>Your app $app is currently under maintenance!</h1>
  79. <p>This app has been put under maintenance by your administrator at $(date)</p>
  80. <p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
  81. </body>
  82. </html>" > "/var/www/html/maintenance.$app.html"
  83. # Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
  84. echo "# All request to the app will be redirected to ${path}_maintenance and fall on the maintenance notice
  85. rewrite ^${path}/(.*)$ ${path}_maintenance/? redirect;
  86. # Use another location, to not be in conflict with the original config file
  87. location ${path}_maintenance/ {
  88. alias /var/www/html/ ;
  89. try_files maintenance.$app.html =503;
  90. # Include SSOWAT user panel.
  91. include conf.d/yunohost_panel.conf.inc;
  92. }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  93. # The current config file will redirect all requests to the root of the app.
  94. # To keep the full path, we can use the following rewrite rule:
  95. # rewrite ^${path}/(.*)$ ${path}_maintenance/\$1? redirect;
  96. # The difference will be in the $1 at the end, which keep the following queries.
  97. # But, if it works perfectly for a html request, there's an issue with any php files.
  98. # This files are treated as simple files, and will be downloaded by the browser.
  99. # Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was.
  100. systemctl reload nginx
  101. }
  102. ynh_maintenance_mode_OFF () {
  103. # Rewrite the nginx config file to redirect from ${path}_maintenance to the real url of the app.
  104. echo "rewrite ^${path}_maintenance/(.*)$ ${path}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  105. systemctl reload nginx
  106. # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
  107. sleep 4
  108. # Then remove the temporary files used for the maintenance.
  109. rm "/var/www/html/maintenance.$app.html"
  110. rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  111. systemctl reload nginx
  112. }
  113. #=================================================
  114. # FUTURE OFFICIAL HELPERS
  115. #=================================================