-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample.js
More file actions
272 lines (268 loc) · 9.04 KB
/
example.js
File metadata and controls
272 lines (268 loc) · 9.04 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* A test script that runs a webserver on 127.0.0.1:2502
* And creates a small CLI that lets you execute ths commands
*/
var readline = require('readline');
var http = require('http');
var thsBuilder = require('./index');
var ths = new thsBuilder(undefined, undefined, undefined, console.error, console.log, function(controlMessage){console.log('Ctrl: ' + controlMessage)});
var server = http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello world!');
});
server.listen(2502, '127.0.0.1');
console.log('Server running at http://127.0.0.1:2502');
var rl = readline.createInterface({input: process.stdin, output: process.stdout});
rl.setPrompt('ths> ');
rl.prompt();
rl.on('line', function(line){
line = line.trim();
line = line.split(' ');
switch (line[0]){
case 'start':
var startCallback = function(){
console.log('Tor has been started');
}
if (line.length > 1 && line[1] == 'force'){
ths.start(true, startCallback);
} else ths.start(false, startCallback);
break;
case 'stop':
var stopCallback = function(){
console.log('Tor has been stopped');
};
ths.stop(stopCallback);
break;
case 'status':
console.log('Is tor running : ' + (ths.isTorRunning() ? 'Yes' : 'No'));
break;
case 'list':
var serviceList = ths.getServices();
for (var i = 0; i < serviceList.length; i++){
console.log('Service ' + (i + 1).toString() + ': ' + serviceList[i].name + ' - ' + serviceList[i].hostname);
for (var j = 0; j < serviceList[i].ports.length; j++){
console.log(serviceList[i].ports[j]);
}
console.log('');
}
break;
case 'onion':
//syntax : onion service-name
if (line.length > 1){
var serviceName = line[1];
console.log('Onion name for ' + serviceName + ' : ' + ths.getOnionAddress(serviceName));
} else {
console.log('Invalid command. Syntax : onion service-name');
}
break;
case 'async-onion':
if (line.length > 1){
var serviceName = line[1];
ths.getOnionAddress(serviceName, function(err, hostname){
if (err){
console.error('Error while reading hostname file: ' + err);
} else {
console.log('Onion name for ' + serviceName + ' : ' + hostname);
}
});
} else {
console.log('Invalid command. Syntax : async-onion service-name');
}
break;
case 'async-add-onion':
if (line.length > 3){
var serviceName = line[1];
var ports = [];
var actualPort;
for (var i = 1; i < (line.length - 1) / 2; i++){
actualPort = line[2*i] + ' ' + line[2*i + 1];
ports.push(actualPort);
}
ths.createHiddenService(serviceName, ports);
ths.saveConfig();
ths.getOnionAddress(serviceName, function(err, hostname){
if (err){
console.error('Error while reading hostname file: ' + err);
} else {
console.log('Onion name for ' + serviceName + ' : ' + hostname);
}
});
ths.signalReload();
} else {
console.log('Invalid comamnd. Syntax: async-add-onion service-name onePort target1 [otherPort otherTarget, ...]');
}
break;
case 'add':
//syntax : add service-name onePort target1 [port2 target2,...]
if (line.length > 3){
var serviceName = line[1];
var ports = [];
var actualPort;
for (var i = 1; i < (line.length - 1) / 2; i++){
actualPort = line[2*i] + ' ' + line[2*i + 1];
ports.push(actualPort);
}
ths.createHiddenService(serviceName, ports, true);
} else {
console.log('Invalid command. Syntax : add service-name onePort traget1 [otherPort otherTarget, ...]');
}
break;
case 'delete':
//syntax : delete service-name
if (line.length == 2){
var serviceName = line[1];
ths.removeHiddenService(serviceName, true);
} else {
console.log('Invalid command. Syntax : delete service-name');
}
break;
case 'rename':
//syntax : rename service-name new-name
if (line.length == 3){
var serviceName = line[1];
var newName = line[2];
ths.rename(serviceName, newName);
} else {
console.log('Invalid command. Syntax : rename service-name new-name');
}
break;
case 'addport':
//syntax : addport service-name port1 target1 [port2 target2,...]
if (line.length > 3){
var serviceName = line[1];
var ports = [];
var actualPort;
for (var i = 1; i < (line.length - 1) / 2; i++){
actualPort = line[2*i] + ' ' + line[2*i + 1];
ports.push(actualPort);
}
ths.addPorts(serviceName, ports, true);
} else {
console.log('Invalid command. Syntax : addport service-name port1 target1 [port2 target2]');
}
break;
case 'removeport':
//syntax : removeport service-name port1 target1 [port2 target2,...]
if (line.length > 3){
var serviceName = line[1];
var ports = [];
var actualPort;
for (var i = 1; i < (line.length - 1) / 2; i++){
actualPort = line[2*i] + ' ' + line[2*i + 1];
ports.push(actualPort);
}
ths.removePorts(serviceName, ports, false, true);
} else {
console.log('Invalid command. Syntax : removeport service-name port1 [port2,...]');
}
break;
case 'addbridge':
if (line.length > 1 && line.length <= 4){
var bridgeLine = '';
for (var i = 1; i < line.length; i++){
bridgeLine += line[i];
if (i != line.length - 1) bridgeLine += ' ';
}
ths.addBridge(bridgeLine, true);
} else {
console.log('Invalid command. Syntax : addbridge [transportName] bridgeIP:bridgePort [fingerprint]');
}
break;
case 'removebridge':
if (line.length > 1){
var bridgeAddress = line[1];
ths.removeBridge(bridgeAddress, true);
} else {
console.log('Invalid command. Syntax : removebridge bridgeAddress');
}
break;
case 'listbridges':
var bridgesList = ths.getBridges();
if (bridgesList.length == 0){
console.log('No bridges were added');
break;
}
console.log('Bridges:');
for (var i = 0; i < bridgesList.length; i++){
var bridgeLine = '';
if (bridgesList[i].transport) bridgeLine += bridgesList[i].transport + ' ';
bridgeLine += bridgesList[i].address;
if (bridgesList[i].fingerprint) bridgeLine += ' ' + bridgesList[i].fingerprint;
console.log(bridgeLine);
}
break;
case 'clearbridges':
ths.clearBridges();
break;
case 'addtransport':
if (line.length == 4){
var transportLine = '';
for (var i = 1; i < line.length; i++){
transportLine += line[i];
if (i != line.length - 1) transportLine += ' ';
}
ths.addTransport(transportLine, true);
} else {
console.log('Invalid command. Syntax addtransport transportName exec pathToBinary, or addtransport transportName socks4|socks5 IP:Port');
}
break;
case 'removetransport':
if (line.length > 1){
var transportName = line[1];
ths.removeTransport(transportName, true);
} else {
console.log('Invalid command. Syntax removetransport transportName');
}
break;
case 'listtransports':
var transportsList = ths.getTransports();
if (transportsList.length == 0){
console.log('No transports were added');
break;
}
console.log('Transports:');
for (var i = 0; i < transportsList.length; i++){
console.log(transportsList[i].name + ' ' + transportsList[i].type + ' ' + transportsList[i].parameter);
}
break;
case 'pid':
if (ths.isTorRunning()){
console.log('Tor PID : ' + ths.torPid());
} else {
console.log('The Tor process isn\'t running');
}
break;
case 'help':
console.log('Usage:\n' +
'start [force] -- Start the tor process\n' +
'stop -- Stop the tor process\n' +
'list -- List the configured hidden services\n' +
'status -- Get whether the tor process is running or not\n' +
'onion serviceName -- Get the onion address if the service named "serviceName", if defined\n' +
'add serviceName port1 traget1 [port2 target2...] -- Create a hidden service, referenced as "serviceName", with the given targets and ports\n' +
'delete serviceName -- Delete the service named "serviceName"\n' +
'rename oldServiceName newServiceName -- Rename in the config the "oldServiceName" service into "newServiceName"\n' +
'addport serviceName port1 target1 [port2 target2 ...] -- Add ports to service "serviceName"\n' +
'removeport serviceName -- Remove ports from "serviceName"\n' +
'addbridge [transport] bridgeIP:bridgePort [fingerprint] -- Add a Tor bridge to be used by the instance\n' +
'removebridge bridgeIP:bridgePort -- Remove a bridge for this instance\n' +
'listbridges -- List the bridges to be used by this instance\n' +
'clearbridges -- Clear the list of bridges\n' +
'addtransport transportName type parameter -- Add a pluggable transport to the Tor instance\n' +
'removetransport transportName -- Remove a pluggable transport\n' +
'listtransports -- List the added pluggable transports\n' +
'pid -- Get the tor process PID\n' +
'exit -- Exit this program');
break;
case 'exit':
process.exit(0);
break;
default:
console.log('Unknown command: ' + line[0]);
console.log('If you\'re lost, use the "help" command');
break;
}
rl.prompt();
}).on('close', function(){
ths.stop();
process.exit(0);
});