Time out idle connections.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3896 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-06-07 02:08:06 +00:00
parent ae894eaa9d
commit 1dc6084d2d

View File

@ -358,10 +358,12 @@ function badRequest(client, reason) {
gBadRequests[client.peerName] = { gBadRequests[client.peerName] = {
expire: new Date(now.getTime() + 10 * 60 * 1000), expire: new Date(now.getTime() + 10 * 60 * 1000),
count: 1, count: 1,
reason: reason,
}; };
count = 1; count = 1;
} else { } else {
old.count++; old.count++;
old.reason = reason;
count = old.count; count = old.count;
} }
new Response({version: '1.0'}, client).reportError(reason + ': ' + count); new Response({version: '1.0'}, client).reportError(reason + ': ' + count);
@ -385,7 +387,7 @@ function allowRequest(client) {
function handleConnection(client) { function handleConnection(client) {
if (!allowRequest(client)) { if (!allowRequest(client)) {
print('Rejecting client for too many bad requests: ', client.peerName); print('Rejecting client for too many bad requests: ', client.peerName, gBadRequests[client.peerName].reason);
client.close(); client.close();
return; return;
} }
@ -401,7 +403,11 @@ function handleConnection(client) {
function resetTimeout(requestIndex) { function resetTimeout(requestIndex) {
setTimeout(function() { setTimeout(function() {
if (bodyToRead == -1 && requestCount == requestIndex) { if (bodyToRead == -1 && requestCount == requestIndex) {
badRequest(client, 'Timed out waiting for request.'); if (requestCount == 0) {
badRequest(client, 'Timed out waiting for request.');
} else {
client.close();
}
} }
}, kRequestTimeout); }, kRequestTimeout);
} }
@ -470,6 +476,8 @@ function handleConnection(client) {
var requestObject = new Request(request[0], request[1], request[2], headers, body, client); var requestObject = new Request(request[0], request[1], request[2], headers, body, client);
var response = new Response(requestObject, client); var response = new Response(requestObject, client);
handleWebSocketRequest(requestObject, response, client); handleWebSocketRequest(requestObject, response, client);
/* Prevent the timeout from disconnecting us. */
requestCount++;
return false; return false;
} else { } else {
finish(); finish();