Skip to content

Commit ba28685

Browse files
committed
add logic to prevent people connecting too quickly
1 parent d703f96 commit ba28685

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

config.sample.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ exports.ircHost = 'irc.example.net';
66
exports.password = "foobar";
77
exports.redirectUrl = "http://webirc.oftc.net";
88
exports.module = 'ws';
9+
exports.reconnectTime = 15 * 1000;

proxy.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var tls = require('tls');
55

66
var config = require('./config');
77

8+
config.reconnectTime = config.reconnectTime || 15 * 1000;
9+
810
var opts = {
911
key: fs.readFileSync(config.key),
1012
cert: fs.readFileSync(config.cert),
@@ -23,7 +25,7 @@ switch (config.module) {
2325
var io = require('socket.io').listen(server);
2426
io.set('log level', 1);
2527
io.sockets.on('connection', function (client) {
26-
ws_client_connect(client, client.handshake.address.address);
28+
can_connect(client, client.handshake.address.address);
2729
});
2830
break;
2931
case 'ws':
@@ -35,12 +37,38 @@ switch (config.module) {
3537
return true;
3638
},
3739
}).on('connection', function (client) {
38-
ws_client_connect(client, client._socket.remoteAddress);
40+
can_connect(client, client._socket.remoteAddress);
3941
});
4042
break;
4143
}
4244

45+
var last_connect = {};
46+
47+
setInterval(function () {
48+
var k, now = Date.now(), maxAge = 5 * config.reconnectTime;
49+
for (k in last_connect) {
50+
if ((now - last_connect[k]) > maxAge) {
51+
delete last_connect[k];
52+
}
53+
}
54+
}, 5 * config.reconnectTime);
55+
56+
function can_connect(client, ip) {
57+
var time = last_connect[ip];
58+
if (time !== undefined && (Date.now() - time) < config.reconnectTime) {
59+
console.log('client connecting too fast', ip, time, Date.now());
60+
client.send('ERROR :Trying to reconnect too fast.\r\n');
61+
if (client.close)
62+
client.close();
63+
else
64+
client.disconnect();
65+
} else {
66+
ws_client_connect(client, ip);
67+
}
68+
}
69+
4370
function ws_client_connect(client, ip) {
71+
last_connect[ip] = Date.now();
4472
console.log('client connected', ip);
4573
dns.reverse(ip, function (err, domains) {
4674
if (err) {
@@ -89,7 +117,6 @@ function ws_client_resolved(client, ip, host) {
89117
});
90118

91119
client.on('message', function (msg) {
92-
console.log('writing', ip, remote.writable);
93120
if (!webirc) {
94121
webirc = true;
95122
// WEBIRC <password> <user> <host> <ip>
@@ -108,7 +135,7 @@ function ws_client_resolved(client, ip, host) {
108135
});
109136

110137
client.on('disconnect', function () {
111-
console.log('client hungip', ip);
138+
console.log('client hungup', ip);
112139
remote.end();
113140
});
114141
}

0 commit comments

Comments
 (0)