diff options
Diffstat (limited to 'misc/route.js')
-rw-r--r-- | misc/route.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/misc/route.js b/misc/route.js new file mode 100644 index 0000000..97d814a --- /dev/null +++ b/misc/route.js @@ -0,0 +1,57 @@ +const HOME = "home"; +let target; +export async function main(ns) { + let result = []; + let route = []; + let seen = []; + target = ns.args[0]; // The target server + let backdoor = ns.args[1]; // True for connecting to the server and backdooring + if (target === undefined) { + ns.tprint("No target server specified"); + ns.exit(); + } + if (buildRoute(ns, HOME, route, seen)) { + result = await printRoute(ns, route); + } +} +function buildRoute(ns, parent, route, seen) { + //first time we run we need to add the parent to the list of servers we've seen + if (!seen.includes(parent)) + seen.push(parent); + //add to route + route.push(parent); + const children = ns.scan(parent); + for (const child of children) { + if (seen.includes(child)) { + //already checked + continue; + } + seen.push(child); + if (child == target) { + //found add it to the route and finish recursion + route.push(child); + return true; + } + if (buildRoute(ns, child, route, seen)) { + //target found, finish recursion + return true; + } + else { + //target not found in this branch, remove from route + route.pop(); + } + } + //didn't find target in this route, reset route + route = []; + return false; +} +async function printRoute(ns, route) { + let result = []; + for (const node of route) { + result.push(node); + } + result.push('backdoor'); + // await navigator.clipboard.writeText(result); + ns.tprint(result); + return result; +}
\ No newline at end of file |