diff --git a/haproxy/js/challenge.js b/haproxy/js/challenge.js index 1bd26a6..ef22c6b 100644 --- a/haproxy/js/challenge.js +++ b/haproxy/js/challenge.js @@ -1,9 +1,15 @@ +function updateElem(selector, text) { + document.querySelector(selector) + .innerText = text; +} + function insertError(str) { const ring = document.querySelector('.lds-ring'); const captcha = document.querySelector('#captcha'); (ring || captcha).insertAdjacentHTML('afterend', `
Error: ${str}
`); ring && ring.remove(); captcha && captcha.remove(); + updateElem('.powstatus', ''); } function finishRedirect() { @@ -65,6 +71,7 @@ const powFinished = new Promise((resolve, reject) => { type: argon2.ArgonType.Argon2id, }; console.log('Got pow', pow, 'with difficulty', diff); + const eHashes = Math.pow(16, Math.floor(diff/8)) * ((diff%8)*2); const diffString = '0'.repeat(Math.floor(diff/8)); const combined = pow; const [userkey, challenge, signature] = combined.split("#"); @@ -77,8 +84,18 @@ const powFinished = new Promise((resolve, reject) => { const workerThreads = isTor ? cpuThreads : Math.max(Math.ceil(cpuThreads/2),cpuThreads-1); let finished = false; const messageHandler = (e) => { + if (e.data.length === 1) { + const totalHashes = e.data[0]; //assumes all worker threads are same speed + const elapsedSec = Math.floor((Date.now()-start)/1000); + const hps = Math.floor(totalHashes/elapsedSec); + const requiredSec = Math.floor(eHashes/hps) * 1.5; //estimate 1.5x time + const remainingSec = Math.max(0, Math.floor(requiredSec-elapsedSec)); //dont show negative time + return updateElem('.powstatus', `Proof-of-work: ${hps}H/s, ~${remainingSec}s remaining`); + } if (finished) { return; } finished = true; + const hasCaptcha = document.getElementById('captcha'); + updateElem('.powstatus', `Found proof-of-work solution.${!hasCaptcha?' Submitting...':''}`); workers.forEach(w => w.terminate()); const [workerId, answer] = e.data; console.log('Worker', workerId, 'returned answer', answer, 'in', Date.now()-start+'ms'); diff --git a/haproxy/js/worker.js b/haproxy/js/worker.js index 5ea8aaa..6fd4617 100644 --- a/haproxy/js/worker.js +++ b/haproxy/js/worker.js @@ -4,6 +4,11 @@ onmessage = async function(e) { const [userkey, challenge, diff, diffString, argonOpts, id, threads] = e.data; console.log('Worker thread', id, 'started'); let i = id; + if (id === 0) { + setInterval(() => { + postMessage([i]); + }, 500); + } while(true) { const hash = await argon2.hash({ pass: challenge + i.toString(), diff --git a/src/scripts/hcaptcha.lua b/src/scripts/hcaptcha.lua index 1eee098..7aa099b 100644 --- a/src/scripts/hcaptcha.lua +++ b/src/scripts/hcaptcha.lua @@ -87,6 +87,7 @@ local body_template = [[ @media (prefers-color-scheme:light){:root{--text-color:#333;--bg-color:#EEE}} .h-captcha,.g-recaptcha{min-height:85px;display:block} .red{color:red;font-weight:bold} + .powstatus{color:green;font-weight:bold} a,a:visited{color:var(--text-color)} body,html{height:100%%} body{display:flex;flex-direction:column;background-color:var(--bg-color);color:var(--text-color);font-family:Helvetica,Arial,sans-serif;max-width:1200px;margin:0 auto;padding: 0 20px} @@ -115,6 +116,7 @@ local body_template = [[JavaScript is required on this page.
%s +