[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

ExcludeNodes by country



Hi fellow tor users.

I made a little script to exclude the tor routers in some countries. 
Either choose the countries you want, or those you don't.  You can put this
script anywhere but in your torrc directory.



#!/bin/sh


# Set one or another, but not both, alphabetical order plz:
#COUNTRIES_TO_REJECT="CN|FR|GB|HK"; # Those countries will be rejected.
COUNTRIES_TO_ACCEPT="AR|BR|CA|DE|IL|JP|MX|US"; # All other countries will be rejected, those only will be accepted.

# Where to find a routers list:
ROUTERS="http://serifos.eecs.harvard.edu/cgi-bin/exit.pl?textonly=1";

# Location of your torrc file:
TORRC=/etc/tor/torrc

# Command to reload your torrc:
RELOAD="/etc/init.d/tor reload"


####  No need to modify nothing after this line ####

set -e

LOCRC=".tmp_torrc"
LOCROUT=".routers.txt"

test -z $TORRC && exit 0

# create a backup of the torrc file
cp $TORRC ${LOCRC}.bak

# Remove ExcludeNodes line from torrc
cat $TORRC | grep -vi Excludenodes > $LOCRC

# Download routers list.
wget -O $LOCROUT $ROUTERS || exit 0

# Write list of actual countries to a text file for reference
file=Countries.txt
echo -n "Countries " > $file
for i in `cat $LOCROUT | cut -f 1 -d " "| grep -v Tor | sort -u`; do
    echo -n "$i " >> $file
done

file=$LOCRC
echo -n "ExcludeNodes " >> $file
if [ "$COUNTRIES_TO_ACCEPT" != "" ]; then
    for i in `cat $LOCROUT | egrep -v "^($COUNTRIES_TO_ACCEPT)" | cut -f 2 \
-d " " | egrep -v "^(\[|Network)" | sed s/*//`; do
        echo -n "$i, " >> $file
    done
elif [ "$COUNTRIES_TO_REJECT" != "" ]; then
    for i in `cat $LOCROUT | egrep "^($COUNTRIES_TO_REJECT)" | cut -f 2 -d \
" " | egrep -v "^\[" | sed s/*//`; do
        echo -n "$i, " >> $file
    done
else exit 0
fi
echo finished >> $file

cp $LOCRC $TORRC

# Have Tor to reload its config
$RELOAD