From 512dc3bcbce82f793a8e873260a04743c5e2e34e Mon Sep 17 00:00:00 2001 From: sairajzero Date: Wed, 26 Jan 2022 21:46:19 +0530 Subject: [PATCH] Automatically reload user page upon incorrect server - Upon receiving responseError INCORRECT_SERVER_ERROR, reload the client page. (Triggered mainly when master is changed). - Fixed some minor syntax bugs --- public/fn.js | 5 ++++- src/app.js | 2 +- src/request.js | 8 +++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/public/fn.js b/public/fn.js index f225ab0..7501148 100644 --- a/public/fn.js +++ b/public/fn.js @@ -1,4 +1,5 @@ //console.log(document.cookie.toString()); +const INVALID_SERVER_MSG = "INCORRECT_SERVER_ERROR"; var nodeList, nodeURL, nodeKBucket; //Container for (backup) node list function exchangeAPI(api, options) { @@ -69,7 +70,9 @@ const tokenAPI = { } function ResponseError(status, data) { - if (this instanceof ResponseError) { + if (data === INVALID_SERVER_MSG) + location.reload(); + else if (this instanceof ResponseError) { this.data = data; this.status = status; } else diff --git a/src/app.js b/src/app.js index cc05e06..590c03d 100644 --- a/src/app.js +++ b/src/app.js @@ -4,7 +4,7 @@ const express = require('express'); //const sessions = require('express-session'); const Request = require('./request'); -const REFRESH_INTERVAL = 5 * 1000; //10 * 60 * 1000; +const REFRESH_INTERVAL = 1 * 60 * 1000; module.exports = function App(secret, DB) { diff --git a/src/request.js b/src/request.js index 8f4055e..44c11c3 100644 --- a/src/request.js +++ b/src/request.js @@ -22,19 +22,19 @@ const oneDay = 1000 * 60 * 60 * 24; const maxSessionTimeout = 60 * oneDay; var serving; -const INVALID_SERVER_MSG = "Incorrect Server. Please connect to main server."; +const INVALID_SERVER_MSG = "INCORRECT_SERVER_ERROR"; function validateRequestFromFloID(request, sign, floID, proxy = true) { return new Promise((resolve, reject) => { if (!serving) return reject(INVALID(INVALID_SERVER_MSG)); else if (!floCrypto.validateAddr(floID)) - return reject(INVALID.e_code).send("Invalid floID"); + return reject(INVALID("Invalid floID")); DB.query("SELECT " + (proxy ? "session_time, proxyKey AS pubKey FROM Sessions" : "pubKey FROM Users") + " WHERE floID=?", [floID]).then(result => { if (result.length < 1) return reject(INVALID(proxy ? "Session not active" : "User not registered")); if (proxy && result[0].session_time + maxSessionTimeout < Date.now()) - return reject(INVALID.e_code).send("Session Expired! Re-login required"); + return reject(INVALID("Session Expired! Re-login required")); let req_str = validateRequest(request, sign, result[0].pubKey); req_str instanceof INVALID ? reject(req_str) : resolve(req_str); }).catch(error => reject(error)); @@ -62,6 +62,8 @@ function storeRequest(floID, req_str, sign) { } function getLoginCode(req, res) { + if (!serving) + return res.status(INVALID.e_code).send(INVALID_SERVER_MSG); let randID = floCrypto.randString(8, true) + Math.round(Date.now() / 1000); let hash = Crypto.SHA1(randID + secret); res.send({