diff --git a/examples/docs-examples/tutorials/running-parallel-tasks/index.mjs b/examples/docs-examples/tutorials/running-parallel-tasks/index.mjs index 71d20b2a9..65aef11ee 100644 --- a/examples/docs-examples/tutorials/running-parallel-tasks/index.mjs +++ b/examples/docs-examples/tutorials/running-parallel-tasks/index.mjs @@ -19,37 +19,35 @@ async function main(args) { const step = Math.floor(keyspace / args.numberOfProviders + 1); const range = [...Array(Math.floor(keyspace / step) + 1).keys()].map((i) => i * step); - const futureResults = range.map(async (skip = 0) => { - return executor.run(async (ctx) => { - const results = await ctx + const findPasswordInRange = async (skip) => { + const password = await executor.run(async (ctx) => { + const [, potfileResult] = await ctx .beginBatch() .run( - `hashcat -a 3 -m 400 '${args.hash}' '${args.mask}' --skip=${skip} --limit=${Math.min( - keyspace, - skip + step, - )} -o pass.potfile`, + `hashcat -a 3 -m 400 '${args.hash}' '${args.mask}' --skip=${skip} --limit=${ + skip + step + } -o pass.potfile || true`, ) - .run("cat pass.potfile") - .end() - .catch((err) => console.error(err)); - if (!results?.[1]?.stdout) return false; - return results?.[1]?.stdout.toString().split(":")[1]; + .run("cat pass.potfile || true") + .end(); + if (!potfileResult.stdout) return false; + // potfile format is: hash:password + return potfileResult.stdout.toString().trim().split(":")[1]; }); - }); - - const results = await Promise.all(futureResults); - - let password = ""; - for (const result of results) { - if (result) { - password = result; - break; + if (!password) { + throw new Error(`Cannot find password in range ${skip} - ${skip + step}`); } + return password; + }; + + try { + const password = await Promise.any(range.map(findPasswordInRange)); + console.log(`Password found: ${password}`); + } catch (err) { + console.log(`Password not found`); + } finally { + await executor.end(); } - - if (!password) console.log("No password found"); - else console.log(`Password found: ${password}`); - await executor.end(); } program diff --git a/examples/yacat/yacat.ts b/examples/yacat/yacat.ts index 139fcf50a..dc1646934 100644 --- a/examples/yacat/yacat.ts +++ b/examples/yacat/yacat.ts @@ -23,33 +23,35 @@ async function main(args) { const range = [...Array(Math.floor(keyspace / step)).keys()].map((i) => i * step); console.log(`Keyspace size computed. Keyspace size = ${keyspace}. Tasks to compute = ${range.length}`); - const futureResults = range.map((skip) => - executor.run(async (ctx) => { - const results = await ctx + const findPasswordInRange = async (skip: number) => { + const password = await executor.run(async (ctx) => { + const [, potfileResult] = await ctx .beginBatch() .run( `hashcat -a 3 -m 400 '${args.hash}' '${args.mask}' --skip=${skip} --limit=${ - skip! + step + skip + step } -o pass.potfile || true`, ) .run("cat pass.potfile || true") .end(); - if (!results?.[1]?.stdout) return false; - return results?.[1]?.stdout.toString().trim().split(":")[1]; - }), - ); - const results = await Promise.all(futureResults); - - let password = ""; - for await (const result of results) { - if (result) { - password = result; - break; + if (!potfileResult.stdout) return false; + // potfile format is: hash:password + return potfileResult.stdout.toString().trim().split(":")[1]; + }); + if (!password) { + throw new Error(`Cannot find password in range ${skip} - ${skip + step}`); } + return password; + }; + + try { + const password = await Promise.any(range.map(findPasswordInRange)); + console.log(`Password found: ${password}`); + } catch (err) { + console.log(`Password not found`); + } finally { + await executor.end(); } - if (!password) console.log("No password found"); - else console.log(`Password found: ${password}`); - await executor.end(); } program diff --git a/tests/e2e/yacat.spec.ts b/tests/e2e/yacat.spec.ts index 22ebfc5dc..966779ebb 100644 --- a/tests/e2e/yacat.spec.ts +++ b/tests/e2e/yacat.spec.ts @@ -34,28 +34,27 @@ describe("Password cracking", function () { if (!keyspace) return; const step = Math.floor(keyspace / 3); const ranges = range(0, keyspace, step); - const futureResults = ranges.map((skip) => - executor.run(async (ctx) => { - const results = await ctx + + const findPasswordInRange = async (skip: number) => { + const password = await executor.run(async (ctx) => { + const [, potfileResult] = await ctx .beginBatch() .run( - `hashcat -a 3 -m 400 '${hash}' '${mask}' --skip=${skip} --limit=${skip! + step} -o pass.potfile -D 1,2`, + `hashcat -a 3 -m 400 '${hash}' '${mask}' --skip=${skip} --limit=${skip + step} -o pass.potfile || true`, ) - .run("cat pass.potfile") + .run("cat pass.potfile || true") .end(); - if (!results?.[1]?.stdout) return false; - return results?.[1]?.stdout.toString().split(":")?.[1]?.trim(); - }), - ); - const results = await Promise.all(futureResults); - let password = ""; - for (const result of results) { - if (result) { - password = result; - break; + if (!potfileResult.stdout) return false; + // potfile format is: hash:password + return potfileResult.stdout.toString().trim().split(":")[1]; + }); + if (!password) { + throw new Error(`Cannot find password in range ${skip} - ${skip + step}`); } - } - expect(password).toEqual("yo"); + return password; + }; + + await expect(Promise.any(ranges.map(findPasswordInRange))).resolves.toEqual("yo"); await executor.end(); }, 1000 * 60 * 5,