natas/index.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-10-05 05:39:07 +00:00
import solve from "./solve";
import { getPasswordFor, savePassword } from "./util";
async function main() {
let prevPassword = "natas0";
for (let i = 0; ; i++) {
console.log(`Solving level ${i}`);
const savedPassword = await getPasswordFor(i);
if (savedPassword) {
console.log("saved");
prevPassword = savedPassword;
continue;
}
const user = `natas${i}`;
const fetchReplacement = async (url: string, init?: RequestInit) => {
const newUrl = new URL(
url,
`http://natas${i}.natas.labs.overthewire.org/`
);
const headers = new Headers(init?.headers);
const authorization = btoa(`${user}:${prevPassword}`);
headers.set("Authorization", `Basic ${authorization}`);
const newInit = { ...init, headers };
console.log(newUrl, user, prevPassword, newInit);
return await fetch(newUrl, newInit);
};
const solveFn = solve[i];
if (!solveFn) throw new Error(`No solution for natas${i} yet.`);
try {
const password = await solveFn({
username: user,
prevPassword,
fetch: fetchReplacement,
});
if (typeof password !== "string") throw new Error("non-string output");
prevPassword = password;
savePassword(i, password);
} catch (e) {
console.log("sad");
console.trace(e);
break;
}
}
}
main();