From e2213b08ab03f2067c19afa7511b0bc989d700f4 Mon Sep 17 00:00:00 2001 From: Alexandre CATTEAU Date: Sun, 4 Jun 2023 10:55:59 +0200 Subject: [PATCH] Change entirely the way to manage resolv.conf --- README.md | 2 +- install.sh | 4 ++-- set-dns.sh | 15 +++++++++++++++ set-resolv-conf.sh | 20 -------------------- systemd-templates/set-ip-routes.service | 3 ++- unset-dns.sh | 10 ++++++++++ 6 files changed, 30 insertions(+), 24 deletions(-) create mode 100755 set-dns.sh delete mode 100755 set-resolv-conf.sh create mode 100755 unset-dns.sh diff --git a/README.md b/README.md index b1581d4..eabde47 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/install.sh b/install.sh index 4c04306..2db79ef 100755 --- a/install.sh +++ b/install.sh @@ -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 diff --git a/set-dns.sh b/set-dns.sh new file mode 100755 index 0000000..95fba0b --- /dev/null +++ b/set-dns.sh @@ -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 diff --git a/set-resolv-conf.sh b/set-resolv-conf.sh deleted file mode 100755 index ce8ae36..0000000 --- a/set-resolv-conf.sh +++ /dev/null @@ -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 diff --git a/systemd-templates/set-ip-routes.service b/systemd-templates/set-ip-routes.service index bea75fb..c1a2ef5 100644 --- a/systemd-templates/set-ip-routes.service +++ b/systemd-templates/set-ip-routes.service @@ -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 diff --git a/unset-dns.sh b/unset-dns.sh new file mode 100755 index 0000000..a9e3aa6 --- /dev/null +++ b/unset-dns.sh @@ -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