HEX
Server: Apache/2.4.63 (Unix)
System: Linux Synopilou92 4.4.302+ #72806 SMP Mon Jul 21 23:16:00 CST 2025 x86_64
User: pilou92 (1026)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: /volume1/@appstore/VPNCenter/scripts/l2tpd.sh
#!/bin/sh
#
# Start the L2TP server daemon
#

PKG_USERCONF_DIR="/usr/syno/etc/packages/VPNCenter"
LOCALIP=`/bin/get_key_value ${PKG_USERCONF_DIR}/l2tp/xl2tpd.conf "local ip"`
PKG_TARGET="/var/packages/VPNCenter/target"
KERNEL_VERSION=`uname -r`
IPTABLES_MODULE_LIST="/usr/syno/etc.defaults/iptables_modules_list"
MTU=`/bin/cat ${PKG_USERCONF_DIR}/l2tp/options.xl2tpd | grep mtu | sed 's/^.*mtu //g'`
MSS=$((MTU-44))
TMP_L2TP_FILE="/tmp/vpnl2tpserver"

SERVICE="l2tp"
BIN_IPTABLESTOOL="/usr/syno/bin/iptablestool"
BIN_SYNOMODULETOOL="/usr/syno/bin/synomoduletool"

# where the private directory and the config files are
IPSEC_CONFS="${PKG_USERCONF_DIR}/l2tp"
export IPSEC_CONFS

reverse_modules() {
	local modules=$1
	local mod
	local ret=""

	for mod in $modules; do
	    ret="$mod $ret"
	done

	echo $ret
}

IPSec_Mod=""
if [ -f "${IPTABLES_MODULE_LIST}" ]; then
	. ${IPTABLES_MODULE_LIST}

	for mod in $PPP_MODULES; do
		if [ -e "/lib/modules/$mod" ]; then
			IPSec_Mod="${IPSec_Mod} ${mod}"
		fi
	done
	for mod in $L2TP_MODULES; do
		if [ -e "/lib/modules/$mod" ]; then
			IPSec_Mod="${IPSec_Mod} ${mod}"
		fi
	done
	for mod in $KERNEL_MODULES_CORE; do
		if [ -e "/lib/modules/$mod" ]; then
			IPSec_Mod="${IPSec_Mod} ${mod}"
		fi
	done
	for mod in $KERNEL_MODULES_COMMON; do
		if [ -e "/lib/modules/$mod" ]; then
			IPSec_Mod="${IPSec_Mod} ${mod}"
		fi
	done
	for mod in $KERNEL_MODULES_NAT; do
		if [ -e "/lib/modules/$mod" ]; then
			IPSec_Mod="${IPSec_Mod} ${mod}"
		fi
	done
else
	. ${PKG_TARGET}/scripts/vpn_modules.sh

	for mod in $L2TP_MODULES; do
		if [ -e "/lib/modules/$mod" ]; then
			IPSec_Mod="${IPSec_Mod} ${mod}"
		fi
	done
fi

for mod in $IPSec_MODULES; do
	if [ -e "/lib/modules/$mod" ]; then
		IPSec_Mod="${IPSec_Mod} ${mod}"
	fi
done

start() {
	Run=`/bin/get_key_value ${PKG_USERCONF_DIR}/synovpn.conf runl2tpd`
	if [ "yes" != "$Run" ]; then
		exit 0;
	fi

	touch $TMP_L2TP_FILE

	echo "Starting L2TP server: "
	if [ -x ${BIN_SYNOMODULETOOL} ]; then
		$BIN_SYNOMODULETOOL --insmod $SERVICE ${IPSec_Mod}
	elif [ -x ${BIN_IPTABLESTOOL} ]; then
		$BIN_IPTABLESTOOL --insmod $SERVICE ${IPSec_Mod}
	fi

	if [ -n "`pidof xl2tpd`" ]; then
	   echo "Already running"
	   return
	fi
	if [ ! -d "/var/run/xl2tpd" ]; then
		/bin/mkdir -p /var/run/xl2tpd
	fi
	synosystemctl start pkg-VPNCenter-l2tpd-server
	echo 1 > /proc/sys/net/ipv4/ip_forward

	/sbin/iptables -t nat -A POSTROUTING -s ${LOCALIP}/24 -j MASQUERADE
	/sbin/iptables -t mangle -A PRIMITIVE_FORWARD -i ppp3+ -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss "${MSS}"
	/sbin/iptables -t filter -A VPN_SERVER_INPUT -p udp --dport 1701 -m policy --dir in --pol ipsec -j RETURN
	/sbin/iptables -t filter -A VPN_SERVER_INPUT -p udp --dport 1701 -j DROP
	for f in /proc/sys/net/ipv4/conf/*;
	do
		echo 0 > $f/send_redirects;
		echo 0 > $f/accept_redirects;
	done
	synosystemctl start pkg-VPNCenter-ipsec
	${PKG_TARGET}/scripts/radiusd.sh start
}

stop() {
	Run=`/bin/get_key_value ${PKG_USERCONF_DIR}/synovpn.conf runl2tpd`
	if [ "yes" != "$Run" ] || [ ! -f "${TMP_L2TP_FILE}" ]; then
		exit 0;
	fi

	rm -f "${TMP_L2TP_FILE}"

	local modules=`reverse_modules "${IPSec_Mod}"`
	echo -n "Shutting down L2TP server: "

	#disconnect clients
	if [ -n "${LOCALIP}" ]; then
		/bin/kill `ps aux | /bin/grep pppd | /bin/grep l2tp | /bin/grep "${LOCALIP}" | awk '{print $1;}'` 2>/dev/null
	fi

	/sbin/iptables -t nat -D POSTROUTING -s ${LOCALIP}/24 -j MASQUERADE
	/sbin/iptables -t mangle -D PRIMITIVE_FORWARD -i ppp3+ -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss "${MSS}"
	/sbin/iptables -t filter -D VPN_SERVER_INPUT -p udp --dport 1701 -m policy --dir in --pol ipsec -j RETURN
	/sbin/iptables -t filter -D VPN_SERVER_INPUT -p udp --dport 1701 -j DROP

	synosystemctl stop pkg-VPNCenter-l2tpd-server
	synosystemctl stop pkg-VPNCenter-ipsec
	sleep 1
	if [ -n "`pidof xl2tpd`" ]; then
	    echo "Failed to stop xl2tpd"
	fi

	if [ -x ${BIN_SYNOMODULETOOL} ]; then
		$BIN_SYNOMODULETOOL --rmmod $SERVICE $modules
	elif [ -x ${BIN_IPTABLESTOOL} ]; then
		$BIN_IPTABLESTOOL --rmmod $SERVICE $modules
	fi

	${PKG_TARGET}/scripts/radiusd.sh stop
}

case "$1" in
	start)
		start
	;;
	stop)
		stop
	;;
	restart|reload)
		stop
		start
	;;
	*)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac
exit $?