How to set up multiple Tor instances with Polipo on Linux

How to set up multiple Tor instances with Polipo on Linux

Tor is free software and an open network that helps you defend against traffic analysis, a form of network surveillance that threatens personal freedom and privacy, confidential business activities and relationships, and state security.

By default, Tor opens a SOCK proxy on port 9050 that allow internet traffic to go through to access Tor network.

In this post, we will show how to set up multiple Tor instances on the same Linux machine.

If you want to set up multiple Tor instances with Polipo on Windows, read this post instead.

Install Tor

On Ubuntu

$ apt update
$ apt install tor

After that, Tor should be accessible via /usr/bin/tor

Install Polipo

Tor proxy supports SOCKS protocol but does not support HTTP proxy protocol.

To help Tor function as a proxy protocol, we can use Polipo to make a tunnel where Polipo will open a HTTP proxy and transfer the packets between its HTTP proxy and Tor’s SOCKS proxy. That way, an application can leverage Tor’s power even if that application can only communicate through HTTP proxy.

On Ubuntu

$ apt update
$ apt install polipo

Start the first instance

After Tor is installed, we can start Tor right away by running /usr/bin/tor.

However, to run multiple instances of Tor, we still have some extra tasks to do.

First of all, when a Tor instance runs, it will need the following things set up

  • a SOCKSPort, where its proxy listens on
  • a CONTROLPort, where it listens to commands from user
  • a DATADirectory, where it use to save data.

Different instances must have these things set up differently.

Fortunately, Tor allows us to set up these things using command line arguments like this

$ /usr/bin/tor SOCKSPort 10001 CONTROLPort 20001 DATADirectory /tmp/tor10001/data

You can also specify which IP address for your Tor instance to listen on

$ /usr/bin/tor SOCKSPort 127.0.0.1:10001 CONTROLPort 127.0.0.1:20001 DATADirectory /tmp/tor10001/data

Let’s start the first instance now

$ mkdir -p /tmp/tor10001/data
$ /usr/bin/tor SOCKSPort 10001 CONTROLPort 20001 DATADirectory /tmp/tor10001/data

We will also start a Polipo instance that tunnels through that Tor instance

$ polipo socksParentProxy="127.0.0.1:10001" proxyPort=30001 proxyAddress="127.0.0.1"

The command above will start a Polipo instance that has

  • 30001 as HTTP proxy port
  • talks to Tor proxy on port 10001

Start the next instances

In order to start the next instnace, first you have to relaunch the first instance to run in background with nohup command

$ mkdir -p /tmp/tor10001/data
$ nohup /usr/bin/tor SOCKSPort 10001 CONTROLPort 20001 DATADirectory /tmp/tor10001/data &
$ nohup polipo socksParentProxy="127.0.0.1:10001" proxyPort=30001 proxyAddress="127.0.0.1" &

nohup also keeps your Tor instance running after you log out of your ssh session.

You can now start the next instances following the same pattern, just change the arguments appropriately

$ mkdir -p /tmp/tor10002/data
$ nohup /usr/bin/tor SOCKSPort 10002 CONTROLPort 20002 DATADirectory /tmp/tor10002/data &
$ nohup polipo socksParentProxy="127.0.0.1:10002" proxyPort=30002 proxyAddress="127.0.0.1" &

Notice the params has changed to 10002 and 20002 correspondingly.

Automate the task

If you want to start a lot of instances, you can make a script to start all the instances automatically like this

start_all_tor.sh

#!/bin/bash

a=10001
b=20001
n=10100

echo "Start multiple Tors"
echo "Begin port " $a
echo "End port " $n

while [ $a -le $n ]
do 
    echo "Start Tor on port" $a
    mkdir -p /tmp/tor$a/data
    nohup /usr/bin/tor SOCKSPort $a CONTROLPort $b DATADirectory /tmp/tor$a/data &
    a=$(($a + 1)) 
    b=$(($b + 1)) 
done

The above script will start 100 Tor instances with SOCKSPort from 10001 to 10100, CONTROLPort from 20001 to 20100 with corresponding data folders.

start_all_polipo.sh

#!/bin/bash
a=10001
b=30001
n=10100
echo "Start multiple Polipo "
echo "Begin port " $a
echo "End port " $n

while [ $a -le $n ]
do
    echo "Start Polipo " $a
    nohup polipo socksParentProxy="127.0.0.1:$a" proxyPort=$b proxyAddress="127.0.0.1" &
    a=$(($a + 1))
    b=$(($b + 1))
done

To stop all the instances, you can run a script like this

stop_all_tor.sh

#!/bin/bash
ps aux | grep tor | awk '{print $2}' | xargs kill -9

stop_all_polipo.sh

#!/bin/bash
ps aux | grep polipo | awk '{print $2}' | xargs kill -9

Leave a Reply

Your email address will not be published. Required fields are marked *