Advertisement
Rvby1

blue-fire.js

May 8th, 2022
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const PWN_SCRIPT = "pwn-server.js";
  2. const HACK_SCRIPT = "hack-script.js";
  3.  
  4. const SERVER_TO_HACK = "joesguns";
  5.  
  6. let infectedServers = [];
  7.  
  8. /** @param {NS} ns */
  9. export async function main(ns) {
  10.     infectChildServers(ns, "home", 0);
  11. }
  12.  
  13. export async function infectChildServers(ns, serverWhoseChildrenShouldBeInfected, functionDepth) {
  14.     //Since this server is going to get infected, add it to the list of infected servers:
  15.     infectedServers.push(serverWhoseChildrenShouldBeInfected);
  16.     ns.tprint("Servers infected: " + infectedServers);
  17.  
  18.     let childrenOfServer = ns.scan(serverWhoseChildrenShouldBeInfected);
  19.     for(let currentChildServer of childrenOfServer) {
  20.         //If we've already infected the server, then just move on. This shouldn't ever happen, but if loops are introduced, this will save us.
  21.         if(infectedServers.indexOf(currentChildServer) != -1) {
  22.             ns.tprint("Server " + currentChildServer + " is already infected, so it's being skipped.");
  23.             continue;
  24.         }
  25.  
  26.         //If we don't have root access, try to PWN it.
  27.         if(!ns.hasRootAccess(currentChildServer)) {
  28.             ns.exec(PWN_SCRIPT, "home", 1, currentChildServer);
  29.         }
  30.         //If we still don't have root access after trying to PWN it, just move on:
  31.         if(!ns.hasRootAccess(currentChildServer)) {
  32.             continue;
  33.         }
  34.         //Otherwise, clear any current running scripts, then copy our latest hack script over and run it:
  35.         else {
  36.             ns.tprint("Killing all scripts on " + currentChildServer);
  37.             ns.killall(currentChildServer);
  38.             ns.tprint("Copying hack script to " + currentChildServer);
  39.             await ns.scp(HACK_SCRIPT, "home", currentChildServer);
  40.             let numberThreadsToHackWith = determineMaxPossibleHackScriptThreads(ns, currentChildServer);
  41.             ns.tprint("Starting hack script on " + currentChildServer + " with " +  numberThreadsToHackWith + " threads.");
  42.             ns.exec(HACK_SCRIPT, currentChildServer, numberThreadsToHackWith, SERVER_TO_HACK);
  43.             //TODO: Make this recursive. For now, this should only go one layer deep.
  44.         }
  45.     }
  46. }
  47.  
  48. function determineMaxPossibleHackScriptThreads(ns, serverToRunScriptOn) {
  49.     let scriptRamRequirements = ns.getScriptRam(HACK_SCRIPT);
  50.     let currentAvailableRamOnServer = ns.getServerMaxRam(serverToRunScriptOn) - ns.getServerUsedRam(serverToRunScriptOn);
  51.     return Math.floor(currentAvailableRamOnServer / scriptRamRequirements);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement