1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pihole_ynh.git synced 2024-09-03 20:05:58 +02:00

Add dhcp support

This commit is contained in:
Maniack Crudelis 2017-08-14 22:12:33 +02:00
parent d66ce429eb
commit a7ce59065f
8 changed files with 188 additions and 6 deletions

View file

@ -7,9 +7,6 @@ Filtrage publicitaire sur l'ensemble du réseau via votre propre serveur DNS.
https://pi-hole.net/ https://pi-hole.net/
### **Cette application est en cours de travail. Son fonctionnement n'est pas garanti.**
### **Le fonctionnement en tant que serveur dns n'est pas testé, ni le serveur dhcp.**
**Mise à jour du package:** **Mise à jour du package:**
sudo yunohost app upgrade pihole -u https://github.com/YunoHost-Apps/pihole_ynh sudo yunohost app upgrade pihole -u https://github.com/YunoHost-Apps/pihole_ynh

11
conf/02-pihole-dhcp.conf Normal file
View file

@ -0,0 +1,11 @@
###############################################################################
# DHCP SERVER CONFIG FILE AUTOMATICALLY POPULATED BY PI-HOLE WEB INTERFACE. #
# ANY CHANGES MADE TO THIS FILE WILL BE LOST ON CHANGE #
###############################################################################
dhcp-authoritative
dhcp-range=__A_RANGE__,__B_RANGE__,24h
dhcp-option=option:router,__GATEWAY__
dhcp-leasefile=/etc/pihole/dhcp.leases
#quiet-dhcp
domain=local

111
dhcp.md Normal file
View file

