-
Notifications
You must be signed in to change notification settings - Fork 157
/
rc.tor
executable file
·161 lines (145 loc) · 4.22 KB
/
rc.tor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
DAEMON=tor
DAEMON_LONG_NAME="Tor Onion Router"
DAEMON_CONFIG=/mod/etc/$DAEMON/torrc
PID_FILE=/var/run/$DAEMON/$DAEMON.pid
. /etc/init.d/modlibrc
DAEMON_STORAGE=/tmp/flash/$DAEMON
config() {
[ -d "$DAEMON_STORAGE" ] || mkdir -p $DAEMON_STORAGE
chown $DAEMON:$DAEMON "$DAEMON_STORAGE"
if [ ! -d "$TOR_DATADIRECTORY" ]; then
mkdir "$TOR_DATADIRECTORY" 2>/dev/null
if [ ! -d "$TOR_DATADIRECTORY" ]; then
echo "Could not create $TOR_DATADIRECTORY, failed."
exit $exitval
fi
chown $DAEMON:$DAEMON "$TOR_DATADIRECTORY"
chmod 700 "$TOR_DATADIRECTORY"
fi
if [ "$TOR_RELAY_ENABLED" = "yes" ] && [ -e "$DAEMON_STORAGE/secret_id_key" ] && [ ! -e "$TOR_DATADIRECTORY/keys/secret_id_key" ]; then
mkdir -p $TOR_DATADIRECTORY/keys 2>/dev/null
ln -s $DAEMON_STORAGE/secret_id_key $TOR_DATADIRECTORY/keys/secret_id_key
chown $DAEMON:$DAEMON "$TOR_DATADIRECTORY/keys" -R
fi
mkdir -p /mod/etc/$DAEMON
if [ "$TOR_FORCE_GEOIP_UPDATE" = "yes" ] || ( [ -n "$TOR_GEOIP_FILE" ] && [ ! -e "$TOR_GEOIP_FILE.IPv4" -o ! -e "$TOR_GEOIP_FILE.IPv6" ] ); then
update_geoip_db
fi
modlib_config
}
start() {
echo -n "Starting ${DAEMON_LONG_NAME} ... "
config
[ -f $DAEMON_STORAGE/secret_id_key ] && savetimer=no
$DAEMON --runasdaemon 1 --log "notice syslog" --pidfile $PID_FILE > /dev/null 2>&1
exitval=$?
if [ "$exitval" -eq 0 ]; then
if [ "$savetimer" != "no" -a "$TOR_RELAY_ENABLED" == "yes" ]; then
count=0
while [ ! -f $TOR_DATADIRECTORY/keys/secret_id_key ]; do
sleep 1
echo -n "."
let count++
[ $count -gt 99 ] && break
done
if [ $count -le 99 ]; then
cp $TOR_DATADIRECTORY/keys/secret_id_key $DAEMON_STORAGE/secret_id_key
echo -n "secret id_key saved ... "
modsave flash >/dev/null
else
echo -n "secret id_key not found ... "
fi
fi
echo "done."
else
echo "failed."
exit $exitval
fi
}
stop_post() {
[ "$TOR_DATADIRPERSISTENT" == "no" ] && rm -rf $TOR_DATADIRECTORY 2>/dev/null
}
#
# $1: name of the file downloaded GeoIP database to be saved to
# $2, $3, ...: optional codes of the countries to keep, if omitted all countries are kept
#
update_geoipv4_db_impl() {
local outputFilename="$1.IPv4"
shift
local countryCodes="$(IFS='|'; echo -n "$*" | tr [a-z] [A-Z])"
[ -n "$countryCodes" ] && local countryCodesPattern="/, *\"($countryCodes)\",/ "
# note unzipping from stdin is a busybox extension
wget -q -O - http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip \
| busybox unzip -p - GeoIPCountryWhois.csv \
| sed -n -r -e "${countryCodesPattern}s/[ \"]//gp" \
| cut -d, -f3-5 \
> "$outputFilename"
}
update_geoipv6_db_impl() {
local outputFilename="$1.IPv6"
shift
local countryCodes="$(IFS='|'; echo -n "$*" | tr [a-z] [A-Z])"
[ -n "$countryCodes" ] && local countryCodesPattern="/, *\"($countryCodes)\",/ "
wget -q -O - http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz \
| gunzip -c \
| sed -n -r -e "${countryCodesPattern}s/[ \"]//gp" \
| cut -d, -f1,2,5 \
> "$outputFilename"
}
update_geoip_db() {
echo -n "fetching GeoIP database ... "
if [ $# -gt 0 ]; then
update_geoipv4_db_impl "$@" && update_geoipv6_db_impl "$@"
else
if [ -z "$TOR_GEOIP_FILE" ]; then
echo "GeoIP file location must be provided, failed."
exit 1
fi
local countryCodes="$(echo -e "${TOR_ENTRY_NODES}\n${TOR_EXIT_NODES}" | grep -v "^#" | sed -n -r -e 's,[{]([a-zA-Z]{2})[}],\1,gp')"
update_geoipv4_db_impl "$TOR_GEOIP_FILE" $countryCodes && update_geoipv6_db_impl "$TOR_GEOIP_FILE" $countryCodes
fi
exitval=$?
if [ "$exitval" -ne 0 ]; then
echo "failed."
exit $exitval
fi
}
case $1 in
""|load)
modlib_add_user_and_group $DAEMON
mkdir -p ${PID_FILE%/*}
chown $DAEMON:$DAEMON ${PID_FILE%/*}
modreg cgi "$DAEMON" "$DAEMON_LONG_NAME"
modreg daemon $DAEMON
modreg file tor secret_id_key 'Secret ID Key' 0 "secret_id_key"
modlib_start $TOR_ENABLED
;;
unload)
modunreg file tor
modunreg daemon $DAEMON
modunreg cgi "$DAEMON"
modlib_stop
;;
start)
modlib_start
;;
stop)
modlib_stop
;;
restart)
modlib_restart
;;
status)
modlib_status
;;
update-geoip-db)
shift
update_geoip_db "$@" && echo "done."
;;
*)
echo "Usage: $0 [load|unload|start|stop|restart|status|update-geoip-db]" 1>&2
exit 1
;;
esac
exit 0