mirror of https://github.com/calzoneman/sync.git
Fixes, rate limiting
This commit is contained in:
parent
cfc866400c
commit
2c57d2a8f2
|
@ -14,6 +14,23 @@ var NotWebsocket = function() {
|
|||
this.handlers = {};
|
||||
this.room = "";
|
||||
this.lastpoll = Date.now();
|
||||
this.noflood = {};
|
||||
}
|
||||
|
||||
NotWebsocket.prototype.checkFlood = function(id, rate) {
|
||||
if(id in this.noflood) {
|
||||
this.noflood[id].push(Date.now());
|
||||
}
|
||||
else {
|
||||
this.noflood[id] = [Date.now()];
|
||||
}
|
||||
if(this.noflood[id].length > 10) {
|
||||
this.noflood[id].shift();
|
||||
var hz = 10000 / (this.noflood[id][9] - this.noflood[id][0]);
|
||||
if(hz > rate) {
|
||||
throw "Rate is too high: " + id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NotWebsocket.prototype.emit = function(msg, data) {
|
||||
|
@ -22,6 +39,7 @@ NotWebsocket.prototype.emit = function(msg, data) {
|
|||
}
|
||||
|
||||
NotWebsocket.prototype.poll = function() {
|
||||
this.checkFlood("poll", 100);
|
||||
this.lastpoll = Date.now();
|
||||
var q = [];
|
||||
for(var i = 0; i < this.pktqueue.length; i++) {
|
||||
|
@ -38,6 +56,7 @@ NotWebsocket.prototype.on = function(msg, callback) {
|
|||
}
|
||||
|
||||
NotWebsocket.prototype.recv = function(urlstr) {
|
||||
this.checkFlood("recv", 100);
|
||||
var msg, data;
|
||||
try {
|
||||
var js = JSON.parse(urlstr);
|
||||
|
@ -115,6 +134,7 @@ function msgReceived(req, res) {
|
|||
if(h in clients && clients[h] != null) {
|
||||
var str = req.params.str;
|
||||
res.callback = req.query.callback;
|
||||
try {
|
||||
if(str == "poll") {
|
||||
sendJSON(res, clients[h].poll());
|
||||
}
|
||||
|
@ -123,6 +143,10 @@ function msgReceived(req, res) {
|
|||
sendJSON(res, "");
|
||||
}
|
||||
}
|
||||
catch(e) {
|
||||
res.send(429); // 429 Too Many Requests
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.send(404);
|
||||
}
|
||||
|
|
|
@ -846,3 +846,9 @@ $.getScript(IO_URL+"/socket.io/socket.io.js", function() {
|
|||
Callbacks.disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
window.setupNewSocket = function() {
|
||||
for(var key in Callbacks) {
|
||||
socket.on(key, Callbacks[key]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1129,6 +1129,9 @@ function saveOpts() {
|
|||
}
|
||||
}
|
||||
|
||||
// To be overridden in callbacks.js
|
||||
function setupNewSocket() { }
|
||||
|
||||
function applyOpts() {
|
||||
$("#usertheme").remove();
|
||||
if(USEROPTS.theme != "default") {
|
||||
|
@ -1189,22 +1192,18 @@ function applyOpts() {
|
|||
}
|
||||
|
||||
if(USEROPTS.altsocket) {
|
||||
if(socket)
|
||||
socket.disconnect();
|
||||
socket = new NotWebsocket();
|
||||
for(var key in Callbacks) {
|
||||
socket.on(key, Callbacks[key]);
|
||||
}
|
||||
setupNewSocket();
|
||||
}
|
||||
// Switch from NotWebsocket => Socket.io
|
||||
else if(socket && typeof socket.poll !== "undefined") {
|
||||
try {
|
||||
socket = io.connect(IO_URL);
|
||||
for(var key in Callbacks) {
|
||||
socket.on(key, Callbacks[key]);
|
||||
}
|
||||
setupNewSocket();
|
||||
}
|
||||
catch(e) {
|
||||
Callbacks.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,3 +118,4 @@ NotWebsocket.prototype.disconnect = function() {
|
|||
this.reconnect();
|
||||
}.bind(this), this.reconndelay);
|
||||
}
|
||||
|
||||
|
|
|
@ -310,10 +310,10 @@
|
|||
<script src="./assets/js/jquery.js"></script>
|
||||
<!-- My Javascript -->
|
||||
<script src="./assets/js/iourl.js"></script>
|
||||
<script src="./assets/js/notwebsocket.js"></script>
|
||||
<script src="./assets/js/media.js"></script>
|
||||
<script src="./assets/js/functions.js"></script>
|
||||
<script src="./assets/js/client.js"></script>
|
||||
<script src="./assets/js/notwebsocket.js"></script>
|
||||
<script src="./assets/js/callbacks.js"></script>
|
||||
<!-- APIs -->
|
||||
<script src="http://api.dmcdn.net/all.js"></script>
|
||||
|
|
Loading…
Reference in New Issue