torrrly v1.0.0
torrrly
Send HTTP/S requests through Tor network using any request library that supports http.Agent parameter.
Install
$ npm install torrrly
Usage
const torrrly = require('torrrly');
const got = require('got');
(async () => {
const agent = torrrly.createAgent();
const responseA = await got('http://api.ipify.org', { agent });
console.log(responseA.body); // 192.42.116.25
// refresh your Tor client IP address
await torrrly.refreshSession('candy');
// no need to create another agent
const responseB = await got('http://api.ipify.org', { agent });
console.log(responseB.body); // 77.247.181.163
})();
API
createAgent(ip?, port?, version?)
Returns a valid http.Agent that can be attached to various request libraries.
ip
Type: string
\
Default: localhost
Tor client ip address.
port
Type: string | number
\
Default: 9050
Tor client port.
version
SOCKS protocol version.
Type: string | number
\
Default: 5
refreshSession(password?, host?, port?)
Asynchronously refresh Tor client current IP address using ControlPort TCP server.\
It returns an Promise(Error? | null)
.
password
Type: string
\
Default: ''
Tor ControlPort password.
host
Type: string
\
Default: localhost
Tor ControlPort host.
port
Tor ControlPort port.
Type: string | number
\
Default: 9051
Requirements
You need a configured and running Tor client.
It's highly recommended you run the official client yourself.
On Debian/Ubuntu you can install and run a relatively up to date Tor with.
apt install tor
Misc Linux Command for running Tor as daemon
--RunAsDaemon 1
/usr/bin/tor --RunAsDaemon 1
On OSX you can install with homebrew
brew install tor
tor
If you'd like to run it as a background process you can add &
at the end of the command tor &
. I like to have it running on a separate terminal window/tab/tmux/screen during development in order to see what's going on.
On Windows download the tor expert bundle (not the browser), unzip it and run tor.exe
inside the Tor/ directory.
download link: Windows Expert Bundle
./Tor/tor.exe # -f PATH_TO_TORRC
See TorProject.org for detailed installation guides for all platforms.
The Tor client by default runs on localhost at port 9050. You can change it if needed.
(Optional) Configuring Tor, enabling the ControlPort
You need to enable the Tor ControlPort if you want to programmatically refresh the Tor session (i.e., get a new proxy IP address) without restarting your Tor client.
Configure tor by editing the torrc file usually located at /etc/tor/torrc, /lib/etc/tor/torrc, ~/.torrc or /usr/local/etc/tor/torrc - Alternatively you can supply the path yourself with the --default-torrc PATH command line argument. See Tor Command-Line Options
Generate the hash password for the torrc file by running tor --hash-password SECRETPASSWORD.
tor --hash-password candy
The last line of the output contains the hash password that you copy paste into torrc
Jul 21 13:08:50.363 [notice] Tor v0.2.6.10 (git-58c51dc6087b0936) running on Darwin with Libevent 2.0.22-stable, OpenSSL 1.0.2h and Zlib 1.2.5.
Jul 21 13:08:50.363 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
16:AEBC98A6777A318660659EC88648EF43EDACF4C20D564B20FF244E81DF
Copy the generated hash password and add it to your torrc file
# sample torrc file
ControlPort 9051
HashedControlPassword 16:AEBC98A6777A318660659EC88648EF43EDACF4C20D564B20FF244E81DF
Now you can automatically refresh Tor session like in the example below
const torrrly = require('torrrly');
(async () => {
await torrrly.refreshSession('candy');
})();
4 years ago