#!/bin/sh

#--------TOR post install script by gigix
#--------package maintained at ggteknikus.tk


PACKAGE_NAME=$SYNOPKG_PKGNAME
PACKAGE_VER=$SYNOPKG_PKGVER
PACKAGE_DIR="/var/packages/TorRelay"
PACKAGE_DEST=$SYNOPKG_PKGDEST

TORCONF="$PACKAGE_DIR/target/etc/tor/torrc"
STARTUP="/usr/syno/etc.defaults/rc.d"
DAEMON_USER="torusr"
DAEMON_PASS="`openssl rand 12 -base64 2>nul`"
DAEMON_ID="Tor Relay daemon user"
ID_PATH="$PACKAGE_DIR/target/var/lib/tor/keys"
CONF_PATH="$PACKAGE_DIR/target/etc/tor"
SRV_FILE="$PACKAGE_DIR/scripts/torrelay.sc"
CTL_FILE="$PACKAGE_DIR/scripts/S99torrelay.sh"

#Exit policy: Reject all by default
WEB80="reject"
SSL443="reject"
MAIL110="reject"
MAIL143="reject"
MAIL993="reject"
MAIL995="reject"
IM706="reject"
IM1863="reject"
IM5050="reject"
IM5190="reject"
IM5222="reject"
IM5223="reject"
IM8300="reject"
IM8888="reject"
IRC6660="reject"
IRC6697="reject"
IRC7000="reject"
ALLOTHER="reject"

if "$TOREX_WEB" ; then
	WEB80="accept"
fi
if "$TOREX_SSL" ; then
	SSL443="accept"
fi
if "$TOREX_MAIL" ; then
	MAIL110="accept"
	MAIL143="accept"
	MAIL993="accept"
	MAIL995="accept"
fi
if "$TOREX_IM" ; then
	IM706="accept"
	IM1863="accept"
	IM5050="accept"
	IM5190="accept"
	IM5222="accept"
	IM5223="accept"
	IM8300="accept"
	IM8888="accept"
fi
if "$TOREX_IRC" ; then
	IRC6660="accept"
	IRC6697="accept"
	IRC7000="accept"
fi
if "$TOREX_OTHER" ; then
	ALLOTHER="accept"
fi