@ -0,0 +1,111 @@
Using Pi-hole as your DHCP server
==================
> **Be carreful, you should considering that playing with your DHCP may break your network.
In case in your server become to be down, you're going to lose your dns resolution and your ip address.
By this way, you'll losing any internet connection and even the connection to your router.**
> **If you encounter that kind of problem, please see "How to restore my network" at the end of this document.**
### How to configure Pi-hole
There two ways to configure Pi-hole to be used as your DHCP server.
- Either you can choose to use it when you're installing the app.
- Or you can activate the DHCP server after in the "Settings" tab, "Pi-hole DHCP Server" part.
In this second case, it can be better to set the ip of the server to a static address
### How to configure my router
Your personnal router or the router of your ISP brings a DHCP server activate by default.
If you keep this DHCP, at the same time of Pi-hole's one, you will encouter a transparent conflict between them.
The first responding DHCP will distribute its own ip and parameters.
So you have to turn off the DHCP of your router to let Pi-hole managed your network.
#### Why do I should use only the DHCP of Pi-hole ?
By using the DHCP of Pi-hole, you'll allow it to give at each of your client its dns configuration. Like that, each of your request will be filtering by Pi-hole.
An another use case of using Pi-hole's DHCP is if you have some hairpinning problems (You can't connect to your server because its IP is your public IP, and your router doesn't allow that).
In this case, use the dns of Pi-hole will allow you to connect to your server by its local adress instead of its public one.
### How to restore my network
> Oh crap !
Your Pi-hole is down, and you don't have a DHCP anymore.
Don't panic, we can overcome it \o/
Use your favorite terminal on your desktop computer.
And first, get your main interface (`eth0` in most cases).
``` bash
sudo ifconfig
```
Then, set your ip as a static ip.
``` bash
sudo ifconfig eth0 192.168.1.100
```
Now, you can connect to your router and turn on its DHCP server to reuse it.
You can now reset your ip and get a dynamic address
``` bash
sudo ifconfig eth0 0.0.0.0 && sudo dhclient eth0
```
> Don't forget to turn off the DHCP of your router if your server is working again.
---
Faire de Pi-hole votre serveur DHCP
==================
> **Attention, vous devez savoir que toucher à votre DHCP pourrait casser votre réseau.
Dans le cas où votre serveur serait inaccessible, vous perdriez votre résolution dns et votre adresse IP.
Ainsi, vous perdriez toute connexion à internet et même la connexion à votre routeur.**
> **Si vous rencontrez ce genre de problèmes, merci de lire la section "Comment restaurer mon réseau" à la fin de ce document.**
### Comment configurer Pi-hole
Il y a 2 manière de configurer Pi-hole pour qu'il soit utilisé comme votre serveur DHCP.
- Soit vous pouvez choisir de l'utiliser lorsque vous installez l'application.
- Soit vous pouvez activer le serveur DHCP par la suite dans l'onglet "Settings", partie "Pi-hole DHCP Server".
Dans ce second cas, il peut être préférable de forcer l'ip du serveur à une adresse statique.
### Comment configurer mon routeur
Votre routeur ou celui de votre FAI dispose d'un serveur DHCP activé par défaut.
Si vous gardez ce DHCP, en même temps que celui de Pi-hole, vous allez avoir des conflits transparents entre eux.
Le premier serveur DHCP à répondre va distribuer ses propres ip et paramètres.
Donc vous devez éteindre le serveur DHCP de votre routeur et laisser Pi-hole gérer votre réseau.
#### Pourquoi je devrais utiliser le DHCP de Pi-hole ?
En utilisant le DHCP de Pi-hole, vous lui permettez de donner sa configuration dns à chacun de vos clients. De cette manière, chaque requête sera filtrée par Pi-hole.
Un autre cas d'usage du DHCP de Pi-hole est le cas où vous rencontrez des problèmes de hairpinning (Vous ne pouvez pas vous connecter à votre serveur parce que son ip est votre ip publique, et votre routeur n'autorise pas cela).
Dans ce cas, utilisez le dns de Pi-hole va vous permettre de vous connecter à votre serveur par son adresse locale plutôt que son adresse publique.
### Comment restaurer mon réseau
> Oups !
Votre serveur Pi-hole est tombé, et vous n'avez plus de DHCP.
Ne paniquez pas, on va surmonter ça \o/
Utilisez votre terminal favori sur votre ordinateur de bureau.
Et tout d'abord, récupérer votre interface réseau (Le plus souvent `eth0`).
``` bash
sudo ifconfig
```
Ensuite, changer votre ip pour une ip statique.
``` bash
sudo ifconfig eth0 192.168.1.100
```
Maintenant, vous pouvez vous connecter à votre routeur et rallumer son serveur DHCP pour l'utiliser à nouveau.
Vous pouvez maintenant retirer votre ip statique et réobtenir une ip dynamique.
``` bash
sudo ifconfig eth0 0.0.0.0 && sudo dhclient eth0
```
> N'oubliez pas d'éteindre le DHCP de votre routeur si votre serveur fonctionne à nouveau.

View file

@ -59,8 +59,21 @@
"fr": "Voulez-vous enregistrer les requêtes dns ?" "fr": "Voulez-vous enregistrer les requêtes dns ?"
}, },
"help": { "help": {
"en": "Disabling will render graphs on the admin page useless. But will respect the privacy of the other users.", "en": "Keeping this option deactivate will render graphs on the admin page useless. But will respect the privacy of the other users.",
"fr": "La désactivation rendra les graphiques sur la page d'administration inutiles. Mais respectera la vie privée des autres utilisateurs." "fr": "Garder cette option désactivée rendra les graphiques sur la page d'administration inutiles. Mais respectera la vie privée des autres utilisateurs."
},
"default": false
},
{
"name": "enable_dhcp",
"type": "boolean",
"ask": {
"en": "Do you want to set Pi-hole as your DHCP server ?",
"fr": "Voulez-vous utiliser Pi-hole an tant que serveur DHCP ?"
},
"help": {
"en": "If you want to do that, <a href=https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md target=_blank>you really have to read this before</a> !",
"fr": "Si vous voulez faire ça, <a href=https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md target=_blank>vous devez vraiment lire cela avant</a> !"
}, },
"default": false "default": false
} }

View file

