
* Refactor anubis to split business logic into a lib, and cmd to just be direct usage. * Post-rebase fixes. * Update changelog, remove unnecessary one. * lib: refactor this This is mostly based on my personal preferences for how Go code should be laid out. I'm not sold on the package name "lib" (I'd call it anubis but that would stutter), but people are probably gonna import it as libanubis so it's likely fine. Packages have been "flattened" to centralize implementation with area of concern. This goes against the Java-esque style that many people like, but I think this helps make things simple. Most notably: the dnsbl client (which is a hack) is an internal package until it's made more generic. Then it can be made external. I also fixed the logic such that `go generate` works and rebased on main. * internal/test: run tests iff npx exists and DONT_USE_NETWORK is not set Signed-off-by: Xe Iaso <me@xeiaso.net> * internal/test: install deps Signed-off-by: Xe Iaso <me@xeiaso.net> * .github/workflows: verbose go tests? Signed-off-by: Xe Iaso <me@xeiaso.net> * internal/test: sleep 2 Signed-off-by: Xe Iaso <me@xeiaso.net> * internal/test: nix this test so CI works Signed-off-by: Xe Iaso <me@xeiaso.net> * internal/test: warmup per browser? Signed-off-by: Xe Iaso <me@xeiaso.net> * internal/test: disable for now :( Signed-off-by: Xe Iaso <me@xeiaso.net> * lib/anubis: do not apply bot rules if address check fails Closes #83 --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Co-authored-by: Xe Iaso <me@xeiaso.net>
89 lines
No EOL
2.8 KiB
JavaScript
89 lines
No EOL
2.8 KiB
JavaScript
import processFast from "./proof-of-work.mjs";
|
|
import processSlow from "./proof-of-work-slow.mjs";
|
|
import { testVideo } from "./video.mjs";
|
|
|
|
const algorithms = {
|
|
"fast": processFast,
|
|
"slow": processSlow,
|
|
}
|
|
|
|
// from Xeact
|
|
const u = (url = "", params = {}) => {
|
|
let result = new URL(url, window.location.href);
|
|
Object.entries(params).forEach((kv) => {
|
|
let [k, v] = kv;
|
|
result.searchParams.set(k, v);
|
|
});
|
|
return result.toString();
|
|
};
|
|
|
|
const imageURL = (mood, cacheBuster) =>
|
|
u(`/.within.website/x/cmd/anubis/static/img/${mood}.webp`, { cacheBuster });
|
|
|
|
(async () => {
|
|
const status = document.getElementById('status');
|
|
const image = document.getElementById('image');
|
|
const title = document.getElementById('title');
|
|
const spinner = document.getElementById('spinner');
|
|
const anubisVersion = JSON.parse(document.getElementById('anubis_version').textContent);
|
|
|
|
// const testarea = document.getElementById('testarea');
|
|
|
|
// const videoWorks = await testVideo(testarea);
|
|
// console.log(`videoWorks: ${videoWorks}`);
|
|
|
|
// if (!videoWorks) {
|
|
// title.innerHTML = "Oh no!";
|
|
// status.innerHTML = "Checks failed. Please check your browser's settings and try again.";
|
|
// image.src = imageURL("sad");
|
|
// spinner.innerHTML = "";
|
|
// spinner.style.display = "none";
|
|
// return;
|
|
// }
|
|
|
|
status.innerHTML = 'Calculating...';
|
|
|
|
const { challenge, rules } = await fetch("/.within.website/x/cmd/anubis/api/make-challenge", { method: "POST" })
|
|
.then(r => {
|
|
if (!r.ok) {
|
|
throw new Error("Failed to fetch config");
|
|
}
|
|
return r.json();
|
|
})
|
|
.catch(err => {
|
|
title.innerHTML = "Oh no!";
|
|
status.innerHTML = `Failed to fetch config: ${err.message}`;
|
|
image.src = imageURL("sad", anubisVersion);
|
|
spinner.innerHTML = "";
|
|
spinner.style.display = "none";
|
|
throw err;
|
|
});
|
|
|
|
const process = algorithms[rules.algorithm];
|
|
if (!process) {
|
|
title.innerHTML = "Oh no!";
|
|
status.innerHTML = `Failed to resolve check algorithm. You may want to reload the page.`;
|
|
image.src = imageURL("sad", anubisVersion);
|
|
spinner.innerHTML = "";
|
|
spinner.style.display = "none";
|
|
return;
|
|
}
|
|
|
|
status.innerHTML = `Calculating...<br/>Difficulty: ${rules.report_as}`;
|
|
|
|
const t0 = Date.now();
|
|
const { hash, nonce } = await process(challenge, rules.difficulty);
|
|
const t1 = Date.now();
|
|
console.log({ hash, nonce });
|
|
|
|
title.innerHTML = "Success!";
|
|
status.innerHTML = `Done! Took ${t1 - t0}ms, ${nonce} iterations`;
|
|
image.src = imageURL("happy", anubisVersion);
|
|
spinner.innerHTML = "";
|
|
spinner.style.display = "none";
|
|
|
|
setTimeout(() => {
|
|
const redir = window.location.href;
|
|
window.location.href = u("/.within.website/x/cmd/anubis/api/pass-challenge", { response: hash, nonce, redir, elapsedTime: t1 - t0 });
|
|
}, 250);
|
|
})(); |