https://github.com/danielstjules/toragent
HTTP(S) requests through Tor for Node
https://github.com/danielstjules/toragent
agent nodejs tor
Last synced: 9 months ago
JSON representation
HTTP(S) requests through Tor for Node
- Host: GitHub
- URL: https://github.com/danielstjules/toragent
- Owner: danielstjules
- License: mit
- Created: 2015-08-15T21:38:12.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-09-28T10:05:25.000Z (almost 8 years ago)
- Last Synced: 2025-06-01T08:03:38.019Z (about 1 year ago)
- Topics: agent, nodejs, tor
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 32
- Watchers: 3
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# toragent
Easily manage HTTP(S) requests through Tor. TorAgent spawns Tor processes,
handles both HTTP and HTTPS requests, and offers easy IP address rotation.
Compatible with `http.request` and libraries like `request`.
[](https://travis-ci.org/danielstjules/toragent)
## Installation
Requires Node >= 4.
```
npm install --save toragent
```
## Overview
Works with promises.
``` javascript
var TorAgent = require('toragent');
var Promise = require('bluebird');
var request = Promise.promisify(require('request'));
TorAgent.create().then((agent) => {
return request({
url: 'https://www.google.com',
agent: agent,
});
}).spread((res, body) => {
console.log(body);
});
```
And callbacks too!
``` javascript
var TorAgent = require('toragent');
var request = require('request');
TorAgent.create(false, (err, agent) => {
if (err) return console.log(err);
request({
url: 'https://www.google.com',
agent: agent,
}, (err, res, body) => {
console.log(err || body);
});
});
```
## TorAgent
An HTTP Agent for proxying requests through Tor using SOCKS5. In the following
examples, `agent` refers to an instance of `TorAgent`.
#### TorAgent.create(verbose, [fn])
Spawns a Tor process listening on a random unused port, and creates an
agent for use with HTTP(S) requests. The optional verbose param will enable
console output while initializing Tor. Accepts an optional callback,
otherwise it returns a promise that resolves with an instance of TorAgent.
Note that since the Tor process is using a new DataDirectory with no cached
microdescriptors or any other state, bootstrapping can take between 15 - 60s.
The resulting child process is automatically killed when node exits.
``` javascript
TorAgent.create(true).then((agent) => {
// Spawning Tor
// Tor spawned with pid 42776 listening on 55683
});
```
#### agent.rotateAddress()
Rotates the IP address used by Tor by sending a SIGHUP. Returns a promise
that resolves when complete.
``` javascript
TorAgent.create().then(agent => agent.rotateAddress());
```
#### agent.destroy()
Closes all sockets handled by the agent, and closes the Tor process. Returns
a promise that resolves when the Tor process has closed.
``` javascript
TorAgent.create().then(agent => agent.destroy());
```