-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
148 lines (124 loc) · 3.58 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
/**
* @module granax
* @license AGPL-3.0
* @author Gordon Hall <[email protected]>
*/
'use strict';
const path = require('path');
const { spawn, execFileSync } = require('child_process');
const { platform } = require('os');
const { Socket } = require('net');
const { readFileSync } = require('fs');
const BIN_PATH = path.join(__dirname, 'bin');
const LD_LIBRARY_PATH = path.join(
BIN_PATH, 'tor-browser_en-US', 'Browser', 'TorBrowser', 'Tor'
);
/**
* Returns a {@link TorController} with automatically constructed socket
* to the local Tor bundle executable
* @param {object} options
* @param {object} torrcOptions
* @returns {TorController}
*/
module.exports = function(options, torrcOptions) {
let socket = new Socket();
let controller = new module.exports.TorController(socket, options);
let [torrc, datadir] = module.exports.torrc(torrcOptions);
let exe = path.basename(module.exports.tor(platform()));
let tor = path.join(BIN_PATH, 'Tor', exe);
let env = { LD_LIBRARY_PATH: path.join(BIN_PATH, 'Tor') };
if (process.env.GRANAX_USE_SYSTEM_TOR && process.platform === 'linux') {
tor = exe;
env = {};
}
let args = process.env.GRANAX_TOR_ARGS
? process.env.GRANAX_TOR_ARGS.split(' ')
: [];
let child = spawn(tor, ['-f', torrc].concat(args), {
cwd: BIN_PATH,
env
});
let portFileReads = 0;
controller.process = child; // NB: Expose the tor process to userland
function connect() {
let port = null;
try {
port = parseInt(readFileSync(path.join(
datadir,
'control-port'
)).toString().split(':')[1]);
} catch (err) {
/* istanbul ignore next */
portFileReads++;
/* istanbul ignore next */
if (portFileReads <= 20) {
return setTimeout(() => connect(), 1000);
} else {
return controller.emit('error',
new Error('Failed to read control port'));
}
}
socket.connect(port, '127.0.0.1');
}
/* istanbul ignore next */
process.on('exit', () => child.kill());
child.stdout.once('data', () => setTimeout(() => connect(), 1000));
child.on('error', (err) => controller.emit('error', err));
child.on('exit', (code) => {
controller.emit('error', new Error('Tor exited with code ' + code));
});
return controller;
};
/**
* Returns the local path to the tor bundle
* @returns {string}
*/
module.exports.tor = function(platform) {
/* eslint complexity: ["error", 8] */
let torpath = null;
/* istanbul ignore else */
if (process.env.GRANAX_USE_SYSTEM_TOR) {
try {
torpath = execFileSync(
platform === 'win32' ? 'where' : 'which',
['tor']
).toString().trim();
} catch (err) {
/* istanbul ignore next */
throw new Error('Tor is not installed');
}
return torpath;
}
switch (platform) {
case 'win32':
torpath = path.join(BIN_PATH, 'Browser', 'TorBrowser', 'Tor', 'tor.exe');
break;
case 'darwin':
torpath = path.join(BIN_PATH, '.tbb.app', 'Contents', 'MacOS', 'Tor',
'tor.real');
break;
case 'android':
case 'linux':
torpath = path.join(LD_LIBRARY_PATH, 'tor');
break;
default:
throw new Error(`Unsupported platform "${platform}"`);
}
return torpath;
};
/**
* {@link TorController}
*/
module.exports.TorController = require('./lib/controller');
/**
* {@link module:granax/commands}
*/
module.exports.commands = require('./lib/commands');
/**
* {@link module:granax/replies}
*/
module.exports.replies = require('./lib/replies');
/**
* {@link module:granax/torrc}
*/
module.exports.torrc = require('./lib/torrc');