revert time tolerance, calculate time delta from internet

This commit is contained in:
creeper123123321 2021-09-04 11:33:23 -03:00
parent 2fb35f0e2b
commit 8e9290fcc9
3 changed files with 9 additions and 6 deletions

View File

@ -26,7 +26,7 @@ class WebLogin : WebState {
"offline_login" -> { "offline_login" -> {
if (!sha512Hex(msg.toByteArray(Charsets.UTF_8)).startsWith("00000")) throw StacklessException("PoW failed") if (!sha512Hex(msg.toByteArray(Charsets.UTF_8)).startsWith("00000")) throw StacklessException("PoW failed")
if ((obj.getAsJsonPrimitive("date").asLong - System.currentTimeMillis()) if ((obj.getAsJsonPrimitive("date").asLong - System.currentTimeMillis())
.absoluteValue > Duration.ofSeconds(60).toMillis() .absoluteValue > Duration.ofSeconds(20).toMillis()
) { ) {
throw StacklessException("Invalid PoW date") throw StacklessException("Invalid PoW date")
} }

View File

@ -29,6 +29,8 @@ let accounts = document.getElementById("accounts-list");
let cors_proxy_txt = document.getElementById("cors-proxy"); let cors_proxy_txt = document.getElementById("cors-proxy");
let ws_url_txt = document.getElementById("ws-url"); let ws_url_txt = document.getElementById("ws-url");
let listenVisible = false; let listenVisible = false;
// + deltaTime means that the clock is ahead
let deltaTime = 0;
let workers = []; let workers = [];
$(() => { $(() => {
workers = new Array(navigator.hardwareConcurrency) workers = new Array(navigator.hardwareConcurrency)
@ -160,14 +162,13 @@ function renderActions() {
let user = prompt("Offline username (case-sensitive):", ""); let user = prompt("Offline username (case-sensitive):", "");
if (!user) return; if (!user) return;
let taskId = Math.random(); let taskId = Math.random();
workers.forEach(it => it.postMessage({action: "listen_pow", user: user, id: taskId})); workers.forEach(it => it.postMessage({action: "listen_pow", user: user, id: taskId, deltaTime: deltaTime}));
addToast("Offline username", "Please wait a minute..."); addToast("Offline username", "Please wait a minute...");
}); });
} }
} }
function onWorkerMsg(e) { function onWorkerMsg(e) {
console.log(e);
if (e.data.action === "completed_pow") onCompletedPoW(e); if (e.data.action === "completed_pow") onCompletedPoW(e);
} }
@ -239,8 +240,11 @@ function resetHtml() {
function ohNo() { function ohNo() {
try { try {
icanhazepoch().then(sec => { icanhazepoch().then(sec => {
if (Math.abs(Date.now() / 1000 - sec) > 10) { const calcDelta = Date.now() - sec * 1000;
if (Math.abs(calcDelta) > 100000) {
addToast("Time isn't synchronized", "Please synchronize your computer time to NTP servers"); addToast("Time isn't synchronized", "Please synchronize your computer time to NTP servers");
deltaTime = calcDelta;
console.log("applying delta time " + deltaTime);
} else { } else {
console.log("time seems synchronized"); console.log("time seems synchronized");
} }

View File

@ -8,7 +8,6 @@ onmessage = function (e) {
} }
function removePending(id) { function removePending(id) {
console.log("removing task" + id);
pending = pending.filter(it => it !== id); pending = pending.filter(it => it !== id);
} }
@ -27,7 +26,7 @@ function listenPoW(e) {
msg = JSON.stringify({ msg = JSON.stringify({
action: "offline_login", action: "offline_login",
username: user, username: user,
date: Date.now(), date: Date.now() - e.data.deltaTime,
rand: Math.random() rand: Math.random()
}); });