-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
218 lines (203 loc) · 7.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const { spawn } = require("child_process");
const net = require("net");
const os = require("os");
const fs = require("fs");
const path = require("path");
const { localTorDir } = require("./config");
module.exports = class TorProxy {
#executablePath;
#options;
#torProcess;
#pid;
#torIPCTimeOut;
async #createTorrcFile(torrcPath, controlPassword) {
let torrcOptions = [];
torrcOptions.push(
`HashedControlPassword ${await this.#createControlPassword(
controlPassword
)}`
);
fs.writeFileSync(torrcPath, torrcOptions.join("\n"), "utf8");
}
#createControlPassword(controlPassword) {
return new Promise((resolve, reject) => {
spawn(this.#executablePath, ["--hash-password", controlPassword])
.stdout.on("data", (data) => {
let hashedPassword = data.toString().trim().split`\n`[0];
if (/^\d{2}:/.test(hashedPassword)) resolve(hashedPassword);
})
.on("error", (err) => {
reject(err);
})
.on("close", (code) => {
reject(
`Tor process exited with code ${code} (UNKNOWN ERROR)`
);
});
});
}
#torIPC(commands) {
return new Promise((resolve, reject) => {
let socket = net.connect(
{
host: this.#options.ip,
port: this.#options.controlPort,
},
function () {
let commandString = commands.join("\n") + "\n";
socket.write(commandString);
//resolve(commandString);
}
);
socket.on("error", function (err) {
reject(err);
});
let data = "";
socket.on("data", function (chunk) {
data += chunk.toString();
});
socket.on("end", function () {
resolve(data);
});
});
}
constructor(options) {
if (typeof options === "string" || typeof options === "number") {
options = {
port: options,
};
}
this.#options = {
path: path.join(__dirname, localTorDir, "Tor"),
ip: "127.0.0.1",
port: "9050",
controlPort: "9151",
controlPassword: "giraffe", // default password
dataPath: path.join(__dirname, localTorDir, "data", "default"),
torrcPath: path.join(__dirname, localTorDir, "Tor", "torrc"),
...options,
};
this.#executablePath = `${this.#options.path}/${this.#options.executable ? this.#options.executable : "tor.exe"
}`;
if (!fs.existsSync(this.#options.dataPath)) {
fs.mkdirSync(this.#options.dataPath, { recursive: true });
}
}
async startTorProcess() {
await this.#createTorrcFile(
this.#options.torrcPath,
this.#options.controlPassword
);
if (this.#pid) {
await this.killTorProcess();
this.#pid = null;
}
return await new Promise((resolve, reject) => {
this.#torProcess = spawn(this.#executablePath, [
"--SocksPort",
this.#options.port,
"--ControlPort",
this.#options.controlPort,
"--DataDirectory",
this.#options.dataPath,
"-f",
this.#options.torrcPath,
"--GeoIPv6File",
path.resolve(this.#options.path, "geoip6"),
"--GeoIPFile",
path.resolve(this.#options.path, "geoip"),
]);
this.#torProcess.stdout.on("data", (data) => {
// console.log(data.toString() + "\n__________");
if (data.toString().includes("100% (done)")) {
this.#pid = this.#torProcess.pid;
resolve(this.#options.port);
}
if (data.toString().includes("Address already in use")) {
if (data.toString().includes("Opening Control listener")) {
throw new Error("Control port already in use");
}
throw new Error("Port already in use");
}
if (data.toString().includes("same data directory.")) {
throw new Error(
"It looks like another Tor process is running with the same data directory."
);
}
if (data.toString().includes("[err]")) {
throw new Error(
"Tor process failed to start \n" + data.toString()
);
}
});
this.#torProcess
.on("error", (err) => {
reject(`Tor process error: ${err}`);
})
.on("close", (code) => {
reject(`tor process closed with code ${code}`);
})
.on("exit", (code) => {
reject(`tor process exited with code ${code}`);
})
.on("error", (err) => {
reject(`Tor process stderr error: ${err}`);
});
});
}
killTorProcess() {
return new Promise((resolve, reject) => {
try {
let pid = this.#pid;
if (!pid) throw new Error("Tor process not running");
this.#torProcess.kill();
resolve();
} catch (err) {
reject(err);
}
});
}
newTorSession() {
let commands = [
'authenticate "' + this.#options.controlPassword + '"', // authenticate the connection
"signal newnym", // send the signal (renew Tor session)
"quit", // close the connection
];
return new Promise((resolve, reject) => {
let rateLimitingTime = this.#torIPCTimeOut - new Date().getTime();
if (!this.#torIPCTimeOut || rateLimitingTime < 0)
rateLimitingTime = 0;
setTimeout(() => {
this.#torIPC(commands)
.then((data) => {
let lines = data.split(os.EOL).slice(0, -1);
let success = lines.every((val, ind, arr) => {
// each response from the ControlPort should start with 250 (OK STATUS)
return val.length <= 0 || val.indexOf("250") >= 0;
});
if (!success) {
if (data.toString().includes("Password did not match HashedControlPassword")) {
throw new Error(
"controlPassword password did not match 'HashedControlPassword' (in torrc file)"
);
}
let err = new Error(
"Error communicating with Tor ControlPort\n" +
data
);
reject(err);
}
let RATE_LIMITING_NEWNYM_REQUEST_TIME = 10000;
this.#torIPCTimeOut =
new Date().getTime() +
RATE_LIMITING_NEWNYM_REQUEST_TIME;
resolve("Tor session successfully renewed!!");
//resolve(data);
})
.catch(function (err) {
reject(err);
});
}, rateLimitingTime);
});
}
};