diff --git a/server/datahandler.js b/server/datahandler.js index 8d01809..bc29098 100644 --- a/server/datahandler.js +++ b/server/datahandler.js @@ -229,7 +229,7 @@ function handleData(wss, receivedData, rdsWss) { }).catch((error) => console.log("Error fetching Tx info:", error)); // Send the updated data to the client - const dataToSendJSON = JSON.stringify(dataToSend); + const dataToSendJSON = JSON.stringify({...dataToSend, timestamp: Math.floor(Date.now() / 1000)}); if (currentTime - lastUpdateTime >= updateInterval) { wss.clients.forEach((client) => client.send(dataToSendJSON)); lastUpdateTime = Date.now(); diff --git a/server/helpers.js b/server/helpers.js index 51c1030..9fc096e 100644 --- a/server/helpers.js +++ b/server/helpers.js @@ -280,19 +280,19 @@ function resolveDataBuffer(data) { if (receivedData.length) dataHandler.handleData(wss, receivedData, rdsWss); } - function kickClient(ipAddress) { - // Find the entry in connectedClients associated with the provided IP address const targetClient = storage.connectedUsers.find(client => client.ip === ipAddress); if (targetClient && targetClient.instance) { - // Send a termination message to the client targetClient.instance.send('KICK'); - // Close the WebSocket connection after a short delay to allow the client to receive the message setTimeout(() => { targetClient.instance.close(); + + const index = storage.connectedUsers.indexOf(targetClient); + if (index !== -1) storage.connectedUsers.splice(index, 1); + consoleCmd.logInfo(`Web client kicked (${ipAddress})`); - }, 500); + }, 750); } else consoleCmd.logInfo(`Kicking client ${ipAddress} failed. No suitable client found.`); } diff --git a/server/web.js b/server/web.js index bbb21fc..d6eb14f 100644 --- a/server/web.js +++ b/server/web.js @@ -66,20 +66,17 @@ wss.on('connection', (ws, request) => { // Per-IP limit connection open if (clientIp) { - const isLocalIp = (clientIp === '127.0.0.1' || clientIp === '::1' || clientIp.startsWith('192.168.') || clientIp.startsWith('10.') || clientIp.startsWith('172.16.')); - if (!isLocalIp) { - if (!ipConnectionCounts.has(clientIp)) ipConnectionCounts.set(clientIp, 0); - const currentCount = ipConnectionCounts.get(clientIp); - if (currentCount >= MAX_CONNECTIONS_PER_IP) { - ws.close(1008, 'Too many open connections from this IP'); - const lastLogTime = ipLogTimestamps.get(clientIp) || 0; - const now = Date.now(); - if (now - lastLogTime > IP_LOG_INTERVAL_MS) { - logWarn(`Web client \x1b[31mclosed: limit exceeded\x1b[0m (${clientIp}) \x1b[90m[${currentUsers}]`); - ipLogTimestamps.set(clientIp, now); - } return; - } ipConnectionCounts.set(clientIp, currentCount + 1); - } + if (!ipConnectionCounts.has(clientIp)) ipConnectionCounts.set(clientIp, 0); + const currentCount = ipConnectionCounts.get(clientIp); + if (currentCount >= MAX_CONNECTIONS_PER_IP) { + ws.close(1008, 'Too many open connections from this IP'); + const lastLogTime = ipLogTimestamps.get(clientIp) || 0; + const now = Date.now(); + if (now - lastLogTime > IP_LOG_INTERVAL_MS) { + logWarn(`Web client \x1b[31mclosed: limit exceeded\x1b[0m (${clientIp}) \x1b[90m[${currentUsers}]`); + ipLogTimestamps.set(clientIp, now); + } return; + } ipConnectionCounts.set(clientIp, currentCount + 1); } if(currentUsers >= MAX_USERS) { @@ -88,10 +85,7 @@ wss.on('connection', (ws, request) => { return; } - if (clientIp !== '127.0.0.1' || - (request.headers && request.headers['origin'] && request.headers['origin'].trim() !== '')) { - currentUsers++; - } + if (clientIp !== '127.0.0.1') currentUsers++; if (timeoutAntenna) clearTimeout(timeoutAntenna); @@ -170,24 +164,12 @@ wss.on('connection', (ws, request) => { ws.on('close', (code) => { // Per-IP limit connection closed if (clientIp) { - const isLocalIp = ( - clientIp === '127.0.0.1' || - clientIp === '::1' || - clientIp.startsWith('192.168.') || - clientIp.startsWith('10.') || - clientIp.startsWith('172.16.') - ); - if (!isLocalIp) { - const current = ipConnectionCounts.get(clientIp) || 1; - ipConnectionCounts.set(clientIp, Math.max(0, current - 1)); - } + const current = ipConnectionCounts.get(clientIp) || 1; + ipConnectionCounts.set(clientIp, Math.max(0, current - 1)); } - if (clientIp !== '127.0.0.1' || - (request.socket && request.socket.remoteAddress && request.socket.remoteAddress !== '::ffff:127.0.0.1') || - (request.headers && request.headers['origin'] && request.headers['origin'].trim() !== '')) { - currentUsers--; - } dataHandler.showOnlineUsers(currentUsers); + if (clientIp !== '127.0.0.1') currentUsers--; + dataHandler.showOnlineUsers(currentUsers); const index = storage.connectedUsers.findIndex(user => user.ip === clientIp); if (index !== -1) storage.connectedUsers.splice(index, 1); diff --git a/web/js/main.js b/web/js/main.js index 5f0998c..69e53bf 100644 --- a/web/js/main.js +++ b/web/js/main.js @@ -150,8 +150,6 @@ $(document).ready(function () { return false; }); - setInterval(getServerTime, 10000); - getServerTime(); setInterval(sendPingRequest, 5000); sendPingRequest(); @@ -259,37 +257,6 @@ $(document).ready(function () { initTooltips(); }); -function getServerTime() { - $.ajax({ - url: "./server_time", - dataType: "json", - success: function(data) { - const serverTimeUtc = data.serverTime; - - const options = { - year: 'numeric', - month: 'short', - day: 'numeric', - hour: '2-digit', - minute: '2-digit', - hour12: false - }; - - const serverOptions = { - ...options, - timeZone: 'Etc/UTC' - }; - - const formattedServerTime = new Date(serverTimeUtc).toLocaleString(navigator.language ? navigator.language : 'en-US', serverOptions); - - $("#server-time").text(formattedServerTime); - }, - error: function(jqXHR, textStatus, errorThrown) { - console.error("Error fetching server time:", errorThrown); - } - }); -} - function sendPingRequest() { const now = Date.now(); @@ -349,8 +316,21 @@ function handleWebSocketMessage(event) { resetDataTimeout(); updatePanels(parsedData); - const sum = signalData.reduce((acc, strNum) => acc + parseFloat(strNum), 0); - data.push(sum / signalData.length); + data.push(signalData.reduce((acc, strNum) => acc + parseFloat(strNum), 0) / signalData.length); + + if(parsedData.timestamp) { + const options = { + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + hour12: false, + timeZone: 'Etc/UTC' + }; + const formattedServerTime = new Date(parsedData.timestamp * 1000).toLocaleString(navigator.language ? navigator.language : 'en-US', options); + $("#server-time").text(formattedServerTime); + } } socket.onmessage = handleWebSocketMessage;