Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let hacked = { "home": { name: "home" } }
- const farmScriptName = "farm.js"
- export async function main(ns) {
- if (!ns.fileExists(farmScriptName, "home")) {
- await createFarm(ns)
- }
- const onlyInfo = ns.args.length > 0 && ns.args[0] == "info"
- const reServer = ns.args.length > 0 && ns.args[0] == "reserver"
- await farm(ns, true, onlyInfo, reServer)
- report(ns, Object.values(hacked))
- ns.tprint("FINISH")
- }
- async function farm(ns, logFile, onlyInfo, reServer) {
- let usedServers = {
- "home": { name: "home" },
- "darkweb": { name: "darkweb" }
- }
- const myServers = [...ns.getPurchasedServers(), "home"]
- for (const sName of myServers) {
- usedServers[sName] = { name: sName }
- }
- let maxServer = null
- let toScan = ["home"]
- const player = ns.getPlayer()
- while (true) {
- if (toScan.length < 1) {
- if (maxServer != null && !onlyInfo) {
- for (const sName of myServers) {
- const sInfo = ns.getServer(sName)
- ns.scriptKill(farmScriptName, sName)
- if (!ns.fileExists(farmScriptName, sName)) {
- await ns.scp(farmScriptName, "home", sName)
- }
- const threads = Math.trunc(sInfo.maxRam / 2.41)
- ns.exec(farmScriptName, sName, threads, maxServer.hostname)
- }
- }
- if (logFile) {
- await fileReport(ns, Object.values(usedServers), "hack-report.txt")
- }
- return
- }
- let servers = ns.scan(toScan.pop())
- for (let i = 0; i < servers.length; i++) {
- let server = servers[i]
- if (server in usedServers) {
- continue
- }
- toScan.push(server)
- let sInfo = extendServerInfo(ns, ns.getServer(server))
- usedServers[server] = sInfo
- if (player.hacking + 5 < sInfo.requiredHackingSkill) { continue }
- await prepareToHackServer(ns, sInfo)
- if (sInfo.hasAdminRights) {
- if (maxServer == null || (sInfo.moneyAvailable > 0 && maxServer.cf < sInfo.cf)) {
- maxServer = sInfo
- }
- if (server in hacked) {
- continue
- }
- if (!onlyInfo && !reServer && sInfo.maxRam > 2.41) {
- ns.killall(server)
- let copied = await ns.scp(farmScriptName, "home", server)
- if (copied) {
- const threads = Math.trunc(sInfo.maxRam / 2.41)
- ns.exec(farmScriptName, server, threads, server)
- }
- }
- hacked[server] = sInfo
- }
- }
- }
- }
- function report(ns, data) {
- for (let srv of data) {
- ns.print(srv.name, " admin ", srv.hasAdminRights, " skill ", srv.requiredHackingSkill)
- }
- }
- function extendServerInfo(ns, sInfo) {
- sInfo.hostname ||= ""
- sInfo.moneyMax ||= 0
- sInfo.name = sInfo.hostname
- sInfo.hasAdminRights ||= false
- sInfo.requiredHackingSkill ||= 0
- sInfo.moneyMax ||= 0
- try {
- const moneyCF = sInfo.moneyMax / sInfo.moneyAvailable
- const secCF = sInfo.hackDifficulty / sInfo.minDifficulty
- sInfo.ht = ns.getHackTime(sInfo.hostname) / 1000
- sInfo.gt = ns.growthAnalyze(sInfo.hostname, moneyCF) * ns.getGrowTime(sInfo.hostname) / 1000
- sInfo.wt = secCF / ns.weakenAnalyze(1) * ns.getWeakenTime(sInfo.hostname) / 1000
- sInfo.cf = sInfo.moneyMax / (sInfo.ht + sInfo.gt + sInfo.wt)
- } catch {
- sInfo.ht = 0
- sInfo.gt = 0
- sInfo.wt = 0
- sInfo.cf = 0
- }
- return sInfo
- }
- async function fileReport(ns, data, filename) {
- let res = ["name;hasAdminRights;requiredHackingSkill;moneyMax;ht;gt;wt;cf"]
- for (let srv of data) {
- if (!srv.name || srv.name in [...ns.getPurchasedServers(), "home", "darkweb"]) {
- continue
- }
- srv = extendServerInfo(ns, srv)
- res.push(
- // ns.sprintf("%s admin=%t requared=%d mm=%d ht=%d gt=%d wt=%d cf=%d",
- ns.sprintf("%s;%t;%d;%d;%d;%d;%d;%d",
- srv.name,
- srv.hasAdminRights,
- srv.requiredHackingSkill,
- srv.moneyMax,
- srv.ht,
- srv.gt,
- srv.wt,
- srv.cf,
- ))
- }
- await ns.write(filename, res.join("\n"), "w")
- }
- async function prepareToHackServer(ns, sInfo) {
- if (!sInfo.sshPortOpen && ns.fileExists("BruteSSH.exe", "home")) {
- await ns.brutessh(sInfo.hostname)
- ns.print(sInfo.hostname, " sshPortOpen")
- }
- if (!sInfo.ftpPortOpen && ns.fileExists("FTPCrack.exe", "home")) {
- await ns.ftpcrack(sInfo.hostname)
- ns.print(sInfo.hostname, " ftpPortOpen")
- }
- if (!sInfo.httpPortOpen && ns.fileExists("HTTPWorm.exe", "home")) {
- await ns.httpworm(sInfo.hostname)
- ns.print(sInfo.hostname, " httpPortOpen")
- }
- if (!sInfo.smtpPortOpen && ns.fileExists("relaySMTP.exe", "home")) {
- await ns.relaysmtp(sInfo.hostname)
- ns.print(sInfo.hostname, " smtpPortOpen")
- }
- if (!sInfo.sqlPortOpen && ns.fileExists("SQLInject.exe", "home")) {
- await ns.sqlinject(sInfo.hostname)
- ns.print(sInfo.hostname, " sqlPortOpen")
- }
- if (!sInfo.hasAdminRights && sInfo.openPortCount >= sInfo.numOpenPortsRequired) {
- try {
- await ns.nuke(sInfo.hostname)
- ns.print(sInfo.hostname, " hasAdminRights")
- } catch {
- ns.print(sInfo.hostname, " hack fail")
- }
- }
- }
- async function createFarm(ns) {
- const content = `/** @param {NS} ns **/
- export async function main(ns) {
- let target = ns.args[0];
- let moneyThresh = ns.getServerMaxMoney(target) * 0.75;
- let securityThresh = ns.getServerMinSecurityLevel(target) + 5;
- while (true) {
- if (ns.getServerSecurityLevel(target) > securityThresh) {
- await ns.weaken(target);
- } else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
- await ns.grow(target);
- } else {
- await ns.hack(target);
- }
- }
- }`
- await ns.write(farmScriptName, content, "w")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement