97 lines
3.0 KiB
JavaScript
97 lines
3.0 KiB
JavaScript
/** @param {NS} ns */
|
|
export async function main(ns) {
|
|
ns.tail();
|
|
await scanRecursiveWrapper(ns);
|
|
let currentHackingLevel = ns.getHackingLevel();
|
|
let currentArray = [];
|
|
let currentHop = "";
|
|
let serverRoutes = JSON.parse(ns.read("ServerRouteList.txt"));
|
|
let allPaths = getPaths(serverRoutes);
|
|
for (const entry of allPaths) {
|
|
for (const name of entry) {
|
|
if (ns.singularity.connect(name) === false) {
|
|
ns.tprint("Error when trying to connect to: " + currentHop);
|
|
return
|
|
}
|
|
|
|
if (ns.getServer(name).hostname === "CSEC" || ns.getServer(name).hostname === "avmnite-02h" || ns.getServer(name).hostname === "I.I.I.I" || ns.getServer(name).hostname === "run4theh111z" || ns.getServer(name).hostname === "The-Cave") {
|
|
if (!ns.getServer(name).backdoorInstalled) {
|
|
if (ns.getServerRequiredHackingLevel(name) < currentHackingLevel && ns.hasRootAccess(name) === true) {
|
|
ns.print("Trying to backdoor " + name)
|
|
await ns.singularity.installBackdoor(name);
|
|
ns.print("Success on " + name)
|
|
}
|
|
} else { continue }
|
|
}
|
|
}
|
|
}
|
|
ns.singularity.connect("home");
|
|
}
|
|
|
|
function getPaths(obj, path = []) {
|
|
const paths = [];
|
|
for (const key in obj) {
|
|
const newPath = [...path, key];
|
|
paths.push(newPath);
|
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
paths.push(...getPaths(obj[key], newPath));
|
|
}
|
|
}
|
|
return paths;
|
|
}
|
|
|
|
/** @param {NS} ns */
|
|
async function scanRecursiveWrapper(ns) {
|
|
ns.rm("ServerRouteList.txt");
|
|
const home = "home";
|
|
let serverRouteList = { home: {} };
|
|
let knownServers = [];
|
|
let unscanned = [];
|
|
unscanned.push(home);
|
|
knownServers.push(home);
|
|
while (unscanned.length > 0) {
|
|
let currentServer = unscanned.pop();
|
|
let currentChildren = ns.scan(currentServer).filter(element => !knownServers.includes(element));
|
|
knownServers = knownServers.concat(currentChildren);
|
|
let keyPath = findKeyPath(serverRouteList, currentServer);
|
|
let childrenObject = currentChildren.reduce((a, v) => ({ ...a, [v]: {} }), {});
|
|
writeValueToPath(serverRouteList, keyPath, childrenObject);
|
|
for (let i = 0; i < currentChildren.length; i++) {
|
|
let child = currentChildren[i];
|
|
unscanned.push(child);
|
|
}
|
|
}
|
|
ns.write("ServerRouteList.txt", JSON.stringify(serverRouteList), "w");
|
|
}
|
|
|
|
function findKeyPath(json, key) {
|
|
if (typeof json !== 'object' || json === null) {
|
|
return null;
|
|
}
|
|
if (key in json) {
|
|
return key;
|
|
}
|
|
for (const property in json) {
|
|
if (json.hasOwnProperty(property)) {
|
|
const path = findKeyPath(json[property], key);
|
|
if (path !== null) {
|
|
return property + '*' + path;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function writeValueToPath(json, path, value) {
|
|
const parts = path.split('*');
|
|
let currentObject = json;
|
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
const part = parts[i];
|
|
if (currentObject[part] === undefined) {
|
|
currentObject[part] = {};
|
|
}
|
|
currentObject = currentObject[part];
|
|
}
|
|
currentObject[parts[parts.length - 1]] = value;
|
|
}
|