Change entirely the way to manage resolv.conf

This commit is contained in:
Alexandre CATTEAU 2023-06-04 10:55:59 +02:00
parent 7c781475c9
commit e2213b08ab
6 changed files with 30 additions and 24 deletions

View File

@ -7,11 +7,11 @@ This repository contains setup or configuration files for [LibreELEC](https://li
https://wiki.libreelec.tv/configuration/wireguard for reference)
### Files
* `set-resolv-conf.sh` moves last DNS entry up in `/etc/resolv.conf` (is called after VPN initialization)
* `variables.conf.template` contains examples variables definitions, and should be copied locally to `variables.conf`
(with any required modifications).
* `systemd-templates/` contains Systemd units templates for detecting VPN connection, and apply routes and DNS
modifications.
* `set-dns.sh` sets the VPN DNS to `resolv.conf`, and `unset-dns.sh` restores the previous `resolv.conf`.
#### Installation
* `install.sh` script copies Systemd templates to their destination, and enables trigger (Systemd path).

View File

@ -17,8 +17,8 @@ cp $run_directory/systemd-templates/set-ip-routes.path /storage/.config/system.d
echo "Reloading Systemd..."
systemctl daemon-reload
echo "Enabling path unit (not starting it, either do it manually or reboot)..."
systemctl enable set-ip-routes.path
#echo "Enabling path unit (not starting it, either do it manually or reboot)..."
#systemctl enable set-ip-routes.path
echo "Finished install. Exiting..."
exit 0

15
set-dns.sh Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
run_directory=$(dirname $(readlink -f "$0"))
source "$run_directory/variables.conf"
# count lines in resolv.conf
lines=$(cat /run/libreelec/resolv.conf | wc -l)
# count number of 'nameserver' entries
count=$(grep -c 'nameserver' /run/libreelec/resolv.conf)
cp /run/libreelec/resolv.conf /run/libreelec/resolv.conf.bck
sed -i "$((lines - count + 1))s/^/nameserver $dns_server\n/" /run/libreelec/resolv.conf
exit 0

View File

@ -1,20 +0,0 @@
#!/usr/bin/env bash
# TODO review entirely:
# * use DNS variable to set the first nameserver line
# * add an "ExecStop" to service, and a script to remove that first line
# count lines in resolv.conf
lines=$(cat /run/libreelec/resolv.conf | wc -l)
# count number of 'nameserver' entries
count=$(grep -c 'nameserver' /run/libreelec/resolv.conf)
# move last entry to top
i=$((0))
while [ $((count)) -gt 1 ]; do
sed -i -n "$((lines - i - 1)){h;n;G};p" /run/libreelec/resolv.conf
count=$((count - 1))
i=$((i + 1))
done
exit 0

View File

@ -7,4 +7,5 @@ RemainAfterExit=yes
ExecStart=/usr/sbin/ip route del default dev wg0
ExecStart=/usr/sbin/ip route add PH_DNS dev wg0
ExecStart=/usr/sbin/ip route add PH_NETWORK dev wg0
ExecStart=/usr/bin/bash /storage/libreelec-conf/set-resolv-conf.sh
ExecStart=/usr/bin/bash /storage/libreelec-conf/set-dns.sh
ExecStop=/usr/bin/bash /storage/libreelec-conf/unset-dns.sh

10
unset-dns.sh Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
run_directory=$(dirname $(readlink -f "$0"))
source "$run_directory/variables.conf"
rm /run/libreelec/resolv.conf
mv /run/libreelec/resolv.conf.bck /run/libreelec/resolv.conf
exit 0