-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
executable file
·84 lines (70 loc) · 1.56 KB
/
main.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
#!/usr/bin/env node
const { promisify } = require("util");
const lookup = promisify(require("dns").lookup);
function reverseIp(ip) {
return ip.split(".").reverse().join(".");
}
// https://github.com/assafmo/IsTorExit/pull/4
async function isTorExit(ip, print) {
let outputAddress;
try {
const result = await lookup(`${reverseIp(ip)}.dnsel.torproject.org`);
outputAddress = result.address;
} catch (e) {
if (e.code == "ENOTFOUND") {
if (print) {
console.log(ip, false);
}
return false;
} else {
throw e;
}
}
if (!outputAddress) {
if (print) {
console.log(ip, false);
}
return false;
}
const answer =
outputAddress.startsWith("127.0.0.") && outputAddress != "127.0.0.1";
if (print) {
console.log(ip, answer);
}
return answer;
}
module.exports = isTorExit;
if (require.main === module) {
const cmdArgs = process.argv.slice(2);
const helpMesg = `NAME
istorexit - Check if an IP is a Tor exit node
SYNOPSIS
istorexit [options] [IP...]
OPTIONS
-h, --help
Print this message and exit`;
if (cmdArgs.length == 0) {
console.log(helpMesg);
process.exit(0);
}
const ips = [];
for (let i = 0; i < cmdArgs.length; i++) {
if (cmdArgs[i] == "-h" || cmdArgs[i] == "--help") {
console.log(helpMesg);
process.exit(0);
}
ips.push(cmdArgs[i]);
}
(async () => {
for (const ip of ips) {
if (ip == null) {
continue;
}
try {
await isTorExit(ip, true);
} catch (e) {
throw e;
}
}
})();
}