-
Notifications
You must be signed in to change notification settings - Fork 0
/
tor-detector.js
executable file
·244 lines (231 loc) · 7.68 KB
/
tor-detector.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
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
const filesystem = require('fs');
const https = require('https');
const net = require('net');
var TorDetector = {
/**
* @var String listPath A string containing the path to the file that contains a list of Tor exit points separated by a breakline (\n).
*/
listPath: null,
/**
* @var String list A string containing the content of the list, if it is going to be cached for next uses.
*/
list: null,
/**
* @var Boolean cache If set to "true", the content of the list will be cached for next uses, otherwise not.
*/
cache: false,
/**
* Sets the path to the list file, this method is chainable.
*
* @param String path A string containing the path to the list.
*
* @throws exception If an invalid file path is provided.
*/
setListPath: function(path){
if ( typeof(path) !== 'string' ){
throw 'Invalid path.';
}
if ( path === '' ){
path = null;
}
if ( this.listPath !== path ){
this.list = this.cache === false ? null : '';
this.listPath = path;
}
return this;
},
/**
* Returns the path to the list.
*
* @return String A string containing the path to the list.
*/
getListPath: function(){
return this.listPath === '' || typeof(this.listPath) !== 'string' ? null : this.listPath;
},
/**
* Sets if the list cache shall be used or not, this method is chainable.
*
* @param Boolean value If set to "true", the content of the list will be cached for next uses, otherwise not.
*/
setListCache: function(value){
if ( value !== true ){
this.cache = false;
this.list = null;
return this;
}
this.cache = true;
return this;
},
/**
* Returns if the list cache is enabled or not.
*
* @return Boolean If the list cache is enabled will be returned "true", otherwise "false".
*/
getListCache: function(){
return this.cache === false ? false : true;
},
/**
* Cleares the content of the list cache, this method is chainable.
*/
invalidateDictionaryCache: function(){
this.list = null;
return this;
},
/**
* Loads the content of the list that has been set.
*
* @param Boolean asynchronous If set to "false" the operation will be done in synchronous way, otherwise in asynchronous way with promise support.
*
* @throws exception If an error occurs while reading list contents.
*/
loadDictionaryCache: function(asynchronous){
if ( asynchronous !== false ){
return new Promise(function(resolve, reject){
try{
resolve(TorDetector.loadDictionaryCache(false));
}catch(ex){
reject(ex);
}
});
}
if ( this.cache === false || this.listPath === '' || typeof(this.listPath) !== 'string' ){
return false;
}
try{
let content = filesystem.readFileSync(this.listPath).toString();
if ( content === '' ){
return false;
}
this.list = content;
return true;
}catch(ex){
throw 'Unable to load the list.';
}
},
/**
* Updates the content of the list by downloading a new list of Tor exit points, this method is asynchronous and will return a promise used to handle method success or failure.
*
* @throws exception If no file path has been set previously.
* @throws exception If an error occurs while writing the file.
* @throws exception If an error occurs while downloading the data.
*/
updateFile: function(){
return new Promise(function(resolve, reject){
if ( typeof(TorDetector.listPath) !== 'string' || TorDetector.listPath === '' ){
reject('No path has been set.');
}
https.get('https://check.torproject.org/exit-addresses', function(response){
response.setEncoding('UTF-8');
let content = '';
response.on('data', function(data){
content += data;
});
response.on('end', function(data){
content = content.split('\n');
let list = '';
for ( let i = 0 ; i < content.length ; i++ ){
if ( content[i].indexOf('ExitAddress') !== 0 ){
continue;
}
let buffer = content[i].substr(content[i].indexOf(' ') + 1);
if ( buffer === '' ){
continue;
}
list += list === '' ? buffer.substr(0, buffer.indexOf(' ')).toLowerCase() : ( '\n' + buffer.substr(0, buffer.indexOf(' ')).toLowerCase() );
}
try{
filesystem.writeFileSync(__dirname + '/' + TorDetector.listPath, list);
resolve();
}catch(ex){
reject('Unable to save the file.');
}
});
response.on('error', function(){
reject('An error occurred while getting the data.');
});
});
});
},
/**
* Returns the client's IP address.
*
* @param Object request The object that represent the connection with the client.
* @param Boolean proxy If set to "false" will be returned the IP address found in the request, otherwise will be checked for proxy presence, if a proxy were found, will be returned the IP of the client that is using this proxy.
*
* @return String A string containing the client's IP address, if no valid IP address were found, will be returned null.
*/
getClientIPAddress: function(request, proxy){
if ( proxy !== false ){
if ( typeof(request.headers['x-forwarded-for']) === 'string' && request.headers['x-forwarded-for'] !== '' ){
let address = request.headers['x-forwarded-for'].split(',').pop().trim();
if ( net.isIP(address) !== 0 ){
return address.toLowerCase();
}
}
}
if ( typeof(request.connection.remoteAddress) === 'string' && request.connection.remoteAddress !== '' ){
let address = request.connection.remoteAddress.trim();
if ( net.isIP(address) !== 0 ){
return address.toLowerCase();
}
}
if ( typeof(request.socket.remoteAddress) === 'string' && request.socket.remoteAddress !== '' ){
let address = request.socket.remoteAddress.trim();
if ( net.isIP(address) !== 0 ){
return address.toLowerCase();
}
}
if ( typeof(request.connection.socket.remoteAddress) === 'string' && request.connection.socket.remoteAddress !== '' ){
let address = request.connection.socket.remoteAddress.trim();
if ( net.isIP(address) !== 0 ){
return address.toLowerCase();
}
}
return null;
},
/**
* Checks if a given IP address is assigned to a Tor exit point or not: basically, checks if a client is using Tor or not.
*
* @param String address A string containing the IP address to check.
* @param Boolean asynchronous If set to "false" the operation will be done in synchronous way, otherwise in asynchronous way with promise support.
*
* @throws exception If the given IP address is not valid.
* @throws exception If no file path has been set previously.
* @throws exception If the list read from the file is empty.
* @throws exception If an error occurs while reading the content from the file.
*/
isTor: function(address, asynchronous){
if ( asynchronous !== false ){
return new Promise(function(resolve, reject){
try{
resolve(TorDetector.isTor(address, false));
}catch(ex){
reject(ex);
}
});
}
if ( typeof(address) !== 'string' || address === '' || net.isIP(address) === 0 ){
throw 'Invalid IP address.';
}
if ( typeof(TorDetector.listPath) !== 'string' || TorDetector.listPath === '' ){
throw 'No path has been set.';
}
address = address.toLowerCase();
if ( TorDetector.cache === true && typeof(TorDetector.list) === 'string' ){
return TorDetector.list.indexOf(address + '\n') >= 0 || TorDetector.list.indexOf('\n' + address) >= 0 || TorDetector.list === address ? true : false;
}
try{
let content = filesystem.readFileSync(__dirname + '/' + TorDetector.listPath).toString();
if ( content === '' ){
throw 'The given list is empty.';
}
if ( TorDetector.cache === true ){
TorDetector.list = content;
}
return content.indexOf(address + '\n') >= 0 || content.indexOf('\n' + address) >= 0 ? true : false;
}catch(ex){
throw 'An error occurred while reading the file content.';
}
}
}
module.exports = TorDetector;