Add module to block Tor IPs

This commit is contained in:
calzoneman 2013-11-04 16:04:24 -06:00
parent d06ca9a99f
commit 4ad22308a0
4 changed files with 104 additions and 3 deletions

View File

@ -49,7 +49,8 @@ var defaults = {
"stat-interval" : 3600000, "stat-interval" : 3600000,
"stat-max-age" : 86400000, "stat-max-age" : 86400000,
"alias-purge-interval" : 3600000, "alias-purge-interval" : 3600000,
"alias-max-age" : 2592000000 "alias-max-age" : 2592000000,
"tor-blocker" : false
} }
function save(cfg, file) { function save(cfg, file) {

View File

@ -9,7 +9,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
const VERSION = "2.4.4"; const VERSION = "2.4.5";
var singleton = null; var singleton = null;
module.exports = { module.exports = {
@ -63,6 +63,7 @@ var Server = function (cfg) {
self.httplog = null; self.httplog = null;
self.actionlog = null; self.actionlog = null;
self.infogetter = null; self.infogetter = null;
self.torblocker = null;
// database init ------------------------------------------------------ // database init ------------------------------------------------------
var Database = require("./database"); var Database = require("./database");
@ -169,6 +170,11 @@ var Server = function (cfg) {
// background tasks init ---------------------------------------------- // background tasks init ----------------------------------------------
require("./bgtask")(self); require("./bgtask")(self);
// tor blocker init ---------------------------------------------------
if (self.cfg["tor-blocker"]) {
self.torblocker = require("./torblocker")();
}
}; };
Server.prototype.getHTTPIP = function (req) { Server.prototype.getHTTPIP = function (req) {
@ -268,6 +274,15 @@ Server.prototype.handleSocketConnection = function (socket) {
var ip = self.getSocketIP(socket); var ip = self.getSocketIP(socket);
socket._ip = ip; socket._ip = ip;
if (self.torblocker && self.torblocker.shouldBlockIP(ip)) {
socket.emit("kick", {
reason: "This server does not allow connections from Tor. "+
"Please log in with your regular internet connection."
});
Logger.syslog.log("Blocked Tor IP: " + ip);
socket.disconnect(true);
}
if (!(ip in self.ipThrottle)) { if (!(ip in self.ipThrottle)) {
self.ipThrottle[ip] = $util.newRateLimiter(); self.ipThrottle[ip] = $util.newRateLimiter();
} }

85
lib/torblocker.js Normal file
View File

@ -0,0 +1,85 @@
var https = require("https");
var path = require("path");
var fs = require("fs");
var domain = require("domain");
var Logger = require("./logger");
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) {
Logger.errlog.log(err.trace());
});
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);
fs.writeFile(path.join(__dirname, "..", "torlist"),
ips.join("\n"));
return;
}
fs.readFile(path.join(__dirname, "..", "torlist"), function (err, data) {
if (err) {
cb(err, null);
return;
}
data = (""+data).split("\n");
cb(false, data);
});
});
}
module.exports = function () {
var x = {
ipList: [],
shouldBlockIP: function (ip) {
return this.ipList.indexOf(ip) >= 0;
}
};
var init = function () {
getTorIPs(function (err, ips) {
if (err) {
Logger.errlog.log(err);
return;
}
Logger.syslog.log("Loaded Tor IP list");
x.ipList = ips;
});
};
init();
return x;
};

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "2.4.4", "version": "2.4.5",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },