Skip to content

dnscrypt boot script for init.d #558

@uzen

Description

@uzen

what you think?

Afwall custom script
Start:
. /system/etc/init.d/99dnscrypt start &
Stop:
. /system/etc/init.d/99dnscrypt stop &

#!/system/bin/sh

# /etc/init.d/99dnscrypt: start and stop the dnscrypt daemon
PATH=/system/bin:/system/xbin
DAEMON=/system/xbin/dnscrypt-proxy
NAME=dnscrypt
DESC="DNSCrypt client proxy"
PIDFILE=/data/local/tmp/dnscrypt-proxy.pid
CONFIG_FILE=/system/etc/dnscrypt-proxy/dnscrypt-proxy.toml
WAITFORDAEMON=30
DAEMON_ARGS=

# Print info
LI="log -p i -t $NAME"

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

iptables_rules () {
	case "$1" in
		0)
			$LI "Enabling Iptables Firewall Rules"
			iptables -t nat -A OUTPUT -p tcp --dport 53 -j DNAT --to-destination 127.0.0.1:5353
			iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:5353
			;;

		1)
			$LI "Disabling Iptables Firewall Rules"
			iptables -t nat -D OUTPUT -p tcp --dport 53 -j DNAT --to-destination 127.0.0.1:5353
			iptables -t nat -D OUTPUT -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:5353
			;;

	esac
}

wait_for_daemon () {
	pid=$1
	sleep 1
	if test -n "$pid"
	then
		if ! kill -0 $pid 2>/dev/null
		then
			cnt=0
			while ! kill -0 $pid 2>/dev/null
			do
				cnt=`expr $cnt + 1`
				if [ $cnt -gt $WAITFORDAEMON ]
				then
					$LI "daemon still not running."
					return 1
				fi
				sleep 1
				[ "`expr $cnt % 3`" != 2 ] || $LI ""
			done
		fi
	fi
	$LI "0"
	return 0
}

case "$1" in
  start)
	if [ ! -s "$CONFIG_FILE" ]; then
		$LI "missing or empconfig file $CONFIG_FILE"
		$LI 1
		exit 0
	fi
    
	$LI "Starting $DESC $NAME"
	nohup $DAEMON -config $CONFIG_FILE -- $DAEMON_ARGS \
		-pidfile=$PIDFILE >/dev/null 2>&1 &
	pid=$! 
	echo $pid > $PIDFILE

	if ! wait_for_daemon $pid; then
   	$LI "daemon failed to start."
   	exit 1
	fi
	iptables_rules 0
	$LI 0
	;;
	
  restart|force-reload|reload)
	# nothing to do
    :
	;;
	
  stop)
	$LI "Stopping $DESC $NAME"
	pid=`cat $PIDFILE 2>/dev/null` || true
	
	if test ! -f $PIDFILE -o -z "$pid"; then
		$LI "not starting daemon."
		exit 0
	fi
	
	if kill $pid 2>/dev/null; then
		iptables_rules 1
		rm $PIDFILE
	else
		$LI "$DAEMON died: process $pid not running; or permission denied."
		exit 1
	fi
	;;
	
  status)
	$DAEMON -service status && exit 2 || exit $?
	;;
	
  *)
	echo "Usage: $0 {start|stop|restart|force-reload|reload}" >&2
	exit 3
	;;
esac

exit 0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions