FreeBSDにTorを入れる


FreeBSDにTorを入れてみる。
Torというのは通信を匿名化させる目的で作られてるそうだが、自宅にサーバを立てた時に外部からチェックする用途にも役に立つと思う。

インストール

まず、Portsでsecurity/torをそのまま入れる。
インストール後は_torユーザと_torグループが自動的に作られる。


次に、/var/run/torディレクトリを作る。これでインストール作業はおしまい。

# mkdir /var/run/tor
# chown _tor /var/run/tor
# chgrp _tor /var/run/tor

設定

/usr/local/etc/torディレクトリにサンプルがあるのでコピーする。

# cd /usr/local/etc/tor
# cp tor-tsocks.conf.sample tor-tsocks.conf
# cp torrc.sample torrc


他のマシンにTorを使わせないなら設定ファイルはデフォルトのままにする。

起動

必要な時だけTorを起動させる事にする。
昔のPortsで用意されてた起動スクリプト(下記)を/usr/local/etc/rc.d/tor.sh.oldで保存し、「chmod +x」しておく。
なお、パスの設定が昔と今では違ってるので、オリジナルより少し変更した。

#!/bin/sh
#
#tor    The Onion Router
#
# chkconfig: 2345 90 10
# description: Onion Router

TORUSER=_tor
TORGROUP=_tor
TORBIN=/usr/local/bin/tor
TORPID=/var/run/tor/tor.pid
TORLOG=/var/log/tor
TORDATA=/var/db/tor

TORCONF=/usr/local/etc/tor/torrc
# Strictly speaking, we don't need to su if we have --user and --group.
# "Belt and suspenders," says jbash.
TORARGS="--pidfile $TORPID --log \"notice file $TORLOG \" --runasdaemon 1 --datadirectory $TORDATA"
if [ "x$TORUSER" != "x" ]; then
    TORARGS="$TORARGS --user $TORUSER"
fi
if [ "x$TORGROUP" != "x" ]; then
    TORARGS="$TORARGS --group $TORGROUP"
fi
RETVAL=0

if [ -x /bin/su ] ; then
    SUPROG=/bin/su
elif [ -x /sbin/su ] ; then
    SUPROG=/sbin/su
elif [ -x /usr/bin/su ] ; then
    SUPROG=/usr/bin/su
elif [ -x /usr/sbin/su ] ; then
    SUPROG=/usr/sbin/su
else
    SUPROG=/bin/su
fi

case "$1" in

    start)
    if [ -f $TORPID ]; then
        echo "tor appears to be already running (pid file exists)"
        echo "Maybe you should run: $0 restart ?"
        RETVAL=1
    else
        echo -n "Starting tor..."
        if  [ "x$TORUSER" = "x" ]; then
            $TORBIN -f $TORCONF $TORARGS
        else
            $SUPROG $TORUSER -c "$TORBIN -f $TORCONF $TORARGS"
        fi
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            echo " ok"
        else
            echo " ERROR!"
        fi
    fi
    ;;

    stop)
    if [ -f $TORPID ]; then
        echo -n "Killing tor..."
        kill `cat $TORPID`
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            echo " ok"
        else
            echo " ERROR!"
        fi
    else
        echo "Unable to kill tor: $TORPID does not exist. Assuming already dead."
        RETVAL=0
    fi
    ;;

    reload)
    if [ -f $TORPID ]; then
        echo -n "Sending HUP to tor..."
        kill -HUP `cat $TORPID`
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            echo " ok"
        else
            echo " ERROR!"
        fi
    else
        echo "Unable to kill tor: $TORPID does not exist"
        RETVAL=1
    fi
    ;;

    restart)
    $0 stop
    if [ -f $TORPID ]; then
            rm -f $TORPID
    fi
    $0 start
    ;;

    status)
    PID=`cat $TORPID 2>/dev/null`
    if [ "$PID" != "" ]; then
        torstat=`ps -p $PID | grep -c "^$PID"`
        if [ $torstat ]; then
            echo "tor is running ($PID)"
        else
            echo "tor is not running (looks like it crashed, look for core?  $PID)"
        fi
    else
        echo "tor is not running (exited gracefully)"
    fi
    ;;

    log)
    cat $TORLOG
    ;;

    *)
    echo "Usage: $0 (start|stop|restart|status|log)"
    exit 1
esac

exit $RETVAL


Torを起動するには下記のコマンドを入れる。

# /usr/local/etc/rc.d/tor.sh.old start


ブラウザの設定のSOCKSプロクシをホストに127.0.0.1、ポートに9050を指定する。
ちなみにFirefoxの拡張のFoxyProxyを使うと簡単にTor用の設定ができる。


試しに診断くん(http://taruo.net/e/)に繋いでみる。

204.13.236.244なんて知らん


Torを終了するには下記のコマンドを入れる。

# /usr/local/etc/rc.d/tor.sh.old stop