sync/src/tor.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-11-04 22:04:24 +00:00
var https = require("https");
var path = require("path");
var fs = require("fs");
var domain = require("domain");
2017-04-05 06:02:31 +00:00
import { LoggerFactory } from '@calzoneman/jsli';
const LOGGER = LoggerFactory.getLogger('tor');
2013-11-04 22:04:24 +00:00
function retrieveIPs(cb) {
var options = {
host: "www.dan.me.uk",
port: 443,
path: "/torlist/",
method: "GET"
};
var finish = function (status, data) {
if (status !== 200) {
cb(new Error("Failed to retrieve Tor IP list (HTTP " + status + ")"), null);
return;
}
var ips = data.split("\n");
cb(false, ips);
};
var d = domain.create();
d.on("error", function (err) {
if (err.stack)
2017-04-05 06:02:31 +00:00
LOGGER.error(err.stack);
2013-11-14 22:44:40 +00:00
else
2017-04-05 06:02:31 +00:00
LOGGER.error(err);
2013-11-04 22:04:24 +00:00
});
d.run(function () {
var req = https.request(options, function (res) {
var buffer = "";
res.setEncoding("utf-8");
res.on("data", function (data) { buffer += data; });
res.on("end", function () { finish(res.statusCode, buffer); });
});
req.end();
});
}
function getTorIPs(cb) {
retrieveIPs(function (err, ips) {
if (!err) {
cb(false, ips);
const destination = path.join(__dirname, "..", "torlist");
fs.writeFile(destination,
ips.join("\n"),
error => {
if (error) {
LOGGER.error("Failed to write to %s: %s", destination, error);
}
});
2013-11-04 22:04:24 +00:00
return;
}
fs.readFile(path.join(__dirname, "..", "torlist"), function (err, data) {
if (err) {
cb(err, null);
return;
}
data = (""+data).split("\n");
cb(false, data);
});
});
}
var _ipList = [];
getTorIPs(function (err, ips) {
if (err) {
2017-04-05 06:02:31 +00:00
LOGGER.error(err);
return;
}
2013-11-04 22:04:24 +00:00
2017-04-05 06:02:31 +00:00
LOGGER.info("Loaded Tor IP list");
_ipList = ips;
});
2013-11-04 22:04:24 +00:00
exports.isTorExit = function (ip) {
2014-08-15 02:45:25 +00:00
return _ipList.indexOf(ip) >= 0;
2013-11-04 22:04:24 +00:00
};