#!/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" 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