
* 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>
63 lines
No EOL
1.5 KiB
JavaScript
63 lines
No EOL
1.5 KiB
JavaScript
// https://dev.to/ratmd/simple-proof-of-work-in-javascript-3kgm
|
|
|
|
export default function process(data, difficulty = 5, _threads = 1) {
|
|
console.debug("slow algo");
|
|
return new Promise((resolve, reject) => {
|
|
let webWorkerURL = URL.createObjectURL(new Blob([
|
|
'(', processTask(), ')()'
|
|
], { type: 'application/javascript' }));
|
|
|
|
let worker = new Worker(webWorkerURL);
|
|
|
|
worker.onmessage = (event) => {
|
|
worker.terminate();
|
|
resolve(event.data);
|
|
};
|
|
|
|
worker.onerror = (event) => {
|
|
worker.terminate();
|
|
reject();
|
|
};
|
|
|
|
worker.postMessage({
|
|
data,
|
|
difficulty
|
|
});
|
|
|
|
URL.revokeObjectURL(webWorkerURL);
|
|
});
|
|
}
|
|
|
|
function processTask() {
|
|
return function () {
|
|
const sha256 = (text) => {
|
|
const encoded = new TextEncoder().encode(text);
|
|
return crypto.subtle.digest("SHA-256", encoded.buffer)
|
|
.then((result) =>
|
|
Array.from(new Uint8Array(result))
|
|
.map((c) => c.toString(16).padStart(2, "0"))
|
|
.join(""),
|
|
);
|
|
};
|
|
|
|
addEventListener('message', async (event) => {
|
|
let data = event.data.data;
|
|
let difficulty = event.data.difficulty;
|
|
|
|
let hash;
|
|
let nonce = 0;
|
|
do {
|
|
hash = await sha256(data + nonce++);
|
|
} while (hash.substring(0, difficulty) !== Array(difficulty + 1).join('0'));
|
|
|
|
nonce -= 1; // last nonce was post-incremented
|
|
|
|
postMessage({
|
|
hash,
|
|
data,
|
|
difficulty,
|
|
nonce,
|
|
});
|
|
});
|
|
}.toString();
|
|
} |