@ -90,5 +90,8 @@ fi
if test -e "/etc/dnsmasq.d/03-pihole-wildcard.conf"; then if test -e "/etc/dnsmasq.d/03-pihole-wildcard.conf"; then
ynh_backup "/etc/dnsmasq.d/03-pihole-wildcard.conf" "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" ynh_backup "/etc/dnsmasq.d/03-pihole-wildcard.conf" "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf"
fi fi
if test -e "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"; then
ynh_backup "/etc/dnsmasq.d/04-pihole-static-dhcp.conf" "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
fi
ynh_backup "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" "${YNH_APP_BACKUP_DIR}/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" ynh_backup "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" "${YNH_APP_BACKUP_DIR}/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"

View file

@ -28,6 +28,7 @@ domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH path_url=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN admin=$YNH_APP_ARG_ADMIN
query_logging=$YNH_APP_ARG_QUERY_LOGGING query_logging=$YNH_APP_ARG_QUERY_LOGGING
enable_dhcp=$YNH_APP_ARG_ENABLE_DHCP
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -224,13 +225,50 @@ ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
while read perdomain while read perdomain
do do
# Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local # Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local
sed -i "s/^127.0.0.1.*$perdomain/#Commented by pihole# &/g" /etc/hosts ynh_replace_string "^127.0.0.1.*$perdomain" "#Commented by pihole# &" /etc/hosts
# Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà # Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà
grep -q "^$localipv4.*$perdomain" /etc/hosts || \ grep -q "^$localipv4.*$perdomain" /etc/hosts || \
echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
done <<< "$(sudo yunohost domain list | grep "\." | sed 's/.*: \|.*- //')" done <<< "$(sudo yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
#=================================================
# ENABLE DHCP SERVER
#=================================================
if [ $enable_dhcp -eq 1 ]
then
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 ))
# Get the gateway
gateway=$(route | grep default | awk '{print $2;}' | head -n1)
# And the mac adress
hw_adress=$(sudo ifconfig | grep "eth0" | awk '{print $5;}')
# Copy the config file
sudo cp "../conf/02-pihole-dhcp.conf" "/etc/dnsmasq.d/"
# And set the config
ynh_replace_string "__A_RANGE__" "$ip_beginning_part.$a_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
ynh_replace_string "__B_RANGE__" "$ip_beginning_part.$b_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
ynh_replace_string "__GATEWAY__" "$gateway" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
# Set a static ip for the server.
echo "dhcp-host=$hw_adress,$localipv4" > "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
fi
# Open the UDP port 67 for dhcp
ALL_QUIET yunohost firewall allow UDP 67 --no-upnp
#================================================= #=================================================
# RESTART DNSMASQ # RESTART DNSMASQ
#================================================= #=================================================

View file

@ -75,6 +75,12 @@ then
QUIET sudo yunohost firewall disallow TCP $port QUIET sudo yunohost firewall disallow TCP $port
fi fi
if sudo yunohost firewall list | grep -q "\- 67$"
then
echo "Close port 67"
QUIET sudo yunohost firewall disallow UDP 67
fi
#================================================= #=================================================
# SPECIFIC REMOVE # SPECIFIC REMOVE
#================================================= #=================================================
@ -106,6 +112,7 @@ systemctl stop dnsmasq
rm -f "/etc/dnsmasq.d/01-pihole.conf" rm -f "/etc/dnsmasq.d/01-pihole.conf"
rm -f "/etc/dnsmasq.d/02-pihole-dhcp.conf" rm -f "/etc/dnsmasq.d/02-pihole-dhcp.conf"
rm -f "/etc/dnsmasq.d/03-pihole-wildcard.conf" rm -f "/etc/dnsmasq.d/03-pihole-wildcard.conf"
rm -f "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
ynh_replace_string "#pihole# " "" /etc/dnsmasq.conf ynh_replace_string "#pihole# " "" /etc/dnsmasq.conf

View file

@ -136,6 +136,8 @@ test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \
ynh_restore_file "/etc/dnsmasq.d/02-pihole-dhcp.conf" ynh_restore_file "/etc/dnsmasq.d/02-pihole-dhcp.conf"
test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \ test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
ynh_restore_file "/etc/dnsmasq.d/03-pihole-wildcard.conf" ynh_restore_file "/etc/dnsmasq.d/03-pihole-wildcard.conf"
test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \
ynh_restore_file "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
# Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut. # Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut.
ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf