#!/bin/sh set -e cd polipo=$(which polipo) || polipo=$HOME/usr/bin/polipo tor=$(which tor) || tor=$HOME/usr/bin/tor rundir=$HOME/usr/run # pid files go here test_site=http://wtwfzc6ty2s6x4po.onion targetrc=.rtorrent.rc onionrc=.rtorrent.rc.onion feralrc=.rtorrent.rc.feral screensession=onion main() { case "$1" in stop) do_tor stop do_polipo stop if interactive; then fg_screen; fi exit ;; start|"") do_tor start do_polipo start do_test bg_screen if interactive; then fg_screen; fi ;; esac } interactive() { tty -s; } do_tor() { [ -x "$tor" ] || { echo "Warning: could not find tor binary" >&2; return; } cmd=$1 PIDFILE=$rundir/tor.pid; mkdir -p "$rundir" /sbin/start-stop-daemon --pidfile $PIDFILE --exec "$tor" --$cmd -- --RunAsDaemon 1 --PidFile $PIDFILE || true } do_polipo() { [ -x "$polipo" ] || { echo "Warning: could not find polipo binary" >&2; return; } cmd=$1 PIDFILE=$rundir/polipo.pid; mkdir -p "$rundir" /sbin/start-stop-daemon --pidfile $PIDFILE --exec "$polipo" --$cmd -- daemonise=true pidFile=$PIDFILE || true } do_test() { [ -n "$test_site" ] || return for try in 1 2 3; do if http_proxy=http://localhost:8123 wget "$test_site" -O /dev/null; then echo 'Made successful connection over tor!' >&2; if interactive; then echo 'Continuing to rtorrent...' >&2; fi return fi done echo 'Failed to make connection over tor!' >&2 if interactive; then echo -n 'Press control-c to quit or enter to continue...' >&2 read || exit 1 else exit fi } bg_screen() { if ! $(screen -list|grep -q onion 2>/dev/null); then bg_screen="screen -S $screensession -fa -d -m" [ -L $targetrc -o ! -e $targetrc ] ln -sf $onionrc $targetrc $bg_screen rtorrent || true (sleep 5 || true; ln -sf $feralrc $targetrc) & fi } fg_screen() { if $(screen -list|grep -q onion 2>/dev/null); then exec screen -x $screensession else echo "Did not find screen with sessionname '$screensession'! Exiting." >&2 exit 1 fi } main