Advertisement
GBH007

Bitburner v1

Dec 25th, 2021
1,396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let hacked = { "home": { name: "home" } }
  2. const farmScriptName = "farm.js"
  3. export async function main(ns) {
  4.     if (!ns.fileExists(farmScriptName, "home")) {
  5.         await createFarm(ns)
  6.     }
  7.     const onlyInfo = ns.args.length > 0 && ns.args[0] == "info"
  8.     const reServer = ns.args.length > 0 && ns.args[0] == "reserver"
  9.     await farm(ns, true, onlyInfo, reServer)
  10.     report(ns, Object.values(hacked))
  11.     ns.tprint("FINISH")
  12. }
  13.  
  14. async function farm(ns, logFile, onlyInfo, reServer) {
  15.     let usedServers = {
  16.         "home": { name: "home" },
  17.         "darkweb": { name: "darkweb" }
  18.     }
  19.     const myServers = [...ns.getPurchasedServers(), "home"]
  20.     for (const sName of myServers) {
  21.         usedServers[sName] = { name: sName }
  22.     }
  23.     let maxServer = null
  24.     let toScan = ["home"]
  25.     const player = ns.getPlayer()
  26.     while (true) {
  27.         if (toScan.length < 1) {
  28.             if (maxServer != null && !onlyInfo) {
  29.                 for (const sName of myServers) {
  30.                     const sInfo = ns.getServer(sName)
  31.                     ns.scriptKill(farmScriptName, sName)
  32.                     if (!ns.fileExists(farmScriptName, sName)) {
  33.                         await ns.scp(farmScriptName, "home", sName)
  34.                     }
  35.                     const threads = Math.trunc(sInfo.maxRam / 2.41)
  36.                     ns.exec(farmScriptName, sName, threads, maxServer.hostname)
  37.                 }
  38.             }
  39.             if (logFile) {
  40.                 await fileReport(ns, Object.values(usedServers), "hack-report.txt")
  41.             }
  42.             return
  43.         }
  44.         let servers = ns.scan(toScan.pop())
  45.         for (let i = 0; i < servers.length; i++) {
  46.             let server = servers[i]
  47.             if (server in usedServers) {
  48.                 continue
  49.             }
  50.             toScan.push(server)
  51.             let sInfo = extendServerInfo(ns, ns.getServer(server))
  52.             usedServers[server] = sInfo
  53.             if (player.hacking + 5 < sInfo.requiredHackingSkill) { continue }
  54.             await prepareToHackServer(ns, sInfo)
  55.             if (sInfo.hasAdminRights) {
  56.                 if (maxServer == null || (sInfo.moneyAvailable > 0 && maxServer.cf < sInfo.cf)) {
  57.                     maxServer = sInfo
  58.                 }
  59.                 if (server in hacked) {
  60.                     continue
  61.                 }
  62.                 if (!onlyInfo && !reServer && sInfo.maxRam > 2.41) {
  63.                     ns.killall(server)
  64.                     let copied = await ns.scp(farmScriptName, "home", server)
  65.                     if (copied) {
  66.                         const threads = Math.trunc(sInfo.maxRam / 2.41)
  67.                         ns.exec(farmScriptName, server, threads, server)
  68.                     }
  69.                 }
  70.                 hacked[server] = sInfo
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. function report(ns, data) {
  77.     for (let srv of data) {
  78.         ns.print(srv.name, " admin ", srv.hasAdminRights, " skill ", srv.requiredHackingSkill)
  79.     }
  80. }
  81.  
  82. function extendServerInfo(ns, sInfo) {
  83.     sInfo.hostname ||= ""
  84.     sInfo.moneyMax ||= 0
  85.     sInfo.name = sInfo.hostname
  86.     sInfo.hasAdminRights ||= false
  87.     sInfo.requiredHackingSkill ||= 0
  88.     sInfo.moneyMax ||= 0
  89.     try {
  90.         const moneyCF = sInfo.moneyMax / sInfo.moneyAvailable
  91.         const secCF = sInfo.hackDifficulty / sInfo.minDifficulty
  92.         sInfo.ht = ns.getHackTime(sInfo.hostname) / 1000
  93.         sInfo.gt = ns.growthAnalyze(sInfo.hostname, moneyCF) * ns.getGrowTime(sInfo.hostname) / 1000
  94.         sInfo.wt = secCF / ns.weakenAnalyze(1) * ns.getWeakenTime(sInfo.hostname) / 1000
  95.         sInfo.cf = sInfo.moneyMax / (sInfo.ht + sInfo.gt + sInfo.wt)
  96.     } catch {
  97.         sInfo.ht = 0
  98.         sInfo.gt = 0
  99.         sInfo.wt = 0
  100.         sInfo.cf = 0
  101.     }
  102.     return sInfo
  103. }
  104.  
  105. async function fileReport(ns, data, filename) {
  106.     let res = ["name;hasAdminRights;requiredHackingSkill;moneyMax;ht;gt;wt;cf"]
  107.     for (let srv of data) {
  108.         if (!srv.name || srv.name in [...ns.getPurchasedServers(), "home", "darkweb"]) {
  109.             continue
  110.         }
  111.         srv = extendServerInfo(ns, srv)
  112.         res.push(
  113.             // ns.sprintf("%s admin=%t requared=%d mm=%d ht=%d gt=%d wt=%d cf=%d",
  114.             ns.sprintf("%s;%t;%d;%d;%d;%d;%d;%d",
  115.                 srv.name,
  116.                 srv.hasAdminRights,
  117.                 srv.requiredHackingSkill,
  118.                 srv.moneyMax,
  119.                 srv.ht,
  120.                 srv.gt,
  121.                 srv.wt,
  122.                 srv.cf,
  123.             ))
  124.     }
  125.     await ns.write(filename, res.join("\n"), "w")
  126.  
  127. }
  128.  
  129. async function prepareToHackServer(ns, sInfo) {
  130.     if (!sInfo.sshPortOpen && ns.fileExists("BruteSSH.exe", "home")) {
  131.         await ns.brutessh(sInfo.hostname)
  132.         ns.print(sInfo.hostname, " sshPortOpen")
  133.     }
  134.     if (!sInfo.ftpPortOpen && ns.fileExists("FTPCrack.exe", "home")) {
  135.         await ns.ftpcrack(sInfo.hostname)
  136.         ns.print(sInfo.hostname, " ftpPortOpen")
  137.     }
  138.     if (!sInfo.httpPortOpen && ns.fileExists("HTTPWorm.exe", "home")) {
  139.         await ns.httpworm(sInfo.hostname)
  140.         ns.print(sInfo.hostname, " httpPortOpen")
  141.     }
  142.     if (!sInfo.smtpPortOpen && ns.fileExists("relaySMTP.exe", "home")) {
  143.         await ns.relaysmtp(sInfo.hostname)
  144.         ns.print(sInfo.hostname, " smtpPortOpen")
  145.     }
  146.     if (!sInfo.sqlPortOpen && ns.fileExists("SQLInject.exe", "home")) {
  147.         await ns.sqlinject(sInfo.hostname)
  148.         ns.print(sInfo.hostname, " sqlPortOpen")
  149.     }
  150.     if (!sInfo.hasAdminRights && sInfo.openPortCount >= sInfo.numOpenPortsRequired) {
  151.         try {
  152.             await ns.nuke(sInfo.hostname)
  153.             ns.print(sInfo.hostname, " hasAdminRights")
  154.         } catch {
  155.             ns.print(sInfo.hostname, " hack fail")
  156.         }
  157.     }
  158. }
  159.  
  160.  
  161. async function createFarm(ns) {
  162.     const content = `/** @param {NS} ns **/
  163. export async function main(ns) {
  164.     let target = ns.args[0];
  165.     let moneyThresh = ns.getServerMaxMoney(target) * 0.75;
  166.     let securityThresh = ns.getServerMinSecurityLevel(target) + 5;
  167.     while (true) {
  168.         if (ns.getServerSecurityLevel(target) > securityThresh) {
  169.             await ns.weaken(target);
  170.         } else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
  171.             await ns.grow(target);
  172.         } else {
  173.             await ns.hack(target);
  174.         }
  175.     }
  176. }`
  177.     await ns.write(farmScriptName, content, "w")
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement