HEX
Server: Apache/2.2.34 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141
System: Linux Kou-Etsu-Dou 4.4.59+ #25556 SMP PREEMPT Thu Mar 4 18:03:46 CST 2021 x86_64
User: hosam (1026)
PHP: 7.2.29
Disabled: NONE
Upload Files
File: /volume1/@appstore/DNSServer/hook_scripts/dnsserver_ip_change.sh
#!/bin/sh

PKG_DNS_ROOT="/var/packages/DNSServer"
PKG_DNS_TARGET="${PKG_DNS_ROOT}/target"
PKG_DNS_TOOL="${PKG_DNS_TARGET}/app/synodnstool"
SZF_DLZ_CONF="${PKG_DNS_TARGET}/etc/dlz.conf"
SZF_RESOLV_CONF="/etc/resolv.conf"
SYNONET_TOOL="/usr/syno/sbin/synonet"

set_dns()
{
	local OLD_DNS="$(${SYNONET_TOOL} --show_ha | awk '/^DNS/{ print $2 }' | head -1)"
	local NEW_DNS="$(${SYNONET_TOOL} --show_ha | awk '/^IP/{ print $2 }' | head -1)"
	if [ -z ${NEW_DNS} ]; then
		return 1
	fi
	if [ "x${OLD_DNS}" != "x${NEW_DNS}" ]; then
		if ! ${SYNONET_TOOL} --set_dns ${NEW_DNS} ;then
			logger -p warn -t "synodnstool" "set dns failed"
			return 1
		fi
	fi
}

update_dlz_iface_ip_records()
{
	local AD_MODE="$(/usr/syno/bin/synogetkeyvalue ${SZF_DLZ_CONF} "dlz_enable")"
	if [ "x1" != "x${AD_MODE}" ]; then
		return 0
	fi

	if ! pidof named > /dev/null ; then
		return 0
	fi

	# To fix cannot contact cluster IP dns server in HA when IP change
	# We need to set resolv.conf to localhost first
	local HA_RUN=$(synohacore --is_ha_running)
	local ORIGIN_RESOLV=""
	if [ "xyes" == "x${HA_RUN}" ]; then
		ORIGIN_RESOLV=$(cat ${SZF_RESOLV_CONF})
		echo -e "nameserver\t127.0.0.1" > ${SZF_RESOLV_CONF}
	else
		set_dns
	fi

	${PKG_DNS_TOOL} --dlz-records-update-iface-ip

	if [ "xyes" == "x${HA_RUN}" ] ; then
		if [ -n "${ORIGIN_RESOLV}" ]; then
			echo "${ORIGIN_RESOLV}" > ${SZF_RESOLV_CONF}
		fi
		set_dns
	fi
}

case $1 in
	--sdk-mod-ver)
	#Print SDK support version
	echo "1.0";
	;;
	--name)
	#Print package name
	echo "synodns"
	;;
	--pkg-ver)
	#Print package version
	echo "1.0";
	;;
	--vendor)
	#Print package vendor
	echo "Synology";
	;;
	--pre)
	;;
	--post)
	# do nothing when booting-up
	if /usr/syno/bin/synobootseq --is-booting-up > /dev/null 2>&1 ; then
		exit 0
	fi
	# do nothing when shutdown
	if /usr/syno/bin/synobootseq --is-shutdown > /dev/null 2>&1 ; then
		exit 0
	fi
	if [ "${FLOCKER}" != "$0" ]; then
		exec env FLOCKER="$0" flock -x "$0" "$0" "$@" &
	else
		update_dlz_iface_ip_records
	fi
	;;
	*)
	echo "Usage: $0 --sdk-mod-ver|--name|--pkg-ver|--vendor|--pre|--post"
	;;
esac