#Get eth0 ip adress
eth_ip=$(ifconfig eth0 | grep "inet addr")
eth_ip=${eth_ip#*addr:}
eth_ip=${eth_ip% Bcast*}

echo "#This TOR configuration file is created by install wizard" > "$TORCONF"
echo "Log notice $PACKAGE_DIR/target/var/log/tor/tornotices.log" >> "$TORCONF"
#echo "#Log debug file $PACKAGE_DIR/target/var/log/tor/debug.log" >> "$TORCONF"
echo "DirPortFrontPage $PACKAGE_DIR/target/etc/tor/tor-exit-notice.html" >> "$TORCONF"
echo "DataDirectory $PACKAGE_DIR/target/var/lib/tor" >> "$TORCONF"
if $TOR_BRIDGE == true ; then
	echo "BridgeRelay 1" >> "$TORCONF"
		echo "ExitPolicy reject *:*" >> "$TORCONF"
	if "$TORBG_DISTRIB" ; then
		TORBG_DISTRIB="true" #do nothing
	else
		echo "PublishServerDescriptor 0" >> "$TORCONF"
	fi
fi
if $TOR_TEST == true ; then
	echo "PublishServerDescriptor 0"  >> "$TORCONF"
fi
if [ -n "$TOR_NICK" ] ; then
	echo "Nickname $TOR_NICK" >> "$TORCONF"
fi
if [ -n "$TOR_MAIL" ] ; then
	echo "ContactInfo $TOR_NICK <$TOR_MAIL>" >> "$TORCONF"
fi
if [ -n "$TORBW_AVG" ] ; then
	echo "BandwidthRate $TORBW_AVG KB" >> "$TORCONF"
	echo "RelayBandwidthRate $TORBW_AVG KB" >> "$TORCONF"
fi
if [ -n "$TORBW_MAX" ] ; then
	echo "BandwidthBurst $TORBW_MAX KB" >> "$TORCONF"
	echo "RelayBandwidthBurst $TORBW_MAX KB" >> "$TORCONF"
	echo "MaxAdvertisedBandwidth $TORBW_MAX KB" >> "$TORCONF"
fi
if [ -n "$TOR_ORPORTEXT" ] ; then
	echo "ORPort $TOR_ORPORTEXT NoListen" >> "$TORCONF"
	echo "ORPort $TOR_ORPORT NoAdvertise" >> "$TORCONF"
else
	echo "ORPort $TOR_ORPORT" >> "$TORCONF"
fi
if [ -n "$TOR_DIRPORTEXT" ] ; then
	echo "DirPort $TOR_DIRPORTEXT NoListen" >> "$TORCONF"
	echo "DirPort $TOR_DIRPORT NoAdvertise" >> "$TORCONF"
else
	echo "DirPort $TOR_DIRPORT" >> "$TORCONF"
fi
if [ -n "$TOR_SKSPORT" ] ; then
	echo "SocksPort ${eth_ip%* }:${TOR_SKSPORT}" >> "$TORCONF"
	echo "SocksPolicy accept ${TOR_SKSNET}/${TOR_SKSMASK}" >> "$TORCONF"
	echo "SocksPolicy reject *" >> "$TORCONF"
fi
if [ -n "$TOR_CTLPORT" ] ; then
	if [ -n "$TOR_CTLNOLB"  ] ; then
		echo "ControlPort ${eth_ip%* }:${TOR_CTLPORT}" >> "$TORCONF"
	else
		echo "ControlPort $TOR_CTLPORT" >> "$TORCONF"
	fi
fi
if [ -n "$TOR_CTLPASS" ] ; then
	echo HashedControlPassword `/var/packages/TorRelay/target/bin/tor --hash-password $TOR_CTLPASS | grep 16:` >> "$TORCONF"
fi
if [ -n "$TOR_CTLCOOKIE" ] ; then
	echo "CookieAuthentication 1" >> "$TORCONF"
fi
if $TOR_NONEXIT == true ; then
	echo "ExitPolicy reject *:*" >> "$TORCONF"
fi
if $TOR_EXIT == true ; then
	echo "ExitPolicy $WEB80 *:80,$SSL443 *:443,$MAIL110 *:110,$MAIL143 *:143,$MAIL993 *:993,$MAIL995 *:995,$IM706 *:706,$IM1863 *:1863,$IM5050 *:5050,$IM5190 *:5190,$IM5222 *:5222,$IM5223 *:5223,$IM8300 *:8300,$IM8888 *:8888,$IRC6660 *:6660-6669,$IRC6697 *:6697,$IRC7000 *:7000-7001,$ALLOTHER *:*"  >> "$TORCONF"
fi

#Create required paths
mkdir -p $PACKAGE_DIR/target/var/lib/tor/cached-status
mkdir -p $PACKAGE_DIR/target/var/lib/tor/keys
mkdir -p $PACKAGE_DIR/target/var/log/tor
mkdir -p $PACKAGE_DIR/target/var/run/tor
mkdir -p $PACKAGE_DIR/target/etc/tor

#Restore tor id from "Public" shared folder when restore is enabled
if $TOR_RESTORE == true ; then
	synoshare --get public > /dev/null
	if [ $? != 0 ]; then
		echo "Public share folder does not exist. Restore Tor Relay ID cannot be performed."> $SYNOPKG_TEMP_LOGFILE
		exit 1
	else
		pb_path=$(synoshare --get public | grep Path)
		pb_path=${pb_path#*\[}
		pb_path=${pb_path%\]*}
		if [ -f "${pb_path}/secret_id_key" ];
		then
   			cp -f "${pb_path}/secret_id_key" "${ID_PATH}"
		else
   			echo "Public share folder does not contain \"secret_id_key\" file. Restore Tor Relay ID cannot be performed." > $SYNOPKG_TEMP_LOGFILE
			exit 1
		fi		
	fi	
fi

#Restore configuration from "Public" shared folder when restore is enabled
if $TOR_RESTCONF == true ; then
	synoshare --get public > /dev/null
	if [ $? != 0 ]; then
		echo "Public share folder does not exist. Restore Tor Relay configuration cannot be performed."> $SYNOPKG_TEMP_LOGFILE
		exit 1

	else
		pb_path=$(synoshare --get public | grep Path)
		pb_path=${pb_path#*\[}
		pb_path=${pb_path%\]*}
		if [ -f "${pb_path}/torrc" ];
		then
   			cp -f "${pb_path}/torrc" "${CONF_PATH}"
		else
   			echo "Public share folder does not contain \"torrc\" file. Restore Tor Relay configuration cannot be performed." > $SYNOPKG_TEMP_LOGFILE
			exit 1
		fi
	fi	
fi

#Copy startup script
#cp $CTL_FILE $STARTUP done by start-stop-status

#Create tor relay daemon user if not exist
synouser --get torusr
if [ $? != 0 ]; then
	synouser --add "$DAEMON_USER" "${DAEMON_PASS}" "${DAEMON_ID}" 0 "" ""
fi

#Update service registration file
sed -i -e "s/443\/tcp/$TOR_ORPORT\/tcp/" $SRV_FILE
if [ "$TOR_DIRPORT" ] ; then
	sed -i -e "s/80\/tcp/$TOR_DIRPORT\/tcp/" $SRV_FILE
else
	sed -i -e '7,11d' $SRV_FILE

fi

#Service installation
servicetool --install-configure-file --package $SRV_FILE

#Copy tor exit notice
cp $PACKAGE_DIR/scripts/tor-exit-notice.html $PACKAGE_DIR/target/etc/tor/

#Change owner of Tor Relay folder tree
chown -R $DAEMON_USER:root $PACKAGE_DIR/target
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/bin
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/etc
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/etc/tor
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/share
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/run
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/run/tor
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/lib
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/lib/tor
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/lib/tor/keys
chmod 700 $PACKAGE_DIR/target/var/lib/tor/keys
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/log
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/log/tor


exit 0
