#!/usr/bin/env bash
set -euo pipefail

RESOLVED_SERVICE_NAME="systemd-resolved"
RESOLVED_CONF_FILE="/etc/systemd/resolved.conf"

# Проверьте, запущена ли служба 'systemd-resolved'
if ! systemctl is-active --quiet "${RESOLVED_SERVICE_NAME}.service"; then
	echo "Service '${RESOLVED_SERVICE_NAME}' is not running"
	exit
fi

# Проверьте, запущен ли прослушиватель-заглушка DNS.
if cat "${RESOLVED_CONF_FILE}" | grep --quiet "^DNSStubListener=no$"; then
	echo "DNSStubListener already turned off"
	exit
fi

echo "Turning off the DNS Stub Listener"

# Отключите DNSStubListener в файле конфигурации.
sed -r -i.orig 's/#?DNSStubListener=yes/DNSStubListener=no/g' "${RESOLVED_CONF_FILE}"

# Удалите активный resolv.conf, который имел бы
# localhost (прослушиватель DNS systemd-resolved) в качестве преобразователя DNS
rm -f /etc/resolv.conf

# Создайте символическую ссылку между восходящим resolv.conf systemd-resolved.
# и resolv.conf OS
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

echo "Restart service '${RESOLVED_SERVICE_NAME}'"
systemctl restart "${RESOLVED_SERVICE_NAME}"