Skip to content

Commit 2a7430f

Browse files
committed
update 99dnscrypt(.sh)
wait_for_daemon: checks for availability public-resolvers.md check_health: trying to fix the problems that are identified when at restart
1 parent a8c05d6 commit 2a7430f

File tree

2 files changed

+220
-168
lines changed

2 files changed

+220
-168
lines changed

structure/system/etc/init.d/99dnscrypt

-168
This file was deleted.
+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
#!/system/bin/sh
2+
3+
# /etc/init.d/99dnscrypt: start and stop the dnscrypt daemon
4+
5+
set -e
6+
7+
NAME=dnscrypt-proxy
8+
DAEMON=/system/xbin/$NAME
9+
PIDDIR=/data/local/tmp/dnscrypt
10+
PIDFILE=$PIDDIR/dnscrypt-proxy.pid
11+
LOCKFILE=$PIDDIR/dnscrypt-proxy.lock
12+
CONFIG_FILE=/system/etc/dnscrypt-proxy/dnscrypt-proxy.toml
13+
WAITFORDAEMON=30
14+
DESC="dns client proxy"
15+
16+
17+
. /system/etc/dnscrypt-proxy/iptables-rules
18+
19+
# Exit if the package is not installed
20+
test -x $DAEMON || exit 0
21+
22+
log_debug_msg () {
23+
if [ -n "${1:-}" ]; then
24+
echo "[D] $NAME: $@" || true
25+
log -p d -t $NAME "$@" || true
26+
fi
27+
}
28+
29+
log_error_msg () {
30+
if [ -n "${1:-}" ]; then
31+
echo "[E] $NAME: $@" || true
32+
log -p e -t $NAME "$@" || true
33+
fi
34+
}
35+
36+
get_prop () {
37+
sed -n 's/^'$1'=\(.*\)$/\1/p' $LOCKFILE 2>/dev/null
38+
}
39+
40+
set_prop () {
41+
if [ -n "${2:-}" ]; then
42+
[ -s $LOCKFILE ] || printf "#$NAME: lock file\n" > $LOCKFILE
43+
sed -i -e '/^\('$1'=\).*/{s//\1'$2'/;:a;n;ba;q}' \
44+
-e '$a'$1'='$2'' $LOCKFILE 2>/dev/null
45+
fi
46+
}
47+
48+
check_health () {
49+
test -s "$LOCKFILE" || return
50+
51+
if [ "$(get_prop 'dnscrypt-resolvers')" = "none" ]; then
52+
configdir=`dirname "$CONFIG_FILE"`
53+
resolvers="public-resolvers.md"
54+
minisig="$resolvers.minisig"
55+
56+
timestamp=$(date +%s)
57+
timegen=$(sed -n 's/.*timestamp:*\([0-9]\{1,\}\).*/\1/p' $configdir/$minisig )
58+
59+
let "_time=(timestamp-timegen)/3600"
60+
61+
log_debug_msg "configuration of server sources:"
62+
if [ $_time -lt 72 ]; then # updated every 3 days
63+
log_debug_msg "copy $resolvers to $PIDDIR..."
64+
cp $configdir/{$resolvers,$minisig} $PIDDIR/
65+
else
66+
intdir=/sdcard/$NAME
67+
if [ -e "$intdir/$resolvers" -o -e "$intdir/$minisig" ]; then
68+
log_debug_msg "copy $intdir/$resolvers to $PIDDIR..."
69+
cp $intdir/{$resolvers,$minisig} $PIDDIR/
70+
else
71+
log_debug_msg "$intdir/$resolvers(.minisig): file not found"
72+
fi
73+
fi
74+
fi
75+
76+
if [ "$(get_prop 'ipv4-enabled')" = "false" ]; then
77+
log_debug_msg "ipv4_addr_unlock: enable IPv4"
78+
ipv4_addr_unlock
79+
fi
80+
81+
rm -f $LOCKFILE
82+
}
83+
84+
status_of_proc () {
85+
_daemon="$1"
86+
_pidfile="$2"
87+
88+
[ -n "${_pidfile:-}" ] || _pidfile=$PIDFILE
89+
90+
if [ -e "$_pidfile" ] && [ -r "$_pidfile" ]; then
91+
read pid < "$_pidfile"
92+
if [ -n "${pid:-}" ]; then
93+
if $(kill -0 "${pid:-}" 2> /dev/null); then
94+
echo "$pid" || true
95+
return 0
96+
elif ps "${pid:-}" >/dev/null 2>&1; then
97+
echo "$pid" || true
98+
return 0 # program is running, but not owned by this user
99+
else
100+
return 2 # program is dead and pid file exists
101+
fi
102+
fi
103+
else
104+
if ps | grep "$_daemon" >/dev/null 2>&1; then
105+
return 0
106+
fi
107+
return 1
108+
fi
109+
return 3 # Unable to determine status
110+
}
111+
112+
wait_for_daemon () {
113+
status_of_proc "$NAME" || return $?
114+
115+
_timeout=0
116+
while :; do
117+
let _timeout=$_timeout+1
118+
119+
[ $_timeout -gt $WAITFORDAEMON ] && return 10
120+
[ -e $PIDDIR/*.md ] && break
121+
122+
let "_progress=(_timeout*100/WAITFORDAEMON*100)/100"
123+
let "_done=(_progress*4)/10"
124+
let _left=40-$_done
125+
126+
fill=$(printf "%${_done}s")
127+
empty=$(printf "%${_left}s")
128+
printf "\r[${fill// /\#}${empty// /-}] ${_progress}%%"
129+
130+
sleep 1
131+
done
132+
}
133+
134+
case "$1" in
135+
start) log_debug_msg "starting $DESC $NAME"
136+
137+
if test ! -s "$CONFIG_FILE"; then
138+
log_debug_msg "missing config file $CONFIG_FILE"
139+
exit 0
140+
fi
141+
142+
mkdir -p -m 01755 "$PIDDIR" 2>/dev/null || \
143+
{ log_debug_msg "cannot access $PIDDIR directory, are you root?"; exit 1 ; }
144+
145+
check_health
146+
147+
if status_of_proc "$NAME"
148+
then
149+
log_debug_msg "$DESC already started"
150+
exit 0
151+
fi
152+
153+
nohup $DAEMON -config $CONFIG_FILE \
154+
-pidfile=$PIDFILE > /dev/null 2>&1 &
155+
pid=$! && printf "$pid\n" > $PIDFILE
156+
157+
status="0"
158+
wait_for_daemon || status="$?"
159+
160+
case "$status" in
161+
0|10) # ok
162+
log_debug_msg "enabling iptables firewall rules"
163+
do_iptables_rules 0
164+
if [ "$status" = 10 ]; then
165+
log_error_msg "the resolvers file couldn't be uploaded?"
166+
set_prop 'dnscrypt-resolvers' 'none'
167+
exit 10
168+
fi
169+
;;
170+
*) # offline
171+
log_error_msg "ipv4_addr_lock: disable IPv4"
172+
ipv4_addr_lock && $(set_prop 'ipv4-enabled' 'false')
173+
exit 1
174+
;;
175+
esac
176+
;;
177+
stop) log_debug_msg "stopping $DESC $NAME"
178+
179+
status="0"
180+
status_of_proc "$NAME" || status="$?"
181+
182+
if [ "$status" = 0 ]; then
183+
pid=`cat $PIDFILE 2>/dev/null` || true
184+
185+
if kill $pid 2>/dev/null; then
186+
log_debug_msg "disabling iptables firewall rules"
187+
do_iptables_rules 1
188+
else
189+
log_debug_msg "Is $pid not $NAME? Is $DAEMON a different binary now?"
190+
fi
191+
192+
log_debug_msg "Removing stale PID file $PIDFILE"
193+
rm -f $PIDFILE
194+
elif [ "$status" = 3 ]; then
195+
log_debug_msg "not running - there is no $PIDFILE"
196+
killall $NAME >/dev/null 2>&1
197+
198+
exit 1
199+
fi
200+
;;
201+
status)
202+
status="0"
203+
status_of_proc "$NAME" || status="$?"
204+
205+
case "$status" in
206+
0) log_debug_msg "$NAME is running" ;;
207+
1) log_debug_msg "$NAME is NOT running" ;;
208+
2) log_error_msg "program is dead and pid file exists" ;;
209+
*) log_error_msg "could not access PID file $PIDFILE for $NAME" ;;
210+
esac
211+
212+
exit $status
213+
;;
214+
*)
215+
echo "Usage: /etc/init.d/99dnscrypt {start|stop|status}" >&2
216+
exit 1
217+
;;
218+
esac
219+
220+
exit 0

0 commit comments

Comments
 (0)