Skip to contentSkip to conversation

wg-quick: use resolvectl if present to set dns

PATCH In review [email protected] 2 participants 3 messages Aug 1, 2018 – Aug 8, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wg-quick: use resolvectl if present to set dns

only use resolvconf is available, fall back to resolvectl if present otherwise. don't set dns if both are missing.

---
 src/tools/wg-quick/linux.bash | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
src/tools/wg-quick/linux.bash +10 -2
index 3f1976b..f86f0c8 100755
@@ -151,13 +151,21 @@ resolvconf_iface_prefix() {
 HAVE_SET_DNS=0
 set_dns() {
 	[[ ${#DNS[@]} -gt 0 ]] || return 0
-	printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$(resolvconf_iface_prefix)$INTERFACE" -m 0 -x
+	if [ -x /usr/bin/resolvconf ]; then
+		printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$(resolvconf_iface_prefix)$INTERFACE" -m 0 -x
+	elif [ -x /usr/bin/resolvectl ]; then
+		cmd resolvectl dns $INTERFACE "${DNS[@]}"
+	fi
 	HAVE_SET_DNS=1
 }
 
 unset_dns() {
 	[[ ${#DNS[@]} -gt 0 ]] || return 0
-	cmd resolvconf -d "$(resolvconf_iface_prefix)$INTERFACE"
+	if [ -x /usr/bin/resolvconf ]; then
+		cmd resolvconf -d "$(resolvconf_iface_prefix)$INTERFACE"
+	elif [ -x /usr/bin/resolvectl ]; then
+		cmd resolvectl revert $INTERFACE
+	fi
 }
 
 add_route() {
#1 lore raw
Quoted 40 lines · On Wed, Aug 01, 2018 at 02:04:25PM +0200, [email protected] wrote:

only use resolvconf is available, fall back to resolvectl if present otherwise. don't set dns if both are missing.

---
 src/tools/wg-quick/linux.bash | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
src/tools/wg-quick/linux.bash +10 -2
index 3f1976b..f86f0c8 100755
@@ -151,13 +151,21 @@ resolvconf_iface_prefix() {
 HAVE_SET_DNS=0
 set_dns() {
 	[[ ${#DNS[@]} -gt 0 ]] || return 0
-	printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$(resolvconf_iface_prefix)$INTERFACE" -m 0 -x
+	if [ -x /usr/bin/resolvconf ]; then
+		printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$(resolvconf_iface_prefix)$INTERFACE" -m 0 -x
+	elif [ -x /usr/bin/resolvectl ]; then
+		cmd resolvectl dns $INTERFACE "${DNS[@]}"
+	fi
 	HAVE_SET_DNS=1
 }
 
 unset_dns() {
 	[[ ${#DNS[@]} -gt 0 ]] || return 0
-	cmd resolvconf -d "$(resolvconf_iface_prefix)$INTERFACE"
+	if [ -x /usr/bin/resolvconf ]; then
+		cmd resolvconf -d "$(resolvconf_iface_prefix)$INTERFACE"
+	elif [ -x /usr/bin/resolvectl ]; then
+		cmd resolvectl revert $INTERFACE
+	fi
 }
 
 add_route() {
Quoted 4 lines

_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard

I'd argue a better way to check if a command is available is the command built-in:

if command -v resolvconf >/dev/null; then
do stuff
fi

The user may have resolvconf available in PATH but not in that location.

Jason can comment on if this makes sense, I don't use wg-quick on Linux.

Nathan

#2 lore raw

Hi Nathan,

Quoted 6 lines · Nathan Chancellor

I'd argue a better way to check if a command is available is the command built-in:

if command -v resolvconf >/dev/null; then
do stuff
fi

good point, updated patch and added support for systemd-resolve in case resolvectl is not present.

j

src/tools/wg-quick/linux.bash +12 -2
index 3f1976b..b8ef60b 100755
@@ -151,13 +151,23 @@ resolvconf_iface_prefix() {
 HAVE_SET_DNS=0
 set_dns() {
 	[[ ${#DNS[@]} -gt 0 ]] || return 0
-	printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$(resolvconf_iface_prefix)$INTERFACE" -m 0 -x
+	if command -v resolvconf >/dev/null; then
+		printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$(resolvconf_iface_prefix)$INTERFACE" -m 0 -x
+	elif command -v resolvectl >/dev/null; then
+		cmd resolvectl dns $INTERFACE "${DNS[@]}"
+	elif command -v systemd-resolve >/dev/null && systemd-resolve --help | grep -- -set-dns >/dev/null; then
+		cmd systemd-resolve -i $INTERFACE `printf -- ' --set-dns %s' "${DNS[@]}"`
+	else
+		echo "could not configure nameservers" && return 0
+	fi
 	HAVE_SET_DNS=1
 }
 
 unset_dns() {
 	[[ ${#DNS[@]} -gt 0 ]] || return 0
-	cmd resolvconf -d "$(resolvconf_iface_prefix)$INTERFACE"
+	if [ -x /usr/bin/resolvconf ]; then
+		cmd resolvconf -d "$(resolvconf_iface_prefix)$INTERFACE"
+	fi
 }
 
 add_route() {