summaryrefslogtreecommitdiff
path: root/misc/route.js
blob: 97d814a74620c21c2ff08df083255aa0ffa2696b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}